blob: f7864e947955b3b919ba42f5d42503233a88a7ed [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Urvang Joshi8a02d762016-07-28 15:51:12 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Urvang Joshi8a02d762016-07-28 15:51:12 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <limits.h>
Urvang Joshibc82d382019-11-01 17:59:20 -070013#include <float.h>
Yaowu Xuc27fc142016-08-22 16:08:15 -070014#include <math.h>
15#include <stdio.h>
16
Tom Finegan60e653d2018-05-22 11:34:58 -070017#include "config/aom_config.h"
Tom Finegan44702c82018-05-22 13:00:39 -070018#include "config/aom_dsp_rtcd.h"
19#include "config/aom_scale_rtcd.h"
Yaowu Xufa3721d2018-07-30 14:38:49 -070020#include "config/av1_rtcd.h"
21
22#include "aom_dsp/aom_dsp_common.h"
23#include "aom_dsp/aom_filter.h"
24#if CONFIG_DENOISE
25#include "aom_dsp/grain_table.h"
26#include "aom_dsp/noise_util.h"
27#include "aom_dsp/noise_model.h"
28#endif
29#include "aom_dsp/psnr.h"
30#if CONFIG_INTERNAL_STATS
31#include "aom_dsp/ssim.h"
32#endif
33#include "aom_ports/aom_timer.h"
34#include "aom_ports/mem.h"
35#include "aom_ports/system_state.h"
36#include "aom_scale/aom_scale.h"
David Turner1539bb02019-01-24 15:28:13 +000037#if CONFIG_BITSTREAM_DEBUG
Yaowu Xufa3721d2018-07-30 14:38:49 -070038#include "aom_util/debug_util.h"
David Turner1539bb02019-01-24 15:28:13 +000039#endif // CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070040
41#include "av1/common/alloccommon.h"
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010042#include "av1/common/cdef.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070043#include "av1/common/filter.h"
44#include "av1/common/idct.h"
45#include "av1/common/reconinter.h"
46#include "av1/common/reconintra.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070047#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070048#include "av1/common/tile_common.h"
49
Ravi Chaudharyc5e74692018-10-08 16:05:38 +053050#include "av1/encoder/av1_multi_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070051#include "av1/encoder/aq_complexity.h"
52#include "av1/encoder/aq_cyclicrefresh.h"
53#include "av1/encoder/aq_variance.h"
54#include "av1/encoder/bitstream.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070055#include "av1/encoder/context_tree.h"
56#include "av1/encoder/encodeframe.h"
57#include "av1/encoder/encodemv.h"
David Turner056f7cd2019-01-07 17:48:13 +000058#include "av1/encoder/encode_strategy.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070059#include "av1/encoder/encoder.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070060#include "av1/encoder/encodetxb.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070061#include "av1/encoder/ethread.h"
62#include "av1/encoder/firstpass.h"
Yaowu Xufa3721d2018-07-30 14:38:49 -070063#include "av1/encoder/grain_test_vectors.h"
RogerZhoucc5d35d2017-08-07 22:20:15 -070064#include "av1/encoder/hash_motion.h"
chiyotsaic666b1f2019-12-20 10:44:58 -080065#include "av1/encoder/mv_prec.h"
David Turner0fa8c492019-02-06 16:38:13 +000066#include "av1/encoder/pass2_strategy.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070067#include "av1/encoder/picklpf.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070068#include "av1/encoder/pickrst.h"
Debargha Mukherjee7166f222017-09-05 21:32:42 -070069#include "av1/encoder/random.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070070#include "av1/encoder/ratectrl.h"
71#include "av1/encoder/rd.h"
Debargha Mukherjeedf713102018-10-02 12:33:32 -070072#include "av1/encoder/rdopt.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070073#include "av1/encoder/segmentation.h"
74#include "av1/encoder/speed_features.h"
Debargha Mukherjee347c64d2019-05-08 13:53:46 -070075#include "av1/encoder/tpl_model.h"
Yue Chen7cae98f2018-08-24 10:43:16 -070076#include "av1/encoder/reconinter_enc.h"
kyslov7b9d0d62018-12-21 11:12:26 -080077#include "av1/encoder/var_based_part.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070078
sdenge63f9fa2019-12-13 09:29:55 -080079#if CONFIG_TUNE_VMAF
80#include "av1/encoder/tune_vmaf.h"
81#endif
82
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000083#define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000084
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080085#if CONFIG_ENTROPY_STATS
86FRAME_COUNTS aggregate_fc;
87#endif // CONFIG_ENTROPY_STATS
88
Yaowu Xuc27fc142016-08-22 16:08:15 -070089#define AM_SEGMENT_ID_INACTIVE 7
90#define AM_SEGMENT_ID_ACTIVE 0
91
Yaowu Xuc27fc142016-08-22 16:08:15 -070092// #define OUTPUT_YUV_REC
Yaowu Xuc27fc142016-08-22 16:08:15 -070093#ifdef OUTPUT_YUV_SKINMAP
94FILE *yuv_skinmap_file = NULL;
95#endif
96#ifdef OUTPUT_YUV_REC
97FILE *yuv_rec_file;
98#define FILE_NAME_LEN 100
99#endif
100
Yunqing Wangdb70bf42019-08-19 09:28:11 -0700101const int default_tx_type_probs[FRAME_UPDATE_TYPES][TX_SIZES_ALL][TX_TYPES] = {
102 { { 221, 189, 214, 292, 0, 0, 0, 0, 0, 2, 38, 68, 0, 0, 0, 0 },
103 { 262, 203, 216, 239, 0, 0, 0, 0, 0, 1, 37, 66, 0, 0, 0, 0 },
104 { 315, 231, 239, 226, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0 },
105 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
106 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
107 { 222, 188, 214, 287, 0, 0, 0, 0, 0, 2, 50, 61, 0, 0, 0, 0 },
108 { 256, 182, 205, 282, 0, 0, 0, 0, 0, 2, 21, 76, 0, 0, 0, 0 },
109 { 281, 214, 217, 222, 0, 0, 0, 0, 0, 1, 48, 41, 0, 0, 0, 0 },
110 { 263, 194, 225, 225, 0, 0, 0, 0, 0, 2, 15, 100, 0, 0, 0, 0 },
111 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
112 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
113 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
114 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
115 { 170, 192, 242, 293, 0, 0, 0, 0, 0, 1, 68, 58, 0, 0, 0, 0 },
116 { 199, 210, 213, 291, 0, 0, 0, 0, 0, 1, 14, 96, 0, 0, 0, 0 },
117 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
118 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
119 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
120 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
121 { { 106, 69, 107, 278, 9, 15, 20, 45, 49, 23, 23, 88, 36, 74, 25, 57 },
122 { 105, 72, 81, 98, 45, 49, 47, 50, 56, 72, 30, 81, 33, 95, 27, 83 },
123 { 211, 105, 109, 120, 57, 62, 43, 49, 52, 58, 42, 116, 0, 0, 0, 0 },
124 { 1008, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0 },
125 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
126 { 131, 57, 98, 172, 19, 40, 37, 64, 69, 22, 41, 52, 51, 77, 35, 59 },
127 { 176, 83, 93, 202, 22, 24, 28, 47, 50, 16, 12, 93, 26, 76, 17, 59 },
128 { 136, 72, 89, 95, 46, 59, 47, 56, 61, 68, 35, 51, 32, 82, 26, 69 },
129 { 122, 80, 87, 105, 49, 47, 46, 46, 57, 52, 13, 90, 19, 103, 15, 93 },
130 { 1009, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0 },
131 { 1011, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0 },
132 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
133 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
134 { 202, 20, 84, 114, 14, 60, 41, 79, 99, 21, 41, 15, 50, 84, 34, 66 },
135 { 196, 44, 23, 72, 30, 22, 28, 57, 67, 13, 4, 165, 15, 148, 9, 131 },
136 { 882, 0, 0, 0, 0, 0, 0, 0, 0, 142, 0, 0, 0, 0, 0, 0 },
137 { 840, 0, 0, 0, 0, 0, 0, 0, 0, 184, 0, 0, 0, 0, 0, 0 },
138 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
139 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
140 { { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
141 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
142 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
143 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
144 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
145 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
146 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
147 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
148 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
149 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
150 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
151 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
152 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
153 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
154 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
155 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
156 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
157 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
158 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 } },
159 { { 213, 110, 141, 269, 12, 16, 15, 19, 21, 11, 38, 68, 22, 29, 16, 24 },
160 { 216, 119, 128, 143, 38, 41, 26, 30, 31, 30, 42, 70, 23, 36, 19, 32 },
161 { 367, 149, 154, 154, 38, 35, 17, 21, 21, 10, 22, 36, 0, 0, 0, 0 },
162 { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
163 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
164 { 219, 96, 127, 191, 21, 40, 25, 32, 34, 18, 45, 45, 33, 39, 26, 33 },
165 { 296, 99, 122, 198, 23, 21, 19, 24, 25, 13, 20, 64, 23, 32, 18, 27 },
166 { 275, 128, 142, 143, 35, 48, 23, 30, 29, 18, 42, 36, 18, 23, 14, 20 },
167 { 239, 132, 166, 175, 36, 27, 19, 21, 24, 14, 13, 85, 9, 31, 8, 25 },
168 { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
169 { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
170 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
171 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
172 { 309, 25, 79, 59, 25, 80, 34, 53, 61, 25, 49, 23, 43, 64, 36, 59 },
173 { 270, 57, 40, 54, 50, 42, 41, 53, 56, 28, 17, 81, 45, 86, 34, 70 },
174 { 1005, 0, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 0, 0, 0 },
175 { 992, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0 },
176 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
177 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
178 { { 133, 63, 55, 83, 57, 87, 58, 72, 68, 16, 24, 35, 29, 105, 25, 114 },
179 { 131, 75, 74, 60, 71, 77, 65, 66, 73, 33, 21, 79, 20, 83, 18, 78 },
180 { 276, 95, 82, 58, 86, 93, 63, 60, 64, 17, 38, 92, 0, 0, 0, 0 },
181 { 1006, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0 },
182 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
183 { 147, 49, 75, 78, 50, 97, 60, 67, 76, 17, 42, 35, 31, 93, 27, 80 },
184 { 157, 49, 58, 75, 61, 52, 56, 67, 69, 12, 15, 79, 24, 119, 11, 120 },
185 { 178, 69, 83, 77, 69, 85, 72, 77, 77, 20, 35, 40, 25, 48, 23, 46 },
186 { 174, 55, 64, 57, 73, 68, 62, 61, 75, 15, 12, 90, 17, 99, 16, 86 },
187 { 1008, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0 },
188 { 1018, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0 },
189 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
190 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
191 { 266, 31, 63, 64, 21, 52, 39, 54, 63, 30, 52, 31, 48, 89, 46, 75 },
192 { 272, 26, 32, 44, 29, 31, 32, 53, 51, 13, 13, 88, 22, 153, 16, 149 },
193 { 923, 0, 0, 0, 0, 0, 0, 0, 0, 101, 0, 0, 0, 0, 0, 0 },
194 { 969, 0, 0, 0, 0, 0, 0, 0, 0, 55, 0, 0, 0, 0, 0, 0 },
195 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
196 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
197 { { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
198 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
199 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
200 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
201 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
202 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
203 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
204 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
205 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
206 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
207 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
208 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
209 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
210 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
211 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
212 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
213 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
214 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
215 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 } },
216 { { 158, 92, 125, 298, 12, 15, 20, 29, 31, 12, 29, 67, 34, 44, 23, 35 },
217 { 147, 94, 103, 123, 45, 48, 38, 41, 46, 48, 37, 78, 33, 63, 27, 53 },
218 { 268, 126, 125, 136, 54, 53, 31, 38, 38, 33, 35, 87, 0, 0, 0, 0 },
219 { 1018, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0 },
220 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
221 { 159, 72, 103, 194, 20, 35, 37, 50, 56, 21, 39, 40, 51, 61, 38, 48 },
222 { 259, 86, 95, 188, 32, 20, 25, 34, 37, 13, 12, 85, 25, 53, 17, 43 },
223 { 189, 99, 113, 123, 45, 59, 37, 46, 48, 44, 39, 41, 31, 47, 26, 37 },
224 { 175, 110, 113, 128, 58, 38, 33, 33, 43, 29, 13, 100, 14, 68, 12, 57 },
225 { 1017, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0 },
226 { 1019, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0 },
227 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
228 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
229 { 208, 22, 84, 101, 21, 59, 44, 70, 90, 25, 59, 13, 64, 67, 49, 48 },
230 { 277, 52, 32, 63, 43, 26, 33, 48, 54, 11, 6, 130, 18, 119, 11, 101 },
231 { 963, 0, 0, 0, 0, 0, 0, 0, 0, 61, 0, 0, 0, 0, 0, 0 },
232 { 979, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 0, 0, 0 },
233 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
234 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
235};
236
Yunqing Wang5f74dc22019-10-29 10:35:20 -0700237const int default_obmc_probs[FRAME_UPDATE_TYPES][BLOCK_SIZES_ALL] = {
238 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
239 { 0, 0, 0, 106, 90, 90, 97, 67, 59, 70, 28,
240 30, 38, 16, 16, 16, 0, 0, 44, 50, 26, 25 },
241 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
242 { 0, 0, 0, 98, 93, 97, 68, 82, 85, 33, 30,
243 33, 16, 16, 16, 16, 0, 0, 43, 37, 26, 16 },
244 { 0, 0, 0, 91, 80, 76, 78, 55, 49, 24, 16,
245 16, 16, 16, 16, 16, 0, 0, 29, 45, 16, 38 },
246 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
247 { 0, 0, 0, 103, 89, 89, 89, 62, 63, 76, 34,
248 35, 32, 19, 16, 16, 0, 0, 49, 55, 29, 19 }
249};
250
Yunqing Wanga09a46b2019-12-09 13:16:24 -0800251const int default_warped_probs[FRAME_UPDATE_TYPES] = { 64, 64, 64, 64,
252 64, 64, 64 };
253
Yunqing Wang2348c032020-01-09 18:05:49 -0800254// TODO(yunqing): the default probs can be trained later from better
255// performance.
256const int default_switchable_interp_probs[FRAME_UPDATE_TYPES]
257 [SWITCHABLE_FILTER_CONTEXTS]
258 [SWITCHABLE_FILTERS] = {
259 { { 512, 512, 512 },
260 { 512, 512, 512 },
261 { 512, 512, 512 },
262 { 512, 512, 512 },
263 { 512, 512, 512 },
264 { 512, 512, 512 },
265 { 512, 512, 512 },
266 { 512, 512, 512 },
267 { 512, 512, 512 },
268 { 512, 512, 512 },
269 { 512, 512, 512 },
270 { 512, 512, 512 },
271 { 512, 512, 512 },
272 { 512, 512, 512 },
273 { 512, 512, 512 },
274 { 512, 512, 512 } },
275 { { 512, 512, 512 },
276 { 512, 512, 512 },
277 { 512, 512, 512 },
278 { 512, 512, 512 },
279 { 512, 512, 512 },
280 { 512, 512, 512 },
281 { 512, 512, 512 },
282 { 512, 512, 512 },
283 { 512, 512, 512 },
284 { 512, 512, 512 },
285 { 512, 512, 512 },
286 { 512, 512, 512 },
287 { 512, 512, 512 },
288 { 512, 512, 512 },
289 { 512, 512, 512 },
290 { 512, 512, 512 } },
291 { { 512, 512, 512 },
292 { 512, 512, 512 },
293 { 512, 512, 512 },
294 { 512, 512, 512 },
295 { 512, 512, 512 },
296 { 512, 512, 512 },
297 { 512, 512, 512 },
298 { 512, 512, 512 },
299 { 512, 512, 512 },
300 { 512, 512, 512 },
301 { 512, 512, 512 },
302 { 512, 512, 512 },
303 { 512, 512, 512 },
304 { 512, 512, 512 },
305 { 512, 512, 512 },
306 { 512, 512, 512 } },
307 { { 512, 512, 512 },
308 { 512, 512, 512 },
309 { 512, 512, 512 },
310 { 512, 512, 512 },
311 { 512, 512, 512 },
312 { 512, 512, 512 },
313 { 512, 512, 512 },
314 { 512, 512, 512 },
315 { 512, 512, 512 },
316 { 512, 512, 512 },
317 { 512, 512, 512 },
318 { 512, 512, 512 },
319 { 512, 512, 512 },
320 { 512, 512, 512 },
321 { 512, 512, 512 },
322 { 512, 512, 512 } },
323 { { 512, 512, 512 },
324 { 512, 512, 512 },
325 { 512, 512, 512 },
326 { 512, 512, 512 },
327 { 512, 512, 512 },
328 { 512, 512, 512 },
329 { 512, 512, 512 },
330 { 512, 512, 512 },
331 { 512, 512, 512 },
332 { 512, 512, 512 },
333 { 512, 512, 512 },
334 { 512, 512, 512 },
335 { 512, 512, 512 },
336 { 512, 512, 512 },
337 { 512, 512, 512 },
338 { 512, 512, 512 } },
339 { { 512, 512, 512 },
340 { 512, 512, 512 },
341 { 512, 512, 512 },
342 { 512, 512, 512 },
343 { 512, 512, 512 },
344 { 512, 512, 512 },
345 { 512, 512, 512 },
346 { 512, 512, 512 },
347 { 512, 512, 512 },
348 { 512, 512, 512 },
349 { 512, 512, 512 },
350 { 512, 512, 512 },
351 { 512, 512, 512 },
352 { 512, 512, 512 },
353 { 512, 512, 512 },
354 { 512, 512, 512 } },
355 { { 512, 512, 512 },
356 { 512, 512, 512 },
357 { 512, 512, 512 },
358 { 512, 512, 512 },
359 { 512, 512, 512 },
360 { 512, 512, 512 },
361 { 512, 512, 512 },
362 { 512, 512, 512 },
363 { 512, 512, 512 },
364 { 512, 512, 512 },
365 { 512, 512, 512 },
366 { 512, 512, 512 },
367 { 512, 512, 512 },
368 { 512, 512, 512 },
369 { 512, 512, 512 },
370 { 512, 512, 512 } }
371 };
372
Yaowu Xuf883b422016-08-30 14:01:10 -0700373static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700374 switch (mode) {
375 case NORMAL:
376 *hr = 1;
377 *hs = 1;
378 break;
379 case FOURFIVE:
380 *hr = 4;
381 *hs = 5;
382 break;
383 case THREEFIVE:
384 *hr = 3;
385 *hs = 5;
386 break;
387 case ONETWO:
388 *hr = 1;
389 *hs = 2;
390 break;
391 default:
392 *hr = 1;
393 *hs = 1;
394 assert(0);
395 break;
396 }
397}
398
399// Mark all inactive blocks as active. Other segmentation features may be set
400// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700401static void suppress_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700402 unsigned char *const seg_map = cpi->segmentation_map;
403 int i;
404 if (cpi->active_map.enabled || cpi->active_map.update)
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700405 for (i = 0;
406 i < cpi->common.mi_params.mi_rows * cpi->common.mi_params.mi_cols; ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700407 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
408 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
409}
410
Yaowu Xuf883b422016-08-30 14:01:10 -0700411static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700412 struct segmentation *const seg = &cpi->common.seg;
413 unsigned char *const seg_map = cpi->segmentation_map;
414 const unsigned char *const active_map = cpi->active_map.map;
415 int i;
416
417 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
418
419 if (frame_is_intra_only(&cpi->common)) {
420 cpi->active_map.enabled = 0;
421 cpi->active_map.update = 1;
422 }
423
424 if (cpi->active_map.update) {
425 if (cpi->active_map.enabled) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700426 for (i = 0;
427 i < cpi->common.mi_params.mi_rows * cpi->common.mi_params.mi_cols;
428 ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700429 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700430 av1_enable_segmentation(seg);
431 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700432 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
433 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
434 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
435 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
436
437 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
438 -MAX_LOOP_FILTER);
439 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
440 -MAX_LOOP_FILTER);
441 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
442 -MAX_LOOP_FILTER);
443 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
444 -MAX_LOOP_FILTER);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700445 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700446 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700447 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
448 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
449 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
450 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700451 if (seg->enabled) {
452 seg->update_data = 1;
453 seg->update_map = 1;
454 }
455 }
456 cpi->active_map.update = 0;
457 }
458}
459
Yaowu Xuf883b422016-08-30 14:01:10 -0700460int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
461 int cols) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700462 const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
463 if (rows == mi_params->mb_rows && cols == mi_params->mb_cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700464 unsigned char *const active_map_8x8 = cpi->active_map.map;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700465 const int mi_rows = mi_params->mi_rows;
466 const int mi_cols = mi_params->mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700467 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
468 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700469 cpi->active_map.update = 1;
470 if (new_map_16x16) {
471 int r, c;
472 for (r = 0; r < mi_rows; ++r) {
473 for (c = 0; c < mi_cols; ++c) {
474 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700475 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700476 ? AM_SEGMENT_ID_ACTIVE
477 : AM_SEGMENT_ID_INACTIVE;
478 }
479 }
480 cpi->active_map.enabled = 1;
481 } else {
482 cpi->active_map.enabled = 0;
483 }
484 return 0;
485 } else {
486 return -1;
487 }
488}
489
Yaowu Xuf883b422016-08-30 14:01:10 -0700490int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
491 int cols) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700492 const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
493 if (rows == mi_params->mb_rows && cols == mi_params->mb_cols &&
Yaowu Xuc27fc142016-08-22 16:08:15 -0700494 new_map_16x16) {
495 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700496 const int mi_rows = mi_params->mi_rows;
497 const int mi_cols = mi_params->mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700498 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
499 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
500
Yaowu Xuc27fc142016-08-22 16:08:15 -0700501 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
502 if (cpi->active_map.enabled) {
503 int r, c;
504 for (r = 0; r < mi_rows; ++r) {
505 for (c = 0; c < mi_cols; ++c) {
506 // Cyclic refresh segments are considered active despite not having
507 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700508 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700509 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
510 }
511 }
512 }
513 return 0;
514 } else {
515 return -1;
516 }
517}
518
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800519// Compute the horizontal frequency components' energy in a frame
520// by calculuating the 16x4 Horizontal DCT. This is to be used to
521// decide the superresolution parameters.
Yaowu Xubedbf4f2019-05-01 17:54:36 -0700522static void analyze_hor_freq(const AV1_COMP *cpi, double *energy) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800523 uint64_t freq_energy[16] = { 0 };
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800524 const YV12_BUFFER_CONFIG *buf = cpi->source;
525 const int bd = cpi->td.mb.e_mbd.bd;
526 const int width = buf->y_crop_width;
527 const int height = buf->y_crop_height;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800528 DECLARE_ALIGNED(16, int32_t, coeff[16 * 4]);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800529 int n = 0;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800530 memset(freq_energy, 0, sizeof(freq_energy));
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800531 if (buf->flags & YV12_FLAG_HIGHBITDEPTH) {
532 const int16_t *src16 = (const int16_t *)CONVERT_TO_SHORTPTR(buf->y_buffer);
533 for (int i = 0; i < height - 4; i += 4) {
534 for (int j = 0; j < width - 16; j += 16) {
535 av1_fwd_txfm2d_16x4(src16 + i * buf->y_stride + j, coeff, buf->y_stride,
536 H_DCT, bd);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800537 for (int k = 1; k < 16; ++k) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800538 const uint64_t this_energy =
539 ((int64_t)coeff[k] * coeff[k]) +
540 ((int64_t)coeff[k + 16] * coeff[k + 16]) +
541 ((int64_t)coeff[k + 32] * coeff[k + 32]) +
542 ((int64_t)coeff[k + 48] * coeff[k + 48]);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800543 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2 + 2 * (bd - 8));
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800544 }
545 n++;
546 }
547 }
548 } else {
Debargha Mukherjeeac28c722018-11-14 22:09:46 -0800549 assert(bd == 8);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800550 DECLARE_ALIGNED(16, int16_t, src16[16 * 4]);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800551 for (int i = 0; i < height - 4; i += 4) {
552 for (int j = 0; j < width - 16; j += 16) {
553 for (int ii = 0; ii < 4; ++ii)
554 for (int jj = 0; jj < 16; ++jj)
555 src16[ii * 16 + jj] =
556 buf->y_buffer[(i + ii) * buf->y_stride + (j + jj)];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800557 av1_fwd_txfm2d_16x4(src16, coeff, 16, H_DCT, bd);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800558 for (int k = 1; k < 16; ++k) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800559 const uint64_t this_energy =
560 ((int64_t)coeff[k] * coeff[k]) +
561 ((int64_t)coeff[k + 16] * coeff[k + 16]) +
562 ((int64_t)coeff[k + 32] * coeff[k + 32]) +
563 ((int64_t)coeff[k + 48] * coeff[k + 48]);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800564 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800565 }
566 n++;
567 }
568 }
569 }
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800570 if (n) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800571 for (int k = 1; k < 16; ++k) energy[k] = (double)freq_energy[k] / n;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800572 // Convert to cumulative energy
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800573 for (int k = 14; k > 0; --k) energy[k] += energy[k + 1];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800574 } else {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800575 for (int k = 1; k < 16; ++k) energy[k] = 1e+20;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800576 }
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800577}
578
Yaowu Xuf883b422016-08-30 14:01:10 -0700579static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Urvang Joshie4530f82018-01-09 11:43:37 -0800580 const AV1_COMMON *const cm = &cpi->common;
James Zernbf3ca362019-10-21 11:33:44 -0700581
Yaowu Xuf883b422016-08-30 14:01:10 -0700582 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700583 return BLOCK_64X64;
Ryan Leic8b6dd62019-10-23 20:01:26 -0700584 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
585 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700586
Yaowu Xuf883b422016-08-30 14:01:10 -0700587 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700588
Ryan Leic8b6dd62019-10-23 20:01:26 -0700589 // TODO(any): Possibly could improve this with a heuristic.
Urvang Joshiaab74432018-06-01 12:06:22 -0700590 // When superres / resize is on, 'cm->width / height' can change between
Hui Su3e3b9342019-04-12 18:27:28 +0000591 // calls, so we don't apply this heuristic there.
592 // Things break if superblock size changes between the first pass and second
593 // pass encoding, which is why this heuristic is not configured as a
594 // speed-feature.
Urvang Joshiaab74432018-06-01 12:06:22 -0700595 if (cpi->oxcf.superres_mode == SUPERRES_NONE &&
Hui Su3e3b9342019-04-12 18:27:28 +0000596 cpi->oxcf.resize_mode == RESIZE_NONE && cpi->oxcf.speed >= 1) {
597 return AOMMIN(cm->width, cm->height) > 480 ? BLOCK_128X128 : BLOCK_64X64;
Urvang Joshie4530f82018-01-09 11:43:37 -0800598 }
599
Yaowu Xuc27fc142016-08-22 16:08:15 -0700600 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700601}
602
Yaowu Xuf883b422016-08-30 14:01:10 -0700603static void setup_frame(AV1_COMP *cpi) {
604 AV1_COMMON *const cm = &cpi->common;
Johannb0ef6ff2018-02-08 14:32:21 -0800605 // Set up entropy context depending on frame type. The decoder mandates
606 // the use of the default context, index 0, for keyframes and inter
607 // frames where the error_resilient_mode or intra_only flag is set. For
608 // other inter-frames the encoder currently uses only two contexts;
609 // context 1 for ALTREF frames and context 0 for the others.
Soo-Chul Han85e8c792018-01-21 01:58:15 -0500610
Urvang Joshib6409e92020-03-23 11:23:27 -0700611 if (frame_is_intra_only(cm) || cm->features.error_resilient_mode ||
David Turnera7f133c2019-01-22 14:47:16 +0000612 cpi->ext_use_primary_ref_none) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700613 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700614 }
615
Hui Sueb4b7de2019-04-03 11:00:18 -0700616 if ((cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) ||
617 frame_is_sframe(cm)) {
618 if (!cpi->seq_params_locked) {
619 set_sb_size(&cm->seq_params, select_sb_size(cpi));
620 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621 } else {
David Turnera21966b2018-12-05 14:48:49 +0000622 const RefCntBuffer *const primary_ref_buf = get_primary_ref_frame_buf(cm);
623 if (primary_ref_buf == NULL) {
David Barkercc615a82018-03-19 14:38:51 +0000624 av1_setup_past_independence(cm);
625 cm->seg.update_map = 1;
626 cm->seg.update_data = 1;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700627 } else {
David Turnera21966b2018-12-05 14:48:49 +0000628 *cm->fc = primary_ref_buf->frame_context;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700629 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700630 }
631
David Turnerbc0993e2019-02-15 14:42:23 +0000632 av1_zero(cm->cur_frame->interp_filter_selected);
David Turnera21966b2018-12-05 14:48:49 +0000633 cm->prev_frame = get_primary_ref_frame_buf(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700634 cpi->vaq_refresh = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700635}
636
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700637static void enc_set_mb_mi(CommonModeInfoParams *mi_params, int width,
638 int height) {
chiyotsaia7091f12019-08-09 16:48:27 -0700639 // Ensure that the decoded width and height are both multiples of
640 // 8 luma pixels (note: this may only be a multiple of 4 chroma pixels if
641 // subsampling is used).
642 // This simplifies the implementation of various experiments,
643 // eg. cdef, which operates on units of 8x8 luma pixels.
644 const int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
645 const int aligned_height = ALIGN_POWER_OF_TWO(height, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700646
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700647 mi_params->mi_cols = aligned_width >> MI_SIZE_LOG2;
648 mi_params->mi_rows = aligned_height >> MI_SIZE_LOG2;
649 mi_params->mi_stride = calc_mi_size(mi_params->mi_cols);
chiyotsaia7091f12019-08-09 16:48:27 -0700650
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700651 mi_params->mb_cols = (mi_params->mi_cols + 2) >> 2;
652 mi_params->mb_rows = (mi_params->mi_rows + 2) >> 2;
653 mi_params->MBs = mi_params->mb_rows * mi_params->mb_cols;
chiyotsaia7091f12019-08-09 16:48:27 -0700654
655 const int is_4k_or_larger = AOMMIN(width, height) >= 2160;
656
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700657 mi_params->mi_alloc_bsize = is_4k_or_larger ? BLOCK_8X8 : BLOCK_4X4;
658 const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700659 mi_params->mi_alloc_stride =
660 (mi_params->mi_stride + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
chiyotsaia7091f12019-08-09 16:48:27 -0700661
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700662 assert(mi_size_wide[mi_params->mi_alloc_bsize] ==
663 mi_size_high[mi_params->mi_alloc_bsize]);
chiyotsaia7091f12019-08-09 16:48:27 -0700664
665#if CONFIG_LPF_MASK
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700666 av1_alloc_loop_filter_mask(mi_params);
chiyotsaia7091f12019-08-09 16:48:27 -0700667#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700668}
669
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700670static void enc_setup_mi(CommonModeInfoParams *mi_params) {
671 const int mi_grid_size =
672 mi_params->mi_stride * calc_mi_size(mi_params->mi_rows);
Urvang Joshid2998cd2020-03-26 02:27:48 -0700673 memset(mi_params->mi_alloc, 0,
674 mi_params->mi_alloc_size * sizeof(*mi_params->mi_alloc));
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700675 memset(mi_params->mi_grid_base, 0,
676 mi_grid_size * sizeof(*mi_params->mi_grid_base));
677 memset(mi_params->tx_type_map, 0,
678 mi_grid_size * sizeof(*mi_params->tx_type_map));
chiyotsaia7091f12019-08-09 16:48:27 -0700679}
680
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700681static void enc_free_mi(CommonModeInfoParams *mi_params) {
Urvang Joshid2998cd2020-03-26 02:27:48 -0700682 aom_free(mi_params->mi_alloc);
683 mi_params->mi_alloc = NULL;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700684 aom_free(mi_params->mi_grid_base);
685 mi_params->mi_grid_base = NULL;
686 mi_params->mi_alloc_size = 0;
687 aom_free(mi_params->tx_type_map);
688 mi_params->tx_type_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689}
690
Yaowu Xuf883b422016-08-30 14:01:10 -0700691void av1_initialize_enc(void) {
Wan-Teh Chang3cac4542018-06-29 10:21:39 -0700692 av1_rtcd();
693 aom_dsp_rtcd();
694 aom_scale_rtcd();
695 av1_init_intra_predictors();
696 av1_init_me_luts();
697 av1_rc_init_minq_luts();
698 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700699}
700
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700701static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
Remya0cce44c2019-08-16 11:57:24 +0530702 if (cpi->mbmi_ext_frame_base) {
703 aom_free(cpi->mbmi_ext_frame_base);
704 cpi->mbmi_ext_frame_base = NULL;
Urvang Joshif10ee372020-03-26 02:22:43 -0700705 cpi->mbmi_ext_alloc_size = 0;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700706 }
707}
708
709static void alloc_context_buffers_ext(AV1_COMP *cpi) {
710 AV1_COMMON *cm = &cpi->common;
Urvang Joshif10ee372020-03-26 02:22:43 -0700711 const CommonModeInfoParams *const mi_params = &cm->mi_params;
chiyotsai426c0662019-08-05 16:15:11 -0700712
Urvang Joshif10ee372020-03-26 02:22:43 -0700713 const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
714 const int mi_alloc_rows =
715 (mi_params->mi_rows + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
716 const int mi_alloc_cols =
717 (mi_params->mi_cols + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
718 const int new_ext_mi_size = mi_alloc_rows * mi_alloc_cols;
719
720 if (new_ext_mi_size > cpi->mbmi_ext_alloc_size) {
chiyotsaia7091f12019-08-09 16:48:27 -0700721 dealloc_context_buffers_ext(cpi);
Remya0cce44c2019-08-16 11:57:24 +0530722 CHECK_MEM_ERROR(
723 cm, cpi->mbmi_ext_frame_base,
724 aom_calloc(new_ext_mi_size, sizeof(*cpi->mbmi_ext_frame_base)));
Urvang Joshif10ee372020-03-26 02:22:43 -0700725 cpi->mbmi_ext_alloc_size = new_ext_mi_size;
chiyotsaia7091f12019-08-09 16:48:27 -0700726 }
Urvang Joshif10ee372020-03-26 02:22:43 -0700727 // The stride needs to be updated regardless of whether new allocation
728 // happened or not.
729 cpi->mbmi_ext_stride = mi_alloc_cols;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700730}
731
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800732static void reset_film_grain_chroma_params(aom_film_grain_t *pars) {
733 pars->num_cr_points = 0;
734 pars->cr_mult = 0;
735 pars->cr_luma_mult = 0;
736 memset(pars->scaling_points_cr, 0, sizeof(pars->scaling_points_cr));
737 memset(pars->ar_coeffs_cr, 0, sizeof(pars->ar_coeffs_cr));
738 pars->num_cb_points = 0;
739 pars->cb_mult = 0;
740 pars->cb_luma_mult = 0;
Yaowu Xufda7dcb2019-01-16 13:04:33 -0800741 pars->chroma_scaling_from_luma = 0;
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800742 memset(pars->scaling_points_cb, 0, sizeof(pars->scaling_points_cb));
743 memset(pars->ar_coeffs_cb, 0, sizeof(pars->ar_coeffs_cb));
744}
745
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800746static void update_film_grain_parameters(struct AV1_COMP *cpi,
747 const AV1EncoderConfig *oxcf) {
748 AV1_COMMON *const cm = &cpi->common;
749 cpi->oxcf = *oxcf;
750
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700751 if (cpi->film_grain_table) {
752 aom_film_grain_table_free(cpi->film_grain_table);
753 aom_free(cpi->film_grain_table);
754 cpi->film_grain_table = NULL;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700755 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700756
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800757 if (oxcf->film_grain_test_vector) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700758 cm->seq_params.film_grain_params_present = 1;
David Turnerd2a592e2018-11-16 14:59:31 +0000759 if (cm->current_frame.frame_type == KEY_FRAME) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800760 memcpy(&cm->film_grain_params,
761 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
762 sizeof(cm->film_grain_params));
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800763 if (oxcf->monochrome)
764 reset_film_grain_chroma_params(&cm->film_grain_params);
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700765 cm->film_grain_params.bit_depth = cm->seq_params.bit_depth;
766 if (cm->seq_params.color_range == AOM_CR_FULL_RANGE) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800767 cm->film_grain_params.clip_to_restricted_range = 0;
768 }
769 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700770 } else if (oxcf->film_grain_table_filename) {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800771 cm->seq_params.film_grain_params_present = 1;
772
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700773 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
774 memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700775
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700776 aom_film_grain_table_read(cpi->film_grain_table,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700777 oxcf->film_grain_table_filename, &cm->error);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800778 } else {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800779#if CONFIG_DENOISE
780 cm->seq_params.film_grain_params_present = (cpi->oxcf.noise_level > 0);
781#else
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700782 cm->seq_params.film_grain_params_present = 0;
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800783#endif
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800784 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
785 }
786}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800787
Yaowu Xuf883b422016-08-30 14:01:10 -0700788static void dealloc_compressor_data(AV1_COMP *cpi) {
789 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000790 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700792 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793
Yaowu Xuf883b422016-08-30 14:01:10 -0700794 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700795 cpi->tile_data = NULL;
796
797 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700798 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700799 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800
Yaowu Xuf883b422016-08-30 14:01:10 -0700801 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802 cpi->cyclic_refresh = NULL;
803
Yaowu Xuf883b422016-08-30 14:01:10 -0700804 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700805 cpi->active_map.map = NULL;
806
sdengc23c7f12019-06-11 16:56:50 -0700807 aom_free(cpi->ssim_rdmult_scaling_factors);
808 cpi->ssim_rdmult_scaling_factors = NULL;
809
sdengf46a1062019-08-04 18:43:50 -0700810 aom_free(cpi->tpl_rdmult_scaling_factors);
811 cpi->tpl_rdmult_scaling_factors = NULL;
812
813 aom_free(cpi->tpl_sb_rdmult_scaling_factors);
814 cpi->tpl_sb_rdmult_scaling_factors = NULL;
815
sdeng01959162019-12-20 10:46:24 -0800816#if CONFIG_TUNE_VMAF
817 aom_free(cpi->vmaf_rdmult_scaling_factors);
818 cpi->vmaf_rdmult_scaling_factors = NULL;
819#endif
820
Jingning Hand064cf02017-06-01 10:00:39 -0700821 aom_free(cpi->td.mb.above_pred_buf);
822 cpi->td.mb.above_pred_buf = NULL;
823
824 aom_free(cpi->td.mb.left_pred_buf);
825 cpi->td.mb.left_pred_buf = NULL;
826
827 aom_free(cpi->td.mb.wsrc_buf);
828 cpi->td.mb.wsrc_buf = NULL;
829
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530830 aom_free(cpi->td.mb.inter_modes_info);
831 cpi->td.mb.inter_modes_info = NULL;
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530832
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530833 for (int i = 0; i < 2; i++)
834 for (int j = 0; j < 2; j++) {
835 aom_free(cpi->td.mb.hash_value_buffer[i][j]);
836 cpi->td.mb.hash_value_buffer[i][j] = NULL;
837 }
Jingning Hand064cf02017-06-01 10:00:39 -0700838 aom_free(cpi->td.mb.mask_buf);
839 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700840
Jingning Han6cc1fd32017-10-13 09:05:36 -0700841 aom_free(cm->tpl_mvs);
842 cm->tpl_mvs = NULL;
Jingning Han6cc1fd32017-10-13 09:05:36 -0700843
Remya0cce44c2019-08-16 11:57:24 +0530844 aom_free(cpi->td.mb.mbmi_ext);
845 cpi->td.mb.mbmi_ext = NULL;
846
Fyodor Kyslov166648a2020-03-18 16:15:27 -0700847 if (cpi->td.vt64x64) {
848 aom_free(cpi->td.vt64x64);
849 cpi->td.vt64x64 = NULL;
850 }
851
Yaowu Xuf883b422016-08-30 14:01:10 -0700852 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700853 av1_free_txb_buf(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700854 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700855
Yaowu Xuf883b422016-08-30 14:01:10 -0700856 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuf883b422016-08-30 14:01:10 -0700857 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800858 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuf883b422016-08-30 14:01:10 -0700859 aom_free_frame_buffer(&cpi->scaled_source);
860 aom_free_frame_buffer(&cpi->scaled_last_source);
861 aom_free_frame_buffer(&cpi->alt_ref_buffer);
862 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700863
Yaowu Xuf883b422016-08-30 14:01:10 -0700864 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700865 cpi->tile_tok[0][0] = 0;
866
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530867 aom_free(cpi->tplist[0][0]);
868 cpi->tplist[0][0] = NULL;
869
Mufaddal Chakera7b5265f2020-03-27 10:32:34 +0530870 av1_free_pc_tree(cpi, &cpi->td, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700871
hui sud9a812b2017-07-06 14:34:37 -0700872 aom_free(cpi->td.mb.palette_buffer);
Hui Su38711e72019-06-11 10:49:47 -0700873 av1_release_compound_type_rd_buffers(&cpi->td.mb.comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700874 aom_free(cpi->td.mb.tmp_conv_dst);
875 for (int j = 0; j < 2; ++j) {
876 aom_free(cpi->td.mb.tmp_obmc_bufs[j]);
877 }
878
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700879#if CONFIG_DENOISE
880 if (cpi->denoise_and_model) {
881 aom_denoise_and_model_free(cpi->denoise_and_model);
882 cpi->denoise_and_model = NULL;
883 }
884#endif
885 if (cpi->film_grain_table) {
886 aom_film_grain_table_free(cpi->film_grain_table);
887 cpi->film_grain_table = NULL;
888 }
Hui Suc3a8d372019-05-28 15:52:45 -0700889
890 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
891 aom_free(cpi->level_info[i]);
892 }
Marco Paniconi63971322019-08-15 21:32:05 -0700893
894 if (cpi->use_svc) av1_free_svc_cyclic_refresh(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700895}
896
Yaowu Xuf883b422016-08-30 14:01:10 -0700897static void configure_static_seg_features(AV1_COMP *cpi) {
898 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700899 const RATE_CONTROL *const rc = &cpi->rc;
900 struct segmentation *const seg = &cm->seg;
901
902 int high_q = (int)(rc->avg_q > 48.0);
903 int qi_delta;
904
905 // Disable and clear down for KF
David Turnerd2a592e2018-11-16 14:59:31 +0000906 if (cm->current_frame.frame_type == KEY_FRAME) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700907 // Clear down the global segmentation map
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700908 memset(cpi->segmentation_map, 0,
909 cm->mi_params.mi_rows * cm->mi_params.mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700910 seg->update_map = 0;
911 seg->update_data = 0;
912 cpi->static_mb_pct = 0;
913
914 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700915 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700916
917 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700918 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700919 } else if (cpi->refresh_alt_ref_frame) {
920 // If this is an alt ref frame
921 // Clear down the global segmentation map
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700922 memset(cpi->segmentation_map, 0,
923 cm->mi_params.mi_rows * cm->mi_params.mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700924 seg->update_map = 0;
925 seg->update_data = 0;
926 cpi->static_mb_pct = 0;
927
928 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700929 av1_disable_segmentation(seg);
930 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700931
Yaowu Xuc27fc142016-08-22 16:08:15 -0700932 // If segmentation was enabled set those features needed for the
933 // arf itself.
934 if (seg->enabled) {
935 seg->update_map = 1;
936 seg->update_data = 1;
937
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700938 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
939 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700940 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700941 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
942 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
943 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
944 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
945
946 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
947 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
948 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
949 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700950
Yaowu Xuf883b422016-08-30 14:01:10 -0700951 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700952 }
953 } else if (seg->enabled) {
954 // All other frames if segmentation has been enabled
955
956 // First normal frame in a valid gf or alt ref group
957 if (rc->frames_since_golden == 0) {
958 // Set up segment features for normal frames in an arf group
959 if (rc->source_alt_ref_active) {
960 seg->update_map = 0;
961 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700962
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700963 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
964 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700965 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
966 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700967
Cheng Chend8184da2017-09-26 18:15:22 -0700968 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
969 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
970 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
971 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
972
973 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
974 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
975 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
976 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700977
978 // Segment coding disabled for compred testing
979 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700980 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
981 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
982 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700983 }
984 } else {
985 // Disable segmentation and clear down features if alt ref
986 // is not active for this group
987
Yaowu Xuf883b422016-08-30 14:01:10 -0700988 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700989
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700990 memset(cpi->segmentation_map, 0,
991 cm->mi_params.mi_rows * cm->mi_params.mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700992
993 seg->update_map = 0;
994 seg->update_data = 0;
995
Yaowu Xuf883b422016-08-30 14:01:10 -0700996 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700997 }
998 } else if (rc->is_src_frame_alt_ref) {
999 // Special case where we are coding over the top of a previous
1000 // alt ref frame.
1001 // Segment coding disabled for compred testing
1002
1003 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -07001004 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
1005 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001006
1007 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -07001008 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
1009 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
1010 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
1011 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001012
1013 // Skip all MBs if high Q (0,0 mv and skip coeffs)
1014 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001015 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
1016 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001017 }
1018 // Enable data update
1019 seg->update_data = 1;
1020 } else {
1021 // All other frames.
1022
1023 // No updates.. leave things as they are.
1024 seg->update_map = 0;
1025 seg->update_data = 0;
1026 }
1027 }
1028}
1029
Yaowu Xuf883b422016-08-30 14:01:10 -07001030static void update_reference_segmentation_map(AV1_COMP *cpi) {
1031 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001032 const CommonModeInfoParams *const mi_params = &cm->mi_params;
1033 MB_MODE_INFO **mi_4x4_ptr = mi_params->mi_grid_base;
David Turnerb757ce02018-11-12 15:01:28 +00001034 uint8_t *cache_ptr = cm->cur_frame->seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001035
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001036 for (int row = 0; row < mi_params->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -07001037 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001038 uint8_t *cache = cache_ptr;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001039 for (int col = 0; col < mi_params->mi_cols; col++, mi_4x4++, cache++)
Yushin Choa7f65922018-04-04 16:06:11 -07001040 cache[0] = mi_4x4[0]->segment_id;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001041 mi_4x4_ptr += mi_params->mi_stride;
1042 cache_ptr += mi_params->mi_cols;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001043 }
1044}
1045
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05301046static void alloc_altref_frame_buffer(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001047 AV1_COMMON *cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001048 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -07001049 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001050
Yaowu Xuc27fc142016-08-22 16:08:15 -07001051 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001052 if (aom_realloc_frame_buffer(
1053 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
1054 seq_params->subsampling_x, seq_params->subsampling_y,
Yunqing Wanga40d9e82020-02-21 16:37:38 -08001055 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi6237b882020-03-26 15:02:26 -07001056 cm->features.byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07001057 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001058 "Failed to allocate altref buffer");
1059}
1060
Yaowu Xuf883b422016-08-30 14:01:10 -07001061static void alloc_util_frame_buffers(AV1_COMP *cpi) {
1062 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001063 const SequenceHeader *const seq_params = &cm->seq_params;
Urvang Joshi6237b882020-03-26 15:02:26 -07001064 const int byte_alignment = cm->features.byte_alignment;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001065 if (aom_realloc_frame_buffer(
1066 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
1067 seq_params->subsampling_y, seq_params->use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07001068 cpi->oxcf.border_in_pixels, byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07001069 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001070 "Failed to allocate last frame buffer");
1071
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07001072 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08001073 &cpi->trial_frame_rst, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001074 cm->superres_upscaled_height, seq_params->subsampling_x,
1075 seq_params->subsampling_y, seq_params->use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07001076 AOM_RESTORATION_FRAME_BORDER, byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -08001077 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -08001078 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -07001079
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001080 if (aom_realloc_frame_buffer(
1081 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
1082 seq_params->subsampling_y, seq_params->use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07001083 cpi->oxcf.border_in_pixels, byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07001084 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001085 "Failed to allocate scaled source buffer");
1086
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001087 if (aom_realloc_frame_buffer(
1088 &cpi->scaled_last_source, cm->width, cm->height,
1089 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +05301090 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi6237b882020-03-26 15:02:26 -07001091 byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07001092 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001093 "Failed to allocate scaled last source buffer");
1094}
1095
Cheng Chen46f30c72017-09-07 11:13:33 -07001096static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001097 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001098 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001099
chiyotsaia7091f12019-08-09 16:48:27 -07001100 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
1101 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1102 "Failed to allocate context buffers");
1103 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001104
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +05301105 int mi_rows_aligned_to_sb =
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001106 ALIGN_POWER_OF_TWO(cm->mi_params.mi_rows, cm->seq_params.mib_size_log2);
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +05301107 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
1108
Angie Chiangf0fbf9d2017-03-15 15:01:22 -07001109 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -07001110
Yaowu Xuc27fc142016-08-22 16:08:15 -07001111 alloc_context_buffers_ext(cpi);
1112
Yaowu Xuf883b422016-08-30 14:01:10 -07001113 aom_free(cpi->tile_tok[0][0]);
Mufaddal Chakera3d5c0fb2020-03-27 11:04:26 +05301114 aom_free(cpi->tplist[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001115
Mufaddal Chakera3d5c0fb2020-03-27 11:04:26 +05301116 if (!is_stat_generation_stage(cpi)) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001117 unsigned int tokens =
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001118 get_token_alloc(cm->mi_params.mb_rows, cm->mi_params.mb_cols,
1119 MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001120 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -07001121 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +05301122
Mufaddal Chakera3d5c0fb2020-03-27 11:04:26 +05301123 CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
1124 aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
1125 sizeof(*cpi->tplist[0][0])));
1126 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001127
Mufaddal Chakera7b5265f2020-03-27 10:32:34 +05301128 av1_setup_pc_tree(cpi, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001129}
1130
Yaowu Xuf883b422016-08-30 14:01:10 -07001131void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001132 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001133 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001134}
1135
Hui Suef139e12019-05-20 15:51:22 -07001136double av1_get_compression_ratio(const AV1_COMMON *const cm,
1137 size_t encoded_frame_size) {
1138 const int upscaled_width = cm->superres_upscaled_width;
1139 const int height = cm->height;
1140 const int luma_pic_size = upscaled_width * height;
1141 const SequenceHeader *const seq_params = &cm->seq_params;
1142 const BITSTREAM_PROFILE profile = seq_params->profile;
1143 const int pic_size_profile_factor =
1144 profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
1145 encoded_frame_size =
1146 (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
1147 const size_t uncompressed_frame_size =
1148 (luma_pic_size * pic_size_profile_factor) >> 3;
1149 return uncompressed_frame_size / (double)encoded_frame_size;
1150}
1151
Yunqing Wang75e20e82018-06-16 12:10:48 -07001152static void set_tile_info(AV1_COMP *cpi) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001153 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001154 const CommonModeInfoParams *const mi_params = &cm->mi_params;
Urvang Joshi54ffae72020-03-23 13:37:10 -07001155 const SequenceHeader *const seq_params = &cm->seq_params;
1156 CommonTileParams *const tiles = &cm->tiles;
Dominic Symesf58f1112017-09-25 12:47:40 +02001157 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001158
1159 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001160
1161 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +02001162 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Urvang Joshi54ffae72020-03-23 13:37:10 -07001163 tiles->uniform_spacing = 1;
1164 tiles->log2_cols = AOMMAX(cpi->oxcf.tile_columns, tiles->min_log2_cols);
1165 tiles->log2_cols = AOMMIN(tiles->log2_cols, tiles->max_log2_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +02001166 } else {
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001167 int mi_cols =
1168 ALIGN_POWER_OF_TWO(mi_params->mi_cols, seq_params->mib_size_log2);
Urvang Joshi54ffae72020-03-23 13:37:10 -07001169 int sb_cols = mi_cols >> seq_params->mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001170 int size_sb, j = 0;
Urvang Joshi54ffae72020-03-23 13:37:10 -07001171 tiles->uniform_spacing = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +02001172 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
Urvang Joshi54ffae72020-03-23 13:37:10 -07001173 tiles->col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001174 size_sb = cpi->oxcf.tile_widths[j++];
1175 if (j >= cpi->oxcf.tile_width_count) j = 0;
Urvang Joshi54ffae72020-03-23 13:37:10 -07001176 start_sb += AOMMIN(size_sb, tiles->max_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +02001177 }
Urvang Joshi54ffae72020-03-23 13:37:10 -07001178 tiles->cols = i;
1179 tiles->col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001180 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001181 av1_calculate_tile_cols(seq_params, mi_params->mi_rows, mi_params->mi_cols,
1182 tiles);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001183
1184 // configure tile rows
Urvang Joshi54ffae72020-03-23 13:37:10 -07001185 if (tiles->uniform_spacing) {
1186 tiles->log2_rows = AOMMAX(cpi->oxcf.tile_rows, tiles->min_log2_rows);
1187 tiles->log2_rows = AOMMIN(tiles->log2_rows, tiles->max_log2_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +02001188 } else {
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001189 int mi_rows =
1190 ALIGN_POWER_OF_TWO(mi_params->mi_rows, seq_params->mib_size_log2);
Urvang Joshi54ffae72020-03-23 13:37:10 -07001191 int sb_rows = mi_rows >> seq_params->mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001192 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +02001193 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
Urvang Joshi54ffae72020-03-23 13:37:10 -07001194 tiles->row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001195 size_sb = cpi->oxcf.tile_heights[j++];
1196 if (j >= cpi->oxcf.tile_height_count) j = 0;
Urvang Joshi54ffae72020-03-23 13:37:10 -07001197 start_sb += AOMMIN(size_sb, tiles->max_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +02001198 }
Urvang Joshi54ffae72020-03-23 13:37:10 -07001199 tiles->rows = i;
1200 tiles->row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001201 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001202 av1_calculate_tile_rows(seq_params, mi_params->mi_rows, tiles);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001203}
1204
Yaowu Xuf883b422016-08-30 14:01:10 -07001205static void update_frame_size(AV1_COMP *cpi) {
1206 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001207 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
1208
chiyotsaia7091f12019-08-09 16:48:27 -07001209 // We need to reallocate the context buffers here in case we need more mis.
1210 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
1211 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1212 "Failed to allocate context buffers");
1213 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001214 av1_init_mi_buffers(&cm->mi_params);
chiyotsaia7091f12019-08-09 16:48:27 -07001215
Luc Trudeau1e84af52017-11-25 15:00:28 -05001216 av1_init_macroblockd(cm, xd, NULL);
chiyotsai426c0662019-08-05 16:15:11 -07001217
chiyotsaia7091f12019-08-09 16:48:27 -07001218 alloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001219 set_tile_info(cpi);
1220}
1221
Yaowu Xuf883b422016-08-30 14:01:10 -07001222static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001223 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -07001224 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
David Turnera21966b2018-12-05 14:48:49 +00001225 cpi->common.remapped_ref_idx[fb_idx] = fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -07001226 cpi->rate_index = 0;
1227 cpi->rate_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001228}
1229
Debargha Mukherjee57498692018-05-11 13:29:31 -07001230static INLINE int does_level_match(int width, int height, double fps,
1231 int lvl_width, int lvl_height,
1232 double lvl_fps, int lvl_dim_mult) {
1233 const int64_t lvl_luma_pels = lvl_width * lvl_height;
1234 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
1235 const int64_t luma_pels = width * height;
1236 const double display_sample_rate = luma_pels * fps;
1237 return luma_pels <= lvl_luma_pels &&
1238 display_sample_rate <= lvl_display_sample_rate &&
1239 width <= lvl_width * lvl_dim_mult &&
1240 height <= lvl_height * lvl_dim_mult;
1241}
1242
Andrey Norkin26495512018-06-20 17:13:11 -07001243static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Andrey Norkinf481d982018-05-15 12:05:31 -07001244 const AV1EncoderConfig *oxcf) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001245 // TODO(any): This is a placeholder function that only addresses dimensions
1246 // and max display sample rates.
1247 // Need to add checks for max bit rate, max decoded luma sample rate, header
1248 // rate, etc. that are not covered by this function.
Hui Su8427ff22019-03-11 10:14:33 -07001249 AV1_LEVEL level = SEQ_LEVEL_MAX;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001250 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
1251 288, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001252 level = SEQ_LEVEL_2_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001253 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1254 704, 396, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001255 level = SEQ_LEVEL_2_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001256 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1257 1088, 612, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001258 level = SEQ_LEVEL_3_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001259 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1260 1376, 774, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001261 level = SEQ_LEVEL_3_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001262 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1263 2048, 1152, 30.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001264 level = SEQ_LEVEL_4_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001265 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1266 2048, 1152, 60.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001267 level = SEQ_LEVEL_4_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001268 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1269 4096, 2176, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001270 level = SEQ_LEVEL_5_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001271 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1272 4096, 2176, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001273 level = SEQ_LEVEL_5_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001274 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1275 4096, 2176, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001276 level = SEQ_LEVEL_5_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001277 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1278 8192, 4352, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001279 level = SEQ_LEVEL_6_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001280 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1281 8192, 4352, 60.0, 2)) {
Todd Nguyenff4c8602019-11-25 13:20:21 -08001282 level = SEQ_LEVEL_6_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001283 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1284 8192, 4352, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001285 level = SEQ_LEVEL_6_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001286 }
chiyotsaiaa33bbf2019-12-02 12:07:53 -08001287
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001288 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su8427ff22019-03-11 10:14:33 -07001289 seq->seq_level_idx[i] = level;
Andrey Norkin26495512018-06-20 17:13:11 -07001290 // Set the maximum parameters for bitrate and buffer size for this profile,
1291 // level, and tier
Yaowu Xu7e450882019-04-30 15:09:18 -07001292 cm->op_params[i].bitrate = av1_max_level_bitrate(
Hui Su8427ff22019-03-11 10:14:33 -07001293 cm->seq_params.profile, seq->seq_level_idx[i], seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001294 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
1295 // check
Andrey Norkin26495512018-06-20 17:13:11 -07001296 if (cm->op_params[i].bitrate == 0)
1297 aom_internal_error(
1298 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
1299 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -07001300 // Buffer size in bits/s is bitrate in bits/s * 1 s
Andrey Norkin26495512018-06-20 17:13:11 -07001301 cm->op_params[i].buffer_size = cm->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001302 }
1303}
1304
Andrey Norkin26495512018-06-20 17:13:11 -07001305static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
Marco Paniconi67142112019-07-24 15:00:31 -07001306 const AV1EncoderConfig *oxcf, int use_svc) {
Yaowu Xu2a9ac432019-08-06 14:21:17 -07001307 seq->still_picture = (oxcf->force_video_mode == 0) && (oxcf->limit == 1);
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001308 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -07001309 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
kyslov94243382019-05-02 15:33:32 -07001310 seq->force_screen_content_tools = (oxcf->mode == REALTIME) ? 0 : 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001311 seq->force_integer_mv = 2;
David Turnerebf96f42018-11-14 16:57:57 +00001312 seq->order_hint_info.enable_order_hint = oxcf->enable_order_hint;
David Turner936235c2018-11-28 13:42:01 +00001313 seq->frame_id_numbers_present_flag =
1314 !(seq->still_picture && seq->reduced_still_picture_hdr) &&
Marco Paniconi67142112019-07-24 15:00:31 -07001315 !oxcf->large_scale_tile && oxcf->error_resilient_mode && !use_svc;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001316 if (seq->still_picture && seq->reduced_still_picture_hdr) {
David Turnerebf96f42018-11-14 16:57:57 +00001317 seq->order_hint_info.enable_order_hint = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001318 seq->force_screen_content_tools = 2;
1319 seq->force_integer_mv = 2;
1320 }
David Turnerebf96f42018-11-14 16:57:57 +00001321 seq->order_hint_info.order_hint_bits_minus_1 =
1322 seq->order_hint_info.enable_order_hint
1323 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
1324 : -1;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001325
David Turner760a2f42018-12-07 15:25:36 +00001326 seq->max_frame_width =
1327 oxcf->forced_max_frame_width ? oxcf->forced_max_frame_width : oxcf->width;
1328 seq->max_frame_height = oxcf->forced_max_frame_height
1329 ? oxcf->forced_max_frame_height
1330 : oxcf->height;
1331 seq->num_bits_width =
1332 (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
1333 seq->num_bits_height =
1334 (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
1335 assert(seq->num_bits_width <= 16);
1336 assert(seq->num_bits_height <= 16);
1337
1338 seq->frame_id_length = FRAME_ID_LENGTH;
1339 seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
1340
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001341 seq->enable_dual_filter = oxcf->enable_dual_filter;
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -08001342 seq->order_hint_info.enable_dist_wtd_comp = oxcf->enable_dist_wtd_comp;
1343 seq->order_hint_info.enable_dist_wtd_comp &=
David Turnerebf96f42018-11-14 16:57:57 +00001344 seq->order_hint_info.enable_order_hint;
1345 seq->order_hint_info.enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1346 seq->order_hint_info.enable_ref_frame_mvs &=
1347 seq->order_hint_info.enable_order_hint;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001348 seq->enable_superres = oxcf->enable_superres;
1349 seq->enable_cdef = oxcf->enable_cdef;
1350 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -07001351 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -08001352 seq->enable_interintra_compound = oxcf->enable_interintra_comp;
1353 seq->enable_masked_compound = oxcf->enable_masked_comp;
Debargha Mukherjee03c43ba2018-12-14 13:08:08 -08001354 seq->enable_intra_edge_filter = oxcf->enable_intra_edge_filter;
Yue Chen8f9ca582018-12-12 15:11:47 -08001355 seq->enable_filter_intra = oxcf->enable_filter_intra;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001356
Andrey Norkin26495512018-06-20 17:13:11 -07001357 set_bitstream_level_tier(seq, cm, oxcf);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001358
1359 if (seq->operating_points_cnt_minus_1 == 0) {
1360 seq->operating_point_idc[0] = 0;
1361 } else {
Marco Paniconi6da983b2019-09-16 20:12:43 -07001362 // Set operating_point_idc[] such that the i=0 point corresponds to the
1363 // highest quality operating point (all layers), and subsequent
1364 // operarting points (i > 0) are lower quality corresponding to
1365 // skip decoding enhancement layers (temporal first).
1366 int i = 0;
1367 assert(seq->operating_points_cnt_minus_1 ==
1368 (int)(cm->number_spatial_layers * cm->number_temporal_layers - 1));
1369 for (unsigned int sl = 0; sl < cm->number_spatial_layers; sl++) {
1370 for (unsigned int tl = 0; tl < cm->number_temporal_layers; tl++) {
1371 seq->operating_point_idc[i] =
1372 (~(~0u << (cm->number_spatial_layers - sl)) << 8) |
1373 ~(~0u << (cm->number_temporal_layers - tl));
1374 i++;
1375 }
1376 }
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001377 }
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001378}
1379
Yaowu Xuf883b422016-08-30 14:01:10 -07001380static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1381 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001382
1383 cpi->oxcf = *oxcf;
1384 cpi->framerate = oxcf->init_framerate;
1385
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001386 cm->seq_params.profile = oxcf->profile;
1387 cm->seq_params.bit_depth = oxcf->bit_depth;
1388 cm->seq_params.use_highbitdepth = oxcf->use_highbitdepth;
1389 cm->seq_params.color_primaries = oxcf->color_primaries;
1390 cm->seq_params.transfer_characteristics = oxcf->transfer_characteristics;
1391 cm->seq_params.matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001392 cm->seq_params.monochrome = oxcf->monochrome;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001393 cm->seq_params.chroma_sample_position = oxcf->chroma_sample_position;
1394 cm->seq_params.color_range = oxcf->color_range;
Andrey Norkin28e9ce22018-01-08 10:11:21 -08001395 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08001396 cm->timing_info.num_units_in_display_tick =
1397 oxcf->timing_info.num_units_in_display_tick;
1398 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
1399 cm->timing_info.equal_picture_interval =
1400 oxcf->timing_info.equal_picture_interval;
1401 cm->timing_info.num_ticks_per_picture =
1402 oxcf->timing_info.num_ticks_per_picture;
1403
Andrey Norkin26495512018-06-20 17:13:11 -07001404 cm->seq_params.display_model_info_present_flag =
1405 oxcf->display_model_info_present_flag;
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001406 cm->seq_params.decoder_model_info_present_flag =
1407 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08001408 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001409 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08001410 cm->buffer_model.num_units_in_decoding_tick =
1411 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07001412 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07001413 av1_set_aom_dec_model_info(&cm->buffer_model);
1414 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07001415 } else if (cm->timing_info_present &&
1416 cm->timing_info.equal_picture_interval &&
1417 !cm->seq_params.decoder_model_info_present_flag) {
1418 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07001419 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001420 } else {
1421 cm->op_params[0].initial_display_delay =
1422 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08001423 }
Andrey Norkinc7511de2018-06-22 12:31:06 -07001424
Tom Fineganf8d6a162018-08-21 10:47:55 -07001425 if (cm->seq_params.monochrome) {
1426 cm->seq_params.subsampling_x = 1;
1427 cm->seq_params.subsampling_y = 1;
1428 } else if (cm->seq_params.color_primaries == AOM_CICP_CP_BT_709 &&
1429 cm->seq_params.transfer_characteristics == AOM_CICP_TC_SRGB &&
1430 cm->seq_params.matrix_coefficients == AOM_CICP_MC_IDENTITY) {
1431 cm->seq_params.subsampling_x = 0;
1432 cm->seq_params.subsampling_y = 0;
1433 } else {
1434 if (cm->seq_params.profile == 0) {
1435 cm->seq_params.subsampling_x = 1;
1436 cm->seq_params.subsampling_y = 1;
1437 } else if (cm->seq_params.profile == 1) {
1438 cm->seq_params.subsampling_x = 0;
1439 cm->seq_params.subsampling_y = 0;
1440 } else {
1441 if (cm->seq_params.bit_depth == AOM_BITS_12) {
1442 cm->seq_params.subsampling_x = oxcf->chroma_subsampling_x;
1443 cm->seq_params.subsampling_y = oxcf->chroma_subsampling_y;
1444 } else {
1445 cm->seq_params.subsampling_x = 1;
1446 cm->seq_params.subsampling_y = 0;
1447 }
1448 }
Tom Finegan02b2a842018-08-24 13:50:00 -07001449 }
1450
Yaowu Xuc27fc142016-08-22 16:08:15 -07001451 cm->width = oxcf->width;
1452 cm->height = oxcf->height;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001453 set_sb_size(&cm->seq_params,
1454 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -07001455 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -07001456
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08001457 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001458
1459 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -07001460 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001461
Marco Paniconi882c5ad2019-09-30 21:32:29 -07001462 // Set init SVC parameters.
Marco Paniconi67142112019-07-24 15:00:31 -07001463 cpi->use_svc = 0;
Marco Paniconid8574e32019-08-04 21:30:12 -07001464 cpi->svc.external_ref_frame_config = 0;
1465 cpi->svc.non_reference_frame = 0;
Marco Paniconi67142112019-07-24 15:00:31 -07001466 cm->number_spatial_layers = 1;
1467 cm->number_temporal_layers = 1;
Marco Paniconi882c5ad2019-09-30 21:32:29 -07001468 cm->spatial_layer_id = 0;
1469 cm->temporal_layer_id = 0;
Marco Paniconi67142112019-07-24 15:00:31 -07001470
Yaowu Xuc27fc142016-08-22 16:08:15 -07001471 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001472 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001473
1474 cpi->static_mb_pct = 0;
1475 cpi->ref_frame_flags = 0;
1476
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001477 // Reset resize pending flags
1478 cpi->resize_pending_width = 0;
1479 cpi->resize_pending_height = 0;
1480
Yaowu Xuc27fc142016-08-22 16:08:15 -07001481 init_buffer_indices(cpi);
1482}
1483
1484static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001485 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001486 const int64_t bandwidth = oxcf->target_bandwidth;
1487 const int64_t starting = oxcf->starting_buffer_level_ms;
1488 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1489 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1490
1491 rc->starting_buffer_level = starting * bandwidth / 1000;
1492 rc->optimal_buffer_level =
1493 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1494 rc->maximum_buffer_size =
1495 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1496}
1497
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001498#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1499 cpi->fn_ptr[BT].sdf = SDF; \
1500 cpi->fn_ptr[BT].sdaf = SDAF; \
1501 cpi->fn_ptr[BT].vf = VF; \
1502 cpi->fn_ptr[BT].svf = SVF; \
1503 cpi->fn_ptr[BT].svaf = SVAF; \
1504 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1505 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001506 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001507
1508#define MAKE_BFP_SAD_WRAPPER(fnname) \
1509 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1510 int source_stride, \
1511 const uint8_t *ref_ptr, int ref_stride) { \
1512 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1513 } \
1514 static unsigned int fnname##_bits10( \
1515 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1516 int ref_stride) { \
1517 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1518 } \
1519 static unsigned int fnname##_bits12( \
1520 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1521 int ref_stride) { \
1522 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1523 }
1524
1525#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1526 static unsigned int fnname##_bits8( \
1527 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1528 int ref_stride, const uint8_t *second_pred) { \
1529 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1530 } \
1531 static unsigned int fnname##_bits10( \
1532 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1533 int ref_stride, const uint8_t *second_pred) { \
1534 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1535 2; \
1536 } \
1537 static unsigned int fnname##_bits12( \
1538 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1539 int ref_stride, const uint8_t *second_pred) { \
1540 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1541 4; \
1542 }
1543
Yaowu Xuc27fc142016-08-22 16:08:15 -07001544#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1545 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1546 const uint8_t *const ref_ptr[], int ref_stride, \
1547 unsigned int *sad_array) { \
1548 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1549 } \
1550 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1551 const uint8_t *const ref_ptr[], int ref_stride, \
1552 unsigned int *sad_array) { \
1553 int i; \
1554 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1555 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1556 } \
1557 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1558 const uint8_t *const ref_ptr[], int ref_stride, \
1559 unsigned int *sad_array) { \
1560 int i; \
1561 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1562 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1563 }
1564
Cheng Chenbf3d4962017-11-01 14:48:52 -07001565#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1566 static unsigned int fnname##_bits8( \
1567 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1568 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001569 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001570 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1571 jcp_param); \
1572 } \
1573 static unsigned int fnname##_bits10( \
1574 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1575 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001576 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001577 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1578 jcp_param) >> \
1579 2; \
1580 } \
1581 static unsigned int fnname##_bits12( \
1582 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1583 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001584 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001585 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1586 jcp_param) >> \
1587 4; \
1588 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001589
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001590#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001591MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1592MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001593MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1594MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1595MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1596MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1597MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1598MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1599MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001600MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1601MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1602MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1603MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1604MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1605MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1606MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1607MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1608MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1609MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1610MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1611MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1612MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1613MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001614MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1615MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1616MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001617MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1618MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1619MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001620MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1621MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1622MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001623MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1624MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1625MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001626MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1627MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1628MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001629MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1630MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1631MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001632MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1633MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1634MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001635MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1636MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1637MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001638MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001639
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001640MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1641MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1642MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1643MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1644MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1645MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1646MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1647MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1648MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1649MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1650MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1651MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001652MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1653MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1654MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1655MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1656MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1657MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001658
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001659MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x128_avg)
1660MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x64_avg)
1661MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x128_avg)
1662MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x16_avg)
1663MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x32_avg)
1664MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x32_avg)
1665MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x64_avg)
1666MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x32_avg)
1667MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x64_avg)
1668MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x16_avg)
1669MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x8_avg)
1670MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x16_avg)
1671MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x8_avg)
1672MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x4_avg)
1673MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x8_avg)
1674MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x4_avg)
1675MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x16_avg)
1676MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x4_avg)
1677MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x32_avg)
1678MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x8_avg)
1679MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x64_avg)
1680MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x16_avg)
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001681#endif // CONFIG_AV1_HIGHBITDEPTH
Cheng Chenbf3d4962017-11-01 14:48:52 -07001682
David Barker0f3c94e2017-05-16 15:21:50 +01001683#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001684 cpi->fn_ptr[BT].msdf = MCSDF; \
1685 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001686
David Barkerc155e012017-05-11 13:54:54 +01001687#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1688 static unsigned int fnname##_bits8( \
1689 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1690 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1691 int m_stride, int invert_mask) { \
1692 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1693 second_pred_ptr, m, m_stride, invert_mask); \
1694 } \
1695 static unsigned int fnname##_bits10( \
1696 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1697 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1698 int m_stride, int invert_mask) { \
1699 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1700 second_pred_ptr, m, m_stride, invert_mask) >> \
1701 2; \
1702 } \
1703 static unsigned int fnname##_bits12( \
1704 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1705 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1706 int m_stride, int invert_mask) { \
1707 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1708 second_pred_ptr, m, m_stride, invert_mask) >> \
1709 4; \
1710 }
1711
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001712#if CONFIG_AV1_HIGHBITDEPTH
David Barkerf19f35f2017-05-22 16:33:22 +01001713MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1714MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1715MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001716MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1717MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1718MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1719MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1720MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1721MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1722MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1723MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1724MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1725MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1726MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1727MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1728MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001729MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1730MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1731MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1732MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001733MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1734MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001735#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001736
Yaowu Xuc27fc142016-08-22 16:08:15 -07001737#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1738 cpi->fn_ptr[BT].osdf = OSDF; \
1739 cpi->fn_ptr[BT].ovf = OVF; \
1740 cpi->fn_ptr[BT].osvf = OSVF;
1741
1742#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1743 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1744 const int32_t *wsrc, \
1745 const int32_t *msk) { \
1746 return fnname(ref, ref_stride, wsrc, msk); \
1747 } \
1748 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1749 const int32_t *wsrc, \
1750 const int32_t *msk) { \
1751 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1752 } \
1753 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1754 const int32_t *wsrc, \
1755 const int32_t *msk) { \
1756 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1757 }
1758
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001759#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001760MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1761MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1762MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001763MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1764MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1765MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1766MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1767MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1768MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1769MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1770MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1771MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1772MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1773MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1774MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1775MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001776MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1777MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1778MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1779MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001780MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1781MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001782
Yaowu Xuf883b422016-08-30 14:01:10 -07001783static void highbd_set_var_fns(AV1_COMP *const cpi) {
1784 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001785 if (cm->seq_params.use_highbitdepth) {
1786 switch (cm->seq_params.bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001787 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001788 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1789 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1790 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001791 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001792 aom_highbd_sad64x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001793 aom_highbd_dist_wtd_sad64x16_avg_bits8,
1794 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001795
1796 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1797 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1798 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001799 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001800 aom_highbd_sad16x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001801 aom_highbd_dist_wtd_sad16x64_avg_bits8,
1802 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001803
1804 HIGHBD_BFP(
1805 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1806 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001807 aom_highbd_8_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001808 aom_highbd_sad32x8x4d_bits8, aom_highbd_dist_wtd_sad32x8_avg_bits8,
1809 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001810
1811 HIGHBD_BFP(
1812 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1813 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001814 aom_highbd_8_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001815 aom_highbd_sad8x32x4d_bits8, aom_highbd_dist_wtd_sad8x32_avg_bits8,
1816 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001817
1818 HIGHBD_BFP(
1819 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1820 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001821 aom_highbd_8_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001822 aom_highbd_sad16x4x4d_bits8, aom_highbd_dist_wtd_sad16x4_avg_bits8,
1823 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001824
1825 HIGHBD_BFP(
1826 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1827 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001828 aom_highbd_8_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001829 aom_highbd_sad4x16x4d_bits8, aom_highbd_dist_wtd_sad4x16_avg_bits8,
1830 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001831
1832 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1833 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1834 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001835 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001836 aom_highbd_sad32x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001837 aom_highbd_dist_wtd_sad32x16_avg_bits8,
1838 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001839
1840 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1841 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1842 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001843 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001844 aom_highbd_sad16x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001845 aom_highbd_dist_wtd_sad16x32_avg_bits8,
1846 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001847
1848 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1849 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1850 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001851 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001852 aom_highbd_sad64x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001853 aom_highbd_dist_wtd_sad64x32_avg_bits8,
1854 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001855
1856 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1857 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1858 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001859 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001860 aom_highbd_sad32x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001861 aom_highbd_dist_wtd_sad32x64_avg_bits8,
1862 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001863
1864 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1865 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1866 aom_highbd_8_sub_pixel_variance32x32,
1867 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001868 aom_highbd_sad32x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001869 aom_highbd_dist_wtd_sad32x32_avg_bits8,
1870 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001871
1872 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1873 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1874 aom_highbd_8_sub_pixel_variance64x64,
1875 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001876 aom_highbd_sad64x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001877 aom_highbd_dist_wtd_sad64x64_avg_bits8,
1878 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001879
1880 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1881 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1882 aom_highbd_8_sub_pixel_variance16x16,
1883 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001884 aom_highbd_sad16x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001885 aom_highbd_dist_wtd_sad16x16_avg_bits8,
1886 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001887
1888 HIGHBD_BFP(
1889 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1890 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001891 aom_highbd_8_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001892 aom_highbd_sad16x8x4d_bits8, aom_highbd_dist_wtd_sad16x8_avg_bits8,
1893 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001894
1895 HIGHBD_BFP(
1896 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1897 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001898 aom_highbd_8_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001899 aom_highbd_sad8x16x4d_bits8, aom_highbd_dist_wtd_sad8x16_avg_bits8,
1900 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001901
Cheng Chenbf3d4962017-11-01 14:48:52 -07001902 HIGHBD_BFP(
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001903 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1904 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1905 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x4d_bits8,
1906 aom_highbd_dist_wtd_sad8x8_avg_bits8,
1907 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x8)
1908
1909 HIGHBD_BFP(
1910 BLOCK_8X4, aom_highbd_sad8x4_bits8, aom_highbd_sad8x4_avg_bits8,
1911 aom_highbd_8_variance8x4, aom_highbd_8_sub_pixel_variance8x4,
1912 aom_highbd_8_sub_pixel_avg_variance8x4, aom_highbd_sad8x4x4d_bits8,
1913 aom_highbd_dist_wtd_sad8x4_avg_bits8,
1914 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x4)
1915
1916 HIGHBD_BFP(
1917 BLOCK_4X8, aom_highbd_sad4x8_bits8, aom_highbd_sad4x8_avg_bits8,
1918 aom_highbd_8_variance4x8, aom_highbd_8_sub_pixel_variance4x8,
1919 aom_highbd_8_sub_pixel_avg_variance4x8, aom_highbd_sad4x8x4d_bits8,
1920 aom_highbd_dist_wtd_sad4x8_avg_bits8,
1921 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x8)
1922
1923 HIGHBD_BFP(
1924 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1925 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1926 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x4d_bits8,
1927 aom_highbd_dist_wtd_sad4x4_avg_bits8,
1928 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x4)
1929
1930 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1931 aom_highbd_sad128x128_avg_bits8,
1932 aom_highbd_8_variance128x128,
1933 aom_highbd_8_sub_pixel_variance128x128,
1934 aom_highbd_8_sub_pixel_avg_variance128x128,
1935 aom_highbd_sad128x128x4d_bits8,
1936 aom_highbd_dist_wtd_sad128x128_avg_bits8,
1937 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001938
1939 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1940 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1941 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001942 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001943 aom_highbd_sad128x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001944 aom_highbd_dist_wtd_sad128x64_avg_bits8,
1945 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001946
1947 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1948 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1949 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001950 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001951 aom_highbd_sad64x128x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001952 aom_highbd_dist_wtd_sad64x128_avg_bits8,
1953 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001954
David Barkerf19f35f2017-05-22 16:33:22 +01001955 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1956 aom_highbd_8_masked_sub_pixel_variance128x128)
1957 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1958 aom_highbd_8_masked_sub_pixel_variance128x64)
1959 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1960 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001961 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1962 aom_highbd_8_masked_sub_pixel_variance64x64)
1963 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1964 aom_highbd_8_masked_sub_pixel_variance64x32)
1965 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1966 aom_highbd_8_masked_sub_pixel_variance32x64)
1967 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1968 aom_highbd_8_masked_sub_pixel_variance32x32)
1969 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1970 aom_highbd_8_masked_sub_pixel_variance32x16)
1971 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1972 aom_highbd_8_masked_sub_pixel_variance16x32)
1973 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1974 aom_highbd_8_masked_sub_pixel_variance16x16)
1975 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1976 aom_highbd_8_masked_sub_pixel_variance8x16)
1977 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1978 aom_highbd_8_masked_sub_pixel_variance16x8)
1979 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1980 aom_highbd_8_masked_sub_pixel_variance8x8)
1981 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1982 aom_highbd_8_masked_sub_pixel_variance4x8)
1983 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1984 aom_highbd_8_masked_sub_pixel_variance8x4)
1985 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1986 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001987 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1988 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001989 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1990 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001991 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1992 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001993 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1994 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001995 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1996 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001997 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1998 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001999 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
2000 aom_highbd_obmc_variance128x128,
2001 aom_highbd_obmc_sub_pixel_variance128x128)
2002 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
2003 aom_highbd_obmc_variance128x64,
2004 aom_highbd_obmc_sub_pixel_variance128x64)
2005 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
2006 aom_highbd_obmc_variance64x128,
2007 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002008 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
2009 aom_highbd_obmc_variance64x64,
2010 aom_highbd_obmc_sub_pixel_variance64x64)
2011 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
2012 aom_highbd_obmc_variance64x32,
2013 aom_highbd_obmc_sub_pixel_variance64x32)
2014 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
2015 aom_highbd_obmc_variance32x64,
2016 aom_highbd_obmc_sub_pixel_variance32x64)
2017 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
2018 aom_highbd_obmc_variance32x32,
2019 aom_highbd_obmc_sub_pixel_variance32x32)
2020 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
2021 aom_highbd_obmc_variance32x16,
2022 aom_highbd_obmc_sub_pixel_variance32x16)
2023 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
2024 aom_highbd_obmc_variance16x32,
2025 aom_highbd_obmc_sub_pixel_variance16x32)
2026 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
2027 aom_highbd_obmc_variance16x16,
2028 aom_highbd_obmc_sub_pixel_variance16x16)
2029 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
2030 aom_highbd_obmc_variance8x16,
2031 aom_highbd_obmc_sub_pixel_variance8x16)
2032 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
2033 aom_highbd_obmc_variance16x8,
2034 aom_highbd_obmc_sub_pixel_variance16x8)
2035 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
2036 aom_highbd_obmc_variance8x8,
2037 aom_highbd_obmc_sub_pixel_variance8x8)
2038 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
2039 aom_highbd_obmc_variance4x8,
2040 aom_highbd_obmc_sub_pixel_variance4x8)
2041 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
2042 aom_highbd_obmc_variance8x4,
2043 aom_highbd_obmc_sub_pixel_variance8x4)
2044 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
2045 aom_highbd_obmc_variance4x4,
2046 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002047 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
2048 aom_highbd_obmc_variance64x16,
2049 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002050 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
2051 aom_highbd_obmc_variance16x64,
2052 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002053 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
2054 aom_highbd_obmc_variance32x8,
2055 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002056 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
2057 aom_highbd_obmc_variance8x32,
2058 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002059 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
2060 aom_highbd_obmc_variance16x4,
2061 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002062 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
2063 aom_highbd_obmc_variance4x16,
2064 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002065 break;
2066
Yaowu Xuf883b422016-08-30 14:01:10 -07002067 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002068 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
2069 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
2070 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002071 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002072 aom_highbd_sad64x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002073 aom_highbd_dist_wtd_sad64x16_avg_bits10,
2074 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002075
2076 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
2077 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
2078 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002079 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002080 aom_highbd_sad16x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002081 aom_highbd_dist_wtd_sad16x64_avg_bits10,
2082 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002083
2084 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
2085 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
2086 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002087 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002088 aom_highbd_sad32x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002089 aom_highbd_dist_wtd_sad32x8_avg_bits10,
2090 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002091
2092 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
2093 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
2094 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002095 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002096 aom_highbd_sad8x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002097 aom_highbd_dist_wtd_sad8x32_avg_bits10,
2098 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002099
2100 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
2101 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
2102 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002103 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002104 aom_highbd_sad16x4x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002105 aom_highbd_dist_wtd_sad16x4_avg_bits10,
2106 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002107
2108 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
2109 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
2110 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002111 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002112 aom_highbd_sad4x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002113 aom_highbd_dist_wtd_sad4x16_avg_bits10,
2114 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002115
2116 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
2117 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
2118 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002119 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002120 aom_highbd_sad32x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002121 aom_highbd_dist_wtd_sad32x16_avg_bits10,
2122 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002123
2124 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
2125 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
2126 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002127 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002128 aom_highbd_sad16x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002129 aom_highbd_dist_wtd_sad16x32_avg_bits10,
2130 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002131
2132 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
2133 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
2134 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002135 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002136 aom_highbd_sad64x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002137 aom_highbd_dist_wtd_sad64x32_avg_bits10,
2138 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002139
2140 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
2141 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
2142 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002143 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002144 aom_highbd_sad32x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002145 aom_highbd_dist_wtd_sad32x64_avg_bits10,
2146 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002147
2148 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
2149 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
2150 aom_highbd_10_sub_pixel_variance32x32,
2151 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002152 aom_highbd_sad32x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002153 aom_highbd_dist_wtd_sad32x32_avg_bits10,
2154 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002155
2156 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
2157 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
2158 aom_highbd_10_sub_pixel_variance64x64,
2159 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002160 aom_highbd_sad64x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002161 aom_highbd_dist_wtd_sad64x64_avg_bits10,
2162 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002163
2164 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
2165 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
2166 aom_highbd_10_sub_pixel_variance16x16,
2167 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002168 aom_highbd_sad16x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002169 aom_highbd_dist_wtd_sad16x16_avg_bits10,
2170 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002171
2172 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
2173 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
2174 aom_highbd_10_sub_pixel_variance16x8,
2175 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002176 aom_highbd_sad16x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002177 aom_highbd_dist_wtd_sad16x8_avg_bits10,
2178 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002179
2180 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
2181 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
2182 aom_highbd_10_sub_pixel_variance8x16,
2183 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002184 aom_highbd_sad8x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002185 aom_highbd_dist_wtd_sad8x16_avg_bits10,
2186 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002187
2188 HIGHBD_BFP(
2189 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
2190 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002191 aom_highbd_10_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002192 aom_highbd_sad8x8x4d_bits10, aom_highbd_dist_wtd_sad8x8_avg_bits10,
2193 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002194
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002195 HIGHBD_BFP(
2196 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
2197 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
2198 aom_highbd_10_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002199 aom_highbd_sad8x4x4d_bits10, aom_highbd_dist_wtd_sad8x4_avg_bits10,
2200 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002201
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002202 HIGHBD_BFP(
2203 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
2204 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
2205 aom_highbd_10_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002206 aom_highbd_sad4x8x4d_bits10, aom_highbd_dist_wtd_sad4x8_avg_bits10,
2207 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002208
2209 HIGHBD_BFP(
2210 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
2211 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002212 aom_highbd_10_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002213 aom_highbd_sad4x4x4d_bits10, aom_highbd_dist_wtd_sad4x4_avg_bits10,
2214 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002215
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002216 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
2217 aom_highbd_sad128x128_avg_bits10,
2218 aom_highbd_10_variance128x128,
2219 aom_highbd_10_sub_pixel_variance128x128,
2220 aom_highbd_10_sub_pixel_avg_variance128x128,
2221 aom_highbd_sad128x128x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002222 aom_highbd_dist_wtd_sad128x128_avg_bits10,
2223 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002224
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002225 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
2226 aom_highbd_sad128x64_avg_bits10,
2227 aom_highbd_10_variance128x64,
2228 aom_highbd_10_sub_pixel_variance128x64,
2229 aom_highbd_10_sub_pixel_avg_variance128x64,
2230 aom_highbd_sad128x64x4d_bits10,
2231 aom_highbd_dist_wtd_sad128x64_avg_bits10,
2232 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002233
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002234 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
2235 aom_highbd_sad64x128_avg_bits10,
2236 aom_highbd_10_variance64x128,
2237 aom_highbd_10_sub_pixel_variance64x128,
2238 aom_highbd_10_sub_pixel_avg_variance64x128,
2239 aom_highbd_sad64x128x4d_bits10,
2240 aom_highbd_dist_wtd_sad64x128_avg_bits10,
2241 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002242
David Barkerf19f35f2017-05-22 16:33:22 +01002243 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
2244 aom_highbd_10_masked_sub_pixel_variance128x128)
2245 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
2246 aom_highbd_10_masked_sub_pixel_variance128x64)
2247 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
2248 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002249 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
2250 aom_highbd_10_masked_sub_pixel_variance64x64)
2251 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
2252 aom_highbd_10_masked_sub_pixel_variance64x32)
2253 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
2254 aom_highbd_10_masked_sub_pixel_variance32x64)
2255 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
2256 aom_highbd_10_masked_sub_pixel_variance32x32)
2257 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
2258 aom_highbd_10_masked_sub_pixel_variance32x16)
2259 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
2260 aom_highbd_10_masked_sub_pixel_variance16x32)
2261 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
2262 aom_highbd_10_masked_sub_pixel_variance16x16)
2263 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
2264 aom_highbd_10_masked_sub_pixel_variance8x16)
2265 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
2266 aom_highbd_10_masked_sub_pixel_variance16x8)
2267 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
2268 aom_highbd_10_masked_sub_pixel_variance8x8)
2269 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
2270 aom_highbd_10_masked_sub_pixel_variance4x8)
2271 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
2272 aom_highbd_10_masked_sub_pixel_variance8x4)
2273 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
2274 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002275 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
2276 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002277 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
2278 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002279 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
2280 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002281 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
2282 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002283 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
2284 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002285 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
2286 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002287 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
2288 aom_highbd_10_obmc_variance128x128,
2289 aom_highbd_10_obmc_sub_pixel_variance128x128)
2290 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
2291 aom_highbd_10_obmc_variance128x64,
2292 aom_highbd_10_obmc_sub_pixel_variance128x64)
2293 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
2294 aom_highbd_10_obmc_variance64x128,
2295 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002296 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2297 aom_highbd_10_obmc_variance64x64,
2298 aom_highbd_10_obmc_sub_pixel_variance64x64)
2299 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2300 aom_highbd_10_obmc_variance64x32,
2301 aom_highbd_10_obmc_sub_pixel_variance64x32)
2302 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2303 aom_highbd_10_obmc_variance32x64,
2304 aom_highbd_10_obmc_sub_pixel_variance32x64)
2305 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2306 aom_highbd_10_obmc_variance32x32,
2307 aom_highbd_10_obmc_sub_pixel_variance32x32)
2308 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2309 aom_highbd_10_obmc_variance32x16,
2310 aom_highbd_10_obmc_sub_pixel_variance32x16)
2311 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2312 aom_highbd_10_obmc_variance16x32,
2313 aom_highbd_10_obmc_sub_pixel_variance16x32)
2314 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2315 aom_highbd_10_obmc_variance16x16,
2316 aom_highbd_10_obmc_sub_pixel_variance16x16)
2317 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2318 aom_highbd_10_obmc_variance8x16,
2319 aom_highbd_10_obmc_sub_pixel_variance8x16)
2320 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2321 aom_highbd_10_obmc_variance16x8,
2322 aom_highbd_10_obmc_sub_pixel_variance16x8)
2323 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2324 aom_highbd_10_obmc_variance8x8,
2325 aom_highbd_10_obmc_sub_pixel_variance8x8)
2326 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2327 aom_highbd_10_obmc_variance4x8,
2328 aom_highbd_10_obmc_sub_pixel_variance4x8)
2329 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2330 aom_highbd_10_obmc_variance8x4,
2331 aom_highbd_10_obmc_sub_pixel_variance8x4)
2332 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2333 aom_highbd_10_obmc_variance4x4,
2334 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002335
Rupert Swarbrick72678572017-08-02 12:05:26 +01002336 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2337 aom_highbd_10_obmc_variance64x16,
2338 aom_highbd_10_obmc_sub_pixel_variance64x16)
2339
2340 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2341 aom_highbd_10_obmc_variance16x64,
2342 aom_highbd_10_obmc_sub_pixel_variance16x64)
2343
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002344 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2345 aom_highbd_10_obmc_variance32x8,
2346 aom_highbd_10_obmc_sub_pixel_variance32x8)
2347
2348 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2349 aom_highbd_10_obmc_variance8x32,
2350 aom_highbd_10_obmc_sub_pixel_variance8x32)
2351
2352 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2353 aom_highbd_10_obmc_variance16x4,
2354 aom_highbd_10_obmc_sub_pixel_variance16x4)
2355
2356 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2357 aom_highbd_10_obmc_variance4x16,
2358 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002359 break;
2360
Yaowu Xuf883b422016-08-30 14:01:10 -07002361 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002362 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2363 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2364 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002365 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002366 aom_highbd_sad64x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002367 aom_highbd_dist_wtd_sad64x16_avg_bits12,
2368 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002369
2370 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2371 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2372 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002373 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002374 aom_highbd_sad16x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002375 aom_highbd_dist_wtd_sad16x64_avg_bits12,
2376 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002377
2378 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2379 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2380 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002381 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002382 aom_highbd_sad32x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002383 aom_highbd_dist_wtd_sad32x8_avg_bits12,
2384 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002385
2386 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2387 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2388 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002389 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002390 aom_highbd_sad8x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002391 aom_highbd_dist_wtd_sad8x32_avg_bits12,
2392 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002393
2394 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2395 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2396 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002397 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002398 aom_highbd_sad16x4x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002399 aom_highbd_dist_wtd_sad16x4_avg_bits12,
2400 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002401
2402 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2403 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2404 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002405 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002406 aom_highbd_sad4x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002407 aom_highbd_dist_wtd_sad4x16_avg_bits12,
2408 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002409
2410 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2411 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2412 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002413 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002414 aom_highbd_sad32x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002415 aom_highbd_dist_wtd_sad32x16_avg_bits12,
2416 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002417
2418 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2419 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2420 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002421 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002422 aom_highbd_sad16x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002423 aom_highbd_dist_wtd_sad16x32_avg_bits12,
2424 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002425
2426 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2427 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2428 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002429 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002430 aom_highbd_sad64x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002431 aom_highbd_dist_wtd_sad64x32_avg_bits12,
2432 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002433
2434 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2435 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2436 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002437 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002438 aom_highbd_sad32x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002439 aom_highbd_dist_wtd_sad32x64_avg_bits12,
2440 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002441
2442 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2443 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2444 aom_highbd_12_sub_pixel_variance32x32,
2445 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002446 aom_highbd_sad32x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002447 aom_highbd_dist_wtd_sad32x32_avg_bits12,
2448 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002449
2450 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2451 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2452 aom_highbd_12_sub_pixel_variance64x64,
2453 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002454 aom_highbd_sad64x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002455 aom_highbd_dist_wtd_sad64x64_avg_bits12,
2456 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002457
2458 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2459 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2460 aom_highbd_12_sub_pixel_variance16x16,
2461 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002462 aom_highbd_sad16x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002463 aom_highbd_dist_wtd_sad16x16_avg_bits12,
2464 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002465
2466 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2467 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2468 aom_highbd_12_sub_pixel_variance16x8,
2469 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002470 aom_highbd_sad16x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002471 aom_highbd_dist_wtd_sad16x8_avg_bits12,
2472 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002473
2474 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2475 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2476 aom_highbd_12_sub_pixel_variance8x16,
2477 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002478 aom_highbd_sad8x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002479 aom_highbd_dist_wtd_sad8x16_avg_bits12,
2480 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002481
2482 HIGHBD_BFP(
2483 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2484 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002485 aom_highbd_12_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002486 aom_highbd_sad8x8x4d_bits12, aom_highbd_dist_wtd_sad8x8_avg_bits12,
2487 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002488
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002489 HIGHBD_BFP(
2490 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2491 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2492 aom_highbd_12_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002493 aom_highbd_sad8x4x4d_bits12, aom_highbd_dist_wtd_sad8x4_avg_bits12,
2494 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002495
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002496 HIGHBD_BFP(
2497 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2498 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2499 aom_highbd_12_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002500 aom_highbd_sad4x8x4d_bits12, aom_highbd_dist_wtd_sad4x8_avg_bits12,
2501 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002502
2503 HIGHBD_BFP(
2504 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2505 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002506 aom_highbd_12_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002507 aom_highbd_sad4x4x4d_bits12, aom_highbd_dist_wtd_sad4x4_avg_bits12,
2508 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002509
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002510 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2511 aom_highbd_sad128x128_avg_bits12,
2512 aom_highbd_12_variance128x128,
2513 aom_highbd_12_sub_pixel_variance128x128,
2514 aom_highbd_12_sub_pixel_avg_variance128x128,
2515 aom_highbd_sad128x128x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002516 aom_highbd_dist_wtd_sad128x128_avg_bits12,
2517 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002518
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002519 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2520 aom_highbd_sad128x64_avg_bits12,
2521 aom_highbd_12_variance128x64,
2522 aom_highbd_12_sub_pixel_variance128x64,
2523 aom_highbd_12_sub_pixel_avg_variance128x64,
2524 aom_highbd_sad128x64x4d_bits12,
2525 aom_highbd_dist_wtd_sad128x64_avg_bits12,
2526 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002527
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002528 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2529 aom_highbd_sad64x128_avg_bits12,
2530 aom_highbd_12_variance64x128,
2531 aom_highbd_12_sub_pixel_variance64x128,
2532 aom_highbd_12_sub_pixel_avg_variance64x128,
2533 aom_highbd_sad64x128x4d_bits12,
2534 aom_highbd_dist_wtd_sad64x128_avg_bits12,
2535 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002536
David Barkerf19f35f2017-05-22 16:33:22 +01002537 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2538 aom_highbd_12_masked_sub_pixel_variance128x128)
2539 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2540 aom_highbd_12_masked_sub_pixel_variance128x64)
2541 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2542 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002543 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2544 aom_highbd_12_masked_sub_pixel_variance64x64)
2545 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2546 aom_highbd_12_masked_sub_pixel_variance64x32)
2547 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2548 aom_highbd_12_masked_sub_pixel_variance32x64)
2549 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2550 aom_highbd_12_masked_sub_pixel_variance32x32)
2551 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2552 aom_highbd_12_masked_sub_pixel_variance32x16)
2553 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2554 aom_highbd_12_masked_sub_pixel_variance16x32)
2555 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2556 aom_highbd_12_masked_sub_pixel_variance16x16)
2557 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2558 aom_highbd_12_masked_sub_pixel_variance8x16)
2559 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2560 aom_highbd_12_masked_sub_pixel_variance16x8)
2561 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2562 aom_highbd_12_masked_sub_pixel_variance8x8)
2563 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2564 aom_highbd_12_masked_sub_pixel_variance4x8)
2565 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2566 aom_highbd_12_masked_sub_pixel_variance8x4)
2567 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2568 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002569 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2570 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002571 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2572 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002573 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2574 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002575 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2576 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002577 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2578 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002579 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2580 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002581 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2582 aom_highbd_12_obmc_variance128x128,
2583 aom_highbd_12_obmc_sub_pixel_variance128x128)
2584 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2585 aom_highbd_12_obmc_variance128x64,
2586 aom_highbd_12_obmc_sub_pixel_variance128x64)
2587 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2588 aom_highbd_12_obmc_variance64x128,
2589 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002590 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2591 aom_highbd_12_obmc_variance64x64,
2592 aom_highbd_12_obmc_sub_pixel_variance64x64)
2593 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2594 aom_highbd_12_obmc_variance64x32,
2595 aom_highbd_12_obmc_sub_pixel_variance64x32)
2596 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2597 aom_highbd_12_obmc_variance32x64,
2598 aom_highbd_12_obmc_sub_pixel_variance32x64)
2599 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2600 aom_highbd_12_obmc_variance32x32,
2601 aom_highbd_12_obmc_sub_pixel_variance32x32)
2602 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2603 aom_highbd_12_obmc_variance32x16,
2604 aom_highbd_12_obmc_sub_pixel_variance32x16)
2605 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2606 aom_highbd_12_obmc_variance16x32,
2607 aom_highbd_12_obmc_sub_pixel_variance16x32)
2608 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2609 aom_highbd_12_obmc_variance16x16,
2610 aom_highbd_12_obmc_sub_pixel_variance16x16)
2611 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2612 aom_highbd_12_obmc_variance8x16,
2613 aom_highbd_12_obmc_sub_pixel_variance8x16)
2614 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2615 aom_highbd_12_obmc_variance16x8,
2616 aom_highbd_12_obmc_sub_pixel_variance16x8)
2617 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2618 aom_highbd_12_obmc_variance8x8,
2619 aom_highbd_12_obmc_sub_pixel_variance8x8)
2620 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2621 aom_highbd_12_obmc_variance4x8,
2622 aom_highbd_12_obmc_sub_pixel_variance4x8)
2623 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2624 aom_highbd_12_obmc_variance8x4,
2625 aom_highbd_12_obmc_sub_pixel_variance8x4)
2626 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2627 aom_highbd_12_obmc_variance4x4,
2628 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002629 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2630 aom_highbd_12_obmc_variance64x16,
2631 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002632 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2633 aom_highbd_12_obmc_variance16x64,
2634 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002635 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2636 aom_highbd_12_obmc_variance32x8,
2637 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002638 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2639 aom_highbd_12_obmc_variance8x32,
2640 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002641 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2642 aom_highbd_12_obmc_variance16x4,
2643 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002644 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2645 aom_highbd_12_obmc_variance4x16,
2646 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002647 break;
2648
2649 default:
2650 assert(0 &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002651 "cm->seq_params.bit_depth should be AOM_BITS_8, "
Yaowu Xuf883b422016-08-30 14:01:10 -07002652 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002653 }
2654 }
2655}
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002656#endif // CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002657
Yaowu Xuf883b422016-08-30 14:01:10 -07002658static void realloc_segmentation_maps(AV1_COMP *cpi) {
2659 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002660 CommonModeInfoParams *const mi_params = &cm->mi_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002661
2662 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002663 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002664 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002665 aom_calloc(mi_params->mi_rows * mi_params->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002666
2667 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002668 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002669 CHECK_MEM_ERROR(
2670 cm, cpi->cyclic_refresh,
2671 av1_cyclic_refresh_alloc(mi_params->mi_rows, mi_params->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002672
2673 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002674 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002675 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002676 aom_calloc(mi_params->mi_rows * mi_params->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002677}
2678
Yue Chenb4c93f02019-08-05 13:47:31 -07002679static void set_tpl_stats_block_size(AV1_COMP *cpi) {
2680 AV1_COMMON *const cm = &cpi->common;
2681 const int is_720p_or_larger = AOMMIN(cm->width, cm->height) >= 720;
2682
2683 // 0: 4x4, 1: 8x8, 2: 16x16
2684 cpi->tpl_stats_block_mis_log2 = is_720p_or_larger ? 2 : 1;
2685}
2686
Hui Su38711e72019-06-11 10:49:47 -07002687void av1_alloc_compound_type_rd_buffers(AV1_COMMON *const cm,
2688 CompoundTypeRdBuffers *const bufs) {
2689 CHECK_MEM_ERROR(
2690 cm, bufs->pred0,
2691 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred0)));
2692 CHECK_MEM_ERROR(
2693 cm, bufs->pred1,
2694 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred1)));
2695 CHECK_MEM_ERROR(
2696 cm, bufs->residual1,
2697 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->residual1)));
2698 CHECK_MEM_ERROR(
2699 cm, bufs->diff10,
2700 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->diff10)));
2701 CHECK_MEM_ERROR(cm, bufs->tmp_best_mask_buf,
2702 (uint8_t *)aom_malloc(2 * MAX_SB_SQUARE *
2703 sizeof(*bufs->tmp_best_mask_buf)));
2704}
2705
2706void av1_release_compound_type_rd_buffers(CompoundTypeRdBuffers *const bufs) {
2707 aom_free(bufs->pred0);
2708 aom_free(bufs->pred1);
2709 aom_free(bufs->residual1);
2710 aom_free(bufs->diff10);
2711 aom_free(bufs->tmp_best_mask_buf);
2712 av1_zero(*bufs); // Set all pointers to NULL for safety.
2713}
2714
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002715static void config_target_level(AV1_COMP *const cpi, AV1_LEVEL target_level,
2716 int tier) {
2717 aom_clear_system_state();
2718
2719 AV1EncoderConfig *const oxcf = &cpi->oxcf;
2720 SequenceHeader *const seq_params = &cpi->common.seq_params;
2721
2722 // Adjust target bitrate to be no larger than 70% of level limit.
2723 const BITSTREAM_PROFILE profile = seq_params->profile;
2724 const double level_bitrate_limit =
2725 av1_get_max_bitrate_for_level(target_level, tier, profile);
2726 const int64_t max_bitrate = (int64_t)(level_bitrate_limit * 0.70);
2727 oxcf->target_bandwidth = AOMMIN(oxcf->target_bandwidth, max_bitrate);
2728 // Also need to update cpi->twopass.bits_left.
2729 TWO_PASS *const twopass = &cpi->twopass;
Mufaddal Chakera74c9cbe2020-01-17 16:44:59 +05302730 FIRSTPASS_STATS *stats = twopass->total_stats;
2731 if (stats != NULL)
2732 cpi->twopass.bits_left =
2733 (int64_t)(stats->duration * cpi->oxcf.target_bandwidth / 10000000.0);
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002734
2735 // Adjust max over-shoot percentage.
2736 oxcf->over_shoot_pct = 0;
2737
2738 // Adjust max quantizer.
2739 oxcf->worst_allowed_q = 255;
2740
2741 // Adjust number of tiles and tile columns to be under level limit.
2742 int max_tiles, max_tile_cols;
2743 av1_get_max_tiles_for_level(target_level, &max_tiles, &max_tile_cols);
2744 while (oxcf->tile_columns > 0 && (1 << oxcf->tile_columns) > max_tile_cols) {
2745 --oxcf->tile_columns;
2746 }
2747 const int tile_cols = (1 << oxcf->tile_columns);
2748 while (oxcf->tile_rows > 0 &&
2749 tile_cols * (1 << oxcf->tile_rows) > max_tiles) {
2750 --oxcf->tile_rows;
2751 }
2752
2753 // Adjust min compression ratio.
2754 const int still_picture = seq_params->still_picture;
2755 const double min_cr =
2756 av1_get_min_cr_for_level(target_level, tier, still_picture);
2757 oxcf->min_cr = AOMMAX(oxcf->min_cr, (unsigned int)(min_cr * 100));
2758}
2759
Yaowu Xuf883b422016-08-30 14:01:10 -07002760void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2761 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002762 SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002763 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002764 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002765 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002766
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002767 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2768 seq_params->bit_depth = oxcf->bit_depth;
2769 seq_params->color_primaries = oxcf->color_primaries;
2770 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2771 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2772 seq_params->monochrome = oxcf->monochrome;
2773 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2774 seq_params->color_range = oxcf->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002775
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002776 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2777 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002778
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002779 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08002780 cm->timing_info.num_units_in_display_tick =
2781 oxcf->timing_info.num_units_in_display_tick;
2782 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
2783 cm->timing_info.equal_picture_interval =
2784 oxcf->timing_info.equal_picture_interval;
2785 cm->timing_info.num_ticks_per_picture =
2786 oxcf->timing_info.num_ticks_per_picture;
2787
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002788 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07002789 oxcf->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002790 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002791 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08002792 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002793 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08002794 cm->buffer_model.num_units_in_decoding_tick =
2795 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07002796 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07002797 av1_set_aom_dec_model_info(&cm->buffer_model);
2798 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07002799 } else if (cm->timing_info_present &&
2800 cm->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002801 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002802 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07002803 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07002804 } else {
2805 cm->op_params[0].initial_display_delay =
2806 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08002807 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002808
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002809 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002810
Yaowu Xuc27fc142016-08-22 16:08:15 -07002811 cpi->oxcf = *oxcf;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002812 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002813 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002814
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002815 memcpy(cpi->target_seq_level_idx, cpi->oxcf.target_seq_level_idx,
2816 sizeof(cpi->target_seq_level_idx));
2817 cpi->keep_level_stats = 0;
2818 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
2819 if (cpi->target_seq_level_idx[i] <= SEQ_LEVELS) {
2820 cpi->keep_level_stats |= 1u << i;
2821 if (!cpi->level_info[i]) {
2822 CHECK_MEM_ERROR(cm, cpi->level_info[i],
2823 aom_calloc(1, sizeof(*cpi->level_info[i])));
2824 }
2825 }
2826 }
2827
2828 // TODO(huisu@): level targeting currently only works for the 0th operating
2829 // point, so scalable coding is not supported yet.
2830 if (cpi->target_seq_level_idx[0] < SEQ_LEVELS) {
2831 // Adjust encoder config in order to meet target level.
2832 config_target_level(cpi, cpi->target_seq_level_idx[0], seq_params->tier[0]);
2833 }
2834
Mufaddal Chakera3bcc72c2019-12-11 14:38:37 +05302835 if ((has_no_stats_stage(cpi)) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002836 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2837 } else {
2838 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2839 }
2840
2841 cpi->refresh_last_frame = 1;
2842 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002843 cpi->refresh_bwd_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002844
Urvang Joshi6237b882020-03-26 15:02:26 -07002845 cm->features.refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2846 ? REFRESH_FRAME_CONTEXT_DISABLED
2847 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002848 if (oxcf->large_scale_tile)
Urvang Joshi6237b882020-03-26 15:02:26 -07002849 cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002850
Alex Converse74ad0912017-07-18 10:22:58 -07002851 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002852 CHECK_MEM_ERROR(cm, x->palette_buffer,
2853 aom_memalign(16, sizeof(*x->palette_buffer)));
2854 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002855
Hui Su38711e72019-06-11 10:49:47 -07002856 if (x->comp_rd_buffer.pred0 == NULL) {
2857 av1_alloc_compound_type_rd_buffers(cm, &x->comp_rd_buffer);
2858 }
2859
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002860 if (x->tmp_conv_dst == NULL) {
2861 CHECK_MEM_ERROR(
2862 cm, x->tmp_conv_dst,
2863 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002864 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002865 }
2866 for (int i = 0; i < 2; ++i) {
2867 if (x->tmp_obmc_bufs[i] == NULL) {
2868 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
wenyao.liu22d8ab32018-10-16 09:11:29 +08002869 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002870 sizeof(*x->tmp_obmc_bufs[i])));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002871 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002872 }
2873 }
2874
Marco Paniconi177b8c32020-01-26 20:37:29 -08002875 av1_reset_segment_features(cm);
Fyodor Kyslov64aaa092020-01-24 11:44:24 -08002876
chiyotsaic666b1f2019-12-20 10:44:58 -08002877 av1_set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002878
Yaowu Xuc27fc142016-08-22 16:08:15 -07002879 set_rc_buffer_sizes(rc, &cpi->oxcf);
2880
2881 // Under a configuration change, where maximum_buffer_size may change,
2882 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002883 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2884 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002885
2886 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002887 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002888
2889 // Set absolute upper and lower quality limits
2890 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2891 rc->best_quality = cpi->oxcf.best_allowed_q;
2892
Urvang Joshi6237b882020-03-26 15:02:26 -07002893 cm->features.interp_filter =
2894 oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
2895 cm->features.switchable_motion_mode = 1;
Yue Chen5380cb52018-02-23 15:33:21 -08002896
Yaowu Xuc27fc142016-08-22 16:08:15 -07002897 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2898 cm->render_width = cpi->oxcf.render_width;
2899 cm->render_height = cpi->oxcf.render_height;
2900 } else {
2901 cm->render_width = cpi->oxcf.width;
2902 cm->render_height = cpi->oxcf.height;
2903 }
2904 cm->width = cpi->oxcf.width;
2905 cm->height = cpi->oxcf.height;
2906
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002907 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -08002908 // Superblock size should not be updated after the first key frame.
2909 if (!cpi->seq_params_locked) {
2910 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Hui Sud909c2c2019-03-08 11:51:14 -08002911 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
2912 seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
Urvang Joshie4530f82018-01-09 11:43:37 -08002913 }
Dominic Symes917d6c02017-10-11 18:00:52 +02002914
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002915 if (cpi->initial_width || sb_size != seq_params->sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002916 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002917 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002918 av1_free_context_buffers(cm);
Mufaddal Chakera7b5265f2020-03-27 10:32:34 +05302919 av1_free_pc_tree(cpi, &cpi->td, num_planes);
Cheng Chen46f30c72017-09-07 11:13:33 -07002920 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002921 realloc_segmentation_maps(cpi);
2922 cpi->initial_width = cpi->initial_height = 0;
2923 }
2924 }
2925 update_frame_size(cpi);
2926
Yaowu Xuc27fc142016-08-22 16:08:15 -07002927 rc->is_src_frame_alt_ref = 0;
2928
Yaowu Xuc27fc142016-08-22 16:08:15 -07002929 set_tile_info(cpi);
2930
Marco Paniconid8574e32019-08-04 21:30:12 -07002931 if (!cpi->svc.external_ref_frame_config)
2932 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002933 cpi->ext_refresh_frame_context_pending = 0;
2934
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002935#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002936 highbd_set_var_fns(cpi);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002937#endif
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002938
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002939 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002940 // This should not be called after the first key frame.
2941 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002942 seq_params->operating_points_cnt_minus_1 =
Marco Paniconi6da983b2019-09-16 20:12:43 -07002943 (cm->number_spatial_layers > 1 || cm->number_temporal_layers > 1)
2944 ? cm->number_spatial_layers * cm->number_temporal_layers - 1
2945 : 0;
Marco Paniconi67142112019-07-24 15:00:31 -07002946 init_seq_coding_tools(&cm->seq_params, cm, oxcf, cpi->use_svc);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002947 }
Marco Paniconi5b1e4732019-08-08 18:57:53 -07002948
2949 if (cpi->use_svc)
2950 av1_update_layer_context_change_config(cpi, oxcf->target_bandwidth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002951}
2952
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05302953static INLINE void setup_tpl_buffers(AV1_COMP *cpi) {
2954 AV1_COMMON *cm = &cpi->common;
2955 CommonModeInfoParams *const mi_params = &cm->mi_params;
2956
2957 set_tpl_stats_block_size(cpi);
2958 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
2959 const int mi_cols =
2960 ALIGN_POWER_OF_TWO(mi_params->mi_cols, MAX_MIB_SIZE_LOG2);
2961 const int mi_rows =
2962 ALIGN_POWER_OF_TWO(mi_params->mi_rows, MAX_MIB_SIZE_LOG2);
2963
2964 cpi->tpl_stats_buffer[frame].is_valid = 0;
2965 cpi->tpl_stats_buffer[frame].width =
2966 mi_cols >> cpi->tpl_stats_block_mis_log2;
2967 cpi->tpl_stats_buffer[frame].height =
2968 mi_rows >> cpi->tpl_stats_block_mis_log2;
2969 cpi->tpl_stats_buffer[frame].stride = cpi->tpl_stats_buffer[frame].width;
2970 cpi->tpl_stats_buffer[frame].mi_rows = mi_params->mi_rows;
2971 cpi->tpl_stats_buffer[frame].mi_cols = mi_params->mi_cols;
2972 }
2973
2974 for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
2975 CHECK_MEM_ERROR(
2976 cm, cpi->tpl_stats_pool[frame],
2977 aom_calloc(cpi->tpl_stats_buffer[frame].width *
2978 cpi->tpl_stats_buffer[frame].height,
2979 sizeof(*cpi->tpl_stats_buffer[frame].tpl_stats_ptr)));
Urvang Joshi6237b882020-03-26 15:02:26 -07002980 if (aom_alloc_frame_buffer(
2981 &cpi->tpl_rec_pool[frame], cm->width, cm->height,
2982 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
2983 cm->seq_params.use_highbitdepth, AOM_ENC_NO_SCALE_BORDER,
2984 cm->features.byte_alignment))
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05302985 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
2986 "Failed to allocate frame buffer");
2987 }
2988
2989 cpi->tpl_frame = &cpi->tpl_stats_buffer[REF_FRAMES + 1];
2990}
2991
Aasaipriyabd659e42019-12-13 16:12:13 +05302992AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf, BufferPool *const pool,
Mufaddal Chakera5517b282019-12-13 16:39:56 +05302993 FIRSTPASS_STATS *frame_stats_buf,
Akshata Jadhava49be172019-12-18 00:03:53 +05302994 COMPRESSOR_STAGE stage, int num_lap_buffers,
Mufaddal Chakera412efe22020-01-17 14:35:01 +05302995 int lap_lag_in_frames,
Akshata Jadhava49be172019-12-18 00:03:53 +05302996 STATS_BUFFER_CTX *stats_buf_context) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002997 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2998 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002999
3000 if (!cm) return NULL;
3001
Yaowu Xuf883b422016-08-30 14:01:10 -07003002 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003003
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07003004 // The jmp_buf is valid only for the duration of the function that calls
3005 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
3006 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003007 if (setjmp(cm->error.jmp)) {
3008 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003009 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003010 return 0;
3011 }
3012
3013 cm->error.setjmp = 1;
chiyotsaia7091f12019-08-09 16:48:27 -07003014
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003015 CommonModeInfoParams *const mi_params = &cm->mi_params;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003016 mi_params->free_mi = enc_free_mi;
3017 mi_params->setup_mi = enc_setup_mi;
3018 mi_params->set_mb_mi = enc_set_mb_mi;
3019
3020 mi_params->mi_alloc_bsize = BLOCK_4X4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003021
Angie Chianga5d96c42016-10-21 16:16:56 -07003022 CHECK_MEM_ERROR(cm, cm->fc,
3023 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +00003024 CHECK_MEM_ERROR(
3025 cm, cm->default_frame_context,
3026 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -07003027 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +00003028 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003029
Yaowu Xuc27fc142016-08-22 16:08:15 -07003030 cpi->common.buffer_pool = pool;
3031
Mufaddal Chakera5517b282019-12-13 16:39:56 +05303032 cpi->lap_enabled = num_lap_buffers > 0;
3033 cpi->compressor_stage = stage;
Mufaddal Chakera7b5265f2020-03-27 10:32:34 +05303034 init_config(cpi, oxcf);
Mufaddal Chakera11f284a2019-12-19 11:38:46 +05303035 if (cpi->compressor_stage == LAP_STAGE) {
Mufaddal Chakera412efe22020-01-17 14:35:01 +05303036 cpi->oxcf.lag_in_frames = lap_lag_in_frames;
Mufaddal Chakera11f284a2019-12-19 11:38:46 +05303037 }
Mufaddal Chakeraac828682019-12-13 16:31:42 +05303038
Yaowu Xuf883b422016-08-30 14:01:10 -07003039 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003040
Aasaipriyad1ef4602020-03-16 20:13:35 +05303041 cpi->rc.enable_scenecut_detection = 1;
3042 if (cpi->lap_enabled &&
3043 (num_lap_buffers < (MAX_GF_LENGTH_LAP + SCENE_CUT_KEY_TEST_INTERVAL + 1)))
3044 cpi->rc.enable_scenecut_detection = 0;
Jingning Han17af7742019-09-17 16:58:03 -07003045 init_frame_info(&cpi->frame_info, cm);
3046
David Turnerd2a592e2018-11-16 14:59:31 +00003047 cm->current_frame.frame_number = 0;
David Turnera4c96252019-01-11 16:36:39 +00003048 cm->current_frame_id = -1;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07003049 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003050 cpi->partition_search_skippable_frame = 0;
3051 cpi->tile_data = NULL;
David Turnere7ebf902018-12-04 14:04:55 +00003052 cpi->last_show_frame_buf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003053 realloc_segmentation_maps(cpi);
3054
Yaowu Xuc27fc142016-08-22 16:08:15 -07003055 cpi->refresh_alt_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003056
3057 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
3058#if CONFIG_INTERNAL_STATS
3059 cpi->b_calculate_blockiness = 1;
3060 cpi->b_calculate_consistency = 1;
3061 cpi->total_inconsistency = 0;
3062 cpi->psnr.worst = 100.0;
3063 cpi->worst_ssim = 100.0;
3064
3065 cpi->count = 0;
3066 cpi->bytes = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003067#if CONFIG_SPEED_STATS
3068 cpi->tx_search_count = 0;
3069#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003070
3071 if (cpi->b_calculate_psnr) {
3072 cpi->total_sq_error = 0;
3073 cpi->total_samples = 0;
3074 cpi->tot_recode_hits = 0;
3075 cpi->summed_quality = 0;
3076 cpi->summed_weights = 0;
3077 }
3078
3079 cpi->fastssim.worst = 100.0;
3080 cpi->psnrhvs.worst = 100.0;
3081
3082 if (cpi->b_calculate_blockiness) {
3083 cpi->total_blockiness = 0;
3084 cpi->worst_blockiness = 0.0;
3085 }
3086
3087 if (cpi->b_calculate_consistency) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003088 CHECK_MEM_ERROR(
3089 cm, cpi->ssim_vars,
3090 aom_malloc(sizeof(*cpi->ssim_vars) * 4 * cpi->common.mi_params.mi_rows *
3091 cpi->common.mi_params.mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003092 cpi->worst_consistency = 100.0;
3093 }
3094#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003095#if CONFIG_ENTROPY_STATS
3096 av1_zero(aggregate_fc);
3097#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003098
3099 cpi->first_time_stamp_ever = INT64_MAX;
3100
Yaowu Xuc27fc142016-08-22 16:08:15 -07003101#ifdef OUTPUT_YUV_SKINMAP
3102 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
3103#endif
3104#ifdef OUTPUT_YUV_REC
3105 yuv_rec_file = fopen("rec.yuv", "wb");
3106#endif
3107
Mufaddal Chakera5517b282019-12-13 16:39:56 +05303108 assert(MAX_LAP_BUFFERS >= MAX_LAG_BUFFERS);
Akshata Jadhav4be65112019-12-18 00:26:25 +05303109 int size = get_stats_buf_size(num_lap_buffers, MAX_LAG_BUFFERS);
3110 for (int i = 0; i < size; i++)
Aasaipriyabd659e42019-12-13 16:12:13 +05303111 cpi->twopass.frame_stats_arr[i] = &frame_stats_buf[i];
3112
Akshata Jadhava49be172019-12-18 00:03:53 +05303113 cpi->twopass.stats_buf_ctx = stats_buf_context;
3114 cpi->twopass.stats_in = cpi->twopass.stats_buf_ctx->stats_in_start;
3115
Jerome Jiang2612b4d2019-05-29 17:46:47 -07003116#if !CONFIG_REALTIME_ONLY
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303117 if (is_stat_generation_stage(cpi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003118 av1_init_first_pass(cpi);
Hamsalekha S37cc1d12019-12-12 19:27:41 +05303119 } else if (is_stat_consumption_stage(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003120 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
3121 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
3122
Akshata Jadhav1fddf7f2019-12-18 00:49:25 +05303123 if (!cpi->lap_enabled) {
3124 /*Re-initialize to stats buffer, populated by application in the case of
3125 * two pass*/
3126 cpi->twopass.stats_buf_ctx->stats_in_start = oxcf->two_pass_stats_in.buf;
3127 cpi->twopass.stats_in = cpi->twopass.stats_buf_ctx->stats_in_start;
3128 cpi->twopass.stats_buf_ctx->stats_in_end =
3129 &cpi->twopass.stats_buf_ctx->stats_in_start[packets - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003130
Mufaddal Chakera74c9cbe2020-01-17 16:44:59 +05303131 av1_init_second_pass(cpi);
3132 } else {
3133 av1_init_single_pass_lap(cpi);
3134 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003135 }
Jerome Jiang2612b4d2019-05-29 17:46:47 -07003136#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003137
Remya0cce44c2019-08-16 11:57:24 +05303138 int sb_mi_size = av1_get_sb_mi_size(cm);
3139
Jingning Hand064cf02017-06-01 10:00:39 -07003140 CHECK_MEM_ERROR(
3141 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08003142 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08003143 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07003144 CHECK_MEM_ERROR(
3145 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08003146 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08003147 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07003148
3149 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
3150 (int32_t *)aom_memalign(
3151 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
3152
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303153 CHECK_MEM_ERROR(
3154 cm, cpi->td.mb.inter_modes_info,
3155 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303156
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303157 for (int x = 0; x < 2; x++)
3158 for (int y = 0; y < 2; y++)
3159 CHECK_MEM_ERROR(
3160 cm, cpi->td.mb.hash_value_buffer[x][y],
3161 (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
3162 sizeof(*cpi->td.mb.hash_value_buffer[0][0])));
3163
3164 cpi->td.mb.g_crc_initialized = 0;
3165
Jingning Hand064cf02017-06-01 10:00:39 -07003166 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
3167 (int32_t *)aom_memalign(
3168 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
3169
Remya0cce44c2019-08-16 11:57:24 +05303170 CHECK_MEM_ERROR(cm, cpi->td.mb.mbmi_ext,
3171 aom_calloc(sb_mi_size, sizeof(*cpi->td.mb.mbmi_ext)));
3172
David Turner04b70d82019-01-24 15:39:19 +00003173 av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
3174 av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003175
sdengc23c7f12019-06-11 16:56:50 -07003176 {
3177 const int bsize = BLOCK_16X16;
3178 const int w = mi_size_wide[bsize];
3179 const int h = mi_size_high[bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003180 const int num_cols = (mi_params->mi_cols + w - 1) / w;
3181 const int num_rows = (mi_params->mi_rows + h - 1) / h;
sdengf46a1062019-08-04 18:43:50 -07003182 CHECK_MEM_ERROR(cm, cpi->tpl_rdmult_scaling_factors,
3183 aom_calloc(num_rows * num_cols,
3184 sizeof(*cpi->tpl_rdmult_scaling_factors)));
3185 CHECK_MEM_ERROR(cm, cpi->tpl_sb_rdmult_scaling_factors,
3186 aom_calloc(num_rows * num_cols,
3187 sizeof(*cpi->tpl_sb_rdmult_scaling_factors)));
3188 }
3189
3190 {
3191 const int bsize = BLOCK_16X16;
3192 const int w = mi_size_wide[bsize];
3193 const int h = mi_size_high[bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003194 const int num_cols = (mi_params->mi_cols + w - 1) / w;
3195 const int num_rows = (mi_params->mi_rows + h - 1) / h;
sdengc23c7f12019-06-11 16:56:50 -07003196 CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors,
3197 aom_calloc(num_rows * num_cols,
3198 sizeof(*cpi->ssim_rdmult_scaling_factors)));
3199 }
3200
sdeng01959162019-12-20 10:46:24 -08003201#if CONFIG_TUNE_VMAF
3202 {
3203 const int bsize = BLOCK_64X64;
3204 const int w = mi_size_wide[bsize];
3205 const int h = mi_size_high[bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07003206 const int num_cols = (mi_params->mi_cols + w - 1) / w;
3207 const int num_rows = (mi_params->mi_rows + h - 1) / h;
sdeng01959162019-12-20 10:46:24 -08003208 CHECK_MEM_ERROR(cm, cpi->vmaf_rdmult_scaling_factors,
3209 aom_calloc(num_rows * num_cols,
3210 sizeof(*cpi->vmaf_rdmult_scaling_factors)));
sdeng152013e2020-03-18 14:20:31 -07003211 cpi->last_frame_unsharp_amount = 0.0;
sdeng01959162019-12-20 10:46:24 -08003212 }
3213#endif
3214
Mufaddal Chakerae11600c2020-03-26 12:20:12 +05303215 if (!is_stat_generation_stage(cpi)) {
3216 setup_tpl_buffers(cpi);
Jingning Hanf83d6812020-02-27 13:08:19 -08003217 }
Yue Chen7cae98f2018-08-24 10:43:16 -07003218
chiyotsai9c484b32019-03-07 16:01:50 -08003219#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08003220 av1_zero(cpi->partition_stats);
3221#endif
3222
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003223#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
3224 cpi->fn_ptr[BT].sdf = SDF; \
3225 cpi->fn_ptr[BT].sdaf = SDAF; \
3226 cpi->fn_ptr[BT].vf = VF; \
3227 cpi->fn_ptr[BT].svf = SVF; \
3228 cpi->fn_ptr[BT].svaf = SVAF; \
3229 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
3230 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07003231 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003232
Cheng Chenf78632e2017-10-20 15:30:51 -07003233 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003234 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003235 aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
3236 aom_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003237
3238 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003239 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003240 aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
3241 aom_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003242
3243 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003244 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003245 aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
3246 aom_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003247
3248 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003249 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003250 aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
3251 aom_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003252
3253 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003254 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003255 aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
3256 aom_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003257
3258 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003259 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003260 aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
3261 aom_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003262
Cheng Chenf78632e2017-10-20 15:30:51 -07003263 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
3264 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003265 aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
3266 aom_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07003267
3268 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003269 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003270 aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
3271 aom_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003272
3273 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003274 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003275 aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
3276 aom_dist_wtd_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07003277
3278 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003279 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003280 aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
3281 aom_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003282
3283 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003284 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003285 aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
3286 aom_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003287
3288 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003289 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003290 aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
3291 aom_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003292
3293 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003294 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003295 aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
3296 aom_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003297
3298 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
3299 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003300 aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
3301 aom_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003302
3303 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
3304 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003305 aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
3306 aom_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003307
3308 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
3309 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003310 aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
3311 aom_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003312
3313 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003314 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003315 aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
3316 aom_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003317
3318 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003319 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003320 aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
3321 aom_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003322
3323 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003324 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003325 aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003326
3327 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003328 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003329 aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003330
3331 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003332 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003333 aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003334
3335 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003336 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003337 aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003338
Yaowu Xuc27fc142016-08-22 16:08:15 -07003339#define OBFP(BT, OSDF, OVF, OSVF) \
3340 cpi->fn_ptr[BT].osdf = OSDF; \
3341 cpi->fn_ptr[BT].ovf = OVF; \
3342 cpi->fn_ptr[BT].osvf = OSVF;
3343
Yaowu Xuf883b422016-08-30 14:01:10 -07003344 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
3345 aom_obmc_sub_pixel_variance128x128)
3346 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
3347 aom_obmc_sub_pixel_variance128x64)
3348 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
3349 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07003350 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
3351 aom_obmc_sub_pixel_variance64x64)
3352 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
3353 aom_obmc_sub_pixel_variance64x32)
3354 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
3355 aom_obmc_sub_pixel_variance32x64)
3356 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
3357 aom_obmc_sub_pixel_variance32x32)
3358 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
3359 aom_obmc_sub_pixel_variance32x16)
3360 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
3361 aom_obmc_sub_pixel_variance16x32)
3362 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
3363 aom_obmc_sub_pixel_variance16x16)
3364 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
3365 aom_obmc_sub_pixel_variance16x8)
3366 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
3367 aom_obmc_sub_pixel_variance8x16)
3368 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
3369 aom_obmc_sub_pixel_variance8x8)
3370 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
3371 aom_obmc_sub_pixel_variance4x8)
3372 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
3373 aom_obmc_sub_pixel_variance8x4)
3374 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
3375 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003376 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
3377 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003378 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
3379 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003380 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
3381 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003382 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
3383 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003384 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
3385 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003386 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
3387 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003388
David Barkerf19f35f2017-05-22 16:33:22 +01003389#define MBFP(BT, MCSDF, MCSVF) \
3390 cpi->fn_ptr[BT].msdf = MCSDF; \
3391 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003392
David Barkerf19f35f2017-05-22 16:33:22 +01003393 MBFP(BLOCK_128X128, aom_masked_sad128x128,
3394 aom_masked_sub_pixel_variance128x128)
3395 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
3396 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01003397 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
3398 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
3399 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
3400 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
3401 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
3402 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
3403 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
3404 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
3405 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
3406 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
3407 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
3408 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
3409 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003410
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003411 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
3412
3413 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
3414
3415 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
3416
3417 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003418
3419 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
3420
3421 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01003422
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003423#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003424 highbd_set_var_fns(cpi);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003425#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003426
Yaowu Xuf883b422016-08-30 14:01:10 -07003427 /* av1_init_quantizer() is first called here. Add check in
3428 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07003429 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07003430 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003431 */
Yaowu Xuf883b422016-08-30 14:01:10 -07003432 av1_init_quantizer(cpi);
Urvang Joshi17814622020-03-27 17:26:17 -07003433 av1_qm_init(&cm->quant_params, av1_num_planes(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003434
Yaowu Xuf883b422016-08-30 14:01:10 -07003435 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07003436 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003437 cm->superres_upscaled_width = oxcf->width;
3438 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07003439 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003440
3441 cm->error.setjmp = 0;
3442
3443 return cpi;
3444}
3445
Urvang Joshiee2c8112018-05-04 14:53:15 -07003446#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003447#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
3448
3449#define SNPRINT2(H, T, V) \
3450 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07003451#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003452
Yaowu Xuf883b422016-08-30 14:01:10 -07003453void av1_remove_compressor(AV1_COMP *cpi) {
3454 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003455 int t;
3456
3457 if (!cpi) return;
3458
3459 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003460 const int num_planes = av1_num_planes(cm);
3461
David Turnerd2a592e2018-11-16 14:59:31 +00003462 if (cm->current_frame.frame_number > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003463#if CONFIG_ENTROPY_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303464 if (!is_stat_generation_stage(cpi)) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003465 fprintf(stderr, "Writing counts.stt\n");
3466 FILE *f = fopen("counts.stt", "wb");
3467 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
3468 fclose(f);
3469 }
3470#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003471#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003472 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003473
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303474 if (!is_stat_generation_stage(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003475 char headings[512] = { 0 };
3476 char results[512] = { 0 };
3477 FILE *f = fopen("opsnr.stt", "a");
3478 double time_encoded =
3479 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
3480 10000000.000;
3481 double total_encode_time =
3482 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3483 const double dr =
3484 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3485 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3486 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3487 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3488
3489 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003490 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003491 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3492 const double total_ssim =
3493 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3494 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08003495 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003496 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003497 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07003498 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003499 snprintf(results, sizeof(results),
3500 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3501 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003502 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07003503 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003504 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
3505 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
3506 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
3507 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003508 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003509 cpi->psnr.stat[STAT_Y] / cpi->count,
3510 cpi->psnr.stat[STAT_U] / cpi->count,
Yue Chenf0652ed2019-08-13 16:09:25 -07003511 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003512
3513 if (cpi->b_calculate_blockiness) {
3514 SNPRINT(headings, "\t Block\tWstBlck");
3515 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3516 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3517 }
3518
3519 if (cpi->b_calculate_consistency) {
3520 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003521 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003522 (double)cpi->total_inconsistency);
3523
3524 SNPRINT(headings, "\tConsist\tWstCons");
3525 SNPRINT2(results, "\t%7.3f", consistency);
3526 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3527 }
Yue Chenf0652ed2019-08-13 16:09:25 -07003528
3529 SNPRINT(headings, "\t Time\tRcErr\tAbsErr");
3530 SNPRINT2(results, "\t%8.0f", total_encode_time);
3531 SNPRINT2(results, "\t%7.2f", rate_err);
3532 SNPRINT2(results, "\t%7.2f", fabs(rate_err));
3533
3534 fprintf(f, "%s\tAPsnr611\n", headings);
3535 fprintf(f, "%s\t%7.3f\n", results,
3536 (6 * cpi->psnr.stat[STAT_Y] + cpi->psnr.stat[STAT_U] +
3537 cpi->psnr.stat[STAT_V]) /
3538 (cpi->count * 8));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003539 }
3540
3541 fclose(f);
3542 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07003543#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003544#if CONFIG_SPEED_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303545 if (!is_stat_generation_stage(cpi)) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003546 fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
3547 }
3548#endif // CONFIG_SPEED_STATS
chiyotsai92ed0dd2019-01-25 14:50:14 -08003549
chiyotsai9c484b32019-03-07 16:01:50 -08003550#if CONFIG_COLLECT_PARTITION_STATS == 2
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303551 if (!is_stat_generation_stage(cpi)) {
chiyotsai92ed0dd2019-01-25 14:50:14 -08003552 av1_print_partition_stats(&cpi->partition_stats);
3553 }
3554#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003555 }
3556
Jingning Hanf83d6812020-02-27 13:08:19 -08003557 for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
3558 aom_free(cpi->tpl_stats_pool[frame]);
3559 aom_free_frame_buffer(&cpi->tpl_rec_pool[frame]);
Yue Chen7cae98f2018-08-24 10:43:16 -07003560 }
3561
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303562 for (t = cpi->num_workers - 1; t >= 0; --t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003563 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003564 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3565
3566 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003567 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003568
3569 // Deallocate allocated thread data.
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303570 aom_free(thread_data->td->tctx);
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303571 if (t > 0) {
hui sud9a812b2017-07-06 14:34:37 -07003572 aom_free(thread_data->td->palette_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003573 aom_free(thread_data->td->tmp_conv_dst);
Hui Su38711e72019-06-11 10:49:47 -07003574 av1_release_compound_type_rd_buffers(&thread_data->td->comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003575 for (int j = 0; j < 2; ++j) {
3576 aom_free(thread_data->td->tmp_obmc_bufs[j]);
3577 }
Jingning Hand064cf02017-06-01 10:00:39 -07003578 aom_free(thread_data->td->above_pred_buf);
3579 aom_free(thread_data->td->left_pred_buf);
3580 aom_free(thread_data->td->wsrc_buf);
Fyodor Kyslov166648a2020-03-18 16:15:27 -07003581 aom_free(thread_data->td->vt64x64);
wenyao.liu22d8ab32018-10-16 09:11:29 +08003582
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303583 aom_free(thread_data->td->inter_modes_info);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003584 for (int x = 0; x < 2; x++) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303585 for (int y = 0; y < 2; y++) {
3586 aom_free(thread_data->td->hash_value_buffer[x][y]);
3587 thread_data->td->hash_value_buffer[x][y] = NULL;
3588 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003589 }
Jingning Hand064cf02017-06-01 10:00:39 -07003590 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003591 aom_free(thread_data->td->counts);
Mufaddal Chakera7b5265f2020-03-27 10:32:34 +05303592 av1_free_pc_tree(cpi, thread_data->td, num_planes);
Remya0cce44c2019-08-16 11:57:24 +05303593 aom_free(thread_data->td->mbmi_ext);
Yaowu Xuf883b422016-08-30 14:01:10 -07003594 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003595 }
3596 }
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303597#if CONFIG_MULTITHREAD
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303598 if (cpi->row_mt_mutex_ != NULL) {
3599 pthread_mutex_destroy(cpi->row_mt_mutex_);
3600 aom_free(cpi->row_mt_mutex_);
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303601 }
3602#endif
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05303603 av1_row_mt_mem_dealloc(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07003604 aom_free(cpi->tile_thr_data);
3605 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003606
Deepa K G964e72e2018-05-16 16:56:01 +05303607 if (cpi->num_workers > 1) {
3608 av1_loop_filter_dealloc(&cpi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05303609 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
Deepa K G964e72e2018-05-16 16:56:01 +05303610 }
3611
Yaowu Xuc27fc142016-08-22 16:08:15 -07003612 dealloc_compressor_data(cpi);
3613
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003614#if CONFIG_INTERNAL_STATS
3615 aom_free(cpi->ssim_vars);
3616 cpi->ssim_vars = NULL;
3617#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003618
Yaowu Xuf883b422016-08-30 14:01:10 -07003619 av1_remove_common(cm);
Yaowu Xu74e63352019-05-06 09:21:33 -07003620#if CONFIG_HTB_TRELLIS
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08003621 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xu74e63352019-05-06 09:21:33 -07003622#endif // CONFIG_HTB_TRELLIS
Yaowu Xuf883b422016-08-30 14:01:10 -07003623 av1_free_ref_frame_buffers(cm->buffer_pool);
Mufaddal Chakera74c9cbe2020-01-17 16:44:59 +05303624
3625 aom_free(cpi->twopass.total_stats);
3626 aom_free(cpi->twopass.total_left_stats);
3627
Yaowu Xuf883b422016-08-30 14:01:10 -07003628 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003629
3630#ifdef OUTPUT_YUV_SKINMAP
3631 fclose(yuv_skinmap_file);
3632#endif
3633#ifdef OUTPUT_YUV_REC
3634 fclose(yuv_rec_file);
3635#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003636}
3637
Yaowu Xuf883b422016-08-30 14:01:10 -07003638static void generate_psnr_packet(AV1_COMP *cpi) {
3639 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003640 int i;
3641 PSNR_STATS psnr;
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003642#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xue75b58a2020-01-03 12:56:12 -08003643 const uint32_t in_bit_depth = cpi->oxcf.input_bit_depth;
3644 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
3645 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
3646 bit_depth, in_bit_depth);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003647#else
3648 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
3649#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003650
3651 for (i = 0; i < 4; ++i) {
3652 pkt.data.psnr.samples[i] = psnr.samples[i];
3653 pkt.data.psnr.sse[i] = psnr.sse[i];
3654 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3655 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003656 pkt.kind = AOM_CODEC_PSNR_PKT;
3657 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003658}
3659
Yaowu Xuf883b422016-08-30 14:01:10 -07003660int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003661 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3662
Yunqing Wangf2e7a392017-11-08 00:27:21 -08003663 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003664 return 0;
3665}
3666
Thomas Daede497d1952017-08-08 17:33:06 -07003667int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3668 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003669 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003670 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003671 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003672 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003673 return 0;
3674 } else {
3675 return -1;
3676 }
3677}
3678
Thomas Daede497d1952017-08-08 17:33:06 -07003679int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3680 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003681 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003682 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003683 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003684 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003685 return 0;
3686 } else {
3687 return -1;
3688 }
3689}
3690
3691int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003692 cpi->ext_refresh_frame_context = update;
3693 cpi->ext_refresh_frame_context_pending = 1;
3694 return 0;
3695}
3696
3697#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3698// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3699// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3700// not denoise the UV channels at this time. If ever we implement UV channel
3701// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003702void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003703 uint8_t *src = s->y_buffer;
3704 int h = s->y_height;
3705
3706 do {
3707 fwrite(src, s->y_width, 1, f);
3708 src += s->y_stride;
3709 } while (--h);
3710
3711 src = s->u_buffer;
3712 h = s->uv_height;
3713
3714 do {
3715 fwrite(src, s->uv_width, 1, f);
3716 src += s->uv_stride;
3717 } while (--h);
3718
3719 src = s->v_buffer;
3720 h = s->uv_height;
3721
3722 do {
3723 fwrite(src, s->uv_width, 1, f);
3724 src += s->uv_stride;
3725 } while (--h);
3726}
3727#endif
3728
Yaowu Xuc27fc142016-08-22 16:08:15 -07003729#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003730void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003731 uint8_t *src = s->y_buffer;
3732 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003733 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003734 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3735 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3736
3737 do {
3738 fwrite(src16, s->y_width, 2, yuv_rec_file);
3739 src16 += s->y_stride;
3740 } while (--h);
3741
3742 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3743 h = s->uv_height;
3744
3745 do {
3746 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3747 src16 += s->uv_stride;
3748 } while (--h);
3749
3750 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3751 h = s->uv_height;
3752
3753 do {
3754 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3755 src16 += s->uv_stride;
3756 } while (--h);
3757
3758 fflush(yuv_rec_file);
3759 return;
3760 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003761
3762 do {
3763 fwrite(src, s->y_width, 1, yuv_rec_file);
3764 src += s->y_stride;
3765 } while (--h);
3766
3767 src = s->u_buffer;
3768 h = s->uv_height;
3769
3770 do {
3771 fwrite(src, s->uv_width, 1, yuv_rec_file);
3772 src += s->uv_stride;
3773 } while (--h);
3774
3775 src = s->v_buffer;
3776 h = s->uv_height;
3777
3778 do {
3779 fwrite(src, s->uv_width, 1, yuv_rec_file);
3780 src += s->uv_stride;
3781 } while (--h);
3782
3783 fflush(yuv_rec_file);
3784}
3785#endif // OUTPUT_YUV_REC
3786
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003787#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003788static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3789 int i;
3790 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003791 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003792 AV1_COMMON *const cm = &cpi->common;
3793 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3794 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003795 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003796 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003797 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003798 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003799 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003800 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003801 // TODO(sarahparker): The earlier condition for recoding here was:
3802 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3803 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003804 }
3805 }
3806 return recode;
3807}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003808
Yaowu Xuc27fc142016-08-22 16:08:15 -07003809// Function to test for conditions that indicate we should loop
3810// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003811static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3812 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003813 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003814 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003815 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3816 int force_recode = 0;
3817
3818 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
chiyotsai6b430132019-12-18 10:33:51 -08003819 (cpi->sf.hl_sf.recode_loop == ALLOW_RECODE) ||
3820 (frame_is_kfgfarf &&
3821 (cpi->sf.hl_sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003822 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3823 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3824 (rc->projected_frame_size < low_limit && q > minq)) {
3825 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003826 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003827 // Deal with frame undershoot and whether or not we are
3828 // below the automatically set cq level.
3829 if (q > oxcf->cq_level &&
3830 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3831 force_recode = 1;
3832 }
3833 }
3834 }
3835 return force_recode;
3836}
3837
Cheng Chen46f30c72017-09-07 11:13:33 -07003838static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003839 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003840 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003841 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003842
3843 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003844 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Jingning Han667561a2019-07-22 15:48:01 -07003845 if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003846 BufferPool *const pool = cm->buffer_pool;
3847 const YV12_BUFFER_CONFIG *const ref =
David Turnera21966b2018-12-05 14:48:49 +00003848 get_ref_frame_yv12_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003849
3850 if (ref == NULL) {
David Turnere7ebf902018-12-04 14:04:55 +00003851 cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003852 continue;
3853 }
3854
Yaowu Xuc27fc142016-08-22 16:08:15 -07003855 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
Debargha Mukherjee13cccf22019-03-27 23:49:14 -07003856 // Replace the reference buffer with a copy having a thicker border,
3857 // if the reference buffer is higher resolution than the current
3858 // frame, and the border is thin.
3859 if ((ref->y_crop_width > cm->width ||
3860 ref->y_crop_height > cm->height) &&
3861 ref->border < AOM_BORDER_IN_PIXELS) {
3862 RefCntBuffer *ref_fb = get_ref_frame_buf(cm, ref_frame);
3863 if (aom_yv12_realloc_with_new_border(
Urvang Joshi6237b882020-03-26 15:02:26 -07003864 &ref_fb->buf, AOM_BORDER_IN_PIXELS,
3865 cm->features.byte_alignment, num_planes) != 0) {
Debargha Mukherjee13cccf22019-03-27 23:49:14 -07003866 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3867 "Failed to allocate frame buffer");
3868 }
3869 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003870 int force_scaling = 0;
David Turnere7ebf902018-12-04 14:04:55 +00003871 RefCntBuffer *new_fb = cpi->scaled_ref_buf[ref_frame - 1];
3872 if (new_fb == NULL) {
3873 const int new_fb_idx = get_free_fb(cm);
3874 if (new_fb_idx == INVALID_IDX) {
Wan-Teh Chang4a8c0042018-10-05 09:41:52 -07003875 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3876 "Unable to find free frame buffer");
David Turnere7ebf902018-12-04 14:04:55 +00003877 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003878 force_scaling = 1;
David Turnere7ebf902018-12-04 14:04:55 +00003879 new_fb = &pool->frame_bufs[new_fb_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003880 }
David Turnere7ebf902018-12-04 14:04:55 +00003881
3882 if (force_scaling || new_fb->buf.y_crop_width != cm->width ||
3883 new_fb->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003884 if (aom_realloc_frame_buffer(
David Turnere7ebf902018-12-04 14:04:55 +00003885 &new_fb->buf, cm->width, cm->height,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003886 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
Debargha Mukherjeefa946af2019-03-26 16:58:55 -07003887 cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
Urvang Joshi6237b882020-03-26 15:02:26 -07003888 cm->features.byte_alignment, NULL, NULL, NULL)) {
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003889 if (force_scaling) {
3890 // Release the reference acquired in the get_free_fb() call above.
David Turnere7ebf902018-12-04 14:04:55 +00003891 --new_fb->ref_count;
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003892 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003893 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003894 "Failed to allocate frame buffer");
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003895 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003896 av1_resize_and_extend_frame(
David Turnere7ebf902018-12-04 14:04:55 +00003897 ref, &new_fb->buf, (int)cm->seq_params.bit_depth, num_planes);
3898 cpi->scaled_ref_buf[ref_frame - 1] = new_fb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003899 alloc_frame_mvs(cm, new_fb);
3900 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003901 } else {
David Turnera21966b2018-12-05 14:48:49 +00003902 RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003903 buf->buf.y_crop_width = ref->y_crop_width;
3904 buf->buf.y_crop_height = ref->y_crop_height;
David Turnere7ebf902018-12-04 14:04:55 +00003905 cpi->scaled_ref_buf[ref_frame - 1] = buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003906 ++buf->ref_count;
3907 }
3908 } else {
Mufaddal Chakera3bcc72c2019-12-11 14:38:37 +05303909 if (!has_no_stats_stage(cpi)) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003910 }
3911 }
3912}
3913
Yaowu Xuf883b422016-08-30 14:01:10 -07003914static void release_scaled_references(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003915 // TODO(isbs): only refresh the necessary frames, rather than all of them
David Turnere7ebf902018-12-04 14:04:55 +00003916 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3917 RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
3918 if (buf != NULL) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003919 --buf->ref_count;
David Turnere7ebf902018-12-04 14:04:55 +00003920 cpi->scaled_ref_buf[i] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003921 }
3922 }
3923}
3924
Yaowu Xuf883b422016-08-30 14:01:10 -07003925static void set_mv_search_params(AV1_COMP *cpi) {
3926 const AV1_COMMON *const cm = &cpi->common;
Yunqing Wang4b7bf402020-01-28 15:20:53 -08003927 const int max_mv_def = AOMMAX(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003928
3929 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003930 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003931
chiyotsai8cc054a2019-12-12 14:57:43 -08003932 if (cpi->sf.mv_sf.auto_mv_step_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003933 if (frame_is_intra_only(cm)) {
3934 // Initialize max_mv_magnitude for use in the first INTER frame
3935 // after a key/intra-only frame.
3936 cpi->max_mv_magnitude = max_mv_def;
3937 } else {
Yunqing Wang4b7bf402020-01-28 15:20:53 -08003938 // Use cpi->max_mv_magnitude == -1 to exclude first pass case.
3939 if (cm->show_frame && cpi->max_mv_magnitude != -1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003940 // Allow mv_steps to correspond to twice the max mv magnitude found
3941 // in the previous frame, capped by the default max_mv_magnitude based
3942 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003943 cpi->mv_step_param = av1_init_search_range(
3944 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003945 }
Yunqing Wang4b7bf402020-01-28 15:20:53 -08003946 cpi->max_mv_magnitude = -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003947 }
3948 }
3949}
3950
Urvang Joshib6409e92020-03-23 11:23:27 -07003951static void set_screen_content_options(const AV1_COMP *const cpi,
3952 FeatureFlags *const features) {
3953 const AV1_COMMON *const cm = &cpi->common;
Hui Subdf0c992019-02-14 14:52:41 -08003954
3955 if (cm->seq_params.force_screen_content_tools != 2) {
Urvang Joshib6409e92020-03-23 11:23:27 -07003956 features->allow_screen_content_tools = features->allow_intrabc =
Hui Subdf0c992019-02-14 14:52:41 -08003957 cm->seq_params.force_screen_content_tools;
3958 return;
3959 }
3960
3961 if (cpi->oxcf.content == AOM_CONTENT_SCREEN) {
Urvang Joshib6409e92020-03-23 11:23:27 -07003962 features->allow_screen_content_tools = features->allow_intrabc = 1;
Hui Subdf0c992019-02-14 14:52:41 -08003963 return;
3964 }
3965
3966 // Estimate if the source frame is screen content, based on the portion of
3967 // blocks that have few luma colors.
Cheng Chen4f666ca2019-11-18 17:05:03 -08003968 const uint8_t *src = cpi->unfiltered_source->y_buffer;
Hui Subdf0c992019-02-14 14:52:41 -08003969 assert(src != NULL);
Cheng Chen4f666ca2019-11-18 17:05:03 -08003970 const int use_hbd = cpi->unfiltered_source->flags & YV12_FLAG_HIGHBITDEPTH;
3971 const int stride = cpi->unfiltered_source->y_stride;
3972 const int width = cpi->unfiltered_source->y_width;
3973 const int height = cpi->unfiltered_source->y_height;
Hui Subdf0c992019-02-14 14:52:41 -08003974 const int bd = cm->seq_params.bit_depth;
3975 const int blk_w = 16;
3976 const int blk_h = 16;
3977 // These threshold values are selected experimentally.
3978 const int color_thresh = 4;
3979 const unsigned int var_thresh = 0;
3980 // Counts of blocks with no more than color_thresh colors.
3981 int counts_1 = 0;
3982 // Counts of blocks with no more than color_thresh colors and variance larger
3983 // than var_thresh.
3984 int counts_2 = 0;
3985
3986 for (int r = 0; r + blk_h <= height; r += blk_h) {
3987 for (int c = 0; c + blk_w <= width; c += blk_w) {
3988 int count_buf[1 << 12]; // Maximum (1 << 12) color levels.
3989 const uint8_t *const this_src = src + r * stride + c;
3990 const int n_colors =
3991 use_hbd ? av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd,
3992 count_buf)
3993 : av1_count_colors(this_src, stride, blk_w, blk_h, count_buf);
3994 if (n_colors > 1 && n_colors <= color_thresh) {
3995 ++counts_1;
3996 struct buf_2d buf;
3997 buf.stride = stride;
3998 buf.buf = (uint8_t *)this_src;
3999 const unsigned int var =
4000 use_hbd
4001 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
4002 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
4003 if (var > var_thresh) ++counts_2;
4004 }
4005 }
4006 }
4007
4008 // The threshold values are selected experimentally.
Urvang Joshib6409e92020-03-23 11:23:27 -07004009 features->allow_screen_content_tools =
Hui Subdf0c992019-02-14 14:52:41 -08004010 counts_1 * blk_h * blk_w * 10 > width * height;
4011 // IntraBC would force loop filters off, so we use more strict rules that also
4012 // requires that the block has high variance.
Urvang Joshib6409e92020-03-23 11:23:27 -07004013 features->allow_intrabc = features->allow_screen_content_tools &&
4014 counts_2 * blk_h * blk_w * 12 > width * height;
Hui Subdf0c992019-02-14 14:52:41 -08004015}
4016
Yaowu Xuf883b422016-08-30 14:01:10 -07004017static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004018 int i;
Urvang Joshib6409e92020-03-23 11:23:27 -07004019 AV1_COMMON *const cm = &cpi->common;
4020 FeatureFlags *const features = &cm->features;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004021 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07004022 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004023 }
4024 cpi->global_motion_search_done = 0;
Yunqing Wang9411e432019-03-14 15:53:23 -07004025
Urvang Joshib6409e92020-03-23 11:23:27 -07004026 if (frame_is_intra_only(cm)) set_screen_content_options(cpi, features);
4027 cpi->is_screen_content_type = (features->allow_screen_content_tools != 0);
Yunqing Wang9411e432019-03-14 15:53:23 -07004028
David Turner04b70d82019-01-24 15:39:19 +00004029 av1_set_speed_features_framesize_independent(cpi, cpi->speed);
Yaowu Xuf883b422016-08-30 14:01:10 -07004030 av1_set_rd_speed_thresholds(cpi);
Urvang Joshi6237b882020-03-26 15:02:26 -07004031 cm->features.interp_filter = SWITCHABLE;
4032 cm->features.switchable_motion_mode = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004033}
4034
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004035#if !CONFIG_REALTIME_ONLY
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304036double av1_get_gfu_boost_projection_factor(double min_factor, double max_factor,
4037 int frame_count) {
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304038 double factor = sqrt((double)frame_count);
4039 factor = AOMMIN(factor, max_factor);
4040 factor = AOMMAX(factor, min_factor);
4041 factor = (200.0 + 10.0 * factor);
4042 return factor;
4043}
4044
4045static int get_gfu_boost_from_r0_lap(double min_factor, double max_factor,
4046 double r0, int frames_to_key) {
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304047 double factor = av1_get_gfu_boost_projection_factor(min_factor, max_factor,
4048 frames_to_key);
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304049 const int boost = (int)rint(factor / r0);
4050 return boost;
4051}
4052
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304053double av1_get_kf_boost_projection_factor(int frame_count) {
Aasaipriya197771d2020-02-06 20:11:21 +05304054 double factor = sqrt((double)frame_count);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07004055 factor = AOMMIN(factor, 10.0);
4056 factor = AOMMAX(factor, 4.0);
Aasaipriya197771d2020-02-06 20:11:21 +05304057 factor = (75.0 + 14.0 * factor);
4058 return factor;
4059}
4060
4061static int get_kf_boost_from_r0(double r0, int frames_to_key) {
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304062 double factor = av1_get_kf_boost_projection_factor(frames_to_key);
Aasaipriya197771d2020-02-06 20:11:21 +05304063 const int boost = (int)rint(factor / r0);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07004064 return boost;
4065}
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004066#endif
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07004067
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304068#define MIN_BOOST_COMBINE_FACTOR 4.0
4069#define MAX_BOOST_COMBINE_FACTOR 12.0
4070int combine_prior_with_tpl_boost(double min_factor, double max_factor,
4071 int prior_boost, int tpl_boost,
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07004072 int frames_to_key) {
4073 double factor = sqrt((double)frames_to_key);
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304074 double range = max_factor - min_factor;
4075 factor = AOMMIN(factor, max_factor);
4076 factor = AOMMAX(factor, min_factor);
4077 factor -= min_factor;
4078 int boost =
4079 (int)((factor * prior_boost + (range - factor) * tpl_boost) / range);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07004080 return boost;
4081}
4082
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004083#if !CONFIG_REALTIME_ONLY
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004084static void process_tpl_stats_frame(AV1_COMP *cpi) {
Sarah Parkere1b22012019-06-06 16:35:25 -07004085 const GF_GROUP *const gf_group = &cpi->gf_group;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004086 AV1_COMMON *const cm = &cpi->common;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004087
Sarah Parkere1b22012019-06-06 16:35:25 -07004088 assert(IMPLIES(gf_group->size > 0, gf_group->index < gf_group->size));
Jingning Han31a0ee92019-07-15 13:56:55 -07004089
4090 const int tpl_idx = gf_group->index;
Jingning Han81d6fbb2019-07-15 10:14:13 -07004091 TplDepFrame *tpl_frame = &cpi->tpl_frame[tpl_idx];
Yue Chen4e585cc2019-06-03 14:47:16 -07004092 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004093
Yue Chen4e585cc2019-06-03 14:47:16 -07004094 if (tpl_frame->is_valid) {
4095 int tpl_stride = tpl_frame->stride;
4096 int64_t intra_cost_base = 0;
4097 int64_t mc_dep_cost_base = 0;
Yue Chenbd934232019-08-05 14:23:39 -07004098#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07004099 int64_t mc_saved_base = 0;
4100 int64_t mc_count_base = 0;
Yue Chenbd934232019-08-05 14:23:39 -07004101#endif // !USE_TPL_CLASSIC_MODEL
Yue Chenb4c93f02019-08-05 13:47:31 -07004102 const int step = 1 << cpi->tpl_stats_block_mis_log2;
Debargha Mukherjeeedfa4fe2019-07-08 14:23:39 -07004103 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
Yue Chenb4c93f02019-08-05 13:47:31 -07004104
Urvang Joshi9dc909d2020-03-23 16:07:02 -07004105 for (int row = 0; row < cm->mi_params.mi_rows; row += step) {
Yue Chenb4c93f02019-08-05 13:47:31 -07004106 for (int col = 0; col < mi_cols_sr; col += step) {
4107 TplDepStats *this_stats =
4108 &tpl_stats[av1_tpl_ptr_pos(cpi, row, col, tpl_stride)];
Jingning Han40e870f2019-10-02 16:05:39 -07004109 int64_t mc_dep_delta =
4110 RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
4111 this_stats->mc_dep_dist);
4112 intra_cost_base += (this_stats->recrf_dist << RDDIV_BITS);
4113 mc_dep_cost_base +=
4114 (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta;
Yue Chenbd934232019-08-05 14:23:39 -07004115#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07004116 mc_count_base += this_stats->mc_count;
4117 mc_saved_base += this_stats->mc_saved;
Yue Chenbd934232019-08-05 14:23:39 -07004118#endif // !USE_TPL_CLASSIC_MODEL
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004119 }
Yue Chen4e585cc2019-06-03 14:47:16 -07004120 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004121
Yue Chen4e585cc2019-06-03 14:47:16 -07004122 if (mc_dep_cost_base == 0) {
4123 tpl_frame->is_valid = 0;
4124 } else {
4125 aom_clear_system_state();
4126 cpi->rd.r0 = (double)intra_cost_base / mc_dep_cost_base;
Jingning Han44a573b2019-09-04 14:01:22 -07004127 if (is_frame_arf_and_tpl_eligible(gf_group)) {
Yue Chen4e585cc2019-06-03 14:47:16 -07004128 cpi->rd.arf_r0 = cpi->rd.r0;
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304129 if (cpi->lap_enabled) {
4130 double min_boost_factor = sqrt(cpi->rc.baseline_gf_interval);
4131 const int gfu_boost = get_gfu_boost_from_r0_lap(
4132 min_boost_factor, MAX_GFUBOOST_FACTOR, cpi->rd.arf_r0,
4133 cpi->rc.num_stats_required_for_gfu_boost);
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304134 // printf("old boost %d new boost %d\n", cpi->rc.gfu_boost,
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304135 // gfu_boost);
4136 cpi->rc.gfu_boost = combine_prior_with_tpl_boost(
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304137 min_boost_factor, MAX_BOOST_COMBINE_FACTOR, cpi->rc.gfu_boost,
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304138 gfu_boost, cpi->rc.num_stats_used_for_gfu_boost);
4139 } else {
4140 const int gfu_boost = (int)(200.0 / cpi->rd.r0);
4141 cpi->rc.gfu_boost = combine_prior_with_tpl_boost(
4142 MIN_BOOST_COMBINE_FACTOR, MAX_BOOST_COMBINE_FACTOR,
4143 cpi->rc.gfu_boost, gfu_boost, cpi->rc.frames_to_key);
4144 }
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07004145 } else if (frame_is_intra_only(cm)) {
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07004146 // TODO(debargha): Turn off q adjustment for kf temporarily to
4147 // reduce impact on speed of encoding. Need to investigate how
4148 // to mitigate the issue.
4149 if (cpi->oxcf.rc_mode == AOM_Q) {
4150 const int kf_boost =
4151 get_kf_boost_from_r0(cpi->rd.r0, cpi->rc.frames_to_key);
Aasaipriya197771d2020-02-06 20:11:21 +05304152 if (cpi->lap_enabled) {
Aasaipriya197771d2020-02-06 20:11:21 +05304153 cpi->rc.kf_boost = combine_prior_with_tpl_boost(
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304154 MIN_BOOST_COMBINE_FACTOR, MAX_BOOST_COMBINE_FACTOR,
Mufaddal Chakera0ec9bb62020-02-25 14:23:10 +05304155 cpi->rc.kf_boost, kf_boost,
Aasaipriya197771d2020-02-06 20:11:21 +05304156 cpi->rc.num_stats_used_for_kf_boost);
4157 } else {
4158 cpi->rc.kf_boost = combine_prior_with_tpl_boost(
Mufaddal Chakerac956d2d2020-02-12 15:42:23 +05304159 MIN_BOOST_COMBINE_FACTOR, MAX_BOOST_COMBINE_FACTOR,
Aasaipriya197771d2020-02-06 20:11:21 +05304160 cpi->rc.kf_boost, kf_boost, cpi->rc.frames_to_key);
4161 }
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07004162 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004163 }
Yue Chenbd934232019-08-05 14:23:39 -07004164#if !USE_TPL_CLASSIC_MODEL
Urvang Joshi9dc909d2020-03-23 16:07:02 -07004165 cpi->rd.mc_count_base = (double)mc_count_base /
4166 (cm->mi_params.mi_rows * cm->mi_params.mi_cols);
4167 cpi->rd.mc_saved_base = (double)mc_saved_base /
4168 (cm->mi_params.mi_rows * cm->mi_params.mi_cols);
Yue Chenbd934232019-08-05 14:23:39 -07004169#endif // !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07004170 aom_clear_system_state();
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004171 }
4172 }
4173}
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004174#endif // !CONFIG_REALTIME_ONLY
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004175
Yaowu Xuf883b422016-08-30 14:01:10 -07004176static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004177 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004178 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004179
4180 // Setup variables that depend on the dimensions of the frame.
David Turner04b70d82019-01-24 15:39:19 +00004181 av1_set_speed_features_framesize_dependent(cpi, cpi->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004182
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004183#if !CONFIG_REALTIME_ONLY
chiyotsai0fa717c2020-02-05 12:22:01 -08004184 if (cpi->oxcf.enable_tpl_model && is_frame_tpl_eligible(cpi)) {
Yue Chen4e585cc2019-06-03 14:47:16 -07004185 process_tpl_stats_frame(cpi);
sdengf46a1062019-08-04 18:43:50 -07004186 av1_tpl_rdmult_setup(cpi);
4187 }
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004188#endif
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004189
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01004190 // Decide q and q bounds.
Angie Chiang958904c2019-09-19 16:03:35 -07004191 *q = av1_rc_pick_q_and_bounds(cpi, &cpi->rc, cm->width, cm->height,
4192 cpi->gf_group.index, bottom_index, top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004193
Yaowu Xuc27fc142016-08-22 16:08:15 -07004194 // Configure experimental use of segmentation for enhanced coding of
4195 // static regions if indicated.
4196 // Only allowed in the second pass of a two pass encode, as it requires
4197 // lagged coding, and if the relevant speed feature flag is set.
chiyotsai6b430132019-12-18 10:33:51 -08004198 if (is_stat_consumption_stage_twopass(cpi) &&
4199 cpi->sf.hl_sf.static_segmentation)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004200 configure_static_seg_features(cpi);
4201}
4202
Yaowu Xuf883b422016-08-30 14:01:10 -07004203static void init_motion_estimation(AV1_COMP *cpi) {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004204 AV1_COMMON *const cm = &cpi->common;
chiyotsai836b69b2019-04-09 13:41:24 -07004205 const int y_stride = cpi->scaled_source.y_stride;
Urvang Joshi510d8f62019-01-10 12:11:50 -08004206 const int y_stride_src =
4207 ((cpi->oxcf.width != cm->width || cpi->oxcf.height != cm->height) ||
4208 av1_superres_scaled(cm))
4209 ? y_stride
4210 : cpi->lookahead->buf->img.y_stride;
chiyotsai836b69b2019-04-09 13:41:24 -07004211 // Update if ss_cfg is uninitialized or the current frame has a new stride
4212 const int should_update = !cpi->ss_cfg[SS_CFG_SRC].stride ||
4213 !cpi->ss_cfg[SS_CFG_LOOKAHEAD].stride ||
4214 (y_stride != cpi->ss_cfg[SS_CFG_SRC].stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004215
chiyotsai836b69b2019-04-09 13:41:24 -07004216 if (!should_update) {
4217 return;
4218 }
4219
chiyotsai8cc054a2019-12-12 14:57:43 -08004220 if (cpi->sf.mv_sf.search_method == DIAMOND) {
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05304221 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
4222 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD],
4223 y_stride_src);
chiyotsai836b69b2019-04-09 13:41:24 -07004224 } else {
chiyotsai836b69b2019-04-09 13:41:24 -07004225 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
4226 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD], y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004227 }
Jingning Han969a8d42019-12-16 19:40:14 -08004228 av1_init_motion_fpf(&cpi->ss_cfg[SS_CFG_FPF], y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004229}
4230
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004231#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004232static void set_restoration_unit_size(int width, int height, int sx, int sy,
4233 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004234 (void)width;
4235 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004236 (void)sx;
4237 (void)sy;
4238#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
4239 int s = AOMMIN(sx, sy);
4240#else
4241 int s = 0;
4242#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
4243
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07004244 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08004245 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07004246 else
Urvang Joshi813186b2018-03-08 15:38:46 -08004247 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004248 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
4249 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004250}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004251
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05304252static void init_ref_frame_bufs(AV1_COMP *cpi) {
4253 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07004254 int i;
4255 BufferPool *const pool = cm->buffer_pool;
Jack Haughtonddb80602018-11-21 16:41:49 +00004256 cm->cur_frame = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -07004257 for (i = 0; i < REF_FRAMES; ++i) {
David Turnere7ebf902018-12-04 14:04:55 +00004258 cm->ref_frame_map[i] = NULL;
Wan-Teh Changd05e0332018-10-03 12:00:43 -07004259 }
4260 for (i = 0; i < FRAME_BUFFERS; ++i) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004261 pool->frame_bufs[i].ref_count = 0;
4262 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004263}
4264
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05304265void av1_check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
4266 int subsampling_x, int subsampling_y) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004267 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004268 SequenceHeader *const seq_params = &cm->seq_params;
Cheng Chen46f30c72017-09-07 11:13:33 -07004269
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004270 if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
4271 seq_params->subsampling_x != subsampling_x ||
4272 seq_params->subsampling_y != subsampling_y) {
4273 seq_params->subsampling_x = subsampling_x;
4274 seq_params->subsampling_y = subsampling_y;
4275 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07004276
Jingning Han183b2a82019-12-18 16:03:41 -08004277 av1_set_speed_features_framesize_independent(cpi, cpi->oxcf.speed);
4278 av1_set_speed_features_framesize_dependent(cpi, cpi->oxcf.speed);
4279
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05304280 alloc_altref_frame_buffer(cpi);
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05304281 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07004282 alloc_util_frame_buffers(cpi);
4283
4284 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
4285
4286 cpi->initial_width = cm->width;
4287 cpi->initial_height = cm->height;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07004288 cpi->initial_mbs = cm->mi_params.MBs;
Cheng Chen46f30c72017-09-07 11:13:33 -07004289 }
4290}
4291
4292// Returns 1 if the assigned width or height was <= 0.
Marco Paniconi63971322019-08-15 21:32:05 -07004293int av1_set_size_literal(AV1_COMP *cpi, int width, int height) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004294 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004295 const int num_planes = av1_num_planes(cm);
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05304296 av1_check_initial_width(cpi, cm->seq_params.use_highbitdepth,
4297 cm->seq_params.subsampling_x,
4298 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07004299
4300 if (width <= 0 || height <= 0) return 1;
4301
4302 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07004303 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004304
4305 if (cpi->initial_width && cpi->initial_height &&
4306 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
4307 av1_free_context_buffers(cm);
Mufaddal Chakera7b5265f2020-03-27 10:32:34 +05304308 av1_free_pc_tree(cpi, &cpi->td, num_planes);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004309 alloc_compressor_data(cpi);
4310 realloc_segmentation_maps(cpi);
4311 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07004312 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004313 update_frame_size(cpi);
4314
4315 return 0;
4316}
4317
David Turner475a3132019-01-18 15:17:17 +00004318void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07004319 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004320 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004321 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004322 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004323 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004324
Fergus Simpsonbc189932017-05-16 17:02:39 -07004325 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07004326 // There has been a change in the encoded frame size
Marco Paniconi63971322019-08-15 21:32:05 -07004327 av1_set_size_literal(cpi, width, height);
Urvang Joshic8b52d52018-03-23 13:16:51 -07004328 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Urvang Joshib6409e92020-03-23 11:23:27 -07004329 cm->features.all_lossless =
4330 cm->features.coded_lossless && !av1_superres_scaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004331 }
Yunqing Wang4b7bf402020-01-28 15:20:53 -08004332 set_mv_search_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004333
Hamsalekha S37cc1d12019-12-12 19:27:41 +05304334 if (is_stat_consumption_stage(cpi)) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004335 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004336 }
4337
David Turnere7ebf902018-12-04 14:04:55 +00004338 alloc_frame_mvs(cm, cm->cur_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004339
Cherma Rajan A71d20db2018-04-27 11:15:32 +05304340 // Allocate above context buffers
Cherma Rajan Af1479082018-05-09 14:26:34 +05304341 if (cm->num_allocated_above_context_planes < av1_num_planes(cm) ||
Urvang Joshi9dc909d2020-03-23 16:07:02 -07004342 cm->num_allocated_above_context_mi_col < cm->mi_params.mi_cols ||
Urvang Joshi54ffae72020-03-23 13:37:10 -07004343 cm->num_allocated_above_contexts < cm->tiles.rows) {
Cherma Rajan A71d20db2018-04-27 11:15:32 +05304344 av1_free_above_context_buffers(cm, cm->num_allocated_above_contexts);
Urvang Joshi54ffae72020-03-23 13:37:10 -07004345 if (av1_alloc_above_context_buffers(cm, cm->tiles.rows))
Cherma Rajan A71d20db2018-04-27 11:15:32 +05304346 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
4347 "Failed to allocate context buffers");
4348 }
4349
Yaowu Xuc27fc142016-08-22 16:08:15 -07004350 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004351 if (aom_realloc_frame_buffer(
Jack Haughtonddb80602018-11-21 16:41:49 +00004352 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
4353 seq_params->subsampling_y, seq_params->use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07004354 cpi->oxcf.border_in_pixels, cm->features.byte_alignment, NULL, NULL,
4355 NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07004356 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004357 "Failed to allocate frame buffer");
4358
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004359 const int frame_width = cm->superres_upscaled_width;
4360 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004361 set_restoration_unit_size(frame_width, frame_height,
4362 seq_params->subsampling_x,
4363 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004364 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01004365 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004366
4367 av1_alloc_restoration_buffers(cm);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004368 alloc_util_frame_buffers(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004369 init_motion_estimation(cpi);
4370
4371 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00004372 RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00004373 if (buf != NULL) {
David Turnera21966b2018-12-05 14:48:49 +00004374 struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
4375 av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
David Turner1bcefb32018-11-19 17:54:00 +00004376 buf->buf.y_crop_height, cm->width,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08004377 cm->height);
David Turnera21966b2018-12-05 14:48:49 +00004378 if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004379 }
4380 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07004381
Hui Su5ebd8702018-01-08 18:09:20 -08004382 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08004383 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004384
4385 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
4386}
4387
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004388static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
4389 // Choose an arbitrary random number
4390 static unsigned int seed = 56789;
4391 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Mufaddal Chakerae7326122019-12-04 14:49:09 +05304392 if (is_stat_generation_stage(cpi)) return SCALE_NUMERATOR;
Urvang Joshide71d142017-10-05 12:12:15 -07004393 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004394
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07004395 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004396 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004397 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004398 case RESIZE_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00004399 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004400 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004401 else
Urvang Joshide71d142017-10-05 12:12:15 -07004402 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004403 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004404 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004405 default: assert(0);
4406 }
Urvang Joshide71d142017-10-05 12:12:15 -07004407 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004408}
4409
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004410#if CONFIG_SUPERRES_IN_RECODE
4411static int superres_in_recode_allowed(const AV1_COMP *const cpi) {
4412 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
4413 // Empirically found to not be beneficial for AOM_Q mode and images coding.
4414 return oxcf->superres_mode == SUPERRES_AUTO &&
4415 (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ) &&
4416 cpi->rc.frames_to_key > 1;
4417}
4418#endif // CONFIG_SUPERRES_IN_RECODE
4419
Urvang Joshi510d8f62019-01-10 12:11:50 -08004420#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO 0.012
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004421#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME 0.008
4422#define SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME 0.008
Urvang Joshi510d8f62019-01-10 12:11:50 -08004423#define SUPERRES_ENERGY_BY_AC_THRESH 0.2
4424
4425static double get_energy_by_q2_thresh(const GF_GROUP *gf_group,
4426 const RATE_CONTROL *rc) {
4427 // TODO(now): Return keyframe thresh * factor based on frame type / pyramid
4428 // level.
4429 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
4430 return SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME;
4431 } else if (gf_group->update_type[gf_group->index] == KF_UPDATE) {
4432 if (rc->frames_to_key <= 1)
4433 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO;
4434 else
4435 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME;
4436 } else {
4437 assert(0);
4438 }
4439 return 0;
4440}
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004441
4442static uint8_t get_superres_denom_from_qindex_energy(int qindex, double *energy,
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004443 double threshq,
4444 double threshp) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004445 const double q = av1_convert_qindex_to_q(qindex, AOM_BITS_8);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004446 const double tq = threshq * q * q;
4447 const double tp = threshp * energy[1];
4448 const double thresh = AOMMIN(tq, tp);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004449 int k;
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004450 for (k = SCALE_NUMERATOR * 2; k > SCALE_NUMERATOR; --k) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004451 if (energy[k - 1] > thresh) break;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004452 }
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004453 return 3 * SCALE_NUMERATOR - k;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004454}
4455
Urvang Joshi510d8f62019-01-10 12:11:50 -08004456static uint8_t get_superres_denom_for_qindex(const AV1_COMP *cpi, int qindex,
4457 int sr_kf, int sr_arf) {
4458 // Use superres for Key-frames and Alt-ref frames only.
4459 const GF_GROUP *gf_group = &cpi->gf_group;
4460 if (gf_group->update_type[gf_group->index] != KF_UPDATE &&
4461 gf_group->update_type[gf_group->index] != ARF_UPDATE) {
4462 return SCALE_NUMERATOR;
4463 }
4464 if (gf_group->update_type[gf_group->index] == KF_UPDATE && !sr_kf) {
4465 return SCALE_NUMERATOR;
4466 }
4467 if (gf_group->update_type[gf_group->index] == ARF_UPDATE && !sr_arf) {
4468 return SCALE_NUMERATOR;
4469 }
4470
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004471 double energy[16];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004472 analyze_hor_freq(cpi, energy);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004473
4474 const double energy_by_q2_thresh =
4475 get_energy_by_q2_thresh(gf_group, &cpi->rc);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004476 int denom = get_superres_denom_from_qindex_energy(
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004477 qindex, energy, energy_by_q2_thresh, SUPERRES_ENERGY_BY_AC_THRESH);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004478 /*
4479 printf("\nenergy = [");
4480 for (int k = 1; k < 16; ++k) printf("%f, ", energy[k]);
4481 printf("]\n");
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004482 printf("boost = %d\n",
4483 (gf_group->update_type[gf_group->index] == KF_UPDATE)
4484 ? cpi->rc.kf_boost
4485 : cpi->rc.gfu_boost);
4486 printf("denom = %d\n", denom);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004487 */
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004488#if CONFIG_SUPERRES_IN_RECODE
4489 if (superres_in_recode_allowed(cpi)) {
4490 // Force superres to be tried in the recode loop, as full-res is also going
4491 // to be tried anyway.
4492 denom = AOMMAX(denom, SCALE_NUMERATOR + 1);
4493 }
4494#endif // CONFIG_SUPERRES_IN_RECODE
Urvang Joshi510d8f62019-01-10 12:11:50 -08004495 return denom;
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004496}
4497
Urvang Joshibc82d382019-11-01 17:59:20 -07004498// If true, SUPERRES_AUTO mode will exhaustively search over all superres
4499// denominators for all frames (except overlay and internal overlay frames).
4500#define SUPERRES_RECODE_ALL_RATIOS 0
4501
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004502static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
4503 // Choose an arbitrary random number
4504 static unsigned int seed = 34567;
4505 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Mufaddal Chakerae7326122019-12-04 14:49:09 +05304506 if (is_stat_generation_stage(cpi)) return SCALE_NUMERATOR;
Urvang Joshide71d142017-10-05 12:12:15 -07004507 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07004508
4509 // Make sure that superres mode of the frame is consistent with the
4510 // sequence-level flag.
4511 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
4512 cpi->common.seq_params.enable_superres));
4513 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
4514 oxcf->superres_mode == SUPERRES_NONE));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004515
4516 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004517 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004518 case SUPERRES_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00004519 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004520 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004521 else
Urvang Joshide71d142017-10-05 12:12:15 -07004522 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004523 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004524 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004525 case SUPERRES_QTHRESH: {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07004526 // Do not use superres when screen content tools are used.
Urvang Joshib6409e92020-03-23 11:23:27 -07004527 if (cpi->common.features.allow_screen_content_tools) break;
Debargha Mukherjee2b2c5fd2018-11-14 13:21:24 -08004528 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4529 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004530
4531 // Now decide the use of superres based on 'q'.
Urvang Joshi2c92b072018-03-19 17:23:31 -07004532 int bottom_index, top_index;
4533 const int q = av1_rc_pick_q_and_bounds(
Angie Chiang958904c2019-09-19 16:03:35 -07004534 cpi, &cpi->rc, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
Jingning Han2e029872019-08-02 10:54:19 -07004535 &bottom_index, &top_index);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004536
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004537 const int qthresh = (frame_is_intra_only(&cpi->common))
4538 ? oxcf->superres_kf_qthresh
4539 : oxcf->superres_qthresh;
Urvang Joshib17e0a62019-01-11 16:11:54 -08004540 if (q <= qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07004541 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004542 } else {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004543 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004544 }
4545 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004546 }
Urvang Joshi36a83732019-01-31 15:31:57 -08004547 case SUPERRES_AUTO: {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004548 // Do not use superres when screen content tools are used.
Urvang Joshib6409e92020-03-23 11:23:27 -07004549 if (cpi->common.features.allow_screen_content_tools) break;
Urvang Joshi510d8f62019-01-10 12:11:50 -08004550 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4551 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi36a83732019-01-31 15:31:57 -08004552
4553 // Now decide the use of superres based on 'q'.
4554 int bottom_index, top_index;
4555 const int q = av1_rc_pick_q_and_bounds(
Angie Chiang958904c2019-09-19 16:03:35 -07004556 cpi, &cpi->rc, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
Jingning Han2e029872019-08-02 10:54:19 -07004557 &bottom_index, &top_index);
Urvang Joshi36a83732019-01-31 15:31:57 -08004558
4559 const int qthresh = 128;
4560 if (q <= qthresh) {
4561 new_denom = SCALE_NUMERATOR;
4562 } else {
Urvang Joshibc82d382019-11-01 17:59:20 -07004563#if SUPERRES_RECODE_ALL_RATIOS
4564 if (cpi->common.current_frame.frame_type == KEY_FRAME)
4565 new_denom = oxcf->superres_kf_scale_denominator;
4566 else
4567 new_denom = oxcf->superres_scale_denominator;
4568#else
Urvang Joshi510d8f62019-01-10 12:11:50 -08004569 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Urvang Joshibc82d382019-11-01 17:59:20 -07004570#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi36a83732019-01-31 15:31:57 -08004571 }
4572 break;
4573 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004574 default: assert(0);
4575 }
Urvang Joshide71d142017-10-05 12:12:15 -07004576 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004577}
4578
Urvang Joshide71d142017-10-05 12:12:15 -07004579static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4580 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4581}
4582
4583static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08004584 // Only need to check the width, as scaling is horizontal only.
4585 (void)oheight;
4586 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07004587}
4588
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004589static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004590 SUPERRES_MODE superres_mode, int owidth,
4591 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004592 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004593 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004594 }
4595
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004596 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07004597 int resize_denom =
4598 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4599 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4600
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004601 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004602 // Alter superres scale as needed to enforce conformity.
4603 rsz->superres_denom =
4604 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4605 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4606 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004607 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004608 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004609 // Alter resize scale as needed to enforce conformity.
4610 resize_denom =
4611 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004612 rsz->resize_width = owidth;
4613 rsz->resize_height = oheight;
4614 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004615 resize_denom);
4616 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4617 if (resize_denom > SCALE_NUMERATOR) {
4618 --resize_denom;
4619 rsz->resize_width = owidth;
4620 rsz->resize_height = oheight;
4621 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4622 resize_denom);
4623 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004624 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004625 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004626 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004627 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004628 if (resize_denom > rsz->superres_denom)
4629 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004630 else
Urvang Joshide71d142017-10-05 12:12:15 -07004631 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004632 rsz->resize_width = owidth;
4633 rsz->resize_height = oheight;
4634 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004635 resize_denom);
4636 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4637 (resize_denom > SCALE_NUMERATOR ||
4638 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08004639 } else { // We are allowed to alter neither resize scale nor superres
4640 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004641 return 0;
4642 }
Urvang Joshide71d142017-10-05 12:12:15 -07004643 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004644}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004645
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004646// Calculates resize and superres params for next frame
David Turner475a3132019-01-18 15:17:17 +00004647static size_params_type calculate_next_size_params(AV1_COMP *cpi) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004648 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08004649 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshi510d8f62019-01-10 12:11:50 -08004650 int resize_denom = SCALE_NUMERATOR;
Mufaddal Chakera3bcc72c2019-12-11 14:38:37 +05304651 if (has_no_stats_stage(cpi) && cpi->use_svc &&
Marco Paniconi63971322019-08-15 21:32:05 -07004652 cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1) {
4653 rsz.resize_width = cpi->common.width;
4654 rsz.resize_height = cpi->common.height;
4655 return rsz;
4656 }
Mufaddal Chakerae7326122019-12-04 14:49:09 +05304657 if (is_stat_generation_stage(cpi)) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004658 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4659 rsz.resize_width = cpi->resize_pending_width;
4660 rsz.resize_height = cpi->resize_pending_height;
4661 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4662 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004663 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004664 rsz.resize_width = cpi->oxcf.width;
4665 rsz.resize_height = cpi->oxcf.height;
4666 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004667 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004668 }
Urvang Joshide71d142017-10-05 12:12:15 -07004669 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004670 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
4671 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004672 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004673 return rsz;
4674}
4675
Urvang Joshi22b150b2019-01-10 14:32:32 -08004676static void setup_frame_size_from_params(AV1_COMP *cpi,
4677 const size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004678 int encode_width = rsz->resize_width;
4679 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004680
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004681 AV1_COMMON *cm = &cpi->common;
4682 cm->superres_upscaled_width = encode_width;
4683 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004684 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004685 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4686 rsz->superres_denom);
David Turner475a3132019-01-18 15:17:17 +00004687 av1_set_frame_size(cpi, encode_width, encode_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004688}
4689
David Turnerdedd8ff2019-01-23 13:59:46 +00004690void av1_setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004691 AV1_COMMON *cm = &cpi->common;
Urvang Joshi22b150b2019-01-10 14:32:32 -08004692 // Reset superres params from previous frame.
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004693 cm->superres_scale_denominator = SCALE_NUMERATOR;
David Turner475a3132019-01-18 15:17:17 +00004694 const size_params_type rsz = calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004695 setup_frame_size_from_params(cpi, &rsz);
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004696
Yaowu Xu7e450882019-04-30 15:09:18 -07004697 assert(av1_is_min_tile_width_satisfied(cm));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004698}
4699
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004700static void superres_post_encode(AV1_COMP *cpi) {
4701 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004702 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004703
Cheng Chen09c83a52018-06-05 12:27:36 -07004704 if (!av1_superres_scaled(cm)) return;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004705
Urvang Joshid6b5d512018-03-20 13:34:38 -07004706 assert(cpi->oxcf.enable_superres);
4707 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshib6409e92020-03-23 11:23:27 -07004708 assert(!cm->features.all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07004709
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004710 av1_superres_upscale(cm, NULL);
4711
4712 // If regular resizing is occurring the source will need to be downscaled to
4713 // match the upscaled superres resolution. Otherwise the original source is
4714 // used.
Cheng Chen09c83a52018-06-05 12:27:36 -07004715 if (!av1_resize_scaled(cm)) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004716 cpi->source = cpi->unscaled_source;
4717 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4718 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004719 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4720 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08004721 // Do downscale. cm->(width|height) has been updated by
4722 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004723 if (aom_realloc_frame_buffer(
4724 &cpi->scaled_source, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004725 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4726 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07004727 AOM_BORDER_IN_PIXELS, cm->features.byte_alignment, NULL, NULL,
4728 NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004729 aom_internal_error(
4730 &cm->error, AOM_CODEC_MEM_ERROR,
4731 "Failed to reallocate scaled source buffer for superres");
4732 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4733 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004734 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004735 (int)cm->seq_params.bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004736 cpi->source = &cpi->scaled_source;
4737 }
4738}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004739
Yue Chen92271e12019-07-09 16:28:02 -07004740static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
4741 MACROBLOCKD *xd, int use_restoration,
4742 int use_cdef) {
logangwf95c9162019-02-20 12:02:32 -08004743 if (use_restoration)
David Turnerc29e1a92018-12-06 14:10:14 +00004744 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004745
logangwf95c9162019-02-20 12:02:32 -08004746 if (use_cdef) {
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004747#if CONFIG_COLLECT_COMPONENT_TIMING
4748 start_timing(cpi, cdef_time);
4749#endif
Steinar Midtskogen59782122017-07-20 08:49:43 +02004750 // Find CDEF parameters
David Turnerc29e1a92018-12-06 14:10:14 +00004751 av1_cdef_search(&cm->cur_frame->buf, cpi->source, cm, xd,
chiyotsaid2f7b412019-12-18 10:16:42 -08004752 cpi->sf.lpf_sf.cdef_pick_method, cpi->td.mb.rdmult);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004753
4754 // Apply the filter
David Turnerc29e1a92018-12-06 14:10:14 +00004755 av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004756#if CONFIG_COLLECT_COMPONENT_TIMING
4757 end_timing(cpi, cdef_time);
4758#endif
logangwf95c9162019-02-20 12:02:32 -08004759 } else {
4760 cm->cdef_info.cdef_bits = 0;
4761 cm->cdef_info.cdef_strengths[0] = 0;
4762 cm->cdef_info.nb_cdef_strengths = 1;
4763 cm->cdef_info.cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004764 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004765
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004766 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004767
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004768#if CONFIG_COLLECT_COMPONENT_TIMING
4769 start_timing(cpi, loop_restoration_time);
4770#endif
logangwf95c9162019-02-20 12:02:32 -08004771 if (use_restoration) {
David Turnerc29e1a92018-12-06 14:10:14 +00004772 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004773 av1_pick_filter_restoration(cpi->source, cpi);
4774 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4775 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4776 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304777 if (cpi->num_workers > 1)
David Turnerc29e1a92018-12-06 14:10:14 +00004778 av1_loop_restoration_filter_frame_mt(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304779 cpi->workers, cpi->num_workers,
4780 &cpi->lr_row_sync, &cpi->lr_ctxt);
4781 else
David Turnerc29e1a92018-12-06 14:10:14 +00004782 av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304783 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004784 }
logangwf95c9162019-02-20 12:02:32 -08004785 } else {
4786 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4787 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4788 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004789 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004790#if CONFIG_COLLECT_COMPONENT_TIMING
4791 end_timing(cpi, loop_restoration_time);
4792#endif
Fergus Simpsonbc189932017-05-16 17:02:39 -07004793}
4794
Yue Chen92271e12019-07-09 16:28:02 -07004795static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
4796 const int num_planes = av1_num_planes(cm);
4797 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4798
4799 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
Urvang Joshib6409e92020-03-23 11:23:27 -07004800 cm->features.coded_lossless && cm->features.all_lossless));
Yue Chen92271e12019-07-09 16:28:02 -07004801
Urvang Joshib6409e92020-03-23 11:23:27 -07004802 const int use_loopfilter =
Urvang Joshi54ffae72020-03-23 13:37:10 -07004803 !cm->features.coded_lossless && !cm->tiles.large_scale;
Urvang Joshib6409e92020-03-23 11:23:27 -07004804 const int use_cdef = cm->seq_params.enable_cdef &&
Urvang Joshi54ffae72020-03-23 13:37:10 -07004805 !cm->features.coded_lossless && !cm->tiles.large_scale;
Yue Chen92271e12019-07-09 16:28:02 -07004806 const int use_restoration = cm->seq_params.enable_restoration &&
Urvang Joshib6409e92020-03-23 11:23:27 -07004807 !cm->features.all_lossless &&
Urvang Joshi54ffae72020-03-23 13:37:10 -07004808 !cm->tiles.large_scale;
Yue Chen92271e12019-07-09 16:28:02 -07004809
4810 struct loopfilter *lf = &cm->lf;
4811
4812#if CONFIG_COLLECT_COMPONENT_TIMING
4813 start_timing(cpi, loop_filter_time);
4814#endif
4815 if (use_loopfilter) {
4816 aom_clear_system_state();
chiyotsaid2f7b412019-12-18 10:16:42 -08004817 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_sf.lpf_pick);
Yue Chen92271e12019-07-09 16:28:02 -07004818 } else {
4819 lf->filter_level[0] = 0;
4820 lf->filter_level[1] = 0;
4821 }
4822
4823 if (lf->filter_level[0] || lf->filter_level[1]) {
4824 if (cpi->num_workers > 1)
4825 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
4826#if CONFIG_LPF_MASK
4827 0,
4828#endif
4829 cpi->workers, cpi->num_workers,
4830 &cpi->lf_row_sync);
4831 else
4832 av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
4833#if CONFIG_LPF_MASK
4834 0,
4835#endif
4836 0, num_planes, 0);
4837 }
4838#if CONFIG_COLLECT_COMPONENT_TIMING
4839 end_timing(cpi, loop_filter_time);
4840#endif
4841
4842 cdef_restoration_frame(cpi, cm, xd, use_restoration, use_cdef);
4843}
4844
David Turnerf2b334c2018-12-13 13:00:55 +00004845static void fix_interp_filter(InterpFilter *const interp_filter,
4846 const FRAME_COUNTS *const counts) {
4847 if (*interp_filter == SWITCHABLE) {
4848 // Check to see if only one of the filters is actually used
4849 int count[SWITCHABLE_FILTERS] = { 0 };
4850 int num_filters_used = 0;
4851 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4852 for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
4853 count[i] += counts->switchable_interp[j][i];
4854 num_filters_used += (count[i] > 0);
4855 }
4856 if (num_filters_used == 1) {
4857 // Only one filter is used. So set the filter at frame level
4858 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4859 if (count[i]) {
4860 if (i == EIGHTTAP_REGULAR) *interp_filter = i;
4861 break;
4862 }
4863 }
4864 }
4865 }
4866}
4867
David Turner996b2c12018-12-07 15:52:30 +00004868static void finalize_encoded_frame(AV1_COMP *const cpi) {
4869 AV1_COMMON *const cm = &cpi->common;
David Turner99e990e2018-12-10 12:54:26 +00004870 CurrentFrame *const current_frame = &cm->current_frame;
David Turner996b2c12018-12-07 15:52:30 +00004871
David Turner99e990e2018-12-10 12:54:26 +00004872 if (!cm->seq_params.reduced_still_picture_hdr &&
4873 encode_show_existing_frame(cm)) {
4874 RefCntBuffer *const frame_to_show =
4875 cm->ref_frame_map[cpi->existing_fb_idx_to_show];
4876
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004877 if (frame_to_show == NULL) {
David Turner99e990e2018-12-10 12:54:26 +00004878 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4879 "Buffer does not contain a reconstructed frame");
4880 }
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004881 assert(frame_to_show->ref_count > 0);
David Turner99e990e2018-12-10 12:54:26 +00004882 assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
David Turner99e990e2018-12-10 12:54:26 +00004883 }
David Turner08f909c2018-12-18 13:29:14 +00004884
4885 if (!encode_show_existing_frame(cm) &&
4886 cm->seq_params.film_grain_params_present &&
4887 (cm->show_frame || cm->showable_frame)) {
4888 // Copy the current frame's film grain params to the its corresponding
4889 // RefCntBuffer slot.
4890 cm->cur_frame->film_grain_params = cm->film_grain_params;
4891
4892 // We must update the parameters if this is not an INTER_FRAME
4893 if (current_frame->frame_type != INTER_FRAME)
4894 cm->cur_frame->film_grain_params.update_parameters = 1;
4895
4896 // Iterate the random seed for the next frame.
4897 cm->film_grain_params.random_seed += 3381;
4898 if (cm->film_grain_params.random_seed == 0)
4899 cm->film_grain_params.random_seed = 7391;
4900 }
David Turnerf2b334c2018-12-13 13:00:55 +00004901
4902 // Initialise all tiles' contexts from the global frame context
Urvang Joshi54ffae72020-03-23 13:37:10 -07004903 for (int tile_col = 0; tile_col < cm->tiles.cols; tile_col++) {
4904 for (int tile_row = 0; tile_row < cm->tiles.rows; tile_row++) {
4905 const int tile_idx = tile_row * cm->tiles.cols + tile_col;
David Turnerf2b334c2018-12-13 13:00:55 +00004906 cpi->tile_data[tile_idx].tctx = *cm->fc;
4907 }
4908 }
4909
Urvang Joshi6237b882020-03-26 15:02:26 -07004910 fix_interp_filter(&cm->features.interp_filter, cpi->td.counts);
David Turner996b2c12018-12-07 15:52:30 +00004911}
4912
Urvang Joshi57643372019-02-21 11:10:57 -08004913static int get_regulated_q_overshoot(AV1_COMP *const cpi, int q_low, int q_high,
4914 int top_index, int bottom_index) {
4915 const AV1_COMMON *const cm = &cpi->common;
4916 const RATE_CONTROL *const rc = &cpi->rc;
4917
4918 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4919
4920 int q_regulated =
4921 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4922 AOMMAX(q_high, top_index), cm->width, cm->height);
4923
4924 int retries = 0;
4925 while (q_regulated < q_low && retries < 10) {
4926 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4927 q_regulated =
4928 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4929 AOMMAX(q_high, top_index), cm->width, cm->height);
4930 retries++;
4931 }
4932 return q_regulated;
4933}
4934
4935static int get_regulated_q_undershoot(AV1_COMP *const cpi, int q_high,
4936 int top_index, int bottom_index) {
4937 const AV1_COMMON *const cm = &cpi->common;
4938 const RATE_CONTROL *const rc = &cpi->rc;
4939
4940 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4941 int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4942 top_index, cm->width, cm->height);
4943
4944 int retries = 0;
4945 while (q_regulated > q_high && retries < 10) {
4946 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4947 q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4948 top_index, cm->width, cm->height);
4949 retries++;
4950 }
4951 return q_regulated;
4952}
4953
David Turner2f3b5df2019-01-02 14:30:50 +00004954// Called after encode_with_recode_loop() has just encoded a frame and packed
4955// its bitstream. This function works out whether we under- or over-shot
4956// our bitrate target and adjusts q as appropriate. Also decides whether
4957// or not we should do another recode loop, indicated by *loop
Hui Suef139e12019-05-20 15:51:22 -07004958static void recode_loop_update_q(
4959 AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low,
4960 int *const q_high, const int top_index, const int bottom_index,
4961 int *const undershoot_seen, int *const overshoot_seen,
4962 int *const low_cr_seen, const int loop_at_this_size) {
David Turner2f3b5df2019-01-02 14:30:50 +00004963 AV1_COMMON *const cm = &cpi->common;
4964 RATE_CONTROL *const rc = &cpi->rc;
Debargha Mukherjee90b20752020-03-19 23:16:34 -07004965 *loop = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00004966
Hui Suef139e12019-05-20 15:51:22 -07004967 const int min_cr = cpi->oxcf.min_cr;
4968 if (min_cr > 0) {
4969 aom_clear_system_state();
4970 const double compression_ratio =
4971 av1_get_compression_ratio(cm, rc->projected_frame_size >> 3);
4972 const double target_cr = min_cr / 100.0;
4973 if (compression_ratio < target_cr) {
4974 *low_cr_seen = 1;
4975 if (*q < rc->worst_quality) {
4976 const double cr_ratio = target_cr / compression_ratio;
4977 const int projected_q = AOMMAX(*q + 1, (int)(*q * cr_ratio * cr_ratio));
4978 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->worst_quality);
4979 *q_low = AOMMAX(*q, *q_low);
4980 *q_high = AOMMAX(*q, *q_high);
4981 *loop = 1;
4982 }
4983 }
4984 if (*low_cr_seen) return;
4985 }
4986
4987 if (cpi->oxcf.rc_mode == AOM_Q) return;
4988
Hui Su01dfe032019-09-11 14:10:42 -07004989 const int last_q = *q;
David Turner2f3b5df2019-01-02 14:30:50 +00004990 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
4991 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4992 &frame_under_shoot_limit,
4993 &frame_over_shoot_limit);
4994 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4995
Hui Su01dfe032019-09-11 14:10:42 -07004996 if (cm->current_frame.frame_type == KEY_FRAME && rc->this_key_frame_forced &&
4997 rc->projected_frame_size < rc->max_frame_bandwidth) {
David Turner2f3b5df2019-01-02 14:30:50 +00004998 int64_t kf_err;
Hui Su01dfe032019-09-11 14:10:42 -07004999 const int64_t high_err_target = cpi->ambient_err;
5000 const int64_t low_err_target = cpi->ambient_err >> 1;
David Turner2f3b5df2019-01-02 14:30:50 +00005001
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005002#if CONFIG_AV1_HIGHBITDEPTH
David Turner2f3b5df2019-01-02 14:30:50 +00005003 if (cm->seq_params.use_highbitdepth) {
5004 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
5005 } else {
5006 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5007 }
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005008#else
5009 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5010#endif
David Turner2f3b5df2019-01-02 14:30:50 +00005011 // Prevent possible divide by zero error below for perfect KF
5012 kf_err += !kf_err;
5013
5014 // The key frame is not good enough or we can afford
5015 // to make it better without undue risk of popping.
5016 if ((kf_err > high_err_target &&
5017 rc->projected_frame_size <= frame_over_shoot_limit) ||
5018 (kf_err > low_err_target &&
5019 rc->projected_frame_size <= frame_under_shoot_limit)) {
5020 // Lower q_high
Hui Su01dfe032019-09-11 14:10:42 -07005021 *q_high = AOMMAX(*q - 1, *q_low);
David Turner2f3b5df2019-01-02 14:30:50 +00005022
5023 // Adjust Q
5024 *q = (int)((*q * high_err_target) / kf_err);
5025 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
5026 } else if (kf_err < low_err_target &&
5027 rc->projected_frame_size >= frame_under_shoot_limit) {
5028 // The key frame is much better than the previous frame
5029 // Raise q_low
Hui Su01dfe032019-09-11 14:10:42 -07005030 *q_low = AOMMIN(*q + 1, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00005031
5032 // Adjust Q
5033 *q = (int)((*q * low_err_target) / kf_err);
5034 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
5035 }
5036
5037 // Clamp Q to upper and lower limits:
5038 *q = clamp(*q, *q_low, *q_high);
Hui Su01dfe032019-09-11 14:10:42 -07005039 *loop = (*q != last_q);
5040 return;
5041 }
David Turner2f3b5df2019-01-02 14:30:50 +00005042
Hui Su01dfe032019-09-11 14:10:42 -07005043 if (recode_loop_test(cpi, frame_over_shoot_limit, frame_under_shoot_limit, *q,
5044 AOMMAX(*q_high, top_index), bottom_index)) {
David Turner2f3b5df2019-01-02 14:30:50 +00005045 // Is the projected frame size out of range and are we allowed
5046 // to attempt to recode.
David Turner2f3b5df2019-01-02 14:30:50 +00005047
5048 // Frame size out of permitted range:
5049 // Update correction factor & compute new Q to try...
5050 // Frame is too large
5051 if (rc->projected_frame_size > rc->this_frame_target) {
5052 // Special case if the projected size is > the max allowed.
Urvang Joshi7344d3e2019-07-25 17:24:57 -07005053 if (*q == *q_high &&
5054 rc->projected_frame_size >= rc->max_frame_bandwidth) {
5055 const double q_val_high_current =
5056 av1_convert_qindex_to_q(*q_high, cm->seq_params.bit_depth);
5057 const double q_val_high_new =
5058 q_val_high_current *
5059 ((double)rc->projected_frame_size / rc->max_frame_bandwidth);
5060 *q_high = av1_find_qindex(q_val_high_new, cm->seq_params.bit_depth,
5061 rc->best_quality, rc->worst_quality);
5062 }
David Turner2f3b5df2019-01-02 14:30:50 +00005063
5064 // Raise Qlow as to at least the current value
Hui Su01dfe032019-09-11 14:10:42 -07005065 *q_low = AOMMIN(*q + 1, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00005066
Urvang Joshi57643372019-02-21 11:10:57 -08005067 if (*undershoot_seen || loop_at_this_size > 2 ||
5068 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00005069 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
5070
5071 *q = (*q_high + *q_low + 1) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08005072 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
5073 const int q_mid = (*q_high + *q_low + 1) / 2;
5074 const int q_regulated = get_regulated_q_overshoot(
5075 cpi, *q_low, *q_high, top_index, bottom_index);
5076 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
5077 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
5078 *q = (q_mid + q_regulated + 1) / 2;
David Turner2f3b5df2019-01-02 14:30:50 +00005079 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08005080 *q = get_regulated_q_overshoot(cpi, *q_low, *q_high, top_index,
5081 bottom_index);
David Turner2f3b5df2019-01-02 14:30:50 +00005082 }
5083
5084 *overshoot_seen = 1;
5085 } else {
5086 // Frame is too small
Hui Su01dfe032019-09-11 14:10:42 -07005087 *q_high = AOMMAX(*q - 1, *q_low);
David Turner2f3b5df2019-01-02 14:30:50 +00005088
Urvang Joshi57643372019-02-21 11:10:57 -08005089 if (*overshoot_seen || loop_at_this_size > 2 ||
5090 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00005091 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
5092 *q = (*q_high + *q_low) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08005093 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
5094 const int q_mid = (*q_high + *q_low) / 2;
5095 const int q_regulated =
5096 get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
5097 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
5098 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
5099 *q = (q_mid + q_regulated) / 2;
5100
5101 // Special case reset for qlow for constrained quality.
5102 // This should only trigger where there is very substantial
5103 // undershoot on a frame and the auto cq level is above
5104 // the user passsed in value.
5105 if (cpi->oxcf.rc_mode == AOM_CQ && q_regulated < *q_low) {
5106 *q_low = *q;
5107 }
David Turner2f3b5df2019-01-02 14:30:50 +00005108 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08005109 *q = get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
5110
David Turner2f3b5df2019-01-02 14:30:50 +00005111 // Special case reset for qlow for constrained quality.
5112 // This should only trigger where there is very substantial
5113 // undershoot on a frame and the auto cq level is above
5114 // the user passsed in value.
5115 if (cpi->oxcf.rc_mode == AOM_CQ && *q < *q_low) {
5116 *q_low = *q;
5117 }
David Turner2f3b5df2019-01-02 14:30:50 +00005118 }
5119
5120 *undershoot_seen = 1;
5121 }
5122
5123 // Clamp Q to upper and lower limits:
5124 *q = clamp(*q, *q_low, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00005125 }
Hui Su01dfe032019-09-11 14:10:42 -07005126
5127 *loop = (*q != last_q);
David Turner2f3b5df2019-01-02 14:30:50 +00005128}
5129
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305130static int get_interp_filter_selected(const AV1_COMMON *const cm,
5131 MV_REFERENCE_FRAME ref,
5132 InterpFilter ifilter) {
5133 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
5134 if (buf == NULL) return 0;
5135 return buf->interp_filter_selected[ifilter];
5136}
5137
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305138static uint16_t setup_interp_filter_search_mask(AV1_COMP *cpi) {
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305139 const AV1_COMMON *const cm = &cpi->common;
5140 int ref_total[REF_FRAMES] = { 0 };
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305141 uint16_t mask = ALLOW_ALL_INTERP_FILT_MASK;
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305142
Urvang Joshi1de67aa2020-03-20 11:21:57 -07005143 if (cpi->last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305144 return mask;
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305145
5146 for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
5147 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
5148 ++ifilter) {
5149 ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter);
5150 }
5151 }
5152 int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] +
5153 ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] +
5154 ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]);
5155
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305156 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
5157 ++ifilter) {
5158 int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30;
5159 if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) {
5160 int filter_score =
5161 get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 +
5162 get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 +
5163 get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 +
5164 get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 +
5165 get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 +
5166 get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10;
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305167 if (filter_score < ref_total_total) {
5168 DUAL_FILTER_TYPE filt_type = ifilter + SWITCHABLE_FILTERS * ifilter;
5169 reset_interp_filter_allowed_mask(&mask, filt_type);
5170 }
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305171 }
5172 }
5173 return mask;
5174}
5175
Cheng Chen92a926a2020-01-30 17:57:33 -08005176#if !CONFIG_REALTIME_ONLY
chiyotsai155e2862020-03-09 15:29:34 -07005177#define STRICT_PSNR_DIFF_THRESH 0.9
Cheng Chen92a926a2020-01-30 17:57:33 -08005178// Encode key frame with/without screen content tools to determine whether
5179// screen content tools should be enabled for this key frame group or not.
5180// The first encoding is without screen content tools.
5181// The second encoding is with screen content tools.
5182// We compare the psnr and frame size to make the decision.
5183static void screen_content_tools_determination(
5184 AV1_COMP *cpi, const int allow_screen_content_tools_orig_decision,
5185 const int allow_intrabc_orig_decision,
5186 const int is_screen_content_type_orig_decision, const int pass,
5187 int *projected_size_pass, PSNR_STATS *psnr) {
5188 AV1_COMMON *const cm = &cpi->common;
Urvang Joshib6409e92020-03-23 11:23:27 -07005189 FeatureFlags *const features = &cm->features;
Cheng Chen92a926a2020-01-30 17:57:33 -08005190 projected_size_pass[pass] = cpi->rc.projected_frame_size;
5191#if CONFIG_AV1_HIGHBITDEPTH
5192 const uint32_t in_bit_depth = cpi->oxcf.input_bit_depth;
5193 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
5194 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr[pass],
5195 bit_depth, in_bit_depth);
5196#else
5197 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr[pass]);
5198#endif
5199 if (pass != 1) return;
5200
5201 const double psnr_diff = psnr[1].psnr[0] - psnr[0].psnr[0];
5202 const int is_sc_encoding_much_better = psnr_diff > STRICT_PSNR_DIFF_THRESH;
5203 if (is_sc_encoding_much_better) {
5204 // Use screen content tools, if we get coding gain.
Urvang Joshib6409e92020-03-23 11:23:27 -07005205 features->allow_screen_content_tools = 1;
5206 features->allow_intrabc = cpi->intrabc_used;
Cheng Chen92a926a2020-01-30 17:57:33 -08005207 cpi->is_screen_content_type = 1;
5208 } else {
5209 // Use original screen content decision.
Urvang Joshib6409e92020-03-23 11:23:27 -07005210 features->allow_screen_content_tools =
5211 allow_screen_content_tools_orig_decision;
5212 features->allow_intrabc = allow_intrabc_orig_decision;
Cheng Chen92a926a2020-01-30 17:57:33 -08005213 cpi->is_screen_content_type = is_screen_content_type_orig_decision;
5214 }
5215}
5216
Cheng Chenb8c938b2020-03-23 11:30:18 -07005217// Set some encoding parameters to make the encoding process fast.
5218// A fixed block partition size, and a large q is used.
5219static void set_encoding_params_for_screen_content(AV1_COMP *cpi,
5220 const int pass) {
Cheng Chen92a926a2020-01-30 17:57:33 -08005221 AV1_COMMON *const cm = &cpi->common;
5222 if (pass == 0) {
5223 // In the first pass, encode without screen content tools.
5224 // Use a high q, and a fixed block size for fast encoding.
Urvang Joshib6409e92020-03-23 11:23:27 -07005225 cm->features.allow_screen_content_tools = 0;
5226 cm->features.allow_intrabc = 0;
Cheng Chen92a926a2020-01-30 17:57:33 -08005227 cpi->is_screen_content_type = 0;
5228 cpi->sf.part_sf.partition_search_type = FIXED_PARTITION;
5229 cpi->sf.part_sf.always_this_block_size = BLOCK_32X32;
Cheng Chenb8c938b2020-03-23 11:30:18 -07005230 return;
Cheng Chen92a926a2020-01-30 17:57:33 -08005231 }
Cheng Chenb8c938b2020-03-23 11:30:18 -07005232 assert(pass == 1);
5233 // In the second pass, encode with screen content tools.
5234 // Use a high q, and a fixed block size for fast encoding.
Urvang Joshib6409e92020-03-23 11:23:27 -07005235 cm->features.allow_screen_content_tools = 1;
Cheng Chenb8c938b2020-03-23 11:30:18 -07005236 // TODO(chengchen): turn intrabc on could lead to data race issue.
5237 // cm->allow_intrabc = 1;
5238 cpi->is_screen_content_type = 1;
5239 cpi->sf.part_sf.partition_search_type = FIXED_PARTITION;
5240 cpi->sf.part_sf.always_this_block_size = BLOCK_32X32;
Cheng Chen92a926a2020-01-30 17:57:33 -08005241}
5242
5243// Determines whether to use screen content tools for the key frame group.
Urvang Joshib6409e92020-03-23 11:23:27 -07005244// This function modifies "cm->features.allow_screen_content_tools",
5245// "cm->features.allow_intrabc" and "cpi->is_screen_content_type".
Cheng Chen92a926a2020-01-30 17:57:33 -08005246static void determine_sc_tools_with_encoding(AV1_COMP *cpi, const int q_orig) {
5247 if (!is_stat_consumption_stage_twopass(cpi)) return;
5248
5249 AV1_COMMON *const cm = &cpi->common;
5250 // Variables to help determine if we should allow screen content tools.
5251 int projected_size_pass[3] = { 0 };
5252 PSNR_STATS psnr[3];
5253 const int is_key_frame = cm->current_frame.frame_type == KEY_FRAME;
5254 const int allow_screen_content_tools_orig_decision =
Urvang Joshib6409e92020-03-23 11:23:27 -07005255 cm->features.allow_screen_content_tools;
5256 const int allow_intrabc_orig_decision = cm->features.allow_intrabc;
Cheng Chen92a926a2020-01-30 17:57:33 -08005257 const int is_screen_content_type_orig_decision = cpi->is_screen_content_type;
5258 // Turn off the encoding trial for forward key frame and superres.
5259 if (cpi->sf.rt_sf.use_nonrd_pick_mode || cpi->oxcf.fwd_kf_enabled ||
5260 cpi->oxcf.superres_mode != SUPERRES_NONE || cpi->oxcf.mode == REALTIME ||
5261 is_screen_content_type_orig_decision || !is_key_frame) {
5262 return;
5263 }
5264
5265 // TODO(chengchen): multiple encoding for the lossless mode is time consuming.
5266 // Find a better way to determine whether screen content tools should be used
5267 // for lossless coding.
5268 // Use a high q and a fixed partition to do quick encoding.
5269 const int q_for_screen_content_quick_run =
5270 is_lossless_requested(&cpi->oxcf) ? q_orig : AOMMAX(q_orig, 244);
5271 const int partition_search_type_orig = cpi->sf.part_sf.partition_search_type;
5272 const BLOCK_SIZE fixed_partition_block_size_orig =
5273 cpi->sf.part_sf.always_this_block_size;
5274
5275 // Setup necessary params for encoding, including frame source, etc.
chiyotsaie6ae98e2020-03-23 13:17:08 -07005276 aom_clear_system_state();
Cheng Chen92a926a2020-01-30 17:57:33 -08005277
chiyotsaie6ae98e2020-03-23 13:17:08 -07005278 cpi->source =
5279 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
5280 if (cpi->unscaled_last_source != NULL) {
5281 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
5282 &cpi->scaled_last_source);
Cheng Chen92a926a2020-01-30 17:57:33 -08005283 }
5284
chiyotsaie6ae98e2020-03-23 13:17:08 -07005285 setup_frame(cpi);
5286
5287 if (cm->seg.enabled) {
5288 if (!cm->seg.update_data && cm->prev_frame) {
5289 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
5290 cm->seg.enabled = cm->prev_frame->seg.enabled;
5291 } else {
5292 av1_calculate_segdata(&cm->seg);
5293 }
5294 } else {
5295 memset(&cm->seg, 0, sizeof(cm->seg));
5296 }
5297 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
5298 cm->cur_frame->seg.enabled = cm->seg.enabled;
5299
Cheng Chenb8c938b2020-03-23 11:30:18 -07005300 // The two encoding passes aim to help determine whether to use screen
5301 // content tools, with a high q and fixed partition.
Cheng Chen92a926a2020-01-30 17:57:33 -08005302 for (int pass = 0; pass < 2; ++pass) {
Cheng Chenb8c938b2020-03-23 11:30:18 -07005303 set_encoding_params_for_screen_content(cpi, pass);
Cheng Chen92a926a2020-01-30 17:57:33 -08005304#if CONFIG_TUNE_VMAF
5305 if (cpi->oxcf.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING ||
5306 cpi->oxcf.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
5307 cpi->oxcf.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
5308 av1_set_quantizer(
Urvang Joshi1de67aa2020-03-20 11:21:57 -07005309 cpi, av1_get_vmaf_base_qindex(cpi, q_for_screen_content_quick_run));
Cheng Chen92a926a2020-01-30 17:57:33 -08005310 } else {
5311#endif
Urvang Joshi1de67aa2020-03-20 11:21:57 -07005312 av1_set_quantizer(cpi, q_for_screen_content_quick_run);
Cheng Chen92a926a2020-01-30 17:57:33 -08005313#if CONFIG_TUNE_VMAF
5314 }
5315#endif
Debargha Mukherjee90b20752020-03-19 23:16:34 -07005316 av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
Cheng Chen92a926a2020-01-30 17:57:33 -08005317 if (cpi->oxcf.deltaq_mode != NO_DELTA_Q) av1_init_quantizer(cpi);
5318
5319 av1_set_variance_partition_thresholds(cpi, q_for_screen_content_quick_run,
5320 0);
5321 // transform / motion compensation build reconstruction frame
5322 av1_encode_frame(cpi);
5323 // Screen content decision
5324 screen_content_tools_determination(
5325 cpi, allow_screen_content_tools_orig_decision,
5326 allow_intrabc_orig_decision, is_screen_content_type_orig_decision, pass,
5327 projected_size_pass, psnr);
5328 }
5329
5330 // Set partition speed feature back.
5331 cpi->sf.part_sf.partition_search_type = partition_search_type_orig;
5332 cpi->sf.part_sf.always_this_block_size = fixed_partition_block_size_orig;
5333}
5334#endif // CONFIG_REALTIME_ONLY
5335
Tom Finegane4099e32018-01-23 12:01:51 -08005336static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005337 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005338 RATE_CONTROL *const rc = &cpi->rc;
chiyotsai6b430132019-12-18 10:33:51 -08005339 const int allow_recode = (cpi->sf.hl_sf.recode_loop != DISALLOW_RECODE);
Hui Sua1d71842019-07-31 12:02:24 -07005340 // Must allow recode if minimum compression ratio is set.
5341 assert(IMPLIES(cpi->oxcf.min_cr > 0, allow_recode));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005342
5343 set_size_independent_vars(cpi);
Hamsalekha S37cc1d12019-12-12 19:27:41 +05305344 if (is_stat_consumption_stage_twopass(cpi) &&
chiyotsaicbf1d112019-12-16 11:28:47 -08005345 cpi->sf.interp_sf.adaptive_interp_filter_search)
Yunqing Wangc3ab9272019-11-01 17:43:58 -07005346 cpi->interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07005347 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07005348
David Turnerdedd8ff2019-01-23 13:59:46 +00005349 av1_setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005350
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005351#if CONFIG_SUPERRES_IN_RECODE
5352 if (superres_in_recode_allowed(cpi) &&
5353 cm->superres_scale_denominator == SCALE_NUMERATOR) {
5354 // Superres won't be picked, so no need to try, as we will go through
5355 // another recode loop for full-resolution after this anyway.
5356 return -1;
5357 }
5358#endif // CONFIG_SUPERRES_IN_RECODE
5359
David Turner2f3b5df2019-01-02 14:30:50 +00005360 int top_index = 0, bottom_index = 0;
5361 int q = 0, q_low = 0, q_high = 0;
5362 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
5363 q_low = bottom_index;
5364 q_high = top_index;
Fyodor Kyslov166648a2020-03-18 16:15:27 -07005365 if (cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION) {
5366 const int num_64x64_blocks =
5367 (cm->seq_params.sb_size == BLOCK_64X64) ? 1 : 4;
5368 if (cpi->td.vt64x64) {
5369 if (num_64x64_blocks != cpi->td.num_64x64_blocks) {
5370 aom_free(cpi->td.vt64x64);
5371 cpi->td.vt64x64 = NULL;
5372 }
5373 }
5374 if (!cpi->td.vt64x64) {
5375 CHECK_MEM_ERROR(cm, cpi->td.vt64x64,
5376 aom_malloc(sizeof(*cpi->td.vt64x64) * num_64x64_blocks));
5377 cpi->td.num_64x64_blocks = num_64x64_blocks;
5378 }
5379 }
David Turner2f3b5df2019-01-02 14:30:50 +00005380
chiyotsaidcc2e012019-12-17 10:29:32 -08005381 if (cpi->sf.tx_sf.tx_type_search.prune_tx_type_using_stats &&
Yunqing Wang80a7af62019-08-22 16:07:44 -07005382 cm->current_frame.frame_type == KEY_FRAME) {
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005383 av1_copy(cpi->tx_type_probs, default_tx_type_probs);
5384
Yunqing Wang2348c032020-01-09 18:05:49 -08005385 const int thr[2][2] = { { 15, 10 }, { 17, 10 } };
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005386 for (int f = 0; f < FRAME_UPDATE_TYPES; f++) {
Yunqing Wangeb7c2182019-08-22 18:15:48 -07005387 int kf_arf_update = (f == KF_UPDATE || f == ARF_UPDATE);
5388 cpi->tx_type_probs_thresh[f] =
chiyotsaidcc2e012019-12-17 10:29:32 -08005389 thr[cpi->sf.tx_sf.tx_type_search.prune_tx_type_using_stats - 1]
Yunqing Wangeb7c2182019-08-22 18:15:48 -07005390 [kf_arf_update];
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005391 }
5392 }
5393
chiyotsai868dc902019-12-12 15:32:52 -08005394 if (!cpi->sf.inter_sf.disable_obmc &&
5395 cpi->sf.inter_sf.prune_obmc_prob_thresh > 0 &&
Yunqing Wang5f74dc22019-10-29 10:35:20 -07005396 cm->current_frame.frame_type == KEY_FRAME) {
5397 av1_copy(cpi->obmc_probs, default_obmc_probs);
Yunqing Wang5f74dc22019-10-29 10:35:20 -07005398 }
chiyotsai868dc902019-12-12 15:32:52 -08005399 if (cpi->sf.inter_sf.prune_warped_prob_thresh > 0 &&
Yunqing Wanga09a46b2019-12-09 13:16:24 -08005400 cm->current_frame.frame_type == KEY_FRAME) {
5401 av1_copy(cpi->warped_probs, default_warped_probs);
5402 }
Yunqing Wang5f74dc22019-10-29 10:35:20 -07005403
Yunqing Wang2348c032020-01-09 18:05:49 -08005404 if (cpi->sf.interp_sf.adaptive_interp_filter_search == 2 &&
5405 cm->current_frame.frame_type == KEY_FRAME) {
5406 av1_copy(cpi->switchable_interp_probs, default_switchable_interp_probs);
5407
5408 const int thr[7] = { 0, 8, 8, 8, 8, 0, 8 };
5409 for (int f = 0; f < FRAME_UPDATE_TYPES; f++) {
5410 cpi->switchable_interp_thresh[f] = thr[f];
5411 }
5412 }
5413
Cheng Chen92a926a2020-01-30 17:57:33 -08005414#if !CONFIG_REALTIME_ONLY
5415 // Determine whether to use screen content tools using two fast encoding.
5416 determine_sc_tools_with_encoding(cpi, q);
5417#endif // CONFIG_REALTIME_ONLY
5418
5419#if CONFIG_COLLECT_COMPONENT_TIMING
5420 printf("\n Encoding a frame:");
5421#endif
5422
David Turner2f3b5df2019-01-02 14:30:50 +00005423 // Loop variables
Cheng Chen92a926a2020-01-30 17:57:33 -08005424 int loop = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00005425 int loop_count = 0;
5426 int loop_at_this_size = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00005427 int overshoot_seen = 0;
5428 int undershoot_seen = 0;
Hui Suef139e12019-05-20 15:51:22 -07005429 int low_cr_seen = 0;
chiyotsai8b7cef82020-01-21 16:52:54 -08005430 int last_loop_allow_hp = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005431
Yaowu Xuc27fc142016-08-22 16:08:15 -07005432 do {
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005433 loop = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005434 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005435
Urvang Joshif1fa6862018-01-08 16:39:33 -08005436 // if frame was scaled calculate global_motion_search again if already
5437 // done
David Turner2f3b5df2019-01-02 14:30:50 +00005438 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005439 if (cpi->source->y_crop_width != cm->width ||
David Turner2f3b5df2019-01-02 14:30:50 +00005440 cpi->source->y_crop_height != cm->height) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005441 cpi->global_motion_search_done = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00005442 }
5443 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005444 cpi->source =
5445 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
David Turner2f3b5df2019-01-02 14:30:50 +00005446 if (cpi->unscaled_last_source != NULL) {
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07005447 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
5448 &cpi->scaled_last_source);
David Turner2f3b5df2019-01-02 14:30:50 +00005449 }
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07005450
David Turner2f3b5df2019-01-02 14:30:50 +00005451 if (!frame_is_intra_only(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005452 if (loop_count > 0) {
5453 release_scaled_references(cpi);
5454 }
Cheng Chen46f30c72017-09-07 11:13:33 -07005455 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005456 }
sdeng3cd9eec2020-01-23 15:49:50 -08005457#if CONFIG_TUNE_VMAF
5458 if (cpi->oxcf.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING ||
sdeng615dc242020-02-04 16:06:14 -08005459 cpi->oxcf.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
5460 cpi->oxcf.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
Urvang Joshi1de67aa2020-03-20 11:21:57 -07005461 av1_set_quantizer(cpi, av1_get_vmaf_base_qindex(cpi, q));
sdeng3cd9eec2020-01-23 15:49:50 -08005462 } else {
5463#endif
Urvang Joshi1de67aa2020-03-20 11:21:57 -07005464 av1_set_quantizer(cpi, q);
sdeng3cd9eec2020-01-23 15:49:50 -08005465#if CONFIG_TUNE_VMAF
5466 }
5467#endif
Debargha Mukherjee90b20752020-03-19 23:16:34 -07005468 av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
sdeng3cd9eec2020-01-23 15:49:50 -08005469
Fyodor Kyslov25daf5d2019-10-23 10:33:26 -07005470 if (cpi->oxcf.deltaq_mode != NO_DELTA_Q) av1_init_quantizer(cpi);
kyslov7b9d0d62018-12-21 11:12:26 -08005471
5472 av1_set_variance_partition_thresholds(cpi, q, 0);
5473
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08005474 // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
5475 // cm->current_frame.frame_number, cm->show_frame, q,
5476 // cm->current_frame.frame_type, cm->superres_scale_denominator);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005477
David Turner2f3b5df2019-01-02 14:30:50 +00005478 if (loop_count == 0) {
5479 setup_frame(cpi);
5480 } else if (get_primary_ref_frame_buf(cm) == NULL) {
5481 // Base q-index may have changed, so we need to assign proper default coef
5482 // probs before every iteration.
Yaowu Xuf883b422016-08-30 14:01:10 -07005483 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08005484 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00005485 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005486
Yaowu Xuc27fc142016-08-22 16:08:15 -07005487 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005488 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005489 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005490 av1_setup_in_frame_q_adj(cpi);
David Turner2f3b5df2019-01-02 14:30:50 +00005491 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !allow_recode) {
5492 suppress_active_map(cpi);
5493 av1_cyclic_refresh_setup(cpi);
5494 apply_active_map(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005495 }
David Turner2f3b5df2019-01-02 14:30:50 +00005496
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005497 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00005498 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005499 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
Jerome Jiangb23571d2019-10-23 14:15:55 -07005500 cm->seg.enabled = cm->prev_frame->seg.enabled;
David Barker11c93562018-06-05 12:00:07 +01005501 } else {
Yaowu Xu7e450882019-04-30 15:09:18 -07005502 av1_calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07005503 }
David Barkercab37552018-03-21 11:56:24 +00005504 } else {
5505 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005506 }
David Barkercab37552018-03-21 11:56:24 +00005507 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Jerome Jiangb23571d2019-10-23 14:15:55 -07005508 cm->cur_frame->seg.enabled = cm->seg.enabled;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005509
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005510#if CONFIG_COLLECT_COMPONENT_TIMING
5511 start_timing(cpi, av1_encode_frame_time);
5512#endif
chiyotsai8b7cef82020-01-21 16:52:54 -08005513 // Set the motion vector precision based on mv stats from the last coded
5514 // frame.
5515 if (!frame_is_intra_only(cm)) {
5516 av1_pick_and_set_high_precision_mv(cpi, q);
5517
5518 // If the precision has changed during different iteration of the loop,
5519 // then we need to reset the global motion vectors
Urvang Joshib6409e92020-03-23 11:23:27 -07005520 if (loop_count > 0 &&
5521 cm->features.allow_high_precision_mv != last_loop_allow_hp) {
chiyotsai8b7cef82020-01-21 16:52:54 -08005522 cpi->global_motion_search_done = 0;
5523 }
Urvang Joshib6409e92020-03-23 11:23:27 -07005524 last_loop_allow_hp = cm->features.allow_high_precision_mv;
chiyotsai8b7cef82020-01-21 16:52:54 -08005525 }
chiyotsaic666b1f2019-12-20 10:44:58 -08005526
Yaowu Xuc27fc142016-08-22 16:08:15 -07005527 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07005528 av1_encode_frame(cpi);
chiyotsai51bd6482019-12-20 10:49:34 -08005529#if !CONFIG_REALTIME_ONLY
5530 // Reset the mv_stats in case we are interrupted by an intraframe or an
5531 // overlay frame.
5532 if (cpi->mv_stats.valid) {
5533 av1_zero(cpi->mv_stats);
5534 }
5535 // Gather the mv_stats for the next frame
5536 if (cpi->sf.hl_sf.high_precision_mv_usage == LAST_MV_DATA &&
5537 av1_frame_allows_smart_mv(cpi)) {
5538 av1_collect_mv_stats(cpi, q);
5539 }
5540#endif // !CONFIG_REALTIME_ONLY
5541
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005542#if CONFIG_COLLECT_COMPONENT_TIMING
5543 end_timing(cpi, av1_encode_frame_time);
5544#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005545
Yaowu Xuf883b422016-08-30 14:01:10 -07005546 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005547
5548 // Dummy pack of the bitstream using up to date stats to get an
5549 // accurate estimate of output frame size to determine if we need
5550 // to recode.
chiyotsai6b430132019-12-18 10:33:51 -08005551 const int do_dummy_pack =
5552 (cpi->sf.hl_sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
5553 cpi->oxcf.rc_mode != AOM_Q) ||
5554 cpi->oxcf.min_cr > 0;
Hui Sua1d71842019-07-31 12:02:24 -07005555 if (do_dummy_pack) {
David Turner996b2c12018-12-07 15:52:30 +00005556 finalize_encoded_frame(cpi);
David Turner35cba132018-12-10 15:48:15 +00005557 int largest_tile_id = 0; // Output from bitstream: unused here
chiyotsaic666b1f2019-12-20 10:44:58 -08005558 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) !=
5559 AOM_CODEC_OK) {
Tom Finegane4099e32018-01-23 12:01:51 -08005560 return AOM_CODEC_ERROR;
chiyotsaic666b1f2019-12-20 10:44:58 -08005561 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005562
5563 rc->projected_frame_size = (int)(*size) << 3;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005564 }
5565
Hui Suef139e12019-05-20 15:51:22 -07005566 if (allow_recode) {
David Turner2f3b5df2019-01-02 14:30:50 +00005567 // Update q and decide whether to do a recode loop
5568 recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
5569 bottom_index, &undershoot_seen, &overshoot_seen,
Hui Suef139e12019-05-20 15:51:22 -07005570 &low_cr_seen, loop_at_this_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005571 }
5572
5573 // Special case for overlay frame.
Hui Su7e3eeaa2019-09-11 15:50:41 -07005574 if (loop && rc->is_src_frame_alt_ref &&
5575 rc->projected_frame_size < rc->max_frame_bandwidth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005576 loop = 0;
Hui Su7e3eeaa2019-09-11 15:50:41 -07005577 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005578
chiyotsai76cd2652019-12-18 10:26:36 -08005579 if (allow_recode && !cpi->sf.gm_sf.gm_disable_recode &&
David Turner2f3b5df2019-01-02 14:30:50 +00005580 recode_loop_test_global_motion(cpi)) {
5581 loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005582 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005583
Yaowu Xuc27fc142016-08-22 16:08:15 -07005584 if (loop) {
5585 ++loop_count;
5586 ++loop_at_this_size;
5587
5588#if CONFIG_INTERNAL_STATS
5589 ++cpi->tot_recode_hits;
5590#endif
5591 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005592#if CONFIG_COLLECT_COMPONENT_TIMING
5593 if (loop) printf("\n Recoding:");
5594#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005595 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08005596
Marco Paniconi574e59e2019-10-23 15:47:28 -07005597 // Update some stats from cyclic refresh.
Marco Paniconi51415512019-11-27 12:38:24 -08005598 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !frame_is_intra_only(cm))
Marco Paniconi574e59e2019-10-23 15:47:28 -07005599 av1_cyclic_refresh_postencode(cpi);
5600
Tom Finegane4099e32018-01-23 12:01:51 -08005601 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005602}
5603
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005604static int encode_with_recode_loop_and_filter(AV1_COMP *cpi, size_t *size,
5605 uint8_t *dest, int64_t *sse,
5606 int64_t *rate,
5607 int *largest_tile_id) {
5608#if CONFIG_COLLECT_COMPONENT_TIMING
5609 start_timing(cpi, encode_with_recode_loop_time);
5610#endif
5611 int err = encode_with_recode_loop(cpi, size, dest);
5612#if CONFIG_COLLECT_COMPONENT_TIMING
5613 end_timing(cpi, encode_with_recode_loop_time);
5614#endif
5615 if (err != AOM_CODEC_OK) {
5616 if (err == -1) {
5617 // special case as described in encode_with_recode_loop().
5618 // Encoding was skipped.
5619 err = AOM_CODEC_OK;
5620 if (sse != NULL) *sse = INT64_MAX;
5621 if (rate != NULL) *rate = INT64_MAX;
5622 *largest_tile_id = 0;
5623 }
5624 return err;
5625 }
5626
5627#ifdef OUTPUT_YUV_SKINMAP
5628 if (cpi->common.current_frame.frame_number > 1) {
5629 av1_compute_skin_map(cpi, yuv_skinmap_file);
5630 }
5631#endif // OUTPUT_YUV_SKINMAP
5632
5633 AV1_COMMON *const cm = &cpi->common;
5634 SequenceHeader *const seq_params = &cm->seq_params;
5635
5636 // Special case code to reduce pulsing when key frames are forced at a
5637 // fixed interval. Note the reconstruction error if it is the frame before
5638 // the force key frame
5639 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005640#if CONFIG_AV1_HIGHBITDEPTH
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005641 if (seq_params->use_highbitdepth) {
5642 cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
5643 } else {
5644 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5645 }
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005646#else
5647 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5648#endif
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005649 }
5650
5651 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
5652 cm->cur_frame->buf.transfer_characteristics =
5653 seq_params->transfer_characteristics;
5654 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
5655 cm->cur_frame->buf.monochrome = seq_params->monochrome;
5656 cm->cur_frame->buf.chroma_sample_position =
5657 seq_params->chroma_sample_position;
5658 cm->cur_frame->buf.color_range = seq_params->color_range;
5659 cm->cur_frame->buf.render_width = cm->render_width;
5660 cm->cur_frame->buf.render_height = cm->render_height;
5661
5662 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5663 // off.
5664
5665 // Pick the loop filter level for the frame.
Urvang Joshib6409e92020-03-23 11:23:27 -07005666 if (!cm->features.allow_intrabc) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005667 loopfilter_frame(cpi, cm);
5668 } else {
5669 cm->lf.filter_level[0] = 0;
5670 cm->lf.filter_level[1] = 0;
5671 cm->cdef_info.cdef_bits = 0;
5672 cm->cdef_info.cdef_strengths[0] = 0;
5673 cm->cdef_info.nb_cdef_strengths = 1;
5674 cm->cdef_info.cdef_uv_strengths[0] = 0;
5675 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5676 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5677 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
5678 }
5679
5680 // TODO(debargha): Fix mv search range on encoder side
5681 // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5682 aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5683
5684#ifdef OUTPUT_YUV_REC
5685 aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
5686#endif
5687
5688 finalize_encoded_frame(cpi);
5689 // Build the bitstream
5690#if CONFIG_COLLECT_COMPONENT_TIMING
5691 start_timing(cpi, av1_pack_bitstream_final_time);
5692#endif
5693 if (av1_pack_bitstream(cpi, dest, size, largest_tile_id) != AOM_CODEC_OK)
5694 return AOM_CODEC_ERROR;
5695#if CONFIG_COLLECT_COMPONENT_TIMING
5696 end_timing(cpi, av1_pack_bitstream_final_time);
5697#endif
5698
5699 // Compute sse and rate.
5700 if (sse != NULL) {
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005701#if CONFIG_AV1_HIGHBITDEPTH
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005702 *sse = (seq_params->use_highbitdepth)
5703 ? aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf)
5704 : aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005705#else
5706 *sse = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5707#endif
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005708 }
5709 if (rate != NULL) {
5710 const int64_t bits = (*size << 3);
5711 *rate = (bits << 5); // To match scale.
5712 }
5713 return AOM_CODEC_OK;
5714}
5715
5716#if CONFIG_SUPERRES_IN_RECODE
5717
5718static void save_cur_buf(AV1_COMP *cpi) {
5719 CODING_CONTEXT *const cc = &cpi->coding_context;
5720 AV1_COMMON *cm = &cpi->common;
5721 const YV12_BUFFER_CONFIG *ybf = &cm->cur_frame->buf;
5722 memset(&cc->copy_buffer, 0, sizeof(cc->copy_buffer));
5723 if (aom_alloc_frame_buffer(&cc->copy_buffer, ybf->y_crop_width,
5724 ybf->y_crop_height, ybf->subsampling_x,
5725 ybf->subsampling_y,
5726 ybf->flags & YV12_FLAG_HIGHBITDEPTH, ybf->border,
Urvang Joshi6237b882020-03-26 15:02:26 -07005727 cm->features.byte_alignment) != AOM_CODEC_OK) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005728 aom_internal_error(
5729 &cm->error, AOM_CODEC_MEM_ERROR,
5730 "Failed to allocate copy buffer for saving coding context");
5731 }
5732 aom_yv12_copy_frame(ybf, &cc->copy_buffer, av1_num_planes(cm));
5733}
5734
5735// Coding context that only needs to be saved when recode loop includes
5736// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5737// restoraton).
5738static void save_extra_coding_context(AV1_COMP *cpi) {
5739 CODING_CONTEXT *const cc = &cpi->coding_context;
5740 AV1_COMMON *cm = &cpi->common;
5741
5742 cc->lf = cm->lf;
5743 cc->cdef_info = cm->cdef_info;
5744 cc->rc = cpi->rc;
5745}
5746
5747static void save_all_coding_context(AV1_COMP *cpi) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005748 save_cur_buf(cpi);
5749 save_extra_coding_context(cpi);
5750 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5751}
5752
5753static void restore_cur_buf(AV1_COMP *cpi) {
5754 CODING_CONTEXT *const cc = &cpi->coding_context;
5755 AV1_COMMON *cm = &cpi->common;
5756 aom_yv12_copy_frame(&cc->copy_buffer, &cm->cur_frame->buf,
5757 av1_num_planes(cm));
5758}
5759
5760// Coding context that only needs to be restored when recode loop includes
5761// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5762// restoraton).
5763static void restore_extra_coding_context(AV1_COMP *cpi) {
5764 CODING_CONTEXT *const cc = &cpi->coding_context;
5765 AV1_COMMON *cm = &cpi->common;
5766 cm->lf = cc->lf;
5767 cm->cdef_info = cc->cdef_info;
5768 cpi->rc = cc->rc;
5769}
5770
5771static void restore_all_coding_context(AV1_COMP *cpi) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005772 restore_cur_buf(cpi);
5773 restore_extra_coding_context(cpi);
5774 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5775}
5776
5777static int encode_with_and_without_superres(AV1_COMP *cpi, size_t *size,
5778 uint8_t *dest,
5779 int *largest_tile_id) {
5780 const AV1_COMMON *const cm = &cpi->common;
5781 AV1EncoderConfig *const oxcf = &cpi->oxcf;
5782 assert(cm->seq_params.enable_superres);
5783 assert(superres_in_recode_allowed(cpi));
5784 aom_codec_err_t err = AOM_CODEC_OK;
5785 save_all_coding_context(cpi);
5786
5787 // Encode with superres.
Urvang Joshibc82d382019-11-01 17:59:20 -07005788#if SUPERRES_RECODE_ALL_RATIOS
5789 int64_t superres_sses[SCALE_NUMERATOR];
5790 int64_t superres_rates[SCALE_NUMERATOR];
5791 int superres_largest_tile_ids[SCALE_NUMERATOR];
5792 // Use superres for Key-frames and Alt-ref frames only.
5793 const GF_GROUP *const gf_group = &cpi->gf_group;
5794 if (gf_group->update_type[gf_group->index] != OVERLAY_UPDATE &&
5795 gf_group->update_type[gf_group->index] != INTNL_OVERLAY_UPDATE) {
5796 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
5797 ++denom) {
5798 oxcf->superres_scale_denominator = denom;
5799 oxcf->superres_kf_scale_denominator = denom;
5800 const int this_index = denom - (SCALE_NUMERATOR + 1);
5801 err = encode_with_recode_loop_and_filter(
5802 cpi, size, dest, &superres_sses[this_index],
5803 &superres_rates[this_index], &superres_largest_tile_ids[this_index]);
5804 if (err != AOM_CODEC_OK) return err;
5805 restore_all_coding_context(cpi);
5806 }
5807 // Reset.
5808 oxcf->superres_scale_denominator = SCALE_NUMERATOR;
5809 oxcf->superres_kf_scale_denominator = SCALE_NUMERATOR;
5810 } else {
5811 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
5812 ++denom) {
5813 const int this_index = denom - (SCALE_NUMERATOR + 1);
5814 superres_sses[this_index] = INT64_MAX;
5815 superres_rates[this_index] = INT64_MAX;
5816 }
5817 }
5818#else
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005819 int64_t sse1 = INT64_MAX;
5820 int64_t rate1 = INT64_MAX;
5821 int largest_tile_id1;
5822 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse1, &rate1,
5823 &largest_tile_id1);
5824 if (err != AOM_CODEC_OK) return err;
Urvang Joshibc82d382019-11-01 17:59:20 -07005825 restore_all_coding_context(cpi);
5826#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005827
5828 // Encode without superres.
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005829 int64_t sse2 = INT64_MAX;
5830 int64_t rate2 = INT64_MAX;
5831 int largest_tile_id2;
5832 oxcf->superres_mode = SUPERRES_NONE; // To force full-res.
5833 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
5834 &largest_tile_id2);
5835 oxcf->superres_mode = SUPERRES_AUTO; // Reset.
5836 if (err != AOM_CODEC_OK) return err;
5837
5838 // Note: Both use common rdmult based on base qindex of fullres.
5839 const int64_t rdmult =
Urvang Joshi17814622020-03-27 17:26:17 -07005840 av1_compute_rd_mult_based_on_qindex(cpi, cm->quant_params.base_qindex);
Urvang Joshibc82d382019-11-01 17:59:20 -07005841
5842#if SUPERRES_RECODE_ALL_RATIOS
5843 // Find the best rdcost among all superres denoms.
5844 double proj_rdcost1 = DBL_MAX;
5845 int64_t sse1 = INT64_MAX;
5846 int64_t rate1 = INT64_MAX;
5847 int largest_tile_id1 = 0;
5848 (void)sse1;
5849 (void)rate1;
5850 (void)largest_tile_id1;
5851 int best_denom = -1;
5852 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR; ++denom) {
5853 const int this_index = denom - (SCALE_NUMERATOR + 1);
5854 const int64_t this_sse = superres_sses[this_index];
5855 const int64_t this_rate = superres_rates[this_index];
5856 const int this_largest_tile_id = superres_largest_tile_ids[this_index];
5857 const double this_rdcost = RDCOST_DBL(rdmult, this_rate, this_sse);
5858 if (this_rdcost < proj_rdcost1) {
5859 sse1 = this_sse;
5860 rate1 = this_rate;
5861 largest_tile_id1 = this_largest_tile_id;
5862 proj_rdcost1 = this_rdcost;
5863 best_denom = denom;
5864 }
5865 }
5866#else
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005867 const double proj_rdcost1 = RDCOST_DBL(rdmult, rate1, sse1);
Urvang Joshibc82d382019-11-01 17:59:20 -07005868#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005869 const double proj_rdcost2 = RDCOST_DBL(rdmult, rate2, sse2);
5870
5871 // Re-encode with superres if it's better.
5872 if (proj_rdcost1 < proj_rdcost2) {
5873 restore_all_coding_context(cpi);
5874 // TODO(urvang): We should avoid rerunning the recode loop by saving
5875 // previous output+state, or running encode only for the selected 'q' in
5876 // previous step.
Urvang Joshibc82d382019-11-01 17:59:20 -07005877#if SUPERRES_RECODE_ALL_RATIOS
5878 // Again, temporarily force the best denom.
5879 oxcf->superres_scale_denominator = best_denom;
5880 oxcf->superres_kf_scale_denominator = best_denom;
5881#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005882 int64_t sse3 = INT64_MAX;
5883 int64_t rate3 = INT64_MAX;
5884 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
5885 largest_tile_id);
5886 assert(sse1 == sse3);
5887 assert(rate1 == rate3);
5888 assert(largest_tile_id1 == *largest_tile_id);
Urvang Joshibc82d382019-11-01 17:59:20 -07005889#if SUPERRES_RECODE_ALL_RATIOS
5890 // Reset.
5891 oxcf->superres_scale_denominator = SCALE_NUMERATOR;
5892 oxcf->superres_kf_scale_denominator = SCALE_NUMERATOR;
5893#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005894 } else {
5895 *largest_tile_id = largest_tile_id2;
5896 }
5897
5898 return err;
5899}
5900#endif // CONFIG_SUPERRES_IN_RECODE
5901
Yaowu Xuc27fc142016-08-22 16:08:15 -07005902#define DUMP_RECON_FRAMES 0
5903
5904#if DUMP_RECON_FRAMES == 1
5905// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07005906static void dump_filtered_recon_frames(AV1_COMP *cpi) {
5907 AV1_COMMON *const cm = &cpi->common;
David Turnerd2a592e2018-11-16 14:59:31 +00005908 const CurrentFrame *const current_frame = &cm->current_frame;
David Turnerc29e1a92018-12-06 14:10:14 +00005909 const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005910
Zoe Liub4f31032017-11-03 23:48:35 -07005911 if (recon_buf == NULL) {
David Turnerd2a592e2018-11-16 14:59:31 +00005912 printf("Frame %d is not ready.\n", current_frame->frame_number);
Zoe Liub4f31032017-11-03 23:48:35 -07005913 return;
5914 }
5915
Zoe Liu27deb382018-03-27 15:13:56 -07005916 static const int flag_list[REF_FRAMES] = { 0,
5917 AOM_LAST_FLAG,
5918 AOM_LAST2_FLAG,
5919 AOM_LAST3_FLAG,
5920 AOM_GOLD_FLAG,
5921 AOM_BWD_FLAG,
5922 AOM_ALT2_FLAG,
5923 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07005924 printf(
5925 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
5926 "show_existing_frame=%d) "
5927 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
David Turnerd2a592e2018-11-16 14:59:31 +00005928 current_frame->frame_number, current_frame->order_hint, cm->show_frame,
Zoe Liub4f31032017-11-03 23:48:35 -07005929 cm->show_existing_frame);
5930 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00005931 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
5932 const int ref_offset = buf != NULL ? (int)buf->order_hint : -1;
Urvang Joshib6f17672019-03-05 11:51:02 -08005933 printf(" %d(%c)", ref_offset,
5934 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N');
Zoe Liub4f31032017-11-03 23:48:35 -07005935 }
5936 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07005937
5938 if (!cm->show_frame) {
5939 printf("Frame %d is a no show frame, so no image dump.\n",
David Turnerd2a592e2018-11-16 14:59:31 +00005940 current_frame->frame_number);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005941 return;
5942 }
5943
Zoe Liub4f31032017-11-03 23:48:35 -07005944 int h;
5945 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
5946 FILE *f_recon = NULL;
5947
David Turnerd2a592e2018-11-16 14:59:31 +00005948 if (current_frame->frame_number == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005949 if ((f_recon = fopen(file_name, "wb")) == NULL) {
5950 printf("Unable to open file %s to write.\n", file_name);
5951 return;
5952 }
5953 } else {
5954 if ((f_recon = fopen(file_name, "ab")) == NULL) {
5955 printf("Unable to open file %s to append.\n", file_name);
5956 return;
5957 }
5958 }
5959 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07005960 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
5961 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
Urvang Joshi7a890232019-03-22 17:00:31 -07005962 "refresh_alt_ref_frame=%d, "
Zoe Liuf40a9572017-10-13 12:37:19 -07005963 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Sarah Parkere1b22012019-06-06 16:35:25 -07005964 current_frame->frame_number, cpi->gf_group.index,
5965 cpi->gf_group.update_type[cpi->gf_group.index], current_frame->order_hint,
5966 cm->show_frame, cm->show_existing_frame, cpi->rc.source_alt_ref_active,
5967 cpi->refresh_alt_ref_frame, recon_buf->y_stride, recon_buf->uv_stride,
5968 cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07005969#if 0
5970 int ref_frame;
5971 printf("get_ref_frame_map_idx: [");
5972 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
David Turnera21966b2018-12-05 14:48:49 +00005973 printf(" %d", get_ref_frame_map_idx(cm, ref_frame));
Zoe Liue9b15e22017-07-19 15:53:01 -07005974 printf(" ]\n");
Zoe Liue9b15e22017-07-19 15:53:01 -07005975#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07005976
5977 // --- Y ---
5978 for (h = 0; h < cm->height; ++h) {
5979 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
5980 f_recon);
5981 }
5982 // --- U ---
5983 for (h = 0; h < (cm->height >> 1); ++h) {
5984 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5985 f_recon);
5986 }
5987 // --- V ---
5988 for (h = 0; h < (cm->height >> 1); ++h) {
5989 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5990 f_recon);
5991 }
5992
5993 fclose(f_recon);
5994}
5995#endif // DUMP_RECON_FRAMES
5996
David Turnerefed6372019-01-11 15:14:11 +00005997static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
Remya163db3c2020-02-17 14:29:41 +05305998 const YV12_BUFFER_CONFIG *last_picture) {
David Turnerefed6372019-01-11 15:14:11 +00005999 aom_clear_system_state();
6000 // check use hash ME
6001 int k;
David Turnerefed6372019-01-11 15:14:11 +00006002
Ranjit Kumar Tulabandu9f26c992019-10-24 12:24:52 +05306003 const int block_size = FORCE_INT_MV_DECISION_BLOCK_SIZE;
David Turnerefed6372019-01-11 15:14:11 +00006004 const double threshold_current = 0.8;
6005 const double threshold_average = 0.95;
6006 const int max_history_size = 32;
6007 int T = 0; // total block
6008 int C = 0; // match with collocated block
6009 int S = 0; // smooth region but not match with collocated block
David Turnerefed6372019-01-11 15:14:11 +00006010
6011 const int pic_width = cur_picture->y_width;
6012 const int pic_height = cur_picture->y_height;
6013 for (int i = 0; i + block_size <= pic_height; i += block_size) {
6014 for (int j = 0; j + block_size <= pic_width; j += block_size) {
6015 const int x_pos = j;
6016 const int y_pos = i;
6017 int match = 1;
6018 T++;
6019
6020 // check whether collocated block match with current
6021 uint8_t *p_cur = cur_picture->y_buffer;
6022 uint8_t *p_ref = last_picture->y_buffer;
6023 int stride_cur = cur_picture->y_stride;
6024 int stride_ref = last_picture->y_stride;
6025 p_cur += (y_pos * stride_cur + x_pos);
6026 p_ref += (y_pos * stride_ref + x_pos);
6027
6028 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
6029 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
6030 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
6031 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
6032 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
6033 if (p16_cur[tmpX] != p16_ref[tmpX]) {
6034 match = 0;
6035 }
6036 }
6037 p16_cur += stride_cur;
6038 p16_ref += stride_ref;
6039 }
6040 } else {
6041 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
6042 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
6043 if (p_cur[tmpX] != p_ref[tmpX]) {
6044 match = 0;
6045 }
6046 }
6047 p_cur += stride_cur;
6048 p_ref += stride_ref;
6049 }
6050 }
6051
6052 if (match) {
6053 C++;
6054 continue;
6055 }
6056
6057 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
6058 y_pos) ||
6059 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
6060 S++;
6061 continue;
6062 }
David Turnerefed6372019-01-11 15:14:11 +00006063 }
6064 }
6065
6066 assert(T > 0);
Remya163db3c2020-02-17 14:29:41 +05306067 double cs_rate = ((double)(C + S)) / ((double)(T));
David Turnerefed6372019-01-11 15:14:11 +00006068
Remya163db3c2020-02-17 14:29:41 +05306069 cpi->cs_rate_array[cpi->rate_index] = cs_rate;
David Turnerefed6372019-01-11 15:14:11 +00006070
6071 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
6072 cpi->rate_size++;
6073 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
6074
Remya163db3c2020-02-17 14:29:41 +05306075 if (cs_rate < threshold_current) {
David Turnerefed6372019-01-11 15:14:11 +00006076 return 0;
6077 }
6078
6079 if (C == T) {
6080 return 1;
6081 }
6082
Remya163db3c2020-02-17 14:29:41 +05306083 double cs_average = 0.0;
David Turnerefed6372019-01-11 15:14:11 +00006084
6085 for (k = 0; k < cpi->rate_size; k++) {
Remya163db3c2020-02-17 14:29:41 +05306086 cs_average += cpi->cs_rate_array[k];
David Turnerefed6372019-01-11 15:14:11 +00006087 }
Remya163db3c2020-02-17 14:29:41 +05306088 cs_average /= cpi->rate_size;
David Turnerefed6372019-01-11 15:14:11 +00006089
Remya163db3c2020-02-17 14:29:41 +05306090 if (cs_average < threshold_average) {
David Turnerefed6372019-01-11 15:14:11 +00006091 return 0;
6092 }
6093
Remya163db3c2020-02-17 14:29:41 +05306094 if ((T - C - S) < 0) {
David Turnerefed6372019-01-11 15:14:11 +00006095 return 1;
6096 }
6097
Remya163db3c2020-02-17 14:29:41 +05306098 if (cs_average > 1.01) {
David Turnerefed6372019-01-11 15:14:11 +00006099 return 1;
6100 }
6101
6102 return 0;
6103}
6104
David Turner73245762019-02-11 16:42:34 +00006105// Refresh reference frame buffers according to refresh_frame_flags.
6106static void refresh_reference_frames(AV1_COMP *cpi) {
6107 AV1_COMMON *const cm = &cpi->common;
6108 // All buffers are refreshed for shown keyframes and S-frames.
6109
6110 for (int ref_frame = 0; ref_frame < REF_FRAMES; ref_frame++) {
6111 if (((cm->current_frame.refresh_frame_flags >> ref_frame) & 1) == 1) {
6112 assign_frame_buffer_p(&cm->ref_frame_map[ref_frame], cm->cur_frame);
6113 }
6114 }
6115}
6116
sdengc23c7f12019-06-11 16:56:50 -07006117static void set_mb_ssim_rdmult_scaling(AV1_COMP *cpi) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006118 const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
sdengc23c7f12019-06-11 16:56:50 -07006119 ThreadData *td = &cpi->td;
6120 MACROBLOCK *x = &td->mb;
6121 MACROBLOCKD *xd = &x->e_mbd;
6122 uint8_t *y_buffer = cpi->source->y_buffer;
6123 const int y_stride = cpi->source->y_stride;
6124 const int block_size = BLOCK_16X16;
6125
6126 const int num_mi_w = mi_size_wide[block_size];
6127 const int num_mi_h = mi_size_high[block_size];
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006128 const int num_cols = (mi_params->mi_cols + num_mi_w - 1) / num_mi_w;
6129 const int num_rows = (mi_params->mi_rows + num_mi_h - 1) / num_mi_h;
sdengc23c7f12019-06-11 16:56:50 -07006130 double log_sum = 0.0;
sdengc23c7f12019-06-11 16:56:50 -07006131 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
6132
sdengc23c7f12019-06-11 16:56:50 -07006133 // Loop through each 16x16 block.
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006134 for (int row = 0; row < num_rows; ++row) {
6135 for (int col = 0; col < num_cols; ++col) {
sdengc23c7f12019-06-11 16:56:50 -07006136 double var = 0.0, num_of_var = 0.0;
6137 const int index = row * num_cols + col;
6138
6139 // Loop through each 8x8 block.
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006140 for (int mi_row = row * num_mi_h;
6141 mi_row < mi_params->mi_rows && mi_row < (row + 1) * num_mi_h;
6142 mi_row += 2) {
6143 for (int mi_col = col * num_mi_w;
6144 mi_col < mi_params->mi_cols && mi_col < (col + 1) * num_mi_w;
sdengc23c7f12019-06-11 16:56:50 -07006145 mi_col += 2) {
6146 struct buf_2d buf;
6147 const int row_offset_y = mi_row << 2;
6148 const int col_offset_y = mi_col << 2;
6149
6150 buf.buf = y_buffer + row_offset_y * y_stride + col_offset_y;
6151 buf.stride = y_stride;
6152
6153 if (use_hbd) {
6154 var += av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8,
6155 xd->bd);
6156 } else {
6157 var += av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8);
6158 }
6159
6160 num_of_var += 1.0;
6161 }
6162 }
sdeng32185d12019-06-19 14:47:09 -07006163 var = var / num_of_var;
sdengbdc0cd22019-11-11 15:09:34 -08006164
6165 // Curve fitting with an exponential model on all 16x16 blocks from the
6166 // midres dataset.
6167 var = 67.035434 * (1 - exp(-0.0021489 * var)) + 17.492222;
sdengc23c7f12019-06-11 16:56:50 -07006168 cpi->ssim_rdmult_scaling_factors[index] = var;
6169 log_sum += log(var);
6170 }
6171 }
6172 log_sum = exp(log_sum / (double)(num_rows * num_cols));
6173
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006174 for (int row = 0; row < num_rows; ++row) {
6175 for (int col = 0; col < num_cols; ++col) {
sdengc23c7f12019-06-11 16:56:50 -07006176 const int index = row * num_cols + col;
6177 cpi->ssim_rdmult_scaling_factors[index] /= log_sum;
6178 }
6179 }
sdengc23c7f12019-06-11 16:56:50 -07006180}
6181
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08006182extern void av1_print_frame_contexts(const FRAME_CONTEXT *fc,
6183 const char *filename);
6184
David Turner73245762019-02-11 16:42:34 +00006185static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
6186 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006187 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006188 SequenceHeader *const seq_params = &cm->seq_params;
David Turnerd2a592e2018-11-16 14:59:31 +00006189 CurrentFrame *const current_frame = &cm->current_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07006190 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006191 struct segmentation *const seg = &cm->seg;
Urvang Joshib6409e92020-03-23 11:23:27 -07006192 FeatureFlags *const features = &cm->features;
Thomas Davies4822e142017-10-10 11:30:36 +01006193
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006194#if CONFIG_COLLECT_COMPONENT_TIMING
6195 start_timing(cpi, encode_frame_to_data_rate_time);
6196#endif
6197
Fangwen Fu8d164de2016-12-14 13:40:54 -08006198 // frame type has been decided outside of this function call
David Turnerd2a592e2018-11-16 14:59:31 +00006199 cm->cur_frame->frame_type = current_frame->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07006200
Urvang Joshi54ffae72020-03-23 13:37:10 -07006201 cm->tiles.large_scale = cpi->oxcf.large_scale_tile;
6202 cm->tiles.single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wang9612d552018-05-15 14:58:30 -07006203
Urvang Joshib6409e92020-03-23 11:23:27 -07006204 features->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
6205 // features->allow_ref_frame_mvs needs to be written into the frame header
Urvang Joshi54ffae72020-03-23 13:37:10 -07006206 // while cm->tiles.large_scale is 1, therefore, "cm->tiles.large_scale=1" case
Urvang Joshib6409e92020-03-23 11:23:27 -07006207 // is separated from frame_might_allow_ref_frame_mvs().
Urvang Joshi54ffae72020-03-23 13:37:10 -07006208 features->allow_ref_frame_mvs &= !cm->tiles.large_scale;
Yunqing Wangd48fb162018-06-15 10:55:28 -07006209
Urvang Joshib6409e92020-03-23 11:23:27 -07006210 features->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07006211 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006212
Urvang Joshi1de67aa2020-03-20 11:21:57 -07006213 cpi->last_frame_type = current_frame->frame_type;
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05306214
Sarah Parker33005522018-07-27 14:46:25 -07006215 if (encode_show_existing_frame(cm)) {
David Turner996b2c12018-12-07 15:52:30 +00006216 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006217 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00006218 int largest_tile_id = 0; // Output from bitstream: unused here
6219 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08006220 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006221
David Turner90311862018-11-29 13:34:36 +00006222 if (seq_params->frame_id_numbers_present_flag &&
6223 current_frame->frame_type == KEY_FRAME) {
6224 // Displaying a forward key-frame, so reset the ref buffer IDs
6225 int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
6226 for (int i = 0; i < REF_FRAMES; i++)
6227 cm->ref_frame_id[i] = display_frame_id;
6228 }
6229
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07006230 cpi->seq_params_locked = 1;
6231
Yaowu Xuc27fc142016-08-22 16:08:15 -07006232#if DUMP_RECON_FRAMES == 1
6233 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
6234 dump_filtered_recon_frames(cpi);
6235#endif // DUMP_RECON_FRAMES
6236
David Turner73245762019-02-11 16:42:34 +00006237 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
6238 // for the purpose to verify no mismatch between encoder and decoder.
6239 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006240
David Turner73245762019-02-11 16:42:34 +00006241 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006242
Yaowu Xuc27fc142016-08-22 16:08:15 -07006243 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
6244 // to do post-encoding update accordingly.
6245 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07006246 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07006247 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006248 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006249
David Turnerd2a592e2018-11-16 14:59:31 +00006250 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006251
Tom Finegane4099e32018-01-23 12:01:51 -08006252 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006253 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006254
David Turnerefed6372019-01-11 15:14:11 +00006255 // Work out whether to force_integer_mv this frame
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306256 if (!is_stat_generation_stage(cpi) &&
Urvang Joshib6409e92020-03-23 11:23:27 -07006257 cpi->common.features.allow_screen_content_tools &&
6258 !frame_is_intra_only(cm)) {
David Turnerefed6372019-01-11 15:14:11 +00006259 if (cpi->common.seq_params.force_integer_mv == 2) {
6260 // Adaptive mode: see what previous frame encoded did
6261 if (cpi->unscaled_last_source != NULL) {
Urvang Joshib6409e92020-03-23 11:23:27 -07006262 features->cur_frame_force_integer_mv =
Remya163db3c2020-02-17 14:29:41 +05306263 is_integer_mv(cpi, cpi->source, cpi->unscaled_last_source);
David Turnerefed6372019-01-11 15:14:11 +00006264 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07006265 cpi->common.features.cur_frame_force_integer_mv = 0;
David Turnerefed6372019-01-11 15:14:11 +00006266 }
6267 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07006268 cpi->common.features.cur_frame_force_integer_mv =
David Turnerefed6372019-01-11 15:14:11 +00006269 cpi->common.seq_params.force_integer_mv;
6270 }
6271 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07006272 cpi->common.features.cur_frame_force_integer_mv = 0;
David Turnerefed6372019-01-11 15:14:11 +00006273 }
6274
Yaowu Xuc27fc142016-08-22 16:08:15 -07006275 // Set default state for segment based loop filter update flags.
6276 cm->lf.mode_ref_delta_update = 0;
6277
Yaowu Xuc27fc142016-08-22 16:08:15 -07006278 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05006279 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006280 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07006281 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006282
6283 // If segmentation is enabled force a map update for key frames.
6284 if (seg->enabled) {
6285 seg->update_map = 1;
6286 seg->update_data = 1;
6287 }
6288
6289 // The alternate reference frame cannot be active for a key frame.
6290 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006291 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00006292 if (cpi->oxcf.mtu == 0) {
Urvang Joshi1de67aa2020-03-20 11:21:57 -07006293 cpi->num_tg = cpi->oxcf.num_tile_groups;
Thomas Daviesaf6df172016-11-09 14:04:18 +00006294 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08006295 // Use a default value for the purposes of weighting costs in probability
6296 // updates
Urvang Joshi1de67aa2020-03-20 11:21:57 -07006297 cpi->num_tg = DEFAULT_MAX_NUM_TG;
Thomas Daviesaf6df172016-11-09 14:04:18 +00006298 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006299
6300 // For 1 pass CBR, check if we are dropping this frame.
6301 // Never drop on key frame.
Mufaddal Chakera3bcc72c2019-12-11 14:38:37 +05306302 if (has_no_stats_stage(cpi) && oxcf->rc_mode == AOM_CBR &&
David Turnerd2a592e2018-11-16 14:59:31 +00006303 current_frame->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006304 if (av1_rc_drop_frame(cpi)) {
6305 av1_rc_postencode_update_drop_frame(cpi);
David Turnera4c96252019-01-11 16:36:39 +00006306 release_scaled_references(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08006307 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006308 }
6309 }
6310
sdengc23c7f12019-06-11 16:56:50 -07006311 if (oxcf->tuning == AOM_TUNE_SSIM) set_mb_ssim_rdmult_scaling(cpi);
6312
sdeng01959162019-12-20 10:46:24 -08006313#if CONFIG_TUNE_VMAF
6314 if (oxcf->tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
sdeng615dc242020-02-04 16:06:14 -08006315 oxcf->tuning == AOM_TUNE_VMAF_MAX_GAIN) {
sdeng01959162019-12-20 10:46:24 -08006316 av1_set_mb_vmaf_rdmult_scaling(cpi);
sdeng29c62152019-12-09 12:44:18 -08006317 }
sdeng01959162019-12-20 10:46:24 -08006318#endif
sdeng29c62152019-12-09 12:44:18 -08006319
Yaowu Xuf883b422016-08-30 14:01:10 -07006320 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006321
6322#if CONFIG_INTERNAL_STATS
6323 memset(cpi->mode_chosen_counts, 0,
6324 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
6325#endif
6326
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006327 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006328 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08006329 * wraparound) */
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006330 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00006331 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08006332 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07006333 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
6334 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
6335 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00006336 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07006337 lsb = cpi->source->y_buffer[0] & 0xff;
6338 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00006339 }
David Turner760a2f42018-12-07 15:25:36 +00006340 cm->current_frame_id =
6341 ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05006342
6343 // S_frame is meant for stitching different streams of different
6344 // resolutions together, so current_frame_id must be the
6345 // same across different streams of the same content current_frame_id
6346 // should be the same and not random. 0x37 is a chosen number as start
6347 // point
6348 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006349 } else {
6350 cm->current_frame_id =
David Turner760a2f42018-12-07 15:25:36 +00006351 (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
6352 (1 << seq_params->frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006353 }
6354 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006355
Hui Su483a8452018-02-26 12:28:48 -08006356 switch (cpi->oxcf.cdf_update_mode) {
6357 case 0: // No CDF update for any frames(4~6% compression loss).
Urvang Joshib6409e92020-03-23 11:23:27 -07006358 features->disable_cdf_update = 1;
Hui Su483a8452018-02-26 12:28:48 -08006359 break;
6360 case 1: // Enable CDF update for all frames.
Urvang Joshib6409e92020-03-23 11:23:27 -07006361 features->disable_cdf_update = 0;
Hui Su483a8452018-02-26 12:28:48 -08006362 break;
6363 case 2:
6364 // Strategically determine at which frames to do CDF update.
6365 // Currently only enable CDF update for all-intra and no-show frames(1.5%
6366 // compression loss).
6367 // TODO(huisu@google.com): design schemes for various trade-offs between
6368 // compression quality and decoding speed.
Urvang Joshib6409e92020-03-23 11:23:27 -07006369 features->disable_cdf_update =
Hui Sub1b76b32018-02-27 15:24:48 -08006370 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08006371 break;
6372 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006373 cm->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08006374
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07006375 int largest_tile_id = 0;
6376#if CONFIG_SUPERRES_IN_RECODE
6377 if (superres_in_recode_allowed(cpi)) {
6378 if (encode_with_and_without_superres(cpi, size, dest, &largest_tile_id) !=
6379 AOM_CODEC_OK) {
6380 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006381 }
Hui Su06463e42018-02-23 22:17:36 -08006382 } else {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07006383#endif // CONFIG_SUPERRES_IN_RECODE
6384 if (encode_with_recode_loop_and_filter(cpi, size, dest, NULL, NULL,
6385 &largest_tile_id) != AOM_CODEC_OK) {
6386 return AOM_CODEC_ERROR;
6387 }
6388#if CONFIG_SUPERRES_IN_RECODE
Hui Su06463e42018-02-23 22:17:36 -08006389 }
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07006390#endif // CONFIG_SUPERRES_IN_RECODE
Yaowu Xuc27fc142016-08-22 16:08:15 -07006391
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07006392 cpi->seq_params_locked = 1;
6393
David Turner996b2c12018-12-07 15:52:30 +00006394 // Update reference frame ids for reference frames this frame will overwrite
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006395 if (seq_params->frame_id_numbers_present_flag) {
David Turner996b2c12018-12-07 15:52:30 +00006396 for (int i = 0; i < REF_FRAMES; i++) {
6397 if ((current_frame->refresh_frame_flags >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006398 cm->ref_frame_id[i] = cm->current_frame_id;
6399 }
6400 }
6401 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006402
Yaowu Xuc27fc142016-08-22 16:08:15 -07006403#if DUMP_RECON_FRAMES == 1
6404 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07006405 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006406#endif // DUMP_RECON_FRAMES
6407
Soo-Chul Han934af352017-10-15 15:21:51 -04006408 if (cm->seg.enabled) {
6409 if (cm->seg.update_map) {
6410 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07006411 } else if (cm->last_frame_seg_map) {
David Turnerb757ce02018-11-12 15:01:28 +00006412 memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
Urvang Joshi9dc909d2020-03-23 16:07:02 -07006413 cm->mi_params.mi_cols * cm->mi_params.mi_rows * sizeof(uint8_t));
Soo-Chul Han934af352017-10-15 15:21:51 -04006414 }
6415 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006416
6417 if (frame_is_intra_only(cm) == 0) {
6418 release_scaled_references(cpi);
6419 }
6420
David Turner73245762019-02-11 16:42:34 +00006421 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
6422 // for the purpose to verify no mismatch between encoder and decoder.
6423 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
6424
6425 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006426
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08006427#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07006428 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08006429#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006430
Urvang Joshi6237b882020-03-26 15:02:26 -07006431 if (features->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
David Turner35cba132018-12-10 15:48:15 +00006432 *cm->fc = cpi->tile_data[largest_tile_id].tctx;
Hui Sudc54be62018-03-14 19:14:28 -07006433 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006434 }
Urvang Joshi54ffae72020-03-23 13:37:10 -07006435 if (!cm->tiles.large_scale) {
David Turnera4c96252019-01-11 16:36:39 +00006436 cm->cur_frame->frame_context = *cm->fc;
6437 }
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08006438
6439 if (cpi->oxcf.ext_tile_debug) {
6440 // (yunqing) This test ensures the correctness of large scale tile coding.
Urvang Joshi54ffae72020-03-23 13:37:10 -07006441 if (cm->tiles.large_scale && is_stat_consumption_stage(cpi)) {
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08006442 char fn[20] = "./fc";
6443 fn[4] = current_frame->frame_number / 100 + '0';
6444 fn[5] = (current_frame->frame_number % 100) / 10 + '0';
6445 fn[6] = (current_frame->frame_number % 10) + '0';
6446 fn[7] = '\0';
6447 av1_print_frame_contexts(cm->fc, fn);
6448 }
David Turnera4c96252019-01-11 16:36:39 +00006449 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006450
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006451#if CONFIG_COLLECT_COMPONENT_TIMING
6452 end_timing(cpi, encode_frame_to_data_rate_time);
6453
6454 // Print out timing information.
6455 int i;
chiyotsai9c484b32019-03-07 16:01:50 -08006456 fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n",
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006457 cm->current_frame.frame_number,
chiyotsai9c484b32019-03-07 16:01:50 -08006458 get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006459 for (i = 0; i < kTimingComponents; i++) {
6460 cpi->component_time[i] += cpi->frame_component_time[i];
6461 fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n",
6462 get_component_name(i), cpi->frame_component_time[i],
6463 cpi->component_time[i]);
6464 cpi->frame_component_time[i] = 0;
6465 }
6466#endif
6467
Urvang Joshi1de67aa2020-03-20 11:21:57 -07006468 cpi->last_frame_type = current_frame->frame_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006469
Yaowu Xuf883b422016-08-30 14:01:10 -07006470 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006471
Yaowu Xuc27fc142016-08-22 16:08:15 -07006472 // Clear the one shot update flags for segmentation map and mode/ref loop
6473 // filter deltas.
6474 cm->seg.update_map = 0;
6475 cm->seg.update_data = 0;
6476 cm->lf.mode_ref_delta_update = 0;
6477
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07006478 // A droppable frame might not be shown but it always
6479 // takes a space in the gf group. Therefore, even when
6480 // it is not shown, we still need update the count down.
6481
Yaowu Xuc27fc142016-08-22 16:08:15 -07006482 if (cm->show_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006483 // Don't increment frame counters if this was an altref buffer
6484 // update not a real frame
David Turnerd2a592e2018-11-16 14:59:31 +00006485 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006486 }
6487
Tom Finegane4099e32018-01-23 12:01:51 -08006488 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006489}
6490
David Turner056f7cd2019-01-07 17:48:13 +00006491int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
David Turnercb5e36f2019-01-17 17:15:25 +00006492 const EncodeFrameInput *const frame_input,
David Turner056f7cd2019-01-07 17:48:13 +00006493 const EncodeFrameParams *const frame_params,
6494 EncodeFrameResults *const frame_results) {
David Turner07dbd8e2019-01-08 17:16:25 +00006495 AV1_COMMON *const cm = &cpi->common;
David Turnera7f133c2019-01-22 14:47:16 +00006496 CurrentFrame *const current_frame = &cm->current_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00006497
David Turnercb5e36f2019-01-17 17:15:25 +00006498 cpi->unscaled_source = frame_input->source;
6499 cpi->source = frame_input->source;
6500 cpi->unscaled_last_source = frame_input->last_source;
David Turner056f7cd2019-01-07 17:48:13 +00006501
David Turner6e8b4d92019-02-18 15:01:33 +00006502 current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
Urvang Joshib6409e92020-03-23 11:23:27 -07006503 cm->features.error_resilient_mode = frame_params->error_resilient_mode;
Urvang Joshi6237b882020-03-26 15:02:26 -07006504 cm->features.primary_ref_frame = frame_params->primary_ref_frame;
David Turner475a3132019-01-18 15:17:17 +00006505 cm->current_frame.frame_type = frame_params->frame_type;
David Turnerdedd8ff2019-01-23 13:59:46 +00006506 cm->show_frame = frame_params->show_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00006507 cpi->ref_frame_flags = frame_params->ref_frame_flags;
David Turner04b70d82019-01-24 15:39:19 +00006508 cpi->speed = frame_params->speed;
David Turnere86ee0d2019-02-18 17:16:28 +00006509 cm->show_existing_frame = frame_params->show_existing_frame;
6510 cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
David Turner07dbd8e2019-01-08 17:16:25 +00006511
David Turner73245762019-02-11 16:42:34 +00006512 memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
6513 REF_FRAMES * sizeof(*cm->remapped_ref_idx));
6514
David Turnerfe3aecb2019-02-06 14:42:42 +00006515 cpi->refresh_last_frame = frame_params->refresh_last_frame;
6516 cpi->refresh_golden_frame = frame_params->refresh_golden_frame;
6517 cpi->refresh_bwd_ref_frame = frame_params->refresh_bwd_ref_frame;
David Turnerfe3aecb2019-02-06 14:42:42 +00006518 cpi->refresh_alt_ref_frame = frame_params->refresh_alt_ref_frame;
6519
David Turnera7f133c2019-01-22 14:47:16 +00006520 if (current_frame->frame_type == KEY_FRAME && cm->show_frame)
6521 current_frame->frame_number = 0;
6522
Remyafc038662020-02-06 11:46:11 +05306523 current_frame->order_hint =
6524 current_frame->frame_number + frame_params->order_offset;
6525 current_frame->display_order_hint = current_frame->order_hint;
6526 current_frame->order_hint %=
6527 (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
David Turnera7f133c2019-01-22 14:47:16 +00006528
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306529 if (is_stat_generation_stage(cpi)) {
Jerome Jiang2612b4d2019-05-29 17:46:47 -07006530#if !CONFIG_REALTIME_ONLY
David Turnercb5e36f2019-01-17 17:15:25 +00006531 av1_first_pass(cpi, frame_input->ts_duration);
Jerome Jiang2612b4d2019-05-29 17:46:47 -07006532#endif
David Turnercb5e36f2019-01-17 17:15:25 +00006533 } else if (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) {
David Turner73245762019-02-11 16:42:34 +00006534 if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
6535 AOM_CODEC_OK) {
David Turnercb5e36f2019-01-17 17:15:25 +00006536 return AOM_CODEC_ERROR;
6537 }
6538 } else {
David Turner056f7cd2019-01-07 17:48:13 +00006539 return AOM_CODEC_ERROR;
6540 }
6541
6542 return AOM_CODEC_OK;
6543}
6544
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006545#if CONFIG_DENOISE
6546static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
6547 int block_size, float noise_level,
6548 int64_t time_stamp, int64_t end_time) {
6549 AV1_COMMON *const cm = &cpi->common;
6550 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006551 cpi->denoise_and_model = aom_denoise_and_model_alloc(
6552 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006553 if (!cpi->denoise_and_model) {
6554 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6555 "Error allocating denoise and model");
6556 return -1;
6557 }
6558 }
6559 if (!cpi->film_grain_table) {
6560 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
6561 if (!cpi->film_grain_table) {
6562 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6563 "Error allocating grain table");
6564 return -1;
6565 }
6566 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
6567 }
6568 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
6569 &cm->film_grain_params)) {
6570 if (cm->film_grain_params.apply_grain) {
6571 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
6572 &cm->film_grain_params);
6573 }
6574 }
6575 return 0;
6576}
6577#endif
6578
James Zern3e2613b2017-03-30 23:14:40 -07006579int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07006580 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
6581 int64_t end_time) {
6582 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006583 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006584 int res = 0;
6585 const int subsampling_x = sd->subsampling_x;
6586 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006587 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006588
sdenge63f9fa2019-12-13 09:29:55 -08006589#if CONFIG_TUNE_VMAF
6590 if (!is_stat_generation_stage(cpi) &&
6591 cpi->oxcf.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING) {
sdeng615dc242020-02-04 16:06:14 -08006592 av1_vmaf_frame_preprocessing(cpi, sd);
6593 }
6594 if (!is_stat_generation_stage(cpi) &&
6595 cpi->oxcf.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
6596 av1_vmaf_blk_preprocessing(cpi, sd);
sdenge63f9fa2019-12-13 09:29:55 -08006597 }
6598#endif
6599
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006600#if CONFIG_INTERNAL_STATS
6601 struct aom_usec_timer timer;
Yaowu Xuf883b422016-08-30 14:01:10 -07006602 aom_usec_timer_start(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006603#endif
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006604#if CONFIG_DENOISE
6605 if (cpi->oxcf.noise_level > 0)
6606 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
6607 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
6608 res = -1;
6609#endif // CONFIG_DENOISE
6610
Yaowu Xuf883b422016-08-30 14:01:10 -07006611 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08006612 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07006613 res = -1;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006614#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006615 aom_usec_timer_mark(&timer);
6616 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006617#endif
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006618 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07006619 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006620 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006621 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07006622 res = -1;
6623 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006624 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006625 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006626 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006627 "Profile 1 requires 4:4:4 color format");
6628 res = -1;
6629 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006630 if ((seq_params->profile == PROFILE_2) &&
6631 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006632 !(subsampling_x == 1 && subsampling_y == 0)) {
6633 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
6634 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07006635 res = -1;
6636 }
6637
6638 return res;
6639}
6640
Yaowu Xuc27fc142016-08-22 16:08:15 -07006641#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006642extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
6643 const unsigned char *img2, int img2_pitch,
6644 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006645
6646static void adjust_image_stat(double y, double u, double v, double all,
6647 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07006648 s->stat[STAT_Y] += y;
6649 s->stat[STAT_U] += u;
6650 s->stat[STAT_V] += v;
6651 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07006652 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006653}
6654
Angie Chiang08a22a62017-07-17 17:29:17 -07006655static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006656 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006657 double samples = 0.0;
Yaowu Xue75b58a2020-01-03 12:56:12 -08006658 const uint32_t in_bit_depth = cpi->oxcf.input_bit_depth;
6659 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006660
Angie Chiang08a22a62017-07-17 17:29:17 -07006661#if CONFIG_INTER_STATS_ONLY
David Turnerd2a592e2018-11-16 14:59:31 +00006662 if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07006663#endif
6664 cpi->bytes += frame_bytes;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006665 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07006666 const YV12_BUFFER_CONFIG *orig = cpi->source;
David Turnerc29e1a92018-12-06 14:10:14 +00006667 const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006668 double y, u, v, frame_all;
6669
6670 cpi->count++;
6671 if (cpi->b_calculate_psnr) {
6672 PSNR_STATS psnr;
6673 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07006674 aom_clear_system_state();
Jerome Jiangfa1d1732019-08-06 10:31:20 -07006675#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xue75b58a2020-01-03 12:56:12 -08006676 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07006677#else
6678 aom_calc_psnr(orig, recon, &psnr);
6679#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006680 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
6681 &cpi->psnr);
6682 cpi->total_sq_error += psnr.sse[0];
6683 cpi->total_samples += psnr.samples[0];
6684 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08006685 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006686 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07006687 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07006688 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006689 else
Yaowu Xuf883b422016-08-30 14:01:10 -07006690 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006691
Yaowu Xuf883b422016-08-30 14:01:10 -07006692 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006693 cpi->summed_quality += frame_ssim2 * weight;
6694 cpi->summed_weights += weight;
6695
6696#if 0
6697 {
6698 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08006699 double y2 = psnr.psnr[1];
6700 double u2 = psnr.psnr[2];
6701 double v2 = psnr.psnr[3];
6702 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07006703 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
David Turnerd2a592e2018-11-16 14:59:31 +00006704 cm->current_frame.frame_number, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07006705 frame_psnr2, frame_ssim2);
6706 fclose(f);
6707 }
6708#endif
6709 }
6710 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006711 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006712 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07006713 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
6714 recon->y_stride, orig->y_width, orig->y_height);
6715 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006716 cpi->total_blockiness += frame_blockiness;
6717 }
6718
6719 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006720 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006721 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07006722 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
6723 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
6724
6725 const double peak = (double)((1 << in_bit_depth) - 1);
6726 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006727 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006728 if (consistency > 0.0)
6729 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006730 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006731 cpi->total_inconsistency += this_inconsistency;
6732 }
6733 }
6734 }
6735
6736 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07006737 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006738 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07006739 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006740 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
6741 }
6742}
6743#endif // CONFIG_INTERNAL_STATS
Andrey Norkin795ba872018-03-06 13:24:14 -08006744int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
6745 size_t *size, uint8_t *dest, int64_t *time_stamp,
6746 int64_t *time_end, int flush,
Yue Chen1bc5be62018-08-24 13:57:32 -07006747 const aom_rational64_t *timestamp_ratio) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006748 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
6749 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006750
6751#if CONFIG_BITSTREAM_DEBUG
6752 assert(cpi->oxcf.max_threads == 0 &&
6753 "bitstream debug tool does not support multithreading");
6754 bitstream_queue_record_write();
Yaowu Xu63f2ea32019-04-29 10:47:42 -07006755 aom_bitstream_queue_set_frame_write(cm->current_frame.frame_number * 2 +
6756 cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006757#endif
Marco Paniconi63971322019-08-15 21:32:05 -07006758 if (cpi->use_svc && cm->number_spatial_layers > 1) {
6759 av1_one_pass_cbr_svc_start_layer(cpi);
6760 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006761
Sarah Parker740e8392019-01-23 15:47:53 -08006762 // Indicates whether or not to use an adaptive quantize b rather than
6763 // the traditional version
Urvang Joshi1de67aa2020-03-20 11:21:57 -07006764 cpi->use_quant_b_adapt = cpi->oxcf.quant_b_adapt;
Sarah Parker740e8392019-01-23 15:47:53 -08006765
Dominic Symesd4929012018-01-31 17:32:01 +01006766 cm->showable_frame = 0;
David Turnere43f7fe2019-01-15 14:58:00 +00006767 *size = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006768#if CONFIG_INTERNAL_STATS
6769 struct aom_usec_timer cmptimer;
Yaowu Xuf883b422016-08-30 14:01:10 -07006770 aom_usec_timer_start(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006771#endif
chiyotsaic666b1f2019-12-20 10:44:58 -08006772 av1_set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006773
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07006774 // Normal defaults
Urvang Joshi6237b882020-03-26 15:02:26 -07006775 cm->features.refresh_frame_context = oxcf->frame_parallel_decoding_mode
6776 ? REFRESH_FRAME_CONTEXT_DISABLED
6777 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01006778 if (oxcf->large_scale_tile)
Urvang Joshi6237b882020-03-26 15:02:26 -07006779 cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006780
Sarah Parkerb9041612018-05-22 19:06:47 -07006781 // Initialize fields related to forward keyframes
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07006782 cpi->no_show_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08006783
David Turnerdedd8ff2019-01-23 13:59:46 +00006784 if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006785
Yue Chen1bc5be62018-08-24 13:57:32 -07006786 const int result =
6787 av1_encode_strategy(cpi, size, dest, frame_flags, time_stamp, time_end,
6788 timestamp_ratio, flush);
David Turnerdedd8ff2019-01-23 13:59:46 +00006789 if (result != AOM_CODEC_OK && result != -1) {
David Turner1539bb02019-01-24 15:28:13 +00006790 return AOM_CODEC_ERROR;
David Turnerdedd8ff2019-01-23 13:59:46 +00006791 } else if (result == -1) {
6792 // Returning -1 indicates no frame encoded; more input is required
6793 return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006794 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006795#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006796 aom_usec_timer_mark(&cmptimer);
6797 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yue Chen1bc5be62018-08-24 13:57:32 -07006798#endif // CONFIG_INTERNAL_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00006799 if (cpi->b_calculate_psnr) {
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306800 if (cm->show_existing_frame ||
6801 (!is_stat_generation_stage(cpi) && cm->show_frame)) {
David Turnerc4bf8c72019-01-15 13:14:37 +00006802 generate_psnr_packet(cpi);
6803 }
6804 }
Hui Su8ea87322019-03-29 14:44:32 -07006805
sdeng3cd9eec2020-01-23 15:49:50 -08006806#if CONFIG_TUNE_VMAF
sdeng995c54e2020-02-18 14:51:00 -08006807 if (!is_stat_generation_stage(cpi) &&
6808 (oxcf->tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING ||
6809 oxcf->tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
6810 oxcf->tuning == AOM_TUNE_VMAF_MAX_GAIN)) {
6811 av1_update_vmaf_curve(cpi, cpi->source, &cpi->common.cur_frame->buf);
sdeng3cd9eec2020-01-23 15:49:50 -08006812 }
6813#endif
6814
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306815 if (cpi->keep_level_stats && !is_stat_generation_stage(cpi)) {
Hui Su8ea87322019-03-29 14:44:32 -07006816 // Initialize level info. at the beginning of each sequence.
6817 if (cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) {
Hui Su58753d62019-05-29 09:56:19 -07006818 av1_init_level_info(cpi);
Hui Su8ea87322019-03-29 14:44:32 -07006819 }
kyslovabeeb7c2019-03-06 18:35:04 -08006820 av1_update_level_info(cpi, *size, *time_stamp, *time_end);
Hui Su8ea87322019-03-29 14:44:32 -07006821 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006822
6823#if CONFIG_INTERNAL_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306824 if (!is_stat_generation_stage(cpi)) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006825 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006826 }
6827#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08006828#if CONFIG_SPEED_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306829 if (!is_stat_generation_stage(cpi) && !cm->show_existing_frame) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08006830 cpi->tx_search_count += cpi->td.mb.tx_search_count;
6831 cpi->td.mb.tx_search_count = 0;
6832 }
6833#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006834
Yaowu Xuf883b422016-08-30 14:01:10 -07006835 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006836
Hui Su0d0ee662019-07-29 14:38:36 -07006837 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006838}
6839
Yaowu Xuf883b422016-08-30 14:01:10 -07006840int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6841 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006842 if (!cm->show_frame) {
6843 return -1;
6844 } else {
6845 int ret;
David Turnerc29e1a92018-12-06 14:10:14 +00006846 if (cm->cur_frame != NULL) {
6847 *dest = cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006848 dest->y_width = cm->width;
6849 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006850 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
6851 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006852 ret = 0;
6853 } else {
6854 ret = -1;
6855 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006856 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006857 return ret;
6858 }
6859}
6860
Yaowu Xuf883b422016-08-30 14:01:10 -07006861int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
David Turnere7ebf902018-12-04 14:04:55 +00006862 if (cpi->last_show_frame_buf == NULL) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006863
David Turnere7ebf902018-12-04 14:04:55 +00006864 *frame = cpi->last_show_frame_buf->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006865 return 0;
6866}
6867
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006868static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
6869 const YV12_BUFFER_CONFIG *b) {
6870 return a->y_height == b->y_height && a->y_width == b->y_width &&
6871 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
6872 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
6873 a->border == b->border &&
6874 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
6875 (b->flags & YV12_FLAG_HIGHBITDEPTH);
6876}
6877
Yunqing Wang93b18f32018-06-08 21:08:29 -07006878aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
6879 YV12_BUFFER_CONFIG *new_frame,
6880 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006881 const int num_planes = av1_num_planes(cm);
6882 if (!equal_dimensions_and_border(new_frame, sd))
6883 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
6884 "Incorrect buffer dimensions");
6885 else
6886 aom_yv12_copy_frame(new_frame, sd, num_planes);
6887
6888 return cm->error.error_code;
6889}
6890
Yaowu Xuf883b422016-08-30 14:01:10 -07006891int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6892 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006893 int hr = 0, hs = 0, vr = 0, vs = 0;
6894
6895 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6896
6897 Scale2Ratio(horiz_mode, &hr, &hs);
6898 Scale2Ratio(vert_mode, &vr, &vs);
6899
6900 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07006901 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6902 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006903
6904 return 0;
6905}
6906
Urvang Joshi17814622020-03-27 17:26:17 -07006907int av1_get_quantizer(AV1_COMP *cpi) {
6908 return cpi->common.quant_params.base_qindex;
6909}
Yaowu Xuc27fc142016-08-22 16:08:15 -07006910
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006911int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
6912 size_t output_size = 0;
6913 size_t total_bytes_read = 0;
6914 size_t remaining_size = *frame_size;
6915 uint8_t *buff_ptr = buffer;
6916
6917 // go through each OBUs
6918 while (total_bytes_read < *frame_size) {
6919 uint8_t saved_obu_header[2];
6920 uint64_t obu_payload_size;
6921 size_t length_of_payload_size;
6922 size_t length_of_obu_size;
6923 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
6924 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
6925
6926 // save the obu header (1 or 2 bytes)
6927 memmove(saved_obu_header, buff_ptr, obu_header_size);
6928 // clear the obu_has_size_field
6929 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
6930
6931 // get the payload_size and length of payload_size
6932 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
6933 &obu_payload_size, &length_of_payload_size) != 0) {
6934 return AOM_CODEC_ERROR;
6935 }
6936 obu_bytes_read += length_of_payload_size;
6937
6938 // calculate the length of size of the obu header plus payload
6939 length_of_obu_size =
6940 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
6941
6942 // move the rest of data to new location
6943 memmove(buff_ptr + length_of_obu_size + obu_header_size,
6944 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07006945 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006946
6947 // write the new obu size
6948 const uint64_t obu_size = obu_header_size + obu_payload_size;
6949 size_t coded_obu_size;
6950 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
6951 &coded_obu_size) != 0) {
6952 return AOM_CODEC_ERROR;
6953 }
6954
6955 // write the saved (modified) obu_header following obu size
6956 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
6957
6958 total_bytes_read += obu_bytes_read;
6959 remaining_size -= obu_bytes_read;
6960 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07006961 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006962 }
6963
6964 *frame_size = output_size;
6965 return AOM_CODEC_OK;
6966}
6967
Marco Paniconid8574e32019-08-04 21:30:12 -07006968static void svc_set_updates_external_ref_frame_config(AV1_COMP *cpi) {
6969 cpi->ext_refresh_frame_flags_pending = 1;
6970 cpi->ext_refresh_last_frame = cpi->svc.refresh[cpi->svc.ref_idx[0]];
6971 cpi->ext_refresh_golden_frame = cpi->svc.refresh[cpi->svc.ref_idx[3]];
6972 cpi->ext_refresh_bwd_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[4]];
6973 cpi->ext_refresh_alt2_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[5]];
6974 cpi->ext_refresh_alt_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[6]];
6975 cpi->svc.non_reference_frame = 1;
6976 for (int i = 0; i < REF_FRAMES; i++) {
6977 if (cpi->svc.refresh[i] == 1) {
6978 cpi->svc.non_reference_frame = 0;
6979 break;
6980 }
6981 }
6982}
6983
Yaowu Xuf883b422016-08-30 14:01:10 -07006984void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006985 // TODO(yunqingwang): For what references to use, external encoding flags
6986 // should be consistent with internal reference frame selection. Need to
6987 // ensure that there is not conflict between the two. In AV1 encoder, the
6988 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006989 // GOLDEN, BWDREF, ALTREF2.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08006990 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006991 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006992 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
6993 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
6994 AOM_EFLAG_NO_REF_ARF2)) {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006995 int ref = AOM_REFFRAME_ALL;
6996
6997 if (flags & AOM_EFLAG_NO_REF_LAST) ref ^= AOM_LAST_FLAG;
6998 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
6999 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
7000
7001 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
7002
7003 if (flags & AOM_EFLAG_NO_REF_ARF) {
7004 ref ^= AOM_ALT_FLAG;
7005 ref ^= AOM_BWD_FLAG;
7006 ref ^= AOM_ALT2_FLAG;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007007 } else {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07007008 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
7009 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007010 }
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07007011
7012 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007013 }
7014
7015 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007016 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07007017 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007018
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007019 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
7020 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007021
Yaowu Xuf883b422016-08-30 14:01:10 -07007022 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007023
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007024 if (flags & AOM_EFLAG_NO_UPD_ARF) {
7025 upd ^= AOM_ALT_FLAG;
7026 upd ^= AOM_BWD_FLAG;
7027 upd ^= AOM_ALT2_FLAG;
7028 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07007029
David Turnerce9b5902019-01-23 17:25:47 +00007030 cpi->ext_refresh_last_frame = (upd & AOM_LAST_FLAG) != 0;
7031 cpi->ext_refresh_golden_frame = (upd & AOM_GOLD_FLAG) != 0;
7032 cpi->ext_refresh_alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
7033 cpi->ext_refresh_bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
7034 cpi->ext_refresh_alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
7035 cpi->ext_refresh_frame_flags_pending = 1;
David Turner4f1f1812019-01-24 17:00:24 +00007036 } else {
Marco Paniconid8574e32019-08-04 21:30:12 -07007037 if (cpi->svc.external_ref_frame_config)
7038 svc_set_updates_external_ref_frame_config(cpi);
7039 else
7040 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007041 }
7042
sarahparker21dbca42018-03-30 17:43:44 -07007043 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
7044 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
sarahparker27d686a2018-03-30 17:43:44 -07007045 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
7046 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
sarahparker9806fed2018-03-30 17:43:44 -07007047 cpi->ext_use_s_frame =
7048 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Sarah Parker50b6d6e2018-04-11 19:21:54 -07007049 cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07007050
Yaowu Xuf883b422016-08-30 14:01:10 -07007051 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
7052 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007053 }
7054}
Andrey Norkin795ba872018-03-06 13:24:14 -08007055
Tom Fineganf8d6a162018-08-21 10:47:55 -07007056aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
7057 if (!cpi) return NULL;
7058
7059 uint8_t header_buf[512] = { 0 };
7060 const uint32_t sequence_header_size =
Yaowu Xu797674b2019-05-01 17:38:11 -07007061 av1_write_sequence_header_obu(cpi, &header_buf[0]);
Tom Fineganf8d6a162018-08-21 10:47:55 -07007062 assert(sequence_header_size <= sizeof(header_buf));
7063 if (sequence_header_size == 0) return NULL;
7064
7065 const size_t obu_header_size = 1;
7066 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
7067 const size_t payload_offset = obu_header_size + size_field_size;
7068
7069 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
7070 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
7071
Hui Su4fd11762019-03-26 16:05:07 -07007072 if (av1_write_obu_header(cpi, OBU_SEQUENCE_HEADER, 0, &header_buf[0]) !=
Tom Fineganf8d6a162018-08-21 10:47:55 -07007073 obu_header_size) {
7074 return NULL;
7075 }
7076
7077 size_t coded_size_field_size = 0;
7078 if (aom_uleb_encode(sequence_header_size, size_field_size,
7079 &header_buf[obu_header_size],
7080 &coded_size_field_size) != 0) {
7081 return NULL;
7082 }
7083 assert(coded_size_field_size == size_field_size);
7084
7085 aom_fixed_buf_t *global_headers =
7086 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
7087 if (!global_headers) return NULL;
7088
7089 const size_t global_header_buf_size =
7090 obu_header_size + size_field_size + sequence_header_size;
7091
7092 global_headers->buf = malloc(global_header_buf_size);
7093 if (!global_headers->buf) {
7094 free(global_headers);
7095 return NULL;
7096 }
7097
7098 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
7099 global_headers->sz = global_header_buf_size;
7100 return global_headers;
7101}