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_dpb.h"
34 #include "gstvaapidecoder_priv.h"
35 #include "gstvaapidisplay_priv.h"
36 #include "gstvaapiobject_priv.h"
39 #include "gstvaapidebug.h"
41 G_DEFINE_TYPE(GstVaapiDecoderMpeg2,
42 gst_vaapi_decoder_mpeg2,
43 GST_VAAPI_TYPE_DECODER);
45 #define GST_VAAPI_DECODER_MPEG2_GET_PRIVATE(obj) \
46 (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
47 GST_VAAPI_TYPE_DECODER_MPEG2, \
48 GstVaapiDecoderMpeg2Private))
50 #define READ_UINT8(br, val, nbits) G_STMT_START { \
51 if (!gst_bit_reader_get_bits_uint8 (br, &val, nbits)) { \
52 GST_WARNING ("failed to read uint8, nbits: %d", nbits); \
57 #define SKIP(reader, nbits) G_STMT_START { \
58 if (!gst_bit_reader_skip (reader, nbits)) { \
59 GST_WARNING ("failed to skip nbits: %d", nbits); \
65 typedef struct _PTSGenerator PTSGenerator;
66 struct _PTSGenerator {
67 GstClockTime gop_pts; // Current GOP PTS
68 GstClockTime max_pts; // Max picture PTS
69 guint gop_tsn; // Absolute GOP TSN
70 guint max_tsn; // Max picture TSN, relative to last GOP TSN
71 guint ovl_tsn; // How many times TSN overflowed since GOP
72 guint lst_tsn; // Last picture TSN
78 pts_init(PTSGenerator *tsg)
80 tsg->gop_pts = GST_CLOCK_TIME_NONE;
81 tsg->max_pts = GST_CLOCK_TIME_NONE;
90 static inline GstClockTime
91 pts_get_duration(PTSGenerator *tsg, guint num_frames)
93 return gst_util_uint64_scale(num_frames,
94 GST_SECOND * tsg->fps_d, tsg->fps_n);
98 pts_get_poc(PTSGenerator *tsg)
100 return tsg->gop_tsn + tsg->ovl_tsn * 1024 + tsg->lst_tsn;
104 pts_set_framerate(PTSGenerator *tsg, guint fps_n, guint fps_d)
111 pts_sync(PTSGenerator *tsg, GstClockTime gop_pts)
115 if (!GST_CLOCK_TIME_IS_VALID(gop_pts) ||
116 (GST_CLOCK_TIME_IS_VALID(tsg->max_pts) && tsg->max_pts >= gop_pts)) {
117 /* Invalid GOP PTS, interpolate from the last known picture PTS */
118 if (GST_CLOCK_TIME_IS_VALID(tsg->max_pts)) {
119 gop_pts = tsg->max_pts + pts_get_duration(tsg, 1);
120 gop_tsn = tsg->gop_tsn + tsg->ovl_tsn * 1024 + tsg->max_tsn + 1;
128 /* Interpolate GOP TSN from this valid PTS */
129 if (GST_CLOCK_TIME_IS_VALID(tsg->gop_pts))
130 gop_tsn = tsg->gop_tsn + gst_util_uint64_scale(
131 gop_pts - tsg->gop_pts, tsg->fps_n, GST_SECOND * tsg->fps_d);
136 tsg->gop_pts = gop_pts;
137 tsg->gop_tsn = gop_tsn;
144 pts_eval(PTSGenerator *tsg, GstClockTime pic_pts, guint pic_tsn)
148 if (!GST_CLOCK_TIME_IS_VALID(tsg->gop_pts))
151 pts = tsg->gop_pts + pts_get_duration(tsg, tsg->ovl_tsn * 1024 + pic_tsn);
153 if (!GST_CLOCK_TIME_IS_VALID(tsg->max_pts) || tsg->max_pts < pts)
156 if (tsg->max_tsn < pic_tsn)
157 tsg->max_tsn = pic_tsn;
158 else if (tsg->max_tsn == 1023 && pic_tsn < tsg->lst_tsn) { /* TSN wrapped */
159 tsg->max_tsn = pic_tsn;
162 tsg->lst_tsn = pic_tsn;
166 struct _GstVaapiDecoderMpeg2Private {
167 GstVaapiProfile profile;
172 GstMpegVideoSequenceHdr seq_hdr;
173 GstMpegVideoSequenceExt seq_ext;
174 GstMpegVideoPictureHdr pic_hdr;
175 GstMpegVideoPictureExt pic_ext;
176 GstMpegVideoQuantMatrixExt quant_matrix_ext;
177 GstVaapiPicture *current_picture;
181 guint is_constructed : 1;
183 guint has_seq_ext : 1;
184 guint has_seq_scalable_ext : 1;
185 guint has_pic_ext : 1;
186 guint has_quant_matrix_ext : 1;
187 guint size_changed : 1;
188 guint profile_changed : 1;
189 guint quant_matrix_changed : 1;
190 guint progressive_sequence : 1;
191 guint closed_gop : 1;
192 guint broken_link : 1;
195 /* VLC decoder from gst-plugins-bad */
196 typedef struct _VLCTable VLCTable;
204 decode_vlc(GstBitReader *br, gint *res, const VLCTable *table, guint length)
210 for (i = 0; i < length; i++) {
211 if (cbits != table[i].cbits) {
212 cbits = table[i].cbits;
213 if (!gst_bit_reader_peek_bits_uint32(br, &value, cbits)) {
218 if (value == table[i].cword) {
221 *res = table[i].value;
225 GST_DEBUG("failed to find VLC code");
228 GST_WARNING("failed to decode VLC, returning");
233 GST_MPEG_VIDEO_MACROBLOCK_ESCAPE = -1,
236 /* Table B-1: Variable length codes for macroblock_address_increment */
237 static const VLCTable mpeg2_mbaddr_vlc_table[] = {
271 { GST_MPEG_VIDEO_MACROBLOCK_ESCAPE, 0x08, 11 }
275 gst_vaapi_decoder_mpeg2_close(GstVaapiDecoderMpeg2 *decoder)
277 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
279 gst_vaapi_picture_replace(&priv->current_picture, NULL);
282 gst_vaapi_dpb_unref(priv->dpb);
287 gst_adapter_clear(priv->adapter);
288 g_object_unref(priv->adapter);
289 priv->adapter = NULL;
294 gst_vaapi_decoder_mpeg2_open(GstVaapiDecoderMpeg2 *decoder, GstBuffer *buffer)
296 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
298 gst_vaapi_decoder_mpeg2_close(decoder);
300 priv->adapter = gst_adapter_new();
304 priv->dpb = gst_vaapi_dpb_mpeg2_new();
308 pts_init(&priv->tsg);
313 gst_vaapi_decoder_mpeg2_destroy(GstVaapiDecoderMpeg2 *decoder)
315 gst_vaapi_decoder_mpeg2_close(decoder);
319 gst_vaapi_decoder_mpeg2_create(GstVaapiDecoderMpeg2 *decoder)
321 if (!GST_VAAPI_DECODER_CODEC(decoder))
327 copy_quant_matrix(guint8 dst[64], const guint8 src[64])
329 memcpy(dst, src, 64);
332 static GstVaapiDecoderStatus
333 ensure_context(GstVaapiDecoderMpeg2 *decoder)
335 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
336 GstVaapiProfile profiles[2];
337 GstVaapiEntrypoint entrypoint = GST_VAAPI_ENTRYPOINT_VLD;
338 guint i, n_profiles = 0;
339 gboolean reset_context = FALSE;
341 if (priv->profile_changed) {
342 GST_DEBUG("profile changed");
343 priv->profile_changed = FALSE;
344 reset_context = TRUE;
346 profiles[n_profiles++] = priv->profile;
347 if (priv->profile == GST_VAAPI_PROFILE_MPEG2_SIMPLE)
348 profiles[n_profiles++] = GST_VAAPI_PROFILE_MPEG2_MAIN;
350 for (i = 0; i < n_profiles; i++) {
351 if (gst_vaapi_display_has_decoder(GST_VAAPI_DECODER_DISPLAY(decoder),
352 profiles[i], entrypoint))
356 return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
357 priv->profile = profiles[i];
360 if (priv->size_changed) {
361 GST_DEBUG("size changed");
362 priv->size_changed = FALSE;
363 reset_context = TRUE;
367 reset_context = gst_vaapi_decoder_ensure_context(
368 GST_VAAPI_DECODER(decoder),
371 priv->width, priv->height
374 return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
376 return GST_VAAPI_DECODER_STATUS_SUCCESS;
379 static GstVaapiDecoderStatus
380 ensure_quant_matrix(GstVaapiDecoderMpeg2 *decoder, GstVaapiPicture *picture)
382 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
383 VAIQMatrixBufferMPEG2 *iq_matrix;
384 guint8 *intra_quant_matrix = NULL;
385 guint8 *non_intra_quant_matrix = NULL;
386 guint8 *chroma_intra_quant_matrix = NULL;
387 guint8 *chroma_non_intra_quant_matrix = NULL;
389 if (!priv->quant_matrix_changed)
390 return GST_VAAPI_DECODER_STATUS_SUCCESS;
392 priv->quant_matrix_changed = FALSE;
394 picture->iq_matrix = GST_VAAPI_IQ_MATRIX_NEW(MPEG2, decoder);
395 if (!picture->iq_matrix) {
396 GST_ERROR("failed to allocate IQ matrix");
397 return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
399 iq_matrix = picture->iq_matrix->param;
401 intra_quant_matrix = priv->seq_hdr.intra_quantizer_matrix;
402 non_intra_quant_matrix = priv->seq_hdr.non_intra_quantizer_matrix;
403 if (priv->has_quant_matrix_ext) {
404 if (priv->quant_matrix_ext.load_intra_quantiser_matrix)
405 intra_quant_matrix = priv->quant_matrix_ext.intra_quantiser_matrix;
406 if (priv->quant_matrix_ext.load_non_intra_quantiser_matrix)
407 non_intra_quant_matrix = priv->quant_matrix_ext.non_intra_quantiser_matrix;
408 if (priv->quant_matrix_ext.load_chroma_intra_quantiser_matrix)
409 chroma_intra_quant_matrix = priv->quant_matrix_ext.chroma_intra_quantiser_matrix;
410 if (priv->quant_matrix_ext.load_chroma_non_intra_quantiser_matrix)
411 chroma_non_intra_quant_matrix = priv->quant_matrix_ext.chroma_non_intra_quantiser_matrix;
414 iq_matrix->load_intra_quantiser_matrix = intra_quant_matrix != NULL;
415 if (intra_quant_matrix)
416 copy_quant_matrix(iq_matrix->intra_quantiser_matrix,
419 iq_matrix->load_non_intra_quantiser_matrix = non_intra_quant_matrix != NULL;
420 if (non_intra_quant_matrix)
421 copy_quant_matrix(iq_matrix->non_intra_quantiser_matrix,
422 non_intra_quant_matrix);
424 iq_matrix->load_chroma_intra_quantiser_matrix = chroma_intra_quant_matrix != NULL;
425 if (chroma_intra_quant_matrix)
426 copy_quant_matrix(iq_matrix->chroma_intra_quantiser_matrix,
427 chroma_intra_quant_matrix);
429 iq_matrix->load_chroma_non_intra_quantiser_matrix = chroma_non_intra_quant_matrix != NULL;
430 if (chroma_non_intra_quant_matrix)
431 copy_quant_matrix(iq_matrix->chroma_non_intra_quantiser_matrix,
432 chroma_non_intra_quant_matrix);
433 return GST_VAAPI_DECODER_STATUS_SUCCESS;
437 decode_current_picture(GstVaapiDecoderMpeg2 *decoder)
439 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
440 GstVaapiPicture * const picture = priv->current_picture;
443 if (!gst_vaapi_picture_decode(picture))
445 if (GST_VAAPI_PICTURE_IS_COMPLETE(picture)) {
446 if (!gst_vaapi_dpb_add(priv->dpb, picture))
448 gst_vaapi_picture_replace(&priv->current_picture, NULL);
454 static GstVaapiDecoderStatus
455 decode_sequence(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
457 GstVaapiDecoder * const base_decoder = GST_VAAPI_DECODER(decoder);
458 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
459 GstMpegVideoSequenceHdr * const seq_hdr = &priv->seq_hdr;
461 if (!gst_mpeg_video_parse_sequence_header(seq_hdr, buf, buf_size, 0)) {
462 GST_ERROR("failed to parse sequence header");
463 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
466 priv->fps_n = seq_hdr->fps_n;
467 priv->fps_d = seq_hdr->fps_d;
468 pts_set_framerate(&priv->tsg, priv->fps_n, priv->fps_d);
469 gst_vaapi_decoder_set_framerate(base_decoder, priv->fps_n, priv->fps_d);
471 priv->width = seq_hdr->width;
472 priv->height = seq_hdr->height;
473 priv->has_seq_ext = FALSE;
474 priv->size_changed = TRUE;
475 priv->quant_matrix_changed = TRUE;
476 priv->progressive_sequence = TRUE;
477 return GST_VAAPI_DECODER_STATUS_SUCCESS;
480 static GstVaapiDecoderStatus
481 decode_sequence_ext(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
483 GstVaapiDecoder * const base_decoder = GST_VAAPI_DECODER(decoder);
484 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
485 GstMpegVideoSequenceExt * const seq_ext = &priv->seq_ext;
486 GstVaapiProfile profile;
489 if (!gst_mpeg_video_parse_sequence_extension(seq_ext, buf, buf_size, 0)) {
490 GST_ERROR("failed to parse sequence-extension");
491 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
493 priv->has_seq_ext = TRUE;
494 priv->progressive_sequence = seq_ext->progressive;
495 gst_vaapi_decoder_set_interlaced(base_decoder, !priv->progressive_sequence);
497 width = (priv->width & 0x0fff) | ((guint32)seq_ext->horiz_size_ext << 12);
498 height = (priv->height & 0x0fff) | ((guint32)seq_ext->vert_size_ext << 12);
499 GST_DEBUG("video resolution %ux%u", width, height);
501 if (seq_ext->fps_n_ext && seq_ext->fps_d_ext) {
502 priv->fps_n *= seq_ext->fps_n_ext + 1;
503 priv->fps_d *= seq_ext->fps_d_ext + 1;
504 pts_set_framerate(&priv->tsg, priv->fps_n, priv->fps_d);
505 gst_vaapi_decoder_set_framerate(base_decoder, priv->fps_n, priv->fps_d);
508 if (priv->width != width) {
510 priv->size_changed = TRUE;
513 if (priv->height != height) {
514 priv->height = height;
515 priv->size_changed = TRUE;
518 switch (seq_ext->profile) {
519 case GST_MPEG_VIDEO_PROFILE_SIMPLE:
520 profile = GST_VAAPI_PROFILE_MPEG2_SIMPLE;
522 case GST_MPEG_VIDEO_PROFILE_MAIN:
523 profile = GST_VAAPI_PROFILE_MPEG2_MAIN;
526 GST_ERROR("unsupported profile %d", seq_ext->profile);
527 return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
529 if (priv->profile != profile) {
530 priv->profile = profile;
531 priv->profile_changed = TRUE;
533 return GST_VAAPI_DECODER_STATUS_SUCCESS;
536 static GstVaapiDecoderStatus
537 decode_sequence_end(GstVaapiDecoderMpeg2 *decoder)
539 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
541 if (priv->current_picture && !decode_current_picture(decoder))
542 return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
544 gst_vaapi_dpb_flush(priv->dpb);
545 return GST_VAAPI_DECODER_STATUS_END_OF_STREAM;
548 static GstVaapiDecoderStatus
549 decode_quant_matrix_ext(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
551 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
552 GstMpegVideoQuantMatrixExt * const quant_matrix_ext = &priv->quant_matrix_ext;
554 if (!gst_mpeg_video_parse_quant_matrix_extension(quant_matrix_ext, buf, buf_size, 0)) {
555 GST_ERROR("failed to parse quant-matrix-extension");
556 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
558 priv->has_quant_matrix_ext = TRUE;
559 priv->quant_matrix_changed = TRUE;
560 return GST_VAAPI_DECODER_STATUS_SUCCESS;
563 static GstVaapiDecoderStatus
564 decode_gop(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
566 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
570 if (!gst_mpeg_video_parse_gop(&gop, buf, buf_size, 0)) {
571 GST_ERROR("failed to parse GOP");
572 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
575 priv->closed_gop = gop.closed_gop;
576 priv->broken_link = gop.broken_link;
578 GST_DEBUG("GOP %02u:%02u:%02u:%02u (closed_gop %d, broken_link %d)",
579 gop.hour, gop.minute, gop.second, gop.frame,
580 priv->closed_gop, priv->broken_link);
582 pts = gst_adapter_prev_timestamp(priv->adapter, NULL);
583 pts_sync(&priv->tsg, pts);
584 return GST_VAAPI_DECODER_STATUS_SUCCESS;
587 static GstVaapiDecoderStatus
588 decode_picture(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
590 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
591 GstMpegVideoPictureHdr * const pic_hdr = &priv->pic_hdr;
592 GstVaapiPicture *picture;
593 GstVaapiDecoderStatus status;
596 status = ensure_context(decoder);
597 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS) {
598 GST_ERROR("failed to reset context");
602 if (priv->current_picture && !decode_current_picture(decoder))
603 return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
605 if (priv->current_picture) {
606 /* Re-use current picture where the first field was decoded */
607 picture = gst_vaapi_picture_new_field(priv->current_picture);
609 GST_ERROR("failed to allocate field picture");
610 return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
614 /* Create new picture */
615 picture = GST_VAAPI_PICTURE_NEW(MPEG2, decoder);
617 GST_ERROR("failed to allocate picture");
618 return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
621 gst_vaapi_picture_replace(&priv->current_picture, picture);
622 gst_vaapi_picture_unref(picture);
624 status = ensure_quant_matrix(decoder, picture);
625 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS) {
626 GST_ERROR("failed to reset quantizer matrix");
630 if (!gst_mpeg_video_parse_picture_header(pic_hdr, buf, buf_size, 0)) {
631 GST_ERROR("failed to parse picture header");
632 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
634 priv->has_pic_ext = FALSE;
636 switch (pic_hdr->pic_type) {
637 case GST_MPEG_VIDEO_PICTURE_TYPE_I:
638 GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_REFERENCE);
639 picture->type = GST_VAAPI_PICTURE_TYPE_I;
641 case GST_MPEG_VIDEO_PICTURE_TYPE_P:
642 GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_REFERENCE);
643 picture->type = GST_VAAPI_PICTURE_TYPE_P;
645 case GST_MPEG_VIDEO_PICTURE_TYPE_B:
646 picture->type = GST_VAAPI_PICTURE_TYPE_B;
649 GST_ERROR("unsupported picture type %d", pic_hdr->pic_type);
650 return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
653 /* Update presentation time */
654 pts = gst_adapter_prev_timestamp(priv->adapter, NULL);
655 picture->pts = pts_eval(&priv->tsg, pts, pic_hdr->tsn);
656 picture->poc = pts_get_poc(&priv->tsg);
660 static GstVaapiDecoderStatus
661 decode_picture_ext(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
663 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
664 GstMpegVideoPictureExt * const pic_ext = &priv->pic_ext;
665 GstVaapiPicture * const picture = priv->current_picture;
667 if (!gst_mpeg_video_parse_picture_extension(pic_ext, buf, buf_size, 0)) {
668 GST_ERROR("failed to parse picture-extension");
669 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
671 priv->has_pic_ext = TRUE;
673 if (priv->progressive_sequence && !pic_ext->progressive_frame) {
674 GST_WARNING("invalid interlaced frame in progressive sequence, fixing");
675 pic_ext->progressive_frame = 1;
678 if (pic_ext->picture_structure == 0 ||
679 (pic_ext->progressive_frame &&
680 pic_ext->picture_structure != GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME)) {
681 GST_WARNING("invalid picture_structure %d, replacing with \"frame\"",
682 pic_ext->picture_structure);
683 pic_ext->picture_structure = GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME;
686 if (!priv->progressive_sequence && !pic_ext->progressive_frame) {
687 GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_INTERLACED);
688 if (pic_ext->top_field_first)
689 GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_TFF);
692 switch (pic_ext->picture_structure) {
693 case GST_MPEG_VIDEO_PICTURE_STRUCTURE_TOP_FIELD:
694 picture->structure = GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD;
696 case GST_MPEG_VIDEO_PICTURE_STRUCTURE_BOTTOM_FIELD:
697 picture->structure = GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD;
699 case GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME:
700 picture->structure = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
703 return GST_VAAPI_DECODER_STATUS_SUCCESS;
706 static inline guint32
707 pack_f_code(guint8 f_code[2][2])
709 return (((guint32)f_code[0][0] << 12) |
710 ((guint32)f_code[0][1] << 8) |
711 ((guint32)f_code[1][0] << 4) |
716 fill_picture(GstVaapiDecoderMpeg2 *decoder, GstVaapiPicture *picture)
718 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
719 VAPictureParameterBufferMPEG2 * const pic_param = picture->param;
720 GstMpegVideoPictureHdr * const pic_hdr = &priv->pic_hdr;
721 GstMpegVideoPictureExt * const pic_ext = &priv->pic_ext;
722 GstVaapiPicture *prev_picture, *next_picture;
724 if (!priv->has_pic_ext)
727 /* Fill in VAPictureParameterBufferMPEG2 */
728 pic_param->horizontal_size = priv->width;
729 pic_param->vertical_size = priv->height;
730 pic_param->forward_reference_picture = VA_INVALID_ID;
731 pic_param->backward_reference_picture = VA_INVALID_ID;
732 pic_param->picture_coding_type = pic_hdr->pic_type;
733 pic_param->f_code = pack_f_code(pic_ext->f_code);
735 #define COPY_FIELD(a, b, f) \
736 pic_param->a.b.f = pic_ext->f
737 pic_param->picture_coding_extension.value = 0;
738 pic_param->picture_coding_extension.bits.is_first_field = GST_VAAPI_PICTURE_IS_FIRST_FIELD(picture);
739 COPY_FIELD(picture_coding_extension, bits, intra_dc_precision);
740 COPY_FIELD(picture_coding_extension, bits, picture_structure);
741 COPY_FIELD(picture_coding_extension, bits, top_field_first);
742 COPY_FIELD(picture_coding_extension, bits, frame_pred_frame_dct);
743 COPY_FIELD(picture_coding_extension, bits, concealment_motion_vectors);
744 COPY_FIELD(picture_coding_extension, bits, q_scale_type);
745 COPY_FIELD(picture_coding_extension, bits, intra_vlc_format);
746 COPY_FIELD(picture_coding_extension, bits, alternate_scan);
747 COPY_FIELD(picture_coding_extension, bits, repeat_first_field);
748 COPY_FIELD(picture_coding_extension, bits, progressive_frame);
750 gst_vaapi_dpb_mpeg2_get_references(
757 switch (pic_hdr->pic_type) {
758 case GST_MPEG_VIDEO_PICTURE_TYPE_B:
760 pic_param->backward_reference_picture = next_picture->surface_id;
762 pic_param->forward_reference_picture = prev_picture->surface_id;
763 else if (!priv->closed_gop)
764 GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_SKIPPED);
766 case GST_MPEG_VIDEO_PICTURE_TYPE_P:
768 pic_param->forward_reference_picture = prev_picture->surface_id;
774 static GstVaapiDecoderStatus
776 GstVaapiDecoderMpeg2 *decoder,
782 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
783 GstVaapiPicture * const picture = priv->current_picture;
784 GstVaapiSlice *slice;
785 VASliceParameterBufferMPEG2 *slice_param;
787 gint mb_x, mb_y, mb_inc;
788 guint macroblock_offset;
789 guint8 slice_vertical_position_extension;
790 guint8 quantiser_scale_code;
791 guint8 intra_slice_flag, intra_slice = 0;
792 guint8 extra_bit_slice, junk8;
794 GST_DEBUG("slice %d @ %p, %u bytes)", slice_no, buf, buf_size);
796 if (picture->slices->len == 0 && !fill_picture(decoder, picture))
797 return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
799 slice = GST_VAAPI_SLICE_NEW(MPEG2, decoder, buf, buf_size);
801 GST_ERROR("failed to allocate slice");
802 return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
804 gst_vaapi_picture_add_slice(picture, slice);
807 gst_bit_reader_init(&br, buf, buf_size);
808 if (priv->height > 2800)
809 READ_UINT8(&br, slice_vertical_position_extension, 3);
810 if (priv->has_seq_scalable_ext) {
811 GST_ERROR("failed to parse slice %d. Unsupported sequence_scalable_extension()", slice_no);
812 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
814 READ_UINT8(&br, quantiser_scale_code, 5);
815 READ_UINT8(&br, extra_bit_slice, 1);
816 if (extra_bit_slice == 1) {
817 READ_UINT8(&br, intra_slice_flag, 1);
818 if (intra_slice_flag) {
819 READ_UINT8(&br, intra_slice, 1);
820 READ_UINT8(&br, junk8, 7);
822 READ_UINT8(&br, extra_bit_slice, 1);
823 while (extra_bit_slice == 1) {
824 READ_UINT8(&br, junk8, 8);
825 READ_UINT8(&br, extra_bit_slice, 1);
828 macroblock_offset = gst_bit_reader_get_pos(&br);
833 if (!decode_vlc(&br, &mb_inc, mpeg2_mbaddr_vlc_table,
834 G_N_ELEMENTS(mpeg2_mbaddr_vlc_table))) {
835 GST_WARNING("failed to decode first macroblock_address_increment");
838 mb_x += mb_inc == GST_MPEG_VIDEO_MACROBLOCK_ESCAPE ? 33 : mb_inc;
839 } while (mb_inc == GST_MPEG_VIDEO_MACROBLOCK_ESCAPE);
841 /* Fill in VASliceParameterBufferMPEG2 */
842 slice_param = slice->param;
843 slice_param->macroblock_offset = macroblock_offset;
844 slice_param->slice_horizontal_position = mb_x;
845 slice_param->slice_vertical_position = mb_y;
846 slice_param->quantiser_scale_code = quantiser_scale_code;
847 slice_param->intra_slice_flag = intra_slice;
848 return GST_VAAPI_DECODER_STATUS_SUCCESS;
851 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
855 scan_for_start_code(GstAdapter *adapter, guint ofs, guint size, guint32 *scp)
857 return (gint)gst_adapter_masked_scan_uint32_peek(adapter,
858 0xffffff00, 0x00000100,
863 static GstVaapiDecoderStatus
864 decode_buffer(GstVaapiDecoderMpeg2 *decoder, GstBuffer *buffer)
866 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
867 GstVaapiDecoderStatus status;
869 guint buf_size, size;
874 buf = GST_BUFFER_DATA(buffer);
875 buf_size = GST_BUFFER_SIZE(buffer);
876 if (!buf && buf_size == 0)
877 return decode_sequence_end(decoder);
879 gst_adapter_push(priv->adapter, gst_buffer_ref(buffer));
881 size = gst_adapter_available(priv->adapter);
882 status = GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
886 ofs = scan_for_start_code(priv->adapter, 0, size, &start_code);
889 gst_adapter_flush(priv->adapter, ofs);
892 status = gst_vaapi_decoder_check_status(GST_VAAPI_DECODER(decoder));
893 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
898 ofs = scan_for_start_code(priv->adapter, 4, size - 4, NULL);
901 gst_adapter_flush(priv->adapter, 4);
905 // Ignore empty user-data packets
906 if ((start_code & 0xff) == GST_MPEG_VIDEO_PACKET_USER_DATA)
908 GST_ERROR("failed to get a valid packet (SC: 0x%08x)", start_code);
909 status = GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
913 buffer = gst_adapter_take_buffer(priv->adapter, ofs - 4);
914 buf = GST_BUFFER_DATA(buffer);
915 buf_size = GST_BUFFER_SIZE(buffer);
917 type = start_code & 0xff;
919 case GST_MPEG_VIDEO_PACKET_PICTURE:
920 if (!priv->width || !priv->height)
922 status = decode_picture(decoder, buf, buf_size);
924 case GST_MPEG_VIDEO_PACKET_SEQUENCE:
925 status = decode_sequence(decoder, buf, buf_size);
927 case GST_MPEG_VIDEO_PACKET_EXTENSION: {
928 const guchar id = buf[0] >> 4;
930 case GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE:
931 status = decode_sequence_ext(decoder, buf, buf_size);
933 case GST_MPEG_VIDEO_PACKET_EXT_QUANT_MATRIX:
934 status = decode_quant_matrix_ext(decoder, buf, buf_size);
936 case GST_MPEG_VIDEO_PACKET_EXT_PICTURE:
937 if (!priv->width || !priv->height)
939 status = decode_picture_ext(decoder, buf, buf_size);
942 // Ignore unknown extensions
943 GST_WARNING("unsupported start-code extension (0x%02x)", id);
948 case GST_MPEG_VIDEO_PACKET_SEQUENCE_END:
949 status = decode_sequence_end(decoder);
951 case GST_MPEG_VIDEO_PACKET_GOP:
952 status = decode_gop(decoder, buf, buf_size);
954 case GST_MPEG_VIDEO_PACKET_USER_DATA:
955 // Ignore user-data packets
956 status = GST_VAAPI_DECODER_STATUS_SUCCESS;
959 if (type >= GST_MPEG_VIDEO_PACKET_SLICE_MIN &&
960 type <= GST_MPEG_VIDEO_PACKET_SLICE_MAX) {
961 if (!priv->current_picture)
963 status = decode_slice(
965 type - GST_MPEG_VIDEO_PACKET_SLICE_MIN,
970 else if (type >= 0xb9 && type <= 0xff) {
971 // Ignore system start codes (PES headers)
972 status = GST_VAAPI_DECODER_STATUS_SUCCESS;
975 GST_WARNING("unsupported start code (0x%02x)", type);
976 status = GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
979 gst_buffer_unref(buffer);
980 } while (status == GST_VAAPI_DECODER_STATUS_SUCCESS);
984 GstVaapiDecoderStatus
985 gst_vaapi_decoder_mpeg2_decode(GstVaapiDecoder *base, GstBuffer *buffer)
987 GstVaapiDecoderMpeg2 * const decoder = GST_VAAPI_DECODER_MPEG2(base);
988 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
990 g_return_val_if_fail(priv->is_constructed,
991 GST_VAAPI_DECODER_STATUS_ERROR_INIT_FAILED);
993 if (!priv->is_opened) {
994 priv->is_opened = gst_vaapi_decoder_mpeg2_open(decoder, buffer);
995 if (!priv->is_opened)
996 return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CODEC;
998 return decode_buffer(decoder, buffer);
1002 gst_vaapi_decoder_mpeg2_finalize(GObject *object)
1004 GstVaapiDecoderMpeg2 * const decoder = GST_VAAPI_DECODER_MPEG2(object);
1006 gst_vaapi_decoder_mpeg2_destroy(decoder);
1008 G_OBJECT_CLASS(gst_vaapi_decoder_mpeg2_parent_class)->finalize(object);
1012 gst_vaapi_decoder_mpeg2_constructed(GObject *object)
1014 GstVaapiDecoderMpeg2 * const decoder = GST_VAAPI_DECODER_MPEG2(object);
1015 GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
1016 GObjectClass *parent_class;
1018 parent_class = G_OBJECT_CLASS(gst_vaapi_decoder_mpeg2_parent_class);
1019 if (parent_class->constructed)
1020 parent_class->constructed(object);
1022 priv->is_constructed = gst_vaapi_decoder_mpeg2_create(decoder);
1026 gst_vaapi_decoder_mpeg2_class_init(GstVaapiDecoderMpeg2Class *klass)
1028 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
1029 GstVaapiDecoderClass * const decoder_class = GST_VAAPI_DECODER_CLASS(klass);
1031 g_type_class_add_private(klass, sizeof(GstVaapiDecoderMpeg2Private));
1033 object_class->finalize = gst_vaapi_decoder_mpeg2_finalize;
1034 object_class->constructed = gst_vaapi_decoder_mpeg2_constructed;
1036 decoder_class->decode = gst_vaapi_decoder_mpeg2_decode;
1040 gst_vaapi_decoder_mpeg2_init(GstVaapiDecoderMpeg2 *decoder)
1042 GstVaapiDecoderMpeg2Private *priv;
1044 priv = GST_VAAPI_DECODER_MPEG2_GET_PRIVATE(decoder);
1045 decoder->priv = priv;
1050 priv->profile = GST_VAAPI_PROFILE_MPEG2_SIMPLE;
1051 priv->current_picture = NULL;
1052 priv->adapter = NULL;
1053 priv->is_constructed = FALSE;
1054 priv->is_opened = FALSE;
1055 priv->has_seq_ext = FALSE;
1056 priv->has_seq_scalable_ext = FALSE;
1057 priv->has_pic_ext = FALSE;
1058 priv->has_quant_matrix_ext = FALSE;
1059 priv->size_changed = FALSE;
1060 priv->profile_changed = FALSE;
1061 priv->quant_matrix_changed = FALSE;
1062 priv->progressive_sequence = FALSE;
1063 priv->closed_gop = FALSE;
1064 priv->broken_link = FALSE;
1068 * gst_vaapi_decoder_mpeg2_new:
1069 * @display: a #GstVaapiDisplay
1070 * @caps: a #GstCaps holding codec information
1072 * Creates a new #GstVaapiDecoder for MPEG-2 decoding. The @caps can
1073 * hold extra information like codec-data and pictured coded size.
1075 * Return value: the newly allocated #GstVaapiDecoder object
1078 gst_vaapi_decoder_mpeg2_new(GstVaapiDisplay *display, GstCaps *caps)
1080 GstVaapiDecoderMpeg2 *decoder;
1082 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1083 g_return_val_if_fail(GST_IS_CAPS(caps), NULL);
1085 decoder = g_object_new(
1086 GST_VAAPI_TYPE_DECODER_MPEG2,
1091 if (!decoder->priv->is_constructed) {
1092 g_object_unref(decoder);
1095 return GST_VAAPI_DECODER_CAST(decoder);