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>
55 GST_DEBUG_CATEGORY_STATIC (avidemux_debug);
56 #define GST_CAT_DEFAULT avidemux_debug
58 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_EVENT);
60 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
63 GST_STATIC_CAPS ("video/x-msvideo")
66 static void gst_avi_demux_base_init (GstAviDemuxClass * klass);
67 static void gst_avi_demux_class_init (GstAviDemuxClass * klass);
68 static void gst_avi_demux_init (GstAviDemux * avi);
69 static void gst_avi_demux_finalize (GObject * object);
71 static void gst_avi_demux_reset (GstAviDemux * avi);
74 static const GstEventMask *gst_avi_demux_get_event_mask (GstPad * pad);
76 static gboolean gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event);
77 static gboolean gst_avi_demux_handle_sink_event (GstPad * pad,
79 static gboolean gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event);
82 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
84 static const GstQueryType *gst_avi_demux_get_src_query_types (GstPad * pad);
85 static gboolean gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query);
86 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
87 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
89 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
90 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
92 static void gst_avi_demux_loop (GstPad * pad);
93 static gboolean gst_avi_demux_sink_activate (GstPad * sinkpad);
94 static gboolean gst_avi_demux_sink_activate_pull (GstPad * sinkpad,
96 static gboolean gst_avi_demux_activate_push (GstPad * pad, gboolean active);
97 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstBuffer * buf);
99 static GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
100 GstStateChange transition);
102 static GstElementClass *parent_class = NULL;
104 /* GObject methods */
107 gst_avi_demux_get_type (void)
109 static GType avi_demux_type = 0;
111 if (!avi_demux_type) {
112 static const GTypeInfo avi_demux_info = {
113 sizeof (GstAviDemuxClass),
114 (GBaseInitFunc) gst_avi_demux_base_init,
116 (GClassInitFunc) gst_avi_demux_class_init,
119 sizeof (GstAviDemux),
121 (GInstanceInitFunc) gst_avi_demux_init,
125 g_type_register_static (GST_TYPE_ELEMENT,
126 "GstAviDemux", &avi_demux_info, 0);
129 return avi_demux_type;
133 gst_avi_demux_base_init (GstAviDemuxClass * klass)
135 static const GstElementDetails gst_avi_demux_details =
136 GST_ELEMENT_DETAILS ("Avi demuxer",
138 "Demultiplex an avi file into audio and video",
139 "Erik Walthinsen <omega@cse.ogi.edu>\n"
140 "Wim Taymans <wim.taymans@chello.be>\n"
141 "Ronald Bultje <rbultje@ronald.bitfreak.net>\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 (GstAviDemux * avi)
223 GST_DEBUG ("AVI: reset");
225 for (i = 0; i < avi->num_streams; i++) {
226 g_free (avi->stream[i].strh);
227 g_free (avi->stream[i].strf.data);
228 if (avi->stream[i].name)
229 g_free (avi->stream[i].name);
230 if (avi->stream[i].initdata)
231 gst_buffer_unref (avi->stream[i].initdata);
232 if (avi->stream[i].extradata)
233 gst_buffer_unref (avi->stream[i].extradata);
234 if (avi->stream[i].pad) {
235 gst_pad_set_active (avi->stream[i].pad, FALSE);
236 gst_element_remove_pad (GST_ELEMENT (avi), avi->stream[i].pad);
238 if (avi->stream[i].taglist) {
239 gst_tag_list_free (avi->stream[i].taglist);
240 avi->stream[i].taglist = NULL;
243 memset (&avi->stream, 0, sizeof (avi->stream));
245 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
246 avi->num_streams = 0;
247 avi->num_v_streams = 0;
248 avi->num_a_streams = 0;
249 avi->num_t_streams = 0;
251 avi->state = GST_AVI_DEMUX_START;
254 g_free (avi->index_entries);
255 avi->index_entries = NULL;
257 avi->index_offset = 0;
258 avi->current_entry = 0;
262 if (avi->seek_event) {
263 gst_event_unref (avi->seek_event);
264 avi->seek_event = NULL;
268 gst_tag_list_free (avi->globaltags);
269 avi->globaltags = NULL;
271 avi->got_tags = TRUE; /* we always want to push global tags */
272 avi->have_eos = FALSE;
274 gst_adapter_clear (avi->adapter);
276 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
280 static gst_avi_index_entry *
281 gst_avi_demux_index_last (GstAviDemux * avi, gint stream_nr)
284 gst_avi_index_entry *result = NULL;
286 for (i = avi->index_size - 1; i >= 0; i--) {
287 if (avi->index_entries[i].stream_nr == stream_nr) {
288 result = &avi->index_entries[i];
295 static gst_avi_index_entry *
296 gst_avi_demux_index_next (GstAviDemux * avi, gint stream_nr, gint last,
300 gst_avi_index_entry *result = NULL, *entry;
302 for (i = last + 1; i < avi->index_size; i++) {
303 entry = &avi->index_entries[i];
305 if (entry->stream_nr != stream_nr)
308 if ((entry->flags & flags) == flags) {
316 static gst_avi_index_entry *
317 gst_avi_demux_index_prev (GstAviDemux * avi, gint stream_nr, gint last,
321 gst_avi_index_entry *result = NULL, *entry;
323 for (i = last - 1; i >= 0; i--) {
324 entry = &avi->index_entries[i];
326 if (entry->stream_nr != stream_nr)
329 if ((entry->flags & flags) == flags) {
338 gst_avi_demux_index_entry_search (gst_avi_index_entry * entry, guint64 * time)
340 if (entry->ts < *time)
342 else if (entry->ts > *time)
348 * gst_avi_index_entry:
350 * @stream_nr: stream number
351 * @time: seek time position
353 * Finds the index entry which time is less or equal than the requested time.
355 * Returns: the found index entry or %NULL
357 static gst_avi_index_entry *
358 gst_avi_demux_index_entry_for_time (GstAviDemux * avi,
359 gint stream_nr, guint64 time)
361 gst_avi_index_entry *entry = NULL;
364 GST_LOG_OBJECT (avi, "stream_nr:%d , time:%" GST_TIME_FORMAT,
365 stream_nr, GST_TIME_ARGS (time));
367 entry = gst_util_array_binary_search (avi->index_entries,
369 sizeof (gst_avi_index_entry),
370 (GCompareDataFunc) gst_avi_demux_index_entry_search,
371 GST_SEARCH_MODE_BEFORE, &time, NULL);
373 n = entry - avi->index_entries;
375 entry = &avi->index_entries[0];
377 while (entry->stream_nr != stream_nr && n < avi->index_size - 1) {
379 entry = &avi->index_entries[n];
381 } else if (entry->stream_nr != stream_nr) {
382 while (entry->stream_nr != stream_nr && n > 0) {
384 entry = &avi->index_entries[n];
389 "best at entry %u / ts:%" GST_TIME_FORMAT " / dur:%" GST_TIME_FORMAT
390 " flags:%02x", n, GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (entry->dur),
396 /* GstElement methods */
399 static const GstFormat *
400 gst_avi_demux_get_src_formats (GstPad * pad)
402 avi_stream_context *stream = gst_pad_get_element_private (pad);
404 static const GstFormat src_a_formats[] = {
410 static const GstFormat src_v_formats[] = {
416 return (stream->strh->type == GST_RIFF_FCC_auds ?
417 src_a_formats : src_v_formats);
421 /* assumes stream->strf.auds->av_bps != 0 */
422 static inline GstClockTime
423 avi_stream_convert_bytes_to_time_unchecked (avi_stream_context * stream,
426 return gst_util_uint64_scale (bytes, GST_SECOND, stream->strf.auds->av_bps);
429 /* assumes stream->strh->rate != 0 */
430 static inline GstClockTime
431 avi_stream_convert_frames_to_time_unchecked (avi_stream_context * stream,
434 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
439 gst_avi_demux_src_convert (GstPad * pad,
440 GstFormat src_format,
441 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
443 avi_stream_context *stream = gst_pad_get_element_private (pad);
447 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
448 ", dest_format:%s", gst_format_get_name (src_format), src_value,
449 gst_format_get_name (*dest_format));
451 if (G_UNLIKELY (src_format == *dest_format)) {
452 *dest_value = src_value;
455 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
459 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
460 (src_format == GST_FORMAT_BYTES
461 || *dest_format == GST_FORMAT_BYTES))) {
466 switch (src_format) {
467 case GST_FORMAT_TIME:
468 switch (*dest_format) {
469 case GST_FORMAT_BYTES:
470 *dest_value = gst_util_uint64_scale (src_value,
471 (guint64) stream->strf.auds->av_bps, GST_SECOND);
473 case GST_FORMAT_DEFAULT:
477 *dest_value = gst_util_uint64_scale (src_value, stream->strh->rate,
478 stream->strh->scale * GST_SECOND);
480 /* Attempt to round to nearest integer: if the difference is more
481 * than 0.5 (less than -0.5), it means that gst_util_uint64_scale()
482 * just truncated an integer, while it had to be rounded
484 error = *dest_value * GST_SECOND -
485 src_value * stream->strh->rate / stream->strh->scale;
495 case GST_FORMAT_BYTES:
496 switch (*dest_format) {
497 case GST_FORMAT_TIME:
498 if (stream->strf.auds->av_bps != 0) {
499 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
509 case GST_FORMAT_DEFAULT:
510 switch (*dest_format) {
511 case GST_FORMAT_TIME:
513 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
526 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
527 gst_format_get_name (*dest_format), *dest_value);
531 static const GstQueryType *
532 gst_avi_demux_get_src_query_types (GstPad * pad)
534 static const GstQueryType src_types[] = {
546 gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query)
549 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
551 avi_stream_context *stream = gst_pad_get_element_private (pad);
553 if (!stream->strh || !stream->strf.data)
554 return gst_pad_query_default (pad, query);
556 switch (GST_QUERY_TYPE (query)) {
557 case GST_QUERY_POSITION:{
560 GST_DEBUG ("pos query for stream %d: frames %d, bytes %" G_GUINT64_FORMAT,
561 stream->num, stream->current_frame, stream->current_byte);
563 if (stream->strh->type == GST_RIFF_FCC_auds) {
564 if (stream->is_vbr) {
566 pos = gst_util_uint64_scale ((gint64) stream->current_frame *
567 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
568 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
569 GST_TIME_FORMAT, stream->current_frame, GST_TIME_ARGS (pos));
570 } else if (stream->strf.auds->av_bps != 0) {
572 pos = gst_util_uint64_scale (stream->current_byte, GST_SECOND,
573 (guint64) stream->strf.auds->av_bps);
574 GST_DEBUG_OBJECT (avi,
575 "CBR convert bytes %" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT,
576 stream->current_byte, GST_TIME_ARGS (pos));
577 } else if (stream->total_frames != 0 && stream->total_bytes != 0) {
578 /* calculate timestamps based on percentage of length */
579 guint64 xlen = avi->avih->us_frame *
580 avi->avih->tot_frames * GST_USECOND;
582 if (stream->is_vbr) {
583 pos = gst_util_uint64_scale (xlen, stream->current_frame,
584 stream->total_frames);
585 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
586 GST_TIME_FORMAT, stream->current_frame, GST_TIME_ARGS (pos));
588 pos = gst_util_uint64_scale (xlen, stream->current_byte,
589 stream->total_bytes);
590 GST_DEBUG_OBJECT (avi, "CBR perc convert bytes %" G_GUINT64_FORMAT
591 ", time %" GST_TIME_FORMAT, stream->current_byte,
592 GST_TIME_ARGS (pos));
599 if (stream->strh->rate != 0) {
600 pos = gst_util_uint64_scale ((guint64) stream->current_frame *
601 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
603 pos = stream->current_frame * avi->avih->us_frame * GST_USECOND;
607 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
608 gst_query_set_position (query, GST_FORMAT_TIME, pos);
610 GST_WARNING ("pos query failed");
613 case GST_QUERY_DURATION:
617 if (stream->strh->type != GST_RIFF_FCC_auds &&
618 stream->strh->type != GST_RIFF_FCC_vids) {
623 gst_query_parse_duration (query, &fmt, NULL);
626 case GST_FORMAT_TIME:
627 gst_query_set_duration (query, fmt, stream->duration);
629 case GST_FORMAT_DEFAULT:
632 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
633 stream->total_frames);
635 if (stream->total_frames >= 0)
636 gst_query_set_duration (query, fmt, stream->total_frames);
637 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
638 stream->duration, &fmt, &dur))
639 gst_query_set_duration (query, fmt, dur);
648 case GST_QUERY_SEEKING:{
651 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
652 if (fmt == GST_FORMAT_TIME) {
653 gboolean seekable = TRUE;
655 if (avi->streaming) {
658 if (avi->index_entries == NULL) {
660 /* FIXME: when building index_entried, count keyframes
661 if (!(avi->key_frame_ct > 1))
667 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
668 0, stream->duration);
673 case GST_QUERY_CONVERT:{
674 GstFormat src_fmt, dest_fmt;
675 gint64 src_val, dest_val;
677 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
678 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
680 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
682 res = gst_pad_query_default (pad, query);
686 res = gst_pad_query_default (pad, query);
690 gst_object_unref (avi);
695 static const GstEventMask *
696 gst_avi_demux_get_event_mask (GstPad * pad)
698 static const GstEventMask masks[] = {
699 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
708 gst_avi_demux_handle_sink_event (GstPad * pad, GstEvent * event)
711 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
713 GST_DEBUG_OBJECT (avi,
714 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
716 switch (GST_EVENT_TYPE (event)) {
717 case GST_EVENT_NEWSEGMENT:
718 /* Drop NEWSEGMENT events, new ones are generated later */
719 gst_event_unref (event);
723 if (avi->state != GST_AVI_DEMUX_MOVI) {
724 gst_event_unref (event);
725 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
726 (NULL), ("got eos and didn't receive a complete header object"));
727 } else if (!gst_avi_demux_push_event (avi, event)) {
728 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
729 (NULL), ("got eos but no streams (yet)"));
734 res = gst_pad_event_default (pad, event);
738 gst_object_unref (avi);
744 gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event)
747 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
749 GST_DEBUG_OBJECT (avi,
750 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
752 switch (GST_EVENT_TYPE (event)) {
754 /* handle seeking only in pull mode */
755 if (!avi->streaming) {
756 res = gst_avi_demux_handle_seek (avi, pad, event);
757 gst_event_unref (event);
759 res = gst_pad_event_default (pad, event);
763 case GST_EVENT_NAVIGATION:
765 gst_event_unref (event);
768 res = gst_pad_event_default (pad, event);
772 gst_object_unref (avi);
777 /* streaming helper (push) */
780 * gst_avi_demux_peek_chunk_info:
782 * @tag: holder for tag
783 * @size: holder for tag size
785 * Peek next chunk info (tag and size)
787 * Returns: TRUE when one chunk info has been got
790 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
792 const guint8 *data = NULL;
794 if (gst_adapter_available (avi->adapter) < 8) {
798 data = gst_adapter_peek (avi->adapter, 8);
799 *tag = GST_READ_UINT32_LE (data);
800 *size = GST_READ_UINT32_LE (data + 4);
806 * gst_avi_demux_peek_chunk:
808 * @tag: holder for tag
809 * @size: holder for tag size
811 * Peek enough data for one full chunk
813 * Returns: %TRUE when one chunk has been got
816 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
818 guint32 peek_size = 0;
821 if (!gst_avi_demux_peek_chunk_info (avi, tag, size)) {
825 /* size 0 -> empty data buffer would surprise most callers,
826 * large size -> do not bother trying to squeeze that into adapter,
827 * so we throw poor man's exception, which can be caught if caller really
828 * wants to handle 0 size chunk */
829 if (!(*size) || (*size) >= (1 << 30)) {
830 GST_INFO ("Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT,
831 *size, GST_FOURCC_ARGS (*tag));
832 /* chain should give up */
833 avi->abort_buffering = TRUE;
836 peek_size = (*size + 1) & ~1;
837 available = gst_adapter_available (avi->adapter);
839 GST_DEBUG ("Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
840 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
842 if (available >= (8 + peek_size)) {
852 * gst_avi_demux_parse_file_header:
853 * @element: caller element (used for errors/debug).
854 * @buf: input data to be used for parsing.
856 * "Open" a RIFF/AVI file. The buffer should be at least 12
857 * bytes long. Takes ownership of @buf.
859 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
860 * Throws an error, caller should error out (fatal).
863 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
867 /* riff_parse posts an error */
868 if (!gst_riff_parse_file_header (element, buf, &doctype))
871 if (doctype != GST_RIFF_RIFF_AVI)
879 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
880 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
881 GST_FOURCC_ARGS (doctype)));
887 * Read AVI file tag when streaming
890 gst_avi_demux_stream_init_push (GstAviDemux * avi)
892 if (gst_adapter_available (avi->adapter) >= 12) {
895 tmp = gst_adapter_take_buffer (avi->adapter, 12);
897 GST_DEBUG ("Parsing avi header");
898 if (!gst_avi_demux_parse_file_header (GST_ELEMENT (avi), tmp)) {
899 return GST_FLOW_ERROR;
901 GST_DEBUG ("header ok");
904 avi->state = GST_AVI_DEMUX_HEADER;
913 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
916 GstBuffer *buf = NULL;
918 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
919 if (res != GST_FLOW_OK)
921 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
931 GST_DEBUG_OBJECT (avi, "error parsing file header");
932 return GST_FLOW_ERROR;
936 /* AVI header handling */
939 * gst_avi_demux_parse_avih:
940 * @element: caller element (used for errors/debug).
941 * @buf: input data to be used for parsing.
942 * @avih: pointer to structure (filled in by function) containing
943 * stream information (such as flags, number of streams, etc.).
945 * Read 'avih' header. Discards buffer after use.
947 * Returns: TRUE on success, FALSE otherwise. Throws an error if
948 * the header is invalid. The caller should error out
952 gst_avi_demux_parse_avih (GstElement * element,
953 GstBuffer * buf, gst_riff_avih ** _avih)
960 if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_avih))
963 avih = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
965 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
966 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
967 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
968 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
969 avih->flags = GUINT32_FROM_LE (avih->flags);
970 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
971 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
972 avih->streams = GUINT32_FROM_LE (avih->streams);
973 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
974 avih->width = GUINT32_FROM_LE (avih->width);
975 avih->height = GUINT32_FROM_LE (avih->height);
976 avih->scale = GUINT32_FROM_LE (avih->scale);
977 avih->rate = GUINT32_FROM_LE (avih->rate);
978 avih->start = GUINT32_FROM_LE (avih->start);
979 avih->length = GUINT32_FROM_LE (avih->length);
983 GST_INFO_OBJECT (element, "avih tag found:");
984 GST_INFO_OBJECT (element, " us_frame %u", avih->us_frame);
985 GST_INFO_OBJECT (element, " max_bps %u", avih->max_bps);
986 GST_INFO_OBJECT (element, " pad_gran %u", avih->pad_gran);
987 GST_INFO_OBJECT (element, " flags 0x%08x", avih->flags);
988 GST_INFO_OBJECT (element, " tot_frames %u", avih->tot_frames);
989 GST_INFO_OBJECT (element, " init_frames %u", avih->init_frames);
990 GST_INFO_OBJECT (element, " streams %u", avih->streams);
991 GST_INFO_OBJECT (element, " bufsize %u", avih->bufsize);
992 GST_INFO_OBJECT (element, " width %u", avih->width);
993 GST_INFO_OBJECT (element, " height %u", avih->height);
994 GST_INFO_OBJECT (element, " scale %u", avih->scale);
995 GST_INFO_OBJECT (element, " rate %u", avih->rate);
996 GST_INFO_OBJECT (element, " start %u", avih->start);
997 GST_INFO_OBJECT (element, " length %u", avih->length);
1000 gst_buffer_unref (buf);
1007 GST_ELEMENT_ERROR (element, STREAM, DEMUX, (NULL), ("No buffer"));
1012 GST_ELEMENT_ERROR (element, STREAM, DEMUX, (NULL),
1013 ("Too small avih (%d available, %d needed)",
1014 GST_BUFFER_SIZE (buf), (int) sizeof (gst_riff_avih)));
1015 gst_buffer_unref (buf);
1021 * gst_avi_demux_parse_superindex:
1022 * @avi: caller element (used for debugging/errors).
1023 * @buf: input data to use for parsing.
1024 * @locations: locations in the file (byte-offsets) that contain
1025 * the actual indexes (see get_avi_demux_parse_subindex()).
1026 * The array ends with GST_BUFFER_OFFSET_NONE.
1028 * Reads superindex (openDML-2 spec stuff) from the provided data.
1030 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
1031 * on error, but they are not fatal.
1034 gst_avi_demux_parse_superindex (GstAviDemux * avi,
1035 GstBuffer * buf, guint64 ** _indexes)
1045 size = buf ? GST_BUFFER_SIZE (buf) : 0;
1049 data = GST_BUFFER_DATA (buf);
1051 /* check type of index. The opendml2 specs state that
1052 * there should be 4 dwords per array entry. Type can be
1053 * either frame or field (and we don't care). */
1054 if (GST_READ_UINT16_LE (data) != 4 ||
1055 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
1056 GST_WARNING_OBJECT (avi,
1057 "Superindex for stream has unexpected "
1058 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1059 GST_READ_UINT16_LE (data), data[2], data[3]);
1060 bpe = GST_READ_UINT16_LE (data) * 4;
1062 num = GST_READ_UINT32_LE (&data[4]);
1064 indexes = g_new (guint64, num + 1);
1065 for (i = 0; i < num; i++) {
1066 if (size < 24 + bpe * (i + 1))
1068 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
1070 indexes[i] = GST_BUFFER_OFFSET_NONE;
1071 *_indexes = indexes;
1073 gst_buffer_unref (buf);
1080 GST_ERROR_OBJECT (avi,
1081 "Not enough data to parse superindex (%d available, 24 needed)", size);
1083 gst_buffer_unref (buf);
1089 * gst_avi_demux_parse_subindex:
1091 * @buf: input data to use for parsing.
1092 * @stream: stream context.
1093 * @entries_list: a list (returned by the function) containing all the
1094 * indexes parsed in this specific subindex. The first
1095 * entry is also a pointer to allocated memory that needs
1096 * to be free´ed. May be NULL if no supported indexes were
1099 * Reads superindex (openDML-2 spec stuff) from the provided data.
1100 * The buffer will be discarded after use.
1102 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1103 * throw an error, caller should bail out asap.
1106 gst_avi_demux_parse_subindex (GstAviDemux * avi,
1107 GstBuffer * buf, avi_stream_context * stream, GList ** _entries_list)
1109 guint8 *data = GST_BUFFER_DATA (buf);
1113 gst_avi_index_entry *entries, *entry;
1114 GList *entries_list = NULL;
1117 *_entries_list = NULL;
1119 size = buf ? GST_BUFFER_SIZE (buf) : 0;
1125 /* We don't support index-data yet */
1127 goto not_implemented;
1129 /* check type of index. The opendml2 specs state that
1130 * there should be 4 dwords per array entry. Type can be
1131 * either frame or field (and we don't care). */
1132 bpe = (data[2] & 0x01) ? 12 : 8;
1133 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1134 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1135 GST_WARNING_OBJECT (avi,
1136 "Superindex for stream %d has unexpected "
1137 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1138 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1139 bpe = GST_READ_UINT16_LE (data) * 4;
1141 num = GST_READ_UINT32_LE (&data[4]);
1142 baseoff = GST_READ_UINT64_LE (&data[12]);
1144 /* If there's nothing, just return ! */
1148 if (!(entries = g_try_new (gst_avi_index_entry, num)))
1151 for (i = 0; i < num; i++) {
1154 entry = &entries[i];
1156 if (size < 24 + bpe * (i + 1))
1160 entry->offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1161 entry->size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1163 (entry->size & 0x80000000) ? 0 : GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
1164 entry->size &= ~0x80000000;
1165 entry->index_nr = i;
1166 entry->stream_nr = stream->num;
1168 /* stream duration unknown, now we can calculate it */
1169 if (stream->idx_duration == -1)
1170 stream->idx_duration = 0;
1173 entry->ts = stream->idx_duration;
1174 if (stream->is_vbr) {
1175 /* VBR stream next timestamp */
1176 if (stream->strh->type == GST_RIFF_FCC_auds) {
1177 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
1178 stream->total_blocks + 1);
1180 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
1181 stream->total_frames + 1);
1184 /* CBR get next timestamp */
1185 next_ts = avi_stream_convert_bytes_to_time_unchecked (stream,
1186 stream->total_bytes + entry->size);
1188 /* duration is next - current */
1189 entry->dur = next_ts - entry->ts;
1191 /* stream position */
1192 entry->bytes_before = stream->total_bytes;
1193 entry->frames_before = stream->total_frames;
1195 stream->total_bytes += entry->size;
1196 stream->total_frames++;
1197 if (stream->strh->type == GST_RIFF_FCC_auds) {
1198 if (stream->strf.auds->blockalign > 0)
1199 stream->total_blocks +=
1200 (entry->size + stream->strf.auds->blockalign -
1201 1) / stream->strf.auds->blockalign;
1203 stream->total_blocks++;
1205 stream->idx_duration = next_ts;
1207 entries_list = g_list_prepend (entries_list, entry);
1210 GST_LOG_OBJECT (avi, "Read %d index entries", i);
1212 gst_buffer_unref (buf);
1215 *_entries_list = g_list_reverse (entries_list);
1225 GST_ERROR_OBJECT (avi,
1226 "Not enough data to parse subindex (%d available, 24 needed)", size);
1228 gst_buffer_unref (buf);
1229 return TRUE; /* continue */
1233 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1234 ("Subindex-is-data is not implemented"));
1235 gst_buffer_unref (buf);
1240 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1241 ("Cannot allocate memory for %u*%u=%u bytes",
1242 (guint) sizeof (gst_avi_index_entry), num,
1243 (guint) sizeof (gst_avi_index_entry) * num));
1244 gst_buffer_unref (buf);
1251 * Read AVI index when streaming
1254 gst_avi_demux_read_subindexes_push (GstAviDemux * avi,
1255 GList ** index, GList ** alloc_list)
1258 guint32 tag = 0, size;
1259 GstBuffer *buf = NULL;
1262 GST_DEBUG_OBJECT (avi, "gst_avi_demux_read_subindexes_push for %d streams",
1265 for (n = 0; n < avi->num_streams; n++) {
1266 avi_stream_context *stream = &avi->stream[n];
1268 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1269 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1271 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1272 '0' + stream->num % 10)) &&
1273 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1274 '0' + stream->num % 10, 'i', 'x'))) {
1275 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1276 GST_FOURCC_ARGS (tag));
1280 avi->offset += 8 + ((size + 1) & ~1);
1282 buf = gst_buffer_new ();
1283 GST_BUFFER_DATA (buf) = gst_adapter_take (avi->adapter, size);
1284 GST_BUFFER_SIZE (buf) = size;
1286 if (!gst_avi_demux_parse_subindex (avi, buf, stream, &list))
1289 GST_DEBUG_OBJECT (avi, " adding %d entries", g_list_length (list));
1290 *alloc_list = g_list_append (*alloc_list, list->data);
1291 *index = g_list_concat (*index, list);
1295 g_free (stream->indexes);
1296 stream->indexes = NULL;
1298 GST_DEBUG_OBJECT (avi, "index %s", ((*index) ? "!= 0" : "== 0"));
1306 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi,
1307 GList ** index, GList ** alloc_list)
1314 GST_DEBUG_OBJECT (avi, "gst_avi_demux_read_subindexes_pull for %d streams",
1317 for (n = 0; n < avi->num_streams; n++) {
1318 avi_stream_context *stream = &avi->stream[n];
1320 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1321 if (gst_riff_read_chunk (GST_ELEMENT (avi), avi->sinkpad,
1322 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1324 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1325 '0' + stream->num % 10)) &&
1326 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1327 '0' + stream->num % 10, 'i', 'x'))) {
1328 /* Some ODML files (created by god knows what muxer) have a ##ix format
1329 * instead of the 'official' ix##. They are still valid though. */
1330 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1331 GST_FOURCC_ARGS (tag));
1332 gst_buffer_unref (buf);
1336 if (!gst_avi_demux_parse_subindex (avi, buf, stream, &list))
1339 GST_DEBUG_OBJECT (avi, " adding %5d entries, total %2d %5d",
1340 g_list_length (list), g_list_length (*alloc_list),
1341 g_list_length (*index));
1342 *alloc_list = g_list_append (*alloc_list, list->data);
1343 *index = g_list_concat (*index, list);
1347 g_free (stream->indexes);
1348 stream->indexes = NULL;
1350 GST_DEBUG_OBJECT (avi, "index %s", ((*index) ? "!= 0" : "== 0"));
1354 * gst_avi_demux_riff_parse_vprp:
1355 * @element: caller element (used for debugging/error).
1356 * @buf: input data to be used for parsing, stripped from header.
1357 * @vprp: a pointer (returned by this function) to a filled-in vprp
1358 * structure. Caller should free it.
1360 * Parses a video stream´s vprp. This function takes ownership of @buf.
1362 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1363 * should be skipped on error, but it is not fatal.
1366 gst_avi_demux_riff_parse_vprp (GstElement * element,
1367 GstBuffer * buf, gst_riff_vprp ** _vprp)
1369 gst_riff_vprp *vprp;
1372 g_return_val_if_fail (buf != NULL, FALSE);
1373 g_return_val_if_fail (_vprp != NULL, FALSE);
1375 if (GST_BUFFER_SIZE (buf) < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1378 vprp = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1380 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1381 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1382 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1383 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1384 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1385 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1386 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1387 vprp->width = GUINT32_FROM_LE (vprp->width);
1388 vprp->height = GUINT32_FROM_LE (vprp->height);
1389 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1393 /* calculate fields based on size */
1394 k = (GST_BUFFER_SIZE (buf) - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) /
1396 if (vprp->fields > k) {
1397 GST_WARNING_OBJECT (element,
1398 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1401 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1402 GST_WARNING_OBJECT (element,
1403 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1404 GST_RIFF_VPRP_VIDEO_FIELDS);
1405 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1407 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1408 for (k = 0; k < vprp->fields; k++) {
1409 gst_riff_vprp_video_field_desc *fd;
1411 fd = &vprp->field_info[k];
1412 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1413 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1414 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1415 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1416 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1417 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1418 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1419 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1424 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1425 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1426 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1427 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1428 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1429 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1430 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1431 vprp->aspect & 0xffff);
1432 GST_INFO_OBJECT (element, " width %d", vprp->width);
1433 GST_INFO_OBJECT (element, " height %d", vprp->height);
1434 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1435 for (k = 0; k < vprp->fields; k++) {
1436 gst_riff_vprp_video_field_desc *fd;
1438 fd = &(vprp->field_info[k]);
1439 GST_INFO_OBJECT (element, " field %u description:", k);
1440 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1441 fd->compressed_bm_height);
1442 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1443 fd->compressed_bm_width);
1444 GST_INFO_OBJECT (element, " valid_bm_height %d",
1445 fd->valid_bm_height);
1446 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1447 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1448 fd->valid_bm_x_offset);
1449 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1450 fd->valid_bm_y_offset);
1451 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1452 fd->video_x_t_offset);
1453 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1456 gst_buffer_unref (buf);
1465 GST_ERROR_OBJECT (element,
1466 "Too small vprp (%d available, at least %d needed)",
1467 GST_BUFFER_SIZE (buf),
1468 (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1469 gst_buffer_unref (buf);
1475 * gst_avi_demux_parse_stream:
1476 * @avi: calling element (used for debugging/errors).
1477 * @buf: input buffer used to parse the stream.
1479 * Parses all subchunks in a strl chunk (which defines a single
1480 * stream). Discards the buffer after use. This function will
1481 * increment the stream counter internally.
1483 * Returns: whether the stream was identified successfully.
1484 * Errors are not fatal. It does indicate the stream
1488 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
1490 avi_stream_context *stream;
1491 GstElementClass *klass;
1492 GstPadTemplate *templ;
1493 GstBuffer *sub = NULL;
1496 gchar *codec_name = NULL, *padname = NULL;
1497 const gchar *tag_name;
1498 GstCaps *caps = NULL;
1500 GstElement *element;
1501 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
1502 gst_riff_vprp *vprp = NULL;
1504 element = GST_ELEMENT_CAST (avi);
1506 GST_DEBUG_OBJECT (avi, "Parsing stream");
1508 if (avi->num_streams >= GST_AVI_DEMUX_MAX_STREAMS) {
1509 GST_WARNING_OBJECT (avi,
1510 "maximum no of streams (%d) exceeded, ignoring stream",
1511 GST_AVI_DEMUX_MAX_STREAMS);
1512 gst_buffer_unref (buf);
1513 /* not a fatal error, let's say */
1517 stream = &avi->stream[avi->num_streams];
1519 /* initial settings */
1520 stream->idx_duration = GST_CLOCK_TIME_NONE;
1521 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1522 stream->duration = GST_CLOCK_TIME_NONE;
1524 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
1525 /* sub can be NULL if the chunk is empty */
1527 GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
1528 GST_FOURCC_ARGS (tag));
1532 case GST_RIFF_TAG_strh:
1534 gst_riff_strh *strh;
1537 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
1540 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
1541 /* ownership given away */
1543 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
1547 strh = stream->strh;
1548 /* sanity check; stream header frame rate matches global header
1550 if (stream->strh->type == GST_RIFF_FCC_vids) {
1552 GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
1554 s_dur = gst_util_uint64_scale (GST_SECOND, strh->scale, strh->rate);
1555 GST_DEBUG_OBJECT (avi, "verifying stream framerate %d/%d, "
1556 "frame duration = %d ms", strh->rate, strh->scale,
1557 s_dur / GST_MSECOND);
1558 if (h_dur > (10 * GST_MSECOND) && (s_dur > 10 * h_dur)) {
1559 strh->rate = GST_SECOND / GST_USECOND;
1560 strh->scale = h_dur / GST_USECOND;
1561 GST_DEBUG_OBJECT (avi, "correcting stream framerate to %d/%d",
1562 strh->rate, strh->scale);
1565 /* determine duration as indicated by header */
1566 stream->hdr_duration = gst_util_uint64_scale ((guint64) strh->length *
1567 strh->scale, GST_SECOND, (guint64) strh->rate);
1568 GST_INFO ("Stream duration according to header: %" GST_TIME_FORMAT,
1569 GST_TIME_ARGS (stream->hdr_duration));
1570 if (stream->hdr_duration == 0)
1571 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1576 case GST_RIFF_TAG_strf:
1578 gboolean res = FALSE;
1581 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
1585 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
1588 switch (stream->strh->type) {
1589 case GST_RIFF_FCC_vids:
1590 stream->is_vbr = TRUE;
1591 res = gst_riff_parse_strf_vids (element, sub,
1592 &stream->strf.vids, &stream->extradata);
1594 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
1596 case GST_RIFF_FCC_auds:
1597 stream->is_vbr = (stream->strh->samplesize == 0)
1598 && stream->strh->scale > 1;
1600 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
1601 &stream->extradata);
1603 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
1604 stream->is_vbr, res);
1606 case GST_RIFF_FCC_iavs:
1607 stream->is_vbr = TRUE;
1608 res = gst_riff_parse_strf_iavs (element, sub,
1609 &stream->strf.iavs, &stream->extradata);
1611 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
1613 case GST_RIFF_FCC_txts:
1614 /* nothing to parse here */
1615 stream->is_vbr = (stream->strh->samplesize == 0)
1616 && (stream->strh->scale > 1);
1620 GST_ERROR_OBJECT (avi,
1621 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
1622 GST_FOURCC_ARGS (stream->strh->type));
1626 gst_buffer_unref (sub);
1634 case GST_RIFF_TAG_vprp:
1637 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
1641 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
1645 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
1649 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
1650 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
1651 /* not considered fatal */
1659 case GST_RIFF_TAG_strd:
1660 if (stream->initdata)
1661 gst_buffer_unref (stream->initdata);
1662 stream->initdata = sub;
1665 case GST_RIFF_TAG_strn:
1666 g_free (stream->name);
1669 g_strndup ((gchar *) GST_BUFFER_DATA (sub),
1670 (gsize) GST_BUFFER_SIZE (sub));
1671 gst_buffer_unref (sub);
1674 stream->name = g_strdup ("");
1676 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
1679 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
1680 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
1681 '0' + avi->num_streams % 10)) {
1682 g_free (stream->indexes);
1683 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
1684 stream->superindex = TRUE;
1688 GST_WARNING_OBJECT (avi,
1689 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
1690 GST_FOURCC_ARGS (tag));
1692 case GST_RIFF_TAG_JUNK:
1696 gst_buffer_unref (sub);
1702 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
1707 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
1711 /* get class to figure out the template */
1712 klass = GST_ELEMENT_GET_CLASS (avi);
1714 /* we now have all info, let´s set up a pad and a caps and be done */
1715 /* create stream name + pad */
1716 switch (stream->strh->type) {
1717 case GST_RIFF_FCC_vids:{
1720 fourcc = (stream->strf.vids->compression) ?
1721 stream->strf.vids->compression : stream->strh->fcc_handler;
1722 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
1723 templ = gst_element_class_get_pad_template (klass, "video_%02d");
1724 caps = gst_riff_create_video_caps (fourcc, stream->strh,
1725 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
1727 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
1728 GST_TYPE_FOURCC, fourcc, NULL);
1729 } else if (got_vprp && vprp) {
1730 guint32 aspect_n, aspect_d;
1733 aspect_n = vprp->aspect >> 16;
1734 aspect_d = vprp->aspect & 0xffff;
1735 /* calculate the pixel aspect ratio using w/h and aspect ratio */
1736 n = aspect_n * stream->strf.vids->height;
1737 d = aspect_d * stream->strf.vids->width;
1739 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1741 /* very local, not needed elsewhere */
1745 tag_name = GST_TAG_VIDEO_CODEC;
1746 avi->num_v_streams++;
1749 case GST_RIFF_FCC_auds:{
1750 padname = g_strdup_printf ("audio_%02d", avi->num_a_streams);
1751 templ = gst_element_class_get_pad_template (klass, "audio_%02d");
1752 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
1753 stream->strh, stream->strf.auds, stream->extradata,
1754 stream->initdata, &codec_name);
1756 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
1757 G_TYPE_INT, stream->strf.auds->format, NULL);
1759 tag_name = GST_TAG_AUDIO_CODEC;
1760 avi->num_a_streams++;
1763 case GST_RIFF_FCC_iavs:{
1764 guint32 fourcc = stream->strh->fcc_handler;
1766 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
1767 templ = gst_element_class_get_pad_template (klass, "video_%02d");
1768 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
1769 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
1771 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
1772 GST_TYPE_FOURCC, fourcc, NULL);
1774 tag_name = GST_TAG_VIDEO_CODEC;
1775 avi->num_v_streams++;
1778 case GST_RIFF_FCC_txts:{
1779 padname = g_strdup_printf ("subtitle_%02d", avi->num_t_streams);
1780 templ = gst_element_class_get_pad_template (klass, "subtitle_%02d");
1781 caps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
1783 avi->num_t_streams++;
1787 g_assert_not_reached ();
1790 /* no caps means no stream */
1792 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
1796 GST_DEBUG_OBJECT (element, "codec-name=%s",
1797 (codec_name ? codec_name : "NULL"));
1798 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
1800 /* set proper settings and add it */
1802 gst_object_unref (stream->pad);
1803 pad = stream->pad = gst_pad_new_from_template (templ, padname);
1804 stream->last_flow = GST_FLOW_OK;
1805 stream->discont = TRUE;
1808 gst_pad_use_fixed_caps (pad);
1810 gst_pad_set_formats_function (pad,
1811 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
1812 gst_pad_set_event_mask_function (pad,
1813 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
1815 gst_pad_set_event_function (pad,
1816 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
1817 gst_pad_set_query_type_function (pad,
1818 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_query_types));
1819 gst_pad_set_query_function (pad,
1820 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
1822 gst_pad_set_convert_function (pad,
1823 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
1826 stream->num = avi->num_streams;
1827 stream->total_bytes = 0;
1828 stream->total_frames = 0;
1829 stream->total_blocks = 0;
1830 stream->current_frame = 0;
1831 stream->current_byte = 0;
1832 gst_pad_set_element_private (pad, stream);
1834 gst_pad_set_caps (pad, caps);
1835 gst_pad_set_active (pad, TRUE);
1836 gst_element_add_pad (GST_ELEMENT (avi), pad);
1837 GST_LOG_OBJECT (element, "Added pad %s with caps %" GST_PTR_FORMAT,
1838 GST_PAD_NAME (pad), caps);
1839 gst_caps_unref (caps);
1843 if (!stream->taglist)
1844 stream->taglist = gst_tag_list_new ();
1846 avi->got_tags = TRUE;
1848 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
1850 g_free (codec_name);
1853 gst_buffer_unref (buf);
1860 /* unref any mem that may be in use */
1862 gst_buffer_unref (buf);
1864 gst_buffer_unref (sub);
1866 g_free (codec_name);
1867 g_free (stream->strh);
1868 g_free (stream->strf.data);
1869 g_free (stream->name);
1870 g_free (stream->indexes);
1871 if (stream->initdata)
1872 gst_buffer_unref (stream->initdata);
1873 if (stream->extradata)
1874 gst_buffer_unref (stream->extradata);
1875 memset (stream, 0, sizeof (avi_stream_context));
1882 * gst_avi_demux_parse_odml:
1883 * @avi: calling element (used for debug/error).
1884 * @buf: input buffer to be used for parsing.
1886 * Read an openDML-2.0 extension header. Fills in the frame number
1887 * in the avi demuxer object when reading succeeds.
1890 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
1894 GstBuffer *sub = NULL;
1896 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
1899 case GST_RIFF_TAG_dmlh:{
1900 gst_riff_dmlh dmlh, *_dmlh;
1903 /* sub == NULL is possible and means an empty buffer */
1904 size = sub ? GST_BUFFER_SIZE (sub) : 0;
1907 if (size < sizeof (gst_riff_dmlh)) {
1908 GST_ERROR_OBJECT (avi,
1909 "DMLH entry is too small (%d bytes, %d needed)",
1910 size, (int) sizeof (gst_riff_dmlh));
1913 _dmlh = (gst_riff_dmlh *) GST_BUFFER_DATA (sub);
1914 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
1916 GST_INFO_OBJECT (avi, "dmlh tag found:");
1917 GST_INFO_OBJECT (avi, " totalframes: %u", dmlh.totalframes);
1919 avi->avih->tot_frames = dmlh.totalframes;
1924 GST_WARNING_OBJECT (avi,
1925 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
1926 GST_FOURCC_ARGS (tag));
1928 case GST_RIFF_TAG_JUNK:
1930 /* skip and move to next chunk */
1932 gst_buffer_unref (sub);
1939 gst_buffer_unref (buf);
1943 * Sort helper for index entries that sorts by index time.
1944 * If times are equal we sort by stream number.
1947 sort (gst_avi_index_entry * a, gst_avi_index_entry * b)
1951 else if (a->ts < b->ts)
1954 return a->stream_nr - b->stream_nr;
1958 * gst_avi_demux_parse_index:
1959 * @avi: calling element (used for debugging/errors).
1960 * @buf: buffer containing the full index.
1961 * @entries_list: list (returned by this function) containing the index
1962 * entries parsed from the buffer. The first in the list
1963 * is also a pointer to the allocated data and should be
1964 * free'ed at some point.
1966 * Read index entries from the provided buffer. Takes ownership of @buf.
1969 gst_avi_demux_parse_index (GstAviDemux * avi,
1970 GstBuffer * buf, GList ** _entries_list)
1972 guint64 pos_before = avi->offset;
1973 gst_avi_index_entry *entries = NULL;
1975 GList *entries_list = NULL;
1978 #ifndef GST_DISABLE_GST_DEBUG
1979 gulong _nr_keyframes = 0;
1982 if (!buf || !GST_BUFFER_SIZE (buf)) {
1983 *_entries_list = NULL;
1984 GST_DEBUG ("empty index");
1986 gst_buffer_unref (buf);
1990 data = GST_BUFFER_DATA (buf);
1991 num = GST_BUFFER_SIZE (buf) / sizeof (gst_riff_index_entry);
1992 if (!(entries = g_try_new (gst_avi_index_entry, num)))
1995 GST_INFO ("Parsing index, nr_entries = %6d", num);
1997 for (i = 0, n = 0; i < num; i++) {
1999 gst_riff_index_entry entry, *_entry;
2000 avi_stream_context *stream;
2002 gst_avi_index_entry *target;
2004 _entry = &((gst_riff_index_entry *) data)[i];
2005 entry.id = GST_READ_UINT32_LE (&_entry->id);
2006 entry.offset = GST_READ_UINT32_LE (&_entry->offset);
2007 entry.flags = GST_READ_UINT32_LE (&_entry->flags);
2008 entry.size = GST_READ_UINT32_LE (&_entry->size);
2009 target = &entries[n];
2011 if (entry.id == GST_RIFF_rec || entry.id == 0 ||
2012 (entry.offset == 0 && n > 0))
2015 stream_nr = CHUNKID_TO_STREAMNR (entry.id);
2016 if (stream_nr >= avi->num_streams) {
2017 GST_WARNING_OBJECT (avi,
2018 "Index entry %d has invalid stream nr %d", i, stream_nr);
2021 target->stream_nr = stream_nr;
2022 stream = &avi->stream[stream_nr];
2024 if (!stream->strh) {
2025 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2029 target->index_nr = i;
2031 (entry.flags & GST_RIFF_IF_KEYFRAME) ? GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME
2033 target->size = entry.size;
2034 target->offset = entry.offset + 8;
2036 /* figure out if the index is 0 based or relative to the MOVI start */
2038 if (target->offset < pos_before)
2039 avi->index_offset = pos_before + 8;
2041 avi->index_offset = 0;
2042 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2045 if (stream->strh->type == GST_RIFF_FCC_auds) {
2046 /* all audio frames are keyframes */
2047 target->flags |= GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
2049 #ifndef GST_DISABLE_GST_DEBUG
2050 if (target->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)
2054 /* stream duration unknown, now we can calculate it */
2055 if (stream->idx_duration == -1)
2056 stream->idx_duration = 0;
2059 target->ts = stream->idx_duration;
2060 if (stream->is_vbr) {
2061 /* VBR stream next timestamp */
2062 if (stream->strh->type == GST_RIFF_FCC_auds) {
2063 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
2064 stream->total_blocks + 1);
2066 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
2067 stream->total_frames + 1);
2070 /* constant rate stream */
2071 next_ts = avi_stream_convert_bytes_to_time_unchecked (stream,
2072 stream->total_bytes + target->size);
2074 /* duration is next - current */
2075 target->dur = next_ts - target->ts;
2077 /* stream position */
2078 target->bytes_before = stream->total_bytes;
2079 target->frames_before = stream->total_frames;
2081 stream->total_bytes += target->size;
2082 stream->total_frames++;
2083 if (stream->strh->type == GST_RIFF_FCC_auds) {
2084 if (stream->strf.auds->blockalign > 0)
2085 stream->total_blocks +=
2086 (target->size + stream->strf.auds->blockalign -
2087 1) / stream->strf.auds->blockalign;
2089 stream->total_blocks++;
2091 stream->idx_duration = next_ts;
2093 GST_LOG_OBJECT (avi,
2094 "Adding index entry %d (%6u), flags %02x, stream %d, size %u "
2095 ", offset %" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT ", dur %"
2097 target->index_nr, stream->total_frames - 1, target->flags,
2098 target->stream_nr, target->size, target->offset,
2099 GST_TIME_ARGS (target->ts), GST_TIME_ARGS (target->dur));
2100 entries_list = g_list_prepend (entries_list, target);
2105 GST_INFO ("Parsed index, %6d entries, %5ld keyframes, entry size = %2d, "
2106 "total size = %10d", num, _nr_keyframes,
2107 (gint) sizeof (gst_avi_index_entry),
2108 (gint) (num * sizeof (gst_avi_index_entry)));
2110 gst_buffer_unref (buf);
2113 *_entries_list = g_list_reverse (entries_list);
2122 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2123 ("Cannot allocate memory for %u*%u=%u bytes",
2124 (guint) sizeof (gst_avi_index_entry), num,
2125 (guint) sizeof (gst_avi_index_entry) * num));
2126 gst_buffer_unref (buf);
2131 * gst_avi_demux_stream_index:
2132 * @avi: avi demuxer object.
2133 * @index: list of index entries, returned by this function.
2134 * @alloc_list: list of allocated data, returned by this function.
2136 * Seeks to index and reads it.
2139 gst_avi_demux_stream_index (GstAviDemux * avi,
2140 GList ** index, GList ** alloc_list)
2143 guint64 offset = avi->offset;
2149 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2154 /* get chunk information */
2155 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2156 if (res != GST_FLOW_OK)
2158 else if (GST_BUFFER_SIZE (buf) < 8)
2161 /* check tag first before blindy trying to read 'size' bytes */
2162 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2163 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2164 if (tag == GST_RIFF_TAG_LIST) {
2165 /* this is the movi tag */
2166 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2167 (8 + ((size + 1) & ~1)));
2168 offset += 8 + ((size + 1) & ~1);
2169 gst_buffer_unref (buf);
2170 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2171 if (res != GST_FLOW_OK)
2173 else if (GST_BUFFER_SIZE (buf) < 8)
2175 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2176 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2179 if (tag != GST_RIFF_TAG_idx1)
2184 gst_buffer_unref (buf);
2186 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2188 /* read chunk, advance offset */
2189 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2190 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2193 GST_INFO ("will parse index chunk size %u for tag %"
2194 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2196 gst_avi_demux_parse_index (avi, buf, index);
2198 *alloc_list = g_list_append (*alloc_list, (*index)->data);
2200 /* debug our indexes */
2201 for (i = 0; i < avi->num_streams; i++) {
2202 avi_stream_context *stream;
2204 stream = &avi->stream[i];
2205 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2206 i, stream->total_frames, stream->total_bytes);
2213 GST_DEBUG_OBJECT (avi,
2214 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2219 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2220 gst_buffer_unref (buf);
2225 GST_WARNING_OBJECT (avi,
2226 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2227 GST_FOURCC_ARGS (tag));
2228 gst_buffer_unref (buf);
2233 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2234 gst_buffer_unref (buf);
2241 * Sync to next data chunk.
2244 gst_avi_demux_skip (GstAviDemux * avi, gboolean prevent_eos)
2246 GstRiffRead *riff = GST_RIFF_READ (avi);
2249 guint64 pos, length;
2253 pos = gst_bytestream_tell (riff->bs);
2254 length = gst_bytestream_length (riff->bs);
2256 if (pos + 8 > length)
2259 if (gst_bytestream_peek_bytes (riff->bs, &data, 8) != 8)
2262 size = GST_READ_UINT32_LE (&data[4]);
2266 /* Note, we're going to skip which might involve seeks. Therefore,
2267 * we need 1 byte more! */
2268 if (pos + 8 + size >= length)
2272 return gst_riff_read_skip (riff);
2276 gst_avi_demux_sync (GstAviDemux * avi, guint32 * ret_tag, gboolean prevent_eos)
2278 GstRiffRead *riff = GST_RIFF_READ (avi);
2280 guint64 length = gst_bytestream_length (riff->bs);
2282 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2285 /* peek first (for the end of this 'list/movi' section) */
2286 if (!(tag = gst_riff_peek_tag (riff, &avi->level_up)))
2289 /* if we're at top-level, we didn't read the 'movi'
2290 * list tag yet. This can also be 'AVIX' in case of
2291 * openDML-2.0 AVI files. Lastly, it might be idx1,
2292 * in which case we skip it so we come at EOS. */
2294 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2297 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2301 case GST_RIFF_TAG_LIST:
2302 if (!(tag = gst_riff_peek_list (riff)))
2306 case GST_RIFF_LIST_AVIX:
2307 if (!gst_riff_read_list (riff, &tag))
2311 case GST_RIFF_LIST_movi:
2312 if (!gst_riff_read_list (riff, &tag))
2320 GST_WARNING ("Unknown list %" GST_FOURCC_FORMAT " before AVI data",
2321 GST_FOURCC_ARGS (tag));
2324 case GST_RIFF_TAG_JUNK:
2325 if (!gst_avi_demux_skip (avi, prevent_eos))
2332 if ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
2333 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9') {
2338 case GST_RIFF_TAG_idx1:
2339 case GST_RIFF_TAG_JUNK:
2340 if (!gst_avi_demux_skip (avi, prevent_eos)) {
2347 /* And then, we get the data */
2348 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2351 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2354 /* Support for rec-list files */
2356 case GST_RIFF_TAG_LIST:
2357 if (!(tag = gst_riff_peek_list (riff)))
2359 if (tag == GST_RIFF_rec) {
2360 /* Simply skip the list */
2361 if (!gst_riff_read_list (riff, &tag))
2363 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2368 case GST_RIFF_TAG_JUNK:
2369 gst_avi_demux_skip (avi, prevent_eos);
2381 * gst_avi_demux_peek_tag:
2383 * Returns the tag and size of the next chunk
2385 static GstFlowReturn
2386 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2389 GstFlowReturn res = GST_FLOW_OK;
2390 GstBuffer *buf = NULL;
2393 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2394 if (res != GST_FLOW_OK)
2397 bufsize = GST_BUFFER_SIZE (buf);
2401 *tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2402 *size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2404 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2405 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2406 *size, offset + 8, offset + 8 + (gint64) * size);
2408 gst_buffer_unref (buf);
2415 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2420 GST_DEBUG_OBJECT (avi, "got %d bytes which is <> 8 bytes", bufsize);
2421 res = GST_FLOW_ERROR;
2427 * gst_avi_demux_next_data_buffer:
2429 * Returns the offset and size of the next buffer
2430 * Position is the position of the buffer (after tag and size)
2432 static GstFlowReturn
2433 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2434 guint32 * tag, guint * size)
2436 guint64 off = *offset;
2441 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2442 if (res != GST_FLOW_OK)
2444 if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
2445 off += 8 + 4; /* skip tag + size + subtag */
2457 * gst_avi_demux_stream_scan:
2458 * @avi: calling element (used for debugging/errors).
2459 * @index: list of index entries, returned by this function.
2460 * @alloc_list: list of allocated data, returned by this function.
2462 * Scan the file for all chunks to "create" a new index.
2463 * Return value indicates if we can continue reading the stream. It
2464 * does not say anything about whether we created an index.
2469 gst_avi_demux_stream_scan (GstAviDemux * avi,
2470 GList ** index, GList ** alloc_list)
2473 gst_avi_index_entry *entry, *entries = NULL;
2474 avi_stream_context *stream;
2476 guint64 pos = avi->offset;
2481 guint index_size = 0;
2484 * - implement non-seekable source support.
2486 GST_DEBUG_OBJECT (avi,
2487 "Creating index %s existing index, starting at offset %" G_GUINT64_FORMAT,
2488 ((*index) ? "with" : "without"), pos);
2490 format = GST_FORMAT_BYTES;
2491 if (!gst_pad_query_peer_duration (avi->sinkpad, &format, &tmplength))
2497 entry = g_list_last (*index)->data;
2498 pos = entry->offset + avi->index_offset + entry->size;
2499 if (entry->size & 1)
2502 if (pos >= length) {
2503 GST_LOG_OBJECT (avi, "Complete index, we're done");
2507 GST_LOG_OBJECT (avi, "Incomplete index, seeking to last valid entry @ %"
2508 G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT " (%"
2509 G_GUINT64_FORMAT "+%u)", pos, length, entry->offset, entry->size);
2516 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
2517 if (G_UNLIKELY (res != GST_FLOW_OK))
2520 /* check valid stream */
2521 stream_nr = CHUNKID_TO_STREAMNR (tag);
2522 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2523 GST_WARNING_OBJECT (avi,
2524 "Index entry has invalid stream nr %d", stream_nr);
2528 stream = &avi->stream[stream_nr];
2529 if (G_UNLIKELY (stream->pad == NULL)) {
2530 GST_WARNING_OBJECT (avi,
2531 "Stream %d does not have an output pad, can't create new index",
2537 if (G_UNLIKELY (index_size % 1024 == 0)) {
2538 entries = g_new (gst_avi_index_entry, 1024);
2539 *alloc_list = g_list_prepend (*alloc_list, entries);
2541 entry = &entries[index_size % 1024];
2543 entry->index_nr = index_size++;
2544 entry->stream_nr = stream_nr;
2545 entry->flags = GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
2546 entry->offset = pos - avi->index_offset;
2549 /* timestamps, get timestamps of two consecutive frames to calculate
2550 * timestamp and duration. */
2551 format = GST_FORMAT_TIME;
2552 if (stream->is_vbr) {
2554 entry->ts = avi_stream_convert_frames_to_time_unchecked (stream,
2555 stream->total_frames);
2556 entry->dur = avi_stream_convert_frames_to_time_unchecked (stream,
2557 stream->total_frames + 1);
2559 /* constant rate stream */
2560 entry->ts = avi_stream_convert_bytes_to_time_unchecked (stream,
2561 stream->total_bytes);
2562 entry->dur = avi_stream_convert_bytes_to_time_unchecked (stream,
2563 stream->total_bytes + entry->size);
2565 entry->dur -= entry->ts;
2567 /* stream position */
2568 entry->bytes_before = stream->total_bytes;
2569 stream->total_bytes += entry->size;
2570 entry->frames_before = stream->total_frames;
2571 stream->total_frames++;
2572 stream->idx_duration = entry->ts + entry->dur;
2574 list = g_list_prepend (list, entry);
2575 GST_DEBUG_OBJECT (avi, "Added index entry %d (in stream: %d), offset %"
2576 G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT " for stream %d",
2577 index_size - 1, entry->frames_before, entry->offset,
2578 GST_TIME_ARGS (entry->ts), entry->stream_nr);
2581 /* update position */
2582 pos += GST_ROUND_UP_2 (size);
2583 if (G_UNLIKELY (pos > length)) {
2584 GST_WARNING_OBJECT (avi,
2585 "Stopping index lookup since we are further than EOF");
2590 /* FIXME: why is this disabled */
2592 while (gst_avi_demux_sync (avi, &tag, TRUE)) {
2593 guint stream_nr = CHUNKID_TO_STREAMNR (tag);
2595 GstFormat format = GST_FORMAT_TIME;
2597 if (stream_nr >= avi->num_streams)
2599 stream = &avi->stream[stream_nr];
2601 /* get chunk size */
2602 if (gst_bytestream_peek_bytes (riff->bs, &data, 8) != 8)
2606 entry->index_nr = index_size++;
2607 entry->stream_nr = stream_nr;
2608 entry->flags = GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
2609 entry->offset = gst_bytestream_tell (riff->bs) + 8 - avi->index_offset;
2610 entry->size = GST_READ_UINT32_LE (&data[4]);
2613 if (stream->is_vbr) {
2615 entry->ts = avi_stream_convert_frames_to_time_unchecked (stream,
2616 stream->total_frames);
2617 entry->dur = avi_stream_convert_frames_to_time_unchecked (stream,
2618 stream->total_frames + 1);
2620 /* constant rate stream */
2621 entry->ts = avi_stream_convert_bytes_to_time_unchecked (stream,
2622 stream->total_bytes);
2623 entry->dur = avi_stream_convert_bytes_to_time_unchecked (stream,
2624 stream->total_bytes + entry->size);
2626 entry->dur -= entry->ts;
2628 /* stream position */
2629 entry->bytes_before = stream->total_bytes;
2630 stream->total_bytes += entry->size;
2631 entry->frames_before = stream->total_frames;
2632 stream->total_frames++;
2634 list = g_list_prepend (list, entry);
2635 GST_DEBUG_OBJECT (avi, "Added index entry %d (in stream: %d), offset %"
2636 G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT " for stream %d",
2637 index_size - 1, entry->frames_before, entry->offset,
2638 GST_TIME_ARGS (entry->ts), entry->stream_nr);
2641 if (!gst_avi_demux_skip (avi, TRUE))
2645 if (!(event = gst_riff_read_seek (riff, pos))) {
2649 gst_event_unref (event);
2653 GST_DEBUG_OBJECT (avi, "index created, %d items", index_size);
2655 *index = g_list_concat (*index, g_list_reverse (list));
2661 * gst_avi_demux_massage_index:
2662 * @avi: calling element (used for debugging/errors).
2664 * We're going to go over each entry in the index and finetune
2665 * some things we don't like about AVI. For example, a single
2666 * chunk might be too long. Also, individual streams might be
2667 * out-of-sync. In the first case, we cut the chunk in several
2668 * smaller pieces. In the second case, we re-order chunk reading
2669 * order. The end result should be a smoother playing AVI.
2672 gst_avi_demux_massage_index (GstAviDemux * avi,
2673 GList * list, GList * alloc_list)
2675 gst_avi_index_entry *entry;
2676 avi_stream_context *stream;
2679 gint64 delay = G_GINT64_CONSTANT (0);
2681 GST_LOG_OBJECT (avi, "Starting index massage, nr_entries = %d",
2682 list ? g_list_length (list) : 0);
2685 #ifndef GST_DISABLE_GST_DEBUG
2686 guint num_added_total = 0;
2687 guint num_per_stream[GST_AVI_DEMUX_MAX_STREAMS] = { 0, };
2689 GST_LOG_OBJECT (avi,
2690 "I'm now going to cut large chunks into smaller pieces");
2692 /* cut chunks in small (seekable) pieces
2693 * FIXME: this should be a property where a value of
2694 * GST_CLOCK_TIME_NONE would disable the chunking
2696 #define MAX_DURATION (GST_SECOND / 2)
2697 for (i = 0; i < avi->num_streams; i++) {
2698 /* only chop streams that have exactly *one* chunk */
2699 if (avi->stream[i].total_frames != 1)
2702 for (node = list; node != NULL; node = node->next) {
2705 if (entry->stream_nr != i)
2708 /* check for max duration of a single buffer. I suppose that
2709 * the allocation of index entries could be improved. */
2710 stream = &avi->stream[entry->stream_nr];
2711 if (entry->dur > MAX_DURATION
2712 && stream->strh->type == GST_RIFF_FCC_auds) {
2714 gst_avi_index_entry *entries;
2715 guint old_size, num_added;
2718 /* cut in 1/10th of a second */
2719 ideal_size = stream->strf.auds->av_bps / 10;
2721 /* ensure chunk size is multiple of blockalign */
2722 if (stream->strf.auds->blockalign > 1)
2723 ideal_size -= ideal_size % stream->strf.auds->blockalign;
2726 old_size = entry->size;
2727 num_added = (entry->size - 1) / ideal_size;
2728 avi->index_size += num_added;
2729 entries = g_malloc (sizeof (gst_avi_index_entry) * num_added);
2730 alloc_list = g_list_prepend (alloc_list, entries);
2731 for (node2 = node->next; node2 != NULL; node2 = node2->next) {
2732 gst_avi_index_entry *entry2 = node2->data;
2734 entry2->index_nr += num_added;
2735 if (entry2->stream_nr == entry->stream_nr)
2736 entry2->frames_before += num_added;
2739 /* new sized index chunks */
2740 for (i = 0; i < num_added + 1; i++) {
2741 gst_avi_index_entry *entry2;
2746 entry2 = &entries[i - 1];
2747 list = g_list_insert_before (list, node->next, entry2);
2750 memcpy (entry2, entry, sizeof (gst_avi_index_entry));
2753 if (old_size >= ideal_size) {
2754 entry2->size = ideal_size;
2755 old_size -= ideal_size;
2757 entry2->size = old_size;
2760 entry2->dur = GST_SECOND * entry2->size / stream->strf.auds->av_bps;
2763 entry2->ts += entry->dur;
2764 entry2->offset += entry->size;
2765 entry2->bytes_before += entry->size;
2766 entry2->frames_before++;
2769 #ifndef GST_DISABLE_GST_DEBUG
2770 num_added_total += num_added;
2775 #ifndef GST_DISABLE_GST_DEBUG
2776 if (num_added_total)
2777 GST_LOG ("added %u new index entries", num_added_total);
2780 GST_LOG_OBJECT (avi, "I'm now going to reorder the index entries for time");
2782 /* re-order for time */
2783 list = g_list_sort (list, (GCompareFunc) sort);
2785 /* make a continous array out of the list */
2786 avi->index_size = g_list_length (list);
2787 avi->index_entries = g_try_new (gst_avi_index_entry, avi->index_size);
2788 if (!avi->index_entries)
2791 entry = (gst_avi_index_entry *) (list->data);
2794 GST_LOG_OBJECT (avi,
2795 "Building index array, nr_entries = %d (time offset = %"
2796 GST_TIME_FORMAT, avi->index_size, GST_TIME_ARGS (delay));
2798 for (i = 0, node = list; node != NULL; node = node->next, i++) {
2800 entry->index_nr = i;
2802 memcpy (&avi->index_entries[i], entry, sizeof (gst_avi_index_entry));
2803 #ifndef GST_DISABLE_GST_DEBUG
2804 num_per_stream[entry->stream_nr]++;
2807 GST_LOG_OBJECT (avi, "Sorted index entry %3d for stream %d of size %6u"
2808 " at offset %7" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT
2809 " dur %" GST_TIME_FORMAT,
2810 avi->index_entries[i].index_nr, entry->stream_nr, entry->size,
2811 entry->offset, GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (entry->dur));
2814 for (i = 0; i < avi->num_streams; i++) {
2815 stream = &avi->stream[i];
2816 stream->idx_duration -= delay;
2819 #ifndef GST_DISABLE_GST_DEBUG
2821 gchar str[GST_AVI_DEMUX_MAX_STREAMS * (1 + 6 + 2)];
2824 for (i = 0; i < avi->num_streams; i++) {
2825 if (!avi->stream[i].pad)
2827 pad_name = GST_OBJECT_NAME (avi->stream[i].pad);
2828 sprintf (&str[i * (1 + 6 + 2)], " %6u %c", num_per_stream[i],
2831 GST_LOG_OBJECT (avi, "indizies per stream:%20s", str);
2835 GST_LOG_OBJECT (avi, "Freeing original index list");
2836 /* all the node->data in list point to alloc_list chunks */
2841 g_list_foreach (alloc_list, (GFunc) g_free, NULL);
2842 g_list_free (alloc_list);
2844 #ifndef GST_DISABLE_GST_DEBUG
2845 for (i = 0; i < avi->num_streams; i++) {
2846 GST_LOG_OBJECT (avi, "Stream %d, %d frames, %8" G_GUINT64_FORMAT " bytes",
2847 i, avi->stream[i].total_frames, avi->stream[i].total_bytes);
2851 GST_LOG_OBJECT (avi, "Index massaging done");
2856 GST_WARNING_OBJECT (avi, "Out of memory for %" G_GSIZE_FORMAT " bytes",
2857 sizeof (gst_avi_index_entry) * avi->index_size);
2862 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
2867 total = GST_CLOCK_TIME_NONE;
2869 /* all streams start at a timestamp 0 */
2870 for (stream = 0; stream < avi->num_streams; stream++) {
2871 GstClockTime duration, hduration;
2872 avi_stream_context *streamc = &avi->stream[stream];
2873 gst_riff_strh *strh = streamc->strh;
2878 /* get header duration for the stream */
2879 hduration = streamc->hdr_duration;
2881 /* index duration calculated during parsing, invariant under massage */
2882 duration = streamc->idx_duration;
2884 /* now pick a good duration */
2885 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2886 /* index gave valid duration, use that */
2887 GST_INFO ("Stream %d duration according to index: %" GST_TIME_FORMAT,
2888 stream, GST_TIME_ARGS (duration));
2890 /* fall back to header info to calculate a duration */
2891 duration = hduration;
2893 /* set duration for the stream */
2894 streamc->duration = duration;
2896 /* find total duration */
2897 if (total == GST_CLOCK_TIME_NONE || duration > total)
2901 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
2902 /* now update the duration for those streams where we had none */
2903 for (stream = 0; stream < avi->num_streams; stream++) {
2904 avi_stream_context *streamc = &avi->stream[stream];
2906 if (!GST_CLOCK_TIME_IS_VALID (streamc->duration)
2907 || streamc->duration == 0) {
2908 streamc->duration = total;
2910 GST_INFO ("Stream %d duration according to total: %" GST_TIME_FORMAT,
2911 stream, GST_TIME_ARGS (total));
2916 /* and set the total duration in the segment. */
2917 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
2918 GST_TIME_ARGS (total));
2920 gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
2923 /* returns FALSE if there are no pads to deliver event to,
2924 * otherwise TRUE (whatever the outcome of event sending) */
2926 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
2928 gboolean result = FALSE;
2931 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
2932 GST_EVENT_TYPE_NAME (event), avi->num_streams);
2934 if (avi->num_streams) {
2935 for (i = 0; i < avi->num_streams; i++) {
2936 avi_stream_context *stream = &avi->stream[i];
2940 gst_pad_push_event (stream->pad, gst_event_ref (event));
2944 gst_event_unref (event);
2949 * Read AVI headers when streaming
2951 static GstFlowReturn
2952 gst_avi_demux_stream_header_push (GstAviDemux * avi)
2954 GstFlowReturn ret = GST_FLOW_OK;
2959 GstBuffer *buf = NULL, *sub = NULL;
2963 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
2965 switch (avi->header_state) {
2966 case GST_AVI_DEMUX_HEADER_TAG_LIST:
2967 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2968 avi->offset += 8 + ((size + 1) & ~1);
2969 if (tag != GST_RIFF_TAG_LIST)
2970 goto header_no_list;
2972 gst_adapter_flush (avi->adapter, 8);
2973 /* Find the 'hdrl' LIST tag */
2974 GST_DEBUG ("Reading %d bytes", size);
2975 buf = gst_adapter_take_buffer (avi->adapter, size);
2977 if (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl)
2978 goto header_no_hdrl;
2982 gst_adapter_flush (avi->adapter, 1);
2984 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
2986 /* the hdrl starts with a 'avih' header */
2987 if (!gst_riff_parse_chunk (GST_ELEMENT (avi), buf, &offset, &tag, &sub))
2988 goto header_no_avih;
2990 if (tag != GST_RIFF_TAG_avih)
2991 goto header_no_avih;
2993 if (!gst_avi_demux_parse_avih (GST_ELEMENT (avi), sub, &avi->avih))
2994 goto header_wrong_avih;
2996 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
2998 /* now, read the elements from the header until the end */
2999 while (gst_riff_parse_chunk (GST_ELEMENT (avi), buf, &offset, &tag,
3001 /* sub can be NULL on empty tags */
3006 case GST_RIFF_TAG_LIST:
3007 if (GST_BUFFER_SIZE (sub) < 4)
3010 switch (GST_READ_UINT32_LE (GST_BUFFER_DATA (sub))) {
3011 case GST_RIFF_LIST_strl:
3012 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3014 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3015 ("failed to parse stream, ignoring"));
3020 case GST_RIFF_LIST_odml:
3021 gst_avi_demux_parse_odml (avi, sub);
3025 GST_WARNING_OBJECT (avi,
3026 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3027 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA
3030 case GST_RIFF_TAG_JUNK:
3035 GST_WARNING_OBJECT (avi,
3036 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
3037 offset, GST_FOURCC_ARGS (tag));
3039 case GST_RIFF_TAG_JUNK:
3041 /* move to next chunk */
3043 gst_buffer_unref (sub);
3048 gst_buffer_unref (buf);
3049 GST_DEBUG ("elements parsed");
3051 /* check parsed streams */
3052 if (avi->num_streams == 0) {
3054 } else if (avi->num_streams != avi->avih->streams) {
3055 GST_WARNING_OBJECT (avi,
3056 "Stream header mentioned %d streams, but %d available",
3057 avi->avih->streams, avi->num_streams);
3059 GST_DEBUG ("Get junk and info next");
3060 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
3062 /* Need more data */
3066 case GST_AVI_DEMUX_HEADER_INFO:
3067 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
3069 if (gst_adapter_available (avi->adapter) < 12)
3072 data = gst_adapter_peek (avi->adapter, 12);
3073 tag = GST_READ_UINT32_LE (data);
3074 size = GST_READ_UINT32_LE (data + 4);
3075 ltag = GST_READ_UINT32_LE (data + 8);
3077 if (tag == GST_RIFF_TAG_LIST) {
3079 case GST_RIFF_LIST_movi:
3080 gst_adapter_flush (avi->adapter, 12);
3083 case GST_RIFF_LIST_INFO:
3084 GST_DEBUG ("Found INFO chunk");
3085 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3086 GST_DEBUG ("got size %d", size);
3088 gst_adapter_flush (avi->adapter, 12);
3090 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
3093 gst_adapter_flush (avi->adapter, 1);
3094 gst_riff_parse_info (GST_ELEMENT (avi), buf,
3096 gst_buffer_unref (buf);
3098 avi->offset += ((size + 1) & ~1) - 4;
3100 GST_DEBUG ("skipping INFO LIST prefix");
3103 /* Need more data */
3108 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3109 avi->offset += 8 + ((size + 1) & ~1);
3110 gst_adapter_flush (avi->adapter, 8 + ((size + 1) & ~1));
3111 // ??? goto iterate; ???
3113 /* Need more data */
3119 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3120 avi->offset += 8 + ((size + 1) & ~1);
3121 gst_adapter_flush (avi->adapter, 8 + ((size + 1) & ~1));
3124 /* Need more data */
3131 GST_WARNING ("unhandled header state: %d", avi->header_state);
3136 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3137 avi->num_streams, avi->stream[0].indexes);
3139 GST_DEBUG ("Found movi chunk. Starting to stream data");
3140 avi->state = GST_AVI_DEMUX_MOVI;
3143 /*GList *index = NULL, *alloc = NULL; */
3145 /* ######################## this need to be integrated with the state */
3146 /* create or read stream index (for seeking) */
3147 if (avi->stream[0].indexes != NULL) {
3148 gst_avi_demux_read_subindexes_push (avi, &index, &alloc);
3151 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX) {
3152 gst_avi_demux_stream_index (avi, &index, &alloc);
3154 /* some indexes are incomplete, continue streaming from there */
3156 gst_avi_demux_stream_scan (avi, &index, &alloc);
3159 /* this is a fatal error */
3163 if (!gst_avi_demux_massage_index (avi, index, alloc))
3166 gst_avi_demux_calculate_durations_from_index (avi);
3167 /* ######################## */
3170 /* create initial NEWSEGMENT event */
3171 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3172 stop = avi->segment.duration;
3174 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3176 if (avi->seek_event)
3177 gst_event_unref (avi->seek_event);
3178 avi->seek_event = gst_event_new_new_segment
3179 (FALSE, avi->segment.rate, GST_FORMAT_TIME,
3180 avi->segment.start, stop, avi->segment.start);
3182 /* at this point we know all the streams and we can signal the no more
3184 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3185 gst_element_no_more_pads (GST_ELEMENT (avi));
3192 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3193 return GST_FLOW_ERROR;
3197 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3198 ("Invalid AVI header (no LIST at start): %"
3199 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3200 return GST_FLOW_ERROR;
3204 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3205 ("Invalid AVI header (no hdrl at start): %"
3206 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3207 gst_buffer_unref (buf);
3208 return GST_FLOW_ERROR;
3212 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3213 ("Invalid AVI header (no avih at start): %"
3214 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3216 gst_buffer_unref (sub);
3218 gst_buffer_unref (buf);
3219 return GST_FLOW_ERROR;
3223 gst_buffer_unref (buf);
3224 return GST_FLOW_ERROR;
3229 * Read full AVI headers.
3231 static GstFlowReturn
3232 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
3235 GstBuffer *buf, *sub = NULL;
3237 GList *index = NULL, *alloc = NULL;
3240 GstElement *element = GST_ELEMENT_CAST (avi);
3242 /* the header consists of a 'hdrl' LIST tag */
3243 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3244 if (res != GST_FLOW_OK)
3245 goto pull_range_failed;
3246 else if (tag != GST_RIFF_TAG_LIST)
3248 else if (GST_BUFFER_SIZE (buf) < 4)
3251 GST_DEBUG_OBJECT (avi, "parsing headers");
3253 /* Find the 'hdrl' LIST tag */
3254 while (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl) {
3255 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
3256 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf))));
3259 gst_buffer_unref (buf);
3261 /* read new chunk */
3262 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3263 if (res != GST_FLOW_OK)
3264 goto pull_range_failed;
3265 else if (tag != GST_RIFF_TAG_LIST)
3267 else if (GST_BUFFER_SIZE (buf) < 4)
3271 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
3273 /* the hdrl starts with a 'avih' header */
3274 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
3276 else if (tag != GST_RIFF_TAG_avih)
3278 else if (!gst_avi_demux_parse_avih (element, sub, &avi->avih))
3281 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3283 /* now, read the elements from the header until the end */
3284 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
3285 /* sub can be NULL on empty tags */
3290 case GST_RIFF_TAG_LIST:
3295 if (GST_BUFFER_SIZE (sub) < 4)
3298 data = GST_BUFFER_DATA (sub);
3299 fourcc = GST_READ_UINT32_LE (data);
3302 case GST_RIFF_LIST_strl:
3303 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3304 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3305 ("failed to parse stream, ignoring"));
3310 case GST_RIFF_LIST_odml:
3311 gst_avi_demux_parse_odml (avi, sub);
3315 GST_WARNING_OBJECT (avi,
3316 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3317 GST_FOURCC_ARGS (fourcc));
3318 GST_MEMDUMP_OBJECT (avi, "Unknown list", GST_BUFFER_DATA (sub),
3319 GST_BUFFER_SIZE (sub));
3321 case GST_RIFF_TAG_JUNK:
3327 GST_WARNING_OBJECT (avi,
3328 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
3329 GST_FOURCC_ARGS (tag), offset);
3330 GST_MEMDUMP_OBJECT (avi, "Unknown tag", GST_BUFFER_DATA (sub),
3331 GST_BUFFER_SIZE (sub));
3333 case GST_RIFF_TAG_JUNK:
3336 gst_buffer_unref (sub);
3341 gst_buffer_unref (buf);
3342 GST_DEBUG ("elements parsed");
3344 /* check parsed streams */
3345 if (avi->num_streams == 0)
3347 else if (avi->num_streams != avi->avih->streams) {
3348 GST_WARNING_OBJECT (avi,
3349 "Stream header mentioned %d streams, but %d available",
3350 avi->avih->streams, avi->num_streams);
3353 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
3354 G_GUINT64_FORMAT, avi->offset);
3356 /* Now, find the data (i.e. skip all junk between header and data) */
3361 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
3362 if (res != GST_FLOW_OK) {
3363 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
3364 goto pull_range_failed;
3365 } else if (GST_BUFFER_SIZE (buf) < 12) {
3366 GST_DEBUG_OBJECT (avi, "got %d bytes which is less than 12 bytes",
3367 GST_BUFFER_SIZE (buf));
3368 gst_buffer_unref (buf);
3369 return GST_FLOW_ERROR;
3372 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
3373 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
3374 ltag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 8);
3376 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
3377 GST_FOURCC_ARGS (tag), size);
3378 GST_MEMDUMP ("Tag content", GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
3379 gst_buffer_unref (buf);
3382 case GST_RIFF_TAG_LIST:{
3384 case GST_RIFF_LIST_movi:
3385 GST_DEBUG_OBJECT (avi,
3386 "Reached the 'movi' tag, we're done with skipping");
3388 case GST_RIFF_LIST_INFO:
3390 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3392 if (res != GST_FLOW_OK) {
3393 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3394 goto pull_range_failed;
3396 GST_DEBUG ("got size %u", GST_BUFFER_SIZE (buf));
3398 GST_DEBUG ("skipping INFO LIST prefix");
3399 avi->offset += (4 - GST_ROUND_UP_2 (size));
3400 gst_buffer_unref (buf);
3404 sub = gst_buffer_create_sub (buf, 4, GST_BUFFER_SIZE (buf) - 4);
3405 gst_riff_parse_info (element, sub, &avi->globaltags);
3407 gst_buffer_unref (sub);
3410 gst_buffer_unref (buf);
3411 /* gst_riff_read_chunk() has already advanced avi->offset */
3414 GST_WARNING_OBJECT (avi,
3415 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3416 GST_FOURCC_ARGS (ltag));
3417 avi->offset += 8 + ((size + 1) & ~1);
3423 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3424 GST_FOURCC_ARGS (tag));
3426 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3427 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3428 avi->offset += 8 + ((size + 1) & ~1);
3434 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3435 avi->num_streams, avi->stream[0].indexes);
3437 /* create or read stream index (for seeking) */
3438 if (avi->stream[0].indexes != NULL) {
3439 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3440 gst_avi_demux_read_subindexes_pull (avi, &index, &alloc);
3443 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX) {
3444 gst_avi_demux_stream_index (avi, &index, &alloc);
3446 /* some indexes are incomplete, continue streaming from there */
3448 gst_avi_demux_stream_scan (avi, &index, &alloc);
3451 /* this is a fatal error */
3455 if (!gst_avi_demux_massage_index (avi, index, alloc))
3458 gst_avi_demux_calculate_durations_from_index (avi);
3460 /* create initial NEWSEGMENT event */
3461 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3462 stop = avi->segment.duration;
3464 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3466 if (avi->seek_event)
3467 gst_event_unref (avi->seek_event);
3468 avi->seek_event = gst_event_new_new_segment
3469 (FALSE, avi->segment.rate, GST_FORMAT_TIME,
3470 avi->segment.start, stop, avi->segment.start);
3472 /* at this point we know all the streams and we can signal the no more
3474 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3475 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3482 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3483 ("Invalid AVI header (no LIST at start): %"
3484 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3485 gst_buffer_unref (buf);
3486 return GST_FLOW_ERROR;
3490 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3491 ("Invalid AVI header (no hdrl at start): %"
3492 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3493 gst_buffer_unref (buf);
3494 return GST_FLOW_ERROR;
3498 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3499 ("Invalid AVI header (no avih at start): %"
3500 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3502 gst_buffer_unref (sub);
3503 gst_buffer_unref (buf);
3504 return GST_FLOW_ERROR;
3508 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3509 ("Invalid AVI header (cannot parse avih at start)"));
3510 gst_buffer_unref (buf);
3511 return GST_FLOW_ERROR;
3515 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3516 return GST_FLOW_ERROR;
3520 GST_WARNING ("file without or too big index");
3521 g_list_free (index);
3522 g_list_foreach (alloc, (GFunc) g_free, NULL);
3523 g_list_free (alloc);
3525 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3526 ("Could not get/create index"));
3527 return GST_FLOW_ERROR;
3531 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3532 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3533 return GST_FLOW_ERROR;
3538 * Do the actual seeking.
3541 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
3543 GstClockTime seek_time;
3545 gst_avi_index_entry *entry, *kentry;
3548 seek_time = segment->last_stop;
3549 keyframe = !!(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
3551 /* FIXME: if we seek in an openDML file, we will have multiple
3552 * primary levels. Seeking in between those will cause havoc. */
3554 /* save old position so we can see if we must mark a discont. */
3555 old_entry = avi->current_entry;
3557 /* get the entry for the requested position, which is always in last_stop.
3558 * we search the index entry for stream 0, since all entries are sorted by
3559 * time and stream we automagically are positioned for the other streams as
3560 * well. FIXME, this code assumes the main stream with keyframes is stream 0,
3561 * which is mostly correct... */
3562 if (!(entry = gst_avi_demux_index_entry_for_time (avi, 0, seek_time)))
3565 GST_DEBUG_OBJECT (avi,
3566 "Got requested entry %d [stream:%d / ts:%" GST_TIME_FORMAT
3567 " / duration:%" GST_TIME_FORMAT "]", entry->index_nr,
3568 entry->stream_nr, GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (entry->dur));
3570 /* check if we are already on a keyframe */
3571 if (!(entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)) {
3572 /* now go to the previous keyframe, this is where we should start
3574 if (!(kentry = gst_avi_demux_index_prev (avi, 0, entry->index_nr,
3575 GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME))) {
3579 /* we were on a keyframe */
3583 GST_DEBUG_OBJECT (avi,
3584 "Got keyframe entry %d [stream:%d / ts:%" GST_TIME_FORMAT
3585 " / duration:%" GST_TIME_FORMAT "]", kentry->index_nr,
3586 entry->stream_nr, GST_TIME_ARGS (kentry->ts),
3587 GST_TIME_ARGS (kentry->dur));
3589 /* we must start decoding at the keyframe */
3590 avi->current_entry = kentry->index_nr;
3592 if (segment->rate < 0.0) {
3593 gst_avi_index_entry *next_keyframe;
3595 /* Because we don't know the frame order we need to push from the prev keyframe
3596 * to the next keyframe. If there is a smart decoder downstream he will notice
3597 * that there are too many encoded frames send and return UNEXPECTED when there
3598 * are enough decoded frames to fill the segment.
3601 gst_avi_demux_index_next (avi, 0, kentry->index_nr,
3602 GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME);
3604 next_keyframe = gst_avi_demux_index_last (avi, 0);
3606 avi->reverse_start_index = kentry->index_nr;
3607 avi->reverse_stop_index = next_keyframe->index_nr;
3609 GST_DEBUG_OBJECT (avi, "reverse seek: start idx (%d) and stop idx (%d)",
3610 avi->reverse_start_index, avi->reverse_stop_index);
3614 /* when seeking to a keyframe, we update the result seek time
3615 * to the time of the keyframe. */
3616 seek_time = avi->index_entries[avi->current_entry].ts;
3620 /* if we changed position, mark a DISCONT on all streams */
3621 if (avi->current_entry != old_entry) {
3624 for (i = 0; i < avi->num_streams; i++) {
3625 avi->stream[i].discont = TRUE;
3629 GST_DEBUG_OBJECT (avi, "seek: %" GST_TIME_FORMAT
3630 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
3632 /* the seek time is also the last_stop and stream time */
3633 segment->last_stop = seek_time;
3634 segment->time = seek_time;
3640 /* we could not find an entry for the given time */
3641 GST_WARNING_OBJECT (avi,
3642 "Couldn't find AviIndexEntry for time:%" GST_TIME_FORMAT,
3643 GST_TIME_ARGS (seek_time));
3644 if (avi->current_entry >= avi->index_size && avi->index_size > 0)
3645 avi->current_entry = avi->index_size - 1;
3652 * Handle seek event.
3655 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
3660 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
3664 GstSegment seeksegment = { 0, };
3667 GST_DEBUG_OBJECT (avi, "doing seek with event");
3669 gst_event_parse_seek (event, &rate, &format, &flags,
3670 &cur_type, &cur, &stop_type, &stop);
3672 /* we have to have a format as the segment format. Try to convert
3674 if (format != GST_FORMAT_TIME) {
3675 GstFormat fmt = GST_FORMAT_TIME;
3676 gboolean res = TRUE;
3678 if (cur_type != GST_SEEK_TYPE_NONE)
3679 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
3680 if (res && stop_type != GST_SEEK_TYPE_NONE)
3681 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
3687 GST_DEBUG_OBJECT (avi,
3688 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
3689 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
3690 /* FIXME: can we do anything with rate!=1.0 */
3692 GST_DEBUG_OBJECT (avi, "doing seek without event");
3697 /* save flush flag */
3698 flush = flags & GST_SEEK_FLAG_FLUSH;
3701 GstEvent *event = gst_event_new_flush_start ();
3703 /* for a flushing seek, we send a flush_start on all pads. This will
3704 * eventually stop streaming with a WRONG_STATE. We can thus eventually
3705 * take the STREAM_LOCK. */
3706 GST_DEBUG_OBJECT (avi, "sending flush start");
3707 gst_avi_demux_push_event (avi, gst_event_ref (event));
3708 gst_pad_push_event (avi->sinkpad, event);
3710 /* a non-flushing seek, we PAUSE the task so that we can take the
3712 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
3713 gst_pad_pause_task (avi->sinkpad);
3716 /* wait for streaming to stop */
3717 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
3718 GST_PAD_STREAM_LOCK (avi->sinkpad);
3720 /* copy segment, we need this because we still need the old
3721 * segment when we close the current segment. */
3722 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
3725 GST_DEBUG_OBJECT (avi, "configuring seek");
3726 gst_segment_set_seek (&seeksegment, rate, format, flags,
3727 cur_type, cur, stop_type, stop, &update);
3730 /* do the seek, seeksegment.last_stop contains the new position, this
3731 * actually never fails. */
3732 gst_avi_demux_do_seek (avi, &seeksegment);
3737 GST_DEBUG_OBJECT (avi, "sending flush stop");
3738 gst_avi_demux_push_event (avi, gst_event_new_flush_stop ());
3739 gst_pad_push_event (avi->sinkpad, gst_event_new_flush_stop ());
3740 /* reset the last flow and mark discont, FLUSH is always DISCONT */
3741 for (i = 0; i < avi->num_streams; i++) {
3742 avi->stream[i].last_flow = GST_FLOW_OK;
3743 avi->stream[i].discont = TRUE;
3745 } else if (avi->segment_running) {
3748 /* we are running the current segment and doing a non-flushing seek,
3749 * close the segment first based on the last_stop. */
3750 GST_DEBUG_OBJECT (avi, "closing running segment %" G_GINT64_FORMAT
3751 " to %" G_GINT64_FORMAT, avi->segment.start, avi->segment.last_stop);
3752 seg = gst_event_new_new_segment (TRUE,
3753 avi->segment.rate, avi->segment.format,
3754 avi->segment.start, avi->segment.last_stop, avi->segment.time);
3755 gst_avi_demux_push_event (avi, seg);
3758 /* now update the real segment info */
3759 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
3761 /* post the SEGMENT_START message when we do segmented playback */
3762 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3763 gst_element_post_message (GST_ELEMENT (avi),
3764 gst_message_new_segment_start (GST_OBJECT (avi),
3765 avi->segment.format, avi->segment.last_stop));
3768 /* prepare for streaming again */
3769 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3770 stop = avi->segment.duration;
3772 /* queue the segment event for the streaming thread. */
3773 if (avi->seek_event)
3774 gst_event_unref (avi->seek_event);
3775 if (avi->segment.rate > 0.0) {
3776 avi->seek_event = gst_event_new_new_segment (FALSE,
3777 avi->segment.rate, avi->segment.format,
3778 avi->segment.last_stop, stop, avi->segment.time);
3780 avi->seek_event = gst_event_new_new_segment (FALSE,
3781 avi->segment.rate, avi->segment.format,
3782 avi->segment.start, avi->segment.last_stop, avi->segment.start);
3785 if (!avi->streaming) {
3786 avi->segment_running = TRUE;
3787 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
3790 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
3797 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
3803 * Helper for gst_avi_demux_invert()
3806 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
3808 memcpy (tmp, d1, bytes);
3809 memcpy (d1, d2, bytes);
3810 memcpy (d2, tmp, bytes);
3814 #define gst_avi_demux_is_uncompressed(fourcc) \
3815 (fourcc == GST_RIFF_DIB || \
3816 fourcc == GST_RIFF_rgb || \
3817 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW)
3820 * Invert DIB buffers... Takes existing buffer and
3821 * returns either the buffer or a new one (with old
3822 * one dereferenced).
3823 * FIXME: can't we preallocate tmp? and remember stride, bpp?
3826 gst_avi_demux_invert (avi_stream_context * stream, GstBuffer * buf)
3833 if (stream->strh->type != GST_RIFF_FCC_vids)
3836 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
3837 return buf; /* Ignore non DIB buffers */
3840 s = gst_caps_get_structure (GST_PAD_CAPS (stream->pad), 0);
3841 if (!gst_structure_get_int (s, "bpp", &bpp)) {
3842 GST_WARNING ("Failed to retrieve depth from caps");
3846 if (stream->strf.vids == NULL) {
3847 GST_WARNING ("Failed to retrieve vids for stream");
3851 h = stream->strf.vids->height;
3852 w = stream->strf.vids->width;
3853 stride = w * (bpp / 8);
3855 buf = gst_buffer_make_writable (buf);
3856 if (GST_BUFFER_SIZE (buf) < (stride * h)) {
3857 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
3861 tmp = g_malloc (stride);
3863 for (y = 0; y < h / 2; y++) {
3864 swap_line (GST_BUFFER_DATA (buf) + stride * y,
3865 GST_BUFFER_DATA (buf) + stride * (h - 1 - y), tmp, stride);
3874 * Returns the aggregated GstFlowReturn.
3876 static GstFlowReturn
3877 gst_avi_demux_combine_flows (GstAviDemux * avi, avi_stream_context * stream,
3882 /* store the value */
3883 stream->last_flow = ret;
3885 /* any other error that is not-linked can be returned right away */
3886 if (G_UNLIKELY (ret != GST_FLOW_NOT_LINKED))
3889 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3890 for (i = 0; i < avi->num_streams; i++) {
3891 avi_stream_context *ostream = &avi->stream[i];
3893 ret = ostream->last_flow;
3894 /* some other return value (must be SUCCESS but we can return
3895 * other values as well) */
3896 if (G_UNLIKELY (ret != GST_FLOW_NOT_LINKED))
3899 /* if we get here, all other pads were unlinked and we return
3900 * NOT_LINKED then */
3902 GST_LOG_OBJECT (avi, "combined return %s", gst_flow_get_name (ret));
3907 * prepare the avi element for a reverse jump to a prev keyframe
3908 * this function will return the start entry. if the function returns
3909 * NULL there was no prev keyframe.
3911 static gst_avi_index_entry *
3912 gst_avi_demux_step_reverse (GstAviDemux * avi)
3914 gst_avi_index_entry *entry;
3917 avi->reverse_stop_index = avi->reverse_start_index;
3919 gst_avi_demux_index_prev (avi, 0, avi->reverse_stop_index,
3920 GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME);
3922 GST_DEBUG_OBJECT (avi, "no valid index entry found index %d",
3923 avi->reverse_stop_index);
3926 avi->current_entry = avi->reverse_start_index = entry->index_nr;
3927 GST_DEBUG_OBJECT (avi,
3928 "reverse playback jump: start idx (%d) and stop idx (%d)",
3929 avi->reverse_start_index, avi->reverse_stop_index);
3930 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, entry->ts);
3931 for (i = 0; i < avi->num_streams; i++) {
3932 avi->stream[i].last_flow = GST_FLOW_OK;
3933 avi->stream[i].discont = TRUE;
3939 * Read data from one index entry
3941 static GstFlowReturn
3942 gst_avi_demux_process_next_entry (GstAviDemux * avi)
3944 GstFlowReturn res = GST_FLOW_OK;
3945 gboolean processed = FALSE;
3946 avi_stream_context *stream;
3947 gst_avi_index_entry *entry;
3951 /* see if we are at the end */
3952 if ((avi->segment.rate > 0 && avi->current_entry >= avi->index_size))
3955 /* get next entry, this will work as we checked for the index size above */
3956 entry = &avi->index_entries[avi->current_entry++];
3958 /* check for reverse playback */
3959 if (avi->segment.rate < 0 && avi->current_entry > avi->reverse_stop_index) {
3960 GST_LOG_OBJECT (avi, "stop_index %d reached", avi->reverse_stop_index);
3962 /* check if we have pushed enough data for this segment */
3963 if (avi->reverse_start_index == 0)
3964 goto eos_reverse_zero;
3965 if (avi->index_entries[avi->reverse_start_index].ts < avi->segment.start)
3966 goto eos_reverse_segment;
3968 if (!(entry = gst_avi_demux_step_reverse (avi)))
3971 avi->current_entry++;
3974 /* see if we have a valid stream, ignore if not
3975 * FIXME: can't we check this when building the index?
3976 * we check it in _parse_index(), _stream_scan()
3978 if (entry->stream_nr >= avi->num_streams) {
3979 GST_WARNING_OBJECT (avi,
3980 "Entry %d has non-existing stream nr %d",
3981 avi->current_entry - 1, entry->stream_nr);
3985 /* get stream now */
3986 stream = &avi->stream[entry->stream_nr];
3988 if (avi->segment.rate > 0.0) {
3989 /* only check this for fowards playback for now */
3990 if ((entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)
3991 && GST_CLOCK_TIME_IS_VALID (entry->ts)
3992 && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
3993 && (entry->ts > avi->segment.stop)) {
3998 /* skip empty entries */
3999 if (entry->size == 0 || !stream->pad) {
4000 GST_DEBUG_OBJECT (avi, "Skipping entry %d (%d, %p)",
4001 avi->current_entry - 1, entry->size, stream->pad);
4005 GST_LOG ("reading buffer (size=%d) from stream %d at current pos %"
4006 G_GUINT64_FORMAT " (%llx)", entry->size, entry->stream_nr,
4007 avi->index_offset + entry->offset, avi->index_offset + entry->offset);
4009 /* pull in the data */
4010 res = gst_pad_pull_range (avi->sinkpad, entry->offset +
4011 avi->index_offset, entry->size, &buf);
4012 if (res != GST_FLOW_OK)
4015 /* check for short buffers, this is EOS as well */
4016 if (GST_BUFFER_SIZE (buf) < entry->size)
4019 /* invert the picture if needed */
4020 buf = gst_avi_demux_invert (stream, buf);
4022 /* mark non-keyframes */
4023 if (!(entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME))
4024 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4026 GST_BUFFER_TIMESTAMP (buf) = entry->ts;
4027 GST_BUFFER_DURATION (buf) = entry->dur;
4028 if (stream->strh->type == GST_RIFF_FCC_vids) {
4029 if (stream->current_frame >= 0)
4030 GST_BUFFER_OFFSET (buf) = stream->current_frame;
4033 GstFormat fmt = GST_FORMAT_DEFAULT;
4035 if (gst_pad_query_convert (stream->pad, GST_FORMAT_TIME, entry->ts,
4037 GST_BUFFER_OFFSET (buf) = framenum;
4039 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4042 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4043 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
4044 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4046 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %d, offset %"
4047 G_GUINT64_FORMAT " and time %"
4048 GST_TIME_FORMAT " on pad %s",
4049 GST_BUFFER_SIZE (buf), GST_BUFFER_OFFSET (buf),
4050 GST_TIME_ARGS (entry->ts), GST_PAD_NAME (stream->pad));
4052 /* update current position in the segment */
4053 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, entry->ts);
4055 /* mark discont when pending */
4056 if (stream->discont) {
4057 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4058 stream->discont = FALSE;
4061 res = gst_pad_push (stream->pad, buf);
4063 /* mark as processed, we increment the frame and byte counters then
4064 * leave the while loop and return the GstFlowReturn */
4066 GST_DEBUG_OBJECT (avi, "Processed buffer %d: %s", entry->index_nr,
4067 gst_flow_get_name (res));
4069 if (avi->segment.rate < 0
4070 && entry->ts > avi->segment.stop && res == GST_FLOW_UNEXPECTED) {
4071 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
4072 * we are at the end of the segment, so we just need to jump
4073 * back to the previous section.
4075 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
4077 if (!(entry = gst_avi_demux_step_reverse (avi)))
4082 stream->current_frame = entry->frames_before;
4083 stream->current_byte = entry->bytes_before;
4089 res = gst_avi_demux_combine_flows (avi, stream, res);
4092 stream->current_frame = entry->frames_before + 1;
4093 stream->current_byte = entry->bytes_before + entry->size;
4094 } while (!processed);
4097 GST_DEBUG_OBJECT (avi, "returning %s", gst_flow_get_name (res));
4104 GST_LOG_OBJECT (avi, "Handled last index entry, setting EOS (%d > %d)",
4105 avi->current_entry, avi->index_size);
4106 /* we mark the first stream as EOS */
4107 res = GST_FLOW_UNEXPECTED;
4112 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
4113 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
4114 GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (avi->segment.stop));
4115 res = GST_FLOW_UNEXPECTED;
4120 GST_DEBUG_OBJECT (avi, "start_index was 0, setting EOS");
4121 res = GST_FLOW_UNEXPECTED;
4124 eos_reverse_segment:
4126 GST_DEBUG_OBJECT (avi, "full segment pushed, setting EOS");
4127 res = GST_FLOW_UNEXPECTED;
4132 GST_DEBUG_OBJECT (avi,
4133 "pull range failed: pos=%" G_GUINT64_FORMAT " size=%d",
4134 entry->offset + avi->index_offset, entry->size);
4139 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
4140 ", only got %d/%d bytes (truncated file?)", entry->offset +
4141 avi->index_offset, GST_BUFFER_SIZE (buf), entry->size);
4142 gst_buffer_unref (buf);
4143 res = GST_FLOW_UNEXPECTED;
4149 * Read data. If we have an index it delegates to
4150 * gst_avi_demux_process_next_entry().
4152 static GstFlowReturn
4153 gst_avi_demux_stream_data (GstAviDemux * avi)
4158 GstFlowReturn res = GST_FLOW_OK;
4159 GstFormat format = GST_FORMAT_TIME;
4161 /* if we have a avi->index_entries[], we don't want to read
4162 * the stream linearly, but seek to the next ts/index_entry. */
4163 if (G_LIKELY (avi->index_entries != NULL))
4164 return gst_avi_demux_process_next_entry (avi);
4166 if (G_UNLIKELY (avi->have_eos)) {
4167 /* Clean adapter, we're done */
4168 gst_adapter_clear (avi->adapter);
4173 if (!gst_avi_demux_sync (avi, &tag, FALSE))
4177 /* Iterate until need more data, so adapter won't grow too much */
4179 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
4183 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
4184 GST_FOURCC_ARGS (tag), size);
4186 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
4187 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
4188 GST_LOG ("Chunk ok");
4189 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
4190 GST_DEBUG ("Found sub-index tag");
4191 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4192 /* accept 0 size buffer here */
4193 avi->abort_buffering = FALSE;
4194 GST_DEBUG (" skipping %d bytes for now", size);
4195 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4198 } else if (tag == GST_RIFF_TAG_idx1) {
4199 GST_DEBUG ("Found index tag, stream done");
4200 avi->have_eos = TRUE;
4201 return GST_FLOW_UNEXPECTED;
4202 } else if (tag == GST_RIFF_TAG_LIST) {
4203 /* movi chunks might be grouped in rec list */
4204 if (gst_adapter_available (avi->adapter) >= 12) {
4205 GST_DEBUG ("Found LIST tag, skipping LIST header");
4206 gst_adapter_flush (avi->adapter, 12);
4210 } else if (tag == GST_RIFF_TAG_JUNK) {
4211 /* rec list might contain JUNK chunks */
4212 GST_DEBUG ("Found JUNK tag");
4213 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4214 /* accept 0 size buffer here */
4215 avi->abort_buffering = FALSE;
4216 GST_DEBUG (" skipping %d bytes for now", size);
4217 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4221 GST_DEBUG ("No more stream chunks, send EOS");
4222 avi->have_eos = TRUE;
4223 return GST_FLOW_UNEXPECTED;
4226 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
4227 /* supposedly one hopes to catch a nicer chunk later on ... */
4228 /* FIXME ?? give up here rather than possibly ending up going
4229 * through the whole file */
4230 if (avi->abort_buffering) {
4231 avi->abort_buffering = FALSE;
4232 gst_adapter_flush (avi->adapter, 8);
4236 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
4237 GST_FOURCC_ARGS (tag), size);
4239 stream_nr = CHUNKID_TO_STREAMNR (tag);
4241 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
4243 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
4244 stream_nr, GST_FOURCC_ARGS (tag));
4245 avi->offset += 8 + ((size + 1) & ~1);
4246 gst_adapter_flush (avi->adapter, 8 + ((size + 1) & ~1));
4248 avi_stream_context *stream;
4249 GstClockTime next_ts = 0;
4252 gst_adapter_flush (avi->adapter, 8);
4255 buf = gst_adapter_take_buffer (avi->adapter, ((size + 1) & ~1));
4256 /* patch the size */
4257 GST_BUFFER_SIZE (buf) = size;
4258 avi->offset += 8 + ((size + 1) & ~1);
4260 stream = &avi->stream[stream_nr];
4262 /* set delay (if any)
4263 if (stream->strh->init_frames == stream->current_frame &&
4265 stream->delay = next_ts;
4268 stream->current_frame++;
4269 stream->current_byte += size;
4271 /* parsing of corresponding header may have failed */
4272 if (G_UNLIKELY (!stream->pad)) {
4273 GST_WARNING_OBJECT (avi, "no pad for stream ID %" GST_FOURCC_FORMAT,
4274 GST_FOURCC_ARGS (tag));
4275 gst_buffer_unref (buf);
4277 GstClockTime dur_ts = 0;
4279 /* get time of this buffer */
4280 gst_pad_query_position (stream->pad, &format, (gint64 *) & next_ts);
4281 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4284 /* invert the picture if needed */
4285 buf = gst_avi_demux_invert (stream, buf);
4287 gst_pad_query_position (stream->pad, &format, (gint64 *) & dur_ts);
4288 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4291 GST_BUFFER_TIMESTAMP (buf) = next_ts;
4292 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
4293 if (stream->strh->type == GST_RIFF_FCC_vids)
4294 GST_BUFFER_OFFSET (buf) = stream->current_frame - 1;
4296 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4298 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4299 GST_DEBUG_OBJECT (avi,
4300 "Pushing buffer with time=%" GST_TIME_FORMAT
4301 ", offset %" G_GUINT64_FORMAT " and size %d over pad %s",
4302 GST_TIME_ARGS (next_ts), GST_BUFFER_OFFSET (buf), size,
4303 GST_PAD_NAME (stream->pad));
4305 /* update current position in the segment */
4306 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, next_ts);
4308 /* mark discont when pending */
4309 if (G_UNLIKELY (stream->discont)) {
4310 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4311 stream->discont = FALSE;
4313 res = gst_pad_push (stream->pad, buf);
4316 res = gst_avi_demux_combine_flows (avi, stream, res);
4317 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4318 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
4331 GST_DEBUG_OBJECT (avi, "format %s != GST_FORMAT_TIME",
4332 gst_format_get_name (format));
4333 res = GST_FLOW_ERROR;
4339 * Send pending tags.
4342 push_tag_lists (GstAviDemux * avi)
4349 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
4351 for (i = 0; i < avi->num_streams; i++)
4352 if (avi->stream[i].pad && avi->stream[i].taglist) {
4353 GST_DEBUG_OBJECT (avi->stream[i].pad, "Tags: %" GST_PTR_FORMAT,
4354 avi->stream[i].taglist);
4355 gst_element_found_tags_for_pad (GST_ELEMENT (avi), avi->stream[i].pad,
4356 avi->stream[i].taglist);
4357 avi->stream[i].taglist = NULL;
4360 if (avi->globaltags == NULL)
4361 avi->globaltags = gst_tag_list_new ();
4363 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE,
4364 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
4366 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, avi->globaltags);
4367 gst_element_found_tags (GST_ELEMENT (avi), avi->globaltags);
4368 avi->globaltags = NULL;
4369 avi->got_tags = FALSE;
4373 gst_avi_demux_loop (GstPad * pad)
4376 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4378 switch (avi->state) {
4379 case GST_AVI_DEMUX_START:
4380 if (G_UNLIKELY ((res =
4381 gst_avi_demux_stream_init_pull (avi)) != GST_FLOW_OK)) {
4382 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4385 avi->state = GST_AVI_DEMUX_HEADER;
4387 case GST_AVI_DEMUX_HEADER:
4388 if (G_UNLIKELY ((res =
4389 gst_avi_demux_stream_header_pull (avi)) != GST_FLOW_OK)) {
4390 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4393 avi->state = GST_AVI_DEMUX_MOVI;
4395 case GST_AVI_DEMUX_MOVI:
4396 if (G_UNLIKELY (avi->seek_event)) {
4397 gst_avi_demux_push_event (avi, avi->seek_event);
4398 avi->seek_event = NULL;
4400 if (G_UNLIKELY (avi->got_tags)) {
4401 push_tag_lists (avi);
4403 /* process each index entry in turn */
4404 res = gst_avi_demux_stream_data (avi);
4406 /* pause when error */
4407 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4408 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
4413 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
4414 res = GST_FLOW_ERROR;
4418 GST_LOG_OBJECT (avi, "state: %d res:%s", avi->state, gst_flow_get_name (res));
4424 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
4425 avi->segment_running = FALSE;
4426 gst_pad_pause_task (avi->sinkpad);
4428 if (GST_FLOW_IS_FATAL (res) || (res == GST_FLOW_NOT_LINKED)) {
4429 gboolean push_eos = TRUE;
4431 if (res == GST_FLOW_UNEXPECTED) {
4432 /* handle end-of-stream/segment */
4433 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4436 if ((stop = avi->segment.stop) == -1)
4437 stop = avi->segment.duration;
4439 GST_INFO_OBJECT (avi, "sending segment_done");
4441 gst_element_post_message
4443 gst_message_new_segment_done (GST_OBJECT (avi), GST_FORMAT_TIME,
4448 /* for fatal errors we post an error message */
4449 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
4450 (_("Internal data stream error.")),
4451 ("streaming stopped, reason %s", gst_flow_get_name (res)));
4454 GST_INFO_OBJECT (avi, "sending eos");
4455 if (!gst_avi_demux_push_event (avi, gst_event_new_eos ()) &&
4456 (res == GST_FLOW_UNEXPECTED)) {
4457 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
4458 (NULL), ("got eos but no streams (yet)"));
4465 static GstFlowReturn
4466 gst_avi_demux_chain (GstPad * pad, GstBuffer * buf)
4469 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4471 GST_DEBUG ("Store %d bytes in adapter", GST_BUFFER_SIZE (buf));
4472 gst_adapter_push (avi->adapter, buf);
4474 switch (avi->state) {
4475 case GST_AVI_DEMUX_START:
4476 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
4477 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4481 case GST_AVI_DEMUX_HEADER:
4482 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
4483 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4487 case GST_AVI_DEMUX_MOVI:
4488 if (G_UNLIKELY (avi->seek_event)) {
4489 gst_avi_demux_push_event (avi, avi->seek_event);
4490 avi->seek_event = NULL;
4492 if (G_UNLIKELY (avi->got_tags)) {
4493 push_tag_lists (avi);
4495 res = gst_avi_demux_stream_data (avi);
4498 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
4499 ("Illegal internal state"));
4500 res = GST_FLOW_ERROR;
4504 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
4505 gst_flow_get_name (res));
4507 if (G_UNLIKELY (avi->abort_buffering)) {
4508 avi->abort_buffering = FALSE;
4509 res = GST_FLOW_ERROR;
4510 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, NULL, ("unhandled buffer size"));
4517 gst_avi_demux_sink_activate (GstPad * sinkpad)
4519 if (gst_pad_check_pull_range (sinkpad)) {
4520 GST_DEBUG ("going to pull mode");
4521 return gst_pad_activate_pull (sinkpad, TRUE);
4523 GST_DEBUG ("going to push (streaming) mode");
4524 return gst_pad_activate_push (sinkpad, TRUE);
4529 gst_avi_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
4531 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (sinkpad));
4534 avi->segment_running = TRUE;
4535 avi->streaming = FALSE;
4536 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4539 avi->segment_running = FALSE;
4540 return gst_pad_stop_task (sinkpad);
4545 gst_avi_demux_activate_push (GstPad * pad, gboolean active)
4547 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (pad));
4550 GST_DEBUG ("avi: activating push/chain function");
4551 avi->streaming = TRUE;
4553 GST_DEBUG ("avi: deactivating push/chain function");
4559 static GstStateChangeReturn
4560 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
4562 GstStateChangeReturn ret;
4563 GstAviDemux *avi = GST_AVI_DEMUX (element);
4565 switch (transition) {
4566 case GST_STATE_CHANGE_READY_TO_PAUSED:
4567 avi->streaming = FALSE;
4568 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
4574 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4575 if (ret == GST_STATE_CHANGE_FAILURE)
4578 switch (transition) {
4579 case GST_STATE_CHANGE_PAUSED_TO_READY:
4580 gst_avi_demux_reset (avi);