1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * 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.
10 */
11
12 #include <limits.h>
13 #include <float.h>
14 #include <math.h>
15 #include <stdio.h>
16
17 #include "config/aom_config.h"
18 #include "config/aom_dsp_rtcd.h"
19 #include "config/aom_scale_rtcd.h"
20 #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"
37 #if CONFIG_BITSTREAM_DEBUG
38 #include "aom_util/debug_util.h"
39 #endif // CONFIG_BITSTREAM_DEBUG
40
41 #include "av1/common/alloccommon.h"
42 #include "av1/common/cdef.h"
43 #include "av1/common/filter.h"
44 #include "av1/common/idct.h"
45 #include "av1/common/reconinter.h"
46 #include "av1/common/reconintra.h"
47 #include "av1/common/resize.h"
48 #include "av1/common/tile_common.h"
49
50 #include "av1/encoder/av1_multi_thread.h"
51 #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"
55 #include "av1/encoder/context_tree.h"
56 #include "av1/encoder/encodeframe.h"
57 #include "av1/encoder/encodemv.h"
58 #include "av1/encoder/encode_strategy.h"
59 #include "av1/encoder/encoder.h"
60 #include "av1/encoder/encodetxb.h"
61 #include "av1/encoder/ethread.h"
62 #include "av1/encoder/firstpass.h"
63 #include "av1/encoder/grain_test_vectors.h"
64 #include "av1/encoder/hash_motion.h"
65 #include "av1/encoder/mv_prec.h"
66 #include "av1/encoder/pass2_strategy.h"
67 #include "av1/encoder/picklpf.h"
68 #include "av1/encoder/pickrst.h"
69 #include "av1/encoder/random.h"
70 #include "av1/encoder/ratectrl.h"
71 #include "av1/encoder/rd.h"
72 #include "av1/encoder/rdopt.h"
73 #include "av1/encoder/segmentation.h"
74 #include "av1/encoder/speed_features.h"
75 #include "av1/encoder/tpl_model.h"
76 #include "av1/encoder/reconinter_enc.h"
77 #include "av1/encoder/var_based_part.h"
78
79 #if CONFIG_TUNE_VMAF
80 #include "av1/encoder/tune_vmaf.h"
81 #endif
82
83 #define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
84
85 #if CONFIG_ENTROPY_STATS
86 FRAME_COUNTS aggregate_fc;
87 #endif // CONFIG_ENTROPY_STATS
88
89 #define AM_SEGMENT_ID_INACTIVE 7
90 #define AM_SEGMENT_ID_ACTIVE 0
91
92 // #define OUTPUT_YUV_REC
93 #ifdef OUTPUT_YUV_SKINMAP
94 FILE *yuv_skinmap_file = NULL;
95 #endif
96 #ifdef OUTPUT_YUV_REC
97 FILE *yuv_rec_file;
98 #define FILE_NAME_LEN 100
99 #endif
100
101 const 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
237 const 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
251 const int default_warped_probs[FRAME_UPDATE_TYPES] = { 64, 64, 64, 64,
252 64, 64, 64 };
253
254 // TODO(yunqing): the default probs can be trained later from better
255 // performance.
256 const 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
Scale2Ratio(AOM_SCALING mode,int * hr,int * hs)373 static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
374 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.
suppress_active_map(AV1_COMP * cpi)401 static void suppress_active_map(AV1_COMP *cpi) {
402 unsigned char *const seg_map = cpi->enc_seg.map;
403 int i;
404 if (cpi->active_map.enabled || cpi->active_map.update)
405 for (i = 0;
406 i < cpi->common.mi_params.mi_rows * cpi->common.mi_params.mi_cols; ++i)
407 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
408 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
409 }
410
apply_active_map(AV1_COMP * cpi)411 static void apply_active_map(AV1_COMP *cpi) {
412 struct segmentation *const seg = &cpi->common.seg;
413 unsigned char *const seg_map = cpi->enc_seg.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) {
426 for (i = 0;
427 i < cpi->common.mi_params.mi_rows * cpi->common.mi_params.mi_cols;
428 ++i)
429 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
430 av1_enable_segmentation(seg);
431 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
432 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);
445 } else {
446 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
447 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);
451 if (seg->enabled) {
452 seg->update_data = 1;
453 seg->update_map = 1;
454 }
455 }
456 cpi->active_map.update = 0;
457 }
458 }
459
av1_set_active_map(AV1_COMP * cpi,unsigned char * new_map_16x16,int rows,int cols)460 int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
461 int cols) {
462 const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
463 if (rows == mi_params->mb_rows && cols == mi_params->mb_cols) {
464 unsigned char *const active_map_8x8 = cpi->active_map.map;
465 const int mi_rows = mi_params->mi_rows;
466 const int mi_cols = mi_params->mi_cols;
467 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;
469 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] =
475 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
476 ? 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
av1_get_active_map(AV1_COMP * cpi,unsigned char * new_map_16x16,int rows,int cols)490 int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
491 int cols) {
492 const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
493 if (rows == mi_params->mb_rows && cols == mi_params->mb_cols &&
494 new_map_16x16) {
495 unsigned char *const seg_map_8x8 = cpi->enc_seg.map;
496 const int mi_rows = mi_params->mi_rows;
497 const int mi_cols = mi_params->mi_cols;
498 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
501 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
508 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
509 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
519 // 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.
analyze_hor_freq(const AV1_COMP * cpi,double * energy)522 static void analyze_hor_freq(const AV1_COMP *cpi, double *energy) {
523 uint64_t freq_energy[16] = { 0 };
524 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;
528 DECLARE_ALIGNED(16, int32_t, coeff[16 * 4]);
529 int n = 0;
530 memset(freq_energy, 0, sizeof(freq_energy));
531 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);
537 for (int k = 1; k < 16; ++k) {
538 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]);
543 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2 + 2 * (bd - 8));
544 }
545 n++;
546 }
547 }
548 } else {
549 assert(bd == 8);
550 DECLARE_ALIGNED(16, int16_t, src16[16 * 4]);
551 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)];
557 av1_fwd_txfm2d_16x4(src16, coeff, 16, H_DCT, bd);
558 for (int k = 1; k < 16; ++k) {
559 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]);
564 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2);
565 }
566 n++;
567 }
568 }
569 }
570 if (n) {
571 for (int k = 1; k < 16; ++k) energy[k] = (double)freq_energy[k] / n;
572 // Convert to cumulative energy
573 for (int k = 14; k > 0; --k) energy[k] += energy[k + 1];
574 } else {
575 for (int k = 1; k < 16; ++k) energy[k] = 1e+20;
576 }
577 }
578
select_sb_size(const AV1_COMP * const cpi)579 static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
580 const AV1_COMMON *const cm = &cpi->common;
581
582 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
583 return BLOCK_64X64;
584 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
585 return BLOCK_128X128;
586
587 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
588
589 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
595 // TODO(any): Possibly could improve this with a heuristic.
596 // When superres / resize is on, 'cm->width / height' can change between
597 // 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.
601 if (cpi->oxcf.superres_mode == SUPERRES_NONE &&
602 cpi->oxcf.resize_mode == RESIZE_NONE && cpi->oxcf.speed >= 1) {
603 return AOMMIN(cm->width, cm->height) > 480 ? BLOCK_128X128 : BLOCK_64X64;
604 }
605
606 return BLOCK_128X128;
607 }
608
setup_frame(AV1_COMP * cpi)609 static void setup_frame(AV1_COMP *cpi) {
610 AV1_COMMON *const cm = &cpi->common;
611 // 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.
616
617 if (frame_is_intra_only(cm) || cm->features.error_resilient_mode ||
618 cpi->ext_flags.use_primary_ref_none) {
619 av1_setup_past_independence(cm);
620 }
621
622 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 }
627 } else {
628 const RefCntBuffer *const primary_ref_buf = get_primary_ref_frame_buf(cm);
629 if (primary_ref_buf == NULL) {
630 av1_setup_past_independence(cm);
631 cm->seg.update_map = 1;
632 cm->seg.update_data = 1;
633 } else {
634 *cm->fc = primary_ref_buf->frame_context;
635 }
636 }
637
638 av1_zero(cm->cur_frame->interp_filter_selected);
639 cm->prev_frame = get_primary_ref_frame_buf(cm);
640 cpi->vaq_refresh = 0;
641 }
642
set_mb_mi(CommonModeInfoParams * mi_params,int width,int height)643 static void set_mb_mi(CommonModeInfoParams *mi_params, int width, int height) {
644 // 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);
651
652 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);
655
656 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;
659
660 const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
661 mi_params->mi_alloc_stride =
662 (mi_params->mi_stride + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
663
664 assert(mi_size_wide[mi_params->mi_alloc_bsize] ==
665 mi_size_high[mi_params->mi_alloc_bsize]);
666
667 #if CONFIG_LPF_MASK
668 av1_alloc_loop_filter_mask(mi_params);
669 #endif
670 }
671
enc_set_mb_mi(CommonModeInfoParams * mi_params,int width,int height)672 static 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
stat_stage_set_mb_mi(CommonModeInfoParams * mi_params,int width,int height)680 static 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
enc_setup_mi(CommonModeInfoParams * mi_params)687 static 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);
690 memset(mi_params->mi_alloc, 0,
691 mi_params->mi_alloc_size * sizeof(*mi_params->mi_alloc));
692 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));
696 }
697
enc_free_mi(CommonModeInfoParams * mi_params)698 static void enc_free_mi(CommonModeInfoParams *mi_params) {
699 aom_free(mi_params->mi_alloc);
700 mi_params->mi_alloc = NULL;
701 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;
706 }
707
av1_initialize_enc(void)708 void av1_initialize_enc(void) {
709 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();
716 }
717
dealloc_context_buffers_ext(MBMIExtFrameBufferInfo * mbmi_ext_info)718 static void dealloc_context_buffers_ext(MBMIExtFrameBufferInfo *mbmi_ext_info) {
719 if (mbmi_ext_info->frame_base) {
720 aom_free(mbmi_ext_info->frame_base);
721 mbmi_ext_info->frame_base = NULL;
722 mbmi_ext_info->alloc_size = 0;
723 }
724 }
725
alloc_context_buffers_ext(AV1_COMMON * cm,MBMIExtFrameBufferInfo * mbmi_ext_info)726 static void alloc_context_buffers_ext(AV1_COMMON *cm,
727 MBMIExtFrameBufferInfo *mbmi_ext_info) {
728 const CommonModeInfoParams *const mi_params = &cm->mi_params;
729
730 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 > mbmi_ext_info->alloc_size) {
738 dealloc_context_buffers_ext(mbmi_ext_info);
739 CHECK_MEM_ERROR(
740 cm, mbmi_ext_info->frame_base,
741 aom_calloc(new_ext_mi_size, sizeof(*mbmi_ext_info->frame_base)));
742 mbmi_ext_info->alloc_size = new_ext_mi_size;
743 }
744 // The stride needs to be updated regardless of whether new allocation
745 // happened or not.
746 mbmi_ext_info->stride = mi_alloc_cols;
747 }
748
reset_film_grain_chroma_params(aom_film_grain_t * pars)749 static 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;
758 pars->chroma_scaling_from_luma = 0;
759 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
update_film_grain_parameters(struct AV1_COMP * cpi,const AV1EncoderConfig * oxcf)763 static 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
768 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;
772 }
773
774 if (oxcf->film_grain_test_vector) {
775 cm->seq_params.film_grain_params_present = 1;
776 if (cm->current_frame.frame_type == KEY_FRAME) {
777 memcpy(&cm->film_grain_params,
778 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
779 sizeof(cm->film_grain_params));
780 if (oxcf->monochrome)
781 reset_film_grain_chroma_params(&cm->film_grain_params);
782 cm->film_grain_params.bit_depth = cm->seq_params.bit_depth;
783 if (cm->seq_params.color_range == AOM_CR_FULL_RANGE) {
784 cm->film_grain_params.clip_to_restricted_range = 0;
785 }
786 }
787 } else if (oxcf->film_grain_table_filename) {
788 cm->seq_params.film_grain_params_present = 1;
789
790 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
791 memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
792
793 aom_film_grain_table_read(cpi->film_grain_table,
794 oxcf->film_grain_table_filename, &cm->error);
795 } else {
796 #if CONFIG_DENOISE
797 cm->seq_params.film_grain_params_present = (cpi->oxcf.noise_level > 0);
798 #else
799 cm->seq_params.film_grain_params_present = 0;
800 #endif
801 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
802 }
803 }
804
dealloc_compressor_data(AV1_COMP * cpi)805 static void dealloc_compressor_data(AV1_COMP *cpi) {
806 AV1_COMMON *const cm = &cpi->common;
807 const int num_planes = av1_num_planes(cm);
808
809 dealloc_context_buffers_ext(&cpi->mbmi_ext_info);
810
811 aom_free(cpi->tile_data);
812 cpi->tile_data = NULL;
813
814 // Delete sementation map
815 aom_free(cpi->enc_seg.map);
816 cpi->enc_seg.map = NULL;
817
818 av1_cyclic_refresh_free(cpi->cyclic_refresh);
819 cpi->cyclic_refresh = NULL;
820
821 aom_free(cpi->active_map.map);
822 cpi->active_map.map = NULL;
823
824 aom_free(cpi->ssim_rdmult_scaling_factors);
825 cpi->ssim_rdmult_scaling_factors = NULL;
826
827 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
833 #if CONFIG_TUNE_VMAF
834 aom_free(cpi->vmaf_rdmult_scaling_factors);
835 cpi->vmaf_rdmult_scaling_factors = NULL;
836 #endif
837
838 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
847 aom_free(cpi->td.mb.inter_modes_info);
848 cpi->td.mb.inter_modes_info = NULL;
849
850 for (int i = 0; i < 2; i++)
851 for (int j = 0; j < 2; j++) {
852 aom_free(cpi->td.mb.intrabc_hash_info.hash_value_buffer[i][j]);
853 cpi->td.mb.intrabc_hash_info.hash_value_buffer[i][j] = NULL;
854 }
855 aom_free(cpi->td.mb.mask_buf);
856 cpi->td.mb.mask_buf = NULL;
857
858 aom_free(cm->tpl_mvs);
859 cm->tpl_mvs = NULL;
860
861 aom_free(cpi->td.mb.mbmi_ext);
862 cpi->td.mb.mbmi_ext = NULL;
863
864 if (cpi->td.vt64x64) {
865 aom_free(cpi->td.vt64x64);
866 cpi->td.vt64x64 = NULL;
867 }
868
869 av1_free_ref_frame_buffers(cm->buffer_pool);
870 av1_free_txb_buf(cpi);
871 av1_free_context_buffers(cm);
872
873 aom_free_frame_buffer(&cpi->last_frame_uf);
874 av1_free_restoration_buffers(cm);
875 aom_free_frame_buffer(&cpi->trial_frame_rst);
876 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);
880
881 aom_free(cpi->tile_tok[0][0]);
882 cpi->tile_tok[0][0] = 0;
883
884 aom_free(cpi->tplist[0][0]);
885 cpi->tplist[0][0] = NULL;
886
887 av1_free_pc_tree(cpi, &cpi->td, num_planes, cm->seq_params.sb_size);
888
889 aom_free(cpi->td.mb.palette_buffer);
890 av1_release_compound_type_rd_buffers(&cpi->td.mb.comp_rd_buffer);
891 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
896 #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 }
906
907 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
908 aom_free(cpi->level_params.level_info[i]);
909 }
910
911 if (cpi->use_svc) av1_free_svc_cyclic_refresh(cpi);
912 }
913
configure_static_seg_features(AV1_COMP * cpi)914 static void configure_static_seg_features(AV1_COMP *cpi) {
915 AV1_COMMON *const cm = &cpi->common;
916 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
923 if (cm->current_frame.frame_type == KEY_FRAME) {
924 // Clear down the global segmentation map
925 memset(cpi->enc_seg.map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols);
926 seg->update_map = 0;
927 seg->update_data = 0;
928
929 // Disable segmentation
930 av1_disable_segmentation(seg);
931
932 // Clear down the segment features.
933 av1_clearall_segfeatures(seg);
934 } else if (cpi->refresh_alt_ref_frame) {
935 // If this is an alt ref frame
936 // Clear down the global segmentation map
937 memset(cpi->enc_seg.map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols);
938 seg->update_map = 0;
939 seg->update_data = 0;
940
941 // Disable segmentation and individual segment features by default
942 av1_disable_segmentation(seg);
943 av1_clearall_segfeatures(seg);
944
945 // If segmentation was enabled set those features needed for the
946 // arf itself.
947 if (seg->enabled) {
948 seg->update_map = 1;
949 seg->update_data = 1;
950
951 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
952 cm->seq_params.bit_depth);
953 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
954 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
955 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
956 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
957 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
958
959 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
960 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
961 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
962 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
963
964 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
965 }
966 } else if (seg->enabled) {
967 // All other frames if segmentation has been enabled
968
969 // First normal frame in a valid gf or alt ref group
970 if (rc->frames_since_golden == 0) {
971 // Set up segment features for normal frames in an arf group
972 if (rc->source_alt_ref_active) {
973 seg->update_map = 0;
974 seg->update_data = 1;
975
976 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
977 cm->seq_params.bit_depth);
978 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
979 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
980
981 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
982 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
983 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
984 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
985
986 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
987 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
988 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
989 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
990
991 // Segment coding disabled for compred testing
992 if (high_q) {
993 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
994 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
995 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
996 }
997 } else {
998 // Disable segmentation and clear down features if alt ref
999 // is not active for this group
1000
1001 av1_disable_segmentation(seg);
1002
1003 memset(cpi->enc_seg.map, 0,
1004 cm->mi_params.mi_rows * cm->mi_params.mi_cols);
1005
1006 seg->update_map = 0;
1007 seg->update_data = 0;
1008
1009 av1_clearall_segfeatures(seg);
1010 }
1011 } else if (rc->is_src_frame_alt_ref) {
1012 // Special case where we are coding over the top of a previous
1013 // alt ref frame.
1014 // Segment coding disabled for compred testing
1015
1016 // Enable ref frame features for segment 0 as well
1017 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
1018 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
1019
1020 // All mbs should use ALTREF_FRAME
1021 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
1022 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
1023 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
1024 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
1025
1026 // Skip all MBs if high Q (0,0 mv and skip coeffs)
1027 if (high_q) {
1028 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
1029 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
1030 }
1031 // Enable data update
1032 seg->update_data = 1;
1033 } else {
1034 // All other frames.
1035
1036 // No updates.. leave things as they are.
1037 seg->update_map = 0;
1038 seg->update_data = 0;
1039 }
1040 }
1041 }
1042
update_reference_segmentation_map(AV1_COMP * cpi)1043 static void update_reference_segmentation_map(AV1_COMP *cpi) {
1044 AV1_COMMON *const cm = &cpi->common;
1045 const CommonModeInfoParams *const mi_params = &cm->mi_params;
1046 MB_MODE_INFO **mi_4x4_ptr = mi_params->mi_grid_base;
1047 uint8_t *cache_ptr = cm->cur_frame->seg_map;
1048
1049 for (int row = 0; row < mi_params->mi_rows; row++) {
1050 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
1051 uint8_t *cache = cache_ptr;
1052 for (int col = 0; col < mi_params->mi_cols; col++, mi_4x4++, cache++)
1053 cache[0] = mi_4x4[0]->segment_id;
1054 mi_4x4_ptr += mi_params->mi_stride;
1055 cache_ptr += mi_params->mi_cols;
1056 }
1057 }
1058
alloc_altref_frame_buffer(AV1_COMP * cpi)1059 static void alloc_altref_frame_buffer(AV1_COMP *cpi) {
1060 AV1_COMMON *cm = &cpi->common;
1061 const SequenceHeader *const seq_params = &cm->seq_params;
1062 const AV1EncoderConfig *oxcf = &cpi->oxcf;
1063
1064 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
1065 if (aom_realloc_frame_buffer(
1066 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
1067 seq_params->subsampling_x, seq_params->subsampling_y,
1068 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
1069 cm->features.byte_alignment, NULL, NULL, NULL))
1070 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1071 "Failed to allocate altref buffer");
1072 }
1073
alloc_util_frame_buffers(AV1_COMP * cpi)1074 static void alloc_util_frame_buffers(AV1_COMP *cpi) {
1075 AV1_COMMON *const cm = &cpi->common;
1076 const SequenceHeader *const seq_params = &cm->seq_params;
1077 const int byte_alignment = cm->features.byte_alignment;
1078 if (aom_realloc_frame_buffer(
1079 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
1080 seq_params->subsampling_y, seq_params->use_highbitdepth,
1081 cpi->oxcf.border_in_pixels, byte_alignment, NULL, NULL, NULL))
1082 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1083 "Failed to allocate last frame buffer");
1084
1085 if (aom_realloc_frame_buffer(
1086 &cpi->trial_frame_rst, cm->superres_upscaled_width,
1087 cm->superres_upscaled_height, seq_params->subsampling_x,
1088 seq_params->subsampling_y, seq_params->use_highbitdepth,
1089 AOM_RESTORATION_FRAME_BORDER, byte_alignment, NULL, NULL, NULL))
1090 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1091 "Failed to allocate trial restored frame buffer");
1092
1093 if (aom_realloc_frame_buffer(
1094 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
1095 seq_params->subsampling_y, seq_params->use_highbitdepth,
1096 cpi->oxcf.border_in_pixels, byte_alignment, NULL, NULL, NULL))
1097 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1098 "Failed to allocate scaled source buffer");
1099
1100 if (aom_realloc_frame_buffer(
1101 &cpi->scaled_last_source, cm->width, cm->height,
1102 seq_params->subsampling_x, seq_params->subsampling_y,
1103 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
1104 byte_alignment, NULL, NULL, NULL))
1105 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1106 "Failed to allocate scaled last source buffer");
1107 }
1108
alloc_compressor_data(AV1_COMP * cpi)1109 static void alloc_compressor_data(AV1_COMP *cpi) {
1110 AV1_COMMON *cm = &cpi->common;
1111 const int num_planes = av1_num_planes(cm);
1112
1113 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
1114 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1115 "Failed to allocate context buffers");
1116 }
1117
1118 int mi_rows_aligned_to_sb =
1119 ALIGN_POWER_OF_TWO(cm->mi_params.mi_rows, cm->seq_params.mib_size_log2);
1120 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
1121
1122 if (!is_stat_generation_stage(cpi)) {
1123 av1_alloc_txb_buf(cpi);
1124
1125 alloc_context_buffers_ext(cm, &cpi->mbmi_ext_info);
1126 }
1127
1128 aom_free(cpi->tile_tok[0][0]);
1129 aom_free(cpi->tplist[0][0]);
1130
1131 if (!is_stat_generation_stage(cpi)) {
1132 unsigned int tokens =
1133 get_token_alloc(cm->mi_params.mb_rows, cm->mi_params.mb_cols,
1134 MAX_SB_SIZE_LOG2, num_planes);
1135 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
1136 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
1137
1138 CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
1139 aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
1140 sizeof(*cpi->tplist[0][0])));
1141 }
1142
1143 av1_setup_pc_tree(cpi, &cpi->td);
1144 }
1145
av1_new_framerate(AV1_COMP * cpi,double framerate)1146 void av1_new_framerate(AV1_COMP *cpi, double framerate) {
1147 cpi->framerate = framerate < 0.1 ? 30 : framerate;
1148 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
1149 }
1150
av1_get_compression_ratio(const AV1_COMMON * const cm,size_t encoded_frame_size)1151 double av1_get_compression_ratio(const AV1_COMMON *const cm,
1152 size_t encoded_frame_size) {
1153 const int upscaled_width = cm->superres_upscaled_width;
1154 const int height = cm->height;
1155 const int luma_pic_size = upscaled_width * height;
1156 const SequenceHeader *const seq_params = &cm->seq_params;
1157 const BITSTREAM_PROFILE profile = seq_params->profile;
1158 const int pic_size_profile_factor =
1159 profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
1160 encoded_frame_size =
1161 (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
1162 const size_t uncompressed_frame_size =
1163 (luma_pic_size * pic_size_profile_factor) >> 3;
1164 return uncompressed_frame_size / (double)encoded_frame_size;
1165 }
1166
set_tile_info(AV1_COMP * cpi)1167 static void set_tile_info(AV1_COMP *cpi) {
1168 AV1_COMMON *const cm = &cpi->common;
1169 const CommonModeInfoParams *const mi_params = &cm->mi_params;
1170 const SequenceHeader *const seq_params = &cm->seq_params;
1171 CommonTileParams *const tiles = &cm->tiles;
1172 int i, start_sb;
1173
1174 av1_get_tile_limits(cm);
1175
1176 // configure tile columns
1177 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
1178 tiles->uniform_spacing = 1;
1179 tiles->log2_cols = AOMMAX(cpi->oxcf.tile_columns, tiles->min_log2_cols);
1180 tiles->log2_cols = AOMMIN(tiles->log2_cols, tiles->max_log2_cols);
1181 } else {
1182 int mi_cols =
1183 ALIGN_POWER_OF_TWO(mi_params->mi_cols, seq_params->mib_size_log2);
1184 int sb_cols = mi_cols >> seq_params->mib_size_log2;
1185 int size_sb, j = 0;
1186 tiles->uniform_spacing = 0;
1187 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
1188 tiles->col_start_sb[i] = start_sb;
1189 size_sb = cpi->oxcf.tile_widths[j++];
1190 if (j >= cpi->oxcf.tile_width_count) j = 0;
1191 start_sb += AOMMIN(size_sb, tiles->max_width_sb);
1192 }
1193 tiles->cols = i;
1194 tiles->col_start_sb[i] = sb_cols;
1195 }
1196 av1_calculate_tile_cols(seq_params, mi_params->mi_rows, mi_params->mi_cols,
1197 tiles);
1198
1199 // configure tile rows
1200 if (tiles->uniform_spacing) {
1201 tiles->log2_rows = AOMMAX(cpi->oxcf.tile_rows, tiles->min_log2_rows);
1202 tiles->log2_rows = AOMMIN(tiles->log2_rows, tiles->max_log2_rows);
1203 } else {
1204 int mi_rows =
1205 ALIGN_POWER_OF_TWO(mi_params->mi_rows, seq_params->mib_size_log2);
1206 int sb_rows = mi_rows >> seq_params->mib_size_log2;
1207 int size_sb, j = 0;
1208 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
1209 tiles->row_start_sb[i] = start_sb;
1210 size_sb = cpi->oxcf.tile_heights[j++];
1211 if (j >= cpi->oxcf.tile_height_count) j = 0;
1212 start_sb += AOMMIN(size_sb, tiles->max_height_sb);
1213 }
1214 tiles->rows = i;
1215 tiles->row_start_sb[i] = sb_rows;
1216 }
1217 av1_calculate_tile_rows(seq_params, mi_params->mi_rows, tiles);
1218 }
1219
update_frame_size(AV1_COMP * cpi)1220 static void update_frame_size(AV1_COMP *cpi) {
1221 AV1_COMMON *const cm = &cpi->common;
1222 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
1223
1224 // We need to reallocate the context buffers here in case we need more mis.
1225 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
1226 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1227 "Failed to allocate context buffers");
1228 }
1229 av1_init_mi_buffers(&cm->mi_params);
1230
1231 av1_init_macroblockd(cm, xd, NULL);
1232
1233 if (!is_stat_generation_stage(cpi))
1234 alloc_context_buffers_ext(cm, &cpi->mbmi_ext_info);
1235 set_tile_info(cpi);
1236 }
1237
init_buffer_indices(ForceIntegerMVInfo * const force_intpel_info,int * const remapped_ref_idx)1238 static void init_buffer_indices(ForceIntegerMVInfo *const force_intpel_info,
1239 int *const remapped_ref_idx) {
1240 int fb_idx;
1241 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
1242 remapped_ref_idx[fb_idx] = fb_idx;
1243 force_intpel_info->rate_index = 0;
1244 force_intpel_info->rate_size = 0;
1245 }
1246
does_level_match(int width,int height,double fps,int lvl_width,int lvl_height,double lvl_fps,int lvl_dim_mult)1247 static INLINE int does_level_match(int width, int height, double fps,
1248 int lvl_width, int lvl_height,
1249 double lvl_fps, int lvl_dim_mult) {
1250 const int64_t lvl_luma_pels = lvl_width * lvl_height;
1251 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
1252 const int64_t luma_pels = width * height;
1253 const double display_sample_rate = luma_pels * fps;
1254 return luma_pels <= lvl_luma_pels &&
1255 display_sample_rate <= lvl_display_sample_rate &&
1256 width <= lvl_width * lvl_dim_mult &&
1257 height <= lvl_height * lvl_dim_mult;
1258 }
1259
set_bitstream_level_tier(SequenceHeader * seq,AV1_COMMON * cm,const AV1EncoderConfig * oxcf)1260 static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
1261 const AV1EncoderConfig *oxcf) {
1262 // TODO(any): This is a placeholder function that only addresses dimensions
1263 // and max display sample rates.
1264 // Need to add checks for max bit rate, max decoded luma sample rate, header
1265 // rate, etc. that are not covered by this function.
1266 AV1_LEVEL level = SEQ_LEVEL_MAX;
1267 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
1268 288, 30.0, 4)) {
1269 level = SEQ_LEVEL_2_0;
1270 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1271 704, 396, 30.0, 4)) {
1272 level = SEQ_LEVEL_2_1;
1273 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1274 1088, 612, 30.0, 4)) {
1275 level = SEQ_LEVEL_3_0;
1276 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1277 1376, 774, 30.0, 4)) {
1278 level = SEQ_LEVEL_3_1;
1279 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1280 2048, 1152, 30.0, 3)) {
1281 level = SEQ_LEVEL_4_0;
1282 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1283 2048, 1152, 60.0, 3)) {
1284 level = SEQ_LEVEL_4_1;
1285 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1286 4096, 2176, 30.0, 2)) {
1287 level = SEQ_LEVEL_5_0;
1288 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1289 4096, 2176, 60.0, 2)) {
1290 level = SEQ_LEVEL_5_1;
1291 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1292 4096, 2176, 120.0, 2)) {
1293 level = SEQ_LEVEL_5_2;
1294 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1295 8192, 4352, 30.0, 2)) {
1296 level = SEQ_LEVEL_6_0;
1297 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1298 8192, 4352, 60.0, 2)) {
1299 level = SEQ_LEVEL_6_1;
1300 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1301 8192, 4352, 120.0, 2)) {
1302 level = SEQ_LEVEL_6_2;
1303 }
1304
1305 SequenceHeader *const seq_params = &cm->seq_params;
1306 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
1307 seq->seq_level_idx[i] = level;
1308 // Set the maximum parameters for bitrate and buffer size for this profile,
1309 // level, and tier
1310 seq_params->op_params[i].bitrate = av1_max_level_bitrate(
1311 cm->seq_params.profile, seq->seq_level_idx[i], seq->tier[i]);
1312 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
1313 // check
1314 if (seq_params->op_params[i].bitrate == 0)
1315 aom_internal_error(
1316 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
1317 "AV1 does not support this combination of profile, level, and tier.");
1318 // Buffer size in bits/s is bitrate in bits/s * 1 s
1319 seq_params->op_params[i].buffer_size = seq_params->op_params[i].bitrate;
1320 }
1321 }
1322
init_seq_coding_tools(SequenceHeader * seq,AV1_COMMON * cm,const AV1EncoderConfig * oxcf,int use_svc)1323 static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
1324 const AV1EncoderConfig *oxcf, int use_svc) {
1325 seq->still_picture = (oxcf->force_video_mode == 0) && (oxcf->limit == 1);
1326 seq->reduced_still_picture_hdr = seq->still_picture;
1327 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
1328 seq->force_screen_content_tools = (oxcf->mode == REALTIME) ? 0 : 2;
1329 seq->force_integer_mv = 2;
1330 seq->order_hint_info.enable_order_hint = oxcf->enable_order_hint;
1331 seq->frame_id_numbers_present_flag =
1332 !(seq->still_picture && seq->reduced_still_picture_hdr) &&
1333 !oxcf->large_scale_tile && oxcf->error_resilient_mode && !use_svc;
1334 if (seq->still_picture && seq->reduced_still_picture_hdr) {
1335 seq->order_hint_info.enable_order_hint = 0;
1336 seq->force_screen_content_tools = 2;
1337 seq->force_integer_mv = 2;
1338 }
1339 seq->order_hint_info.order_hint_bits_minus_1 =
1340 seq->order_hint_info.enable_order_hint
1341 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
1342 : -1;
1343
1344 seq->max_frame_width =
1345 oxcf->forced_max_frame_width ? oxcf->forced_max_frame_width : oxcf->width;
1346 seq->max_frame_height = oxcf->forced_max_frame_height
1347 ? oxcf->forced_max_frame_height
1348 : oxcf->height;
1349 seq->num_bits_width =
1350 (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
1351 seq->num_bits_height =
1352 (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
1353 assert(seq->num_bits_width <= 16);
1354 assert(seq->num_bits_height <= 16);
1355
1356 seq->frame_id_length = FRAME_ID_LENGTH;
1357 seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
1358
1359 seq->enable_dual_filter = oxcf->enable_dual_filter;
1360 seq->order_hint_info.enable_dist_wtd_comp = oxcf->enable_dist_wtd_comp;
1361 seq->order_hint_info.enable_dist_wtd_comp &=
1362 seq->order_hint_info.enable_order_hint;
1363 seq->order_hint_info.enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1364 seq->order_hint_info.enable_ref_frame_mvs &=
1365 seq->order_hint_info.enable_order_hint;
1366 seq->enable_superres = oxcf->enable_superres;
1367 seq->enable_cdef = oxcf->enable_cdef;
1368 seq->enable_restoration = oxcf->enable_restoration;
1369 seq->enable_warped_motion = oxcf->enable_warped_motion;
1370 seq->enable_interintra_compound = oxcf->enable_interintra_comp;
1371 seq->enable_masked_compound = oxcf->enable_masked_comp;
1372 seq->enable_intra_edge_filter = oxcf->enable_intra_edge_filter;
1373 seq->enable_filter_intra = oxcf->enable_filter_intra;
1374
1375 set_bitstream_level_tier(seq, cm, oxcf);
1376
1377 if (seq->operating_points_cnt_minus_1 == 0) {
1378 seq->operating_point_idc[0] = 0;
1379 } else {
1380 // Set operating_point_idc[] such that the i=0 point corresponds to the
1381 // highest quality operating point (all layers), and subsequent
1382 // operarting points (i > 0) are lower quality corresponding to
1383 // skip decoding enhancement layers (temporal first).
1384 int i = 0;
1385 assert(seq->operating_points_cnt_minus_1 ==
1386 (int)(cm->number_spatial_layers * cm->number_temporal_layers - 1));
1387 for (unsigned int sl = 0; sl < cm->number_spatial_layers; sl++) {
1388 for (unsigned int tl = 0; tl < cm->number_temporal_layers; tl++) {
1389 seq->operating_point_idc[i] =
1390 (~(~0u << (cm->number_spatial_layers - sl)) << 8) |
1391 ~(~0u << (cm->number_temporal_layers - tl));
1392 i++;
1393 }
1394 }
1395 }
1396 }
1397
init_config(struct AV1_COMP * cpi,AV1EncoderConfig * oxcf)1398 static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1399 AV1_COMMON *const cm = &cpi->common;
1400 SequenceHeader *const seq_params = &cm->seq_params;
1401 ResizePendingParams *resize_pending_params = &cpi->resize_pending_params;
1402
1403 cpi->oxcf = *oxcf;
1404 cpi->framerate = oxcf->init_framerate;
1405
1406 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 =
1417 oxcf->timing_info.num_units_in_display_tick;
1418 seq_params->timing_info.time_scale = oxcf->timing_info.time_scale;
1419 seq_params->timing_info.equal_picture_interval =
1420 oxcf->timing_info.equal_picture_interval;
1421 seq_params->timing_info.num_ticks_per_picture =
1422 oxcf->timing_info.num_ticks_per_picture;
1423
1424 seq_params->display_model_info_present_flag =
1425 oxcf->display_model_info_present_flag;
1426 seq_params->decoder_model_info_present_flag =
1427 oxcf->decoder_model_info_present_flag;
1428 if (oxcf->decoder_model_info_present_flag) {
1429 // set the decoder model parameters in schedule mode
1430 seq_params->decoder_model_info.num_units_in_decoding_tick =
1431 oxcf->buffer_model.num_units_in_decoding_tick;
1432 cm->buffer_removal_time_present = 1;
1433 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) {
1438 // set the decoder model parameters in resource availability mode
1439 av1_set_resource_availability_parameters(&seq_params->op_params[0]);
1440 } else {
1441 seq_params->op_params[0].initial_display_delay =
1442 10; // Default value (not signaled)
1443 }
1444
1445 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;
1453 } else {
1454 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;
1460 } else {
1461 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;
1464 } else {
1465 seq_params->subsampling_x = 1;
1466 seq_params->subsampling_y = 0;
1467 }
1468 }
1469 }
1470
1471 cm->width = oxcf->width;
1472 cm->height = oxcf->height;
1473 set_sb_size(seq_params,
1474 select_sb_size(cpi)); // set sb size before allocations
1475 alloc_compressor_data(cpi);
1476
1477 update_film_grain_parameters(cpi, oxcf);
1478
1479 // Single thread case: use counts in common.
1480 cpi->td.counts = &cpi->counts;
1481
1482 // Set init SVC parameters.
1483 cpi->use_svc = 0;
1484 cpi->svc.external_ref_frame_config = 0;
1485 cpi->svc.non_reference_frame = 0;
1486 cpi->svc.number_spatial_layers = 1;
1487 cpi->svc.number_temporal_layers = 1;
1488 cm->number_spatial_layers = 1;
1489 cm->number_temporal_layers = 1;
1490 cm->spatial_layer_id = 0;
1491 cm->temporal_layer_id = 0;
1492
1493 // change includes all joint functionality
1494 av1_change_config(cpi, oxcf);
1495
1496 cpi->ref_frame_flags = 0;
1497
1498 // Reset resize pending flags
1499 resize_pending_params->width = 0;
1500 resize_pending_params->height = 0;
1501
1502 init_buffer_indices(&cpi->force_intpel_info, cm->remapped_ref_idx);
1503 }
1504
set_rc_buffer_sizes(RATE_CONTROL * rc,const AV1EncoderConfig * oxcf)1505 static void set_rc_buffer_sizes(RATE_CONTROL *rc,
1506 const AV1EncoderConfig *oxcf) {
1507 const int64_t bandwidth = oxcf->target_bandwidth;
1508 const int64_t starting = oxcf->starting_buffer_level_ms;
1509 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1510 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1511
1512 rc->starting_buffer_level = starting * bandwidth / 1000;
1513 rc->optimal_buffer_level =
1514 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1515 rc->maximum_buffer_size =
1516 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1517 }
1518
1519 #define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1520 cpi->fn_ptr[BT].sdf = SDF; \
1521 cpi->fn_ptr[BT].sdaf = SDAF; \
1522 cpi->fn_ptr[BT].vf = VF; \
1523 cpi->fn_ptr[BT].svf = SVF; \
1524 cpi->fn_ptr[BT].svaf = SVAF; \
1525 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1526 cpi->fn_ptr[BT].jsdaf = JSDAF; \
1527 cpi->fn_ptr[BT].jsvaf = JSVAF;
1528
1529 #define MAKE_BFP_SAD_WRAPPER(fnname) \
1530 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1531 int source_stride, \
1532 const uint8_t *ref_ptr, int ref_stride) { \
1533 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1534 } \
1535 static unsigned int fnname##_bits10( \
1536 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1537 int ref_stride) { \
1538 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1539 } \
1540 static unsigned int fnname##_bits12( \
1541 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1542 int ref_stride) { \
1543 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1544 }
1545
1546 #define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1547 static unsigned int fnname##_bits8( \
1548 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1549 int ref_stride, const uint8_t *second_pred) { \
1550 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1551 } \
1552 static unsigned int fnname##_bits10( \
1553 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1554 int ref_stride, const uint8_t *second_pred) { \
1555 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1556 2; \
1557 } \
1558 static unsigned int fnname##_bits12( \
1559 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1560 int ref_stride, const uint8_t *second_pred) { \
1561 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1562 4; \
1563 }
1564
1565 #define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1566 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1567 const uint8_t *const ref_ptr[], int ref_stride, \
1568 unsigned int *sad_array) { \
1569 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1570 } \
1571 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1572 const uint8_t *const ref_ptr[], int ref_stride, \
1573 unsigned int *sad_array) { \
1574 int i; \
1575 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1576 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1577 } \
1578 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1579 const uint8_t *const ref_ptr[], int ref_stride, \
1580 unsigned int *sad_array) { \
1581 int i; \
1582 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1583 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1584 }
1585
1586 #define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1587 static unsigned int fnname##_bits8( \
1588 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1589 int ref_stride, const uint8_t *second_pred, \
1590 const DIST_WTD_COMP_PARAMS *jcp_param) { \
1591 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1592 jcp_param); \
1593 } \
1594 static unsigned int fnname##_bits10( \
1595 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1596 int ref_stride, const uint8_t *second_pred, \
1597 const DIST_WTD_COMP_PARAMS *jcp_param) { \
1598 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1599 jcp_param) >> \
1600 2; \
1601 } \
1602 static unsigned int fnname##_bits12( \
1603 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1604 int ref_stride, const uint8_t *second_pred, \
1605 const DIST_WTD_COMP_PARAMS *jcp_param) { \
1606 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1607 jcp_param) >> \
1608 4; \
1609 }
1610
1611 #if CONFIG_AV1_HIGHBITDEPTH
1612 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)1613 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
1614 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1615 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1616 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1617 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1618 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1619 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1620 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
1621 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1622 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1623 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1624 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1625 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1626 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1627 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1628 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1629 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1630 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1631 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1632 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1633 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1634 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
1635 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1636 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1637 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
1638 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1639 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1640 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
1641 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1642 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1643 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
1644 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1645 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1646 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
1647 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1648 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1649 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
1650 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1651 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1652 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
1653 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1654 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1655 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
1656 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1657 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1658 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
1659 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
1660
1661 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1662 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1663 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1664 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1665 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1666 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1667 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1668 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1669 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1670 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1671 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1672 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
1673 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1674 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1675 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1676 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1677 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1678 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
1679
1680 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x128_avg)
1681 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x64_avg)
1682 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x128_avg)
1683 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x16_avg)
1684 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x32_avg)
1685 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x32_avg)
1686 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x64_avg)
1687 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x32_avg)
1688 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x64_avg)
1689 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x16_avg)
1690 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x8_avg)
1691 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x16_avg)
1692 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x8_avg)
1693 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x4_avg)
1694 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x8_avg)
1695 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x4_avg)
1696 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x16_avg)
1697 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x4_avg)
1698 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x32_avg)
1699 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x8_avg)
1700 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x64_avg)
1701 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x16_avg)
1702 #endif // CONFIG_AV1_HIGHBITDEPTH
1703
1704 #define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
1705 cpi->fn_ptr[BT].msdf = MCSDF; \
1706 cpi->fn_ptr[BT].msvf = MCSVF;
1707
1708 #define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1709 static unsigned int fnname##_bits8( \
1710 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1711 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1712 int m_stride, int invert_mask) { \
1713 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1714 second_pred_ptr, m, m_stride, invert_mask); \
1715 } \
1716 static unsigned int fnname##_bits10( \
1717 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1718 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1719 int m_stride, int invert_mask) { \
1720 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1721 second_pred_ptr, m, m_stride, invert_mask) >> \
1722 2; \
1723 } \
1724 static unsigned int fnname##_bits12( \
1725 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1726 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1727 int m_stride, int invert_mask) { \
1728 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1729 second_pred_ptr, m, m_stride, invert_mask) >> \
1730 4; \
1731 }
1732
1733 #if CONFIG_AV1_HIGHBITDEPTH
1734 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1735 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1736 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
1737 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1738 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1739 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1740 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1741 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1742 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1743 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1744 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1745 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1746 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1747 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1748 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1749 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
1750 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1751 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1752 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1753 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
1754 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1755 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
1756 #endif
1757
1758 #define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1759 cpi->fn_ptr[BT].osdf = OSDF; \
1760 cpi->fn_ptr[BT].ovf = OVF; \
1761 cpi->fn_ptr[BT].osvf = OSVF;
1762
1763 #define MAKE_OBFP_SAD_WRAPPER(fnname) \
1764 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1765 const int32_t *wsrc, \
1766 const int32_t *msk) { \
1767 return fnname(ref, ref_stride, wsrc, msk); \
1768 } \
1769 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1770 const int32_t *wsrc, \
1771 const int32_t *msk) { \
1772 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1773 } \
1774 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1775 const int32_t *wsrc, \
1776 const int32_t *msk) { \
1777 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1778 }
1779
1780 #if CONFIG_AV1_HIGHBITDEPTH
1781 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1782 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1783 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
1784 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1785 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1786 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1787 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1788 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1789 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1790 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1791 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1792 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1793 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1794 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1795 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1796 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
1797 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1798 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1799 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1800 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
1801 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1802 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
1803
1804 static void highbd_set_var_fns(AV1_COMP *const cpi) {
1805 AV1_COMMON *const cm = &cpi->common;
1806 if (cm->seq_params.use_highbitdepth) {
1807 switch (cm->seq_params.bit_depth) {
1808 case AOM_BITS_8:
1809 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1810 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1811 aom_highbd_8_sub_pixel_variance64x16,
1812 aom_highbd_8_sub_pixel_avg_variance64x16,
1813 aom_highbd_sad64x16x4d_bits8,
1814 aom_highbd_dist_wtd_sad64x16_avg_bits8,
1815 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x16)
1816
1817 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1818 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1819 aom_highbd_8_sub_pixel_variance16x64,
1820 aom_highbd_8_sub_pixel_avg_variance16x64,
1821 aom_highbd_sad16x64x4d_bits8,
1822 aom_highbd_dist_wtd_sad16x64_avg_bits8,
1823 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x64)
1824
1825 HIGHBD_BFP(
1826 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1827 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
1828 aom_highbd_8_sub_pixel_avg_variance32x8,
1829 aom_highbd_sad32x8x4d_bits8, aom_highbd_dist_wtd_sad32x8_avg_bits8,
1830 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x8)
1831
1832 HIGHBD_BFP(
1833 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1834 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
1835 aom_highbd_8_sub_pixel_avg_variance8x32,
1836 aom_highbd_sad8x32x4d_bits8, aom_highbd_dist_wtd_sad8x32_avg_bits8,
1837 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x32)
1838
1839 HIGHBD_BFP(
1840 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1841 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
1842 aom_highbd_8_sub_pixel_avg_variance16x4,
1843 aom_highbd_sad16x4x4d_bits8, aom_highbd_dist_wtd_sad16x4_avg_bits8,
1844 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x4)
1845
1846 HIGHBD_BFP(
1847 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1848 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
1849 aom_highbd_8_sub_pixel_avg_variance4x16,
1850 aom_highbd_sad4x16x4d_bits8, aom_highbd_dist_wtd_sad4x16_avg_bits8,
1851 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x16)
1852
1853 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1854 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1855 aom_highbd_8_sub_pixel_variance32x16,
1856 aom_highbd_8_sub_pixel_avg_variance32x16,
1857 aom_highbd_sad32x16x4d_bits8,
1858 aom_highbd_dist_wtd_sad32x16_avg_bits8,
1859 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x16)
1860
1861 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1862 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1863 aom_highbd_8_sub_pixel_variance16x32,
1864 aom_highbd_8_sub_pixel_avg_variance16x32,
1865 aom_highbd_sad16x32x4d_bits8,
1866 aom_highbd_dist_wtd_sad16x32_avg_bits8,
1867 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x32)
1868
1869 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1870 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1871 aom_highbd_8_sub_pixel_variance64x32,
1872 aom_highbd_8_sub_pixel_avg_variance64x32,
1873 aom_highbd_sad64x32x4d_bits8,
1874 aom_highbd_dist_wtd_sad64x32_avg_bits8,
1875 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x32)
1876
1877 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1878 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1879 aom_highbd_8_sub_pixel_variance32x64,
1880 aom_highbd_8_sub_pixel_avg_variance32x64,
1881 aom_highbd_sad32x64x4d_bits8,
1882 aom_highbd_dist_wtd_sad32x64_avg_bits8,
1883 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x64)
1884
1885 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1886 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1887 aom_highbd_8_sub_pixel_variance32x32,
1888 aom_highbd_8_sub_pixel_avg_variance32x32,
1889 aom_highbd_sad32x32x4d_bits8,
1890 aom_highbd_dist_wtd_sad32x32_avg_bits8,
1891 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x32)
1892
1893 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1894 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1895 aom_highbd_8_sub_pixel_variance64x64,
1896 aom_highbd_8_sub_pixel_avg_variance64x64,
1897 aom_highbd_sad64x64x4d_bits8,
1898 aom_highbd_dist_wtd_sad64x64_avg_bits8,
1899 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x64)
1900
1901 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1902 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1903 aom_highbd_8_sub_pixel_variance16x16,
1904 aom_highbd_8_sub_pixel_avg_variance16x16,
1905 aom_highbd_sad16x16x4d_bits8,
1906 aom_highbd_dist_wtd_sad16x16_avg_bits8,
1907 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x16)
1908
1909 HIGHBD_BFP(
1910 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1911 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
1912 aom_highbd_8_sub_pixel_avg_variance16x8,
1913 aom_highbd_sad16x8x4d_bits8, aom_highbd_dist_wtd_sad16x8_avg_bits8,
1914 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x8)
1915
1916 HIGHBD_BFP(
1917 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1918 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
1919 aom_highbd_8_sub_pixel_avg_variance8x16,
1920 aom_highbd_sad8x16x4d_bits8, aom_highbd_dist_wtd_sad8x16_avg_bits8,
1921 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x16)
1922
1923 HIGHBD_BFP(
1924 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1925 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1926 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x4d_bits8,
1927 aom_highbd_dist_wtd_sad8x8_avg_bits8,
1928 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x8)
1929
1930 HIGHBD_BFP(
1931 BLOCK_8X4, aom_highbd_sad8x4_bits8, aom_highbd_sad8x4_avg_bits8,
1932 aom_highbd_8_variance8x4, aom_highbd_8_sub_pixel_variance8x4,
1933 aom_highbd_8_sub_pixel_avg_variance8x4, aom_highbd_sad8x4x4d_bits8,
1934 aom_highbd_dist_wtd_sad8x4_avg_bits8,
1935 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x4)
1936
1937 HIGHBD_BFP(
1938 BLOCK_4X8, aom_highbd_sad4x8_bits8, aom_highbd_sad4x8_avg_bits8,
1939 aom_highbd_8_variance4x8, aom_highbd_8_sub_pixel_variance4x8,
1940 aom_highbd_8_sub_pixel_avg_variance4x8, aom_highbd_sad4x8x4d_bits8,
1941 aom_highbd_dist_wtd_sad4x8_avg_bits8,
1942 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x8)
1943
1944 HIGHBD_BFP(
1945 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1946 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1947 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x4d_bits8,
1948 aom_highbd_dist_wtd_sad4x4_avg_bits8,
1949 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x4)
1950
1951 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1952 aom_highbd_sad128x128_avg_bits8,
1953 aom_highbd_8_variance128x128,
1954 aom_highbd_8_sub_pixel_variance128x128,
1955 aom_highbd_8_sub_pixel_avg_variance128x128,
1956 aom_highbd_sad128x128x4d_bits8,
1957 aom_highbd_dist_wtd_sad128x128_avg_bits8,
1958 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x128)
1959
1960 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1961 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1962 aom_highbd_8_sub_pixel_variance128x64,
1963 aom_highbd_8_sub_pixel_avg_variance128x64,
1964 aom_highbd_sad128x64x4d_bits8,
1965 aom_highbd_dist_wtd_sad128x64_avg_bits8,
1966 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x64)
1967
1968 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1969 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1970 aom_highbd_8_sub_pixel_variance64x128,
1971 aom_highbd_8_sub_pixel_avg_variance64x128,
1972 aom_highbd_sad64x128x4d_bits8,
1973 aom_highbd_dist_wtd_sad64x128_avg_bits8,
1974 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x128)
1975
1976 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1977 aom_highbd_8_masked_sub_pixel_variance128x128)
1978 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1979 aom_highbd_8_masked_sub_pixel_variance128x64)
1980 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1981 aom_highbd_8_masked_sub_pixel_variance64x128)
1982 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1983 aom_highbd_8_masked_sub_pixel_variance64x64)
1984 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1985 aom_highbd_8_masked_sub_pixel_variance64x32)
1986 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1987 aom_highbd_8_masked_sub_pixel_variance32x64)
1988 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1989 aom_highbd_8_masked_sub_pixel_variance32x32)
1990 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1991 aom_highbd_8_masked_sub_pixel_variance32x16)
1992 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1993 aom_highbd_8_masked_sub_pixel_variance16x32)
1994 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1995 aom_highbd_8_masked_sub_pixel_variance16x16)
1996 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1997 aom_highbd_8_masked_sub_pixel_variance8x16)
1998 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1999 aom_highbd_8_masked_sub_pixel_variance16x8)
2000 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
2001 aom_highbd_8_masked_sub_pixel_variance8x8)
2002 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
2003 aom_highbd_8_masked_sub_pixel_variance4x8)
2004 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
2005 aom_highbd_8_masked_sub_pixel_variance8x4)
2006 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
2007 aom_highbd_8_masked_sub_pixel_variance4x4)
2008 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
2009 aom_highbd_8_masked_sub_pixel_variance64x16)
2010 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
2011 aom_highbd_8_masked_sub_pixel_variance16x64)
2012 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
2013 aom_highbd_8_masked_sub_pixel_variance32x8)
2014 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
2015 aom_highbd_8_masked_sub_pixel_variance8x32)
2016 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
2017 aom_highbd_8_masked_sub_pixel_variance16x4)
2018 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
2019 aom_highbd_8_masked_sub_pixel_variance4x16)
2020 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
2021 aom_highbd_obmc_variance128x128,
2022 aom_highbd_obmc_sub_pixel_variance128x128)
2023 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
2024 aom_highbd_obmc_variance128x64,
2025 aom_highbd_obmc_sub_pixel_variance128x64)
2026 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
2027 aom_highbd_obmc_variance64x128,
2028 aom_highbd_obmc_sub_pixel_variance64x128)
2029 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
2030 aom_highbd_obmc_variance64x64,
2031 aom_highbd_obmc_sub_pixel_variance64x64)
2032 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
2033 aom_highbd_obmc_variance64x32,
2034 aom_highbd_obmc_sub_pixel_variance64x32)
2035 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
2036 aom_highbd_obmc_variance32x64,
2037 aom_highbd_obmc_sub_pixel_variance32x64)
2038 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
2039 aom_highbd_obmc_variance32x32,
2040 aom_highbd_obmc_sub_pixel_variance32x32)
2041 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
2042 aom_highbd_obmc_variance32x16,
2043 aom_highbd_obmc_sub_pixel_variance32x16)
2044 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
2045 aom_highbd_obmc_variance16x32,
2046 aom_highbd_obmc_sub_pixel_variance16x32)
2047 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
2048 aom_highbd_obmc_variance16x16,
2049 aom_highbd_obmc_sub_pixel_variance16x16)
2050 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
2051 aom_highbd_obmc_variance8x16,
2052 aom_highbd_obmc_sub_pixel_variance8x16)
2053 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
2054 aom_highbd_obmc_variance16x8,
2055 aom_highbd_obmc_sub_pixel_variance16x8)
2056 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
2057 aom_highbd_obmc_variance8x8,
2058 aom_highbd_obmc_sub_pixel_variance8x8)
2059 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
2060 aom_highbd_obmc_variance4x8,
2061 aom_highbd_obmc_sub_pixel_variance4x8)
2062 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
2063 aom_highbd_obmc_variance8x4,
2064 aom_highbd_obmc_sub_pixel_variance8x4)
2065 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
2066 aom_highbd_obmc_variance4x4,
2067 aom_highbd_obmc_sub_pixel_variance4x4)
2068 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
2069 aom_highbd_obmc_variance64x16,
2070 aom_highbd_obmc_sub_pixel_variance64x16)
2071 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
2072 aom_highbd_obmc_variance16x64,
2073 aom_highbd_obmc_sub_pixel_variance16x64)
2074 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
2075 aom_highbd_obmc_variance32x8,
2076 aom_highbd_obmc_sub_pixel_variance32x8)
2077 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
2078 aom_highbd_obmc_variance8x32,
2079 aom_highbd_obmc_sub_pixel_variance8x32)
2080 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
2081 aom_highbd_obmc_variance16x4,
2082 aom_highbd_obmc_sub_pixel_variance16x4)
2083 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
2084 aom_highbd_obmc_variance4x16,
2085 aom_highbd_obmc_sub_pixel_variance4x16)
2086 break;
2087
2088 case AOM_BITS_10:
2089 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
2090 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
2091 aom_highbd_10_sub_pixel_variance64x16,
2092 aom_highbd_10_sub_pixel_avg_variance64x16,
2093 aom_highbd_sad64x16x4d_bits10,
2094 aom_highbd_dist_wtd_sad64x16_avg_bits10,
2095 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x16);
2096
2097 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
2098 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
2099 aom_highbd_10_sub_pixel_variance16x64,
2100 aom_highbd_10_sub_pixel_avg_variance16x64,
2101 aom_highbd_sad16x64x4d_bits10,
2102 aom_highbd_dist_wtd_sad16x64_avg_bits10,
2103 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x64);
2104
2105 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
2106 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
2107 aom_highbd_10_sub_pixel_variance32x8,
2108 aom_highbd_10_sub_pixel_avg_variance32x8,
2109 aom_highbd_sad32x8x4d_bits10,
2110 aom_highbd_dist_wtd_sad32x8_avg_bits10,
2111 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x8);
2112
2113 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
2114 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
2115 aom_highbd_10_sub_pixel_variance8x32,
2116 aom_highbd_10_sub_pixel_avg_variance8x32,
2117 aom_highbd_sad8x32x4d_bits10,
2118 aom_highbd_dist_wtd_sad8x32_avg_bits10,
2119 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x32);
2120
2121 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
2122 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
2123 aom_highbd_10_sub_pixel_variance16x4,
2124 aom_highbd_10_sub_pixel_avg_variance16x4,
2125 aom_highbd_sad16x4x4d_bits10,
2126 aom_highbd_dist_wtd_sad16x4_avg_bits10,
2127 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x4);
2128
2129 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
2130 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
2131 aom_highbd_10_sub_pixel_variance4x16,
2132 aom_highbd_10_sub_pixel_avg_variance4x16,
2133 aom_highbd_sad4x16x4d_bits10,
2134 aom_highbd_dist_wtd_sad4x16_avg_bits10,
2135 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x16);
2136
2137 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
2138 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
2139 aom_highbd_10_sub_pixel_variance32x16,
2140 aom_highbd_10_sub_pixel_avg_variance32x16,
2141 aom_highbd_sad32x16x4d_bits10,
2142 aom_highbd_dist_wtd_sad32x16_avg_bits10,
2143 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x16);
2144
2145 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
2146 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
2147 aom_highbd_10_sub_pixel_variance16x32,
2148 aom_highbd_10_sub_pixel_avg_variance16x32,
2149 aom_highbd_sad16x32x4d_bits10,
2150 aom_highbd_dist_wtd_sad16x32_avg_bits10,
2151 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x32);
2152
2153 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
2154 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
2155 aom_highbd_10_sub_pixel_variance64x32,
2156 aom_highbd_10_sub_pixel_avg_variance64x32,
2157 aom_highbd_sad64x32x4d_bits10,
2158 aom_highbd_dist_wtd_sad64x32_avg_bits10,
2159 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x32);
2160
2161 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
2162 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
2163 aom_highbd_10_sub_pixel_variance32x64,
2164 aom_highbd_10_sub_pixel_avg_variance32x64,
2165 aom_highbd_sad32x64x4d_bits10,
2166 aom_highbd_dist_wtd_sad32x64_avg_bits10,
2167 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x64);
2168
2169 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
2170 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
2171 aom_highbd_10_sub_pixel_variance32x32,
2172 aom_highbd_10_sub_pixel_avg_variance32x32,
2173 aom_highbd_sad32x32x4d_bits10,
2174 aom_highbd_dist_wtd_sad32x32_avg_bits10,
2175 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x32);
2176
2177 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
2178 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
2179 aom_highbd_10_sub_pixel_variance64x64,
2180 aom_highbd_10_sub_pixel_avg_variance64x64,
2181 aom_highbd_sad64x64x4d_bits10,
2182 aom_highbd_dist_wtd_sad64x64_avg_bits10,
2183 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x64);
2184
2185 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
2186 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
2187 aom_highbd_10_sub_pixel_variance16x16,
2188 aom_highbd_10_sub_pixel_avg_variance16x16,
2189 aom_highbd_sad16x16x4d_bits10,
2190 aom_highbd_dist_wtd_sad16x16_avg_bits10,
2191 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x16);
2192
2193 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
2194 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
2195 aom_highbd_10_sub_pixel_variance16x8,
2196 aom_highbd_10_sub_pixel_avg_variance16x8,
2197 aom_highbd_sad16x8x4d_bits10,
2198 aom_highbd_dist_wtd_sad16x8_avg_bits10,
2199 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x8);
2200
2201 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
2202 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
2203 aom_highbd_10_sub_pixel_variance8x16,
2204 aom_highbd_10_sub_pixel_avg_variance8x16,
2205 aom_highbd_sad8x16x4d_bits10,
2206 aom_highbd_dist_wtd_sad8x16_avg_bits10,
2207 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x16);
2208
2209 HIGHBD_BFP(
2210 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
2211 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
2212 aom_highbd_10_sub_pixel_avg_variance8x8,
2213 aom_highbd_sad8x8x4d_bits10, aom_highbd_dist_wtd_sad8x8_avg_bits10,
2214 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x8);
2215
2216 HIGHBD_BFP(
2217 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
2218 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
2219 aom_highbd_10_sub_pixel_avg_variance8x4,
2220 aom_highbd_sad8x4x4d_bits10, aom_highbd_dist_wtd_sad8x4_avg_bits10,
2221 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x4);
2222
2223 HIGHBD_BFP(
2224 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
2225 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
2226 aom_highbd_10_sub_pixel_avg_variance4x8,
2227 aom_highbd_sad4x8x4d_bits10, aom_highbd_dist_wtd_sad4x8_avg_bits10,
2228 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x8);
2229
2230 HIGHBD_BFP(
2231 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
2232 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
2233 aom_highbd_10_sub_pixel_avg_variance4x4,
2234 aom_highbd_sad4x4x4d_bits10, aom_highbd_dist_wtd_sad4x4_avg_bits10,
2235 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x4);
2236
2237 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
2238 aom_highbd_sad128x128_avg_bits10,
2239 aom_highbd_10_variance128x128,
2240 aom_highbd_10_sub_pixel_variance128x128,
2241 aom_highbd_10_sub_pixel_avg_variance128x128,
2242 aom_highbd_sad128x128x4d_bits10,
2243 aom_highbd_dist_wtd_sad128x128_avg_bits10,
2244 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x128);
2245
2246 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
2247 aom_highbd_sad128x64_avg_bits10,
2248 aom_highbd_10_variance128x64,
2249 aom_highbd_10_sub_pixel_variance128x64,
2250 aom_highbd_10_sub_pixel_avg_variance128x64,
2251 aom_highbd_sad128x64x4d_bits10,
2252 aom_highbd_dist_wtd_sad128x64_avg_bits10,
2253 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x64);
2254
2255 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
2256 aom_highbd_sad64x128_avg_bits10,
2257 aom_highbd_10_variance64x128,
2258 aom_highbd_10_sub_pixel_variance64x128,
2259 aom_highbd_10_sub_pixel_avg_variance64x128,
2260 aom_highbd_sad64x128x4d_bits10,
2261 aom_highbd_dist_wtd_sad64x128_avg_bits10,
2262 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x128);
2263
2264 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
2265 aom_highbd_10_masked_sub_pixel_variance128x128)
2266 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
2267 aom_highbd_10_masked_sub_pixel_variance128x64)
2268 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
2269 aom_highbd_10_masked_sub_pixel_variance64x128)
2270 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
2271 aom_highbd_10_masked_sub_pixel_variance64x64)
2272 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
2273 aom_highbd_10_masked_sub_pixel_variance64x32)
2274 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
2275 aom_highbd_10_masked_sub_pixel_variance32x64)
2276 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
2277 aom_highbd_10_masked_sub_pixel_variance32x32)
2278 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
2279 aom_highbd_10_masked_sub_pixel_variance32x16)
2280 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
2281 aom_highbd_10_masked_sub_pixel_variance16x32)
2282 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
2283 aom_highbd_10_masked_sub_pixel_variance16x16)
2284 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
2285 aom_highbd_10_masked_sub_pixel_variance8x16)
2286 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
2287 aom_highbd_10_masked_sub_pixel_variance16x8)
2288 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
2289 aom_highbd_10_masked_sub_pixel_variance8x8)
2290 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
2291 aom_highbd_10_masked_sub_pixel_variance4x8)
2292 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
2293 aom_highbd_10_masked_sub_pixel_variance8x4)
2294 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
2295 aom_highbd_10_masked_sub_pixel_variance4x4)
2296 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
2297 aom_highbd_10_masked_sub_pixel_variance64x16)
2298 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
2299 aom_highbd_10_masked_sub_pixel_variance16x64)
2300 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
2301 aom_highbd_10_masked_sub_pixel_variance32x8)
2302 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
2303 aom_highbd_10_masked_sub_pixel_variance8x32)
2304 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
2305 aom_highbd_10_masked_sub_pixel_variance16x4)
2306 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
2307 aom_highbd_10_masked_sub_pixel_variance4x16)
2308 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
2309 aom_highbd_10_obmc_variance128x128,
2310 aom_highbd_10_obmc_sub_pixel_variance128x128)
2311 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
2312 aom_highbd_10_obmc_variance128x64,
2313 aom_highbd_10_obmc_sub_pixel_variance128x64)
2314 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
2315 aom_highbd_10_obmc_variance64x128,
2316 aom_highbd_10_obmc_sub_pixel_variance64x128)
2317 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2318 aom_highbd_10_obmc_variance64x64,
2319 aom_highbd_10_obmc_sub_pixel_variance64x64)
2320 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2321 aom_highbd_10_obmc_variance64x32,
2322 aom_highbd_10_obmc_sub_pixel_variance64x32)
2323 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2324 aom_highbd_10_obmc_variance32x64,
2325 aom_highbd_10_obmc_sub_pixel_variance32x64)
2326 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2327 aom_highbd_10_obmc_variance32x32,
2328 aom_highbd_10_obmc_sub_pixel_variance32x32)
2329 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2330 aom_highbd_10_obmc_variance32x16,
2331 aom_highbd_10_obmc_sub_pixel_variance32x16)
2332 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2333 aom_highbd_10_obmc_variance16x32,
2334 aom_highbd_10_obmc_sub_pixel_variance16x32)
2335 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2336 aom_highbd_10_obmc_variance16x16,
2337 aom_highbd_10_obmc_sub_pixel_variance16x16)
2338 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2339 aom_highbd_10_obmc_variance8x16,
2340 aom_highbd_10_obmc_sub_pixel_variance8x16)
2341 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2342 aom_highbd_10_obmc_variance16x8,
2343 aom_highbd_10_obmc_sub_pixel_variance16x8)
2344 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2345 aom_highbd_10_obmc_variance8x8,
2346 aom_highbd_10_obmc_sub_pixel_variance8x8)
2347 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2348 aom_highbd_10_obmc_variance4x8,
2349 aom_highbd_10_obmc_sub_pixel_variance4x8)
2350 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2351 aom_highbd_10_obmc_variance8x4,
2352 aom_highbd_10_obmc_sub_pixel_variance8x4)
2353 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2354 aom_highbd_10_obmc_variance4x4,
2355 aom_highbd_10_obmc_sub_pixel_variance4x4)
2356
2357 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2358 aom_highbd_10_obmc_variance64x16,
2359 aom_highbd_10_obmc_sub_pixel_variance64x16)
2360
2361 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2362 aom_highbd_10_obmc_variance16x64,
2363 aom_highbd_10_obmc_sub_pixel_variance16x64)
2364
2365 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2366 aom_highbd_10_obmc_variance32x8,
2367 aom_highbd_10_obmc_sub_pixel_variance32x8)
2368
2369 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2370 aom_highbd_10_obmc_variance8x32,
2371 aom_highbd_10_obmc_sub_pixel_variance8x32)
2372
2373 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2374 aom_highbd_10_obmc_variance16x4,
2375 aom_highbd_10_obmc_sub_pixel_variance16x4)
2376
2377 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2378 aom_highbd_10_obmc_variance4x16,
2379 aom_highbd_10_obmc_sub_pixel_variance4x16)
2380 break;
2381
2382 case AOM_BITS_12:
2383 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2384 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2385 aom_highbd_12_sub_pixel_variance64x16,
2386 aom_highbd_12_sub_pixel_avg_variance64x16,
2387 aom_highbd_sad64x16x4d_bits12,
2388 aom_highbd_dist_wtd_sad64x16_avg_bits12,
2389 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x16);
2390
2391 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2392 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2393 aom_highbd_12_sub_pixel_variance16x64,
2394 aom_highbd_12_sub_pixel_avg_variance16x64,
2395 aom_highbd_sad16x64x4d_bits12,
2396 aom_highbd_dist_wtd_sad16x64_avg_bits12,
2397 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x64);
2398
2399 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2400 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2401 aom_highbd_12_sub_pixel_variance32x8,
2402 aom_highbd_12_sub_pixel_avg_variance32x8,
2403 aom_highbd_sad32x8x4d_bits12,
2404 aom_highbd_dist_wtd_sad32x8_avg_bits12,
2405 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x8);
2406
2407 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2408 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2409 aom_highbd_12_sub_pixel_variance8x32,
2410 aom_highbd_12_sub_pixel_avg_variance8x32,
2411 aom_highbd_sad8x32x4d_bits12,
2412 aom_highbd_dist_wtd_sad8x32_avg_bits12,
2413 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x32);
2414
2415 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2416 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2417 aom_highbd_12_sub_pixel_variance16x4,
2418 aom_highbd_12_sub_pixel_avg_variance16x4,
2419 aom_highbd_sad16x4x4d_bits12,
2420 aom_highbd_dist_wtd_sad16x4_avg_bits12,
2421 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x4);
2422
2423 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2424 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2425 aom_highbd_12_sub_pixel_variance4x16,
2426 aom_highbd_12_sub_pixel_avg_variance4x16,
2427 aom_highbd_sad4x16x4d_bits12,
2428 aom_highbd_dist_wtd_sad4x16_avg_bits12,
2429 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x16);
2430
2431 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2432 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2433 aom_highbd_12_sub_pixel_variance32x16,
2434 aom_highbd_12_sub_pixel_avg_variance32x16,
2435 aom_highbd_sad32x16x4d_bits12,
2436 aom_highbd_dist_wtd_sad32x16_avg_bits12,
2437 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x16);
2438
2439 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2440 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2441 aom_highbd_12_sub_pixel_variance16x32,
2442 aom_highbd_12_sub_pixel_avg_variance16x32,
2443 aom_highbd_sad16x32x4d_bits12,
2444 aom_highbd_dist_wtd_sad16x32_avg_bits12,
2445 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x32);
2446
2447 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2448 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2449 aom_highbd_12_sub_pixel_variance64x32,
2450 aom_highbd_12_sub_pixel_avg_variance64x32,
2451 aom_highbd_sad64x32x4d_bits12,
2452 aom_highbd_dist_wtd_sad64x32_avg_bits12,
2453 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x32);
2454
2455 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2456 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2457 aom_highbd_12_sub_pixel_variance32x64,
2458 aom_highbd_12_sub_pixel_avg_variance32x64,
2459 aom_highbd_sad32x64x4d_bits12,
2460 aom_highbd_dist_wtd_sad32x64_avg_bits12,
2461 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x64);
2462
2463 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2464 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2465 aom_highbd_12_sub_pixel_variance32x32,
2466 aom_highbd_12_sub_pixel_avg_variance32x32,
2467 aom_highbd_sad32x32x4d_bits12,
2468 aom_highbd_dist_wtd_sad32x32_avg_bits12,
2469 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x32);
2470
2471 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2472 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2473 aom_highbd_12_sub_pixel_variance64x64,
2474 aom_highbd_12_sub_pixel_avg_variance64x64,
2475 aom_highbd_sad64x64x4d_bits12,
2476 aom_highbd_dist_wtd_sad64x64_avg_bits12,
2477 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x64);
2478
2479 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2480 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2481 aom_highbd_12_sub_pixel_variance16x16,
2482 aom_highbd_12_sub_pixel_avg_variance16x16,
2483 aom_highbd_sad16x16x4d_bits12,
2484 aom_highbd_dist_wtd_sad16x16_avg_bits12,
2485 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x16);
2486
2487 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2488 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2489 aom_highbd_12_sub_pixel_variance16x8,
2490 aom_highbd_12_sub_pixel_avg_variance16x8,
2491 aom_highbd_sad16x8x4d_bits12,
2492 aom_highbd_dist_wtd_sad16x8_avg_bits12,
2493 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x8);
2494
2495 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2496 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2497 aom_highbd_12_sub_pixel_variance8x16,
2498 aom_highbd_12_sub_pixel_avg_variance8x16,
2499 aom_highbd_sad8x16x4d_bits12,
2500 aom_highbd_dist_wtd_sad8x16_avg_bits12,
2501 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x16);
2502
2503 HIGHBD_BFP(
2504 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2505 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
2506 aom_highbd_12_sub_pixel_avg_variance8x8,
2507 aom_highbd_sad8x8x4d_bits12, aom_highbd_dist_wtd_sad8x8_avg_bits12,
2508 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x8);
2509
2510 HIGHBD_BFP(
2511 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2512 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2513 aom_highbd_12_sub_pixel_avg_variance8x4,
2514 aom_highbd_sad8x4x4d_bits12, aom_highbd_dist_wtd_sad8x4_avg_bits12,
2515 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x4);
2516
2517 HIGHBD_BFP(
2518 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2519 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2520 aom_highbd_12_sub_pixel_avg_variance4x8,
2521 aom_highbd_sad4x8x4d_bits12, aom_highbd_dist_wtd_sad4x8_avg_bits12,
2522 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x8);
2523
2524 HIGHBD_BFP(
2525 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2526 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
2527 aom_highbd_12_sub_pixel_avg_variance4x4,
2528 aom_highbd_sad4x4x4d_bits12, aom_highbd_dist_wtd_sad4x4_avg_bits12,
2529 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x4);
2530
2531 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2532 aom_highbd_sad128x128_avg_bits12,
2533 aom_highbd_12_variance128x128,
2534 aom_highbd_12_sub_pixel_variance128x128,
2535 aom_highbd_12_sub_pixel_avg_variance128x128,
2536 aom_highbd_sad128x128x4d_bits12,
2537 aom_highbd_dist_wtd_sad128x128_avg_bits12,
2538 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x128);
2539
2540 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2541 aom_highbd_sad128x64_avg_bits12,
2542 aom_highbd_12_variance128x64,
2543 aom_highbd_12_sub_pixel_variance128x64,
2544 aom_highbd_12_sub_pixel_avg_variance128x64,
2545 aom_highbd_sad128x64x4d_bits12,
2546 aom_highbd_dist_wtd_sad128x64_avg_bits12,
2547 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x64);
2548
2549 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2550 aom_highbd_sad64x128_avg_bits12,
2551 aom_highbd_12_variance64x128,
2552 aom_highbd_12_sub_pixel_variance64x128,
2553 aom_highbd_12_sub_pixel_avg_variance64x128,
2554 aom_highbd_sad64x128x4d_bits12,
2555 aom_highbd_dist_wtd_sad64x128_avg_bits12,
2556 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x128);
2557
2558 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2559 aom_highbd_12_masked_sub_pixel_variance128x128)
2560 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2561 aom_highbd_12_masked_sub_pixel_variance128x64)
2562 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2563 aom_highbd_12_masked_sub_pixel_variance64x128)
2564 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2565 aom_highbd_12_masked_sub_pixel_variance64x64)
2566 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2567 aom_highbd_12_masked_sub_pixel_variance64x32)
2568 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2569 aom_highbd_12_masked_sub_pixel_variance32x64)
2570 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2571 aom_highbd_12_masked_sub_pixel_variance32x32)
2572 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2573 aom_highbd_12_masked_sub_pixel_variance32x16)
2574 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2575 aom_highbd_12_masked_sub_pixel_variance16x32)
2576 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2577 aom_highbd_12_masked_sub_pixel_variance16x16)
2578 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2579 aom_highbd_12_masked_sub_pixel_variance8x16)
2580 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2581 aom_highbd_12_masked_sub_pixel_variance16x8)
2582 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2583 aom_highbd_12_masked_sub_pixel_variance8x8)
2584 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2585 aom_highbd_12_masked_sub_pixel_variance4x8)
2586 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2587 aom_highbd_12_masked_sub_pixel_variance8x4)
2588 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2589 aom_highbd_12_masked_sub_pixel_variance4x4)
2590 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2591 aom_highbd_12_masked_sub_pixel_variance64x16)
2592 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2593 aom_highbd_12_masked_sub_pixel_variance16x64)
2594 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2595 aom_highbd_12_masked_sub_pixel_variance32x8)
2596 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2597 aom_highbd_12_masked_sub_pixel_variance8x32)
2598 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2599 aom_highbd_12_masked_sub_pixel_variance16x4)
2600 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2601 aom_highbd_12_masked_sub_pixel_variance4x16)
2602 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2603 aom_highbd_12_obmc_variance128x128,
2604 aom_highbd_12_obmc_sub_pixel_variance128x128)
2605 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2606 aom_highbd_12_obmc_variance128x64,
2607 aom_highbd_12_obmc_sub_pixel_variance128x64)
2608 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2609 aom_highbd_12_obmc_variance64x128,
2610 aom_highbd_12_obmc_sub_pixel_variance64x128)
2611 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2612 aom_highbd_12_obmc_variance64x64,
2613 aom_highbd_12_obmc_sub_pixel_variance64x64)
2614 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2615 aom_highbd_12_obmc_variance64x32,
2616 aom_highbd_12_obmc_sub_pixel_variance64x32)
2617 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2618 aom_highbd_12_obmc_variance32x64,
2619 aom_highbd_12_obmc_sub_pixel_variance32x64)
2620 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2621 aom_highbd_12_obmc_variance32x32,
2622 aom_highbd_12_obmc_sub_pixel_variance32x32)
2623 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2624 aom_highbd_12_obmc_variance32x16,
2625 aom_highbd_12_obmc_sub_pixel_variance32x16)
2626 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2627 aom_highbd_12_obmc_variance16x32,
2628 aom_highbd_12_obmc_sub_pixel_variance16x32)
2629 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2630 aom_highbd_12_obmc_variance16x16,
2631 aom_highbd_12_obmc_sub_pixel_variance16x16)
2632 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2633 aom_highbd_12_obmc_variance8x16,
2634 aom_highbd_12_obmc_sub_pixel_variance8x16)
2635 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2636 aom_highbd_12_obmc_variance16x8,
2637 aom_highbd_12_obmc_sub_pixel_variance16x8)
2638 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2639 aom_highbd_12_obmc_variance8x8,
2640 aom_highbd_12_obmc_sub_pixel_variance8x8)
2641 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2642 aom_highbd_12_obmc_variance4x8,
2643 aom_highbd_12_obmc_sub_pixel_variance4x8)
2644 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2645 aom_highbd_12_obmc_variance8x4,
2646 aom_highbd_12_obmc_sub_pixel_variance8x4)
2647 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2648 aom_highbd_12_obmc_variance4x4,
2649 aom_highbd_12_obmc_sub_pixel_variance4x4)
2650 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2651 aom_highbd_12_obmc_variance64x16,
2652 aom_highbd_12_obmc_sub_pixel_variance64x16)
2653 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2654 aom_highbd_12_obmc_variance16x64,
2655 aom_highbd_12_obmc_sub_pixel_variance16x64)
2656 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2657 aom_highbd_12_obmc_variance32x8,
2658 aom_highbd_12_obmc_sub_pixel_variance32x8)
2659 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2660 aom_highbd_12_obmc_variance8x32,
2661 aom_highbd_12_obmc_sub_pixel_variance8x32)
2662 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2663 aom_highbd_12_obmc_variance16x4,
2664 aom_highbd_12_obmc_sub_pixel_variance16x4)
2665 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2666 aom_highbd_12_obmc_variance4x16,
2667 aom_highbd_12_obmc_sub_pixel_variance4x16)
2668 break;
2669
2670 default:
2671 assert(0 &&
2672 "cm->seq_params.bit_depth should be AOM_BITS_8, "
2673 "AOM_BITS_10 or AOM_BITS_12");
2674 }
2675 }
2676 }
2677 #endif // CONFIG_AV1_HIGHBITDEPTH
2678
realloc_segmentation_maps(AV1_COMP * cpi)2679 static void realloc_segmentation_maps(AV1_COMP *cpi) {
2680 AV1_COMMON *const cm = &cpi->common;
2681 CommonModeInfoParams *const mi_params = &cm->mi_params;
2682
2683 // Create the encoder segmentation map and set all entries to 0
2684 aom_free(cpi->enc_seg.map);
2685 CHECK_MEM_ERROR(cm, cpi->enc_seg.map,
2686 aom_calloc(mi_params->mi_rows * mi_params->mi_cols, 1));
2687
2688 // Create a map used for cyclic background refresh.
2689 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
2690 CHECK_MEM_ERROR(
2691 cm, cpi->cyclic_refresh,
2692 av1_cyclic_refresh_alloc(mi_params->mi_rows, mi_params->mi_cols));
2693
2694 // Create a map used to mark inactive areas.
2695 aom_free(cpi->active_map.map);
2696 CHECK_MEM_ERROR(cm, cpi->active_map.map,
2697 aom_calloc(mi_params->mi_rows * mi_params->mi_cols, 1));
2698 }
2699
set_tpl_stats_block_size(int width,int height,uint8_t * block_mis_log2)2700 static AOM_INLINE void set_tpl_stats_block_size(int width, int height,
2701 uint8_t *block_mis_log2) {
2702 const int is_720p_or_larger = AOMMIN(width, height) >= 720;
2703
2704 // 0: 4x4, 1: 8x8, 2: 16x16
2705 *block_mis_log2 = is_720p_or_larger ? 2 : 1;
2706 }
2707
av1_alloc_compound_type_rd_buffers(AV1_COMMON * const cm,CompoundTypeRdBuffers * const bufs)2708 void av1_alloc_compound_type_rd_buffers(AV1_COMMON *const cm,
2709 CompoundTypeRdBuffers *const bufs) {
2710 CHECK_MEM_ERROR(
2711 cm, bufs->pred0,
2712 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred0)));
2713 CHECK_MEM_ERROR(
2714 cm, bufs->pred1,
2715 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred1)));
2716 CHECK_MEM_ERROR(
2717 cm, bufs->residual1,
2718 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->residual1)));
2719 CHECK_MEM_ERROR(
2720 cm, bufs->diff10,
2721 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->diff10)));
2722 CHECK_MEM_ERROR(cm, bufs->tmp_best_mask_buf,
2723 (uint8_t *)aom_malloc(2 * MAX_SB_SQUARE *
2724 sizeof(*bufs->tmp_best_mask_buf)));
2725 }
2726
av1_release_compound_type_rd_buffers(CompoundTypeRdBuffers * const bufs)2727 void av1_release_compound_type_rd_buffers(CompoundTypeRdBuffers *const bufs) {
2728 aom_free(bufs->pred0);
2729 aom_free(bufs->pred1);
2730 aom_free(bufs->residual1);
2731 aom_free(bufs->diff10);
2732 aom_free(bufs->tmp_best_mask_buf);
2733 av1_zero(*bufs); // Set all pointers to NULL for safety.
2734 }
2735
config_target_level(AV1_COMP * const cpi,AV1_LEVEL target_level,int tier)2736 static void config_target_level(AV1_COMP *const cpi, AV1_LEVEL target_level,
2737 int tier) {
2738 aom_clear_system_state();
2739
2740 AV1EncoderConfig *const oxcf = &cpi->oxcf;
2741 SequenceHeader *const seq_params = &cpi->common.seq_params;
2742
2743 // Adjust target bitrate to be no larger than 70% of level limit.
2744 const BITSTREAM_PROFILE profile = seq_params->profile;
2745 const double level_bitrate_limit =
2746 av1_get_max_bitrate_for_level(target_level, tier, profile);
2747 const int64_t max_bitrate = (int64_t)(level_bitrate_limit * 0.70);
2748 oxcf->target_bandwidth = AOMMIN(oxcf->target_bandwidth, max_bitrate);
2749 // Also need to update cpi->twopass.bits_left.
2750 TWO_PASS *const twopass = &cpi->twopass;
2751 FIRSTPASS_STATS *stats = twopass->stats_buf_ctx->total_stats;
2752 if (stats != NULL)
2753 cpi->twopass.bits_left =
2754 (int64_t)(stats->duration * cpi->oxcf.target_bandwidth / 10000000.0);
2755
2756 // Adjust max over-shoot percentage.
2757 oxcf->over_shoot_pct = 0;
2758
2759 // Adjust max quantizer.
2760 oxcf->worst_allowed_q = 255;
2761
2762 // Adjust number of tiles and tile columns to be under level limit.
2763 int max_tiles, max_tile_cols;
2764 av1_get_max_tiles_for_level(target_level, &max_tiles, &max_tile_cols);
2765 while (oxcf->tile_columns > 0 && (1 << oxcf->tile_columns) > max_tile_cols) {
2766 --oxcf->tile_columns;
2767 }
2768 const int tile_cols = (1 << oxcf->tile_columns);
2769 while (oxcf->tile_rows > 0 &&
2770 tile_cols * (1 << oxcf->tile_rows) > max_tiles) {
2771 --oxcf->tile_rows;
2772 }
2773
2774 // Adjust min compression ratio.
2775 const int still_picture = seq_params->still_picture;
2776 const double min_cr =
2777 av1_get_min_cr_for_level(target_level, tier, still_picture);
2778 oxcf->min_cr = AOMMAX(oxcf->min_cr, (unsigned int)(min_cr * 100));
2779 }
2780
av1_change_config(struct AV1_COMP * cpi,const AV1EncoderConfig * oxcf)2781 void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2782 AV1_COMMON *const cm = &cpi->common;
2783 SequenceHeader *const seq_params = &cm->seq_params;
2784 const int num_planes = av1_num_planes(cm);
2785 RATE_CONTROL *const rc = &cpi->rc;
2786 MACROBLOCK *const x = &cpi->td.mb;
2787 AV1LevelParams *const level_params = &cpi->level_params;
2788
2789 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2790 seq_params->bit_depth = oxcf->bit_depth;
2791 seq_params->color_primaries = oxcf->color_primaries;
2792 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2793 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2794 seq_params->monochrome = oxcf->monochrome;
2795 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2796 seq_params->color_range = oxcf->color_range;
2797
2798 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2799 seq_params->bit_depth <= AOM_BITS_10));
2800
2801 seq_params->timing_info_present = oxcf->timing_info_present;
2802 seq_params->timing_info.num_units_in_display_tick =
2803 oxcf->timing_info.num_units_in_display_tick;
2804 seq_params->timing_info.time_scale = oxcf->timing_info.time_scale;
2805 seq_params->timing_info.equal_picture_interval =
2806 oxcf->timing_info.equal_picture_interval;
2807 seq_params->timing_info.num_ticks_per_picture =
2808 oxcf->timing_info.num_ticks_per_picture;
2809
2810 seq_params->display_model_info_present_flag =
2811 oxcf->display_model_info_present_flag;
2812 seq_params->decoder_model_info_present_flag =
2813 oxcf->decoder_model_info_present_flag;
2814 if (oxcf->decoder_model_info_present_flag) {
2815 // set the decoder model parameters in schedule mode
2816 seq_params->decoder_model_info.num_units_in_decoding_tick =
2817 oxcf->buffer_model.num_units_in_decoding_tick;
2818 cm->buffer_removal_time_present = 1;
2819 av1_set_aom_dec_model_info(&seq_params->decoder_model_info);
2820 av1_set_dec_model_op_parameters(&seq_params->op_params[0]);
2821 } else if (seq_params->timing_info_present &&
2822 seq_params->timing_info.equal_picture_interval &&
2823 !seq_params->decoder_model_info_present_flag) {
2824 // set the decoder model parameters in resource availability mode
2825 av1_set_resource_availability_parameters(&seq_params->op_params[0]);
2826 } else {
2827 seq_params->op_params[0].initial_display_delay =
2828 10; // Default value (not signaled)
2829 }
2830
2831 update_film_grain_parameters(cpi, oxcf);
2832
2833 cpi->oxcf = *oxcf;
2834 cpi->superres_mode = oxcf->superres_mode; // default
2835 x->e_mbd.bd = (int)seq_params->bit_depth;
2836 x->e_mbd.global_motion = cm->global_motion;
2837
2838 memcpy(level_params->target_seq_level_idx, cpi->oxcf.target_seq_level_idx,
2839 sizeof(level_params->target_seq_level_idx));
2840 level_params->keep_level_stats = 0;
2841 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
2842 if (level_params->target_seq_level_idx[i] <= SEQ_LEVELS) {
2843 level_params->keep_level_stats |= 1u << i;
2844 if (!level_params->level_info[i]) {
2845 CHECK_MEM_ERROR(cm, level_params->level_info[i],
2846 aom_calloc(1, sizeof(*level_params->level_info[i])));
2847 }
2848 }
2849 }
2850
2851 // TODO(huisu@): level targeting currently only works for the 0th operating
2852 // point, so scalable coding is not supported yet.
2853 if (level_params->target_seq_level_idx[0] < SEQ_LEVELS) {
2854 // Adjust encoder config in order to meet target level.
2855 config_target_level(cpi, level_params->target_seq_level_idx[0],
2856 seq_params->tier[0]);
2857 }
2858
2859 if ((has_no_stats_stage(cpi)) && (oxcf->rc_mode == AOM_Q)) {
2860 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2861 } else {
2862 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2863 }
2864
2865 cpi->refresh_golden_frame = 0;
2866 cpi->refresh_bwd_ref_frame = 0;
2867
2868 cm->features.refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2869 ? REFRESH_FRAME_CONTEXT_DISABLED
2870 : REFRESH_FRAME_CONTEXT_BACKWARD;
2871 if (oxcf->large_scale_tile)
2872 cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
2873
2874 if (x->palette_buffer == NULL) {
2875 CHECK_MEM_ERROR(cm, x->palette_buffer,
2876 aom_memalign(16, sizeof(*x->palette_buffer)));
2877 }
2878
2879 if (x->comp_rd_buffer.pred0 == NULL) {
2880 av1_alloc_compound_type_rd_buffers(cm, &x->comp_rd_buffer);
2881 }
2882
2883 if (x->tmp_conv_dst == NULL) {
2884 CHECK_MEM_ERROR(
2885 cm, x->tmp_conv_dst,
2886 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
2887 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
2888 }
2889 for (int i = 0; i < 2; ++i) {
2890 if (x->tmp_obmc_bufs[i] == NULL) {
2891 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
2892 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
2893 sizeof(*x->tmp_obmc_bufs[i])));
2894 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
2895 }
2896 }
2897
2898 av1_reset_segment_features(cm);
2899
2900 av1_set_high_precision_mv(cpi, 1, 0);
2901
2902 set_rc_buffer_sizes(rc, &cpi->oxcf);
2903
2904 // Under a configuration change, where maximum_buffer_size may change,
2905 // keep buffer level clipped to the maximum allowed buffer size.
2906 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2907 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
2908
2909 // Set up frame rate and related parameters rate control values.
2910 av1_new_framerate(cpi, cpi->framerate);
2911
2912 // Set absolute upper and lower quality limits
2913 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2914 rc->best_quality = cpi->oxcf.best_allowed_q;
2915
2916 cm->features.interp_filter =
2917 oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
2918 cm->features.switchable_motion_mode = 1;
2919
2920 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2921 cm->render_width = cpi->oxcf.render_width;
2922 cm->render_height = cpi->oxcf.render_height;
2923 } else {
2924 cm->render_width = cpi->oxcf.width;
2925 cm->render_height = cpi->oxcf.height;
2926 }
2927 cm->width = cpi->oxcf.width;
2928 cm->height = cpi->oxcf.height;
2929
2930 int sb_size = seq_params->sb_size;
2931 // Superblock size should not be updated after the first key frame.
2932 if (!cpi->seq_params_locked) {
2933 set_sb_size(&cm->seq_params, select_sb_size(cpi));
2934 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
2935 seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
2936 }
2937
2938 if (cpi->initial_width || sb_size != seq_params->sb_size) {
2939 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
2940 seq_params->sb_size != sb_size) {
2941 av1_free_context_buffers(cm);
2942 av1_free_pc_tree(cpi, &cpi->td, num_planes, (BLOCK_SIZE)sb_size);
2943 alloc_compressor_data(cpi);
2944 realloc_segmentation_maps(cpi);
2945 cpi->initial_width = cpi->initial_height = 0;
2946 }
2947 }
2948 update_frame_size(cpi);
2949
2950 rc->is_src_frame_alt_ref = 0;
2951
2952 set_tile_info(cpi);
2953
2954 if (!cpi->svc.external_ref_frame_config)
2955 cpi->ext_flags.refresh_frame_flags_pending = 0;
2956 cpi->ext_flags.refresh_frame_context_pending = 0;
2957
2958 #if CONFIG_AV1_HIGHBITDEPTH
2959 highbd_set_var_fns(cpi);
2960 #endif
2961
2962 // Init sequence level coding tools
2963 // This should not be called after the first key frame.
2964 if (!cpi->seq_params_locked) {
2965 seq_params->operating_points_cnt_minus_1 =
2966 (cm->number_spatial_layers > 1 || cm->number_temporal_layers > 1)
2967 ? cm->number_spatial_layers * cm->number_temporal_layers - 1
2968 : 0;
2969 init_seq_coding_tools(&cm->seq_params, cm, oxcf, cpi->use_svc);
2970 }
2971
2972 if (cpi->use_svc)
2973 av1_update_layer_context_change_config(cpi, oxcf->target_bandwidth);
2974 }
2975
setup_tpl_buffers(AV1_COMMON * const cm,TplParams * const tpl_data)2976 static INLINE void setup_tpl_buffers(AV1_COMMON *const cm,
2977 TplParams *const tpl_data) {
2978 CommonModeInfoParams *const mi_params = &cm->mi_params;
2979 set_tpl_stats_block_size(cm->width, cm->height,
2980 &tpl_data->tpl_stats_block_mis_log2);
2981 const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
2982
2983 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
2984 const int mi_cols =
2985 ALIGN_POWER_OF_TWO(mi_params->mi_cols, MAX_MIB_SIZE_LOG2);
2986 const int mi_rows =
2987 ALIGN_POWER_OF_TWO(mi_params->mi_rows, MAX_MIB_SIZE_LOG2);
2988
2989 tpl_data->tpl_stats_buffer[frame].is_valid = 0;
2990 tpl_data->tpl_stats_buffer[frame].width = mi_cols >> block_mis_log2;
2991 tpl_data->tpl_stats_buffer[frame].height = mi_rows >> block_mis_log2;
2992 tpl_data->tpl_stats_buffer[frame].stride =
2993 tpl_data->tpl_stats_buffer[frame].width;
2994 tpl_data->tpl_stats_buffer[frame].mi_rows = mi_params->mi_rows;
2995 tpl_data->tpl_stats_buffer[frame].mi_cols = mi_params->mi_cols;
2996 }
2997
2998 for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
2999 CHECK_MEM_ERROR(
3000 cm, tpl_data->tpl_stats_pool[frame],
3001 aom_calloc(tpl_data->tpl_stats_buffer[frame].width *
3002 tpl_data->tpl_stats_buffer[frame].height,
3003 sizeof(*tpl_data->tpl_stats_buffer[frame].tpl_stats_ptr)));
3004 if (aom_alloc_frame_buffer(
3005 &tpl_data->tpl_rec_pool[frame], cm->width, cm->height,
3006 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
3007 cm->seq_params.use_highbitdepth, AOM_ENC_NO_SCALE_BORDER,
3008 cm->features.byte_alignment))
3009 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3010 "Failed to allocate frame buffer");
3011 }
3012
3013 tpl_data->tpl_frame = &tpl_data->tpl_stats_buffer[REF_FRAMES + 1];
3014 }
3015
init_frame_info(FRAME_INFO * frame_info,const AV1_COMMON * const cm)3016 static INLINE void init_frame_info(FRAME_INFO *frame_info,
3017 const AV1_COMMON *const cm) {
3018 const CommonModeInfoParams *const mi_params = &cm->mi_params;
3019 const SequenceHeader *const seq_params = &cm->seq_params;
3020 frame_info->frame_width = cm->width;
3021 frame_info->frame_height = cm->height;
3022 frame_info->mi_cols = mi_params->mi_cols;
3023 frame_info->mi_rows = mi_params->mi_rows;
3024 frame_info->mb_cols = mi_params->mb_cols;
3025 frame_info->mb_rows = mi_params->mb_rows;
3026 frame_info->num_mbs = mi_params->MBs;
3027 frame_info->bit_depth = seq_params->bit_depth;
3028 frame_info->subsampling_x = seq_params->subsampling_x;
3029 frame_info->subsampling_y = seq_params->subsampling_y;
3030 }
3031
av1_create_compressor(AV1EncoderConfig * oxcf,BufferPool * const pool,FIRSTPASS_STATS * frame_stats_buf,COMPRESSOR_STAGE stage,int num_lap_buffers,int lap_lag_in_frames,STATS_BUFFER_CTX * stats_buf_context)3032 AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf, BufferPool *const pool,
3033 FIRSTPASS_STATS *frame_stats_buf,
3034 COMPRESSOR_STAGE stage, int num_lap_buffers,
3035 int lap_lag_in_frames,
3036 STATS_BUFFER_CTX *stats_buf_context) {
3037 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
3038 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
3039
3040 if (!cm) return NULL;
3041
3042 av1_zero(*cpi);
3043
3044 // The jmp_buf is valid only for the duration of the function that calls
3045 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
3046 // before it returns.
3047 if (setjmp(cm->error.jmp)) {
3048 cm->error.setjmp = 0;
3049 av1_remove_compressor(cpi);
3050 return 0;
3051 }
3052
3053 cm->error.setjmp = 1;
3054 cpi->lap_enabled = num_lap_buffers > 0;
3055 cpi->compressor_stage = stage;
3056
3057 CommonModeInfoParams *const mi_params = &cm->mi_params;
3058 mi_params->free_mi = enc_free_mi;
3059 mi_params->setup_mi = enc_setup_mi;
3060 mi_params->set_mb_mi = (oxcf->pass == 1 || cpi->compressor_stage == LAP_STAGE)
3061 ? stat_stage_set_mb_mi
3062 : enc_set_mb_mi;
3063
3064 mi_params->mi_alloc_bsize = BLOCK_4X4;
3065
3066 CHECK_MEM_ERROR(cm, cm->fc,
3067 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
3068 CHECK_MEM_ERROR(
3069 cm, cm->default_frame_context,
3070 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
3071 memset(cm->fc, 0, sizeof(*cm->fc));
3072 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
3073
3074 cpi->common.buffer_pool = pool;
3075
3076 init_config(cpi, oxcf);
3077 if (cpi->compressor_stage == LAP_STAGE) {
3078 cpi->oxcf.lag_in_frames = lap_lag_in_frames;
3079 }
3080
3081 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
3082
3083 cpi->rc.enable_scenecut_detection = 1;
3084 if (cpi->lap_enabled &&
3085 (num_lap_buffers < (MAX_GF_LENGTH_LAP + SCENE_CUT_KEY_TEST_INTERVAL + 1)))
3086 cpi->rc.enable_scenecut_detection = 0;
3087 init_frame_info(&cpi->frame_info, cm);
3088
3089 cm->current_frame.frame_number = 0;
3090 cm->current_frame_id = -1;
3091 cpi->seq_params_locked = 0;
3092 cpi->partition_search_skippable_frame = 0;
3093 cpi->tile_data = NULL;
3094 cpi->last_show_frame_buf = NULL;
3095 realloc_segmentation_maps(cpi);
3096
3097 cpi->refresh_alt_ref_frame = 0;
3098
3099 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
3100 #if CONFIG_INTERNAL_STATS
3101 cpi->b_calculate_blockiness = 1;
3102 cpi->b_calculate_consistency = 1;
3103 cpi->total_inconsistency = 0;
3104 cpi->psnr.worst = 100.0;
3105 cpi->worst_ssim = 100.0;
3106
3107 cpi->count = 0;
3108 cpi->bytes = 0;
3109 #if CONFIG_SPEED_STATS
3110 cpi->tx_search_count = 0;
3111 #endif // CONFIG_SPEED_STATS
3112
3113 if (cpi->b_calculate_psnr) {
3114 cpi->total_sq_error = 0;
3115 cpi->total_samples = 0;
3116 cpi->tot_recode_hits = 0;
3117 cpi->summed_quality = 0;
3118 cpi->summed_weights = 0;
3119 }
3120
3121 cpi->fastssim.worst = 100.0;
3122 cpi->psnrhvs.worst = 100.0;
3123
3124 if (cpi->b_calculate_blockiness) {
3125 cpi->total_blockiness = 0;
3126 cpi->worst_blockiness = 0.0;
3127 }
3128
3129 if (cpi->b_calculate_consistency) {
3130 CHECK_MEM_ERROR(
3131 cm, cpi->ssim_vars,
3132 aom_malloc(sizeof(*cpi->ssim_vars) * 4 * cpi->common.mi_params.mi_rows *
3133 cpi->common.mi_params.mi_cols));
3134 cpi->worst_consistency = 100.0;
3135 }
3136 #endif
3137 #if CONFIG_ENTROPY_STATS
3138 av1_zero(aggregate_fc);
3139 #endif // CONFIG_ENTROPY_STATS
3140
3141 cpi->time_stamps.first_ever = INT64_MAX;
3142
3143 #ifdef OUTPUT_YUV_SKINMAP
3144 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
3145 #endif
3146 #ifdef OUTPUT_YUV_REC
3147 yuv_rec_file = fopen("rec.yuv", "wb");
3148 #endif
3149
3150 assert(MAX_LAP_BUFFERS >= MAX_LAG_BUFFERS);
3151 int size = get_stats_buf_size(num_lap_buffers, MAX_LAG_BUFFERS);
3152 for (int i = 0; i < size; i++)
3153 cpi->twopass.frame_stats_arr[i] = &frame_stats_buf[i];
3154
3155 cpi->twopass.stats_buf_ctx = stats_buf_context;
3156 cpi->twopass.stats_in = cpi->twopass.stats_buf_ctx->stats_in_start;
3157
3158 #if !CONFIG_REALTIME_ONLY
3159 if (is_stat_consumption_stage(cpi)) {
3160 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
3161 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
3162
3163 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];
3170
3171 av1_init_second_pass(cpi);
3172 } else {
3173 av1_init_single_pass_lap(cpi);
3174 }
3175 }
3176 #endif
3177
3178 int sb_mi_size = av1_get_sb_mi_size(cm);
3179
3180 CHECK_MEM_ERROR(
3181 cm, cpi->td.mb.above_pred_buf,
3182 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
3183 sizeof(*cpi->td.mb.above_pred_buf)));
3184 CHECK_MEM_ERROR(
3185 cm, cpi->td.mb.left_pred_buf,
3186 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
3187 sizeof(*cpi->td.mb.left_pred_buf)));
3188
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
3193 CHECK_MEM_ERROR(
3194 cm, cpi->td.mb.inter_modes_info,
3195 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
3196
3197 for (int x = 0; x < 2; x++)
3198 for (int y = 0; y < 2; y++)
3199 CHECK_MEM_ERROR(
3200 cm, cpi->td.mb.intrabc_hash_info.hash_value_buffer[x][y],
3201 (uint32_t *)aom_malloc(
3202 AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
3203 sizeof(*cpi->td.mb.intrabc_hash_info.hash_value_buffer[0][0])));
3204
3205 cpi->td.mb.intrabc_hash_info.g_crc_initialized = 0;
3206
3207 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
3208 (int32_t *)aom_memalign(
3209 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
3210
3211 CHECK_MEM_ERROR(cm, cpi->td.mb.mbmi_ext,
3212 aom_calloc(sb_mi_size, sizeof(*cpi->td.mb.mbmi_ext)));
3213
3214 av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
3215 av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
3216
3217 {
3218 const int bsize = BLOCK_16X16;
3219 const int w = mi_size_wide[bsize];
3220 const int h = mi_size_high[bsize];
3221 const int num_cols = (mi_params->mi_cols + w - 1) / w;
3222 const int num_rows = (mi_params->mi_rows + h - 1) / h;
3223 CHECK_MEM_ERROR(cm, cpi->tpl_rdmult_scaling_factors,
3224 aom_calloc(num_rows * num_cols,
3225 sizeof(*cpi->tpl_rdmult_scaling_factors)));
3226 CHECK_MEM_ERROR(cm, cpi->tpl_sb_rdmult_scaling_factors,
3227 aom_calloc(num_rows * num_cols,
3228 sizeof(*cpi->tpl_sb_rdmult_scaling_factors)));
3229 }
3230
3231 {
3232 const int bsize = BLOCK_16X16;
3233 const int w = mi_size_wide[bsize];
3234 const int h = mi_size_high[bsize];
3235 const int num_cols = (mi_params->mi_cols + w - 1) / w;
3236 const int num_rows = (mi_params->mi_rows + h - 1) / h;
3237 CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors,
3238 aom_calloc(num_rows * num_cols,
3239 sizeof(*cpi->ssim_rdmult_scaling_factors)));
3240 }
3241
3242 #if CONFIG_TUNE_VMAF
3243 {
3244 const int bsize = BLOCK_64X64;
3245 const int w = mi_size_wide[bsize];
3246 const int h = mi_size_high[bsize];
3247 const int num_cols = (mi_params->mi_cols + w - 1) / w;
3248 const int num_rows = (mi_params->mi_rows + h - 1) / h;
3249 CHECK_MEM_ERROR(cm, cpi->vmaf_rdmult_scaling_factors,
3250 aom_calloc(num_rows * num_cols,
3251 sizeof(*cpi->vmaf_rdmult_scaling_factors)));
3252 cpi->last_frame_unsharp_amount = 0.0;
3253 }
3254 #endif
3255
3256 if (!is_stat_generation_stage(cpi)) {
3257 setup_tpl_buffers(cm, &cpi->tpl_data);
3258 }
3259
3260 #if CONFIG_COLLECT_PARTITION_STATS == 2
3261 av1_zero(cpi->partition_stats);
3262 #endif
3263
3264 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
3265 cpi->fn_ptr[BT].sdf = SDF; \
3266 cpi->fn_ptr[BT].sdaf = SDAF; \
3267 cpi->fn_ptr[BT].vf = VF; \
3268 cpi->fn_ptr[BT].svf = SVF; \
3269 cpi->fn_ptr[BT].svaf = SVAF; \
3270 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
3271 cpi->fn_ptr[BT].jsdaf = JSDAF; \
3272 cpi->fn_ptr[BT].jsvaf = JSVAF;
3273
3274 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
3275 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
3276 aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
3277 aom_dist_wtd_sub_pixel_avg_variance4x16)
3278
3279 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
3280 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
3281 aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
3282 aom_dist_wtd_sub_pixel_avg_variance16x4)
3283
3284 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
3285 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
3286 aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
3287 aom_dist_wtd_sub_pixel_avg_variance8x32)
3288
3289 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
3290 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
3291 aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
3292 aom_dist_wtd_sub_pixel_avg_variance32x8)
3293
3294 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
3295 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
3296 aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
3297 aom_dist_wtd_sub_pixel_avg_variance16x64)
3298
3299 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
3300 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
3301 aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
3302 aom_dist_wtd_sub_pixel_avg_variance64x16)
3303
3304 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
3305 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
3306 aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
3307 aom_dist_wtd_sub_pixel_avg_variance128x128)
3308
3309 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
3310 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
3311 aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
3312 aom_dist_wtd_sub_pixel_avg_variance128x64)
3313
3314 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
3315 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
3316 aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
3317 aom_dist_wtd_sub_pixel_avg_variance64x128)
3318
3319 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
3320 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
3321 aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
3322 aom_dist_wtd_sub_pixel_avg_variance32x16)
3323
3324 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
3325 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
3326 aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
3327 aom_dist_wtd_sub_pixel_avg_variance16x32)
3328
3329 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
3330 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
3331 aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
3332 aom_dist_wtd_sub_pixel_avg_variance64x32)
3333
3334 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
3335 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
3336 aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
3337 aom_dist_wtd_sub_pixel_avg_variance32x64)
3338
3339 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
3340 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
3341 aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
3342 aom_dist_wtd_sub_pixel_avg_variance32x32)
3343
3344 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
3345 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
3346 aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
3347 aom_dist_wtd_sub_pixel_avg_variance64x64)
3348
3349 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
3350 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
3351 aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
3352 aom_dist_wtd_sub_pixel_avg_variance16x16)
3353
3354 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
3355 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
3356 aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
3357 aom_dist_wtd_sub_pixel_avg_variance16x8)
3358
3359 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
3360 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
3361 aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
3362 aom_dist_wtd_sub_pixel_avg_variance8x16)
3363
3364 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
3365 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
3366 aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
3367
3368 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
3369 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
3370 aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
3371
3372 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
3373 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
3374 aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
3375
3376 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
3377 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
3378 aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
3379
3380 #define OBFP(BT, OSDF, OVF, OSVF) \
3381 cpi->fn_ptr[BT].osdf = OSDF; \
3382 cpi->fn_ptr[BT].ovf = OVF; \
3383 cpi->fn_ptr[BT].osvf = OSVF;
3384
3385 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
3386 aom_obmc_sub_pixel_variance128x128)
3387 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
3388 aom_obmc_sub_pixel_variance128x64)
3389 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
3390 aom_obmc_sub_pixel_variance64x128)
3391 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
3392 aom_obmc_sub_pixel_variance64x64)
3393 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
3394 aom_obmc_sub_pixel_variance64x32)
3395 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
3396 aom_obmc_sub_pixel_variance32x64)
3397 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
3398 aom_obmc_sub_pixel_variance32x32)
3399 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
3400 aom_obmc_sub_pixel_variance32x16)
3401 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
3402 aom_obmc_sub_pixel_variance16x32)
3403 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
3404 aom_obmc_sub_pixel_variance16x16)
3405 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
3406 aom_obmc_sub_pixel_variance16x8)
3407 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
3408 aom_obmc_sub_pixel_variance8x16)
3409 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
3410 aom_obmc_sub_pixel_variance8x8)
3411 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
3412 aom_obmc_sub_pixel_variance4x8)
3413 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
3414 aom_obmc_sub_pixel_variance8x4)
3415 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
3416 aom_obmc_sub_pixel_variance4x4)
3417 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
3418 aom_obmc_sub_pixel_variance4x16)
3419 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
3420 aom_obmc_sub_pixel_variance16x4)
3421 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
3422 aom_obmc_sub_pixel_variance8x32)
3423 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
3424 aom_obmc_sub_pixel_variance32x8)
3425 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
3426 aom_obmc_sub_pixel_variance16x64)
3427 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
3428 aom_obmc_sub_pixel_variance64x16)
3429
3430 #define MBFP(BT, MCSDF, MCSVF) \
3431 cpi->fn_ptr[BT].msdf = MCSDF; \
3432 cpi->fn_ptr[BT].msvf = MCSVF;
3433
3434 MBFP(BLOCK_128X128, aom_masked_sad128x128,
3435 aom_masked_sub_pixel_variance128x128)
3436 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
3437 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
3438 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
3439 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
3440 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
3441 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
3442 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
3443 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
3444 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
3445 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
3446 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
3447 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
3448 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
3449 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
3450 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
3451
3452 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
3453
3454 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
3455
3456 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
3457
3458 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
3459
3460 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
3461
3462 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
3463
3464 #if CONFIG_AV1_HIGHBITDEPTH
3465 highbd_set_var_fns(cpi);
3466 #endif
3467
3468 /* av1_init_quantizer() is first called here. Add check in
3469 * av1_frame_init_quantizer() so that av1_init_quantizer is only
3470 * called later when needed. This will avoid unnecessary calls of
3471 * av1_init_quantizer() for every frame.
3472 */
3473 av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
3474 cm->seq_params.bit_depth);
3475 av1_qm_init(&cm->quant_params, av1_num_planes(cm));
3476
3477 av1_loop_filter_init(cm);
3478 cm->superres_scale_denominator = SCALE_NUMERATOR;
3479 cm->superres_upscaled_width = oxcf->width;
3480 cm->superres_upscaled_height = oxcf->height;
3481 av1_loop_restoration_precal();
3482
3483 cm->error.setjmp = 0;
3484
3485 return cpi;
3486 }
3487
3488 #if CONFIG_INTERNAL_STATS
3489 #define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
3490
3491 #define SNPRINT2(H, T, V) \
3492 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
3493 #endif // CONFIG_INTERNAL_STATS
3494
av1_remove_compressor(AV1_COMP * cpi)3495 void av1_remove_compressor(AV1_COMP *cpi) {
3496 AV1_COMMON *cm;
3497 TplParams *const tpl_data = &cpi->tpl_data;
3498 int t;
3499
3500 if (!cpi) return;
3501
3502 cm = &cpi->common;
3503 const int num_planes = av1_num_planes(cm);
3504
3505 if (cm->current_frame.frame_number > 0) {
3506 #if CONFIG_ENTROPY_STATS
3507 if (!is_stat_generation_stage(cpi)) {
3508 fprintf(stderr, "Writing counts.stt\n");
3509 FILE *f = fopen("counts.stt", "wb");
3510 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
3511 fclose(f);
3512 }
3513 #endif // CONFIG_ENTROPY_STATS
3514 #if CONFIG_INTERNAL_STATS
3515 aom_clear_system_state();
3516
3517 if (!is_stat_generation_stage(cpi)) {
3518 char headings[512] = { 0 };
3519 char results[512] = { 0 };
3520 FILE *f = fopen("opsnr.stt", "a");
3521 double time_encoded =
3522 (cpi->time_stamps.prev_end_seen - cpi->time_stamps.first_ever) /
3523 10000000.000;
3524 double total_encode_time =
3525 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3526 const double dr =
3527 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3528 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3529 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3530 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3531
3532 if (cpi->b_calculate_psnr) {
3533 const double total_psnr = aom_sse_to_psnr(
3534 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3535 const double total_ssim =
3536 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3537 snprintf(headings, sizeof(headings),
3538 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
3539 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
3540 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
3541 "AVPsrnY\tAPsnrCb\tAPsnrCr");
3542 snprintf(results, sizeof(results),
3543 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3544 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3545 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3546 "%7.3f\t%7.3f\t%7.3f",
3547 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
3548 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
3549 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
3550 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
3551 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
3552 cpi->psnr.stat[STAT_Y] / cpi->count,
3553 cpi->psnr.stat[STAT_U] / cpi->count,
3554 cpi->psnr.stat[STAT_V] / cpi->count);
3555
3556 if (cpi->b_calculate_blockiness) {
3557 SNPRINT(headings, "\t Block\tWstBlck");
3558 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3559 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3560 }
3561
3562 if (cpi->b_calculate_consistency) {
3563 double consistency =
3564 aom_sse_to_psnr((double)cpi->total_samples, peak,
3565 (double)cpi->total_inconsistency);
3566
3567 SNPRINT(headings, "\tConsist\tWstCons");
3568 SNPRINT2(results, "\t%7.3f", consistency);
3569 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3570 }
3571
3572 SNPRINT(headings, "\t Time\tRcErr\tAbsErr");
3573 SNPRINT2(results, "\t%8.0f", total_encode_time);
3574 SNPRINT2(results, "\t%7.2f", rate_err);
3575 SNPRINT2(results, "\t%7.2f", fabs(rate_err));
3576
3577 fprintf(f, "%s\tAPsnr611\n", headings);
3578 fprintf(f, "%s\t%7.3f\n", results,
3579 (6 * cpi->psnr.stat[STAT_Y] + cpi->psnr.stat[STAT_U] +
3580 cpi->psnr.stat[STAT_V]) /
3581 (cpi->count * 8));
3582 }
3583
3584 fclose(f);
3585 }
3586 #endif // CONFIG_INTERNAL_STATS
3587 #if CONFIG_SPEED_STATS
3588 if (!is_stat_generation_stage(cpi)) {
3589 fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
3590 }
3591 #endif // CONFIG_SPEED_STATS
3592
3593 #if CONFIG_COLLECT_PARTITION_STATS == 2
3594 if (!is_stat_generation_stage(cpi)) {
3595 av1_print_partition_stats(&cpi->partition_stats);
3596 }
3597 #endif
3598 }
3599
3600 for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
3601 aom_free(tpl_data->tpl_stats_pool[frame]);
3602 aom_free_frame_buffer(&tpl_data->tpl_rec_pool[frame]);
3603 }
3604
3605 for (t = cpi->num_workers - 1; t >= 0; --t) {
3606 AVxWorker *const worker = &cpi->workers[t];
3607 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3608
3609 // Deallocate allocated threads.
3610 aom_get_worker_interface()->end(worker);
3611
3612 // Deallocate allocated thread data.
3613 aom_free(thread_data->td->tctx);
3614 if (t > 0) {
3615 aom_free(thread_data->td->palette_buffer);
3616 aom_free(thread_data->td->tmp_conv_dst);
3617 av1_release_compound_type_rd_buffers(&thread_data->td->comp_rd_buffer);
3618 for (int j = 0; j < 2; ++j) {
3619 aom_free(thread_data->td->tmp_obmc_bufs[j]);
3620 }
3621 aom_free(thread_data->td->above_pred_buf);
3622 aom_free(thread_data->td->left_pred_buf);
3623 aom_free(thread_data->td->wsrc_buf);
3624 aom_free(thread_data->td->vt64x64);
3625
3626 aom_free(thread_data->td->inter_modes_info);
3627 for (int x = 0; x < 2; x++) {
3628 for (int y = 0; y < 2; y++) {
3629 aom_free(thread_data->td->hash_value_buffer[x][y]);
3630 thread_data->td->hash_value_buffer[x][y] = NULL;
3631 }
3632 }
3633 aom_free(thread_data->td->mask_buf);
3634 aom_free(thread_data->td->counts);
3635 av1_free_pc_tree(cpi, thread_data->td, num_planes,
3636 cm->seq_params.sb_size);
3637 aom_free(thread_data->td->mbmi_ext);
3638 aom_free(thread_data->td);
3639 }
3640 }
3641 #if CONFIG_MULTITHREAD
3642 if (cpi->row_mt_mutex_ != NULL) {
3643 pthread_mutex_destroy(cpi->row_mt_mutex_);
3644 aom_free(cpi->row_mt_mutex_);
3645 }
3646 #endif
3647 av1_row_mt_mem_dealloc(cpi);
3648 aom_free(cpi->tile_thr_data);
3649 aom_free(cpi->workers);
3650
3651 if (cpi->num_workers > 1) {
3652 av1_loop_filter_dealloc(&cpi->lf_row_sync);
3653 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
3654 }
3655
3656 dealloc_compressor_data(cpi);
3657
3658 #if CONFIG_INTERNAL_STATS
3659 aom_free(cpi->ssim_vars);
3660 cpi->ssim_vars = NULL;
3661 #endif // CONFIG_INTERNAL_STATS
3662
3663 av1_remove_common(cm);
3664 #if CONFIG_HTB_TRELLIS
3665 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
3666 #endif // CONFIG_HTB_TRELLIS
3667 av1_free_ref_frame_buffers(cm->buffer_pool);
3668
3669 aom_free(cpi);
3670
3671 #ifdef OUTPUT_YUV_SKINMAP
3672 fclose(yuv_skinmap_file);
3673 #endif
3674 #ifdef OUTPUT_YUV_REC
3675 fclose(yuv_rec_file);
3676 #endif
3677 }
3678
generate_psnr_packet(AV1_COMP * cpi)3679 static void generate_psnr_packet(AV1_COMP *cpi) {
3680 struct aom_codec_cx_pkt pkt;
3681 int i;
3682 PSNR_STATS psnr;
3683 #if CONFIG_AV1_HIGHBITDEPTH
3684 const uint32_t in_bit_depth = cpi->oxcf.input_bit_depth;
3685 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
3686 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
3687 bit_depth, in_bit_depth);
3688 #else
3689 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
3690 #endif
3691
3692 for (i = 0; i < 4; ++i) {
3693 pkt.data.psnr.samples[i] = psnr.samples[i];
3694 pkt.data.psnr.sse[i] = psnr.sse[i];
3695 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3696 }
3697 pkt.kind = AOM_CODEC_PSNR_PKT;
3698 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
3699 }
3700
av1_use_as_reference(int * ext_ref_frame_flags,int ref_frame_flags)3701 int av1_use_as_reference(int *ext_ref_frame_flags, int ref_frame_flags) {
3702 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3703
3704 *ext_ref_frame_flags = ref_frame_flags;
3705 return 0;
3706 }
3707
av1_copy_reference_enc(AV1_COMP * cpi,int idx,YV12_BUFFER_CONFIG * sd)3708 int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3709 AV1_COMMON *const cm = &cpi->common;
3710 const int num_planes = av1_num_planes(cm);
3711 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
3712 if (cfg) {
3713 aom_yv12_copy_frame(cfg, sd, num_planes);
3714 return 0;
3715 } else {
3716 return -1;
3717 }
3718 }
3719
av1_set_reference_enc(AV1_COMP * cpi,int idx,YV12_BUFFER_CONFIG * sd)3720 int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3721 AV1_COMMON *const cm = &cpi->common;
3722 const int num_planes = av1_num_planes(cm);
3723 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
3724 if (cfg) {
3725 aom_yv12_copy_frame(sd, cfg, num_planes);
3726 return 0;
3727 } else {
3728 return -1;
3729 }
3730 }
3731
av1_update_entropy(bool * ext_refresh_frame_context,bool * ext_refresh_frame_context_pending,bool update)3732 int av1_update_entropy(bool *ext_refresh_frame_context,
3733 bool *ext_refresh_frame_context_pending, bool update) {
3734 *ext_refresh_frame_context = update;
3735 *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.
aom_write_yuv_frame_420(YV12_BUFFER_CONFIG * s,FILE * f)3744 void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
3745 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
3771 #ifdef OUTPUT_YUV_REC
aom_write_one_yuv_frame(AV1_COMMON * cm,YV12_BUFFER_CONFIG * s)3772 void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
3773 uint8_t *src = s->y_buffer;
3774 int h = cm->height;
3775 if (yuv_rec_file == NULL) return;
3776 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 }
3803
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
3829 #define GM_RECODE_LOOP_NUM4X4_FACTOR 192
recode_loop_test_global_motion(WarpedMotionParams * const global_motion,const int * const global_motion_used,int * const gm_params_cost)3830 static int recode_loop_test_global_motion(
3831 WarpedMotionParams *const global_motion,
3832 const int *const global_motion_used, int *const gm_params_cost) {
3833 int i;
3834 int recode = 0;
3835 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3836 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;
3842 recode = 1;
3843 // 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?
3846 }
3847 }
3848 return recode;
3849 }
3850
3851 // Function to test for conditions that indicate we should loop
3852 // back and recode a frame.
recode_loop_test(AV1_COMP * cpi,int high_limit,int low_limit,int q,int maxq,int minq)3853 static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3854 int maxq, int minq) {
3855 const RATE_CONTROL *const rc = &cpi->rc;
3856 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
3857 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) ||
3861 (cpi->sf.hl_sf.recode_loop == ALLOW_RECODE) ||
3862 (frame_is_kfgfarf &&
3863 (cpi->sf.hl_sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
3864 // 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;
3868 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
3869 // 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
scale_references(AV1_COMP * cpi)3880 static void scale_references(AV1_COMP *cpi) {
3881 AV1_COMMON *cm = &cpi->common;
3882 const int num_planes = av1_num_planes(cm);
3883 MV_REFERENCE_FRAME ref_frame;
3884
3885 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3886 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
3887 if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) {
3888 BufferPool *const pool = cm->buffer_pool;
3889 const YV12_BUFFER_CONFIG *const ref =
3890 get_ref_frame_yv12_buf(cm, ref_frame);
3891
3892 if (ref == NULL) {
3893 cpi->scaled_ref_buf[ref_frame - 1] = NULL;
3894 continue;
3895 }
3896
3897 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3898 // 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(
3906 &ref_fb->buf, AOM_BORDER_IN_PIXELS,
3907 cm->features.byte_alignment, num_planes) != 0) {
3908 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3909 "Failed to allocate frame buffer");
3910 }
3911 }
3912 int force_scaling = 0;
3913 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) {
3917 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3918 "Unable to find free frame buffer");
3919 }
3920 force_scaling = 1;
3921 new_fb = &pool->frame_bufs[new_fb_idx];
3922 }
3923
3924 if (force_scaling || new_fb->buf.y_crop_width != cm->width ||
3925 new_fb->buf.y_crop_height != cm->height) {
3926 if (aom_realloc_frame_buffer(
3927 &new_fb->buf, cm->width, cm->height,
3928 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
3929 cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
3930 cm->features.byte_alignment, NULL, NULL, NULL)) {
3931 if (force_scaling) {
3932 // Release the reference acquired in the get_free_fb() call above.
3933 --new_fb->ref_count;
3934 }
3935 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3936 "Failed to allocate frame buffer");
3937 }
3938 av1_resize_and_extend_frame(
3939 ref, &new_fb->buf, (int)cm->seq_params.bit_depth, num_planes);
3940 cpi->scaled_ref_buf[ref_frame - 1] = new_fb;
3941 alloc_frame_mvs(cm, new_fb);
3942 }
3943 } else {
3944 RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
3945 buf->buf.y_crop_width = ref->y_crop_width;
3946 buf->buf.y_crop_height = ref->y_crop_height;
3947 cpi->scaled_ref_buf[ref_frame - 1] = buf;
3948 ++buf->ref_count;
3949 }
3950 } else {
3951 if (!has_no_stats_stage(cpi)) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
3952 }
3953 }
3954 }
3955
release_scaled_references(AV1_COMP * cpi)3956 static void release_scaled_references(AV1_COMP *cpi) {
3957 // TODO(isbs): only refresh the necessary frames, rather than all of them
3958 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3959 RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
3960 if (buf != NULL) {
3961 --buf->ref_count;
3962 cpi->scaled_ref_buf[i] = NULL;
3963 }
3964 }
3965 }
3966
set_mv_search_params(AV1_COMP * cpi)3967 static void set_mv_search_params(AV1_COMP *cpi) {
3968 const AV1_COMMON *const cm = &cpi->common;
3969 MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params;
3970 const int max_mv_def = AOMMAX(cm->width, cm->height);
3971
3972 // Default based on max resolution.
3973 mv_search_params->mv_step_param = av1_init_search_range(max_mv_def);
3974
3975 if (cpi->sf.mv_sf.auto_mv_step_size) {
3976 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.
3979 mv_search_params->max_mv_magnitude = max_mv_def;
3980 } else {
3981 // Use cpi->max_mv_magnitude == -1 to exclude first pass case.
3982 if (cm->show_frame && mv_search_params->max_mv_magnitude != -1) {
3983 // 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.
3986 mv_search_params->mv_step_param = av1_init_search_range(
3987 AOMMIN(max_mv_def, 2 * mv_search_params->max_mv_magnitude));
3988 }
3989 mv_search_params->max_mv_magnitude = -1;
3990 }
3991 }
3992 }
3993
av1_set_screen_content_options(const AV1_COMP * cpi,FeatureFlags * features)3994 void av1_set_screen_content_options(const AV1_COMP *cpi,
3995 FeatureFlags *features) {
3996 const AV1_COMMON *const cm = &cpi->common;
3997
3998 if (cm->seq_params.force_screen_content_tools != 2) {
3999 features->allow_screen_content_tools = features->allow_intrabc =
4000 cm->seq_params.force_screen_content_tools;
4001 return;
4002 }
4003
4004 if (cpi->oxcf.content == AOM_CONTENT_SCREEN) {
4005 features->allow_screen_content_tools = features->allow_intrabc = 1;
4006 return;
4007 }
4008
4009 // Estimate if the source frame is screen content, based on the portion of
4010 // blocks that have few luma colors.
4011 const uint8_t *src = cpi->unfiltered_source->y_buffer;
4012 assert(src != NULL);
4013 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;
4017 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.
4052 features->allow_screen_content_tools =
4053 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.
4056 features->allow_intrabc = features->allow_screen_content_tools &&
4057 counts_2 * blk_h * blk_w * 12 > width * height;
4058 }
4059
set_size_independent_vars(AV1_COMP * cpi)4060 static void set_size_independent_vars(AV1_COMP *cpi) {
4061 int i;
4062 AV1_COMMON *const cm = &cpi->common;
4063 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
4064 cm->global_motion[i] = default_warp_params;
4065 }
4066 cpi->gm_info.search_done = 0;
4067
4068 av1_set_speed_features_framesize_independent(cpi, cpi->speed);
4069 av1_set_rd_speed_thresholds(cpi);
4070 cm->features.interp_filter = SWITCHABLE;
4071 cm->features.switchable_motion_mode = 1;
4072 }
4073
4074 #if !CONFIG_REALTIME_ONLY
av1_get_gfu_boost_projection_factor(double min_factor,double max_factor,int frame_count)4075 double av1_get_gfu_boost_projection_factor(double min_factor, double max_factor,
4076 int frame_count) {
4077 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
get_gfu_boost_from_r0_lap(double min_factor,double max_factor,double r0,int frames_to_key)4084 static int get_gfu_boost_from_r0_lap(double min_factor, double max_factor,
4085 double r0, int frames_to_key) {
4086 double factor = av1_get_gfu_boost_projection_factor(min_factor, max_factor,
4087 frames_to_key);
4088 const int boost = (int)rint(factor / r0);
4089 return boost;
4090 }
4091
av1_get_kf_boost_projection_factor(int frame_count)4092 double av1_get_kf_boost_projection_factor(int frame_count) {
4093 double factor = sqrt((double)frame_count);
4094 factor = AOMMIN(factor, 10.0);
4095 factor = AOMMAX(factor, 4.0);
4096 factor = (75.0 + 14.0 * factor);
4097 return factor;
4098 }
4099
get_kf_boost_from_r0(double r0,int frames_to_key)4100 static int get_kf_boost_from_r0(double r0, int frames_to_key) {
4101 double factor = av1_get_kf_boost_projection_factor(frames_to_key);
4102 const int boost = (int)rint(factor / r0);
4103 return boost;
4104 }
4105 #endif
4106
4107 #define MIN_BOOST_COMBINE_FACTOR 4.0
4108 #define MAX_BOOST_COMBINE_FACTOR 12.0
combine_prior_with_tpl_boost(double min_factor,double max_factor,int prior_boost,int tpl_boost,int frames_to_key)4109 int combine_prior_with_tpl_boost(double min_factor, double max_factor,
4110 int prior_boost, int tpl_boost,
4111 int frames_to_key) {
4112 double factor = sqrt((double)frames_to_key);
4113 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);
4119 return boost;
4120 }
4121
4122 #if !CONFIG_REALTIME_ONLY
process_tpl_stats_frame(AV1_COMP * cpi)4123 static void process_tpl_stats_frame(AV1_COMP *cpi) {
4124 const GF_GROUP *const gf_group = &cpi->gf_group;
4125 AV1_COMMON *const cm = &cpi->common;
4126
4127 assert(IMPLIES(gf_group->size > 0, gf_group->index < gf_group->size));
4128
4129 const int tpl_idx = gf_group->index;
4130 TplParams *const tpl_data = &cpi->tpl_data;
4131 TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
4132 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
4133
4134 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;
4140 const int step = 1 << tpl_data->tpl_stats_block_mis_log2;
4141 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
4142
4143 for (int row = 0; row < cm->mi_params.mi_rows; row += step) {
4144 for (int col = 0; col < mi_cols_sr; col += step) {
4145 TplDepStats *this_stats = &tpl_stats[av1_tpl_ptr_pos(
4146 row, col, tpl_stride, tpl_data->tpl_stats_block_mis_log2)];
4147 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;
4153 mc_count_base += this_stats->mc_count;
4154 mc_saved_base += this_stats->mc_saved;
4155 }
4156 }
4157
4158 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;
4163 if (is_frame_arf_and_tpl_eligible(gf_group)) {
4164 cpi->rd.arf_r0 = cpi->rd.r0;
4165 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);
4170 // printf("old boost %d new boost %d\n", cpi->rc.gfu_boost,
4171 // gfu_boost);
4172 cpi->rc.gfu_boost = combine_prior_with_tpl_boost(
4173 min_boost_factor, MAX_BOOST_COMBINE_FACTOR, cpi->rc.gfu_boost,
4174 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 }
4181 } else if (frame_is_intra_only(cm)) {
4182 // 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);
4188 if (cpi->lap_enabled) {
4189 cpi->rc.kf_boost = combine_prior_with_tpl_boost(
4190 MIN_BOOST_COMBINE_FACTOR, MAX_BOOST_COMBINE_FACTOR,
4191 cpi->rc.kf_boost, kf_boost,
4192 cpi->rc.num_stats_used_for_kf_boost);
4193 } else {
4194 cpi->rc.kf_boost = combine_prior_with_tpl_boost(
4195 MIN_BOOST_COMBINE_FACTOR, MAX_BOOST_COMBINE_FACTOR,
4196 cpi->rc.kf_boost, kf_boost, cpi->rc.frames_to_key);
4197 }
4198 }
4199 }
4200 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);
4204 aom_clear_system_state();
4205 }
4206 }
4207 }
4208 #endif // !CONFIG_REALTIME_ONLY
4209
set_size_dependent_vars(AV1_COMP * cpi,int * q,int * bottom_index,int * top_index)4210 static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
4211 int *top_index) {
4212 AV1_COMMON *const cm = &cpi->common;
4213
4214 // Setup variables that depend on the dimensions of the frame.
4215 av1_set_speed_features_framesize_dependent(cpi, cpi->speed);
4216
4217 #if !CONFIG_REALTIME_ONLY
4218 if (cpi->oxcf.enable_tpl_model && is_frame_tpl_eligible(cpi)) {
4219 process_tpl_stats_frame(cpi);
4220 av1_tpl_rdmult_setup(cpi);
4221 }
4222 #endif
4223
4224 // Decide q and q bounds.
4225 *q = av1_rc_pick_q_and_bounds(cpi, &cpi->rc, cm->width, cm->height,
4226 cpi->gf_group.index, bottom_index, top_index);
4227
4228 // 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.
4232 if (is_stat_consumption_stage_twopass(cpi) &&
4233 cpi->sf.hl_sf.static_segmentation)
4234 configure_static_seg_features(cpi);
4235 }
4236
init_motion_estimation(AV1_COMP * cpi)4237 static void init_motion_estimation(AV1_COMP *cpi) {
4238 AV1_COMMON *const cm = &cpi->common;
4239 MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params;
4240 const int y_stride = cpi->scaled_source.y_stride;
4241 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;
4246 int fpf_y_stride = cm->cur_frame != NULL ? cm->cur_frame->buf.y_stride
4247 : cpi->scaled_source.y_stride;
4248
4249 // Update if ss_cfg is uninitialized or the current frame has a new stride
4250 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);
4254
4255 if (!should_update) {
4256 return;
4257 }
4258
4259 if (cpi->sf.mv_sf.search_method == DIAMOND) {
4260 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],
4263 y_stride_src);
4264 } else {
4265 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);
4269 }
4270 av1_init_motion_fpf(&mv_search_params->ss_cfg[SS_CFG_FPF], fpf_y_stride);
4271 }
4272
4273 #define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
set_restoration_unit_size(int width,int height,int sx,int sy,RestorationInfo * rst)4274 static void set_restoration_unit_size(int width, int height, int sx, int sy,
4275 RestorationInfo *rst) {
4276 (void)width;
4277 (void)height;
4278 (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
4286 if (width * height > 352 * 288)
4287 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
4288 else
4289 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
4290 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
4291 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
4292 }
4293
init_ref_frame_bufs(AV1_COMP * cpi)4294 static void init_ref_frame_bufs(AV1_COMP *cpi) {
4295 AV1_COMMON *const cm = &cpi->common;
4296 int i;
4297 BufferPool *const pool = cm->buffer_pool;
4298 cm->cur_frame = NULL;
4299 for (i = 0; i < REF_FRAMES; ++i) {
4300 cm->ref_frame_map[i] = NULL;
4301 }
4302 for (i = 0; i < FRAME_BUFFERS; ++i) {
4303 pool->frame_bufs[i].ref_count = 0;
4304 }
4305 }
4306
av1_check_initial_width(AV1_COMP * cpi,int use_highbitdepth,int subsampling_x,int subsampling_y)4307 void av1_check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
4308 int subsampling_x, int subsampling_y) {
4309 AV1_COMMON *const cm = &cpi->common;
4310 SequenceHeader *const seq_params = &cm->seq_params;
4311
4312 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;
4318
4319 av1_set_speed_features_framesize_independent(cpi, cpi->oxcf.speed);
4320 av1_set_speed_features_framesize_dependent(cpi, cpi->oxcf.speed);
4321
4322 if (!is_stat_generation_stage(cpi)) {
4323 alloc_altref_frame_buffer(cpi);
4324 alloc_util_frame_buffers(cpi);
4325 }
4326 init_ref_frame_bufs(cpi);
4327
4328 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
4329
4330 cpi->initial_width = cm->width;
4331 cpi->initial_height = cm->height;
4332 cpi->initial_mbs = cm->mi_params.MBs;
4333 }
4334 }
4335
4336 // Returns 1 if the assigned width or height was <= 0.
av1_set_size_literal(AV1_COMP * cpi,int width,int height)4337 int av1_set_size_literal(AV1_COMP *cpi, int width, int height) {
4338 AV1_COMMON *cm = &cpi->common;
4339 const int num_planes = av1_num_planes(cm);
4340 av1_check_initial_width(cpi, cm->seq_params.use_highbitdepth,
4341 cm->seq_params.subsampling_x,
4342 cm->seq_params.subsampling_y);
4343
4344 if (width <= 0 || height <= 0) return 1;
4345
4346 cm->width = width;
4347 cm->height = height;
4348
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);
4352 av1_free_pc_tree(cpi, &cpi->td, num_planes, cm->seq_params.sb_size);
4353 alloc_compressor_data(cpi);
4354 realloc_segmentation_maps(cpi);
4355 cpi->initial_width = cpi->initial_height = 0;
4356 }
4357 update_frame_size(cpi);
4358
4359 return 0;
4360 }
4361
av1_set_frame_size(AV1_COMP * cpi,int width,int height)4362 void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
4363 AV1_COMMON *const cm = &cpi->common;
4364 const SequenceHeader *const seq_params = &cm->seq_params;
4365 const int num_planes = av1_num_planes(cm);
4366 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
4367 int ref_frame;
4368
4369 if (width != cm->width || height != cm->height) {
4370 // There has been a change in the encoded frame size
4371 av1_set_size_literal(cpi, width, height);
4372 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
4373 cm->features.all_lossless =
4374 cm->features.coded_lossless && !av1_superres_scaled(cm);
4375 }
4376 set_mv_search_params(cpi);
4377
4378 if (is_stat_consumption_stage(cpi)) {
4379 av1_set_target_rate(cpi, cm->width, cm->height);
4380 }
4381
4382 alloc_frame_mvs(cm, cm->cur_frame);
4383
4384 // Allocate above context buffers
4385 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)))
4393 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
4394 "Failed to allocate context buffers");
4395 }
4396
4397 // Reset the frame pointers to the current frame size.
4398 if (aom_realloc_frame_buffer(
4399 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
4400 seq_params->subsampling_y, seq_params->use_highbitdepth,
4401 cpi->oxcf.border_in_pixels, cm->features.byte_alignment, NULL, NULL,
4402 NULL))
4403 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
4404 "Failed to allocate frame buffer");
4405
4406 const int frame_width = cm->superres_upscaled_width;
4407 const int frame_height = cm->superres_upscaled_height;
4408 set_restoration_unit_size(frame_width, frame_height,
4409 seq_params->subsampling_x,
4410 seq_params->subsampling_y, cm->rst_info);
4411 for (int i = 0; i < num_planes; ++i)
4412 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
4413
4414 av1_alloc_restoration_buffers(cm);
4415 if (!is_stat_generation_stage(cpi)) alloc_util_frame_buffers(cpi);
4416 init_motion_estimation(cpi);
4417
4418 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4419 RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
4420 if (buf != NULL) {
4421 struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
4422 av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
4423 buf->buf.y_crop_height, cm->width,
4424 cm->height);
4425 if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
4426 }
4427 }
4428
4429 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
4430 cm->width, cm->height);
4431
4432 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
4433 }
4434
calculate_next_resize_scale(const AV1_COMP * cpi)4435 static 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;
4439 if (is_stat_generation_stage(cpi)) return SCALE_NUMERATOR;
4440 uint8_t new_denom = SCALE_NUMERATOR;
4441
4442 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
4443 switch (oxcf->resize_mode) {
4444 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
4445 case RESIZE_FIXED:
4446 if (cpi->common.current_frame.frame_type == KEY_FRAME)
4447 new_denom = oxcf->resize_kf_scale_denominator;
4448 else
4449 new_denom = oxcf->resize_scale_denominator;
4450 break;
4451 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
4452 default: assert(0);
4453 }
4454 return new_denom;
4455 }
4456
4457 #if CONFIG_SUPERRES_IN_RECODE
superres_in_recode_allowed(const AV1_COMP * const cpi)4458 static 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
4467 #define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO 0.012
4468 #define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME 0.008
4469 #define SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME 0.008
4470 #define SUPERRES_ENERGY_BY_AC_THRESH 0.2
4471
get_energy_by_q2_thresh(const GF_GROUP * gf_group,const RATE_CONTROL * rc)4472 static 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 }
4488
get_superres_denom_from_qindex_energy(int qindex,double * energy,double threshq,double threshp)4489 static uint8_t get_superres_denom_from_qindex_energy(int qindex, double *energy,
4490 double threshq,
4491 double threshp) {
4492 const double q = av1_convert_qindex_to_q(qindex, AOM_BITS_8);
4493 const double tq = threshq * q * q;
4494 const double tp = threshp * energy[1];
4495 const double thresh = AOMMIN(tq, tp);
4496 int k;
4497 for (k = SCALE_NUMERATOR * 2; k > SCALE_NUMERATOR; --k) {
4498 if (energy[k - 1] > thresh) break;
4499 }
4500 return 3 * SCALE_NUMERATOR - k;
4501 }
4502
get_superres_denom_for_qindex(const AV1_COMP * cpi,int qindex,int sr_kf,int sr_arf)4503 static 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
4518 double energy[16];
4519 analyze_hor_freq(cpi, energy);
4520
4521 const double energy_by_q2_thresh =
4522 get_energy_by_q2_thresh(gf_group, &cpi->rc);
4523 int denom = get_superres_denom_from_qindex_energy(
4524 qindex, energy, energy_by_q2_thresh, SUPERRES_ENERGY_BY_AC_THRESH);
4525 /*
4526 printf("\nenergy = [");
4527 for (int k = 1; k < 16; ++k) printf("%f, ", energy[k]);
4528 printf("]\n");
4529 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);
4534 */
4535 #if CONFIG_SUPERRES_IN_RECODE
4536 if (superres_in_recode_allowed(cpi)) {
4537 assert(cpi->superres_mode != SUPERRES_NONE);
4538 // 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
4543 return denom;
4544 }
4545
4546 // 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
calculate_next_superres_scale(AV1_COMP * cpi)4550 static 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;
4554 if (is_stat_generation_stage(cpi)) return SCALE_NUMERATOR;
4555 uint8_t new_denom = SCALE_NUMERATOR;
4556
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));
4563 // 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));
4567
4568 // 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) {
4571 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
4572 case SUPERRES_FIXED:
4573 if (cpi->common.current_frame.frame_type == KEY_FRAME)
4574 new_denom = oxcf->superres_kf_scale_denominator;
4575 else
4576 new_denom = oxcf->superres_scale_denominator;
4577 break;
4578 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
4579 case SUPERRES_QTHRESH: {
4580 // Do not use superres when screen content tools are used.
4581 if (cpi->common.features.allow_screen_content_tools) break;
4582 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4583 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
4584
4585 // Now decide the use of superres based on 'q'.
4586 int bottom_index, top_index;
4587 const int q = av1_rc_pick_q_and_bounds(
4588 cpi, &cpi->rc, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
4589 &bottom_index, &top_index);
4590
4591 const int qthresh = (frame_is_intra_only(&cpi->common))
4592 ? oxcf->superres_kf_qthresh
4593 : oxcf->superres_qthresh;
4594 if (q <= qthresh) {
4595 new_denom = SCALE_NUMERATOR;
4596 } else {
4597 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
4598 }
4599 break;
4600 }
4601 case SUPERRES_AUTO: {
4602 // Do not use superres when screen content tools are used.
4603 if (cpi->common.features.allow_screen_content_tools) break;
4604 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4605 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
4606
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(
4610 cpi, &cpi->rc, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
4611 &bottom_index, &top_index);
4612
4613 const int qthresh = 128;
4614 if (q <= qthresh) {
4615 new_denom = SCALE_NUMERATOR;
4616 } else {
4617 #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
4623 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
4624 #endif // SUPERRES_RECODE_ALL_RATIOS
4625 }
4626 break;
4627 }
4628 default: assert(0);
4629 }
4630 return new_denom;
4631 }
4632
dimension_is_ok(int orig_dim,int resized_dim,int denom)4633 static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4634 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4635 }
4636
dimensions_are_ok(int owidth,int oheight,size_params_type * rsz)4637 static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
4638 // 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);
4641 }
4642
validate_size_scales(RESIZE_MODE resize_mode,SUPERRES_MODE superres_mode,int owidth,int oheight,size_params_type * rsz)4643 static int validate_size_scales(RESIZE_MODE resize_mode,
4644 SUPERRES_MODE superres_mode, int owidth,
4645 int oheight, size_params_type *rsz) {
4646 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
4647 return 1;
4648 }
4649
4650 // Calculate current resize scale.
4651 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
4655 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
4656 // 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;
4661 }
4662 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
4663 // Alter resize scale as needed to enforce conformity.
4664 resize_denom =
4665 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
4666 rsz->resize_width = owidth;
4667 rsz->resize_height = oheight;
4668 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4669 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 }
4678 }
4679 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
4680 // Alter both resize and superres scales as needed to enforce conformity.
4681 do {
4682 if (resize_denom > rsz->superres_denom)
4683 --resize_denom;
4684 else
4685 --rsz->superres_denom;
4686 rsz->resize_width = owidth;
4687 rsz->resize_height = oheight;
4688 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4689 resize_denom);
4690 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4691 (resize_denom > SCALE_NUMERATOR ||
4692 rsz->superres_denom > SCALE_NUMERATOR));
4693 } else { // We are allowed to alter neither resize scale nor superres
4694 // scale.
4695 return 0;
4696 }
4697 return dimensions_are_ok(owidth, oheight, rsz);
4698 }
4699
4700 // Calculates resize and superres params for next frame
calculate_next_size_params(AV1_COMP * cpi)4701 static size_params_type calculate_next_size_params(AV1_COMP *cpi) {
4702 const AV1EncoderConfig *oxcf = &cpi->oxcf;
4703 ResizePendingParams *resize_pending_params = &cpi->resize_pending_params;
4704 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
4705 int resize_denom = SCALE_NUMERATOR;
4706 if (has_no_stats_stage(cpi) && cpi->use_svc &&
4707 cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1) {
4708 rsz.resize_width = cpi->common.width;
4709 rsz.resize_height = cpi->common.height;
4710 return rsz;
4711 }
4712 if (is_stat_generation_stage(cpi)) return rsz;
4713 if (resize_pending_params->width && resize_pending_params->height) {
4714 rsz.resize_width = resize_pending_params->width;
4715 rsz.resize_height = resize_pending_params->height;
4716 resize_pending_params->width = resize_pending_params->height = 0;
4717 } else {
4718 resize_denom = calculate_next_resize_scale(cpi);
4719 rsz.resize_width = oxcf->width;
4720 rsz.resize_height = oxcf->height;
4721 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
4722 resize_denom);
4723 }
4724 rsz.superres_denom = calculate_next_superres_scale(cpi);
4725 if (!validate_size_scales(oxcf->resize_mode, cpi->superres_mode, oxcf->width,
4726 oxcf->height, &rsz))
4727 assert(0 && "Invalid scale parameters");
4728 return rsz;
4729 }
4730
setup_frame_size_from_params(AV1_COMP * cpi,const size_params_type * rsz)4731 static void setup_frame_size_from_params(AV1_COMP *cpi,
4732 const size_params_type *rsz) {
4733 int encode_width = rsz->resize_width;
4734 int encode_height = rsz->resize_height;
4735
4736 AV1_COMMON *cm = &cpi->common;
4737 cm->superres_upscaled_width = encode_width;
4738 cm->superres_upscaled_height = encode_height;
4739 cm->superres_scale_denominator = rsz->superres_denom;
4740 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4741 rsz->superres_denom);
4742 av1_set_frame_size(cpi, encode_width, encode_height);
4743 }
4744
av1_setup_frame_size(AV1_COMP * cpi)4745 void av1_setup_frame_size(AV1_COMP *cpi) {
4746 AV1_COMMON *cm = &cpi->common;
4747 // Reset superres params from previous frame.
4748 cm->superres_scale_denominator = SCALE_NUMERATOR;
4749 const size_params_type rsz = calculate_next_size_params(cpi);
4750 setup_frame_size_from_params(cpi, &rsz);
4751
4752 assert(av1_is_min_tile_width_satisfied(cm));
4753 }
4754
superres_post_encode(AV1_COMP * cpi)4755 static void superres_post_encode(AV1_COMP *cpi) {
4756 AV1_COMMON *cm = &cpi->common;
4757 const int num_planes = av1_num_planes(cm);
4758
4759 if (!av1_superres_scaled(cm)) return;
4760
4761 assert(cpi->oxcf.enable_superres);
4762 assert(!is_lossless_requested(&cpi->oxcf));
4763 assert(!cm->features.all_lossless);
4764
4765 av1_superres_upscale(cm, NULL);
4766
4767 // If regular resizing is occurring the source will need to be downscaled to
4768 // match the upscaled superres resolution. Otherwise the original source is
4769 // used.
4770 if (!av1_resize_scaled(cm)) {
4771 cpi->source = cpi->unscaled_source;
4772 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4773 } else {
4774 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4775 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
4776 // Do downscale. cm->(width|height) has been updated by
4777 // av1_superres_upscale
4778 if (aom_realloc_frame_buffer(
4779 &cpi->scaled_source, cm->superres_upscaled_width,
4780 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4781 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
4782 AOM_BORDER_IN_PIXELS, cm->features.byte_alignment, NULL, NULL,
4783 NULL))
4784 aom_internal_error(
4785 &cm->error, AOM_CODEC_MEM_ERROR,
4786 "Failed to reallocate scaled source buffer for superres");
4787 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4788 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
4789 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
4790 (int)cm->seq_params.bit_depth, num_planes);
4791 cpi->source = &cpi->scaled_source;
4792 }
4793 }
4794
cdef_restoration_frame(AV1_COMP * cpi,AV1_COMMON * cm,MACROBLOCKD * xd,int use_restoration,int use_cdef)4795 static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
4796 MACROBLOCKD *xd, int use_restoration,
4797 int use_cdef) {
4798 if (use_restoration)
4799 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
4800
4801 if (use_cdef) {
4802 #if CONFIG_COLLECT_COMPONENT_TIMING
4803 start_timing(cpi, cdef_time);
4804 #endif
4805 // Find CDEF parameters
4806 av1_cdef_search(&cm->cur_frame->buf, cpi->source, cm, xd,
4807 cpi->sf.lpf_sf.cdef_pick_method, cpi->td.mb.rdmult);
4808
4809 // Apply the filter
4810 av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
4811 #if CONFIG_COLLECT_COMPONENT_TIMING
4812 end_timing(cpi, cdef_time);
4813 #endif
4814 } else {
4815 cm->cdef_info.cdef_bits = 0;
4816 cm->cdef_info.cdef_strengths[0] = 0;
4817 cm->cdef_info.nb_cdef_strengths = 1;
4818 cm->cdef_info.cdef_uv_strengths[0] = 0;
4819 }
4820
4821 superres_post_encode(cpi);
4822
4823 #if CONFIG_COLLECT_COMPONENT_TIMING
4824 start_timing(cpi, loop_restoration_time);
4825 #endif
4826 if (use_restoration) {
4827 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
4828 av1_pick_filter_restoration(cpi->source, cpi);
4829 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4830 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4831 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4832 if (cpi->num_workers > 1)
4833 av1_loop_restoration_filter_frame_mt(&cm->cur_frame->buf, cm, 0,
4834 cpi->workers, cpi->num_workers,
4835 &cpi->lr_row_sync, &cpi->lr_ctxt);
4836 else
4837 av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
4838 &cpi->lr_ctxt);
4839 }
4840 } else {
4841 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4842 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4843 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
4844 }
4845 #if CONFIG_COLLECT_COMPONENT_TIMING
4846 end_timing(cpi, loop_restoration_time);
4847 #endif
4848 }
4849
loopfilter_frame(AV1_COMP * cpi,AV1_COMMON * cm)4850 static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
4851 const int num_planes = av1_num_planes(cm);
4852 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4853
4854 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
4855 cm->features.coded_lossless && cm->features.all_lossless));
4856
4857 const int use_loopfilter =
4858 !cm->features.coded_lossless && !cm->tiles.large_scale;
4859 const int use_cdef = cm->seq_params.enable_cdef &&
4860 !cm->features.coded_lossless && !cm->tiles.large_scale;
4861 const int use_restoration = cm->seq_params.enable_restoration &&
4862 !cm->features.all_lossless &&
4863 !cm->tiles.large_scale;
4864
4865 struct loopfilter *lf = &cm->lf;
4866
4867 #if CONFIG_COLLECT_COMPONENT_TIMING
4868 start_timing(cpi, loop_filter_time);
4869 #endif
4870 if (use_loopfilter) {
4871 aom_clear_system_state();
4872 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_sf.lpf_pick);
4873 } else {
4874 lf->filter_level[0] = 0;
4875 lf->filter_level[1] = 0;
4876 }
4877
4878 if (lf->filter_level[0] || lf->filter_level[1]) {
4879 if (cpi->num_workers > 1)
4880 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
4881 #if CONFIG_LPF_MASK
4882 0,
4883 #endif
4884 cpi->workers, cpi->num_workers,
4885 &cpi->lf_row_sync);
4886 else
4887 av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
4888 #if CONFIG_LPF_MASK
4889 0,
4890 #endif
4891 0, num_planes, 0);
4892 }
4893 #if CONFIG_COLLECT_COMPONENT_TIMING
4894 end_timing(cpi, loop_filter_time);
4895 #endif
4896
4897 cdef_restoration_frame(cpi, cm, xd, use_restoration, use_cdef);
4898 }
4899
fix_interp_filter(InterpFilter * const interp_filter,const FRAME_COUNTS * const counts)4900 static void fix_interp_filter(InterpFilter *const interp_filter,
4901 const FRAME_COUNTS *const counts) {
4902 if (*interp_filter == SWITCHABLE) {
4903 // Check to see if only one of the filters is actually used
4904 int count[SWITCHABLE_FILTERS] = { 0 };
4905 int num_filters_used = 0;
4906 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4907 for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
4908 count[i] += counts->switchable_interp[j][i];
4909 num_filters_used += (count[i] > 0);
4910 }
4911 if (num_filters_used == 1) {
4912 // Only one filter is used. So set the filter at frame level
4913 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4914 if (count[i]) {
4915 if (i == EIGHTTAP_REGULAR) *interp_filter = i;
4916 break;
4917 }
4918 }
4919 }
4920 }
4921 }
4922
finalize_encoded_frame(AV1_COMP * const cpi)4923 static void finalize_encoded_frame(AV1_COMP *const cpi) {
4924 AV1_COMMON *const cm = &cpi->common;
4925 CurrentFrame *const current_frame = &cm->current_frame;
4926
4927 if (!cm->seq_params.reduced_still_picture_hdr &&
4928 encode_show_existing_frame(cm)) {
4929 RefCntBuffer *const frame_to_show =
4930 cm->ref_frame_map[cpi->existing_fb_idx_to_show];
4931
4932 if (frame_to_show == NULL) {
4933 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4934 "Buffer does not contain a reconstructed frame");
4935 }
4936 assert(frame_to_show->ref_count > 0);
4937 assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
4938 }
4939
4940 if (!encode_show_existing_frame(cm) &&
4941 cm->seq_params.film_grain_params_present &&
4942 (cm->show_frame || cm->showable_frame)) {
4943 // Copy the current frame's film grain params to the its corresponding
4944 // RefCntBuffer slot.
4945 cm->cur_frame->film_grain_params = cm->film_grain_params;
4946
4947 // We must update the parameters if this is not an INTER_FRAME
4948 if (current_frame->frame_type != INTER_FRAME)
4949 cm->cur_frame->film_grain_params.update_parameters = 1;
4950
4951 // Iterate the random seed for the next frame.
4952 cm->film_grain_params.random_seed += 3381;
4953 if (cm->film_grain_params.random_seed == 0)
4954 cm->film_grain_params.random_seed = 7391;
4955 }
4956
4957 // Initialise all tiles' contexts from the global frame context
4958 for (int tile_col = 0; tile_col < cm->tiles.cols; tile_col++) {
4959 for (int tile_row = 0; tile_row < cm->tiles.rows; tile_row++) {
4960 const int tile_idx = tile_row * cm->tiles.cols + tile_col;
4961 cpi->tile_data[tile_idx].tctx = *cm->fc;
4962 }
4963 }
4964
4965 fix_interp_filter(&cm->features.interp_filter, cpi->td.counts);
4966 }
4967
get_regulated_q_overshoot(AV1_COMP * const cpi,int q_low,int q_high,int top_index,int bottom_index)4968 static int get_regulated_q_overshoot(AV1_COMP *const cpi, int q_low, int q_high,
4969 int top_index, int bottom_index) {
4970 const AV1_COMMON *const cm = &cpi->common;
4971 const RATE_CONTROL *const rc = &cpi->rc;
4972
4973 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4974
4975 int q_regulated =
4976 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4977 AOMMAX(q_high, top_index), cm->width, cm->height);
4978
4979 int retries = 0;
4980 while (q_regulated < q_low && retries < 10) {
4981 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4982 q_regulated =
4983 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4984 AOMMAX(q_high, top_index), cm->width, cm->height);
4985 retries++;
4986 }
4987 return q_regulated;
4988 }
4989
get_regulated_q_undershoot(AV1_COMP * const cpi,int q_high,int top_index,int bottom_index)4990 static int get_regulated_q_undershoot(AV1_COMP *const cpi, int q_high,
4991 int top_index, int bottom_index) {
4992 const AV1_COMMON *const cm = &cpi->common;
4993 const RATE_CONTROL *const rc = &cpi->rc;
4994
4995 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4996 int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4997 top_index, cm->width, cm->height);
4998
4999 int retries = 0;
5000 while (q_regulated > q_high && retries < 10) {
5001 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
5002 q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
5003 top_index, cm->width, cm->height);
5004 retries++;
5005 }
5006 return q_regulated;
5007 }
5008
5009 // Called after encode_with_recode_loop() has just encoded a frame and packed
5010 // its bitstream. This function works out whether we under- or over-shot
5011 // our bitrate target and adjusts q as appropriate. Also decides whether
5012 // or not we should do another recode loop, indicated by *loop
recode_loop_update_q(AV1_COMP * const cpi,int * const loop,int * const q,int * const q_low,int * const q_high,const int top_index,const int bottom_index,int * const undershoot_seen,int * const overshoot_seen,int * const low_cr_seen,const int loop_at_this_size)5013 static void recode_loop_update_q(
5014 AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low,
5015 int *const q_high, const int top_index, const int bottom_index,
5016 int *const undershoot_seen, int *const overshoot_seen,
5017 int *const low_cr_seen, const int loop_at_this_size) {
5018 AV1_COMMON *const cm = &cpi->common;
5019 RATE_CONTROL *const rc = &cpi->rc;
5020 *loop = 0;
5021
5022 const int min_cr = cpi->oxcf.min_cr;
5023 if (min_cr > 0) {
5024 aom_clear_system_state();
5025 const double compression_ratio =
5026 av1_get_compression_ratio(cm, rc->projected_frame_size >> 3);
5027 const double target_cr = min_cr / 100.0;
5028 if (compression_ratio < target_cr) {
5029 *low_cr_seen = 1;
5030 if (*q < rc->worst_quality) {
5031 const double cr_ratio = target_cr / compression_ratio;
5032 const int projected_q = AOMMAX(*q + 1, (int)(*q * cr_ratio * cr_ratio));
5033 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->worst_quality);
5034 *q_low = AOMMAX(*q, *q_low);
5035 *q_high = AOMMAX(*q, *q_high);
5036 *loop = 1;
5037 }
5038 }
5039 if (*low_cr_seen) return;
5040 }
5041
5042 if (cpi->oxcf.rc_mode == AOM_Q) return;
5043
5044 const int last_q = *q;
5045 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
5046 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
5047 &frame_under_shoot_limit,
5048 &frame_over_shoot_limit);
5049 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
5050
5051 if (cm->current_frame.frame_type == KEY_FRAME && rc->this_key_frame_forced &&
5052 rc->projected_frame_size < rc->max_frame_bandwidth) {
5053 int64_t kf_err;
5054 const int64_t high_err_target = cpi->ambient_err;
5055 const int64_t low_err_target = cpi->ambient_err >> 1;
5056
5057 #if CONFIG_AV1_HIGHBITDEPTH
5058 if (cm->seq_params.use_highbitdepth) {
5059 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
5060 } else {
5061 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5062 }
5063 #else
5064 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5065 #endif
5066 // Prevent possible divide by zero error below for perfect KF
5067 kf_err += !kf_err;
5068
5069 // The key frame is not good enough or we can afford
5070 // to make it better without undue risk of popping.
5071 if ((kf_err > high_err_target &&
5072 rc->projected_frame_size <= frame_over_shoot_limit) ||
5073 (kf_err > low_err_target &&
5074 rc->projected_frame_size <= frame_under_shoot_limit)) {
5075 // Lower q_high
5076 *q_high = AOMMAX(*q - 1, *q_low);
5077
5078 // Adjust Q
5079 *q = (int)((*q * high_err_target) / kf_err);
5080 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
5081 } else if (kf_err < low_err_target &&
5082 rc->projected_frame_size >= frame_under_shoot_limit) {
5083 // The key frame is much better than the previous frame
5084 // Raise q_low
5085 *q_low = AOMMIN(*q + 1, *q_high);
5086
5087 // Adjust Q
5088 *q = (int)((*q * low_err_target) / kf_err);
5089 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
5090 }
5091
5092 // Clamp Q to upper and lower limits:
5093 *q = clamp(*q, *q_low, *q_high);
5094 *loop = (*q != last_q);
5095 return;
5096 }
5097
5098 if (recode_loop_test(cpi, frame_over_shoot_limit, frame_under_shoot_limit, *q,
5099 AOMMAX(*q_high, top_index), bottom_index)) {
5100 // Is the projected frame size out of range and are we allowed
5101 // to attempt to recode.
5102
5103 // Frame size out of permitted range:
5104 // Update correction factor & compute new Q to try...
5105 // Frame is too large
5106 if (rc->projected_frame_size > rc->this_frame_target) {
5107 // Special case if the projected size is > the max allowed.
5108 if (*q == *q_high &&
5109 rc->projected_frame_size >= rc->max_frame_bandwidth) {
5110 const double q_val_high_current =
5111 av1_convert_qindex_to_q(*q_high, cm->seq_params.bit_depth);
5112 const double q_val_high_new =
5113 q_val_high_current *
5114 ((double)rc->projected_frame_size / rc->max_frame_bandwidth);
5115 *q_high = av1_find_qindex(q_val_high_new, cm->seq_params.bit_depth,
5116 rc->best_quality, rc->worst_quality);
5117 }
5118
5119 // Raise Qlow as to at least the current value
5120 *q_low = AOMMIN(*q + 1, *q_high);
5121
5122 if (*undershoot_seen || loop_at_this_size > 2 ||
5123 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
5124 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
5125
5126 *q = (*q_high + *q_low + 1) / 2;
5127 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
5128 const int q_mid = (*q_high + *q_low + 1) / 2;
5129 const int q_regulated = get_regulated_q_overshoot(
5130 cpi, *q_low, *q_high, top_index, bottom_index);
5131 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
5132 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
5133 *q = (q_mid + q_regulated + 1) / 2;
5134 } else {
5135 *q = get_regulated_q_overshoot(cpi, *q_low, *q_high, top_index,
5136 bottom_index);
5137 }
5138
5139 *overshoot_seen = 1;
5140 } else {
5141 // Frame is too small
5142 *q_high = AOMMAX(*q - 1, *q_low);
5143
5144 if (*overshoot_seen || loop_at_this_size > 2 ||
5145 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
5146 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
5147 *q = (*q_high + *q_low) / 2;
5148 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
5149 const int q_mid = (*q_high + *q_low) / 2;
5150 const int q_regulated =
5151 get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
5152 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
5153 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
5154 *q = (q_mid + q_regulated) / 2;
5155
5156 // Special case reset for qlow for constrained quality.
5157 // This should only trigger where there is very substantial
5158 // undershoot on a frame and the auto cq level is above
5159 // the user passsed in value.
5160 if (cpi->oxcf.rc_mode == AOM_CQ && q_regulated < *q_low) {
5161 *q_low = *q;
5162 }
5163 } else {
5164 *q = get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
5165
5166 // Special case reset for qlow for constrained quality.
5167 // This should only trigger where there is very substantial
5168 // undershoot on a frame and the auto cq level is above
5169 // the user passsed in value.
5170 if (cpi->oxcf.rc_mode == AOM_CQ && *q < *q_low) {
5171 *q_low = *q;
5172 }
5173 }
5174
5175 *undershoot_seen = 1;
5176 }
5177
5178 // Clamp Q to upper and lower limits:
5179 *q = clamp(*q, *q_low, *q_high);
5180 }
5181
5182 *loop = (*q != last_q);
5183 }
5184
get_interp_filter_selected(const AV1_COMMON * const cm,MV_REFERENCE_FRAME ref,InterpFilter ifilter)5185 static int get_interp_filter_selected(const AV1_COMMON *const cm,
5186 MV_REFERENCE_FRAME ref,
5187 InterpFilter ifilter) {
5188 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
5189 if (buf == NULL) return 0;
5190 return buf->interp_filter_selected[ifilter];
5191 }
5192
setup_interp_filter_search_mask(AV1_COMP * cpi)5193 static uint16_t setup_interp_filter_search_mask(AV1_COMP *cpi) {
5194 const AV1_COMMON *const cm = &cpi->common;
5195 int ref_total[REF_FRAMES] = { 0 };
5196 uint16_t mask = ALLOW_ALL_INTERP_FILT_MASK;
5197
5198 if (cpi->last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
5199 return mask;
5200
5201 for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
5202 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
5203 ++ifilter) {
5204 ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter);
5205 }
5206 }
5207 int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] +
5208 ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] +
5209 ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]);
5210
5211 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
5212 ++ifilter) {
5213 int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30;
5214 if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) {
5215 int filter_score =
5216 get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 +
5217 get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 +
5218 get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 +
5219 get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 +
5220 get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 +
5221 get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10;
5222 if (filter_score < ref_total_total) {
5223 DUAL_FILTER_TYPE filt_type = ifilter + SWITCHABLE_FILTERS * ifilter;
5224 reset_interp_filter_allowed_mask(&mask, filt_type);
5225 }
5226 }
5227 }
5228 return mask;
5229 }
5230
5231 #if !CONFIG_REALTIME_ONLY
5232 #define STRICT_PSNR_DIFF_THRESH 0.9
5233 // Encode key frame with/without screen content tools to determine whether
5234 // screen content tools should be enabled for this key frame group or not.
5235 // The first encoding is without screen content tools.
5236 // The second encoding is with screen content tools.
5237 // We compare the psnr and frame size to make the decision.
screen_content_tools_determination(AV1_COMP * cpi,const int allow_screen_content_tools_orig_decision,const int allow_intrabc_orig_decision,const int is_screen_content_type_orig_decision,const int pass,int * projected_size_pass,PSNR_STATS * psnr)5238 static void screen_content_tools_determination(
5239 AV1_COMP *cpi, const int allow_screen_content_tools_orig_decision,
5240 const int allow_intrabc_orig_decision,
5241 const int is_screen_content_type_orig_decision, const int pass,
5242 int *projected_size_pass, PSNR_STATS *psnr) {
5243 AV1_COMMON *const cm = &cpi->common;
5244 FeatureFlags *const features = &cm->features;
5245 projected_size_pass[pass] = cpi->rc.projected_frame_size;
5246 #if CONFIG_AV1_HIGHBITDEPTH
5247 const uint32_t in_bit_depth = cpi->oxcf.input_bit_depth;
5248 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
5249 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr[pass],
5250 bit_depth, in_bit_depth);
5251 #else
5252 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr[pass]);
5253 #endif
5254 if (pass != 1) return;
5255
5256 const double psnr_diff = psnr[1].psnr[0] - psnr[0].psnr[0];
5257 const int is_sc_encoding_much_better = psnr_diff > STRICT_PSNR_DIFF_THRESH;
5258 if (is_sc_encoding_much_better) {
5259 // Use screen content tools, if we get coding gain.
5260 features->allow_screen_content_tools = 1;
5261 features->allow_intrabc = cpi->intrabc_used;
5262 cpi->is_screen_content_type = 1;
5263 } else {
5264 // Use original screen content decision.
5265 features->allow_screen_content_tools =
5266 allow_screen_content_tools_orig_decision;
5267 features->allow_intrabc = allow_intrabc_orig_decision;
5268 cpi->is_screen_content_type = is_screen_content_type_orig_decision;
5269 }
5270 }
5271
5272 // Set some encoding parameters to make the encoding process fast.
5273 // A fixed block partition size, and a large q is used.
set_encoding_params_for_screen_content(AV1_COMP * cpi,const int pass)5274 static void set_encoding_params_for_screen_content(AV1_COMP *cpi,
5275 const int pass) {
5276 AV1_COMMON *const cm = &cpi->common;
5277 if (pass == 0) {
5278 // In the first pass, encode without screen content tools.
5279 // Use a high q, and a fixed block size for fast encoding.
5280 cm->features.allow_screen_content_tools = 0;
5281 cm->features.allow_intrabc = 0;
5282 cpi->is_screen_content_type = 0;
5283 cpi->sf.part_sf.partition_search_type = FIXED_PARTITION;
5284 cpi->sf.part_sf.always_this_block_size = BLOCK_32X32;
5285 return;
5286 }
5287 assert(pass == 1);
5288 // In the second pass, encode with screen content tools.
5289 // Use a high q, and a fixed block size for fast encoding.
5290 cm->features.allow_screen_content_tools = 1;
5291 // TODO(chengchen): turn intrabc on could lead to data race issue.
5292 // cm->allow_intrabc = 1;
5293 cpi->is_screen_content_type = 1;
5294 cpi->sf.part_sf.partition_search_type = FIXED_PARTITION;
5295 cpi->sf.part_sf.always_this_block_size = BLOCK_32X32;
5296 }
5297
5298 // Determines whether to use screen content tools for the key frame group.
5299 // This function modifies "cm->features.allow_screen_content_tools",
5300 // "cm->features.allow_intrabc" and "cpi->is_screen_content_type".
determine_sc_tools_with_encoding(AV1_COMP * cpi,const int q_orig)5301 static void determine_sc_tools_with_encoding(AV1_COMP *cpi, const int q_orig) {
5302 AV1_COMMON *const cm = &cpi->common;
5303 // Variables to help determine if we should allow screen content tools.
5304 int projected_size_pass[3] = { 0 };
5305 PSNR_STATS psnr[3];
5306 const int is_key_frame = cm->current_frame.frame_type == KEY_FRAME;
5307 const int allow_screen_content_tools_orig_decision =
5308 cm->features.allow_screen_content_tools;
5309 const int allow_intrabc_orig_decision = cm->features.allow_intrabc;
5310 const int is_screen_content_type_orig_decision = cpi->is_screen_content_type;
5311 // Turn off the encoding trial for forward key frame and superres.
5312 if (cpi->sf.rt_sf.use_nonrd_pick_mode || cpi->oxcf.fwd_kf_enabled ||
5313 cpi->superres_mode != SUPERRES_NONE || cpi->oxcf.mode == REALTIME ||
5314 is_screen_content_type_orig_decision || !is_key_frame) {
5315 return;
5316 }
5317
5318 // TODO(chengchen): multiple encoding for the lossless mode is time consuming.
5319 // Find a better way to determine whether screen content tools should be used
5320 // for lossless coding.
5321 // Use a high q and a fixed partition to do quick encoding.
5322 const int q_for_screen_content_quick_run =
5323 is_lossless_requested(&cpi->oxcf) ? q_orig : AOMMAX(q_orig, 244);
5324 const int partition_search_type_orig = cpi->sf.part_sf.partition_search_type;
5325 const BLOCK_SIZE fixed_partition_block_size_orig =
5326 cpi->sf.part_sf.always_this_block_size;
5327
5328 // Setup necessary params for encoding, including frame source, etc.
5329 aom_clear_system_state();
5330
5331 cpi->source =
5332 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
5333 if (cpi->unscaled_last_source != NULL) {
5334 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
5335 &cpi->scaled_last_source);
5336 }
5337
5338 setup_frame(cpi);
5339
5340 if (cm->seg.enabled) {
5341 if (!cm->seg.update_data && cm->prev_frame) {
5342 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
5343 cm->seg.enabled = cm->prev_frame->seg.enabled;
5344 } else {
5345 av1_calculate_segdata(&cm->seg);
5346 }
5347 } else {
5348 memset(&cm->seg, 0, sizeof(cm->seg));
5349 }
5350 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
5351 cm->cur_frame->seg.enabled = cm->seg.enabled;
5352
5353 // The two encoding passes aim to help determine whether to use screen
5354 // content tools, with a high q and fixed partition.
5355 for (int pass = 0; pass < 2; ++pass) {
5356 set_encoding_params_for_screen_content(cpi, pass);
5357 #if CONFIG_TUNE_VMAF
5358 if (cpi->oxcf.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING ||
5359 cpi->oxcf.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
5360 cpi->oxcf.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
5361 av1_set_quantizer(
5362 cm, cpi->oxcf.qm_minlevel, cpi->oxcf.qm_maxlevel,
5363 av1_get_vmaf_base_qindex(cpi, q_for_screen_content_quick_run));
5364 } else {
5365 #endif
5366 av1_set_quantizer(cm, cpi->oxcf.qm_minlevel, cpi->oxcf.qm_maxlevel,
5367 q_for_screen_content_quick_run);
5368 #if CONFIG_TUNE_VMAF
5369 }
5370 #endif
5371 av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
5372 if (cpi->oxcf.deltaq_mode != NO_DELTA_Q)
5373 av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
5374 cm->seq_params.bit_depth);
5375
5376 av1_set_variance_partition_thresholds(cpi, q_for_screen_content_quick_run,
5377 0);
5378 // transform / motion compensation build reconstruction frame
5379 av1_encode_frame(cpi);
5380 // Screen content decision
5381 screen_content_tools_determination(
5382 cpi, allow_screen_content_tools_orig_decision,
5383 allow_intrabc_orig_decision, is_screen_content_type_orig_decision, pass,
5384 projected_size_pass, psnr);
5385 }
5386
5387 // Set partition speed feature back.
5388 cpi->sf.part_sf.partition_search_type = partition_search_type_orig;
5389 cpi->sf.part_sf.always_this_block_size = fixed_partition_block_size_orig;
5390 }
5391 #endif // CONFIG_REALTIME_ONLY
5392
encode_with_recode_loop(AV1_COMP * cpi,size_t * size,uint8_t * dest)5393 static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
5394 AV1_COMMON *const cm = &cpi->common;
5395 RATE_CONTROL *const rc = &cpi->rc;
5396 GlobalMotionInfo *const gm_info = &cpi->gm_info;
5397 const int allow_recode = (cpi->sf.hl_sf.recode_loop != DISALLOW_RECODE);
5398 // Must allow recode if minimum compression ratio is set.
5399 assert(IMPLIES(cpi->oxcf.min_cr > 0, allow_recode));
5400
5401 set_size_independent_vars(cpi);
5402 if (is_stat_consumption_stage_twopass(cpi) &&
5403 cpi->sf.interp_sf.adaptive_interp_filter_search)
5404 cpi->interp_search_flags.interp_filter_search_mask =
5405 setup_interp_filter_search_mask(cpi);
5406 cpi->source->buf_8bit_valid = 0;
5407
5408 av1_setup_frame_size(cpi);
5409
5410 #if CONFIG_SUPERRES_IN_RECODE
5411 if (superres_in_recode_allowed(cpi) && cpi->superres_mode != SUPERRES_NONE &&
5412 cm->superres_scale_denominator == SCALE_NUMERATOR) {
5413 // Superres mode is currently enabled, but the denominator selected will
5414 // disable superres. So no need to continue, as we will go through another
5415 // recode loop for full-resolution after this anyway.
5416 return -1;
5417 }
5418 #endif // CONFIG_SUPERRES_IN_RECODE
5419
5420 int top_index = 0, bottom_index = 0;
5421 int q = 0, q_low = 0, q_high = 0;
5422 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
5423 q_low = bottom_index;
5424 q_high = top_index;
5425 if (cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION) {
5426 const int num_64x64_blocks =
5427 (cm->seq_params.sb_size == BLOCK_64X64) ? 1 : 4;
5428 if (cpi->td.vt64x64) {
5429 if (num_64x64_blocks != cpi->td.num_64x64_blocks) {
5430 aom_free(cpi->td.vt64x64);
5431 cpi->td.vt64x64 = NULL;
5432 }
5433 }
5434 if (!cpi->td.vt64x64) {
5435 CHECK_MEM_ERROR(cm, cpi->td.vt64x64,
5436 aom_malloc(sizeof(*cpi->td.vt64x64) * num_64x64_blocks));
5437 cpi->td.num_64x64_blocks = num_64x64_blocks;
5438 }
5439 }
5440
5441 if (cm->current_frame.frame_type == KEY_FRAME) {
5442 FrameProbInfo *const frame_probs = &cpi->frame_probs;
5443
5444 if (cpi->sf.tx_sf.tx_type_search.prune_tx_type_using_stats) {
5445 av1_copy(frame_probs->tx_type_probs, default_tx_type_probs);
5446 }
5447
5448 if (!cpi->sf.inter_sf.disable_obmc &&
5449 cpi->sf.inter_sf.prune_obmc_prob_thresh > 0) {
5450 av1_copy(frame_probs->obmc_probs, default_obmc_probs);
5451 }
5452
5453 if (cpi->sf.inter_sf.prune_warped_prob_thresh > 0) {
5454 av1_copy(frame_probs->warped_probs, default_warped_probs);
5455 }
5456
5457 if (cpi->sf.interp_sf.adaptive_interp_filter_search == 2) {
5458 av1_copy(frame_probs->switchable_interp_probs,
5459 default_switchable_interp_probs);
5460 }
5461 }
5462 #if !CONFIG_REALTIME_ONLY
5463 // Determine whether to use screen content tools using two fast encoding.
5464 determine_sc_tools_with_encoding(cpi, q);
5465 #endif // CONFIG_REALTIME_ONLY
5466
5467 #if CONFIG_COLLECT_COMPONENT_TIMING
5468 printf("\n Encoding a frame:");
5469 #endif
5470
5471 // Loop variables
5472 int loop = 0;
5473 int loop_count = 0;
5474 int loop_at_this_size = 0;
5475 int overshoot_seen = 0;
5476 int undershoot_seen = 0;
5477 int low_cr_seen = 0;
5478 int last_loop_allow_hp = 0;
5479
5480 do {
5481 loop = 0;
5482 aom_clear_system_state();
5483
5484 // if frame was scaled calculate global_motion_search again if already
5485 // done
5486 if (loop_count > 0 && cpi->source && gm_info->search_done) {
5487 if (cpi->source->y_crop_width != cm->width ||
5488 cpi->source->y_crop_height != cm->height) {
5489 gm_info->search_done = 0;
5490 }
5491 }
5492 cpi->source =
5493 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
5494 if (cpi->unscaled_last_source != NULL) {
5495 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
5496 &cpi->scaled_last_source);
5497 }
5498
5499 if (!frame_is_intra_only(cm)) {
5500 if (loop_count > 0) {
5501 release_scaled_references(cpi);
5502 }
5503 scale_references(cpi);
5504 }
5505 #if CONFIG_TUNE_VMAF
5506 if (cpi->oxcf.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING ||
5507 cpi->oxcf.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
5508 cpi->oxcf.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
5509 av1_set_quantizer(cm, cpi->oxcf.qm_minlevel, cpi->oxcf.qm_maxlevel,
5510 av1_get_vmaf_base_qindex(cpi, q));
5511 } else {
5512 #endif
5513 av1_set_quantizer(cm, cpi->oxcf.qm_minlevel, cpi->oxcf.qm_maxlevel, q);
5514 #if CONFIG_TUNE_VMAF
5515 }
5516 #endif
5517 av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
5518
5519 if (cpi->oxcf.deltaq_mode != NO_DELTA_Q)
5520 av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
5521 cm->seq_params.bit_depth);
5522
5523 av1_set_variance_partition_thresholds(cpi, q, 0);
5524
5525 // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
5526 // cm->current_frame.frame_number, cm->show_frame, q,
5527 // cm->current_frame.frame_type, cm->superres_scale_denominator);
5528
5529 if (loop_count == 0) {
5530 setup_frame(cpi);
5531 } else if (get_primary_ref_frame_buf(cm) == NULL) {
5532 // Base q-index may have changed, so we need to assign proper default coef
5533 // probs before every iteration.
5534 av1_default_coef_probs(cm);
5535 av1_setup_frame_contexts(cm);
5536 }
5537
5538 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
5539 av1_vaq_frame_setup(cpi);
5540 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
5541 av1_setup_in_frame_q_adj(cpi);
5542 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !allow_recode) {
5543 suppress_active_map(cpi);
5544 av1_cyclic_refresh_setup(cpi);
5545 apply_active_map(cpi);
5546 }
5547
5548 if (cm->seg.enabled) {
5549 if (!cm->seg.update_data && cm->prev_frame) {
5550 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
5551 cm->seg.enabled = cm->prev_frame->seg.enabled;
5552 } else {
5553 av1_calculate_segdata(&cm->seg);
5554 }
5555 } else {
5556 memset(&cm->seg, 0, sizeof(cm->seg));
5557 }
5558 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
5559 cm->cur_frame->seg.enabled = cm->seg.enabled;
5560
5561 #if CONFIG_COLLECT_COMPONENT_TIMING
5562 start_timing(cpi, av1_encode_frame_time);
5563 #endif
5564 // Set the motion vector precision based on mv stats from the last coded
5565 // frame.
5566 if (!frame_is_intra_only(cm)) {
5567 av1_pick_and_set_high_precision_mv(cpi, q);
5568
5569 // If the precision has changed during different iteration of the loop,
5570 // then we need to reset the global motion vectors
5571 if (loop_count > 0 &&
5572 cm->features.allow_high_precision_mv != last_loop_allow_hp) {
5573 gm_info->search_done = 0;
5574 }
5575 last_loop_allow_hp = cm->features.allow_high_precision_mv;
5576 }
5577
5578 // transform / motion compensation build reconstruction frame
5579 av1_encode_frame(cpi);
5580 #if !CONFIG_REALTIME_ONLY
5581 // Reset the mv_stats in case we are interrupted by an intraframe or an
5582 // overlay frame.
5583 if (cpi->mv_stats.valid) {
5584 av1_zero(cpi->mv_stats);
5585 }
5586 // Gather the mv_stats for the next frame
5587 if (cpi->sf.hl_sf.high_precision_mv_usage == LAST_MV_DATA &&
5588 av1_frame_allows_smart_mv(cpi)) {
5589 av1_collect_mv_stats(cpi, q);
5590 }
5591 #endif // !CONFIG_REALTIME_ONLY
5592
5593 #if CONFIG_COLLECT_COMPONENT_TIMING
5594 end_timing(cpi, av1_encode_frame_time);
5595 #endif
5596
5597 aom_clear_system_state();
5598
5599 // Dummy pack of the bitstream using up to date stats to get an
5600 // accurate estimate of output frame size to determine if we need
5601 // to recode.
5602 const int do_dummy_pack =
5603 (cpi->sf.hl_sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
5604 cpi->oxcf.rc_mode != AOM_Q) ||
5605 cpi->oxcf.min_cr > 0;
5606 if (do_dummy_pack) {
5607 finalize_encoded_frame(cpi);
5608 int largest_tile_id = 0; // Output from bitstream: unused here
5609 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) !=
5610 AOM_CODEC_OK) {
5611 return AOM_CODEC_ERROR;
5612 }
5613
5614 rc->projected_frame_size = (int)(*size) << 3;
5615 }
5616
5617 if (allow_recode) {
5618 // Update q and decide whether to do a recode loop
5619 recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
5620 bottom_index, &undershoot_seen, &overshoot_seen,
5621 &low_cr_seen, loop_at_this_size);
5622 }
5623
5624 // Special case for overlay frame.
5625 if (loop && rc->is_src_frame_alt_ref &&
5626 rc->projected_frame_size < rc->max_frame_bandwidth) {
5627 loop = 0;
5628 }
5629
5630 if (allow_recode && !cpi->sf.gm_sf.gm_disable_recode &&
5631 recode_loop_test_global_motion(cm->global_motion,
5632 cpi->td.rd_counts.global_motion_used,
5633 gm_info->params_cost)) {
5634 loop = 1;
5635 }
5636
5637 if (loop) {
5638 ++loop_count;
5639 ++loop_at_this_size;
5640
5641 #if CONFIG_INTERNAL_STATS
5642 ++cpi->tot_recode_hits;
5643 #endif
5644 }
5645 #if CONFIG_COLLECT_COMPONENT_TIMING
5646 if (loop) printf("\n Recoding:");
5647 #endif
5648 } while (loop);
5649
5650 // Update some stats from cyclic refresh.
5651 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !frame_is_intra_only(cm))
5652 av1_cyclic_refresh_postencode(cpi);
5653
5654 return AOM_CODEC_OK;
5655 }
5656
encode_with_recode_loop_and_filter(AV1_COMP * cpi,size_t * size,uint8_t * dest,int64_t * sse,int64_t * rate,int * largest_tile_id)5657 static int encode_with_recode_loop_and_filter(AV1_COMP *cpi, size_t *size,
5658 uint8_t *dest, int64_t *sse,
5659 int64_t *rate,
5660 int *largest_tile_id) {
5661 #if CONFIG_COLLECT_COMPONENT_TIMING
5662 start_timing(cpi, encode_with_recode_loop_time);
5663 #endif
5664 int err = encode_with_recode_loop(cpi, size, dest);
5665 #if CONFIG_COLLECT_COMPONENT_TIMING
5666 end_timing(cpi, encode_with_recode_loop_time);
5667 #endif
5668 if (err != AOM_CODEC_OK) {
5669 if (err == -1) {
5670 // special case as described in encode_with_recode_loop().
5671 // Encoding was skipped.
5672 err = AOM_CODEC_OK;
5673 if (sse != NULL) *sse = INT64_MAX;
5674 if (rate != NULL) *rate = INT64_MAX;
5675 *largest_tile_id = 0;
5676 }
5677 return err;
5678 }
5679
5680 #ifdef OUTPUT_YUV_SKINMAP
5681 if (cpi->common.current_frame.frame_number > 1) {
5682 av1_compute_skin_map(cpi, yuv_skinmap_file);
5683 }
5684 #endif // OUTPUT_YUV_SKINMAP
5685
5686 AV1_COMMON *const cm = &cpi->common;
5687 SequenceHeader *const seq_params = &cm->seq_params;
5688
5689 // Special case code to reduce pulsing when key frames are forced at a
5690 // fixed interval. Note the reconstruction error if it is the frame before
5691 // the force key frame
5692 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
5693 #if CONFIG_AV1_HIGHBITDEPTH
5694 if (seq_params->use_highbitdepth) {
5695 cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
5696 } else {
5697 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5698 }
5699 #else
5700 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5701 #endif
5702 }
5703
5704 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
5705 cm->cur_frame->buf.transfer_characteristics =
5706 seq_params->transfer_characteristics;
5707 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
5708 cm->cur_frame->buf.monochrome = seq_params->monochrome;
5709 cm->cur_frame->buf.chroma_sample_position =
5710 seq_params->chroma_sample_position;
5711 cm->cur_frame->buf.color_range = seq_params->color_range;
5712 cm->cur_frame->buf.render_width = cm->render_width;
5713 cm->cur_frame->buf.render_height = cm->render_height;
5714
5715 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5716 // off.
5717
5718 // Pick the loop filter level for the frame.
5719 if (!cm->features.allow_intrabc) {
5720 loopfilter_frame(cpi, cm);
5721 } else {
5722 cm->lf.filter_level[0] = 0;
5723 cm->lf.filter_level[1] = 0;
5724 cm->cdef_info.cdef_bits = 0;
5725 cm->cdef_info.cdef_strengths[0] = 0;
5726 cm->cdef_info.nb_cdef_strengths = 1;
5727 cm->cdef_info.cdef_uv_strengths[0] = 0;
5728 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5729 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5730 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
5731 }
5732
5733 // TODO(debargha): Fix mv search range on encoder side
5734 // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5735 aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5736
5737 #ifdef OUTPUT_YUV_REC
5738 aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
5739 #endif
5740
5741 finalize_encoded_frame(cpi);
5742 // Build the bitstream
5743 #if CONFIG_COLLECT_COMPONENT_TIMING
5744 start_timing(cpi, av1_pack_bitstream_final_time);
5745 #endif
5746 if (av1_pack_bitstream(cpi, dest, size, largest_tile_id) != AOM_CODEC_OK)
5747 return AOM_CODEC_ERROR;
5748 #if CONFIG_COLLECT_COMPONENT_TIMING
5749 end_timing(cpi, av1_pack_bitstream_final_time);
5750 #endif
5751
5752 // Compute sse and rate.
5753 if (sse != NULL) {
5754 #if CONFIG_AV1_HIGHBITDEPTH
5755 *sse = (seq_params->use_highbitdepth)
5756 ? aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf)
5757 : aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5758 #else
5759 *sse = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5760 #endif
5761 }
5762 if (rate != NULL) {
5763 const int64_t bits = (*size << 3);
5764 *rate = (bits << 5); // To match scale.
5765 }
5766 return AOM_CODEC_OK;
5767 }
5768
5769 #if CONFIG_SUPERRES_IN_RECODE
5770
save_cur_buf(AV1_COMP * cpi)5771 static void save_cur_buf(AV1_COMP *cpi) {
5772 CODING_CONTEXT *const cc = &cpi->coding_context;
5773 AV1_COMMON *cm = &cpi->common;
5774 const YV12_BUFFER_CONFIG *ybf = &cm->cur_frame->buf;
5775 memset(&cc->copy_buffer, 0, sizeof(cc->copy_buffer));
5776 if (aom_alloc_frame_buffer(&cc->copy_buffer, ybf->y_crop_width,
5777 ybf->y_crop_height, ybf->subsampling_x,
5778 ybf->subsampling_y,
5779 ybf->flags & YV12_FLAG_HIGHBITDEPTH, ybf->border,
5780 cm->features.byte_alignment) != AOM_CODEC_OK) {
5781 aom_internal_error(
5782 &cm->error, AOM_CODEC_MEM_ERROR,
5783 "Failed to allocate copy buffer for saving coding context");
5784 }
5785 aom_yv12_copy_frame(ybf, &cc->copy_buffer, av1_num_planes(cm));
5786 }
5787
5788 // Coding context that only needs to be saved when recode loop includes
5789 // filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5790 // restoraton).
save_extra_coding_context(AV1_COMP * cpi)5791 static void save_extra_coding_context(AV1_COMP *cpi) {
5792 CODING_CONTEXT *const cc = &cpi->coding_context;
5793 AV1_COMMON *cm = &cpi->common;
5794
5795 cc->lf = cm->lf;
5796 cc->cdef_info = cm->cdef_info;
5797 cc->rc = cpi->rc;
5798 }
5799
save_all_coding_context(AV1_COMP * cpi)5800 static void save_all_coding_context(AV1_COMP *cpi) {
5801 save_cur_buf(cpi);
5802 save_extra_coding_context(cpi);
5803 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5804 }
5805
restore_cur_buf(AV1_COMP * cpi)5806 static void restore_cur_buf(AV1_COMP *cpi) {
5807 CODING_CONTEXT *const cc = &cpi->coding_context;
5808 AV1_COMMON *cm = &cpi->common;
5809 aom_yv12_copy_frame(&cc->copy_buffer, &cm->cur_frame->buf,
5810 av1_num_planes(cm));
5811 }
5812
5813 // Coding context that only needs to be restored when recode loop includes
5814 // filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5815 // restoraton).
restore_extra_coding_context(AV1_COMP * cpi)5816 static void restore_extra_coding_context(AV1_COMP *cpi) {
5817 CODING_CONTEXT *const cc = &cpi->coding_context;
5818 AV1_COMMON *cm = &cpi->common;
5819 cm->lf = cc->lf;
5820 cm->cdef_info = cc->cdef_info;
5821 cpi->rc = cc->rc;
5822 }
5823
restore_all_coding_context(AV1_COMP * cpi)5824 static void restore_all_coding_context(AV1_COMP *cpi) {
5825 restore_cur_buf(cpi);
5826 restore_extra_coding_context(cpi);
5827 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5828 }
5829
release_copy_buffer(CODING_CONTEXT * cc)5830 static void release_copy_buffer(CODING_CONTEXT *cc) {
5831 aom_free_frame_buffer(&cc->copy_buffer);
5832 }
5833
encode_with_and_without_superres(AV1_COMP * cpi,size_t * size,uint8_t * dest,int * largest_tile_id)5834 static int encode_with_and_without_superres(AV1_COMP *cpi, size_t *size,
5835 uint8_t *dest,
5836 int *largest_tile_id) {
5837 const AV1_COMMON *const cm = &cpi->common;
5838 assert(cm->seq_params.enable_superres);
5839 assert(superres_in_recode_allowed(cpi));
5840 aom_codec_err_t err = AOM_CODEC_OK;
5841 save_all_coding_context(cpi);
5842
5843 // Encode with superres.
5844 #if SUPERRES_RECODE_ALL_RATIOS
5845 AV1EncoderConfig *const oxcf = &cpi->oxcf;
5846 int64_t superres_sses[SCALE_NUMERATOR];
5847 int64_t superres_rates[SCALE_NUMERATOR];
5848 int superres_largest_tile_ids[SCALE_NUMERATOR];
5849 // Use superres for Key-frames and Alt-ref frames only.
5850 const GF_GROUP *const gf_group = &cpi->gf_group;
5851 if (gf_group->update_type[gf_group->index] != OVERLAY_UPDATE &&
5852 gf_group->update_type[gf_group->index] != INTNL_OVERLAY_UPDATE) {
5853 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
5854 ++denom) {
5855 oxcf->superres_scale_denominator = denom;
5856 oxcf->superres_kf_scale_denominator = denom;
5857 const int this_index = denom - (SCALE_NUMERATOR + 1);
5858 err = encode_with_recode_loop_and_filter(
5859 cpi, size, dest, &superres_sses[this_index],
5860 &superres_rates[this_index], &superres_largest_tile_ids[this_index]);
5861 if (err != AOM_CODEC_OK) return err;
5862 restore_all_coding_context(cpi);
5863 }
5864 // Reset.
5865 oxcf->superres_scale_denominator = SCALE_NUMERATOR;
5866 oxcf->superres_kf_scale_denominator = SCALE_NUMERATOR;
5867 } else {
5868 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
5869 ++denom) {
5870 const int this_index = denom - (SCALE_NUMERATOR + 1);
5871 superres_sses[this_index] = INT64_MAX;
5872 superres_rates[this_index] = INT64_MAX;
5873 }
5874 }
5875 #else
5876 int64_t sse1 = INT64_MAX;
5877 int64_t rate1 = INT64_MAX;
5878 int largest_tile_id1;
5879 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse1, &rate1,
5880 &largest_tile_id1);
5881 if (err != AOM_CODEC_OK) return err;
5882 restore_all_coding_context(cpi);
5883 #endif // SUPERRES_RECODE_ALL_RATIOS
5884
5885 // Encode without superres.
5886 int64_t sse2 = INT64_MAX;
5887 int64_t rate2 = INT64_MAX;
5888 int largest_tile_id2;
5889 cpi->superres_mode = SUPERRES_NONE; // To force full-res.
5890 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
5891 &largest_tile_id2);
5892 cpi->superres_mode = cpi->oxcf.superres_mode; // Reset.
5893 assert(cpi->oxcf.superres_mode == SUPERRES_AUTO);
5894 if (err != AOM_CODEC_OK) return err;
5895
5896 // Note: Both use common rdmult based on base qindex of fullres.
5897 const int64_t rdmult =
5898 av1_compute_rd_mult_based_on_qindex(cpi, cm->quant_params.base_qindex);
5899
5900 #if SUPERRES_RECODE_ALL_RATIOS
5901 // Find the best rdcost among all superres denoms.
5902 double proj_rdcost1 = DBL_MAX;
5903 int64_t sse1 = INT64_MAX;
5904 int64_t rate1 = INT64_MAX;
5905 int largest_tile_id1 = 0;
5906 (void)sse1;
5907 (void)rate1;
5908 (void)largest_tile_id1;
5909 int best_denom = -1;
5910 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR; ++denom) {
5911 const int this_index = denom - (SCALE_NUMERATOR + 1);
5912 const int64_t this_sse = superres_sses[this_index];
5913 const int64_t this_rate = superres_rates[this_index];
5914 const int this_largest_tile_id = superres_largest_tile_ids[this_index];
5915 const double this_rdcost = RDCOST_DBL(rdmult, this_rate, this_sse);
5916 if (this_rdcost < proj_rdcost1) {
5917 sse1 = this_sse;
5918 rate1 = this_rate;
5919 largest_tile_id1 = this_largest_tile_id;
5920 proj_rdcost1 = this_rdcost;
5921 best_denom = denom;
5922 }
5923 }
5924 #else
5925 const double proj_rdcost1 = RDCOST_DBL(rdmult, rate1, sse1);
5926 #endif // SUPERRES_RECODE_ALL_RATIOS
5927 const double proj_rdcost2 = RDCOST_DBL(rdmult, rate2, sse2);
5928
5929 // Re-encode with superres if it's better.
5930 if (proj_rdcost1 < proj_rdcost2) {
5931 restore_all_coding_context(cpi);
5932 // TODO(urvang): We should avoid rerunning the recode loop by saving
5933 // previous output+state, or running encode only for the selected 'q' in
5934 // previous step.
5935 #if SUPERRES_RECODE_ALL_RATIOS
5936 // Again, temporarily force the best denom.
5937 oxcf->superres_scale_denominator = best_denom;
5938 oxcf->superres_kf_scale_denominator = best_denom;
5939 #endif // SUPERRES_RECODE_ALL_RATIOS
5940 int64_t sse3 = INT64_MAX;
5941 int64_t rate3 = INT64_MAX;
5942 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
5943 largest_tile_id);
5944 assert(sse1 == sse3);
5945 assert(rate1 == rate3);
5946 assert(largest_tile_id1 == *largest_tile_id);
5947 #if SUPERRES_RECODE_ALL_RATIOS
5948 // Reset.
5949 oxcf->superres_scale_denominator = SCALE_NUMERATOR;
5950 oxcf->superres_kf_scale_denominator = SCALE_NUMERATOR;
5951 #endif // SUPERRES_RECODE_ALL_RATIOS
5952 } else {
5953 *largest_tile_id = largest_tile_id2;
5954 }
5955
5956 release_copy_buffer(&cpi->coding_context);
5957
5958 return err;
5959 }
5960 #endif // CONFIG_SUPERRES_IN_RECODE
5961
5962 #define DUMP_RECON_FRAMES 0
5963
5964 #if DUMP_RECON_FRAMES == 1
5965 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
dump_filtered_recon_frames(AV1_COMP * cpi)5966 static void dump_filtered_recon_frames(AV1_COMP *cpi) {
5967 AV1_COMMON *const cm = &cpi->common;
5968 const CurrentFrame *const current_frame = &cm->current_frame;
5969 const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf;
5970
5971 if (recon_buf == NULL) {
5972 printf("Frame %d is not ready.\n", current_frame->frame_number);
5973 return;
5974 }
5975
5976 static const int flag_list[REF_FRAMES] = { 0,
5977 AOM_LAST_FLAG,
5978 AOM_LAST2_FLAG,
5979 AOM_LAST3_FLAG,
5980 AOM_GOLD_FLAG,
5981 AOM_BWD_FLAG,
5982 AOM_ALT2_FLAG,
5983 AOM_ALT_FLAG };
5984 printf(
5985 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
5986 "show_existing_frame=%d) "
5987 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
5988 current_frame->frame_number, current_frame->order_hint, cm->show_frame,
5989 cm->show_existing_frame);
5990 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
5991 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
5992 const int ref_offset = buf != NULL ? (int)buf->order_hint : -1;
5993 printf(" %d(%c)", ref_offset,
5994 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N');
5995 }
5996 printf(" ]\n");
5997
5998 if (!cm->show_frame) {
5999 printf("Frame %d is a no show frame, so no image dump.\n",
6000 current_frame->frame_number);
6001 return;
6002 }
6003
6004 int h;
6005 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
6006 FILE *f_recon = NULL;
6007
6008 if (current_frame->frame_number == 0) {
6009 if ((f_recon = fopen(file_name, "wb")) == NULL) {
6010 printf("Unable to open file %s to write.\n", file_name);
6011 return;
6012 }
6013 } else {
6014 if ((f_recon = fopen(file_name, "ab")) == NULL) {
6015 printf("Unable to open file %s to append.\n", file_name);
6016 return;
6017 }
6018 }
6019 printf(
6020 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
6021 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
6022 "refresh_alt_ref_frame=%d, "
6023 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
6024 current_frame->frame_number, cpi->gf_group.index,
6025 cpi->gf_group.update_type[cpi->gf_group.index], current_frame->order_hint,
6026 cm->show_frame, cm->show_existing_frame, cpi->rc.source_alt_ref_active,
6027 cpi->refresh_alt_ref_frame, recon_buf->y_stride, recon_buf->uv_stride,
6028 cm->width, cm->height);
6029 #if 0
6030 int ref_frame;
6031 printf("get_ref_frame_map_idx: [");
6032 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
6033 printf(" %d", get_ref_frame_map_idx(cm, ref_frame));
6034 printf(" ]\n");
6035 #endif // 0
6036
6037 // --- Y ---
6038 for (h = 0; h < cm->height; ++h) {
6039 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
6040 f_recon);
6041 }
6042 // --- U ---
6043 for (h = 0; h < (cm->height >> 1); ++h) {
6044 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
6045 f_recon);
6046 }
6047 // --- V ---
6048 for (h = 0; h < (cm->height >> 1); ++h) {
6049 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
6050 f_recon);
6051 }
6052
6053 fclose(f_recon);
6054 }
6055 #endif // DUMP_RECON_FRAMES
6056
is_integer_mv(const YV12_BUFFER_CONFIG * cur_picture,const YV12_BUFFER_CONFIG * last_picture,ForceIntegerMVInfo * const force_intpel_info)6057 static int is_integer_mv(const YV12_BUFFER_CONFIG *cur_picture,
6058 const YV12_BUFFER_CONFIG *last_picture,
6059 ForceIntegerMVInfo *const force_intpel_info) {
6060 aom_clear_system_state();
6061 // check use hash ME
6062 int k;
6063
6064 const int block_size = FORCE_INT_MV_DECISION_BLOCK_SIZE;
6065 const double threshold_current = 0.8;
6066 const double threshold_average = 0.95;
6067 const int max_history_size = 32;
6068 int T = 0; // total block
6069 int C = 0; // match with collocated block
6070 int S = 0; // smooth region but not match with collocated block
6071
6072 const int pic_width = cur_picture->y_width;
6073 const int pic_height = cur_picture->y_height;
6074 for (int i = 0; i + block_size <= pic_height; i += block_size) {
6075 for (int j = 0; j + block_size <= pic_width; j += block_size) {
6076 const int x_pos = j;
6077 const int y_pos = i;
6078 int match = 1;
6079 T++;
6080
6081 // check whether collocated block match with current
6082 uint8_t *p_cur = cur_picture->y_buffer;
6083 uint8_t *p_ref = last_picture->y_buffer;
6084 int stride_cur = cur_picture->y_stride;
6085 int stride_ref = last_picture->y_stride;
6086 p_cur += (y_pos * stride_cur + x_pos);
6087 p_ref += (y_pos * stride_ref + x_pos);
6088
6089 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
6090 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
6091 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
6092 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
6093 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
6094 if (p16_cur[tmpX] != p16_ref[tmpX]) {
6095 match = 0;
6096 }
6097 }
6098 p16_cur += stride_cur;
6099 p16_ref += stride_ref;
6100 }
6101 } else {
6102 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
6103 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
6104 if (p_cur[tmpX] != p_ref[tmpX]) {
6105 match = 0;
6106 }
6107 }
6108 p_cur += stride_cur;
6109 p_ref += stride_ref;
6110 }
6111 }
6112
6113 if (match) {
6114 C++;
6115 continue;
6116 }
6117
6118 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
6119 y_pos) ||
6120 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
6121 S++;
6122 continue;
6123 }
6124 }
6125 }
6126
6127 assert(T > 0);
6128 double cs_rate = ((double)(C + S)) / ((double)(T));
6129
6130 force_intpel_info->cs_rate_array[force_intpel_info->rate_index] = cs_rate;
6131
6132 force_intpel_info->rate_index =
6133 (force_intpel_info->rate_index + 1) % max_history_size;
6134 force_intpel_info->rate_size++;
6135 force_intpel_info->rate_size =
6136 AOMMIN(force_intpel_info->rate_size, max_history_size);
6137
6138 if (cs_rate < threshold_current) {
6139 return 0;
6140 }
6141
6142 if (C == T) {
6143 return 1;
6144 }
6145
6146 double cs_average = 0.0;
6147
6148 for (k = 0; k < force_intpel_info->rate_size; k++) {
6149 cs_average += force_intpel_info->cs_rate_array[k];
6150 }
6151 cs_average /= force_intpel_info->rate_size;
6152
6153 if (cs_average < threshold_average) {
6154 return 0;
6155 }
6156
6157 if ((T - C - S) < 0) {
6158 return 1;
6159 }
6160
6161 if (cs_average > 1.01) {
6162 return 1;
6163 }
6164
6165 return 0;
6166 }
6167
6168 // Refresh reference frame buffers according to refresh_frame_flags.
refresh_reference_frames(AV1_COMP * cpi)6169 static void refresh_reference_frames(AV1_COMP *cpi) {
6170 AV1_COMMON *const cm = &cpi->common;
6171 // All buffers are refreshed for shown keyframes and S-frames.
6172
6173 for (int ref_frame = 0; ref_frame < REF_FRAMES; ref_frame++) {
6174 if (((cm->current_frame.refresh_frame_flags >> ref_frame) & 1) == 1) {
6175 assign_frame_buffer_p(&cm->ref_frame_map[ref_frame], cm->cur_frame);
6176 }
6177 }
6178 }
6179
set_mb_ssim_rdmult_scaling(AV1_COMP * cpi)6180 static void set_mb_ssim_rdmult_scaling(AV1_COMP *cpi) {
6181 const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
6182 ThreadData *td = &cpi->td;
6183 MACROBLOCK *x = &td->mb;
6184 MACROBLOCKD *xd = &x->e_mbd;
6185 uint8_t *y_buffer = cpi->source->y_buffer;
6186 const int y_stride = cpi->source->y_stride;
6187 const int block_size = BLOCK_16X16;
6188
6189 const int num_mi_w = mi_size_wide[block_size];
6190 const int num_mi_h = mi_size_high[block_size];
6191 const int num_cols = (mi_params->mi_cols + num_mi_w - 1) / num_mi_w;
6192 const int num_rows = (mi_params->mi_rows + num_mi_h - 1) / num_mi_h;
6193 double log_sum = 0.0;
6194 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
6195
6196 // Loop through each 16x16 block.
6197 for (int row = 0; row < num_rows; ++row) {
6198 for (int col = 0; col < num_cols; ++col) {
6199 double var = 0.0, num_of_var = 0.0;
6200 const int index = row * num_cols + col;
6201
6202 // Loop through each 8x8 block.
6203 for (int mi_row = row * num_mi_h;
6204 mi_row < mi_params->mi_rows && mi_row < (row + 1) * num_mi_h;
6205 mi_row += 2) {
6206 for (int mi_col = col * num_mi_w;
6207 mi_col < mi_params->mi_cols && mi_col < (col + 1) * num_mi_w;
6208 mi_col += 2) {
6209 struct buf_2d buf;
6210 const int row_offset_y = mi_row << 2;
6211 const int col_offset_y = mi_col << 2;
6212
6213 buf.buf = y_buffer + row_offset_y * y_stride + col_offset_y;
6214 buf.stride = y_stride;
6215
6216 if (use_hbd) {
6217 var += av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8,
6218 xd->bd);
6219 } else {
6220 var += av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8);
6221 }
6222
6223 num_of_var += 1.0;
6224 }
6225 }
6226 var = var / num_of_var;
6227
6228 // Curve fitting with an exponential model on all 16x16 blocks from the
6229 // midres dataset.
6230 var = 67.035434 * (1 - exp(-0.0021489 * var)) + 17.492222;
6231 cpi->ssim_rdmult_scaling_factors[index] = var;
6232 log_sum += log(var);
6233 }
6234 }
6235 log_sum = exp(log_sum / (double)(num_rows * num_cols));
6236
6237 for (int row = 0; row < num_rows; ++row) {
6238 for (int col = 0; col < num_cols; ++col) {
6239 const int index = row * num_cols + col;
6240 cpi->ssim_rdmult_scaling_factors[index] /= log_sum;
6241 }
6242 }
6243 }
6244
6245 extern void av1_print_frame_contexts(const FRAME_CONTEXT *fc,
6246 const char *filename);
6247
encode_frame_to_data_rate(AV1_COMP * cpi,size_t * size,uint8_t * dest)6248 static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
6249 uint8_t *dest) {
6250 AV1_COMMON *const cm = &cpi->common;
6251 SequenceHeader *const seq_params = &cm->seq_params;
6252 CurrentFrame *const current_frame = &cm->current_frame;
6253 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
6254 struct segmentation *const seg = &cm->seg;
6255 FeatureFlags *const features = &cm->features;
6256
6257 #if CONFIG_COLLECT_COMPONENT_TIMING
6258 start_timing(cpi, encode_frame_to_data_rate_time);
6259 #endif
6260
6261 // frame type has been decided outside of this function call
6262 cm->cur_frame->frame_type = current_frame->frame_type;
6263
6264 cm->tiles.large_scale = cpi->oxcf.large_scale_tile;
6265 cm->tiles.single_tile_decoding = cpi->oxcf.single_tile_decoding;
6266
6267 features->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
6268 // features->allow_ref_frame_mvs needs to be written into the frame header
6269 // while cm->tiles.large_scale is 1, therefore, "cm->tiles.large_scale=1" case
6270 // is separated from frame_might_allow_ref_frame_mvs().
6271 features->allow_ref_frame_mvs &= !cm->tiles.large_scale;
6272
6273 features->allow_warped_motion =
6274 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
6275
6276 cpi->last_frame_type = current_frame->frame_type;
6277
6278 if (encode_show_existing_frame(cm)) {
6279 finalize_encoded_frame(cpi);
6280 // Build the bitstream
6281 int largest_tile_id = 0; // Output from bitstream: unused here
6282 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
6283 return AOM_CODEC_ERROR;
6284
6285 if (seq_params->frame_id_numbers_present_flag &&
6286 current_frame->frame_type == KEY_FRAME) {
6287 // Displaying a forward key-frame, so reset the ref buffer IDs
6288 int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
6289 for (int i = 0; i < REF_FRAMES; i++)
6290 cm->ref_frame_id[i] = display_frame_id;
6291 }
6292
6293 cpi->seq_params_locked = 1;
6294
6295 #if DUMP_RECON_FRAMES == 1
6296 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
6297 dump_filtered_recon_frames(cpi);
6298 #endif // DUMP_RECON_FRAMES
6299
6300 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
6301 // for the purpose to verify no mismatch between encoder and decoder.
6302 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
6303
6304 refresh_reference_frames(cpi);
6305
6306 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
6307 // to do post-encoding update accordingly.
6308 if (cpi->rc.is_src_frame_alt_ref) {
6309 av1_set_target_rate(cpi, cm->width, cm->height);
6310 av1_rc_postencode_update(cpi, *size);
6311 }
6312
6313 ++current_frame->frame_number;
6314
6315 return AOM_CODEC_OK;
6316 }
6317
6318 // Work out whether to force_integer_mv this frame
6319 if (!is_stat_generation_stage(cpi) &&
6320 cpi->common.features.allow_screen_content_tools &&
6321 !frame_is_intra_only(cm)) {
6322 if (cpi->common.seq_params.force_integer_mv == 2) {
6323 // Adaptive mode: see what previous frame encoded did
6324 if (cpi->unscaled_last_source != NULL) {
6325 features->cur_frame_force_integer_mv = is_integer_mv(
6326 cpi->source, cpi->unscaled_last_source, &cpi->force_intpel_info);
6327 } else {
6328 cpi->common.features.cur_frame_force_integer_mv = 0;
6329 }
6330 } else {
6331 cpi->common.features.cur_frame_force_integer_mv =
6332 cpi->common.seq_params.force_integer_mv;
6333 }
6334 } else {
6335 cpi->common.features.cur_frame_force_integer_mv = 0;
6336 }
6337
6338 // Set default state for segment based loop filter update flags.
6339 cm->lf.mode_ref_delta_update = 0;
6340
6341 // Set various flags etc to special state if it is a key frame.
6342 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
6343 // Reset the loop filter deltas and segmentation map.
6344 av1_reset_segment_features(cm);
6345
6346 // If segmentation is enabled force a map update for key frames.
6347 if (seg->enabled) {
6348 seg->update_map = 1;
6349 seg->update_data = 1;
6350 }
6351
6352 // The alternate reference frame cannot be active for a key frame.
6353 cpi->rc.source_alt_ref_active = 0;
6354 }
6355 if (cpi->oxcf.mtu == 0) {
6356 cpi->num_tg = cpi->oxcf.num_tile_groups;
6357 } else {
6358 // Use a default value for the purposes of weighting costs in probability
6359 // updates
6360 cpi->num_tg = DEFAULT_MAX_NUM_TG;
6361 }
6362
6363 // For 1 pass CBR, check if we are dropping this frame.
6364 // Never drop on key frame.
6365 if (has_no_stats_stage(cpi) && oxcf->rc_mode == AOM_CBR &&
6366 current_frame->frame_type != KEY_FRAME) {
6367 if (av1_rc_drop_frame(cpi)) {
6368 av1_rc_postencode_update_drop_frame(cpi);
6369 release_scaled_references(cpi);
6370 return AOM_CODEC_OK;
6371 }
6372 }
6373
6374 if (oxcf->tuning == AOM_TUNE_SSIM) set_mb_ssim_rdmult_scaling(cpi);
6375
6376 #if CONFIG_TUNE_VMAF
6377 if (oxcf->tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
6378 oxcf->tuning == AOM_TUNE_VMAF_MAX_GAIN) {
6379 av1_set_mb_vmaf_rdmult_scaling(cpi);
6380 }
6381 #endif
6382
6383 aom_clear_system_state();
6384
6385 #if CONFIG_INTERNAL_STATS
6386 memset(cpi->mode_chosen_counts, 0,
6387 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
6388 #endif
6389
6390 if (seq_params->frame_id_numbers_present_flag) {
6391 /* Non-normative definition of current_frame_id ("frame counter" with
6392 * wraparound) */
6393 if (cm->current_frame_id == -1) {
6394 int lsb, msb;
6395 /* quasi-random initialization of current_frame_id for a key frame */
6396 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
6397 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
6398 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
6399 } else {
6400 lsb = cpi->source->y_buffer[0] & 0xff;
6401 msb = cpi->source->y_buffer[1] & 0xff;
6402 }
6403 cm->current_frame_id =
6404 ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
6405
6406 // S_frame is meant for stitching different streams of different
6407 // resolutions together, so current_frame_id must be the
6408 // same across different streams of the same content current_frame_id
6409 // should be the same and not random. 0x37 is a chosen number as start
6410 // point
6411 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
6412 } else {
6413 cm->current_frame_id =
6414 (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
6415 (1 << seq_params->frame_id_length);
6416 }
6417 }
6418
6419 switch (cpi->oxcf.cdf_update_mode) {
6420 case 0: // No CDF update for any frames(4~6% compression loss).
6421 features->disable_cdf_update = 1;
6422 break;
6423 case 1: // Enable CDF update for all frames.
6424 features->disable_cdf_update = 0;
6425 break;
6426 case 2:
6427 // Strategically determine at which frames to do CDF update.
6428 // Currently only enable CDF update for all-intra and no-show frames(1.5%
6429 // compression loss).
6430 // TODO(huisu@google.com): design schemes for various trade-offs between
6431 // compression quality and decoding speed.
6432 features->disable_cdf_update =
6433 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
6434 break;
6435 }
6436 seq_params->timing_info_present &= !seq_params->reduced_still_picture_hdr;
6437
6438 int largest_tile_id = 0;
6439 #if CONFIG_SUPERRES_IN_RECODE
6440 if (superres_in_recode_allowed(cpi)) {
6441 if (encode_with_and_without_superres(cpi, size, dest, &largest_tile_id) !=
6442 AOM_CODEC_OK) {
6443 return AOM_CODEC_ERROR;
6444 }
6445 } else {
6446 #endif // CONFIG_SUPERRES_IN_RECODE
6447 if (encode_with_recode_loop_and_filter(cpi, size, dest, NULL, NULL,
6448 &largest_tile_id) != AOM_CODEC_OK) {
6449 return AOM_CODEC_ERROR;
6450 }
6451 #if CONFIG_SUPERRES_IN_RECODE
6452 }
6453 #endif // CONFIG_SUPERRES_IN_RECODE
6454
6455 cpi->seq_params_locked = 1;
6456
6457 // Update reference frame ids for reference frames this frame will overwrite
6458 if (seq_params->frame_id_numbers_present_flag) {
6459 for (int i = 0; i < REF_FRAMES; i++) {
6460 if ((current_frame->refresh_frame_flags >> i) & 1) {
6461 cm->ref_frame_id[i] = cm->current_frame_id;
6462 }
6463 }
6464 }
6465
6466 #if DUMP_RECON_FRAMES == 1
6467 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
6468 dump_filtered_recon_frames(cpi);
6469 #endif // DUMP_RECON_FRAMES
6470
6471 if (cm->seg.enabled) {
6472 if (cm->seg.update_map) {
6473 update_reference_segmentation_map(cpi);
6474 } else if (cm->last_frame_seg_map) {
6475 memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
6476 cm->mi_params.mi_cols * cm->mi_params.mi_rows * sizeof(uint8_t));
6477 }
6478 }
6479
6480 if (frame_is_intra_only(cm) == 0) {
6481 release_scaled_references(cpi);
6482 }
6483
6484 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
6485 // for the purpose to verify no mismatch between encoder and decoder.
6486 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
6487
6488 refresh_reference_frames(cpi);
6489
6490 #if CONFIG_ENTROPY_STATS
6491 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
6492 #endif // CONFIG_ENTROPY_STATS
6493
6494 if (features->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
6495 *cm->fc = cpi->tile_data[largest_tile_id].tctx;
6496 av1_reset_cdf_symbol_counters(cm->fc);
6497 }
6498 if (!cm->tiles.large_scale) {
6499 cm->cur_frame->frame_context = *cm->fc;
6500 }
6501
6502 if (cpi->oxcf.ext_tile_debug) {
6503 // (yunqing) This test ensures the correctness of large scale tile coding.
6504 if (cm->tiles.large_scale && is_stat_consumption_stage(cpi)) {
6505 char fn[20] = "./fc";
6506 fn[4] = current_frame->frame_number / 100 + '0';
6507 fn[5] = (current_frame->frame_number % 100) / 10 + '0';
6508 fn[6] = (current_frame->frame_number % 10) + '0';
6509 fn[7] = '\0';
6510 av1_print_frame_contexts(cm->fc, fn);
6511 }
6512 }
6513
6514 #if CONFIG_COLLECT_COMPONENT_TIMING
6515 end_timing(cpi, encode_frame_to_data_rate_time);
6516
6517 // Print out timing information.
6518 int i;
6519 fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n",
6520 cm->current_frame.frame_number,
6521 get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame);
6522 for (i = 0; i < kTimingComponents; i++) {
6523 cpi->component_time[i] += cpi->frame_component_time[i];
6524 fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n",
6525 get_component_name(i), cpi->frame_component_time[i],
6526 cpi->component_time[i]);
6527 cpi->frame_component_time[i] = 0;
6528 }
6529 #endif
6530
6531 cpi->last_frame_type = current_frame->frame_type;
6532
6533 av1_rc_postencode_update(cpi, *size);
6534
6535 // Clear the one shot update flags for segmentation map and mode/ref loop
6536 // filter deltas.
6537 cm->seg.update_map = 0;
6538 cm->seg.update_data = 0;
6539 cm->lf.mode_ref_delta_update = 0;
6540
6541 // A droppable frame might not be shown but it always
6542 // takes a space in the gf group. Therefore, even when
6543 // it is not shown, we still need update the count down.
6544
6545 if (cm->show_frame) {
6546 // Don't increment frame counters if this was an altref buffer
6547 // update not a real frame
6548 ++current_frame->frame_number;
6549 }
6550
6551 return AOM_CODEC_OK;
6552 }
6553
av1_encode(AV1_COMP * const cpi,uint8_t * const dest,const EncodeFrameInput * const frame_input,const EncodeFrameParams * const frame_params,EncodeFrameResults * const frame_results)6554 int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
6555 const EncodeFrameInput *const frame_input,
6556 const EncodeFrameParams *const frame_params,
6557 EncodeFrameResults *const frame_results) {
6558 AV1_COMMON *const cm = &cpi->common;
6559 CurrentFrame *const current_frame = &cm->current_frame;
6560
6561 cpi->unscaled_source = frame_input->source;
6562 cpi->source = frame_input->source;
6563 cpi->unscaled_last_source = frame_input->last_source;
6564
6565 current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
6566 cm->features.error_resilient_mode = frame_params->error_resilient_mode;
6567 cm->features.primary_ref_frame = frame_params->primary_ref_frame;
6568 cm->current_frame.frame_type = frame_params->frame_type;
6569 cm->show_frame = frame_params->show_frame;
6570 cpi->ref_frame_flags = frame_params->ref_frame_flags;
6571 cpi->speed = frame_params->speed;
6572 cm->show_existing_frame = frame_params->show_existing_frame;
6573 cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
6574
6575 memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
6576 REF_FRAMES * sizeof(*cm->remapped_ref_idx));
6577
6578 cpi->refresh_golden_frame = frame_params->refresh_golden_frame;
6579 cpi->refresh_bwd_ref_frame = frame_params->refresh_bwd_ref_frame;
6580 cpi->refresh_alt_ref_frame = frame_params->refresh_alt_ref_frame;
6581
6582 if (current_frame->frame_type == KEY_FRAME && cm->show_frame)
6583 current_frame->frame_number = 0;
6584
6585 current_frame->order_hint =
6586 current_frame->frame_number + frame_params->order_offset;
6587 current_frame->display_order_hint = current_frame->order_hint;
6588 current_frame->order_hint %=
6589 (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
6590
6591 if (is_stat_generation_stage(cpi)) {
6592 #if !CONFIG_REALTIME_ONLY
6593 av1_first_pass(cpi, frame_input->ts_duration);
6594 #endif
6595 } else if (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) {
6596 if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
6597 AOM_CODEC_OK) {
6598 return AOM_CODEC_ERROR;
6599 }
6600 } else {
6601 return AOM_CODEC_ERROR;
6602 }
6603
6604 return AOM_CODEC_OK;
6605 }
6606
6607 #if CONFIG_DENOISE
apply_denoise_2d(AV1_COMP * cpi,YV12_BUFFER_CONFIG * sd,int block_size,float noise_level,int64_t time_stamp,int64_t end_time)6608 static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
6609 int block_size, float noise_level,
6610 int64_t time_stamp, int64_t end_time) {
6611 AV1_COMMON *const cm = &cpi->common;
6612 if (!cpi->denoise_and_model) {
6613 cpi->denoise_and_model = aom_denoise_and_model_alloc(
6614 cm->seq_params.bit_depth, block_size, noise_level);
6615 if (!cpi->denoise_and_model) {
6616 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6617 "Error allocating denoise and model");
6618 return -1;
6619 }
6620 }
6621 if (!cpi->film_grain_table) {
6622 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
6623 if (!cpi->film_grain_table) {
6624 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6625 "Error allocating grain table");
6626 return -1;
6627 }
6628 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
6629 }
6630 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
6631 &cm->film_grain_params)) {
6632 if (cm->film_grain_params.apply_grain) {
6633 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
6634 &cm->film_grain_params);
6635 }
6636 }
6637 return 0;
6638 }
6639 #endif
6640
av1_receive_raw_frame(AV1_COMP * cpi,aom_enc_frame_flags_t frame_flags,YV12_BUFFER_CONFIG * sd,int64_t time_stamp,int64_t end_time)6641 int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
6642 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
6643 int64_t end_time) {
6644 AV1_COMMON *const cm = &cpi->common;
6645 const SequenceHeader *const seq_params = &cm->seq_params;
6646 int res = 0;
6647 const int subsampling_x = sd->subsampling_x;
6648 const int subsampling_y = sd->subsampling_y;
6649 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
6650
6651 #if CONFIG_TUNE_VMAF
6652 if (!is_stat_generation_stage(cpi) &&
6653 cpi->oxcf.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING) {
6654 av1_vmaf_frame_preprocessing(cpi, sd);
6655 }
6656 if (!is_stat_generation_stage(cpi) &&
6657 cpi->oxcf.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
6658 av1_vmaf_blk_preprocessing(cpi, sd);
6659 }
6660 #endif
6661
6662 #if CONFIG_INTERNAL_STATS
6663 struct aom_usec_timer timer;
6664 aom_usec_timer_start(&timer);
6665 #endif
6666 #if CONFIG_DENOISE
6667 if (cpi->oxcf.noise_level > 0)
6668 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
6669 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
6670 res = -1;
6671 #endif // CONFIG_DENOISE
6672
6673 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
6674 use_highbitdepth, frame_flags))
6675 res = -1;
6676 #if CONFIG_INTERNAL_STATS
6677 aom_usec_timer_mark(&timer);
6678 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
6679 #endif
6680 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
6681 (subsampling_x != 1 || subsampling_y != 1)) {
6682 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
6683 "Non-4:2:0 color format requires profile 1 or 2");
6684 res = -1;
6685 }
6686 if ((seq_params->profile == PROFILE_1) &&
6687 !(subsampling_x == 0 && subsampling_y == 0)) {
6688 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
6689 "Profile 1 requires 4:4:4 color format");
6690 res = -1;
6691 }
6692 if ((seq_params->profile == PROFILE_2) &&
6693 (seq_params->bit_depth <= AOM_BITS_10) &&
6694 !(subsampling_x == 1 && subsampling_y == 0)) {
6695 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
6696 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
6697 res = -1;
6698 }
6699
6700 return res;
6701 }
6702
6703 #if CONFIG_INTERNAL_STATS
6704 extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
6705 const unsigned char *img2, int img2_pitch,
6706 int width, int height);
6707
adjust_image_stat(double y,double u,double v,double all,ImageStat * s)6708 static void adjust_image_stat(double y, double u, double v, double all,
6709 ImageStat *s) {
6710 s->stat[STAT_Y] += y;
6711 s->stat[STAT_U] += u;
6712 s->stat[STAT_V] += v;
6713 s->stat[STAT_ALL] += all;
6714 s->worst = AOMMIN(s->worst, all);
6715 }
6716
compute_internal_stats(AV1_COMP * cpi,int frame_bytes)6717 static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
6718 AV1_COMMON *const cm = &cpi->common;
6719 double samples = 0.0;
6720 const uint32_t in_bit_depth = cpi->oxcf.input_bit_depth;
6721 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
6722
6723 #if CONFIG_INTER_STATS_ONLY
6724 if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame
6725 #endif
6726 cpi->bytes += frame_bytes;
6727 if (cm->show_frame) {
6728 const YV12_BUFFER_CONFIG *orig = cpi->source;
6729 const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
6730 double y, u, v, frame_all;
6731
6732 cpi->count++;
6733 if (cpi->b_calculate_psnr) {
6734 PSNR_STATS psnr;
6735 double frame_ssim2 = 0.0, weight = 0.0;
6736 aom_clear_system_state();
6737 #if CONFIG_AV1_HIGHBITDEPTH
6738 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
6739 #else
6740 aom_calc_psnr(orig, recon, &psnr);
6741 #endif
6742 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
6743 &cpi->psnr);
6744 cpi->total_sq_error += psnr.sse[0];
6745 cpi->total_samples += psnr.samples[0];
6746 samples = psnr.samples[0];
6747 // TODO(yaowu): unify these two versions into one.
6748 if (cm->seq_params.use_highbitdepth)
6749 frame_ssim2 =
6750 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
6751 else
6752 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
6753
6754 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
6755 cpi->summed_quality += frame_ssim2 * weight;
6756 cpi->summed_weights += weight;
6757
6758 #if 0
6759 {
6760 FILE *f = fopen("q_used.stt", "a");
6761 double y2 = psnr.psnr[1];
6762 double u2 = psnr.psnr[2];
6763 double v2 = psnr.psnr[3];
6764 double frame_psnr2 = psnr.psnr[0];
6765 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
6766 cm->current_frame.frame_number, y2, u2, v2,
6767 frame_psnr2, frame_ssim2);
6768 fclose(f);
6769 }
6770 #endif
6771 }
6772 if (cpi->b_calculate_blockiness) {
6773 if (!cm->seq_params.use_highbitdepth) {
6774 const double frame_blockiness =
6775 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
6776 recon->y_stride, orig->y_width, orig->y_height);
6777 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
6778 cpi->total_blockiness += frame_blockiness;
6779 }
6780
6781 if (cpi->b_calculate_consistency) {
6782 if (!cm->seq_params.use_highbitdepth) {
6783 const double this_inconsistency = aom_get_ssim_metrics(
6784 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
6785 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
6786
6787 const double peak = (double)((1 << in_bit_depth) - 1);
6788 const double consistency =
6789 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
6790 if (consistency > 0.0)
6791 cpi->worst_consistency =
6792 AOMMIN(cpi->worst_consistency, consistency);
6793 cpi->total_inconsistency += this_inconsistency;
6794 }
6795 }
6796 }
6797
6798 frame_all =
6799 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
6800 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
6801 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
6802 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
6803 }
6804 }
6805 #endif // CONFIG_INTERNAL_STATS
av1_get_compressed_data(AV1_COMP * cpi,unsigned int * frame_flags,size_t * size,uint8_t * dest,int64_t * time_stamp,int64_t * time_end,int flush,const aom_rational64_t * timestamp_ratio)6806 int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
6807 size_t *size, uint8_t *dest, int64_t *time_stamp,
6808 int64_t *time_end, int flush,
6809 const aom_rational64_t *timestamp_ratio) {
6810 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
6811 AV1_COMMON *const cm = &cpi->common;
6812
6813 #if CONFIG_BITSTREAM_DEBUG
6814 assert(cpi->oxcf.max_threads == 0 &&
6815 "bitstream debug tool does not support multithreading");
6816 bitstream_queue_record_write();
6817 aom_bitstream_queue_set_frame_write(cm->current_frame.frame_number * 2 +
6818 cm->show_frame);
6819 #endif
6820 if (cpi->use_svc && cm->number_spatial_layers > 1) {
6821 av1_one_pass_cbr_svc_start_layer(cpi);
6822 }
6823
6824 cm->showable_frame = 0;
6825 *size = 0;
6826 #if CONFIG_INTERNAL_STATS
6827 struct aom_usec_timer cmptimer;
6828 aom_usec_timer_start(&cmptimer);
6829 #endif
6830 av1_set_high_precision_mv(cpi, 1, 0);
6831
6832 // Normal defaults
6833 cm->features.refresh_frame_context = oxcf->frame_parallel_decoding_mode
6834 ? REFRESH_FRAME_CONTEXT_DISABLED
6835 : REFRESH_FRAME_CONTEXT_BACKWARD;
6836 if (oxcf->large_scale_tile)
6837 cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
6838
6839 // Initialize fields related to forward keyframes
6840 cpi->no_show_kf = 0;
6841
6842 if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
6843
6844 const int result =
6845 av1_encode_strategy(cpi, size, dest, frame_flags, time_stamp, time_end,
6846 timestamp_ratio, flush);
6847 if (result != AOM_CODEC_OK && result != -1) {
6848 return AOM_CODEC_ERROR;
6849 } else if (result == -1) {
6850 // Returning -1 indicates no frame encoded; more input is required
6851 return -1;
6852 }
6853 #if CONFIG_INTERNAL_STATS
6854 aom_usec_timer_mark(&cmptimer);
6855 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
6856 #endif // CONFIG_INTERNAL_STATS
6857 if (cpi->b_calculate_psnr) {
6858 if (cm->show_existing_frame ||
6859 (!is_stat_generation_stage(cpi) && cm->show_frame)) {
6860 generate_psnr_packet(cpi);
6861 }
6862 }
6863
6864 #if CONFIG_TUNE_VMAF
6865 if (!is_stat_generation_stage(cpi) &&
6866 (oxcf->tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING ||
6867 oxcf->tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
6868 oxcf->tuning == AOM_TUNE_VMAF_MAX_GAIN)) {
6869 av1_update_vmaf_curve(cpi, cpi->source, &cpi->common.cur_frame->buf);
6870 }
6871 #endif
6872
6873 if (cpi->level_params.keep_level_stats && !is_stat_generation_stage(cpi)) {
6874 // Initialize level info. at the beginning of each sequence.
6875 if (cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) {
6876 av1_init_level_info(cpi);
6877 }
6878 av1_update_level_info(cpi, *size, *time_stamp, *time_end);
6879 }
6880
6881 #if CONFIG_INTERNAL_STATS
6882 if (!is_stat_generation_stage(cpi)) {
6883 compute_internal_stats(cpi, (int)(*size));
6884 }
6885 #endif // CONFIG_INTERNAL_STATS
6886 #if CONFIG_SPEED_STATS
6887 if (!is_stat_generation_stage(cpi) && !cm->show_existing_frame) {
6888 cpi->tx_search_count += cpi->td.mb.tx_search_count;
6889 cpi->td.mb.tx_search_count = 0;
6890 }
6891 #endif // CONFIG_SPEED_STATS
6892
6893 aom_clear_system_state();
6894
6895 return AOM_CODEC_OK;
6896 }
6897
av1_get_preview_raw_frame(AV1_COMP * cpi,YV12_BUFFER_CONFIG * dest)6898 int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6899 AV1_COMMON *cm = &cpi->common;
6900 if (!cm->show_frame) {
6901 return -1;
6902 } else {
6903 int ret;
6904 if (cm->cur_frame != NULL) {
6905 *dest = cm->cur_frame->buf;
6906 dest->y_width = cm->width;
6907 dest->y_height = cm->height;
6908 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
6909 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
6910 ret = 0;
6911 } else {
6912 ret = -1;
6913 }
6914 aom_clear_system_state();
6915 return ret;
6916 }
6917 }
6918
av1_get_last_show_frame(AV1_COMP * cpi,YV12_BUFFER_CONFIG * frame)6919 int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
6920 if (cpi->last_show_frame_buf == NULL) return -1;
6921
6922 *frame = cpi->last_show_frame_buf->buf;
6923 return 0;
6924 }
6925
equal_dimensions_and_border(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b)6926 static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
6927 const YV12_BUFFER_CONFIG *b) {
6928 return a->y_height == b->y_height && a->y_width == b->y_width &&
6929 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
6930 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
6931 a->border == b->border &&
6932 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
6933 (b->flags & YV12_FLAG_HIGHBITDEPTH);
6934 }
6935
av1_copy_new_frame_enc(AV1_COMMON * cm,YV12_BUFFER_CONFIG * new_frame,YV12_BUFFER_CONFIG * sd)6936 aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
6937 YV12_BUFFER_CONFIG *new_frame,
6938 YV12_BUFFER_CONFIG *sd) {
6939 const int num_planes = av1_num_planes(cm);
6940 if (!equal_dimensions_and_border(new_frame, sd))
6941 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
6942 "Incorrect buffer dimensions");
6943 else
6944 aom_yv12_copy_frame(new_frame, sd, num_planes);
6945
6946 return cm->error.error_code;
6947 }
6948
av1_set_internal_size(AV1EncoderConfig * const oxcf,ResizePendingParams * resize_pending_params,AOM_SCALING horiz_mode,AOM_SCALING vert_mode)6949 int av1_set_internal_size(AV1EncoderConfig *const oxcf,
6950 ResizePendingParams *resize_pending_params,
6951 AOM_SCALING horiz_mode, AOM_SCALING vert_mode) {
6952 int hr = 0, hs = 0, vr = 0, vs = 0;
6953
6954 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6955
6956 Scale2Ratio(horiz_mode, &hr, &hs);
6957 Scale2Ratio(vert_mode, &vr, &vs);
6958
6959 // always go to the next whole number
6960 resize_pending_params->width = (hs - 1 + oxcf->width * hr) / hs;
6961 resize_pending_params->height = (vs - 1 + oxcf->height * vr) / vs;
6962
6963 return 0;
6964 }
6965
av1_get_quantizer(AV1_COMP * cpi)6966 int av1_get_quantizer(AV1_COMP *cpi) {
6967 return cpi->common.quant_params.base_qindex;
6968 }
6969
av1_convert_sect5obus_to_annexb(uint8_t * buffer,size_t * frame_size)6970 int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
6971 size_t output_size = 0;
6972 size_t total_bytes_read = 0;
6973 size_t remaining_size = *frame_size;
6974 uint8_t *buff_ptr = buffer;
6975
6976 // go through each OBUs
6977 while (total_bytes_read < *frame_size) {
6978 uint8_t saved_obu_header[2];
6979 uint64_t obu_payload_size;
6980 size_t length_of_payload_size;
6981 size_t length_of_obu_size;
6982 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
6983 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
6984
6985 // save the obu header (1 or 2 bytes)
6986 memmove(saved_obu_header, buff_ptr, obu_header_size);
6987 // clear the obu_has_size_field
6988 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
6989
6990 // get the payload_size and length of payload_size
6991 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
6992 &obu_payload_size, &length_of_payload_size) != 0) {
6993 return AOM_CODEC_ERROR;
6994 }
6995 obu_bytes_read += length_of_payload_size;
6996
6997 // calculate the length of size of the obu header plus payload
6998 length_of_obu_size =
6999 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
7000
7001 // move the rest of data to new location
7002 memmove(buff_ptr + length_of_obu_size + obu_header_size,
7003 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
7004 obu_bytes_read += (size_t)obu_payload_size;
7005
7006 // write the new obu size
7007 const uint64_t obu_size = obu_header_size + obu_payload_size;
7008 size_t coded_obu_size;
7009 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
7010 &coded_obu_size) != 0) {
7011 return AOM_CODEC_ERROR;
7012 }
7013
7014 // write the saved (modified) obu_header following obu size
7015 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
7016
7017 total_bytes_read += obu_bytes_read;
7018 remaining_size -= obu_bytes_read;
7019 buff_ptr += length_of_obu_size + obu_size;
7020 output_size += length_of_obu_size + (size_t)obu_size;
7021 }
7022
7023 *frame_size = output_size;
7024 return AOM_CODEC_OK;
7025 }
7026
svc_set_updates_external_ref_frame_config(ExternalFlags * const ext_flags,SVC * const svc)7027 static void svc_set_updates_external_ref_frame_config(
7028 ExternalFlags *const ext_flags, SVC *const svc) {
7029 ext_flags->refresh_frame_flags_pending = 1;
7030 ext_flags->refresh_last_frame = svc->refresh[svc->ref_idx[0]];
7031 ext_flags->refresh_golden_frame = svc->refresh[svc->ref_idx[3]];
7032 ext_flags->refresh_bwd_ref_frame = svc->refresh[svc->ref_idx[4]];
7033 ext_flags->refresh_alt2_ref_frame = svc->refresh[svc->ref_idx[5]];
7034 ext_flags->refresh_alt_ref_frame = svc->refresh[svc->ref_idx[6]];
7035 svc->non_reference_frame = 1;
7036 for (int i = 0; i < REF_FRAMES; i++) {
7037 if (svc->refresh[i] == 1) {
7038 svc->non_reference_frame = 0;
7039 break;
7040 }
7041 }
7042 }
7043
svc_set_references_external_ref_frame_config(AV1_COMP * cpi)7044 static int svc_set_references_external_ref_frame_config(AV1_COMP *cpi) {
7045 // LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3),
7046 // BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6).
7047 int ref = AOM_REFFRAME_ALL;
7048 for (int i = 0; i < INTER_REFS_PER_FRAME; i++) {
7049 if (!cpi->svc.reference[i]) ref ^= (1 << i);
7050 }
7051 return ref;
7052 }
7053
av1_apply_encoding_flags(AV1_COMP * cpi,aom_enc_frame_flags_t flags)7054 void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
7055 // TODO(yunqingwang): For what references to use, external encoding flags
7056 // should be consistent with internal reference frame selection. Need to
7057 // ensure that there is not conflict between the two. In AV1 encoder, the
7058 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
7059 // GOLDEN, BWDREF, ALTREF2.
7060
7061 ExternalFlags *const ext_flags = &cpi->ext_flags;
7062 ext_flags->ref_frame_flags = AOM_REFFRAME_ALL;
7063 if (flags &
7064 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
7065 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
7066 AOM_EFLAG_NO_REF_ARF2)) {
7067 int ref = AOM_REFFRAME_ALL;
7068
7069 if (flags & AOM_EFLAG_NO_REF_LAST) ref ^= AOM_LAST_FLAG;
7070 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
7071 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
7072
7073 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
7074
7075 if (flags & AOM_EFLAG_NO_REF_ARF) {
7076 ref ^= AOM_ALT_FLAG;
7077 ref ^= AOM_BWD_FLAG;
7078 ref ^= AOM_ALT2_FLAG;
7079 } else {
7080 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
7081 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
7082 }
7083
7084 av1_use_as_reference(&ext_flags->ref_frame_flags, ref);
7085 } else {
7086 if (cpi->svc.external_ref_frame_config) {
7087 int ref = svc_set_references_external_ref_frame_config(cpi);
7088 av1_use_as_reference(&ext_flags->ref_frame_flags, ref);
7089 }
7090 }
7091
7092 if (flags &
7093 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
7094 int upd = AOM_REFFRAME_ALL;
7095
7096 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
7097 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
7098
7099 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
7100
7101 if (flags & AOM_EFLAG_NO_UPD_ARF) {
7102 upd ^= AOM_ALT_FLAG;
7103 upd ^= AOM_BWD_FLAG;
7104 upd ^= AOM_ALT2_FLAG;
7105 }
7106
7107 ext_flags->refresh_last_frame = (upd & AOM_LAST_FLAG) != 0;
7108 ext_flags->refresh_golden_frame = (upd & AOM_GOLD_FLAG) != 0;
7109 ext_flags->refresh_alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
7110 ext_flags->refresh_bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
7111 ext_flags->refresh_alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
7112 ext_flags->refresh_frame_flags_pending = 1;
7113 } else {
7114 if (cpi->svc.external_ref_frame_config)
7115 svc_set_updates_external_ref_frame_config(ext_flags, &cpi->svc);
7116 else
7117 ext_flags->refresh_frame_flags_pending = 0;
7118 }
7119
7120 ext_flags->use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
7121 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
7122 ext_flags->use_error_resilient = cpi->oxcf.error_resilient_mode |
7123 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
7124 ext_flags->use_s_frame =
7125 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
7126 ext_flags->use_primary_ref_none =
7127 (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
7128
7129 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
7130 av1_update_entropy(&ext_flags->refresh_frame_context,
7131 &ext_flags->refresh_frame_context_pending, 0);
7132 }
7133 }
7134
av1_get_global_headers(AV1_COMP * cpi)7135 aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
7136 if (!cpi) return NULL;
7137
7138 uint8_t header_buf[512] = { 0 };
7139 const uint32_t sequence_header_size =
7140 av1_write_sequence_header_obu(&cpi->common.seq_params, &header_buf[0]);
7141 assert(sequence_header_size <= sizeof(header_buf));
7142 if (sequence_header_size == 0) return NULL;
7143
7144 const size_t obu_header_size = 1;
7145 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
7146 const size_t payload_offset = obu_header_size + size_field_size;
7147
7148 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
7149 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
7150
7151 if (av1_write_obu_header(&cpi->level_params, OBU_SEQUENCE_HEADER, 0,
7152 &header_buf[0]) != obu_header_size) {
7153 return NULL;
7154 }
7155
7156 size_t coded_size_field_size = 0;
7157 if (aom_uleb_encode(sequence_header_size, size_field_size,
7158 &header_buf[obu_header_size],
7159 &coded_size_field_size) != 0) {
7160 return NULL;
7161 }
7162 assert(coded_size_field_size == size_field_size);
7163
7164 aom_fixed_buf_t *global_headers =
7165 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
7166 if (!global_headers) return NULL;
7167
7168 const size_t global_header_buf_size =
7169 obu_header_size + size_field_size + sequence_header_size;
7170
7171 global_headers->buf = malloc(global_header_buf_size);
7172 if (!global_headers->buf) {
7173 free(global_headers);
7174 return NULL;
7175 }
7176
7177 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
7178 global_headers->sz = global_header_buf_size;
7179 return global_headers;
7180 }
7181