2 * gstvaapidecoder_mpeg2.c - MPEG-2 decoder
4 * Copyright (C) 2011 Intel Corporation
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1
9 * of the License, or (at your option) any later version.
11 * This library 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
23 * SECTION:gstvaapidecoder_mpeg2
24 * @short_description: MPEG-2 decoder
29 #include <gst/base/gstbitreader.h>
30 #include <gst/codecparsers/gstmpegvideoparser.h>
31 #include "gstvaapidecoder_mpeg2.h"
32 #include "gstvaapidecoder_objects.h"
33 #include "gstvaapidecoder_priv.h"
34 #include "gstvaapidisplay_priv.h"
35 #include "gstvaapiobject_priv.h"
38 #include "gstvaapidebug.h"
40 G_DEFINE_TYPE(GstVaapiDecoderMpeg2,
41 gst_vaapi_decoder_mpeg2,
42 GST_VAAPI_TYPE_DECODER);
44 #define GST_VAAPI_DECODER_MPEG2_GET_PRIVATE(obj) \
45 (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
46 GST_VAAPI_TYPE_DECODER_MPEG2, \
47 GstVaapiDecoderMpeg2Private))
49 #define READ_UINT8(br, val, nbits) G_STMT_START { \
50 if (!gst_bit_reader_get_bits_uint8 (br, &val, nbits)) { \
51 GST_WARNING ("failed to read uint8, nbits: %d", nbits); \
56 #define SKIP(reader, nbits) G_STMT_START { \
57 if (!gst_bit_reader_skip (reader, nbits)) { \
58 GST_WARNING ("failed to skip nbits: %d", nbits); \
63 struct _GstVaapiDecoderMpeg2Private {
64 GstVaapiProfile profile;
69 GstMpegVideoSequenceHdr seq_hdr;
70 GstMpegVideoSequenceExt seq_ext;
71 GstMpegVideoPictureHdr pic_hdr;
72 GstMpegVideoPictureExt pic_ext;
73 GstMpegVideoQuantMatrixExt quant_matrix_ext;
74 GstVaapiPicture *current_picture;
75 GstVaapiPicture *next_picture;
76 GstVaapiPicture *prev_picture;
80 GstClockTime pts_diff;
81 guint is_constructed : 1;
83 guint is_first_field : 1;
84 guint has_seq_ext : 1;
85 guint has_seq_scalable_ext : 1;
86 guint has_pic_ext : 1;
87 guint has_quant_matrix_ext : 1;
88 guint size_changed : 1;
89 guint profile_changed : 1;
90 guint quant_matrix_changed : 1;
91 guint progressive_sequence : 1;
93 guint broken_link : 1;
96 /* VLC decoder from gst-plugins-bad */
97 typedef struct _VLCTable VLCTable;
105 decode_vlc(GstBitReader *br, gint *res, const VLCTable *table, guint length)
111 for (i = 0; i < length; i++) {
112 if (cbits != table[i].cbits) {
113 cbits = table[i].cbits;
114 if (!gst_bit_reader_peek_bits_uint32(br, &value, cbits)) {
119 if (value == table[i].cword) {
122 *res = table[i].value;
126 GST_DEBUG("failed to find VLC code");
129 GST_WARNING("failed to decode VLC, returning");
134 GST_MPEG_VIDEO_MACROBLOCK_ESCAPE = -1,
137 /* Table B-1: Variable length codes for macroblock_address_increment */
138 static const VLCTable mpeg2_mbaddr_vlc_table[] = {
172 { GST_MPEG_VIDEO_MACROBLOCK_ESCAPE, 0x08, 11 }
176 gst_vaapi_decoder_mpeg2_close(GstVaapiDecoderMpeg2 *decoder)
178 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
180 gst_vaapi_picture_replace(&priv->current_picture, NULL);
181 gst_vaapi_picture_replace(&priv->next_picture, NULL);
182 gst_vaapi_picture_replace(&priv->prev_picture, NULL);
185 gst_adapter_clear(priv->adapter);
186 g_object_unref(priv->adapter);
187 priv->adapter = NULL;
192 gst_vaapi_decoder_mpeg2_open(GstVaapiDecoderMpeg2 *decoder, GstBuffer *buffer)
194 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
196 gst_vaapi_decoder_mpeg2_close(decoder);
198 priv->adapter = gst_adapter_new();
205 gst_vaapi_decoder_mpeg2_destroy(GstVaapiDecoderMpeg2 *decoder)
207 gst_vaapi_decoder_mpeg2_close(decoder);
211 gst_vaapi_decoder_mpeg2_create(GstVaapiDecoderMpeg2 *decoder)
213 if (!GST_VAAPI_DECODER_CODEC(decoder))
219 copy_quant_matrix(guint8 dst[64], const guint8 src[64])
221 memcpy(dst, src, 64);
224 static GstVaapiDecoderStatus
225 ensure_context(GstVaapiDecoderMpeg2 *decoder)
227 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
228 GstVaapiProfile profiles[2];
229 GstVaapiEntrypoint entrypoint = GST_VAAPI_ENTRYPOINT_VLD;
230 guint i, n_profiles = 0;
231 gboolean reset_context = FALSE;
233 if (priv->profile_changed) {
234 GST_DEBUG("profile changed");
235 priv->profile_changed = FALSE;
236 reset_context = TRUE;
238 profiles[n_profiles++] = priv->profile;
239 if (priv->profile == GST_VAAPI_PROFILE_MPEG2_SIMPLE)
240 profiles[n_profiles++] = GST_VAAPI_PROFILE_MPEG2_MAIN;
242 for (i = 0; i < n_profiles; i++) {
243 if (gst_vaapi_display_has_decoder(GST_VAAPI_DECODER_DISPLAY(decoder),
244 profiles[i], entrypoint))
248 return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
249 priv->profile = profiles[i];
252 if (priv->size_changed) {
253 GST_DEBUG("size changed");
254 priv->size_changed = FALSE;
255 reset_context = TRUE;
259 reset_context = gst_vaapi_decoder_ensure_context(
260 GST_VAAPI_DECODER(decoder),
263 priv->width, priv->height
266 return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
268 return GST_VAAPI_DECODER_STATUS_SUCCESS;
271 static GstVaapiDecoderStatus
272 ensure_quant_matrix(GstVaapiDecoderMpeg2 *decoder, GstVaapiPicture *picture)
274 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
275 VAIQMatrixBufferMPEG2 *iq_matrix;
276 guint8 *intra_quant_matrix = NULL;
277 guint8 *non_intra_quant_matrix = NULL;
278 guint8 *chroma_intra_quant_matrix = NULL;
279 guint8 *chroma_non_intra_quant_matrix = NULL;
281 if (!priv->quant_matrix_changed)
282 return GST_VAAPI_DECODER_STATUS_SUCCESS;
284 priv->quant_matrix_changed = FALSE;
286 picture->iq_matrix = GST_VAAPI_IQ_MATRIX_NEW(MPEG2, decoder);
287 if (!picture->iq_matrix) {
288 GST_DEBUG("failed to allocate IQ matrix");
289 return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
291 iq_matrix = picture->iq_matrix->param;
293 intra_quant_matrix = priv->seq_hdr.intra_quantizer_matrix;
294 non_intra_quant_matrix = priv->seq_hdr.non_intra_quantizer_matrix;
295 if (priv->has_quant_matrix_ext) {
296 if (priv->quant_matrix_ext.load_intra_quantiser_matrix)
297 intra_quant_matrix = priv->quant_matrix_ext.intra_quantiser_matrix;
298 if (priv->quant_matrix_ext.load_non_intra_quantiser_matrix)
299 non_intra_quant_matrix = priv->quant_matrix_ext.non_intra_quantiser_matrix;
300 if (priv->quant_matrix_ext.load_chroma_intra_quantiser_matrix)
301 chroma_intra_quant_matrix = priv->quant_matrix_ext.chroma_intra_quantiser_matrix;
302 if (priv->quant_matrix_ext.load_chroma_non_intra_quantiser_matrix)
303 chroma_non_intra_quant_matrix = priv->quant_matrix_ext.chroma_non_intra_quantiser_matrix;
306 iq_matrix->load_intra_quantiser_matrix = intra_quant_matrix != NULL;
307 if (intra_quant_matrix)
308 copy_quant_matrix(iq_matrix->intra_quantiser_matrix,
311 iq_matrix->load_non_intra_quantiser_matrix = non_intra_quant_matrix != NULL;
312 if (non_intra_quant_matrix)
313 copy_quant_matrix(iq_matrix->non_intra_quantiser_matrix,
314 non_intra_quant_matrix);
316 iq_matrix->load_chroma_intra_quantiser_matrix = chroma_intra_quant_matrix != NULL;
317 if (chroma_intra_quant_matrix)
318 copy_quant_matrix(iq_matrix->chroma_intra_quantiser_matrix,
319 chroma_intra_quant_matrix);
321 iq_matrix->load_chroma_non_intra_quantiser_matrix = chroma_non_intra_quant_matrix != NULL;
322 if (chroma_non_intra_quant_matrix)
323 copy_quant_matrix(iq_matrix->chroma_non_intra_quantiser_matrix,
324 chroma_non_intra_quant_matrix);
325 return GST_VAAPI_DECODER_STATUS_SUCCESS;
328 static inline GstVaapiDecoderStatus
329 render_picture(GstVaapiDecoderMpeg2 *decoder, GstVaapiPicture *picture)
331 if (!gst_vaapi_picture_output(picture))
332 return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
333 return GST_VAAPI_DECODER_STATUS_SUCCESS;
336 static GstVaapiDecoderStatus
337 decode_current_picture(GstVaapiDecoderMpeg2 *decoder)
339 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
340 GstVaapiPicture * const picture = priv->current_picture;
341 GstVaapiDecoderStatus status = GST_VAAPI_DECODER_STATUS_SUCCESS;
344 if (!gst_vaapi_picture_decode(picture))
345 status = GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
346 if (!GST_VAAPI_PICTURE_IS_REFERENCE(picture)) {
347 if ((priv->prev_picture && priv->next_picture) ||
348 (priv->closed_gop && priv->next_picture))
349 status = render_picture(decoder, picture);
351 gst_vaapi_picture_replace(&priv->current_picture, NULL);
356 static GstVaapiDecoderStatus
357 decode_sequence(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
359 GstVaapiDecoder * const base_decoder = GST_VAAPI_DECODER(decoder);
360 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
361 GstMpegVideoSequenceHdr * const seq_hdr = &priv->seq_hdr;
363 if (!gst_mpeg_video_parse_sequence_header(seq_hdr, buf, buf_size, 0)) {
364 GST_DEBUG("failed to parse sequence header");
365 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
368 priv->fps_n = seq_hdr->fps_n;
369 priv->fps_d = seq_hdr->fps_d;
370 gst_vaapi_decoder_set_framerate(base_decoder, priv->fps_n, priv->fps_d);
372 priv->seq_pts = gst_adapter_prev_timestamp(priv->adapter, NULL);
374 priv->width = seq_hdr->width;
375 priv->height = seq_hdr->height;
376 priv->has_seq_ext = FALSE;
377 priv->size_changed = TRUE;
378 priv->quant_matrix_changed = TRUE;
379 priv->progressive_sequence = TRUE;
380 return GST_VAAPI_DECODER_STATUS_SUCCESS;
383 static GstVaapiDecoderStatus
384 decode_sequence_ext(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
386 GstVaapiDecoder * const base_decoder = GST_VAAPI_DECODER(decoder);
387 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
388 GstMpegVideoSequenceExt * const seq_ext = &priv->seq_ext;
389 GstVaapiProfile profile;
392 if (!gst_mpeg_video_parse_sequence_extension(seq_ext, buf, buf_size, 0)) {
393 GST_DEBUG("failed to parse sequence-extension");
394 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
396 priv->has_seq_ext = TRUE;
397 priv->progressive_sequence = seq_ext->progressive;
399 width = (priv->width & 0x0fff) | ((guint32)seq_ext->horiz_size_ext << 12);
400 height = (priv->height & 0x0fff) | ((guint32)seq_ext->vert_size_ext << 12);
401 GST_DEBUG("video resolution %ux%u", width, height);
403 if (seq_ext->fps_n_ext && seq_ext->fps_d_ext) {
404 priv->fps_n *= seq_ext->fps_n_ext + 1;
405 priv->fps_d *= seq_ext->fps_d_ext + 1;
406 gst_vaapi_decoder_set_framerate(base_decoder, priv->fps_n, priv->fps_d);
409 if (priv->width != width) {
411 priv->size_changed = TRUE;
414 if (priv->height != height) {
415 priv->height = height;
416 priv->size_changed = TRUE;
419 switch (seq_ext->profile) {
420 case GST_MPEG_VIDEO_PROFILE_SIMPLE:
421 profile = GST_VAAPI_PROFILE_MPEG2_SIMPLE;
423 case GST_MPEG_VIDEO_PROFILE_MAIN:
424 profile = GST_VAAPI_PROFILE_MPEG2_MAIN;
427 GST_DEBUG("unsupported profile %d", seq_ext->profile);
428 return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
430 if (priv->profile != profile) {
431 priv->profile = profile;
432 priv->profile_changed = TRUE;
434 return GST_VAAPI_DECODER_STATUS_SUCCESS;
437 static GstVaapiDecoderStatus
438 decode_sequence_end(GstVaapiDecoderMpeg2 *decoder)
440 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
441 GstVaapiDecoderStatus status;
443 if (priv->current_picture) {
444 status = decode_current_picture(decoder);
445 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
449 if (priv->next_picture) {
450 status = render_picture(decoder, priv->next_picture);
451 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
454 return GST_VAAPI_DECODER_STATUS_END_OF_STREAM;
457 static GstVaapiDecoderStatus
458 decode_quant_matrix_ext(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
460 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
461 GstMpegVideoQuantMatrixExt * const quant_matrix_ext = &priv->quant_matrix_ext;
463 if (!gst_mpeg_video_parse_quant_matrix_extension(quant_matrix_ext, buf, buf_size, 0)) {
464 GST_DEBUG("failed to parse quant-matrix-extension");
465 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
467 priv->has_quant_matrix_ext = TRUE;
468 priv->quant_matrix_changed = TRUE;
469 return GST_VAAPI_DECODER_STATUS_SUCCESS;
472 static GstVaapiDecoderStatus
473 decode_gop(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
475 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
479 if (!gst_mpeg_video_parse_gop(&gop, buf, buf_size, 0)) {
480 GST_DEBUG("failed to parse GOP");
481 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
484 priv->closed_gop = gop.closed_gop;
485 priv->broken_link = gop.broken_link;
487 GST_DEBUG("GOP %02u:%02u:%02u:%02u (closed_gop %d, broken_link %d)",
488 gop.hour, gop.minute, gop.second, gop.frame,
489 priv->closed_gop, priv->broken_link);
491 pts = GST_SECOND * (gop.hour * 3600 + gop.minute * 60 + gop.second);
492 pts += gst_util_uint64_scale(gop.frame, GST_SECOND * priv->fps_d, priv->fps_n);
495 priv->pts_diff = priv->seq_pts - priv->gop_pts;
497 priv->is_first_field = FALSE;
498 return GST_VAAPI_DECODER_STATUS_SUCCESS;
501 static GstVaapiDecoderStatus
502 decode_picture(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
504 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
505 GstMpegVideoPictureHdr * const pic_hdr = &priv->pic_hdr;
506 GstVaapiPicture *picture;
507 GstVaapiDecoderStatus status;
510 status = ensure_context(decoder);
511 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS) {
512 GST_DEBUG("failed to reset context");
516 if (priv->current_picture) {
517 status = decode_current_picture(decoder);
518 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
522 priv->current_picture = GST_VAAPI_PICTURE_NEW(MPEG2, decoder);
523 if (!priv->current_picture) {
524 GST_DEBUG("failed to allocate picture");
525 return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
527 picture = priv->current_picture;
529 status = ensure_quant_matrix(decoder, picture);
530 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS) {
531 GST_DEBUG("failed to reset quantizer matrix");
535 if (!gst_mpeg_video_parse_picture_header(pic_hdr, buf, buf_size, 0)) {
536 GST_DEBUG("failed to parse picture header");
537 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
539 priv->has_pic_ext = FALSE;
541 switch (pic_hdr->pic_type) {
542 case GST_MPEG_VIDEO_PICTURE_TYPE_I:
543 picture->type = GST_VAAPI_PICTURE_TYPE_I;
545 case GST_MPEG_VIDEO_PICTURE_TYPE_P:
546 picture->type = GST_VAAPI_PICTURE_TYPE_P;
548 case GST_MPEG_VIDEO_PICTURE_TYPE_B:
549 picture->type = GST_VAAPI_PICTURE_TYPE_B;
552 GST_DEBUG("unsupported picture type %d", pic_hdr->pic_type);
553 return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
556 /* Update presentation time */
558 pts += gst_util_uint64_scale(pic_hdr->tsn, GST_SECOND * priv->fps_d, priv->fps_n);
559 picture->pts = pts + priv->pts_diff;
561 /* Update reference pictures */
562 if (pic_hdr->pic_type != GST_MPEG_VIDEO_PICTURE_TYPE_B) {
563 GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_REFERENCE);
564 if (priv->next_picture)
565 status = render_picture(decoder, priv->next_picture);
566 gst_vaapi_picture_replace(&priv->prev_picture, priv->next_picture);
567 gst_vaapi_picture_replace(&priv->next_picture, picture);
572 static GstVaapiDecoderStatus
573 decode_picture_ext(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
575 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
576 GstMpegVideoPictureExt * const pic_ext = &priv->pic_ext;
577 GstVaapiPicture * const picture = priv->current_picture;
579 if (!gst_mpeg_video_parse_picture_extension(pic_ext, buf, buf_size, 0)) {
580 GST_DEBUG("failed to parse picture-extension");
581 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
583 priv->has_pic_ext = TRUE;
585 if (pic_ext->picture_structure == 0 ||
586 (pic_ext->progressive_frame &&
587 pic_ext->picture_structure != GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME)) {
588 GST_WARNING("invalid picture_structure %d, replacing with \"frame\"",
589 pic_ext->picture_structure);
590 pic_ext->picture_structure = GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME;
593 priv->is_first_field ^= 1;
594 switch (pic_ext->picture_structure) {
595 case GST_MPEG_VIDEO_PICTURE_STRUCTURE_TOP_FIELD:
596 GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_TOP_FIELD);
598 case GST_MPEG_VIDEO_PICTURE_STRUCTURE_BOTTOM_FIELD:
599 GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_BOTTOM_FIELD);
601 case GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME:
602 priv->is_first_field = TRUE;
605 return GST_VAAPI_DECODER_STATUS_SUCCESS;
608 static inline guint32
609 pack_f_code(guint8 f_code[2][2])
611 return (((guint32)f_code[0][0] << 12) |
612 ((guint32)f_code[0][1] << 8) |
613 ((guint32)f_code[1][0] << 4) |
618 fill_picture(GstVaapiDecoderMpeg2 *decoder, GstVaapiPicture *picture)
620 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
621 VAPictureParameterBufferMPEG2 * const pic_param = picture->param;
622 GstMpegVideoPictureHdr * const pic_hdr = &priv->pic_hdr;
623 GstMpegVideoPictureExt * const pic_ext = &priv->pic_ext;
625 if (!priv->has_pic_ext)
628 /* Fill in VAPictureParameterBufferMPEG2 */
629 pic_param->horizontal_size = priv->width;
630 pic_param->vertical_size = priv->height;
631 pic_param->forward_reference_picture = VA_INVALID_ID;
632 pic_param->backward_reference_picture = VA_INVALID_ID;
633 pic_param->picture_coding_type = pic_hdr->pic_type;
634 pic_param->f_code = pack_f_code(pic_ext->f_code);
636 #define COPY_FIELD(a, b, f) \
637 pic_param->a.b.f = pic_ext->f
638 pic_param->picture_coding_extension.value = 0;
639 pic_param->picture_coding_extension.bits.is_first_field = priv->is_first_field;
640 COPY_FIELD(picture_coding_extension, bits, intra_dc_precision);
641 COPY_FIELD(picture_coding_extension, bits, picture_structure);
642 COPY_FIELD(picture_coding_extension, bits, top_field_first);
643 COPY_FIELD(picture_coding_extension, bits, frame_pred_frame_dct);
644 COPY_FIELD(picture_coding_extension, bits, concealment_motion_vectors);
645 COPY_FIELD(picture_coding_extension, bits, q_scale_type);
646 COPY_FIELD(picture_coding_extension, bits, intra_vlc_format);
647 COPY_FIELD(picture_coding_extension, bits, alternate_scan);
648 COPY_FIELD(picture_coding_extension, bits, repeat_first_field);
649 COPY_FIELD(picture_coding_extension, bits, progressive_frame);
651 switch (pic_hdr->pic_type) {
652 case GST_MPEG_VIDEO_PICTURE_TYPE_B:
653 if (priv->next_picture)
654 pic_param->backward_reference_picture = priv->next_picture->surface_id;
656 case GST_MPEG_VIDEO_PICTURE_TYPE_P:
657 if (priv->prev_picture)
658 pic_param->forward_reference_picture = priv->prev_picture->surface_id;
664 static GstVaapiDecoderStatus
666 GstVaapiDecoderMpeg2 *decoder,
672 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
673 GstVaapiPicture * const picture = priv->current_picture;
674 GstVaapiSlice *slice;
675 VASliceParameterBufferMPEG2 *slice_param;
677 gint mb_x, mb_y, mb_inc;
678 guint macroblock_offset;
679 guint8 slice_vertical_position_extension;
680 guint8 quantiser_scale_code;
681 guint8 intra_slice_flag, intra_slice = 0;
682 guint8 extra_bit_slice, junk8;
684 GST_DEBUG("slice %d @ %p, %u bytes)", slice_no, buf, buf_size);
686 if (picture->slices->len == 0 && !fill_picture(decoder, picture))
687 return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
689 slice = GST_VAAPI_SLICE_NEW(MPEG2, decoder, buf, buf_size);
691 GST_DEBUG("failed to allocate slice");
692 return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
694 gst_vaapi_picture_add_slice(picture, slice);
697 gst_bit_reader_init(&br, buf, buf_size);
698 if (priv->height > 2800)
699 READ_UINT8(&br, slice_vertical_position_extension, 3);
700 if (priv->has_seq_scalable_ext) {
701 GST_DEBUG("failed to parse slice %d. Unsupported sequence_scalable_extension()", slice_no);
702 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
704 READ_UINT8(&br, quantiser_scale_code, 5);
705 READ_UINT8(&br, extra_bit_slice, 1);
706 if (extra_bit_slice == 1) {
707 READ_UINT8(&br, intra_slice_flag, 1);
708 if (intra_slice_flag) {
709 READ_UINT8(&br, intra_slice, 1);
710 READ_UINT8(&br, junk8, 7);
712 READ_UINT8(&br, extra_bit_slice, 1);
713 while (extra_bit_slice == 1) {
714 READ_UINT8(&br, junk8, 8);
715 READ_UINT8(&br, extra_bit_slice, 1);
718 macroblock_offset = gst_bit_reader_get_pos(&br);
723 if (!decode_vlc(&br, &mb_inc, mpeg2_mbaddr_vlc_table,
724 G_N_ELEMENTS(mpeg2_mbaddr_vlc_table))) {
725 GST_WARNING("failed to decode first macroblock_address_increment");
728 mb_x += mb_inc == GST_MPEG_VIDEO_MACROBLOCK_ESCAPE ? 33 : mb_inc;
729 } while (mb_inc == GST_MPEG_VIDEO_MACROBLOCK_ESCAPE);
731 /* Fill in VASliceParameterBufferMPEG2 */
732 slice_param = slice->param;
733 slice_param->macroblock_offset = macroblock_offset;
734 slice_param->slice_horizontal_position = mb_x;
735 slice_param->slice_vertical_position = mb_y;
736 slice_param->quantiser_scale_code = quantiser_scale_code;
737 slice_param->intra_slice_flag = intra_slice;
738 return GST_VAAPI_DECODER_STATUS_SUCCESS;
741 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
745 scan_for_start_code(GstAdapter *adapter, guint ofs, guint size, guint32 *scp)
747 return (gint)gst_adapter_masked_scan_uint32_peek(adapter,
748 0xffffff00, 0x00000100,
753 static GstVaapiDecoderStatus
754 decode_buffer(GstVaapiDecoderMpeg2 *decoder, GstBuffer *buffer)
756 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
757 GstVaapiDecoderStatus status;
759 guint buf_size, size;
764 buf = GST_BUFFER_DATA(buffer);
765 buf_size = GST_BUFFER_SIZE(buffer);
766 if (!buf && buf_size == 0)
767 return decode_sequence_end(decoder);
769 gst_adapter_push(priv->adapter, gst_buffer_ref(buffer));
771 size = gst_adapter_available(priv->adapter);
772 status = GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
776 ofs = scan_for_start_code(priv->adapter, 0, size, &start_code);
779 gst_adapter_flush(priv->adapter, ofs);
782 status = gst_vaapi_decoder_check_status(GST_VAAPI_DECODER(decoder));
783 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
788 ofs = scan_for_start_code(priv->adapter, 4, size - 4, NULL);
791 gst_adapter_flush(priv->adapter, 4);
794 buffer = gst_adapter_take_buffer(priv->adapter, ofs - 4);
795 buf = GST_BUFFER_DATA(buffer);
796 buf_size = GST_BUFFER_SIZE(buffer);
798 type = start_code & 0xff;
800 case GST_MPEG_VIDEO_PACKET_PICTURE:
801 if (!priv->width || !priv->height)
803 status = decode_picture(decoder, buf, buf_size);
805 case GST_MPEG_VIDEO_PACKET_SEQUENCE:
806 status = decode_sequence(decoder, buf, buf_size);
808 case GST_MPEG_VIDEO_PACKET_EXTENSION: {
809 const guchar id = buf[0] >> 4;
811 case GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE:
812 status = decode_sequence_ext(decoder, buf, buf_size);
814 case GST_MPEG_VIDEO_PACKET_EXT_QUANT_MATRIX:
815 status = decode_quant_matrix_ext(decoder, buf, buf_size);
817 case GST_MPEG_VIDEO_PACKET_EXT_PICTURE:
818 if (!priv->width || !priv->height)
820 status = decode_picture_ext(decoder, buf, buf_size);
823 // Ignore unknown extensions
824 GST_DEBUG("unsupported start-code extension (0x%02x)", id);
829 case GST_MPEG_VIDEO_PACKET_SEQUENCE_END:
830 status = decode_sequence_end(decoder);
832 case GST_MPEG_VIDEO_PACKET_GOP:
833 status = decode_gop(decoder, buf, buf_size);
835 case GST_MPEG_VIDEO_PACKET_USER_DATA:
836 // Ignore user-data packets
837 status = GST_VAAPI_DECODER_STATUS_SUCCESS;
840 if (type >= GST_MPEG_VIDEO_PACKET_SLICE_MIN &&
841 type <= GST_MPEG_VIDEO_PACKET_SLICE_MAX) {
842 if (!priv->current_picture)
844 status = decode_slice(
846 type - GST_MPEG_VIDEO_PACKET_SLICE_MIN,
851 else if (type >= 0xb9 && type <= 0xff) {
852 // Ignore system start codes (PES headers)
853 status = GST_VAAPI_DECODER_STATUS_SUCCESS;
856 GST_DEBUG("unsupported start code (0x%02x)", type);
857 status = GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
860 gst_buffer_unref(buffer);
861 } while (status == GST_VAAPI_DECODER_STATUS_SUCCESS);
865 GstVaapiDecoderStatus
866 gst_vaapi_decoder_mpeg2_decode(GstVaapiDecoder *base, GstBuffer *buffer)
868 GstVaapiDecoderMpeg2 * const decoder = GST_VAAPI_DECODER_MPEG2(base);
869 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
871 g_return_val_if_fail(priv->is_constructed,
872 GST_VAAPI_DECODER_STATUS_ERROR_INIT_FAILED);
874 if (!priv->is_opened) {
875 priv->is_opened = gst_vaapi_decoder_mpeg2_open(decoder, buffer);
876 if (!priv->is_opened)
877 return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CODEC;
879 return decode_buffer(decoder, buffer);
883 gst_vaapi_decoder_mpeg2_finalize(GObject *object)
885 GstVaapiDecoderMpeg2 * const decoder = GST_VAAPI_DECODER_MPEG2(object);
887 gst_vaapi_decoder_mpeg2_destroy(decoder);
889 G_OBJECT_CLASS(gst_vaapi_decoder_mpeg2_parent_class)->finalize(object);
893 gst_vaapi_decoder_mpeg2_constructed(GObject *object)
895 GstVaapiDecoderMpeg2 * const decoder = GST_VAAPI_DECODER_MPEG2(object);
896 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
897 GObjectClass *parent_class;
899 parent_class = G_OBJECT_CLASS(gst_vaapi_decoder_mpeg2_parent_class);
900 if (parent_class->constructed)
901 parent_class->constructed(object);
903 priv->is_constructed = gst_vaapi_decoder_mpeg2_create(decoder);
907 gst_vaapi_decoder_mpeg2_class_init(GstVaapiDecoderMpeg2Class *klass)
909 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
910 GstVaapiDecoderClass * const decoder_class = GST_VAAPI_DECODER_CLASS(klass);
912 g_type_class_add_private(klass, sizeof(GstVaapiDecoderMpeg2Private));
914 object_class->finalize = gst_vaapi_decoder_mpeg2_finalize;
915 object_class->constructed = gst_vaapi_decoder_mpeg2_constructed;
917 decoder_class->decode = gst_vaapi_decoder_mpeg2_decode;
921 gst_vaapi_decoder_mpeg2_init(GstVaapiDecoderMpeg2 *decoder)
923 GstVaapiDecoderMpeg2Private *priv;
925 priv = GST_VAAPI_DECODER_MPEG2_GET_PRIVATE(decoder);
926 decoder->priv = priv;
931 priv->profile = GST_VAAPI_PROFILE_MPEG2_SIMPLE;
932 priv->current_picture = NULL;
933 priv->next_picture = NULL;
934 priv->prev_picture = NULL;
935 priv->adapter = NULL;
936 priv->seq_pts = GST_CLOCK_TIME_NONE;
937 priv->gop_pts = GST_CLOCK_TIME_NONE;
939 priv->is_constructed = FALSE;
940 priv->is_opened = FALSE;
941 priv->is_first_field = FALSE;
942 priv->has_seq_ext = FALSE;
943 priv->has_seq_scalable_ext = FALSE;
944 priv->has_pic_ext = FALSE;
945 priv->has_quant_matrix_ext = FALSE;
946 priv->size_changed = FALSE;
947 priv->profile_changed = FALSE;
948 priv->quant_matrix_changed = FALSE;
949 priv->progressive_sequence = FALSE;
950 priv->closed_gop = FALSE;
951 priv->broken_link = FALSE;
955 * gst_vaapi_decoder_mpeg2_new:
956 * @display: a #GstVaapiDisplay
957 * @caps: a #GstCaps holding codec information
959 * Creates a new #GstVaapiDecoder for MPEG-2 decoding. The @caps can
960 * hold extra information like codec-data and pictured coded size.
962 * Return value: the newly allocated #GstVaapiDecoder object
965 gst_vaapi_decoder_mpeg2_new(GstVaapiDisplay *display, GstCaps *caps)
967 GstVaapiDecoderMpeg2 *decoder;
969 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
970 g_return_val_if_fail(GST_IS_CAPS(caps), NULL);
972 decoder = g_object_new(
973 GST_VAAPI_TYPE_DECODER_MPEG2,
978 if (!decoder->priv->is_constructed) {
979 g_object_unref(decoder);
982 return GST_VAAPI_DECODER_CAST(decoder);