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
27
33
37
41
43
47
48
49 #define vseq_var(name) vseq->name, name
50 #define vseqext_field(name) vseq->sequence_extension.bits.name, name
51 #define vgop_field(name) vseq->gop_header.bits.name, name
52 #define vpic_var(name) vpic->name, name
53 #define vpcext_field(name) vpic->picture_coding_extension.bits.name, name
54 #define vcomp_field(name) vpic->composite_display.bits.name, name
55
56 #define u2(width, value, name) put_bits(&pbc, width, value)
57 #define u(width, ...) u2(width, __VA_ARGS__)
58
60 char *
data,
size_t *data_len)
61 {
66
68
70
71 u(12, vseq->picture_width, horizontal_size_value);
72 u(12, vseq->picture_height, vertical_size_value);
74 u(4, 8, frame_rate_code);
75 u(18, priv->
bit_rate & 0x3fff, bit_rate_value);
78 u(1, 0, constrained_parameters_flag);
79 u(1, 0, load_intra_quantiser_matrix);
80 // intra_quantiser_matrix[64]
81 u(1, 0, load_non_intra_quantiser_matrix);
82 // non_intra_quantiser_matrix[64]
83
86
88 u(4, 1, extension_start_code_identifier);
92 u(2, 0, horizontal_size_extension);
93 u(2, 0, vertical_size_extension);
94 u(12, priv->
bit_rate >> 18, bit_rate_extension);
100
103
108
111
114
115 return 0;
116 }
117
120 char *
data,
size_t *data_len)
121 {
123 int picture_coding_type;
125
127
130
131 switch (vpic->picture_type) {
132 case VAEncPictureTypeIntra:
134 break;
135 case VAEncPictureTypePredictive:
137 break;
138 case VAEncPictureTypeBidirectional:
140 break;
141 default:
142 av_assert0(0 &&
"invalid picture_coding_type");
143 }
144 u(3, picture_coding_type, picture_coding_type);
145 u(16, 0xffff, vbv_delay);
146 if (picture_coding_type == 2 || picture_coding_type == 3) {
147 u(1, 0, full_pel_forward_vector);
148 u(3, 7, forward_f_code);
149 }
150 if (picture_coding_type == 3) {
151 u(1, 0, full_pel_backward_vector);
152 u(3, 7, backward_f_code);
153 }
154 u(1, 0, extra_bit_picture);
155
158
160 u(4, 8, extension_start_code_identifier);
174 u(1, 1, chroma_420_type);
177 if (vpic->picture_coding_extension.bits.composite_display_flag) {
183 }
184
187
190
191 return 0;
192 }
193
195 {
200
201 vseq->intra_period = avctx->
gop_size;
202 vseq->ip_period = ctx->
b_per_p + 1;
203
204 vseq->picture_width = avctx->
width;
205 vseq->picture_height = avctx->
height;
206
207 vseq->bits_per_second = avctx->
bit_rate;
210 else
212
213 vseq->aspect_ratio_information = 1;
215
216 vseq->sequence_extension.bits.profile_and_level_indication =
218 vseq->sequence_extension.bits.progressive_sequence = 1;
219 vseq->sequence_extension.bits.chroma_format = 1;
220 vseq->sequence_extension.bits.low_delay = 0;
221 vseq->sequence_extension.bits.frame_rate_extension_n = 0;
222 vseq->sequence_extension.bits.frame_rate_extension_d = 0;
223
224 vseq->new_gop_header = 0;
225 vseq->gop_header.bits.time_code = 0;
226 vseq->gop_header.bits.closed_gop = 1;
227 vseq->gop_header.bits.broken_link = 0;
228
229 vpic->forward_reference_picture = VA_INVALID_ID;
230 vpic->backward_reference_picture = VA_INVALID_ID;
231 vpic->reconstructed_picture = VA_INVALID_ID;
232
233 vpic->coded_buf = VA_INVALID_ID;
234
235 vpic->temporal_reference = 0;
236 vpic->f_code[0][0] = 15;
237 vpic->f_code[0][1] = 15;
238 vpic->f_code[1][0] = 15;
239 vpic->f_code[1][1] = 15;
240
241 vpic->picture_coding_extension.bits.intra_dc_precision = 0;
242 vpic->picture_coding_extension.bits.picture_structure = 3;
243 vpic->picture_coding_extension.bits.top_field_first = 0;
244 vpic->picture_coding_extension.bits.frame_pred_frame_dct = 1;
245 vpic->picture_coding_extension.bits.concealment_motion_vectors = 0;
246 vpic->picture_coding_extension.bits.q_scale_type = 0;
247 vpic->picture_coding_extension.bits.intra_vlc_format = 0;
248 vpic->picture_coding_extension.bits.alternate_scan = 0;
249 vpic->picture_coding_extension.bits.repeat_first_field = 0;
250 vpic->picture_coding_extension.bits.progressive_frame = 1;
251 vpic->picture_coding_extension.bits.composite_display_flag = 0;
252
255
256 return 0;
257 }
258
261 {
265 int fch, fcv;
266
267 switch (avctx->
level) {
268 case 4: // High.
269 case 6: // High 1440.
270 fch = 9;
271 fcv = 5;
272 break;
273 case 8: // Main.
274 fch = 8;
275 fcv = 5;
276 break;
277 case 10: // Low.
278 default:
279 fch = 7;
280 fcv = 4;
281 break;
282 }
283
287 vpic->picture_type = VAEncPictureTypeIntra;
289 break;
291 vpic->picture_type = VAEncPictureTypePredictive;
293 vpic->f_code[0][0] = fch;
294 vpic->f_code[0][1] = fcv;
295 break;
297 vpic->picture_type = VAEncPictureTypeBidirectional;
300 vpic->f_code[0][0] = fch;
301 vpic->f_code[0][1] = fcv;
302 vpic->f_code[1][0] = fch;
303 vpic->f_code[1][1] = fcv;
304 break;
305 default:
307 }
308
311
313
315
316 return 0;
317 }
318
322 {
326 int qp;
327
329 vslice->num_macroblocks = priv->
mb_width;
330
335 break;
338 break;
341 break;
342 default:
344 }
345
346 vslice->quantiser_scale_code = qp;
349
350 return 0;
351 }
352
354 {
357
360
367 1, 31);
368 else
374 1, 31);
375 else
377
379 "%d / %d / %d for I- / P- / B-frames.\n",
381
382 } else {
384 }
385
386 return 0;
387 }
388
391
393
394 .sequence_params_size = sizeof(VAEncSequenceParameterBufferMPEG2),
396
397 .picture_params_size = sizeof(VAEncPictureParameterBufferMPEG2),
399
400 .slice_params_size = sizeof(VAEncSliceParameterBufferMPEG2),
402
403 .sequence_header_type = VAEncPackedHeaderSequence,
405
406 .picture_header_type = VAEncPackedHeaderPicture,
408 };
409
411 {
413
415
419 break;
422 break;
423 default:
427 }
428
432
434 VA_ENC_PACKED_HEADER_PICTURE;
435
438
440 }
441
443 { "profile", "4" },
444 { "level", "4" },
445 { "bf", "1" },
446 { "g", "120" },
447 { "i_qfactor", "1" },
448 { "i_qoffset", "0" },
449 { "b_qfactor", "6/5" },
450 { "b_qoffset", "0" },
451 { "global_quality", "10" },
453 };
454
456 .
name =
"mpeg2_vaapi",
465 .
defaults = vaapi_encode_mpeg2_defaults,
469 },
470 };
static av_cold int vaapi_encode_mpeg2_configure(AVCodecContext *avctx)
#define FF_PROFILE_MPEG2_MAIN
#define vcomp_field(name)
VAEntrypoint va_entrypoint
ptrdiff_t const GLvoid * data
int64_t bit_rate
the average bitrate
av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
static av_cold int init(AVCodecContext *avctx)
av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
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
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
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)
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...
VASurfaceID recon_surface
AVCodec ff_mpeg2_vaapi_encoder
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define vpcext_field(name)
static int vaapi_encode_mpeg2_init_picture_params(AVCodecContext *avctx, VAAPIEncodePicture *pic)
int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *input_image, int *got_packet)
#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[]
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_...
void * codec_picture_params
static int put_bits_count(PutBitContext *s)
int rc_buffer_size
decoder bitstream buffer size
common internal API header
static int vaapi_encode_mpeg2_init_sequence_params(AVCodecContext *avctx)
int width
picture width / height.
unsigned int va_rt_format
void * codec_picture_params
#define vseqext_field(name)
preferred ID for MPEG-1/2 video decoding
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.
main external API structure.
static const VAAPIEncodeType vaapi_encode_type_mpeg2
float b_quant_offset
qscale offset between IP and B-frames
static enum AVPixelFormat pix_fmts[]
int global_quality
Global quality for codecs which cannot change it per frame.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
common internal and external API header
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
#define FF_PROFILE_MPEG2_SIMPLE
void * codec_slice_params
#define PICTURE_START_CODE
static const AVCodecDefault defaults[]
static int vaapi_encode_mpeg2_write_sequence_header(AVCodecContext *avctx, char *data, size_t *data_len)
AVPixelFormat
Pixel format.