blob: 989c6e1e4b410f1034b16a97eaf7fe24e68100c3 [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) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700402 unsigned char *const seg_map = cpi->segmentation_map;
403 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;
413 unsigned char *const seg_map = cpi->segmentation_map;
414 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) {
495 unsigned char *const seg_map_8x8 = cpi->segmentation_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 Joshiaab74432018-06-01 12:06:22 -0700601 if (cpi->oxcf.superres_mode == 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 ||
David Turnera7f133c2019-01-22 14:47:16 +0000618 cpi->ext_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
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700718static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
Remya0cce44c2019-08-16 11:57:24 +0530719 if (cpi->mbmi_ext_frame_base) {
720 aom_free(cpi->mbmi_ext_frame_base);
721 cpi->mbmi_ext_frame_base = NULL;
Urvang Joshif10ee372020-03-26 02:22:43 -0700722 cpi->mbmi_ext_alloc_size = 0;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700723 }
724}
725
726static void alloc_context_buffers_ext(AV1_COMP *cpi) {
727 AV1_COMMON *cm = &cpi->common;
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
737 if (new_ext_mi_size > cpi->mbmi_ext_alloc_size) {
chiyotsaia7091f12019-08-09 16:48:27 -0700738 dealloc_context_buffers_ext(cpi);
Remya0cce44c2019-08-16 11:57:24 +0530739 CHECK_MEM_ERROR(
740 cm, cpi->mbmi_ext_frame_base,
741 aom_calloc(new_ext_mi_size, sizeof(*cpi->mbmi_ext_frame_base)));
Urvang Joshif10ee372020-03-26 02:22:43 -0700742 cpi->mbmi_ext_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.
746 cpi->mbmi_ext_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;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000807 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700808
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700809 dealloc_context_buffers_ext(cpi);
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
Yaowu Xuf883b422016-08-30 14:01:10 -0700815 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700816 cpi->segmentation_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
834 aom_free(cpi->vmaf_rdmult_scaling_factors);
835 cpi->vmaf_rdmult_scaling_factors = NULL;
836#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++) {
852 aom_free(cpi->td.mb.hash_value_buffer[i][j]);
853 cpi->td.mb.hash_value_buffer[i][j] = NULL;
854 }
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
Yaowu Xuf883b422016-08-30 14:01:10 -0700881 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700882 cpi->tile_tok[0][0] = 0;
883
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530884 aom_free(cpi->tplist[0][0]);
885 cpi->tplist[0][0] = NULL;
886
Ravi Chaudhary0e5cd562020-04-02 11:22:24 +0530887 av1_free_pc_tree(cpi, &cpi->td, num_planes, cm->seq_params.sb_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700888
hui sud9a812b2017-07-06 14:34:37 -0700889 aom_free(cpi->td.mb.palette_buffer);
Hui Su38711e72019-06-11 10:49:47 -0700890 av1_release_compound_type_rd_buffers(&cpi->td.mb.comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700891 aom_free(cpi->td.mb.tmp_conv_dst);
892 for (int j = 0; j < 2; ++j) {
893 aom_free(cpi->td.mb.tmp_obmc_bufs[j]);
894 }
895
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700896#if CONFIG_DENOISE
897 if (cpi->denoise_and_model) {
898 aom_denoise_and_model_free(cpi->denoise_and_model);
899 cpi->denoise_and_model = NULL;
900 }
901#endif
902 if (cpi->film_grain_table) {
903 aom_film_grain_table_free(cpi->film_grain_table);
904 cpi->film_grain_table = NULL;
905 }
Hui Suc3a8d372019-05-28 15:52:45 -0700906
907 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Vishesh8ac928b2020-04-01 02:36:35 +0530908 aom_free(cpi->level_params.level_info[i]);
Hui Suc3a8d372019-05-28 15:52:45 -0700909 }
Marco Paniconi63971322019-08-15 21:32:05 -0700910
911 if (cpi->use_svc) av1_free_svc_cyclic_refresh(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700912}
913
Yaowu Xuf883b422016-08-30 14:01:10 -0700914static void configure_static_seg_features(AV1_COMP *cpi) {
915 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700916 const RATE_CONTROL *const rc = &cpi->rc;
917 struct segmentation *const seg = &cm->seg;
918
919 int high_q = (int)(rc->avg_q > 48.0);
920 int qi_delta;
921
922 // Disable and clear down for KF
David Turnerd2a592e2018-11-16 14:59:31 +0000923 if (cm->current_frame.frame_type == KEY_FRAME) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700924 // Clear down the global segmentation map
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700925 memset(cpi->segmentation_map, 0,
926 cm->mi_params.mi_rows * cm->mi_params.mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700927 seg->update_map = 0;
928 seg->update_data = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700929
930 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700931 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700932
933 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700934 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700935 } else if (cpi->refresh_alt_ref_frame) {
936 // If this is an alt ref frame
937 // Clear down the global segmentation map
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700938 memset(cpi->segmentation_map, 0,
939 cm->mi_params.mi_rows * cm->mi_params.mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700940 seg->update_map = 0;
941 seg->update_data = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700942
943 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700944 av1_disable_segmentation(seg);
945 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700946
Yaowu Xuc27fc142016-08-22 16:08:15 -0700947 // If segmentation was enabled set those features needed for the
948 // arf itself.
949 if (seg->enabled) {
950 seg->update_map = 1;
951 seg->update_data = 1;
952
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700953 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
954 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700955 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700956 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
957 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
958 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
959 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
960
961 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
962 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
963 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
964 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700965
Yaowu Xuf883b422016-08-30 14:01:10 -0700966 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700967 }
968 } else if (seg->enabled) {
969 // All other frames if segmentation has been enabled
970
971 // First normal frame in a valid gf or alt ref group
972 if (rc->frames_since_golden == 0) {
973 // Set up segment features for normal frames in an arf group
974 if (rc->source_alt_ref_active) {
975 seg->update_map = 0;
976 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700977
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700978 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
979 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700980 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
981 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700982
Cheng Chend8184da2017-09-26 18:15:22 -0700983 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
984 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
985 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
986 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
987
988 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
989 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
990 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
991 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700992
993 // Segment coding disabled for compred testing
Visheshb1f6eb62020-03-31 13:43:35 +0530994 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700995 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
996 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
997 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700998 }
999 } else {
1000 // Disable segmentation and clear down features if alt ref
1001 // is not active for this group
1002
Yaowu Xuf883b422016-08-30 14:01:10 -07001003 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001004
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001005 memset(cpi->segmentation_map, 0,
1006 cm->mi_params.mi_rows * cm->mi_params.mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001007
1008 seg->update_map = 0;
1009 seg->update_data = 0;
1010
Yaowu Xuf883b422016-08-30 14:01:10 -07001011 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001012 }
1013 } else if (rc->is_src_frame_alt_ref) {
1014 // Special case where we are coding over the top of a previous
1015 // alt ref frame.
1016 // Segment coding disabled for compred testing
1017
1018 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -07001019 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
1020 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001021
1022 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -07001023 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
1024 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
1025 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
1026 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001027
1028 // Skip all MBs if high Q (0,0 mv and skip coeffs)
1029 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001030 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
1031 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001032 }
1033 // Enable data update
1034 seg->update_data = 1;
1035 } else {
1036 // All other frames.
1037
1038 // No updates.. leave things as they are.
1039 seg->update_map = 0;
1040 seg->update_data = 0;
1041 }
1042 }
1043}
1044
Yaowu Xuf883b422016-08-30 14:01:10 -07001045static void update_reference_segmentation_map(AV1_COMP *cpi) {
1046 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001047 const CommonModeInfoParams *const mi_params = &cm->mi_params;
1048 MB_MODE_INFO **mi_4x4_ptr = mi_params->mi_grid_base;
David Turnerb757ce02018-11-12 15:01:28 +00001049 uint8_t *cache_ptr = cm->cur_frame->seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001050
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001051 for (int row = 0; row < mi_params->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -07001052 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001053 uint8_t *cache = cache_ptr;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001054 for (int col = 0; col < mi_params->mi_cols; col++, mi_4x4++, cache++)
Yushin Choa7f65922018-04-04 16:06:11 -07001055 cache[0] = mi_4x4[0]->segment_id;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001056 mi_4x4_ptr += mi_params->mi_stride;
1057 cache_ptr += mi_params->mi_cols;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001058 }
1059}
1060
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05301061static void alloc_altref_frame_buffer(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001062 AV1_COMMON *cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001063 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -07001064 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001065
Yaowu Xuc27fc142016-08-22 16:08:15 -07001066 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001067 if (aom_realloc_frame_buffer(
1068 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
1069 seq_params->subsampling_x, seq_params->subsampling_y,
Yunqing Wanga40d9e82020-02-21 16:37:38 -08001070 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi6237b882020-03-26 15:02:26 -07001071 cm->features.byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07001072 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001073 "Failed to allocate altref buffer");
1074}
1075
Yaowu Xuf883b422016-08-30 14:01:10 -07001076static void alloc_util_frame_buffers(AV1_COMP *cpi) {
1077 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001078 const SequenceHeader *const seq_params = &cm->seq_params;
Urvang Joshi6237b882020-03-26 15:02:26 -07001079 const int byte_alignment = cm->features.byte_alignment;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001080 if (aom_realloc_frame_buffer(
1081 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
1082 seq_params->subsampling_y, seq_params->use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07001083 cpi->oxcf.border_in_pixels, byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07001084 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001085 "Failed to allocate last frame buffer");
1086
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07001087 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08001088 &cpi->trial_frame_rst, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001089 cm->superres_upscaled_height, seq_params->subsampling_x,
1090 seq_params->subsampling_y, seq_params->use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07001091 AOM_RESTORATION_FRAME_BORDER, byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -08001092 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -08001093 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -07001094
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001095 if (aom_realloc_frame_buffer(
1096 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
1097 seq_params->subsampling_y, seq_params->use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07001098 cpi->oxcf.border_in_pixels, byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07001099 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001100 "Failed to allocate scaled source buffer");
1101
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001102 if (aom_realloc_frame_buffer(
1103 &cpi->scaled_last_source, cm->width, cm->height,
1104 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +05301105 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi6237b882020-03-26 15:02:26 -07001106 byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07001107 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001108 "Failed to allocate scaled last source buffer");
1109}
1110
Cheng Chen46f30c72017-09-07 11:13:33 -07001111static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001112 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001113 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001114
chiyotsaia7091f12019-08-09 16:48:27 -07001115 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
1116 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1117 "Failed to allocate context buffers");
1118 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001119
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +05301120 int mi_rows_aligned_to_sb =
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001121 ALIGN_POWER_OF_TWO(cm->mi_params.mi_rows, cm->seq_params.mib_size_log2);
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +05301122 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
1123
Mufaddal Chakeracfa88a32020-03-30 18:34:43 +05301124 if (!is_stat_generation_stage(cpi)) {
1125 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -07001126
Mufaddal Chakeracfa88a32020-03-30 18:34:43 +05301127 alloc_context_buffers_ext(cpi);
1128 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001129
Yaowu Xuf883b422016-08-30 14:01:10 -07001130 aom_free(cpi->tile_tok[0][0]);
Mufaddal Chakera3d5c0fb2020-03-27 11:04:26 +05301131 aom_free(cpi->tplist[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001132
Mufaddal Chakera3d5c0fb2020-03-27 11:04:26 +05301133 if (!is_stat_generation_stage(cpi)) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001134 unsigned int tokens =
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001135 get_token_alloc(cm->mi_params.mb_rows, cm->mi_params.mb_cols,
1136 MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001137 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -07001138 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +05301139
Mufaddal Chakera3d5c0fb2020-03-27 11:04:26 +05301140 CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
1141 aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
1142 sizeof(*cpi->tplist[0][0])));
1143 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001144
Mufaddal Chakera7b5265f2020-03-27 10:32:34 +05301145 av1_setup_pc_tree(cpi, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001146}
1147
Yaowu Xuf883b422016-08-30 14:01:10 -07001148void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001149 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001150 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001151}
1152
Hui Suef139e12019-05-20 15:51:22 -07001153double av1_get_compression_ratio(const AV1_COMMON *const cm,
1154 size_t encoded_frame_size) {
1155 const int upscaled_width = cm->superres_upscaled_width;
1156 const int height = cm->height;
1157 const int luma_pic_size = upscaled_width * height;
1158 const SequenceHeader *const seq_params = &cm->seq_params;
1159 const BITSTREAM_PROFILE profile = seq_params->profile;
1160 const int pic_size_profile_factor =
1161 profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
1162 encoded_frame_size =
1163 (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
1164 const size_t uncompressed_frame_size =
1165 (luma_pic_size * pic_size_profile_factor) >> 3;
1166 return uncompressed_frame_size / (double)encoded_frame_size;
1167}
1168
Yunqing Wang75e20e82018-06-16 12:10:48 -07001169static void set_tile_info(AV1_COMP *cpi) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001170 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001171 const CommonModeInfoParams *const mi_params = &cm->mi_params;
Urvang Joshi54ffae72020-03-23 13:37:10 -07001172 const SequenceHeader *const seq_params = &cm->seq_params;
1173 CommonTileParams *const tiles = &cm->tiles;
Dominic Symesf58f1112017-09-25 12:47:40 +02001174 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001175
1176 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001177
1178 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +02001179 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Urvang Joshi54ffae72020-03-23 13:37:10 -07001180 tiles->uniform_spacing = 1;
1181 tiles->log2_cols = AOMMAX(cpi->oxcf.tile_columns, tiles->min_log2_cols);
1182 tiles->log2_cols = AOMMIN(tiles->log2_cols, tiles->max_log2_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +02001183 } else {
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001184 int mi_cols =
1185 ALIGN_POWER_OF_TWO(mi_params->mi_cols, seq_params->mib_size_log2);
Urvang Joshi54ffae72020-03-23 13:37:10 -07001186 int sb_cols = mi_cols >> seq_params->mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001187 int size_sb, j = 0;
Urvang Joshi54ffae72020-03-23 13:37:10 -07001188 tiles->uniform_spacing = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +02001189 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
Urvang Joshi54ffae72020-03-23 13:37:10 -07001190 tiles->col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001191 size_sb = cpi->oxcf.tile_widths[j++];
1192 if (j >= cpi->oxcf.tile_width_count) j = 0;
Urvang Joshi54ffae72020-03-23 13:37:10 -07001193 start_sb += AOMMIN(size_sb, tiles->max_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +02001194 }
Urvang Joshi54ffae72020-03-23 13:37:10 -07001195 tiles->cols = i;
1196 tiles->col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001197 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001198 av1_calculate_tile_cols(seq_params, mi_params->mi_rows, mi_params->mi_cols,
1199 tiles);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001200
1201 // configure tile rows
Urvang Joshi54ffae72020-03-23 13:37:10 -07001202 if (tiles->uniform_spacing) {
1203 tiles->log2_rows = AOMMAX(cpi->oxcf.tile_rows, tiles->min_log2_rows);
1204 tiles->log2_rows = AOMMIN(tiles->log2_rows, tiles->max_log2_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +02001205 } else {
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001206 int mi_rows =
1207 ALIGN_POWER_OF_TWO(mi_params->mi_rows, seq_params->mib_size_log2);
Urvang Joshi54ffae72020-03-23 13:37:10 -07001208 int sb_rows = mi_rows >> seq_params->mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001209 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +02001210 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
Urvang Joshi54ffae72020-03-23 13:37:10 -07001211 tiles->row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001212 size_sb = cpi->oxcf.tile_heights[j++];
1213 if (j >= cpi->oxcf.tile_height_count) j = 0;
Urvang Joshi54ffae72020-03-23 13:37:10 -07001214 start_sb += AOMMIN(size_sb, tiles->max_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +02001215 }
Urvang Joshi54ffae72020-03-23 13:37:10 -07001216 tiles->rows = i;
1217 tiles->row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001218 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001219 av1_calculate_tile_rows(seq_params, mi_params->mi_rows, tiles);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001220}
1221
Yaowu Xuf883b422016-08-30 14:01:10 -07001222static void update_frame_size(AV1_COMP *cpi) {
1223 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001224 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
1225
chiyotsaia7091f12019-08-09 16:48:27 -07001226 // We need to reallocate the context buffers here in case we need more mis.
1227 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
1228 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1229 "Failed to allocate context buffers");
1230 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001231 av1_init_mi_buffers(&cm->mi_params);
chiyotsaia7091f12019-08-09 16:48:27 -07001232
Luc Trudeau1e84af52017-11-25 15:00:28 -05001233 av1_init_macroblockd(cm, xd, NULL);
chiyotsai426c0662019-08-05 16:15:11 -07001234
Mufaddal Chakeracfa88a32020-03-30 18:34:43 +05301235 if (!is_stat_generation_stage(cpi)) alloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001236 set_tile_info(cpi);
1237}
1238
Vishesh2260a592020-04-06 15:34:51 +05301239static void init_buffer_indices(ForceIntegerMVInfo *const force_intpel_info,
1240 int *const remapped_ref_idx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001241 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -07001242 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
Vishesh2260a592020-04-06 15:34:51 +05301243 remapped_ref_idx[fb_idx] = fb_idx;
1244 force_intpel_info->rate_index = 0;
1245 force_intpel_info->rate_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001246}
1247
Debargha Mukherjee57498692018-05-11 13:29:31 -07001248static INLINE int does_level_match(int width, int height, double fps,
1249 int lvl_width, int lvl_height,
1250 double lvl_fps, int lvl_dim_mult) {
1251 const int64_t lvl_luma_pels = lvl_width * lvl_height;
1252 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
1253 const int64_t luma_pels = width * height;
1254 const double display_sample_rate = luma_pels * fps;
1255 return luma_pels <= lvl_luma_pels &&
1256 display_sample_rate <= lvl_display_sample_rate &&
1257 width <= lvl_width * lvl_dim_mult &&
1258 height <= lvl_height * lvl_dim_mult;
1259}
1260
Andrey Norkin26495512018-06-20 17:13:11 -07001261static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Andrey Norkinf481d982018-05-15 12:05:31 -07001262 const AV1EncoderConfig *oxcf) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001263 // TODO(any): This is a placeholder function that only addresses dimensions
1264 // and max display sample rates.
1265 // Need to add checks for max bit rate, max decoded luma sample rate, header
1266 // rate, etc. that are not covered by this function.
Hui Su8427ff22019-03-11 10:14:33 -07001267 AV1_LEVEL level = SEQ_LEVEL_MAX;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001268 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
1269 288, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001270 level = SEQ_LEVEL_2_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001271 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1272 704, 396, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001273 level = SEQ_LEVEL_2_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001274 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1275 1088, 612, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001276 level = SEQ_LEVEL_3_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001277 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1278 1376, 774, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001279 level = SEQ_LEVEL_3_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001280 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1281 2048, 1152, 30.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001282 level = SEQ_LEVEL_4_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001283 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1284 2048, 1152, 60.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001285 level = SEQ_LEVEL_4_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001286 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1287 4096, 2176, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001288 level = SEQ_LEVEL_5_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001289 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1290 4096, 2176, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001291 level = SEQ_LEVEL_5_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001292 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1293 4096, 2176, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001294 level = SEQ_LEVEL_5_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001295 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1296 8192, 4352, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001297 level = SEQ_LEVEL_6_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001298 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1299 8192, 4352, 60.0, 2)) {
Todd Nguyenff4c8602019-11-25 13:20:21 -08001300 level = SEQ_LEVEL_6_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001301 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1302 8192, 4352, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001303 level = SEQ_LEVEL_6_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001304 }
chiyotsaiaa33bbf2019-12-02 12:07:53 -08001305
Urvang Joshi450a9a22020-03-31 16:00:22 -07001306 SequenceHeader *const seq_params = &cm->seq_params;
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001307 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su8427ff22019-03-11 10:14:33 -07001308 seq->seq_level_idx[i] = level;
Andrey Norkin26495512018-06-20 17:13:11 -07001309 // Set the maximum parameters for bitrate and buffer size for this profile,
1310 // level, and tier
Urvang Joshi450a9a22020-03-31 16:00:22 -07001311 seq_params->op_params[i].bitrate = av1_max_level_bitrate(
Hui Su8427ff22019-03-11 10:14:33 -07001312 cm->seq_params.profile, seq->seq_level_idx[i], seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001313 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
1314 // check
Urvang Joshi450a9a22020-03-31 16:00:22 -07001315 if (seq_params->op_params[i].bitrate == 0)
Andrey Norkin26495512018-06-20 17:13:11 -07001316 aom_internal_error(
1317 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
1318 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -07001319 // Buffer size in bits/s is bitrate in bits/s * 1 s
Urvang Joshi450a9a22020-03-31 16:00:22 -07001320 seq_params->op_params[i].buffer_size = seq_params->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001321 }
1322}
1323
Andrey Norkin26495512018-06-20 17:13:11 -07001324static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
Marco Paniconi67142112019-07-24 15:00:31 -07001325 const AV1EncoderConfig *oxcf, int use_svc) {
Yaowu Xu2a9ac432019-08-06 14:21:17 -07001326 seq->still_picture = (oxcf->force_video_mode == 0) && (oxcf->limit == 1);
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001327 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -07001328 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
kyslov94243382019-05-02 15:33:32 -07001329 seq->force_screen_content_tools = (oxcf->mode == REALTIME) ? 0 : 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001330 seq->force_integer_mv = 2;
David Turnerebf96f42018-11-14 16:57:57 +00001331 seq->order_hint_info.enable_order_hint = oxcf->enable_order_hint;
David Turner936235c2018-11-28 13:42:01 +00001332 seq->frame_id_numbers_present_flag =
1333 !(seq->still_picture && seq->reduced_still_picture_hdr) &&
Marco Paniconi67142112019-07-24 15:00:31 -07001334 !oxcf->large_scale_tile && oxcf->error_resilient_mode && !use_svc;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001335 if (seq->still_picture && seq->reduced_still_picture_hdr) {
David Turnerebf96f42018-11-14 16:57:57 +00001336 seq->order_hint_info.enable_order_hint = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001337 seq->force_screen_content_tools = 2;
1338 seq->force_integer_mv = 2;
1339 }
David Turnerebf96f42018-11-14 16:57:57 +00001340 seq->order_hint_info.order_hint_bits_minus_1 =
1341 seq->order_hint_info.enable_order_hint
1342 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
1343 : -1;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001344
David Turner760a2f42018-12-07 15:25:36 +00001345 seq->max_frame_width =
1346 oxcf->forced_max_frame_width ? oxcf->forced_max_frame_width : oxcf->width;
1347 seq->max_frame_height = oxcf->forced_max_frame_height
1348 ? oxcf->forced_max_frame_height
1349 : oxcf->height;
1350 seq->num_bits_width =
1351 (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
1352 seq->num_bits_height =
1353 (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
1354 assert(seq->num_bits_width <= 16);
1355 assert(seq->num_bits_height <= 16);
1356
1357 seq->frame_id_length = FRAME_ID_LENGTH;
1358 seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
1359
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001360 seq->enable_dual_filter = oxcf->enable_dual_filter;
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -08001361 seq->order_hint_info.enable_dist_wtd_comp = oxcf->enable_dist_wtd_comp;
1362 seq->order_hint_info.enable_dist_wtd_comp &=
David Turnerebf96f42018-11-14 16:57:57 +00001363 seq->order_hint_info.enable_order_hint;
1364 seq->order_hint_info.enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1365 seq->order_hint_info.enable_ref_frame_mvs &=
1366 seq->order_hint_info.enable_order_hint;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001367 seq->enable_superres = oxcf->enable_superres;
1368 seq->enable_cdef = oxcf->enable_cdef;
1369 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -07001370 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -08001371 seq->enable_interintra_compound = oxcf->enable_interintra_comp;
1372 seq->enable_masked_compound = oxcf->enable_masked_comp;
Debargha Mukherjee03c43ba2018-12-14 13:08:08 -08001373 seq->enable_intra_edge_filter = oxcf->enable_intra_edge_filter;
Yue Chen8f9ca582018-12-12 15:11:47 -08001374 seq->enable_filter_intra = oxcf->enable_filter_intra;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001375
Andrey Norkin26495512018-06-20 17:13:11 -07001376 set_bitstream_level_tier(seq, cm, oxcf);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001377
1378 if (seq->operating_points_cnt_minus_1 == 0) {
1379 seq->operating_point_idc[0] = 0;
1380 } else {
Marco Paniconi6da983b2019-09-16 20:12:43 -07001381 // Set operating_point_idc[] such that the i=0 point corresponds to the
1382 // highest quality operating point (all layers), and subsequent
1383 // operarting points (i > 0) are lower quality corresponding to
1384 // skip decoding enhancement layers (temporal first).
1385 int i = 0;
1386 assert(seq->operating_points_cnt_minus_1 ==
1387 (int)(cm->number_spatial_layers * cm->number_temporal_layers - 1));
1388 for (unsigned int sl = 0; sl < cm->number_spatial_layers; sl++) {
1389 for (unsigned int tl = 0; tl < cm->number_temporal_layers; tl++) {
1390 seq->operating_point_idc[i] =
1391 (~(~0u << (cm->number_spatial_layers - sl)) << 8) |
1392 ~(~0u << (cm->number_temporal_layers - tl));
1393 i++;
1394 }
1395 }
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001396 }
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001397}
1398
Yaowu Xuf883b422016-08-30 14:01:10 -07001399static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1400 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi450a9a22020-03-31 16:00:22 -07001401 SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001402
1403 cpi->oxcf = *oxcf;
1404 cpi->framerate = oxcf->init_framerate;
1405
Urvang Joshi450a9a22020-03-31 16:00:22 -07001406 seq_params->profile = oxcf->profile;
1407 seq_params->bit_depth = oxcf->bit_depth;
1408 seq_params->use_highbitdepth = oxcf->use_highbitdepth;
1409 seq_params->color_primaries = oxcf->color_primaries;
1410 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
1411 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
1412 seq_params->monochrome = oxcf->monochrome;
1413 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
1414 seq_params->color_range = oxcf->color_range;
1415 seq_params->timing_info_present = oxcf->timing_info_present;
1416 seq_params->timing_info.num_units_in_display_tick =
Andrey Norkin795ba872018-03-06 13:24:14 -08001417 oxcf->timing_info.num_units_in_display_tick;
Urvang Joshi450a9a22020-03-31 16:00:22 -07001418 seq_params->timing_info.time_scale = oxcf->timing_info.time_scale;
1419 seq_params->timing_info.equal_picture_interval =
Andrey Norkin795ba872018-03-06 13:24:14 -08001420 oxcf->timing_info.equal_picture_interval;
Urvang Joshi450a9a22020-03-31 16:00:22 -07001421 seq_params->timing_info.num_ticks_per_picture =
Andrey Norkin795ba872018-03-06 13:24:14 -08001422 oxcf->timing_info.num_ticks_per_picture;
1423
Urvang Joshi450a9a22020-03-31 16:00:22 -07001424 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07001425 oxcf->display_model_info_present_flag;
Urvang Joshi450a9a22020-03-31 16:00:22 -07001426 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001427 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08001428 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001429 // set the decoder model parameters in schedule mode
Urvang Joshi450a9a22020-03-31 16:00:22 -07001430 seq_params->decoder_model_info.num_units_in_decoding_tick =
Andrey Norkin795ba872018-03-06 13:24:14 -08001431 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07001432 cm->buffer_removal_time_present = 1;
Urvang Joshi450a9a22020-03-31 16:00:22 -07001433 av1_set_aom_dec_model_info(&seq_params->decoder_model_info);
1434 av1_set_dec_model_op_parameters(&seq_params->op_params[0]);
1435 } else if (seq_params->timing_info_present &&
1436 seq_params->timing_info.equal_picture_interval &&
1437 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001438 // set the decoder model parameters in resource availability mode
Urvang Joshi450a9a22020-03-31 16:00:22 -07001439 av1_set_resource_availability_parameters(&seq_params->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001440 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -07001441 seq_params->op_params[0].initial_display_delay =
Andrey Norkinc7511de2018-06-22 12:31:06 -07001442 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08001443 }
Andrey Norkinc7511de2018-06-22 12:31:06 -07001444
Urvang Joshi450a9a22020-03-31 16:00:22 -07001445 if (seq_params->monochrome) {
1446 seq_params->subsampling_x = 1;
1447 seq_params->subsampling_y = 1;
1448 } else if (seq_params->color_primaries == AOM_CICP_CP_BT_709 &&
1449 seq_params->transfer_characteristics == AOM_CICP_TC_SRGB &&
1450 seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) {
1451 seq_params->subsampling_x = 0;
1452 seq_params->subsampling_y = 0;
Tom Fineganf8d6a162018-08-21 10:47:55 -07001453 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -07001454 if (seq_params->profile == 0) {
1455 seq_params->subsampling_x = 1;
1456 seq_params->subsampling_y = 1;
1457 } else if (seq_params->profile == 1) {
1458 seq_params->subsampling_x = 0;
1459 seq_params->subsampling_y = 0;
Tom Fineganf8d6a162018-08-21 10:47:55 -07001460 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -07001461 if (seq_params->bit_depth == AOM_BITS_12) {
1462 seq_params->subsampling_x = oxcf->chroma_subsampling_x;
1463 seq_params->subsampling_y = oxcf->chroma_subsampling_y;
Tom Fineganf8d6a162018-08-21 10:47:55 -07001464 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -07001465 seq_params->subsampling_x = 1;
1466 seq_params->subsampling_y = 0;
Tom Fineganf8d6a162018-08-21 10:47:55 -07001467 }
1468 }
Tom Finegan02b2a842018-08-24 13:50:00 -07001469 }
1470
Yaowu Xuc27fc142016-08-22 16:08:15 -07001471 cm->width = oxcf->width;
1472 cm->height = oxcf->height;
Urvang Joshi450a9a22020-03-31 16:00:22 -07001473 set_sb_size(seq_params,
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001474 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -07001475 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -07001476
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08001477 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001478
1479 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -07001480 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001481
Marco Paniconi882c5ad2019-09-30 21:32:29 -07001482 // Set init SVC parameters.
Marco Paniconi67142112019-07-24 15:00:31 -07001483 cpi->use_svc = 0;
Marco Paniconid8574e32019-08-04 21:30:12 -07001484 cpi->svc.external_ref_frame_config = 0;
1485 cpi->svc.non_reference_frame = 0;
Marco Paniconi67142112019-07-24 15:00:31 -07001486 cm->number_spatial_layers = 1;
1487 cm->number_temporal_layers = 1;
Marco Paniconi882c5ad2019-09-30 21:32:29 -07001488 cm->spatial_layer_id = 0;
1489 cm->temporal_layer_id = 0;
Marco Paniconi67142112019-07-24 15:00:31 -07001490
Yaowu Xuc27fc142016-08-22 16:08:15 -07001491 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001492 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001493
Yaowu Xuc27fc142016-08-22 16:08:15 -07001494 cpi->ref_frame_flags = 0;
1495
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001496 // Reset resize pending flags
1497 cpi->resize_pending_width = 0;
1498 cpi->resize_pending_height = 0;
1499
Vishesh2260a592020-04-06 15:34:51 +05301500 init_buffer_indices(&cpi->force_intpel_info, cm->remapped_ref_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001501}
1502
1503static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001504 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001505 const int64_t bandwidth = oxcf->target_bandwidth;
1506 const int64_t starting = oxcf->starting_buffer_level_ms;
1507 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1508 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1509
1510 rc->starting_buffer_level = starting * bandwidth / 1000;
1511 rc->optimal_buffer_level =
1512 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1513 rc->maximum_buffer_size =
1514 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1515}
1516
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001517#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1518 cpi->fn_ptr[BT].sdf = SDF; \
1519 cpi->fn_ptr[BT].sdaf = SDAF; \
1520 cpi->fn_ptr[BT].vf = VF; \
1521 cpi->fn_ptr[BT].svf = SVF; \
1522 cpi->fn_ptr[BT].svaf = SVAF; \
1523 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1524 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001525 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001526
1527#define MAKE_BFP_SAD_WRAPPER(fnname) \
1528 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1529 int source_stride, \
1530 const uint8_t *ref_ptr, int ref_stride) { \
1531 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1532 } \
1533 static unsigned int fnname##_bits10( \
1534 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1535 int ref_stride) { \
1536 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1537 } \
1538 static unsigned int fnname##_bits12( \
1539 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1540 int ref_stride) { \
1541 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1542 }
1543
1544#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1545 static unsigned int fnname##_bits8( \
1546 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1547 int ref_stride, const uint8_t *second_pred) { \
1548 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1549 } \
1550 static unsigned int fnname##_bits10( \
1551 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1552 int ref_stride, const uint8_t *second_pred) { \
1553 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1554 2; \
1555 } \
1556 static unsigned int fnname##_bits12( \
1557 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1558 int ref_stride, const uint8_t *second_pred) { \
1559 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1560 4; \
1561 }
1562
Yaowu Xuc27fc142016-08-22 16:08:15 -07001563#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1564 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1565 const uint8_t *const ref_ptr[], int ref_stride, \
1566 unsigned int *sad_array) { \
1567 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1568 } \
1569 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1570 const uint8_t *const ref_ptr[], int ref_stride, \
1571 unsigned int *sad_array) { \
1572 int i; \
1573 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1574 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1575 } \
1576 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1577 const uint8_t *const ref_ptr[], int ref_stride, \
1578 unsigned int *sad_array) { \
1579 int i; \
1580 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1581 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1582 }
1583
Cheng Chenbf3d4962017-11-01 14:48:52 -07001584#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1585 static unsigned int fnname##_bits8( \
1586 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1587 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001588 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001589 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1590 jcp_param); \
1591 } \
1592 static unsigned int fnname##_bits10( \
1593 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1594 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001595 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001596 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1597 jcp_param) >> \
1598 2; \
1599 } \
1600 static unsigned int fnname##_bits12( \
1601 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1602 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001603 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001604 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1605 jcp_param) >> \
1606 4; \
1607 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001608
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001609#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001610MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1611MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001612MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1613MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1614MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1615MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1616MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1617MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1618MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001619MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1620MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1621MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1622MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1623MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1624MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1625MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1626MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1627MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1628MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1629MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1630MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1631MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1632MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001633MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1634MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1635MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001636MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1637MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1638MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001639MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1640MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1641MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001642MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1643MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1644MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001645MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1646MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1647MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001648MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1649MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1650MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001651MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1652MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1653MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001654MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1655MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1656MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001657MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001658
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001659MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1660MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1661MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1662MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1663MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1664MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1665MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1666MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1667MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1668MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1669MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1670MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001671MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1672MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1673MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1674MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1675MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1676MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001677
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001678MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x128_avg)
1679MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x64_avg)
1680MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x128_avg)
1681MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x16_avg)
1682MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x32_avg)
1683MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x32_avg)
1684MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x64_avg)
1685MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x32_avg)
1686MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x64_avg)
1687MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x16_avg)
1688MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x8_avg)
1689MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x16_avg)
1690MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x8_avg)
1691MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x4_avg)
1692MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x8_avg)
1693MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x4_avg)
1694MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x16_avg)
1695MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x4_avg)
1696MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x32_avg)
1697MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x8_avg)
1698MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x64_avg)
1699MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x16_avg)
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001700#endif // CONFIG_AV1_HIGHBITDEPTH
Cheng Chenbf3d4962017-11-01 14:48:52 -07001701
David Barker0f3c94e2017-05-16 15:21:50 +01001702#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001703 cpi->fn_ptr[BT].msdf = MCSDF; \
1704 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001705
David Barkerc155e012017-05-11 13:54:54 +01001706#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1707 static unsigned int fnname##_bits8( \
1708 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1709 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1710 int m_stride, int invert_mask) { \
1711 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1712 second_pred_ptr, m, m_stride, invert_mask); \
1713 } \
1714 static unsigned int fnname##_bits10( \
1715 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1716 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1717 int m_stride, int invert_mask) { \
1718 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1719 second_pred_ptr, m, m_stride, invert_mask) >> \
1720 2; \
1721 } \
1722 static unsigned int fnname##_bits12( \
1723 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1724 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1725 int m_stride, int invert_mask) { \
1726 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1727 second_pred_ptr, m, m_stride, invert_mask) >> \
1728 4; \
1729 }
1730
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001731#if CONFIG_AV1_HIGHBITDEPTH
David Barkerf19f35f2017-05-22 16:33:22 +01001732MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1733MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1734MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001735MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1736MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1737MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1738MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1739MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1740MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1741MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1742MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1743MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1744MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1745MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1746MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1747MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001748MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1749MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1750MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1751MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001752MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1753MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001754#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001755
Yaowu Xuc27fc142016-08-22 16:08:15 -07001756#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1757 cpi->fn_ptr[BT].osdf = OSDF; \
1758 cpi->fn_ptr[BT].ovf = OVF; \
1759 cpi->fn_ptr[BT].osvf = OSVF;
1760
1761#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1762 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1763 const int32_t *wsrc, \
1764 const int32_t *msk) { \
1765 return fnname(ref, ref_stride, wsrc, msk); \
1766 } \
1767 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1768 const int32_t *wsrc, \
1769 const int32_t *msk) { \
1770 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1771 } \
1772 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1773 const int32_t *wsrc, \
1774 const int32_t *msk) { \
1775 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1776 }
1777
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001778#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001779MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1780MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1781MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001782MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1783MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1784MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1785MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1786MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1787MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1788MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1789MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1790MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1791MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1792MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1793MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1794MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001795MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1796MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1797MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1798MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001799MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1800MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001801
Yaowu Xuf883b422016-08-30 14:01:10 -07001802static void highbd_set_var_fns(AV1_COMP *const cpi) {
1803 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001804 if (cm->seq_params.use_highbitdepth) {
1805 switch (cm->seq_params.bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001806 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001807 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1808 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1809 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001810 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001811 aom_highbd_sad64x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001812 aom_highbd_dist_wtd_sad64x16_avg_bits8,
1813 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001814
1815 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1816 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1817 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001818 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001819 aom_highbd_sad16x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001820 aom_highbd_dist_wtd_sad16x64_avg_bits8,
1821 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001822
1823 HIGHBD_BFP(
1824 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1825 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001826 aom_highbd_8_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001827 aom_highbd_sad32x8x4d_bits8, aom_highbd_dist_wtd_sad32x8_avg_bits8,
1828 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001829
1830 HIGHBD_BFP(
1831 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1832 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001833 aom_highbd_8_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001834 aom_highbd_sad8x32x4d_bits8, aom_highbd_dist_wtd_sad8x32_avg_bits8,
1835 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001836
1837 HIGHBD_BFP(
1838 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1839 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001840 aom_highbd_8_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001841 aom_highbd_sad16x4x4d_bits8, aom_highbd_dist_wtd_sad16x4_avg_bits8,
1842 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001843
1844 HIGHBD_BFP(
1845 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1846 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001847 aom_highbd_8_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001848 aom_highbd_sad4x16x4d_bits8, aom_highbd_dist_wtd_sad4x16_avg_bits8,
1849 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001850
1851 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1852 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1853 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001854 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001855 aom_highbd_sad32x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001856 aom_highbd_dist_wtd_sad32x16_avg_bits8,
1857 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001858
1859 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1860 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1861 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001862 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001863 aom_highbd_sad16x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001864 aom_highbd_dist_wtd_sad16x32_avg_bits8,
1865 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001866
1867 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1868 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1869 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001870 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001871 aom_highbd_sad64x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001872 aom_highbd_dist_wtd_sad64x32_avg_bits8,
1873 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001874
1875 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1876 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1877 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001878 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001879 aom_highbd_sad32x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001880 aom_highbd_dist_wtd_sad32x64_avg_bits8,
1881 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001882
1883 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1884 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1885 aom_highbd_8_sub_pixel_variance32x32,
1886 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001887 aom_highbd_sad32x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001888 aom_highbd_dist_wtd_sad32x32_avg_bits8,
1889 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001890
1891 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1892 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1893 aom_highbd_8_sub_pixel_variance64x64,
1894 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001895 aom_highbd_sad64x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001896 aom_highbd_dist_wtd_sad64x64_avg_bits8,
1897 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001898
1899 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1900 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1901 aom_highbd_8_sub_pixel_variance16x16,
1902 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001903 aom_highbd_sad16x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001904 aom_highbd_dist_wtd_sad16x16_avg_bits8,
1905 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001906
1907 HIGHBD_BFP(
1908 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1909 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001910 aom_highbd_8_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001911 aom_highbd_sad16x8x4d_bits8, aom_highbd_dist_wtd_sad16x8_avg_bits8,
1912 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001913
1914 HIGHBD_BFP(
1915 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1916 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001917 aom_highbd_8_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001918 aom_highbd_sad8x16x4d_bits8, aom_highbd_dist_wtd_sad8x16_avg_bits8,
1919 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001920
Cheng Chenbf3d4962017-11-01 14:48:52 -07001921 HIGHBD_BFP(
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001922 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1923 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1924 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x4d_bits8,
1925 aom_highbd_dist_wtd_sad8x8_avg_bits8,
1926 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x8)
1927
1928 HIGHBD_BFP(
1929 BLOCK_8X4, aom_highbd_sad8x4_bits8, aom_highbd_sad8x4_avg_bits8,
1930 aom_highbd_8_variance8x4, aom_highbd_8_sub_pixel_variance8x4,
1931 aom_highbd_8_sub_pixel_avg_variance8x4, aom_highbd_sad8x4x4d_bits8,
1932 aom_highbd_dist_wtd_sad8x4_avg_bits8,
1933 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x4)
1934
1935 HIGHBD_BFP(
1936 BLOCK_4X8, aom_highbd_sad4x8_bits8, aom_highbd_sad4x8_avg_bits8,
1937 aom_highbd_8_variance4x8, aom_highbd_8_sub_pixel_variance4x8,
1938 aom_highbd_8_sub_pixel_avg_variance4x8, aom_highbd_sad4x8x4d_bits8,
1939 aom_highbd_dist_wtd_sad4x8_avg_bits8,
1940 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x8)
1941
1942 HIGHBD_BFP(
1943 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1944 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1945 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x4d_bits8,
1946 aom_highbd_dist_wtd_sad4x4_avg_bits8,
1947 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x4)
1948
1949 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1950 aom_highbd_sad128x128_avg_bits8,
1951 aom_highbd_8_variance128x128,
1952 aom_highbd_8_sub_pixel_variance128x128,
1953 aom_highbd_8_sub_pixel_avg_variance128x128,
1954 aom_highbd_sad128x128x4d_bits8,
1955 aom_highbd_dist_wtd_sad128x128_avg_bits8,
1956 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001957
1958 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1959 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1960 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001961 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001962 aom_highbd_sad128x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001963 aom_highbd_dist_wtd_sad128x64_avg_bits8,
1964 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001965
1966 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1967 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1968 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001969 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001970 aom_highbd_sad64x128x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001971 aom_highbd_dist_wtd_sad64x128_avg_bits8,
1972 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001973
David Barkerf19f35f2017-05-22 16:33:22 +01001974 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1975 aom_highbd_8_masked_sub_pixel_variance128x128)
1976 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1977 aom_highbd_8_masked_sub_pixel_variance128x64)
1978 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1979 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001980 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1981 aom_highbd_8_masked_sub_pixel_variance64x64)
1982 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1983 aom_highbd_8_masked_sub_pixel_variance64x32)
1984 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1985 aom_highbd_8_masked_sub_pixel_variance32x64)
1986 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1987 aom_highbd_8_masked_sub_pixel_variance32x32)
1988 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1989 aom_highbd_8_masked_sub_pixel_variance32x16)
1990 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1991 aom_highbd_8_masked_sub_pixel_variance16x32)
1992 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1993 aom_highbd_8_masked_sub_pixel_variance16x16)
1994 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1995 aom_highbd_8_masked_sub_pixel_variance8x16)
1996 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1997 aom_highbd_8_masked_sub_pixel_variance16x8)
1998 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1999 aom_highbd_8_masked_sub_pixel_variance8x8)
2000 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
2001 aom_highbd_8_masked_sub_pixel_variance4x8)
2002 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
2003 aom_highbd_8_masked_sub_pixel_variance8x4)
2004 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
2005 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002006 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
2007 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002008 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
2009 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002010 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
2011 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002012 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
2013 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002014 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
2015 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002016 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
2017 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002018 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
2019 aom_highbd_obmc_variance128x128,
2020 aom_highbd_obmc_sub_pixel_variance128x128)
2021 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
2022 aom_highbd_obmc_variance128x64,
2023 aom_highbd_obmc_sub_pixel_variance128x64)
2024 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
2025 aom_highbd_obmc_variance64x128,
2026 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002027 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
2028 aom_highbd_obmc_variance64x64,
2029 aom_highbd_obmc_sub_pixel_variance64x64)
2030 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
2031 aom_highbd_obmc_variance64x32,
2032 aom_highbd_obmc_sub_pixel_variance64x32)
2033 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
2034 aom_highbd_obmc_variance32x64,
2035 aom_highbd_obmc_sub_pixel_variance32x64)
2036 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
2037 aom_highbd_obmc_variance32x32,
2038 aom_highbd_obmc_sub_pixel_variance32x32)
2039 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
2040 aom_highbd_obmc_variance32x16,
2041 aom_highbd_obmc_sub_pixel_variance32x16)
2042 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
2043 aom_highbd_obmc_variance16x32,
2044 aom_highbd_obmc_sub_pixel_variance16x32)
2045 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
2046 aom_highbd_obmc_variance16x16,
2047 aom_highbd_obmc_sub_pixel_variance16x16)
2048 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
2049 aom_highbd_obmc_variance8x16,
2050 aom_highbd_obmc_sub_pixel_variance8x16)
2051 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
2052 aom_highbd_obmc_variance16x8,
2053 aom_highbd_obmc_sub_pixel_variance16x8)
2054 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
2055 aom_highbd_obmc_variance8x8,
2056 aom_highbd_obmc_sub_pixel_variance8x8)
2057 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
2058 aom_highbd_obmc_variance4x8,
2059 aom_highbd_obmc_sub_pixel_variance4x8)
2060 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
2061 aom_highbd_obmc_variance8x4,
2062 aom_highbd_obmc_sub_pixel_variance8x4)
2063 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
2064 aom_highbd_obmc_variance4x4,
2065 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002066 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
2067 aom_highbd_obmc_variance64x16,
2068 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002069 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
2070 aom_highbd_obmc_variance16x64,
2071 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002072 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
2073 aom_highbd_obmc_variance32x8,
2074 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002075 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
2076 aom_highbd_obmc_variance8x32,
2077 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002078 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
2079 aom_highbd_obmc_variance16x4,
2080 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002081 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
2082 aom_highbd_obmc_variance4x16,
2083 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002084 break;
2085
Yaowu Xuf883b422016-08-30 14:01:10 -07002086 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002087 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
2088 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
2089 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002090 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002091 aom_highbd_sad64x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002092 aom_highbd_dist_wtd_sad64x16_avg_bits10,
2093 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002094
2095 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
2096 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
2097 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002098 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002099 aom_highbd_sad16x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002100 aom_highbd_dist_wtd_sad16x64_avg_bits10,
2101 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002102
2103 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
2104 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
2105 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002106 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002107 aom_highbd_sad32x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002108 aom_highbd_dist_wtd_sad32x8_avg_bits10,
2109 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002110
2111 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
2112 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
2113 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002114 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002115 aom_highbd_sad8x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002116 aom_highbd_dist_wtd_sad8x32_avg_bits10,
2117 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002118
2119 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
2120 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
2121 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002122 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002123 aom_highbd_sad16x4x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002124 aom_highbd_dist_wtd_sad16x4_avg_bits10,
2125 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002126
2127 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
2128 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
2129 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002130 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002131 aom_highbd_sad4x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002132 aom_highbd_dist_wtd_sad4x16_avg_bits10,
2133 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002134
2135 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
2136 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
2137 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002138 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002139 aom_highbd_sad32x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002140 aom_highbd_dist_wtd_sad32x16_avg_bits10,
2141 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002142
2143 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
2144 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
2145 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002146 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002147 aom_highbd_sad16x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002148 aom_highbd_dist_wtd_sad16x32_avg_bits10,
2149 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002150
2151 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
2152 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
2153 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002154 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002155 aom_highbd_sad64x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002156 aom_highbd_dist_wtd_sad64x32_avg_bits10,
2157 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002158
2159 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
2160 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
2161 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002162 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002163 aom_highbd_sad32x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002164 aom_highbd_dist_wtd_sad32x64_avg_bits10,
2165 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002166
2167 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
2168 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
2169 aom_highbd_10_sub_pixel_variance32x32,
2170 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002171 aom_highbd_sad32x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002172 aom_highbd_dist_wtd_sad32x32_avg_bits10,
2173 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002174
2175 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
2176 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
2177 aom_highbd_10_sub_pixel_variance64x64,
2178 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002179 aom_highbd_sad64x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002180 aom_highbd_dist_wtd_sad64x64_avg_bits10,
2181 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002182
2183 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
2184 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
2185 aom_highbd_10_sub_pixel_variance16x16,
2186 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002187 aom_highbd_sad16x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002188 aom_highbd_dist_wtd_sad16x16_avg_bits10,
2189 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002190
2191 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
2192 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
2193 aom_highbd_10_sub_pixel_variance16x8,
2194 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002195 aom_highbd_sad16x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002196 aom_highbd_dist_wtd_sad16x8_avg_bits10,
2197 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002198
2199 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
2200 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
2201 aom_highbd_10_sub_pixel_variance8x16,
2202 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002203 aom_highbd_sad8x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002204 aom_highbd_dist_wtd_sad8x16_avg_bits10,
2205 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002206
2207 HIGHBD_BFP(
2208 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
2209 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002210 aom_highbd_10_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002211 aom_highbd_sad8x8x4d_bits10, aom_highbd_dist_wtd_sad8x8_avg_bits10,
2212 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002213
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002214 HIGHBD_BFP(
2215 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
2216 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
2217 aom_highbd_10_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002218 aom_highbd_sad8x4x4d_bits10, aom_highbd_dist_wtd_sad8x4_avg_bits10,
2219 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002220
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002221 HIGHBD_BFP(
2222 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
2223 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
2224 aom_highbd_10_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002225 aom_highbd_sad4x8x4d_bits10, aom_highbd_dist_wtd_sad4x8_avg_bits10,
2226 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002227
2228 HIGHBD_BFP(
2229 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
2230 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002231 aom_highbd_10_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002232 aom_highbd_sad4x4x4d_bits10, aom_highbd_dist_wtd_sad4x4_avg_bits10,
2233 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002234
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002235 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
2236 aom_highbd_sad128x128_avg_bits10,
2237 aom_highbd_10_variance128x128,
2238 aom_highbd_10_sub_pixel_variance128x128,
2239 aom_highbd_10_sub_pixel_avg_variance128x128,
2240 aom_highbd_sad128x128x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002241 aom_highbd_dist_wtd_sad128x128_avg_bits10,
2242 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002243
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002244 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
2245 aom_highbd_sad128x64_avg_bits10,
2246 aom_highbd_10_variance128x64,
2247 aom_highbd_10_sub_pixel_variance128x64,
2248 aom_highbd_10_sub_pixel_avg_variance128x64,
2249 aom_highbd_sad128x64x4d_bits10,
2250 aom_highbd_dist_wtd_sad128x64_avg_bits10,
2251 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002252
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002253 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
2254 aom_highbd_sad64x128_avg_bits10,
2255 aom_highbd_10_variance64x128,
2256 aom_highbd_10_sub_pixel_variance64x128,
2257 aom_highbd_10_sub_pixel_avg_variance64x128,
2258 aom_highbd_sad64x128x4d_bits10,
2259 aom_highbd_dist_wtd_sad64x128_avg_bits10,
2260 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002261
David Barkerf19f35f2017-05-22 16:33:22 +01002262 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
2263 aom_highbd_10_masked_sub_pixel_variance128x128)
2264 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
2265 aom_highbd_10_masked_sub_pixel_variance128x64)
2266 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
2267 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002268 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
2269 aom_highbd_10_masked_sub_pixel_variance64x64)
2270 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
2271 aom_highbd_10_masked_sub_pixel_variance64x32)
2272 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
2273 aom_highbd_10_masked_sub_pixel_variance32x64)
2274 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
2275 aom_highbd_10_masked_sub_pixel_variance32x32)
2276 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
2277 aom_highbd_10_masked_sub_pixel_variance32x16)
2278 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
2279 aom_highbd_10_masked_sub_pixel_variance16x32)
2280 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
2281 aom_highbd_10_masked_sub_pixel_variance16x16)
2282 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
2283 aom_highbd_10_masked_sub_pixel_variance8x16)
2284 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
2285 aom_highbd_10_masked_sub_pixel_variance16x8)
2286 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
2287 aom_highbd_10_masked_sub_pixel_variance8x8)
2288 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
2289 aom_highbd_10_masked_sub_pixel_variance4x8)
2290 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
2291 aom_highbd_10_masked_sub_pixel_variance8x4)
2292 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
2293 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002294 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
2295 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002296 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
2297 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002298 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
2299 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002300 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
2301 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002302 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
2303 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002304 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
2305 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002306 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
2307 aom_highbd_10_obmc_variance128x128,
2308 aom_highbd_10_obmc_sub_pixel_variance128x128)
2309 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
2310 aom_highbd_10_obmc_variance128x64,
2311 aom_highbd_10_obmc_sub_pixel_variance128x64)
2312 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
2313 aom_highbd_10_obmc_variance64x128,
2314 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002315 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2316 aom_highbd_10_obmc_variance64x64,
2317 aom_highbd_10_obmc_sub_pixel_variance64x64)
2318 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2319 aom_highbd_10_obmc_variance64x32,
2320 aom_highbd_10_obmc_sub_pixel_variance64x32)
2321 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2322 aom_highbd_10_obmc_variance32x64,
2323 aom_highbd_10_obmc_sub_pixel_variance32x64)
2324 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2325 aom_highbd_10_obmc_variance32x32,
2326 aom_highbd_10_obmc_sub_pixel_variance32x32)
2327 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2328 aom_highbd_10_obmc_variance32x16,
2329 aom_highbd_10_obmc_sub_pixel_variance32x16)
2330 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2331 aom_highbd_10_obmc_variance16x32,
2332 aom_highbd_10_obmc_sub_pixel_variance16x32)
2333 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2334 aom_highbd_10_obmc_variance16x16,
2335 aom_highbd_10_obmc_sub_pixel_variance16x16)
2336 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2337 aom_highbd_10_obmc_variance8x16,
2338 aom_highbd_10_obmc_sub_pixel_variance8x16)
2339 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2340 aom_highbd_10_obmc_variance16x8,
2341 aom_highbd_10_obmc_sub_pixel_variance16x8)
2342 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2343 aom_highbd_10_obmc_variance8x8,
2344 aom_highbd_10_obmc_sub_pixel_variance8x8)
2345 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2346 aom_highbd_10_obmc_variance4x8,
2347 aom_highbd_10_obmc_sub_pixel_variance4x8)
2348 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2349 aom_highbd_10_obmc_variance8x4,
2350 aom_highbd_10_obmc_sub_pixel_variance8x4)
2351 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2352 aom_highbd_10_obmc_variance4x4,
2353 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002354
Rupert Swarbrick72678572017-08-02 12:05:26 +01002355 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2356 aom_highbd_10_obmc_variance64x16,
2357 aom_highbd_10_obmc_sub_pixel_variance64x16)
2358
2359 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2360 aom_highbd_10_obmc_variance16x64,
2361 aom_highbd_10_obmc_sub_pixel_variance16x64)
2362
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002363 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2364 aom_highbd_10_obmc_variance32x8,
2365 aom_highbd_10_obmc_sub_pixel_variance32x8)
2366
2367 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2368 aom_highbd_10_obmc_variance8x32,
2369 aom_highbd_10_obmc_sub_pixel_variance8x32)
2370
2371 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2372 aom_highbd_10_obmc_variance16x4,
2373 aom_highbd_10_obmc_sub_pixel_variance16x4)
2374
2375 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2376 aom_highbd_10_obmc_variance4x16,
2377 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002378 break;
2379
Yaowu Xuf883b422016-08-30 14:01:10 -07002380 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002381 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2382 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2383 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002384 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002385 aom_highbd_sad64x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002386 aom_highbd_dist_wtd_sad64x16_avg_bits12,
2387 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002388
2389 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2390 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2391 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002392 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002393 aom_highbd_sad16x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002394 aom_highbd_dist_wtd_sad16x64_avg_bits12,
2395 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002396
2397 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2398 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2399 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002400 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002401 aom_highbd_sad32x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002402 aom_highbd_dist_wtd_sad32x8_avg_bits12,
2403 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002404
2405 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2406 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2407 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002408 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002409 aom_highbd_sad8x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002410 aom_highbd_dist_wtd_sad8x32_avg_bits12,
2411 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002412
2413 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2414 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2415 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002416 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002417 aom_highbd_sad16x4x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002418 aom_highbd_dist_wtd_sad16x4_avg_bits12,
2419 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002420
2421 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2422 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2423 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002424 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002425 aom_highbd_sad4x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002426 aom_highbd_dist_wtd_sad4x16_avg_bits12,
2427 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002428
2429 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2430 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2431 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002432 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002433 aom_highbd_sad32x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002434 aom_highbd_dist_wtd_sad32x16_avg_bits12,
2435 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002436
2437 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2438 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2439 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002440 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002441 aom_highbd_sad16x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002442 aom_highbd_dist_wtd_sad16x32_avg_bits12,
2443 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002444
2445 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2446 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2447 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002448 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002449 aom_highbd_sad64x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002450 aom_highbd_dist_wtd_sad64x32_avg_bits12,
2451 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002452
2453 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2454 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2455 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002456 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002457 aom_highbd_sad32x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002458 aom_highbd_dist_wtd_sad32x64_avg_bits12,
2459 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002460
2461 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2462 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2463 aom_highbd_12_sub_pixel_variance32x32,
2464 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002465 aom_highbd_sad32x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002466 aom_highbd_dist_wtd_sad32x32_avg_bits12,
2467 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002468
2469 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2470 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2471 aom_highbd_12_sub_pixel_variance64x64,
2472 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002473 aom_highbd_sad64x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002474 aom_highbd_dist_wtd_sad64x64_avg_bits12,
2475 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002476
2477 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2478 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2479 aom_highbd_12_sub_pixel_variance16x16,
2480 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002481 aom_highbd_sad16x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002482 aom_highbd_dist_wtd_sad16x16_avg_bits12,
2483 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002484
2485 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2486 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2487 aom_highbd_12_sub_pixel_variance16x8,
2488 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002489 aom_highbd_sad16x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002490 aom_highbd_dist_wtd_sad16x8_avg_bits12,
2491 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002492
2493 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2494 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2495 aom_highbd_12_sub_pixel_variance8x16,
2496 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002497 aom_highbd_sad8x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002498 aom_highbd_dist_wtd_sad8x16_avg_bits12,
2499 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002500
2501 HIGHBD_BFP(
2502 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2503 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002504 aom_highbd_12_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002505 aom_highbd_sad8x8x4d_bits12, aom_highbd_dist_wtd_sad8x8_avg_bits12,
2506 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002507
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002508 HIGHBD_BFP(
2509 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2510 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2511 aom_highbd_12_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002512 aom_highbd_sad8x4x4d_bits12, aom_highbd_dist_wtd_sad8x4_avg_bits12,
2513 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002514
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002515 HIGHBD_BFP(
2516 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2517 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2518 aom_highbd_12_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002519 aom_highbd_sad4x8x4d_bits12, aom_highbd_dist_wtd_sad4x8_avg_bits12,
2520 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002521
2522 HIGHBD_BFP(
2523 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2524 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002525 aom_highbd_12_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002526 aom_highbd_sad4x4x4d_bits12, aom_highbd_dist_wtd_sad4x4_avg_bits12,
2527 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002528
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002529 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2530 aom_highbd_sad128x128_avg_bits12,
2531 aom_highbd_12_variance128x128,
2532 aom_highbd_12_sub_pixel_variance128x128,
2533 aom_highbd_12_sub_pixel_avg_variance128x128,
2534 aom_highbd_sad128x128x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002535 aom_highbd_dist_wtd_sad128x128_avg_bits12,
2536 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002537
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002538 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2539 aom_highbd_sad128x64_avg_bits12,
2540 aom_highbd_12_variance128x64,
2541 aom_highbd_12_sub_pixel_variance128x64,
2542 aom_highbd_12_sub_pixel_avg_variance128x64,
2543 aom_highbd_sad128x64x4d_bits12,
2544 aom_highbd_dist_wtd_sad128x64_avg_bits12,
2545 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002546
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002547 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2548 aom_highbd_sad64x128_avg_bits12,
2549 aom_highbd_12_variance64x128,
2550 aom_highbd_12_sub_pixel_variance64x128,
2551 aom_highbd_12_sub_pixel_avg_variance64x128,
2552 aom_highbd_sad64x128x4d_bits12,
2553 aom_highbd_dist_wtd_sad64x128_avg_bits12,
2554 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002555
David Barkerf19f35f2017-05-22 16:33:22 +01002556 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2557 aom_highbd_12_masked_sub_pixel_variance128x128)
2558 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2559 aom_highbd_12_masked_sub_pixel_variance128x64)
2560 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2561 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002562 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2563 aom_highbd_12_masked_sub_pixel_variance64x64)
2564 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2565 aom_highbd_12_masked_sub_pixel_variance64x32)
2566 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2567 aom_highbd_12_masked_sub_pixel_variance32x64)
2568 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2569 aom_highbd_12_masked_sub_pixel_variance32x32)
2570 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2571 aom_highbd_12_masked_sub_pixel_variance32x16)
2572 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2573 aom_highbd_12_masked_sub_pixel_variance16x32)
2574 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2575 aom_highbd_12_masked_sub_pixel_variance16x16)
2576 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2577 aom_highbd_12_masked_sub_pixel_variance8x16)
2578 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2579 aom_highbd_12_masked_sub_pixel_variance16x8)
2580 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2581 aom_highbd_12_masked_sub_pixel_variance8x8)
2582 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2583 aom_highbd_12_masked_sub_pixel_variance4x8)
2584 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2585 aom_highbd_12_masked_sub_pixel_variance8x4)
2586 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2587 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002588 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2589 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002590 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2591 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002592 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2593 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002594 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2595 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002596 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2597 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002598 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2599 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002600 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2601 aom_highbd_12_obmc_variance128x128,
2602 aom_highbd_12_obmc_sub_pixel_variance128x128)
2603 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2604 aom_highbd_12_obmc_variance128x64,
2605 aom_highbd_12_obmc_sub_pixel_variance128x64)
2606 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2607 aom_highbd_12_obmc_variance64x128,
2608 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002609 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2610 aom_highbd_12_obmc_variance64x64,
2611 aom_highbd_12_obmc_sub_pixel_variance64x64)
2612 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2613 aom_highbd_12_obmc_variance64x32,
2614 aom_highbd_12_obmc_sub_pixel_variance64x32)
2615 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2616 aom_highbd_12_obmc_variance32x64,
2617 aom_highbd_12_obmc_sub_pixel_variance32x64)
2618 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2619 aom_highbd_12_obmc_variance32x32,
2620 aom_highbd_12_obmc_sub_pixel_variance32x32)
2621 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2622 aom_highbd_12_obmc_variance32x16,
2623 aom_highbd_12_obmc_sub_pixel_variance32x16)
2624 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2625 aom_highbd_12_obmc_variance16x32,
2626 aom_highbd_12_obmc_sub_pixel_variance16x32)
2627 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2628 aom_highbd_12_obmc_variance16x16,
2629 aom_highbd_12_obmc_sub_pixel_variance16x16)
2630 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2631 aom_highbd_12_obmc_variance8x16,
2632 aom_highbd_12_obmc_sub_pixel_variance8x16)
2633 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2634 aom_highbd_12_obmc_variance16x8,
2635 aom_highbd_12_obmc_sub_pixel_variance16x8)
2636 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2637 aom_highbd_12_obmc_variance8x8,
2638 aom_highbd_12_obmc_sub_pixel_variance8x8)
2639 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2640 aom_highbd_12_obmc_variance4x8,
2641 aom_highbd_12_obmc_sub_pixel_variance4x8)
2642 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2643 aom_highbd_12_obmc_variance8x4,
2644 aom_highbd_12_obmc_sub_pixel_variance8x4)
2645 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2646 aom_highbd_12_obmc_variance4x4,
2647 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002648 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2649 aom_highbd_12_obmc_variance64x16,
2650 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002651 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2652 aom_highbd_12_obmc_variance16x64,
2653 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002654 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2655 aom_highbd_12_obmc_variance32x8,
2656 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002657 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2658 aom_highbd_12_obmc_variance8x32,
2659 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002660 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2661 aom_highbd_12_obmc_variance16x4,
2662 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002663 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2664 aom_highbd_12_obmc_variance4x16,
2665 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002666 break;
2667
2668 default:
2669 assert(0 &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002670 "cm->seq_params.bit_depth should be AOM_BITS_8, "
Yaowu Xuf883b422016-08-30 14:01:10 -07002671 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002672 }
2673 }
2674}
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002675#endif // CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002676
Yaowu Xuf883b422016-08-30 14:01:10 -07002677static void realloc_segmentation_maps(AV1_COMP *cpi) {
2678 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002679 CommonModeInfoParams *const mi_params = &cm->mi_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002680
2681 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002682 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002683 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002684 aom_calloc(mi_params->mi_rows * mi_params->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002685
2686 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002687 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002688 CHECK_MEM_ERROR(
2689 cm, cpi->cyclic_refresh,
2690 av1_cyclic_refresh_alloc(mi_params->mi_rows, mi_params->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002691
2692 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002693 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002694 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002695 aom_calloc(mi_params->mi_rows * mi_params->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002696}
2697
Vishesha9349502020-04-01 01:54:30 +05302698static AOM_INLINE void set_tpl_stats_block_size(int width, int height,
2699 uint8_t *block_mis_log2) {
Vishesh39d03622020-03-31 15:18:16 +05302700 const int is_720p_or_larger = AOMMIN(width, height) >= 720;
Yue Chenb4c93f02019-08-05 13:47:31 -07002701
2702 // 0: 4x4, 1: 8x8, 2: 16x16
Vishesha9349502020-04-01 01:54:30 +05302703 *block_mis_log2 = is_720p_or_larger ? 2 : 1;
Yue Chenb4c93f02019-08-05 13:47:31 -07002704}
2705
Hui Su38711e72019-06-11 10:49:47 -07002706void av1_alloc_compound_type_rd_buffers(AV1_COMMON *const cm,
2707 CompoundTypeRdBuffers *const bufs) {
2708 CHECK_MEM_ERROR(
2709 cm, bufs->pred0,
2710 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred0)));
2711 CHECK_MEM_ERROR(
2712 cm, bufs->pred1,
2713 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred1)));
2714 CHECK_MEM_ERROR(
2715 cm, bufs->residual1,
2716 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->residual1)));
2717 CHECK_MEM_ERROR(
2718 cm, bufs->diff10,
2719 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->diff10)));
2720 CHECK_MEM_ERROR(cm, bufs->tmp_best_mask_buf,
2721 (uint8_t *)aom_malloc(2 * MAX_SB_SQUARE *
2722 sizeof(*bufs->tmp_best_mask_buf)));
2723}
2724
2725void av1_release_compound_type_rd_buffers(CompoundTypeRdBuffers *const bufs) {
2726 aom_free(bufs->pred0);
2727 aom_free(bufs->pred1);
2728 aom_free(bufs->residual1);
2729 aom_free(bufs->diff10);
2730 aom_free(bufs->tmp_best_mask_buf);
2731 av1_zero(*bufs); // Set all pointers to NULL for safety.
2732}
2733
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002734static void config_target_level(AV1_COMP *const cpi, AV1_LEVEL target_level,
2735 int tier) {
2736 aom_clear_system_state();
2737
2738 AV1EncoderConfig *const oxcf = &cpi->oxcf;
2739 SequenceHeader *const seq_params = &cpi->common.seq_params;
2740
2741 // Adjust target bitrate to be no larger than 70% of level limit.
2742 const BITSTREAM_PROFILE profile = seq_params->profile;
2743 const double level_bitrate_limit =
2744 av1_get_max_bitrate_for_level(target_level, tier, profile);
2745 const int64_t max_bitrate = (int64_t)(level_bitrate_limit * 0.70);
2746 oxcf->target_bandwidth = AOMMIN(oxcf->target_bandwidth, max_bitrate);
2747 // Also need to update cpi->twopass.bits_left.
2748 TWO_PASS *const twopass = &cpi->twopass;
Mufaddal Chakera74c9cbe2020-01-17 16:44:59 +05302749 FIRSTPASS_STATS *stats = twopass->total_stats;
2750 if (stats != NULL)
2751 cpi->twopass.bits_left =
2752 (int64_t)(stats->duration * cpi->oxcf.target_bandwidth / 10000000.0);
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002753
2754 // Adjust max over-shoot percentage.
2755 oxcf->over_shoot_pct = 0;
2756
2757 // Adjust max quantizer.
2758 oxcf->worst_allowed_q = 255;
2759
2760 // Adjust number of tiles and tile columns to be under level limit.
2761 int max_tiles, max_tile_cols;
2762 av1_get_max_tiles_for_level(target_level, &max_tiles, &max_tile_cols);
2763 while (oxcf->tile_columns > 0 && (1 << oxcf->tile_columns) > max_tile_cols) {
2764 --oxcf->tile_columns;
2765 }
2766 const int tile_cols = (1 << oxcf->tile_columns);
2767 while (oxcf->tile_rows > 0 &&
2768 tile_cols * (1 << oxcf->tile_rows) > max_tiles) {
2769 --oxcf->tile_rows;
2770 }
2771
2772 // Adjust min compression ratio.
2773 const int still_picture = seq_params->still_picture;
2774 const double min_cr =
2775 av1_get_min_cr_for_level(target_level, tier, still_picture);
2776 oxcf->min_cr = AOMMAX(oxcf->min_cr, (unsigned int)(min_cr * 100));
2777}
2778
Yaowu Xuf883b422016-08-30 14:01:10 -07002779void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2780 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002781 SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002782 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002783 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002784 MACROBLOCK *const x = &cpi->td.mb;
Vishesh8ac928b2020-04-01 02:36:35 +05302785 AV1LevelParams *const level_params = &cpi->level_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002786
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002787 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2788 seq_params->bit_depth = oxcf->bit_depth;
2789 seq_params->color_primaries = oxcf->color_primaries;
2790 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2791 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2792 seq_params->monochrome = oxcf->monochrome;
2793 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2794 seq_params->color_range = oxcf->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002795
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002796 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2797 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002798
Urvang Joshi450a9a22020-03-31 16:00:22 -07002799 seq_params->timing_info_present = oxcf->timing_info_present;
2800 seq_params->timing_info.num_units_in_display_tick =
Andrey Norkin795ba872018-03-06 13:24:14 -08002801 oxcf->timing_info.num_units_in_display_tick;
Urvang Joshi450a9a22020-03-31 16:00:22 -07002802 seq_params->timing_info.time_scale = oxcf->timing_info.time_scale;
2803 seq_params->timing_info.equal_picture_interval =
Andrey Norkin795ba872018-03-06 13:24:14 -08002804 oxcf->timing_info.equal_picture_interval;
Urvang Joshi450a9a22020-03-31 16:00:22 -07002805 seq_params->timing_info.num_ticks_per_picture =
Andrey Norkin795ba872018-03-06 13:24:14 -08002806 oxcf->timing_info.num_ticks_per_picture;
2807
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002808 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07002809 oxcf->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002810 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002811 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08002812 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002813 // set the decoder model parameters in schedule mode
Urvang Joshi450a9a22020-03-31 16:00:22 -07002814 seq_params->decoder_model_info.num_units_in_decoding_tick =
Andrey Norkin795ba872018-03-06 13:24:14 -08002815 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07002816 cm->buffer_removal_time_present = 1;
Urvang Joshi450a9a22020-03-31 16:00:22 -07002817 av1_set_aom_dec_model_info(&seq_params->decoder_model_info);
2818 av1_set_dec_model_op_parameters(&seq_params->op_params[0]);
2819 } else if (seq_params->timing_info_present &&
2820 seq_params->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002821 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002822 // set the decoder model parameters in resource availability mode
Urvang Joshi450a9a22020-03-31 16:00:22 -07002823 av1_set_resource_availability_parameters(&seq_params->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07002824 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -07002825 seq_params->op_params[0].initial_display_delay =
Andrey Norkinc7511de2018-06-22 12:31:06 -07002826 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08002827 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002828
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002829 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002830
Yaowu Xuc27fc142016-08-22 16:08:15 -07002831 cpi->oxcf = *oxcf;
Urvang Joshif9c2bd72020-04-07 15:11:51 -07002832 cpi->superres_mode = oxcf->superres_mode; // default
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002833 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002834 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002835
Vishesh8ac928b2020-04-01 02:36:35 +05302836 memcpy(level_params->target_seq_level_idx, cpi->oxcf.target_seq_level_idx,
2837 sizeof(level_params->target_seq_level_idx));
2838 level_params->keep_level_stats = 0;
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002839 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Vishesh8ac928b2020-04-01 02:36:35 +05302840 if (level_params->target_seq_level_idx[i] <= SEQ_LEVELS) {
2841 level_params->keep_level_stats |= 1u << i;
2842 if (!level_params->level_info[i]) {
2843 CHECK_MEM_ERROR(cm, level_params->level_info[i],
2844 aom_calloc(1, sizeof(*level_params->level_info[i])));
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002845 }
2846 }
2847 }
2848
2849 // TODO(huisu@): level targeting currently only works for the 0th operating
2850 // point, so scalable coding is not supported yet.
Vishesh8ac928b2020-04-01 02:36:35 +05302851 if (level_params->target_seq_level_idx[0] < SEQ_LEVELS) {
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002852 // Adjust encoder config in order to meet target level.
Vishesh8ac928b2020-04-01 02:36:35 +05302853 config_target_level(cpi, level_params->target_seq_level_idx[0],
2854 seq_params->tier[0]);
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002855 }
2856
Mufaddal Chakera3bcc72c2019-12-11 14:38:37 +05302857 if ((has_no_stats_stage(cpi)) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002858 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2859 } else {
2860 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2861 }
2862
Yaowu Xuc27fc142016-08-22 16:08:15 -07002863 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002864 cpi->refresh_bwd_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002865
Urvang Joshi6237b882020-03-26 15:02:26 -07002866 cm->features.refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2867 ? REFRESH_FRAME_CONTEXT_DISABLED
2868 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002869 if (oxcf->large_scale_tile)
Urvang Joshi6237b882020-03-26 15:02:26 -07002870 cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002871
Alex Converse74ad0912017-07-18 10:22:58 -07002872 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002873 CHECK_MEM_ERROR(cm, x->palette_buffer,
2874 aom_memalign(16, sizeof(*x->palette_buffer)));
2875 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002876
Hui Su38711e72019-06-11 10:49:47 -07002877 if (x->comp_rd_buffer.pred0 == NULL) {
2878 av1_alloc_compound_type_rd_buffers(cm, &x->comp_rd_buffer);
2879 }
2880
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002881 if (x->tmp_conv_dst == NULL) {
2882 CHECK_MEM_ERROR(
2883 cm, x->tmp_conv_dst,
2884 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002885 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002886 }
2887 for (int i = 0; i < 2; ++i) {
2888 if (x->tmp_obmc_bufs[i] == NULL) {
2889 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
wenyao.liu22d8ab32018-10-16 09:11:29 +08002890 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002891 sizeof(*x->tmp_obmc_bufs[i])));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002892 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002893 }
2894 }
2895
Marco Paniconi177b8c32020-01-26 20:37:29 -08002896 av1_reset_segment_features(cm);
Fyodor Kyslov64aaa092020-01-24 11:44:24 -08002897
chiyotsaic666b1f2019-12-20 10:44:58 -08002898 av1_set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002899
Yaowu Xuc27fc142016-08-22 16:08:15 -07002900 set_rc_buffer_sizes(rc, &cpi->oxcf);
2901
2902 // Under a configuration change, where maximum_buffer_size may change,
2903 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002904 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2905 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002906
2907 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002908 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002909
2910 // Set absolute upper and lower quality limits
2911 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2912 rc->best_quality = cpi->oxcf.best_allowed_q;
2913
Urvang Joshi6237b882020-03-26 15:02:26 -07002914 cm->features.interp_filter =
2915 oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
2916 cm->features.switchable_motion_mode = 1;
Yue Chen5380cb52018-02-23 15:33:21 -08002917
Yaowu Xuc27fc142016-08-22 16:08:15 -07002918 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2919 cm->render_width = cpi->oxcf.render_width;
2920 cm->render_height = cpi->oxcf.render_height;
2921 } else {
2922 cm->render_width = cpi->oxcf.width;
2923 cm->render_height = cpi->oxcf.height;
2924 }
2925 cm->width = cpi->oxcf.width;
2926 cm->height = cpi->oxcf.height;
2927
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002928 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -08002929 // Superblock size should not be updated after the first key frame.
2930 if (!cpi->seq_params_locked) {
2931 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Hui Sud909c2c2019-03-08 11:51:14 -08002932 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
2933 seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
Urvang Joshie4530f82018-01-09 11:43:37 -08002934 }
Dominic Symes917d6c02017-10-11 18:00:52 +02002935
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002936 if (cpi->initial_width || sb_size != seq_params->sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002937 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002938 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002939 av1_free_context_buffers(cm);
Ravi Chaudhary0e5cd562020-04-02 11:22:24 +05302940 av1_free_pc_tree(cpi, &cpi->td, num_planes, (BLOCK_SIZE)sb_size);
Cheng Chen46f30c72017-09-07 11:13:33 -07002941 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002942 realloc_segmentation_maps(cpi);
2943 cpi->initial_width = cpi->initial_height = 0;
2944 }
2945 }
2946 update_frame_size(cpi);
2947
Yaowu Xuc27fc142016-08-22 16:08:15 -07002948 rc->is_src_frame_alt_ref = 0;
2949
Yaowu Xuc27fc142016-08-22 16:08:15 -07002950 set_tile_info(cpi);
2951
Marco Paniconid8574e32019-08-04 21:30:12 -07002952 if (!cpi->svc.external_ref_frame_config)
2953 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002954 cpi->ext_refresh_frame_context_pending = 0;
2955
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002956#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002957 highbd_set_var_fns(cpi);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002958#endif
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002959
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002960 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002961 // This should not be called after the first key frame.
2962 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002963 seq_params->operating_points_cnt_minus_1 =
Marco Paniconi6da983b2019-09-16 20:12:43 -07002964 (cm->number_spatial_layers > 1 || cm->number_temporal_layers > 1)
2965 ? cm->number_spatial_layers * cm->number_temporal_layers - 1
2966 : 0;
Marco Paniconi67142112019-07-24 15:00:31 -07002967 init_seq_coding_tools(&cm->seq_params, cm, oxcf, cpi->use_svc);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002968 }
Marco Paniconi5b1e4732019-08-08 18:57:53 -07002969
2970 if (cpi->use_svc)
2971 av1_update_layer_context_change_config(cpi, oxcf->target_bandwidth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002972}
2973
Vishesh39d03622020-03-31 15:18:16 +05302974static INLINE void setup_tpl_buffers(AV1_COMMON *const cm,
2975 TplParams *const tpl_data) {
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05302976 CommonModeInfoParams *const mi_params = &cm->mi_params;
Vishesh39d03622020-03-31 15:18:16 +05302977 set_tpl_stats_block_size(cm->width, cm->height,
2978 &tpl_data->tpl_stats_block_mis_log2);
Vishesha9349502020-04-01 01:54:30 +05302979 const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
2980
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05302981 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
2982 const int mi_cols =
2983 ALIGN_POWER_OF_TWO(mi_params->mi_cols, MAX_MIB_SIZE_LOG2);
2984 const int mi_rows =
2985 ALIGN_POWER_OF_TWO(mi_params->mi_rows, MAX_MIB_SIZE_LOG2);
2986
Vishesh39d03622020-03-31 15:18:16 +05302987 tpl_data->tpl_stats_buffer[frame].is_valid = 0;
Vishesha9349502020-04-01 01:54:30 +05302988 tpl_data->tpl_stats_buffer[frame].width = mi_cols >> block_mis_log2;
2989 tpl_data->tpl_stats_buffer[frame].height = mi_rows >> block_mis_log2;
Vishesh39d03622020-03-31 15:18:16 +05302990 tpl_data->tpl_stats_buffer[frame].stride =
2991 tpl_data->tpl_stats_buffer[frame].width;
2992 tpl_data->tpl_stats_buffer[frame].mi_rows = mi_params->mi_rows;
2993 tpl_data->tpl_stats_buffer[frame].mi_cols = mi_params->mi_cols;
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05302994 }
2995
2996 for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
2997 CHECK_MEM_ERROR(
Vishesh39d03622020-03-31 15:18:16 +05302998 cm, tpl_data->tpl_stats_pool[frame],
2999 aom_calloc(tpl_data->tpl_stats_buffer[frame].width *
3000 tpl_data->tpl_stats_buffer[frame].height,
3001 sizeof(*tpl_data->tpl_stats_buffer[frame].tpl_stats_ptr)));
Urvang Joshi6237b882020-03-26 15:02:26 -07003002 if (aom_alloc_frame_buffer(
Vishesh39d03622020-03-31 15:18:16 +05303003 &tpl_data->tpl_rec_pool[frame], cm->width, cm->height,
Urvang Joshi6237b882020-03-26 15:02:26 -07003004 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
3005 cm->seq_params.use_highbitdepth, AOM_ENC_NO_SCALE_BORDER,
3006 cm->features.byte_alignment))
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05303007 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3008 "Failed to allocate frame buffer");
3009 }
3010
Vishesh39d03622020-03-31 15:18:16 +05303011 tpl_data->tpl_frame = &tpl_data->tpl_stats_buffer[REF_FRAMES + 1];
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05303012}
3013
Urvang Joshi31a7aa52020-03-31 13:09:13 -07003014static INLINE void init_frame_info(FRAME_INFO *frame_info,
3015 const AV1_COMMON *const cm) {
3016 const CommonModeInfoParams *const mi_params = &cm->mi_params;
3017 const SequenceHeader *const seq_params = &cm->seq_params;
3018 frame_info->frame_width = cm->width;
3019 frame_info->frame_height = cm->height;
3020 frame_info->mi_cols = mi_params->mi_cols;
3021 frame_info->mi_rows = mi_params->mi_rows;
3022 frame_info->mb_cols = mi_params->mb_cols;
3023 frame_info->mb_rows = mi_params->mb_rows;
3024 frame_info->num_mbs = mi_params->MBs;
3025 frame_info->bit_depth = seq_params->bit_depth;
3026 frame_info->subsampling_x = seq_params->subsampling_x;
3027 frame_info->subsampling_y = seq_params->subsampling_y;
3028}
3029
Aasaipriyabd659e42019-12-13 16:12:13 +05303030AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf, BufferPool *const pool,
Mufaddal Chakera5517b282019-12-13 16:39:56 +05303031 FIRSTPASS_STATS *frame_stats_buf,
Akshata Jadhava49be172019-12-18 00:03:53 +05303032 COMPRESSOR_STAGE stage, int num_lap_buffers,
Mufaddal Chakera412efe22020-01-17 14:35:01 +05303033 int lap_lag_in_frames,
Akshata Jadhava49be172019-12-18 00:03:53 +05303034 STATS_BUFFER_CTX *stats_buf_context) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003035 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
3036 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003037
3038 if (!cm) return NULL;
3039
Yaowu Xuf883b422016-08-30 14:01:10 -07003040 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003041
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07003042 // The jmp_buf is valid only for the duration of the function that calls
3043 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
3044 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003045 if (setjmp(cm->error.jmp)) {
3046 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003047 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003048 return 0;
3049 }
3050
3051 cm->error.setjmp = 1;
Mufaddal Chakeraed7b1622020-04-06 12:30:23 +05303052 cpi->lap_enabled = num_lap_buffers > 0;
3053 cpi->compressor_stage = stage;
chiyotsaia7091f12019-08-09 16:48:27 -07003054
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003055 CommonModeInfoParams *const mi_params = &cm->mi_params;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003056 mi_params->free_mi = enc_free_mi;
3057 mi_params->setup_mi = enc_setup_mi;
Mufaddal Chakeraed7b1622020-04-06 12:30:23 +05303058 mi_params->set_mb_mi = (oxcf->pass == 1 || cpi->compressor_stage == LAP_STAGE)
3059 ? stat_stage_set_mb_mi
3060 : enc_set_mb_mi;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003061
3062 mi_params->mi_alloc_bsize = BLOCK_4X4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003063
Angie Chianga5d96c42016-10-21 16:16:56 -07003064 CHECK_MEM_ERROR(cm, cm->fc,
3065 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +00003066 CHECK_MEM_ERROR(
3067 cm, cm->default_frame_context,
3068 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -07003069 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +00003070 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003071
Yaowu Xuc27fc142016-08-22 16:08:15 -07003072 cpi->common.buffer_pool = pool;
3073
Mufaddal Chakera7b5265f2020-03-27 10:32:34 +05303074 init_config(cpi, oxcf);
Mufaddal Chakera11f284a2019-12-19 11:38:46 +05303075 if (cpi->compressor_stage == LAP_STAGE) {
Mufaddal Chakera412efe22020-01-17 14:35:01 +05303076 cpi->oxcf.lag_in_frames = lap_lag_in_frames;
Mufaddal Chakera11f284a2019-12-19 11:38:46 +05303077 }
Mufaddal Chakeraac828682019-12-13 16:31:42 +05303078
Yaowu Xuf883b422016-08-30 14:01:10 -07003079 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003080
Aasaipriyad1ef4602020-03-16 20:13:35 +05303081 cpi->rc.enable_scenecut_detection = 1;
3082 if (cpi->lap_enabled &&
3083 (num_lap_buffers < (MAX_GF_LENGTH_LAP + SCENE_CUT_KEY_TEST_INTERVAL + 1)))
3084 cpi->rc.enable_scenecut_detection = 0;
Jingning Han17af7742019-09-17 16:58:03 -07003085 init_frame_info(&cpi->frame_info, cm);
3086
David Turnerd2a592e2018-11-16 14:59:31 +00003087 cm->current_frame.frame_number = 0;
David Turnera4c96252019-01-11 16:36:39 +00003088 cm->current_frame_id = -1;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07003089 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003090 cpi->partition_search_skippable_frame = 0;
3091 cpi->tile_data = NULL;
David Turnere7ebf902018-12-04 14:04:55 +00003092 cpi->last_show_frame_buf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003093 realloc_segmentation_maps(cpi);
3094
Yaowu Xuc27fc142016-08-22 16:08:15 -07003095 cpi->refresh_alt_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003096
3097 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
3098#if CONFIG_INTERNAL_STATS
3099 cpi->b_calculate_blockiness = 1;
3100 cpi->b_calculate_consistency = 1;
3101 cpi->total_inconsistency = 0;
3102 cpi->psnr.worst = 100.0;
3103 cpi->worst_ssim = 100.0;
3104
3105 cpi->count = 0;
3106 cpi->bytes = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003107#if CONFIG_SPEED_STATS
3108 cpi->tx_search_count = 0;
3109#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003110
3111 if (cpi->b_calculate_psnr) {
3112 cpi->total_sq_error = 0;
3113 cpi->total_samples = 0;
3114 cpi->tot_recode_hits = 0;
3115 cpi->summed_quality = 0;
3116 cpi->summed_weights = 0;
3117 }
3118
3119 cpi->fastssim.worst = 100.0;
3120 cpi->psnrhvs.worst = 100.0;
3121
3122 if (cpi->b_calculate_blockiness) {
3123 cpi->total_blockiness = 0;
3124 cpi->worst_blockiness = 0.0;
3125 }
3126
3127 if (cpi->b_calculate_consistency) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003128 CHECK_MEM_ERROR(
3129 cm, cpi->ssim_vars,
3130 aom_malloc(sizeof(*cpi->ssim_vars) * 4 * cpi->common.mi_params.mi_rows *
3131 cpi->common.mi_params.mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003132 cpi->worst_consistency = 100.0;
3133 }
3134#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003135#if CONFIG_ENTROPY_STATS
3136 av1_zero(aggregate_fc);
3137#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003138
Deepa K Gfb89ce02020-04-06 13:34:42 +05303139 cpi->time_stamps.first_ever = INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003140
Yaowu Xuc27fc142016-08-22 16:08:15 -07003141#ifdef OUTPUT_YUV_SKINMAP
3142 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
3143#endif
3144#ifdef OUTPUT_YUV_REC
3145 yuv_rec_file = fopen("rec.yuv", "wb");
3146#endif
3147
Mufaddal Chakera5517b282019-12-13 16:39:56 +05303148 assert(MAX_LAP_BUFFERS >= MAX_LAG_BUFFERS);
Akshata Jadhav4be65112019-12-18 00:26:25 +05303149 int size = get_stats_buf_size(num_lap_buffers, MAX_LAG_BUFFERS);
3150 for (int i = 0; i < size; i++)
Aasaipriyabd659e42019-12-13 16:12:13 +05303151 cpi->twopass.frame_stats_arr[i] = &frame_stats_buf[i];
3152
Akshata Jadhava49be172019-12-18 00:03:53 +05303153 cpi->twopass.stats_buf_ctx = stats_buf_context;
3154 cpi->twopass.stats_in = cpi->twopass.stats_buf_ctx->stats_in_start;
3155
Jerome Jiang2612b4d2019-05-29 17:46:47 -07003156#if !CONFIG_REALTIME_ONLY
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303157 if (is_stat_generation_stage(cpi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003158 av1_init_first_pass(cpi);
Hamsalekha S37cc1d12019-12-12 19:27:41 +05303159 } else if (is_stat_consumption_stage(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003160 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
3161 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
3162
Akshata Jadhav1fddf7f2019-12-18 00:49:25 +05303163 if (!cpi->lap_enabled) {
3164 /*Re-initialize to stats buffer, populated by application in the case of
3165 * two pass*/
3166 cpi->twopass.stats_buf_ctx->stats_in_start = oxcf->two_pass_stats_in.buf;
3167 cpi->twopass.stats_in = cpi->twopass.stats_buf_ctx->stats_in_start;
3168 cpi->twopass.stats_buf_ctx->stats_in_end =
3169 &cpi->twopass.stats_buf_ctx->stats_in_start[packets - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003170
Mufaddal Chakera74c9cbe2020-01-17 16:44:59 +05303171 av1_init_second_pass(cpi);
3172 } else {
3173 av1_init_single_pass_lap(cpi);
3174 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003175 }
Jerome Jiang2612b4d2019-05-29 17:46:47 -07003176#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003177
Remya0cce44c2019-08-16 11:57:24 +05303178 int sb_mi_size = av1_get_sb_mi_size(cm);
3179
Jingning Hand064cf02017-06-01 10:00:39 -07003180 CHECK_MEM_ERROR(
3181 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08003182 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08003183 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07003184 CHECK_MEM_ERROR(
3185 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08003186 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08003187 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07003188
3189 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
3190 (int32_t *)aom_memalign(
3191 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
3192
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303193 CHECK_MEM_ERROR(
3194 cm, cpi->td.mb.inter_modes_info,
3195 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303196
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303197 for (int x = 0; x < 2; x++)
3198 for (int y = 0; y < 2; y++)
3199 CHECK_MEM_ERROR(
3200 cm, cpi->td.mb.hash_value_buffer[x][y],
3201 (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
3202 sizeof(*cpi->td.mb.hash_value_buffer[0][0])));
3203
3204 cpi->td.mb.g_crc_initialized = 0;
3205
Jingning Hand064cf02017-06-01 10:00:39 -07003206 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
3207 (int32_t *)aom_memalign(
3208 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
3209
Remya0cce44c2019-08-16 11:57:24 +05303210 CHECK_MEM_ERROR(cm, cpi->td.mb.mbmi_ext,
3211 aom_calloc(sb_mi_size, sizeof(*cpi->td.mb.mbmi_ext)));
3212
David Turner04b70d82019-01-24 15:39:19 +00003213 av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
3214 av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003215
sdengc23c7f12019-06-11 16:56:50 -07003216 {
3217 const int bsize = BLOCK_16X16;
3218 const int w = mi_size_wide[bsize];
3219 const int h = mi_size_high[bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003220 const int num_cols = (mi_params->mi_cols + w - 1) / w;
3221 const int num_rows = (mi_params->mi_rows + h - 1) / h;
sdengf46a1062019-08-04 18:43:50 -07003222 CHECK_MEM_ERROR(cm, cpi->tpl_rdmult_scaling_factors,
3223 aom_calloc(num_rows * num_cols,
3224 sizeof(*cpi->tpl_rdmult_scaling_factors)));
3225 CHECK_MEM_ERROR(cm, cpi->tpl_sb_rdmult_scaling_factors,
3226 aom_calloc(num_rows * num_cols,
3227 sizeof(*cpi->tpl_sb_rdmult_scaling_factors)));
3228 }
3229
3230 {
3231 const int bsize = BLOCK_16X16;
3232 const int w = mi_size_wide[bsize];
3233 const int h = mi_size_high[bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003234 const int num_cols = (mi_params->mi_cols + w - 1) / w;
3235 const int num_rows = (mi_params->mi_rows + h - 1) / h;
sdengc23c7f12019-06-11 16:56:50 -07003236 CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors,
3237 aom_calloc(num_rows * num_cols,
3238 sizeof(*cpi->ssim_rdmult_scaling_factors)));
3239 }
3240
sdeng01959162019-12-20 10:46:24 -08003241#if CONFIG_TUNE_VMAF
3242 {
3243 const int bsize = BLOCK_64X64;
3244 const int w = mi_size_wide[bsize];
3245 const int h = mi_size_high[bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003246 const int num_cols = (mi_params->mi_cols + w - 1) / w;
3247 const int num_rows = (mi_params->mi_rows + h - 1) / h;
sdeng01959162019-12-20 10:46:24 -08003248 CHECK_MEM_ERROR(cm, cpi->vmaf_rdmult_scaling_factors,
3249 aom_calloc(num_rows * num_cols,
3250 sizeof(*cpi->vmaf_rdmult_scaling_factors)));
sdeng152013e2020-03-18 14:20:31 -07003251 cpi->last_frame_unsharp_amount = 0.0;
sdeng01959162019-12-20 10:46:24 -08003252 }
3253#endif
3254
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05303255 if (!is_stat_generation_stage(cpi)) {
Vishesh39d03622020-03-31 15:18:16 +05303256 setup_tpl_buffers(cm, &cpi->tpl_data);
Jingning Hanf83d6812020-02-27 13:08:19 -08003257 }
Yue Chen7cae98f2018-08-24 10:43:16 -07003258
chiyotsai9c484b32019-03-07 16:01:50 -08003259#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08003260 av1_zero(cpi->partition_stats);
3261#endif
3262
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003263#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
3264 cpi->fn_ptr[BT].sdf = SDF; \
3265 cpi->fn_ptr[BT].sdaf = SDAF; \
3266 cpi->fn_ptr[BT].vf = VF; \
3267 cpi->fn_ptr[BT].svf = SVF; \
3268 cpi->fn_ptr[BT].svaf = SVAF; \
3269 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
3270 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07003271 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003272
Cheng Chenf78632e2017-10-20 15:30:51 -07003273 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003274 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003275 aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
3276 aom_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003277
3278 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003279 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003280 aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
3281 aom_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003282
3283 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003284 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003285 aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
3286 aom_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003287
3288 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003289 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003290 aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
3291 aom_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003292
3293 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003294 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003295 aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
3296 aom_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003297
3298 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003299 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003300 aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
3301 aom_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003302
Cheng Chenf78632e2017-10-20 15:30:51 -07003303 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
3304 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003305 aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
3306 aom_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07003307
3308 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003309 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003310 aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
3311 aom_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003312
3313 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003314 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003315 aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
3316 aom_dist_wtd_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07003317
3318 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003319 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003320 aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
3321 aom_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003322
3323 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003324 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003325 aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
3326 aom_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003327
3328 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003329 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003330 aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
3331 aom_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003332
3333 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003334 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003335 aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
3336 aom_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003337
3338 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
3339 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003340 aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
3341 aom_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003342
3343 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
3344 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003345 aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
3346 aom_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003347
3348 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
3349 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003350 aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
3351 aom_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003352
3353 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003354 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003355 aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
3356 aom_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003357
3358 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003359 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003360 aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
3361 aom_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003362
3363 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003364 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003365 aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003366
3367 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003368 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003369 aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003370
3371 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003372 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003373 aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003374
3375 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003376 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003377 aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003378
Yaowu Xuc27fc142016-08-22 16:08:15 -07003379#define OBFP(BT, OSDF, OVF, OSVF) \
3380 cpi->fn_ptr[BT].osdf = OSDF; \
3381 cpi->fn_ptr[BT].ovf = OVF; \
3382 cpi->fn_ptr[BT].osvf = OSVF;
3383
Yaowu Xuf883b422016-08-30 14:01:10 -07003384 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
3385 aom_obmc_sub_pixel_variance128x128)
3386 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
3387 aom_obmc_sub_pixel_variance128x64)
3388 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
3389 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07003390 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
3391 aom_obmc_sub_pixel_variance64x64)
3392 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
3393 aom_obmc_sub_pixel_variance64x32)
3394 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
3395 aom_obmc_sub_pixel_variance32x64)
3396 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
3397 aom_obmc_sub_pixel_variance32x32)
3398 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
3399 aom_obmc_sub_pixel_variance32x16)
3400 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
3401 aom_obmc_sub_pixel_variance16x32)
3402 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
3403 aom_obmc_sub_pixel_variance16x16)
3404 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
3405 aom_obmc_sub_pixel_variance16x8)
3406 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
3407 aom_obmc_sub_pixel_variance8x16)
3408 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
3409 aom_obmc_sub_pixel_variance8x8)
3410 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
3411 aom_obmc_sub_pixel_variance4x8)
3412 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
3413 aom_obmc_sub_pixel_variance8x4)
3414 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
3415 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003416 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
3417 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003418 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
3419 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003420 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
3421 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003422 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
3423 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003424 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
3425 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003426 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
3427 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003428
David Barkerf19f35f2017-05-22 16:33:22 +01003429#define MBFP(BT, MCSDF, MCSVF) \
3430 cpi->fn_ptr[BT].msdf = MCSDF; \
3431 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003432
David Barkerf19f35f2017-05-22 16:33:22 +01003433 MBFP(BLOCK_128X128, aom_masked_sad128x128,
3434 aom_masked_sub_pixel_variance128x128)
3435 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
3436 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01003437 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
3438 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
3439 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
3440 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
3441 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
3442 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
3443 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
3444 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
3445 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
3446 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
3447 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
3448 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
3449 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003450
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003451 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
3452
3453 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
3454
3455 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
3456
3457 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003458
3459 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
3460
3461 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01003462
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003463#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003464 highbd_set_var_fns(cpi);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003465#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003466
Yaowu Xuf883b422016-08-30 14:01:10 -07003467 /* av1_init_quantizer() is first called here. Add check in
3468 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07003469 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07003470 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003471 */
Yaowu Xuf883b422016-08-30 14:01:10 -07003472 av1_init_quantizer(cpi);
Urvang Joshi17814622020-03-27 17:26:17 -07003473 av1_qm_init(&cm->quant_params, av1_num_planes(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003474
Yaowu Xuf883b422016-08-30 14:01:10 -07003475 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07003476 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003477 cm->superres_upscaled_width = oxcf->width;
3478 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07003479 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003480
3481 cm->error.setjmp = 0;
3482
3483 return cpi;
3484}
3485
Urvang Joshiee2c8112018-05-04 14:53:15 -07003486#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003487#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
3488
3489#define SNPRINT2(H, T, V) \
3490 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07003491#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003492
Yaowu Xuf883b422016-08-30 14:01:10 -07003493void av1_remove_compressor(AV1_COMP *cpi) {
3494 AV1_COMMON *cm;
Vishesh39d03622020-03-31 15:18:16 +05303495 TplParams *const tpl_data = &cpi->tpl_data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003496 int t;
3497
3498 if (!cpi) return;
3499
3500 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003501 const int num_planes = av1_num_planes(cm);
3502
David Turnerd2a592e2018-11-16 14:59:31 +00003503 if (cm->current_frame.frame_number > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003504#if CONFIG_ENTROPY_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303505 if (!is_stat_generation_stage(cpi)) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003506 fprintf(stderr, "Writing counts.stt\n");
3507 FILE *f = fopen("counts.stt", "wb");
3508 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
3509 fclose(f);
3510 }
3511#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003512#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003513 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003514
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303515 if (!is_stat_generation_stage(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003516 char headings[512] = { 0 };
3517 char results[512] = { 0 };
3518 FILE *f = fopen("opsnr.stt", "a");
3519 double time_encoded =
Deepa K Gfb89ce02020-04-06 13:34:42 +05303520 (cpi->time_stamps.prev_end_seen - cpi->time_stamps.first_ever) /
Yaowu Xuc27fc142016-08-22 16:08:15 -07003521 10000000.000;
3522 double total_encode_time =
3523 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3524 const double dr =
3525 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3526 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3527 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3528 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3529
3530 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003531 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003532 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3533 const double total_ssim =
3534 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3535 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08003536 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003537 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003538 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07003539 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003540 snprintf(results, sizeof(results),
3541 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3542 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003543 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07003544 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003545 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
3546 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
3547 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
3548 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003549 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003550 cpi->psnr.stat[STAT_Y] / cpi->count,
3551 cpi->psnr.stat[STAT_U] / cpi->count,
Yue Chenf0652ed2019-08-13 16:09:25 -07003552 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003553
3554 if (cpi->b_calculate_blockiness) {
3555 SNPRINT(headings, "\t Block\tWstBlck");
3556 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3557 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3558 }
3559
3560 if (cpi->b_calculate_consistency) {
3561 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003562 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003563 (double)cpi->total_inconsistency);
3564
3565 SNPRINT(headings, "\tConsist\tWstCons");
3566 SNPRINT2(results, "\t%7.3f", consistency);
3567 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3568 }
Yue Chenf0652ed2019-08-13 16:09:25 -07003569
3570 SNPRINT(headings, "\t Time\tRcErr\tAbsErr");
3571 SNPRINT2(results, "\t%8.0f", total_encode_time);
3572 SNPRINT2(results, "\t%7.2f", rate_err);
3573 SNPRINT2(results, "\t%7.2f", fabs(rate_err));
3574
3575 fprintf(f, "%s\tAPsnr611\n", headings);
3576 fprintf(f, "%s\t%7.3f\n", results,
3577 (6 * cpi->psnr.stat[STAT_Y] + cpi->psnr.stat[STAT_U] +
3578 cpi->psnr.stat[STAT_V]) /
3579 (cpi->count * 8));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003580 }
3581
3582 fclose(f);
3583 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07003584#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003585#if CONFIG_SPEED_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303586 if (!is_stat_generation_stage(cpi)) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003587 fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
3588 }
3589#endif // CONFIG_SPEED_STATS
chiyotsai92ed0dd2019-01-25 14:50:14 -08003590
chiyotsai9c484b32019-03-07 16:01:50 -08003591#if CONFIG_COLLECT_PARTITION_STATS == 2
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303592 if (!is_stat_generation_stage(cpi)) {
chiyotsai92ed0dd2019-01-25 14:50:14 -08003593 av1_print_partition_stats(&cpi->partition_stats);
3594 }
3595#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003596 }
3597
Jingning Hanf83d6812020-02-27 13:08:19 -08003598 for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
Vishesh39d03622020-03-31 15:18:16 +05303599 aom_free(tpl_data->tpl_stats_pool[frame]);
3600 aom_free_frame_buffer(&tpl_data->tpl_rec_pool[frame]);
Yue Chen7cae98f2018-08-24 10:43:16 -07003601 }
3602
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303603 for (t = cpi->num_workers - 1; t >= 0; --t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003604 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003605 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3606
3607 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003608 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003609
3610 // Deallocate allocated thread data.
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303611 aom_free(thread_data->td->tctx);
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303612 if (t > 0) {
hui sud9a812b2017-07-06 14:34:37 -07003613 aom_free(thread_data->td->palette_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003614 aom_free(thread_data->td->tmp_conv_dst);
Hui Su38711e72019-06-11 10:49:47 -07003615 av1_release_compound_type_rd_buffers(&thread_data->td->comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003616 for (int j = 0; j < 2; ++j) {
3617 aom_free(thread_data->td->tmp_obmc_bufs[j]);
3618 }
Jingning Hand064cf02017-06-01 10:00:39 -07003619 aom_free(thread_data->td->above_pred_buf);
3620 aom_free(thread_data->td->left_pred_buf);
3621 aom_free(thread_data->td->wsrc_buf);
Fyodor Kyslov166648a2020-03-18 16:15:27 -07003622 aom_free(thread_data->td->vt64x64);
wenyao.liu22d8ab32018-10-16 09:11:29 +08003623
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303624 aom_free(thread_data->td->inter_modes_info);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003625 for (int x = 0; x < 2; x++) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303626 for (int y = 0; y < 2; y++) {
3627 aom_free(thread_data->td->hash_value_buffer[x][y]);
3628 thread_data->td->hash_value_buffer[x][y] = NULL;
3629 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003630 }
Jingning Hand064cf02017-06-01 10:00:39 -07003631 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003632 aom_free(thread_data->td->counts);
Ravi Chaudhary0e5cd562020-04-02 11:22:24 +05303633 av1_free_pc_tree(cpi, thread_data->td, num_planes,
3634 cm->seq_params.sb_size);
Remya0cce44c2019-08-16 11:57:24 +05303635 aom_free(thread_data->td->mbmi_ext);
Yaowu Xuf883b422016-08-30 14:01:10 -07003636 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003637 }
3638 }
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303639#if CONFIG_MULTITHREAD
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303640 if (cpi->row_mt_mutex_ != NULL) {
3641 pthread_mutex_destroy(cpi->row_mt_mutex_);
3642 aom_free(cpi->row_mt_mutex_);
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303643 }
3644#endif
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05303645 av1_row_mt_mem_dealloc(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07003646 aom_free(cpi->tile_thr_data);
3647 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003648
Deepa K G964e72e2018-05-16 16:56:01 +05303649 if (cpi->num_workers > 1) {
3650 av1_loop_filter_dealloc(&cpi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05303651 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
Deepa K G964e72e2018-05-16 16:56:01 +05303652 }
3653
Yaowu Xuc27fc142016-08-22 16:08:15 -07003654 dealloc_compressor_data(cpi);
3655
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003656#if CONFIG_INTERNAL_STATS
3657 aom_free(cpi->ssim_vars);
3658 cpi->ssim_vars = NULL;
3659#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003660
Yaowu Xuf883b422016-08-30 14:01:10 -07003661 av1_remove_common(cm);
Yaowu Xu74e63352019-05-06 09:21:33 -07003662#if CONFIG_HTB_TRELLIS
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08003663 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xu74e63352019-05-06 09:21:33 -07003664#endif // CONFIG_HTB_TRELLIS
Yaowu Xuf883b422016-08-30 14:01:10 -07003665 av1_free_ref_frame_buffers(cm->buffer_pool);
Mufaddal Chakera74c9cbe2020-01-17 16:44:59 +05303666
3667 aom_free(cpi->twopass.total_stats);
3668 aom_free(cpi->twopass.total_left_stats);
3669
Yaowu Xuf883b422016-08-30 14:01:10 -07003670 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003671
3672#ifdef OUTPUT_YUV_SKINMAP
3673 fclose(yuv_skinmap_file);
3674#endif
3675#ifdef OUTPUT_YUV_REC
3676 fclose(yuv_rec_file);
3677#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003678}
3679
Yaowu Xuf883b422016-08-30 14:01:10 -07003680static void generate_psnr_packet(AV1_COMP *cpi) {
3681 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003682 int i;
3683 PSNR_STATS psnr;
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003684#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xue75b58a2020-01-03 12:56:12 -08003685 const uint32_t in_bit_depth = cpi->oxcf.input_bit_depth;
3686 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
3687 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
3688 bit_depth, in_bit_depth);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003689#else
3690 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
3691#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003692
3693 for (i = 0; i < 4; ++i) {
3694 pkt.data.psnr.samples[i] = psnr.samples[i];
3695 pkt.data.psnr.sse[i] = psnr.sse[i];
3696 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3697 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003698 pkt.kind = AOM_CODEC_PSNR_PKT;
3699 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003700}
3701
Yaowu Xuf883b422016-08-30 14:01:10 -07003702int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003703 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3704
Yunqing Wangf2e7a392017-11-08 00:27:21 -08003705 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003706 return 0;
3707}
3708
Thomas Daede497d1952017-08-08 17:33:06 -07003709int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3710 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003711 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003712 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003713 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003714 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003715 return 0;
3716 } else {
3717 return -1;
3718 }
3719}
3720
Thomas Daede497d1952017-08-08 17:33:06 -07003721int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3722 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003723 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003724 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003725 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003726 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003727 return 0;
3728 } else {
3729 return -1;
3730 }
3731}
3732
3733int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003734 cpi->ext_refresh_frame_context = update;
3735 cpi->ext_refresh_frame_context_pending = 1;
3736 return 0;
3737}
3738
3739#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3740// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3741// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3742// not denoise the UV channels at this time. If ever we implement UV channel
3743// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003744void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003745 uint8_t *src = s->y_buffer;
3746 int h = s->y_height;
3747
3748 do {
3749 fwrite(src, s->y_width, 1, f);
3750 src += s->y_stride;
3751 } while (--h);
3752
3753 src = s->u_buffer;
3754 h = s->uv_height;
3755
3756 do {
3757 fwrite(src, s->uv_width, 1, f);
3758 src += s->uv_stride;
3759 } while (--h);
3760
3761 src = s->v_buffer;
3762 h = s->uv_height;
3763
3764 do {
3765 fwrite(src, s->uv_width, 1, f);
3766 src += s->uv_stride;
3767 } while (--h);
3768}
3769#endif
3770
Yaowu Xuc27fc142016-08-22 16:08:15 -07003771#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003772void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003773 uint8_t *src = s->y_buffer;
3774 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003775 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003776 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3777 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3778
3779 do {
3780 fwrite(src16, s->y_width, 2, yuv_rec_file);
3781 src16 += s->y_stride;
3782 } while (--h);
3783
3784 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3785 h = s->uv_height;
3786
3787 do {
3788 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3789 src16 += s->uv_stride;
3790 } while (--h);
3791
3792 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3793 h = s->uv_height;
3794
3795 do {
3796 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3797 src16 += s->uv_stride;
3798 } while (--h);
3799
3800 fflush(yuv_rec_file);
3801 return;
3802 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003803
3804 do {
3805 fwrite(src, s->y_width, 1, yuv_rec_file);
3806 src += s->y_stride;
3807 } while (--h);
3808
3809 src = s->u_buffer;
3810 h = s->uv_height;
3811
3812 do {
3813 fwrite(src, s->uv_width, 1, yuv_rec_file);
3814 src += s->uv_stride;
3815 } while (--h);
3816
3817 src = s->v_buffer;
3818 h = s->uv_height;
3819
3820 do {
3821 fwrite(src, s->uv_width, 1, yuv_rec_file);
3822 src += s->uv_stride;
3823 } while (--h);
3824
3825 fflush(yuv_rec_file);
3826}
3827#endif // OUTPUT_YUV_REC
3828
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003829#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Vishesh8ba8bfe2020-04-03 14:20:29 +05303830static int recode_loop_test_global_motion(
3831 WarpedMotionParams *const global_motion,
3832 const int *const global_motion_used, int *const gm_params_cost) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003833 int i;
3834 int recode = 0;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003835 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Vishesh8ba8bfe2020-04-03 14:20:29 +05303836 if (global_motion[i].wmtype != IDENTITY &&
3837 global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
3838 gm_params_cost[i]) {
3839 global_motion[i] = default_warp_params;
3840 assert(global_motion[i].wmtype == IDENTITY);
3841 gm_params_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003842 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003843 // TODO(sarahparker): The earlier condition for recoding here was:
3844 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3845 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003846 }
3847 }
3848 return recode;
3849}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003850
Yaowu Xuc27fc142016-08-22 16:08:15 -07003851// Function to test for conditions that indicate we should loop
3852// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003853static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3854 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003855 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003856 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003857 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3858 int force_recode = 0;
3859
3860 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
chiyotsai6b430132019-12-18 10:33:51 -08003861 (cpi->sf.hl_sf.recode_loop == ALLOW_RECODE) ||
3862 (frame_is_kfgfarf &&
3863 (cpi->sf.hl_sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003864 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3865 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3866 (rc->projected_frame_size < low_limit && q > minq)) {
3867 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003868 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003869 // Deal with frame undershoot and whether or not we are
3870 // below the automatically set cq level.
3871 if (q > oxcf->cq_level &&
3872 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3873 force_recode = 1;
3874 }
3875 }
3876 }
3877 return force_recode;
3878}
3879
Cheng Chen46f30c72017-09-07 11:13:33 -07003880static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003881 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003882 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003883 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003884
3885 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003886 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Jingning Han667561a2019-07-22 15:48:01 -07003887 if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003888 BufferPool *const pool = cm->buffer_pool;
3889 const YV12_BUFFER_CONFIG *const ref =
David Turnera21966b2018-12-05 14:48:49 +00003890 get_ref_frame_yv12_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003891
3892 if (ref == NULL) {
David Turnere7ebf902018-12-04 14:04:55 +00003893 cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003894 continue;
3895 }
3896
Yaowu Xuc27fc142016-08-22 16:08:15 -07003897 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
Debargha Mukherjee13cccf22019-03-27 23:49:14 -07003898 // Replace the reference buffer with a copy having a thicker border,
3899 // if the reference buffer is higher resolution than the current
3900 // frame, and the border is thin.
3901 if ((ref->y_crop_width > cm->width ||
3902 ref->y_crop_height > cm->height) &&
3903 ref->border < AOM_BORDER_IN_PIXELS) {
3904 RefCntBuffer *ref_fb = get_ref_frame_buf(cm, ref_frame);
3905 if (aom_yv12_realloc_with_new_border(
Urvang Joshi6237b882020-03-26 15:02:26 -07003906 &ref_fb->buf, AOM_BORDER_IN_PIXELS,
3907 cm->features.byte_alignment, num_planes) != 0) {
Debargha Mukherjee13cccf22019-03-27 23:49:14 -07003908 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3909 "Failed to allocate frame buffer");
3910 }
3911 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003912 int force_scaling = 0;
David Turnere7ebf902018-12-04 14:04:55 +00003913 RefCntBuffer *new_fb = cpi->scaled_ref_buf[ref_frame - 1];
3914 if (new_fb == NULL) {
3915 const int new_fb_idx = get_free_fb(cm);
3916 if (new_fb_idx == INVALID_IDX) {
Wan-Teh Chang4a8c0042018-10-05 09:41:52 -07003917 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3918 "Unable to find free frame buffer");
David Turnere7ebf902018-12-04 14:04:55 +00003919 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003920 force_scaling = 1;
David Turnere7ebf902018-12-04 14:04:55 +00003921 new_fb = &pool->frame_bufs[new_fb_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003922 }
David Turnere7ebf902018-12-04 14:04:55 +00003923
3924 if (force_scaling || new_fb->buf.y_crop_width != cm->width ||
3925 new_fb->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003926 if (aom_realloc_frame_buffer(
David Turnere7ebf902018-12-04 14:04:55 +00003927 &new_fb->buf, cm->width, cm->height,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003928 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
Debargha Mukherjeefa946af2019-03-26 16:58:55 -07003929 cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
Urvang Joshi6237b882020-03-26 15:02:26 -07003930 cm->features.byte_alignment, NULL, NULL, NULL)) {
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003931 if (force_scaling) {
3932 // Release the reference acquired in the get_free_fb() call above.
David Turnere7ebf902018-12-04 14:04:55 +00003933 --new_fb->ref_count;
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003934 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003935 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003936 "Failed to allocate frame buffer");
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003937 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003938 av1_resize_and_extend_frame(
David Turnere7ebf902018-12-04 14:04:55 +00003939 ref, &new_fb->buf, (int)cm->seq_params.bit_depth, num_planes);
3940 cpi->scaled_ref_buf[ref_frame - 1] = new_fb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003941 alloc_frame_mvs(cm, new_fb);
3942 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003943 } else {
David Turnera21966b2018-12-05 14:48:49 +00003944 RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003945 buf->buf.y_crop_width = ref->y_crop_width;
3946 buf->buf.y_crop_height = ref->y_crop_height;
David Turnere7ebf902018-12-04 14:04:55 +00003947 cpi->scaled_ref_buf[ref_frame - 1] = buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003948 ++buf->ref_count;
3949 }
3950 } else {
Mufaddal Chakera3bcc72c2019-12-11 14:38:37 +05303951 if (!has_no_stats_stage(cpi)) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003952 }
3953 }
3954}
3955
Yaowu Xuf883b422016-08-30 14:01:10 -07003956static void release_scaled_references(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003957 // TODO(isbs): only refresh the necessary frames, rather than all of them
David Turnere7ebf902018-12-04 14:04:55 +00003958 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3959 RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
3960 if (buf != NULL) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003961 --buf->ref_count;
David Turnere7ebf902018-12-04 14:04:55 +00003962 cpi->scaled_ref_buf[i] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003963 }
3964 }
3965}
3966
Yaowu Xuf883b422016-08-30 14:01:10 -07003967static void set_mv_search_params(AV1_COMP *cpi) {
3968 const AV1_COMMON *const cm = &cpi->common;
Nithya V Sace29f32020-04-07 16:15:12 +05303969 MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params;
Yunqing Wang4b7bf402020-01-28 15:20:53 -08003970 const int max_mv_def = AOMMAX(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003971
3972 // Default based on max resolution.
Nithya V Sace29f32020-04-07 16:15:12 +05303973 mv_search_params->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003974
chiyotsai8cc054a2019-12-12 14:57:43 -08003975 if (cpi->sf.mv_sf.auto_mv_step_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003976 if (frame_is_intra_only(cm)) {
3977 // Initialize max_mv_magnitude for use in the first INTER frame
3978 // after a key/intra-only frame.
Nithya V Sace29f32020-04-07 16:15:12 +05303979 mv_search_params->max_mv_magnitude = max_mv_def;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003980 } else {
Yunqing Wang4b7bf402020-01-28 15:20:53 -08003981 // Use cpi->max_mv_magnitude == -1 to exclude first pass case.
Nithya V Sace29f32020-04-07 16:15:12 +05303982 if (cm->show_frame && mv_search_params->max_mv_magnitude != -1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003983 // Allow mv_steps to correspond to twice the max mv magnitude found
3984 // in the previous frame, capped by the default max_mv_magnitude based
3985 // on resolution.
Nithya V Sace29f32020-04-07 16:15:12 +05303986 mv_search_params->mv_step_param = av1_init_search_range(
3987 AOMMIN(max_mv_def, 2 * mv_search_params->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003988 }
Nithya V Sace29f32020-04-07 16:15:12 +05303989 mv_search_params->max_mv_magnitude = -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003990 }
3991 }
3992}
3993
Yaowu Xubca61902020-04-08 08:41:47 -07003994void av1_set_screen_content_options(const AV1_COMP *cpi,
3995 FeatureFlags *features) {
Urvang Joshib6409e92020-03-23 11:23:27 -07003996 const AV1_COMMON *const cm = &cpi->common;
Hui Subdf0c992019-02-14 14:52:41 -08003997
3998 if (cm->seq_params.force_screen_content_tools != 2) {
Urvang Joshib6409e92020-03-23 11:23:27 -07003999 features->allow_screen_content_tools = features->allow_intrabc =
Hui Subdf0c992019-02-14 14:52:41 -08004000 cm->seq_params.force_screen_content_tools;
4001 return;
4002 }
4003
4004 if (cpi->oxcf.content == AOM_CONTENT_SCREEN) {
Urvang Joshib6409e92020-03-23 11:23:27 -07004005 features->allow_screen_content_tools = features->allow_intrabc = 1;
Hui Subdf0c992019-02-14 14:52:41 -08004006 return;
4007 }
4008
4009 // Estimate if the source frame is screen content, based on the portion of
4010 // blocks that have few luma colors.
Cheng Chen4f666ca2019-11-18 17:05:03 -08004011 const uint8_t *src = cpi->unfiltered_source->y_buffer;
Hui Subdf0c992019-02-14 14:52:41 -08004012 assert(src != NULL);
Cheng Chen4f666ca2019-11-18 17:05:03 -08004013 const int use_hbd = cpi->unfiltered_source->flags & YV12_FLAG_HIGHBITDEPTH;
4014 const int stride = cpi->unfiltered_source->y_stride;
4015 const int width = cpi->unfiltered_source->y_width;
4016 const int height = cpi->unfiltered_source->y_height;
Hui Subdf0c992019-02-14 14:52:41 -08004017 const int bd = cm->seq_params.bit_depth;
4018 const int blk_w = 16;
4019 const int blk_h = 16;
4020 // These threshold values are selected experimentally.
4021 const int color_thresh = 4;
4022 const unsigned int var_thresh = 0;
4023 // Counts of blocks with no more than color_thresh colors.
4024 int counts_1 = 0;
4025 // Counts of blocks with no more than color_thresh colors and variance larger
4026 // than var_thresh.
4027 int counts_2 = 0;
4028
4029 for (int r = 0; r + blk_h <= height; r += blk_h) {
4030 for (int c = 0; c + blk_w <= width; c += blk_w) {
4031 int count_buf[1 << 12]; // Maximum (1 << 12) color levels.
4032 const uint8_t *const this_src = src + r * stride + c;
4033 const int n_colors =
4034 use_hbd ? av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd,
4035 count_buf)
4036 : av1_count_colors(this_src, stride, blk_w, blk_h, count_buf);
4037 if (n_colors > 1 && n_colors <= color_thresh) {
4038 ++counts_1;
4039 struct buf_2d buf;
4040 buf.stride = stride;
4041 buf.buf = (uint8_t *)this_src;
4042 const unsigned int var =
4043 use_hbd
4044 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
4045 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
4046 if (var > var_thresh) ++counts_2;
4047 }
4048 }
4049 }
4050
4051 // The threshold values are selected experimentally.
Urvang Joshib6409e92020-03-23 11:23:27 -07004052 features->allow_screen_content_tools =
Hui Subdf0c992019-02-14 14:52:41 -08004053 counts_1 * blk_h * blk_w * 10 > width * height;
4054 // IntraBC would force loop filters off, so we use more strict rules that also
4055 // requires that the block has high variance.
Urvang Joshib6409e92020-03-23 11:23:27 -07004056 features->allow_intrabc = features->allow_screen_content_tools &&
4057 counts_2 * blk_h * blk_w * 12 > width * height;
Hui Subdf0c992019-02-14 14:52:41 -08004058}
4059
Yaowu Xuf883b422016-08-30 14:01:10 -07004060static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004061 int i;
Urvang Joshib6409e92020-03-23 11:23:27 -07004062 AV1_COMMON *const cm = &cpi->common;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004063 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07004064 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004065 }
Vishesh8ba8bfe2020-04-03 14:20:29 +05304066 cpi->gm_info.search_done = 0;
Yunqing Wang9411e432019-03-14 15:53:23 -07004067
David Turner04b70d82019-01-24 15:39:19 +00004068 av1_set_speed_features_framesize_independent(cpi, cpi->speed);
Yaowu Xuf883b422016-08-30 14:01:10 -07004069 av1_set_rd_speed_thresholds(cpi);
Urvang Joshi6237b882020-03-26 15:02:26 -07004070 cm->features.interp_filter = SWITCHABLE;
4071 cm->features.switchable_motion_mode = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004072}
4073
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004074#if !CONFIG_REALTIME_ONLY
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304075double av1_get_gfu_boost_projection_factor(double min_factor, double max_factor,
4076 int frame_count) {
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304077 double factor = sqrt((double)frame_count);
4078 factor = AOMMIN(factor, max_factor);
4079 factor = AOMMAX(factor, min_factor);
4080 factor = (200.0 + 10.0 * factor);
4081 return factor;
4082}
4083
4084static int get_gfu_boost_from_r0_lap(double min_factor, double max_factor,
4085 double r0, int frames_to_key) {
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304086 double factor = av1_get_gfu_boost_projection_factor(min_factor, max_factor,
4087 frames_to_key);
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304088 const int boost = (int)rint(factor / r0);
4089 return boost;
4090}
4091
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304092double av1_get_kf_boost_projection_factor(int frame_count) {
Aasaipriya197771d2020-02-06 20:11:21 +05304093 double factor = sqrt((double)frame_count);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07004094 factor = AOMMIN(factor, 10.0);
4095 factor = AOMMAX(factor, 4.0);
Aasaipriya197771d2020-02-06 20:11:21 +05304096 factor = (75.0 + 14.0 * factor);
4097 return factor;
4098}
4099
4100static int get_kf_boost_from_r0(double r0, int frames_to_key) {
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304101 double factor = av1_get_kf_boost_projection_factor(frames_to_key);
Aasaipriya197771d2020-02-06 20:11:21 +05304102 const int boost = (int)rint(factor / r0);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07004103 return boost;
4104}
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004105#endif
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07004106
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304107#define MIN_BOOST_COMBINE_FACTOR 4.0
4108#define MAX_BOOST_COMBINE_FACTOR 12.0
4109int combine_prior_with_tpl_boost(double min_factor, double max_factor,
4110 int prior_boost, int tpl_boost,
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07004111 int frames_to_key) {
4112 double factor = sqrt((double)frames_to_key);
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304113 double range = max_factor - min_factor;
4114 factor = AOMMIN(factor, max_factor);
4115 factor = AOMMAX(factor, min_factor);
4116 factor -= min_factor;
4117 int boost =
4118 (int)((factor * prior_boost + (range - factor) * tpl_boost) / range);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07004119 return boost;
4120}
4121
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004122#if !CONFIG_REALTIME_ONLY
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004123static void process_tpl_stats_frame(AV1_COMP *cpi) {
Sarah Parkere1b22012019-06-06 16:35:25 -07004124 const GF_GROUP *const gf_group = &cpi->gf_group;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004125 AV1_COMMON *const cm = &cpi->common;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004126
Sarah Parkere1b22012019-06-06 16:35:25 -07004127 assert(IMPLIES(gf_group->size > 0, gf_group->index < gf_group->size));
Jingning Han31a0ee92019-07-15 13:56:55 -07004128
4129 const int tpl_idx = gf_group->index;
Vishesh39d03622020-03-31 15:18:16 +05304130 TplParams *const tpl_data = &cpi->tpl_data;
4131 TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
Yue Chen4e585cc2019-06-03 14:47:16 -07004132 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004133
Yue Chen4e585cc2019-06-03 14:47:16 -07004134 if (tpl_frame->is_valid) {
4135 int tpl_stride = tpl_frame->stride;
4136 int64_t intra_cost_base = 0;
4137 int64_t mc_dep_cost_base = 0;
4138 int64_t mc_saved_base = 0;
4139 int64_t mc_count_base = 0;
Vishesh39d03622020-03-31 15:18:16 +05304140 const int step = 1 << tpl_data->tpl_stats_block_mis_log2;
Debargha Mukherjeeedfa4fe2019-07-08 14:23:39 -07004141 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
Yue Chenb4c93f02019-08-05 13:47:31 -07004142
Urvang Joshi9dc909d2020-03-23 16:07:02 -07004143 for (int row = 0; row < cm->mi_params.mi_rows; row += step) {
Yue Chenb4c93f02019-08-05 13:47:31 -07004144 for (int col = 0; col < mi_cols_sr; col += step) {
Vishesha9349502020-04-01 01:54:30 +05304145 TplDepStats *this_stats = &tpl_stats[av1_tpl_ptr_pos(
4146 row, col, tpl_stride, tpl_data->tpl_stats_block_mis_log2)];
Jingning Han40e870f2019-10-02 16:05:39 -07004147 int64_t mc_dep_delta =
4148 RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
4149 this_stats->mc_dep_dist);
4150 intra_cost_base += (this_stats->recrf_dist << RDDIV_BITS);
4151 mc_dep_cost_base +=
4152 (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta;
Yue Chen4e585cc2019-06-03 14:47:16 -07004153 mc_count_base += this_stats->mc_count;
4154 mc_saved_base += this_stats->mc_saved;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004155 }
Yue Chen4e585cc2019-06-03 14:47:16 -07004156 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004157
Yue Chen4e585cc2019-06-03 14:47:16 -07004158 if (mc_dep_cost_base == 0) {
4159 tpl_frame->is_valid = 0;
4160 } else {
4161 aom_clear_system_state();
4162 cpi->rd.r0 = (double)intra_cost_base / mc_dep_cost_base;
Jingning Han44a573b2019-09-04 14:01:22 -07004163 if (is_frame_arf_and_tpl_eligible(gf_group)) {
Yue Chen4e585cc2019-06-03 14:47:16 -07004164 cpi->rd.arf_r0 = cpi->rd.r0;
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304165 if (cpi->lap_enabled) {
4166 double min_boost_factor = sqrt(cpi->rc.baseline_gf_interval);
4167 const int gfu_boost = get_gfu_boost_from_r0_lap(
4168 min_boost_factor, MAX_GFUBOOST_FACTOR, cpi->rd.arf_r0,
4169 cpi->rc.num_stats_required_for_gfu_boost);
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304170 // printf("old boost %d new boost %d\n", cpi->rc.gfu_boost,
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304171 // gfu_boost);
4172 cpi->rc.gfu_boost = combine_prior_with_tpl_boost(
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304173 min_boost_factor, MAX_BOOST_COMBINE_FACTOR, cpi->rc.gfu_boost,
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304174 gfu_boost, cpi->rc.num_stats_used_for_gfu_boost);
4175 } else {
4176 const int gfu_boost = (int)(200.0 / cpi->rd.r0);
4177 cpi->rc.gfu_boost = combine_prior_with_tpl_boost(
4178 MIN_BOOST_COMBINE_FACTOR, MAX_BOOST_COMBINE_FACTOR,
4179 cpi->rc.gfu_boost, gfu_boost, cpi->rc.frames_to_key);
4180 }
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07004181 } else if (frame_is_intra_only(cm)) {
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07004182 // TODO(debargha): Turn off q adjustment for kf temporarily to
4183 // reduce impact on speed of encoding. Need to investigate how
4184 // to mitigate the issue.
4185 if (cpi->oxcf.rc_mode == AOM_Q) {
4186 const int kf_boost =
4187 get_kf_boost_from_r0(cpi->rd.r0, cpi->rc.frames_to_key);
Aasaipriya197771d2020-02-06 20:11:21 +05304188 if (cpi->lap_enabled) {
Aasaipriya197771d2020-02-06 20:11:21 +05304189 cpi->rc.kf_boost = combine_prior_with_tpl_boost(
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304190 MIN_BOOST_COMBINE_FACTOR, MAX_BOOST_COMBINE_FACTOR,
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304191 cpi->rc.kf_boost, kf_boost,
Aasaipriya197771d2020-02-06 20:11:21 +05304192 cpi->rc.num_stats_used_for_kf_boost);
4193 } else {
4194 cpi->rc.kf_boost = combine_prior_with_tpl_boost(
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304195 MIN_BOOST_COMBINE_FACTOR, MAX_BOOST_COMBINE_FACTOR,
Aasaipriya197771d2020-02-06 20:11:21 +05304196 cpi->rc.kf_boost, kf_boost, cpi->rc.frames_to_key);
4197 }
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07004198 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004199 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -07004200 cpi->rd.mc_count_base = (double)mc_count_base /
4201 (cm->mi_params.mi_rows * cm->mi_params.mi_cols);
4202 cpi->rd.mc_saved_base = (double)mc_saved_base /
4203 (cm->mi_params.mi_rows * cm->mi_params.mi_cols);
Yue Chen4e585cc2019-06-03 14:47:16 -07004204 aom_clear_system_state();
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004205 }
4206 }
4207}
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004208#endif // !CONFIG_REALTIME_ONLY
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004209
Yaowu Xuf883b422016-08-30 14:01:10 -07004210static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004211 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004212 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004213
4214 // Setup variables that depend on the dimensions of the frame.
David Turner04b70d82019-01-24 15:39:19 +00004215 av1_set_speed_features_framesize_dependent(cpi, cpi->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004216
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004217#if !CONFIG_REALTIME_ONLY
chiyotsai0fa717c2020-02-05 12:22:01 -08004218 if (cpi->oxcf.enable_tpl_model && is_frame_tpl_eligible(cpi)) {
Yue Chen4e585cc2019-06-03 14:47:16 -07004219 process_tpl_stats_frame(cpi);
sdengf46a1062019-08-04 18:43:50 -07004220 av1_tpl_rdmult_setup(cpi);
4221 }
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004222#endif
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004223
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01004224 // Decide q and q bounds.
Angie Chiang958904c2019-09-19 16:03:35 -07004225 *q = av1_rc_pick_q_and_bounds(cpi, &cpi->rc, cm->width, cm->height,
4226 cpi->gf_group.index, bottom_index, top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004227
Yaowu Xuc27fc142016-08-22 16:08:15 -07004228 // Configure experimental use of segmentation for enhanced coding of
4229 // static regions if indicated.
4230 // Only allowed in the second pass of a two pass encode, as it requires
4231 // lagged coding, and if the relevant speed feature flag is set.
chiyotsai6b430132019-12-18 10:33:51 -08004232 if (is_stat_consumption_stage_twopass(cpi) &&
4233 cpi->sf.hl_sf.static_segmentation)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004234 configure_static_seg_features(cpi);
4235}
4236
Yaowu Xuf883b422016-08-30 14:01:10 -07004237static void init_motion_estimation(AV1_COMP *cpi) {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004238 AV1_COMMON *const cm = &cpi->common;
Nithya V Sace29f32020-04-07 16:15:12 +05304239 MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params;
chiyotsai836b69b2019-04-09 13:41:24 -07004240 const int y_stride = cpi->scaled_source.y_stride;
Urvang Joshi510d8f62019-01-10 12:11:50 -08004241 const int y_stride_src =
4242 ((cpi->oxcf.width != cm->width || cpi->oxcf.height != cm->height) ||
4243 av1_superres_scaled(cm))
4244 ? y_stride
4245 : cpi->lookahead->buf->img.y_stride;
Mufaddal Chakera975f73a2020-03-31 13:22:25 +05304246 int fpf_y_stride = cm->cur_frame != NULL ? cm->cur_frame->buf.y_stride
4247 : cpi->scaled_source.y_stride;
4248
chiyotsai836b69b2019-04-09 13:41:24 -07004249 // Update if ss_cfg is uninitialized or the current frame has a new stride
Nithya V Sace29f32020-04-07 16:15:12 +05304250 const int should_update =
4251 !mv_search_params->ss_cfg[SS_CFG_SRC].stride ||
4252 !mv_search_params->ss_cfg[SS_CFG_LOOKAHEAD].stride ||
4253 (y_stride != mv_search_params->ss_cfg[SS_CFG_SRC].stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004254
chiyotsai836b69b2019-04-09 13:41:24 -07004255 if (!should_update) {
4256 return;
4257 }
4258
chiyotsai8cc054a2019-12-12 14:57:43 -08004259 if (cpi->sf.mv_sf.search_method == DIAMOND) {
Nithya V Sace29f32020-04-07 16:15:12 +05304260 av1_init_dsmotion_compensation(&mv_search_params->ss_cfg[SS_CFG_SRC],
4261 y_stride);
4262 av1_init_dsmotion_compensation(&mv_search_params->ss_cfg[SS_CFG_LOOKAHEAD],
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05304263 y_stride_src);
chiyotsai836b69b2019-04-09 13:41:24 -07004264 } else {
Nithya V Sace29f32020-04-07 16:15:12 +05304265 av1_init3smotion_compensation(&mv_search_params->ss_cfg[SS_CFG_SRC],
4266 y_stride);
4267 av1_init3smotion_compensation(&mv_search_params->ss_cfg[SS_CFG_LOOKAHEAD],
4268 y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004269 }
Nithya V Sace29f32020-04-07 16:15:12 +05304270 av1_init_motion_fpf(&mv_search_params->ss_cfg[SS_CFG_FPF], fpf_y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004271}
4272
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004273#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004274static void set_restoration_unit_size(int width, int height, int sx, int sy,
4275 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004276 (void)width;
4277 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004278 (void)sx;
4279 (void)sy;
4280#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
4281 int s = AOMMIN(sx, sy);
4282#else
4283 int s = 0;
4284#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
4285
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07004286 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08004287 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07004288 else
Urvang Joshi813186b2018-03-08 15:38:46 -08004289 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004290 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
4291 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004292}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004293
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05304294static void init_ref_frame_bufs(AV1_COMP *cpi) {
4295 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07004296 int i;
4297 BufferPool *const pool = cm->buffer_pool;
Jack Haughtonddb80602018-11-21 16:41:49 +00004298 cm->cur_frame = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -07004299 for (i = 0; i < REF_FRAMES; ++i) {
David Turnere7ebf902018-12-04 14:04:55 +00004300 cm->ref_frame_map[i] = NULL;
Wan-Teh Changd05e0332018-10-03 12:00:43 -07004301 }
4302 for (i = 0; i < FRAME_BUFFERS; ++i) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004303 pool->frame_bufs[i].ref_count = 0;
4304 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004305}
4306
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05304307void av1_check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
4308 int subsampling_x, int subsampling_y) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004309 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004310 SequenceHeader *const seq_params = &cm->seq_params;
Cheng Chen46f30c72017-09-07 11:13:33 -07004311
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004312 if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
4313 seq_params->subsampling_x != subsampling_x ||
4314 seq_params->subsampling_y != subsampling_y) {
4315 seq_params->subsampling_x = subsampling_x;
4316 seq_params->subsampling_y = subsampling_y;
4317 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07004318
Jingning Han183b2a82019-12-18 16:03:41 -08004319 av1_set_speed_features_framesize_independent(cpi, cpi->oxcf.speed);
4320 av1_set_speed_features_framesize_dependent(cpi, cpi->oxcf.speed);
4321
Mufaddal Chakera975f73a2020-03-31 13:22:25 +05304322 if (!is_stat_generation_stage(cpi)) {
4323 alloc_altref_frame_buffer(cpi);
4324 alloc_util_frame_buffers(cpi);
4325 }
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05304326 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07004327
4328 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
4329
4330 cpi->initial_width = cm->width;
4331 cpi->initial_height = cm->height;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07004332 cpi->initial_mbs = cm->mi_params.MBs;
Cheng Chen46f30c72017-09-07 11:13:33 -07004333 }
4334}
4335
4336// Returns 1 if the assigned width or height was <= 0.
Marco Paniconi63971322019-08-15 21:32:05 -07004337int av1_set_size_literal(AV1_COMP *cpi, int width, int height) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004338 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004339 const int num_planes = av1_num_planes(cm);
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05304340 av1_check_initial_width(cpi, cm->seq_params.use_highbitdepth,
4341 cm->seq_params.subsampling_x,
4342 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07004343
4344 if (width <= 0 || height <= 0) return 1;
4345
4346 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07004347 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004348
4349 if (cpi->initial_width && cpi->initial_height &&
4350 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
4351 av1_free_context_buffers(cm);
Ravi Chaudhary0e5cd562020-04-02 11:22:24 +05304352 av1_free_pc_tree(cpi, &cpi->td, num_planes, cm->seq_params.sb_size);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004353 alloc_compressor_data(cpi);
4354 realloc_segmentation_maps(cpi);
4355 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07004356 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004357 update_frame_size(cpi);
4358
4359 return 0;
4360}
4361
David Turner475a3132019-01-18 15:17:17 +00004362void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07004363 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004364 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004365 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004366 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004367 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004368
Fergus Simpsonbc189932017-05-16 17:02:39 -07004369 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07004370 // There has been a change in the encoded frame size
Marco Paniconi63971322019-08-15 21:32:05 -07004371 av1_set_size_literal(cpi, width, height);
Urvang Joshic8b52d52018-03-23 13:16:51 -07004372 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Urvang Joshib6409e92020-03-23 11:23:27 -07004373 cm->features.all_lossless =
4374 cm->features.coded_lossless && !av1_superres_scaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004375 }
Yunqing Wang4b7bf402020-01-28 15:20:53 -08004376 set_mv_search_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004377
Hamsalekha S37cc1d12019-12-12 19:27:41 +05304378 if (is_stat_consumption_stage(cpi)) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004379 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004380 }
4381
David Turnere7ebf902018-12-04 14:04:55 +00004382 alloc_frame_mvs(cm, cm->cur_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004383
Cherma Rajan A71d20db2018-04-27 11:15:32 +05304384 // Allocate above context buffers
Urvang Joshi5c8625a2020-03-30 13:16:37 -07004385 CommonContexts *const above_contexts = &cm->above_contexts;
4386 if (above_contexts->num_planes < av1_num_planes(cm) ||
4387 above_contexts->num_mi_cols < cm->mi_params.mi_cols ||
4388 above_contexts->num_tile_rows < cm->tiles.rows) {
4389 av1_free_above_context_buffers(above_contexts);
4390 if (av1_alloc_above_context_buffers(above_contexts, cm->tiles.rows,
4391 cm->mi_params.mi_cols,
4392 av1_num_planes(cm)))
Cherma Rajan A71d20db2018-04-27 11:15:32 +05304393 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
4394 "Failed to allocate context buffers");
4395 }
4396
Yaowu Xuc27fc142016-08-22 16:08:15 -07004397 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004398 if (aom_realloc_frame_buffer(
Jack Haughtonddb80602018-11-21 16:41:49 +00004399 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
4400 seq_params->subsampling_y, seq_params->use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07004401 cpi->oxcf.border_in_pixels, cm->features.byte_alignment, NULL, NULL,
4402 NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07004403 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004404 "Failed to allocate frame buffer");
4405
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004406 const int frame_width = cm->superres_upscaled_width;
4407 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004408 set_restoration_unit_size(frame_width, frame_height,
4409 seq_params->subsampling_x,
4410 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004411 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01004412 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004413
4414 av1_alloc_restoration_buffers(cm);
Mufaddal Chakera975f73a2020-03-31 13:22:25 +05304415 if (!is_stat_generation_stage(cpi)) alloc_util_frame_buffers(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004416 init_motion_estimation(cpi);
4417
4418 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00004419 RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00004420 if (buf != NULL) {
David Turnera21966b2018-12-05 14:48:49 +00004421 struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
4422 av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
David Turner1bcefb32018-11-19 17:54:00 +00004423 buf->buf.y_crop_height, cm->width,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08004424 cm->height);
David Turnera21966b2018-12-05 14:48:49 +00004425 if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004426 }
4427 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07004428
Hui Su5ebd8702018-01-08 18:09:20 -08004429 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08004430 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004431
4432 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
4433}
4434
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004435static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
4436 // Choose an arbitrary random number
4437 static unsigned int seed = 56789;
4438 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Mufaddal Chakerae7326122019-12-04 14:49:09 +05304439 if (is_stat_generation_stage(cpi)) return SCALE_NUMERATOR;
Urvang Joshide71d142017-10-05 12:12:15 -07004440 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004441
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07004442 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004443 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004444 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004445 case RESIZE_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00004446 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004447 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004448 else
Urvang Joshide71d142017-10-05 12:12:15 -07004449 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004450 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004451 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004452 default: assert(0);
4453 }
Urvang Joshide71d142017-10-05 12:12:15 -07004454 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004455}
4456
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004457#if CONFIG_SUPERRES_IN_RECODE
4458static int superres_in_recode_allowed(const AV1_COMP *const cpi) {
4459 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
4460 // Empirically found to not be beneficial for AOM_Q mode and images coding.
4461 return oxcf->superres_mode == SUPERRES_AUTO &&
4462 (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ) &&
4463 cpi->rc.frames_to_key > 1;
4464}
4465#endif // CONFIG_SUPERRES_IN_RECODE
4466
Urvang Joshi510d8f62019-01-10 12:11:50 -08004467#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO 0.012
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004468#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME 0.008
4469#define SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME 0.008
Urvang Joshi510d8f62019-01-10 12:11:50 -08004470#define SUPERRES_ENERGY_BY_AC_THRESH 0.2
4471
4472static double get_energy_by_q2_thresh(const GF_GROUP *gf_group,
4473 const RATE_CONTROL *rc) {
4474 // TODO(now): Return keyframe thresh * factor based on frame type / pyramid
4475 // level.
4476 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
4477 return SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME;
4478 } else if (gf_group->update_type[gf_group->index] == KF_UPDATE) {
4479 if (rc->frames_to_key <= 1)
4480 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO;
4481 else
4482 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME;
4483 } else {
4484 assert(0);
4485 }
4486 return 0;
4487}
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004488
4489static uint8_t get_superres_denom_from_qindex_energy(int qindex, double *energy,
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004490 double threshq,
4491 double threshp) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004492 const double q = av1_convert_qindex_to_q(qindex, AOM_BITS_8);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004493 const double tq = threshq * q * q;
4494 const double tp = threshp * energy[1];
4495 const double thresh = AOMMIN(tq, tp);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004496 int k;
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004497 for (k = SCALE_NUMERATOR * 2; k > SCALE_NUMERATOR; --k) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004498 if (energy[k - 1] > thresh) break;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004499 }
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004500 return 3 * SCALE_NUMERATOR - k;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004501}
4502
Urvang Joshi510d8f62019-01-10 12:11:50 -08004503static uint8_t get_superres_denom_for_qindex(const AV1_COMP *cpi, int qindex,
4504 int sr_kf, int sr_arf) {
4505 // Use superres for Key-frames and Alt-ref frames only.
4506 const GF_GROUP *gf_group = &cpi->gf_group;
4507 if (gf_group->update_type[gf_group->index] != KF_UPDATE &&
4508 gf_group->update_type[gf_group->index] != ARF_UPDATE) {
4509 return SCALE_NUMERATOR;
4510 }
4511 if (gf_group->update_type[gf_group->index] == KF_UPDATE && !sr_kf) {
4512 return SCALE_NUMERATOR;
4513 }
4514 if (gf_group->update_type[gf_group->index] == ARF_UPDATE && !sr_arf) {
4515 return SCALE_NUMERATOR;
4516 }
4517
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004518 double energy[16];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004519 analyze_hor_freq(cpi, energy);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004520
4521 const double energy_by_q2_thresh =
4522 get_energy_by_q2_thresh(gf_group, &cpi->rc);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004523 int denom = get_superres_denom_from_qindex_energy(
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004524 qindex, energy, energy_by_q2_thresh, SUPERRES_ENERGY_BY_AC_THRESH);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004525 /*
4526 printf("\nenergy = [");
4527 for (int k = 1; k < 16; ++k) printf("%f, ", energy[k]);
4528 printf("]\n");
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004529 printf("boost = %d\n",
4530 (gf_group->update_type[gf_group->index] == KF_UPDATE)
4531 ? cpi->rc.kf_boost
4532 : cpi->rc.gfu_boost);
4533 printf("denom = %d\n", denom);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004534 */
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004535#if CONFIG_SUPERRES_IN_RECODE
4536 if (superres_in_recode_allowed(cpi)) {
Urvang Joshif9c2bd72020-04-07 15:11:51 -07004537 assert(cpi->superres_mode != SUPERRES_NONE);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004538 // Force superres to be tried in the recode loop, as full-res is also going
4539 // to be tried anyway.
4540 denom = AOMMAX(denom, SCALE_NUMERATOR + 1);
4541 }
4542#endif // CONFIG_SUPERRES_IN_RECODE
Urvang Joshi510d8f62019-01-10 12:11:50 -08004543 return denom;
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004544}
4545
Urvang Joshibc82d382019-11-01 17:59:20 -07004546// If true, SUPERRES_AUTO mode will exhaustively search over all superres
4547// denominators for all frames (except overlay and internal overlay frames).
4548#define SUPERRES_RECODE_ALL_RATIOS 0
4549
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004550static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
4551 // Choose an arbitrary random number
4552 static unsigned int seed = 34567;
4553 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Mufaddal Chakerae7326122019-12-04 14:49:09 +05304554 if (is_stat_generation_stage(cpi)) return SCALE_NUMERATOR;
Urvang Joshide71d142017-10-05 12:12:15 -07004555 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07004556
4557 // Make sure that superres mode of the frame is consistent with the
4558 // sequence-level flag.
4559 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
4560 cpi->common.seq_params.enable_superres));
4561 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
4562 oxcf->superres_mode == SUPERRES_NONE));
Urvang Joshif9c2bd72020-04-07 15:11:51 -07004563 // Make sure that superres mode for current encoding is consistent with user
4564 // provided superres mode.
4565 assert(IMPLIES(oxcf->superres_mode != SUPERRES_AUTO,
4566 cpi->superres_mode == oxcf->superres_mode));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004567
Urvang Joshif9c2bd72020-04-07 15:11:51 -07004568 // Note: we must look at the current superres_mode to be tried in 'cpi' here,
4569 // not the user given mode in 'oxcf'.
4570 switch (cpi->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004571 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004572 case SUPERRES_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00004573 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004574 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004575 else
Urvang Joshide71d142017-10-05 12:12:15 -07004576 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004577 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004578 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004579 case SUPERRES_QTHRESH: {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07004580 // Do not use superres when screen content tools are used.
Urvang Joshib6409e92020-03-23 11:23:27 -07004581 if (cpi->common.features.allow_screen_content_tools) break;
Debargha Mukherjee2b2c5fd2018-11-14 13:21:24 -08004582 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4583 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004584
4585 // Now decide the use of superres based on 'q'.
Urvang Joshi2c92b072018-03-19 17:23:31 -07004586 int bottom_index, top_index;
4587 const int q = av1_rc_pick_q_and_bounds(
Angie Chiang958904c2019-09-19 16:03:35 -07004588 cpi, &cpi->rc, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
Jingning Han2e029872019-08-02 10:54:19 -07004589 &bottom_index, &top_index);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004590
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004591 const int qthresh = (frame_is_intra_only(&cpi->common))
4592 ? oxcf->superres_kf_qthresh
4593 : oxcf->superres_qthresh;
Urvang Joshib17e0a62019-01-11 16:11:54 -08004594 if (q <= qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07004595 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004596 } else {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004597 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004598 }
4599 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004600 }
Urvang Joshi36a83732019-01-31 15:31:57 -08004601 case SUPERRES_AUTO: {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004602 // Do not use superres when screen content tools are used.
Urvang Joshib6409e92020-03-23 11:23:27 -07004603 if (cpi->common.features.allow_screen_content_tools) break;
Urvang Joshi510d8f62019-01-10 12:11:50 -08004604 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4605 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi36a83732019-01-31 15:31:57 -08004606
4607 // Now decide the use of superres based on 'q'.
4608 int bottom_index, top_index;
4609 const int q = av1_rc_pick_q_and_bounds(
Angie Chiang958904c2019-09-19 16:03:35 -07004610 cpi, &cpi->rc, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
Jingning Han2e029872019-08-02 10:54:19 -07004611 &bottom_index, &top_index);
Urvang Joshi36a83732019-01-31 15:31:57 -08004612
4613 const int qthresh = 128;
4614 if (q <= qthresh) {
4615 new_denom = SCALE_NUMERATOR;
4616 } else {
Urvang Joshibc82d382019-11-01 17:59:20 -07004617#if SUPERRES_RECODE_ALL_RATIOS
4618 if (cpi->common.current_frame.frame_type == KEY_FRAME)
4619 new_denom = oxcf->superres_kf_scale_denominator;
4620 else
4621 new_denom = oxcf->superres_scale_denominator;
4622#else
Urvang Joshi510d8f62019-01-10 12:11:50 -08004623 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Urvang Joshibc82d382019-11-01 17:59:20 -07004624#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi36a83732019-01-31 15:31:57 -08004625 }
4626 break;
4627 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004628 default: assert(0);
4629 }
Urvang Joshide71d142017-10-05 12:12:15 -07004630 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004631}
4632
Urvang Joshide71d142017-10-05 12:12:15 -07004633static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4634 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4635}
4636
4637static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08004638 // Only need to check the width, as scaling is horizontal only.
4639 (void)oheight;
4640 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07004641}
4642
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004643static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004644 SUPERRES_MODE superres_mode, int owidth,
4645 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004646 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004647 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004648 }
4649
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004650 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07004651 int resize_denom =
4652 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4653 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4654
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004655 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004656 // Alter superres scale as needed to enforce conformity.
4657 rsz->superres_denom =
4658 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4659 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4660 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004661 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004662 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004663 // Alter resize scale as needed to enforce conformity.
4664 resize_denom =
4665 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004666 rsz->resize_width = owidth;
4667 rsz->resize_height = oheight;
4668 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004669 resize_denom);
4670 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4671 if (resize_denom > SCALE_NUMERATOR) {
4672 --resize_denom;
4673 rsz->resize_width = owidth;
4674 rsz->resize_height = oheight;
4675 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4676 resize_denom);
4677 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004678 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004679 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004680 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004681 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004682 if (resize_denom > rsz->superres_denom)
4683 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004684 else
Urvang Joshide71d142017-10-05 12:12:15 -07004685 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004686 rsz->resize_width = owidth;
4687 rsz->resize_height = oheight;
4688 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004689 resize_denom);
4690 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4691 (resize_denom > SCALE_NUMERATOR ||
4692 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08004693 } else { // We are allowed to alter neither resize scale nor superres
4694 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004695 return 0;
4696 }
Urvang Joshide71d142017-10-05 12:12:15 -07004697 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004698}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004699
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004700// Calculates resize and superres params for next frame
David Turner475a3132019-01-18 15:17:17 +00004701static size_params_type calculate_next_size_params(AV1_COMP *cpi) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004702 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08004703 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshi510d8f62019-01-10 12:11:50 -08004704 int resize_denom = SCALE_NUMERATOR;
Mufaddal Chakera3bcc72c2019-12-11 14:38:37 +05304705 if (has_no_stats_stage(cpi) && cpi->use_svc &&
Marco Paniconi63971322019-08-15 21:32:05 -07004706 cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1) {
4707 rsz.resize_width = cpi->common.width;
4708 rsz.resize_height = cpi->common.height;
4709 return rsz;
4710 }
Mufaddal Chakerae7326122019-12-04 14:49:09 +05304711 if (is_stat_generation_stage(cpi)) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004712 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4713 rsz.resize_width = cpi->resize_pending_width;
4714 rsz.resize_height = cpi->resize_pending_height;
4715 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4716 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004717 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004718 rsz.resize_width = cpi->oxcf.width;
4719 rsz.resize_height = cpi->oxcf.height;
4720 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004721 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004722 }
Urvang Joshide71d142017-10-05 12:12:15 -07004723 rsz.superres_denom = calculate_next_superres_scale(cpi);
Urvang Joshif9c2bd72020-04-07 15:11:51 -07004724 if (!validate_size_scales(oxcf->resize_mode, cpi->superres_mode, oxcf->width,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004725 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004726 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004727 return rsz;
4728}
4729
Urvang Joshi22b150b2019-01-10 14:32:32 -08004730static void setup_frame_size_from_params(AV1_COMP *cpi,
4731 const size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004732 int encode_width = rsz->resize_width;
4733 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004734
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004735 AV1_COMMON *cm = &cpi->common;
4736 cm->superres_upscaled_width = encode_width;
4737 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004738 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004739 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4740 rsz->superres_denom);
David Turner475a3132019-01-18 15:17:17 +00004741 av1_set_frame_size(cpi, encode_width, encode_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004742}
4743
David Turnerdedd8ff2019-01-23 13:59:46 +00004744void av1_setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004745 AV1_COMMON *cm = &cpi->common;
Urvang Joshi22b150b2019-01-10 14:32:32 -08004746 // Reset superres params from previous frame.
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004747 cm->superres_scale_denominator = SCALE_NUMERATOR;
David Turner475a3132019-01-18 15:17:17 +00004748 const size_params_type rsz = calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004749 setup_frame_size_from_params(cpi, &rsz);
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004750
Yaowu Xu7e450882019-04-30 15:09:18 -07004751 assert(av1_is_min_tile_width_satisfied(cm));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004752}
4753
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004754static void superres_post_encode(AV1_COMP *cpi) {
4755 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004756 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004757
Cheng Chen09c83a52018-06-05 12:27:36 -07004758 if (!av1_superres_scaled(cm)) return;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004759
Urvang Joshid6b5d512018-03-20 13:34:38 -07004760 assert(cpi->oxcf.enable_superres);
4761 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshib6409e92020-03-23 11:23:27 -07004762 assert(!cm->features.all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07004763
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004764 av1_superres_upscale(cm, NULL);
4765
4766 // If regular resizing is occurring the source will need to be downscaled to
4767 // match the upscaled superres resolution. Otherwise the original source is
4768 // used.
Cheng Chen09c83a52018-06-05 12:27:36 -07004769 if (!av1_resize_scaled(cm)) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004770 cpi->source = cpi->unscaled_source;
4771 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4772 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004773 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4774 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08004775 // Do downscale. cm->(width|height) has been updated by
4776 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004777 if (aom_realloc_frame_buffer(
4778 &cpi->scaled_source, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004779 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4780 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07004781 AOM_BORDER_IN_PIXELS, cm->features.byte_alignment, NULL, NULL,
4782 NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004783 aom_internal_error(
4784 &cm->error, AOM_CODEC_MEM_ERROR,
4785 "Failed to reallocate scaled source buffer for superres");
4786 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4787 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004788 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004789 (int)cm->seq_params.bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004790 cpi->source = &cpi->scaled_source;
4791 }
4792}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004793
Yue Chen92271e12019-07-09 16:28:02 -07004794static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
4795 MACROBLOCKD *xd, int use_restoration,
4796 int use_cdef) {
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) {
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304831 if (cpi->num_workers > 1)
David Turnerc29e1a92018-12-06 14:10:14 +00004832 av1_loop_restoration_filter_frame_mt(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304833 cpi->workers, cpi->num_workers,
4834 &cpi->lr_row_sync, &cpi->lr_ctxt);
4835 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) {
4850 const int num_planes = av1_num_planes(cm);
4851 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4852
4853 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
Urvang Joshib6409e92020-03-23 11:23:27 -07004854 cm->features.coded_lossless && cm->features.all_lossless));
Yue Chen92271e12019-07-09 16:28:02 -07004855
Urvang Joshib6409e92020-03-23 11:23:27 -07004856 const int use_loopfilter =
Urvang Joshi54ffae72020-03-23 13:37:10 -07004857 !cm->features.coded_lossless && !cm->tiles.large_scale;
Urvang Joshib6409e92020-03-23 11:23:27 -07004858 const int use_cdef = cm->seq_params.enable_cdef &&
Urvang Joshi54ffae72020-03-23 13:37:10 -07004859 !cm->features.coded_lossless && !cm->tiles.large_scale;
Yue Chen92271e12019-07-09 16:28:02 -07004860 const int use_restoration = cm->seq_params.enable_restoration &&
Urvang Joshib6409e92020-03-23 11:23:27 -07004861 !cm->features.all_lossless &&
Urvang Joshi54ffae72020-03-23 13:37:10 -07004862 !cm->tiles.large_scale;
Yue Chen92271e12019-07-09 16:28:02 -07004863
4864 struct loopfilter *lf = &cm->lf;
4865
4866#if CONFIG_COLLECT_COMPONENT_TIMING
4867 start_timing(cpi, loop_filter_time);
4868#endif
4869 if (use_loopfilter) {
4870 aom_clear_system_state();
chiyotsaid2f7b412019-12-18 10:16:42 -08004871 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_sf.lpf_pick);
Yue Chen92271e12019-07-09 16:28:02 -07004872 } else {
4873 lf->filter_level[0] = 0;
4874 lf->filter_level[1] = 0;
4875 }
4876
4877 if (lf->filter_level[0] || lf->filter_level[1]) {
4878 if (cpi->num_workers > 1)
4879 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
4880#if CONFIG_LPF_MASK
4881 0,
4882#endif
4883 cpi->workers, cpi->num_workers,
4884 &cpi->lf_row_sync);
4885 else
4886 av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
4887#if CONFIG_LPF_MASK
4888 0,
4889#endif
4890 0, num_planes, 0);
4891 }
4892#if CONFIG_COLLECT_COMPONENT_TIMING
4893 end_timing(cpi, loop_filter_time);
4894#endif
4895
4896 cdef_restoration_frame(cpi, cm, xd, use_restoration, use_cdef);
4897}
4898
David Turnerf2b334c2018-12-13 13:00:55 +00004899static void fix_interp_filter(InterpFilter *const interp_filter,
4900 const FRAME_COUNTS *const counts) {
4901 if (*interp_filter == SWITCHABLE) {
4902 // Check to see if only one of the filters is actually used
4903 int count[SWITCHABLE_FILTERS] = { 0 };
4904 int num_filters_used = 0;
4905 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4906 for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
4907 count[i] += counts->switchable_interp[j][i];
4908 num_filters_used += (count[i] > 0);
4909 }
4910 if (num_filters_used == 1) {
4911 // Only one filter is used. So set the filter at frame level
4912 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4913 if (count[i]) {
4914 if (i == EIGHTTAP_REGULAR) *interp_filter = i;
4915 break;
4916 }
4917 }
4918 }
4919 }
4920}
4921
David Turner996b2c12018-12-07 15:52:30 +00004922static void finalize_encoded_frame(AV1_COMP *const cpi) {
4923 AV1_COMMON *const cm = &cpi->common;
David Turner99e990e2018-12-10 12:54:26 +00004924 CurrentFrame *const current_frame = &cm->current_frame;
David Turner996b2c12018-12-07 15:52:30 +00004925
David Turner99e990e2018-12-10 12:54:26 +00004926 if (!cm->seq_params.reduced_still_picture_hdr &&
4927 encode_show_existing_frame(cm)) {
4928 RefCntBuffer *const frame_to_show =
4929 cm->ref_frame_map[cpi->existing_fb_idx_to_show];
4930
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004931 if (frame_to_show == NULL) {
David Turner99e990e2018-12-10 12:54:26 +00004932 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4933 "Buffer does not contain a reconstructed frame");
4934 }
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004935 assert(frame_to_show->ref_count > 0);
David Turner99e990e2018-12-10 12:54:26 +00004936 assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
David Turner99e990e2018-12-10 12:54:26 +00004937 }
David Turner08f909c2018-12-18 13:29:14 +00004938
4939 if (!encode_show_existing_frame(cm) &&
4940 cm->seq_params.film_grain_params_present &&
4941 (cm->show_frame || cm->showable_frame)) {
4942 // Copy the current frame's film grain params to the its corresponding
4943 // RefCntBuffer slot.
4944 cm->cur_frame->film_grain_params = cm->film_grain_params;
4945
4946 // We must update the parameters if this is not an INTER_FRAME
4947 if (current_frame->frame_type != INTER_FRAME)
4948 cm->cur_frame->film_grain_params.update_parameters = 1;
4949
4950 // Iterate the random seed for the next frame.
4951 cm->film_grain_params.random_seed += 3381;
4952 if (cm->film_grain_params.random_seed == 0)
4953 cm->film_grain_params.random_seed = 7391;
4954 }
David Turnerf2b334c2018-12-13 13:00:55 +00004955
4956 // Initialise all tiles' contexts from the global frame context
Urvang Joshi54ffae72020-03-23 13:37:10 -07004957 for (int tile_col = 0; tile_col < cm->tiles.cols; tile_col++) {
4958 for (int tile_row = 0; tile_row < cm->tiles.rows; tile_row++) {
4959 const int tile_idx = tile_row * cm->tiles.cols + tile_col;
David Turnerf2b334c2018-12-13 13:00:55 +00004960 cpi->tile_data[tile_idx].tctx = *cm->fc;
4961 }
4962 }
4963
Urvang Joshi6237b882020-03-26 15:02:26 -07004964 fix_interp_filter(&cm->features.interp_filter, cpi->td.counts);
David Turner996b2c12018-12-07 15:52:30 +00004965}
4966
Urvang Joshi57643372019-02-21 11:10:57 -08004967static int get_regulated_q_overshoot(AV1_COMP *const cpi, int q_low, int q_high,
4968 int top_index, int bottom_index) {
4969 const AV1_COMMON *const cm = &cpi->common;
4970 const RATE_CONTROL *const rc = &cpi->rc;
4971
4972 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4973
4974 int q_regulated =
4975 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4976 AOMMAX(q_high, top_index), cm->width, cm->height);
4977
4978 int retries = 0;
4979 while (q_regulated < q_low && retries < 10) {
4980 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4981 q_regulated =
4982 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4983 AOMMAX(q_high, top_index), cm->width, cm->height);
4984 retries++;
4985 }
4986 return q_regulated;
4987}
4988
4989static int get_regulated_q_undershoot(AV1_COMP *const cpi, int q_high,
4990 int top_index, int bottom_index) {
4991 const AV1_COMMON *const cm = &cpi->common;
4992 const RATE_CONTROL *const rc = &cpi->rc;
4993
4994 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4995 int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4996 top_index, cm->width, cm->height);
4997
4998 int retries = 0;
4999 while (q_regulated > q_high && retries < 10) {
5000 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
5001 q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
5002 top_index, cm->width, cm->height);
5003 retries++;
5004 }
5005 return q_regulated;
5006}
5007
David Turner2f3b5df2019-01-02 14:30:50 +00005008// Called after encode_with_recode_loop() has just encoded a frame and packed
5009// its bitstream. This function works out whether we under- or over-shot
5010// our bitrate target and adjusts q as appropriate. Also decides whether
5011// or not we should do another recode loop, indicated by *loop
Hui Suef139e12019-05-20 15:51:22 -07005012static void recode_loop_update_q(
5013 AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low,
5014 int *const q_high, const int top_index, const int bottom_index,
5015 int *const undershoot_seen, int *const overshoot_seen,
5016 int *const low_cr_seen, const int loop_at_this_size) {
David Turner2f3b5df2019-01-02 14:30:50 +00005017 AV1_COMMON *const cm = &cpi->common;
5018 RATE_CONTROL *const rc = &cpi->rc;
Debargha Mukherjee90b20752020-03-19 23:16:34 -07005019 *loop = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00005020
Hui Suef139e12019-05-20 15:51:22 -07005021 const int min_cr = cpi->oxcf.min_cr;
5022 if (min_cr > 0) {
5023 aom_clear_system_state();
5024 const double compression_ratio =
5025 av1_get_compression_ratio(cm, rc->projected_frame_size >> 3);
5026 const double target_cr = min_cr / 100.0;
5027 if (compression_ratio < target_cr) {
5028 *low_cr_seen = 1;
5029 if (*q < rc->worst_quality) {
5030 const double cr_ratio = target_cr / compression_ratio;
5031 const int projected_q = AOMMAX(*q + 1, (int)(*q * cr_ratio * cr_ratio));
5032 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->worst_quality);
5033 *q_low = AOMMAX(*q, *q_low);
5034 *q_high = AOMMAX(*q, *q_high);
5035 *loop = 1;
5036 }
5037 }
5038 if (*low_cr_seen) return;
5039 }
5040
5041 if (cpi->oxcf.rc_mode == AOM_Q) return;
5042
Hui Su01dfe032019-09-11 14:10:42 -07005043 const int last_q = *q;
David Turner2f3b5df2019-01-02 14:30:50 +00005044 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
5045 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
5046 &frame_under_shoot_limit,
5047 &frame_over_shoot_limit);
5048 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
5049
Hui Su01dfe032019-09-11 14:10:42 -07005050 if (cm->current_frame.frame_type == KEY_FRAME && rc->this_key_frame_forced &&
5051 rc->projected_frame_size < rc->max_frame_bandwidth) {
David Turner2f3b5df2019-01-02 14:30:50 +00005052 int64_t kf_err;
Hui Su01dfe032019-09-11 14:10:42 -07005053 const int64_t high_err_target = cpi->ambient_err;
5054 const int64_t low_err_target = cpi->ambient_err >> 1;
David Turner2f3b5df2019-01-02 14:30:50 +00005055
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005056#if CONFIG_AV1_HIGHBITDEPTH
David Turner2f3b5df2019-01-02 14:30:50 +00005057 if (cm->seq_params.use_highbitdepth) {
5058 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
5059 } else {
5060 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5061 }
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005062#else
5063 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5064#endif
David Turner2f3b5df2019-01-02 14:30:50 +00005065 // Prevent possible divide by zero error below for perfect KF
5066 kf_err += !kf_err;
5067
5068 // The key frame is not good enough or we can afford
5069 // to make it better without undue risk of popping.
5070 if ((kf_err > high_err_target &&
5071 rc->projected_frame_size <= frame_over_shoot_limit) ||
5072 (kf_err > low_err_target &&
5073 rc->projected_frame_size <= frame_under_shoot_limit)) {
5074 // Lower q_high
Hui Su01dfe032019-09-11 14:10:42 -07005075 *q_high = AOMMAX(*q - 1, *q_low);
David Turner2f3b5df2019-01-02 14:30:50 +00005076
5077 // Adjust Q
5078 *q = (int)((*q * high_err_target) / kf_err);
5079 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
5080 } else if (kf_err < low_err_target &&
5081 rc->projected_frame_size >= frame_under_shoot_limit) {
5082 // The key frame is much better than the previous frame
5083 // Raise q_low
Hui Su01dfe032019-09-11 14:10:42 -07005084 *q_low = AOMMIN(*q + 1, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00005085
5086 // Adjust Q
5087 *q = (int)((*q * low_err_target) / kf_err);
5088 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
5089 }
5090
5091 // Clamp Q to upper and lower limits:
5092 *q = clamp(*q, *q_low, *q_high);
Hui Su01dfe032019-09-11 14:10:42 -07005093 *loop = (*q != last_q);
5094 return;
5095 }
David Turner2f3b5df2019-01-02 14:30:50 +00005096
Hui Su01dfe032019-09-11 14:10:42 -07005097 if (recode_loop_test(cpi, frame_over_shoot_limit, frame_under_shoot_limit, *q,
5098 AOMMAX(*q_high, top_index), bottom_index)) {
David Turner2f3b5df2019-01-02 14:30:50 +00005099 // Is the projected frame size out of range and are we allowed
5100 // to attempt to recode.
David Turner2f3b5df2019-01-02 14:30:50 +00005101
5102 // Frame size out of permitted range:
5103 // Update correction factor & compute new Q to try...
5104 // Frame is too large
5105 if (rc->projected_frame_size > rc->this_frame_target) {
5106 // Special case if the projected size is > the max allowed.
Urvang Joshi7344d3e2019-07-25 17:24:57 -07005107 if (*q == *q_high &&
5108 rc->projected_frame_size >= rc->max_frame_bandwidth) {
5109 const double q_val_high_current =
5110 av1_convert_qindex_to_q(*q_high, cm->seq_params.bit_depth);
5111 const double q_val_high_new =
5112 q_val_high_current *
5113 ((double)rc->projected_frame_size / rc->max_frame_bandwidth);
5114 *q_high = av1_find_qindex(q_val_high_new, cm->seq_params.bit_depth,
5115 rc->best_quality, rc->worst_quality);
5116 }
David Turner2f3b5df2019-01-02 14:30:50 +00005117
5118 // Raise Qlow as to at least the current value
Hui Su01dfe032019-09-11 14:10:42 -07005119 *q_low = AOMMIN(*q + 1, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00005120
Urvang Joshi57643372019-02-21 11:10:57 -08005121 if (*undershoot_seen || loop_at_this_size > 2 ||
5122 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00005123 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
5124
5125 *q = (*q_high + *q_low + 1) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08005126 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
5127 const int q_mid = (*q_high + *q_low + 1) / 2;
5128 const int q_regulated = get_regulated_q_overshoot(
5129 cpi, *q_low, *q_high, top_index, bottom_index);
5130 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
5131 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
5132 *q = (q_mid + q_regulated + 1) / 2;
David Turner2f3b5df2019-01-02 14:30:50 +00005133 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08005134 *q = get_regulated_q_overshoot(cpi, *q_low, *q_high, top_index,
5135 bottom_index);
David Turner2f3b5df2019-01-02 14:30:50 +00005136 }
5137
5138 *overshoot_seen = 1;
5139 } else {
5140 // Frame is too small
Hui Su01dfe032019-09-11 14:10:42 -07005141 *q_high = AOMMAX(*q - 1, *q_low);
David Turner2f3b5df2019-01-02 14:30:50 +00005142
Urvang Joshi57643372019-02-21 11:10:57 -08005143 if (*overshoot_seen || loop_at_this_size > 2 ||
5144 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00005145 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
5146 *q = (*q_high + *q_low) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08005147 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
5148 const int q_mid = (*q_high + *q_low) / 2;
5149 const int q_regulated =
5150 get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
5151 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
5152 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
5153 *q = (q_mid + q_regulated) / 2;
5154
5155 // Special case reset for qlow for constrained quality.
5156 // This should only trigger where there is very substantial
5157 // undershoot on a frame and the auto cq level is above
5158 // the user passsed in value.
5159 if (cpi->oxcf.rc_mode == AOM_CQ && q_regulated < *q_low) {
5160 *q_low = *q;
5161 }
David Turner2f3b5df2019-01-02 14:30:50 +00005162 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08005163 *q = get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
5164
David Turner2f3b5df2019-01-02 14:30:50 +00005165 // Special case reset for qlow for constrained quality.
5166 // This should only trigger where there is very substantial
5167 // undershoot on a frame and the auto cq level is above
5168 // the user passsed in value.
5169 if (cpi->oxcf.rc_mode == AOM_CQ && *q < *q_low) {
5170 *q_low = *q;
5171 }
David Turner2f3b5df2019-01-02 14:30:50 +00005172 }
5173
5174 *undershoot_seen = 1;
5175 }
5176
5177 // Clamp Q to upper and lower limits:
5178 *q = clamp(*q, *q_low, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00005179 }
Hui Su01dfe032019-09-11 14:10:42 -07005180
5181 *loop = (*q != last_q);
David Turner2f3b5df2019-01-02 14:30:50 +00005182}
5183
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305184static int get_interp_filter_selected(const AV1_COMMON *const cm,
5185 MV_REFERENCE_FRAME ref,
5186 InterpFilter ifilter) {
5187 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
5188 if (buf == NULL) return 0;
5189 return buf->interp_filter_selected[ifilter];
5190}
5191
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305192static uint16_t setup_interp_filter_search_mask(AV1_COMP *cpi) {
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305193 const AV1_COMMON *const cm = &cpi->common;
5194 int ref_total[REF_FRAMES] = { 0 };
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305195 uint16_t mask = ALLOW_ALL_INTERP_FILT_MASK;
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305196
Urvang Joshi1de67aa2020-03-20 11:21:57 -07005197 if (cpi->last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305198 return mask;
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305199
5200 for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
5201 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
5202 ++ifilter) {
5203 ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter);
5204 }
5205 }
5206 int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] +
5207 ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] +
5208 ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]);
5209
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305210 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
5211 ++ifilter) {
5212 int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30;
5213 if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) {
5214 int filter_score =
5215 get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 +
5216 get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 +
5217 get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 +
5218 get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 +
5219 get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 +
5220 get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10;
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305221 if (filter_score < ref_total_total) {
5222 DUAL_FILTER_TYPE filt_type = ifilter + SWITCHABLE_FILTERS * ifilter;
5223 reset_interp_filter_allowed_mask(&mask, filt_type);
5224 }
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305225 }
5226 }
5227 return mask;
5228}
5229
Cheng Chen92a926a2020-01-30 17:57:33 -08005230#if !CONFIG_REALTIME_ONLY
chiyotsai155e2862020-03-09 15:29:34 -07005231#define STRICT_PSNR_DIFF_THRESH 0.9
Cheng Chen92a926a2020-01-30 17:57:33 -08005232// Encode key frame with/without screen content tools to determine whether
5233// screen content tools should be enabled for this key frame group or not.
5234// The first encoding is without screen content tools.
5235// The second encoding is with screen content tools.
5236// We compare the psnr and frame size to make the decision.
5237static void screen_content_tools_determination(
5238 AV1_COMP *cpi, const int allow_screen_content_tools_orig_decision,
5239 const int allow_intrabc_orig_decision,
5240 const int is_screen_content_type_orig_decision, const int pass,
5241 int *projected_size_pass, PSNR_STATS *psnr) {
5242 AV1_COMMON *const cm = &cpi->common;
Urvang Joshib6409e92020-03-23 11:23:27 -07005243 FeatureFlags *const features = &cm->features;
Cheng Chen92a926a2020-01-30 17:57:33 -08005244 projected_size_pass[pass] = cpi->rc.projected_frame_size;
5245#if CONFIG_AV1_HIGHBITDEPTH
5246 const uint32_t in_bit_depth = cpi->oxcf.input_bit_depth;
5247 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
5248 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr[pass],
5249 bit_depth, in_bit_depth);
5250#else
5251 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr[pass]);
5252#endif
5253 if (pass != 1) return;
5254
5255 const double psnr_diff = psnr[1].psnr[0] - psnr[0].psnr[0];
5256 const int is_sc_encoding_much_better = psnr_diff > STRICT_PSNR_DIFF_THRESH;
5257 if (is_sc_encoding_much_better) {
5258 // Use screen content tools, if we get coding gain.
Urvang Joshib6409e92020-03-23 11:23:27 -07005259 features->allow_screen_content_tools = 1;
5260 features->allow_intrabc = cpi->intrabc_used;
Cheng Chen92a926a2020-01-30 17:57:33 -08005261 cpi->is_screen_content_type = 1;
5262 } else {
5263 // Use original screen content decision.
Urvang Joshib6409e92020-03-23 11:23:27 -07005264 features->allow_screen_content_tools =
5265 allow_screen_content_tools_orig_decision;
5266 features->allow_intrabc = allow_intrabc_orig_decision;
Cheng Chen92a926a2020-01-30 17:57:33 -08005267 cpi->is_screen_content_type = is_screen_content_type_orig_decision;
5268 }
5269}
5270
Cheng Chenb8c938b2020-03-23 11:30:18 -07005271// Set some encoding parameters to make the encoding process fast.
5272// A fixed block partition size, and a large q is used.
5273static void set_encoding_params_for_screen_content(AV1_COMP *cpi,
5274 const int pass) {
Cheng Chen92a926a2020-01-30 17:57:33 -08005275 AV1_COMMON *const cm = &cpi->common;
5276 if (pass == 0) {
5277 // In the first pass, encode without screen content tools.
5278 // Use a high q, and a fixed block size for fast encoding.
Urvang Joshib6409e92020-03-23 11:23:27 -07005279 cm->features.allow_screen_content_tools = 0;
5280 cm->features.allow_intrabc = 0;
Cheng Chen92a926a2020-01-30 17:57:33 -08005281 cpi->is_screen_content_type = 0;
5282 cpi->sf.part_sf.partition_search_type = FIXED_PARTITION;
5283 cpi->sf.part_sf.always_this_block_size = BLOCK_32X32;
Cheng Chenb8c938b2020-03-23 11:30:18 -07005284 return;
Cheng Chen92a926a2020-01-30 17:57:33 -08005285 }
Cheng Chenb8c938b2020-03-23 11:30:18 -07005286 assert(pass == 1);
5287 // In the second pass, encode with screen content tools.
5288 // Use a high q, and a fixed block size for fast encoding.
Urvang Joshib6409e92020-03-23 11:23:27 -07005289 cm->features.allow_screen_content_tools = 1;
Cheng Chenb8c938b2020-03-23 11:30:18 -07005290 // TODO(chengchen): turn intrabc on could lead to data race issue.
5291 // cm->allow_intrabc = 1;
5292 cpi->is_screen_content_type = 1;
5293 cpi->sf.part_sf.partition_search_type = FIXED_PARTITION;
5294 cpi->sf.part_sf.always_this_block_size = BLOCK_32X32;
Cheng Chen92a926a2020-01-30 17:57:33 -08005295}
5296
5297// Determines whether to use screen content tools for the key frame group.
Urvang Joshib6409e92020-03-23 11:23:27 -07005298// This function modifies "cm->features.allow_screen_content_tools",
5299// "cm->features.allow_intrabc" and "cpi->is_screen_content_type".
Cheng Chen92a926a2020-01-30 17:57:33 -08005300static void determine_sc_tools_with_encoding(AV1_COMP *cpi, const int q_orig) {
5301 if (!is_stat_consumption_stage_twopass(cpi)) return;
5302
5303 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 Joshif9c2bd72020-04-07 15:11:51 -07005314 cpi->superres_mode != 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(
Urvang Joshi1de67aa2020-03-20 11:21:57 -07005363 cpi, av1_get_vmaf_base_qindex(cpi, q_for_screen_content_quick_run));
Cheng Chen92a926a2020-01-30 17:57:33 -08005364 } else {
5365#endif
Urvang Joshi1de67aa2020-03-20 11:21:57 -07005366 av1_set_quantizer(cpi, q_for_screen_content_quick_run);
Cheng Chen92a926a2020-01-30 17:57:33 -08005367#if CONFIG_TUNE_VMAF
5368 }
5369#endif
Debargha Mukherjee90b20752020-03-19 23:16:34 -07005370 av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
Cheng Chen92a926a2020-01-30 17:57:33 -08005371 if (cpi->oxcf.deltaq_mode != NO_DELTA_Q) av1_init_quantizer(cpi);
5372
5373 av1_set_variance_partition_thresholds(cpi, q_for_screen_content_quick_run,
5374 0);
5375 // transform / motion compensation build reconstruction frame
5376 av1_encode_frame(cpi);
5377 // Screen content decision
5378 screen_content_tools_determination(
5379 cpi, allow_screen_content_tools_orig_decision,
5380 allow_intrabc_orig_decision, is_screen_content_type_orig_decision, pass,
5381 projected_size_pass, psnr);
5382 }
5383
5384 // Set partition speed feature back.
5385 cpi->sf.part_sf.partition_search_type = partition_search_type_orig;
5386 cpi->sf.part_sf.always_this_block_size = fixed_partition_block_size_orig;
5387}
5388#endif // CONFIG_REALTIME_ONLY
5389
Tom Finegane4099e32018-01-23 12:01:51 -08005390static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005391 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005392 RATE_CONTROL *const rc = &cpi->rc;
Vishesh8ba8bfe2020-04-03 14:20:29 +05305393 GlobalMotionInfo *const gm_info = &cpi->gm_info;
chiyotsai6b430132019-12-18 10:33:51 -08005394 const int allow_recode = (cpi->sf.hl_sf.recode_loop != DISALLOW_RECODE);
Hui Sua1d71842019-07-31 12:02:24 -07005395 // Must allow recode if minimum compression ratio is set.
5396 assert(IMPLIES(cpi->oxcf.min_cr > 0, allow_recode));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005397
5398 set_size_independent_vars(cpi);
Hamsalekha S37cc1d12019-12-12 19:27:41 +05305399 if (is_stat_consumption_stage_twopass(cpi) &&
chiyotsaicbf1d112019-12-16 11:28:47 -08005400 cpi->sf.interp_sf.adaptive_interp_filter_search)
Yunqing Wangc3ab9272019-11-01 17:43:58 -07005401 cpi->interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07005402 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07005403
David Turnerdedd8ff2019-01-23 13:59:46 +00005404 av1_setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005405
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005406#if CONFIG_SUPERRES_IN_RECODE
Urvang Joshif9c2bd72020-04-07 15:11:51 -07005407 if (superres_in_recode_allowed(cpi) && cpi->superres_mode != SUPERRES_NONE &&
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005408 cm->superres_scale_denominator == SCALE_NUMERATOR) {
Urvang Joshif9c2bd72020-04-07 15:11:51 -07005409 // Superres mode in currently enabled, but the denominator selected will
5410 // disable superres. So no need to continue, as we will go through another
5411 // recode loop for full-resolution after this anyway.
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005412 return -1;
5413 }
5414#endif // CONFIG_SUPERRES_IN_RECODE
5415
David Turner2f3b5df2019-01-02 14:30:50 +00005416 int top_index = 0, bottom_index = 0;
5417 int q = 0, q_low = 0, q_high = 0;
5418 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
5419 q_low = bottom_index;
5420 q_high = top_index;
Fyodor Kyslov166648a2020-03-18 16:15:27 -07005421 if (cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION) {
5422 const int num_64x64_blocks =
5423 (cm->seq_params.sb_size == BLOCK_64X64) ? 1 : 4;
5424 if (cpi->td.vt64x64) {
5425 if (num_64x64_blocks != cpi->td.num_64x64_blocks) {
5426 aom_free(cpi->td.vt64x64);
5427 cpi->td.vt64x64 = NULL;
5428 }
5429 }
5430 if (!cpi->td.vt64x64) {
5431 CHECK_MEM_ERROR(cm, cpi->td.vt64x64,
5432 aom_malloc(sizeof(*cpi->td.vt64x64) * num_64x64_blocks));
5433 cpi->td.num_64x64_blocks = num_64x64_blocks;
5434 }
5435 }
David Turner2f3b5df2019-01-02 14:30:50 +00005436
Vishesh20771502020-04-06 15:37:15 +05305437 if (cpi->sf.tx_sf.tx_type_search.prune_tx_type_using_stats) {
Yunqing Wang2348c032020-01-09 18:05:49 -08005438 const int thr[2][2] = { { 15, 10 }, { 17, 10 } };
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005439 for (int f = 0; f < FRAME_UPDATE_TYPES; f++) {
Yunqing Wangeb7c2182019-08-22 18:15:48 -07005440 int kf_arf_update = (f == KF_UPDATE || f == ARF_UPDATE);
5441 cpi->tx_type_probs_thresh[f] =
chiyotsaidcc2e012019-12-17 10:29:32 -08005442 thr[cpi->sf.tx_sf.tx_type_search.prune_tx_type_using_stats - 1]
Yunqing Wangeb7c2182019-08-22 18:15:48 -07005443 [kf_arf_update];
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005444 }
Vishesh20771502020-04-06 15:37:15 +05305445 if (cm->current_frame.frame_type == KEY_FRAME) {
5446 av1_copy(cpi->tx_type_probs, default_tx_type_probs);
5447 }
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005448 }
5449
chiyotsai868dc902019-12-12 15:32:52 -08005450 if (!cpi->sf.inter_sf.disable_obmc &&
5451 cpi->sf.inter_sf.prune_obmc_prob_thresh > 0 &&
Yunqing Wang5f74dc22019-10-29 10:35:20 -07005452 cm->current_frame.frame_type == KEY_FRAME) {
5453 av1_copy(cpi->obmc_probs, default_obmc_probs);
Yunqing Wang5f74dc22019-10-29 10:35:20 -07005454 }
chiyotsai868dc902019-12-12 15:32:52 -08005455 if (cpi->sf.inter_sf.prune_warped_prob_thresh > 0 &&
Yunqing Wanga09a46b2019-12-09 13:16:24 -08005456 cm->current_frame.frame_type == KEY_FRAME) {
5457 av1_copy(cpi->warped_probs, default_warped_probs);
5458 }
Yunqing Wang5f74dc22019-10-29 10:35:20 -07005459
Yunqing Wang2348c032020-01-09 18:05:49 -08005460 if (cpi->sf.interp_sf.adaptive_interp_filter_search == 2 &&
5461 cm->current_frame.frame_type == KEY_FRAME) {
5462 av1_copy(cpi->switchable_interp_probs, default_switchable_interp_probs);
5463
5464 const int thr[7] = { 0, 8, 8, 8, 8, 0, 8 };
5465 for (int f = 0; f < FRAME_UPDATE_TYPES; f++) {
5466 cpi->switchable_interp_thresh[f] = thr[f];
5467 }
5468 }
5469
Cheng Chen92a926a2020-01-30 17:57:33 -08005470#if !CONFIG_REALTIME_ONLY
5471 // Determine whether to use screen content tools using two fast encoding.
5472 determine_sc_tools_with_encoding(cpi, q);
5473#endif // CONFIG_REALTIME_ONLY
5474
5475#if CONFIG_COLLECT_COMPONENT_TIMING
5476 printf("\n Encoding a frame:");
5477#endif
5478
David Turner2f3b5df2019-01-02 14:30:50 +00005479 // Loop variables
Cheng Chen92a926a2020-01-30 17:57:33 -08005480 int loop = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00005481 int loop_count = 0;
5482 int loop_at_this_size = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00005483 int overshoot_seen = 0;
5484 int undershoot_seen = 0;
Hui Suef139e12019-05-20 15:51:22 -07005485 int low_cr_seen = 0;
chiyotsai8b7cef82020-01-21 16:52:54 -08005486 int last_loop_allow_hp = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005487
Yaowu Xuc27fc142016-08-22 16:08:15 -07005488 do {
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005489 loop = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005490 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005491
Urvang Joshif1fa6862018-01-08 16:39:33 -08005492 // if frame was scaled calculate global_motion_search again if already
5493 // done
Vishesh8ba8bfe2020-04-03 14:20:29 +05305494 if (loop_count > 0 && cpi->source && gm_info->search_done) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005495 if (cpi->source->y_crop_width != cm->width ||
David Turner2f3b5df2019-01-02 14:30:50 +00005496 cpi->source->y_crop_height != cm->height) {
Vishesh8ba8bfe2020-04-03 14:20:29 +05305497 gm_info->search_done = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00005498 }
5499 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005500 cpi->source =
5501 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
David Turner2f3b5df2019-01-02 14:30:50 +00005502 if (cpi->unscaled_last_source != NULL) {
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07005503 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
5504 &cpi->scaled_last_source);
David Turner2f3b5df2019-01-02 14:30:50 +00005505 }
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07005506
David Turner2f3b5df2019-01-02 14:30:50 +00005507 if (!frame_is_intra_only(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005508 if (loop_count > 0) {
5509 release_scaled_references(cpi);
5510 }
Cheng Chen46f30c72017-09-07 11:13:33 -07005511 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005512 }
sdeng3cd9eec2020-01-23 15:49:50 -08005513#if CONFIG_TUNE_VMAF
5514 if (cpi->oxcf.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING ||
sdeng615dc242020-02-04 16:06:14 -08005515 cpi->oxcf.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
5516 cpi->oxcf.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
Urvang Joshi1de67aa2020-03-20 11:21:57 -07005517 av1_set_quantizer(cpi, av1_get_vmaf_base_qindex(cpi, q));
sdeng3cd9eec2020-01-23 15:49:50 -08005518 } else {
5519#endif
Urvang Joshi1de67aa2020-03-20 11:21:57 -07005520 av1_set_quantizer(cpi, q);
sdeng3cd9eec2020-01-23 15:49:50 -08005521#if CONFIG_TUNE_VMAF
5522 }
5523#endif
Debargha Mukherjee90b20752020-03-19 23:16:34 -07005524 av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
sdeng3cd9eec2020-01-23 15:49:50 -08005525
Fyodor Kyslov25daf5d2019-10-23 10:33:26 -07005526 if (cpi->oxcf.deltaq_mode != NO_DELTA_Q) av1_init_quantizer(cpi);
kyslov7b9d0d62018-12-21 11:12:26 -08005527
5528 av1_set_variance_partition_thresholds(cpi, q, 0);
5529
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08005530 // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
5531 // cm->current_frame.frame_number, cm->show_frame, q,
5532 // cm->current_frame.frame_type, cm->superres_scale_denominator);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005533
David Turner2f3b5df2019-01-02 14:30:50 +00005534 if (loop_count == 0) {
5535 setup_frame(cpi);
5536 } else if (get_primary_ref_frame_buf(cm) == NULL) {
5537 // Base q-index may have changed, so we need to assign proper default coef
5538 // probs before every iteration.
Yaowu Xuf883b422016-08-30 14:01:10 -07005539 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08005540 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00005541 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005542
Yaowu Xuc27fc142016-08-22 16:08:15 -07005543 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005544 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005545 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005546 av1_setup_in_frame_q_adj(cpi);
David Turner2f3b5df2019-01-02 14:30:50 +00005547 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !allow_recode) {
5548 suppress_active_map(cpi);
5549 av1_cyclic_refresh_setup(cpi);
5550 apply_active_map(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005551 }
David Turner2f3b5df2019-01-02 14:30:50 +00005552
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005553 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00005554 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005555 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
Jerome Jiangb23571d2019-10-23 14:15:55 -07005556 cm->seg.enabled = cm->prev_frame->seg.enabled;
David Barker11c93562018-06-05 12:00:07 +01005557 } else {
Yaowu Xu7e450882019-04-30 15:09:18 -07005558 av1_calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07005559 }
David Barkercab37552018-03-21 11:56:24 +00005560 } else {
5561 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005562 }
David Barkercab37552018-03-21 11:56:24 +00005563 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Jerome Jiangb23571d2019-10-23 14:15:55 -07005564 cm->cur_frame->seg.enabled = cm->seg.enabled;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005565
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005566#if CONFIG_COLLECT_COMPONENT_TIMING
5567 start_timing(cpi, av1_encode_frame_time);
5568#endif
chiyotsai8b7cef82020-01-21 16:52:54 -08005569 // Set the motion vector precision based on mv stats from the last coded
5570 // frame.
5571 if (!frame_is_intra_only(cm)) {
5572 av1_pick_and_set_high_precision_mv(cpi, q);
5573
5574 // If the precision has changed during different iteration of the loop,
5575 // then we need to reset the global motion vectors
Urvang Joshib6409e92020-03-23 11:23:27 -07005576 if (loop_count > 0 &&
5577 cm->features.allow_high_precision_mv != last_loop_allow_hp) {
Vishesh8ba8bfe2020-04-03 14:20:29 +05305578 gm_info->search_done = 0;
chiyotsai8b7cef82020-01-21 16:52:54 -08005579 }
Urvang Joshib6409e92020-03-23 11:23:27 -07005580 last_loop_allow_hp = cm->features.allow_high_precision_mv;
chiyotsai8b7cef82020-01-21 16:52:54 -08005581 }
chiyotsaic666b1f2019-12-20 10:44:58 -08005582
Yaowu Xuc27fc142016-08-22 16:08:15 -07005583 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07005584 av1_encode_frame(cpi);
chiyotsai51bd6482019-12-20 10:49:34 -08005585#if !CONFIG_REALTIME_ONLY
5586 // Reset the mv_stats in case we are interrupted by an intraframe or an
5587 // overlay frame.
5588 if (cpi->mv_stats.valid) {
5589 av1_zero(cpi->mv_stats);
5590 }
5591 // Gather the mv_stats for the next frame
5592 if (cpi->sf.hl_sf.high_precision_mv_usage == LAST_MV_DATA &&
5593 av1_frame_allows_smart_mv(cpi)) {
5594 av1_collect_mv_stats(cpi, q);
5595 }
5596#endif // !CONFIG_REALTIME_ONLY
5597
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005598#if CONFIG_COLLECT_COMPONENT_TIMING
5599 end_timing(cpi, av1_encode_frame_time);
5600#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005601
Yaowu Xuf883b422016-08-30 14:01:10 -07005602 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005603
5604 // Dummy pack of the bitstream using up to date stats to get an
5605 // accurate estimate of output frame size to determine if we need
5606 // to recode.
chiyotsai6b430132019-12-18 10:33:51 -08005607 const int do_dummy_pack =
5608 (cpi->sf.hl_sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
5609 cpi->oxcf.rc_mode != AOM_Q) ||
5610 cpi->oxcf.min_cr > 0;
Hui Sua1d71842019-07-31 12:02:24 -07005611 if (do_dummy_pack) {
David Turner996b2c12018-12-07 15:52:30 +00005612 finalize_encoded_frame(cpi);
David Turner35cba132018-12-10 15:48:15 +00005613 int largest_tile_id = 0; // Output from bitstream: unused here
chiyotsaic666b1f2019-12-20 10:44:58 -08005614 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) !=
5615 AOM_CODEC_OK) {
Tom Finegane4099e32018-01-23 12:01:51 -08005616 return AOM_CODEC_ERROR;
chiyotsaic666b1f2019-12-20 10:44:58 -08005617 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005618
5619 rc->projected_frame_size = (int)(*size) << 3;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005620 }
5621
Hui Suef139e12019-05-20 15:51:22 -07005622 if (allow_recode) {
David Turner2f3b5df2019-01-02 14:30:50 +00005623 // Update q and decide whether to do a recode loop
5624 recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
5625 bottom_index, &undershoot_seen, &overshoot_seen,
Hui Suef139e12019-05-20 15:51:22 -07005626 &low_cr_seen, loop_at_this_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005627 }
5628
5629 // Special case for overlay frame.
Hui Su7e3eeaa2019-09-11 15:50:41 -07005630 if (loop && rc->is_src_frame_alt_ref &&
5631 rc->projected_frame_size < rc->max_frame_bandwidth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005632 loop = 0;
Hui Su7e3eeaa2019-09-11 15:50:41 -07005633 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005634
chiyotsai76cd2652019-12-18 10:26:36 -08005635 if (allow_recode && !cpi->sf.gm_sf.gm_disable_recode &&
Vishesh8ba8bfe2020-04-03 14:20:29 +05305636 recode_loop_test_global_motion(cm->global_motion,
5637 cpi->td.rd_counts.global_motion_used,
5638 gm_info->params_cost)) {
David Turner2f3b5df2019-01-02 14:30:50 +00005639 loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005640 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005641
Yaowu Xuc27fc142016-08-22 16:08:15 -07005642 if (loop) {
5643 ++loop_count;
5644 ++loop_at_this_size;
5645
5646#if CONFIG_INTERNAL_STATS
5647 ++cpi->tot_recode_hits;
5648#endif
5649 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005650#if CONFIG_COLLECT_COMPONENT_TIMING
5651 if (loop) printf("\n Recoding:");
5652#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005653 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08005654
Marco Paniconi574e59e2019-10-23 15:47:28 -07005655 // Update some stats from cyclic refresh.
Marco Paniconi51415512019-11-27 12:38:24 -08005656 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !frame_is_intra_only(cm))
Marco Paniconi574e59e2019-10-23 15:47:28 -07005657 av1_cyclic_refresh_postencode(cpi);
5658
Tom Finegane4099e32018-01-23 12:01:51 -08005659 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005660}
5661
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005662static int encode_with_recode_loop_and_filter(AV1_COMP *cpi, size_t *size,
5663 uint8_t *dest, int64_t *sse,
5664 int64_t *rate,
5665 int *largest_tile_id) {
5666#if CONFIG_COLLECT_COMPONENT_TIMING
5667 start_timing(cpi, encode_with_recode_loop_time);
5668#endif
5669 int err = encode_with_recode_loop(cpi, size, dest);
5670#if CONFIG_COLLECT_COMPONENT_TIMING
5671 end_timing(cpi, encode_with_recode_loop_time);
5672#endif
5673 if (err != AOM_CODEC_OK) {
5674 if (err == -1) {
5675 // special case as described in encode_with_recode_loop().
5676 // Encoding was skipped.
5677 err = AOM_CODEC_OK;
5678 if (sse != NULL) *sse = INT64_MAX;
5679 if (rate != NULL) *rate = INT64_MAX;
5680 *largest_tile_id = 0;
5681 }
5682 return err;
5683 }
5684
5685#ifdef OUTPUT_YUV_SKINMAP
5686 if (cpi->common.current_frame.frame_number > 1) {
5687 av1_compute_skin_map(cpi, yuv_skinmap_file);
5688 }
5689#endif // OUTPUT_YUV_SKINMAP
5690
5691 AV1_COMMON *const cm = &cpi->common;
5692 SequenceHeader *const seq_params = &cm->seq_params;
5693
5694 // Special case code to reduce pulsing when key frames are forced at a
5695 // fixed interval. Note the reconstruction error if it is the frame before
5696 // the force key frame
5697 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005698#if CONFIG_AV1_HIGHBITDEPTH
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005699 if (seq_params->use_highbitdepth) {
5700 cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
5701 } else {
5702 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5703 }
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005704#else
5705 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5706#endif
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005707 }
5708
5709 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
5710 cm->cur_frame->buf.transfer_characteristics =
5711 seq_params->transfer_characteristics;
5712 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
5713 cm->cur_frame->buf.monochrome = seq_params->monochrome;
5714 cm->cur_frame->buf.chroma_sample_position =
5715 seq_params->chroma_sample_position;
5716 cm->cur_frame->buf.color_range = seq_params->color_range;
5717 cm->cur_frame->buf.render_width = cm->render_width;
5718 cm->cur_frame->buf.render_height = cm->render_height;
5719
5720 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5721 // off.
5722
5723 // Pick the loop filter level for the frame.
Urvang Joshib6409e92020-03-23 11:23:27 -07005724 if (!cm->features.allow_intrabc) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005725 loopfilter_frame(cpi, cm);
5726 } else {
5727 cm->lf.filter_level[0] = 0;
5728 cm->lf.filter_level[1] = 0;
5729 cm->cdef_info.cdef_bits = 0;
5730 cm->cdef_info.cdef_strengths[0] = 0;
5731 cm->cdef_info.nb_cdef_strengths = 1;
5732 cm->cdef_info.cdef_uv_strengths[0] = 0;
5733 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5734 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5735 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
5736 }
5737
5738 // TODO(debargha): Fix mv search range on encoder side
5739 // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5740 aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5741
5742#ifdef OUTPUT_YUV_REC
5743 aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
5744#endif
5745
5746 finalize_encoded_frame(cpi);
5747 // Build the bitstream
5748#if CONFIG_COLLECT_COMPONENT_TIMING
5749 start_timing(cpi, av1_pack_bitstream_final_time);
5750#endif
5751 if (av1_pack_bitstream(cpi, dest, size, largest_tile_id) != AOM_CODEC_OK)
5752 return AOM_CODEC_ERROR;
5753#if CONFIG_COLLECT_COMPONENT_TIMING
5754 end_timing(cpi, av1_pack_bitstream_final_time);
5755#endif
5756
5757 // Compute sse and rate.
5758 if (sse != NULL) {
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005759#if CONFIG_AV1_HIGHBITDEPTH
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005760 *sse = (seq_params->use_highbitdepth)
5761 ? aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf)
5762 : aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005763#else
5764 *sse = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5765#endif
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005766 }
5767 if (rate != NULL) {
5768 const int64_t bits = (*size << 3);
5769 *rate = (bits << 5); // To match scale.
5770 }
5771 return AOM_CODEC_OK;
5772}
5773
5774#if CONFIG_SUPERRES_IN_RECODE
5775
5776static void save_cur_buf(AV1_COMP *cpi) {
5777 CODING_CONTEXT *const cc = &cpi->coding_context;
5778 AV1_COMMON *cm = &cpi->common;
5779 const YV12_BUFFER_CONFIG *ybf = &cm->cur_frame->buf;
5780 memset(&cc->copy_buffer, 0, sizeof(cc->copy_buffer));
5781 if (aom_alloc_frame_buffer(&cc->copy_buffer, ybf->y_crop_width,
5782 ybf->y_crop_height, ybf->subsampling_x,
5783 ybf->subsampling_y,
5784 ybf->flags & YV12_FLAG_HIGHBITDEPTH, ybf->border,
Urvang Joshi6237b882020-03-26 15:02:26 -07005785 cm->features.byte_alignment) != AOM_CODEC_OK) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005786 aom_internal_error(
5787 &cm->error, AOM_CODEC_MEM_ERROR,
5788 "Failed to allocate copy buffer for saving coding context");
5789 }
5790 aom_yv12_copy_frame(ybf, &cc->copy_buffer, av1_num_planes(cm));
5791}
5792
5793// Coding context that only needs to be saved when recode loop includes
5794// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5795// restoraton).
5796static void save_extra_coding_context(AV1_COMP *cpi) {
5797 CODING_CONTEXT *const cc = &cpi->coding_context;
5798 AV1_COMMON *cm = &cpi->common;
5799
5800 cc->lf = cm->lf;
5801 cc->cdef_info = cm->cdef_info;
5802 cc->rc = cpi->rc;
5803}
5804
5805static void save_all_coding_context(AV1_COMP *cpi) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005806 save_cur_buf(cpi);
5807 save_extra_coding_context(cpi);
5808 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5809}
5810
5811static void restore_cur_buf(AV1_COMP *cpi) {
5812 CODING_CONTEXT *const cc = &cpi->coding_context;
5813 AV1_COMMON *cm = &cpi->common;
5814 aom_yv12_copy_frame(&cc->copy_buffer, &cm->cur_frame->buf,
5815 av1_num_planes(cm));
5816}
5817
5818// Coding context that only needs to be restored when recode loop includes
5819// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5820// restoraton).
5821static void restore_extra_coding_context(AV1_COMP *cpi) {
5822 CODING_CONTEXT *const cc = &cpi->coding_context;
5823 AV1_COMMON *cm = &cpi->common;
5824 cm->lf = cc->lf;
5825 cm->cdef_info = cc->cdef_info;
5826 cpi->rc = cc->rc;
5827}
5828
5829static void restore_all_coding_context(AV1_COMP *cpi) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005830 restore_cur_buf(cpi);
5831 restore_extra_coding_context(cpi);
5832 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5833}
5834
5835static int encode_with_and_without_superres(AV1_COMP *cpi, size_t *size,
5836 uint8_t *dest,
5837 int *largest_tile_id) {
5838 const AV1_COMMON *const cm = &cpi->common;
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005839 assert(cm->seq_params.enable_superres);
5840 assert(superres_in_recode_allowed(cpi));
5841 aom_codec_err_t err = AOM_CODEC_OK;
5842 save_all_coding_context(cpi);
5843
5844 // Encode with superres.
Urvang Joshibc82d382019-11-01 17:59:20 -07005845#if SUPERRES_RECODE_ALL_RATIOS
Urvang Joshif9c2bd72020-04-07 15:11:51 -07005846 AV1EncoderConfig *const oxcf = &cpi->oxcf;
Urvang Joshibc82d382019-11-01 17:59:20 -07005847 int64_t superres_sses[SCALE_NUMERATOR];
5848 int64_t superres_rates[SCALE_NUMERATOR];
5849 int superres_largest_tile_ids[SCALE_NUMERATOR];
5850 // Use superres for Key-frames and Alt-ref frames only.
5851 const GF_GROUP *const gf_group = &cpi->gf_group;
5852 if (gf_group->update_type[gf_group->index] != OVERLAY_UPDATE &&
5853 gf_group->update_type[gf_group->index] != INTNL_OVERLAY_UPDATE) {
5854 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
5855 ++denom) {
5856 oxcf->superres_scale_denominator = denom;
5857 oxcf->superres_kf_scale_denominator = denom;
5858 const int this_index = denom - (SCALE_NUMERATOR + 1);
5859 err = encode_with_recode_loop_and_filter(
5860 cpi, size, dest, &superres_sses[this_index],
5861 &superres_rates[this_index], &superres_largest_tile_ids[this_index]);
5862 if (err != AOM_CODEC_OK) return err;
5863 restore_all_coding_context(cpi);
5864 }
5865 // Reset.
5866 oxcf->superres_scale_denominator = SCALE_NUMERATOR;
5867 oxcf->superres_kf_scale_denominator = SCALE_NUMERATOR;
5868 } else {
5869 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
5870 ++denom) {
5871 const int this_index = denom - (SCALE_NUMERATOR + 1);
5872 superres_sses[this_index] = INT64_MAX;
5873 superres_rates[this_index] = INT64_MAX;
5874 }
5875 }
5876#else
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005877 int64_t sse1 = INT64_MAX;
5878 int64_t rate1 = INT64_MAX;
5879 int largest_tile_id1;
5880 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse1, &rate1,
5881 &largest_tile_id1);
5882 if (err != AOM_CODEC_OK) return err;
Urvang Joshibc82d382019-11-01 17:59:20 -07005883 restore_all_coding_context(cpi);
5884#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005885
5886 // Encode without superres.
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005887 int64_t sse2 = INT64_MAX;
5888 int64_t rate2 = INT64_MAX;
5889 int largest_tile_id2;
Urvang Joshif9c2bd72020-04-07 15:11:51 -07005890 cpi->superres_mode = SUPERRES_NONE; // To force full-res.
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005891 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
5892 &largest_tile_id2);
Urvang Joshif9c2bd72020-04-07 15:11:51 -07005893 cpi->superres_mode = cpi->oxcf.superres_mode; // Reset.
5894 assert(cpi->oxcf.superres_mode == SUPERRES_AUTO);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005895 if (err != AOM_CODEC_OK) return err;
5896
5897 // Note: Both use common rdmult based on base qindex of fullres.
5898 const int64_t rdmult =
Urvang Joshi17814622020-03-27 17:26:17 -07005899 av1_compute_rd_mult_based_on_qindex(cpi, cm->quant_params.base_qindex);
Urvang Joshibc82d382019-11-01 17:59:20 -07005900
5901#if SUPERRES_RECODE_ALL_RATIOS
5902 // Find the best rdcost among all superres denoms.
5903 double proj_rdcost1 = DBL_MAX;
5904 int64_t sse1 = INT64_MAX;
5905 int64_t rate1 = INT64_MAX;
5906 int largest_tile_id1 = 0;
5907 (void)sse1;
5908 (void)rate1;
5909 (void)largest_tile_id1;
5910 int best_denom = -1;
5911 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR; ++denom) {
5912 const int this_index = denom - (SCALE_NUMERATOR + 1);
5913 const int64_t this_sse = superres_sses[this_index];
5914 const int64_t this_rate = superres_rates[this_index];
5915 const int this_largest_tile_id = superres_largest_tile_ids[this_index];
5916 const double this_rdcost = RDCOST_DBL(rdmult, this_rate, this_sse);
5917 if (this_rdcost < proj_rdcost1) {
5918 sse1 = this_sse;
5919 rate1 = this_rate;
5920 largest_tile_id1 = this_largest_tile_id;
5921 proj_rdcost1 = this_rdcost;
5922 best_denom = denom;
5923 }
5924 }
5925#else
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005926 const double proj_rdcost1 = RDCOST_DBL(rdmult, rate1, sse1);
Urvang Joshibc82d382019-11-01 17:59:20 -07005927#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005928 const double proj_rdcost2 = RDCOST_DBL(rdmult, rate2, sse2);
5929
5930 // Re-encode with superres if it's better.
5931 if (proj_rdcost1 < proj_rdcost2) {
5932 restore_all_coding_context(cpi);
5933 // TODO(urvang): We should avoid rerunning the recode loop by saving
5934 // previous output+state, or running encode only for the selected 'q' in
5935 // previous step.
Urvang Joshibc82d382019-11-01 17:59:20 -07005936#if SUPERRES_RECODE_ALL_RATIOS
5937 // Again, temporarily force the best denom.
5938 oxcf->superres_scale_denominator = best_denom;
5939 oxcf->superres_kf_scale_denominator = best_denom;
5940#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005941 int64_t sse3 = INT64_MAX;
5942 int64_t rate3 = INT64_MAX;
5943 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
5944 largest_tile_id);
5945 assert(sse1 == sse3);
5946 assert(rate1 == rate3);
5947 assert(largest_tile_id1 == *largest_tile_id);
Urvang Joshibc82d382019-11-01 17:59:20 -07005948#if SUPERRES_RECODE_ALL_RATIOS
5949 // Reset.
5950 oxcf->superres_scale_denominator = SCALE_NUMERATOR;
5951 oxcf->superres_kf_scale_denominator = SCALE_NUMERATOR;
5952#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005953 } else {
5954 *largest_tile_id = largest_tile_id2;
5955 }
5956
5957 return err;
5958}
5959#endif // CONFIG_SUPERRES_IN_RECODE
5960
Yaowu Xuc27fc142016-08-22 16:08:15 -07005961#define DUMP_RECON_FRAMES 0
5962
5963#if DUMP_RECON_FRAMES == 1
5964// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07005965static void dump_filtered_recon_frames(AV1_COMP *cpi) {
5966 AV1_COMMON *const cm = &cpi->common;
David Turnerd2a592e2018-11-16 14:59:31 +00005967 const CurrentFrame *const current_frame = &cm->current_frame;
David Turnerc29e1a92018-12-06 14:10:14 +00005968 const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005969
Zoe Liub4f31032017-11-03 23:48:35 -07005970 if (recon_buf == NULL) {
David Turnerd2a592e2018-11-16 14:59:31 +00005971 printf("Frame %d is not ready.\n", current_frame->frame_number);
Zoe Liub4f31032017-11-03 23:48:35 -07005972 return;
5973 }
5974
Zoe Liu27deb382018-03-27 15:13:56 -07005975 static const int flag_list[REF_FRAMES] = { 0,
5976 AOM_LAST_FLAG,
5977 AOM_LAST2_FLAG,
5978 AOM_LAST3_FLAG,
5979 AOM_GOLD_FLAG,
5980 AOM_BWD_FLAG,
5981 AOM_ALT2_FLAG,
5982 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07005983 printf(
5984 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
5985 "show_existing_frame=%d) "
5986 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
David Turnerd2a592e2018-11-16 14:59:31 +00005987 current_frame->frame_number, current_frame->order_hint, cm->show_frame,
Zoe Liub4f31032017-11-03 23:48:35 -07005988 cm->show_existing_frame);
5989 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00005990 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
5991 const int ref_offset = buf != NULL ? (int)buf->order_hint : -1;
Urvang Joshib6f17672019-03-05 11:51:02 -08005992 printf(" %d(%c)", ref_offset,
5993 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N');
Zoe Liub4f31032017-11-03 23:48:35 -07005994 }
5995 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07005996
5997 if (!cm->show_frame) {
5998 printf("Frame %d is a no show frame, so no image dump.\n",
David Turnerd2a592e2018-11-16 14:59:31 +00005999 current_frame->frame_number);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006000 return;
6001 }
6002
Zoe Liub4f31032017-11-03 23:48:35 -07006003 int h;
6004 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
6005 FILE *f_recon = NULL;
6006
David Turnerd2a592e2018-11-16 14:59:31 +00006007 if (current_frame->frame_number == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006008 if ((f_recon = fopen(file_name, "wb")) == NULL) {
6009 printf("Unable to open file %s to write.\n", file_name);
6010 return;
6011 }
6012 } else {
6013 if ((f_recon = fopen(file_name, "ab")) == NULL) {
6014 printf("Unable to open file %s to append.\n", file_name);
6015 return;
6016 }
6017 }
6018 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07006019 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
6020 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
Urvang Joshi7a890232019-03-22 17:00:31 -07006021 "refresh_alt_ref_frame=%d, "
Zoe Liuf40a9572017-10-13 12:37:19 -07006022 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Sarah Parkere1b22012019-06-06 16:35:25 -07006023 current_frame->frame_number, cpi->gf_group.index,
6024 cpi->gf_group.update_type[cpi->gf_group.index], current_frame->order_hint,
6025 cm->show_frame, cm->show_existing_frame, cpi->rc.source_alt_ref_active,
6026 cpi->refresh_alt_ref_frame, recon_buf->y_stride, recon_buf->uv_stride,
6027 cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07006028#if 0
6029 int ref_frame;
6030 printf("get_ref_frame_map_idx: [");
6031 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
David Turnera21966b2018-12-05 14:48:49 +00006032 printf(" %d", get_ref_frame_map_idx(cm, ref_frame));
Zoe Liue9b15e22017-07-19 15:53:01 -07006033 printf(" ]\n");
Zoe Liue9b15e22017-07-19 15:53:01 -07006034#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07006035
6036 // --- Y ---
6037 for (h = 0; h < cm->height; ++h) {
6038 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
6039 f_recon);
6040 }
6041 // --- U ---
6042 for (h = 0; h < (cm->height >> 1); ++h) {
6043 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
6044 f_recon);
6045 }
6046 // --- V ---
6047 for (h = 0; h < (cm->height >> 1); ++h) {
6048 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
6049 f_recon);
6050 }
6051
6052 fclose(f_recon);
6053}
6054#endif // DUMP_RECON_FRAMES
6055
Vishesh2260a592020-04-06 15:34:51 +05306056static int is_integer_mv(const YV12_BUFFER_CONFIG *cur_picture,
6057 const YV12_BUFFER_CONFIG *last_picture,
6058 ForceIntegerMVInfo *const force_intpel_info) {
David Turnerefed6372019-01-11 15:14:11 +00006059 aom_clear_system_state();
6060 // check use hash ME
6061 int k;
David Turnerefed6372019-01-11 15:14:11 +00006062
Ranjit Kumar Tulabandu9f26c992019-10-24 12:24:52 +05306063 const int block_size = FORCE_INT_MV_DECISION_BLOCK_SIZE;
David Turnerefed6372019-01-11 15:14:11 +00006064 const double threshold_current = 0.8;
6065 const double threshold_average = 0.95;
6066 const int max_history_size = 32;
6067 int T = 0; // total block
6068 int C = 0; // match with collocated block
6069 int S = 0; // smooth region but not match with collocated block
David Turnerefed6372019-01-11 15:14:11 +00006070
6071 const int pic_width = cur_picture->y_width;
6072 const int pic_height = cur_picture->y_height;
6073 for (int i = 0; i + block_size <= pic_height; i += block_size) {
6074 for (int j = 0; j + block_size <= pic_width; j += block_size) {
6075 const int x_pos = j;
6076 const int y_pos = i;
6077 int match = 1;
6078 T++;
6079
6080 // check whether collocated block match with current
6081 uint8_t *p_cur = cur_picture->y_buffer;
6082 uint8_t *p_ref = last_picture->y_buffer;
6083 int stride_cur = cur_picture->y_stride;
6084 int stride_ref = last_picture->y_stride;
6085 p_cur += (y_pos * stride_cur + x_pos);
6086 p_ref += (y_pos * stride_ref + x_pos);
6087
6088 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
6089 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
6090 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
6091 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
6092 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
6093 if (p16_cur[tmpX] != p16_ref[tmpX]) {
6094 match = 0;
6095 }
6096 }
6097 p16_cur += stride_cur;
6098 p16_ref += stride_ref;
6099 }
6100 } else {
6101 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
6102 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
6103 if (p_cur[tmpX] != p_ref[tmpX]) {
6104 match = 0;
6105 }
6106 }
6107 p_cur += stride_cur;
6108 p_ref += stride_ref;
6109 }
6110 }
6111
6112 if (match) {
6113 C++;
6114 continue;
6115 }
6116
6117 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
6118 y_pos) ||
6119 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
6120 S++;
6121 continue;
6122 }
David Turnerefed6372019-01-11 15:14:11 +00006123 }
6124 }
6125
6126 assert(T > 0);
Remya163db3c2020-02-17 14:29:41 +05306127 double cs_rate = ((double)(C + S)) / ((double)(T));
David Turnerefed6372019-01-11 15:14:11 +00006128
Vishesh2260a592020-04-06 15:34:51 +05306129 force_intpel_info->cs_rate_array[force_intpel_info->rate_index] = cs_rate;
David Turnerefed6372019-01-11 15:14:11 +00006130
Vishesh2260a592020-04-06 15:34:51 +05306131 force_intpel_info->rate_index =
6132 (force_intpel_info->rate_index + 1) % max_history_size;
6133 force_intpel_info->rate_size++;
6134 force_intpel_info->rate_size =
6135 AOMMIN(force_intpel_info->rate_size, max_history_size);
David Turnerefed6372019-01-11 15:14:11 +00006136
Remya163db3c2020-02-17 14:29:41 +05306137 if (cs_rate < threshold_current) {
David Turnerefed6372019-01-11 15:14:11 +00006138 return 0;
6139 }
6140
6141 if (C == T) {
6142 return 1;
6143 }
6144
Remya163db3c2020-02-17 14:29:41 +05306145 double cs_average = 0.0;
David Turnerefed6372019-01-11 15:14:11 +00006146
Vishesh2260a592020-04-06 15:34:51 +05306147 for (k = 0; k < force_intpel_info->rate_size; k++) {
6148 cs_average += force_intpel_info->cs_rate_array[k];
David Turnerefed6372019-01-11 15:14:11 +00006149 }
Vishesh2260a592020-04-06 15:34:51 +05306150 cs_average /= force_intpel_info->rate_size;
David Turnerefed6372019-01-11 15:14:11 +00006151
Remya163db3c2020-02-17 14:29:41 +05306152 if (cs_average < threshold_average) {
David Turnerefed6372019-01-11 15:14:11 +00006153 return 0;
6154 }
6155
Remya163db3c2020-02-17 14:29:41 +05306156 if ((T - C - S) < 0) {
David Turnerefed6372019-01-11 15:14:11 +00006157 return 1;
6158 }
6159
Remya163db3c2020-02-17 14:29:41 +05306160 if (cs_average > 1.01) {
David Turnerefed6372019-01-11 15:14:11 +00006161 return 1;
6162 }
6163
6164 return 0;
6165}
6166
David Turner73245762019-02-11 16:42:34 +00006167// Refresh reference frame buffers according to refresh_frame_flags.
6168static void refresh_reference_frames(AV1_COMP *cpi) {
6169 AV1_COMMON *const cm = &cpi->common;
6170 // All buffers are refreshed for shown keyframes and S-frames.
6171
6172 for (int ref_frame = 0; ref_frame < REF_FRAMES; ref_frame++) {
6173 if (((cm->current_frame.refresh_frame_flags >> ref_frame) & 1) == 1) {
6174 assign_frame_buffer_p(&cm->ref_frame_map[ref_frame], cm->cur_frame);
6175 }
6176 }
6177}
6178
sdengc23c7f12019-06-11 16:56:50 -07006179static void set_mb_ssim_rdmult_scaling(AV1_COMP *cpi) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006180 const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
sdengc23c7f12019-06-11 16:56:50 -07006181 ThreadData *td = &cpi->td;
6182 MACROBLOCK *x = &td->mb;
6183 MACROBLOCKD *xd = &x->e_mbd;
6184 uint8_t *y_buffer = cpi->source->y_buffer;
6185 const int y_stride = cpi->source->y_stride;
6186 const int block_size = BLOCK_16X16;
6187
6188 const int num_mi_w = mi_size_wide[block_size];
6189 const int num_mi_h = mi_size_high[block_size];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006190 const int num_cols = (mi_params->mi_cols + num_mi_w - 1) / num_mi_w;
6191 const int num_rows = (mi_params->mi_rows + num_mi_h - 1) / num_mi_h;
sdengc23c7f12019-06-11 16:56:50 -07006192 double log_sum = 0.0;
sdengc23c7f12019-06-11 16:56:50 -07006193 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
6194
sdengc23c7f12019-06-11 16:56:50 -07006195 // Loop through each 16x16 block.
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006196 for (int row = 0; row < num_rows; ++row) {
6197 for (int col = 0; col < num_cols; ++col) {
sdengc23c7f12019-06-11 16:56:50 -07006198 double var = 0.0, num_of_var = 0.0;
6199 const int index = row * num_cols + col;
6200
6201 // Loop through each 8x8 block.
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006202 for (int mi_row = row * num_mi_h;
6203 mi_row < mi_params->mi_rows && mi_row < (row + 1) * num_mi_h;
6204 mi_row += 2) {
6205 for (int mi_col = col * num_mi_w;
6206 mi_col < mi_params->mi_cols && mi_col < (col + 1) * num_mi_w;
sdengc23c7f12019-06-11 16:56:50 -07006207 mi_col += 2) {
6208 struct buf_2d buf;
6209 const int row_offset_y = mi_row << 2;
6210 const int col_offset_y = mi_col << 2;
6211
6212 buf.buf = y_buffer + row_offset_y * y_stride + col_offset_y;
6213 buf.stride = y_stride;
6214
6215 if (use_hbd) {
6216 var += av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8,
6217 xd->bd);
6218 } else {
6219 var += av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8);
6220 }
6221
6222 num_of_var += 1.0;
6223 }
6224 }
sdeng32185d12019-06-19 14:47:09 -07006225 var = var / num_of_var;
sdengbdc0cd22019-11-11 15:09:34 -08006226
6227 // Curve fitting with an exponential model on all 16x16 blocks from the
6228 // midres dataset.
6229 var = 67.035434 * (1 - exp(-0.0021489 * var)) + 17.492222;
sdengc23c7f12019-06-11 16:56:50 -07006230 cpi->ssim_rdmult_scaling_factors[index] = var;
6231 log_sum += log(var);
6232 }
6233 }
6234 log_sum = exp(log_sum / (double)(num_rows * num_cols));
6235
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006236 for (int row = 0; row < num_rows; ++row) {
6237 for (int col = 0; col < num_cols; ++col) {
sdengc23c7f12019-06-11 16:56:50 -07006238 const int index = row * num_cols + col;
6239 cpi->ssim_rdmult_scaling_factors[index] /= log_sum;
6240 }
6241 }
sdengc23c7f12019-06-11 16:56:50 -07006242}
6243
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08006244extern void av1_print_frame_contexts(const FRAME_CONTEXT *fc,
6245 const char *filename);
6246
David Turner73245762019-02-11 16:42:34 +00006247static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
6248 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006249 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006250 SequenceHeader *const seq_params = &cm->seq_params;
David Turnerd2a592e2018-11-16 14:59:31 +00006251 CurrentFrame *const current_frame = &cm->current_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07006252 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006253 struct segmentation *const seg = &cm->seg;
Urvang Joshib6409e92020-03-23 11:23:27 -07006254 FeatureFlags *const features = &cm->features;
Thomas Davies4822e142017-10-10 11:30:36 +01006255
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006256#if CONFIG_COLLECT_COMPONENT_TIMING
6257 start_timing(cpi, encode_frame_to_data_rate_time);
6258#endif
6259
Fangwen Fu8d164de2016-12-14 13:40:54 -08006260 // frame type has been decided outside of this function call
David Turnerd2a592e2018-11-16 14:59:31 +00006261 cm->cur_frame->frame_type = current_frame->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07006262
Urvang Joshi54ffae72020-03-23 13:37:10 -07006263 cm->tiles.large_scale = cpi->oxcf.large_scale_tile;
6264 cm->tiles.single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wang9612d552018-05-15 14:58:30 -07006265
Urvang Joshib6409e92020-03-23 11:23:27 -07006266 features->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
6267 // features->allow_ref_frame_mvs needs to be written into the frame header
Urvang Joshi54ffae72020-03-23 13:37:10 -07006268 // while cm->tiles.large_scale is 1, therefore, "cm->tiles.large_scale=1" case
Urvang Joshib6409e92020-03-23 11:23:27 -07006269 // is separated from frame_might_allow_ref_frame_mvs().
Urvang Joshi54ffae72020-03-23 13:37:10 -07006270 features->allow_ref_frame_mvs &= !cm->tiles.large_scale;
Yunqing Wangd48fb162018-06-15 10:55:28 -07006271
Urvang Joshib6409e92020-03-23 11:23:27 -07006272 features->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07006273 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006274
Urvang Joshi1de67aa2020-03-20 11:21:57 -07006275 cpi->last_frame_type = current_frame->frame_type;
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05306276
Sarah Parker33005522018-07-27 14:46:25 -07006277 if (encode_show_existing_frame(cm)) {
David Turner996b2c12018-12-07 15:52:30 +00006278 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006279 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00006280 int largest_tile_id = 0; // Output from bitstream: unused here
6281 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08006282 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006283
David Turner90311862018-11-29 13:34:36 +00006284 if (seq_params->frame_id_numbers_present_flag &&
6285 current_frame->frame_type == KEY_FRAME) {
6286 // Displaying a forward key-frame, so reset the ref buffer IDs
6287 int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
6288 for (int i = 0; i < REF_FRAMES; i++)
6289 cm->ref_frame_id[i] = display_frame_id;
6290 }
6291
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07006292 cpi->seq_params_locked = 1;
6293
Yaowu Xuc27fc142016-08-22 16:08:15 -07006294#if DUMP_RECON_FRAMES == 1
6295 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
6296 dump_filtered_recon_frames(cpi);
6297#endif // DUMP_RECON_FRAMES
6298
David Turner73245762019-02-11 16:42:34 +00006299 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
6300 // for the purpose to verify no mismatch between encoder and decoder.
6301 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006302
David Turner73245762019-02-11 16:42:34 +00006303 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006304
Yaowu Xuc27fc142016-08-22 16:08:15 -07006305 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
6306 // to do post-encoding update accordingly.
6307 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07006308 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07006309 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006310 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006311
David Turnerd2a592e2018-11-16 14:59:31 +00006312 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006313
Tom Finegane4099e32018-01-23 12:01:51 -08006314 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006315 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006316
David Turnerefed6372019-01-11 15:14:11 +00006317 // Work out whether to force_integer_mv this frame
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306318 if (!is_stat_generation_stage(cpi) &&
Urvang Joshib6409e92020-03-23 11:23:27 -07006319 cpi->common.features.allow_screen_content_tools &&
6320 !frame_is_intra_only(cm)) {
David Turnerefed6372019-01-11 15:14:11 +00006321 if (cpi->common.seq_params.force_integer_mv == 2) {
6322 // Adaptive mode: see what previous frame encoded did
6323 if (cpi->unscaled_last_source != NULL) {
Vishesh2260a592020-04-06 15:34:51 +05306324 features->cur_frame_force_integer_mv = is_integer_mv(
6325 cpi->source, cpi->unscaled_last_source, &cpi->force_intpel_info);
David Turnerefed6372019-01-11 15:14:11 +00006326 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07006327 cpi->common.features.cur_frame_force_integer_mv = 0;
David Turnerefed6372019-01-11 15:14:11 +00006328 }
6329 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07006330 cpi->common.features.cur_frame_force_integer_mv =
David Turnerefed6372019-01-11 15:14:11 +00006331 cpi->common.seq_params.force_integer_mv;
6332 }
6333 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07006334 cpi->common.features.cur_frame_force_integer_mv = 0;
David Turnerefed6372019-01-11 15:14:11 +00006335 }
6336
Yaowu Xuc27fc142016-08-22 16:08:15 -07006337 // Set default state for segment based loop filter update flags.
6338 cm->lf.mode_ref_delta_update = 0;
6339
Yaowu Xuc27fc142016-08-22 16:08:15 -07006340 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05006341 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006342 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07006343 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006344
6345 // If segmentation is enabled force a map update for key frames.
6346 if (seg->enabled) {
6347 seg->update_map = 1;
6348 seg->update_data = 1;
6349 }
6350
6351 // The alternate reference frame cannot be active for a key frame.
6352 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006353 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00006354 if (cpi->oxcf.mtu == 0) {
Urvang Joshi1de67aa2020-03-20 11:21:57 -07006355 cpi->num_tg = cpi->oxcf.num_tile_groups;
Thomas Daviesaf6df172016-11-09 14:04:18 +00006356 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08006357 // Use a default value for the purposes of weighting costs in probability
6358 // updates
Urvang Joshi1de67aa2020-03-20 11:21:57 -07006359 cpi->num_tg = DEFAULT_MAX_NUM_TG;
Thomas Daviesaf6df172016-11-09 14:04:18 +00006360 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006361
6362 // For 1 pass CBR, check if we are dropping this frame.
6363 // Never drop on key frame.
Mufaddal Chakera3bcc72c2019-12-11 14:38:37 +05306364 if (has_no_stats_stage(cpi) && oxcf->rc_mode == AOM_CBR &&
David Turnerd2a592e2018-11-16 14:59:31 +00006365 current_frame->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006366 if (av1_rc_drop_frame(cpi)) {
6367 av1_rc_postencode_update_drop_frame(cpi);
David Turnera4c96252019-01-11 16:36:39 +00006368 release_scaled_references(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08006369 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006370 }
6371 }
6372
sdengc23c7f12019-06-11 16:56:50 -07006373 if (oxcf->tuning == AOM_TUNE_SSIM) set_mb_ssim_rdmult_scaling(cpi);
6374
sdeng01959162019-12-20 10:46:24 -08006375#if CONFIG_TUNE_VMAF
6376 if (oxcf->tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
sdeng615dc242020-02-04 16:06:14 -08006377 oxcf->tuning == AOM_TUNE_VMAF_MAX_GAIN) {
sdeng01959162019-12-20 10:46:24 -08006378 av1_set_mb_vmaf_rdmult_scaling(cpi);
sdeng29c62152019-12-09 12:44:18 -08006379 }
sdeng01959162019-12-20 10:46:24 -08006380#endif
sdeng29c62152019-12-09 12:44:18 -08006381
Yaowu Xuf883b422016-08-30 14:01:10 -07006382 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006383
6384#if CONFIG_INTERNAL_STATS
6385 memset(cpi->mode_chosen_counts, 0,
6386 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
6387#endif
6388
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006389 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006390 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08006391 * wraparound) */
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006392 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00006393 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08006394 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07006395 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
6396 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
6397 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00006398 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07006399 lsb = cpi->source->y_buffer[0] & 0xff;
6400 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00006401 }
David Turner760a2f42018-12-07 15:25:36 +00006402 cm->current_frame_id =
6403 ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05006404
6405 // S_frame is meant for stitching different streams of different
6406 // resolutions together, so current_frame_id must be the
6407 // same across different streams of the same content current_frame_id
6408 // should be the same and not random. 0x37 is a chosen number as start
6409 // point
6410 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006411 } else {
6412 cm->current_frame_id =
David Turner760a2f42018-12-07 15:25:36 +00006413 (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
6414 (1 << seq_params->frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006415 }
6416 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006417
Hui Su483a8452018-02-26 12:28:48 -08006418 switch (cpi->oxcf.cdf_update_mode) {
6419 case 0: // No CDF update for any frames(4~6% compression loss).
Urvang Joshib6409e92020-03-23 11:23:27 -07006420 features->disable_cdf_update = 1;
Hui Su483a8452018-02-26 12:28:48 -08006421 break;
6422 case 1: // Enable CDF update for all frames.
Urvang Joshib6409e92020-03-23 11:23:27 -07006423 features->disable_cdf_update = 0;
Hui Su483a8452018-02-26 12:28:48 -08006424 break;
6425 case 2:
6426 // Strategically determine at which frames to do CDF update.
6427 // Currently only enable CDF update for all-intra and no-show frames(1.5%
6428 // compression loss).
6429 // TODO(huisu@google.com): design schemes for various trade-offs between
6430 // compression quality and decoding speed.
Urvang Joshib6409e92020-03-23 11:23:27 -07006431 features->disable_cdf_update =
Hui Sub1b76b32018-02-27 15:24:48 -08006432 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08006433 break;
6434 }
Urvang Joshi450a9a22020-03-31 16:00:22 -07006435 seq_params->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08006436
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07006437 int largest_tile_id = 0;
6438#if CONFIG_SUPERRES_IN_RECODE
6439 if (superres_in_recode_allowed(cpi)) {
6440 if (encode_with_and_without_superres(cpi, size, dest, &largest_tile_id) !=
6441 AOM_CODEC_OK) {
6442 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006443 }
Hui Su06463e42018-02-23 22:17:36 -08006444 } else {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07006445#endif // CONFIG_SUPERRES_IN_RECODE
6446 if (encode_with_recode_loop_and_filter(cpi, size, dest, NULL, NULL,
6447 &largest_tile_id) != AOM_CODEC_OK) {
6448 return AOM_CODEC_ERROR;
6449 }
6450#if CONFIG_SUPERRES_IN_RECODE
Hui Su06463e42018-02-23 22:17:36 -08006451 }
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07006452#endif // CONFIG_SUPERRES_IN_RECODE
Yaowu Xuc27fc142016-08-22 16:08:15 -07006453
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07006454 cpi->seq_params_locked = 1;
6455
David Turner996b2c12018-12-07 15:52:30 +00006456 // Update reference frame ids for reference frames this frame will overwrite
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006457 if (seq_params->frame_id_numbers_present_flag) {
David Turner996b2c12018-12-07 15:52:30 +00006458 for (int i = 0; i < REF_FRAMES; i++) {
6459 if ((current_frame->refresh_frame_flags >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006460 cm->ref_frame_id[i] = cm->current_frame_id;
6461 }
6462 }
6463 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006464
Yaowu Xuc27fc142016-08-22 16:08:15 -07006465#if DUMP_RECON_FRAMES == 1
6466 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07006467 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006468#endif // DUMP_RECON_FRAMES
6469
Soo-Chul Han934af352017-10-15 15:21:51 -04006470 if (cm->seg.enabled) {
6471 if (cm->seg.update_map) {
6472 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07006473 } else if (cm->last_frame_seg_map) {
David Turnerb757ce02018-11-12 15:01:28 +00006474 memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006475 cm->mi_params.mi_cols * cm->mi_params.mi_rows * sizeof(uint8_t));
Soo-Chul Han934af352017-10-15 15:21:51 -04006476 }
6477 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006478
6479 if (frame_is_intra_only(cm) == 0) {
6480 release_scaled_references(cpi);
6481 }
6482
David Turner73245762019-02-11 16:42:34 +00006483 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
6484 // for the purpose to verify no mismatch between encoder and decoder.
6485 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
6486
6487 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006488
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08006489#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07006490 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08006491#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006492
Urvang Joshi6237b882020-03-26 15:02:26 -07006493 if (features->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
David Turner35cba132018-12-10 15:48:15 +00006494 *cm->fc = cpi->tile_data[largest_tile_id].tctx;
Hui Sudc54be62018-03-14 19:14:28 -07006495 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006496 }
Urvang Joshi54ffae72020-03-23 13:37:10 -07006497 if (!cm->tiles.large_scale) {
David Turnera4c96252019-01-11 16:36:39 +00006498 cm->cur_frame->frame_context = *cm->fc;
6499 }
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08006500
6501 if (cpi->oxcf.ext_tile_debug) {
6502 // (yunqing) This test ensures the correctness of large scale tile coding.
Urvang Joshi54ffae72020-03-23 13:37:10 -07006503 if (cm->tiles.large_scale && is_stat_consumption_stage(cpi)) {
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08006504 char fn[20] = "./fc";
6505 fn[4] = current_frame->frame_number / 100 + '0';
6506 fn[5] = (current_frame->frame_number % 100) / 10 + '0';
6507 fn[6] = (current_frame->frame_number % 10) + '0';
6508 fn[7] = '\0';
6509 av1_print_frame_contexts(cm->fc, fn);
6510 }
David Turnera4c96252019-01-11 16:36:39 +00006511 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006512
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006513#if CONFIG_COLLECT_COMPONENT_TIMING
6514 end_timing(cpi, encode_frame_to_data_rate_time);
6515
6516 // Print out timing information.
6517 int i;
chiyotsai9c484b32019-03-07 16:01:50 -08006518 fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n",
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006519 cm->current_frame.frame_number,
chiyotsai9c484b32019-03-07 16:01:50 -08006520 get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006521 for (i = 0; i < kTimingComponents; i++) {
6522 cpi->component_time[i] += cpi->frame_component_time[i];
6523 fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n",
6524 get_component_name(i), cpi->frame_component_time[i],
6525 cpi->component_time[i]);
6526 cpi->frame_component_time[i] = 0;
6527 }
6528#endif
6529
Urvang Joshi1de67aa2020-03-20 11:21:57 -07006530 cpi->last_frame_type = current_frame->frame_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006531
Yaowu Xuf883b422016-08-30 14:01:10 -07006532 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006533
Yaowu Xuc27fc142016-08-22 16:08:15 -07006534 // Clear the one shot update flags for segmentation map and mode/ref loop
6535 // filter deltas.
6536 cm->seg.update_map = 0;
6537 cm->seg.update_data = 0;
6538 cm->lf.mode_ref_delta_update = 0;
6539
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07006540 // A droppable frame might not be shown but it always
6541 // takes a space in the gf group. Therefore, even when
6542 // it is not shown, we still need update the count down.
6543
Yaowu Xuc27fc142016-08-22 16:08:15 -07006544 if (cm->show_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006545 // Don't increment frame counters if this was an altref buffer
6546 // update not a real frame
David Turnerd2a592e2018-11-16 14:59:31 +00006547 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006548 }
6549
Tom Finegane4099e32018-01-23 12:01:51 -08006550 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006551}
6552
David Turner056f7cd2019-01-07 17:48:13 +00006553int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
David Turnercb5e36f2019-01-17 17:15:25 +00006554 const EncodeFrameInput *const frame_input,
David Turner056f7cd2019-01-07 17:48:13 +00006555 const EncodeFrameParams *const frame_params,
6556 EncodeFrameResults *const frame_results) {
David Turner07dbd8e2019-01-08 17:16:25 +00006557 AV1_COMMON *const cm = &cpi->common;
David Turnera7f133c2019-01-22 14:47:16 +00006558 CurrentFrame *const current_frame = &cm->current_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00006559
David Turnercb5e36f2019-01-17 17:15:25 +00006560 cpi->unscaled_source = frame_input->source;
6561 cpi->source = frame_input->source;
6562 cpi->unscaled_last_source = frame_input->last_source;
David Turner056f7cd2019-01-07 17:48:13 +00006563
David Turner6e8b4d92019-02-18 15:01:33 +00006564 current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
Urvang Joshib6409e92020-03-23 11:23:27 -07006565 cm->features.error_resilient_mode = frame_params->error_resilient_mode;
Urvang Joshi6237b882020-03-26 15:02:26 -07006566 cm->features.primary_ref_frame = frame_params->primary_ref_frame;
David Turner475a3132019-01-18 15:17:17 +00006567 cm->current_frame.frame_type = frame_params->frame_type;
David Turnerdedd8ff2019-01-23 13:59:46 +00006568 cm->show_frame = frame_params->show_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00006569 cpi->ref_frame_flags = frame_params->ref_frame_flags;
David Turner04b70d82019-01-24 15:39:19 +00006570 cpi->speed = frame_params->speed;
David Turnere86ee0d2019-02-18 17:16:28 +00006571 cm->show_existing_frame = frame_params->show_existing_frame;
6572 cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
David Turner07dbd8e2019-01-08 17:16:25 +00006573
David Turner73245762019-02-11 16:42:34 +00006574 memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
6575 REF_FRAMES * sizeof(*cm->remapped_ref_idx));
6576
David Turnerfe3aecb2019-02-06 14:42:42 +00006577 cpi->refresh_golden_frame = frame_params->refresh_golden_frame;
6578 cpi->refresh_bwd_ref_frame = frame_params->refresh_bwd_ref_frame;
David Turnerfe3aecb2019-02-06 14:42:42 +00006579 cpi->refresh_alt_ref_frame = frame_params->refresh_alt_ref_frame;
6580
David Turnera7f133c2019-01-22 14:47:16 +00006581 if (current_frame->frame_type == KEY_FRAME && cm->show_frame)
6582 current_frame->frame_number = 0;
6583
Remyafc038662020-02-06 11:46:11 +05306584 current_frame->order_hint =
6585 current_frame->frame_number + frame_params->order_offset;
6586 current_frame->display_order_hint = current_frame->order_hint;
6587 current_frame->order_hint %=
6588 (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
David Turnera7f133c2019-01-22 14:47:16 +00006589
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306590 if (is_stat_generation_stage(cpi)) {
Jerome Jiang2612b4d2019-05-29 17:46:47 -07006591#if !CONFIG_REALTIME_ONLY
David Turnercb5e36f2019-01-17 17:15:25 +00006592 av1_first_pass(cpi, frame_input->ts_duration);
Jerome Jiang2612b4d2019-05-29 17:46:47 -07006593#endif
David Turnercb5e36f2019-01-17 17:15:25 +00006594 } else if (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) {
David Turner73245762019-02-11 16:42:34 +00006595 if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
6596 AOM_CODEC_OK) {
David Turnercb5e36f2019-01-17 17:15:25 +00006597 return AOM_CODEC_ERROR;
6598 }
6599 } else {
David Turner056f7cd2019-01-07 17:48:13 +00006600 return AOM_CODEC_ERROR;
6601 }
6602
6603 return AOM_CODEC_OK;
6604}
6605
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006606#if CONFIG_DENOISE
6607static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
6608 int block_size, float noise_level,
6609 int64_t time_stamp, int64_t end_time) {
6610 AV1_COMMON *const cm = &cpi->common;
6611 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006612 cpi->denoise_and_model = aom_denoise_and_model_alloc(
6613 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006614 if (!cpi->denoise_and_model) {
6615 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6616 "Error allocating denoise and model");
6617 return -1;
6618 }
6619 }
6620 if (!cpi->film_grain_table) {
6621 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
6622 if (!cpi->film_grain_table) {
6623 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6624 "Error allocating grain table");
6625 return -1;
6626 }
6627 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
6628 }
6629 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
6630 &cm->film_grain_params)) {
6631 if (cm->film_grain_params.apply_grain) {
6632 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
6633 &cm->film_grain_params);
6634 }
6635 }
6636 return 0;
6637}
6638#endif
6639
James Zern3e2613b2017-03-30 23:14:40 -07006640int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07006641 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
6642 int64_t end_time) {
6643 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006644 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006645 int res = 0;
6646 const int subsampling_x = sd->subsampling_x;
6647 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006648 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006649
sdenge63f9fa2019-12-13 09:29:55 -08006650#if CONFIG_TUNE_VMAF
6651 if (!is_stat_generation_stage(cpi) &&
6652 cpi->oxcf.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING) {
sdeng615dc242020-02-04 16:06:14 -08006653 av1_vmaf_frame_preprocessing(cpi, sd);
6654 }
6655 if (!is_stat_generation_stage(cpi) &&
6656 cpi->oxcf.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
6657 av1_vmaf_blk_preprocessing(cpi, sd);
sdenge63f9fa2019-12-13 09:29:55 -08006658 }
6659#endif
6660
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006661#if CONFIG_INTERNAL_STATS
6662 struct aom_usec_timer timer;
Yaowu Xuf883b422016-08-30 14:01:10 -07006663 aom_usec_timer_start(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006664#endif
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006665#if CONFIG_DENOISE
6666 if (cpi->oxcf.noise_level > 0)
6667 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
6668 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
6669 res = -1;
6670#endif // CONFIG_DENOISE
6671
Yaowu Xuf883b422016-08-30 14:01:10 -07006672 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08006673 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07006674 res = -1;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006675#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006676 aom_usec_timer_mark(&timer);
6677 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006678#endif
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006679 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07006680 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006681 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006682 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07006683 res = -1;
6684 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006685 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006686 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006687 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006688 "Profile 1 requires 4:4:4 color format");
6689 res = -1;
6690 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006691 if ((seq_params->profile == PROFILE_2) &&
6692 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006693 !(subsampling_x == 1 && subsampling_y == 0)) {
6694 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
6695 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07006696 res = -1;
6697 }
6698
6699 return res;
6700}
6701
Yaowu Xuc27fc142016-08-22 16:08:15 -07006702#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006703extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
6704 const unsigned char *img2, int img2_pitch,
6705 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006706
6707static void adjust_image_stat(double y, double u, double v, double all,
6708 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07006709 s->stat[STAT_Y] += y;
6710 s->stat[STAT_U] += u;
6711 s->stat[STAT_V] += v;
6712 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07006713 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006714}
6715
Angie Chiang08a22a62017-07-17 17:29:17 -07006716static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006717 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006718 double samples = 0.0;
Yaowu Xue75b58a2020-01-03 12:56:12 -08006719 const uint32_t in_bit_depth = cpi->oxcf.input_bit_depth;
6720 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006721
Angie Chiang08a22a62017-07-17 17:29:17 -07006722#if CONFIG_INTER_STATS_ONLY
David Turnerd2a592e2018-11-16 14:59:31 +00006723 if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07006724#endif
6725 cpi->bytes += frame_bytes;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006726 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07006727 const YV12_BUFFER_CONFIG *orig = cpi->source;
David Turnerc29e1a92018-12-06 14:10:14 +00006728 const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006729 double y, u, v, frame_all;
6730
6731 cpi->count++;
6732 if (cpi->b_calculate_psnr) {
6733 PSNR_STATS psnr;
6734 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07006735 aom_clear_system_state();
Jerome Jiangfa1d1732019-08-06 10:31:20 -07006736#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xue75b58a2020-01-03 12:56:12 -08006737 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07006738#else
6739 aom_calc_psnr(orig, recon, &psnr);
6740#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006741 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
6742 &cpi->psnr);
6743 cpi->total_sq_error += psnr.sse[0];
6744 cpi->total_samples += psnr.samples[0];
6745 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08006746 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006747 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07006748 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07006749 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006750 else
Yaowu Xuf883b422016-08-30 14:01:10 -07006751 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006752
Yaowu Xuf883b422016-08-30 14:01:10 -07006753 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006754 cpi->summed_quality += frame_ssim2 * weight;
6755 cpi->summed_weights += weight;
6756
6757#if 0
6758 {
6759 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08006760 double y2 = psnr.psnr[1];
6761 double u2 = psnr.psnr[2];
6762 double v2 = psnr.psnr[3];
6763 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07006764 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 +00006765 cm->current_frame.frame_number, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07006766 frame_psnr2, frame_ssim2);
6767 fclose(f);
6768 }
6769#endif
6770 }
6771 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006772 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006773 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07006774 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
6775 recon->y_stride, orig->y_width, orig->y_height);
6776 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006777 cpi->total_blockiness += frame_blockiness;
6778 }
6779
6780 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006781 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006782 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07006783 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
6784 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
6785
6786 const double peak = (double)((1 << in_bit_depth) - 1);
6787 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006788 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006789 if (consistency > 0.0)
6790 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006791 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006792 cpi->total_inconsistency += this_inconsistency;
6793 }
6794 }
6795 }
6796
6797 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07006798 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006799 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07006800 frame_all = aom_psnrhvs(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->psnrhvs);
6802 }
6803}
6804#endif // CONFIG_INTERNAL_STATS
Andrey Norkin795ba872018-03-06 13:24:14 -08006805int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
6806 size_t *size, uint8_t *dest, int64_t *time_stamp,
6807 int64_t *time_end, int flush,
Yue Chen1bc5be62018-08-24 13:57:32 -07006808 const aom_rational64_t *timestamp_ratio) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006809 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
6810 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006811
6812#if CONFIG_BITSTREAM_DEBUG
6813 assert(cpi->oxcf.max_threads == 0 &&
6814 "bitstream debug tool does not support multithreading");
6815 bitstream_queue_record_write();
Yaowu Xu63f2ea32019-04-29 10:47:42 -07006816 aom_bitstream_queue_set_frame_write(cm->current_frame.frame_number * 2 +
6817 cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006818#endif
Marco Paniconi63971322019-08-15 21:32:05 -07006819 if (cpi->use_svc && cm->number_spatial_layers > 1) {
6820 av1_one_pass_cbr_svc_start_layer(cpi);
6821 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006822
Sarah Parker740e8392019-01-23 15:47:53 -08006823 // Indicates whether or not to use an adaptive quantize b rather than
6824 // the traditional version
Urvang Joshi1de67aa2020-03-20 11:21:57 -07006825 cpi->use_quant_b_adapt = cpi->oxcf.quant_b_adapt;
Sarah Parker740e8392019-01-23 15:47:53 -08006826
Dominic Symesd4929012018-01-31 17:32:01 +01006827 cm->showable_frame = 0;
David Turnere43f7fe2019-01-15 14:58:00 +00006828 *size = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006829#if CONFIG_INTERNAL_STATS
6830 struct aom_usec_timer cmptimer;
Yaowu Xuf883b422016-08-30 14:01:10 -07006831 aom_usec_timer_start(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006832#endif
chiyotsaic666b1f2019-12-20 10:44:58 -08006833 av1_set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006834
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07006835 // Normal defaults
Urvang Joshi6237b882020-03-26 15:02:26 -07006836 cm->features.refresh_frame_context = oxcf->frame_parallel_decoding_mode
6837 ? REFRESH_FRAME_CONTEXT_DISABLED
6838 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01006839 if (oxcf->large_scale_tile)
Urvang Joshi6237b882020-03-26 15:02:26 -07006840 cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006841
Sarah Parkerb9041612018-05-22 19:06:47 -07006842 // Initialize fields related to forward keyframes
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07006843 cpi->no_show_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08006844
David Turnerdedd8ff2019-01-23 13:59:46 +00006845 if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006846
Yue Chen1bc5be62018-08-24 13:57:32 -07006847 const int result =
6848 av1_encode_strategy(cpi, size, dest, frame_flags, time_stamp, time_end,
6849 timestamp_ratio, flush);
David Turnerdedd8ff2019-01-23 13:59:46 +00006850 if (result != AOM_CODEC_OK && result != -1) {
David Turner1539bb02019-01-24 15:28:13 +00006851 return AOM_CODEC_ERROR;
David Turnerdedd8ff2019-01-23 13:59:46 +00006852 } else if (result == -1) {
6853 // Returning -1 indicates no frame encoded; more input is required
6854 return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006855 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006856#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006857 aom_usec_timer_mark(&cmptimer);
6858 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yue Chen1bc5be62018-08-24 13:57:32 -07006859#endif // CONFIG_INTERNAL_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00006860 if (cpi->b_calculate_psnr) {
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306861 if (cm->show_existing_frame ||
6862 (!is_stat_generation_stage(cpi) && cm->show_frame)) {
David Turnerc4bf8c72019-01-15 13:14:37 +00006863 generate_psnr_packet(cpi);
6864 }
6865 }
Hui Su8ea87322019-03-29 14:44:32 -07006866
sdeng3cd9eec2020-01-23 15:49:50 -08006867#if CONFIG_TUNE_VMAF
sdeng995c54e2020-02-18 14:51:00 -08006868 if (!is_stat_generation_stage(cpi) &&
6869 (oxcf->tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING ||
6870 oxcf->tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
6871 oxcf->tuning == AOM_TUNE_VMAF_MAX_GAIN)) {
6872 av1_update_vmaf_curve(cpi, cpi->source, &cpi->common.cur_frame->buf);
sdeng3cd9eec2020-01-23 15:49:50 -08006873 }
6874#endif
6875
Vishesh8ac928b2020-04-01 02:36:35 +05306876 if (cpi->level_params.keep_level_stats && !is_stat_generation_stage(cpi)) {
Hui Su8ea87322019-03-29 14:44:32 -07006877 // Initialize level info. at the beginning of each sequence.
6878 if (cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) {
Hui Su58753d62019-05-29 09:56:19 -07006879 av1_init_level_info(cpi);
Hui Su8ea87322019-03-29 14:44:32 -07006880 }
kyslovabeeb7c2019-03-06 18:35:04 -08006881 av1_update_level_info(cpi, *size, *time_stamp, *time_end);
Hui Su8ea87322019-03-29 14:44:32 -07006882 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006883
6884#if CONFIG_INTERNAL_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306885 if (!is_stat_generation_stage(cpi)) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006886 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006887 }
6888#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08006889#if CONFIG_SPEED_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306890 if (!is_stat_generation_stage(cpi) && !cm->show_existing_frame) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08006891 cpi->tx_search_count += cpi->td.mb.tx_search_count;
6892 cpi->td.mb.tx_search_count = 0;
6893 }
6894#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006895
Yaowu Xuf883b422016-08-30 14:01:10 -07006896 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006897
Hui Su0d0ee662019-07-29 14:38:36 -07006898 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006899}
6900
Yaowu Xuf883b422016-08-30 14:01:10 -07006901int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6902 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006903 if (!cm->show_frame) {
6904 return -1;
6905 } else {
6906 int ret;
David Turnerc29e1a92018-12-06 14:10:14 +00006907 if (cm->cur_frame != NULL) {
6908 *dest = cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006909 dest->y_width = cm->width;
6910 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006911 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
6912 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006913 ret = 0;
6914 } else {
6915 ret = -1;
6916 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006917 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006918 return ret;
6919 }
6920}
6921
Yaowu Xuf883b422016-08-30 14:01:10 -07006922int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
David Turnere7ebf902018-12-04 14:04:55 +00006923 if (cpi->last_show_frame_buf == NULL) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006924
David Turnere7ebf902018-12-04 14:04:55 +00006925 *frame = cpi->last_show_frame_buf->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006926 return 0;
6927}
6928
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006929static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
6930 const YV12_BUFFER_CONFIG *b) {
6931 return a->y_height == b->y_height && a->y_width == b->y_width &&
6932 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
6933 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
6934 a->border == b->border &&
6935 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
6936 (b->flags & YV12_FLAG_HIGHBITDEPTH);
6937}
6938
Yunqing Wang93b18f32018-06-08 21:08:29 -07006939aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
6940 YV12_BUFFER_CONFIG *new_frame,
6941 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006942 const int num_planes = av1_num_planes(cm);
6943 if (!equal_dimensions_and_border(new_frame, sd))
6944 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
6945 "Incorrect buffer dimensions");
6946 else
6947 aom_yv12_copy_frame(new_frame, sd, num_planes);
6948
6949 return cm->error.error_code;
6950}
6951
Yaowu Xuf883b422016-08-30 14:01:10 -07006952int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6953 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006954 int hr = 0, hs = 0, vr = 0, vs = 0;
6955
6956 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6957
6958 Scale2Ratio(horiz_mode, &hr, &hs);
6959 Scale2Ratio(vert_mode, &vr, &vs);
6960
6961 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07006962 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6963 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006964
6965 return 0;
6966}
6967
Urvang Joshi17814622020-03-27 17:26:17 -07006968int av1_get_quantizer(AV1_COMP *cpi) {
6969 return cpi->common.quant_params.base_qindex;
6970}
Yaowu Xuc27fc142016-08-22 16:08:15 -07006971
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006972int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
6973 size_t output_size = 0;
6974 size_t total_bytes_read = 0;
6975 size_t remaining_size = *frame_size;
6976 uint8_t *buff_ptr = buffer;
6977
6978 // go through each OBUs
6979 while (total_bytes_read < *frame_size) {
6980 uint8_t saved_obu_header[2];
6981 uint64_t obu_payload_size;
6982 size_t length_of_payload_size;
6983 size_t length_of_obu_size;
6984 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
6985 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
6986
6987 // save the obu header (1 or 2 bytes)
6988 memmove(saved_obu_header, buff_ptr, obu_header_size);
6989 // clear the obu_has_size_field
6990 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
6991
6992 // get the payload_size and length of payload_size
6993 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
6994 &obu_payload_size, &length_of_payload_size) != 0) {
6995 return AOM_CODEC_ERROR;
6996 }
6997 obu_bytes_read += length_of_payload_size;
6998
6999 // calculate the length of size of the obu header plus payload
7000 length_of_obu_size =
7001 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
7002
7003 // move the rest of data to new location
7004 memmove(buff_ptr + length_of_obu_size + obu_header_size,
7005 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07007006 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04007007
7008 // write the new obu size
7009 const uint64_t obu_size = obu_header_size + obu_payload_size;
7010 size_t coded_obu_size;
7011 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
7012 &coded_obu_size) != 0) {
7013 return AOM_CODEC_ERROR;
7014 }
7015
7016 // write the saved (modified) obu_header following obu size
7017 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
7018
7019 total_bytes_read += obu_bytes_read;
7020 remaining_size -= obu_bytes_read;
7021 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07007022 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04007023 }
7024
7025 *frame_size = output_size;
7026 return AOM_CODEC_OK;
7027}
7028
Marco Paniconid8574e32019-08-04 21:30:12 -07007029static void svc_set_updates_external_ref_frame_config(AV1_COMP *cpi) {
7030 cpi->ext_refresh_frame_flags_pending = 1;
7031 cpi->ext_refresh_last_frame = cpi->svc.refresh[cpi->svc.ref_idx[0]];
7032 cpi->ext_refresh_golden_frame = cpi->svc.refresh[cpi->svc.ref_idx[3]];
7033 cpi->ext_refresh_bwd_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[4]];
7034 cpi->ext_refresh_alt2_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[5]];
7035 cpi->ext_refresh_alt_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[6]];
7036 cpi->svc.non_reference_frame = 1;
7037 for (int i = 0; i < REF_FRAMES; i++) {
7038 if (cpi->svc.refresh[i] == 1) {
7039 cpi->svc.non_reference_frame = 0;
7040 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.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08007061 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007062 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007063 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
7064 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
7065 AOM_EFLAG_NO_REF_ARF2)) {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07007066 int ref = AOM_REFFRAME_ALL;
7067
7068 if (flags & AOM_EFLAG_NO_REF_LAST) ref ^= AOM_LAST_FLAG;
7069 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
7070 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
7071
7072 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
7073
7074 if (flags & AOM_EFLAG_NO_REF_ARF) {
7075 ref ^= AOM_ALT_FLAG;
7076 ref ^= AOM_BWD_FLAG;
7077 ref ^= AOM_ALT2_FLAG;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007078 } else {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07007079 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
7080 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007081 }
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07007082
7083 av1_use_as_reference(cpi, ref);
Marco Paniconiad4953a2020-04-02 09:52:58 -07007084 } else {
7085 if (cpi->svc.external_ref_frame_config) {
7086 int ref = svc_set_references_external_ref_frame_config(cpi);
7087 av1_use_as_reference(cpi, ref);
7088 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07007089 }
7090
7091 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007092 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07007093 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007094
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007095 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
7096 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007097
Yaowu Xuf883b422016-08-30 14:01:10 -07007098 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007099
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007100 if (flags & AOM_EFLAG_NO_UPD_ARF) {
7101 upd ^= AOM_ALT_FLAG;
7102 upd ^= AOM_BWD_FLAG;
7103 upd ^= AOM_ALT2_FLAG;
7104 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07007105
David Turnerce9b5902019-01-23 17:25:47 +00007106 cpi->ext_refresh_last_frame = (upd & AOM_LAST_FLAG) != 0;
7107 cpi->ext_refresh_golden_frame = (upd & AOM_GOLD_FLAG) != 0;
7108 cpi->ext_refresh_alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
7109 cpi->ext_refresh_bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
7110 cpi->ext_refresh_alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
7111 cpi->ext_refresh_frame_flags_pending = 1;
David Turner4f1f1812019-01-24 17:00:24 +00007112 } else {
Marco Paniconid8574e32019-08-04 21:30:12 -07007113 if (cpi->svc.external_ref_frame_config)
7114 svc_set_updates_external_ref_frame_config(cpi);
7115 else
7116 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007117 }
7118
sarahparker21dbca42018-03-30 17:43:44 -07007119 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
7120 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
sarahparker27d686a2018-03-30 17:43:44 -07007121 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
7122 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
sarahparker9806fed2018-03-30 17:43:44 -07007123 cpi->ext_use_s_frame =
7124 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Sarah Parker50b6d6e2018-04-11 19:21:54 -07007125 cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07007126
Yaowu Xuf883b422016-08-30 14:01:10 -07007127 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
7128 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007129 }
7130}
Andrey Norkin795ba872018-03-06 13:24:14 -08007131
Tom Fineganf8d6a162018-08-21 10:47:55 -07007132aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
7133 if (!cpi) return NULL;
7134
7135 uint8_t header_buf[512] = { 0 };
7136 const uint32_t sequence_header_size =
Urvang Joshi450a9a22020-03-31 16:00:22 -07007137 av1_write_sequence_header_obu(&cpi->common.seq_params, &header_buf[0]);
Tom Fineganf8d6a162018-08-21 10:47:55 -07007138 assert(sequence_header_size <= sizeof(header_buf));
7139 if (sequence_header_size == 0) return NULL;
7140
7141 const size_t obu_header_size = 1;
7142 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
7143 const size_t payload_offset = obu_header_size + size_field_size;
7144
7145 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
7146 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
7147
Vishesh8ac928b2020-04-01 02:36:35 +05307148 if (av1_write_obu_header(&cpi->level_params, OBU_SEQUENCE_HEADER, 0,
7149 &header_buf[0]) != obu_header_size) {
Tom Fineganf8d6a162018-08-21 10:47:55 -07007150 return NULL;
7151 }
7152
7153 size_t coded_size_field_size = 0;
7154 if (aom_uleb_encode(sequence_header_size, size_field_size,
7155 &header_buf[obu_header_size],
7156 &coded_size_field_size) != 0) {
7157 return NULL;
7158 }
7159 assert(coded_size_field_size == size_field_size);
7160
7161 aom_fixed_buf_t *global_headers =
7162 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
7163 if (!global_headers) return NULL;
7164
7165 const size_t global_header_buf_size =
7166 obu_header_size + size_field_size + sequence_header_size;
7167
7168 global_headers->buf = malloc(global_header_buf_size);
7169 if (!global_headers->buf) {
7170 free(global_headers);
7171 return NULL;
7172 }
7173
7174 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
7175 global_headers->sz = global_header_buf_size;
7176 return global_headers;
7177}