blob: 1c04df7e0c9867369171034807912de63b3c5cb5 [file] [log] [blame]
Jayasanker Jdee179d2020-07-10 23:47:37 +05301/*
2 * Copyright (c) 2020, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
Wan-Teh Chang9e9534e2024-03-26 21:39:08 -070012#include <string.h>
13
Jingning Han4aefbde2021-02-03 15:09:04 -080014#include "aom/aomcx.h"
15
Wan-Teh Chang9e9534e2024-03-26 21:39:08 -070016#include "av1/common/av1_common_int.h"
Jayasanker Jdee179d2020-07-10 23:47:37 +053017#include "av1/encoder/bitstream.h"
18#include "av1/encoder/encodeframe.h"
19#include "av1/encoder/encoder.h"
20#include "av1/encoder/encoder_alloc.h"
21#include "av1/encoder/encodetxb.h"
22#include "av1/encoder/encoder_utils.h"
23#include "av1/encoder/grain_test_vectors.h"
24#include "av1/encoder/mv_prec.h"
25#include "av1/encoder/rc_utils.h"
26#include "av1/encoder/rdopt.h"
27#include "av1/encoder/segmentation.h"
28#include "av1/encoder/superres_scale.h"
Jingning Hane9187e32021-08-02 23:57:30 -070029#include "av1/encoder/tpl_model.h"
Jayasanker Jdee179d2020-07-10 23:47:37 +053030#include "av1/encoder/var_based_part.h"
31
Christopher Degawa4484d5a2020-08-09 00:13:27 +000032#if CONFIG_TUNE_VMAF
33#include "av1/encoder/tune_vmaf.h"
34#endif
35
Jayasanker Jdee179d2020-07-10 23:47:37 +053036#define MIN_BOOST_COMBINE_FACTOR 4.0
37#define MAX_BOOST_COMBINE_FACTOR 12.0
38
39const int default_tx_type_probs[FRAME_UPDATE_TYPES][TX_SIZES_ALL][TX_TYPES] = {
40 { { 221, 189, 214, 292, 0, 0, 0, 0, 0, 2, 38, 68, 0, 0, 0, 0 },
41 { 262, 203, 216, 239, 0, 0, 0, 0, 0, 1, 37, 66, 0, 0, 0, 0 },
42 { 315, 231, 239, 226, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0 },
43 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
44 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
45 { 222, 188, 214, 287, 0, 0, 0, 0, 0, 2, 50, 61, 0, 0, 0, 0 },
46 { 256, 182, 205, 282, 0, 0, 0, 0, 0, 2, 21, 76, 0, 0, 0, 0 },
47 { 281, 214, 217, 222, 0, 0, 0, 0, 0, 1, 48, 41, 0, 0, 0, 0 },
48 { 263, 194, 225, 225, 0, 0, 0, 0, 0, 2, 15, 100, 0, 0, 0, 0 },
49 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
50 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
51 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
52 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
53 { 170, 192, 242, 293, 0, 0, 0, 0, 0, 1, 68, 58, 0, 0, 0, 0 },
54 { 199, 210, 213, 291, 0, 0, 0, 0, 0, 1, 14, 96, 0, 0, 0, 0 },
55 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
56 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
57 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
58 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
59 { { 106, 69, 107, 278, 9, 15, 20, 45, 49, 23, 23, 88, 36, 74, 25, 57 },
60 { 105, 72, 81, 98, 45, 49, 47, 50, 56, 72, 30, 81, 33, 95, 27, 83 },
61 { 211, 105, 109, 120, 57, 62, 43, 49, 52, 58, 42, 116, 0, 0, 0, 0 },
62 { 1008, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0 },
63 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
64 { 131, 57, 98, 172, 19, 40, 37, 64, 69, 22, 41, 52, 51, 77, 35, 59 },
65 { 176, 83, 93, 202, 22, 24, 28, 47, 50, 16, 12, 93, 26, 76, 17, 59 },
66 { 136, 72, 89, 95, 46, 59, 47, 56, 61, 68, 35, 51, 32, 82, 26, 69 },
67 { 122, 80, 87, 105, 49, 47, 46, 46, 57, 52, 13, 90, 19, 103, 15, 93 },
68 { 1009, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0 },
69 { 1011, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0 },
70 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
71 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
72 { 202, 20, 84, 114, 14, 60, 41, 79, 99, 21, 41, 15, 50, 84, 34, 66 },
73 { 196, 44, 23, 72, 30, 22, 28, 57, 67, 13, 4, 165, 15, 148, 9, 131 },
74 { 882, 0, 0, 0, 0, 0, 0, 0, 0, 142, 0, 0, 0, 0, 0, 0 },
75 { 840, 0, 0, 0, 0, 0, 0, 0, 0, 184, 0, 0, 0, 0, 0, 0 },
76 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
77 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
78 { { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
79 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
80 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
81 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
82 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
83 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
84 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
85 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
86 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
87 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
88 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
89 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
90 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
91 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
92 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
93 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
94 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
95 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
96 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 } },
97 { { 213, 110, 141, 269, 12, 16, 15, 19, 21, 11, 38, 68, 22, 29, 16, 24 },
98 { 216, 119, 128, 143, 38, 41, 26, 30, 31, 30, 42, 70, 23, 36, 19, 32 },
99 { 367, 149, 154, 154, 38, 35, 17, 21, 21, 10, 22, 36, 0, 0, 0, 0 },
100 { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
101 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
102 { 219, 96, 127, 191, 21, 40, 25, 32, 34, 18, 45, 45, 33, 39, 26, 33 },
103 { 296, 99, 122, 198, 23, 21, 19, 24, 25, 13, 20, 64, 23, 32, 18, 27 },
104 { 275, 128, 142, 143, 35, 48, 23, 30, 29, 18, 42, 36, 18, 23, 14, 20 },
105 { 239, 132, 166, 175, 36, 27, 19, 21, 24, 14, 13, 85, 9, 31, 8, 25 },
106 { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
107 { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
108 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
109 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
110 { 309, 25, 79, 59, 25, 80, 34, 53, 61, 25, 49, 23, 43, 64, 36, 59 },
111 { 270, 57, 40, 54, 50, 42, 41, 53, 56, 28, 17, 81, 45, 86, 34, 70 },
112 { 1005, 0, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 0, 0, 0 },
113 { 992, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0 },
114 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
115 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
116 { { 133, 63, 55, 83, 57, 87, 58, 72, 68, 16, 24, 35, 29, 105, 25, 114 },
117 { 131, 75, 74, 60, 71, 77, 65, 66, 73, 33, 21, 79, 20, 83, 18, 78 },
118 { 276, 95, 82, 58, 86, 93, 63, 60, 64, 17, 38, 92, 0, 0, 0, 0 },
119 { 1006, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0 },
120 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
121 { 147, 49, 75, 78, 50, 97, 60, 67, 76, 17, 42, 35, 31, 93, 27, 80 },
122 { 157, 49, 58, 75, 61, 52, 56, 67, 69, 12, 15, 79, 24, 119, 11, 120 },
123 { 178, 69, 83, 77, 69, 85, 72, 77, 77, 20, 35, 40, 25, 48, 23, 46 },
124 { 174, 55, 64, 57, 73, 68, 62, 61, 75, 15, 12, 90, 17, 99, 16, 86 },
125 { 1008, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0 },
126 { 1018, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0 },
127 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
128 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
129 { 266, 31, 63, 64, 21, 52, 39, 54, 63, 30, 52, 31, 48, 89, 46, 75 },
130 { 272, 26, 32, 44, 29, 31, 32, 53, 51, 13, 13, 88, 22, 153, 16, 149 },
131 { 923, 0, 0, 0, 0, 0, 0, 0, 0, 101, 0, 0, 0, 0, 0, 0 },
132 { 969, 0, 0, 0, 0, 0, 0, 0, 0, 55, 0, 0, 0, 0, 0, 0 },
133 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
134 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
135 { { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
136 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
137 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
138 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
139 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
140 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
141 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
142 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
143 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
144 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
145 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
146 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
147 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
148 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
149 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
150 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
151 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
152 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 },
153 { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 } },
154 { { 158, 92, 125, 298, 12, 15, 20, 29, 31, 12, 29, 67, 34, 44, 23, 35 },
155 { 147, 94, 103, 123, 45, 48, 38, 41, 46, 48, 37, 78, 33, 63, 27, 53 },
156 { 268, 126, 125, 136, 54, 53, 31, 38, 38, 33, 35, 87, 0, 0, 0, 0 },
157 { 1018, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0 },
158 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
159 { 159, 72, 103, 194, 20, 35, 37, 50, 56, 21, 39, 40, 51, 61, 38, 48 },
160 { 259, 86, 95, 188, 32, 20, 25, 34, 37, 13, 12, 85, 25, 53, 17, 43 },
161 { 189, 99, 113, 123, 45, 59, 37, 46, 48, 44, 39, 41, 31, 47, 26, 37 },
162 { 175, 110, 113, 128, 58, 38, 33, 33, 43, 29, 13, 100, 14, 68, 12, 57 },
163 { 1017, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0 },
164 { 1019, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0 },
165 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
166 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
167 { 208, 22, 84, 101, 21, 59, 44, 70, 90, 25, 59, 13, 64, 67, 49, 48 },
168 { 277, 52, 32, 63, 43, 26, 33, 48, 54, 11, 6, 130, 18, 119, 11, 101 },
169 { 963, 0, 0, 0, 0, 0, 0, 0, 0, 61, 0, 0, 0, 0, 0, 0 },
170 { 979, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 0, 0, 0 },
171 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
172 { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
173};
174
175const int default_obmc_probs[FRAME_UPDATE_TYPES][BLOCK_SIZES_ALL] = {
176 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
177 { 0, 0, 0, 106, 90, 90, 97, 67, 59, 70, 28,
178 30, 38, 16, 16, 16, 0, 0, 44, 50, 26, 25 },
179 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
180 { 0, 0, 0, 98, 93, 97, 68, 82, 85, 33, 30,
181 33, 16, 16, 16, 16, 0, 0, 43, 37, 26, 16 },
182 { 0, 0, 0, 91, 80, 76, 78, 55, 49, 24, 16,
183 16, 16, 16, 16, 16, 0, 0, 29, 45, 16, 38 },
184 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
185 { 0, 0, 0, 103, 89, 89, 89, 62, 63, 76, 34,
186 35, 32, 19, 16, 16, 0, 0, 49, 55, 29, 19 }
187};
188
189const int default_warped_probs[FRAME_UPDATE_TYPES] = { 64, 64, 64, 64,
190 64, 64, 64 };
191
192// TODO(yunqing): the default probs can be trained later from better
193// performance.
194const int default_switchable_interp_probs[FRAME_UPDATE_TYPES]
195 [SWITCHABLE_FILTER_CONTEXTS]
196 [SWITCHABLE_FILTERS] = {
197 { { 512, 512, 512 },
198 { 512, 512, 512 },
199 { 512, 512, 512 },
200 { 512, 512, 512 },
201 { 512, 512, 512 },
202 { 512, 512, 512 },
203 { 512, 512, 512 },
204 { 512, 512, 512 },
205 { 512, 512, 512 },
206 { 512, 512, 512 },
207 { 512, 512, 512 },
208 { 512, 512, 512 },
209 { 512, 512, 512 },
210 { 512, 512, 512 },
211 { 512, 512, 512 },
212 { 512, 512, 512 } },
213 { { 512, 512, 512 },
214 { 512, 512, 512 },
215 { 512, 512, 512 },
216 { 512, 512, 512 },
217 { 512, 512, 512 },
218 { 512, 512, 512 },
219 { 512, 512, 512 },
220 { 512, 512, 512 },
221 { 512, 512, 512 },
222 { 512, 512, 512 },
223 { 512, 512, 512 },
224 { 512, 512, 512 },
225 { 512, 512, 512 },
226 { 512, 512, 512 },
227 { 512, 512, 512 },
228 { 512, 512, 512 } },
229 { { 512, 512, 512 },
230 { 512, 512, 512 },
231 { 512, 512, 512 },
232 { 512, 512, 512 },
233 { 512, 512, 512 },
234 { 512, 512, 512 },
235 { 512, 512, 512 },
236 { 512, 512, 512 },
237 { 512, 512, 512 },
238 { 512, 512, 512 },
239 { 512, 512, 512 },
240 { 512, 512, 512 },
241 { 512, 512, 512 },
242 { 512, 512, 512 },
243 { 512, 512, 512 },
244 { 512, 512, 512 } },
245 { { 512, 512, 512 },
246 { 512, 512, 512 },
247 { 512, 512, 512 },
248 { 512, 512, 512 },
249 { 512, 512, 512 },
250 { 512, 512, 512 },
251 { 512, 512, 512 },
252 { 512, 512, 512 },
253 { 512, 512, 512 },
254 { 512, 512, 512 },
255 { 512, 512, 512 },
256 { 512, 512, 512 },
257 { 512, 512, 512 },
258 { 512, 512, 512 },
259 { 512, 512, 512 },
260 { 512, 512, 512 } },
261 { { 512, 512, 512 },
262 { 512, 512, 512 },
263 { 512, 512, 512 },
264 { 512, 512, 512 },
265 { 512, 512, 512 },
266 { 512, 512, 512 },
267 { 512, 512, 512 },
268 { 512, 512, 512 },
269 { 512, 512, 512 },
270 { 512, 512, 512 },
271 { 512, 512, 512 },
272 { 512, 512, 512 },
273 { 512, 512, 512 },
274 { 512, 512, 512 },
275 { 512, 512, 512 },
276 { 512, 512, 512 } },
277 { { 512, 512, 512 },
278 { 512, 512, 512 },
279 { 512, 512, 512 },
280 { 512, 512, 512 },
281 { 512, 512, 512 },
282 { 512, 512, 512 },
283 { 512, 512, 512 },
284 { 512, 512, 512 },
285 { 512, 512, 512 },
286 { 512, 512, 512 },
287 { 512, 512, 512 },
288 { 512, 512, 512 },
289 { 512, 512, 512 },
290 { 512, 512, 512 },
291 { 512, 512, 512 },
292 { 512, 512, 512 } },
293 { { 512, 512, 512 },
294 { 512, 512, 512 },
295 { 512, 512, 512 },
296 { 512, 512, 512 },
297 { 512, 512, 512 },
298 { 512, 512, 512 },
299 { 512, 512, 512 },
300 { 512, 512, 512 },
301 { 512, 512, 512 },
302 { 512, 512, 512 },
303 { 512, 512, 512 },
304 { 512, 512, 512 },
305 { 512, 512, 512 },
306 { 512, 512, 512 },
307 { 512, 512, 512 },
308 { 512, 512, 512 } }
309 };
310
311static void configure_static_seg_features(AV1_COMP *cpi) {
312 AV1_COMMON *const cm = &cpi->common;
313 const RATE_CONTROL *const rc = &cpi->rc;
314 struct segmentation *const seg = &cm->seg;
315
Remya Prakasan6566bc82021-11-05 23:21:12 +0530316 double avg_q;
Remya Prakasanffeb4972022-06-21 20:00:28 +0530317#if CONFIG_FPMT_TEST
Remya Prakasan6566bc82021-11-05 23:21:12 +0530318 avg_q = ((cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) &&
319 (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE))
320 ? cpi->ppi->p_rc.temp_avg_q
321 : cpi->ppi->p_rc.avg_q;
322#else
323 avg_q = cpi->ppi->p_rc.avg_q;
324#endif
325
Aasaipriyacdfcf172021-06-05 16:40:57 +0530326 int high_q = (int)(avg_q > 48.0);
Jayasanker Jdee179d2020-07-10 23:47:37 +0530327 int qi_delta;
328
329 // Disable and clear down for KF
330 if (cm->current_frame.frame_type == KEY_FRAME) {
331 // Clear down the global segmentation map
332 memset(cpi->enc_seg.map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols);
333 seg->update_map = 0;
334 seg->update_data = 0;
335
336 // Disable segmentation
337 av1_disable_segmentation(seg);
338
339 // Clear down the segment features.
340 av1_clearall_segfeatures(seg);
341 } else if (cpi->refresh_frame.alt_ref_frame) {
342 // If this is an alt ref frame
343 // Clear down the global segmentation map
344 memset(cpi->enc_seg.map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols);
345 seg->update_map = 0;
346 seg->update_data = 0;
347
348 // Disable segmentation and individual segment features by default
349 av1_disable_segmentation(seg);
350 av1_clearall_segfeatures(seg);
351
352 // If segmentation was enabled set those features needed for the
353 // arf itself.
354 if (seg->enabled) {
355 seg->update_map = 1;
356 seg->update_data = 1;
357
Aasaipriyacdfcf172021-06-05 16:40:57 +0530358 qi_delta = av1_compute_qdelta(rc, avg_q, avg_q * 0.875,
Tarundeep Singh4243e622021-04-20 16:10:22 +0530359 cm->seq_params->bit_depth);
Jayasanker Jdee179d2020-07-10 23:47:37 +0530360 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
361 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
362 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
363 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
364 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
365
366 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
367 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
368 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
369 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
370
371 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
372 }
373 } else if (seg->enabled) {
374 // All other frames if segmentation has been enabled
375
376 // First normal frame in a valid gf or alt ref group
377 if (rc->frames_since_golden == 0) {
378 // Set up segment features for normal frames in an arf group
Jingning Han98bcaef2020-09-27 14:57:39 -0700379 // Disable segmentation and clear down features if alt ref
380 // is not active for this group
Jayasanker Jdee179d2020-07-10 23:47:37 +0530381
Jingning Han98bcaef2020-09-27 14:57:39 -0700382 av1_disable_segmentation(seg);
Jayasanker Jdee179d2020-07-10 23:47:37 +0530383
Jingning Han98bcaef2020-09-27 14:57:39 -0700384 memset(cpi->enc_seg.map, 0,
385 cm->mi_params.mi_rows * cm->mi_params.mi_cols);
Jayasanker Jdee179d2020-07-10 23:47:37 +0530386
Jingning Han98bcaef2020-09-27 14:57:39 -0700387 seg->update_map = 0;
388 seg->update_data = 0;
Jayasanker Jdee179d2020-07-10 23:47:37 +0530389
Jingning Han98bcaef2020-09-27 14:57:39 -0700390 av1_clearall_segfeatures(seg);
Jayasanker Jdee179d2020-07-10 23:47:37 +0530391 } else if (rc->is_src_frame_alt_ref) {
392 // Special case where we are coding over the top of a previous
393 // alt ref frame.
394 // Segment coding disabled for compred testing
395
396 // Enable ref frame features for segment 0 as well
397 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
398 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
399
400 // All mbs should use ALTREF_FRAME
401 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
402 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
403 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
404 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
405
406 // Skip all MBs if high Q (0,0 mv and skip coeffs)
407 if (high_q) {
408 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
409 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
410 }
411 // Enable data update
412 seg->update_data = 1;
413 } else {
414 // All other frames.
415
416 // No updates.. leave things as they are.
417 seg->update_map = 0;
418 seg->update_data = 0;
419 }
420 }
421}
422
423void av1_apply_active_map(AV1_COMP *cpi) {
424 struct segmentation *const seg = &cpi->common.seg;
425 unsigned char *const seg_map = cpi->enc_seg.map;
426 const unsigned char *const active_map = cpi->active_map.map;
Jayasanker Jdee179d2020-07-10 23:47:37 +0530427
428 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
429
Marco Paniconi70c64d72024-03-16 23:45:34 -0700430 // Disable the active_maps on intra_only frames or if the
431 // input map for the current frame has no inactive blocks.
432 if (frame_is_intra_only(&cpi->common) ||
433 cpi->rc.percent_blocks_inactive == 0) {
Jayasanker Jdee179d2020-07-10 23:47:37 +0530434 cpi->active_map.enabled = 0;
435 cpi->active_map.update = 1;
436 }
437
438 if (cpi->active_map.update) {
439 if (cpi->active_map.enabled) {
chiyotsaiae4339f2022-11-16 10:44:11 -0800440 const int num_mis =
441 cpi->common.mi_params.mi_rows * cpi->common.mi_params.mi_cols;
Marco Paniconi70c64d72024-03-16 23:45:34 -0700442 memcpy(seg_map, active_map, sizeof(active_map[0]) * num_mis);
Jayasanker Jdee179d2020-07-10 23:47:37 +0530443 av1_enable_segmentation(seg);
444 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
445 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
446 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
447 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
448 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
449
450 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
451 -MAX_LOOP_FILTER);
452 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
453 -MAX_LOOP_FILTER);
454 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
455 -MAX_LOOP_FILTER);
456 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
457 -MAX_LOOP_FILTER);
458 } else {
459 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
460 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
461 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
462 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
463 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
464 if (seg->enabled) {
465 seg->update_data = 1;
466 seg->update_map = 1;
467 }
468 }
469 cpi->active_map.update = 0;
470 }
471}
472
473#if !CONFIG_REALTIME_ONLY
474static void process_tpl_stats_frame(AV1_COMP *cpi) {
Mufaddal Chakera8ee04fa2021-03-17 13:33:18 +0530475 const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
Jayasanker Jdee179d2020-07-10 23:47:37 +0530476 AV1_COMMON *const cm = &cpi->common;
477
Mufaddal Chakeraab20d372021-03-17 12:18:34 +0530478 assert(IMPLIES(gf_group->size > 0, cpi->gf_frame_index < gf_group->size));
Jayasanker Jdee179d2020-07-10 23:47:37 +0530479
Mufaddal Chakeraab20d372021-03-17 12:18:34 +0530480 const int tpl_idx = cpi->gf_frame_index;
Mufaddal Chakera7260d142021-04-12 01:03:40 +0530481 TplParams *const tpl_data = &cpi->ppi->tpl_data;
Jayasanker Jdee179d2020-07-10 23:47:37 +0530482 TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
483 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
484
485 if (tpl_frame->is_valid) {
486 int tpl_stride = tpl_frame->stride;
Jingning Han4a479402021-12-01 15:16:06 -0800487 double intra_cost_base = 0;
488 double mc_dep_cost_base = 0;
489 double cbcmp_base = 1;
Jayasanker Jdee179d2020-07-10 23:47:37 +0530490 const int step = 1 << tpl_data->tpl_stats_block_mis_log2;
Urvang Joshi68af5282020-10-08 15:37:55 -0700491 const int row_step = step;
492 const int col_step_sr =
493 coded_to_superres_mi(step, cm->superres_scale_denominator);
Jayasanker Jdee179d2020-07-10 23:47:37 +0530494 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
495
Urvang Joshi68af5282020-10-08 15:37:55 -0700496 for (int row = 0; row < cm->mi_params.mi_rows; row += row_step) {
497 for (int col = 0; col < mi_cols_sr; col += col_step_sr) {
Jayasanker Jdee179d2020-07-10 23:47:37 +0530498 TplDepStats *this_stats = &tpl_stats[av1_tpl_ptr_pos(
499 row, col, tpl_stride, tpl_data->tpl_stats_block_mis_log2)];
Jingning Hanfe2ec602021-12-31 07:26:21 -0800500 double cbcmp = (double)(this_stats->srcrf_dist);
Jayasanker Jdee179d2020-07-10 23:47:37 +0530501 int64_t mc_dep_delta =
502 RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
503 this_stats->mc_dep_dist);
Yaowu Xuf4360272021-12-30 09:14:28 -0800504 double dist_scaled = (double)(this_stats->recrf_dist << RDDIV_BITS);
505 intra_cost_base += log(dist_scaled) * cbcmp;
506 mc_dep_cost_base += log(dist_scaled + mc_dep_delta) * cbcmp;
Jingning Han4a479402021-12-01 15:16:06 -0800507 cbcmp_base += cbcmp;
Jayasanker Jdee179d2020-07-10 23:47:37 +0530508 }
509 }
510
511 if (mc_dep_cost_base == 0) {
512 tpl_frame->is_valid = 0;
513 } else {
Jingning Han4a479402021-12-01 15:16:06 -0800514 cpi->rd.r0 = exp((intra_cost_base - mc_dep_cost_base) / cbcmp_base);
Mufaddal Chakeraab20d372021-03-17 12:18:34 +0530515 if (is_frame_tpl_eligible(gf_group, cpi->gf_frame_index)) {
Tarundeep Singh5e5305a2021-03-16 13:04:04 +0530516 if (cpi->ppi->lap_enabled) {
Mufaddal Chakera94ee9bf2021-04-12 01:02:22 +0530517 double min_boost_factor = sqrt(cpi->ppi->p_rc.baseline_gf_interval);
Jayasanker Jdee179d2020-07-10 23:47:37 +0530518 const int gfu_boost = get_gfu_boost_from_r0_lap(
Jingning Han94b28542020-08-03 23:11:27 -0700519 min_boost_factor, MAX_GFUBOOST_FACTOR, cpi->rd.r0,
Mufaddal Chakera94ee9bf2021-04-12 01:02:22 +0530520 cpi->ppi->p_rc.num_stats_required_for_gfu_boost);
Jayasanker Jdee179d2020-07-10 23:47:37 +0530521 // printf("old boost %d new boost %d\n", cpi->rc.gfu_boost,
522 // gfu_boost);
Mufaddal Chakera94ee9bf2021-04-12 01:02:22 +0530523 cpi->ppi->p_rc.gfu_boost = combine_prior_with_tpl_boost(
524 min_boost_factor, MAX_BOOST_COMBINE_FACTOR,
525 cpi->ppi->p_rc.gfu_boost, gfu_boost,
526 cpi->ppi->p_rc.num_stats_used_for_gfu_boost);
Jayasanker Jdee179d2020-07-10 23:47:37 +0530527 } else {
Deepa K Gccca9fe2023-08-03 00:55:00 +0530528 // TPL may only look at a subset of frame in the gf group when the
529 // speed feature 'reduce_num_frames' is on, which affects the r0
530 // calcuation. Thus, to compensate for TPL not using all frames a
531 // factor to adjust r0 is used.
532 const int gfu_boost =
533 (int)(200.0 * cpi->ppi->tpl_data.r0_adjust_factor / cpi->rd.r0);
Mufaddal Chakera94ee9bf2021-04-12 01:02:22 +0530534 cpi->ppi->p_rc.gfu_boost = combine_prior_with_tpl_boost(
Jayasanker Jdee179d2020-07-10 23:47:37 +0530535 MIN_BOOST_COMBINE_FACTOR, MAX_BOOST_COMBINE_FACTOR,
Mufaddal Chakera94ee9bf2021-04-12 01:02:22 +0530536 cpi->ppi->p_rc.gfu_boost, gfu_boost, cpi->rc.frames_to_key);
Jayasanker Jdee179d2020-07-10 23:47:37 +0530537 }
538 }
Jayasanker Jdee179d2020-07-10 23:47:37 +0530539 }
540 }
541}
542#endif // !CONFIG_REALTIME_ONLY
543
544void av1_set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
545 int *top_index) {
546 AV1_COMMON *const cm = &cpi->common;
547
548 // Setup variables that depend on the dimensions of the frame.
549 av1_set_speed_features_framesize_dependent(cpi, cpi->speed);
550
551#if !CONFIG_REALTIME_ONLY
Mufaddal Chakera8ee04fa2021-03-17 13:33:18 +0530552 GF_GROUP *gf_group = &cpi->ppi->gf_group;
James Zernce095f12022-04-08 15:55:52 -0700553 if (cpi->oxcf.algo_cfg.enable_tpl_model &&
Jingning Han6850cc32021-12-25 00:52:32 -0800554 av1_tpl_stats_ready(&cpi->ppi->tpl_data, cpi->gf_frame_index)) {
Jayasanker Jdee179d2020-07-10 23:47:37 +0530555 process_tpl_stats_frame(cpi);
556 av1_tpl_rdmult_setup(cpi);
557 }
558#endif
559
560 // Decide q and q bounds.
Mufaddal Chakera94ee9bf2021-04-12 01:02:22 +0530561 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, cpi->gf_frame_index,
562 bottom_index, top_index);
Jayasanker Jdee179d2020-07-10 23:47:37 +0530563
Jingning Han16579342021-05-12 00:02:01 -0700564#if !CONFIG_REALTIME_ONLY
565 if (cpi->oxcf.rc_cfg.mode == AOM_Q &&
James Zernce095f12022-04-08 15:55:52 -0700566 cpi->ppi->tpl_data.tpl_frame[cpi->gf_frame_index].is_valid &&
Angie Chiangb93d5b72021-10-06 12:17:09 -0700567 !is_lossless_requested(&cpi->oxcf.rc_cfg)) {
Angie Chiang6dabc842021-11-02 15:34:15 -0700568 const RateControlCfg *const rc_cfg = &cpi->oxcf.rc_cfg;
569 const int tpl_q = av1_tpl_get_q_index(
570 &cpi->ppi->tpl_data, cpi->gf_frame_index, cpi->rc.active_worst_quality,
571 cm->seq_params->bit_depth);
572 *q = clamp(tpl_q, rc_cfg->best_allowed_q, rc_cfg->worst_allowed_q);
Jingning Han16579342021-05-12 00:02:01 -0700573 *top_index = *bottom_index = *q;
574 if (gf_group->update_type[cpi->gf_frame_index] == ARF_UPDATE)
575 cpi->ppi->p_rc.arf_q = *q;
576 }
Jingning Hane9187e32021-08-02 23:57:30 -0700577
Jingning Han958fcc52021-09-01 00:45:11 -0700578 if (cpi->oxcf.q_cfg.use_fixed_qp_offsets && cpi->oxcf.rc_cfg.mode == AOM_Q) {
579 if (is_frame_tpl_eligible(gf_group, cpi->gf_frame_index)) {
Jingning Hana6054862021-09-08 15:53:44 -0700580 const double qratio_grad =
581 cpi->ppi->p_rc.baseline_gf_interval > 20 ? 0.2 : 0.3;
Jingning Han958fcc52021-09-01 00:45:11 -0700582 const double qstep_ratio =
Jingning Hana6054862021-09-08 15:53:44 -0700583 0.2 +
584 (1.0 - (double)cpi->rc.active_worst_quality / MAXQ) * qratio_grad;
Jingning Han958fcc52021-09-01 00:45:11 -0700585 *q = av1_get_q_index_from_qstep_ratio(
586 cpi->rc.active_worst_quality, qstep_ratio, cm->seq_params->bit_depth);
587 *top_index = *bottom_index = *q;
588 if (gf_group->update_type[cpi->gf_frame_index] == ARF_UPDATE ||
589 gf_group->update_type[cpi->gf_frame_index] == KF_UPDATE ||
590 gf_group->update_type[cpi->gf_frame_index] == GF_UPDATE)
591 cpi->ppi->p_rc.arf_q = *q;
Jingning Hanaf3cd5b2021-09-09 13:21:21 -0700592 } else if (gf_group->layer_depth[cpi->gf_frame_index] <
593 gf_group->max_layer_depth) {
Jingning Han958fcc52021-09-01 00:45:11 -0700594 int this_height = gf_group->layer_depth[cpi->gf_frame_index];
595 int arf_q = cpi->ppi->p_rc.arf_q;
596 while (this_height > 1) {
597 arf_q = (arf_q + cpi->oxcf.rc_cfg.cq_level + 1) / 2;
598 --this_height;
599 }
600 *top_index = *bottom_index = *q = arf_q;
601 }
Jingning Hane9187e32021-08-02 23:57:30 -0700602 }
Jingning Han16579342021-05-12 00:02:01 -0700603#endif
604
Jayasanker Jdee179d2020-07-10 23:47:37 +0530605 // Configure experimental use of segmentation for enhanced coding of
606 // static regions if indicated.
607 // Only allowed in the second pass of a two pass encode, as it requires
608 // lagged coding, and if the relevant speed feature flag is set.
609 if (is_stat_consumption_stage_twopass(cpi) &&
610 cpi->sf.hl_sf.static_segmentation)
611 configure_static_seg_features(cpi);
612}
613
614static void reset_film_grain_chroma_params(aom_film_grain_t *pars) {
615 pars->num_cr_points = 0;
616 pars->cr_mult = 0;
617 pars->cr_luma_mult = 0;
618 memset(pars->scaling_points_cr, 0, sizeof(pars->scaling_points_cr));
619 memset(pars->ar_coeffs_cr, 0, sizeof(pars->ar_coeffs_cr));
620 pars->num_cb_points = 0;
621 pars->cb_mult = 0;
622 pars->cb_luma_mult = 0;
623 pars->chroma_scaling_from_luma = 0;
624 memset(pars->scaling_points_cb, 0, sizeof(pars->scaling_points_cb));
625 memset(pars->ar_coeffs_cb, 0, sizeof(pars->ar_coeffs_cb));
626}
627
Tarundeep Singh9e77b302021-03-19 16:07:48 +0530628void av1_update_film_grain_parameters_seq(struct AV1_PRIMARY *ppi,
629 const AV1EncoderConfig *oxcf) {
630 SequenceHeader *const seq_params = &ppi->seq_params;
631 const TuneCfg *const tune_cfg = &oxcf->tune_cfg;
632
633 if (tune_cfg->film_grain_test_vector || tune_cfg->film_grain_table_filename ||
634 tune_cfg->content == AOM_CONTENT_FILM) {
635 seq_params->film_grain_params_present = 1;
636 } else {
637#if CONFIG_DENOISE
638 seq_params->film_grain_params_present = (oxcf->noise_level > 0);
639#else
640 seq_params->film_grain_params_present = 0;
641#endif
642 }
643}
644
Jayasanker Jdee179d2020-07-10 23:47:37 +0530645void av1_update_film_grain_parameters(struct AV1_COMP *cpi,
646 const AV1EncoderConfig *oxcf) {
647 AV1_COMMON *const cm = &cpi->common;
Jayasanker Jdee179d2020-07-10 23:47:37 +0530648 const TuneCfg *const tune_cfg = &oxcf->tune_cfg;
649
650 if (cpi->film_grain_table) {
651 aom_film_grain_table_free(cpi->film_grain_table);
652 aom_free(cpi->film_grain_table);
653 cpi->film_grain_table = NULL;
654 }
655
656 if (tune_cfg->film_grain_test_vector) {
Jayasanker Jdee179d2020-07-10 23:47:37 +0530657 if (cm->current_frame.frame_type == KEY_FRAME) {
658 memcpy(&cm->film_grain_params,
659 film_grain_test_vectors + tune_cfg->film_grain_test_vector - 1,
660 sizeof(cm->film_grain_params));
661 if (oxcf->tool_cfg.enable_monochrome)
662 reset_film_grain_chroma_params(&cm->film_grain_params);
Tarundeep Singh4243e622021-04-20 16:10:22 +0530663 cm->film_grain_params.bit_depth = cm->seq_params->bit_depth;
664 if (cm->seq_params->color_range == AOM_CR_FULL_RANGE) {
Jayasanker Jdee179d2020-07-10 23:47:37 +0530665 cm->film_grain_params.clip_to_restricted_range = 0;
666 }
667 }
668 } else if (tune_cfg->film_grain_table_filename) {
James Zern8ff28562022-04-28 12:54:06 -0700669 CHECK_MEM_ERROR(cm, cpi->film_grain_table,
670 aom_calloc(1, sizeof(*cpi->film_grain_table)));
Jayasanker Jdee179d2020-07-10 23:47:37 +0530671
672 aom_film_grain_table_read(cpi->film_grain_table,
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +0530673 tune_cfg->film_grain_table_filename, cm->error);
Jingning Han4aefbde2021-02-03 15:09:04 -0800674 } else if (tune_cfg->content == AOM_CONTENT_FILM) {
Tarundeep Singh4243e622021-04-20 16:10:22 +0530675 cm->film_grain_params.bit_depth = cm->seq_params->bit_depth;
Jingning Han4aefbde2021-02-03 15:09:04 -0800676 if (oxcf->tool_cfg.enable_monochrome)
677 reset_film_grain_chroma_params(&cm->film_grain_params);
Tarundeep Singh4243e622021-04-20 16:10:22 +0530678 if (cm->seq_params->color_range == AOM_CR_FULL_RANGE)
Jingning Han4aefbde2021-02-03 15:09:04 -0800679 cm->film_grain_params.clip_to_restricted_range = 0;
Jayasanker Jdee179d2020-07-10 23:47:37 +0530680 } else {
Jayasanker Jdee179d2020-07-10 23:47:37 +0530681 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
682 }
683}
684
685void av1_scale_references(AV1_COMP *cpi, const InterpFilter filter,
686 const int phase, const int use_optimized_scaler) {
687 AV1_COMMON *cm = &cpi->common;
688 const int num_planes = av1_num_planes(cm);
689 MV_REFERENCE_FRAME ref_frame;
690
691 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
692 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
693 if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) {
694 BufferPool *const pool = cm->buffer_pool;
695 const YV12_BUFFER_CONFIG *const ref =
696 get_ref_frame_yv12_buf(cm, ref_frame);
697
698 if (ref == NULL) {
699 cpi->scaled_ref_buf[ref_frame - 1] = NULL;
700 continue;
701 }
702
Marco Paniconia7a3dce2023-07-26 15:36:30 -0700703 // For RTC-SVC: if force_zero_mode_spatial_ref is enabled, check if the
704 // motion search can be skipped for the references: last, golden, altref.
705 // If so, we can skip scaling that reference.
706 if (cpi->ppi->use_svc && cpi->svc.force_zero_mode_spatial_ref &&
707 cpi->ppi->rtc_ref.set_ref_frame_config) {
708 if (ref_frame == LAST_FRAME && cpi->svc.skip_mvsearch_last) continue;
709 if (ref_frame == GOLDEN_FRAME && cpi->svc.skip_mvsearch_gf) continue;
710 if (ref_frame == ALTREF_FRAME && cpi->svc.skip_mvsearch_altref)
711 continue;
712 }
Marco Paniconia5804712023-12-12 16:01:27 -0800713 // For RTC with superres on: golden reference only needs to be scaled
714 // if it was refreshed in previous frame.
715 if (is_one_pass_rt_params(cpi) &&
716 cpi->oxcf.superres_cfg.enable_superres && ref_frame == GOLDEN_FRAME &&
717 cpi->rc.frame_num_last_gf_refresh <
718 (int)cm->current_frame.frame_number - 1) {
719 continue;
720 }
Marco Paniconia7a3dce2023-07-26 15:36:30 -0700721
Jayasanker Jdee179d2020-07-10 23:47:37 +0530722 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
723 // Replace the reference buffer with a copy having a thicker border,
724 // if the reference buffer is higher resolution than the current
725 // frame, and the border is thin.
726 if ((ref->y_crop_width > cm->width ||
727 ref->y_crop_height > cm->height) &&
728 ref->border < AOM_BORDER_IN_PIXELS) {
729 RefCntBuffer *ref_fb = get_ref_frame_buf(cm, ref_frame);
730 if (aom_yv12_realloc_with_new_border(
731 &ref_fb->buf, AOM_BORDER_IN_PIXELS,
Rachel Barker72e349e2024-02-13 14:31:10 +0000732 cm->features.byte_alignment, cpi->alloc_pyramid,
Rachel Barker674eaa02022-12-22 16:03:44 +0000733 num_planes) != 0) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +0530734 aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
Jayasanker Jdee179d2020-07-10 23:47:37 +0530735 "Failed to allocate frame buffer");
736 }
737 }
738 int force_scaling = 0;
739 RefCntBuffer *new_fb = cpi->scaled_ref_buf[ref_frame - 1];
740 if (new_fb == NULL) {
741 const int new_fb_idx = get_free_fb(cm);
742 if (new_fb_idx == INVALID_IDX) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +0530743 aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
Jayasanker Jdee179d2020-07-10 23:47:37 +0530744 "Unable to find free frame buffer");
745 }
746 force_scaling = 1;
747 new_fb = &pool->frame_bufs[new_fb_idx];
748 }
749
750 if (force_scaling || new_fb->buf.y_crop_width != cm->width ||
751 new_fb->buf.y_crop_height != cm->height) {
752 if (aom_realloc_frame_buffer(
753 &new_fb->buf, cm->width, cm->height,
Tarundeep Singh4243e622021-04-20 16:10:22 +0530754 cm->seq_params->subsampling_x, cm->seq_params->subsampling_y,
755 cm->seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
Rachel Barker72e349e2024-02-13 14:31:10 +0000756 cm->features.byte_alignment, NULL, NULL, NULL, false, 0)) {
Jayasanker Jdee179d2020-07-10 23:47:37 +0530757 if (force_scaling) {
758 // Release the reference acquired in the get_free_fb() call above.
759 --new_fb->ref_count;
760 }
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +0530761 aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
Jayasanker Jdee179d2020-07-10 23:47:37 +0530762 "Failed to allocate frame buffer");
763 }
Yuan Tonge1399f32022-11-15 19:47:27 +0800764 bool has_optimized_scaler = av1_has_optimized_scaler(
Wan-Teh Changcc7a0cc2022-11-23 16:01:26 -0800765 ref->y_crop_width, ref->y_crop_height, new_fb->buf.y_crop_width,
Jerome Jiangff7b7532022-08-30 14:45:28 -0400766 new_fb->buf.y_crop_height);
Yuan Tonge1399f32022-11-15 19:47:27 +0800767 if (num_planes > 1) {
768 has_optimized_scaler =
769 has_optimized_scaler &&
770 av1_has_optimized_scaler(
771 ref->uv_crop_width, ref->uv_crop_height,
772 new_fb->buf.uv_crop_width, new_fb->buf.uv_crop_height);
773 }
Jayasanker Jdee179d2020-07-10 23:47:37 +0530774#if CONFIG_AV1_HIGHBITDEPTH
Jerome Jiangff7b7532022-08-30 14:45:28 -0400775 if (use_optimized_scaler && has_optimized_scaler &&
Satheesh Kumar31768902023-08-30 10:16:10 +0530776 cm->seq_params->bit_depth == AOM_BITS_8) {
Jayasanker Jdee179d2020-07-10 23:47:37 +0530777 av1_resize_and_extend_frame(ref, &new_fb->buf, filter, phase,
778 num_planes);
Satheesh Kumar31768902023-08-30 10:16:10 +0530779 } else if (!av1_resize_and_extend_frame_nonnormative(
780 ref, &new_fb->buf, (int)cm->seq_params->bit_depth,
781 num_planes)) {
782 aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
783 "Failed to allocate buffer during resize");
784 }
Jayasanker Jdee179d2020-07-10 23:47:37 +0530785#else
Satheesh Kumar31768902023-08-30 10:16:10 +0530786 if (use_optimized_scaler && has_optimized_scaler) {
Jayasanker Jdee179d2020-07-10 23:47:37 +0530787 av1_resize_and_extend_frame(ref, &new_fb->buf, filter, phase,
788 num_planes);
Satheesh Kumar31768902023-08-30 10:16:10 +0530789 } else if (!av1_resize_and_extend_frame_nonnormative(
790 ref, &new_fb->buf, (int)cm->seq_params->bit_depth,
791 num_planes)) {
792 aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
793 "Failed to allocate buffer during resize");
794 }
Jayasanker Jdee179d2020-07-10 23:47:37 +0530795#endif
796 cpi->scaled_ref_buf[ref_frame - 1] = new_fb;
797 alloc_frame_mvs(cm, new_fb);
798 }
799 } else {
800 RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
801 buf->buf.y_crop_width = ref->y_crop_width;
802 buf->buf.y_crop_height = ref->y_crop_height;
803 cpi->scaled_ref_buf[ref_frame - 1] = buf;
804 ++buf->ref_count;
805 }
806 } else {
807 if (!has_no_stats_stage(cpi)) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
808 }
809 }
810}
811
Tarundeep Singh49128692021-04-07 12:37:21 +0530812BLOCK_SIZE av1_select_sb_size(const AV1EncoderConfig *const oxcf, int width,
813 int height, int number_spatial_layers) {
Sai Deng06a32d92021-12-13 14:49:17 -0800814 if (oxcf->tool_cfg.superblock_size == AOM_SUPERBLOCK_SIZE_64X64) {
Jayasanker Jdee179d2020-07-10 23:47:37 +0530815 return BLOCK_64X64;
Sai Deng06a32d92021-12-13 14:49:17 -0800816 }
817 if (oxcf->tool_cfg.superblock_size == AOM_SUPERBLOCK_SIZE_128X128) {
Jayasanker Jdee179d2020-07-10 23:47:37 +0530818 return BLOCK_128X128;
Sai Deng06a32d92021-12-13 14:49:17 -0800819 }
820#if CONFIG_TFLITE
821 if (oxcf->q_cfg.deltaq_mode == DELTA_Q_USER_RATING_BASED) return BLOCK_64X64;
822#endif
Jingning Han2e92bd52021-06-28 11:20:37 -0700823 // Force 64x64 superblock size to increase resolution in perceptual
824 // AQ mode.
825 if (oxcf->mode == ALLINTRA &&
Sai Deng039ef512021-08-03 10:13:43 -0700826 (oxcf->q_cfg.deltaq_mode == DELTA_Q_PERCEPTUAL_AI ||
Sai Deng06a32d92021-12-13 14:49:17 -0800827 oxcf->q_cfg.deltaq_mode == DELTA_Q_USER_RATING_BASED)) {
Jingning Han2e92bd52021-06-28 11:20:37 -0700828 return BLOCK_64X64;
Sai Deng06a32d92021-12-13 14:49:17 -0800829 }
Jayasanker Jdee179d2020-07-10 23:47:37 +0530830 assert(oxcf->tool_cfg.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
831
Tarundeep Singh49128692021-04-07 12:37:21 +0530832 if (number_spatial_layers > 1 ||
Jayasanker Jdee179d2020-07-10 23:47:37 +0530833 oxcf->resize_cfg.resize_mode != RESIZE_NONE) {
834 // Use the configured size (top resolution) for spatial layers or
835 // on resize.
Marco Paniconia7684752022-04-13 15:18:53 -0700836 return AOMMIN(oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height) > 720
Jayasanker Jdee179d2020-07-10 23:47:37 +0530837 ? BLOCK_128X128
838 : BLOCK_64X64;
Marco Paniconia7684752022-04-13 15:18:53 -0700839 } else if (oxcf->mode == REALTIME) {
Marco Paniconi9ad7c3c2024-05-29 12:27:22 -0700840 return AOMMIN(width, height) > 720 ? BLOCK_128X128 : BLOCK_64X64;
Jayasanker Jdee179d2020-07-10 23:47:37 +0530841 }
842
843 // TODO(any): Possibly could improve this with a heuristic.
844 // When superres / resize is on, 'cm->width / height' can change between
845 // calls, so we don't apply this heuristic there.
846 // Things break if superblock size changes between the first pass and second
847 // pass encoding, which is why this heuristic is not configured as a
848 // speed-feature.
849 if (oxcf->superres_cfg.superres_mode == AOM_SUPERRES_NONE &&
Remya Prakasan22e84922021-10-25 19:48:53 +0530850 oxcf->resize_cfg.resize_mode == RESIZE_NONE) {
851 int is_480p_or_lesser = AOMMIN(width, height) <= 480;
chiyotsaib6daca72022-05-25 11:01:09 -0700852 if (oxcf->speed >= 1 && is_480p_or_lesser) return BLOCK_64X64;
Jayasanker Jdee179d2020-07-10 23:47:37 +0530853
Remya Prakasan22e84922021-10-25 19:48:53 +0530854 // For 1080p and lower resolutions, choose SB size adaptively based on
855 // resolution and speed level for multi-thread encode.
856 int is_1080p_or_lesser = AOMMIN(width, height) <= 1080;
857 if (!is_480p_or_lesser && is_1080p_or_lesser && oxcf->mode == GOOD &&
858 oxcf->row_mt == 1 && oxcf->max_threads > 1 && oxcf->speed >= 5)
859 return BLOCK_64X64;
Mudassir Galaganath4a7863b2023-01-04 19:03:44 +0530860
861 // For allintra encode, since the maximum partition size is set to 32X32 for
862 // speed>=6, superblock size is set to 64X64 instead of 128X128. This
863 // improves the multithread performance due to reduction in top right delay
864 // and thread sync wastage. Currently, this setting is selectively enabled
865 // only for speed>=9 and resolutions less than 4k since cost update
866 // frequency is set to INTERNAL_COST_UPD_OFF in these cases.
867 const int is_4k_or_larger = AOMMIN(width, height) >= 2160;
868 if (oxcf->mode == ALLINTRA && oxcf->speed >= 9 && !is_4k_or_larger)
869 return BLOCK_64X64;
Remya Prakasan22e84922021-10-25 19:48:53 +0530870 }
Jayasanker Jdee179d2020-07-10 23:47:37 +0530871 return BLOCK_128X128;
872}
873
874void av1_setup_frame(AV1_COMP *cpi) {
875 AV1_COMMON *const cm = &cpi->common;
876 // Set up entropy context depending on frame type. The decoder mandates
877 // the use of the default context, index 0, for keyframes and inter
878 // frames where the error_resilient_mode or intra_only flag is set. For
879 // other inter-frames the encoder currently uses only two contexts;
880 // context 1 for ALTREF frames and context 0 for the others.
881
882 if (frame_is_intra_only(cm) || cm->features.error_resilient_mode ||
883 cpi->ext_flags.use_primary_ref_none) {
884 av1_setup_past_independence(cm);
885 }
886
887 if ((cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) ||
888 frame_is_sframe(cm)) {
Tarundeep Singh02091352021-03-12 01:22:08 +0530889 if (!cpi->ppi->seq_params_locked) {
Tarundeep Singh4243e622021-04-20 16:10:22 +0530890 set_sb_size(cm->seq_params,
Tarundeep Singh49128692021-04-07 12:37:21 +0530891 av1_select_sb_size(&cpi->oxcf, cm->width, cm->height,
Yuan Tong0189e1a2023-02-09 14:06:42 +0800892 cpi->ppi->number_spatial_layers));
Jayasanker Jdee179d2020-07-10 23:47:37 +0530893 }
894 } else {
895 const RefCntBuffer *const primary_ref_buf = get_primary_ref_frame_buf(cm);
896 if (primary_ref_buf == NULL) {
897 av1_setup_past_independence(cm);
898 cm->seg.update_map = 1;
899 cm->seg.update_data = 1;
900 } else {
901 *cm->fc = primary_ref_buf->frame_context;
902 }
903 }
904
905 av1_zero(cm->cur_frame->interp_filter_selected);
906 cm->prev_frame = get_primary_ref_frame_buf(cm);
907 cpi->vaq_refresh = 0;
908}
909
910#if !CONFIG_REALTIME_ONLY
911static int get_interp_filter_selected(const AV1_COMMON *const cm,
912 MV_REFERENCE_FRAME ref,
913 InterpFilter ifilter) {
914 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
915 if (buf == NULL) return 0;
916 return buf->interp_filter_selected[ifilter];
917}
918
919uint16_t av1_setup_interp_filter_search_mask(AV1_COMP *cpi) {
920 const AV1_COMMON *const cm = &cpi->common;
921 int ref_total[REF_FRAMES] = { 0 };
922 uint16_t mask = ALLOW_ALL_INTERP_FILT_MASK;
923
924 if (cpi->last_frame_type == KEY_FRAME || cpi->refresh_frame.alt_ref_frame)
925 return mask;
926
927 for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
928 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
929 ++ifilter) {
930 ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter);
931 }
932 }
933 int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] +
934 ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] +
935 ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]);
936
937 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
938 ++ifilter) {
939 int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30;
940 if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) {
941 int filter_score =
942 get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 +
943 get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 +
944 get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 +
945 get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 +
946 get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 +
947 get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10;
948 if (filter_score < ref_total_total) {
949 DUAL_FILTER_TYPE filt_type = ifilter + SWITCHABLE_FILTERS * ifilter;
950 reset_interp_filter_allowed_mask(&mask, filt_type);
951 }
952 }
953 }
954 return mask;
955}
956
957#define STRICT_PSNR_DIFF_THRESH 0.9
958// Encode key frame with/without screen content tools to determine whether
959// screen content tools should be enabled for this key frame group or not.
960// The first encoding is without screen content tools.
961// The second encoding is with screen content tools.
962// We compare the psnr and frame size to make the decision.
963static void screen_content_tools_determination(
964 AV1_COMP *cpi, const int allow_screen_content_tools_orig_decision,
965 const int allow_intrabc_orig_decision,
Cheng Chenfd883dc2020-08-14 15:38:16 -0700966 const int use_screen_content_tools_orig_decision,
Jayasanker Jdee179d2020-07-10 23:47:37 +0530967 const int is_screen_content_type_orig_decision, const int pass,
968 int *projected_size_pass, PSNR_STATS *psnr) {
969 AV1_COMMON *const cm = &cpi->common;
970 FeatureFlags *const features = &cm->features;
Remya Prakasan6566bc82021-11-05 23:21:12 +0530971
Remya Prakasanffeb4972022-06-21 20:00:28 +0530972#if CONFIG_FPMT_TEST
Remya Prakasan6566bc82021-11-05 23:21:12 +0530973 projected_size_pass[pass] =
974 ((cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) &&
975 (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE))
976 ? cpi->ppi->p_rc.temp_projected_frame_size
977 : cpi->rc.projected_frame_size;
978#else
Aasaipriya65e5e9e2021-08-12 11:31:27 +0530979 projected_size_pass[pass] = cpi->rc.projected_frame_size;
Remya Prakasan6566bc82021-11-05 23:21:12 +0530980#endif
981
Jayasanker Jdee179d2020-07-10 23:47:37 +0530982#if CONFIG_AV1_HIGHBITDEPTH
983 const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth;
984 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
985 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr[pass],
986 bit_depth, in_bit_depth);
987#else
988 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr[pass]);
989#endif
990 if (pass != 1) return;
991
992 const double psnr_diff = psnr[1].psnr[0] - psnr[0].psnr[0];
Lin Zheng4c3c2c12023-03-02 01:49:58 +0000993 // Calculate % of palette mode to be chosen in a frame from mode decision.
994 const double palette_ratio =
995 (double)cpi->palette_pixel_num / (double)(cm->height * cm->width);
Lin Zhengd32006b2023-03-22 13:28:44 -0700996 const int psnr_diff_is_large = (psnr_diff > STRICT_PSNR_DIFF_THRESH);
997 const int ratio_is_large =
998 ((palette_ratio >= 0.0001) && ((psnr_diff / palette_ratio) > 4));
999 const int is_sc_encoding_much_better = (psnr_diff_is_large || ratio_is_large);
Jayasanker Jdee179d2020-07-10 23:47:37 +05301000 if (is_sc_encoding_much_better) {
1001 // Use screen content tools, if we get coding gain.
1002 features->allow_screen_content_tools = 1;
1003 features->allow_intrabc = cpi->intrabc_used;
Cheng Chenfd883dc2020-08-14 15:38:16 -07001004 cpi->use_screen_content_tools = 1;
Jayasanker Jdee179d2020-07-10 23:47:37 +05301005 cpi->is_screen_content_type = 1;
1006 } else {
1007 // Use original screen content decision.
1008 features->allow_screen_content_tools =
1009 allow_screen_content_tools_orig_decision;
1010 features->allow_intrabc = allow_intrabc_orig_decision;
Cheng Chenfd883dc2020-08-14 15:38:16 -07001011 cpi->use_screen_content_tools = use_screen_content_tools_orig_decision;
Jayasanker Jdee179d2020-07-10 23:47:37 +05301012 cpi->is_screen_content_type = is_screen_content_type_orig_decision;
1013 }
1014}
1015
1016// Set some encoding parameters to make the encoding process fast.
1017// A fixed block partition size, and a large q is used.
1018static void set_encoding_params_for_screen_content(AV1_COMP *cpi,
1019 const int pass) {
1020 AV1_COMMON *const cm = &cpi->common;
1021 if (pass == 0) {
1022 // In the first pass, encode without screen content tools.
1023 // Use a high q, and a fixed block size for fast encoding.
1024 cm->features.allow_screen_content_tools = 0;
1025 cm->features.allow_intrabc = 0;
Cheng Chenfd883dc2020-08-14 15:38:16 -07001026 cpi->use_screen_content_tools = 0;
Jayasanker Jdee179d2020-07-10 23:47:37 +05301027 cpi->sf.part_sf.partition_search_type = FIXED_PARTITION;
1028 cpi->sf.part_sf.fixed_partition_size = BLOCK_32X32;
1029 return;
1030 }
1031 assert(pass == 1);
1032 // In the second pass, encode with screen content tools.
1033 // Use a high q, and a fixed block size for fast encoding.
1034 cm->features.allow_screen_content_tools = 1;
1035 // TODO(chengchen): turn intrabc on could lead to data race issue.
1036 // cm->allow_intrabc = 1;
Cheng Chenfd883dc2020-08-14 15:38:16 -07001037 cpi->use_screen_content_tools = 1;
Jayasanker Jdee179d2020-07-10 23:47:37 +05301038 cpi->sf.part_sf.partition_search_type = FIXED_PARTITION;
1039 cpi->sf.part_sf.fixed_partition_size = BLOCK_32X32;
1040}
1041
1042// Determines whether to use screen content tools for the key frame group.
1043// This function modifies "cm->features.allow_screen_content_tools",
Cheng Chenfd883dc2020-08-14 15:38:16 -07001044// "cm->features.allow_intrabc" and "cpi->use_screen_content_tools".
Jayasanker Jdee179d2020-07-10 23:47:37 +05301045void av1_determine_sc_tools_with_encoding(AV1_COMP *cpi, const int q_orig) {
1046 AV1_COMMON *const cm = &cpi->common;
1047 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1048 const QuantizationCfg *const q_cfg = &oxcf->q_cfg;
1049 // Variables to help determine if we should allow screen content tools.
1050 int projected_size_pass[3] = { 0 };
1051 PSNR_STATS psnr[3];
1052 const int is_key_frame = cm->current_frame.frame_type == KEY_FRAME;
1053 const int allow_screen_content_tools_orig_decision =
1054 cm->features.allow_screen_content_tools;
1055 const int allow_intrabc_orig_decision = cm->features.allow_intrabc;
Cheng Chenfd883dc2020-08-14 15:38:16 -07001056 const int use_screen_content_tools_orig_decision =
1057 cpi->use_screen_content_tools;
Jayasanker Jdee179d2020-07-10 23:47:37 +05301058 const int is_screen_content_type_orig_decision = cpi->is_screen_content_type;
1059 // Turn off the encoding trial for forward key frame and superres.
1060 if (cpi->sf.rt_sf.use_nonrd_pick_mode || oxcf->kf_cfg.fwd_kf_enabled ||
1061 cpi->superres_mode != AOM_SUPERRES_NONE || oxcf->mode == REALTIME ||
Cheng Chenfd883dc2020-08-14 15:38:16 -07001062 use_screen_content_tools_orig_decision || !is_key_frame) {
Jayasanker Jdee179d2020-07-10 23:47:37 +05301063 return;
1064 }
1065
1066 // TODO(chengchen): multiple encoding for the lossless mode is time consuming.
1067 // Find a better way to determine whether screen content tools should be used
1068 // for lossless coding.
1069 // Use a high q and a fixed partition to do quick encoding.
1070 const int q_for_screen_content_quick_run =
1071 is_lossless_requested(&oxcf->rc_cfg) ? q_orig : AOMMAX(q_orig, 244);
1072 const int partition_search_type_orig = cpi->sf.part_sf.partition_search_type;
1073 const BLOCK_SIZE fixed_partition_block_size_orig =
1074 cpi->sf.part_sf.fixed_partition_size;
1075
1076 // Setup necessary params for encoding, including frame source, etc.
Jayasanker Jdee179d2020-07-10 23:47:37 +05301077
Jayasanker Jdba4f0f2021-07-27 22:45:35 +05301078 cpi->source = av1_realloc_and_scale_if_required(
1079 cm, cpi->unscaled_source, &cpi->scaled_source, cm->features.interp_filter,
Rachel Barker72e349e2024-02-13 14:31:10 +00001080 0, false, false, cpi->oxcf.border_in_pixels, cpi->alloc_pyramid);
Jayasanker Jdee179d2020-07-10 23:47:37 +05301081 if (cpi->unscaled_last_source != NULL) {
Jayasanker Jdba4f0f2021-07-27 22:45:35 +05301082 cpi->last_source = av1_realloc_and_scale_if_required(
Urvang Joshi708a34f2020-10-06 16:05:26 -07001083 cm, cpi->unscaled_last_source, &cpi->scaled_last_source,
Jayasanker Jdba4f0f2021-07-27 22:45:35 +05301084 cm->features.interp_filter, 0, false, false, cpi->oxcf.border_in_pixels,
Rachel Barker72e349e2024-02-13 14:31:10 +00001085 cpi->alloc_pyramid);
Jayasanker Jdee179d2020-07-10 23:47:37 +05301086 }
1087
1088 av1_setup_frame(cpi);
1089
1090 if (cm->seg.enabled) {
1091 if (!cm->seg.update_data && cm->prev_frame) {
1092 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
1093 cm->seg.enabled = cm->prev_frame->seg.enabled;
1094 } else {
1095 av1_calculate_segdata(&cm->seg);
1096 }
1097 } else {
1098 memset(&cm->seg, 0, sizeof(cm->seg));
1099 }
1100 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
1101 cm->cur_frame->seg.enabled = cm->seg.enabled;
1102
1103 // The two encoding passes aim to help determine whether to use screen
1104 // content tools, with a high q and fixed partition.
1105 for (int pass = 0; pass < 2; ++pass) {
1106 set_encoding_params_for_screen_content(cpi, pass);
Sai Deng14a02942020-08-13 15:30:50 -07001107 av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel,
1108 q_for_screen_content_quick_run,
Ryan Leib89e8f82021-09-26 15:10:32 -07001109 q_cfg->enable_chroma_deltaq, q_cfg->enable_hdr_deltaq);
Jayasanker Jdee179d2020-07-10 23:47:37 +05301110 av1_set_speed_features_qindex_dependent(cpi, oxcf->speed);
Apurve Kumar Pandey917568c2022-12-16 16:27:34 +05301111 av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
1112 cm->seq_params->bit_depth);
Jayasanker Jdee179d2020-07-10 23:47:37 +05301113
1114 av1_set_variance_partition_thresholds(cpi, q_for_screen_content_quick_run,
1115 0);
1116 // transform / motion compensation build reconstruction frame
1117 av1_encode_frame(cpi);
1118 // Screen content decision
1119 screen_content_tools_determination(
1120 cpi, allow_screen_content_tools_orig_decision,
Cheng Chenfd883dc2020-08-14 15:38:16 -07001121 allow_intrabc_orig_decision, use_screen_content_tools_orig_decision,
1122 is_screen_content_type_orig_decision, pass, projected_size_pass, psnr);
Jayasanker Jdee179d2020-07-10 23:47:37 +05301123 }
1124
1125 // Set partition speed feature back.
1126 cpi->sf.part_sf.partition_search_type = partition_search_type_orig;
1127 cpi->sf.part_sf.fixed_partition_size = fixed_partition_block_size_orig;
Jayasanker J92cca7a2021-10-27 15:05:52 +05301128
1129 // Free token related info if screen content coding tools are not enabled.
1130 if (!cm->features.allow_screen_content_tools)
1131 free_token_info(&cpi->token_info);
Jayasanker Jdee179d2020-07-10 23:47:37 +05301132}
Jayasanker Jdee179d2020-07-10 23:47:37 +05301133#endif // CONFIG_REALTIME_ONLY
1134
1135static void fix_interp_filter(InterpFilter *const interp_filter,
1136 const FRAME_COUNTS *const counts) {
1137 if (*interp_filter == SWITCHABLE) {
1138 // Check to see if only one of the filters is actually used
1139 int count[SWITCHABLE_FILTERS] = { 0 };
1140 int num_filters_used = 0;
1141 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
1142 for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
1143 count[i] += counts->switchable_interp[j][i];
1144 num_filters_used += (count[i] > 0);
1145 }
1146 if (num_filters_used == 1) {
1147 // Only one filter is used. So set the filter at frame level
1148 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
1149 if (count[i]) {
Yunqing Wangea726392023-01-18 15:41:16 -08001150 *interp_filter = i;
Jayasanker Jdee179d2020-07-10 23:47:37 +05301151 break;
1152 }
1153 }
1154 }
1155 }
1156}
1157
1158void av1_finalize_encoded_frame(AV1_COMP *const cpi) {
1159 AV1_COMMON *const cm = &cpi->common;
1160 CurrentFrame *const current_frame = &cm->current_frame;
1161
Tarundeep Singh4243e622021-04-20 16:10:22 +05301162 if (!cm->seq_params->reduced_still_picture_hdr &&
Jayasanker Jdee179d2020-07-10 23:47:37 +05301163 encode_show_existing_frame(cm)) {
1164 RefCntBuffer *const frame_to_show =
1165 cm->ref_frame_map[cpi->existing_fb_idx_to_show];
1166
1167 if (frame_to_show == NULL) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05301168 aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Jayasanker Jdee179d2020-07-10 23:47:37 +05301169 "Buffer does not contain a reconstructed frame");
1170 }
1171 assert(frame_to_show->ref_count > 0);
1172 assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
1173 }
1174
1175 if (!encode_show_existing_frame(cm) &&
Tarundeep Singh4243e622021-04-20 16:10:22 +05301176 cm->seq_params->film_grain_params_present &&
Jayasanker Jdee179d2020-07-10 23:47:37 +05301177 (cm->show_frame || cm->showable_frame)) {
1178 // Copy the current frame's film grain params to the its corresponding
1179 // RefCntBuffer slot.
1180 cm->cur_frame->film_grain_params = cm->film_grain_params;
1181
1182 // We must update the parameters if this is not an INTER_FRAME
1183 if (current_frame->frame_type != INTER_FRAME)
1184 cm->cur_frame->film_grain_params.update_parameters = 1;
1185
1186 // Iterate the random seed for the next frame.
1187 cm->film_grain_params.random_seed += 3381;
1188 if (cm->film_grain_params.random_seed == 0)
1189 cm->film_grain_params.random_seed = 7391;
1190 }
1191
1192 // Initialise all tiles' contexts from the global frame context
1193 for (int tile_col = 0; tile_col < cm->tiles.cols; tile_col++) {
1194 for (int tile_row = 0; tile_row < cm->tiles.rows; tile_row++) {
1195 const int tile_idx = tile_row * cm->tiles.cols + tile_col;
1196 cpi->tile_data[tile_idx].tctx = *cm->fc;
1197 }
1198 }
1199
Yunqing Wangea726392023-01-18 15:41:16 -08001200 if (!frame_is_intra_only(cm))
1201 fix_interp_filter(&cm->features.interp_filter, cpi->td.counts);
Jayasanker Jdee179d2020-07-10 23:47:37 +05301202}
1203
1204int av1_is_integer_mv(const YV12_BUFFER_CONFIG *cur_picture,
1205 const YV12_BUFFER_CONFIG *last_picture,
1206 ForceIntegerMVInfo *const force_intpel_info) {
Jayasanker Jdee179d2020-07-10 23:47:37 +05301207 // check use hash ME
1208 int k;
1209
1210 const int block_size = FORCE_INT_MV_DECISION_BLOCK_SIZE;
1211 const double threshold_current = 0.8;
1212 const double threshold_average = 0.95;
1213 const int max_history_size = 32;
1214 int T = 0; // total block
1215 int C = 0; // match with collocated block
1216 int S = 0; // smooth region but not match with collocated block
1217
1218 const int pic_width = cur_picture->y_width;
1219 const int pic_height = cur_picture->y_height;
1220 for (int i = 0; i + block_size <= pic_height; i += block_size) {
1221 for (int j = 0; j + block_size <= pic_width; j += block_size) {
1222 const int x_pos = j;
1223 const int y_pos = i;
1224 int match = 1;
1225 T++;
1226
1227 // check whether collocated block match with current
1228 uint8_t *p_cur = cur_picture->y_buffer;
1229 uint8_t *p_ref = last_picture->y_buffer;
1230 int stride_cur = cur_picture->y_stride;
1231 int stride_ref = last_picture->y_stride;
1232 p_cur += (y_pos * stride_cur + x_pos);
1233 p_ref += (y_pos * stride_ref + x_pos);
1234
1235 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
1236 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
1237 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
1238 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
1239 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
1240 if (p16_cur[tmpX] != p16_ref[tmpX]) {
1241 match = 0;
1242 }
1243 }
1244 p16_cur += stride_cur;
1245 p16_ref += stride_ref;
1246 }
1247 } else {
1248 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
1249 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
1250 if (p_cur[tmpX] != p_ref[tmpX]) {
1251 match = 0;
1252 }
1253 }
1254 p_cur += stride_cur;
1255 p_ref += stride_ref;
1256 }
1257 }
1258
1259 if (match) {
1260 C++;
1261 continue;
1262 }
1263
1264 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
1265 y_pos) ||
1266 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
1267 S++;
1268 continue;
1269 }
1270 }
1271 }
1272
1273 assert(T > 0);
1274 double cs_rate = ((double)(C + S)) / ((double)(T));
1275
1276 force_intpel_info->cs_rate_array[force_intpel_info->rate_index] = cs_rate;
1277
1278 force_intpel_info->rate_index =
1279 (force_intpel_info->rate_index + 1) % max_history_size;
1280 force_intpel_info->rate_size++;
1281 force_intpel_info->rate_size =
1282 AOMMIN(force_intpel_info->rate_size, max_history_size);
1283
1284 if (cs_rate < threshold_current) {
1285 return 0;
1286 }
1287
1288 if (C == T) {
1289 return 1;
1290 }
1291
1292 double cs_average = 0.0;
1293
1294 for (k = 0; k < force_intpel_info->rate_size; k++) {
1295 cs_average += force_intpel_info->cs_rate_array[k];
1296 }
1297 cs_average /= force_intpel_info->rate_size;
1298
1299 if (cs_average < threshold_average) {
1300 return 0;
1301 }
1302
1303 if ((T - C - S) < 0) {
1304 return 1;
1305 }
1306
1307 if (cs_average > 1.01) {
1308 return 1;
1309 }
1310
1311 return 0;
1312}
1313
1314void av1_set_mb_ssim_rdmult_scaling(AV1_COMP *cpi) {
1315 const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
Mudassir Galagnathcd33f5f2022-07-15 12:17:07 +05301316 const MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Jayasanker Jdee179d2020-07-10 23:47:37 +05301317 uint8_t *y_buffer = cpi->source->y_buffer;
1318 const int y_stride = cpi->source->y_stride;
1319 const int block_size = BLOCK_16X16;
1320
1321 const int num_mi_w = mi_size_wide[block_size];
1322 const int num_mi_h = mi_size_high[block_size];
1323 const int num_cols = (mi_params->mi_cols + num_mi_w - 1) / num_mi_w;
1324 const int num_rows = (mi_params->mi_rows + num_mi_h - 1) / num_mi_h;
1325 double log_sum = 0.0;
Jayasanker Jdee179d2020-07-10 23:47:37 +05301326
1327 // Loop through each 16x16 block.
1328 for (int row = 0; row < num_rows; ++row) {
1329 for (int col = 0; col < num_cols; ++col) {
1330 double var = 0.0, num_of_var = 0.0;
1331 const int index = row * num_cols + col;
1332
1333 // Loop through each 8x8 block.
1334 for (int mi_row = row * num_mi_h;
1335 mi_row < mi_params->mi_rows && mi_row < (row + 1) * num_mi_h;
1336 mi_row += 2) {
1337 for (int mi_col = col * num_mi_w;
1338 mi_col < mi_params->mi_cols && mi_col < (col + 1) * num_mi_w;
1339 mi_col += 2) {
1340 struct buf_2d buf;
1341 const int row_offset_y = mi_row << 2;
1342 const int col_offset_y = mi_col << 2;
1343
1344 buf.buf = y_buffer + row_offset_y * y_stride + col_offset_y;
1345 buf.stride = y_stride;
1346
Mudassir Galagnath46739722022-07-22 00:35:17 +05301347 var += av1_get_perpixel_variance_facade(cpi, xd, &buf, BLOCK_8X8,
1348 AOM_PLANE_Y);
Jayasanker Jdee179d2020-07-10 23:47:37 +05301349 num_of_var += 1.0;
1350 }
1351 }
1352 var = var / num_of_var;
1353
1354 // Curve fitting with an exponential model on all 16x16 blocks from the
1355 // midres dataset.
1356 var = 67.035434 * (1 - exp(-0.0021489 * var)) + 17.492222;
Apurve Kumar Pandey175bb562023-01-23 10:32:41 +05301357
1358 // As per the above computation, var will be in the range of
1359 // [17.492222, 84.527656], assuming the data type is of infinite
1360 // precision. The following assert conservatively checks if var is in the
1361 // range of [17.0, 85.0] to avoid any issues due to the precision of the
1362 // relevant data type.
1363 assert(var > 17.0 && var < 85.0);
Jayasanker Jdee179d2020-07-10 23:47:37 +05301364 cpi->ssim_rdmult_scaling_factors[index] = var;
1365 log_sum += log(var);
1366 }
1367 }
Apurve Kumar Pandey175bb562023-01-23 10:32:41 +05301368
1369 // As log_sum holds the geometric mean, it will be in the range
1370 // [17.492222, 84.527656]. Hence, in the below loop, the value of
1371 // cpi->ssim_rdmult_scaling_factors[index] would be in the range
1372 // [0.2069, 4.8323].
Jayasanker Jdee179d2020-07-10 23:47:37 +05301373 log_sum = exp(log_sum / (double)(num_rows * num_cols));
1374
1375 for (int row = 0; row < num_rows; ++row) {
1376 for (int col = 0; col < num_cols; ++col) {
1377 const int index = row * num_cols + col;
1378 cpi->ssim_rdmult_scaling_factors[index] /= log_sum;
1379 }
1380 }
1381}
1382
Jayasanker Jdee179d2020-07-10 23:47:37 +05301383// Coding context that only needs to be saved when recode loop includes
1384// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
1385// restoraton).
1386static void save_extra_coding_context(AV1_COMP *cpi) {
1387 CODING_CONTEXT *const cc = &cpi->coding_context;
1388 AV1_COMMON *cm = &cpi->common;
1389
1390 cc->lf = cm->lf;
1391 cc->cdef_info = cm->cdef_info;
1392 cc->rc = cpi->rc;
Aasaipriya Chandranbe8ccad2021-05-25 20:37:01 +05301393 cc->mv_stats = cpi->ppi->mv_stats;
Jayasanker Jdee179d2020-07-10 23:47:37 +05301394}
1395
1396void av1_save_all_coding_context(AV1_COMP *cpi) {
Jayasanker Jdee179d2020-07-10 23:47:37 +05301397 save_extra_coding_context(cpi);
1398 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
1399}
Jayasanker Jdee179d2020-07-10 23:47:37 +05301400
1401#if DUMP_RECON_FRAMES == 1
1402
1403// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
1404void av1_dump_filtered_recon_frames(AV1_COMP *cpi) {
1405 AV1_COMMON *const cm = &cpi->common;
1406 const CurrentFrame *const current_frame = &cm->current_frame;
1407 const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf;
1408
1409 if (recon_buf == NULL) {
1410 printf("Frame %d is not ready.\n", current_frame->frame_number);
1411 return;
1412 }
1413
1414 static const int flag_list[REF_FRAMES] = { 0,
1415 AOM_LAST_FLAG,
1416 AOM_LAST2_FLAG,
1417 AOM_LAST3_FLAG,
1418 AOM_GOLD_FLAG,
1419 AOM_BWD_FLAG,
1420 AOM_ALT2_FLAG,
1421 AOM_ALT_FLAG };
1422 printf(
1423 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
1424 "show_existing_frame=%d) "
1425 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
1426 current_frame->frame_number, current_frame->order_hint, cm->show_frame,
1427 cm->show_existing_frame);
1428 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
1429 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
1430 const int ref_offset = buf != NULL ? (int)buf->order_hint : -1;
1431 printf(" %d(%c)", ref_offset,
1432 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N');
1433 }
1434 printf(" ]\n");
1435
1436 if (!cm->show_frame) {
1437 printf("Frame %d is a no show frame, so no image dump.\n",
1438 current_frame->frame_number);
1439 return;
1440 }
1441
1442 int h;
1443 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
1444 FILE *f_recon = NULL;
1445
1446 if (current_frame->frame_number == 0) {
1447 if ((f_recon = fopen(file_name, "wb")) == NULL) {
1448 printf("Unable to open file %s to write.\n", file_name);
1449 return;
1450 }
1451 } else {
1452 if ((f_recon = fopen(file_name, "ab")) == NULL) {
1453 printf("Unable to open file %s to append.\n", file_name);
1454 return;
1455 }
1456 }
1457 printf(
1458 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
1459 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
1460 "refresh_alt_ref_frame=%d, "
1461 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Mufaddal Chakeraab20d372021-03-17 12:18:34 +05301462 current_frame->frame_number, cpi->gf_frame_index,
Mufaddal Chakera8ee04fa2021-03-17 13:33:18 +05301463 cpi->ppi->gf_group.update_type[cpi->gf_frame_index],
1464 current_frame->order_hint, cm->show_frame, cm->show_existing_frame,
Mufaddal Chakera09b847f2021-04-20 07:11:24 +05301465 cpi->rc.source_alt_ref_active, cpi->refresh_frame.alt_ref_frame,
Mufaddal Chakera8ee04fa2021-03-17 13:33:18 +05301466 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
Jayasanker Jdee179d2020-07-10 23:47:37 +05301467#if 0
1468 int ref_frame;
1469 printf("get_ref_frame_map_idx: [");
1470 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
1471 printf(" %d", get_ref_frame_map_idx(cm, ref_frame));
1472 printf(" ]\n");
1473#endif // 0
1474
1475 // --- Y ---
1476 for (h = 0; h < cm->height; ++h) {
1477 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
1478 f_recon);
1479 }
1480 // --- U ---
1481 for (h = 0; h < (cm->height >> 1); ++h) {
1482 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
1483 f_recon);
1484 }
1485 // --- V ---
1486 for (h = 0; h < (cm->height >> 1); ++h) {
1487 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
1488 f_recon);
1489 }
1490
1491 fclose(f_recon);
1492}
1493#endif // DUMP_RECON_FRAMES