blob: a4675ef536d3ef357e58b3b694e8982551f451cd [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)
405 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
406 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
407 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
408}
409
Yaowu Xuf883b422016-08-30 14:01:10 -0700410static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700411 struct segmentation *const seg = &cpi->common.seg;
412 unsigned char *const seg_map = cpi->segmentation_map;
413 const unsigned char *const active_map = cpi->active_map.map;
414 int i;
415
416 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
417
418 if (frame_is_intra_only(&cpi->common)) {
419 cpi->active_map.enabled = 0;
420 cpi->active_map.update = 1;
421 }
422
423 if (cpi->active_map.update) {
424 if (cpi->active_map.enabled) {
425 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
426 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700427 av1_enable_segmentation(seg);
428 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700429 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
430 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
431 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
432 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
433
434 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
435 -MAX_LOOP_FILTER);
436 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
437 -MAX_LOOP_FILTER);
438 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
439 -MAX_LOOP_FILTER);
440 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
441 -MAX_LOOP_FILTER);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700442 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700443 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700444 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
445 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
446 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
447 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700448 if (seg->enabled) {
449 seg->update_data = 1;
450 seg->update_map = 1;
451 }
452 }
453 cpi->active_map.update = 0;
454 }
455}
456
Yaowu Xuf883b422016-08-30 14:01:10 -0700457int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
458 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700459 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
460 unsigned char *const active_map_8x8 = cpi->active_map.map;
461 const int mi_rows = cpi->common.mi_rows;
462 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700463 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
464 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700465 cpi->active_map.update = 1;
466 if (new_map_16x16) {
467 int r, c;
468 for (r = 0; r < mi_rows; ++r) {
469 for (c = 0; c < mi_cols; ++c) {
470 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700471 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700472 ? AM_SEGMENT_ID_ACTIVE
473 : AM_SEGMENT_ID_INACTIVE;
474 }
475 }
476 cpi->active_map.enabled = 1;
477 } else {
478 cpi->active_map.enabled = 0;
479 }
480 return 0;
481 } else {
482 return -1;
483 }
484}
485
Yaowu Xuf883b422016-08-30 14:01:10 -0700486int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
487 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700488 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
489 new_map_16x16) {
490 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
491 const int mi_rows = cpi->common.mi_rows;
492 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700493 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
494 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
495
Yaowu Xuc27fc142016-08-22 16:08:15 -0700496 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
497 if (cpi->active_map.enabled) {
498 int r, c;
499 for (r = 0; r < mi_rows; ++r) {
500 for (c = 0; c < mi_cols; ++c) {
501 // Cyclic refresh segments are considered active despite not having
502 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700503 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700504 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
505 }
506 }
507 }
508 return 0;
509 } else {
510 return -1;
511 }
512}
513
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800514// Compute the horizontal frequency components' energy in a frame
515// by calculuating the 16x4 Horizontal DCT. This is to be used to
516// decide the superresolution parameters.
Yaowu Xubedbf4f2019-05-01 17:54:36 -0700517static void analyze_hor_freq(const AV1_COMP *cpi, double *energy) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800518 uint64_t freq_energy[16] = { 0 };
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800519 const YV12_BUFFER_CONFIG *buf = cpi->source;
520 const int bd = cpi->td.mb.e_mbd.bd;
521 const int width = buf->y_crop_width;
522 const int height = buf->y_crop_height;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800523 DECLARE_ALIGNED(16, int32_t, coeff[16 * 4]);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800524 int n = 0;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800525 memset(freq_energy, 0, sizeof(freq_energy));
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800526 if (buf->flags & YV12_FLAG_HIGHBITDEPTH) {
527 const int16_t *src16 = (const int16_t *)CONVERT_TO_SHORTPTR(buf->y_buffer);
528 for (int i = 0; i < height - 4; i += 4) {
529 for (int j = 0; j < width - 16; j += 16) {
530 av1_fwd_txfm2d_16x4(src16 + i * buf->y_stride + j, coeff, buf->y_stride,
531 H_DCT, bd);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800532 for (int k = 1; k < 16; ++k) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800533 const uint64_t this_energy =
534 ((int64_t)coeff[k] * coeff[k]) +
535 ((int64_t)coeff[k + 16] * coeff[k + 16]) +
536 ((int64_t)coeff[k + 32] * coeff[k + 32]) +
537 ((int64_t)coeff[k + 48] * coeff[k + 48]);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800538 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2 + 2 * (bd - 8));
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800539 }
540 n++;
541 }
542 }
543 } else {
Debargha Mukherjeeac28c722018-11-14 22:09:46 -0800544 assert(bd == 8);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800545 DECLARE_ALIGNED(16, int16_t, src16[16 * 4]);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800546 for (int i = 0; i < height - 4; i += 4) {
547 for (int j = 0; j < width - 16; j += 16) {
548 for (int ii = 0; ii < 4; ++ii)
549 for (int jj = 0; jj < 16; ++jj)
550 src16[ii * 16 + jj] =
551 buf->y_buffer[(i + ii) * buf->y_stride + (j + jj)];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800552 av1_fwd_txfm2d_16x4(src16, coeff, 16, H_DCT, bd);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800553 for (int k = 1; k < 16; ++k) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800554 const uint64_t this_energy =
555 ((int64_t)coeff[k] * coeff[k]) +
556 ((int64_t)coeff[k + 16] * coeff[k + 16]) +
557 ((int64_t)coeff[k + 32] * coeff[k + 32]) +
558 ((int64_t)coeff[k + 48] * coeff[k + 48]);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800559 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800560 }
561 n++;
562 }
563 }
564 }
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800565 if (n) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800566 for (int k = 1; k < 16; ++k) energy[k] = (double)freq_energy[k] / n;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800567 // Convert to cumulative energy
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800568 for (int k = 14; k > 0; --k) energy[k] += energy[k + 1];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800569 } else {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800570 for (int k = 1; k < 16; ++k) energy[k] = 1e+20;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800571 }
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800572}
573
Yaowu Xuf883b422016-08-30 14:01:10 -0700574static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Urvang Joshie4530f82018-01-09 11:43:37 -0800575 const AV1_COMMON *const cm = &cpi->common;
James Zernbf3ca362019-10-21 11:33:44 -0700576
Yaowu Xuf883b422016-08-30 14:01:10 -0700577 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700578 return BLOCK_64X64;
Ryan Leic8b6dd62019-10-23 20:01:26 -0700579 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
580 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700581
Yaowu Xuf883b422016-08-30 14:01:10 -0700582 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700583
Ryan Leic8b6dd62019-10-23 20:01:26 -0700584 // TODO(any): Possibly could improve this with a heuristic.
Urvang Joshiaab74432018-06-01 12:06:22 -0700585 // When superres / resize is on, 'cm->width / height' can change between
Hui Su3e3b9342019-04-12 18:27:28 +0000586 // calls, so we don't apply this heuristic there.
587 // Things break if superblock size changes between the first pass and second
588 // pass encoding, which is why this heuristic is not configured as a
589 // speed-feature.
Urvang Joshiaab74432018-06-01 12:06:22 -0700590 if (cpi->oxcf.superres_mode == SUPERRES_NONE &&
Hui Su3e3b9342019-04-12 18:27:28 +0000591 cpi->oxcf.resize_mode == RESIZE_NONE && cpi->oxcf.speed >= 1) {
592 return AOMMIN(cm->width, cm->height) > 480 ? BLOCK_128X128 : BLOCK_64X64;
Urvang Joshie4530f82018-01-09 11:43:37 -0800593 }
594
Yaowu Xuc27fc142016-08-22 16:08:15 -0700595 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700596}
597
Yaowu Xuf883b422016-08-30 14:01:10 -0700598static void setup_frame(AV1_COMP *cpi) {
599 AV1_COMMON *const cm = &cpi->common;
Johannb0ef6ff2018-02-08 14:32:21 -0800600 // Set up entropy context depending on frame type. The decoder mandates
601 // the use of the default context, index 0, for keyframes and inter
602 // frames where the error_resilient_mode or intra_only flag is set. For
603 // other inter-frames the encoder currently uses only two contexts;
604 // context 1 for ALTREF frames and context 0 for the others.
Soo-Chul Han85e8c792018-01-21 01:58:15 -0500605
Sarah Parker50b6d6e2018-04-11 19:21:54 -0700606 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
David Turnera7f133c2019-01-22 14:47:16 +0000607 cpi->ext_use_primary_ref_none) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700608 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700609 }
610
Hui Sueb4b7de2019-04-03 11:00:18 -0700611 if ((cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) ||
612 frame_is_sframe(cm)) {
613 if (!cpi->seq_params_locked) {
614 set_sb_size(&cm->seq_params, select_sb_size(cpi));
615 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700616 } else {
David Turnera21966b2018-12-05 14:48:49 +0000617 const RefCntBuffer *const primary_ref_buf = get_primary_ref_frame_buf(cm);
618 if (primary_ref_buf == NULL) {
David Barkercc615a82018-03-19 14:38:51 +0000619 av1_setup_past_independence(cm);
620 cm->seg.update_map = 1;
621 cm->seg.update_data = 1;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700622 } else {
David Turnera21966b2018-12-05 14:48:49 +0000623 *cm->fc = primary_ref_buf->frame_context;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700624 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700625 }
626
David Turnerbc0993e2019-02-15 14:42:23 +0000627 av1_zero(cm->cur_frame->interp_filter_selected);
David Turnera21966b2018-12-05 14:48:49 +0000628 cm->prev_frame = get_primary_ref_frame_buf(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700629 cpi->vaq_refresh = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700630}
631
chiyotsaia7091f12019-08-09 16:48:27 -0700632static void enc_set_mb_mi(AV1_COMMON *cm, int width, int height) {
633 // Ensure that the decoded width and height are both multiples of
634 // 8 luma pixels (note: this may only be a multiple of 4 chroma pixels if
635 // subsampling is used).
636 // This simplifies the implementation of various experiments,
637 // eg. cdef, which operates on units of 8x8 luma pixels.
638 const int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
639 const int aligned_height = ALIGN_POWER_OF_TWO(height, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700640
chiyotsaia7091f12019-08-09 16:48:27 -0700641 cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
642 cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
643 cm->mi_stride = calc_mi_size(cm->mi_cols);
644
645 cm->mb_cols = (cm->mi_cols + 2) >> 2;
646 cm->mb_rows = (cm->mi_rows + 2) >> 2;
647 cm->MBs = cm->mb_rows * cm->mb_cols;
648
649 const int is_4k_or_larger = AOMMIN(width, height) >= 2160;
650
651 cm->mi_alloc_bsize = is_4k_or_larger ? BLOCK_8X8 : BLOCK_4X4;
652 const int mi_alloc_size_1d = mi_size_wide[cm->mi_alloc_bsize];
653 cm->mi_alloc_rows = (cm->mi_rows + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
654 cm->mi_alloc_cols = (cm->mi_cols + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
655 cm->mi_alloc_stride =
656 (cm->mi_stride + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
657
658 assert(mi_size_wide[cm->mi_alloc_bsize] == mi_size_high[cm->mi_alloc_bsize]);
659
660#if CONFIG_LPF_MASK
chiyotsaia13c19f2019-08-15 10:12:57 -0700661 av1_alloc_loop_filter_mask(cm);
chiyotsaia7091f12019-08-09 16:48:27 -0700662#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700663}
664
chiyotsaia7091f12019-08-09 16:48:27 -0700665static void enc_setup_mi(AV1_COMMON *cm) {
666 const int mi_grid_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
667 memset(cm->mi, 0, cm->mi_alloc_size * sizeof(*cm->mi));
chiyotsaia7091f12019-08-09 16:48:27 -0700668 memset(cm->mi_grid_base, 0, mi_grid_size * sizeof(*cm->mi_grid_base));
Hui Su52b7ddc2019-10-10 16:27:16 -0700669 memset(cm->tx_type_map, 0, mi_grid_size * sizeof(*cm->tx_type_map));
chiyotsaia7091f12019-08-09 16:48:27 -0700670}
671
672static int enc_alloc_mi(AV1_COMMON *cm) {
673 const int mi_grid_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
674 const int alloc_size_1d = mi_size_wide[cm->mi_alloc_bsize];
675 const int alloc_mi_size =
676 cm->mi_alloc_stride * (calc_mi_size(cm->mi_rows) / alloc_size_1d);
677
678 if (cm->mi_alloc_size < alloc_mi_size || cm->mi_grid_size < mi_grid_size) {
679 cm->free_mi(cm);
680
681 cm->mi = aom_calloc(alloc_mi_size, sizeof(*cm->mi));
682 if (!cm->mi) return 1;
683 cm->mi_alloc_size = alloc_mi_size;
684
685 cm->mi_grid_base =
686 (MB_MODE_INFO **)aom_calloc(mi_grid_size, sizeof(MB_MODE_INFO *));
687 if (!cm->mi_grid_base) return 1;
688 cm->mi_grid_size = mi_grid_size;
Hui Su52b7ddc2019-10-10 16:27:16 -0700689
690 cm->tx_type_map = aom_calloc(calc_mi_size(cm->mi_rows) * cm->mi_stride,
691 sizeof(*cm->tx_type_map));
692 if (!cm->tx_type_map) return 1;
chiyotsaia7091f12019-08-09 16:48:27 -0700693 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700694
695 return 0;
696}
697
Cheng Chen46f30c72017-09-07 11:13:33 -0700698static void enc_free_mi(AV1_COMMON *cm) {
chiyotsai04ca87d2019-05-24 15:06:19 -0700699 aom_free(cm->mi);
700 cm->mi = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700701 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700702 cm->mi_grid_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700703 cm->mi_alloc_size = 0;
Hui Su52b7ddc2019-10-10 16:27:16 -0700704 aom_free(cm->tx_type_map);
705 cm->tx_type_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700706}
707
Yaowu Xuf883b422016-08-30 14:01:10 -0700708void av1_initialize_enc(void) {
Wan-Teh Chang3cac4542018-06-29 10:21:39 -0700709 av1_rtcd();
710 aom_dsp_rtcd();
711 aom_scale_rtcd();
712 av1_init_intra_predictors();
713 av1_init_me_luts();
714 av1_rc_init_minq_luts();
715 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700716}
717
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700718static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
Remya0cce44c2019-08-16 11:57:24 +0530719 if (cpi->mbmi_ext_frame_base) {
720 aom_free(cpi->mbmi_ext_frame_base);
721 cpi->mbmi_ext_frame_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700722 }
723}
724
725static void alloc_context_buffers_ext(AV1_COMP *cpi) {
726 AV1_COMMON *cm = &cpi->common;
chiyotsaia7091f12019-08-09 16:48:27 -0700727 const int new_ext_mi_size = cm->mi_alloc_rows * cm->mi_alloc_cols;
chiyotsai426c0662019-08-05 16:15:11 -0700728
chiyotsaia7091f12019-08-09 16:48:27 -0700729 if (new_ext_mi_size > cpi->mi_ext_alloc_size) {
730 dealloc_context_buffers_ext(cpi);
Remya0cce44c2019-08-16 11:57:24 +0530731 CHECK_MEM_ERROR(
732 cm, cpi->mbmi_ext_frame_base,
733 aom_calloc(new_ext_mi_size, sizeof(*cpi->mbmi_ext_frame_base)));
chiyotsaia7091f12019-08-09 16:48:27 -0700734 cpi->mi_ext_alloc_size = new_ext_mi_size;
735 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700736}
737
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800738static void reset_film_grain_chroma_params(aom_film_grain_t *pars) {
739 pars->num_cr_points = 0;
740 pars->cr_mult = 0;
741 pars->cr_luma_mult = 0;
742 memset(pars->scaling_points_cr, 0, sizeof(pars->scaling_points_cr));
743 memset(pars->ar_coeffs_cr, 0, sizeof(pars->ar_coeffs_cr));
744 pars->num_cb_points = 0;
745 pars->cb_mult = 0;
746 pars->cb_luma_mult = 0;
Yaowu Xufda7dcb2019-01-16 13:04:33 -0800747 pars->chroma_scaling_from_luma = 0;
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800748 memset(pars->scaling_points_cb, 0, sizeof(pars->scaling_points_cb));
749 memset(pars->ar_coeffs_cb, 0, sizeof(pars->ar_coeffs_cb));
750}
751
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800752static void update_film_grain_parameters(struct AV1_COMP *cpi,
753 const AV1EncoderConfig *oxcf) {
754 AV1_COMMON *const cm = &cpi->common;
755 cpi->oxcf = *oxcf;
756
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700757 if (cpi->film_grain_table) {
758 aom_film_grain_table_free(cpi->film_grain_table);
759 aom_free(cpi->film_grain_table);
760 cpi->film_grain_table = NULL;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700761 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700762
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800763 if (oxcf->film_grain_test_vector) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700764 cm->seq_params.film_grain_params_present = 1;
David Turnerd2a592e2018-11-16 14:59:31 +0000765 if (cm->current_frame.frame_type == KEY_FRAME) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800766 memcpy(&cm->film_grain_params,
767 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
768 sizeof(cm->film_grain_params));
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800769 if (oxcf->monochrome)
770 reset_film_grain_chroma_params(&cm->film_grain_params);
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700771 cm->film_grain_params.bit_depth = cm->seq_params.bit_depth;
772 if (cm->seq_params.color_range == AOM_CR_FULL_RANGE) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800773 cm->film_grain_params.clip_to_restricted_range = 0;
774 }
775 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700776 } else if (oxcf->film_grain_table_filename) {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800777 cm->seq_params.film_grain_params_present = 1;
778
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700779 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
780 memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700781
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700782 aom_film_grain_table_read(cpi->film_grain_table,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700783 oxcf->film_grain_table_filename, &cm->error);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800784 } else {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800785#if CONFIG_DENOISE
786 cm->seq_params.film_grain_params_present = (cpi->oxcf.noise_level > 0);
787#else
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700788 cm->seq_params.film_grain_params_present = 0;
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800789#endif
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800790 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
791 }
792}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800793
Yaowu Xuf883b422016-08-30 14:01:10 -0700794static void dealloc_compressor_data(AV1_COMP *cpi) {
795 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000796 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700797
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700798 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700799
Yaowu Xuf883b422016-08-30 14:01:10 -0700800 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700801 cpi->tile_data = NULL;
802
803 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700804 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700805 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700806
Yaowu Xuf883b422016-08-30 14:01:10 -0700807 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700808 cpi->cyclic_refresh = NULL;
809
Yaowu Xuf883b422016-08-30 14:01:10 -0700810 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700811 cpi->active_map.map = NULL;
812
sdengc23c7f12019-06-11 16:56:50 -0700813 aom_free(cpi->ssim_rdmult_scaling_factors);
814 cpi->ssim_rdmult_scaling_factors = NULL;
815
sdengf46a1062019-08-04 18:43:50 -0700816 aom_free(cpi->tpl_rdmult_scaling_factors);
817 cpi->tpl_rdmult_scaling_factors = NULL;
818
819 aom_free(cpi->tpl_sb_rdmult_scaling_factors);
820 cpi->tpl_sb_rdmult_scaling_factors = NULL;
821
sdeng01959162019-12-20 10:46:24 -0800822#if CONFIG_TUNE_VMAF
823 aom_free(cpi->vmaf_rdmult_scaling_factors);
824 cpi->vmaf_rdmult_scaling_factors = NULL;
825#endif
826
Jingning Hand064cf02017-06-01 10:00:39 -0700827 aom_free(cpi->td.mb.above_pred_buf);
828 cpi->td.mb.above_pred_buf = NULL;
829
830 aom_free(cpi->td.mb.left_pred_buf);
831 cpi->td.mb.left_pred_buf = NULL;
832
833 aom_free(cpi->td.mb.wsrc_buf);
834 cpi->td.mb.wsrc_buf = NULL;
835
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530836 aom_free(cpi->td.mb.inter_modes_info);
837 cpi->td.mb.inter_modes_info = NULL;
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530838
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530839 for (int i = 0; i < 2; i++)
840 for (int j = 0; j < 2; j++) {
841 aom_free(cpi->td.mb.hash_value_buffer[i][j]);
842 cpi->td.mb.hash_value_buffer[i][j] = NULL;
843 }
Jingning Hand064cf02017-06-01 10:00:39 -0700844 aom_free(cpi->td.mb.mask_buf);
845 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700846
Jingning Han6cc1fd32017-10-13 09:05:36 -0700847 aom_free(cm->tpl_mvs);
848 cm->tpl_mvs = NULL;
Jingning Han6cc1fd32017-10-13 09:05:36 -0700849
Remya0cce44c2019-08-16 11:57:24 +0530850 aom_free(cpi->td.mb.mbmi_ext);
851 cpi->td.mb.mbmi_ext = NULL;
852
Yaowu Xuf883b422016-08-30 14:01:10 -0700853 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700854 av1_free_txb_buf(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700855 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700856
Yaowu Xuf883b422016-08-30 14:01:10 -0700857 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuf883b422016-08-30 14:01:10 -0700858 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800859 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuf883b422016-08-30 14:01:10 -0700860 aom_free_frame_buffer(&cpi->scaled_source);
861 aom_free_frame_buffer(&cpi->scaled_last_source);
862 aom_free_frame_buffer(&cpi->alt_ref_buffer);
863 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700864
Yaowu Xuf883b422016-08-30 14:01:10 -0700865 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700866 cpi->tile_tok[0][0] = 0;
867
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530868 aom_free(cpi->tplist[0][0]);
869 cpi->tplist[0][0] = NULL;
870
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000871 av1_free_pc_tree(&cpi->td, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700872
hui sud9a812b2017-07-06 14:34:37 -0700873 aom_free(cpi->td.mb.palette_buffer);
Hui Su38711e72019-06-11 10:49:47 -0700874 av1_release_compound_type_rd_buffers(&cpi->td.mb.comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700875 aom_free(cpi->td.mb.tmp_conv_dst);
876 for (int j = 0; j < 2; ++j) {
877 aom_free(cpi->td.mb.tmp_obmc_bufs[j]);
878 }
879
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700880#if CONFIG_DENOISE
881 if (cpi->denoise_and_model) {
882 aom_denoise_and_model_free(cpi->denoise_and_model);
883 cpi->denoise_and_model = NULL;
884 }
885#endif
886 if (cpi->film_grain_table) {
887 aom_film_grain_table_free(cpi->film_grain_table);
888 cpi->film_grain_table = NULL;
889 }
Hui Suc3a8d372019-05-28 15:52:45 -0700890
891 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
892 aom_free(cpi->level_info[i]);
893 }
Marco Paniconi63971322019-08-15 21:32:05 -0700894
895 if (cpi->use_svc) av1_free_svc_cyclic_refresh(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700896}
897
Yaowu Xuf883b422016-08-30 14:01:10 -0700898static void configure_static_seg_features(AV1_COMP *cpi) {
899 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700900 const RATE_CONTROL *const rc = &cpi->rc;
901 struct segmentation *const seg = &cm->seg;
902
903 int high_q = (int)(rc->avg_q > 48.0);
904 int qi_delta;
905
906 // Disable and clear down for KF
David Turnerd2a592e2018-11-16 14:59:31 +0000907 if (cm->current_frame.frame_type == KEY_FRAME) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700908 // Clear down the global segmentation map
909 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
910 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
922 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
923 seg->update_map = 0;
924 seg->update_data = 0;
925 cpi->static_mb_pct = 0;
926
927 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700928 av1_disable_segmentation(seg);
929 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700930
Yaowu Xuc27fc142016-08-22 16:08:15 -0700931 // If segmentation was enabled set those features needed for the
932 // arf itself.
933 if (seg->enabled) {
934 seg->update_map = 1;
935 seg->update_data = 1;
936
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700937 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
938 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700939 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700940 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
941 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
942 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
943 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
944
945 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
946 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
947 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
948 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700949
Yaowu Xuf883b422016-08-30 14:01:10 -0700950 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700951 }
952 } else if (seg->enabled) {
953 // All other frames if segmentation has been enabled
954
955 // First normal frame in a valid gf or alt ref group
956 if (rc->frames_since_golden == 0) {
957 // Set up segment features for normal frames in an arf group
958 if (rc->source_alt_ref_active) {
959 seg->update_map = 0;
960 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700961
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700962 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
963 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700964 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
965 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700966
Cheng Chend8184da2017-09-26 18:15:22 -0700967 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
968 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
969 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
970 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
971
972 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
973 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
974 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
975 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700976
977 // Segment coding disabled for compred testing
978 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700979 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
980 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
981 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700982 }
983 } else {
984 // Disable segmentation and clear down features if alt ref
985 // is not active for this group
986
Yaowu Xuf883b422016-08-30 14:01:10 -0700987 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700988
989 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
990
991 seg->update_map = 0;
992 seg->update_data = 0;
993
Yaowu Xuf883b422016-08-30 14:01:10 -0700994 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700995 }
996 } else if (rc->is_src_frame_alt_ref) {
997 // Special case where we are coding over the top of a previous
998 // alt ref frame.
999 // Segment coding disabled for compred testing
1000
1001 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -07001002 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
1003 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001004
1005 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -07001006 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
1007 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
1008 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
1009 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001010
1011 // Skip all MBs if high Q (0,0 mv and skip coeffs)
1012 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001013 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
1014 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001015 }
1016 // Enable data update
1017 seg->update_data = 1;
1018 } else {
1019 // All other frames.
1020
1021 // No updates.. leave things as they are.
1022 seg->update_map = 0;
1023 seg->update_data = 0;
1024 }
1025 }
1026}
1027
Yaowu Xuf883b422016-08-30 14:01:10 -07001028static void update_reference_segmentation_map(AV1_COMP *cpi) {
1029 AV1_COMMON *const cm = &cpi->common;
chiyotsai2202ba02019-07-12 16:09:21 -07001030 MB_MODE_INFO **mi_4x4_ptr = cm->mi_grid_base;
David Turnerb757ce02018-11-12 15:01:28 +00001031 uint8_t *cache_ptr = cm->cur_frame->seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001032 int row, col;
1033
1034 for (row = 0; row < cm->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -07001035 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001036 uint8_t *cache = cache_ptr;
Yushin Choa7f65922018-04-04 16:06:11 -07001037 for (col = 0; col < cm->mi_cols; col++, mi_4x4++, cache++)
1038 cache[0] = mi_4x4[0]->segment_id;
1039 mi_4x4_ptr += cm->mi_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001040 cache_ptr += cm->mi_cols;
1041 }
1042}
1043
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05301044static void alloc_altref_frame_buffer(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001045 AV1_COMMON *cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001046 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -07001047 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001048
Yaowu Xuc27fc142016-08-22 16:08:15 -07001049 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001050 if (aom_realloc_frame_buffer(
1051 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
1052 seq_params->subsampling_x, seq_params->subsampling_y,
Yunqing Wanga40d9e82020-02-21 16:37:38 -08001053 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001054 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07001055 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001056 "Failed to allocate altref buffer");
1057}
1058
Yaowu Xuf883b422016-08-30 14:01:10 -07001059static void alloc_util_frame_buffers(AV1_COMP *cpi) {
1060 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001061 const SequenceHeader *const seq_params = &cm->seq_params;
1062 if (aom_realloc_frame_buffer(
1063 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
1064 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +05301065 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07001066 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001067 "Failed to allocate last frame buffer");
1068
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07001069 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08001070 &cpi->trial_frame_rst, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001071 cm->superres_upscaled_height, seq_params->subsampling_x,
1072 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman3b12c002018-12-19 15:27:20 +05301073 AOM_RESTORATION_FRAME_BORDER, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -08001074 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -08001075 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -07001076
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001077 if (aom_realloc_frame_buffer(
1078 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
1079 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +05301080 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07001081 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001082 "Failed to allocate scaled source buffer");
1083
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001084 if (aom_realloc_frame_buffer(
1085 &cpi->scaled_last_source, cm->width, cm->height,
1086 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +05301087 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001088 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07001089 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001090 "Failed to allocate scaled last source buffer");
1091}
1092
Cheng Chen46f30c72017-09-07 11:13:33 -07001093static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001094 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001095 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001096
chiyotsaia7091f12019-08-09 16:48:27 -07001097 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
1098 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1099 "Failed to allocate context buffers");
1100 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001101
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +05301102 int mi_rows_aligned_to_sb =
1103 ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
1104 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
1105
Angie Chiangf0fbf9d2017-03-15 15:01:22 -07001106 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -07001107
Yaowu Xuc27fc142016-08-22 16:08:15 -07001108 alloc_context_buffers_ext(cpi);
1109
Yaowu Xuf883b422016-08-30 14:01:10 -07001110 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001111
1112 {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001113 unsigned int tokens =
1114 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001115 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -07001116 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001117 }
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +05301118 aom_free(cpi->tplist[0][0]);
1119
1120 CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
1121 aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
1122 sizeof(*cpi->tplist[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001123
Yaowu Xuf883b422016-08-30 14:01:10 -07001124 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001125}
1126
Yaowu Xuf883b422016-08-30 14:01:10 -07001127void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001128 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001129 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001130}
1131
Hui Suef139e12019-05-20 15:51:22 -07001132double av1_get_compression_ratio(const AV1_COMMON *const cm,
1133 size_t encoded_frame_size) {
1134 const int upscaled_width = cm->superres_upscaled_width;
1135 const int height = cm->height;
1136 const int luma_pic_size = upscaled_width * height;
1137 const SequenceHeader *const seq_params = &cm->seq_params;
1138 const BITSTREAM_PROFILE profile = seq_params->profile;
1139 const int pic_size_profile_factor =
1140 profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
1141 encoded_frame_size =
1142 (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
1143 const size_t uncompressed_frame_size =
1144 (luma_pic_size * pic_size_profile_factor) >> 3;
1145 return uncompressed_frame_size / (double)encoded_frame_size;
1146}
1147
Yunqing Wang75e20e82018-06-16 12:10:48 -07001148static void set_tile_info(AV1_COMP *cpi) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001149 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +02001150 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001151
1152 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001153
1154 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +02001155 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +02001156 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001157 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
1158 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +02001159 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001160 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
1161 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001162 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +02001163 cm->uniform_tile_spacing_flag = 0;
1164 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
1165 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001166 size_sb = cpi->oxcf.tile_widths[j++];
1167 if (j >= cpi->oxcf.tile_width_count) j = 0;
David Barker6cd5a822018-03-05 16:19:28 +00001168 start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +02001169 }
1170 cm->tile_cols = i;
1171 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001172 }
1173 av1_calculate_tile_cols(cm);
1174
1175 // configure tile rows
1176 if (cm->uniform_tile_spacing_flag) {
1177 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
1178 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +02001179 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001180 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
1181 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001182 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +02001183 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
1184 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001185 size_sb = cpi->oxcf.tile_heights[j++];
1186 if (j >= cpi->oxcf.tile_height_count) j = 0;
1187 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +02001188 }
1189 cm->tile_rows = i;
1190 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001191 }
1192 av1_calculate_tile_rows(cm);
1193}
1194
Yaowu Xuf883b422016-08-30 14:01:10 -07001195static void update_frame_size(AV1_COMP *cpi) {
1196 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001197 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
1198
chiyotsaia7091f12019-08-09 16:48:27 -07001199 // We need to reallocate the context buffers here in case we need more mis.
1200 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
1201 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1202 "Failed to allocate context buffers");
1203 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001204 av1_init_context_buffers(cm);
chiyotsaia7091f12019-08-09 16:48:27 -07001205
Luc Trudeau1e84af52017-11-25 15:00:28 -05001206 av1_init_macroblockd(cm, xd, NULL);
chiyotsai426c0662019-08-05 16:15:11 -07001207
chiyotsaia7091f12019-08-09 16:48:27 -07001208 const int ext_mi_size = cm->mi_alloc_rows * cm->mi_alloc_cols;
1209 alloc_context_buffers_ext(cpi);
Remya0cce44c2019-08-16 11:57:24 +05301210 memset(cpi->mbmi_ext_frame_base, 0,
1211 ext_mi_size * sizeof(*cpi->mbmi_ext_frame_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001212 set_tile_info(cpi);
1213}
1214
Yaowu Xuf883b422016-08-30 14:01:10 -07001215static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001216 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -07001217 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
David Turnera21966b2018-12-05 14:48:49 +00001218 cpi->common.remapped_ref_idx[fb_idx] = fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -07001219 cpi->rate_index = 0;
1220 cpi->rate_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001221}
1222
Debargha Mukherjee57498692018-05-11 13:29:31 -07001223static INLINE int does_level_match(int width, int height, double fps,
1224 int lvl_width, int lvl_height,
1225 double lvl_fps, int lvl_dim_mult) {
1226 const int64_t lvl_luma_pels = lvl_width * lvl_height;
1227 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
1228 const int64_t luma_pels = width * height;
1229 const double display_sample_rate = luma_pels * fps;
1230 return luma_pels <= lvl_luma_pels &&
1231 display_sample_rate <= lvl_display_sample_rate &&
1232 width <= lvl_width * lvl_dim_mult &&
1233 height <= lvl_height * lvl_dim_mult;
1234}
1235
Andrey Norkin26495512018-06-20 17:13:11 -07001236static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Andrey Norkinf481d982018-05-15 12:05:31 -07001237 const AV1EncoderConfig *oxcf) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001238 // TODO(any): This is a placeholder function that only addresses dimensions
1239 // and max display sample rates.
1240 // Need to add checks for max bit rate, max decoded luma sample rate, header
1241 // rate, etc. that are not covered by this function.
Hui Su8427ff22019-03-11 10:14:33 -07001242 AV1_LEVEL level = SEQ_LEVEL_MAX;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001243 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
1244 288, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001245 level = SEQ_LEVEL_2_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001246 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1247 704, 396, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001248 level = SEQ_LEVEL_2_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001249 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1250 1088, 612, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001251 level = SEQ_LEVEL_3_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001252 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1253 1376, 774, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001254 level = SEQ_LEVEL_3_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001255 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1256 2048, 1152, 30.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001257 level = SEQ_LEVEL_4_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001258 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1259 2048, 1152, 60.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001260 level = SEQ_LEVEL_4_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001261 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1262 4096, 2176, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001263 level = SEQ_LEVEL_5_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001264 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1265 4096, 2176, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001266 level = SEQ_LEVEL_5_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001267 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1268 4096, 2176, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001269 level = SEQ_LEVEL_5_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001270 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1271 8192, 4352, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001272 level = SEQ_LEVEL_6_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001273 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1274 8192, 4352, 60.0, 2)) {
Todd Nguyenff4c8602019-11-25 13:20:21 -08001275 level = SEQ_LEVEL_6_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001276 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1277 8192, 4352, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001278 level = SEQ_LEVEL_6_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001279 }
chiyotsaiaa33bbf2019-12-02 12:07:53 -08001280
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001281 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su8427ff22019-03-11 10:14:33 -07001282 seq->seq_level_idx[i] = level;
Andrey Norkin26495512018-06-20 17:13:11 -07001283 // Set the maximum parameters for bitrate and buffer size for this profile,
1284 // level, and tier
Yaowu Xu7e450882019-04-30 15:09:18 -07001285 cm->op_params[i].bitrate = av1_max_level_bitrate(
Hui Su8427ff22019-03-11 10:14:33 -07001286 cm->seq_params.profile, seq->seq_level_idx[i], seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001287 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
1288 // check
Andrey Norkin26495512018-06-20 17:13:11 -07001289 if (cm->op_params[i].bitrate == 0)
1290 aom_internal_error(
1291 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
1292 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -07001293 // Buffer size in bits/s is bitrate in bits/s * 1 s
Andrey Norkin26495512018-06-20 17:13:11 -07001294 cm->op_params[i].buffer_size = cm->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001295 }
1296}
1297
Andrey Norkin26495512018-06-20 17:13:11 -07001298static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
Marco Paniconi67142112019-07-24 15:00:31 -07001299 const AV1EncoderConfig *oxcf, int use_svc) {
Yaowu Xu2a9ac432019-08-06 14:21:17 -07001300 seq->still_picture = (oxcf->force_video_mode == 0) && (oxcf->limit == 1);
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001301 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -07001302 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
kyslov94243382019-05-02 15:33:32 -07001303 seq->force_screen_content_tools = (oxcf->mode == REALTIME) ? 0 : 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001304 seq->force_integer_mv = 2;
David Turnerebf96f42018-11-14 16:57:57 +00001305 seq->order_hint_info.enable_order_hint = oxcf->enable_order_hint;
David Turner936235c2018-11-28 13:42:01 +00001306 seq->frame_id_numbers_present_flag =
1307 !(seq->still_picture && seq->reduced_still_picture_hdr) &&
Marco Paniconi67142112019-07-24 15:00:31 -07001308 !oxcf->large_scale_tile && oxcf->error_resilient_mode && !use_svc;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001309 if (seq->still_picture && seq->reduced_still_picture_hdr) {
David Turnerebf96f42018-11-14 16:57:57 +00001310 seq->order_hint_info.enable_order_hint = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001311 seq->force_screen_content_tools = 2;
1312 seq->force_integer_mv = 2;
1313 }
David Turnerebf96f42018-11-14 16:57:57 +00001314 seq->order_hint_info.order_hint_bits_minus_1 =
1315 seq->order_hint_info.enable_order_hint
1316 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
1317 : -1;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001318
David Turner760a2f42018-12-07 15:25:36 +00001319 seq->max_frame_width =
1320 oxcf->forced_max_frame_width ? oxcf->forced_max_frame_width : oxcf->width;
1321 seq->max_frame_height = oxcf->forced_max_frame_height
1322 ? oxcf->forced_max_frame_height
1323 : oxcf->height;
1324 seq->num_bits_width =
1325 (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
1326 seq->num_bits_height =
1327 (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
1328 assert(seq->num_bits_width <= 16);
1329 assert(seq->num_bits_height <= 16);
1330
1331 seq->frame_id_length = FRAME_ID_LENGTH;
1332 seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
1333
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001334 seq->enable_dual_filter = oxcf->enable_dual_filter;
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -08001335 seq->order_hint_info.enable_dist_wtd_comp = oxcf->enable_dist_wtd_comp;
1336 seq->order_hint_info.enable_dist_wtd_comp &=
David Turnerebf96f42018-11-14 16:57:57 +00001337 seq->order_hint_info.enable_order_hint;
1338 seq->order_hint_info.enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1339 seq->order_hint_info.enable_ref_frame_mvs &=
1340 seq->order_hint_info.enable_order_hint;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001341 seq->enable_superres = oxcf->enable_superres;
1342 seq->enable_cdef = oxcf->enable_cdef;
1343 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -07001344 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -08001345 seq->enable_interintra_compound = oxcf->enable_interintra_comp;
1346 seq->enable_masked_compound = oxcf->enable_masked_comp;
Debargha Mukherjee03c43ba2018-12-14 13:08:08 -08001347 seq->enable_intra_edge_filter = oxcf->enable_intra_edge_filter;
Yue Chen8f9ca582018-12-12 15:11:47 -08001348 seq->enable_filter_intra = oxcf->enable_filter_intra;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001349
Andrey Norkin26495512018-06-20 17:13:11 -07001350 set_bitstream_level_tier(seq, cm, oxcf);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001351
1352 if (seq->operating_points_cnt_minus_1 == 0) {
1353 seq->operating_point_idc[0] = 0;
1354 } else {
Marco Paniconi6da983b2019-09-16 20:12:43 -07001355 // Set operating_point_idc[] such that the i=0 point corresponds to the
1356 // highest quality operating point (all layers), and subsequent
1357 // operarting points (i > 0) are lower quality corresponding to
1358 // skip decoding enhancement layers (temporal first).
1359 int i = 0;
1360 assert(seq->operating_points_cnt_minus_1 ==
1361 (int)(cm->number_spatial_layers * cm->number_temporal_layers - 1));
1362 for (unsigned int sl = 0; sl < cm->number_spatial_layers; sl++) {
1363 for (unsigned int tl = 0; tl < cm->number_temporal_layers; tl++) {
1364 seq->operating_point_idc[i] =
1365 (~(~0u << (cm->number_spatial_layers - sl)) << 8) |
1366 ~(~0u << (cm->number_temporal_layers - tl));
1367 i++;
1368 }
1369 }
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001370 }
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001371}
1372
Yaowu Xuf883b422016-08-30 14:01:10 -07001373static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1374 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001375
1376 cpi->oxcf = *oxcf;
1377 cpi->framerate = oxcf->init_framerate;
1378
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001379 cm->seq_params.profile = oxcf->profile;
1380 cm->seq_params.bit_depth = oxcf->bit_depth;
1381 cm->seq_params.use_highbitdepth = oxcf->use_highbitdepth;
1382 cm->seq_params.color_primaries = oxcf->color_primaries;
1383 cm->seq_params.transfer_characteristics = oxcf->transfer_characteristics;
1384 cm->seq_params.matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001385 cm->seq_params.monochrome = oxcf->monochrome;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001386 cm->seq_params.chroma_sample_position = oxcf->chroma_sample_position;
1387 cm->seq_params.color_range = oxcf->color_range;
Andrey Norkin28e9ce22018-01-08 10:11:21 -08001388 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08001389 cm->timing_info.num_units_in_display_tick =
1390 oxcf->timing_info.num_units_in_display_tick;
1391 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
1392 cm->timing_info.equal_picture_interval =
1393 oxcf->timing_info.equal_picture_interval;
1394 cm->timing_info.num_ticks_per_picture =
1395 oxcf->timing_info.num_ticks_per_picture;
1396
Andrey Norkin26495512018-06-20 17:13:11 -07001397 cm->seq_params.display_model_info_present_flag =
1398 oxcf->display_model_info_present_flag;
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001399 cm->seq_params.decoder_model_info_present_flag =
1400 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08001401 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001402 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08001403 cm->buffer_model.num_units_in_decoding_tick =
1404 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07001405 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07001406 av1_set_aom_dec_model_info(&cm->buffer_model);
1407 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07001408 } else if (cm->timing_info_present &&
1409 cm->timing_info.equal_picture_interval &&
1410 !cm->seq_params.decoder_model_info_present_flag) {
1411 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07001412 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001413 } else {
1414 cm->op_params[0].initial_display_delay =
1415 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08001416 }
Andrey Norkinc7511de2018-06-22 12:31:06 -07001417
Tom Fineganf8d6a162018-08-21 10:47:55 -07001418 if (cm->seq_params.monochrome) {
1419 cm->seq_params.subsampling_x = 1;
1420 cm->seq_params.subsampling_y = 1;
1421 } else if (cm->seq_params.color_primaries == AOM_CICP_CP_BT_709 &&
1422 cm->seq_params.transfer_characteristics == AOM_CICP_TC_SRGB &&
1423 cm->seq_params.matrix_coefficients == AOM_CICP_MC_IDENTITY) {
1424 cm->seq_params.subsampling_x = 0;
1425 cm->seq_params.subsampling_y = 0;
1426 } else {
1427 if (cm->seq_params.profile == 0) {
1428 cm->seq_params.subsampling_x = 1;
1429 cm->seq_params.subsampling_y = 1;
1430 } else if (cm->seq_params.profile == 1) {
1431 cm->seq_params.subsampling_x = 0;
1432 cm->seq_params.subsampling_y = 0;
1433 } else {
1434 if (cm->seq_params.bit_depth == AOM_BITS_12) {
1435 cm->seq_params.subsampling_x = oxcf->chroma_subsampling_x;
1436 cm->seq_params.subsampling_y = oxcf->chroma_subsampling_y;
1437 } else {
1438 cm->seq_params.subsampling_x = 1;
1439 cm->seq_params.subsampling_y = 0;
1440 }
1441 }
Tom Finegan02b2a842018-08-24 13:50:00 -07001442 }
1443
Yaowu Xuc27fc142016-08-22 16:08:15 -07001444 cm->width = oxcf->width;
1445 cm->height = oxcf->height;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001446 set_sb_size(&cm->seq_params,
1447 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -07001448 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -07001449
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08001450 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001451
1452 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -07001453 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001454
Marco Paniconi882c5ad2019-09-30 21:32:29 -07001455 // Set init SVC parameters.
Marco Paniconi67142112019-07-24 15:00:31 -07001456 cpi->use_svc = 0;
Marco Paniconid8574e32019-08-04 21:30:12 -07001457 cpi->svc.external_ref_frame_config = 0;
1458 cpi->svc.non_reference_frame = 0;
Marco Paniconi67142112019-07-24 15:00:31 -07001459 cm->number_spatial_layers = 1;
1460 cm->number_temporal_layers = 1;
Marco Paniconi882c5ad2019-09-30 21:32:29 -07001461 cm->spatial_layer_id = 0;
1462 cm->temporal_layer_id = 0;
Marco Paniconi67142112019-07-24 15:00:31 -07001463
Yaowu Xuc27fc142016-08-22 16:08:15 -07001464 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001465 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001466
1467 cpi->static_mb_pct = 0;
1468 cpi->ref_frame_flags = 0;
1469
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001470 // Reset resize pending flags
1471 cpi->resize_pending_width = 0;
1472 cpi->resize_pending_height = 0;
1473
Yaowu Xuc27fc142016-08-22 16:08:15 -07001474 init_buffer_indices(cpi);
1475}
1476
1477static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001478 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001479 const int64_t bandwidth = oxcf->target_bandwidth;
1480 const int64_t starting = oxcf->starting_buffer_level_ms;
1481 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1482 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1483
1484 rc->starting_buffer_level = starting * bandwidth / 1000;
1485 rc->optimal_buffer_level =
1486 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1487 rc->maximum_buffer_size =
1488 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1489}
1490
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001491#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1492 cpi->fn_ptr[BT].sdf = SDF; \
1493 cpi->fn_ptr[BT].sdaf = SDAF; \
1494 cpi->fn_ptr[BT].vf = VF; \
1495 cpi->fn_ptr[BT].svf = SVF; \
1496 cpi->fn_ptr[BT].svaf = SVAF; \
1497 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1498 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001499 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001500
1501#define MAKE_BFP_SAD_WRAPPER(fnname) \
1502 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1503 int source_stride, \
1504 const uint8_t *ref_ptr, int ref_stride) { \
1505 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1506 } \
1507 static unsigned int fnname##_bits10( \
1508 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1509 int ref_stride) { \
1510 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1511 } \
1512 static unsigned int fnname##_bits12( \
1513 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1514 int ref_stride) { \
1515 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1516 }
1517
1518#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1519 static unsigned int fnname##_bits8( \
1520 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1521 int ref_stride, const uint8_t *second_pred) { \
1522 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1523 } \
1524 static unsigned int fnname##_bits10( \
1525 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1526 int ref_stride, const uint8_t *second_pred) { \
1527 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1528 2; \
1529 } \
1530 static unsigned int fnname##_bits12( \
1531 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1532 int ref_stride, const uint8_t *second_pred) { \
1533 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1534 4; \
1535 }
1536
Yaowu Xuc27fc142016-08-22 16:08:15 -07001537#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1538 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1539 const uint8_t *const ref_ptr[], int ref_stride, \
1540 unsigned int *sad_array) { \
1541 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1542 } \
1543 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1544 const uint8_t *const ref_ptr[], int ref_stride, \
1545 unsigned int *sad_array) { \
1546 int i; \
1547 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1548 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1549 } \
1550 static void fnname##_bits12(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] >>= 4; \
1556 }
1557
Cheng Chenbf3d4962017-11-01 14:48:52 -07001558#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1559 static unsigned int fnname##_bits8( \
1560 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1561 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001562 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001563 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1564 jcp_param); \
1565 } \
1566 static unsigned int fnname##_bits10( \
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 2; \
1573 } \
1574 static unsigned int fnname##_bits12( \
1575 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1576 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001577 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001578 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1579 jcp_param) >> \
1580 4; \
1581 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001582
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001583#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001584MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1585MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001586MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1587MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1588MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1589MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1590MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1591MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1592MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001593MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1594MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1595MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1596MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1597MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1598MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1599MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1600MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1601MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1602MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1603MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1604MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1605MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1606MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001607MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1608MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1609MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001610MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1611MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1612MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001613MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1614MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1615MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001616MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1617MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1618MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001619MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1620MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1621MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001622MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1623MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1624MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001625MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1626MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1627MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001628MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1629MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1630MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001631MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001632
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001633MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1634MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1635MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1636MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1637MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1638MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1639MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1640MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1641MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1642MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1643MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1644MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001645MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1646MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1647MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1648MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1649MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1650MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001651
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001652MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x128_avg)
1653MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x64_avg)
1654MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x128_avg)
1655MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x16_avg)
1656MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x32_avg)
1657MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x32_avg)
1658MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x64_avg)
1659MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x32_avg)
1660MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x64_avg)
1661MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x16_avg)
1662MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x8_avg)
1663MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x16_avg)
1664MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x8_avg)
1665MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x4_avg)
1666MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x8_avg)
1667MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x4_avg)
1668MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x16_avg)
1669MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x4_avg)
1670MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x32_avg)
1671MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x8_avg)
1672MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x64_avg)
1673MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x16_avg)
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001674#endif // CONFIG_AV1_HIGHBITDEPTH
Cheng Chenbf3d4962017-11-01 14:48:52 -07001675
David Barker0f3c94e2017-05-16 15:21:50 +01001676#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001677 cpi->fn_ptr[BT].msdf = MCSDF; \
1678 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001679
David Barkerc155e012017-05-11 13:54:54 +01001680#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1681 static unsigned int fnname##_bits8( \
1682 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1683 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1684 int m_stride, int invert_mask) { \
1685 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1686 second_pred_ptr, m, m_stride, invert_mask); \
1687 } \
1688 static unsigned int fnname##_bits10( \
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 2; \
1695 } \
1696 static unsigned int fnname##_bits12( \
1697 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1698 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1699 int m_stride, int invert_mask) { \
1700 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1701 second_pred_ptr, m, m_stride, invert_mask) >> \
1702 4; \
1703 }
1704
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001705#if CONFIG_AV1_HIGHBITDEPTH
David Barkerf19f35f2017-05-22 16:33:22 +01001706MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1707MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1708MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001709MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1710MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1711MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1712MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1713MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1714MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1715MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1716MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1717MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1718MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1719MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1720MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1721MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001722MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1723MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1724MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1725MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001726MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1727MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001728#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001729
Yaowu Xuc27fc142016-08-22 16:08:15 -07001730#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1731 cpi->fn_ptr[BT].osdf = OSDF; \
1732 cpi->fn_ptr[BT].ovf = OVF; \
1733 cpi->fn_ptr[BT].osvf = OSVF;
1734
1735#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1736 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1737 const int32_t *wsrc, \
1738 const int32_t *msk) { \
1739 return fnname(ref, ref_stride, wsrc, msk); \
1740 } \
1741 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1742 const int32_t *wsrc, \
1743 const int32_t *msk) { \
1744 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1745 } \
1746 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1747 const int32_t *wsrc, \
1748 const int32_t *msk) { \
1749 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1750 }
1751
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001752#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001753MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1754MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1755MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001756MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1757MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1758MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1759MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1760MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1761MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1762MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1763MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1764MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1765MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1766MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1767MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1768MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001769MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1770MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1771MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1772MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001773MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1774MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001775
Yaowu Xuf883b422016-08-30 14:01:10 -07001776static void highbd_set_var_fns(AV1_COMP *const cpi) {
1777 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001778 if (cm->seq_params.use_highbitdepth) {
1779 switch (cm->seq_params.bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001780 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001781 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1782 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1783 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001784 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001785 aom_highbd_sad64x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001786 aom_highbd_dist_wtd_sad64x16_avg_bits8,
1787 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001788
1789 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1790 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1791 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001792 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001793 aom_highbd_sad16x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001794 aom_highbd_dist_wtd_sad16x64_avg_bits8,
1795 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001796
1797 HIGHBD_BFP(
1798 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1799 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001800 aom_highbd_8_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001801 aom_highbd_sad32x8x4d_bits8, aom_highbd_dist_wtd_sad32x8_avg_bits8,
1802 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001803
1804 HIGHBD_BFP(
1805 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1806 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001807 aom_highbd_8_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001808 aom_highbd_sad8x32x4d_bits8, aom_highbd_dist_wtd_sad8x32_avg_bits8,
1809 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001810
1811 HIGHBD_BFP(
1812 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1813 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001814 aom_highbd_8_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001815 aom_highbd_sad16x4x4d_bits8, aom_highbd_dist_wtd_sad16x4_avg_bits8,
1816 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001817
1818 HIGHBD_BFP(
1819 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1820 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001821 aom_highbd_8_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001822 aom_highbd_sad4x16x4d_bits8, aom_highbd_dist_wtd_sad4x16_avg_bits8,
1823 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001824
1825 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1826 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1827 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001828 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001829 aom_highbd_sad32x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001830 aom_highbd_dist_wtd_sad32x16_avg_bits8,
1831 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001832
1833 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1834 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1835 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001836 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001837 aom_highbd_sad16x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001838 aom_highbd_dist_wtd_sad16x32_avg_bits8,
1839 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001840
1841 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1842 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1843 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001844 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001845 aom_highbd_sad64x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001846 aom_highbd_dist_wtd_sad64x32_avg_bits8,
1847 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001848
1849 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1850 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1851 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001852 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001853 aom_highbd_sad32x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001854 aom_highbd_dist_wtd_sad32x64_avg_bits8,
1855 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001856
1857 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1858 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1859 aom_highbd_8_sub_pixel_variance32x32,
1860 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001861 aom_highbd_sad32x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001862 aom_highbd_dist_wtd_sad32x32_avg_bits8,
1863 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001864
1865 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1866 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1867 aom_highbd_8_sub_pixel_variance64x64,
1868 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001869 aom_highbd_sad64x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001870 aom_highbd_dist_wtd_sad64x64_avg_bits8,
1871 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001872
1873 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1874 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1875 aom_highbd_8_sub_pixel_variance16x16,
1876 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001877 aom_highbd_sad16x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001878 aom_highbd_dist_wtd_sad16x16_avg_bits8,
1879 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001880
1881 HIGHBD_BFP(
1882 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1883 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001884 aom_highbd_8_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001885 aom_highbd_sad16x8x4d_bits8, aom_highbd_dist_wtd_sad16x8_avg_bits8,
1886 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001887
1888 HIGHBD_BFP(
1889 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1890 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001891 aom_highbd_8_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001892 aom_highbd_sad8x16x4d_bits8, aom_highbd_dist_wtd_sad8x16_avg_bits8,
1893 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001894
Cheng Chenbf3d4962017-11-01 14:48:52 -07001895 HIGHBD_BFP(
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001896 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1897 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1898 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x4d_bits8,
1899 aom_highbd_dist_wtd_sad8x8_avg_bits8,
1900 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x8)
1901
1902 HIGHBD_BFP(
1903 BLOCK_8X4, aom_highbd_sad8x4_bits8, aom_highbd_sad8x4_avg_bits8,
1904 aom_highbd_8_variance8x4, aom_highbd_8_sub_pixel_variance8x4,
1905 aom_highbd_8_sub_pixel_avg_variance8x4, aom_highbd_sad8x4x4d_bits8,
1906 aom_highbd_dist_wtd_sad8x4_avg_bits8,
1907 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x4)
1908
1909 HIGHBD_BFP(
1910 BLOCK_4X8, aom_highbd_sad4x8_bits8, aom_highbd_sad4x8_avg_bits8,
1911 aom_highbd_8_variance4x8, aom_highbd_8_sub_pixel_variance4x8,
1912 aom_highbd_8_sub_pixel_avg_variance4x8, aom_highbd_sad4x8x4d_bits8,
1913 aom_highbd_dist_wtd_sad4x8_avg_bits8,
1914 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x8)
1915
1916 HIGHBD_BFP(
1917 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1918 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1919 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x4d_bits8,
1920 aom_highbd_dist_wtd_sad4x4_avg_bits8,
1921 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x4)
1922
1923 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1924 aom_highbd_sad128x128_avg_bits8,
1925 aom_highbd_8_variance128x128,
1926 aom_highbd_8_sub_pixel_variance128x128,
1927 aom_highbd_8_sub_pixel_avg_variance128x128,
1928 aom_highbd_sad128x128x4d_bits8,
1929 aom_highbd_dist_wtd_sad128x128_avg_bits8,
1930 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001931
1932 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1933 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1934 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001935 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001936 aom_highbd_sad128x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001937 aom_highbd_dist_wtd_sad128x64_avg_bits8,
1938 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001939
1940 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1941 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1942 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001943 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001944 aom_highbd_sad64x128x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001945 aom_highbd_dist_wtd_sad64x128_avg_bits8,
1946 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001947
David Barkerf19f35f2017-05-22 16:33:22 +01001948 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1949 aom_highbd_8_masked_sub_pixel_variance128x128)
1950 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1951 aom_highbd_8_masked_sub_pixel_variance128x64)
1952 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1953 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001954 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1955 aom_highbd_8_masked_sub_pixel_variance64x64)
1956 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1957 aom_highbd_8_masked_sub_pixel_variance64x32)
1958 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1959 aom_highbd_8_masked_sub_pixel_variance32x64)
1960 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1961 aom_highbd_8_masked_sub_pixel_variance32x32)
1962 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1963 aom_highbd_8_masked_sub_pixel_variance32x16)
1964 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1965 aom_highbd_8_masked_sub_pixel_variance16x32)
1966 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1967 aom_highbd_8_masked_sub_pixel_variance16x16)
1968 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1969 aom_highbd_8_masked_sub_pixel_variance8x16)
1970 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1971 aom_highbd_8_masked_sub_pixel_variance16x8)
1972 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1973 aom_highbd_8_masked_sub_pixel_variance8x8)
1974 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1975 aom_highbd_8_masked_sub_pixel_variance4x8)
1976 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1977 aom_highbd_8_masked_sub_pixel_variance8x4)
1978 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1979 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001980 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1981 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001982 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1983 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001984 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1985 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001986 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1987 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001988 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1989 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001990 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1991 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001992 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1993 aom_highbd_obmc_variance128x128,
1994 aom_highbd_obmc_sub_pixel_variance128x128)
1995 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1996 aom_highbd_obmc_variance128x64,
1997 aom_highbd_obmc_sub_pixel_variance128x64)
1998 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1999 aom_highbd_obmc_variance64x128,
2000 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002001 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
2002 aom_highbd_obmc_variance64x64,
2003 aom_highbd_obmc_sub_pixel_variance64x64)
2004 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
2005 aom_highbd_obmc_variance64x32,
2006 aom_highbd_obmc_sub_pixel_variance64x32)
2007 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
2008 aom_highbd_obmc_variance32x64,
2009 aom_highbd_obmc_sub_pixel_variance32x64)
2010 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
2011 aom_highbd_obmc_variance32x32,
2012 aom_highbd_obmc_sub_pixel_variance32x32)
2013 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
2014 aom_highbd_obmc_variance32x16,
2015 aom_highbd_obmc_sub_pixel_variance32x16)
2016 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
2017 aom_highbd_obmc_variance16x32,
2018 aom_highbd_obmc_sub_pixel_variance16x32)
2019 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
2020 aom_highbd_obmc_variance16x16,
2021 aom_highbd_obmc_sub_pixel_variance16x16)
2022 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
2023 aom_highbd_obmc_variance8x16,
2024 aom_highbd_obmc_sub_pixel_variance8x16)
2025 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
2026 aom_highbd_obmc_variance16x8,
2027 aom_highbd_obmc_sub_pixel_variance16x8)
2028 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
2029 aom_highbd_obmc_variance8x8,
2030 aom_highbd_obmc_sub_pixel_variance8x8)
2031 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
2032 aom_highbd_obmc_variance4x8,
2033 aom_highbd_obmc_sub_pixel_variance4x8)
2034 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
2035 aom_highbd_obmc_variance8x4,
2036 aom_highbd_obmc_sub_pixel_variance8x4)
2037 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
2038 aom_highbd_obmc_variance4x4,
2039 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002040 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
2041 aom_highbd_obmc_variance64x16,
2042 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002043 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
2044 aom_highbd_obmc_variance16x64,
2045 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002046 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
2047 aom_highbd_obmc_variance32x8,
2048 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002049 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
2050 aom_highbd_obmc_variance8x32,
2051 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002052 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
2053 aom_highbd_obmc_variance16x4,
2054 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002055 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
2056 aom_highbd_obmc_variance4x16,
2057 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002058 break;
2059
Yaowu Xuf883b422016-08-30 14:01:10 -07002060 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002061 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
2062 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
2063 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002064 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002065 aom_highbd_sad64x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002066 aom_highbd_dist_wtd_sad64x16_avg_bits10,
2067 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002068
2069 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
2070 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
2071 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002072 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002073 aom_highbd_sad16x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002074 aom_highbd_dist_wtd_sad16x64_avg_bits10,
2075 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002076
2077 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
2078 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
2079 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002080 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002081 aom_highbd_sad32x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002082 aom_highbd_dist_wtd_sad32x8_avg_bits10,
2083 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002084
2085 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
2086 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
2087 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002088 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002089 aom_highbd_sad8x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002090 aom_highbd_dist_wtd_sad8x32_avg_bits10,
2091 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002092
2093 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
2094 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
2095 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002096 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002097 aom_highbd_sad16x4x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002098 aom_highbd_dist_wtd_sad16x4_avg_bits10,
2099 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002100
2101 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
2102 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
2103 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002104 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002105 aom_highbd_sad4x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002106 aom_highbd_dist_wtd_sad4x16_avg_bits10,
2107 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002108
2109 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
2110 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
2111 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002112 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002113 aom_highbd_sad32x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002114 aom_highbd_dist_wtd_sad32x16_avg_bits10,
2115 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002116
2117 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
2118 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
2119 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002120 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002121 aom_highbd_sad16x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002122 aom_highbd_dist_wtd_sad16x32_avg_bits10,
2123 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002124
2125 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
2126 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
2127 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002128 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002129 aom_highbd_sad64x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002130 aom_highbd_dist_wtd_sad64x32_avg_bits10,
2131 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002132
2133 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
2134 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
2135 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002136 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002137 aom_highbd_sad32x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002138 aom_highbd_dist_wtd_sad32x64_avg_bits10,
2139 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002140
2141 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
2142 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
2143 aom_highbd_10_sub_pixel_variance32x32,
2144 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002145 aom_highbd_sad32x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002146 aom_highbd_dist_wtd_sad32x32_avg_bits10,
2147 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002148
2149 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
2150 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
2151 aom_highbd_10_sub_pixel_variance64x64,
2152 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002153 aom_highbd_sad64x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002154 aom_highbd_dist_wtd_sad64x64_avg_bits10,
2155 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002156
2157 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
2158 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
2159 aom_highbd_10_sub_pixel_variance16x16,
2160 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002161 aom_highbd_sad16x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002162 aom_highbd_dist_wtd_sad16x16_avg_bits10,
2163 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002164
2165 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
2166 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
2167 aom_highbd_10_sub_pixel_variance16x8,
2168 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002169 aom_highbd_sad16x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002170 aom_highbd_dist_wtd_sad16x8_avg_bits10,
2171 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002172
2173 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
2174 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
2175 aom_highbd_10_sub_pixel_variance8x16,
2176 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002177 aom_highbd_sad8x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002178 aom_highbd_dist_wtd_sad8x16_avg_bits10,
2179 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002180
2181 HIGHBD_BFP(
2182 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
2183 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002184 aom_highbd_10_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002185 aom_highbd_sad8x8x4d_bits10, aom_highbd_dist_wtd_sad8x8_avg_bits10,
2186 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002187
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002188 HIGHBD_BFP(
2189 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
2190 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
2191 aom_highbd_10_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002192 aom_highbd_sad8x4x4d_bits10, aom_highbd_dist_wtd_sad8x4_avg_bits10,
2193 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002194
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002195 HIGHBD_BFP(
2196 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
2197 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
2198 aom_highbd_10_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002199 aom_highbd_sad4x8x4d_bits10, aom_highbd_dist_wtd_sad4x8_avg_bits10,
2200 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002201
2202 HIGHBD_BFP(
2203 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
2204 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002205 aom_highbd_10_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002206 aom_highbd_sad4x4x4d_bits10, aom_highbd_dist_wtd_sad4x4_avg_bits10,
2207 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002208
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002209 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
2210 aom_highbd_sad128x128_avg_bits10,
2211 aom_highbd_10_variance128x128,
2212 aom_highbd_10_sub_pixel_variance128x128,
2213 aom_highbd_10_sub_pixel_avg_variance128x128,
2214 aom_highbd_sad128x128x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002215 aom_highbd_dist_wtd_sad128x128_avg_bits10,
2216 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002217
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002218 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
2219 aom_highbd_sad128x64_avg_bits10,
2220 aom_highbd_10_variance128x64,
2221 aom_highbd_10_sub_pixel_variance128x64,
2222 aom_highbd_10_sub_pixel_avg_variance128x64,
2223 aom_highbd_sad128x64x4d_bits10,
2224 aom_highbd_dist_wtd_sad128x64_avg_bits10,
2225 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002226
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002227 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
2228 aom_highbd_sad64x128_avg_bits10,
2229 aom_highbd_10_variance64x128,
2230 aom_highbd_10_sub_pixel_variance64x128,
2231 aom_highbd_10_sub_pixel_avg_variance64x128,
2232 aom_highbd_sad64x128x4d_bits10,
2233 aom_highbd_dist_wtd_sad64x128_avg_bits10,
2234 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002235
David Barkerf19f35f2017-05-22 16:33:22 +01002236 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
2237 aom_highbd_10_masked_sub_pixel_variance128x128)
2238 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
2239 aom_highbd_10_masked_sub_pixel_variance128x64)
2240 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
2241 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002242 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
2243 aom_highbd_10_masked_sub_pixel_variance64x64)
2244 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
2245 aom_highbd_10_masked_sub_pixel_variance64x32)
2246 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
2247 aom_highbd_10_masked_sub_pixel_variance32x64)
2248 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
2249 aom_highbd_10_masked_sub_pixel_variance32x32)
2250 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
2251 aom_highbd_10_masked_sub_pixel_variance32x16)
2252 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
2253 aom_highbd_10_masked_sub_pixel_variance16x32)
2254 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
2255 aom_highbd_10_masked_sub_pixel_variance16x16)
2256 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
2257 aom_highbd_10_masked_sub_pixel_variance8x16)
2258 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
2259 aom_highbd_10_masked_sub_pixel_variance16x8)
2260 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
2261 aom_highbd_10_masked_sub_pixel_variance8x8)
2262 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
2263 aom_highbd_10_masked_sub_pixel_variance4x8)
2264 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
2265 aom_highbd_10_masked_sub_pixel_variance8x4)
2266 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
2267 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002268 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
2269 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002270 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
2271 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002272 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
2273 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002274 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
2275 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002276 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
2277 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002278 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
2279 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002280 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
2281 aom_highbd_10_obmc_variance128x128,
2282 aom_highbd_10_obmc_sub_pixel_variance128x128)
2283 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
2284 aom_highbd_10_obmc_variance128x64,
2285 aom_highbd_10_obmc_sub_pixel_variance128x64)
2286 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
2287 aom_highbd_10_obmc_variance64x128,
2288 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002289 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2290 aom_highbd_10_obmc_variance64x64,
2291 aom_highbd_10_obmc_sub_pixel_variance64x64)
2292 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2293 aom_highbd_10_obmc_variance64x32,
2294 aom_highbd_10_obmc_sub_pixel_variance64x32)
2295 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2296 aom_highbd_10_obmc_variance32x64,
2297 aom_highbd_10_obmc_sub_pixel_variance32x64)
2298 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2299 aom_highbd_10_obmc_variance32x32,
2300 aom_highbd_10_obmc_sub_pixel_variance32x32)
2301 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2302 aom_highbd_10_obmc_variance32x16,
2303 aom_highbd_10_obmc_sub_pixel_variance32x16)
2304 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2305 aom_highbd_10_obmc_variance16x32,
2306 aom_highbd_10_obmc_sub_pixel_variance16x32)
2307 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2308 aom_highbd_10_obmc_variance16x16,
2309 aom_highbd_10_obmc_sub_pixel_variance16x16)
2310 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2311 aom_highbd_10_obmc_variance8x16,
2312 aom_highbd_10_obmc_sub_pixel_variance8x16)
2313 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2314 aom_highbd_10_obmc_variance16x8,
2315 aom_highbd_10_obmc_sub_pixel_variance16x8)
2316 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2317 aom_highbd_10_obmc_variance8x8,
2318 aom_highbd_10_obmc_sub_pixel_variance8x8)
2319 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2320 aom_highbd_10_obmc_variance4x8,
2321 aom_highbd_10_obmc_sub_pixel_variance4x8)
2322 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2323 aom_highbd_10_obmc_variance8x4,
2324 aom_highbd_10_obmc_sub_pixel_variance8x4)
2325 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2326 aom_highbd_10_obmc_variance4x4,
2327 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002328
Rupert Swarbrick72678572017-08-02 12:05:26 +01002329 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2330 aom_highbd_10_obmc_variance64x16,
2331 aom_highbd_10_obmc_sub_pixel_variance64x16)
2332
2333 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2334 aom_highbd_10_obmc_variance16x64,
2335 aom_highbd_10_obmc_sub_pixel_variance16x64)
2336
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002337 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2338 aom_highbd_10_obmc_variance32x8,
2339 aom_highbd_10_obmc_sub_pixel_variance32x8)
2340
2341 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2342 aom_highbd_10_obmc_variance8x32,
2343 aom_highbd_10_obmc_sub_pixel_variance8x32)
2344
2345 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2346 aom_highbd_10_obmc_variance16x4,
2347 aom_highbd_10_obmc_sub_pixel_variance16x4)
2348
2349 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2350 aom_highbd_10_obmc_variance4x16,
2351 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002352 break;
2353
Yaowu Xuf883b422016-08-30 14:01:10 -07002354 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002355 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2356 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2357 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002358 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002359 aom_highbd_sad64x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002360 aom_highbd_dist_wtd_sad64x16_avg_bits12,
2361 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002362
2363 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2364 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2365 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002366 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002367 aom_highbd_sad16x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002368 aom_highbd_dist_wtd_sad16x64_avg_bits12,
2369 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002370
2371 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2372 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2373 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002374 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002375 aom_highbd_sad32x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002376 aom_highbd_dist_wtd_sad32x8_avg_bits12,
2377 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002378
2379 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2380 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2381 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002382 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002383 aom_highbd_sad8x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002384 aom_highbd_dist_wtd_sad8x32_avg_bits12,
2385 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002386
2387 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2388 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2389 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002390 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002391 aom_highbd_sad16x4x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002392 aom_highbd_dist_wtd_sad16x4_avg_bits12,
2393 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002394
2395 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2396 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2397 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002398 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002399 aom_highbd_sad4x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002400 aom_highbd_dist_wtd_sad4x16_avg_bits12,
2401 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002402
2403 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2404 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2405 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002406 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002407 aom_highbd_sad32x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002408 aom_highbd_dist_wtd_sad32x16_avg_bits12,
2409 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002410
2411 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2412 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2413 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002414 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002415 aom_highbd_sad16x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002416 aom_highbd_dist_wtd_sad16x32_avg_bits12,
2417 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002418
2419 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2420 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2421 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002422 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002423 aom_highbd_sad64x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002424 aom_highbd_dist_wtd_sad64x32_avg_bits12,
2425 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002426
2427 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2428 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2429 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002430 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002431 aom_highbd_sad32x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002432 aom_highbd_dist_wtd_sad32x64_avg_bits12,
2433 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002434
2435 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2436 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2437 aom_highbd_12_sub_pixel_variance32x32,
2438 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002439 aom_highbd_sad32x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002440 aom_highbd_dist_wtd_sad32x32_avg_bits12,
2441 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002442
2443 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2444 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2445 aom_highbd_12_sub_pixel_variance64x64,
2446 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002447 aom_highbd_sad64x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002448 aom_highbd_dist_wtd_sad64x64_avg_bits12,
2449 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002450
2451 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2452 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2453 aom_highbd_12_sub_pixel_variance16x16,
2454 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002455 aom_highbd_sad16x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002456 aom_highbd_dist_wtd_sad16x16_avg_bits12,
2457 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002458
2459 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2460 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2461 aom_highbd_12_sub_pixel_variance16x8,
2462 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002463 aom_highbd_sad16x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002464 aom_highbd_dist_wtd_sad16x8_avg_bits12,
2465 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002466
2467 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2468 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2469 aom_highbd_12_sub_pixel_variance8x16,
2470 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002471 aom_highbd_sad8x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002472 aom_highbd_dist_wtd_sad8x16_avg_bits12,
2473 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002474
2475 HIGHBD_BFP(
2476 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2477 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002478 aom_highbd_12_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002479 aom_highbd_sad8x8x4d_bits12, aom_highbd_dist_wtd_sad8x8_avg_bits12,
2480 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002481
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002482 HIGHBD_BFP(
2483 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2484 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2485 aom_highbd_12_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002486 aom_highbd_sad8x4x4d_bits12, aom_highbd_dist_wtd_sad8x4_avg_bits12,
2487 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002488
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002489 HIGHBD_BFP(
2490 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2491 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2492 aom_highbd_12_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002493 aom_highbd_sad4x8x4d_bits12, aom_highbd_dist_wtd_sad4x8_avg_bits12,
2494 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002495
2496 HIGHBD_BFP(
2497 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2498 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002499 aom_highbd_12_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002500 aom_highbd_sad4x4x4d_bits12, aom_highbd_dist_wtd_sad4x4_avg_bits12,
2501 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002502
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002503 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2504 aom_highbd_sad128x128_avg_bits12,
2505 aom_highbd_12_variance128x128,
2506 aom_highbd_12_sub_pixel_variance128x128,
2507 aom_highbd_12_sub_pixel_avg_variance128x128,
2508 aom_highbd_sad128x128x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002509 aom_highbd_dist_wtd_sad128x128_avg_bits12,
2510 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002511
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002512 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2513 aom_highbd_sad128x64_avg_bits12,
2514 aom_highbd_12_variance128x64,
2515 aom_highbd_12_sub_pixel_variance128x64,
2516 aom_highbd_12_sub_pixel_avg_variance128x64,
2517 aom_highbd_sad128x64x4d_bits12,
2518 aom_highbd_dist_wtd_sad128x64_avg_bits12,
2519 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002520
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002521 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2522 aom_highbd_sad64x128_avg_bits12,
2523 aom_highbd_12_variance64x128,
2524 aom_highbd_12_sub_pixel_variance64x128,
2525 aom_highbd_12_sub_pixel_avg_variance64x128,
2526 aom_highbd_sad64x128x4d_bits12,
2527 aom_highbd_dist_wtd_sad64x128_avg_bits12,
2528 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002529
David Barkerf19f35f2017-05-22 16:33:22 +01002530 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2531 aom_highbd_12_masked_sub_pixel_variance128x128)
2532 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2533 aom_highbd_12_masked_sub_pixel_variance128x64)
2534 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2535 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002536 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2537 aom_highbd_12_masked_sub_pixel_variance64x64)
2538 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2539 aom_highbd_12_masked_sub_pixel_variance64x32)
2540 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2541 aom_highbd_12_masked_sub_pixel_variance32x64)
2542 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2543 aom_highbd_12_masked_sub_pixel_variance32x32)
2544 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2545 aom_highbd_12_masked_sub_pixel_variance32x16)
2546 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2547 aom_highbd_12_masked_sub_pixel_variance16x32)
2548 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2549 aom_highbd_12_masked_sub_pixel_variance16x16)
2550 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2551 aom_highbd_12_masked_sub_pixel_variance8x16)
2552 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2553 aom_highbd_12_masked_sub_pixel_variance16x8)
2554 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2555 aom_highbd_12_masked_sub_pixel_variance8x8)
2556 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2557 aom_highbd_12_masked_sub_pixel_variance4x8)
2558 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2559 aom_highbd_12_masked_sub_pixel_variance8x4)
2560 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2561 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002562 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2563 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002564 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2565 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002566 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2567 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002568 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2569 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002570 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2571 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002572 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2573 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002574 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2575 aom_highbd_12_obmc_variance128x128,
2576 aom_highbd_12_obmc_sub_pixel_variance128x128)
2577 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2578 aom_highbd_12_obmc_variance128x64,
2579 aom_highbd_12_obmc_sub_pixel_variance128x64)
2580 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2581 aom_highbd_12_obmc_variance64x128,
2582 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002583 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2584 aom_highbd_12_obmc_variance64x64,
2585 aom_highbd_12_obmc_sub_pixel_variance64x64)
2586 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2587 aom_highbd_12_obmc_variance64x32,
2588 aom_highbd_12_obmc_sub_pixel_variance64x32)
2589 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2590 aom_highbd_12_obmc_variance32x64,
2591 aom_highbd_12_obmc_sub_pixel_variance32x64)
2592 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2593 aom_highbd_12_obmc_variance32x32,
2594 aom_highbd_12_obmc_sub_pixel_variance32x32)
2595 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2596 aom_highbd_12_obmc_variance32x16,
2597 aom_highbd_12_obmc_sub_pixel_variance32x16)
2598 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2599 aom_highbd_12_obmc_variance16x32,
2600 aom_highbd_12_obmc_sub_pixel_variance16x32)
2601 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2602 aom_highbd_12_obmc_variance16x16,
2603 aom_highbd_12_obmc_sub_pixel_variance16x16)
2604 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2605 aom_highbd_12_obmc_variance8x16,
2606 aom_highbd_12_obmc_sub_pixel_variance8x16)
2607 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2608 aom_highbd_12_obmc_variance16x8,
2609 aom_highbd_12_obmc_sub_pixel_variance16x8)
2610 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2611 aom_highbd_12_obmc_variance8x8,
2612 aom_highbd_12_obmc_sub_pixel_variance8x8)
2613 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2614 aom_highbd_12_obmc_variance4x8,
2615 aom_highbd_12_obmc_sub_pixel_variance4x8)
2616 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2617 aom_highbd_12_obmc_variance8x4,
2618 aom_highbd_12_obmc_sub_pixel_variance8x4)
2619 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2620 aom_highbd_12_obmc_variance4x4,
2621 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002622 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2623 aom_highbd_12_obmc_variance64x16,
2624 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002625 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2626 aom_highbd_12_obmc_variance16x64,
2627 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002628 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2629 aom_highbd_12_obmc_variance32x8,
2630 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002631 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2632 aom_highbd_12_obmc_variance8x32,
2633 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002634 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2635 aom_highbd_12_obmc_variance16x4,
2636 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002637 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2638 aom_highbd_12_obmc_variance4x16,
2639 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002640 break;
2641
2642 default:
2643 assert(0 &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002644 "cm->seq_params.bit_depth should be AOM_BITS_8, "
Yaowu Xuf883b422016-08-30 14:01:10 -07002645 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002646 }
2647 }
2648}
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002649#endif // CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002650
Yaowu Xuf883b422016-08-30 14:01:10 -07002651static void realloc_segmentation_maps(AV1_COMP *cpi) {
2652 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002653
2654 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002655 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002656 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002657 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002658
2659 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002660 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002661 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002662 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002663
2664 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002665 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002666 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002667 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002668}
2669
Yue Chenb4c93f02019-08-05 13:47:31 -07002670static void set_tpl_stats_block_size(AV1_COMP *cpi) {
2671 AV1_COMMON *const cm = &cpi->common;
2672 const int is_720p_or_larger = AOMMIN(cm->width, cm->height) >= 720;
2673
2674 // 0: 4x4, 1: 8x8, 2: 16x16
2675 cpi->tpl_stats_block_mis_log2 = is_720p_or_larger ? 2 : 1;
2676}
2677
Hui Su38711e72019-06-11 10:49:47 -07002678void av1_alloc_compound_type_rd_buffers(AV1_COMMON *const cm,
2679 CompoundTypeRdBuffers *const bufs) {
2680 CHECK_MEM_ERROR(
2681 cm, bufs->pred0,
2682 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred0)));
2683 CHECK_MEM_ERROR(
2684 cm, bufs->pred1,
2685 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred1)));
2686 CHECK_MEM_ERROR(
2687 cm, bufs->residual1,
2688 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->residual1)));
2689 CHECK_MEM_ERROR(
2690 cm, bufs->diff10,
2691 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->diff10)));
2692 CHECK_MEM_ERROR(cm, bufs->tmp_best_mask_buf,
2693 (uint8_t *)aom_malloc(2 * MAX_SB_SQUARE *
2694 sizeof(*bufs->tmp_best_mask_buf)));
2695}
2696
2697void av1_release_compound_type_rd_buffers(CompoundTypeRdBuffers *const bufs) {
2698 aom_free(bufs->pred0);
2699 aom_free(bufs->pred1);
2700 aom_free(bufs->residual1);
2701 aom_free(bufs->diff10);
2702 aom_free(bufs->tmp_best_mask_buf);
2703 av1_zero(*bufs); // Set all pointers to NULL for safety.
2704}
2705
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002706static void config_target_level(AV1_COMP *const cpi, AV1_LEVEL target_level,
2707 int tier) {
2708 aom_clear_system_state();
2709
2710 AV1EncoderConfig *const oxcf = &cpi->oxcf;
2711 SequenceHeader *const seq_params = &cpi->common.seq_params;
2712
2713 // Adjust target bitrate to be no larger than 70% of level limit.
2714 const BITSTREAM_PROFILE profile = seq_params->profile;
2715 const double level_bitrate_limit =
2716 av1_get_max_bitrate_for_level(target_level, tier, profile);
2717 const int64_t max_bitrate = (int64_t)(level_bitrate_limit * 0.70);
2718 oxcf->target_bandwidth = AOMMIN(oxcf->target_bandwidth, max_bitrate);
2719 // Also need to update cpi->twopass.bits_left.
2720 TWO_PASS *const twopass = &cpi->twopass;
Mufaddal Chakera74c9cbe2020-01-17 16:44:59 +05302721 FIRSTPASS_STATS *stats = twopass->total_stats;
2722 if (stats != NULL)
2723 cpi->twopass.bits_left =
2724 (int64_t)(stats->duration * cpi->oxcf.target_bandwidth / 10000000.0);
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002725
2726 // Adjust max over-shoot percentage.
2727 oxcf->over_shoot_pct = 0;
2728
2729 // Adjust max quantizer.
2730 oxcf->worst_allowed_q = 255;
2731
2732 // Adjust number of tiles and tile columns to be under level limit.
2733 int max_tiles, max_tile_cols;
2734 av1_get_max_tiles_for_level(target_level, &max_tiles, &max_tile_cols);
2735 while (oxcf->tile_columns > 0 && (1 << oxcf->tile_columns) > max_tile_cols) {
2736 --oxcf->tile_columns;
2737 }
2738 const int tile_cols = (1 << oxcf->tile_columns);
2739 while (oxcf->tile_rows > 0 &&
2740 tile_cols * (1 << oxcf->tile_rows) > max_tiles) {
2741 --oxcf->tile_rows;
2742 }
2743
2744 // Adjust min compression ratio.
2745 const int still_picture = seq_params->still_picture;
2746 const double min_cr =
2747 av1_get_min_cr_for_level(target_level, tier, still_picture);
2748 oxcf->min_cr = AOMMAX(oxcf->min_cr, (unsigned int)(min_cr * 100));
2749}
2750
Yaowu Xuf883b422016-08-30 14:01:10 -07002751void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2752 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002753 SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002754 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002755 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002756 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002757
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002758 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2759 seq_params->bit_depth = oxcf->bit_depth;
2760 seq_params->color_primaries = oxcf->color_primaries;
2761 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2762 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2763 seq_params->monochrome = oxcf->monochrome;
2764 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2765 seq_params->color_range = oxcf->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002766
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002767 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2768 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002769
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002770 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08002771 cm->timing_info.num_units_in_display_tick =
2772 oxcf->timing_info.num_units_in_display_tick;
2773 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
2774 cm->timing_info.equal_picture_interval =
2775 oxcf->timing_info.equal_picture_interval;
2776 cm->timing_info.num_ticks_per_picture =
2777 oxcf->timing_info.num_ticks_per_picture;
2778
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002779 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07002780 oxcf->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002781 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002782 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08002783 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002784 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08002785 cm->buffer_model.num_units_in_decoding_tick =
2786 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07002787 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07002788 av1_set_aom_dec_model_info(&cm->buffer_model);
2789 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07002790 } else if (cm->timing_info_present &&
2791 cm->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002792 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002793 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07002794 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07002795 } else {
2796 cm->op_params[0].initial_display_delay =
2797 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08002798 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002799
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002800 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002801
Yaowu Xuc27fc142016-08-22 16:08:15 -07002802 cpi->oxcf = *oxcf;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002803 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002804 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002805
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002806 memcpy(cpi->target_seq_level_idx, cpi->oxcf.target_seq_level_idx,
2807 sizeof(cpi->target_seq_level_idx));
2808 cpi->keep_level_stats = 0;
2809 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
2810 if (cpi->target_seq_level_idx[i] <= SEQ_LEVELS) {
2811 cpi->keep_level_stats |= 1u << i;
2812 if (!cpi->level_info[i]) {
2813 CHECK_MEM_ERROR(cm, cpi->level_info[i],
2814 aom_calloc(1, sizeof(*cpi->level_info[i])));
2815 }
2816 }
2817 }
2818
2819 // TODO(huisu@): level targeting currently only works for the 0th operating
2820 // point, so scalable coding is not supported yet.
2821 if (cpi->target_seq_level_idx[0] < SEQ_LEVELS) {
2822 // Adjust encoder config in order to meet target level.
2823 config_target_level(cpi, cpi->target_seq_level_idx[0], seq_params->tier[0]);
2824 }
2825
Mufaddal Chakera3bcc72c2019-12-11 14:38:37 +05302826 if ((has_no_stats_stage(cpi)) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002827 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2828 } else {
2829 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2830 }
2831
2832 cpi->refresh_last_frame = 1;
2833 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002834 cpi->refresh_bwd_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002835
Debargha Mukherjee229fdc82018-03-10 07:45:33 -08002836 cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2837 ? REFRESH_FRAME_CONTEXT_DISABLED
2838 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002839 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08002840 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002841
Alex Converse74ad0912017-07-18 10:22:58 -07002842 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002843 CHECK_MEM_ERROR(cm, x->palette_buffer,
2844 aom_memalign(16, sizeof(*x->palette_buffer)));
2845 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002846
Hui Su38711e72019-06-11 10:49:47 -07002847 if (x->comp_rd_buffer.pred0 == NULL) {
2848 av1_alloc_compound_type_rd_buffers(cm, &x->comp_rd_buffer);
2849 }
2850
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002851 if (x->tmp_conv_dst == NULL) {
2852 CHECK_MEM_ERROR(
2853 cm, x->tmp_conv_dst,
2854 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002855 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002856 }
2857 for (int i = 0; i < 2; ++i) {
2858 if (x->tmp_obmc_bufs[i] == NULL) {
2859 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
wenyao.liu22d8ab32018-10-16 09:11:29 +08002860 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002861 sizeof(*x->tmp_obmc_bufs[i])));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002862 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002863 }
2864 }
2865
Marco Paniconi177b8c32020-01-26 20:37:29 -08002866 av1_reset_segment_features(cm);
Fyodor Kyslov64aaa092020-01-24 11:44:24 -08002867
chiyotsaic666b1f2019-12-20 10:44:58 -08002868 av1_set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002869
Yaowu Xuc27fc142016-08-22 16:08:15 -07002870 set_rc_buffer_sizes(rc, &cpi->oxcf);
2871
2872 // Under a configuration change, where maximum_buffer_size may change,
2873 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002874 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2875 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002876
2877 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002878 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002879
2880 // Set absolute upper and lower quality limits
2881 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2882 rc->best_quality = cpi->oxcf.best_allowed_q;
2883
Urvang Joshib55cb5e2018-09-12 14:50:21 -07002884 cm->interp_filter = oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
Yue Chen5380cb52018-02-23 15:33:21 -08002885 cm->switchable_motion_mode = 1;
2886
Yaowu Xuc27fc142016-08-22 16:08:15 -07002887 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2888 cm->render_width = cpi->oxcf.render_width;
2889 cm->render_height = cpi->oxcf.render_height;
2890 } else {
2891 cm->render_width = cpi->oxcf.width;
2892 cm->render_height = cpi->oxcf.height;
2893 }
2894 cm->width = cpi->oxcf.width;
2895 cm->height = cpi->oxcf.height;
2896
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002897 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -08002898 // Superblock size should not be updated after the first key frame.
2899 if (!cpi->seq_params_locked) {
2900 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Hui Sud909c2c2019-03-08 11:51:14 -08002901 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
2902 seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
Urvang Joshie4530f82018-01-09 11:43:37 -08002903 }
Dominic Symes917d6c02017-10-11 18:00:52 +02002904
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002905 if (cpi->initial_width || sb_size != seq_params->sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002906 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002907 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002908 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002909 av1_free_pc_tree(&cpi->td, num_planes);
Cheng Chen46f30c72017-09-07 11:13:33 -07002910 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002911 realloc_segmentation_maps(cpi);
2912 cpi->initial_width = cpi->initial_height = 0;
2913 }
2914 }
2915 update_frame_size(cpi);
2916
2917 cpi->alt_ref_source = NULL;
2918 rc->is_src_frame_alt_ref = 0;
2919
Yaowu Xuc27fc142016-08-22 16:08:15 -07002920 set_tile_info(cpi);
2921
Marco Paniconid8574e32019-08-04 21:30:12 -07002922 if (!cpi->svc.external_ref_frame_config)
2923 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002924 cpi->ext_refresh_frame_context_pending = 0;
2925
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002926#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002927 highbd_set_var_fns(cpi);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002928#endif
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002929
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002930 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002931 // This should not be called after the first key frame.
2932 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002933 seq_params->operating_points_cnt_minus_1 =
Marco Paniconi6da983b2019-09-16 20:12:43 -07002934 (cm->number_spatial_layers > 1 || cm->number_temporal_layers > 1)
2935 ? cm->number_spatial_layers * cm->number_temporal_layers - 1
2936 : 0;
Marco Paniconi67142112019-07-24 15:00:31 -07002937 init_seq_coding_tools(&cm->seq_params, cm, oxcf, cpi->use_svc);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002938 }
Marco Paniconi5b1e4732019-08-08 18:57:53 -07002939
2940 if (cpi->use_svc)
2941 av1_update_layer_context_change_config(cpi, oxcf->target_bandwidth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002942}
2943
Aasaipriyabd659e42019-12-13 16:12:13 +05302944AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf, BufferPool *const pool,
Mufaddal Chakera5517b282019-12-13 16:39:56 +05302945 FIRSTPASS_STATS *frame_stats_buf,
Akshata Jadhava49be172019-12-18 00:03:53 +05302946 COMPRESSOR_STAGE stage, int num_lap_buffers,
Mufaddal Chakera412efe22020-01-17 14:35:01 +05302947 int lap_lag_in_frames,
Akshata Jadhava49be172019-12-18 00:03:53 +05302948 STATS_BUFFER_CTX *stats_buf_context) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002949 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2950 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002951
2952 if (!cm) return NULL;
2953
Yaowu Xuf883b422016-08-30 14:01:10 -07002954 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002955
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07002956 // The jmp_buf is valid only for the duration of the function that calls
2957 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2958 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002959 if (setjmp(cm->error.jmp)) {
2960 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002961 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002962 return 0;
2963 }
2964
2965 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002966 cm->alloc_mi = enc_alloc_mi;
2967 cm->free_mi = enc_free_mi;
2968 cm->setup_mi = enc_setup_mi;
chiyotsaia7091f12019-08-09 16:48:27 -07002969 cm->set_mb_mi = enc_set_mb_mi;
2970
2971 cm->mi_alloc_bsize = BLOCK_4X4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002972
Angie Chianga5d96c42016-10-21 16:16:56 -07002973 CHECK_MEM_ERROR(cm, cm->fc,
2974 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +00002975 CHECK_MEM_ERROR(
2976 cm, cm->default_frame_context,
2977 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -07002978 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +00002979 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002980
2981 cpi->resize_state = 0;
2982 cpi->resize_avg_qp = 0;
2983 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002984
Yaowu Xuc27fc142016-08-22 16:08:15 -07002985 cpi->common.buffer_pool = pool;
2986
2987 init_config(cpi, oxcf);
Mufaddal Chakera5517b282019-12-13 16:39:56 +05302988 cpi->lap_enabled = num_lap_buffers > 0;
2989 cpi->compressor_stage = stage;
Mufaddal Chakera11f284a2019-12-19 11:38:46 +05302990 if (cpi->compressor_stage == LAP_STAGE) {
Mufaddal Chakera412efe22020-01-17 14:35:01 +05302991 cpi->oxcf.lag_in_frames = lap_lag_in_frames;
Mufaddal Chakera11f284a2019-12-19 11:38:46 +05302992 }
Mufaddal Chakeraac828682019-12-13 16:31:42 +05302993
Yaowu Xuf883b422016-08-30 14:01:10 -07002994 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002995
Jingning Han17af7742019-09-17 16:58:03 -07002996 init_frame_info(&cpi->frame_info, cm);
2997
David Turnerd2a592e2018-11-16 14:59:31 +00002998 cm->current_frame.frame_number = 0;
David Turnera4c96252019-01-11 16:36:39 +00002999 cm->current_frame_id = -1;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07003000 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003001 cpi->partition_search_skippable_frame = 0;
3002 cpi->tile_data = NULL;
David Turnere7ebf902018-12-04 14:04:55 +00003003 cpi->last_show_frame_buf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003004 realloc_segmentation_maps(cpi);
3005
Yaowu Xuc27fc142016-08-22 16:08:15 -07003006 cpi->refresh_alt_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003007
3008 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
3009#if CONFIG_INTERNAL_STATS
3010 cpi->b_calculate_blockiness = 1;
3011 cpi->b_calculate_consistency = 1;
3012 cpi->total_inconsistency = 0;
3013 cpi->psnr.worst = 100.0;
3014 cpi->worst_ssim = 100.0;
3015
3016 cpi->count = 0;
3017 cpi->bytes = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003018#if CONFIG_SPEED_STATS
3019 cpi->tx_search_count = 0;
3020#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003021
3022 if (cpi->b_calculate_psnr) {
3023 cpi->total_sq_error = 0;
3024 cpi->total_samples = 0;
3025 cpi->tot_recode_hits = 0;
3026 cpi->summed_quality = 0;
3027 cpi->summed_weights = 0;
3028 }
3029
3030 cpi->fastssim.worst = 100.0;
3031 cpi->psnrhvs.worst = 100.0;
3032
3033 if (cpi->b_calculate_blockiness) {
3034 cpi->total_blockiness = 0;
3035 cpi->worst_blockiness = 0.0;
3036 }
3037
3038 if (cpi->b_calculate_consistency) {
3039 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07003040 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07003041 cpi->common.mi_rows * cpi->common.mi_cols));
3042 cpi->worst_consistency = 100.0;
3043 }
3044#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003045#if CONFIG_ENTROPY_STATS
3046 av1_zero(aggregate_fc);
3047#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003048
3049 cpi->first_time_stamp_ever = INT64_MAX;
3050
Yaowu Xuc27fc142016-08-22 16:08:15 -07003051#ifdef OUTPUT_YUV_SKINMAP
3052 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
3053#endif
3054#ifdef OUTPUT_YUV_REC
3055 yuv_rec_file = fopen("rec.yuv", "wb");
3056#endif
3057
Mufaddal Chakera5517b282019-12-13 16:39:56 +05303058 assert(MAX_LAP_BUFFERS >= MAX_LAG_BUFFERS);
Akshata Jadhav4be65112019-12-18 00:26:25 +05303059 int size = get_stats_buf_size(num_lap_buffers, MAX_LAG_BUFFERS);
3060 for (int i = 0; i < size; i++)
Aasaipriyabd659e42019-12-13 16:12:13 +05303061 cpi->twopass.frame_stats_arr[i] = &frame_stats_buf[i];
3062
Akshata Jadhava49be172019-12-18 00:03:53 +05303063 cpi->twopass.stats_buf_ctx = stats_buf_context;
3064 cpi->twopass.stats_in = cpi->twopass.stats_buf_ctx->stats_in_start;
3065
Jerome Jiang2612b4d2019-05-29 17:46:47 -07003066#if !CONFIG_REALTIME_ONLY
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303067 if (is_stat_generation_stage(cpi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003068 av1_init_first_pass(cpi);
Hamsalekha S37cc1d12019-12-12 19:27:41 +05303069 } else if (is_stat_consumption_stage(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003070 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
3071 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
3072
Akshata Jadhav1fddf7f2019-12-18 00:49:25 +05303073 if (!cpi->lap_enabled) {
3074 /*Re-initialize to stats buffer, populated by application in the case of
3075 * two pass*/
3076 cpi->twopass.stats_buf_ctx->stats_in_start = oxcf->two_pass_stats_in.buf;
3077 cpi->twopass.stats_in = cpi->twopass.stats_buf_ctx->stats_in_start;
3078 cpi->twopass.stats_buf_ctx->stats_in_end =
3079 &cpi->twopass.stats_buf_ctx->stats_in_start[packets - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003080
Mufaddal Chakera74c9cbe2020-01-17 16:44:59 +05303081 av1_init_second_pass(cpi);
3082 } else {
3083 av1_init_single_pass_lap(cpi);
3084 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003085 }
Jerome Jiang2612b4d2019-05-29 17:46:47 -07003086#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003087
Remya0cce44c2019-08-16 11:57:24 +05303088 int sb_mi_size = av1_get_sb_mi_size(cm);
3089
Jingning Hand064cf02017-06-01 10:00:39 -07003090 CHECK_MEM_ERROR(
3091 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08003092 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08003093 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07003094 CHECK_MEM_ERROR(
3095 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08003096 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08003097 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07003098
3099 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
3100 (int32_t *)aom_memalign(
3101 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
3102
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303103 CHECK_MEM_ERROR(
3104 cm, cpi->td.mb.inter_modes_info,
3105 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303106
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303107 for (int x = 0; x < 2; x++)
3108 for (int y = 0; y < 2; y++)
3109 CHECK_MEM_ERROR(
3110 cm, cpi->td.mb.hash_value_buffer[x][y],
3111 (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
3112 sizeof(*cpi->td.mb.hash_value_buffer[0][0])));
3113
3114 cpi->td.mb.g_crc_initialized = 0;
3115
Jingning Hand064cf02017-06-01 10:00:39 -07003116 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
3117 (int32_t *)aom_memalign(
3118 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
3119
Remya0cce44c2019-08-16 11:57:24 +05303120 CHECK_MEM_ERROR(cm, cpi->td.mb.mbmi_ext,
3121 aom_calloc(sb_mi_size, sizeof(*cpi->td.mb.mbmi_ext)));
3122
David Turner04b70d82019-01-24 15:39:19 +00003123 av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
3124 av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003125
sdengc23c7f12019-06-11 16:56:50 -07003126 {
3127 const int bsize = BLOCK_16X16;
3128 const int w = mi_size_wide[bsize];
3129 const int h = mi_size_high[bsize];
3130 const int num_cols = (cm->mi_cols + w - 1) / w;
3131 const int num_rows = (cm->mi_rows + h - 1) / h;
sdengf46a1062019-08-04 18:43:50 -07003132 CHECK_MEM_ERROR(cm, cpi->tpl_rdmult_scaling_factors,
3133 aom_calloc(num_rows * num_cols,
3134 sizeof(*cpi->tpl_rdmult_scaling_factors)));
3135 CHECK_MEM_ERROR(cm, cpi->tpl_sb_rdmult_scaling_factors,
3136 aom_calloc(num_rows * num_cols,
3137 sizeof(*cpi->tpl_sb_rdmult_scaling_factors)));
3138 }
3139
3140 {
3141 const int bsize = BLOCK_16X16;
3142 const int w = mi_size_wide[bsize];
3143 const int h = mi_size_high[bsize];
3144 const int num_cols = (cm->mi_cols + w - 1) / w;
3145 const int num_rows = (cm->mi_rows + h - 1) / h;
sdengc23c7f12019-06-11 16:56:50 -07003146 CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors,
3147 aom_calloc(num_rows * num_cols,
3148 sizeof(*cpi->ssim_rdmult_scaling_factors)));
3149 }
3150
sdeng01959162019-12-20 10:46:24 -08003151#if CONFIG_TUNE_VMAF
3152 {
3153 const int bsize = BLOCK_64X64;
3154 const int w = mi_size_wide[bsize];
3155 const int h = mi_size_high[bsize];
3156 const int num_cols = (cm->mi_cols + w - 1) / w;
3157 const int num_rows = (cm->mi_rows + h - 1) / h;
3158 CHECK_MEM_ERROR(cm, cpi->vmaf_rdmult_scaling_factors,
3159 aom_calloc(num_rows * num_cols,
3160 sizeof(*cpi->vmaf_rdmult_scaling_factors)));
3161 }
3162#endif
3163
Yue Chenb4c93f02019-08-05 13:47:31 -07003164 set_tpl_stats_block_size(cpi);
Yue Chenc9b23e02019-04-10 16:54:03 -07003165 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
Yue Chenb4c93f02019-08-05 13:47:31 -07003166 const int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
3167 const int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
3168
3169 cpi->tpl_stats_buffer[frame].is_valid = 0;
3170 cpi->tpl_stats_buffer[frame].width =
3171 mi_cols >> cpi->tpl_stats_block_mis_log2;
3172 cpi->tpl_stats_buffer[frame].height =
3173 mi_rows >> cpi->tpl_stats_block_mis_log2;
3174 cpi->tpl_stats_buffer[frame].stride = cpi->tpl_stats_buffer[frame].width;
3175 cpi->tpl_stats_buffer[frame].mi_rows = cm->mi_rows;
3176 cpi->tpl_stats_buffer[frame].mi_cols = cm->mi_cols;
Yue Chen7cae98f2018-08-24 10:43:16 -07003177
Jingning Han81d6fbb2019-07-15 10:14:13 -07003178 CHECK_MEM_ERROR(
3179 cm, cpi->tpl_stats_buffer[frame].tpl_stats_ptr,
Yue Chenb4c93f02019-08-05 13:47:31 -07003180 aom_calloc(cpi->tpl_stats_buffer[frame].width *
3181 cpi->tpl_stats_buffer[frame].height,
Jingning Han81d6fbb2019-07-15 10:14:13 -07003182 sizeof(*cpi->tpl_stats_buffer[frame].tpl_stats_ptr)));
Jingning Han5e98d3b2019-09-23 21:59:36 -07003183
3184 if (aom_alloc_frame_buffer(
3185 &cpi->tpl_stats_buffer[frame].rec_picture_buf, cm->width,
3186 cm->height, cm->seq_params.subsampling_x,
3187 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
Jingning Han55ba1702020-01-14 11:43:04 -08003188 AOM_ENC_NO_SCALE_BORDER, cm->byte_alignment))
Jingning Han5e98d3b2019-09-23 21:59:36 -07003189 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3190 "Failed to allocate frame buffer");
Yue Chen7cae98f2018-08-24 10:43:16 -07003191 }
Jingning Han31a0ee92019-07-15 13:56:55 -07003192 cpi->tpl_frame = &cpi->tpl_stats_buffer[REF_FRAMES + 1];
Yue Chen7cae98f2018-08-24 10:43:16 -07003193
chiyotsai9c484b32019-03-07 16:01:50 -08003194#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08003195 av1_zero(cpi->partition_stats);
3196#endif
3197
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003198#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
3199 cpi->fn_ptr[BT].sdf = SDF; \
3200 cpi->fn_ptr[BT].sdaf = SDAF; \
3201 cpi->fn_ptr[BT].vf = VF; \
3202 cpi->fn_ptr[BT].svf = SVF; \
3203 cpi->fn_ptr[BT].svaf = SVAF; \
3204 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
3205 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07003206 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003207
Cheng Chenf78632e2017-10-20 15:30:51 -07003208 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003209 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003210 aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
3211 aom_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003212
3213 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003214 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003215 aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
3216 aom_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003217
3218 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003219 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003220 aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
3221 aom_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003222
3223 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003224 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003225 aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
3226 aom_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003227
3228 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003229 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003230 aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
3231 aom_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003232
3233 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003234 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003235 aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
3236 aom_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003237
Cheng Chenf78632e2017-10-20 15:30:51 -07003238 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
3239 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003240 aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
3241 aom_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07003242
3243 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003244 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003245 aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
3246 aom_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003247
3248 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003249 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003250 aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
3251 aom_dist_wtd_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07003252
3253 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003254 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003255 aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
3256 aom_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003257
3258 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003259 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003260 aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
3261 aom_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003262
3263 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003264 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003265 aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
3266 aom_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003267
3268 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003269 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003270 aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
3271 aom_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003272
3273 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
3274 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003275 aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
3276 aom_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003277
3278 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
3279 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003280 aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
3281 aom_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003282
3283 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
3284 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003285 aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
3286 aom_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003287
3288 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003289 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003290 aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
3291 aom_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003292
3293 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003294 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003295 aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
3296 aom_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003297
3298 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003299 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003300 aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003301
3302 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003303 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003304 aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003305
3306 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003307 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003308 aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003309
3310 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003311 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003312 aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003313
Yaowu Xuc27fc142016-08-22 16:08:15 -07003314#define OBFP(BT, OSDF, OVF, OSVF) \
3315 cpi->fn_ptr[BT].osdf = OSDF; \
3316 cpi->fn_ptr[BT].ovf = OVF; \
3317 cpi->fn_ptr[BT].osvf = OSVF;
3318
Yaowu Xuf883b422016-08-30 14:01:10 -07003319 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
3320 aom_obmc_sub_pixel_variance128x128)
3321 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
3322 aom_obmc_sub_pixel_variance128x64)
3323 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
3324 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07003325 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
3326 aom_obmc_sub_pixel_variance64x64)
3327 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
3328 aom_obmc_sub_pixel_variance64x32)
3329 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
3330 aom_obmc_sub_pixel_variance32x64)
3331 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
3332 aom_obmc_sub_pixel_variance32x32)
3333 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
3334 aom_obmc_sub_pixel_variance32x16)
3335 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
3336 aom_obmc_sub_pixel_variance16x32)
3337 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
3338 aom_obmc_sub_pixel_variance16x16)
3339 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
3340 aom_obmc_sub_pixel_variance16x8)
3341 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
3342 aom_obmc_sub_pixel_variance8x16)
3343 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
3344 aom_obmc_sub_pixel_variance8x8)
3345 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
3346 aom_obmc_sub_pixel_variance4x8)
3347 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
3348 aom_obmc_sub_pixel_variance8x4)
3349 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
3350 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003351 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
3352 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003353 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
3354 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003355 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
3356 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003357 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
3358 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003359 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
3360 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003361 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
3362 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003363
David Barkerf19f35f2017-05-22 16:33:22 +01003364#define MBFP(BT, MCSDF, MCSVF) \
3365 cpi->fn_ptr[BT].msdf = MCSDF; \
3366 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003367
David Barkerf19f35f2017-05-22 16:33:22 +01003368 MBFP(BLOCK_128X128, aom_masked_sad128x128,
3369 aom_masked_sub_pixel_variance128x128)
3370 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
3371 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01003372 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
3373 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
3374 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
3375 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
3376 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
3377 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
3378 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
3379 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
3380 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
3381 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
3382 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
3383 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
3384 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003385
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003386 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
3387
3388 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
3389
3390 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
3391
3392 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003393
3394 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
3395
3396 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01003397
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003398#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003399 highbd_set_var_fns(cpi);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003400#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003401
Yaowu Xuf883b422016-08-30 14:01:10 -07003402 /* av1_init_quantizer() is first called here. Add check in
3403 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07003404 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07003405 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003406 */
Yaowu Xuf883b422016-08-30 14:01:10 -07003407 av1_init_quantizer(cpi);
Zoe Liud902b742018-02-19 17:02:41 -08003408 av1_qm_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003409
Yaowu Xuf883b422016-08-30 14:01:10 -07003410 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07003411 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003412 cm->superres_upscaled_width = oxcf->width;
3413 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07003414 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003415
3416 cm->error.setjmp = 0;
3417
3418 return cpi;
3419}
3420
Urvang Joshiee2c8112018-05-04 14:53:15 -07003421#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003422#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
3423
3424#define SNPRINT2(H, T, V) \
3425 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07003426#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003427
Yaowu Xuf883b422016-08-30 14:01:10 -07003428void av1_remove_compressor(AV1_COMP *cpi) {
3429 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003430 unsigned int i;
3431 int t;
3432
3433 if (!cpi) return;
3434
3435 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003436 const int num_planes = av1_num_planes(cm);
3437
David Turnerd2a592e2018-11-16 14:59:31 +00003438 if (cm->current_frame.frame_number > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003439#if CONFIG_ENTROPY_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303440 if (!is_stat_generation_stage(cpi)) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003441 fprintf(stderr, "Writing counts.stt\n");
3442 FILE *f = fopen("counts.stt", "wb");
3443 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
3444 fclose(f);
3445 }
3446#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003447#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003448 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003449
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303450 if (!is_stat_generation_stage(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003451 char headings[512] = { 0 };
3452 char results[512] = { 0 };
3453 FILE *f = fopen("opsnr.stt", "a");
3454 double time_encoded =
3455 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
3456 10000000.000;
3457 double total_encode_time =
3458 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3459 const double dr =
3460 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3461 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3462 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3463 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3464
3465 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003466 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003467 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3468 const double total_ssim =
3469 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3470 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08003471 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003472 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003473 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07003474 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003475 snprintf(results, sizeof(results),
3476 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3477 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003478 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07003479 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003480 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
3481 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
3482 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
3483 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003484 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003485 cpi->psnr.stat[STAT_Y] / cpi->count,
3486 cpi->psnr.stat[STAT_U] / cpi->count,
Yue Chenf0652ed2019-08-13 16:09:25 -07003487 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003488
3489 if (cpi->b_calculate_blockiness) {
3490 SNPRINT(headings, "\t Block\tWstBlck");
3491 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3492 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3493 }
3494
3495 if (cpi->b_calculate_consistency) {
3496 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003497 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003498 (double)cpi->total_inconsistency);
3499
3500 SNPRINT(headings, "\tConsist\tWstCons");
3501 SNPRINT2(results, "\t%7.3f", consistency);
3502 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3503 }
Yue Chenf0652ed2019-08-13 16:09:25 -07003504
3505 SNPRINT(headings, "\t Time\tRcErr\tAbsErr");
3506 SNPRINT2(results, "\t%8.0f", total_encode_time);
3507 SNPRINT2(results, "\t%7.2f", rate_err);
3508 SNPRINT2(results, "\t%7.2f", fabs(rate_err));
3509
3510 fprintf(f, "%s\tAPsnr611\n", headings);
3511 fprintf(f, "%s\t%7.3f\n", results,
3512 (6 * cpi->psnr.stat[STAT_Y] + cpi->psnr.stat[STAT_U] +
3513 cpi->psnr.stat[STAT_V]) /
3514 (cpi->count * 8));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003515 }
3516
3517 fclose(f);
3518 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07003519#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003520#if CONFIG_SPEED_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303521 if (!is_stat_generation_stage(cpi)) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003522 fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
3523 }
3524#endif // CONFIG_SPEED_STATS
chiyotsai92ed0dd2019-01-25 14:50:14 -08003525
chiyotsai9c484b32019-03-07 16:01:50 -08003526#if CONFIG_COLLECT_PARTITION_STATS == 2
Mufaddal Chakerae7326122019-12-04 14:49:09 +05303527 if (!is_stat_generation_stage(cpi)) {
chiyotsai92ed0dd2019-01-25 14:50:14 -08003528 av1_print_partition_stats(&cpi->partition_stats);
3529 }
3530#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003531 }
3532
Yue Chenc9b23e02019-04-10 16:54:03 -07003533 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
Jingning Han81d6fbb2019-07-15 10:14:13 -07003534 aom_free(cpi->tpl_stats_buffer[frame].tpl_stats_ptr);
3535 cpi->tpl_stats_buffer[frame].is_valid = 0;
Jingning Han5e98d3b2019-09-23 21:59:36 -07003536
3537 aom_free_frame_buffer(&cpi->tpl_stats_buffer[frame].rec_picture_buf);
3538 cpi->tpl_stats_buffer[frame].rec_picture = NULL;
Yue Chen7cae98f2018-08-24 10:43:16 -07003539 }
3540
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303541 for (t = cpi->num_workers - 1; t >= 0; --t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003542 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003543 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3544
3545 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003546 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003547
3548 // Deallocate allocated thread data.
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303549 aom_free(thread_data->td->tctx);
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303550 if (t > 0) {
hui sud9a812b2017-07-06 14:34:37 -07003551 aom_free(thread_data->td->palette_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003552 aom_free(thread_data->td->tmp_conv_dst);
Hui Su38711e72019-06-11 10:49:47 -07003553 av1_release_compound_type_rd_buffers(&thread_data->td->comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003554 for (int j = 0; j < 2; ++j) {
3555 aom_free(thread_data->td->tmp_obmc_bufs[j]);
3556 }
Jingning Hand064cf02017-06-01 10:00:39 -07003557 aom_free(thread_data->td->above_pred_buf);
3558 aom_free(thread_data->td->left_pred_buf);
3559 aom_free(thread_data->td->wsrc_buf);
wenyao.liu22d8ab32018-10-16 09:11:29 +08003560
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303561 aom_free(thread_data->td->inter_modes_info);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003562 for (int x = 0; x < 2; x++) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303563 for (int y = 0; y < 2; y++) {
3564 aom_free(thread_data->td->hash_value_buffer[x][y]);
3565 thread_data->td->hash_value_buffer[x][y] = NULL;
3566 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003567 }
Jingning Hand064cf02017-06-01 10:00:39 -07003568 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003569 aom_free(thread_data->td->counts);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003570 av1_free_pc_tree(thread_data->td, num_planes);
Remya0cce44c2019-08-16 11:57:24 +05303571 aom_free(thread_data->td->mbmi_ext);
Yaowu Xuf883b422016-08-30 14:01:10 -07003572 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003573 }
3574 }
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303575#if CONFIG_MULTITHREAD
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303576 if (cpi->row_mt_mutex_ != NULL) {
3577 pthread_mutex_destroy(cpi->row_mt_mutex_);
3578 aom_free(cpi->row_mt_mutex_);
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303579 }
3580#endif
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05303581 av1_row_mt_mem_dealloc(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07003582 aom_free(cpi->tile_thr_data);
3583 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003584
Deepa K G964e72e2018-05-16 16:56:01 +05303585 if (cpi->num_workers > 1) {
3586 av1_loop_filter_dealloc(&cpi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05303587 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
Deepa K G964e72e2018-05-16 16:56:01 +05303588 }
3589
Yaowu Xuc27fc142016-08-22 16:08:15 -07003590 dealloc_compressor_data(cpi);
3591
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003592#if CONFIG_INTERNAL_STATS
3593 aom_free(cpi->ssim_vars);
3594 cpi->ssim_vars = NULL;
3595#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003596
Yaowu Xuf883b422016-08-30 14:01:10 -07003597 av1_remove_common(cm);
RogerZhou80d52342017-11-20 10:56:26 -08003598 for (i = 0; i < FRAME_BUFFERS; ++i) {
3599 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
3600 }
Yaowu Xu74e63352019-05-06 09:21:33 -07003601#if CONFIG_HTB_TRELLIS
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08003602 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xu74e63352019-05-06 09:21:33 -07003603#endif // CONFIG_HTB_TRELLIS
Yaowu Xuf883b422016-08-30 14:01:10 -07003604 av1_free_ref_frame_buffers(cm->buffer_pool);
Mufaddal Chakera74c9cbe2020-01-17 16:44:59 +05303605
3606 aom_free(cpi->twopass.total_stats);
3607 aom_free(cpi->twopass.total_left_stats);
3608
Yaowu Xuf883b422016-08-30 14:01:10 -07003609 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003610
3611#ifdef OUTPUT_YUV_SKINMAP
3612 fclose(yuv_skinmap_file);
3613#endif
3614#ifdef OUTPUT_YUV_REC
3615 fclose(yuv_rec_file);
3616#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003617}
3618
Yaowu Xuf883b422016-08-30 14:01:10 -07003619static void generate_psnr_packet(AV1_COMP *cpi) {
3620 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003621 int i;
3622 PSNR_STATS psnr;
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003623#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xue75b58a2020-01-03 12:56:12 -08003624 const uint32_t in_bit_depth = cpi->oxcf.input_bit_depth;
3625 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
3626 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
3627 bit_depth, in_bit_depth);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003628#else
3629 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
3630#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003631
3632 for (i = 0; i < 4; ++i) {
3633 pkt.data.psnr.samples[i] = psnr.samples[i];
3634 pkt.data.psnr.sse[i] = psnr.sse[i];
3635 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3636 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003637 pkt.kind = AOM_CODEC_PSNR_PKT;
3638 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003639}
3640
Yaowu Xuf883b422016-08-30 14:01:10 -07003641int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003642 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3643
Yunqing Wangf2e7a392017-11-08 00:27:21 -08003644 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003645 return 0;
3646}
3647
Thomas Daede497d1952017-08-08 17:33:06 -07003648int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3649 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003650 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003651 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003652 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003653 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003654 return 0;
3655 } else {
3656 return -1;
3657 }
3658}
3659
Thomas Daede497d1952017-08-08 17:33:06 -07003660int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3661 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003662 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003663 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003664 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003665 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003666 return 0;
3667 } else {
3668 return -1;
3669 }
3670}
3671
3672int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003673 cpi->ext_refresh_frame_context = update;
3674 cpi->ext_refresh_frame_context_pending = 1;
3675 return 0;
3676}
3677
3678#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3679// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3680// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3681// not denoise the UV channels at this time. If ever we implement UV channel
3682// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003683void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003684 uint8_t *src = s->y_buffer;
3685 int h = s->y_height;
3686
3687 do {
3688 fwrite(src, s->y_width, 1, f);
3689 src += s->y_stride;
3690 } while (--h);
3691
3692 src = s->u_buffer;
3693 h = s->uv_height;
3694
3695 do {
3696 fwrite(src, s->uv_width, 1, f);
3697 src += s->uv_stride;
3698 } while (--h);
3699
3700 src = s->v_buffer;
3701 h = s->uv_height;
3702
3703 do {
3704 fwrite(src, s->uv_width, 1, f);
3705 src += s->uv_stride;
3706 } while (--h);
3707}
3708#endif
3709
Yaowu Xuc27fc142016-08-22 16:08:15 -07003710#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003711void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003712 uint8_t *src = s->y_buffer;
3713 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003714 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003715 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3716 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3717
3718 do {
3719 fwrite(src16, s->y_width, 2, yuv_rec_file);
3720 src16 += s->y_stride;
3721 } while (--h);
3722
3723 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3724 h = s->uv_height;
3725
3726 do {
3727 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3728 src16 += s->uv_stride;
3729 } while (--h);
3730
3731 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3732 h = s->uv_height;
3733
3734 do {
3735 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3736 src16 += s->uv_stride;
3737 } while (--h);
3738
3739 fflush(yuv_rec_file);
3740 return;
3741 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003742
3743 do {
3744 fwrite(src, s->y_width, 1, yuv_rec_file);
3745 src += s->y_stride;
3746 } while (--h);
3747
3748 src = s->u_buffer;
3749 h = s->uv_height;
3750
3751 do {
3752 fwrite(src, s->uv_width, 1, yuv_rec_file);
3753 src += s->uv_stride;
3754 } while (--h);
3755
3756 src = s->v_buffer;
3757 h = s->uv_height;
3758
3759 do {
3760 fwrite(src, s->uv_width, 1, yuv_rec_file);
3761 src += s->uv_stride;
3762 } while (--h);
3763
3764 fflush(yuv_rec_file);
3765}
3766#endif // OUTPUT_YUV_REC
3767
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003768#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003769static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3770 int i;
3771 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003772 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003773 AV1_COMMON *const cm = &cpi->common;
3774 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3775 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003776 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003777 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003778 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003779 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003780 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003781 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003782 // TODO(sarahparker): The earlier condition for recoding here was:
3783 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3784 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003785 }
3786 }
3787 return recode;
3788}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003789
Yaowu Xuc27fc142016-08-22 16:08:15 -07003790// Function to test for conditions that indicate we should loop
3791// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003792static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3793 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003794 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003795 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003796 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3797 int force_recode = 0;
3798
3799 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
chiyotsai6b430132019-12-18 10:33:51 -08003800 (cpi->sf.hl_sf.recode_loop == ALLOW_RECODE) ||
3801 (frame_is_kfgfarf &&
3802 (cpi->sf.hl_sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003803 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3804 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3805 (rc->projected_frame_size < low_limit && q > minq)) {
3806 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003807 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003808 // Deal with frame undershoot and whether or not we are
3809 // below the automatically set cq level.
3810 if (q > oxcf->cq_level &&
3811 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3812 force_recode = 1;
3813 }
3814 }
3815 }
3816 return force_recode;
3817}
3818
Cheng Chen46f30c72017-09-07 11:13:33 -07003819static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003820 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003821 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003822 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003823
3824 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003825 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Jingning Han667561a2019-07-22 15:48:01 -07003826 if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003827 BufferPool *const pool = cm->buffer_pool;
3828 const YV12_BUFFER_CONFIG *const ref =
David Turnera21966b2018-12-05 14:48:49 +00003829 get_ref_frame_yv12_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003830
3831 if (ref == NULL) {
David Turnere7ebf902018-12-04 14:04:55 +00003832 cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003833 continue;
3834 }
3835
Yaowu Xuc27fc142016-08-22 16:08:15 -07003836 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
Debargha Mukherjee13cccf22019-03-27 23:49:14 -07003837 // Replace the reference buffer with a copy having a thicker border,
3838 // if the reference buffer is higher resolution than the current
3839 // frame, and the border is thin.
3840 if ((ref->y_crop_width > cm->width ||
3841 ref->y_crop_height > cm->height) &&
3842 ref->border < AOM_BORDER_IN_PIXELS) {
3843 RefCntBuffer *ref_fb = get_ref_frame_buf(cm, ref_frame);
3844 if (aom_yv12_realloc_with_new_border(
3845 &ref_fb->buf, AOM_BORDER_IN_PIXELS, cm->byte_alignment,
3846 num_planes) != 0) {
3847 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3848 "Failed to allocate frame buffer");
3849 }
3850 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003851 int force_scaling = 0;
David Turnere7ebf902018-12-04 14:04:55 +00003852 RefCntBuffer *new_fb = cpi->scaled_ref_buf[ref_frame - 1];
3853 if (new_fb == NULL) {
3854 const int new_fb_idx = get_free_fb(cm);
3855 if (new_fb_idx == INVALID_IDX) {
Wan-Teh Chang4a8c0042018-10-05 09:41:52 -07003856 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3857 "Unable to find free frame buffer");
David Turnere7ebf902018-12-04 14:04:55 +00003858 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003859 force_scaling = 1;
David Turnere7ebf902018-12-04 14:04:55 +00003860 new_fb = &pool->frame_bufs[new_fb_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003861 }
David Turnere7ebf902018-12-04 14:04:55 +00003862
3863 if (force_scaling || new_fb->buf.y_crop_width != cm->width ||
3864 new_fb->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003865 if (aom_realloc_frame_buffer(
David Turnere7ebf902018-12-04 14:04:55 +00003866 &new_fb->buf, cm->width, cm->height,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003867 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
Debargha Mukherjeefa946af2019-03-26 16:58:55 -07003868 cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003869 cm->byte_alignment, NULL, NULL, NULL)) {
3870 if (force_scaling) {
3871 // Release the reference acquired in the get_free_fb() call above.
David Turnere7ebf902018-12-04 14:04:55 +00003872 --new_fb->ref_count;
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003873 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003874 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003875 "Failed to allocate frame buffer");
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003876 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003877 av1_resize_and_extend_frame(
David Turnere7ebf902018-12-04 14:04:55 +00003878 ref, &new_fb->buf, (int)cm->seq_params.bit_depth, num_planes);
3879 cpi->scaled_ref_buf[ref_frame - 1] = new_fb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003880 alloc_frame_mvs(cm, new_fb);
3881 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003882 } else {
David Turnera21966b2018-12-05 14:48:49 +00003883 RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003884 buf->buf.y_crop_width = ref->y_crop_width;
3885 buf->buf.y_crop_height = ref->y_crop_height;
David Turnere7ebf902018-12-04 14:04:55 +00003886 cpi->scaled_ref_buf[ref_frame - 1] = buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003887 ++buf->ref_count;
3888 }
3889 } else {
Mufaddal Chakera3bcc72c2019-12-11 14:38:37 +05303890 if (!has_no_stats_stage(cpi)) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003891 }
3892 }
3893}
3894
Yaowu Xuf883b422016-08-30 14:01:10 -07003895static void release_scaled_references(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003896 // TODO(isbs): only refresh the necessary frames, rather than all of them
David Turnere7ebf902018-12-04 14:04:55 +00003897 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3898 RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
3899 if (buf != NULL) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003900 --buf->ref_count;
David Turnere7ebf902018-12-04 14:04:55 +00003901 cpi->scaled_ref_buf[i] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003902 }
3903 }
3904}
3905
Yaowu Xuf883b422016-08-30 14:01:10 -07003906static void set_mv_search_params(AV1_COMP *cpi) {
3907 const AV1_COMMON *const cm = &cpi->common;
Yunqing Wang4b7bf402020-01-28 15:20:53 -08003908 const int max_mv_def = AOMMAX(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003909
3910 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003911 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003912
chiyotsai8cc054a2019-12-12 14:57:43 -08003913 if (cpi->sf.mv_sf.auto_mv_step_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003914 if (frame_is_intra_only(cm)) {
3915 // Initialize max_mv_magnitude for use in the first INTER frame
3916 // after a key/intra-only frame.
3917 cpi->max_mv_magnitude = max_mv_def;
3918 } else {
Yunqing Wang4b7bf402020-01-28 15:20:53 -08003919 // Use cpi->max_mv_magnitude == -1 to exclude first pass case.
3920 if (cm->show_frame && cpi->max_mv_magnitude != -1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003921 // Allow mv_steps to correspond to twice the max mv magnitude found
3922 // in the previous frame, capped by the default max_mv_magnitude based
3923 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003924 cpi->mv_step_param = av1_init_search_range(
3925 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003926 }
Yunqing Wang4b7bf402020-01-28 15:20:53 -08003927 cpi->max_mv_magnitude = -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003928 }
3929 }
3930}
3931
Hui Subdf0c992019-02-14 14:52:41 -08003932static void set_screen_content_options(AV1_COMP *cpi) {
3933 AV1_COMMON *cm = &cpi->common;
3934
3935 if (cm->seq_params.force_screen_content_tools != 2) {
3936 cm->allow_screen_content_tools = cm->allow_intrabc =
3937 cm->seq_params.force_screen_content_tools;
3938 return;
3939 }
3940
3941 if (cpi->oxcf.content == AOM_CONTENT_SCREEN) {
3942 cm->allow_screen_content_tools = cm->allow_intrabc = 1;
3943 return;
3944 }
3945
3946 // Estimate if the source frame is screen content, based on the portion of
3947 // blocks that have few luma colors.
Cheng Chen4f666ca2019-11-18 17:05:03 -08003948 const uint8_t *src = cpi->unfiltered_source->y_buffer;
Hui Subdf0c992019-02-14 14:52:41 -08003949 assert(src != NULL);
Cheng Chen4f666ca2019-11-18 17:05:03 -08003950 const int use_hbd = cpi->unfiltered_source->flags & YV12_FLAG_HIGHBITDEPTH;
3951 const int stride = cpi->unfiltered_source->y_stride;
3952 const int width = cpi->unfiltered_source->y_width;
3953 const int height = cpi->unfiltered_source->y_height;
Hui Subdf0c992019-02-14 14:52:41 -08003954 const int bd = cm->seq_params.bit_depth;
3955 const int blk_w = 16;
3956 const int blk_h = 16;
3957 // These threshold values are selected experimentally.
3958 const int color_thresh = 4;
3959 const unsigned int var_thresh = 0;
3960 // Counts of blocks with no more than color_thresh colors.
3961 int counts_1 = 0;
3962 // Counts of blocks with no more than color_thresh colors and variance larger
3963 // than var_thresh.
3964 int counts_2 = 0;
3965
3966 for (int r = 0; r + blk_h <= height; r += blk_h) {
3967 for (int c = 0; c + blk_w <= width; c += blk_w) {
3968 int count_buf[1 << 12]; // Maximum (1 << 12) color levels.
3969 const uint8_t *const this_src = src + r * stride + c;
3970 const int n_colors =
3971 use_hbd ? av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd,
3972 count_buf)
3973 : av1_count_colors(this_src, stride, blk_w, blk_h, count_buf);
3974 if (n_colors > 1 && n_colors <= color_thresh) {
3975 ++counts_1;
3976 struct buf_2d buf;
3977 buf.stride = stride;
3978 buf.buf = (uint8_t *)this_src;
3979 const unsigned int var =
3980 use_hbd
3981 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
3982 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
3983 if (var > var_thresh) ++counts_2;
3984 }
3985 }
3986 }
3987
3988 // The threshold values are selected experimentally.
3989 cm->allow_screen_content_tools =
3990 counts_1 * blk_h * blk_w * 10 > width * height;
3991 // IntraBC would force loop filters off, so we use more strict rules that also
3992 // requires that the block has high variance.
3993 cm->allow_intrabc = cm->allow_screen_content_tools &&
Hui Su64a2ffb2019-04-11 16:47:13 -07003994 counts_2 * blk_h * blk_w * 12 > width * height;
Hui Subdf0c992019-02-14 14:52:41 -08003995}
3996
Yaowu Xuf883b422016-08-30 14:01:10 -07003997static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003998 int i;
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003999 AV1_COMMON *cm = &cpi->common;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004000 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07004001 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004002 }
4003 cpi->global_motion_search_done = 0;
Yunqing Wang9411e432019-03-14 15:53:23 -07004004
4005 if (frame_is_intra_only(cm)) set_screen_content_options(cpi);
4006 cpi->is_screen_content_type = (cm->allow_screen_content_tools != 0);
4007
David Turner04b70d82019-01-24 15:39:19 +00004008 av1_set_speed_features_framesize_independent(cpi, cpi->speed);
Yaowu Xuf883b422016-08-30 14:01:10 -07004009 av1_set_rd_speed_thresholds(cpi);
Debargha Mukherjeedf713102018-10-02 12:33:32 -07004010 cm->interp_filter = SWITCHABLE;
4011 cm->switchable_motion_mode = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004012}
4013
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004014#if !CONFIG_REALTIME_ONLY
Aasaipriya197771d2020-02-06 20:11:21 +05304015static double get_kf_boost_projection_factor(int frame_count) {
4016 double factor = sqrt((double)frame_count);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07004017 factor = AOMMIN(factor, 10.0);
4018 factor = AOMMAX(factor, 4.0);
Aasaipriya197771d2020-02-06 20:11:21 +05304019 factor = (75.0 + 14.0 * factor);
4020 return factor;
4021}
4022
4023static int get_kf_boost_from_r0(double r0, int frames_to_key) {
4024 double factor = get_kf_boost_projection_factor(frames_to_key);
4025 const int boost = (int)rint(factor / r0);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07004026 return boost;
4027}
Aasaipriya197771d2020-02-06 20:11:21 +05304028
4029static int get_projected_prior_boost(AV1_COMP *cpi) {
4030 /*
4031 * If num_stats_used_for_kf_boost >= frames_to_key, then
4032 * all stats needed for prior boost calculation are available.
4033 * Hence projecting the prior boost is not needed in this cases.
4034 */
4035 if (cpi->rc.num_stats_used_for_kf_boost >= cpi->rc.frames_to_key)
4036 return cpi->rc.kf_boost;
4037
4038 // Get the current tpl factor (number of frames = frames_to_key).
4039 double tpl_factor = get_kf_boost_projection_factor(cpi->rc.frames_to_key);
4040 // Get the tpl factor when number of frames = num_stats_used_for_kf_boost.
4041 double tpl_factor_num_stats =
4042 get_kf_boost_projection_factor(cpi->rc.num_stats_used_for_kf_boost);
4043 int projected_kf_boost =
4044 (int)rint((tpl_factor * cpi->rc.kf_boost) / tpl_factor_num_stats);
4045 return projected_kf_boost;
4046}
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004047#endif
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07004048
4049int combine_prior_with_tpl_boost(int prior_boost, int tpl_boost,
4050 int frames_to_key) {
4051 double factor = sqrt((double)frames_to_key);
4052 factor = AOMMIN(factor, 12.0);
4053 factor = AOMMAX(factor, 4.0);
4054 factor -= 4.0;
4055 int boost = (int)((factor * prior_boost + (8.0 - factor) * tpl_boost) / 8.0);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07004056 return boost;
4057}
4058
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004059#if !CONFIG_REALTIME_ONLY
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004060static void process_tpl_stats_frame(AV1_COMP *cpi) {
Sarah Parkere1b22012019-06-06 16:35:25 -07004061 const GF_GROUP *const gf_group = &cpi->gf_group;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004062 AV1_COMMON *const cm = &cpi->common;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004063
Sarah Parkere1b22012019-06-06 16:35:25 -07004064 assert(IMPLIES(gf_group->size > 0, gf_group->index < gf_group->size));
Jingning Han31a0ee92019-07-15 13:56:55 -07004065
4066 const int tpl_idx = gf_group->index;
Jingning Han81d6fbb2019-07-15 10:14:13 -07004067 TplDepFrame *tpl_frame = &cpi->tpl_frame[tpl_idx];
Yue Chen4e585cc2019-06-03 14:47:16 -07004068 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004069
Yue Chen4e585cc2019-06-03 14:47:16 -07004070 if (tpl_frame->is_valid) {
4071 int tpl_stride = tpl_frame->stride;
4072 int64_t intra_cost_base = 0;
4073 int64_t mc_dep_cost_base = 0;
Yue Chenbd934232019-08-05 14:23:39 -07004074#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07004075 int64_t mc_saved_base = 0;
4076 int64_t mc_count_base = 0;
Yue Chenbd934232019-08-05 14:23:39 -07004077#endif // !USE_TPL_CLASSIC_MODEL
Yue Chenb4c93f02019-08-05 13:47:31 -07004078 const int step = 1 << cpi->tpl_stats_block_mis_log2;
Debargha Mukherjeeedfa4fe2019-07-08 14:23:39 -07004079 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
Yue Chenb4c93f02019-08-05 13:47:31 -07004080
4081 for (int row = 0; row < cm->mi_rows; row += step) {
4082 for (int col = 0; col < mi_cols_sr; col += step) {
4083 TplDepStats *this_stats =
4084 &tpl_stats[av1_tpl_ptr_pos(cpi, row, col, tpl_stride)];
Jingning Han40e870f2019-10-02 16:05:39 -07004085 int64_t mc_dep_delta =
4086 RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
4087 this_stats->mc_dep_dist);
4088 intra_cost_base += (this_stats->recrf_dist << RDDIV_BITS);
4089 mc_dep_cost_base +=
4090 (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta;
Yue Chenbd934232019-08-05 14:23:39 -07004091#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07004092 mc_count_base += this_stats->mc_count;
4093 mc_saved_base += this_stats->mc_saved;
Yue Chenbd934232019-08-05 14:23:39 -07004094#endif // !USE_TPL_CLASSIC_MODEL
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004095 }
Yue Chen4e585cc2019-06-03 14:47:16 -07004096 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004097
Yue Chen4e585cc2019-06-03 14:47:16 -07004098 if (mc_dep_cost_base == 0) {
4099 tpl_frame->is_valid = 0;
4100 } else {
4101 aom_clear_system_state();
4102 cpi->rd.r0 = (double)intra_cost_base / mc_dep_cost_base;
Jingning Han44a573b2019-09-04 14:01:22 -07004103 if (is_frame_arf_and_tpl_eligible(gf_group)) {
Yue Chen4e585cc2019-06-03 14:47:16 -07004104 cpi->rd.arf_r0 = cpi->rd.r0;
Jingning Han491198d2020-02-13 21:53:41 -08004105 const int gfu_boost = (int)(200.0 / cpi->rd.r0);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07004106 cpi->rc.gfu_boost = combine_prior_with_tpl_boost(
4107 cpi->rc.gfu_boost, gfu_boost, cpi->rc.frames_to_key);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07004108 } else if (frame_is_intra_only(cm)) {
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07004109 // TODO(debargha): Turn off q adjustment for kf temporarily to
4110 // reduce impact on speed of encoding. Need to investigate how
4111 // to mitigate the issue.
4112 if (cpi->oxcf.rc_mode == AOM_Q) {
4113 const int kf_boost =
4114 get_kf_boost_from_r0(cpi->rd.r0, cpi->rc.frames_to_key);
Aasaipriya197771d2020-02-06 20:11:21 +05304115 if (cpi->lap_enabled) {
4116 const int projected_prior_boost = get_projected_prior_boost(cpi);
4117 cpi->rc.kf_boost = combine_prior_with_tpl_boost(
4118 projected_prior_boost, kf_boost,
4119 cpi->rc.num_stats_used_for_kf_boost);
4120 } else {
4121 cpi->rc.kf_boost = combine_prior_with_tpl_boost(
4122 cpi->rc.kf_boost, kf_boost, cpi->rc.frames_to_key);
4123 }
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07004124 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004125 }
Yue Chenbd934232019-08-05 14:23:39 -07004126#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07004127 cpi->rd.mc_count_base =
4128 (double)mc_count_base / (cm->mi_rows * cm->mi_cols);
4129 cpi->rd.mc_saved_base =
4130 (double)mc_saved_base / (cm->mi_rows * cm->mi_cols);
Yue Chenbd934232019-08-05 14:23:39 -07004131#endif // !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07004132 aom_clear_system_state();
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004133 }
4134 }
4135}
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004136#endif // !CONFIG_REALTIME_ONLY
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004137
Yaowu Xuf883b422016-08-30 14:01:10 -07004138static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004139 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004140 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004141
4142 // Setup variables that depend on the dimensions of the frame.
David Turner04b70d82019-01-24 15:39:19 +00004143 av1_set_speed_features_framesize_dependent(cpi, cpi->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004144
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004145#if !CONFIG_REALTIME_ONLY
chiyotsai0fa717c2020-02-05 12:22:01 -08004146 if (cpi->oxcf.enable_tpl_model && is_frame_tpl_eligible(cpi)) {
Yue Chen4e585cc2019-06-03 14:47:16 -07004147 process_tpl_stats_frame(cpi);
sdengf46a1062019-08-04 18:43:50 -07004148 av1_tpl_rdmult_setup(cpi);
4149 }
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004150#endif
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004151
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01004152 // Decide q and q bounds.
Angie Chiang958904c2019-09-19 16:03:35 -07004153 *q = av1_rc_pick_q_and_bounds(cpi, &cpi->rc, cm->width, cm->height,
4154 cpi->gf_group.index, bottom_index, top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004155
Yaowu Xuc27fc142016-08-22 16:08:15 -07004156 // Configure experimental use of segmentation for enhanced coding of
4157 // static regions if indicated.
4158 // Only allowed in the second pass of a two pass encode, as it requires
4159 // lagged coding, and if the relevant speed feature flag is set.
chiyotsai6b430132019-12-18 10:33:51 -08004160 if (is_stat_consumption_stage_twopass(cpi) &&
4161 cpi->sf.hl_sf.static_segmentation)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004162 configure_static_seg_features(cpi);
4163}
4164
Yaowu Xuf883b422016-08-30 14:01:10 -07004165static void init_motion_estimation(AV1_COMP *cpi) {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004166 AV1_COMMON *const cm = &cpi->common;
chiyotsai836b69b2019-04-09 13:41:24 -07004167 const int y_stride = cpi->scaled_source.y_stride;
Urvang Joshi510d8f62019-01-10 12:11:50 -08004168 const int y_stride_src =
4169 ((cpi->oxcf.width != cm->width || cpi->oxcf.height != cm->height) ||
4170 av1_superres_scaled(cm))
4171 ? y_stride
4172 : cpi->lookahead->buf->img.y_stride;
chiyotsai836b69b2019-04-09 13:41:24 -07004173 // Update if ss_cfg is uninitialized or the current frame has a new stride
4174 const int should_update = !cpi->ss_cfg[SS_CFG_SRC].stride ||
4175 !cpi->ss_cfg[SS_CFG_LOOKAHEAD].stride ||
4176 (y_stride != cpi->ss_cfg[SS_CFG_SRC].stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004177
chiyotsai836b69b2019-04-09 13:41:24 -07004178 if (!should_update) {
4179 return;
4180 }
4181
chiyotsai8cc054a2019-12-12 14:57:43 -08004182 if (cpi->sf.mv_sf.search_method == DIAMOND) {
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05304183 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
4184 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD],
4185 y_stride_src);
chiyotsai836b69b2019-04-09 13:41:24 -07004186 } else {
chiyotsai836b69b2019-04-09 13:41:24 -07004187 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
4188 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD], y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004189 }
Jingning Han969a8d42019-12-16 19:40:14 -08004190 av1_init_motion_fpf(&cpi->ss_cfg[SS_CFG_FPF], y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004191}
4192
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004193#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004194static void set_restoration_unit_size(int width, int height, int sx, int sy,
4195 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004196 (void)width;
4197 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004198 (void)sx;
4199 (void)sy;
4200#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
4201 int s = AOMMIN(sx, sy);
4202#else
4203 int s = 0;
4204#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
4205
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07004206 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08004207 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07004208 else
Urvang Joshi813186b2018-03-08 15:38:46 -08004209 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004210 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
4211 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004212}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004213
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05304214static void init_ref_frame_bufs(AV1_COMP *cpi) {
4215 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07004216 int i;
4217 BufferPool *const pool = cm->buffer_pool;
Jack Haughtonddb80602018-11-21 16:41:49 +00004218 cm->cur_frame = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -07004219 for (i = 0; i < REF_FRAMES; ++i) {
David Turnere7ebf902018-12-04 14:04:55 +00004220 cm->ref_frame_map[i] = NULL;
Wan-Teh Changd05e0332018-10-03 12:00:43 -07004221 }
4222 for (i = 0; i < FRAME_BUFFERS; ++i) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004223 pool->frame_bufs[i].ref_count = 0;
4224 }
RogerZhou86902d02018-02-28 15:29:16 -08004225 if (cm->seq_params.force_screen_content_tools) {
Hui Su2d5fd742018-02-21 18:10:37 -08004226 for (i = 0; i < FRAME_BUFFERS; ++i) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05304227 av1_hash_table_init(&pool->frame_bufs[i].hash_table, &cpi->td.mb);
Hui Su2d5fd742018-02-21 18:10:37 -08004228 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004229 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004230}
4231
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05304232void av1_check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
4233 int subsampling_x, int subsampling_y) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004234 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004235 SequenceHeader *const seq_params = &cm->seq_params;
Cheng Chen46f30c72017-09-07 11:13:33 -07004236
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004237 if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
4238 seq_params->subsampling_x != subsampling_x ||
4239 seq_params->subsampling_y != subsampling_y) {
4240 seq_params->subsampling_x = subsampling_x;
4241 seq_params->subsampling_y = subsampling_y;
4242 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07004243
Jingning Han183b2a82019-12-18 16:03:41 -08004244 av1_set_speed_features_framesize_independent(cpi, cpi->oxcf.speed);
4245 av1_set_speed_features_framesize_dependent(cpi, cpi->oxcf.speed);
4246
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05304247 alloc_altref_frame_buffer(cpi);
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05304248 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07004249 alloc_util_frame_buffers(cpi);
4250
4251 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
4252
4253 cpi->initial_width = cm->width;
4254 cpi->initial_height = cm->height;
4255 cpi->initial_mbs = cm->MBs;
4256 }
4257}
4258
4259// Returns 1 if the assigned width or height was <= 0.
Marco Paniconi63971322019-08-15 21:32:05 -07004260int av1_set_size_literal(AV1_COMP *cpi, int width, int height) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004261 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004262 const int num_planes = av1_num_planes(cm);
Mufaddal Chakera02ac17f2019-12-09 18:09:55 +05304263 av1_check_initial_width(cpi, cm->seq_params.use_highbitdepth,
4264 cm->seq_params.subsampling_x,
4265 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07004266
4267 if (width <= 0 || height <= 0) return 1;
4268
4269 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07004270 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004271
4272 if (cpi->initial_width && cpi->initial_height &&
4273 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
4274 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004275 av1_free_pc_tree(&cpi->td, num_planes);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004276 alloc_compressor_data(cpi);
4277 realloc_segmentation_maps(cpi);
4278 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07004279 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004280 update_frame_size(cpi);
4281
4282 return 0;
4283}
4284
David Turner475a3132019-01-18 15:17:17 +00004285void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07004286 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004287 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004288 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004289 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004290 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004291
Fergus Simpsonbc189932017-05-16 17:02:39 -07004292 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07004293 // There has been a change in the encoded frame size
Marco Paniconi63971322019-08-15 21:32:05 -07004294 av1_set_size_literal(cpi, width, height);
Urvang Joshic8b52d52018-03-23 13:16:51 -07004295 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Cheng Chen09c83a52018-06-05 12:27:36 -07004296 cm->all_lossless = cm->coded_lossless && !av1_superres_scaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004297 }
Yunqing Wang4b7bf402020-01-28 15:20:53 -08004298 set_mv_search_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004299
Hamsalekha S37cc1d12019-12-12 19:27:41 +05304300 if (is_stat_consumption_stage(cpi)) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004301 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004302 }
4303
David Turnere7ebf902018-12-04 14:04:55 +00004304 alloc_frame_mvs(cm, cm->cur_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004305
Cherma Rajan A71d20db2018-04-27 11:15:32 +05304306 // Allocate above context buffers
Cherma Rajan Af1479082018-05-09 14:26:34 +05304307 if (cm->num_allocated_above_context_planes < av1_num_planes(cm) ||
4308 cm->num_allocated_above_context_mi_col < cm->mi_cols ||
Cherma Rajan A71d20db2018-04-27 11:15:32 +05304309 cm->num_allocated_above_contexts < cm->tile_rows) {
4310 av1_free_above_context_buffers(cm, cm->num_allocated_above_contexts);
4311 if (av1_alloc_above_context_buffers(cm, cm->tile_rows))
4312 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
4313 "Failed to allocate context buffers");
4314 }
4315
Yaowu Xuc27fc142016-08-22 16:08:15 -07004316 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004317 if (aom_realloc_frame_buffer(
Jack Haughtonddb80602018-11-21 16:41:49 +00004318 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
4319 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +05304320 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07004321 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004322 "Failed to allocate frame buffer");
4323
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004324 const int frame_width = cm->superres_upscaled_width;
4325 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004326 set_restoration_unit_size(frame_width, frame_height,
4327 seq_params->subsampling_x,
4328 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004329 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01004330 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004331
4332 av1_alloc_restoration_buffers(cm);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004333 alloc_util_frame_buffers(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004334 init_motion_estimation(cpi);
4335
4336 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00004337 RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00004338 if (buf != NULL) {
David Turnera21966b2018-12-05 14:48:49 +00004339 struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
4340 av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
David Turner1bcefb32018-11-19 17:54:00 +00004341 buf->buf.y_crop_height, cm->width,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08004342 cm->height);
David Turnera21966b2018-12-05 14:48:49 +00004343 if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004344 }
4345 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07004346
Hui Su5ebd8702018-01-08 18:09:20 -08004347 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08004348 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004349
4350 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
4351}
4352
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004353static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
4354 // Choose an arbitrary random number
4355 static unsigned int seed = 56789;
4356 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Mufaddal Chakerae7326122019-12-04 14:49:09 +05304357 if (is_stat_generation_stage(cpi)) return SCALE_NUMERATOR;
Urvang Joshide71d142017-10-05 12:12:15 -07004358 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004359
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07004360 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004361 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004362 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004363 case RESIZE_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00004364 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004365 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004366 else
Urvang Joshide71d142017-10-05 12:12:15 -07004367 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004368 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004369 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004370 default: assert(0);
4371 }
Urvang Joshide71d142017-10-05 12:12:15 -07004372 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004373}
4374
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004375#if CONFIG_SUPERRES_IN_RECODE
4376static int superres_in_recode_allowed(const AV1_COMP *const cpi) {
4377 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
4378 // Empirically found to not be beneficial for AOM_Q mode and images coding.
4379 return oxcf->superres_mode == SUPERRES_AUTO &&
4380 (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ) &&
4381 cpi->rc.frames_to_key > 1;
4382}
4383#endif // CONFIG_SUPERRES_IN_RECODE
4384
Urvang Joshi510d8f62019-01-10 12:11:50 -08004385#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO 0.012
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004386#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME 0.008
4387#define SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME 0.008
Urvang Joshi510d8f62019-01-10 12:11:50 -08004388#define SUPERRES_ENERGY_BY_AC_THRESH 0.2
4389
4390static double get_energy_by_q2_thresh(const GF_GROUP *gf_group,
4391 const RATE_CONTROL *rc) {
4392 // TODO(now): Return keyframe thresh * factor based on frame type / pyramid
4393 // level.
4394 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
4395 return SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME;
4396 } else if (gf_group->update_type[gf_group->index] == KF_UPDATE) {
4397 if (rc->frames_to_key <= 1)
4398 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO;
4399 else
4400 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME;
4401 } else {
4402 assert(0);
4403 }
4404 return 0;
4405}
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004406
4407static uint8_t get_superres_denom_from_qindex_energy(int qindex, double *energy,
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004408 double threshq,
4409 double threshp) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004410 const double q = av1_convert_qindex_to_q(qindex, AOM_BITS_8);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004411 const double tq = threshq * q * q;
4412 const double tp = threshp * energy[1];
4413 const double thresh = AOMMIN(tq, tp);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004414 int k;
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004415 for (k = SCALE_NUMERATOR * 2; k > SCALE_NUMERATOR; --k) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004416 if (energy[k - 1] > thresh) break;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004417 }
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004418 return 3 * SCALE_NUMERATOR - k;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004419}
4420
Urvang Joshi510d8f62019-01-10 12:11:50 -08004421static uint8_t get_superres_denom_for_qindex(const AV1_COMP *cpi, int qindex,
4422 int sr_kf, int sr_arf) {
4423 // Use superres for Key-frames and Alt-ref frames only.
4424 const GF_GROUP *gf_group = &cpi->gf_group;
4425 if (gf_group->update_type[gf_group->index] != KF_UPDATE &&
4426 gf_group->update_type[gf_group->index] != ARF_UPDATE) {
4427 return SCALE_NUMERATOR;
4428 }
4429 if (gf_group->update_type[gf_group->index] == KF_UPDATE && !sr_kf) {
4430 return SCALE_NUMERATOR;
4431 }
4432 if (gf_group->update_type[gf_group->index] == ARF_UPDATE && !sr_arf) {
4433 return SCALE_NUMERATOR;
4434 }
4435
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004436 double energy[16];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004437 analyze_hor_freq(cpi, energy);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004438
4439 const double energy_by_q2_thresh =
4440 get_energy_by_q2_thresh(gf_group, &cpi->rc);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004441 int denom = get_superres_denom_from_qindex_energy(
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004442 qindex, energy, energy_by_q2_thresh, SUPERRES_ENERGY_BY_AC_THRESH);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004443 /*
4444 printf("\nenergy = [");
4445 for (int k = 1; k < 16; ++k) printf("%f, ", energy[k]);
4446 printf("]\n");
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004447 printf("boost = %d\n",
4448 (gf_group->update_type[gf_group->index] == KF_UPDATE)
4449 ? cpi->rc.kf_boost
4450 : cpi->rc.gfu_boost);
4451 printf("denom = %d\n", denom);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004452 */
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004453#if CONFIG_SUPERRES_IN_RECODE
4454 if (superres_in_recode_allowed(cpi)) {
4455 // Force superres to be tried in the recode loop, as full-res is also going
4456 // to be tried anyway.
4457 denom = AOMMAX(denom, SCALE_NUMERATOR + 1);
4458 }
4459#endif // CONFIG_SUPERRES_IN_RECODE
Urvang Joshi510d8f62019-01-10 12:11:50 -08004460 return denom;
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004461}
4462
Urvang Joshibc82d382019-11-01 17:59:20 -07004463// If true, SUPERRES_AUTO mode will exhaustively search over all superres
4464// denominators for all frames (except overlay and internal overlay frames).
4465#define SUPERRES_RECODE_ALL_RATIOS 0
4466
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004467static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
4468 // Choose an arbitrary random number
4469 static unsigned int seed = 34567;
4470 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Mufaddal Chakerae7326122019-12-04 14:49:09 +05304471 if (is_stat_generation_stage(cpi)) return SCALE_NUMERATOR;
Urvang Joshide71d142017-10-05 12:12:15 -07004472 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07004473
4474 // Make sure that superres mode of the frame is consistent with the
4475 // sequence-level flag.
4476 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
4477 cpi->common.seq_params.enable_superres));
4478 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
4479 oxcf->superres_mode == SUPERRES_NONE));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004480
4481 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004482 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004483 case SUPERRES_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00004484 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004485 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004486 else
Urvang Joshide71d142017-10-05 12:12:15 -07004487 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004488 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004489 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004490 case SUPERRES_QTHRESH: {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07004491 // Do not use superres when screen content tools are used.
4492 if (cpi->common.allow_screen_content_tools) break;
Debargha Mukherjee2b2c5fd2018-11-14 13:21:24 -08004493 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4494 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004495
4496 // Now decide the use of superres based on 'q'.
Urvang Joshi2c92b072018-03-19 17:23:31 -07004497 int bottom_index, top_index;
4498 const int q = av1_rc_pick_q_and_bounds(
Angie Chiang958904c2019-09-19 16:03:35 -07004499 cpi, &cpi->rc, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
Jingning Han2e029872019-08-02 10:54:19 -07004500 &bottom_index, &top_index);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004501
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004502 const int qthresh = (frame_is_intra_only(&cpi->common))
4503 ? oxcf->superres_kf_qthresh
4504 : oxcf->superres_qthresh;
Urvang Joshib17e0a62019-01-11 16:11:54 -08004505 if (q <= qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07004506 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004507 } else {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004508 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004509 }
4510 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004511 }
Urvang Joshi36a83732019-01-31 15:31:57 -08004512 case SUPERRES_AUTO: {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004513 // Do not use superres when screen content tools are used.
Urvang Joshi36a83732019-01-31 15:31:57 -08004514 if (cpi->common.allow_screen_content_tools) break;
Urvang Joshi510d8f62019-01-10 12:11:50 -08004515 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4516 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi36a83732019-01-31 15:31:57 -08004517
4518 // Now decide the use of superres based on 'q'.
4519 int bottom_index, top_index;
4520 const int q = av1_rc_pick_q_and_bounds(
Angie Chiang958904c2019-09-19 16:03:35 -07004521 cpi, &cpi->rc, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
Jingning Han2e029872019-08-02 10:54:19 -07004522 &bottom_index, &top_index);
Urvang Joshi36a83732019-01-31 15:31:57 -08004523
4524 const int qthresh = 128;
4525 if (q <= qthresh) {
4526 new_denom = SCALE_NUMERATOR;
4527 } else {
Urvang Joshibc82d382019-11-01 17:59:20 -07004528#if SUPERRES_RECODE_ALL_RATIOS
4529 if (cpi->common.current_frame.frame_type == KEY_FRAME)
4530 new_denom = oxcf->superres_kf_scale_denominator;
4531 else
4532 new_denom = oxcf->superres_scale_denominator;
4533#else
Urvang Joshi510d8f62019-01-10 12:11:50 -08004534 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Urvang Joshibc82d382019-11-01 17:59:20 -07004535#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi36a83732019-01-31 15:31:57 -08004536 }
4537 break;
4538 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004539 default: assert(0);
4540 }
Urvang Joshide71d142017-10-05 12:12:15 -07004541 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004542}
4543
Urvang Joshide71d142017-10-05 12:12:15 -07004544static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4545 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4546}
4547
4548static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08004549 // Only need to check the width, as scaling is horizontal only.
4550 (void)oheight;
4551 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07004552}
4553
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004554static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004555 SUPERRES_MODE superres_mode, int owidth,
4556 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004557 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004558 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004559 }
4560
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004561 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07004562 int resize_denom =
4563 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4564 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4565
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004566 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004567 // Alter superres scale as needed to enforce conformity.
4568 rsz->superres_denom =
4569 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4570 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4571 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004572 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004573 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004574 // Alter resize scale as needed to enforce conformity.
4575 resize_denom =
4576 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004577 rsz->resize_width = owidth;
4578 rsz->resize_height = oheight;
4579 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004580 resize_denom);
4581 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4582 if (resize_denom > SCALE_NUMERATOR) {
4583 --resize_denom;
4584 rsz->resize_width = owidth;
4585 rsz->resize_height = oheight;
4586 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4587 resize_denom);
4588 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004589 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004590 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004591 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004592 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004593 if (resize_denom > rsz->superres_denom)
4594 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004595 else
Urvang Joshide71d142017-10-05 12:12:15 -07004596 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004597 rsz->resize_width = owidth;
4598 rsz->resize_height = oheight;
4599 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004600 resize_denom);
4601 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4602 (resize_denom > SCALE_NUMERATOR ||
4603 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08004604 } else { // We are allowed to alter neither resize scale nor superres
4605 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004606 return 0;
4607 }
Urvang Joshide71d142017-10-05 12:12:15 -07004608 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004609}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004610
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004611// Calculates resize and superres params for next frame
David Turner475a3132019-01-18 15:17:17 +00004612static size_params_type calculate_next_size_params(AV1_COMP *cpi) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004613 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08004614 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshi510d8f62019-01-10 12:11:50 -08004615 int resize_denom = SCALE_NUMERATOR;
Mufaddal Chakera3bcc72c2019-12-11 14:38:37 +05304616 if (has_no_stats_stage(cpi) && cpi->use_svc &&
Marco Paniconi63971322019-08-15 21:32:05 -07004617 cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1) {
4618 rsz.resize_width = cpi->common.width;
4619 rsz.resize_height = cpi->common.height;
4620 return rsz;
4621 }
Mufaddal Chakerae7326122019-12-04 14:49:09 +05304622 if (is_stat_generation_stage(cpi)) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004623 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4624 rsz.resize_width = cpi->resize_pending_width;
4625 rsz.resize_height = cpi->resize_pending_height;
4626 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4627 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004628 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004629 rsz.resize_width = cpi->oxcf.width;
4630 rsz.resize_height = cpi->oxcf.height;
4631 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004632 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004633 }
Urvang Joshide71d142017-10-05 12:12:15 -07004634 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004635 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
4636 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004637 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004638 return rsz;
4639}
4640
Urvang Joshi22b150b2019-01-10 14:32:32 -08004641static void setup_frame_size_from_params(AV1_COMP *cpi,
4642 const size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004643 int encode_width = rsz->resize_width;
4644 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004645
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004646 AV1_COMMON *cm = &cpi->common;
4647 cm->superres_upscaled_width = encode_width;
4648 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004649 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004650 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4651 rsz->superres_denom);
David Turner475a3132019-01-18 15:17:17 +00004652 av1_set_frame_size(cpi, encode_width, encode_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004653}
4654
David Turnerdedd8ff2019-01-23 13:59:46 +00004655void av1_setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004656 AV1_COMMON *cm = &cpi->common;
Urvang Joshi22b150b2019-01-10 14:32:32 -08004657 // Reset superres params from previous frame.
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004658 cm->superres_scale_denominator = SCALE_NUMERATOR;
David Turner475a3132019-01-18 15:17:17 +00004659 const size_params_type rsz = calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004660 setup_frame_size_from_params(cpi, &rsz);
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004661
Yaowu Xu7e450882019-04-30 15:09:18 -07004662 assert(av1_is_min_tile_width_satisfied(cm));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004663}
4664
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004665static void superres_post_encode(AV1_COMP *cpi) {
4666 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004667 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004668
Cheng Chen09c83a52018-06-05 12:27:36 -07004669 if (!av1_superres_scaled(cm)) return;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004670
Urvang Joshid6b5d512018-03-20 13:34:38 -07004671 assert(cpi->oxcf.enable_superres);
4672 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshic8b52d52018-03-23 13:16:51 -07004673 assert(!cm->all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07004674
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004675 av1_superres_upscale(cm, NULL);
4676
4677 // If regular resizing is occurring the source will need to be downscaled to
4678 // match the upscaled superres resolution. Otherwise the original source is
4679 // used.
Cheng Chen09c83a52018-06-05 12:27:36 -07004680 if (!av1_resize_scaled(cm)) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004681 cpi->source = cpi->unscaled_source;
4682 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4683 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004684 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4685 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08004686 // Do downscale. cm->(width|height) has been updated by
4687 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004688 if (aom_realloc_frame_buffer(
4689 &cpi->scaled_source, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004690 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4691 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
4692 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004693 aom_internal_error(
4694 &cm->error, AOM_CODEC_MEM_ERROR,
4695 "Failed to reallocate scaled source buffer for superres");
4696 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4697 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004698 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004699 (int)cm->seq_params.bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004700 cpi->source = &cpi->scaled_source;
4701 }
4702}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004703
Yue Chen92271e12019-07-09 16:28:02 -07004704static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
4705 MACROBLOCKD *xd, int use_restoration,
4706 int use_cdef) {
logangwf95c9162019-02-20 12:02:32 -08004707 if (use_restoration)
David Turnerc29e1a92018-12-06 14:10:14 +00004708 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004709
logangwf95c9162019-02-20 12:02:32 -08004710 if (use_cdef) {
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004711#if CONFIG_COLLECT_COMPONENT_TIMING
4712 start_timing(cpi, cdef_time);
4713#endif
Steinar Midtskogen59782122017-07-20 08:49:43 +02004714 // Find CDEF parameters
David Turnerc29e1a92018-12-06 14:10:14 +00004715 av1_cdef_search(&cm->cur_frame->buf, cpi->source, cm, xd,
chiyotsaid2f7b412019-12-18 10:16:42 -08004716 cpi->sf.lpf_sf.cdef_pick_method, cpi->td.mb.rdmult);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004717
4718 // Apply the filter
David Turnerc29e1a92018-12-06 14:10:14 +00004719 av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004720#if CONFIG_COLLECT_COMPONENT_TIMING
4721 end_timing(cpi, cdef_time);
4722#endif
logangwf95c9162019-02-20 12:02:32 -08004723 } else {
4724 cm->cdef_info.cdef_bits = 0;
4725 cm->cdef_info.cdef_strengths[0] = 0;
4726 cm->cdef_info.nb_cdef_strengths = 1;
4727 cm->cdef_info.cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004728 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004729
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004730 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004731
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004732#if CONFIG_COLLECT_COMPONENT_TIMING
4733 start_timing(cpi, loop_restoration_time);
4734#endif
logangwf95c9162019-02-20 12:02:32 -08004735 if (use_restoration) {
David Turnerc29e1a92018-12-06 14:10:14 +00004736 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004737 av1_pick_filter_restoration(cpi->source, cpi);
4738 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4739 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4740 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304741 if (cpi->num_workers > 1)
David Turnerc29e1a92018-12-06 14:10:14 +00004742 av1_loop_restoration_filter_frame_mt(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304743 cpi->workers, cpi->num_workers,
4744 &cpi->lr_row_sync, &cpi->lr_ctxt);
4745 else
David Turnerc29e1a92018-12-06 14:10:14 +00004746 av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304747 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004748 }
logangwf95c9162019-02-20 12:02:32 -08004749 } else {
4750 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4751 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4752 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004753 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004754#if CONFIG_COLLECT_COMPONENT_TIMING
4755 end_timing(cpi, loop_restoration_time);
4756#endif
Fergus Simpsonbc189932017-05-16 17:02:39 -07004757}
4758
Yue Chen92271e12019-07-09 16:28:02 -07004759static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
4760 const int num_planes = av1_num_planes(cm);
4761 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4762
4763 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
4764 cm->coded_lossless && cm->all_lossless));
4765
4766 const int use_loopfilter = !cm->coded_lossless && !cm->large_scale_tile;
4767 const int use_cdef = cm->seq_params.enable_cdef && !cm->coded_lossless &&
4768 !cm->large_scale_tile;
4769 const int use_restoration = cm->seq_params.enable_restoration &&
4770 !cm->all_lossless && !cm->large_scale_tile;
4771
4772 struct loopfilter *lf = &cm->lf;
4773
4774#if CONFIG_COLLECT_COMPONENT_TIMING
4775 start_timing(cpi, loop_filter_time);
4776#endif
4777 if (use_loopfilter) {
4778 aom_clear_system_state();
chiyotsaid2f7b412019-12-18 10:16:42 -08004779 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_sf.lpf_pick);
Yue Chen92271e12019-07-09 16:28:02 -07004780 } else {
4781 lf->filter_level[0] = 0;
4782 lf->filter_level[1] = 0;
4783 }
4784
4785 if (lf->filter_level[0] || lf->filter_level[1]) {
4786 if (cpi->num_workers > 1)
4787 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
4788#if CONFIG_LPF_MASK
4789 0,
4790#endif
4791 cpi->workers, cpi->num_workers,
4792 &cpi->lf_row_sync);
4793 else
4794 av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
4795#if CONFIG_LPF_MASK
4796 0,
4797#endif
4798 0, num_planes, 0);
4799 }
4800#if CONFIG_COLLECT_COMPONENT_TIMING
4801 end_timing(cpi, loop_filter_time);
4802#endif
4803
4804 cdef_restoration_frame(cpi, cm, xd, use_restoration, use_cdef);
4805}
4806
David Turnerf2b334c2018-12-13 13:00:55 +00004807static void fix_interp_filter(InterpFilter *const interp_filter,
4808 const FRAME_COUNTS *const counts) {
4809 if (*interp_filter == SWITCHABLE) {
4810 // Check to see if only one of the filters is actually used
4811 int count[SWITCHABLE_FILTERS] = { 0 };
4812 int num_filters_used = 0;
4813 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4814 for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
4815 count[i] += counts->switchable_interp[j][i];
4816 num_filters_used += (count[i] > 0);
4817 }
4818 if (num_filters_used == 1) {
4819 // Only one filter is used. So set the filter at frame level
4820 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4821 if (count[i]) {
4822 if (i == EIGHTTAP_REGULAR) *interp_filter = i;
4823 break;
4824 }
4825 }
4826 }
4827 }
4828}
4829
David Turner996b2c12018-12-07 15:52:30 +00004830static void finalize_encoded_frame(AV1_COMP *const cpi) {
4831 AV1_COMMON *const cm = &cpi->common;
David Turner99e990e2018-12-10 12:54:26 +00004832 CurrentFrame *const current_frame = &cm->current_frame;
David Turner996b2c12018-12-07 15:52:30 +00004833
David Turner99e990e2018-12-10 12:54:26 +00004834 if (!cm->seq_params.reduced_still_picture_hdr &&
4835 encode_show_existing_frame(cm)) {
4836 RefCntBuffer *const frame_to_show =
4837 cm->ref_frame_map[cpi->existing_fb_idx_to_show];
4838
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004839 if (frame_to_show == NULL) {
David Turner99e990e2018-12-10 12:54:26 +00004840 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4841 "Buffer does not contain a reconstructed frame");
4842 }
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004843 assert(frame_to_show->ref_count > 0);
David Turner99e990e2018-12-10 12:54:26 +00004844 assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
David Turner99e990e2018-12-10 12:54:26 +00004845 }
David Turner08f909c2018-12-18 13:29:14 +00004846
4847 if (!encode_show_existing_frame(cm) &&
4848 cm->seq_params.film_grain_params_present &&
4849 (cm->show_frame || cm->showable_frame)) {
4850 // Copy the current frame's film grain params to the its corresponding
4851 // RefCntBuffer slot.
4852 cm->cur_frame->film_grain_params = cm->film_grain_params;
4853
4854 // We must update the parameters if this is not an INTER_FRAME
4855 if (current_frame->frame_type != INTER_FRAME)
4856 cm->cur_frame->film_grain_params.update_parameters = 1;
4857
4858 // Iterate the random seed for the next frame.
4859 cm->film_grain_params.random_seed += 3381;
4860 if (cm->film_grain_params.random_seed == 0)
4861 cm->film_grain_params.random_seed = 7391;
4862 }
David Turnerf2b334c2018-12-13 13:00:55 +00004863
4864 // Initialise all tiles' contexts from the global frame context
4865 for (int tile_col = 0; tile_col < cm->tile_cols; tile_col++) {
4866 for (int tile_row = 0; tile_row < cm->tile_rows; tile_row++) {
4867 const int tile_idx = tile_row * cm->tile_cols + tile_col;
4868 cpi->tile_data[tile_idx].tctx = *cm->fc;
4869 }
4870 }
4871
4872 fix_interp_filter(&cm->interp_filter, cpi->td.counts);
David Turner996b2c12018-12-07 15:52:30 +00004873}
4874
Urvang Joshi57643372019-02-21 11:10:57 -08004875static int get_regulated_q_overshoot(AV1_COMP *const cpi, int q_low, int q_high,
4876 int top_index, int bottom_index) {
4877 const AV1_COMMON *const cm = &cpi->common;
4878 const RATE_CONTROL *const rc = &cpi->rc;
4879
4880 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4881
4882 int q_regulated =
4883 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4884 AOMMAX(q_high, top_index), cm->width, cm->height);
4885
4886 int retries = 0;
4887 while (q_regulated < q_low && retries < 10) {
4888 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4889 q_regulated =
4890 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4891 AOMMAX(q_high, top_index), cm->width, cm->height);
4892 retries++;
4893 }
4894 return q_regulated;
4895}
4896
4897static int get_regulated_q_undershoot(AV1_COMP *const cpi, int q_high,
4898 int top_index, int bottom_index) {
4899 const AV1_COMMON *const cm = &cpi->common;
4900 const RATE_CONTROL *const rc = &cpi->rc;
4901
4902 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4903 int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4904 top_index, cm->width, cm->height);
4905
4906 int retries = 0;
4907 while (q_regulated > q_high && retries < 10) {
4908 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4909 q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4910 top_index, cm->width, cm->height);
4911 retries++;
4912 }
4913 return q_regulated;
4914}
4915
David Turner2f3b5df2019-01-02 14:30:50 +00004916// Called after encode_with_recode_loop() has just encoded a frame and packed
4917// its bitstream. This function works out whether we under- or over-shot
4918// our bitrate target and adjusts q as appropriate. Also decides whether
4919// or not we should do another recode loop, indicated by *loop
Hui Suef139e12019-05-20 15:51:22 -07004920static void recode_loop_update_q(
4921 AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low,
4922 int *const q_high, const int top_index, const int bottom_index,
4923 int *const undershoot_seen, int *const overshoot_seen,
4924 int *const low_cr_seen, const int loop_at_this_size) {
David Turner2f3b5df2019-01-02 14:30:50 +00004925 AV1_COMMON *const cm = &cpi->common;
4926 RATE_CONTROL *const rc = &cpi->rc;
4927
Hui Suef139e12019-05-20 15:51:22 -07004928 const int min_cr = cpi->oxcf.min_cr;
4929 if (min_cr > 0) {
4930 aom_clear_system_state();
4931 const double compression_ratio =
4932 av1_get_compression_ratio(cm, rc->projected_frame_size >> 3);
4933 const double target_cr = min_cr / 100.0;
4934 if (compression_ratio < target_cr) {
4935 *low_cr_seen = 1;
4936 if (*q < rc->worst_quality) {
4937 const double cr_ratio = target_cr / compression_ratio;
4938 const int projected_q = AOMMAX(*q + 1, (int)(*q * cr_ratio * cr_ratio));
4939 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->worst_quality);
4940 *q_low = AOMMAX(*q, *q_low);
4941 *q_high = AOMMAX(*q, *q_high);
4942 *loop = 1;
4943 }
4944 }
4945 if (*low_cr_seen) return;
4946 }
4947
4948 if (cpi->oxcf.rc_mode == AOM_Q) return;
4949
Hui Su01dfe032019-09-11 14:10:42 -07004950 const int last_q = *q;
David Turner2f3b5df2019-01-02 14:30:50 +00004951 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
4952 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4953 &frame_under_shoot_limit,
4954 &frame_over_shoot_limit);
4955 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4956
Hui Su01dfe032019-09-11 14:10:42 -07004957 if (cm->current_frame.frame_type == KEY_FRAME && rc->this_key_frame_forced &&
4958 rc->projected_frame_size < rc->max_frame_bandwidth) {
David Turner2f3b5df2019-01-02 14:30:50 +00004959 int64_t kf_err;
Hui Su01dfe032019-09-11 14:10:42 -07004960 const int64_t high_err_target = cpi->ambient_err;
4961 const int64_t low_err_target = cpi->ambient_err >> 1;
David Turner2f3b5df2019-01-02 14:30:50 +00004962
Jerome Jiangfa1d1732019-08-06 10:31:20 -07004963#if CONFIG_AV1_HIGHBITDEPTH
David Turner2f3b5df2019-01-02 14:30:50 +00004964 if (cm->seq_params.use_highbitdepth) {
4965 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
4966 } else {
4967 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
4968 }
Jerome Jiangfa1d1732019-08-06 10:31:20 -07004969#else
4970 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
4971#endif
David Turner2f3b5df2019-01-02 14:30:50 +00004972 // Prevent possible divide by zero error below for perfect KF
4973 kf_err += !kf_err;
4974
4975 // The key frame is not good enough or we can afford
4976 // to make it better without undue risk of popping.
4977 if ((kf_err > high_err_target &&
4978 rc->projected_frame_size <= frame_over_shoot_limit) ||
4979 (kf_err > low_err_target &&
4980 rc->projected_frame_size <= frame_under_shoot_limit)) {
4981 // Lower q_high
Hui Su01dfe032019-09-11 14:10:42 -07004982 *q_high = AOMMAX(*q - 1, *q_low);
David Turner2f3b5df2019-01-02 14:30:50 +00004983
4984 // Adjust Q
4985 *q = (int)((*q * high_err_target) / kf_err);
4986 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
4987 } else if (kf_err < low_err_target &&
4988 rc->projected_frame_size >= frame_under_shoot_limit) {
4989 // The key frame is much better than the previous frame
4990 // Raise q_low
Hui Su01dfe032019-09-11 14:10:42 -07004991 *q_low = AOMMIN(*q + 1, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00004992
4993 // Adjust Q
4994 *q = (int)((*q * low_err_target) / kf_err);
4995 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
4996 }
4997
4998 // Clamp Q to upper and lower limits:
4999 *q = clamp(*q, *q_low, *q_high);
Hui Su01dfe032019-09-11 14:10:42 -07005000 *loop = (*q != last_q);
5001 return;
5002 }
David Turner2f3b5df2019-01-02 14:30:50 +00005003
Hui Su01dfe032019-09-11 14:10:42 -07005004 if (recode_loop_test(cpi, frame_over_shoot_limit, frame_under_shoot_limit, *q,
5005 AOMMAX(*q_high, top_index), bottom_index)) {
David Turner2f3b5df2019-01-02 14:30:50 +00005006 // Is the projected frame size out of range and are we allowed
5007 // to attempt to recode.
David Turner2f3b5df2019-01-02 14:30:50 +00005008
5009 // Frame size out of permitted range:
5010 // Update correction factor & compute new Q to try...
5011 // Frame is too large
5012 if (rc->projected_frame_size > rc->this_frame_target) {
5013 // Special case if the projected size is > the max allowed.
Urvang Joshi7344d3e2019-07-25 17:24:57 -07005014 if (*q == *q_high &&
5015 rc->projected_frame_size >= rc->max_frame_bandwidth) {
5016 const double q_val_high_current =
5017 av1_convert_qindex_to_q(*q_high, cm->seq_params.bit_depth);
5018 const double q_val_high_new =
5019 q_val_high_current *
5020 ((double)rc->projected_frame_size / rc->max_frame_bandwidth);
5021 *q_high = av1_find_qindex(q_val_high_new, cm->seq_params.bit_depth,
5022 rc->best_quality, rc->worst_quality);
5023 }
David Turner2f3b5df2019-01-02 14:30:50 +00005024
5025 // Raise Qlow as to at least the current value
Hui Su01dfe032019-09-11 14:10:42 -07005026 *q_low = AOMMIN(*q + 1, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00005027
Urvang Joshi57643372019-02-21 11:10:57 -08005028 if (*undershoot_seen || loop_at_this_size > 2 ||
5029 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00005030 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
5031
5032 *q = (*q_high + *q_low + 1) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08005033 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
5034 const int q_mid = (*q_high + *q_low + 1) / 2;
5035 const int q_regulated = get_regulated_q_overshoot(
5036 cpi, *q_low, *q_high, top_index, bottom_index);
5037 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
5038 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
5039 *q = (q_mid + q_regulated + 1) / 2;
David Turner2f3b5df2019-01-02 14:30:50 +00005040 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08005041 *q = get_regulated_q_overshoot(cpi, *q_low, *q_high, top_index,
5042 bottom_index);
David Turner2f3b5df2019-01-02 14:30:50 +00005043 }
5044
5045 *overshoot_seen = 1;
5046 } else {
5047 // Frame is too small
Hui Su01dfe032019-09-11 14:10:42 -07005048 *q_high = AOMMAX(*q - 1, *q_low);
David Turner2f3b5df2019-01-02 14:30:50 +00005049
Urvang Joshi57643372019-02-21 11:10:57 -08005050 if (*overshoot_seen || loop_at_this_size > 2 ||
5051 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00005052 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
5053 *q = (*q_high + *q_low) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08005054 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
5055 const int q_mid = (*q_high + *q_low) / 2;
5056 const int q_regulated =
5057 get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
5058 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
5059 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
5060 *q = (q_mid + q_regulated) / 2;
5061
5062 // Special case reset for qlow for constrained quality.
5063 // This should only trigger where there is very substantial
5064 // undershoot on a frame and the auto cq level is above
5065 // the user passsed in value.
5066 if (cpi->oxcf.rc_mode == AOM_CQ && q_regulated < *q_low) {
5067 *q_low = *q;
5068 }
David Turner2f3b5df2019-01-02 14:30:50 +00005069 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08005070 *q = get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
5071
David Turner2f3b5df2019-01-02 14:30:50 +00005072 // Special case reset for qlow for constrained quality.
5073 // This should only trigger where there is very substantial
5074 // undershoot on a frame and the auto cq level is above
5075 // the user passsed in value.
5076 if (cpi->oxcf.rc_mode == AOM_CQ && *q < *q_low) {
5077 *q_low = *q;
5078 }
David Turner2f3b5df2019-01-02 14:30:50 +00005079 }
5080
5081 *undershoot_seen = 1;
5082 }
5083
5084 // Clamp Q to upper and lower limits:
5085 *q = clamp(*q, *q_low, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00005086 }
Hui Su01dfe032019-09-11 14:10:42 -07005087
5088 *loop = (*q != last_q);
David Turner2f3b5df2019-01-02 14:30:50 +00005089}
5090
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305091static int get_interp_filter_selected(const AV1_COMMON *const cm,
5092 MV_REFERENCE_FRAME ref,
5093 InterpFilter ifilter) {
5094 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
5095 if (buf == NULL) return 0;
5096 return buf->interp_filter_selected[ifilter];
5097}
5098
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305099static uint16_t setup_interp_filter_search_mask(AV1_COMP *cpi) {
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305100 const AV1_COMMON *const cm = &cpi->common;
5101 int ref_total[REF_FRAMES] = { 0 };
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305102 uint16_t mask = ALLOW_ALL_INTERP_FILT_MASK;
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305103
5104 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305105 return mask;
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305106
5107 for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
5108 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
5109 ++ifilter) {
5110 ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter);
5111 }
5112 }
5113 int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] +
5114 ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] +
5115 ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]);
5116
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305117 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
5118 ++ifilter) {
5119 int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30;
5120 if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) {
5121 int filter_score =
5122 get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 +
5123 get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 +
5124 get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 +
5125 get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 +
5126 get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 +
5127 get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10;
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305128 if (filter_score < ref_total_total) {
5129 DUAL_FILTER_TYPE filt_type = ifilter + SWITCHABLE_FILTERS * ifilter;
5130 reset_interp_filter_allowed_mask(&mask, filt_type);
5131 }
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305132 }
5133 }
5134 return mask;
5135}
5136
Cheng Chen92a926a2020-01-30 17:57:33 -08005137#if !CONFIG_REALTIME_ONLY
5138#define STRICT_PSNR_DIFF_THRESH 1.0
5139// Encode key frame with/without screen content tools to determine whether
5140// screen content tools should be enabled for this key frame group or not.
5141// The first encoding is without screen content tools.
5142// The second encoding is with screen content tools.
5143// We compare the psnr and frame size to make the decision.
5144static void screen_content_tools_determination(
5145 AV1_COMP *cpi, const int allow_screen_content_tools_orig_decision,
5146 const int allow_intrabc_orig_decision,
5147 const int is_screen_content_type_orig_decision, const int pass,
5148 int *projected_size_pass, PSNR_STATS *psnr) {
5149 AV1_COMMON *const cm = &cpi->common;
5150 projected_size_pass[pass] = cpi->rc.projected_frame_size;
5151#if CONFIG_AV1_HIGHBITDEPTH
5152 const uint32_t in_bit_depth = cpi->oxcf.input_bit_depth;
5153 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
5154 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr[pass],
5155 bit_depth, in_bit_depth);
5156#else
5157 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr[pass]);
5158#endif
5159 if (pass != 1) return;
5160
5161 const double psnr_diff = psnr[1].psnr[0] - psnr[0].psnr[0];
5162 const int is_sc_encoding_much_better = psnr_diff > STRICT_PSNR_DIFF_THRESH;
5163 if (is_sc_encoding_much_better) {
5164 // Use screen content tools, if we get coding gain.
5165 cm->allow_screen_content_tools = 1;
5166 cm->allow_intrabc = cpi->intrabc_used;
5167 cpi->is_screen_content_type = 1;
5168 } else {
5169 // Use original screen content decision.
5170 cm->allow_screen_content_tools = allow_screen_content_tools_orig_decision;
5171 cm->allow_intrabc = allow_intrabc_orig_decision;
5172 cpi->is_screen_content_type = is_screen_content_type_orig_decision;
5173 }
5174}
5175
5176// Set or save some encoding parameters. In the two encoding pass,
5177// we want to make the encoding process fast. A fixed block partition size,
5178// and a large q is used.
5179static void set_or_save_encoding_params_for_screen_content(AV1_COMP *cpi,
5180 const int pass) {
5181 AV1_COMMON *const cm = &cpi->common;
5182 if (pass == 0) {
5183 // In the first pass, encode without screen content tools.
5184 // Use a high q, and a fixed block size for fast encoding.
5185 cm->allow_screen_content_tools = 0;
5186 cm->allow_intrabc = 0;
5187 cpi->is_screen_content_type = 0;
5188 cpi->sf.part_sf.partition_search_type = FIXED_PARTITION;
5189 cpi->sf.part_sf.always_this_block_size = BLOCK_32X32;
5190 } else {
5191 // In the second pass, encode with screen content tools.
5192 // Use a high q, and a fixed block size for fast encoding.
5193 cm->allow_screen_content_tools = 1;
5194 // TODO(chengchen): turn intrabc on could lead to data race issue.
5195 // cm->allow_intrabc = 1;
5196 cpi->is_screen_content_type = 1;
5197 cpi->sf.part_sf.partition_search_type = FIXED_PARTITION;
5198 cpi->sf.part_sf.always_this_block_size = BLOCK_32X32;
5199 av1_hash_table_create(&cm->cur_frame->hash_table);
5200 }
5201}
5202
5203// Determines whether to use screen content tools for the key frame group.
5204// This function modifies "cm->allow_screen_content_tools",
5205// "cm->allow_intrabc" and "cpi->is_screen_content_type".
5206static void determine_sc_tools_with_encoding(AV1_COMP *cpi, const int q_orig) {
5207 if (!is_stat_consumption_stage_twopass(cpi)) return;
5208
5209 AV1_COMMON *const cm = &cpi->common;
5210 // Variables to help determine if we should allow screen content tools.
5211 int projected_size_pass[3] = { 0 };
5212 PSNR_STATS psnr[3];
5213 const int is_key_frame = cm->current_frame.frame_type == KEY_FRAME;
5214 const int allow_screen_content_tools_orig_decision =
5215 cm->allow_screen_content_tools;
5216 const int allow_intrabc_orig_decision = cm->allow_intrabc;
5217 const int is_screen_content_type_orig_decision = cpi->is_screen_content_type;
5218 // Turn off the encoding trial for forward key frame and superres.
5219 if (cpi->sf.rt_sf.use_nonrd_pick_mode || cpi->oxcf.fwd_kf_enabled ||
5220 cpi->oxcf.superres_mode != SUPERRES_NONE || cpi->oxcf.mode == REALTIME ||
5221 is_screen_content_type_orig_decision || !is_key_frame) {
5222 return;
5223 }
5224
5225 // TODO(chengchen): multiple encoding for the lossless mode is time consuming.
5226 // Find a better way to determine whether screen content tools should be used
5227 // for lossless coding.
5228 // Use a high q and a fixed partition to do quick encoding.
5229 const int q_for_screen_content_quick_run =
5230 is_lossless_requested(&cpi->oxcf) ? q_orig : AOMMAX(q_orig, 244);
5231 const int partition_search_type_orig = cpi->sf.part_sf.partition_search_type;
5232 const BLOCK_SIZE fixed_partition_block_size_orig =
5233 cpi->sf.part_sf.always_this_block_size;
5234
5235 // Setup necessary params for encoding, including frame source, etc.
5236 {
5237 aom_clear_system_state();
5238
5239 cpi->source =
5240 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
5241 if (cpi->unscaled_last_source != NULL) {
5242 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
5243 &cpi->scaled_last_source);
5244 }
5245
5246 setup_frame(cpi);
5247
5248 if (cm->seg.enabled) {
5249 if (!cm->seg.update_data && cm->prev_frame) {
5250 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
5251 cm->seg.enabled = cm->prev_frame->seg.enabled;
5252 } else {
5253 av1_calculate_segdata(&cm->seg);
5254 }
5255 } else {
5256 memset(&cm->seg, 0, sizeof(cm->seg));
5257 }
5258 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
5259 cm->cur_frame->seg.enabled = cm->seg.enabled;
5260 }
5261
5262 // The two encodes is to help determine whether to use screen content tools,
5263 // with a high q and fixed partition.
5264 // Then reset the partition speed feature.
5265 for (int pass = 0; pass < 2; ++pass) {
5266 set_or_save_encoding_params_for_screen_content(cpi, pass);
5267#if CONFIG_TUNE_VMAF
5268 if (cpi->oxcf.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING ||
5269 cpi->oxcf.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
5270 cpi->oxcf.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
5271 av1_set_quantizer(
5272 cm, av1_get_vmaf_base_qindex(cpi, q_for_screen_content_quick_run));
5273 } else {
5274#endif
5275 av1_set_quantizer(cm, q_for_screen_content_quick_run);
5276#if CONFIG_TUNE_VMAF
5277 }
5278#endif
5279 if (cpi->oxcf.deltaq_mode != NO_DELTA_Q) av1_init_quantizer(cpi);
5280
5281 av1_set_variance_partition_thresholds(cpi, q_for_screen_content_quick_run,
5282 0);
5283 // transform / motion compensation build reconstruction frame
5284 av1_encode_frame(cpi);
5285 // Screen content decision
5286 screen_content_tools_determination(
5287 cpi, allow_screen_content_tools_orig_decision,
5288 allow_intrabc_orig_decision, is_screen_content_type_orig_decision, pass,
5289 projected_size_pass, psnr);
5290 }
5291
5292 // Set partition speed feature back.
5293 cpi->sf.part_sf.partition_search_type = partition_search_type_orig;
5294 cpi->sf.part_sf.always_this_block_size = fixed_partition_block_size_orig;
5295}
5296#endif // CONFIG_REALTIME_ONLY
5297
Tom Finegane4099e32018-01-23 12:01:51 -08005298static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005299 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005300 RATE_CONTROL *const rc = &cpi->rc;
chiyotsai6b430132019-12-18 10:33:51 -08005301 const int allow_recode = (cpi->sf.hl_sf.recode_loop != DISALLOW_RECODE);
Hui Sua1d71842019-07-31 12:02:24 -07005302 // Must allow recode if minimum compression ratio is set.
5303 assert(IMPLIES(cpi->oxcf.min_cr > 0, allow_recode));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005304
5305 set_size_independent_vars(cpi);
Hamsalekha S37cc1d12019-12-12 19:27:41 +05305306 if (is_stat_consumption_stage_twopass(cpi) &&
chiyotsaicbf1d112019-12-16 11:28:47 -08005307 cpi->sf.interp_sf.adaptive_interp_filter_search)
Yunqing Wangc3ab9272019-11-01 17:43:58 -07005308 cpi->interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07005309 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07005310
David Turnerdedd8ff2019-01-23 13:59:46 +00005311 av1_setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005312
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005313#if CONFIG_SUPERRES_IN_RECODE
5314 if (superres_in_recode_allowed(cpi) &&
5315 cm->superres_scale_denominator == SCALE_NUMERATOR) {
5316 // Superres won't be picked, so no need to try, as we will go through
5317 // another recode loop for full-resolution after this anyway.
5318 return -1;
5319 }
5320#endif // CONFIG_SUPERRES_IN_RECODE
5321
David Turner2f3b5df2019-01-02 14:30:50 +00005322 int top_index = 0, bottom_index = 0;
5323 int q = 0, q_low = 0, q_high = 0;
5324 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
5325 q_low = bottom_index;
5326 q_high = top_index;
5327
chiyotsaidcc2e012019-12-17 10:29:32 -08005328 if (cpi->sf.tx_sf.tx_type_search.prune_tx_type_using_stats &&
Yunqing Wang80a7af62019-08-22 16:07:44 -07005329 cm->current_frame.frame_type == KEY_FRAME) {
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005330 av1_copy(cpi->tx_type_probs, default_tx_type_probs);
5331
Yunqing Wang2348c032020-01-09 18:05:49 -08005332 const int thr[2][2] = { { 15, 10 }, { 17, 10 } };
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005333 for (int f = 0; f < FRAME_UPDATE_TYPES; f++) {
Yunqing Wangeb7c2182019-08-22 18:15:48 -07005334 int kf_arf_update = (f == KF_UPDATE || f == ARF_UPDATE);
5335 cpi->tx_type_probs_thresh[f] =
chiyotsaidcc2e012019-12-17 10:29:32 -08005336 thr[cpi->sf.tx_sf.tx_type_search.prune_tx_type_using_stats - 1]
Yunqing Wangeb7c2182019-08-22 18:15:48 -07005337 [kf_arf_update];
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005338 }
5339 }
5340
chiyotsai868dc902019-12-12 15:32:52 -08005341 if (!cpi->sf.inter_sf.disable_obmc &&
5342 cpi->sf.inter_sf.prune_obmc_prob_thresh > 0 &&
Yunqing Wang5f74dc22019-10-29 10:35:20 -07005343 cm->current_frame.frame_type == KEY_FRAME) {
5344 av1_copy(cpi->obmc_probs, default_obmc_probs);
Yunqing Wang5f74dc22019-10-29 10:35:20 -07005345 }
chiyotsai868dc902019-12-12 15:32:52 -08005346 if (cpi->sf.inter_sf.prune_warped_prob_thresh > 0 &&
Yunqing Wanga09a46b2019-12-09 13:16:24 -08005347 cm->current_frame.frame_type == KEY_FRAME) {
5348 av1_copy(cpi->warped_probs, default_warped_probs);
5349 }
Yunqing Wang5f74dc22019-10-29 10:35:20 -07005350
Yunqing Wang2348c032020-01-09 18:05:49 -08005351 if (cpi->sf.interp_sf.adaptive_interp_filter_search == 2 &&
5352 cm->current_frame.frame_type == KEY_FRAME) {
5353 av1_copy(cpi->switchable_interp_probs, default_switchable_interp_probs);
5354
5355 const int thr[7] = { 0, 8, 8, 8, 8, 0, 8 };
5356 for (int f = 0; f < FRAME_UPDATE_TYPES; f++) {
5357 cpi->switchable_interp_thresh[f] = thr[f];
5358 }
5359 }
5360
Cheng Chen92a926a2020-01-30 17:57:33 -08005361#if !CONFIG_REALTIME_ONLY
5362 // Determine whether to use screen content tools using two fast encoding.
5363 determine_sc_tools_with_encoding(cpi, q);
5364#endif // CONFIG_REALTIME_ONLY
5365
5366#if CONFIG_COLLECT_COMPONENT_TIMING
5367 printf("\n Encoding a frame:");
5368#endif
5369
David Turner2f3b5df2019-01-02 14:30:50 +00005370 // Loop variables
Cheng Chen92a926a2020-01-30 17:57:33 -08005371 int loop = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00005372 int loop_count = 0;
5373 int loop_at_this_size = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00005374 int overshoot_seen = 0;
5375 int undershoot_seen = 0;
Hui Suef139e12019-05-20 15:51:22 -07005376 int low_cr_seen = 0;
chiyotsai8b7cef82020-01-21 16:52:54 -08005377 int last_loop_allow_hp = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005378
Yaowu Xuc27fc142016-08-22 16:08:15 -07005379 do {
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005380 loop = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005381 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005382
Urvang Joshif1fa6862018-01-08 16:39:33 -08005383 // if frame was scaled calculate global_motion_search again if already
5384 // done
David Turner2f3b5df2019-01-02 14:30:50 +00005385 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005386 if (cpi->source->y_crop_width != cm->width ||
David Turner2f3b5df2019-01-02 14:30:50 +00005387 cpi->source->y_crop_height != cm->height) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005388 cpi->global_motion_search_done = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00005389 }
5390 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005391 cpi->source =
5392 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
David Turner2f3b5df2019-01-02 14:30:50 +00005393 if (cpi->unscaled_last_source != NULL) {
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07005394 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
5395 &cpi->scaled_last_source);
David Turner2f3b5df2019-01-02 14:30:50 +00005396 }
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07005397
David Turner2f3b5df2019-01-02 14:30:50 +00005398 if (!frame_is_intra_only(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005399 if (loop_count > 0) {
5400 release_scaled_references(cpi);
5401 }
Cheng Chen46f30c72017-09-07 11:13:33 -07005402 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005403 }
sdeng3cd9eec2020-01-23 15:49:50 -08005404#if CONFIG_TUNE_VMAF
5405 if (cpi->oxcf.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING ||
sdeng615dc242020-02-04 16:06:14 -08005406 cpi->oxcf.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
5407 cpi->oxcf.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
sdeng3cd9eec2020-01-23 15:49:50 -08005408 av1_set_quantizer(cm, av1_get_vmaf_base_qindex(cpi, q));
5409 } else {
5410#endif
5411 av1_set_quantizer(cm, q);
5412#if CONFIG_TUNE_VMAF
5413 }
5414#endif
5415
Fyodor Kyslov25daf5d2019-10-23 10:33:26 -07005416 if (cpi->oxcf.deltaq_mode != NO_DELTA_Q) av1_init_quantizer(cpi);
kyslov7b9d0d62018-12-21 11:12:26 -08005417
5418 av1_set_variance_partition_thresholds(cpi, q, 0);
5419
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08005420 // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
5421 // cm->current_frame.frame_number, cm->show_frame, q,
5422 // cm->current_frame.frame_type, cm->superres_scale_denominator);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005423
David Turner2f3b5df2019-01-02 14:30:50 +00005424 if (loop_count == 0) {
5425 setup_frame(cpi);
5426 } else if (get_primary_ref_frame_buf(cm) == NULL) {
5427 // Base q-index may have changed, so we need to assign proper default coef
5428 // probs before every iteration.
Yaowu Xuf883b422016-08-30 14:01:10 -07005429 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08005430 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00005431 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005432
Yaowu Xuc27fc142016-08-22 16:08:15 -07005433 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005434 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005435 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005436 av1_setup_in_frame_q_adj(cpi);
David Turner2f3b5df2019-01-02 14:30:50 +00005437 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !allow_recode) {
5438 suppress_active_map(cpi);
5439 av1_cyclic_refresh_setup(cpi);
5440 apply_active_map(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005441 }
David Turner2f3b5df2019-01-02 14:30:50 +00005442
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005443 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00005444 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005445 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
Jerome Jiangb23571d2019-10-23 14:15:55 -07005446 cm->seg.enabled = cm->prev_frame->seg.enabled;
David Barker11c93562018-06-05 12:00:07 +01005447 } else {
Yaowu Xu7e450882019-04-30 15:09:18 -07005448 av1_calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07005449 }
David Barkercab37552018-03-21 11:56:24 +00005450 } else {
5451 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005452 }
David Barkercab37552018-03-21 11:56:24 +00005453 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Jerome Jiangb23571d2019-10-23 14:15:55 -07005454 cm->cur_frame->seg.enabled = cm->seg.enabled;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005455
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005456#if CONFIG_COLLECT_COMPONENT_TIMING
5457 start_timing(cpi, av1_encode_frame_time);
5458#endif
chiyotsai8b7cef82020-01-21 16:52:54 -08005459 // Set the motion vector precision based on mv stats from the last coded
5460 // frame.
5461 if (!frame_is_intra_only(cm)) {
5462 av1_pick_and_set_high_precision_mv(cpi, q);
5463
5464 // If the precision has changed during different iteration of the loop,
5465 // then we need to reset the global motion vectors
5466 if (loop_count > 0 && cm->allow_high_precision_mv != last_loop_allow_hp) {
5467 cpi->global_motion_search_done = 0;
5468 }
5469 last_loop_allow_hp = cm->allow_high_precision_mv;
5470 }
chiyotsaic666b1f2019-12-20 10:44:58 -08005471
Yaowu Xuc27fc142016-08-22 16:08:15 -07005472 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07005473 av1_encode_frame(cpi);
chiyotsai51bd6482019-12-20 10:49:34 -08005474#if !CONFIG_REALTIME_ONLY
5475 // Reset the mv_stats in case we are interrupted by an intraframe or an
5476 // overlay frame.
5477 if (cpi->mv_stats.valid) {
5478 av1_zero(cpi->mv_stats);
5479 }
5480 // Gather the mv_stats for the next frame
5481 if (cpi->sf.hl_sf.high_precision_mv_usage == LAST_MV_DATA &&
5482 av1_frame_allows_smart_mv(cpi)) {
5483 av1_collect_mv_stats(cpi, q);
5484 }
5485#endif // !CONFIG_REALTIME_ONLY
5486
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005487#if CONFIG_COLLECT_COMPONENT_TIMING
5488 end_timing(cpi, av1_encode_frame_time);
5489#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005490
Yaowu Xuf883b422016-08-30 14:01:10 -07005491 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005492
5493 // Dummy pack of the bitstream using up to date stats to get an
5494 // accurate estimate of output frame size to determine if we need
5495 // to recode.
chiyotsai6b430132019-12-18 10:33:51 -08005496 const int do_dummy_pack =
5497 (cpi->sf.hl_sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
5498 cpi->oxcf.rc_mode != AOM_Q) ||
5499 cpi->oxcf.min_cr > 0;
Hui Sua1d71842019-07-31 12:02:24 -07005500 if (do_dummy_pack) {
David Turner996b2c12018-12-07 15:52:30 +00005501 finalize_encoded_frame(cpi);
David Turner35cba132018-12-10 15:48:15 +00005502 int largest_tile_id = 0; // Output from bitstream: unused here
chiyotsaic666b1f2019-12-20 10:44:58 -08005503 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) !=
5504 AOM_CODEC_OK) {
Tom Finegane4099e32018-01-23 12:01:51 -08005505 return AOM_CODEC_ERROR;
chiyotsaic666b1f2019-12-20 10:44:58 -08005506 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005507
5508 rc->projected_frame_size = (int)(*size) << 3;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005509 }
5510
Hui Suef139e12019-05-20 15:51:22 -07005511 if (allow_recode) {
David Turner2f3b5df2019-01-02 14:30:50 +00005512 // Update q and decide whether to do a recode loop
5513 recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
5514 bottom_index, &undershoot_seen, &overshoot_seen,
Hui Suef139e12019-05-20 15:51:22 -07005515 &low_cr_seen, loop_at_this_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005516 }
5517
5518 // Special case for overlay frame.
Hui Su7e3eeaa2019-09-11 15:50:41 -07005519 if (loop && rc->is_src_frame_alt_ref &&
5520 rc->projected_frame_size < rc->max_frame_bandwidth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005521 loop = 0;
Hui Su7e3eeaa2019-09-11 15:50:41 -07005522 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005523
chiyotsai76cd2652019-12-18 10:26:36 -08005524 if (allow_recode && !cpi->sf.gm_sf.gm_disable_recode &&
David Turner2f3b5df2019-01-02 14:30:50 +00005525 recode_loop_test_global_motion(cpi)) {
5526 loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005527 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005528
Yaowu Xuc27fc142016-08-22 16:08:15 -07005529 if (loop) {
5530 ++loop_count;
5531 ++loop_at_this_size;
5532
5533#if CONFIG_INTERNAL_STATS
5534 ++cpi->tot_recode_hits;
5535#endif
5536 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005537#if CONFIG_COLLECT_COMPONENT_TIMING
5538 if (loop) printf("\n Recoding:");
5539#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005540 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08005541
Marco Paniconi574e59e2019-10-23 15:47:28 -07005542 // Update some stats from cyclic refresh.
Marco Paniconi51415512019-11-27 12:38:24 -08005543 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !frame_is_intra_only(cm))
Marco Paniconi574e59e2019-10-23 15:47:28 -07005544 av1_cyclic_refresh_postencode(cpi);
5545
Tom Finegane4099e32018-01-23 12:01:51 -08005546 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005547}
5548
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005549static int encode_with_recode_loop_and_filter(AV1_COMP *cpi, size_t *size,
5550 uint8_t *dest, int64_t *sse,
5551 int64_t *rate,
5552 int *largest_tile_id) {
5553#if CONFIG_COLLECT_COMPONENT_TIMING
5554 start_timing(cpi, encode_with_recode_loop_time);
5555#endif
5556 int err = encode_with_recode_loop(cpi, size, dest);
5557#if CONFIG_COLLECT_COMPONENT_TIMING
5558 end_timing(cpi, encode_with_recode_loop_time);
5559#endif
5560 if (err != AOM_CODEC_OK) {
5561 if (err == -1) {
5562 // special case as described in encode_with_recode_loop().
5563 // Encoding was skipped.
5564 err = AOM_CODEC_OK;
5565 if (sse != NULL) *sse = INT64_MAX;
5566 if (rate != NULL) *rate = INT64_MAX;
5567 *largest_tile_id = 0;
5568 }
5569 return err;
5570 }
5571
5572#ifdef OUTPUT_YUV_SKINMAP
5573 if (cpi->common.current_frame.frame_number > 1) {
5574 av1_compute_skin_map(cpi, yuv_skinmap_file);
5575 }
5576#endif // OUTPUT_YUV_SKINMAP
5577
5578 AV1_COMMON *const cm = &cpi->common;
5579 SequenceHeader *const seq_params = &cm->seq_params;
5580
5581 // Special case code to reduce pulsing when key frames are forced at a
5582 // fixed interval. Note the reconstruction error if it is the frame before
5583 // the force key frame
5584 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005585#if CONFIG_AV1_HIGHBITDEPTH
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005586 if (seq_params->use_highbitdepth) {
5587 cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
5588 } else {
5589 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5590 }
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005591#else
5592 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5593#endif
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005594 }
5595
5596 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
5597 cm->cur_frame->buf.transfer_characteristics =
5598 seq_params->transfer_characteristics;
5599 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
5600 cm->cur_frame->buf.monochrome = seq_params->monochrome;
5601 cm->cur_frame->buf.chroma_sample_position =
5602 seq_params->chroma_sample_position;
5603 cm->cur_frame->buf.color_range = seq_params->color_range;
5604 cm->cur_frame->buf.render_width = cm->render_width;
5605 cm->cur_frame->buf.render_height = cm->render_height;
5606
5607 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5608 // off.
5609
5610 // Pick the loop filter level for the frame.
5611 if (!cm->allow_intrabc) {
5612 loopfilter_frame(cpi, cm);
5613 } else {
5614 cm->lf.filter_level[0] = 0;
5615 cm->lf.filter_level[1] = 0;
5616 cm->cdef_info.cdef_bits = 0;
5617 cm->cdef_info.cdef_strengths[0] = 0;
5618 cm->cdef_info.nb_cdef_strengths = 1;
5619 cm->cdef_info.cdef_uv_strengths[0] = 0;
5620 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5621 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5622 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
5623 }
5624
5625 // TODO(debargha): Fix mv search range on encoder side
5626 // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5627 aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5628
5629#ifdef OUTPUT_YUV_REC
5630 aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
5631#endif
5632
5633 finalize_encoded_frame(cpi);
5634 // Build the bitstream
5635#if CONFIG_COLLECT_COMPONENT_TIMING
5636 start_timing(cpi, av1_pack_bitstream_final_time);
5637#endif
5638 if (av1_pack_bitstream(cpi, dest, size, largest_tile_id) != AOM_CODEC_OK)
5639 return AOM_CODEC_ERROR;
5640#if CONFIG_COLLECT_COMPONENT_TIMING
5641 end_timing(cpi, av1_pack_bitstream_final_time);
5642#endif
5643
5644 // Compute sse and rate.
5645 if (sse != NULL) {
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005646#if CONFIG_AV1_HIGHBITDEPTH
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005647 *sse = (seq_params->use_highbitdepth)
5648 ? aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf)
5649 : aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005650#else
5651 *sse = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5652#endif
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005653 }
5654 if (rate != NULL) {
5655 const int64_t bits = (*size << 3);
5656 *rate = (bits << 5); // To match scale.
5657 }
5658 return AOM_CODEC_OK;
5659}
5660
5661#if CONFIG_SUPERRES_IN_RECODE
5662
5663static void save_cur_buf(AV1_COMP *cpi) {
5664 CODING_CONTEXT *const cc = &cpi->coding_context;
5665 AV1_COMMON *cm = &cpi->common;
5666 const YV12_BUFFER_CONFIG *ybf = &cm->cur_frame->buf;
5667 memset(&cc->copy_buffer, 0, sizeof(cc->copy_buffer));
5668 if (aom_alloc_frame_buffer(&cc->copy_buffer, ybf->y_crop_width,
5669 ybf->y_crop_height, ybf->subsampling_x,
5670 ybf->subsampling_y,
5671 ybf->flags & YV12_FLAG_HIGHBITDEPTH, ybf->border,
5672 cm->byte_alignment) != AOM_CODEC_OK) {
5673 aom_internal_error(
5674 &cm->error, AOM_CODEC_MEM_ERROR,
5675 "Failed to allocate copy buffer for saving coding context");
5676 }
5677 aom_yv12_copy_frame(ybf, &cc->copy_buffer, av1_num_planes(cm));
5678}
5679
5680// Coding context that only needs to be saved when recode loop includes
5681// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5682// restoraton).
5683static void save_extra_coding_context(AV1_COMP *cpi) {
5684 CODING_CONTEXT *const cc = &cpi->coding_context;
5685 AV1_COMMON *cm = &cpi->common;
5686
5687 cc->lf = cm->lf;
5688 cc->cdef_info = cm->cdef_info;
5689 cc->rc = cpi->rc;
5690}
5691
5692static void save_all_coding_context(AV1_COMP *cpi) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005693 save_cur_buf(cpi);
5694 save_extra_coding_context(cpi);
5695 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5696}
5697
5698static void restore_cur_buf(AV1_COMP *cpi) {
5699 CODING_CONTEXT *const cc = &cpi->coding_context;
5700 AV1_COMMON *cm = &cpi->common;
5701 aom_yv12_copy_frame(&cc->copy_buffer, &cm->cur_frame->buf,
5702 av1_num_planes(cm));
5703}
5704
5705// Coding context that only needs to be restored when recode loop includes
5706// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5707// restoraton).
5708static void restore_extra_coding_context(AV1_COMP *cpi) {
5709 CODING_CONTEXT *const cc = &cpi->coding_context;
5710 AV1_COMMON *cm = &cpi->common;
5711 cm->lf = cc->lf;
5712 cm->cdef_info = cc->cdef_info;
5713 cpi->rc = cc->rc;
5714}
5715
5716static void restore_all_coding_context(AV1_COMP *cpi) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005717 restore_cur_buf(cpi);
5718 restore_extra_coding_context(cpi);
5719 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5720}
5721
5722static int encode_with_and_without_superres(AV1_COMP *cpi, size_t *size,
5723 uint8_t *dest,
5724 int *largest_tile_id) {
5725 const AV1_COMMON *const cm = &cpi->common;
5726 AV1EncoderConfig *const oxcf = &cpi->oxcf;
5727 assert(cm->seq_params.enable_superres);
5728 assert(superres_in_recode_allowed(cpi));
5729 aom_codec_err_t err = AOM_CODEC_OK;
5730 save_all_coding_context(cpi);
5731
5732 // Encode with superres.
Urvang Joshibc82d382019-11-01 17:59:20 -07005733#if SUPERRES_RECODE_ALL_RATIOS
5734 int64_t superres_sses[SCALE_NUMERATOR];
5735 int64_t superres_rates[SCALE_NUMERATOR];
5736 int superres_largest_tile_ids[SCALE_NUMERATOR];
5737 // Use superres for Key-frames and Alt-ref frames only.
5738 const GF_GROUP *const gf_group = &cpi->gf_group;
5739 if (gf_group->update_type[gf_group->index] != OVERLAY_UPDATE &&
5740 gf_group->update_type[gf_group->index] != INTNL_OVERLAY_UPDATE) {
5741 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
5742 ++denom) {
5743 oxcf->superres_scale_denominator = denom;
5744 oxcf->superres_kf_scale_denominator = denom;
5745 const int this_index = denom - (SCALE_NUMERATOR + 1);
5746 err = encode_with_recode_loop_and_filter(
5747 cpi, size, dest, &superres_sses[this_index],
5748 &superres_rates[this_index], &superres_largest_tile_ids[this_index]);
5749 if (err != AOM_CODEC_OK) return err;
5750 restore_all_coding_context(cpi);
5751 }
5752 // Reset.
5753 oxcf->superres_scale_denominator = SCALE_NUMERATOR;
5754 oxcf->superres_kf_scale_denominator = SCALE_NUMERATOR;
5755 } else {
5756 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
5757 ++denom) {
5758 const int this_index = denom - (SCALE_NUMERATOR + 1);
5759 superres_sses[this_index] = INT64_MAX;
5760 superres_rates[this_index] = INT64_MAX;
5761 }
5762 }
5763#else
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005764 int64_t sse1 = INT64_MAX;
5765 int64_t rate1 = INT64_MAX;
5766 int largest_tile_id1;
5767 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse1, &rate1,
5768 &largest_tile_id1);
5769 if (err != AOM_CODEC_OK) return err;
Urvang Joshibc82d382019-11-01 17:59:20 -07005770 restore_all_coding_context(cpi);
5771#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005772
5773 // Encode without superres.
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005774 int64_t sse2 = INT64_MAX;
5775 int64_t rate2 = INT64_MAX;
5776 int largest_tile_id2;
5777 oxcf->superres_mode = SUPERRES_NONE; // To force full-res.
5778 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
5779 &largest_tile_id2);
5780 oxcf->superres_mode = SUPERRES_AUTO; // Reset.
5781 if (err != AOM_CODEC_OK) return err;
5782
5783 // Note: Both use common rdmult based on base qindex of fullres.
5784 const int64_t rdmult =
5785 av1_compute_rd_mult_based_on_qindex(cpi, cm->base_qindex);
Urvang Joshibc82d382019-11-01 17:59:20 -07005786
5787#if SUPERRES_RECODE_ALL_RATIOS
5788 // Find the best rdcost among all superres denoms.
5789 double proj_rdcost1 = DBL_MAX;
5790 int64_t sse1 = INT64_MAX;
5791 int64_t rate1 = INT64_MAX;
5792 int largest_tile_id1 = 0;
5793 (void)sse1;
5794 (void)rate1;
5795 (void)largest_tile_id1;
5796 int best_denom = -1;
5797 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR; ++denom) {
5798 const int this_index = denom - (SCALE_NUMERATOR + 1);
5799 const int64_t this_sse = superres_sses[this_index];
5800 const int64_t this_rate = superres_rates[this_index];
5801 const int this_largest_tile_id = superres_largest_tile_ids[this_index];
5802 const double this_rdcost = RDCOST_DBL(rdmult, this_rate, this_sse);
5803 if (this_rdcost < proj_rdcost1) {
5804 sse1 = this_sse;
5805 rate1 = this_rate;
5806 largest_tile_id1 = this_largest_tile_id;
5807 proj_rdcost1 = this_rdcost;
5808 best_denom = denom;
5809 }
5810 }
5811#else
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005812 const double proj_rdcost1 = RDCOST_DBL(rdmult, rate1, sse1);
Urvang Joshibc82d382019-11-01 17:59:20 -07005813#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005814 const double proj_rdcost2 = RDCOST_DBL(rdmult, rate2, sse2);
5815
5816 // Re-encode with superres if it's better.
5817 if (proj_rdcost1 < proj_rdcost2) {
5818 restore_all_coding_context(cpi);
5819 // TODO(urvang): We should avoid rerunning the recode loop by saving
5820 // previous output+state, or running encode only for the selected 'q' in
5821 // previous step.
Urvang Joshibc82d382019-11-01 17:59:20 -07005822#if SUPERRES_RECODE_ALL_RATIOS
5823 // Again, temporarily force the best denom.
5824 oxcf->superres_scale_denominator = best_denom;
5825 oxcf->superres_kf_scale_denominator = best_denom;
5826#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005827 int64_t sse3 = INT64_MAX;
5828 int64_t rate3 = INT64_MAX;
5829 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
5830 largest_tile_id);
5831 assert(sse1 == sse3);
5832 assert(rate1 == rate3);
5833 assert(largest_tile_id1 == *largest_tile_id);
Urvang Joshibc82d382019-11-01 17:59:20 -07005834#if SUPERRES_RECODE_ALL_RATIOS
5835 // Reset.
5836 oxcf->superres_scale_denominator = SCALE_NUMERATOR;
5837 oxcf->superres_kf_scale_denominator = SCALE_NUMERATOR;
5838#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005839 } else {
5840 *largest_tile_id = largest_tile_id2;
5841 }
5842
5843 return err;
5844}
5845#endif // CONFIG_SUPERRES_IN_RECODE
5846
Yaowu Xuc27fc142016-08-22 16:08:15 -07005847#define DUMP_RECON_FRAMES 0
5848
5849#if DUMP_RECON_FRAMES == 1
5850// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07005851static void dump_filtered_recon_frames(AV1_COMP *cpi) {
5852 AV1_COMMON *const cm = &cpi->common;
David Turnerd2a592e2018-11-16 14:59:31 +00005853 const CurrentFrame *const current_frame = &cm->current_frame;
David Turnerc29e1a92018-12-06 14:10:14 +00005854 const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005855
Zoe Liub4f31032017-11-03 23:48:35 -07005856 if (recon_buf == NULL) {
David Turnerd2a592e2018-11-16 14:59:31 +00005857 printf("Frame %d is not ready.\n", current_frame->frame_number);
Zoe Liub4f31032017-11-03 23:48:35 -07005858 return;
5859 }
5860
Zoe Liu27deb382018-03-27 15:13:56 -07005861 static const int flag_list[REF_FRAMES] = { 0,
5862 AOM_LAST_FLAG,
5863 AOM_LAST2_FLAG,
5864 AOM_LAST3_FLAG,
5865 AOM_GOLD_FLAG,
5866 AOM_BWD_FLAG,
5867 AOM_ALT2_FLAG,
5868 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07005869 printf(
5870 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
5871 "show_existing_frame=%d) "
5872 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
David Turnerd2a592e2018-11-16 14:59:31 +00005873 current_frame->frame_number, current_frame->order_hint, cm->show_frame,
Zoe Liub4f31032017-11-03 23:48:35 -07005874 cm->show_existing_frame);
5875 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00005876 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
5877 const int ref_offset = buf != NULL ? (int)buf->order_hint : -1;
Urvang Joshib6f17672019-03-05 11:51:02 -08005878 printf(" %d(%c)", ref_offset,
5879 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N');
Zoe Liub4f31032017-11-03 23:48:35 -07005880 }
5881 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07005882
5883 if (!cm->show_frame) {
5884 printf("Frame %d is a no show frame, so no image dump.\n",
David Turnerd2a592e2018-11-16 14:59:31 +00005885 current_frame->frame_number);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005886 return;
5887 }
5888
Zoe Liub4f31032017-11-03 23:48:35 -07005889 int h;
5890 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
5891 FILE *f_recon = NULL;
5892
David Turnerd2a592e2018-11-16 14:59:31 +00005893 if (current_frame->frame_number == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005894 if ((f_recon = fopen(file_name, "wb")) == NULL) {
5895 printf("Unable to open file %s to write.\n", file_name);
5896 return;
5897 }
5898 } else {
5899 if ((f_recon = fopen(file_name, "ab")) == NULL) {
5900 printf("Unable to open file %s to append.\n", file_name);
5901 return;
5902 }
5903 }
5904 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07005905 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
5906 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
Urvang Joshi7a890232019-03-22 17:00:31 -07005907 "refresh_alt_ref_frame=%d, "
Zoe Liuf40a9572017-10-13 12:37:19 -07005908 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Sarah Parkere1b22012019-06-06 16:35:25 -07005909 current_frame->frame_number, cpi->gf_group.index,
5910 cpi->gf_group.update_type[cpi->gf_group.index], current_frame->order_hint,
5911 cm->show_frame, cm->show_existing_frame, cpi->rc.source_alt_ref_active,
5912 cpi->refresh_alt_ref_frame, recon_buf->y_stride, recon_buf->uv_stride,
5913 cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07005914#if 0
5915 int ref_frame;
5916 printf("get_ref_frame_map_idx: [");
5917 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
David Turnera21966b2018-12-05 14:48:49 +00005918 printf(" %d", get_ref_frame_map_idx(cm, ref_frame));
Zoe Liue9b15e22017-07-19 15:53:01 -07005919 printf(" ]\n");
Zoe Liue9b15e22017-07-19 15:53:01 -07005920#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07005921
5922 // --- Y ---
5923 for (h = 0; h < cm->height; ++h) {
5924 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
5925 f_recon);
5926 }
5927 // --- U ---
5928 for (h = 0; h < (cm->height >> 1); ++h) {
5929 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5930 f_recon);
5931 }
5932 // --- V ---
5933 for (h = 0; h < (cm->height >> 1); ++h) {
5934 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5935 f_recon);
5936 }
5937
5938 fclose(f_recon);
5939}
5940#endif // DUMP_RECON_FRAMES
5941
David Turnerefed6372019-01-11 15:14:11 +00005942static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
5943 const YV12_BUFFER_CONFIG *last_picture,
5944 hash_table *last_hash_table) {
5945 aom_clear_system_state();
5946 // check use hash ME
5947 int k;
David Turnerefed6372019-01-11 15:14:11 +00005948
Ranjit Kumar Tulabandu9f26c992019-10-24 12:24:52 +05305949 const int block_size = FORCE_INT_MV_DECISION_BLOCK_SIZE;
David Turnerefed6372019-01-11 15:14:11 +00005950 const double threshold_current = 0.8;
5951 const double threshold_average = 0.95;
5952 const int max_history_size = 32;
5953 int T = 0; // total block
5954 int C = 0; // match with collocated block
5955 int S = 0; // smooth region but not match with collocated block
5956 int M = 0; // match with other block
5957
5958 const int pic_width = cur_picture->y_width;
5959 const int pic_height = cur_picture->y_height;
5960 for (int i = 0; i + block_size <= pic_height; i += block_size) {
5961 for (int j = 0; j + block_size <= pic_width; j += block_size) {
5962 const int x_pos = j;
5963 const int y_pos = i;
5964 int match = 1;
5965 T++;
5966
5967 // check whether collocated block match with current
5968 uint8_t *p_cur = cur_picture->y_buffer;
5969 uint8_t *p_ref = last_picture->y_buffer;
5970 int stride_cur = cur_picture->y_stride;
5971 int stride_ref = last_picture->y_stride;
5972 p_cur += (y_pos * stride_cur + x_pos);
5973 p_ref += (y_pos * stride_ref + x_pos);
5974
5975 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
5976 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
5977 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
5978 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5979 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5980 if (p16_cur[tmpX] != p16_ref[tmpX]) {
5981 match = 0;
5982 }
5983 }
5984 p16_cur += stride_cur;
5985 p16_ref += stride_ref;
5986 }
5987 } else {
5988 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5989 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5990 if (p_cur[tmpX] != p_ref[tmpX]) {
5991 match = 0;
5992 }
5993 }
5994 p_cur += stride_cur;
5995 p_ref += stride_ref;
5996 }
5997 }
5998
5999 if (match) {
6000 C++;
6001 continue;
6002 }
6003
6004 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
6005 y_pos) ||
6006 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
6007 S++;
6008 continue;
6009 }
Remyaa14a4582019-11-05 13:17:36 +05306010 if (av1_use_hash_me(cpi)) {
6011 uint32_t hash_value_1;
6012 uint32_t hash_value_2;
6013 av1_get_block_hash_value(
6014 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
6015 block_size, &hash_value_1, &hash_value_2,
6016 (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH), &cpi->td.mb);
6017 // Hashing does not work for highbitdepth currently.
6018 // TODO(Roger): Make it work for highbitdepth.
David Turnerefed6372019-01-11 15:14:11 +00006019 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
6020 M++;
6021 }
6022 }
6023 }
6024 }
6025
6026 assert(T > 0);
6027 double csm_rate = ((double)(C + S + M)) / ((double)(T));
6028 double m_rate = ((double)(M)) / ((double)(T));
6029
6030 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
6031 cpi->m_rate_array[cpi->rate_index] = m_rate;
6032
6033 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
6034 cpi->rate_size++;
6035 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
6036
6037 if (csm_rate < threshold_current) {
6038 return 0;
6039 }
6040
6041 if (C == T) {
6042 return 1;
6043 }
6044
6045 double csm_average = 0.0;
6046 double m_average = 0.0;
6047
6048 for (k = 0; k < cpi->rate_size; k++) {
6049 csm_average += cpi->csm_rate_array[k];
6050 m_average += cpi->m_rate_array[k];
6051 }
6052 csm_average /= cpi->rate_size;
6053 m_average /= cpi->rate_size;
6054
6055 if (csm_average < threshold_average) {
6056 return 0;
6057 }
6058
6059 if (M > (T - C - S) / 3) {
6060 return 1;
6061 }
6062
6063 if (csm_rate > 0.99 && m_rate > 0.01) {
6064 return 1;
6065 }
6066
6067 if (csm_average + m_average > 1.01) {
6068 return 1;
6069 }
6070
6071 return 0;
6072}
6073
David Turner73245762019-02-11 16:42:34 +00006074// Refresh reference frame buffers according to refresh_frame_flags.
6075static void refresh_reference_frames(AV1_COMP *cpi) {
6076 AV1_COMMON *const cm = &cpi->common;
6077 // All buffers are refreshed for shown keyframes and S-frames.
6078
6079 for (int ref_frame = 0; ref_frame < REF_FRAMES; ref_frame++) {
6080 if (((cm->current_frame.refresh_frame_flags >> ref_frame) & 1) == 1) {
6081 assign_frame_buffer_p(&cm->ref_frame_map[ref_frame], cm->cur_frame);
6082 }
6083 }
6084}
6085
sdengc23c7f12019-06-11 16:56:50 -07006086static void set_mb_ssim_rdmult_scaling(AV1_COMP *cpi) {
6087 AV1_COMMON *cm = &cpi->common;
6088 ThreadData *td = &cpi->td;
6089 MACROBLOCK *x = &td->mb;
6090 MACROBLOCKD *xd = &x->e_mbd;
6091 uint8_t *y_buffer = cpi->source->y_buffer;
6092 const int y_stride = cpi->source->y_stride;
6093 const int block_size = BLOCK_16X16;
6094
6095 const int num_mi_w = mi_size_wide[block_size];
6096 const int num_mi_h = mi_size_high[block_size];
6097 const int num_cols = (cm->mi_cols + num_mi_w - 1) / num_mi_w;
6098 const int num_rows = (cm->mi_rows + num_mi_h - 1) / num_mi_h;
6099 double log_sum = 0.0;
6100 int row, col;
6101 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
6102
sdengc23c7f12019-06-11 16:56:50 -07006103 // Loop through each 16x16 block.
6104 for (row = 0; row < num_rows; ++row) {
6105 for (col = 0; col < num_cols; ++col) {
6106 int mi_row, mi_col;
6107 double var = 0.0, num_of_var = 0.0;
6108 const int index = row * num_cols + col;
6109
6110 // Loop through each 8x8 block.
6111 for (mi_row = row * num_mi_h;
6112 mi_row < cm->mi_rows && mi_row < (row + 1) * num_mi_h; mi_row += 2) {
6113 for (mi_col = col * num_mi_w;
6114 mi_col < cm->mi_cols && mi_col < (col + 1) * num_mi_w;
6115 mi_col += 2) {
6116 struct buf_2d buf;
6117 const int row_offset_y = mi_row << 2;
6118 const int col_offset_y = mi_col << 2;
6119
6120 buf.buf = y_buffer + row_offset_y * y_stride + col_offset_y;
6121 buf.stride = y_stride;
6122
6123 if (use_hbd) {
6124 var += av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8,
6125 xd->bd);
6126 } else {
6127 var += av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8);
6128 }
6129
6130 num_of_var += 1.0;
6131 }
6132 }
sdeng32185d12019-06-19 14:47:09 -07006133 var = var / num_of_var;
sdengbdc0cd22019-11-11 15:09:34 -08006134
6135 // Curve fitting with an exponential model on all 16x16 blocks from the
6136 // midres dataset.
6137 var = 67.035434 * (1 - exp(-0.0021489 * var)) + 17.492222;
sdengc23c7f12019-06-11 16:56:50 -07006138 cpi->ssim_rdmult_scaling_factors[index] = var;
6139 log_sum += log(var);
6140 }
6141 }
6142 log_sum = exp(log_sum / (double)(num_rows * num_cols));
6143
6144 for (row = 0; row < num_rows; ++row) {
6145 for (col = 0; col < num_cols; ++col) {
6146 const int index = row * num_cols + col;
6147 cpi->ssim_rdmult_scaling_factors[index] /= log_sum;
6148 }
6149 }
6150
6151 (void)xd;
6152}
6153
chiyotsaifc1404d2019-08-08 12:09:12 -07006154#if CONFIG_DEBUG
6155static int hash_me_has_at_most_two_refs(RefCntBuffer *frame_bufs) {
6156 int total_count = 0;
6157 for (int frame_idx = 0; frame_idx < FRAME_BUFFERS; ++frame_idx) {
6158 if (frame_bufs[frame_idx].hash_table.has_content > 1) {
6159 return 0;
6160 }
6161 total_count += frame_bufs[frame_idx].hash_table.has_content;
6162 }
6163
6164 return total_count <= 2;
6165}
6166#endif
6167
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08006168extern void av1_print_frame_contexts(const FRAME_CONTEXT *fc,
6169 const char *filename);
6170
David Turner73245762019-02-11 16:42:34 +00006171static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
6172 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006173 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006174 SequenceHeader *const seq_params = &cm->seq_params;
David Turnerd2a592e2018-11-16 14:59:31 +00006175 CurrentFrame *const current_frame = &cm->current_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07006176 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006177 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01006178
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006179#if CONFIG_COLLECT_COMPONENT_TIMING
6180 start_timing(cpi, encode_frame_to_data_rate_time);
6181#endif
6182
Fangwen Fu8d164de2016-12-14 13:40:54 -08006183 // frame type has been decided outside of this function call
David Turnerd2a592e2018-11-16 14:59:31 +00006184 cm->cur_frame->frame_type = current_frame->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07006185
Yunqing Wang9612d552018-05-15 14:58:30 -07006186 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
6187 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wang9612d552018-05-15 14:58:30 -07006188
sarahparker21dbca42018-03-30 17:43:44 -07006189 cm->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
Yunqing Wangd48fb162018-06-15 10:55:28 -07006190 // cm->allow_ref_frame_mvs needs to be written into the frame header while
6191 // cm->large_scale_tile is 1, therefore, "cm->large_scale_tile=1" case is
6192 // separated from frame_might_allow_ref_frame_mvs().
6193 cm->allow_ref_frame_mvs &= !cm->large_scale_tile;
6194
Debargha Mukherjee1d7217e2018-03-26 13:32:13 -07006195 cm->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07006196 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006197
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05306198 cm->last_frame_type = current_frame->frame_type;
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05306199
Sarah Parker33005522018-07-27 14:46:25 -07006200 if (encode_show_existing_frame(cm)) {
David Turner996b2c12018-12-07 15:52:30 +00006201 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006202 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00006203 int largest_tile_id = 0; // Output from bitstream: unused here
6204 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08006205 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006206
David Turner90311862018-11-29 13:34:36 +00006207 if (seq_params->frame_id_numbers_present_flag &&
6208 current_frame->frame_type == KEY_FRAME) {
6209 // Displaying a forward key-frame, so reset the ref buffer IDs
6210 int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
6211 for (int i = 0; i < REF_FRAMES; i++)
6212 cm->ref_frame_id[i] = display_frame_id;
6213 }
6214
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07006215 cpi->seq_params_locked = 1;
6216
Yaowu Xuc27fc142016-08-22 16:08:15 -07006217#if DUMP_RECON_FRAMES == 1
6218 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
6219 dump_filtered_recon_frames(cpi);
6220#endif // DUMP_RECON_FRAMES
6221
David Turner73245762019-02-11 16:42:34 +00006222 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
6223 // for the purpose to verify no mismatch between encoder and decoder.
6224 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006225
David Turner73245762019-02-11 16:42:34 +00006226 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006227
Yaowu Xuc27fc142016-08-22 16:08:15 -07006228 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
6229 // to do post-encoding update accordingly.
6230 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07006231 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07006232 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006233 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006234
David Turnerd2a592e2018-11-16 14:59:31 +00006235 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006236
Tom Finegane4099e32018-01-23 12:01:51 -08006237 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006238 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006239
David Turnerefed6372019-01-11 15:14:11 +00006240 // Work out whether to force_integer_mv this frame
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306241 if (!is_stat_generation_stage(cpi) &&
6242 cpi->common.allow_screen_content_tools && !frame_is_intra_only(cm)) {
David Turnerefed6372019-01-11 15:14:11 +00006243 if (cpi->common.seq_params.force_integer_mv == 2) {
6244 // Adaptive mode: see what previous frame encoded did
6245 if (cpi->unscaled_last_source != NULL) {
6246 cm->cur_frame_force_integer_mv =
6247 is_integer_mv(cpi, cpi->source, cpi->unscaled_last_source,
6248 cpi->previous_hash_table);
6249 } else {
6250 cpi->common.cur_frame_force_integer_mv = 0;
6251 }
6252 } else {
6253 cpi->common.cur_frame_force_integer_mv =
6254 cpi->common.seq_params.force_integer_mv;
6255 }
6256 } else {
6257 cpi->common.cur_frame_force_integer_mv = 0;
6258 }
6259
chiyotsaifc1404d2019-08-08 12:09:12 -07006260#if CONFIG_DEBUG
6261 assert(hash_me_has_at_most_two_refs(cm->buffer_pool->frame_bufs) &&
6262 "Hash-me is leaking memory!");
6263#endif
6264
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306265 if (!is_stat_generation_stage(cpi) && cpi->need_to_clear_prev_hash_table) {
chiyotsaifc1404d2019-08-08 12:09:12 -07006266 av1_hash_table_clear_all(cpi->previous_hash_table);
6267 cpi->need_to_clear_prev_hash_table = 0;
6268 }
6269
Yaowu Xuc27fc142016-08-22 16:08:15 -07006270 // Set default state for segment based loop filter update flags.
6271 cm->lf.mode_ref_delta_update = 0;
6272
Yaowu Xuc27fc142016-08-22 16:08:15 -07006273 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05006274 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006275 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07006276 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006277
6278 // If segmentation is enabled force a map update for key frames.
6279 if (seg->enabled) {
6280 seg->update_map = 1;
6281 seg->update_data = 1;
6282 }
6283
6284 // The alternate reference frame cannot be active for a key frame.
6285 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006286 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00006287 if (cpi->oxcf.mtu == 0) {
6288 cm->num_tg = cpi->oxcf.num_tile_groups;
6289 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08006290 // Use a default value for the purposes of weighting costs in probability
6291 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00006292 cm->num_tg = DEFAULT_MAX_NUM_TG;
6293 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006294
6295 // For 1 pass CBR, check if we are dropping this frame.
6296 // Never drop on key frame.
Mufaddal Chakera3bcc72c2019-12-11 14:38:37 +05306297 if (has_no_stats_stage(cpi) && oxcf->rc_mode == AOM_CBR &&
David Turnerd2a592e2018-11-16 14:59:31 +00006298 current_frame->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006299 if (av1_rc_drop_frame(cpi)) {
6300 av1_rc_postencode_update_drop_frame(cpi);
David Turnera4c96252019-01-11 16:36:39 +00006301 release_scaled_references(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08006302 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006303 }
6304 }
6305
sdengc23c7f12019-06-11 16:56:50 -07006306 if (oxcf->tuning == AOM_TUNE_SSIM) set_mb_ssim_rdmult_scaling(cpi);
6307
sdeng01959162019-12-20 10:46:24 -08006308#if CONFIG_TUNE_VMAF
6309 if (oxcf->tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
sdeng615dc242020-02-04 16:06:14 -08006310 oxcf->tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING ||
6311 oxcf->tuning == AOM_TUNE_VMAF_MAX_GAIN) {
sdeng01959162019-12-20 10:46:24 -08006312 av1_set_mb_vmaf_rdmult_scaling(cpi);
sdeng29c62152019-12-09 12:44:18 -08006313 }
sdeng01959162019-12-20 10:46:24 -08006314#endif
sdeng29c62152019-12-09 12:44:18 -08006315
Yaowu Xuf883b422016-08-30 14:01:10 -07006316 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006317
6318#if CONFIG_INTERNAL_STATS
6319 memset(cpi->mode_chosen_counts, 0,
6320 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
6321#endif
6322
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006323 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006324 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08006325 * wraparound) */
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006326 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00006327 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08006328 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07006329 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
6330 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
6331 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00006332 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07006333 lsb = cpi->source->y_buffer[0] & 0xff;
6334 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00006335 }
David Turner760a2f42018-12-07 15:25:36 +00006336 cm->current_frame_id =
6337 ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05006338
6339 // S_frame is meant for stitching different streams of different
6340 // resolutions together, so current_frame_id must be the
6341 // same across different streams of the same content current_frame_id
6342 // should be the same and not random. 0x37 is a chosen number as start
6343 // point
6344 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006345 } else {
6346 cm->current_frame_id =
David Turner760a2f42018-12-07 15:25:36 +00006347 (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
6348 (1 << seq_params->frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006349 }
6350 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006351
Hui Su483a8452018-02-26 12:28:48 -08006352 switch (cpi->oxcf.cdf_update_mode) {
6353 case 0: // No CDF update for any frames(4~6% compression loss).
6354 cm->disable_cdf_update = 1;
6355 break;
6356 case 1: // Enable CDF update for all frames.
6357 cm->disable_cdf_update = 0;
6358 break;
6359 case 2:
6360 // Strategically determine at which frames to do CDF update.
6361 // Currently only enable CDF update for all-intra and no-show frames(1.5%
6362 // compression loss).
6363 // TODO(huisu@google.com): design schemes for various trade-offs between
6364 // compression quality and decoding speed.
Hui Sub1b76b32018-02-27 15:24:48 -08006365 cm->disable_cdf_update =
6366 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08006367 break;
6368 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006369 cm->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08006370
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07006371 int largest_tile_id = 0;
6372#if CONFIG_SUPERRES_IN_RECODE
6373 if (superres_in_recode_allowed(cpi)) {
6374 if (encode_with_and_without_superres(cpi, size, dest, &largest_tile_id) !=
6375 AOM_CODEC_OK) {
6376 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006377 }
Hui Su06463e42018-02-23 22:17:36 -08006378 } else {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07006379#endif // CONFIG_SUPERRES_IN_RECODE
6380 if (encode_with_recode_loop_and_filter(cpi, size, dest, NULL, NULL,
6381 &largest_tile_id) != AOM_CODEC_OK) {
6382 return AOM_CODEC_ERROR;
6383 }
6384#if CONFIG_SUPERRES_IN_RECODE
Hui Su06463e42018-02-23 22:17:36 -08006385 }
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07006386#endif // CONFIG_SUPERRES_IN_RECODE
Yaowu Xuc27fc142016-08-22 16:08:15 -07006387
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07006388 cpi->seq_params_locked = 1;
6389
David Turner996b2c12018-12-07 15:52:30 +00006390 // Update reference frame ids for reference frames this frame will overwrite
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006391 if (seq_params->frame_id_numbers_present_flag) {
David Turner996b2c12018-12-07 15:52:30 +00006392 for (int i = 0; i < REF_FRAMES; i++) {
6393 if ((current_frame->refresh_frame_flags >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006394 cm->ref_frame_id[i] = cm->current_frame_id;
6395 }
6396 }
6397 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006398
Yaowu Xuc27fc142016-08-22 16:08:15 -07006399#if DUMP_RECON_FRAMES == 1
6400 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07006401 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006402#endif // DUMP_RECON_FRAMES
6403
Soo-Chul Han934af352017-10-15 15:21:51 -04006404 if (cm->seg.enabled) {
6405 if (cm->seg.update_map) {
6406 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07006407 } else if (cm->last_frame_seg_map) {
David Turnerb757ce02018-11-12 15:01:28 +00006408 memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
Soo-Chul Han934af352017-10-15 15:21:51 -04006409 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
6410 }
6411 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006412
6413 if (frame_is_intra_only(cm) == 0) {
6414 release_scaled_references(cpi);
6415 }
6416
David Turner73245762019-02-11 16:42:34 +00006417 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
6418 // for the purpose to verify no mismatch between encoder and decoder.
6419 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
6420
6421 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006422
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08006423#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07006424 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08006425#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006426
Hui Sudc54be62018-03-14 19:14:28 -07006427 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
David Turner35cba132018-12-10 15:48:15 +00006428 *cm->fc = cpi->tile_data[largest_tile_id].tctx;
Hui Sudc54be62018-03-14 19:14:28 -07006429 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006430 }
David Turnera4c96252019-01-11 16:36:39 +00006431 if (!cm->large_scale_tile) {
6432 cm->cur_frame->frame_context = *cm->fc;
6433 }
Yunqing Wangd8fd9e72019-12-26 15:36:31 -08006434
6435 if (cpi->oxcf.ext_tile_debug) {
6436 // (yunqing) This test ensures the correctness of large scale tile coding.
6437 if (cm->large_scale_tile && is_stat_consumption_stage(cpi)) {
6438 char fn[20] = "./fc";
6439 fn[4] = current_frame->frame_number / 100 + '0';
6440 fn[5] = (current_frame->frame_number % 100) / 10 + '0';
6441 fn[6] = (current_frame->frame_number % 10) + '0';
6442 fn[7] = '\0';
6443 av1_print_frame_contexts(cm->fc, fn);
6444 }
David Turnera4c96252019-01-11 16:36:39 +00006445 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006446
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006447#if CONFIG_COLLECT_COMPONENT_TIMING
6448 end_timing(cpi, encode_frame_to_data_rate_time);
6449
6450 // Print out timing information.
6451 int i;
chiyotsai9c484b32019-03-07 16:01:50 -08006452 fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n",
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006453 cm->current_frame.frame_number,
chiyotsai9c484b32019-03-07 16:01:50 -08006454 get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006455 for (i = 0; i < kTimingComponents; i++) {
6456 cpi->component_time[i] += cpi->frame_component_time[i];
6457 fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n",
6458 get_component_name(i), cpi->frame_component_time[i],
6459 cpi->component_time[i]);
6460 cpi->frame_component_time[i] = 0;
6461 }
6462#endif
6463
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05306464 cm->last_frame_type = current_frame->frame_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006465
Yaowu Xuf883b422016-08-30 14:01:10 -07006466 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006467
chiyotsaifc1404d2019-08-08 12:09:12 -07006468 // Store encoded frame's hash table for in_integer_mv() next time.
6469 // Beware! If we don't update previous_hash_table here we will leak the
6470 // items stored in cur_frame's hash_table!
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306471 if (!is_stat_generation_stage(cpi) && av1_use_hash_me(cpi)) {
David Turnerefed6372019-01-11 15:14:11 +00006472 cpi->previous_hash_table = &cm->cur_frame->hash_table;
chiyotsaifc1404d2019-08-08 12:09:12 -07006473 cpi->need_to_clear_prev_hash_table = 1;
David Turnerefed6372019-01-11 15:14:11 +00006474 }
6475
Yaowu Xuc27fc142016-08-22 16:08:15 -07006476 // Clear the one shot update flags for segmentation map and mode/ref loop
6477 // filter deltas.
6478 cm->seg.update_map = 0;
6479 cm->seg.update_data = 0;
6480 cm->lf.mode_ref_delta_update = 0;
6481
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07006482 // A droppable frame might not be shown but it always
6483 // takes a space in the gf group. Therefore, even when
6484 // it is not shown, we still need update the count down.
6485
Yaowu Xuc27fc142016-08-22 16:08:15 -07006486 if (cm->show_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006487 // Don't increment frame counters if this was an altref buffer
6488 // update not a real frame
David Turnerd2a592e2018-11-16 14:59:31 +00006489 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006490 }
6491
Tom Finegane4099e32018-01-23 12:01:51 -08006492 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006493}
6494
David Turner056f7cd2019-01-07 17:48:13 +00006495int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
David Turnercb5e36f2019-01-17 17:15:25 +00006496 const EncodeFrameInput *const frame_input,
David Turner056f7cd2019-01-07 17:48:13 +00006497 const EncodeFrameParams *const frame_params,
6498 EncodeFrameResults *const frame_results) {
David Turner07dbd8e2019-01-08 17:16:25 +00006499 AV1_COMMON *const cm = &cpi->common;
David Turnera7f133c2019-01-22 14:47:16 +00006500 CurrentFrame *const current_frame = &cm->current_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00006501
David Turnercb5e36f2019-01-17 17:15:25 +00006502 cpi->unscaled_source = frame_input->source;
6503 cpi->source = frame_input->source;
6504 cpi->unscaled_last_source = frame_input->last_source;
David Turner056f7cd2019-01-07 17:48:13 +00006505
David Turner6e8b4d92019-02-18 15:01:33 +00006506 current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
David Turner07dbd8e2019-01-08 17:16:25 +00006507 cm->error_resilient_mode = frame_params->error_resilient_mode;
David Turnera7f133c2019-01-22 14:47:16 +00006508 cm->primary_ref_frame = frame_params->primary_ref_frame;
David Turner475a3132019-01-18 15:17:17 +00006509 cm->current_frame.frame_type = frame_params->frame_type;
David Turnerdedd8ff2019-01-23 13:59:46 +00006510 cm->show_frame = frame_params->show_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00006511 cpi->ref_frame_flags = frame_params->ref_frame_flags;
David Turner04b70d82019-01-24 15:39:19 +00006512 cpi->speed = frame_params->speed;
David Turnere86ee0d2019-02-18 17:16:28 +00006513 cm->show_existing_frame = frame_params->show_existing_frame;
6514 cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
David Turner07dbd8e2019-01-08 17:16:25 +00006515
David Turner73245762019-02-11 16:42:34 +00006516 memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
6517 REF_FRAMES * sizeof(*cm->remapped_ref_idx));
6518
David Turnerfe3aecb2019-02-06 14:42:42 +00006519 cpi->refresh_last_frame = frame_params->refresh_last_frame;
6520 cpi->refresh_golden_frame = frame_params->refresh_golden_frame;
6521 cpi->refresh_bwd_ref_frame = frame_params->refresh_bwd_ref_frame;
David Turnerfe3aecb2019-02-06 14:42:42 +00006522 cpi->refresh_alt_ref_frame = frame_params->refresh_alt_ref_frame;
6523
David Turnera7f133c2019-01-22 14:47:16 +00006524 if (current_frame->frame_type == KEY_FRAME && cm->show_frame)
6525 current_frame->frame_number = 0;
6526
Remyafc038662020-02-06 11:46:11 +05306527 current_frame->order_hint =
6528 current_frame->frame_number + frame_params->order_offset;
6529 current_frame->display_order_hint = current_frame->order_hint;
6530 current_frame->order_hint %=
6531 (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
David Turnera7f133c2019-01-22 14:47:16 +00006532
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306533 if (is_stat_generation_stage(cpi)) {
Jerome Jiang2612b4d2019-05-29 17:46:47 -07006534#if !CONFIG_REALTIME_ONLY
David Turnercb5e36f2019-01-17 17:15:25 +00006535 av1_first_pass(cpi, frame_input->ts_duration);
Jerome Jiang2612b4d2019-05-29 17:46:47 -07006536#endif
David Turnercb5e36f2019-01-17 17:15:25 +00006537 } else if (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) {
David Turner73245762019-02-11 16:42:34 +00006538 if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
6539 AOM_CODEC_OK) {
David Turnercb5e36f2019-01-17 17:15:25 +00006540 return AOM_CODEC_ERROR;
6541 }
6542 } else {
David Turner056f7cd2019-01-07 17:48:13 +00006543 return AOM_CODEC_ERROR;
6544 }
6545
6546 return AOM_CODEC_OK;
6547}
6548
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006549#if CONFIG_DENOISE
6550static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
6551 int block_size, float noise_level,
6552 int64_t time_stamp, int64_t end_time) {
6553 AV1_COMMON *const cm = &cpi->common;
6554 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006555 cpi->denoise_and_model = aom_denoise_and_model_alloc(
6556 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006557 if (!cpi->denoise_and_model) {
6558 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6559 "Error allocating denoise and model");
6560 return -1;
6561 }
6562 }
6563 if (!cpi->film_grain_table) {
6564 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
6565 if (!cpi->film_grain_table) {
6566 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6567 "Error allocating grain table");
6568 return -1;
6569 }
6570 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
6571 }
6572 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
6573 &cm->film_grain_params)) {
6574 if (cm->film_grain_params.apply_grain) {
6575 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
6576 &cm->film_grain_params);
6577 }
6578 }
6579 return 0;
6580}
6581#endif
6582
James Zern3e2613b2017-03-30 23:14:40 -07006583int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07006584 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
6585 int64_t end_time) {
6586 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006587 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006588 int res = 0;
6589 const int subsampling_x = sd->subsampling_x;
6590 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006591 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006592
sdenge63f9fa2019-12-13 09:29:55 -08006593#if CONFIG_TUNE_VMAF
6594 if (!is_stat_generation_stage(cpi) &&
6595 cpi->oxcf.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING) {
sdeng615dc242020-02-04 16:06:14 -08006596 av1_vmaf_frame_preprocessing(cpi, sd);
6597 }
6598 if (!is_stat_generation_stage(cpi) &&
6599 cpi->oxcf.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
6600 av1_vmaf_blk_preprocessing(cpi, sd);
sdenge63f9fa2019-12-13 09:29:55 -08006601 }
6602#endif
6603
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006604#if CONFIG_INTERNAL_STATS
6605 struct aom_usec_timer timer;
Yaowu Xuf883b422016-08-30 14:01:10 -07006606 aom_usec_timer_start(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006607#endif
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006608#if CONFIG_DENOISE
6609 if (cpi->oxcf.noise_level > 0)
6610 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
6611 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
6612 res = -1;
6613#endif // CONFIG_DENOISE
6614
Yaowu Xuf883b422016-08-30 14:01:10 -07006615 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08006616 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07006617 res = -1;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006618#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006619 aom_usec_timer_mark(&timer);
6620 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006621#endif
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006622 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07006623 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006624 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006625 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07006626 res = -1;
6627 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006628 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006629 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006630 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006631 "Profile 1 requires 4:4:4 color format");
6632 res = -1;
6633 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006634 if ((seq_params->profile == PROFILE_2) &&
6635 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006636 !(subsampling_x == 1 && subsampling_y == 0)) {
6637 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
6638 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07006639 res = -1;
6640 }
6641
6642 return res;
6643}
6644
Yaowu Xuc27fc142016-08-22 16:08:15 -07006645#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006646extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
6647 const unsigned char *img2, int img2_pitch,
6648 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006649
6650static void adjust_image_stat(double y, double u, double v, double all,
6651 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07006652 s->stat[STAT_Y] += y;
6653 s->stat[STAT_U] += u;
6654 s->stat[STAT_V] += v;
6655 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07006656 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006657}
6658
Angie Chiang08a22a62017-07-17 17:29:17 -07006659static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006660 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006661 double samples = 0.0;
Yaowu Xue75b58a2020-01-03 12:56:12 -08006662 const uint32_t in_bit_depth = cpi->oxcf.input_bit_depth;
6663 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006664
Angie Chiang08a22a62017-07-17 17:29:17 -07006665#if CONFIG_INTER_STATS_ONLY
David Turnerd2a592e2018-11-16 14:59:31 +00006666 if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07006667#endif
6668 cpi->bytes += frame_bytes;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006669 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07006670 const YV12_BUFFER_CONFIG *orig = cpi->source;
David Turnerc29e1a92018-12-06 14:10:14 +00006671 const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006672 double y, u, v, frame_all;
6673
6674 cpi->count++;
6675 if (cpi->b_calculate_psnr) {
6676 PSNR_STATS psnr;
6677 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07006678 aom_clear_system_state();
Jerome Jiangfa1d1732019-08-06 10:31:20 -07006679#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xue75b58a2020-01-03 12:56:12 -08006680 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07006681#else
6682 aom_calc_psnr(orig, recon, &psnr);
6683#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006684 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
6685 &cpi->psnr);
6686 cpi->total_sq_error += psnr.sse[0];
6687 cpi->total_samples += psnr.samples[0];
6688 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08006689 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006690 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07006691 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07006692 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006693 else
Yaowu Xuf883b422016-08-30 14:01:10 -07006694 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006695
Yaowu Xuf883b422016-08-30 14:01:10 -07006696 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006697 cpi->summed_quality += frame_ssim2 * weight;
6698 cpi->summed_weights += weight;
6699
6700#if 0
6701 {
6702 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08006703 double y2 = psnr.psnr[1];
6704 double u2 = psnr.psnr[2];
6705 double v2 = psnr.psnr[3];
6706 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07006707 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 +00006708 cm->current_frame.frame_number, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07006709 frame_psnr2, frame_ssim2);
6710 fclose(f);
6711 }
6712#endif
6713 }
6714 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006715 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006716 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07006717 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
6718 recon->y_stride, orig->y_width, orig->y_height);
6719 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006720 cpi->total_blockiness += frame_blockiness;
6721 }
6722
6723 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006724 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006725 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07006726 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
6727 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
6728
6729 const double peak = (double)((1 << in_bit_depth) - 1);
6730 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006731 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006732 if (consistency > 0.0)
6733 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006734 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006735 cpi->total_inconsistency += this_inconsistency;
6736 }
6737 }
6738 }
6739
6740 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07006741 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006742 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07006743 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006744 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
6745 }
6746}
6747#endif // CONFIG_INTERNAL_STATS
Andrey Norkin795ba872018-03-06 13:24:14 -08006748int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
6749 size_t *size, uint8_t *dest, int64_t *time_stamp,
6750 int64_t *time_end, int flush,
Yue Chen1bc5be62018-08-24 13:57:32 -07006751 const aom_rational64_t *timestamp_ratio) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006752 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
6753 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006754
6755#if CONFIG_BITSTREAM_DEBUG
6756 assert(cpi->oxcf.max_threads == 0 &&
6757 "bitstream debug tool does not support multithreading");
6758 bitstream_queue_record_write();
Yaowu Xu63f2ea32019-04-29 10:47:42 -07006759 aom_bitstream_queue_set_frame_write(cm->current_frame.frame_number * 2 +
6760 cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006761#endif
Marco Paniconi63971322019-08-15 21:32:05 -07006762 if (cpi->use_svc && cm->number_spatial_layers > 1) {
6763 av1_one_pass_cbr_svc_start_layer(cpi);
6764 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006765
Sarah Parker740e8392019-01-23 15:47:53 -08006766 // Indicates whether or not to use an adaptive quantize b rather than
6767 // the traditional version
6768 cm->use_quant_b_adapt = cpi->oxcf.quant_b_adapt;
6769
Dominic Symesd4929012018-01-31 17:32:01 +01006770 cm->showable_frame = 0;
David Turnere43f7fe2019-01-15 14:58:00 +00006771 *size = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006772#if CONFIG_INTERNAL_STATS
6773 struct aom_usec_timer cmptimer;
Yaowu Xuf883b422016-08-30 14:01:10 -07006774 aom_usec_timer_start(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006775#endif
chiyotsaic666b1f2019-12-20 10:44:58 -08006776 av1_set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006777
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07006778 // Normal defaults
sarahparker27d686a2018-03-30 17:43:44 -07006779 cm->refresh_frame_context = oxcf->frame_parallel_decoding_mode
6780 ? REFRESH_FRAME_CONTEXT_DISABLED
6781 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01006782 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08006783 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006784
Sarah Parkerb9041612018-05-22 19:06:47 -07006785 // Initialize fields related to forward keyframes
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07006786 cpi->no_show_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08006787
David Turnerdedd8ff2019-01-23 13:59:46 +00006788 if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006789
Yue Chen1bc5be62018-08-24 13:57:32 -07006790 const int result =
6791 av1_encode_strategy(cpi, size, dest, frame_flags, time_stamp, time_end,
6792 timestamp_ratio, flush);
David Turnerdedd8ff2019-01-23 13:59:46 +00006793 if (result != AOM_CODEC_OK && result != -1) {
David Turner1539bb02019-01-24 15:28:13 +00006794 return AOM_CODEC_ERROR;
David Turnerdedd8ff2019-01-23 13:59:46 +00006795 } else if (result == -1) {
6796 // Returning -1 indicates no frame encoded; more input is required
6797 return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006798 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006799#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006800 aom_usec_timer_mark(&cmptimer);
6801 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yue Chen1bc5be62018-08-24 13:57:32 -07006802#endif // CONFIG_INTERNAL_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00006803 if (cpi->b_calculate_psnr) {
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306804 if (cm->show_existing_frame ||
6805 (!is_stat_generation_stage(cpi) && cm->show_frame)) {
David Turnerc4bf8c72019-01-15 13:14:37 +00006806 generate_psnr_packet(cpi);
6807 }
6808 }
Hui Su8ea87322019-03-29 14:44:32 -07006809
sdeng3cd9eec2020-01-23 15:49:50 -08006810#if CONFIG_TUNE_VMAF
sdeng995c54e2020-02-18 14:51:00 -08006811 if (!is_stat_generation_stage(cpi) &&
6812 (oxcf->tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING ||
6813 oxcf->tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
6814 oxcf->tuning == AOM_TUNE_VMAF_MAX_GAIN)) {
6815 av1_update_vmaf_curve(cpi, cpi->source, &cpi->common.cur_frame->buf);
sdeng3cd9eec2020-01-23 15:49:50 -08006816 }
6817#endif
6818
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306819 if (cpi->keep_level_stats && !is_stat_generation_stage(cpi)) {
Hui Su8ea87322019-03-29 14:44:32 -07006820 // Initialize level info. at the beginning of each sequence.
6821 if (cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) {
Hui Su58753d62019-05-29 09:56:19 -07006822 av1_init_level_info(cpi);
Hui Su8ea87322019-03-29 14:44:32 -07006823 }
kyslovabeeb7c2019-03-06 18:35:04 -08006824 av1_update_level_info(cpi, *size, *time_stamp, *time_end);
Hui Su8ea87322019-03-29 14:44:32 -07006825 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006826
6827#if CONFIG_INTERNAL_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306828 if (!is_stat_generation_stage(cpi)) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006829 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006830 }
6831#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08006832#if CONFIG_SPEED_STATS
Mufaddal Chakerae7326122019-12-04 14:49:09 +05306833 if (!is_stat_generation_stage(cpi) && !cm->show_existing_frame) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08006834 cpi->tx_search_count += cpi->td.mb.tx_search_count;
6835 cpi->td.mb.tx_search_count = 0;
6836 }
6837#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006838
Yaowu Xuf883b422016-08-30 14:01:10 -07006839 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006840
Hui Su0d0ee662019-07-29 14:38:36 -07006841 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006842}
6843
Yaowu Xuf883b422016-08-30 14:01:10 -07006844int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6845 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006846 if (!cm->show_frame) {
6847 return -1;
6848 } else {
6849 int ret;
David Turnerc29e1a92018-12-06 14:10:14 +00006850 if (cm->cur_frame != NULL) {
6851 *dest = cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006852 dest->y_width = cm->width;
6853 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006854 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
6855 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006856 ret = 0;
6857 } else {
6858 ret = -1;
6859 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006860 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006861 return ret;
6862 }
6863}
6864
Yaowu Xuf883b422016-08-30 14:01:10 -07006865int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
David Turnere7ebf902018-12-04 14:04:55 +00006866 if (cpi->last_show_frame_buf == NULL) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006867
David Turnere7ebf902018-12-04 14:04:55 +00006868 *frame = cpi->last_show_frame_buf->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006869 return 0;
6870}
6871
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006872static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
6873 const YV12_BUFFER_CONFIG *b) {
6874 return a->y_height == b->y_height && a->y_width == b->y_width &&
6875 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
6876 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
6877 a->border == b->border &&
6878 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
6879 (b->flags & YV12_FLAG_HIGHBITDEPTH);
6880}
6881
Yunqing Wang93b18f32018-06-08 21:08:29 -07006882aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
6883 YV12_BUFFER_CONFIG *new_frame,
6884 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006885 const int num_planes = av1_num_planes(cm);
6886 if (!equal_dimensions_and_border(new_frame, sd))
6887 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
6888 "Incorrect buffer dimensions");
6889 else
6890 aom_yv12_copy_frame(new_frame, sd, num_planes);
6891
6892 return cm->error.error_code;
6893}
6894
Yaowu Xuf883b422016-08-30 14:01:10 -07006895int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6896 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006897 int hr = 0, hs = 0, vr = 0, vs = 0;
6898
6899 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6900
6901 Scale2Ratio(horiz_mode, &hr, &hs);
6902 Scale2Ratio(vert_mode, &vr, &vs);
6903
6904 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07006905 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6906 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006907
6908 return 0;
6909}
6910
Yaowu Xuf883b422016-08-30 14:01:10 -07006911int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006912
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006913int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
6914 size_t output_size = 0;
6915 size_t total_bytes_read = 0;
6916 size_t remaining_size = *frame_size;
6917 uint8_t *buff_ptr = buffer;
6918
6919 // go through each OBUs
6920 while (total_bytes_read < *frame_size) {
6921 uint8_t saved_obu_header[2];
6922 uint64_t obu_payload_size;
6923 size_t length_of_payload_size;
6924 size_t length_of_obu_size;
6925 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
6926 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
6927
6928 // save the obu header (1 or 2 bytes)
6929 memmove(saved_obu_header, buff_ptr, obu_header_size);
6930 // clear the obu_has_size_field
6931 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
6932
6933 // get the payload_size and length of payload_size
6934 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
6935 &obu_payload_size, &length_of_payload_size) != 0) {
6936 return AOM_CODEC_ERROR;
6937 }
6938 obu_bytes_read += length_of_payload_size;
6939
6940 // calculate the length of size of the obu header plus payload
6941 length_of_obu_size =
6942 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
6943
6944 // move the rest of data to new location
6945 memmove(buff_ptr + length_of_obu_size + obu_header_size,
6946 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07006947 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006948
6949 // write the new obu size
6950 const uint64_t obu_size = obu_header_size + obu_payload_size;
6951 size_t coded_obu_size;
6952 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
6953 &coded_obu_size) != 0) {
6954 return AOM_CODEC_ERROR;
6955 }
6956
6957 // write the saved (modified) obu_header following obu size
6958 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
6959
6960 total_bytes_read += obu_bytes_read;
6961 remaining_size -= obu_bytes_read;
6962 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07006963 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006964 }
6965
6966 *frame_size = output_size;
6967 return AOM_CODEC_OK;
6968}
6969
Marco Paniconid8574e32019-08-04 21:30:12 -07006970static void svc_set_updates_external_ref_frame_config(AV1_COMP *cpi) {
6971 cpi->ext_refresh_frame_flags_pending = 1;
6972 cpi->ext_refresh_last_frame = cpi->svc.refresh[cpi->svc.ref_idx[0]];
6973 cpi->ext_refresh_golden_frame = cpi->svc.refresh[cpi->svc.ref_idx[3]];
6974 cpi->ext_refresh_bwd_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[4]];
6975 cpi->ext_refresh_alt2_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[5]];
6976 cpi->ext_refresh_alt_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[6]];
6977 cpi->svc.non_reference_frame = 1;
6978 for (int i = 0; i < REF_FRAMES; i++) {
6979 if (cpi->svc.refresh[i] == 1) {
6980 cpi->svc.non_reference_frame = 0;
6981 break;
6982 }
6983 }
6984}
6985
Yaowu Xuf883b422016-08-30 14:01:10 -07006986void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006987 // TODO(yunqingwang): For what references to use, external encoding flags
6988 // should be consistent with internal reference frame selection. Need to
6989 // ensure that there is not conflict between the two. In AV1 encoder, the
6990 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006991 // GOLDEN, BWDREF, ALTREF2.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08006992 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006993 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006994 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
6995 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
6996 AOM_EFLAG_NO_REF_ARF2)) {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006997 int ref = AOM_REFFRAME_ALL;
6998
6999 if (flags & AOM_EFLAG_NO_REF_LAST) ref ^= AOM_LAST_FLAG;
7000 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
7001 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
7002
7003 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
7004
7005 if (flags & AOM_EFLAG_NO_REF_ARF) {
7006 ref ^= AOM_ALT_FLAG;
7007 ref ^= AOM_BWD_FLAG;
7008 ref ^= AOM_ALT2_FLAG;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007009 } else {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07007010 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
7011 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007012 }
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07007013
7014 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007015 }
7016
7017 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007018 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07007019 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007020
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007021 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
7022 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007023
Yaowu Xuf883b422016-08-30 14:01:10 -07007024 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007025
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007026 if (flags & AOM_EFLAG_NO_UPD_ARF) {
7027 upd ^= AOM_ALT_FLAG;
7028 upd ^= AOM_BWD_FLAG;
7029 upd ^= AOM_ALT2_FLAG;
7030 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07007031
David Turnerce9b5902019-01-23 17:25:47 +00007032 cpi->ext_refresh_last_frame = (upd & AOM_LAST_FLAG) != 0;
7033 cpi->ext_refresh_golden_frame = (upd & AOM_GOLD_FLAG) != 0;
7034 cpi->ext_refresh_alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
7035 cpi->ext_refresh_bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
7036 cpi->ext_refresh_alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
7037 cpi->ext_refresh_frame_flags_pending = 1;
David Turner4f1f1812019-01-24 17:00:24 +00007038 } else {
Marco Paniconid8574e32019-08-04 21:30:12 -07007039 if (cpi->svc.external_ref_frame_config)
7040 svc_set_updates_external_ref_frame_config(cpi);
7041 else
7042 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007043 }
7044
sarahparker21dbca42018-03-30 17:43:44 -07007045 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
7046 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
sarahparker27d686a2018-03-30 17:43:44 -07007047 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
7048 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
sarahparker9806fed2018-03-30 17:43:44 -07007049 cpi->ext_use_s_frame =
7050 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Sarah Parker50b6d6e2018-04-11 19:21:54 -07007051 cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07007052
Yaowu Xuf883b422016-08-30 14:01:10 -07007053 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
7054 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007055 }
7056}
Andrey Norkin795ba872018-03-06 13:24:14 -08007057
Tom Fineganf8d6a162018-08-21 10:47:55 -07007058aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
7059 if (!cpi) return NULL;
7060
7061 uint8_t header_buf[512] = { 0 };
7062 const uint32_t sequence_header_size =
Yaowu Xu797674b2019-05-01 17:38:11 -07007063 av1_write_sequence_header_obu(cpi, &header_buf[0]);
Tom Fineganf8d6a162018-08-21 10:47:55 -07007064 assert(sequence_header_size <= sizeof(header_buf));
7065 if (sequence_header_size == 0) return NULL;
7066
7067 const size_t obu_header_size = 1;
7068 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
7069 const size_t payload_offset = obu_header_size + size_field_size;
7070
7071 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
7072 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
7073
Hui Su4fd11762019-03-26 16:05:07 -07007074 if (av1_write_obu_header(cpi, OBU_SEQUENCE_HEADER, 0, &header_buf[0]) !=
Tom Fineganf8d6a162018-08-21 10:47:55 -07007075 obu_header_size) {
7076 return NULL;
7077 }
7078
7079 size_t coded_size_field_size = 0;
7080 if (aom_uleb_encode(sequence_header_size, size_field_size,
7081 &header_buf[obu_header_size],
7082 &coded_size_field_size) != 0) {
7083 return NULL;
7084 }
7085 assert(coded_size_field_size == size_field_size);
7086
7087 aom_fixed_buf_t *global_headers =
7088 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
7089 if (!global_headers) return NULL;
7090
7091 const size_t global_header_buf_size =
7092 obu_header_size + size_field_size + sequence_header_size;
7093
7094 global_headers->buf = malloc(global_header_buf_size);
7095 if (!global_headers->buf) {
7096 free(global_headers);
7097 return NULL;
7098 }
7099
7100 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
7101 global_headers->sz = global_header_buf_size;
7102 return global_headers;
7103}