blob: 803107fbc3beb349d1ecef943547607f6393f7ae [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>
Urvang Joshibc82d382019-11-01 17:59:20 -070013#include <float.h>
Yaowu Xuc27fc142016-08-22 16:08:15 -070014#include <math.h>
15#include <stdio.h>
16
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"
chiyotsaic666b1f2019-12-20 10:44:58 -080065#include "av1/encoder/mv_prec.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
sdenge63f9fa2019-12-13 09:29:55 -080079#if CONFIG_TUNE_VMAF
80#include "av1/encoder/tune_vmaf.h"
81#endif
82
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000083#define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000084
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080085#if CONFIG_ENTROPY_STATS
86FRAME_COUNTS aggregate_fc;
87#endif // CONFIG_ENTROPY_STATS
88
Yaowu Xuc27fc142016-08-22 16:08:15 -070089#define AM_SEGMENT_ID_INACTIVE 7
90#define AM_SEGMENT_ID_ACTIVE 0
91
Yaowu Xuc27fc142016-08-22 16:08:15 -070092// #define OUTPUT_YUV_REC
Yaowu Xuc27fc142016-08-22 16:08:15 -070093#ifdef OUTPUT_YUV_SKINMAP
94FILE *yuv_skinmap_file = NULL;
95#endif
96#ifdef OUTPUT_YUV_REC
97FILE *yuv_rec_file;
98#define FILE_NAME_LEN 100
99#endif
100
Yunqing Wangdb70bf42019-08-19 09:28:11 -0700101const int default_tx_type_probs[FRAME_UPDATE_TYPES][TX_SIZES_ALL][TX_TYPES] = {
102 { { 221, 189, 214, 292, 0, 0, 0, 0, 0, 2, 38, 68, 0, 0, 0, 0 },
103 { 262, 203, 216, 239, 0, 0, 0, 0, 0, 1, 37, 66, 0, 0, 0, 0 },
104 { 315, 231, 239, 226, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0 },
105 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
106 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
107 { 222, 188, 214, 287, 0, 0, 0, 0, 0, 2, 50, 61, 0, 0, 0, 0 },
108 { 256, 182, 205, 282, 0, 0, 0, 0, 0, 2, 21, 76, 0, 0, 0, 0 },
109 { 281, 214, 217, 222, 0, 0, 0, 0, 0, 1, 48, 41, 0, 0, 0, 0 },
110 { 263, 194, 225, 225, 0, 0, 0, 0, 0, 2, 15, 100, 0, 0, 0, 0 },
111 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
112 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
113 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
114 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
115 { 170, 192, 242, 293, 0, 0, 0, 0, 0, 1, 68, 58, 0, 0, 0, 0 },
116 { 199, 210, 213, 291, 0, 0, 0, 0, 0, 1, 14, 96, 0, 0, 0, 0 },
117 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
118 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
119 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
120 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
121 { { 106, 69, 107, 278, 9, 15, 20, 45, 49, 23, 23, 88, 36, 74, 25, 57 },
122 { 105, 72, 81, 98, 45, 49, 47, 50, 56, 72, 30, 81, 33, 95, 27, 83 },
123 { 211, 105, 109, 120, 57, 62, 43, 49, 52, 58, 42, 116, 0, 0, 0, 0 },
124 { 1008, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0 },
125 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
126 { 131, 57, 98, 172, 19, 40, 37, 64, 69, 22, 41, 52, 51, 77, 35, 59 },
127 { 176, 83, 93, 202, 22, 24, 28, 47, 50, 16, 12, 93, 26, 76, 17, 59 },
128 { 136, 72, 89, 95, 46, 59, 47, 56, 61, 68, 35, 51, 32, 82, 26, 69 },
129 { 122, 80, 87, 105, 49, 47, 46, 46, 57, 52, 13, 90, 19, 103, 15, 93 },
130 { 1009, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0 },
131 { 1011, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0 },
132 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
133 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
134 { 202, 20, 84, 114, 14, 60, 41, 79, 99, 21, 41, 15, 50, 84, 34, 66 },
135 { 196, 44, 23, 72, 30, 22, 28, 57, 67, 13, 4, 165, 15, 148, 9, 131 },
136 { 882, 0, 0, 0, 0, 0, 0, 0, 0, 142, 0, 0, 0, 0, 0, 0 },
137 { 840, 0, 0, 0, 0, 0, 0, 0, 0, 184, 0, 0, 0, 0, 0, 0 },
138 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
139 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
140 { { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
141 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
142 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
143 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
144 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
145 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
146 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
147 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
148 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
149 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
150 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
151 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
152 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
153 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
154 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
155 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
156 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
157 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
158 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 } },
159 { { 213, 110, 141, 269, 12, 16, 15, 19, 21, 11, 38, 68, 22, 29, 16, 24 },
160 { 216, 119, 128, 143, 38, 41, 26, 30, 31, 30, 42, 70, 23, 36, 19, 32 },
161 { 367, 149, 154, 154, 38, 35, 17, 21, 21, 10, 22, 36, 0, 0, 0, 0 },
162 { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
163 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
164 { 219, 96, 127, 191, 21, 40, 25, 32, 34, 18, 45, 45, 33, 39, 26, 33 },
165 { 296, 99, 122, 198, 23, 21, 19, 24, 25, 13, 20, 64, 23, 32, 18, 27 },
166 { 275, 128, 142, 143, 35, 48, 23, 30, 29, 18, 42, 36, 18, 23, 14, 20 },
167 { 239, 132, 166, 175, 36, 27, 19, 21, 24, 14, 13, 85, 9, 31, 8, 25 },
168 { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
169 { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
170 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
171 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
172 { 309, 25, 79, 59, 25, 80, 34, 53, 61, 25, 49, 23, 43, 64, 36, 59 },
173 { 270, 57, 40, 54, 50, 42, 41, 53, 56, 28, 17, 81, 45, 86, 34, 70 },
174 { 1005, 0, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 0, 0, 0 },
175 { 992, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0 },
176 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
177 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
178 { { 133, 63, 55, 83, 57, 87, 58, 72, 68, 16, 24, 35, 29, 105, 25, 114 },
179 { 131, 75, 74, 60, 71, 77, 65, 66, 73, 33, 21, 79, 20, 83, 18, 78 },
180 { 276, 95, 82, 58, 86, 93, 63, 60, 64, 17, 38, 92, 0, 0, 0, 0 },
181 { 1006, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0 },
182 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
183 { 147, 49, 75, 78, 50, 97, 60, 67, 76, 17, 42, 35, 31, 93, 27, 80 },
184 { 157, 49, 58, 75, 61, 52, 56, 67, 69, 12, 15, 79, 24, 119, 11, 120 },
185 { 178, 69, 83, 77, 69, 85, 72, 77, 77, 20, 35, 40, 25, 48, 23, 46 },
186 { 174, 55, 64, 57, 73, 68, 62, 61, 75, 15, 12, 90, 17, 99, 16, 86 },
187 { 1008, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0 },
188 { 1018, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0 },
189 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
190 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
191 { 266, 31, 63, 64, 21, 52, 39, 54, 63, 30, 52, 31, 48, 89, 46, 75 },
192 { 272, 26, 32, 44, 29, 31, 32, 53, 51, 13, 13, 88, 22, 153, 16, 149 },
193 { 923, 0, 0, 0, 0, 0, 0, 0, 0, 101, 0, 0, 0, 0, 0, 0 },
194 { 969, 0, 0, 0, 0, 0, 0, 0, 0, 55, 0, 0, 0, 0, 0, 0 },
195 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
196 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
197 { { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
198 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
199 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
200 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
201 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
202 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
203 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
204 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
205 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
206 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
207 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
208 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
209 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
210 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
211 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
212 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
213 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
214 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
215 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 } },
216 { { 158, 92, 125, 298, 12, 15, 20, 29, 31, 12, 29, 67, 34, 44, 23, 35 },
217 { 147, 94, 103, 123, 45, 48, 38, 41, 46, 48, 37, 78, 33, 63, 27, 53 },
218 { 268, 126, 125, 136, 54, 53, 31, 38, 38, 33, 35, 87, 0, 0, 0, 0 },
219 { 1018, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0 },
220 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
221 { 159, 72, 103, 194, 20, 35, 37, 50, 56, 21, 39, 40, 51, 61, 38, 48 },
222 { 259, 86, 95, 188, 32, 20, 25, 34, 37, 13, 12, 85, 25, 53, 17, 43 },
223 { 189, 99, 113, 123, 45, 59, 37, 46, 48, 44, 39, 41, 31, 47, 26, 37 },
224 { 175, 110, 113, 128, 58, 38, 33, 33, 43, 29, 13, 100, 14, 68, 12, 57 },
225 { 1017, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0 },
226 { 1019, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0 },
227 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
228 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
229 { 208, 22, 84, 101, 21, 59, 44, 70, 90, 25, 59, 13, 64, 67, 49, 48 },
230 { 277, 52, 32, 63, 43, 26, 33, 48, 54, 11, 6, 130, 18, 119, 11, 101 },
231 { 963, 0, 0, 0, 0, 0, 0, 0, 0, 61, 0, 0, 0, 0, 0, 0 },
232 { 979, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 0, 0, 0 },
233 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
234 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
235};
236
Yunqing Wang5f74dc22019-10-29 10:35:20 -0700237const int default_obmc_probs[FRAME_UPDATE_TYPES][BLOCK_SIZES_ALL] = {
238 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
239 { 0, 0, 0, 106, 90, 90, 97, 67, 59, 70, 28,
240 30, 38, 16, 16, 16, 0, 0, 44, 50, 26, 25 },
241 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
242 { 0, 0, 0, 98, 93, 97, 68, 82, 85, 33, 30,
243 33, 16, 16, 16, 16, 0, 0, 43, 37, 26, 16 },
244 { 0, 0, 0, 91, 80, 76, 78, 55, 49, 24, 16,
245 16, 16, 16, 16, 16, 0, 0, 29, 45, 16, 38 },
246 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
247 { 0, 0, 0, 103, 89, 89, 89, 62, 63, 76, 34,
248 35, 32, 19, 16, 16, 0, 0, 49, 55, 29, 19 }
249};
250
Yunqing Wanga09a46b2019-12-09 13:16:24 -0800251const int default_warped_probs[FRAME_UPDATE_TYPES] = { 64, 64, 64, 64,
252 64, 64, 64 };
253
Yunqing Wang2348c032020-01-09 18:05:49 -0800254// TODO(yunqing): the default probs can be trained later from better
255// performance.
256const int default_switchable_interp_probs[FRAME_UPDATE_TYPES]
257 [SWITCHABLE_FILTER_CONTEXTS]
258 [SWITCHABLE_FILTERS] = {
259 { { 512, 512, 512 },
260 { 512, 512, 512 },
261 { 512, 512, 512 },
262 { 512, 512, 512 },
263 { 512, 512, 512 },
264 { 512, 512, 512 },
265 { 512, 512, 512 },
266 { 512, 512, 512 },
267 { 512, 512, 512 },
268 { 512, 512, 512 },
269 { 512, 512, 512 },
270 { 512, 512, 512 },
271 { 512, 512, 512 },
272 { 512, 512, 512 },
273 { 512, 512, 512 },
274 { 512, 512, 512 } },
275 { { 512, 512, 512 },
276 { 512, 512, 512 },
277 { 512, 512, 512 },
278 { 512, 512, 512 },
279 { 512, 512, 512 },
280 { 512, 512, 512 },
281 { 512, 512, 512 },
282 { 512, 512, 512 },
283 { 512, 512, 512 },
284 { 512, 512, 512 },
285 { 512, 512, 512 },
286 { 512, 512, 512 },
287 { 512, 512, 512 },
288 { 512, 512, 512 },
289 { 512, 512, 512 },
290 { 512, 512, 512 } },
291 { { 512, 512, 512 },
292 { 512, 512, 512 },
293 { 512, 512, 512 },
294 { 512, 512, 512 },
295 { 512, 512, 512 },
296 { 512, 512, 512 },
297 { 512, 512, 512 },
298 { 512, 512, 512 },
299 { 512, 512, 512 },
300 { 512, 512, 512 },
301 { 512, 512, 512 },
302 { 512, 512, 512 },
303 { 512, 512, 512 },
304 { 512, 512, 512 },
305 { 512, 512, 512 },
306 { 512, 512, 512 } },
307 { { 512, 512, 512 },
308 { 512, 512, 512 },
309 { 512, 512, 512 },
310 { 512, 512, 512 },
311 { 512, 512, 512 },
312 { 512, 512, 512 },
313 { 512, 512, 512 },
314 { 512, 512, 512 },
315 { 512, 512, 512 },
316 { 512, 512, 512 },
317 { 512, 512, 512 },
318 { 512, 512, 512 },
319 { 512, 512, 512 },
320 { 512, 512, 512 },
321 { 512, 512, 512 },
322 { 512, 512, 512 } },
323 { { 512, 512, 512 },
324 { 512, 512, 512 },
325 { 512, 512, 512 },
326 { 512, 512, 512 },
327 { 512, 512, 512 },
328 { 512, 512, 512 },
329 { 512, 512, 512 },
330 { 512, 512, 512 },
331 { 512, 512, 512 },
332 { 512, 512, 512 },
333 { 512, 512, 512 },
334 { 512, 512, 512 },
335 { 512, 512, 512 },
336 { 512, 512, 512 },
337 { 512, 512, 512 },
338 { 512, 512, 512 } },
339 { { 512, 512, 512 },
340 { 512, 512, 512 },
341 { 512, 512, 512 },
342 { 512, 512, 512 },
343 { 512, 512, 512 },
344 { 512, 512, 512 },
345 { 512, 512, 512 },
346 { 512, 512, 512 },
347 { 512, 512, 512 },
348 { 512, 512, 512 },
349 { 512, 512, 512 },
350 { 512, 512, 512 },
351 { 512, 512, 512 },
352 { 512, 512, 512 },
353 { 512, 512, 512 },
354 { 512, 512, 512 } },
355 { { 512, 512, 512 },
356 { 512, 512, 512 },
357 { 512, 512, 512 },
358 { 512, 512, 512 },
359 { 512, 512, 512 },
360 { 512, 512, 512 },
361 { 512, 512, 512 },
362 { 512, 512, 512 },
363 { 512, 512, 512 },
364 { 512, 512, 512 },
365 { 512, 512, 512 },
366 { 512, 512, 512 },
367 { 512, 512, 512 },
368 { 512, 512, 512 },
369 { 512, 512, 512 },
370 { 512, 512, 512 } }
371 };
372
Yaowu Xuf883b422016-08-30 14:01:10 -0700373static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700374 switch (mode) {
375 case NORMAL:
376 *hr = 1;
377 *hs = 1;
378 break;
379 case FOURFIVE:
380 *hr = 4;
381 *hs = 5;
382 break;
383 case THREEFIVE:
384 *hr = 3;
385 *hs = 5;
386 break;
387 case ONETWO:
388 *hr = 1;
389 *hs = 2;
390 break;
391 default:
392 *hr = 1;
393 *hs = 1;
394 assert(0);
395 break;
396 }
397}
398
399// Mark all inactive blocks as active. Other segmentation features may be set
400// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700401static void suppress_active_map(AV1_COMP *cpi) {
Vishesh8c90fff2020-04-13 11:32:56 +0530402 unsigned char *const seg_map = cpi->enc_seg.map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700403 int i;
404 if (cpi->active_map.enabled || cpi->active_map.update)
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700405 for (i = 0;
406 i < cpi->common.mi_params.mi_rows * cpi->common.mi_params.mi_cols; ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700407 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
408 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
409}
410
Yaowu Xuf883b422016-08-30 14:01:10 -0700411static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700412 struct segmentation *const seg = &cpi->common.seg;
Vishesh8c90fff2020-04-13 11:32:56 +0530413 unsigned char *const seg_map = cpi->enc_seg.map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700414 const unsigned char *const active_map = cpi->active_map.map;
415 int i;
416
417 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
418
419 if (frame_is_intra_only(&cpi->common)) {
420 cpi->active_map.enabled = 0;
421 cpi->active_map.update = 1;
422 }
423
424 if (cpi->active_map.update) {
425 if (cpi->active_map.enabled) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700426 for (i = 0;
427 i < cpi->common.mi_params.mi_rows * cpi->common.mi_params.mi_cols;
428 ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700429 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700430 av1_enable_segmentation(seg);
431 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700432 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
433 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
434 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
435 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
436
437 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
438 -MAX_LOOP_FILTER);
439 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
440 -MAX_LOOP_FILTER);
441 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
442 -MAX_LOOP_FILTER);
443 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
444 -MAX_LOOP_FILTER);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700445 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700446 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700447 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
448 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
449 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
450 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700451 if (seg->enabled) {
452 seg->update_data = 1;
453 seg->update_map = 1;
454 }
455 }
456 cpi->active_map.update = 0;
457 }
458}
459
Yaowu Xuf883b422016-08-30 14:01:10 -0700460int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
461 int cols) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700462 const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
463 if (rows == mi_params->mb_rows && cols == mi_params->mb_cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700464 unsigned char *const active_map_8x8 = cpi->active_map.map;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700465 const int mi_rows = mi_params->mi_rows;
466 const int mi_cols = mi_params->mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700467 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
468 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700469 cpi->active_map.update = 1;
470 if (new_map_16x16) {
471 int r, c;
472 for (r = 0; r < mi_rows; ++r) {
473 for (c = 0; c < mi_cols; ++c) {
474 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700475 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700476 ? AM_SEGMENT_ID_ACTIVE
477 : AM_SEGMENT_ID_INACTIVE;
478 }
479 }
480 cpi->active_map.enabled = 1;
481 } else {
482 cpi->active_map.enabled = 0;
483 }
484 return 0;
485 } else {
486 return -1;
487 }
488}
489
Yaowu Xuf883b422016-08-30 14:01:10 -0700490int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
491 int cols) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700492 const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
493 if (rows == mi_params->mb_rows && cols == mi_params->mb_cols &&
Yaowu Xuc27fc142016-08-22 16:08:15 -0700494 new_map_16x16) {
Vishesh8c90fff2020-04-13 11:32:56 +0530495 unsigned char *const seg_map_8x8 = cpi->enc_seg.map;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700496 const int mi_rows = mi_params->mi_rows;
497 const int mi_cols = mi_params->mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700498 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
499 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
500
Yaowu Xuc27fc142016-08-22 16:08:15 -0700501 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
502 if (cpi->active_map.enabled) {
503 int r, c;
504 for (r = 0; r < mi_rows; ++r) {
505 for (c = 0; c < mi_cols; ++c) {
506 // Cyclic refresh segments are considered active despite not having
507 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700508 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700509 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
510 }
511 }
512 }
513 return 0;
514 } else {
515 return -1;
516 }
517}
518
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800519// Compute the horizontal frequency components' energy in a frame
520// by calculuating the 16x4 Horizontal DCT. This is to be used to
521// decide the superresolution parameters.
Yaowu Xubedbf4f2019-05-01 17:54:36 -0700522static void analyze_hor_freq(const AV1_COMP *cpi, double *energy) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800523 uint64_t freq_energy[16] = { 0 };
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800524 const YV12_BUFFER_CONFIG *buf = cpi->source;
525 const int bd = cpi->td.mb.e_mbd.bd;
526 const int width = buf->y_crop_width;
527 const int height = buf->y_crop_height;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800528 DECLARE_ALIGNED(16, int32_t, coeff[16 * 4]);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800529 int n = 0;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800530 memset(freq_energy, 0, sizeof(freq_energy));
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800531 if (buf->flags & YV12_FLAG_HIGHBITDEPTH) {
532 const int16_t *src16 = (const int16_t *)CONVERT_TO_SHORTPTR(buf->y_buffer);
533 for (int i = 0; i < height - 4; i += 4) {
534 for (int j = 0; j < width - 16; j += 16) {
535 av1_fwd_txfm2d_16x4(src16 + i * buf->y_stride + j, coeff, buf->y_stride,
536 H_DCT, bd);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800537 for (int k = 1; k < 16; ++k) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800538 const uint64_t this_energy =
539 ((int64_t)coeff[k] * coeff[k]) +
540 ((int64_t)coeff[k + 16] * coeff[k + 16]) +
541 ((int64_t)coeff[k + 32] * coeff[k + 32]) +
542 ((int64_t)coeff[k + 48] * coeff[k + 48]);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800543 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2 + 2 * (bd - 8));
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800544 }
545 n++;
546 }
547 }
548 } else {
Debargha Mukherjeeac28c722018-11-14 22:09:46 -0800549 assert(bd == 8);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800550 DECLARE_ALIGNED(16, int16_t, src16[16 * 4]);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800551 for (int i = 0; i < height - 4; i += 4) {
552 for (int j = 0; j < width - 16; j += 16) {
553 for (int ii = 0; ii < 4; ++ii)
554 for (int jj = 0; jj < 16; ++jj)
555 src16[ii * 16 + jj] =
556 buf->y_buffer[(i + ii) * buf->y_stride + (j + jj)];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800557 av1_fwd_txfm2d_16x4(src16, coeff, 16, H_DCT, bd);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800558 for (int k = 1; k < 16; ++k) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800559 const uint64_t this_energy =
560 ((int64_t)coeff[k] * coeff[k]) +
561 ((int64_t)coeff[k + 16] * coeff[k + 16]) +
562 ((int64_t)coeff[k + 32] * coeff[k + 32]) +
563 ((int64_t)coeff[k + 48] * coeff[k + 48]);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800564 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800565 }
566 n++;
567 }
568 }
569 }
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800570 if (n) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800571 for (int k = 1; k < 16; ++k) energy[k] = (double)freq_energy[k] / n;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800572 // Convert to cumulative energy
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800573 for (int k = 14; k > 0; --k) energy[k] += energy[k + 1];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800574 } else {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800575 for (int k = 1; k < 16; ++k) energy[k] = 1e+20;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800576 }
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800577}
578
Yaowu Xuf883b422016-08-30 14:01:10 -0700579static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Urvang Joshie4530f82018-01-09 11:43:37 -0800580 const AV1_COMMON *const cm = &cpi->common;
James Zernbf3ca362019-10-21 11:33:44 -0700581
Yaowu Xuf883b422016-08-30 14:01:10 -0700582 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700583 return BLOCK_64X64;
Ryan Leic8b6dd62019-10-23 20:01:26 -0700584 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
585 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700586
Yaowu Xuf883b422016-08-30 14:01:10 -0700587 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700588
Marco Paniconi8c449df2020-04-02 23:41:46 -0700589 if (cpi->svc.number_spatial_layers > 1) {
590 // Use the configured size (top resolution) for spatial layers.
591 return AOMMIN(cpi->oxcf.width, cpi->oxcf.height) > 480 ? BLOCK_128X128
592 : BLOCK_64X64;
593 }
594
Ryan Leic8b6dd62019-10-23 20:01:26 -0700595 // TODO(any): Possibly could improve this with a heuristic.
Urvang Joshiaab74432018-06-01 12:06:22 -0700596 // When superres / resize is on, 'cm->width / height' can change between
Hui Su3e3b9342019-04-12 18:27:28 +0000597 // calls, so we don't apply this heuristic there.
598 // Things break if superblock size changes between the first pass and second
599 // pass encoding, which is why this heuristic is not configured as a
600 // speed-feature.
Urvang Joshi9d2606c2020-04-14 15:58:01 -0700601 if (cpi->oxcf.superres_mode == AOM_SUPERRES_NONE &&
Hui Su3e3b9342019-04-12 18:27:28 +0000602 cpi->oxcf.resize_mode == RESIZE_NONE && cpi->oxcf.speed >= 1) {
603 return AOMMIN(cm->width, cm->height) > 480 ? BLOCK_128X128 : BLOCK_64X64;
Urvang Joshie4530f82018-01-09 11:43:37 -0800604 }
605
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700607}
608
Yaowu Xuf883b422016-08-30 14:01:10 -0700609static void setup_frame(AV1_COMP *cpi) {
610 AV1_COMMON *const cm = &cpi->common;
Johannb0ef6ff2018-02-08 14:32:21 -0800611 // Set up entropy context depending on frame type. The decoder mandates
612 // the use of the default context, index 0, for keyframes and inter
613 // frames where the error_resilient_mode or intra_only flag is set. For
614 // other inter-frames the encoder currently uses only two contexts;
615 // context 1 for ALTREF frames and context 0 for the others.
Soo-Chul Han85e8c792018-01-21 01:58:15 -0500616
Urvang Joshib6409e92020-03-23 11:23:27 -0700617 if (frame_is_intra_only(cm) || cm->features.error_resilient_mode ||
Vishesha195ca32020-04-07 18:46:20 +0530618 cpi->ext_flags.use_primary_ref_none) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700619 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700620 }
621
Hui Sueb4b7de2019-04-03 11:00:18 -0700622 if ((cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) ||
623 frame_is_sframe(cm)) {
624 if (!cpi->seq_params_locked) {
625 set_sb_size(&cm->seq_params, select_sb_size(cpi));
626 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700627 } else {
David Turnera21966b2018-12-05 14:48:49 +0000628 const RefCntBuffer *const primary_ref_buf = get_primary_ref_frame_buf(cm);
629 if (primary_ref_buf == NULL) {
David Barkercc615a82018-03-19 14:38:51 +0000630 av1_setup_past_independence(cm);
631 cm->seg.update_map = 1;
632 cm->seg.update_data = 1;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700633 } else {
David Turnera21966b2018-12-05 14:48:49 +0000634 *cm->fc = primary_ref_buf->frame_context;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700635 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700636 }
637
David Turnerbc0993e2019-02-15 14:42:23 +0000638 av1_zero(cm->cur_frame->interp_filter_selected);
David Turnera21966b2018-12-05 14:48:49 +0000639 cm->prev_frame = get_primary_ref_frame_buf(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700640 cpi->vaq_refresh = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700641}
642
Mufaddal Chakeraed7b1622020-04-06 12:30:23 +0530643static void set_mb_mi(CommonModeInfoParams *mi_params, int width, int height) {
chiyotsaia7091f12019-08-09 16:48:27 -0700644 // Ensure that the decoded width and height are both multiples of
645 // 8 luma pixels (note: this may only be a multiple of 4 chroma pixels if
646 // subsampling is used).
647 // This simplifies the implementation of various experiments,
648 // eg. cdef, which operates on units of 8x8 luma pixels.
649 const int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
650 const int aligned_height = ALIGN_POWER_OF_TWO(height, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700651
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700652 mi_params->mi_cols = aligned_width >> MI_SIZE_LOG2;
653 mi_params->mi_rows = aligned_height >> MI_SIZE_LOG2;
654 mi_params->mi_stride = calc_mi_size(mi_params->mi_cols);
chiyotsaia7091f12019-08-09 16:48:27 -0700655
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700656 mi_params->mb_cols = (mi_params->mi_cols + 2) >> 2;
657 mi_params->mb_rows = (mi_params->mi_rows + 2) >> 2;
658 mi_params->MBs = mi_params->mb_rows * mi_params->mb_cols;
chiyotsaia7091f12019-08-09 16:48:27 -0700659
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700660 const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700661 mi_params->mi_alloc_stride =
662 (mi_params->mi_stride + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
chiyotsaia7091f12019-08-09 16:48:27 -0700663
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700664 assert(mi_size_wide[mi_params->mi_alloc_bsize] ==
665 mi_size_high[mi_params->mi_alloc_bsize]);
chiyotsaia7091f12019-08-09 16:48:27 -0700666
667#if CONFIG_LPF_MASK
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700668 av1_alloc_loop_filter_mask(mi_params);
chiyotsaia7091f12019-08-09 16:48:27 -0700669#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700670}
671
Mufaddal Chakeraed7b1622020-04-06 12:30:23 +0530672static void enc_set_mb_mi(CommonModeInfoParams *mi_params, int width,
673 int height) {
674 const int is_4k_or_larger = AOMMIN(width, height) >= 2160;
675 mi_params->mi_alloc_bsize = is_4k_or_larger ? BLOCK_8X8 : BLOCK_4X4;
676
677 set_mb_mi(mi_params, width, height);
678}
679
680static void stat_stage_set_mb_mi(CommonModeInfoParams *mi_params, int width,
681 int height) {
682 mi_params->mi_alloc_bsize = BLOCK_16X16;
683
684 set_mb_mi(mi_params, width, height);
685}
686
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700687static void enc_setup_mi(CommonModeInfoParams *mi_params) {
688 const int mi_grid_size =
689 mi_params->mi_stride * calc_mi_size(mi_params->mi_rows);
Urvang Joshid2998cd2020-03-26 02:27:48 -0700690 memset(mi_params->mi_alloc, 0,
691 mi_params->mi_alloc_size * sizeof(*mi_params->mi_alloc));
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700692 memset(mi_params->mi_grid_base, 0,
693 mi_grid_size * sizeof(*mi_params->mi_grid_base));
694 memset(mi_params->tx_type_map, 0,
695 mi_grid_size * sizeof(*mi_params->tx_type_map));
chiyotsaia7091f12019-08-09 16:48:27 -0700696}
697
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700698static void enc_free_mi(CommonModeInfoParams *mi_params) {
Urvang Joshid2998cd2020-03-26 02:27:48 -0700699 aom_free(mi_params->mi_alloc);
700 mi_params->mi_alloc = NULL;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700701 aom_free(mi_params->mi_grid_base);
702 mi_params->mi_grid_base = NULL;
703 mi_params->mi_alloc_size = 0;
704 aom_free(mi_params->tx_type_map);
705 mi_params->tx_type_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700706}
707
Yaowu Xuf883b422016-08-30 14:01:10 -0700708void av1_initialize_enc(void) {
Wan-Teh Chang3cac4542018-06-29 10:21:39 -0700709 av1_rtcd();
710 aom_dsp_rtcd();
711 aom_scale_rtcd();
712 av1_init_intra_predictors();
713 av1_init_me_luts();
714 av1_rc_init_minq_luts();
715 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700716}
717
Visheshd1317912020-04-07 14:39:44 +0530718static void dealloc_context_buffers_ext(MBMIExtFrameBufferInfo *mbmi_ext_info) {
719 if (mbmi_ext_info->frame_base) {
720 aom_free(mbmi_ext_info->frame_base);
721 mbmi_ext_info->frame_base = NULL;
722 mbmi_ext_info->alloc_size = 0;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700723 }
724}
725
Visheshd1317912020-04-07 14:39:44 +0530726static void alloc_context_buffers_ext(AV1_COMMON *cm,
727 MBMIExtFrameBufferInfo *mbmi_ext_info) {
Urvang Joshif10ee372020-03-26 02:22:43 -0700728 const CommonModeInfoParams *const mi_params = &cm->mi_params;
chiyotsai426c0662019-08-05 16:15:11 -0700729
Urvang Joshif10ee372020-03-26 02:22:43 -0700730 const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
731 const int mi_alloc_rows =
732 (mi_params->mi_rows + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
733 const int mi_alloc_cols =
734 (mi_params->mi_cols + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
735 const int new_ext_mi_size = mi_alloc_rows * mi_alloc_cols;
736
Visheshd1317912020-04-07 14:39:44 +0530737 if (new_ext_mi_size > mbmi_ext_info->alloc_size) {
738 dealloc_context_buffers_ext(mbmi_ext_info);
Remya0cce44c2019-08-16 11:57:24 +0530739 CHECK_MEM_ERROR(
Visheshd1317912020-04-07 14:39:44 +0530740 cm, mbmi_ext_info->frame_base,
741 aom_calloc(new_ext_mi_size, sizeof(*mbmi_ext_info->frame_base)));
742 mbmi_ext_info->alloc_size = new_ext_mi_size;
chiyotsaia7091f12019-08-09 16:48:27 -0700743 }
Urvang Joshif10ee372020-03-26 02:22:43 -0700744 // The stride needs to be updated regardless of whether new allocation
745 // happened or not.
Visheshd1317912020-04-07 14:39:44 +0530746 mbmi_ext_info->stride = mi_alloc_cols;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700747}
748
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800749static void reset_film_grain_chroma_params(aom_film_grain_t *pars) {
750 pars->num_cr_points = 0;
751 pars->cr_mult = 0;
752 pars->cr_luma_mult = 0;
753 memset(pars->scaling_points_cr, 0, sizeof(pars->scaling_points_cr));
754 memset(pars->ar_coeffs_cr, 0, sizeof(pars->ar_coeffs_cr));
755 pars->num_cb_points = 0;
756 pars->cb_mult = 0;
757 pars->cb_luma_mult = 0;
Yaowu Xufda7dcb2019-01-16 13:04:33 -0800758 pars->chroma_scaling_from_luma = 0;
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800759 memset(pars->scaling_points_cb, 0, sizeof(pars->scaling_points_cb));
760 memset(pars->ar_coeffs_cb, 0, sizeof(pars->ar_coeffs_cb));
761}
762
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800763static void update_film_grain_parameters(struct AV1_COMP *cpi,
764 const AV1EncoderConfig *oxcf) {
765 AV1_COMMON *const cm = &cpi->common;
766 cpi->oxcf = *oxcf;
767
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700768 if (cpi->film_grain_table) {
769 aom_film_grain_table_free(cpi->film_grain_table);
770 aom_free(cpi->film_grain_table);
771 cpi->film_grain_table = NULL;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700772 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700773
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800774 if (oxcf->film_grain_test_vector) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700775 cm->seq_params.film_grain_params_present = 1;
David Turnerd2a592e2018-11-16 14:59:31 +0000776 if (cm->current_frame.frame_type == KEY_FRAME) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800777 memcpy(&cm->film_grain_params,
778 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
779 sizeof(cm->film_grain_params));
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800780 if (oxcf->monochrome)
781 reset_film_grain_chroma_params(&cm->film_grain_params);
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700782 cm->film_grain_params.bit_depth = cm->seq_params.bit_depth;
783 if (cm->seq_params.color_range == AOM_CR_FULL_RANGE) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800784 cm->film_grain_params.clip_to_restricted_range = 0;
785 }
786 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700787 } else if (oxcf->film_grain_table_filename) {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800788 cm->seq_params.film_grain_params_present = 1;
789
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700790 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
791 memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700792
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700793 aom_film_grain_table_read(cpi->film_grain_table,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700794 oxcf->film_grain_table_filename, &cm->error);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800795 } else {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800796#if CONFIG_DENOISE
797 cm->seq_params.film_grain_params_present = (cpi->oxcf.noise_level > 0);
798#else
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700799 cm->seq_params.film_grain_params_present = 0;
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800800#endif
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800801 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
802 }
803}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800804
Yaowu Xuf883b422016-08-30 14:01:10 -0700805static void dealloc_compressor_data(AV1_COMP *cpi) {
806 AV1_COMMON *const cm = &cpi->common;
Vishesh686aa772020-04-13 14:40:12 +0530807 TokenInfo *token_info = &cpi->token_info;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700808
Visheshd1317912020-04-07 14:39:44 +0530809 dealloc_context_buffers_ext(&cpi->mbmi_ext_info);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700810
Yaowu Xuf883b422016-08-30 14:01:10 -0700811 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700812 cpi->tile_data = NULL;
813
814 // Delete sementation map
Vishesh8c90fff2020-04-13 11:32:56 +0530815 aom_free(cpi->enc_seg.map);
816 cpi->enc_seg.map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700817
Yaowu Xuf883b422016-08-30 14:01:10 -0700818 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700819 cpi->cyclic_refresh = NULL;
820
Yaowu Xuf883b422016-08-30 14:01:10 -0700821 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700822 cpi->active_map.map = NULL;
823
sdengc23c7f12019-06-11 16:56:50 -0700824 aom_free(cpi->ssim_rdmult_scaling_factors);
825 cpi->ssim_rdmult_scaling_factors = NULL;
826
sdengf46a1062019-08-04 18:43:50 -0700827 aom_free(cpi->tpl_rdmult_scaling_factors);
828 cpi->tpl_rdmult_scaling_factors = NULL;
829
830 aom_free(cpi->tpl_sb_rdmult_scaling_factors);
831 cpi->tpl_sb_rdmult_scaling_factors = NULL;
832
sdeng01959162019-12-20 10:46:24 -0800833#if CONFIG_TUNE_VMAF
Visheshf18766f2020-04-17 10:22:48 +0530834 aom_free(cpi->vmaf_info.rdmult_scaling_factors);
835 cpi->vmaf_info.rdmult_scaling_factors = NULL;
sdeng01959162019-12-20 10:46:24 -0800836#endif
837
Jingning Hand064cf02017-06-01 10:00:39 -0700838 aom_free(cpi->td.mb.above_pred_buf);
839 cpi->td.mb.above_pred_buf = NULL;
840
841 aom_free(cpi->td.mb.left_pred_buf);
842 cpi->td.mb.left_pred_buf = NULL;
843
844 aom_free(cpi->td.mb.wsrc_buf);
845 cpi->td.mb.wsrc_buf = NULL;
846
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530847 aom_free(cpi->td.mb.inter_modes_info);
848 cpi->td.mb.inter_modes_info = NULL;
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530849
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530850 for (int i = 0; i < 2; i++)
851 for (int j = 0; j < 2; j++) {
chiyotsai82f36c92020-04-09 16:18:02 -0700852 aom_free(cpi->td.mb.intrabc_hash_info.hash_value_buffer[i][j]);
853 cpi->td.mb.intrabc_hash_info.hash_value_buffer[i][j] = NULL;
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530854 }
Jingning Hand064cf02017-06-01 10:00:39 -0700855 aom_free(cpi->td.mb.mask_buf);
856 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700857
Jingning Han6cc1fd32017-10-13 09:05:36 -0700858 aom_free(cm->tpl_mvs);
859 cm->tpl_mvs = NULL;
Jingning Han6cc1fd32017-10-13 09:05:36 -0700860
Remya0cce44c2019-08-16 11:57:24 +0530861 aom_free(cpi->td.mb.mbmi_ext);
862 cpi->td.mb.mbmi_ext = NULL;
863
Fyodor Kyslov166648a2020-03-18 16:15:27 -0700864 if (cpi->td.vt64x64) {
865 aom_free(cpi->td.vt64x64);
866 cpi->td.vt64x64 = NULL;
867 }
868
Yaowu Xuf883b422016-08-30 14:01:10 -0700869 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700870 av1_free_txb_buf(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700871 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700872
Yaowu Xuf883b422016-08-30 14:01:10 -0700873 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuf883b422016-08-30 14:01:10 -0700874 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800875 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuf883b422016-08-30 14:01:10 -0700876 aom_free_frame_buffer(&cpi->scaled_source);
877 aom_free_frame_buffer(&cpi->scaled_last_source);
878 aom_free_frame_buffer(&cpi->alt_ref_buffer);
879 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700880
Vishesh686aa772020-04-13 14:40:12 +0530881 free_token_info(token_info);
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530882
Yue Cheneb628982019-08-29 15:17:13 -0700883 av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf);
884 av1_free_sms_tree(&cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700885
hui sud9a812b2017-07-06 14:34:37 -0700886 aom_free(cpi->td.mb.palette_buffer);
Hui Su38711e72019-06-11 10:49:47 -0700887 av1_release_compound_type_rd_buffers(&cpi->td.mb.comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700888 aom_free(cpi->td.mb.tmp_conv_dst);
889 for (int j = 0; j < 2; ++j) {
890 aom_free(cpi->td.mb.tmp_obmc_bufs[j]);
891 }
892
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700893#if CONFIG_DENOISE
894 if (cpi->denoise_and_model) {
895 aom_denoise_and_model_free(cpi->denoise_and_model);
896 cpi->denoise_and_model = NULL;
897 }
898#endif
899 if (cpi->film_grain_table) {
900 aom_film_grain_table_free(cpi->film_grain_table);
901 cpi->film_grain_table = NULL;
902 }
Hui Suc3a8d372019-05-28 15:52:45 -0700903
904 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Vishesh8ac928b2020-04-01 02:36:35 +0530905 aom_free(cpi->level_params.level_info[i]);
Hui Suc3a8d372019-05-28 15:52:45 -0700906 }
Marco Paniconi63971322019-08-15 21:32:05 -0700907
908 if (cpi->use_svc) av1_free_svc_cyclic_refresh(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700909}
910
Yaowu Xuf883b422016-08-30 14:01:10 -0700911static void configure_static_seg_features(AV1_COMP *cpi) {
912 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700913 const RATE_CONTROL *const rc = &cpi->rc;
914 struct segmentation *const seg = &cm->seg;
915
916 int high_q = (int)(rc->avg_q > 48.0);
917 int qi_delta;
918
919 // Disable and clear down for KF
David Turnerd2a592e2018-11-16 14:59:31 +0000920 if (cm->current_frame.frame_type == KEY_FRAME) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700921 // Clear down the global segmentation map
Vishesh8c90fff2020-04-13 11:32:56 +0530922 memset(cpi->enc_seg.map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700923 seg->update_map = 0;
924 seg->update_data = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700925
926 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700927 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700928
929 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700930 av1_clearall_segfeatures(seg);
Jayasanker J24cb9bc2020-04-15 13:43:10 +0530931 } else if (cpi->refresh_frame.alt_ref_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700932 // If this is an alt ref frame
933 // Clear down the global segmentation map
Vishesh8c90fff2020-04-13 11:32:56 +0530934 memset(cpi->enc_seg.map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700935 seg->update_map = 0;
936 seg->update_data = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700937
938 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700939 av1_disable_segmentation(seg);
940 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700941
Yaowu Xuc27fc142016-08-22 16:08:15 -0700942 // If segmentation was enabled set those features needed for the
943 // arf itself.
944 if (seg->enabled) {
945 seg->update_map = 1;
946 seg->update_data = 1;
947
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700948 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
949 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700950 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700951 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
952 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
953 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
954 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
955
956 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
957 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
958 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
959 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700960
Yaowu Xuf883b422016-08-30 14:01:10 -0700961 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700962 }
963 } else if (seg->enabled) {
964 // All other frames if segmentation has been enabled
965
966 // First normal frame in a valid gf or alt ref group
967 if (rc->frames_since_golden == 0) {
968 // Set up segment features for normal frames in an arf group
969 if (rc->source_alt_ref_active) {
970 seg->update_map = 0;
971 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700972
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700973 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
974 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700975 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
976 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700977
Cheng Chend8184da2017-09-26 18:15:22 -0700978 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
979 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
980 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
981 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
982
983 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
984 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
985 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
986 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700987
988 // Segment coding disabled for compred testing
Visheshb1f6eb62020-03-31 13:43:35 +0530989 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700990 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
991 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
992 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700993 }
994 } else {
995 // Disable segmentation and clear down features if alt ref
996 // is not active for this group
997
Yaowu Xuf883b422016-08-30 14:01:10 -0700998 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700999
Vishesh8c90fff2020-04-13 11:32:56 +05301000 memset(cpi->enc_seg.map, 0,
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001001 cm->mi_params.mi_rows * cm->mi_params.mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001002
1003 seg->update_map = 0;
1004 seg->update_data = 0;
1005
Yaowu Xuf883b422016-08-30 14:01:10 -07001006 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001007 }
1008 } else if (rc->is_src_frame_alt_ref) {
1009 // Special case where we are coding over the top of a previous
1010 // alt ref frame.
1011 // Segment coding disabled for compred testing
1012
1013 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -07001014 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
1015 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001016
1017 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -07001018 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
1019 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
1020 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
1021 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001022
1023 // Skip all MBs if high Q (0,0 mv and skip coeffs)
1024 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001025 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
1026 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001027 }
1028 // Enable data update
1029 seg->update_data = 1;
1030 } else {
1031 // All other frames.
1032
1033 // No updates.. leave things as they are.
1034 seg->update_map = 0;
1035 seg->update_data = 0;
1036 }
1037 }
1038}
1039
Yaowu Xuf883b422016-08-30 14:01:10 -07001040static void update_reference_segmentation_map(AV1_COMP *cpi) {
1041 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001042 const CommonModeInfoParams *const mi_params = &cm->mi_params;
1043 MB_MODE_INFO **mi_4x4_ptr = mi_params->mi_grid_base;
David Turnerb757ce02018-11-12 15:01:28 +00001044 uint8_t *cache_ptr = cm->cur_frame->seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001045
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001046 for (int row = 0; row < mi_params->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -07001047 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001048 uint8_t *cache = cache_ptr;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001049 for (int col = 0; col < mi_params->mi_cols; col++, mi_4x4++, cache++)
Yushin Choa7f65922018-04-04 16:06:11 -07001050 cache[0] = mi_4x4[0]->segment_id;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001051 mi_4x4_ptr += mi_params->mi_stride;
1052 cache_ptr += mi_params->mi_cols;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001053 }
1054}
1055
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05301056static void alloc_altref_frame_buffer(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001057 AV1_COMMON *cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001058 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -07001059 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001060
Yaowu Xuc27fc142016-08-22 16:08:15 -07001061 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001062 if (aom_realloc_frame_buffer(
1063 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
1064 seq_params->subsampling_x, seq_params->subsampling_y,
Yunqing Wanga40d9e82020-02-21 16:37:38 -08001065 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi6237b882020-03-26 15:02:26 -07001066 cm->features.byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07001067 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001068 "Failed to allocate altref buffer");
1069}
1070
Yaowu Xuf883b422016-08-30 14:01:10 -07001071static void alloc_util_frame_buffers(AV1_COMP *cpi) {
1072 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001073 const SequenceHeader *const seq_params = &cm->seq_params;
Urvang Joshi6237b882020-03-26 15:02:26 -07001074 const int byte_alignment = cm->features.byte_alignment;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001075 if (aom_realloc_frame_buffer(
1076 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
1077 seq_params->subsampling_y, seq_params->use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07001078 cpi->oxcf.border_in_pixels, byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07001079 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001080 "Failed to allocate last frame buffer");
1081
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07001082 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08001083 &cpi->trial_frame_rst, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001084 cm->superres_upscaled_height, seq_params->subsampling_x,
1085 seq_params->subsampling_y, seq_params->use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07001086 AOM_RESTORATION_FRAME_BORDER, byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -08001087 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -08001088 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -07001089
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001090 if (aom_realloc_frame_buffer(
1091 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
1092 seq_params->subsampling_y, seq_params->use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07001093 cpi->oxcf.border_in_pixels, byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07001094 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001095 "Failed to allocate scaled source buffer");
1096
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001097 if (aom_realloc_frame_buffer(
1098 &cpi->scaled_last_source, cm->width, cm->height,
1099 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +05301100 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi6237b882020-03-26 15:02:26 -07001101 byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07001102 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001103 "Failed to allocate scaled last source buffer");
1104}
1105
Cheng Chen46f30c72017-09-07 11:13:33 -07001106static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001107 AV1_COMMON *cm = &cpi->common;
Vishesh686aa772020-04-13 14:40:12 +05301108 TokenInfo *token_info = &cpi->token_info;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001109
chiyotsaia7091f12019-08-09 16:48:27 -07001110 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
1111 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1112 "Failed to allocate context buffers");
1113 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001114
Mufaddal Chakeracfa88a32020-03-30 18:34:43 +05301115 if (!is_stat_generation_stage(cpi)) {
1116 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -07001117
Visheshd1317912020-04-07 14:39:44 +05301118 alloc_context_buffers_ext(cm, &cpi->mbmi_ext_info);
Mufaddal Chakeracfa88a32020-03-30 18:34:43 +05301119 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001120
Vishesh686aa772020-04-13 14:40:12 +05301121 free_token_info(token_info);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001122
Mufaddal Chakera3d5c0fb2020-03-27 11:04:26 +05301123 if (!is_stat_generation_stage(cpi)) {
Vishesh686aa772020-04-13 14:40:12 +05301124 alloc_token_info(cm, token_info);
Mufaddal Chakera3d5c0fb2020-03-27 11:04:26 +05301125 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001126
Yue Cheneb628982019-08-29 15:17:13 -07001127 av1_setup_shared_coeff_buffer(&cpi->common, &cpi->td.shared_coeff_buf);
1128 av1_setup_sms_tree(cpi, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001129}
1130
Yaowu Xuf883b422016-08-30 14:01:10 -07001131void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001132 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001133 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001134}
1135
Hui Suef139e12019-05-20 15:51:22 -07001136double av1_get_compression_ratio(const AV1_COMMON *const cm,
1137 size_t encoded_frame_size) {
1138 const int upscaled_width = cm->superres_upscaled_width;
1139 const int height = cm->height;
1140 const int luma_pic_size = upscaled_width * height;
1141 const SequenceHeader *const seq_params = &cm->seq_params;
1142 const BITSTREAM_PROFILE profile = seq_params->profile;
1143 const int pic_size_profile_factor =
1144 profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
1145 encoded_frame_size =
1146 (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
1147 const size_t uncompressed_frame_size =
1148 (luma_pic_size * pic_size_profile_factor) >> 3;
1149 return uncompressed_frame_size / (double)encoded_frame_size;
1150}
1151
Yunqing Wang75e20e82018-06-16 12:10:48 -07001152static void set_tile_info(AV1_COMP *cpi) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001153 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001154 const CommonModeInfoParams *const mi_params = &cm->mi_params;
Urvang Joshi54ffae72020-03-23 13:37:10 -07001155 const SequenceHeader *const seq_params = &cm->seq_params;
1156 CommonTileParams *const tiles = &cm->tiles;
Dominic Symesf58f1112017-09-25 12:47:40 +02001157 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001158
1159 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001160
1161 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +02001162 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Urvang Joshi54ffae72020-03-23 13:37:10 -07001163 tiles->uniform_spacing = 1;
1164 tiles->log2_cols = AOMMAX(cpi->oxcf.tile_columns, tiles->min_log2_cols);
1165 tiles->log2_cols = AOMMIN(tiles->log2_cols, tiles->max_log2_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +02001166 } else {
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001167 int mi_cols =
1168 ALIGN_POWER_OF_TWO(mi_params->mi_cols, seq_params->mib_size_log2);
Urvang Joshi54ffae72020-03-23 13:37:10 -07001169 int sb_cols = mi_cols >> seq_params->mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001170 int size_sb, j = 0;
Urvang Joshi54ffae72020-03-23 13:37:10 -07001171 tiles->uniform_spacing = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +02001172 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
Urvang Joshi54ffae72020-03-23 13:37:10 -07001173 tiles->col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001174 size_sb = cpi->oxcf.tile_widths[j++];
1175 if (j >= cpi->oxcf.tile_width_count) j = 0;
Urvang Joshi54ffae72020-03-23 13:37:10 -07001176 start_sb += AOMMIN(size_sb, tiles->max_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +02001177 }
Urvang Joshi54ffae72020-03-23 13:37:10 -07001178 tiles->cols = i;
1179 tiles->col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001180 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001181 av1_calculate_tile_cols(seq_params, mi_params->mi_rows, mi_params->mi_cols,
1182 tiles);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001183
1184 // configure tile rows
Urvang Joshi54ffae72020-03-23 13:37:10 -07001185 if (tiles->uniform_spacing) {
1186 tiles->log2_rows = AOMMAX(cpi->oxcf.tile_rows, tiles->min_log2_rows);
1187 tiles->log2_rows = AOMMIN(tiles->log2_rows, tiles->max_log2_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +02001188 } else {
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001189 int mi_rows =
1190 ALIGN_POWER_OF_TWO(mi_params->mi_rows, seq_params->mib_size_log2);
Urvang Joshi54ffae72020-03-23 13:37:10 -07001191 int sb_rows = mi_rows >> seq_params->mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001192 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +02001193 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
Urvang Joshi54ffae72020-03-23 13:37:10 -07001194 tiles->row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001195 size_sb = cpi->oxcf.tile_heights[j++];
1196 if (j >= cpi->oxcf.tile_height_count) j = 0;
Urvang Joshi54ffae72020-03-23 13:37:10 -07001197 start_sb += AOMMIN(size_sb, tiles->max_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +02001198 }
Urvang Joshi54ffae72020-03-23 13:37:10 -07001199 tiles->rows = i;
1200 tiles->row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001201 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001202 av1_calculate_tile_rows(seq_params, mi_params->mi_rows, tiles);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001203}
1204
Yaowu Xuf883b422016-08-30 14:01:10 -07001205static void update_frame_size(AV1_COMP *cpi) {
1206 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001207 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
1208
chiyotsaia7091f12019-08-09 16:48:27 -07001209 // We need to reallocate the context buffers here in case we need more mis.
1210 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
1211 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1212 "Failed to allocate context buffers");
1213 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001214 av1_init_mi_buffers(&cm->mi_params);
chiyotsaia7091f12019-08-09 16:48:27 -07001215
Luc Trudeau1e84af52017-11-25 15:00:28 -05001216 av1_init_macroblockd(cm, xd, NULL);
chiyotsai426c0662019-08-05 16:15:11 -07001217
Visheshd1317912020-04-07 14:39:44 +05301218 if (!is_stat_generation_stage(cpi))
1219 alloc_context_buffers_ext(cm, &cpi->mbmi_ext_info);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001220 set_tile_info(cpi);
1221}
1222
Vishesh2260a592020-04-06 15:34:51 +05301223static void init_buffer_indices(ForceIntegerMVInfo *const force_intpel_info,
1224 int *const remapped_ref_idx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001225 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -07001226 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
Vishesh2260a592020-04-06 15:34:51 +05301227 remapped_ref_idx[fb_idx] = fb_idx;
1228 force_intpel_info->rate_index = 0;
1229 force_intpel_info->rate_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001230}
1231
Debargha Mukherjee57498692018-05-11 13:29:31 -07001232static INLINE int does_level_match(int width, int height, double fps,
1233 int lvl_width, int lvl_height,
1234 double lvl_fps, int lvl_dim_mult) {
1235 const int64_t lvl_luma_pels = lvl_width * lvl_height;
1236 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
1237 const int64_t luma_pels = width * height;
1238 const double display_sample_rate = luma_pels * fps;
1239 return luma_pels <= lvl_luma_pels &&
1240 display_sample_rate <= lvl_display_sample_rate &&
1241 width <= lvl_width * lvl_dim_mult &&
1242 height <= lvl_height * lvl_dim_mult;
1243}
1244
Andrey Norkin26495512018-06-20 17:13:11 -07001245static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Andrey Norkinf481d982018-05-15 12:05:31 -07001246 const AV1EncoderConfig *oxcf) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001247 // TODO(any): This is a placeholder function that only addresses dimensions
1248 // and max display sample rates.
1249 // Need to add checks for max bit rate, max decoded luma sample rate, header
1250 // rate, etc. that are not covered by this function.
Hui Su8427ff22019-03-11 10:14:33 -07001251 AV1_LEVEL level = SEQ_LEVEL_MAX;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001252 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
1253 288, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001254 level = SEQ_LEVEL_2_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001255 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1256 704, 396, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001257 level = SEQ_LEVEL_2_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001258 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1259 1088, 612, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001260 level = SEQ_LEVEL_3_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001261 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1262 1376, 774, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001263 level = SEQ_LEVEL_3_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001264 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1265 2048, 1152, 30.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001266 level = SEQ_LEVEL_4_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001267 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1268 2048, 1152, 60.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001269 level = SEQ_LEVEL_4_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001270 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1271 4096, 2176, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001272 level = SEQ_LEVEL_5_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001273 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1274 4096, 2176, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001275 level = SEQ_LEVEL_5_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001276 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1277 4096, 2176, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001278 level = SEQ_LEVEL_5_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001279 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1280 8192, 4352, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001281 level = SEQ_LEVEL_6_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001282 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1283 8192, 4352, 60.0, 2)) {
Todd Nguyenff4c8602019-11-25 13:20:21 -08001284 level = SEQ_LEVEL_6_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001285 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1286 8192, 4352, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001287 level = SEQ_LEVEL_6_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001288 }
chiyotsaiaa33bbf2019-12-02 12:07:53 -08001289
Urvang Joshi450a9a22020-03-31 16:00:22 -07001290 SequenceHeader *const seq_params = &cm->seq_params;
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001291 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su8427ff22019-03-11 10:14:33 -07001292 seq->seq_level_idx[i] = level;
Andrey Norkin26495512018-06-20 17:13:11 -07001293 // Set the maximum parameters for bitrate and buffer size for this profile,
1294 // level, and tier
Urvang Joshi450a9a22020-03-31 16:00:22 -07001295 seq_params->op_params[i].bitrate = av1_max_level_bitrate(
Hui Su8427ff22019-03-11 10:14:33 -07001296 cm->seq_params.profile, seq->seq_level_idx[i], seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001297 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
1298 // check
Urvang Joshi450a9a22020-03-31 16:00:22 -07001299 if (seq_params->op_params[i].bitrate == 0)
Andrey Norkin26495512018-06-20 17:13:11 -07001300 aom_internal_error(
1301 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
1302 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -07001303 // Buffer size in bits/s is bitrate in bits/s * 1 s
Urvang Joshi450a9a22020-03-31 16:00:22 -07001304 seq_params->op_params[i].buffer_size = seq_params->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001305 }
1306}
1307
Andrey Norkin26495512018-06-20 17:13:11 -07001308static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
Marco Paniconi67142112019-07-24 15:00:31 -07001309 const AV1EncoderConfig *oxcf, int use_svc) {
Yaowu Xu2a9ac432019-08-06 14:21:17 -07001310 seq->still_picture = (oxcf->force_video_mode == 0) && (oxcf->limit == 1);
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001311 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -07001312 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
kyslov94243382019-05-02 15:33:32 -07001313 seq->force_screen_content_tools = (oxcf->mode == REALTIME) ? 0 : 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001314 seq->force_integer_mv = 2;
David Turnerebf96f42018-11-14 16:57:57 +00001315 seq->order_hint_info.enable_order_hint = oxcf->enable_order_hint;
David Turner936235c2018-11-28 13:42:01 +00001316 seq->frame_id_numbers_present_flag =
1317 !(seq->still_picture && seq->reduced_still_picture_hdr) &&
Marco Paniconi67142112019-07-24 15:00:31 -07001318 !oxcf->large_scale_tile && oxcf->error_resilient_mode && !use_svc;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001319 if (seq->still_picture && seq->reduced_still_picture_hdr) {
David Turnerebf96f42018-11-14 16:57:57 +00001320 seq->order_hint_info.enable_order_hint = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001321 seq->force_screen_content_tools = 2;
1322 seq->force_integer_mv = 2;
1323 }
David Turnerebf96f42018-11-14 16:57:57 +00001324 seq->order_hint_info.order_hint_bits_minus_1 =
1325 seq->order_hint_info.enable_order_hint
1326 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
1327 : -1;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001328
David Turner760a2f42018-12-07 15:25:36 +00001329 seq->max_frame_width =
1330 oxcf->forced_max_frame_width ? oxcf->forced_max_frame_width : oxcf->width;
1331 seq->max_frame_height = oxcf->forced_max_frame_height
1332 ? oxcf->forced_max_frame_height
1333 : oxcf->height;
1334 seq->num_bits_width =
1335 (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
1336 seq->num_bits_height =
1337 (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
1338 assert(seq->num_bits_width <= 16);
1339 assert(seq->num_bits_height <= 16);
1340
1341 seq->frame_id_length = FRAME_ID_LENGTH;
1342 seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
1343
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001344 seq->enable_dual_filter = oxcf->enable_dual_filter;
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -08001345 seq->order_hint_info.enable_dist_wtd_comp = oxcf->enable_dist_wtd_comp;
1346 seq->order_hint_info.enable_dist_wtd_comp &=
David Turnerebf96f42018-11-14 16:57:57 +00001347 seq->order_hint_info.enable_order_hint;
1348 seq->order_hint_info.enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1349 seq->order_hint_info.enable_ref_frame_mvs &=
1350 seq->order_hint_info.enable_order_hint;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001351 seq->enable_superres = oxcf->enable_superres;
1352 seq->enable_cdef = oxcf->enable_cdef;
1353 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -07001354 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -08001355 seq->enable_interintra_compound = oxcf->enable_interintra_comp;
1356 seq->enable_masked_compound = oxcf->enable_masked_comp;
Debargha Mukherjee03c43ba2018-12-14 13:08:08 -08001357 seq->enable_intra_edge_filter = oxcf->enable_intra_edge_filter;
Yue Chen8f9ca582018-12-12 15:11:47 -08001358 seq->enable_filter_intra = oxcf->enable_filter_intra;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001359
Andrey Norkin26495512018-06-20 17:13:11 -07001360 set_bitstream_level_tier(seq, cm, oxcf);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001361
1362 if (seq->operating_points_cnt_minus_1 == 0) {
1363 seq->operating_point_idc[0] = 0;
1364 } else {
Marco Paniconi6da983b2019-09-16 20:12:43 -07001365 // Set operating_point_idc[] such that the i=0 point corresponds to the
1366 // highest quality operating point (all layers), and subsequent
1367 // operarting points (i > 0) are lower quality corresponding to
1368 // skip decoding enhancement layers (temporal first).
1369 int i = 0;
1370 assert(seq->operating_points_cnt_minus_1 ==
1371 (int)(cm->number_spatial_layers * cm->number_temporal_layers - 1));
1372 for (unsigned int sl = 0; sl < cm->number_spatial_layers; sl++) {
1373 for (unsigned int tl = 0; tl < cm->number_temporal_layers; tl++) {
1374 seq->operating_point_idc[i] =
1375 (~(~0u << (cm->number_spatial_layers - sl)) << 8) |
1376 ~(~0u << (cm->number_temporal_layers - tl));
1377 i++;
1378 }
1379 }
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001380 }
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001381}
1382
Yaowu Xuf883b422016-08-30 14:01:10 -07001383static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1384 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi450a9a22020-03-31 16:00:22 -07001385 SequenceHeader *const seq_params = &cm->seq_params;
Jayasanker J0b1dd292020-04-09 15:47:25 +05301386 ResizePendingParams *resize_pending_params = &cpi->resize_pending_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001387
1388 cpi->oxcf = *oxcf;
1389 cpi->framerate = oxcf->init_framerate;
1390
Urvang Joshi450a9a22020-03-31 16:00:22 -07001391 seq_params->profile = oxcf->profile;
1392 seq_params->bit_depth = oxcf->bit_depth;
1393 seq_params->use_highbitdepth = oxcf->use_highbitdepth;
1394 seq_params->color_primaries = oxcf->color_primaries;
1395 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
1396 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
1397 seq_params->monochrome = oxcf->monochrome;
1398 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
1399 seq_params->color_range = oxcf->color_range;
1400 seq_params->timing_info_present = oxcf->timing_info_present;
1401 seq_params->timing_info.num_units_in_display_tick =
Andrey Norkin795ba872018-03-06 13:24:14 -08001402 oxcf->timing_info.num_units_in_display_tick;
Urvang Joshi450a9a22020-03-31 16:00:22 -07001403 seq_params->timing_info.time_scale = oxcf->timing_info.time_scale;
1404 seq_params->timing_info.equal_picture_interval =
Andrey Norkin795ba872018-03-06 13:24:14 -08001405 oxcf->timing_info.equal_picture_interval;
Urvang Joshi450a9a22020-03-31 16:00:22 -07001406 seq_params->timing_info.num_ticks_per_picture =
Andrey Norkin795ba872018-03-06 13:24:14 -08001407 oxcf->timing_info.num_ticks_per_picture;
1408
Urvang Joshi450a9a22020-03-31 16:00:22 -07001409 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07001410 oxcf->display_model_info_present_flag;
Urvang Joshi450a9a22020-03-31 16:00:22 -07001411 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001412 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08001413 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001414 // set the decoder model parameters in schedule mode
Urvang Joshi450a9a22020-03-31 16:00:22 -07001415 seq_params->decoder_model_info.num_units_in_decoding_tick =
Andrey Norkin795ba872018-03-06 13:24:14 -08001416 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07001417 cm->buffer_removal_time_present = 1;
Urvang Joshi450a9a22020-03-31 16:00:22 -07001418 av1_set_aom_dec_model_info(&seq_params->decoder_model_info);
1419 av1_set_dec_model_op_parameters(&seq_params->op_params[0]);
1420 } else if (seq_params->timing_info_present &&
1421 seq_params->timing_info.equal_picture_interval &&
1422 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001423 // set the decoder model parameters in resource availability mode
Urvang Joshi450a9a22020-03-31 16:00:22 -07001424 av1_set_resource_availability_parameters(&seq_params->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001425 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -07001426 seq_params->op_params[0].initial_display_delay =
Andrey Norkinc7511de2018-06-22 12:31:06 -07001427 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08001428 }
Andrey Norkinc7511de2018-06-22 12:31:06 -07001429
Urvang Joshi450a9a22020-03-31 16:00:22 -07001430 if (seq_params->monochrome) {
1431 seq_params->subsampling_x = 1;
1432 seq_params->subsampling_y = 1;
1433 } else if (seq_params->color_primaries == AOM_CICP_CP_BT_709 &&
1434 seq_params->transfer_characteristics == AOM_CICP_TC_SRGB &&
1435 seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) {
1436 seq_params->subsampling_x = 0;
1437 seq_params->subsampling_y = 0;
Tom Fineganf8d6a162018-08-21 10:47:55 -07001438 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -07001439 if (seq_params->profile == 0) {
1440 seq_params->subsampling_x = 1;
1441 seq_params->subsampling_y = 1;
1442 } else if (seq_params->profile == 1) {
1443 seq_params->subsampling_x = 0;
1444 seq_params->subsampling_y = 0;
Tom Fineganf8d6a162018-08-21 10:47:55 -07001445 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -07001446 if (seq_params->bit_depth == AOM_BITS_12) {
1447 seq_params->subsampling_x = oxcf->chroma_subsampling_x;
1448 seq_params->subsampling_y = oxcf->chroma_subsampling_y;
Tom Fineganf8d6a162018-08-21 10:47:55 -07001449 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -07001450 seq_params->subsampling_x = 1;
1451 seq_params->subsampling_y = 0;
Tom Fineganf8d6a162018-08-21 10:47:55 -07001452 }
1453 }
Tom Finegan02b2a842018-08-24 13:50:00 -07001454 }
1455
Yaowu Xuc27fc142016-08-22 16:08:15 -07001456 cm->width = oxcf->width;
1457 cm->height = oxcf->height;
Urvang Joshi450a9a22020-03-31 16:00:22 -07001458 set_sb_size(seq_params,
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001459 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -07001460 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -07001461
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08001462 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001463
1464 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -07001465 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001466
Marco Paniconi882c5ad2019-09-30 21:32:29 -07001467 // Set init SVC parameters.
Marco Paniconi67142112019-07-24 15:00:31 -07001468 cpi->use_svc = 0;
Marco Paniconid8574e32019-08-04 21:30:12 -07001469 cpi->svc.external_ref_frame_config = 0;
1470 cpi->svc.non_reference_frame = 0;
Marco Paniconi57247a82020-04-13 09:19:07 -07001471 cpi->svc.number_spatial_layers = 1;
1472 cpi->svc.number_temporal_layers = 1;
Marco Paniconi67142112019-07-24 15:00:31 -07001473 cm->number_spatial_layers = 1;
1474 cm->number_temporal_layers = 1;
Marco Paniconi882c5ad2019-09-30 21:32:29 -07001475 cm->spatial_layer_id = 0;
1476 cm->temporal_layer_id = 0;
Marco Paniconi67142112019-07-24 15:00:31 -07001477
Yaowu Xuc27fc142016-08-22 16:08:15 -07001478 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001479 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001480
Yaowu Xuc27fc142016-08-22 16:08:15 -07001481 cpi->ref_frame_flags = 0;
1482
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001483 // Reset resize pending flags
Jayasanker J0b1dd292020-04-09 15:47:25 +05301484 resize_pending_params->width = 0;
1485 resize_pending_params->height = 0;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001486
Vishesh2260a592020-04-06 15:34:51 +05301487 init_buffer_indices(&cpi->force_intpel_info, cm->remapped_ref_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001488}
1489
1490static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001491 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001492 const int64_t bandwidth = oxcf->target_bandwidth;
1493 const int64_t starting = oxcf->starting_buffer_level_ms;
1494 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1495 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1496
1497 rc->starting_buffer_level = starting * bandwidth / 1000;
1498 rc->optimal_buffer_level =
1499 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1500 rc->maximum_buffer_size =
1501 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1502}
1503
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001504#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1505 cpi->fn_ptr[BT].sdf = SDF; \
1506 cpi->fn_ptr[BT].sdaf = SDAF; \
1507 cpi->fn_ptr[BT].vf = VF; \
1508 cpi->fn_ptr[BT].svf = SVF; \
1509 cpi->fn_ptr[BT].svaf = SVAF; \
1510 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1511 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001512 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001513
1514#define MAKE_BFP_SAD_WRAPPER(fnname) \
1515 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1516 int source_stride, \
1517 const uint8_t *ref_ptr, int ref_stride) { \
1518 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1519 } \
1520 static unsigned int fnname##_bits10( \
1521 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1522 int ref_stride) { \
1523 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1524 } \
1525 static unsigned int fnname##_bits12( \
1526 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1527 int ref_stride) { \
1528 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1529 }
1530
1531#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1532 static unsigned int fnname##_bits8( \
1533 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1534 int ref_stride, const uint8_t *second_pred) { \
1535 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1536 } \
1537 static unsigned int fnname##_bits10( \
1538 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1539 int ref_stride, const uint8_t *second_pred) { \
1540 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1541 2; \
1542 } \
1543 static unsigned int fnname##_bits12( \
1544 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1545 int ref_stride, const uint8_t *second_pred) { \
1546 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1547 4; \
1548 }
1549
Yaowu Xuc27fc142016-08-22 16:08:15 -07001550#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1551 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1552 const uint8_t *const ref_ptr[], int ref_stride, \
1553 unsigned int *sad_array) { \
1554 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1555 } \
1556 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1557 const uint8_t *const ref_ptr[], int ref_stride, \
1558 unsigned int *sad_array) { \
1559 int i; \
1560 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1561 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1562 } \
1563 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1564 const uint8_t *const ref_ptr[], int ref_stride, \
1565 unsigned int *sad_array) { \
1566 int i; \
1567 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1568 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1569 }
1570
Cheng Chenbf3d4962017-11-01 14:48:52 -07001571#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1572 static unsigned int fnname##_bits8( \
1573 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1574 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001575 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001576 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1577 jcp_param); \
1578 } \
1579 static unsigned int fnname##_bits10( \
1580 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1581 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001582 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001583 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1584 jcp_param) >> \
1585 2; \
1586 } \
1587 static unsigned int fnname##_bits12( \
1588 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1589 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001590 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001591 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1592 jcp_param) >> \
1593 4; \
1594 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001595
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001596#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001597MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1598MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001599MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1600MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1601MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1602MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1603MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1604MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1605MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001606MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1607MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1608MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1609MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1610MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1611MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1612MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1613MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1614MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1615MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1616MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1617MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1618MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1619MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001620MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1621MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1622MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001623MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1624MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1625MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001626MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1627MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1628MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001629MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1630MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1631MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001632MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1633MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1634MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001635MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1636MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1637MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001638MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1639MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1640MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001641MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1642MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1643MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001644MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001645
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001646MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1647MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1648MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1649MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1650MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1651MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1652MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1653MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1654MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1655MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1656MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1657MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001658MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1659MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1660MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1661MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1662MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1663MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001664
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001665MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x128_avg)
1666MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x64_avg)
1667MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x128_avg)
1668MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x16_avg)
1669MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x32_avg)
1670MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x32_avg)
1671MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x64_avg)
1672MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x32_avg)
1673MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x64_avg)
1674MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x16_avg)
1675MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x8_avg)
1676MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x16_avg)
1677MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x8_avg)
1678MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x4_avg)
1679MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x8_avg)
1680MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x4_avg)
1681MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x16_avg)
1682MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x4_avg)
1683MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x32_avg)
1684MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x8_avg)
1685MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x64_avg)
1686MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x16_avg)
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001687#endif // CONFIG_AV1_HIGHBITDEPTH
Cheng Chenbf3d4962017-11-01 14:48:52 -07001688
David Barker0f3c94e2017-05-16 15:21:50 +01001689#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001690 cpi->fn_ptr[BT].msdf = MCSDF; \
1691 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001692
David Barkerc155e012017-05-11 13:54:54 +01001693#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1694 static unsigned int fnname##_bits8( \
1695 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1696 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1697 int m_stride, int invert_mask) { \
1698 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1699 second_pred_ptr, m, m_stride, invert_mask); \
1700 } \
1701 static unsigned int fnname##_bits10( \
1702 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1703 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1704 int m_stride, int invert_mask) { \
1705 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1706 second_pred_ptr, m, m_stride, invert_mask) >> \
1707 2; \
1708 } \
1709 static unsigned int fnname##_bits12( \
1710 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1711 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1712 int m_stride, int invert_mask) { \
1713 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1714 second_pred_ptr, m, m_stride, invert_mask) >> \
1715 4; \
1716 }
1717
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001718#if CONFIG_AV1_HIGHBITDEPTH
David Barkerf19f35f2017-05-22 16:33:22 +01001719MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1720MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1721MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001722MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1723MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1724MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1725MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1726MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1727MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1728MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1729MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1730MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1731MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1732MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1733MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1734MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001735MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1736MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1737MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1738MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001739MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1740MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001741#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001742
Yaowu Xuc27fc142016-08-22 16:08:15 -07001743#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1744 cpi->fn_ptr[BT].osdf = OSDF; \
1745 cpi->fn_ptr[BT].ovf = OVF; \
1746 cpi->fn_ptr[BT].osvf = OSVF;
1747
1748#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1749 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1750 const int32_t *wsrc, \
1751 const int32_t *msk) { \
1752 return fnname(ref, ref_stride, wsrc, msk); \
1753 } \
1754 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1755 const int32_t *wsrc, \
1756 const int32_t *msk) { \
1757 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1758 } \
1759 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1760 const int32_t *wsrc, \
1761 const int32_t *msk) { \
1762 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1763 }
1764
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001765#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001766MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1767MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1768MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001769MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1770MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1771MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1772MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1773MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1774MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1775MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1776MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1777MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1778MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1779MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1780MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1781MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001782MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1783MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1784MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1785MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001786MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1787MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001788
Yaowu Xuf883b422016-08-30 14:01:10 -07001789static void highbd_set_var_fns(AV1_COMP *const cpi) {
1790 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001791 if (cm->seq_params.use_highbitdepth) {
1792 switch (cm->seq_params.bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001793 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001794 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1795 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1796 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001797 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001798 aom_highbd_sad64x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001799 aom_highbd_dist_wtd_sad64x16_avg_bits8,
1800 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001801
1802 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1803 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1804 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001805 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001806 aom_highbd_sad16x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001807 aom_highbd_dist_wtd_sad16x64_avg_bits8,
1808 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001809
1810 HIGHBD_BFP(
1811 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1812 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001813 aom_highbd_8_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001814 aom_highbd_sad32x8x4d_bits8, aom_highbd_dist_wtd_sad32x8_avg_bits8,
1815 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001816
1817 HIGHBD_BFP(
1818 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1819 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001820 aom_highbd_8_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001821 aom_highbd_sad8x32x4d_bits8, aom_highbd_dist_wtd_sad8x32_avg_bits8,
1822 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001823
1824 HIGHBD_BFP(
1825 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1826 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001827 aom_highbd_8_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001828 aom_highbd_sad16x4x4d_bits8, aom_highbd_dist_wtd_sad16x4_avg_bits8,
1829 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001830
1831 HIGHBD_BFP(
1832 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1833 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001834 aom_highbd_8_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001835 aom_highbd_sad4x16x4d_bits8, aom_highbd_dist_wtd_sad4x16_avg_bits8,
1836 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001837
1838 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1839 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1840 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001841 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001842 aom_highbd_sad32x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001843 aom_highbd_dist_wtd_sad32x16_avg_bits8,
1844 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001845
1846 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1847 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1848 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001849 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001850 aom_highbd_sad16x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001851 aom_highbd_dist_wtd_sad16x32_avg_bits8,
1852 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001853
1854 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1855 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1856 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001857 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001858 aom_highbd_sad64x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001859 aom_highbd_dist_wtd_sad64x32_avg_bits8,
1860 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001861
1862 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1863 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1864 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001865 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001866 aom_highbd_sad32x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001867 aom_highbd_dist_wtd_sad32x64_avg_bits8,
1868 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001869
1870 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1871 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1872 aom_highbd_8_sub_pixel_variance32x32,
1873 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001874 aom_highbd_sad32x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001875 aom_highbd_dist_wtd_sad32x32_avg_bits8,
1876 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001877
1878 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1879 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1880 aom_highbd_8_sub_pixel_variance64x64,
1881 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001882 aom_highbd_sad64x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001883 aom_highbd_dist_wtd_sad64x64_avg_bits8,
1884 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001885
1886 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1887 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1888 aom_highbd_8_sub_pixel_variance16x16,
1889 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001890 aom_highbd_sad16x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001891 aom_highbd_dist_wtd_sad16x16_avg_bits8,
1892 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001893
1894 HIGHBD_BFP(
1895 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1896 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001897 aom_highbd_8_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001898 aom_highbd_sad16x8x4d_bits8, aom_highbd_dist_wtd_sad16x8_avg_bits8,
1899 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001900
1901 HIGHBD_BFP(
1902 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1903 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001904 aom_highbd_8_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001905 aom_highbd_sad8x16x4d_bits8, aom_highbd_dist_wtd_sad8x16_avg_bits8,
1906 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001907
Cheng Chenbf3d4962017-11-01 14:48:52 -07001908 HIGHBD_BFP(
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001909 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1910 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1911 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x4d_bits8,
1912 aom_highbd_dist_wtd_sad8x8_avg_bits8,
1913 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x8)
1914
1915 HIGHBD_BFP(
1916 BLOCK_8X4, aom_highbd_sad8x4_bits8, aom_highbd_sad8x4_avg_bits8,
1917 aom_highbd_8_variance8x4, aom_highbd_8_sub_pixel_variance8x4,
1918 aom_highbd_8_sub_pixel_avg_variance8x4, aom_highbd_sad8x4x4d_bits8,
1919 aom_highbd_dist_wtd_sad8x4_avg_bits8,
1920 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x4)
1921
1922 HIGHBD_BFP(
1923 BLOCK_4X8, aom_highbd_sad4x8_bits8, aom_highbd_sad4x8_avg_bits8,
1924 aom_highbd_8_variance4x8, aom_highbd_8_sub_pixel_variance4x8,
1925 aom_highbd_8_sub_pixel_avg_variance4x8, aom_highbd_sad4x8x4d_bits8,
1926 aom_highbd_dist_wtd_sad4x8_avg_bits8,
1927 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x8)
1928
1929 HIGHBD_BFP(
1930 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1931 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1932 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x4d_bits8,
1933 aom_highbd_dist_wtd_sad4x4_avg_bits8,
1934 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x4)
1935
1936 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1937 aom_highbd_sad128x128_avg_bits8,
1938 aom_highbd_8_variance128x128,
1939 aom_highbd_8_sub_pixel_variance128x128,
1940 aom_highbd_8_sub_pixel_avg_variance128x128,
1941 aom_highbd_sad128x128x4d_bits8,
1942 aom_highbd_dist_wtd_sad128x128_avg_bits8,
1943 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001944
1945 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1946 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1947 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001948 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001949 aom_highbd_sad128x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001950 aom_highbd_dist_wtd_sad128x64_avg_bits8,
1951 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001952
1953 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1954 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1955 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001956 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001957 aom_highbd_sad64x128x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001958 aom_highbd_dist_wtd_sad64x128_avg_bits8,
1959 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001960
David Barkerf19f35f2017-05-22 16:33:22 +01001961 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1962 aom_highbd_8_masked_sub_pixel_variance128x128)
1963 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1964 aom_highbd_8_masked_sub_pixel_variance128x64)
1965 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1966 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001967 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1968 aom_highbd_8_masked_sub_pixel_variance64x64)
1969 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1970 aom_highbd_8_masked_sub_pixel_variance64x32)
1971 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1972 aom_highbd_8_masked_sub_pixel_variance32x64)
1973 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1974 aom_highbd_8_masked_sub_pixel_variance32x32)
1975 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1976 aom_highbd_8_masked_sub_pixel_variance32x16)
1977 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1978 aom_highbd_8_masked_sub_pixel_variance16x32)
1979 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1980 aom_highbd_8_masked_sub_pixel_variance16x16)
1981 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1982 aom_highbd_8_masked_sub_pixel_variance8x16)
1983 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1984 aom_highbd_8_masked_sub_pixel_variance16x8)
1985 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1986 aom_highbd_8_masked_sub_pixel_variance8x8)
1987 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1988 aom_highbd_8_masked_sub_pixel_variance4x8)
1989 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1990 aom_highbd_8_masked_sub_pixel_variance8x4)
1991 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1992 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001993 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1994 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001995 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1996 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001997 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1998 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001999 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
2000 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002001 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
2002 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002003 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
2004 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002005 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
2006 aom_highbd_obmc_variance128x128,
2007 aom_highbd_obmc_sub_pixel_variance128x128)
2008 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
2009 aom_highbd_obmc_variance128x64,
2010 aom_highbd_obmc_sub_pixel_variance128x64)
2011 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
2012 aom_highbd_obmc_variance64x128,
2013 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002014 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
2015 aom_highbd_obmc_variance64x64,
2016 aom_highbd_obmc_sub_pixel_variance64x64)
2017 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
2018 aom_highbd_obmc_variance64x32,
2019 aom_highbd_obmc_sub_pixel_variance64x32)
2020 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
2021 aom_highbd_obmc_variance32x64,
2022 aom_highbd_obmc_sub_pixel_variance32x64)
2023 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
2024 aom_highbd_obmc_variance32x32,
2025 aom_highbd_obmc_sub_pixel_variance32x32)
2026 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
2027 aom_highbd_obmc_variance32x16,
2028 aom_highbd_obmc_sub_pixel_variance32x16)
2029 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
2030 aom_highbd_obmc_variance16x32,
2031 aom_highbd_obmc_sub_pixel_variance16x32)
2032 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
2033 aom_highbd_obmc_variance16x16,
2034 aom_highbd_obmc_sub_pixel_variance16x16)
2035 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
2036 aom_highbd_obmc_variance8x16,
2037 aom_highbd_obmc_sub_pixel_variance8x16)
2038 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
2039 aom_highbd_obmc_variance16x8,
2040 aom_highbd_obmc_sub_pixel_variance16x8)
2041 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
2042 aom_highbd_obmc_variance8x8,
2043 aom_highbd_obmc_sub_pixel_variance8x8)
2044 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
2045 aom_highbd_obmc_variance4x8,
2046 aom_highbd_obmc_sub_pixel_variance4x8)
2047 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
2048 aom_highbd_obmc_variance8x4,
2049 aom_highbd_obmc_sub_pixel_variance8x4)
2050 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
2051 aom_highbd_obmc_variance4x4,
2052 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002053 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
2054 aom_highbd_obmc_variance64x16,
2055 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002056 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
2057 aom_highbd_obmc_variance16x64,
2058 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002059 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
2060 aom_highbd_obmc_variance32x8,
2061 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002062 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
2063 aom_highbd_obmc_variance8x32,
2064 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002065 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
2066 aom_highbd_obmc_variance16x4,
2067 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002068 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
2069 aom_highbd_obmc_variance4x16,
2070 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002071 break;
2072
Yaowu Xuf883b422016-08-30 14:01:10 -07002073 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002074 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
2075 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
2076 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002077 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002078 aom_highbd_sad64x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002079 aom_highbd_dist_wtd_sad64x16_avg_bits10,
2080 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002081
2082 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
2083 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
2084 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002085 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002086 aom_highbd_sad16x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002087 aom_highbd_dist_wtd_sad16x64_avg_bits10,
2088 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002089
2090 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
2091 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
2092 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002093 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002094 aom_highbd_sad32x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002095 aom_highbd_dist_wtd_sad32x8_avg_bits10,
2096 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002097
2098 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
2099 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
2100 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002101 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002102 aom_highbd_sad8x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002103 aom_highbd_dist_wtd_sad8x32_avg_bits10,
2104 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002105
2106 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
2107 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
2108 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002109 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002110 aom_highbd_sad16x4x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002111 aom_highbd_dist_wtd_sad16x4_avg_bits10,
2112 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002113
2114 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
2115 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
2116 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002117 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002118 aom_highbd_sad4x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002119 aom_highbd_dist_wtd_sad4x16_avg_bits10,
2120 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002121
2122 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
2123 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
2124 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002125 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002126 aom_highbd_sad32x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002127 aom_highbd_dist_wtd_sad32x16_avg_bits10,
2128 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002129
2130 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
2131 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
2132 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002133 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002134 aom_highbd_sad16x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002135 aom_highbd_dist_wtd_sad16x32_avg_bits10,
2136 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002137
2138 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
2139 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
2140 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002141 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002142 aom_highbd_sad64x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002143 aom_highbd_dist_wtd_sad64x32_avg_bits10,
2144 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002145
2146 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
2147 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
2148 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002149 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002150 aom_highbd_sad32x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002151 aom_highbd_dist_wtd_sad32x64_avg_bits10,
2152 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002153
2154 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
2155 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
2156 aom_highbd_10_sub_pixel_variance32x32,
2157 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002158 aom_highbd_sad32x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002159 aom_highbd_dist_wtd_sad32x32_avg_bits10,
2160 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002161
2162 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
2163 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
2164 aom_highbd_10_sub_pixel_variance64x64,
2165 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002166 aom_highbd_sad64x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002167 aom_highbd_dist_wtd_sad64x64_avg_bits10,
2168 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002169
2170 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
2171 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
2172 aom_highbd_10_sub_pixel_variance16x16,
2173 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002174 aom_highbd_sad16x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002175 aom_highbd_dist_wtd_sad16x16_avg_bits10,
2176 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002177
2178 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
2179 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
2180 aom_highbd_10_sub_pixel_variance16x8,
2181 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002182 aom_highbd_sad16x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002183 aom_highbd_dist_wtd_sad16x8_avg_bits10,
2184 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002185
2186 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
2187 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
2188 aom_highbd_10_sub_pixel_variance8x16,
2189 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002190 aom_highbd_sad8x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002191 aom_highbd_dist_wtd_sad8x16_avg_bits10,
2192 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002193
2194 HIGHBD_BFP(
2195 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
2196 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002197 aom_highbd_10_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002198 aom_highbd_sad8x8x4d_bits10, aom_highbd_dist_wtd_sad8x8_avg_bits10,
2199 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002200
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002201 HIGHBD_BFP(
2202 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
2203 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
2204 aom_highbd_10_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002205 aom_highbd_sad8x4x4d_bits10, aom_highbd_dist_wtd_sad8x4_avg_bits10,
2206 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002207
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002208 HIGHBD_BFP(
2209 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
2210 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
2211 aom_highbd_10_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002212 aom_highbd_sad4x8x4d_bits10, aom_highbd_dist_wtd_sad4x8_avg_bits10,
2213 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002214
2215 HIGHBD_BFP(
2216 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
2217 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002218 aom_highbd_10_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002219 aom_highbd_sad4x4x4d_bits10, aom_highbd_dist_wtd_sad4x4_avg_bits10,
2220 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002221
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002222 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
2223 aom_highbd_sad128x128_avg_bits10,
2224 aom_highbd_10_variance128x128,
2225 aom_highbd_10_sub_pixel_variance128x128,
2226 aom_highbd_10_sub_pixel_avg_variance128x128,
2227 aom_highbd_sad128x128x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002228 aom_highbd_dist_wtd_sad128x128_avg_bits10,
2229 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002230
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002231 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
2232 aom_highbd_sad128x64_avg_bits10,
2233 aom_highbd_10_variance128x64,
2234 aom_highbd_10_sub_pixel_variance128x64,
2235 aom_highbd_10_sub_pixel_avg_variance128x64,
2236 aom_highbd_sad128x64x4d_bits10,
2237 aom_highbd_dist_wtd_sad128x64_avg_bits10,
2238 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002239
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002240 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
2241 aom_highbd_sad64x128_avg_bits10,
2242 aom_highbd_10_variance64x128,
2243 aom_highbd_10_sub_pixel_variance64x128,
2244 aom_highbd_10_sub_pixel_avg_variance64x128,
2245 aom_highbd_sad64x128x4d_bits10,
2246 aom_highbd_dist_wtd_sad64x128_avg_bits10,
2247 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002248
David Barkerf19f35f2017-05-22 16:33:22 +01002249 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
2250 aom_highbd_10_masked_sub_pixel_variance128x128)
2251 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
2252 aom_highbd_10_masked_sub_pixel_variance128x64)
2253 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
2254 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002255 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
2256 aom_highbd_10_masked_sub_pixel_variance64x64)
2257 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
2258 aom_highbd_10_masked_sub_pixel_variance64x32)
2259 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
2260 aom_highbd_10_masked_sub_pixel_variance32x64)
2261 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
2262 aom_highbd_10_masked_sub_pixel_variance32x32)
2263 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
2264 aom_highbd_10_masked_sub_pixel_variance32x16)
2265 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
2266 aom_highbd_10_masked_sub_pixel_variance16x32)
2267 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
2268 aom_highbd_10_masked_sub_pixel_variance16x16)
2269 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
2270 aom_highbd_10_masked_sub_pixel_variance8x16)
2271 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
2272 aom_highbd_10_masked_sub_pixel_variance16x8)
2273 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
2274 aom_highbd_10_masked_sub_pixel_variance8x8)
2275 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
2276 aom_highbd_10_masked_sub_pixel_variance4x8)
2277 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
2278 aom_highbd_10_masked_sub_pixel_variance8x4)
2279 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
2280 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002281 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
2282 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002283 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
2284 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002285 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
2286 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002287 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
2288 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002289 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
2290 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002291 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
2292 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002293 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
2294 aom_highbd_10_obmc_variance128x128,
2295 aom_highbd_10_obmc_sub_pixel_variance128x128)
2296 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
2297 aom_highbd_10_obmc_variance128x64,
2298 aom_highbd_10_obmc_sub_pixel_variance128x64)
2299 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
2300 aom_highbd_10_obmc_variance64x128,
2301 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002302 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2303 aom_highbd_10_obmc_variance64x64,
2304 aom_highbd_10_obmc_sub_pixel_variance64x64)
2305 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2306 aom_highbd_10_obmc_variance64x32,
2307 aom_highbd_10_obmc_sub_pixel_variance64x32)
2308 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2309 aom_highbd_10_obmc_variance32x64,
2310 aom_highbd_10_obmc_sub_pixel_variance32x64)
2311 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2312 aom_highbd_10_obmc_variance32x32,
2313 aom_highbd_10_obmc_sub_pixel_variance32x32)
2314 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2315 aom_highbd_10_obmc_variance32x16,
2316 aom_highbd_10_obmc_sub_pixel_variance32x16)
2317 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2318 aom_highbd_10_obmc_variance16x32,
2319 aom_highbd_10_obmc_sub_pixel_variance16x32)
2320 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2321 aom_highbd_10_obmc_variance16x16,
2322 aom_highbd_10_obmc_sub_pixel_variance16x16)
2323 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2324 aom_highbd_10_obmc_variance8x16,
2325 aom_highbd_10_obmc_sub_pixel_variance8x16)
2326 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2327 aom_highbd_10_obmc_variance16x8,
2328 aom_highbd_10_obmc_sub_pixel_variance16x8)
2329 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2330 aom_highbd_10_obmc_variance8x8,
2331 aom_highbd_10_obmc_sub_pixel_variance8x8)
2332 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2333 aom_highbd_10_obmc_variance4x8,
2334 aom_highbd_10_obmc_sub_pixel_variance4x8)
2335 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2336 aom_highbd_10_obmc_variance8x4,
2337 aom_highbd_10_obmc_sub_pixel_variance8x4)
2338 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2339 aom_highbd_10_obmc_variance4x4,
2340 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002341
Rupert Swarbrick72678572017-08-02 12:05:26 +01002342 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2343 aom_highbd_10_obmc_variance64x16,
2344 aom_highbd_10_obmc_sub_pixel_variance64x16)
2345
2346 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2347 aom_highbd_10_obmc_variance16x64,
2348 aom_highbd_10_obmc_sub_pixel_variance16x64)
2349
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002350 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2351 aom_highbd_10_obmc_variance32x8,
2352 aom_highbd_10_obmc_sub_pixel_variance32x8)
2353
2354 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2355 aom_highbd_10_obmc_variance8x32,
2356 aom_highbd_10_obmc_sub_pixel_variance8x32)
2357
2358 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2359 aom_highbd_10_obmc_variance16x4,
2360 aom_highbd_10_obmc_sub_pixel_variance16x4)
2361
2362 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2363 aom_highbd_10_obmc_variance4x16,
2364 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002365 break;
2366
Yaowu Xuf883b422016-08-30 14:01:10 -07002367 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002368 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2369 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2370 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002371 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002372 aom_highbd_sad64x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002373 aom_highbd_dist_wtd_sad64x16_avg_bits12,
2374 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002375
2376 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2377 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2378 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002379 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002380 aom_highbd_sad16x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002381 aom_highbd_dist_wtd_sad16x64_avg_bits12,
2382 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002383
2384 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2385 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2386 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002387 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002388 aom_highbd_sad32x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002389 aom_highbd_dist_wtd_sad32x8_avg_bits12,
2390 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002391
2392 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2393 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2394 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002395 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002396 aom_highbd_sad8x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002397 aom_highbd_dist_wtd_sad8x32_avg_bits12,
2398 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002399
2400 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2401 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2402 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002403 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002404 aom_highbd_sad16x4x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002405 aom_highbd_dist_wtd_sad16x4_avg_bits12,
2406 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002407
2408 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2409 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2410 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002411 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002412 aom_highbd_sad4x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002413 aom_highbd_dist_wtd_sad4x16_avg_bits12,
2414 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002415
2416 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2417 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2418 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002419 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002420 aom_highbd_sad32x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002421 aom_highbd_dist_wtd_sad32x16_avg_bits12,
2422 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002423
2424 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2425 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2426 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002427 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002428 aom_highbd_sad16x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002429 aom_highbd_dist_wtd_sad16x32_avg_bits12,
2430 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002431
2432 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2433 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2434 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002435 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002436 aom_highbd_sad64x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002437 aom_highbd_dist_wtd_sad64x32_avg_bits12,
2438 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002439
2440 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2441 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2442 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002443 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002444 aom_highbd_sad32x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002445 aom_highbd_dist_wtd_sad32x64_avg_bits12,
2446 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002447
2448 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2449 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2450 aom_highbd_12_sub_pixel_variance32x32,
2451 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002452 aom_highbd_sad32x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002453 aom_highbd_dist_wtd_sad32x32_avg_bits12,
2454 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002455
2456 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2457 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2458 aom_highbd_12_sub_pixel_variance64x64,
2459 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002460 aom_highbd_sad64x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002461 aom_highbd_dist_wtd_sad64x64_avg_bits12,
2462 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002463
2464 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2465 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2466 aom_highbd_12_sub_pixel_variance16x16,
2467 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002468 aom_highbd_sad16x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002469 aom_highbd_dist_wtd_sad16x16_avg_bits12,
2470 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002471
2472 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2473 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2474 aom_highbd_12_sub_pixel_variance16x8,
2475 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002476 aom_highbd_sad16x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002477 aom_highbd_dist_wtd_sad16x8_avg_bits12,
2478 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002479
2480 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2481 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2482 aom_highbd_12_sub_pixel_variance8x16,
2483 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002484 aom_highbd_sad8x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002485 aom_highbd_dist_wtd_sad8x16_avg_bits12,
2486 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002487
2488 HIGHBD_BFP(
2489 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2490 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002491 aom_highbd_12_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002492 aom_highbd_sad8x8x4d_bits12, aom_highbd_dist_wtd_sad8x8_avg_bits12,
2493 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002494
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002495 HIGHBD_BFP(
2496 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2497 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2498 aom_highbd_12_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002499 aom_highbd_sad8x4x4d_bits12, aom_highbd_dist_wtd_sad8x4_avg_bits12,
2500 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002501
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002502 HIGHBD_BFP(
2503 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2504 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2505 aom_highbd_12_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002506 aom_highbd_sad4x8x4d_bits12, aom_highbd_dist_wtd_sad4x8_avg_bits12,
2507 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002508
2509 HIGHBD_BFP(
2510 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2511 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002512 aom_highbd_12_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002513 aom_highbd_sad4x4x4d_bits12, aom_highbd_dist_wtd_sad4x4_avg_bits12,
2514 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002515
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002516 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2517 aom_highbd_sad128x128_avg_bits12,
2518 aom_highbd_12_variance128x128,
2519 aom_highbd_12_sub_pixel_variance128x128,
2520 aom_highbd_12_sub_pixel_avg_variance128x128,
2521 aom_highbd_sad128x128x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002522 aom_highbd_dist_wtd_sad128x128_avg_bits12,
2523 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002524
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002525 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2526 aom_highbd_sad128x64_avg_bits12,
2527 aom_highbd_12_variance128x64,
2528 aom_highbd_12_sub_pixel_variance128x64,
2529 aom_highbd_12_sub_pixel_avg_variance128x64,
2530 aom_highbd_sad128x64x4d_bits12,
2531 aom_highbd_dist_wtd_sad128x64_avg_bits12,
2532 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002533
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002534 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2535 aom_highbd_sad64x128_avg_bits12,
2536 aom_highbd_12_variance64x128,
2537 aom_highbd_12_sub_pixel_variance64x128,
2538 aom_highbd_12_sub_pixel_avg_variance64x128,
2539 aom_highbd_sad64x128x4d_bits12,
2540 aom_highbd_dist_wtd_sad64x128_avg_bits12,
2541 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002542
David Barkerf19f35f2017-05-22 16:33:22 +01002543 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2544 aom_highbd_12_masked_sub_pixel_variance128x128)
2545 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2546 aom_highbd_12_masked_sub_pixel_variance128x64)
2547 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2548 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002549 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2550 aom_highbd_12_masked_sub_pixel_variance64x64)
2551 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2552 aom_highbd_12_masked_sub_pixel_variance64x32)
2553 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2554 aom_highbd_12_masked_sub_pixel_variance32x64)
2555 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2556 aom_highbd_12_masked_sub_pixel_variance32x32)
2557 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2558 aom_highbd_12_masked_sub_pixel_variance32x16)
2559 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2560 aom_highbd_12_masked_sub_pixel_variance16x32)
2561 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2562 aom_highbd_12_masked_sub_pixel_variance16x16)
2563 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2564 aom_highbd_12_masked_sub_pixel_variance8x16)
2565 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2566 aom_highbd_12_masked_sub_pixel_variance16x8)
2567 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2568 aom_highbd_12_masked_sub_pixel_variance8x8)
2569 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2570 aom_highbd_12_masked_sub_pixel_variance4x8)
2571 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2572 aom_highbd_12_masked_sub_pixel_variance8x4)
2573 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2574 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002575 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2576 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002577 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2578 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002579 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2580 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002581 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2582 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002583 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2584 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002585 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2586 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002587 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2588 aom_highbd_12_obmc_variance128x128,
2589 aom_highbd_12_obmc_sub_pixel_variance128x128)
2590 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2591 aom_highbd_12_obmc_variance128x64,
2592 aom_highbd_12_obmc_sub_pixel_variance128x64)
2593 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2594 aom_highbd_12_obmc_variance64x128,
2595 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002596 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2597 aom_highbd_12_obmc_variance64x64,
2598 aom_highbd_12_obmc_sub_pixel_variance64x64)
2599 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2600 aom_highbd_12_obmc_variance64x32,
2601 aom_highbd_12_obmc_sub_pixel_variance64x32)
2602 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2603 aom_highbd_12_obmc_variance32x64,
2604 aom_highbd_12_obmc_sub_pixel_variance32x64)
2605 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2606 aom_highbd_12_obmc_variance32x32,
2607 aom_highbd_12_obmc_sub_pixel_variance32x32)
2608 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2609 aom_highbd_12_obmc_variance32x16,
2610 aom_highbd_12_obmc_sub_pixel_variance32x16)
2611 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2612 aom_highbd_12_obmc_variance16x32,
2613 aom_highbd_12_obmc_sub_pixel_variance16x32)
2614 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2615 aom_highbd_12_obmc_variance16x16,
2616 aom_highbd_12_obmc_sub_pixel_variance16x16)
2617 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2618 aom_highbd_12_obmc_variance8x16,
2619 aom_highbd_12_obmc_sub_pixel_variance8x16)
2620 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2621 aom_highbd_12_obmc_variance16x8,
2622 aom_highbd_12_obmc_sub_pixel_variance16x8)
2623 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2624 aom_highbd_12_obmc_variance8x8,
2625 aom_highbd_12_obmc_sub_pixel_variance8x8)
2626 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2627 aom_highbd_12_obmc_variance4x8,
2628 aom_highbd_12_obmc_sub_pixel_variance4x8)
2629 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2630 aom_highbd_12_obmc_variance8x4,
2631 aom_highbd_12_obmc_sub_pixel_variance8x4)
2632 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2633 aom_highbd_12_obmc_variance4x4,
2634 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002635 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2636 aom_highbd_12_obmc_variance64x16,
2637 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002638 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2639 aom_highbd_12_obmc_variance16x64,
2640 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002641 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2642 aom_highbd_12_obmc_variance32x8,
2643 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002644 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2645 aom_highbd_12_obmc_variance8x32,
2646 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002647 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2648 aom_highbd_12_obmc_variance16x4,
2649 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002650 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2651 aom_highbd_12_obmc_variance4x16,
2652 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002653 break;
2654
2655 default:
2656 assert(0 &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002657 "cm->seq_params.bit_depth should be AOM_BITS_8, "
Yaowu Xuf883b422016-08-30 14:01:10 -07002658 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002659 }
2660 }
2661}
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002662#endif // CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002663
Yaowu Xuf883b422016-08-30 14:01:10 -07002664static void realloc_segmentation_maps(AV1_COMP *cpi) {
2665 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002666 CommonModeInfoParams *const mi_params = &cm->mi_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002667
2668 // Create the encoder segmentation map and set all entries to 0
Vishesh8c90fff2020-04-13 11:32:56 +05302669 aom_free(cpi->enc_seg.map);
2670 CHECK_MEM_ERROR(cm, cpi->enc_seg.map,
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002671 aom_calloc(mi_params->mi_rows * mi_params->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002672
2673 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002674 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002675 CHECK_MEM_ERROR(
2676 cm, cpi->cyclic_refresh,
2677 av1_cyclic_refresh_alloc(mi_params->mi_rows, mi_params->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002678
2679 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002680 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002681 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002682 aom_calloc(mi_params->mi_rows * mi_params->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002683}
2684
Vishesha9349502020-04-01 01:54:30 +05302685static AOM_INLINE void set_tpl_stats_block_size(int width, int height,
2686 uint8_t *block_mis_log2) {
Vishesh39d03622020-03-31 15:18:16 +05302687 const int is_720p_or_larger = AOMMIN(width, height) >= 720;
Yue Chenb4c93f02019-08-05 13:47:31 -07002688
2689 // 0: 4x4, 1: 8x8, 2: 16x16
Vishesha9349502020-04-01 01:54:30 +05302690 *block_mis_log2 = is_720p_or_larger ? 2 : 1;
Yue Chenb4c93f02019-08-05 13:47:31 -07002691}
2692
Hui Su38711e72019-06-11 10:49:47 -07002693void av1_alloc_compound_type_rd_buffers(AV1_COMMON *const cm,
2694 CompoundTypeRdBuffers *const bufs) {
2695 CHECK_MEM_ERROR(
2696 cm, bufs->pred0,
2697 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred0)));
2698 CHECK_MEM_ERROR(
2699 cm, bufs->pred1,
2700 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred1)));
2701 CHECK_MEM_ERROR(
2702 cm, bufs->residual1,
2703 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->residual1)));
2704 CHECK_MEM_ERROR(
2705 cm, bufs->diff10,
2706 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->diff10)));
2707 CHECK_MEM_ERROR(cm, bufs->tmp_best_mask_buf,
2708 (uint8_t *)aom_malloc(2 * MAX_SB_SQUARE *
2709 sizeof(*bufs->tmp_best_mask_buf)));
2710}
2711
2712void av1_release_compound_type_rd_buffers(CompoundTypeRdBuffers *const bufs) {
2713 aom_free(bufs->pred0);
2714 aom_free(bufs->pred1);
2715 aom_free(bufs->residual1);
2716 aom_free(bufs->diff10);
2717 aom_free(bufs->tmp_best_mask_buf);
2718 av1_zero(*bufs); // Set all pointers to NULL for safety.
2719}
2720
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002721static void config_target_level(AV1_COMP *const cpi, AV1_LEVEL target_level,
2722 int tier) {
2723 aom_clear_system_state();
2724
2725 AV1EncoderConfig *const oxcf = &cpi->oxcf;
2726 SequenceHeader *const seq_params = &cpi->common.seq_params;
2727
2728 // Adjust target bitrate to be no larger than 70% of level limit.
2729 const BITSTREAM_PROFILE profile = seq_params->profile;
2730 const double level_bitrate_limit =
2731 av1_get_max_bitrate_for_level(target_level, tier, profile);
2732 const int64_t max_bitrate = (int64_t)(level_bitrate_limit * 0.70);
2733 oxcf->target_bandwidth = AOMMIN(oxcf->target_bandwidth, max_bitrate);
2734 // Also need to update cpi->twopass.bits_left.
2735 TWO_PASS *const twopass = &cpi->twopass;
Aasaipriyaeb417c12020-04-07 12:08:24 +05302736 FIRSTPASS_STATS *stats = twopass->stats_buf_ctx->total_stats;
Mufaddal Chakera74c9cbe2020-01-17 16:44:59 +05302737 if (stats != NULL)
2738 cpi->twopass.bits_left =
2739 (int64_t)(stats->duration * cpi->oxcf.target_bandwidth / 10000000.0);
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002740
2741 // Adjust max over-shoot percentage.
2742 oxcf->over_shoot_pct = 0;
2743
2744 // Adjust max quantizer.
2745 oxcf->worst_allowed_q = 255;
2746
2747 // Adjust number of tiles and tile columns to be under level limit.
2748 int max_tiles, max_tile_cols;
2749 av1_get_max_tiles_for_level(target_level, &max_tiles, &max_tile_cols);
2750 while (oxcf->tile_columns > 0 && (1 << oxcf->tile_columns) > max_tile_cols) {
2751 --oxcf->tile_columns;
2752 }
2753 const int tile_cols = (1 << oxcf->tile_columns);
2754 while (oxcf->tile_rows > 0 &&
2755 tile_cols * (1 << oxcf->tile_rows) > max_tiles) {
2756 --oxcf->tile_rows;
2757 }
2758
2759 // Adjust min compression ratio.
2760 const int still_picture = seq_params->still_picture;
2761 const double min_cr =
2762 av1_get_min_cr_for_level(target_level, tier, still_picture);
2763 oxcf->min_cr = AOMMAX(oxcf->min_cr, (unsigned int)(min_cr * 100));
2764}
2765
Yaowu Xuf883b422016-08-30 14:01:10 -07002766void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2767 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002768 SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002769 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002770 MACROBLOCK *const x = &cpi->td.mb;
Vishesh8ac928b2020-04-01 02:36:35 +05302771 AV1LevelParams *const level_params = &cpi->level_params;
Jayasanker J44fdab72020-04-13 20:34:38 +05302772 InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
Jayasanker J24cb9bc2020-04-15 13:43:10 +05302773 RefreshFrameFlagsInfo *const refresh_frame_flags = &cpi->refresh_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002774
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002775 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2776 seq_params->bit_depth = oxcf->bit_depth;
2777 seq_params->color_primaries = oxcf->color_primaries;
2778 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2779 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2780 seq_params->monochrome = oxcf->monochrome;
2781 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2782 seq_params->color_range = oxcf->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002783
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002784 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2785 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002786
Urvang Joshi450a9a22020-03-31 16:00:22 -07002787 seq_params->timing_info_present = oxcf->timing_info_present;
2788 seq_params->timing_info.num_units_in_display_tick =
Andrey Norkin795ba872018-03-06 13:24:14 -08002789 oxcf->timing_info.num_units_in_display_tick;
Urvang Joshi450a9a22020-03-31 16:00:22 -07002790 seq_params->timing_info.time_scale = oxcf->timing_info.time_scale;
2791 seq_params->timing_info.equal_picture_interval =
Andrey Norkin795ba872018-03-06 13:24:14 -08002792 oxcf->timing_info.equal_picture_interval;
Urvang Joshi450a9a22020-03-31 16:00:22 -07002793 seq_params->timing_info.num_ticks_per_picture =
Andrey Norkin795ba872018-03-06 13:24:14 -08002794 oxcf->timing_info.num_ticks_per_picture;
2795
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002796 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07002797 oxcf->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002798 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002799 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08002800 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002801 // set the decoder model parameters in schedule mode
Urvang Joshi450a9a22020-03-31 16:00:22 -07002802 seq_params->decoder_model_info.num_units_in_decoding_tick =
Andrey Norkin795ba872018-03-06 13:24:14 -08002803 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07002804 cm->buffer_removal_time_present = 1;
Urvang Joshi450a9a22020-03-31 16:00:22 -07002805 av1_set_aom_dec_model_info(&seq_params->decoder_model_info);
2806 av1_set_dec_model_op_parameters(&seq_params->op_params[0]);
2807 } else if (seq_params->timing_info_present &&
2808 seq_params->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002809 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002810 // set the decoder model parameters in resource availability mode
Urvang Joshi450a9a22020-03-31 16:00:22 -07002811 av1_set_resource_availability_parameters(&seq_params->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07002812 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -07002813 seq_params->op_params[0].initial_display_delay =
Andrey Norkinc7511de2018-06-22 12:31:06 -07002814 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08002815 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002816
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002817 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002818
Yaowu Xuc27fc142016-08-22 16:08:15 -07002819 cpi->oxcf = *oxcf;
Urvang Joshif9c2bd72020-04-07 15:11:51 -07002820 cpi->superres_mode = oxcf->superres_mode; // default
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002821 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002822 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002823
Vishesh8ac928b2020-04-01 02:36:35 +05302824 memcpy(level_params->target_seq_level_idx, cpi->oxcf.target_seq_level_idx,
2825 sizeof(level_params->target_seq_level_idx));
2826 level_params->keep_level_stats = 0;
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002827 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Vishesh8ac928b2020-04-01 02:36:35 +05302828 if (level_params->target_seq_level_idx[i] <= SEQ_LEVELS) {
2829 level_params->keep_level_stats |= 1u << i;
2830 if (!level_params->level_info[i]) {
2831 CHECK_MEM_ERROR(cm, level_params->level_info[i],
2832 aom_calloc(1, sizeof(*level_params->level_info[i])));
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002833 }
2834 }
2835 }
2836
2837 // TODO(huisu@): level targeting currently only works for the 0th operating
2838 // point, so scalable coding is not supported yet.
Vishesh8ac928b2020-04-01 02:36:35 +05302839 if (level_params->target_seq_level_idx[0] < SEQ_LEVELS) {
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002840 // Adjust encoder config in order to meet target level.
Vishesh8ac928b2020-04-01 02:36:35 +05302841 config_target_level(cpi, level_params->target_seq_level_idx[0],
2842 seq_params->tier[0]);
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002843 }
2844
Mufaddal Chakera3bcc72c2019-12-11 14:38:37 +05302845 if ((has_no_stats_stage(cpi)) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002846 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2847 } else {
2848 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2849 }
2850
Jayasanker J24cb9bc2020-04-15 13:43:10 +05302851 refresh_frame_flags->golden_frame = false;
2852 refresh_frame_flags->bwd_ref_frame = false;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002853
Urvang Joshi6237b882020-03-26 15:02:26 -07002854 cm->features.refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2855 ? REFRESH_FRAME_CONTEXT_DISABLED
2856 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002857 if (oxcf->large_scale_tile)
Urvang Joshi6237b882020-03-26 15:02:26 -07002858 cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002859
Alex Converse74ad0912017-07-18 10:22:58 -07002860 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002861 CHECK_MEM_ERROR(cm, x->palette_buffer,
2862 aom_memalign(16, sizeof(*x->palette_buffer)));
2863 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002864
Hui Su38711e72019-06-11 10:49:47 -07002865 if (x->comp_rd_buffer.pred0 == NULL) {
2866 av1_alloc_compound_type_rd_buffers(cm, &x->comp_rd_buffer);
2867 }
2868
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002869 if (x->tmp_conv_dst == NULL) {
2870 CHECK_MEM_ERROR(
2871 cm, x->tmp_conv_dst,
2872 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002873 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002874 }
2875 for (int i = 0; i < 2; ++i) {
2876 if (x->tmp_obmc_bufs[i] == NULL) {
2877 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
wenyao.liu22d8ab32018-10-16 09:11:29 +08002878 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002879 sizeof(*x->tmp_obmc_bufs[i])));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002880 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002881 }
2882 }
2883
Marco Paniconi177b8c32020-01-26 20:37:29 -08002884 av1_reset_segment_features(cm);
Fyodor Kyslov64aaa092020-01-24 11:44:24 -08002885
chiyotsaic666b1f2019-12-20 10:44:58 -08002886 av1_set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002887
Yaowu Xuc27fc142016-08-22 16:08:15 -07002888 set_rc_buffer_sizes(rc, &cpi->oxcf);
2889
2890 // Under a configuration change, where maximum_buffer_size may change,
2891 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002892 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2893 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002894
2895 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002896 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002897
2898 // Set absolute upper and lower quality limits
2899 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2900 rc->best_quality = cpi->oxcf.best_allowed_q;
2901
Urvang Joshi6237b882020-03-26 15:02:26 -07002902 cm->features.interp_filter =
2903 oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
2904 cm->features.switchable_motion_mode = 1;
Yue Chen5380cb52018-02-23 15:33:21 -08002905
Yaowu Xuc27fc142016-08-22 16:08:15 -07002906 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2907 cm->render_width = cpi->oxcf.render_width;
2908 cm->render_height = cpi->oxcf.render_height;
2909 } else {
2910 cm->render_width = cpi->oxcf.width;
2911 cm->render_height = cpi->oxcf.height;
2912 }
2913 cm->width = cpi->oxcf.width;
2914 cm->height = cpi->oxcf.height;
2915
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002916 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -08002917 // Superblock size should not be updated after the first key frame.
2918 if (!cpi->seq_params_locked) {
2919 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Hui Sud909c2c2019-03-08 11:51:14 -08002920 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
2921 seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
Urvang Joshie4530f82018-01-09 11:43:37 -08002922 }
Dominic Symes917d6c02017-10-11 18:00:52 +02002923
Jayasanker J44fdab72020-04-13 20:34:38 +05302924 if (initial_dimensions->width || sb_size != seq_params->sb_size) {
2925 if (cm->width > initial_dimensions->width ||
2926 cm->height > initial_dimensions->height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002927 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002928 av1_free_context_buffers(cm);
Yue Cheneb628982019-08-29 15:17:13 -07002929 av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf);
2930 av1_free_sms_tree(&cpi->td);
Cheng Chen46f30c72017-09-07 11:13:33 -07002931 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002932 realloc_segmentation_maps(cpi);
Jayasanker J44fdab72020-04-13 20:34:38 +05302933 initial_dimensions->width = initial_dimensions->height = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002934 }
2935 }
2936 update_frame_size(cpi);
2937
Yaowu Xuc27fc142016-08-22 16:08:15 -07002938 rc->is_src_frame_alt_ref = 0;
2939
Yaowu Xuc27fc142016-08-22 16:08:15 -07002940 set_tile_info(cpi);
2941
Marco Paniconid8574e32019-08-04 21:30:12 -07002942 if (!cpi->svc.external_ref_frame_config)
Vishesh38c05d72020-04-14 12:19:14 +05302943 cpi->ext_flags.refresh_frame.update_pending = 0;
Vishesha195ca32020-04-07 18:46:20 +05302944 cpi->ext_flags.refresh_frame_context_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002945
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002946#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002947 highbd_set_var_fns(cpi);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002948#endif
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002949
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002950 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002951 // This should not be called after the first key frame.
2952 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002953 seq_params->operating_points_cnt_minus_1 =
Marco Paniconi6da983b2019-09-16 20:12:43 -07002954 (cm->number_spatial_layers > 1 || cm->number_temporal_layers > 1)
2955 ? cm->number_spatial_layers * cm->number_temporal_layers - 1
2956 : 0;
Marco Paniconi67142112019-07-24 15:00:31 -07002957 init_seq_coding_tools(&cm->seq_params, cm, oxcf, cpi->use_svc);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002958 }
Marco Paniconi5b1e4732019-08-08 18:57:53 -07002959
2960 if (cpi->use_svc)
2961 av1_update_layer_context_change_config(cpi, oxcf->target_bandwidth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002962}
2963
Vishesh39d03622020-03-31 15:18:16 +05302964static INLINE void setup_tpl_buffers(AV1_COMMON *const cm,
2965 TplParams *const tpl_data) {
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05302966 CommonModeInfoParams *const mi_params = &cm->mi_params;
Vishesh39d03622020-03-31 15:18:16 +05302967 set_tpl_stats_block_size(cm->width, cm->height,
2968 &tpl_data->tpl_stats_block_mis_log2);
Vishesha9349502020-04-01 01:54:30 +05302969 const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
2970
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05302971 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
2972 const int mi_cols =
2973 ALIGN_POWER_OF_TWO(mi_params->mi_cols, MAX_MIB_SIZE_LOG2);
2974 const int mi_rows =
2975 ALIGN_POWER_OF_TWO(mi_params->mi_rows, MAX_MIB_SIZE_LOG2);
2976
Vishesh39d03622020-03-31 15:18:16 +05302977 tpl_data->tpl_stats_buffer[frame].is_valid = 0;
Vishesha9349502020-04-01 01:54:30 +05302978 tpl_data->tpl_stats_buffer[frame].width = mi_cols >> block_mis_log2;
2979 tpl_data->tpl_stats_buffer[frame].height = mi_rows >> block_mis_log2;
Vishesh39d03622020-03-31 15:18:16 +05302980 tpl_data->tpl_stats_buffer[frame].stride =
2981 tpl_data->tpl_stats_buffer[frame].width;
2982 tpl_data->tpl_stats_buffer[frame].mi_rows = mi_params->mi_rows;
2983 tpl_data->tpl_stats_buffer[frame].mi_cols = mi_params->mi_cols;
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05302984 }
2985
2986 for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
2987 CHECK_MEM_ERROR(
Vishesh39d03622020-03-31 15:18:16 +05302988 cm, tpl_data->tpl_stats_pool[frame],
2989 aom_calloc(tpl_data->tpl_stats_buffer[frame].width *
2990 tpl_data->tpl_stats_buffer[frame].height,
2991 sizeof(*tpl_data->tpl_stats_buffer[frame].tpl_stats_ptr)));
Urvang Joshi6237b882020-03-26 15:02:26 -07002992 if (aom_alloc_frame_buffer(
Vishesh39d03622020-03-31 15:18:16 +05302993 &tpl_data->tpl_rec_pool[frame], cm->width, cm->height,
Urvang Joshi6237b882020-03-26 15:02:26 -07002994 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
2995 cm->seq_params.use_highbitdepth, AOM_ENC_NO_SCALE_BORDER,
2996 cm->features.byte_alignment))
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05302997 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
2998 "Failed to allocate frame buffer");
2999 }
3000
Vishesh39d03622020-03-31 15:18:16 +05303001 tpl_data->tpl_frame = &tpl_data->tpl_stats_buffer[REF_FRAMES + 1];
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05303002}
3003
Urvang Joshi31a7aa52020-03-31 13:09:13 -07003004static INLINE void init_frame_info(FRAME_INFO *frame_info,
3005 const AV1_COMMON *const cm) {
3006 const CommonModeInfoParams *const mi_params = &cm->mi_params;
3007 const SequenceHeader *const seq_params = &cm->seq_params;
3008 frame_info->frame_width = cm->width;
3009 frame_info->frame_height = cm->height;
3010 frame_info->mi_cols = mi_params->mi_cols;
3011 frame_info->mi_rows = mi_params->mi_rows;
3012 frame_info->mb_cols = mi_params->mb_cols;
3013 frame_info->mb_rows = mi_params->mb_rows;
3014 frame_info->num_mbs = mi_params->MBs;
3015 frame_info->bit_depth = seq_params->bit_depth;
3016 frame_info->subsampling_x = seq_params->subsampling_x;
3017 frame_info->subsampling_y = seq_params->subsampling_y;
3018}
3019
Aasaipriyabd659e42019-12-13 16:12:13 +05303020AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf, BufferPool *const pool,
Mufaddal Chakera5517b282019-12-13 16:39:56 +05303021 FIRSTPASS_STATS *frame_stats_buf,
Akshata Jadhava49be172019-12-18 00:03:53 +05303022 COMPRESSOR_STAGE stage, int num_lap_buffers,
Mufaddal Chakera412efe22020-01-17 14:35:01 +05303023 int lap_lag_in_frames,
Akshata Jadhava49be172019-12-18 00:03:53 +05303024 STATS_BUFFER_CTX *stats_buf_context) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003025 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
3026 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003027
3028 if (!cm) return NULL;
3029
Yaowu Xuf883b422016-08-30 14:01:10 -07003030 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003031
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07003032 // The jmp_buf is valid only for the duration of the function that calls
3033 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
3034 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003035 if (setjmp(cm->error.jmp)) {
3036 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003037 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003038 return 0;
3039 }
3040
3041 cm->error.setjmp = 1;
Mufaddal Chakeraed7b1622020-04-06 12:30:23 +05303042 cpi->lap_enabled = num_lap_buffers > 0;
3043 cpi->compressor_stage = stage;
chiyotsaia7091f12019-08-09 16:48:27 -07003044
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003045 CommonModeInfoParams *const mi_params = &cm->mi_params;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003046 mi_params->free_mi = enc_free_mi;
3047 mi_params->setup_mi = enc_setup_mi;
Mufaddal Chakeraed7b1622020-04-06 12:30:23 +05303048 mi_params->set_mb_mi = (oxcf->pass == 1 || cpi->compressor_stage == LAP_STAGE)
3049 ? stat_stage_set_mb_mi
3050 : enc_set_mb_mi;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003051
3052 mi_params->mi_alloc_bsize = BLOCK_4X4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003053
Angie Chianga5d96c42016-10-21 16:16:56 -07003054 CHECK_MEM_ERROR(cm, cm->fc,
3055 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +00003056 CHECK_MEM_ERROR(
3057 cm, cm->default_frame_context,
3058 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -07003059 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +00003060 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003061
Yaowu Xuc27fc142016-08-22 16:08:15 -07003062 cpi->common.buffer_pool = pool;
3063
Mufaddal Chakera7b5265f2020-03-27 10:32:34 +05303064 init_config(cpi, oxcf);
Mufaddal Chakera11f284a2019-12-19 11:38:46 +05303065 if (cpi->compressor_stage == LAP_STAGE) {
Mufaddal Chakera412efe22020-01-17 14:35:01 +05303066 cpi->oxcf.lag_in_frames = lap_lag_in_frames;
Mufaddal Chakera11f284a2019-12-19 11:38:46 +05303067 }
Mufaddal Chakeraac828682019-12-13 16:31:42 +05303068
Yaowu Xuf883b422016-08-30 14:01:10 -07003069 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003070
Aasaipriyad1ef4602020-03-16 20:13:35 +05303071 cpi->rc.enable_scenecut_detection = 1;
3072 if (cpi->lap_enabled &&
3073 (num_lap_buffers < (MAX_GF_LENGTH_LAP + SCENE_CUT_KEY_TEST_INTERVAL + 1)))
3074 cpi->rc.enable_scenecut_detection = 0;
Jingning Han17af7742019-09-17 16:58:03 -07003075 init_frame_info(&cpi->frame_info, cm);
3076
David Turnerd2a592e2018-11-16 14:59:31 +00003077 cm->current_frame.frame_number = 0;
David Turnera4c96252019-01-11 16:36:39 +00003078 cm->current_frame_id = -1;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07003079 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003080 cpi->partition_search_skippable_frame = 0;
3081 cpi->tile_data = NULL;
David Turnere7ebf902018-12-04 14:04:55 +00003082 cpi->last_show_frame_buf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003083 realloc_segmentation_maps(cpi);
3084
Jayasanker J24cb9bc2020-04-15 13:43:10 +05303085 cpi->refresh_frame.alt_ref_frame = false;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003086
3087 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
3088#if CONFIG_INTERNAL_STATS
3089 cpi->b_calculate_blockiness = 1;
3090 cpi->b_calculate_consistency = 1;
3091 cpi->total_inconsistency = 0;
3092 cpi->psnr.worst = 100.0;
3093 cpi->worst_ssim = 100.0;
3094
3095 cpi->count = 0;
3096 cpi->bytes = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003097#if CONFIG_SPEED_STATS
3098 cpi->tx_search_count = 0;
3099#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003100
3101 if (cpi->b_calculate_psnr) {
3102 cpi->total_sq_error = 0;
3103 cpi->total_samples = 0;
3104 cpi->tot_recode_hits = 0;
3105 cpi->summed_quality = 0;
3106 cpi->summed_weights = 0;
3107 }
3108
3109 cpi->fastssim.worst = 100.0;
3110 cpi->psnrhvs.worst = 100.0;
3111
3112 if (cpi->b_calculate_blockiness) {
3113 cpi->total_blockiness = 0;
3114 cpi->worst_blockiness = 0.0;
3115 }
3116
3117 if (cpi->b_calculate_consistency) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003118 CHECK_MEM_ERROR(
3119 cm, cpi->ssim_vars,
3120 aom_malloc(sizeof(*cpi->ssim_vars) * 4 * cpi->common.mi_params.mi_rows *
3121 cpi->common.mi_params.mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003122 cpi->worst_consistency = 100.0;
3123 }
3124#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003125#if CONFIG_ENTROPY_STATS
3126 av1_zero(aggregate_fc);
3127#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003128
Deepa K Gfb89ce02020-04-06 13:34:42 +05303129 cpi->time_stamps.first_ever = INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003130
Yaowu Xuc27fc142016-08-22 16:08:15 -07003131#ifdef OUTPUT_YUV_SKINMAP
3132 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
3133#endif
3134#ifdef OUTPUT_YUV_REC
3135 yuv_rec_file = fopen("rec.yuv", "wb");
3136#endif
3137
Mufaddal Chakera5517b282019-12-13 16:39:56 +05303138 assert(MAX_LAP_BUFFERS >= MAX_LAG_BUFFERS);
Akshata Jadhav4be65112019-12-18 00:26:25 +05303139 int size = get_stats_buf_size(num_lap_buffers, MAX_LAG_BUFFERS);
3140 for (int i = 0; i < size; i++)
Aasaipriyabd659e42019-12-13 16:12:13 +05303141 cpi->twopass.frame_stats_arr[i] = &frame_stats_buf[i];
3142
Akshata Jadhava49be172019-12-18 00:03:53 +05303143 cpi->twopass.stats_buf_ctx = stats_buf_context;
3144 cpi->twopass.stats_in = cpi->twopass.stats_buf_ctx->stats_in_start;
3145
Jerome Jiang2612b4d2019-05-29 17:46:47 -07003146#if !CONFIG_REALTIME_ONLY
Aasaipriyaeb417c12020-04-07 12:08:24 +05303147 if (is_stat_consumption_stage(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003148 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
3149 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
3150
Akshata Jadhav1fddf7f2019-12-18 00:49:25 +05303151 if (!cpi->lap_enabled) {
3152 /*Re-initialize to stats buffer, populated by application in the case of
3153 * two pass*/
3154 cpi->twopass.stats_buf_ctx->stats_in_start = oxcf->two_pass_stats_in.buf;
3155 cpi->twopass.stats_in = cpi->twopass.stats_buf_ctx->stats_in_start;
3156 cpi->twopass.stats_buf_ctx->stats_in_end =
3157 &cpi->twopass.stats_buf_ctx->stats_in_start[packets - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003158
Mufaddal Chakera74c9cbe2020-01-17 16:44:59 +05303159 av1_init_second_pass(cpi);
3160 } else {
3161 av1_init_single_pass_lap(cpi);
3162 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003163 }
Jerome Jiang2612b4d2019-05-29 17:46:47 -07003164#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003165
Remya0cce44c2019-08-16 11:57:24 +05303166 int sb_mi_size = av1_get_sb_mi_size(cm);
3167
Jingning Hand064cf02017-06-01 10:00:39 -07003168 CHECK_MEM_ERROR(
3169 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08003170 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08003171 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07003172 CHECK_MEM_ERROR(
3173 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08003174 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08003175 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07003176
3177 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
3178 (int32_t *)aom_memalign(
3179 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
3180
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303181 CHECK_MEM_ERROR(
3182 cm, cpi->td.mb.inter_modes_info,
3183 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303184
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303185 for (int x = 0; x < 2; x++)
3186 for (int y = 0; y < 2; y++)
3187 CHECK_MEM_ERROR(
chiyotsai82f36c92020-04-09 16:18:02 -07003188 cm, cpi->td.mb.intrabc_hash_info.hash_value_buffer[x][y],
3189 (uint32_t *)aom_malloc(
3190 AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
3191 sizeof(*cpi->td.mb.intrabc_hash_info.hash_value_buffer[0][0])));
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303192
chiyotsai82f36c92020-04-09 16:18:02 -07003193 cpi->td.mb.intrabc_hash_info.g_crc_initialized = 0;
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303194
Jingning Hand064cf02017-06-01 10:00:39 -07003195 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
3196 (int32_t *)aom_memalign(
3197 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
3198
Remya0cce44c2019-08-16 11:57:24 +05303199 CHECK_MEM_ERROR(cm, cpi->td.mb.mbmi_ext,
3200 aom_calloc(sb_mi_size, sizeof(*cpi->td.mb.mbmi_ext)));
3201
David Turner04b70d82019-01-24 15:39:19 +00003202 av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
3203 av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003204
sdengc23c7f12019-06-11 16:56:50 -07003205 {
3206 const int bsize = BLOCK_16X16;
3207 const int w = mi_size_wide[bsize];
3208 const int h = mi_size_high[bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003209 const int num_cols = (mi_params->mi_cols + w - 1) / w;
3210 const int num_rows = (mi_params->mi_rows + h - 1) / h;
sdengf46a1062019-08-04 18:43:50 -07003211 CHECK_MEM_ERROR(cm, cpi->tpl_rdmult_scaling_factors,
3212 aom_calloc(num_rows * num_cols,
3213 sizeof(*cpi->tpl_rdmult_scaling_factors)));
3214 CHECK_MEM_ERROR(cm, cpi->tpl_sb_rdmult_scaling_factors,
3215 aom_calloc(num_rows * num_cols,
3216 sizeof(*cpi->tpl_sb_rdmult_scaling_factors)));
3217 }
3218
3219 {
3220 const int bsize = BLOCK_16X16;
3221 const int w = mi_size_wide[bsize];
3222 const int h = mi_size_high[bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003223 const int num_cols = (mi_params->mi_cols + w - 1) / w;
3224 const int num_rows = (mi_params->mi_rows + h - 1) / h;
sdengc23c7f12019-06-11 16:56:50 -07003225 CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors,
3226 aom_calloc(num_rows * num_cols,
3227 sizeof(*cpi->ssim_rdmult_scaling_factors)));
3228 }
3229
sdeng01959162019-12-20 10:46:24 -08003230#if CONFIG_TUNE_VMAF
3231 {
3232 const int bsize = BLOCK_64X64;
3233 const int w = mi_size_wide[bsize];
3234 const int h = mi_size_high[bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003235 const int num_cols = (mi_params->mi_cols + w - 1) / w;
3236 const int num_rows = (mi_params->mi_rows + h - 1) / h;
Visheshf18766f2020-04-17 10:22:48 +05303237 CHECK_MEM_ERROR(cm, cpi->vmaf_info.rdmult_scaling_factors,
sdeng01959162019-12-20 10:46:24 -08003238 aom_calloc(num_rows * num_cols,
Visheshf18766f2020-04-17 10:22:48 +05303239 sizeof(*cpi->vmaf_info.rdmult_scaling_factors)));
3240 cpi->vmaf_info.last_frame_unsharp_amount = 0.0;
sdeng01959162019-12-20 10:46:24 -08003241 }
3242#endif
3243
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05303244 if (!is_stat_generation_stage(cpi)) {
Vishesh39d03622020-03-31 15:18:16 +05303245 setup_tpl_buffers(cm, &cpi->tpl_data);
Jingning Hanf83d6812020-02-27 13:08:19 -08003246 }
Yue Chen7cae98f2018-08-24 10:43:16 -07003247
chiyotsai9c484b32019-03-07 16:01:50 -08003248#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08003249 av1_zero(cpi->partition_stats);
3250#endif
3251
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003252#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
3253 cpi->fn_ptr[BT].sdf = SDF; \
3254 cpi->fn_ptr[BT].sdaf = SDAF; \
3255 cpi->fn_ptr[BT].vf = VF; \
3256 cpi->fn_ptr[BT].svf = SVF; \
3257 cpi->fn_ptr[BT].svaf = SVAF; \
3258 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
3259 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07003260 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003261
Cheng Chenf78632e2017-10-20 15:30:51 -07003262 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003263 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003264 aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
3265 aom_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003266
3267 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003268 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003269 aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
3270 aom_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003271
3272 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003273 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003274 aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
3275 aom_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003276
3277 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003278 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003279 aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
3280 aom_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003281
3282 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003283 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003284 aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
3285 aom_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003286
3287 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003288 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003289 aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
3290 aom_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003291
Cheng Chenf78632e2017-10-20 15:30:51 -07003292 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
3293 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003294 aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
3295 aom_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07003296
3297 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003298 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003299 aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
3300 aom_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003301
3302 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003303 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003304 aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
3305 aom_dist_wtd_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07003306
3307 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003308 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003309 aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
3310 aom_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003311
3312 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003313 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003314 aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
3315 aom_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003316
3317 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003318 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003319 aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
3320 aom_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003321
3322 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003323 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003324 aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
3325 aom_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003326
3327 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
3328 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003329 aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
3330 aom_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003331
3332 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
3333 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003334 aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
3335 aom_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003336
3337 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
3338 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003339 aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
3340 aom_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003341
3342 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003343 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003344 aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
3345 aom_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003346
3347 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003348 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003349 aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
3350 aom_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003351
3352 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003353 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003354 aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003355
3356 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003357 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003358 aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003359
3360 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003361 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003362 aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003363
3364 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003365 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003366 aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003367
Yaowu Xuc27fc142016-08-22 16:08:15 -07003368#define OBFP(BT, OSDF, OVF, OSVF) \
3369 cpi->fn_ptr[BT].osdf = OSDF; \
3370 cpi->fn_ptr[BT].ovf = OVF; \
3371 cpi->fn_ptr[BT].osvf = OSVF;
3372
Yaowu Xuf883b422016-08-30 14:01:10 -07003373 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
3374 aom_obmc_sub_pixel_variance128x128)
3375 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
3376 aom_obmc_sub_pixel_variance128x64)
3377 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
3378 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07003379 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
3380 aom_obmc_sub_pixel_variance64x64)
3381 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
3382 aom_obmc_sub_pixel_variance64x32)
3383 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
3384 aom_obmc_sub_pixel_variance32x64)
3385 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
3386 aom_obmc_sub_pixel_variance32x32)
3387 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
3388 aom_obmc_sub_pixel_variance32x16)
3389 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
3390 aom_obmc_sub_pixel_variance16x32)
3391 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
3392 aom_obmc_sub_pixel_variance16x16)
3393 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
3394 aom_obmc_sub_pixel_variance16x8)
3395 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
3396 aom_obmc_sub_pixel_variance8x16)
3397 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
3398 aom_obmc_sub_pixel_variance8x8)
3399 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
3400 aom_obmc_sub_pixel_variance4x8)
3401 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
3402 aom_obmc_sub_pixel_variance8x4)
3403 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
3404 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003405 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
3406 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003407 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
3408 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003409 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
3410 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003411 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
3412 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003413 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
3414 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003415 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
3416 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003417
David Barkerf19f35f2017-05-22 16:33:22 +01003418#define MBFP(BT, MCSDF, MCSVF) \
3419 cpi->fn_ptr[BT].msdf = MCSDF; \
3420 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003421
David Barkerf19f35f2017-05-22 16:33:22 +01003422 MBFP(BLOCK_128X128, aom_masked_sad128x128,
3423 aom_masked_sub_pixel_variance128x128)
3424 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
3425 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01003426 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
3427 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
3428 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
3429 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
3430 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
3431 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
3432 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
3433 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
3434 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
3435 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
3436 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
3437 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
3438 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003439
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003440 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
3441
3442 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
3443
3444 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
3445
3446 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003447
3448 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
3449
3450 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01003451
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003452#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003453 highbd_set_var_fns(cpi);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003454#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003455
Yaowu Xuf883b422016-08-30 14:01:10 -07003456 /* av1_init_quantizer() is first called here. Add check in
3457 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07003458 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07003459 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003460 */
Deepa K Gc0d27832020-04-07 16:52:27 +05303461 av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
3462 cm->seq_params.bit_depth);
Urvang Joshi17814622020-03-27 17:26:17 -07003463 av1_qm_init(&cm->quant_params, av1_num_planes(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003464
Yaowu Xuf883b422016-08-30 14:01:10 -07003465 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07003466 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003467 cm->superres_upscaled_width = oxcf->width;
3468 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07003469 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003470
3471 cm->error.setjmp = 0;
3472
3473 return cpi;
3474}
3475
Urvang Joshiee2c8112018-05-04 14:53:15 -07003476#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003477#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
3478
3479#define SNPRINT2(H, T, V) \
3480 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07003481#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003482
Yaowu Xuf883b422016-08-30 14:01:10 -07003483void av1_remove_compressor(AV1_COMP *cpi) {
3484 AV1_COMMON *cm;
Vishesh39d03622020-03-31 15:18:16 +05303485 TplParams *const tpl_data = &cpi->tpl_data;
Deepa K G74de2a02020-04-11 13:09:11 +05303486 MultiThreadInfo *const mt_info = &cpi->mt_info;
3487#if CONFIG_MULTITHREAD
3488 pthread_mutex_t *const enc_row_mt_mutex_ = mt_info->enc_row_mt.mutex_;
3489#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003490 int t;
3491
3492 if (!cpi) return;
3493
3494 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003495
David Turnerd2a592e2018-11-16 14:59:31 +00003496 if (cm->current_frame.frame_number > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003497#if CONFIG_ENTROPY_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303498 if (!is_stat_generation_stage(cpi)) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003499 fprintf(stderr, "Writing counts.stt\n");
3500 FILE *f = fopen("counts.stt", "wb");
3501 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
3502 fclose(f);
3503 }
3504#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003505#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003506 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003507
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303508 if (!is_stat_generation_stage(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003509 char headings[512] = { 0 };
3510 char results[512] = { 0 };
3511 FILE *f = fopen("opsnr.stt", "a");
3512 double time_encoded =
Deepa K Gfb89ce02020-04-06 13:34:42 +05303513 (cpi->time_stamps.prev_end_seen - cpi->time_stamps.first_ever) /
Yaowu Xuc27fc142016-08-22 16:08:15 -07003514 10000000.000;
3515 double total_encode_time =
3516 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3517 const double dr =
3518 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3519 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3520 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3521 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3522
3523 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003524 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003525 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3526 const double total_ssim =
3527 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3528 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08003529 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003530 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003531 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07003532 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003533 snprintf(results, sizeof(results),
3534 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3535 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003536 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07003537 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003538 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
3539 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
3540 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
3541 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003542 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003543 cpi->psnr.stat[STAT_Y] / cpi->count,
3544 cpi->psnr.stat[STAT_U] / cpi->count,
Yue Chenf0652ed2019-08-13 16:09:25 -07003545 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003546
3547 if (cpi->b_calculate_blockiness) {
3548 SNPRINT(headings, "\t Block\tWstBlck");
3549 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3550 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3551 }
3552
3553 if (cpi->b_calculate_consistency) {
3554 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003555 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003556 (double)cpi->total_inconsistency);
3557
3558 SNPRINT(headings, "\tConsist\tWstCons");
3559 SNPRINT2(results, "\t%7.3f", consistency);
3560 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3561 }
Yue Chenf0652ed2019-08-13 16:09:25 -07003562
3563 SNPRINT(headings, "\t Time\tRcErr\tAbsErr");
3564 SNPRINT2(results, "\t%8.0f", total_encode_time);
3565 SNPRINT2(results, "\t%7.2f", rate_err);
3566 SNPRINT2(results, "\t%7.2f", fabs(rate_err));
3567
3568 fprintf(f, "%s\tAPsnr611\n", headings);
3569 fprintf(f, "%s\t%7.3f\n", results,
3570 (6 * cpi->psnr.stat[STAT_Y] + cpi->psnr.stat[STAT_U] +
3571 cpi->psnr.stat[STAT_V]) /
3572 (cpi->count * 8));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003573 }
3574
3575 fclose(f);
3576 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07003577#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003578#if CONFIG_SPEED_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303579 if (!is_stat_generation_stage(cpi)) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003580 fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
3581 }
3582#endif // CONFIG_SPEED_STATS
chiyotsai92ed0dd2019-01-25 14:50:14 -08003583
chiyotsai9c484b32019-03-07 16:01:50 -08003584#if CONFIG_COLLECT_PARTITION_STATS == 2
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303585 if (!is_stat_generation_stage(cpi)) {
chiyotsai92ed0dd2019-01-25 14:50:14 -08003586 av1_print_partition_stats(&cpi->partition_stats);
3587 }
3588#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003589 }
3590
Jingning Hanf83d6812020-02-27 13:08:19 -08003591 for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
Vishesh39d03622020-03-31 15:18:16 +05303592 aom_free(tpl_data->tpl_stats_pool[frame]);
3593 aom_free_frame_buffer(&tpl_data->tpl_rec_pool[frame]);
Yue Chen7cae98f2018-08-24 10:43:16 -07003594 }
3595
Deepa K G74de2a02020-04-11 13:09:11 +05303596 for (t = mt_info->num_workers - 1; t >= 0; --t) {
3597 AVxWorker *const worker = &mt_info->workers[t];
3598 EncWorkerData *const thread_data = &mt_info->tile_thr_data[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003599
3600 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003601 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003602
3603 // Deallocate allocated thread data.
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303604 aom_free(thread_data->td->tctx);
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303605 if (t > 0) {
hui sud9a812b2017-07-06 14:34:37 -07003606 aom_free(thread_data->td->palette_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003607 aom_free(thread_data->td->tmp_conv_dst);
Hui Su38711e72019-06-11 10:49:47 -07003608 av1_release_compound_type_rd_buffers(&thread_data->td->comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003609 for (int j = 0; j < 2; ++j) {
3610 aom_free(thread_data->td->tmp_obmc_bufs[j]);
3611 }
Jingning Hand064cf02017-06-01 10:00:39 -07003612 aom_free(thread_data->td->above_pred_buf);
3613 aom_free(thread_data->td->left_pred_buf);
3614 aom_free(thread_data->td->wsrc_buf);
Fyodor Kyslov166648a2020-03-18 16:15:27 -07003615 aom_free(thread_data->td->vt64x64);
wenyao.liu22d8ab32018-10-16 09:11:29 +08003616
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303617 aom_free(thread_data->td->inter_modes_info);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003618 for (int x = 0; x < 2; x++) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303619 for (int y = 0; y < 2; y++) {
3620 aom_free(thread_data->td->hash_value_buffer[x][y]);
3621 thread_data->td->hash_value_buffer[x][y] = NULL;
3622 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003623 }
Jingning Hand064cf02017-06-01 10:00:39 -07003624 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003625 aom_free(thread_data->td->counts);
Remya0cce44c2019-08-16 11:57:24 +05303626 aom_free(thread_data->td->mbmi_ext);
Yue Cheneb628982019-08-29 15:17:13 -07003627 av1_free_shared_coeff_buffer(&thread_data->td->shared_coeff_buf);
3628 av1_free_sms_tree(thread_data->td);
Yaowu Xuf883b422016-08-30 14:01:10 -07003629 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003630 }
3631 }
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303632#if CONFIG_MULTITHREAD
Deepa K G74de2a02020-04-11 13:09:11 +05303633 if (enc_row_mt_mutex_ != NULL) {
3634 pthread_mutex_destroy(enc_row_mt_mutex_);
3635 aom_free(enc_row_mt_mutex_);
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303636 }
3637#endif
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05303638 av1_row_mt_mem_dealloc(cpi);
Deepa K G74de2a02020-04-11 13:09:11 +05303639 aom_free(mt_info->tile_thr_data);
3640 aom_free(mt_info->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003641
Deepa K G74de2a02020-04-11 13:09:11 +05303642 if (mt_info->num_workers > 1) {
3643 av1_loop_filter_dealloc(&mt_info->lf_row_sync);
3644 av1_loop_restoration_dealloc(&mt_info->lr_row_sync, mt_info->num_workers);
Deepa K G964e72e2018-05-16 16:56:01 +05303645 }
3646
Yaowu Xuc27fc142016-08-22 16:08:15 -07003647 dealloc_compressor_data(cpi);
3648
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003649#if CONFIG_INTERNAL_STATS
3650 aom_free(cpi->ssim_vars);
3651 cpi->ssim_vars = NULL;
3652#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003653
Yaowu Xuf883b422016-08-30 14:01:10 -07003654 av1_remove_common(cm);
Yaowu Xu74e63352019-05-06 09:21:33 -07003655#if CONFIG_HTB_TRELLIS
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08003656 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xu74e63352019-05-06 09:21:33 -07003657#endif // CONFIG_HTB_TRELLIS
Yaowu Xuf883b422016-08-30 14:01:10 -07003658 av1_free_ref_frame_buffers(cm->buffer_pool);
Mufaddal Chakera74c9cbe2020-01-17 16:44:59 +05303659
Yaowu Xuf883b422016-08-30 14:01:10 -07003660 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003661
3662#ifdef OUTPUT_YUV_SKINMAP
3663 fclose(yuv_skinmap_file);
3664#endif
3665#ifdef OUTPUT_YUV_REC
3666 fclose(yuv_rec_file);
3667#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003668}
3669
Yaowu Xuf883b422016-08-30 14:01:10 -07003670static void generate_psnr_packet(AV1_COMP *cpi) {
3671 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003672 int i;
3673 PSNR_STATS psnr;
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003674#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xue75b58a2020-01-03 12:56:12 -08003675 const uint32_t in_bit_depth = cpi->oxcf.input_bit_depth;
3676 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
3677 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
3678 bit_depth, in_bit_depth);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003679#else
3680 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
3681#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003682
3683 for (i = 0; i < 4; ++i) {
3684 pkt.data.psnr.samples[i] = psnr.samples[i];
3685 pkt.data.psnr.sse[i] = psnr.sse[i];
3686 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3687 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003688 pkt.kind = AOM_CODEC_PSNR_PKT;
3689 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003690}
3691
Vishesha195ca32020-04-07 18:46:20 +05303692int av1_use_as_reference(int *ext_ref_frame_flags, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003693 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3694
Vishesha195ca32020-04-07 18:46:20 +05303695 *ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003696 return 0;
3697}
3698
Thomas Daede497d1952017-08-08 17:33:06 -07003699int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3700 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003701 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003702 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003703 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003704 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003705 return 0;
3706 } else {
3707 return -1;
3708 }
3709}
3710
Thomas Daede497d1952017-08-08 17:33:06 -07003711int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3712 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003713 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003714 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003715 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003716 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003717 return 0;
3718 } else {
3719 return -1;
3720 }
3721}
3722
Vishesha195ca32020-04-07 18:46:20 +05303723int av1_update_entropy(bool *ext_refresh_frame_context,
3724 bool *ext_refresh_frame_context_pending, bool update) {
3725 *ext_refresh_frame_context = update;
3726 *ext_refresh_frame_context_pending = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003727 return 0;
3728}
3729
3730#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3731// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3732// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3733// not denoise the UV channels at this time. If ever we implement UV channel
3734// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003735void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003736 uint8_t *src = s->y_buffer;
3737 int h = s->y_height;
3738
3739 do {
3740 fwrite(src, s->y_width, 1, f);
3741 src += s->y_stride;
3742 } while (--h);
3743
3744 src = s->u_buffer;
3745 h = s->uv_height;
3746
3747 do {
3748 fwrite(src, s->uv_width, 1, f);
3749 src += s->uv_stride;
3750 } while (--h);
3751
3752 src = s->v_buffer;
3753 h = s->uv_height;
3754
3755 do {
3756 fwrite(src, s->uv_width, 1, f);
3757 src += s->uv_stride;
3758 } while (--h);
3759}
3760#endif
3761
Yaowu Xuc27fc142016-08-22 16:08:15 -07003762#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003763void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003764 uint8_t *src = s->y_buffer;
3765 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003766 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003767 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3768 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3769
3770 do {
3771 fwrite(src16, s->y_width, 2, yuv_rec_file);
3772 src16 += s->y_stride;
3773 } while (--h);
3774
3775 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3776 h = s->uv_height;
3777
3778 do {
3779 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3780 src16 += s->uv_stride;
3781 } while (--h);
3782
3783 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3784 h = s->uv_height;
3785
3786 do {
3787 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3788 src16 += s->uv_stride;
3789 } while (--h);
3790
3791 fflush(yuv_rec_file);
3792 return;
3793 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003794
3795 do {
3796 fwrite(src, s->y_width, 1, yuv_rec_file);
3797 src += s->y_stride;
3798 } while (--h);
3799
3800 src = s->u_buffer;
3801 h = s->uv_height;
3802
3803 do {
3804 fwrite(src, s->uv_width, 1, yuv_rec_file);
3805 src += s->uv_stride;
3806 } while (--h);
3807
3808 src = s->v_buffer;
3809 h = s->uv_height;
3810
3811 do {
3812 fwrite(src, s->uv_width, 1, yuv_rec_file);
3813 src += s->uv_stride;
3814 } while (--h);
3815
3816 fflush(yuv_rec_file);
3817}
3818#endif // OUTPUT_YUV_REC
3819
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003820#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Vishesh8ba8bfe2020-04-03 14:20:29 +05303821static int recode_loop_test_global_motion(
3822 WarpedMotionParams *const global_motion,
3823 const int *const global_motion_used, int *const gm_params_cost) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003824 int i;
3825 int recode = 0;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003826 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Vishesh8ba8bfe2020-04-03 14:20:29 +05303827 if (global_motion[i].wmtype != IDENTITY &&
3828 global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
3829 gm_params_cost[i]) {
3830 global_motion[i] = default_warp_params;
3831 assert(global_motion[i].wmtype == IDENTITY);
3832 gm_params_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003833 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003834 // TODO(sarahparker): The earlier condition for recoding here was:
3835 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3836 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003837 }
3838 }
3839 return recode;
3840}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003841
Yaowu Xuc27fc142016-08-22 16:08:15 -07003842// Function to test for conditions that indicate we should loop
3843// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003844static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3845 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003846 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003847 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003848 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3849 int force_recode = 0;
3850
3851 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
chiyotsai6b430132019-12-18 10:33:51 -08003852 (cpi->sf.hl_sf.recode_loop == ALLOW_RECODE) ||
3853 (frame_is_kfgfarf &&
3854 (cpi->sf.hl_sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003855 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3856 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3857 (rc->projected_frame_size < low_limit && q > minq)) {
3858 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003859 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003860 // Deal with frame undershoot and whether or not we are
3861 // below the automatically set cq level.
3862 if (q > oxcf->cq_level &&
3863 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3864 force_recode = 1;
3865 }
3866 }
3867 }
3868 return force_recode;
3869}
3870
Cheng Chen46f30c72017-09-07 11:13:33 -07003871static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003872 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003873 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003874 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003875
3876 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003877 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Jingning Han667561a2019-07-22 15:48:01 -07003878 if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003879 BufferPool *const pool = cm->buffer_pool;
3880 const YV12_BUFFER_CONFIG *const ref =
David Turnera21966b2018-12-05 14:48:49 +00003881 get_ref_frame_yv12_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003882
3883 if (ref == NULL) {
David Turnere7ebf902018-12-04 14:04:55 +00003884 cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003885 continue;
3886 }
3887
Yaowu Xuc27fc142016-08-22 16:08:15 -07003888 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
Debargha Mukherjee13cccf22019-03-27 23:49:14 -07003889 // Replace the reference buffer with a copy having a thicker border,
3890 // if the reference buffer is higher resolution than the current
3891 // frame, and the border is thin.
3892 if ((ref->y_crop_width > cm->width ||
3893 ref->y_crop_height > cm->height) &&
3894 ref->border < AOM_BORDER_IN_PIXELS) {
3895 RefCntBuffer *ref_fb = get_ref_frame_buf(cm, ref_frame);
3896 if (aom_yv12_realloc_with_new_border(
Urvang Joshi6237b882020-03-26 15:02:26 -07003897 &ref_fb->buf, AOM_BORDER_IN_PIXELS,
3898 cm->features.byte_alignment, num_planes) != 0) {
Debargha Mukherjee13cccf22019-03-27 23:49:14 -07003899 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3900 "Failed to allocate frame buffer");
3901 }
3902 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003903 int force_scaling = 0;
David Turnere7ebf902018-12-04 14:04:55 +00003904 RefCntBuffer *new_fb = cpi->scaled_ref_buf[ref_frame - 1];
3905 if (new_fb == NULL) {
3906 const int new_fb_idx = get_free_fb(cm);
3907 if (new_fb_idx == INVALID_IDX) {
Wan-Teh Chang4a8c0042018-10-05 09:41:52 -07003908 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3909 "Unable to find free frame buffer");
David Turnere7ebf902018-12-04 14:04:55 +00003910 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003911 force_scaling = 1;
David Turnere7ebf902018-12-04 14:04:55 +00003912 new_fb = &pool->frame_bufs[new_fb_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003913 }
David Turnere7ebf902018-12-04 14:04:55 +00003914
3915 if (force_scaling || new_fb->buf.y_crop_width != cm->width ||
3916 new_fb->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003917 if (aom_realloc_frame_buffer(
David Turnere7ebf902018-12-04 14:04:55 +00003918 &new_fb->buf, cm->width, cm->height,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003919 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
Debargha Mukherjeefa946af2019-03-26 16:58:55 -07003920 cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
Urvang Joshi6237b882020-03-26 15:02:26 -07003921 cm->features.byte_alignment, NULL, NULL, NULL)) {
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003922 if (force_scaling) {
3923 // Release the reference acquired in the get_free_fb() call above.
David Turnere7ebf902018-12-04 14:04:55 +00003924 --new_fb->ref_count;
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003925 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003926 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003927 "Failed to allocate frame buffer");
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003928 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003929 av1_resize_and_extend_frame(
David Turnere7ebf902018-12-04 14:04:55 +00003930 ref, &new_fb->buf, (int)cm->seq_params.bit_depth, num_planes);
3931 cpi->scaled_ref_buf[ref_frame - 1] = new_fb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003932 alloc_frame_mvs(cm, new_fb);
3933 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003934 } else {
David Turnera21966b2018-12-05 14:48:49 +00003935 RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003936 buf->buf.y_crop_width = ref->y_crop_width;
3937 buf->buf.y_crop_height = ref->y_crop_height;
David Turnere7ebf902018-12-04 14:04:55 +00003938 cpi->scaled_ref_buf[ref_frame - 1] = buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003939 ++buf->ref_count;
3940 }
3941 } else {
Mufaddal Chakera3bcc72c2019-12-11 14:38:37 +05303942 if (!has_no_stats_stage(cpi)) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003943 }
3944 }
3945}
3946
Yaowu Xuf883b422016-08-30 14:01:10 -07003947static void release_scaled_references(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003948 // TODO(isbs): only refresh the necessary frames, rather than all of them
David Turnere7ebf902018-12-04 14:04:55 +00003949 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3950 RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
3951 if (buf != NULL) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003952 --buf->ref_count;
David Turnere7ebf902018-12-04 14:04:55 +00003953 cpi->scaled_ref_buf[i] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003954 }
3955 }
3956}
3957
Yaowu Xuf883b422016-08-30 14:01:10 -07003958static void set_mv_search_params(AV1_COMP *cpi) {
3959 const AV1_COMMON *const cm = &cpi->common;
Nithya V Sace29f32020-04-07 16:15:12 +05303960 MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params;
Yunqing Wang4b7bf402020-01-28 15:20:53 -08003961 const int max_mv_def = AOMMAX(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003962
3963 // Default based on max resolution.
Nithya V Sace29f32020-04-07 16:15:12 +05303964 mv_search_params->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003965
chiyotsai8cc054a2019-12-12 14:57:43 -08003966 if (cpi->sf.mv_sf.auto_mv_step_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003967 if (frame_is_intra_only(cm)) {
3968 // Initialize max_mv_magnitude for use in the first INTER frame
3969 // after a key/intra-only frame.
Nithya V Sace29f32020-04-07 16:15:12 +05303970 mv_search_params->max_mv_magnitude = max_mv_def;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003971 } else {
Yunqing Wang4b7bf402020-01-28 15:20:53 -08003972 // Use cpi->max_mv_magnitude == -1 to exclude first pass case.
Nithya V Sace29f32020-04-07 16:15:12 +05303973 if (cm->show_frame && mv_search_params->max_mv_magnitude != -1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003974 // Allow mv_steps to correspond to twice the max mv magnitude found
3975 // in the previous frame, capped by the default max_mv_magnitude based
3976 // on resolution.
Nithya V Sace29f32020-04-07 16:15:12 +05303977 mv_search_params->mv_step_param = av1_init_search_range(
3978 AOMMIN(max_mv_def, 2 * mv_search_params->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003979 }
Nithya V Sace29f32020-04-07 16:15:12 +05303980 mv_search_params->max_mv_magnitude = -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003981 }
3982 }
3983}
3984
Yaowu Xubca61902020-04-08 08:41:47 -07003985void av1_set_screen_content_options(const AV1_COMP *cpi,
3986 FeatureFlags *features) {
Urvang Joshib6409e92020-03-23 11:23:27 -07003987 const AV1_COMMON *const cm = &cpi->common;
Hui Subdf0c992019-02-14 14:52:41 -08003988
3989 if (cm->seq_params.force_screen_content_tools != 2) {
Urvang Joshib6409e92020-03-23 11:23:27 -07003990 features->allow_screen_content_tools = features->allow_intrabc =
Hui Subdf0c992019-02-14 14:52:41 -08003991 cm->seq_params.force_screen_content_tools;
3992 return;
3993 }
3994
3995 if (cpi->oxcf.content == AOM_CONTENT_SCREEN) {
Urvang Joshib6409e92020-03-23 11:23:27 -07003996 features->allow_screen_content_tools = features->allow_intrabc = 1;
Hui Subdf0c992019-02-14 14:52:41 -08003997 return;
3998 }
3999
4000 // Estimate if the source frame is screen content, based on the portion of
4001 // blocks that have few luma colors.
Cheng Chen4f666ca2019-11-18 17:05:03 -08004002 const uint8_t *src = cpi->unfiltered_source->y_buffer;
Hui Subdf0c992019-02-14 14:52:41 -08004003 assert(src != NULL);
Cheng Chen4f666ca2019-11-18 17:05:03 -08004004 const int use_hbd = cpi->unfiltered_source->flags & YV12_FLAG_HIGHBITDEPTH;
4005 const int stride = cpi->unfiltered_source->y_stride;
4006 const int width = cpi->unfiltered_source->y_width;
4007 const int height = cpi->unfiltered_source->y_height;
Hui Subdf0c992019-02-14 14:52:41 -08004008 const int bd = cm->seq_params.bit_depth;
4009 const int blk_w = 16;
4010 const int blk_h = 16;
4011 // These threshold values are selected experimentally.
4012 const int color_thresh = 4;
4013 const unsigned int var_thresh = 0;
4014 // Counts of blocks with no more than color_thresh colors.
4015 int counts_1 = 0;
4016 // Counts of blocks with no more than color_thresh colors and variance larger
4017 // than var_thresh.
4018 int counts_2 = 0;
4019
4020 for (int r = 0; r + blk_h <= height; r += blk_h) {
4021 for (int c = 0; c + blk_w <= width; c += blk_w) {
4022 int count_buf[1 << 12]; // Maximum (1 << 12) color levels.
4023 const uint8_t *const this_src = src + r * stride + c;
4024 const int n_colors =
4025 use_hbd ? av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd,
4026 count_buf)
4027 : av1_count_colors(this_src, stride, blk_w, blk_h, count_buf);
4028 if (n_colors > 1 && n_colors <= color_thresh) {
4029 ++counts_1;
4030 struct buf_2d buf;
4031 buf.stride = stride;
4032 buf.buf = (uint8_t *)this_src;
4033 const unsigned int var =
4034 use_hbd
4035 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
4036 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
4037 if (var > var_thresh) ++counts_2;
4038 }
4039 }
4040 }
4041
4042 // The threshold values are selected experimentally.
Urvang Joshib6409e92020-03-23 11:23:27 -07004043 features->allow_screen_content_tools =
Hui Subdf0c992019-02-14 14:52:41 -08004044 counts_1 * blk_h * blk_w * 10 > width * height;
4045 // IntraBC would force loop filters off, so we use more strict rules that also
4046 // requires that the block has high variance.
Urvang Joshib6409e92020-03-23 11:23:27 -07004047 features->allow_intrabc = features->allow_screen_content_tools &&
4048 counts_2 * blk_h * blk_w * 12 > width * height;
Hui Subdf0c992019-02-14 14:52:41 -08004049}
4050
Yaowu Xuf883b422016-08-30 14:01:10 -07004051static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004052 int i;
Urvang Joshib6409e92020-03-23 11:23:27 -07004053 AV1_COMMON *const cm = &cpi->common;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004054 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07004055 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004056 }
Vishesh8ba8bfe2020-04-03 14:20:29 +05304057 cpi->gm_info.search_done = 0;
Yunqing Wang9411e432019-03-14 15:53:23 -07004058
David Turner04b70d82019-01-24 15:39:19 +00004059 av1_set_speed_features_framesize_independent(cpi, cpi->speed);
Yaowu Xuf883b422016-08-30 14:01:10 -07004060 av1_set_rd_speed_thresholds(cpi);
Urvang Joshi6237b882020-03-26 15:02:26 -07004061 cm->features.interp_filter = SWITCHABLE;
4062 cm->features.switchable_motion_mode = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004063}
4064
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004065#if !CONFIG_REALTIME_ONLY
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304066double av1_get_gfu_boost_projection_factor(double min_factor, double max_factor,
4067 int frame_count) {
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304068 double factor = sqrt((double)frame_count);
4069 factor = AOMMIN(factor, max_factor);
4070 factor = AOMMAX(factor, min_factor);
4071 factor = (200.0 + 10.0 * factor);
4072 return factor;
4073}
4074
4075static int get_gfu_boost_from_r0_lap(double min_factor, double max_factor,
4076 double r0, int frames_to_key) {
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304077 double factor = av1_get_gfu_boost_projection_factor(min_factor, max_factor,
4078 frames_to_key);
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304079 const int boost = (int)rint(factor / r0);
4080 return boost;
4081}
4082
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304083double av1_get_kf_boost_projection_factor(int frame_count) {
Aasaipriya197771d2020-02-06 20:11:21 +05304084 double factor = sqrt((double)frame_count);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07004085 factor = AOMMIN(factor, 10.0);
4086 factor = AOMMAX(factor, 4.0);
Aasaipriya197771d2020-02-06 20:11:21 +05304087 factor = (75.0 + 14.0 * factor);
4088 return factor;
4089}
4090
4091static int get_kf_boost_from_r0(double r0, int frames_to_key) {
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304092 double factor = av1_get_kf_boost_projection_factor(frames_to_key);
Aasaipriya197771d2020-02-06 20:11:21 +05304093 const int boost = (int)rint(factor / r0);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07004094 return boost;
4095}
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004096#endif
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07004097
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304098#define MIN_BOOST_COMBINE_FACTOR 4.0
4099#define MAX_BOOST_COMBINE_FACTOR 12.0
4100int combine_prior_with_tpl_boost(double min_factor, double max_factor,
4101 int prior_boost, int tpl_boost,
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07004102 int frames_to_key) {
4103 double factor = sqrt((double)frames_to_key);
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304104 double range = max_factor - min_factor;
4105 factor = AOMMIN(factor, max_factor);
4106 factor = AOMMAX(factor, min_factor);
4107 factor -= min_factor;
4108 int boost =
4109 (int)((factor * prior_boost + (range - factor) * tpl_boost) / range);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07004110 return boost;
4111}
4112
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004113#if !CONFIG_REALTIME_ONLY
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004114static void process_tpl_stats_frame(AV1_COMP *cpi) {
Sarah Parkere1b22012019-06-06 16:35:25 -07004115 const GF_GROUP *const gf_group = &cpi->gf_group;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004116 AV1_COMMON *const cm = &cpi->common;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004117
Sarah Parkere1b22012019-06-06 16:35:25 -07004118 assert(IMPLIES(gf_group->size > 0, gf_group->index < gf_group->size));
Jingning Han31a0ee92019-07-15 13:56:55 -07004119
4120 const int tpl_idx = gf_group->index;
Vishesh39d03622020-03-31 15:18:16 +05304121 TplParams *const tpl_data = &cpi->tpl_data;
4122 TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
Yue Chen4e585cc2019-06-03 14:47:16 -07004123 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004124
Yue Chen4e585cc2019-06-03 14:47:16 -07004125 if (tpl_frame->is_valid) {
4126 int tpl_stride = tpl_frame->stride;
4127 int64_t intra_cost_base = 0;
4128 int64_t mc_dep_cost_base = 0;
4129 int64_t mc_saved_base = 0;
4130 int64_t mc_count_base = 0;
Vishesh39d03622020-03-31 15:18:16 +05304131 const int step = 1 << tpl_data->tpl_stats_block_mis_log2;
Debargha Mukherjeeedfa4fe2019-07-08 14:23:39 -07004132 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
Yue Chenb4c93f02019-08-05 13:47:31 -07004133
Urvang Joshi9dc909d2020-03-23 16:07:02 -07004134 for (int row = 0; row < cm->mi_params.mi_rows; row += step) {
Yue Chenb4c93f02019-08-05 13:47:31 -07004135 for (int col = 0; col < mi_cols_sr; col += step) {
Vishesha9349502020-04-01 01:54:30 +05304136 TplDepStats *this_stats = &tpl_stats[av1_tpl_ptr_pos(
4137 row, col, tpl_stride, tpl_data->tpl_stats_block_mis_log2)];
Jingning Han40e870f2019-10-02 16:05:39 -07004138 int64_t mc_dep_delta =
4139 RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
4140 this_stats->mc_dep_dist);
4141 intra_cost_base += (this_stats->recrf_dist << RDDIV_BITS);
4142 mc_dep_cost_base +=
4143 (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta;
Yue Chen4e585cc2019-06-03 14:47:16 -07004144 mc_count_base += this_stats->mc_count;
4145 mc_saved_base += this_stats->mc_saved;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004146 }
Yue Chen4e585cc2019-06-03 14:47:16 -07004147 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004148
Yue Chen4e585cc2019-06-03 14:47:16 -07004149 if (mc_dep_cost_base == 0) {
4150 tpl_frame->is_valid = 0;
4151 } else {
4152 aom_clear_system_state();
4153 cpi->rd.r0 = (double)intra_cost_base / mc_dep_cost_base;
Jingning Han44a573b2019-09-04 14:01:22 -07004154 if (is_frame_arf_and_tpl_eligible(gf_group)) {
Yue Chen4e585cc2019-06-03 14:47:16 -07004155 cpi->rd.arf_r0 = cpi->rd.r0;
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304156 if (cpi->lap_enabled) {
4157 double min_boost_factor = sqrt(cpi->rc.baseline_gf_interval);
4158 const int gfu_boost = get_gfu_boost_from_r0_lap(
4159 min_boost_factor, MAX_GFUBOOST_FACTOR, cpi->rd.arf_r0,
4160 cpi->rc.num_stats_required_for_gfu_boost);
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304161 // printf("old boost %d new boost %d\n", cpi->rc.gfu_boost,
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304162 // gfu_boost);
4163 cpi->rc.gfu_boost = combine_prior_with_tpl_boost(
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304164 min_boost_factor, MAX_BOOST_COMBINE_FACTOR, cpi->rc.gfu_boost,
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304165 gfu_boost, cpi->rc.num_stats_used_for_gfu_boost);
4166 } else {
4167 const int gfu_boost = (int)(200.0 / cpi->rd.r0);
4168 cpi->rc.gfu_boost = combine_prior_with_tpl_boost(
4169 MIN_BOOST_COMBINE_FACTOR, MAX_BOOST_COMBINE_FACTOR,
4170 cpi->rc.gfu_boost, gfu_boost, cpi->rc.frames_to_key);
4171 }
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07004172 } else if (frame_is_intra_only(cm)) {
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07004173 // TODO(debargha): Turn off q adjustment for kf temporarily to
4174 // reduce impact on speed of encoding. Need to investigate how
4175 // to mitigate the issue.
4176 if (cpi->oxcf.rc_mode == AOM_Q) {
4177 const int kf_boost =
4178 get_kf_boost_from_r0(cpi->rd.r0, cpi->rc.frames_to_key);
Aasaipriya197771d2020-02-06 20:11:21 +05304179 if (cpi->lap_enabled) {
Aasaipriya197771d2020-02-06 20:11:21 +05304180 cpi->rc.kf_boost = combine_prior_with_tpl_boost(
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304181 MIN_BOOST_COMBINE_FACTOR, MAX_BOOST_COMBINE_FACTOR,
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304182 cpi->rc.kf_boost, kf_boost,
Aasaipriya197771d2020-02-06 20:11:21 +05304183 cpi->rc.num_stats_used_for_kf_boost);
4184 } else {
4185 cpi->rc.kf_boost = combine_prior_with_tpl_boost(
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304186 MIN_BOOST_COMBINE_FACTOR, MAX_BOOST_COMBINE_FACTOR,
Aasaipriya197771d2020-02-06 20:11:21 +05304187 cpi->rc.kf_boost, kf_boost, cpi->rc.frames_to_key);
4188 }
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07004189 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004190 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -07004191 cpi->rd.mc_count_base = (double)mc_count_base /
4192 (cm->mi_params.mi_rows * cm->mi_params.mi_cols);
4193 cpi->rd.mc_saved_base = (double)mc_saved_base /
4194 (cm->mi_params.mi_rows * cm->mi_params.mi_cols);
Yue Chen4e585cc2019-06-03 14:47:16 -07004195 aom_clear_system_state();
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004196 }
4197 }
4198}
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004199#endif // !CONFIG_REALTIME_ONLY
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004200
Yaowu Xuf883b422016-08-30 14:01:10 -07004201static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004202 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004203 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004204
4205 // Setup variables that depend on the dimensions of the frame.
David Turner04b70d82019-01-24 15:39:19 +00004206 av1_set_speed_features_framesize_dependent(cpi, cpi->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004207
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004208#if !CONFIG_REALTIME_ONLY
chiyotsai0fa717c2020-02-05 12:22:01 -08004209 if (cpi->oxcf.enable_tpl_model && is_frame_tpl_eligible(cpi)) {
Yue Chen4e585cc2019-06-03 14:47:16 -07004210 process_tpl_stats_frame(cpi);
sdengf46a1062019-08-04 18:43:50 -07004211 av1_tpl_rdmult_setup(cpi);
4212 }
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004213#endif
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004214
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01004215 // Decide q and q bounds.
Angie Chiang958904c2019-09-19 16:03:35 -07004216 *q = av1_rc_pick_q_and_bounds(cpi, &cpi->rc, cm->width, cm->height,
4217 cpi->gf_group.index, bottom_index, top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004218
Yaowu Xuc27fc142016-08-22 16:08:15 -07004219 // Configure experimental use of segmentation for enhanced coding of
4220 // static regions if indicated.
4221 // Only allowed in the second pass of a two pass encode, as it requires
4222 // lagged coding, and if the relevant speed feature flag is set.
chiyotsai6b430132019-12-18 10:33:51 -08004223 if (is_stat_consumption_stage_twopass(cpi) &&
4224 cpi->sf.hl_sf.static_segmentation)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004225 configure_static_seg_features(cpi);
4226}
4227
Yaowu Xuf883b422016-08-30 14:01:10 -07004228static void init_motion_estimation(AV1_COMP *cpi) {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004229 AV1_COMMON *const cm = &cpi->common;
Nithya V Sace29f32020-04-07 16:15:12 +05304230 MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params;
chiyotsai836b69b2019-04-09 13:41:24 -07004231 const int y_stride = cpi->scaled_source.y_stride;
Urvang Joshi510d8f62019-01-10 12:11:50 -08004232 const int y_stride_src =
4233 ((cpi->oxcf.width != cm->width || cpi->oxcf.height != cm->height) ||
4234 av1_superres_scaled(cm))
4235 ? y_stride
4236 : cpi->lookahead->buf->img.y_stride;
Mufaddal Chakera975f73a2020-03-31 13:22:25 +05304237 int fpf_y_stride = cm->cur_frame != NULL ? cm->cur_frame->buf.y_stride
4238 : cpi->scaled_source.y_stride;
4239
chiyotsai836b69b2019-04-09 13:41:24 -07004240 // Update if ss_cfg is uninitialized or the current frame has a new stride
Nithya V Sace29f32020-04-07 16:15:12 +05304241 const int should_update =
4242 !mv_search_params->ss_cfg[SS_CFG_SRC].stride ||
4243 !mv_search_params->ss_cfg[SS_CFG_LOOKAHEAD].stride ||
4244 (y_stride != mv_search_params->ss_cfg[SS_CFG_SRC].stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004245
chiyotsai836b69b2019-04-09 13:41:24 -07004246 if (!should_update) {
4247 return;
4248 }
4249
chiyotsai8cc054a2019-12-12 14:57:43 -08004250 if (cpi->sf.mv_sf.search_method == DIAMOND) {
Nithya V Sace29f32020-04-07 16:15:12 +05304251 av1_init_dsmotion_compensation(&mv_search_params->ss_cfg[SS_CFG_SRC],
4252 y_stride);
4253 av1_init_dsmotion_compensation(&mv_search_params->ss_cfg[SS_CFG_LOOKAHEAD],
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05304254 y_stride_src);
chiyotsai836b69b2019-04-09 13:41:24 -07004255 } else {
Nithya V Sace29f32020-04-07 16:15:12 +05304256 av1_init3smotion_compensation(&mv_search_params->ss_cfg[SS_CFG_SRC],
4257 y_stride);
4258 av1_init3smotion_compensation(&mv_search_params->ss_cfg[SS_CFG_LOOKAHEAD],
4259 y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004260 }
Nithya V Sace29f32020-04-07 16:15:12 +05304261 av1_init_motion_fpf(&mv_search_params->ss_cfg[SS_CFG_FPF], fpf_y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004262}
4263
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004264#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004265static void set_restoration_unit_size(int width, int height, int sx, int sy,
4266 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004267 (void)width;
4268 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004269 (void)sx;
4270 (void)sy;
4271#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
4272 int s = AOMMIN(sx, sy);
4273#else
4274 int s = 0;
4275#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
4276
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07004277 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08004278 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07004279 else
Urvang Joshi813186b2018-03-08 15:38:46 -08004280 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004281 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
4282 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004283}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004284
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05304285static void init_ref_frame_bufs(AV1_COMP *cpi) {
4286 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07004287 int i;
4288 BufferPool *const pool = cm->buffer_pool;
Jack Haughtonddb80602018-11-21 16:41:49 +00004289 cm->cur_frame = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -07004290 for (i = 0; i < REF_FRAMES; ++i) {
David Turnere7ebf902018-12-04 14:04:55 +00004291 cm->ref_frame_map[i] = NULL;
Wan-Teh Changd05e0332018-10-03 12:00:43 -07004292 }
4293 for (i = 0; i < FRAME_BUFFERS; ++i) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004294 pool->frame_bufs[i].ref_count = 0;
4295 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004296}
4297
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05304298void av1_check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
4299 int subsampling_x, int subsampling_y) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004300 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004301 SequenceHeader *const seq_params = &cm->seq_params;
Jayasanker J44fdab72020-04-13 20:34:38 +05304302 InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
Cheng Chen46f30c72017-09-07 11:13:33 -07004303
Jayasanker J44fdab72020-04-13 20:34:38 +05304304 if (!initial_dimensions->width ||
4305 seq_params->use_highbitdepth != use_highbitdepth ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004306 seq_params->subsampling_x != subsampling_x ||
4307 seq_params->subsampling_y != subsampling_y) {
4308 seq_params->subsampling_x = subsampling_x;
4309 seq_params->subsampling_y = subsampling_y;
4310 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07004311
Jingning Han183b2a82019-12-18 16:03:41 -08004312 av1_set_speed_features_framesize_independent(cpi, cpi->oxcf.speed);
4313 av1_set_speed_features_framesize_dependent(cpi, cpi->oxcf.speed);
4314
Mufaddal Chakera975f73a2020-03-31 13:22:25 +05304315 if (!is_stat_generation_stage(cpi)) {
4316 alloc_altref_frame_buffer(cpi);
4317 alloc_util_frame_buffers(cpi);
4318 }
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05304319 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07004320
4321 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
4322
Jayasanker J44fdab72020-04-13 20:34:38 +05304323 initial_dimensions->width = cm->width;
4324 initial_dimensions->height = cm->height;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07004325 cpi->initial_mbs = cm->mi_params.MBs;
Cheng Chen46f30c72017-09-07 11:13:33 -07004326 }
4327}
4328
4329// Returns 1 if the assigned width or height was <= 0.
Marco Paniconi63971322019-08-15 21:32:05 -07004330int av1_set_size_literal(AV1_COMP *cpi, int width, int height) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004331 AV1_COMMON *cm = &cpi->common;
Jayasanker J44fdab72020-04-13 20:34:38 +05304332 InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05304333 av1_check_initial_width(cpi, cm->seq_params.use_highbitdepth,
4334 cm->seq_params.subsampling_x,
4335 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07004336
4337 if (width <= 0 || height <= 0) return 1;
4338
4339 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07004340 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004341
Jayasanker J44fdab72020-04-13 20:34:38 +05304342 if (initial_dimensions->width && initial_dimensions->height &&
4343 (cm->width > initial_dimensions->width ||
4344 cm->height > initial_dimensions->height)) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004345 av1_free_context_buffers(cm);
Yue Cheneb628982019-08-29 15:17:13 -07004346 av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf);
4347 av1_free_sms_tree(&cpi->td);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004348 alloc_compressor_data(cpi);
4349 realloc_segmentation_maps(cpi);
Jayasanker J44fdab72020-04-13 20:34:38 +05304350 initial_dimensions->width = initial_dimensions->height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07004351 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004352 update_frame_size(cpi);
4353
4354 return 0;
4355}
4356
David Turner475a3132019-01-18 15:17:17 +00004357void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07004358 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004359 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004360 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004361 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004362 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004363
Fergus Simpsonbc189932017-05-16 17:02:39 -07004364 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07004365 // There has been a change in the encoded frame size
Marco Paniconi63971322019-08-15 21:32:05 -07004366 av1_set_size_literal(cpi, width, height);
Urvang Joshic8b52d52018-03-23 13:16:51 -07004367 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Urvang Joshib6409e92020-03-23 11:23:27 -07004368 cm->features.all_lossless =
4369 cm->features.coded_lossless && !av1_superres_scaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004370 }
Yunqing Wang4b7bf402020-01-28 15:20:53 -08004371 set_mv_search_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004372
Hamsalekha S37cc1d12019-12-12 19:27:41 +05304373 if (is_stat_consumption_stage(cpi)) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004374 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004375 }
4376
David Turnere7ebf902018-12-04 14:04:55 +00004377 alloc_frame_mvs(cm, cm->cur_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004378
Cherma Rajan A71d20db2018-04-27 11:15:32 +05304379 // Allocate above context buffers
Urvang Joshi5c8625a2020-03-30 13:16:37 -07004380 CommonContexts *const above_contexts = &cm->above_contexts;
4381 if (above_contexts->num_planes < av1_num_planes(cm) ||
4382 above_contexts->num_mi_cols < cm->mi_params.mi_cols ||
4383 above_contexts->num_tile_rows < cm->tiles.rows) {
4384 av1_free_above_context_buffers(above_contexts);
4385 if (av1_alloc_above_context_buffers(above_contexts, cm->tiles.rows,
4386 cm->mi_params.mi_cols,
4387 av1_num_planes(cm)))
Cherma Rajan A71d20db2018-04-27 11:15:32 +05304388 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
4389 "Failed to allocate context buffers");
4390 }
4391
Yaowu Xuc27fc142016-08-22 16:08:15 -07004392 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004393 if (aom_realloc_frame_buffer(
Jack Haughtonddb80602018-11-21 16:41:49 +00004394 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
4395 seq_params->subsampling_y, seq_params->use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07004396 cpi->oxcf.border_in_pixels, cm->features.byte_alignment, NULL, NULL,
4397 NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07004398 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004399 "Failed to allocate frame buffer");
4400
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004401 const int frame_width = cm->superres_upscaled_width;
4402 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004403 set_restoration_unit_size(frame_width, frame_height,
4404 seq_params->subsampling_x,
4405 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004406 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01004407 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004408
4409 av1_alloc_restoration_buffers(cm);
Mufaddal Chakera975f73a2020-03-31 13:22:25 +05304410 if (!is_stat_generation_stage(cpi)) alloc_util_frame_buffers(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004411 init_motion_estimation(cpi);
4412
4413 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00004414 RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00004415 if (buf != NULL) {
David Turnera21966b2018-12-05 14:48:49 +00004416 struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
4417 av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
David Turner1bcefb32018-11-19 17:54:00 +00004418 buf->buf.y_crop_height, cm->width,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08004419 cm->height);
David Turnera21966b2018-12-05 14:48:49 +00004420 if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004421 }
4422 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07004423
Hui Su5ebd8702018-01-08 18:09:20 -08004424 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08004425 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004426
4427 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
4428}
4429
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004430static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
4431 // Choose an arbitrary random number
4432 static unsigned int seed = 56789;
4433 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Mufaddal Chakerae7326122019-12-04 14:49:09 +05304434 if (is_stat_generation_stage(cpi)) return SCALE_NUMERATOR;
Urvang Joshide71d142017-10-05 12:12:15 -07004435 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004436
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07004437 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004438 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004439 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004440 case RESIZE_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00004441 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004442 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004443 else
Urvang Joshide71d142017-10-05 12:12:15 -07004444 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004445 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004446 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004447 default: assert(0);
4448 }
Urvang Joshide71d142017-10-05 12:12:15 -07004449 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004450}
4451
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004452#if CONFIG_SUPERRES_IN_RECODE
4453static int superres_in_recode_allowed(const AV1_COMP *const cpi) {
4454 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
4455 // Empirically found to not be beneficial for AOM_Q mode and images coding.
Urvang Joshi9d2606c2020-04-14 15:58:01 -07004456 return oxcf->superres_mode == AOM_SUPERRES_AUTO &&
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004457 (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ) &&
4458 cpi->rc.frames_to_key > 1;
4459}
4460#endif // CONFIG_SUPERRES_IN_RECODE
4461
Urvang Joshi510d8f62019-01-10 12:11:50 -08004462#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO 0.012
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004463#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME 0.008
4464#define SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME 0.008
Urvang Joshi510d8f62019-01-10 12:11:50 -08004465#define SUPERRES_ENERGY_BY_AC_THRESH 0.2
4466
4467static double get_energy_by_q2_thresh(const GF_GROUP *gf_group,
4468 const RATE_CONTROL *rc) {
4469 // TODO(now): Return keyframe thresh * factor based on frame type / pyramid
4470 // level.
4471 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
4472 return SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME;
4473 } else if (gf_group->update_type[gf_group->index] == KF_UPDATE) {
4474 if (rc->frames_to_key <= 1)
4475 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO;
4476 else
4477 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME;
4478 } else {
4479 assert(0);
4480 }
4481 return 0;
4482}
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004483
4484static uint8_t get_superres_denom_from_qindex_energy(int qindex, double *energy,
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004485 double threshq,
4486 double threshp) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004487 const double q = av1_convert_qindex_to_q(qindex, AOM_BITS_8);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004488 const double tq = threshq * q * q;
4489 const double tp = threshp * energy[1];
4490 const double thresh = AOMMIN(tq, tp);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004491 int k;
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004492 for (k = SCALE_NUMERATOR * 2; k > SCALE_NUMERATOR; --k) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004493 if (energy[k - 1] > thresh) break;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004494 }
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004495 return 3 * SCALE_NUMERATOR - k;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004496}
4497
Urvang Joshi510d8f62019-01-10 12:11:50 -08004498static uint8_t get_superres_denom_for_qindex(const AV1_COMP *cpi, int qindex,
4499 int sr_kf, int sr_arf) {
4500 // Use superres for Key-frames and Alt-ref frames only.
4501 const GF_GROUP *gf_group = &cpi->gf_group;
4502 if (gf_group->update_type[gf_group->index] != KF_UPDATE &&
4503 gf_group->update_type[gf_group->index] != ARF_UPDATE) {
4504 return SCALE_NUMERATOR;
4505 }
4506 if (gf_group->update_type[gf_group->index] == KF_UPDATE && !sr_kf) {
4507 return SCALE_NUMERATOR;
4508 }
4509 if (gf_group->update_type[gf_group->index] == ARF_UPDATE && !sr_arf) {
4510 return SCALE_NUMERATOR;
4511 }
4512
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004513 double energy[16];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004514 analyze_hor_freq(cpi, energy);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004515
4516 const double energy_by_q2_thresh =
4517 get_energy_by_q2_thresh(gf_group, &cpi->rc);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004518 int denom = get_superres_denom_from_qindex_energy(
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004519 qindex, energy, energy_by_q2_thresh, SUPERRES_ENERGY_BY_AC_THRESH);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004520 /*
4521 printf("\nenergy = [");
4522 for (int k = 1; k < 16; ++k) printf("%f, ", energy[k]);
4523 printf("]\n");
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004524 printf("boost = %d\n",
4525 (gf_group->update_type[gf_group->index] == KF_UPDATE)
4526 ? cpi->rc.kf_boost
4527 : cpi->rc.gfu_boost);
4528 printf("denom = %d\n", denom);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004529 */
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004530#if CONFIG_SUPERRES_IN_RECODE
4531 if (superres_in_recode_allowed(cpi)) {
Urvang Joshi9d2606c2020-04-14 15:58:01 -07004532 assert(cpi->superres_mode != AOM_SUPERRES_NONE);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004533 // Force superres to be tried in the recode loop, as full-res is also going
4534 // to be tried anyway.
4535 denom = AOMMAX(denom, SCALE_NUMERATOR + 1);
4536 }
4537#endif // CONFIG_SUPERRES_IN_RECODE
Urvang Joshi510d8f62019-01-10 12:11:50 -08004538 return denom;
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004539}
4540
Urvang Joshi9d2606c2020-04-14 15:58:01 -07004541// If true, AOM_SUPERRES_AUTO mode will exhaustively search over all superres
Urvang Joshibc82d382019-11-01 17:59:20 -07004542// denominators for all frames (except overlay and internal overlay frames).
4543#define SUPERRES_RECODE_ALL_RATIOS 0
4544
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004545static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
4546 // Choose an arbitrary random number
4547 static unsigned int seed = 34567;
4548 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Mufaddal Chakerae7326122019-12-04 14:49:09 +05304549 if (is_stat_generation_stage(cpi)) return SCALE_NUMERATOR;
Urvang Joshide71d142017-10-05 12:12:15 -07004550 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07004551
4552 // Make sure that superres mode of the frame is consistent with the
4553 // sequence-level flag.
Urvang Joshi9d2606c2020-04-14 15:58:01 -07004554 assert(IMPLIES(oxcf->superres_mode != AOM_SUPERRES_NONE,
Urvang Joshi2c92b072018-03-19 17:23:31 -07004555 cpi->common.seq_params.enable_superres));
4556 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
Urvang Joshi9d2606c2020-04-14 15:58:01 -07004557 oxcf->superres_mode == AOM_SUPERRES_NONE));
Urvang Joshif9c2bd72020-04-07 15:11:51 -07004558 // Make sure that superres mode for current encoding is consistent with user
4559 // provided superres mode.
Urvang Joshi9d2606c2020-04-14 15:58:01 -07004560 assert(IMPLIES(oxcf->superres_mode != AOM_SUPERRES_AUTO,
Urvang Joshif9c2bd72020-04-07 15:11:51 -07004561 cpi->superres_mode == oxcf->superres_mode));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004562
Urvang Joshif9c2bd72020-04-07 15:11:51 -07004563 // Note: we must look at the current superres_mode to be tried in 'cpi' here,
4564 // not the user given mode in 'oxcf'.
4565 switch (cpi->superres_mode) {
Urvang Joshi9d2606c2020-04-14 15:58:01 -07004566 case AOM_SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
4567 case AOM_SUPERRES_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00004568 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004569 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004570 else
Urvang Joshide71d142017-10-05 12:12:15 -07004571 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004572 break;
Urvang Joshi9d2606c2020-04-14 15:58:01 -07004573 case AOM_SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
4574 case AOM_SUPERRES_QTHRESH: {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07004575 // Do not use superres when screen content tools are used.
Urvang Joshib6409e92020-03-23 11:23:27 -07004576 if (cpi->common.features.allow_screen_content_tools) break;
Debargha Mukherjee2b2c5fd2018-11-14 13:21:24 -08004577 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4578 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004579
4580 // Now decide the use of superres based on 'q'.
Urvang Joshi2c92b072018-03-19 17:23:31 -07004581 int bottom_index, top_index;
4582 const int q = av1_rc_pick_q_and_bounds(
Angie Chiang958904c2019-09-19 16:03:35 -07004583 cpi, &cpi->rc, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
Jingning Han2e029872019-08-02 10:54:19 -07004584 &bottom_index, &top_index);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004585
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004586 const int qthresh = (frame_is_intra_only(&cpi->common))
4587 ? oxcf->superres_kf_qthresh
4588 : oxcf->superres_qthresh;
Urvang Joshib17e0a62019-01-11 16:11:54 -08004589 if (q <= qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07004590 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004591 } else {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004592 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004593 }
4594 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004595 }
Urvang Joshi9d2606c2020-04-14 15:58:01 -07004596 case AOM_SUPERRES_AUTO: {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004597 // Do not use superres when screen content tools are used.
Urvang Joshib6409e92020-03-23 11:23:27 -07004598 if (cpi->common.features.allow_screen_content_tools) break;
Urvang Joshi510d8f62019-01-10 12:11:50 -08004599 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4600 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi36a83732019-01-31 15:31:57 -08004601
4602 // Now decide the use of superres based on 'q'.
4603 int bottom_index, top_index;
4604 const int q = av1_rc_pick_q_and_bounds(
Angie Chiang958904c2019-09-19 16:03:35 -07004605 cpi, &cpi->rc, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
Jingning Han2e029872019-08-02 10:54:19 -07004606 &bottom_index, &top_index);
Urvang Joshi36a83732019-01-31 15:31:57 -08004607
4608 const int qthresh = 128;
4609 if (q <= qthresh) {
4610 new_denom = SCALE_NUMERATOR;
4611 } else {
Urvang Joshibc82d382019-11-01 17:59:20 -07004612#if SUPERRES_RECODE_ALL_RATIOS
4613 if (cpi->common.current_frame.frame_type == KEY_FRAME)
4614 new_denom = oxcf->superres_kf_scale_denominator;
4615 else
4616 new_denom = oxcf->superres_scale_denominator;
4617#else
Urvang Joshi510d8f62019-01-10 12:11:50 -08004618 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Urvang Joshibc82d382019-11-01 17:59:20 -07004619#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi36a83732019-01-31 15:31:57 -08004620 }
4621 break;
4622 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004623 default: assert(0);
4624 }
Urvang Joshide71d142017-10-05 12:12:15 -07004625 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004626}
4627
Urvang Joshide71d142017-10-05 12:12:15 -07004628static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4629 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4630}
4631
4632static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08004633 // Only need to check the width, as scaling is horizontal only.
4634 (void)oheight;
4635 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07004636}
4637
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004638static int validate_size_scales(RESIZE_MODE resize_mode,
Urvang Joshi9d2606c2020-04-14 15:58:01 -07004639 aom_superres_mode superres_mode, int owidth,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004640 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004641 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004642 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004643 }
4644
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004645 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07004646 int resize_denom =
4647 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4648 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4649
Urvang Joshi9d2606c2020-04-14 15:58:01 -07004650 if (resize_mode != RESIZE_RANDOM && superres_mode == AOM_SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004651 // Alter superres scale as needed to enforce conformity.
4652 rsz->superres_denom =
4653 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4654 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4655 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004656 }
Urvang Joshi9d2606c2020-04-14 15:58:01 -07004657 } else if (resize_mode == RESIZE_RANDOM &&
4658 superres_mode != AOM_SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004659 // Alter resize scale as needed to enforce conformity.
4660 resize_denom =
4661 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004662 rsz->resize_width = owidth;
4663 rsz->resize_height = oheight;
4664 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004665 resize_denom);
4666 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4667 if (resize_denom > SCALE_NUMERATOR) {
4668 --resize_denom;
4669 rsz->resize_width = owidth;
4670 rsz->resize_height = oheight;
4671 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4672 resize_denom);
4673 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004674 }
Urvang Joshi9d2606c2020-04-14 15:58:01 -07004675 } else if (resize_mode == RESIZE_RANDOM &&
4676 superres_mode == AOM_SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004677 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004678 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004679 if (resize_denom > rsz->superres_denom)
4680 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004681 else
Urvang Joshide71d142017-10-05 12:12:15 -07004682 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004683 rsz->resize_width = owidth;
4684 rsz->resize_height = oheight;
4685 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004686 resize_denom);
4687 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4688 (resize_denom > SCALE_NUMERATOR ||
4689 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08004690 } else { // We are allowed to alter neither resize scale nor superres
4691 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004692 return 0;
4693 }
Urvang Joshide71d142017-10-05 12:12:15 -07004694 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004695}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004696
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004697// Calculates resize and superres params for next frame
David Turner475a3132019-01-18 15:17:17 +00004698static size_params_type calculate_next_size_params(AV1_COMP *cpi) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004699 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Jayasanker J0b1dd292020-04-09 15:47:25 +05304700 ResizePendingParams *resize_pending_params = &cpi->resize_pending_params;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08004701 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshi510d8f62019-01-10 12:11:50 -08004702 int resize_denom = SCALE_NUMERATOR;
Mufaddal Chakera3bcc72c2019-12-11 14:38:37 +05304703 if (has_no_stats_stage(cpi) && cpi->use_svc &&
Marco Paniconi63971322019-08-15 21:32:05 -07004704 cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1) {
4705 rsz.resize_width = cpi->common.width;
4706 rsz.resize_height = cpi->common.height;
4707 return rsz;
4708 }
Mufaddal Chakerae7326122019-12-04 14:49:09 +05304709 if (is_stat_generation_stage(cpi)) return rsz;
Jayasanker J0b1dd292020-04-09 15:47:25 +05304710 if (resize_pending_params->width && resize_pending_params->height) {
4711 rsz.resize_width = resize_pending_params->width;
4712 rsz.resize_height = resize_pending_params->height;
4713 resize_pending_params->width = resize_pending_params->height = 0;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004714 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004715 resize_denom = calculate_next_resize_scale(cpi);
Jayasanker J0b1dd292020-04-09 15:47:25 +05304716 rsz.resize_width = oxcf->width;
4717 rsz.resize_height = oxcf->height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004718 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004719 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004720 }
Urvang Joshide71d142017-10-05 12:12:15 -07004721 rsz.superres_denom = calculate_next_superres_scale(cpi);
Urvang Joshif9c2bd72020-04-07 15:11:51 -07004722 if (!validate_size_scales(oxcf->resize_mode, cpi->superres_mode, oxcf->width,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004723 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004724 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004725 return rsz;
4726}
4727
Urvang Joshi22b150b2019-01-10 14:32:32 -08004728static void setup_frame_size_from_params(AV1_COMP *cpi,
4729 const size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004730 int encode_width = rsz->resize_width;
4731 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004732
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004733 AV1_COMMON *cm = &cpi->common;
4734 cm->superres_upscaled_width = encode_width;
4735 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004736 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004737 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4738 rsz->superres_denom);
David Turner475a3132019-01-18 15:17:17 +00004739 av1_set_frame_size(cpi, encode_width, encode_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004740}
4741
David Turnerdedd8ff2019-01-23 13:59:46 +00004742void av1_setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004743 AV1_COMMON *cm = &cpi->common;
Urvang Joshi22b150b2019-01-10 14:32:32 -08004744 // Reset superres params from previous frame.
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004745 cm->superres_scale_denominator = SCALE_NUMERATOR;
David Turner475a3132019-01-18 15:17:17 +00004746 const size_params_type rsz = calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004747 setup_frame_size_from_params(cpi, &rsz);
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004748
Yaowu Xu7e450882019-04-30 15:09:18 -07004749 assert(av1_is_min_tile_width_satisfied(cm));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004750}
4751
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004752static void superres_post_encode(AV1_COMP *cpi) {
4753 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004754 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004755
Cheng Chen09c83a52018-06-05 12:27:36 -07004756 if (!av1_superres_scaled(cm)) return;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004757
Urvang Joshid6b5d512018-03-20 13:34:38 -07004758 assert(cpi->oxcf.enable_superres);
4759 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshib6409e92020-03-23 11:23:27 -07004760 assert(!cm->features.all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07004761
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004762 av1_superres_upscale(cm, NULL);
4763
4764 // If regular resizing is occurring the source will need to be downscaled to
4765 // match the upscaled superres resolution. Otherwise the original source is
4766 // used.
Cheng Chen09c83a52018-06-05 12:27:36 -07004767 if (!av1_resize_scaled(cm)) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004768 cpi->source = cpi->unscaled_source;
4769 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4770 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004771 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4772 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08004773 // Do downscale. cm->(width|height) has been updated by
4774 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004775 if (aom_realloc_frame_buffer(
4776 &cpi->scaled_source, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004777 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4778 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07004779 AOM_BORDER_IN_PIXELS, cm->features.byte_alignment, NULL, NULL,
4780 NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004781 aom_internal_error(
4782 &cm->error, AOM_CODEC_MEM_ERROR,
4783 "Failed to reallocate scaled source buffer for superres");
4784 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4785 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004786 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004787 (int)cm->seq_params.bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004788 cpi->source = &cpi->scaled_source;
4789 }
4790}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004791
Yue Chen92271e12019-07-09 16:28:02 -07004792static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
4793 MACROBLOCKD *xd, int use_restoration,
4794 int use_cdef) {
Deepa K G74de2a02020-04-11 13:09:11 +05304795 MultiThreadInfo *const mt_info = &cpi->mt_info;
4796 const int num_workers = mt_info->num_workers;
logangwf95c9162019-02-20 12:02:32 -08004797 if (use_restoration)
David Turnerc29e1a92018-12-06 14:10:14 +00004798 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004799
logangwf95c9162019-02-20 12:02:32 -08004800 if (use_cdef) {
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004801#if CONFIG_COLLECT_COMPONENT_TIMING
4802 start_timing(cpi, cdef_time);
4803#endif
Steinar Midtskogen59782122017-07-20 08:49:43 +02004804 // Find CDEF parameters
David Turnerc29e1a92018-12-06 14:10:14 +00004805 av1_cdef_search(&cm->cur_frame->buf, cpi->source, cm, xd,
chiyotsaid2f7b412019-12-18 10:16:42 -08004806 cpi->sf.lpf_sf.cdef_pick_method, cpi->td.mb.rdmult);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004807
4808 // Apply the filter
David Turnerc29e1a92018-12-06 14:10:14 +00004809 av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004810#if CONFIG_COLLECT_COMPONENT_TIMING
4811 end_timing(cpi, cdef_time);
4812#endif
logangwf95c9162019-02-20 12:02:32 -08004813 } else {
4814 cm->cdef_info.cdef_bits = 0;
4815 cm->cdef_info.cdef_strengths[0] = 0;
4816 cm->cdef_info.nb_cdef_strengths = 1;
4817 cm->cdef_info.cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004818 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004819
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004820 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004821
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004822#if CONFIG_COLLECT_COMPONENT_TIMING
4823 start_timing(cpi, loop_restoration_time);
4824#endif
logangwf95c9162019-02-20 12:02:32 -08004825 if (use_restoration) {
David Turnerc29e1a92018-12-06 14:10:14 +00004826 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004827 av1_pick_filter_restoration(cpi->source, cpi);
4828 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4829 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4830 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Deepa K G74de2a02020-04-11 13:09:11 +05304831 if (num_workers > 1)
4832 av1_loop_restoration_filter_frame_mt(
4833 &cm->cur_frame->buf, cm, 0, mt_info->workers, num_workers,
4834 &mt_info->lr_row_sync, &cpi->lr_ctxt);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304835 else
David Turnerc29e1a92018-12-06 14:10:14 +00004836 av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304837 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004838 }
logangwf95c9162019-02-20 12:02:32 -08004839 } else {
4840 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4841 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4842 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004843 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004844#if CONFIG_COLLECT_COMPONENT_TIMING
4845 end_timing(cpi, loop_restoration_time);
4846#endif
Fergus Simpsonbc189932017-05-16 17:02:39 -07004847}
4848
Yue Chen92271e12019-07-09 16:28:02 -07004849static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
Deepa K G74de2a02020-04-11 13:09:11 +05304850 MultiThreadInfo *const mt_info = &cpi->mt_info;
4851 const int num_workers = mt_info->num_workers;
Yue Chen92271e12019-07-09 16:28:02 -07004852 const int num_planes = av1_num_planes(cm);
4853 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4854
4855 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
Urvang Joshib6409e92020-03-23 11:23:27 -07004856 cm->features.coded_lossless && cm->features.all_lossless));
Yue Chen92271e12019-07-09 16:28:02 -07004857
Urvang Joshib6409e92020-03-23 11:23:27 -07004858 const int use_loopfilter =
Urvang Joshi54ffae72020-03-23 13:37:10 -07004859 !cm->features.coded_lossless && !cm->tiles.large_scale;
Urvang Joshib6409e92020-03-23 11:23:27 -07004860 const int use_cdef = cm->seq_params.enable_cdef &&
Urvang Joshi54ffae72020-03-23 13:37:10 -07004861 !cm->features.coded_lossless && !cm->tiles.large_scale;
Yue Chen92271e12019-07-09 16:28:02 -07004862 const int use_restoration = cm->seq_params.enable_restoration &&
Urvang Joshib6409e92020-03-23 11:23:27 -07004863 !cm->features.all_lossless &&
Urvang Joshi54ffae72020-03-23 13:37:10 -07004864 !cm->tiles.large_scale;
Yue Chen92271e12019-07-09 16:28:02 -07004865
4866 struct loopfilter *lf = &cm->lf;
4867
4868#if CONFIG_COLLECT_COMPONENT_TIMING
4869 start_timing(cpi, loop_filter_time);
4870#endif
4871 if (use_loopfilter) {
4872 aom_clear_system_state();
chiyotsaid2f7b412019-12-18 10:16:42 -08004873 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_sf.lpf_pick);
Yue Chen92271e12019-07-09 16:28:02 -07004874 } else {
4875 lf->filter_level[0] = 0;
4876 lf->filter_level[1] = 0;
4877 }
4878
4879 if (lf->filter_level[0] || lf->filter_level[1]) {
Deepa K G74de2a02020-04-11 13:09:11 +05304880 if (num_workers > 1)
Yue Chen92271e12019-07-09 16:28:02 -07004881 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
4882#if CONFIG_LPF_MASK
4883 0,
4884#endif
Deepa K G74de2a02020-04-11 13:09:11 +05304885 mt_info->workers, num_workers,
4886 &mt_info->lf_row_sync);
Yue Chen92271e12019-07-09 16:28:02 -07004887 else
4888 av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
4889#if CONFIG_LPF_MASK
4890 0,
4891#endif
4892 0, num_planes, 0);
4893 }
4894#if CONFIG_COLLECT_COMPONENT_TIMING
4895 end_timing(cpi, loop_filter_time);
4896#endif
4897
4898 cdef_restoration_frame(cpi, cm, xd, use_restoration, use_cdef);
4899}
4900
David Turnerf2b334c2018-12-13 13:00:55 +00004901static void fix_interp_filter(InterpFilter *const interp_filter,
4902 const FRAME_COUNTS *const counts) {
4903 if (*interp_filter == SWITCHABLE) {
4904 // Check to see if only one of the filters is actually used
4905 int count[SWITCHABLE_FILTERS] = { 0 };
4906 int num_filters_used = 0;
4907 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4908 for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
4909 count[i] += counts->switchable_interp[j][i];
4910 num_filters_used += (count[i] > 0);
4911 }
4912 if (num_filters_used == 1) {
4913 // Only one filter is used. So set the filter at frame level
4914 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4915 if (count[i]) {
4916 if (i == EIGHTTAP_REGULAR) *interp_filter = i;
4917 break;
4918 }
4919 }
4920 }
4921 }
4922}
4923
David Turner996b2c12018-12-07 15:52:30 +00004924static void finalize_encoded_frame(AV1_COMP *const cpi) {
4925 AV1_COMMON *const cm = &cpi->common;
David Turner99e990e2018-12-10 12:54:26 +00004926 CurrentFrame *const current_frame = &cm->current_frame;
David Turner996b2c12018-12-07 15:52:30 +00004927
David Turner99e990e2018-12-10 12:54:26 +00004928 if (!cm->seq_params.reduced_still_picture_hdr &&
4929 encode_show_existing_frame(cm)) {
4930 RefCntBuffer *const frame_to_show =
4931 cm->ref_frame_map[cpi->existing_fb_idx_to_show];
4932
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004933 if (frame_to_show == NULL) {
David Turner99e990e2018-12-10 12:54:26 +00004934 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4935 "Buffer does not contain a reconstructed frame");
4936 }
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004937 assert(frame_to_show->ref_count > 0);
David Turner99e990e2018-12-10 12:54:26 +00004938 assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
David Turner99e990e2018-12-10 12:54:26 +00004939 }
David Turner08f909c2018-12-18 13:29:14 +00004940
4941 if (!encode_show_existing_frame(cm) &&
4942 cm->seq_params.film_grain_params_present &&
4943 (cm->show_frame || cm->showable_frame)) {
4944 // Copy the current frame's film grain params to the its corresponding
4945 // RefCntBuffer slot.
4946 cm->cur_frame->film_grain_params = cm->film_grain_params;
4947
4948 // We must update the parameters if this is not an INTER_FRAME
4949 if (current_frame->frame_type != INTER_FRAME)
4950 cm->cur_frame->film_grain_params.update_parameters = 1;
4951
4952 // Iterate the random seed for the next frame.
4953 cm->film_grain_params.random_seed += 3381;
4954 if (cm->film_grain_params.random_seed == 0)
4955 cm->film_grain_params.random_seed = 7391;
4956 }
David Turnerf2b334c2018-12-13 13:00:55 +00004957
4958 // Initialise all tiles' contexts from the global frame context
Urvang Joshi54ffae72020-03-23 13:37:10 -07004959 for (int tile_col = 0; tile_col < cm->tiles.cols; tile_col++) {
4960 for (int tile_row = 0; tile_row < cm->tiles.rows; tile_row++) {
4961 const int tile_idx = tile_row * cm->tiles.cols + tile_col;
David Turnerf2b334c2018-12-13 13:00:55 +00004962 cpi->tile_data[tile_idx].tctx = *cm->fc;
4963 }
4964 }
4965
Urvang Joshi6237b882020-03-26 15:02:26 -07004966 fix_interp_filter(&cm->features.interp_filter, cpi->td.counts);
David Turner996b2c12018-12-07 15:52:30 +00004967}
4968
Urvang Joshi57643372019-02-21 11:10:57 -08004969static int get_regulated_q_overshoot(AV1_COMP *const cpi, int q_low, int q_high,
4970 int top_index, int bottom_index) {
4971 const AV1_COMMON *const cm = &cpi->common;
4972 const RATE_CONTROL *const rc = &cpi->rc;
4973
4974 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4975
4976 int q_regulated =
4977 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4978 AOMMAX(q_high, top_index), cm->width, cm->height);
4979
4980 int retries = 0;
4981 while (q_regulated < q_low && retries < 10) {
4982 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4983 q_regulated =
4984 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4985 AOMMAX(q_high, top_index), cm->width, cm->height);
4986 retries++;
4987 }
4988 return q_regulated;
4989}
4990
4991static int get_regulated_q_undershoot(AV1_COMP *const cpi, int q_high,
4992 int top_index, int bottom_index) {
4993 const AV1_COMMON *const cm = &cpi->common;
4994 const RATE_CONTROL *const rc = &cpi->rc;
4995
4996 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4997 int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4998 top_index, cm->width, cm->height);
4999
5000 int retries = 0;
5001 while (q_regulated > q_high && retries < 10) {
5002 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
5003 q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
5004 top_index, cm->width, cm->height);
5005 retries++;
5006 }
5007 return q_regulated;
5008}
5009
David Turner2f3b5df2019-01-02 14:30:50 +00005010// Called after encode_with_recode_loop() has just encoded a frame and packed
5011// its bitstream. This function works out whether we under- or over-shot
5012// our bitrate target and adjusts q as appropriate. Also decides whether
5013// or not we should do another recode loop, indicated by *loop
Hui Suef139e12019-05-20 15:51:22 -07005014static void recode_loop_update_q(
5015 AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low,
5016 int *const q_high, const int top_index, const int bottom_index,
5017 int *const undershoot_seen, int *const overshoot_seen,
5018 int *const low_cr_seen, const int loop_at_this_size) {
David Turner2f3b5df2019-01-02 14:30:50 +00005019 AV1_COMMON *const cm = &cpi->common;
5020 RATE_CONTROL *const rc = &cpi->rc;
Debargha Mukherjee90b20752020-03-19 23:16:34 -07005021 *loop = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00005022
Hui Suef139e12019-05-20 15:51:22 -07005023 const int min_cr = cpi->oxcf.min_cr;
5024 if (min_cr > 0) {
5025 aom_clear_system_state();
5026 const double compression_ratio =
5027 av1_get_compression_ratio(cm, rc->projected_frame_size >> 3);
5028 const double target_cr = min_cr / 100.0;
5029 if (compression_ratio < target_cr) {
5030 *low_cr_seen = 1;
5031 if (*q < rc->worst_quality) {
5032 const double cr_ratio = target_cr / compression_ratio;
5033 const int projected_q = AOMMAX(*q + 1, (int)(*q * cr_ratio * cr_ratio));
5034 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->worst_quality);
5035 *q_low = AOMMAX(*q, *q_low);
5036 *q_high = AOMMAX(*q, *q_high);
5037 *loop = 1;
5038 }
5039 }
5040 if (*low_cr_seen) return;
5041 }
5042
5043 if (cpi->oxcf.rc_mode == AOM_Q) return;
5044
Hui Su01dfe032019-09-11 14:10:42 -07005045 const int last_q = *q;
David Turner2f3b5df2019-01-02 14:30:50 +00005046 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
5047 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
5048 &frame_under_shoot_limit,
5049 &frame_over_shoot_limit);
5050 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
5051
Hui Su01dfe032019-09-11 14:10:42 -07005052 if (cm->current_frame.frame_type == KEY_FRAME && rc->this_key_frame_forced &&
5053 rc->projected_frame_size < rc->max_frame_bandwidth) {
David Turner2f3b5df2019-01-02 14:30:50 +00005054 int64_t kf_err;
Hui Su01dfe032019-09-11 14:10:42 -07005055 const int64_t high_err_target = cpi->ambient_err;
5056 const int64_t low_err_target = cpi->ambient_err >> 1;
David Turner2f3b5df2019-01-02 14:30:50 +00005057
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005058#if CONFIG_AV1_HIGHBITDEPTH
David Turner2f3b5df2019-01-02 14:30:50 +00005059 if (cm->seq_params.use_highbitdepth) {
5060 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
5061 } else {
5062 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5063 }
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005064#else
5065 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5066#endif
David Turner2f3b5df2019-01-02 14:30:50 +00005067 // Prevent possible divide by zero error below for perfect KF
5068 kf_err += !kf_err;
5069
5070 // The key frame is not good enough or we can afford
5071 // to make it better without undue risk of popping.
5072 if ((kf_err > high_err_target &&
5073 rc->projected_frame_size <= frame_over_shoot_limit) ||
5074 (kf_err > low_err_target &&
5075 rc->projected_frame_size <= frame_under_shoot_limit)) {
5076 // Lower q_high
Hui Su01dfe032019-09-11 14:10:42 -07005077 *q_high = AOMMAX(*q - 1, *q_low);
David Turner2f3b5df2019-01-02 14:30:50 +00005078
5079 // Adjust Q
5080 *q = (int)((*q * high_err_target) / kf_err);
5081 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
5082 } else if (kf_err < low_err_target &&
5083 rc->projected_frame_size >= frame_under_shoot_limit) {
5084 // The key frame is much better than the previous frame
5085 // Raise q_low
Hui Su01dfe032019-09-11 14:10:42 -07005086 *q_low = AOMMIN(*q + 1, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00005087
5088 // Adjust Q
5089 *q = (int)((*q * low_err_target) / kf_err);
5090 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
5091 }
5092
5093 // Clamp Q to upper and lower limits:
5094 *q = clamp(*q, *q_low, *q_high);
Hui Su01dfe032019-09-11 14:10:42 -07005095 *loop = (*q != last_q);
5096 return;
5097 }
David Turner2f3b5df2019-01-02 14:30:50 +00005098
Hui Su01dfe032019-09-11 14:10:42 -07005099 if (recode_loop_test(cpi, frame_over_shoot_limit, frame_under_shoot_limit, *q,
5100 AOMMAX(*q_high, top_index), bottom_index)) {
David Turner2f3b5df2019-01-02 14:30:50 +00005101 // Is the projected frame size out of range and are we allowed
5102 // to attempt to recode.
David Turner2f3b5df2019-01-02 14:30:50 +00005103
5104 // Frame size out of permitted range:
5105 // Update correction factor & compute new Q to try...
5106 // Frame is too large
5107 if (rc->projected_frame_size > rc->this_frame_target) {
5108 // Special case if the projected size is > the max allowed.
Urvang Joshi7344d3e2019-07-25 17:24:57 -07005109 if (*q == *q_high &&
5110 rc->projected_frame_size >= rc->max_frame_bandwidth) {
5111 const double q_val_high_current =
5112 av1_convert_qindex_to_q(*q_high, cm->seq_params.bit_depth);
5113 const double q_val_high_new =
5114 q_val_high_current *
5115 ((double)rc->projected_frame_size / rc->max_frame_bandwidth);
5116 *q_high = av1_find_qindex(q_val_high_new, cm->seq_params.bit_depth,
5117 rc->best_quality, rc->worst_quality);
5118 }
David Turner2f3b5df2019-01-02 14:30:50 +00005119
5120 // Raise Qlow as to at least the current value
Hui Su01dfe032019-09-11 14:10:42 -07005121 *q_low = AOMMIN(*q + 1, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00005122
Urvang Joshi57643372019-02-21 11:10:57 -08005123 if (*undershoot_seen || loop_at_this_size > 2 ||
5124 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00005125 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
5126
5127 *q = (*q_high + *q_low + 1) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08005128 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
5129 const int q_mid = (*q_high + *q_low + 1) / 2;
5130 const int q_regulated = get_regulated_q_overshoot(
5131 cpi, *q_low, *q_high, top_index, bottom_index);
5132 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
5133 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
5134 *q = (q_mid + q_regulated + 1) / 2;
David Turner2f3b5df2019-01-02 14:30:50 +00005135 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08005136 *q = get_regulated_q_overshoot(cpi, *q_low, *q_high, top_index,
5137 bottom_index);
David Turner2f3b5df2019-01-02 14:30:50 +00005138 }
5139
5140 *overshoot_seen = 1;
5141 } else {
5142 // Frame is too small
Hui Su01dfe032019-09-11 14:10:42 -07005143 *q_high = AOMMAX(*q - 1, *q_low);
David Turner2f3b5df2019-01-02 14:30:50 +00005144
Urvang Joshi57643372019-02-21 11:10:57 -08005145 if (*overshoot_seen || loop_at_this_size > 2 ||
5146 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00005147 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
5148 *q = (*q_high + *q_low) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08005149 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
5150 const int q_mid = (*q_high + *q_low) / 2;
5151 const int q_regulated =
5152 get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
5153 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
5154 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
5155 *q = (q_mid + q_regulated) / 2;
5156
5157 // Special case reset for qlow for constrained quality.
5158 // This should only trigger where there is very substantial
5159 // undershoot on a frame and the auto cq level is above
5160 // the user passsed in value.
5161 if (cpi->oxcf.rc_mode == AOM_CQ && q_regulated < *q_low) {
5162 *q_low = *q;
5163 }
David Turner2f3b5df2019-01-02 14:30:50 +00005164 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08005165 *q = get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
5166
David Turner2f3b5df2019-01-02 14:30:50 +00005167 // Special case reset for qlow for constrained quality.
5168 // This should only trigger where there is very substantial
5169 // undershoot on a frame and the auto cq level is above
5170 // the user passsed in value.
5171 if (cpi->oxcf.rc_mode == AOM_CQ && *q < *q_low) {
5172 *q_low = *q;
5173 }
David Turner2f3b5df2019-01-02 14:30:50 +00005174 }
5175
5176 *undershoot_seen = 1;
5177 }
5178
5179 // Clamp Q to upper and lower limits:
5180 *q = clamp(*q, *q_low, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00005181 }
Hui Su01dfe032019-09-11 14:10:42 -07005182
5183 *loop = (*q != last_q);
David Turner2f3b5df2019-01-02 14:30:50 +00005184}
5185
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305186static int get_interp_filter_selected(const AV1_COMMON *const cm,
5187 MV_REFERENCE_FRAME ref,
5188 InterpFilter ifilter) {
5189 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
5190 if (buf == NULL) return 0;
5191 return buf->interp_filter_selected[ifilter];
5192}
5193
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305194static uint16_t setup_interp_filter_search_mask(AV1_COMP *cpi) {
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305195 const AV1_COMMON *const cm = &cpi->common;
5196 int ref_total[REF_FRAMES] = { 0 };
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305197 uint16_t mask = ALLOW_ALL_INTERP_FILT_MASK;
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305198
Jayasanker J24cb9bc2020-04-15 13:43:10 +05305199 if (cpi->last_frame_type == KEY_FRAME || cpi->refresh_frame.alt_ref_frame)
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305200 return mask;
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305201
5202 for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
5203 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
5204 ++ifilter) {
5205 ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter);
5206 }
5207 }
5208 int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] +
5209 ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] +
5210 ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]);
5211
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305212 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
5213 ++ifilter) {
5214 int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30;
5215 if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) {
5216 int filter_score =
5217 get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 +
5218 get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 +
5219 get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 +
5220 get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 +
5221 get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 +
5222 get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10;
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305223 if (filter_score < ref_total_total) {
5224 DUAL_FILTER_TYPE filt_type = ifilter + SWITCHABLE_FILTERS * ifilter;
5225 reset_interp_filter_allowed_mask(&mask, filt_type);
5226 }
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305227 }
5228 }
5229 return mask;
5230}
5231
Cheng Chen92a926a2020-01-30 17:57:33 -08005232#if !CONFIG_REALTIME_ONLY
chiyotsai155e2862020-03-09 15:29:34 -07005233#define STRICT_PSNR_DIFF_THRESH 0.9
Cheng Chen92a926a2020-01-30 17:57:33 -08005234// Encode key frame with/without screen content tools to determine whether
5235// screen content tools should be enabled for this key frame group or not.
5236// The first encoding is without screen content tools.
5237// The second encoding is with screen content tools.
5238// We compare the psnr and frame size to make the decision.
5239static void screen_content_tools_determination(
5240 AV1_COMP *cpi, const int allow_screen_content_tools_orig_decision,
5241 const int allow_intrabc_orig_decision,
5242 const int is_screen_content_type_orig_decision, const int pass,
5243 int *projected_size_pass, PSNR_STATS *psnr) {
5244 AV1_COMMON *const cm = &cpi->common;
Urvang Joshib6409e92020-03-23 11:23:27 -07005245 FeatureFlags *const features = &cm->features;
Cheng Chen92a926a2020-01-30 17:57:33 -08005246 projected_size_pass[pass] = cpi->rc.projected_frame_size;
5247#if CONFIG_AV1_HIGHBITDEPTH
5248 const uint32_t in_bit_depth = cpi->oxcf.input_bit_depth;
5249 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
5250 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr[pass],
5251 bit_depth, in_bit_depth);
5252#else
5253 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr[pass]);
5254#endif
5255 if (pass != 1) return;
5256
5257 const double psnr_diff = psnr[1].psnr[0] - psnr[0].psnr[0];
5258 const int is_sc_encoding_much_better = psnr_diff > STRICT_PSNR_DIFF_THRESH;
5259 if (is_sc_encoding_much_better) {
5260 // Use screen content tools, if we get coding gain.
Urvang Joshib6409e92020-03-23 11:23:27 -07005261 features->allow_screen_content_tools = 1;
5262 features->allow_intrabc = cpi->intrabc_used;
Cheng Chen92a926a2020-01-30 17:57:33 -08005263 cpi->is_screen_content_type = 1;
5264 } else {
5265 // Use original screen content decision.
Urvang Joshib6409e92020-03-23 11:23:27 -07005266 features->allow_screen_content_tools =
5267 allow_screen_content_tools_orig_decision;
5268 features->allow_intrabc = allow_intrabc_orig_decision;
Cheng Chen92a926a2020-01-30 17:57:33 -08005269 cpi->is_screen_content_type = is_screen_content_type_orig_decision;
5270 }
5271}
5272
Cheng Chenb8c938b2020-03-23 11:30:18 -07005273// Set some encoding parameters to make the encoding process fast.
5274// A fixed block partition size, and a large q is used.
5275static void set_encoding_params_for_screen_content(AV1_COMP *cpi,
5276 const int pass) {
Cheng Chen92a926a2020-01-30 17:57:33 -08005277 AV1_COMMON *const cm = &cpi->common;
5278 if (pass == 0) {
5279 // In the first pass, encode without screen content tools.
5280 // Use a high q, and a fixed block size for fast encoding.
Urvang Joshib6409e92020-03-23 11:23:27 -07005281 cm->features.allow_screen_content_tools = 0;
5282 cm->features.allow_intrabc = 0;
Cheng Chen92a926a2020-01-30 17:57:33 -08005283 cpi->is_screen_content_type = 0;
5284 cpi->sf.part_sf.partition_search_type = FIXED_PARTITION;
5285 cpi->sf.part_sf.always_this_block_size = BLOCK_32X32;
Cheng Chenb8c938b2020-03-23 11:30:18 -07005286 return;
Cheng Chen92a926a2020-01-30 17:57:33 -08005287 }
Cheng Chenb8c938b2020-03-23 11:30:18 -07005288 assert(pass == 1);
5289 // In the second pass, encode with screen content tools.
5290 // Use a high q, and a fixed block size for fast encoding.
Urvang Joshib6409e92020-03-23 11:23:27 -07005291 cm->features.allow_screen_content_tools = 1;
Cheng Chenb8c938b2020-03-23 11:30:18 -07005292 // TODO(chengchen): turn intrabc on could lead to data race issue.
5293 // cm->allow_intrabc = 1;
5294 cpi->is_screen_content_type = 1;
5295 cpi->sf.part_sf.partition_search_type = FIXED_PARTITION;
5296 cpi->sf.part_sf.always_this_block_size = BLOCK_32X32;
Cheng Chen92a926a2020-01-30 17:57:33 -08005297}
5298
5299// Determines whether to use screen content tools for the key frame group.
Urvang Joshib6409e92020-03-23 11:23:27 -07005300// This function modifies "cm->features.allow_screen_content_tools",
5301// "cm->features.allow_intrabc" and "cpi->is_screen_content_type".
Cheng Chen92a926a2020-01-30 17:57:33 -08005302static void determine_sc_tools_with_encoding(AV1_COMP *cpi, const int q_orig) {
Cheng Chen92a926a2020-01-30 17:57:33 -08005303 AV1_COMMON *const cm = &cpi->common;
5304 // Variables to help determine if we should allow screen content tools.
5305 int projected_size_pass[3] = { 0 };
5306 PSNR_STATS psnr[3];
5307 const int is_key_frame = cm->current_frame.frame_type == KEY_FRAME;
5308 const int allow_screen_content_tools_orig_decision =
Urvang Joshib6409e92020-03-23 11:23:27 -07005309 cm->features.allow_screen_content_tools;
5310 const int allow_intrabc_orig_decision = cm->features.allow_intrabc;
Cheng Chen92a926a2020-01-30 17:57:33 -08005311 const int is_screen_content_type_orig_decision = cpi->is_screen_content_type;
5312 // Turn off the encoding trial for forward key frame and superres.
5313 if (cpi->sf.rt_sf.use_nonrd_pick_mode || cpi->oxcf.fwd_kf_enabled ||
Urvang Joshi9d2606c2020-04-14 15:58:01 -07005314 cpi->superres_mode != AOM_SUPERRES_NONE || cpi->oxcf.mode == REALTIME ||
Cheng Chen92a926a2020-01-30 17:57:33 -08005315 is_screen_content_type_orig_decision || !is_key_frame) {
5316 return;
5317 }
5318
5319 // TODO(chengchen): multiple encoding for the lossless mode is time consuming.
5320 // Find a better way to determine whether screen content tools should be used
5321 // for lossless coding.
5322 // Use a high q and a fixed partition to do quick encoding.
5323 const int q_for_screen_content_quick_run =
5324 is_lossless_requested(&cpi->oxcf) ? q_orig : AOMMAX(q_orig, 244);
5325 const int partition_search_type_orig = cpi->sf.part_sf.partition_search_type;
5326 const BLOCK_SIZE fixed_partition_block_size_orig =
5327 cpi->sf.part_sf.always_this_block_size;
5328
5329 // Setup necessary params for encoding, including frame source, etc.
chiyotsaie6ae98e2020-03-23 13:17:08 -07005330 aom_clear_system_state();
Cheng Chen92a926a2020-01-30 17:57:33 -08005331
chiyotsaie6ae98e2020-03-23 13:17:08 -07005332 cpi->source =
5333 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
5334 if (cpi->unscaled_last_source != NULL) {
5335 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
5336 &cpi->scaled_last_source);
Cheng Chen92a926a2020-01-30 17:57:33 -08005337 }
5338
chiyotsaie6ae98e2020-03-23 13:17:08 -07005339 setup_frame(cpi);
5340
5341 if (cm->seg.enabled) {
5342 if (!cm->seg.update_data && cm->prev_frame) {
5343 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
5344 cm->seg.enabled = cm->prev_frame->seg.enabled;
5345 } else {
5346 av1_calculate_segdata(&cm->seg);
5347 }
5348 } else {
5349 memset(&cm->seg, 0, sizeof(cm->seg));
5350 }
5351 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
5352 cm->cur_frame->seg.enabled = cm->seg.enabled;
5353
Cheng Chenb8c938b2020-03-23 11:30:18 -07005354 // The two encoding passes aim to help determine whether to use screen
5355 // content tools, with a high q and fixed partition.
Cheng Chen92a926a2020-01-30 17:57:33 -08005356 for (int pass = 0; pass < 2; ++pass) {
Cheng Chenb8c938b2020-03-23 11:30:18 -07005357 set_encoding_params_for_screen_content(cpi, pass);
Cheng Chen92a926a2020-01-30 17:57:33 -08005358#if CONFIG_TUNE_VMAF
5359 if (cpi->oxcf.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING ||
5360 cpi->oxcf.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
5361 cpi->oxcf.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
5362 av1_set_quantizer(
Deepa K Gc0d27832020-04-07 16:52:27 +05305363 cm, cpi->oxcf.qm_minlevel, cpi->oxcf.qm_maxlevel,
5364 av1_get_vmaf_base_qindex(cpi, q_for_screen_content_quick_run));
Cheng Chen92a926a2020-01-30 17:57:33 -08005365 } else {
5366#endif
Deepa K Gc0d27832020-04-07 16:52:27 +05305367 av1_set_quantizer(cm, cpi->oxcf.qm_minlevel, cpi->oxcf.qm_maxlevel,
5368 q_for_screen_content_quick_run);
Cheng Chen92a926a2020-01-30 17:57:33 -08005369#if CONFIG_TUNE_VMAF
5370 }
5371#endif
Debargha Mukherjee90b20752020-03-19 23:16:34 -07005372 av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
Deepa K Gc0d27832020-04-07 16:52:27 +05305373 if (cpi->oxcf.deltaq_mode != NO_DELTA_Q)
5374 av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
5375 cm->seq_params.bit_depth);
Cheng Chen92a926a2020-01-30 17:57:33 -08005376
5377 av1_set_variance_partition_thresholds(cpi, q_for_screen_content_quick_run,
5378 0);
5379 // transform / motion compensation build reconstruction frame
5380 av1_encode_frame(cpi);
5381 // Screen content decision
5382 screen_content_tools_determination(
5383 cpi, allow_screen_content_tools_orig_decision,
5384 allow_intrabc_orig_decision, is_screen_content_type_orig_decision, pass,
5385 projected_size_pass, psnr);
5386 }
5387
5388 // Set partition speed feature back.
5389 cpi->sf.part_sf.partition_search_type = partition_search_type_orig;
5390 cpi->sf.part_sf.always_this_block_size = fixed_partition_block_size_orig;
5391}
5392#endif // CONFIG_REALTIME_ONLY
5393
Tom Finegane4099e32018-01-23 12:01:51 -08005394static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005395 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005396 RATE_CONTROL *const rc = &cpi->rc;
Vishesh8ba8bfe2020-04-03 14:20:29 +05305397 GlobalMotionInfo *const gm_info = &cpi->gm_info;
chiyotsai6b430132019-12-18 10:33:51 -08005398 const int allow_recode = (cpi->sf.hl_sf.recode_loop != DISALLOW_RECODE);
Hui Sua1d71842019-07-31 12:02:24 -07005399 // Must allow recode if minimum compression ratio is set.
5400 assert(IMPLIES(cpi->oxcf.min_cr > 0, allow_recode));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005401
5402 set_size_independent_vars(cpi);
Hamsalekha S37cc1d12019-12-12 19:27:41 +05305403 if (is_stat_consumption_stage_twopass(cpi) &&
chiyotsaicbf1d112019-12-16 11:28:47 -08005404 cpi->sf.interp_sf.adaptive_interp_filter_search)
Jayasanker Je0bbb432020-04-08 18:16:18 +05305405 cpi->interp_search_flags.interp_filter_search_mask =
5406 setup_interp_filter_search_mask(cpi);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07005407 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07005408
David Turnerdedd8ff2019-01-23 13:59:46 +00005409 av1_setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005410
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005411#if CONFIG_SUPERRES_IN_RECODE
Urvang Joshi9d2606c2020-04-14 15:58:01 -07005412 if (superres_in_recode_allowed(cpi) &&
5413 cpi->superres_mode != AOM_SUPERRES_NONE &&
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005414 cm->superres_scale_denominator == SCALE_NUMERATOR) {
Urvang Joshidb4551e2020-04-08 12:30:21 -07005415 // Superres mode is currently enabled, but the denominator selected will
Urvang Joshif9c2bd72020-04-07 15:11:51 -07005416 // disable superres. So no need to continue, as we will go through another
5417 // recode loop for full-resolution after this anyway.
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005418 return -1;
5419 }
5420#endif // CONFIG_SUPERRES_IN_RECODE
5421
David Turner2f3b5df2019-01-02 14:30:50 +00005422 int top_index = 0, bottom_index = 0;
5423 int q = 0, q_low = 0, q_high = 0;
5424 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
5425 q_low = bottom_index;
5426 q_high = top_index;
Fyodor Kyslov166648a2020-03-18 16:15:27 -07005427 if (cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION) {
5428 const int num_64x64_blocks =
5429 (cm->seq_params.sb_size == BLOCK_64X64) ? 1 : 4;
5430 if (cpi->td.vt64x64) {
5431 if (num_64x64_blocks != cpi->td.num_64x64_blocks) {
5432 aom_free(cpi->td.vt64x64);
5433 cpi->td.vt64x64 = NULL;
5434 }
5435 }
5436 if (!cpi->td.vt64x64) {
5437 CHECK_MEM_ERROR(cm, cpi->td.vt64x64,
5438 aom_malloc(sizeof(*cpi->td.vt64x64) * num_64x64_blocks));
5439 cpi->td.num_64x64_blocks = num_64x64_blocks;
5440 }
5441 }
David Turner2f3b5df2019-01-02 14:30:50 +00005442
Vishesh2770b972020-04-08 13:49:55 +05305443 if (cm->current_frame.frame_type == KEY_FRAME) {
5444 FrameProbInfo *const frame_probs = &cpi->frame_probs;
5445
5446 if (cpi->sf.tx_sf.tx_type_search.prune_tx_type_using_stats) {
5447 av1_copy(frame_probs->tx_type_probs, default_tx_type_probs);
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005448 }
Vishesh2770b972020-04-08 13:49:55 +05305449
5450 if (!cpi->sf.inter_sf.disable_obmc &&
5451 cpi->sf.inter_sf.prune_obmc_prob_thresh > 0) {
5452 av1_copy(frame_probs->obmc_probs, default_obmc_probs);
5453 }
5454
5455 if (cpi->sf.inter_sf.prune_warped_prob_thresh > 0) {
5456 av1_copy(frame_probs->warped_probs, default_warped_probs);
5457 }
5458
5459 if (cpi->sf.interp_sf.adaptive_interp_filter_search == 2) {
5460 av1_copy(frame_probs->switchable_interp_probs,
5461 default_switchable_interp_probs);
Vishesh20771502020-04-06 15:37:15 +05305462 }
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005463 }
Cheng Chen92a926a2020-01-30 17:57:33 -08005464#if !CONFIG_REALTIME_ONLY
5465 // Determine whether to use screen content tools using two fast encoding.
5466 determine_sc_tools_with_encoding(cpi, q);
5467#endif // CONFIG_REALTIME_ONLY
5468
5469#if CONFIG_COLLECT_COMPONENT_TIMING
5470 printf("\n Encoding a frame:");
5471#endif
5472
David Turner2f3b5df2019-01-02 14:30:50 +00005473 // Loop variables
Cheng Chen92a926a2020-01-30 17:57:33 -08005474 int loop = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00005475 int loop_count = 0;
5476 int loop_at_this_size = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00005477 int overshoot_seen = 0;
5478 int undershoot_seen = 0;
Hui Suef139e12019-05-20 15:51:22 -07005479 int low_cr_seen = 0;
chiyotsai8b7cef82020-01-21 16:52:54 -08005480 int last_loop_allow_hp = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005481
Yaowu Xuc27fc142016-08-22 16:08:15 -07005482 do {
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005483 loop = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005484 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005485
Urvang Joshif1fa6862018-01-08 16:39:33 -08005486 // if frame was scaled calculate global_motion_search again if already
5487 // done
Vishesh8ba8bfe2020-04-03 14:20:29 +05305488 if (loop_count > 0 && cpi->source && gm_info->search_done) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005489 if (cpi->source->y_crop_width != cm->width ||
David Turner2f3b5df2019-01-02 14:30:50 +00005490 cpi->source->y_crop_height != cm->height) {
Vishesh8ba8bfe2020-04-03 14:20:29 +05305491 gm_info->search_done = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00005492 }
5493 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005494 cpi->source =
5495 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
David Turner2f3b5df2019-01-02 14:30:50 +00005496 if (cpi->unscaled_last_source != NULL) {
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07005497 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
5498 &cpi->scaled_last_source);
David Turner2f3b5df2019-01-02 14:30:50 +00005499 }
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07005500
David Turner2f3b5df2019-01-02 14:30:50 +00005501 if (!frame_is_intra_only(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005502 if (loop_count > 0) {
5503 release_scaled_references(cpi);
5504 }
Cheng Chen46f30c72017-09-07 11:13:33 -07005505 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005506 }
sdeng3cd9eec2020-01-23 15:49:50 -08005507#if CONFIG_TUNE_VMAF
5508 if (cpi->oxcf.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING ||
sdeng615dc242020-02-04 16:06:14 -08005509 cpi->oxcf.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
5510 cpi->oxcf.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
Deepa K Gc0d27832020-04-07 16:52:27 +05305511 av1_set_quantizer(cm, cpi->oxcf.qm_minlevel, cpi->oxcf.qm_maxlevel,
5512 av1_get_vmaf_base_qindex(cpi, q));
sdeng3cd9eec2020-01-23 15:49:50 -08005513 } else {
5514#endif
Deepa K Gc0d27832020-04-07 16:52:27 +05305515 av1_set_quantizer(cm, cpi->oxcf.qm_minlevel, cpi->oxcf.qm_maxlevel, q);
sdeng3cd9eec2020-01-23 15:49:50 -08005516#if CONFIG_TUNE_VMAF
5517 }
5518#endif
Debargha Mukherjee90b20752020-03-19 23:16:34 -07005519 av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
sdeng3cd9eec2020-01-23 15:49:50 -08005520
Deepa K Gc0d27832020-04-07 16:52:27 +05305521 if (cpi->oxcf.deltaq_mode != NO_DELTA_Q)
5522 av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
5523 cm->seq_params.bit_depth);
kyslov7b9d0d62018-12-21 11:12:26 -08005524
5525 av1_set_variance_partition_thresholds(cpi, q, 0);
5526
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08005527 // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
5528 // cm->current_frame.frame_number, cm->show_frame, q,
5529 // cm->current_frame.frame_type, cm->superres_scale_denominator);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005530
David Turner2f3b5df2019-01-02 14:30:50 +00005531 if (loop_count == 0) {
5532 setup_frame(cpi);
5533 } else if (get_primary_ref_frame_buf(cm) == NULL) {
5534 // Base q-index may have changed, so we need to assign proper default coef
5535 // probs before every iteration.
Yaowu Xuf883b422016-08-30 14:01:10 -07005536 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08005537 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00005538 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005539
Yaowu Xuc27fc142016-08-22 16:08:15 -07005540 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005541 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005542 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005543 av1_setup_in_frame_q_adj(cpi);
David Turner2f3b5df2019-01-02 14:30:50 +00005544 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !allow_recode) {
5545 suppress_active_map(cpi);
5546 av1_cyclic_refresh_setup(cpi);
5547 apply_active_map(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005548 }
David Turner2f3b5df2019-01-02 14:30:50 +00005549
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005550 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00005551 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005552 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
Jerome Jiangb23571d2019-10-23 14:15:55 -07005553 cm->seg.enabled = cm->prev_frame->seg.enabled;
David Barker11c93562018-06-05 12:00:07 +01005554 } else {
Yaowu Xu7e450882019-04-30 15:09:18 -07005555 av1_calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07005556 }
David Barkercab37552018-03-21 11:56:24 +00005557 } else {
5558 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005559 }
David Barkercab37552018-03-21 11:56:24 +00005560 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Jerome Jiangb23571d2019-10-23 14:15:55 -07005561 cm->cur_frame->seg.enabled = cm->seg.enabled;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005562
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005563#if CONFIG_COLLECT_COMPONENT_TIMING
5564 start_timing(cpi, av1_encode_frame_time);
5565#endif
chiyotsai8b7cef82020-01-21 16:52:54 -08005566 // Set the motion vector precision based on mv stats from the last coded
5567 // frame.
5568 if (!frame_is_intra_only(cm)) {
5569 av1_pick_and_set_high_precision_mv(cpi, q);
5570
5571 // If the precision has changed during different iteration of the loop,
5572 // then we need to reset the global motion vectors
Urvang Joshib6409e92020-03-23 11:23:27 -07005573 if (loop_count > 0 &&
5574 cm->features.allow_high_precision_mv != last_loop_allow_hp) {
Vishesh8ba8bfe2020-04-03 14:20:29 +05305575 gm_info->search_done = 0;
chiyotsai8b7cef82020-01-21 16:52:54 -08005576 }
Urvang Joshib6409e92020-03-23 11:23:27 -07005577 last_loop_allow_hp = cm->features.allow_high_precision_mv;
chiyotsai8b7cef82020-01-21 16:52:54 -08005578 }
chiyotsaic666b1f2019-12-20 10:44:58 -08005579
Yaowu Xuc27fc142016-08-22 16:08:15 -07005580 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07005581 av1_encode_frame(cpi);
chiyotsai51bd6482019-12-20 10:49:34 -08005582#if !CONFIG_REALTIME_ONLY
5583 // Reset the mv_stats in case we are interrupted by an intraframe or an
5584 // overlay frame.
5585 if (cpi->mv_stats.valid) {
5586 av1_zero(cpi->mv_stats);
5587 }
5588 // Gather the mv_stats for the next frame
5589 if (cpi->sf.hl_sf.high_precision_mv_usage == LAST_MV_DATA &&
5590 av1_frame_allows_smart_mv(cpi)) {
5591 av1_collect_mv_stats(cpi, q);
5592 }
5593#endif // !CONFIG_REALTIME_ONLY
5594
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005595#if CONFIG_COLLECT_COMPONENT_TIMING
5596 end_timing(cpi, av1_encode_frame_time);
5597#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005598
Yaowu Xuf883b422016-08-30 14:01:10 -07005599 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005600
5601 // Dummy pack of the bitstream using up to date stats to get an
5602 // accurate estimate of output frame size to determine if we need
5603 // to recode.
chiyotsai6b430132019-12-18 10:33:51 -08005604 const int do_dummy_pack =
5605 (cpi->sf.hl_sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
5606 cpi->oxcf.rc_mode != AOM_Q) ||
5607 cpi->oxcf.min_cr > 0;
Hui Sua1d71842019-07-31 12:02:24 -07005608 if (do_dummy_pack) {
David Turner996b2c12018-12-07 15:52:30 +00005609 finalize_encoded_frame(cpi);
David Turner35cba132018-12-10 15:48:15 +00005610 int largest_tile_id = 0; // Output from bitstream: unused here
chiyotsaic666b1f2019-12-20 10:44:58 -08005611 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) !=
5612 AOM_CODEC_OK) {
Tom Finegane4099e32018-01-23 12:01:51 -08005613 return AOM_CODEC_ERROR;
chiyotsaic666b1f2019-12-20 10:44:58 -08005614 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005615
5616 rc->projected_frame_size = (int)(*size) << 3;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005617 }
5618
Hui Suef139e12019-05-20 15:51:22 -07005619 if (allow_recode) {
David Turner2f3b5df2019-01-02 14:30:50 +00005620 // Update q and decide whether to do a recode loop
5621 recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
5622 bottom_index, &undershoot_seen, &overshoot_seen,
Hui Suef139e12019-05-20 15:51:22 -07005623 &low_cr_seen, loop_at_this_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005624 }
5625
5626 // Special case for overlay frame.
Hui Su7e3eeaa2019-09-11 15:50:41 -07005627 if (loop && rc->is_src_frame_alt_ref &&
5628 rc->projected_frame_size < rc->max_frame_bandwidth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005629 loop = 0;
Hui Su7e3eeaa2019-09-11 15:50:41 -07005630 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005631
chiyotsai76cd2652019-12-18 10:26:36 -08005632 if (allow_recode && !cpi->sf.gm_sf.gm_disable_recode &&
Vishesh8ba8bfe2020-04-03 14:20:29 +05305633 recode_loop_test_global_motion(cm->global_motion,
5634 cpi->td.rd_counts.global_motion_used,
5635 gm_info->params_cost)) {
David Turner2f3b5df2019-01-02 14:30:50 +00005636 loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005637 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005638
Yaowu Xuc27fc142016-08-22 16:08:15 -07005639 if (loop) {
5640 ++loop_count;
5641 ++loop_at_this_size;
5642
5643#if CONFIG_INTERNAL_STATS
5644 ++cpi->tot_recode_hits;
5645#endif
5646 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005647#if CONFIG_COLLECT_COMPONENT_TIMING
5648 if (loop) printf("\n Recoding:");
5649#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005650 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08005651
Marco Paniconi574e59e2019-10-23 15:47:28 -07005652 // Update some stats from cyclic refresh.
Marco Paniconi51415512019-11-27 12:38:24 -08005653 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !frame_is_intra_only(cm))
Marco Paniconi574e59e2019-10-23 15:47:28 -07005654 av1_cyclic_refresh_postencode(cpi);
5655
Tom Finegane4099e32018-01-23 12:01:51 -08005656 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005657}
5658
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005659static int encode_with_recode_loop_and_filter(AV1_COMP *cpi, size_t *size,
5660 uint8_t *dest, int64_t *sse,
5661 int64_t *rate,
5662 int *largest_tile_id) {
5663#if CONFIG_COLLECT_COMPONENT_TIMING
5664 start_timing(cpi, encode_with_recode_loop_time);
5665#endif
5666 int err = encode_with_recode_loop(cpi, size, dest);
5667#if CONFIG_COLLECT_COMPONENT_TIMING
5668 end_timing(cpi, encode_with_recode_loop_time);
5669#endif
5670 if (err != AOM_CODEC_OK) {
5671 if (err == -1) {
5672 // special case as described in encode_with_recode_loop().
5673 // Encoding was skipped.
5674 err = AOM_CODEC_OK;
5675 if (sse != NULL) *sse = INT64_MAX;
5676 if (rate != NULL) *rate = INT64_MAX;
5677 *largest_tile_id = 0;
5678 }
5679 return err;
5680 }
5681
5682#ifdef OUTPUT_YUV_SKINMAP
5683 if (cpi->common.current_frame.frame_number > 1) {
5684 av1_compute_skin_map(cpi, yuv_skinmap_file);
5685 }
5686#endif // OUTPUT_YUV_SKINMAP
5687
5688 AV1_COMMON *const cm = &cpi->common;
5689 SequenceHeader *const seq_params = &cm->seq_params;
5690
5691 // Special case code to reduce pulsing when key frames are forced at a
5692 // fixed interval. Note the reconstruction error if it is the frame before
5693 // the force key frame
5694 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005695#if CONFIG_AV1_HIGHBITDEPTH
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005696 if (seq_params->use_highbitdepth) {
5697 cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
5698 } else {
5699 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5700 }
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005701#else
5702 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5703#endif
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005704 }
5705
5706 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
5707 cm->cur_frame->buf.transfer_characteristics =
5708 seq_params->transfer_characteristics;
5709 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
5710 cm->cur_frame->buf.monochrome = seq_params->monochrome;
5711 cm->cur_frame->buf.chroma_sample_position =
5712 seq_params->chroma_sample_position;
5713 cm->cur_frame->buf.color_range = seq_params->color_range;
5714 cm->cur_frame->buf.render_width = cm->render_width;
5715 cm->cur_frame->buf.render_height = cm->render_height;
5716
5717 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5718 // off.
5719
5720 // Pick the loop filter level for the frame.
Urvang Joshib6409e92020-03-23 11:23:27 -07005721 if (!cm->features.allow_intrabc) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005722 loopfilter_frame(cpi, cm);
5723 } else {
5724 cm->lf.filter_level[0] = 0;
5725 cm->lf.filter_level[1] = 0;
5726 cm->cdef_info.cdef_bits = 0;
5727 cm->cdef_info.cdef_strengths[0] = 0;
5728 cm->cdef_info.nb_cdef_strengths = 1;
5729 cm->cdef_info.cdef_uv_strengths[0] = 0;
5730 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5731 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5732 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
5733 }
5734
5735 // TODO(debargha): Fix mv search range on encoder side
5736 // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5737 aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5738
5739#ifdef OUTPUT_YUV_REC
5740 aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
5741#endif
5742
5743 finalize_encoded_frame(cpi);
5744 // Build the bitstream
5745#if CONFIG_COLLECT_COMPONENT_TIMING
5746 start_timing(cpi, av1_pack_bitstream_final_time);
5747#endif
5748 if (av1_pack_bitstream(cpi, dest, size, largest_tile_id) != AOM_CODEC_OK)
5749 return AOM_CODEC_ERROR;
5750#if CONFIG_COLLECT_COMPONENT_TIMING
5751 end_timing(cpi, av1_pack_bitstream_final_time);
5752#endif
5753
5754 // Compute sse and rate.
5755 if (sse != NULL) {
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005756#if CONFIG_AV1_HIGHBITDEPTH
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005757 *sse = (seq_params->use_highbitdepth)
5758 ? aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf)
5759 : aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005760#else
5761 *sse = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5762#endif
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005763 }
5764 if (rate != NULL) {
5765 const int64_t bits = (*size << 3);
5766 *rate = (bits << 5); // To match scale.
5767 }
5768 return AOM_CODEC_OK;
5769}
5770
5771#if CONFIG_SUPERRES_IN_RECODE
5772
5773static void save_cur_buf(AV1_COMP *cpi) {
5774 CODING_CONTEXT *const cc = &cpi->coding_context;
5775 AV1_COMMON *cm = &cpi->common;
5776 const YV12_BUFFER_CONFIG *ybf = &cm->cur_frame->buf;
5777 memset(&cc->copy_buffer, 0, sizeof(cc->copy_buffer));
5778 if (aom_alloc_frame_buffer(&cc->copy_buffer, ybf->y_crop_width,
5779 ybf->y_crop_height, ybf->subsampling_x,
5780 ybf->subsampling_y,
5781 ybf->flags & YV12_FLAG_HIGHBITDEPTH, ybf->border,
Urvang Joshi6237b882020-03-26 15:02:26 -07005782 cm->features.byte_alignment) != AOM_CODEC_OK) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005783 aom_internal_error(
5784 &cm->error, AOM_CODEC_MEM_ERROR,
5785 "Failed to allocate copy buffer for saving coding context");
5786 }
5787 aom_yv12_copy_frame(ybf, &cc->copy_buffer, av1_num_planes(cm));
5788}
5789
5790// Coding context that only needs to be saved when recode loop includes
5791// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5792// restoraton).
5793static void save_extra_coding_context(AV1_COMP *cpi) {
5794 CODING_CONTEXT *const cc = &cpi->coding_context;
5795 AV1_COMMON *cm = &cpi->common;
5796
5797 cc->lf = cm->lf;
5798 cc->cdef_info = cm->cdef_info;
5799 cc->rc = cpi->rc;
5800}
5801
5802static void save_all_coding_context(AV1_COMP *cpi) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005803 save_cur_buf(cpi);
5804 save_extra_coding_context(cpi);
5805 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5806}
5807
5808static void restore_cur_buf(AV1_COMP *cpi) {
5809 CODING_CONTEXT *const cc = &cpi->coding_context;
5810 AV1_COMMON *cm = &cpi->common;
5811 aom_yv12_copy_frame(&cc->copy_buffer, &cm->cur_frame->buf,
5812 av1_num_planes(cm));
5813}
5814
5815// Coding context that only needs to be restored when recode loop includes
5816// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5817// restoraton).
5818static void restore_extra_coding_context(AV1_COMP *cpi) {
5819 CODING_CONTEXT *const cc = &cpi->coding_context;
5820 AV1_COMMON *cm = &cpi->common;
5821 cm->lf = cc->lf;
5822 cm->cdef_info = cc->cdef_info;
5823 cpi->rc = cc->rc;
5824}
5825
5826static void restore_all_coding_context(AV1_COMP *cpi) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005827 restore_cur_buf(cpi);
5828 restore_extra_coding_context(cpi);
5829 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5830}
5831
Urvang Joshibd6abad2020-04-08 01:00:18 -07005832static void release_copy_buffer(CODING_CONTEXT *cc) {
5833 aom_free_frame_buffer(&cc->copy_buffer);
5834}
5835
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005836static int encode_with_and_without_superres(AV1_COMP *cpi, size_t *size,
5837 uint8_t *dest,
5838 int *largest_tile_id) {
5839 const AV1_COMMON *const cm = &cpi->common;
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005840 assert(cm->seq_params.enable_superres);
5841 assert(superres_in_recode_allowed(cpi));
5842 aom_codec_err_t err = AOM_CODEC_OK;
5843 save_all_coding_context(cpi);
5844
5845 // Encode with superres.
Urvang Joshibc82d382019-11-01 17:59:20 -07005846#if SUPERRES_RECODE_ALL_RATIOS
Urvang Joshif9c2bd72020-04-07 15:11:51 -07005847 AV1EncoderConfig *const oxcf = &cpi->oxcf;
Urvang Joshibc82d382019-11-01 17:59:20 -07005848 int64_t superres_sses[SCALE_NUMERATOR];
5849 int64_t superres_rates[SCALE_NUMERATOR];
5850 int superres_largest_tile_ids[SCALE_NUMERATOR];
5851 // Use superres for Key-frames and Alt-ref frames only.
5852 const GF_GROUP *const gf_group = &cpi->gf_group;
5853 if (gf_group->update_type[gf_group->index] != OVERLAY_UPDATE &&
5854 gf_group->update_type[gf_group->index] != INTNL_OVERLAY_UPDATE) {
5855 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
5856 ++denom) {
5857 oxcf->superres_scale_denominator = denom;
5858 oxcf->superres_kf_scale_denominator = denom;
5859 const int this_index = denom - (SCALE_NUMERATOR + 1);
5860 err = encode_with_recode_loop_and_filter(
5861 cpi, size, dest, &superres_sses[this_index],
5862 &superres_rates[this_index], &superres_largest_tile_ids[this_index]);
5863 if (err != AOM_CODEC_OK) return err;
5864 restore_all_coding_context(cpi);
5865 }
5866 // Reset.
5867 oxcf->superres_scale_denominator = SCALE_NUMERATOR;
5868 oxcf->superres_kf_scale_denominator = SCALE_NUMERATOR;
5869 } else {
5870 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
5871 ++denom) {
5872 const int this_index = denom - (SCALE_NUMERATOR + 1);
5873 superres_sses[this_index] = INT64_MAX;
5874 superres_rates[this_index] = INT64_MAX;
5875 }
5876 }
5877#else
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005878 int64_t sse1 = INT64_MAX;
5879 int64_t rate1 = INT64_MAX;
5880 int largest_tile_id1;
5881 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse1, &rate1,
5882 &largest_tile_id1);
5883 if (err != AOM_CODEC_OK) return err;
Urvang Joshibc82d382019-11-01 17:59:20 -07005884 restore_all_coding_context(cpi);
5885#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005886
5887 // Encode without superres.
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005888 int64_t sse2 = INT64_MAX;
5889 int64_t rate2 = INT64_MAX;
5890 int largest_tile_id2;
Urvang Joshi9d2606c2020-04-14 15:58:01 -07005891 cpi->superres_mode = AOM_SUPERRES_NONE; // To force full-res.
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005892 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
5893 &largest_tile_id2);
Urvang Joshif9c2bd72020-04-07 15:11:51 -07005894 cpi->superres_mode = cpi->oxcf.superres_mode; // Reset.
Urvang Joshi9d2606c2020-04-14 15:58:01 -07005895 assert(cpi->oxcf.superres_mode == AOM_SUPERRES_AUTO);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005896 if (err != AOM_CODEC_OK) return err;
5897
5898 // Note: Both use common rdmult based on base qindex of fullres.
5899 const int64_t rdmult =
Urvang Joshi17814622020-03-27 17:26:17 -07005900 av1_compute_rd_mult_based_on_qindex(cpi, cm->quant_params.base_qindex);
Urvang Joshibc82d382019-11-01 17:59:20 -07005901
5902#if SUPERRES_RECODE_ALL_RATIOS
5903 // Find the best rdcost among all superres denoms.
5904 double proj_rdcost1 = DBL_MAX;
5905 int64_t sse1 = INT64_MAX;
5906 int64_t rate1 = INT64_MAX;
5907 int largest_tile_id1 = 0;
5908 (void)sse1;
5909 (void)rate1;
5910 (void)largest_tile_id1;
5911 int best_denom = -1;
5912 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR; ++denom) {
5913 const int this_index = denom - (SCALE_NUMERATOR + 1);
5914 const int64_t this_sse = superres_sses[this_index];
5915 const int64_t this_rate = superres_rates[this_index];
5916 const int this_largest_tile_id = superres_largest_tile_ids[this_index];
5917 const double this_rdcost = RDCOST_DBL(rdmult, this_rate, this_sse);
5918 if (this_rdcost < proj_rdcost1) {
5919 sse1 = this_sse;
5920 rate1 = this_rate;
5921 largest_tile_id1 = this_largest_tile_id;
5922 proj_rdcost1 = this_rdcost;
5923 best_denom = denom;
5924 }
5925 }
5926#else
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005927 const double proj_rdcost1 = RDCOST_DBL(rdmult, rate1, sse1);
Urvang Joshibc82d382019-11-01 17:59:20 -07005928#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005929 const double proj_rdcost2 = RDCOST_DBL(rdmult, rate2, sse2);
5930
5931 // Re-encode with superres if it's better.
5932 if (proj_rdcost1 < proj_rdcost2) {
5933 restore_all_coding_context(cpi);
5934 // TODO(urvang): We should avoid rerunning the recode loop by saving
5935 // previous output+state, or running encode only for the selected 'q' in
5936 // previous step.
Urvang Joshibc82d382019-11-01 17:59:20 -07005937#if SUPERRES_RECODE_ALL_RATIOS
5938 // Again, temporarily force the best denom.
5939 oxcf->superres_scale_denominator = best_denom;
5940 oxcf->superres_kf_scale_denominator = best_denom;
5941#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005942 int64_t sse3 = INT64_MAX;
5943 int64_t rate3 = INT64_MAX;
5944 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
5945 largest_tile_id);
5946 assert(sse1 == sse3);
5947 assert(rate1 == rate3);
5948 assert(largest_tile_id1 == *largest_tile_id);
Urvang Joshibc82d382019-11-01 17:59:20 -07005949#if SUPERRES_RECODE_ALL_RATIOS
5950 // Reset.
5951 oxcf->superres_scale_denominator = SCALE_NUMERATOR;
5952 oxcf->superres_kf_scale_denominator = SCALE_NUMERATOR;
5953#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005954 } else {
5955 *largest_tile_id = largest_tile_id2;
5956 }
5957
Urvang Joshibd6abad2020-04-08 01:00:18 -07005958 release_copy_buffer(&cpi->coding_context);
5959
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005960 return err;
5961}
5962#endif // CONFIG_SUPERRES_IN_RECODE
5963
Yaowu Xuc27fc142016-08-22 16:08:15 -07005964#define DUMP_RECON_FRAMES 0
5965
5966#if DUMP_RECON_FRAMES == 1
5967// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07005968static void dump_filtered_recon_frames(AV1_COMP *cpi) {
5969 AV1_COMMON *const cm = &cpi->common;
David Turnerd2a592e2018-11-16 14:59:31 +00005970 const CurrentFrame *const current_frame = &cm->current_frame;
David Turnerc29e1a92018-12-06 14:10:14 +00005971 const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005972
Zoe Liub4f31032017-11-03 23:48:35 -07005973 if (recon_buf == NULL) {
David Turnerd2a592e2018-11-16 14:59:31 +00005974 printf("Frame %d is not ready.\n", current_frame->frame_number);
Zoe Liub4f31032017-11-03 23:48:35 -07005975 return;
5976 }
5977
Zoe Liu27deb382018-03-27 15:13:56 -07005978 static const int flag_list[REF_FRAMES] = { 0,
5979 AOM_LAST_FLAG,
5980 AOM_LAST2_FLAG,
5981 AOM_LAST3_FLAG,
5982 AOM_GOLD_FLAG,
5983 AOM_BWD_FLAG,
5984 AOM_ALT2_FLAG,
5985 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07005986 printf(
5987 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
5988 "show_existing_frame=%d) "
5989 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
David Turnerd2a592e2018-11-16 14:59:31 +00005990 current_frame->frame_number, current_frame->order_hint, cm->show_frame,
Zoe Liub4f31032017-11-03 23:48:35 -07005991 cm->show_existing_frame);
5992 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00005993 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
5994 const int ref_offset = buf != NULL ? (int)buf->order_hint : -1;
Urvang Joshib6f17672019-03-05 11:51:02 -08005995 printf(" %d(%c)", ref_offset,
5996 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N');
Zoe Liub4f31032017-11-03 23:48:35 -07005997 }
5998 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07005999
6000 if (!cm->show_frame) {
6001 printf("Frame %d is a no show frame, so no image dump.\n",
David Turnerd2a592e2018-11-16 14:59:31 +00006002 current_frame->frame_number);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006003 return;
6004 }
6005
Zoe Liub4f31032017-11-03 23:48:35 -07006006 int h;
6007 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
6008 FILE *f_recon = NULL;
6009
David Turnerd2a592e2018-11-16 14:59:31 +00006010 if (current_frame->frame_number == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006011 if ((f_recon = fopen(file_name, "wb")) == NULL) {
6012 printf("Unable to open file %s to write.\n", file_name);
6013 return;
6014 }
6015 } else {
6016 if ((f_recon = fopen(file_name, "ab")) == NULL) {
6017 printf("Unable to open file %s to append.\n", file_name);
6018 return;
6019 }
6020 }
6021 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07006022 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
6023 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
Urvang Joshi7a890232019-03-22 17:00:31 -07006024 "refresh_alt_ref_frame=%d, "
Zoe Liuf40a9572017-10-13 12:37:19 -07006025 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Sarah Parkere1b22012019-06-06 16:35:25 -07006026 current_frame->frame_number, cpi->gf_group.index,
6027 cpi->gf_group.update_type[cpi->gf_group.index], current_frame->order_hint,
6028 cm->show_frame, cm->show_existing_frame, cpi->rc.source_alt_ref_active,
Jayasanker J24cb9bc2020-04-15 13:43:10 +05306029 cpi->refresh_frame.alt_ref_frame, recon_buf->y_stride,
6030 recon_buf->uv_stride, cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07006031#if 0
6032 int ref_frame;
6033 printf("get_ref_frame_map_idx: [");
6034 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
David Turnera21966b2018-12-05 14:48:49 +00006035 printf(" %d", get_ref_frame_map_idx(cm, ref_frame));
Zoe Liue9b15e22017-07-19 15:53:01 -07006036 printf(" ]\n");
Zoe Liue9b15e22017-07-19 15:53:01 -07006037#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07006038
6039 // --- Y ---
6040 for (h = 0; h < cm->height; ++h) {
6041 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
6042 f_recon);
6043 }
6044 // --- U ---
6045 for (h = 0; h < (cm->height >> 1); ++h) {
6046 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
6047 f_recon);
6048 }
6049 // --- V ---
6050 for (h = 0; h < (cm->height >> 1); ++h) {
6051 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
6052 f_recon);
6053 }
6054
6055 fclose(f_recon);
6056}
6057#endif // DUMP_RECON_FRAMES
6058
Vishesh2260a592020-04-06 15:34:51 +05306059static int is_integer_mv(const YV12_BUFFER_CONFIG *cur_picture,
6060 const YV12_BUFFER_CONFIG *last_picture,
6061 ForceIntegerMVInfo *const force_intpel_info) {
David Turnerefed6372019-01-11 15:14:11 +00006062 aom_clear_system_state();
6063 // check use hash ME
6064 int k;
David Turnerefed6372019-01-11 15:14:11 +00006065
Ranjit Kumar Tulabandu9f26c992019-10-24 12:24:52 +05306066 const int block_size = FORCE_INT_MV_DECISION_BLOCK_SIZE;
David Turnerefed6372019-01-11 15:14:11 +00006067 const double threshold_current = 0.8;
6068 const double threshold_average = 0.95;
6069 const int max_history_size = 32;
6070 int T = 0; // total block
6071 int C = 0; // match with collocated block
6072 int S = 0; // smooth region but not match with collocated block
David Turnerefed6372019-01-11 15:14:11 +00006073
6074 const int pic_width = cur_picture->y_width;
6075 const int pic_height = cur_picture->y_height;
6076 for (int i = 0; i + block_size <= pic_height; i += block_size) {
6077 for (int j = 0; j + block_size <= pic_width; j += block_size) {
6078 const int x_pos = j;
6079 const int y_pos = i;
6080 int match = 1;
6081 T++;
6082
6083 // check whether collocated block match with current
6084 uint8_t *p_cur = cur_picture->y_buffer;
6085 uint8_t *p_ref = last_picture->y_buffer;
6086 int stride_cur = cur_picture->y_stride;
6087 int stride_ref = last_picture->y_stride;
6088 p_cur += (y_pos * stride_cur + x_pos);
6089 p_ref += (y_pos * stride_ref + x_pos);
6090
6091 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
6092 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
6093 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
6094 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
6095 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
6096 if (p16_cur[tmpX] != p16_ref[tmpX]) {
6097 match = 0;
6098 }
6099 }
6100 p16_cur += stride_cur;
6101 p16_ref += stride_ref;
6102 }
6103 } else {
6104 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
6105 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
6106 if (p_cur[tmpX] != p_ref[tmpX]) {
6107 match = 0;
6108 }
6109 }
6110 p_cur += stride_cur;
6111 p_ref += stride_ref;
6112 }
6113 }
6114
6115 if (match) {
6116 C++;
6117 continue;
6118 }
6119
6120 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
6121 y_pos) ||
6122 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
6123 S++;
6124 continue;
6125 }
David Turnerefed6372019-01-11 15:14:11 +00006126 }
6127 }
6128
6129 assert(T > 0);
Remya163db3c2020-02-17 14:29:41 +05306130 double cs_rate = ((double)(C + S)) / ((double)(T));
David Turnerefed6372019-01-11 15:14:11 +00006131
Vishesh2260a592020-04-06 15:34:51 +05306132 force_intpel_info->cs_rate_array[force_intpel_info->rate_index] = cs_rate;
David Turnerefed6372019-01-11 15:14:11 +00006133
Vishesh2260a592020-04-06 15:34:51 +05306134 force_intpel_info->rate_index =
6135 (force_intpel_info->rate_index + 1) % max_history_size;
6136 force_intpel_info->rate_size++;
6137 force_intpel_info->rate_size =
6138 AOMMIN(force_intpel_info->rate_size, max_history_size);
David Turnerefed6372019-01-11 15:14:11 +00006139
Remya163db3c2020-02-17 14:29:41 +05306140 if (cs_rate < threshold_current) {
David Turnerefed6372019-01-11 15:14:11 +00006141 return 0;
6142 }
6143
6144 if (C == T) {
6145 return 1;
6146 }
6147
Remya163db3c2020-02-17 14:29:41 +05306148 double cs_average = 0.0;
David Turnerefed6372019-01-11 15:14:11 +00006149
Vishesh2260a592020-04-06 15:34:51 +05306150 for (k = 0; k < force_intpel_info->rate_size; k++) {
6151 cs_average += force_intpel_info->cs_rate_array[k];
David Turnerefed6372019-01-11 15:14:11 +00006152 }
Vishesh2260a592020-04-06 15:34:51 +05306153 cs_average /= force_intpel_info->rate_size;
David Turnerefed6372019-01-11 15:14:11 +00006154
Remya163db3c2020-02-17 14:29:41 +05306155 if (cs_average < threshold_average) {
David Turnerefed6372019-01-11 15:14:11 +00006156 return 0;
6157 }
6158
Remya163db3c2020-02-17 14:29:41 +05306159 if ((T - C - S) < 0) {
David Turnerefed6372019-01-11 15:14:11 +00006160 return 1;
6161 }
6162
Remya163db3c2020-02-17 14:29:41 +05306163 if (cs_average > 1.01) {
David Turnerefed6372019-01-11 15:14:11 +00006164 return 1;
6165 }
6166
6167 return 0;
6168}
6169
David Turner73245762019-02-11 16:42:34 +00006170// Refresh reference frame buffers according to refresh_frame_flags.
6171static void refresh_reference_frames(AV1_COMP *cpi) {
6172 AV1_COMMON *const cm = &cpi->common;
6173 // All buffers are refreshed for shown keyframes and S-frames.
6174
6175 for (int ref_frame = 0; ref_frame < REF_FRAMES; ref_frame++) {
6176 if (((cm->current_frame.refresh_frame_flags >> ref_frame) & 1) == 1) {
6177 assign_frame_buffer_p(&cm->ref_frame_map[ref_frame], cm->cur_frame);
6178 }
6179 }
6180}
6181
sdengc23c7f12019-06-11 16:56:50 -07006182static void set_mb_ssim_rdmult_scaling(AV1_COMP *cpi) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006183 const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
sdengc23c7f12019-06-11 16:56:50 -07006184 ThreadData *td = &cpi->td;
6185 MACROBLOCK *x = &td->mb;
6186 MACROBLOCKD *xd = &x->e_mbd;
6187 uint8_t *y_buffer = cpi->source->y_buffer;
6188 const int y_stride = cpi->source->y_stride;
6189 const int block_size = BLOCK_16X16;
6190
6191 const int num_mi_w = mi_size_wide[block_size];
6192 const int num_mi_h = mi_size_high[block_size];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006193 const int num_cols = (mi_params->mi_cols + num_mi_w - 1) / num_mi_w;
6194 const int num_rows = (mi_params->mi_rows + num_mi_h - 1) / num_mi_h;
sdengc23c7f12019-06-11 16:56:50 -07006195 double log_sum = 0.0;
sdengc23c7f12019-06-11 16:56:50 -07006196 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
6197
sdengc23c7f12019-06-11 16:56:50 -07006198 // Loop through each 16x16 block.
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006199 for (int row = 0; row < num_rows; ++row) {
6200 for (int col = 0; col < num_cols; ++col) {
sdengc23c7f12019-06-11 16:56:50 -07006201 double var = 0.0, num_of_var = 0.0;
6202 const int index = row * num_cols + col;
6203
6204 // Loop through each 8x8 block.
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006205 for (int mi_row = row * num_mi_h;
6206 mi_row < mi_params->mi_rows && mi_row < (row + 1) * num_mi_h;
6207 mi_row += 2) {
6208 for (int mi_col = col * num_mi_w;
6209 mi_col < mi_params->mi_cols && mi_col < (col + 1) * num_mi_w;
sdengc23c7f12019-06-11 16:56:50 -07006210 mi_col += 2) {
6211 struct buf_2d buf;
6212 const int row_offset_y = mi_row << 2;
6213 const int col_offset_y = mi_col << 2;
6214
6215 buf.buf = y_buffer + row_offset_y * y_stride + col_offset_y;
6216 buf.stride = y_stride;
6217
6218 if (use_hbd) {
6219 var += av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8,
6220 xd->bd);
6221 } else {
6222 var += av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8);
6223 }
6224
6225 num_of_var += 1.0;
6226 }
6227 }
sdeng32185d12019-06-19 14:47:09 -07006228 var = var / num_of_var;
sdengbdc0cd22019-11-11 15:09:34 -08006229
6230 // Curve fitting with an exponential model on all 16x16 blocks from the
6231 // midres dataset.
6232 var = 67.035434 * (1 - exp(-0.0021489 * var)) + 17.492222;
sdengc23c7f12019-06-11 16:56:50 -07006233 cpi->ssim_rdmult_scaling_factors[index] = var;
6234 log_sum += log(var);
6235 }
6236 }
6237 log_sum = exp(log_sum / (double)(num_rows * num_cols));
6238
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006239 for (int row = 0; row < num_rows; ++row) {
6240 for (int col = 0; col < num_cols; ++col) {
sdengc23c7f12019-06-11 16:56:50 -07006241 const int index = row * num_cols + col;
6242 cpi->ssim_rdmult_scaling_factors[index] /= log_sum;
6243 }
6244 }
sdengc23c7f12019-06-11 16:56:50 -07006245}
6246
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08006247extern void av1_print_frame_contexts(const FRAME_CONTEXT *fc,
6248 const char *filename);
6249
David Turner73245762019-02-11 16:42:34 +00006250static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
6251 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006252 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006253 SequenceHeader *const seq_params = &cm->seq_params;
David Turnerd2a592e2018-11-16 14:59:31 +00006254 CurrentFrame *const current_frame = &cm->current_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07006255 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006256 struct segmentation *const seg = &cm->seg;
Urvang Joshib6409e92020-03-23 11:23:27 -07006257 FeatureFlags *const features = &cm->features;
Thomas Davies4822e142017-10-10 11:30:36 +01006258
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006259#if CONFIG_COLLECT_COMPONENT_TIMING
6260 start_timing(cpi, encode_frame_to_data_rate_time);
6261#endif
6262
Fangwen Fu8d164de2016-12-14 13:40:54 -08006263 // frame type has been decided outside of this function call
David Turnerd2a592e2018-11-16 14:59:31 +00006264 cm->cur_frame->frame_type = current_frame->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07006265
Urvang Joshi54ffae72020-03-23 13:37:10 -07006266 cm->tiles.large_scale = cpi->oxcf.large_scale_tile;
6267 cm->tiles.single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wang9612d552018-05-15 14:58:30 -07006268
Urvang Joshib6409e92020-03-23 11:23:27 -07006269 features->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
6270 // features->allow_ref_frame_mvs needs to be written into the frame header
Urvang Joshi54ffae72020-03-23 13:37:10 -07006271 // while cm->tiles.large_scale is 1, therefore, "cm->tiles.large_scale=1" case
Urvang Joshib6409e92020-03-23 11:23:27 -07006272 // is separated from frame_might_allow_ref_frame_mvs().
Urvang Joshi54ffae72020-03-23 13:37:10 -07006273 features->allow_ref_frame_mvs &= !cm->tiles.large_scale;
Yunqing Wangd48fb162018-06-15 10:55:28 -07006274
Urvang Joshib6409e92020-03-23 11:23:27 -07006275 features->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07006276 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006277
Urvang Joshi1de67aa2020-03-20 11:21:57 -07006278 cpi->last_frame_type = current_frame->frame_type;
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05306279
Sarah Parker33005522018-07-27 14:46:25 -07006280 if (encode_show_existing_frame(cm)) {
David Turner996b2c12018-12-07 15:52:30 +00006281 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006282 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00006283 int largest_tile_id = 0; // Output from bitstream: unused here
6284 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08006285 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006286
David Turner90311862018-11-29 13:34:36 +00006287 if (seq_params->frame_id_numbers_present_flag &&
6288 current_frame->frame_type == KEY_FRAME) {
6289 // Displaying a forward key-frame, so reset the ref buffer IDs
6290 int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
6291 for (int i = 0; i < REF_FRAMES; i++)
6292 cm->ref_frame_id[i] = display_frame_id;
6293 }
6294
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07006295 cpi->seq_params_locked = 1;
6296
Yaowu Xuc27fc142016-08-22 16:08:15 -07006297#if DUMP_RECON_FRAMES == 1
6298 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
6299 dump_filtered_recon_frames(cpi);
6300#endif // DUMP_RECON_FRAMES
6301
David Turner73245762019-02-11 16:42:34 +00006302 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
6303 // for the purpose to verify no mismatch between encoder and decoder.
6304 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006305
David Turner73245762019-02-11 16:42:34 +00006306 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006307
Yaowu Xuc27fc142016-08-22 16:08:15 -07006308 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
6309 // to do post-encoding update accordingly.
6310 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07006311 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07006312 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006313 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006314
David Turnerd2a592e2018-11-16 14:59:31 +00006315 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006316
Tom Finegane4099e32018-01-23 12:01:51 -08006317 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006318 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006319
David Turnerefed6372019-01-11 15:14:11 +00006320 // Work out whether to force_integer_mv this frame
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306321 if (!is_stat_generation_stage(cpi) &&
Urvang Joshib6409e92020-03-23 11:23:27 -07006322 cpi->common.features.allow_screen_content_tools &&
6323 !frame_is_intra_only(cm)) {
David Turnerefed6372019-01-11 15:14:11 +00006324 if (cpi->common.seq_params.force_integer_mv == 2) {
6325 // Adaptive mode: see what previous frame encoded did
6326 if (cpi->unscaled_last_source != NULL) {
Vishesh2260a592020-04-06 15:34:51 +05306327 features->cur_frame_force_integer_mv = is_integer_mv(
6328 cpi->source, cpi->unscaled_last_source, &cpi->force_intpel_info);
David Turnerefed6372019-01-11 15:14:11 +00006329 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07006330 cpi->common.features.cur_frame_force_integer_mv = 0;
David Turnerefed6372019-01-11 15:14:11 +00006331 }
6332 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07006333 cpi->common.features.cur_frame_force_integer_mv =
David Turnerefed6372019-01-11 15:14:11 +00006334 cpi->common.seq_params.force_integer_mv;
6335 }
6336 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07006337 cpi->common.features.cur_frame_force_integer_mv = 0;
David Turnerefed6372019-01-11 15:14:11 +00006338 }
6339
Yaowu Xuc27fc142016-08-22 16:08:15 -07006340 // Set default state for segment based loop filter update flags.
6341 cm->lf.mode_ref_delta_update = 0;
6342
Yaowu Xuc27fc142016-08-22 16:08:15 -07006343 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05006344 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006345 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07006346 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006347
6348 // If segmentation is enabled force a map update for key frames.
6349 if (seg->enabled) {
6350 seg->update_map = 1;
6351 seg->update_data = 1;
6352 }
6353
6354 // The alternate reference frame cannot be active for a key frame.
6355 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006356 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00006357 if (cpi->oxcf.mtu == 0) {
Urvang Joshi1de67aa2020-03-20 11:21:57 -07006358 cpi->num_tg = cpi->oxcf.num_tile_groups;
Thomas Daviesaf6df172016-11-09 14:04:18 +00006359 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08006360 // Use a default value for the purposes of weighting costs in probability
6361 // updates
Urvang Joshi1de67aa2020-03-20 11:21:57 -07006362 cpi->num_tg = DEFAULT_MAX_NUM_TG;
Thomas Daviesaf6df172016-11-09 14:04:18 +00006363 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006364
6365 // For 1 pass CBR, check if we are dropping this frame.
6366 // Never drop on key frame.
Mufaddal Chakera3bcc72c2019-12-11 14:38:37 +05306367 if (has_no_stats_stage(cpi) && oxcf->rc_mode == AOM_CBR &&
David Turnerd2a592e2018-11-16 14:59:31 +00006368 current_frame->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006369 if (av1_rc_drop_frame(cpi)) {
6370 av1_rc_postencode_update_drop_frame(cpi);
David Turnera4c96252019-01-11 16:36:39 +00006371 release_scaled_references(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08006372 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006373 }
6374 }
6375
sdengc23c7f12019-06-11 16:56:50 -07006376 if (oxcf->tuning == AOM_TUNE_SSIM) set_mb_ssim_rdmult_scaling(cpi);
6377
sdeng01959162019-12-20 10:46:24 -08006378#if CONFIG_TUNE_VMAF
6379 if (oxcf->tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
sdeng615dc242020-02-04 16:06:14 -08006380 oxcf->tuning == AOM_TUNE_VMAF_MAX_GAIN) {
sdeng01959162019-12-20 10:46:24 -08006381 av1_set_mb_vmaf_rdmult_scaling(cpi);
sdeng29c62152019-12-09 12:44:18 -08006382 }
sdeng01959162019-12-20 10:46:24 -08006383#endif
sdeng29c62152019-12-09 12:44:18 -08006384
Yaowu Xuf883b422016-08-30 14:01:10 -07006385 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006386
6387#if CONFIG_INTERNAL_STATS
6388 memset(cpi->mode_chosen_counts, 0,
6389 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
6390#endif
6391
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006392 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006393 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08006394 * wraparound) */
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006395 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00006396 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08006397 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07006398 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
6399 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
6400 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00006401 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07006402 lsb = cpi->source->y_buffer[0] & 0xff;
6403 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00006404 }
David Turner760a2f42018-12-07 15:25:36 +00006405 cm->current_frame_id =
6406 ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05006407
6408 // S_frame is meant for stitching different streams of different
6409 // resolutions together, so current_frame_id must be the
6410 // same across different streams of the same content current_frame_id
6411 // should be the same and not random. 0x37 is a chosen number as start
6412 // point
6413 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006414 } else {
6415 cm->current_frame_id =
David Turner760a2f42018-12-07 15:25:36 +00006416 (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
6417 (1 << seq_params->frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006418 }
6419 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006420
Hui Su483a8452018-02-26 12:28:48 -08006421 switch (cpi->oxcf.cdf_update_mode) {
6422 case 0: // No CDF update for any frames(4~6% compression loss).
Urvang Joshib6409e92020-03-23 11:23:27 -07006423 features->disable_cdf_update = 1;
Hui Su483a8452018-02-26 12:28:48 -08006424 break;
6425 case 1: // Enable CDF update for all frames.
Urvang Joshib6409e92020-03-23 11:23:27 -07006426 features->disable_cdf_update = 0;
Hui Su483a8452018-02-26 12:28:48 -08006427 break;
6428 case 2:
6429 // Strategically determine at which frames to do CDF update.
6430 // Currently only enable CDF update for all-intra and no-show frames(1.5%
6431 // compression loss).
6432 // TODO(huisu@google.com): design schemes for various trade-offs between
6433 // compression quality and decoding speed.
Urvang Joshib6409e92020-03-23 11:23:27 -07006434 features->disable_cdf_update =
Hui Sub1b76b32018-02-27 15:24:48 -08006435 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08006436 break;
6437 }
Urvang Joshi450a9a22020-03-31 16:00:22 -07006438 seq_params->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08006439
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07006440 int largest_tile_id = 0;
6441#if CONFIG_SUPERRES_IN_RECODE
6442 if (superres_in_recode_allowed(cpi)) {
6443 if (encode_with_and_without_superres(cpi, size, dest, &largest_tile_id) !=
6444 AOM_CODEC_OK) {
6445 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006446 }
Hui Su06463e42018-02-23 22:17:36 -08006447 } else {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07006448#endif // CONFIG_SUPERRES_IN_RECODE
6449 if (encode_with_recode_loop_and_filter(cpi, size, dest, NULL, NULL,
6450 &largest_tile_id) != AOM_CODEC_OK) {
6451 return AOM_CODEC_ERROR;
6452 }
6453#if CONFIG_SUPERRES_IN_RECODE
Hui Su06463e42018-02-23 22:17:36 -08006454 }
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07006455#endif // CONFIG_SUPERRES_IN_RECODE
Yaowu Xuc27fc142016-08-22 16:08:15 -07006456
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07006457 cpi->seq_params_locked = 1;
6458
David Turner996b2c12018-12-07 15:52:30 +00006459 // Update reference frame ids for reference frames this frame will overwrite
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006460 if (seq_params->frame_id_numbers_present_flag) {
David Turner996b2c12018-12-07 15:52:30 +00006461 for (int i = 0; i < REF_FRAMES; i++) {
6462 if ((current_frame->refresh_frame_flags >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006463 cm->ref_frame_id[i] = cm->current_frame_id;
6464 }
6465 }
6466 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006467
Yaowu Xuc27fc142016-08-22 16:08:15 -07006468#if DUMP_RECON_FRAMES == 1
6469 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07006470 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006471#endif // DUMP_RECON_FRAMES
6472
Soo-Chul Han934af352017-10-15 15:21:51 -04006473 if (cm->seg.enabled) {
6474 if (cm->seg.update_map) {
6475 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07006476 } else if (cm->last_frame_seg_map) {
David Turnerb757ce02018-11-12 15:01:28 +00006477 memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006478 cm->mi_params.mi_cols * cm->mi_params.mi_rows * sizeof(uint8_t));
Soo-Chul Han934af352017-10-15 15:21:51 -04006479 }
6480 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006481
6482 if (frame_is_intra_only(cm) == 0) {
6483 release_scaled_references(cpi);
6484 }
6485
David Turner73245762019-02-11 16:42:34 +00006486 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
6487 // for the purpose to verify no mismatch between encoder and decoder.
6488 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
6489
6490 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006491
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08006492#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07006493 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08006494#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006495
Urvang Joshi6237b882020-03-26 15:02:26 -07006496 if (features->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
David Turner35cba132018-12-10 15:48:15 +00006497 *cm->fc = cpi->tile_data[largest_tile_id].tctx;
Hui Sudc54be62018-03-14 19:14:28 -07006498 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006499 }
Urvang Joshi54ffae72020-03-23 13:37:10 -07006500 if (!cm->tiles.large_scale) {
David Turnera4c96252019-01-11 16:36:39 +00006501 cm->cur_frame->frame_context = *cm->fc;
6502 }
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08006503
6504 if (cpi->oxcf.ext_tile_debug) {
6505 // (yunqing) This test ensures the correctness of large scale tile coding.
Urvang Joshi54ffae72020-03-23 13:37:10 -07006506 if (cm->tiles.large_scale && is_stat_consumption_stage(cpi)) {
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08006507 char fn[20] = "./fc";
6508 fn[4] = current_frame->frame_number / 100 + '0';
6509 fn[5] = (current_frame->frame_number % 100) / 10 + '0';
6510 fn[6] = (current_frame->frame_number % 10) + '0';
6511 fn[7] = '\0';
6512 av1_print_frame_contexts(cm->fc, fn);
6513 }
David Turnera4c96252019-01-11 16:36:39 +00006514 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006515
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006516#if CONFIG_COLLECT_COMPONENT_TIMING
6517 end_timing(cpi, encode_frame_to_data_rate_time);
6518
6519 // Print out timing information.
6520 int i;
chiyotsai9c484b32019-03-07 16:01:50 -08006521 fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n",
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006522 cm->current_frame.frame_number,
chiyotsai9c484b32019-03-07 16:01:50 -08006523 get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006524 for (i = 0; i < kTimingComponents; i++) {
6525 cpi->component_time[i] += cpi->frame_component_time[i];
6526 fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n",
6527 get_component_name(i), cpi->frame_component_time[i],
6528 cpi->component_time[i]);
6529 cpi->frame_component_time[i] = 0;
6530 }
6531#endif
6532
Urvang Joshi1de67aa2020-03-20 11:21:57 -07006533 cpi->last_frame_type = current_frame->frame_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006534
Yaowu Xuf883b422016-08-30 14:01:10 -07006535 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006536
Yaowu Xuc27fc142016-08-22 16:08:15 -07006537 // Clear the one shot update flags for segmentation map and mode/ref loop
6538 // filter deltas.
6539 cm->seg.update_map = 0;
6540 cm->seg.update_data = 0;
6541 cm->lf.mode_ref_delta_update = 0;
6542
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07006543 // A droppable frame might not be shown but it always
6544 // takes a space in the gf group. Therefore, even when
6545 // it is not shown, we still need update the count down.
6546
Yaowu Xuc27fc142016-08-22 16:08:15 -07006547 if (cm->show_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006548 // Don't increment frame counters if this was an altref buffer
6549 // update not a real frame
David Turnerd2a592e2018-11-16 14:59:31 +00006550 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006551 }
6552
Tom Finegane4099e32018-01-23 12:01:51 -08006553 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006554}
6555
David Turner056f7cd2019-01-07 17:48:13 +00006556int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
David Turnercb5e36f2019-01-17 17:15:25 +00006557 const EncodeFrameInput *const frame_input,
David Turner056f7cd2019-01-07 17:48:13 +00006558 const EncodeFrameParams *const frame_params,
6559 EncodeFrameResults *const frame_results) {
David Turner07dbd8e2019-01-08 17:16:25 +00006560 AV1_COMMON *const cm = &cpi->common;
David Turnera7f133c2019-01-22 14:47:16 +00006561 CurrentFrame *const current_frame = &cm->current_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00006562
David Turnercb5e36f2019-01-17 17:15:25 +00006563 cpi->unscaled_source = frame_input->source;
6564 cpi->source = frame_input->source;
6565 cpi->unscaled_last_source = frame_input->last_source;
David Turner056f7cd2019-01-07 17:48:13 +00006566
David Turner6e8b4d92019-02-18 15:01:33 +00006567 current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
Urvang Joshib6409e92020-03-23 11:23:27 -07006568 cm->features.error_resilient_mode = frame_params->error_resilient_mode;
Urvang Joshi6237b882020-03-26 15:02:26 -07006569 cm->features.primary_ref_frame = frame_params->primary_ref_frame;
David Turner475a3132019-01-18 15:17:17 +00006570 cm->current_frame.frame_type = frame_params->frame_type;
David Turnerdedd8ff2019-01-23 13:59:46 +00006571 cm->show_frame = frame_params->show_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00006572 cpi->ref_frame_flags = frame_params->ref_frame_flags;
David Turner04b70d82019-01-24 15:39:19 +00006573 cpi->speed = frame_params->speed;
David Turnere86ee0d2019-02-18 17:16:28 +00006574 cm->show_existing_frame = frame_params->show_existing_frame;
6575 cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
David Turner07dbd8e2019-01-08 17:16:25 +00006576
David Turner73245762019-02-11 16:42:34 +00006577 memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
6578 REF_FRAMES * sizeof(*cm->remapped_ref_idx));
6579
Jayasanker J24cb9bc2020-04-15 13:43:10 +05306580 memcpy(&cpi->refresh_frame, &frame_params->refresh_frame,
6581 sizeof(cpi->refresh_frame));
David Turnerfe3aecb2019-02-06 14:42:42 +00006582
David Turnera7f133c2019-01-22 14:47:16 +00006583 if (current_frame->frame_type == KEY_FRAME && cm->show_frame)
6584 current_frame->frame_number = 0;
6585
Remyafc038662020-02-06 11:46:11 +05306586 current_frame->order_hint =
6587 current_frame->frame_number + frame_params->order_offset;
6588 current_frame->display_order_hint = current_frame->order_hint;
6589 current_frame->order_hint %=
6590 (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
David Turnera7f133c2019-01-22 14:47:16 +00006591
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306592 if (is_stat_generation_stage(cpi)) {
Jerome Jiang2612b4d2019-05-29 17:46:47 -07006593#if !CONFIG_REALTIME_ONLY
David Turnercb5e36f2019-01-17 17:15:25 +00006594 av1_first_pass(cpi, frame_input->ts_duration);
Jerome Jiang2612b4d2019-05-29 17:46:47 -07006595#endif
David Turnercb5e36f2019-01-17 17:15:25 +00006596 } else if (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) {
David Turner73245762019-02-11 16:42:34 +00006597 if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
6598 AOM_CODEC_OK) {
David Turnercb5e36f2019-01-17 17:15:25 +00006599 return AOM_CODEC_ERROR;
6600 }
6601 } else {
David Turner056f7cd2019-01-07 17:48:13 +00006602 return AOM_CODEC_ERROR;
6603 }
6604
6605 return AOM_CODEC_OK;
6606}
6607
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006608#if CONFIG_DENOISE
6609static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
6610 int block_size, float noise_level,
6611 int64_t time_stamp, int64_t end_time) {
6612 AV1_COMMON *const cm = &cpi->common;
6613 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006614 cpi->denoise_and_model = aom_denoise_and_model_alloc(
6615 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006616 if (!cpi->denoise_and_model) {
6617 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6618 "Error allocating denoise and model");
6619 return -1;
6620 }
6621 }
6622 if (!cpi->film_grain_table) {
6623 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
6624 if (!cpi->film_grain_table) {
6625 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6626 "Error allocating grain table");
6627 return -1;
6628 }
6629 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
6630 }
6631 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
6632 &cm->film_grain_params)) {
6633 if (cm->film_grain_params.apply_grain) {
6634 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
6635 &cm->film_grain_params);
6636 }
6637 }
6638 return 0;
6639}
6640#endif
6641
James Zern3e2613b2017-03-30 23:14:40 -07006642int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07006643 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
6644 int64_t end_time) {
6645 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006646 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006647 int res = 0;
6648 const int subsampling_x = sd->subsampling_x;
6649 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006650 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006651
sdenge63f9fa2019-12-13 09:29:55 -08006652#if CONFIG_TUNE_VMAF
6653 if (!is_stat_generation_stage(cpi) &&
6654 cpi->oxcf.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING) {
sdeng615dc242020-02-04 16:06:14 -08006655 av1_vmaf_frame_preprocessing(cpi, sd);
6656 }
6657 if (!is_stat_generation_stage(cpi) &&
6658 cpi->oxcf.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
6659 av1_vmaf_blk_preprocessing(cpi, sd);
sdenge63f9fa2019-12-13 09:29:55 -08006660 }
6661#endif
6662
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006663#if CONFIG_INTERNAL_STATS
6664 struct aom_usec_timer timer;
Yaowu Xuf883b422016-08-30 14:01:10 -07006665 aom_usec_timer_start(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006666#endif
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006667#if CONFIG_DENOISE
6668 if (cpi->oxcf.noise_level > 0)
6669 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
6670 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
6671 res = -1;
6672#endif // CONFIG_DENOISE
6673
Yaowu Xuf883b422016-08-30 14:01:10 -07006674 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08006675 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07006676 res = -1;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006677#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006678 aom_usec_timer_mark(&timer);
6679 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006680#endif
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006681 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07006682 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006683 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006684 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07006685 res = -1;
6686 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006687 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006688 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006689 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006690 "Profile 1 requires 4:4:4 color format");
6691 res = -1;
6692 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006693 if ((seq_params->profile == PROFILE_2) &&
6694 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006695 !(subsampling_x == 1 && subsampling_y == 0)) {
6696 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
6697 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07006698 res = -1;
6699 }
6700
6701 return res;
6702}
6703
Yaowu Xuc27fc142016-08-22 16:08:15 -07006704#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006705extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
6706 const unsigned char *img2, int img2_pitch,
6707 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006708
6709static void adjust_image_stat(double y, double u, double v, double all,
6710 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07006711 s->stat[STAT_Y] += y;
6712 s->stat[STAT_U] += u;
6713 s->stat[STAT_V] += v;
6714 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07006715 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006716}
6717
Angie Chiang08a22a62017-07-17 17:29:17 -07006718static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006719 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006720 double samples = 0.0;
Yaowu Xue75b58a2020-01-03 12:56:12 -08006721 const uint32_t in_bit_depth = cpi->oxcf.input_bit_depth;
6722 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006723
Angie Chiang08a22a62017-07-17 17:29:17 -07006724#if CONFIG_INTER_STATS_ONLY
David Turnerd2a592e2018-11-16 14:59:31 +00006725 if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07006726#endif
6727 cpi->bytes += frame_bytes;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006728 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07006729 const YV12_BUFFER_CONFIG *orig = cpi->source;
David Turnerc29e1a92018-12-06 14:10:14 +00006730 const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006731 double y, u, v, frame_all;
6732
6733 cpi->count++;
6734 if (cpi->b_calculate_psnr) {
6735 PSNR_STATS psnr;
6736 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07006737 aom_clear_system_state();
Jerome Jiangfa1d1732019-08-06 10:31:20 -07006738#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xue75b58a2020-01-03 12:56:12 -08006739 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07006740#else
6741 aom_calc_psnr(orig, recon, &psnr);
6742#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006743 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
6744 &cpi->psnr);
6745 cpi->total_sq_error += psnr.sse[0];
6746 cpi->total_samples += psnr.samples[0];
6747 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08006748 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006749 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07006750 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07006751 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006752 else
Yaowu Xuf883b422016-08-30 14:01:10 -07006753 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006754
Yaowu Xuf883b422016-08-30 14:01:10 -07006755 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006756 cpi->summed_quality += frame_ssim2 * weight;
6757 cpi->summed_weights += weight;
6758
6759#if 0
6760 {
6761 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08006762 double y2 = psnr.psnr[1];
6763 double u2 = psnr.psnr[2];
6764 double v2 = psnr.psnr[3];
6765 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07006766 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 +00006767 cm->current_frame.frame_number, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07006768 frame_psnr2, frame_ssim2);
6769 fclose(f);
6770 }
6771#endif
6772 }
6773 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006774 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006775 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07006776 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
6777 recon->y_stride, orig->y_width, orig->y_height);
6778 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006779 cpi->total_blockiness += frame_blockiness;
6780 }
6781
6782 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006783 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006784 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07006785 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
6786 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
6787
6788 const double peak = (double)((1 << in_bit_depth) - 1);
6789 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006790 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006791 if (consistency > 0.0)
6792 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006793 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006794 cpi->total_inconsistency += this_inconsistency;
6795 }
6796 }
6797 }
6798
6799 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07006800 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006801 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07006802 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006803 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
6804 }
6805}
6806#endif // CONFIG_INTERNAL_STATS
Andrey Norkin795ba872018-03-06 13:24:14 -08006807int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
6808 size_t *size, uint8_t *dest, int64_t *time_stamp,
6809 int64_t *time_end, int flush,
Yue Chen1bc5be62018-08-24 13:57:32 -07006810 const aom_rational64_t *timestamp_ratio) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006811 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
6812 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006813
6814#if CONFIG_BITSTREAM_DEBUG
6815 assert(cpi->oxcf.max_threads == 0 &&
6816 "bitstream debug tool does not support multithreading");
6817 bitstream_queue_record_write();
Yaowu Xu63f2ea32019-04-29 10:47:42 -07006818 aom_bitstream_queue_set_frame_write(cm->current_frame.frame_number * 2 +
6819 cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006820#endif
Marco Paniconi63971322019-08-15 21:32:05 -07006821 if (cpi->use_svc && cm->number_spatial_layers > 1) {
6822 av1_one_pass_cbr_svc_start_layer(cpi);
6823 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006824
Dominic Symesd4929012018-01-31 17:32:01 +01006825 cm->showable_frame = 0;
David Turnere43f7fe2019-01-15 14:58:00 +00006826 *size = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006827#if CONFIG_INTERNAL_STATS
6828 struct aom_usec_timer cmptimer;
Yaowu Xuf883b422016-08-30 14:01:10 -07006829 aom_usec_timer_start(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006830#endif
chiyotsaic666b1f2019-12-20 10:44:58 -08006831 av1_set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006832
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07006833 // Normal defaults
Urvang Joshi6237b882020-03-26 15:02:26 -07006834 cm->features.refresh_frame_context = oxcf->frame_parallel_decoding_mode
6835 ? REFRESH_FRAME_CONTEXT_DISABLED
6836 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01006837 if (oxcf->large_scale_tile)
Urvang Joshi6237b882020-03-26 15:02:26 -07006838 cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006839
Sarah Parkerb9041612018-05-22 19:06:47 -07006840 // Initialize fields related to forward keyframes
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07006841 cpi->no_show_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08006842
David Turnerdedd8ff2019-01-23 13:59:46 +00006843 if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006844
Yue Chen1bc5be62018-08-24 13:57:32 -07006845 const int result =
6846 av1_encode_strategy(cpi, size, dest, frame_flags, time_stamp, time_end,
6847 timestamp_ratio, flush);
David Turnerdedd8ff2019-01-23 13:59:46 +00006848 if (result != AOM_CODEC_OK && result != -1) {
David Turner1539bb02019-01-24 15:28:13 +00006849 return AOM_CODEC_ERROR;
David Turnerdedd8ff2019-01-23 13:59:46 +00006850 } else if (result == -1) {
6851 // Returning -1 indicates no frame encoded; more input is required
6852 return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006853 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006854#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006855 aom_usec_timer_mark(&cmptimer);
6856 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yue Chen1bc5be62018-08-24 13:57:32 -07006857#endif // CONFIG_INTERNAL_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00006858 if (cpi->b_calculate_psnr) {
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306859 if (cm->show_existing_frame ||
6860 (!is_stat_generation_stage(cpi) && cm->show_frame)) {
David Turnerc4bf8c72019-01-15 13:14:37 +00006861 generate_psnr_packet(cpi);
6862 }
6863 }
Hui Su8ea87322019-03-29 14:44:32 -07006864
sdeng3cd9eec2020-01-23 15:49:50 -08006865#if CONFIG_TUNE_VMAF
sdeng995c54e2020-02-18 14:51:00 -08006866 if (!is_stat_generation_stage(cpi) &&
6867 (oxcf->tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING ||
6868 oxcf->tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
6869 oxcf->tuning == AOM_TUNE_VMAF_MAX_GAIN)) {
6870 av1_update_vmaf_curve(cpi, cpi->source, &cpi->common.cur_frame->buf);
sdeng3cd9eec2020-01-23 15:49:50 -08006871 }
6872#endif
6873
Vishesh8ac928b2020-04-01 02:36:35 +05306874 if (cpi->level_params.keep_level_stats && !is_stat_generation_stage(cpi)) {
Hui Su8ea87322019-03-29 14:44:32 -07006875 // Initialize level info. at the beginning of each sequence.
6876 if (cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) {
Hui Su58753d62019-05-29 09:56:19 -07006877 av1_init_level_info(cpi);
Hui Su8ea87322019-03-29 14:44:32 -07006878 }
kyslovabeeb7c2019-03-06 18:35:04 -08006879 av1_update_level_info(cpi, *size, *time_stamp, *time_end);
Hui Su8ea87322019-03-29 14:44:32 -07006880 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006881
6882#if CONFIG_INTERNAL_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306883 if (!is_stat_generation_stage(cpi)) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006884 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006885 }
6886#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08006887#if CONFIG_SPEED_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306888 if (!is_stat_generation_stage(cpi) && !cm->show_existing_frame) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08006889 cpi->tx_search_count += cpi->td.mb.tx_search_count;
6890 cpi->td.mb.tx_search_count = 0;
6891 }
6892#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006893
Yaowu Xuf883b422016-08-30 14:01:10 -07006894 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006895
Hui Su0d0ee662019-07-29 14:38:36 -07006896 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006897}
6898
Yaowu Xuf883b422016-08-30 14:01:10 -07006899int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6900 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006901 if (!cm->show_frame) {
6902 return -1;
6903 } else {
6904 int ret;
David Turnerc29e1a92018-12-06 14:10:14 +00006905 if (cm->cur_frame != NULL) {
6906 *dest = cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006907 dest->y_width = cm->width;
6908 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006909 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
6910 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006911 ret = 0;
6912 } else {
6913 ret = -1;
6914 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006915 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006916 return ret;
6917 }
6918}
6919
Yaowu Xuf883b422016-08-30 14:01:10 -07006920int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
David Turnere7ebf902018-12-04 14:04:55 +00006921 if (cpi->last_show_frame_buf == NULL) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006922
David Turnere7ebf902018-12-04 14:04:55 +00006923 *frame = cpi->last_show_frame_buf->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006924 return 0;
6925}
6926
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006927static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
6928 const YV12_BUFFER_CONFIG *b) {
6929 return a->y_height == b->y_height && a->y_width == b->y_width &&
6930 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
6931 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
6932 a->border == b->border &&
6933 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
6934 (b->flags & YV12_FLAG_HIGHBITDEPTH);
6935}
6936
Yunqing Wang93b18f32018-06-08 21:08:29 -07006937aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
6938 YV12_BUFFER_CONFIG *new_frame,
6939 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006940 const int num_planes = av1_num_planes(cm);
6941 if (!equal_dimensions_and_border(new_frame, sd))
6942 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
6943 "Incorrect buffer dimensions");
6944 else
6945 aom_yv12_copy_frame(new_frame, sd, num_planes);
6946
6947 return cm->error.error_code;
6948}
6949
Jayasanker J0b1dd292020-04-09 15:47:25 +05306950int av1_set_internal_size(AV1EncoderConfig *const oxcf,
6951 ResizePendingParams *resize_pending_params,
6952 AOM_SCALING horiz_mode, AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006953 int hr = 0, hs = 0, vr = 0, vs = 0;
6954
6955 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6956
6957 Scale2Ratio(horiz_mode, &hr, &hs);
6958 Scale2Ratio(vert_mode, &vr, &vs);
6959
6960 // always go to the next whole number
Jayasanker J0b1dd292020-04-09 15:47:25 +05306961 resize_pending_params->width = (hs - 1 + oxcf->width * hr) / hs;
6962 resize_pending_params->height = (vs - 1 + oxcf->height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006963
6964 return 0;
6965}
6966
Urvang Joshi17814622020-03-27 17:26:17 -07006967int av1_get_quantizer(AV1_COMP *cpi) {
6968 return cpi->common.quant_params.base_qindex;
6969}
Yaowu Xuc27fc142016-08-22 16:08:15 -07006970
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006971int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
6972 size_t output_size = 0;
6973 size_t total_bytes_read = 0;
6974 size_t remaining_size = *frame_size;
6975 uint8_t *buff_ptr = buffer;
6976
6977 // go through each OBUs
6978 while (total_bytes_read < *frame_size) {
6979 uint8_t saved_obu_header[2];
6980 uint64_t obu_payload_size;
6981 size_t length_of_payload_size;
6982 size_t length_of_obu_size;
6983 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
6984 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
6985
6986 // save the obu header (1 or 2 bytes)
6987 memmove(saved_obu_header, buff_ptr, obu_header_size);
6988 // clear the obu_has_size_field
6989 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
6990
6991 // get the payload_size and length of payload_size
6992 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
6993 &obu_payload_size, &length_of_payload_size) != 0) {
6994 return AOM_CODEC_ERROR;
6995 }
6996 obu_bytes_read += length_of_payload_size;
6997
6998 // calculate the length of size of the obu header plus payload
6999 length_of_obu_size =
7000 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
7001
7002 // move the rest of data to new location
7003 memmove(buff_ptr + length_of_obu_size + obu_header_size,
7004 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07007005 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04007006
7007 // write the new obu size
7008 const uint64_t obu_size = obu_header_size + obu_payload_size;
7009 size_t coded_obu_size;
7010 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
7011 &coded_obu_size) != 0) {
7012 return AOM_CODEC_ERROR;
7013 }
7014
7015 // write the saved (modified) obu_header following obu size
7016 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
7017
7018 total_bytes_read += obu_bytes_read;
7019 remaining_size -= obu_bytes_read;
7020 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07007021 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04007022 }
7023
7024 *frame_size = output_size;
7025 return AOM_CODEC_OK;
7026}
7027
Vishesha195ca32020-04-07 18:46:20 +05307028static void svc_set_updates_external_ref_frame_config(
Vishesh38c05d72020-04-14 12:19:14 +05307029 ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags, SVC *const svc) {
7030 ext_refresh_frame_flags->update_pending = 1;
7031 ext_refresh_frame_flags->last_frame = svc->refresh[svc->ref_idx[0]];
7032 ext_refresh_frame_flags->golden_frame = svc->refresh[svc->ref_idx[3]];
7033 ext_refresh_frame_flags->bwd_ref_frame = svc->refresh[svc->ref_idx[4]];
7034 ext_refresh_frame_flags->alt2_ref_frame = svc->refresh[svc->ref_idx[5]];
7035 ext_refresh_frame_flags->alt_ref_frame = svc->refresh[svc->ref_idx[6]];
Vishesha195ca32020-04-07 18:46:20 +05307036 svc->non_reference_frame = 1;
Marco Paniconid8574e32019-08-04 21:30:12 -07007037 for (int i = 0; i < REF_FRAMES; i++) {
Vishesha195ca32020-04-07 18:46:20 +05307038 if (svc->refresh[i] == 1) {
7039 svc->non_reference_frame = 0;
Marco Paniconid8574e32019-08-04 21:30:12 -07007040 break;
7041 }
7042 }
7043}
7044
Marco Paniconiad4953a2020-04-02 09:52:58 -07007045static int svc_set_references_external_ref_frame_config(AV1_COMP *cpi) {
7046 // LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3),
7047 // BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6).
7048 int ref = AOM_REFFRAME_ALL;
7049 for (int i = 0; i < INTER_REFS_PER_FRAME; i++) {
7050 if (!cpi->svc.reference[i]) ref ^= (1 << i);
7051 }
7052 return ref;
7053}
7054
Yaowu Xuf883b422016-08-30 14:01:10 -07007055void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007056 // TODO(yunqingwang): For what references to use, external encoding flags
7057 // should be consistent with internal reference frame selection. Need to
7058 // ensure that there is not conflict between the two. In AV1 encoder, the
7059 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07007060 // GOLDEN, BWDREF, ALTREF2.
Vishesha195ca32020-04-07 18:46:20 +05307061
7062 ExternalFlags *const ext_flags = &cpi->ext_flags;
Vishesh38c05d72020-04-14 12:19:14 +05307063 ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags =
7064 &ext_flags->refresh_frame;
Vishesha195ca32020-04-07 18:46:20 +05307065 ext_flags->ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007066 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007067 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
7068 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
7069 AOM_EFLAG_NO_REF_ARF2)) {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07007070 int ref = AOM_REFFRAME_ALL;
7071
7072 if (flags & AOM_EFLAG_NO_REF_LAST) ref ^= AOM_LAST_FLAG;
7073 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
7074 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
7075
7076 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
7077
7078 if (flags & AOM_EFLAG_NO_REF_ARF) {
7079 ref ^= AOM_ALT_FLAG;
7080 ref ^= AOM_BWD_FLAG;
7081 ref ^= AOM_ALT2_FLAG;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007082 } else {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07007083 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
7084 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007085 }
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07007086
Vishesha195ca32020-04-07 18:46:20 +05307087 av1_use_as_reference(&ext_flags->ref_frame_flags, ref);
Marco Paniconiad4953a2020-04-02 09:52:58 -07007088 } else {
7089 if (cpi->svc.external_ref_frame_config) {
7090 int ref = svc_set_references_external_ref_frame_config(cpi);
Vishesha195ca32020-04-07 18:46:20 +05307091 av1_use_as_reference(&ext_flags->ref_frame_flags, ref);
Marco Paniconiad4953a2020-04-02 09:52:58 -07007092 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07007093 }
7094
7095 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007096 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07007097 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007098
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007099 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
7100 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007101
Yaowu Xuf883b422016-08-30 14:01:10 -07007102 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007103
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007104 if (flags & AOM_EFLAG_NO_UPD_ARF) {
7105 upd ^= AOM_ALT_FLAG;
7106 upd ^= AOM_BWD_FLAG;
7107 upd ^= AOM_ALT2_FLAG;
7108 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07007109
Vishesh38c05d72020-04-14 12:19:14 +05307110 ext_refresh_frame_flags->last_frame = (upd & AOM_LAST_FLAG) != 0;
7111 ext_refresh_frame_flags->golden_frame = (upd & AOM_GOLD_FLAG) != 0;
7112 ext_refresh_frame_flags->alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
7113 ext_refresh_frame_flags->bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
7114 ext_refresh_frame_flags->alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
7115 ext_refresh_frame_flags->update_pending = 1;
David Turner4f1f1812019-01-24 17:00:24 +00007116 } else {
Marco Paniconid8574e32019-08-04 21:30:12 -07007117 if (cpi->svc.external_ref_frame_config)
Vishesh38c05d72020-04-14 12:19:14 +05307118 svc_set_updates_external_ref_frame_config(ext_refresh_frame_flags,
7119 &cpi->svc);
Marco Paniconid8574e32019-08-04 21:30:12 -07007120 else
Vishesh38c05d72020-04-14 12:19:14 +05307121 ext_refresh_frame_flags->update_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007122 }
7123
Vishesha195ca32020-04-07 18:46:20 +05307124 ext_flags->use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
7125 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
7126 ext_flags->use_error_resilient = cpi->oxcf.error_resilient_mode |
7127 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
7128 ext_flags->use_s_frame =
sarahparker9806fed2018-03-30 17:43:44 -07007129 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Vishesha195ca32020-04-07 18:46:20 +05307130 ext_flags->use_primary_ref_none =
7131 (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07007132
Yaowu Xuf883b422016-08-30 14:01:10 -07007133 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
Vishesha195ca32020-04-07 18:46:20 +05307134 av1_update_entropy(&ext_flags->refresh_frame_context,
7135 &ext_flags->refresh_frame_context_pending, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007136 }
7137}
Andrey Norkin795ba872018-03-06 13:24:14 -08007138
Tom Fineganf8d6a162018-08-21 10:47:55 -07007139aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
7140 if (!cpi) return NULL;
7141
7142 uint8_t header_buf[512] = { 0 };
7143 const uint32_t sequence_header_size =
Urvang Joshi450a9a22020-03-31 16:00:22 -07007144 av1_write_sequence_header_obu(&cpi->common.seq_params, &header_buf[0]);
Tom Fineganf8d6a162018-08-21 10:47:55 -07007145 assert(sequence_header_size <= sizeof(header_buf));
7146 if (sequence_header_size == 0) return NULL;
7147
7148 const size_t obu_header_size = 1;
7149 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
7150 const size_t payload_offset = obu_header_size + size_field_size;
7151
7152 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
7153 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
7154
Vishesh8ac928b2020-04-01 02:36:35 +05307155 if (av1_write_obu_header(&cpi->level_params, OBU_SEQUENCE_HEADER, 0,
7156 &header_buf[0]) != obu_header_size) {
Tom Fineganf8d6a162018-08-21 10:47:55 -07007157 return NULL;
7158 }
7159
7160 size_t coded_size_field_size = 0;
7161 if (aom_uleb_encode(sequence_header_size, size_field_size,
7162 &header_buf[obu_header_size],
7163 &coded_size_field_size) != 0) {
7164 return NULL;
7165 }
7166 assert(coded_size_field_size == size_field_size);
7167
7168 aom_fixed_buf_t *global_headers =
7169 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
7170 if (!global_headers) return NULL;
7171
7172 const size_t global_header_buf_size =
7173 obu_header_size + size_field_size + sequence_header_size;
7174
7175 global_headers->buf = malloc(global_header_buf_size);
7176 if (!global_headers->buf) {
7177 free(global_headers);
7178 return NULL;
7179 }
7180
7181 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
7182 global_headers->sz = global_header_buf_size;
7183 return global_headers;
7184}