2 * Copyright (C) 2020 Intel Corporation
3 * Author: He Junyan <junyan.he@intel.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 * SECTION:gstmpeg2decoder
22 * @title: GstMpeg2Decoder
23 * @short_description: Base class to implement stateless MPEG2 decoders
32 #include "gstmpeg2decoder.h"
34 GST_DEBUG_CATEGORY (gst_mpeg2_decoder_debug);
35 #define GST_CAT_DEFAULT gst_mpeg2_decoder_debug
37 /* ------------------------------------------------------------------------- */
38 /* --- PTS Generator --- */
39 /* ------------------------------------------------------------------------- */
40 typedef struct _PTSGenerator PTSGenerator;
43 /* The current GOP PTS */
47 /* Absolute GOP TSN */
49 /* Max picture TSN, relative to last GOP TSN */
51 /* How many times TSN overflowed since GOP */
53 /* Last picture TSN */
60 _pts_init (PTSGenerator * tsg)
62 tsg->gop_pts = GST_CLOCK_TIME_NONE;
63 tsg->max_pts = GST_CLOCK_TIME_NONE;
72 static inline GstClockTime
73 _pts_get_duration (PTSGenerator * tsg, guint num_frames)
75 return gst_util_uint64_scale (num_frames, GST_SECOND * tsg->fps_d,
80 _pts_get_poc (PTSGenerator * tsg)
82 return tsg->gop_tsn + tsg->ovl_tsn * 1024 + tsg->lst_tsn;
86 _pts_set_framerate (PTSGenerator * tsg, guint fps_n, guint fps_d)
93 _pts_sync (PTSGenerator * tsg, GstClockTime gop_pts)
97 if (!GST_CLOCK_TIME_IS_VALID (gop_pts) ||
98 (GST_CLOCK_TIME_IS_VALID (tsg->max_pts) && tsg->max_pts >= gop_pts)) {
99 /* Invalid GOP PTS, interpolate from the last known picture PTS */
100 if (GST_CLOCK_TIME_IS_VALID (tsg->max_pts)) {
101 gop_pts = tsg->max_pts + _pts_get_duration (tsg, 1);
102 gop_tsn = tsg->gop_tsn + tsg->ovl_tsn * 1024 + tsg->max_tsn + 1;
108 /* Interpolate GOP TSN from this valid PTS */
109 if (GST_CLOCK_TIME_IS_VALID (tsg->gop_pts))
110 gop_tsn = tsg->gop_tsn + gst_util_uint64_scale (gop_pts - tsg->gop_pts +
111 _pts_get_duration (tsg, 1) - 1, tsg->fps_n, GST_SECOND * tsg->fps_d);
116 tsg->gop_pts = gop_pts;
117 tsg->gop_tsn = gop_tsn;
124 _pts_eval (PTSGenerator * tsg, GstClockTime pic_pts, guint pic_tsn)
128 if (!GST_CLOCK_TIME_IS_VALID (tsg->gop_pts))
129 tsg->gop_pts = _pts_get_duration (tsg, pic_tsn);
132 if (!GST_CLOCK_TIME_IS_VALID (pts))
133 pts = tsg->gop_pts + _pts_get_duration (tsg, tsg->ovl_tsn * 1024 + pic_tsn);
134 else if (pts == tsg->gop_pts) {
135 /* The picture following the GOP header shall be an I-frame.
136 So we can compensate for the GOP start time from here */
137 tsg->gop_pts -= _pts_get_duration (tsg, pic_tsn);
140 if (!GST_CLOCK_TIME_IS_VALID (tsg->max_pts) || tsg->max_pts < pts)
143 if (tsg->max_tsn < pic_tsn)
144 tsg->max_tsn = pic_tsn;
145 else if (tsg->max_tsn == 1023 && pic_tsn < tsg->lst_tsn) { /* TSN wrapped */
146 tsg->max_tsn = pic_tsn;
149 tsg->lst_tsn = pic_tsn;
154 static inline gboolean
155 _seq_hdr_is_valid (GstMpegVideoSequenceHdr * hdr)
157 return hdr->width > 0 && hdr->height > 0;
160 #define SEQ_HDR_INIT (GstMpegVideoSequenceHdr) { 0, }
162 static inline gboolean
163 _seq_ext_is_valid (GstMpegVideoSequenceExt * ext)
165 return ext->profile >= GST_MPEG_VIDEO_PROFILE_422
166 && ext->profile <= GST_MPEG_VIDEO_PROFILE_SIMPLE;
169 #define SEQ_EXT_INIT (GstMpegVideoSequenceExt) { 0xff, 0, }
171 static inline gboolean
172 _seq_display_ext_is_valid (GstMpegVideoSequenceDisplayExt * ext)
174 return ext->video_format != 0xff;
177 #define SEQ_DISPLAY_EXT_INIT (GstMpegVideoSequenceDisplayExt) { 0xff, 0, }
179 static inline gboolean
180 _seq_scalable_ext_is_valid (GstMpegVideoSequenceScalableExt * ext)
182 return ext->scalable_mode != 0xff;
185 #define SEQ_SCALABLE_EXT_INIT (GstMpegVideoSequenceScalableExt) { 0xff, 0, }
187 static inline gboolean
188 _quant_matrix_ext_is_valid (GstMpegVideoQuantMatrixExt * ext)
190 return ext->load_intra_quantiser_matrix != 0xff;
193 #define QUANT_MATRIX_EXT_INIT (GstMpegVideoQuantMatrixExt) { 0xff, { 0, } }
195 static inline gboolean
196 _pic_hdr_is_valid (GstMpegVideoPictureHdr * hdr)
198 return hdr->tsn != 0xffff;
201 #define PIC_HDR_INIT (GstMpegVideoPictureHdr) { 0xffff, 0, }
203 static inline gboolean
204 _pic_hdr_ext_is_valid (GstMpegVideoPictureExt * ext)
206 return ext->f_code[0][0] != 0xff;
209 #define PIC_HDR_EXT_INIT \
210 (GstMpegVideoPictureExt) { { { 0xff, 0, }, { 0, } }, 0, }
214 GST_MPEG2_DECODER_STATE_GOT_SEQ_HDR = 1 << 0,
215 GST_MPEG2_DECODER_STATE_GOT_SEQ_EXT = 1 << 1,
216 GST_MPEG2_DECODER_STATE_GOT_PIC_HDR = 1 << 2,
217 GST_MPEG2_DECODER_STATE_GOT_PIC_EXT = 1 << 3,
218 GST_MPEG2_DECODER_STATE_GOT_SLICE = 1 << 4,
220 GST_MPEG2_DECODER_STATE_VALID_SEQ_HEADERS =
221 (GST_MPEG2_DECODER_STATE_GOT_SEQ_HDR |
222 GST_MPEG2_DECODER_STATE_GOT_SEQ_EXT),
223 GST_MPEG2_DECODER_STATE_VALID_PIC_HEADERS =
224 (GST_MPEG2_DECODER_STATE_GOT_PIC_HDR |
225 GST_MPEG2_DECODER_STATE_GOT_PIC_EXT),
226 GST_MPEG2_DECODER_STATE_VALID_PICTURE =
227 (GST_MPEG2_DECODER_STATE_VALID_SEQ_HEADERS |
228 GST_MPEG2_DECODER_STATE_VALID_PIC_HEADERS |
229 GST_MPEG2_DECODER_STATE_GOT_SLICE)
230 } GstMpeg2DecoderState;
232 struct _GstMpeg2DecoderPrivate
238 GstMpegVideoProfile profile;
239 gboolean progressive;
241 GstMpegVideoSequenceHdr seq_hdr;
242 GstMpegVideoSequenceExt seq_ext;
243 GstMpegVideoSequenceDisplayExt seq_display_ext;
244 GstMpegVideoSequenceScalableExt seq_scalable_ext;
246 /* some sequence info changed after last new_sequence () */
247 gboolean seq_changed;
249 GstMpegVideoQuantMatrixExt quant_matrix;
250 GstMpegVideoPictureHdr pic_hdr;
251 GstMpegVideoPictureExt pic_ext;
254 GstMpeg2DecoderState state;
256 GstClockTime current_pts;
258 GstMpeg2Picture *current_picture;
259 GstVideoCodecFrame *current_frame;
260 GstMpeg2Picture *first_field;
263 #define parent_class gst_mpeg2_decoder_parent_class
264 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstMpeg2Decoder, gst_mpeg2_decoder,
265 GST_TYPE_VIDEO_DECODER,
266 G_ADD_PRIVATE (GstMpeg2Decoder);
267 GST_DEBUG_CATEGORY_INIT (gst_mpeg2_decoder_debug, "mpeg2decoder", 0,
268 "MPEG2 Video Decoder"));
270 static gboolean gst_mpeg2_decoder_start (GstVideoDecoder * decoder);
271 static gboolean gst_mpeg2_decoder_stop (GstVideoDecoder * decoder);
272 static gboolean gst_mpeg2_decoder_set_format (GstVideoDecoder * decoder,
273 GstVideoCodecState * state);
274 static GstFlowReturn gst_mpeg2_decoder_finish (GstVideoDecoder * decoder);
275 static gboolean gst_mpeg2_decoder_flush (GstVideoDecoder * decoder);
276 static GstFlowReturn gst_mpeg2_decoder_drain (GstVideoDecoder * decoder);
277 static GstFlowReturn gst_mpeg2_decoder_handle_frame (GstVideoDecoder * decoder,
278 GstVideoCodecFrame * frame);
279 static gboolean gst_mpeg2_decoder_do_output_picture (GstMpeg2Decoder * self,
280 GstMpeg2Picture * picture);
283 gst_mpeg2_decoder_class_init (GstMpeg2DecoderClass * klass)
285 GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
287 decoder_class->start = GST_DEBUG_FUNCPTR (gst_mpeg2_decoder_start);
288 decoder_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg2_decoder_stop);
289 decoder_class->set_format = GST_DEBUG_FUNCPTR (gst_mpeg2_decoder_set_format);
290 decoder_class->finish = GST_DEBUG_FUNCPTR (gst_mpeg2_decoder_finish);
291 decoder_class->flush = GST_DEBUG_FUNCPTR (gst_mpeg2_decoder_flush);
292 decoder_class->drain = GST_DEBUG_FUNCPTR (gst_mpeg2_decoder_drain);
293 decoder_class->handle_frame =
294 GST_DEBUG_FUNCPTR (gst_mpeg2_decoder_handle_frame);
298 gst_mpeg2_decoder_init (GstMpeg2Decoder * self)
300 gst_video_decoder_set_packetized (GST_VIDEO_DECODER (self), TRUE);
302 self->priv = gst_mpeg2_decoder_get_instance_private (self);
304 self->priv->seq_hdr = SEQ_HDR_INIT;
305 self->priv->seq_ext = SEQ_EXT_INIT;
306 self->priv->seq_display_ext = SEQ_DISPLAY_EXT_INIT;
307 self->priv->seq_scalable_ext = SEQ_SCALABLE_EXT_INIT;
308 self->priv->quant_matrix = QUANT_MATRIX_EXT_INIT;
309 self->priv->pic_hdr = PIC_HDR_INIT;
310 self->priv->pic_ext = PIC_HDR_EXT_INIT;
314 gst_mpeg2_decoder_start (GstVideoDecoder * decoder)
316 GstMpeg2Decoder *self = GST_MPEG2_DECODER (decoder);
317 GstMpeg2DecoderPrivate *priv = self->priv;
319 _pts_init (&priv->tsg);
320 priv->dpb = gst_mpeg2_dpb_new ();
322 priv->progressive = TRUE;
328 gst_mpeg2_decoder_stop (GstVideoDecoder * decoder)
330 GstMpeg2Decoder *self = GST_MPEG2_DECODER (decoder);
331 GstMpeg2DecoderPrivate *priv = self->priv;
333 g_clear_pointer (&self->input_state, gst_video_codec_state_unref);
334 g_clear_pointer (&priv->dpb, gst_mpeg2_dpb_free);
340 gst_mpeg2_decoder_set_format (GstVideoDecoder * decoder,
341 GstVideoCodecState * state)
343 GstMpeg2Decoder *self = GST_MPEG2_DECODER (decoder);
344 GstMpeg2DecoderPrivate *priv = self->priv;
346 GST_DEBUG_OBJECT (decoder, "Set format");
348 if (self->input_state)
349 gst_video_codec_state_unref (self->input_state);
351 self->input_state = gst_video_codec_state_ref (state);
353 priv->width = GST_VIDEO_INFO_WIDTH (&state->info);
354 priv->height = GST_VIDEO_INFO_HEIGHT (&state->info);
360 gst_mpeg2_decoder_drain (GstVideoDecoder * decoder)
362 GstMpeg2Decoder *self = GST_MPEG2_DECODER (decoder);
363 GstMpeg2DecoderPrivate *priv = self->priv;
364 GstMpeg2Picture *picture;
366 while ((picture = gst_mpeg2_dpb_bump (priv->dpb)) != NULL) {
367 gst_mpeg2_decoder_do_output_picture (self, picture);
370 gst_mpeg2_dpb_clear (priv->dpb);
375 gst_mpeg2_decoder_finish (GstVideoDecoder * decoder)
377 return gst_mpeg2_decoder_drain (decoder);
381 gst_mpeg2_decoder_flush (GstVideoDecoder * decoder)
383 GstMpeg2Decoder *self = GST_MPEG2_DECODER (decoder);
384 GstMpeg2DecoderPrivate *priv = self->priv;
386 gst_mpeg2_dpb_clear (priv->dpb);
387 priv->state &= GST_MPEG2_DECODER_STATE_VALID_SEQ_HEADERS;
388 priv->pic_hdr = PIC_HDR_INIT;
389 priv->pic_ext = PIC_HDR_EXT_INIT;
394 static inline gboolean
395 _is_valid_state (GstMpeg2Decoder * decoder, GstMpeg2DecoderState state)
397 GstMpeg2DecoderPrivate *priv = decoder->priv;
399 return (priv->state & state) == state;
403 gst_mpeg2_decoder_set_latency (GstMpeg2Decoder * decoder)
406 GstClockTime min, max;
407 GstMpeg2DecoderPrivate *priv = decoder->priv;
408 GstStructure *structure;
409 gint fps_d = 1, fps_n = 0;
411 if (priv->tsg.fps_d > 0 && priv->tsg.fps_n > 0) {
412 fps_n = priv->tsg.fps_n;
413 fps_d = priv->tsg.fps_d;
415 caps = gst_pad_get_current_caps (GST_VIDEO_DECODER_SINK_PAD (decoder));
417 structure = gst_caps_get_structure (caps, 0);
418 if (gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d)) {
420 /* variable framerate: see if we have a max-framerate */
421 gst_structure_get_fraction (structure, "max-framerate", &fps_n,
425 gst_caps_unref (caps);
429 /* if no fps or variable, then 25/1 */
435 max = gst_util_uint64_scale (2 * GST_SECOND, fps_d, fps_n);
436 min = gst_util_uint64_scale (1 * GST_SECOND, fps_d, fps_n);
438 GST_LOG_OBJECT (decoder,
439 "latency min %" G_GUINT64_FORMAT " max %" G_GUINT64_FORMAT, min, max);
441 gst_video_decoder_set_latency (GST_VIDEO_DECODER (decoder), min, max);
445 gst_mpeg2_decoder_handle_sequence (GstMpeg2Decoder * decoder,
446 GstMpegVideoPacket * packet)
448 GstMpeg2DecoderPrivate *priv = decoder->priv;
449 GstMpegVideoSequenceHdr seq_hdr = { 0, };
451 if (!gst_mpeg_video_packet_parse_sequence_header (packet, &seq_hdr)) {
452 GST_ERROR_OBJECT (decoder, "failed to parse sequence header");
456 /* 6.1.1.6 Sequence header
457 The quantisation matrices may be redefined each time that a sequence
458 header occurs in the bitstream */
459 priv->quant_matrix = QUANT_MATRIX_EXT_INIT;
461 if (_seq_hdr_is_valid (&priv->seq_hdr) &&
462 memcmp (&priv->seq_hdr, &seq_hdr, sizeof (seq_hdr)) == 0)
465 priv->seq_ext = SEQ_EXT_INIT;
466 priv->seq_display_ext = SEQ_DISPLAY_EXT_INIT;
467 priv->seq_scalable_ext = SEQ_SCALABLE_EXT_INIT;
468 priv->pic_ext = PIC_HDR_EXT_INIT;
470 priv->seq_hdr = seq_hdr;
471 priv->seq_changed = TRUE;
473 priv->width = seq_hdr.width;
474 priv->height = seq_hdr.height;
475 priv->display_width = priv->width;
476 priv->display_height = priv->height;
478 _pts_set_framerate (&priv->tsg, seq_hdr.fps_n, seq_hdr.fps_d);
480 gst_mpeg2_decoder_set_latency (decoder);
482 priv->state = GST_MPEG2_DECODER_STATE_GOT_SEQ_HDR;
488 gst_mpeg2_decoder_handle_sequence_ext (GstMpeg2Decoder * decoder,
489 GstMpegVideoPacket * packet)
491 GstMpeg2DecoderPrivate *priv = decoder->priv;
492 GstMpegVideoSequenceExt seq_ext = { 0, };
495 if (!_is_valid_state (decoder, GST_MPEG2_DECODER_STATE_GOT_SEQ_HDR)) {
496 GST_ERROR_OBJECT (decoder, "no sequence before parsing sequence-extension");
500 if (!gst_mpeg_video_packet_parse_sequence_extension (packet, &seq_ext)) {
501 GST_ERROR_OBJECT (decoder, "failed to parse sequence-extension");
505 if (_seq_ext_is_valid (&priv->seq_ext) &&
506 memcmp (&priv->seq_ext, &seq_ext, sizeof (seq_ext)) == 0)
509 priv->seq_ext = seq_ext;
510 priv->seq_changed = TRUE;
512 priv->progressive = seq_ext.progressive;
514 if (seq_ext.fps_n_ext && seq_ext.fps_d_ext) {
515 guint fps_n = priv->tsg.fps_n;
516 guint fps_d = priv->tsg.fps_d;
517 fps_n *= seq_ext.fps_n_ext + 1;
518 fps_d *= seq_ext.fps_d_ext + 1;
519 _pts_set_framerate (&priv->tsg, fps_n, fps_d);
520 gst_mpeg2_decoder_set_latency (decoder);
523 width = (priv->width & 0x0fff) | ((guint32) seq_ext.horiz_size_ext << 12);
524 height = (priv->height & 0x0fff) | ((guint32) seq_ext.vert_size_ext << 12);
525 GST_DEBUG_OBJECT (decoder, "video resolution %ux%u", width, height);
527 priv->height = height;
529 priv->profile = seq_ext.profile;
531 priv->state |= GST_MPEG2_DECODER_STATE_GOT_SEQ_EXT;
537 gst_mpeg2_decoder_handle_sequence_display_ext (GstMpeg2Decoder * decoder,
538 GstMpegVideoPacket * packet)
540 GstMpeg2DecoderPrivate *priv = decoder->priv;
541 GstMpegVideoSequenceDisplayExt seq_display_ext = { 0, };
543 if (!_is_valid_state (decoder, GST_MPEG2_DECODER_STATE_GOT_SEQ_HDR)) {
544 GST_ERROR_OBJECT (decoder,
545 "no sequence before parsing sequence-display-extension");
549 if (!gst_mpeg_video_packet_parse_sequence_display_extension (packet,
551 GST_ERROR_OBJECT (decoder, "failed to parse sequence-display-extension");
555 if (_seq_display_ext_is_valid (&priv->seq_display_ext) &&
556 memcmp (&priv->seq_display_ext, &seq_display_ext,
557 sizeof (seq_display_ext)) == 0)
560 priv->seq_display_ext = seq_display_ext;
561 priv->seq_changed = TRUE;
563 priv->display_width = seq_display_ext.display_horizontal_size;
564 priv->display_height = seq_display_ext.display_vertical_size;
570 gst_mpeg2_decoder_handle_sequence_scalable_ext (GstMpeg2Decoder * decoder,
571 GstMpegVideoPacket * packet)
573 GstMpeg2DecoderPrivate *priv = decoder->priv;
574 GstMpegVideoSequenceScalableExt seq_scalable_ext = { 0, };
576 if (!_is_valid_state (decoder, GST_MPEG2_DECODER_STATE_GOT_SEQ_HDR)) {
577 GST_ERROR_OBJECT (decoder,
578 "no sequence before parsing sequence-scalable-extension");
582 if (!gst_mpeg_video_packet_parse_sequence_scalable_extension (packet,
583 &seq_scalable_ext)) {
584 GST_ERROR_OBJECT (decoder, "failed to parse sequence-scalable-extension");
588 if (_seq_scalable_ext_is_valid (&priv->seq_scalable_ext) &&
589 memcmp (&priv->seq_scalable_ext, &seq_scalable_ext,
590 sizeof (seq_scalable_ext)) == 0)
593 priv->seq_scalable_ext = seq_scalable_ext;
594 priv->seq_changed = TRUE;
600 gst_mpeg2_decoder_handle_quant_matrix_ext (GstMpeg2Decoder * decoder,
601 GstMpegVideoPacket * packet)
603 GstMpeg2DecoderPrivate *priv = decoder->priv;
604 GstMpegVideoQuantMatrixExt matrix_ext = { 0, };
606 if (!gst_mpeg_video_packet_parse_quant_matrix_extension (packet, &matrix_ext)) {
607 GST_ERROR_OBJECT (decoder, "failed to parse sequence-scalable-extension");
611 priv->quant_matrix = matrix_ext;
617 gst_mpeg2_decoder_handle_picture_ext (GstMpeg2Decoder * decoder,
618 GstMpegVideoPacket * packet)
620 GstMpeg2DecoderPrivate *priv = decoder->priv;
621 GstMpegVideoPictureExt pic_ext = { {{0,},}, };
623 if (!_is_valid_state (decoder,
624 GST_MPEG2_DECODER_STATE_VALID_SEQ_HEADERS |
625 GST_MPEG2_DECODER_STATE_GOT_PIC_HDR)) {
626 GST_ERROR_OBJECT (decoder,
627 "no sequence before parsing sequence-scalable-extension");
631 if (!gst_mpeg_video_packet_parse_picture_extension (packet, &pic_ext)) {
632 GST_ERROR_OBJECT (decoder, "failed to parse picture-extension");
636 if (priv->progressive && !pic_ext.progressive_frame) {
637 GST_WARNING_OBJECT (decoder,
638 "invalid interlaced frame in progressive sequence, fixing");
639 pic_ext.progressive_frame = 1;
642 if (pic_ext.picture_structure == 0 ||
643 (pic_ext.progressive_frame &&
644 pic_ext.picture_structure !=
645 GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME)) {
646 GST_WARNING_OBJECT (decoder,
647 "invalid picture_structure %d, replacing with \"frame\"",
648 pic_ext.picture_structure);
649 pic_ext.picture_structure = GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME;
652 priv->pic_ext = pic_ext;
654 priv->state |= GST_MPEG2_DECODER_STATE_GOT_PIC_EXT;
660 gst_mpeg2_decoder_handle_gop (GstMpeg2Decoder * decoder,
661 GstMpegVideoPacket * packet)
663 GstMpeg2DecoderPrivate *priv = decoder->priv;
664 GstMpegVideoGop gop = { 0, };
666 if (!gst_mpeg_video_packet_parse_gop (packet, &gop)) {
667 GST_ERROR_OBJECT (decoder, "failed to parse GOP");
671 GST_DEBUG_OBJECT (decoder,
672 "GOP %02u:%02u:%02u:%02u (closed_gop %d, broken_link %d)", gop.hour,
673 gop.minute, gop.second, gop.frame, gop.closed_gop, gop.broken_link);
677 _pts_sync (&priv->tsg, priv->current_frame->pts);
683 gst_mpeg2_decoder_handle_picture (GstMpeg2Decoder * decoder,
684 GstMpegVideoPacket * packet)
686 GstMpeg2DecoderPrivate *priv = decoder->priv;
687 GstMpegVideoPictureHdr pic_hdr = { 0, };
688 GstMpeg2DecoderClass *klass = GST_MPEG2_DECODER_GET_CLASS (decoder);
690 if (!_is_valid_state (decoder, GST_MPEG2_DECODER_STATE_VALID_SEQ_HEADERS)) {
691 GST_ERROR_OBJECT (decoder, "no sequence before parsing picture header");
695 /* 6.1.1.6: Conversely if no sequence_xxx_extension() occurs between
696 the first sequence_header() and the first picture_header() then
697 sequence_xxx_extension() shall not occur in the bitstream. */
698 if (priv->seq_changed && klass->new_sequence) {
699 priv->seq_changed = FALSE;
700 if (!klass->new_sequence (decoder, &priv->seq_hdr,
701 _seq_ext_is_valid (&priv->seq_ext) ? &priv->seq_ext : NULL,
702 _seq_display_ext_is_valid (&priv->seq_display_ext) ?
703 &priv->seq_display_ext : NULL,
704 _seq_scalable_ext_is_valid (&priv->seq_scalable_ext) ?
705 &priv->seq_scalable_ext : NULL)) {
706 GST_ERROR_OBJECT (decoder, "new sequence error");
711 priv->state &= (GST_MPEG2_DECODER_STATE_GOT_SEQ_HDR |
712 GST_MPEG2_DECODER_STATE_GOT_SEQ_EXT);
714 if (!gst_mpeg_video_packet_parse_picture_header (packet, &pic_hdr)) {
715 GST_ERROR_OBJECT (decoder, "failed to parse picture header");
719 priv->pic_hdr = pic_hdr;
721 priv->state |= GST_MPEG2_DECODER_STATE_GOT_PIC_HDR;
727 gst_mpeg2_decoder_start_current_picture (GstMpeg2Decoder * decoder,
728 GstMpeg2Slice * slice)
730 GstMpeg2DecoderPrivate *priv = decoder->priv;
731 GstMpeg2DecoderClass *klass = GST_MPEG2_DECODER_GET_CLASS (decoder);
732 GstMpeg2Picture *prev_picture, *next_picture;
735 if (!klass->start_picture)
738 gst_mpeg2_dpb_get_neighbours (priv->dpb, priv->current_picture,
739 &prev_picture, &next_picture);
741 if (priv->current_picture->type == GST_MPEG_VIDEO_PICTURE_TYPE_B
742 && !prev_picture && !priv->gop.closed_gop) {
743 GST_VIDEO_CODEC_FRAME_FLAG_SET (priv->current_frame,
744 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
747 ret = klass->start_picture (decoder, priv->current_picture, slice,
748 prev_picture, next_picture);
751 GST_ERROR_OBJECT (decoder, "subclass does not want to start picture");
759 gst_mpeg2_decoder_ensure_current_picture (GstMpeg2Decoder * decoder,
760 GstMpeg2Slice * slice)
762 GstMpeg2DecoderPrivate *priv = decoder->priv;
763 GstMpeg2DecoderClass *klass = GST_MPEG2_DECODER_GET_CLASS (decoder);
764 GstMpeg2Picture *picture = NULL;
767 if (priv->current_picture) {
768 g_assert (_is_valid_state (decoder, GST_MPEG2_DECODER_STATE_GOT_SLICE));
772 if (priv->progressive ||
773 priv->pic_ext.picture_structure ==
774 GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME) {
775 g_assert (!_is_valid_state (decoder, GST_MPEG2_DECODER_STATE_GOT_SLICE));
777 if (priv->first_field) {
778 GST_WARNING_OBJECT (decoder, "An unmatched first field");
779 gst_mpeg2_picture_clear (&priv->first_field);
782 picture = gst_mpeg2_picture_new ();
783 if (klass->new_picture)
784 ret = klass->new_picture (decoder, priv->current_frame, picture);
787 GST_ERROR_OBJECT (decoder, "subclass does not want accept new picture");
788 gst_mpeg2_picture_unref (picture);
792 picture->structure = GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME;
794 if (!priv->first_field) {
795 picture = gst_mpeg2_picture_new ();
796 if (klass->new_picture)
797 ret = klass->new_picture (decoder, priv->current_frame, picture);
800 GST_ERROR_OBJECT (decoder, "subclass does not want accept new picture");
801 gst_mpeg2_picture_unref (picture);
805 picture = gst_mpeg2_picture_new ();
807 if (klass->new_field_picture)
808 ret = klass->new_field_picture (decoder, priv->first_field, picture);
811 GST_ERROR_OBJECT (decoder,
812 "Subclass couldn't handle new field picture");
813 gst_mpeg2_picture_unref (picture);
817 picture->first_field = gst_mpeg2_picture_ref (priv->first_field);
819 /* At this moment, this picture should be interlaced */
820 picture->buffer_flags |= GST_VIDEO_BUFFER_FLAG_INTERLACED;
821 if (priv->pic_ext.top_field_first)
822 picture->buffer_flags |= GST_VIDEO_BUFFER_FLAG_TFF;
825 picture->structure = priv->pic_ext.picture_structure;
828 picture->needed_for_output = TRUE;
829 /* This allows accessing the frame from the picture. */
830 picture->system_frame_number = priv->current_frame->system_frame_number;
831 picture->type = priv->pic_hdr.pic_type;
832 picture->tsn = priv->pic_hdr.tsn;
834 _pts_eval (&priv->tsg, priv->current_frame->pts, picture->tsn);
835 picture->pic_order_cnt = _pts_get_poc (&priv->tsg);
837 priv->current_picture = picture;
838 GST_LOG_OBJECT (decoder,
839 "Create new picture %p(%s), system number: %d, poc: %d,"
840 " type: 0x%d, first field %p",
842 (picture->structure == GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME) ?
844 picture->system_frame_number, picture->pic_order_cnt, picture->type,
845 picture->first_field);
847 if (!gst_mpeg2_decoder_start_current_picture (decoder, slice))
854 gst_mpeg2_decoder_finish_current_field (GstMpeg2Decoder * decoder)
856 GstMpeg2DecoderPrivate *priv = decoder->priv;
857 GstMpeg2DecoderClass *klass = GST_MPEG2_DECODER_GET_CLASS (decoder);
860 if (priv->current_picture == NULL)
863 ret = klass->end_picture (decoder, priv->current_picture);
865 GST_ERROR_OBJECT (decoder, "subclass end_picture failed");
869 if (priv->current_picture->structure !=
870 GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME &&
871 !priv->current_picture->first_field) {
872 priv->first_field = priv->current_picture;
873 priv->current_picture = NULL;
875 GST_WARNING_OBJECT (decoder, "The current picture %p is not %s, should not "
876 "begin another picture. Just discard this.",
877 priv->current_picture, priv->current_picture->structure ==
878 GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME ?
879 " a field" : "the first field");
880 gst_mpeg2_picture_clear (&priv->current_picture);
887 gst_mpeg2_decoder_finish_current_picture (GstMpeg2Decoder * decoder)
889 GstMpeg2DecoderPrivate *priv = decoder->priv;
890 GstMpeg2DecoderClass *klass = GST_MPEG2_DECODER_GET_CLASS (decoder);
893 g_assert (priv->current_picture != NULL);
895 ret = klass->end_picture (decoder, priv->current_picture);
897 GST_ERROR_OBJECT (decoder, "subclass end_picture failed");
901 if (priv->current_picture->structure !=
902 GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME &&
903 !priv->current_picture->first_field) {
904 priv->first_field = priv->current_picture;
905 priv->current_picture = NULL;
912 gst_mpeg2_decoder_handle_slice (GstMpeg2Decoder * decoder,
913 GstMpegVideoPacket * packet)
915 GstMpeg2DecoderPrivate *priv = decoder->priv;
916 GstMpegVideoSliceHdr slice_hdr;
917 GstMpeg2DecoderClass *klass = GST_MPEG2_DECODER_GET_CLASS (decoder);
921 if (!_is_valid_state (decoder, GST_MPEG2_DECODER_STATE_VALID_PIC_HEADERS)) {
922 GST_ERROR_OBJECT (decoder,
923 "no sequence or picture header before parsing picture header");
927 if (!gst_mpeg_video_packet_parse_slice_header (packet, &slice_hdr,
929 _seq_scalable_ext_is_valid (&priv->seq_scalable_ext) ?
930 &priv->seq_scalable_ext : NULL)) {
931 GST_ERROR_OBJECT (decoder, "failed to parse slice header");
935 slice.header = slice_hdr;
936 slice.packet = *packet;
937 slice.quant_matrix = _quant_matrix_ext_is_valid (&priv->quant_matrix) ?
938 &priv->quant_matrix : NULL;
939 g_assert (_pic_hdr_is_valid (&priv->pic_hdr));
940 slice.pic_hdr = &priv->pic_hdr;
941 slice.pic_ext = _pic_hdr_ext_is_valid (&priv->pic_ext) ?
942 &priv->pic_ext : NULL;
944 if (!gst_mpeg2_decoder_ensure_current_picture (decoder, &slice)) {
945 GST_ERROR_OBJECT (decoder, "failed to start current picture");
949 g_assert (klass->decode_slice);
950 ret = klass->decode_slice (decoder, priv->current_picture, &slice);
952 GST_ERROR_OBJECT (decoder,
953 "Subclass didn't want to decode picture %p (frame_num %d, poc %d)",
954 priv->current_picture, priv->current_picture->system_frame_number,
955 priv->current_picture->pic_order_cnt);
959 priv->state |= GST_MPEG2_DECODER_STATE_GOT_SLICE;
965 gst_mpeg2_decoder_decode_packet (GstMpeg2Decoder * decoder,
966 GstMpegVideoPacket * packet)
968 GstMpegVideoPacketExtensionCode ext_type;
971 GST_LOG_OBJECT (decoder, "Parsing the packet 0x%x, size %d",
972 packet->type, packet->size);
973 switch (packet->type) {
974 case GST_MPEG_VIDEO_PACKET_PICTURE:{
975 ret = gst_mpeg2_decoder_finish_current_field (decoder);
979 ret = gst_mpeg2_decoder_handle_picture (decoder, packet);
982 case GST_MPEG_VIDEO_PACKET_SEQUENCE:
983 ret = gst_mpeg2_decoder_handle_sequence (decoder, packet);
985 case GST_MPEG_VIDEO_PACKET_EXTENSION:
986 ext_type = packet->data[packet->offset] >> 4;
987 GST_LOG_OBJECT (decoder, " Parsing the ext packet 0x%x", ext_type);
989 case GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE:
990 ret = gst_mpeg2_decoder_handle_sequence_ext (decoder, packet);
992 case GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE_DISPLAY:
993 ret = gst_mpeg2_decoder_handle_sequence_display_ext (decoder, packet);
995 case GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE_SCALABLE:
997 gst_mpeg2_decoder_handle_sequence_scalable_ext (decoder, packet);
999 case GST_MPEG_VIDEO_PACKET_EXT_QUANT_MATRIX:
1000 ret = gst_mpeg2_decoder_handle_quant_matrix_ext (decoder, packet);
1002 case GST_MPEG_VIDEO_PACKET_EXT_PICTURE:
1003 ret = gst_mpeg2_decoder_handle_picture_ext (decoder, packet);
1006 /* Ignore unknown start-code extensions */
1011 case GST_MPEG_VIDEO_PACKET_SEQUENCE_END:
1014 case GST_MPEG_VIDEO_PACKET_GOP:
1015 ret = gst_mpeg2_decoder_handle_gop (decoder, packet);
1017 case GST_MPEG_VIDEO_PACKET_USER_DATA:
1021 if (packet->type >= GST_MPEG_VIDEO_PACKET_SLICE_MIN &&
1022 packet->type <= GST_MPEG_VIDEO_PACKET_SLICE_MAX) {
1023 ret = gst_mpeg2_decoder_handle_slice (decoder, packet);
1026 GST_WARNING_OBJECT (decoder, "unsupported packet type 0x%02x, ignore",
1035 static GstFlowReturn
1036 gst_mpeg2_decoder_do_output_picture (GstMpeg2Decoder * decoder,
1037 GstMpeg2Picture * to_output)
1039 GstMpeg2DecoderClass *klass = GST_MPEG2_DECODER_GET_CLASS (decoder);
1040 GstVideoCodecFrame *frame = NULL;
1041 GstFlowReturn ret = GST_FLOW_OK;
1044 gst_video_decoder_get_frame (GST_VIDEO_DECODER (decoder),
1045 to_output->system_frame_number);
1047 GST_ERROR_OBJECT (decoder,
1048 "No available codec frame with frame number %d",
1049 to_output->system_frame_number);
1050 gst_mpeg2_picture_unref (to_output);
1051 return GST_FLOW_ERROR;
1054 g_assert (klass->output_picture);
1055 GST_LOG_OBJECT (decoder,
1056 "Output picture %p (frame_num %d, poc %d, pts: %" GST_TIME_FORMAT
1058 to_output, to_output->system_frame_number, to_output->pic_order_cnt,
1059 GST_TIME_ARGS (frame->pts));
1060 ret = klass->output_picture (decoder, frame, to_output);
1065 static GstFlowReturn
1066 gst_mpeg2_decoder_output_current_picture (GstMpeg2Decoder * decoder)
1068 GstMpeg2DecoderPrivate *priv = decoder->priv;
1069 GstMpeg2Picture *picture = priv->current_picture;
1070 GstFlowReturn ret = GST_FLOW_OK;
1072 if (!picture && priv->first_field) {
1073 GST_WARNING_OBJECT (decoder, "Missing the second field");
1074 picture = priv->first_field;
1079 /* Update the presentation time */
1080 priv->current_frame->pts = priv->current_pts;
1082 gst_mpeg2_dpb_add (priv->dpb, picture);
1084 GST_LOG_OBJECT (decoder,
1085 "Add picture %p (frame_num %d, poc %d, type 0x%x), into DPB", picture,
1086 picture->system_frame_number, picture->pic_order_cnt, picture->type);
1088 while (gst_mpeg2_dpb_need_bump (priv->dpb)) {
1089 GstMpeg2Picture *to_output;
1091 to_output = gst_mpeg2_dpb_bump (priv->dpb);
1092 g_assert (to_output);
1093 ret = gst_mpeg2_decoder_do_output_picture (decoder, to_output);
1094 if (ret != GST_FLOW_OK)
1101 static GstFlowReturn
1102 gst_mpeg2_decoder_handle_frame (GstVideoDecoder * decoder,
1103 GstVideoCodecFrame * frame)
1105 GstMpeg2Decoder *self = GST_MPEG2_DECODER (decoder);
1106 GstMpeg2DecoderPrivate *priv = self->priv;
1107 GstBuffer *in_buf = frame->input_buffer;
1108 GstMapInfo map_info;
1109 GstMpegVideoPacket packet;
1110 GstFlowReturn ret = GST_FLOW_OK;
1114 GST_LOG_OBJECT (self, "handle frame, PTS: %" GST_TIME_FORMAT
1115 ", DTS: %" GST_TIME_FORMAT " system frame number is %d",
1116 GST_TIME_ARGS (GST_BUFFER_PTS (in_buf)),
1117 GST_TIME_ARGS (GST_BUFFER_DTS (in_buf)), frame->system_frame_number);
1119 priv->state &= ~GST_MPEG2_DECODER_STATE_GOT_SLICE;
1121 priv->current_frame = frame;
1122 gst_buffer_map (in_buf, &map_info, GST_MAP_READ);
1126 while (gst_mpeg_video_parse (&packet, map_info.data, map_info.size, offset)) {
1127 /* The packet is the last one */
1128 if (packet.size == -1) {
1129 if (packet.offset < map_info.size) {
1130 packet.size = map_info.size - packet.offset;
1133 GST_WARNING_OBJECT (decoder, "Get a packet with wrong size");
1138 if (!gst_mpeg2_decoder_decode_packet (self, &packet)) {
1139 gst_buffer_unmap (in_buf, &map_info);
1140 GST_ERROR_OBJECT (decoder, "failed to handle the packet type 0x%x",
1148 offset = packet.offset;
1151 gst_buffer_unmap (in_buf, &map_info);
1153 if (!priv->current_picture) {
1154 GST_ERROR_OBJECT (decoder, "no valid picture created");
1158 if (!gst_mpeg2_decoder_finish_current_picture (self)) {
1159 GST_ERROR_OBJECT (decoder, "failed to decode the current picture");
1163 ret = gst_mpeg2_decoder_output_current_picture (self);
1164 gst_mpeg2_picture_clear (&priv->current_picture);
1165 gst_mpeg2_picture_clear (&priv->first_field);
1166 gst_video_codec_frame_unref (priv->current_frame);
1167 priv->current_frame = NULL;
1172 GST_VIDEO_DECODER_ERROR (decoder, 1, STREAM, DECODE,
1173 ("failed to handle the frame %d", frame->system_frame_number), (NULL),
1175 gst_video_decoder_drop_frame (decoder, frame);
1176 gst_mpeg2_picture_clear (&priv->current_picture);
1177 gst_mpeg2_picture_clear (&priv->first_field);
1178 priv->current_frame = NULL;