2 * Copyright (C) <1999> Erik Walthinsen <omega@temple-baptist.com>
3 * Copyright (C) <2006> Nokia Corporation (contact <stefan.kost@nokia.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.
20 /* Element-Checklist-Version: 5 */
23 * SECTION:element-avidemux
25 * Demuxes an .avi file into raw or compressed audio and/or video streams.
27 * This element supports both push and pull-based scheduling, depending on the
28 * capabilities of the upstream elements.
31 * <title>Example launch line</title>
33 * gst-launch filesrc location=test.avi ! avidemux name=demux demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink demux.video_00 ! queue ! decodebin ! ffmpegcolorspace ! videoscale ! autovideosink
34 * ]| Play (parse and decode) an .avi file and try to output it to
35 * an automatically detected soundcard and videosink. If the AVI file contains
36 * compressed audio or video data, this will only work if you have the
37 * right decoder elements/plugins installed.
40 * Last reviewed on 2006-12-29 (0.10.6)
49 #include "gst/riff/riff-media.h"
50 #include "gstavidemux.h"
52 #include <gst/gst-i18n-plugin.h>
53 #include <gst/base/gstadapter.h>
56 #define DIV_ROUND_UP(s,v) (((s) + ((v)-1)) / (v))
58 GST_DEBUG_CATEGORY_STATIC (avidemux_debug);
59 #define GST_CAT_DEFAULT avidemux_debug
61 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
64 GST_STATIC_CAPS ("video/x-msvideo")
67 static void gst_avi_demux_base_init (GstAviDemuxClass * klass);
68 static void gst_avi_demux_class_init (GstAviDemuxClass * klass);
69 static void gst_avi_demux_init (GstAviDemux * avi);
70 static void gst_avi_demux_finalize (GObject * object);
72 static void gst_avi_demux_reset (GstAviDemux * avi);
75 static const GstEventMask *gst_avi_demux_get_event_mask (GstPad * pad);
77 static gboolean gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event);
78 static gboolean gst_avi_demux_handle_sink_event (GstPad * pad,
80 static gboolean gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event);
83 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
85 static const GstQueryType *gst_avi_demux_get_src_query_types (GstPad * pad);
86 static gboolean gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query);
87 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
88 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
90 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
91 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
93 static void gst_avi_demux_loop (GstPad * pad);
94 static gboolean gst_avi_demux_sink_activate (GstPad * sinkpad);
95 static gboolean gst_avi_demux_sink_activate_pull (GstPad * sinkpad,
97 static gboolean gst_avi_demux_activate_push (GstPad * pad, gboolean active);
98 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstBuffer * buf);
100 static GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
101 GstStateChange transition);
103 static GstElementClass *parent_class = NULL;
105 /* GObject methods */
108 gst_avi_demux_get_type (void)
110 static GType avi_demux_type = 0;
112 if (!avi_demux_type) {
113 static const GTypeInfo avi_demux_info = {
114 sizeof (GstAviDemuxClass),
115 (GBaseInitFunc) gst_avi_demux_base_init,
117 (GClassInitFunc) gst_avi_demux_class_init,
120 sizeof (GstAviDemux),
122 (GInstanceInitFunc) gst_avi_demux_init,
126 g_type_register_static (GST_TYPE_ELEMENT,
127 "GstAviDemux", &avi_demux_info, 0);
130 return avi_demux_type;
134 gst_avi_demux_base_init (GstAviDemuxClass * klass)
136 static const GstElementDetails gst_avi_demux_details =
137 GST_ELEMENT_DETAILS ("Avi demuxer",
139 "Demultiplex an avi file into audio and video",
140 "Erik Walthinsen <omega@cse.ogi.edu>\n"
141 "Wim Taymans <wim.taymans@chello.be>\n"
142 "Thijs Vermeir <thijsvermeir@gmail.com>");
143 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
144 GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl;
145 GstCaps *audcaps, *vidcaps, *subcaps;
147 audcaps = gst_riff_create_audio_template_caps ();
148 gst_caps_append (audcaps, gst_caps_new_simple ("audio/x-avi-unknown", NULL));
149 audiosrctempl = gst_pad_template_new ("audio_%02d",
150 GST_PAD_SRC, GST_PAD_SOMETIMES, audcaps);
152 vidcaps = gst_riff_create_video_template_caps ();
153 gst_caps_append (vidcaps, gst_riff_create_iavs_template_caps ());
154 gst_caps_append (vidcaps, gst_caps_new_simple ("video/x-avi-unknown", NULL));
155 videosrctempl = gst_pad_template_new ("video_%02d",
156 GST_PAD_SRC, GST_PAD_SOMETIMES, vidcaps);
158 subcaps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
159 subsrctempl = gst_pad_template_new ("subtitle_%02d",
160 GST_PAD_SRC, GST_PAD_SOMETIMES, subcaps);
161 gst_element_class_add_pad_template (element_class, audiosrctempl);
162 gst_element_class_add_pad_template (element_class, videosrctempl);
163 gst_element_class_add_pad_template (element_class, subsrctempl);
164 gst_element_class_add_pad_template (element_class,
165 gst_static_pad_template_get (&sink_templ));
166 gst_element_class_set_details (element_class, &gst_avi_demux_details);
170 gst_avi_demux_class_init (GstAviDemuxClass * klass)
172 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
173 GObjectClass *gobject_class = (GObjectClass *) klass;
175 GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
176 0, "Demuxer for AVI streams");
178 parent_class = g_type_class_peek_parent (klass);
180 gobject_class->finalize = gst_avi_demux_finalize;
181 gstelement_class->change_state =
182 GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
186 gst_avi_demux_init (GstAviDemux * avi)
188 avi->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
189 gst_pad_set_activate_function (avi->sinkpad,
190 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate));
191 gst_pad_set_activatepull_function (avi->sinkpad,
192 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate_pull));
193 gst_pad_set_activatepush_function (avi->sinkpad,
194 GST_DEBUG_FUNCPTR (gst_avi_demux_activate_push));
195 gst_pad_set_chain_function (avi->sinkpad,
196 GST_DEBUG_FUNCPTR (gst_avi_demux_chain));
197 gst_pad_set_event_function (avi->sinkpad,
198 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_sink_event));
199 gst_element_add_pad (GST_ELEMENT (avi), avi->sinkpad);
201 avi->adapter = gst_adapter_new ();
203 gst_avi_demux_reset (avi);
207 gst_avi_demux_finalize (GObject * object)
209 GstAviDemux *avi = GST_AVI_DEMUX (object);
211 GST_DEBUG ("AVI: finalize");
213 g_object_unref (avi->adapter);
215 G_OBJECT_CLASS (parent_class)->finalize (object);
219 gst_avi_demux_reset_stream (GstAviDemux * avi, GstAviStream * stream)
221 g_free (stream->strh);
222 g_free (stream->strf.data);
223 g_free (stream->name);
224 g_free (stream->index);
225 g_free (stream->indexes);
226 if (stream->initdata)
227 gst_buffer_unref (stream->initdata);
228 if (stream->extradata)
229 gst_buffer_unref (stream->extradata);
231 gst_pad_set_active (stream->pad, FALSE);
232 gst_element_remove_pad (GST_ELEMENT (avi), stream->pad);
234 if (stream->taglist) {
235 gst_tag_list_free (stream->taglist);
236 stream->taglist = NULL;
238 memset (stream, 0, sizeof (GstAviStream));
242 gst_avi_demux_reset (GstAviDemux * avi)
246 GST_DEBUG ("AVI: reset");
248 for (i = 0; i < avi->num_streams; i++)
249 gst_avi_demux_reset_stream (avi, &avi->stream[i]);
251 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
252 avi->num_streams = 0;
253 avi->num_v_streams = 0;
254 avi->num_a_streams = 0;
255 avi->num_t_streams = 0;
257 avi->state = GST_AVI_DEMUX_START;
260 avi->index_offset = 0;
264 if (avi->seek_event) {
265 gst_event_unref (avi->seek_event);
266 avi->seek_event = NULL;
270 gst_tag_list_free (avi->globaltags);
271 avi->globaltags = NULL;
273 avi->got_tags = TRUE; /* we always want to push global tags */
274 avi->have_eos = FALSE;
276 gst_adapter_clear (avi->adapter);
278 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
282 /* GstElement methods */
285 static const GstFormat *
286 gst_avi_demux_get_src_formats (GstPad * pad)
288 GstAviStream *stream = gst_pad_get_element_private (pad);
290 static const GstFormat src_a_formats[] = {
296 static const GstFormat src_v_formats[] = {
302 return (stream->strh->type == GST_RIFF_FCC_auds ?
303 src_a_formats : src_v_formats);
307 /* assumes stream->strf.auds->av_bps != 0 */
308 static inline GstClockTime
309 avi_stream_convert_bytes_to_time_unchecked (GstAviStream * stream,
312 return gst_util_uint64_scale_int (bytes, GST_SECOND,
313 stream->strf.auds->av_bps);
316 static inline guint64
317 avi_stream_convert_time_to_bytes_unchecked (GstAviStream * stream,
320 return gst_util_uint64_scale_int (time, stream->strf.auds->av_bps,
324 /* assumes stream->strh->rate != 0 */
325 static inline GstClockTime
326 avi_stream_convert_frames_to_time_unchecked (GstAviStream * stream,
329 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
333 static inline guint64
334 avi_stream_convert_time_to_frames_unchecked (GstAviStream * stream,
337 return gst_util_uint64_scale (time, stream->strh->rate,
338 stream->strh->scale * GST_SECOND);
342 gst_avi_demux_src_convert (GstPad * pad,
343 GstFormat src_format,
344 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
346 GstAviStream *stream = gst_pad_get_element_private (pad);
350 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
351 ", dest_format:%s", gst_format_get_name (src_format), src_value,
352 gst_format_get_name (*dest_format));
354 if (G_UNLIKELY (src_format == *dest_format)) {
355 *dest_value = src_value;
358 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
362 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
363 (src_format == GST_FORMAT_BYTES
364 || *dest_format == GST_FORMAT_BYTES))) {
369 switch (src_format) {
370 case GST_FORMAT_TIME:
371 switch (*dest_format) {
372 case GST_FORMAT_BYTES:
373 *dest_value = gst_util_uint64_scale_int (src_value,
374 stream->strf.auds->av_bps, GST_SECOND);
376 case GST_FORMAT_DEFAULT:
378 gst_util_uint64_scale_round (src_value, stream->strh->rate,
379 stream->strh->scale * GST_SECOND);
386 case GST_FORMAT_BYTES:
387 switch (*dest_format) {
388 case GST_FORMAT_TIME:
389 if (stream->strf.auds->av_bps != 0) {
390 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
400 case GST_FORMAT_DEFAULT:
401 switch (*dest_format) {
402 case GST_FORMAT_TIME:
404 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
417 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
418 gst_format_get_name (*dest_format), *dest_value);
422 static const GstQueryType *
423 gst_avi_demux_get_src_query_types (GstPad * pad)
425 static const GstQueryType src_types[] = {
437 gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query)
440 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
442 GstAviStream *stream = gst_pad_get_element_private (pad);
444 if (!stream->strh || !stream->strf.data)
445 return gst_pad_query_default (pad, query);
447 switch (GST_QUERY_TYPE (query)) {
448 case GST_QUERY_POSITION:{
451 GST_DEBUG ("pos query for stream %u: frames %u, bytes %u",
452 stream->num, stream->current_entry, stream->current_total);
454 /* FIXME, this looks clumsy */
455 if (stream->strh->type == GST_RIFF_FCC_auds) {
456 if (stream->is_vbr) {
458 pos = gst_util_uint64_scale ((gint64) stream->current_entry *
459 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
460 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
461 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
462 } else if (stream->strf.auds->av_bps != 0) {
464 pos = gst_util_uint64_scale (stream->current_total, GST_SECOND,
465 (guint64) stream->strf.auds->av_bps);
466 GST_DEBUG_OBJECT (avi,
467 "CBR convert bytes %u, time %" GST_TIME_FORMAT,
468 stream->current_total, GST_TIME_ARGS (pos));
469 } else if (stream->idx_n != 0 && stream->total_bytes != 0) {
470 /* calculate timestamps based on percentage of length */
471 guint64 xlen = avi->avih->us_frame *
472 avi->avih->tot_frames * GST_USECOND;
474 if (stream->is_vbr) {
475 pos = gst_util_uint64_scale (xlen, stream->current_entry,
477 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
478 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
480 pos = gst_util_uint64_scale (xlen, stream->current_total,
481 stream->total_bytes);
482 GST_DEBUG_OBJECT (avi,
483 "CBR perc convert bytes %u, time %" GST_TIME_FORMAT,
484 stream->current_total, GST_TIME_ARGS (pos));
491 if (stream->strh->rate != 0) {
492 pos = gst_util_uint64_scale ((guint64) stream->current_entry *
493 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
495 pos = stream->current_entry * avi->avih->us_frame * GST_USECOND;
499 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
500 gst_query_set_position (query, GST_FORMAT_TIME, pos);
502 GST_WARNING ("pos query failed");
505 case GST_QUERY_DURATION:
509 if (stream->strh->type != GST_RIFF_FCC_auds &&
510 stream->strh->type != GST_RIFF_FCC_vids) {
515 gst_query_parse_duration (query, &fmt, NULL);
518 case GST_FORMAT_TIME:
519 gst_query_set_duration (query, fmt, stream->duration);
521 case GST_FORMAT_DEFAULT:
524 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
527 if (stream->idx_n >= 0)
528 gst_query_set_duration (query, fmt, stream->idx_n);
529 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
530 stream->duration, &fmt, &dur))
531 gst_query_set_duration (query, fmt, dur);
540 case GST_QUERY_SEEKING:{
543 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
544 if (fmt == GST_FORMAT_TIME) {
545 gboolean seekable = TRUE;
547 if (avi->streaming) {
551 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
552 0, stream->duration);
557 case GST_QUERY_CONVERT:{
558 GstFormat src_fmt, dest_fmt;
559 gint64 src_val, dest_val;
561 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
562 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
564 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
566 res = gst_pad_query_default (pad, query);
570 res = gst_pad_query_default (pad, query);
574 gst_object_unref (avi);
579 static const GstEventMask *
580 gst_avi_demux_get_event_mask (GstPad * pad)
582 static const GstEventMask masks[] = {
583 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
592 gst_avi_demux_handle_sink_event (GstPad * pad, GstEvent * event)
595 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
597 GST_DEBUG_OBJECT (avi,
598 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
600 switch (GST_EVENT_TYPE (event)) {
601 case GST_EVENT_NEWSEGMENT:
602 /* Drop NEWSEGMENT events, new ones are generated later */
603 gst_event_unref (event);
607 if (avi->state != GST_AVI_DEMUX_MOVI) {
608 gst_event_unref (event);
609 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
610 (NULL), ("got eos and didn't receive a complete header object"));
611 } else if (!gst_avi_demux_push_event (avi, event)) {
612 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
613 (NULL), ("got eos but no streams (yet)"));
618 res = gst_pad_event_default (pad, event);
622 gst_object_unref (avi);
628 gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event)
631 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
633 GST_DEBUG_OBJECT (avi,
634 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
636 switch (GST_EVENT_TYPE (event)) {
638 /* handle seeking only in pull mode */
639 if (!avi->streaming) {
640 res = gst_avi_demux_handle_seek (avi, pad, event);
641 gst_event_unref (event);
643 res = gst_pad_event_default (pad, event);
647 case GST_EVENT_NAVIGATION:
649 gst_event_unref (event);
652 res = gst_pad_event_default (pad, event);
656 gst_object_unref (avi);
661 /* streaming helper (push) */
664 * gst_avi_demux_peek_chunk_info:
666 * @tag: holder for tag
667 * @size: holder for tag size
669 * Peek next chunk info (tag and size)
671 * Returns: TRUE when one chunk info has been got
674 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
676 const guint8 *data = NULL;
678 if (gst_adapter_available (avi->adapter) < 8)
681 data = gst_adapter_peek (avi->adapter, 8);
682 *tag = GST_READ_UINT32_LE (data);
683 *size = GST_READ_UINT32_LE (data + 4);
689 * gst_avi_demux_peek_chunk:
691 * @tag: holder for tag
692 * @size: holder for tag size
694 * Peek enough data for one full chunk
696 * Returns: %TRUE when one chunk has been got
699 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
701 guint32 peek_size = 0;
704 if (!gst_avi_demux_peek_chunk_info (avi, tag, size))
707 /* size 0 -> empty data buffer would surprise most callers,
708 * large size -> do not bother trying to squeeze that into adapter,
709 * so we throw poor man's exception, which can be caught if caller really
710 * wants to handle 0 size chunk */
711 if (!(*size) || (*size) >= (1 << 30))
714 peek_size = (*size + 1) & ~1;
715 available = gst_adapter_available (avi->adapter);
717 GST_DEBUG_OBJECT (avi,
718 "Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
719 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
721 if (available < (8 + peek_size))
729 GST_INFO_OBJECT (avi, "Failed to peek");
734 GST_INFO_OBJECT (avi,
735 "Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT, *size,
736 GST_FOURCC_ARGS (*tag));
737 /* chain should give up */
738 avi->abort_buffering = TRUE;
743 GST_INFO_OBJECT (avi, "need more %d < %" G_GUINT32_FORMAT,
744 available, 8 + peek_size);
752 * gst_avi_demux_parse_file_header:
753 * @element: caller element (used for errors/debug).
754 * @buf: input data to be used for parsing.
756 * "Open" a RIFF/AVI file. The buffer should be at least 12
757 * bytes long. Takes ownership of @buf.
759 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
760 * Throws an error, caller should error out (fatal).
763 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
768 stamp = gst_util_get_timestamp ();
770 /* riff_parse posts an error */
771 if (!gst_riff_parse_file_header (element, buf, &doctype))
774 if (doctype != GST_RIFF_RIFF_AVI)
777 stamp = gst_util_get_timestamp () - stamp;
778 GST_DEBUG_OBJECT (element, "parsing header %" GST_TIME_FORMAT,
779 GST_TIME_ARGS (stamp));
786 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
787 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
788 GST_FOURCC_ARGS (doctype)));
794 * Read AVI file tag when streaming
797 gst_avi_demux_stream_init_push (GstAviDemux * avi)
799 if (gst_adapter_available (avi->adapter) >= 12) {
802 tmp = gst_adapter_take_buffer (avi->adapter, 12);
804 GST_DEBUG ("Parsing avi header");
805 if (!gst_avi_demux_parse_file_header (GST_ELEMENT (avi), tmp)) {
806 return GST_FLOW_ERROR;
808 GST_DEBUG ("header ok");
811 avi->state = GST_AVI_DEMUX_HEADER;
820 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
823 GstBuffer *buf = NULL;
825 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
826 if (res != GST_FLOW_OK)
828 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
838 GST_DEBUG_OBJECT (avi, "error parsing file header");
839 return GST_FLOW_ERROR;
843 /* AVI header handling */
846 * gst_avi_demux_parse_avih:
847 * @element: caller element (used for errors/debug).
848 * @buf: input data to be used for parsing.
849 * @avih: pointer to structure (filled in by function) containing
850 * stream information (such as flags, number of streams, etc.).
852 * Read 'avih' header. Discards buffer after use.
854 * Returns: TRUE on success, FALSE otherwise. Throws an error if
855 * the header is invalid. The caller should error out
859 gst_avi_demux_parse_avih (GstElement * element,
860 GstBuffer * buf, gst_riff_avih ** _avih)
867 if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_avih))
870 avih = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
872 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
873 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
874 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
875 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
876 avih->flags = GUINT32_FROM_LE (avih->flags);
877 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
878 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
879 avih->streams = GUINT32_FROM_LE (avih->streams);
880 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
881 avih->width = GUINT32_FROM_LE (avih->width);
882 avih->height = GUINT32_FROM_LE (avih->height);
883 avih->scale = GUINT32_FROM_LE (avih->scale);
884 avih->rate = GUINT32_FROM_LE (avih->rate);
885 avih->start = GUINT32_FROM_LE (avih->start);
886 avih->length = GUINT32_FROM_LE (avih->length);
890 GST_INFO_OBJECT (element, "avih tag found:");
891 GST_INFO_OBJECT (element, " us_frame %u", avih->us_frame);
892 GST_INFO_OBJECT (element, " max_bps %u", avih->max_bps);
893 GST_INFO_OBJECT (element, " pad_gran %u", avih->pad_gran);
894 GST_INFO_OBJECT (element, " flags 0x%08x", avih->flags);
895 GST_INFO_OBJECT (element, " tot_frames %u", avih->tot_frames);
896 GST_INFO_OBJECT (element, " init_frames %u", avih->init_frames);
897 GST_INFO_OBJECT (element, " streams %u", avih->streams);
898 GST_INFO_OBJECT (element, " bufsize %u", avih->bufsize);
899 GST_INFO_OBJECT (element, " width %u", avih->width);
900 GST_INFO_OBJECT (element, " height %u", avih->height);
901 GST_INFO_OBJECT (element, " scale %u", avih->scale);
902 GST_INFO_OBJECT (element, " rate %u", avih->rate);
903 GST_INFO_OBJECT (element, " start %u", avih->start);
904 GST_INFO_OBJECT (element, " length %u", avih->length);
907 gst_buffer_unref (buf);
914 GST_ELEMENT_ERROR (element, STREAM, DEMUX, (NULL), ("No buffer"));
919 GST_ELEMENT_ERROR (element, STREAM, DEMUX, (NULL),
920 ("Too small avih (%d available, %d needed)",
921 GST_BUFFER_SIZE (buf), (int) sizeof (gst_riff_avih)));
922 gst_buffer_unref (buf);
928 * gst_avi_demux_parse_superindex:
929 * @avi: caller element (used for debugging/errors).
930 * @buf: input data to use for parsing.
931 * @locations: locations in the file (byte-offsets) that contain
932 * the actual indexes (see get_avi_demux_parse_subindex()).
933 * The array ends with GST_BUFFER_OFFSET_NONE.
935 * Reads superindex (openDML-2 spec stuff) from the provided data.
937 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
938 * on error, but they are not fatal.
941 gst_avi_demux_parse_superindex (GstAviDemux * avi,
942 GstBuffer * buf, guint64 ** _indexes)
952 size = buf ? GST_BUFFER_SIZE (buf) : 0;
956 data = GST_BUFFER_DATA (buf);
958 /* check type of index. The opendml2 specs state that
959 * there should be 4 dwords per array entry. Type can be
960 * either frame or field (and we don't care). */
961 if (GST_READ_UINT16_LE (data) != 4 ||
962 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
963 GST_WARNING_OBJECT (avi,
964 "Superindex for stream has unexpected "
965 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
966 GST_READ_UINT16_LE (data), data[2], data[3]);
967 bpe = GST_READ_UINT16_LE (data) * 4;
969 num = GST_READ_UINT32_LE (&data[4]);
971 GST_DEBUG_OBJECT (avi, "got %d indexes", num);
973 indexes = g_new (guint64, num + 1);
974 for (i = 0; i < num; i++) {
975 if (size < 24 + bpe * (i + 1))
977 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
978 GST_DEBUG_OBJECT (avi, "index %d at %" G_GUINT64_FORMAT, i, indexes[i]);
980 indexes[i] = GST_BUFFER_OFFSET_NONE;
983 gst_buffer_unref (buf);
990 GST_ERROR_OBJECT (avi,
991 "Not enough data to parse superindex (%d available, 24 needed)", size);
993 gst_buffer_unref (buf);
998 /* add an entry to the index of a stream. @num should be an estimate of the
999 * total amount of index entries for all streams and is used to dynamically
1000 * allocate memory for the index entries. */
1001 static inline gboolean
1002 gst_avi_demux_add_index (GstAviDemux * avi, GstAviStream * stream,
1003 guint num, GstAviIndexEntry * entry)
1005 /* ensure index memory */
1006 if (G_UNLIKELY (stream->idx_n >= stream->idx_max)) {
1007 guint idx_max = stream->idx_max;
1008 GstAviIndexEntry *new_idx;
1010 /* we need to make some more room */
1012 /* initial size guess, assume each stream has an equal amount of entries,
1013 * overshoot with at least 8K */
1014 idx_max = (num / avi->num_streams) + (8192 / sizeof (GstAviIndexEntry));
1016 idx_max += 8192 / sizeof (GstAviIndexEntry);
1017 GST_DEBUG_OBJECT (avi, "expanded index from %u to %u",
1018 stream->idx_max, idx_max);
1020 new_idx = g_try_renew (GstAviIndexEntry, stream->index, idx_max);
1021 /* out of memory, if this fails stream->index is untouched. */
1022 if (G_UNLIKELY (!new_idx))
1025 stream->index = new_idx;
1026 stream->idx_max = idx_max;
1029 /* update entry total and stream stats. The entry total can be converted to
1030 * the timestamp of the entry easily. */
1031 if (stream->strh->type == GST_RIFF_FCC_auds) {
1034 if (stream->is_vbr) {
1035 entry->total = stream->total_blocks;
1037 entry->total = stream->total_bytes;
1039 blockalign = stream->strf.auds->blockalign;
1041 stream->total_blocks += DIV_ROUND_UP (entry->size, blockalign);
1043 stream->total_blocks++;
1045 if (stream->is_vbr) {
1046 entry->total = stream->idx_n;
1048 entry->total = stream->total_bytes;
1051 stream->total_bytes += entry->size;
1052 if (ENTRY_IS_KEYFRAME (entry))
1053 stream->n_keyframes++;
1056 GST_LOG_OBJECT (avi,
1057 "Adding stream %u, index entry %d, kf %d, size %u "
1058 ", offset %" G_GUINT64_FORMAT ", total %" G_GUINT64_FORMAT, stream->num,
1059 stream->idx_n, ENTRY_IS_KEYFRAME (entry), entry->size, entry->offset,
1061 stream->index[stream->idx_n++] = *entry;
1066 /* given @entry_n in @stream, calculate info such as timestamps and
1067 * offsets for the entry. */
1069 gst_avi_demux_get_buffer_info (GstAviDemux * avi, GstAviStream * stream,
1070 guint entry_n, GstClockTime * timestamp, GstClockTime * ts_end,
1071 guint64 * offset, guint64 * offset_end)
1073 GstAviIndexEntry *entry;
1075 entry = &stream->index[entry_n];
1077 if (stream->is_vbr) {
1078 /* VBR stream next timestamp */
1079 if (stream->strh->type == GST_RIFF_FCC_auds) {
1082 avi_stream_convert_frames_to_time_unchecked (stream, entry->total);
1084 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1089 avi_stream_convert_frames_to_time_unchecked (stream, entry_n);
1091 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1095 /* constant rate stream */
1098 avi_stream_convert_bytes_to_time_unchecked (stream, entry->total);
1100 *ts_end = avi_stream_convert_bytes_to_time_unchecked (stream,
1101 entry->total + entry->size);
1103 if (stream->strh->type == GST_RIFF_FCC_vids) {
1104 /* video offsets are the frame number */
1108 *offset_end = entry_n + 1;
1110 /* no offsets for audio */
1118 /* collect and debug stats about the indexes for all streams.
1119 * This method is also responsible for filling in the stream duration
1120 * as measured by the amount of index entries. */
1122 gst_avi_demux_do_index_stats (GstAviDemux * avi)
1125 #ifndef GST_DISABLE_GST_DEBUG
1126 guint total_idx = 0, total_max = 0;
1129 /* get stream stats now */
1130 for (i = 0; i < avi->num_streams; i++) {
1131 GstAviStream *stream;
1133 if (G_UNLIKELY (!(stream = &avi->stream[i])))
1135 if (G_UNLIKELY (!stream->strh))
1137 if (G_UNLIKELY (!stream->index || stream->idx_n == 0))
1140 /* we interested in the end_ts of the last entry, which is the total
1141 * duration of this stream */
1142 gst_avi_demux_get_buffer_info (avi, stream, stream->idx_n - 1,
1143 NULL, &stream->idx_duration, NULL, NULL);
1145 #ifndef GST_DISABLE_GST_DEBUG
1146 total_idx += stream->idx_n;
1147 total_max += stream->idx_max;
1149 GST_INFO_OBJECT (avi, "Stream %d, dur %" GST_TIME_FORMAT ", %6u entries, "
1150 "%5u keyframes, entry size = %2u, total size = %10u, allocated %10u",
1151 i, GST_TIME_ARGS (stream->idx_duration), stream->idx_n,
1152 stream->n_keyframes, (guint) sizeof (GstAviIndexEntry),
1153 (guint) (stream->idx_n * sizeof (GstAviIndexEntry)),
1154 (guint) (stream->idx_max * sizeof (GstAviIndexEntry)));
1156 #ifndef GST_DISABLE_GST_DEBUG
1157 total_idx *= sizeof (GstAviIndexEntry);
1158 total_max *= sizeof (GstAviIndexEntry);
1160 GST_INFO_OBJECT (avi, "%u bytes for index vs %u ideally, %u wasted",
1161 total_max, total_idx, total_max - total_idx);
1165 * gst_avi_demux_parse_subindex:
1167 * @buf: input data to use for parsing.
1168 * @stream: stream context.
1169 * @entries_list: a list (returned by the function) containing all the
1170 * indexes parsed in this specific subindex. The first
1171 * entry is also a pointer to allocated memory that needs
1172 * to be free´ed. May be NULL if no supported indexes were
1175 * Reads superindex (openDML-2 spec stuff) from the provided data.
1176 * The buffer should contain a GST_RIFF_TAG_ix?? chunk.
1178 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1179 * throw an error, caller should bail out asap.
1182 gst_avi_demux_parse_subindex (GstAviDemux * avi, GstAviStream * stream,
1194 size = GST_BUFFER_SIZE (buf);
1200 data = GST_BUFFER_DATA (buf);
1202 /* We don't support index-data yet */
1204 goto not_implemented;
1206 /* check type of index. The opendml2 specs state that
1207 * there should be 4 dwords per array entry. Type can be
1208 * either frame or field (and we don't care). */
1209 bpe = (data[2] & 0x01) ? 12 : 8;
1210 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1211 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1212 GST_WARNING_OBJECT (avi,
1213 "Superindex for stream %d has unexpected "
1214 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1215 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1216 bpe = GST_READ_UINT16_LE (data) * 4;
1218 num = GST_READ_UINT32_LE (&data[4]);
1219 baseoff = GST_READ_UINT64_LE (&data[12]);
1221 /* If there's nothing, just return ! */
1225 GST_INFO_OBJECT (avi, "Parsing subindex, nr_entries = %6d", num);
1227 for (i = 0; i < num; i++) {
1228 GstAviIndexEntry entry;
1230 if (size < 24 + bpe * (i + 1))
1233 /* fill in offset and size. offset contains the keyframe flag in the
1235 entry.offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1236 entry.size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1238 if (stream->strh->type == GST_RIFF_FCC_auds) {
1239 /* all audio frames are keyframes */
1240 ENTRY_SET_KEYFRAME (&entry);
1242 /* else read flags */
1243 entry.flags = (entry.size & 0x80000000) ? 0 : GST_AVI_KEYFRAME;
1245 entry.size &= ~0x80000000;
1248 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
1251 gst_buffer_unref (buf);
1258 GST_ERROR_OBJECT (avi,
1259 "Not enough data to parse subindex (%d available, 24 needed)", size);
1260 gst_buffer_unref (buf);
1261 return TRUE; /* continue */
1265 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1266 ("Subindex-is-data is not implemented"));
1267 gst_buffer_unref (buf);
1272 GST_DEBUG_OBJECT (avi, "the index is empty");
1273 gst_buffer_unref (buf);
1278 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1279 ("Cannot allocate memory for %u*%u=%u bytes",
1280 (guint) sizeof (GstAviIndexEntry), num,
1281 (guint) sizeof (GstAviIndexEntry) * num));
1282 gst_buffer_unref (buf);
1289 * Read AVI index when streaming
1292 gst_avi_demux_read_subindexes_push (GstAviDemux * avi)
1294 guint32 tag = 0, size;
1295 GstBuffer *buf = NULL;
1298 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1300 for (n = 0; n < avi->num_streams; n++) {
1301 GstAviStream *stream = &avi->stream[n];
1303 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1304 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1306 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1307 '0' + stream->num % 10)) &&
1308 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1309 '0' + stream->num % 10, 'i', 'x'))) {
1310 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1311 GST_FOURCC_ARGS (tag));
1315 avi->offset += 8 + GST_ROUND_UP_2 (size);
1317 buf = gst_buffer_new ();
1318 GST_BUFFER_DATA (buf) = gst_adapter_take (avi->adapter, size);
1319 GST_BUFFER_SIZE (buf) = size;
1321 if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1325 g_free (stream->indexes);
1326 stream->indexes = NULL;
1328 /* get stream stats now */
1329 gst_avi_demux_do_index_stats (avi);
1331 avi->have_index = TRUE;
1339 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi)
1345 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1347 for (n = 0; n < avi->num_streams; n++) {
1348 GstAviStream *stream = &avi->stream[n];
1350 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1351 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi), avi->sinkpad,
1352 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1354 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1355 '0' + stream->num % 10)) &&
1356 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1357 '0' + stream->num % 10, 'i', 'x'))) {
1358 /* Some ODML files (created by god knows what muxer) have a ##ix format
1359 * instead of the 'official' ix##. They are still valid though. */
1360 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1361 GST_FOURCC_ARGS (tag));
1362 gst_buffer_unref (buf);
1366 if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1370 g_free (stream->indexes);
1371 stream->indexes = NULL;
1373 /* get stream stats now */
1374 gst_avi_demux_do_index_stats (avi);
1376 avi->have_index = TRUE;
1380 * gst_avi_demux_riff_parse_vprp:
1381 * @element: caller element (used for debugging/error).
1382 * @buf: input data to be used for parsing, stripped from header.
1383 * @vprp: a pointer (returned by this function) to a filled-in vprp
1384 * structure. Caller should free it.
1386 * Parses a video stream´s vprp. This function takes ownership of @buf.
1388 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1389 * should be skipped on error, but it is not fatal.
1392 gst_avi_demux_riff_parse_vprp (GstElement * element,
1393 GstBuffer * buf, gst_riff_vprp ** _vprp)
1395 gst_riff_vprp *vprp;
1398 g_return_val_if_fail (buf != NULL, FALSE);
1399 g_return_val_if_fail (_vprp != NULL, FALSE);
1401 if (GST_BUFFER_SIZE (buf) < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1404 vprp = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1406 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1407 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1408 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1409 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1410 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1411 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1412 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1413 vprp->width = GUINT32_FROM_LE (vprp->width);
1414 vprp->height = GUINT32_FROM_LE (vprp->height);
1415 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1419 /* calculate fields based on size */
1420 k = (GST_BUFFER_SIZE (buf) - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) /
1422 if (vprp->fields > k) {
1423 GST_WARNING_OBJECT (element,
1424 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1427 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1428 GST_WARNING_OBJECT (element,
1429 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1430 GST_RIFF_VPRP_VIDEO_FIELDS);
1431 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1433 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1434 for (k = 0; k < vprp->fields; k++) {
1435 gst_riff_vprp_video_field_desc *fd;
1437 fd = &vprp->field_info[k];
1438 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1439 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1440 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1441 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1442 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1443 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1444 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1445 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1450 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1451 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1452 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1453 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1454 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1455 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1456 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1457 vprp->aspect & 0xffff);
1458 GST_INFO_OBJECT (element, " width %d", vprp->width);
1459 GST_INFO_OBJECT (element, " height %d", vprp->height);
1460 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1461 for (k = 0; k < vprp->fields; k++) {
1462 gst_riff_vprp_video_field_desc *fd;
1464 fd = &(vprp->field_info[k]);
1465 GST_INFO_OBJECT (element, " field %u description:", k);
1466 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1467 fd->compressed_bm_height);
1468 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1469 fd->compressed_bm_width);
1470 GST_INFO_OBJECT (element, " valid_bm_height %d",
1471 fd->valid_bm_height);
1472 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1473 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1474 fd->valid_bm_x_offset);
1475 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1476 fd->valid_bm_y_offset);
1477 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1478 fd->video_x_t_offset);
1479 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1482 gst_buffer_unref (buf);
1491 GST_ERROR_OBJECT (element,
1492 "Too small vprp (%d available, at least %d needed)",
1493 GST_BUFFER_SIZE (buf),
1494 (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1495 gst_buffer_unref (buf);
1501 * gst_avi_demux_parse_stream:
1502 * @avi: calling element (used for debugging/errors).
1503 * @buf: input buffer used to parse the stream.
1505 * Parses all subchunks in a strl chunk (which defines a single
1506 * stream). Discards the buffer after use. This function will
1507 * increment the stream counter internally.
1509 * Returns: whether the stream was identified successfully.
1510 * Errors are not fatal. It does indicate the stream
1514 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
1516 GstAviStream *stream;
1517 GstElementClass *klass;
1518 GstPadTemplate *templ;
1519 GstBuffer *sub = NULL;
1522 gchar *codec_name = NULL, *padname = NULL;
1523 const gchar *tag_name;
1524 GstCaps *caps = NULL;
1526 GstElement *element;
1527 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
1528 gst_riff_vprp *vprp = NULL;
1530 element = GST_ELEMENT_CAST (avi);
1532 GST_DEBUG_OBJECT (avi, "Parsing stream");
1534 if (avi->num_streams >= GST_AVI_DEMUX_MAX_STREAMS) {
1535 GST_WARNING_OBJECT (avi,
1536 "maximum no of streams (%d) exceeded, ignoring stream",
1537 GST_AVI_DEMUX_MAX_STREAMS);
1538 gst_buffer_unref (buf);
1539 /* not a fatal error, let's say */
1543 stream = &avi->stream[avi->num_streams];
1545 /* initial settings */
1546 stream->idx_duration = GST_CLOCK_TIME_NONE;
1547 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1548 stream->duration = GST_CLOCK_TIME_NONE;
1550 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
1551 /* sub can be NULL if the chunk is empty */
1553 GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
1554 GST_FOURCC_ARGS (tag));
1558 case GST_RIFF_TAG_strh:
1560 gst_riff_strh *strh;
1563 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
1566 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
1567 /* ownership given away */
1569 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
1573 strh = stream->strh;
1574 /* sanity check; stream header frame rate matches global header
1576 if (stream->strh->type == GST_RIFF_FCC_vids) {
1578 GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
1580 s_dur = gst_util_uint64_scale (GST_SECOND, strh->scale, strh->rate);
1581 GST_DEBUG_OBJECT (avi, "verifying stream framerate %d/%d, "
1582 "frame duration = %d ms", strh->rate, strh->scale,
1583 (gint) (s_dur / GST_MSECOND));
1584 if (h_dur > (10 * GST_MSECOND) && (s_dur > 10 * h_dur)) {
1585 strh->rate = GST_SECOND / GST_USECOND;
1586 strh->scale = h_dur / GST_USECOND;
1587 GST_DEBUG_OBJECT (avi, "correcting stream framerate to %d/%d",
1588 strh->rate, strh->scale);
1591 /* determine duration as indicated by header */
1592 stream->hdr_duration = gst_util_uint64_scale ((guint64) strh->length *
1593 strh->scale, GST_SECOND, (guint64) strh->rate);
1594 GST_INFO ("Stream duration according to header: %" GST_TIME_FORMAT,
1595 GST_TIME_ARGS (stream->hdr_duration));
1596 if (stream->hdr_duration == 0)
1597 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1602 case GST_RIFF_TAG_strf:
1604 gboolean res = FALSE;
1607 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
1611 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
1614 switch (stream->strh->type) {
1615 case GST_RIFF_FCC_vids:
1616 stream->is_vbr = TRUE;
1617 res = gst_riff_parse_strf_vids (element, sub,
1618 &stream->strf.vids, &stream->extradata);
1620 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
1622 case GST_RIFF_FCC_auds:
1623 stream->is_vbr = (stream->strh->samplesize == 0)
1624 && stream->strh->scale > 1;
1626 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
1627 &stream->extradata);
1629 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
1630 stream->is_vbr, res);
1632 case GST_RIFF_FCC_iavs:
1633 stream->is_vbr = TRUE;
1634 res = gst_riff_parse_strf_iavs (element, sub,
1635 &stream->strf.iavs, &stream->extradata);
1637 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
1639 case GST_RIFF_FCC_txts:
1640 /* nothing to parse here */
1641 stream->is_vbr = (stream->strh->samplesize == 0)
1642 && (stream->strh->scale > 1);
1646 GST_ERROR_OBJECT (avi,
1647 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
1648 GST_FOURCC_ARGS (stream->strh->type));
1652 gst_buffer_unref (sub);
1660 case GST_RIFF_TAG_vprp:
1663 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
1667 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
1671 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
1675 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
1676 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
1677 /* not considered fatal */
1685 case GST_RIFF_TAG_strd:
1686 if (stream->initdata)
1687 gst_buffer_unref (stream->initdata);
1688 stream->initdata = sub;
1691 case GST_RIFF_TAG_strn:
1692 g_free (stream->name);
1695 g_strndup ((gchar *) GST_BUFFER_DATA (sub),
1696 (gsize) GST_BUFFER_SIZE (sub));
1697 gst_buffer_unref (sub);
1700 stream->name = g_strdup ("");
1702 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
1705 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
1706 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
1707 '0' + avi->num_streams % 10)) {
1708 g_free (stream->indexes);
1709 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
1710 stream->superindex = TRUE;
1714 GST_WARNING_OBJECT (avi,
1715 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
1716 GST_FOURCC_ARGS (tag));
1718 case GST_RIFF_TAG_JUNK:
1722 gst_buffer_unref (sub);
1728 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
1733 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
1737 /* get class to figure out the template */
1738 klass = GST_ELEMENT_GET_CLASS (avi);
1740 /* we now have all info, let´s set up a pad and a caps and be done */
1741 /* create stream name + pad */
1742 switch (stream->strh->type) {
1743 case GST_RIFF_FCC_vids:{
1746 fourcc = (stream->strf.vids->compression) ?
1747 stream->strf.vids->compression : stream->strh->fcc_handler;
1748 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
1749 templ = gst_element_class_get_pad_template (klass, "video_%02d");
1750 caps = gst_riff_create_video_caps (fourcc, stream->strh,
1751 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
1753 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
1754 GST_TYPE_FOURCC, fourcc, NULL);
1755 } else if (got_vprp && vprp) {
1756 guint32 aspect_n, aspect_d;
1759 aspect_n = vprp->aspect >> 16;
1760 aspect_d = vprp->aspect & 0xffff;
1761 /* calculate the pixel aspect ratio using w/h and aspect ratio */
1762 n = aspect_n * stream->strf.vids->height;
1763 d = aspect_d * stream->strf.vids->width;
1765 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1767 /* very local, not needed elsewhere */
1771 tag_name = GST_TAG_VIDEO_CODEC;
1772 avi->num_v_streams++;
1775 case GST_RIFF_FCC_auds:{
1776 padname = g_strdup_printf ("audio_%02d", avi->num_a_streams);
1777 templ = gst_element_class_get_pad_template (klass, "audio_%02d");
1778 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
1779 stream->strh, stream->strf.auds, stream->extradata,
1780 stream->initdata, &codec_name);
1782 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
1783 G_TYPE_INT, stream->strf.auds->format, NULL);
1785 tag_name = GST_TAG_AUDIO_CODEC;
1786 avi->num_a_streams++;
1789 case GST_RIFF_FCC_iavs:{
1790 guint32 fourcc = stream->strh->fcc_handler;
1792 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
1793 templ = gst_element_class_get_pad_template (klass, "video_%02d");
1794 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
1795 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
1797 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
1798 GST_TYPE_FOURCC, fourcc, NULL);
1800 tag_name = GST_TAG_VIDEO_CODEC;
1801 avi->num_v_streams++;
1804 case GST_RIFF_FCC_txts:{
1805 padname = g_strdup_printf ("subtitle_%02d", avi->num_t_streams);
1806 templ = gst_element_class_get_pad_template (klass, "subtitle_%02d");
1807 caps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
1809 avi->num_t_streams++;
1813 g_assert_not_reached ();
1816 /* no caps means no stream */
1818 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
1822 GST_DEBUG_OBJECT (element, "codec-name=%s",
1823 (codec_name ? codec_name : "NULL"));
1824 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
1826 /* set proper settings and add it */
1828 gst_object_unref (stream->pad);
1829 pad = stream->pad = gst_pad_new_from_template (templ, padname);
1832 gst_pad_use_fixed_caps (pad);
1834 gst_pad_set_formats_function (pad,
1835 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
1836 gst_pad_set_event_mask_function (pad,
1837 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
1839 gst_pad_set_event_function (pad,
1840 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
1841 gst_pad_set_query_type_function (pad,
1842 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_query_types));
1843 gst_pad_set_query_function (pad,
1844 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
1846 gst_pad_set_convert_function (pad,
1847 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
1850 stream->num = avi->num_streams;
1852 stream->start_entry = 0;
1853 stream->step_entry = 0;
1854 stream->stop_entry = 0;
1856 stream->current_entry = -1;
1857 stream->current_total = 0;
1859 stream->last_flow = GST_FLOW_OK;
1860 stream->discont = TRUE;
1862 stream->total_bytes = 0;
1863 stream->total_blocks = 0;
1864 stream->n_keyframes = 0;
1867 stream->idx_max = 0;
1869 gst_pad_set_element_private (pad, stream);
1872 gst_pad_set_caps (pad, caps);
1873 gst_pad_set_active (pad, TRUE);
1874 gst_element_add_pad (GST_ELEMENT (avi), pad);
1876 GST_LOG_OBJECT (element, "Added pad %s with caps %" GST_PTR_FORMAT,
1877 GST_PAD_NAME (pad), caps);
1878 gst_caps_unref (caps);
1882 if (!stream->taglist)
1883 stream->taglist = gst_tag_list_new ();
1885 avi->got_tags = TRUE;
1887 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
1889 g_free (codec_name);
1892 gst_buffer_unref (buf);
1899 /* unref any mem that may be in use */
1901 gst_buffer_unref (buf);
1903 gst_buffer_unref (sub);
1905 g_free (codec_name);
1906 gst_avi_demux_reset_stream (avi, stream);
1913 * gst_avi_demux_parse_odml:
1914 * @avi: calling element (used for debug/error).
1915 * @buf: input buffer to be used for parsing.
1917 * Read an openDML-2.0 extension header. Fills in the frame number
1918 * in the avi demuxer object when reading succeeds.
1921 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
1925 GstBuffer *sub = NULL;
1927 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
1930 case GST_RIFF_TAG_dmlh:{
1931 gst_riff_dmlh dmlh, *_dmlh;
1934 /* sub == NULL is possible and means an empty buffer */
1935 size = sub ? GST_BUFFER_SIZE (sub) : 0;
1938 if (size < sizeof (gst_riff_dmlh)) {
1939 GST_ERROR_OBJECT (avi,
1940 "DMLH entry is too small (%d bytes, %d needed)",
1941 size, (int) sizeof (gst_riff_dmlh));
1944 _dmlh = (gst_riff_dmlh *) GST_BUFFER_DATA (sub);
1945 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
1947 GST_INFO_OBJECT (avi, "dmlh tag found: totalframes: %u",
1950 avi->avih->tot_frames = dmlh.totalframes;
1955 GST_WARNING_OBJECT (avi,
1956 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
1957 GST_FOURCC_ARGS (tag));
1959 case GST_RIFF_TAG_JUNK:
1961 /* skip and move to next chunk */
1963 gst_buffer_unref (sub);
1970 gst_buffer_unref (buf);
1975 gst_avi_demux_index_last (GstAviDemux * avi, GstAviStream * stream)
1977 return stream->idx_n - 1;
1980 /* find a previous entry in the index with the given flags */
1982 gst_avi_demux_index_prev (GstAviDemux * avi, GstAviStream * stream,
1983 guint last, gboolean keyframe)
1985 GstAviIndexEntry *entry;
1988 for (i = last; i > 0; i--) {
1989 entry = &stream->index[i - 1];
1990 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
1998 gst_avi_demux_index_next (GstAviDemux * avi, GstAviStream * stream,
1999 guint last, gboolean keyframe)
2001 GstAviIndexEntry *entry;
2004 for (i = last + 1; i < stream->idx_n; i++) {
2005 entry = &stream->index[i];
2006 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2010 return stream->idx_n - 1;
2014 gst_avi_demux_index_entry_search (GstAviIndexEntry * entry, guint64 * total)
2016 if (entry->total < *total)
2018 else if (entry->total > *total)
2024 * gst_avi_demux_index_for_time:
2026 * @stream: the stream
2027 * @time: a time position
2029 * Finds the index entry which time is less or equal than the requested time.
2030 * Try to avoid binary search when we can convert the time to an index
2031 * position directly (for example for video frames with a fixed duration).
2033 * Returns: the found position in the index.
2036 gst_avi_demux_index_for_time (GstAviDemux * avi,
2037 GstAviStream * stream, guint64 time)
2042 GST_LOG_OBJECT (avi, "search time:%" GST_TIME_FORMAT, GST_TIME_ARGS (time));
2044 /* easy (and common) cases */
2045 if (time == 0 || stream->idx_n == 0)
2047 if (time >= stream->idx_duration)
2048 return stream->idx_n - 1;
2050 /* figure out where we need to go. For that we convert the time to an
2051 * index entry or we convert it to a total and then do a binary search. */
2052 if (stream->is_vbr) {
2053 /* VBR stream next timestamp */
2054 if (stream->strh->type == GST_RIFF_FCC_auds) {
2055 total = avi_stream_convert_time_to_frames_unchecked (stream, time);
2057 index = avi_stream_convert_time_to_frames_unchecked (stream, time);
2060 /* constant rate stream */
2061 total = avi_stream_convert_time_to_bytes_unchecked (stream, time);
2065 GstAviIndexEntry *entry;
2067 /* no index, find index with binary search on total */
2068 GST_LOG_OBJECT (avi, "binary search for entry with total %"
2069 G_GUINT64_FORMAT, total);
2071 entry = gst_util_array_binary_search (stream->index,
2072 stream->idx_n, sizeof (GstAviIndexEntry),
2073 (GCompareDataFunc) gst_avi_demux_index_entry_search,
2074 GST_SEARCH_MODE_BEFORE, &total, NULL);
2076 if (entry == NULL) {
2077 GST_LOG_OBJECT (avi, "not found, assume index 0");
2080 index = entry - stream->index;
2081 GST_LOG_OBJECT (avi, "found at %u", index);
2084 GST_LOG_OBJECT (avi, "converted time to index %u", index);
2090 static inline GstAviStream *
2091 gst_avi_demux_stream_for_id (GstAviDemux * avi, guint32 id)
2094 GstAviStream *stream;
2096 /* get the stream for this entry */
2097 stream_nr = CHUNKID_TO_STREAMNR (id);
2098 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2099 GST_WARNING_OBJECT (avi, "invalid stream nr %d", stream_nr);
2102 stream = &avi->stream[stream_nr];
2103 if (G_UNLIKELY (!stream->strh)) {
2104 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2111 * gst_avi_demux_parse_index:
2112 * @avi: calling element (used for debugging/errors).
2113 * @buf: buffer containing the full index.
2115 * Read index entries from the provided buffer.
2116 * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
2119 gst_avi_demux_parse_index (GstAviDemux * avi, GstBuffer * buf)
2125 gst_riff_index_entry *index;
2127 GstAviStream *stream;
2128 GstAviIndexEntry entry;
2134 data = GST_BUFFER_DATA (buf);
2135 size = GST_BUFFER_SIZE (buf);
2137 stamp = gst_util_get_timestamp ();
2139 /* see how many items in the index */
2140 num = size / sizeof (gst_riff_index_entry);
2144 GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2146 index = (gst_riff_index_entry *) data;
2147 pos_before = avi->offset;
2149 /* figure out if the index is 0 based or relative to the MOVI start */
2150 entry.offset = GST_READ_UINT32_LE (&index[0].offset);
2151 if (entry.offset < avi->offset) {
2152 avi->index_offset = avi->offset + 8;
2153 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2155 avi->index_offset = 0;
2156 GST_DEBUG ("index is 0 based");
2159 for (i = 0, n = 0; i < num; i++) {
2160 id = GST_READ_UINT32_LE (&index[i].id);
2161 entry.offset = GST_READ_UINT32_LE (&index[i].offset);
2163 /* some sanity checks */
2164 if (G_UNLIKELY (id == GST_RIFF_rec || id == 0 ||
2165 (entry.offset == 0 && n > 0)))
2168 /* get the stream for this entry */
2169 stream = gst_avi_demux_stream_for_id (avi, id);
2170 if (G_UNLIKELY (!stream))
2173 /* handle offset and size */
2174 entry.offset += avi->index_offset + 8;
2175 entry.size = GST_READ_UINT32_LE (&index[i].size);
2178 if (stream->strh->type == GST_RIFF_FCC_auds) {
2179 /* all audio frames are keyframes */
2180 ENTRY_SET_KEYFRAME (&entry);
2183 /* else read flags */
2184 flags = GST_READ_UINT32_LE (&index[i].flags);
2185 if (flags & GST_RIFF_IF_KEYFRAME) {
2186 ENTRY_SET_KEYFRAME (&entry);
2188 ENTRY_UNSET_KEYFRAME (&entry);
2193 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2198 gst_buffer_unref (buf);
2200 /* get stream stats now */
2201 gst_avi_demux_do_index_stats (avi);
2203 stamp = gst_util_get_timestamp () - stamp;
2204 GST_DEBUG_OBJECT (avi, "parsing index %" GST_TIME_FORMAT,
2205 GST_TIME_ARGS (stamp));
2207 /* we have an index now */
2208 avi->have_index = TRUE;
2215 GST_DEBUG_OBJECT (avi, "empty index");
2216 gst_buffer_unref (buf);
2221 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2222 ("Cannot allocate memory for %u*%u=%u bytes",
2223 (guint) sizeof (GstAviIndexEntry), num,
2224 (guint) sizeof (GstAviIndexEntry) * num));
2225 gst_buffer_unref (buf);
2231 * gst_avi_demux_stream_index:
2232 * @avi: avi demuxer object.
2234 * Seeks to index and reads it.
2237 gst_avi_demux_stream_index (GstAviDemux * avi)
2240 guint64 offset = avi->offset;
2245 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2247 /* get chunk information */
2248 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2249 if (res != GST_FLOW_OK)
2251 else if (GST_BUFFER_SIZE (buf) < 8)
2254 /* check tag first before blindy trying to read 'size' bytes */
2255 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2256 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2257 if (tag == GST_RIFF_TAG_LIST) {
2258 /* this is the movi tag */
2259 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2260 (8 + GST_ROUND_UP_2 (size)));
2261 offset += 8 + GST_ROUND_UP_2 (size);
2262 gst_buffer_unref (buf);
2263 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2264 if (res != GST_FLOW_OK)
2266 else if (GST_BUFFER_SIZE (buf) < 8)
2268 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2269 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2272 if (tag != GST_RIFF_TAG_idx1)
2277 gst_buffer_unref (buf);
2279 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2281 /* read chunk, advance offset */
2282 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2283 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2286 GST_DEBUG ("will parse index chunk size %u for tag %"
2287 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2289 gst_avi_demux_parse_index (avi, buf);
2291 #ifndef GST_DISABLE_GST_DEBUG
2292 /* debug our indexes */
2295 GstAviStream *stream;
2297 for (i = 0; i < avi->num_streams; i++) {
2298 stream = &avi->stream[i];
2299 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2300 i, stream->idx_n, stream->total_bytes);
2309 GST_DEBUG_OBJECT (avi,
2310 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2315 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2316 gst_buffer_unref (buf);
2321 GST_WARNING_OBJECT (avi,
2322 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2323 GST_FOURCC_ARGS (tag));
2324 gst_buffer_unref (buf);
2329 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2330 gst_buffer_unref (buf);
2336 * gst_avi_demux_peek_tag:
2338 * Returns the tag and size of the next chunk
2340 static GstFlowReturn
2341 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2344 GstFlowReturn res = GST_FLOW_OK;
2345 GstBuffer *buf = NULL;
2349 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2350 if (res != GST_FLOW_OK)
2353 bufsize = GST_BUFFER_SIZE (buf);
2357 bufdata = GST_BUFFER_DATA (buf);
2359 *tag = GST_READ_UINT32_LE (bufdata);
2360 *size = GST_READ_UINT32_LE (bufdata + 4);
2362 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2363 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2364 *size, offset + 8, offset + 8 + (gint64) * size);
2367 gst_buffer_unref (buf);
2374 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2379 GST_DEBUG_OBJECT (avi, "got %d bytes which is <> 8 bytes", bufsize);
2380 res = GST_FLOW_ERROR;
2386 * gst_avi_demux_next_data_buffer:
2388 * Returns the offset and size of the next buffer
2389 * Position is the position of the buffer (after tag and size)
2391 static GstFlowReturn
2392 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2393 guint32 * tag, guint * size)
2395 guint64 off = *offset;
2400 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2401 if (res != GST_FLOW_OK)
2403 if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
2404 off += 8 + 4; /* skip tag + size + subtag */
2416 * gst_avi_demux_stream_scan:
2417 * @avi: calling element (used for debugging/errors).
2419 * Scan the file for all chunks to "create" a new index.
2423 gst_avi_demux_stream_scan (GstAviDemux * avi)
2426 GstAviStream *stream;
2435 * - implement non-seekable source support.
2437 GST_DEBUG_OBJECT (avi, "Creating index");
2439 /* get the size of the file */
2440 format = GST_FORMAT_BYTES;
2441 if (!gst_pad_query_peer_duration (avi->sinkpad, &format, &tmplength))
2445 /* guess the total amount of entries we expect */
2449 GstAviIndexEntry entry;
2452 /* start reading data buffers to find the id and offset */
2453 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
2454 if (G_UNLIKELY (res != GST_FLOW_OK))
2458 stream = gst_avi_demux_stream_for_id (avi, tag);
2459 if (G_UNLIKELY (!stream))
2462 /* we can't figure out the keyframes, assume they all are */
2463 entry.flags = GST_AVI_KEYFRAME;
2467 /* and add to the index of this stream */
2468 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2472 /* update position */
2473 pos += GST_ROUND_UP_2 (size);
2474 if (G_UNLIKELY (pos > length)) {
2475 GST_WARNING_OBJECT (avi,
2476 "Stopping index lookup since we are further than EOF");
2481 gst_avi_demux_do_index_stats (avi);
2483 /* we have an index now */
2484 avi->have_index = TRUE;
2491 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2492 ("Cannot allocate memory for %u*%u=%u bytes",
2493 (guint) sizeof (GstAviIndexEntry), num,
2494 (guint) sizeof (GstAviIndexEntry) * num));
2500 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
2504 GstAviStream *stream;
2506 total = GST_CLOCK_TIME_NONE;
2508 /* all streams start at a timestamp 0 */
2509 for (i = 0; i < avi->num_streams; i++) {
2510 GstClockTime duration, hduration;
2511 gst_riff_strh *strh;
2513 stream = &avi->stream[i];
2514 if (G_UNLIKELY (!stream || !(strh = stream->strh)))
2517 /* get header duration for the stream */
2518 hduration = stream->hdr_duration;
2519 /* index duration calculated during parsing */
2520 duration = stream->idx_duration;
2522 /* now pick a good duration */
2523 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2524 /* index gave valid duration, use that */
2525 GST_INFO ("Stream %p duration according to index: %" GST_TIME_FORMAT,
2526 stream, GST_TIME_ARGS (duration));
2528 /* fall back to header info to calculate a duration */
2529 duration = hduration;
2531 /* set duration for the stream */
2532 stream->duration = duration;
2534 /* find total duration */
2535 if (total == GST_CLOCK_TIME_NONE || duration > total)
2539 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
2540 /* now update the duration for those streams where we had none */
2541 for (i = 0; i < avi->num_streams; i++) {
2542 stream = &avi->stream[i];
2544 if (!GST_CLOCK_TIME_IS_VALID (stream->duration)
2545 || stream->duration == 0) {
2546 stream->duration = total;
2548 GST_INFO ("Stream %p duration according to total: %" GST_TIME_FORMAT,
2549 stream, GST_TIME_ARGS (total));
2554 /* and set the total duration in the segment. */
2555 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
2556 GST_TIME_ARGS (total));
2558 gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
2561 /* returns FALSE if there are no pads to deliver event to,
2562 * otherwise TRUE (whatever the outcome of event sending),
2563 * takes ownership of the event. */
2565 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
2567 gboolean result = FALSE;
2570 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
2571 GST_EVENT_TYPE_NAME (event), avi->num_streams);
2573 for (i = 0; i < avi->num_streams; i++) {
2574 GstAviStream *stream = &avi->stream[i];
2578 gst_pad_push_event (stream->pad, gst_event_ref (event));
2581 gst_event_unref (event);
2586 * Read AVI headers when streaming
2588 static GstFlowReturn
2589 gst_avi_demux_stream_header_push (GstAviDemux * avi)
2591 GstFlowReturn ret = GST_FLOW_OK;
2596 GstBuffer *buf = NULL, *sub = NULL;
2600 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
2602 switch (avi->header_state) {
2603 case GST_AVI_DEMUX_HEADER_TAG_LIST:
2604 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2605 avi->offset += 8 + GST_ROUND_UP_2 (size);
2606 if (tag != GST_RIFF_TAG_LIST)
2607 goto header_no_list;
2609 gst_adapter_flush (avi->adapter, 8);
2610 /* Find the 'hdrl' LIST tag */
2611 GST_DEBUG ("Reading %d bytes", size);
2612 buf = gst_adapter_take_buffer (avi->adapter, size);
2614 if (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl)
2615 goto header_no_hdrl;
2619 gst_adapter_flush (avi->adapter, 1);
2621 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
2623 /* the hdrl starts with a 'avih' header */
2624 if (!gst_riff_parse_chunk (GST_ELEMENT (avi), buf, &offset, &tag, &sub))
2625 goto header_no_avih;
2627 if (tag != GST_RIFF_TAG_avih)
2628 goto header_no_avih;
2630 if (!gst_avi_demux_parse_avih (GST_ELEMENT (avi), sub, &avi->avih))
2631 goto header_wrong_avih;
2633 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
2635 /* now, read the elements from the header until the end */
2636 while (gst_riff_parse_chunk (GST_ELEMENT (avi), buf, &offset, &tag,
2638 /* sub can be NULL on empty tags */
2643 case GST_RIFF_TAG_LIST:
2644 if (GST_BUFFER_SIZE (sub) < 4)
2647 switch (GST_READ_UINT32_LE (GST_BUFFER_DATA (sub))) {
2648 case GST_RIFF_LIST_strl:
2649 if (!(gst_avi_demux_parse_stream (avi, sub))) {
2651 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
2652 ("failed to parse stream, ignoring"));
2657 case GST_RIFF_LIST_odml:
2658 gst_avi_demux_parse_odml (avi, sub);
2662 GST_WARNING_OBJECT (avi,
2663 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
2664 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA
2667 case GST_RIFF_TAG_JUNK:
2672 GST_WARNING_OBJECT (avi,
2673 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
2674 offset, GST_FOURCC_ARGS (tag));
2676 case GST_RIFF_TAG_JUNK:
2678 /* move to next chunk */
2680 gst_buffer_unref (sub);
2685 gst_buffer_unref (buf);
2686 GST_DEBUG ("elements parsed");
2688 /* check parsed streams */
2689 if (avi->num_streams == 0) {
2691 } else if (avi->num_streams != avi->avih->streams) {
2692 GST_WARNING_OBJECT (avi,
2693 "Stream header mentioned %d streams, but %d available",
2694 avi->avih->streams, avi->num_streams);
2696 GST_DEBUG ("Get junk and info next");
2697 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
2699 /* Need more data */
2703 case GST_AVI_DEMUX_HEADER_INFO:
2704 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
2706 if (gst_adapter_available (avi->adapter) < 12)
2709 data = gst_adapter_peek (avi->adapter, 12);
2710 tag = GST_READ_UINT32_LE (data);
2711 size = GST_READ_UINT32_LE (data + 4);
2712 ltag = GST_READ_UINT32_LE (data + 8);
2714 if (tag == GST_RIFF_TAG_LIST) {
2716 case GST_RIFF_LIST_movi:
2717 gst_adapter_flush (avi->adapter, 12);
2720 case GST_RIFF_LIST_INFO:
2721 GST_DEBUG ("Found INFO chunk");
2722 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2723 GST_DEBUG ("got size %d", size);
2725 gst_adapter_flush (avi->adapter, 12);
2727 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
2730 gst_adapter_flush (avi->adapter, 1);
2731 gst_riff_parse_info (GST_ELEMENT (avi), buf,
2733 gst_buffer_unref (buf);
2735 avi->offset += GST_ROUND_UP_2 (size) - 4;
2737 GST_DEBUG ("skipping INFO LIST prefix");
2740 /* Need more data */
2745 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2746 avi->offset += 8 + GST_ROUND_UP_2 (size);
2747 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
2748 // ??? goto iterate; ???
2750 /* Need more data */
2756 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2757 avi->offset += 8 + GST_ROUND_UP_2 (size);
2758 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
2761 /* Need more data */
2768 GST_WARNING ("unhandled header state: %d", avi->header_state);
2773 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
2774 avi->num_streams, avi->stream[0].indexes);
2776 GST_DEBUG ("Found movi chunk. Starting to stream data");
2777 avi->state = GST_AVI_DEMUX_MOVI;
2779 /* no indexes in push mode, but it still sets some variables */
2780 gst_avi_demux_calculate_durations_from_index (avi);
2782 /* create initial NEWSEGMENT event */
2783 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
2784 stop = avi->segment.duration;
2786 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
2788 if (avi->seek_event)
2789 gst_event_unref (avi->seek_event);
2790 avi->seek_event = gst_event_new_new_segment
2791 (FALSE, avi->segment.rate, GST_FORMAT_TIME,
2792 avi->segment.start, stop, avi->segment.time);
2794 /* at this point we know all the streams and we can signal the no more
2796 GST_DEBUG_OBJECT (avi, "signaling no more pads");
2797 gst_element_no_more_pads (GST_ELEMENT (avi));
2804 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
2805 return GST_FLOW_ERROR;
2809 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
2810 ("Invalid AVI header (no LIST at start): %"
2811 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
2812 return GST_FLOW_ERROR;
2816 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
2817 ("Invalid AVI header (no hdrl at start): %"
2818 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
2819 gst_buffer_unref (buf);
2820 return GST_FLOW_ERROR;
2824 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
2825 ("Invalid AVI header (no avih at start): %"
2826 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
2828 gst_buffer_unref (sub);
2830 gst_buffer_unref (buf);
2831 return GST_FLOW_ERROR;
2835 gst_buffer_unref (buf);
2836 return GST_FLOW_ERROR;
2841 * Read full AVI headers.
2843 static GstFlowReturn
2844 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
2847 GstBuffer *buf, *sub = NULL;
2851 GstElement *element = GST_ELEMENT_CAST (avi);
2854 stamp = gst_util_get_timestamp ();
2856 /* the header consists of a 'hdrl' LIST tag */
2857 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
2858 if (res != GST_FLOW_OK)
2859 goto pull_range_failed;
2860 else if (tag != GST_RIFF_TAG_LIST)
2862 else if (GST_BUFFER_SIZE (buf) < 4)
2865 GST_DEBUG_OBJECT (avi, "parsing headers");
2867 /* Find the 'hdrl' LIST tag */
2868 while (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl) {
2869 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
2870 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf))));
2873 gst_buffer_unref (buf);
2875 /* read new chunk */
2876 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
2877 if (res != GST_FLOW_OK)
2878 goto pull_range_failed;
2879 else if (tag != GST_RIFF_TAG_LIST)
2881 else if (GST_BUFFER_SIZE (buf) < 4)
2885 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
2887 /* the hdrl starts with a 'avih' header */
2888 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
2890 else if (tag != GST_RIFF_TAG_avih)
2892 else if (!gst_avi_demux_parse_avih (element, sub, &avi->avih))
2895 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
2897 /* now, read the elements from the header until the end */
2898 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
2899 /* sub can be NULL on empty tags */
2904 case GST_RIFF_TAG_LIST:
2909 if (GST_BUFFER_SIZE (sub) < 4)
2912 data = GST_BUFFER_DATA (sub);
2913 fourcc = GST_READ_UINT32_LE (data);
2916 case GST_RIFF_LIST_strl:
2917 if (!(gst_avi_demux_parse_stream (avi, sub))) {
2918 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
2919 ("failed to parse stream, ignoring"));
2924 case GST_RIFF_LIST_odml:
2925 gst_avi_demux_parse_odml (avi, sub);
2929 GST_WARNING_OBJECT (avi,
2930 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
2931 GST_FOURCC_ARGS (fourcc));
2932 GST_MEMDUMP_OBJECT (avi, "Unknown list", GST_BUFFER_DATA (sub),
2933 GST_BUFFER_SIZE (sub));
2935 case GST_RIFF_TAG_JUNK:
2941 GST_WARNING_OBJECT (avi,
2942 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
2943 GST_FOURCC_ARGS (tag), offset);
2944 GST_MEMDUMP_OBJECT (avi, "Unknown tag", GST_BUFFER_DATA (sub),
2945 GST_BUFFER_SIZE (sub));
2947 case GST_RIFF_TAG_JUNK:
2950 gst_buffer_unref (sub);
2955 gst_buffer_unref (buf);
2956 GST_DEBUG ("elements parsed");
2958 /* check parsed streams */
2959 if (avi->num_streams == 0)
2961 else if (avi->num_streams != avi->avih->streams) {
2962 GST_WARNING_OBJECT (avi,
2963 "Stream header mentioned %d streams, but %d available",
2964 avi->avih->streams, avi->num_streams);
2967 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
2968 G_GUINT64_FORMAT, avi->offset);
2970 /* Now, find the data (i.e. skip all junk between header and data) */
2976 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
2977 if (res != GST_FLOW_OK) {
2978 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
2979 goto pull_range_failed;
2980 } else if (GST_BUFFER_SIZE (buf) < 12) {
2981 GST_DEBUG_OBJECT (avi, "got %d bytes which is less than 12 bytes",
2982 GST_BUFFER_SIZE (buf));
2983 gst_buffer_unref (buf);
2984 return GST_FLOW_ERROR;
2987 data = GST_BUFFER_DATA (buf);
2989 tag = GST_READ_UINT32_LE (data);
2990 size = GST_READ_UINT32_LE (data + 4);
2991 ltag = GST_READ_UINT32_LE (data + 8);
2993 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
2994 GST_FOURCC_ARGS (tag), size);
2995 GST_MEMDUMP ("Tag content", data, GST_BUFFER_SIZE (buf));
2996 gst_buffer_unref (buf);
2999 case GST_RIFF_TAG_LIST:{
3001 case GST_RIFF_LIST_movi:
3002 GST_DEBUG_OBJECT (avi,
3003 "Reached the 'movi' tag, we're done with skipping");
3005 case GST_RIFF_LIST_INFO:
3007 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3009 if (res != GST_FLOW_OK) {
3010 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3011 goto pull_range_failed;
3013 GST_DEBUG ("got size %u", GST_BUFFER_SIZE (buf));
3015 GST_DEBUG ("skipping INFO LIST prefix");
3016 avi->offset += (4 - GST_ROUND_UP_2 (size));
3017 gst_buffer_unref (buf);
3021 sub = gst_buffer_create_sub (buf, 4, GST_BUFFER_SIZE (buf) - 4);
3022 gst_riff_parse_info (element, sub, &avi->globaltags);
3024 gst_buffer_unref (sub);
3027 gst_buffer_unref (buf);
3028 /* gst_riff_read_chunk() has already advanced avi->offset */
3031 GST_WARNING_OBJECT (avi,
3032 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3033 GST_FOURCC_ARGS (ltag));
3034 avi->offset += 8 + GST_ROUND_UP_2 (size);
3040 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3041 GST_FOURCC_ARGS (tag));
3043 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3044 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3045 avi->offset += 8 + GST_ROUND_UP_2 (size);
3051 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3052 avi->num_streams, avi->stream[0].indexes);
3054 /* create or read stream index (for seeking) */
3055 if (avi->stream[0].indexes != NULL) {
3056 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3057 gst_avi_demux_read_subindexes_pull (avi);
3059 if (!avi->have_index) {
3060 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
3061 gst_avi_demux_stream_index (avi);
3063 /* still no index, scan */
3064 if (!avi->have_index) {
3065 gst_avi_demux_stream_scan (avi);
3067 /* still no index.. this is a fatal error for now.
3068 * FIXME, we should switch to plain push mode without seeking
3069 * instead of failing. */
3070 if (!avi->have_index)
3074 /* use the indexes now to construct nice durations */
3075 gst_avi_demux_calculate_durations_from_index (avi);
3077 /* create initial NEWSEGMENT event */
3078 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3079 stop = avi->segment.duration;
3081 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3083 /* do initial seek to the default segment values */
3084 gst_avi_demux_do_seek (avi, &avi->segment);
3085 /* prepare initial segment */
3086 if (avi->seek_event)
3087 gst_event_unref (avi->seek_event);
3088 avi->seek_event = gst_event_new_new_segment
3089 (FALSE, avi->segment.rate, GST_FORMAT_TIME,
3090 avi->segment.start, stop, avi->segment.time);
3092 stamp = gst_util_get_timestamp () - stamp;
3093 GST_DEBUG_OBJECT (avi, "pulling header %" GST_TIME_FORMAT,
3094 GST_TIME_ARGS (stamp));
3096 /* at this point we know all the streams and we can signal the no more
3098 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3099 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3106 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3107 ("Invalid AVI header (no LIST at start): %"
3108 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3109 gst_buffer_unref (buf);
3110 return GST_FLOW_ERROR;
3114 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3115 ("Invalid AVI header (no hdrl at start): %"
3116 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3117 gst_buffer_unref (buf);
3118 return GST_FLOW_ERROR;
3122 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3123 ("Invalid AVI header (no avih at start): %"
3124 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3126 gst_buffer_unref (sub);
3127 gst_buffer_unref (buf);
3128 return GST_FLOW_ERROR;
3132 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3133 ("Invalid AVI header (cannot parse avih at start)"));
3134 gst_buffer_unref (buf);
3135 return GST_FLOW_ERROR;
3139 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3140 return GST_FLOW_ERROR;
3144 GST_WARNING ("file without or too big index");
3145 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3146 ("Could not get/create index"));
3147 return GST_FLOW_ERROR;
3151 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3152 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3153 return GST_FLOW_ERROR;
3157 /* move a stream to @index */
3159 gst_avi_demux_move_stream (GstAviDemux * avi, GstAviStream * stream,
3160 GstSegment * segment, guint index)
3162 GST_DEBUG_OBJECT (avi, "Move stream %d to %u", stream->num, index);
3164 if (segment->rate < 0.0) {
3166 /* Because we don't know the frame order we need to push from the prev keyframe
3167 * to the next keyframe. If there is a smart decoder downstream he will notice
3168 * that there are too many encoded frames send and return UNEXPECTED when there
3169 * are enough decoded frames to fill the segment. */
3170 next_key = gst_avi_demux_index_next (avi, stream, index, TRUE);
3172 /* FIXME, we go back to 0, we should look at segment.start. We will however
3173 * stop earlier when the see the timestamp < segment.start */
3174 stream->start_entry = 0;
3175 stream->step_entry = index;
3176 stream->current_entry = index;
3177 stream->stop_entry = next_key;
3179 GST_DEBUG_OBJECT (avi, "reverse seek: start %u, step %u, stop %u",
3180 stream->start_entry, stream->step_entry, stream->stop_entry);
3182 stream->start_entry = index;
3183 stream->step_entry = index;
3184 stream->stop_entry = gst_avi_demux_index_last (avi, stream);
3186 if (stream->current_entry != index) {
3187 GST_DEBUG_OBJECT (avi, "Move DISCONT from %u to %u",
3188 stream->current_entry, index);
3189 stream->current_entry = index;
3190 stream->discont = TRUE;
3193 /* update the buffer info */
3194 gst_avi_demux_get_buffer_info (avi, stream, index,
3195 &stream->current_timestamp, &stream->current_ts_end,
3196 &stream->current_offset, &stream->current_offset_end);
3198 GST_DEBUG_OBJECT (avi, "Moved to %u, ts %" GST_TIME_FORMAT
3199 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
3200 ", off_end %" G_GUINT64_FORMAT, index,
3201 GST_TIME_ARGS (stream->current_timestamp),
3202 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
3203 stream->current_offset_end);
3207 * Do the actual seeking.
3210 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
3212 GstClockTime seek_time;
3215 GstAviStream *stream;
3217 seek_time = segment->last_stop;
3218 keyframe = !!(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
3220 GST_DEBUG_OBJECT (avi, "seek to: %" GST_TIME_FORMAT
3221 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
3223 /* FIXME, this code assumes the main stream with keyframes is stream 0,
3224 * which is mostly correct... */
3225 stream = &avi->stream[0];
3227 /* get the entry index for the requested position */
3228 index = gst_avi_demux_index_for_time (avi, stream, seek_time);
3229 GST_DEBUG_OBJECT (avi, "Got entry %u", index);
3231 /* check if we are already on a keyframe */
3232 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
3233 GST_DEBUG_OBJECT (avi, "not keyframe, searching back");
3234 /* now go to the previous keyframe, this is where we should start
3236 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
3237 GST_DEBUG_OBJECT (avi, "previous keyframe at %u", index);
3240 /* move the main stream to this position */
3241 gst_avi_demux_move_stream (avi, stream, segment, index);
3244 /* when seeking to a keyframe, we update the result seek time
3245 * to the time of the keyframe. */
3246 seek_time = stream->current_timestamp;
3247 GST_DEBUG_OBJECT (avi, "keyframe adjusted to %" GST_TIME_FORMAT,
3248 GST_TIME_ARGS (seek_time));
3251 /* the seek time is also the last_stop and stream time when going
3253 segment->last_stop = seek_time;
3254 if (segment->rate > 0.0)
3255 segment->time = seek_time;
3257 /* now set DISCONT and align the other streams */
3258 for (i = 0; i < avi->num_streams; i++) {
3259 GstAviStream *ostream;
3261 ostream = &avi->stream[i];
3262 if (ostream == stream)
3265 /* get the entry index for the requested position */
3266 index = gst_avi_demux_index_for_time (avi, ostream, seek_time);
3268 /* move to previous keyframe */
3269 if (!ENTRY_IS_KEYFRAME (&ostream->index[index]))
3270 index = gst_avi_demux_index_prev (avi, ostream, index, TRUE);
3272 gst_avi_demux_move_stream (avi, ostream, segment, index);
3274 GST_DEBUG_OBJECT (avi, "done seek to: %" GST_TIME_FORMAT,
3275 GST_TIME_ARGS (seek_time));
3281 * Handle seek event.
3284 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
3289 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
3293 GstSegment seeksegment = { 0, };
3297 GST_DEBUG_OBJECT (avi, "doing seek with event");
3299 gst_event_parse_seek (event, &rate, &format, &flags,
3300 &cur_type, &cur, &stop_type, &stop);
3302 /* we have to have a format as the segment format. Try to convert
3304 if (format != GST_FORMAT_TIME) {
3305 GstFormat fmt = GST_FORMAT_TIME;
3306 gboolean res = TRUE;
3308 if (cur_type != GST_SEEK_TYPE_NONE)
3309 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
3310 if (res && stop_type != GST_SEEK_TYPE_NONE)
3311 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
3317 GST_DEBUG_OBJECT (avi,
3318 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
3319 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
3320 /* FIXME: can we do anything with rate!=1.0 */
3322 GST_DEBUG_OBJECT (avi, "doing seek without event");
3327 /* save flush flag */
3328 flush = flags & GST_SEEK_FLAG_FLUSH;
3331 GstEvent *fevent = gst_event_new_flush_start ();
3333 /* for a flushing seek, we send a flush_start on all pads. This will
3334 * eventually stop streaming with a WRONG_STATE. We can thus eventually
3335 * take the STREAM_LOCK. */
3336 GST_DEBUG_OBJECT (avi, "sending flush start");
3337 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
3338 gst_pad_push_event (avi->sinkpad, fevent);
3340 /* a non-flushing seek, we PAUSE the task so that we can take the
3342 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
3343 gst_pad_pause_task (avi->sinkpad);
3346 /* wait for streaming to stop */
3347 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
3348 GST_PAD_STREAM_LOCK (avi->sinkpad);
3350 /* copy segment, we need this because we still need the old
3351 * segment when we close the current segment. */
3352 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
3355 GST_DEBUG_OBJECT (avi, "configuring seek");
3356 gst_segment_set_seek (&seeksegment, rate, format, flags,
3357 cur_type, cur, stop_type, stop, &update);
3359 /* do the seek, seeksegment.last_stop contains the new position, this
3360 * actually never fails. */
3361 gst_avi_demux_do_seek (avi, &seeksegment);
3364 GstEvent *fevent = gst_event_new_flush_stop ();
3366 GST_DEBUG_OBJECT (avi, "sending flush stop");
3367 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
3368 gst_pad_push_event (avi->sinkpad, fevent);
3370 /* reset the last flow and mark discont, FLUSH is always DISCONT */
3371 for (i = 0; i < avi->num_streams; i++) {
3372 avi->stream[i].last_flow = GST_FLOW_OK;
3373 avi->stream[i].discont = TRUE;
3375 } else if (avi->segment_running) {
3378 /* we are running the current segment and doing a non-flushing seek,
3379 * close the segment first based on the last_stop. */
3380 GST_DEBUG_OBJECT (avi, "closing running segment %" G_GINT64_FORMAT
3381 " to %" G_GINT64_FORMAT, avi->segment.start, avi->segment.last_stop);
3382 seg = gst_event_new_new_segment (TRUE,
3383 avi->segment.rate, avi->segment.format,
3384 avi->segment.start, avi->segment.last_stop, avi->segment.time);
3385 gst_avi_demux_push_event (avi, seg);
3388 /* now update the real segment info */
3389 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
3391 /* post the SEGMENT_START message when we do segmented playback */
3392 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3393 gst_element_post_message (GST_ELEMENT (avi),
3394 gst_message_new_segment_start (GST_OBJECT (avi),
3395 avi->segment.format, avi->segment.last_stop));
3398 /* prepare for streaming again */
3399 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3400 stop = avi->segment.duration;
3402 /* queue the segment event for the streaming thread. */
3403 if (avi->seek_event)
3404 gst_event_unref (avi->seek_event);
3405 if (avi->segment.rate > 0.0) {
3406 /* forwards goes from last_stop to stop */
3407 avi->seek_event = gst_event_new_new_segment (FALSE,
3408 avi->segment.rate, avi->segment.format,
3409 avi->segment.last_stop, stop, avi->segment.time);
3411 /* reverse goes from start to last_stop */
3412 avi->seek_event = gst_event_new_new_segment (FALSE,
3413 avi->segment.rate, avi->segment.format,
3414 avi->segment.start, avi->segment.last_stop, avi->segment.time);
3417 if (!avi->streaming) {
3418 avi->segment_running = TRUE;
3419 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
3422 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
3429 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
3435 * Helper for gst_avi_demux_invert()
3438 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
3440 memcpy (tmp, d1, bytes);
3441 memcpy (d1, d2, bytes);
3442 memcpy (d2, tmp, bytes);
3446 #define gst_avi_demux_is_uncompressed(fourcc) \
3447 (fourcc == GST_RIFF_DIB || \
3448 fourcc == GST_RIFF_rgb || \
3449 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW)
3452 * Invert DIB buffers... Takes existing buffer and
3453 * returns either the buffer or a new one (with old
3454 * one dereferenced).
3455 * FIXME: can't we preallocate tmp? and remember stride, bpp?
3458 gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
3465 if (stream->strh->type != GST_RIFF_FCC_vids)
3468 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
3469 return buf; /* Ignore non DIB buffers */
3472 s = gst_caps_get_structure (GST_PAD_CAPS (stream->pad), 0);
3473 if (!gst_structure_get_int (s, "bpp", &bpp)) {
3474 GST_WARNING ("Failed to retrieve depth from caps");
3478 if (stream->strf.vids == NULL) {
3479 GST_WARNING ("Failed to retrieve vids for stream");
3483 h = stream->strf.vids->height;
3484 w = stream->strf.vids->width;
3485 stride = w * (bpp / 8);
3487 buf = gst_buffer_make_writable (buf);
3488 if (GST_BUFFER_SIZE (buf) < (stride * h)) {
3489 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
3493 tmp = g_malloc (stride);
3495 for (y = 0; y < h / 2; y++) {
3496 swap_line (GST_BUFFER_DATA (buf) + stride * y,
3497 GST_BUFFER_DATA (buf) + stride * (h - 1 - y), tmp, stride);
3506 * Returns the aggregated GstFlowReturn.
3508 static GstFlowReturn
3509 gst_avi_demux_combine_flows (GstAviDemux * avi, GstAviStream * stream,
3514 /* store the value */
3515 stream->last_flow = ret;
3517 /* any other error that is not-linked can be returned right away */
3518 if (G_UNLIKELY (ret != GST_FLOW_NOT_LINKED))
3521 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3522 for (i = 0; i < avi->num_streams; i++) {
3523 GstAviStream *ostream = &avi->stream[i];
3525 ret = ostream->last_flow;
3526 /* some other return value (must be SUCCESS but we can return
3527 * other values as well) */
3528 if (G_UNLIKELY (ret != GST_FLOW_NOT_LINKED))
3531 /* if we get here, all other pads were unlinked and we return
3532 * NOT_LINKED then */
3534 GST_LOG_OBJECT (avi, "combined %s to return %s",
3535 gst_flow_get_name (stream->last_flow), gst_flow_get_name (ret));
3539 /* move @stream to the next position in its index */
3540 static GstFlowReturn
3541 gst_avi_demux_advance (GstAviDemux * avi, GstAviStream * stream,
3544 guint old_entry, new_entry;
3546 old_entry = stream->current_entry;
3548 new_entry = old_entry + 1;
3550 /* see if we reached the end */
3551 if (new_entry >= stream->stop_entry) {
3552 if (avi->segment.rate < 0.0) {
3553 if (stream->step_entry == stream->start_entry) {
3554 /* we stepped all the way to the start, eos */
3555 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
3558 /* backwards, stop becomes step, find a new step */
3559 stream->stop_entry = stream->step_entry;
3560 stream->step_entry = gst_avi_demux_index_prev (avi, stream,
3561 stream->stop_entry, TRUE);
3563 GST_DEBUG_OBJECT (avi,
3564 "reverse playback jump: start %u, step %u, stop %u",
3565 stream->start_entry, stream->step_entry, stream->stop_entry);
3567 /* and start from the previous keyframe now */
3568 new_entry = stream->step_entry;
3571 GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
3576 if (new_entry != old_entry) {
3577 stream->current_entry = new_entry;
3578 stream->current_total = stream->index[new_entry].total;
3580 if (new_entry == old_entry + 1) {
3581 GST_DEBUG_OBJECT (avi, "moved forwards from %u to %u",
3582 old_entry, new_entry);
3583 /* we simply moved one step forwards, reuse current info */
3584 stream->current_timestamp = stream->current_ts_end;
3585 stream->current_offset = stream->current_offset_end;
3586 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
3587 NULL, &stream->current_ts_end, NULL, &stream->current_offset_end);
3589 /* we moved DISCONT, full update */
3590 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
3591 &stream->current_timestamp, &stream->current_ts_end,
3592 &stream->current_offset, &stream->current_offset_end);
3593 /* and MARK discont for this stream */
3594 stream->last_flow = GST_FLOW_OK;
3595 stream->discont = TRUE;
3596 GST_DEBUG_OBJECT (avi, "Moved from %u to %u, ts %" GST_TIME_FORMAT
3597 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
3598 ", off_end %" G_GUINT64_FORMAT, old_entry, new_entry,
3599 GST_TIME_ARGS (stream->current_timestamp),
3600 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
3601 stream->current_offset_end);
3609 GST_DEBUG_OBJECT (avi, "we are EOS");
3610 /* setting current_timestamp to -1 marks EOS */
3611 stream->current_timestamp = -1;
3612 return GST_FLOW_UNEXPECTED;
3616 /* find the stream with the lowest current position when going forwards or with
3617 * the highest position when going backwards, this is the stream
3618 * we should push from next */
3620 gst_avi_demux_find_next (GstAviDemux * avi, gfloat rate)
3622 guint64 min_time, max_time;
3623 guint stream_num, i;
3626 min_time = G_MAXUINT64;
3629 for (i = 0; i < avi->num_streams; i++) {
3631 GstAviStream *stream;
3633 stream = &avi->stream[i];
3634 position = stream->current_timestamp;
3636 /* position of -1 is EOS */
3637 if (position != -1) {
3638 if (rate > 0.0 && position < min_time) {
3639 min_time = position;
3641 } else if (rate < 0.0 && position >= max_time) {
3642 max_time = position;
3650 static GstFlowReturn
3651 gst_avi_demux_loop_data (GstAviDemux * avi)
3653 GstFlowReturn ret = GST_FLOW_OK;
3655 GstAviStream *stream;
3656 gboolean processed = FALSE;
3658 guint64 offset, size;
3659 GstClockTime timestamp, duration;
3660 guint64 out_offset, out_offset_end;
3662 GstAviIndexEntry *entry;
3665 stream_num = gst_avi_demux_find_next (avi, avi->segment.rate);
3668 if (G_UNLIKELY (stream_num == -1)) {
3669 GST_DEBUG_OBJECT (avi, "all streams are EOS");
3673 /* we have the stream now */
3674 stream = &avi->stream[stream_num];
3676 /* skip streams without pads */
3678 GST_DEBUG_OBJECT (avi, "skipping entry from stream %d without pad",
3683 /* get the timing info for the entry */
3684 timestamp = stream->current_timestamp;
3685 duration = stream->current_ts_end - timestamp;
3686 out_offset = stream->current_offset;
3687 out_offset_end = stream->current_offset_end;
3689 /* get the entry data info */
3690 entry = &stream->index[stream->current_entry];
3691 offset = entry->offset;
3693 keyframe = ENTRY_IS_KEYFRAME (entry);
3695 /* skip empty entries */
3697 GST_DEBUG_OBJECT (avi, "Skipping entry %u (%" G_GUINT64_FORMAT ", %p)",
3698 stream->current_entry, size, stream->pad);
3702 if (avi->segment.rate > 0.0) {
3703 /* only check this for fowards playback for now */
3704 if (keyframe && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
3705 && (timestamp > avi->segment.stop)) {
3710 GST_LOG ("reading buffer (size=%" G_GUINT64_FORMAT "), stream %d, pos %"
3711 G_GUINT64_FORMAT " (0x%" G_GINT64_MODIFIER "x), kf %d", size,
3712 stream_num, offset, offset, keyframe);
3714 /* FIXME, check large chunks and cut them up */
3716 /* pull in the data */
3717 ret = gst_pad_pull_range (avi->sinkpad, offset, size, &buf);
3718 if (ret != GST_FLOW_OK)
3721 /* check for short buffers, this is EOS as well */
3722 if (GST_BUFFER_SIZE (buf) < size)
3725 /* invert the picture if needed */
3726 buf = gst_avi_demux_invert (stream, buf);
3728 /* mark non-keyframes */
3730 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
3732 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
3734 GST_BUFFER_TIMESTAMP (buf) = timestamp;
3735 GST_BUFFER_DURATION (buf) = duration;
3736 GST_BUFFER_OFFSET (buf) = out_offset;
3737 GST_BUFFER_OFFSET_END (buf) = out_offset_end;
3739 /* mark discont when pending */
3740 if (stream->discont) {
3741 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
3742 stream->discont = FALSE;
3745 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
3747 /* update current position in the segment */
3748 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, timestamp);
3750 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %u, ts %"
3751 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
3752 ", off_end %" G_GUINT64_FORMAT,
3753 GST_BUFFER_SIZE (buf), GST_TIME_ARGS (timestamp),
3754 GST_TIME_ARGS (duration), out_offset, out_offset_end);
3756 ret = gst_pad_push (stream->pad, buf);
3758 /* mark as processed, we increment the frame and byte counters then
3759 * leave the while loop and return the GstFlowReturn */
3762 if (avi->segment.rate < 0) {
3763 if (timestamp > avi->segment.stop && ret == GST_FLOW_UNEXPECTED) {
3764 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
3765 * we are at the end of the segment, so we just need to jump
3766 * back to the previous section. */
3767 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
3772 /* move to next item */
3773 ret = gst_avi_demux_advance (avi, stream, ret);
3776 ret = gst_avi_demux_combine_flows (avi, stream, ret);
3777 } while (!processed);
3785 GST_DEBUG_OBJECT (avi, "No samples left for any streams - EOS");
3786 ret = GST_FLOW_UNEXPECTED;
3791 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
3792 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
3793 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (avi->segment.stop));
3794 ret = GST_FLOW_UNEXPECTED;
3799 GST_DEBUG_OBJECT (avi, "pull range failed: pos=%" G_GUINT64_FORMAT
3800 " size=%" G_GUINT64_FORMAT, offset, size);
3805 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
3806 ", only got %d/%" G_GUINT64_FORMAT " bytes (truncated file?)", offset,
3807 GST_BUFFER_SIZE (buf), size);
3808 gst_buffer_unref (buf);
3809 ret = GST_FLOW_UNEXPECTED;
3815 * Read data. If we have an index it delegates to
3816 * gst_avi_demux_process_next_entry().
3818 static GstFlowReturn
3819 gst_avi_demux_stream_data (GstAviDemux * avi)
3824 GstFlowReturn res = GST_FLOW_OK;
3825 GstFormat format = GST_FORMAT_TIME;
3827 if (G_UNLIKELY (avi->have_eos)) {
3828 /* Clean adapter, we're done */
3829 gst_adapter_clear (avi->adapter);
3833 /* Iterate until need more data, so adapter won't grow too much */
3835 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
3839 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
3840 GST_FOURCC_ARGS (tag), size);
3842 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
3843 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
3844 GST_LOG ("Chunk ok");
3845 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
3846 GST_DEBUG ("Found sub-index tag");
3847 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
3848 /* accept 0 size buffer here */
3849 avi->abort_buffering = FALSE;
3850 GST_DEBUG (" skipping %d bytes for now", size);
3851 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3854 } else if (tag == GST_RIFF_TAG_idx1) {
3855 GST_DEBUG ("Found index tag, stream done");
3856 avi->have_eos = TRUE;
3857 return GST_FLOW_UNEXPECTED;
3858 } else if (tag == GST_RIFF_TAG_LIST) {
3859 /* movi chunks might be grouped in rec list */
3860 if (gst_adapter_available (avi->adapter) >= 12) {
3861 GST_DEBUG ("Found LIST tag, skipping LIST header");
3862 gst_adapter_flush (avi->adapter, 12);
3866 } else if (tag == GST_RIFF_TAG_JUNK) {
3867 /* rec list might contain JUNK chunks */
3868 GST_DEBUG ("Found JUNK tag");
3869 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
3870 /* accept 0 size buffer here */
3871 avi->abort_buffering = FALSE;
3872 GST_DEBUG (" skipping %d bytes for now", size);
3873 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3877 GST_DEBUG ("No more stream chunks, send EOS");
3878 avi->have_eos = TRUE;
3879 return GST_FLOW_UNEXPECTED;
3882 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
3883 /* supposedly one hopes to catch a nicer chunk later on ... */
3884 /* FIXME ?? give up here rather than possibly ending up going
3885 * through the whole file */
3886 if (avi->abort_buffering) {
3887 avi->abort_buffering = FALSE;
3888 gst_adapter_flush (avi->adapter, 8);
3892 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
3893 GST_FOURCC_ARGS (tag), size);
3895 stream_nr = CHUNKID_TO_STREAMNR (tag);
3897 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
3899 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
3900 stream_nr, GST_FOURCC_ARGS (tag));
3901 avi->offset += 8 + GST_ROUND_UP_2 (size);
3902 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3904 GstAviStream *stream;
3905 GstClockTime next_ts = 0;
3908 gst_adapter_flush (avi->adapter, 8);
3911 buf = gst_adapter_take_buffer (avi->adapter, GST_ROUND_UP_2 (size));
3912 /* patch the size */
3913 GST_BUFFER_SIZE (buf) = size;
3914 avi->offset += 8 + GST_ROUND_UP_2 (size);
3916 stream = &avi->stream[stream_nr];
3918 /* set delay (if any)
3919 if (stream->strh->init_frames == stream->current_frame &&
3921 stream->delay = next_ts;
3924 /* parsing of corresponding header may have failed */
3925 if (G_UNLIKELY (!stream->pad)) {
3926 GST_WARNING_OBJECT (avi, "no pad for stream ID %" GST_FOURCC_FORMAT,
3927 GST_FOURCC_ARGS (tag));
3928 gst_buffer_unref (buf);
3930 GstClockTime dur_ts = 0;
3932 /* get time of this buffer */
3933 gst_pad_query_position (stream->pad, &format, (gint64 *) & next_ts);
3934 if (G_UNLIKELY (format != GST_FORMAT_TIME))
3937 /* increment our positions */
3938 stream->current_entry++;
3939 stream->current_total += size;
3941 /* invert the picture if needed */
3942 buf = gst_avi_demux_invert (stream, buf);
3944 gst_pad_query_position (stream->pad, &format, (gint64 *) & dur_ts);
3945 if (G_UNLIKELY (format != GST_FORMAT_TIME))
3948 GST_BUFFER_TIMESTAMP (buf) = next_ts;
3949 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
3950 if (stream->strh->type == GST_RIFF_FCC_vids) {
3951 GST_BUFFER_OFFSET (buf) = stream->current_entry - 1;
3952 GST_BUFFER_OFFSET_END (buf) = stream->current_entry;
3954 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
3955 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
3958 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
3959 GST_DEBUG_OBJECT (avi,
3960 "Pushing buffer with time=%" GST_TIME_FORMAT ", duration %"
3961 GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
3962 " and size %d over pad %s", GST_TIME_ARGS (next_ts),
3963 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), GST_BUFFER_OFFSET (buf),
3964 size, GST_PAD_NAME (stream->pad));
3966 /* update current position in the segment */
3967 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, next_ts);
3969 /* mark discont when pending */
3970 if (G_UNLIKELY (stream->discont)) {
3971 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
3972 stream->discont = FALSE;
3974 res = gst_pad_push (stream->pad, buf);
3977 res = gst_avi_demux_combine_flows (avi, stream, res);
3978 if (G_UNLIKELY (res != GST_FLOW_OK)) {
3979 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
3992 GST_DEBUG_OBJECT (avi, "format %s != GST_FORMAT_TIME",
3993 gst_format_get_name (format));
3994 res = GST_FLOW_ERROR;
4000 * Send pending tags.
4003 push_tag_lists (GstAviDemux * avi)
4011 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
4013 for (i = 0; i < avi->num_streams; i++) {
4014 GstAviStream *stream = &avi->stream[i];
4015 GstPad *pad = stream->pad;
4017 tags = stream->taglist;
4020 GST_DEBUG_OBJECT (pad, "Tags: %" GST_PTR_FORMAT, tags);
4022 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (avi), pad, tags);
4023 stream->taglist = NULL;
4027 if (!(tags = avi->globaltags))
4028 tags = gst_tag_list_new ();
4030 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
4031 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
4033 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, tags);
4034 gst_element_found_tags (GST_ELEMENT_CAST (avi), tags);
4035 avi->globaltags = NULL;
4036 avi->got_tags = FALSE;
4040 gst_avi_demux_loop (GstPad * pad)
4043 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4045 switch (avi->state) {
4046 case GST_AVI_DEMUX_START:
4047 res = gst_avi_demux_stream_init_pull (avi);
4048 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4049 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4052 avi->state = GST_AVI_DEMUX_HEADER;
4054 case GST_AVI_DEMUX_HEADER:
4055 res = gst_avi_demux_stream_header_pull (avi);
4056 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4057 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4060 avi->state = GST_AVI_DEMUX_MOVI;
4062 case GST_AVI_DEMUX_MOVI:
4063 if (G_UNLIKELY (avi->seek_event)) {
4064 gst_avi_demux_push_event (avi, avi->seek_event);
4065 avi->seek_event = NULL;
4067 if (G_UNLIKELY (avi->got_tags)) {
4068 push_tag_lists (avi);
4070 /* process each index entry in turn */
4071 res = gst_avi_demux_loop_data (avi);
4073 /* pause when error */
4074 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4075 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
4080 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
4081 res = GST_FLOW_ERROR;
4089 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
4090 avi->segment_running = FALSE;
4091 gst_pad_pause_task (avi->sinkpad);
4093 if (GST_FLOW_IS_FATAL (res) || (res == GST_FLOW_NOT_LINKED)) {
4094 gboolean push_eos = TRUE;
4096 if (res == GST_FLOW_UNEXPECTED) {
4097 /* handle end-of-stream/segment */
4098 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4101 if ((stop = avi->segment.stop) == -1)
4102 stop = avi->segment.duration;
4104 GST_INFO_OBJECT (avi, "sending segment_done");
4106 gst_element_post_message
4108 gst_message_new_segment_done (GST_OBJECT (avi), GST_FORMAT_TIME,
4113 /* for fatal errors we post an error message */
4114 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
4115 (_("Internal data stream error.")),
4116 ("streaming stopped, reason %s", gst_flow_get_name (res)));
4119 GST_INFO_OBJECT (avi, "sending eos");
4120 if (!gst_avi_demux_push_event (avi, gst_event_new_eos ()) &&
4121 (res == GST_FLOW_UNEXPECTED)) {
4122 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
4123 (NULL), ("got eos but no streams (yet)"));
4130 static GstFlowReturn
4131 gst_avi_demux_chain (GstPad * pad, GstBuffer * buf)
4134 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4136 GST_DEBUG ("Store %d bytes in adapter", GST_BUFFER_SIZE (buf));
4137 gst_adapter_push (avi->adapter, buf);
4139 switch (avi->state) {
4140 case GST_AVI_DEMUX_START:
4141 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
4142 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4146 case GST_AVI_DEMUX_HEADER:
4147 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
4148 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4152 case GST_AVI_DEMUX_MOVI:
4153 if (G_UNLIKELY (avi->seek_event)) {
4154 gst_avi_demux_push_event (avi, avi->seek_event);
4155 avi->seek_event = NULL;
4157 if (G_UNLIKELY (avi->got_tags)) {
4158 push_tag_lists (avi);
4160 res = gst_avi_demux_stream_data (avi);
4163 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
4164 ("Illegal internal state"));
4165 res = GST_FLOW_ERROR;
4169 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
4170 gst_flow_get_name (res));
4172 if (G_UNLIKELY (avi->abort_buffering)) {
4173 avi->abort_buffering = FALSE;
4174 res = GST_FLOW_ERROR;
4175 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
4182 gst_avi_demux_sink_activate (GstPad * sinkpad)
4184 if (gst_pad_check_pull_range (sinkpad)) {
4185 GST_DEBUG ("going to pull mode");
4186 return gst_pad_activate_pull (sinkpad, TRUE);
4188 GST_DEBUG ("going to push (streaming) mode");
4189 return gst_pad_activate_push (sinkpad, TRUE);
4194 gst_avi_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
4196 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (sinkpad));
4199 avi->segment_running = TRUE;
4200 avi->streaming = FALSE;
4201 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4204 avi->segment_running = FALSE;
4205 return gst_pad_stop_task (sinkpad);
4210 gst_avi_demux_activate_push (GstPad * pad, gboolean active)
4212 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (pad));
4215 GST_DEBUG ("avi: activating push/chain function");
4216 avi->streaming = TRUE;
4218 GST_DEBUG ("avi: deactivating push/chain function");
4224 static GstStateChangeReturn
4225 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
4227 GstStateChangeReturn ret;
4228 GstAviDemux *avi = GST_AVI_DEMUX (element);
4230 switch (transition) {
4231 case GST_STATE_CHANGE_READY_TO_PAUSED:
4232 avi->streaming = FALSE;
4233 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
4239 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4240 if (ret == GST_STATE_CHANGE_FAILURE)
4243 switch (transition) {
4244 case GST_STATE_CHANGE_PAUSED_TO_READY:
4245 gst_avi_demux_reset (avi);