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 <va/va.h>
20 #include <va/va_enc_mpeg2.h>
21
23
29
33
37
44
46
49
51
54
58
59
61 char *
data,
size_t *data_len,
63 {
66 int err;
67
69 if (err < 0) {
71 return err;
72 }
73
76 "%zu < %zu.\n", *data_len,
79 }
80
83
84 return 0;
85 }
86
90 {
93 int err;
94
96 if (err < 0) {
98 "type = %d.\n", type);
99 return err;
100 }
101
102 return 0;
103 }
104
106 char *
data,
size_t *data_len)
107 {
111 int err;
112
115 if (err < 0)
117
120 if (err < 0)
122
125 if (err < 0)
127
130 if (err < 0)
132
136 return 0;
137 }
138
141 char *
data,
size_t *data_len)
142 {
146 int err;
147
150 if (err < 0)
152
155 if (err < 0)
157
161 return 0;
162 }
163
165 {
176 int code, ext_n, ext_d;
177
178 memset(sh, 0, sizeof(*sh));
179 memset(se, 0, sizeof(*se));
180 memset(sde, 0, sizeof(*sde));
181 memset(goph, 0, sizeof(*goph));
182 memset(ph, 0, sizeof(*ph));
183 memset(pce, 0, sizeof(*pce));
184
185
188 } else {
189 // Unknown (not a bitrate-targetting mode), so just use the
190 // highest value.
192 }
195 } else {
196 // Unknown, so guess a value from the bitrate.
198 }
199
200 switch (avctx->
level) {
201 case 4: // High.
202 case 6: // High 1440.
205 break;
206 case 8: // Main.
209 break;
210 case 10: // Low.
211 default:
214 break;
215 }
216
217
218 // Sequence header
219
221
224
229
231 sh->aspect_ratio_information = 1;
233 sh->aspect_ratio_information = 2;
235 sh->aspect_ratio_information = 3;
237 sh->aspect_ratio_information = 4;
238 } else {
240 "representable, signalling square pixels instead.\n",
241 avctx->sample_aspect_ratio.num,
242 avctx->sample_aspect_ratio.den);
243 sh->aspect_ratio_information = 1;
244 }
245 } else {
246 // Unknown - assume square pixels.
247 sh->aspect_ratio_information = 1;
248 }
249
250 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
251 priv->frame_rate = avctx->framerate;
252 else
253 priv->frame_rate =
av_inv_q(avctx->time_base);
255 &code, &ext_n, &ext_d, 0);
256 sh->frame_rate_code = code;
257
258 sh->bit_rate_value = priv->bit_rate & 0x3ffff;
259 sh->vbv_buffer_size_value = priv->vbv_buffer_size & 0x3ff;
260
261 sh->constrained_parameters_flag = 0;
262 sh->load_intra_quantiser_matrix = 0;
263 sh->load_non_intra_quantiser_matrix = 0;
264
265
266 // Sequence extension
267
269 priv->sequence_extension.extension_start_code_identifier =
271
272 se->profile_and_level_indication = avctx->profile << 4 | avctx->level;
273 se->progressive_sequence = 1;
274 se->chroma_format = 1;
275
276 se->horizontal_size_extension = avctx->width >> 12;
277 se->vertical_size_extension = avctx->height >> 12;
278
279 se->bit_rate_extension = priv->bit_rate >> 18;
280 se->vbv_buffer_size_extension = priv->vbv_buffer_size >> 10;
281 se->low_delay =
ctx->b_per_p == 0;
282
283 se->frame_rate_extension_n = ext_n;
284 se->frame_rate_extension_d = ext_d;
285
286
287 // Sequence display extension
288
289 priv->sequence_display_extension.extension_start_code =
291 priv->sequence_display_extension.extension_start_code_identifier =
293
294 sde->video_format = 5;
298 sde->colour_description = 1;
299 sde->colour_primaries = avctx->color_primaries;
300 sde->transfer_characteristics = avctx->color_trc;
301 sde->matrix_coefficients = avctx->colorspace;
302 } else {
303 sde->colour_description = 0;
304 }
305
306 sde->display_horizontal_size = avctx->width;
307 sde->display_vertical_size = avctx->height;
308
309
310 // GOP header
311
313
314 goph->time_code = 0;
315 goph->closed_gop = 1;
316 goph->broken_link = 0;
317
318
319 // Defaults for picture header
320
322
323 ph->vbv_delay = 0xffff; // Not currently calculated.
324
325 ph->full_pel_forward_vector = 0;
326 ph->forward_f_code = 7;
327 ph->full_pel_backward_vector = 0;
328 ph->forward_f_code = 7;
329
330
331 // Defaults for picture coding extension
332
333 priv->picture_coding_extension.extension_start_code =
335 priv->picture_coding_extension.extension_start_code_identifier =
337
338 pce->intra_dc_precision = 0;
339 pce->picture_structure = 3;
340 pce->top_field_first = 0;
341 pce->frame_pred_frame_dct = 1;
342 pce->concealment_motion_vectors = 0;
343 pce->q_scale_type = 0;
344 pce->intra_vlc_format = 0;
345 pce->alternate_scan = 0;
346 pce->repeat_first_field = 0;
347 pce->progressive_frame = 1;
348 pce->composite_display_flag = 0;
349
350
351
352 *vseq = (VAEncSequenceParameterBufferMPEG2) {
353 .intra_period = avctx->gop_size,
354 .ip_period =
ctx->b_per_p + 1,
355
356 .picture_width = avctx->width,
357 .picture_height = avctx->height,
358
360 .frame_rate =
av_q2d(priv->frame_rate),
361 .aspect_ratio_information = sh->aspect_ratio_information,
362 .vbv_buffer_size = priv->vbv_buffer_size,
363
364 .sequence_extension.bits = {
365 .profile_and_level_indication =
se->profile_and_level_indication,
366 .progressive_sequence =
se->progressive_sequence,
367 .chroma_format =
se->chroma_format,
368 .low_delay =
se->low_delay,
369 .frame_rate_extension_n =
se->frame_rate_extension_n,
370 .frame_rate_extension_d =
se->frame_rate_extension_d,
371 },
372
373 .new_gop_header = 1,
374 .gop_header.bits = {
375 .time_code = goph->time_code,
376 .closed_gop = goph->closed_gop,
377 .broken_link = goph->broken_link,
378 },
379 };
380
381 *vpic = (VAEncPictureParameterBufferMPEG2) {
382 .forward_reference_picture = VA_INVALID_ID,
383 .backward_reference_picture = VA_INVALID_ID,
384 .reconstructed_picture = VA_INVALID_ID,
385 .coded_buf = VA_INVALID_ID,
386
387 .vbv_delay = 0xffff,
388 .f_code = { { 15, 15 }, { 15, 15 } },
389
390 .picture_coding_extension.bits = {
391 .intra_dc_precision = pce->intra_dc_precision,
392 .picture_structure = pce->picture_structure,
393 .top_field_first = pce->top_field_first,
394 .frame_pred_frame_dct = pce->frame_pred_frame_dct,
395 .concealment_motion_vectors = pce->concealment_motion_vectors,
396 .q_scale_type = pce->q_scale_type,
397 .intra_vlc_format = pce->intra_vlc_format,
398 .alternate_scan = pce->alternate_scan,
399 .repeat_first_field = pce->repeat_first_field,
400 .progressive_frame = pce->progressive_frame,
401 .composite_display_flag = pce->composite_display_flag,
402 },
403
404 .composite_display.bits = {
405 .v_axis = pce->v_axis,
406 .field_sequence = pce->field_sequence,
407 .sub_carrier = pce->sub_carrier,
408 .burst_amplitude = pce->burst_amplitude,
409 .sub_carrier_phase = pce->sub_carrier_phase,
410 },
411 };
412
413 return 0;
414 }
415
418 {
424
429 } else {
432 }
433
437 } else {
440 }
444 } else {
447 }
448
451
455 vpic->picture_type = VAEncPictureTypeIntra;
456 break;
458 vpic->picture_type = VAEncPictureTypePredictive;
460 break;
462 vpic->picture_type = VAEncPictureTypeBidirectional;
465 break;
466 default:
468 }
469
471 vpic->f_code[0][0] = pce->
f_code[0][0];
472 vpic->f_code[0][1] = pce->
f_code[0][1];
473 vpic->f_code[1][0] = pce->
f_code[1][0];
474 vpic->f_code[1][1] = pce->
f_code[1][1];
475
477
478 return 0;
479 }
480
484 {
488 int qp;
489
491 vslice->num_macroblocks = priv->
mb_width;
492
497 break;
500 break;
503 break;
504 default:
506 }
507
508 vslice->quantiser_scale_code = qp;
511
512 return 0;
513 }
514
516 {
519 int err;
520
522 if (err < 0)
523 return err;
524
527
534 1, 31);
535 else
541 1, 31);
542 else
544
546 "%d / %d / %d for I- / P- / B-frames.\n",
548
549 } else {
551 }
552
553 return 0;
554 }
555
558
560
561 .sequence_params_size = sizeof(VAEncSequenceParameterBufferMPEG2),
563
564 .picture_params_size = sizeof(VAEncPictureParameterBufferMPEG2),
566
567 .slice_params_size = sizeof(VAEncSliceParameterBufferMPEG2),
569
570 .sequence_header_type = VAEncPackedHeaderSequence,
572
573 .picture_header_type = VAEncPackedHeaderPicture,
575 };
576
578 {
580
582
586 break;
589 break;
592 "is not supported.\n");
596 "is not supported.\n");
601 "are not supported.\n");
603 default:
607 }
608 switch (avctx->
level) {
609 case 4: // High
610 case 6: // High 1440
611 case 8: // Main
612 case 10: // Low
613 break;
614 default:
618 }
619
620 if (avctx->
height % 4096 == 0 || avctx->
width % 4096 == 0) {
622 "height or width divisible by 4096.\n");
624 }
625
629
631 VA_ENC_PACKED_HEADER_PICTURE;
632
635
637 }
638
640 {
643
644 if (priv)
646
648 }
649
651 { "profile", "4" },
652 { "level", "4" },
653 { "bf", "1" },
654 { "g", "120" },
655 { "i_qfactor", "1" },
656 { "i_qoffset", "0" },
657 { "b_qfactor", "6/5" },
658 { "b_qoffset", "0" },
659 { "global_quality", "10" },
661 };
662
664 .
name =
"mpeg2_vaapi",
673 .
defaults = vaapi_encode_mpeg2_defaults,
677 },
678 .wrapper_name = "vaapi",
679 };
static av_cold int vaapi_encode_mpeg2_configure(AVCodecContext *avctx)
MPEG2RawExtensionData sequence_extension
#define FF_PROFILE_MPEG2_MAIN
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
#define se(name, range_min, range_max)
VAEntrypoint va_entrypoint
ptrdiff_t const GLvoid * data
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t bit_rate
the average bitrate
static av_cold int init(AVCodecContext *avctx)
MPEG2RawSequenceExtension sequence
#define AV_CODEC_CAP_HARDWARE
Codec is backed by a hardware implementation.
int ff_cbs_init(CodedBitstreamContext **ctx_ptr, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, AVBufferRef *content_buf)
Insert a new unit into a fragment with the given content.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
int64_t bit_rate
Total stream bitrate in bit/s, 0 if not available.
MPEG2RawExtensionData sequence_display_extension
union MPEG2RawExtensionData::@48 data
void * codec_sequence_params
static av_cold int vaapi_encode_mpeg2_init(AVCodecContext *avctx)
unsigned int vbv_buffer_size
float i_quant_offset
qscale offset between P and I-frames
unsigned int va_packed_headers
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static int vaapi_encode_mpeg2_write_picture_header(AVCodecContext *avctx, VAAPIEncodePicture *pic, char *data, size_t *data_len)
static int vaapi_encode_mpeg2_add_header(AVCodecContext *avctx, CodedBitstreamFragment *frag, int type, void *header)
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
static double av_q2d(AVRational a)
Convert an AVRational to a double.
VASurfaceID recon_surface
static const uint8_t header[24]
MPEG2RawSequenceHeader sequence_header
static int vaapi_encode_mpeg2_write_fragment(AVCodecContext *avctx, char *data, size_t *data_len, CodedBitstreamFragment *frag)
MPEG2RawPictureCodingExtension picture_coding
size_t data_size
The number of bytes in the bitstream.
AVCodec ff_mpeg2_vaapi_encoder
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free all allocated memory in a fragment.
static int vaapi_encode_mpeg2_init_picture_params(AVCodecContext *avctx, VAAPIEncodePicture *pic)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static const AVCodecDefault vaapi_encode_mpeg2_defaults[]
MPEG2RawPictureHeader picture_header
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
float i_quant_factor
qscale factor between P- and I-frames If > 0 then the last P-frame quantizer will be used (q = lastp_...
static const AVCodecDefault defaults[]
void ff_mpeg12_find_best_frame_rate(AVRational frame_rate, int *code, int *ext_n, int *ext_d, int nonstandard)
void * codec_picture_params
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
#define FF_PROFILE_MPEG2_SNR_SCALABLE
int rc_buffer_size
decoder bitstream buffer size
CodedBitstreamContext * cbc
#define FF_PROFILE_MPEG2_HIGH
static av_cold int vaapi_encode_mpeg2_close(AVCodecContext *avctx)
static int vaapi_encode_mpeg2_init_sequence_params(AVCodecContext *avctx)
int width
picture width / height.
unsigned int f_code_vertical
av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
#define FF_PROFILE_MPEG2_422
unsigned int va_rt_format
void * codec_picture_params
preferred ID for MPEG-1/2 video decoding
uint8_t * data
Pointer to the bitstream form of this fragment.
int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
struct VAAPIEncodePicture * refs[MAX_PICTURE_REFERENCES]
static int vaapi_encode_mpeg2_init_slice_params(AVCodecContext *avctx, VAAPIEncodePicture *pic, VAAPIEncodeSlice *slice)
const struct VAAPIEncodeType * codec
Libavcodec external API header.
MPEG2RawSequenceDisplayExtension sequence_display
Coded bitstream fragment structure, combining one or more units.
main external API structure.
CodedBitstreamFragment current_fragment
Context structure for coded bitstream operations.
Rational number (pair of numerator and denominator).
MPEG2RawExtensionData picture_coding_extension
static const VAAPIEncodeType vaapi_encode_type_mpeg2
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
float b_quant_offset
qscale offset between IP and B-frames
unsigned int f_code_horizontal
#define FF_PROFILE_MPEG2_SS
static enum AVPixelFormat pix_fmts[]
int global_quality
Global quality for codecs which cannot change it per frame.
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *input_image, int *got_packet)
#define FF_PROFILE_MPEG2_SIMPLE
void * codec_slice_params
MPEG2RawGroupOfPicturesHeader gop_header
static int vaapi_encode_mpeg2_write_sequence_header(AVCodecContext *avctx, char *data, size_t *data_len)
AVPixelFormat
Pixel format.
av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)