blob: a94467d876acd68a6926c34cea0515ce336ac99c [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>
13#include <math.h>
14#include <stdio.h>
15
Tom Finegan60e653d2018-05-22 11:34:58 -070016#include "config/aom_config.h"
Tom Finegan44702c82018-05-22 13:00:39 -070017#include "config/aom_dsp_rtcd.h"
18#include "config/aom_scale_rtcd.h"
Yaowu Xufa3721d2018-07-30 14:38:49 -070019#include "config/av1_rtcd.h"
20
21#include "aom_dsp/aom_dsp_common.h"
22#include "aom_dsp/aom_filter.h"
23#if CONFIG_DENOISE
24#include "aom_dsp/grain_table.h"
25#include "aom_dsp/noise_util.h"
26#include "aom_dsp/noise_model.h"
27#endif
28#include "aom_dsp/psnr.h"
29#if CONFIG_INTERNAL_STATS
30#include "aom_dsp/ssim.h"
31#endif
32#include "aom_ports/aom_timer.h"
33#include "aom_ports/mem.h"
34#include "aom_ports/system_state.h"
35#include "aom_scale/aom_scale.h"
David Turner1539bb02019-01-24 15:28:13 +000036#if CONFIG_BITSTREAM_DEBUG
Yaowu Xufa3721d2018-07-30 14:38:49 -070037#include "aom_util/debug_util.h"
David Turner1539bb02019-01-24 15:28:13 +000038#endif // CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070039
40#include "av1/common/alloccommon.h"
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010041#include "av1/common/cdef.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070042#include "av1/common/filter.h"
43#include "av1/common/idct.h"
44#include "av1/common/reconinter.h"
45#include "av1/common/reconintra.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070046#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070047#include "av1/common/tile_common.h"
48
Ravi Chaudharyc5e74692018-10-08 16:05:38 +053049#include "av1/encoder/av1_multi_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070050#include "av1/encoder/aq_complexity.h"
51#include "av1/encoder/aq_cyclicrefresh.h"
52#include "av1/encoder/aq_variance.h"
53#include "av1/encoder/bitstream.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070054#include "av1/encoder/context_tree.h"
55#include "av1/encoder/encodeframe.h"
56#include "av1/encoder/encodemv.h"
David Turner056f7cd2019-01-07 17:48:13 +000057#include "av1/encoder/encode_strategy.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070058#include "av1/encoder/encoder.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070059#include "av1/encoder/encodetxb.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070060#include "av1/encoder/ethread.h"
61#include "av1/encoder/firstpass.h"
Yaowu Xufa3721d2018-07-30 14:38:49 -070062#include "av1/encoder/grain_test_vectors.h"
RogerZhoucc5d35d2017-08-07 22:20:15 -070063#include "av1/encoder/hash_motion.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070064#include "av1/encoder/mbgraph.h"
David Turner0fa8c492019-02-06 16:38:13 +000065#include "av1/encoder/pass2_strategy.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070066#include "av1/encoder/picklpf.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070067#include "av1/encoder/pickrst.h"
Debargha Mukherjee7166f222017-09-05 21:32:42 -070068#include "av1/encoder/random.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070069#include "av1/encoder/ratectrl.h"
70#include "av1/encoder/rd.h"
Debargha Mukherjeedf713102018-10-02 12:33:32 -070071#include "av1/encoder/rdopt.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070072#include "av1/encoder/segmentation.h"
73#include "av1/encoder/speed_features.h"
Debargha Mukherjee347c64d2019-05-08 13:53:46 -070074#include "av1/encoder/tpl_model.h"
Yue Chen7cae98f2018-08-24 10:43:16 -070075#include "av1/encoder/reconinter_enc.h"
kyslov7b9d0d62018-12-21 11:12:26 -080076#include "av1/encoder/var_based_part.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070077
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000078#define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000079
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080080#if CONFIG_ENTROPY_STATS
81FRAME_COUNTS aggregate_fc;
82#endif // CONFIG_ENTROPY_STATS
83
Yaowu Xuc27fc142016-08-22 16:08:15 -070084#define AM_SEGMENT_ID_INACTIVE 7
85#define AM_SEGMENT_ID_ACTIVE 0
86
Debargha Mukherjeecd14cc12019-09-06 09:55:52 -070087// Q threshold for high precision mv.
88#define HIGH_PRECISION_MV_QTHRESH 128
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -070089
Yaowu Xuc27fc142016-08-22 16:08:15 -070090// #define OUTPUT_YUV_REC
Yaowu Xuc27fc142016-08-22 16:08:15 -070091#ifdef OUTPUT_YUV_SKINMAP
92FILE *yuv_skinmap_file = NULL;
93#endif
94#ifdef OUTPUT_YUV_REC
95FILE *yuv_rec_file;
96#define FILE_NAME_LEN 100
97#endif
98
Yunqing Wangdb70bf42019-08-19 09:28:11 -070099const int default_tx_type_probs[FRAME_UPDATE_TYPES][TX_SIZES_ALL][TX_TYPES] = {
100 { { 221, 189, 214, 292, 0, 0, 0, 0, 0, 2, 38, 68, 0, 0, 0, 0 },
101 { 262, 203, 216, 239, 0, 0, 0, 0, 0, 1, 37, 66, 0, 0, 0, 0 },
102 { 315, 231, 239, 226, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0 },
103 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
104 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
105 { 222, 188, 214, 287, 0, 0, 0, 0, 0, 2, 50, 61, 0, 0, 0, 0 },
106 { 256, 182, 205, 282, 0, 0, 0, 0, 0, 2, 21, 76, 0, 0, 0, 0 },
107 { 281, 214, 217, 222, 0, 0, 0, 0, 0, 1, 48, 41, 0, 0, 0, 0 },
108 { 263, 194, 225, 225, 0, 0, 0, 0, 0, 2, 15, 100, 0, 0, 0, 0 },
109 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
110 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 { 170, 192, 242, 293, 0, 0, 0, 0, 0, 1, 68, 58, 0, 0, 0, 0 },
114 { 199, 210, 213, 291, 0, 0, 0, 0, 0, 1, 14, 96, 0, 0, 0, 0 },
115 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
116 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 { { 106, 69, 107, 278, 9, 15, 20, 45, 49, 23, 23, 88, 36, 74, 25, 57 },
120 { 105, 72, 81, 98, 45, 49, 47, 50, 56, 72, 30, 81, 33, 95, 27, 83 },
121 { 211, 105, 109, 120, 57, 62, 43, 49, 52, 58, 42, 116, 0, 0, 0, 0 },
122 { 1008, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0 },
123 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
124 { 131, 57, 98, 172, 19, 40, 37, 64, 69, 22, 41, 52, 51, 77, 35, 59 },
125 { 176, 83, 93, 202, 22, 24, 28, 47, 50, 16, 12, 93, 26, 76, 17, 59 },
126 { 136, 72, 89, 95, 46, 59, 47, 56, 61, 68, 35, 51, 32, 82, 26, 69 },
127 { 122, 80, 87, 105, 49, 47, 46, 46, 57, 52, 13, 90, 19, 103, 15, 93 },
128 { 1009, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0 },
129 { 1011, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0 },
130 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
131 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
132 { 202, 20, 84, 114, 14, 60, 41, 79, 99, 21, 41, 15, 50, 84, 34, 66 },
133 { 196, 44, 23, 72, 30, 22, 28, 57, 67, 13, 4, 165, 15, 148, 9, 131 },
134 { 882, 0, 0, 0, 0, 0, 0, 0, 0, 142, 0, 0, 0, 0, 0, 0 },
135 { 840, 0, 0, 0, 0, 0, 0, 0, 0, 184, 0, 0, 0, 0, 0, 0 },
136 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
137 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
138 { { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
139 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
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 { { 213, 110, 141, 269, 12, 16, 15, 19, 21, 11, 38, 68, 22, 29, 16, 24 },
158 { 216, 119, 128, 143, 38, 41, 26, 30, 31, 30, 42, 70, 23, 36, 19, 32 },
159 { 367, 149, 154, 154, 38, 35, 17, 21, 21, 10, 22, 36, 0, 0, 0, 0 },
160 { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
161 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
162 { 219, 96, 127, 191, 21, 40, 25, 32, 34, 18, 45, 45, 33, 39, 26, 33 },
163 { 296, 99, 122, 198, 23, 21, 19, 24, 25, 13, 20, 64, 23, 32, 18, 27 },
164 { 275, 128, 142, 143, 35, 48, 23, 30, 29, 18, 42, 36, 18, 23, 14, 20 },
165 { 239, 132, 166, 175, 36, 27, 19, 21, 24, 14, 13, 85, 9, 31, 8, 25 },
166 { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
167 { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
168 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
169 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
170 { 309, 25, 79, 59, 25, 80, 34, 53, 61, 25, 49, 23, 43, 64, 36, 59 },
171 { 270, 57, 40, 54, 50, 42, 41, 53, 56, 28, 17, 81, 45, 86, 34, 70 },
172 { 1005, 0, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 0, 0, 0 },
173 { 992, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0 },
174 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
175 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
176 { { 133, 63, 55, 83, 57, 87, 58, 72, 68, 16, 24, 35, 29, 105, 25, 114 },
177 { 131, 75, 74, 60, 71, 77, 65, 66, 73, 33, 21, 79, 20, 83, 18, 78 },
178 { 276, 95, 82, 58, 86, 93, 63, 60, 64, 17, 38, 92, 0, 0, 0, 0 },
179 { 1006, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0 },
180 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
181 { 147, 49, 75, 78, 50, 97, 60, 67, 76, 17, 42, 35, 31, 93, 27, 80 },
182 { 157, 49, 58, 75, 61, 52, 56, 67, 69, 12, 15, 79, 24, 119, 11, 120 },
183 { 178, 69, 83, 77, 69, 85, 72, 77, 77, 20, 35, 40, 25, 48, 23, 46 },
184 { 174, 55, 64, 57, 73, 68, 62, 61, 75, 15, 12, 90, 17, 99, 16, 86 },
185 { 1008, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0 },
186 { 1018, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0 },
187 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
188 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
189 { 266, 31, 63, 64, 21, 52, 39, 54, 63, 30, 52, 31, 48, 89, 46, 75 },
190 { 272, 26, 32, 44, 29, 31, 32, 53, 51, 13, 13, 88, 22, 153, 16, 149 },
191 { 923, 0, 0, 0, 0, 0, 0, 0, 0, 101, 0, 0, 0, 0, 0, 0 },
192 { 969, 0, 0, 0, 0, 0, 0, 0, 0, 55, 0, 0, 0, 0, 0, 0 },
193 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
194 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
195 { { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
196 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
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 { { 158, 92, 125, 298, 12, 15, 20, 29, 31, 12, 29, 67, 34, 44, 23, 35 },
215 { 147, 94, 103, 123, 45, 48, 38, 41, 46, 48, 37, 78, 33, 63, 27, 53 },
216 { 268, 126, 125, 136, 54, 53, 31, 38, 38, 33, 35, 87, 0, 0, 0, 0 },
217 { 1018, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0 },
218 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
219 { 159, 72, 103, 194, 20, 35, 37, 50, 56, 21, 39, 40, 51, 61, 38, 48 },
220 { 259, 86, 95, 188, 32, 20, 25, 34, 37, 13, 12, 85, 25, 53, 17, 43 },
221 { 189, 99, 113, 123, 45, 59, 37, 46, 48, 44, 39, 41, 31, 47, 26, 37 },
222 { 175, 110, 113, 128, 58, 38, 33, 33, 43, 29, 13, 100, 14, 68, 12, 57 },
223 { 1017, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0 },
224 { 1019, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0 },
225 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
226 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
227 { 208, 22, 84, 101, 21, 59, 44, 70, 90, 25, 59, 13, 64, 67, 49, 48 },
228 { 277, 52, 32, 63, 43, 26, 33, 48, 54, 11, 6, 130, 18, 119, 11, 101 },
229 { 963, 0, 0, 0, 0, 0, 0, 0, 0, 61, 0, 0, 0, 0, 0, 0 },
230 { 979, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 0, 0, 0 },
231 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
232 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
233};
234
Yunqing Wang5f74dc22019-10-29 10:35:20 -0700235const int default_obmc_probs[FRAME_UPDATE_TYPES][BLOCK_SIZES_ALL] = {
236 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
237 { 0, 0, 0, 106, 90, 90, 97, 67, 59, 70, 28,
238 30, 38, 16, 16, 16, 0, 0, 44, 50, 26, 25 },
239 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
240 { 0, 0, 0, 98, 93, 97, 68, 82, 85, 33, 30,
241 33, 16, 16, 16, 16, 0, 0, 43, 37, 26, 16 },
242 { 0, 0, 0, 91, 80, 76, 78, 55, 49, 24, 16,
243 16, 16, 16, 16, 16, 0, 0, 29, 45, 16, 38 },
244 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
245 { 0, 0, 0, 103, 89, 89, 89, 62, 63, 76, 34,
246 35, 32, 19, 16, 16, 0, 0, 49, 55, 29, 19 }
247};
248
Yaowu Xuf883b422016-08-30 14:01:10 -0700249static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700250 switch (mode) {
251 case NORMAL:
252 *hr = 1;
253 *hs = 1;
254 break;
255 case FOURFIVE:
256 *hr = 4;
257 *hs = 5;
258 break;
259 case THREEFIVE:
260 *hr = 3;
261 *hs = 5;
262 break;
263 case ONETWO:
264 *hr = 1;
265 *hs = 2;
266 break;
267 default:
268 *hr = 1;
269 *hs = 1;
270 assert(0);
271 break;
272 }
273}
274
275// Mark all inactive blocks as active. Other segmentation features may be set
276// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700277static void suppress_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700278 unsigned char *const seg_map = cpi->segmentation_map;
279 int i;
280 if (cpi->active_map.enabled || cpi->active_map.update)
281 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
282 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
283 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
284}
285
Yaowu Xuf883b422016-08-30 14:01:10 -0700286static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700287 struct segmentation *const seg = &cpi->common.seg;
288 unsigned char *const seg_map = cpi->segmentation_map;
289 const unsigned char *const active_map = cpi->active_map.map;
290 int i;
291
292 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
293
294 if (frame_is_intra_only(&cpi->common)) {
295 cpi->active_map.enabled = 0;
296 cpi->active_map.update = 1;
297 }
298
299 if (cpi->active_map.update) {
300 if (cpi->active_map.enabled) {
301 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
302 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700303 av1_enable_segmentation(seg);
304 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700305 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
306 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
307 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
308 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
309
310 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
311 -MAX_LOOP_FILTER);
312 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
313 -MAX_LOOP_FILTER);
314 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
315 -MAX_LOOP_FILTER);
316 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
317 -MAX_LOOP_FILTER);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700318 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700319 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700320 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
321 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
322 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
323 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700324 if (seg->enabled) {
325 seg->update_data = 1;
326 seg->update_map = 1;
327 }
328 }
329 cpi->active_map.update = 0;
330 }
331}
332
Yaowu Xuf883b422016-08-30 14:01:10 -0700333int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
334 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700335 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
336 unsigned char *const active_map_8x8 = cpi->active_map.map;
337 const int mi_rows = cpi->common.mi_rows;
338 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700339 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
340 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700341 cpi->active_map.update = 1;
342 if (new_map_16x16) {
343 int r, c;
344 for (r = 0; r < mi_rows; ++r) {
345 for (c = 0; c < mi_cols; ++c) {
346 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700347 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700348 ? AM_SEGMENT_ID_ACTIVE
349 : AM_SEGMENT_ID_INACTIVE;
350 }
351 }
352 cpi->active_map.enabled = 1;
353 } else {
354 cpi->active_map.enabled = 0;
355 }
356 return 0;
357 } else {
358 return -1;
359 }
360}
361
Yaowu Xuf883b422016-08-30 14:01:10 -0700362int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
363 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700364 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
365 new_map_16x16) {
366 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
367 const int mi_rows = cpi->common.mi_rows;
368 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700369 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
370 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
371
Yaowu Xuc27fc142016-08-22 16:08:15 -0700372 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
373 if (cpi->active_map.enabled) {
374 int r, c;
375 for (r = 0; r < mi_rows; ++r) {
376 for (c = 0; c < mi_cols; ++c) {
377 // Cyclic refresh segments are considered active despite not having
378 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700379 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700380 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
381 }
382 }
383 }
384 return 0;
385 } else {
386 return -1;
387 }
388}
389
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800390// Compute the horizontal frequency components' energy in a frame
391// by calculuating the 16x4 Horizontal DCT. This is to be used to
392// decide the superresolution parameters.
Yaowu Xubedbf4f2019-05-01 17:54:36 -0700393static void analyze_hor_freq(const AV1_COMP *cpi, double *energy) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800394 uint64_t freq_energy[16] = { 0 };
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800395 const YV12_BUFFER_CONFIG *buf = cpi->source;
396 const int bd = cpi->td.mb.e_mbd.bd;
397 const int width = buf->y_crop_width;
398 const int height = buf->y_crop_height;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800399 DECLARE_ALIGNED(16, int32_t, coeff[16 * 4]);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800400 int n = 0;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800401 memset(freq_energy, 0, sizeof(freq_energy));
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800402 if (buf->flags & YV12_FLAG_HIGHBITDEPTH) {
403 const int16_t *src16 = (const int16_t *)CONVERT_TO_SHORTPTR(buf->y_buffer);
404 for (int i = 0; i < height - 4; i += 4) {
405 for (int j = 0; j < width - 16; j += 16) {
406 av1_fwd_txfm2d_16x4(src16 + i * buf->y_stride + j, coeff, buf->y_stride,
407 H_DCT, bd);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800408 for (int k = 1; k < 16; ++k) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800409 const uint64_t this_energy =
410 ((int64_t)coeff[k] * coeff[k]) +
411 ((int64_t)coeff[k + 16] * coeff[k + 16]) +
412 ((int64_t)coeff[k + 32] * coeff[k + 32]) +
413 ((int64_t)coeff[k + 48] * coeff[k + 48]);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800414 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2 + 2 * (bd - 8));
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800415 }
416 n++;
417 }
418 }
419 } else {
Debargha Mukherjeeac28c722018-11-14 22:09:46 -0800420 assert(bd == 8);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800421 DECLARE_ALIGNED(16, int16_t, src16[16 * 4]);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800422 for (int i = 0; i < height - 4; i += 4) {
423 for (int j = 0; j < width - 16; j += 16) {
424 for (int ii = 0; ii < 4; ++ii)
425 for (int jj = 0; jj < 16; ++jj)
426 src16[ii * 16 + jj] =
427 buf->y_buffer[(i + ii) * buf->y_stride + (j + jj)];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800428 av1_fwd_txfm2d_16x4(src16, coeff, 16, H_DCT, bd);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800429 for (int k = 1; k < 16; ++k) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800430 const uint64_t this_energy =
431 ((int64_t)coeff[k] * coeff[k]) +
432 ((int64_t)coeff[k + 16] * coeff[k + 16]) +
433 ((int64_t)coeff[k + 32] * coeff[k + 32]) +
434 ((int64_t)coeff[k + 48] * coeff[k + 48]);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800435 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800436 }
437 n++;
438 }
439 }
440 }
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800441 if (n) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800442 for (int k = 1; k < 16; ++k) energy[k] = (double)freq_energy[k] / n;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800443 // Convert to cumulative energy
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800444 for (int k = 14; k > 0; --k) energy[k] += energy[k + 1];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800445 } else {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800446 for (int k = 1; k < 16; ++k) energy[k] = 1e+20;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800447 }
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800448}
449
Yaowu Xu45295c32018-03-29 12:06:10 -0700450static void set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv,
451 int cur_frame_force_integer_mv) {
Debargha Mukherjee8d273412019-09-11 10:36:50 -0700452 MACROBLOCK *const x = &cpi->td.mb;
Hui Su50361152018-03-02 11:01:42 -0800453 cpi->common.allow_high_precision_mv =
454 allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Rupert Swarbricka84faf22017-12-11 13:56:40 +0000455 const int copy_hp =
456 cpi->common.allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Debargha Mukherjee8d273412019-09-11 10:36:50 -0700457 x->nmvcost[0] = &x->nmv_costs[0][MV_MAX];
458 x->nmvcost[1] = &x->nmv_costs[1][MV_MAX];
459 x->nmvcost_hp[0] = &x->nmv_costs_hp[0][MV_MAX];
460 x->nmvcost_hp[1] = &x->nmv_costs_hp[1][MV_MAX];
461 int *(*src)[2] = copy_hp ? &x->nmvcost_hp : &x->nmvcost;
462 x->mv_cost_stack = *src;
James Zern01a9d702017-08-25 19:09:33 +0000463}
464
Yaowu Xuf883b422016-08-30 14:01:10 -0700465static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Urvang Joshie4530f82018-01-09 11:43:37 -0800466 const AV1_COMMON *const cm = &cpi->common;
James Zernbf3ca362019-10-21 11:33:44 -0700467
Yaowu Xuf883b422016-08-30 14:01:10 -0700468 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700469 return BLOCK_64X64;
Ryan Leic8b6dd62019-10-23 20:01:26 -0700470 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
471 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700472
Yaowu Xuf883b422016-08-30 14:01:10 -0700473 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700474
Ryan Leic8b6dd62019-10-23 20:01:26 -0700475 // TODO(any): Possibly could improve this with a heuristic.
Urvang Joshiaab74432018-06-01 12:06:22 -0700476 // When superres / resize is on, 'cm->width / height' can change between
Hui Su3e3b9342019-04-12 18:27:28 +0000477 // calls, so we don't apply this heuristic there.
478 // Things break if superblock size changes between the first pass and second
479 // pass encoding, which is why this heuristic is not configured as a
480 // speed-feature.
Urvang Joshiaab74432018-06-01 12:06:22 -0700481 if (cpi->oxcf.superres_mode == SUPERRES_NONE &&
Hui Su3e3b9342019-04-12 18:27:28 +0000482 cpi->oxcf.resize_mode == RESIZE_NONE && cpi->oxcf.speed >= 1) {
483 return AOMMIN(cm->width, cm->height) > 480 ? BLOCK_128X128 : BLOCK_64X64;
Urvang Joshie4530f82018-01-09 11:43:37 -0800484 }
485
Yaowu Xuc27fc142016-08-22 16:08:15 -0700486 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700487}
488
Yaowu Xuf883b422016-08-30 14:01:10 -0700489static void setup_frame(AV1_COMP *cpi) {
490 AV1_COMMON *const cm = &cpi->common;
Johannb0ef6ff2018-02-08 14:32:21 -0800491 // Set up entropy context depending on frame type. The decoder mandates
492 // the use of the default context, index 0, for keyframes and inter
493 // frames where the error_resilient_mode or intra_only flag is set. For
494 // other inter-frames the encoder currently uses only two contexts;
495 // context 1 for ALTREF frames and context 0 for the others.
Soo-Chul Han85e8c792018-01-21 01:58:15 -0500496
Sarah Parker50b6d6e2018-04-11 19:21:54 -0700497 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
David Turnera7f133c2019-01-22 14:47:16 +0000498 cpi->ext_use_primary_ref_none) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700499 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700500 }
501
Hui Sueb4b7de2019-04-03 11:00:18 -0700502 if ((cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) ||
503 frame_is_sframe(cm)) {
504 if (!cpi->seq_params_locked) {
505 set_sb_size(&cm->seq_params, select_sb_size(cpi));
506 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700507 } else {
David Turnera21966b2018-12-05 14:48:49 +0000508 const RefCntBuffer *const primary_ref_buf = get_primary_ref_frame_buf(cm);
509 if (primary_ref_buf == NULL) {
David Barkercc615a82018-03-19 14:38:51 +0000510 av1_setup_past_independence(cm);
511 cm->seg.update_map = 1;
512 cm->seg.update_data = 1;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700513 } else {
David Turnera21966b2018-12-05 14:48:49 +0000514 *cm->fc = primary_ref_buf->frame_context;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700515 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700516 }
517
David Turnerbc0993e2019-02-15 14:42:23 +0000518 av1_zero(cm->cur_frame->interp_filter_selected);
David Turnera21966b2018-12-05 14:48:49 +0000519 cm->prev_frame = get_primary_ref_frame_buf(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700520 cpi->vaq_refresh = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700521}
522
chiyotsaia7091f12019-08-09 16:48:27 -0700523static void enc_set_mb_mi(AV1_COMMON *cm, int width, int height) {
524 // Ensure that the decoded width and height are both multiples of
525 // 8 luma pixels (note: this may only be a multiple of 4 chroma pixels if
526 // subsampling is used).
527 // This simplifies the implementation of various experiments,
528 // eg. cdef, which operates on units of 8x8 luma pixels.
529 const int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
530 const int aligned_height = ALIGN_POWER_OF_TWO(height, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700531
chiyotsaia7091f12019-08-09 16:48:27 -0700532 cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
533 cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
534 cm->mi_stride = calc_mi_size(cm->mi_cols);
535
536 cm->mb_cols = (cm->mi_cols + 2) >> 2;
537 cm->mb_rows = (cm->mi_rows + 2) >> 2;
538 cm->MBs = cm->mb_rows * cm->mb_cols;
539
540 const int is_4k_or_larger = AOMMIN(width, height) >= 2160;
541
542 cm->mi_alloc_bsize = is_4k_or_larger ? BLOCK_8X8 : BLOCK_4X4;
543 const int mi_alloc_size_1d = mi_size_wide[cm->mi_alloc_bsize];
544 cm->mi_alloc_rows = (cm->mi_rows + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
545 cm->mi_alloc_cols = (cm->mi_cols + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
546 cm->mi_alloc_stride =
547 (cm->mi_stride + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
548
549 assert(mi_size_wide[cm->mi_alloc_bsize] == mi_size_high[cm->mi_alloc_bsize]);
550
551#if CONFIG_LPF_MASK
chiyotsaia13c19f2019-08-15 10:12:57 -0700552 av1_alloc_loop_filter_mask(cm);
chiyotsaia7091f12019-08-09 16:48:27 -0700553#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700554}
555
chiyotsaia7091f12019-08-09 16:48:27 -0700556static void enc_setup_mi(AV1_COMMON *cm) {
557 const int mi_grid_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
558 memset(cm->mi, 0, cm->mi_alloc_size * sizeof(*cm->mi));
chiyotsaia7091f12019-08-09 16:48:27 -0700559 memset(cm->mi_grid_base, 0, mi_grid_size * sizeof(*cm->mi_grid_base));
Hui Su52b7ddc2019-10-10 16:27:16 -0700560 memset(cm->tx_type_map, 0, mi_grid_size * sizeof(*cm->tx_type_map));
chiyotsaia7091f12019-08-09 16:48:27 -0700561}
562
563static int enc_alloc_mi(AV1_COMMON *cm) {
564 const int mi_grid_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
565 const int alloc_size_1d = mi_size_wide[cm->mi_alloc_bsize];
566 const int alloc_mi_size =
567 cm->mi_alloc_stride * (calc_mi_size(cm->mi_rows) / alloc_size_1d);
568
569 if (cm->mi_alloc_size < alloc_mi_size || cm->mi_grid_size < mi_grid_size) {
570 cm->free_mi(cm);
571
572 cm->mi = aom_calloc(alloc_mi_size, sizeof(*cm->mi));
573 if (!cm->mi) return 1;
574 cm->mi_alloc_size = alloc_mi_size;
575
576 cm->mi_grid_base =
577 (MB_MODE_INFO **)aom_calloc(mi_grid_size, sizeof(MB_MODE_INFO *));
578 if (!cm->mi_grid_base) return 1;
579 cm->mi_grid_size = mi_grid_size;
Hui Su52b7ddc2019-10-10 16:27:16 -0700580
581 cm->tx_type_map = aom_calloc(calc_mi_size(cm->mi_rows) * cm->mi_stride,
582 sizeof(*cm->tx_type_map));
583 if (!cm->tx_type_map) return 1;
chiyotsaia7091f12019-08-09 16:48:27 -0700584 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700585
586 return 0;
587}
588
Cheng Chen46f30c72017-09-07 11:13:33 -0700589static void enc_free_mi(AV1_COMMON *cm) {
chiyotsai04ca87d2019-05-24 15:06:19 -0700590 aom_free(cm->mi);
591 cm->mi = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700592 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700593 cm->mi_grid_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700594 cm->mi_alloc_size = 0;
Hui Su52b7ddc2019-10-10 16:27:16 -0700595 aom_free(cm->tx_type_map);
596 cm->tx_type_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700597}
598
Yaowu Xuf883b422016-08-30 14:01:10 -0700599void av1_initialize_enc(void) {
Wan-Teh Chang3cac4542018-06-29 10:21:39 -0700600 av1_rtcd();
601 aom_dsp_rtcd();
602 aom_scale_rtcd();
603 av1_init_intra_predictors();
604 av1_init_me_luts();
605 av1_rc_init_minq_luts();
606 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700607}
608
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700609static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
Remya0cce44c2019-08-16 11:57:24 +0530610 if (cpi->mbmi_ext_frame_base) {
611 aom_free(cpi->mbmi_ext_frame_base);
612 cpi->mbmi_ext_frame_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700613 }
614}
615
616static void alloc_context_buffers_ext(AV1_COMP *cpi) {
617 AV1_COMMON *cm = &cpi->common;
chiyotsaia7091f12019-08-09 16:48:27 -0700618 const int new_ext_mi_size = cm->mi_alloc_rows * cm->mi_alloc_cols;
chiyotsai426c0662019-08-05 16:15:11 -0700619
chiyotsaia7091f12019-08-09 16:48:27 -0700620 if (new_ext_mi_size > cpi->mi_ext_alloc_size) {
621 dealloc_context_buffers_ext(cpi);
Remya0cce44c2019-08-16 11:57:24 +0530622 CHECK_MEM_ERROR(
623 cm, cpi->mbmi_ext_frame_base,
624 aom_calloc(new_ext_mi_size, sizeof(*cpi->mbmi_ext_frame_base)));
chiyotsaia7091f12019-08-09 16:48:27 -0700625 cpi->mi_ext_alloc_size = new_ext_mi_size;
626 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700627}
628
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800629static void reset_film_grain_chroma_params(aom_film_grain_t *pars) {
630 pars->num_cr_points = 0;
631 pars->cr_mult = 0;
632 pars->cr_luma_mult = 0;
633 memset(pars->scaling_points_cr, 0, sizeof(pars->scaling_points_cr));
634 memset(pars->ar_coeffs_cr, 0, sizeof(pars->ar_coeffs_cr));
635 pars->num_cb_points = 0;
636 pars->cb_mult = 0;
637 pars->cb_luma_mult = 0;
Yaowu Xufda7dcb2019-01-16 13:04:33 -0800638 pars->chroma_scaling_from_luma = 0;
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800639 memset(pars->scaling_points_cb, 0, sizeof(pars->scaling_points_cb));
640 memset(pars->ar_coeffs_cb, 0, sizeof(pars->ar_coeffs_cb));
641}
642
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800643static void update_film_grain_parameters(struct AV1_COMP *cpi,
644 const AV1EncoderConfig *oxcf) {
645 AV1_COMMON *const cm = &cpi->common;
646 cpi->oxcf = *oxcf;
647
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700648 if (cpi->film_grain_table) {
649 aom_film_grain_table_free(cpi->film_grain_table);
650 aom_free(cpi->film_grain_table);
651 cpi->film_grain_table = NULL;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700652 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700653
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800654 if (oxcf->film_grain_test_vector) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700655 cm->seq_params.film_grain_params_present = 1;
David Turnerd2a592e2018-11-16 14:59:31 +0000656 if (cm->current_frame.frame_type == KEY_FRAME) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800657 memcpy(&cm->film_grain_params,
658 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
659 sizeof(cm->film_grain_params));
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800660 if (oxcf->monochrome)
661 reset_film_grain_chroma_params(&cm->film_grain_params);
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700662 cm->film_grain_params.bit_depth = cm->seq_params.bit_depth;
663 if (cm->seq_params.color_range == AOM_CR_FULL_RANGE) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800664 cm->film_grain_params.clip_to_restricted_range = 0;
665 }
666 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700667 } else if (oxcf->film_grain_table_filename) {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800668 cm->seq_params.film_grain_params_present = 1;
669
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700670 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
671 memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700672
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700673 aom_film_grain_table_read(cpi->film_grain_table,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700674 oxcf->film_grain_table_filename, &cm->error);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800675 } else {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800676#if CONFIG_DENOISE
677 cm->seq_params.film_grain_params_present = (cpi->oxcf.noise_level > 0);
678#else
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700679 cm->seq_params.film_grain_params_present = 0;
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800680#endif
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800681 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
682 }
683}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800684
Yaowu Xuf883b422016-08-30 14:01:10 -0700685static void dealloc_compressor_data(AV1_COMP *cpi) {
686 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000687 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700688
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700689 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700690
Yaowu Xuf883b422016-08-30 14:01:10 -0700691 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700692 cpi->tile_data = NULL;
693
694 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700695 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700696 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700697
Yaowu Xuf883b422016-08-30 14:01:10 -0700698 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700699 cpi->cyclic_refresh = NULL;
700
Yaowu Xuf883b422016-08-30 14:01:10 -0700701 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700702 cpi->active_map.map = NULL;
703
sdengc23c7f12019-06-11 16:56:50 -0700704 aom_free(cpi->ssim_rdmult_scaling_factors);
705 cpi->ssim_rdmult_scaling_factors = NULL;
706
sdengf46a1062019-08-04 18:43:50 -0700707 aom_free(cpi->tpl_rdmult_scaling_factors);
708 cpi->tpl_rdmult_scaling_factors = NULL;
709
710 aom_free(cpi->tpl_sb_rdmult_scaling_factors);
711 cpi->tpl_sb_rdmult_scaling_factors = NULL;
712
Jingning Hand064cf02017-06-01 10:00:39 -0700713 aom_free(cpi->td.mb.above_pred_buf);
714 cpi->td.mb.above_pred_buf = NULL;
715
716 aom_free(cpi->td.mb.left_pred_buf);
717 cpi->td.mb.left_pred_buf = NULL;
718
719 aom_free(cpi->td.mb.wsrc_buf);
720 cpi->td.mb.wsrc_buf = NULL;
721
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530722 aom_free(cpi->td.mb.inter_modes_info);
723 cpi->td.mb.inter_modes_info = NULL;
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530724
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530725 for (int i = 0; i < 2; i++)
726 for (int j = 0; j < 2; j++) {
727 aom_free(cpi->td.mb.hash_value_buffer[i][j]);
728 cpi->td.mb.hash_value_buffer[i][j] = NULL;
729 }
Jingning Hand064cf02017-06-01 10:00:39 -0700730 aom_free(cpi->td.mb.mask_buf);
731 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700732
Jingning Han6cc1fd32017-10-13 09:05:36 -0700733 aom_free(cm->tpl_mvs);
734 cm->tpl_mvs = NULL;
Jingning Han6cc1fd32017-10-13 09:05:36 -0700735
Remya0cce44c2019-08-16 11:57:24 +0530736 aom_free(cpi->td.mb.mbmi_ext);
737 cpi->td.mb.mbmi_ext = NULL;
738
Yaowu Xuf883b422016-08-30 14:01:10 -0700739 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700740 av1_free_txb_buf(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700741 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700742
Yaowu Xuf883b422016-08-30 14:01:10 -0700743 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuf883b422016-08-30 14:01:10 -0700744 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800745 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuf883b422016-08-30 14:01:10 -0700746 aom_free_frame_buffer(&cpi->scaled_source);
747 aom_free_frame_buffer(&cpi->scaled_last_source);
748 aom_free_frame_buffer(&cpi->alt_ref_buffer);
749 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700750
Yaowu Xuf883b422016-08-30 14:01:10 -0700751 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700752 cpi->tile_tok[0][0] = 0;
753
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530754 aom_free(cpi->tplist[0][0]);
755 cpi->tplist[0][0] = NULL;
756
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000757 av1_free_pc_tree(&cpi->td, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700758
hui sud9a812b2017-07-06 14:34:37 -0700759 aom_free(cpi->td.mb.palette_buffer);
Hui Su38711e72019-06-11 10:49:47 -0700760 av1_release_compound_type_rd_buffers(&cpi->td.mb.comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700761 aom_free(cpi->td.mb.tmp_conv_dst);
762 for (int j = 0; j < 2; ++j) {
763 aom_free(cpi->td.mb.tmp_obmc_bufs[j]);
764 }
765
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700766#if CONFIG_DENOISE
767 if (cpi->denoise_and_model) {
768 aom_denoise_and_model_free(cpi->denoise_and_model);
769 cpi->denoise_and_model = NULL;
770 }
771#endif
772 if (cpi->film_grain_table) {
773 aom_film_grain_table_free(cpi->film_grain_table);
774 cpi->film_grain_table = NULL;
775 }
Hui Suc3a8d372019-05-28 15:52:45 -0700776
777 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
778 aom_free(cpi->level_info[i]);
779 }
Marco Paniconi63971322019-08-15 21:32:05 -0700780
781 if (cpi->use_svc) av1_free_svc_cyclic_refresh(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700782}
783
Yaowu Xuf883b422016-08-30 14:01:10 -0700784static void configure_static_seg_features(AV1_COMP *cpi) {
785 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700786 const RATE_CONTROL *const rc = &cpi->rc;
787 struct segmentation *const seg = &cm->seg;
788
789 int high_q = (int)(rc->avg_q > 48.0);
790 int qi_delta;
791
792 // Disable and clear down for KF
David Turnerd2a592e2018-11-16 14:59:31 +0000793 if (cm->current_frame.frame_type == KEY_FRAME) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700794 // Clear down the global segmentation map
795 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
796 seg->update_map = 0;
797 seg->update_data = 0;
798 cpi->static_mb_pct = 0;
799
800 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700801 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802
803 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700804 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700805 } else if (cpi->refresh_alt_ref_frame) {
806 // If this is an alt ref frame
807 // Clear down the global segmentation map
808 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
809 seg->update_map = 0;
810 seg->update_data = 0;
811 cpi->static_mb_pct = 0;
812
813 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700814 av1_disable_segmentation(seg);
815 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700816
Jerome Jiang6acbfdc2019-05-30 10:05:17 -0700817#if !CONFIG_REALTIME_ONLY
Yaowu Xuc27fc142016-08-22 16:08:15 -0700818 // Scan frames from current to arf frame.
819 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700820 av1_update_mbgraph_stats(cpi);
Jerome Jiang6acbfdc2019-05-30 10:05:17 -0700821#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700822
823 // If segmentation was enabled set those features needed for the
824 // arf itself.
825 if (seg->enabled) {
826 seg->update_map = 1;
827 seg->update_data = 1;
828
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700829 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
830 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700831 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700832 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
833 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
834 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
835 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
836
837 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
838 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
839 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
840 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700841
Yaowu Xuf883b422016-08-30 14:01:10 -0700842 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700843 }
844 } else if (seg->enabled) {
845 // All other frames if segmentation has been enabled
846
847 // First normal frame in a valid gf or alt ref group
848 if (rc->frames_since_golden == 0) {
849 // Set up segment features for normal frames in an arf group
850 if (rc->source_alt_ref_active) {
851 seg->update_map = 0;
852 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700853
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700854 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
855 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700856 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
857 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700858
Cheng Chend8184da2017-09-26 18:15:22 -0700859 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
860 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
861 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
862 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
863
864 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
865 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
866 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
867 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868
869 // Segment coding disabled for compred testing
870 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700871 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
872 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
873 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700874 }
875 } else {
876 // Disable segmentation and clear down features if alt ref
877 // is not active for this group
878
Yaowu Xuf883b422016-08-30 14:01:10 -0700879 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700880
881 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
882
883 seg->update_map = 0;
884 seg->update_data = 0;
885
Yaowu Xuf883b422016-08-30 14:01:10 -0700886 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700887 }
888 } else if (rc->is_src_frame_alt_ref) {
889 // Special case where we are coding over the top of a previous
890 // alt ref frame.
891 // Segment coding disabled for compred testing
892
893 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700894 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
895 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700896
897 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700898 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
899 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
900 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
901 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700902
903 // Skip all MBs if high Q (0,0 mv and skip coeffs)
904 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700905 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
906 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700907 }
908 // Enable data update
909 seg->update_data = 1;
910 } else {
911 // All other frames.
912
913 // No updates.. leave things as they are.
914 seg->update_map = 0;
915 seg->update_data = 0;
916 }
917 }
918}
919
Yaowu Xuf883b422016-08-30 14:01:10 -0700920static void update_reference_segmentation_map(AV1_COMP *cpi) {
921 AV1_COMMON *const cm = &cpi->common;
chiyotsai2202ba02019-07-12 16:09:21 -0700922 MB_MODE_INFO **mi_4x4_ptr = cm->mi_grid_base;
David Turnerb757ce02018-11-12 15:01:28 +0000923 uint8_t *cache_ptr = cm->cur_frame->seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700924 int row, col;
925
926 for (row = 0; row < cm->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -0700927 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700928 uint8_t *cache = cache_ptr;
Yushin Choa7f65922018-04-04 16:06:11 -0700929 for (col = 0; col < cm->mi_cols; col++, mi_4x4++, cache++)
930 cache[0] = mi_4x4[0]->segment_id;
931 mi_4x4_ptr += cm->mi_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700932 cache_ptr += cm->mi_cols;
933 }
934}
935
Yaowu Xuf883b422016-08-30 14:01:10 -0700936static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
937 AV1_COMMON *cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700938 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -0700939 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yunqing Wang1d569be2019-10-09 13:23:12 -0700940 int is_scale = (oxcf->resize_mode || oxcf->superres_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700941
Satish Kumar Suman7a7239b2019-03-13 14:48:14 +0530942 if (!cpi->lookahead) {
Satish Kumar Suman29909962019-01-09 10:31:21 +0530943 cpi->lookahead = av1_lookahead_init(
944 oxcf->width, oxcf->height, seq_params->subsampling_x,
945 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman7a7239b2019-03-13 14:48:14 +0530946 oxcf->lag_in_frames, oxcf->border_in_pixels, is_scale);
947 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700948 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700949 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700950 "Failed to allocate lag buffers");
951
952 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Yunqing Wang1d569be2019-10-09 13:23:12 -0700953 // (yunqing)Here use same border as lookahead buffers.
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700954 if (aom_realloc_frame_buffer(
955 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
956 seq_params->subsampling_x, seq_params->subsampling_y,
Yunqing Wang1d569be2019-10-09 13:23:12 -0700957 seq_params->use_highbitdepth,
958 is_scale ? oxcf->border_in_pixels : AOM_ENC_LOOKAHEAD_BORDER,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700959 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700960 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700961 "Failed to allocate altref buffer");
962}
963
Yaowu Xuf883b422016-08-30 14:01:10 -0700964static void alloc_util_frame_buffers(AV1_COMP *cpi) {
965 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700966 const SequenceHeader *const seq_params = &cm->seq_params;
967 if (aom_realloc_frame_buffer(
968 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
969 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530970 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700971 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700972 "Failed to allocate last frame buffer");
973
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700974 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800975 &cpi->trial_frame_rst, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700976 cm->superres_upscaled_height, seq_params->subsampling_x,
977 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman3b12c002018-12-19 15:27:20 +0530978 AOM_RESTORATION_FRAME_BORDER, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800979 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800980 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700981
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700982 if (aom_realloc_frame_buffer(
983 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
984 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530985 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700986 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700987 "Failed to allocate scaled source buffer");
988
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700989 if (aom_realloc_frame_buffer(
990 &cpi->scaled_last_source, cm->width, cm->height,
991 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530992 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700993 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700994 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700995 "Failed to allocate scaled last source buffer");
996}
997
Cheng Chen46f30c72017-09-07 11:13:33 -0700998static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700999 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001000 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001001
chiyotsaia7091f12019-08-09 16:48:27 -07001002 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
1003 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1004 "Failed to allocate context buffers");
1005 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001006
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +05301007 int mi_rows_aligned_to_sb =
1008 ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
1009 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
1010
Angie Chiangf0fbf9d2017-03-15 15:01:22 -07001011 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -07001012
Yaowu Xuc27fc142016-08-22 16:08:15 -07001013 alloc_context_buffers_ext(cpi);
1014
Yaowu Xuf883b422016-08-30 14:01:10 -07001015 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001016
1017 {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001018 unsigned int tokens =
1019 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001020 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -07001021 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001022 }
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +05301023 aom_free(cpi->tplist[0][0]);
1024
1025 CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
1026 aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
1027 sizeof(*cpi->tplist[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001028
Yaowu Xuf883b422016-08-30 14:01:10 -07001029 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001030}
1031
Yaowu Xuf883b422016-08-30 14:01:10 -07001032void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001033 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001034 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001035}
1036
Hui Suef139e12019-05-20 15:51:22 -07001037double av1_get_compression_ratio(const AV1_COMMON *const cm,
1038 size_t encoded_frame_size) {
1039 const int upscaled_width = cm->superres_upscaled_width;
1040 const int height = cm->height;
1041 const int luma_pic_size = upscaled_width * height;
1042 const SequenceHeader *const seq_params = &cm->seq_params;
1043 const BITSTREAM_PROFILE profile = seq_params->profile;
1044 const int pic_size_profile_factor =
1045 profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
1046 encoded_frame_size =
1047 (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
1048 const size_t uncompressed_frame_size =
1049 (luma_pic_size * pic_size_profile_factor) >> 3;
1050 return uncompressed_frame_size / (double)encoded_frame_size;
1051}
1052
Yunqing Wang75e20e82018-06-16 12:10:48 -07001053static void set_tile_info(AV1_COMP *cpi) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001054 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +02001055 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001056
1057 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001058
1059 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +02001060 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +02001061 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001062 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
1063 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +02001064 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001065 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
1066 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001067 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +02001068 cm->uniform_tile_spacing_flag = 0;
1069 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
1070 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001071 size_sb = cpi->oxcf.tile_widths[j++];
1072 if (j >= cpi->oxcf.tile_width_count) j = 0;
David Barker6cd5a822018-03-05 16:19:28 +00001073 start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +02001074 }
1075 cm->tile_cols = i;
1076 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001077 }
1078 av1_calculate_tile_cols(cm);
1079
1080 // configure tile rows
1081 if (cm->uniform_tile_spacing_flag) {
1082 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
1083 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +02001084 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001085 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
1086 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001087 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +02001088 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
1089 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001090 size_sb = cpi->oxcf.tile_heights[j++];
1091 if (j >= cpi->oxcf.tile_height_count) j = 0;
1092 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +02001093 }
1094 cm->tile_rows = i;
1095 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001096 }
1097 av1_calculate_tile_rows(cm);
1098}
1099
Yaowu Xuf883b422016-08-30 14:01:10 -07001100static void update_frame_size(AV1_COMP *cpi) {
1101 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001102 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
1103
chiyotsaia7091f12019-08-09 16:48:27 -07001104 // We need to reallocate the context buffers here in case we need more mis.
1105 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
1106 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1107 "Failed to allocate context buffers");
1108 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001109 av1_init_context_buffers(cm);
chiyotsaia7091f12019-08-09 16:48:27 -07001110
Luc Trudeau1e84af52017-11-25 15:00:28 -05001111 av1_init_macroblockd(cm, xd, NULL);
chiyotsai426c0662019-08-05 16:15:11 -07001112
chiyotsaia7091f12019-08-09 16:48:27 -07001113 const int ext_mi_size = cm->mi_alloc_rows * cm->mi_alloc_cols;
1114 alloc_context_buffers_ext(cpi);
Remya0cce44c2019-08-16 11:57:24 +05301115 memset(cpi->mbmi_ext_frame_base, 0,
1116 ext_mi_size * sizeof(*cpi->mbmi_ext_frame_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001117 set_tile_info(cpi);
1118}
1119
Yaowu Xuf883b422016-08-30 14:01:10 -07001120static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001121 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -07001122 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
David Turnera21966b2018-12-05 14:48:49 +00001123 cpi->common.remapped_ref_idx[fb_idx] = fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -07001124 cpi->rate_index = 0;
1125 cpi->rate_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001126}
1127
Debargha Mukherjee57498692018-05-11 13:29:31 -07001128static INLINE int does_level_match(int width, int height, double fps,
1129 int lvl_width, int lvl_height,
1130 double lvl_fps, int lvl_dim_mult) {
1131 const int64_t lvl_luma_pels = lvl_width * lvl_height;
1132 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
1133 const int64_t luma_pels = width * height;
1134 const double display_sample_rate = luma_pels * fps;
1135 return luma_pels <= lvl_luma_pels &&
1136 display_sample_rate <= lvl_display_sample_rate &&
1137 width <= lvl_width * lvl_dim_mult &&
1138 height <= lvl_height * lvl_dim_mult;
1139}
1140
Andrey Norkin26495512018-06-20 17:13:11 -07001141static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Andrey Norkinf481d982018-05-15 12:05:31 -07001142 const AV1EncoderConfig *oxcf) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001143 // TODO(any): This is a placeholder function that only addresses dimensions
1144 // and max display sample rates.
1145 // Need to add checks for max bit rate, max decoded luma sample rate, header
1146 // rate, etc. that are not covered by this function.
Hui Su8427ff22019-03-11 10:14:33 -07001147 AV1_LEVEL level = SEQ_LEVEL_MAX;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001148 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
1149 288, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001150 level = SEQ_LEVEL_2_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001151 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1152 704, 396, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001153 level = SEQ_LEVEL_2_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001154 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1155 1088, 612, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001156 level = SEQ_LEVEL_3_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001157 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1158 1376, 774, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001159 level = SEQ_LEVEL_3_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001160 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1161 2048, 1152, 30.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001162 level = SEQ_LEVEL_4_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001163 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1164 2048, 1152, 60.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001165 level = SEQ_LEVEL_4_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001166 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1167 4096, 2176, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001168 level = SEQ_LEVEL_5_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001169 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1170 4096, 2176, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001171 level = SEQ_LEVEL_5_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001172 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1173 4096, 2176, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001174 level = SEQ_LEVEL_5_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001175 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1176 8192, 4352, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001177 level = SEQ_LEVEL_6_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001178 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1179 8192, 4352, 60.0, 2)) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001180 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1181 8192, 4352, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001182 level = SEQ_LEVEL_6_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001183 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1184 16384, 8704, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001185 level = SEQ_LEVEL_7_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001186 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1187 16384, 8704, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001188 level = SEQ_LEVEL_7_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001189 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1190 16384, 8704, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001191 level = SEQ_LEVEL_7_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001192 }
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001193 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su8427ff22019-03-11 10:14:33 -07001194 seq->seq_level_idx[i] = level;
Andrey Norkin26495512018-06-20 17:13:11 -07001195 // Set the maximum parameters for bitrate and buffer size for this profile,
1196 // level, and tier
Yaowu Xu7e450882019-04-30 15:09:18 -07001197 cm->op_params[i].bitrate = av1_max_level_bitrate(
Hui Su8427ff22019-03-11 10:14:33 -07001198 cm->seq_params.profile, seq->seq_level_idx[i], seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001199 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
1200 // check
Andrey Norkin26495512018-06-20 17:13:11 -07001201 if (cm->op_params[i].bitrate == 0)
1202 aom_internal_error(
1203 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
1204 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -07001205 // Buffer size in bits/s is bitrate in bits/s * 1 s
Andrey Norkin26495512018-06-20 17:13:11 -07001206 cm->op_params[i].buffer_size = cm->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001207 }
1208}
1209
Andrey Norkin26495512018-06-20 17:13:11 -07001210static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
Marco Paniconi67142112019-07-24 15:00:31 -07001211 const AV1EncoderConfig *oxcf, int use_svc) {
Yaowu Xu2a9ac432019-08-06 14:21:17 -07001212 seq->still_picture = (oxcf->force_video_mode == 0) && (oxcf->limit == 1);
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001213 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -07001214 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
kyslov94243382019-05-02 15:33:32 -07001215 seq->force_screen_content_tools = (oxcf->mode == REALTIME) ? 0 : 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001216 seq->force_integer_mv = 2;
David Turnerebf96f42018-11-14 16:57:57 +00001217 seq->order_hint_info.enable_order_hint = oxcf->enable_order_hint;
David Turner936235c2018-11-28 13:42:01 +00001218 seq->frame_id_numbers_present_flag =
1219 !(seq->still_picture && seq->reduced_still_picture_hdr) &&
Marco Paniconi67142112019-07-24 15:00:31 -07001220 !oxcf->large_scale_tile && oxcf->error_resilient_mode && !use_svc;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001221 if (seq->still_picture && seq->reduced_still_picture_hdr) {
David Turnerebf96f42018-11-14 16:57:57 +00001222 seq->order_hint_info.enable_order_hint = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001223 seq->force_screen_content_tools = 2;
1224 seq->force_integer_mv = 2;
1225 }
David Turnerebf96f42018-11-14 16:57:57 +00001226 seq->order_hint_info.order_hint_bits_minus_1 =
1227 seq->order_hint_info.enable_order_hint
1228 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
1229 : -1;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001230
David Turner760a2f42018-12-07 15:25:36 +00001231 seq->max_frame_width =
1232 oxcf->forced_max_frame_width ? oxcf->forced_max_frame_width : oxcf->width;
1233 seq->max_frame_height = oxcf->forced_max_frame_height
1234 ? oxcf->forced_max_frame_height
1235 : oxcf->height;
1236 seq->num_bits_width =
1237 (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
1238 seq->num_bits_height =
1239 (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
1240 assert(seq->num_bits_width <= 16);
1241 assert(seq->num_bits_height <= 16);
1242
1243 seq->frame_id_length = FRAME_ID_LENGTH;
1244 seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
1245
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001246 seq->enable_dual_filter = oxcf->enable_dual_filter;
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -08001247 seq->order_hint_info.enable_dist_wtd_comp = oxcf->enable_dist_wtd_comp;
1248 seq->order_hint_info.enable_dist_wtd_comp &=
David Turnerebf96f42018-11-14 16:57:57 +00001249 seq->order_hint_info.enable_order_hint;
1250 seq->order_hint_info.enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1251 seq->order_hint_info.enable_ref_frame_mvs &=
1252 seq->order_hint_info.enable_order_hint;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001253 seq->enable_superres = oxcf->enable_superres;
1254 seq->enable_cdef = oxcf->enable_cdef;
1255 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -07001256 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -08001257 seq->enable_interintra_compound = oxcf->enable_interintra_comp;
1258 seq->enable_masked_compound = oxcf->enable_masked_comp;
Debargha Mukherjee03c43ba2018-12-14 13:08:08 -08001259 seq->enable_intra_edge_filter = oxcf->enable_intra_edge_filter;
Yue Chen8f9ca582018-12-12 15:11:47 -08001260 seq->enable_filter_intra = oxcf->enable_filter_intra;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001261
Andrey Norkin26495512018-06-20 17:13:11 -07001262 set_bitstream_level_tier(seq, cm, oxcf);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001263
1264 if (seq->operating_points_cnt_minus_1 == 0) {
1265 seq->operating_point_idc[0] = 0;
1266 } else {
Marco Paniconi6da983b2019-09-16 20:12:43 -07001267 // Set operating_point_idc[] such that the i=0 point corresponds to the
1268 // highest quality operating point (all layers), and subsequent
1269 // operarting points (i > 0) are lower quality corresponding to
1270 // skip decoding enhancement layers (temporal first).
1271 int i = 0;
1272 assert(seq->operating_points_cnt_minus_1 ==
1273 (int)(cm->number_spatial_layers * cm->number_temporal_layers - 1));
1274 for (unsigned int sl = 0; sl < cm->number_spatial_layers; sl++) {
1275 for (unsigned int tl = 0; tl < cm->number_temporal_layers; tl++) {
1276 seq->operating_point_idc[i] =
1277 (~(~0u << (cm->number_spatial_layers - sl)) << 8) |
1278 ~(~0u << (cm->number_temporal_layers - tl));
1279 i++;
1280 }
1281 }
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001282 }
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001283}
1284
Yaowu Xuf883b422016-08-30 14:01:10 -07001285static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1286 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001287
1288 cpi->oxcf = *oxcf;
1289 cpi->framerate = oxcf->init_framerate;
1290
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001291 cm->seq_params.profile = oxcf->profile;
1292 cm->seq_params.bit_depth = oxcf->bit_depth;
1293 cm->seq_params.use_highbitdepth = oxcf->use_highbitdepth;
1294 cm->seq_params.color_primaries = oxcf->color_primaries;
1295 cm->seq_params.transfer_characteristics = oxcf->transfer_characteristics;
1296 cm->seq_params.matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001297 cm->seq_params.monochrome = oxcf->monochrome;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001298 cm->seq_params.chroma_sample_position = oxcf->chroma_sample_position;
1299 cm->seq_params.color_range = oxcf->color_range;
Andrey Norkin28e9ce22018-01-08 10:11:21 -08001300 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08001301 cm->timing_info.num_units_in_display_tick =
1302 oxcf->timing_info.num_units_in_display_tick;
1303 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
1304 cm->timing_info.equal_picture_interval =
1305 oxcf->timing_info.equal_picture_interval;
1306 cm->timing_info.num_ticks_per_picture =
1307 oxcf->timing_info.num_ticks_per_picture;
1308
Andrey Norkin26495512018-06-20 17:13:11 -07001309 cm->seq_params.display_model_info_present_flag =
1310 oxcf->display_model_info_present_flag;
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001311 cm->seq_params.decoder_model_info_present_flag =
1312 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08001313 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001314 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08001315 cm->buffer_model.num_units_in_decoding_tick =
1316 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07001317 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07001318 av1_set_aom_dec_model_info(&cm->buffer_model);
1319 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07001320 } else if (cm->timing_info_present &&
1321 cm->timing_info.equal_picture_interval &&
1322 !cm->seq_params.decoder_model_info_present_flag) {
1323 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07001324 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001325 } else {
1326 cm->op_params[0].initial_display_delay =
1327 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08001328 }
Andrey Norkinc7511de2018-06-22 12:31:06 -07001329
Tom Fineganf8d6a162018-08-21 10:47:55 -07001330 if (cm->seq_params.monochrome) {
1331 cm->seq_params.subsampling_x = 1;
1332 cm->seq_params.subsampling_y = 1;
1333 } else if (cm->seq_params.color_primaries == AOM_CICP_CP_BT_709 &&
1334 cm->seq_params.transfer_characteristics == AOM_CICP_TC_SRGB &&
1335 cm->seq_params.matrix_coefficients == AOM_CICP_MC_IDENTITY) {
1336 cm->seq_params.subsampling_x = 0;
1337 cm->seq_params.subsampling_y = 0;
1338 } else {
1339 if (cm->seq_params.profile == 0) {
1340 cm->seq_params.subsampling_x = 1;
1341 cm->seq_params.subsampling_y = 1;
1342 } else if (cm->seq_params.profile == 1) {
1343 cm->seq_params.subsampling_x = 0;
1344 cm->seq_params.subsampling_y = 0;
1345 } else {
1346 if (cm->seq_params.bit_depth == AOM_BITS_12) {
1347 cm->seq_params.subsampling_x = oxcf->chroma_subsampling_x;
1348 cm->seq_params.subsampling_y = oxcf->chroma_subsampling_y;
1349 } else {
1350 cm->seq_params.subsampling_x = 1;
1351 cm->seq_params.subsampling_y = 0;
1352 }
1353 }
Tom Finegan02b2a842018-08-24 13:50:00 -07001354 }
1355
Yaowu Xuc27fc142016-08-22 16:08:15 -07001356 cm->width = oxcf->width;
1357 cm->height = oxcf->height;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001358 set_sb_size(&cm->seq_params,
1359 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -07001360 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -07001361
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08001362 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001363
1364 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -07001365 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001366
Marco Paniconi882c5ad2019-09-30 21:32:29 -07001367 // Set init SVC parameters.
Marco Paniconi67142112019-07-24 15:00:31 -07001368 cpi->use_svc = 0;
Marco Paniconid8574e32019-08-04 21:30:12 -07001369 cpi->svc.external_ref_frame_config = 0;
1370 cpi->svc.non_reference_frame = 0;
Marco Paniconi67142112019-07-24 15:00:31 -07001371 cm->number_spatial_layers = 1;
1372 cm->number_temporal_layers = 1;
Marco Paniconi882c5ad2019-09-30 21:32:29 -07001373 cm->spatial_layer_id = 0;
1374 cm->temporal_layer_id = 0;
Marco Paniconi67142112019-07-24 15:00:31 -07001375
Yaowu Xuc27fc142016-08-22 16:08:15 -07001376 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001377 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001378
1379 cpi->static_mb_pct = 0;
1380 cpi->ref_frame_flags = 0;
1381
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001382 // Reset resize pending flags
1383 cpi->resize_pending_width = 0;
1384 cpi->resize_pending_height = 0;
1385
Yaowu Xuc27fc142016-08-22 16:08:15 -07001386 init_buffer_indices(cpi);
1387}
1388
1389static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001390 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001391 const int64_t bandwidth = oxcf->target_bandwidth;
1392 const int64_t starting = oxcf->starting_buffer_level_ms;
1393 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1394 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1395
1396 rc->starting_buffer_level = starting * bandwidth / 1000;
1397 rc->optimal_buffer_level =
1398 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1399 rc->maximum_buffer_size =
1400 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1401}
1402
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001403#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1404 cpi->fn_ptr[BT].sdf = SDF; \
1405 cpi->fn_ptr[BT].sdaf = SDAF; \
1406 cpi->fn_ptr[BT].vf = VF; \
1407 cpi->fn_ptr[BT].svf = SVF; \
1408 cpi->fn_ptr[BT].svaf = SVAF; \
1409 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1410 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001411 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001412
1413#define MAKE_BFP_SAD_WRAPPER(fnname) \
1414 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1415 int source_stride, \
1416 const uint8_t *ref_ptr, int ref_stride) { \
1417 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1418 } \
1419 static unsigned int fnname##_bits10( \
1420 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1421 int ref_stride) { \
1422 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1423 } \
1424 static unsigned int fnname##_bits12( \
1425 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1426 int ref_stride) { \
1427 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1428 }
1429
1430#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1431 static unsigned int fnname##_bits8( \
1432 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1433 int ref_stride, const uint8_t *second_pred) { \
1434 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1435 } \
1436 static unsigned int fnname##_bits10( \
1437 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1438 int ref_stride, const uint8_t *second_pred) { \
1439 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1440 2; \
1441 } \
1442 static unsigned int fnname##_bits12( \
1443 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1444 int ref_stride, const uint8_t *second_pred) { \
1445 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1446 4; \
1447 }
1448
Yaowu Xuc27fc142016-08-22 16:08:15 -07001449#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1450 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1451 const uint8_t *const ref_ptr[], int ref_stride, \
1452 unsigned int *sad_array) { \
1453 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1454 } \
1455 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1456 const uint8_t *const ref_ptr[], int ref_stride, \
1457 unsigned int *sad_array) { \
1458 int i; \
1459 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1460 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1461 } \
1462 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1463 const uint8_t *const ref_ptr[], int ref_stride, \
1464 unsigned int *sad_array) { \
1465 int i; \
1466 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1467 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1468 }
1469
Cheng Chenbf3d4962017-11-01 14:48:52 -07001470#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1471 static unsigned int fnname##_bits8( \
1472 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1473 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001474 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001475 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1476 jcp_param); \
1477 } \
1478 static unsigned int fnname##_bits10( \
1479 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1480 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001481 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001482 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1483 jcp_param) >> \
1484 2; \
1485 } \
1486 static unsigned int fnname##_bits12( \
1487 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1488 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001489 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001490 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1491 jcp_param) >> \
1492 4; \
1493 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001494
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001495#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001496MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1497MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001498MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1499MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1500MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1501MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1502MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1503MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1504MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001505MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1506MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1507MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1508MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1509MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1510MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1511MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1512MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1513MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1514MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1515MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1516MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1517MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1518MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001519MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1520MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1521MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001522MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1523MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1524MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001525MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1526MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1527MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001528MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1529MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1530MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001531MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1532MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1533MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001534MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1535MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1536MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001537MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1538MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1539MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001540MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1541MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1542MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001543MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001544
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001545MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1546MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1547MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1548MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1549MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1550MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1551MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1552MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1553MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1554MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1555MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1556MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001557MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1558MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1559MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1560MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1561MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1562MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001563
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001564MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x128_avg)
1565MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x64_avg)
1566MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x128_avg)
1567MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x16_avg)
1568MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x32_avg)
1569MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x32_avg)
1570MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x64_avg)
1571MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x32_avg)
1572MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x64_avg)
1573MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x16_avg)
1574MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x8_avg)
1575MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x16_avg)
1576MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x8_avg)
1577MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x4_avg)
1578MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x8_avg)
1579MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x4_avg)
1580MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x16_avg)
1581MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x4_avg)
1582MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x32_avg)
1583MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x8_avg)
1584MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x64_avg)
1585MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x16_avg)
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001586#endif // CONFIG_AV1_HIGHBITDEPTH
Cheng Chenbf3d4962017-11-01 14:48:52 -07001587
David Barker0f3c94e2017-05-16 15:21:50 +01001588#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001589 cpi->fn_ptr[BT].msdf = MCSDF; \
1590 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001591
David Barkerc155e012017-05-11 13:54:54 +01001592#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1593 static unsigned int fnname##_bits8( \
1594 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1595 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1596 int m_stride, int invert_mask) { \
1597 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1598 second_pred_ptr, m, m_stride, invert_mask); \
1599 } \
1600 static unsigned int fnname##_bits10( \
1601 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1602 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1603 int m_stride, int invert_mask) { \
1604 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1605 second_pred_ptr, m, m_stride, invert_mask) >> \
1606 2; \
1607 } \
1608 static unsigned int fnname##_bits12( \
1609 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1610 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1611 int m_stride, int invert_mask) { \
1612 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1613 second_pred_ptr, m, m_stride, invert_mask) >> \
1614 4; \
1615 }
1616
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001617#if CONFIG_AV1_HIGHBITDEPTH
David Barkerf19f35f2017-05-22 16:33:22 +01001618MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1619MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1620MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001621MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1622MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1623MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1624MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1625MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1626MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1627MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1628MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1629MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1630MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1631MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1632MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1633MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001634MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1635MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1636MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1637MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001638MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1639MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001640#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001641
Yaowu Xuc27fc142016-08-22 16:08:15 -07001642#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1643 cpi->fn_ptr[BT].osdf = OSDF; \
1644 cpi->fn_ptr[BT].ovf = OVF; \
1645 cpi->fn_ptr[BT].osvf = OSVF;
1646
1647#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1648 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1649 const int32_t *wsrc, \
1650 const int32_t *msk) { \
1651 return fnname(ref, ref_stride, wsrc, msk); \
1652 } \
1653 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1654 const int32_t *wsrc, \
1655 const int32_t *msk) { \
1656 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1657 } \
1658 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1659 const int32_t *wsrc, \
1660 const int32_t *msk) { \
1661 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1662 }
1663
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001664#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001665MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1666MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1667MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001668MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1669MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1670MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1671MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1672MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1673MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1674MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1675MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1676MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1677MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1678MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1679MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1680MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001681MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1682MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1683MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1684MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001685MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1686MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001687
Yaowu Xuf883b422016-08-30 14:01:10 -07001688static void highbd_set_var_fns(AV1_COMP *const cpi) {
1689 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001690 if (cm->seq_params.use_highbitdepth) {
1691 switch (cm->seq_params.bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001692 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001693 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1694 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1695 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001696 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001697 aom_highbd_sad64x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001698 aom_highbd_dist_wtd_sad64x16_avg_bits8,
1699 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001700
1701 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1702 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1703 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001704 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001705 aom_highbd_sad16x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001706 aom_highbd_dist_wtd_sad16x64_avg_bits8,
1707 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001708
1709 HIGHBD_BFP(
1710 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1711 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001712 aom_highbd_8_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001713 aom_highbd_sad32x8x4d_bits8, aom_highbd_dist_wtd_sad32x8_avg_bits8,
1714 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001715
1716 HIGHBD_BFP(
1717 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1718 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001719 aom_highbd_8_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001720 aom_highbd_sad8x32x4d_bits8, aom_highbd_dist_wtd_sad8x32_avg_bits8,
1721 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001722
1723 HIGHBD_BFP(
1724 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1725 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001726 aom_highbd_8_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001727 aom_highbd_sad16x4x4d_bits8, aom_highbd_dist_wtd_sad16x4_avg_bits8,
1728 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001729
1730 HIGHBD_BFP(
1731 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1732 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001733 aom_highbd_8_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001734 aom_highbd_sad4x16x4d_bits8, aom_highbd_dist_wtd_sad4x16_avg_bits8,
1735 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001736
1737 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1738 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1739 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001740 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001741 aom_highbd_sad32x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001742 aom_highbd_dist_wtd_sad32x16_avg_bits8,
1743 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001744
1745 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1746 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1747 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001748 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001749 aom_highbd_sad16x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001750 aom_highbd_dist_wtd_sad16x32_avg_bits8,
1751 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001752
1753 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1754 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1755 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001756 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001757 aom_highbd_sad64x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001758 aom_highbd_dist_wtd_sad64x32_avg_bits8,
1759 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001760
1761 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1762 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1763 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001764 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001765 aom_highbd_sad32x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001766 aom_highbd_dist_wtd_sad32x64_avg_bits8,
1767 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001768
1769 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1770 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1771 aom_highbd_8_sub_pixel_variance32x32,
1772 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001773 aom_highbd_sad32x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001774 aom_highbd_dist_wtd_sad32x32_avg_bits8,
1775 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001776
1777 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1778 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1779 aom_highbd_8_sub_pixel_variance64x64,
1780 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001781 aom_highbd_sad64x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001782 aom_highbd_dist_wtd_sad64x64_avg_bits8,
1783 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001784
1785 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1786 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1787 aom_highbd_8_sub_pixel_variance16x16,
1788 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001789 aom_highbd_sad16x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001790 aom_highbd_dist_wtd_sad16x16_avg_bits8,
1791 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001792
1793 HIGHBD_BFP(
1794 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1795 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001796 aom_highbd_8_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001797 aom_highbd_sad16x8x4d_bits8, aom_highbd_dist_wtd_sad16x8_avg_bits8,
1798 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001799
1800 HIGHBD_BFP(
1801 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1802 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001803 aom_highbd_8_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001804 aom_highbd_sad8x16x4d_bits8, aom_highbd_dist_wtd_sad8x16_avg_bits8,
1805 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001806
Cheng Chenbf3d4962017-11-01 14:48:52 -07001807 HIGHBD_BFP(
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001808 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1809 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1810 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x4d_bits8,
1811 aom_highbd_dist_wtd_sad8x8_avg_bits8,
1812 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x8)
1813
1814 HIGHBD_BFP(
1815 BLOCK_8X4, aom_highbd_sad8x4_bits8, aom_highbd_sad8x4_avg_bits8,
1816 aom_highbd_8_variance8x4, aom_highbd_8_sub_pixel_variance8x4,
1817 aom_highbd_8_sub_pixel_avg_variance8x4, aom_highbd_sad8x4x4d_bits8,
1818 aom_highbd_dist_wtd_sad8x4_avg_bits8,
1819 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x4)
1820
1821 HIGHBD_BFP(
1822 BLOCK_4X8, aom_highbd_sad4x8_bits8, aom_highbd_sad4x8_avg_bits8,
1823 aom_highbd_8_variance4x8, aom_highbd_8_sub_pixel_variance4x8,
1824 aom_highbd_8_sub_pixel_avg_variance4x8, aom_highbd_sad4x8x4d_bits8,
1825 aom_highbd_dist_wtd_sad4x8_avg_bits8,
1826 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x8)
1827
1828 HIGHBD_BFP(
1829 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1830 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1831 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x4d_bits8,
1832 aom_highbd_dist_wtd_sad4x4_avg_bits8,
1833 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x4)
1834
1835 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1836 aom_highbd_sad128x128_avg_bits8,
1837 aom_highbd_8_variance128x128,
1838 aom_highbd_8_sub_pixel_variance128x128,
1839 aom_highbd_8_sub_pixel_avg_variance128x128,
1840 aom_highbd_sad128x128x4d_bits8,
1841 aom_highbd_dist_wtd_sad128x128_avg_bits8,
1842 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001843
1844 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1845 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1846 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001847 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001848 aom_highbd_sad128x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001849 aom_highbd_dist_wtd_sad128x64_avg_bits8,
1850 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001851
1852 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1853 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1854 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001855 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001856 aom_highbd_sad64x128x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001857 aom_highbd_dist_wtd_sad64x128_avg_bits8,
1858 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001859
David Barkerf19f35f2017-05-22 16:33:22 +01001860 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1861 aom_highbd_8_masked_sub_pixel_variance128x128)
1862 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1863 aom_highbd_8_masked_sub_pixel_variance128x64)
1864 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1865 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001866 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1867 aom_highbd_8_masked_sub_pixel_variance64x64)
1868 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1869 aom_highbd_8_masked_sub_pixel_variance64x32)
1870 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1871 aom_highbd_8_masked_sub_pixel_variance32x64)
1872 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1873 aom_highbd_8_masked_sub_pixel_variance32x32)
1874 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1875 aom_highbd_8_masked_sub_pixel_variance32x16)
1876 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1877 aom_highbd_8_masked_sub_pixel_variance16x32)
1878 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1879 aom_highbd_8_masked_sub_pixel_variance16x16)
1880 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1881 aom_highbd_8_masked_sub_pixel_variance8x16)
1882 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1883 aom_highbd_8_masked_sub_pixel_variance16x8)
1884 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1885 aom_highbd_8_masked_sub_pixel_variance8x8)
1886 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1887 aom_highbd_8_masked_sub_pixel_variance4x8)
1888 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1889 aom_highbd_8_masked_sub_pixel_variance8x4)
1890 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1891 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001892 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1893 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001894 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1895 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001896 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1897 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001898 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1899 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001900 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1901 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001902 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1903 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001904 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1905 aom_highbd_obmc_variance128x128,
1906 aom_highbd_obmc_sub_pixel_variance128x128)
1907 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1908 aom_highbd_obmc_variance128x64,
1909 aom_highbd_obmc_sub_pixel_variance128x64)
1910 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1911 aom_highbd_obmc_variance64x128,
1912 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001913 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1914 aom_highbd_obmc_variance64x64,
1915 aom_highbd_obmc_sub_pixel_variance64x64)
1916 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1917 aom_highbd_obmc_variance64x32,
1918 aom_highbd_obmc_sub_pixel_variance64x32)
1919 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1920 aom_highbd_obmc_variance32x64,
1921 aom_highbd_obmc_sub_pixel_variance32x64)
1922 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1923 aom_highbd_obmc_variance32x32,
1924 aom_highbd_obmc_sub_pixel_variance32x32)
1925 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1926 aom_highbd_obmc_variance32x16,
1927 aom_highbd_obmc_sub_pixel_variance32x16)
1928 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1929 aom_highbd_obmc_variance16x32,
1930 aom_highbd_obmc_sub_pixel_variance16x32)
1931 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1932 aom_highbd_obmc_variance16x16,
1933 aom_highbd_obmc_sub_pixel_variance16x16)
1934 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1935 aom_highbd_obmc_variance8x16,
1936 aom_highbd_obmc_sub_pixel_variance8x16)
1937 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1938 aom_highbd_obmc_variance16x8,
1939 aom_highbd_obmc_sub_pixel_variance16x8)
1940 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1941 aom_highbd_obmc_variance8x8,
1942 aom_highbd_obmc_sub_pixel_variance8x8)
1943 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1944 aom_highbd_obmc_variance4x8,
1945 aom_highbd_obmc_sub_pixel_variance4x8)
1946 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1947 aom_highbd_obmc_variance8x4,
1948 aom_highbd_obmc_sub_pixel_variance8x4)
1949 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1950 aom_highbd_obmc_variance4x4,
1951 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001952 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1953 aom_highbd_obmc_variance64x16,
1954 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001955 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1956 aom_highbd_obmc_variance16x64,
1957 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001958 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1959 aom_highbd_obmc_variance32x8,
1960 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001961 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1962 aom_highbd_obmc_variance8x32,
1963 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001964 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1965 aom_highbd_obmc_variance16x4,
1966 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001967 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1968 aom_highbd_obmc_variance4x16,
1969 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001970 break;
1971
Yaowu Xuf883b422016-08-30 14:01:10 -07001972 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001973 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1974 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1975 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001976 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001977 aom_highbd_sad64x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001978 aom_highbd_dist_wtd_sad64x16_avg_bits10,
1979 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001980
1981 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1982 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1983 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001984 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001985 aom_highbd_sad16x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001986 aom_highbd_dist_wtd_sad16x64_avg_bits10,
1987 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001988
1989 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1990 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1991 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001992 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001993 aom_highbd_sad32x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001994 aom_highbd_dist_wtd_sad32x8_avg_bits10,
1995 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001996
1997 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1998 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1999 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002000 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002001 aom_highbd_sad8x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002002 aom_highbd_dist_wtd_sad8x32_avg_bits10,
2003 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002004
2005 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
2006 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
2007 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002008 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002009 aom_highbd_sad16x4x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002010 aom_highbd_dist_wtd_sad16x4_avg_bits10,
2011 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002012
2013 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
2014 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
2015 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002016 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002017 aom_highbd_sad4x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002018 aom_highbd_dist_wtd_sad4x16_avg_bits10,
2019 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002020
2021 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
2022 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
2023 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002024 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002025 aom_highbd_sad32x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002026 aom_highbd_dist_wtd_sad32x16_avg_bits10,
2027 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002028
2029 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
2030 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
2031 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002032 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002033 aom_highbd_sad16x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002034 aom_highbd_dist_wtd_sad16x32_avg_bits10,
2035 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002036
2037 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
2038 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
2039 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002040 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002041 aom_highbd_sad64x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002042 aom_highbd_dist_wtd_sad64x32_avg_bits10,
2043 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002044
2045 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
2046 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
2047 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002048 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002049 aom_highbd_sad32x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002050 aom_highbd_dist_wtd_sad32x64_avg_bits10,
2051 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002052
2053 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
2054 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
2055 aom_highbd_10_sub_pixel_variance32x32,
2056 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002057 aom_highbd_sad32x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002058 aom_highbd_dist_wtd_sad32x32_avg_bits10,
2059 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002060
2061 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
2062 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
2063 aom_highbd_10_sub_pixel_variance64x64,
2064 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002065 aom_highbd_sad64x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002066 aom_highbd_dist_wtd_sad64x64_avg_bits10,
2067 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002068
2069 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
2070 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
2071 aom_highbd_10_sub_pixel_variance16x16,
2072 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002073 aom_highbd_sad16x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002074 aom_highbd_dist_wtd_sad16x16_avg_bits10,
2075 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002076
2077 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
2078 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
2079 aom_highbd_10_sub_pixel_variance16x8,
2080 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002081 aom_highbd_sad16x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002082 aom_highbd_dist_wtd_sad16x8_avg_bits10,
2083 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002084
2085 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
2086 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
2087 aom_highbd_10_sub_pixel_variance8x16,
2088 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002089 aom_highbd_sad8x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002090 aom_highbd_dist_wtd_sad8x16_avg_bits10,
2091 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002092
2093 HIGHBD_BFP(
2094 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
2095 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002096 aom_highbd_10_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002097 aom_highbd_sad8x8x4d_bits10, aom_highbd_dist_wtd_sad8x8_avg_bits10,
2098 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002099
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002100 HIGHBD_BFP(
2101 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
2102 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
2103 aom_highbd_10_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002104 aom_highbd_sad8x4x4d_bits10, aom_highbd_dist_wtd_sad8x4_avg_bits10,
2105 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002106
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002107 HIGHBD_BFP(
2108 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
2109 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
2110 aom_highbd_10_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002111 aom_highbd_sad4x8x4d_bits10, aom_highbd_dist_wtd_sad4x8_avg_bits10,
2112 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002113
2114 HIGHBD_BFP(
2115 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
2116 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002117 aom_highbd_10_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002118 aom_highbd_sad4x4x4d_bits10, aom_highbd_dist_wtd_sad4x4_avg_bits10,
2119 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002120
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002121 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
2122 aom_highbd_sad128x128_avg_bits10,
2123 aom_highbd_10_variance128x128,
2124 aom_highbd_10_sub_pixel_variance128x128,
2125 aom_highbd_10_sub_pixel_avg_variance128x128,
2126 aom_highbd_sad128x128x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002127 aom_highbd_dist_wtd_sad128x128_avg_bits10,
2128 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002129
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002130 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
2131 aom_highbd_sad128x64_avg_bits10,
2132 aom_highbd_10_variance128x64,
2133 aom_highbd_10_sub_pixel_variance128x64,
2134 aom_highbd_10_sub_pixel_avg_variance128x64,
2135 aom_highbd_sad128x64x4d_bits10,
2136 aom_highbd_dist_wtd_sad128x64_avg_bits10,
2137 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002138
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002139 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
2140 aom_highbd_sad64x128_avg_bits10,
2141 aom_highbd_10_variance64x128,
2142 aom_highbd_10_sub_pixel_variance64x128,
2143 aom_highbd_10_sub_pixel_avg_variance64x128,
2144 aom_highbd_sad64x128x4d_bits10,
2145 aom_highbd_dist_wtd_sad64x128_avg_bits10,
2146 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002147
David Barkerf19f35f2017-05-22 16:33:22 +01002148 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
2149 aom_highbd_10_masked_sub_pixel_variance128x128)
2150 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
2151 aom_highbd_10_masked_sub_pixel_variance128x64)
2152 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
2153 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002154 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
2155 aom_highbd_10_masked_sub_pixel_variance64x64)
2156 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
2157 aom_highbd_10_masked_sub_pixel_variance64x32)
2158 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
2159 aom_highbd_10_masked_sub_pixel_variance32x64)
2160 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
2161 aom_highbd_10_masked_sub_pixel_variance32x32)
2162 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
2163 aom_highbd_10_masked_sub_pixel_variance32x16)
2164 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
2165 aom_highbd_10_masked_sub_pixel_variance16x32)
2166 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
2167 aom_highbd_10_masked_sub_pixel_variance16x16)
2168 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
2169 aom_highbd_10_masked_sub_pixel_variance8x16)
2170 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
2171 aom_highbd_10_masked_sub_pixel_variance16x8)
2172 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
2173 aom_highbd_10_masked_sub_pixel_variance8x8)
2174 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
2175 aom_highbd_10_masked_sub_pixel_variance4x8)
2176 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
2177 aom_highbd_10_masked_sub_pixel_variance8x4)
2178 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
2179 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002180 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
2181 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002182 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
2183 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002184 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
2185 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002186 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
2187 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002188 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
2189 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002190 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
2191 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002192 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
2193 aom_highbd_10_obmc_variance128x128,
2194 aom_highbd_10_obmc_sub_pixel_variance128x128)
2195 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
2196 aom_highbd_10_obmc_variance128x64,
2197 aom_highbd_10_obmc_sub_pixel_variance128x64)
2198 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
2199 aom_highbd_10_obmc_variance64x128,
2200 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002201 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2202 aom_highbd_10_obmc_variance64x64,
2203 aom_highbd_10_obmc_sub_pixel_variance64x64)
2204 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2205 aom_highbd_10_obmc_variance64x32,
2206 aom_highbd_10_obmc_sub_pixel_variance64x32)
2207 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2208 aom_highbd_10_obmc_variance32x64,
2209 aom_highbd_10_obmc_sub_pixel_variance32x64)
2210 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2211 aom_highbd_10_obmc_variance32x32,
2212 aom_highbd_10_obmc_sub_pixel_variance32x32)
2213 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2214 aom_highbd_10_obmc_variance32x16,
2215 aom_highbd_10_obmc_sub_pixel_variance32x16)
2216 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2217 aom_highbd_10_obmc_variance16x32,
2218 aom_highbd_10_obmc_sub_pixel_variance16x32)
2219 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2220 aom_highbd_10_obmc_variance16x16,
2221 aom_highbd_10_obmc_sub_pixel_variance16x16)
2222 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2223 aom_highbd_10_obmc_variance8x16,
2224 aom_highbd_10_obmc_sub_pixel_variance8x16)
2225 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2226 aom_highbd_10_obmc_variance16x8,
2227 aom_highbd_10_obmc_sub_pixel_variance16x8)
2228 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2229 aom_highbd_10_obmc_variance8x8,
2230 aom_highbd_10_obmc_sub_pixel_variance8x8)
2231 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2232 aom_highbd_10_obmc_variance4x8,
2233 aom_highbd_10_obmc_sub_pixel_variance4x8)
2234 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2235 aom_highbd_10_obmc_variance8x4,
2236 aom_highbd_10_obmc_sub_pixel_variance8x4)
2237 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2238 aom_highbd_10_obmc_variance4x4,
2239 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002240
Rupert Swarbrick72678572017-08-02 12:05:26 +01002241 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2242 aom_highbd_10_obmc_variance64x16,
2243 aom_highbd_10_obmc_sub_pixel_variance64x16)
2244
2245 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2246 aom_highbd_10_obmc_variance16x64,
2247 aom_highbd_10_obmc_sub_pixel_variance16x64)
2248
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002249 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2250 aom_highbd_10_obmc_variance32x8,
2251 aom_highbd_10_obmc_sub_pixel_variance32x8)
2252
2253 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2254 aom_highbd_10_obmc_variance8x32,
2255 aom_highbd_10_obmc_sub_pixel_variance8x32)
2256
2257 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2258 aom_highbd_10_obmc_variance16x4,
2259 aom_highbd_10_obmc_sub_pixel_variance16x4)
2260
2261 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2262 aom_highbd_10_obmc_variance4x16,
2263 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002264 break;
2265
Yaowu Xuf883b422016-08-30 14:01:10 -07002266 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002267 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2268 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2269 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002270 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002271 aom_highbd_sad64x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002272 aom_highbd_dist_wtd_sad64x16_avg_bits12,
2273 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002274
2275 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2276 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2277 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002278 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002279 aom_highbd_sad16x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002280 aom_highbd_dist_wtd_sad16x64_avg_bits12,
2281 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002282
2283 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2284 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2285 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002286 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002287 aom_highbd_sad32x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002288 aom_highbd_dist_wtd_sad32x8_avg_bits12,
2289 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002290
2291 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2292 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2293 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002294 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002295 aom_highbd_sad8x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002296 aom_highbd_dist_wtd_sad8x32_avg_bits12,
2297 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002298
2299 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2300 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2301 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002302 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002303 aom_highbd_sad16x4x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002304 aom_highbd_dist_wtd_sad16x4_avg_bits12,
2305 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002306
2307 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2308 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2309 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002310 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002311 aom_highbd_sad4x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002312 aom_highbd_dist_wtd_sad4x16_avg_bits12,
2313 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002314
2315 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2316 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2317 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002318 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002319 aom_highbd_sad32x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002320 aom_highbd_dist_wtd_sad32x16_avg_bits12,
2321 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002322
2323 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2324 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2325 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002326 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002327 aom_highbd_sad16x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002328 aom_highbd_dist_wtd_sad16x32_avg_bits12,
2329 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002330
2331 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2332 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2333 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002334 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002335 aom_highbd_sad64x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002336 aom_highbd_dist_wtd_sad64x32_avg_bits12,
2337 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002338
2339 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2340 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2341 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002342 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002343 aom_highbd_sad32x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002344 aom_highbd_dist_wtd_sad32x64_avg_bits12,
2345 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002346
2347 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2348 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2349 aom_highbd_12_sub_pixel_variance32x32,
2350 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002351 aom_highbd_sad32x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002352 aom_highbd_dist_wtd_sad32x32_avg_bits12,
2353 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002354
2355 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2356 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2357 aom_highbd_12_sub_pixel_variance64x64,
2358 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002359 aom_highbd_sad64x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002360 aom_highbd_dist_wtd_sad64x64_avg_bits12,
2361 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002362
2363 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2364 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2365 aom_highbd_12_sub_pixel_variance16x16,
2366 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002367 aom_highbd_sad16x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002368 aom_highbd_dist_wtd_sad16x16_avg_bits12,
2369 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002370
2371 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2372 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2373 aom_highbd_12_sub_pixel_variance16x8,
2374 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002375 aom_highbd_sad16x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002376 aom_highbd_dist_wtd_sad16x8_avg_bits12,
2377 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002378
2379 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2380 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2381 aom_highbd_12_sub_pixel_variance8x16,
2382 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002383 aom_highbd_sad8x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002384 aom_highbd_dist_wtd_sad8x16_avg_bits12,
2385 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002386
2387 HIGHBD_BFP(
2388 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2389 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002390 aom_highbd_12_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002391 aom_highbd_sad8x8x4d_bits12, aom_highbd_dist_wtd_sad8x8_avg_bits12,
2392 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002393
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002394 HIGHBD_BFP(
2395 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2396 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2397 aom_highbd_12_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002398 aom_highbd_sad8x4x4d_bits12, aom_highbd_dist_wtd_sad8x4_avg_bits12,
2399 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002400
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002401 HIGHBD_BFP(
2402 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2403 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2404 aom_highbd_12_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002405 aom_highbd_sad4x8x4d_bits12, aom_highbd_dist_wtd_sad4x8_avg_bits12,
2406 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002407
2408 HIGHBD_BFP(
2409 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2410 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002411 aom_highbd_12_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002412 aom_highbd_sad4x4x4d_bits12, aom_highbd_dist_wtd_sad4x4_avg_bits12,
2413 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002414
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002415 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2416 aom_highbd_sad128x128_avg_bits12,
2417 aom_highbd_12_variance128x128,
2418 aom_highbd_12_sub_pixel_variance128x128,
2419 aom_highbd_12_sub_pixel_avg_variance128x128,
2420 aom_highbd_sad128x128x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002421 aom_highbd_dist_wtd_sad128x128_avg_bits12,
2422 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002423
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002424 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2425 aom_highbd_sad128x64_avg_bits12,
2426 aom_highbd_12_variance128x64,
2427 aom_highbd_12_sub_pixel_variance128x64,
2428 aom_highbd_12_sub_pixel_avg_variance128x64,
2429 aom_highbd_sad128x64x4d_bits12,
2430 aom_highbd_dist_wtd_sad128x64_avg_bits12,
2431 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002432
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002433 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2434 aom_highbd_sad64x128_avg_bits12,
2435 aom_highbd_12_variance64x128,
2436 aom_highbd_12_sub_pixel_variance64x128,
2437 aom_highbd_12_sub_pixel_avg_variance64x128,
2438 aom_highbd_sad64x128x4d_bits12,
2439 aom_highbd_dist_wtd_sad64x128_avg_bits12,
2440 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002441
David Barkerf19f35f2017-05-22 16:33:22 +01002442 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2443 aom_highbd_12_masked_sub_pixel_variance128x128)
2444 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2445 aom_highbd_12_masked_sub_pixel_variance128x64)
2446 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2447 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002448 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2449 aom_highbd_12_masked_sub_pixel_variance64x64)
2450 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2451 aom_highbd_12_masked_sub_pixel_variance64x32)
2452 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2453 aom_highbd_12_masked_sub_pixel_variance32x64)
2454 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2455 aom_highbd_12_masked_sub_pixel_variance32x32)
2456 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2457 aom_highbd_12_masked_sub_pixel_variance32x16)
2458 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2459 aom_highbd_12_masked_sub_pixel_variance16x32)
2460 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2461 aom_highbd_12_masked_sub_pixel_variance16x16)
2462 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2463 aom_highbd_12_masked_sub_pixel_variance8x16)
2464 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2465 aom_highbd_12_masked_sub_pixel_variance16x8)
2466 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2467 aom_highbd_12_masked_sub_pixel_variance8x8)
2468 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2469 aom_highbd_12_masked_sub_pixel_variance4x8)
2470 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2471 aom_highbd_12_masked_sub_pixel_variance8x4)
2472 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2473 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002474 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2475 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002476 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2477 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002478 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2479 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002480 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2481 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002482 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2483 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002484 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2485 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002486 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2487 aom_highbd_12_obmc_variance128x128,
2488 aom_highbd_12_obmc_sub_pixel_variance128x128)
2489 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2490 aom_highbd_12_obmc_variance128x64,
2491 aom_highbd_12_obmc_sub_pixel_variance128x64)
2492 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2493 aom_highbd_12_obmc_variance64x128,
2494 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002495 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2496 aom_highbd_12_obmc_variance64x64,
2497 aom_highbd_12_obmc_sub_pixel_variance64x64)
2498 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2499 aom_highbd_12_obmc_variance64x32,
2500 aom_highbd_12_obmc_sub_pixel_variance64x32)
2501 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2502 aom_highbd_12_obmc_variance32x64,
2503 aom_highbd_12_obmc_sub_pixel_variance32x64)
2504 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2505 aom_highbd_12_obmc_variance32x32,
2506 aom_highbd_12_obmc_sub_pixel_variance32x32)
2507 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2508 aom_highbd_12_obmc_variance32x16,
2509 aom_highbd_12_obmc_sub_pixel_variance32x16)
2510 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2511 aom_highbd_12_obmc_variance16x32,
2512 aom_highbd_12_obmc_sub_pixel_variance16x32)
2513 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2514 aom_highbd_12_obmc_variance16x16,
2515 aom_highbd_12_obmc_sub_pixel_variance16x16)
2516 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2517 aom_highbd_12_obmc_variance8x16,
2518 aom_highbd_12_obmc_sub_pixel_variance8x16)
2519 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2520 aom_highbd_12_obmc_variance16x8,
2521 aom_highbd_12_obmc_sub_pixel_variance16x8)
2522 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2523 aom_highbd_12_obmc_variance8x8,
2524 aom_highbd_12_obmc_sub_pixel_variance8x8)
2525 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2526 aom_highbd_12_obmc_variance4x8,
2527 aom_highbd_12_obmc_sub_pixel_variance4x8)
2528 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2529 aom_highbd_12_obmc_variance8x4,
2530 aom_highbd_12_obmc_sub_pixel_variance8x4)
2531 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2532 aom_highbd_12_obmc_variance4x4,
2533 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002534 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2535 aom_highbd_12_obmc_variance64x16,
2536 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002537 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2538 aom_highbd_12_obmc_variance16x64,
2539 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002540 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2541 aom_highbd_12_obmc_variance32x8,
2542 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002543 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2544 aom_highbd_12_obmc_variance8x32,
2545 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002546 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2547 aom_highbd_12_obmc_variance16x4,
2548 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002549 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2550 aom_highbd_12_obmc_variance4x16,
2551 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002552 break;
2553
2554 default:
2555 assert(0 &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002556 "cm->seq_params.bit_depth should be AOM_BITS_8, "
Yaowu Xuf883b422016-08-30 14:01:10 -07002557 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002558 }
2559 }
2560}
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002561#endif // CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002562
Yaowu Xuf883b422016-08-30 14:01:10 -07002563static void realloc_segmentation_maps(AV1_COMP *cpi) {
2564 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002565
2566 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002567 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002568 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002569 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002570
2571 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002572 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002573 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002574 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002575
2576 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002577 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002578 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002579 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002580}
2581
Yue Chenb4c93f02019-08-05 13:47:31 -07002582static void set_tpl_stats_block_size(AV1_COMP *cpi) {
2583 AV1_COMMON *const cm = &cpi->common;
2584 const int is_720p_or_larger = AOMMIN(cm->width, cm->height) >= 720;
2585
2586 // 0: 4x4, 1: 8x8, 2: 16x16
2587 cpi->tpl_stats_block_mis_log2 = is_720p_or_larger ? 2 : 1;
2588}
2589
Hui Su38711e72019-06-11 10:49:47 -07002590void av1_alloc_compound_type_rd_buffers(AV1_COMMON *const cm,
2591 CompoundTypeRdBuffers *const bufs) {
2592 CHECK_MEM_ERROR(
2593 cm, bufs->pred0,
2594 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred0)));
2595 CHECK_MEM_ERROR(
2596 cm, bufs->pred1,
2597 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred1)));
2598 CHECK_MEM_ERROR(
2599 cm, bufs->residual1,
2600 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->residual1)));
2601 CHECK_MEM_ERROR(
2602 cm, bufs->diff10,
2603 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->diff10)));
2604 CHECK_MEM_ERROR(cm, bufs->tmp_best_mask_buf,
2605 (uint8_t *)aom_malloc(2 * MAX_SB_SQUARE *
2606 sizeof(*bufs->tmp_best_mask_buf)));
2607}
2608
2609void av1_release_compound_type_rd_buffers(CompoundTypeRdBuffers *const bufs) {
2610 aom_free(bufs->pred0);
2611 aom_free(bufs->pred1);
2612 aom_free(bufs->residual1);
2613 aom_free(bufs->diff10);
2614 aom_free(bufs->tmp_best_mask_buf);
2615 av1_zero(*bufs); // Set all pointers to NULL for safety.
2616}
2617
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002618static void config_target_level(AV1_COMP *const cpi, AV1_LEVEL target_level,
2619 int tier) {
2620 aom_clear_system_state();
2621
2622 AV1EncoderConfig *const oxcf = &cpi->oxcf;
2623 SequenceHeader *const seq_params = &cpi->common.seq_params;
2624
2625 // Adjust target bitrate to be no larger than 70% of level limit.
2626 const BITSTREAM_PROFILE profile = seq_params->profile;
2627 const double level_bitrate_limit =
2628 av1_get_max_bitrate_for_level(target_level, tier, profile);
2629 const int64_t max_bitrate = (int64_t)(level_bitrate_limit * 0.70);
2630 oxcf->target_bandwidth = AOMMIN(oxcf->target_bandwidth, max_bitrate);
2631 // Also need to update cpi->twopass.bits_left.
2632 TWO_PASS *const twopass = &cpi->twopass;
2633 FIRSTPASS_STATS *stats = &twopass->total_stats;
2634 cpi->twopass.bits_left =
2635 (int64_t)(stats->duration * cpi->oxcf.target_bandwidth / 10000000.0);
2636
2637 // Adjust max over-shoot percentage.
2638 oxcf->over_shoot_pct = 0;
2639
2640 // Adjust max quantizer.
2641 oxcf->worst_allowed_q = 255;
2642
2643 // Adjust number of tiles and tile columns to be under level limit.
2644 int max_tiles, max_tile_cols;
2645 av1_get_max_tiles_for_level(target_level, &max_tiles, &max_tile_cols);
2646 while (oxcf->tile_columns > 0 && (1 << oxcf->tile_columns) > max_tile_cols) {
2647 --oxcf->tile_columns;
2648 }
2649 const int tile_cols = (1 << oxcf->tile_columns);
2650 while (oxcf->tile_rows > 0 &&
2651 tile_cols * (1 << oxcf->tile_rows) > max_tiles) {
2652 --oxcf->tile_rows;
2653 }
2654
2655 // Adjust min compression ratio.
2656 const int still_picture = seq_params->still_picture;
2657 const double min_cr =
2658 av1_get_min_cr_for_level(target_level, tier, still_picture);
2659 oxcf->min_cr = AOMMAX(oxcf->min_cr, (unsigned int)(min_cr * 100));
2660}
2661
Yaowu Xuf883b422016-08-30 14:01:10 -07002662void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2663 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002664 SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002665 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002666 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002667 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002668
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002669 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2670 seq_params->bit_depth = oxcf->bit_depth;
2671 seq_params->color_primaries = oxcf->color_primaries;
2672 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2673 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2674 seq_params->monochrome = oxcf->monochrome;
2675 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2676 seq_params->color_range = oxcf->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002677
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002678 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2679 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002680
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002681 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08002682 cm->timing_info.num_units_in_display_tick =
2683 oxcf->timing_info.num_units_in_display_tick;
2684 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
2685 cm->timing_info.equal_picture_interval =
2686 oxcf->timing_info.equal_picture_interval;
2687 cm->timing_info.num_ticks_per_picture =
2688 oxcf->timing_info.num_ticks_per_picture;
2689
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002690 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07002691 oxcf->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002692 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002693 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08002694 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002695 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08002696 cm->buffer_model.num_units_in_decoding_tick =
2697 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07002698 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07002699 av1_set_aom_dec_model_info(&cm->buffer_model);
2700 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07002701 } else if (cm->timing_info_present &&
2702 cm->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002703 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002704 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07002705 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07002706 } else {
2707 cm->op_params[0].initial_display_delay =
2708 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08002709 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002710
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002711 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002712
Yaowu Xuc27fc142016-08-22 16:08:15 -07002713 cpi->oxcf = *oxcf;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002714 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002715 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002716
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002717 memcpy(cpi->target_seq_level_idx, cpi->oxcf.target_seq_level_idx,
2718 sizeof(cpi->target_seq_level_idx));
2719 cpi->keep_level_stats = 0;
2720 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
2721 if (cpi->target_seq_level_idx[i] <= SEQ_LEVELS) {
2722 cpi->keep_level_stats |= 1u << i;
2723 if (!cpi->level_info[i]) {
2724 CHECK_MEM_ERROR(cm, cpi->level_info[i],
2725 aom_calloc(1, sizeof(*cpi->level_info[i])));
2726 }
2727 }
2728 }
2729
2730 // TODO(huisu@): level targeting currently only works for the 0th operating
2731 // point, so scalable coding is not supported yet.
2732 if (cpi->target_seq_level_idx[0] < SEQ_LEVELS) {
2733 // Adjust encoder config in order to meet target level.
2734 config_target_level(cpi, cpi->target_seq_level_idx[0], seq_params->tier[0]);
2735 }
2736
Yaowu Xuf883b422016-08-30 14:01:10 -07002737 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002738 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2739 } else {
2740 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2741 }
2742
2743 cpi->refresh_last_frame = 1;
2744 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002745 cpi->refresh_bwd_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002746
Debargha Mukherjee229fdc82018-03-10 07:45:33 -08002747 cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2748 ? REFRESH_FRAME_CONTEXT_DISABLED
2749 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002750 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08002751 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002752
Alex Converse74ad0912017-07-18 10:22:58 -07002753 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002754 CHECK_MEM_ERROR(cm, x->palette_buffer,
2755 aom_memalign(16, sizeof(*x->palette_buffer)));
2756 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002757
Hui Su38711e72019-06-11 10:49:47 -07002758 if (x->comp_rd_buffer.pred0 == NULL) {
2759 av1_alloc_compound_type_rd_buffers(cm, &x->comp_rd_buffer);
2760 }
2761
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002762 if (x->tmp_conv_dst == NULL) {
2763 CHECK_MEM_ERROR(
2764 cm, x->tmp_conv_dst,
2765 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002766 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002767 }
2768 for (int i = 0; i < 2; ++i) {
2769 if (x->tmp_obmc_bufs[i] == NULL) {
2770 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
wenyao.liu22d8ab32018-10-16 09:11:29 +08002771 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002772 sizeof(*x->tmp_obmc_bufs[i])));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002773 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002774 }
2775 }
2776
Yaowu Xuf883b422016-08-30 14:01:10 -07002777 av1_reset_segment_features(cm);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002778 set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002779
Yaowu Xuc27fc142016-08-22 16:08:15 -07002780 set_rc_buffer_sizes(rc, &cpi->oxcf);
2781
2782 // Under a configuration change, where maximum_buffer_size may change,
2783 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002784 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2785 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002786
2787 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002788 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002789
2790 // Set absolute upper and lower quality limits
2791 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2792 rc->best_quality = cpi->oxcf.best_allowed_q;
2793
Urvang Joshib55cb5e2018-09-12 14:50:21 -07002794 cm->interp_filter = oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
Yue Chen5380cb52018-02-23 15:33:21 -08002795 cm->switchable_motion_mode = 1;
2796
Yaowu Xuc27fc142016-08-22 16:08:15 -07002797 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2798 cm->render_width = cpi->oxcf.render_width;
2799 cm->render_height = cpi->oxcf.render_height;
2800 } else {
2801 cm->render_width = cpi->oxcf.width;
2802 cm->render_height = cpi->oxcf.height;
2803 }
2804 cm->width = cpi->oxcf.width;
2805 cm->height = cpi->oxcf.height;
2806
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002807 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -08002808 // Superblock size should not be updated after the first key frame.
2809 if (!cpi->seq_params_locked) {
2810 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Hui Sud909c2c2019-03-08 11:51:14 -08002811 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
2812 seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
Urvang Joshie4530f82018-01-09 11:43:37 -08002813 }
Dominic Symes917d6c02017-10-11 18:00:52 +02002814
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002815 if (cpi->initial_width || sb_size != seq_params->sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002816 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002817 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002818 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002819 av1_free_pc_tree(&cpi->td, num_planes);
Cheng Chen46f30c72017-09-07 11:13:33 -07002820 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002821 realloc_segmentation_maps(cpi);
2822 cpi->initial_width = cpi->initial_height = 0;
2823 }
2824 }
2825 update_frame_size(cpi);
2826
2827 cpi->alt_ref_source = NULL;
2828 rc->is_src_frame_alt_ref = 0;
2829
Yaowu Xuc27fc142016-08-22 16:08:15 -07002830 set_tile_info(cpi);
2831
Marco Paniconid8574e32019-08-04 21:30:12 -07002832 if (!cpi->svc.external_ref_frame_config)
2833 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002834 cpi->ext_refresh_frame_context_pending = 0;
2835
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002836#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002837 highbd_set_var_fns(cpi);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002838#endif
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002839
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002840 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002841 // This should not be called after the first key frame.
2842 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002843 seq_params->operating_points_cnt_minus_1 =
Marco Paniconi6da983b2019-09-16 20:12:43 -07002844 (cm->number_spatial_layers > 1 || cm->number_temporal_layers > 1)
2845 ? cm->number_spatial_layers * cm->number_temporal_layers - 1
2846 : 0;
Marco Paniconi67142112019-07-24 15:00:31 -07002847 init_seq_coding_tools(&cm->seq_params, cm, oxcf, cpi->use_svc);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002848 }
Marco Paniconi5b1e4732019-08-08 18:57:53 -07002849
2850 if (cpi->use_svc)
2851 av1_update_layer_context_change_config(cpi, oxcf->target_bandwidth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002852}
2853
Yaowu Xuf883b422016-08-30 14:01:10 -07002854AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2855 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002856 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002857 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2858 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002859
2860 if (!cm) return NULL;
2861
Yaowu Xuf883b422016-08-30 14:01:10 -07002862 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002863
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07002864 // The jmp_buf is valid only for the duration of the function that calls
2865 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2866 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002867 if (setjmp(cm->error.jmp)) {
2868 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002869 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002870 return 0;
2871 }
2872
2873 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002874 cm->alloc_mi = enc_alloc_mi;
2875 cm->free_mi = enc_free_mi;
2876 cm->setup_mi = enc_setup_mi;
chiyotsaia7091f12019-08-09 16:48:27 -07002877 cm->set_mb_mi = enc_set_mb_mi;
2878
2879 cm->mi_alloc_bsize = BLOCK_4X4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002880
Angie Chianga5d96c42016-10-21 16:16:56 -07002881 CHECK_MEM_ERROR(cm, cm->fc,
2882 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +00002883 CHECK_MEM_ERROR(
2884 cm, cm->default_frame_context,
2885 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -07002886 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +00002887 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002888
2889 cpi->resize_state = 0;
2890 cpi->resize_avg_qp = 0;
2891 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002892
Yaowu Xuc27fc142016-08-22 16:08:15 -07002893 cpi->common.buffer_pool = pool;
2894
2895 init_config(cpi, oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002896 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002897
Jingning Han17af7742019-09-17 16:58:03 -07002898 init_frame_info(&cpi->frame_info, cm);
2899
David Turnerd2a592e2018-11-16 14:59:31 +00002900 cm->current_frame.frame_number = 0;
David Turnera4c96252019-01-11 16:36:39 +00002901 cm->current_frame_id = -1;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002902 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002903 cpi->partition_search_skippable_frame = 0;
2904 cpi->tile_data = NULL;
David Turnere7ebf902018-12-04 14:04:55 +00002905 cpi->last_show_frame_buf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002906 realloc_segmentation_maps(cpi);
2907
Yaowu Xuc27fc142016-08-22 16:08:15 -07002908 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2909 i++) {
2910 CHECK_MEM_ERROR(
2911 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002912 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002913 }
2914
Yaowu Xuc27fc142016-08-22 16:08:15 -07002915 cpi->refresh_alt_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002916
2917 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2918#if CONFIG_INTERNAL_STATS
2919 cpi->b_calculate_blockiness = 1;
2920 cpi->b_calculate_consistency = 1;
2921 cpi->total_inconsistency = 0;
2922 cpi->psnr.worst = 100.0;
2923 cpi->worst_ssim = 100.0;
2924
2925 cpi->count = 0;
2926 cpi->bytes = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08002927#if CONFIG_SPEED_STATS
2928 cpi->tx_search_count = 0;
2929#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002930
2931 if (cpi->b_calculate_psnr) {
2932 cpi->total_sq_error = 0;
2933 cpi->total_samples = 0;
2934 cpi->tot_recode_hits = 0;
2935 cpi->summed_quality = 0;
2936 cpi->summed_weights = 0;
2937 }
2938
2939 cpi->fastssim.worst = 100.0;
2940 cpi->psnrhvs.worst = 100.0;
2941
2942 if (cpi->b_calculate_blockiness) {
2943 cpi->total_blockiness = 0;
2944 cpi->worst_blockiness = 0.0;
2945 }
2946
2947 if (cpi->b_calculate_consistency) {
2948 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002949 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002950 cpi->common.mi_rows * cpi->common.mi_cols));
2951 cpi->worst_consistency = 100.0;
2952 }
2953#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002954#if CONFIG_ENTROPY_STATS
2955 av1_zero(aggregate_fc);
2956#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002957
2958 cpi->first_time_stamp_ever = INT64_MAX;
2959
Yaowu Xuc27fc142016-08-22 16:08:15 -07002960#ifdef OUTPUT_YUV_SKINMAP
2961 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2962#endif
2963#ifdef OUTPUT_YUV_REC
2964 yuv_rec_file = fopen("rec.yuv", "wb");
2965#endif
2966
Jerome Jiang2612b4d2019-05-29 17:46:47 -07002967#if !CONFIG_REALTIME_ONLY
Yaowu Xuc27fc142016-08-22 16:08:15 -07002968 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002969 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002970 } else if (oxcf->pass == 2) {
2971 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2972 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2973
Yaowu Xuc27fc142016-08-22 16:08:15 -07002974 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2975 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2976 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2977
Yaowu Xuf883b422016-08-30 14:01:10 -07002978 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002979 }
Jerome Jiang2612b4d2019-05-29 17:46:47 -07002980#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002981
Remya0cce44c2019-08-16 11:57:24 +05302982 int sb_mi_size = av1_get_sb_mi_size(cm);
2983
Jingning Hand064cf02017-06-01 10:00:39 -07002984 CHECK_MEM_ERROR(
2985 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002986 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002987 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002988 CHECK_MEM_ERROR(
2989 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002990 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002991 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002992
2993 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2994 (int32_t *)aom_memalign(
2995 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2996
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302997 CHECK_MEM_ERROR(
2998 cm, cpi->td.mb.inter_modes_info,
2999 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303000
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303001 for (int x = 0; x < 2; x++)
3002 for (int y = 0; y < 2; y++)
3003 CHECK_MEM_ERROR(
3004 cm, cpi->td.mb.hash_value_buffer[x][y],
3005 (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
3006 sizeof(*cpi->td.mb.hash_value_buffer[0][0])));
3007
3008 cpi->td.mb.g_crc_initialized = 0;
3009
Jingning Hand064cf02017-06-01 10:00:39 -07003010 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
3011 (int32_t *)aom_memalign(
3012 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
3013
Remya0cce44c2019-08-16 11:57:24 +05303014 CHECK_MEM_ERROR(cm, cpi->td.mb.mbmi_ext,
3015 aom_calloc(sb_mi_size, sizeof(*cpi->td.mb.mbmi_ext)));
3016
David Turner04b70d82019-01-24 15:39:19 +00003017 av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
3018 av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003019
sdengc23c7f12019-06-11 16:56:50 -07003020 {
3021 const int bsize = BLOCK_16X16;
3022 const int w = mi_size_wide[bsize];
3023 const int h = mi_size_high[bsize];
3024 const int num_cols = (cm->mi_cols + w - 1) / w;
3025 const int num_rows = (cm->mi_rows + h - 1) / h;
sdengf46a1062019-08-04 18:43:50 -07003026 CHECK_MEM_ERROR(cm, cpi->tpl_rdmult_scaling_factors,
3027 aom_calloc(num_rows * num_cols,
3028 sizeof(*cpi->tpl_rdmult_scaling_factors)));
3029 CHECK_MEM_ERROR(cm, cpi->tpl_sb_rdmult_scaling_factors,
3030 aom_calloc(num_rows * num_cols,
3031 sizeof(*cpi->tpl_sb_rdmult_scaling_factors)));
3032 }
3033
3034 {
3035 const int bsize = BLOCK_16X16;
3036 const int w = mi_size_wide[bsize];
3037 const int h = mi_size_high[bsize];
3038 const int num_cols = (cm->mi_cols + w - 1) / w;
3039 const int num_rows = (cm->mi_rows + h - 1) / h;
sdengc23c7f12019-06-11 16:56:50 -07003040 CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors,
3041 aom_calloc(num_rows * num_cols,
3042 sizeof(*cpi->ssim_rdmult_scaling_factors)));
3043 }
3044
Yue Chenb4c93f02019-08-05 13:47:31 -07003045 set_tpl_stats_block_size(cpi);
Yue Chenc9b23e02019-04-10 16:54:03 -07003046 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
Yue Chenb4c93f02019-08-05 13:47:31 -07003047 const int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
3048 const int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
3049
3050 cpi->tpl_stats_buffer[frame].is_valid = 0;
3051 cpi->tpl_stats_buffer[frame].width =
3052 mi_cols >> cpi->tpl_stats_block_mis_log2;
3053 cpi->tpl_stats_buffer[frame].height =
3054 mi_rows >> cpi->tpl_stats_block_mis_log2;
3055 cpi->tpl_stats_buffer[frame].stride = cpi->tpl_stats_buffer[frame].width;
3056 cpi->tpl_stats_buffer[frame].mi_rows = cm->mi_rows;
3057 cpi->tpl_stats_buffer[frame].mi_cols = cm->mi_cols;
Yue Chen7cae98f2018-08-24 10:43:16 -07003058
Jingning Han81d6fbb2019-07-15 10:14:13 -07003059 CHECK_MEM_ERROR(
3060 cm, cpi->tpl_stats_buffer[frame].tpl_stats_ptr,
Yue Chenb4c93f02019-08-05 13:47:31 -07003061 aom_calloc(cpi->tpl_stats_buffer[frame].width *
3062 cpi->tpl_stats_buffer[frame].height,
Jingning Han81d6fbb2019-07-15 10:14:13 -07003063 sizeof(*cpi->tpl_stats_buffer[frame].tpl_stats_ptr)));
Jingning Han5e98d3b2019-09-23 21:59:36 -07003064
3065 if (aom_alloc_frame_buffer(
3066 &cpi->tpl_stats_buffer[frame].rec_picture_buf, cm->width,
3067 cm->height, cm->seq_params.subsampling_x,
3068 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
3069 cpi->oxcf.border_in_pixels, cm->byte_alignment))
3070 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3071 "Failed to allocate frame buffer");
Yue Chen7cae98f2018-08-24 10:43:16 -07003072 }
Jingning Han31a0ee92019-07-15 13:56:55 -07003073 cpi->tpl_frame = &cpi->tpl_stats_buffer[REF_FRAMES + 1];
Yue Chen7cae98f2018-08-24 10:43:16 -07003074
chiyotsai9c484b32019-03-07 16:01:50 -08003075#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08003076 av1_zero(cpi->partition_stats);
3077#endif
3078
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003079#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
3080 cpi->fn_ptr[BT].sdf = SDF; \
3081 cpi->fn_ptr[BT].sdaf = SDAF; \
3082 cpi->fn_ptr[BT].vf = VF; \
3083 cpi->fn_ptr[BT].svf = SVF; \
3084 cpi->fn_ptr[BT].svaf = SVAF; \
3085 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
3086 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07003087 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003088
Cheng Chenf78632e2017-10-20 15:30:51 -07003089 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003090 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003091 aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
3092 aom_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003093
3094 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003095 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003096 aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
3097 aom_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003098
3099 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003100 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003101 aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
3102 aom_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003103
3104 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003105 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003106 aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
3107 aom_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003108
3109 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003110 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003111 aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
3112 aom_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003113
3114 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003115 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003116 aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
3117 aom_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003118
Cheng Chenf78632e2017-10-20 15:30:51 -07003119 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
3120 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003121 aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
3122 aom_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07003123
3124 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003125 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003126 aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
3127 aom_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003128
3129 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003130 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003131 aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
3132 aom_dist_wtd_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07003133
3134 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003135 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003136 aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
3137 aom_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003138
3139 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003140 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003141 aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
3142 aom_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003143
3144 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003145 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003146 aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
3147 aom_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003148
3149 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003150 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003151 aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
3152 aom_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003153
3154 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
3155 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003156 aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
3157 aom_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003158
3159 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
3160 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003161 aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
3162 aom_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003163
3164 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
3165 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003166 aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
3167 aom_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003168
3169 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003170 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003171 aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
3172 aom_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003173
3174 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003175 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003176 aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
3177 aom_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003178
3179 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003180 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003181 aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003182
3183 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003184 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003185 aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003186
3187 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003188 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003189 aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003190
3191 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003192 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003193 aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003194
Yaowu Xuc27fc142016-08-22 16:08:15 -07003195#define OBFP(BT, OSDF, OVF, OSVF) \
3196 cpi->fn_ptr[BT].osdf = OSDF; \
3197 cpi->fn_ptr[BT].ovf = OVF; \
3198 cpi->fn_ptr[BT].osvf = OSVF;
3199
Yaowu Xuf883b422016-08-30 14:01:10 -07003200 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
3201 aom_obmc_sub_pixel_variance128x128)
3202 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
3203 aom_obmc_sub_pixel_variance128x64)
3204 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
3205 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07003206 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
3207 aom_obmc_sub_pixel_variance64x64)
3208 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
3209 aom_obmc_sub_pixel_variance64x32)
3210 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
3211 aom_obmc_sub_pixel_variance32x64)
3212 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
3213 aom_obmc_sub_pixel_variance32x32)
3214 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
3215 aom_obmc_sub_pixel_variance32x16)
3216 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
3217 aom_obmc_sub_pixel_variance16x32)
3218 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
3219 aom_obmc_sub_pixel_variance16x16)
3220 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
3221 aom_obmc_sub_pixel_variance16x8)
3222 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
3223 aom_obmc_sub_pixel_variance8x16)
3224 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
3225 aom_obmc_sub_pixel_variance8x8)
3226 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
3227 aom_obmc_sub_pixel_variance4x8)
3228 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
3229 aom_obmc_sub_pixel_variance8x4)
3230 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
3231 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003232 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
3233 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003234 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
3235 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003236 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
3237 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003238 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
3239 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003240 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
3241 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003242 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
3243 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003244
David Barkerf19f35f2017-05-22 16:33:22 +01003245#define MBFP(BT, MCSDF, MCSVF) \
3246 cpi->fn_ptr[BT].msdf = MCSDF; \
3247 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003248
David Barkerf19f35f2017-05-22 16:33:22 +01003249 MBFP(BLOCK_128X128, aom_masked_sad128x128,
3250 aom_masked_sub_pixel_variance128x128)
3251 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
3252 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01003253 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
3254 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
3255 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
3256 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
3257 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
3258 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
3259 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
3260 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
3261 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
3262 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
3263 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
3264 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
3265 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003266
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003267 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
3268
3269 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
3270
3271 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
3272
3273 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003274
3275 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
3276
3277 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01003278
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003279#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003280 highbd_set_var_fns(cpi);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003281#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003282
Yaowu Xuf883b422016-08-30 14:01:10 -07003283 /* av1_init_quantizer() is first called here. Add check in
3284 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07003285 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07003286 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003287 */
Yaowu Xuf883b422016-08-30 14:01:10 -07003288 av1_init_quantizer(cpi);
Zoe Liud902b742018-02-19 17:02:41 -08003289 av1_qm_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003290
Yaowu Xuf883b422016-08-30 14:01:10 -07003291 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07003292 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003293 cm->superres_upscaled_width = oxcf->width;
3294 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07003295 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003296
3297 cm->error.setjmp = 0;
3298
3299 return cpi;
3300}
3301
Urvang Joshiee2c8112018-05-04 14:53:15 -07003302#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003303#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
3304
3305#define SNPRINT2(H, T, V) \
3306 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07003307#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003308
Yaowu Xuf883b422016-08-30 14:01:10 -07003309void av1_remove_compressor(AV1_COMP *cpi) {
3310 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003311 unsigned int i;
3312 int t;
3313
3314 if (!cpi) return;
3315
3316 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003317 const int num_planes = av1_num_planes(cm);
3318
David Turnerd2a592e2018-11-16 14:59:31 +00003319 if (cm->current_frame.frame_number > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003320#if CONFIG_ENTROPY_STATS
3321 if (cpi->oxcf.pass != 1) {
3322 fprintf(stderr, "Writing counts.stt\n");
3323 FILE *f = fopen("counts.stt", "wb");
3324 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
3325 fclose(f);
3326 }
3327#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003328#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003329 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003330
3331 if (cpi->oxcf.pass != 1) {
3332 char headings[512] = { 0 };
3333 char results[512] = { 0 };
3334 FILE *f = fopen("opsnr.stt", "a");
3335 double time_encoded =
3336 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
3337 10000000.000;
3338 double total_encode_time =
3339 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3340 const double dr =
3341 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3342 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3343 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3344 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3345
3346 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003347 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003348 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3349 const double total_ssim =
3350 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3351 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08003352 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003353 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003354 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07003355 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003356 snprintf(results, sizeof(results),
3357 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3358 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003359 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07003360 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003361 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
3362 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
3363 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
3364 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003365 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003366 cpi->psnr.stat[STAT_Y] / cpi->count,
3367 cpi->psnr.stat[STAT_U] / cpi->count,
Yue Chenf0652ed2019-08-13 16:09:25 -07003368 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003369
3370 if (cpi->b_calculate_blockiness) {
3371 SNPRINT(headings, "\t Block\tWstBlck");
3372 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3373 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3374 }
3375
3376 if (cpi->b_calculate_consistency) {
3377 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003378 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003379 (double)cpi->total_inconsistency);
3380
3381 SNPRINT(headings, "\tConsist\tWstCons");
3382 SNPRINT2(results, "\t%7.3f", consistency);
3383 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3384 }
Yue Chenf0652ed2019-08-13 16:09:25 -07003385
3386 SNPRINT(headings, "\t Time\tRcErr\tAbsErr");
3387 SNPRINT2(results, "\t%8.0f", total_encode_time);
3388 SNPRINT2(results, "\t%7.2f", rate_err);
3389 SNPRINT2(results, "\t%7.2f", fabs(rate_err));
3390
3391 fprintf(f, "%s\tAPsnr611\n", headings);
3392 fprintf(f, "%s\t%7.3f\n", results,
3393 (6 * cpi->psnr.stat[STAT_Y] + cpi->psnr.stat[STAT_U] +
3394 cpi->psnr.stat[STAT_V]) /
3395 (cpi->count * 8));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003396 }
3397
3398 fclose(f);
3399 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07003400#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003401#if CONFIG_SPEED_STATS
3402 if (cpi->oxcf.pass != 1) {
3403 fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
3404 }
3405#endif // CONFIG_SPEED_STATS
chiyotsai92ed0dd2019-01-25 14:50:14 -08003406
chiyotsai9c484b32019-03-07 16:01:50 -08003407#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08003408 if (cpi->oxcf.pass != 1) {
3409 av1_print_partition_stats(&cpi->partition_stats);
3410 }
3411#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003412 }
3413
Yue Chenc9b23e02019-04-10 16:54:03 -07003414 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
Jingning Han81d6fbb2019-07-15 10:14:13 -07003415 aom_free(cpi->tpl_stats_buffer[frame].tpl_stats_ptr);
3416 cpi->tpl_stats_buffer[frame].is_valid = 0;
Jingning Han5e98d3b2019-09-23 21:59:36 -07003417
3418 aom_free_frame_buffer(&cpi->tpl_stats_buffer[frame].rec_picture_buf);
3419 cpi->tpl_stats_buffer[frame].rec_picture = NULL;
Yue Chen7cae98f2018-08-24 10:43:16 -07003420 }
3421
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303422 for (t = cpi->num_workers - 1; t >= 0; --t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003423 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003424 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3425
3426 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003427 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003428
3429 // Deallocate allocated thread data.
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303430 aom_free(thread_data->td->tctx);
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303431 if (t > 0) {
hui sud9a812b2017-07-06 14:34:37 -07003432 aom_free(thread_data->td->palette_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003433 aom_free(thread_data->td->tmp_conv_dst);
Hui Su38711e72019-06-11 10:49:47 -07003434 av1_release_compound_type_rd_buffers(&thread_data->td->comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003435 for (int j = 0; j < 2; ++j) {
3436 aom_free(thread_data->td->tmp_obmc_bufs[j]);
3437 }
Jingning Hand064cf02017-06-01 10:00:39 -07003438 aom_free(thread_data->td->above_pred_buf);
3439 aom_free(thread_data->td->left_pred_buf);
3440 aom_free(thread_data->td->wsrc_buf);
wenyao.liu22d8ab32018-10-16 09:11:29 +08003441
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303442 aom_free(thread_data->td->inter_modes_info);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003443 for (int x = 0; x < 2; x++) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303444 for (int y = 0; y < 2; y++) {
3445 aom_free(thread_data->td->hash_value_buffer[x][y]);
3446 thread_data->td->hash_value_buffer[x][y] = NULL;
3447 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003448 }
Jingning Hand064cf02017-06-01 10:00:39 -07003449 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003450 aom_free(thread_data->td->counts);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003451 av1_free_pc_tree(thread_data->td, num_planes);
Remya0cce44c2019-08-16 11:57:24 +05303452 aom_free(thread_data->td->mbmi_ext);
Yaowu Xuf883b422016-08-30 14:01:10 -07003453 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003454 }
3455 }
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303456#if CONFIG_MULTITHREAD
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303457 if (cpi->row_mt_mutex_ != NULL) {
3458 pthread_mutex_destroy(cpi->row_mt_mutex_);
3459 aom_free(cpi->row_mt_mutex_);
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303460 }
3461#endif
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05303462 av1_row_mt_mem_dealloc(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07003463 aom_free(cpi->tile_thr_data);
3464 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003465
Deepa K G964e72e2018-05-16 16:56:01 +05303466 if (cpi->num_workers > 1) {
3467 av1_loop_filter_dealloc(&cpi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05303468 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
Deepa K G964e72e2018-05-16 16:56:01 +05303469 }
3470
Yaowu Xuc27fc142016-08-22 16:08:15 -07003471 dealloc_compressor_data(cpi);
3472
3473 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
3474 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003475 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003476 }
3477
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003478#if CONFIG_INTERNAL_STATS
3479 aom_free(cpi->ssim_vars);
3480 cpi->ssim_vars = NULL;
3481#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003482
Yaowu Xuf883b422016-08-30 14:01:10 -07003483 av1_remove_common(cm);
RogerZhou80d52342017-11-20 10:56:26 -08003484 for (i = 0; i < FRAME_BUFFERS; ++i) {
3485 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
3486 }
Yaowu Xu74e63352019-05-06 09:21:33 -07003487#if CONFIG_HTB_TRELLIS
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08003488 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xu74e63352019-05-06 09:21:33 -07003489#endif // CONFIG_HTB_TRELLIS
Yaowu Xuf883b422016-08-30 14:01:10 -07003490 av1_free_ref_frame_buffers(cm->buffer_pool);
3491 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003492
3493#ifdef OUTPUT_YUV_SKINMAP
3494 fclose(yuv_skinmap_file);
3495#endif
3496#ifdef OUTPUT_YUV_REC
3497 fclose(yuv_rec_file);
3498#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003499}
3500
Yaowu Xuf883b422016-08-30 14:01:10 -07003501static void generate_psnr_packet(AV1_COMP *cpi) {
3502 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003503 int i;
3504 PSNR_STATS psnr;
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003505#if CONFIG_AV1_HIGHBITDEPTH
3506 // TODO(yaowu): unify these two versions into one.
3507 if (cpi->common.seq_params.use_highbitdepth)
3508 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
3509 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
3510 else
3511 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
3512#else
3513 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
3514#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003515
3516 for (i = 0; i < 4; ++i) {
3517 pkt.data.psnr.samples[i] = psnr.samples[i];
3518 pkt.data.psnr.sse[i] = psnr.sse[i];
3519 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3520 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003521 pkt.kind = AOM_CODEC_PSNR_PKT;
3522 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003523}
3524
Yaowu Xuf883b422016-08-30 14:01:10 -07003525int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003526 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3527
Yunqing Wangf2e7a392017-11-08 00:27:21 -08003528 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003529 return 0;
3530}
3531
Thomas Daede497d1952017-08-08 17:33:06 -07003532int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3533 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003534 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003535 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003536 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003537 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003538 return 0;
3539 } else {
3540 return -1;
3541 }
3542}
3543
Thomas Daede497d1952017-08-08 17:33:06 -07003544int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3545 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003546 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003547 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003548 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003549 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003550 return 0;
3551 } else {
3552 return -1;
3553 }
3554}
3555
3556int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003557 cpi->ext_refresh_frame_context = update;
3558 cpi->ext_refresh_frame_context_pending = 1;
3559 return 0;
3560}
3561
3562#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3563// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3564// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3565// not denoise the UV channels at this time. If ever we implement UV channel
3566// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003567void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003568 uint8_t *src = s->y_buffer;
3569 int h = s->y_height;
3570
3571 do {
3572 fwrite(src, s->y_width, 1, f);
3573 src += s->y_stride;
3574 } while (--h);
3575
3576 src = s->u_buffer;
3577 h = s->uv_height;
3578
3579 do {
3580 fwrite(src, s->uv_width, 1, f);
3581 src += s->uv_stride;
3582 } while (--h);
3583
3584 src = s->v_buffer;
3585 h = s->uv_height;
3586
3587 do {
3588 fwrite(src, s->uv_width, 1, f);
3589 src += s->uv_stride;
3590 } while (--h);
3591}
3592#endif
3593
Yaowu Xuc27fc142016-08-22 16:08:15 -07003594#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003595void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003596 uint8_t *src = s->y_buffer;
3597 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003598 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003599 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3600 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3601
3602 do {
3603 fwrite(src16, s->y_width, 2, yuv_rec_file);
3604 src16 += s->y_stride;
3605 } while (--h);
3606
3607 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3608 h = s->uv_height;
3609
3610 do {
3611 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3612 src16 += s->uv_stride;
3613 } while (--h);
3614
3615 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3616 h = s->uv_height;
3617
3618 do {
3619 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3620 src16 += s->uv_stride;
3621 } while (--h);
3622
3623 fflush(yuv_rec_file);
3624 return;
3625 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003626
3627 do {
3628 fwrite(src, s->y_width, 1, yuv_rec_file);
3629 src += s->y_stride;
3630 } while (--h);
3631
3632 src = s->u_buffer;
3633 h = s->uv_height;
3634
3635 do {
3636 fwrite(src, s->uv_width, 1, yuv_rec_file);
3637 src += s->uv_stride;
3638 } while (--h);
3639
3640 src = s->v_buffer;
3641 h = s->uv_height;
3642
3643 do {
3644 fwrite(src, s->uv_width, 1, yuv_rec_file);
3645 src += s->uv_stride;
3646 } while (--h);
3647
3648 fflush(yuv_rec_file);
3649}
3650#endif // OUTPUT_YUV_REC
3651
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003652#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003653static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3654 int i;
3655 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003656 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003657 AV1_COMMON *const cm = &cpi->common;
3658 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3659 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003660 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003661 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003662 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003663 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003664 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003665 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003666 // TODO(sarahparker): The earlier condition for recoding here was:
3667 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3668 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003669 }
3670 }
3671 return recode;
3672}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003673
Yaowu Xuc27fc142016-08-22 16:08:15 -07003674// Function to test for conditions that indicate we should loop
3675// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003676static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3677 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003678 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003679 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003680 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3681 int force_recode = 0;
3682
3683 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3684 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3685 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003686 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3687 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3688 (rc->projected_frame_size < low_limit && q > minq)) {
3689 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003690 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003691 // Deal with frame undershoot and whether or not we are
3692 // below the automatically set cq level.
3693 if (q > oxcf->cq_level &&
3694 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3695 force_recode = 1;
3696 }
3697 }
3698 }
3699 return force_recode;
3700}
3701
Cheng Chen46f30c72017-09-07 11:13:33 -07003702static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003703 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003704 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003705 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003706
3707 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003708 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Jingning Han667561a2019-07-22 15:48:01 -07003709 if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003710 BufferPool *const pool = cm->buffer_pool;
3711 const YV12_BUFFER_CONFIG *const ref =
David Turnera21966b2018-12-05 14:48:49 +00003712 get_ref_frame_yv12_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003713
3714 if (ref == NULL) {
David Turnere7ebf902018-12-04 14:04:55 +00003715 cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003716 continue;
3717 }
3718
Yaowu Xuc27fc142016-08-22 16:08:15 -07003719 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
Debargha Mukherjee13cccf22019-03-27 23:49:14 -07003720 // Replace the reference buffer with a copy having a thicker border,
3721 // if the reference buffer is higher resolution than the current
3722 // frame, and the border is thin.
3723 if ((ref->y_crop_width > cm->width ||
3724 ref->y_crop_height > cm->height) &&
3725 ref->border < AOM_BORDER_IN_PIXELS) {
3726 RefCntBuffer *ref_fb = get_ref_frame_buf(cm, ref_frame);
3727 if (aom_yv12_realloc_with_new_border(
3728 &ref_fb->buf, AOM_BORDER_IN_PIXELS, cm->byte_alignment,
3729 num_planes) != 0) {
3730 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3731 "Failed to allocate frame buffer");
3732 }
3733 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003734 int force_scaling = 0;
David Turnere7ebf902018-12-04 14:04:55 +00003735 RefCntBuffer *new_fb = cpi->scaled_ref_buf[ref_frame - 1];
3736 if (new_fb == NULL) {
3737 const int new_fb_idx = get_free_fb(cm);
3738 if (new_fb_idx == INVALID_IDX) {
Wan-Teh Chang4a8c0042018-10-05 09:41:52 -07003739 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3740 "Unable to find free frame buffer");
David Turnere7ebf902018-12-04 14:04:55 +00003741 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003742 force_scaling = 1;
David Turnere7ebf902018-12-04 14:04:55 +00003743 new_fb = &pool->frame_bufs[new_fb_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003744 }
David Turnere7ebf902018-12-04 14:04:55 +00003745
3746 if (force_scaling || new_fb->buf.y_crop_width != cm->width ||
3747 new_fb->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003748 if (aom_realloc_frame_buffer(
David Turnere7ebf902018-12-04 14:04:55 +00003749 &new_fb->buf, cm->width, cm->height,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003750 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
Debargha Mukherjeefa946af2019-03-26 16:58:55 -07003751 cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003752 cm->byte_alignment, NULL, NULL, NULL)) {
3753 if (force_scaling) {
3754 // Release the reference acquired in the get_free_fb() call above.
David Turnere7ebf902018-12-04 14:04:55 +00003755 --new_fb->ref_count;
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003756 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003757 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003758 "Failed to allocate frame buffer");
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003759 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003760 av1_resize_and_extend_frame(
David Turnere7ebf902018-12-04 14:04:55 +00003761 ref, &new_fb->buf, (int)cm->seq_params.bit_depth, num_planes);
3762 cpi->scaled_ref_buf[ref_frame - 1] = new_fb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003763 alloc_frame_mvs(cm, new_fb);
3764 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003765 } else {
David Turnera21966b2018-12-05 14:48:49 +00003766 RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003767 buf->buf.y_crop_width = ref->y_crop_width;
3768 buf->buf.y_crop_height = ref->y_crop_height;
David Turnere7ebf902018-12-04 14:04:55 +00003769 cpi->scaled_ref_buf[ref_frame - 1] = buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003770 ++buf->ref_count;
3771 }
3772 } else {
David Turnere7ebf902018-12-04 14:04:55 +00003773 if (cpi->oxcf.pass != 0) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003774 }
3775 }
3776}
3777
Yaowu Xuf883b422016-08-30 14:01:10 -07003778static void release_scaled_references(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003779 // TODO(isbs): only refresh the necessary frames, rather than all of them
David Turnere7ebf902018-12-04 14:04:55 +00003780 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3781 RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
3782 if (buf != NULL) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003783 --buf->ref_count;
David Turnere7ebf902018-12-04 14:04:55 +00003784 cpi->scaled_ref_buf[i] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003785 }
3786 }
3787}
3788
Yaowu Xuf883b422016-08-30 14:01:10 -07003789static void set_mv_search_params(AV1_COMP *cpi) {
3790 const AV1_COMMON *const cm = &cpi->common;
3791 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003792
3793 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003794 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003795
3796 if (cpi->sf.mv.auto_mv_step_size) {
3797 if (frame_is_intra_only(cm)) {
3798 // Initialize max_mv_magnitude for use in the first INTER frame
3799 // after a key/intra-only frame.
3800 cpi->max_mv_magnitude = max_mv_def;
3801 } else {
3802 if (cm->show_frame) {
3803 // Allow mv_steps to correspond to twice the max mv magnitude found
3804 // in the previous frame, capped by the default max_mv_magnitude based
3805 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003806 cpi->mv_step_param = av1_init_search_range(
3807 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003808 }
3809 cpi->max_mv_magnitude = 0;
3810 }
3811 }
3812}
3813
Hui Subdf0c992019-02-14 14:52:41 -08003814static void set_screen_content_options(AV1_COMP *cpi) {
3815 AV1_COMMON *cm = &cpi->common;
3816
3817 if (cm->seq_params.force_screen_content_tools != 2) {
3818 cm->allow_screen_content_tools = cm->allow_intrabc =
3819 cm->seq_params.force_screen_content_tools;
3820 return;
3821 }
3822
3823 if (cpi->oxcf.content == AOM_CONTENT_SCREEN) {
3824 cm->allow_screen_content_tools = cm->allow_intrabc = 1;
3825 return;
3826 }
3827
3828 // Estimate if the source frame is screen content, based on the portion of
3829 // blocks that have few luma colors.
3830 const uint8_t *src = cpi->source->y_buffer;
3831 assert(src != NULL);
3832 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
3833 const int stride = cpi->source->y_stride;
3834 const int width = cpi->source->y_width;
3835 const int height = cpi->source->y_height;
3836 const int bd = cm->seq_params.bit_depth;
3837 const int blk_w = 16;
3838 const int blk_h = 16;
3839 // These threshold values are selected experimentally.
3840 const int color_thresh = 4;
3841 const unsigned int var_thresh = 0;
3842 // Counts of blocks with no more than color_thresh colors.
3843 int counts_1 = 0;
3844 // Counts of blocks with no more than color_thresh colors and variance larger
3845 // than var_thresh.
3846 int counts_2 = 0;
3847
3848 for (int r = 0; r + blk_h <= height; r += blk_h) {
3849 for (int c = 0; c + blk_w <= width; c += blk_w) {
3850 int count_buf[1 << 12]; // Maximum (1 << 12) color levels.
3851 const uint8_t *const this_src = src + r * stride + c;
3852 const int n_colors =
3853 use_hbd ? av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd,
3854 count_buf)
3855 : av1_count_colors(this_src, stride, blk_w, blk_h, count_buf);
3856 if (n_colors > 1 && n_colors <= color_thresh) {
3857 ++counts_1;
3858 struct buf_2d buf;
3859 buf.stride = stride;
3860 buf.buf = (uint8_t *)this_src;
3861 const unsigned int var =
3862 use_hbd
3863 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
3864 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
3865 if (var > var_thresh) ++counts_2;
3866 }
3867 }
3868 }
3869
3870 // The threshold values are selected experimentally.
3871 cm->allow_screen_content_tools =
3872 counts_1 * blk_h * blk_w * 10 > width * height;
3873 // IntraBC would force loop filters off, so we use more strict rules that also
3874 // requires that the block has high variance.
3875 cm->allow_intrabc = cm->allow_screen_content_tools &&
Hui Su64a2ffb2019-04-11 16:47:13 -07003876 counts_2 * blk_h * blk_w * 12 > width * height;
Hui Subdf0c992019-02-14 14:52:41 -08003877}
3878
Yaowu Xuf883b422016-08-30 14:01:10 -07003879static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003880 int i;
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003881 AV1_COMMON *cm = &cpi->common;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003882 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003883 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003884 }
3885 cpi->global_motion_search_done = 0;
Yunqing Wang9411e432019-03-14 15:53:23 -07003886
3887 if (frame_is_intra_only(cm)) set_screen_content_options(cpi);
3888 cpi->is_screen_content_type = (cm->allow_screen_content_tools != 0);
3889
David Turner04b70d82019-01-24 15:39:19 +00003890 av1_set_speed_features_framesize_independent(cpi, cpi->speed);
Yaowu Xuf883b422016-08-30 14:01:10 -07003891 av1_set_rd_speed_thresholds(cpi);
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003892 cm->interp_filter = SWITCHABLE;
3893 cm->switchable_motion_mode = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003894}
3895
Jerome Jiangd413f4a2019-09-23 14:53:03 -07003896#if !CONFIG_REALTIME_ONLY
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003897static int get_gfu_boost_from_r0(double r0, int frames_to_key) {
3898 double factor = sqrt((double)frames_to_key);
3899 factor = AOMMIN(factor, 10.0);
3900 factor = AOMMAX(factor, 4.0);
3901 const int boost = (int)rint((200.0 + 10.0 * factor) / r0);
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003902 return boost;
3903}
3904
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003905static int get_kf_boost_from_r0(double r0, int frames_to_key) {
3906 double factor = sqrt((double)frames_to_key);
3907 factor = AOMMIN(factor, 10.0);
3908 factor = AOMMAX(factor, 4.0);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003909 const int boost = (int)rint((75.0 + 14.0 * factor) / r0);
3910 return boost;
3911}
Jerome Jiangd413f4a2019-09-23 14:53:03 -07003912#endif
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003913
3914int combine_prior_with_tpl_boost(int prior_boost, int tpl_boost,
3915 int frames_to_key) {
3916 double factor = sqrt((double)frames_to_key);
3917 factor = AOMMIN(factor, 12.0);
3918 factor = AOMMAX(factor, 4.0);
3919 factor -= 4.0;
3920 int boost = (int)((factor * prior_boost + (8.0 - factor) * tpl_boost) / 8.0);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003921 return boost;
3922}
3923
Jerome Jiangd413f4a2019-09-23 14:53:03 -07003924#if !CONFIG_REALTIME_ONLY
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003925static void process_tpl_stats_frame(AV1_COMP *cpi) {
Sarah Parkere1b22012019-06-06 16:35:25 -07003926 const GF_GROUP *const gf_group = &cpi->gf_group;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003927 AV1_COMMON *const cm = &cpi->common;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003928
Sarah Parkere1b22012019-06-06 16:35:25 -07003929 assert(IMPLIES(gf_group->size > 0, gf_group->index < gf_group->size));
Jingning Han31a0ee92019-07-15 13:56:55 -07003930
3931 const int tpl_idx = gf_group->index;
Jingning Han81d6fbb2019-07-15 10:14:13 -07003932 TplDepFrame *tpl_frame = &cpi->tpl_frame[tpl_idx];
Yue Chen4e585cc2019-06-03 14:47:16 -07003933 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003934
Yue Chen4e585cc2019-06-03 14:47:16 -07003935 if (tpl_frame->is_valid) {
3936 int tpl_stride = tpl_frame->stride;
3937 int64_t intra_cost_base = 0;
3938 int64_t mc_dep_cost_base = 0;
Yue Chenbd934232019-08-05 14:23:39 -07003939#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07003940 int64_t mc_saved_base = 0;
3941 int64_t mc_count_base = 0;
Yue Chenbd934232019-08-05 14:23:39 -07003942#endif // !USE_TPL_CLASSIC_MODEL
Yue Chenb4c93f02019-08-05 13:47:31 -07003943 const int step = 1 << cpi->tpl_stats_block_mis_log2;
Debargha Mukherjeeedfa4fe2019-07-08 14:23:39 -07003944 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
Yue Chenb4c93f02019-08-05 13:47:31 -07003945
3946 for (int row = 0; row < cm->mi_rows; row += step) {
3947 for (int col = 0; col < mi_cols_sr; col += step) {
3948 TplDepStats *this_stats =
3949 &tpl_stats[av1_tpl_ptr_pos(cpi, row, col, tpl_stride)];
Jingning Han40e870f2019-10-02 16:05:39 -07003950 int64_t mc_dep_delta =
3951 RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
3952 this_stats->mc_dep_dist);
3953 intra_cost_base += (this_stats->recrf_dist << RDDIV_BITS);
3954 mc_dep_cost_base +=
3955 (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta;
Yue Chenbd934232019-08-05 14:23:39 -07003956#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07003957 mc_count_base += this_stats->mc_count;
3958 mc_saved_base += this_stats->mc_saved;
Yue Chenbd934232019-08-05 14:23:39 -07003959#endif // !USE_TPL_CLASSIC_MODEL
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003960 }
Yue Chen4e585cc2019-06-03 14:47:16 -07003961 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003962
Yue Chen4e585cc2019-06-03 14:47:16 -07003963 if (mc_dep_cost_base == 0) {
3964 tpl_frame->is_valid = 0;
3965 } else {
3966 aom_clear_system_state();
3967 cpi->rd.r0 = (double)intra_cost_base / mc_dep_cost_base;
Jingning Han44a573b2019-09-04 14:01:22 -07003968 if (is_frame_arf_and_tpl_eligible(gf_group)) {
Yue Chen4e585cc2019-06-03 14:47:16 -07003969 cpi->rd.arf_r0 = cpi->rd.r0;
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003970 const int gfu_boost =
3971 get_gfu_boost_from_r0(cpi->rd.arf_r0, cpi->rc.frames_to_key);
Yue Chen4e585cc2019-06-03 14:47:16 -07003972 // printf("old boost %d new boost %d\n", cpi->rc.gfu_boost,
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003973 // gfu_boost);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003974 cpi->rc.gfu_boost = combine_prior_with_tpl_boost(
3975 cpi->rc.gfu_boost, gfu_boost, cpi->rc.frames_to_key);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003976 } else if (frame_is_intra_only(cm)) {
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07003977 // TODO(debargha): Turn off q adjustment for kf temporarily to
3978 // reduce impact on speed of encoding. Need to investigate how
3979 // to mitigate the issue.
3980 if (cpi->oxcf.rc_mode == AOM_Q) {
3981 const int kf_boost =
3982 get_kf_boost_from_r0(cpi->rd.r0, cpi->rc.frames_to_key);
3983 // printf("old kf boost %d new kf boost %d [%d]\n", cpi->rc.kf_boost,
3984 // kf_boost, cpi->rc.frames_to_key);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003985 cpi->rc.kf_boost = combine_prior_with_tpl_boost(
3986 cpi->rc.kf_boost, kf_boost, cpi->rc.frames_to_key);
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07003987 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003988 }
Yue Chenbd934232019-08-05 14:23:39 -07003989#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07003990 cpi->rd.mc_count_base =
3991 (double)mc_count_base / (cm->mi_rows * cm->mi_cols);
3992 cpi->rd.mc_saved_base =
3993 (double)mc_saved_base / (cm->mi_rows * cm->mi_cols);
Yue Chenbd934232019-08-05 14:23:39 -07003994#endif // !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07003995 aom_clear_system_state();
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003996 }
3997 }
3998}
Jerome Jiangd413f4a2019-09-23 14:53:03 -07003999#endif // !CONFIG_REALTIME_ONLY
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004000
Debargha Mukherjeecd14cc12019-09-06 09:55:52 -07004001static int determine_frame_high_precision_mv(const AV1_COMP *cpi, int qindex) {
4002 (void)cpi;
4003 if (cpi->sf.reduce_high_precision_mv_usage == 2)
4004 return 0;
4005 else if (cpi->sf.reduce_high_precision_mv_usage == 1)
4006 return qindex < HIGH_PRECISION_MV_QTHRESH / 2;
4007 else
4008 return qindex < HIGH_PRECISION_MV_QTHRESH;
4009}
4010
Yaowu Xuf883b422016-08-30 14:01:10 -07004011static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004012 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004013 AV1_COMMON *const cm = &cpi->common;
4014 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004015
4016 // Setup variables that depend on the dimensions of the frame.
David Turner04b70d82019-01-24 15:39:19 +00004017 av1_set_speed_features_framesize_dependent(cpi, cpi->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004018
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004019#if !CONFIG_REALTIME_ONLY
Yue Chen4e585cc2019-06-03 14:47:16 -07004020 if (cpi->oxcf.enable_tpl_model && cpi->tpl_model_pass == 0 &&
sdengf46a1062019-08-04 18:43:50 -07004021 is_frame_tpl_eligible(cpi)) {
Yue Chen4e585cc2019-06-03 14:47:16 -07004022 process_tpl_stats_frame(cpi);
sdengf46a1062019-08-04 18:43:50 -07004023 av1_tpl_rdmult_setup(cpi);
4024 }
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004025#endif
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004026
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01004027 // Decide q and q bounds.
Angie Chiang958904c2019-09-19 16:03:35 -07004028 *q = av1_rc_pick_q_and_bounds(cpi, &cpi->rc, cm->width, cm->height,
4029 cpi->gf_group.index, bottom_index, top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004030
James Zern01a9d702017-08-25 19:09:33 +00004031 if (!frame_is_intra_only(cm)) {
Debargha Mukherjeecd14cc12019-09-06 09:55:52 -07004032 const int use_hp = cpi->common.cur_frame_force_integer_mv
4033 ? 0
4034 : determine_frame_high_precision_mv(cpi, *q);
4035 set_high_precision_mv(cpi, use_hp, cpi->common.cur_frame_force_integer_mv);
James Zern01a9d702017-08-25 19:09:33 +00004036 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004037
4038 // Configure experimental use of segmentation for enhanced coding of
4039 // static regions if indicated.
4040 // Only allowed in the second pass of a two pass encode, as it requires
4041 // lagged coding, and if the relevant speed feature flag is set.
4042 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
4043 configure_static_seg_features(cpi);
4044}
4045
Yaowu Xuf883b422016-08-30 14:01:10 -07004046static void init_motion_estimation(AV1_COMP *cpi) {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004047 AV1_COMMON *const cm = &cpi->common;
chiyotsai836b69b2019-04-09 13:41:24 -07004048 const int y_stride = cpi->scaled_source.y_stride;
Urvang Joshi510d8f62019-01-10 12:11:50 -08004049 const int y_stride_src =
4050 ((cpi->oxcf.width != cm->width || cpi->oxcf.height != cm->height) ||
4051 av1_superres_scaled(cm))
4052 ? y_stride
4053 : cpi->lookahead->buf->img.y_stride;
chiyotsai836b69b2019-04-09 13:41:24 -07004054 // Update if ss_cfg is uninitialized or the current frame has a new stride
4055 const int should_update = !cpi->ss_cfg[SS_CFG_SRC].stride ||
4056 !cpi->ss_cfg[SS_CFG_LOOKAHEAD].stride ||
4057 (y_stride != cpi->ss_cfg[SS_CFG_SRC].stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004058
chiyotsai836b69b2019-04-09 13:41:24 -07004059 if (!should_update) {
4060 return;
4061 }
4062
4063 if (cpi->sf.mv.search_method == DIAMOND) {
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05304064 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
4065 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD],
4066 y_stride_src);
chiyotsai836b69b2019-04-09 13:41:24 -07004067 } else {
4068 // Update the offsets in search_sites as y_stride can change due to scaled
4069 // references. This update allows NSTEP to be used on scaled references as
4070 // long as sf.mv.search_method is not DIAMOND. Currently in the codebae,
4071 // sf.mv.search_method is never set to DIAMOND.
4072 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
4073 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD], y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004074 }
4075}
4076
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004077#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004078static void set_restoration_unit_size(int width, int height, int sx, int sy,
4079 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004080 (void)width;
4081 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004082 (void)sx;
4083 (void)sy;
4084#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
4085 int s = AOMMIN(sx, sy);
4086#else
4087 int s = 0;
4088#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
4089
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07004090 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08004091 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07004092 else
Urvang Joshi813186b2018-03-08 15:38:46 -08004093 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004094 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
4095 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004096}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004097
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05304098static void init_ref_frame_bufs(AV1_COMP *cpi) {
4099 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07004100 int i;
4101 BufferPool *const pool = cm->buffer_pool;
Jack Haughtonddb80602018-11-21 16:41:49 +00004102 cm->cur_frame = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -07004103 for (i = 0; i < REF_FRAMES; ++i) {
David Turnere7ebf902018-12-04 14:04:55 +00004104 cm->ref_frame_map[i] = NULL;
Wan-Teh Changd05e0332018-10-03 12:00:43 -07004105 }
4106 for (i = 0; i < FRAME_BUFFERS; ++i) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004107 pool->frame_bufs[i].ref_count = 0;
4108 }
RogerZhou86902d02018-02-28 15:29:16 -08004109 if (cm->seq_params.force_screen_content_tools) {
Hui Su2d5fd742018-02-21 18:10:37 -08004110 for (i = 0; i < FRAME_BUFFERS; ++i) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05304111 av1_hash_table_init(&pool->frame_bufs[i].hash_table, &cpi->td.mb);
Hui Su2d5fd742018-02-21 18:10:37 -08004112 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004113 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004114}
4115
Yaowu Xud3e7c682017-12-21 14:08:25 -08004116static void check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
Cheng Chen46f30c72017-09-07 11:13:33 -07004117 int subsampling_x, int subsampling_y) {
4118 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004119 SequenceHeader *const seq_params = &cm->seq_params;
Cheng Chen46f30c72017-09-07 11:13:33 -07004120
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004121 if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
4122 seq_params->subsampling_x != subsampling_x ||
4123 seq_params->subsampling_y != subsampling_y) {
4124 seq_params->subsampling_x = subsampling_x;
4125 seq_params->subsampling_y = subsampling_y;
4126 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07004127
4128 alloc_raw_frame_buffers(cpi);
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05304129 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07004130 alloc_util_frame_buffers(cpi);
4131
4132 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
4133
4134 cpi->initial_width = cm->width;
4135 cpi->initial_height = cm->height;
4136 cpi->initial_mbs = cm->MBs;
4137 }
4138}
4139
4140// Returns 1 if the assigned width or height was <= 0.
Marco Paniconi63971322019-08-15 21:32:05 -07004141int av1_set_size_literal(AV1_COMP *cpi, int width, int height) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004142 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004143 const int num_planes = av1_num_planes(cm);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004144 check_initial_width(cpi, cm->seq_params.use_highbitdepth,
4145 cm->seq_params.subsampling_x,
4146 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07004147
4148 if (width <= 0 || height <= 0) return 1;
4149
4150 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07004151 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004152
4153 if (cpi->initial_width && cpi->initial_height &&
4154 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
4155 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004156 av1_free_pc_tree(&cpi->td, num_planes);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004157 alloc_compressor_data(cpi);
4158 realloc_segmentation_maps(cpi);
4159 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07004160 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004161 update_frame_size(cpi);
4162
4163 return 0;
4164}
4165
David Turner475a3132019-01-18 15:17:17 +00004166void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07004167 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004168 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004169 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004170 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004171 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004172
Fergus Simpsonbc189932017-05-16 17:02:39 -07004173 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07004174 // There has been a change in the encoded frame size
Marco Paniconi63971322019-08-15 21:32:05 -07004175 av1_set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004176 set_mv_search_params(cpi);
Urvang Joshic8b52d52018-03-23 13:16:51 -07004177 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Cheng Chen09c83a52018-06-05 12:27:36 -07004178 cm->all_lossless = cm->coded_lossless && !av1_superres_scaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004179 }
4180
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004181 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004182 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004183 }
4184
David Turnere7ebf902018-12-04 14:04:55 +00004185 alloc_frame_mvs(cm, cm->cur_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004186
Cherma Rajan A71d20db2018-04-27 11:15:32 +05304187 // Allocate above context buffers
Cherma Rajan Af1479082018-05-09 14:26:34 +05304188 if (cm->num_allocated_above_context_planes < av1_num_planes(cm) ||
4189 cm->num_allocated_above_context_mi_col < cm->mi_cols ||
Cherma Rajan A71d20db2018-04-27 11:15:32 +05304190 cm->num_allocated_above_contexts < cm->tile_rows) {
4191 av1_free_above_context_buffers(cm, cm->num_allocated_above_contexts);
4192 if (av1_alloc_above_context_buffers(cm, cm->tile_rows))
4193 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
4194 "Failed to allocate context buffers");
4195 }
4196
Yaowu Xuc27fc142016-08-22 16:08:15 -07004197 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004198 if (aom_realloc_frame_buffer(
Jack Haughtonddb80602018-11-21 16:41:49 +00004199 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
4200 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +05304201 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07004202 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004203 "Failed to allocate frame buffer");
4204
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004205 const int frame_width = cm->superres_upscaled_width;
4206 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004207 set_restoration_unit_size(frame_width, frame_height,
4208 seq_params->subsampling_x,
4209 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004210 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01004211 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004212
4213 av1_alloc_restoration_buffers(cm);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004214 alloc_util_frame_buffers(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004215 init_motion_estimation(cpi);
4216
4217 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00004218 RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00004219 if (buf != NULL) {
David Turnera21966b2018-12-05 14:48:49 +00004220 struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
4221 av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
David Turner1bcefb32018-11-19 17:54:00 +00004222 buf->buf.y_crop_height, cm->width,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08004223 cm->height);
David Turnera21966b2018-12-05 14:48:49 +00004224 if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004225 }
4226 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07004227
Hui Su5ebd8702018-01-08 18:09:20 -08004228 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08004229 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004230
4231 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
4232}
4233
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004234static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
4235 // Choose an arbitrary random number
4236 static unsigned int seed = 56789;
4237 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004238 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4239 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004240
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07004241 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004242 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004243 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004244 case RESIZE_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00004245 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004246 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004247 else
Urvang Joshide71d142017-10-05 12:12:15 -07004248 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004249 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004250 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004251 default: assert(0);
4252 }
Urvang Joshide71d142017-10-05 12:12:15 -07004253 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004254}
4255
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004256#if CONFIG_SUPERRES_IN_RECODE
4257static int superres_in_recode_allowed(const AV1_COMP *const cpi) {
4258 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
4259 // Empirically found to not be beneficial for AOM_Q mode and images coding.
4260 return oxcf->superres_mode == SUPERRES_AUTO &&
4261 (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ) &&
4262 cpi->rc.frames_to_key > 1;
4263}
4264#endif // CONFIG_SUPERRES_IN_RECODE
4265
Urvang Joshi510d8f62019-01-10 12:11:50 -08004266#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO 0.012
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004267#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME 0.008
4268#define SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME 0.008
Urvang Joshi510d8f62019-01-10 12:11:50 -08004269#define SUPERRES_ENERGY_BY_AC_THRESH 0.2
4270
4271static double get_energy_by_q2_thresh(const GF_GROUP *gf_group,
4272 const RATE_CONTROL *rc) {
4273 // TODO(now): Return keyframe thresh * factor based on frame type / pyramid
4274 // level.
4275 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
4276 return SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME;
4277 } else if (gf_group->update_type[gf_group->index] == KF_UPDATE) {
4278 if (rc->frames_to_key <= 1)
4279 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO;
4280 else
4281 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME;
4282 } else {
4283 assert(0);
4284 }
4285 return 0;
4286}
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004287
4288static uint8_t get_superres_denom_from_qindex_energy(int qindex, double *energy,
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004289 double threshq,
4290 double threshp) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004291 const double q = av1_convert_qindex_to_q(qindex, AOM_BITS_8);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004292 const double tq = threshq * q * q;
4293 const double tp = threshp * energy[1];
4294 const double thresh = AOMMIN(tq, tp);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004295 int k;
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004296 for (k = SCALE_NUMERATOR * 2; k > SCALE_NUMERATOR; --k) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004297 if (energy[k - 1] > thresh) break;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004298 }
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004299 return 3 * SCALE_NUMERATOR - k;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004300}
4301
Urvang Joshi510d8f62019-01-10 12:11:50 -08004302static uint8_t get_superres_denom_for_qindex(const AV1_COMP *cpi, int qindex,
4303 int sr_kf, int sr_arf) {
4304 // Use superres for Key-frames and Alt-ref frames only.
4305 const GF_GROUP *gf_group = &cpi->gf_group;
4306 if (gf_group->update_type[gf_group->index] != KF_UPDATE &&
4307 gf_group->update_type[gf_group->index] != ARF_UPDATE) {
4308 return SCALE_NUMERATOR;
4309 }
4310 if (gf_group->update_type[gf_group->index] == KF_UPDATE && !sr_kf) {
4311 return SCALE_NUMERATOR;
4312 }
4313 if (gf_group->update_type[gf_group->index] == ARF_UPDATE && !sr_arf) {
4314 return SCALE_NUMERATOR;
4315 }
4316
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004317 double energy[16];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004318 analyze_hor_freq(cpi, energy);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004319
4320 const double energy_by_q2_thresh =
4321 get_energy_by_q2_thresh(gf_group, &cpi->rc);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004322 int denom = get_superres_denom_from_qindex_energy(
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004323 qindex, energy, energy_by_q2_thresh, SUPERRES_ENERGY_BY_AC_THRESH);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004324 /*
4325 printf("\nenergy = [");
4326 for (int k = 1; k < 16; ++k) printf("%f, ", energy[k]);
4327 printf("]\n");
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004328 printf("boost = %d\n",
4329 (gf_group->update_type[gf_group->index] == KF_UPDATE)
4330 ? cpi->rc.kf_boost
4331 : cpi->rc.gfu_boost);
4332 printf("denom = %d\n", denom);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004333 */
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004334#if CONFIG_SUPERRES_IN_RECODE
4335 if (superres_in_recode_allowed(cpi)) {
4336 // Force superres to be tried in the recode loop, as full-res is also going
4337 // to be tried anyway.
4338 denom = AOMMAX(denom, SCALE_NUMERATOR + 1);
4339 }
4340#endif // CONFIG_SUPERRES_IN_RECODE
Urvang Joshi510d8f62019-01-10 12:11:50 -08004341 return denom;
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004342}
4343
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004344static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
4345 // Choose an arbitrary random number
4346 static unsigned int seed = 34567;
4347 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004348 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4349 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07004350
4351 // Make sure that superres mode of the frame is consistent with the
4352 // sequence-level flag.
4353 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
4354 cpi->common.seq_params.enable_superres));
4355 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
4356 oxcf->superres_mode == SUPERRES_NONE));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004357
4358 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004359 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004360 case SUPERRES_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00004361 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004362 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004363 else
Urvang Joshide71d142017-10-05 12:12:15 -07004364 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004365 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004366 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004367 case SUPERRES_QTHRESH: {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07004368 // Do not use superres when screen content tools are used.
4369 if (cpi->common.allow_screen_content_tools) break;
Debargha Mukherjee2b2c5fd2018-11-14 13:21:24 -08004370 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4371 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004372
4373 // Now decide the use of superres based on 'q'.
Urvang Joshi2c92b072018-03-19 17:23:31 -07004374 int bottom_index, top_index;
4375 const int q = av1_rc_pick_q_and_bounds(
Angie Chiang958904c2019-09-19 16:03:35 -07004376 cpi, &cpi->rc, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
Jingning Han2e029872019-08-02 10:54:19 -07004377 &bottom_index, &top_index);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004378
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004379 const int qthresh = (frame_is_intra_only(&cpi->common))
4380 ? oxcf->superres_kf_qthresh
4381 : oxcf->superres_qthresh;
Urvang Joshib17e0a62019-01-11 16:11:54 -08004382 if (q <= qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07004383 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004384 } else {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004385 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004386 }
4387 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004388 }
Urvang Joshi36a83732019-01-31 15:31:57 -08004389 case SUPERRES_AUTO: {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004390 // Do not use superres when screen content tools are used.
Urvang Joshi36a83732019-01-31 15:31:57 -08004391 if (cpi->common.allow_screen_content_tools) break;
Urvang Joshi510d8f62019-01-10 12:11:50 -08004392 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4393 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi36a83732019-01-31 15:31:57 -08004394
4395 // Now decide the use of superres based on 'q'.
4396 int bottom_index, top_index;
4397 const int q = av1_rc_pick_q_and_bounds(
Angie Chiang958904c2019-09-19 16:03:35 -07004398 cpi, &cpi->rc, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
Jingning Han2e029872019-08-02 10:54:19 -07004399 &bottom_index, &top_index);
Urvang Joshi36a83732019-01-31 15:31:57 -08004400
4401 const int qthresh = 128;
4402 if (q <= qthresh) {
4403 new_denom = SCALE_NUMERATOR;
4404 } else {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004405 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Urvang Joshi36a83732019-01-31 15:31:57 -08004406 }
4407 break;
4408 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004409 default: assert(0);
4410 }
Urvang Joshide71d142017-10-05 12:12:15 -07004411 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004412}
4413
Urvang Joshide71d142017-10-05 12:12:15 -07004414static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4415 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4416}
4417
4418static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08004419 // Only need to check the width, as scaling is horizontal only.
4420 (void)oheight;
4421 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07004422}
4423
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004424static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004425 SUPERRES_MODE superres_mode, int owidth,
4426 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004427 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004428 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004429 }
4430
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004431 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07004432 int resize_denom =
4433 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4434 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4435
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004436 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004437 // Alter superres scale as needed to enforce conformity.
4438 rsz->superres_denom =
4439 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4440 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4441 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004442 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004443 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004444 // Alter resize scale as needed to enforce conformity.
4445 resize_denom =
4446 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004447 rsz->resize_width = owidth;
4448 rsz->resize_height = oheight;
4449 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004450 resize_denom);
4451 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4452 if (resize_denom > SCALE_NUMERATOR) {
4453 --resize_denom;
4454 rsz->resize_width = owidth;
4455 rsz->resize_height = oheight;
4456 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4457 resize_denom);
4458 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004459 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004460 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004461 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004462 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004463 if (resize_denom > rsz->superres_denom)
4464 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004465 else
Urvang Joshide71d142017-10-05 12:12:15 -07004466 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004467 rsz->resize_width = owidth;
4468 rsz->resize_height = oheight;
4469 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004470 resize_denom);
4471 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4472 (resize_denom > SCALE_NUMERATOR ||
4473 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08004474 } else { // We are allowed to alter neither resize scale nor superres
4475 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004476 return 0;
4477 }
Urvang Joshide71d142017-10-05 12:12:15 -07004478 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004479}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004480
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004481// Calculates resize and superres params for next frame
David Turner475a3132019-01-18 15:17:17 +00004482static size_params_type calculate_next_size_params(AV1_COMP *cpi) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004483 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08004484 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshi510d8f62019-01-10 12:11:50 -08004485 int resize_denom = SCALE_NUMERATOR;
Marco Paniconi63971322019-08-15 21:32:05 -07004486 if (oxcf->pass == 0 && cpi->use_svc &&
4487 cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1) {
4488 rsz.resize_width = cpi->common.width;
4489 rsz.resize_height = cpi->common.height;
4490 return rsz;
4491 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004492 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004493 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4494 rsz.resize_width = cpi->resize_pending_width;
4495 rsz.resize_height = cpi->resize_pending_height;
4496 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4497 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004498 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004499 rsz.resize_width = cpi->oxcf.width;
4500 rsz.resize_height = cpi->oxcf.height;
4501 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004502 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004503 }
Urvang Joshide71d142017-10-05 12:12:15 -07004504 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004505 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
4506 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004507 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004508 return rsz;
4509}
4510
Urvang Joshi22b150b2019-01-10 14:32:32 -08004511static void setup_frame_size_from_params(AV1_COMP *cpi,
4512 const size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004513 int encode_width = rsz->resize_width;
4514 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004515
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004516 AV1_COMMON *cm = &cpi->common;
4517 cm->superres_upscaled_width = encode_width;
4518 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004519 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004520 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4521 rsz->superres_denom);
David Turner475a3132019-01-18 15:17:17 +00004522 av1_set_frame_size(cpi, encode_width, encode_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004523}
4524
David Turnerdedd8ff2019-01-23 13:59:46 +00004525void av1_setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004526 AV1_COMMON *cm = &cpi->common;
Urvang Joshi22b150b2019-01-10 14:32:32 -08004527 // Reset superres params from previous frame.
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004528 cm->superres_scale_denominator = SCALE_NUMERATOR;
David Turner475a3132019-01-18 15:17:17 +00004529 const size_params_type rsz = calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004530 setup_frame_size_from_params(cpi, &rsz);
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004531
Yaowu Xu7e450882019-04-30 15:09:18 -07004532 assert(av1_is_min_tile_width_satisfied(cm));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004533}
4534
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004535static void superres_post_encode(AV1_COMP *cpi) {
4536 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004537 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004538
Cheng Chen09c83a52018-06-05 12:27:36 -07004539 if (!av1_superres_scaled(cm)) return;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004540
Urvang Joshid6b5d512018-03-20 13:34:38 -07004541 assert(cpi->oxcf.enable_superres);
4542 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshic8b52d52018-03-23 13:16:51 -07004543 assert(!cm->all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07004544
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004545 av1_superres_upscale(cm, NULL);
4546
4547 // If regular resizing is occurring the source will need to be downscaled to
4548 // match the upscaled superres resolution. Otherwise the original source is
4549 // used.
Cheng Chen09c83a52018-06-05 12:27:36 -07004550 if (!av1_resize_scaled(cm)) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004551 cpi->source = cpi->unscaled_source;
4552 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4553 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004554 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4555 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08004556 // Do downscale. cm->(width|height) has been updated by
4557 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004558 if (aom_realloc_frame_buffer(
4559 &cpi->scaled_source, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004560 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4561 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
4562 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004563 aom_internal_error(
4564 &cm->error, AOM_CODEC_MEM_ERROR,
4565 "Failed to reallocate scaled source buffer for superres");
4566 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4567 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004568 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004569 (int)cm->seq_params.bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004570 cpi->source = &cpi->scaled_source;
4571 }
4572}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004573
Yue Chen92271e12019-07-09 16:28:02 -07004574static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
4575 MACROBLOCKD *xd, int use_restoration,
4576 int use_cdef) {
logangwf95c9162019-02-20 12:02:32 -08004577 if (use_restoration)
David Turnerc29e1a92018-12-06 14:10:14 +00004578 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004579
logangwf95c9162019-02-20 12:02:32 -08004580 if (use_cdef) {
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004581#if CONFIG_COLLECT_COMPONENT_TIMING
4582 start_timing(cpi, cdef_time);
4583#endif
Steinar Midtskogen59782122017-07-20 08:49:43 +02004584 // Find CDEF parameters
David Turnerc29e1a92018-12-06 14:10:14 +00004585 av1_cdef_search(&cm->cur_frame->buf, cpi->source, cm, xd,
Hui Sub93880a2019-06-27 18:29:30 -07004586 cpi->sf.cdef_pick_method, cpi->td.mb.rdmult);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004587
4588 // Apply the filter
David Turnerc29e1a92018-12-06 14:10:14 +00004589 av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004590#if CONFIG_COLLECT_COMPONENT_TIMING
4591 end_timing(cpi, cdef_time);
4592#endif
logangwf95c9162019-02-20 12:02:32 -08004593 } else {
4594 cm->cdef_info.cdef_bits = 0;
4595 cm->cdef_info.cdef_strengths[0] = 0;
4596 cm->cdef_info.nb_cdef_strengths = 1;
4597 cm->cdef_info.cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004598 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004599
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004600 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004601
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004602#if CONFIG_COLLECT_COMPONENT_TIMING
4603 start_timing(cpi, loop_restoration_time);
4604#endif
logangwf95c9162019-02-20 12:02:32 -08004605 if (use_restoration) {
David Turnerc29e1a92018-12-06 14:10:14 +00004606 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004607 av1_pick_filter_restoration(cpi->source, cpi);
4608 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4609 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4610 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304611 if (cpi->num_workers > 1)
David Turnerc29e1a92018-12-06 14:10:14 +00004612 av1_loop_restoration_filter_frame_mt(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304613 cpi->workers, cpi->num_workers,
4614 &cpi->lr_row_sync, &cpi->lr_ctxt);
4615 else
David Turnerc29e1a92018-12-06 14:10:14 +00004616 av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304617 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004618 }
logangwf95c9162019-02-20 12:02:32 -08004619 } else {
4620 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4621 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4622 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004623 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004624#if CONFIG_COLLECT_COMPONENT_TIMING
4625 end_timing(cpi, loop_restoration_time);
4626#endif
Fergus Simpsonbc189932017-05-16 17:02:39 -07004627}
4628
Yue Chen92271e12019-07-09 16:28:02 -07004629static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
4630 const int num_planes = av1_num_planes(cm);
4631 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4632
4633 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
4634 cm->coded_lossless && cm->all_lossless));
4635
4636 const int use_loopfilter = !cm->coded_lossless && !cm->large_scale_tile;
4637 const int use_cdef = cm->seq_params.enable_cdef && !cm->coded_lossless &&
4638 !cm->large_scale_tile;
4639 const int use_restoration = cm->seq_params.enable_restoration &&
4640 !cm->all_lossless && !cm->large_scale_tile;
4641
4642 struct loopfilter *lf = &cm->lf;
4643
4644#if CONFIG_COLLECT_COMPONENT_TIMING
4645 start_timing(cpi, loop_filter_time);
4646#endif
4647 if (use_loopfilter) {
4648 aom_clear_system_state();
4649 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
4650 } else {
4651 lf->filter_level[0] = 0;
4652 lf->filter_level[1] = 0;
4653 }
4654
4655 if (lf->filter_level[0] || lf->filter_level[1]) {
4656 if (cpi->num_workers > 1)
4657 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
4658#if CONFIG_LPF_MASK
4659 0,
4660#endif
4661 cpi->workers, cpi->num_workers,
4662 &cpi->lf_row_sync);
4663 else
4664 av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
4665#if CONFIG_LPF_MASK
4666 0,
4667#endif
4668 0, num_planes, 0);
4669 }
4670#if CONFIG_COLLECT_COMPONENT_TIMING
4671 end_timing(cpi, loop_filter_time);
4672#endif
4673
4674 cdef_restoration_frame(cpi, cm, xd, use_restoration, use_cdef);
4675}
4676
David Turnerf2b334c2018-12-13 13:00:55 +00004677static void fix_interp_filter(InterpFilter *const interp_filter,
4678 const FRAME_COUNTS *const counts) {
4679 if (*interp_filter == SWITCHABLE) {
4680 // Check to see if only one of the filters is actually used
4681 int count[SWITCHABLE_FILTERS] = { 0 };
4682 int num_filters_used = 0;
4683 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4684 for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
4685 count[i] += counts->switchable_interp[j][i];
4686 num_filters_used += (count[i] > 0);
4687 }
4688 if (num_filters_used == 1) {
4689 // Only one filter is used. So set the filter at frame level
4690 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4691 if (count[i]) {
4692 if (i == EIGHTTAP_REGULAR) *interp_filter = i;
4693 break;
4694 }
4695 }
4696 }
4697 }
4698}
4699
David Turner996b2c12018-12-07 15:52:30 +00004700static void finalize_encoded_frame(AV1_COMP *const cpi) {
4701 AV1_COMMON *const cm = &cpi->common;
David Turner99e990e2018-12-10 12:54:26 +00004702 CurrentFrame *const current_frame = &cm->current_frame;
David Turner996b2c12018-12-07 15:52:30 +00004703
David Turner99e990e2018-12-10 12:54:26 +00004704 if (!cm->seq_params.reduced_still_picture_hdr &&
4705 encode_show_existing_frame(cm)) {
4706 RefCntBuffer *const frame_to_show =
4707 cm->ref_frame_map[cpi->existing_fb_idx_to_show];
4708
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004709 if (frame_to_show == NULL) {
David Turner99e990e2018-12-10 12:54:26 +00004710 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4711 "Buffer does not contain a reconstructed frame");
4712 }
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004713 assert(frame_to_show->ref_count > 0);
David Turner99e990e2018-12-10 12:54:26 +00004714 assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
David Turner99e990e2018-12-10 12:54:26 +00004715 }
David Turner08f909c2018-12-18 13:29:14 +00004716
4717 if (!encode_show_existing_frame(cm) &&
4718 cm->seq_params.film_grain_params_present &&
4719 (cm->show_frame || cm->showable_frame)) {
4720 // Copy the current frame's film grain params to the its corresponding
4721 // RefCntBuffer slot.
4722 cm->cur_frame->film_grain_params = cm->film_grain_params;
4723
4724 // We must update the parameters if this is not an INTER_FRAME
4725 if (current_frame->frame_type != INTER_FRAME)
4726 cm->cur_frame->film_grain_params.update_parameters = 1;
4727
4728 // Iterate the random seed for the next frame.
4729 cm->film_grain_params.random_seed += 3381;
4730 if (cm->film_grain_params.random_seed == 0)
4731 cm->film_grain_params.random_seed = 7391;
4732 }
David Turnerf2b334c2018-12-13 13:00:55 +00004733
4734 // Initialise all tiles' contexts from the global frame context
4735 for (int tile_col = 0; tile_col < cm->tile_cols; tile_col++) {
4736 for (int tile_row = 0; tile_row < cm->tile_rows; tile_row++) {
4737 const int tile_idx = tile_row * cm->tile_cols + tile_col;
4738 cpi->tile_data[tile_idx].tctx = *cm->fc;
4739 }
4740 }
4741
4742 fix_interp_filter(&cm->interp_filter, cpi->td.counts);
David Turner996b2c12018-12-07 15:52:30 +00004743}
4744
Urvang Joshi57643372019-02-21 11:10:57 -08004745static int get_regulated_q_overshoot(AV1_COMP *const cpi, int q_low, int q_high,
4746 int top_index, int bottom_index) {
4747 const AV1_COMMON *const cm = &cpi->common;
4748 const RATE_CONTROL *const rc = &cpi->rc;
4749
4750 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4751
4752 int q_regulated =
4753 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4754 AOMMAX(q_high, top_index), cm->width, cm->height);
4755
4756 int retries = 0;
4757 while (q_regulated < q_low && retries < 10) {
4758 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4759 q_regulated =
4760 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4761 AOMMAX(q_high, top_index), cm->width, cm->height);
4762 retries++;
4763 }
4764 return q_regulated;
4765}
4766
4767static int get_regulated_q_undershoot(AV1_COMP *const cpi, int q_high,
4768 int top_index, int bottom_index) {
4769 const AV1_COMMON *const cm = &cpi->common;
4770 const RATE_CONTROL *const rc = &cpi->rc;
4771
4772 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4773 int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4774 top_index, cm->width, cm->height);
4775
4776 int retries = 0;
4777 while (q_regulated > q_high && retries < 10) {
4778 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4779 q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4780 top_index, cm->width, cm->height);
4781 retries++;
4782 }
4783 return q_regulated;
4784}
4785
David Turner2f3b5df2019-01-02 14:30:50 +00004786// Called after encode_with_recode_loop() has just encoded a frame and packed
4787// its bitstream. This function works out whether we under- or over-shot
4788// our bitrate target and adjusts q as appropriate. Also decides whether
4789// or not we should do another recode loop, indicated by *loop
Hui Suef139e12019-05-20 15:51:22 -07004790static void recode_loop_update_q(
4791 AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low,
4792 int *const q_high, const int top_index, const int bottom_index,
4793 int *const undershoot_seen, int *const overshoot_seen,
4794 int *const low_cr_seen, const int loop_at_this_size) {
David Turner2f3b5df2019-01-02 14:30:50 +00004795 AV1_COMMON *const cm = &cpi->common;
4796 RATE_CONTROL *const rc = &cpi->rc;
4797
Hui Suef139e12019-05-20 15:51:22 -07004798 const int min_cr = cpi->oxcf.min_cr;
4799 if (min_cr > 0) {
4800 aom_clear_system_state();
4801 const double compression_ratio =
4802 av1_get_compression_ratio(cm, rc->projected_frame_size >> 3);
4803 const double target_cr = min_cr / 100.0;
4804 if (compression_ratio < target_cr) {
4805 *low_cr_seen = 1;
4806 if (*q < rc->worst_quality) {
4807 const double cr_ratio = target_cr / compression_ratio;
4808 const int projected_q = AOMMAX(*q + 1, (int)(*q * cr_ratio * cr_ratio));
4809 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->worst_quality);
4810 *q_low = AOMMAX(*q, *q_low);
4811 *q_high = AOMMAX(*q, *q_high);
4812 *loop = 1;
4813 }
4814 }
4815 if (*low_cr_seen) return;
4816 }
4817
4818 if (cpi->oxcf.rc_mode == AOM_Q) return;
4819
Hui Su01dfe032019-09-11 14:10:42 -07004820 const int last_q = *q;
David Turner2f3b5df2019-01-02 14:30:50 +00004821 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
4822 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4823 &frame_under_shoot_limit,
4824 &frame_over_shoot_limit);
4825 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4826
Hui Su01dfe032019-09-11 14:10:42 -07004827 if (cm->current_frame.frame_type == KEY_FRAME && rc->this_key_frame_forced &&
4828 rc->projected_frame_size < rc->max_frame_bandwidth) {
David Turner2f3b5df2019-01-02 14:30:50 +00004829 int64_t kf_err;
Hui Su01dfe032019-09-11 14:10:42 -07004830 const int64_t high_err_target = cpi->ambient_err;
4831 const int64_t low_err_target = cpi->ambient_err >> 1;
David Turner2f3b5df2019-01-02 14:30:50 +00004832
Jerome Jiangfa1d1732019-08-06 10:31:20 -07004833#if CONFIG_AV1_HIGHBITDEPTH
David Turner2f3b5df2019-01-02 14:30:50 +00004834 if (cm->seq_params.use_highbitdepth) {
4835 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
4836 } else {
4837 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
4838 }
Jerome Jiangfa1d1732019-08-06 10:31:20 -07004839#else
4840 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
4841#endif
David Turner2f3b5df2019-01-02 14:30:50 +00004842 // Prevent possible divide by zero error below for perfect KF
4843 kf_err += !kf_err;
4844
4845 // The key frame is not good enough or we can afford
4846 // to make it better without undue risk of popping.
4847 if ((kf_err > high_err_target &&
4848 rc->projected_frame_size <= frame_over_shoot_limit) ||
4849 (kf_err > low_err_target &&
4850 rc->projected_frame_size <= frame_under_shoot_limit)) {
4851 // Lower q_high
Hui Su01dfe032019-09-11 14:10:42 -07004852 *q_high = AOMMAX(*q - 1, *q_low);
David Turner2f3b5df2019-01-02 14:30:50 +00004853
4854 // Adjust Q
4855 *q = (int)((*q * high_err_target) / kf_err);
4856 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
4857 } else if (kf_err < low_err_target &&
4858 rc->projected_frame_size >= frame_under_shoot_limit) {
4859 // The key frame is much better than the previous frame
4860 // Raise q_low
Hui Su01dfe032019-09-11 14:10:42 -07004861 *q_low = AOMMIN(*q + 1, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00004862
4863 // Adjust Q
4864 *q = (int)((*q * low_err_target) / kf_err);
4865 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
4866 }
4867
4868 // Clamp Q to upper and lower limits:
4869 *q = clamp(*q, *q_low, *q_high);
Hui Su01dfe032019-09-11 14:10:42 -07004870 *loop = (*q != last_q);
4871 return;
4872 }
David Turner2f3b5df2019-01-02 14:30:50 +00004873
Hui Su01dfe032019-09-11 14:10:42 -07004874 if (recode_loop_test(cpi, frame_over_shoot_limit, frame_under_shoot_limit, *q,
4875 AOMMAX(*q_high, top_index), bottom_index)) {
David Turner2f3b5df2019-01-02 14:30:50 +00004876 // Is the projected frame size out of range and are we allowed
4877 // to attempt to recode.
David Turner2f3b5df2019-01-02 14:30:50 +00004878
4879 // Frame size out of permitted range:
4880 // Update correction factor & compute new Q to try...
4881 // Frame is too large
4882 if (rc->projected_frame_size > rc->this_frame_target) {
4883 // Special case if the projected size is > the max allowed.
Urvang Joshi7344d3e2019-07-25 17:24:57 -07004884 if (*q == *q_high &&
4885 rc->projected_frame_size >= rc->max_frame_bandwidth) {
4886 const double q_val_high_current =
4887 av1_convert_qindex_to_q(*q_high, cm->seq_params.bit_depth);
4888 const double q_val_high_new =
4889 q_val_high_current *
4890 ((double)rc->projected_frame_size / rc->max_frame_bandwidth);
4891 *q_high = av1_find_qindex(q_val_high_new, cm->seq_params.bit_depth,
4892 rc->best_quality, rc->worst_quality);
4893 }
David Turner2f3b5df2019-01-02 14:30:50 +00004894
4895 // Raise Qlow as to at least the current value
Hui Su01dfe032019-09-11 14:10:42 -07004896 *q_low = AOMMIN(*q + 1, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00004897
Urvang Joshi57643372019-02-21 11:10:57 -08004898 if (*undershoot_seen || loop_at_this_size > 2 ||
4899 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00004900 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4901
4902 *q = (*q_high + *q_low + 1) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08004903 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
4904 const int q_mid = (*q_high + *q_low + 1) / 2;
4905 const int q_regulated = get_regulated_q_overshoot(
4906 cpi, *q_low, *q_high, top_index, bottom_index);
4907 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
4908 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
4909 *q = (q_mid + q_regulated + 1) / 2;
David Turner2f3b5df2019-01-02 14:30:50 +00004910 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08004911 *q = get_regulated_q_overshoot(cpi, *q_low, *q_high, top_index,
4912 bottom_index);
David Turner2f3b5df2019-01-02 14:30:50 +00004913 }
4914
4915 *overshoot_seen = 1;
4916 } else {
4917 // Frame is too small
Hui Su01dfe032019-09-11 14:10:42 -07004918 *q_high = AOMMAX(*q - 1, *q_low);
David Turner2f3b5df2019-01-02 14:30:50 +00004919
Urvang Joshi57643372019-02-21 11:10:57 -08004920 if (*overshoot_seen || loop_at_this_size > 2 ||
4921 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00004922 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4923 *q = (*q_high + *q_low) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08004924 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
4925 const int q_mid = (*q_high + *q_low) / 2;
4926 const int q_regulated =
4927 get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
4928 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
4929 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
4930 *q = (q_mid + q_regulated) / 2;
4931
4932 // Special case reset for qlow for constrained quality.
4933 // This should only trigger where there is very substantial
4934 // undershoot on a frame and the auto cq level is above
4935 // the user passsed in value.
4936 if (cpi->oxcf.rc_mode == AOM_CQ && q_regulated < *q_low) {
4937 *q_low = *q;
4938 }
David Turner2f3b5df2019-01-02 14:30:50 +00004939 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08004940 *q = get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
4941
David Turner2f3b5df2019-01-02 14:30:50 +00004942 // Special case reset for qlow for constrained quality.
4943 // This should only trigger where there is very substantial
4944 // undershoot on a frame and the auto cq level is above
4945 // the user passsed in value.
4946 if (cpi->oxcf.rc_mode == AOM_CQ && *q < *q_low) {
4947 *q_low = *q;
4948 }
David Turner2f3b5df2019-01-02 14:30:50 +00004949 }
4950
4951 *undershoot_seen = 1;
4952 }
4953
4954 // Clamp Q to upper and lower limits:
4955 *q = clamp(*q, *q_low, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00004956 }
Hui Su01dfe032019-09-11 14:10:42 -07004957
4958 *loop = (*q != last_q);
David Turner2f3b5df2019-01-02 14:30:50 +00004959}
4960
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304961static int get_interp_filter_selected(const AV1_COMMON *const cm,
4962 MV_REFERENCE_FRAME ref,
4963 InterpFilter ifilter) {
4964 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
4965 if (buf == NULL) return 0;
4966 return buf->interp_filter_selected[ifilter];
4967}
4968
Satish Kumar Suman0389a492019-07-31 13:35:58 +05304969static uint16_t setup_interp_filter_search_mask(AV1_COMP *cpi) {
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304970 const AV1_COMMON *const cm = &cpi->common;
4971 int ref_total[REF_FRAMES] = { 0 };
Satish Kumar Suman0389a492019-07-31 13:35:58 +05304972 uint16_t mask = ALLOW_ALL_INTERP_FILT_MASK;
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304973
4974 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
Satish Kumar Suman0389a492019-07-31 13:35:58 +05304975 return mask;
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304976
4977 for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
4978 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
4979 ++ifilter) {
4980 ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter);
4981 }
4982 }
4983 int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] +
4984 ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] +
4985 ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]);
4986
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304987 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
4988 ++ifilter) {
4989 int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30;
4990 if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) {
4991 int filter_score =
4992 get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 +
4993 get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 +
4994 get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 +
4995 get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 +
4996 get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 +
4997 get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10;
Satish Kumar Suman0389a492019-07-31 13:35:58 +05304998 if (filter_score < ref_total_total) {
4999 DUAL_FILTER_TYPE filt_type = ifilter + SWITCHABLE_FILTERS * ifilter;
5000 reset_interp_filter_allowed_mask(&mask, filt_type);
5001 }
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305002 }
5003 }
5004 return mask;
5005}
5006
Tom Finegane4099e32018-01-23 12:01:51 -08005007static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005008 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005009 RATE_CONTROL *const rc = &cpi->rc;
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005010 const int allow_recode = (cpi->sf.recode_loop != DISALLOW_RECODE);
Hui Sua1d71842019-07-31 12:02:24 -07005011 // Must allow recode if minimum compression ratio is set.
5012 assert(IMPLIES(cpi->oxcf.min_cr > 0, allow_recode));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005013
5014 set_size_independent_vars(cpi);
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305015 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
5016 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07005017 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07005018
David Turnerdedd8ff2019-01-23 13:59:46 +00005019 av1_setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005020
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005021#if CONFIG_SUPERRES_IN_RECODE
5022 if (superres_in_recode_allowed(cpi) &&
5023 cm->superres_scale_denominator == SCALE_NUMERATOR) {
5024 // Superres won't be picked, so no need to try, as we will go through
5025 // another recode loop for full-resolution after this anyway.
5026 return -1;
5027 }
5028#endif // CONFIG_SUPERRES_IN_RECODE
5029
David Turner2f3b5df2019-01-02 14:30:50 +00005030 int top_index = 0, bottom_index = 0;
5031 int q = 0, q_low = 0, q_high = 0;
5032 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
5033 q_low = bottom_index;
5034 q_high = top_index;
5035
Yunqing Wang80a7af62019-08-22 16:07:44 -07005036 if (cpi->sf.tx_type_search.prune_tx_type_using_stats &&
5037 cm->current_frame.frame_type == KEY_FRAME) {
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005038 av1_copy(cpi->tx_type_probs, default_tx_type_probs);
5039
Yunqing Wangeb7c2182019-08-22 18:15:48 -07005040 int thr[2][2] = { { 15, 10 }, { 17, 10 } };
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005041 for (int f = 0; f < FRAME_UPDATE_TYPES; f++) {
Yunqing Wangeb7c2182019-08-22 18:15:48 -07005042 int kf_arf_update = (f == KF_UPDATE || f == ARF_UPDATE);
5043 cpi->tx_type_probs_thresh[f] =
5044 thr[cpi->sf.tx_type_search.prune_tx_type_using_stats - 1]
5045 [kf_arf_update];
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005046 }
5047 }
5048
Yunqing Wang5f74dc22019-10-29 10:35:20 -07005049 if (cpi->sf.prune_obmc_using_stats &&
5050 cm->current_frame.frame_type == KEY_FRAME) {
5051 av1_copy(cpi->obmc_probs, default_obmc_probs);
5052 cpi->obmc_probs_thresh = 16;
5053 }
5054
David Turner2f3b5df2019-01-02 14:30:50 +00005055 // Loop variables
5056 int loop_count = 0;
5057 int loop_at_this_size = 0;
5058 int loop = 0;
5059 int overshoot_seen = 0;
5060 int undershoot_seen = 0;
Hui Suef139e12019-05-20 15:51:22 -07005061 int low_cr_seen = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005062
5063#if CONFIG_COLLECT_COMPONENT_TIMING
5064 printf("\n Encoding a frame:");
5065#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005066 do {
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005067 loop = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005068 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005069
Urvang Joshif1fa6862018-01-08 16:39:33 -08005070 // if frame was scaled calculate global_motion_search again if already
5071 // done
David Turner2f3b5df2019-01-02 14:30:50 +00005072 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005073 if (cpi->source->y_crop_width != cm->width ||
David Turner2f3b5df2019-01-02 14:30:50 +00005074 cpi->source->y_crop_height != cm->height) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005075 cpi->global_motion_search_done = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00005076 }
5077 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005078 cpi->source =
5079 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
David Turner2f3b5df2019-01-02 14:30:50 +00005080 if (cpi->unscaled_last_source != NULL) {
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07005081 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
5082 &cpi->scaled_last_source);
David Turner2f3b5df2019-01-02 14:30:50 +00005083 }
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07005084
David Turner2f3b5df2019-01-02 14:30:50 +00005085 if (!frame_is_intra_only(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005086 if (loop_count > 0) {
5087 release_scaled_references(cpi);
5088 }
Cheng Chen46f30c72017-09-07 11:13:33 -07005089 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005090 }
Yaowu Xuf883b422016-08-30 14:01:10 -07005091 av1_set_quantizer(cm, q);
Fyodor Kyslov25daf5d2019-10-23 10:33:26 -07005092 if (cpi->oxcf.deltaq_mode != NO_DELTA_Q) av1_init_quantizer(cpi);
kyslov7b9d0d62018-12-21 11:12:26 -08005093
5094 av1_set_variance_partition_thresholds(cpi, q, 0);
5095
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08005096 // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
5097 // cm->current_frame.frame_number, cm->show_frame, q,
5098 // cm->current_frame.frame_type, cm->superres_scale_denominator);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005099
David Turner2f3b5df2019-01-02 14:30:50 +00005100 if (loop_count == 0) {
5101 setup_frame(cpi);
5102 } else if (get_primary_ref_frame_buf(cm) == NULL) {
5103 // Base q-index may have changed, so we need to assign proper default coef
5104 // probs before every iteration.
Yaowu Xuf883b422016-08-30 14:01:10 -07005105 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08005106 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00005107 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005108
Yaowu Xuc27fc142016-08-22 16:08:15 -07005109 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005110 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005111 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005112 av1_setup_in_frame_q_adj(cpi);
David Turner2f3b5df2019-01-02 14:30:50 +00005113 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !allow_recode) {
5114 suppress_active_map(cpi);
5115 av1_cyclic_refresh_setup(cpi);
5116 apply_active_map(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005117 }
David Turner2f3b5df2019-01-02 14:30:50 +00005118
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005119 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00005120 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005121 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
Jerome Jiangb23571d2019-10-23 14:15:55 -07005122 cm->seg.enabled = cm->prev_frame->seg.enabled;
David Barker11c93562018-06-05 12:00:07 +01005123 } else {
Yaowu Xu7e450882019-04-30 15:09:18 -07005124 av1_calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07005125 }
David Barkercab37552018-03-21 11:56:24 +00005126 } else {
5127 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005128 }
David Barkercab37552018-03-21 11:56:24 +00005129 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Jerome Jiangb23571d2019-10-23 14:15:55 -07005130 cm->cur_frame->seg.enabled = cm->seg.enabled;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005131
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005132#if CONFIG_COLLECT_COMPONENT_TIMING
5133 start_timing(cpi, av1_encode_frame_time);
5134#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005135 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07005136 av1_encode_frame(cpi);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005137#if CONFIG_COLLECT_COMPONENT_TIMING
5138 end_timing(cpi, av1_encode_frame_time);
5139#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005140
Yaowu Xuf883b422016-08-30 14:01:10 -07005141 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005142
5143 // Dummy pack of the bitstream using up to date stats to get an
5144 // accurate estimate of output frame size to determine if we need
5145 // to recode.
Hui Su7e3eeaa2019-09-11 15:50:41 -07005146 const int do_dummy_pack = (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
5147 cpi->oxcf.rc_mode != AOM_Q) ||
5148 cpi->oxcf.min_cr > 0;
Hui Sua1d71842019-07-31 12:02:24 -07005149 if (do_dummy_pack) {
David Turner996b2c12018-12-07 15:52:30 +00005150 finalize_encoded_frame(cpi);
David Turner35cba132018-12-10 15:48:15 +00005151 int largest_tile_id = 0; // Output from bitstream: unused here
5152 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08005153 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005154
5155 rc->projected_frame_size = (int)(*size) << 3;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005156 }
5157
Hui Suef139e12019-05-20 15:51:22 -07005158 if (allow_recode) {
David Turner2f3b5df2019-01-02 14:30:50 +00005159 // Update q and decide whether to do a recode loop
5160 recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
5161 bottom_index, &undershoot_seen, &overshoot_seen,
Hui Suef139e12019-05-20 15:51:22 -07005162 &low_cr_seen, loop_at_this_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005163 }
5164
5165 // Special case for overlay frame.
Hui Su7e3eeaa2019-09-11 15:50:41 -07005166 if (loop && rc->is_src_frame_alt_ref &&
5167 rc->projected_frame_size < rc->max_frame_bandwidth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005168 loop = 0;
Hui Su7e3eeaa2019-09-11 15:50:41 -07005169 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005170
David Turner2f3b5df2019-01-02 14:30:50 +00005171 if (allow_recode && !cpi->sf.gm_disable_recode &&
5172 recode_loop_test_global_motion(cpi)) {
5173 loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005174 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005175
Jerome Jiangd413f4a2019-09-23 14:53:03 -07005176#if !CONFIG_REALTIME_ONLY
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005177 if (cpi->tpl_model_pass == 1) {
5178 assert(cpi->oxcf.enable_tpl_model == 2);
Debargha Mukherjee347c64d2019-05-08 13:53:46 -07005179 av1_tpl_setup_forward_stats(cpi);
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005180 cpi->tpl_model_pass = 0;
5181 loop = 1;
5182 }
Jerome Jiangd413f4a2019-09-23 14:53:03 -07005183#endif
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005184
Yaowu Xuc27fc142016-08-22 16:08:15 -07005185 if (loop) {
5186 ++loop_count;
5187 ++loop_at_this_size;
5188
5189#if CONFIG_INTERNAL_STATS
5190 ++cpi->tot_recode_hits;
5191#endif
5192 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005193#if CONFIG_COLLECT_COMPONENT_TIMING
5194 if (loop) printf("\n Recoding:");
5195#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005196 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08005197
Marco Paniconi574e59e2019-10-23 15:47:28 -07005198 // Update some stats from cyclic refresh.
5199 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
5200 !frame_is_intra_only(cm))
5201 av1_cyclic_refresh_postencode(cpi);
5202
Tom Finegane4099e32018-01-23 12:01:51 -08005203 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005204}
5205
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005206static int encode_with_recode_loop_and_filter(AV1_COMP *cpi, size_t *size,
5207 uint8_t *dest, int64_t *sse,
5208 int64_t *rate,
5209 int *largest_tile_id) {
5210#if CONFIG_COLLECT_COMPONENT_TIMING
5211 start_timing(cpi, encode_with_recode_loop_time);
5212#endif
5213 int err = encode_with_recode_loop(cpi, size, dest);
5214#if CONFIG_COLLECT_COMPONENT_TIMING
5215 end_timing(cpi, encode_with_recode_loop_time);
5216#endif
5217 if (err != AOM_CODEC_OK) {
5218 if (err == -1) {
5219 // special case as described in encode_with_recode_loop().
5220 // Encoding was skipped.
5221 err = AOM_CODEC_OK;
5222 if (sse != NULL) *sse = INT64_MAX;
5223 if (rate != NULL) *rate = INT64_MAX;
5224 *largest_tile_id = 0;
5225 }
5226 return err;
5227 }
5228
5229#ifdef OUTPUT_YUV_SKINMAP
5230 if (cpi->common.current_frame.frame_number > 1) {
5231 av1_compute_skin_map(cpi, yuv_skinmap_file);
5232 }
5233#endif // OUTPUT_YUV_SKINMAP
5234
5235 AV1_COMMON *const cm = &cpi->common;
5236 SequenceHeader *const seq_params = &cm->seq_params;
5237
5238 // Special case code to reduce pulsing when key frames are forced at a
5239 // fixed interval. Note the reconstruction error if it is the frame before
5240 // the force key frame
5241 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005242#if CONFIG_AV1_HIGHBITDEPTH
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005243 if (seq_params->use_highbitdepth) {
5244 cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
5245 } else {
5246 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5247 }
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005248#else
5249 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5250#endif
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005251 }
5252
5253 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
5254 cm->cur_frame->buf.transfer_characteristics =
5255 seq_params->transfer_characteristics;
5256 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
5257 cm->cur_frame->buf.monochrome = seq_params->monochrome;
5258 cm->cur_frame->buf.chroma_sample_position =
5259 seq_params->chroma_sample_position;
5260 cm->cur_frame->buf.color_range = seq_params->color_range;
5261 cm->cur_frame->buf.render_width = cm->render_width;
5262 cm->cur_frame->buf.render_height = cm->render_height;
5263
5264 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5265 // off.
5266
5267 // Pick the loop filter level for the frame.
5268 if (!cm->allow_intrabc) {
5269 loopfilter_frame(cpi, cm);
5270 } else {
5271 cm->lf.filter_level[0] = 0;
5272 cm->lf.filter_level[1] = 0;
5273 cm->cdef_info.cdef_bits = 0;
5274 cm->cdef_info.cdef_strengths[0] = 0;
5275 cm->cdef_info.nb_cdef_strengths = 1;
5276 cm->cdef_info.cdef_uv_strengths[0] = 0;
5277 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5278 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5279 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
5280 }
5281
5282 // TODO(debargha): Fix mv search range on encoder side
5283 // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5284 aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5285
5286#ifdef OUTPUT_YUV_REC
5287 aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
5288#endif
5289
5290 finalize_encoded_frame(cpi);
5291 // Build the bitstream
5292#if CONFIG_COLLECT_COMPONENT_TIMING
5293 start_timing(cpi, av1_pack_bitstream_final_time);
5294#endif
5295 if (av1_pack_bitstream(cpi, dest, size, largest_tile_id) != AOM_CODEC_OK)
5296 return AOM_CODEC_ERROR;
5297#if CONFIG_COLLECT_COMPONENT_TIMING
5298 end_timing(cpi, av1_pack_bitstream_final_time);
5299#endif
5300
5301 // Compute sse and rate.
5302 if (sse != NULL) {
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005303#if CONFIG_AV1_HIGHBITDEPTH
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005304 *sse = (seq_params->use_highbitdepth)
5305 ? aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf)
5306 : aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005307#else
5308 *sse = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5309#endif
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005310 }
5311 if (rate != NULL) {
5312 const int64_t bits = (*size << 3);
5313 *rate = (bits << 5); // To match scale.
5314 }
5315 return AOM_CODEC_OK;
5316}
5317
5318#if CONFIG_SUPERRES_IN_RECODE
5319
5320static void save_cur_buf(AV1_COMP *cpi) {
5321 CODING_CONTEXT *const cc = &cpi->coding_context;
5322 AV1_COMMON *cm = &cpi->common;
5323 const YV12_BUFFER_CONFIG *ybf = &cm->cur_frame->buf;
5324 memset(&cc->copy_buffer, 0, sizeof(cc->copy_buffer));
5325 if (aom_alloc_frame_buffer(&cc->copy_buffer, ybf->y_crop_width,
5326 ybf->y_crop_height, ybf->subsampling_x,
5327 ybf->subsampling_y,
5328 ybf->flags & YV12_FLAG_HIGHBITDEPTH, ybf->border,
5329 cm->byte_alignment) != AOM_CODEC_OK) {
5330 aom_internal_error(
5331 &cm->error, AOM_CODEC_MEM_ERROR,
5332 "Failed to allocate copy buffer for saving coding context");
5333 }
5334 aom_yv12_copy_frame(ybf, &cc->copy_buffer, av1_num_planes(cm));
5335}
5336
5337// Coding context that only needs to be saved when recode loop includes
5338// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5339// restoraton).
5340static void save_extra_coding_context(AV1_COMP *cpi) {
5341 CODING_CONTEXT *const cc = &cpi->coding_context;
5342 AV1_COMMON *cm = &cpi->common;
5343
5344 cc->lf = cm->lf;
5345 cc->cdef_info = cm->cdef_info;
5346 cc->rc = cpi->rc;
5347}
5348
5349static void save_all_coding_context(AV1_COMP *cpi) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005350 save_cur_buf(cpi);
5351 save_extra_coding_context(cpi);
5352 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5353}
5354
5355static void restore_cur_buf(AV1_COMP *cpi) {
5356 CODING_CONTEXT *const cc = &cpi->coding_context;
5357 AV1_COMMON *cm = &cpi->common;
5358 aom_yv12_copy_frame(&cc->copy_buffer, &cm->cur_frame->buf,
5359 av1_num_planes(cm));
5360}
5361
5362// Coding context that only needs to be restored when recode loop includes
5363// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5364// restoraton).
5365static void restore_extra_coding_context(AV1_COMP *cpi) {
5366 CODING_CONTEXT *const cc = &cpi->coding_context;
5367 AV1_COMMON *cm = &cpi->common;
5368 cm->lf = cc->lf;
5369 cm->cdef_info = cc->cdef_info;
5370 cpi->rc = cc->rc;
5371}
5372
5373static void restore_all_coding_context(AV1_COMP *cpi) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005374 restore_cur_buf(cpi);
5375 restore_extra_coding_context(cpi);
5376 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5377}
5378
5379static int encode_with_and_without_superres(AV1_COMP *cpi, size_t *size,
5380 uint8_t *dest,
5381 int *largest_tile_id) {
5382 const AV1_COMMON *const cm = &cpi->common;
5383 AV1EncoderConfig *const oxcf = &cpi->oxcf;
5384 assert(cm->seq_params.enable_superres);
5385 assert(superres_in_recode_allowed(cpi));
5386 aom_codec_err_t err = AOM_CODEC_OK;
5387 save_all_coding_context(cpi);
5388
5389 // Encode with superres.
5390 int64_t sse1 = INT64_MAX;
5391 int64_t rate1 = INT64_MAX;
5392 int largest_tile_id1;
5393 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse1, &rate1,
5394 &largest_tile_id1);
5395 if (err != AOM_CODEC_OK) return err;
5396
5397 // Encode without superres.
5398 restore_all_coding_context(cpi);
5399 int64_t sse2 = INT64_MAX;
5400 int64_t rate2 = INT64_MAX;
5401 int largest_tile_id2;
5402 oxcf->superres_mode = SUPERRES_NONE; // To force full-res.
5403 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
5404 &largest_tile_id2);
5405 oxcf->superres_mode = SUPERRES_AUTO; // Reset.
5406 if (err != AOM_CODEC_OK) return err;
5407
5408 // Note: Both use common rdmult based on base qindex of fullres.
5409 const int64_t rdmult =
5410 av1_compute_rd_mult_based_on_qindex(cpi, cm->base_qindex);
5411 const double proj_rdcost1 = RDCOST_DBL(rdmult, rate1, sse1);
5412 const double proj_rdcost2 = RDCOST_DBL(rdmult, rate2, sse2);
5413
5414 // Re-encode with superres if it's better.
5415 if (proj_rdcost1 < proj_rdcost2) {
5416 restore_all_coding_context(cpi);
5417 // TODO(urvang): We should avoid rerunning the recode loop by saving
5418 // previous output+state, or running encode only for the selected 'q' in
5419 // previous step.
5420 int64_t sse3 = INT64_MAX;
5421 int64_t rate3 = INT64_MAX;
5422 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
5423 largest_tile_id);
5424 assert(sse1 == sse3);
5425 assert(rate1 == rate3);
5426 assert(largest_tile_id1 == *largest_tile_id);
5427 } else {
5428 *largest_tile_id = largest_tile_id2;
5429 }
5430
5431 return err;
5432}
5433#endif // CONFIG_SUPERRES_IN_RECODE
5434
Yaowu Xuc27fc142016-08-22 16:08:15 -07005435#define DUMP_RECON_FRAMES 0
5436
5437#if DUMP_RECON_FRAMES == 1
5438// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07005439static void dump_filtered_recon_frames(AV1_COMP *cpi) {
5440 AV1_COMMON *const cm = &cpi->common;
David Turnerd2a592e2018-11-16 14:59:31 +00005441 const CurrentFrame *const current_frame = &cm->current_frame;
David Turnerc29e1a92018-12-06 14:10:14 +00005442 const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005443
Zoe Liub4f31032017-11-03 23:48:35 -07005444 if (recon_buf == NULL) {
David Turnerd2a592e2018-11-16 14:59:31 +00005445 printf("Frame %d is not ready.\n", current_frame->frame_number);
Zoe Liub4f31032017-11-03 23:48:35 -07005446 return;
5447 }
5448
Zoe Liu27deb382018-03-27 15:13:56 -07005449 static const int flag_list[REF_FRAMES] = { 0,
5450 AOM_LAST_FLAG,
5451 AOM_LAST2_FLAG,
5452 AOM_LAST3_FLAG,
5453 AOM_GOLD_FLAG,
5454 AOM_BWD_FLAG,
5455 AOM_ALT2_FLAG,
5456 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07005457 printf(
5458 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
5459 "show_existing_frame=%d) "
5460 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
David Turnerd2a592e2018-11-16 14:59:31 +00005461 current_frame->frame_number, current_frame->order_hint, cm->show_frame,
Zoe Liub4f31032017-11-03 23:48:35 -07005462 cm->show_existing_frame);
5463 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00005464 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
5465 const int ref_offset = buf != NULL ? (int)buf->order_hint : -1;
Urvang Joshib6f17672019-03-05 11:51:02 -08005466 printf(" %d(%c)", ref_offset,
5467 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N');
Zoe Liub4f31032017-11-03 23:48:35 -07005468 }
5469 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07005470
5471 if (!cm->show_frame) {
5472 printf("Frame %d is a no show frame, so no image dump.\n",
David Turnerd2a592e2018-11-16 14:59:31 +00005473 current_frame->frame_number);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005474 return;
5475 }
5476
Zoe Liub4f31032017-11-03 23:48:35 -07005477 int h;
5478 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
5479 FILE *f_recon = NULL;
5480
David Turnerd2a592e2018-11-16 14:59:31 +00005481 if (current_frame->frame_number == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005482 if ((f_recon = fopen(file_name, "wb")) == NULL) {
5483 printf("Unable to open file %s to write.\n", file_name);
5484 return;
5485 }
5486 } else {
5487 if ((f_recon = fopen(file_name, "ab")) == NULL) {
5488 printf("Unable to open file %s to append.\n", file_name);
5489 return;
5490 }
5491 }
5492 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07005493 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
5494 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
Urvang Joshi7a890232019-03-22 17:00:31 -07005495 "refresh_alt_ref_frame=%d, "
Zoe Liuf40a9572017-10-13 12:37:19 -07005496 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Sarah Parkere1b22012019-06-06 16:35:25 -07005497 current_frame->frame_number, cpi->gf_group.index,
5498 cpi->gf_group.update_type[cpi->gf_group.index], current_frame->order_hint,
5499 cm->show_frame, cm->show_existing_frame, cpi->rc.source_alt_ref_active,
5500 cpi->refresh_alt_ref_frame, recon_buf->y_stride, recon_buf->uv_stride,
5501 cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07005502#if 0
5503 int ref_frame;
5504 printf("get_ref_frame_map_idx: [");
5505 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
David Turnera21966b2018-12-05 14:48:49 +00005506 printf(" %d", get_ref_frame_map_idx(cm, ref_frame));
Zoe Liue9b15e22017-07-19 15:53:01 -07005507 printf(" ]\n");
Zoe Liue9b15e22017-07-19 15:53:01 -07005508#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07005509
5510 // --- Y ---
5511 for (h = 0; h < cm->height; ++h) {
5512 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
5513 f_recon);
5514 }
5515 // --- U ---
5516 for (h = 0; h < (cm->height >> 1); ++h) {
5517 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5518 f_recon);
5519 }
5520 // --- V ---
5521 for (h = 0; h < (cm->height >> 1); ++h) {
5522 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5523 f_recon);
5524 }
5525
5526 fclose(f_recon);
5527}
5528#endif // DUMP_RECON_FRAMES
5529
David Turnerefed6372019-01-11 15:14:11 +00005530static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
5531 const YV12_BUFFER_CONFIG *last_picture,
5532 hash_table *last_hash_table) {
5533 aom_clear_system_state();
5534 // check use hash ME
5535 int k;
5536 uint32_t hash_value_1;
5537 uint32_t hash_value_2;
5538
Ranjit Kumar Tulabandu9f26c992019-10-24 12:24:52 +05305539 const int block_size = FORCE_INT_MV_DECISION_BLOCK_SIZE;
David Turnerefed6372019-01-11 15:14:11 +00005540 const double threshold_current = 0.8;
5541 const double threshold_average = 0.95;
5542 const int max_history_size = 32;
5543 int T = 0; // total block
5544 int C = 0; // match with collocated block
5545 int S = 0; // smooth region but not match with collocated block
5546 int M = 0; // match with other block
5547
5548 const int pic_width = cur_picture->y_width;
5549 const int pic_height = cur_picture->y_height;
5550 for (int i = 0; i + block_size <= pic_height; i += block_size) {
5551 for (int j = 0; j + block_size <= pic_width; j += block_size) {
5552 const int x_pos = j;
5553 const int y_pos = i;
5554 int match = 1;
5555 T++;
5556
5557 // check whether collocated block match with current
5558 uint8_t *p_cur = cur_picture->y_buffer;
5559 uint8_t *p_ref = last_picture->y_buffer;
5560 int stride_cur = cur_picture->y_stride;
5561 int stride_ref = last_picture->y_stride;
5562 p_cur += (y_pos * stride_cur + x_pos);
5563 p_ref += (y_pos * stride_ref + x_pos);
5564
5565 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
5566 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
5567 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
5568 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5569 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5570 if (p16_cur[tmpX] != p16_ref[tmpX]) {
5571 match = 0;
5572 }
5573 }
5574 p16_cur += stride_cur;
5575 p16_ref += stride_ref;
5576 }
5577 } else {
5578 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5579 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5580 if (p_cur[tmpX] != p_ref[tmpX]) {
5581 match = 0;
5582 }
5583 }
5584 p_cur += stride_cur;
5585 p_ref += stride_ref;
5586 }
5587 }
5588
5589 if (match) {
5590 C++;
5591 continue;
5592 }
5593
5594 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
5595 y_pos) ||
5596 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
5597 S++;
5598 continue;
5599 }
5600
5601 av1_get_block_hash_value(
5602 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
5603 block_size, &hash_value_1, &hash_value_2,
5604 (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH), &cpi->td.mb);
5605 // Hashing does not work for highbitdepth currently.
5606 // TODO(Roger): Make it work for highbitdepth.
5607 if (av1_use_hash_me(&cpi->common)) {
5608 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
5609 M++;
5610 }
5611 }
5612 }
5613 }
5614
5615 assert(T > 0);
5616 double csm_rate = ((double)(C + S + M)) / ((double)(T));
5617 double m_rate = ((double)(M)) / ((double)(T));
5618
5619 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
5620 cpi->m_rate_array[cpi->rate_index] = m_rate;
5621
5622 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
5623 cpi->rate_size++;
5624 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
5625
5626 if (csm_rate < threshold_current) {
5627 return 0;
5628 }
5629
5630 if (C == T) {
5631 return 1;
5632 }
5633
5634 double csm_average = 0.0;
5635 double m_average = 0.0;
5636
5637 for (k = 0; k < cpi->rate_size; k++) {
5638 csm_average += cpi->csm_rate_array[k];
5639 m_average += cpi->m_rate_array[k];
5640 }
5641 csm_average /= cpi->rate_size;
5642 m_average /= cpi->rate_size;
5643
5644 if (csm_average < threshold_average) {
5645 return 0;
5646 }
5647
5648 if (M > (T - C - S) / 3) {
5649 return 1;
5650 }
5651
5652 if (csm_rate > 0.99 && m_rate > 0.01) {
5653 return 1;
5654 }
5655
5656 if (csm_average + m_average > 1.01) {
5657 return 1;
5658 }
5659
5660 return 0;
5661}
5662
David Turner73245762019-02-11 16:42:34 +00005663// Refresh reference frame buffers according to refresh_frame_flags.
5664static void refresh_reference_frames(AV1_COMP *cpi) {
5665 AV1_COMMON *const cm = &cpi->common;
5666 // All buffers are refreshed for shown keyframes and S-frames.
5667
5668 for (int ref_frame = 0; ref_frame < REF_FRAMES; ref_frame++) {
5669 if (((cm->current_frame.refresh_frame_flags >> ref_frame) & 1) == 1) {
5670 assign_frame_buffer_p(&cm->ref_frame_map[ref_frame], cm->cur_frame);
5671 }
5672 }
5673}
5674
sdengc23c7f12019-06-11 16:56:50 -07005675// Implementation and modifications of C. Yeo, H. L. Tan, and Y. H. Tan, "On
5676// rate distortion optimization using SSIM," Circuits and Systems for Video
5677// Technology, IEEE Transactions on, vol. 23, no. 7, pp. 1170-1181, 2013.
sdeng32185d12019-06-19 14:47:09 -07005678// SSIM_VAR_SCALE defines the strength of the bias towards SSIM in RDO:
5679// Test data set: mid_res (33 frames)
5680// SSIM_VAR_SCALE avg_psnr ssim ms-ssim
5681// 8 8.2 -6.0 -6.4
5682// 16 4.0 -5.7 -5.9
5683// 32 1.6 -4.4 -4.5
sdengc23c7f12019-06-11 16:56:50 -07005684#define SSIM_VAR_SCALE 16.0
5685static void set_mb_ssim_rdmult_scaling(AV1_COMP *cpi) {
5686 AV1_COMMON *cm = &cpi->common;
5687 ThreadData *td = &cpi->td;
5688 MACROBLOCK *x = &td->mb;
5689 MACROBLOCKD *xd = &x->e_mbd;
5690 uint8_t *y_buffer = cpi->source->y_buffer;
5691 const int y_stride = cpi->source->y_stride;
5692 const int block_size = BLOCK_16X16;
5693
5694 const int num_mi_w = mi_size_wide[block_size];
5695 const int num_mi_h = mi_size_high[block_size];
5696 const int num_cols = (cm->mi_cols + num_mi_w - 1) / num_mi_w;
5697 const int num_rows = (cm->mi_rows + num_mi_h - 1) / num_mi_h;
5698 double log_sum = 0.0;
5699 int row, col;
5700 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
5701
sdeng32185d12019-06-19 14:47:09 -07005702 // TODO(sdeng): tune this param for 12bit videos.
5703 double c2 = 58.5225; // (.03*255)^2
5704 c2 *= SSIM_VAR_SCALE;
sdengc23c7f12019-06-11 16:56:50 -07005705
5706 // Loop through each 16x16 block.
5707 for (row = 0; row < num_rows; ++row) {
5708 for (col = 0; col < num_cols; ++col) {
5709 int mi_row, mi_col;
5710 double var = 0.0, num_of_var = 0.0;
5711 const int index = row * num_cols + col;
5712
5713 // Loop through each 8x8 block.
5714 for (mi_row = row * num_mi_h;
5715 mi_row < cm->mi_rows && mi_row < (row + 1) * num_mi_h; mi_row += 2) {
5716 for (mi_col = col * num_mi_w;
5717 mi_col < cm->mi_cols && mi_col < (col + 1) * num_mi_w;
5718 mi_col += 2) {
5719 struct buf_2d buf;
5720 const int row_offset_y = mi_row << 2;
5721 const int col_offset_y = mi_col << 2;
5722
5723 buf.buf = y_buffer + row_offset_y * y_stride + col_offset_y;
5724 buf.stride = y_stride;
5725
5726 if (use_hbd) {
5727 var += av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8,
5728 xd->bd);
5729 } else {
5730 var += av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8);
5731 }
5732
5733 num_of_var += 1.0;
5734 }
5735 }
sdeng32185d12019-06-19 14:47:09 -07005736 var = var / num_of_var;
sdengc23c7f12019-06-11 16:56:50 -07005737 var = 2.0 * var + c2;
5738 cpi->ssim_rdmult_scaling_factors[index] = var;
5739 log_sum += log(var);
5740 }
5741 }
5742 log_sum = exp(log_sum / (double)(num_rows * num_cols));
5743
5744 for (row = 0; row < num_rows; ++row) {
5745 for (col = 0; col < num_cols; ++col) {
5746 const int index = row * num_cols + col;
5747 cpi->ssim_rdmult_scaling_factors[index] /= log_sum;
5748 }
5749 }
5750
5751 (void)xd;
5752}
5753
chiyotsaifc1404d2019-08-08 12:09:12 -07005754#if CONFIG_DEBUG
5755static int hash_me_has_at_most_two_refs(RefCntBuffer *frame_bufs) {
5756 int total_count = 0;
5757 for (int frame_idx = 0; frame_idx < FRAME_BUFFERS; ++frame_idx) {
5758 if (frame_bufs[frame_idx].hash_table.has_content > 1) {
5759 return 0;
5760 }
5761 total_count += frame_bufs[frame_idx].hash_table.has_content;
5762 }
5763
5764 return total_count <= 2;
5765}
5766#endif
5767
David Turner73245762019-02-11 16:42:34 +00005768static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
5769 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005770 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005771 SequenceHeader *const seq_params = &cm->seq_params;
David Turnerd2a592e2018-11-16 14:59:31 +00005772 CurrentFrame *const current_frame = &cm->current_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07005773 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005774 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01005775
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005776#if CONFIG_COLLECT_COMPONENT_TIMING
5777 start_timing(cpi, encode_frame_to_data_rate_time);
5778#endif
5779
Fangwen Fu8d164de2016-12-14 13:40:54 -08005780 // frame type has been decided outside of this function call
David Turnerd2a592e2018-11-16 14:59:31 +00005781 cm->cur_frame->frame_type = current_frame->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07005782
Yunqing Wang9612d552018-05-15 14:58:30 -07005783 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
5784 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wang9612d552018-05-15 14:58:30 -07005785
sarahparker21dbca42018-03-30 17:43:44 -07005786 cm->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
Yunqing Wangd48fb162018-06-15 10:55:28 -07005787 // cm->allow_ref_frame_mvs needs to be written into the frame header while
5788 // cm->large_scale_tile is 1, therefore, "cm->large_scale_tile=1" case is
5789 // separated from frame_might_allow_ref_frame_mvs().
5790 cm->allow_ref_frame_mvs &= !cm->large_scale_tile;
5791
Debargha Mukherjee1d7217e2018-03-26 13:32:13 -07005792 cm->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07005793 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005794
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305795 cm->last_frame_type = current_frame->frame_type;
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305796
Sarah Parker33005522018-07-27 14:46:25 -07005797 if (encode_show_existing_frame(cm)) {
David Turner996b2c12018-12-07 15:52:30 +00005798 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005799 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00005800 int largest_tile_id = 0; // Output from bitstream: unused here
5801 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08005802 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005803
David Turner90311862018-11-29 13:34:36 +00005804 if (seq_params->frame_id_numbers_present_flag &&
5805 current_frame->frame_type == KEY_FRAME) {
5806 // Displaying a forward key-frame, so reset the ref buffer IDs
5807 int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
5808 for (int i = 0; i < REF_FRAMES; i++)
5809 cm->ref_frame_id[i] = display_frame_id;
5810 }
5811
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005812 cpi->seq_params_locked = 1;
5813
Yaowu Xuc27fc142016-08-22 16:08:15 -07005814#if DUMP_RECON_FRAMES == 1
5815 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
5816 dump_filtered_recon_frames(cpi);
5817#endif // DUMP_RECON_FRAMES
5818
David Turner73245762019-02-11 16:42:34 +00005819 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
5820 // for the purpose to verify no mismatch between encoder and decoder.
5821 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005822
David Turner73245762019-02-11 16:42:34 +00005823 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005824
Yaowu Xuc27fc142016-08-22 16:08:15 -07005825 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
5826 // to do post-encoding update accordingly.
5827 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005828 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07005829 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005830 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005831
David Turnerd2a592e2018-11-16 14:59:31 +00005832 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005833
Tom Finegane4099e32018-01-23 12:01:51 -08005834 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005835 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005836
David Turnerefed6372019-01-11 15:14:11 +00005837 // Work out whether to force_integer_mv this frame
5838 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools &&
5839 !frame_is_intra_only(cm)) {
5840 if (cpi->common.seq_params.force_integer_mv == 2) {
5841 // Adaptive mode: see what previous frame encoded did
5842 if (cpi->unscaled_last_source != NULL) {
5843 cm->cur_frame_force_integer_mv =
5844 is_integer_mv(cpi, cpi->source, cpi->unscaled_last_source,
5845 cpi->previous_hash_table);
5846 } else {
5847 cpi->common.cur_frame_force_integer_mv = 0;
5848 }
5849 } else {
5850 cpi->common.cur_frame_force_integer_mv =
5851 cpi->common.seq_params.force_integer_mv;
5852 }
5853 } else {
5854 cpi->common.cur_frame_force_integer_mv = 0;
5855 }
5856
chiyotsaifc1404d2019-08-08 12:09:12 -07005857#if CONFIG_DEBUG
5858 assert(hash_me_has_at_most_two_refs(cm->buffer_pool->frame_bufs) &&
5859 "Hash-me is leaking memory!");
5860#endif
5861
5862 if (cpi->oxcf.pass != 1 && cpi->need_to_clear_prev_hash_table) {
5863 av1_hash_table_clear_all(cpi->previous_hash_table);
5864 cpi->need_to_clear_prev_hash_table = 0;
5865 }
5866
Yaowu Xuc27fc142016-08-22 16:08:15 -07005867 // Set default state for segment based loop filter update flags.
5868 cm->lf.mode_ref_delta_update = 0;
5869
Yaowu Xuc27fc142016-08-22 16:08:15 -07005870 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05005871 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005872 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07005873 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005874
5875 // If segmentation is enabled force a map update for key frames.
5876 if (seg->enabled) {
5877 seg->update_map = 1;
5878 seg->update_data = 1;
5879 }
5880
5881 // The alternate reference frame cannot be active for a key frame.
5882 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005883 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00005884 if (cpi->oxcf.mtu == 0) {
5885 cm->num_tg = cpi->oxcf.num_tile_groups;
5886 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08005887 // Use a default value for the purposes of weighting costs in probability
5888 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00005889 cm->num_tg = DEFAULT_MAX_NUM_TG;
5890 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005891
5892 // For 1 pass CBR, check if we are dropping this frame.
5893 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005894 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
David Turnerd2a592e2018-11-16 14:59:31 +00005895 current_frame->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005896 if (av1_rc_drop_frame(cpi)) {
5897 av1_rc_postencode_update_drop_frame(cpi);
David Turnera4c96252019-01-11 16:36:39 +00005898 release_scaled_references(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08005899 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005900 }
5901 }
5902
sdengc23c7f12019-06-11 16:56:50 -07005903 if (oxcf->tuning == AOM_TUNE_SSIM) set_mb_ssim_rdmult_scaling(cpi);
5904
Yaowu Xuf883b422016-08-30 14:01:10 -07005905 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005906
5907#if CONFIG_INTERNAL_STATS
5908 memset(cpi->mode_chosen_counts, 0,
5909 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
5910#endif
5911
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005912 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005913 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08005914 * wraparound) */
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005915 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00005916 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08005917 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07005918 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
5919 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
5920 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005921 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005922 lsb = cpi->source->y_buffer[0] & 0xff;
5923 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005924 }
David Turner760a2f42018-12-07 15:25:36 +00005925 cm->current_frame_id =
5926 ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05005927
5928 // S_frame is meant for stitching different streams of different
5929 // resolutions together, so current_frame_id must be the
5930 // same across different streams of the same content current_frame_id
5931 // should be the same and not random. 0x37 is a chosen number as start
5932 // point
5933 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005934 } else {
5935 cm->current_frame_id =
David Turner760a2f42018-12-07 15:25:36 +00005936 (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
5937 (1 << seq_params->frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005938 }
5939 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005940
Hui Su483a8452018-02-26 12:28:48 -08005941 switch (cpi->oxcf.cdf_update_mode) {
5942 case 0: // No CDF update for any frames(4~6% compression loss).
5943 cm->disable_cdf_update = 1;
5944 break;
5945 case 1: // Enable CDF update for all frames.
5946 cm->disable_cdf_update = 0;
5947 break;
5948 case 2:
5949 // Strategically determine at which frames to do CDF update.
5950 // Currently only enable CDF update for all-intra and no-show frames(1.5%
5951 // compression loss).
5952 // TODO(huisu@google.com): design schemes for various trade-offs between
5953 // compression quality and decoding speed.
Hui Sub1b76b32018-02-27 15:24:48 -08005954 cm->disable_cdf_update =
5955 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08005956 break;
5957 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005958 cm->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08005959
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005960 if (cpi->oxcf.pass == 2 && cpi->oxcf.enable_tpl_model == 2 &&
5961 current_frame->frame_type == INTER_FRAME) {
5962 if (!cm->show_frame) {
5963 assert(cpi->tpl_model_pass == 0);
5964 cpi->tpl_model_pass = 1;
5965 }
5966 }
5967
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005968 int largest_tile_id = 0;
5969#if CONFIG_SUPERRES_IN_RECODE
5970 if (superres_in_recode_allowed(cpi)) {
5971 if (encode_with_and_without_superres(cpi, size, dest, &largest_tile_id) !=
5972 AOM_CODEC_OK) {
5973 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005974 }
Hui Su06463e42018-02-23 22:17:36 -08005975 } else {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005976#endif // CONFIG_SUPERRES_IN_RECODE
5977 if (encode_with_recode_loop_and_filter(cpi, size, dest, NULL, NULL,
5978 &largest_tile_id) != AOM_CODEC_OK) {
5979 return AOM_CODEC_ERROR;
5980 }
5981#if CONFIG_SUPERRES_IN_RECODE
Hui Su06463e42018-02-23 22:17:36 -08005982 }
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005983#endif // CONFIG_SUPERRES_IN_RECODE
Yaowu Xuc27fc142016-08-22 16:08:15 -07005984
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005985 cpi->seq_params_locked = 1;
5986
David Turner996b2c12018-12-07 15:52:30 +00005987 // Update reference frame ids for reference frames this frame will overwrite
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005988 if (seq_params->frame_id_numbers_present_flag) {
David Turner996b2c12018-12-07 15:52:30 +00005989 for (int i = 0; i < REF_FRAMES; i++) {
5990 if ((current_frame->refresh_frame_flags >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005991 cm->ref_frame_id[i] = cm->current_frame_id;
5992 }
5993 }
5994 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005995
Yaowu Xuc27fc142016-08-22 16:08:15 -07005996#if DUMP_RECON_FRAMES == 1
5997 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07005998 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005999#endif // DUMP_RECON_FRAMES
6000
Soo-Chul Han934af352017-10-15 15:21:51 -04006001 if (cm->seg.enabled) {
6002 if (cm->seg.update_map) {
6003 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07006004 } else if (cm->last_frame_seg_map) {
David Turnerb757ce02018-11-12 15:01:28 +00006005 memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
Soo-Chul Han934af352017-10-15 15:21:51 -04006006 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
6007 }
6008 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006009
6010 if (frame_is_intra_only(cm) == 0) {
6011 release_scaled_references(cpi);
6012 }
6013
David Turner73245762019-02-11 16:42:34 +00006014 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
6015 // for the purpose to verify no mismatch between encoder and decoder.
6016 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
6017
6018 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006019
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08006020#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07006021 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08006022#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006023
Hui Sudc54be62018-03-14 19:14:28 -07006024 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
David Turner35cba132018-12-10 15:48:15 +00006025 *cm->fc = cpi->tile_data[largest_tile_id].tctx;
Hui Sudc54be62018-03-14 19:14:28 -07006026 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006027 }
David Turnera4c96252019-01-11 16:36:39 +00006028 if (!cm->large_scale_tile) {
6029 cm->cur_frame->frame_context = *cm->fc;
6030 }
6031#define EXT_TILE_DEBUG 0
6032#if EXT_TILE_DEBUG
6033 if (cm->large_scale_tile && oxcf->pass == 2) {
6034 char fn[20] = "./fc";
6035 fn[4] = current_frame->frame_number / 100 + '0';
6036 fn[5] = (current_frame->frame_number % 100) / 10 + '0';
6037 fn[6] = (current_frame->frame_number % 10) + '0';
6038 fn[7] = '\0';
6039 av1_print_frame_contexts(cm->fc, fn);
6040 }
6041#endif // EXT_TILE_DEBUG
6042#undef EXT_TILE_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07006043
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006044#if CONFIG_COLLECT_COMPONENT_TIMING
6045 end_timing(cpi, encode_frame_to_data_rate_time);
6046
6047 // Print out timing information.
6048 int i;
chiyotsai9c484b32019-03-07 16:01:50 -08006049 fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n",
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006050 cm->current_frame.frame_number,
chiyotsai9c484b32019-03-07 16:01:50 -08006051 get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006052 for (i = 0; i < kTimingComponents; i++) {
6053 cpi->component_time[i] += cpi->frame_component_time[i];
6054 fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n",
6055 get_component_name(i), cpi->frame_component_time[i],
6056 cpi->component_time[i]);
6057 cpi->frame_component_time[i] = 0;
6058 }
6059#endif
6060
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05306061 cm->last_frame_type = current_frame->frame_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006062
Yaowu Xuf883b422016-08-30 14:01:10 -07006063 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006064
chiyotsaifc1404d2019-08-08 12:09:12 -07006065 // Store encoded frame's hash table for in_integer_mv() next time.
6066 // Beware! If we don't update previous_hash_table here we will leak the
6067 // items stored in cur_frame's hash_table!
6068 if (oxcf->pass != 1 && av1_use_hash_me(cm)) {
David Turnerefed6372019-01-11 15:14:11 +00006069 cpi->previous_hash_table = &cm->cur_frame->hash_table;
chiyotsaifc1404d2019-08-08 12:09:12 -07006070 cpi->need_to_clear_prev_hash_table = 1;
David Turnerefed6372019-01-11 15:14:11 +00006071 }
6072
Yaowu Xuc27fc142016-08-22 16:08:15 -07006073 // Clear the one shot update flags for segmentation map and mode/ref loop
6074 // filter deltas.
6075 cm->seg.update_map = 0;
6076 cm->seg.update_data = 0;
6077 cm->lf.mode_ref_delta_update = 0;
6078
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07006079 // A droppable frame might not be shown but it always
6080 // takes a space in the gf group. Therefore, even when
6081 // it is not shown, we still need update the count down.
6082
Yaowu Xuc27fc142016-08-22 16:08:15 -07006083 if (cm->show_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006084 // Don't increment frame counters if this was an altref buffer
6085 // update not a real frame
David Turnerd2a592e2018-11-16 14:59:31 +00006086 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006087 }
6088
Tom Finegane4099e32018-01-23 12:01:51 -08006089 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006090}
6091
David Turner056f7cd2019-01-07 17:48:13 +00006092int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
David Turnercb5e36f2019-01-17 17:15:25 +00006093 const EncodeFrameInput *const frame_input,
David Turner056f7cd2019-01-07 17:48:13 +00006094 const EncodeFrameParams *const frame_params,
6095 EncodeFrameResults *const frame_results) {
David Turner07dbd8e2019-01-08 17:16:25 +00006096 AV1_COMMON *const cm = &cpi->common;
David Turnera7f133c2019-01-22 14:47:16 +00006097 CurrentFrame *const current_frame = &cm->current_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00006098
David Turnercb5e36f2019-01-17 17:15:25 +00006099 cpi->unscaled_source = frame_input->source;
6100 cpi->source = frame_input->source;
6101 cpi->unscaled_last_source = frame_input->last_source;
David Turner056f7cd2019-01-07 17:48:13 +00006102
David Turner6e8b4d92019-02-18 15:01:33 +00006103 current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
David Turner07dbd8e2019-01-08 17:16:25 +00006104 cm->error_resilient_mode = frame_params->error_resilient_mode;
David Turnera7f133c2019-01-22 14:47:16 +00006105 cm->primary_ref_frame = frame_params->primary_ref_frame;
David Turner475a3132019-01-18 15:17:17 +00006106 cm->current_frame.frame_type = frame_params->frame_type;
David Turnerdedd8ff2019-01-23 13:59:46 +00006107 cm->show_frame = frame_params->show_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00006108 cpi->ref_frame_flags = frame_params->ref_frame_flags;
David Turner04b70d82019-01-24 15:39:19 +00006109 cpi->speed = frame_params->speed;
David Turnere86ee0d2019-02-18 17:16:28 +00006110 cm->show_existing_frame = frame_params->show_existing_frame;
6111 cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
David Turner07dbd8e2019-01-08 17:16:25 +00006112
David Turner73245762019-02-11 16:42:34 +00006113 memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
6114 REF_FRAMES * sizeof(*cm->remapped_ref_idx));
6115
David Turnerfe3aecb2019-02-06 14:42:42 +00006116 cpi->refresh_last_frame = frame_params->refresh_last_frame;
6117 cpi->refresh_golden_frame = frame_params->refresh_golden_frame;
6118 cpi->refresh_bwd_ref_frame = frame_params->refresh_bwd_ref_frame;
David Turnerfe3aecb2019-02-06 14:42:42 +00006119 cpi->refresh_alt_ref_frame = frame_params->refresh_alt_ref_frame;
6120
David Turnera7f133c2019-01-22 14:47:16 +00006121 if (current_frame->frame_type == KEY_FRAME && cm->show_frame)
6122 current_frame->frame_number = 0;
6123
6124 if (cm->show_existing_frame) {
6125 current_frame->order_hint = cm->cur_frame->order_hint;
Ravi Chaudhary9701cd62019-07-18 17:32:26 +05306126 current_frame->display_order_hint = cm->cur_frame->display_order_hint;
David Turnera7f133c2019-01-22 14:47:16 +00006127 } else {
6128 current_frame->order_hint =
6129 current_frame->frame_number + frame_params->order_offset;
Ravi Chaudhary9701cd62019-07-18 17:32:26 +05306130 current_frame->display_order_hint = current_frame->order_hint;
David Turnera7f133c2019-01-22 14:47:16 +00006131 current_frame->order_hint %=
6132 (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
6133 }
6134
David Turnercb5e36f2019-01-17 17:15:25 +00006135 if (cpi->oxcf.pass == 1) {
Jerome Jiang2612b4d2019-05-29 17:46:47 -07006136#if !CONFIG_REALTIME_ONLY
David Turnercb5e36f2019-01-17 17:15:25 +00006137 av1_first_pass(cpi, frame_input->ts_duration);
Jerome Jiang2612b4d2019-05-29 17:46:47 -07006138#endif
David Turnercb5e36f2019-01-17 17:15:25 +00006139 } else if (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) {
David Turner73245762019-02-11 16:42:34 +00006140 if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
6141 AOM_CODEC_OK) {
David Turnercb5e36f2019-01-17 17:15:25 +00006142 return AOM_CODEC_ERROR;
6143 }
6144 } else {
David Turner056f7cd2019-01-07 17:48:13 +00006145 return AOM_CODEC_ERROR;
6146 }
6147
6148 return AOM_CODEC_OK;
6149}
6150
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006151#if CONFIG_DENOISE
6152static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
6153 int block_size, float noise_level,
6154 int64_t time_stamp, int64_t end_time) {
6155 AV1_COMMON *const cm = &cpi->common;
6156 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006157 cpi->denoise_and_model = aom_denoise_and_model_alloc(
6158 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006159 if (!cpi->denoise_and_model) {
6160 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6161 "Error allocating denoise and model");
6162 return -1;
6163 }
6164 }
6165 if (!cpi->film_grain_table) {
6166 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
6167 if (!cpi->film_grain_table) {
6168 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6169 "Error allocating grain table");
6170 return -1;
6171 }
6172 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
6173 }
6174 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
6175 &cm->film_grain_params)) {
6176 if (cm->film_grain_params.apply_grain) {
6177 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
6178 &cm->film_grain_params);
6179 }
6180 }
6181 return 0;
6182}
6183#endif
6184
James Zern3e2613b2017-03-30 23:14:40 -07006185int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07006186 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
6187 int64_t end_time) {
6188 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006189 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006190 int res = 0;
6191 const int subsampling_x = sd->subsampling_x;
6192 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006193 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006194
Yaowu Xuc27fc142016-08-22 16:08:15 -07006195 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006196
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006197#if CONFIG_INTERNAL_STATS
6198 struct aom_usec_timer timer;
Yaowu Xuf883b422016-08-30 14:01:10 -07006199 aom_usec_timer_start(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006200#endif
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006201#if CONFIG_DENOISE
6202 if (cpi->oxcf.noise_level > 0)
6203 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
6204 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
6205 res = -1;
6206#endif // CONFIG_DENOISE
6207
Yaowu Xuf883b422016-08-30 14:01:10 -07006208 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08006209 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07006210 res = -1;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006211#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006212 aom_usec_timer_mark(&timer);
6213 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006214#endif
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006215 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07006216 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006217 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006218 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07006219 res = -1;
6220 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006221 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006222 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006223 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006224 "Profile 1 requires 4:4:4 color format");
6225 res = -1;
6226 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006227 if ((seq_params->profile == PROFILE_2) &&
6228 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006229 !(subsampling_x == 1 && subsampling_y == 0)) {
6230 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
6231 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07006232 res = -1;
6233 }
6234
6235 return res;
6236}
6237
Yaowu Xuc27fc142016-08-22 16:08:15 -07006238#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006239extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
6240 const unsigned char *img2, int img2_pitch,
6241 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006242
6243static void adjust_image_stat(double y, double u, double v, double all,
6244 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07006245 s->stat[STAT_Y] += y;
6246 s->stat[STAT_U] += u;
6247 s->stat[STAT_V] += v;
6248 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07006249 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006250}
6251
Angie Chiang08a22a62017-07-17 17:29:17 -07006252static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006253 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006254 double samples = 0.0;
6255 uint32_t in_bit_depth = 8;
6256 uint32_t bit_depth = 8;
6257
Angie Chiang08a22a62017-07-17 17:29:17 -07006258#if CONFIG_INTER_STATS_ONLY
David Turnerd2a592e2018-11-16 14:59:31 +00006259 if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07006260#endif
6261 cpi->bytes += frame_bytes;
6262
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006263 if (cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006264 in_bit_depth = cpi->oxcf.input_bit_depth;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006265 bit_depth = cm->seq_params.bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006266 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006267 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07006268 const YV12_BUFFER_CONFIG *orig = cpi->source;
David Turnerc29e1a92018-12-06 14:10:14 +00006269 const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006270 double y, u, v, frame_all;
6271
6272 cpi->count++;
6273 if (cpi->b_calculate_psnr) {
6274 PSNR_STATS psnr;
6275 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07006276 aom_clear_system_state();
Jerome Jiangfa1d1732019-08-06 10:31:20 -07006277#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xud3e7c682017-12-21 14:08:25 -08006278 // TODO(yaowu): unify these two versions into one.
Jerome Jiangfa1d1732019-08-06 10:31:20 -07006279 if (cm->seq_params.use_highbitdepth)
6280 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
6281 else
6282 aom_calc_psnr(orig, recon, &psnr);
6283#else
6284 aom_calc_psnr(orig, recon, &psnr);
6285#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006286 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
6287 &cpi->psnr);
6288 cpi->total_sq_error += psnr.sse[0];
6289 cpi->total_samples += psnr.samples[0];
6290 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08006291 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006292 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07006293 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07006294 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006295 else
Yaowu Xuf883b422016-08-30 14:01:10 -07006296 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006297
Yaowu Xuf883b422016-08-30 14:01:10 -07006298 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006299 cpi->summed_quality += frame_ssim2 * weight;
6300 cpi->summed_weights += weight;
6301
6302#if 0
6303 {
6304 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08006305 double y2 = psnr.psnr[1];
6306 double u2 = psnr.psnr[2];
6307 double v2 = psnr.psnr[3];
6308 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07006309 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 +00006310 cm->current_frame.frame_number, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07006311 frame_psnr2, frame_ssim2);
6312 fclose(f);
6313 }
6314#endif
6315 }
6316 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006317 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006318 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07006319 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
6320 recon->y_stride, orig->y_width, orig->y_height);
6321 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006322 cpi->total_blockiness += frame_blockiness;
6323 }
6324
6325 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006326 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006327 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07006328 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
6329 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
6330
6331 const double peak = (double)((1 << in_bit_depth) - 1);
6332 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006333 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006334 if (consistency > 0.0)
6335 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006336 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006337 cpi->total_inconsistency += this_inconsistency;
6338 }
6339 }
6340 }
6341
6342 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07006343 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006344 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07006345 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006346 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
6347 }
6348}
6349#endif // CONFIG_INTERNAL_STATS
Andrey Norkin795ba872018-03-06 13:24:14 -08006350int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
6351 size_t *size, uint8_t *dest, int64_t *time_stamp,
6352 int64_t *time_end, int flush,
Yue Chen1bc5be62018-08-24 13:57:32 -07006353 const aom_rational64_t *timestamp_ratio) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006354 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
6355 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006356
6357#if CONFIG_BITSTREAM_DEBUG
6358 assert(cpi->oxcf.max_threads == 0 &&
6359 "bitstream debug tool does not support multithreading");
6360 bitstream_queue_record_write();
Yaowu Xu63f2ea32019-04-29 10:47:42 -07006361 aom_bitstream_queue_set_frame_write(cm->current_frame.frame_number * 2 +
6362 cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006363#endif
Marco Paniconi63971322019-08-15 21:32:05 -07006364 if (cpi->use_svc && cm->number_spatial_layers > 1) {
6365 av1_one_pass_cbr_svc_start_layer(cpi);
6366 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006367
Sarah Parker740e8392019-01-23 15:47:53 -08006368 // Indicates whether or not to use an adaptive quantize b rather than
6369 // the traditional version
6370 cm->use_quant_b_adapt = cpi->oxcf.quant_b_adapt;
6371
Dominic Symesd4929012018-01-31 17:32:01 +01006372 cm->showable_frame = 0;
David Turnere43f7fe2019-01-15 14:58:00 +00006373 *size = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006374#if CONFIG_INTERNAL_STATS
6375 struct aom_usec_timer cmptimer;
Yaowu Xuf883b422016-08-30 14:01:10 -07006376 aom_usec_timer_start(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006377#endif
Debargha Mukherjeecd14cc12019-09-06 09:55:52 -07006378 set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006379
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07006380 // Normal defaults
sarahparker27d686a2018-03-30 17:43:44 -07006381 cm->refresh_frame_context = oxcf->frame_parallel_decoding_mode
6382 ? REFRESH_FRAME_CONTEXT_DISABLED
6383 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01006384 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08006385 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006386
Sarah Parkerb9041612018-05-22 19:06:47 -07006387 // Initialize fields related to forward keyframes
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07006388 cpi->no_show_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08006389
David Turnerdedd8ff2019-01-23 13:59:46 +00006390 if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006391
Yue Chen1bc5be62018-08-24 13:57:32 -07006392 const int result =
6393 av1_encode_strategy(cpi, size, dest, frame_flags, time_stamp, time_end,
6394 timestamp_ratio, flush);
David Turnerdedd8ff2019-01-23 13:59:46 +00006395 if (result != AOM_CODEC_OK && result != -1) {
David Turner1539bb02019-01-24 15:28:13 +00006396 return AOM_CODEC_ERROR;
David Turnerdedd8ff2019-01-23 13:59:46 +00006397 } else if (result == -1) {
6398 // Returning -1 indicates no frame encoded; more input is required
6399 return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006400 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006401#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006402 aom_usec_timer_mark(&cmptimer);
6403 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yue Chen1bc5be62018-08-24 13:57:32 -07006404#endif // CONFIG_INTERNAL_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00006405 if (cpi->b_calculate_psnr) {
6406 if (cm->show_existing_frame || (oxcf->pass != 1 && cm->show_frame)) {
6407 generate_psnr_packet(cpi);
6408 }
6409 }
Hui Su8ea87322019-03-29 14:44:32 -07006410
6411 if (cpi->keep_level_stats && oxcf->pass != 1) {
6412 // Initialize level info. at the beginning of each sequence.
6413 if (cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) {
Hui Su58753d62019-05-29 09:56:19 -07006414 av1_init_level_info(cpi);
Hui Su8ea87322019-03-29 14:44:32 -07006415 }
kyslovabeeb7c2019-03-06 18:35:04 -08006416 av1_update_level_info(cpi, *size, *time_stamp, *time_end);
Hui Su8ea87322019-03-29 14:44:32 -07006417 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006418
6419#if CONFIG_INTERNAL_STATS
6420 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006421 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006422 }
6423#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08006424#if CONFIG_SPEED_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00006425 if (cpi->oxcf.pass != 1 && !cm->show_existing_frame) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08006426 cpi->tx_search_count += cpi->td.mb.tx_search_count;
6427 cpi->td.mb.tx_search_count = 0;
6428 }
6429#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006430
Yaowu Xuf883b422016-08-30 14:01:10 -07006431 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006432
Hui Su0d0ee662019-07-29 14:38:36 -07006433 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006434}
6435
Yaowu Xuf883b422016-08-30 14:01:10 -07006436int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6437 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006438 if (!cm->show_frame) {
6439 return -1;
6440 } else {
6441 int ret;
David Turnerc29e1a92018-12-06 14:10:14 +00006442 if (cm->cur_frame != NULL) {
6443 *dest = cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006444 dest->y_width = cm->width;
6445 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006446 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
6447 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006448 ret = 0;
6449 } else {
6450 ret = -1;
6451 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006452 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006453 return ret;
6454 }
6455}
6456
Yaowu Xuf883b422016-08-30 14:01:10 -07006457int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
David Turnere7ebf902018-12-04 14:04:55 +00006458 if (cpi->last_show_frame_buf == NULL) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006459
David Turnere7ebf902018-12-04 14:04:55 +00006460 *frame = cpi->last_show_frame_buf->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006461 return 0;
6462}
6463
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006464static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
6465 const YV12_BUFFER_CONFIG *b) {
6466 return a->y_height == b->y_height && a->y_width == b->y_width &&
6467 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
6468 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
6469 a->border == b->border &&
6470 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
6471 (b->flags & YV12_FLAG_HIGHBITDEPTH);
6472}
6473
Yunqing Wang93b18f32018-06-08 21:08:29 -07006474aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
6475 YV12_BUFFER_CONFIG *new_frame,
6476 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006477 const int num_planes = av1_num_planes(cm);
6478 if (!equal_dimensions_and_border(new_frame, sd))
6479 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
6480 "Incorrect buffer dimensions");
6481 else
6482 aom_yv12_copy_frame(new_frame, sd, num_planes);
6483
6484 return cm->error.error_code;
6485}
6486
Yaowu Xuf883b422016-08-30 14:01:10 -07006487int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6488 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006489 int hr = 0, hs = 0, vr = 0, vs = 0;
6490
6491 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6492
6493 Scale2Ratio(horiz_mode, &hr, &hs);
6494 Scale2Ratio(vert_mode, &vr, &vs);
6495
6496 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07006497 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6498 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006499
6500 return 0;
6501}
6502
Yaowu Xuf883b422016-08-30 14:01:10 -07006503int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006504
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006505int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
6506 size_t output_size = 0;
6507 size_t total_bytes_read = 0;
6508 size_t remaining_size = *frame_size;
6509 uint8_t *buff_ptr = buffer;
6510
6511 // go through each OBUs
6512 while (total_bytes_read < *frame_size) {
6513 uint8_t saved_obu_header[2];
6514 uint64_t obu_payload_size;
6515 size_t length_of_payload_size;
6516 size_t length_of_obu_size;
6517 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
6518 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
6519
6520 // save the obu header (1 or 2 bytes)
6521 memmove(saved_obu_header, buff_ptr, obu_header_size);
6522 // clear the obu_has_size_field
6523 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
6524
6525 // get the payload_size and length of payload_size
6526 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
6527 &obu_payload_size, &length_of_payload_size) != 0) {
6528 return AOM_CODEC_ERROR;
6529 }
6530 obu_bytes_read += length_of_payload_size;
6531
6532 // calculate the length of size of the obu header plus payload
6533 length_of_obu_size =
6534 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
6535
6536 // move the rest of data to new location
6537 memmove(buff_ptr + length_of_obu_size + obu_header_size,
6538 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07006539 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006540
6541 // write the new obu size
6542 const uint64_t obu_size = obu_header_size + obu_payload_size;
6543 size_t coded_obu_size;
6544 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
6545 &coded_obu_size) != 0) {
6546 return AOM_CODEC_ERROR;
6547 }
6548
6549 // write the saved (modified) obu_header following obu size
6550 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
6551
6552 total_bytes_read += obu_bytes_read;
6553 remaining_size -= obu_bytes_read;
6554 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07006555 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006556 }
6557
6558 *frame_size = output_size;
6559 return AOM_CODEC_OK;
6560}
6561
Marco Paniconid8574e32019-08-04 21:30:12 -07006562static void svc_set_updates_external_ref_frame_config(AV1_COMP *cpi) {
6563 cpi->ext_refresh_frame_flags_pending = 1;
6564 cpi->ext_refresh_last_frame = cpi->svc.refresh[cpi->svc.ref_idx[0]];
6565 cpi->ext_refresh_golden_frame = cpi->svc.refresh[cpi->svc.ref_idx[3]];
6566 cpi->ext_refresh_bwd_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[4]];
6567 cpi->ext_refresh_alt2_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[5]];
6568 cpi->ext_refresh_alt_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[6]];
6569 cpi->svc.non_reference_frame = 1;
6570 for (int i = 0; i < REF_FRAMES; i++) {
6571 if (cpi->svc.refresh[i] == 1) {
6572 cpi->svc.non_reference_frame = 0;
6573 break;
6574 }
6575 }
6576}
6577
Yaowu Xuf883b422016-08-30 14:01:10 -07006578void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006579 // TODO(yunqingwang): For what references to use, external encoding flags
6580 // should be consistent with internal reference frame selection. Need to
6581 // ensure that there is not conflict between the two. In AV1 encoder, the
6582 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006583 // GOLDEN, BWDREF, ALTREF2.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08006584 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006585 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006586 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
6587 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
6588 AOM_EFLAG_NO_REF_ARF2)) {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006589 int ref = AOM_REFFRAME_ALL;
6590
6591 if (flags & AOM_EFLAG_NO_REF_LAST) ref ^= AOM_LAST_FLAG;
6592 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
6593 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
6594
6595 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
6596
6597 if (flags & AOM_EFLAG_NO_REF_ARF) {
6598 ref ^= AOM_ALT_FLAG;
6599 ref ^= AOM_BWD_FLAG;
6600 ref ^= AOM_ALT2_FLAG;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006601 } else {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006602 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
6603 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006604 }
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006605
6606 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006607 }
6608
6609 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006610 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006611 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006612
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006613 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
6614 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006615
Yaowu Xuf883b422016-08-30 14:01:10 -07006616 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006617
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006618 if (flags & AOM_EFLAG_NO_UPD_ARF) {
6619 upd ^= AOM_ALT_FLAG;
6620 upd ^= AOM_BWD_FLAG;
6621 upd ^= AOM_ALT2_FLAG;
6622 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006623
David Turnerce9b5902019-01-23 17:25:47 +00006624 cpi->ext_refresh_last_frame = (upd & AOM_LAST_FLAG) != 0;
6625 cpi->ext_refresh_golden_frame = (upd & AOM_GOLD_FLAG) != 0;
6626 cpi->ext_refresh_alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
6627 cpi->ext_refresh_bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
6628 cpi->ext_refresh_alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
6629 cpi->ext_refresh_frame_flags_pending = 1;
David Turner4f1f1812019-01-24 17:00:24 +00006630 } else {
Marco Paniconid8574e32019-08-04 21:30:12 -07006631 if (cpi->svc.external_ref_frame_config)
6632 svc_set_updates_external_ref_frame_config(cpi);
6633 else
6634 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006635 }
6636
sarahparker21dbca42018-03-30 17:43:44 -07006637 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
6638 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
sarahparker27d686a2018-03-30 17:43:44 -07006639 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
6640 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
sarahparker9806fed2018-03-30 17:43:44 -07006641 cpi->ext_use_s_frame =
6642 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Sarah Parker50b6d6e2018-04-11 19:21:54 -07006643 cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07006644
Yaowu Xuf883b422016-08-30 14:01:10 -07006645 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
6646 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006647 }
6648}
Andrey Norkin795ba872018-03-06 13:24:14 -08006649
Tom Fineganf8d6a162018-08-21 10:47:55 -07006650aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
6651 if (!cpi) return NULL;
6652
6653 uint8_t header_buf[512] = { 0 };
6654 const uint32_t sequence_header_size =
Yaowu Xu797674b2019-05-01 17:38:11 -07006655 av1_write_sequence_header_obu(cpi, &header_buf[0]);
Tom Fineganf8d6a162018-08-21 10:47:55 -07006656 assert(sequence_header_size <= sizeof(header_buf));
6657 if (sequence_header_size == 0) return NULL;
6658
6659 const size_t obu_header_size = 1;
6660 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
6661 const size_t payload_offset = obu_header_size + size_field_size;
6662
6663 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
6664 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
6665
Hui Su4fd11762019-03-26 16:05:07 -07006666 if (av1_write_obu_header(cpi, OBU_SEQUENCE_HEADER, 0, &header_buf[0]) !=
Tom Fineganf8d6a162018-08-21 10:47:55 -07006667 obu_header_size) {
6668 return NULL;
6669 }
6670
6671 size_t coded_size_field_size = 0;
6672 if (aom_uleb_encode(sequence_header_size, size_field_size,
6673 &header_buf[obu_header_size],
6674 &coded_size_field_size) != 0) {
6675 return NULL;
6676 }
6677 assert(coded_size_field_size == size_field_size);
6678
6679 aom_fixed_buf_t *global_headers =
6680 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
6681 if (!global_headers) return NULL;
6682
6683 const size_t global_header_buf_size =
6684 obu_header_size + size_field_size + sequence_header_size;
6685
6686 global_headers->buf = malloc(global_header_buf_size);
6687 if (!global_headers->buf) {
6688 free(global_headers);
6689 return NULL;
6690 }
6691
6692 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
6693 global_headers->sz = global_header_buf_size;
6694 return global_headers;
6695}