blob: d5349d6b2c7bc2f0f6b809189310efa666592df0 [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
chiyotsai426c0662019-08-05 16:15:11 -070016#include "av1/common/enums.h"
Tom Finegan60e653d2018-05-22 11:34:58 -070017#include "config/aom_config.h"
Tom Finegan44702c82018-05-22 13:00:39 -070018#include "config/aom_dsp_rtcd.h"
19#include "config/aom_scale_rtcd.h"
Yaowu Xufa3721d2018-07-30 14:38:49 -070020#include "config/av1_rtcd.h"
21
22#include "aom_dsp/aom_dsp_common.h"
23#include "aom_dsp/aom_filter.h"
24#if CONFIG_DENOISE
25#include "aom_dsp/grain_table.h"
26#include "aom_dsp/noise_util.h"
27#include "aom_dsp/noise_model.h"
28#endif
29#include "aom_dsp/psnr.h"
30#if CONFIG_INTERNAL_STATS
31#include "aom_dsp/ssim.h"
32#endif
33#include "aom_ports/aom_timer.h"
34#include "aom_ports/mem.h"
35#include "aom_ports/system_state.h"
36#include "aom_scale/aom_scale.h"
David Turner1539bb02019-01-24 15:28:13 +000037#if CONFIG_BITSTREAM_DEBUG
Yaowu Xufa3721d2018-07-30 14:38:49 -070038#include "aom_util/debug_util.h"
David Turner1539bb02019-01-24 15:28:13 +000039#endif // CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070040
41#include "av1/common/alloccommon.h"
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010042#include "av1/common/cdef.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070043#include "av1/common/filter.h"
44#include "av1/common/idct.h"
45#include "av1/common/reconinter.h"
46#include "av1/common/reconintra.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070047#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070048#include "av1/common/tile_common.h"
49
Ravi Chaudharyc5e74692018-10-08 16:05:38 +053050#include "av1/encoder/av1_multi_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070051#include "av1/encoder/aq_complexity.h"
52#include "av1/encoder/aq_cyclicrefresh.h"
53#include "av1/encoder/aq_variance.h"
54#include "av1/encoder/bitstream.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070055#include "av1/encoder/context_tree.h"
56#include "av1/encoder/encodeframe.h"
57#include "av1/encoder/encodemv.h"
David Turner056f7cd2019-01-07 17:48:13 +000058#include "av1/encoder/encode_strategy.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070059#include "av1/encoder/encoder.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070060#include "av1/encoder/encodetxb.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070061#include "av1/encoder/ethread.h"
62#include "av1/encoder/firstpass.h"
Yaowu Xufa3721d2018-07-30 14:38:49 -070063#include "av1/encoder/grain_test_vectors.h"
RogerZhoucc5d35d2017-08-07 22:20:15 -070064#include "av1/encoder/hash_motion.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070065#include "av1/encoder/mbgraph.h"
David Turner0fa8c492019-02-06 16:38:13 +000066#include "av1/encoder/pass2_strategy.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070067#include "av1/encoder/picklpf.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070068#include "av1/encoder/pickrst.h"
Debargha Mukherjee7166f222017-09-05 21:32:42 -070069#include "av1/encoder/random.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070070#include "av1/encoder/ratectrl.h"
71#include "av1/encoder/rd.h"
Debargha Mukherjeedf713102018-10-02 12:33:32 -070072#include "av1/encoder/rdopt.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070073#include "av1/encoder/segmentation.h"
74#include "av1/encoder/speed_features.h"
Debargha Mukherjee347c64d2019-05-08 13:53:46 -070075#include "av1/encoder/tpl_model.h"
Yue Chen7cae98f2018-08-24 10:43:16 -070076#include "av1/encoder/reconinter_enc.h"
kyslov7b9d0d62018-12-21 11:12:26 -080077#include "av1/encoder/var_based_part.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070078
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000079#define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000080
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080081#if CONFIG_ENTROPY_STATS
82FRAME_COUNTS aggregate_fc;
83#endif // CONFIG_ENTROPY_STATS
84
Yaowu Xuc27fc142016-08-22 16:08:15 -070085#define AM_SEGMENT_ID_INACTIVE 7
86#define AM_SEGMENT_ID_ACTIVE 0
87
Johannb0ef6ff2018-02-08 14:32:21 -080088// Whether to use high precision mv for altref computation.
89#define ALTREF_HIGH_PRECISION_MV 1
90
91// Q threshold for high precision mv. Choose a very high value for now so that
92// HIGH_PRECISION is always chosen.
93#define HIGH_PRECISION_MV_QTHRESH 200
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -070094
Yaowu Xuc27fc142016-08-22 16:08:15 -070095// #define OUTPUT_YUV_REC
Yaowu Xuc27fc142016-08-22 16:08:15 -070096#ifdef OUTPUT_YUV_SKINMAP
97FILE *yuv_skinmap_file = NULL;
98#endif
99#ifdef OUTPUT_YUV_REC
100FILE *yuv_rec_file;
101#define FILE_NAME_LEN 100
102#endif
103
Yaowu Xuf883b422016-08-30 14:01:10 -0700104static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700105 switch (mode) {
106 case NORMAL:
107 *hr = 1;
108 *hs = 1;
109 break;
110 case FOURFIVE:
111 *hr = 4;
112 *hs = 5;
113 break;
114 case THREEFIVE:
115 *hr = 3;
116 *hs = 5;
117 break;
118 case ONETWO:
119 *hr = 1;
120 *hs = 2;
121 break;
122 default:
123 *hr = 1;
124 *hs = 1;
125 assert(0);
126 break;
127 }
128}
129
130// Mark all inactive blocks as active. Other segmentation features may be set
131// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700132static void suppress_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700133 unsigned char *const seg_map = cpi->segmentation_map;
134 int i;
135 if (cpi->active_map.enabled || cpi->active_map.update)
136 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
137 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
138 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
139}
140
Yaowu Xuf883b422016-08-30 14:01:10 -0700141static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700142 struct segmentation *const seg = &cpi->common.seg;
143 unsigned char *const seg_map = cpi->segmentation_map;
144 const unsigned char *const active_map = cpi->active_map.map;
145 int i;
146
147 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
148
149 if (frame_is_intra_only(&cpi->common)) {
150 cpi->active_map.enabled = 0;
151 cpi->active_map.update = 1;
152 }
153
154 if (cpi->active_map.update) {
155 if (cpi->active_map.enabled) {
156 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
157 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700158 av1_enable_segmentation(seg);
159 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700160 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
161 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
162 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
163 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
164
165 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
166 -MAX_LOOP_FILTER);
167 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
168 -MAX_LOOP_FILTER);
169 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
170 -MAX_LOOP_FILTER);
171 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
172 -MAX_LOOP_FILTER);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700173 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700174 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700175 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
176 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
177 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
178 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700179 if (seg->enabled) {
180 seg->update_data = 1;
181 seg->update_map = 1;
182 }
183 }
184 cpi->active_map.update = 0;
185 }
186}
187
Yaowu Xuf883b422016-08-30 14:01:10 -0700188int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
189 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700190 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
191 unsigned char *const active_map_8x8 = cpi->active_map.map;
192 const int mi_rows = cpi->common.mi_rows;
193 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700194 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
195 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700196 cpi->active_map.update = 1;
197 if (new_map_16x16) {
198 int r, c;
199 for (r = 0; r < mi_rows; ++r) {
200 for (c = 0; c < mi_cols; ++c) {
201 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700202 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700203 ? AM_SEGMENT_ID_ACTIVE
204 : AM_SEGMENT_ID_INACTIVE;
205 }
206 }
207 cpi->active_map.enabled = 1;
208 } else {
209 cpi->active_map.enabled = 0;
210 }
211 return 0;
212 } else {
213 return -1;
214 }
215}
216
Yaowu Xuf883b422016-08-30 14:01:10 -0700217int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
218 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700219 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
220 new_map_16x16) {
221 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
222 const int mi_rows = cpi->common.mi_rows;
223 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700224 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
225 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
226
Yaowu Xuc27fc142016-08-22 16:08:15 -0700227 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
228 if (cpi->active_map.enabled) {
229 int r, c;
230 for (r = 0; r < mi_rows; ++r) {
231 for (c = 0; c < mi_cols; ++c) {
232 // Cyclic refresh segments are considered active despite not having
233 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700234 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700235 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
236 }
237 }
238 }
239 return 0;
240 } else {
241 return -1;
242 }
243}
244
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800245// Compute the horizontal frequency components' energy in a frame
246// by calculuating the 16x4 Horizontal DCT. This is to be used to
247// decide the superresolution parameters.
Yaowu Xubedbf4f2019-05-01 17:54:36 -0700248static void analyze_hor_freq(const AV1_COMP *cpi, double *energy) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800249 uint64_t freq_energy[16] = { 0 };
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800250 const YV12_BUFFER_CONFIG *buf = cpi->source;
251 const int bd = cpi->td.mb.e_mbd.bd;
252 const int width = buf->y_crop_width;
253 const int height = buf->y_crop_height;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800254 DECLARE_ALIGNED(16, int32_t, coeff[16 * 4]);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800255 int n = 0;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800256 memset(freq_energy, 0, sizeof(freq_energy));
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800257 if (buf->flags & YV12_FLAG_HIGHBITDEPTH) {
258 const int16_t *src16 = (const int16_t *)CONVERT_TO_SHORTPTR(buf->y_buffer);
259 for (int i = 0; i < height - 4; i += 4) {
260 for (int j = 0; j < width - 16; j += 16) {
261 av1_fwd_txfm2d_16x4(src16 + i * buf->y_stride + j, coeff, buf->y_stride,
262 H_DCT, bd);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800263 for (int k = 1; k < 16; ++k) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800264 const uint64_t this_energy =
265 ((int64_t)coeff[k] * coeff[k]) +
266 ((int64_t)coeff[k + 16] * coeff[k + 16]) +
267 ((int64_t)coeff[k + 32] * coeff[k + 32]) +
268 ((int64_t)coeff[k + 48] * coeff[k + 48]);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800269 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2 + 2 * (bd - 8));
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800270 }
271 n++;
272 }
273 }
274 } else {
Debargha Mukherjeeac28c722018-11-14 22:09:46 -0800275 assert(bd == 8);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800276 DECLARE_ALIGNED(16, int16_t, src16[16 * 4]);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800277 for (int i = 0; i < height - 4; i += 4) {
278 for (int j = 0; j < width - 16; j += 16) {
279 for (int ii = 0; ii < 4; ++ii)
280 for (int jj = 0; jj < 16; ++jj)
281 src16[ii * 16 + jj] =
282 buf->y_buffer[(i + ii) * buf->y_stride + (j + jj)];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800283 av1_fwd_txfm2d_16x4(src16, coeff, 16, H_DCT, bd);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800284 for (int k = 1; k < 16; ++k) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800285 const uint64_t this_energy =
286 ((int64_t)coeff[k] * coeff[k]) +
287 ((int64_t)coeff[k + 16] * coeff[k + 16]) +
288 ((int64_t)coeff[k + 32] * coeff[k + 32]) +
289 ((int64_t)coeff[k + 48] * coeff[k + 48]);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800290 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800291 }
292 n++;
293 }
294 }
295 }
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800296 if (n) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800297 for (int k = 1; k < 16; ++k) energy[k] = (double)freq_energy[k] / n;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800298 // Convert to cumulative energy
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800299 for (int k = 14; k > 0; --k) energy[k] += energy[k + 1];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800300 } else {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800301 for (int k = 1; k < 16; ++k) energy[k] = 1e+20;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800302 }
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800303}
304
Yaowu Xu45295c32018-03-29 12:06:10 -0700305static void set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv,
306 int cur_frame_force_integer_mv) {
James Zern01a9d702017-08-25 19:09:33 +0000307 MACROBLOCK *const mb = &cpi->td.mb;
Hui Su50361152018-03-02 11:01:42 -0800308 cpi->common.allow_high_precision_mv =
309 allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Rupert Swarbricka84faf22017-12-11 13:56:40 +0000310 const int copy_hp =
311 cpi->common.allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Jingning Hanf050fc12018-03-09 14:53:33 -0800312 int *(*src)[2] = copy_hp ? &mb->nmvcost_hp : &mb->nmvcost;
313 mb->mv_cost_stack = *src;
James Zern01a9d702017-08-25 19:09:33 +0000314}
315
Yaowu Xuf883b422016-08-30 14:01:10 -0700316static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Urvang Joshie4530f82018-01-09 11:43:37 -0800317 const AV1_COMMON *const cm = &cpi->common;
318
Yaowu Xuf883b422016-08-30 14:01:10 -0700319 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700320 return BLOCK_64X64;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100321#if CONFIG_FILEOPTIONS
Urvang Joshie4530f82018-01-09 11:43:37 -0800322 if (cm->options && cm->options->ext_partition)
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100323#endif
324 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
325 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700326
Yaowu Xuf883b422016-08-30 14:01:10 -0700327 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700328
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100329// TODO(any): Possibly could improve this with a heuristic.
330#if CONFIG_FILEOPTIONS
Urvang Joshie4530f82018-01-09 11:43:37 -0800331 if (cm->options && !cm->options->ext_partition) return BLOCK_64X64;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100332#endif
Urvang Joshie4530f82018-01-09 11:43:37 -0800333
Urvang Joshiaab74432018-06-01 12:06:22 -0700334 // When superres / resize is on, 'cm->width / height' can change between
Hui Su3e3b9342019-04-12 18:27:28 +0000335 // calls, so we don't apply this heuristic there.
336 // Things break if superblock size changes between the first pass and second
337 // pass encoding, which is why this heuristic is not configured as a
338 // speed-feature.
Urvang Joshiaab74432018-06-01 12:06:22 -0700339 if (cpi->oxcf.superres_mode == SUPERRES_NONE &&
Hui Su3e3b9342019-04-12 18:27:28 +0000340 cpi->oxcf.resize_mode == RESIZE_NONE && cpi->oxcf.speed >= 1) {
341 return AOMMIN(cm->width, cm->height) > 480 ? BLOCK_128X128 : BLOCK_64X64;
Urvang Joshie4530f82018-01-09 11:43:37 -0800342 }
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
Hui Sueb4b7de2019-04-03 11:00:18 -0700360 if ((cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) ||
361 frame_is_sframe(cm)) {
362 if (!cpi->seq_params_locked) {
363 set_sb_size(&cm->seq_params, select_sb_size(cpi));
364 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700365 } else {
David Turnera21966b2018-12-05 14:48:49 +0000366 const RefCntBuffer *const primary_ref_buf = get_primary_ref_frame_buf(cm);
367 if (primary_ref_buf == NULL) {
David Barkercc615a82018-03-19 14:38:51 +0000368 av1_setup_past_independence(cm);
369 cm->seg.update_map = 1;
370 cm->seg.update_data = 1;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700371 } else {
David Turnera21966b2018-12-05 14:48:49 +0000372 *cm->fc = primary_ref_buf->frame_context;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700373 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700374 }
375
David Turnerbc0993e2019-02-15 14:42:23 +0000376 av1_zero(cm->cur_frame->interp_filter_selected);
David Turnera21966b2018-12-05 14:48:49 +0000377 cm->prev_frame = get_primary_ref_frame_buf(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700378 cpi->vaq_refresh = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700379}
380
Cheng Chen46f30c72017-09-07 11:13:33 -0700381static void enc_setup_mi(AV1_COMMON *cm) {
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530382 int mi_rows_sb_aligned = calc_mi_size(cm->mi_rows);
chiyotsai04ca87d2019-05-24 15:06:19 -0700383 memset(cm->mi, 0, cm->mi_stride * mi_rows_sb_aligned * sizeof(*cm->mi));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700384
385 memset(cm->mi_grid_base, 0,
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530386 cm->mi_stride * mi_rows_sb_aligned * sizeof(*cm->mi_grid_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700387}
388
Cheng Chen46f30c72017-09-07 11:13:33 -0700389static int enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
chiyotsai04ca87d2019-05-24 15:06:19 -0700390 cm->mi = aom_calloc(mi_size, sizeof(*cm->mi));
391 if (!cm->mi) return 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700392 cm->mi_alloc_size = mi_size;
393
Yue Chen53b53f02018-03-29 14:31:23 -0700394 cm->mi_grid_base =
395 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700396 if (!cm->mi_grid_base) return 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700397
398 return 0;
399}
400
Cheng Chen46f30c72017-09-07 11:13:33 -0700401static void enc_free_mi(AV1_COMMON *cm) {
chiyotsai04ca87d2019-05-24 15:06:19 -0700402 aom_free(cm->mi);
403 cm->mi = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700404 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700405 cm->mi_grid_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700406 cm->mi_alloc_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700407}
408
Yaowu Xuf883b422016-08-30 14:01:10 -0700409void av1_initialize_enc(void) {
Wan-Teh Chang3cac4542018-06-29 10:21:39 -0700410 av1_rtcd();
411 aom_dsp_rtcd();
412 aom_scale_rtcd();
413 av1_init_intra_predictors();
414 av1_init_me_luts();
415 av1_rc_init_minq_luts();
416 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700417}
418
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700419static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
420 if (cpi->mbmi_ext_base) {
421 aom_free(cpi->mbmi_ext_base);
422 cpi->mbmi_ext_base = NULL;
423 }
424}
425
426static void alloc_context_buffers_ext(AV1_COMP *cpi) {
427 AV1_COMMON *cm = &cpi->common;
chiyotsai426c0662019-08-05 16:15:11 -0700428 const int is_4k_or_larger = AOMMIN(cm->width, cm->height) >= 2160;
429
430 cpi->mi_alloc_bsize = is_4k_or_larger ? BLOCK_8X8 : BLOCK_4X4;
431 cpi->mi_alloc_size_1d = mi_size_wide[cpi->mi_alloc_bsize];
432 cpi->mi_alloc_rows =
433 (cm->mi_rows + cpi->mi_alloc_size_1d - 1) / cpi->mi_alloc_size_1d;
434 cpi->mi_alloc_cols =
435 (cm->mi_cols + cpi->mi_alloc_size_1d - 1) / cpi->mi_alloc_size_1d;
436
437 assert(mi_size_wide[cpi->mi_alloc_bsize] ==
438 mi_size_high[cpi->mi_alloc_bsize]);
439
440 const int alloc_mi_size = cpi->mi_alloc_rows * cpi->mi_alloc_cols;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700441
442 dealloc_context_buffers_ext(cpi);
443 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
chiyotsai426c0662019-08-05 16:15:11 -0700444 aom_calloc(alloc_mi_size, sizeof(*cpi->mbmi_ext_base)));
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700445}
446
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800447static void reset_film_grain_chroma_params(aom_film_grain_t *pars) {
448 pars->num_cr_points = 0;
449 pars->cr_mult = 0;
450 pars->cr_luma_mult = 0;
451 memset(pars->scaling_points_cr, 0, sizeof(pars->scaling_points_cr));
452 memset(pars->ar_coeffs_cr, 0, sizeof(pars->ar_coeffs_cr));
453 pars->num_cb_points = 0;
454 pars->cb_mult = 0;
455 pars->cb_luma_mult = 0;
Yaowu Xufda7dcb2019-01-16 13:04:33 -0800456 pars->chroma_scaling_from_luma = 0;
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800457 memset(pars->scaling_points_cb, 0, sizeof(pars->scaling_points_cb));
458 memset(pars->ar_coeffs_cb, 0, sizeof(pars->ar_coeffs_cb));
459}
460
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800461static void update_film_grain_parameters(struct AV1_COMP *cpi,
462 const AV1EncoderConfig *oxcf) {
463 AV1_COMMON *const cm = &cpi->common;
464 cpi->oxcf = *oxcf;
465
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700466 if (cpi->film_grain_table) {
467 aom_film_grain_table_free(cpi->film_grain_table);
468 aom_free(cpi->film_grain_table);
469 cpi->film_grain_table = NULL;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700470 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700471
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800472 if (oxcf->film_grain_test_vector) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700473 cm->seq_params.film_grain_params_present = 1;
David Turnerd2a592e2018-11-16 14:59:31 +0000474 if (cm->current_frame.frame_type == KEY_FRAME) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800475 memcpy(&cm->film_grain_params,
476 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
477 sizeof(cm->film_grain_params));
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800478 if (oxcf->monochrome)
479 reset_film_grain_chroma_params(&cm->film_grain_params);
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700480 cm->film_grain_params.bit_depth = cm->seq_params.bit_depth;
481 if (cm->seq_params.color_range == AOM_CR_FULL_RANGE) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800482 cm->film_grain_params.clip_to_restricted_range = 0;
483 }
484 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700485 } else if (oxcf->film_grain_table_filename) {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800486 cm->seq_params.film_grain_params_present = 1;
487
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700488 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
489 memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700490
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700491 aom_film_grain_table_read(cpi->film_grain_table,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700492 oxcf->film_grain_table_filename, &cm->error);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800493 } else {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800494#if CONFIG_DENOISE
495 cm->seq_params.film_grain_params_present = (cpi->oxcf.noise_level > 0);
496#else
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700497 cm->seq_params.film_grain_params_present = 0;
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800498#endif
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800499 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
500 }
501}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800502
Yaowu Xuf883b422016-08-30 14:01:10 -0700503static void dealloc_compressor_data(AV1_COMP *cpi) {
504 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000505 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700506
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700507 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700508
Yaowu Xuf883b422016-08-30 14:01:10 -0700509 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700510 cpi->tile_data = NULL;
511
512 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700513 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700514 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700515
Yaowu Xuf883b422016-08-30 14:01:10 -0700516 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700517 cpi->cyclic_refresh = NULL;
518
Yaowu Xuf883b422016-08-30 14:01:10 -0700519 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700520 cpi->active_map.map = NULL;
521
sdengc23c7f12019-06-11 16:56:50 -0700522 aom_free(cpi->ssim_rdmult_scaling_factors);
523 cpi->ssim_rdmult_scaling_factors = NULL;
524
Jingning Hand064cf02017-06-01 10:00:39 -0700525 aom_free(cpi->td.mb.above_pred_buf);
526 cpi->td.mb.above_pred_buf = NULL;
527
528 aom_free(cpi->td.mb.left_pred_buf);
529 cpi->td.mb.left_pred_buf = NULL;
530
531 aom_free(cpi->td.mb.wsrc_buf);
532 cpi->td.mb.wsrc_buf = NULL;
533
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530534 aom_free(cpi->td.mb.inter_modes_info);
535 cpi->td.mb.inter_modes_info = NULL;
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530536
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530537 for (int i = 0; i < 2; i++)
538 for (int j = 0; j < 2; j++) {
539 aom_free(cpi->td.mb.hash_value_buffer[i][j]);
540 cpi->td.mb.hash_value_buffer[i][j] = NULL;
541 }
Jingning Hand064cf02017-06-01 10:00:39 -0700542 aom_free(cpi->td.mb.mask_buf);
543 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700544
Jingning Han6cc1fd32017-10-13 09:05:36 -0700545 aom_free(cm->tpl_mvs);
546 cm->tpl_mvs = NULL;
Jingning Han6cc1fd32017-10-13 09:05:36 -0700547
Yaowu Xuf883b422016-08-30 14:01:10 -0700548 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700549 av1_free_txb_buf(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700550 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700551
Yaowu Xuf883b422016-08-30 14:01:10 -0700552 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuf883b422016-08-30 14:01:10 -0700553 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800554 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuf883b422016-08-30 14:01:10 -0700555 aom_free_frame_buffer(&cpi->scaled_source);
556 aom_free_frame_buffer(&cpi->scaled_last_source);
557 aom_free_frame_buffer(&cpi->alt_ref_buffer);
Cheng Chen7abe3132019-06-19 11:55:28 -0700558 aom_free_frame_buffer(&cpi->source_kf_buffer);
Yaowu Xuf883b422016-08-30 14:01:10 -0700559 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700560
Yaowu Xuf883b422016-08-30 14:01:10 -0700561 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700562 cpi->tile_tok[0][0] = 0;
563
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530564 aom_free(cpi->tplist[0][0]);
565 cpi->tplist[0][0] = NULL;
566
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000567 av1_free_pc_tree(&cpi->td, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700568
hui sud9a812b2017-07-06 14:34:37 -0700569 aom_free(cpi->td.mb.palette_buffer);
Hui Su38711e72019-06-11 10:49:47 -0700570 av1_release_compound_type_rd_buffers(&cpi->td.mb.comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700571 aom_free(cpi->td.mb.tmp_conv_dst);
572 for (int j = 0; j < 2; ++j) {
573 aom_free(cpi->td.mb.tmp_obmc_bufs[j]);
574 }
575
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700576#if CONFIG_DENOISE
577 if (cpi->denoise_and_model) {
578 aom_denoise_and_model_free(cpi->denoise_and_model);
579 cpi->denoise_and_model = NULL;
580 }
581#endif
582 if (cpi->film_grain_table) {
583 aom_film_grain_table_free(cpi->film_grain_table);
584 cpi->film_grain_table = NULL;
585 }
Hui Suc3a8d372019-05-28 15:52:45 -0700586
587 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
588 aom_free(cpi->level_info[i]);
589 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700590}
591
Yaowu Xuf883b422016-08-30 14:01:10 -0700592static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700593 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700594 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700595
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200596 // Stores a snapshot of key state variables which can subsequently be
597 // restored with a call to av1_restore_coding_context. These functions are
598 // intended for use in a re-code loop in av1_compress_frame where the
599 // quantizer value is adjusted between loop iterations.
Jingning Hanf050fc12018-03-09 14:53:33 -0800600 av1_copy(cc->nmv_vec_cost, cpi->td.mb.nmv_vec_cost);
601 av1_copy(cc->nmv_costs, cpi->nmv_costs);
602 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000603
Yaowu Xuc27fc142016-08-22 16:08:15 -0700604 cc->fc = *cm->fc;
605}
606
Yaowu Xuf883b422016-08-30 14:01:10 -0700607static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700608 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700609 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700610
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200611 // Restore key state variables to the snapshot state stored in the
612 // previous call to av1_save_coding_context.
Jingning Hanf050fc12018-03-09 14:53:33 -0800613 av1_copy(cpi->td.mb.nmv_vec_cost, cc->nmv_vec_cost);
614 av1_copy(cpi->nmv_costs, cc->nmv_costs);
615 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000616
Yaowu Xuc27fc142016-08-22 16:08:15 -0700617 *cm->fc = cc->fc;
618}
619
Yaowu Xuf883b422016-08-30 14:01:10 -0700620static void configure_static_seg_features(AV1_COMP *cpi) {
621 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700622 const RATE_CONTROL *const rc = &cpi->rc;
623 struct segmentation *const seg = &cm->seg;
624
625 int high_q = (int)(rc->avg_q > 48.0);
626 int qi_delta;
627
628 // Disable and clear down for KF
David Turnerd2a592e2018-11-16 14:59:31 +0000629 if (cm->current_frame.frame_type == KEY_FRAME) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700630 // Clear down the global segmentation map
631 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
632 seg->update_map = 0;
633 seg->update_data = 0;
634 cpi->static_mb_pct = 0;
635
636 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700637 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700638
639 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700640 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700641 } else if (cpi->refresh_alt_ref_frame) {
642 // If this is an alt ref frame
643 // Clear down the global segmentation map
644 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
645 seg->update_map = 0;
646 seg->update_data = 0;
647 cpi->static_mb_pct = 0;
648
649 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700650 av1_disable_segmentation(seg);
651 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700652
Jerome Jiang6acbfdc2019-05-30 10:05:17 -0700653#if !CONFIG_REALTIME_ONLY
Yaowu Xuc27fc142016-08-22 16:08:15 -0700654 // Scan frames from current to arf frame.
655 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700656 av1_update_mbgraph_stats(cpi);
Jerome Jiang6acbfdc2019-05-30 10:05:17 -0700657#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700658
659 // If segmentation was enabled set those features needed for the
660 // arf itself.
661 if (seg->enabled) {
662 seg->update_map = 1;
663 seg->update_data = 1;
664
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700665 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
666 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700667 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700668 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
669 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
670 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
671 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
672
673 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
674 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
675 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
676 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700677
Yaowu Xuf883b422016-08-30 14:01:10 -0700678 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700679 }
680 } else if (seg->enabled) {
681 // All other frames if segmentation has been enabled
682
683 // First normal frame in a valid gf or alt ref group
684 if (rc->frames_since_golden == 0) {
685 // Set up segment features for normal frames in an arf group
686 if (rc->source_alt_ref_active) {
687 seg->update_map = 0;
688 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700690 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
691 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700692 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
693 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700694
Cheng Chend8184da2017-09-26 18:15:22 -0700695 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
696 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
697 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
698 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
699
700 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
701 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
702 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
703 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700704
705 // Segment coding disabled for compred testing
706 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700707 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
708 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
709 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700710 }
711 } else {
712 // Disable segmentation and clear down features if alt ref
713 // is not active for this group
714
Yaowu Xuf883b422016-08-30 14:01:10 -0700715 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700716
717 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
718
719 seg->update_map = 0;
720 seg->update_data = 0;
721
Yaowu Xuf883b422016-08-30 14:01:10 -0700722 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700723 }
724 } else if (rc->is_src_frame_alt_ref) {
725 // Special case where we are coding over the top of a previous
726 // alt ref frame.
727 // Segment coding disabled for compred testing
728
729 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700730 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
731 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700732
733 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700734 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
735 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
736 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
737 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700738
739 // Skip all MBs if high Q (0,0 mv and skip coeffs)
740 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700741 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
742 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700743 }
744 // Enable data update
745 seg->update_data = 1;
746 } else {
747 // All other frames.
748
749 // No updates.. leave things as they are.
750 seg->update_map = 0;
751 seg->update_data = 0;
752 }
753 }
754}
755
Yaowu Xuf883b422016-08-30 14:01:10 -0700756static void update_reference_segmentation_map(AV1_COMP *cpi) {
757 AV1_COMMON *const cm = &cpi->common;
chiyotsai2202ba02019-07-12 16:09:21 -0700758 MB_MODE_INFO **mi_4x4_ptr = cm->mi_grid_base;
David Turnerb757ce02018-11-12 15:01:28 +0000759 uint8_t *cache_ptr = cm->cur_frame->seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700760 int row, col;
761
762 for (row = 0; row < cm->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -0700763 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700764 uint8_t *cache = cache_ptr;
Yushin Choa7f65922018-04-04 16:06:11 -0700765 for (col = 0; col < cm->mi_cols; col++, mi_4x4++, cache++)
766 cache[0] = mi_4x4[0]->segment_id;
767 mi_4x4_ptr += cm->mi_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700768 cache_ptr += cm->mi_cols;
769 }
770}
771
Yaowu Xuf883b422016-08-30 14:01:10 -0700772static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
773 AV1_COMMON *cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700774 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -0700775 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700776
Satish Kumar Suman7a7239b2019-03-13 14:48:14 +0530777 if (!cpi->lookahead) {
778 int is_scale = (oxcf->resize_mode || oxcf->superres_mode);
Satish Kumar Suman29909962019-01-09 10:31:21 +0530779 cpi->lookahead = av1_lookahead_init(
780 oxcf->width, oxcf->height, seq_params->subsampling_x,
781 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman7a7239b2019-03-13 14:48:14 +0530782 oxcf->lag_in_frames, oxcf->border_in_pixels, is_scale);
783 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700784 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700785 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700786 "Failed to allocate lag buffers");
787
788 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700789 if (aom_realloc_frame_buffer(
790 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
791 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530792 seq_params->use_highbitdepth, oxcf->border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700793 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700794 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700795 "Failed to allocate altref buffer");
Cheng Chen7abe3132019-06-19 11:55:28 -0700796
797 // Allocate frame buffer to hold source frame whey key frame filtering
798 // is applied.
799 if (aom_realloc_frame_buffer(
800 &cpi->source_kf_buffer, oxcf->width, oxcf->height,
801 seq_params->subsampling_x, seq_params->subsampling_y,
802 seq_params->use_highbitdepth, oxcf->border_in_pixels,
803 cm->byte_alignment, NULL, NULL, NULL))
804 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
805 "Failed to allocate altref buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700806}
807
Yaowu Xuf883b422016-08-30 14:01:10 -0700808static void alloc_util_frame_buffers(AV1_COMP *cpi) {
809 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700810 const SequenceHeader *const seq_params = &cm->seq_params;
811 if (aom_realloc_frame_buffer(
812 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
813 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530814 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700815 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700816 "Failed to allocate last frame buffer");
817
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700818 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800819 &cpi->trial_frame_rst, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700820 cm->superres_upscaled_height, seq_params->subsampling_x,
821 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman3b12c002018-12-19 15:27:20 +0530822 AOM_RESTORATION_FRAME_BORDER, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800823 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800824 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700825
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700826 if (aom_realloc_frame_buffer(
827 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
828 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530829 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700830 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700831 "Failed to allocate scaled source buffer");
832
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700833 if (aom_realloc_frame_buffer(
834 &cpi->scaled_last_source, cm->width, cm->height,
835 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530836 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700837 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700838 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700839 "Failed to allocate scaled last source buffer");
840}
841
Cheng Chen46f30c72017-09-07 11:13:33 -0700842static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700843 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000844 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700845
Yaowu Xuf883b422016-08-30 14:01:10 -0700846 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700847
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530848 int mi_rows_aligned_to_sb =
849 ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
850 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
851
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700852 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700853
Yaowu Xuc27fc142016-08-22 16:08:15 -0700854 alloc_context_buffers_ext(cpi);
855
Yaowu Xuf883b422016-08-30 14:01:10 -0700856 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700857
858 {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000859 unsigned int tokens =
860 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700861 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700862 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700863 }
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530864 aom_free(cpi->tplist[0][0]);
865
866 CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
867 aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
868 sizeof(*cpi->tplist[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700869
Yaowu Xuf883b422016-08-30 14:01:10 -0700870 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700871}
872
Yaowu Xuf883b422016-08-30 14:01:10 -0700873void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700874 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700875 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700876}
877
Hui Suef139e12019-05-20 15:51:22 -0700878double av1_get_compression_ratio(const AV1_COMMON *const cm,
879 size_t encoded_frame_size) {
880 const int upscaled_width = cm->superres_upscaled_width;
881 const int height = cm->height;
882 const int luma_pic_size = upscaled_width * height;
883 const SequenceHeader *const seq_params = &cm->seq_params;
884 const BITSTREAM_PROFILE profile = seq_params->profile;
885 const int pic_size_profile_factor =
886 profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
887 encoded_frame_size =
888 (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
889 const size_t uncompressed_frame_size =
890 (luma_pic_size * pic_size_profile_factor) >> 3;
891 return uncompressed_frame_size / (double)encoded_frame_size;
892}
893
Yunqing Wang75e20e82018-06-16 12:10:48 -0700894static void set_tile_info(AV1_COMP *cpi) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200895 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +0200896 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200897
898 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200899
900 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +0200901 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +0200902 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200903 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
904 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200905 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000906 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
907 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200908 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200909 cm->uniform_tile_spacing_flag = 0;
910 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
911 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200912 size_sb = cpi->oxcf.tile_widths[j++];
913 if (j >= cpi->oxcf.tile_width_count) j = 0;
David Barker6cd5a822018-03-05 16:19:28 +0000914 start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200915 }
916 cm->tile_cols = i;
917 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200918 }
919 av1_calculate_tile_cols(cm);
920
921 // configure tile rows
922 if (cm->uniform_tile_spacing_flag) {
923 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
924 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200925 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000926 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
927 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200928 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200929 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
930 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200931 size_sb = cpi->oxcf.tile_heights[j++];
932 if (j >= cpi->oxcf.tile_height_count) j = 0;
933 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200934 }
935 cm->tile_rows = i;
936 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200937 }
938 av1_calculate_tile_rows(cm);
939}
940
Yaowu Xuf883b422016-08-30 14:01:10 -0700941static void update_frame_size(AV1_COMP *cpi) {
942 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700943 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
944
Yaowu Xuf883b422016-08-30 14:01:10 -0700945 av1_set_mb_mi(cm, cm->width, cm->height);
946 av1_init_context_buffers(cm);
Luc Trudeau1e84af52017-11-25 15:00:28 -0500947 av1_init_macroblockd(cm, xd, NULL);
chiyotsai426c0662019-08-05 16:15:11 -0700948
949 const int alloc_mi_size = cpi->mi_alloc_rows * cpi->mi_alloc_cols;
950 memset(cpi->mbmi_ext_base, 0, alloc_mi_size * sizeof(*cpi->mbmi_ext_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700951 set_tile_info(cpi);
952}
953
Yaowu Xuf883b422016-08-30 14:01:10 -0700954static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700955 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -0700956 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
David Turnera21966b2018-12-05 14:48:49 +0000957 cpi->common.remapped_ref_idx[fb_idx] = fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -0700958 cpi->rate_index = 0;
959 cpi->rate_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700960}
961
Debargha Mukherjee57498692018-05-11 13:29:31 -0700962static INLINE int does_level_match(int width, int height, double fps,
963 int lvl_width, int lvl_height,
964 double lvl_fps, int lvl_dim_mult) {
965 const int64_t lvl_luma_pels = lvl_width * lvl_height;
966 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
967 const int64_t luma_pels = width * height;
968 const double display_sample_rate = luma_pels * fps;
969 return luma_pels <= lvl_luma_pels &&
970 display_sample_rate <= lvl_display_sample_rate &&
971 width <= lvl_width * lvl_dim_mult &&
972 height <= lvl_height * lvl_dim_mult;
973}
974
Andrey Norkin26495512018-06-20 17:13:11 -0700975static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Andrey Norkinf481d982018-05-15 12:05:31 -0700976 const AV1EncoderConfig *oxcf) {
Debargha Mukherjee57498692018-05-11 13:29:31 -0700977 // TODO(any): This is a placeholder function that only addresses dimensions
978 // and max display sample rates.
979 // Need to add checks for max bit rate, max decoded luma sample rate, header
980 // rate, etc. that are not covered by this function.
Hui Su8427ff22019-03-11 10:14:33 -0700981 AV1_LEVEL level = SEQ_LEVEL_MAX;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700982 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
983 288, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700984 level = SEQ_LEVEL_2_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700985 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
986 704, 396, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700987 level = SEQ_LEVEL_2_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700988 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
989 1088, 612, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700990 level = SEQ_LEVEL_3_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700991 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
992 1376, 774, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700993 level = SEQ_LEVEL_3_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700994 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
995 2048, 1152, 30.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -0700996 level = SEQ_LEVEL_4_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700997 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
998 2048, 1152, 60.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -0700999 level = SEQ_LEVEL_4_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001000 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1001 4096, 2176, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001002 level = SEQ_LEVEL_5_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001003 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1004 4096, 2176, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001005 level = SEQ_LEVEL_5_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001006 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1007 4096, 2176, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001008 level = SEQ_LEVEL_5_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001009 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1010 8192, 4352, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001011 level = SEQ_LEVEL_6_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001012 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1013 8192, 4352, 60.0, 2)) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001014 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1015 8192, 4352, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001016 level = SEQ_LEVEL_6_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001017 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1018 16384, 8704, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001019 level = SEQ_LEVEL_7_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001020 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1021 16384, 8704, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001022 level = SEQ_LEVEL_7_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001023 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1024 16384, 8704, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001025 level = SEQ_LEVEL_7_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001026 }
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001027 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su8427ff22019-03-11 10:14:33 -07001028 seq->seq_level_idx[i] = level;
Andrey Norkin26495512018-06-20 17:13:11 -07001029 // Set the maximum parameters for bitrate and buffer size for this profile,
1030 // level, and tier
Yaowu Xu7e450882019-04-30 15:09:18 -07001031 cm->op_params[i].bitrate = av1_max_level_bitrate(
Hui Su8427ff22019-03-11 10:14:33 -07001032 cm->seq_params.profile, seq->seq_level_idx[i], seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001033 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
1034 // check
Andrey Norkin26495512018-06-20 17:13:11 -07001035 if (cm->op_params[i].bitrate == 0)
1036 aom_internal_error(
1037 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
1038 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -07001039 // Buffer size in bits/s is bitrate in bits/s * 1 s
Andrey Norkin26495512018-06-20 17:13:11 -07001040 cm->op_params[i].buffer_size = cm->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001041 }
1042}
1043
Andrey Norkin26495512018-06-20 17:13:11 -07001044static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
Marco Paniconi67142112019-07-24 15:00:31 -07001045 const AV1EncoderConfig *oxcf, int use_svc) {
Yaowu Xu2a9ac432019-08-06 14:21:17 -07001046 seq->still_picture = (oxcf->force_video_mode == 0) && (oxcf->limit == 1);
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001047 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -07001048 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
kyslov94243382019-05-02 15:33:32 -07001049 seq->force_screen_content_tools = (oxcf->mode == REALTIME) ? 0 : 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001050 seq->force_integer_mv = 2;
David Turnerebf96f42018-11-14 16:57:57 +00001051 seq->order_hint_info.enable_order_hint = oxcf->enable_order_hint;
David Turner936235c2018-11-28 13:42:01 +00001052 seq->frame_id_numbers_present_flag =
1053 !(seq->still_picture && seq->reduced_still_picture_hdr) &&
Marco Paniconi67142112019-07-24 15:00:31 -07001054 !oxcf->large_scale_tile && oxcf->error_resilient_mode && !use_svc;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001055 if (seq->still_picture && seq->reduced_still_picture_hdr) {
David Turnerebf96f42018-11-14 16:57:57 +00001056 seq->order_hint_info.enable_order_hint = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001057 seq->force_screen_content_tools = 2;
1058 seq->force_integer_mv = 2;
1059 }
David Turnerebf96f42018-11-14 16:57:57 +00001060 seq->order_hint_info.order_hint_bits_minus_1 =
1061 seq->order_hint_info.enable_order_hint
1062 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
1063 : -1;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001064
David Turner760a2f42018-12-07 15:25:36 +00001065 seq->max_frame_width =
1066 oxcf->forced_max_frame_width ? oxcf->forced_max_frame_width : oxcf->width;
1067 seq->max_frame_height = oxcf->forced_max_frame_height
1068 ? oxcf->forced_max_frame_height
1069 : oxcf->height;
1070 seq->num_bits_width =
1071 (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
1072 seq->num_bits_height =
1073 (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
1074 assert(seq->num_bits_width <= 16);
1075 assert(seq->num_bits_height <= 16);
1076
1077 seq->frame_id_length = FRAME_ID_LENGTH;
1078 seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
1079
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001080 seq->enable_dual_filter = oxcf->enable_dual_filter;
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -08001081 seq->order_hint_info.enable_dist_wtd_comp = oxcf->enable_dist_wtd_comp;
1082 seq->order_hint_info.enable_dist_wtd_comp &=
David Turnerebf96f42018-11-14 16:57:57 +00001083 seq->order_hint_info.enable_order_hint;
1084 seq->order_hint_info.enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1085 seq->order_hint_info.enable_ref_frame_mvs &=
1086 seq->order_hint_info.enable_order_hint;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001087 seq->enable_superres = oxcf->enable_superres;
1088 seq->enable_cdef = oxcf->enable_cdef;
1089 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -07001090 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -08001091 seq->enable_interintra_compound = oxcf->enable_interintra_comp;
1092 seq->enable_masked_compound = oxcf->enable_masked_comp;
Debargha Mukherjee03c43ba2018-12-14 13:08:08 -08001093 seq->enable_intra_edge_filter = oxcf->enable_intra_edge_filter;
Yue Chen8f9ca582018-12-12 15:11:47 -08001094 seq->enable_filter_intra = oxcf->enable_filter_intra;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001095
Andrey Norkin26495512018-06-20 17:13:11 -07001096 set_bitstream_level_tier(seq, cm, oxcf);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001097
1098 if (seq->operating_points_cnt_minus_1 == 0) {
1099 seq->operating_point_idc[0] = 0;
1100 } else {
1101 // Set operating_point_idc[] such that for the i-th operating point the
1102 // first (operating_points_cnt-i) spatial layers and the first temporal
1103 // layer are decoded Note that highest quality operating point should come
1104 // first
1105 for (int i = 0; i < seq->operating_points_cnt_minus_1 + 1; i++)
1106 seq->operating_point_idc[i] =
1107 (~(~0u << (seq->operating_points_cnt_minus_1 + 1 - i)) << 8) | 1;
1108 }
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001109}
1110
Yaowu Xuf883b422016-08-30 14:01:10 -07001111static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1112 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001113
1114 cpi->oxcf = *oxcf;
1115 cpi->framerate = oxcf->init_framerate;
1116
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001117 cm->seq_params.profile = oxcf->profile;
1118 cm->seq_params.bit_depth = oxcf->bit_depth;
1119 cm->seq_params.use_highbitdepth = oxcf->use_highbitdepth;
1120 cm->seq_params.color_primaries = oxcf->color_primaries;
1121 cm->seq_params.transfer_characteristics = oxcf->transfer_characteristics;
1122 cm->seq_params.matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001123 cm->seq_params.monochrome = oxcf->monochrome;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001124 cm->seq_params.chroma_sample_position = oxcf->chroma_sample_position;
1125 cm->seq_params.color_range = oxcf->color_range;
Andrey Norkin28e9ce22018-01-08 10:11:21 -08001126 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08001127 cm->timing_info.num_units_in_display_tick =
1128 oxcf->timing_info.num_units_in_display_tick;
1129 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
1130 cm->timing_info.equal_picture_interval =
1131 oxcf->timing_info.equal_picture_interval;
1132 cm->timing_info.num_ticks_per_picture =
1133 oxcf->timing_info.num_ticks_per_picture;
1134
Andrey Norkin26495512018-06-20 17:13:11 -07001135 cm->seq_params.display_model_info_present_flag =
1136 oxcf->display_model_info_present_flag;
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001137 cm->seq_params.decoder_model_info_present_flag =
1138 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08001139 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001140 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08001141 cm->buffer_model.num_units_in_decoding_tick =
1142 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07001143 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07001144 av1_set_aom_dec_model_info(&cm->buffer_model);
1145 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07001146 } else if (cm->timing_info_present &&
1147 cm->timing_info.equal_picture_interval &&
1148 !cm->seq_params.decoder_model_info_present_flag) {
1149 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07001150 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001151 } else {
1152 cm->op_params[0].initial_display_delay =
1153 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08001154 }
Andrey Norkinc7511de2018-06-22 12:31:06 -07001155
Tom Fineganf8d6a162018-08-21 10:47:55 -07001156 if (cm->seq_params.monochrome) {
1157 cm->seq_params.subsampling_x = 1;
1158 cm->seq_params.subsampling_y = 1;
1159 } else if (cm->seq_params.color_primaries == AOM_CICP_CP_BT_709 &&
1160 cm->seq_params.transfer_characteristics == AOM_CICP_TC_SRGB &&
1161 cm->seq_params.matrix_coefficients == AOM_CICP_MC_IDENTITY) {
1162 cm->seq_params.subsampling_x = 0;
1163 cm->seq_params.subsampling_y = 0;
1164 } else {
1165 if (cm->seq_params.profile == 0) {
1166 cm->seq_params.subsampling_x = 1;
1167 cm->seq_params.subsampling_y = 1;
1168 } else if (cm->seq_params.profile == 1) {
1169 cm->seq_params.subsampling_x = 0;
1170 cm->seq_params.subsampling_y = 0;
1171 } else {
1172 if (cm->seq_params.bit_depth == AOM_BITS_12) {
1173 cm->seq_params.subsampling_x = oxcf->chroma_subsampling_x;
1174 cm->seq_params.subsampling_y = oxcf->chroma_subsampling_y;
1175 } else {
1176 cm->seq_params.subsampling_x = 1;
1177 cm->seq_params.subsampling_y = 0;
1178 }
1179 }
Tom Finegan02b2a842018-08-24 13:50:00 -07001180 }
1181
Yaowu Xuc27fc142016-08-22 16:08:15 -07001182 cm->width = oxcf->width;
1183 cm->height = oxcf->height;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001184 set_sb_size(&cm->seq_params,
1185 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -07001186 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -07001187
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08001188 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001189
1190 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -07001191 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001192
Marco Paniconi67142112019-07-24 15:00:31 -07001193 cpi->use_svc = 0;
Marco Paniconid8574e32019-08-04 21:30:12 -07001194 cpi->svc.external_ref_frame_config = 0;
1195 cpi->svc.non_reference_frame = 0;
Marco Paniconi67142112019-07-24 15:00:31 -07001196 cm->number_spatial_layers = 1;
1197 cm->number_temporal_layers = 1;
1198
Yaowu Xuc27fc142016-08-22 16:08:15 -07001199 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001200 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001201
1202 cpi->static_mb_pct = 0;
1203 cpi->ref_frame_flags = 0;
1204
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001205 // Reset resize pending flags
1206 cpi->resize_pending_width = 0;
1207 cpi->resize_pending_height = 0;
1208
Yaowu Xuc27fc142016-08-22 16:08:15 -07001209 init_buffer_indices(cpi);
1210}
1211
1212static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001213 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001214 const int64_t bandwidth = oxcf->target_bandwidth;
1215 const int64_t starting = oxcf->starting_buffer_level_ms;
1216 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1217 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1218
1219 rc->starting_buffer_level = starting * bandwidth / 1000;
1220 rc->optimal_buffer_level =
1221 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1222 rc->maximum_buffer_size =
1223 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1224}
1225
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001226#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1227 cpi->fn_ptr[BT].sdf = SDF; \
1228 cpi->fn_ptr[BT].sdaf = SDAF; \
1229 cpi->fn_ptr[BT].vf = VF; \
1230 cpi->fn_ptr[BT].svf = SVF; \
1231 cpi->fn_ptr[BT].svaf = SVAF; \
1232 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1233 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001234 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001235
1236#define MAKE_BFP_SAD_WRAPPER(fnname) \
1237 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1238 int source_stride, \
1239 const uint8_t *ref_ptr, int ref_stride) { \
1240 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1241 } \
1242 static unsigned int fnname##_bits10( \
1243 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1244 int ref_stride) { \
1245 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1246 } \
1247 static unsigned int fnname##_bits12( \
1248 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1249 int ref_stride) { \
1250 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1251 }
1252
1253#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1254 static unsigned int fnname##_bits8( \
1255 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1256 int ref_stride, const uint8_t *second_pred) { \
1257 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1258 } \
1259 static unsigned int fnname##_bits10( \
1260 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1261 int ref_stride, const uint8_t *second_pred) { \
1262 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1263 2; \
1264 } \
1265 static unsigned int fnname##_bits12( \
1266 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1267 int ref_stride, const uint8_t *second_pred) { \
1268 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1269 4; \
1270 }
1271
Yaowu Xuc27fc142016-08-22 16:08:15 -07001272#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1273 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1274 const uint8_t *const ref_ptr[], int ref_stride, \
1275 unsigned int *sad_array) { \
1276 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1277 } \
1278 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1279 const uint8_t *const ref_ptr[], int ref_stride, \
1280 unsigned int *sad_array) { \
1281 int i; \
1282 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1283 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1284 } \
1285 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1286 const uint8_t *const ref_ptr[], int ref_stride, \
1287 unsigned int *sad_array) { \
1288 int i; \
1289 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1290 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1291 }
1292
Cheng Chenbf3d4962017-11-01 14:48:52 -07001293#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1294 static unsigned int fnname##_bits8( \
1295 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1296 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001297 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001298 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1299 jcp_param); \
1300 } \
1301 static unsigned int fnname##_bits10( \
1302 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1303 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001304 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001305 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1306 jcp_param) >> \
1307 2; \
1308 } \
1309 static unsigned int fnname##_bits12( \
1310 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1311 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001312 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001313 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1314 jcp_param) >> \
1315 4; \
1316 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001317
Yaowu Xuf883b422016-08-30 14:01:10 -07001318MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1319MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001320MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1321MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1322MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1323MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1324MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1325MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1326MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001327MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1328MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1329MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1330MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1331MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1332MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1333MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1334MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1335MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1336MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1337MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1338MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1339MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1340MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001341MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1342MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1343MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001344MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1345MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1346MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001347MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1348MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1349MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001350MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1351MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1352MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001353MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1354MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1355MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001356MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1357MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1358MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001359MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1360MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1361MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001362MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1363MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1364MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001365MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001366
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001367MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1368MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1369MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1370MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1371MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1372MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1373MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1374MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1375MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1376MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1377MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1378MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001379MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1380MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1381MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1382MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1383MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1384MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001385
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001386MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x128_avg)
1387MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x64_avg)
1388MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x128_avg)
1389MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x16_avg)
1390MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x32_avg)
1391MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x32_avg)
1392MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x64_avg)
1393MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x32_avg)
1394MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x64_avg)
1395MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x16_avg)
1396MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x8_avg)
1397MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x16_avg)
1398MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x8_avg)
1399MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x4_avg)
1400MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x8_avg)
1401MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x4_avg)
1402MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x16_avg)
1403MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x4_avg)
1404MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x32_avg)
1405MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x8_avg)
1406MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x64_avg)
1407MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x16_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001408
David Barker0f3c94e2017-05-16 15:21:50 +01001409#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001410 cpi->fn_ptr[BT].msdf = MCSDF; \
1411 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001412
David Barkerc155e012017-05-11 13:54:54 +01001413#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1414 static unsigned int fnname##_bits8( \
1415 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1416 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1417 int m_stride, int invert_mask) { \
1418 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1419 second_pred_ptr, m, m_stride, invert_mask); \
1420 } \
1421 static unsigned int fnname##_bits10( \
1422 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1423 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1424 int m_stride, int invert_mask) { \
1425 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1426 second_pred_ptr, m, m_stride, invert_mask) >> \
1427 2; \
1428 } \
1429 static unsigned int fnname##_bits12( \
1430 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1431 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1432 int m_stride, int invert_mask) { \
1433 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1434 second_pred_ptr, m, m_stride, invert_mask) >> \
1435 4; \
1436 }
1437
David Barkerf19f35f2017-05-22 16:33:22 +01001438MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1439MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1440MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001441MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1442MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1443MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1444MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1445MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1446MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1447MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1448MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1449MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1450MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1451MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1452MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1453MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001454MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1455MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1456MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1457MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001458MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1459MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001460
Yaowu Xuc27fc142016-08-22 16:08:15 -07001461#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1462 cpi->fn_ptr[BT].osdf = OSDF; \
1463 cpi->fn_ptr[BT].ovf = OVF; \
1464 cpi->fn_ptr[BT].osvf = OSVF;
1465
1466#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1467 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1468 const int32_t *wsrc, \
1469 const int32_t *msk) { \
1470 return fnname(ref, ref_stride, wsrc, msk); \
1471 } \
1472 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1473 const int32_t *wsrc, \
1474 const int32_t *msk) { \
1475 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1476 } \
1477 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1478 const int32_t *wsrc, \
1479 const int32_t *msk) { \
1480 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1481 }
1482
Yaowu Xuf883b422016-08-30 14:01:10 -07001483MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1484MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1485MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001486MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1487MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1488MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1489MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1490MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1491MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1492MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1493MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1494MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1495MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1496MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1497MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1498MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001499MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1500MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1501MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1502MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001503MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1504MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001505
Yaowu Xuf883b422016-08-30 14:01:10 -07001506static void highbd_set_var_fns(AV1_COMP *const cpi) {
1507 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001508 if (cm->seq_params.use_highbitdepth) {
1509 switch (cm->seq_params.bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001510 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001511 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1512 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1513 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001514 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001515 aom_highbd_sad64x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001516 aom_highbd_dist_wtd_sad64x16_avg_bits8,
1517 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001518
1519 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1520 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1521 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001522 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001523 aom_highbd_sad16x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001524 aom_highbd_dist_wtd_sad16x64_avg_bits8,
1525 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001526
1527 HIGHBD_BFP(
1528 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1529 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001530 aom_highbd_8_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001531 aom_highbd_sad32x8x4d_bits8, aom_highbd_dist_wtd_sad32x8_avg_bits8,
1532 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001533
1534 HIGHBD_BFP(
1535 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1536 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001537 aom_highbd_8_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001538 aom_highbd_sad8x32x4d_bits8, aom_highbd_dist_wtd_sad8x32_avg_bits8,
1539 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001540
1541 HIGHBD_BFP(
1542 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1543 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001544 aom_highbd_8_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001545 aom_highbd_sad16x4x4d_bits8, aom_highbd_dist_wtd_sad16x4_avg_bits8,
1546 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001547
1548 HIGHBD_BFP(
1549 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1550 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001551 aom_highbd_8_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001552 aom_highbd_sad4x16x4d_bits8, aom_highbd_dist_wtd_sad4x16_avg_bits8,
1553 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001554
1555 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1556 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1557 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001558 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001559 aom_highbd_sad32x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001560 aom_highbd_dist_wtd_sad32x16_avg_bits8,
1561 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001562
1563 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1564 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1565 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001566 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001567 aom_highbd_sad16x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001568 aom_highbd_dist_wtd_sad16x32_avg_bits8,
1569 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001570
1571 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1572 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1573 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001574 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001575 aom_highbd_sad64x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001576 aom_highbd_dist_wtd_sad64x32_avg_bits8,
1577 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001578
1579 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1580 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1581 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001582 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001583 aom_highbd_sad32x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001584 aom_highbd_dist_wtd_sad32x64_avg_bits8,
1585 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001586
1587 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1588 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1589 aom_highbd_8_sub_pixel_variance32x32,
1590 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001591 aom_highbd_sad32x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001592 aom_highbd_dist_wtd_sad32x32_avg_bits8,
1593 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001594
1595 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1596 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1597 aom_highbd_8_sub_pixel_variance64x64,
1598 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001599 aom_highbd_sad64x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001600 aom_highbd_dist_wtd_sad64x64_avg_bits8,
1601 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001602
1603 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1604 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1605 aom_highbd_8_sub_pixel_variance16x16,
1606 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001607 aom_highbd_sad16x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001608 aom_highbd_dist_wtd_sad16x16_avg_bits8,
1609 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001610
1611 HIGHBD_BFP(
1612 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1613 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001614 aom_highbd_8_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001615 aom_highbd_sad16x8x4d_bits8, aom_highbd_dist_wtd_sad16x8_avg_bits8,
1616 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001617
1618 HIGHBD_BFP(
1619 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1620 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001621 aom_highbd_8_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001622 aom_highbd_sad8x16x4d_bits8, aom_highbd_dist_wtd_sad8x16_avg_bits8,
1623 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001624
Cheng Chenbf3d4962017-11-01 14:48:52 -07001625 HIGHBD_BFP(
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001626 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1627 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1628 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x4d_bits8,
1629 aom_highbd_dist_wtd_sad8x8_avg_bits8,
1630 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x8)
1631
1632 HIGHBD_BFP(
1633 BLOCK_8X4, aom_highbd_sad8x4_bits8, aom_highbd_sad8x4_avg_bits8,
1634 aom_highbd_8_variance8x4, aom_highbd_8_sub_pixel_variance8x4,
1635 aom_highbd_8_sub_pixel_avg_variance8x4, aom_highbd_sad8x4x4d_bits8,
1636 aom_highbd_dist_wtd_sad8x4_avg_bits8,
1637 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x4)
1638
1639 HIGHBD_BFP(
1640 BLOCK_4X8, aom_highbd_sad4x8_bits8, aom_highbd_sad4x8_avg_bits8,
1641 aom_highbd_8_variance4x8, aom_highbd_8_sub_pixel_variance4x8,
1642 aom_highbd_8_sub_pixel_avg_variance4x8, aom_highbd_sad4x8x4d_bits8,
1643 aom_highbd_dist_wtd_sad4x8_avg_bits8,
1644 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x8)
1645
1646 HIGHBD_BFP(
1647 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1648 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1649 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x4d_bits8,
1650 aom_highbd_dist_wtd_sad4x4_avg_bits8,
1651 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x4)
1652
1653 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1654 aom_highbd_sad128x128_avg_bits8,
1655 aom_highbd_8_variance128x128,
1656 aom_highbd_8_sub_pixel_variance128x128,
1657 aom_highbd_8_sub_pixel_avg_variance128x128,
1658 aom_highbd_sad128x128x4d_bits8,
1659 aom_highbd_dist_wtd_sad128x128_avg_bits8,
1660 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001661
1662 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1663 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1664 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001665 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001666 aom_highbd_sad128x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001667 aom_highbd_dist_wtd_sad128x64_avg_bits8,
1668 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001669
1670 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1671 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1672 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001673 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001674 aom_highbd_sad64x128x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001675 aom_highbd_dist_wtd_sad64x128_avg_bits8,
1676 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001677
David Barkerf19f35f2017-05-22 16:33:22 +01001678 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1679 aom_highbd_8_masked_sub_pixel_variance128x128)
1680 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1681 aom_highbd_8_masked_sub_pixel_variance128x64)
1682 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1683 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001684 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1685 aom_highbd_8_masked_sub_pixel_variance64x64)
1686 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1687 aom_highbd_8_masked_sub_pixel_variance64x32)
1688 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1689 aom_highbd_8_masked_sub_pixel_variance32x64)
1690 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1691 aom_highbd_8_masked_sub_pixel_variance32x32)
1692 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1693 aom_highbd_8_masked_sub_pixel_variance32x16)
1694 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1695 aom_highbd_8_masked_sub_pixel_variance16x32)
1696 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1697 aom_highbd_8_masked_sub_pixel_variance16x16)
1698 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1699 aom_highbd_8_masked_sub_pixel_variance8x16)
1700 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1701 aom_highbd_8_masked_sub_pixel_variance16x8)
1702 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1703 aom_highbd_8_masked_sub_pixel_variance8x8)
1704 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1705 aom_highbd_8_masked_sub_pixel_variance4x8)
1706 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1707 aom_highbd_8_masked_sub_pixel_variance8x4)
1708 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1709 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001710 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1711 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001712 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1713 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001714 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1715 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001716 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1717 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001718 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1719 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001720 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1721 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001722 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1723 aom_highbd_obmc_variance128x128,
1724 aom_highbd_obmc_sub_pixel_variance128x128)
1725 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1726 aom_highbd_obmc_variance128x64,
1727 aom_highbd_obmc_sub_pixel_variance128x64)
1728 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1729 aom_highbd_obmc_variance64x128,
1730 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001731 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1732 aom_highbd_obmc_variance64x64,
1733 aom_highbd_obmc_sub_pixel_variance64x64)
1734 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1735 aom_highbd_obmc_variance64x32,
1736 aom_highbd_obmc_sub_pixel_variance64x32)
1737 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1738 aom_highbd_obmc_variance32x64,
1739 aom_highbd_obmc_sub_pixel_variance32x64)
1740 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1741 aom_highbd_obmc_variance32x32,
1742 aom_highbd_obmc_sub_pixel_variance32x32)
1743 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1744 aom_highbd_obmc_variance32x16,
1745 aom_highbd_obmc_sub_pixel_variance32x16)
1746 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1747 aom_highbd_obmc_variance16x32,
1748 aom_highbd_obmc_sub_pixel_variance16x32)
1749 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1750 aom_highbd_obmc_variance16x16,
1751 aom_highbd_obmc_sub_pixel_variance16x16)
1752 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1753 aom_highbd_obmc_variance8x16,
1754 aom_highbd_obmc_sub_pixel_variance8x16)
1755 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1756 aom_highbd_obmc_variance16x8,
1757 aom_highbd_obmc_sub_pixel_variance16x8)
1758 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1759 aom_highbd_obmc_variance8x8,
1760 aom_highbd_obmc_sub_pixel_variance8x8)
1761 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1762 aom_highbd_obmc_variance4x8,
1763 aom_highbd_obmc_sub_pixel_variance4x8)
1764 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1765 aom_highbd_obmc_variance8x4,
1766 aom_highbd_obmc_sub_pixel_variance8x4)
1767 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1768 aom_highbd_obmc_variance4x4,
1769 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001770 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1771 aom_highbd_obmc_variance64x16,
1772 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001773 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1774 aom_highbd_obmc_variance16x64,
1775 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001776 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1777 aom_highbd_obmc_variance32x8,
1778 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001779 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1780 aom_highbd_obmc_variance8x32,
1781 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001782 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1783 aom_highbd_obmc_variance16x4,
1784 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001785 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1786 aom_highbd_obmc_variance4x16,
1787 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001788 break;
1789
Yaowu Xuf883b422016-08-30 14:01:10 -07001790 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001791 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1792 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1793 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001794 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001795 aom_highbd_sad64x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001796 aom_highbd_dist_wtd_sad64x16_avg_bits10,
1797 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001798
1799 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1800 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1801 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001802 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001803 aom_highbd_sad16x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001804 aom_highbd_dist_wtd_sad16x64_avg_bits10,
1805 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001806
1807 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1808 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1809 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001810 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001811 aom_highbd_sad32x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001812 aom_highbd_dist_wtd_sad32x8_avg_bits10,
1813 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001814
1815 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1816 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1817 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001818 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001819 aom_highbd_sad8x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001820 aom_highbd_dist_wtd_sad8x32_avg_bits10,
1821 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001822
1823 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1824 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1825 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001826 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001827 aom_highbd_sad16x4x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001828 aom_highbd_dist_wtd_sad16x4_avg_bits10,
1829 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001830
1831 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1832 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1833 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001834 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001835 aom_highbd_sad4x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001836 aom_highbd_dist_wtd_sad4x16_avg_bits10,
1837 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001838
1839 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1840 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1841 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001842 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001843 aom_highbd_sad32x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001844 aom_highbd_dist_wtd_sad32x16_avg_bits10,
1845 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001846
1847 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1848 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1849 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001850 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001851 aom_highbd_sad16x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001852 aom_highbd_dist_wtd_sad16x32_avg_bits10,
1853 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001854
1855 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1856 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1857 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001858 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001859 aom_highbd_sad64x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001860 aom_highbd_dist_wtd_sad64x32_avg_bits10,
1861 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001862
1863 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1864 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1865 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001866 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001867 aom_highbd_sad32x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001868 aom_highbd_dist_wtd_sad32x64_avg_bits10,
1869 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001870
1871 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1872 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1873 aom_highbd_10_sub_pixel_variance32x32,
1874 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001875 aom_highbd_sad32x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001876 aom_highbd_dist_wtd_sad32x32_avg_bits10,
1877 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001878
1879 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1880 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1881 aom_highbd_10_sub_pixel_variance64x64,
1882 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001883 aom_highbd_sad64x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001884 aom_highbd_dist_wtd_sad64x64_avg_bits10,
1885 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001886
1887 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1888 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1889 aom_highbd_10_sub_pixel_variance16x16,
1890 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001891 aom_highbd_sad16x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001892 aom_highbd_dist_wtd_sad16x16_avg_bits10,
1893 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001894
1895 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1896 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1897 aom_highbd_10_sub_pixel_variance16x8,
1898 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001899 aom_highbd_sad16x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001900 aom_highbd_dist_wtd_sad16x8_avg_bits10,
1901 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001902
1903 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1904 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1905 aom_highbd_10_sub_pixel_variance8x16,
1906 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001907 aom_highbd_sad8x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001908 aom_highbd_dist_wtd_sad8x16_avg_bits10,
1909 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001910
1911 HIGHBD_BFP(
1912 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1913 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001914 aom_highbd_10_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001915 aom_highbd_sad8x8x4d_bits10, aom_highbd_dist_wtd_sad8x8_avg_bits10,
1916 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001917
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001918 HIGHBD_BFP(
1919 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
1920 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
1921 aom_highbd_10_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001922 aom_highbd_sad8x4x4d_bits10, aom_highbd_dist_wtd_sad8x4_avg_bits10,
1923 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001924
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001925 HIGHBD_BFP(
1926 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
1927 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
1928 aom_highbd_10_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001929 aom_highbd_sad4x8x4d_bits10, aom_highbd_dist_wtd_sad4x8_avg_bits10,
1930 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001931
1932 HIGHBD_BFP(
1933 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1934 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001935 aom_highbd_10_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001936 aom_highbd_sad4x4x4d_bits10, aom_highbd_dist_wtd_sad4x4_avg_bits10,
1937 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001938
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001939 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
1940 aom_highbd_sad128x128_avg_bits10,
1941 aom_highbd_10_variance128x128,
1942 aom_highbd_10_sub_pixel_variance128x128,
1943 aom_highbd_10_sub_pixel_avg_variance128x128,
1944 aom_highbd_sad128x128x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001945 aom_highbd_dist_wtd_sad128x128_avg_bits10,
1946 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001947
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001948 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
1949 aom_highbd_sad128x64_avg_bits10,
1950 aom_highbd_10_variance128x64,
1951 aom_highbd_10_sub_pixel_variance128x64,
1952 aom_highbd_10_sub_pixel_avg_variance128x64,
1953 aom_highbd_sad128x64x4d_bits10,
1954 aom_highbd_dist_wtd_sad128x64_avg_bits10,
1955 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001956
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001957 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
1958 aom_highbd_sad64x128_avg_bits10,
1959 aom_highbd_10_variance64x128,
1960 aom_highbd_10_sub_pixel_variance64x128,
1961 aom_highbd_10_sub_pixel_avg_variance64x128,
1962 aom_highbd_sad64x128x4d_bits10,
1963 aom_highbd_dist_wtd_sad64x128_avg_bits10,
1964 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001965
David Barkerf19f35f2017-05-22 16:33:22 +01001966 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1967 aom_highbd_10_masked_sub_pixel_variance128x128)
1968 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1969 aom_highbd_10_masked_sub_pixel_variance128x64)
1970 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1971 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001972 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1973 aom_highbd_10_masked_sub_pixel_variance64x64)
1974 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1975 aom_highbd_10_masked_sub_pixel_variance64x32)
1976 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1977 aom_highbd_10_masked_sub_pixel_variance32x64)
1978 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1979 aom_highbd_10_masked_sub_pixel_variance32x32)
1980 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1981 aom_highbd_10_masked_sub_pixel_variance32x16)
1982 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1983 aom_highbd_10_masked_sub_pixel_variance16x32)
1984 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1985 aom_highbd_10_masked_sub_pixel_variance16x16)
1986 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1987 aom_highbd_10_masked_sub_pixel_variance8x16)
1988 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1989 aom_highbd_10_masked_sub_pixel_variance16x8)
1990 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1991 aom_highbd_10_masked_sub_pixel_variance8x8)
1992 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1993 aom_highbd_10_masked_sub_pixel_variance4x8)
1994 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1995 aom_highbd_10_masked_sub_pixel_variance8x4)
1996 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1997 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001998 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
1999 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002000 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
2001 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002002 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
2003 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002004 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
2005 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002006 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
2007 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002008 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
2009 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002010 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
2011 aom_highbd_10_obmc_variance128x128,
2012 aom_highbd_10_obmc_sub_pixel_variance128x128)
2013 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
2014 aom_highbd_10_obmc_variance128x64,
2015 aom_highbd_10_obmc_sub_pixel_variance128x64)
2016 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
2017 aom_highbd_10_obmc_variance64x128,
2018 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002019 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2020 aom_highbd_10_obmc_variance64x64,
2021 aom_highbd_10_obmc_sub_pixel_variance64x64)
2022 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2023 aom_highbd_10_obmc_variance64x32,
2024 aom_highbd_10_obmc_sub_pixel_variance64x32)
2025 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2026 aom_highbd_10_obmc_variance32x64,
2027 aom_highbd_10_obmc_sub_pixel_variance32x64)
2028 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2029 aom_highbd_10_obmc_variance32x32,
2030 aom_highbd_10_obmc_sub_pixel_variance32x32)
2031 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2032 aom_highbd_10_obmc_variance32x16,
2033 aom_highbd_10_obmc_sub_pixel_variance32x16)
2034 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2035 aom_highbd_10_obmc_variance16x32,
2036 aom_highbd_10_obmc_sub_pixel_variance16x32)
2037 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2038 aom_highbd_10_obmc_variance16x16,
2039 aom_highbd_10_obmc_sub_pixel_variance16x16)
2040 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2041 aom_highbd_10_obmc_variance8x16,
2042 aom_highbd_10_obmc_sub_pixel_variance8x16)
2043 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2044 aom_highbd_10_obmc_variance16x8,
2045 aom_highbd_10_obmc_sub_pixel_variance16x8)
2046 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2047 aom_highbd_10_obmc_variance8x8,
2048 aom_highbd_10_obmc_sub_pixel_variance8x8)
2049 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2050 aom_highbd_10_obmc_variance4x8,
2051 aom_highbd_10_obmc_sub_pixel_variance4x8)
2052 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2053 aom_highbd_10_obmc_variance8x4,
2054 aom_highbd_10_obmc_sub_pixel_variance8x4)
2055 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2056 aom_highbd_10_obmc_variance4x4,
2057 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002058
Rupert Swarbrick72678572017-08-02 12:05:26 +01002059 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2060 aom_highbd_10_obmc_variance64x16,
2061 aom_highbd_10_obmc_sub_pixel_variance64x16)
2062
2063 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2064 aom_highbd_10_obmc_variance16x64,
2065 aom_highbd_10_obmc_sub_pixel_variance16x64)
2066
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002067 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2068 aom_highbd_10_obmc_variance32x8,
2069 aom_highbd_10_obmc_sub_pixel_variance32x8)
2070
2071 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2072 aom_highbd_10_obmc_variance8x32,
2073 aom_highbd_10_obmc_sub_pixel_variance8x32)
2074
2075 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2076 aom_highbd_10_obmc_variance16x4,
2077 aom_highbd_10_obmc_sub_pixel_variance16x4)
2078
2079 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2080 aom_highbd_10_obmc_variance4x16,
2081 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002082 break;
2083
Yaowu Xuf883b422016-08-30 14:01:10 -07002084 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002085 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2086 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2087 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002088 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002089 aom_highbd_sad64x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002090 aom_highbd_dist_wtd_sad64x16_avg_bits12,
2091 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002092
2093 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2094 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2095 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002096 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002097 aom_highbd_sad16x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002098 aom_highbd_dist_wtd_sad16x64_avg_bits12,
2099 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002100
2101 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2102 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2103 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002104 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002105 aom_highbd_sad32x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002106 aom_highbd_dist_wtd_sad32x8_avg_bits12,
2107 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002108
2109 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2110 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2111 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002112 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002113 aom_highbd_sad8x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002114 aom_highbd_dist_wtd_sad8x32_avg_bits12,
2115 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002116
2117 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2118 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2119 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002120 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002121 aom_highbd_sad16x4x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002122 aom_highbd_dist_wtd_sad16x4_avg_bits12,
2123 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002124
2125 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2126 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2127 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002128 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002129 aom_highbd_sad4x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002130 aom_highbd_dist_wtd_sad4x16_avg_bits12,
2131 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002132
2133 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2134 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2135 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002136 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002137 aom_highbd_sad32x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002138 aom_highbd_dist_wtd_sad32x16_avg_bits12,
2139 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002140
2141 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2142 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2143 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002144 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002145 aom_highbd_sad16x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002146 aom_highbd_dist_wtd_sad16x32_avg_bits12,
2147 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002148
2149 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2150 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2151 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002152 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002153 aom_highbd_sad64x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002154 aom_highbd_dist_wtd_sad64x32_avg_bits12,
2155 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002156
2157 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2158 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2159 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002160 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002161 aom_highbd_sad32x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002162 aom_highbd_dist_wtd_sad32x64_avg_bits12,
2163 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002164
2165 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2166 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2167 aom_highbd_12_sub_pixel_variance32x32,
2168 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002169 aom_highbd_sad32x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002170 aom_highbd_dist_wtd_sad32x32_avg_bits12,
2171 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002172
2173 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2174 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2175 aom_highbd_12_sub_pixel_variance64x64,
2176 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002177 aom_highbd_sad64x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002178 aom_highbd_dist_wtd_sad64x64_avg_bits12,
2179 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002180
2181 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2182 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2183 aom_highbd_12_sub_pixel_variance16x16,
2184 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002185 aom_highbd_sad16x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002186 aom_highbd_dist_wtd_sad16x16_avg_bits12,
2187 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002188
2189 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2190 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2191 aom_highbd_12_sub_pixel_variance16x8,
2192 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002193 aom_highbd_sad16x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002194 aom_highbd_dist_wtd_sad16x8_avg_bits12,
2195 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002196
2197 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2198 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2199 aom_highbd_12_sub_pixel_variance8x16,
2200 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002201 aom_highbd_sad8x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002202 aom_highbd_dist_wtd_sad8x16_avg_bits12,
2203 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002204
2205 HIGHBD_BFP(
2206 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2207 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002208 aom_highbd_12_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002209 aom_highbd_sad8x8x4d_bits12, aom_highbd_dist_wtd_sad8x8_avg_bits12,
2210 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002211
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002212 HIGHBD_BFP(
2213 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2214 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2215 aom_highbd_12_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002216 aom_highbd_sad8x4x4d_bits12, aom_highbd_dist_wtd_sad8x4_avg_bits12,
2217 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002218
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002219 HIGHBD_BFP(
2220 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2221 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2222 aom_highbd_12_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002223 aom_highbd_sad4x8x4d_bits12, aom_highbd_dist_wtd_sad4x8_avg_bits12,
2224 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002225
2226 HIGHBD_BFP(
2227 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2228 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002229 aom_highbd_12_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002230 aom_highbd_sad4x4x4d_bits12, aom_highbd_dist_wtd_sad4x4_avg_bits12,
2231 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002232
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002233 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2234 aom_highbd_sad128x128_avg_bits12,
2235 aom_highbd_12_variance128x128,
2236 aom_highbd_12_sub_pixel_variance128x128,
2237 aom_highbd_12_sub_pixel_avg_variance128x128,
2238 aom_highbd_sad128x128x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002239 aom_highbd_dist_wtd_sad128x128_avg_bits12,
2240 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002241
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002242 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2243 aom_highbd_sad128x64_avg_bits12,
2244 aom_highbd_12_variance128x64,
2245 aom_highbd_12_sub_pixel_variance128x64,
2246 aom_highbd_12_sub_pixel_avg_variance128x64,
2247 aom_highbd_sad128x64x4d_bits12,
2248 aom_highbd_dist_wtd_sad128x64_avg_bits12,
2249 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002250
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002251 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2252 aom_highbd_sad64x128_avg_bits12,
2253 aom_highbd_12_variance64x128,
2254 aom_highbd_12_sub_pixel_variance64x128,
2255 aom_highbd_12_sub_pixel_avg_variance64x128,
2256 aom_highbd_sad64x128x4d_bits12,
2257 aom_highbd_dist_wtd_sad64x128_avg_bits12,
2258 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002259
David Barkerf19f35f2017-05-22 16:33:22 +01002260 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2261 aom_highbd_12_masked_sub_pixel_variance128x128)
2262 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2263 aom_highbd_12_masked_sub_pixel_variance128x64)
2264 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2265 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002266 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2267 aom_highbd_12_masked_sub_pixel_variance64x64)
2268 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2269 aom_highbd_12_masked_sub_pixel_variance64x32)
2270 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2271 aom_highbd_12_masked_sub_pixel_variance32x64)
2272 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2273 aom_highbd_12_masked_sub_pixel_variance32x32)
2274 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2275 aom_highbd_12_masked_sub_pixel_variance32x16)
2276 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2277 aom_highbd_12_masked_sub_pixel_variance16x32)
2278 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2279 aom_highbd_12_masked_sub_pixel_variance16x16)
2280 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2281 aom_highbd_12_masked_sub_pixel_variance8x16)
2282 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2283 aom_highbd_12_masked_sub_pixel_variance16x8)
2284 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2285 aom_highbd_12_masked_sub_pixel_variance8x8)
2286 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2287 aom_highbd_12_masked_sub_pixel_variance4x8)
2288 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2289 aom_highbd_12_masked_sub_pixel_variance8x4)
2290 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2291 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002292 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2293 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002294 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2295 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002296 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2297 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002298 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2299 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002300 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2301 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002302 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2303 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002304 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2305 aom_highbd_12_obmc_variance128x128,
2306 aom_highbd_12_obmc_sub_pixel_variance128x128)
2307 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2308 aom_highbd_12_obmc_variance128x64,
2309 aom_highbd_12_obmc_sub_pixel_variance128x64)
2310 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2311 aom_highbd_12_obmc_variance64x128,
2312 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002313 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2314 aom_highbd_12_obmc_variance64x64,
2315 aom_highbd_12_obmc_sub_pixel_variance64x64)
2316 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2317 aom_highbd_12_obmc_variance64x32,
2318 aom_highbd_12_obmc_sub_pixel_variance64x32)
2319 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2320 aom_highbd_12_obmc_variance32x64,
2321 aom_highbd_12_obmc_sub_pixel_variance32x64)
2322 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2323 aom_highbd_12_obmc_variance32x32,
2324 aom_highbd_12_obmc_sub_pixel_variance32x32)
2325 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2326 aom_highbd_12_obmc_variance32x16,
2327 aom_highbd_12_obmc_sub_pixel_variance32x16)
2328 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2329 aom_highbd_12_obmc_variance16x32,
2330 aom_highbd_12_obmc_sub_pixel_variance16x32)
2331 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2332 aom_highbd_12_obmc_variance16x16,
2333 aom_highbd_12_obmc_sub_pixel_variance16x16)
2334 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2335 aom_highbd_12_obmc_variance8x16,
2336 aom_highbd_12_obmc_sub_pixel_variance8x16)
2337 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2338 aom_highbd_12_obmc_variance16x8,
2339 aom_highbd_12_obmc_sub_pixel_variance16x8)
2340 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2341 aom_highbd_12_obmc_variance8x8,
2342 aom_highbd_12_obmc_sub_pixel_variance8x8)
2343 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2344 aom_highbd_12_obmc_variance4x8,
2345 aom_highbd_12_obmc_sub_pixel_variance4x8)
2346 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2347 aom_highbd_12_obmc_variance8x4,
2348 aom_highbd_12_obmc_sub_pixel_variance8x4)
2349 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2350 aom_highbd_12_obmc_variance4x4,
2351 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002352 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2353 aom_highbd_12_obmc_variance64x16,
2354 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002355 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2356 aom_highbd_12_obmc_variance16x64,
2357 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002358 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2359 aom_highbd_12_obmc_variance32x8,
2360 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002361 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2362 aom_highbd_12_obmc_variance8x32,
2363 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002364 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2365 aom_highbd_12_obmc_variance16x4,
2366 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002367 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2368 aom_highbd_12_obmc_variance4x16,
2369 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002370 break;
2371
2372 default:
2373 assert(0 &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002374 "cm->seq_params.bit_depth should be AOM_BITS_8, "
Yaowu Xuf883b422016-08-30 14:01:10 -07002375 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002376 }
2377 }
2378}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002379
Yaowu Xuf883b422016-08-30 14:01:10 -07002380static void realloc_segmentation_maps(AV1_COMP *cpi) {
2381 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002382
2383 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002384 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002385 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002386 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002387
2388 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002389 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002390 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002391 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002392
2393 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002394 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002395 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002396 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002397}
2398
Yue Chenb4c93f02019-08-05 13:47:31 -07002399static void set_tpl_stats_block_size(AV1_COMP *cpi) {
2400 AV1_COMMON *const cm = &cpi->common;
2401 const int is_720p_or_larger = AOMMIN(cm->width, cm->height) >= 720;
2402
2403 // 0: 4x4, 1: 8x8, 2: 16x16
2404 cpi->tpl_stats_block_mis_log2 = is_720p_or_larger ? 2 : 1;
2405}
2406
Hui Su38711e72019-06-11 10:49:47 -07002407void av1_alloc_compound_type_rd_buffers(AV1_COMMON *const cm,
2408 CompoundTypeRdBuffers *const bufs) {
2409 CHECK_MEM_ERROR(
2410 cm, bufs->pred0,
2411 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred0)));
2412 CHECK_MEM_ERROR(
2413 cm, bufs->pred1,
2414 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred1)));
2415 CHECK_MEM_ERROR(
2416 cm, bufs->residual1,
2417 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->residual1)));
2418 CHECK_MEM_ERROR(
2419 cm, bufs->diff10,
2420 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->diff10)));
2421 CHECK_MEM_ERROR(cm, bufs->tmp_best_mask_buf,
2422 (uint8_t *)aom_malloc(2 * MAX_SB_SQUARE *
2423 sizeof(*bufs->tmp_best_mask_buf)));
2424}
2425
2426void av1_release_compound_type_rd_buffers(CompoundTypeRdBuffers *const bufs) {
2427 aom_free(bufs->pred0);
2428 aom_free(bufs->pred1);
2429 aom_free(bufs->residual1);
2430 aom_free(bufs->diff10);
2431 aom_free(bufs->tmp_best_mask_buf);
2432 av1_zero(*bufs); // Set all pointers to NULL for safety.
2433}
2434
Yaowu Xuf883b422016-08-30 14:01:10 -07002435void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2436 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002437 SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002438 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002439 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002440 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002441
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002442 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2443 seq_params->bit_depth = oxcf->bit_depth;
2444 seq_params->color_primaries = oxcf->color_primaries;
2445 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2446 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2447 seq_params->monochrome = oxcf->monochrome;
2448 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2449 seq_params->color_range = oxcf->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002450
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002451 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2452 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002453
Hui Su2aa492c2019-03-12 15:18:18 -07002454 memcpy(cpi->target_seq_level_idx, oxcf->target_seq_level_idx,
2455 sizeof(cpi->target_seq_level_idx));
2456 cpi->keep_level_stats = 0;
2457 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su6d666502019-06-13 15:16:50 -07002458 if (cpi->target_seq_level_idx[i] <= SEQ_LEVELS) {
Hui Su72ff0482019-05-28 14:07:37 -07002459 cpi->keep_level_stats |= 1u << i;
Hui Suc3a8d372019-05-28 15:52:45 -07002460 if (!cpi->level_info[i]) {
2461 CHECK_MEM_ERROR(cm, cpi->level_info[i],
2462 aom_calloc(1, sizeof(*cpi->level_info[i])));
2463 }
Hui Su2aa492c2019-03-12 15:18:18 -07002464 }
2465 }
kyslovabeeb7c2019-03-06 18:35:04 -08002466
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002467 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08002468 cm->timing_info.num_units_in_display_tick =
2469 oxcf->timing_info.num_units_in_display_tick;
2470 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
2471 cm->timing_info.equal_picture_interval =
2472 oxcf->timing_info.equal_picture_interval;
2473 cm->timing_info.num_ticks_per_picture =
2474 oxcf->timing_info.num_ticks_per_picture;
2475
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002476 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07002477 oxcf->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002478 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002479 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08002480 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002481 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08002482 cm->buffer_model.num_units_in_decoding_tick =
2483 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07002484 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07002485 av1_set_aom_dec_model_info(&cm->buffer_model);
2486 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07002487 } else if (cm->timing_info_present &&
2488 cm->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002489 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002490 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07002491 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07002492 } else {
2493 cm->op_params[0].initial_display_delay =
2494 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08002495 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002496
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002497 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002498
Yaowu Xuc27fc142016-08-22 16:08:15 -07002499 cpi->oxcf = *oxcf;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01002500 cpi->common.options = oxcf->cfg;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002501 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002502 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002503
Yaowu Xuf883b422016-08-30 14:01:10 -07002504 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002505 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2506 } else {
2507 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2508 }
2509
2510 cpi->refresh_last_frame = 1;
2511 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002512 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002513 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002514
Debargha Mukherjee229fdc82018-03-10 07:45:33 -08002515 cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2516 ? REFRESH_FRAME_CONTEXT_DISABLED
2517 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002518 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08002519 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002520
Alex Converse74ad0912017-07-18 10:22:58 -07002521 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002522 CHECK_MEM_ERROR(cm, x->palette_buffer,
2523 aom_memalign(16, sizeof(*x->palette_buffer)));
2524 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002525
Hui Su38711e72019-06-11 10:49:47 -07002526 if (x->comp_rd_buffer.pred0 == NULL) {
2527 av1_alloc_compound_type_rd_buffers(cm, &x->comp_rd_buffer);
2528 }
2529
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002530 if (x->tmp_conv_dst == NULL) {
2531 CHECK_MEM_ERROR(
2532 cm, x->tmp_conv_dst,
2533 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002534 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002535 }
2536 for (int i = 0; i < 2; ++i) {
2537 if (x->tmp_obmc_bufs[i] == NULL) {
2538 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
wenyao.liu22d8ab32018-10-16 09:11:29 +08002539 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002540 sizeof(*x->tmp_obmc_bufs[i])));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002541 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002542 }
2543 }
2544
Yaowu Xuf883b422016-08-30 14:01:10 -07002545 av1_reset_segment_features(cm);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002546 set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002547
Yaowu Xuc27fc142016-08-22 16:08:15 -07002548 set_rc_buffer_sizes(rc, &cpi->oxcf);
2549
2550 // Under a configuration change, where maximum_buffer_size may change,
2551 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002552 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2553 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002554
2555 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002556 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002557
2558 // Set absolute upper and lower quality limits
2559 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2560 rc->best_quality = cpi->oxcf.best_allowed_q;
2561
Urvang Joshib55cb5e2018-09-12 14:50:21 -07002562 cm->interp_filter = oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
Yue Chen5380cb52018-02-23 15:33:21 -08002563 cm->switchable_motion_mode = 1;
2564
Yaowu Xuc27fc142016-08-22 16:08:15 -07002565 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2566 cm->render_width = cpi->oxcf.render_width;
2567 cm->render_height = cpi->oxcf.render_height;
2568 } else {
2569 cm->render_width = cpi->oxcf.width;
2570 cm->render_height = cpi->oxcf.height;
2571 }
2572 cm->width = cpi->oxcf.width;
2573 cm->height = cpi->oxcf.height;
2574
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002575 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -08002576 // Superblock size should not be updated after the first key frame.
2577 if (!cpi->seq_params_locked) {
2578 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Hui Sud909c2c2019-03-08 11:51:14 -08002579 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
2580 seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
Urvang Joshie4530f82018-01-09 11:43:37 -08002581 }
Dominic Symes917d6c02017-10-11 18:00:52 +02002582
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002583 if (cpi->initial_width || sb_size != seq_params->sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002584 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002585 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002586 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002587 av1_free_pc_tree(&cpi->td, num_planes);
Cheng Chen46f30c72017-09-07 11:13:33 -07002588 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002589 realloc_segmentation_maps(cpi);
2590 cpi->initial_width = cpi->initial_height = 0;
2591 }
2592 }
2593 update_frame_size(cpi);
2594
2595 cpi->alt_ref_source = NULL;
2596 rc->is_src_frame_alt_ref = 0;
2597
Yaowu Xuc27fc142016-08-22 16:08:15 -07002598 set_tile_info(cpi);
2599
Marco Paniconid8574e32019-08-04 21:30:12 -07002600 if (!cpi->svc.external_ref_frame_config)
2601 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002602 cpi->ext_refresh_frame_context_pending = 0;
2603
Yaowu Xuc27fc142016-08-22 16:08:15 -07002604 highbd_set_var_fns(cpi);
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002605
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002606 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002607 // This should not be called after the first key frame.
2608 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002609 seq_params->operating_points_cnt_minus_1 =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002610 cm->number_spatial_layers > 1 ? cm->number_spatial_layers - 1 : 0;
Marco Paniconi67142112019-07-24 15:00:31 -07002611 init_seq_coding_tools(&cm->seq_params, cm, oxcf, cpi->use_svc);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002612 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002613}
2614
Yaowu Xuf883b422016-08-30 14:01:10 -07002615AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2616 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002617 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002618 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2619 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002620
2621 if (!cm) return NULL;
2622
Yaowu Xuf883b422016-08-30 14:01:10 -07002623 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002624
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07002625 // The jmp_buf is valid only for the duration of the function that calls
2626 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2627 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002628 if (setjmp(cm->error.jmp)) {
2629 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002630 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002631 return 0;
2632 }
2633
2634 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002635 cm->alloc_mi = enc_alloc_mi;
2636 cm->free_mi = enc_free_mi;
2637 cm->setup_mi = enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002638
Angie Chianga5d96c42016-10-21 16:16:56 -07002639 CHECK_MEM_ERROR(cm, cm->fc,
2640 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +00002641 CHECK_MEM_ERROR(
2642 cm, cm->default_frame_context,
2643 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -07002644 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +00002645 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002646
2647 cpi->resize_state = 0;
2648 cpi->resize_avg_qp = 0;
2649 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002650
Yaowu Xuc27fc142016-08-22 16:08:15 -07002651 cpi->common.buffer_pool = pool;
2652
2653 init_config(cpi, oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002654 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002655
David Turnerd2a592e2018-11-16 14:59:31 +00002656 cm->current_frame.frame_number = 0;
David Turnera4c96252019-01-11 16:36:39 +00002657 cm->current_frame_id = -1;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002658 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002659 cpi->partition_search_skippable_frame = 0;
2660 cpi->tile_data = NULL;
David Turnere7ebf902018-12-04 14:04:55 +00002661 cpi->last_show_frame_buf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002662 realloc_segmentation_maps(cpi);
2663
Jingning Hanf050fc12018-03-09 14:53:33 -08002664 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2665 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
James Zern01a9d702017-08-25 19:09:33 +00002666
Yaowu Xuc27fc142016-08-22 16:08:15 -07002667 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2668 i++) {
2669 CHECK_MEM_ERROR(
2670 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002671 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002672 }
2673
Yaowu Xuc27fc142016-08-22 16:08:15 -07002674 cpi->refresh_alt_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002675
2676 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2677#if CONFIG_INTERNAL_STATS
2678 cpi->b_calculate_blockiness = 1;
2679 cpi->b_calculate_consistency = 1;
2680 cpi->total_inconsistency = 0;
2681 cpi->psnr.worst = 100.0;
2682 cpi->worst_ssim = 100.0;
2683
2684 cpi->count = 0;
2685 cpi->bytes = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08002686#if CONFIG_SPEED_STATS
2687 cpi->tx_search_count = 0;
2688#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002689
2690 if (cpi->b_calculate_psnr) {
2691 cpi->total_sq_error = 0;
2692 cpi->total_samples = 0;
2693 cpi->tot_recode_hits = 0;
2694 cpi->summed_quality = 0;
2695 cpi->summed_weights = 0;
2696 }
2697
2698 cpi->fastssim.worst = 100.0;
2699 cpi->psnrhvs.worst = 100.0;
2700
2701 if (cpi->b_calculate_blockiness) {
2702 cpi->total_blockiness = 0;
2703 cpi->worst_blockiness = 0.0;
2704 }
2705
2706 if (cpi->b_calculate_consistency) {
2707 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002708 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002709 cpi->common.mi_rows * cpi->common.mi_cols));
2710 cpi->worst_consistency = 100.0;
2711 }
2712#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002713#if CONFIG_ENTROPY_STATS
2714 av1_zero(aggregate_fc);
2715#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002716
2717 cpi->first_time_stamp_ever = INT64_MAX;
2718
Jingning Hanf050fc12018-03-09 14:53:33 -08002719 cpi->td.mb.nmvcost[0] = &cpi->nmv_costs[0][MV_MAX];
2720 cpi->td.mb.nmvcost[1] = &cpi->nmv_costs[1][MV_MAX];
2721 cpi->td.mb.nmvcost_hp[0] = &cpi->nmv_costs_hp[0][MV_MAX];
2722 cpi->td.mb.nmvcost_hp[1] = &cpi->nmv_costs_hp[1][MV_MAX];
James Zern01a9d702017-08-25 19:09:33 +00002723
Yaowu Xuc27fc142016-08-22 16:08:15 -07002724#ifdef OUTPUT_YUV_SKINMAP
2725 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2726#endif
2727#ifdef OUTPUT_YUV_REC
2728 yuv_rec_file = fopen("rec.yuv", "wb");
2729#endif
2730
Jerome Jiang2612b4d2019-05-29 17:46:47 -07002731#if !CONFIG_REALTIME_ONLY
Yaowu Xuc27fc142016-08-22 16:08:15 -07002732 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002733 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002734 } else if (oxcf->pass == 2) {
2735 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2736 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2737
Yaowu Xuc27fc142016-08-22 16:08:15 -07002738 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2739 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2740 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2741
Yaowu Xuf883b422016-08-30 14:01:10 -07002742 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002743 }
Jerome Jiang2612b4d2019-05-29 17:46:47 -07002744#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002745
Jingning Hand064cf02017-06-01 10:00:39 -07002746 CHECK_MEM_ERROR(
2747 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002748 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002749 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002750 CHECK_MEM_ERROR(
2751 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002752 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002753 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002754
2755 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2756 (int32_t *)aom_memalign(
2757 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2758
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302759 CHECK_MEM_ERROR(
2760 cm, cpi->td.mb.inter_modes_info,
2761 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302762
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05302763 for (int x = 0; x < 2; x++)
2764 for (int y = 0; y < 2; y++)
2765 CHECK_MEM_ERROR(
2766 cm, cpi->td.mb.hash_value_buffer[x][y],
2767 (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
2768 sizeof(*cpi->td.mb.hash_value_buffer[0][0])));
2769
2770 cpi->td.mb.g_crc_initialized = 0;
2771
Jingning Hand064cf02017-06-01 10:00:39 -07002772 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2773 (int32_t *)aom_memalign(
2774 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2775
David Turner04b70d82019-01-24 15:39:19 +00002776 av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
2777 av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002778
sdengc23c7f12019-06-11 16:56:50 -07002779 {
2780 const int bsize = BLOCK_16X16;
2781 const int w = mi_size_wide[bsize];
2782 const int h = mi_size_high[bsize];
2783 const int num_cols = (cm->mi_cols + w - 1) / w;
2784 const int num_rows = (cm->mi_rows + h - 1) / h;
2785 CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors,
2786 aom_calloc(num_rows * num_cols,
2787 sizeof(*cpi->ssim_rdmult_scaling_factors)));
2788 }
2789
Yue Chenb4c93f02019-08-05 13:47:31 -07002790 set_tpl_stats_block_size(cpi);
Yue Chenc9b23e02019-04-10 16:54:03 -07002791 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
Yue Chenb4c93f02019-08-05 13:47:31 -07002792 const int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2793 const int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2794
2795 cpi->tpl_stats_buffer[frame].is_valid = 0;
2796 cpi->tpl_stats_buffer[frame].width =
2797 mi_cols >> cpi->tpl_stats_block_mis_log2;
2798 cpi->tpl_stats_buffer[frame].height =
2799 mi_rows >> cpi->tpl_stats_block_mis_log2;
2800 cpi->tpl_stats_buffer[frame].stride = cpi->tpl_stats_buffer[frame].width;
2801 cpi->tpl_stats_buffer[frame].mi_rows = cm->mi_rows;
2802 cpi->tpl_stats_buffer[frame].mi_cols = cm->mi_cols;
Yue Chen7cae98f2018-08-24 10:43:16 -07002803
Jingning Han81d6fbb2019-07-15 10:14:13 -07002804 CHECK_MEM_ERROR(
2805 cm, cpi->tpl_stats_buffer[frame].tpl_stats_ptr,
Yue Chenb4c93f02019-08-05 13:47:31 -07002806 aom_calloc(cpi->tpl_stats_buffer[frame].width *
2807 cpi->tpl_stats_buffer[frame].height,
Jingning Han81d6fbb2019-07-15 10:14:13 -07002808 sizeof(*cpi->tpl_stats_buffer[frame].tpl_stats_ptr)));
Yue Chen7cae98f2018-08-24 10:43:16 -07002809 }
Jingning Han31a0ee92019-07-15 13:56:55 -07002810 cpi->tpl_frame = &cpi->tpl_stats_buffer[REF_FRAMES + 1];
Yue Chen7cae98f2018-08-24 10:43:16 -07002811
chiyotsai9c484b32019-03-07 16:01:50 -08002812#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08002813 av1_zero(cpi->partition_stats);
2814#endif
2815
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002816#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
2817 cpi->fn_ptr[BT].sdf = SDF; \
2818 cpi->fn_ptr[BT].sdaf = SDAF; \
2819 cpi->fn_ptr[BT].vf = VF; \
2820 cpi->fn_ptr[BT].svf = SVF; \
2821 cpi->fn_ptr[BT].svaf = SVAF; \
2822 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
2823 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07002824 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002825
Cheng Chenf78632e2017-10-20 15:30:51 -07002826 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002827 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002828 aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
2829 aom_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002830
2831 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002832 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002833 aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
2834 aom_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002835
2836 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002837 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002838 aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
2839 aom_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002840
2841 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002842 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002843 aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
2844 aom_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002845
2846 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002847 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002848 aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
2849 aom_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002850
2851 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002852 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002853 aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
2854 aom_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002855
Cheng Chenf78632e2017-10-20 15:30:51 -07002856 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2857 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002858 aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
2859 aom_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002860
2861 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002862 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002863 aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
2864 aom_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002865
2866 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002867 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002868 aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
2869 aom_dist_wtd_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002870
2871 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002872 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002873 aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
2874 aom_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002875
2876 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002877 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002878 aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
2879 aom_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002880
2881 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002882 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002883 aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
2884 aom_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002885
2886 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002887 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002888 aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
2889 aom_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002890
2891 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2892 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002893 aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
2894 aom_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002895
2896 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2897 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002898 aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
2899 aom_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002900
2901 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2902 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002903 aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
2904 aom_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002905
2906 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002907 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002908 aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
2909 aom_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002910
2911 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002912 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002913 aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
2914 aom_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002915
2916 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002917 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002918 aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002919
2920 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002921 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002922 aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002923
2924 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002925 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002926 aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002927
2928 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002929 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002930 aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002931
Yaowu Xuc27fc142016-08-22 16:08:15 -07002932#define OBFP(BT, OSDF, OVF, OSVF) \
2933 cpi->fn_ptr[BT].osdf = OSDF; \
2934 cpi->fn_ptr[BT].ovf = OVF; \
2935 cpi->fn_ptr[BT].osvf = OSVF;
2936
Yaowu Xuf883b422016-08-30 14:01:10 -07002937 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2938 aom_obmc_sub_pixel_variance128x128)
2939 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2940 aom_obmc_sub_pixel_variance128x64)
2941 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2942 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002943 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2944 aom_obmc_sub_pixel_variance64x64)
2945 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2946 aom_obmc_sub_pixel_variance64x32)
2947 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2948 aom_obmc_sub_pixel_variance32x64)
2949 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2950 aom_obmc_sub_pixel_variance32x32)
2951 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2952 aom_obmc_sub_pixel_variance32x16)
2953 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2954 aom_obmc_sub_pixel_variance16x32)
2955 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2956 aom_obmc_sub_pixel_variance16x16)
2957 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2958 aom_obmc_sub_pixel_variance16x8)
2959 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2960 aom_obmc_sub_pixel_variance8x16)
2961 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2962 aom_obmc_sub_pixel_variance8x8)
2963 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2964 aom_obmc_sub_pixel_variance4x8)
2965 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2966 aom_obmc_sub_pixel_variance8x4)
2967 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2968 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002969 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
2970 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002971 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
2972 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002973 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
2974 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002975 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
2976 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002977 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
2978 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002979 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
2980 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002981
David Barkerf19f35f2017-05-22 16:33:22 +01002982#define MBFP(BT, MCSDF, MCSVF) \
2983 cpi->fn_ptr[BT].msdf = MCSDF; \
2984 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002985
David Barkerf19f35f2017-05-22 16:33:22 +01002986 MBFP(BLOCK_128X128, aom_masked_sad128x128,
2987 aom_masked_sub_pixel_variance128x128)
2988 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
2989 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002990 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
2991 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
2992 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
2993 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
2994 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
2995 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
2996 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
2997 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
2998 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
2999 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
3000 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
3001 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
3002 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003003
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003004 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
3005
3006 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
3007
3008 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
3009
3010 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003011
3012 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
3013
3014 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01003015
Yaowu Xuc27fc142016-08-22 16:08:15 -07003016 highbd_set_var_fns(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003017
Yaowu Xuf883b422016-08-30 14:01:10 -07003018 /* av1_init_quantizer() is first called here. Add check in
3019 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07003020 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07003021 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003022 */
Yaowu Xuf883b422016-08-30 14:01:10 -07003023 av1_init_quantizer(cpi);
Zoe Liud902b742018-02-19 17:02:41 -08003024 av1_qm_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003025
Yaowu Xuf883b422016-08-30 14:01:10 -07003026 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07003027 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003028 cm->superres_upscaled_width = oxcf->width;
3029 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07003030 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003031
3032 cm->error.setjmp = 0;
3033
3034 return cpi;
3035}
3036
Urvang Joshiee2c8112018-05-04 14:53:15 -07003037#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003038#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
3039
3040#define SNPRINT2(H, T, V) \
3041 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07003042#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003043
Yaowu Xuf883b422016-08-30 14:01:10 -07003044void av1_remove_compressor(AV1_COMP *cpi) {
3045 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003046 unsigned int i;
3047 int t;
3048
3049 if (!cpi) return;
3050
3051 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003052 const int num_planes = av1_num_planes(cm);
3053
David Turnerd2a592e2018-11-16 14:59:31 +00003054 if (cm->current_frame.frame_number > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003055#if CONFIG_ENTROPY_STATS
3056 if (cpi->oxcf.pass != 1) {
3057 fprintf(stderr, "Writing counts.stt\n");
3058 FILE *f = fopen("counts.stt", "wb");
3059 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
3060 fclose(f);
3061 }
3062#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003063#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003064 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003065
3066 if (cpi->oxcf.pass != 1) {
3067 char headings[512] = { 0 };
3068 char results[512] = { 0 };
3069 FILE *f = fopen("opsnr.stt", "a");
3070 double time_encoded =
3071 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
3072 10000000.000;
3073 double total_encode_time =
3074 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3075 const double dr =
3076 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3077 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3078 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3079 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3080
3081 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003082 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003083 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3084 const double total_ssim =
3085 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3086 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08003087 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003088 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003089 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Jingning Han87651b22017-11-28 20:02:26 -08003090 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003091 snprintf(results, sizeof(results),
3092 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3093 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003094 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Han87651b22017-11-28 20:02:26 -08003095 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003096 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
3097 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
3098 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
3099 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003100 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003101 cpi->psnr.stat[STAT_Y] / cpi->count,
3102 cpi->psnr.stat[STAT_U] / cpi->count,
3103 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003104
3105 if (cpi->b_calculate_blockiness) {
3106 SNPRINT(headings, "\t Block\tWstBlck");
3107 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3108 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3109 }
3110
3111 if (cpi->b_calculate_consistency) {
3112 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003113 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003114 (double)cpi->total_inconsistency);
3115
3116 SNPRINT(headings, "\tConsist\tWstCons");
3117 SNPRINT2(results, "\t%7.3f", consistency);
3118 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3119 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07003120 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
3121 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003122 rate_err, fabs(rate_err));
3123 }
3124
3125 fclose(f);
3126 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07003127#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003128#if CONFIG_SPEED_STATS
3129 if (cpi->oxcf.pass != 1) {
3130 fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
3131 }
3132#endif // CONFIG_SPEED_STATS
chiyotsai92ed0dd2019-01-25 14:50:14 -08003133
chiyotsai9c484b32019-03-07 16:01:50 -08003134#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08003135 if (cpi->oxcf.pass != 1) {
3136 av1_print_partition_stats(&cpi->partition_stats);
3137 }
3138#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003139 }
3140
Yue Chenc9b23e02019-04-10 16:54:03 -07003141 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
Jingning Han81d6fbb2019-07-15 10:14:13 -07003142 aom_free(cpi->tpl_stats_buffer[frame].tpl_stats_ptr);
3143 cpi->tpl_stats_buffer[frame].is_valid = 0;
Yue Chen7cae98f2018-08-24 10:43:16 -07003144 }
3145
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303146 for (t = cpi->num_workers - 1; t >= 0; --t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003147 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003148 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3149
3150 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003151 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003152
3153 // Deallocate allocated thread data.
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303154 aom_free(thread_data->td->tctx);
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303155 if (t > 0) {
hui sud9a812b2017-07-06 14:34:37 -07003156 aom_free(thread_data->td->palette_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003157 aom_free(thread_data->td->tmp_conv_dst);
Hui Su38711e72019-06-11 10:49:47 -07003158 av1_release_compound_type_rd_buffers(&thread_data->td->comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003159 for (int j = 0; j < 2; ++j) {
3160 aom_free(thread_data->td->tmp_obmc_bufs[j]);
3161 }
Jingning Hand064cf02017-06-01 10:00:39 -07003162 aom_free(thread_data->td->above_pred_buf);
3163 aom_free(thread_data->td->left_pred_buf);
3164 aom_free(thread_data->td->wsrc_buf);
wenyao.liu22d8ab32018-10-16 09:11:29 +08003165
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303166 aom_free(thread_data->td->inter_modes_info);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003167 for (int x = 0; x < 2; x++) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303168 for (int y = 0; y < 2; y++) {
3169 aom_free(thread_data->td->hash_value_buffer[x][y]);
3170 thread_data->td->hash_value_buffer[x][y] = NULL;
3171 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003172 }
Jingning Hand064cf02017-06-01 10:00:39 -07003173 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003174 aom_free(thread_data->td->counts);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003175 av1_free_pc_tree(thread_data->td, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003176 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003177 }
3178 }
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303179#if CONFIG_MULTITHREAD
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303180 if (cpi->row_mt_mutex_ != NULL) {
3181 pthread_mutex_destroy(cpi->row_mt_mutex_);
3182 aom_free(cpi->row_mt_mutex_);
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303183 }
3184#endif
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05303185 av1_row_mt_mem_dealloc(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07003186 aom_free(cpi->tile_thr_data);
3187 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003188
Deepa K G964e72e2018-05-16 16:56:01 +05303189 if (cpi->num_workers > 1) {
3190 av1_loop_filter_dealloc(&cpi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05303191 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
Deepa K G964e72e2018-05-16 16:56:01 +05303192 }
3193
Yaowu Xuc27fc142016-08-22 16:08:15 -07003194 dealloc_compressor_data(cpi);
3195
3196 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
3197 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003198 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003199 }
3200
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003201#if CONFIG_INTERNAL_STATS
3202 aom_free(cpi->ssim_vars);
3203 cpi->ssim_vars = NULL;
3204#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003205
Yaowu Xuf883b422016-08-30 14:01:10 -07003206 av1_remove_common(cm);
RogerZhou80d52342017-11-20 10:56:26 -08003207 for (i = 0; i < FRAME_BUFFERS; ++i) {
3208 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
3209 }
Yaowu Xu74e63352019-05-06 09:21:33 -07003210#if CONFIG_HTB_TRELLIS
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08003211 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xu74e63352019-05-06 09:21:33 -07003212#endif // CONFIG_HTB_TRELLIS
Yaowu Xuf883b422016-08-30 14:01:10 -07003213 av1_free_ref_frame_buffers(cm->buffer_pool);
3214 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003215
3216#ifdef OUTPUT_YUV_SKINMAP
3217 fclose(yuv_skinmap_file);
3218#endif
3219#ifdef OUTPUT_YUV_REC
3220 fclose(yuv_rec_file);
3221#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003222}
3223
Yaowu Xuf883b422016-08-30 14:01:10 -07003224static void generate_psnr_packet(AV1_COMP *cpi) {
3225 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003226 int i;
3227 PSNR_STATS psnr;
David Turnerc29e1a92018-12-06 14:10:14 +00003228 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003229 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003230
3231 for (i = 0; i < 4; ++i) {
3232 pkt.data.psnr.samples[i] = psnr.samples[i];
3233 pkt.data.psnr.sse[i] = psnr.sse[i];
3234 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3235 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003236 pkt.kind = AOM_CODEC_PSNR_PKT;
3237 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003238}
3239
Yaowu Xuf883b422016-08-30 14:01:10 -07003240int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003241 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3242
Yunqing Wangf2e7a392017-11-08 00:27:21 -08003243 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003244 return 0;
3245}
3246
Thomas Daede497d1952017-08-08 17:33:06 -07003247int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3248 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003249 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003250 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003251 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003252 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003253 return 0;
3254 } else {
3255 return -1;
3256 }
3257}
3258
Thomas Daede497d1952017-08-08 17:33:06 -07003259int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3260 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003261 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003262 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003263 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003264 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003265 return 0;
3266 } else {
3267 return -1;
3268 }
3269}
3270
3271int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003272 cpi->ext_refresh_frame_context = update;
3273 cpi->ext_refresh_frame_context_pending = 1;
3274 return 0;
3275}
3276
3277#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3278// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3279// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3280// not denoise the UV channels at this time. If ever we implement UV channel
3281// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003282void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003283 uint8_t *src = s->y_buffer;
3284 int h = s->y_height;
3285
3286 do {
3287 fwrite(src, s->y_width, 1, f);
3288 src += s->y_stride;
3289 } while (--h);
3290
3291 src = s->u_buffer;
3292 h = s->uv_height;
3293
3294 do {
3295 fwrite(src, s->uv_width, 1, f);
3296 src += s->uv_stride;
3297 } while (--h);
3298
3299 src = s->v_buffer;
3300 h = s->uv_height;
3301
3302 do {
3303 fwrite(src, s->uv_width, 1, f);
3304 src += s->uv_stride;
3305 } while (--h);
3306}
3307#endif
3308
Yaowu Xuc27fc142016-08-22 16:08:15 -07003309#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003310void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003311 uint8_t *src = s->y_buffer;
3312 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003313 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003314 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3315 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3316
3317 do {
3318 fwrite(src16, s->y_width, 2, yuv_rec_file);
3319 src16 += s->y_stride;
3320 } while (--h);
3321
3322 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3323 h = s->uv_height;
3324
3325 do {
3326 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3327 src16 += s->uv_stride;
3328 } while (--h);
3329
3330 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3331 h = s->uv_height;
3332
3333 do {
3334 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3335 src16 += s->uv_stride;
3336 } while (--h);
3337
3338 fflush(yuv_rec_file);
3339 return;
3340 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003341
3342 do {
3343 fwrite(src, s->y_width, 1, yuv_rec_file);
3344 src += s->y_stride;
3345 } while (--h);
3346
3347 src = s->u_buffer;
3348 h = s->uv_height;
3349
3350 do {
3351 fwrite(src, s->uv_width, 1, yuv_rec_file);
3352 src += s->uv_stride;
3353 } while (--h);
3354
3355 src = s->v_buffer;
3356 h = s->uv_height;
3357
3358 do {
3359 fwrite(src, s->uv_width, 1, yuv_rec_file);
3360 src += s->uv_stride;
3361 } while (--h);
3362
3363 fflush(yuv_rec_file);
3364}
3365#endif // OUTPUT_YUV_REC
3366
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003367#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003368static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3369 int i;
3370 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003371 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003372 AV1_COMMON *const cm = &cpi->common;
3373 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3374 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003375 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003376 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003377 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003378 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003379 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003380 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003381 // TODO(sarahparker): The earlier condition for recoding here was:
3382 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3383 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003384 }
3385 }
3386 return recode;
3387}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003388
Yaowu Xuc27fc142016-08-22 16:08:15 -07003389// Function to test for conditions that indicate we should loop
3390// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003391static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3392 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003393 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003394 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003395 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3396 int force_recode = 0;
3397
3398 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3399 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3400 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003401 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3402 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3403 (rc->projected_frame_size < low_limit && q > minq)) {
3404 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003405 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003406 // Deal with frame undershoot and whether or not we are
3407 // below the automatically set cq level.
3408 if (q > oxcf->cq_level &&
3409 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3410 force_recode = 1;
3411 }
3412 }
3413 }
3414 return force_recode;
3415}
3416
Cheng Chen46f30c72017-09-07 11:13:33 -07003417static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003418 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003419 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003420 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003421
3422 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003423 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Jingning Han667561a2019-07-22 15:48:01 -07003424 if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003425 BufferPool *const pool = cm->buffer_pool;
3426 const YV12_BUFFER_CONFIG *const ref =
David Turnera21966b2018-12-05 14:48:49 +00003427 get_ref_frame_yv12_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003428
3429 if (ref == NULL) {
David Turnere7ebf902018-12-04 14:04:55 +00003430 cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003431 continue;
3432 }
3433
Yaowu Xuc27fc142016-08-22 16:08:15 -07003434 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
Debargha Mukherjee13cccf22019-03-27 23:49:14 -07003435 // Replace the reference buffer with a copy having a thicker border,
3436 // if the reference buffer is higher resolution than the current
3437 // frame, and the border is thin.
3438 if ((ref->y_crop_width > cm->width ||
3439 ref->y_crop_height > cm->height) &&
3440 ref->border < AOM_BORDER_IN_PIXELS) {
3441 RefCntBuffer *ref_fb = get_ref_frame_buf(cm, ref_frame);
3442 if (aom_yv12_realloc_with_new_border(
3443 &ref_fb->buf, AOM_BORDER_IN_PIXELS, cm->byte_alignment,
3444 num_planes) != 0) {
3445 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3446 "Failed to allocate frame buffer");
3447 }
3448 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003449 int force_scaling = 0;
David Turnere7ebf902018-12-04 14:04:55 +00003450 RefCntBuffer *new_fb = cpi->scaled_ref_buf[ref_frame - 1];
3451 if (new_fb == NULL) {
3452 const int new_fb_idx = get_free_fb(cm);
3453 if (new_fb_idx == INVALID_IDX) {
Wan-Teh Chang4a8c0042018-10-05 09:41:52 -07003454 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3455 "Unable to find free frame buffer");
David Turnere7ebf902018-12-04 14:04:55 +00003456 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003457 force_scaling = 1;
David Turnere7ebf902018-12-04 14:04:55 +00003458 new_fb = &pool->frame_bufs[new_fb_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003459 }
David Turnere7ebf902018-12-04 14:04:55 +00003460
3461 if (force_scaling || new_fb->buf.y_crop_width != cm->width ||
3462 new_fb->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003463 if (aom_realloc_frame_buffer(
David Turnere7ebf902018-12-04 14:04:55 +00003464 &new_fb->buf, cm->width, cm->height,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003465 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
Debargha Mukherjeefa946af2019-03-26 16:58:55 -07003466 cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003467 cm->byte_alignment, NULL, NULL, NULL)) {
3468 if (force_scaling) {
3469 // Release the reference acquired in the get_free_fb() call above.
David Turnere7ebf902018-12-04 14:04:55 +00003470 --new_fb->ref_count;
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003471 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003472 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003473 "Failed to allocate frame buffer");
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003474 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003475 av1_resize_and_extend_frame(
David Turnere7ebf902018-12-04 14:04:55 +00003476 ref, &new_fb->buf, (int)cm->seq_params.bit_depth, num_planes);
3477 cpi->scaled_ref_buf[ref_frame - 1] = new_fb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003478 alloc_frame_mvs(cm, new_fb);
3479 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003480 } else {
David Turnera21966b2018-12-05 14:48:49 +00003481 RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003482 buf->buf.y_crop_width = ref->y_crop_width;
3483 buf->buf.y_crop_height = ref->y_crop_height;
David Turnere7ebf902018-12-04 14:04:55 +00003484 cpi->scaled_ref_buf[ref_frame - 1] = buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003485 ++buf->ref_count;
3486 }
3487 } else {
David Turnere7ebf902018-12-04 14:04:55 +00003488 if (cpi->oxcf.pass != 0) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003489 }
3490 }
3491}
3492
Yaowu Xuf883b422016-08-30 14:01:10 -07003493static void release_scaled_references(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003494 // TODO(isbs): only refresh the necessary frames, rather than all of them
David Turnere7ebf902018-12-04 14:04:55 +00003495 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3496 RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
3497 if (buf != NULL) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003498 --buf->ref_count;
David Turnere7ebf902018-12-04 14:04:55 +00003499 cpi->scaled_ref_buf[i] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003500 }
3501 }
3502}
3503
Yaowu Xuf883b422016-08-30 14:01:10 -07003504static void set_mv_search_params(AV1_COMP *cpi) {
3505 const AV1_COMMON *const cm = &cpi->common;
3506 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003507
3508 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003509 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003510
3511 if (cpi->sf.mv.auto_mv_step_size) {
3512 if (frame_is_intra_only(cm)) {
3513 // Initialize max_mv_magnitude for use in the first INTER frame
3514 // after a key/intra-only frame.
3515 cpi->max_mv_magnitude = max_mv_def;
3516 } else {
3517 if (cm->show_frame) {
3518 // Allow mv_steps to correspond to twice the max mv magnitude found
3519 // in the previous frame, capped by the default max_mv_magnitude based
3520 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003521 cpi->mv_step_param = av1_init_search_range(
3522 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003523 }
3524 cpi->max_mv_magnitude = 0;
3525 }
3526 }
3527}
3528
Hui Subdf0c992019-02-14 14:52:41 -08003529static void set_screen_content_options(AV1_COMP *cpi) {
3530 AV1_COMMON *cm = &cpi->common;
3531
3532 if (cm->seq_params.force_screen_content_tools != 2) {
3533 cm->allow_screen_content_tools = cm->allow_intrabc =
3534 cm->seq_params.force_screen_content_tools;
3535 return;
3536 }
3537
3538 if (cpi->oxcf.content == AOM_CONTENT_SCREEN) {
3539 cm->allow_screen_content_tools = cm->allow_intrabc = 1;
3540 return;
3541 }
3542
3543 // Estimate if the source frame is screen content, based on the portion of
3544 // blocks that have few luma colors.
3545 const uint8_t *src = cpi->source->y_buffer;
3546 assert(src != NULL);
3547 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
3548 const int stride = cpi->source->y_stride;
3549 const int width = cpi->source->y_width;
3550 const int height = cpi->source->y_height;
3551 const int bd = cm->seq_params.bit_depth;
3552 const int blk_w = 16;
3553 const int blk_h = 16;
3554 // These threshold values are selected experimentally.
3555 const int color_thresh = 4;
3556 const unsigned int var_thresh = 0;
3557 // Counts of blocks with no more than color_thresh colors.
3558 int counts_1 = 0;
3559 // Counts of blocks with no more than color_thresh colors and variance larger
3560 // than var_thresh.
3561 int counts_2 = 0;
3562
3563 for (int r = 0; r + blk_h <= height; r += blk_h) {
3564 for (int c = 0; c + blk_w <= width; c += blk_w) {
3565 int count_buf[1 << 12]; // Maximum (1 << 12) color levels.
3566 const uint8_t *const this_src = src + r * stride + c;
3567 const int n_colors =
3568 use_hbd ? av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd,
3569 count_buf)
3570 : av1_count_colors(this_src, stride, blk_w, blk_h, count_buf);
3571 if (n_colors > 1 && n_colors <= color_thresh) {
3572 ++counts_1;
3573 struct buf_2d buf;
3574 buf.stride = stride;
3575 buf.buf = (uint8_t *)this_src;
3576 const unsigned int var =
3577 use_hbd
3578 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
3579 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
3580 if (var > var_thresh) ++counts_2;
3581 }
3582 }
3583 }
3584
3585 // The threshold values are selected experimentally.
3586 cm->allow_screen_content_tools =
3587 counts_1 * blk_h * blk_w * 10 > width * height;
3588 // IntraBC would force loop filters off, so we use more strict rules that also
3589 // requires that the block has high variance.
3590 cm->allow_intrabc = cm->allow_screen_content_tools &&
Hui Su64a2ffb2019-04-11 16:47:13 -07003591 counts_2 * blk_h * blk_w * 12 > width * height;
Hui Subdf0c992019-02-14 14:52:41 -08003592}
3593
Yaowu Xuf883b422016-08-30 14:01:10 -07003594static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003595 int i;
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003596 AV1_COMMON *cm = &cpi->common;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003597 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003598 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003599 }
3600 cpi->global_motion_search_done = 0;
Yunqing Wang9411e432019-03-14 15:53:23 -07003601
3602 if (frame_is_intra_only(cm)) set_screen_content_options(cpi);
3603 cpi->is_screen_content_type = (cm->allow_screen_content_tools != 0);
3604
David Turner04b70d82019-01-24 15:39:19 +00003605 av1_set_speed_features_framesize_independent(cpi, cpi->speed);
Yaowu Xuf883b422016-08-30 14:01:10 -07003606 av1_set_rd_speed_thresholds(cpi);
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003607 cm->interp_filter = SWITCHABLE;
3608 cm->switchable_motion_mode = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003609}
3610
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003611static int get_gfu_boost_from_r0(double r0, int frames_to_key) {
3612 double factor = sqrt((double)frames_to_key);
3613 factor = AOMMIN(factor, 10.0);
3614 factor = AOMMAX(factor, 4.0);
3615 const int boost = (int)rint((200.0 + 10.0 * factor) / r0);
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003616 return boost;
3617}
3618
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003619static int get_kf_boost_from_r0(double r0, int frames_to_key) {
3620 double factor = sqrt((double)frames_to_key);
3621 factor = AOMMIN(factor, 10.0);
3622 factor = AOMMAX(factor, 4.0);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003623 const int boost = (int)rint((75.0 + 14.0 * factor) / r0);
3624 return boost;
3625}
3626
3627int combine_prior_with_tpl_boost(int prior_boost, int tpl_boost,
3628 int frames_to_key) {
3629 double factor = sqrt((double)frames_to_key);
3630 factor = AOMMIN(factor, 12.0);
3631 factor = AOMMAX(factor, 4.0);
3632 factor -= 4.0;
3633 int boost = (int)((factor * prior_boost + (8.0 - factor) * tpl_boost) / 8.0);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003634 return boost;
3635}
3636
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003637static void process_tpl_stats_frame(AV1_COMP *cpi) {
Sarah Parkere1b22012019-06-06 16:35:25 -07003638 const GF_GROUP *const gf_group = &cpi->gf_group;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003639 AV1_COMMON *const cm = &cpi->common;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003640
Sarah Parkere1b22012019-06-06 16:35:25 -07003641 assert(IMPLIES(gf_group->size > 0, gf_group->index < gf_group->size));
Jingning Han31a0ee92019-07-15 13:56:55 -07003642
3643 const int tpl_idx = gf_group->index;
Jingning Han81d6fbb2019-07-15 10:14:13 -07003644 TplDepFrame *tpl_frame = &cpi->tpl_frame[tpl_idx];
Yue Chen4e585cc2019-06-03 14:47:16 -07003645 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003646
Yue Chen4e585cc2019-06-03 14:47:16 -07003647 if (tpl_frame->is_valid) {
3648 int tpl_stride = tpl_frame->stride;
3649 int64_t intra_cost_base = 0;
3650 int64_t mc_dep_cost_base = 0;
Yue Chenbd934232019-08-05 14:23:39 -07003651#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07003652 int64_t mc_saved_base = 0;
3653 int64_t mc_count_base = 0;
Yue Chenbd934232019-08-05 14:23:39 -07003654#endif // !USE_TPL_CLASSIC_MODEL
Yue Chenb4c93f02019-08-05 13:47:31 -07003655 const int step = 1 << cpi->tpl_stats_block_mis_log2;
Debargha Mukherjeeedfa4fe2019-07-08 14:23:39 -07003656 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
Yue Chenb4c93f02019-08-05 13:47:31 -07003657
3658 for (int row = 0; row < cm->mi_rows; row += step) {
3659 for (int col = 0; col < mi_cols_sr; col += step) {
3660 TplDepStats *this_stats =
3661 &tpl_stats[av1_tpl_ptr_pos(cpi, row, col, tpl_stride)];
Yue Chen4e585cc2019-06-03 14:47:16 -07003662 intra_cost_base += this_stats->intra_cost;
3663 mc_dep_cost_base += this_stats->intra_cost + this_stats->mc_flow;
Yue Chenbd934232019-08-05 14:23:39 -07003664#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07003665 mc_count_base += this_stats->mc_count;
3666 mc_saved_base += this_stats->mc_saved;
Yue Chenbd934232019-08-05 14:23:39 -07003667#endif // !USE_TPL_CLASSIC_MODEL
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003668 }
Yue Chen4e585cc2019-06-03 14:47:16 -07003669 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003670
Yue Chen4e585cc2019-06-03 14:47:16 -07003671 if (mc_dep_cost_base == 0) {
3672 tpl_frame->is_valid = 0;
3673 } else {
3674 aom_clear_system_state();
3675 cpi->rd.r0 = (double)intra_cost_base / mc_dep_cost_base;
3676 if (is_frame_arf_and_tpl_eligible(cpi)) {
3677 cpi->rd.arf_r0 = cpi->rd.r0;
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003678 const int gfu_boost =
3679 get_gfu_boost_from_r0(cpi->rd.arf_r0, cpi->rc.frames_to_key);
Yue Chen4e585cc2019-06-03 14:47:16 -07003680 // printf("old boost %d new boost %d\n", cpi->rc.gfu_boost,
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003681 // gfu_boost);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003682 cpi->rc.gfu_boost = combine_prior_with_tpl_boost(
3683 cpi->rc.gfu_boost, gfu_boost, cpi->rc.frames_to_key);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003684 } else if (frame_is_intra_only(cm)) {
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07003685 // TODO(debargha): Turn off q adjustment for kf temporarily to
3686 // reduce impact on speed of encoding. Need to investigate how
3687 // to mitigate the issue.
3688 if (cpi->oxcf.rc_mode == AOM_Q) {
3689 const int kf_boost =
3690 get_kf_boost_from_r0(cpi->rd.r0, cpi->rc.frames_to_key);
3691 // printf("old kf boost %d new kf boost %d [%d]\n", cpi->rc.kf_boost,
3692 // kf_boost, cpi->rc.frames_to_key);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003693 cpi->rc.kf_boost = combine_prior_with_tpl_boost(
3694 cpi->rc.kf_boost, kf_boost, cpi->rc.frames_to_key);
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07003695 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003696 }
Yue Chenbd934232019-08-05 14:23:39 -07003697#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07003698 cpi->rd.mc_count_base =
3699 (double)mc_count_base / (cm->mi_rows * cm->mi_cols);
3700 cpi->rd.mc_saved_base =
3701 (double)mc_saved_base / (cm->mi_rows * cm->mi_cols);
Yue Chenbd934232019-08-05 14:23:39 -07003702#endif // !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07003703 aom_clear_system_state();
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003704 }
3705 }
3706}
3707
Yaowu Xuf883b422016-08-30 14:01:10 -07003708static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003709 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003710 AV1_COMMON *const cm = &cpi->common;
3711 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003712
3713 // Setup variables that depend on the dimensions of the frame.
David Turner04b70d82019-01-24 15:39:19 +00003714 av1_set_speed_features_framesize_dependent(cpi, cpi->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003715
Yue Chen4e585cc2019-06-03 14:47:16 -07003716 if (cpi->oxcf.enable_tpl_model && cpi->tpl_model_pass == 0 &&
3717 is_frame_tpl_eligible(cpi))
3718 process_tpl_stats_frame(cpi);
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003719
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01003720 // Decide q and q bounds.
Jingning Han2e029872019-08-02 10:54:19 -07003721 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, cpi->gf_group.index,
3722 bottom_index, top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003723
James Zern01a9d702017-08-25 19:09:33 +00003724 if (!frame_is_intra_only(cm)) {
RogerZhou3b635242017-09-19 10:06:46 -07003725 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
RogerZhou10a03802017-10-26 11:49:48 -07003726 cpi->common.cur_frame_force_integer_mv);
James Zern01a9d702017-08-25 19:09:33 +00003727 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003728
3729 // Configure experimental use of segmentation for enhanced coding of
3730 // static regions if indicated.
3731 // Only allowed in the second pass of a two pass encode, as it requires
3732 // lagged coding, and if the relevant speed feature flag is set.
3733 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3734 configure_static_seg_features(cpi);
3735}
3736
Yaowu Xuf883b422016-08-30 14:01:10 -07003737static void init_motion_estimation(AV1_COMP *cpi) {
Urvang Joshi510d8f62019-01-10 12:11:50 -08003738 AV1_COMMON *const cm = &cpi->common;
chiyotsai836b69b2019-04-09 13:41:24 -07003739 const int y_stride = cpi->scaled_source.y_stride;
Urvang Joshi510d8f62019-01-10 12:11:50 -08003740 const int y_stride_src =
3741 ((cpi->oxcf.width != cm->width || cpi->oxcf.height != cm->height) ||
3742 av1_superres_scaled(cm))
3743 ? y_stride
3744 : cpi->lookahead->buf->img.y_stride;
chiyotsai836b69b2019-04-09 13:41:24 -07003745 // Update if ss_cfg is uninitialized or the current frame has a new stride
3746 const int should_update = !cpi->ss_cfg[SS_CFG_SRC].stride ||
3747 !cpi->ss_cfg[SS_CFG_LOOKAHEAD].stride ||
3748 (y_stride != cpi->ss_cfg[SS_CFG_SRC].stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003749
chiyotsai836b69b2019-04-09 13:41:24 -07003750 if (!should_update) {
3751 return;
3752 }
3753
3754 if (cpi->sf.mv.search_method == DIAMOND) {
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05303755 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
3756 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD],
3757 y_stride_src);
chiyotsai836b69b2019-04-09 13:41:24 -07003758 } else {
3759 // Update the offsets in search_sites as y_stride can change due to scaled
3760 // references. This update allows NSTEP to be used on scaled references as
3761 // long as sf.mv.search_method is not DIAMOND. Currently in the codebae,
3762 // sf.mv.search_method is never set to DIAMOND.
3763 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
3764 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD], y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003765 }
3766}
3767
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003768#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003769static void set_restoration_unit_size(int width, int height, int sx, int sy,
3770 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003771 (void)width;
3772 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003773 (void)sx;
3774 (void)sy;
3775#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
3776 int s = AOMMIN(sx, sy);
3777#else
3778 int s = 0;
3779#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
3780
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003781 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08003782 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003783 else
Urvang Joshi813186b2018-03-08 15:38:46 -08003784 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003785 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
3786 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003787}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003788
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303789static void init_ref_frame_bufs(AV1_COMP *cpi) {
3790 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07003791 int i;
3792 BufferPool *const pool = cm->buffer_pool;
Jack Haughtonddb80602018-11-21 16:41:49 +00003793 cm->cur_frame = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -07003794 for (i = 0; i < REF_FRAMES; ++i) {
David Turnere7ebf902018-12-04 14:04:55 +00003795 cm->ref_frame_map[i] = NULL;
Wan-Teh Changd05e0332018-10-03 12:00:43 -07003796 }
3797 for (i = 0; i < FRAME_BUFFERS; ++i) {
Cheng Chen46f30c72017-09-07 11:13:33 -07003798 pool->frame_bufs[i].ref_count = 0;
3799 }
RogerZhou86902d02018-02-28 15:29:16 -08003800 if (cm->seq_params.force_screen_content_tools) {
Hui Su2d5fd742018-02-21 18:10:37 -08003801 for (i = 0; i < FRAME_BUFFERS; ++i) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303802 av1_hash_table_init(&pool->frame_bufs[i].hash_table, &cpi->td.mb);
Hui Su2d5fd742018-02-21 18:10:37 -08003803 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003804 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003805}
3806
Yaowu Xud3e7c682017-12-21 14:08:25 -08003807static void check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
Cheng Chen46f30c72017-09-07 11:13:33 -07003808 int subsampling_x, int subsampling_y) {
3809 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003810 SequenceHeader *const seq_params = &cm->seq_params;
Cheng Chen46f30c72017-09-07 11:13:33 -07003811
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003812 if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
3813 seq_params->subsampling_x != subsampling_x ||
3814 seq_params->subsampling_y != subsampling_y) {
3815 seq_params->subsampling_x = subsampling_x;
3816 seq_params->subsampling_y = subsampling_y;
3817 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07003818
3819 alloc_raw_frame_buffers(cpi);
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303820 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07003821 alloc_util_frame_buffers(cpi);
3822
3823 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
3824
3825 cpi->initial_width = cm->width;
3826 cpi->initial_height = cm->height;
3827 cpi->initial_mbs = cm->MBs;
3828 }
3829}
3830
3831// Returns 1 if the assigned width or height was <= 0.
3832static int set_size_literal(AV1_COMP *cpi, int width, int height) {
3833 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003834 const int num_planes = av1_num_planes(cm);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003835 check_initial_width(cpi, cm->seq_params.use_highbitdepth,
3836 cm->seq_params.subsampling_x,
3837 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07003838
3839 if (width <= 0 || height <= 0) return 1;
3840
3841 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07003842 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003843
3844 if (cpi->initial_width && cpi->initial_height &&
3845 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
3846 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003847 av1_free_pc_tree(&cpi->td, num_planes);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003848 alloc_compressor_data(cpi);
3849 realloc_segmentation_maps(cpi);
3850 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07003851 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003852 update_frame_size(cpi);
3853
3854 return 0;
3855}
3856
David Turner475a3132019-01-18 15:17:17 +00003857void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07003858 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003859 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003860 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07003861 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003862 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003863
Fergus Simpsonbc189932017-05-16 17:02:39 -07003864 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07003865 // There has been a change in the encoded frame size
Cheng Chen46f30c72017-09-07 11:13:33 -07003866 set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003867 set_mv_search_params(cpi);
Urvang Joshic8b52d52018-03-23 13:16:51 -07003868 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Cheng Chen09c83a52018-06-05 12:27:36 -07003869 cm->all_lossless = cm->coded_lossless && !av1_superres_scaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003870 }
3871
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003872 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003873 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003874 }
3875
David Turnere7ebf902018-12-04 14:04:55 +00003876 alloc_frame_mvs(cm, cm->cur_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003877
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303878 // Allocate above context buffers
Cherma Rajan Af1479082018-05-09 14:26:34 +05303879 if (cm->num_allocated_above_context_planes < av1_num_planes(cm) ||
3880 cm->num_allocated_above_context_mi_col < cm->mi_cols ||
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303881 cm->num_allocated_above_contexts < cm->tile_rows) {
3882 av1_free_above_context_buffers(cm, cm->num_allocated_above_contexts);
3883 if (av1_alloc_above_context_buffers(cm, cm->tile_rows))
3884 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3885 "Failed to allocate context buffers");
3886 }
3887
Yaowu Xuc27fc142016-08-22 16:08:15 -07003888 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003889 if (aom_realloc_frame_buffer(
Jack Haughtonddb80602018-11-21 16:41:49 +00003890 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
3891 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +05303892 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003893 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003894 "Failed to allocate frame buffer");
3895
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003896 const int frame_width = cm->superres_upscaled_width;
3897 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003898 set_restoration_unit_size(frame_width, frame_height,
3899 seq_params->subsampling_x,
3900 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003901 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01003902 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003903
3904 av1_alloc_restoration_buffers(cm);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003905 alloc_util_frame_buffers(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003906 init_motion_estimation(cpi);
3907
3908 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00003909 RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00003910 if (buf != NULL) {
David Turnera21966b2018-12-05 14:48:49 +00003911 struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
3912 av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
David Turner1bcefb32018-11-19 17:54:00 +00003913 buf->buf.y_crop_height, cm->width,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003914 cm->height);
David Turnera21966b2018-12-05 14:48:49 +00003915 if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003916 }
3917 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07003918
Hui Su5ebd8702018-01-08 18:09:20 -08003919 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003920 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003921
3922 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3923}
3924
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003925static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
3926 // Choose an arbitrary random number
3927 static unsigned int seed = 56789;
3928 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003929 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3930 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003931
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07003932 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003933 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003934 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003935 case RESIZE_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00003936 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07003937 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003938 else
Urvang Joshide71d142017-10-05 12:12:15 -07003939 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003940 break;
Urvang Joshide71d142017-10-05 12:12:15 -07003941 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003942 default: assert(0);
3943 }
Urvang Joshide71d142017-10-05 12:12:15 -07003944 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003945}
3946
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07003947#if CONFIG_SUPERRES_IN_RECODE
3948static int superres_in_recode_allowed(const AV1_COMP *const cpi) {
3949 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
3950 // Empirically found to not be beneficial for AOM_Q mode and images coding.
3951 return oxcf->superres_mode == SUPERRES_AUTO &&
3952 (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ) &&
3953 cpi->rc.frames_to_key > 1;
3954}
3955#endif // CONFIG_SUPERRES_IN_RECODE
3956
Urvang Joshi510d8f62019-01-10 12:11:50 -08003957#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO 0.012
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07003958#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME 0.008
3959#define SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME 0.008
Urvang Joshi510d8f62019-01-10 12:11:50 -08003960#define SUPERRES_ENERGY_BY_AC_THRESH 0.2
3961
3962static double get_energy_by_q2_thresh(const GF_GROUP *gf_group,
3963 const RATE_CONTROL *rc) {
3964 // TODO(now): Return keyframe thresh * factor based on frame type / pyramid
3965 // level.
3966 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
3967 return SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME;
3968 } else if (gf_group->update_type[gf_group->index] == KF_UPDATE) {
3969 if (rc->frames_to_key <= 1)
3970 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO;
3971 else
3972 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME;
3973 } else {
3974 assert(0);
3975 }
3976 return 0;
3977}
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003978
3979static uint8_t get_superres_denom_from_qindex_energy(int qindex, double *energy,
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003980 double threshq,
3981 double threshp) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003982 const double q = av1_convert_qindex_to_q(qindex, AOM_BITS_8);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003983 const double tq = threshq * q * q;
3984 const double tp = threshp * energy[1];
3985 const double thresh = AOMMIN(tq, tp);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003986 int k;
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07003987 for (k = SCALE_NUMERATOR * 2; k > SCALE_NUMERATOR; --k) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003988 if (energy[k - 1] > thresh) break;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003989 }
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003990 return 3 * SCALE_NUMERATOR - k;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003991}
3992
Urvang Joshi510d8f62019-01-10 12:11:50 -08003993static uint8_t get_superres_denom_for_qindex(const AV1_COMP *cpi, int qindex,
3994 int sr_kf, int sr_arf) {
3995 // Use superres for Key-frames and Alt-ref frames only.
3996 const GF_GROUP *gf_group = &cpi->gf_group;
3997 if (gf_group->update_type[gf_group->index] != KF_UPDATE &&
3998 gf_group->update_type[gf_group->index] != ARF_UPDATE) {
3999 return SCALE_NUMERATOR;
4000 }
4001 if (gf_group->update_type[gf_group->index] == KF_UPDATE && !sr_kf) {
4002 return SCALE_NUMERATOR;
4003 }
4004 if (gf_group->update_type[gf_group->index] == ARF_UPDATE && !sr_arf) {
4005 return SCALE_NUMERATOR;
4006 }
4007
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004008 double energy[16];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004009 analyze_hor_freq(cpi, energy);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004010
4011 const double energy_by_q2_thresh =
4012 get_energy_by_q2_thresh(gf_group, &cpi->rc);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004013 int denom = get_superres_denom_from_qindex_energy(
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004014 qindex, energy, energy_by_q2_thresh, SUPERRES_ENERGY_BY_AC_THRESH);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004015 /*
4016 printf("\nenergy = [");
4017 for (int k = 1; k < 16; ++k) printf("%f, ", energy[k]);
4018 printf("]\n");
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004019 printf("boost = %d\n",
4020 (gf_group->update_type[gf_group->index] == KF_UPDATE)
4021 ? cpi->rc.kf_boost
4022 : cpi->rc.gfu_boost);
4023 printf("denom = %d\n", denom);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004024 */
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004025#if CONFIG_SUPERRES_IN_RECODE
4026 if (superres_in_recode_allowed(cpi)) {
4027 // Force superres to be tried in the recode loop, as full-res is also going
4028 // to be tried anyway.
4029 denom = AOMMAX(denom, SCALE_NUMERATOR + 1);
4030 }
4031#endif // CONFIG_SUPERRES_IN_RECODE
Urvang Joshi510d8f62019-01-10 12:11:50 -08004032 return denom;
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004033}
4034
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004035static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
4036 // Choose an arbitrary random number
4037 static unsigned int seed = 34567;
4038 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004039 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4040 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07004041
4042 // Make sure that superres mode of the frame is consistent with the
4043 // sequence-level flag.
4044 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
4045 cpi->common.seq_params.enable_superres));
4046 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
4047 oxcf->superres_mode == SUPERRES_NONE));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004048
4049 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004050 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004051 case SUPERRES_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00004052 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004053 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004054 else
Urvang Joshide71d142017-10-05 12:12:15 -07004055 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004056 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004057 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004058 case SUPERRES_QTHRESH: {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07004059 // Do not use superres when screen content tools are used.
4060 if (cpi->common.allow_screen_content_tools) break;
Debargha Mukherjee2b2c5fd2018-11-14 13:21:24 -08004061 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4062 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004063
4064 // Now decide the use of superres based on 'q'.
Urvang Joshi2c92b072018-03-19 17:23:31 -07004065 int bottom_index, top_index;
4066 const int q = av1_rc_pick_q_and_bounds(
Jingning Han2e029872019-08-02 10:54:19 -07004067 cpi, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
4068 &bottom_index, &top_index);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004069
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004070 const int qthresh = (frame_is_intra_only(&cpi->common))
4071 ? oxcf->superres_kf_qthresh
4072 : oxcf->superres_qthresh;
Urvang Joshib17e0a62019-01-11 16:11:54 -08004073 if (q <= qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07004074 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004075 } else {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004076 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004077 }
4078 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004079 }
Urvang Joshi36a83732019-01-31 15:31:57 -08004080 case SUPERRES_AUTO: {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004081 // Do not use superres when screen content tools are used.
Urvang Joshi36a83732019-01-31 15:31:57 -08004082 if (cpi->common.allow_screen_content_tools) break;
Urvang Joshi510d8f62019-01-10 12:11:50 -08004083 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4084 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi36a83732019-01-31 15:31:57 -08004085
4086 // Now decide the use of superres based on 'q'.
4087 int bottom_index, top_index;
4088 const int q = av1_rc_pick_q_and_bounds(
Jingning Han2e029872019-08-02 10:54:19 -07004089 cpi, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
4090 &bottom_index, &top_index);
Urvang Joshi36a83732019-01-31 15:31:57 -08004091
4092 const int qthresh = 128;
4093 if (q <= qthresh) {
4094 new_denom = SCALE_NUMERATOR;
4095 } else {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004096 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Urvang Joshi36a83732019-01-31 15:31:57 -08004097 }
4098 break;
4099 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004100 default: assert(0);
4101 }
Urvang Joshide71d142017-10-05 12:12:15 -07004102 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004103}
4104
Urvang Joshide71d142017-10-05 12:12:15 -07004105static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4106 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4107}
4108
4109static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08004110 // Only need to check the width, as scaling is horizontal only.
4111 (void)oheight;
4112 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07004113}
4114
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004115static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004116 SUPERRES_MODE superres_mode, int owidth,
4117 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004118 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004119 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004120 }
4121
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004122 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07004123 int resize_denom =
4124 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4125 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4126
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004127 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004128 // Alter superres scale as needed to enforce conformity.
4129 rsz->superres_denom =
4130 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4131 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4132 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004133 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004134 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004135 // Alter resize scale as needed to enforce conformity.
4136 resize_denom =
4137 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004138 rsz->resize_width = owidth;
4139 rsz->resize_height = oheight;
4140 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004141 resize_denom);
4142 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4143 if (resize_denom > SCALE_NUMERATOR) {
4144 --resize_denom;
4145 rsz->resize_width = owidth;
4146 rsz->resize_height = oheight;
4147 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4148 resize_denom);
4149 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004150 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004151 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004152 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004153 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004154 if (resize_denom > rsz->superres_denom)
4155 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004156 else
Urvang Joshide71d142017-10-05 12:12:15 -07004157 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004158 rsz->resize_width = owidth;
4159 rsz->resize_height = oheight;
4160 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004161 resize_denom);
4162 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4163 (resize_denom > SCALE_NUMERATOR ||
4164 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08004165 } else { // We are allowed to alter neither resize scale nor superres
4166 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004167 return 0;
4168 }
Urvang Joshide71d142017-10-05 12:12:15 -07004169 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004170}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004171
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004172// Calculates resize and superres params for next frame
David Turner475a3132019-01-18 15:17:17 +00004173static size_params_type calculate_next_size_params(AV1_COMP *cpi) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004174 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08004175 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshi510d8f62019-01-10 12:11:50 -08004176 int resize_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004177 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004178 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4179 rsz.resize_width = cpi->resize_pending_width;
4180 rsz.resize_height = cpi->resize_pending_height;
4181 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4182 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004183 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004184 rsz.resize_width = cpi->oxcf.width;
4185 rsz.resize_height = cpi->oxcf.height;
4186 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004187 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004188 }
Urvang Joshide71d142017-10-05 12:12:15 -07004189 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004190 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
4191 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004192 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004193 return rsz;
4194}
4195
Urvang Joshi22b150b2019-01-10 14:32:32 -08004196static void setup_frame_size_from_params(AV1_COMP *cpi,
4197 const size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004198 int encode_width = rsz->resize_width;
4199 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004200
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004201 AV1_COMMON *cm = &cpi->common;
4202 cm->superres_upscaled_width = encode_width;
4203 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004204 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004205 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4206 rsz->superres_denom);
David Turner475a3132019-01-18 15:17:17 +00004207 av1_set_frame_size(cpi, encode_width, encode_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004208}
4209
David Turnerdedd8ff2019-01-23 13:59:46 +00004210void av1_setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004211 AV1_COMMON *cm = &cpi->common;
Urvang Joshi22b150b2019-01-10 14:32:32 -08004212 // Reset superres params from previous frame.
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004213 cm->superres_scale_denominator = SCALE_NUMERATOR;
David Turner475a3132019-01-18 15:17:17 +00004214 const size_params_type rsz = calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004215 setup_frame_size_from_params(cpi, &rsz);
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004216
Yaowu Xu7e450882019-04-30 15:09:18 -07004217 assert(av1_is_min_tile_width_satisfied(cm));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004218}
4219
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004220static void superres_post_encode(AV1_COMP *cpi) {
4221 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004222 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004223
Cheng Chen09c83a52018-06-05 12:27:36 -07004224 if (!av1_superres_scaled(cm)) return;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004225
Urvang Joshid6b5d512018-03-20 13:34:38 -07004226 assert(cpi->oxcf.enable_superres);
4227 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshic8b52d52018-03-23 13:16:51 -07004228 assert(!cm->all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07004229
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004230 av1_superres_upscale(cm, NULL);
4231
4232 // If regular resizing is occurring the source will need to be downscaled to
4233 // match the upscaled superres resolution. Otherwise the original source is
4234 // used.
Cheng Chen09c83a52018-06-05 12:27:36 -07004235 if (!av1_resize_scaled(cm)) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004236 cpi->source = cpi->unscaled_source;
4237 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4238 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004239 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4240 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08004241 // Do downscale. cm->(width|height) has been updated by
4242 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004243 if (aom_realloc_frame_buffer(
4244 &cpi->scaled_source, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004245 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4246 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
4247 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004248 aom_internal_error(
4249 &cm->error, AOM_CODEC_MEM_ERROR,
4250 "Failed to reallocate scaled source buffer for superres");
4251 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4252 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004253 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004254 (int)cm->seq_params.bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004255 cpi->source = &cpi->scaled_source;
4256 }
4257}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004258
Yue Chen92271e12019-07-09 16:28:02 -07004259static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
4260 MACROBLOCKD *xd, int use_restoration,
4261 int use_cdef) {
logangwf95c9162019-02-20 12:02:32 -08004262 if (use_restoration)
David Turnerc29e1a92018-12-06 14:10:14 +00004263 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004264
logangwf95c9162019-02-20 12:02:32 -08004265 if (use_cdef) {
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004266#if CONFIG_COLLECT_COMPONENT_TIMING
4267 start_timing(cpi, cdef_time);
4268#endif
Steinar Midtskogen59782122017-07-20 08:49:43 +02004269 // Find CDEF parameters
David Turnerc29e1a92018-12-06 14:10:14 +00004270 av1_cdef_search(&cm->cur_frame->buf, cpi->source, cm, xd,
Hui Sub93880a2019-06-27 18:29:30 -07004271 cpi->sf.cdef_pick_method, cpi->td.mb.rdmult);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004272
4273 // Apply the filter
David Turnerc29e1a92018-12-06 14:10:14 +00004274 av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004275#if CONFIG_COLLECT_COMPONENT_TIMING
4276 end_timing(cpi, cdef_time);
4277#endif
logangwf95c9162019-02-20 12:02:32 -08004278 } else {
4279 cm->cdef_info.cdef_bits = 0;
4280 cm->cdef_info.cdef_strengths[0] = 0;
4281 cm->cdef_info.nb_cdef_strengths = 1;
4282 cm->cdef_info.cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004283 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004284
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004285 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004286
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004287#if CONFIG_COLLECT_COMPONENT_TIMING
4288 start_timing(cpi, loop_restoration_time);
4289#endif
logangwf95c9162019-02-20 12:02:32 -08004290 if (use_restoration) {
David Turnerc29e1a92018-12-06 14:10:14 +00004291 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004292 av1_pick_filter_restoration(cpi->source, cpi);
4293 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4294 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4295 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304296 if (cpi->num_workers > 1)
David Turnerc29e1a92018-12-06 14:10:14 +00004297 av1_loop_restoration_filter_frame_mt(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304298 cpi->workers, cpi->num_workers,
4299 &cpi->lr_row_sync, &cpi->lr_ctxt);
4300 else
David Turnerc29e1a92018-12-06 14:10:14 +00004301 av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304302 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004303 }
logangwf95c9162019-02-20 12:02:32 -08004304 } else {
4305 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4306 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4307 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004308 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004309#if CONFIG_COLLECT_COMPONENT_TIMING
4310 end_timing(cpi, loop_restoration_time);
4311#endif
Fergus Simpsonbc189932017-05-16 17:02:39 -07004312}
4313
Yue Chen92271e12019-07-09 16:28:02 -07004314static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
4315 const int num_planes = av1_num_planes(cm);
4316 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4317
4318 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
4319 cm->coded_lossless && cm->all_lossless));
4320
4321 const int use_loopfilter = !cm->coded_lossless && !cm->large_scale_tile;
4322 const int use_cdef = cm->seq_params.enable_cdef && !cm->coded_lossless &&
4323 !cm->large_scale_tile;
4324 const int use_restoration = cm->seq_params.enable_restoration &&
4325 !cm->all_lossless && !cm->large_scale_tile;
4326
4327 struct loopfilter *lf = &cm->lf;
4328
4329#if CONFIG_COLLECT_COMPONENT_TIMING
4330 start_timing(cpi, loop_filter_time);
4331#endif
4332 if (use_loopfilter) {
4333 aom_clear_system_state();
4334 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
4335 } else {
4336 lf->filter_level[0] = 0;
4337 lf->filter_level[1] = 0;
4338 }
4339
4340 if (lf->filter_level[0] || lf->filter_level[1]) {
4341 if (cpi->num_workers > 1)
4342 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
4343#if CONFIG_LPF_MASK
4344 0,
4345#endif
4346 cpi->workers, cpi->num_workers,
4347 &cpi->lf_row_sync);
4348 else
4349 av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
4350#if CONFIG_LPF_MASK
4351 0,
4352#endif
4353 0, num_planes, 0);
4354 }
4355#if CONFIG_COLLECT_COMPONENT_TIMING
4356 end_timing(cpi, loop_filter_time);
4357#endif
4358
4359 cdef_restoration_frame(cpi, cm, xd, use_restoration, use_cdef);
4360}
4361
David Turnerf2b334c2018-12-13 13:00:55 +00004362static void fix_interp_filter(InterpFilter *const interp_filter,
4363 const FRAME_COUNTS *const counts) {
4364 if (*interp_filter == SWITCHABLE) {
4365 // Check to see if only one of the filters is actually used
4366 int count[SWITCHABLE_FILTERS] = { 0 };
4367 int num_filters_used = 0;
4368 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4369 for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
4370 count[i] += counts->switchable_interp[j][i];
4371 num_filters_used += (count[i] > 0);
4372 }
4373 if (num_filters_used == 1) {
4374 // Only one filter is used. So set the filter at frame level
4375 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4376 if (count[i]) {
4377 if (i == EIGHTTAP_REGULAR) *interp_filter = i;
4378 break;
4379 }
4380 }
4381 }
4382 }
4383}
4384
David Turner996b2c12018-12-07 15:52:30 +00004385static void finalize_encoded_frame(AV1_COMP *const cpi) {
4386 AV1_COMMON *const cm = &cpi->common;
David Turner99e990e2018-12-10 12:54:26 +00004387 CurrentFrame *const current_frame = &cm->current_frame;
David Turner996b2c12018-12-07 15:52:30 +00004388
David Turner99e990e2018-12-10 12:54:26 +00004389 if (!cm->seq_params.reduced_still_picture_hdr &&
4390 encode_show_existing_frame(cm)) {
4391 RefCntBuffer *const frame_to_show =
4392 cm->ref_frame_map[cpi->existing_fb_idx_to_show];
4393
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004394 if (frame_to_show == NULL) {
David Turner99e990e2018-12-10 12:54:26 +00004395 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4396 "Buffer does not contain a reconstructed frame");
4397 }
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004398 assert(frame_to_show->ref_count > 0);
David Turner99e990e2018-12-10 12:54:26 +00004399 assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
David Turner99e990e2018-12-10 12:54:26 +00004400 }
David Turner08f909c2018-12-18 13:29:14 +00004401
4402 if (!encode_show_existing_frame(cm) &&
4403 cm->seq_params.film_grain_params_present &&
4404 (cm->show_frame || cm->showable_frame)) {
4405 // Copy the current frame's film grain params to the its corresponding
4406 // RefCntBuffer slot.
4407 cm->cur_frame->film_grain_params = cm->film_grain_params;
4408
4409 // We must update the parameters if this is not an INTER_FRAME
4410 if (current_frame->frame_type != INTER_FRAME)
4411 cm->cur_frame->film_grain_params.update_parameters = 1;
4412
4413 // Iterate the random seed for the next frame.
4414 cm->film_grain_params.random_seed += 3381;
4415 if (cm->film_grain_params.random_seed == 0)
4416 cm->film_grain_params.random_seed = 7391;
4417 }
David Turnerf2b334c2018-12-13 13:00:55 +00004418
4419 // Initialise all tiles' contexts from the global frame context
4420 for (int tile_col = 0; tile_col < cm->tile_cols; tile_col++) {
4421 for (int tile_row = 0; tile_row < cm->tile_rows; tile_row++) {
4422 const int tile_idx = tile_row * cm->tile_cols + tile_col;
4423 cpi->tile_data[tile_idx].tctx = *cm->fc;
4424 }
4425 }
4426
4427 fix_interp_filter(&cm->interp_filter, cpi->td.counts);
David Turner996b2c12018-12-07 15:52:30 +00004428}
4429
Urvang Joshi57643372019-02-21 11:10:57 -08004430static int get_regulated_q_overshoot(AV1_COMP *const cpi, int q_low, int q_high,
4431 int top_index, int bottom_index) {
4432 const AV1_COMMON *const cm = &cpi->common;
4433 const RATE_CONTROL *const rc = &cpi->rc;
4434
4435 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4436
4437 int q_regulated =
4438 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4439 AOMMAX(q_high, top_index), cm->width, cm->height);
4440
4441 int retries = 0;
4442 while (q_regulated < q_low && retries < 10) {
4443 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4444 q_regulated =
4445 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4446 AOMMAX(q_high, top_index), cm->width, cm->height);
4447 retries++;
4448 }
4449 return q_regulated;
4450}
4451
4452static int get_regulated_q_undershoot(AV1_COMP *const cpi, int q_high,
4453 int top_index, int bottom_index) {
4454 const AV1_COMMON *const cm = &cpi->common;
4455 const RATE_CONTROL *const rc = &cpi->rc;
4456
4457 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4458 int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4459 top_index, cm->width, cm->height);
4460
4461 int retries = 0;
4462 while (q_regulated > q_high && retries < 10) {
4463 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4464 q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4465 top_index, cm->width, cm->height);
4466 retries++;
4467 }
4468 return q_regulated;
4469}
4470
David Turner2f3b5df2019-01-02 14:30:50 +00004471// Called after encode_with_recode_loop() has just encoded a frame and packed
4472// its bitstream. This function works out whether we under- or over-shot
4473// our bitrate target and adjusts q as appropriate. Also decides whether
4474// or not we should do another recode loop, indicated by *loop
Hui Suef139e12019-05-20 15:51:22 -07004475static void recode_loop_update_q(
4476 AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low,
4477 int *const q_high, const int top_index, const int bottom_index,
4478 int *const undershoot_seen, int *const overshoot_seen,
4479 int *const low_cr_seen, const int loop_at_this_size) {
David Turner2f3b5df2019-01-02 14:30:50 +00004480 AV1_COMMON *const cm = &cpi->common;
4481 RATE_CONTROL *const rc = &cpi->rc;
4482
Hui Suef139e12019-05-20 15:51:22 -07004483 const int min_cr = cpi->oxcf.min_cr;
4484 if (min_cr > 0) {
4485 aom_clear_system_state();
4486 const double compression_ratio =
4487 av1_get_compression_ratio(cm, rc->projected_frame_size >> 3);
4488 const double target_cr = min_cr / 100.0;
4489 if (compression_ratio < target_cr) {
4490 *low_cr_seen = 1;
4491 if (*q < rc->worst_quality) {
4492 const double cr_ratio = target_cr / compression_ratio;
4493 const int projected_q = AOMMAX(*q + 1, (int)(*q * cr_ratio * cr_ratio));
4494 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->worst_quality);
4495 *q_low = AOMMAX(*q, *q_low);
4496 *q_high = AOMMAX(*q, *q_high);
4497 *loop = 1;
4498 }
4499 }
4500 if (*low_cr_seen) return;
4501 }
4502
4503 if (cpi->oxcf.rc_mode == AOM_Q) return;
4504
David Turner2f3b5df2019-01-02 14:30:50 +00004505 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
4506 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4507 &frame_under_shoot_limit,
4508 &frame_over_shoot_limit);
4509 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4510
4511 if ((cm->current_frame.frame_type == KEY_FRAME) &&
4512 rc->this_key_frame_forced &&
4513 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4514 int last_q = *q;
4515 int64_t kf_err;
4516
4517 int64_t high_err_target = cpi->ambient_err;
4518 int64_t low_err_target = cpi->ambient_err >> 1;
4519
4520 if (cm->seq_params.use_highbitdepth) {
4521 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
4522 } else {
4523 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
4524 }
4525 // Prevent possible divide by zero error below for perfect KF
4526 kf_err += !kf_err;
4527
4528 // The key frame is not good enough or we can afford
4529 // to make it better without undue risk of popping.
4530 if ((kf_err > high_err_target &&
4531 rc->projected_frame_size <= frame_over_shoot_limit) ||
4532 (kf_err > low_err_target &&
4533 rc->projected_frame_size <= frame_under_shoot_limit)) {
4534 // Lower q_high
4535 *q_high = *q > *q_low ? *q - 1 : *q_low;
4536
4537 // Adjust Q
4538 *q = (int)((*q * high_err_target) / kf_err);
4539 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
4540 } else if (kf_err < low_err_target &&
4541 rc->projected_frame_size >= frame_under_shoot_limit) {
4542 // The key frame is much better than the previous frame
4543 // Raise q_low
4544 *q_low = *q < *q_high ? *q + 1 : *q_high;
4545
4546 // Adjust Q
4547 *q = (int)((*q * low_err_target) / kf_err);
4548 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
4549 }
4550
4551 // Clamp Q to upper and lower limits:
4552 *q = clamp(*q, *q_low, *q_high);
4553
4554 *loop = *q != last_q;
4555 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4556 frame_under_shoot_limit, *q,
4557 AOMMAX(*q_high, top_index), bottom_index)) {
4558 // Is the projected frame size out of range and are we allowed
4559 // to attempt to recode.
4560 int last_q = *q;
David Turner2f3b5df2019-01-02 14:30:50 +00004561
4562 // Frame size out of permitted range:
4563 // Update correction factor & compute new Q to try...
4564 // Frame is too large
4565 if (rc->projected_frame_size > rc->this_frame_target) {
4566 // Special case if the projected size is > the max allowed.
Urvang Joshi7344d3e2019-07-25 17:24:57 -07004567 if (*q == *q_high &&
4568 rc->projected_frame_size >= rc->max_frame_bandwidth) {
4569 const double q_val_high_current =
4570 av1_convert_qindex_to_q(*q_high, cm->seq_params.bit_depth);
4571 const double q_val_high_new =
4572 q_val_high_current *
4573 ((double)rc->projected_frame_size / rc->max_frame_bandwidth);
4574 *q_high = av1_find_qindex(q_val_high_new, cm->seq_params.bit_depth,
4575 rc->best_quality, rc->worst_quality);
4576 }
David Turner2f3b5df2019-01-02 14:30:50 +00004577
4578 // Raise Qlow as to at least the current value
4579 *q_low = *q < *q_high ? *q + 1 : *q_high;
4580
Urvang Joshi57643372019-02-21 11:10:57 -08004581 if (*undershoot_seen || loop_at_this_size > 2 ||
4582 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00004583 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4584
4585 *q = (*q_high + *q_low + 1) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08004586 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
4587 const int q_mid = (*q_high + *q_low + 1) / 2;
4588 const int q_regulated = get_regulated_q_overshoot(
4589 cpi, *q_low, *q_high, top_index, bottom_index);
4590 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
4591 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
4592 *q = (q_mid + q_regulated + 1) / 2;
David Turner2f3b5df2019-01-02 14:30:50 +00004593 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08004594 *q = get_regulated_q_overshoot(cpi, *q_low, *q_high, top_index,
4595 bottom_index);
David Turner2f3b5df2019-01-02 14:30:50 +00004596 }
4597
4598 *overshoot_seen = 1;
4599 } else {
4600 // Frame is too small
4601 *q_high = *q > *q_low ? *q - 1 : *q_low;
4602
Urvang Joshi57643372019-02-21 11:10:57 -08004603 if (*overshoot_seen || loop_at_this_size > 2 ||
4604 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00004605 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4606 *q = (*q_high + *q_low) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08004607 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
4608 const int q_mid = (*q_high + *q_low) / 2;
4609 const int q_regulated =
4610 get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
4611 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
4612 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
4613 *q = (q_mid + q_regulated) / 2;
4614
4615 // Special case reset for qlow for constrained quality.
4616 // This should only trigger where there is very substantial
4617 // undershoot on a frame and the auto cq level is above
4618 // the user passsed in value.
4619 if (cpi->oxcf.rc_mode == AOM_CQ && q_regulated < *q_low) {
4620 *q_low = *q;
4621 }
David Turner2f3b5df2019-01-02 14:30:50 +00004622 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08004623 *q = get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
4624
David Turner2f3b5df2019-01-02 14:30:50 +00004625 // Special case reset for qlow for constrained quality.
4626 // This should only trigger where there is very substantial
4627 // undershoot on a frame and the auto cq level is above
4628 // the user passsed in value.
4629 if (cpi->oxcf.rc_mode == AOM_CQ && *q < *q_low) {
4630 *q_low = *q;
4631 }
David Turner2f3b5df2019-01-02 14:30:50 +00004632 }
4633
4634 *undershoot_seen = 1;
4635 }
4636
4637 // Clamp Q to upper and lower limits:
4638 *q = clamp(*q, *q_low, *q_high);
4639
4640 *loop = (*q != last_q);
4641 } else {
4642 *loop = 0;
4643 }
4644}
4645
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304646static int get_interp_filter_selected(const AV1_COMMON *const cm,
4647 MV_REFERENCE_FRAME ref,
4648 InterpFilter ifilter) {
4649 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
4650 if (buf == NULL) return 0;
4651 return buf->interp_filter_selected[ifilter];
4652}
4653
Satish Kumar Suman0389a492019-07-31 13:35:58 +05304654static uint16_t setup_interp_filter_search_mask(AV1_COMP *cpi) {
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304655 const AV1_COMMON *const cm = &cpi->common;
4656 int ref_total[REF_FRAMES] = { 0 };
Satish Kumar Suman0389a492019-07-31 13:35:58 +05304657 uint16_t mask = ALLOW_ALL_INTERP_FILT_MASK;
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304658
4659 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
Satish Kumar Suman0389a492019-07-31 13:35:58 +05304660 return mask;
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304661
4662 for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
4663 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
4664 ++ifilter) {
4665 ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter);
4666 }
4667 }
4668 int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] +
4669 ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] +
4670 ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]);
4671
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304672 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
4673 ++ifilter) {
4674 int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30;
4675 if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) {
4676 int filter_score =
4677 get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 +
4678 get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 +
4679 get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 +
4680 get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 +
4681 get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 +
4682 get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10;
Satish Kumar Suman0389a492019-07-31 13:35:58 +05304683 if (filter_score < ref_total_total) {
4684 DUAL_FILTER_TYPE filt_type = ifilter + SWITCHABLE_FILTERS * ifilter;
4685 reset_interp_filter_allowed_mask(&mask, filt_type);
4686 }
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304687 }
4688 }
4689 return mask;
4690}
4691
Tom Finegane4099e32018-01-23 12:01:51 -08004692static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004693 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004694 RATE_CONTROL *const rc = &cpi->rc;
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004695 const int allow_recode = (cpi->sf.recode_loop != DISALLOW_RECODE);
Hui Sua1d71842019-07-31 12:02:24 -07004696 // Must allow recode if minimum compression ratio is set.
4697 assert(IMPLIES(cpi->oxcf.min_cr > 0, allow_recode));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004698
4699 set_size_independent_vars(cpi);
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304700 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
4701 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004702 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004703
David Turnerdedd8ff2019-01-23 13:59:46 +00004704 av1_setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004705
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004706#if CONFIG_SUPERRES_IN_RECODE
4707 if (superres_in_recode_allowed(cpi) &&
4708 cm->superres_scale_denominator == SCALE_NUMERATOR) {
4709 // Superres won't be picked, so no need to try, as we will go through
4710 // another recode loop for full-resolution after this anyway.
4711 return -1;
4712 }
4713#endif // CONFIG_SUPERRES_IN_RECODE
4714
David Turner2f3b5df2019-01-02 14:30:50 +00004715 int top_index = 0, bottom_index = 0;
4716 int q = 0, q_low = 0, q_high = 0;
4717 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4718 q_low = bottom_index;
4719 q_high = top_index;
4720
4721 // Loop variables
4722 int loop_count = 0;
4723 int loop_at_this_size = 0;
4724 int loop = 0;
4725 int overshoot_seen = 0;
4726 int undershoot_seen = 0;
Hui Suef139e12019-05-20 15:51:22 -07004727 int low_cr_seen = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004728
4729#if CONFIG_COLLECT_COMPONENT_TIMING
4730 printf("\n Encoding a frame:");
4731#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004732 do {
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004733 loop = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004734 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004735
Urvang Joshif1fa6862018-01-08 16:39:33 -08004736 // if frame was scaled calculate global_motion_search again if already
4737 // done
David Turner2f3b5df2019-01-02 14:30:50 +00004738 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004739 if (cpi->source->y_crop_width != cm->width ||
David Turner2f3b5df2019-01-02 14:30:50 +00004740 cpi->source->y_crop_height != cm->height) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004741 cpi->global_motion_search_done = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00004742 }
4743 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004744 cpi->source =
4745 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
David Turner2f3b5df2019-01-02 14:30:50 +00004746 if (cpi->unscaled_last_source != NULL) {
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004747 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4748 &cpi->scaled_last_source);
David Turner2f3b5df2019-01-02 14:30:50 +00004749 }
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004750
David Turner2f3b5df2019-01-02 14:30:50 +00004751 if (!frame_is_intra_only(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004752 if (loop_count > 0) {
4753 release_scaled_references(cpi);
4754 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004755 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004756 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004757 av1_set_quantizer(cm, q);
Yue Chena7d80482019-01-31 09:48:09 -08004758 av1_init_quantizer(cpi);
kyslov7b9d0d62018-12-21 11:12:26 -08004759
4760 av1_set_variance_partition_thresholds(cpi, q, 0);
4761
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004762 // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
4763 // cm->current_frame.frame_number, cm->show_frame, q,
4764 // cm->current_frame.frame_type, cm->superres_scale_denominator);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004765
David Turner2f3b5df2019-01-02 14:30:50 +00004766 if (loop_count == 0) {
4767 setup_frame(cpi);
4768 } else if (get_primary_ref_frame_buf(cm) == NULL) {
4769 // Base q-index may have changed, so we need to assign proper default coef
4770 // probs before every iteration.
Yaowu Xuf883b422016-08-30 14:01:10 -07004771 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08004772 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00004773 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004774
Yaowu Xuc27fc142016-08-22 16:08:15 -07004775 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004776 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004777 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004778 av1_setup_in_frame_q_adj(cpi);
David Turner2f3b5df2019-01-02 14:30:50 +00004779 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !allow_recode) {
4780 suppress_active_map(cpi);
4781 av1_cyclic_refresh_setup(cpi);
4782 apply_active_map(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004783 }
David Turner2f3b5df2019-01-02 14:30:50 +00004784
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004785 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004786 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004787 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
David Barker11c93562018-06-05 12:00:07 +01004788 } else {
Yaowu Xu7e450882019-04-30 15:09:18 -07004789 av1_calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004790 }
David Barkercab37552018-03-21 11:56:24 +00004791 } else {
4792 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004793 }
David Barkercab37552018-03-21 11:56:24 +00004794 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004795
David Turner2f3b5df2019-01-02 14:30:50 +00004796 if (allow_recode) save_coding_context(cpi);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004797#if CONFIG_COLLECT_COMPONENT_TIMING
4798 start_timing(cpi, av1_encode_frame_time);
4799#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004800 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004801 av1_encode_frame(cpi);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004802#if CONFIG_COLLECT_COMPONENT_TIMING
4803 end_timing(cpi, av1_encode_frame_time);
4804#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004805
Yaowu Xuf883b422016-08-30 14:01:10 -07004806 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004807
4808 // Dummy pack of the bitstream using up to date stats to get an
4809 // accurate estimate of output frame size to determine if we need
4810 // to recode.
Hui Sua1d71842019-07-31 12:02:24 -07004811 const int do_dummy_pack =
4812 cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF || cpi->oxcf.min_cr > 0;
4813 if (do_dummy_pack) {
Jingning Han8f661602017-08-19 08:16:50 -07004814 restore_coding_context(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004815
David Turner996b2c12018-12-07 15:52:30 +00004816 finalize_encoded_frame(cpi);
David Turner35cba132018-12-10 15:48:15 +00004817 int largest_tile_id = 0; // Output from bitstream: unused here
4818 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08004819 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004820
4821 rc->projected_frame_size = (int)(*size) << 3;
4822 restore_coding_context(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004823 }
4824
Hui Suef139e12019-05-20 15:51:22 -07004825 if (allow_recode) {
David Turner2f3b5df2019-01-02 14:30:50 +00004826 // Update q and decide whether to do a recode loop
4827 recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
4828 bottom_index, &undershoot_seen, &overshoot_seen,
Hui Suef139e12019-05-20 15:51:22 -07004829 &low_cr_seen, loop_at_this_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004830 }
4831
4832 // Special case for overlay frame.
4833 if (rc->is_src_frame_alt_ref &&
4834 rc->projected_frame_size < rc->max_frame_bandwidth)
4835 loop = 0;
4836
David Turner2f3b5df2019-01-02 14:30:50 +00004837 if (allow_recode && !cpi->sf.gm_disable_recode &&
4838 recode_loop_test_global_motion(cpi)) {
4839 loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004840 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004841
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004842 if (cpi->tpl_model_pass == 1) {
4843 assert(cpi->oxcf.enable_tpl_model == 2);
Debargha Mukherjee347c64d2019-05-08 13:53:46 -07004844 av1_tpl_setup_forward_stats(cpi);
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004845 cpi->tpl_model_pass = 0;
4846 loop = 1;
4847 }
4848
Yaowu Xuc27fc142016-08-22 16:08:15 -07004849 if (loop) {
4850 ++loop_count;
4851 ++loop_at_this_size;
4852
4853#if CONFIG_INTERNAL_STATS
4854 ++cpi->tot_recode_hits;
4855#endif
4856 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004857#if CONFIG_COLLECT_COMPONENT_TIMING
4858 if (loop) printf("\n Recoding:");
4859#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004860 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08004861
4862 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004863}
4864
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004865static int encode_with_recode_loop_and_filter(AV1_COMP *cpi, size_t *size,
4866 uint8_t *dest, int64_t *sse,
4867 int64_t *rate,
4868 int *largest_tile_id) {
4869#if CONFIG_COLLECT_COMPONENT_TIMING
4870 start_timing(cpi, encode_with_recode_loop_time);
4871#endif
4872 int err = encode_with_recode_loop(cpi, size, dest);
4873#if CONFIG_COLLECT_COMPONENT_TIMING
4874 end_timing(cpi, encode_with_recode_loop_time);
4875#endif
4876 if (err != AOM_CODEC_OK) {
4877 if (err == -1) {
4878 // special case as described in encode_with_recode_loop().
4879 // Encoding was skipped.
4880 err = AOM_CODEC_OK;
4881 if (sse != NULL) *sse = INT64_MAX;
4882 if (rate != NULL) *rate = INT64_MAX;
4883 *largest_tile_id = 0;
4884 }
4885 return err;
4886 }
4887
4888#ifdef OUTPUT_YUV_SKINMAP
4889 if (cpi->common.current_frame.frame_number > 1) {
4890 av1_compute_skin_map(cpi, yuv_skinmap_file);
4891 }
4892#endif // OUTPUT_YUV_SKINMAP
4893
4894 AV1_COMMON *const cm = &cpi->common;
4895 SequenceHeader *const seq_params = &cm->seq_params;
4896
4897 // Special case code to reduce pulsing when key frames are forced at a
4898 // fixed interval. Note the reconstruction error if it is the frame before
4899 // the force key frame
4900 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
4901 if (seq_params->use_highbitdepth) {
4902 cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
4903 } else {
4904 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
4905 }
4906 }
4907
4908 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
4909 cm->cur_frame->buf.transfer_characteristics =
4910 seq_params->transfer_characteristics;
4911 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
4912 cm->cur_frame->buf.monochrome = seq_params->monochrome;
4913 cm->cur_frame->buf.chroma_sample_position =
4914 seq_params->chroma_sample_position;
4915 cm->cur_frame->buf.color_range = seq_params->color_range;
4916 cm->cur_frame->buf.render_width = cm->render_width;
4917 cm->cur_frame->buf.render_height = cm->render_height;
4918
4919 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
4920 // off.
4921
4922 // Pick the loop filter level for the frame.
4923 if (!cm->allow_intrabc) {
4924 loopfilter_frame(cpi, cm);
4925 } else {
4926 cm->lf.filter_level[0] = 0;
4927 cm->lf.filter_level[1] = 0;
4928 cm->cdef_info.cdef_bits = 0;
4929 cm->cdef_info.cdef_strengths[0] = 0;
4930 cm->cdef_info.nb_cdef_strengths = 1;
4931 cm->cdef_info.cdef_uv_strengths[0] = 0;
4932 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4933 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4934 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
4935 }
4936
4937 // TODO(debargha): Fix mv search range on encoder side
4938 // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
4939 aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
4940
4941#ifdef OUTPUT_YUV_REC
4942 aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
4943#endif
4944
4945 finalize_encoded_frame(cpi);
4946 // Build the bitstream
4947#if CONFIG_COLLECT_COMPONENT_TIMING
4948 start_timing(cpi, av1_pack_bitstream_final_time);
4949#endif
4950 if (av1_pack_bitstream(cpi, dest, size, largest_tile_id) != AOM_CODEC_OK)
4951 return AOM_CODEC_ERROR;
4952#if CONFIG_COLLECT_COMPONENT_TIMING
4953 end_timing(cpi, av1_pack_bitstream_final_time);
4954#endif
4955
4956 // Compute sse and rate.
4957 if (sse != NULL) {
4958 *sse = (seq_params->use_highbitdepth)
4959 ? aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf)
4960 : aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
4961 }
4962 if (rate != NULL) {
4963 const int64_t bits = (*size << 3);
4964 *rate = (bits << 5); // To match scale.
4965 }
4966 return AOM_CODEC_OK;
4967}
4968
4969#if CONFIG_SUPERRES_IN_RECODE
4970
4971static void save_cur_buf(AV1_COMP *cpi) {
4972 CODING_CONTEXT *const cc = &cpi->coding_context;
4973 AV1_COMMON *cm = &cpi->common;
4974 const YV12_BUFFER_CONFIG *ybf = &cm->cur_frame->buf;
4975 memset(&cc->copy_buffer, 0, sizeof(cc->copy_buffer));
4976 if (aom_alloc_frame_buffer(&cc->copy_buffer, ybf->y_crop_width,
4977 ybf->y_crop_height, ybf->subsampling_x,
4978 ybf->subsampling_y,
4979 ybf->flags & YV12_FLAG_HIGHBITDEPTH, ybf->border,
4980 cm->byte_alignment) != AOM_CODEC_OK) {
4981 aom_internal_error(
4982 &cm->error, AOM_CODEC_MEM_ERROR,
4983 "Failed to allocate copy buffer for saving coding context");
4984 }
4985 aom_yv12_copy_frame(ybf, &cc->copy_buffer, av1_num_planes(cm));
4986}
4987
4988// Coding context that only needs to be saved when recode loop includes
4989// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
4990// restoraton).
4991static void save_extra_coding_context(AV1_COMP *cpi) {
4992 CODING_CONTEXT *const cc = &cpi->coding_context;
4993 AV1_COMMON *cm = &cpi->common;
4994
4995 cc->lf = cm->lf;
4996 cc->cdef_info = cm->cdef_info;
4997 cc->rc = cpi->rc;
4998}
4999
5000static void save_all_coding_context(AV1_COMP *cpi) {
5001 save_coding_context(cpi);
5002 save_cur_buf(cpi);
5003 save_extra_coding_context(cpi);
5004 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5005}
5006
5007static void restore_cur_buf(AV1_COMP *cpi) {
5008 CODING_CONTEXT *const cc = &cpi->coding_context;
5009 AV1_COMMON *cm = &cpi->common;
5010 aom_yv12_copy_frame(&cc->copy_buffer, &cm->cur_frame->buf,
5011 av1_num_planes(cm));
5012}
5013
5014// Coding context that only needs to be restored when recode loop includes
5015// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5016// restoraton).
5017static void restore_extra_coding_context(AV1_COMP *cpi) {
5018 CODING_CONTEXT *const cc = &cpi->coding_context;
5019 AV1_COMMON *cm = &cpi->common;
5020 cm->lf = cc->lf;
5021 cm->cdef_info = cc->cdef_info;
5022 cpi->rc = cc->rc;
5023}
5024
5025static void restore_all_coding_context(AV1_COMP *cpi) {
5026 restore_coding_context(cpi);
5027 restore_cur_buf(cpi);
5028 restore_extra_coding_context(cpi);
5029 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5030}
5031
5032static int encode_with_and_without_superres(AV1_COMP *cpi, size_t *size,
5033 uint8_t *dest,
5034 int *largest_tile_id) {
5035 const AV1_COMMON *const cm = &cpi->common;
5036 AV1EncoderConfig *const oxcf = &cpi->oxcf;
5037 assert(cm->seq_params.enable_superres);
5038 assert(superres_in_recode_allowed(cpi));
5039 aom_codec_err_t err = AOM_CODEC_OK;
5040 save_all_coding_context(cpi);
5041
5042 // Encode with superres.
5043 int64_t sse1 = INT64_MAX;
5044 int64_t rate1 = INT64_MAX;
5045 int largest_tile_id1;
5046 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse1, &rate1,
5047 &largest_tile_id1);
5048 if (err != AOM_CODEC_OK) return err;
5049
5050 // Encode without superres.
5051 restore_all_coding_context(cpi);
5052 int64_t sse2 = INT64_MAX;
5053 int64_t rate2 = INT64_MAX;
5054 int largest_tile_id2;
5055 oxcf->superres_mode = SUPERRES_NONE; // To force full-res.
5056 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
5057 &largest_tile_id2);
5058 oxcf->superres_mode = SUPERRES_AUTO; // Reset.
5059 if (err != AOM_CODEC_OK) return err;
5060
5061 // Note: Both use common rdmult based on base qindex of fullres.
5062 const int64_t rdmult =
5063 av1_compute_rd_mult_based_on_qindex(cpi, cm->base_qindex);
5064 const double proj_rdcost1 = RDCOST_DBL(rdmult, rate1, sse1);
5065 const double proj_rdcost2 = RDCOST_DBL(rdmult, rate2, sse2);
5066
5067 // Re-encode with superres if it's better.
5068 if (proj_rdcost1 < proj_rdcost2) {
5069 restore_all_coding_context(cpi);
5070 // TODO(urvang): We should avoid rerunning the recode loop by saving
5071 // previous output+state, or running encode only for the selected 'q' in
5072 // previous step.
5073 int64_t sse3 = INT64_MAX;
5074 int64_t rate3 = INT64_MAX;
5075 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
5076 largest_tile_id);
5077 assert(sse1 == sse3);
5078 assert(rate1 == rate3);
5079 assert(largest_tile_id1 == *largest_tile_id);
5080 } else {
5081 *largest_tile_id = largest_tile_id2;
5082 }
5083
5084 return err;
5085}
5086#endif // CONFIG_SUPERRES_IN_RECODE
5087
Yaowu Xuc27fc142016-08-22 16:08:15 -07005088#define DUMP_RECON_FRAMES 0
5089
5090#if DUMP_RECON_FRAMES == 1
5091// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07005092static void dump_filtered_recon_frames(AV1_COMP *cpi) {
5093 AV1_COMMON *const cm = &cpi->common;
David Turnerd2a592e2018-11-16 14:59:31 +00005094 const CurrentFrame *const current_frame = &cm->current_frame;
David Turnerc29e1a92018-12-06 14:10:14 +00005095 const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005096
Zoe Liub4f31032017-11-03 23:48:35 -07005097 if (recon_buf == NULL) {
David Turnerd2a592e2018-11-16 14:59:31 +00005098 printf("Frame %d is not ready.\n", current_frame->frame_number);
Zoe Liub4f31032017-11-03 23:48:35 -07005099 return;
5100 }
5101
Zoe Liu27deb382018-03-27 15:13:56 -07005102 static const int flag_list[REF_FRAMES] = { 0,
5103 AOM_LAST_FLAG,
5104 AOM_LAST2_FLAG,
5105 AOM_LAST3_FLAG,
5106 AOM_GOLD_FLAG,
5107 AOM_BWD_FLAG,
5108 AOM_ALT2_FLAG,
5109 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07005110 printf(
5111 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
5112 "show_existing_frame=%d) "
5113 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
David Turnerd2a592e2018-11-16 14:59:31 +00005114 current_frame->frame_number, current_frame->order_hint, cm->show_frame,
Zoe Liub4f31032017-11-03 23:48:35 -07005115 cm->show_existing_frame);
5116 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00005117 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
5118 const int ref_offset = buf != NULL ? (int)buf->order_hint : -1;
Urvang Joshib6f17672019-03-05 11:51:02 -08005119 printf(" %d(%c)", ref_offset,
5120 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N');
Zoe Liub4f31032017-11-03 23:48:35 -07005121 }
5122 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07005123
5124 if (!cm->show_frame) {
5125 printf("Frame %d is a no show frame, so no image dump.\n",
David Turnerd2a592e2018-11-16 14:59:31 +00005126 current_frame->frame_number);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005127 return;
5128 }
5129
Zoe Liub4f31032017-11-03 23:48:35 -07005130 int h;
5131 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
5132 FILE *f_recon = NULL;
5133
David Turnerd2a592e2018-11-16 14:59:31 +00005134 if (current_frame->frame_number == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005135 if ((f_recon = fopen(file_name, "wb")) == NULL) {
5136 printf("Unable to open file %s to write.\n", file_name);
5137 return;
5138 }
5139 } else {
5140 if ((f_recon = fopen(file_name, "ab")) == NULL) {
5141 printf("Unable to open file %s to append.\n", file_name);
5142 return;
5143 }
5144 }
5145 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07005146 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
5147 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
Urvang Joshi7a890232019-03-22 17:00:31 -07005148 "refresh_alt_ref_frame=%d, "
Zoe Liuf40a9572017-10-13 12:37:19 -07005149 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Sarah Parkere1b22012019-06-06 16:35:25 -07005150 current_frame->frame_number, cpi->gf_group.index,
5151 cpi->gf_group.update_type[cpi->gf_group.index], current_frame->order_hint,
5152 cm->show_frame, cm->show_existing_frame, cpi->rc.source_alt_ref_active,
5153 cpi->refresh_alt_ref_frame, recon_buf->y_stride, recon_buf->uv_stride,
5154 cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07005155#if 0
5156 int ref_frame;
5157 printf("get_ref_frame_map_idx: [");
5158 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
David Turnera21966b2018-12-05 14:48:49 +00005159 printf(" %d", get_ref_frame_map_idx(cm, ref_frame));
Zoe Liue9b15e22017-07-19 15:53:01 -07005160 printf(" ]\n");
Zoe Liue9b15e22017-07-19 15:53:01 -07005161#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07005162
5163 // --- Y ---
5164 for (h = 0; h < cm->height; ++h) {
5165 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
5166 f_recon);
5167 }
5168 // --- U ---
5169 for (h = 0; h < (cm->height >> 1); ++h) {
5170 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5171 f_recon);
5172 }
5173 // --- V ---
5174 for (h = 0; h < (cm->height >> 1); ++h) {
5175 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5176 f_recon);
5177 }
5178
5179 fclose(f_recon);
5180}
5181#endif // DUMP_RECON_FRAMES
5182
David Turnerefed6372019-01-11 15:14:11 +00005183static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
5184 const YV12_BUFFER_CONFIG *last_picture,
5185 hash_table *last_hash_table) {
5186 aom_clear_system_state();
5187 // check use hash ME
5188 int k;
5189 uint32_t hash_value_1;
5190 uint32_t hash_value_2;
5191
5192 const int block_size = 8;
5193 const double threshold_current = 0.8;
5194 const double threshold_average = 0.95;
5195 const int max_history_size = 32;
5196 int T = 0; // total block
5197 int C = 0; // match with collocated block
5198 int S = 0; // smooth region but not match with collocated block
5199 int M = 0; // match with other block
5200
5201 const int pic_width = cur_picture->y_width;
5202 const int pic_height = cur_picture->y_height;
5203 for (int i = 0; i + block_size <= pic_height; i += block_size) {
5204 for (int j = 0; j + block_size <= pic_width; j += block_size) {
5205 const int x_pos = j;
5206 const int y_pos = i;
5207 int match = 1;
5208 T++;
5209
5210 // check whether collocated block match with current
5211 uint8_t *p_cur = cur_picture->y_buffer;
5212 uint8_t *p_ref = last_picture->y_buffer;
5213 int stride_cur = cur_picture->y_stride;
5214 int stride_ref = last_picture->y_stride;
5215 p_cur += (y_pos * stride_cur + x_pos);
5216 p_ref += (y_pos * stride_ref + x_pos);
5217
5218 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
5219 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
5220 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
5221 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5222 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5223 if (p16_cur[tmpX] != p16_ref[tmpX]) {
5224 match = 0;
5225 }
5226 }
5227 p16_cur += stride_cur;
5228 p16_ref += stride_ref;
5229 }
5230 } else {
5231 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5232 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5233 if (p_cur[tmpX] != p_ref[tmpX]) {
5234 match = 0;
5235 }
5236 }
5237 p_cur += stride_cur;
5238 p_ref += stride_ref;
5239 }
5240 }
5241
5242 if (match) {
5243 C++;
5244 continue;
5245 }
5246
5247 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
5248 y_pos) ||
5249 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
5250 S++;
5251 continue;
5252 }
5253
5254 av1_get_block_hash_value(
5255 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
5256 block_size, &hash_value_1, &hash_value_2,
5257 (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH), &cpi->td.mb);
5258 // Hashing does not work for highbitdepth currently.
5259 // TODO(Roger): Make it work for highbitdepth.
5260 if (av1_use_hash_me(&cpi->common)) {
5261 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
5262 M++;
5263 }
5264 }
5265 }
5266 }
5267
5268 assert(T > 0);
5269 double csm_rate = ((double)(C + S + M)) / ((double)(T));
5270 double m_rate = ((double)(M)) / ((double)(T));
5271
5272 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
5273 cpi->m_rate_array[cpi->rate_index] = m_rate;
5274
5275 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
5276 cpi->rate_size++;
5277 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
5278
5279 if (csm_rate < threshold_current) {
5280 return 0;
5281 }
5282
5283 if (C == T) {
5284 return 1;
5285 }
5286
5287 double csm_average = 0.0;
5288 double m_average = 0.0;
5289
5290 for (k = 0; k < cpi->rate_size; k++) {
5291 csm_average += cpi->csm_rate_array[k];
5292 m_average += cpi->m_rate_array[k];
5293 }
5294 csm_average /= cpi->rate_size;
5295 m_average /= cpi->rate_size;
5296
5297 if (csm_average < threshold_average) {
5298 return 0;
5299 }
5300
5301 if (M > (T - C - S) / 3) {
5302 return 1;
5303 }
5304
5305 if (csm_rate > 0.99 && m_rate > 0.01) {
5306 return 1;
5307 }
5308
5309 if (csm_average + m_average > 1.01) {
5310 return 1;
5311 }
5312
5313 return 0;
5314}
5315
David Turner73245762019-02-11 16:42:34 +00005316// Refresh reference frame buffers according to refresh_frame_flags.
5317static void refresh_reference_frames(AV1_COMP *cpi) {
5318 AV1_COMMON *const cm = &cpi->common;
5319 // All buffers are refreshed for shown keyframes and S-frames.
5320
5321 for (int ref_frame = 0; ref_frame < REF_FRAMES; ref_frame++) {
5322 if (((cm->current_frame.refresh_frame_flags >> ref_frame) & 1) == 1) {
5323 assign_frame_buffer_p(&cm->ref_frame_map[ref_frame], cm->cur_frame);
5324 }
5325 }
5326}
5327
sdengc23c7f12019-06-11 16:56:50 -07005328// Implementation and modifications of C. Yeo, H. L. Tan, and Y. H. Tan, "On
5329// rate distortion optimization using SSIM," Circuits and Systems for Video
5330// Technology, IEEE Transactions on, vol. 23, no. 7, pp. 1170-1181, 2013.
sdeng32185d12019-06-19 14:47:09 -07005331// SSIM_VAR_SCALE defines the strength of the bias towards SSIM in RDO:
5332// Test data set: mid_res (33 frames)
5333// SSIM_VAR_SCALE avg_psnr ssim ms-ssim
5334// 8 8.2 -6.0 -6.4
5335// 16 4.0 -5.7 -5.9
5336// 32 1.6 -4.4 -4.5
sdengc23c7f12019-06-11 16:56:50 -07005337#define SSIM_VAR_SCALE 16.0
5338static void set_mb_ssim_rdmult_scaling(AV1_COMP *cpi) {
5339 AV1_COMMON *cm = &cpi->common;
5340 ThreadData *td = &cpi->td;
5341 MACROBLOCK *x = &td->mb;
5342 MACROBLOCKD *xd = &x->e_mbd;
5343 uint8_t *y_buffer = cpi->source->y_buffer;
5344 const int y_stride = cpi->source->y_stride;
5345 const int block_size = BLOCK_16X16;
5346
5347 const int num_mi_w = mi_size_wide[block_size];
5348 const int num_mi_h = mi_size_high[block_size];
5349 const int num_cols = (cm->mi_cols + num_mi_w - 1) / num_mi_w;
5350 const int num_rows = (cm->mi_rows + num_mi_h - 1) / num_mi_h;
5351 double log_sum = 0.0;
5352 int row, col;
5353 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
5354
sdeng32185d12019-06-19 14:47:09 -07005355 // TODO(sdeng): tune this param for 12bit videos.
5356 double c2 = 58.5225; // (.03*255)^2
5357 c2 *= SSIM_VAR_SCALE;
sdengc23c7f12019-06-11 16:56:50 -07005358
5359 // Loop through each 16x16 block.
5360 for (row = 0; row < num_rows; ++row) {
5361 for (col = 0; col < num_cols; ++col) {
5362 int mi_row, mi_col;
5363 double var = 0.0, num_of_var = 0.0;
5364 const int index = row * num_cols + col;
5365
5366 // Loop through each 8x8 block.
5367 for (mi_row = row * num_mi_h;
5368 mi_row < cm->mi_rows && mi_row < (row + 1) * num_mi_h; mi_row += 2) {
5369 for (mi_col = col * num_mi_w;
5370 mi_col < cm->mi_cols && mi_col < (col + 1) * num_mi_w;
5371 mi_col += 2) {
5372 struct buf_2d buf;
5373 const int row_offset_y = mi_row << 2;
5374 const int col_offset_y = mi_col << 2;
5375
5376 buf.buf = y_buffer + row_offset_y * y_stride + col_offset_y;
5377 buf.stride = y_stride;
5378
5379 if (use_hbd) {
5380 var += av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8,
5381 xd->bd);
5382 } else {
5383 var += av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8);
5384 }
5385
5386 num_of_var += 1.0;
5387 }
5388 }
sdeng32185d12019-06-19 14:47:09 -07005389 var = var / num_of_var;
sdengc23c7f12019-06-11 16:56:50 -07005390 var = 2.0 * var + c2;
5391 cpi->ssim_rdmult_scaling_factors[index] = var;
5392 log_sum += log(var);
5393 }
5394 }
5395 log_sum = exp(log_sum / (double)(num_rows * num_cols));
5396
5397 for (row = 0; row < num_rows; ++row) {
5398 for (col = 0; col < num_cols; ++col) {
5399 const int index = row * num_cols + col;
5400 cpi->ssim_rdmult_scaling_factors[index] /= log_sum;
5401 }
5402 }
5403
5404 (void)xd;
5405}
5406
chiyotsaifc1404d2019-08-08 12:09:12 -07005407#if CONFIG_DEBUG
5408static int hash_me_has_at_most_two_refs(RefCntBuffer *frame_bufs) {
5409 int total_count = 0;
5410 for (int frame_idx = 0; frame_idx < FRAME_BUFFERS; ++frame_idx) {
5411 if (frame_bufs[frame_idx].hash_table.has_content > 1) {
5412 return 0;
5413 }
5414 total_count += frame_bufs[frame_idx].hash_table.has_content;
5415 }
5416
5417 return total_count <= 2;
5418}
5419#endif
5420
David Turner73245762019-02-11 16:42:34 +00005421static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
5422 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005423 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005424 SequenceHeader *const seq_params = &cm->seq_params;
David Turnerd2a592e2018-11-16 14:59:31 +00005425 CurrentFrame *const current_frame = &cm->current_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07005426 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005427 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01005428
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005429#if CONFIG_COLLECT_COMPONENT_TIMING
5430 start_timing(cpi, encode_frame_to_data_rate_time);
5431#endif
5432
Fangwen Fu8d164de2016-12-14 13:40:54 -08005433 // frame type has been decided outside of this function call
David Turnerd2a592e2018-11-16 14:59:31 +00005434 cm->cur_frame->frame_type = current_frame->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07005435
Yunqing Wang9612d552018-05-15 14:58:30 -07005436 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
5437 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wang9612d552018-05-15 14:58:30 -07005438
sarahparker21dbca42018-03-30 17:43:44 -07005439 cm->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
Yunqing Wangd48fb162018-06-15 10:55:28 -07005440 // cm->allow_ref_frame_mvs needs to be written into the frame header while
5441 // cm->large_scale_tile is 1, therefore, "cm->large_scale_tile=1" case is
5442 // separated from frame_might_allow_ref_frame_mvs().
5443 cm->allow_ref_frame_mvs &= !cm->large_scale_tile;
5444
Debargha Mukherjee1d7217e2018-03-26 13:32:13 -07005445 cm->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07005446 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005447
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305448 cm->last_frame_type = current_frame->frame_type;
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305449
Sarah Parker33005522018-07-27 14:46:25 -07005450 if (encode_show_existing_frame(cm)) {
Jingning Han8f661602017-08-19 08:16:50 -07005451 restore_coding_context(cpi);
Zoe Liub4f31032017-11-03 23:48:35 -07005452
David Turner996b2c12018-12-07 15:52:30 +00005453 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005454 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00005455 int largest_tile_id = 0; // Output from bitstream: unused here
5456 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08005457 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005458
David Turner90311862018-11-29 13:34:36 +00005459 if (seq_params->frame_id_numbers_present_flag &&
5460 current_frame->frame_type == KEY_FRAME) {
5461 // Displaying a forward key-frame, so reset the ref buffer IDs
5462 int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
5463 for (int i = 0; i < REF_FRAMES; i++)
5464 cm->ref_frame_id[i] = display_frame_id;
5465 }
5466
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005467 cpi->seq_params_locked = 1;
5468
Yaowu Xuc27fc142016-08-22 16:08:15 -07005469#if DUMP_RECON_FRAMES == 1
5470 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
5471 dump_filtered_recon_frames(cpi);
5472#endif // DUMP_RECON_FRAMES
5473
David Turner73245762019-02-11 16:42:34 +00005474 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
5475 // for the purpose to verify no mismatch between encoder and decoder.
5476 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005477
David Turner73245762019-02-11 16:42:34 +00005478 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005479
Yaowu Xuc27fc142016-08-22 16:08:15 -07005480 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
5481 // to do post-encoding update accordingly.
5482 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005483 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07005484 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005485 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005486
David Turnerd2a592e2018-11-16 14:59:31 +00005487 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005488
Tom Finegane4099e32018-01-23 12:01:51 -08005489 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005490 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005491
David Turnerefed6372019-01-11 15:14:11 +00005492 // Work out whether to force_integer_mv this frame
5493 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools &&
5494 !frame_is_intra_only(cm)) {
5495 if (cpi->common.seq_params.force_integer_mv == 2) {
5496 // Adaptive mode: see what previous frame encoded did
5497 if (cpi->unscaled_last_source != NULL) {
5498 cm->cur_frame_force_integer_mv =
5499 is_integer_mv(cpi, cpi->source, cpi->unscaled_last_source,
5500 cpi->previous_hash_table);
5501 } else {
5502 cpi->common.cur_frame_force_integer_mv = 0;
5503 }
5504 } else {
5505 cpi->common.cur_frame_force_integer_mv =
5506 cpi->common.seq_params.force_integer_mv;
5507 }
5508 } else {
5509 cpi->common.cur_frame_force_integer_mv = 0;
5510 }
5511
chiyotsaifc1404d2019-08-08 12:09:12 -07005512#if CONFIG_DEBUG
5513 assert(hash_me_has_at_most_two_refs(cm->buffer_pool->frame_bufs) &&
5514 "Hash-me is leaking memory!");
5515#endif
5516
5517 if (cpi->oxcf.pass != 1 && cpi->need_to_clear_prev_hash_table) {
5518 av1_hash_table_clear_all(cpi->previous_hash_table);
5519 cpi->need_to_clear_prev_hash_table = 0;
5520 }
5521
Yaowu Xuc27fc142016-08-22 16:08:15 -07005522 // Set default state for segment based loop filter update flags.
5523 cm->lf.mode_ref_delta_update = 0;
5524
Yaowu Xuc27fc142016-08-22 16:08:15 -07005525 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05005526 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005527 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07005528 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005529
5530 // If segmentation is enabled force a map update for key frames.
5531 if (seg->enabled) {
5532 seg->update_map = 1;
5533 seg->update_data = 1;
5534 }
5535
5536 // The alternate reference frame cannot be active for a key frame.
5537 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005538 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00005539 if (cpi->oxcf.mtu == 0) {
5540 cm->num_tg = cpi->oxcf.num_tile_groups;
5541 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08005542 // Use a default value for the purposes of weighting costs in probability
5543 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00005544 cm->num_tg = DEFAULT_MAX_NUM_TG;
5545 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005546
5547 // For 1 pass CBR, check if we are dropping this frame.
5548 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005549 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
David Turnerd2a592e2018-11-16 14:59:31 +00005550 current_frame->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005551 if (av1_rc_drop_frame(cpi)) {
5552 av1_rc_postencode_update_drop_frame(cpi);
David Turnera4c96252019-01-11 16:36:39 +00005553 release_scaled_references(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08005554 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005555 }
5556 }
5557
sdengc23c7f12019-06-11 16:56:50 -07005558 if (oxcf->tuning == AOM_TUNE_SSIM) set_mb_ssim_rdmult_scaling(cpi);
5559
Yaowu Xuf883b422016-08-30 14:01:10 -07005560 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005561
5562#if CONFIG_INTERNAL_STATS
5563 memset(cpi->mode_chosen_counts, 0,
5564 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
5565#endif
5566
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005567 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005568 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08005569 * wraparound) */
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005570 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00005571 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08005572 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07005573 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
5574 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
5575 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005576 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005577 lsb = cpi->source->y_buffer[0] & 0xff;
5578 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005579 }
David Turner760a2f42018-12-07 15:25:36 +00005580 cm->current_frame_id =
5581 ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05005582
5583 // S_frame is meant for stitching different streams of different
5584 // resolutions together, so current_frame_id must be the
5585 // same across different streams of the same content current_frame_id
5586 // should be the same and not random. 0x37 is a chosen number as start
5587 // point
5588 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005589 } else {
5590 cm->current_frame_id =
David Turner760a2f42018-12-07 15:25:36 +00005591 (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
5592 (1 << seq_params->frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005593 }
5594 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005595
Hui Su483a8452018-02-26 12:28:48 -08005596 switch (cpi->oxcf.cdf_update_mode) {
5597 case 0: // No CDF update for any frames(4~6% compression loss).
5598 cm->disable_cdf_update = 1;
5599 break;
5600 case 1: // Enable CDF update for all frames.
5601 cm->disable_cdf_update = 0;
5602 break;
5603 case 2:
5604 // Strategically determine at which frames to do CDF update.
5605 // Currently only enable CDF update for all-intra and no-show frames(1.5%
5606 // compression loss).
5607 // TODO(huisu@google.com): design schemes for various trade-offs between
5608 // compression quality and decoding speed.
Hui Sub1b76b32018-02-27 15:24:48 -08005609 cm->disable_cdf_update =
5610 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08005611 break;
5612 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005613 cm->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08005614
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005615 if (cpi->oxcf.pass == 2 && cpi->oxcf.enable_tpl_model == 2 &&
5616 current_frame->frame_type == INTER_FRAME) {
5617 if (!cm->show_frame) {
5618 assert(cpi->tpl_model_pass == 0);
5619 cpi->tpl_model_pass = 1;
5620 }
5621 }
5622
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005623 int largest_tile_id = 0;
5624#if CONFIG_SUPERRES_IN_RECODE
5625 if (superres_in_recode_allowed(cpi)) {
5626 if (encode_with_and_without_superres(cpi, size, dest, &largest_tile_id) !=
5627 AOM_CODEC_OK) {
5628 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005629 }
Hui Su06463e42018-02-23 22:17:36 -08005630 } else {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005631#endif // CONFIG_SUPERRES_IN_RECODE
5632 if (encode_with_recode_loop_and_filter(cpi, size, dest, NULL, NULL,
5633 &largest_tile_id) != AOM_CODEC_OK) {
5634 return AOM_CODEC_ERROR;
5635 }
5636#if CONFIG_SUPERRES_IN_RECODE
Hui Su06463e42018-02-23 22:17:36 -08005637 }
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005638#endif // CONFIG_SUPERRES_IN_RECODE
Yaowu Xuc27fc142016-08-22 16:08:15 -07005639
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005640 cpi->seq_params_locked = 1;
5641
David Turner996b2c12018-12-07 15:52:30 +00005642 // Update reference frame ids for reference frames this frame will overwrite
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005643 if (seq_params->frame_id_numbers_present_flag) {
David Turner996b2c12018-12-07 15:52:30 +00005644 for (int i = 0; i < REF_FRAMES; i++) {
5645 if ((current_frame->refresh_frame_flags >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005646 cm->ref_frame_id[i] = cm->current_frame_id;
5647 }
5648 }
5649 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005650
Yaowu Xuc27fc142016-08-22 16:08:15 -07005651#if DUMP_RECON_FRAMES == 1
5652 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07005653 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005654#endif // DUMP_RECON_FRAMES
5655
Soo-Chul Han934af352017-10-15 15:21:51 -04005656 if (cm->seg.enabled) {
5657 if (cm->seg.update_map) {
5658 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07005659 } else if (cm->last_frame_seg_map) {
David Turnerb757ce02018-11-12 15:01:28 +00005660 memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
Soo-Chul Han934af352017-10-15 15:21:51 -04005661 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
5662 }
5663 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005664
5665 if (frame_is_intra_only(cm) == 0) {
5666 release_scaled_references(cpi);
5667 }
5668
David Turner73245762019-02-11 16:42:34 +00005669 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
5670 // for the purpose to verify no mismatch between encoder and decoder.
5671 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
5672
5673 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005674
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005675#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07005676 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005677#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005678
Hui Sudc54be62018-03-14 19:14:28 -07005679 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
David Turner35cba132018-12-10 15:48:15 +00005680 *cm->fc = cpi->tile_data[largest_tile_id].tctx;
Hui Sudc54be62018-03-14 19:14:28 -07005681 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005682 }
David Turnera4c96252019-01-11 16:36:39 +00005683 if (!cm->large_scale_tile) {
5684 cm->cur_frame->frame_context = *cm->fc;
5685 }
5686#define EXT_TILE_DEBUG 0
5687#if EXT_TILE_DEBUG
5688 if (cm->large_scale_tile && oxcf->pass == 2) {
5689 char fn[20] = "./fc";
5690 fn[4] = current_frame->frame_number / 100 + '0';
5691 fn[5] = (current_frame->frame_number % 100) / 10 + '0';
5692 fn[6] = (current_frame->frame_number % 10) + '0';
5693 fn[7] = '\0';
5694 av1_print_frame_contexts(cm->fc, fn);
5695 }
5696#endif // EXT_TILE_DEBUG
5697#undef EXT_TILE_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07005698
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005699#if CONFIG_COLLECT_COMPONENT_TIMING
5700 end_timing(cpi, encode_frame_to_data_rate_time);
5701
5702 // Print out timing information.
5703 int i;
chiyotsai9c484b32019-03-07 16:01:50 -08005704 fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n",
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005705 cm->current_frame.frame_number,
chiyotsai9c484b32019-03-07 16:01:50 -08005706 get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005707 for (i = 0; i < kTimingComponents; i++) {
5708 cpi->component_time[i] += cpi->frame_component_time[i];
5709 fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n",
5710 get_component_name(i), cpi->frame_component_time[i],
5711 cpi->component_time[i]);
5712 cpi->frame_component_time[i] = 0;
5713 }
5714#endif
5715
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305716 cm->last_frame_type = current_frame->frame_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005717
Yaowu Xuf883b422016-08-30 14:01:10 -07005718 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005719
chiyotsaifc1404d2019-08-08 12:09:12 -07005720 // Store encoded frame's hash table for in_integer_mv() next time.
5721 // Beware! If we don't update previous_hash_table here we will leak the
5722 // items stored in cur_frame's hash_table!
5723 if (oxcf->pass != 1 && av1_use_hash_me(cm)) {
David Turnerefed6372019-01-11 15:14:11 +00005724 cpi->previous_hash_table = &cm->cur_frame->hash_table;
chiyotsaifc1404d2019-08-08 12:09:12 -07005725 cpi->need_to_clear_prev_hash_table = 1;
David Turnerefed6372019-01-11 15:14:11 +00005726 }
5727
Yaowu Xuc27fc142016-08-22 16:08:15 -07005728 // Clear the one shot update flags for segmentation map and mode/ref loop
5729 // filter deltas.
5730 cm->seg.update_map = 0;
5731 cm->seg.update_data = 0;
5732 cm->lf.mode_ref_delta_update = 0;
5733
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07005734 // A droppable frame might not be shown but it always
5735 // takes a space in the gf group. Therefore, even when
5736 // it is not shown, we still need update the count down.
5737
Yaowu Xuc27fc142016-08-22 16:08:15 -07005738 if (cm->show_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005739 // Don't increment frame counters if this was an altref buffer
5740 // update not a real frame
David Turnerd2a592e2018-11-16 14:59:31 +00005741 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005742 }
5743
Tom Finegane4099e32018-01-23 12:01:51 -08005744 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005745}
5746
David Turner056f7cd2019-01-07 17:48:13 +00005747int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
David Turnercb5e36f2019-01-17 17:15:25 +00005748 const EncodeFrameInput *const frame_input,
David Turner056f7cd2019-01-07 17:48:13 +00005749 const EncodeFrameParams *const frame_params,
5750 EncodeFrameResults *const frame_results) {
David Turner07dbd8e2019-01-08 17:16:25 +00005751 AV1_COMMON *const cm = &cpi->common;
David Turnera7f133c2019-01-22 14:47:16 +00005752 CurrentFrame *const current_frame = &cm->current_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00005753
David Turnercb5e36f2019-01-17 17:15:25 +00005754 cpi->unscaled_source = frame_input->source;
5755 cpi->source = frame_input->source;
5756 cpi->unscaled_last_source = frame_input->last_source;
David Turner056f7cd2019-01-07 17:48:13 +00005757
David Turner6e8b4d92019-02-18 15:01:33 +00005758 current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
David Turner07dbd8e2019-01-08 17:16:25 +00005759 cm->error_resilient_mode = frame_params->error_resilient_mode;
David Turnera7f133c2019-01-22 14:47:16 +00005760 cm->primary_ref_frame = frame_params->primary_ref_frame;
David Turner475a3132019-01-18 15:17:17 +00005761 cm->current_frame.frame_type = frame_params->frame_type;
David Turnerdedd8ff2019-01-23 13:59:46 +00005762 cm->show_frame = frame_params->show_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00005763 cpi->ref_frame_flags = frame_params->ref_frame_flags;
David Turner04b70d82019-01-24 15:39:19 +00005764 cpi->speed = frame_params->speed;
David Turnere86ee0d2019-02-18 17:16:28 +00005765 cm->show_existing_frame = frame_params->show_existing_frame;
5766 cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
David Turner07dbd8e2019-01-08 17:16:25 +00005767
David Turner73245762019-02-11 16:42:34 +00005768 memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
5769 REF_FRAMES * sizeof(*cm->remapped_ref_idx));
5770
David Turnerfe3aecb2019-02-06 14:42:42 +00005771 cpi->refresh_last_frame = frame_params->refresh_last_frame;
5772 cpi->refresh_golden_frame = frame_params->refresh_golden_frame;
5773 cpi->refresh_bwd_ref_frame = frame_params->refresh_bwd_ref_frame;
5774 cpi->refresh_alt2_ref_frame = frame_params->refresh_alt2_ref_frame;
5775 cpi->refresh_alt_ref_frame = frame_params->refresh_alt_ref_frame;
5776
David Turnera7f133c2019-01-22 14:47:16 +00005777 if (current_frame->frame_type == KEY_FRAME && cm->show_frame)
5778 current_frame->frame_number = 0;
5779
5780 if (cm->show_existing_frame) {
5781 current_frame->order_hint = cm->cur_frame->order_hint;
Ravi Chaudhary9701cd62019-07-18 17:32:26 +05305782 current_frame->display_order_hint = cm->cur_frame->display_order_hint;
David Turnera7f133c2019-01-22 14:47:16 +00005783 } else {
5784 current_frame->order_hint =
5785 current_frame->frame_number + frame_params->order_offset;
Ravi Chaudhary9701cd62019-07-18 17:32:26 +05305786 current_frame->display_order_hint = current_frame->order_hint;
David Turnera7f133c2019-01-22 14:47:16 +00005787 current_frame->order_hint %=
5788 (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
5789 }
5790
David Turnercb5e36f2019-01-17 17:15:25 +00005791 if (cpi->oxcf.pass == 1) {
Jerome Jiang2612b4d2019-05-29 17:46:47 -07005792#if !CONFIG_REALTIME_ONLY
David Turnercb5e36f2019-01-17 17:15:25 +00005793 av1_first_pass(cpi, frame_input->ts_duration);
Jerome Jiang2612b4d2019-05-29 17:46:47 -07005794#endif
David Turnercb5e36f2019-01-17 17:15:25 +00005795 } else if (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) {
David Turner73245762019-02-11 16:42:34 +00005796 if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
5797 AOM_CODEC_OK) {
David Turnercb5e36f2019-01-17 17:15:25 +00005798 return AOM_CODEC_ERROR;
5799 }
5800 } else {
David Turner056f7cd2019-01-07 17:48:13 +00005801 return AOM_CODEC_ERROR;
5802 }
5803
5804 return AOM_CODEC_OK;
5805}
5806
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005807#if CONFIG_DENOISE
5808static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
5809 int block_size, float noise_level,
5810 int64_t time_stamp, int64_t end_time) {
5811 AV1_COMMON *const cm = &cpi->common;
5812 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005813 cpi->denoise_and_model = aom_denoise_and_model_alloc(
5814 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005815 if (!cpi->denoise_and_model) {
5816 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5817 "Error allocating denoise and model");
5818 return -1;
5819 }
5820 }
5821 if (!cpi->film_grain_table) {
5822 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
5823 if (!cpi->film_grain_table) {
5824 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5825 "Error allocating grain table");
5826 return -1;
5827 }
5828 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
5829 }
5830 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
5831 &cm->film_grain_params)) {
5832 if (cm->film_grain_params.apply_grain) {
5833 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
5834 &cm->film_grain_params);
5835 }
5836 }
5837 return 0;
5838}
5839#endif
5840
James Zern3e2613b2017-03-30 23:14:40 -07005841int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005842 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5843 int64_t end_time) {
5844 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005845 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005846 int res = 0;
5847 const int subsampling_x = sd->subsampling_x;
5848 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005849 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005850
Yaowu Xuc27fc142016-08-22 16:08:15 -07005851 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005852
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005853#if CONFIG_INTERNAL_STATS
5854 struct aom_usec_timer timer;
Yaowu Xuf883b422016-08-30 14:01:10 -07005855 aom_usec_timer_start(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005856#endif
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005857#if CONFIG_DENOISE
5858 if (cpi->oxcf.noise_level > 0)
5859 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
5860 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
5861 res = -1;
5862#endif // CONFIG_DENOISE
5863
Yaowu Xuf883b422016-08-30 14:01:10 -07005864 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08005865 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005866 res = -1;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005867#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005868 aom_usec_timer_mark(&timer);
5869 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005870#endif
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005871 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005872 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005873 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005874 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005875 res = -1;
5876 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005877 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005878 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005879 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005880 "Profile 1 requires 4:4:4 color format");
5881 res = -1;
5882 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005883 if ((seq_params->profile == PROFILE_2) &&
5884 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005885 !(subsampling_x == 1 && subsampling_y == 0)) {
5886 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
5887 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005888 res = -1;
5889 }
5890
5891 return res;
5892}
5893
Yaowu Xuc27fc142016-08-22 16:08:15 -07005894#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005895extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5896 const unsigned char *img2, int img2_pitch,
5897 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005898
5899static void adjust_image_stat(double y, double u, double v, double all,
5900 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07005901 s->stat[STAT_Y] += y;
5902 s->stat[STAT_U] += u;
5903 s->stat[STAT_V] += v;
5904 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07005905 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005906}
5907
Angie Chiang08a22a62017-07-17 17:29:17 -07005908static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005909 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005910 double samples = 0.0;
5911 uint32_t in_bit_depth = 8;
5912 uint32_t bit_depth = 8;
5913
Angie Chiang08a22a62017-07-17 17:29:17 -07005914#if CONFIG_INTER_STATS_ONLY
David Turnerd2a592e2018-11-16 14:59:31 +00005915 if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07005916#endif
5917 cpi->bytes += frame_bytes;
5918
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005919 if (cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005920 in_bit_depth = cpi->oxcf.input_bit_depth;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005921 bit_depth = cm->seq_params.bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005922 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005923 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005924 const YV12_BUFFER_CONFIG *orig = cpi->source;
David Turnerc29e1a92018-12-06 14:10:14 +00005925 const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005926 double y, u, v, frame_all;
5927
5928 cpi->count++;
5929 if (cpi->b_calculate_psnr) {
5930 PSNR_STATS psnr;
5931 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005932 aom_clear_system_state();
Yaowu Xud3e7c682017-12-21 14:08:25 -08005933 // TODO(yaowu): unify these two versions into one.
Yaowu Xuf883b422016-08-30 14:01:10 -07005934 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005935
5936 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5937 &cpi->psnr);
5938 cpi->total_sq_error += psnr.sse[0];
5939 cpi->total_samples += psnr.samples[0];
5940 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08005941 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005942 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005943 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07005944 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005945 else
Yaowu Xuf883b422016-08-30 14:01:10 -07005946 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005947
Yaowu Xuf883b422016-08-30 14:01:10 -07005948 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005949 cpi->summed_quality += frame_ssim2 * weight;
5950 cpi->summed_weights += weight;
5951
5952#if 0
5953 {
5954 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08005955 double y2 = psnr.psnr[1];
5956 double u2 = psnr.psnr[2];
5957 double v2 = psnr.psnr[3];
5958 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005959 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 +00005960 cm->current_frame.frame_number, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005961 frame_psnr2, frame_ssim2);
5962 fclose(f);
5963 }
5964#endif
5965 }
5966 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005967 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005968 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07005969 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5970 recon->y_stride, orig->y_width, orig->y_height);
5971 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005972 cpi->total_blockiness += frame_blockiness;
5973 }
5974
5975 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005976 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005977 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005978 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5979 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5980
5981 const double peak = (double)((1 << in_bit_depth) - 1);
5982 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005983 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005984 if (consistency > 0.0)
5985 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005986 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005987 cpi->total_inconsistency += this_inconsistency;
5988 }
5989 }
5990 }
5991
5992 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07005993 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005994 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07005995 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005996 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5997 }
5998}
5999#endif // CONFIG_INTERNAL_STATS
Andrey Norkin795ba872018-03-06 13:24:14 -08006000int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
6001 size_t *size, uint8_t *dest, int64_t *time_stamp,
6002 int64_t *time_end, int flush,
Yue Chen1bc5be62018-08-24 13:57:32 -07006003 const aom_rational64_t *timestamp_ratio) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006004 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
6005 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006006
6007#if CONFIG_BITSTREAM_DEBUG
6008 assert(cpi->oxcf.max_threads == 0 &&
6009 "bitstream debug tool does not support multithreading");
6010 bitstream_queue_record_write();
Yaowu Xu63f2ea32019-04-29 10:47:42 -07006011 aom_bitstream_queue_set_frame_write(cm->current_frame.frame_number * 2 +
6012 cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006013#endif
6014
Sarah Parker740e8392019-01-23 15:47:53 -08006015 // Indicates whether or not to use an adaptive quantize b rather than
6016 // the traditional version
6017 cm->use_quant_b_adapt = cpi->oxcf.quant_b_adapt;
6018
Dominic Symesd4929012018-01-31 17:32:01 +01006019 cm->showable_frame = 0;
David Turnere43f7fe2019-01-15 14:58:00 +00006020 *size = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006021#if CONFIG_INTERNAL_STATS
6022 struct aom_usec_timer cmptimer;
Yaowu Xuf883b422016-08-30 14:01:10 -07006023 aom_usec_timer_start(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006024#endif
RogerZhou3b635242017-09-19 10:06:46 -07006025 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006026
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07006027 // Normal defaults
sarahparker27d686a2018-03-30 17:43:44 -07006028 cm->refresh_frame_context = oxcf->frame_parallel_decoding_mode
6029 ? REFRESH_FRAME_CONTEXT_DISABLED
6030 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01006031 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08006032 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006033
Sarah Parkerb9041612018-05-22 19:06:47 -07006034 // Initialize fields related to forward keyframes
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07006035 cpi->no_show_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08006036
David Turnerdedd8ff2019-01-23 13:59:46 +00006037 if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006038
Yue Chen1bc5be62018-08-24 13:57:32 -07006039 const int result =
6040 av1_encode_strategy(cpi, size, dest, frame_flags, time_stamp, time_end,
6041 timestamp_ratio, flush);
David Turnerdedd8ff2019-01-23 13:59:46 +00006042 if (result != AOM_CODEC_OK && result != -1) {
David Turner1539bb02019-01-24 15:28:13 +00006043 return AOM_CODEC_ERROR;
David Turnerdedd8ff2019-01-23 13:59:46 +00006044 } else if (result == -1) {
6045 // Returning -1 indicates no frame encoded; more input is required
6046 return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006047 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006048#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006049 aom_usec_timer_mark(&cmptimer);
6050 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yue Chen1bc5be62018-08-24 13:57:32 -07006051#endif // CONFIG_INTERNAL_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00006052 if (cpi->b_calculate_psnr) {
6053 if (cm->show_existing_frame || (oxcf->pass != 1 && cm->show_frame)) {
6054 generate_psnr_packet(cpi);
6055 }
6056 }
Hui Su8ea87322019-03-29 14:44:32 -07006057
6058 if (cpi->keep_level_stats && oxcf->pass != 1) {
6059 // Initialize level info. at the beginning of each sequence.
6060 if (cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) {
Hui Su58753d62019-05-29 09:56:19 -07006061 av1_init_level_info(cpi);
Hui Su8ea87322019-03-29 14:44:32 -07006062 }
kyslovabeeb7c2019-03-06 18:35:04 -08006063 av1_update_level_info(cpi, *size, *time_stamp, *time_end);
Hui Su8ea87322019-03-29 14:44:32 -07006064 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006065
6066#if CONFIG_INTERNAL_STATS
6067 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006068 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006069 }
6070#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08006071#if CONFIG_SPEED_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00006072 if (cpi->oxcf.pass != 1 && !cm->show_existing_frame) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08006073 cpi->tx_search_count += cpi->td.mb.tx_search_count;
6074 cpi->td.mb.tx_search_count = 0;
6075 }
6076#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006077
Yaowu Xuf883b422016-08-30 14:01:10 -07006078 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006079
Hui Su0d0ee662019-07-29 14:38:36 -07006080 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006081}
6082
Yaowu Xuf883b422016-08-30 14:01:10 -07006083int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6084 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006085 if (!cm->show_frame) {
6086 return -1;
6087 } else {
6088 int ret;
David Turnerc29e1a92018-12-06 14:10:14 +00006089 if (cm->cur_frame != NULL) {
6090 *dest = cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006091 dest->y_width = cm->width;
6092 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006093 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
6094 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006095 ret = 0;
6096 } else {
6097 ret = -1;
6098 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006099 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006100 return ret;
6101 }
6102}
6103
Yaowu Xuf883b422016-08-30 14:01:10 -07006104int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
David Turnere7ebf902018-12-04 14:04:55 +00006105 if (cpi->last_show_frame_buf == NULL) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006106
David Turnere7ebf902018-12-04 14:04:55 +00006107 *frame = cpi->last_show_frame_buf->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006108 return 0;
6109}
6110
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006111static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
6112 const YV12_BUFFER_CONFIG *b) {
6113 return a->y_height == b->y_height && a->y_width == b->y_width &&
6114 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
6115 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
6116 a->border == b->border &&
6117 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
6118 (b->flags & YV12_FLAG_HIGHBITDEPTH);
6119}
6120
Yunqing Wang93b18f32018-06-08 21:08:29 -07006121aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
6122 YV12_BUFFER_CONFIG *new_frame,
6123 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006124 const int num_planes = av1_num_planes(cm);
6125 if (!equal_dimensions_and_border(new_frame, sd))
6126 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
6127 "Incorrect buffer dimensions");
6128 else
6129 aom_yv12_copy_frame(new_frame, sd, num_planes);
6130
6131 return cm->error.error_code;
6132}
6133
Yaowu Xuf883b422016-08-30 14:01:10 -07006134int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6135 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006136 int hr = 0, hs = 0, vr = 0, vs = 0;
6137
6138 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6139
6140 Scale2Ratio(horiz_mode, &hr, &hs);
6141 Scale2Ratio(vert_mode, &vr, &vs);
6142
6143 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07006144 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6145 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006146
6147 return 0;
6148}
6149
Yaowu Xuf883b422016-08-30 14:01:10 -07006150int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006151
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006152int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
6153 size_t output_size = 0;
6154 size_t total_bytes_read = 0;
6155 size_t remaining_size = *frame_size;
6156 uint8_t *buff_ptr = buffer;
6157
6158 // go through each OBUs
6159 while (total_bytes_read < *frame_size) {
6160 uint8_t saved_obu_header[2];
6161 uint64_t obu_payload_size;
6162 size_t length_of_payload_size;
6163 size_t length_of_obu_size;
6164 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
6165 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
6166
6167 // save the obu header (1 or 2 bytes)
6168 memmove(saved_obu_header, buff_ptr, obu_header_size);
6169 // clear the obu_has_size_field
6170 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
6171
6172 // get the payload_size and length of payload_size
6173 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
6174 &obu_payload_size, &length_of_payload_size) != 0) {
6175 return AOM_CODEC_ERROR;
6176 }
6177 obu_bytes_read += length_of_payload_size;
6178
6179 // calculate the length of size of the obu header plus payload
6180 length_of_obu_size =
6181 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
6182
6183 // move the rest of data to new location
6184 memmove(buff_ptr + length_of_obu_size + obu_header_size,
6185 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07006186 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006187
6188 // write the new obu size
6189 const uint64_t obu_size = obu_header_size + obu_payload_size;
6190 size_t coded_obu_size;
6191 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
6192 &coded_obu_size) != 0) {
6193 return AOM_CODEC_ERROR;
6194 }
6195
6196 // write the saved (modified) obu_header following obu size
6197 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
6198
6199 total_bytes_read += obu_bytes_read;
6200 remaining_size -= obu_bytes_read;
6201 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07006202 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006203 }
6204
6205 *frame_size = output_size;
6206 return AOM_CODEC_OK;
6207}
6208
Marco Paniconid8574e32019-08-04 21:30:12 -07006209static void svc_set_updates_external_ref_frame_config(AV1_COMP *cpi) {
6210 cpi->ext_refresh_frame_flags_pending = 1;
6211 cpi->ext_refresh_last_frame = cpi->svc.refresh[cpi->svc.ref_idx[0]];
6212 cpi->ext_refresh_golden_frame = cpi->svc.refresh[cpi->svc.ref_idx[3]];
6213 cpi->ext_refresh_bwd_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[4]];
6214 cpi->ext_refresh_alt2_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[5]];
6215 cpi->ext_refresh_alt_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[6]];
6216 cpi->svc.non_reference_frame = 1;
6217 for (int i = 0; i < REF_FRAMES; i++) {
6218 if (cpi->svc.refresh[i] == 1) {
6219 cpi->svc.non_reference_frame = 0;
6220 break;
6221 }
6222 }
6223}
6224
Yaowu Xuf883b422016-08-30 14:01:10 -07006225void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006226 // TODO(yunqingwang): For what references to use, external encoding flags
6227 // should be consistent with internal reference frame selection. Need to
6228 // ensure that there is not conflict between the two. In AV1 encoder, the
6229 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006230 // GOLDEN, BWDREF, ALTREF2.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08006231 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006232 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006233 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
6234 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
6235 AOM_EFLAG_NO_REF_ARF2)) {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006236 int ref = AOM_REFFRAME_ALL;
6237
6238 if (flags & AOM_EFLAG_NO_REF_LAST) ref ^= AOM_LAST_FLAG;
6239 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
6240 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
6241
6242 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
6243
6244 if (flags & AOM_EFLAG_NO_REF_ARF) {
6245 ref ^= AOM_ALT_FLAG;
6246 ref ^= AOM_BWD_FLAG;
6247 ref ^= AOM_ALT2_FLAG;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006248 } else {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006249 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
6250 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006251 }
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006252
6253 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006254 }
6255
6256 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006257 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006258 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006259
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006260 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
6261 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006262
Yaowu Xuf883b422016-08-30 14:01:10 -07006263 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006264
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006265 if (flags & AOM_EFLAG_NO_UPD_ARF) {
6266 upd ^= AOM_ALT_FLAG;
6267 upd ^= AOM_BWD_FLAG;
6268 upd ^= AOM_ALT2_FLAG;
6269 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006270
David Turnerce9b5902019-01-23 17:25:47 +00006271 cpi->ext_refresh_last_frame = (upd & AOM_LAST_FLAG) != 0;
6272 cpi->ext_refresh_golden_frame = (upd & AOM_GOLD_FLAG) != 0;
6273 cpi->ext_refresh_alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
6274 cpi->ext_refresh_bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
6275 cpi->ext_refresh_alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
6276 cpi->ext_refresh_frame_flags_pending = 1;
David Turner4f1f1812019-01-24 17:00:24 +00006277 } else {
Marco Paniconid8574e32019-08-04 21:30:12 -07006278 if (cpi->svc.external_ref_frame_config)
6279 svc_set_updates_external_ref_frame_config(cpi);
6280 else
6281 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006282 }
6283
sarahparker21dbca42018-03-30 17:43:44 -07006284 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
6285 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
sarahparker27d686a2018-03-30 17:43:44 -07006286 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
6287 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
sarahparker9806fed2018-03-30 17:43:44 -07006288 cpi->ext_use_s_frame =
6289 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Sarah Parker50b6d6e2018-04-11 19:21:54 -07006290 cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07006291
Yaowu Xuf883b422016-08-30 14:01:10 -07006292 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
6293 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006294 }
6295}
Andrey Norkin795ba872018-03-06 13:24:14 -08006296
Tom Fineganf8d6a162018-08-21 10:47:55 -07006297aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
6298 if (!cpi) return NULL;
6299
6300 uint8_t header_buf[512] = { 0 };
6301 const uint32_t sequence_header_size =
Yaowu Xu797674b2019-05-01 17:38:11 -07006302 av1_write_sequence_header_obu(cpi, &header_buf[0]);
Tom Fineganf8d6a162018-08-21 10:47:55 -07006303 assert(sequence_header_size <= sizeof(header_buf));
6304 if (sequence_header_size == 0) return NULL;
6305
6306 const size_t obu_header_size = 1;
6307 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
6308 const size_t payload_offset = obu_header_size + size_field_size;
6309
6310 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
6311 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
6312
Hui Su4fd11762019-03-26 16:05:07 -07006313 if (av1_write_obu_header(cpi, OBU_SEQUENCE_HEADER, 0, &header_buf[0]) !=
Tom Fineganf8d6a162018-08-21 10:47:55 -07006314 obu_header_size) {
6315 return NULL;
6316 }
6317
6318 size_t coded_size_field_size = 0;
6319 if (aom_uleb_encode(sequence_header_size, size_field_size,
6320 &header_buf[obu_header_size],
6321 &coded_size_field_size) != 0) {
6322 return NULL;
6323 }
6324 assert(coded_size_field_size == size_field_size);
6325
6326 aom_fixed_buf_t *global_headers =
6327 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
6328 if (!global_headers) return NULL;
6329
6330 const size_t global_header_buf_size =
6331 obu_header_size + size_field_size + sequence_header_size;
6332
6333 global_headers->buf = malloc(global_header_buf_size);
6334 if (!global_headers->buf) {
6335 free(global_headers);
6336 return NULL;
6337 }
6338
6339 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
6340 global_headers->sz = global_header_buf_size;
6341 return global_headers;
6342}