Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* This file is part of FFmpeg. |
3 |
|
|
* |
4 |
|
|
* FFmpeg is free software; you can redistribute it and/or |
5 |
|
|
* modify it under the terms of the GNU Lesser General Public |
6 |
|
|
* License as published by the Free Software Foundation; either |
7 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
8 |
|
|
* |
9 |
|
|
* FFmpeg is distributed in the hope that it will be useful, |
10 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
|
|
* Lesser General Public License for more details. |
13 |
|
|
* |
14 |
|
|
* You should have received a copy of the GNU Lesser General Public |
15 |
|
|
* License along with FFmpeg; if not, write to the Free Software |
16 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
#include <string.h> |
20 |
|
|
|
21 |
|
|
#include <va/va.h> |
22 |
|
|
#include <va/va_enc_h264.h> |
23 |
|
|
|
24 |
|
|
#include "libavutil/avassert.h" |
25 |
|
|
#include "libavutil/common.h" |
26 |
|
|
#include "libavutil/mem.h" |
27 |
|
|
#include "libavutil/pixdesc.h" |
28 |
|
|
#include "libavutil/opt.h" |
29 |
|
|
|
30 |
|
|
#include "atsc_a53.h" |
31 |
|
|
#include "avcodec.h" |
32 |
|
|
#include "cbs.h" |
33 |
|
|
#include "cbs_h264.h" |
34 |
|
|
#include "codec_internal.h" |
35 |
|
|
#include "h264.h" |
36 |
|
|
#include "h264_levels.h" |
37 |
|
|
#include "h2645data.h" |
38 |
|
|
#include "vaapi_encode.h" |
39 |
|
|
#include "version.h" |
40 |
|
|
|
41 |
|
|
enum { |
42 |
|
|
SEI_TIMING = 0x01, |
43 |
|
|
SEI_IDENTIFIER = 0x02, |
44 |
|
|
SEI_RECOVERY_POINT = 0x04, |
45 |
|
|
SEI_A53_CC = 0x08, |
46 |
|
|
}; |
47 |
|
|
|
48 |
|
|
// Random (version 4) ISO 11578 UUID. |
49 |
|
|
static const uint8_t vaapi_encode_h264_sei_identifier_uuid[16] = { |
50 |
|
|
0x59, 0x94, 0x8b, 0x28, 0x11, 0xec, 0x45, 0xaf, |
51 |
|
|
0x96, 0x75, 0x19, 0xd4, 0x1f, 0xea, 0xa9, 0x4d, |
52 |
|
|
}; |
53 |
|
|
|
54 |
|
|
typedef struct VAAPIEncodeH264Picture { |
55 |
|
|
int frame_num; |
56 |
|
|
int pic_order_cnt; |
57 |
|
|
|
58 |
|
|
int64_t last_idr_frame; |
59 |
|
|
uint16_t idr_pic_id; |
60 |
|
|
|
61 |
|
|
int primary_pic_type; |
62 |
|
|
int slice_type; |
63 |
|
|
|
64 |
|
|
int cpb_delay; |
65 |
|
|
int dpb_delay; |
66 |
|
|
} VAAPIEncodeH264Picture; |
67 |
|
|
|
68 |
|
|
typedef struct VAAPIEncodeH264Context { |
69 |
|
|
VAAPIEncodeContext common; |
70 |
|
|
|
71 |
|
|
// User options. |
72 |
|
|
int qp; |
73 |
|
|
int quality; |
74 |
|
|
int coder; |
75 |
|
|
int aud; |
76 |
|
|
int sei; |
77 |
|
|
int profile; |
78 |
|
|
int level; |
79 |
|
|
|
80 |
|
|
// Derived settings. |
81 |
|
|
int mb_width; |
82 |
|
|
int mb_height; |
83 |
|
|
|
84 |
|
|
int fixed_qp_idr; |
85 |
|
|
int fixed_qp_p; |
86 |
|
|
int fixed_qp_b; |
87 |
|
|
|
88 |
|
|
int dpb_frames; |
89 |
|
|
|
90 |
|
|
// Writer structures. |
91 |
|
|
CodedBitstreamContext *cbc; |
92 |
|
|
CodedBitstreamFragment current_access_unit; |
93 |
|
|
|
94 |
|
|
H264RawAUD raw_aud; |
95 |
|
|
H264RawSPS raw_sps; |
96 |
|
|
H264RawPPS raw_pps; |
97 |
|
|
H264RawSlice raw_slice; |
98 |
|
|
|
99 |
|
|
H264RawSEIBufferingPeriod sei_buffering_period; |
100 |
|
|
H264RawSEIPicTiming sei_pic_timing; |
101 |
|
|
H264RawSEIRecoveryPoint sei_recovery_point; |
102 |
|
|
SEIRawUserDataUnregistered sei_identifier; |
103 |
|
|
char *sei_identifier_string; |
104 |
|
|
SEIRawUserDataRegistered sei_a53cc; |
105 |
|
|
void *sei_a53cc_data; |
106 |
|
|
|
107 |
|
|
int aud_needed; |
108 |
|
|
int sei_needed; |
109 |
|
|
int sei_cbr_workaround_needed; |
110 |
|
|
} VAAPIEncodeH264Context; |
111 |
|
|
|
112 |
|
|
|
113 |
|
✗ |
static int vaapi_encode_h264_write_access_unit(AVCodecContext *avctx, |
114 |
|
|
char *data, size_t *data_len, |
115 |
|
|
CodedBitstreamFragment *au) |
116 |
|
|
{ |
117 |
|
✗ |
VAAPIEncodeH264Context *priv = avctx->priv_data; |
118 |
|
|
int err; |
119 |
|
|
|
120 |
|
✗ |
err = ff_cbs_write_fragment_data(priv->cbc, au); |
121 |
|
✗ |
if (err < 0) { |
122 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n"); |
123 |
|
✗ |
return err; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
✗ |
if (*data_len < 8 * au->data_size - au->data_bit_padding) { |
127 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Access unit too large: " |
128 |
|
|
"%zu < %zu.\n", *data_len, |
129 |
|
✗ |
8 * au->data_size - au->data_bit_padding); |
130 |
|
✗ |
return AVERROR(ENOSPC); |
131 |
|
|
} |
132 |
|
|
|
133 |
|
✗ |
memcpy(data, au->data, au->data_size); |
134 |
|
✗ |
*data_len = 8 * au->data_size - au->data_bit_padding; |
135 |
|
|
|
136 |
|
✗ |
return 0; |
137 |
|
|
} |
138 |
|
|
|
139 |
|
✗ |
static int vaapi_encode_h264_add_nal(AVCodecContext *avctx, |
140 |
|
|
CodedBitstreamFragment *au, |
141 |
|
|
void *nal_unit) |
142 |
|
|
{ |
143 |
|
✗ |
H264RawNALUnitHeader *header = nal_unit; |
144 |
|
|
int err; |
145 |
|
|
|
146 |
|
✗ |
err = ff_cbs_insert_unit_content(au, -1, |
147 |
|
✗ |
header->nal_unit_type, nal_unit, NULL); |
148 |
|
✗ |
if (err < 0) { |
149 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: " |
150 |
|
✗ |
"type = %d.\n", header->nal_unit_type); |
151 |
|
✗ |
return err; |
152 |
|
|
} |
153 |
|
|
|
154 |
|
✗ |
return 0; |
155 |
|
|
} |
156 |
|
|
|
157 |
|
✗ |
static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx, |
158 |
|
|
char *data, size_t *data_len) |
159 |
|
|
{ |
160 |
|
✗ |
VAAPIEncodeH264Context *priv = avctx->priv_data; |
161 |
|
✗ |
CodedBitstreamFragment *au = &priv->current_access_unit; |
162 |
|
|
int err; |
163 |
|
|
|
164 |
|
✗ |
if (priv->aud_needed) { |
165 |
|
✗ |
err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud); |
166 |
|
✗ |
if (err < 0) |
167 |
|
✗ |
goto fail; |
168 |
|
✗ |
priv->aud_needed = 0; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
✗ |
err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_sps); |
172 |
|
✗ |
if (err < 0) |
173 |
|
✗ |
goto fail; |
174 |
|
|
|
175 |
|
✗ |
err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_pps); |
176 |
|
✗ |
if (err < 0) |
177 |
|
✗ |
goto fail; |
178 |
|
|
|
179 |
|
✗ |
err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au); |
180 |
|
✗ |
fail: |
181 |
|
✗ |
ff_cbs_fragment_reset(au); |
182 |
|
✗ |
return err; |
183 |
|
|
} |
184 |
|
|
|
185 |
|
✗ |
static int vaapi_encode_h264_write_slice_header(AVCodecContext *avctx, |
186 |
|
|
VAAPIEncodePicture *pic, |
187 |
|
|
VAAPIEncodeSlice *slice, |
188 |
|
|
char *data, size_t *data_len) |
189 |
|
|
{ |
190 |
|
✗ |
VAAPIEncodeH264Context *priv = avctx->priv_data; |
191 |
|
✗ |
CodedBitstreamFragment *au = &priv->current_access_unit; |
192 |
|
|
int err; |
193 |
|
|
|
194 |
|
✗ |
if (priv->aud_needed) { |
195 |
|
✗ |
err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud); |
196 |
|
✗ |
if (err < 0) |
197 |
|
✗ |
goto fail; |
198 |
|
✗ |
priv->aud_needed = 0; |
199 |
|
|
} |
200 |
|
|
|
201 |
|
✗ |
err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_slice); |
202 |
|
✗ |
if (err < 0) |
203 |
|
✗ |
goto fail; |
204 |
|
|
|
205 |
|
✗ |
err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au); |
206 |
|
✗ |
fail: |
207 |
|
✗ |
ff_cbs_fragment_reset(au); |
208 |
|
✗ |
return err; |
209 |
|
|
} |
210 |
|
|
|
211 |
|
✗ |
static int vaapi_encode_h264_write_extra_header(AVCodecContext *avctx, |
212 |
|
|
VAAPIEncodePicture *pic, |
213 |
|
|
int index, int *type, |
214 |
|
|
char *data, size_t *data_len) |
215 |
|
|
{ |
216 |
|
✗ |
VAAPIEncodeH264Context *priv = avctx->priv_data; |
217 |
|
✗ |
CodedBitstreamFragment *au = &priv->current_access_unit; |
218 |
|
|
int err; |
219 |
|
|
|
220 |
|
✗ |
if (priv->sei_needed) { |
221 |
|
✗ |
if (priv->aud_needed) { |
222 |
|
✗ |
err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud); |
223 |
|
✗ |
if (err < 0) |
224 |
|
✗ |
goto fail; |
225 |
|
✗ |
priv->aud_needed = 0; |
226 |
|
|
} |
227 |
|
|
|
228 |
|
✗ |
if (priv->sei_needed & SEI_IDENTIFIER) { |
229 |
|
✗ |
err = ff_cbs_sei_add_message(priv->cbc, au, 1, |
230 |
|
|
SEI_TYPE_USER_DATA_UNREGISTERED, |
231 |
|
✗ |
&priv->sei_identifier, NULL); |
232 |
|
✗ |
if (err < 0) |
233 |
|
✗ |
goto fail; |
234 |
|
|
} |
235 |
|
✗ |
if (priv->sei_needed & SEI_TIMING) { |
236 |
|
✗ |
if (pic->type == PICTURE_TYPE_IDR) { |
237 |
|
✗ |
err = ff_cbs_sei_add_message(priv->cbc, au, 1, |
238 |
|
|
SEI_TYPE_BUFFERING_PERIOD, |
239 |
|
✗ |
&priv->sei_buffering_period, NULL); |
240 |
|
✗ |
if (err < 0) |
241 |
|
✗ |
goto fail; |
242 |
|
|
} |
243 |
|
✗ |
err = ff_cbs_sei_add_message(priv->cbc, au, 1, |
244 |
|
|
SEI_TYPE_PIC_TIMING, |
245 |
|
✗ |
&priv->sei_pic_timing, NULL); |
246 |
|
✗ |
if (err < 0) |
247 |
|
✗ |
goto fail; |
248 |
|
|
} |
249 |
|
✗ |
if (priv->sei_needed & SEI_RECOVERY_POINT) { |
250 |
|
✗ |
err = ff_cbs_sei_add_message(priv->cbc, au, 1, |
251 |
|
|
SEI_TYPE_RECOVERY_POINT, |
252 |
|
✗ |
&priv->sei_recovery_point, NULL); |
253 |
|
✗ |
if (err < 0) |
254 |
|
✗ |
goto fail; |
255 |
|
|
} |
256 |
|
✗ |
if (priv->sei_needed & SEI_A53_CC) { |
257 |
|
✗ |
err = ff_cbs_sei_add_message(priv->cbc, au, 1, |
258 |
|
|
SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35, |
259 |
|
✗ |
&priv->sei_a53cc, NULL); |
260 |
|
✗ |
if (err < 0) |
261 |
|
✗ |
goto fail; |
262 |
|
|
} |
263 |
|
|
|
264 |
|
✗ |
priv->sei_needed = 0; |
265 |
|
|
|
266 |
|
✗ |
err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au); |
267 |
|
✗ |
if (err < 0) |
268 |
|
✗ |
goto fail; |
269 |
|
|
|
270 |
|
✗ |
ff_cbs_fragment_reset(au); |
271 |
|
|
|
272 |
|
✗ |
*type = VAEncPackedHeaderRawData; |
273 |
|
✗ |
return 0; |
274 |
|
|
|
275 |
|
|
#if !CONFIG_VAAPI_1 |
276 |
|
|
} else if (priv->sei_cbr_workaround_needed) { |
277 |
|
|
// Insert a zero-length header using the old SEI type. This is |
278 |
|
|
// required to avoid triggering broken behaviour on Intel platforms |
279 |
|
|
// in CBR mode where an invalid SEI message is generated by the |
280 |
|
|
// driver and inserted into the stream. |
281 |
|
|
*data_len = 0; |
282 |
|
|
*type = VAEncPackedHeaderH264_SEI; |
283 |
|
|
priv->sei_cbr_workaround_needed = 0; |
284 |
|
|
return 0; |
285 |
|
|
#endif |
286 |
|
|
|
287 |
|
|
} else { |
288 |
|
✗ |
return AVERROR_EOF; |
289 |
|
|
} |
290 |
|
|
|
291 |
|
✗ |
fail: |
292 |
|
✗ |
ff_cbs_fragment_reset(au); |
293 |
|
✗ |
return err; |
294 |
|
|
} |
295 |
|
|
|
296 |
|
✗ |
static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx) |
297 |
|
|
{ |
298 |
|
✗ |
VAAPIEncodeContext *ctx = avctx->priv_data; |
299 |
|
✗ |
VAAPIEncodeH264Context *priv = avctx->priv_data; |
300 |
|
✗ |
H264RawSPS *sps = &priv->raw_sps; |
301 |
|
✗ |
H264RawPPS *pps = &priv->raw_pps; |
302 |
|
✗ |
VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params; |
303 |
|
✗ |
VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params; |
304 |
|
|
const AVPixFmtDescriptor *desc; |
305 |
|
|
int bit_depth; |
306 |
|
|
|
307 |
|
✗ |
memset(sps, 0, sizeof(*sps)); |
308 |
|
✗ |
memset(pps, 0, sizeof(*pps)); |
309 |
|
|
|
310 |
|
✗ |
desc = av_pix_fmt_desc_get(priv->common.input_frames->sw_format); |
311 |
|
✗ |
av_assert0(desc); |
312 |
|
✗ |
if (desc->nb_components == 1 || desc->log2_chroma_w != 1 || desc->log2_chroma_h != 1) { |
313 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Chroma format of input pixel format " |
314 |
|
✗ |
"%s is not supported.\n", desc->name); |
315 |
|
✗ |
return AVERROR(EINVAL); |
316 |
|
|
} |
317 |
|
✗ |
bit_depth = desc->comp[0].depth; |
318 |
|
|
|
319 |
|
✗ |
sps->nal_unit_header.nal_ref_idc = 3; |
320 |
|
✗ |
sps->nal_unit_header.nal_unit_type = H264_NAL_SPS; |
321 |
|
|
|
322 |
|
✗ |
sps->profile_idc = avctx->profile & 0xff; |
323 |
|
|
|
324 |
|
✗ |
if (avctx->profile == AV_PROFILE_H264_CONSTRAINED_BASELINE || |
325 |
|
✗ |
avctx->profile == AV_PROFILE_H264_MAIN) |
326 |
|
✗ |
sps->constraint_set1_flag = 1; |
327 |
|
|
|
328 |
|
✗ |
if (avctx->profile == AV_PROFILE_H264_HIGH || avctx->profile == AV_PROFILE_H264_HIGH_10) |
329 |
|
✗ |
sps->constraint_set3_flag = ctx->gop_size == 1; |
330 |
|
|
|
331 |
|
✗ |
if (avctx->profile == AV_PROFILE_H264_MAIN || |
332 |
|
✗ |
avctx->profile == AV_PROFILE_H264_HIGH || avctx->profile == AV_PROFILE_H264_HIGH_10) { |
333 |
|
✗ |
sps->constraint_set4_flag = 1; |
334 |
|
✗ |
sps->constraint_set5_flag = ctx->b_per_p == 0; |
335 |
|
|
} |
336 |
|
|
|
337 |
|
✗ |
if (ctx->gop_size == 1) |
338 |
|
✗ |
priv->dpb_frames = 0; |
339 |
|
|
else |
340 |
|
✗ |
priv->dpb_frames = 1 + ctx->max_b_depth; |
341 |
|
|
|
342 |
|
✗ |
if (avctx->level != AV_LEVEL_UNKNOWN) { |
343 |
|
✗ |
sps->level_idc = avctx->level; |
344 |
|
|
} else { |
345 |
|
|
const H264LevelDescriptor *level; |
346 |
|
|
int framerate; |
347 |
|
|
|
348 |
|
✗ |
if (avctx->framerate.num > 0 && avctx->framerate.den > 0) |
349 |
|
✗ |
framerate = avctx->framerate.num / avctx->framerate.den; |
350 |
|
|
else |
351 |
|
✗ |
framerate = 0; |
352 |
|
|
|
353 |
|
✗ |
level = ff_h264_guess_level(sps->profile_idc, |
354 |
|
|
avctx->bit_rate, |
355 |
|
|
framerate, |
356 |
|
✗ |
priv->mb_width * 16, |
357 |
|
✗ |
priv->mb_height * 16, |
358 |
|
|
priv->dpb_frames); |
359 |
|
✗ |
if (level) { |
360 |
|
✗ |
av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name); |
361 |
|
✗ |
if (level->constraint_set3_flag) |
362 |
|
✗ |
sps->constraint_set3_flag = 1; |
363 |
|
✗ |
sps->level_idc = level->level_idc; |
364 |
|
|
} else { |
365 |
|
✗ |
av_log(avctx, AV_LOG_WARNING, "Stream will not conform " |
366 |
|
|
"to any level: using level 6.2.\n"); |
367 |
|
✗ |
sps->level_idc = 62; |
368 |
|
|
} |
369 |
|
|
} |
370 |
|
|
|
371 |
|
✗ |
sps->seq_parameter_set_id = 0; |
372 |
|
✗ |
sps->chroma_format_idc = 1; |
373 |
|
✗ |
sps->bit_depth_luma_minus8 = bit_depth - 8; |
374 |
|
✗ |
sps->bit_depth_chroma_minus8 = bit_depth - 8; |
375 |
|
|
|
376 |
|
✗ |
sps->log2_max_frame_num_minus4 = 4; |
377 |
|
✗ |
sps->pic_order_cnt_type = ctx->max_b_depth ? 0 : 2; |
378 |
|
✗ |
if (sps->pic_order_cnt_type == 0) { |
379 |
|
✗ |
sps->log2_max_pic_order_cnt_lsb_minus4 = 4; |
380 |
|
|
} |
381 |
|
|
|
382 |
|
✗ |
sps->max_num_ref_frames = priv->dpb_frames; |
383 |
|
|
|
384 |
|
✗ |
sps->pic_width_in_mbs_minus1 = priv->mb_width - 1; |
385 |
|
✗ |
sps->pic_height_in_map_units_minus1 = priv->mb_height - 1; |
386 |
|
|
|
387 |
|
✗ |
sps->frame_mbs_only_flag = 1; |
388 |
|
✗ |
sps->direct_8x8_inference_flag = 1; |
389 |
|
|
|
390 |
|
✗ |
if (avctx->width != 16 * priv->mb_width || |
391 |
|
✗ |
avctx->height != 16 * priv->mb_height) { |
392 |
|
✗ |
sps->frame_cropping_flag = 1; |
393 |
|
|
|
394 |
|
✗ |
sps->frame_crop_left_offset = 0; |
395 |
|
✗ |
sps->frame_crop_right_offset = |
396 |
|
✗ |
(16 * priv->mb_width - avctx->width) / 2; |
397 |
|
✗ |
sps->frame_crop_top_offset = 0; |
398 |
|
✗ |
sps->frame_crop_bottom_offset = |
399 |
|
✗ |
(16 * priv->mb_height - avctx->height) / 2; |
400 |
|
|
} else { |
401 |
|
✗ |
sps->frame_cropping_flag = 0; |
402 |
|
|
} |
403 |
|
|
|
404 |
|
✗ |
sps->vui_parameters_present_flag = 1; |
405 |
|
|
|
406 |
|
✗ |
if (avctx->sample_aspect_ratio.num != 0 && |
407 |
|
✗ |
avctx->sample_aspect_ratio.den != 0) { |
408 |
|
|
int num, den, i; |
409 |
|
✗ |
av_reduce(&num, &den, avctx->sample_aspect_ratio.num, |
410 |
|
✗ |
avctx->sample_aspect_ratio.den, 65535); |
411 |
|
✗ |
for (i = 0; i < FF_ARRAY_ELEMS(ff_h2645_pixel_aspect); i++) { |
412 |
|
✗ |
if (num == ff_h2645_pixel_aspect[i].num && |
413 |
|
✗ |
den == ff_h2645_pixel_aspect[i].den) { |
414 |
|
✗ |
sps->vui.aspect_ratio_idc = i; |
415 |
|
✗ |
break; |
416 |
|
|
} |
417 |
|
|
} |
418 |
|
✗ |
if (i >= FF_ARRAY_ELEMS(ff_h2645_pixel_aspect)) { |
419 |
|
✗ |
sps->vui.aspect_ratio_idc = 255; |
420 |
|
✗ |
sps->vui.sar_width = num; |
421 |
|
✗ |
sps->vui.sar_height = den; |
422 |
|
|
} |
423 |
|
✗ |
sps->vui.aspect_ratio_info_present_flag = 1; |
424 |
|
|
} |
425 |
|
|
|
426 |
|
|
// Unspecified video format, from table E-2. |
427 |
|
✗ |
sps->vui.video_format = 5; |
428 |
|
✗ |
sps->vui.video_full_range_flag = |
429 |
|
✗ |
avctx->color_range == AVCOL_RANGE_JPEG; |
430 |
|
✗ |
sps->vui.colour_primaries = avctx->color_primaries; |
431 |
|
✗ |
sps->vui.transfer_characteristics = avctx->color_trc; |
432 |
|
✗ |
sps->vui.matrix_coefficients = avctx->colorspace; |
433 |
|
✗ |
if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED || |
434 |
|
✗ |
avctx->color_trc != AVCOL_TRC_UNSPECIFIED || |
435 |
|
✗ |
avctx->colorspace != AVCOL_SPC_UNSPECIFIED) |
436 |
|
✗ |
sps->vui.colour_description_present_flag = 1; |
437 |
|
✗ |
if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED || |
438 |
|
✗ |
sps->vui.colour_description_present_flag) |
439 |
|
✗ |
sps->vui.video_signal_type_present_flag = 1; |
440 |
|
|
|
441 |
|
✗ |
if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) { |
442 |
|
✗ |
sps->vui.chroma_loc_info_present_flag = 1; |
443 |
|
✗ |
sps->vui.chroma_sample_loc_type_top_field = |
444 |
|
✗ |
sps->vui.chroma_sample_loc_type_bottom_field = |
445 |
|
✗ |
avctx->chroma_sample_location - 1; |
446 |
|
|
} |
447 |
|
|
|
448 |
|
✗ |
sps->vui.timing_info_present_flag = 1; |
449 |
|
✗ |
if (avctx->framerate.num > 0 && avctx->framerate.den > 0) { |
450 |
|
✗ |
sps->vui.num_units_in_tick = avctx->framerate.den; |
451 |
|
✗ |
sps->vui.time_scale = 2 * avctx->framerate.num; |
452 |
|
✗ |
sps->vui.fixed_frame_rate_flag = 1; |
453 |
|
|
} else { |
454 |
|
✗ |
sps->vui.num_units_in_tick = avctx->time_base.num; |
455 |
|
✗ |
sps->vui.time_scale = 2 * avctx->time_base.den; |
456 |
|
✗ |
sps->vui.fixed_frame_rate_flag = 0; |
457 |
|
|
} |
458 |
|
|
|
459 |
|
✗ |
if (priv->sei & SEI_TIMING) { |
460 |
|
✗ |
H264RawHRD *hrd = &sps->vui.nal_hrd_parameters; |
461 |
|
✗ |
H264RawSEIBufferingPeriod *bp = &priv->sei_buffering_period; |
462 |
|
|
|
463 |
|
✗ |
sps->vui.nal_hrd_parameters_present_flag = 1; |
464 |
|
|
|
465 |
|
✗ |
hrd->cpb_cnt_minus1 = 0; |
466 |
|
|
|
467 |
|
|
// Try to scale these to a sensible range so that the |
468 |
|
|
// golomb encode of the value is not overlong. |
469 |
|
✗ |
hrd->bit_rate_scale = |
470 |
|
✗ |
av_clip_uintp2(av_log2(ctx->va_bit_rate) - 15 - 6, 4); |
471 |
|
✗ |
hrd->bit_rate_value_minus1[0] = |
472 |
|
✗ |
(ctx->va_bit_rate >> hrd->bit_rate_scale + 6) - 1; |
473 |
|
|
|
474 |
|
✗ |
hrd->cpb_size_scale = |
475 |
|
✗ |
av_clip_uintp2(av_log2(ctx->hrd_params.buffer_size) - 15 - 4, 4); |
476 |
|
✗ |
hrd->cpb_size_value_minus1[0] = |
477 |
|
✗ |
(ctx->hrd_params.buffer_size >> hrd->cpb_size_scale + 4) - 1; |
478 |
|
|
|
479 |
|
|
// CBR mode as defined for the HRD cannot be achieved without filler |
480 |
|
|
// data, so this flag cannot be set even with VAAPI CBR modes. |
481 |
|
✗ |
hrd->cbr_flag[0] = 0; |
482 |
|
|
|
483 |
|
✗ |
hrd->initial_cpb_removal_delay_length_minus1 = 23; |
484 |
|
✗ |
hrd->cpb_removal_delay_length_minus1 = 23; |
485 |
|
✗ |
hrd->dpb_output_delay_length_minus1 = 7; |
486 |
|
✗ |
hrd->time_offset_length = 0; |
487 |
|
|
|
488 |
|
✗ |
bp->seq_parameter_set_id = sps->seq_parameter_set_id; |
489 |
|
|
|
490 |
|
|
// This calculation can easily overflow 32 bits. |
491 |
|
✗ |
bp->nal.initial_cpb_removal_delay[0] = 90000 * |
492 |
|
✗ |
(uint64_t)ctx->hrd_params.initial_buffer_fullness / |
493 |
|
✗ |
ctx->hrd_params.buffer_size; |
494 |
|
✗ |
bp->nal.initial_cpb_removal_delay_offset[0] = 0; |
495 |
|
|
} else { |
496 |
|
✗ |
sps->vui.nal_hrd_parameters_present_flag = 0; |
497 |
|
✗ |
sps->vui.low_delay_hrd_flag = 1 - sps->vui.fixed_frame_rate_flag; |
498 |
|
|
} |
499 |
|
|
|
500 |
|
✗ |
sps->vui.bitstream_restriction_flag = 1; |
501 |
|
✗ |
sps->vui.motion_vectors_over_pic_boundaries_flag = 1; |
502 |
|
✗ |
sps->vui.log2_max_mv_length_horizontal = 15; |
503 |
|
✗ |
sps->vui.log2_max_mv_length_vertical = 15; |
504 |
|
✗ |
sps->vui.max_num_reorder_frames = ctx->max_b_depth; |
505 |
|
✗ |
sps->vui.max_dec_frame_buffering = ctx->max_b_depth + 1; |
506 |
|
|
|
507 |
|
✗ |
pps->nal_unit_header.nal_ref_idc = 3; |
508 |
|
✗ |
pps->nal_unit_header.nal_unit_type = H264_NAL_PPS; |
509 |
|
|
|
510 |
|
✗ |
pps->pic_parameter_set_id = 0; |
511 |
|
✗ |
pps->seq_parameter_set_id = 0; |
512 |
|
|
|
513 |
|
✗ |
pps->entropy_coding_mode_flag = |
514 |
|
✗ |
!(sps->profile_idc == AV_PROFILE_H264_BASELINE || |
515 |
|
✗ |
sps->profile_idc == AV_PROFILE_H264_EXTENDED || |
516 |
|
✗ |
sps->profile_idc == AV_PROFILE_H264_CAVLC_444); |
517 |
|
✗ |
if (!priv->coder && pps->entropy_coding_mode_flag) |
518 |
|
✗ |
pps->entropy_coding_mode_flag = 0; |
519 |
|
|
|
520 |
|
✗ |
pps->num_ref_idx_l0_default_active_minus1 = 0; |
521 |
|
✗ |
pps->num_ref_idx_l1_default_active_minus1 = 0; |
522 |
|
|
|
523 |
|
✗ |
pps->pic_init_qp_minus26 = priv->fixed_qp_idr - 26; |
524 |
|
|
|
525 |
|
✗ |
if (sps->profile_idc == AV_PROFILE_H264_BASELINE || |
526 |
|
✗ |
sps->profile_idc == AV_PROFILE_H264_EXTENDED || |
527 |
|
✗ |
sps->profile_idc == AV_PROFILE_H264_MAIN) { |
528 |
|
✗ |
pps->more_rbsp_data = 0; |
529 |
|
|
} else { |
530 |
|
✗ |
pps->more_rbsp_data = 1; |
531 |
|
|
|
532 |
|
✗ |
pps->transform_8x8_mode_flag = 1; |
533 |
|
|
} |
534 |
|
|
|
535 |
|
✗ |
*vseq = (VAEncSequenceParameterBufferH264) { |
536 |
|
✗ |
.seq_parameter_set_id = sps->seq_parameter_set_id, |
537 |
|
✗ |
.level_idc = sps->level_idc, |
538 |
|
✗ |
.intra_period = ctx->gop_size, |
539 |
|
✗ |
.intra_idr_period = ctx->gop_size, |
540 |
|
✗ |
.ip_period = ctx->b_per_p + 1, |
541 |
|
|
|
542 |
|
✗ |
.bits_per_second = ctx->va_bit_rate, |
543 |
|
✗ |
.max_num_ref_frames = sps->max_num_ref_frames, |
544 |
|
✗ |
.picture_width_in_mbs = sps->pic_width_in_mbs_minus1 + 1, |
545 |
|
✗ |
.picture_height_in_mbs = sps->pic_height_in_map_units_minus1 + 1, |
546 |
|
|
|
547 |
|
|
.seq_fields.bits = { |
548 |
|
✗ |
.chroma_format_idc = sps->chroma_format_idc, |
549 |
|
✗ |
.frame_mbs_only_flag = sps->frame_mbs_only_flag, |
550 |
|
✗ |
.mb_adaptive_frame_field_flag = sps->mb_adaptive_frame_field_flag, |
551 |
|
✗ |
.seq_scaling_matrix_present_flag = sps->seq_scaling_matrix_present_flag, |
552 |
|
✗ |
.direct_8x8_inference_flag = sps->direct_8x8_inference_flag, |
553 |
|
✗ |
.log2_max_frame_num_minus4 = sps->log2_max_frame_num_minus4, |
554 |
|
✗ |
.pic_order_cnt_type = sps->pic_order_cnt_type, |
555 |
|
✗ |
.log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_pic_order_cnt_lsb_minus4, |
556 |
|
✗ |
.delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag, |
557 |
|
|
}, |
558 |
|
|
|
559 |
|
✗ |
.bit_depth_luma_minus8 = sps->bit_depth_luma_minus8, |
560 |
|
✗ |
.bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8, |
561 |
|
|
|
562 |
|
✗ |
.frame_cropping_flag = sps->frame_cropping_flag, |
563 |
|
✗ |
.frame_crop_left_offset = sps->frame_crop_left_offset, |
564 |
|
✗ |
.frame_crop_right_offset = sps->frame_crop_right_offset, |
565 |
|
✗ |
.frame_crop_top_offset = sps->frame_crop_top_offset, |
566 |
|
✗ |
.frame_crop_bottom_offset = sps->frame_crop_bottom_offset, |
567 |
|
|
|
568 |
|
✗ |
.vui_parameters_present_flag = sps->vui_parameters_present_flag, |
569 |
|
|
|
570 |
|
|
.vui_fields.bits = { |
571 |
|
✗ |
.aspect_ratio_info_present_flag = sps->vui.aspect_ratio_info_present_flag, |
572 |
|
✗ |
.timing_info_present_flag = sps->vui.timing_info_present_flag, |
573 |
|
✗ |
.bitstream_restriction_flag = sps->vui.bitstream_restriction_flag, |
574 |
|
✗ |
.log2_max_mv_length_horizontal = sps->vui.log2_max_mv_length_horizontal, |
575 |
|
✗ |
.log2_max_mv_length_vertical = sps->vui.log2_max_mv_length_vertical, |
576 |
|
|
}, |
577 |
|
|
|
578 |
|
✗ |
.aspect_ratio_idc = sps->vui.aspect_ratio_idc, |
579 |
|
✗ |
.sar_width = sps->vui.sar_width, |
580 |
|
✗ |
.sar_height = sps->vui.sar_height, |
581 |
|
✗ |
.num_units_in_tick = sps->vui.num_units_in_tick, |
582 |
|
✗ |
.time_scale = sps->vui.time_scale, |
583 |
|
|
}; |
584 |
|
|
|
585 |
|
✗ |
*vpic = (VAEncPictureParameterBufferH264) { |
586 |
|
|
.CurrPic = { |
587 |
|
|
.picture_id = VA_INVALID_ID, |
588 |
|
|
.flags = VA_PICTURE_H264_INVALID, |
589 |
|
|
}, |
590 |
|
|
|
591 |
|
|
.coded_buf = VA_INVALID_ID, |
592 |
|
|
|
593 |
|
✗ |
.pic_parameter_set_id = pps->pic_parameter_set_id, |
594 |
|
✗ |
.seq_parameter_set_id = pps->seq_parameter_set_id, |
595 |
|
|
|
596 |
|
✗ |
.pic_init_qp = pps->pic_init_qp_minus26 + 26, |
597 |
|
✗ |
.num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1, |
598 |
|
✗ |
.num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1, |
599 |
|
|
|
600 |
|
✗ |
.chroma_qp_index_offset = pps->chroma_qp_index_offset, |
601 |
|
✗ |
.second_chroma_qp_index_offset = pps->second_chroma_qp_index_offset, |
602 |
|
|
|
603 |
|
|
.pic_fields.bits = { |
604 |
|
✗ |
.entropy_coding_mode_flag = pps->entropy_coding_mode_flag, |
605 |
|
✗ |
.weighted_pred_flag = pps->weighted_pred_flag, |
606 |
|
✗ |
.weighted_bipred_idc = pps->weighted_bipred_idc, |
607 |
|
✗ |
.constrained_intra_pred_flag = pps->constrained_intra_pred_flag, |
608 |
|
✗ |
.transform_8x8_mode_flag = pps->transform_8x8_mode_flag, |
609 |
|
|
.deblocking_filter_control_present_flag = |
610 |
|
✗ |
pps->deblocking_filter_control_present_flag, |
611 |
|
✗ |
.redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present_flag, |
612 |
|
|
.pic_order_present_flag = |
613 |
|
✗ |
pps->bottom_field_pic_order_in_frame_present_flag, |
614 |
|
✗ |
.pic_scaling_matrix_present_flag = pps->pic_scaling_matrix_present_flag, |
615 |
|
|
}, |
616 |
|
|
}; |
617 |
|
|
|
618 |
|
✗ |
return 0; |
619 |
|
|
} |
620 |
|
|
|
621 |
|
✗ |
static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx, |
622 |
|
|
VAAPIEncodePicture *pic) |
623 |
|
|
{ |
624 |
|
✗ |
VAAPIEncodeContext *ctx = avctx->priv_data; |
625 |
|
✗ |
VAAPIEncodeH264Context *priv = avctx->priv_data; |
626 |
|
✗ |
VAAPIEncodeH264Picture *hpic = pic->priv_data; |
627 |
|
✗ |
VAAPIEncodePicture *prev = pic->prev; |
628 |
|
✗ |
VAAPIEncodeH264Picture *hprev = prev ? prev->priv_data : NULL; |
629 |
|
✗ |
VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params; |
630 |
|
✗ |
int i, j = 0; |
631 |
|
|
|
632 |
|
✗ |
if (pic->type == PICTURE_TYPE_IDR) { |
633 |
|
✗ |
av_assert0(pic->display_order == pic->encode_order); |
634 |
|
|
|
635 |
|
✗ |
hpic->frame_num = 0; |
636 |
|
✗ |
hpic->last_idr_frame = pic->display_order; |
637 |
|
✗ |
hpic->idr_pic_id = hprev ? hprev->idr_pic_id + 1 : 0; |
638 |
|
|
|
639 |
|
✗ |
hpic->primary_pic_type = 0; |
640 |
|
✗ |
hpic->slice_type = 7; |
641 |
|
|
} else { |
642 |
|
✗ |
av_assert0(prev); |
643 |
|
|
|
644 |
|
✗ |
hpic->frame_num = hprev->frame_num + prev->is_reference; |
645 |
|
|
|
646 |
|
✗ |
hpic->last_idr_frame = hprev->last_idr_frame; |
647 |
|
✗ |
hpic->idr_pic_id = hprev->idr_pic_id; |
648 |
|
|
|
649 |
|
✗ |
if (pic->type == PICTURE_TYPE_I) { |
650 |
|
✗ |
hpic->slice_type = 7; |
651 |
|
✗ |
hpic->primary_pic_type = 0; |
652 |
|
✗ |
} else if (pic->type == PICTURE_TYPE_P) { |
653 |
|
✗ |
hpic->slice_type = 5; |
654 |
|
✗ |
hpic->primary_pic_type = 1; |
655 |
|
|
} else { |
656 |
|
✗ |
hpic->slice_type = 6; |
657 |
|
✗ |
hpic->primary_pic_type = 2; |
658 |
|
|
} |
659 |
|
|
} |
660 |
|
✗ |
hpic->pic_order_cnt = pic->display_order - hpic->last_idr_frame; |
661 |
|
✗ |
if (priv->raw_sps.pic_order_cnt_type == 2) { |
662 |
|
✗ |
hpic->pic_order_cnt *= 2; |
663 |
|
|
} |
664 |
|
|
|
665 |
|
✗ |
hpic->dpb_delay = pic->display_order - pic->encode_order + ctx->max_b_depth; |
666 |
|
✗ |
hpic->cpb_delay = pic->encode_order - hpic->last_idr_frame; |
667 |
|
|
|
668 |
|
✗ |
if (priv->aud) { |
669 |
|
✗ |
priv->aud_needed = 1; |
670 |
|
✗ |
priv->raw_aud = (H264RawAUD) { |
671 |
|
|
.nal_unit_header = { |
672 |
|
|
.nal_unit_type = H264_NAL_AUD, |
673 |
|
|
}, |
674 |
|
✗ |
.primary_pic_type = hpic->primary_pic_type, |
675 |
|
|
}; |
676 |
|
|
} else { |
677 |
|
✗ |
priv->aud_needed = 0; |
678 |
|
|
} |
679 |
|
|
|
680 |
|
✗ |
priv->sei_needed = 0; |
681 |
|
|
|
682 |
|
✗ |
if (priv->sei & SEI_IDENTIFIER && pic->encode_order == 0) |
683 |
|
✗ |
priv->sei_needed |= SEI_IDENTIFIER; |
684 |
|
|
#if !CONFIG_VAAPI_1 |
685 |
|
|
if (ctx->va_rc_mode == VA_RC_CBR) |
686 |
|
|
priv->sei_cbr_workaround_needed = 1; |
687 |
|
|
#endif |
688 |
|
|
|
689 |
|
✗ |
if (priv->sei & SEI_TIMING) { |
690 |
|
✗ |
priv->sei_pic_timing = (H264RawSEIPicTiming) { |
691 |
|
✗ |
.cpb_removal_delay = 2 * hpic->cpb_delay, |
692 |
|
✗ |
.dpb_output_delay = 2 * hpic->dpb_delay, |
693 |
|
|
}; |
694 |
|
|
|
695 |
|
✗ |
priv->sei_needed |= SEI_TIMING; |
696 |
|
|
} |
697 |
|
|
|
698 |
|
✗ |
if (priv->sei & SEI_RECOVERY_POINT && pic->type == PICTURE_TYPE_I) { |
699 |
|
✗ |
priv->sei_recovery_point = (H264RawSEIRecoveryPoint) { |
700 |
|
|
.recovery_frame_cnt = 0, |
701 |
|
|
.exact_match_flag = 1, |
702 |
|
✗ |
.broken_link_flag = ctx->b_per_p > 0, |
703 |
|
|
}; |
704 |
|
|
|
705 |
|
✗ |
priv->sei_needed |= SEI_RECOVERY_POINT; |
706 |
|
|
} |
707 |
|
|
|
708 |
|
✗ |
if (priv->sei & SEI_A53_CC) { |
709 |
|
|
int err; |
710 |
|
|
size_t sei_a53cc_len; |
711 |
|
✗ |
av_freep(&priv->sei_a53cc_data); |
712 |
|
✗ |
err = ff_alloc_a53_sei(pic->input_image, 0, &priv->sei_a53cc_data, &sei_a53cc_len); |
713 |
|
✗ |
if (err < 0) |
714 |
|
✗ |
return err; |
715 |
|
✗ |
if (priv->sei_a53cc_data != NULL) { |
716 |
|
✗ |
priv->sei_a53cc.itu_t_t35_country_code = 181; |
717 |
|
✗ |
priv->sei_a53cc.data = (uint8_t *)priv->sei_a53cc_data + 1; |
718 |
|
✗ |
priv->sei_a53cc.data_length = sei_a53cc_len - 1; |
719 |
|
|
|
720 |
|
✗ |
priv->sei_needed |= SEI_A53_CC; |
721 |
|
|
} |
722 |
|
|
} |
723 |
|
|
|
724 |
|
✗ |
vpic->CurrPic = (VAPictureH264) { |
725 |
|
✗ |
.picture_id = pic->recon_surface, |
726 |
|
✗ |
.frame_idx = hpic->frame_num, |
727 |
|
|
.flags = 0, |
728 |
|
✗ |
.TopFieldOrderCnt = hpic->pic_order_cnt, |
729 |
|
✗ |
.BottomFieldOrderCnt = hpic->pic_order_cnt, |
730 |
|
|
}; |
731 |
|
✗ |
for (int k = 0; k < MAX_REFERENCE_LIST_NUM; k++) { |
732 |
|
✗ |
for (i = 0; i < pic->nb_refs[k]; i++) { |
733 |
|
✗ |
VAAPIEncodePicture *ref = pic->refs[k][i]; |
734 |
|
|
VAAPIEncodeH264Picture *href; |
735 |
|
|
|
736 |
|
✗ |
av_assert0(ref && ref->encode_order < pic->encode_order); |
737 |
|
✗ |
href = ref->priv_data; |
738 |
|
|
|
739 |
|
✗ |
vpic->ReferenceFrames[j++] = (VAPictureH264) { |
740 |
|
✗ |
.picture_id = ref->recon_surface, |
741 |
|
✗ |
.frame_idx = href->frame_num, |
742 |
|
|
.flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE, |
743 |
|
✗ |
.TopFieldOrderCnt = href->pic_order_cnt, |
744 |
|
✗ |
.BottomFieldOrderCnt = href->pic_order_cnt, |
745 |
|
|
}; |
746 |
|
|
} |
747 |
|
|
} |
748 |
|
|
|
749 |
|
✗ |
for (; j < FF_ARRAY_ELEMS(vpic->ReferenceFrames); j++) { |
750 |
|
✗ |
vpic->ReferenceFrames[j] = (VAPictureH264) { |
751 |
|
|
.picture_id = VA_INVALID_ID, |
752 |
|
|
.flags = VA_PICTURE_H264_INVALID, |
753 |
|
|
}; |
754 |
|
|
} |
755 |
|
|
|
756 |
|
✗ |
vpic->coded_buf = pic->output_buffer; |
757 |
|
|
|
758 |
|
✗ |
vpic->frame_num = hpic->frame_num; |
759 |
|
|
|
760 |
|
✗ |
vpic->pic_fields.bits.idr_pic_flag = (pic->type == PICTURE_TYPE_IDR); |
761 |
|
✗ |
vpic->pic_fields.bits.reference_pic_flag = pic->is_reference; |
762 |
|
|
|
763 |
|
✗ |
return 0; |
764 |
|
|
} |
765 |
|
|
|
766 |
|
✗ |
static void vaapi_encode_h264_default_ref_pic_list(AVCodecContext *avctx, |
767 |
|
|
VAAPIEncodePicture *pic, |
768 |
|
|
VAAPIEncodePicture **rpl0, |
769 |
|
|
VAAPIEncodePicture **rpl1, |
770 |
|
|
int *rpl_size) |
771 |
|
|
{ |
772 |
|
|
VAAPIEncodePicture *prev; |
773 |
|
|
VAAPIEncodeH264Picture *hp, *hn, *hc; |
774 |
|
✗ |
int i, j, n = 0; |
775 |
|
|
|
776 |
|
✗ |
prev = pic->prev; |
777 |
|
✗ |
av_assert0(prev); |
778 |
|
✗ |
hp = pic->priv_data; |
779 |
|
|
|
780 |
|
✗ |
for (i = 0; i < pic->prev->nb_dpb_pics; i++) { |
781 |
|
✗ |
hn = prev->dpb[i]->priv_data; |
782 |
|
✗ |
av_assert0(hn->frame_num < hp->frame_num); |
783 |
|
|
|
784 |
|
✗ |
if (pic->type == PICTURE_TYPE_P) { |
785 |
|
✗ |
for (j = n; j > 0; j--) { |
786 |
|
✗ |
hc = rpl0[j - 1]->priv_data; |
787 |
|
✗ |
av_assert0(hc->frame_num != hn->frame_num); |
788 |
|
✗ |
if (hc->frame_num > hn->frame_num) |
789 |
|
✗ |
break; |
790 |
|
✗ |
rpl0[j] = rpl0[j - 1]; |
791 |
|
|
} |
792 |
|
✗ |
rpl0[j] = prev->dpb[i]; |
793 |
|
|
|
794 |
|
✗ |
} else if (pic->type == PICTURE_TYPE_B) { |
795 |
|
✗ |
for (j = n; j > 0; j--) { |
796 |
|
✗ |
hc = rpl0[j - 1]->priv_data; |
797 |
|
✗ |
av_assert0(hc->pic_order_cnt != hp->pic_order_cnt); |
798 |
|
✗ |
if (hc->pic_order_cnt < hp->pic_order_cnt) { |
799 |
|
✗ |
if (hn->pic_order_cnt > hp->pic_order_cnt || |
800 |
|
✗ |
hn->pic_order_cnt < hc->pic_order_cnt) |
801 |
|
|
break; |
802 |
|
|
} else { |
803 |
|
✗ |
if (hn->pic_order_cnt > hc->pic_order_cnt) |
804 |
|
✗ |
break; |
805 |
|
|
} |
806 |
|
✗ |
rpl0[j] = rpl0[j - 1]; |
807 |
|
|
} |
808 |
|
✗ |
rpl0[j] = prev->dpb[i]; |
809 |
|
|
|
810 |
|
✗ |
for (j = n; j > 0; j--) { |
811 |
|
✗ |
hc = rpl1[j - 1]->priv_data; |
812 |
|
✗ |
av_assert0(hc->pic_order_cnt != hp->pic_order_cnt); |
813 |
|
✗ |
if (hc->pic_order_cnt > hp->pic_order_cnt) { |
814 |
|
✗ |
if (hn->pic_order_cnt < hp->pic_order_cnt || |
815 |
|
✗ |
hn->pic_order_cnt > hc->pic_order_cnt) |
816 |
|
|
break; |
817 |
|
|
} else { |
818 |
|
✗ |
if (hn->pic_order_cnt < hc->pic_order_cnt) |
819 |
|
✗ |
break; |
820 |
|
|
} |
821 |
|
✗ |
rpl1[j] = rpl1[j - 1]; |
822 |
|
|
} |
823 |
|
✗ |
rpl1[j] = prev->dpb[i]; |
824 |
|
|
} |
825 |
|
|
|
826 |
|
✗ |
++n; |
827 |
|
|
} |
828 |
|
|
|
829 |
|
✗ |
if (pic->type == PICTURE_TYPE_B) { |
830 |
|
✗ |
for (i = 0; i < n; i++) { |
831 |
|
✗ |
if (rpl0[i] != rpl1[i]) |
832 |
|
✗ |
break; |
833 |
|
|
} |
834 |
|
✗ |
if (i == n) |
835 |
|
✗ |
FFSWAP(VAAPIEncodePicture*, rpl1[0], rpl1[1]); |
836 |
|
|
} |
837 |
|
|
|
838 |
|
✗ |
if (pic->type == PICTURE_TYPE_P || |
839 |
|
✗ |
pic->type == PICTURE_TYPE_B) { |
840 |
|
✗ |
av_log(avctx, AV_LOG_DEBUG, "Default RefPicList0 for fn=%d/poc=%d:", |
841 |
|
|
hp->frame_num, hp->pic_order_cnt); |
842 |
|
✗ |
for (i = 0; i < n; i++) { |
843 |
|
✗ |
hn = rpl0[i]->priv_data; |
844 |
|
✗ |
av_log(avctx, AV_LOG_DEBUG, " fn=%d/poc=%d", |
845 |
|
|
hn->frame_num, hn->pic_order_cnt); |
846 |
|
|
} |
847 |
|
✗ |
av_log(avctx, AV_LOG_DEBUG, "\n"); |
848 |
|
|
} |
849 |
|
✗ |
if (pic->type == PICTURE_TYPE_B) { |
850 |
|
✗ |
av_log(avctx, AV_LOG_DEBUG, "Default RefPicList1 for fn=%d/poc=%d:", |
851 |
|
|
hp->frame_num, hp->pic_order_cnt); |
852 |
|
✗ |
for (i = 0; i < n; i++) { |
853 |
|
✗ |
hn = rpl1[i]->priv_data; |
854 |
|
✗ |
av_log(avctx, AV_LOG_DEBUG, " fn=%d/poc=%d", |
855 |
|
|
hn->frame_num, hn->pic_order_cnt); |
856 |
|
|
} |
857 |
|
✗ |
av_log(avctx, AV_LOG_DEBUG, "\n"); |
858 |
|
|
} |
859 |
|
|
|
860 |
|
✗ |
*rpl_size = n; |
861 |
|
✗ |
} |
862 |
|
|
|
863 |
|
✗ |
static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx, |
864 |
|
|
VAAPIEncodePicture *pic, |
865 |
|
|
VAAPIEncodeSlice *slice) |
866 |
|
|
{ |
867 |
|
✗ |
VAAPIEncodeH264Context *priv = avctx->priv_data; |
868 |
|
✗ |
VAAPIEncodeH264Picture *hpic = pic->priv_data; |
869 |
|
✗ |
VAAPIEncodePicture *prev = pic->prev; |
870 |
|
✗ |
H264RawSPS *sps = &priv->raw_sps; |
871 |
|
✗ |
H264RawPPS *pps = &priv->raw_pps; |
872 |
|
✗ |
H264RawSliceHeader *sh = &priv->raw_slice.header; |
873 |
|
✗ |
VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params; |
874 |
|
✗ |
VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params; |
875 |
|
|
int i, j; |
876 |
|
|
|
877 |
|
✗ |
if (pic->type == PICTURE_TYPE_IDR) { |
878 |
|
✗ |
sh->nal_unit_header.nal_unit_type = H264_NAL_IDR_SLICE; |
879 |
|
✗ |
sh->nal_unit_header.nal_ref_idc = 3; |
880 |
|
|
} else { |
881 |
|
✗ |
sh->nal_unit_header.nal_unit_type = H264_NAL_SLICE; |
882 |
|
✗ |
sh->nal_unit_header.nal_ref_idc = pic->is_reference; |
883 |
|
|
} |
884 |
|
|
|
885 |
|
✗ |
sh->first_mb_in_slice = slice->block_start; |
886 |
|
✗ |
sh->slice_type = hpic->slice_type; |
887 |
|
|
|
888 |
|
✗ |
sh->pic_parameter_set_id = pps->pic_parameter_set_id; |
889 |
|
|
|
890 |
|
✗ |
sh->frame_num = hpic->frame_num & |
891 |
|
✗ |
((1 << (4 + sps->log2_max_frame_num_minus4)) - 1); |
892 |
|
✗ |
sh->idr_pic_id = hpic->idr_pic_id; |
893 |
|
✗ |
sh->pic_order_cnt_lsb = hpic->pic_order_cnt & |
894 |
|
✗ |
((1 << (4 + sps->log2_max_pic_order_cnt_lsb_minus4)) - 1); |
895 |
|
|
|
896 |
|
✗ |
sh->direct_spatial_mv_pred_flag = 1; |
897 |
|
|
|
898 |
|
✗ |
if (pic->type == PICTURE_TYPE_B) |
899 |
|
✗ |
sh->slice_qp_delta = priv->fixed_qp_b - (pps->pic_init_qp_minus26 + 26); |
900 |
|
✗ |
else if (pic->type == PICTURE_TYPE_P) |
901 |
|
✗ |
sh->slice_qp_delta = priv->fixed_qp_p - (pps->pic_init_qp_minus26 + 26); |
902 |
|
|
else |
903 |
|
✗ |
sh->slice_qp_delta = priv->fixed_qp_idr - (pps->pic_init_qp_minus26 + 26); |
904 |
|
|
|
905 |
|
✗ |
if (pic->is_reference && pic->type != PICTURE_TYPE_IDR) { |
906 |
|
|
VAAPIEncodePicture *discard_list[MAX_DPB_SIZE]; |
907 |
|
✗ |
int discard = 0, keep = 0; |
908 |
|
|
|
909 |
|
|
// Discard everything which is in the DPB of the previous frame but |
910 |
|
|
// not in the DPB of this one. |
911 |
|
✗ |
for (i = 0; i < prev->nb_dpb_pics; i++) { |
912 |
|
✗ |
for (j = 0; j < pic->nb_dpb_pics; j++) { |
913 |
|
✗ |
if (prev->dpb[i] == pic->dpb[j]) |
914 |
|
✗ |
break; |
915 |
|
|
} |
916 |
|
✗ |
if (j == pic->nb_dpb_pics) { |
917 |
|
✗ |
discard_list[discard] = prev->dpb[i]; |
918 |
|
✗ |
++discard; |
919 |
|
|
} else { |
920 |
|
✗ |
++keep; |
921 |
|
|
} |
922 |
|
|
} |
923 |
|
✗ |
av_assert0(keep <= priv->dpb_frames); |
924 |
|
|
|
925 |
|
✗ |
if (discard == 0) { |
926 |
|
✗ |
sh->adaptive_ref_pic_marking_mode_flag = 0; |
927 |
|
|
} else { |
928 |
|
✗ |
sh->adaptive_ref_pic_marking_mode_flag = 1; |
929 |
|
✗ |
for (i = 0; i < discard; i++) { |
930 |
|
✗ |
VAAPIEncodeH264Picture *old = discard_list[i]->priv_data; |
931 |
|
✗ |
av_assert0(old->frame_num < hpic->frame_num); |
932 |
|
✗ |
sh->mmco[i].memory_management_control_operation = 1; |
933 |
|
✗ |
sh->mmco[i].difference_of_pic_nums_minus1 = |
934 |
|
✗ |
hpic->frame_num - old->frame_num - 1; |
935 |
|
|
} |
936 |
|
✗ |
sh->mmco[i].memory_management_control_operation = 0; |
937 |
|
|
} |
938 |
|
|
} |
939 |
|
|
|
940 |
|
|
// If the intended references are not the first entries of RefPicListN |
941 |
|
|
// by default, use ref-pic-list-modification to move them there. |
942 |
|
✗ |
if (pic->type == PICTURE_TYPE_P || pic->type == PICTURE_TYPE_B) { |
943 |
|
|
VAAPIEncodePicture *def_l0[MAX_DPB_SIZE], *def_l1[MAX_DPB_SIZE]; |
944 |
|
|
VAAPIEncodeH264Picture *href; |
945 |
|
|
int n; |
946 |
|
|
|
947 |
|
✗ |
vaapi_encode_h264_default_ref_pic_list(avctx, pic, |
948 |
|
|
def_l0, def_l1, &n); |
949 |
|
|
|
950 |
|
✗ |
if (pic->type == PICTURE_TYPE_P) { |
951 |
|
✗ |
int need_rplm = 0; |
952 |
|
✗ |
for (i = 0; i < pic->nb_refs[0]; i++) { |
953 |
|
✗ |
av_assert0(pic->refs[0][i]); |
954 |
|
✗ |
if (pic->refs[0][i] != def_l0[i]) |
955 |
|
✗ |
need_rplm = 1; |
956 |
|
|
} |
957 |
|
|
|
958 |
|
✗ |
sh->ref_pic_list_modification_flag_l0 = need_rplm; |
959 |
|
✗ |
if (need_rplm) { |
960 |
|
✗ |
int pic_num = hpic->frame_num; |
961 |
|
✗ |
for (i = 0; i < pic->nb_refs[0]; i++) { |
962 |
|
✗ |
href = pic->refs[0][i]->priv_data; |
963 |
|
✗ |
av_assert0(href->frame_num != pic_num); |
964 |
|
✗ |
if (href->frame_num < pic_num) { |
965 |
|
✗ |
sh->rplm_l0[i].modification_of_pic_nums_idc = 0; |
966 |
|
✗ |
sh->rplm_l0[i].abs_diff_pic_num_minus1 = |
967 |
|
✗ |
pic_num - href->frame_num - 1; |
968 |
|
|
} else { |
969 |
|
✗ |
sh->rplm_l0[i].modification_of_pic_nums_idc = 1; |
970 |
|
✗ |
sh->rplm_l0[i].abs_diff_pic_num_minus1 = |
971 |
|
✗ |
href->frame_num - pic_num - 1; |
972 |
|
|
} |
973 |
|
✗ |
pic_num = href->frame_num; |
974 |
|
|
} |
975 |
|
✗ |
sh->rplm_l0[i].modification_of_pic_nums_idc = 3; |
976 |
|
|
} |
977 |
|
|
|
978 |
|
|
} else { |
979 |
|
✗ |
int need_rplm_l0 = 0, need_rplm_l1 = 0; |
980 |
|
✗ |
int n0 = 0, n1 = 0; |
981 |
|
✗ |
for (i = 0; i < pic->nb_refs[0]; i++) { |
982 |
|
✗ |
av_assert0(pic->refs[0][i]); |
983 |
|
✗ |
href = pic->refs[0][i]->priv_data; |
984 |
|
✗ |
av_assert0(href->pic_order_cnt < hpic->pic_order_cnt); |
985 |
|
✗ |
if (pic->refs[0][i] != def_l0[n0]) |
986 |
|
✗ |
need_rplm_l0 = 1; |
987 |
|
✗ |
++n0; |
988 |
|
|
} |
989 |
|
|
|
990 |
|
✗ |
for (i = 0; i < pic->nb_refs[1]; i++) { |
991 |
|
✗ |
av_assert0(pic->refs[1][i]); |
992 |
|
✗ |
href = pic->refs[1][i]->priv_data; |
993 |
|
✗ |
av_assert0(href->pic_order_cnt > hpic->pic_order_cnt); |
994 |
|
✗ |
if (pic->refs[1][i] != def_l1[n1]) |
995 |
|
✗ |
need_rplm_l1 = 1; |
996 |
|
✗ |
++n1; |
997 |
|
|
} |
998 |
|
|
|
999 |
|
✗ |
sh->ref_pic_list_modification_flag_l0 = need_rplm_l0; |
1000 |
|
✗ |
if (need_rplm_l0) { |
1001 |
|
✗ |
int pic_num = hpic->frame_num; |
1002 |
|
✗ |
for (i = j = 0; i < pic->nb_refs[0]; i++) { |
1003 |
|
✗ |
href = pic->refs[0][i]->priv_data; |
1004 |
|
✗ |
av_assert0(href->frame_num != pic_num); |
1005 |
|
✗ |
if (href->frame_num < pic_num) { |
1006 |
|
✗ |
sh->rplm_l0[j].modification_of_pic_nums_idc = 0; |
1007 |
|
✗ |
sh->rplm_l0[j].abs_diff_pic_num_minus1 = |
1008 |
|
✗ |
pic_num - href->frame_num - 1; |
1009 |
|
|
} else { |
1010 |
|
✗ |
sh->rplm_l0[j].modification_of_pic_nums_idc = 1; |
1011 |
|
✗ |
sh->rplm_l0[j].abs_diff_pic_num_minus1 = |
1012 |
|
✗ |
href->frame_num - pic_num - 1; |
1013 |
|
|
} |
1014 |
|
✗ |
pic_num = href->frame_num; |
1015 |
|
✗ |
++j; |
1016 |
|
|
} |
1017 |
|
✗ |
av_assert0(j == n0); |
1018 |
|
✗ |
sh->rplm_l0[j].modification_of_pic_nums_idc = 3; |
1019 |
|
|
} |
1020 |
|
|
|
1021 |
|
✗ |
sh->ref_pic_list_modification_flag_l1 = need_rplm_l1; |
1022 |
|
✗ |
if (need_rplm_l1) { |
1023 |
|
✗ |
int pic_num = hpic->frame_num; |
1024 |
|
✗ |
for (i = j = 0; i < pic->nb_refs[1]; i++) { |
1025 |
|
✗ |
href = pic->refs[1][i]->priv_data; |
1026 |
|
✗ |
av_assert0(href->frame_num != pic_num); |
1027 |
|
✗ |
if (href->frame_num < pic_num) { |
1028 |
|
✗ |
sh->rplm_l1[j].modification_of_pic_nums_idc = 0; |
1029 |
|
✗ |
sh->rplm_l1[j].abs_diff_pic_num_minus1 = |
1030 |
|
✗ |
pic_num - href->frame_num - 1; |
1031 |
|
|
} else { |
1032 |
|
✗ |
sh->rplm_l1[j].modification_of_pic_nums_idc = 1; |
1033 |
|
✗ |
sh->rplm_l1[j].abs_diff_pic_num_minus1 = |
1034 |
|
✗ |
href->frame_num - pic_num - 1; |
1035 |
|
|
} |
1036 |
|
✗ |
pic_num = href->frame_num; |
1037 |
|
✗ |
++j; |
1038 |
|
|
} |
1039 |
|
✗ |
av_assert0(j == n1); |
1040 |
|
✗ |
sh->rplm_l1[j].modification_of_pic_nums_idc = 3; |
1041 |
|
|
} |
1042 |
|
|
} |
1043 |
|
|
} |
1044 |
|
|
|
1045 |
|
✗ |
vslice->macroblock_address = slice->block_start; |
1046 |
|
✗ |
vslice->num_macroblocks = slice->block_size; |
1047 |
|
|
|
1048 |
|
✗ |
vslice->macroblock_info = VA_INVALID_ID; |
1049 |
|
|
|
1050 |
|
✗ |
vslice->slice_type = sh->slice_type % 5; |
1051 |
|
✗ |
vslice->pic_parameter_set_id = sh->pic_parameter_set_id; |
1052 |
|
✗ |
vslice->idr_pic_id = sh->idr_pic_id; |
1053 |
|
|
|
1054 |
|
✗ |
vslice->pic_order_cnt_lsb = sh->pic_order_cnt_lsb; |
1055 |
|
|
|
1056 |
|
✗ |
vslice->direct_spatial_mv_pred_flag = sh->direct_spatial_mv_pred_flag; |
1057 |
|
|
|
1058 |
|
✗ |
for (i = 0; i < FF_ARRAY_ELEMS(vslice->RefPicList0); i++) { |
1059 |
|
✗ |
vslice->RefPicList0[i].picture_id = VA_INVALID_ID; |
1060 |
|
✗ |
vslice->RefPicList0[i].flags = VA_PICTURE_H264_INVALID; |
1061 |
|
✗ |
vslice->RefPicList1[i].picture_id = VA_INVALID_ID; |
1062 |
|
✗ |
vslice->RefPicList1[i].flags = VA_PICTURE_H264_INVALID; |
1063 |
|
|
} |
1064 |
|
|
|
1065 |
|
✗ |
if (pic->nb_refs[0]) { |
1066 |
|
|
// Backward reference for P- or B-frame. |
1067 |
|
✗ |
av_assert0(pic->type == PICTURE_TYPE_P || |
1068 |
|
|
pic->type == PICTURE_TYPE_B); |
1069 |
|
✗ |
vslice->RefPicList0[0] = vpic->ReferenceFrames[0]; |
1070 |
|
|
} |
1071 |
|
✗ |
if (pic->nb_refs[1]) { |
1072 |
|
|
// Forward reference for B-frame. |
1073 |
|
✗ |
av_assert0(pic->type == PICTURE_TYPE_B); |
1074 |
|
✗ |
vslice->RefPicList1[0] = vpic->ReferenceFrames[1]; |
1075 |
|
|
} |
1076 |
|
|
|
1077 |
|
✗ |
vslice->slice_qp_delta = sh->slice_qp_delta; |
1078 |
|
|
|
1079 |
|
✗ |
return 0; |
1080 |
|
|
} |
1081 |
|
|
|
1082 |
|
✗ |
static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx) |
1083 |
|
|
{ |
1084 |
|
✗ |
VAAPIEncodeContext *ctx = avctx->priv_data; |
1085 |
|
✗ |
VAAPIEncodeH264Context *priv = avctx->priv_data; |
1086 |
|
|
int err; |
1087 |
|
|
|
1088 |
|
✗ |
err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_H264, avctx); |
1089 |
|
✗ |
if (err < 0) |
1090 |
|
✗ |
return err; |
1091 |
|
|
|
1092 |
|
✗ |
priv->mb_width = FFALIGN(avctx->width, 16) / 16; |
1093 |
|
✗ |
priv->mb_height = FFALIGN(avctx->height, 16) / 16; |
1094 |
|
|
|
1095 |
|
✗ |
if (ctx->va_rc_mode == VA_RC_CQP) { |
1096 |
|
✗ |
priv->fixed_qp_p = av_clip(ctx->rc_quality, 1, 51); |
1097 |
|
✗ |
if (avctx->i_quant_factor > 0.0) |
1098 |
|
✗ |
priv->fixed_qp_idr = |
1099 |
|
✗ |
av_clip((avctx->i_quant_factor * priv->fixed_qp_p + |
1100 |
|
✗ |
avctx->i_quant_offset) + 0.5, 1, 51); |
1101 |
|
|
else |
1102 |
|
✗ |
priv->fixed_qp_idr = priv->fixed_qp_p; |
1103 |
|
✗ |
if (avctx->b_quant_factor > 0.0) |
1104 |
|
✗ |
priv->fixed_qp_b = |
1105 |
|
✗ |
av_clip((avctx->b_quant_factor * priv->fixed_qp_p + |
1106 |
|
✗ |
avctx->b_quant_offset) + 0.5, 1, 51); |
1107 |
|
|
else |
1108 |
|
✗ |
priv->fixed_qp_b = priv->fixed_qp_p; |
1109 |
|
|
|
1110 |
|
✗ |
av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = " |
1111 |
|
|
"%d / %d / %d for IDR- / P- / B-frames.\n", |
1112 |
|
|
priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b); |
1113 |
|
|
|
1114 |
|
|
} else { |
1115 |
|
|
// These still need to be set for pic_init_qp/slice_qp_delta. |
1116 |
|
✗ |
priv->fixed_qp_idr = 26; |
1117 |
|
✗ |
priv->fixed_qp_p = 26; |
1118 |
|
✗ |
priv->fixed_qp_b = 26; |
1119 |
|
|
} |
1120 |
|
|
|
1121 |
|
✗ |
if (!ctx->rc_mode->hrd) { |
1122 |
|
|
// Timing SEI requires a mode respecting HRD parameters. |
1123 |
|
✗ |
priv->sei &= ~SEI_TIMING; |
1124 |
|
|
} |
1125 |
|
|
|
1126 |
|
✗ |
if (priv->sei & SEI_IDENTIFIER) { |
1127 |
|
✗ |
const char *lavc = LIBAVCODEC_IDENT; |
1128 |
|
✗ |
const char *vaapi = VA_VERSION_S; |
1129 |
|
|
const char *driver; |
1130 |
|
|
int len; |
1131 |
|
|
|
1132 |
|
✗ |
memcpy(priv->sei_identifier.uuid_iso_iec_11578, |
1133 |
|
|
vaapi_encode_h264_sei_identifier_uuid, |
1134 |
|
|
sizeof(priv->sei_identifier.uuid_iso_iec_11578)); |
1135 |
|
|
|
1136 |
|
✗ |
driver = vaQueryVendorString(ctx->hwctx->display); |
1137 |
|
✗ |
if (!driver) |
1138 |
|
✗ |
driver = "unknown driver"; |
1139 |
|
|
|
1140 |
|
✗ |
len = snprintf(NULL, 0, "%s / VAAPI %s / %s", lavc, vaapi, driver); |
1141 |
|
✗ |
if (len >= 0) { |
1142 |
|
✗ |
priv->sei_identifier_string = av_malloc(len + 1); |
1143 |
|
✗ |
if (!priv->sei_identifier_string) |
1144 |
|
✗ |
return AVERROR(ENOMEM); |
1145 |
|
|
|
1146 |
|
✗ |
snprintf(priv->sei_identifier_string, len + 1, |
1147 |
|
|
"%s / VAAPI %s / %s", lavc, vaapi, driver); |
1148 |
|
|
|
1149 |
|
✗ |
priv->sei_identifier.data = priv->sei_identifier_string; |
1150 |
|
✗ |
priv->sei_identifier.data_length = len + 1; |
1151 |
|
|
} |
1152 |
|
|
} |
1153 |
|
|
|
1154 |
|
✗ |
ctx->roi_quant_range = 51 + 6 * (ctx->profile->depth - 8); |
1155 |
|
|
|
1156 |
|
✗ |
return 0; |
1157 |
|
|
} |
1158 |
|
|
|
1159 |
|
|
static const VAAPIEncodeProfile vaapi_encode_h264_profiles[] = { |
1160 |
|
|
#if VA_CHECK_VERSION(1, 18, 0) |
1161 |
|
|
{ AV_PROFILE_H264_HIGH_10, 10, 3, 1, 1, VAProfileH264High10 }, |
1162 |
|
|
#endif |
1163 |
|
|
{ AV_PROFILE_H264_HIGH, 8, 3, 1, 1, VAProfileH264High }, |
1164 |
|
|
{ AV_PROFILE_H264_MAIN, 8, 3, 1, 1, VAProfileH264Main }, |
1165 |
|
|
{ AV_PROFILE_H264_CONSTRAINED_BASELINE, |
1166 |
|
|
8, 3, 1, 1, VAProfileH264ConstrainedBaseline }, |
1167 |
|
|
{ AV_PROFILE_UNKNOWN } |
1168 |
|
|
}; |
1169 |
|
|
|
1170 |
|
|
static const VAAPIEncodeType vaapi_encode_type_h264 = { |
1171 |
|
|
.profiles = vaapi_encode_h264_profiles, |
1172 |
|
|
|
1173 |
|
|
.flags = FLAG_SLICE_CONTROL | |
1174 |
|
|
FLAG_B_PICTURES | |
1175 |
|
|
FLAG_B_PICTURE_REFERENCES | |
1176 |
|
|
FLAG_NON_IDR_KEY_PICTURES, |
1177 |
|
|
|
1178 |
|
|
.default_quality = 20, |
1179 |
|
|
|
1180 |
|
|
.configure = &vaapi_encode_h264_configure, |
1181 |
|
|
|
1182 |
|
|
.picture_priv_data_size = sizeof(VAAPIEncodeH264Picture), |
1183 |
|
|
|
1184 |
|
|
.sequence_params_size = sizeof(VAEncSequenceParameterBufferH264), |
1185 |
|
|
.init_sequence_params = &vaapi_encode_h264_init_sequence_params, |
1186 |
|
|
|
1187 |
|
|
.picture_params_size = sizeof(VAEncPictureParameterBufferH264), |
1188 |
|
|
.init_picture_params = &vaapi_encode_h264_init_picture_params, |
1189 |
|
|
|
1190 |
|
|
.slice_params_size = sizeof(VAEncSliceParameterBufferH264), |
1191 |
|
|
.init_slice_params = &vaapi_encode_h264_init_slice_params, |
1192 |
|
|
|
1193 |
|
|
.sequence_header_type = VAEncPackedHeaderSequence, |
1194 |
|
|
.write_sequence_header = &vaapi_encode_h264_write_sequence_header, |
1195 |
|
|
|
1196 |
|
|
.slice_header_type = VAEncPackedHeaderH264_Slice, |
1197 |
|
|
.write_slice_header = &vaapi_encode_h264_write_slice_header, |
1198 |
|
|
|
1199 |
|
|
.write_extra_header = &vaapi_encode_h264_write_extra_header, |
1200 |
|
|
}; |
1201 |
|
|
|
1202 |
|
✗ |
static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx) |
1203 |
|
|
{ |
1204 |
|
✗ |
VAAPIEncodeContext *ctx = avctx->priv_data; |
1205 |
|
✗ |
VAAPIEncodeH264Context *priv = avctx->priv_data; |
1206 |
|
|
|
1207 |
|
✗ |
ctx->codec = &vaapi_encode_type_h264; |
1208 |
|
|
|
1209 |
|
✗ |
if (avctx->profile == AV_PROFILE_UNKNOWN) |
1210 |
|
✗ |
avctx->profile = priv->profile; |
1211 |
|
✗ |
if (avctx->level == AV_LEVEL_UNKNOWN) |
1212 |
|
✗ |
avctx->level = priv->level; |
1213 |
|
✗ |
if (avctx->compression_level == FF_COMPRESSION_DEFAULT) |
1214 |
|
✗ |
avctx->compression_level = priv->quality; |
1215 |
|
|
|
1216 |
|
|
// Reject unsupported profiles. |
1217 |
|
✗ |
switch (avctx->profile) { |
1218 |
|
✗ |
case AV_PROFILE_H264_BASELINE: |
1219 |
|
✗ |
av_log(avctx, AV_LOG_WARNING, "H.264 baseline profile is not " |
1220 |
|
|
"supported, using constrained baseline profile instead.\n"); |
1221 |
|
✗ |
avctx->profile = AV_PROFILE_H264_CONSTRAINED_BASELINE; |
1222 |
|
✗ |
break; |
1223 |
|
✗ |
case AV_PROFILE_H264_EXTENDED: |
1224 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "H.264 extended profile " |
1225 |
|
|
"is not supported.\n"); |
1226 |
|
✗ |
return AVERROR_PATCHWELCOME; |
1227 |
|
✗ |
case AV_PROFILE_H264_HIGH_10_INTRA: |
1228 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "H.264 high 10 intra profile " |
1229 |
|
|
"is not supported.\n"); |
1230 |
|
✗ |
return AVERROR_PATCHWELCOME; |
1231 |
|
✗ |
case AV_PROFILE_H264_HIGH_422: |
1232 |
|
|
case AV_PROFILE_H264_HIGH_422_INTRA: |
1233 |
|
|
case AV_PROFILE_H264_HIGH_444: |
1234 |
|
|
case AV_PROFILE_H264_HIGH_444_PREDICTIVE: |
1235 |
|
|
case AV_PROFILE_H264_HIGH_444_INTRA: |
1236 |
|
|
case AV_PROFILE_H264_CAVLC_444: |
1237 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles " |
1238 |
|
|
"are not supported.\n"); |
1239 |
|
✗ |
return AVERROR_PATCHWELCOME; |
1240 |
|
|
} |
1241 |
|
|
|
1242 |
|
✗ |
if (avctx->level != AV_LEVEL_UNKNOWN && avctx->level & ~0xff) { |
1243 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit " |
1244 |
|
|
"in 8-bit unsigned integer.\n", avctx->level); |
1245 |
|
✗ |
return AVERROR(EINVAL); |
1246 |
|
|
} |
1247 |
|
|
|
1248 |
|
✗ |
ctx->desired_packed_headers = |
1249 |
|
|
VA_ENC_PACKED_HEADER_SEQUENCE | // SPS and PPS. |
1250 |
|
|
VA_ENC_PACKED_HEADER_SLICE | // Slice headers. |
1251 |
|
|
VA_ENC_PACKED_HEADER_MISC; // SEI. |
1252 |
|
|
|
1253 |
|
✗ |
ctx->surface_width = FFALIGN(avctx->width, 16); |
1254 |
|
✗ |
ctx->surface_height = FFALIGN(avctx->height, 16); |
1255 |
|
|
|
1256 |
|
✗ |
ctx->slice_block_height = ctx->slice_block_width = 16; |
1257 |
|
|
|
1258 |
|
✗ |
if (priv->qp > 0) |
1259 |
|
✗ |
ctx->explicit_qp = priv->qp; |
1260 |
|
|
|
1261 |
|
✗ |
return ff_vaapi_encode_init(avctx); |
1262 |
|
|
} |
1263 |
|
|
|
1264 |
|
✗ |
static av_cold int vaapi_encode_h264_close(AVCodecContext *avctx) |
1265 |
|
|
{ |
1266 |
|
✗ |
VAAPIEncodeH264Context *priv = avctx->priv_data; |
1267 |
|
|
|
1268 |
|
✗ |
ff_cbs_fragment_free(&priv->current_access_unit); |
1269 |
|
✗ |
ff_cbs_close(&priv->cbc); |
1270 |
|
✗ |
av_freep(&priv->sei_identifier_string); |
1271 |
|
✗ |
av_freep(&priv->sei_a53cc_data); |
1272 |
|
|
|
1273 |
|
✗ |
return ff_vaapi_encode_close(avctx); |
1274 |
|
|
} |
1275 |
|
|
|
1276 |
|
|
#define OFFSET(x) offsetof(VAAPIEncodeH264Context, x) |
1277 |
|
|
#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM) |
1278 |
|
|
static const AVOption vaapi_encode_h264_options[] = { |
1279 |
|
|
VAAPI_ENCODE_COMMON_OPTIONS, |
1280 |
|
|
VAAPI_ENCODE_RC_OPTIONS, |
1281 |
|
|
|
1282 |
|
|
{ "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)", |
1283 |
|
|
OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, FLAGS }, |
1284 |
|
|
{ "quality", "Set encode quality (trades off against speed, higher is faster)", |
1285 |
|
|
OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS }, |
1286 |
|
|
{ "coder", "Entropy coder type", |
1287 |
|
|
OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, .unit = "coder" }, |
1288 |
|
|
{ "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, .unit = "coder" }, |
1289 |
|
|
{ "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, .unit = "coder" }, |
1290 |
|
|
{ "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, .unit = "coder" }, |
1291 |
|
|
{ "ac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, .unit = "coder" }, |
1292 |
|
|
|
1293 |
|
|
{ "aud", "Include AUD", |
1294 |
|
|
OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, |
1295 |
|
|
|
1296 |
|
|
{ "sei", "Set SEI to include", |
1297 |
|
|
OFFSET(sei), AV_OPT_TYPE_FLAGS, |
1298 |
|
|
{ .i64 = SEI_IDENTIFIER | SEI_TIMING | SEI_RECOVERY_POINT | SEI_A53_CC }, |
1299 |
|
|
0, INT_MAX, FLAGS, .unit = "sei" }, |
1300 |
|
|
{ "identifier", "Include encoder version identifier", |
1301 |
|
|
0, AV_OPT_TYPE_CONST, { .i64 = SEI_IDENTIFIER }, |
1302 |
|
|
INT_MIN, INT_MAX, FLAGS, .unit = "sei" }, |
1303 |
|
|
{ "timing", "Include timing parameters (buffering_period and pic_timing)", |
1304 |
|
|
0, AV_OPT_TYPE_CONST, { .i64 = SEI_TIMING }, |
1305 |
|
|
INT_MIN, INT_MAX, FLAGS, .unit = "sei" }, |
1306 |
|
|
{ "recovery_point", "Include recovery points where appropriate", |
1307 |
|
|
0, AV_OPT_TYPE_CONST, { .i64 = SEI_RECOVERY_POINT }, |
1308 |
|
|
INT_MIN, INT_MAX, FLAGS, .unit = "sei" }, |
1309 |
|
|
{ "a53_cc", "Include A/53 caption data", |
1310 |
|
|
0, AV_OPT_TYPE_CONST, { .i64 = SEI_A53_CC }, |
1311 |
|
|
INT_MIN, INT_MAX, FLAGS, .unit = "sei" }, |
1312 |
|
|
|
1313 |
|
|
{ "profile", "Set profile (profile_idc and constraint_set*_flag)", |
1314 |
|
|
OFFSET(profile), AV_OPT_TYPE_INT, |
1315 |
|
|
{ .i64 = AV_PROFILE_UNKNOWN }, AV_PROFILE_UNKNOWN, 0xffff, FLAGS, .unit = "profile" }, |
1316 |
|
|
|
1317 |
|
|
#define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \ |
1318 |
|
|
{ .i64 = value }, 0, 0, FLAGS, .unit = "profile" |
1319 |
|
|
{ PROFILE("constrained_baseline", AV_PROFILE_H264_CONSTRAINED_BASELINE) }, |
1320 |
|
|
{ PROFILE("main", AV_PROFILE_H264_MAIN) }, |
1321 |
|
|
{ PROFILE("high", AV_PROFILE_H264_HIGH) }, |
1322 |
|
|
{ PROFILE("high10", AV_PROFILE_H264_HIGH_10) }, |
1323 |
|
|
#undef PROFILE |
1324 |
|
|
|
1325 |
|
|
{ "level", "Set level (level_idc)", |
1326 |
|
|
OFFSET(level), AV_OPT_TYPE_INT, |
1327 |
|
|
{ .i64 = AV_LEVEL_UNKNOWN }, AV_LEVEL_UNKNOWN, 0xff, FLAGS, .unit = "level" }, |
1328 |
|
|
|
1329 |
|
|
#define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \ |
1330 |
|
|
{ .i64 = value }, 0, 0, FLAGS, .unit = "level" |
1331 |
|
|
{ LEVEL("1", 10) }, |
1332 |
|
|
{ LEVEL("1.1", 11) }, |
1333 |
|
|
{ LEVEL("1.2", 12) }, |
1334 |
|
|
{ LEVEL("1.3", 13) }, |
1335 |
|
|
{ LEVEL("2", 20) }, |
1336 |
|
|
{ LEVEL("2.1", 21) }, |
1337 |
|
|
{ LEVEL("2.2", 22) }, |
1338 |
|
|
{ LEVEL("3", 30) }, |
1339 |
|
|
{ LEVEL("3.1", 31) }, |
1340 |
|
|
{ LEVEL("3.2", 32) }, |
1341 |
|
|
{ LEVEL("4", 40) }, |
1342 |
|
|
{ LEVEL("4.1", 41) }, |
1343 |
|
|
{ LEVEL("4.2", 42) }, |
1344 |
|
|
{ LEVEL("5", 50) }, |
1345 |
|
|
{ LEVEL("5.1", 51) }, |
1346 |
|
|
{ LEVEL("5.2", 52) }, |
1347 |
|
|
{ LEVEL("6", 60) }, |
1348 |
|
|
{ LEVEL("6.1", 61) }, |
1349 |
|
|
{ LEVEL("6.2", 62) }, |
1350 |
|
|
#undef LEVEL |
1351 |
|
|
|
1352 |
|
|
{ NULL }, |
1353 |
|
|
}; |
1354 |
|
|
|
1355 |
|
|
static const FFCodecDefault vaapi_encode_h264_defaults[] = { |
1356 |
|
|
{ "b", "0" }, |
1357 |
|
|
{ "bf", "2" }, |
1358 |
|
|
{ "g", "120" }, |
1359 |
|
|
{ "i_qfactor", "1" }, |
1360 |
|
|
{ "i_qoffset", "0" }, |
1361 |
|
|
{ "b_qfactor", "6/5" }, |
1362 |
|
|
{ "b_qoffset", "0" }, |
1363 |
|
|
{ "qmin", "-1" }, |
1364 |
|
|
{ "qmax", "-1" }, |
1365 |
|
|
{ NULL }, |
1366 |
|
|
}; |
1367 |
|
|
|
1368 |
|
|
static const AVClass vaapi_encode_h264_class = { |
1369 |
|
|
.class_name = "h264_vaapi", |
1370 |
|
|
.item_name = av_default_item_name, |
1371 |
|
|
.option = vaapi_encode_h264_options, |
1372 |
|
|
.version = LIBAVUTIL_VERSION_INT, |
1373 |
|
|
}; |
1374 |
|
|
|
1375 |
|
|
const FFCodec ff_h264_vaapi_encoder = { |
1376 |
|
|
.p.name = "h264_vaapi", |
1377 |
|
|
CODEC_LONG_NAME("H.264/AVC (VAAPI)"), |
1378 |
|
|
.p.type = AVMEDIA_TYPE_VIDEO, |
1379 |
|
|
.p.id = AV_CODEC_ID_H264, |
1380 |
|
|
.priv_data_size = sizeof(VAAPIEncodeH264Context), |
1381 |
|
|
.init = &vaapi_encode_h264_init, |
1382 |
|
|
FF_CODEC_RECEIVE_PACKET_CB(&ff_vaapi_encode_receive_packet), |
1383 |
|
|
.close = &vaapi_encode_h264_close, |
1384 |
|
|
.p.priv_class = &vaapi_encode_h264_class, |
1385 |
|
|
.p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE | |
1386 |
|
|
AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, |
1387 |
|
|
.caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE | |
1388 |
|
|
FF_CODEC_CAP_INIT_CLEANUP, |
1389 |
|
|
.defaults = vaapi_encode_h264_defaults, |
1390 |
|
|
.p.pix_fmts = (const enum AVPixelFormat[]) { |
1391 |
|
|
AV_PIX_FMT_VAAPI, |
1392 |
|
|
AV_PIX_FMT_NONE, |
1393 |
|
|
}, |
1394 |
|
|
.hw_configs = ff_vaapi_encode_hw_configs, |
1395 |
|
|
.p.wrapper_name = "vaapi", |
1396 |
|
|
}; |
1397 |
|
|
|