blob: 5bc215572597a0ccdf6b36f8b44923bc3980dbd6 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Urvang Joshi8a02d762016-07-28 15:51:12 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Urvang Joshi8a02d762016-07-28 15:51:12 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <limits.h>
Urvang Joshibc82d382019-11-01 17:59:20 -070013#include <float.h>
Yaowu Xuc27fc142016-08-22 16:08:15 -070014#include <math.h>
15#include <stdio.h>
16
Tom Finegan60e653d2018-05-22 11:34:58 -070017#include "config/aom_config.h"
Tom Finegan44702c82018-05-22 13:00:39 -070018#include "config/aom_dsp_rtcd.h"
19#include "config/aom_scale_rtcd.h"
Yaowu Xufa3721d2018-07-30 14:38:49 -070020#include "config/av1_rtcd.h"
21
22#include "aom_dsp/aom_dsp_common.h"
23#include "aom_dsp/aom_filter.h"
24#if CONFIG_DENOISE
25#include "aom_dsp/grain_table.h"
26#include "aom_dsp/noise_util.h"
27#include "aom_dsp/noise_model.h"
28#endif
29#include "aom_dsp/psnr.h"
30#if CONFIG_INTERNAL_STATS
31#include "aom_dsp/ssim.h"
32#endif
33#include "aom_ports/aom_timer.h"
34#include "aom_ports/mem.h"
35#include "aom_ports/system_state.h"
36#include "aom_scale/aom_scale.h"
David Turner1539bb02019-01-24 15:28:13 +000037#if CONFIG_BITSTREAM_DEBUG
Yaowu Xufa3721d2018-07-30 14:38:49 -070038#include "aom_util/debug_util.h"
David Turner1539bb02019-01-24 15:28:13 +000039#endif // CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070040
41#include "av1/common/alloccommon.h"
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010042#include "av1/common/cdef.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070043#include "av1/common/filter.h"
44#include "av1/common/idct.h"
45#include "av1/common/reconinter.h"
46#include "av1/common/reconintra.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070047#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070048#include "av1/common/tile_common.h"
49
Ravi Chaudharyc5e74692018-10-08 16:05:38 +053050#include "av1/encoder/av1_multi_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070051#include "av1/encoder/aq_complexity.h"
52#include "av1/encoder/aq_cyclicrefresh.h"
53#include "av1/encoder/aq_variance.h"
54#include "av1/encoder/bitstream.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070055#include "av1/encoder/context_tree.h"
56#include "av1/encoder/encodeframe.h"
57#include "av1/encoder/encodemv.h"
David Turner056f7cd2019-01-07 17:48:13 +000058#include "av1/encoder/encode_strategy.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070059#include "av1/encoder/encoder.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070060#include "av1/encoder/encodetxb.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070061#include "av1/encoder/ethread.h"
62#include "av1/encoder/firstpass.h"
Yaowu Xufa3721d2018-07-30 14:38:49 -070063#include "av1/encoder/grain_test_vectors.h"
RogerZhoucc5d35d2017-08-07 22:20:15 -070064#include "av1/encoder/hash_motion.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070065#include "av1/encoder/mbgraph.h"
David Turner0fa8c492019-02-06 16:38:13 +000066#include "av1/encoder/pass2_strategy.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070067#include "av1/encoder/picklpf.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070068#include "av1/encoder/pickrst.h"
Debargha Mukherjee7166f222017-09-05 21:32:42 -070069#include "av1/encoder/random.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070070#include "av1/encoder/ratectrl.h"
71#include "av1/encoder/rd.h"
Debargha Mukherjeedf713102018-10-02 12:33:32 -070072#include "av1/encoder/rdopt.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070073#include "av1/encoder/segmentation.h"
74#include "av1/encoder/speed_features.h"
Debargha Mukherjee347c64d2019-05-08 13:53:46 -070075#include "av1/encoder/tpl_model.h"
Yue Chen7cae98f2018-08-24 10:43:16 -070076#include "av1/encoder/reconinter_enc.h"
kyslov7b9d0d62018-12-21 11:12:26 -080077#include "av1/encoder/var_based_part.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070078
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000079#define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000080
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080081#if CONFIG_ENTROPY_STATS
82FRAME_COUNTS aggregate_fc;
83#endif // CONFIG_ENTROPY_STATS
84
Yaowu Xuc27fc142016-08-22 16:08:15 -070085#define AM_SEGMENT_ID_INACTIVE 7
86#define AM_SEGMENT_ID_ACTIVE 0
87
Debargha Mukherjeecd14cc12019-09-06 09:55:52 -070088// Q threshold for high precision mv.
89#define HIGH_PRECISION_MV_QTHRESH 128
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -070090
Yaowu Xuc27fc142016-08-22 16:08:15 -070091// #define OUTPUT_YUV_REC
Yaowu Xuc27fc142016-08-22 16:08:15 -070092#ifdef OUTPUT_YUV_SKINMAP
93FILE *yuv_skinmap_file = NULL;
94#endif
95#ifdef OUTPUT_YUV_REC
96FILE *yuv_rec_file;
97#define FILE_NAME_LEN 100
98#endif
99
Yunqing Wangdb70bf42019-08-19 09:28:11 -0700100const int default_tx_type_probs[FRAME_UPDATE_TYPES][TX_SIZES_ALL][TX_TYPES] = {
101 { { 221, 189, 214, 292, 0, 0, 0, 0, 0, 2, 38, 68, 0, 0, 0, 0 },
102 { 262, 203, 216, 239, 0, 0, 0, 0, 0, 1, 37, 66, 0, 0, 0, 0 },
103 { 315, 231, 239, 226, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0 },
104 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
105 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
106 { 222, 188, 214, 287, 0, 0, 0, 0, 0, 2, 50, 61, 0, 0, 0, 0 },
107 { 256, 182, 205, 282, 0, 0, 0, 0, 0, 2, 21, 76, 0, 0, 0, 0 },
108 { 281, 214, 217, 222, 0, 0, 0, 0, 0, 1, 48, 41, 0, 0, 0, 0 },
109 { 263, 194, 225, 225, 0, 0, 0, 0, 0, 2, 15, 100, 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 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
114 { 170, 192, 242, 293, 0, 0, 0, 0, 0, 1, 68, 58, 0, 0, 0, 0 },
115 { 199, 210, 213, 291, 0, 0, 0, 0, 0, 1, 14, 96, 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 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
120 { { 106, 69, 107, 278, 9, 15, 20, 45, 49, 23, 23, 88, 36, 74, 25, 57 },
121 { 105, 72, 81, 98, 45, 49, 47, 50, 56, 72, 30, 81, 33, 95, 27, 83 },
122 { 211, 105, 109, 120, 57, 62, 43, 49, 52, 58, 42, 116, 0, 0, 0, 0 },
123 { 1008, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0 },
124 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
125 { 131, 57, 98, 172, 19, 40, 37, 64, 69, 22, 41, 52, 51, 77, 35, 59 },
126 { 176, 83, 93, 202, 22, 24, 28, 47, 50, 16, 12, 93, 26, 76, 17, 59 },
127 { 136, 72, 89, 95, 46, 59, 47, 56, 61, 68, 35, 51, 32, 82, 26, 69 },
128 { 122, 80, 87, 105, 49, 47, 46, 46, 57, 52, 13, 90, 19, 103, 15, 93 },
129 { 1009, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0 },
130 { 1011, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0 },
131 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
132 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
133 { 202, 20, 84, 114, 14, 60, 41, 79, 99, 21, 41, 15, 50, 84, 34, 66 },
134 { 196, 44, 23, 72, 30, 22, 28, 57, 67, 13, 4, 165, 15, 148, 9, 131 },
135 { 882, 0, 0, 0, 0, 0, 0, 0, 0, 142, 0, 0, 0, 0, 0, 0 },
136 { 840, 0, 0, 0, 0, 0, 0, 0, 0, 184, 0, 0, 0, 0, 0, 0 },
137 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
138 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
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 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 } },
158 { { 213, 110, 141, 269, 12, 16, 15, 19, 21, 11, 38, 68, 22, 29, 16, 24 },
159 { 216, 119, 128, 143, 38, 41, 26, 30, 31, 30, 42, 70, 23, 36, 19, 32 },
160 { 367, 149, 154, 154, 38, 35, 17, 21, 21, 10, 22, 36, 0, 0, 0, 0 },
161 { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
162 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
163 { 219, 96, 127, 191, 21, 40, 25, 32, 34, 18, 45, 45, 33, 39, 26, 33 },
164 { 296, 99, 122, 198, 23, 21, 19, 24, 25, 13, 20, 64, 23, 32, 18, 27 },
165 { 275, 128, 142, 143, 35, 48, 23, 30, 29, 18, 42, 36, 18, 23, 14, 20 },
166 { 239, 132, 166, 175, 36, 27, 19, 21, 24, 14, 13, 85, 9, 31, 8, 25 },
167 { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
168 { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
169 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
170 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
171 { 309, 25, 79, 59, 25, 80, 34, 53, 61, 25, 49, 23, 43, 64, 36, 59 },
172 { 270, 57, 40, 54, 50, 42, 41, 53, 56, 28, 17, 81, 45, 86, 34, 70 },
173 { 1005, 0, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 0, 0, 0 },
174 { 992, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0 },
175 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
176 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
177 { { 133, 63, 55, 83, 57, 87, 58, 72, 68, 16, 24, 35, 29, 105, 25, 114 },
178 { 131, 75, 74, 60, 71, 77, 65, 66, 73, 33, 21, 79, 20, 83, 18, 78 },
179 { 276, 95, 82, 58, 86, 93, 63, 60, 64, 17, 38, 92, 0, 0, 0, 0 },
180 { 1006, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0 },
181 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
182 { 147, 49, 75, 78, 50, 97, 60, 67, 76, 17, 42, 35, 31, 93, 27, 80 },
183 { 157, 49, 58, 75, 61, 52, 56, 67, 69, 12, 15, 79, 24, 119, 11, 120 },
184 { 178, 69, 83, 77, 69, 85, 72, 77, 77, 20, 35, 40, 25, 48, 23, 46 },
185 { 174, 55, 64, 57, 73, 68, 62, 61, 75, 15, 12, 90, 17, 99, 16, 86 },
186 { 1008, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0 },
187 { 1018, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0 },
188 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
189 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
190 { 266, 31, 63, 64, 21, 52, 39, 54, 63, 30, 52, 31, 48, 89, 46, 75 },
191 { 272, 26, 32, 44, 29, 31, 32, 53, 51, 13, 13, 88, 22, 153, 16, 149 },
192 { 923, 0, 0, 0, 0, 0, 0, 0, 0, 101, 0, 0, 0, 0, 0, 0 },
193 { 969, 0, 0, 0, 0, 0, 0, 0, 0, 55, 0, 0, 0, 0, 0, 0 },
194 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
195 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
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 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 } },
215 { { 158, 92, 125, 298, 12, 15, 20, 29, 31, 12, 29, 67, 34, 44, 23, 35 },
216 { 147, 94, 103, 123, 45, 48, 38, 41, 46, 48, 37, 78, 33, 63, 27, 53 },
217 { 268, 126, 125, 136, 54, 53, 31, 38, 38, 33, 35, 87, 0, 0, 0, 0 },
218 { 1018, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0 },
219 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
220 { 159, 72, 103, 194, 20, 35, 37, 50, 56, 21, 39, 40, 51, 61, 38, 48 },
221 { 259, 86, 95, 188, 32, 20, 25, 34, 37, 13, 12, 85, 25, 53, 17, 43 },
222 { 189, 99, 113, 123, 45, 59, 37, 46, 48, 44, 39, 41, 31, 47, 26, 37 },
223 { 175, 110, 113, 128, 58, 38, 33, 33, 43, 29, 13, 100, 14, 68, 12, 57 },
224 { 1017, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0 },
225 { 1019, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0 },
226 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
227 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
228 { 208, 22, 84, 101, 21, 59, 44, 70, 90, 25, 59, 13, 64, 67, 49, 48 },
229 { 277, 52, 32, 63, 43, 26, 33, 48, 54, 11, 6, 130, 18, 119, 11, 101 },
230 { 963, 0, 0, 0, 0, 0, 0, 0, 0, 61, 0, 0, 0, 0, 0, 0 },
231 { 979, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 0, 0, 0 },
232 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
233 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
234};
235
Yunqing Wang5f74dc22019-10-29 10:35:20 -0700236const int default_obmc_probs[FRAME_UPDATE_TYPES][BLOCK_SIZES_ALL] = {
237 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
238 { 0, 0, 0, 106, 90, 90, 97, 67, 59, 70, 28,
239 30, 38, 16, 16, 16, 0, 0, 44, 50, 26, 25 },
240 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
241 { 0, 0, 0, 98, 93, 97, 68, 82, 85, 33, 30,
242 33, 16, 16, 16, 16, 0, 0, 43, 37, 26, 16 },
243 { 0, 0, 0, 91, 80, 76, 78, 55, 49, 24, 16,
244 16, 16, 16, 16, 16, 0, 0, 29, 45, 16, 38 },
245 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
246 { 0, 0, 0, 103, 89, 89, 89, 62, 63, 76, 34,
247 35, 32, 19, 16, 16, 0, 0, 49, 55, 29, 19 }
248};
249
Yaowu Xuf883b422016-08-30 14:01:10 -0700250static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700251 switch (mode) {
252 case NORMAL:
253 *hr = 1;
254 *hs = 1;
255 break;
256 case FOURFIVE:
257 *hr = 4;
258 *hs = 5;
259 break;
260 case THREEFIVE:
261 *hr = 3;
262 *hs = 5;
263 break;
264 case ONETWO:
265 *hr = 1;
266 *hs = 2;
267 break;
268 default:
269 *hr = 1;
270 *hs = 1;
271 assert(0);
272 break;
273 }
274}
275
276// Mark all inactive blocks as active. Other segmentation features may be set
277// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700278static void suppress_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700279 unsigned char *const seg_map = cpi->segmentation_map;
280 int i;
281 if (cpi->active_map.enabled || cpi->active_map.update)
282 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
283 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
284 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
285}
286
Yaowu Xuf883b422016-08-30 14:01:10 -0700287static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700288 struct segmentation *const seg = &cpi->common.seg;
289 unsigned char *const seg_map = cpi->segmentation_map;
290 const unsigned char *const active_map = cpi->active_map.map;
291 int i;
292
293 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
294
295 if (frame_is_intra_only(&cpi->common)) {
296 cpi->active_map.enabled = 0;
297 cpi->active_map.update = 1;
298 }
299
300 if (cpi->active_map.update) {
301 if (cpi->active_map.enabled) {
302 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
303 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700304 av1_enable_segmentation(seg);
305 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700306 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
307 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
308 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
309 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
310
311 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
312 -MAX_LOOP_FILTER);
313 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
314 -MAX_LOOP_FILTER);
315 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
316 -MAX_LOOP_FILTER);
317 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
318 -MAX_LOOP_FILTER);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700319 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700320 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700321 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
322 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
323 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
324 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700325 if (seg->enabled) {
326 seg->update_data = 1;
327 seg->update_map = 1;
328 }
329 }
330 cpi->active_map.update = 0;
331 }
332}
333
Yaowu Xuf883b422016-08-30 14:01:10 -0700334int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
335 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700336 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
337 unsigned char *const active_map_8x8 = cpi->active_map.map;
338 const int mi_rows = cpi->common.mi_rows;
339 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700340 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
341 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700342 cpi->active_map.update = 1;
343 if (new_map_16x16) {
344 int r, c;
345 for (r = 0; r < mi_rows; ++r) {
346 for (c = 0; c < mi_cols; ++c) {
347 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700348 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700349 ? AM_SEGMENT_ID_ACTIVE
350 : AM_SEGMENT_ID_INACTIVE;
351 }
352 }
353 cpi->active_map.enabled = 1;
354 } else {
355 cpi->active_map.enabled = 0;
356 }
357 return 0;
358 } else {
359 return -1;
360 }
361}
362
Yaowu Xuf883b422016-08-30 14:01:10 -0700363int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
364 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700365 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
366 new_map_16x16) {
367 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
368 const int mi_rows = cpi->common.mi_rows;
369 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700370 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
371 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
372
Yaowu Xuc27fc142016-08-22 16:08:15 -0700373 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
374 if (cpi->active_map.enabled) {
375 int r, c;
376 for (r = 0; r < mi_rows; ++r) {
377 for (c = 0; c < mi_cols; ++c) {
378 // Cyclic refresh segments are considered active despite not having
379 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700380 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700381 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
382 }
383 }
384 }
385 return 0;
386 } else {
387 return -1;
388 }
389}
390
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800391// Compute the horizontal frequency components' energy in a frame
392// by calculuating the 16x4 Horizontal DCT. This is to be used to
393// decide the superresolution parameters.
Yaowu Xubedbf4f2019-05-01 17:54:36 -0700394static void analyze_hor_freq(const AV1_COMP *cpi, double *energy) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800395 uint64_t freq_energy[16] = { 0 };
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800396 const YV12_BUFFER_CONFIG *buf = cpi->source;
397 const int bd = cpi->td.mb.e_mbd.bd;
398 const int width = buf->y_crop_width;
399 const int height = buf->y_crop_height;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800400 DECLARE_ALIGNED(16, int32_t, coeff[16 * 4]);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800401 int n = 0;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800402 memset(freq_energy, 0, sizeof(freq_energy));
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800403 if (buf->flags & YV12_FLAG_HIGHBITDEPTH) {
404 const int16_t *src16 = (const int16_t *)CONVERT_TO_SHORTPTR(buf->y_buffer);
405 for (int i = 0; i < height - 4; i += 4) {
406 for (int j = 0; j < width - 16; j += 16) {
407 av1_fwd_txfm2d_16x4(src16 + i * buf->y_stride + j, coeff, buf->y_stride,
408 H_DCT, bd);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800409 for (int k = 1; k < 16; ++k) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800410 const uint64_t this_energy =
411 ((int64_t)coeff[k] * coeff[k]) +
412 ((int64_t)coeff[k + 16] * coeff[k + 16]) +
413 ((int64_t)coeff[k + 32] * coeff[k + 32]) +
414 ((int64_t)coeff[k + 48] * coeff[k + 48]);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800415 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2 + 2 * (bd - 8));
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800416 }
417 n++;
418 }
419 }
420 } else {
Debargha Mukherjeeac28c722018-11-14 22:09:46 -0800421 assert(bd == 8);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800422 DECLARE_ALIGNED(16, int16_t, src16[16 * 4]);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800423 for (int i = 0; i < height - 4; i += 4) {
424 for (int j = 0; j < width - 16; j += 16) {
425 for (int ii = 0; ii < 4; ++ii)
426 for (int jj = 0; jj < 16; ++jj)
427 src16[ii * 16 + jj] =
428 buf->y_buffer[(i + ii) * buf->y_stride + (j + jj)];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800429 av1_fwd_txfm2d_16x4(src16, coeff, 16, H_DCT, bd);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800430 for (int k = 1; k < 16; ++k) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800431 const uint64_t this_energy =
432 ((int64_t)coeff[k] * coeff[k]) +
433 ((int64_t)coeff[k + 16] * coeff[k + 16]) +
434 ((int64_t)coeff[k + 32] * coeff[k + 32]) +
435 ((int64_t)coeff[k + 48] * coeff[k + 48]);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800436 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800437 }
438 n++;
439 }
440 }
441 }
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800442 if (n) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800443 for (int k = 1; k < 16; ++k) energy[k] = (double)freq_energy[k] / n;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800444 // Convert to cumulative energy
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800445 for (int k = 14; k > 0; --k) energy[k] += energy[k + 1];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800446 } else {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800447 for (int k = 1; k < 16; ++k) energy[k] = 1e+20;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800448 }
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800449}
450
Yaowu Xu45295c32018-03-29 12:06:10 -0700451static void set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv,
452 int cur_frame_force_integer_mv) {
Debargha Mukherjee8d273412019-09-11 10:36:50 -0700453 MACROBLOCK *const x = &cpi->td.mb;
Hui Su50361152018-03-02 11:01:42 -0800454 cpi->common.allow_high_precision_mv =
455 allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Rupert Swarbricka84faf22017-12-11 13:56:40 +0000456 const int copy_hp =
457 cpi->common.allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Debargha Mukherjee8d273412019-09-11 10:36:50 -0700458 x->nmvcost[0] = &x->nmv_costs[0][MV_MAX];
459 x->nmvcost[1] = &x->nmv_costs[1][MV_MAX];
460 x->nmvcost_hp[0] = &x->nmv_costs_hp[0][MV_MAX];
461 x->nmvcost_hp[1] = &x->nmv_costs_hp[1][MV_MAX];
462 int *(*src)[2] = copy_hp ? &x->nmvcost_hp : &x->nmvcost;
463 x->mv_cost_stack = *src;
James Zern01a9d702017-08-25 19:09:33 +0000464}
465
Yaowu Xuf883b422016-08-30 14:01:10 -0700466static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Urvang Joshie4530f82018-01-09 11:43:37 -0800467 const AV1_COMMON *const cm = &cpi->common;
James Zernbf3ca362019-10-21 11:33:44 -0700468
Yaowu Xuf883b422016-08-30 14:01:10 -0700469 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700470 return BLOCK_64X64;
Ryan Leic8b6dd62019-10-23 20:01:26 -0700471 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
472 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700473
Yaowu Xuf883b422016-08-30 14:01:10 -0700474 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700475
Ryan Leic8b6dd62019-10-23 20:01:26 -0700476 // TODO(any): Possibly could improve this with a heuristic.
Urvang Joshiaab74432018-06-01 12:06:22 -0700477 // When superres / resize is on, 'cm->width / height' can change between
Hui Su3e3b9342019-04-12 18:27:28 +0000478 // calls, so we don't apply this heuristic there.
479 // Things break if superblock size changes between the first pass and second
480 // pass encoding, which is why this heuristic is not configured as a
481 // speed-feature.
Urvang Joshiaab74432018-06-01 12:06:22 -0700482 if (cpi->oxcf.superres_mode == SUPERRES_NONE &&
Hui Su3e3b9342019-04-12 18:27:28 +0000483 cpi->oxcf.resize_mode == RESIZE_NONE && cpi->oxcf.speed >= 1) {
484 return AOMMIN(cm->width, cm->height) > 480 ? BLOCK_128X128 : BLOCK_64X64;
Urvang Joshie4530f82018-01-09 11:43:37 -0800485 }
486
Yaowu Xuc27fc142016-08-22 16:08:15 -0700487 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700488}
489
Yaowu Xuf883b422016-08-30 14:01:10 -0700490static void setup_frame(AV1_COMP *cpi) {
491 AV1_COMMON *const cm = &cpi->common;
Johannb0ef6ff2018-02-08 14:32:21 -0800492 // Set up entropy context depending on frame type. The decoder mandates
493 // the use of the default context, index 0, for keyframes and inter
494 // frames where the error_resilient_mode or intra_only flag is set. For
495 // other inter-frames the encoder currently uses only two contexts;
496 // context 1 for ALTREF frames and context 0 for the others.
Soo-Chul Han85e8c792018-01-21 01:58:15 -0500497
Sarah Parker50b6d6e2018-04-11 19:21:54 -0700498 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
David Turnera7f133c2019-01-22 14:47:16 +0000499 cpi->ext_use_primary_ref_none) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700500 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700501 }
502
Hui Sueb4b7de2019-04-03 11:00:18 -0700503 if ((cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) ||
504 frame_is_sframe(cm)) {
505 if (!cpi->seq_params_locked) {
506 set_sb_size(&cm->seq_params, select_sb_size(cpi));
507 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700508 } else {
David Turnera21966b2018-12-05 14:48:49 +0000509 const RefCntBuffer *const primary_ref_buf = get_primary_ref_frame_buf(cm);
510 if (primary_ref_buf == NULL) {
David Barkercc615a82018-03-19 14:38:51 +0000511 av1_setup_past_independence(cm);
512 cm->seg.update_map = 1;
513 cm->seg.update_data = 1;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700514 } else {
David Turnera21966b2018-12-05 14:48:49 +0000515 *cm->fc = primary_ref_buf->frame_context;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700516 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700517 }
518
David Turnerbc0993e2019-02-15 14:42:23 +0000519 av1_zero(cm->cur_frame->interp_filter_selected);
David Turnera21966b2018-12-05 14:48:49 +0000520 cm->prev_frame = get_primary_ref_frame_buf(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700521 cpi->vaq_refresh = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700522}
523
chiyotsaia7091f12019-08-09 16:48:27 -0700524static void enc_set_mb_mi(AV1_COMMON *cm, int width, int height) {
525 // Ensure that the decoded width and height are both multiples of
526 // 8 luma pixels (note: this may only be a multiple of 4 chroma pixels if
527 // subsampling is used).
528 // This simplifies the implementation of various experiments,
529 // eg. cdef, which operates on units of 8x8 luma pixels.
530 const int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
531 const int aligned_height = ALIGN_POWER_OF_TWO(height, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700532
chiyotsaia7091f12019-08-09 16:48:27 -0700533 cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
534 cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
535 cm->mi_stride = calc_mi_size(cm->mi_cols);
536
537 cm->mb_cols = (cm->mi_cols + 2) >> 2;
538 cm->mb_rows = (cm->mi_rows + 2) >> 2;
539 cm->MBs = cm->mb_rows * cm->mb_cols;
540
541 const int is_4k_or_larger = AOMMIN(width, height) >= 2160;
542
543 cm->mi_alloc_bsize = is_4k_or_larger ? BLOCK_8X8 : BLOCK_4X4;
544 const int mi_alloc_size_1d = mi_size_wide[cm->mi_alloc_bsize];
545 cm->mi_alloc_rows = (cm->mi_rows + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
546 cm->mi_alloc_cols = (cm->mi_cols + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
547 cm->mi_alloc_stride =
548 (cm->mi_stride + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
549
550 assert(mi_size_wide[cm->mi_alloc_bsize] == mi_size_high[cm->mi_alloc_bsize]);
551
552#if CONFIG_LPF_MASK
chiyotsaia13c19f2019-08-15 10:12:57 -0700553 av1_alloc_loop_filter_mask(cm);
chiyotsaia7091f12019-08-09 16:48:27 -0700554#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700555}
556
chiyotsaia7091f12019-08-09 16:48:27 -0700557static void enc_setup_mi(AV1_COMMON *cm) {
558 const int mi_grid_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
559 memset(cm->mi, 0, cm->mi_alloc_size * sizeof(*cm->mi));
chiyotsaia7091f12019-08-09 16:48:27 -0700560 memset(cm->mi_grid_base, 0, mi_grid_size * sizeof(*cm->mi_grid_base));
Hui Su52b7ddc2019-10-10 16:27:16 -0700561 memset(cm->tx_type_map, 0, mi_grid_size * sizeof(*cm->tx_type_map));
chiyotsaia7091f12019-08-09 16:48:27 -0700562}
563
564static int enc_alloc_mi(AV1_COMMON *cm) {
565 const int mi_grid_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
566 const int alloc_size_1d = mi_size_wide[cm->mi_alloc_bsize];
567 const int alloc_mi_size =
568 cm->mi_alloc_stride * (calc_mi_size(cm->mi_rows) / alloc_size_1d);
569
570 if (cm->mi_alloc_size < alloc_mi_size || cm->mi_grid_size < mi_grid_size) {
571 cm->free_mi(cm);
572
573 cm->mi = aom_calloc(alloc_mi_size, sizeof(*cm->mi));
574 if (!cm->mi) return 1;
575 cm->mi_alloc_size = alloc_mi_size;
576
577 cm->mi_grid_base =
578 (MB_MODE_INFO **)aom_calloc(mi_grid_size, sizeof(MB_MODE_INFO *));
579 if (!cm->mi_grid_base) return 1;
580 cm->mi_grid_size = mi_grid_size;
Hui Su52b7ddc2019-10-10 16:27:16 -0700581
582 cm->tx_type_map = aom_calloc(calc_mi_size(cm->mi_rows) * cm->mi_stride,
583 sizeof(*cm->tx_type_map));
584 if (!cm->tx_type_map) return 1;
chiyotsaia7091f12019-08-09 16:48:27 -0700585 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700586
587 return 0;
588}
589
Cheng Chen46f30c72017-09-07 11:13:33 -0700590static void enc_free_mi(AV1_COMMON *cm) {
chiyotsai04ca87d2019-05-24 15:06:19 -0700591 aom_free(cm->mi);
592 cm->mi = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700593 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700594 cm->mi_grid_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700595 cm->mi_alloc_size = 0;
Hui Su52b7ddc2019-10-10 16:27:16 -0700596 aom_free(cm->tx_type_map);
597 cm->tx_type_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700598}
599
Yaowu Xuf883b422016-08-30 14:01:10 -0700600void av1_initialize_enc(void) {
Wan-Teh Chang3cac4542018-06-29 10:21:39 -0700601 av1_rtcd();
602 aom_dsp_rtcd();
603 aom_scale_rtcd();
604 av1_init_intra_predictors();
605 av1_init_me_luts();
606 av1_rc_init_minq_luts();
607 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700608}
609
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700610static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
Remya0cce44c2019-08-16 11:57:24 +0530611 if (cpi->mbmi_ext_frame_base) {
612 aom_free(cpi->mbmi_ext_frame_base);
613 cpi->mbmi_ext_frame_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700614 }
615}
616
617static void alloc_context_buffers_ext(AV1_COMP *cpi) {
618 AV1_COMMON *cm = &cpi->common;
chiyotsaia7091f12019-08-09 16:48:27 -0700619 const int new_ext_mi_size = cm->mi_alloc_rows * cm->mi_alloc_cols;
chiyotsai426c0662019-08-05 16:15:11 -0700620
chiyotsaia7091f12019-08-09 16:48:27 -0700621 if (new_ext_mi_size > cpi->mi_ext_alloc_size) {
622 dealloc_context_buffers_ext(cpi);
Remya0cce44c2019-08-16 11:57:24 +0530623 CHECK_MEM_ERROR(
624 cm, cpi->mbmi_ext_frame_base,
625 aom_calloc(new_ext_mi_size, sizeof(*cpi->mbmi_ext_frame_base)));
chiyotsaia7091f12019-08-09 16:48:27 -0700626 cpi->mi_ext_alloc_size = new_ext_mi_size;
627 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700628}
629
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800630static void reset_film_grain_chroma_params(aom_film_grain_t *pars) {
631 pars->num_cr_points = 0;
632 pars->cr_mult = 0;
633 pars->cr_luma_mult = 0;
634 memset(pars->scaling_points_cr, 0, sizeof(pars->scaling_points_cr));
635 memset(pars->ar_coeffs_cr, 0, sizeof(pars->ar_coeffs_cr));
636 pars->num_cb_points = 0;
637 pars->cb_mult = 0;
638 pars->cb_luma_mult = 0;
Yaowu Xufda7dcb2019-01-16 13:04:33 -0800639 pars->chroma_scaling_from_luma = 0;
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800640 memset(pars->scaling_points_cb, 0, sizeof(pars->scaling_points_cb));
641 memset(pars->ar_coeffs_cb, 0, sizeof(pars->ar_coeffs_cb));
642}
643
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800644static void update_film_grain_parameters(struct AV1_COMP *cpi,
645 const AV1EncoderConfig *oxcf) {
646 AV1_COMMON *const cm = &cpi->common;
647 cpi->oxcf = *oxcf;
648
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700649 if (cpi->film_grain_table) {
650 aom_film_grain_table_free(cpi->film_grain_table);
651 aom_free(cpi->film_grain_table);
652 cpi->film_grain_table = NULL;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700653 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700654
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800655 if (oxcf->film_grain_test_vector) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700656 cm->seq_params.film_grain_params_present = 1;
David Turnerd2a592e2018-11-16 14:59:31 +0000657 if (cm->current_frame.frame_type == KEY_FRAME) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800658 memcpy(&cm->film_grain_params,
659 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
660 sizeof(cm->film_grain_params));
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800661 if (oxcf->monochrome)
662 reset_film_grain_chroma_params(&cm->film_grain_params);
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700663 cm->film_grain_params.bit_depth = cm->seq_params.bit_depth;
664 if (cm->seq_params.color_range == AOM_CR_FULL_RANGE) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800665 cm->film_grain_params.clip_to_restricted_range = 0;
666 }
667 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700668 } else if (oxcf->film_grain_table_filename) {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800669 cm->seq_params.film_grain_params_present = 1;
670
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700671 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
672 memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700673
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700674 aom_film_grain_table_read(cpi->film_grain_table,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700675 oxcf->film_grain_table_filename, &cm->error);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800676 } else {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800677#if CONFIG_DENOISE
678 cm->seq_params.film_grain_params_present = (cpi->oxcf.noise_level > 0);
679#else
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700680 cm->seq_params.film_grain_params_present = 0;
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800681#endif
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800682 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
683 }
684}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800685
Yaowu Xuf883b422016-08-30 14:01:10 -0700686static void dealloc_compressor_data(AV1_COMP *cpi) {
687 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000688 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700690 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700691
Yaowu Xuf883b422016-08-30 14:01:10 -0700692 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700693 cpi->tile_data = NULL;
694
695 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700696 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700697 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700698
Yaowu Xuf883b422016-08-30 14:01:10 -0700699 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700700 cpi->cyclic_refresh = NULL;
701
Yaowu Xuf883b422016-08-30 14:01:10 -0700702 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700703 cpi->active_map.map = NULL;
704
sdengc23c7f12019-06-11 16:56:50 -0700705 aom_free(cpi->ssim_rdmult_scaling_factors);
706 cpi->ssim_rdmult_scaling_factors = NULL;
707
sdengf46a1062019-08-04 18:43:50 -0700708 aom_free(cpi->tpl_rdmult_scaling_factors);
709 cpi->tpl_rdmult_scaling_factors = NULL;
710
711 aom_free(cpi->tpl_sb_rdmult_scaling_factors);
712 cpi->tpl_sb_rdmult_scaling_factors = NULL;
713
Jingning Hand064cf02017-06-01 10:00:39 -0700714 aom_free(cpi->td.mb.above_pred_buf);
715 cpi->td.mb.above_pred_buf = NULL;
716
717 aom_free(cpi->td.mb.left_pred_buf);
718 cpi->td.mb.left_pred_buf = NULL;
719
720 aom_free(cpi->td.mb.wsrc_buf);
721 cpi->td.mb.wsrc_buf = NULL;
722
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530723 aom_free(cpi->td.mb.inter_modes_info);
724 cpi->td.mb.inter_modes_info = NULL;
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530725
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530726 for (int i = 0; i < 2; i++)
727 for (int j = 0; j < 2; j++) {
728 aom_free(cpi->td.mb.hash_value_buffer[i][j]);
729 cpi->td.mb.hash_value_buffer[i][j] = NULL;
730 }
Jingning Hand064cf02017-06-01 10:00:39 -0700731 aom_free(cpi->td.mb.mask_buf);
732 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700733
Jingning Han6cc1fd32017-10-13 09:05:36 -0700734 aom_free(cm->tpl_mvs);
735 cm->tpl_mvs = NULL;
Jingning Han6cc1fd32017-10-13 09:05:36 -0700736
Remya0cce44c2019-08-16 11:57:24 +0530737 aom_free(cpi->td.mb.mbmi_ext);
738 cpi->td.mb.mbmi_ext = NULL;
739
Yaowu Xuf883b422016-08-30 14:01:10 -0700740 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700741 av1_free_txb_buf(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700742 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700743
Yaowu Xuf883b422016-08-30 14:01:10 -0700744 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuf883b422016-08-30 14:01:10 -0700745 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800746 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuf883b422016-08-30 14:01:10 -0700747 aom_free_frame_buffer(&cpi->scaled_source);
748 aom_free_frame_buffer(&cpi->scaled_last_source);
749 aom_free_frame_buffer(&cpi->alt_ref_buffer);
750 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700751
Yaowu Xuf883b422016-08-30 14:01:10 -0700752 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700753 cpi->tile_tok[0][0] = 0;
754
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530755 aom_free(cpi->tplist[0][0]);
756 cpi->tplist[0][0] = NULL;
757
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000758 av1_free_pc_tree(&cpi->td, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700759
hui sud9a812b2017-07-06 14:34:37 -0700760 aom_free(cpi->td.mb.palette_buffer);
Hui Su38711e72019-06-11 10:49:47 -0700761 av1_release_compound_type_rd_buffers(&cpi->td.mb.comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700762 aom_free(cpi->td.mb.tmp_conv_dst);
763 for (int j = 0; j < 2; ++j) {
764 aom_free(cpi->td.mb.tmp_obmc_bufs[j]);
765 }
766
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700767#if CONFIG_DENOISE
768 if (cpi->denoise_and_model) {
769 aom_denoise_and_model_free(cpi->denoise_and_model);
770 cpi->denoise_and_model = NULL;
771 }
772#endif
773 if (cpi->film_grain_table) {
774 aom_film_grain_table_free(cpi->film_grain_table);
775 cpi->film_grain_table = NULL;
776 }
Hui Suc3a8d372019-05-28 15:52:45 -0700777
778 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
779 aom_free(cpi->level_info[i]);
780 }
Marco Paniconi63971322019-08-15 21:32:05 -0700781
782 if (cpi->use_svc) av1_free_svc_cyclic_refresh(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700783}
784
Yaowu Xuf883b422016-08-30 14:01:10 -0700785static void configure_static_seg_features(AV1_COMP *cpi) {
786 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700787 const RATE_CONTROL *const rc = &cpi->rc;
788 struct segmentation *const seg = &cm->seg;
789
790 int high_q = (int)(rc->avg_q > 48.0);
791 int qi_delta;
792
793 // Disable and clear down for KF
David Turnerd2a592e2018-11-16 14:59:31 +0000794 if (cm->current_frame.frame_type == KEY_FRAME) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700795 // Clear down the global segmentation map
796 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
797 seg->update_map = 0;
798 seg->update_data = 0;
799 cpi->static_mb_pct = 0;
800
801 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700802 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700803
804 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700805 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700806 } else if (cpi->refresh_alt_ref_frame) {
807 // If this is an alt ref frame
808 // Clear down the global segmentation map
809 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
810 seg->update_map = 0;
811 seg->update_data = 0;
812 cpi->static_mb_pct = 0;
813
814 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700815 av1_disable_segmentation(seg);
816 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700817
Jerome Jiang6acbfdc2019-05-30 10:05:17 -0700818#if !CONFIG_REALTIME_ONLY
Yaowu Xuc27fc142016-08-22 16:08:15 -0700819 // Scan frames from current to arf frame.
820 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700821 av1_update_mbgraph_stats(cpi);
Jerome Jiang6acbfdc2019-05-30 10:05:17 -0700822#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700823
824 // If segmentation was enabled set those features needed for the
825 // arf itself.
826 if (seg->enabled) {
827 seg->update_map = 1;
828 seg->update_data = 1;
829
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700830 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
831 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700832 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700833 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
834 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
835 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
836 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
837
838 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
839 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
840 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
841 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700842
Yaowu Xuf883b422016-08-30 14:01:10 -0700843 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700844 }
845 } else if (seg->enabled) {
846 // All other frames if segmentation has been enabled
847
848 // First normal frame in a valid gf or alt ref group
849 if (rc->frames_since_golden == 0) {
850 // Set up segment features for normal frames in an arf group
851 if (rc->source_alt_ref_active) {
852 seg->update_map = 0;
853 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700854
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700855 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
856 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700857 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
858 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700859
Cheng Chend8184da2017-09-26 18:15:22 -0700860 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
861 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
862 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
863 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
864
865 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
866 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
867 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
868 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700869
870 // Segment coding disabled for compred testing
871 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700872 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
873 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
874 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700875 }
876 } else {
877 // Disable segmentation and clear down features if alt ref
878 // is not active for this group
879
Yaowu Xuf883b422016-08-30 14:01:10 -0700880 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700881
882 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
883
884 seg->update_map = 0;
885 seg->update_data = 0;
886
Yaowu Xuf883b422016-08-30 14:01:10 -0700887 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700888 }
889 } else if (rc->is_src_frame_alt_ref) {
890 // Special case where we are coding over the top of a previous
891 // alt ref frame.
892 // Segment coding disabled for compred testing
893
894 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700895 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
896 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700897
898 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700899 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
900 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
901 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
902 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700903
904 // Skip all MBs if high Q (0,0 mv and skip coeffs)
905 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700906 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
907 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700908 }
909 // Enable data update
910 seg->update_data = 1;
911 } else {
912 // All other frames.
913
914 // No updates.. leave things as they are.
915 seg->update_map = 0;
916 seg->update_data = 0;
917 }
918 }
919}
920
Yaowu Xuf883b422016-08-30 14:01:10 -0700921static void update_reference_segmentation_map(AV1_COMP *cpi) {
922 AV1_COMMON *const cm = &cpi->common;
chiyotsai2202ba02019-07-12 16:09:21 -0700923 MB_MODE_INFO **mi_4x4_ptr = cm->mi_grid_base;
David Turnerb757ce02018-11-12 15:01:28 +0000924 uint8_t *cache_ptr = cm->cur_frame->seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700925 int row, col;
926
927 for (row = 0; row < cm->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -0700928 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700929 uint8_t *cache = cache_ptr;
Yushin Choa7f65922018-04-04 16:06:11 -0700930 for (col = 0; col < cm->mi_cols; col++, mi_4x4++, cache++)
931 cache[0] = mi_4x4[0]->segment_id;
932 mi_4x4_ptr += cm->mi_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700933 cache_ptr += cm->mi_cols;
934 }
935}
936
Yaowu Xuf883b422016-08-30 14:01:10 -0700937static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
938 AV1_COMMON *cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700939 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -0700940 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yunqing Wang1d569be2019-10-09 13:23:12 -0700941 int is_scale = (oxcf->resize_mode || oxcf->superres_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700942
Satish Kumar Suman7a7239b2019-03-13 14:48:14 +0530943 if (!cpi->lookahead) {
Satish Kumar Suman29909962019-01-09 10:31:21 +0530944 cpi->lookahead = av1_lookahead_init(
945 oxcf->width, oxcf->height, seq_params->subsampling_x,
946 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman7a7239b2019-03-13 14:48:14 +0530947 oxcf->lag_in_frames, oxcf->border_in_pixels, is_scale);
948 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700949 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700950 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700951 "Failed to allocate lag buffers");
952
953 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Yunqing Wang1d569be2019-10-09 13:23:12 -0700954 // (yunqing)Here use same border as lookahead buffers.
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700955 if (aom_realloc_frame_buffer(
956 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
957 seq_params->subsampling_x, seq_params->subsampling_y,
Yunqing Wang1d569be2019-10-09 13:23:12 -0700958 seq_params->use_highbitdepth,
959 is_scale ? oxcf->border_in_pixels : AOM_ENC_LOOKAHEAD_BORDER,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700960 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700961 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700962 "Failed to allocate altref buffer");
963}
964
Yaowu Xuf883b422016-08-30 14:01:10 -0700965static void alloc_util_frame_buffers(AV1_COMP *cpi) {
966 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700967 const SequenceHeader *const seq_params = &cm->seq_params;
968 if (aom_realloc_frame_buffer(
969 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
970 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530971 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700972 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700973 "Failed to allocate last frame buffer");
974
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700975 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800976 &cpi->trial_frame_rst, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700977 cm->superres_upscaled_height, seq_params->subsampling_x,
978 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman3b12c002018-12-19 15:27:20 +0530979 AOM_RESTORATION_FRAME_BORDER, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800980 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800981 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700982
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700983 if (aom_realloc_frame_buffer(
984 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
985 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530986 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700987 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700988 "Failed to allocate scaled source buffer");
989
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700990 if (aom_realloc_frame_buffer(
991 &cpi->scaled_last_source, cm->width, cm->height,
992 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530993 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700994 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700995 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700996 "Failed to allocate scaled last source buffer");
997}
998
Cheng Chen46f30c72017-09-07 11:13:33 -0700999static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001000 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001001 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001002
chiyotsaia7091f12019-08-09 16:48:27 -07001003 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
1004 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1005 "Failed to allocate context buffers");
1006 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001007
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +05301008 int mi_rows_aligned_to_sb =
1009 ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
1010 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
1011
Angie Chiangf0fbf9d2017-03-15 15:01:22 -07001012 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -07001013
Yaowu Xuc27fc142016-08-22 16:08:15 -07001014 alloc_context_buffers_ext(cpi);
1015
Yaowu Xuf883b422016-08-30 14:01:10 -07001016 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001017
1018 {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001019 unsigned int tokens =
1020 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001021 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -07001022 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001023 }
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +05301024 aom_free(cpi->tplist[0][0]);
1025
1026 CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
1027 aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
1028 sizeof(*cpi->tplist[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001029
Yaowu Xuf883b422016-08-30 14:01:10 -07001030 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001031}
1032
Yaowu Xuf883b422016-08-30 14:01:10 -07001033void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001034 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001035 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001036}
1037
Hui Suef139e12019-05-20 15:51:22 -07001038double av1_get_compression_ratio(const AV1_COMMON *const cm,
1039 size_t encoded_frame_size) {
1040 const int upscaled_width = cm->superres_upscaled_width;
1041 const int height = cm->height;
1042 const int luma_pic_size = upscaled_width * height;
1043 const SequenceHeader *const seq_params = &cm->seq_params;
1044 const BITSTREAM_PROFILE profile = seq_params->profile;
1045 const int pic_size_profile_factor =
1046 profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
1047 encoded_frame_size =
1048 (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
1049 const size_t uncompressed_frame_size =
1050 (luma_pic_size * pic_size_profile_factor) >> 3;
1051 return uncompressed_frame_size / (double)encoded_frame_size;
1052}
1053
Yunqing Wang75e20e82018-06-16 12:10:48 -07001054static void set_tile_info(AV1_COMP *cpi) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001055 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +02001056 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001057
1058 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001059
1060 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +02001061 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +02001062 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001063 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
1064 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +02001065 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001066 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
1067 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001068 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +02001069 cm->uniform_tile_spacing_flag = 0;
1070 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
1071 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001072 size_sb = cpi->oxcf.tile_widths[j++];
1073 if (j >= cpi->oxcf.tile_width_count) j = 0;
David Barker6cd5a822018-03-05 16:19:28 +00001074 start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +02001075 }
1076 cm->tile_cols = i;
1077 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001078 }
1079 av1_calculate_tile_cols(cm);
1080
1081 // configure tile rows
1082 if (cm->uniform_tile_spacing_flag) {
1083 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
1084 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +02001085 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001086 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
1087 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001088 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +02001089 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
1090 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001091 size_sb = cpi->oxcf.tile_heights[j++];
1092 if (j >= cpi->oxcf.tile_height_count) j = 0;
1093 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +02001094 }
1095 cm->tile_rows = i;
1096 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001097 }
1098 av1_calculate_tile_rows(cm);
1099}
1100
Yaowu Xuf883b422016-08-30 14:01:10 -07001101static void update_frame_size(AV1_COMP *cpi) {
1102 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001103 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
1104
chiyotsaia7091f12019-08-09 16:48:27 -07001105 // We need to reallocate the context buffers here in case we need more mis.
1106 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
1107 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1108 "Failed to allocate context buffers");
1109 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001110 av1_init_context_buffers(cm);
chiyotsaia7091f12019-08-09 16:48:27 -07001111
Luc Trudeau1e84af52017-11-25 15:00:28 -05001112 av1_init_macroblockd(cm, xd, NULL);
chiyotsai426c0662019-08-05 16:15:11 -07001113
chiyotsaia7091f12019-08-09 16:48:27 -07001114 const int ext_mi_size = cm->mi_alloc_rows * cm->mi_alloc_cols;
1115 alloc_context_buffers_ext(cpi);
Remya0cce44c2019-08-16 11:57:24 +05301116 memset(cpi->mbmi_ext_frame_base, 0,
1117 ext_mi_size * sizeof(*cpi->mbmi_ext_frame_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001118 set_tile_info(cpi);
1119}
1120
Yaowu Xuf883b422016-08-30 14:01:10 -07001121static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001122 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -07001123 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
David Turnera21966b2018-12-05 14:48:49 +00001124 cpi->common.remapped_ref_idx[fb_idx] = fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -07001125 cpi->rate_index = 0;
1126 cpi->rate_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001127}
1128
Debargha Mukherjee57498692018-05-11 13:29:31 -07001129static INLINE int does_level_match(int width, int height, double fps,
1130 int lvl_width, int lvl_height,
1131 double lvl_fps, int lvl_dim_mult) {
1132 const int64_t lvl_luma_pels = lvl_width * lvl_height;
1133 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
1134 const int64_t luma_pels = width * height;
1135 const double display_sample_rate = luma_pels * fps;
1136 return luma_pels <= lvl_luma_pels &&
1137 display_sample_rate <= lvl_display_sample_rate &&
1138 width <= lvl_width * lvl_dim_mult &&
1139 height <= lvl_height * lvl_dim_mult;
1140}
1141
Andrey Norkin26495512018-06-20 17:13:11 -07001142static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Andrey Norkinf481d982018-05-15 12:05:31 -07001143 const AV1EncoderConfig *oxcf) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001144 // TODO(any): This is a placeholder function that only addresses dimensions
1145 // and max display sample rates.
1146 // Need to add checks for max bit rate, max decoded luma sample rate, header
1147 // rate, etc. that are not covered by this function.
Hui Su8427ff22019-03-11 10:14:33 -07001148 AV1_LEVEL level = SEQ_LEVEL_MAX;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001149 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
1150 288, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001151 level = SEQ_LEVEL_2_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001152 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1153 704, 396, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001154 level = SEQ_LEVEL_2_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001155 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1156 1088, 612, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001157 level = SEQ_LEVEL_3_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001158 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1159 1376, 774, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001160 level = SEQ_LEVEL_3_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001161 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1162 2048, 1152, 30.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001163 level = SEQ_LEVEL_4_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001164 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1165 2048, 1152, 60.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001166 level = SEQ_LEVEL_4_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001167 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1168 4096, 2176, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001169 level = SEQ_LEVEL_5_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001170 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1171 4096, 2176, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001172 level = SEQ_LEVEL_5_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001173 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1174 4096, 2176, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001175 level = SEQ_LEVEL_5_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001176 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1177 8192, 4352, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001178 level = SEQ_LEVEL_6_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001179 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1180 8192, 4352, 60.0, 2)) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001181 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1182 8192, 4352, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001183 level = SEQ_LEVEL_6_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001184 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1185 16384, 8704, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001186 level = SEQ_LEVEL_7_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001187 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1188 16384, 8704, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001189 level = SEQ_LEVEL_7_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001190 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1191 16384, 8704, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001192 level = SEQ_LEVEL_7_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001193 }
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001194 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su8427ff22019-03-11 10:14:33 -07001195 seq->seq_level_idx[i] = level;
Andrey Norkin26495512018-06-20 17:13:11 -07001196 // Set the maximum parameters for bitrate and buffer size for this profile,
1197 // level, and tier
Yaowu Xu7e450882019-04-30 15:09:18 -07001198 cm->op_params[i].bitrate = av1_max_level_bitrate(
Hui Su8427ff22019-03-11 10:14:33 -07001199 cm->seq_params.profile, seq->seq_level_idx[i], seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001200 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
1201 // check
Andrey Norkin26495512018-06-20 17:13:11 -07001202 if (cm->op_params[i].bitrate == 0)
1203 aom_internal_error(
1204 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
1205 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -07001206 // Buffer size in bits/s is bitrate in bits/s * 1 s
Andrey Norkin26495512018-06-20 17:13:11 -07001207 cm->op_params[i].buffer_size = cm->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001208 }
1209}
1210
Andrey Norkin26495512018-06-20 17:13:11 -07001211static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
Marco Paniconi67142112019-07-24 15:00:31 -07001212 const AV1EncoderConfig *oxcf, int use_svc) {
Yaowu Xu2a9ac432019-08-06 14:21:17 -07001213 seq->still_picture = (oxcf->force_video_mode == 0) && (oxcf->limit == 1);
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001214 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -07001215 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
kyslov94243382019-05-02 15:33:32 -07001216 seq->force_screen_content_tools = (oxcf->mode == REALTIME) ? 0 : 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001217 seq->force_integer_mv = 2;
David Turnerebf96f42018-11-14 16:57:57 +00001218 seq->order_hint_info.enable_order_hint = oxcf->enable_order_hint;
David Turner936235c2018-11-28 13:42:01 +00001219 seq->frame_id_numbers_present_flag =
1220 !(seq->still_picture && seq->reduced_still_picture_hdr) &&
Marco Paniconi67142112019-07-24 15:00:31 -07001221 !oxcf->large_scale_tile && oxcf->error_resilient_mode && !use_svc;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001222 if (seq->still_picture && seq->reduced_still_picture_hdr) {
David Turnerebf96f42018-11-14 16:57:57 +00001223 seq->order_hint_info.enable_order_hint = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001224 seq->force_screen_content_tools = 2;
1225 seq->force_integer_mv = 2;
1226 }
David Turnerebf96f42018-11-14 16:57:57 +00001227 seq->order_hint_info.order_hint_bits_minus_1 =
1228 seq->order_hint_info.enable_order_hint
1229 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
1230 : -1;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001231
David Turner760a2f42018-12-07 15:25:36 +00001232 seq->max_frame_width =
1233 oxcf->forced_max_frame_width ? oxcf->forced_max_frame_width : oxcf->width;
1234 seq->max_frame_height = oxcf->forced_max_frame_height
1235 ? oxcf->forced_max_frame_height
1236 : oxcf->height;
1237 seq->num_bits_width =
1238 (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
1239 seq->num_bits_height =
1240 (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
1241 assert(seq->num_bits_width <= 16);
1242 assert(seq->num_bits_height <= 16);
1243
1244 seq->frame_id_length = FRAME_ID_LENGTH;
1245 seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
1246
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001247 seq->enable_dual_filter = oxcf->enable_dual_filter;
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -08001248 seq->order_hint_info.enable_dist_wtd_comp = oxcf->enable_dist_wtd_comp;
1249 seq->order_hint_info.enable_dist_wtd_comp &=
David Turnerebf96f42018-11-14 16:57:57 +00001250 seq->order_hint_info.enable_order_hint;
1251 seq->order_hint_info.enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1252 seq->order_hint_info.enable_ref_frame_mvs &=
1253 seq->order_hint_info.enable_order_hint;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001254 seq->enable_superres = oxcf->enable_superres;
1255 seq->enable_cdef = oxcf->enable_cdef;
1256 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -07001257 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -08001258 seq->enable_interintra_compound = oxcf->enable_interintra_comp;
1259 seq->enable_masked_compound = oxcf->enable_masked_comp;
Debargha Mukherjee03c43ba2018-12-14 13:08:08 -08001260 seq->enable_intra_edge_filter = oxcf->enable_intra_edge_filter;
Yue Chen8f9ca582018-12-12 15:11:47 -08001261 seq->enable_filter_intra = oxcf->enable_filter_intra;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001262
Andrey Norkin26495512018-06-20 17:13:11 -07001263 set_bitstream_level_tier(seq, cm, oxcf);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001264
1265 if (seq->operating_points_cnt_minus_1 == 0) {
1266 seq->operating_point_idc[0] = 0;
1267 } else {
Marco Paniconi6da983b2019-09-16 20:12:43 -07001268 // Set operating_point_idc[] such that the i=0 point corresponds to the
1269 // highest quality operating point (all layers), and subsequent
1270 // operarting points (i > 0) are lower quality corresponding to
1271 // skip decoding enhancement layers (temporal first).
1272 int i = 0;
1273 assert(seq->operating_points_cnt_minus_1 ==
1274 (int)(cm->number_spatial_layers * cm->number_temporal_layers - 1));
1275 for (unsigned int sl = 0; sl < cm->number_spatial_layers; sl++) {
1276 for (unsigned int tl = 0; tl < cm->number_temporal_layers; tl++) {
1277 seq->operating_point_idc[i] =
1278 (~(~0u << (cm->number_spatial_layers - sl)) << 8) |
1279 ~(~0u << (cm->number_temporal_layers - tl));
1280 i++;
1281 }
1282 }
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001283 }
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001284}
1285
Yaowu Xuf883b422016-08-30 14:01:10 -07001286static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1287 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001288
1289 cpi->oxcf = *oxcf;
1290 cpi->framerate = oxcf->init_framerate;
1291
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001292 cm->seq_params.profile = oxcf->profile;
1293 cm->seq_params.bit_depth = oxcf->bit_depth;
1294 cm->seq_params.use_highbitdepth = oxcf->use_highbitdepth;
1295 cm->seq_params.color_primaries = oxcf->color_primaries;
1296 cm->seq_params.transfer_characteristics = oxcf->transfer_characteristics;
1297 cm->seq_params.matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001298 cm->seq_params.monochrome = oxcf->monochrome;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001299 cm->seq_params.chroma_sample_position = oxcf->chroma_sample_position;
1300 cm->seq_params.color_range = oxcf->color_range;
Andrey Norkin28e9ce22018-01-08 10:11:21 -08001301 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08001302 cm->timing_info.num_units_in_display_tick =
1303 oxcf->timing_info.num_units_in_display_tick;
1304 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
1305 cm->timing_info.equal_picture_interval =
1306 oxcf->timing_info.equal_picture_interval;
1307 cm->timing_info.num_ticks_per_picture =
1308 oxcf->timing_info.num_ticks_per_picture;
1309
Andrey Norkin26495512018-06-20 17:13:11 -07001310 cm->seq_params.display_model_info_present_flag =
1311 oxcf->display_model_info_present_flag;
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001312 cm->seq_params.decoder_model_info_present_flag =
1313 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08001314 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001315 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08001316 cm->buffer_model.num_units_in_decoding_tick =
1317 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07001318 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07001319 av1_set_aom_dec_model_info(&cm->buffer_model);
1320 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07001321 } else if (cm->timing_info_present &&
1322 cm->timing_info.equal_picture_interval &&
1323 !cm->seq_params.decoder_model_info_present_flag) {
1324 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07001325 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001326 } else {
1327 cm->op_params[0].initial_display_delay =
1328 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08001329 }
Andrey Norkinc7511de2018-06-22 12:31:06 -07001330
Tom Fineganf8d6a162018-08-21 10:47:55 -07001331 if (cm->seq_params.monochrome) {
1332 cm->seq_params.subsampling_x = 1;
1333 cm->seq_params.subsampling_y = 1;
1334 } else if (cm->seq_params.color_primaries == AOM_CICP_CP_BT_709 &&
1335 cm->seq_params.transfer_characteristics == AOM_CICP_TC_SRGB &&
1336 cm->seq_params.matrix_coefficients == AOM_CICP_MC_IDENTITY) {
1337 cm->seq_params.subsampling_x = 0;
1338 cm->seq_params.subsampling_y = 0;
1339 } else {
1340 if (cm->seq_params.profile == 0) {
1341 cm->seq_params.subsampling_x = 1;
1342 cm->seq_params.subsampling_y = 1;
1343 } else if (cm->seq_params.profile == 1) {
1344 cm->seq_params.subsampling_x = 0;
1345 cm->seq_params.subsampling_y = 0;
1346 } else {
1347 if (cm->seq_params.bit_depth == AOM_BITS_12) {
1348 cm->seq_params.subsampling_x = oxcf->chroma_subsampling_x;
1349 cm->seq_params.subsampling_y = oxcf->chroma_subsampling_y;
1350 } else {
1351 cm->seq_params.subsampling_x = 1;
1352 cm->seq_params.subsampling_y = 0;
1353 }
1354 }
Tom Finegan02b2a842018-08-24 13:50:00 -07001355 }
1356
Yaowu Xuc27fc142016-08-22 16:08:15 -07001357 cm->width = oxcf->width;
1358 cm->height = oxcf->height;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001359 set_sb_size(&cm->seq_params,
1360 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -07001361 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -07001362
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08001363 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001364
1365 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -07001366 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001367
Marco Paniconi882c5ad2019-09-30 21:32:29 -07001368 // Set init SVC parameters.
Marco Paniconi67142112019-07-24 15:00:31 -07001369 cpi->use_svc = 0;
Marco Paniconid8574e32019-08-04 21:30:12 -07001370 cpi->svc.external_ref_frame_config = 0;
1371 cpi->svc.non_reference_frame = 0;
Marco Paniconi67142112019-07-24 15:00:31 -07001372 cm->number_spatial_layers = 1;
1373 cm->number_temporal_layers = 1;
Marco Paniconi882c5ad2019-09-30 21:32:29 -07001374 cm->spatial_layer_id = 0;
1375 cm->temporal_layer_id = 0;
Marco Paniconi67142112019-07-24 15:00:31 -07001376
Yaowu Xuc27fc142016-08-22 16:08:15 -07001377 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001378 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001379
1380 cpi->static_mb_pct = 0;
1381 cpi->ref_frame_flags = 0;
1382
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001383 // Reset resize pending flags
1384 cpi->resize_pending_width = 0;
1385 cpi->resize_pending_height = 0;
1386
Yaowu Xuc27fc142016-08-22 16:08:15 -07001387 init_buffer_indices(cpi);
1388}
1389
1390static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001391 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001392 const int64_t bandwidth = oxcf->target_bandwidth;
1393 const int64_t starting = oxcf->starting_buffer_level_ms;
1394 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1395 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1396
1397 rc->starting_buffer_level = starting * bandwidth / 1000;
1398 rc->optimal_buffer_level =
1399 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1400 rc->maximum_buffer_size =
1401 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1402}
1403
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001404#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1405 cpi->fn_ptr[BT].sdf = SDF; \
1406 cpi->fn_ptr[BT].sdaf = SDAF; \
1407 cpi->fn_ptr[BT].vf = VF; \
1408 cpi->fn_ptr[BT].svf = SVF; \
1409 cpi->fn_ptr[BT].svaf = SVAF; \
1410 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1411 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001412 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001413
1414#define MAKE_BFP_SAD_WRAPPER(fnname) \
1415 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1416 int source_stride, \
1417 const uint8_t *ref_ptr, int ref_stride) { \
1418 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1419 } \
1420 static unsigned int fnname##_bits10( \
1421 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1422 int ref_stride) { \
1423 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1424 } \
1425 static unsigned int fnname##_bits12( \
1426 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1427 int ref_stride) { \
1428 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1429 }
1430
1431#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1432 static unsigned int fnname##_bits8( \
1433 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1434 int ref_stride, const uint8_t *second_pred) { \
1435 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1436 } \
1437 static unsigned int fnname##_bits10( \
1438 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1439 int ref_stride, const uint8_t *second_pred) { \
1440 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1441 2; \
1442 } \
1443 static unsigned int fnname##_bits12( \
1444 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1445 int ref_stride, const uint8_t *second_pred) { \
1446 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1447 4; \
1448 }
1449
Yaowu Xuc27fc142016-08-22 16:08:15 -07001450#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1451 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1452 const uint8_t *const ref_ptr[], int ref_stride, \
1453 unsigned int *sad_array) { \
1454 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1455 } \
1456 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1457 const uint8_t *const ref_ptr[], int ref_stride, \
1458 unsigned int *sad_array) { \
1459 int i; \
1460 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1461 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1462 } \
1463 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1464 const uint8_t *const ref_ptr[], int ref_stride, \
1465 unsigned int *sad_array) { \
1466 int i; \
1467 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1468 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1469 }
1470
Cheng Chenbf3d4962017-11-01 14:48:52 -07001471#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1472 static unsigned int fnname##_bits8( \
1473 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1474 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001475 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001476 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1477 jcp_param); \
1478 } \
1479 static unsigned int fnname##_bits10( \
1480 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1481 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001482 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001483 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1484 jcp_param) >> \
1485 2; \
1486 } \
1487 static unsigned int fnname##_bits12( \
1488 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1489 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001490 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001491 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1492 jcp_param) >> \
1493 4; \
1494 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001495
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001496#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001497MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1498MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001499MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1500MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1501MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1502MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1503MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1504MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1505MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001506MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1507MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1508MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1509MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1510MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1511MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1512MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1513MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1514MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1515MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1516MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1517MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1518MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1519MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001520MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1521MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1522MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001523MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1524MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1525MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001526MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1527MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1528MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001529MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1530MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1531MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001532MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1533MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1534MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001535MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1536MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1537MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001538MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1539MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1540MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001541MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1542MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1543MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001544MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001545
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001546MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1547MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1548MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1549MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1550MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1551MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1552MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1553MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1554MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1555MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1556MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1557MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001558MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1559MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1560MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1561MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1562MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1563MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001564
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001565MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x128_avg)
1566MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x64_avg)
1567MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x128_avg)
1568MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x16_avg)
1569MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x32_avg)
1570MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x32_avg)
1571MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x64_avg)
1572MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x32_avg)
1573MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x64_avg)
1574MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x16_avg)
1575MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x8_avg)
1576MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x16_avg)
1577MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x8_avg)
1578MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x4_avg)
1579MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x8_avg)
1580MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x4_avg)
1581MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x16_avg)
1582MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x4_avg)
1583MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x32_avg)
1584MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x8_avg)
1585MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x64_avg)
1586MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x16_avg)
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001587#endif // CONFIG_AV1_HIGHBITDEPTH
Cheng Chenbf3d4962017-11-01 14:48:52 -07001588
David Barker0f3c94e2017-05-16 15:21:50 +01001589#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001590 cpi->fn_ptr[BT].msdf = MCSDF; \
1591 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001592
David Barkerc155e012017-05-11 13:54:54 +01001593#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1594 static unsigned int fnname##_bits8( \
1595 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1596 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1597 int m_stride, int invert_mask) { \
1598 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1599 second_pred_ptr, m, m_stride, invert_mask); \
1600 } \
1601 static unsigned int fnname##_bits10( \
1602 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1603 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1604 int m_stride, int invert_mask) { \
1605 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1606 second_pred_ptr, m, m_stride, invert_mask) >> \
1607 2; \
1608 } \
1609 static unsigned int fnname##_bits12( \
1610 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1611 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1612 int m_stride, int invert_mask) { \
1613 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1614 second_pred_ptr, m, m_stride, invert_mask) >> \
1615 4; \
1616 }
1617
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001618#if CONFIG_AV1_HIGHBITDEPTH
David Barkerf19f35f2017-05-22 16:33:22 +01001619MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1620MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1621MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001622MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1623MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1624MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1625MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1626MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1627MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1628MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1629MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1630MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1631MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1632MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1633MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1634MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001635MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1636MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1637MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1638MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001639MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1640MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001641#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001642
Yaowu Xuc27fc142016-08-22 16:08:15 -07001643#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1644 cpi->fn_ptr[BT].osdf = OSDF; \
1645 cpi->fn_ptr[BT].ovf = OVF; \
1646 cpi->fn_ptr[BT].osvf = OSVF;
1647
1648#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1649 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1650 const int32_t *wsrc, \
1651 const int32_t *msk) { \
1652 return fnname(ref, ref_stride, wsrc, msk); \
1653 } \
1654 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1655 const int32_t *wsrc, \
1656 const int32_t *msk) { \
1657 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1658 } \
1659 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1660 const int32_t *wsrc, \
1661 const int32_t *msk) { \
1662 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1663 }
1664
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001665#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001666MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1667MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1668MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001669MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1670MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1671MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1672MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1673MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1674MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1675MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1676MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1677MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1678MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1679MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1680MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1681MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001682MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1683MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1684MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1685MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001686MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1687MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001688
Yaowu Xuf883b422016-08-30 14:01:10 -07001689static void highbd_set_var_fns(AV1_COMP *const cpi) {
1690 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001691 if (cm->seq_params.use_highbitdepth) {
1692 switch (cm->seq_params.bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001693 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001694 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1695 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1696 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001697 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001698 aom_highbd_sad64x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001699 aom_highbd_dist_wtd_sad64x16_avg_bits8,
1700 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001701
1702 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1703 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1704 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001705 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001706 aom_highbd_sad16x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001707 aom_highbd_dist_wtd_sad16x64_avg_bits8,
1708 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001709
1710 HIGHBD_BFP(
1711 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1712 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001713 aom_highbd_8_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001714 aom_highbd_sad32x8x4d_bits8, aom_highbd_dist_wtd_sad32x8_avg_bits8,
1715 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001716
1717 HIGHBD_BFP(
1718 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1719 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001720 aom_highbd_8_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001721 aom_highbd_sad8x32x4d_bits8, aom_highbd_dist_wtd_sad8x32_avg_bits8,
1722 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001723
1724 HIGHBD_BFP(
1725 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1726 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001727 aom_highbd_8_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001728 aom_highbd_sad16x4x4d_bits8, aom_highbd_dist_wtd_sad16x4_avg_bits8,
1729 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001730
1731 HIGHBD_BFP(
1732 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1733 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001734 aom_highbd_8_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001735 aom_highbd_sad4x16x4d_bits8, aom_highbd_dist_wtd_sad4x16_avg_bits8,
1736 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001737
1738 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1739 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1740 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001741 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001742 aom_highbd_sad32x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001743 aom_highbd_dist_wtd_sad32x16_avg_bits8,
1744 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001745
1746 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1747 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1748 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001749 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001750 aom_highbd_sad16x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001751 aom_highbd_dist_wtd_sad16x32_avg_bits8,
1752 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001753
1754 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1755 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1756 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001757 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001758 aom_highbd_sad64x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001759 aom_highbd_dist_wtd_sad64x32_avg_bits8,
1760 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001761
1762 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1763 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1764 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001765 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001766 aom_highbd_sad32x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001767 aom_highbd_dist_wtd_sad32x64_avg_bits8,
1768 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001769
1770 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1771 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1772 aom_highbd_8_sub_pixel_variance32x32,
1773 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001774 aom_highbd_sad32x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001775 aom_highbd_dist_wtd_sad32x32_avg_bits8,
1776 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001777
1778 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1779 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1780 aom_highbd_8_sub_pixel_variance64x64,
1781 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001782 aom_highbd_sad64x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001783 aom_highbd_dist_wtd_sad64x64_avg_bits8,
1784 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001785
1786 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1787 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1788 aom_highbd_8_sub_pixel_variance16x16,
1789 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001790 aom_highbd_sad16x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001791 aom_highbd_dist_wtd_sad16x16_avg_bits8,
1792 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001793
1794 HIGHBD_BFP(
1795 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1796 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001797 aom_highbd_8_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001798 aom_highbd_sad16x8x4d_bits8, aom_highbd_dist_wtd_sad16x8_avg_bits8,
1799 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001800
1801 HIGHBD_BFP(
1802 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1803 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001804 aom_highbd_8_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001805 aom_highbd_sad8x16x4d_bits8, aom_highbd_dist_wtd_sad8x16_avg_bits8,
1806 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001807
Cheng Chenbf3d4962017-11-01 14:48:52 -07001808 HIGHBD_BFP(
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001809 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1810 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1811 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x4d_bits8,
1812 aom_highbd_dist_wtd_sad8x8_avg_bits8,
1813 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x8)
1814
1815 HIGHBD_BFP(
1816 BLOCK_8X4, aom_highbd_sad8x4_bits8, aom_highbd_sad8x4_avg_bits8,
1817 aom_highbd_8_variance8x4, aom_highbd_8_sub_pixel_variance8x4,
1818 aom_highbd_8_sub_pixel_avg_variance8x4, aom_highbd_sad8x4x4d_bits8,
1819 aom_highbd_dist_wtd_sad8x4_avg_bits8,
1820 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x4)
1821
1822 HIGHBD_BFP(
1823 BLOCK_4X8, aom_highbd_sad4x8_bits8, aom_highbd_sad4x8_avg_bits8,
1824 aom_highbd_8_variance4x8, aom_highbd_8_sub_pixel_variance4x8,
1825 aom_highbd_8_sub_pixel_avg_variance4x8, aom_highbd_sad4x8x4d_bits8,
1826 aom_highbd_dist_wtd_sad4x8_avg_bits8,
1827 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x8)
1828
1829 HIGHBD_BFP(
1830 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1831 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1832 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x4d_bits8,
1833 aom_highbd_dist_wtd_sad4x4_avg_bits8,
1834 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x4)
1835
1836 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1837 aom_highbd_sad128x128_avg_bits8,
1838 aom_highbd_8_variance128x128,
1839 aom_highbd_8_sub_pixel_variance128x128,
1840 aom_highbd_8_sub_pixel_avg_variance128x128,
1841 aom_highbd_sad128x128x4d_bits8,
1842 aom_highbd_dist_wtd_sad128x128_avg_bits8,
1843 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001844
1845 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1846 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1847 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001848 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001849 aom_highbd_sad128x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001850 aom_highbd_dist_wtd_sad128x64_avg_bits8,
1851 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001852
1853 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1854 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1855 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001856 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001857 aom_highbd_sad64x128x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001858 aom_highbd_dist_wtd_sad64x128_avg_bits8,
1859 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001860
David Barkerf19f35f2017-05-22 16:33:22 +01001861 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1862 aom_highbd_8_masked_sub_pixel_variance128x128)
1863 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1864 aom_highbd_8_masked_sub_pixel_variance128x64)
1865 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1866 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001867 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1868 aom_highbd_8_masked_sub_pixel_variance64x64)
1869 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1870 aom_highbd_8_masked_sub_pixel_variance64x32)
1871 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1872 aom_highbd_8_masked_sub_pixel_variance32x64)
1873 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1874 aom_highbd_8_masked_sub_pixel_variance32x32)
1875 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1876 aom_highbd_8_masked_sub_pixel_variance32x16)
1877 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1878 aom_highbd_8_masked_sub_pixel_variance16x32)
1879 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1880 aom_highbd_8_masked_sub_pixel_variance16x16)
1881 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1882 aom_highbd_8_masked_sub_pixel_variance8x16)
1883 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1884 aom_highbd_8_masked_sub_pixel_variance16x8)
1885 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1886 aom_highbd_8_masked_sub_pixel_variance8x8)
1887 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1888 aom_highbd_8_masked_sub_pixel_variance4x8)
1889 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1890 aom_highbd_8_masked_sub_pixel_variance8x4)
1891 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1892 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001893 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1894 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001895 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1896 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001897 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1898 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001899 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1900 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001901 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1902 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001903 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1904 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001905 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1906 aom_highbd_obmc_variance128x128,
1907 aom_highbd_obmc_sub_pixel_variance128x128)
1908 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1909 aom_highbd_obmc_variance128x64,
1910 aom_highbd_obmc_sub_pixel_variance128x64)
1911 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1912 aom_highbd_obmc_variance64x128,
1913 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001914 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1915 aom_highbd_obmc_variance64x64,
1916 aom_highbd_obmc_sub_pixel_variance64x64)
1917 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1918 aom_highbd_obmc_variance64x32,
1919 aom_highbd_obmc_sub_pixel_variance64x32)
1920 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1921 aom_highbd_obmc_variance32x64,
1922 aom_highbd_obmc_sub_pixel_variance32x64)
1923 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1924 aom_highbd_obmc_variance32x32,
1925 aom_highbd_obmc_sub_pixel_variance32x32)
1926 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1927 aom_highbd_obmc_variance32x16,
1928 aom_highbd_obmc_sub_pixel_variance32x16)
1929 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1930 aom_highbd_obmc_variance16x32,
1931 aom_highbd_obmc_sub_pixel_variance16x32)
1932 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1933 aom_highbd_obmc_variance16x16,
1934 aom_highbd_obmc_sub_pixel_variance16x16)
1935 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1936 aom_highbd_obmc_variance8x16,
1937 aom_highbd_obmc_sub_pixel_variance8x16)
1938 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1939 aom_highbd_obmc_variance16x8,
1940 aom_highbd_obmc_sub_pixel_variance16x8)
1941 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1942 aom_highbd_obmc_variance8x8,
1943 aom_highbd_obmc_sub_pixel_variance8x8)
1944 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1945 aom_highbd_obmc_variance4x8,
1946 aom_highbd_obmc_sub_pixel_variance4x8)
1947 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1948 aom_highbd_obmc_variance8x4,
1949 aom_highbd_obmc_sub_pixel_variance8x4)
1950 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1951 aom_highbd_obmc_variance4x4,
1952 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001953 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1954 aom_highbd_obmc_variance64x16,
1955 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001956 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1957 aom_highbd_obmc_variance16x64,
1958 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001959 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1960 aom_highbd_obmc_variance32x8,
1961 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001962 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1963 aom_highbd_obmc_variance8x32,
1964 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001965 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1966 aom_highbd_obmc_variance16x4,
1967 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001968 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1969 aom_highbd_obmc_variance4x16,
1970 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001971 break;
1972
Yaowu Xuf883b422016-08-30 14:01:10 -07001973 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001974 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1975 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1976 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001977 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001978 aom_highbd_sad64x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001979 aom_highbd_dist_wtd_sad64x16_avg_bits10,
1980 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001981
1982 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1983 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1984 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001985 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001986 aom_highbd_sad16x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001987 aom_highbd_dist_wtd_sad16x64_avg_bits10,
1988 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001989
1990 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1991 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1992 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001993 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001994 aom_highbd_sad32x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001995 aom_highbd_dist_wtd_sad32x8_avg_bits10,
1996 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001997
1998 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1999 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
2000 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002001 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002002 aom_highbd_sad8x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002003 aom_highbd_dist_wtd_sad8x32_avg_bits10,
2004 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002005
2006 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
2007 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
2008 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002009 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002010 aom_highbd_sad16x4x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002011 aom_highbd_dist_wtd_sad16x4_avg_bits10,
2012 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002013
2014 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
2015 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
2016 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002017 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002018 aom_highbd_sad4x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002019 aom_highbd_dist_wtd_sad4x16_avg_bits10,
2020 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002021
2022 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
2023 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
2024 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002025 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002026 aom_highbd_sad32x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002027 aom_highbd_dist_wtd_sad32x16_avg_bits10,
2028 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002029
2030 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
2031 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
2032 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002033 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002034 aom_highbd_sad16x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002035 aom_highbd_dist_wtd_sad16x32_avg_bits10,
2036 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002037
2038 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
2039 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
2040 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002041 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002042 aom_highbd_sad64x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002043 aom_highbd_dist_wtd_sad64x32_avg_bits10,
2044 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002045
2046 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
2047 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
2048 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002049 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002050 aom_highbd_sad32x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002051 aom_highbd_dist_wtd_sad32x64_avg_bits10,
2052 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002053
2054 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
2055 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
2056 aom_highbd_10_sub_pixel_variance32x32,
2057 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002058 aom_highbd_sad32x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002059 aom_highbd_dist_wtd_sad32x32_avg_bits10,
2060 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002061
2062 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
2063 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
2064 aom_highbd_10_sub_pixel_variance64x64,
2065 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002066 aom_highbd_sad64x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002067 aom_highbd_dist_wtd_sad64x64_avg_bits10,
2068 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002069
2070 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
2071 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
2072 aom_highbd_10_sub_pixel_variance16x16,
2073 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002074 aom_highbd_sad16x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002075 aom_highbd_dist_wtd_sad16x16_avg_bits10,
2076 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002077
2078 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
2079 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
2080 aom_highbd_10_sub_pixel_variance16x8,
2081 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002082 aom_highbd_sad16x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002083 aom_highbd_dist_wtd_sad16x8_avg_bits10,
2084 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002085
2086 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
2087 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
2088 aom_highbd_10_sub_pixel_variance8x16,
2089 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002090 aom_highbd_sad8x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002091 aom_highbd_dist_wtd_sad8x16_avg_bits10,
2092 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002093
2094 HIGHBD_BFP(
2095 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
2096 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002097 aom_highbd_10_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002098 aom_highbd_sad8x8x4d_bits10, aom_highbd_dist_wtd_sad8x8_avg_bits10,
2099 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002100
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002101 HIGHBD_BFP(
2102 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
2103 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
2104 aom_highbd_10_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002105 aom_highbd_sad8x4x4d_bits10, aom_highbd_dist_wtd_sad8x4_avg_bits10,
2106 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002107
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002108 HIGHBD_BFP(
2109 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
2110 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
2111 aom_highbd_10_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002112 aom_highbd_sad4x8x4d_bits10, aom_highbd_dist_wtd_sad4x8_avg_bits10,
2113 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002114
2115 HIGHBD_BFP(
2116 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
2117 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002118 aom_highbd_10_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002119 aom_highbd_sad4x4x4d_bits10, aom_highbd_dist_wtd_sad4x4_avg_bits10,
2120 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002121
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002122 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
2123 aom_highbd_sad128x128_avg_bits10,
2124 aom_highbd_10_variance128x128,
2125 aom_highbd_10_sub_pixel_variance128x128,
2126 aom_highbd_10_sub_pixel_avg_variance128x128,
2127 aom_highbd_sad128x128x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002128 aom_highbd_dist_wtd_sad128x128_avg_bits10,
2129 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002130
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002131 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
2132 aom_highbd_sad128x64_avg_bits10,
2133 aom_highbd_10_variance128x64,
2134 aom_highbd_10_sub_pixel_variance128x64,
2135 aom_highbd_10_sub_pixel_avg_variance128x64,
2136 aom_highbd_sad128x64x4d_bits10,
2137 aom_highbd_dist_wtd_sad128x64_avg_bits10,
2138 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002139
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002140 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
2141 aom_highbd_sad64x128_avg_bits10,
2142 aom_highbd_10_variance64x128,
2143 aom_highbd_10_sub_pixel_variance64x128,
2144 aom_highbd_10_sub_pixel_avg_variance64x128,
2145 aom_highbd_sad64x128x4d_bits10,
2146 aom_highbd_dist_wtd_sad64x128_avg_bits10,
2147 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002148
David Barkerf19f35f2017-05-22 16:33:22 +01002149 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
2150 aom_highbd_10_masked_sub_pixel_variance128x128)
2151 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
2152 aom_highbd_10_masked_sub_pixel_variance128x64)
2153 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
2154 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002155 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
2156 aom_highbd_10_masked_sub_pixel_variance64x64)
2157 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
2158 aom_highbd_10_masked_sub_pixel_variance64x32)
2159 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
2160 aom_highbd_10_masked_sub_pixel_variance32x64)
2161 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
2162 aom_highbd_10_masked_sub_pixel_variance32x32)
2163 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
2164 aom_highbd_10_masked_sub_pixel_variance32x16)
2165 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
2166 aom_highbd_10_masked_sub_pixel_variance16x32)
2167 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
2168 aom_highbd_10_masked_sub_pixel_variance16x16)
2169 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
2170 aom_highbd_10_masked_sub_pixel_variance8x16)
2171 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
2172 aom_highbd_10_masked_sub_pixel_variance16x8)
2173 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
2174 aom_highbd_10_masked_sub_pixel_variance8x8)
2175 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
2176 aom_highbd_10_masked_sub_pixel_variance4x8)
2177 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
2178 aom_highbd_10_masked_sub_pixel_variance8x4)
2179 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
2180 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002181 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
2182 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002183 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
2184 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002185 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
2186 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002187 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
2188 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002189 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
2190 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002191 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
2192 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002193 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
2194 aom_highbd_10_obmc_variance128x128,
2195 aom_highbd_10_obmc_sub_pixel_variance128x128)
2196 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
2197 aom_highbd_10_obmc_variance128x64,
2198 aom_highbd_10_obmc_sub_pixel_variance128x64)
2199 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
2200 aom_highbd_10_obmc_variance64x128,
2201 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002202 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2203 aom_highbd_10_obmc_variance64x64,
2204 aom_highbd_10_obmc_sub_pixel_variance64x64)
2205 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2206 aom_highbd_10_obmc_variance64x32,
2207 aom_highbd_10_obmc_sub_pixel_variance64x32)
2208 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2209 aom_highbd_10_obmc_variance32x64,
2210 aom_highbd_10_obmc_sub_pixel_variance32x64)
2211 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2212 aom_highbd_10_obmc_variance32x32,
2213 aom_highbd_10_obmc_sub_pixel_variance32x32)
2214 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2215 aom_highbd_10_obmc_variance32x16,
2216 aom_highbd_10_obmc_sub_pixel_variance32x16)
2217 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2218 aom_highbd_10_obmc_variance16x32,
2219 aom_highbd_10_obmc_sub_pixel_variance16x32)
2220 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2221 aom_highbd_10_obmc_variance16x16,
2222 aom_highbd_10_obmc_sub_pixel_variance16x16)
2223 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2224 aom_highbd_10_obmc_variance8x16,
2225 aom_highbd_10_obmc_sub_pixel_variance8x16)
2226 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2227 aom_highbd_10_obmc_variance16x8,
2228 aom_highbd_10_obmc_sub_pixel_variance16x8)
2229 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2230 aom_highbd_10_obmc_variance8x8,
2231 aom_highbd_10_obmc_sub_pixel_variance8x8)
2232 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2233 aom_highbd_10_obmc_variance4x8,
2234 aom_highbd_10_obmc_sub_pixel_variance4x8)
2235 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2236 aom_highbd_10_obmc_variance8x4,
2237 aom_highbd_10_obmc_sub_pixel_variance8x4)
2238 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2239 aom_highbd_10_obmc_variance4x4,
2240 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002241
Rupert Swarbrick72678572017-08-02 12:05:26 +01002242 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2243 aom_highbd_10_obmc_variance64x16,
2244 aom_highbd_10_obmc_sub_pixel_variance64x16)
2245
2246 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2247 aom_highbd_10_obmc_variance16x64,
2248 aom_highbd_10_obmc_sub_pixel_variance16x64)
2249
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002250 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2251 aom_highbd_10_obmc_variance32x8,
2252 aom_highbd_10_obmc_sub_pixel_variance32x8)
2253
2254 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2255 aom_highbd_10_obmc_variance8x32,
2256 aom_highbd_10_obmc_sub_pixel_variance8x32)
2257
2258 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2259 aom_highbd_10_obmc_variance16x4,
2260 aom_highbd_10_obmc_sub_pixel_variance16x4)
2261
2262 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2263 aom_highbd_10_obmc_variance4x16,
2264 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002265 break;
2266
Yaowu Xuf883b422016-08-30 14:01:10 -07002267 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002268 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2269 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2270 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002271 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002272 aom_highbd_sad64x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002273 aom_highbd_dist_wtd_sad64x16_avg_bits12,
2274 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002275
2276 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2277 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2278 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002279 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002280 aom_highbd_sad16x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002281 aom_highbd_dist_wtd_sad16x64_avg_bits12,
2282 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002283
2284 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2285 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2286 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002287 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002288 aom_highbd_sad32x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002289 aom_highbd_dist_wtd_sad32x8_avg_bits12,
2290 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002291
2292 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2293 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2294 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002295 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002296 aom_highbd_sad8x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002297 aom_highbd_dist_wtd_sad8x32_avg_bits12,
2298 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002299
2300 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2301 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2302 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002303 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002304 aom_highbd_sad16x4x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002305 aom_highbd_dist_wtd_sad16x4_avg_bits12,
2306 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002307
2308 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2309 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2310 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002311 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002312 aom_highbd_sad4x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002313 aom_highbd_dist_wtd_sad4x16_avg_bits12,
2314 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002315
2316 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2317 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2318 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002319 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002320 aom_highbd_sad32x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002321 aom_highbd_dist_wtd_sad32x16_avg_bits12,
2322 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002323
2324 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2325 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2326 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002327 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002328 aom_highbd_sad16x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002329 aom_highbd_dist_wtd_sad16x32_avg_bits12,
2330 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002331
2332 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2333 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2334 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002335 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002336 aom_highbd_sad64x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002337 aom_highbd_dist_wtd_sad64x32_avg_bits12,
2338 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002339
2340 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2341 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2342 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002343 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002344 aom_highbd_sad32x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002345 aom_highbd_dist_wtd_sad32x64_avg_bits12,
2346 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002347
2348 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2349 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2350 aom_highbd_12_sub_pixel_variance32x32,
2351 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002352 aom_highbd_sad32x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002353 aom_highbd_dist_wtd_sad32x32_avg_bits12,
2354 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002355
2356 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2357 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2358 aom_highbd_12_sub_pixel_variance64x64,
2359 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002360 aom_highbd_sad64x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002361 aom_highbd_dist_wtd_sad64x64_avg_bits12,
2362 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002363
2364 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2365 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2366 aom_highbd_12_sub_pixel_variance16x16,
2367 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002368 aom_highbd_sad16x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002369 aom_highbd_dist_wtd_sad16x16_avg_bits12,
2370 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002371
2372 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2373 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2374 aom_highbd_12_sub_pixel_variance16x8,
2375 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002376 aom_highbd_sad16x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002377 aom_highbd_dist_wtd_sad16x8_avg_bits12,
2378 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002379
2380 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2381 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2382 aom_highbd_12_sub_pixel_variance8x16,
2383 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002384 aom_highbd_sad8x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002385 aom_highbd_dist_wtd_sad8x16_avg_bits12,
2386 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002387
2388 HIGHBD_BFP(
2389 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2390 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002391 aom_highbd_12_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002392 aom_highbd_sad8x8x4d_bits12, aom_highbd_dist_wtd_sad8x8_avg_bits12,
2393 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002394
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002395 HIGHBD_BFP(
2396 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2397 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2398 aom_highbd_12_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002399 aom_highbd_sad8x4x4d_bits12, aom_highbd_dist_wtd_sad8x4_avg_bits12,
2400 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002401
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002402 HIGHBD_BFP(
2403 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2404 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2405 aom_highbd_12_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002406 aom_highbd_sad4x8x4d_bits12, aom_highbd_dist_wtd_sad4x8_avg_bits12,
2407 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002408
2409 HIGHBD_BFP(
2410 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2411 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002412 aom_highbd_12_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002413 aom_highbd_sad4x4x4d_bits12, aom_highbd_dist_wtd_sad4x4_avg_bits12,
2414 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002415
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002416 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2417 aom_highbd_sad128x128_avg_bits12,
2418 aom_highbd_12_variance128x128,
2419 aom_highbd_12_sub_pixel_variance128x128,
2420 aom_highbd_12_sub_pixel_avg_variance128x128,
2421 aom_highbd_sad128x128x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002422 aom_highbd_dist_wtd_sad128x128_avg_bits12,
2423 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002424
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002425 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2426 aom_highbd_sad128x64_avg_bits12,
2427 aom_highbd_12_variance128x64,
2428 aom_highbd_12_sub_pixel_variance128x64,
2429 aom_highbd_12_sub_pixel_avg_variance128x64,
2430 aom_highbd_sad128x64x4d_bits12,
2431 aom_highbd_dist_wtd_sad128x64_avg_bits12,
2432 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002433
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002434 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2435 aom_highbd_sad64x128_avg_bits12,
2436 aom_highbd_12_variance64x128,
2437 aom_highbd_12_sub_pixel_variance64x128,
2438 aom_highbd_12_sub_pixel_avg_variance64x128,
2439 aom_highbd_sad64x128x4d_bits12,
2440 aom_highbd_dist_wtd_sad64x128_avg_bits12,
2441 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002442
David Barkerf19f35f2017-05-22 16:33:22 +01002443 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2444 aom_highbd_12_masked_sub_pixel_variance128x128)
2445 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2446 aom_highbd_12_masked_sub_pixel_variance128x64)
2447 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2448 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002449 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2450 aom_highbd_12_masked_sub_pixel_variance64x64)
2451 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2452 aom_highbd_12_masked_sub_pixel_variance64x32)
2453 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2454 aom_highbd_12_masked_sub_pixel_variance32x64)
2455 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2456 aom_highbd_12_masked_sub_pixel_variance32x32)
2457 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2458 aom_highbd_12_masked_sub_pixel_variance32x16)
2459 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2460 aom_highbd_12_masked_sub_pixel_variance16x32)
2461 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2462 aom_highbd_12_masked_sub_pixel_variance16x16)
2463 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2464 aom_highbd_12_masked_sub_pixel_variance8x16)
2465 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2466 aom_highbd_12_masked_sub_pixel_variance16x8)
2467 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2468 aom_highbd_12_masked_sub_pixel_variance8x8)
2469 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2470 aom_highbd_12_masked_sub_pixel_variance4x8)
2471 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2472 aom_highbd_12_masked_sub_pixel_variance8x4)
2473 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2474 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002475 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2476 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002477 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2478 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002479 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2480 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002481 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2482 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002483 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2484 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002485 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2486 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002487 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2488 aom_highbd_12_obmc_variance128x128,
2489 aom_highbd_12_obmc_sub_pixel_variance128x128)
2490 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2491 aom_highbd_12_obmc_variance128x64,
2492 aom_highbd_12_obmc_sub_pixel_variance128x64)
2493 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2494 aom_highbd_12_obmc_variance64x128,
2495 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002496 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2497 aom_highbd_12_obmc_variance64x64,
2498 aom_highbd_12_obmc_sub_pixel_variance64x64)
2499 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2500 aom_highbd_12_obmc_variance64x32,
2501 aom_highbd_12_obmc_sub_pixel_variance64x32)
2502 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2503 aom_highbd_12_obmc_variance32x64,
2504 aom_highbd_12_obmc_sub_pixel_variance32x64)
2505 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2506 aom_highbd_12_obmc_variance32x32,
2507 aom_highbd_12_obmc_sub_pixel_variance32x32)
2508 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2509 aom_highbd_12_obmc_variance32x16,
2510 aom_highbd_12_obmc_sub_pixel_variance32x16)
2511 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2512 aom_highbd_12_obmc_variance16x32,
2513 aom_highbd_12_obmc_sub_pixel_variance16x32)
2514 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2515 aom_highbd_12_obmc_variance16x16,
2516 aom_highbd_12_obmc_sub_pixel_variance16x16)
2517 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2518 aom_highbd_12_obmc_variance8x16,
2519 aom_highbd_12_obmc_sub_pixel_variance8x16)
2520 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2521 aom_highbd_12_obmc_variance16x8,
2522 aom_highbd_12_obmc_sub_pixel_variance16x8)
2523 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2524 aom_highbd_12_obmc_variance8x8,
2525 aom_highbd_12_obmc_sub_pixel_variance8x8)
2526 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2527 aom_highbd_12_obmc_variance4x8,
2528 aom_highbd_12_obmc_sub_pixel_variance4x8)
2529 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2530 aom_highbd_12_obmc_variance8x4,
2531 aom_highbd_12_obmc_sub_pixel_variance8x4)
2532 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2533 aom_highbd_12_obmc_variance4x4,
2534 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002535 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2536 aom_highbd_12_obmc_variance64x16,
2537 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002538 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2539 aom_highbd_12_obmc_variance16x64,
2540 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002541 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2542 aom_highbd_12_obmc_variance32x8,
2543 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002544 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2545 aom_highbd_12_obmc_variance8x32,
2546 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002547 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2548 aom_highbd_12_obmc_variance16x4,
2549 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002550 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2551 aom_highbd_12_obmc_variance4x16,
2552 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002553 break;
2554
2555 default:
2556 assert(0 &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002557 "cm->seq_params.bit_depth should be AOM_BITS_8, "
Yaowu Xuf883b422016-08-30 14:01:10 -07002558 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002559 }
2560 }
2561}
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002562#endif // CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002563
Yaowu Xuf883b422016-08-30 14:01:10 -07002564static void realloc_segmentation_maps(AV1_COMP *cpi) {
2565 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002566
2567 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002568 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002569 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002570 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002571
2572 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002573 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002574 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002575 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002576
2577 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002578 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002579 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002580 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002581}
2582
Yue Chenb4c93f02019-08-05 13:47:31 -07002583static void set_tpl_stats_block_size(AV1_COMP *cpi) {
2584 AV1_COMMON *const cm = &cpi->common;
2585 const int is_720p_or_larger = AOMMIN(cm->width, cm->height) >= 720;
2586
2587 // 0: 4x4, 1: 8x8, 2: 16x16
2588 cpi->tpl_stats_block_mis_log2 = is_720p_or_larger ? 2 : 1;
2589}
2590
Hui Su38711e72019-06-11 10:49:47 -07002591void av1_alloc_compound_type_rd_buffers(AV1_COMMON *const cm,
2592 CompoundTypeRdBuffers *const bufs) {
2593 CHECK_MEM_ERROR(
2594 cm, bufs->pred0,
2595 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred0)));
2596 CHECK_MEM_ERROR(
2597 cm, bufs->pred1,
2598 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred1)));
2599 CHECK_MEM_ERROR(
2600 cm, bufs->residual1,
2601 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->residual1)));
2602 CHECK_MEM_ERROR(
2603 cm, bufs->diff10,
2604 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->diff10)));
2605 CHECK_MEM_ERROR(cm, bufs->tmp_best_mask_buf,
2606 (uint8_t *)aom_malloc(2 * MAX_SB_SQUARE *
2607 sizeof(*bufs->tmp_best_mask_buf)));
2608}
2609
2610void av1_release_compound_type_rd_buffers(CompoundTypeRdBuffers *const bufs) {
2611 aom_free(bufs->pred0);
2612 aom_free(bufs->pred1);
2613 aom_free(bufs->residual1);
2614 aom_free(bufs->diff10);
2615 aom_free(bufs->tmp_best_mask_buf);
2616 av1_zero(*bufs); // Set all pointers to NULL for safety.
2617}
2618
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002619static void config_target_level(AV1_COMP *const cpi, AV1_LEVEL target_level,
2620 int tier) {
2621 aom_clear_system_state();
2622
2623 AV1EncoderConfig *const oxcf = &cpi->oxcf;
2624 SequenceHeader *const seq_params = &cpi->common.seq_params;
2625
2626 // Adjust target bitrate to be no larger than 70% of level limit.
2627 const BITSTREAM_PROFILE profile = seq_params->profile;
2628 const double level_bitrate_limit =
2629 av1_get_max_bitrate_for_level(target_level, tier, profile);
2630 const int64_t max_bitrate = (int64_t)(level_bitrate_limit * 0.70);
2631 oxcf->target_bandwidth = AOMMIN(oxcf->target_bandwidth, max_bitrate);
2632 // Also need to update cpi->twopass.bits_left.
2633 TWO_PASS *const twopass = &cpi->twopass;
2634 FIRSTPASS_STATS *stats = &twopass->total_stats;
2635 cpi->twopass.bits_left =
2636 (int64_t)(stats->duration * cpi->oxcf.target_bandwidth / 10000000.0);
2637
2638 // Adjust max over-shoot percentage.
2639 oxcf->over_shoot_pct = 0;
2640
2641 // Adjust max quantizer.
2642 oxcf->worst_allowed_q = 255;
2643
2644 // Adjust number of tiles and tile columns to be under level limit.
2645 int max_tiles, max_tile_cols;
2646 av1_get_max_tiles_for_level(target_level, &max_tiles, &max_tile_cols);
2647 while (oxcf->tile_columns > 0 && (1 << oxcf->tile_columns) > max_tile_cols) {
2648 --oxcf->tile_columns;
2649 }
2650 const int tile_cols = (1 << oxcf->tile_columns);
2651 while (oxcf->tile_rows > 0 &&
2652 tile_cols * (1 << oxcf->tile_rows) > max_tiles) {
2653 --oxcf->tile_rows;
2654 }
2655
2656 // Adjust min compression ratio.
2657 const int still_picture = seq_params->still_picture;
2658 const double min_cr =
2659 av1_get_min_cr_for_level(target_level, tier, still_picture);
2660 oxcf->min_cr = AOMMAX(oxcf->min_cr, (unsigned int)(min_cr * 100));
2661}
2662
Yaowu Xuf883b422016-08-30 14:01:10 -07002663void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2664 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002665 SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002666 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002667 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002668 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002669
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002670 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2671 seq_params->bit_depth = oxcf->bit_depth;
2672 seq_params->color_primaries = oxcf->color_primaries;
2673 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2674 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2675 seq_params->monochrome = oxcf->monochrome;
2676 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2677 seq_params->color_range = oxcf->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002678
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002679 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2680 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002681
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002682 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08002683 cm->timing_info.num_units_in_display_tick =
2684 oxcf->timing_info.num_units_in_display_tick;
2685 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
2686 cm->timing_info.equal_picture_interval =
2687 oxcf->timing_info.equal_picture_interval;
2688 cm->timing_info.num_ticks_per_picture =
2689 oxcf->timing_info.num_ticks_per_picture;
2690
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002691 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07002692 oxcf->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002693 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002694 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08002695 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002696 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08002697 cm->buffer_model.num_units_in_decoding_tick =
2698 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07002699 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07002700 av1_set_aom_dec_model_info(&cm->buffer_model);
2701 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07002702 } else if (cm->timing_info_present &&
2703 cm->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002704 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002705 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07002706 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07002707 } else {
2708 cm->op_params[0].initial_display_delay =
2709 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08002710 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002711
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002712 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002713
Yaowu Xuc27fc142016-08-22 16:08:15 -07002714 cpi->oxcf = *oxcf;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002715 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002716 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002717
Wan-Teh Changbcacb322019-09-11 14:50:20 -07002718 memcpy(cpi->target_seq_level_idx, cpi->oxcf.target_seq_level_idx,
2719 sizeof(cpi->target_seq_level_idx));
2720 cpi->keep_level_stats = 0;
2721 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
2722 if (cpi->target_seq_level_idx[i] <= SEQ_LEVELS) {
2723 cpi->keep_level_stats |= 1u << i;
2724 if (!cpi->level_info[i]) {
2725 CHECK_MEM_ERROR(cm, cpi->level_info[i],
2726 aom_calloc(1, sizeof(*cpi->level_info[i])));
2727 }
2728 }
2729 }
2730
2731 // TODO(huisu@): level targeting currently only works for the 0th operating
2732 // point, so scalable coding is not supported yet.
2733 if (cpi->target_seq_level_idx[0] < SEQ_LEVELS) {
2734 // Adjust encoder config in order to meet target level.
2735 config_target_level(cpi, cpi->target_seq_level_idx[0], seq_params->tier[0]);
2736 }
2737
Yaowu Xuf883b422016-08-30 14:01:10 -07002738 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002739 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2740 } else {
2741 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2742 }
2743
2744 cpi->refresh_last_frame = 1;
2745 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002746 cpi->refresh_bwd_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002747
Debargha Mukherjee229fdc82018-03-10 07:45:33 -08002748 cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2749 ? REFRESH_FRAME_CONTEXT_DISABLED
2750 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002751 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08002752 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002753
Alex Converse74ad0912017-07-18 10:22:58 -07002754 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002755 CHECK_MEM_ERROR(cm, x->palette_buffer,
2756 aom_memalign(16, sizeof(*x->palette_buffer)));
2757 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002758
Hui Su38711e72019-06-11 10:49:47 -07002759 if (x->comp_rd_buffer.pred0 == NULL) {
2760 av1_alloc_compound_type_rd_buffers(cm, &x->comp_rd_buffer);
2761 }
2762
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002763 if (x->tmp_conv_dst == NULL) {
2764 CHECK_MEM_ERROR(
2765 cm, x->tmp_conv_dst,
2766 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002767 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002768 }
2769 for (int i = 0; i < 2; ++i) {
2770 if (x->tmp_obmc_bufs[i] == NULL) {
2771 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
wenyao.liu22d8ab32018-10-16 09:11:29 +08002772 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002773 sizeof(*x->tmp_obmc_bufs[i])));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002774 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002775 }
2776 }
2777
Yaowu Xuf883b422016-08-30 14:01:10 -07002778 av1_reset_segment_features(cm);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002779 set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002780
Yaowu Xuc27fc142016-08-22 16:08:15 -07002781 set_rc_buffer_sizes(rc, &cpi->oxcf);
2782
2783 // Under a configuration change, where maximum_buffer_size may change,
2784 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002785 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2786 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002787
2788 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002789 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002790
2791 // Set absolute upper and lower quality limits
2792 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2793 rc->best_quality = cpi->oxcf.best_allowed_q;
2794
Urvang Joshib55cb5e2018-09-12 14:50:21 -07002795 cm->interp_filter = oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
Yue Chen5380cb52018-02-23 15:33:21 -08002796 cm->switchable_motion_mode = 1;
2797
Yaowu Xuc27fc142016-08-22 16:08:15 -07002798 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2799 cm->render_width = cpi->oxcf.render_width;
2800 cm->render_height = cpi->oxcf.render_height;
2801 } else {
2802 cm->render_width = cpi->oxcf.width;
2803 cm->render_height = cpi->oxcf.height;
2804 }
2805 cm->width = cpi->oxcf.width;
2806 cm->height = cpi->oxcf.height;
2807
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002808 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -08002809 // Superblock size should not be updated after the first key frame.
2810 if (!cpi->seq_params_locked) {
2811 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Hui Sud909c2c2019-03-08 11:51:14 -08002812 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
2813 seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
Urvang Joshie4530f82018-01-09 11:43:37 -08002814 }
Dominic Symes917d6c02017-10-11 18:00:52 +02002815
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002816 if (cpi->initial_width || sb_size != seq_params->sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002817 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002818 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002819 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002820 av1_free_pc_tree(&cpi->td, num_planes);
Cheng Chen46f30c72017-09-07 11:13:33 -07002821 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002822 realloc_segmentation_maps(cpi);
2823 cpi->initial_width = cpi->initial_height = 0;
2824 }
2825 }
2826 update_frame_size(cpi);
2827
2828 cpi->alt_ref_source = NULL;
2829 rc->is_src_frame_alt_ref = 0;
2830
Yaowu Xuc27fc142016-08-22 16:08:15 -07002831 set_tile_info(cpi);
2832
Marco Paniconid8574e32019-08-04 21:30:12 -07002833 if (!cpi->svc.external_ref_frame_config)
2834 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002835 cpi->ext_refresh_frame_context_pending = 0;
2836
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002837#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002838 highbd_set_var_fns(cpi);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002839#endif
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002840
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002841 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002842 // This should not be called after the first key frame.
2843 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002844 seq_params->operating_points_cnt_minus_1 =
Marco Paniconi6da983b2019-09-16 20:12:43 -07002845 (cm->number_spatial_layers > 1 || cm->number_temporal_layers > 1)
2846 ? cm->number_spatial_layers * cm->number_temporal_layers - 1
2847 : 0;
Marco Paniconi67142112019-07-24 15:00:31 -07002848 init_seq_coding_tools(&cm->seq_params, cm, oxcf, cpi->use_svc);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002849 }
Marco Paniconi5b1e4732019-08-08 18:57:53 -07002850
2851 if (cpi->use_svc)
2852 av1_update_layer_context_change_config(cpi, oxcf->target_bandwidth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002853}
2854
Yaowu Xuf883b422016-08-30 14:01:10 -07002855AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2856 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002857 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002858 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2859 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002860
2861 if (!cm) return NULL;
2862
Yaowu Xuf883b422016-08-30 14:01:10 -07002863 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002864
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07002865 // The jmp_buf is valid only for the duration of the function that calls
2866 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2867 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002868 if (setjmp(cm->error.jmp)) {
2869 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002870 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002871 return 0;
2872 }
2873
2874 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002875 cm->alloc_mi = enc_alloc_mi;
2876 cm->free_mi = enc_free_mi;
2877 cm->setup_mi = enc_setup_mi;
chiyotsaia7091f12019-08-09 16:48:27 -07002878 cm->set_mb_mi = enc_set_mb_mi;
2879
2880 cm->mi_alloc_bsize = BLOCK_4X4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002881
Angie Chianga5d96c42016-10-21 16:16:56 -07002882 CHECK_MEM_ERROR(cm, cm->fc,
2883 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +00002884 CHECK_MEM_ERROR(
2885 cm, cm->default_frame_context,
2886 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -07002887 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +00002888 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002889
2890 cpi->resize_state = 0;
2891 cpi->resize_avg_qp = 0;
2892 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002893
Yaowu Xuc27fc142016-08-22 16:08:15 -07002894 cpi->common.buffer_pool = pool;
2895
2896 init_config(cpi, oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002897 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002898
Jingning Han17af7742019-09-17 16:58:03 -07002899 init_frame_info(&cpi->frame_info, cm);
2900
David Turnerd2a592e2018-11-16 14:59:31 +00002901 cm->current_frame.frame_number = 0;
David Turnera4c96252019-01-11 16:36:39 +00002902 cm->current_frame_id = -1;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002903 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002904 cpi->partition_search_skippable_frame = 0;
2905 cpi->tile_data = NULL;
David Turnere7ebf902018-12-04 14:04:55 +00002906 cpi->last_show_frame_buf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002907 realloc_segmentation_maps(cpi);
2908
Yaowu Xuc27fc142016-08-22 16:08:15 -07002909 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2910 i++) {
2911 CHECK_MEM_ERROR(
2912 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002913 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002914 }
2915
Yaowu Xuc27fc142016-08-22 16:08:15 -07002916 cpi->refresh_alt_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002917
2918 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2919#if CONFIG_INTERNAL_STATS
2920 cpi->b_calculate_blockiness = 1;
2921 cpi->b_calculate_consistency = 1;
2922 cpi->total_inconsistency = 0;
2923 cpi->psnr.worst = 100.0;
2924 cpi->worst_ssim = 100.0;
2925
2926 cpi->count = 0;
2927 cpi->bytes = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08002928#if CONFIG_SPEED_STATS
2929 cpi->tx_search_count = 0;
2930#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002931
2932 if (cpi->b_calculate_psnr) {
2933 cpi->total_sq_error = 0;
2934 cpi->total_samples = 0;
2935 cpi->tot_recode_hits = 0;
2936 cpi->summed_quality = 0;
2937 cpi->summed_weights = 0;
2938 }
2939
2940 cpi->fastssim.worst = 100.0;
2941 cpi->psnrhvs.worst = 100.0;
2942
2943 if (cpi->b_calculate_blockiness) {
2944 cpi->total_blockiness = 0;
2945 cpi->worst_blockiness = 0.0;
2946 }
2947
2948 if (cpi->b_calculate_consistency) {
2949 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002950 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002951 cpi->common.mi_rows * cpi->common.mi_cols));
2952 cpi->worst_consistency = 100.0;
2953 }
2954#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002955#if CONFIG_ENTROPY_STATS
2956 av1_zero(aggregate_fc);
2957#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002958
2959 cpi->first_time_stamp_ever = INT64_MAX;
2960
Yaowu Xuc27fc142016-08-22 16:08:15 -07002961#ifdef OUTPUT_YUV_SKINMAP
2962 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2963#endif
2964#ifdef OUTPUT_YUV_REC
2965 yuv_rec_file = fopen("rec.yuv", "wb");
2966#endif
2967
Jerome Jiang2612b4d2019-05-29 17:46:47 -07002968#if !CONFIG_REALTIME_ONLY
Yaowu Xuc27fc142016-08-22 16:08:15 -07002969 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002970 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002971 } else if (oxcf->pass == 2) {
2972 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2973 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2974
Yaowu Xuc27fc142016-08-22 16:08:15 -07002975 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2976 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2977 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2978
Yaowu Xuf883b422016-08-30 14:01:10 -07002979 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002980 }
Jerome Jiang2612b4d2019-05-29 17:46:47 -07002981#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002982
Remya0cce44c2019-08-16 11:57:24 +05302983 int sb_mi_size = av1_get_sb_mi_size(cm);
2984
Jingning Hand064cf02017-06-01 10:00:39 -07002985 CHECK_MEM_ERROR(
2986 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002987 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002988 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002989 CHECK_MEM_ERROR(
2990 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002991 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002992 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002993
2994 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2995 (int32_t *)aom_memalign(
2996 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2997
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302998 CHECK_MEM_ERROR(
2999 cm, cpi->td.mb.inter_modes_info,
3000 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303001
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303002 for (int x = 0; x < 2; x++)
3003 for (int y = 0; y < 2; y++)
3004 CHECK_MEM_ERROR(
3005 cm, cpi->td.mb.hash_value_buffer[x][y],
3006 (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
3007 sizeof(*cpi->td.mb.hash_value_buffer[0][0])));
3008
3009 cpi->td.mb.g_crc_initialized = 0;
3010
Jingning Hand064cf02017-06-01 10:00:39 -07003011 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
3012 (int32_t *)aom_memalign(
3013 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
3014
Remya0cce44c2019-08-16 11:57:24 +05303015 CHECK_MEM_ERROR(cm, cpi->td.mb.mbmi_ext,
3016 aom_calloc(sb_mi_size, sizeof(*cpi->td.mb.mbmi_ext)));
3017
David Turner04b70d82019-01-24 15:39:19 +00003018 av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
3019 av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003020
sdengc23c7f12019-06-11 16:56:50 -07003021 {
3022 const int bsize = BLOCK_16X16;
3023 const int w = mi_size_wide[bsize];
3024 const int h = mi_size_high[bsize];
3025 const int num_cols = (cm->mi_cols + w - 1) / w;
3026 const int num_rows = (cm->mi_rows + h - 1) / h;
sdengf46a1062019-08-04 18:43:50 -07003027 CHECK_MEM_ERROR(cm, cpi->tpl_rdmult_scaling_factors,
3028 aom_calloc(num_rows * num_cols,
3029 sizeof(*cpi->tpl_rdmult_scaling_factors)));
3030 CHECK_MEM_ERROR(cm, cpi->tpl_sb_rdmult_scaling_factors,
3031 aom_calloc(num_rows * num_cols,
3032 sizeof(*cpi->tpl_sb_rdmult_scaling_factors)));
3033 }
3034
3035 {
3036 const int bsize = BLOCK_16X16;
3037 const int w = mi_size_wide[bsize];
3038 const int h = mi_size_high[bsize];
3039 const int num_cols = (cm->mi_cols + w - 1) / w;
3040 const int num_rows = (cm->mi_rows + h - 1) / h;
sdengc23c7f12019-06-11 16:56:50 -07003041 CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors,
3042 aom_calloc(num_rows * num_cols,
3043 sizeof(*cpi->ssim_rdmult_scaling_factors)));
3044 }
3045
Yue Chenb4c93f02019-08-05 13:47:31 -07003046 set_tpl_stats_block_size(cpi);
Yue Chenc9b23e02019-04-10 16:54:03 -07003047 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
Yue Chenb4c93f02019-08-05 13:47:31 -07003048 const int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
3049 const int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
3050
3051 cpi->tpl_stats_buffer[frame].is_valid = 0;
3052 cpi->tpl_stats_buffer[frame].width =
3053 mi_cols >> cpi->tpl_stats_block_mis_log2;
3054 cpi->tpl_stats_buffer[frame].height =
3055 mi_rows >> cpi->tpl_stats_block_mis_log2;
3056 cpi->tpl_stats_buffer[frame].stride = cpi->tpl_stats_buffer[frame].width;
3057 cpi->tpl_stats_buffer[frame].mi_rows = cm->mi_rows;
3058 cpi->tpl_stats_buffer[frame].mi_cols = cm->mi_cols;
Yue Chen7cae98f2018-08-24 10:43:16 -07003059
Jingning Han81d6fbb2019-07-15 10:14:13 -07003060 CHECK_MEM_ERROR(
3061 cm, cpi->tpl_stats_buffer[frame].tpl_stats_ptr,
Yue Chenb4c93f02019-08-05 13:47:31 -07003062 aom_calloc(cpi->tpl_stats_buffer[frame].width *
3063 cpi->tpl_stats_buffer[frame].height,
Jingning Han81d6fbb2019-07-15 10:14:13 -07003064 sizeof(*cpi->tpl_stats_buffer[frame].tpl_stats_ptr)));
Jingning Han5e98d3b2019-09-23 21:59:36 -07003065
3066 if (aom_alloc_frame_buffer(
3067 &cpi->tpl_stats_buffer[frame].rec_picture_buf, cm->width,
3068 cm->height, cm->seq_params.subsampling_x,
3069 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
3070 cpi->oxcf.border_in_pixels, cm->byte_alignment))
3071 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3072 "Failed to allocate frame buffer");
Yue Chen7cae98f2018-08-24 10:43:16 -07003073 }
Jingning Han31a0ee92019-07-15 13:56:55 -07003074 cpi->tpl_frame = &cpi->tpl_stats_buffer[REF_FRAMES + 1];
Yue Chen7cae98f2018-08-24 10:43:16 -07003075
chiyotsai9c484b32019-03-07 16:01:50 -08003076#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08003077 av1_zero(cpi->partition_stats);
3078#endif
3079
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003080#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
3081 cpi->fn_ptr[BT].sdf = SDF; \
3082 cpi->fn_ptr[BT].sdaf = SDAF; \
3083 cpi->fn_ptr[BT].vf = VF; \
3084 cpi->fn_ptr[BT].svf = SVF; \
3085 cpi->fn_ptr[BT].svaf = SVAF; \
3086 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
3087 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07003088 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003089
Cheng Chenf78632e2017-10-20 15:30:51 -07003090 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003091 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003092 aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
3093 aom_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003094
3095 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003096 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003097 aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
3098 aom_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003099
3100 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003101 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003102 aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
3103 aom_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003104
3105 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003106 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003107 aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
3108 aom_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003109
3110 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003111 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003112 aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
3113 aom_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003114
3115 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003116 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003117 aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
3118 aom_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003119
Cheng Chenf78632e2017-10-20 15:30:51 -07003120 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
3121 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003122 aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
3123 aom_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07003124
3125 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003126 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003127 aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
3128 aom_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003129
3130 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003131 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003132 aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
3133 aom_dist_wtd_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07003134
3135 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003136 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003137 aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
3138 aom_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003139
3140 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003141 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003142 aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
3143 aom_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003144
3145 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003146 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003147 aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
3148 aom_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003149
3150 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003151 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003152 aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
3153 aom_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003154
3155 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
3156 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003157 aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
3158 aom_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07003159
3160 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
3161 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003162 aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
3163 aom_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07003164
3165 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
3166 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003167 aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
3168 aom_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003169
3170 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003171 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003172 aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
3173 aom_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003174
3175 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003176 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003177 aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
3178 aom_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07003179
3180 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003181 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003182 aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003183
3184 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003185 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003186 aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003187
3188 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003189 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003190 aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003191
3192 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003193 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003194 aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003195
Yaowu Xuc27fc142016-08-22 16:08:15 -07003196#define OBFP(BT, OSDF, OVF, OSVF) \
3197 cpi->fn_ptr[BT].osdf = OSDF; \
3198 cpi->fn_ptr[BT].ovf = OVF; \
3199 cpi->fn_ptr[BT].osvf = OSVF;
3200
Yaowu Xuf883b422016-08-30 14:01:10 -07003201 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
3202 aom_obmc_sub_pixel_variance128x128)
3203 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
3204 aom_obmc_sub_pixel_variance128x64)
3205 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
3206 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07003207 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
3208 aom_obmc_sub_pixel_variance64x64)
3209 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
3210 aom_obmc_sub_pixel_variance64x32)
3211 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
3212 aom_obmc_sub_pixel_variance32x64)
3213 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
3214 aom_obmc_sub_pixel_variance32x32)
3215 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
3216 aom_obmc_sub_pixel_variance32x16)
3217 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
3218 aom_obmc_sub_pixel_variance16x32)
3219 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
3220 aom_obmc_sub_pixel_variance16x16)
3221 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
3222 aom_obmc_sub_pixel_variance16x8)
3223 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
3224 aom_obmc_sub_pixel_variance8x16)
3225 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
3226 aom_obmc_sub_pixel_variance8x8)
3227 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
3228 aom_obmc_sub_pixel_variance4x8)
3229 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
3230 aom_obmc_sub_pixel_variance8x4)
3231 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
3232 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003233 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
3234 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003235 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
3236 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003237 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
3238 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003239 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
3240 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003241 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
3242 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003243 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
3244 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003245
David Barkerf19f35f2017-05-22 16:33:22 +01003246#define MBFP(BT, MCSDF, MCSVF) \
3247 cpi->fn_ptr[BT].msdf = MCSDF; \
3248 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003249
David Barkerf19f35f2017-05-22 16:33:22 +01003250 MBFP(BLOCK_128X128, aom_masked_sad128x128,
3251 aom_masked_sub_pixel_variance128x128)
3252 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
3253 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01003254 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
3255 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
3256 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
3257 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
3258 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
3259 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
3260 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
3261 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
3262 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
3263 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
3264 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
3265 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
3266 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003267
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003268 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
3269
3270 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
3271
3272 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
3273
3274 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003275
3276 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
3277
3278 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01003279
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003280#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003281 highbd_set_var_fns(cpi);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003282#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003283
Yaowu Xuf883b422016-08-30 14:01:10 -07003284 /* av1_init_quantizer() is first called here. Add check in
3285 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07003286 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07003287 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003288 */
Yaowu Xuf883b422016-08-30 14:01:10 -07003289 av1_init_quantizer(cpi);
Zoe Liud902b742018-02-19 17:02:41 -08003290 av1_qm_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003291
Yaowu Xuf883b422016-08-30 14:01:10 -07003292 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07003293 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003294 cm->superres_upscaled_width = oxcf->width;
3295 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07003296 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003297
3298 cm->error.setjmp = 0;
3299
3300 return cpi;
3301}
3302
Urvang Joshiee2c8112018-05-04 14:53:15 -07003303#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003304#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
3305
3306#define SNPRINT2(H, T, V) \
3307 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07003308#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003309
Yaowu Xuf883b422016-08-30 14:01:10 -07003310void av1_remove_compressor(AV1_COMP *cpi) {
3311 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003312 unsigned int i;
3313 int t;
3314
3315 if (!cpi) return;
3316
3317 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003318 const int num_planes = av1_num_planes(cm);
3319
David Turnerd2a592e2018-11-16 14:59:31 +00003320 if (cm->current_frame.frame_number > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003321#if CONFIG_ENTROPY_STATS
3322 if (cpi->oxcf.pass != 1) {
3323 fprintf(stderr, "Writing counts.stt\n");
3324 FILE *f = fopen("counts.stt", "wb");
3325 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
3326 fclose(f);
3327 }
3328#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003329#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003330 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003331
3332 if (cpi->oxcf.pass != 1) {
3333 char headings[512] = { 0 };
3334 char results[512] = { 0 };
3335 FILE *f = fopen("opsnr.stt", "a");
3336 double time_encoded =
3337 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
3338 10000000.000;
3339 double total_encode_time =
3340 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3341 const double dr =
3342 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3343 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3344 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3345 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3346
3347 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003348 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003349 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3350 const double total_ssim =
3351 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3352 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08003353 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003354 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003355 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07003356 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003357 snprintf(results, sizeof(results),
3358 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3359 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003360 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07003361 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003362 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
3363 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
3364 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
3365 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003366 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003367 cpi->psnr.stat[STAT_Y] / cpi->count,
3368 cpi->psnr.stat[STAT_U] / cpi->count,
Yue Chenf0652ed2019-08-13 16:09:25 -07003369 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003370
3371 if (cpi->b_calculate_blockiness) {
3372 SNPRINT(headings, "\t Block\tWstBlck");
3373 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3374 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3375 }
3376
3377 if (cpi->b_calculate_consistency) {
3378 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003379 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003380 (double)cpi->total_inconsistency);
3381
3382 SNPRINT(headings, "\tConsist\tWstCons");
3383 SNPRINT2(results, "\t%7.3f", consistency);
3384 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3385 }
Yue Chenf0652ed2019-08-13 16:09:25 -07003386
3387 SNPRINT(headings, "\t Time\tRcErr\tAbsErr");
3388 SNPRINT2(results, "\t%8.0f", total_encode_time);
3389 SNPRINT2(results, "\t%7.2f", rate_err);
3390 SNPRINT2(results, "\t%7.2f", fabs(rate_err));
3391
3392 fprintf(f, "%s\tAPsnr611\n", headings);
3393 fprintf(f, "%s\t%7.3f\n", results,
3394 (6 * cpi->psnr.stat[STAT_Y] + cpi->psnr.stat[STAT_U] +
3395 cpi->psnr.stat[STAT_V]) /
3396 (cpi->count * 8));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003397 }
3398
3399 fclose(f);
3400 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07003401#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003402#if CONFIG_SPEED_STATS
3403 if (cpi->oxcf.pass != 1) {
3404 fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
3405 }
3406#endif // CONFIG_SPEED_STATS
chiyotsai92ed0dd2019-01-25 14:50:14 -08003407
chiyotsai9c484b32019-03-07 16:01:50 -08003408#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08003409 if (cpi->oxcf.pass != 1) {
3410 av1_print_partition_stats(&cpi->partition_stats);
3411 }
3412#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003413 }
3414
Yue Chenc9b23e02019-04-10 16:54:03 -07003415 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
Jingning Han81d6fbb2019-07-15 10:14:13 -07003416 aom_free(cpi->tpl_stats_buffer[frame].tpl_stats_ptr);
3417 cpi->tpl_stats_buffer[frame].is_valid = 0;
Jingning Han5e98d3b2019-09-23 21:59:36 -07003418
3419 aom_free_frame_buffer(&cpi->tpl_stats_buffer[frame].rec_picture_buf);
3420 cpi->tpl_stats_buffer[frame].rec_picture = NULL;
Yue Chen7cae98f2018-08-24 10:43:16 -07003421 }
3422
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303423 for (t = cpi->num_workers - 1; t >= 0; --t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003424 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003425 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3426
3427 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003428 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003429
3430 // Deallocate allocated thread data.
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303431 aom_free(thread_data->td->tctx);
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303432 if (t > 0) {
hui sud9a812b2017-07-06 14:34:37 -07003433 aom_free(thread_data->td->palette_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003434 aom_free(thread_data->td->tmp_conv_dst);
Hui Su38711e72019-06-11 10:49:47 -07003435 av1_release_compound_type_rd_buffers(&thread_data->td->comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003436 for (int j = 0; j < 2; ++j) {
3437 aom_free(thread_data->td->tmp_obmc_bufs[j]);
3438 }
Jingning Hand064cf02017-06-01 10:00:39 -07003439 aom_free(thread_data->td->above_pred_buf);
3440 aom_free(thread_data->td->left_pred_buf);
3441 aom_free(thread_data->td->wsrc_buf);
wenyao.liu22d8ab32018-10-16 09:11:29 +08003442
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303443 aom_free(thread_data->td->inter_modes_info);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003444 for (int x = 0; x < 2; x++) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303445 for (int y = 0; y < 2; y++) {
3446 aom_free(thread_data->td->hash_value_buffer[x][y]);
3447 thread_data->td->hash_value_buffer[x][y] = NULL;
3448 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003449 }
Jingning Hand064cf02017-06-01 10:00:39 -07003450 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003451 aom_free(thread_data->td->counts);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003452 av1_free_pc_tree(thread_data->td, num_planes);
Remya0cce44c2019-08-16 11:57:24 +05303453 aom_free(thread_data->td->mbmi_ext);
Yaowu Xuf883b422016-08-30 14:01:10 -07003454 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003455 }
3456 }
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303457#if CONFIG_MULTITHREAD
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303458 if (cpi->row_mt_mutex_ != NULL) {
3459 pthread_mutex_destroy(cpi->row_mt_mutex_);
3460 aom_free(cpi->row_mt_mutex_);
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303461 }
3462#endif
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05303463 av1_row_mt_mem_dealloc(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07003464 aom_free(cpi->tile_thr_data);
3465 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003466
Deepa K G964e72e2018-05-16 16:56:01 +05303467 if (cpi->num_workers > 1) {
3468 av1_loop_filter_dealloc(&cpi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05303469 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
Deepa K G964e72e2018-05-16 16:56:01 +05303470 }
3471
Yaowu Xuc27fc142016-08-22 16:08:15 -07003472 dealloc_compressor_data(cpi);
3473
3474 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
3475 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003476 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003477 }
3478
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003479#if CONFIG_INTERNAL_STATS
3480 aom_free(cpi->ssim_vars);
3481 cpi->ssim_vars = NULL;
3482#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003483
Yaowu Xuf883b422016-08-30 14:01:10 -07003484 av1_remove_common(cm);
RogerZhou80d52342017-11-20 10:56:26 -08003485 for (i = 0; i < FRAME_BUFFERS; ++i) {
3486 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
3487 }
Yaowu Xu74e63352019-05-06 09:21:33 -07003488#if CONFIG_HTB_TRELLIS
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08003489 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xu74e63352019-05-06 09:21:33 -07003490#endif // CONFIG_HTB_TRELLIS
Yaowu Xuf883b422016-08-30 14:01:10 -07003491 av1_free_ref_frame_buffers(cm->buffer_pool);
3492 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003493
3494#ifdef OUTPUT_YUV_SKINMAP
3495 fclose(yuv_skinmap_file);
3496#endif
3497#ifdef OUTPUT_YUV_REC
3498 fclose(yuv_rec_file);
3499#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003500}
3501
Yaowu Xuf883b422016-08-30 14:01:10 -07003502static void generate_psnr_packet(AV1_COMP *cpi) {
3503 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003504 int i;
3505 PSNR_STATS psnr;
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003506#if CONFIG_AV1_HIGHBITDEPTH
3507 // TODO(yaowu): unify these two versions into one.
3508 if (cpi->common.seq_params.use_highbitdepth)
3509 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
3510 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
3511 else
3512 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
3513#else
3514 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
3515#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003516
3517 for (i = 0; i < 4; ++i) {
3518 pkt.data.psnr.samples[i] = psnr.samples[i];
3519 pkt.data.psnr.sse[i] = psnr.sse[i];
3520 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3521 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003522 pkt.kind = AOM_CODEC_PSNR_PKT;
3523 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003524}
3525
Yaowu Xuf883b422016-08-30 14:01:10 -07003526int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003527 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3528
Yunqing Wangf2e7a392017-11-08 00:27:21 -08003529 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003530 return 0;
3531}
3532
Thomas Daede497d1952017-08-08 17:33:06 -07003533int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3534 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003535 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003536 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003537 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003538 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003539 return 0;
3540 } else {
3541 return -1;
3542 }
3543}
3544
Thomas Daede497d1952017-08-08 17:33:06 -07003545int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3546 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003547 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003548 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003549 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003550 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003551 return 0;
3552 } else {
3553 return -1;
3554 }
3555}
3556
3557int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003558 cpi->ext_refresh_frame_context = update;
3559 cpi->ext_refresh_frame_context_pending = 1;
3560 return 0;
3561}
3562
3563#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3564// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3565// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3566// not denoise the UV channels at this time. If ever we implement UV channel
3567// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003568void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003569 uint8_t *src = s->y_buffer;
3570 int h = s->y_height;
3571
3572 do {
3573 fwrite(src, s->y_width, 1, f);
3574 src += s->y_stride;
3575 } while (--h);
3576
3577 src = s->u_buffer;
3578 h = s->uv_height;
3579
3580 do {
3581 fwrite(src, s->uv_width, 1, f);
3582 src += s->uv_stride;
3583 } while (--h);
3584
3585 src = s->v_buffer;
3586 h = s->uv_height;
3587
3588 do {
3589 fwrite(src, s->uv_width, 1, f);
3590 src += s->uv_stride;
3591 } while (--h);
3592}
3593#endif
3594
Yaowu Xuc27fc142016-08-22 16:08:15 -07003595#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003596void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003597 uint8_t *src = s->y_buffer;
3598 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003599 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003600 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3601 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3602
3603 do {
3604 fwrite(src16, s->y_width, 2, yuv_rec_file);
3605 src16 += s->y_stride;
3606 } while (--h);
3607
3608 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3609 h = s->uv_height;
3610
3611 do {
3612 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3613 src16 += s->uv_stride;
3614 } while (--h);
3615
3616 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3617 h = s->uv_height;
3618
3619 do {
3620 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3621 src16 += s->uv_stride;
3622 } while (--h);
3623
3624 fflush(yuv_rec_file);
3625 return;
3626 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003627
3628 do {
3629 fwrite(src, s->y_width, 1, yuv_rec_file);
3630 src += s->y_stride;
3631 } while (--h);
3632
3633 src = s->u_buffer;
3634 h = s->uv_height;
3635
3636 do {
3637 fwrite(src, s->uv_width, 1, yuv_rec_file);
3638 src += s->uv_stride;
3639 } while (--h);
3640
3641 src = s->v_buffer;
3642 h = s->uv_height;
3643
3644 do {
3645 fwrite(src, s->uv_width, 1, yuv_rec_file);
3646 src += s->uv_stride;
3647 } while (--h);
3648
3649 fflush(yuv_rec_file);
3650}
3651#endif // OUTPUT_YUV_REC
3652
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003653#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003654static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3655 int i;
3656 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003657 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003658 AV1_COMMON *const cm = &cpi->common;
3659 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3660 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003661 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003662 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003663 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003664 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003665 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003666 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003667 // TODO(sarahparker): The earlier condition for recoding here was:
3668 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3669 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003670 }
3671 }
3672 return recode;
3673}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003674
Yaowu Xuc27fc142016-08-22 16:08:15 -07003675// Function to test for conditions that indicate we should loop
3676// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003677static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3678 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003679 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003680 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003681 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3682 int force_recode = 0;
3683
3684 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3685 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3686 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003687 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3688 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3689 (rc->projected_frame_size < low_limit && q > minq)) {
3690 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003691 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003692 // Deal with frame undershoot and whether or not we are
3693 // below the automatically set cq level.
3694 if (q > oxcf->cq_level &&
3695 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3696 force_recode = 1;
3697 }
3698 }
3699 }
3700 return force_recode;
3701}
3702
Cheng Chen46f30c72017-09-07 11:13:33 -07003703static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003704 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003705 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003706 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003707
3708 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003709 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Jingning Han667561a2019-07-22 15:48:01 -07003710 if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003711 BufferPool *const pool = cm->buffer_pool;
3712 const YV12_BUFFER_CONFIG *const ref =
David Turnera21966b2018-12-05 14:48:49 +00003713 get_ref_frame_yv12_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003714
3715 if (ref == NULL) {
David Turnere7ebf902018-12-04 14:04:55 +00003716 cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003717 continue;
3718 }
3719
Yaowu Xuc27fc142016-08-22 16:08:15 -07003720 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
Debargha Mukherjee13cccf22019-03-27 23:49:14 -07003721 // Replace the reference buffer with a copy having a thicker border,
3722 // if the reference buffer is higher resolution than the current
3723 // frame, and the border is thin.
3724 if ((ref->y_crop_width > cm->width ||
3725 ref->y_crop_height > cm->height) &&
3726 ref->border < AOM_BORDER_IN_PIXELS) {
3727 RefCntBuffer *ref_fb = get_ref_frame_buf(cm, ref_frame);
3728 if (aom_yv12_realloc_with_new_border(
3729 &ref_fb->buf, AOM_BORDER_IN_PIXELS, cm->byte_alignment,
3730 num_planes) != 0) {
3731 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3732 "Failed to allocate frame buffer");
3733 }
3734 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003735 int force_scaling = 0;
David Turnere7ebf902018-12-04 14:04:55 +00003736 RefCntBuffer *new_fb = cpi->scaled_ref_buf[ref_frame - 1];
3737 if (new_fb == NULL) {
3738 const int new_fb_idx = get_free_fb(cm);
3739 if (new_fb_idx == INVALID_IDX) {
Wan-Teh Chang4a8c0042018-10-05 09:41:52 -07003740 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3741 "Unable to find free frame buffer");
David Turnere7ebf902018-12-04 14:04:55 +00003742 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003743 force_scaling = 1;
David Turnere7ebf902018-12-04 14:04:55 +00003744 new_fb = &pool->frame_bufs[new_fb_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003745 }
David Turnere7ebf902018-12-04 14:04:55 +00003746
3747 if (force_scaling || new_fb->buf.y_crop_width != cm->width ||
3748 new_fb->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003749 if (aom_realloc_frame_buffer(
David Turnere7ebf902018-12-04 14:04:55 +00003750 &new_fb->buf, cm->width, cm->height,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003751 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
Debargha Mukherjeefa946af2019-03-26 16:58:55 -07003752 cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003753 cm->byte_alignment, NULL, NULL, NULL)) {
3754 if (force_scaling) {
3755 // Release the reference acquired in the get_free_fb() call above.
David Turnere7ebf902018-12-04 14:04:55 +00003756 --new_fb->ref_count;
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003757 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003758 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003759 "Failed to allocate frame buffer");
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003760 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003761 av1_resize_and_extend_frame(
David Turnere7ebf902018-12-04 14:04:55 +00003762 ref, &new_fb->buf, (int)cm->seq_params.bit_depth, num_planes);
3763 cpi->scaled_ref_buf[ref_frame - 1] = new_fb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003764 alloc_frame_mvs(cm, new_fb);
3765 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003766 } else {
David Turnera21966b2018-12-05 14:48:49 +00003767 RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003768 buf->buf.y_crop_width = ref->y_crop_width;
3769 buf->buf.y_crop_height = ref->y_crop_height;
David Turnere7ebf902018-12-04 14:04:55 +00003770 cpi->scaled_ref_buf[ref_frame - 1] = buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003771 ++buf->ref_count;
3772 }
3773 } else {
David Turnere7ebf902018-12-04 14:04:55 +00003774 if (cpi->oxcf.pass != 0) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003775 }
3776 }
3777}
3778
Yaowu Xuf883b422016-08-30 14:01:10 -07003779static void release_scaled_references(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003780 // TODO(isbs): only refresh the necessary frames, rather than all of them
David Turnere7ebf902018-12-04 14:04:55 +00003781 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3782 RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
3783 if (buf != NULL) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003784 --buf->ref_count;
David Turnere7ebf902018-12-04 14:04:55 +00003785 cpi->scaled_ref_buf[i] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003786 }
3787 }
3788}
3789
Yaowu Xuf883b422016-08-30 14:01:10 -07003790static void set_mv_search_params(AV1_COMP *cpi) {
3791 const AV1_COMMON *const cm = &cpi->common;
3792 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003793
3794 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003795 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003796
3797 if (cpi->sf.mv.auto_mv_step_size) {
3798 if (frame_is_intra_only(cm)) {
3799 // Initialize max_mv_magnitude for use in the first INTER frame
3800 // after a key/intra-only frame.
3801 cpi->max_mv_magnitude = max_mv_def;
3802 } else {
3803 if (cm->show_frame) {
3804 // Allow mv_steps to correspond to twice the max mv magnitude found
3805 // in the previous frame, capped by the default max_mv_magnitude based
3806 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003807 cpi->mv_step_param = av1_init_search_range(
3808 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003809 }
3810 cpi->max_mv_magnitude = 0;
3811 }
3812 }
3813}
3814
Hui Subdf0c992019-02-14 14:52:41 -08003815static void set_screen_content_options(AV1_COMP *cpi) {
3816 AV1_COMMON *cm = &cpi->common;
3817
3818 if (cm->seq_params.force_screen_content_tools != 2) {
3819 cm->allow_screen_content_tools = cm->allow_intrabc =
3820 cm->seq_params.force_screen_content_tools;
3821 return;
3822 }
3823
3824 if (cpi->oxcf.content == AOM_CONTENT_SCREEN) {
3825 cm->allow_screen_content_tools = cm->allow_intrabc = 1;
3826 return;
3827 }
3828
3829 // Estimate if the source frame is screen content, based on the portion of
3830 // blocks that have few luma colors.
3831 const uint8_t *src = cpi->source->y_buffer;
3832 assert(src != NULL);
3833 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
3834 const int stride = cpi->source->y_stride;
3835 const int width = cpi->source->y_width;
3836 const int height = cpi->source->y_height;
3837 const int bd = cm->seq_params.bit_depth;
3838 const int blk_w = 16;
3839 const int blk_h = 16;
3840 // These threshold values are selected experimentally.
3841 const int color_thresh = 4;
3842 const unsigned int var_thresh = 0;
3843 // Counts of blocks with no more than color_thresh colors.
3844 int counts_1 = 0;
3845 // Counts of blocks with no more than color_thresh colors and variance larger
3846 // than var_thresh.
3847 int counts_2 = 0;
3848
3849 for (int r = 0; r + blk_h <= height; r += blk_h) {
3850 for (int c = 0; c + blk_w <= width; c += blk_w) {
3851 int count_buf[1 << 12]; // Maximum (1 << 12) color levels.
3852 const uint8_t *const this_src = src + r * stride + c;
3853 const int n_colors =
3854 use_hbd ? av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd,
3855 count_buf)
3856 : av1_count_colors(this_src, stride, blk_w, blk_h, count_buf);
3857 if (n_colors > 1 && n_colors <= color_thresh) {
3858 ++counts_1;
3859 struct buf_2d buf;
3860 buf.stride = stride;
3861 buf.buf = (uint8_t *)this_src;
3862 const unsigned int var =
3863 use_hbd
3864 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
3865 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
3866 if (var > var_thresh) ++counts_2;
3867 }
3868 }
3869 }
3870
3871 // The threshold values are selected experimentally.
3872 cm->allow_screen_content_tools =
3873 counts_1 * blk_h * blk_w * 10 > width * height;
3874 // IntraBC would force loop filters off, so we use more strict rules that also
3875 // requires that the block has high variance.
3876 cm->allow_intrabc = cm->allow_screen_content_tools &&
Hui Su64a2ffb2019-04-11 16:47:13 -07003877 counts_2 * blk_h * blk_w * 12 > width * height;
Hui Subdf0c992019-02-14 14:52:41 -08003878}
3879
Yaowu Xuf883b422016-08-30 14:01:10 -07003880static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003881 int i;
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003882 AV1_COMMON *cm = &cpi->common;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003883 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003884 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003885 }
3886 cpi->global_motion_search_done = 0;
Yunqing Wang9411e432019-03-14 15:53:23 -07003887
3888 if (frame_is_intra_only(cm)) set_screen_content_options(cpi);
3889 cpi->is_screen_content_type = (cm->allow_screen_content_tools != 0);
3890
David Turner04b70d82019-01-24 15:39:19 +00003891 av1_set_speed_features_framesize_independent(cpi, cpi->speed);
Yaowu Xuf883b422016-08-30 14:01:10 -07003892 av1_set_rd_speed_thresholds(cpi);
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003893 cm->interp_filter = SWITCHABLE;
3894 cm->switchable_motion_mode = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003895}
3896
Jerome Jiangd413f4a2019-09-23 14:53:03 -07003897#if !CONFIG_REALTIME_ONLY
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003898static int get_gfu_boost_from_r0(double r0, int frames_to_key) {
3899 double factor = sqrt((double)frames_to_key);
3900 factor = AOMMIN(factor, 10.0);
3901 factor = AOMMAX(factor, 4.0);
3902 const int boost = (int)rint((200.0 + 10.0 * factor) / r0);
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003903 return boost;
3904}
3905
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003906static int get_kf_boost_from_r0(double r0, int frames_to_key) {
3907 double factor = sqrt((double)frames_to_key);
3908 factor = AOMMIN(factor, 10.0);
3909 factor = AOMMAX(factor, 4.0);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003910 const int boost = (int)rint((75.0 + 14.0 * factor) / r0);
3911 return boost;
3912}
Jerome Jiangd413f4a2019-09-23 14:53:03 -07003913#endif
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003914
3915int combine_prior_with_tpl_boost(int prior_boost, int tpl_boost,
3916 int frames_to_key) {
3917 double factor = sqrt((double)frames_to_key);
3918 factor = AOMMIN(factor, 12.0);
3919 factor = AOMMAX(factor, 4.0);
3920 factor -= 4.0;
3921 int boost = (int)((factor * prior_boost + (8.0 - factor) * tpl_boost) / 8.0);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003922 return boost;
3923}
3924
Jerome Jiangd413f4a2019-09-23 14:53:03 -07003925#if !CONFIG_REALTIME_ONLY
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003926static void process_tpl_stats_frame(AV1_COMP *cpi) {
Sarah Parkere1b22012019-06-06 16:35:25 -07003927 const GF_GROUP *const gf_group = &cpi->gf_group;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003928 AV1_COMMON *const cm = &cpi->common;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003929
Sarah Parkere1b22012019-06-06 16:35:25 -07003930 assert(IMPLIES(gf_group->size > 0, gf_group->index < gf_group->size));
Jingning Han31a0ee92019-07-15 13:56:55 -07003931
3932 const int tpl_idx = gf_group->index;
Jingning Han81d6fbb2019-07-15 10:14:13 -07003933 TplDepFrame *tpl_frame = &cpi->tpl_frame[tpl_idx];
Yue Chen4e585cc2019-06-03 14:47:16 -07003934 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003935
Yue Chen4e585cc2019-06-03 14:47:16 -07003936 if (tpl_frame->is_valid) {
3937 int tpl_stride = tpl_frame->stride;
3938 int64_t intra_cost_base = 0;
3939 int64_t mc_dep_cost_base = 0;
Yue Chenbd934232019-08-05 14:23:39 -07003940#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07003941 int64_t mc_saved_base = 0;
3942 int64_t mc_count_base = 0;
Yue Chenbd934232019-08-05 14:23:39 -07003943#endif // !USE_TPL_CLASSIC_MODEL
Yue Chenb4c93f02019-08-05 13:47:31 -07003944 const int step = 1 << cpi->tpl_stats_block_mis_log2;
Debargha Mukherjeeedfa4fe2019-07-08 14:23:39 -07003945 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
Yue Chenb4c93f02019-08-05 13:47:31 -07003946
3947 for (int row = 0; row < cm->mi_rows; row += step) {
3948 for (int col = 0; col < mi_cols_sr; col += step) {
3949 TplDepStats *this_stats =
3950 &tpl_stats[av1_tpl_ptr_pos(cpi, row, col, tpl_stride)];
Jingning Han40e870f2019-10-02 16:05:39 -07003951 int64_t mc_dep_delta =
3952 RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
3953 this_stats->mc_dep_dist);
3954 intra_cost_base += (this_stats->recrf_dist << RDDIV_BITS);
3955 mc_dep_cost_base +=
3956 (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta;
Yue Chenbd934232019-08-05 14:23:39 -07003957#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07003958 mc_count_base += this_stats->mc_count;
3959 mc_saved_base += this_stats->mc_saved;
Yue Chenbd934232019-08-05 14:23:39 -07003960#endif // !USE_TPL_CLASSIC_MODEL
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003961 }
Yue Chen4e585cc2019-06-03 14:47:16 -07003962 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003963
Yue Chen4e585cc2019-06-03 14:47:16 -07003964 if (mc_dep_cost_base == 0) {
3965 tpl_frame->is_valid = 0;
3966 } else {
3967 aom_clear_system_state();
3968 cpi->rd.r0 = (double)intra_cost_base / mc_dep_cost_base;
Jingning Han44a573b2019-09-04 14:01:22 -07003969 if (is_frame_arf_and_tpl_eligible(gf_group)) {
Yue Chen4e585cc2019-06-03 14:47:16 -07003970 cpi->rd.arf_r0 = cpi->rd.r0;
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003971 const int gfu_boost =
3972 get_gfu_boost_from_r0(cpi->rd.arf_r0, cpi->rc.frames_to_key);
Yue Chen4e585cc2019-06-03 14:47:16 -07003973 // printf("old boost %d new boost %d\n", cpi->rc.gfu_boost,
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003974 // gfu_boost);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003975 cpi->rc.gfu_boost = combine_prior_with_tpl_boost(
3976 cpi->rc.gfu_boost, gfu_boost, cpi->rc.frames_to_key);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003977 } else if (frame_is_intra_only(cm)) {
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07003978 // TODO(debargha): Turn off q adjustment for kf temporarily to
3979 // reduce impact on speed of encoding. Need to investigate how
3980 // to mitigate the issue.
3981 if (cpi->oxcf.rc_mode == AOM_Q) {
3982 const int kf_boost =
3983 get_kf_boost_from_r0(cpi->rd.r0, cpi->rc.frames_to_key);
3984 // printf("old kf boost %d new kf boost %d [%d]\n", cpi->rc.kf_boost,
3985 // kf_boost, cpi->rc.frames_to_key);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003986 cpi->rc.kf_boost = combine_prior_with_tpl_boost(
3987 cpi->rc.kf_boost, kf_boost, cpi->rc.frames_to_key);
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07003988 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003989 }
Yue Chenbd934232019-08-05 14:23:39 -07003990#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07003991 cpi->rd.mc_count_base =
3992 (double)mc_count_base / (cm->mi_rows * cm->mi_cols);
3993 cpi->rd.mc_saved_base =
3994 (double)mc_saved_base / (cm->mi_rows * cm->mi_cols);
Yue Chenbd934232019-08-05 14:23:39 -07003995#endif // !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07003996 aom_clear_system_state();
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003997 }
3998 }
3999}
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004000#endif // !CONFIG_REALTIME_ONLY
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004001
Debargha Mukherjeecd14cc12019-09-06 09:55:52 -07004002static int determine_frame_high_precision_mv(const AV1_COMP *cpi, int qindex) {
4003 (void)cpi;
4004 if (cpi->sf.reduce_high_precision_mv_usage == 2)
4005 return 0;
4006 else if (cpi->sf.reduce_high_precision_mv_usage == 1)
4007 return qindex < HIGH_PRECISION_MV_QTHRESH / 2;
4008 else
4009 return qindex < HIGH_PRECISION_MV_QTHRESH;
4010}
4011
Yaowu Xuf883b422016-08-30 14:01:10 -07004012static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004013 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004014 AV1_COMMON *const cm = &cpi->common;
4015 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004016
4017 // Setup variables that depend on the dimensions of the frame.
David Turner04b70d82019-01-24 15:39:19 +00004018 av1_set_speed_features_framesize_dependent(cpi, cpi->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004019
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004020#if !CONFIG_REALTIME_ONLY
Yue Chen4e585cc2019-06-03 14:47:16 -07004021 if (cpi->oxcf.enable_tpl_model && cpi->tpl_model_pass == 0 &&
sdengf46a1062019-08-04 18:43:50 -07004022 is_frame_tpl_eligible(cpi)) {
Yue Chen4e585cc2019-06-03 14:47:16 -07004023 process_tpl_stats_frame(cpi);
sdengf46a1062019-08-04 18:43:50 -07004024 av1_tpl_rdmult_setup(cpi);
4025 }
Jerome Jiangd413f4a2019-09-23 14:53:03 -07004026#endif
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07004027
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01004028 // Decide q and q bounds.
Angie Chiang958904c2019-09-19 16:03:35 -07004029 *q = av1_rc_pick_q_and_bounds(cpi, &cpi->rc, cm->width, cm->height,
4030 cpi->gf_group.index, bottom_index, top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004031
James Zern01a9d702017-08-25 19:09:33 +00004032 if (!frame_is_intra_only(cm)) {
Debargha Mukherjeecd14cc12019-09-06 09:55:52 -07004033 const int use_hp = cpi->common.cur_frame_force_integer_mv
4034 ? 0
4035 : determine_frame_high_precision_mv(cpi, *q);
4036 set_high_precision_mv(cpi, use_hp, cpi->common.cur_frame_force_integer_mv);
James Zern01a9d702017-08-25 19:09:33 +00004037 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004038
4039 // Configure experimental use of segmentation for enhanced coding of
4040 // static regions if indicated.
4041 // Only allowed in the second pass of a two pass encode, as it requires
4042 // lagged coding, and if the relevant speed feature flag is set.
4043 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
4044 configure_static_seg_features(cpi);
4045}
4046
Yaowu Xuf883b422016-08-30 14:01:10 -07004047static void init_motion_estimation(AV1_COMP *cpi) {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004048 AV1_COMMON *const cm = &cpi->common;
chiyotsai836b69b2019-04-09 13:41:24 -07004049 const int y_stride = cpi->scaled_source.y_stride;
Urvang Joshi510d8f62019-01-10 12:11:50 -08004050 const int y_stride_src =
4051 ((cpi->oxcf.width != cm->width || cpi->oxcf.height != cm->height) ||
4052 av1_superres_scaled(cm))
4053 ? y_stride
4054 : cpi->lookahead->buf->img.y_stride;
chiyotsai836b69b2019-04-09 13:41:24 -07004055 // Update if ss_cfg is uninitialized or the current frame has a new stride
4056 const int should_update = !cpi->ss_cfg[SS_CFG_SRC].stride ||
4057 !cpi->ss_cfg[SS_CFG_LOOKAHEAD].stride ||
4058 (y_stride != cpi->ss_cfg[SS_CFG_SRC].stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004059
chiyotsai836b69b2019-04-09 13:41:24 -07004060 if (!should_update) {
4061 return;
4062 }
4063
4064 if (cpi->sf.mv.search_method == DIAMOND) {
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05304065 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
4066 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD],
4067 y_stride_src);
chiyotsai836b69b2019-04-09 13:41:24 -07004068 } else {
4069 // Update the offsets in search_sites as y_stride can change due to scaled
4070 // references. This update allows NSTEP to be used on scaled references as
4071 // long as sf.mv.search_method is not DIAMOND. Currently in the codebae,
4072 // sf.mv.search_method is never set to DIAMOND.
4073 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
4074 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD], y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004075 }
4076}
4077
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004078#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004079static void set_restoration_unit_size(int width, int height, int sx, int sy,
4080 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004081 (void)width;
4082 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004083 (void)sx;
4084 (void)sy;
4085#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
4086 int s = AOMMIN(sx, sy);
4087#else
4088 int s = 0;
4089#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
4090
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07004091 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08004092 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07004093 else
Urvang Joshi813186b2018-03-08 15:38:46 -08004094 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004095 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
4096 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004097}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004098
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05304099static void init_ref_frame_bufs(AV1_COMP *cpi) {
4100 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07004101 int i;
4102 BufferPool *const pool = cm->buffer_pool;
Jack Haughtonddb80602018-11-21 16:41:49 +00004103 cm->cur_frame = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -07004104 for (i = 0; i < REF_FRAMES; ++i) {
David Turnere7ebf902018-12-04 14:04:55 +00004105 cm->ref_frame_map[i] = NULL;
Wan-Teh Changd05e0332018-10-03 12:00:43 -07004106 }
4107 for (i = 0; i < FRAME_BUFFERS; ++i) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004108 pool->frame_bufs[i].ref_count = 0;
4109 }
RogerZhou86902d02018-02-28 15:29:16 -08004110 if (cm->seq_params.force_screen_content_tools) {
Hui Su2d5fd742018-02-21 18:10:37 -08004111 for (i = 0; i < FRAME_BUFFERS; ++i) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05304112 av1_hash_table_init(&pool->frame_bufs[i].hash_table, &cpi->td.mb);
Hui Su2d5fd742018-02-21 18:10:37 -08004113 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004114 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004115}
4116
Yaowu Xud3e7c682017-12-21 14:08:25 -08004117static void check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
Cheng Chen46f30c72017-09-07 11:13:33 -07004118 int subsampling_x, int subsampling_y) {
4119 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004120 SequenceHeader *const seq_params = &cm->seq_params;
Cheng Chen46f30c72017-09-07 11:13:33 -07004121
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004122 if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
4123 seq_params->subsampling_x != subsampling_x ||
4124 seq_params->subsampling_y != subsampling_y) {
4125 seq_params->subsampling_x = subsampling_x;
4126 seq_params->subsampling_y = subsampling_y;
4127 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07004128
4129 alloc_raw_frame_buffers(cpi);
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05304130 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07004131 alloc_util_frame_buffers(cpi);
4132
4133 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
4134
4135 cpi->initial_width = cm->width;
4136 cpi->initial_height = cm->height;
4137 cpi->initial_mbs = cm->MBs;
4138 }
4139}
4140
4141// Returns 1 if the assigned width or height was <= 0.
Marco Paniconi63971322019-08-15 21:32:05 -07004142int av1_set_size_literal(AV1_COMP *cpi, int width, int height) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004143 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004144 const int num_planes = av1_num_planes(cm);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004145 check_initial_width(cpi, cm->seq_params.use_highbitdepth,
4146 cm->seq_params.subsampling_x,
4147 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07004148
4149 if (width <= 0 || height <= 0) return 1;
4150
4151 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07004152 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004153
4154 if (cpi->initial_width && cpi->initial_height &&
4155 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
4156 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004157 av1_free_pc_tree(&cpi->td, num_planes);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004158 alloc_compressor_data(cpi);
4159 realloc_segmentation_maps(cpi);
4160 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07004161 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004162 update_frame_size(cpi);
4163
4164 return 0;
4165}
4166
David Turner475a3132019-01-18 15:17:17 +00004167void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07004168 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004169 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004170 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004171 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004172 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004173
Fergus Simpsonbc189932017-05-16 17:02:39 -07004174 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07004175 // There has been a change in the encoded frame size
Marco Paniconi63971322019-08-15 21:32:05 -07004176 av1_set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004177 set_mv_search_params(cpi);
Urvang Joshic8b52d52018-03-23 13:16:51 -07004178 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Cheng Chen09c83a52018-06-05 12:27:36 -07004179 cm->all_lossless = cm->coded_lossless && !av1_superres_scaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004180 }
4181
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004182 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004183 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004184 }
4185
David Turnere7ebf902018-12-04 14:04:55 +00004186 alloc_frame_mvs(cm, cm->cur_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004187
Cherma Rajan A71d20db2018-04-27 11:15:32 +05304188 // Allocate above context buffers
Cherma Rajan Af1479082018-05-09 14:26:34 +05304189 if (cm->num_allocated_above_context_planes < av1_num_planes(cm) ||
4190 cm->num_allocated_above_context_mi_col < cm->mi_cols ||
Cherma Rajan A71d20db2018-04-27 11:15:32 +05304191 cm->num_allocated_above_contexts < cm->tile_rows) {
4192 av1_free_above_context_buffers(cm, cm->num_allocated_above_contexts);
4193 if (av1_alloc_above_context_buffers(cm, cm->tile_rows))
4194 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
4195 "Failed to allocate context buffers");
4196 }
4197
Yaowu Xuc27fc142016-08-22 16:08:15 -07004198 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004199 if (aom_realloc_frame_buffer(
Jack Haughtonddb80602018-11-21 16:41:49 +00004200 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
4201 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +05304202 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07004203 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004204 "Failed to allocate frame buffer");
4205
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004206 const int frame_width = cm->superres_upscaled_width;
4207 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004208 set_restoration_unit_size(frame_width, frame_height,
4209 seq_params->subsampling_x,
4210 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004211 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01004212 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004213
4214 av1_alloc_restoration_buffers(cm);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004215 alloc_util_frame_buffers(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004216 init_motion_estimation(cpi);
4217
4218 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00004219 RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00004220 if (buf != NULL) {
David Turnera21966b2018-12-05 14:48:49 +00004221 struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
4222 av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
David Turner1bcefb32018-11-19 17:54:00 +00004223 buf->buf.y_crop_height, cm->width,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08004224 cm->height);
David Turnera21966b2018-12-05 14:48:49 +00004225 if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004226 }
4227 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07004228
Hui Su5ebd8702018-01-08 18:09:20 -08004229 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08004230 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004231
4232 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
4233}
4234
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004235static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
4236 // Choose an arbitrary random number
4237 static unsigned int seed = 56789;
4238 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004239 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4240 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004241
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07004242 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004243 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004244 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004245 case RESIZE_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00004246 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004247 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004248 else
Urvang Joshide71d142017-10-05 12:12:15 -07004249 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004250 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004251 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004252 default: assert(0);
4253 }
Urvang Joshide71d142017-10-05 12:12:15 -07004254 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004255}
4256
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004257#if CONFIG_SUPERRES_IN_RECODE
4258static int superres_in_recode_allowed(const AV1_COMP *const cpi) {
4259 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
4260 // Empirically found to not be beneficial for AOM_Q mode and images coding.
4261 return oxcf->superres_mode == SUPERRES_AUTO &&
4262 (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ) &&
4263 cpi->rc.frames_to_key > 1;
4264}
4265#endif // CONFIG_SUPERRES_IN_RECODE
4266
Urvang Joshi510d8f62019-01-10 12:11:50 -08004267#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO 0.012
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004268#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME 0.008
4269#define SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME 0.008
Urvang Joshi510d8f62019-01-10 12:11:50 -08004270#define SUPERRES_ENERGY_BY_AC_THRESH 0.2
4271
4272static double get_energy_by_q2_thresh(const GF_GROUP *gf_group,
4273 const RATE_CONTROL *rc) {
4274 // TODO(now): Return keyframe thresh * factor based on frame type / pyramid
4275 // level.
4276 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
4277 return SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME;
4278 } else if (gf_group->update_type[gf_group->index] == KF_UPDATE) {
4279 if (rc->frames_to_key <= 1)
4280 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO;
4281 else
4282 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME;
4283 } else {
4284 assert(0);
4285 }
4286 return 0;
4287}
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004288
4289static uint8_t get_superres_denom_from_qindex_energy(int qindex, double *energy,
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004290 double threshq,
4291 double threshp) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004292 const double q = av1_convert_qindex_to_q(qindex, AOM_BITS_8);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004293 const double tq = threshq * q * q;
4294 const double tp = threshp * energy[1];
4295 const double thresh = AOMMIN(tq, tp);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004296 int k;
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004297 for (k = SCALE_NUMERATOR * 2; k > SCALE_NUMERATOR; --k) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004298 if (energy[k - 1] > thresh) break;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004299 }
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004300 return 3 * SCALE_NUMERATOR - k;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004301}
4302
Urvang Joshi510d8f62019-01-10 12:11:50 -08004303static uint8_t get_superres_denom_for_qindex(const AV1_COMP *cpi, int qindex,
4304 int sr_kf, int sr_arf) {
4305 // Use superres for Key-frames and Alt-ref frames only.
4306 const GF_GROUP *gf_group = &cpi->gf_group;
4307 if (gf_group->update_type[gf_group->index] != KF_UPDATE &&
4308 gf_group->update_type[gf_group->index] != ARF_UPDATE) {
4309 return SCALE_NUMERATOR;
4310 }
4311 if (gf_group->update_type[gf_group->index] == KF_UPDATE && !sr_kf) {
4312 return SCALE_NUMERATOR;
4313 }
4314 if (gf_group->update_type[gf_group->index] == ARF_UPDATE && !sr_arf) {
4315 return SCALE_NUMERATOR;
4316 }
4317
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004318 double energy[16];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004319 analyze_hor_freq(cpi, energy);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004320
4321 const double energy_by_q2_thresh =
4322 get_energy_by_q2_thresh(gf_group, &cpi->rc);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004323 int denom = get_superres_denom_from_qindex_energy(
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004324 qindex, energy, energy_by_q2_thresh, SUPERRES_ENERGY_BY_AC_THRESH);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004325 /*
4326 printf("\nenergy = [");
4327 for (int k = 1; k < 16; ++k) printf("%f, ", energy[k]);
4328 printf("]\n");
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004329 printf("boost = %d\n",
4330 (gf_group->update_type[gf_group->index] == KF_UPDATE)
4331 ? cpi->rc.kf_boost
4332 : cpi->rc.gfu_boost);
4333 printf("denom = %d\n", denom);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004334 */
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004335#if CONFIG_SUPERRES_IN_RECODE
4336 if (superres_in_recode_allowed(cpi)) {
4337 // Force superres to be tried in the recode loop, as full-res is also going
4338 // to be tried anyway.
4339 denom = AOMMAX(denom, SCALE_NUMERATOR + 1);
4340 }
4341#endif // CONFIG_SUPERRES_IN_RECODE
Urvang Joshi510d8f62019-01-10 12:11:50 -08004342 return denom;
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004343}
4344
Urvang Joshibc82d382019-11-01 17:59:20 -07004345// If true, SUPERRES_AUTO mode will exhaustively search over all superres
4346// denominators for all frames (except overlay and internal overlay frames).
4347#define SUPERRES_RECODE_ALL_RATIOS 0
4348
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004349static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
4350 // Choose an arbitrary random number
4351 static unsigned int seed = 34567;
4352 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004353 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4354 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07004355
4356 // Make sure that superres mode of the frame is consistent with the
4357 // sequence-level flag.
4358 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
4359 cpi->common.seq_params.enable_superres));
4360 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
4361 oxcf->superres_mode == SUPERRES_NONE));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004362
4363 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004364 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004365 case SUPERRES_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00004366 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004367 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004368 else
Urvang Joshide71d142017-10-05 12:12:15 -07004369 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004370 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004371 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004372 case SUPERRES_QTHRESH: {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07004373 // Do not use superres when screen content tools are used.
4374 if (cpi->common.allow_screen_content_tools) break;
Debargha Mukherjee2b2c5fd2018-11-14 13:21:24 -08004375 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4376 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004377
4378 // Now decide the use of superres based on 'q'.
Urvang Joshi2c92b072018-03-19 17:23:31 -07004379 int bottom_index, top_index;
4380 const int q = av1_rc_pick_q_and_bounds(
Angie Chiang958904c2019-09-19 16:03:35 -07004381 cpi, &cpi->rc, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
Jingning Han2e029872019-08-02 10:54:19 -07004382 &bottom_index, &top_index);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004383
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004384 const int qthresh = (frame_is_intra_only(&cpi->common))
4385 ? oxcf->superres_kf_qthresh
4386 : oxcf->superres_qthresh;
Urvang Joshib17e0a62019-01-11 16:11:54 -08004387 if (q <= qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07004388 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004389 } else {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004390 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004391 }
4392 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004393 }
Urvang Joshi36a83732019-01-31 15:31:57 -08004394 case SUPERRES_AUTO: {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004395 // Do not use superres when screen content tools are used.
Urvang Joshi36a83732019-01-31 15:31:57 -08004396 if (cpi->common.allow_screen_content_tools) break;
Urvang Joshi510d8f62019-01-10 12:11:50 -08004397 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4398 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi36a83732019-01-31 15:31:57 -08004399
4400 // Now decide the use of superres based on 'q'.
4401 int bottom_index, top_index;
4402 const int q = av1_rc_pick_q_and_bounds(
Angie Chiang958904c2019-09-19 16:03:35 -07004403 cpi, &cpi->rc, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
Jingning Han2e029872019-08-02 10:54:19 -07004404 &bottom_index, &top_index);
Urvang Joshi36a83732019-01-31 15:31:57 -08004405
4406 const int qthresh = 128;
4407 if (q <= qthresh) {
4408 new_denom = SCALE_NUMERATOR;
4409 } else {
Urvang Joshibc82d382019-11-01 17:59:20 -07004410#if SUPERRES_RECODE_ALL_RATIOS
4411 if (cpi->common.current_frame.frame_type == KEY_FRAME)
4412 new_denom = oxcf->superres_kf_scale_denominator;
4413 else
4414 new_denom = oxcf->superres_scale_denominator;
4415#else
Urvang Joshi510d8f62019-01-10 12:11:50 -08004416 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Urvang Joshibc82d382019-11-01 17:59:20 -07004417#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi36a83732019-01-31 15:31:57 -08004418 }
4419 break;
4420 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004421 default: assert(0);
4422 }
Urvang Joshide71d142017-10-05 12:12:15 -07004423 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004424}
4425
Urvang Joshide71d142017-10-05 12:12:15 -07004426static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4427 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4428}
4429
4430static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08004431 // Only need to check the width, as scaling is horizontal only.
4432 (void)oheight;
4433 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07004434}
4435
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004436static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004437 SUPERRES_MODE superres_mode, int owidth,
4438 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004439 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004440 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004441 }
4442
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004443 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07004444 int resize_denom =
4445 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4446 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4447
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004448 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004449 // Alter superres scale as needed to enforce conformity.
4450 rsz->superres_denom =
4451 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4452 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4453 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004454 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004455 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004456 // Alter resize scale as needed to enforce conformity.
4457 resize_denom =
4458 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004459 rsz->resize_width = owidth;
4460 rsz->resize_height = oheight;
4461 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004462 resize_denom);
4463 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4464 if (resize_denom > SCALE_NUMERATOR) {
4465 --resize_denom;
4466 rsz->resize_width = owidth;
4467 rsz->resize_height = oheight;
4468 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4469 resize_denom);
4470 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004471 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004472 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004473 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004474 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004475 if (resize_denom > rsz->superres_denom)
4476 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004477 else
Urvang Joshide71d142017-10-05 12:12:15 -07004478 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004479 rsz->resize_width = owidth;
4480 rsz->resize_height = oheight;
4481 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004482 resize_denom);
4483 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4484 (resize_denom > SCALE_NUMERATOR ||
4485 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08004486 } else { // We are allowed to alter neither resize scale nor superres
4487 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004488 return 0;
4489 }
Urvang Joshide71d142017-10-05 12:12:15 -07004490 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004491}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004492
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004493// Calculates resize and superres params for next frame
David Turner475a3132019-01-18 15:17:17 +00004494static size_params_type calculate_next_size_params(AV1_COMP *cpi) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004495 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08004496 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshi510d8f62019-01-10 12:11:50 -08004497 int resize_denom = SCALE_NUMERATOR;
Marco Paniconi63971322019-08-15 21:32:05 -07004498 if (oxcf->pass == 0 && cpi->use_svc &&
4499 cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1) {
4500 rsz.resize_width = cpi->common.width;
4501 rsz.resize_height = cpi->common.height;
4502 return rsz;
4503 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004504 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004505 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4506 rsz.resize_width = cpi->resize_pending_width;
4507 rsz.resize_height = cpi->resize_pending_height;
4508 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4509 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004510 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004511 rsz.resize_width = cpi->oxcf.width;
4512 rsz.resize_height = cpi->oxcf.height;
4513 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004514 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004515 }
Urvang Joshide71d142017-10-05 12:12:15 -07004516 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004517 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
4518 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004519 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004520 return rsz;
4521}
4522
Urvang Joshi22b150b2019-01-10 14:32:32 -08004523static void setup_frame_size_from_params(AV1_COMP *cpi,
4524 const size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004525 int encode_width = rsz->resize_width;
4526 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004527
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004528 AV1_COMMON *cm = &cpi->common;
4529 cm->superres_upscaled_width = encode_width;
4530 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004531 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004532 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4533 rsz->superres_denom);
David Turner475a3132019-01-18 15:17:17 +00004534 av1_set_frame_size(cpi, encode_width, encode_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004535}
4536
David Turnerdedd8ff2019-01-23 13:59:46 +00004537void av1_setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004538 AV1_COMMON *cm = &cpi->common;
Urvang Joshi22b150b2019-01-10 14:32:32 -08004539 // Reset superres params from previous frame.
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004540 cm->superres_scale_denominator = SCALE_NUMERATOR;
David Turner475a3132019-01-18 15:17:17 +00004541 const size_params_type rsz = calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004542 setup_frame_size_from_params(cpi, &rsz);
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004543
Yaowu Xu7e450882019-04-30 15:09:18 -07004544 assert(av1_is_min_tile_width_satisfied(cm));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004545}
4546
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004547static void superres_post_encode(AV1_COMP *cpi) {
4548 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004549 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004550
Cheng Chen09c83a52018-06-05 12:27:36 -07004551 if (!av1_superres_scaled(cm)) return;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004552
Urvang Joshid6b5d512018-03-20 13:34:38 -07004553 assert(cpi->oxcf.enable_superres);
4554 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshic8b52d52018-03-23 13:16:51 -07004555 assert(!cm->all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07004556
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004557 av1_superres_upscale(cm, NULL);
4558
4559 // If regular resizing is occurring the source will need to be downscaled to
4560 // match the upscaled superres resolution. Otherwise the original source is
4561 // used.
Cheng Chen09c83a52018-06-05 12:27:36 -07004562 if (!av1_resize_scaled(cm)) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004563 cpi->source = cpi->unscaled_source;
4564 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4565 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004566 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4567 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08004568 // Do downscale. cm->(width|height) has been updated by
4569 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004570 if (aom_realloc_frame_buffer(
4571 &cpi->scaled_source, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004572 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4573 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
4574 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004575 aom_internal_error(
4576 &cm->error, AOM_CODEC_MEM_ERROR,
4577 "Failed to reallocate scaled source buffer for superres");
4578 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4579 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004580 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004581 (int)cm->seq_params.bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004582 cpi->source = &cpi->scaled_source;
4583 }
4584}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004585
Yue Chen92271e12019-07-09 16:28:02 -07004586static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
4587 MACROBLOCKD *xd, int use_restoration,
4588 int use_cdef) {
logangwf95c9162019-02-20 12:02:32 -08004589 if (use_restoration)
David Turnerc29e1a92018-12-06 14:10:14 +00004590 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004591
logangwf95c9162019-02-20 12:02:32 -08004592 if (use_cdef) {
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004593#if CONFIG_COLLECT_COMPONENT_TIMING
4594 start_timing(cpi, cdef_time);
4595#endif
Steinar Midtskogen59782122017-07-20 08:49:43 +02004596 // Find CDEF parameters
David Turnerc29e1a92018-12-06 14:10:14 +00004597 av1_cdef_search(&cm->cur_frame->buf, cpi->source, cm, xd,
Hui Sub93880a2019-06-27 18:29:30 -07004598 cpi->sf.cdef_pick_method, cpi->td.mb.rdmult);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004599
4600 // Apply the filter
David Turnerc29e1a92018-12-06 14:10:14 +00004601 av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004602#if CONFIG_COLLECT_COMPONENT_TIMING
4603 end_timing(cpi, cdef_time);
4604#endif
logangwf95c9162019-02-20 12:02:32 -08004605 } else {
4606 cm->cdef_info.cdef_bits = 0;
4607 cm->cdef_info.cdef_strengths[0] = 0;
4608 cm->cdef_info.nb_cdef_strengths = 1;
4609 cm->cdef_info.cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004610 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004611
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004612 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004613
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004614#if CONFIG_COLLECT_COMPONENT_TIMING
4615 start_timing(cpi, loop_restoration_time);
4616#endif
logangwf95c9162019-02-20 12:02:32 -08004617 if (use_restoration) {
David Turnerc29e1a92018-12-06 14:10:14 +00004618 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004619 av1_pick_filter_restoration(cpi->source, cpi);
4620 if (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) {
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304623 if (cpi->num_workers > 1)
David Turnerc29e1a92018-12-06 14:10:14 +00004624 av1_loop_restoration_filter_frame_mt(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304625 cpi->workers, cpi->num_workers,
4626 &cpi->lr_row_sync, &cpi->lr_ctxt);
4627 else
David Turnerc29e1a92018-12-06 14:10:14 +00004628 av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304629 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004630 }
logangwf95c9162019-02-20 12:02:32 -08004631 } else {
4632 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4633 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4634 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004635 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004636#if CONFIG_COLLECT_COMPONENT_TIMING
4637 end_timing(cpi, loop_restoration_time);
4638#endif
Fergus Simpsonbc189932017-05-16 17:02:39 -07004639}
4640
Yue Chen92271e12019-07-09 16:28:02 -07004641static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
4642 const int num_planes = av1_num_planes(cm);
4643 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4644
4645 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
4646 cm->coded_lossless && cm->all_lossless));
4647
4648 const int use_loopfilter = !cm->coded_lossless && !cm->large_scale_tile;
4649 const int use_cdef = cm->seq_params.enable_cdef && !cm->coded_lossless &&
4650 !cm->large_scale_tile;
4651 const int use_restoration = cm->seq_params.enable_restoration &&
4652 !cm->all_lossless && !cm->large_scale_tile;
4653
4654 struct loopfilter *lf = &cm->lf;
4655
4656#if CONFIG_COLLECT_COMPONENT_TIMING
4657 start_timing(cpi, loop_filter_time);
4658#endif
4659 if (use_loopfilter) {
4660 aom_clear_system_state();
4661 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
4662 } else {
4663 lf->filter_level[0] = 0;
4664 lf->filter_level[1] = 0;
4665 }
4666
4667 if (lf->filter_level[0] || lf->filter_level[1]) {
4668 if (cpi->num_workers > 1)
4669 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
4670#if CONFIG_LPF_MASK
4671 0,
4672#endif
4673 cpi->workers, cpi->num_workers,
4674 &cpi->lf_row_sync);
4675 else
4676 av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
4677#if CONFIG_LPF_MASK
4678 0,
4679#endif
4680 0, num_planes, 0);
4681 }
4682#if CONFIG_COLLECT_COMPONENT_TIMING
4683 end_timing(cpi, loop_filter_time);
4684#endif
4685
4686 cdef_restoration_frame(cpi, cm, xd, use_restoration, use_cdef);
4687}
4688
David Turnerf2b334c2018-12-13 13:00:55 +00004689static void fix_interp_filter(InterpFilter *const interp_filter,
4690 const FRAME_COUNTS *const counts) {
4691 if (*interp_filter == SWITCHABLE) {
4692 // Check to see if only one of the filters is actually used
4693 int count[SWITCHABLE_FILTERS] = { 0 };
4694 int num_filters_used = 0;
4695 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4696 for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
4697 count[i] += counts->switchable_interp[j][i];
4698 num_filters_used += (count[i] > 0);
4699 }
4700 if (num_filters_used == 1) {
4701 // Only one filter is used. So set the filter at frame level
4702 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4703 if (count[i]) {
4704 if (i == EIGHTTAP_REGULAR) *interp_filter = i;
4705 break;
4706 }
4707 }
4708 }
4709 }
4710}
4711
David Turner996b2c12018-12-07 15:52:30 +00004712static void finalize_encoded_frame(AV1_COMP *const cpi) {
4713 AV1_COMMON *const cm = &cpi->common;
David Turner99e990e2018-12-10 12:54:26 +00004714 CurrentFrame *const current_frame = &cm->current_frame;
David Turner996b2c12018-12-07 15:52:30 +00004715
David Turner99e990e2018-12-10 12:54:26 +00004716 if (!cm->seq_params.reduced_still_picture_hdr &&
4717 encode_show_existing_frame(cm)) {
4718 RefCntBuffer *const frame_to_show =
4719 cm->ref_frame_map[cpi->existing_fb_idx_to_show];
4720
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004721 if (frame_to_show == NULL) {
David Turner99e990e2018-12-10 12:54:26 +00004722 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4723 "Buffer does not contain a reconstructed frame");
4724 }
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004725 assert(frame_to_show->ref_count > 0);
David Turner99e990e2018-12-10 12:54:26 +00004726 assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
David Turner99e990e2018-12-10 12:54:26 +00004727 }
David Turner08f909c2018-12-18 13:29:14 +00004728
4729 if (!encode_show_existing_frame(cm) &&
4730 cm->seq_params.film_grain_params_present &&
4731 (cm->show_frame || cm->showable_frame)) {
4732 // Copy the current frame's film grain params to the its corresponding
4733 // RefCntBuffer slot.
4734 cm->cur_frame->film_grain_params = cm->film_grain_params;
4735
4736 // We must update the parameters if this is not an INTER_FRAME
4737 if (current_frame->frame_type != INTER_FRAME)
4738 cm->cur_frame->film_grain_params.update_parameters = 1;
4739
4740 // Iterate the random seed for the next frame.
4741 cm->film_grain_params.random_seed += 3381;
4742 if (cm->film_grain_params.random_seed == 0)
4743 cm->film_grain_params.random_seed = 7391;
4744 }
David Turnerf2b334c2018-12-13 13:00:55 +00004745
4746 // Initialise all tiles' contexts from the global frame context
4747 for (int tile_col = 0; tile_col < cm->tile_cols; tile_col++) {
4748 for (int tile_row = 0; tile_row < cm->tile_rows; tile_row++) {
4749 const int tile_idx = tile_row * cm->tile_cols + tile_col;
4750 cpi->tile_data[tile_idx].tctx = *cm->fc;
4751 }
4752 }
4753
4754 fix_interp_filter(&cm->interp_filter, cpi->td.counts);
David Turner996b2c12018-12-07 15:52:30 +00004755}
4756
Urvang Joshi57643372019-02-21 11:10:57 -08004757static int get_regulated_q_overshoot(AV1_COMP *const cpi, int q_low, int q_high,
4758 int top_index, int bottom_index) {
4759 const AV1_COMMON *const cm = &cpi->common;
4760 const RATE_CONTROL *const rc = &cpi->rc;
4761
4762 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4763
4764 int q_regulated =
4765 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4766 AOMMAX(q_high, top_index), cm->width, cm->height);
4767
4768 int retries = 0;
4769 while (q_regulated < q_low && retries < 10) {
4770 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4771 q_regulated =
4772 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4773 AOMMAX(q_high, top_index), cm->width, cm->height);
4774 retries++;
4775 }
4776 return q_regulated;
4777}
4778
4779static int get_regulated_q_undershoot(AV1_COMP *const cpi, int q_high,
4780 int top_index, int bottom_index) {
4781 const AV1_COMMON *const cm = &cpi->common;
4782 const RATE_CONTROL *const rc = &cpi->rc;
4783
4784 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4785 int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4786 top_index, cm->width, cm->height);
4787
4788 int retries = 0;
4789 while (q_regulated > q_high && retries < 10) {
4790 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4791 q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4792 top_index, cm->width, cm->height);
4793 retries++;
4794 }
4795 return q_regulated;
4796}
4797
David Turner2f3b5df2019-01-02 14:30:50 +00004798// Called after encode_with_recode_loop() has just encoded a frame and packed
4799// its bitstream. This function works out whether we under- or over-shot
4800// our bitrate target and adjusts q as appropriate. Also decides whether
4801// or not we should do another recode loop, indicated by *loop
Hui Suef139e12019-05-20 15:51:22 -07004802static void recode_loop_update_q(
4803 AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low,
4804 int *const q_high, const int top_index, const int bottom_index,
4805 int *const undershoot_seen, int *const overshoot_seen,
4806 int *const low_cr_seen, const int loop_at_this_size) {
David Turner2f3b5df2019-01-02 14:30:50 +00004807 AV1_COMMON *const cm = &cpi->common;
4808 RATE_CONTROL *const rc = &cpi->rc;
4809
Hui Suef139e12019-05-20 15:51:22 -07004810 const int min_cr = cpi->oxcf.min_cr;
4811 if (min_cr > 0) {
4812 aom_clear_system_state();
4813 const double compression_ratio =
4814 av1_get_compression_ratio(cm, rc->projected_frame_size >> 3);
4815 const double target_cr = min_cr / 100.0;
4816 if (compression_ratio < target_cr) {
4817 *low_cr_seen = 1;
4818 if (*q < rc->worst_quality) {
4819 const double cr_ratio = target_cr / compression_ratio;
4820 const int projected_q = AOMMAX(*q + 1, (int)(*q * cr_ratio * cr_ratio));
4821 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->worst_quality);
4822 *q_low = AOMMAX(*q, *q_low);
4823 *q_high = AOMMAX(*q, *q_high);
4824 *loop = 1;
4825 }
4826 }
4827 if (*low_cr_seen) return;
4828 }
4829
4830 if (cpi->oxcf.rc_mode == AOM_Q) return;
4831
Hui Su01dfe032019-09-11 14:10:42 -07004832 const int last_q = *q;
David Turner2f3b5df2019-01-02 14:30:50 +00004833 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
4834 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4835 &frame_under_shoot_limit,
4836 &frame_over_shoot_limit);
4837 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4838
Hui Su01dfe032019-09-11 14:10:42 -07004839 if (cm->current_frame.frame_type == KEY_FRAME && rc->this_key_frame_forced &&
4840 rc->projected_frame_size < rc->max_frame_bandwidth) {
David Turner2f3b5df2019-01-02 14:30:50 +00004841 int64_t kf_err;
Hui Su01dfe032019-09-11 14:10:42 -07004842 const int64_t high_err_target = cpi->ambient_err;
4843 const int64_t low_err_target = cpi->ambient_err >> 1;
David Turner2f3b5df2019-01-02 14:30:50 +00004844
Jerome Jiangfa1d1732019-08-06 10:31:20 -07004845#if CONFIG_AV1_HIGHBITDEPTH
David Turner2f3b5df2019-01-02 14:30:50 +00004846 if (cm->seq_params.use_highbitdepth) {
4847 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
4848 } else {
4849 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
4850 }
Jerome Jiangfa1d1732019-08-06 10:31:20 -07004851#else
4852 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
4853#endif
David Turner2f3b5df2019-01-02 14:30:50 +00004854 // Prevent possible divide by zero error below for perfect KF
4855 kf_err += !kf_err;
4856
4857 // The key frame is not good enough or we can afford
4858 // to make it better without undue risk of popping.
4859 if ((kf_err > high_err_target &&
4860 rc->projected_frame_size <= frame_over_shoot_limit) ||
4861 (kf_err > low_err_target &&
4862 rc->projected_frame_size <= frame_under_shoot_limit)) {
4863 // Lower q_high
Hui Su01dfe032019-09-11 14:10:42 -07004864 *q_high = AOMMAX(*q - 1, *q_low);
David Turner2f3b5df2019-01-02 14:30:50 +00004865
4866 // Adjust Q
4867 *q = (int)((*q * high_err_target) / kf_err);
4868 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
4869 } else if (kf_err < low_err_target &&
4870 rc->projected_frame_size >= frame_under_shoot_limit) {
4871 // The key frame is much better than the previous frame
4872 // Raise q_low
Hui Su01dfe032019-09-11 14:10:42 -07004873 *q_low = AOMMIN(*q + 1, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00004874
4875 // Adjust Q
4876 *q = (int)((*q * low_err_target) / kf_err);
4877 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
4878 }
4879
4880 // Clamp Q to upper and lower limits:
4881 *q = clamp(*q, *q_low, *q_high);
Hui Su01dfe032019-09-11 14:10:42 -07004882 *loop = (*q != last_q);
4883 return;
4884 }
David Turner2f3b5df2019-01-02 14:30:50 +00004885
Hui Su01dfe032019-09-11 14:10:42 -07004886 if (recode_loop_test(cpi, frame_over_shoot_limit, frame_under_shoot_limit, *q,
4887 AOMMAX(*q_high, top_index), bottom_index)) {
David Turner2f3b5df2019-01-02 14:30:50 +00004888 // Is the projected frame size out of range and are we allowed
4889 // to attempt to recode.
David Turner2f3b5df2019-01-02 14:30:50 +00004890
4891 // Frame size out of permitted range:
4892 // Update correction factor & compute new Q to try...
4893 // Frame is too large
4894 if (rc->projected_frame_size > rc->this_frame_target) {
4895 // Special case if the projected size is > the max allowed.
Urvang Joshi7344d3e2019-07-25 17:24:57 -07004896 if (*q == *q_high &&
4897 rc->projected_frame_size >= rc->max_frame_bandwidth) {
4898 const double q_val_high_current =
4899 av1_convert_qindex_to_q(*q_high, cm->seq_params.bit_depth);
4900 const double q_val_high_new =
4901 q_val_high_current *
4902 ((double)rc->projected_frame_size / rc->max_frame_bandwidth);
4903 *q_high = av1_find_qindex(q_val_high_new, cm->seq_params.bit_depth,
4904 rc->best_quality, rc->worst_quality);
4905 }
David Turner2f3b5df2019-01-02 14:30:50 +00004906
4907 // Raise Qlow as to at least the current value
Hui Su01dfe032019-09-11 14:10:42 -07004908 *q_low = AOMMIN(*q + 1, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00004909
Urvang Joshi57643372019-02-21 11:10:57 -08004910 if (*undershoot_seen || loop_at_this_size > 2 ||
4911 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00004912 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4913
4914 *q = (*q_high + *q_low + 1) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08004915 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
4916 const int q_mid = (*q_high + *q_low + 1) / 2;
4917 const int q_regulated = get_regulated_q_overshoot(
4918 cpi, *q_low, *q_high, top_index, bottom_index);
4919 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
4920 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
4921 *q = (q_mid + q_regulated + 1) / 2;
David Turner2f3b5df2019-01-02 14:30:50 +00004922 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08004923 *q = get_regulated_q_overshoot(cpi, *q_low, *q_high, top_index,
4924 bottom_index);
David Turner2f3b5df2019-01-02 14:30:50 +00004925 }
4926
4927 *overshoot_seen = 1;
4928 } else {
4929 // Frame is too small
Hui Su01dfe032019-09-11 14:10:42 -07004930 *q_high = AOMMAX(*q - 1, *q_low);
David Turner2f3b5df2019-01-02 14:30:50 +00004931
Urvang Joshi57643372019-02-21 11:10:57 -08004932 if (*overshoot_seen || loop_at_this_size > 2 ||
4933 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00004934 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4935 *q = (*q_high + *q_low) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08004936 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
4937 const int q_mid = (*q_high + *q_low) / 2;
4938 const int q_regulated =
4939 get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
4940 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
4941 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
4942 *q = (q_mid + q_regulated) / 2;
4943
4944 // Special case reset for qlow for constrained quality.
4945 // This should only trigger where there is very substantial
4946 // undershoot on a frame and the auto cq level is above
4947 // the user passsed in value.
4948 if (cpi->oxcf.rc_mode == AOM_CQ && q_regulated < *q_low) {
4949 *q_low = *q;
4950 }
David Turner2f3b5df2019-01-02 14:30:50 +00004951 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08004952 *q = get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
4953
David Turner2f3b5df2019-01-02 14:30:50 +00004954 // Special case reset for qlow for constrained quality.
4955 // This should only trigger where there is very substantial
4956 // undershoot on a frame and the auto cq level is above
4957 // the user passsed in value.
4958 if (cpi->oxcf.rc_mode == AOM_CQ && *q < *q_low) {
4959 *q_low = *q;
4960 }
David Turner2f3b5df2019-01-02 14:30:50 +00004961 }
4962
4963 *undershoot_seen = 1;
4964 }
4965
4966 // Clamp Q to upper and lower limits:
4967 *q = clamp(*q, *q_low, *q_high);
David Turner2f3b5df2019-01-02 14:30:50 +00004968 }
Hui Su01dfe032019-09-11 14:10:42 -07004969
4970 *loop = (*q != last_q);
David Turner2f3b5df2019-01-02 14:30:50 +00004971}
4972
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304973static int get_interp_filter_selected(const AV1_COMMON *const cm,
4974 MV_REFERENCE_FRAME ref,
4975 InterpFilter ifilter) {
4976 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
4977 if (buf == NULL) return 0;
4978 return buf->interp_filter_selected[ifilter];
4979}
4980
Satish Kumar Suman0389a492019-07-31 13:35:58 +05304981static uint16_t setup_interp_filter_search_mask(AV1_COMP *cpi) {
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304982 const AV1_COMMON *const cm = &cpi->common;
4983 int ref_total[REF_FRAMES] = { 0 };
Satish Kumar Suman0389a492019-07-31 13:35:58 +05304984 uint16_t mask = ALLOW_ALL_INTERP_FILT_MASK;
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304985
4986 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
Satish Kumar Suman0389a492019-07-31 13:35:58 +05304987 return mask;
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304988
4989 for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
4990 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
4991 ++ifilter) {
4992 ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter);
4993 }
4994 }
4995 int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] +
4996 ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] +
4997 ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]);
4998
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304999 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
5000 ++ifilter) {
5001 int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30;
5002 if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) {
5003 int filter_score =
5004 get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 +
5005 get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 +
5006 get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 +
5007 get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 +
5008 get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 +
5009 get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10;
Satish Kumar Suman0389a492019-07-31 13:35:58 +05305010 if (filter_score < ref_total_total) {
5011 DUAL_FILTER_TYPE filt_type = ifilter + SWITCHABLE_FILTERS * ifilter;
5012 reset_interp_filter_allowed_mask(&mask, filt_type);
5013 }
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305014 }
5015 }
5016 return mask;
5017}
5018
Tom Finegane4099e32018-01-23 12:01:51 -08005019static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005020 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005021 RATE_CONTROL *const rc = &cpi->rc;
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005022 const int allow_recode = (cpi->sf.recode_loop != DISALLOW_RECODE);
Hui Sua1d71842019-07-31 12:02:24 -07005023 // Must allow recode if minimum compression ratio is set.
5024 assert(IMPLIES(cpi->oxcf.min_cr > 0, allow_recode));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005025
5026 set_size_independent_vars(cpi);
Satish Kumar Suman829c1682019-07-31 11:30:07 +05305027 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
Yunqing Wangc3ab9272019-11-01 17:43:58 -07005028 cpi->interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07005029 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07005030
David Turnerdedd8ff2019-01-23 13:59:46 +00005031 av1_setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005032
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005033#if CONFIG_SUPERRES_IN_RECODE
5034 if (superres_in_recode_allowed(cpi) &&
5035 cm->superres_scale_denominator == SCALE_NUMERATOR) {
5036 // Superres won't be picked, so no need to try, as we will go through
5037 // another recode loop for full-resolution after this anyway.
5038 return -1;
5039 }
5040#endif // CONFIG_SUPERRES_IN_RECODE
5041
David Turner2f3b5df2019-01-02 14:30:50 +00005042 int top_index = 0, bottom_index = 0;
5043 int q = 0, q_low = 0, q_high = 0;
5044 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
5045 q_low = bottom_index;
5046 q_high = top_index;
5047
Yunqing Wang80a7af62019-08-22 16:07:44 -07005048 if (cpi->sf.tx_type_search.prune_tx_type_using_stats &&
5049 cm->current_frame.frame_type == KEY_FRAME) {
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005050 av1_copy(cpi->tx_type_probs, default_tx_type_probs);
5051
Yunqing Wangeb7c2182019-08-22 18:15:48 -07005052 int thr[2][2] = { { 15, 10 }, { 17, 10 } };
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005053 for (int f = 0; f < FRAME_UPDATE_TYPES; f++) {
Yunqing Wangeb7c2182019-08-22 18:15:48 -07005054 int kf_arf_update = (f == KF_UPDATE || f == ARF_UPDATE);
5055 cpi->tx_type_probs_thresh[f] =
5056 thr[cpi->sf.tx_type_search.prune_tx_type_using_stats - 1]
5057 [kf_arf_update];
Yunqing Wangdb70bf42019-08-19 09:28:11 -07005058 }
5059 }
5060
Yunqing Wang3ea314b2019-11-18 16:01:14 -08005061 if (!cpi->sf.disable_obmc && cpi->sf.prune_obmc_prob_thresh > 0 &&
Yunqing Wang5f74dc22019-10-29 10:35:20 -07005062 cm->current_frame.frame_type == KEY_FRAME) {
5063 av1_copy(cpi->obmc_probs, default_obmc_probs);
Yunqing Wang5f74dc22019-10-29 10:35:20 -07005064 }
5065
David Turner2f3b5df2019-01-02 14:30:50 +00005066 // Loop variables
5067 int loop_count = 0;
5068 int loop_at_this_size = 0;
5069 int loop = 0;
5070 int overshoot_seen = 0;
5071 int undershoot_seen = 0;
Hui Suef139e12019-05-20 15:51:22 -07005072 int low_cr_seen = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005073
5074#if CONFIG_COLLECT_COMPONENT_TIMING
5075 printf("\n Encoding a frame:");
5076#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005077 do {
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005078 loop = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005079 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005080
Urvang Joshif1fa6862018-01-08 16:39:33 -08005081 // if frame was scaled calculate global_motion_search again if already
5082 // done
David Turner2f3b5df2019-01-02 14:30:50 +00005083 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005084 if (cpi->source->y_crop_width != cm->width ||
David Turner2f3b5df2019-01-02 14:30:50 +00005085 cpi->source->y_crop_height != cm->height) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005086 cpi->global_motion_search_done = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00005087 }
5088 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005089 cpi->source =
5090 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
David Turner2f3b5df2019-01-02 14:30:50 +00005091 if (cpi->unscaled_last_source != NULL) {
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07005092 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
5093 &cpi->scaled_last_source);
David Turner2f3b5df2019-01-02 14:30:50 +00005094 }
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07005095
David Turner2f3b5df2019-01-02 14:30:50 +00005096 if (!frame_is_intra_only(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005097 if (loop_count > 0) {
5098 release_scaled_references(cpi);
5099 }
Cheng Chen46f30c72017-09-07 11:13:33 -07005100 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005101 }
Yaowu Xuf883b422016-08-30 14:01:10 -07005102 av1_set_quantizer(cm, q);
Fyodor Kyslov25daf5d2019-10-23 10:33:26 -07005103 if (cpi->oxcf.deltaq_mode != NO_DELTA_Q) av1_init_quantizer(cpi);
kyslov7b9d0d62018-12-21 11:12:26 -08005104
5105 av1_set_variance_partition_thresholds(cpi, q, 0);
5106
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08005107 // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
5108 // cm->current_frame.frame_number, cm->show_frame, q,
5109 // cm->current_frame.frame_type, cm->superres_scale_denominator);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005110
David Turner2f3b5df2019-01-02 14:30:50 +00005111 if (loop_count == 0) {
5112 setup_frame(cpi);
5113 } else if (get_primary_ref_frame_buf(cm) == NULL) {
5114 // Base q-index may have changed, so we need to assign proper default coef
5115 // probs before every iteration.
Yaowu Xuf883b422016-08-30 14:01:10 -07005116 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08005117 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00005118 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005119
Yaowu Xuc27fc142016-08-22 16:08:15 -07005120 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005121 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005122 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005123 av1_setup_in_frame_q_adj(cpi);
David Turner2f3b5df2019-01-02 14:30:50 +00005124 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !allow_recode) {
5125 suppress_active_map(cpi);
5126 av1_cyclic_refresh_setup(cpi);
5127 apply_active_map(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005128 }
David Turner2f3b5df2019-01-02 14:30:50 +00005129
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005130 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00005131 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005132 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
Jerome Jiangb23571d2019-10-23 14:15:55 -07005133 cm->seg.enabled = cm->prev_frame->seg.enabled;
David Barker11c93562018-06-05 12:00:07 +01005134 } else {
Yaowu Xu7e450882019-04-30 15:09:18 -07005135 av1_calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07005136 }
David Barkercab37552018-03-21 11:56:24 +00005137 } else {
5138 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00005139 }
David Barkercab37552018-03-21 11:56:24 +00005140 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Jerome Jiangb23571d2019-10-23 14:15:55 -07005141 cm->cur_frame->seg.enabled = cm->seg.enabled;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005142
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005143#if CONFIG_COLLECT_COMPONENT_TIMING
5144 start_timing(cpi, av1_encode_frame_time);
5145#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005146 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07005147 av1_encode_frame(cpi);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005148#if CONFIG_COLLECT_COMPONENT_TIMING
5149 end_timing(cpi, av1_encode_frame_time);
5150#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005151
Yaowu Xuf883b422016-08-30 14:01:10 -07005152 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005153
5154 // Dummy pack of the bitstream using up to date stats to get an
5155 // accurate estimate of output frame size to determine if we need
5156 // to recode.
Hui Su7e3eeaa2019-09-11 15:50:41 -07005157 const int do_dummy_pack = (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
5158 cpi->oxcf.rc_mode != AOM_Q) ||
5159 cpi->oxcf.min_cr > 0;
Hui Sua1d71842019-07-31 12:02:24 -07005160 if (do_dummy_pack) {
David Turner996b2c12018-12-07 15:52:30 +00005161 finalize_encoded_frame(cpi);
David Turner35cba132018-12-10 15:48:15 +00005162 int largest_tile_id = 0; // Output from bitstream: unused here
5163 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08005164 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005165
5166 rc->projected_frame_size = (int)(*size) << 3;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005167 }
5168
Hui Suef139e12019-05-20 15:51:22 -07005169 if (allow_recode) {
David Turner2f3b5df2019-01-02 14:30:50 +00005170 // Update q and decide whether to do a recode loop
5171 recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
5172 bottom_index, &undershoot_seen, &overshoot_seen,
Hui Suef139e12019-05-20 15:51:22 -07005173 &low_cr_seen, loop_at_this_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005174 }
5175
5176 // Special case for overlay frame.
Hui Su7e3eeaa2019-09-11 15:50:41 -07005177 if (loop && rc->is_src_frame_alt_ref &&
5178 rc->projected_frame_size < rc->max_frame_bandwidth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005179 loop = 0;
Hui Su7e3eeaa2019-09-11 15:50:41 -07005180 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005181
David Turner2f3b5df2019-01-02 14:30:50 +00005182 if (allow_recode && !cpi->sf.gm_disable_recode &&
5183 recode_loop_test_global_motion(cpi)) {
5184 loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005185 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005186
Jerome Jiangd413f4a2019-09-23 14:53:03 -07005187#if !CONFIG_REALTIME_ONLY
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005188 if (cpi->tpl_model_pass == 1) {
5189 assert(cpi->oxcf.enable_tpl_model == 2);
Debargha Mukherjee347c64d2019-05-08 13:53:46 -07005190 av1_tpl_setup_forward_stats(cpi);
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005191 cpi->tpl_model_pass = 0;
5192 loop = 1;
5193 }
Jerome Jiangd413f4a2019-09-23 14:53:03 -07005194#endif
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005195
Yaowu Xuc27fc142016-08-22 16:08:15 -07005196 if (loop) {
5197 ++loop_count;
5198 ++loop_at_this_size;
5199
5200#if CONFIG_INTERNAL_STATS
5201 ++cpi->tot_recode_hits;
5202#endif
5203 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005204#if CONFIG_COLLECT_COMPONENT_TIMING
5205 if (loop) printf("\n Recoding:");
5206#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005207 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08005208
Marco Paniconi574e59e2019-10-23 15:47:28 -07005209 // Update some stats from cyclic refresh.
5210 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
5211 !frame_is_intra_only(cm))
5212 av1_cyclic_refresh_postencode(cpi);
5213
Tom Finegane4099e32018-01-23 12:01:51 -08005214 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005215}
5216
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005217static int encode_with_recode_loop_and_filter(AV1_COMP *cpi, size_t *size,
5218 uint8_t *dest, int64_t *sse,
5219 int64_t *rate,
5220 int *largest_tile_id) {
5221#if CONFIG_COLLECT_COMPONENT_TIMING
5222 start_timing(cpi, encode_with_recode_loop_time);
5223#endif
5224 int err = encode_with_recode_loop(cpi, size, dest);
5225#if CONFIG_COLLECT_COMPONENT_TIMING
5226 end_timing(cpi, encode_with_recode_loop_time);
5227#endif
5228 if (err != AOM_CODEC_OK) {
5229 if (err == -1) {
5230 // special case as described in encode_with_recode_loop().
5231 // Encoding was skipped.
5232 err = AOM_CODEC_OK;
5233 if (sse != NULL) *sse = INT64_MAX;
5234 if (rate != NULL) *rate = INT64_MAX;
5235 *largest_tile_id = 0;
5236 }
5237 return err;
5238 }
5239
5240#ifdef OUTPUT_YUV_SKINMAP
5241 if (cpi->common.current_frame.frame_number > 1) {
5242 av1_compute_skin_map(cpi, yuv_skinmap_file);
5243 }
5244#endif // OUTPUT_YUV_SKINMAP
5245
5246 AV1_COMMON *const cm = &cpi->common;
5247 SequenceHeader *const seq_params = &cm->seq_params;
5248
5249 // Special case code to reduce pulsing when key frames are forced at a
5250 // fixed interval. Note the reconstruction error if it is the frame before
5251 // the force key frame
5252 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005253#if CONFIG_AV1_HIGHBITDEPTH
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005254 if (seq_params->use_highbitdepth) {
5255 cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
5256 } else {
5257 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5258 }
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005259#else
5260 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5261#endif
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005262 }
5263
5264 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
5265 cm->cur_frame->buf.transfer_characteristics =
5266 seq_params->transfer_characteristics;
5267 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
5268 cm->cur_frame->buf.monochrome = seq_params->monochrome;
5269 cm->cur_frame->buf.chroma_sample_position =
5270 seq_params->chroma_sample_position;
5271 cm->cur_frame->buf.color_range = seq_params->color_range;
5272 cm->cur_frame->buf.render_width = cm->render_width;
5273 cm->cur_frame->buf.render_height = cm->render_height;
5274
5275 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5276 // off.
5277
5278 // Pick the loop filter level for the frame.
5279 if (!cm->allow_intrabc) {
5280 loopfilter_frame(cpi, cm);
5281 } else {
5282 cm->lf.filter_level[0] = 0;
5283 cm->lf.filter_level[1] = 0;
5284 cm->cdef_info.cdef_bits = 0;
5285 cm->cdef_info.cdef_strengths[0] = 0;
5286 cm->cdef_info.nb_cdef_strengths = 1;
5287 cm->cdef_info.cdef_uv_strengths[0] = 0;
5288 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5289 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5290 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
5291 }
5292
5293 // TODO(debargha): Fix mv search range on encoder side
5294 // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5295 aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5296
5297#ifdef OUTPUT_YUV_REC
5298 aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
5299#endif
5300
5301 finalize_encoded_frame(cpi);
5302 // Build the bitstream
5303#if CONFIG_COLLECT_COMPONENT_TIMING
5304 start_timing(cpi, av1_pack_bitstream_final_time);
5305#endif
5306 if (av1_pack_bitstream(cpi, dest, size, largest_tile_id) != AOM_CODEC_OK)
5307 return AOM_CODEC_ERROR;
5308#if CONFIG_COLLECT_COMPONENT_TIMING
5309 end_timing(cpi, av1_pack_bitstream_final_time);
5310#endif
5311
5312 // Compute sse and rate.
5313 if (sse != NULL) {
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005314#if CONFIG_AV1_HIGHBITDEPTH
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005315 *sse = (seq_params->use_highbitdepth)
5316 ? aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf)
5317 : aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005318#else
5319 *sse = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5320#endif
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005321 }
5322 if (rate != NULL) {
5323 const int64_t bits = (*size << 3);
5324 *rate = (bits << 5); // To match scale.
5325 }
5326 return AOM_CODEC_OK;
5327}
5328
5329#if CONFIG_SUPERRES_IN_RECODE
5330
5331static void save_cur_buf(AV1_COMP *cpi) {
5332 CODING_CONTEXT *const cc = &cpi->coding_context;
5333 AV1_COMMON *cm = &cpi->common;
5334 const YV12_BUFFER_CONFIG *ybf = &cm->cur_frame->buf;
5335 memset(&cc->copy_buffer, 0, sizeof(cc->copy_buffer));
5336 if (aom_alloc_frame_buffer(&cc->copy_buffer, ybf->y_crop_width,
5337 ybf->y_crop_height, ybf->subsampling_x,
5338 ybf->subsampling_y,
5339 ybf->flags & YV12_FLAG_HIGHBITDEPTH, ybf->border,
5340 cm->byte_alignment) != AOM_CODEC_OK) {
5341 aom_internal_error(
5342 &cm->error, AOM_CODEC_MEM_ERROR,
5343 "Failed to allocate copy buffer for saving coding context");
5344 }
5345 aom_yv12_copy_frame(ybf, &cc->copy_buffer, av1_num_planes(cm));
5346}
5347
5348// Coding context that only needs to be saved when recode loop includes
5349// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5350// restoraton).
5351static void save_extra_coding_context(AV1_COMP *cpi) {
5352 CODING_CONTEXT *const cc = &cpi->coding_context;
5353 AV1_COMMON *cm = &cpi->common;
5354
5355 cc->lf = cm->lf;
5356 cc->cdef_info = cm->cdef_info;
5357 cc->rc = cpi->rc;
5358}
5359
5360static void save_all_coding_context(AV1_COMP *cpi) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005361 save_cur_buf(cpi);
5362 save_extra_coding_context(cpi);
5363 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5364}
5365
5366static void restore_cur_buf(AV1_COMP *cpi) {
5367 CODING_CONTEXT *const cc = &cpi->coding_context;
5368 AV1_COMMON *cm = &cpi->common;
5369 aom_yv12_copy_frame(&cc->copy_buffer, &cm->cur_frame->buf,
5370 av1_num_planes(cm));
5371}
5372
5373// Coding context that only needs to be restored when recode loop includes
5374// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5375// restoraton).
5376static void restore_extra_coding_context(AV1_COMP *cpi) {
5377 CODING_CONTEXT *const cc = &cpi->coding_context;
5378 AV1_COMMON *cm = &cpi->common;
5379 cm->lf = cc->lf;
5380 cm->cdef_info = cc->cdef_info;
5381 cpi->rc = cc->rc;
5382}
5383
5384static void restore_all_coding_context(AV1_COMP *cpi) {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005385 restore_cur_buf(cpi);
5386 restore_extra_coding_context(cpi);
5387 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5388}
5389
5390static int encode_with_and_without_superres(AV1_COMP *cpi, size_t *size,
5391 uint8_t *dest,
5392 int *largest_tile_id) {
5393 const AV1_COMMON *const cm = &cpi->common;
5394 AV1EncoderConfig *const oxcf = &cpi->oxcf;
5395 assert(cm->seq_params.enable_superres);
5396 assert(superres_in_recode_allowed(cpi));
5397 aom_codec_err_t err = AOM_CODEC_OK;
5398 save_all_coding_context(cpi);
5399
5400 // Encode with superres.
Urvang Joshibc82d382019-11-01 17:59:20 -07005401#if SUPERRES_RECODE_ALL_RATIOS
5402 int64_t superres_sses[SCALE_NUMERATOR];
5403 int64_t superres_rates[SCALE_NUMERATOR];
5404 int superres_largest_tile_ids[SCALE_NUMERATOR];
5405 // Use superres for Key-frames and Alt-ref frames only.
5406 const GF_GROUP *const gf_group = &cpi->gf_group;
5407 if (gf_group->update_type[gf_group->index] != OVERLAY_UPDATE &&
5408 gf_group->update_type[gf_group->index] != INTNL_OVERLAY_UPDATE) {
5409 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
5410 ++denom) {
5411 oxcf->superres_scale_denominator = denom;
5412 oxcf->superres_kf_scale_denominator = denom;
5413 const int this_index = denom - (SCALE_NUMERATOR + 1);
5414 err = encode_with_recode_loop_and_filter(
5415 cpi, size, dest, &superres_sses[this_index],
5416 &superres_rates[this_index], &superres_largest_tile_ids[this_index]);
5417 if (err != AOM_CODEC_OK) return err;
5418 restore_all_coding_context(cpi);
5419 }
5420 // Reset.
5421 oxcf->superres_scale_denominator = SCALE_NUMERATOR;
5422 oxcf->superres_kf_scale_denominator = SCALE_NUMERATOR;
5423 } else {
5424 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
5425 ++denom) {
5426 const int this_index = denom - (SCALE_NUMERATOR + 1);
5427 superres_sses[this_index] = INT64_MAX;
5428 superres_rates[this_index] = INT64_MAX;
5429 }
5430 }
5431#else
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005432 int64_t sse1 = INT64_MAX;
5433 int64_t rate1 = INT64_MAX;
5434 int largest_tile_id1;
5435 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse1, &rate1,
5436 &largest_tile_id1);
5437 if (err != AOM_CODEC_OK) return err;
Urvang Joshibc82d382019-11-01 17:59:20 -07005438 restore_all_coding_context(cpi);
5439#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005440
5441 // Encode without superres.
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005442 int64_t sse2 = INT64_MAX;
5443 int64_t rate2 = INT64_MAX;
5444 int largest_tile_id2;
5445 oxcf->superres_mode = SUPERRES_NONE; // To force full-res.
5446 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
5447 &largest_tile_id2);
5448 oxcf->superres_mode = SUPERRES_AUTO; // Reset.
5449 if (err != AOM_CODEC_OK) return err;
5450
5451 // Note: Both use common rdmult based on base qindex of fullres.
5452 const int64_t rdmult =
5453 av1_compute_rd_mult_based_on_qindex(cpi, cm->base_qindex);
Urvang Joshibc82d382019-11-01 17:59:20 -07005454
5455#if SUPERRES_RECODE_ALL_RATIOS
5456 // Find the best rdcost among all superres denoms.
5457 double proj_rdcost1 = DBL_MAX;
5458 int64_t sse1 = INT64_MAX;
5459 int64_t rate1 = INT64_MAX;
5460 int largest_tile_id1 = 0;
5461 (void)sse1;
5462 (void)rate1;
5463 (void)largest_tile_id1;
5464 int best_denom = -1;
5465 for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR; ++denom) {
5466 const int this_index = denom - (SCALE_NUMERATOR + 1);
5467 const int64_t this_sse = superres_sses[this_index];
5468 const int64_t this_rate = superres_rates[this_index];
5469 const int this_largest_tile_id = superres_largest_tile_ids[this_index];
5470 const double this_rdcost = RDCOST_DBL(rdmult, this_rate, this_sse);
5471 if (this_rdcost < proj_rdcost1) {
5472 sse1 = this_sse;
5473 rate1 = this_rate;
5474 largest_tile_id1 = this_largest_tile_id;
5475 proj_rdcost1 = this_rdcost;
5476 best_denom = denom;
5477 }
5478 }
5479#else
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005480 const double proj_rdcost1 = RDCOST_DBL(rdmult, rate1, sse1);
Urvang Joshibc82d382019-11-01 17:59:20 -07005481#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005482 const double proj_rdcost2 = RDCOST_DBL(rdmult, rate2, sse2);
5483
5484 // Re-encode with superres if it's better.
5485 if (proj_rdcost1 < proj_rdcost2) {
5486 restore_all_coding_context(cpi);
5487 // TODO(urvang): We should avoid rerunning the recode loop by saving
5488 // previous output+state, or running encode only for the selected 'q' in
5489 // previous step.
Urvang Joshibc82d382019-11-01 17:59:20 -07005490#if SUPERRES_RECODE_ALL_RATIOS
5491 // Again, temporarily force the best denom.
5492 oxcf->superres_scale_denominator = best_denom;
5493 oxcf->superres_kf_scale_denominator = best_denom;
5494#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005495 int64_t sse3 = INT64_MAX;
5496 int64_t rate3 = INT64_MAX;
5497 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
5498 largest_tile_id);
5499 assert(sse1 == sse3);
5500 assert(rate1 == rate3);
5501 assert(largest_tile_id1 == *largest_tile_id);
Urvang Joshibc82d382019-11-01 17:59:20 -07005502#if SUPERRES_RECODE_ALL_RATIOS
5503 // Reset.
5504 oxcf->superres_scale_denominator = SCALE_NUMERATOR;
5505 oxcf->superres_kf_scale_denominator = SCALE_NUMERATOR;
5506#endif // SUPERRES_RECODE_ALL_RATIOS
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005507 } else {
5508 *largest_tile_id = largest_tile_id2;
5509 }
5510
5511 return err;
5512}
5513#endif // CONFIG_SUPERRES_IN_RECODE
5514
Yaowu Xuc27fc142016-08-22 16:08:15 -07005515#define DUMP_RECON_FRAMES 0
5516
5517#if DUMP_RECON_FRAMES == 1
5518// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07005519static void dump_filtered_recon_frames(AV1_COMP *cpi) {
5520 AV1_COMMON *const cm = &cpi->common;
David Turnerd2a592e2018-11-16 14:59:31 +00005521 const CurrentFrame *const current_frame = &cm->current_frame;
David Turnerc29e1a92018-12-06 14:10:14 +00005522 const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005523
Zoe Liub4f31032017-11-03 23:48:35 -07005524 if (recon_buf == NULL) {
David Turnerd2a592e2018-11-16 14:59:31 +00005525 printf("Frame %d is not ready.\n", current_frame->frame_number);
Zoe Liub4f31032017-11-03 23:48:35 -07005526 return;
5527 }
5528
Zoe Liu27deb382018-03-27 15:13:56 -07005529 static const int flag_list[REF_FRAMES] = { 0,
5530 AOM_LAST_FLAG,
5531 AOM_LAST2_FLAG,
5532 AOM_LAST3_FLAG,
5533 AOM_GOLD_FLAG,
5534 AOM_BWD_FLAG,
5535 AOM_ALT2_FLAG,
5536 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07005537 printf(
5538 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
5539 "show_existing_frame=%d) "
5540 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
David Turnerd2a592e2018-11-16 14:59:31 +00005541 current_frame->frame_number, current_frame->order_hint, cm->show_frame,
Zoe Liub4f31032017-11-03 23:48:35 -07005542 cm->show_existing_frame);
5543 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00005544 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
5545 const int ref_offset = buf != NULL ? (int)buf->order_hint : -1;
Urvang Joshib6f17672019-03-05 11:51:02 -08005546 printf(" %d(%c)", ref_offset,
5547 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N');
Zoe Liub4f31032017-11-03 23:48:35 -07005548 }
5549 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07005550
5551 if (!cm->show_frame) {
5552 printf("Frame %d is a no show frame, so no image dump.\n",
David Turnerd2a592e2018-11-16 14:59:31 +00005553 current_frame->frame_number);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005554 return;
5555 }
5556
Zoe Liub4f31032017-11-03 23:48:35 -07005557 int h;
5558 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
5559 FILE *f_recon = NULL;
5560
David Turnerd2a592e2018-11-16 14:59:31 +00005561 if (current_frame->frame_number == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005562 if ((f_recon = fopen(file_name, "wb")) == NULL) {
5563 printf("Unable to open file %s to write.\n", file_name);
5564 return;
5565 }
5566 } else {
5567 if ((f_recon = fopen(file_name, "ab")) == NULL) {
5568 printf("Unable to open file %s to append.\n", file_name);
5569 return;
5570 }
5571 }
5572 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07005573 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
5574 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
Urvang Joshi7a890232019-03-22 17:00:31 -07005575 "refresh_alt_ref_frame=%d, "
Zoe Liuf40a9572017-10-13 12:37:19 -07005576 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Sarah Parkere1b22012019-06-06 16:35:25 -07005577 current_frame->frame_number, cpi->gf_group.index,
5578 cpi->gf_group.update_type[cpi->gf_group.index], current_frame->order_hint,
5579 cm->show_frame, cm->show_existing_frame, cpi->rc.source_alt_ref_active,
5580 cpi->refresh_alt_ref_frame, recon_buf->y_stride, recon_buf->uv_stride,
5581 cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07005582#if 0
5583 int ref_frame;
5584 printf("get_ref_frame_map_idx: [");
5585 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
David Turnera21966b2018-12-05 14:48:49 +00005586 printf(" %d", get_ref_frame_map_idx(cm, ref_frame));
Zoe Liue9b15e22017-07-19 15:53:01 -07005587 printf(" ]\n");
Zoe Liue9b15e22017-07-19 15:53:01 -07005588#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07005589
5590 // --- Y ---
5591 for (h = 0; h < cm->height; ++h) {
5592 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
5593 f_recon);
5594 }
5595 // --- U ---
5596 for (h = 0; h < (cm->height >> 1); ++h) {
5597 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5598 f_recon);
5599 }
5600 // --- V ---
5601 for (h = 0; h < (cm->height >> 1); ++h) {
5602 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5603 f_recon);
5604 }
5605
5606 fclose(f_recon);
5607}
5608#endif // DUMP_RECON_FRAMES
5609
David Turnerefed6372019-01-11 15:14:11 +00005610static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
5611 const YV12_BUFFER_CONFIG *last_picture,
5612 hash_table *last_hash_table) {
5613 aom_clear_system_state();
5614 // check use hash ME
5615 int k;
David Turnerefed6372019-01-11 15:14:11 +00005616
Ranjit Kumar Tulabandu9f26c992019-10-24 12:24:52 +05305617 const int block_size = FORCE_INT_MV_DECISION_BLOCK_SIZE;
David Turnerefed6372019-01-11 15:14:11 +00005618 const double threshold_current = 0.8;
5619 const double threshold_average = 0.95;
5620 const int max_history_size = 32;
5621 int T = 0; // total block
5622 int C = 0; // match with collocated block
5623 int S = 0; // smooth region but not match with collocated block
5624 int M = 0; // match with other block
5625
5626 const int pic_width = cur_picture->y_width;
5627 const int pic_height = cur_picture->y_height;
5628 for (int i = 0; i + block_size <= pic_height; i += block_size) {
5629 for (int j = 0; j + block_size <= pic_width; j += block_size) {
5630 const int x_pos = j;
5631 const int y_pos = i;
5632 int match = 1;
5633 T++;
5634
5635 // check whether collocated block match with current
5636 uint8_t *p_cur = cur_picture->y_buffer;
5637 uint8_t *p_ref = last_picture->y_buffer;
5638 int stride_cur = cur_picture->y_stride;
5639 int stride_ref = last_picture->y_stride;
5640 p_cur += (y_pos * stride_cur + x_pos);
5641 p_ref += (y_pos * stride_ref + x_pos);
5642
5643 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
5644 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
5645 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
5646 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5647 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5648 if (p16_cur[tmpX] != p16_ref[tmpX]) {
5649 match = 0;
5650 }
5651 }
5652 p16_cur += stride_cur;
5653 p16_ref += stride_ref;
5654 }
5655 } else {
5656 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5657 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5658 if (p_cur[tmpX] != p_ref[tmpX]) {
5659 match = 0;
5660 }
5661 }
5662 p_cur += stride_cur;
5663 p_ref += stride_ref;
5664 }
5665 }
5666
5667 if (match) {
5668 C++;
5669 continue;
5670 }
5671
5672 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
5673 y_pos) ||
5674 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
5675 S++;
5676 continue;
5677 }
Remyaa14a4582019-11-05 13:17:36 +05305678 if (av1_use_hash_me(cpi)) {
5679 uint32_t hash_value_1;
5680 uint32_t hash_value_2;
5681 av1_get_block_hash_value(
5682 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
5683 block_size, &hash_value_1, &hash_value_2,
5684 (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH), &cpi->td.mb);
5685 // Hashing does not work for highbitdepth currently.
5686 // TODO(Roger): Make it work for highbitdepth.
David Turnerefed6372019-01-11 15:14:11 +00005687 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
5688 M++;
5689 }
5690 }
5691 }
5692 }
5693
5694 assert(T > 0);
5695 double csm_rate = ((double)(C + S + M)) / ((double)(T));
5696 double m_rate = ((double)(M)) / ((double)(T));
5697
5698 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
5699 cpi->m_rate_array[cpi->rate_index] = m_rate;
5700
5701 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
5702 cpi->rate_size++;
5703 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
5704
5705 if (csm_rate < threshold_current) {
5706 return 0;
5707 }
5708
5709 if (C == T) {
5710 return 1;
5711 }
5712
5713 double csm_average = 0.0;
5714 double m_average = 0.0;
5715
5716 for (k = 0; k < cpi->rate_size; k++) {
5717 csm_average += cpi->csm_rate_array[k];
5718 m_average += cpi->m_rate_array[k];
5719 }
5720 csm_average /= cpi->rate_size;
5721 m_average /= cpi->rate_size;
5722
5723 if (csm_average < threshold_average) {
5724 return 0;
5725 }
5726
5727 if (M > (T - C - S) / 3) {
5728 return 1;
5729 }
5730
5731 if (csm_rate > 0.99 && m_rate > 0.01) {
5732 return 1;
5733 }
5734
5735 if (csm_average + m_average > 1.01) {
5736 return 1;
5737 }
5738
5739 return 0;
5740}
5741
David Turner73245762019-02-11 16:42:34 +00005742// Refresh reference frame buffers according to refresh_frame_flags.
5743static void refresh_reference_frames(AV1_COMP *cpi) {
5744 AV1_COMMON *const cm = &cpi->common;
5745 // All buffers are refreshed for shown keyframes and S-frames.
5746
5747 for (int ref_frame = 0; ref_frame < REF_FRAMES; ref_frame++) {
5748 if (((cm->current_frame.refresh_frame_flags >> ref_frame) & 1) == 1) {
5749 assign_frame_buffer_p(&cm->ref_frame_map[ref_frame], cm->cur_frame);
5750 }
5751 }
5752}
5753
sdengc23c7f12019-06-11 16:56:50 -07005754static void set_mb_ssim_rdmult_scaling(AV1_COMP *cpi) {
5755 AV1_COMMON *cm = &cpi->common;
5756 ThreadData *td = &cpi->td;
5757 MACROBLOCK *x = &td->mb;
5758 MACROBLOCKD *xd = &x->e_mbd;
5759 uint8_t *y_buffer = cpi->source->y_buffer;
5760 const int y_stride = cpi->source->y_stride;
5761 const int block_size = BLOCK_16X16;
5762
5763 const int num_mi_w = mi_size_wide[block_size];
5764 const int num_mi_h = mi_size_high[block_size];
5765 const int num_cols = (cm->mi_cols + num_mi_w - 1) / num_mi_w;
5766 const int num_rows = (cm->mi_rows + num_mi_h - 1) / num_mi_h;
5767 double log_sum = 0.0;
5768 int row, col;
5769 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
5770
sdengc23c7f12019-06-11 16:56:50 -07005771 // Loop through each 16x16 block.
5772 for (row = 0; row < num_rows; ++row) {
5773 for (col = 0; col < num_cols; ++col) {
5774 int mi_row, mi_col;
5775 double var = 0.0, num_of_var = 0.0;
5776 const int index = row * num_cols + col;
5777
5778 // Loop through each 8x8 block.
5779 for (mi_row = row * num_mi_h;
5780 mi_row < cm->mi_rows && mi_row < (row + 1) * num_mi_h; mi_row += 2) {
5781 for (mi_col = col * num_mi_w;
5782 mi_col < cm->mi_cols && mi_col < (col + 1) * num_mi_w;
5783 mi_col += 2) {
5784 struct buf_2d buf;
5785 const int row_offset_y = mi_row << 2;
5786 const int col_offset_y = mi_col << 2;
5787
5788 buf.buf = y_buffer + row_offset_y * y_stride + col_offset_y;
5789 buf.stride = y_stride;
5790
5791 if (use_hbd) {
5792 var += av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8,
5793 xd->bd);
5794 } else {
5795 var += av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8);
5796 }
5797
5798 num_of_var += 1.0;
5799 }
5800 }
sdeng32185d12019-06-19 14:47:09 -07005801 var = var / num_of_var;
sdengbdc0cd22019-11-11 15:09:34 -08005802
5803 // Curve fitting with an exponential model on all 16x16 blocks from the
5804 // midres dataset.
5805 var = 67.035434 * (1 - exp(-0.0021489 * var)) + 17.492222;
sdengc23c7f12019-06-11 16:56:50 -07005806 cpi->ssim_rdmult_scaling_factors[index] = var;
5807 log_sum += log(var);
5808 }
5809 }
5810 log_sum = exp(log_sum / (double)(num_rows * num_cols));
5811
5812 for (row = 0; row < num_rows; ++row) {
5813 for (col = 0; col < num_cols; ++col) {
5814 const int index = row * num_cols + col;
5815 cpi->ssim_rdmult_scaling_factors[index] /= log_sum;
5816 }
5817 }
5818
5819 (void)xd;
5820}
5821
chiyotsaifc1404d2019-08-08 12:09:12 -07005822#if CONFIG_DEBUG
5823static int hash_me_has_at_most_two_refs(RefCntBuffer *frame_bufs) {
5824 int total_count = 0;
5825 for (int frame_idx = 0; frame_idx < FRAME_BUFFERS; ++frame_idx) {
5826 if (frame_bufs[frame_idx].hash_table.has_content > 1) {
5827 return 0;
5828 }
5829 total_count += frame_bufs[frame_idx].hash_table.has_content;
5830 }
5831
5832 return total_count <= 2;
5833}
5834#endif
5835
David Turner73245762019-02-11 16:42:34 +00005836static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
5837 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005838 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005839 SequenceHeader *const seq_params = &cm->seq_params;
David Turnerd2a592e2018-11-16 14:59:31 +00005840 CurrentFrame *const current_frame = &cm->current_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07005841 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005842 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01005843
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005844#if CONFIG_COLLECT_COMPONENT_TIMING
5845 start_timing(cpi, encode_frame_to_data_rate_time);
5846#endif
5847
Fangwen Fu8d164de2016-12-14 13:40:54 -08005848 // frame type has been decided outside of this function call
David Turnerd2a592e2018-11-16 14:59:31 +00005849 cm->cur_frame->frame_type = current_frame->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07005850
Yunqing Wang9612d552018-05-15 14:58:30 -07005851 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
5852 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wang9612d552018-05-15 14:58:30 -07005853
sarahparker21dbca42018-03-30 17:43:44 -07005854 cm->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
Yunqing Wangd48fb162018-06-15 10:55:28 -07005855 // cm->allow_ref_frame_mvs needs to be written into the frame header while
5856 // cm->large_scale_tile is 1, therefore, "cm->large_scale_tile=1" case is
5857 // separated from frame_might_allow_ref_frame_mvs().
5858 cm->allow_ref_frame_mvs &= !cm->large_scale_tile;
5859
Debargha Mukherjee1d7217e2018-03-26 13:32:13 -07005860 cm->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07005861 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005862
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305863 cm->last_frame_type = current_frame->frame_type;
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305864
Sarah Parker33005522018-07-27 14:46:25 -07005865 if (encode_show_existing_frame(cm)) {
David Turner996b2c12018-12-07 15:52:30 +00005866 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005867 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00005868 int largest_tile_id = 0; // Output from bitstream: unused here
5869 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08005870 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005871
David Turner90311862018-11-29 13:34:36 +00005872 if (seq_params->frame_id_numbers_present_flag &&
5873 current_frame->frame_type == KEY_FRAME) {
5874 // Displaying a forward key-frame, so reset the ref buffer IDs
5875 int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
5876 for (int i = 0; i < REF_FRAMES; i++)
5877 cm->ref_frame_id[i] = display_frame_id;
5878 }
5879
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005880 cpi->seq_params_locked = 1;
5881
Yaowu Xuc27fc142016-08-22 16:08:15 -07005882#if DUMP_RECON_FRAMES == 1
5883 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
5884 dump_filtered_recon_frames(cpi);
5885#endif // DUMP_RECON_FRAMES
5886
David Turner73245762019-02-11 16:42:34 +00005887 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
5888 // for the purpose to verify no mismatch between encoder and decoder.
5889 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005890
David Turner73245762019-02-11 16:42:34 +00005891 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005892
Yaowu Xuc27fc142016-08-22 16:08:15 -07005893 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
5894 // to do post-encoding update accordingly.
5895 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005896 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07005897 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005898 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005899
David Turnerd2a592e2018-11-16 14:59:31 +00005900 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005901
Tom Finegane4099e32018-01-23 12:01:51 -08005902 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005903 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005904
David Turnerefed6372019-01-11 15:14:11 +00005905 // Work out whether to force_integer_mv this frame
5906 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools &&
5907 !frame_is_intra_only(cm)) {
5908 if (cpi->common.seq_params.force_integer_mv == 2) {
5909 // Adaptive mode: see what previous frame encoded did
5910 if (cpi->unscaled_last_source != NULL) {
5911 cm->cur_frame_force_integer_mv =
5912 is_integer_mv(cpi, cpi->source, cpi->unscaled_last_source,
5913 cpi->previous_hash_table);
5914 } else {
5915 cpi->common.cur_frame_force_integer_mv = 0;
5916 }
5917 } else {
5918 cpi->common.cur_frame_force_integer_mv =
5919 cpi->common.seq_params.force_integer_mv;
5920 }
5921 } else {
5922 cpi->common.cur_frame_force_integer_mv = 0;
5923 }
5924
chiyotsaifc1404d2019-08-08 12:09:12 -07005925#if CONFIG_DEBUG
5926 assert(hash_me_has_at_most_two_refs(cm->buffer_pool->frame_bufs) &&
5927 "Hash-me is leaking memory!");
5928#endif
5929
5930 if (cpi->oxcf.pass != 1 && cpi->need_to_clear_prev_hash_table) {
5931 av1_hash_table_clear_all(cpi->previous_hash_table);
5932 cpi->need_to_clear_prev_hash_table = 0;
5933 }
5934
Yaowu Xuc27fc142016-08-22 16:08:15 -07005935 // Set default state for segment based loop filter update flags.
5936 cm->lf.mode_ref_delta_update = 0;
5937
Yaowu Xuc27fc142016-08-22 16:08:15 -07005938 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05005939 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005940 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07005941 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005942
5943 // If segmentation is enabled force a map update for key frames.
5944 if (seg->enabled) {
5945 seg->update_map = 1;
5946 seg->update_data = 1;
5947 }
5948
5949 // The alternate reference frame cannot be active for a key frame.
5950 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005951 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00005952 if (cpi->oxcf.mtu == 0) {
5953 cm->num_tg = cpi->oxcf.num_tile_groups;
5954 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08005955 // Use a default value for the purposes of weighting costs in probability
5956 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00005957 cm->num_tg = DEFAULT_MAX_NUM_TG;
5958 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005959
5960 // For 1 pass CBR, check if we are dropping this frame.
5961 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005962 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
David Turnerd2a592e2018-11-16 14:59:31 +00005963 current_frame->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005964 if (av1_rc_drop_frame(cpi)) {
5965 av1_rc_postencode_update_drop_frame(cpi);
David Turnera4c96252019-01-11 16:36:39 +00005966 release_scaled_references(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08005967 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005968 }
5969 }
5970
sdengc23c7f12019-06-11 16:56:50 -07005971 if (oxcf->tuning == AOM_TUNE_SSIM) set_mb_ssim_rdmult_scaling(cpi);
5972
Yaowu Xuf883b422016-08-30 14:01:10 -07005973 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005974
5975#if CONFIG_INTERNAL_STATS
5976 memset(cpi->mode_chosen_counts, 0,
5977 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
5978#endif
5979
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005980 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005981 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08005982 * wraparound) */
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005983 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00005984 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08005985 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07005986 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
5987 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
5988 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005989 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005990 lsb = cpi->source->y_buffer[0] & 0xff;
5991 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005992 }
David Turner760a2f42018-12-07 15:25:36 +00005993 cm->current_frame_id =
5994 ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05005995
5996 // S_frame is meant for stitching different streams of different
5997 // resolutions together, so current_frame_id must be the
5998 // same across different streams of the same content current_frame_id
5999 // should be the same and not random. 0x37 is a chosen number as start
6000 // point
6001 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006002 } else {
6003 cm->current_frame_id =
David Turner760a2f42018-12-07 15:25:36 +00006004 (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
6005 (1 << seq_params->frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006006 }
6007 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006008
Hui Su483a8452018-02-26 12:28:48 -08006009 switch (cpi->oxcf.cdf_update_mode) {
6010 case 0: // No CDF update for any frames(4~6% compression loss).
6011 cm->disable_cdf_update = 1;
6012 break;
6013 case 1: // Enable CDF update for all frames.
6014 cm->disable_cdf_update = 0;
6015 break;
6016 case 2:
6017 // Strategically determine at which frames to do CDF update.
6018 // Currently only enable CDF update for all-intra and no-show frames(1.5%
6019 // compression loss).
6020 // TODO(huisu@google.com): design schemes for various trade-offs between
6021 // compression quality and decoding speed.
Hui Sub1b76b32018-02-27 15:24:48 -08006022 cm->disable_cdf_update =
6023 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08006024 break;
6025 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006026 cm->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08006027
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07006028 if (cpi->oxcf.pass == 2 && cpi->oxcf.enable_tpl_model == 2 &&
6029 current_frame->frame_type == INTER_FRAME) {
6030 if (!cm->show_frame) {
6031 assert(cpi->tpl_model_pass == 0);
6032 cpi->tpl_model_pass = 1;
6033 }
6034 }
6035
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07006036 int largest_tile_id = 0;
6037#if CONFIG_SUPERRES_IN_RECODE
6038 if (superres_in_recode_allowed(cpi)) {
6039 if (encode_with_and_without_superres(cpi, size, dest, &largest_tile_id) !=
6040 AOM_CODEC_OK) {
6041 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006042 }
Hui Su06463e42018-02-23 22:17:36 -08006043 } else {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07006044#endif // CONFIG_SUPERRES_IN_RECODE
6045 if (encode_with_recode_loop_and_filter(cpi, size, dest, NULL, NULL,
6046 &largest_tile_id) != AOM_CODEC_OK) {
6047 return AOM_CODEC_ERROR;
6048 }
6049#if CONFIG_SUPERRES_IN_RECODE
Hui Su06463e42018-02-23 22:17:36 -08006050 }
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07006051#endif // CONFIG_SUPERRES_IN_RECODE
Yaowu Xuc27fc142016-08-22 16:08:15 -07006052
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07006053 cpi->seq_params_locked = 1;
6054
David Turner996b2c12018-12-07 15:52:30 +00006055 // Update reference frame ids for reference frames this frame will overwrite
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006056 if (seq_params->frame_id_numbers_present_flag) {
David Turner996b2c12018-12-07 15:52:30 +00006057 for (int i = 0; i < REF_FRAMES; i++) {
6058 if ((current_frame->refresh_frame_flags >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006059 cm->ref_frame_id[i] = cm->current_frame_id;
6060 }
6061 }
6062 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006063
Yaowu Xuc27fc142016-08-22 16:08:15 -07006064#if DUMP_RECON_FRAMES == 1
6065 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07006066 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006067#endif // DUMP_RECON_FRAMES
6068
Soo-Chul Han934af352017-10-15 15:21:51 -04006069 if (cm->seg.enabled) {
6070 if (cm->seg.update_map) {
6071 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07006072 } else if (cm->last_frame_seg_map) {
David Turnerb757ce02018-11-12 15:01:28 +00006073 memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
Soo-Chul Han934af352017-10-15 15:21:51 -04006074 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
6075 }
6076 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006077
6078 if (frame_is_intra_only(cm) == 0) {
6079 release_scaled_references(cpi);
6080 }
6081
David Turner73245762019-02-11 16:42:34 +00006082 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
6083 // for the purpose to verify no mismatch between encoder and decoder.
6084 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
6085
6086 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006087
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08006088#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07006089 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08006090#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006091
Hui Sudc54be62018-03-14 19:14:28 -07006092 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
David Turner35cba132018-12-10 15:48:15 +00006093 *cm->fc = cpi->tile_data[largest_tile_id].tctx;
Hui Sudc54be62018-03-14 19:14:28 -07006094 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006095 }
David Turnera4c96252019-01-11 16:36:39 +00006096 if (!cm->large_scale_tile) {
6097 cm->cur_frame->frame_context = *cm->fc;
6098 }
6099#define EXT_TILE_DEBUG 0
6100#if EXT_TILE_DEBUG
6101 if (cm->large_scale_tile && oxcf->pass == 2) {
6102 char fn[20] = "./fc";
6103 fn[4] = current_frame->frame_number / 100 + '0';
6104 fn[5] = (current_frame->frame_number % 100) / 10 + '0';
6105 fn[6] = (current_frame->frame_number % 10) + '0';
6106 fn[7] = '\0';
6107 av1_print_frame_contexts(cm->fc, fn);
6108 }
6109#endif // EXT_TILE_DEBUG
6110#undef EXT_TILE_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07006111
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006112#if CONFIG_COLLECT_COMPONENT_TIMING
6113 end_timing(cpi, encode_frame_to_data_rate_time);
6114
6115 // Print out timing information.
6116 int i;
chiyotsai9c484b32019-03-07 16:01:50 -08006117 fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n",
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006118 cm->current_frame.frame_number,
chiyotsai9c484b32019-03-07 16:01:50 -08006119 get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006120 for (i = 0; i < kTimingComponents; i++) {
6121 cpi->component_time[i] += cpi->frame_component_time[i];
6122 fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n",
6123 get_component_name(i), cpi->frame_component_time[i],
6124 cpi->component_time[i]);
6125 cpi->frame_component_time[i] = 0;
6126 }
6127#endif
6128
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05306129 cm->last_frame_type = current_frame->frame_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006130
Yaowu Xuf883b422016-08-30 14:01:10 -07006131 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006132
chiyotsaifc1404d2019-08-08 12:09:12 -07006133 // Store encoded frame's hash table for in_integer_mv() next time.
6134 // Beware! If we don't update previous_hash_table here we will leak the
6135 // items stored in cur_frame's hash_table!
Remyaa14a4582019-11-05 13:17:36 +05306136 if (oxcf->pass != 1 && av1_use_hash_me(cpi)) {
David Turnerefed6372019-01-11 15:14:11 +00006137 cpi->previous_hash_table = &cm->cur_frame->hash_table;
chiyotsaifc1404d2019-08-08 12:09:12 -07006138 cpi->need_to_clear_prev_hash_table = 1;
David Turnerefed6372019-01-11 15:14:11 +00006139 }
6140
Yaowu Xuc27fc142016-08-22 16:08:15 -07006141 // Clear the one shot update flags for segmentation map and mode/ref loop
6142 // filter deltas.
6143 cm->seg.update_map = 0;
6144 cm->seg.update_data = 0;
6145 cm->lf.mode_ref_delta_update = 0;
6146
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07006147 // A droppable frame might not be shown but it always
6148 // takes a space in the gf group. Therefore, even when
6149 // it is not shown, we still need update the count down.
6150
Yaowu Xuc27fc142016-08-22 16:08:15 -07006151 if (cm->show_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006152 // Don't increment frame counters if this was an altref buffer
6153 // update not a real frame
David Turnerd2a592e2018-11-16 14:59:31 +00006154 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006155 }
6156
Tom Finegane4099e32018-01-23 12:01:51 -08006157 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006158}
6159
David Turner056f7cd2019-01-07 17:48:13 +00006160int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
David Turnercb5e36f2019-01-17 17:15:25 +00006161 const EncodeFrameInput *const frame_input,
David Turner056f7cd2019-01-07 17:48:13 +00006162 const EncodeFrameParams *const frame_params,
6163 EncodeFrameResults *const frame_results) {
David Turner07dbd8e2019-01-08 17:16:25 +00006164 AV1_COMMON *const cm = &cpi->common;
David Turnera7f133c2019-01-22 14:47:16 +00006165 CurrentFrame *const current_frame = &cm->current_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00006166
David Turnercb5e36f2019-01-17 17:15:25 +00006167 cpi->unscaled_source = frame_input->source;
6168 cpi->source = frame_input->source;
6169 cpi->unscaled_last_source = frame_input->last_source;
David Turner056f7cd2019-01-07 17:48:13 +00006170
David Turner6e8b4d92019-02-18 15:01:33 +00006171 current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
David Turner07dbd8e2019-01-08 17:16:25 +00006172 cm->error_resilient_mode = frame_params->error_resilient_mode;
David Turnera7f133c2019-01-22 14:47:16 +00006173 cm->primary_ref_frame = frame_params->primary_ref_frame;
David Turner475a3132019-01-18 15:17:17 +00006174 cm->current_frame.frame_type = frame_params->frame_type;
David Turnerdedd8ff2019-01-23 13:59:46 +00006175 cm->show_frame = frame_params->show_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00006176 cpi->ref_frame_flags = frame_params->ref_frame_flags;
David Turner04b70d82019-01-24 15:39:19 +00006177 cpi->speed = frame_params->speed;
David Turnere86ee0d2019-02-18 17:16:28 +00006178 cm->show_existing_frame = frame_params->show_existing_frame;
6179 cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
David Turner07dbd8e2019-01-08 17:16:25 +00006180
David Turner73245762019-02-11 16:42:34 +00006181 memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
6182 REF_FRAMES * sizeof(*cm->remapped_ref_idx));
6183
David Turnerfe3aecb2019-02-06 14:42:42 +00006184 cpi->refresh_last_frame = frame_params->refresh_last_frame;
6185 cpi->refresh_golden_frame = frame_params->refresh_golden_frame;
6186 cpi->refresh_bwd_ref_frame = frame_params->refresh_bwd_ref_frame;
David Turnerfe3aecb2019-02-06 14:42:42 +00006187 cpi->refresh_alt_ref_frame = frame_params->refresh_alt_ref_frame;
6188
David Turnera7f133c2019-01-22 14:47:16 +00006189 if (current_frame->frame_type == KEY_FRAME && cm->show_frame)
6190 current_frame->frame_number = 0;
6191
6192 if (cm->show_existing_frame) {
6193 current_frame->order_hint = cm->cur_frame->order_hint;
Ravi Chaudhary9701cd62019-07-18 17:32:26 +05306194 current_frame->display_order_hint = cm->cur_frame->display_order_hint;
David Turnera7f133c2019-01-22 14:47:16 +00006195 } else {
6196 current_frame->order_hint =
6197 current_frame->frame_number + frame_params->order_offset;
Ravi Chaudhary9701cd62019-07-18 17:32:26 +05306198 current_frame->display_order_hint = current_frame->order_hint;
David Turnera7f133c2019-01-22 14:47:16 +00006199 current_frame->order_hint %=
6200 (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
6201 }
6202
David Turnercb5e36f2019-01-17 17:15:25 +00006203 if (cpi->oxcf.pass == 1) {
Jerome Jiang2612b4d2019-05-29 17:46:47 -07006204#if !CONFIG_REALTIME_ONLY
David Turnercb5e36f2019-01-17 17:15:25 +00006205 av1_first_pass(cpi, frame_input->ts_duration);
Jerome Jiang2612b4d2019-05-29 17:46:47 -07006206#endif
David Turnercb5e36f2019-01-17 17:15:25 +00006207 } else if (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) {
David Turner73245762019-02-11 16:42:34 +00006208 if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
6209 AOM_CODEC_OK) {
David Turnercb5e36f2019-01-17 17:15:25 +00006210 return AOM_CODEC_ERROR;
6211 }
6212 } else {
David Turner056f7cd2019-01-07 17:48:13 +00006213 return AOM_CODEC_ERROR;
6214 }
6215
6216 return AOM_CODEC_OK;
6217}
6218
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006219#if CONFIG_DENOISE
6220static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
6221 int block_size, float noise_level,
6222 int64_t time_stamp, int64_t end_time) {
6223 AV1_COMMON *const cm = &cpi->common;
6224 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006225 cpi->denoise_and_model = aom_denoise_and_model_alloc(
6226 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006227 if (!cpi->denoise_and_model) {
6228 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6229 "Error allocating denoise and model");
6230 return -1;
6231 }
6232 }
6233 if (!cpi->film_grain_table) {
6234 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
6235 if (!cpi->film_grain_table) {
6236 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6237 "Error allocating grain table");
6238 return -1;
6239 }
6240 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
6241 }
6242 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
6243 &cm->film_grain_params)) {
6244 if (cm->film_grain_params.apply_grain) {
6245 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
6246 &cm->film_grain_params);
6247 }
6248 }
6249 return 0;
6250}
6251#endif
6252
James Zern3e2613b2017-03-30 23:14:40 -07006253int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07006254 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
6255 int64_t end_time) {
6256 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006257 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006258 int res = 0;
6259 const int subsampling_x = sd->subsampling_x;
6260 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006261 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006262
Yaowu Xuc27fc142016-08-22 16:08:15 -07006263 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006264
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006265#if CONFIG_INTERNAL_STATS
6266 struct aom_usec_timer timer;
Yaowu Xuf883b422016-08-30 14:01:10 -07006267 aom_usec_timer_start(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006268#endif
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006269#if CONFIG_DENOISE
6270 if (cpi->oxcf.noise_level > 0)
6271 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
6272 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
6273 res = -1;
6274#endif // CONFIG_DENOISE
6275
Yaowu Xuf883b422016-08-30 14:01:10 -07006276 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08006277 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07006278 res = -1;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006279#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006280 aom_usec_timer_mark(&timer);
6281 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006282#endif
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006283 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07006284 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006285 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006286 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07006287 res = -1;
6288 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006289 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006290 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006291 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006292 "Profile 1 requires 4:4:4 color format");
6293 res = -1;
6294 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006295 if ((seq_params->profile == PROFILE_2) &&
6296 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006297 !(subsampling_x == 1 && subsampling_y == 0)) {
6298 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
6299 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07006300 res = -1;
6301 }
6302
6303 return res;
6304}
6305
Yaowu Xuc27fc142016-08-22 16:08:15 -07006306#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006307extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
6308 const unsigned char *img2, int img2_pitch,
6309 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006310
6311static void adjust_image_stat(double y, double u, double v, double all,
6312 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07006313 s->stat[STAT_Y] += y;
6314 s->stat[STAT_U] += u;
6315 s->stat[STAT_V] += v;
6316 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07006317 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006318}
6319
Angie Chiang08a22a62017-07-17 17:29:17 -07006320static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006321 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006322 double samples = 0.0;
6323 uint32_t in_bit_depth = 8;
6324 uint32_t bit_depth = 8;
6325
Angie Chiang08a22a62017-07-17 17:29:17 -07006326#if CONFIG_INTER_STATS_ONLY
David Turnerd2a592e2018-11-16 14:59:31 +00006327 if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07006328#endif
6329 cpi->bytes += frame_bytes;
6330
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006331 if (cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006332 in_bit_depth = cpi->oxcf.input_bit_depth;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006333 bit_depth = cm->seq_params.bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006334 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006335 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07006336 const YV12_BUFFER_CONFIG *orig = cpi->source;
David Turnerc29e1a92018-12-06 14:10:14 +00006337 const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006338 double y, u, v, frame_all;
6339
6340 cpi->count++;
6341 if (cpi->b_calculate_psnr) {
6342 PSNR_STATS psnr;
6343 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07006344 aom_clear_system_state();
Jerome Jiangfa1d1732019-08-06 10:31:20 -07006345#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xud3e7c682017-12-21 14:08:25 -08006346 // TODO(yaowu): unify these two versions into one.
Jerome Jiangfa1d1732019-08-06 10:31:20 -07006347 if (cm->seq_params.use_highbitdepth)
6348 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
6349 else
6350 aom_calc_psnr(orig, recon, &psnr);
6351#else
6352 aom_calc_psnr(orig, recon, &psnr);
6353#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006354 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
6355 &cpi->psnr);
6356 cpi->total_sq_error += psnr.sse[0];
6357 cpi->total_samples += psnr.samples[0];
6358 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08006359 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006360 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07006361 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07006362 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006363 else
Yaowu Xuf883b422016-08-30 14:01:10 -07006364 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006365
Yaowu Xuf883b422016-08-30 14:01:10 -07006366 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006367 cpi->summed_quality += frame_ssim2 * weight;
6368 cpi->summed_weights += weight;
6369
6370#if 0
6371 {
6372 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08006373 double y2 = psnr.psnr[1];
6374 double u2 = psnr.psnr[2];
6375 double v2 = psnr.psnr[3];
6376 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07006377 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 +00006378 cm->current_frame.frame_number, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07006379 frame_psnr2, frame_ssim2);
6380 fclose(f);
6381 }
6382#endif
6383 }
6384 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006385 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006386 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07006387 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
6388 recon->y_stride, orig->y_width, orig->y_height);
6389 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006390 cpi->total_blockiness += frame_blockiness;
6391 }
6392
6393 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006394 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006395 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07006396 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
6397 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
6398
6399 const double peak = (double)((1 << in_bit_depth) - 1);
6400 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006401 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006402 if (consistency > 0.0)
6403 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006404 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006405 cpi->total_inconsistency += this_inconsistency;
6406 }
6407 }
6408 }
6409
6410 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07006411 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006412 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07006413 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006414 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
6415 }
6416}
6417#endif // CONFIG_INTERNAL_STATS
Andrey Norkin795ba872018-03-06 13:24:14 -08006418int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
6419 size_t *size, uint8_t *dest, int64_t *time_stamp,
6420 int64_t *time_end, int flush,
Yue Chen1bc5be62018-08-24 13:57:32 -07006421 const aom_rational64_t *timestamp_ratio) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006422 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
6423 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006424
6425#if CONFIG_BITSTREAM_DEBUG
6426 assert(cpi->oxcf.max_threads == 0 &&
6427 "bitstream debug tool does not support multithreading");
6428 bitstream_queue_record_write();
Yaowu Xu63f2ea32019-04-29 10:47:42 -07006429 aom_bitstream_queue_set_frame_write(cm->current_frame.frame_number * 2 +
6430 cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006431#endif
Marco Paniconi63971322019-08-15 21:32:05 -07006432 if (cpi->use_svc && cm->number_spatial_layers > 1) {
6433 av1_one_pass_cbr_svc_start_layer(cpi);
6434 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006435
Sarah Parker740e8392019-01-23 15:47:53 -08006436 // Indicates whether or not to use an adaptive quantize b rather than
6437 // the traditional version
6438 cm->use_quant_b_adapt = cpi->oxcf.quant_b_adapt;
6439
Dominic Symesd4929012018-01-31 17:32:01 +01006440 cm->showable_frame = 0;
David Turnere43f7fe2019-01-15 14:58:00 +00006441 *size = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006442#if CONFIG_INTERNAL_STATS
6443 struct aom_usec_timer cmptimer;
Yaowu Xuf883b422016-08-30 14:01:10 -07006444 aom_usec_timer_start(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006445#endif
Debargha Mukherjeecd14cc12019-09-06 09:55:52 -07006446 set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006447
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07006448 // Normal defaults
sarahparker27d686a2018-03-30 17:43:44 -07006449 cm->refresh_frame_context = oxcf->frame_parallel_decoding_mode
6450 ? REFRESH_FRAME_CONTEXT_DISABLED
6451 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01006452 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08006453 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006454
Sarah Parkerb9041612018-05-22 19:06:47 -07006455 // Initialize fields related to forward keyframes
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07006456 cpi->no_show_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08006457
David Turnerdedd8ff2019-01-23 13:59:46 +00006458 if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006459
Yue Chen1bc5be62018-08-24 13:57:32 -07006460 const int result =
6461 av1_encode_strategy(cpi, size, dest, frame_flags, time_stamp, time_end,
6462 timestamp_ratio, flush);
David Turnerdedd8ff2019-01-23 13:59:46 +00006463 if (result != AOM_CODEC_OK && result != -1) {
David Turner1539bb02019-01-24 15:28:13 +00006464 return AOM_CODEC_ERROR;
David Turnerdedd8ff2019-01-23 13:59:46 +00006465 } else if (result == -1) {
6466 // Returning -1 indicates no frame encoded; more input is required
6467 return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006468 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006469#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006470 aom_usec_timer_mark(&cmptimer);
6471 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yue Chen1bc5be62018-08-24 13:57:32 -07006472#endif // CONFIG_INTERNAL_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00006473 if (cpi->b_calculate_psnr) {
6474 if (cm->show_existing_frame || (oxcf->pass != 1 && cm->show_frame)) {
6475 generate_psnr_packet(cpi);
6476 }
6477 }
Hui Su8ea87322019-03-29 14:44:32 -07006478
6479 if (cpi->keep_level_stats && oxcf->pass != 1) {
6480 // Initialize level info. at the beginning of each sequence.
6481 if (cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) {
Hui Su58753d62019-05-29 09:56:19 -07006482 av1_init_level_info(cpi);
Hui Su8ea87322019-03-29 14:44:32 -07006483 }
kyslovabeeb7c2019-03-06 18:35:04 -08006484 av1_update_level_info(cpi, *size, *time_stamp, *time_end);
Hui Su8ea87322019-03-29 14:44:32 -07006485 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006486
6487#if CONFIG_INTERNAL_STATS
6488 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006489 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006490 }
6491#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08006492#if CONFIG_SPEED_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00006493 if (cpi->oxcf.pass != 1 && !cm->show_existing_frame) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08006494 cpi->tx_search_count += cpi->td.mb.tx_search_count;
6495 cpi->td.mb.tx_search_count = 0;
6496 }
6497#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006498
Yaowu Xuf883b422016-08-30 14:01:10 -07006499 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006500
Hui Su0d0ee662019-07-29 14:38:36 -07006501 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006502}
6503
Yaowu Xuf883b422016-08-30 14:01:10 -07006504int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6505 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006506 if (!cm->show_frame) {
6507 return -1;
6508 } else {
6509 int ret;
David Turnerc29e1a92018-12-06 14:10:14 +00006510 if (cm->cur_frame != NULL) {
6511 *dest = cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006512 dest->y_width = cm->width;
6513 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006514 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
6515 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006516 ret = 0;
6517 } else {
6518 ret = -1;
6519 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006520 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006521 return ret;
6522 }
6523}
6524
Yaowu Xuf883b422016-08-30 14:01:10 -07006525int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
David Turnere7ebf902018-12-04 14:04:55 +00006526 if (cpi->last_show_frame_buf == NULL) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006527
David Turnere7ebf902018-12-04 14:04:55 +00006528 *frame = cpi->last_show_frame_buf->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006529 return 0;
6530}
6531
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006532static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
6533 const YV12_BUFFER_CONFIG *b) {
6534 return a->y_height == b->y_height && a->y_width == b->y_width &&
6535 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
6536 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
6537 a->border == b->border &&
6538 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
6539 (b->flags & YV12_FLAG_HIGHBITDEPTH);
6540}
6541
Yunqing Wang93b18f32018-06-08 21:08:29 -07006542aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
6543 YV12_BUFFER_CONFIG *new_frame,
6544 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006545 const int num_planes = av1_num_planes(cm);
6546 if (!equal_dimensions_and_border(new_frame, sd))
6547 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
6548 "Incorrect buffer dimensions");
6549 else
6550 aom_yv12_copy_frame(new_frame, sd, num_planes);
6551
6552 return cm->error.error_code;
6553}
6554
Yaowu Xuf883b422016-08-30 14:01:10 -07006555int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6556 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006557 int hr = 0, hs = 0, vr = 0, vs = 0;
6558
6559 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6560
6561 Scale2Ratio(horiz_mode, &hr, &hs);
6562 Scale2Ratio(vert_mode, &vr, &vs);
6563
6564 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07006565 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6566 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006567
6568 return 0;
6569}
6570
Yaowu Xuf883b422016-08-30 14:01:10 -07006571int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006572
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006573int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
6574 size_t output_size = 0;
6575 size_t total_bytes_read = 0;
6576 size_t remaining_size = *frame_size;
6577 uint8_t *buff_ptr = buffer;
6578
6579 // go through each OBUs
6580 while (total_bytes_read < *frame_size) {
6581 uint8_t saved_obu_header[2];
6582 uint64_t obu_payload_size;
6583 size_t length_of_payload_size;
6584 size_t length_of_obu_size;
6585 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
6586 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
6587
6588 // save the obu header (1 or 2 bytes)
6589 memmove(saved_obu_header, buff_ptr, obu_header_size);
6590 // clear the obu_has_size_field
6591 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
6592
6593 // get the payload_size and length of payload_size
6594 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
6595 &obu_payload_size, &length_of_payload_size) != 0) {
6596 return AOM_CODEC_ERROR;
6597 }
6598 obu_bytes_read += length_of_payload_size;
6599
6600 // calculate the length of size of the obu header plus payload
6601 length_of_obu_size =
6602 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
6603
6604 // move the rest of data to new location
6605 memmove(buff_ptr + length_of_obu_size + obu_header_size,
6606 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07006607 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006608
6609 // write the new obu size
6610 const uint64_t obu_size = obu_header_size + obu_payload_size;
6611 size_t coded_obu_size;
6612 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
6613 &coded_obu_size) != 0) {
6614 return AOM_CODEC_ERROR;
6615 }
6616
6617 // write the saved (modified) obu_header following obu size
6618 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
6619
6620 total_bytes_read += obu_bytes_read;
6621 remaining_size -= obu_bytes_read;
6622 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07006623 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006624 }
6625
6626 *frame_size = output_size;
6627 return AOM_CODEC_OK;
6628}
6629
Marco Paniconid8574e32019-08-04 21:30:12 -07006630static void svc_set_updates_external_ref_frame_config(AV1_COMP *cpi) {
6631 cpi->ext_refresh_frame_flags_pending = 1;
6632 cpi->ext_refresh_last_frame = cpi->svc.refresh[cpi->svc.ref_idx[0]];
6633 cpi->ext_refresh_golden_frame = cpi->svc.refresh[cpi->svc.ref_idx[3]];
6634 cpi->ext_refresh_bwd_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[4]];
6635 cpi->ext_refresh_alt2_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[5]];
6636 cpi->ext_refresh_alt_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[6]];
6637 cpi->svc.non_reference_frame = 1;
6638 for (int i = 0; i < REF_FRAMES; i++) {
6639 if (cpi->svc.refresh[i] == 1) {
6640 cpi->svc.non_reference_frame = 0;
6641 break;
6642 }
6643 }
6644}
6645
Yaowu Xuf883b422016-08-30 14:01:10 -07006646void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006647 // TODO(yunqingwang): For what references to use, external encoding flags
6648 // should be consistent with internal reference frame selection. Need to
6649 // ensure that there is not conflict between the two. In AV1 encoder, the
6650 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006651 // GOLDEN, BWDREF, ALTREF2.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08006652 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006653 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006654 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
6655 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
6656 AOM_EFLAG_NO_REF_ARF2)) {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006657 int ref = AOM_REFFRAME_ALL;
6658
6659 if (flags & AOM_EFLAG_NO_REF_LAST) ref ^= AOM_LAST_FLAG;
6660 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
6661 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
6662
6663 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
6664
6665 if (flags & AOM_EFLAG_NO_REF_ARF) {
6666 ref ^= AOM_ALT_FLAG;
6667 ref ^= AOM_BWD_FLAG;
6668 ref ^= AOM_ALT2_FLAG;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006669 } else {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006670 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
6671 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006672 }
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006673
6674 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006675 }
6676
6677 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006678 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006679 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006680
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006681 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
6682 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006683
Yaowu Xuf883b422016-08-30 14:01:10 -07006684 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006685
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006686 if (flags & AOM_EFLAG_NO_UPD_ARF) {
6687 upd ^= AOM_ALT_FLAG;
6688 upd ^= AOM_BWD_FLAG;
6689 upd ^= AOM_ALT2_FLAG;
6690 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006691
David Turnerce9b5902019-01-23 17:25:47 +00006692 cpi->ext_refresh_last_frame = (upd & AOM_LAST_FLAG) != 0;
6693 cpi->ext_refresh_golden_frame = (upd & AOM_GOLD_FLAG) != 0;
6694 cpi->ext_refresh_alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
6695 cpi->ext_refresh_bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
6696 cpi->ext_refresh_alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
6697 cpi->ext_refresh_frame_flags_pending = 1;
David Turner4f1f1812019-01-24 17:00:24 +00006698 } else {
Marco Paniconid8574e32019-08-04 21:30:12 -07006699 if (cpi->svc.external_ref_frame_config)
6700 svc_set_updates_external_ref_frame_config(cpi);
6701 else
6702 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006703 }
6704
sarahparker21dbca42018-03-30 17:43:44 -07006705 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
6706 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
sarahparker27d686a2018-03-30 17:43:44 -07006707 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
6708 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
sarahparker9806fed2018-03-30 17:43:44 -07006709 cpi->ext_use_s_frame =
6710 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Sarah Parker50b6d6e2018-04-11 19:21:54 -07006711 cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07006712
Yaowu Xuf883b422016-08-30 14:01:10 -07006713 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
6714 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006715 }
6716}
Andrey Norkin795ba872018-03-06 13:24:14 -08006717
Tom Fineganf8d6a162018-08-21 10:47:55 -07006718aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
6719 if (!cpi) return NULL;
6720
6721 uint8_t header_buf[512] = { 0 };
6722 const uint32_t sequence_header_size =
Yaowu Xu797674b2019-05-01 17:38:11 -07006723 av1_write_sequence_header_obu(cpi, &header_buf[0]);
Tom Fineganf8d6a162018-08-21 10:47:55 -07006724 assert(sequence_header_size <= sizeof(header_buf));
6725 if (sequence_header_size == 0) return NULL;
6726
6727 const size_t obu_header_size = 1;
6728 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
6729 const size_t payload_offset = obu_header_size + size_field_size;
6730
6731 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
6732 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
6733
Hui Su4fd11762019-03-26 16:05:07 -07006734 if (av1_write_obu_header(cpi, OBU_SEQUENCE_HEADER, 0, &header_buf[0]) !=
Tom Fineganf8d6a162018-08-21 10:47:55 -07006735 obu_header_size) {
6736 return NULL;
6737 }
6738
6739 size_t coded_size_field_size = 0;
6740 if (aom_uleb_encode(sequence_header_size, size_field_size,
6741 &header_buf[obu_header_size],
6742 &coded_size_field_size) != 0) {
6743 return NULL;
6744 }
6745 assert(coded_size_field_size == size_field_size);
6746
6747 aom_fixed_buf_t *global_headers =
6748 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
6749 if (!global_headers) return NULL;
6750
6751 const size_t global_header_buf_size =
6752 obu_header_size + size_field_size + sequence_header_size;
6753
6754 global_headers->buf = malloc(global_header_buf_size);
6755 if (!global_headers->buf) {
6756 free(global_headers);
6757 return NULL;
6758 }
6759
6760 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
6761 global_headers->sz = global_header_buf_size;
6762 return global_headers;
6763}