2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2007> Jan Schmidt <thaytan@mad.scientist.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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
26 #include "mpegvideoparse.h"
28 /* FIXME: there are still some things to do in this element.
29 * + Handle Sequence Display Extension to output the display size
30 * rather than the encoded size.
31 * + Do all the other stuff (documentation, tests) to get it into
34 * - handle seeking in raw elementary streams
35 * - calculate timestamps for all un-timestamped frames, taking into
36 * account frame re-ordering. Doing this probably requires introducing
37 * an extra end-to-end delay however, so might not be really desirable.
38 * - Collect a list of regions and the sequence headers that apply
39 * to each region so that we properly handle SEQUENCE_END followed
40 * by a new sequence. At the moment, the caps will change if the
41 * sequence changes, but if we then seek to a different spot it might
42 * be wrong. Fortunately almost every stream only has 1 sequence.
44 GST_DEBUG_CATEGORY (mpv_parse_debug);
45 #define GST_CAT_DEFAULT mpv_parse_debug
47 static GstStaticPadTemplate src_template =
48 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
50 GST_STATIC_CAPS ("video/mpeg, "
51 "mpegversion = (int) [ 1, 2 ], "
52 "parsed = (boolean) true, "
53 "systemstream = (boolean) false, "
54 "width = (int) [ 16, 4096 ], "
55 "height = (int) [ 16, 4096 ], "
56 "pixel-aspect-ratio = (fraction) [ 0/1, MAX ], "
57 "framerate = (fraction) [ 0/1, MAX ]")
60 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
63 GST_STATIC_CAPS ("video/mpeg, "
64 "mpegversion = (int) [ 1, 2 ], "
65 "parsed = (boolean) false, " "systemstream = (boolean) false")
68 /* MpegVideoParse signals and args */
81 static void gst_mpegvideoparse_class_init (MpegVideoParseClass * klass);
82 static void gst_mpegvideoparse_base_init (MpegVideoParseClass * klass);
83 static void gst_mpegvideoparse_init (MpegVideoParse * mpegvideoparse);
84 static void gst_mpegvideoparse_dispose (GObject * object);
86 static GstFlowReturn gst_mpegvideoparse_chain (GstPad * pad, GstBuffer * buf);
87 static gboolean mpv_parse_sink_event (GstPad * pad, GstEvent * event);
88 static void gst_mpegvideoparse_flush (MpegVideoParse * mpegvideoparse);
89 static GstStateChangeReturn
90 gst_mpegvideoparse_change_state (GstElement * element,
91 GstStateChange transition);
93 static void mpv_send_pending_segs (MpegVideoParse * mpegvideoparse);
94 static void mpv_clear_pending_segs (MpegVideoParse * mpegvideoparse);
96 static GstElementClass *parent_class = NULL;
98 /*static guint gst_mpegvideoparse_signals[LAST_SIGNAL] = { 0 }; */
101 mpegvideoparse_get_type (void)
103 static GType mpegvideoparse_type = 0;
105 if (!mpegvideoparse_type) {
106 static const GTypeInfo mpegvideoparse_info = {
107 sizeof (MpegVideoParseClass),
108 (GBaseInitFunc) gst_mpegvideoparse_base_init,
110 (GClassInitFunc) gst_mpegvideoparse_class_init,
113 sizeof (MpegVideoParse),
115 (GInstanceInitFunc) gst_mpegvideoparse_init,
118 mpegvideoparse_type =
119 g_type_register_static (GST_TYPE_ELEMENT, "MpegVideoParse",
120 &mpegvideoparse_info, 0);
122 return mpegvideoparse_type;
126 gst_mpegvideoparse_base_init (MpegVideoParseClass * klass)
128 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
130 gst_element_class_add_static_pad_template (element_class, &src_template);
131 gst_element_class_add_static_pad_template (element_class,
134 gst_element_class_set_details_simple (element_class,
135 "MPEG video elementary stream parser",
136 "Codec/Parser/Video",
137 "Parses and frames MPEG-1 and MPEG-2 elementary video streams",
138 "Wim Taymans <wim.taymans@chello.be>, "
139 "Jan Schmidt <thaytan@mad.scientist.com>");
143 gst_mpegvideoparse_class_init (MpegVideoParseClass * klass)
145 GObjectClass *gobject_class;
146 GstElementClass *gstelement_class;
148 gstelement_class = (GstElementClass *) klass;
149 gobject_class = G_OBJECT_CLASS (klass);
151 parent_class = g_type_class_peek_parent (klass);
153 gobject_class->dispose = (GObjectFinalizeFunc) (gst_mpegvideoparse_dispose);
154 gstelement_class->change_state = gst_mpegvideoparse_change_state;
158 mpv_parse_reset (MpegVideoParse * mpegvideoparse)
160 mpegvideoparse->seq_hdr.mpeg_version = 0;
161 mpegvideoparse->seq_hdr.width = mpegvideoparse->seq_hdr.height = -1;
162 mpegvideoparse->seq_hdr.fps_n = mpegvideoparse->seq_hdr.par_w = 0;
163 mpegvideoparse->seq_hdr.fps_d = mpegvideoparse->seq_hdr.par_h = 1;
165 mpv_clear_pending_segs (mpegvideoparse);
169 mpv_send_pending_segs (MpegVideoParse * mpegvideoparse)
171 while (mpegvideoparse->pending_segs) {
172 GstEvent *ev = mpegvideoparse->pending_segs->data;
174 gst_pad_push_event (mpegvideoparse->srcpad, ev);
176 mpegvideoparse->pending_segs =
177 g_list_delete_link (mpegvideoparse->pending_segs,
178 mpegvideoparse->pending_segs);
180 mpegvideoparse->pending_segs = NULL;
184 mpv_clear_pending_segs (MpegVideoParse * mpegvideoparse)
186 while (mpegvideoparse->pending_segs) {
187 GstEvent *ev = mpegvideoparse->pending_segs->data;
188 gst_event_unref (ev);
190 mpegvideoparse->pending_segs =
191 g_list_delete_link (mpegvideoparse->pending_segs,
192 mpegvideoparse->pending_segs);
197 gst_mpegvideoparse_init (MpegVideoParse * mpegvideoparse)
199 mpegvideoparse->sinkpad =
200 gst_pad_new_from_static_template (&sink_template, "sink");
201 gst_pad_set_chain_function (mpegvideoparse->sinkpad,
202 gst_mpegvideoparse_chain);
203 gst_pad_set_event_function (mpegvideoparse->sinkpad, mpv_parse_sink_event);
204 gst_element_add_pad (GST_ELEMENT (mpegvideoparse), mpegvideoparse->sinkpad);
206 mpegvideoparse->srcpad =
207 gst_pad_new_from_static_template (&src_template, "src");
208 gst_pad_use_fixed_caps (mpegvideoparse->srcpad);
209 gst_element_add_pad (GST_ELEMENT (mpegvideoparse), mpegvideoparse->srcpad);
211 mpeg_packetiser_init (&mpegvideoparse->packer);
213 mpv_parse_reset (mpegvideoparse);
217 gst_mpegvideoparse_dispose (GObject * object)
219 MpegVideoParse *mpegvideoparse = GST_MPEGVIDEOPARSE (object);
221 mpeg_packetiser_free (&mpegvideoparse->packer);
222 gst_buffer_replace (&mpegvideoparse->seq_hdr_buf, NULL);
224 G_OBJECT_CLASS (parent_class)->dispose (object);
228 mpegvideoparse_handle_sequence (MpegVideoParse * mpegvideoparse,
234 cur = GST_BUFFER_DATA (buf);
235 end = GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf);
237 memset (&new_hdr, 0, sizeof (MPEGSeqHdr));
239 if (G_UNLIKELY (!mpeg_util_parse_sequence_hdr (&new_hdr, cur, end)))
242 if (new_hdr.width < 16 || new_hdr.width > 4096 ||
243 new_hdr.height < 16 || new_hdr.height > 4096) {
244 GST_WARNING_OBJECT (mpegvideoparse, "Width/height out of valid range "
249 if (memcmp (&mpegvideoparse->seq_hdr, &new_hdr, sizeof (MPEGSeqHdr)) != 0) {
253 * Profile indication - 1 => High, 2 => Spatially Scalable,
254 * 3 => SNR Scalable, 4 => Main, 5 => Simple
255 * 4:2:2 and Multi-view have profile = 0, with the escape bit set to 1
257 const gchar *profiles[] = { "high", "spatial", "snr", "main", "simple" };
259 * Level indication - 4 => High, 6 => High-1440, 8 => Main, 10 => Low,
260 * except in the case of profile = 0
262 const gchar *levels[] = { "high", "high-1440", "main", "low" };
264 /* Store the entire sequence header + sequence header extension
265 for output as codec_data */
266 seq_buf = gst_buffer_copy (buf);
267 gst_buffer_replace (&mpegvideoparse->seq_hdr_buf, seq_buf);
268 gst_buffer_unref (seq_buf);
270 caps = gst_caps_new_simple ("video/mpeg",
271 "systemstream", G_TYPE_BOOLEAN, FALSE,
272 "parsed", G_TYPE_BOOLEAN, TRUE,
273 "mpegversion", G_TYPE_INT, new_hdr.mpeg_version,
274 "width", G_TYPE_INT, new_hdr.width,
275 "height", G_TYPE_INT, new_hdr.height,
276 "framerate", GST_TYPE_FRACTION, new_hdr.fps_n, new_hdr.fps_d,
277 "pixel-aspect-ratio", GST_TYPE_FRACTION, new_hdr.par_w, new_hdr.par_h,
278 "interlaced", G_TYPE_BOOLEAN, !new_hdr.progressive,
279 "codec_data", GST_TYPE_BUFFER, seq_buf, NULL);
281 if (new_hdr.mpeg_version == 2) {
282 const gchar *profile = NULL, *level = NULL;
284 if (new_hdr.profile > 0 && new_hdr.profile < 6)
285 profile = profiles[new_hdr.profile - 1];
287 if ((new_hdr.level > 3) && (new_hdr.level < 11) &&
288 (new_hdr.level % 2 == 0))
289 level = levels[(new_hdr.level >> 1) - 1];
291 if (new_hdr.profile == 8) {
292 /* Non-hierarchical profile */
293 switch (new_hdr.level) {
312 profile = "multiview";
320 gst_caps_set_simple (caps, "profile", G_TYPE_STRING, profile, NULL);
322 GST_DEBUG ("Invalid profile - %u", new_hdr.profile);
325 gst_caps_set_simple (caps, "level", G_TYPE_STRING, level, NULL);
327 GST_DEBUG ("Invalid level - %u", new_hdr.level);
330 GST_DEBUG ("New mpegvideoparse caps: %" GST_PTR_FORMAT, caps);
331 if (!gst_pad_set_caps (mpegvideoparse->srcpad, caps)) {
332 gst_caps_unref (caps);
335 gst_caps_unref (caps);
337 if (new_hdr.bitrate > 0) {
340 taglist = gst_tag_list_new_full (GST_TAG_BITRATE, new_hdr.bitrate, NULL);
341 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (mpegvideoparse),
342 mpegvideoparse->srcpad, taglist);
345 /* And update the new_hdr into our stored version */
346 mpegvideoparse->seq_hdr = new_hdr;
352 #ifndef GST_DISABLE_GST_DEBUG
354 picture_start_code_name (guint8 psc)
363 0x00, "Picture Start"}, {
366 0xb2, "User Data Start"}, {
367 0xb3, "Sequence Header Start"}, {
368 0xb4, "Sequence Error"}, {
369 0xb5, "Extnsion Start"}, {
371 0xb7, "Sequence End"}, {
372 0xb8, "Group Start"}, {
375 if (psc < 0xB0 && psc > 0)
376 return "Slice Start";
378 for (i = 0; i < G_N_ELEMENTS (psc_names); i++)
379 if (psc_names[i].psc == psc)
380 return psc_names[i].name;
386 picture_type_name (guint8 pct)
399 4, "DC Intra Coded (Shall Not Be Used!)"}
402 for (i = 0; i < G_N_ELEMENTS (pct_names); i++)
403 if (pct_names[i].pct == pct)
404 return pct_names[i].name;
406 return "Reserved/Unknown";
408 #endif /* GST_DISABLE_GST_DEBUG */
411 mpegvideoparse_handle_picture (MpegVideoParse * mpegvideoparse, GstBuffer * buf)
414 guint32 sync_word = 0xffffffff;
416 cur = GST_BUFFER_DATA (buf);
417 end = GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf);
419 cur = mpeg_util_find_start_code (&sync_word, cur, end);
420 while (cur != NULL) {
421 if (cur[0] == 0 || cur[0] > 0xaf)
422 GST_LOG_OBJECT (mpegvideoparse, "Picture Start Code : %s",
423 picture_start_code_name (cur[0]));
424 /* Cur points at the last byte of the start code */
425 if (cur[0] == MPEG_PACKET_PICTURE) {
426 guint8 *pic_data = cur - 3;
429 /* pic_data points to the first byte of the sync word now */
430 if (!mpeg_util_parse_picture_hdr (&hdr, pic_data, end))
433 if (hdr.pic_type != MPEG_PICTURE_TYPE_I)
434 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
436 GST_LOG_OBJECT (mpegvideoparse, "Picture type is %s",
437 picture_type_name (hdr.pic_type));
438 /* FIXME: Can use the picture type and number of fields to track a
442 cur = mpeg_util_find_start_code (&sync_word, cur, end);
450 gst_mpegvideoparse_time_code (guchar * gop, MPEGSeqHdr * seq_hdr)
452 guint32 data = GST_READ_UINT32_BE (gop);
456 seconds = ((data & 0xfc000000) >> 26) * 3600; /* hours */
457 seconds += ((data & 0x03f00000) >> 20) * 60; /* minutes */
458 seconds += (data & 0x0007e000) >> 13; /* seconds */
460 frames = (data & 0x00001f80) >> 7;
462 return seconds * GST_SECOND + gst_util_uint64_scale_int (frames * GST_SECOND,
463 seq_hdr->fps_d, seq_hdr->fps_n);
468 gst_mpegvideoparse_flush (MpegVideoParse * mpegvideoparse)
470 GST_DEBUG_OBJECT (mpegvideoparse, "mpegvideoparse: flushing");
472 mpegvideoparse->next_offset = GST_BUFFER_OFFSET_NONE;
474 g_list_foreach (mpegvideoparse->gather, (GFunc) gst_mini_object_unref, NULL);
475 g_list_free (mpegvideoparse->gather);
476 mpegvideoparse->gather = NULL;
477 g_list_foreach (mpegvideoparse->decode, (GFunc) gst_mini_object_unref, NULL);
478 g_list_free (mpegvideoparse->decode);
479 mpegvideoparse->decode = NULL;
480 mpeg_packetiser_flush (&mpegvideoparse->packer);
482 mpv_clear_pending_segs (mpegvideoparse);
486 mpegvideoparse_drain_avail (MpegVideoParse * mpegvideoparse)
489 GstBuffer *buf = NULL;
490 GstFlowReturn res = GST_FLOW_OK;
492 cur = mpeg_packetiser_get_block (&mpegvideoparse->packer, &buf);
493 while ((cur != NULL) && (res == GST_FLOW_OK)) {
494 /* Handle the block */
495 GST_LOG_OBJECT (mpegvideoparse,
496 "Have block of size %u with pack_type %s and flags 0x%02x",
497 cur->length, picture_start_code_name (cur->first_pack_type),
500 if ((cur->flags & MPEG_BLOCK_FLAG_SEQUENCE) && buf != NULL) {
501 /* Found a sequence header */
502 if (!mpegvideoparse_handle_sequence (mpegvideoparse, buf)) {
503 GST_DEBUG_OBJECT (mpegvideoparse,
504 "Invalid sequence header. Dropping buffer.");
505 gst_buffer_unref (buf);
508 } else if (mpegvideoparse->seq_hdr.mpeg_version == 0 && buf) {
509 /* Don't start pushing out buffers until we've seen a sequence header */
510 GST_DEBUG_OBJECT (mpegvideoparse,
511 "No sequence header yet. Dropping buffer of %u bytes",
512 GST_BUFFER_SIZE (buf));
513 gst_buffer_unref (buf);
518 /* If outputting a PICTURE packet, we can calculate the duration
519 and possibly the timestamp */
520 if (cur->flags & MPEG_BLOCK_FLAG_PICTURE) {
521 if (!mpegvideoparse_handle_picture (mpegvideoparse, buf)) {
522 /* Corrupted picture. Drop it. */
523 GST_DEBUG_OBJECT (mpegvideoparse,
524 "Corrupted picture header. Dropping buffer of %u bytes",
525 GST_BUFFER_SIZE (buf));
526 mpegvideoparse->need_discont = TRUE;
527 gst_buffer_unref (buf);
534 GST_DEBUG_OBJECT (mpegvideoparse,
535 "mpegvideoparse: pushing buffer of %u bytes with ts %"
536 GST_TIME_FORMAT, GST_BUFFER_SIZE (buf),
537 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
539 gst_buffer_set_caps (buf, GST_PAD_CAPS (mpegvideoparse->srcpad));
541 if (mpegvideoparse->need_discont) {
542 GST_DEBUG_OBJECT (mpegvideoparse,
543 "setting discont flag on outgoing buffer");
544 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
545 mpegvideoparse->need_discont = FALSE;
548 mpv_send_pending_segs (mpegvideoparse);
550 res = gst_pad_push (mpegvideoparse->srcpad, buf);
554 /* Advance to the next data block */
555 mpeg_packetiser_next_block (&mpegvideoparse->packer);
556 cur = mpeg_packetiser_get_block (&mpegvideoparse->packer, &buf);
559 gst_buffer_unref (buf);
565 gst_mpegvideoparse_chain_forward (MpegVideoParse * mpegvideoparse,
566 gboolean discont, GstBuffer * buf)
569 guint64 next_offset = GST_BUFFER_OFFSET_NONE;
571 GST_DEBUG_OBJECT (mpegvideoparse,
572 "mpegvideoparse: received buffer of %u bytes with ts %"
573 GST_TIME_FORMAT " and offset %" G_GINT64_FORMAT, GST_BUFFER_SIZE (buf),
574 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), GST_BUFFER_OFFSET (buf));
576 /* If we have an offset, and the incoming offset doesn't match,
577 or we have a discont, handle it first by flushing out data
578 we have collected. */
579 if (mpegvideoparse->next_offset != GST_BUFFER_OFFSET_NONE) {
580 if (GST_BUFFER_OFFSET_IS_VALID (buf)) {
581 if (mpegvideoparse->next_offset != GST_BUFFER_OFFSET (buf))
583 next_offset = GST_BUFFER_OFFSET (buf) + GST_BUFFER_SIZE (buf);
585 next_offset = mpegvideoparse->next_offset + GST_BUFFER_SIZE (buf);
589 /* Clear out any existing stuff if the new buffer is discontinuous */
591 GST_DEBUG_OBJECT (mpegvideoparse, "Have discont packet, draining data");
592 mpegvideoparse->need_discont = TRUE;
594 mpeg_packetiser_handle_eos (&mpegvideoparse->packer);
595 res = mpegvideoparse_drain_avail (mpegvideoparse);
596 mpeg_packetiser_flush (&mpegvideoparse->packer);
597 if (res != GST_FLOW_OK) {
598 gst_buffer_unref (buf);
603 /* Takes ownership of the data */
604 mpeg_packetiser_add_buf (&mpegvideoparse->packer, buf);
606 /* And push out what we can */
607 res = mpegvideoparse_drain_avail (mpegvideoparse);
610 /* Update our offset */
611 mpegvideoparse->next_offset = next_offset;
616 /* scan the decode queue for a picture header with an I frame and return the
617 * index in the first buffer. We only scan the first buffer and possibly a
618 * couple of bytes of the next buffers to find the I frame. Scanning is done
619 * backwards because the first buffer could contain many picture start codes
622 scan_keyframe (MpegVideoParse * mpegvideoparse)
631 /* we use an 8 byte buffer, this is enough to hold the picture start code and
632 * the picture header bits we are interested in. We init to 0xff so that when
633 * we have a valid picture start without the header bits, we will be able to
634 * detect this because it will generate an invalid picture type. */
635 scanword = ~G_GUINT64_CONSTANT (0);
637 GST_LOG_OBJECT (mpegvideoparse, "scan keyframe");
639 /* move to the second buffer if we can, we should have at least one buffer */
640 walk = mpegvideoparse->decode;
641 g_return_val_if_fail (walk != NULL, -1);
643 head = GST_BUFFER_CAST (walk->data);
646 walk = g_list_next (walk);
648 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
650 data = GST_BUFFER_DATA (buf);
651 size = GST_BUFFER_SIZE (buf);
653 GST_LOG_OBJECT (mpegvideoparse, "collect remaining %d bytes from %p",
656 while (size > 0 && count < 6) {
657 scanword = (scanword << 8) | *data++;
664 walk = g_list_next (walk);
666 /* move bits to the beginning of the word now */
668 scanword = (scanword << (8 * (8 - count)));
670 GST_LOG_OBJECT (mpegvideoparse, "scanword 0x%016" G_GINT64_MODIFIER "x",
673 data = GST_BUFFER_DATA (head);
674 size = GST_BUFFER_SIZE (head);
677 scanword = (((guint64) data[size - 1]) << 56) | (scanword >> 8);
679 GST_LOG_OBJECT (mpegvideoparse,
680 "scanword at %d 0x%016" G_GINT64_MODIFIER "x", size - 1, scanword);
682 /* check picture start and picture type */
683 if ((scanword & G_GUINT64_CONSTANT (0xffffffff00380000)) ==
684 G_GUINT64_CONSTANT (0x0000010000080000))
692 /* For reverse playback we use a technique that can be used for
693 * any keyframe based video codec.
696 * Buffer decoding order: 7 8 9 4 5 6 1 2 3 EOS
698 * Discont flag: D D D
700 * - Each Discont marks a discont in the decoding order.
701 * - The keyframes mark where we can start decoding. For mpeg they are either
702 * set by the demuxer or we have to scan the buffers for a syncword and
703 * picture header with an I frame.
705 * First we prepend incomming buffers to the gather queue, whenever we receive
706 * a discont, we flush out the gather queue.
708 * The above data will be accumulated in the gather queue like this:
710 * gather queue: 9 8 7
713 * Whe buffer 4 is received (with a DISCONT), we flush the gather queue like
717 * take head of queue and prepend to decode queue.
718 * if we copied a keyframe, decode the decode queue.
720 * After we flushed the gather queue, we add 4 to the (now empty) gather queue.
721 * We get the following situation:
724 * decode queue: 7 8 9
726 * After we received 5 (Keyframe) and 6:
728 * gather queue: 6 5 4
729 * decode queue: 7 8 9
731 * When we receive 1 (DISCONT) which triggers a flush of the gather queue:
733 * Copy head of the gather queue (6) to decode queue:
736 * decode queue: 6 7 8 9
738 * Copy head of the gather queue (5) to decode queue. This is a keyframe so we
739 * can start decoding.
742 * decode queue: 5 6 7 8 9
744 * Decode frames in decode queue, we don't actually do the decoding but we
745 * will send the decode queue to the downstream element. This will empty the
746 * decoding queue again.
748 * Copy head of the gather queue (4) to decode queue, we flushed the gather
749 * queue and can now store input buffer in the gather queue:
754 * When we receive EOS, the queue looks like:
756 * gather queue: 3 2 1
759 * Fill decode queue, first keyframe we copy is 2:
762 * decode queue: 2 3 4
764 * After pushing the decode queue:
769 * Leftover buffer 1 cannot be decoded and must be discarded.
772 gst_mpegvideoparse_flush_decode (MpegVideoParse * mpegvideoparse, guint idx)
774 GstFlowReturn res = GST_FLOW_OK;
775 GstBuffer *head = NULL;
777 while (mpegvideoparse->decode) {
780 buf = GST_BUFFER_CAST (mpegvideoparse->decode->data);
786 /* first buffer, split at the point where the picture start was
787 * detected and store as the new head of the decoding list. */
788 head = gst_buffer_create_sub (buf, 0, idx);
789 /* push the remainder after the picture sync point downstream, this is the
790 * first DISCONT buffer we push. */
791 temp = gst_buffer_create_sub (buf, idx, GST_BUFFER_SIZE (buf) - idx);
792 /* don't need old buffer anymore and swap new buffer */
793 gst_buffer_unref (buf);
796 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
799 /* next buffers are not discont */
800 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
803 GST_DEBUG_OBJECT (mpegvideoparse, "pushing buffer %p, ts %" GST_TIME_FORMAT,
804 buf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
806 res = gst_pad_push (mpegvideoparse->srcpad, buf);
808 mpegvideoparse->decode =
809 g_list_delete_link (mpegvideoparse->decode, mpegvideoparse->decode);
812 /* store remainder of the buffer */
813 mpegvideoparse->decode = g_list_prepend (mpegvideoparse->decode, head);
819 gst_mpegvideoparse_chain_reverse (MpegVideoParse * mpegvideoparse,
820 gboolean discont, GstBuffer * buf)
822 GstFlowReturn res = GST_FLOW_OK;
824 /* if we have a discont, move buffers to the decode list */
825 if (G_UNLIKELY (discont)) {
826 GST_DEBUG_OBJECT (mpegvideoparse, "received discont,gathering buffers");
828 while (mpegvideoparse->gather) {
832 gbuf = GST_BUFFER_CAST (mpegvideoparse->gather->data);
833 /* remove from the gather list */
834 mpegvideoparse->gather =
835 g_list_delete_link (mpegvideoparse->gather, mpegvideoparse->gather);
836 /* copy to decode queue */
837 mpegvideoparse->decode = g_list_prepend (mpegvideoparse->decode, gbuf);
839 GST_DEBUG_OBJECT (mpegvideoparse, "copied decoding buffer %p, len %d",
840 gbuf, g_list_length (mpegvideoparse->decode));
842 /* check if we copied a keyframe, we scan the buffers on the decode queue.
843 * We only need to scan the first buffer and at most 3 bytes of the second
844 * buffer. We return the index of the keyframe (or -1 when nothing was
846 while ((keyframeidx = scan_keyframe (mpegvideoparse)) != -1) {
847 GST_DEBUG_OBJECT (mpegvideoparse, "copied keyframe at %u", keyframeidx);
848 res = gst_mpegvideoparse_flush_decode (mpegvideoparse, keyframeidx);
854 /* add buffer to gather queue */
855 GST_DEBUG_OBJECT (mpegvideoparse, "gathering buffer %p, size %u", buf,
856 GST_BUFFER_SIZE (buf));
857 mpegvideoparse->gather = g_list_prepend (mpegvideoparse->gather, buf);
864 gst_mpegvideoparse_chain (GstPad * pad, GstBuffer * buf)
866 MpegVideoParse *mpegvideoparse;
871 GST_MPEGVIDEOPARSE (gst_object_get_parent (GST_OBJECT (pad)));
873 discont = GST_BUFFER_IS_DISCONT (buf);
875 if (mpegvideoparse->segment.rate > 0.0)
876 res = gst_mpegvideoparse_chain_forward (mpegvideoparse, discont, buf);
878 res = gst_mpegvideoparse_chain_reverse (mpegvideoparse, discont, buf);
880 gst_object_unref (mpegvideoparse);
886 mpv_parse_sink_event (GstPad * pad, GstEvent * event)
889 MpegVideoParse *mpegvideoparse =
890 GST_MPEGVIDEOPARSE (gst_pad_get_parent (pad));
892 switch (GST_EVENT_TYPE (event)) {
893 case GST_EVENT_NEWSEGMENT:
895 gdouble rate, applied_rate;
897 gint64 start, stop, pos;
900 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
901 &format, &start, &stop, &pos);
903 if (format == GST_FORMAT_BYTES) {
904 /* FIXME: Later, we might use a seek table to seek on elementary stream
905 files, and that would allow byte-to-time conversions. It's not a high
906 priority - most mpeg video is muxed and then the demuxer handles
907 seeking. In the meantime, here's some commented out logic copied
910 GstClockTime seg_start, seg_stop, seg_pos;
912 /* stop time is allowed to be open-ended, but not start & pos */
913 if (!mp3parse_bytepos_to_time (mp3parse, stop, &seg_stop))
914 seg_stop = GST_CLOCK_TIME_NONE;
915 if (mp3parse_bytepos_to_time (mp3parse, start, &seg_start) &&
916 mp3parse_bytepos_to_time (mp3parse, pos, &seg_pos)) {
917 gst_event_unref (event);
918 event = gst_event_new_new_segment_full (update, rate, applied_rate,
919 GST_FORMAT_TIME, seg_start, seg_stop, seg_pos);
920 format = GST_FORMAT_TIME;
921 GST_DEBUG_OBJECT (mp3parse, "Converted incoming segment to TIME. "
922 "start = %" G_GINT64_FORMAT ", stop = %" G_GINT64_FORMAT
923 "pos = %" G_GINT64_FORMAT, seg_start, seg_stop, seg_pos);
928 if (format != GST_FORMAT_TIME) {
929 /* Unknown incoming segment format. Output a default open-ended
931 gst_event_unref (event);
934 format = GST_FORMAT_TIME;
936 stop = GST_CLOCK_TIME_NONE;
938 /* create a new segment with these values */
939 event = gst_event_new_new_segment_full (update, rate, applied_rate,
940 format, start, stop, pos);
943 gst_mpegvideoparse_flush (mpegvideoparse);
945 /* now configure the values */
946 gst_segment_set_newsegment_full (&mpegvideoparse->segment, update,
947 rate, applied_rate, format, start, stop, pos);
949 GST_DEBUG_OBJECT (mpegvideoparse,
950 "Pushing newseg rate %g, applied rate %g, format %d, start %"
951 G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT ", pos %" G_GINT64_FORMAT,
952 rate, applied_rate, format, start, stop, pos);
954 /* Forward the event if we've seen a sequence header
955 * and therefore set output caps, otherwise queue it for later */
956 if (mpegvideoparse->seq_hdr.mpeg_version != 0)
957 res = gst_pad_push_event (mpegvideoparse->srcpad, event);
960 mpegvideoparse->pending_segs =
961 g_list_append (mpegvideoparse->pending_segs, event);
965 case GST_EVENT_FLUSH_STOP:
966 GST_DEBUG_OBJECT (mpegvideoparse, "flush stop");
967 gst_mpegvideoparse_flush (mpegvideoparse);
968 res = gst_pad_push_event (mpegvideoparse->srcpad, event);
971 /* Push any remaining buffers out, then flush. */
972 GST_DEBUG_OBJECT (mpegvideoparse, "received EOS");
973 if (mpegvideoparse->segment.rate >= 0.0) {
974 mpeg_packetiser_handle_eos (&mpegvideoparse->packer);
975 mpegvideoparse_drain_avail (mpegvideoparse);
976 gst_mpegvideoparse_flush (mpegvideoparse);
978 gst_mpegvideoparse_chain_reverse (mpegvideoparse, TRUE, NULL);
979 gst_mpegvideoparse_flush_decode (mpegvideoparse, 0);
981 res = gst_pad_push_event (mpegvideoparse->srcpad, event);
984 res = gst_pad_push_event (mpegvideoparse->srcpad, event);
988 gst_object_unref (mpegvideoparse);
992 static GstStateChangeReturn
993 gst_mpegvideoparse_change_state (GstElement * element,
994 GstStateChange transition)
996 MpegVideoParse *mpegvideoparse;
997 GstStateChangeReturn ret;
999 g_return_val_if_fail (GST_IS_MPEGVIDEOPARSE (element),
1000 GST_STATE_CHANGE_FAILURE);
1002 mpegvideoparse = GST_MPEGVIDEOPARSE (element);
1004 switch (transition) {
1005 case GST_STATE_CHANGE_READY_TO_PAUSED:
1006 gst_segment_init (&mpegvideoparse->segment, GST_FORMAT_UNDEFINED);
1012 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1014 switch (transition) {
1015 case GST_STATE_CHANGE_PAUSED_TO_READY:
1016 mpv_parse_reset (mpegvideoparse);
1017 gst_mpegvideoparse_flush (mpegvideoparse);
1027 plugin_init (GstPlugin * plugin)
1029 GST_DEBUG_CATEGORY_INIT (mpv_parse_debug, "legacympegvideoparse", 0,
1030 "MPEG Video Parser");
1032 return gst_element_register (plugin, "legacympegvideoparse",
1033 GST_RANK_NONE, GST_TYPE_MPEGVIDEOPARSE);
1036 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1039 "MPEG-1 and MPEG-2 video parser",
1040 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)