Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Copyright (c) 2023 Intel Corporation |
3 |
|
|
* |
4 |
|
|
* This file is part of FFmpeg. |
5 |
|
|
* |
6 |
|
|
* FFmpeg is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU Lesser General Public |
8 |
|
|
* License as published by the Free Software Foundation; either |
9 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* FFmpeg is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
|
|
* Lesser General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU Lesser General Public |
17 |
|
|
* License along with FFmpeg; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include <va/va.h> |
22 |
|
|
#include <va/va_enc_av1.h> |
23 |
|
|
|
24 |
|
|
#include "libavutil/pixdesc.h" |
25 |
|
|
#include "libavutil/opt.h" |
26 |
|
|
#include "libavutil/mastering_display_metadata.h" |
27 |
|
|
|
28 |
|
|
#include "cbs_av1.h" |
29 |
|
|
#include "put_bits.h" |
30 |
|
|
#include "codec_internal.h" |
31 |
|
|
#include "av1_levels.h" |
32 |
|
|
#include "vaapi_encode.h" |
33 |
|
|
|
34 |
|
|
#define AV1_MAX_QUANT 255 |
35 |
|
|
|
36 |
|
|
typedef struct VAAPIEncodeAV1Picture { |
37 |
|
|
int64_t last_idr_frame; |
38 |
|
|
int slot; |
39 |
|
|
} VAAPIEncodeAV1Picture; |
40 |
|
|
|
41 |
|
|
typedef struct VAAPIEncodeAV1Context { |
42 |
|
|
VAAPIEncodeContext common; |
43 |
|
|
AV1RawOBU sh; /**< sequence header.*/ |
44 |
|
|
AV1RawOBU fh; /**< frame header.*/ |
45 |
|
|
AV1RawOBU mh[4]; /**< metadata header.*/ |
46 |
|
|
int nb_mh; |
47 |
|
|
CodedBitstreamContext *cbc; |
48 |
|
|
CodedBitstreamFragment current_obu; |
49 |
|
|
VAConfigAttribValEncAV1 attr; |
50 |
|
|
VAConfigAttribValEncAV1Ext1 attr_ext1; |
51 |
|
|
VAConfigAttribValEncAV1Ext2 attr_ext2; |
52 |
|
|
|
53 |
|
|
char sh_data[MAX_PARAM_BUFFER_SIZE]; /**< coded sequence header data. */ |
54 |
|
|
size_t sh_data_len; /**< bit length of sh_data. */ |
55 |
|
|
char fh_data[MAX_PARAM_BUFFER_SIZE]; /**< coded frame header data. */ |
56 |
|
|
size_t fh_data_len; /**< bit length of fh_data. */ |
57 |
|
|
|
58 |
|
|
uint8_t uniform_tile; |
59 |
|
|
uint8_t use_128x128_superblock; |
60 |
|
|
int sb_cols; |
61 |
|
|
int sb_rows; |
62 |
|
|
int tile_cols_log2; |
63 |
|
|
int tile_rows_log2; |
64 |
|
|
int max_tile_width_sb; |
65 |
|
|
int max_tile_height_sb; |
66 |
|
|
uint8_t width_in_sbs_minus_1[AV1_MAX_TILE_COLS]; |
67 |
|
|
uint8_t height_in_sbs_minus_1[AV1_MAX_TILE_ROWS]; |
68 |
|
|
|
69 |
|
|
int min_log2_tile_cols; |
70 |
|
|
int max_log2_tile_cols; |
71 |
|
|
int min_log2_tile_rows; |
72 |
|
|
int max_log2_tile_rows; |
73 |
|
|
|
74 |
|
|
int q_idx_idr; |
75 |
|
|
int q_idx_p; |
76 |
|
|
int q_idx_b; |
77 |
|
|
|
78 |
|
|
/** bit positions in current frame header */ |
79 |
|
|
int qindex_offset; |
80 |
|
|
int loopfilter_offset; |
81 |
|
|
int cdef_start_offset; |
82 |
|
|
int cdef_param_size; |
83 |
|
|
|
84 |
|
|
/** user options */ |
85 |
|
|
int profile; |
86 |
|
|
int level; |
87 |
|
|
int tier; |
88 |
|
|
int tile_cols, tile_rows; |
89 |
|
|
int tile_groups; |
90 |
|
|
} VAAPIEncodeAV1Context; |
91 |
|
|
|
92 |
|
✗ |
static void vaapi_encode_av1_trace_write_log(void *ctx, |
93 |
|
|
PutBitContext *pbc, int length, |
94 |
|
|
const char *str, const int *subscripts, |
95 |
|
|
int64_t value) |
96 |
|
|
{ |
97 |
|
✗ |
VAAPIEncodeAV1Context *priv = ctx; |
98 |
|
|
int position; |
99 |
|
|
|
100 |
|
✗ |
position = put_bits_count(pbc); |
101 |
|
✗ |
av_assert0(position >= length); |
102 |
|
|
|
103 |
|
✗ |
if (!strcmp(str, "base_q_idx")) |
104 |
|
✗ |
priv->qindex_offset = position - length; |
105 |
|
✗ |
else if (!strcmp(str, "loop_filter_level[0]")) |
106 |
|
✗ |
priv->loopfilter_offset = position - length; |
107 |
|
✗ |
else if (!strcmp(str, "cdef_damping_minus_3")) |
108 |
|
✗ |
priv->cdef_start_offset = position - length; |
109 |
|
✗ |
else if (!strcmp(str, "cdef_uv_sec_strength[i]")) |
110 |
|
✗ |
priv->cdef_param_size = position - priv->cdef_start_offset; |
111 |
|
✗ |
} |
112 |
|
|
|
113 |
|
✗ |
static av_cold int vaapi_encode_av1_get_encoder_caps(AVCodecContext *avctx) |
114 |
|
|
{ |
115 |
|
✗ |
FFHWBaseEncodeContext *base_ctx = avctx->priv_data; |
116 |
|
✗ |
VAAPIEncodeAV1Context *priv = avctx->priv_data; |
117 |
|
|
|
118 |
|
|
// Surfaces must be aligned to superblock boundaries. |
119 |
|
✗ |
base_ctx->surface_width = FFALIGN(avctx->width, priv->use_128x128_superblock ? 128 : 64); |
120 |
|
✗ |
base_ctx->surface_height = FFALIGN(avctx->height, priv->use_128x128_superblock ? 128 : 64); |
121 |
|
|
|
122 |
|
✗ |
return 0; |
123 |
|
|
} |
124 |
|
|
|
125 |
|
✗ |
static av_cold int vaapi_encode_av1_configure(AVCodecContext *avctx) |
126 |
|
|
{ |
127 |
|
✗ |
VAAPIEncodeContext *ctx = avctx->priv_data; |
128 |
|
✗ |
VAAPIEncodeAV1Context *priv = avctx->priv_data; |
129 |
|
|
int ret; |
130 |
|
|
|
131 |
|
✗ |
ret = ff_cbs_init(&priv->cbc, AV_CODEC_ID_AV1, avctx); |
132 |
|
✗ |
if (ret < 0) |
133 |
|
✗ |
return ret; |
134 |
|
✗ |
priv->cbc->trace_enable = 1; |
135 |
|
✗ |
priv->cbc->trace_level = AV_LOG_DEBUG; |
136 |
|
✗ |
priv->cbc->trace_context = ctx; |
137 |
|
✗ |
priv->cbc->trace_write_callback = vaapi_encode_av1_trace_write_log; |
138 |
|
|
|
139 |
|
✗ |
if (ctx->rc_mode->quality) { |
140 |
|
✗ |
priv->q_idx_p = av_clip(ctx->rc_quality, 0, AV1_MAX_QUANT); |
141 |
|
✗ |
if (fabs(avctx->i_quant_factor) > 0.0) |
142 |
|
✗ |
priv->q_idx_idr = |
143 |
|
✗ |
av_clip((fabs(avctx->i_quant_factor) * priv->q_idx_p + |
144 |
|
✗ |
avctx->i_quant_offset) + 0.5, |
145 |
|
|
0, AV1_MAX_QUANT); |
146 |
|
|
else |
147 |
|
✗ |
priv->q_idx_idr = priv->q_idx_p; |
148 |
|
|
|
149 |
|
✗ |
if (fabs(avctx->b_quant_factor) > 0.0) |
150 |
|
✗ |
priv->q_idx_b = |
151 |
|
✗ |
av_clip((fabs(avctx->b_quant_factor) * priv->q_idx_p + |
152 |
|
✗ |
avctx->b_quant_offset) + 0.5, |
153 |
|
|
0, AV1_MAX_QUANT); |
154 |
|
|
else |
155 |
|
✗ |
priv->q_idx_b = priv->q_idx_p; |
156 |
|
|
} else { |
157 |
|
|
/** Arbitrary value */ |
158 |
|
✗ |
priv->q_idx_idr = priv->q_idx_p = priv->q_idx_b = 128; |
159 |
|
|
} |
160 |
|
|
|
161 |
|
✗ |
ctx->roi_quant_range = AV1_MAX_QUANT; |
162 |
|
|
|
163 |
|
✗ |
return 0; |
164 |
|
|
} |
165 |
|
|
|
166 |
|
✗ |
static int vaapi_encode_av1_add_obu(AVCodecContext *avctx, |
167 |
|
|
CodedBitstreamFragment *au, |
168 |
|
|
uint8_t type, |
169 |
|
|
void *obu_unit) |
170 |
|
|
{ |
171 |
|
|
int ret; |
172 |
|
|
|
173 |
|
✗ |
ret = ff_cbs_insert_unit_content(au, -1, |
174 |
|
|
type, obu_unit, NULL); |
175 |
|
✗ |
if (ret < 0) { |
176 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Failed to add OBU unit: " |
177 |
|
|
"type = %d.\n", type); |
178 |
|
✗ |
return ret; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
✗ |
return 0; |
182 |
|
|
} |
183 |
|
|
|
184 |
|
✗ |
static int vaapi_encode_av1_write_obu(AVCodecContext *avctx, |
185 |
|
|
char *data, size_t *data_len, |
186 |
|
|
CodedBitstreamFragment *bs) |
187 |
|
|
{ |
188 |
|
✗ |
VAAPIEncodeAV1Context *priv = avctx->priv_data; |
189 |
|
|
int ret; |
190 |
|
|
|
191 |
|
✗ |
ret = ff_cbs_write_fragment_data(priv->cbc, bs); |
192 |
|
✗ |
if (ret < 0) { |
193 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n"); |
194 |
|
✗ |
return ret; |
195 |
|
|
} |
196 |
|
|
|
197 |
|
✗ |
if ((size_t)8 * MAX_PARAM_BUFFER_SIZE < 8 * bs->data_size - bs->data_bit_padding) { |
198 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Access unit too large: " |
199 |
|
|
"%zu < %zu.\n", (size_t)8 * MAX_PARAM_BUFFER_SIZE, |
200 |
|
✗ |
8 * bs->data_size - bs->data_bit_padding); |
201 |
|
✗ |
return AVERROR(ENOSPC); |
202 |
|
|
} |
203 |
|
|
|
204 |
|
✗ |
memcpy(data, bs->data, bs->data_size); |
205 |
|
✗ |
*data_len = 8 * bs->data_size - bs->data_bit_padding; |
206 |
|
|
|
207 |
|
✗ |
return 0; |
208 |
|
|
} |
209 |
|
|
|
210 |
|
✗ |
static int tile_log2(int blkSize, int target) { |
211 |
|
|
int k; |
212 |
|
✗ |
for (k = 0; (blkSize << k) < target; k++); |
213 |
|
✗ |
return k; |
214 |
|
|
} |
215 |
|
|
|
216 |
|
✗ |
static int vaapi_encode_av1_set_tile(AVCodecContext *avctx) |
217 |
|
|
{ |
218 |
|
✗ |
VAAPIEncodeAV1Context *priv = avctx->priv_data; |
219 |
|
|
int mi_cols, mi_rows, sb_shift, sb_size; |
220 |
|
|
int max_tile_area_sb, max_tile_area_sb_varied; |
221 |
|
|
int tile_width_sb, tile_height_sb, widest_tile_sb; |
222 |
|
|
int tile_cols, tile_rows; |
223 |
|
|
int min_log2_tiles; |
224 |
|
|
int i; |
225 |
|
|
|
226 |
|
✗ |
if (priv->tile_cols > AV1_MAX_TILE_COLS || |
227 |
|
✗ |
priv->tile_rows > AV1_MAX_TILE_ROWS) { |
228 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Invalid tile number %dx%d, should less than %dx%d.\n", |
229 |
|
|
priv->tile_cols, priv->tile_rows, AV1_MAX_TILE_COLS, AV1_MAX_TILE_ROWS); |
230 |
|
✗ |
return AVERROR(EINVAL); |
231 |
|
|
} |
232 |
|
|
|
233 |
|
✗ |
mi_cols = 2 * ((avctx->width + 7) >> 3); |
234 |
|
✗ |
mi_rows = 2 * ((avctx->height + 7) >> 3); |
235 |
|
✗ |
priv->sb_cols = priv->use_128x128_superblock ? |
236 |
|
✗ |
((mi_cols + 31) >> 5) : ((mi_cols + 15) >> 4); |
237 |
|
✗ |
priv->sb_rows = priv->use_128x128_superblock ? |
238 |
|
✗ |
((mi_rows + 31) >> 5) : ((mi_rows + 15) >> 4); |
239 |
|
✗ |
sb_shift = priv->use_128x128_superblock ? 5 : 4; |
240 |
|
✗ |
sb_size = sb_shift + 2; |
241 |
|
✗ |
priv->max_tile_width_sb = AV1_MAX_TILE_WIDTH >> sb_size; |
242 |
|
✗ |
max_tile_area_sb = AV1_MAX_TILE_AREA >> (2 * sb_size); |
243 |
|
|
|
244 |
|
✗ |
priv->min_log2_tile_cols = tile_log2(priv->max_tile_width_sb, priv->sb_cols); |
245 |
|
✗ |
priv->max_log2_tile_cols = tile_log2(1, FFMIN(priv->sb_cols, AV1_MAX_TILE_COLS)); |
246 |
|
✗ |
priv->max_log2_tile_rows = tile_log2(1, FFMIN(priv->sb_rows, AV1_MAX_TILE_ROWS)); |
247 |
|
✗ |
min_log2_tiles = FFMAX(priv->min_log2_tile_cols, |
248 |
|
|
tile_log2(max_tile_area_sb, priv->sb_rows * priv->sb_cols)); |
249 |
|
|
|
250 |
|
✗ |
tile_cols = av_clip(priv->tile_cols, (priv->sb_cols + priv->max_tile_width_sb - 1) / priv->max_tile_width_sb, priv->sb_cols); |
251 |
|
|
|
252 |
|
✗ |
if (!priv->tile_cols) |
253 |
|
✗ |
priv->tile_cols = tile_cols; |
254 |
|
✗ |
else if (priv->tile_cols != tile_cols){ |
255 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Invalid tile cols %d, should be in range of %d~%d\n", |
256 |
|
|
priv->tile_cols, |
257 |
|
✗ |
(priv->sb_cols + priv->max_tile_width_sb - 1) / priv->max_tile_width_sb, |
258 |
|
|
priv->sb_cols); |
259 |
|
✗ |
return AVERROR(EINVAL); |
260 |
|
|
} |
261 |
|
|
|
262 |
|
✗ |
priv->tile_cols_log2 = tile_log2(1, priv->tile_cols); |
263 |
|
✗ |
tile_width_sb = (priv->sb_cols + (1 << priv->tile_cols_log2) - 1) >> |
264 |
|
✗ |
priv->tile_cols_log2; |
265 |
|
|
|
266 |
|
✗ |
if (priv->tile_rows > priv->sb_rows) { |
267 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Invalid tile rows %d, should be less than %d.\n", |
268 |
|
|
priv->tile_rows, priv->sb_rows); |
269 |
|
✗ |
return AVERROR(EINVAL); |
270 |
|
|
} |
271 |
|
|
|
272 |
|
|
/** Try user setting tile rows number first. */ |
273 |
|
✗ |
tile_rows = priv->tile_rows ? priv->tile_rows : 1; |
274 |
|
✗ |
for (; tile_rows <= priv->sb_rows && tile_rows <= AV1_MAX_TILE_ROWS; tile_rows++) { |
275 |
|
|
/** try uniformed tile. */ |
276 |
|
✗ |
priv->tile_rows_log2 = tile_log2(1, tile_rows); |
277 |
|
✗ |
if ((priv->sb_cols + tile_width_sb - 1) / tile_width_sb == priv->tile_cols) { |
278 |
|
✗ |
for (i = 0; i < priv->tile_cols - 1; i++) |
279 |
|
✗ |
priv->width_in_sbs_minus_1[i] = tile_width_sb - 1; |
280 |
|
✗ |
priv->width_in_sbs_minus_1[i] = priv->sb_cols - (priv->tile_cols - 1) * tile_width_sb - 1; |
281 |
|
|
|
282 |
|
✗ |
tile_height_sb = (priv->sb_rows + (1 << priv->tile_rows_log2) - 1) >> |
283 |
|
✗ |
priv->tile_rows_log2; |
284 |
|
|
|
285 |
|
✗ |
if ((priv->sb_rows + tile_height_sb - 1) / tile_height_sb == tile_rows && |
286 |
|
✗ |
tile_height_sb <= max_tile_area_sb / tile_width_sb) { |
287 |
|
✗ |
for (i = 0; i < tile_rows - 1; i++) |
288 |
|
✗ |
priv->height_in_sbs_minus_1[i] = tile_height_sb - 1; |
289 |
|
✗ |
priv->height_in_sbs_minus_1[i] = priv->sb_rows - (tile_rows - 1) * tile_height_sb - 1; |
290 |
|
|
|
291 |
|
✗ |
priv->uniform_tile = 1; |
292 |
|
✗ |
priv->min_log2_tile_rows = FFMAX(min_log2_tiles - priv->tile_cols_log2, 0); |
293 |
|
|
|
294 |
|
✗ |
break; |
295 |
|
|
} |
296 |
|
|
} |
297 |
|
|
|
298 |
|
|
/** try non-uniformed tile. */ |
299 |
|
✗ |
widest_tile_sb = 0; |
300 |
|
✗ |
for (i = 0; i < priv->tile_cols; i++) { |
301 |
|
✗ |
priv->width_in_sbs_minus_1[i] = (i + 1) * priv->sb_cols / priv->tile_cols - i * priv->sb_cols / priv->tile_cols - 1; |
302 |
|
✗ |
widest_tile_sb = FFMAX(widest_tile_sb, priv->width_in_sbs_minus_1[i] + 1); |
303 |
|
|
} |
304 |
|
|
|
305 |
|
✗ |
if (min_log2_tiles) |
306 |
|
✗ |
max_tile_area_sb_varied = (priv->sb_rows * priv->sb_cols) >> (min_log2_tiles + 1); |
307 |
|
|
else |
308 |
|
✗ |
max_tile_area_sb_varied = priv->sb_rows * priv->sb_cols; |
309 |
|
✗ |
priv->max_tile_height_sb = FFMAX(1, max_tile_area_sb_varied / widest_tile_sb); |
310 |
|
|
|
311 |
|
✗ |
if (tile_rows == av_clip(tile_rows, (priv->sb_rows + priv->max_tile_height_sb - 1) / priv->max_tile_height_sb, priv->sb_rows)) { |
312 |
|
✗ |
for (i = 0; i < tile_rows; i++) |
313 |
|
✗ |
priv->height_in_sbs_minus_1[i] = (i + 1) * priv->sb_rows / tile_rows - i * priv->sb_rows / tile_rows - 1; |
314 |
|
|
|
315 |
|
✗ |
break; |
316 |
|
|
} |
317 |
|
|
|
318 |
|
|
/** Return invalid parameter if explicit tile rows is set. */ |
319 |
|
✗ |
if (priv->tile_rows) { |
320 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Invalid tile rows %d.\n", priv->tile_rows); |
321 |
|
✗ |
return AVERROR(EINVAL); |
322 |
|
|
} |
323 |
|
|
} |
324 |
|
|
|
325 |
|
✗ |
priv->tile_rows = tile_rows; |
326 |
|
✗ |
av_log(avctx, AV_LOG_DEBUG, "Setting tile cols/rows to %d/%d.\n", |
327 |
|
|
priv->tile_cols, priv->tile_rows); |
328 |
|
|
|
329 |
|
|
/** check if tile cols/rows is supported by driver. */ |
330 |
|
✗ |
if (priv->attr_ext2.bits.max_tile_num_minus1) { |
331 |
|
✗ |
if ((priv->tile_cols * priv->tile_rows - 1) > priv->attr_ext2.bits.max_tile_num_minus1) { |
332 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Unsupported tile num %d * %d = %d by driver, " |
333 |
|
|
"should be at most %d.\n", priv->tile_cols, priv->tile_rows, |
334 |
|
✗ |
priv->tile_cols * priv->tile_rows, |
335 |
|
✗ |
priv->attr_ext2.bits.max_tile_num_minus1 + 1); |
336 |
|
✗ |
return AVERROR(EINVAL); |
337 |
|
|
} |
338 |
|
|
} |
339 |
|
|
|
340 |
|
|
/** check if tile group numbers is valid. */ |
341 |
|
✗ |
if (priv->tile_groups > priv->tile_cols * priv->tile_rows) { |
342 |
|
✗ |
av_log(avctx, AV_LOG_WARNING, "Invalid tile groups number %d, " |
343 |
|
✗ |
"correct to %d.\n", priv->tile_groups, priv->tile_cols * priv->tile_rows); |
344 |
|
✗ |
priv->tile_groups = priv->tile_cols * priv->tile_rows; |
345 |
|
|
} |
346 |
|
|
|
347 |
|
✗ |
return 0; |
348 |
|
|
} |
349 |
|
|
|
350 |
|
✗ |
static int vaapi_encode_av1_write_sequence_header(AVCodecContext *avctx, |
351 |
|
|
char *data, size_t *data_len) |
352 |
|
|
{ |
353 |
|
✗ |
VAAPIEncodeAV1Context *priv = avctx->priv_data; |
354 |
|
|
|
355 |
|
✗ |
memcpy(data, &priv->sh_data, MAX_PARAM_BUFFER_SIZE * sizeof(char)); |
356 |
|
✗ |
*data_len = priv->sh_data_len; |
357 |
|
|
|
358 |
|
✗ |
return 0; |
359 |
|
|
} |
360 |
|
|
|
361 |
|
✗ |
static int vaapi_encode_av1_init_sequence_params(AVCodecContext *avctx) |
362 |
|
|
{ |
363 |
|
✗ |
FFHWBaseEncodeContext *base_ctx = avctx->priv_data; |
364 |
|
✗ |
VAAPIEncodeContext *ctx = avctx->priv_data; |
365 |
|
✗ |
VAAPIEncodeAV1Context *priv = avctx->priv_data; |
366 |
|
✗ |
AV1RawOBU *sh_obu = &priv->sh; |
367 |
|
✗ |
AV1RawSequenceHeader *sh = &sh_obu->obu.sequence_header; |
368 |
|
✗ |
VAEncSequenceParameterBufferAV1 *vseq = ctx->codec_sequence_params; |
369 |
|
✗ |
CodedBitstreamFragment *obu = &priv->current_obu; |
370 |
|
|
const AVPixFmtDescriptor *desc; |
371 |
|
|
int ret; |
372 |
|
|
|
373 |
|
✗ |
memset(sh_obu, 0, sizeof(*sh_obu)); |
374 |
|
✗ |
sh_obu->header.obu_type = AV1_OBU_SEQUENCE_HEADER; |
375 |
|
|
|
376 |
|
✗ |
desc = av_pix_fmt_desc_get(base_ctx->input_frames->sw_format); |
377 |
|
✗ |
av_assert0(desc); |
378 |
|
|
|
379 |
|
✗ |
sh->seq_profile = avctx->profile; |
380 |
|
✗ |
if (!sh->seq_force_screen_content_tools) |
381 |
|
✗ |
sh->seq_force_integer_mv = AV1_SELECT_INTEGER_MV; |
382 |
|
✗ |
sh->frame_width_bits_minus_1 = av_log2(avctx->width); |
383 |
|
✗ |
sh->frame_height_bits_minus_1 = av_log2(avctx->height); |
384 |
|
✗ |
sh->max_frame_width_minus_1 = avctx->width - 1; |
385 |
|
✗ |
sh->max_frame_height_minus_1 = avctx->height - 1; |
386 |
|
✗ |
sh->seq_tier[0] = priv->tier; |
387 |
|
|
/** enable order hint and reserve maximum 8 bits for it by default. */ |
388 |
|
✗ |
sh->enable_order_hint = 1; |
389 |
|
✗ |
sh->order_hint_bits_minus_1 = 7; |
390 |
|
|
|
391 |
|
✗ |
sh->color_config = (AV1RawColorConfig) { |
392 |
|
✗ |
.high_bitdepth = desc->comp[0].depth == 8 ? 0 : 1, |
393 |
|
✗ |
.color_primaries = avctx->color_primaries, |
394 |
|
✗ |
.transfer_characteristics = avctx->color_trc, |
395 |
|
✗ |
.matrix_coefficients = avctx->colorspace, |
396 |
|
✗ |
.color_description_present_flag = (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED || |
397 |
|
✗ |
avctx->color_trc != AVCOL_TRC_UNSPECIFIED || |
398 |
|
✗ |
avctx->colorspace != AVCOL_SPC_UNSPECIFIED), |
399 |
|
✗ |
.color_range = avctx->color_range == AVCOL_RANGE_JPEG, |
400 |
|
✗ |
.subsampling_x = desc->log2_chroma_w, |
401 |
|
✗ |
.subsampling_y = desc->log2_chroma_h, |
402 |
|
|
}; |
403 |
|
|
|
404 |
|
✗ |
switch (avctx->chroma_sample_location) { |
405 |
|
✗ |
case AVCHROMA_LOC_LEFT: |
406 |
|
✗ |
sh->color_config.chroma_sample_position = AV1_CSP_VERTICAL; |
407 |
|
✗ |
break; |
408 |
|
✗ |
case AVCHROMA_LOC_TOPLEFT: |
409 |
|
✗ |
sh->color_config.chroma_sample_position = AV1_CSP_COLOCATED; |
410 |
|
✗ |
break; |
411 |
|
✗ |
default: |
412 |
|
✗ |
sh->color_config.chroma_sample_position = AV1_CSP_UNKNOWN; |
413 |
|
✗ |
break; |
414 |
|
|
} |
415 |
|
|
|
416 |
|
✗ |
if (avctx->level != AV_LEVEL_UNKNOWN) { |
417 |
|
✗ |
sh->seq_level_idx[0] = avctx->level; |
418 |
|
|
} else { |
419 |
|
|
const AV1LevelDescriptor *level; |
420 |
|
|
float framerate; |
421 |
|
|
|
422 |
|
✗ |
if (avctx->framerate.num > 0 && avctx->framerate.den > 0) |
423 |
|
✗ |
framerate = avctx->framerate.num / avctx->framerate.den; |
424 |
|
|
else |
425 |
|
✗ |
framerate = 0; |
426 |
|
|
|
427 |
|
✗ |
level = ff_av1_guess_level(avctx->bit_rate, priv->tier, |
428 |
|
|
base_ctx->surface_width, base_ctx->surface_height, |
429 |
|
✗ |
priv->tile_rows * priv->tile_cols, |
430 |
|
|
priv->tile_cols, framerate); |
431 |
|
✗ |
if (level) { |
432 |
|
✗ |
av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name); |
433 |
|
✗ |
sh->seq_level_idx[0] = level->level_idx; |
434 |
|
|
} else { |
435 |
|
✗ |
av_log(avctx, AV_LOG_VERBOSE, "Stream will not conform to " |
436 |
|
|
"any normal level, using maximum parameters level by default.\n"); |
437 |
|
✗ |
sh->seq_level_idx[0] = 31; |
438 |
|
✗ |
sh->seq_tier[0] = 1; |
439 |
|
|
} |
440 |
|
|
} |
441 |
|
✗ |
vseq->seq_profile = sh->seq_profile; |
442 |
|
✗ |
vseq->seq_level_idx = sh->seq_level_idx[0]; |
443 |
|
✗ |
vseq->seq_tier = sh->seq_tier[0]; |
444 |
|
✗ |
vseq->order_hint_bits_minus_1 = sh->order_hint_bits_minus_1; |
445 |
|
✗ |
vseq->intra_period = base_ctx->gop_size; |
446 |
|
✗ |
vseq->ip_period = base_ctx->b_per_p + 1; |
447 |
|
|
|
448 |
|
✗ |
vseq->seq_fields.bits.enable_order_hint = sh->enable_order_hint; |
449 |
|
|
|
450 |
|
✗ |
if (!(ctx->va_rc_mode & VA_RC_CQP)) { |
451 |
|
✗ |
vseq->bits_per_second = ctx->va_bit_rate; |
452 |
|
✗ |
vseq->seq_fields.bits.enable_cdef = sh->enable_cdef = 1; |
453 |
|
|
} |
454 |
|
|
|
455 |
|
✗ |
ret = vaapi_encode_av1_add_obu(avctx, obu, AV1_OBU_SEQUENCE_HEADER, &priv->sh); |
456 |
|
✗ |
if (ret < 0) |
457 |
|
✗ |
goto end; |
458 |
|
|
|
459 |
|
✗ |
ret = vaapi_encode_av1_write_obu(avctx, priv->sh_data, &priv->sh_data_len, obu); |
460 |
|
✗ |
if (ret < 0) |
461 |
|
✗ |
goto end; |
462 |
|
|
|
463 |
|
✗ |
end: |
464 |
|
✗ |
ff_cbs_fragment_reset(obu); |
465 |
|
✗ |
return ret; |
466 |
|
|
} |
467 |
|
|
|
468 |
|
✗ |
static int vaapi_encode_av1_init_picture_params(AVCodecContext *avctx, |
469 |
|
|
FFHWBaseEncodePicture *pic) |
470 |
|
|
{ |
471 |
|
✗ |
VAAPIEncodeContext *ctx = avctx->priv_data; |
472 |
|
✗ |
VAAPIEncodeAV1Context *priv = avctx->priv_data; |
473 |
|
✗ |
VAAPIEncodePicture *vaapi_pic = pic->priv; |
474 |
|
✗ |
VAAPIEncodeAV1Picture *hpic = pic->codec_priv; |
475 |
|
✗ |
AV1RawOBU *fh_obu = &priv->fh; |
476 |
|
✗ |
AV1RawFrameHeader *fh = &fh_obu->obu.frame.header; |
477 |
|
✗ |
VAEncPictureParameterBufferAV1 *vpic = vaapi_pic->codec_picture_params; |
478 |
|
✗ |
CodedBitstreamFragment *obu = &priv->current_obu; |
479 |
|
|
FFHWBaseEncodePicture *ref; |
480 |
|
|
VAAPIEncodeAV1Picture *href; |
481 |
|
|
int slot, i; |
482 |
|
|
int ret; |
483 |
|
|
static const int8_t default_loop_filter_ref_deltas[AV1_TOTAL_REFS_PER_FRAME] = |
484 |
|
|
{ 1, 0, 0, 0, -1, 0, -1, -1 }; |
485 |
|
|
|
486 |
|
✗ |
memset(fh_obu, 0, sizeof(*fh_obu)); |
487 |
|
✗ |
vaapi_pic->nb_slices = priv->tile_groups; |
488 |
|
✗ |
vaapi_pic->non_independent_frame = pic->encode_order < pic->display_order; |
489 |
|
✗ |
fh_obu->header.obu_type = AV1_OBU_FRAME_HEADER; |
490 |
|
✗ |
fh_obu->header.obu_has_size_field = 1; |
491 |
|
|
|
492 |
|
✗ |
switch (pic->type) { |
493 |
|
✗ |
case FF_HW_PICTURE_TYPE_IDR: |
494 |
|
✗ |
av_assert0(pic->nb_refs[0] == 0 || pic->nb_refs[1]); |
495 |
|
✗ |
fh->frame_type = AV1_FRAME_KEY; |
496 |
|
✗ |
fh->refresh_frame_flags = 0xFF; |
497 |
|
✗ |
fh->base_q_idx = priv->q_idx_idr; |
498 |
|
✗ |
hpic->slot = 0; |
499 |
|
✗ |
hpic->last_idr_frame = pic->display_order; |
500 |
|
✗ |
break; |
501 |
|
✗ |
case FF_HW_PICTURE_TYPE_P: |
502 |
|
✗ |
av_assert0(pic->nb_refs[0]); |
503 |
|
✗ |
fh->frame_type = AV1_FRAME_INTER; |
504 |
|
✗ |
fh->base_q_idx = priv->q_idx_p; |
505 |
|
✗ |
ref = pic->refs[0][pic->nb_refs[0] - 1]; |
506 |
|
✗ |
href = ref->codec_priv; |
507 |
|
✗ |
hpic->slot = !href->slot; |
508 |
|
✗ |
hpic->last_idr_frame = href->last_idr_frame; |
509 |
|
✗ |
fh->refresh_frame_flags = 1 << hpic->slot; |
510 |
|
|
|
511 |
|
|
/** set the nearest frame in L0 as all reference frame. */ |
512 |
|
✗ |
for (i = 0; i < AV1_REFS_PER_FRAME; i++) { |
513 |
|
✗ |
fh->ref_frame_idx[i] = href->slot; |
514 |
|
|
} |
515 |
|
✗ |
fh->primary_ref_frame = href->slot; |
516 |
|
✗ |
fh->ref_order_hint[href->slot] = ref->display_order - href->last_idr_frame; |
517 |
|
✗ |
vpic->ref_frame_ctrl_l0.fields.search_idx0 = AV1_REF_FRAME_LAST; |
518 |
|
|
|
519 |
|
|
/** set the 2nd nearest frame in L0 as Golden frame. */ |
520 |
|
✗ |
if (pic->nb_refs[0] > 1) { |
521 |
|
✗ |
ref = pic->refs[0][pic->nb_refs[0] - 2]; |
522 |
|
✗ |
href = ref->codec_priv; |
523 |
|
✗ |
fh->ref_frame_idx[3] = href->slot; |
524 |
|
✗ |
fh->ref_order_hint[href->slot] = ref->display_order - href->last_idr_frame; |
525 |
|
✗ |
vpic->ref_frame_ctrl_l0.fields.search_idx1 = AV1_REF_FRAME_GOLDEN; |
526 |
|
|
} |
527 |
|
✗ |
break; |
528 |
|
✗ |
case FF_HW_PICTURE_TYPE_B: |
529 |
|
✗ |
av_assert0(pic->nb_refs[0] && pic->nb_refs[1]); |
530 |
|
✗ |
fh->frame_type = AV1_FRAME_INTER; |
531 |
|
✗ |
fh->base_q_idx = priv->q_idx_b; |
532 |
|
✗ |
fh->refresh_frame_flags = 0x0; |
533 |
|
✗ |
fh->reference_select = 1; |
534 |
|
|
|
535 |
|
|
/** B frame will not be referenced, disable its recon frame. */ |
536 |
|
✗ |
vpic->picture_flags.bits.disable_frame_recon = 1; |
537 |
|
|
|
538 |
|
|
/** Use LAST_FRAME and BWDREF_FRAME for reference. */ |
539 |
|
✗ |
vpic->ref_frame_ctrl_l0.fields.search_idx0 = AV1_REF_FRAME_LAST; |
540 |
|
✗ |
vpic->ref_frame_ctrl_l1.fields.search_idx0 = AV1_REF_FRAME_BWDREF; |
541 |
|
|
|
542 |
|
✗ |
ref = pic->refs[0][pic->nb_refs[0] - 1]; |
543 |
|
✗ |
href = ref->codec_priv; |
544 |
|
✗ |
hpic->last_idr_frame = href->last_idr_frame; |
545 |
|
✗ |
fh->primary_ref_frame = href->slot; |
546 |
|
✗ |
fh->ref_order_hint[href->slot] = ref->display_order - href->last_idr_frame; |
547 |
|
✗ |
for (i = 0; i < AV1_REF_FRAME_GOLDEN; i++) { |
548 |
|
✗ |
fh->ref_frame_idx[i] = href->slot; |
549 |
|
|
} |
550 |
|
|
|
551 |
|
✗ |
ref = pic->refs[1][pic->nb_refs[1] - 1]; |
552 |
|
✗ |
href = ref->codec_priv; |
553 |
|
✗ |
fh->ref_order_hint[href->slot] = ref->display_order - href->last_idr_frame; |
554 |
|
✗ |
for (i = AV1_REF_FRAME_GOLDEN; i < AV1_REFS_PER_FRAME; i++) { |
555 |
|
✗ |
fh->ref_frame_idx[i] = href->slot; |
556 |
|
|
} |
557 |
|
✗ |
break; |
558 |
|
✗ |
default: |
559 |
|
✗ |
av_assert0(0 && "invalid picture type"); |
560 |
|
|
} |
561 |
|
|
|
562 |
|
✗ |
fh->show_frame = pic->display_order <= pic->encode_order; |
563 |
|
✗ |
fh->showable_frame = fh->frame_type != AV1_FRAME_KEY; |
564 |
|
✗ |
fh->frame_width_minus_1 = avctx->width - 1; |
565 |
|
✗ |
fh->frame_height_minus_1 = avctx->height - 1; |
566 |
|
✗ |
fh->render_width_minus_1 = fh->frame_width_minus_1; |
567 |
|
✗ |
fh->render_height_minus_1 = fh->frame_height_minus_1; |
568 |
|
✗ |
fh->order_hint = pic->display_order - hpic->last_idr_frame; |
569 |
|
✗ |
fh->tile_cols = priv->tile_cols; |
570 |
|
✗ |
fh->tile_rows = priv->tile_rows; |
571 |
|
✗ |
fh->tile_cols_log2 = priv->tile_cols_log2; |
572 |
|
✗ |
fh->tile_rows_log2 = priv->tile_rows_log2; |
573 |
|
✗ |
fh->uniform_tile_spacing_flag = priv->uniform_tile; |
574 |
|
✗ |
fh->tile_size_bytes_minus1 = priv->attr_ext2.bits.tile_size_bytes_minus1; |
575 |
|
|
|
576 |
|
|
/** ignore ONLY_4x4 mode for codedlossless is not fully implemented. */ |
577 |
|
✗ |
if (priv->attr_ext2.bits.tx_mode_support & 0x04) |
578 |
|
✗ |
fh->tx_mode = AV1_TX_MODE_SELECT; |
579 |
|
✗ |
else if (priv->attr_ext2.bits.tx_mode_support & 0x02) |
580 |
|
✗ |
fh->tx_mode = AV1_TX_MODE_LARGEST; |
581 |
|
|
else { |
582 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "No available tx mode found.\n"); |
583 |
|
✗ |
return AVERROR(EINVAL); |
584 |
|
|
} |
585 |
|
|
|
586 |
|
✗ |
for (i = 0; i < fh->tile_cols; i++) |
587 |
|
✗ |
fh->width_in_sbs_minus_1[i] = vpic->width_in_sbs_minus_1[i] = priv->width_in_sbs_minus_1[i]; |
588 |
|
|
|
589 |
|
✗ |
for (i = 0; i < fh->tile_rows; i++) |
590 |
|
✗ |
fh->height_in_sbs_minus_1[i] = vpic->height_in_sbs_minus_1[i] = priv->height_in_sbs_minus_1[i]; |
591 |
|
|
|
592 |
|
✗ |
memcpy(fh->loop_filter_ref_deltas, default_loop_filter_ref_deltas, |
593 |
|
|
AV1_TOTAL_REFS_PER_FRAME * sizeof(int8_t)); |
594 |
|
|
|
595 |
|
✗ |
if (fh->frame_type == AV1_FRAME_KEY && fh->show_frame) { |
596 |
|
✗ |
fh->error_resilient_mode = 1; |
597 |
|
|
} |
598 |
|
|
|
599 |
|
✗ |
if (fh->frame_type == AV1_FRAME_KEY || fh->error_resilient_mode) |
600 |
|
✗ |
fh->primary_ref_frame = AV1_PRIMARY_REF_NONE; |
601 |
|
|
|
602 |
|
✗ |
vpic->base_qindex = fh->base_q_idx; |
603 |
|
✗ |
vpic->frame_width_minus_1 = fh->frame_width_minus_1; |
604 |
|
✗ |
vpic->frame_height_minus_1 = fh->frame_height_minus_1; |
605 |
|
✗ |
vpic->primary_ref_frame = fh->primary_ref_frame; |
606 |
|
✗ |
vpic->reconstructed_frame = vaapi_pic->recon_surface; |
607 |
|
✗ |
vpic->coded_buf = vaapi_pic->output_buffer; |
608 |
|
✗ |
vpic->tile_cols = fh->tile_cols; |
609 |
|
✗ |
vpic->tile_rows = fh->tile_rows; |
610 |
|
✗ |
vpic->order_hint = fh->order_hint; |
611 |
|
|
#if VA_CHECK_VERSION(1, 15, 0) |
612 |
|
✗ |
vpic->refresh_frame_flags = fh->refresh_frame_flags; |
613 |
|
|
#endif |
614 |
|
|
|
615 |
|
✗ |
vpic->picture_flags.bits.enable_frame_obu = 0; |
616 |
|
✗ |
vpic->picture_flags.bits.frame_type = fh->frame_type; |
617 |
|
✗ |
vpic->picture_flags.bits.reduced_tx_set = fh->reduced_tx_set; |
618 |
|
✗ |
vpic->picture_flags.bits.error_resilient_mode = fh->error_resilient_mode; |
619 |
|
|
|
620 |
|
|
/** let driver decide to use single or compound reference prediction mode. */ |
621 |
|
✗ |
vpic->mode_control_flags.bits.reference_mode = fh->reference_select ? 2 : 0; |
622 |
|
✗ |
vpic->mode_control_flags.bits.tx_mode = fh->tx_mode; |
623 |
|
|
|
624 |
|
✗ |
vpic->tile_group_obu_hdr_info.bits.obu_has_size_field = 1; |
625 |
|
|
|
626 |
|
|
/** set reference. */ |
627 |
|
✗ |
for (i = 0; i < AV1_REFS_PER_FRAME; i++) |
628 |
|
✗ |
vpic->ref_frame_idx[i] = fh->ref_frame_idx[i]; |
629 |
|
|
|
630 |
|
✗ |
for (i = 0; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++) |
631 |
|
✗ |
vpic->reference_frames[i] = VA_INVALID_SURFACE; |
632 |
|
|
|
633 |
|
✗ |
for (i = 0; i < MAX_REFERENCE_LIST_NUM; i++) { |
634 |
|
✗ |
for (int j = 0; j < pic->nb_refs[i]; j++) { |
635 |
|
✗ |
FFHWBaseEncodePicture *ref_pic = pic->refs[i][j]; |
636 |
|
|
|
637 |
|
✗ |
slot = ((VAAPIEncodeAV1Picture*)ref_pic->codec_priv)->slot; |
638 |
|
✗ |
av_assert0(vpic->reference_frames[slot] == VA_INVALID_SURFACE); |
639 |
|
|
|
640 |
|
✗ |
vpic->reference_frames[slot] = ((VAAPIEncodePicture *)ref_pic->priv)->recon_surface; |
641 |
|
|
} |
642 |
|
|
} |
643 |
|
|
|
644 |
|
✗ |
ret = vaapi_encode_av1_add_obu(avctx, obu, AV1_OBU_FRAME_HEADER, &priv->fh); |
645 |
|
✗ |
if (ret < 0) |
646 |
|
✗ |
goto end; |
647 |
|
|
|
648 |
|
✗ |
ret = vaapi_encode_av1_write_obu(avctx, priv->fh_data, &priv->fh_data_len, obu); |
649 |
|
✗ |
if (ret < 0) |
650 |
|
✗ |
goto end; |
651 |
|
|
|
652 |
|
✗ |
if (!(ctx->va_rc_mode & VA_RC_CQP)) { |
653 |
|
✗ |
vpic->min_base_qindex = av_clip(avctx->qmin, 1, AV1_MAX_QUANT); |
654 |
|
✗ |
vpic->max_base_qindex = av_clip(avctx->qmax, 1, AV1_MAX_QUANT); |
655 |
|
|
|
656 |
|
✗ |
vpic->bit_offset_qindex = priv->qindex_offset; |
657 |
|
✗ |
vpic->bit_offset_loopfilter_params = priv->loopfilter_offset; |
658 |
|
✗ |
vpic->bit_offset_cdef_params = priv->cdef_start_offset; |
659 |
|
✗ |
vpic->size_in_bits_cdef_params = priv->cdef_param_size; |
660 |
|
✗ |
vpic->size_in_bits_frame_hdr_obu = priv->fh_data_len; |
661 |
|
✗ |
vpic->byte_offset_frame_hdr_obu_size = (((pic->type == FF_HW_PICTURE_TYPE_IDR) ? |
662 |
|
✗ |
priv->sh_data_len / 8 : 0) + |
663 |
|
✗ |
(fh_obu->header.obu_extension_flag ? |
664 |
|
|
2 : 1)); |
665 |
|
|
} |
666 |
|
|
|
667 |
|
✗ |
priv->nb_mh = 0; |
668 |
|
|
|
669 |
|
✗ |
if (pic->type == FF_HW_PICTURE_TYPE_IDR) { |
670 |
|
|
AVFrameSideData *sd = |
671 |
|
✗ |
av_frame_get_side_data(pic->input_image, |
672 |
|
|
AV_FRAME_DATA_MASTERING_DISPLAY_METADATA); |
673 |
|
✗ |
if (sd) { |
674 |
|
✗ |
AVMasteringDisplayMetadata *mdm = |
675 |
|
|
(AVMasteringDisplayMetadata *)sd->data; |
676 |
|
✗ |
if (mdm->has_primaries && mdm->has_luminance) { |
677 |
|
✗ |
AV1RawOBU *obu = &priv->mh[priv->nb_mh++]; |
678 |
|
✗ |
AV1RawMetadata *md = &obu->obu.metadata; |
679 |
|
✗ |
AV1RawMetadataHDRMDCV *mdcv = &md->metadata.hdr_mdcv; |
680 |
|
✗ |
const int chroma_den = 1 << 16; |
681 |
|
✗ |
const int max_luma_den = 1 << 8; |
682 |
|
✗ |
const int min_luma_den = 1 << 14; |
683 |
|
|
|
684 |
|
✗ |
memset(obu, 0, sizeof(*obu)); |
685 |
|
✗ |
obu->header.obu_type = AV1_OBU_METADATA; |
686 |
|
✗ |
md->metadata_type = AV1_METADATA_TYPE_HDR_MDCV; |
687 |
|
|
|
688 |
|
✗ |
for (i = 0; i < 3; i++) { |
689 |
|
✗ |
mdcv->primary_chromaticity_x[i] = |
690 |
|
✗ |
av_rescale(mdm->display_primaries[i][0].num, chroma_den, |
691 |
|
✗ |
mdm->display_primaries[i][0].den); |
692 |
|
✗ |
mdcv->primary_chromaticity_y[i] = |
693 |
|
✗ |
av_rescale(mdm->display_primaries[i][1].num, chroma_den, |
694 |
|
✗ |
mdm->display_primaries[i][1].den); |
695 |
|
|
} |
696 |
|
|
|
697 |
|
✗ |
mdcv->white_point_chromaticity_x = |
698 |
|
✗ |
av_rescale(mdm->white_point[0].num, chroma_den, |
699 |
|
✗ |
mdm->white_point[0].den); |
700 |
|
✗ |
mdcv->white_point_chromaticity_y = |
701 |
|
✗ |
av_rescale(mdm->white_point[1].num, chroma_den, |
702 |
|
✗ |
mdm->white_point[1].den); |
703 |
|
|
|
704 |
|
✗ |
mdcv->luminance_max = |
705 |
|
✗ |
av_rescale(mdm->max_luminance.num, max_luma_den, |
706 |
|
✗ |
mdm->max_luminance.den); |
707 |
|
✗ |
mdcv->luminance_min = |
708 |
|
✗ |
av_rescale(mdm->min_luminance.num, min_luma_den, |
709 |
|
✗ |
mdm->min_luminance.den); |
710 |
|
|
} |
711 |
|
|
} |
712 |
|
|
|
713 |
|
✗ |
sd = av_frame_get_side_data(pic->input_image, |
714 |
|
|
AV_FRAME_DATA_CONTENT_LIGHT_LEVEL); |
715 |
|
✗ |
if (sd) { |
716 |
|
✗ |
AVContentLightMetadata *cllm = (AVContentLightMetadata *)sd->data; |
717 |
|
✗ |
AV1RawOBU *obu = &priv->mh[priv->nb_mh++]; |
718 |
|
✗ |
AV1RawMetadata *md = &obu->obu.metadata; |
719 |
|
✗ |
AV1RawMetadataHDRCLL *cll = &md->metadata.hdr_cll; |
720 |
|
|
|
721 |
|
✗ |
memset(obu, 0, sizeof(*obu)); |
722 |
|
✗ |
obu->header.obu_type = AV1_OBU_METADATA; |
723 |
|
✗ |
md->metadata_type = AV1_METADATA_TYPE_HDR_CLL; |
724 |
|
✗ |
cll->max_cll = cllm->MaxCLL; |
725 |
|
✗ |
cll->max_fall = cllm->MaxFALL; |
726 |
|
|
} |
727 |
|
|
} |
728 |
|
|
|
729 |
|
✗ |
end: |
730 |
|
✗ |
ff_cbs_fragment_reset(obu); |
731 |
|
✗ |
return ret; |
732 |
|
|
} |
733 |
|
|
|
734 |
|
✗ |
static int vaapi_encode_av1_init_slice_params(AVCodecContext *avctx, |
735 |
|
|
FFHWBaseEncodePicture *base, |
736 |
|
|
VAAPIEncodeSlice *slice) |
737 |
|
|
{ |
738 |
|
✗ |
VAAPIEncodeAV1Context *priv = avctx->priv_data; |
739 |
|
✗ |
VAEncTileGroupBufferAV1 *vslice = slice->codec_slice_params; |
740 |
|
✗ |
CodedBitstreamAV1Context *cbctx = priv->cbc->priv_data; |
741 |
|
|
int div; |
742 |
|
|
|
743 |
|
|
/** Set tile group info. */ |
744 |
|
✗ |
div = priv->tile_cols * priv->tile_rows / priv->tile_groups; |
745 |
|
✗ |
vslice->tg_start = slice->index * div; |
746 |
|
✗ |
if (slice->index == (priv->tile_groups - 1)) { |
747 |
|
✗ |
vslice->tg_end = priv->tile_cols * priv->tile_rows - 1; |
748 |
|
✗ |
cbctx->seen_frame_header = 0; |
749 |
|
|
} else { |
750 |
|
✗ |
vslice->tg_end = (slice->index + 1) * div - 1; |
751 |
|
|
} |
752 |
|
|
|
753 |
|
✗ |
return 0; |
754 |
|
|
} |
755 |
|
|
|
756 |
|
✗ |
static int vaapi_encode_av1_write_picture_header(AVCodecContext *avctx, |
757 |
|
|
FFHWBaseEncodePicture *pic, |
758 |
|
|
char *data, size_t *data_len) |
759 |
|
|
{ |
760 |
|
✗ |
VAAPIEncodeAV1Context *priv = avctx->priv_data; |
761 |
|
✗ |
CodedBitstreamFragment *obu = &priv->current_obu; |
762 |
|
✗ |
CodedBitstreamAV1Context *cbctx = priv->cbc->priv_data; |
763 |
|
✗ |
AV1RawOBU *fh_obu = &priv->fh; |
764 |
|
✗ |
AV1RawFrameHeader *rep_fh = &fh_obu->obu.frame_header; |
765 |
|
✗ |
VAAPIEncodePicture *vaapi_pic = pic->priv; |
766 |
|
|
VAAPIEncodeAV1Picture *href; |
767 |
|
✗ |
int ret = 0; |
768 |
|
|
|
769 |
|
✗ |
vaapi_pic->tail_size = 0; |
770 |
|
|
/** Pack repeat frame header. */ |
771 |
|
✗ |
if (pic->display_order > pic->encode_order) { |
772 |
|
✗ |
memset(fh_obu, 0, sizeof(*fh_obu)); |
773 |
|
✗ |
href = pic->refs[0][pic->nb_refs[0] - 1]->codec_priv; |
774 |
|
✗ |
fh_obu->header.obu_type = AV1_OBU_FRAME_HEADER; |
775 |
|
✗ |
fh_obu->header.obu_has_size_field = 1; |
776 |
|
|
|
777 |
|
✗ |
rep_fh->show_existing_frame = 1; |
778 |
|
✗ |
rep_fh->frame_to_show_map_idx = href->slot == 0; |
779 |
|
✗ |
rep_fh->frame_type = AV1_FRAME_INTER; |
780 |
|
✗ |
rep_fh->frame_width_minus_1 = avctx->width - 1; |
781 |
|
✗ |
rep_fh->frame_height_minus_1 = avctx->height - 1; |
782 |
|
✗ |
rep_fh->render_width_minus_1 = rep_fh->frame_width_minus_1; |
783 |
|
✗ |
rep_fh->render_height_minus_1 = rep_fh->frame_height_minus_1; |
784 |
|
|
|
785 |
|
✗ |
cbctx->seen_frame_header = 0; |
786 |
|
|
|
787 |
|
✗ |
ret = vaapi_encode_av1_add_obu(avctx, obu, AV1_OBU_FRAME_HEADER, &priv->fh); |
788 |
|
✗ |
if (ret < 0) |
789 |
|
✗ |
goto end; |
790 |
|
|
|
791 |
|
✗ |
ret = vaapi_encode_av1_write_obu(avctx, vaapi_pic->tail_data, &vaapi_pic->tail_size, obu); |
792 |
|
✗ |
if (ret < 0) |
793 |
|
✗ |
goto end; |
794 |
|
|
|
795 |
|
✗ |
vaapi_pic->tail_size /= 8; |
796 |
|
|
} |
797 |
|
|
|
798 |
|
✗ |
memcpy(data, &priv->fh_data, MAX_PARAM_BUFFER_SIZE * sizeof(char)); |
799 |
|
✗ |
*data_len = priv->fh_data_len; |
800 |
|
|
|
801 |
|
✗ |
end: |
802 |
|
✗ |
ff_cbs_fragment_reset(obu); |
803 |
|
✗ |
return ret; |
804 |
|
|
} |
805 |
|
|
|
806 |
|
✗ |
static int vaapi_encode_av1_write_extra_header(AVCodecContext *avctx, |
807 |
|
|
FFHWBaseEncodePicture *base_pic, |
808 |
|
|
int index, int *type, |
809 |
|
|
char *data, size_t *data_len) |
810 |
|
|
{ |
811 |
|
✗ |
VAAPIEncodeAV1Context *priv = avctx->priv_data; |
812 |
|
✗ |
CodedBitstreamFragment *obu = &priv->current_obu; |
813 |
|
|
AV1RawOBU *mh_obu; |
814 |
|
|
char mh_data[MAX_PARAM_BUFFER_SIZE]; |
815 |
|
|
size_t mh_data_len; |
816 |
|
✗ |
int ret = 0; |
817 |
|
|
|
818 |
|
✗ |
if (index >= priv->nb_mh) |
819 |
|
✗ |
return AVERROR_EOF; |
820 |
|
|
|
821 |
|
✗ |
mh_obu = &priv->mh[index]; |
822 |
|
✗ |
ret = vaapi_encode_av1_add_obu(avctx, obu, AV1_OBU_METADATA, mh_obu); |
823 |
|
✗ |
if (ret < 0) |
824 |
|
✗ |
goto end; |
825 |
|
|
|
826 |
|
✗ |
ret = vaapi_encode_av1_write_obu(avctx, mh_data, &mh_data_len, obu); |
827 |
|
✗ |
if (ret < 0) |
828 |
|
✗ |
goto end; |
829 |
|
|
|
830 |
|
✗ |
memcpy(data, mh_data, MAX_PARAM_BUFFER_SIZE * sizeof(char)); |
831 |
|
✗ |
*data_len = mh_data_len; |
832 |
|
✗ |
*type = VAEncPackedHeaderRawData; |
833 |
|
|
|
834 |
|
✗ |
end: |
835 |
|
✗ |
ff_cbs_fragment_reset(obu); |
836 |
|
✗ |
return ret; |
837 |
|
|
} |
838 |
|
|
|
839 |
|
|
static const VAAPIEncodeProfile vaapi_encode_av1_profiles[] = { |
840 |
|
|
{ AV_PROFILE_AV1_MAIN, 8, 3, 1, 1, VAProfileAV1Profile0 }, |
841 |
|
|
{ AV_PROFILE_AV1_MAIN, 10, 3, 1, 1, VAProfileAV1Profile0 }, |
842 |
|
|
{ AV_PROFILE_UNKNOWN } |
843 |
|
|
}; |
844 |
|
|
|
845 |
|
|
static const VAAPIEncodeType vaapi_encode_type_av1 = { |
846 |
|
|
.profiles = vaapi_encode_av1_profiles, |
847 |
|
|
.flags = FF_HW_FLAG_B_PICTURES | FLAG_TIMESTAMP_NO_DELAY, |
848 |
|
|
.default_quality = 25, |
849 |
|
|
|
850 |
|
|
.get_encoder_caps = &vaapi_encode_av1_get_encoder_caps, |
851 |
|
|
.configure = &vaapi_encode_av1_configure, |
852 |
|
|
|
853 |
|
|
.sequence_header_type = VAEncPackedHeaderSequence, |
854 |
|
|
.sequence_params_size = sizeof(VAEncSequenceParameterBufferAV1), |
855 |
|
|
.init_sequence_params = &vaapi_encode_av1_init_sequence_params, |
856 |
|
|
.write_sequence_header = &vaapi_encode_av1_write_sequence_header, |
857 |
|
|
|
858 |
|
|
.picture_priv_data_size = sizeof(VAAPIEncodeAV1Picture), |
859 |
|
|
.picture_header_type = VAEncPackedHeaderPicture, |
860 |
|
|
.picture_params_size = sizeof(VAEncPictureParameterBufferAV1), |
861 |
|
|
.init_picture_params = &vaapi_encode_av1_init_picture_params, |
862 |
|
|
.write_picture_header = &vaapi_encode_av1_write_picture_header, |
863 |
|
|
|
864 |
|
|
.slice_params_size = sizeof(VAEncTileGroupBufferAV1), |
865 |
|
|
.init_slice_params = &vaapi_encode_av1_init_slice_params, |
866 |
|
|
|
867 |
|
|
.write_extra_header = &vaapi_encode_av1_write_extra_header, |
868 |
|
|
}; |
869 |
|
|
|
870 |
|
✗ |
static av_cold int vaapi_encode_av1_init(AVCodecContext *avctx) |
871 |
|
|
{ |
872 |
|
✗ |
VAAPIEncodeContext *ctx = avctx->priv_data; |
873 |
|
✗ |
VAAPIEncodeAV1Context *priv = avctx->priv_data; |
874 |
|
|
VAConfigAttrib attr; |
875 |
|
|
VAStatus vas; |
876 |
|
|
int ret; |
877 |
|
|
|
878 |
|
✗ |
ctx->codec = &vaapi_encode_type_av1; |
879 |
|
|
|
880 |
|
✗ |
ctx->desired_packed_headers = |
881 |
|
|
VA_ENC_PACKED_HEADER_SEQUENCE | |
882 |
|
|
VA_ENC_PACKED_HEADER_PICTURE | |
883 |
|
|
VA_ENC_PACKED_HEADER_MISC; // Metadata |
884 |
|
|
|
885 |
|
✗ |
if (avctx->profile == AV_PROFILE_UNKNOWN) |
886 |
|
✗ |
avctx->profile = priv->profile; |
887 |
|
✗ |
if (avctx->level == AV_LEVEL_UNKNOWN) |
888 |
|
✗ |
avctx->level = priv->level; |
889 |
|
|
|
890 |
|
✗ |
if (avctx->level != AV_LEVEL_UNKNOWN && avctx->level & ~0x1f) { |
891 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Invalid level %d\n", avctx->level); |
892 |
|
✗ |
return AVERROR(EINVAL); |
893 |
|
|
} |
894 |
|
|
|
895 |
|
✗ |
ret = ff_vaapi_encode_init(avctx); |
896 |
|
✗ |
if (ret < 0) |
897 |
|
✗ |
return ret; |
898 |
|
|
|
899 |
|
✗ |
attr.type = VAConfigAttribEncAV1; |
900 |
|
✗ |
vas = vaGetConfigAttributes(ctx->hwctx->display, |
901 |
|
|
ctx->va_profile, |
902 |
|
|
ctx->va_entrypoint, |
903 |
|
|
&attr, 1); |
904 |
|
✗ |
if (vas != VA_STATUS_SUCCESS) { |
905 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Failed to query " |
906 |
|
|
"config attribute: %d (%s).\n", vas, vaErrorStr(vas)); |
907 |
|
✗ |
return AVERROR_EXTERNAL; |
908 |
|
✗ |
} else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) { |
909 |
|
✗ |
priv->attr.value = 0; |
910 |
|
✗ |
av_log(avctx, AV_LOG_WARNING, "Attribute type:%d is not " |
911 |
|
✗ |
"supported.\n", attr.type); |
912 |
|
|
} else { |
913 |
|
✗ |
priv->attr.value = attr.value; |
914 |
|
|
} |
915 |
|
|
|
916 |
|
✗ |
attr.type = VAConfigAttribEncAV1Ext1; |
917 |
|
✗ |
vas = vaGetConfigAttributes(ctx->hwctx->display, |
918 |
|
|
ctx->va_profile, |
919 |
|
|
ctx->va_entrypoint, |
920 |
|
|
&attr, 1); |
921 |
|
✗ |
if (vas != VA_STATUS_SUCCESS) { |
922 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Failed to query " |
923 |
|
|
"config attribute: %d (%s).\n", vas, vaErrorStr(vas)); |
924 |
|
✗ |
return AVERROR_EXTERNAL; |
925 |
|
✗ |
} else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) { |
926 |
|
✗ |
priv->attr_ext1.value = 0; |
927 |
|
✗ |
av_log(avctx, AV_LOG_WARNING, "Attribute type:%d is not " |
928 |
|
✗ |
"supported.\n", attr.type); |
929 |
|
|
} else { |
930 |
|
✗ |
priv->attr_ext1.value = attr.value; |
931 |
|
|
} |
932 |
|
|
|
933 |
|
|
/** This attr provides essential indicators, return error if not support. */ |
934 |
|
✗ |
attr.type = VAConfigAttribEncAV1Ext2; |
935 |
|
✗ |
vas = vaGetConfigAttributes(ctx->hwctx->display, |
936 |
|
|
ctx->va_profile, |
937 |
|
|
ctx->va_entrypoint, |
938 |
|
|
&attr, 1); |
939 |
|
✗ |
if (vas != VA_STATUS_SUCCESS || attr.value == VA_ATTRIB_NOT_SUPPORTED) { |
940 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Failed to query " |
941 |
|
|
"config attribute: %d (%s).\n", vas, vaErrorStr(vas)); |
942 |
|
✗ |
return AVERROR_EXTERNAL; |
943 |
|
|
} else { |
944 |
|
✗ |
priv->attr_ext2.value = attr.value; |
945 |
|
|
} |
946 |
|
|
|
947 |
|
✗ |
av_opt_set_int(priv->cbc->priv_data, "fixed_obu_size_length", |
948 |
|
✗ |
priv->attr_ext2.bits.obu_size_bytes_minus1 + 1, 0); |
949 |
|
|
|
950 |
|
✗ |
ret = vaapi_encode_av1_set_tile(avctx); |
951 |
|
✗ |
if (ret < 0) |
952 |
|
✗ |
return ret; |
953 |
|
|
|
954 |
|
✗ |
return 0; |
955 |
|
|
} |
956 |
|
|
|
957 |
|
✗ |
static av_cold int vaapi_encode_av1_close(AVCodecContext *avctx) |
958 |
|
|
{ |
959 |
|
✗ |
VAAPIEncodeAV1Context *priv = avctx->priv_data; |
960 |
|
|
|
961 |
|
✗ |
ff_cbs_fragment_free(&priv->current_obu); |
962 |
|
✗ |
ff_cbs_close(&priv->cbc); |
963 |
|
|
|
964 |
|
✗ |
return ff_vaapi_encode_close(avctx); |
965 |
|
|
} |
966 |
|
|
|
967 |
|
|
#define OFFSET(x) offsetof(VAAPIEncodeAV1Context, x) |
968 |
|
|
#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM) |
969 |
|
|
|
970 |
|
|
static const AVOption vaapi_encode_av1_options[] = { |
971 |
|
|
HW_BASE_ENCODE_COMMON_OPTIONS, |
972 |
|
|
VAAPI_ENCODE_COMMON_OPTIONS, |
973 |
|
|
VAAPI_ENCODE_RC_OPTIONS, |
974 |
|
|
{ "profile", "Set profile (seq_profile)", |
975 |
|
|
OFFSET(profile), AV_OPT_TYPE_INT, |
976 |
|
|
{ .i64 = AV_PROFILE_UNKNOWN }, AV_PROFILE_UNKNOWN, 0xff, FLAGS, .unit = "profile" }, |
977 |
|
|
|
978 |
|
|
#define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \ |
979 |
|
|
{ .i64 = value }, 0, 0, FLAGS, .unit = "profile" |
980 |
|
|
{ PROFILE("main", AV_PROFILE_AV1_MAIN) }, |
981 |
|
|
{ PROFILE("high", AV_PROFILE_AV1_HIGH) }, |
982 |
|
|
{ PROFILE("professional", AV_PROFILE_AV1_PROFESSIONAL) }, |
983 |
|
|
#undef PROFILE |
984 |
|
|
|
985 |
|
|
{ "tier", "Set tier (seq_tier)", |
986 |
|
|
OFFSET(tier), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS, .unit = "tier" }, |
987 |
|
|
{ "main", NULL, 0, AV_OPT_TYPE_CONST, |
988 |
|
|
{ .i64 = 0 }, 0, 0, FLAGS, .unit = "tier" }, |
989 |
|
|
{ "high", NULL, 0, AV_OPT_TYPE_CONST, |
990 |
|
|
{ .i64 = 1 }, 0, 0, FLAGS, .unit = "tier" }, |
991 |
|
|
{ "level", "Set level (seq_level_idx)", |
992 |
|
|
OFFSET(level), AV_OPT_TYPE_INT, |
993 |
|
|
{ .i64 = AV_LEVEL_UNKNOWN }, AV_LEVEL_UNKNOWN, 0x1f, FLAGS, .unit = "level" }, |
994 |
|
|
|
995 |
|
|
#define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \ |
996 |
|
|
{ .i64 = value }, 0, 0, FLAGS, .unit = "level" |
997 |
|
|
{ LEVEL("2.0", 0) }, |
998 |
|
|
{ LEVEL("2.1", 1) }, |
999 |
|
|
{ LEVEL("3.0", 4) }, |
1000 |
|
|
{ LEVEL("3.1", 5) }, |
1001 |
|
|
{ LEVEL("4.0", 8) }, |
1002 |
|
|
{ LEVEL("4.1", 9) }, |
1003 |
|
|
{ LEVEL("5.0", 12) }, |
1004 |
|
|
{ LEVEL("5.1", 13) }, |
1005 |
|
|
{ LEVEL("5.2", 14) }, |
1006 |
|
|
{ LEVEL("5.3", 15) }, |
1007 |
|
|
{ LEVEL("6.0", 16) }, |
1008 |
|
|
{ LEVEL("6.1", 17) }, |
1009 |
|
|
{ LEVEL("6.2", 18) }, |
1010 |
|
|
{ LEVEL("6.3", 19) }, |
1011 |
|
|
#undef LEVEL |
1012 |
|
|
|
1013 |
|
|
{ "tiles", "Tile columns x rows (Use minimal tile column/row number automatically by default)", |
1014 |
|
|
OFFSET(tile_cols), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, FLAGS }, |
1015 |
|
|
{ "tile_groups", "Number of tile groups for encoding", |
1016 |
|
|
OFFSET(tile_groups), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, AV1_MAX_TILE_ROWS * AV1_MAX_TILE_COLS, FLAGS }, |
1017 |
|
|
|
1018 |
|
|
{ NULL }, |
1019 |
|
|
}; |
1020 |
|
|
|
1021 |
|
|
static const FFCodecDefault vaapi_encode_av1_defaults[] = { |
1022 |
|
|
{ "b", "0" }, |
1023 |
|
|
{ "bf", "2" }, |
1024 |
|
|
{ "g", "120" }, |
1025 |
|
|
{ "qmin", "1" }, |
1026 |
|
|
{ "qmax", "255" }, |
1027 |
|
|
{ NULL }, |
1028 |
|
|
}; |
1029 |
|
|
|
1030 |
|
|
static const AVClass vaapi_encode_av1_class = { |
1031 |
|
|
.class_name = "av1_vaapi", |
1032 |
|
|
.item_name = av_default_item_name, |
1033 |
|
|
.option = vaapi_encode_av1_options, |
1034 |
|
|
.version = LIBAVUTIL_VERSION_INT, |
1035 |
|
|
}; |
1036 |
|
|
|
1037 |
|
|
const FFCodec ff_av1_vaapi_encoder = { |
1038 |
|
|
.p.name = "av1_vaapi", |
1039 |
|
|
CODEC_LONG_NAME("AV1 (VAAPI)"), |
1040 |
|
|
.p.type = AVMEDIA_TYPE_VIDEO, |
1041 |
|
|
.p.id = AV_CODEC_ID_AV1, |
1042 |
|
|
.priv_data_size = sizeof(VAAPIEncodeAV1Context), |
1043 |
|
|
.init = &vaapi_encode_av1_init, |
1044 |
|
|
FF_CODEC_RECEIVE_PACKET_CB(&ff_vaapi_encode_receive_packet), |
1045 |
|
|
.close = &vaapi_encode_av1_close, |
1046 |
|
|
.p.priv_class = &vaapi_encode_av1_class, |
1047 |
|
|
.p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE | |
1048 |
|
|
AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, |
1049 |
|
|
.caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE | |
1050 |
|
|
FF_CODEC_CAP_INIT_CLEANUP, |
1051 |
|
|
.defaults = vaapi_encode_av1_defaults, |
1052 |
|
|
.p.pix_fmts = (const enum AVPixelFormat[]) { |
1053 |
|
|
AV_PIX_FMT_VAAPI, |
1054 |
|
|
AV_PIX_FMT_NONE, |
1055 |
|
|
}, |
1056 |
|
|
.color_ranges = AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG, |
1057 |
|
|
.hw_configs = ff_vaapi_encode_hw_configs, |
1058 |
|
|
.p.wrapper_name = "vaapi", |
1059 |
|
|
}; |
1060 |
|
|
|