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 "Thijs Vermeir <thijsvermeir@gmail.com>");
142 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
143 GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl;
144 GstCaps *audcaps, *vidcaps, *subcaps;
146 audcaps = gst_riff_create_audio_template_caps ();
147 gst_caps_append (audcaps, gst_caps_new_simple ("audio/x-avi-unknown", NULL));
148 audiosrctempl = gst_pad_template_new ("audio_%02d",
149 GST_PAD_SRC, GST_PAD_SOMETIMES, audcaps);
151 vidcaps = gst_riff_create_video_template_caps ();
152 gst_caps_append (vidcaps, gst_riff_create_iavs_template_caps ());
153 gst_caps_append (vidcaps, gst_caps_new_simple ("video/x-avi-unknown", NULL));
154 videosrctempl = gst_pad_template_new ("video_%02d",
155 GST_PAD_SRC, GST_PAD_SOMETIMES, vidcaps);
157 subcaps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
158 subsrctempl = gst_pad_template_new ("subtitle_%02d",
159 GST_PAD_SRC, GST_PAD_SOMETIMES, subcaps);
160 gst_element_class_add_pad_template (element_class, audiosrctempl);
161 gst_element_class_add_pad_template (element_class, videosrctempl);
162 gst_element_class_add_pad_template (element_class, subsrctempl);
163 gst_element_class_add_pad_template (element_class,
164 gst_static_pad_template_get (&sink_templ));
165 gst_element_class_set_details (element_class, &gst_avi_demux_details);
169 gst_avi_demux_class_init (GstAviDemuxClass * klass)
171 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
172 GObjectClass *gobject_class = (GObjectClass *) klass;
174 GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
175 0, "Demuxer for AVI streams");
177 parent_class = g_type_class_peek_parent (klass);
179 gobject_class->finalize = gst_avi_demux_finalize;
180 gstelement_class->change_state =
181 GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
185 gst_avi_demux_init (GstAviDemux * avi)
187 avi->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
188 gst_pad_set_activate_function (avi->sinkpad,
189 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate));
190 gst_pad_set_activatepull_function (avi->sinkpad,
191 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate_pull));
192 gst_pad_set_activatepush_function (avi->sinkpad,
193 GST_DEBUG_FUNCPTR (gst_avi_demux_activate_push));
194 gst_pad_set_chain_function (avi->sinkpad,
195 GST_DEBUG_FUNCPTR (gst_avi_demux_chain));
196 gst_pad_set_event_function (avi->sinkpad,
197 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_sink_event));
198 gst_element_add_pad (GST_ELEMENT (avi), avi->sinkpad);
200 avi->adapter = gst_adapter_new ();
202 gst_avi_demux_reset (avi);
206 gst_avi_demux_finalize (GObject * object)
208 GstAviDemux *avi = GST_AVI_DEMUX (object);
210 GST_DEBUG ("AVI: finalize");
212 g_object_unref (avi->adapter);
214 G_OBJECT_CLASS (parent_class)->finalize (object);
218 gst_avi_demux_reset (GstAviDemux * avi)
222 GST_DEBUG ("AVI: reset");
224 for (i = 0; i < avi->num_streams; i++) {
225 g_free (avi->stream[i].strh);
226 g_free (avi->stream[i].strf.data);
227 if (avi->stream[i].name)
228 g_free (avi->stream[i].name);
229 if (avi->stream[i].initdata)
230 gst_buffer_unref (avi->stream[i].initdata);
231 if (avi->stream[i].extradata)
232 gst_buffer_unref (avi->stream[i].extradata);
233 if (avi->stream[i].pad) {
234 gst_pad_set_active (avi->stream[i].pad, FALSE);
235 gst_element_remove_pad (GST_ELEMENT (avi), avi->stream[i].pad);
237 if (avi->stream[i].taglist) {
238 gst_tag_list_free (avi->stream[i].taglist);
239 avi->stream[i].taglist = NULL;
242 memset (&avi->stream, 0, sizeof (avi->stream));
244 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
245 avi->num_streams = 0;
246 avi->num_v_streams = 0;
247 avi->num_a_streams = 0;
248 avi->num_t_streams = 0;
250 avi->state = GST_AVI_DEMUX_START;
253 g_free (avi->index_entries);
254 avi->index_entries = NULL;
256 avi->index_offset = 0;
257 avi->current_entry = 0;
261 if (avi->seek_event) {
262 gst_event_unref (avi->seek_event);
263 avi->seek_event = NULL;
267 gst_tag_list_free (avi->globaltags);
268 avi->globaltags = NULL;
270 avi->got_tags = TRUE; /* we always want to push global tags */
271 avi->have_eos = FALSE;
273 gst_adapter_clear (avi->adapter);
275 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
279 static gst_avi_index_entry *
280 gst_avi_demux_index_last (GstAviDemux * avi, gint stream_nr)
283 gst_avi_index_entry *result = NULL;
285 for (i = avi->index_size - 1; i >= 0; i--) {
286 if (avi->index_entries[i].stream_nr == stream_nr) {
287 result = &avi->index_entries[i];
294 static gst_avi_index_entry *
295 gst_avi_demux_index_next (GstAviDemux * avi, gint stream_nr, gint last,
299 gst_avi_index_entry *result = NULL, *entry;
301 for (i = last + 1; i < avi->index_size; i++) {
302 entry = &avi->index_entries[i];
304 if (entry->stream_nr != stream_nr)
307 if ((entry->flags & flags) == flags) {
315 static gst_avi_index_entry *
316 gst_avi_demux_index_prev (GstAviDemux * avi, gint stream_nr, gint last,
320 gst_avi_index_entry *result = NULL, *entry;
322 for (i = last - 1; i >= 0; i--) {
323 entry = &avi->index_entries[i];
325 if (entry->stream_nr != stream_nr)
328 if ((entry->flags & flags) == flags) {
337 gst_avi_demux_index_entry_search (gst_avi_index_entry * entry, guint64 * time)
339 if (entry->ts < *time)
341 else if (entry->ts > *time)
347 * gst_avi_index_entry:
349 * @stream_nr: stream number
350 * @time: seek time position
352 * Finds the index entry which time is less or equal than the requested time.
354 * Returns: the found index entry or %NULL
356 static gst_avi_index_entry *
357 gst_avi_demux_index_entry_for_time (GstAviDemux * avi,
358 gint stream_nr, guint64 time)
360 gst_avi_index_entry *entry = NULL;
363 GST_LOG_OBJECT (avi, "stream_nr:%d , time:%" GST_TIME_FORMAT,
364 stream_nr, GST_TIME_ARGS (time));
366 entry = gst_util_array_binary_search (avi->index_entries,
368 sizeof (gst_avi_index_entry),
369 (GCompareDataFunc) gst_avi_demux_index_entry_search,
370 GST_SEARCH_MODE_BEFORE, &time, NULL);
372 n = entry - avi->index_entries;
374 entry = &avi->index_entries[0];
376 while (entry->stream_nr != stream_nr && n < avi->index_size - 1) {
378 entry = &avi->index_entries[n];
380 } else if (entry->stream_nr != stream_nr) {
381 while (entry->stream_nr != stream_nr && n > 0) {
383 entry = &avi->index_entries[n];
388 "best at entry %u / ts:%" GST_TIME_FORMAT " / dur:%" GST_TIME_FORMAT
389 " flags:%02x", n, GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (entry->dur),
395 /* GstElement methods */
398 static const GstFormat *
399 gst_avi_demux_get_src_formats (GstPad * pad)
401 avi_stream_context *stream = gst_pad_get_element_private (pad);
403 static const GstFormat src_a_formats[] = {
409 static const GstFormat src_v_formats[] = {
415 return (stream->strh->type == GST_RIFF_FCC_auds ?
416 src_a_formats : src_v_formats);
420 /* assumes stream->strf.auds->av_bps != 0 */
421 static inline GstClockTime
422 avi_stream_convert_bytes_to_time_unchecked (avi_stream_context * stream,
425 return gst_util_uint64_scale (bytes, GST_SECOND, stream->strf.auds->av_bps);
428 /* assumes stream->strh->rate != 0 */
429 static inline GstClockTime
430 avi_stream_convert_frames_to_time_unchecked (avi_stream_context * stream,
433 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
438 gst_avi_demux_src_convert (GstPad * pad,
439 GstFormat src_format,
440 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
442 avi_stream_context *stream = gst_pad_get_element_private (pad);
446 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
447 ", dest_format:%s", gst_format_get_name (src_format), src_value,
448 gst_format_get_name (*dest_format));
450 if (G_UNLIKELY (src_format == *dest_format)) {
451 *dest_value = src_value;
454 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
458 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
459 (src_format == GST_FORMAT_BYTES
460 || *dest_format == GST_FORMAT_BYTES))) {
465 switch (src_format) {
466 case GST_FORMAT_TIME:
467 switch (*dest_format) {
468 case GST_FORMAT_BYTES:
469 *dest_value = gst_util_uint64_scale (src_value,
470 (guint64) stream->strf.auds->av_bps, GST_SECOND);
472 case GST_FORMAT_DEFAULT:
476 *dest_value = gst_util_uint64_scale (src_value, stream->strh->rate,
477 stream->strh->scale * GST_SECOND);
479 /* Attempt to round to nearest integer: if the difference is more
480 * than 0.5 (less than -0.5), it means that gst_util_uint64_scale()
481 * just truncated an integer, while it had to be rounded
483 error = *dest_value * GST_SECOND -
484 src_value * stream->strh->rate / stream->strh->scale;
494 case GST_FORMAT_BYTES:
495 switch (*dest_format) {
496 case GST_FORMAT_TIME:
497 if (stream->strf.auds->av_bps != 0) {
498 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
508 case GST_FORMAT_DEFAULT:
509 switch (*dest_format) {
510 case GST_FORMAT_TIME:
512 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
525 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
526 gst_format_get_name (*dest_format), *dest_value);
530 static const GstQueryType *
531 gst_avi_demux_get_src_query_types (GstPad * pad)
533 static const GstQueryType src_types[] = {
545 gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query)
548 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
550 avi_stream_context *stream = gst_pad_get_element_private (pad);
552 if (!stream->strh || !stream->strf.data)
553 return gst_pad_query_default (pad, query);
555 switch (GST_QUERY_TYPE (query)) {
556 case GST_QUERY_POSITION:{
559 GST_DEBUG ("pos query for stream %d: frames %d, bytes %" G_GUINT64_FORMAT,
560 stream->num, stream->current_frame, stream->current_byte);
562 if (stream->strh->type == GST_RIFF_FCC_auds) {
563 if (stream->is_vbr) {
565 pos = gst_util_uint64_scale ((gint64) stream->current_frame *
566 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
567 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
568 GST_TIME_FORMAT, stream->current_frame, GST_TIME_ARGS (pos));
569 } else if (stream->strf.auds->av_bps != 0) {
571 pos = gst_util_uint64_scale (stream->current_byte, GST_SECOND,
572 (guint64) stream->strf.auds->av_bps);
573 GST_DEBUG_OBJECT (avi,
574 "CBR convert bytes %" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT,
575 stream->current_byte, GST_TIME_ARGS (pos));
576 } else if (stream->total_frames != 0 && stream->total_bytes != 0) {
577 /* calculate timestamps based on percentage of length */
578 guint64 xlen = avi->avih->us_frame *
579 avi->avih->tot_frames * GST_USECOND;
581 if (stream->is_vbr) {
582 pos = gst_util_uint64_scale (xlen, stream->current_frame,
583 stream->total_frames);
584 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
585 GST_TIME_FORMAT, stream->current_frame, GST_TIME_ARGS (pos));
587 pos = gst_util_uint64_scale (xlen, stream->current_byte,
588 stream->total_bytes);
589 GST_DEBUG_OBJECT (avi, "CBR perc convert bytes %" G_GUINT64_FORMAT
590 ", time %" GST_TIME_FORMAT, stream->current_byte,
591 GST_TIME_ARGS (pos));
598 if (stream->strh->rate != 0) {
599 pos = gst_util_uint64_scale ((guint64) stream->current_frame *
600 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
602 pos = stream->current_frame * avi->avih->us_frame * GST_USECOND;
606 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
607 gst_query_set_position (query, GST_FORMAT_TIME, pos);
609 GST_WARNING ("pos query failed");
612 case GST_QUERY_DURATION:
616 if (stream->strh->type != GST_RIFF_FCC_auds &&
617 stream->strh->type != GST_RIFF_FCC_vids) {
622 gst_query_parse_duration (query, &fmt, NULL);
625 case GST_FORMAT_TIME:
626 gst_query_set_duration (query, fmt, stream->duration);
628 case GST_FORMAT_DEFAULT:
631 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
632 stream->total_frames);
634 if (stream->total_frames >= 0)
635 gst_query_set_duration (query, fmt, stream->total_frames);
636 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
637 stream->duration, &fmt, &dur))
638 gst_query_set_duration (query, fmt, dur);
647 case GST_QUERY_SEEKING:{
650 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
651 if (fmt == GST_FORMAT_TIME) {
652 gboolean seekable = TRUE;
654 if (avi->streaming) {
657 if (avi->index_entries == NULL) {
659 /* FIXME: when building index_entried, count keyframes
660 if (!(avi->key_frame_ct > 1))
666 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
667 0, stream->duration);
672 case GST_QUERY_CONVERT:{
673 GstFormat src_fmt, dest_fmt;
674 gint64 src_val, dest_val;
676 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
677 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
679 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
681 res = gst_pad_query_default (pad, query);
685 res = gst_pad_query_default (pad, query);
689 gst_object_unref (avi);
694 static const GstEventMask *
695 gst_avi_demux_get_event_mask (GstPad * pad)
697 static const GstEventMask masks[] = {
698 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
707 gst_avi_demux_handle_sink_event (GstPad * pad, GstEvent * event)
710 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
712 GST_DEBUG_OBJECT (avi,
713 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
715 switch (GST_EVENT_TYPE (event)) {
716 case GST_EVENT_NEWSEGMENT:
717 /* Drop NEWSEGMENT events, new ones are generated later */
718 gst_event_unref (event);
722 if (avi->state != GST_AVI_DEMUX_MOVI) {
723 gst_event_unref (event);
724 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
725 (NULL), ("got eos and didn't receive a complete header object"));
726 } else if (!gst_avi_demux_push_event (avi, event)) {
727 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
728 (NULL), ("got eos but no streams (yet)"));
733 res = gst_pad_event_default (pad, event);
737 gst_object_unref (avi);
743 gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event)
746 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
748 GST_DEBUG_OBJECT (avi,
749 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
751 switch (GST_EVENT_TYPE (event)) {
753 /* handle seeking only in pull mode */
754 if (!avi->streaming) {
755 res = gst_avi_demux_handle_seek (avi, pad, event);
756 gst_event_unref (event);
758 res = gst_pad_event_default (pad, event);
762 case GST_EVENT_NAVIGATION:
764 gst_event_unref (event);
767 res = gst_pad_event_default (pad, event);
771 gst_object_unref (avi);
776 /* streaming helper (push) */
779 * gst_avi_demux_peek_chunk_info:
781 * @tag: holder for tag
782 * @size: holder for tag size
784 * Peek next chunk info (tag and size)
786 * Returns: TRUE when one chunk info has been got
789 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
791 const guint8 *data = NULL;
793 if (gst_adapter_available (avi->adapter) < 8) {
797 data = gst_adapter_peek (avi->adapter, 8);
798 *tag = GST_READ_UINT32_LE (data);
799 *size = GST_READ_UINT32_LE (data + 4);
805 * gst_avi_demux_peek_chunk:
807 * @tag: holder for tag
808 * @size: holder for tag size
810 * Peek enough data for one full chunk
812 * Returns: %TRUE when one chunk has been got
815 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
817 guint32 peek_size = 0;
820 if (!gst_avi_demux_peek_chunk_info (avi, tag, size)) {
824 /* size 0 -> empty data buffer would surprise most callers,
825 * large size -> do not bother trying to squeeze that into adapter,
826 * so we throw poor man's exception, which can be caught if caller really
827 * wants to handle 0 size chunk */
828 if (!(*size) || (*size) >= (1 << 30)) {
829 GST_INFO ("Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT,
830 *size, GST_FOURCC_ARGS (*tag));
831 /* chain should give up */
832 avi->abort_buffering = TRUE;
835 peek_size = (*size + 1) & ~1;
836 available = gst_adapter_available (avi->adapter);
838 GST_DEBUG ("Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
839 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
841 if (available >= (8 + peek_size)) {
851 * gst_avi_demux_parse_file_header:
852 * @element: caller element (used for errors/debug).
853 * @buf: input data to be used for parsing.
855 * "Open" a RIFF/AVI file. The buffer should be at least 12
856 * bytes long. Takes ownership of @buf.
858 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
859 * Throws an error, caller should error out (fatal).
862 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
866 /* riff_parse posts an error */
867 if (!gst_riff_parse_file_header (element, buf, &doctype))
870 if (doctype != GST_RIFF_RIFF_AVI)
878 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
879 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
880 GST_FOURCC_ARGS (doctype)));
886 * Read AVI file tag when streaming
889 gst_avi_demux_stream_init_push (GstAviDemux * avi)
891 if (gst_adapter_available (avi->adapter) >= 12) {
894 tmp = gst_adapter_take_buffer (avi->adapter, 12);
896 GST_DEBUG ("Parsing avi header");
897 if (!gst_avi_demux_parse_file_header (GST_ELEMENT (avi), tmp)) {
898 return GST_FLOW_ERROR;
900 GST_DEBUG ("header ok");
903 avi->state = GST_AVI_DEMUX_HEADER;
912 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
915 GstBuffer *buf = NULL;
917 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
918 if (res != GST_FLOW_OK)
920 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
930 GST_DEBUG_OBJECT (avi, "error parsing file header");
931 return GST_FLOW_ERROR;
935 /* AVI header handling */
938 * gst_avi_demux_parse_avih:
939 * @element: caller element (used for errors/debug).
940 * @buf: input data to be used for parsing.
941 * @avih: pointer to structure (filled in by function) containing
942 * stream information (such as flags, number of streams, etc.).
944 * Read 'avih' header. Discards buffer after use.
946 * Returns: TRUE on success, FALSE otherwise. Throws an error if
947 * the header is invalid. The caller should error out
951 gst_avi_demux_parse_avih (GstElement * element,
952 GstBuffer * buf, gst_riff_avih ** _avih)
959 if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_avih))
962 avih = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
964 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
965 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
966 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
967 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
968 avih->flags = GUINT32_FROM_LE (avih->flags);
969 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
970 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
971 avih->streams = GUINT32_FROM_LE (avih->streams);
972 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
973 avih->width = GUINT32_FROM_LE (avih->width);
974 avih->height = GUINT32_FROM_LE (avih->height);
975 avih->scale = GUINT32_FROM_LE (avih->scale);
976 avih->rate = GUINT32_FROM_LE (avih->rate);
977 avih->start = GUINT32_FROM_LE (avih->start);
978 avih->length = GUINT32_FROM_LE (avih->length);
982 GST_INFO_OBJECT (element, "avih tag found:");
983 GST_INFO_OBJECT (element, " us_frame %u", avih->us_frame);
984 GST_INFO_OBJECT (element, " max_bps %u", avih->max_bps);
985 GST_INFO_OBJECT (element, " pad_gran %u", avih->pad_gran);
986 GST_INFO_OBJECT (element, " flags 0x%08x", avih->flags);
987 GST_INFO_OBJECT (element, " tot_frames %u", avih->tot_frames);
988 GST_INFO_OBJECT (element, " init_frames %u", avih->init_frames);
989 GST_INFO_OBJECT (element, " streams %u", avih->streams);
990 GST_INFO_OBJECT (element, " bufsize %u", avih->bufsize);
991 GST_INFO_OBJECT (element, " width %u", avih->width);
992 GST_INFO_OBJECT (element, " height %u", avih->height);
993 GST_INFO_OBJECT (element, " scale %u", avih->scale);
994 GST_INFO_OBJECT (element, " rate %u", avih->rate);
995 GST_INFO_OBJECT (element, " start %u", avih->start);
996 GST_INFO_OBJECT (element, " length %u", avih->length);
999 gst_buffer_unref (buf);
1006 GST_ELEMENT_ERROR (element, STREAM, DEMUX, (NULL), ("No buffer"));
1011 GST_ELEMENT_ERROR (element, STREAM, DEMUX, (NULL),
1012 ("Too small avih (%d available, %d needed)",
1013 GST_BUFFER_SIZE (buf), (int) sizeof (gst_riff_avih)));
1014 gst_buffer_unref (buf);
1020 * gst_avi_demux_parse_superindex:
1021 * @avi: caller element (used for debugging/errors).
1022 * @buf: input data to use for parsing.
1023 * @locations: locations in the file (byte-offsets) that contain
1024 * the actual indexes (see get_avi_demux_parse_subindex()).
1025 * The array ends with GST_BUFFER_OFFSET_NONE.
1027 * Reads superindex (openDML-2 spec stuff) from the provided data.
1029 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
1030 * on error, but they are not fatal.
1033 gst_avi_demux_parse_superindex (GstAviDemux * avi,
1034 GstBuffer * buf, guint64 ** _indexes)
1044 size = buf ? GST_BUFFER_SIZE (buf) : 0;
1048 data = GST_BUFFER_DATA (buf);
1050 /* check type of index. The opendml2 specs state that
1051 * there should be 4 dwords per array entry. Type can be
1052 * either frame or field (and we don't care). */
1053 if (GST_READ_UINT16_LE (data) != 4 ||
1054 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
1055 GST_WARNING_OBJECT (avi,
1056 "Superindex for stream has unexpected "
1057 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1058 GST_READ_UINT16_LE (data), data[2], data[3]);
1059 bpe = GST_READ_UINT16_LE (data) * 4;
1061 num = GST_READ_UINT32_LE (&data[4]);
1063 indexes = g_new (guint64, num + 1);
1064 for (i = 0; i < num; i++) {
1065 if (size < 24 + bpe * (i + 1))
1067 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
1069 indexes[i] = GST_BUFFER_OFFSET_NONE;
1070 *_indexes = indexes;
1072 gst_buffer_unref (buf);
1079 GST_ERROR_OBJECT (avi,
1080 "Not enough data to parse superindex (%d available, 24 needed)", size);
1082 gst_buffer_unref (buf);
1088 * gst_avi_demux_parse_subindex:
1090 * @buf: input data to use for parsing.
1091 * @stream: stream context.
1092 * @entries_list: a list (returned by the function) containing all the
1093 * indexes parsed in this specific subindex. The first
1094 * entry is also a pointer to allocated memory that needs
1095 * to be free´ed. May be NULL if no supported indexes were
1098 * Reads superindex (openDML-2 spec stuff) from the provided data.
1099 * The buffer will be discarded after use.
1100 * The buffer should contain a GST_RIFF_TAG_ix?? chunk.
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 #ifndef GST_DISABLE_GST_DEBUG
1118 gulong _nr_keyframes = 0;
1121 *_entries_list = NULL;
1123 size = buf ? GST_BUFFER_SIZE (buf) : 0;
1129 /* We don't support index-data yet */
1131 goto not_implemented;
1133 /* check type of index. The opendml2 specs state that
1134 * there should be 4 dwords per array entry. Type can be
1135 * either frame or field (and we don't care). */
1136 bpe = (data[2] & 0x01) ? 12 : 8;
1137 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1138 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1139 GST_WARNING_OBJECT (avi,
1140 "Superindex for stream %d has unexpected "
1141 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1142 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1143 bpe = GST_READ_UINT16_LE (data) * 4;
1145 num = GST_READ_UINT32_LE (&data[4]);
1146 baseoff = GST_READ_UINT64_LE (&data[12]);
1148 /* If there's nothing, just return ! */
1152 if (!(entries = g_try_new (gst_avi_index_entry, num)))
1155 GST_INFO_OBJECT (avi, "Parsing subindex, nr_entries = %6d", num);
1157 for (i = 0; i < num; i++) {
1160 entry = &entries[i];
1162 if (size < 24 + bpe * (i + 1))
1166 entry->offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1167 entry->size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1169 (entry->size & 0x80000000) ? 0 : GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
1170 entry->size &= ~0x80000000;
1171 entry->index_nr = i;
1172 entry->stream_nr = stream->num;
1174 if (stream->strh->type == GST_RIFF_FCC_auds) {
1175 /* all audio frames are keyframes */
1176 entry->flags |= GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
1178 #ifndef GST_DISABLE_GST_DEBUG
1179 if (entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)
1183 /* stream duration unknown, now we can calculate it */
1184 if (stream->idx_duration == -1)
1185 stream->idx_duration = 0;
1188 entry->ts = stream->idx_duration;
1189 if (stream->is_vbr) {
1190 /* VBR stream next timestamp */
1191 if (stream->strh->type == GST_RIFF_FCC_auds) {
1192 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
1193 stream->total_blocks + 1);
1195 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
1196 stream->total_frames + 1);
1199 /* CBR get next timestamp */
1200 next_ts = avi_stream_convert_bytes_to_time_unchecked (stream,
1201 stream->total_bytes + entry->size);
1203 /* duration is next - current */
1204 entry->dur = next_ts - entry->ts;
1206 /* stream position */
1207 entry->bytes_before = stream->total_bytes;
1208 entry->frames_before = stream->total_frames;
1210 stream->total_bytes += entry->size;
1211 stream->total_frames++;
1212 if (stream->strh->type == GST_RIFF_FCC_auds) {
1213 if (stream->strf.auds->blockalign > 0)
1214 stream->total_blocks +=
1215 (entry->size + stream->strf.auds->blockalign -
1216 1) / stream->strf.auds->blockalign;
1218 stream->total_blocks++;
1220 stream->idx_duration = next_ts;
1222 entries_list = g_list_prepend (entries_list, entry);
1225 GST_INFO_OBJECT (avi, "Parsed index, %6lu/%6lu entries, %5lu keyframes, "
1226 "entry size = %2u, total size = %10d", i, num, _nr_keyframes,
1227 (gint) sizeof (gst_avi_index_entry),
1228 (gint) (i * sizeof (gst_avi_index_entry)));
1230 gst_buffer_unref (buf);
1233 *_entries_list = g_list_reverse (entries_list);
1243 GST_ERROR_OBJECT (avi,
1244 "Not enough data to parse subindex (%d available, 24 needed)", size);
1246 gst_buffer_unref (buf);
1247 return TRUE; /* continue */
1251 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1252 ("Subindex-is-data is not implemented"));
1253 gst_buffer_unref (buf);
1258 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1259 ("Cannot allocate memory for %u*%u=%u bytes",
1260 (guint) sizeof (gst_avi_index_entry), num,
1261 (guint) sizeof (gst_avi_index_entry) * num));
1262 gst_buffer_unref (buf);
1269 * Read AVI index when streaming
1272 gst_avi_demux_read_subindexes_push (GstAviDemux * avi,
1273 GList ** index, GList ** alloc_list)
1276 guint32 tag = 0, size;
1277 GstBuffer *buf = NULL;
1280 GST_DEBUG_OBJECT (avi, "gst_avi_demux_read_subindexes_push for %d streams",
1283 for (n = 0; n < avi->num_streams; n++) {
1284 avi_stream_context *stream = &avi->stream[n];
1286 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1287 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1289 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1290 '0' + stream->num % 10)) &&
1291 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1292 '0' + stream->num % 10, 'i', 'x'))) {
1293 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1294 GST_FOURCC_ARGS (tag));
1298 avi->offset += 8 + ((size + 1) & ~1);
1300 buf = gst_buffer_new ();
1301 GST_BUFFER_DATA (buf) = gst_adapter_take (avi->adapter, size);
1302 GST_BUFFER_SIZE (buf) = size;
1304 if (!gst_avi_demux_parse_subindex (avi, buf, stream, &list))
1307 GST_DEBUG_OBJECT (avi, " adding %d entries", g_list_length (list));
1308 *alloc_list = g_list_append (*alloc_list, list->data);
1309 *index = g_list_concat (*index, list);
1313 g_free (stream->indexes);
1314 stream->indexes = NULL;
1316 GST_DEBUG_OBJECT (avi, "index %s", ((*index) ? "!= 0" : "== 0"));
1324 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi,
1325 GList ** index, GList ** alloc_list)
1332 GST_DEBUG_OBJECT (avi, "gst_avi_demux_read_subindexes_pull for %d streams",
1335 for (n = 0; n < avi->num_streams; n++) {
1336 avi_stream_context *stream = &avi->stream[n];
1338 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1339 if (gst_riff_read_chunk (GST_ELEMENT (avi), avi->sinkpad,
1340 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1342 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1343 '0' + stream->num % 10)) &&
1344 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1345 '0' + stream->num % 10, 'i', 'x'))) {
1346 /* Some ODML files (created by god knows what muxer) have a ##ix format
1347 * instead of the 'official' ix##. They are still valid though. */
1348 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1349 GST_FOURCC_ARGS (tag));
1350 gst_buffer_unref (buf);
1354 if (!gst_avi_demux_parse_subindex (avi, buf, stream, &list))
1357 GST_DEBUG_OBJECT (avi, " adding %5d entries, total %2d %5d",
1358 g_list_length (list), g_list_length (*alloc_list),
1359 g_list_length (*index));
1360 *alloc_list = g_list_append (*alloc_list, list->data);
1361 *index = g_list_concat (*index, list);
1365 g_free (stream->indexes);
1366 stream->indexes = NULL;
1368 GST_DEBUG_OBJECT (avi, "index %s", ((*index) ? "!= 0" : "== 0"));
1372 * gst_avi_demux_riff_parse_vprp:
1373 * @element: caller element (used for debugging/error).
1374 * @buf: input data to be used for parsing, stripped from header.
1375 * @vprp: a pointer (returned by this function) to a filled-in vprp
1376 * structure. Caller should free it.
1378 * Parses a video stream´s vprp. This function takes ownership of @buf.
1380 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1381 * should be skipped on error, but it is not fatal.
1384 gst_avi_demux_riff_parse_vprp (GstElement * element,
1385 GstBuffer * buf, gst_riff_vprp ** _vprp)
1387 gst_riff_vprp *vprp;
1390 g_return_val_if_fail (buf != NULL, FALSE);
1391 g_return_val_if_fail (_vprp != NULL, FALSE);
1393 if (GST_BUFFER_SIZE (buf) < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1396 vprp = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1398 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1399 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1400 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1401 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1402 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1403 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1404 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1405 vprp->width = GUINT32_FROM_LE (vprp->width);
1406 vprp->height = GUINT32_FROM_LE (vprp->height);
1407 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1411 /* calculate fields based on size */
1412 k = (GST_BUFFER_SIZE (buf) - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) /
1414 if (vprp->fields > k) {
1415 GST_WARNING_OBJECT (element,
1416 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1419 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1420 GST_WARNING_OBJECT (element,
1421 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1422 GST_RIFF_VPRP_VIDEO_FIELDS);
1423 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1425 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1426 for (k = 0; k < vprp->fields; k++) {
1427 gst_riff_vprp_video_field_desc *fd;
1429 fd = &vprp->field_info[k];
1430 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1431 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1432 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1433 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1434 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1435 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1436 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1437 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1442 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1443 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1444 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1445 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1446 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1447 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1448 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1449 vprp->aspect & 0xffff);
1450 GST_INFO_OBJECT (element, " width %d", vprp->width);
1451 GST_INFO_OBJECT (element, " height %d", vprp->height);
1452 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1453 for (k = 0; k < vprp->fields; k++) {
1454 gst_riff_vprp_video_field_desc *fd;
1456 fd = &(vprp->field_info[k]);
1457 GST_INFO_OBJECT (element, " field %u description:", k);
1458 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1459 fd->compressed_bm_height);
1460 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1461 fd->compressed_bm_width);
1462 GST_INFO_OBJECT (element, " valid_bm_height %d",
1463 fd->valid_bm_height);
1464 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1465 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1466 fd->valid_bm_x_offset);
1467 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1468 fd->valid_bm_y_offset);
1469 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1470 fd->video_x_t_offset);
1471 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1474 gst_buffer_unref (buf);
1483 GST_ERROR_OBJECT (element,
1484 "Too small vprp (%d available, at least %d needed)",
1485 GST_BUFFER_SIZE (buf),
1486 (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1487 gst_buffer_unref (buf);
1493 * gst_avi_demux_parse_stream:
1494 * @avi: calling element (used for debugging/errors).
1495 * @buf: input buffer used to parse the stream.
1497 * Parses all subchunks in a strl chunk (which defines a single
1498 * stream). Discards the buffer after use. This function will
1499 * increment the stream counter internally.
1501 * Returns: whether the stream was identified successfully.
1502 * Errors are not fatal. It does indicate the stream
1506 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
1508 avi_stream_context *stream;
1509 GstElementClass *klass;
1510 GstPadTemplate *templ;
1511 GstBuffer *sub = NULL;
1514 gchar *codec_name = NULL, *padname = NULL;
1515 const gchar *tag_name;
1516 GstCaps *caps = NULL;
1518 GstElement *element;
1519 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
1520 gst_riff_vprp *vprp = NULL;
1522 element = GST_ELEMENT_CAST (avi);
1524 GST_DEBUG_OBJECT (avi, "Parsing stream");
1526 if (avi->num_streams >= GST_AVI_DEMUX_MAX_STREAMS) {
1527 GST_WARNING_OBJECT (avi,
1528 "maximum no of streams (%d) exceeded, ignoring stream",
1529 GST_AVI_DEMUX_MAX_STREAMS);
1530 gst_buffer_unref (buf);
1531 /* not a fatal error, let's say */
1535 stream = &avi->stream[avi->num_streams];
1537 /* initial settings */
1538 stream->idx_duration = GST_CLOCK_TIME_NONE;
1539 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1540 stream->duration = GST_CLOCK_TIME_NONE;
1542 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
1543 /* sub can be NULL if the chunk is empty */
1545 GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
1546 GST_FOURCC_ARGS (tag));
1550 case GST_RIFF_TAG_strh:
1552 gst_riff_strh *strh;
1555 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
1558 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
1559 /* ownership given away */
1561 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
1565 strh = stream->strh;
1566 /* sanity check; stream header frame rate matches global header
1568 if (stream->strh->type == GST_RIFF_FCC_vids) {
1570 GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
1572 s_dur = gst_util_uint64_scale (GST_SECOND, strh->scale, strh->rate);
1573 GST_DEBUG_OBJECT (avi, "verifying stream framerate %d/%d, "
1574 "frame duration = %d ms", strh->rate, strh->scale,
1575 s_dur / GST_MSECOND);
1576 if (h_dur > (10 * GST_MSECOND) && (s_dur > 10 * h_dur)) {
1577 strh->rate = GST_SECOND / GST_USECOND;
1578 strh->scale = h_dur / GST_USECOND;
1579 GST_DEBUG_OBJECT (avi, "correcting stream framerate to %d/%d",
1580 strh->rate, strh->scale);
1583 /* determine duration as indicated by header */
1584 stream->hdr_duration = gst_util_uint64_scale ((guint64) strh->length *
1585 strh->scale, GST_SECOND, (guint64) strh->rate);
1586 GST_INFO ("Stream duration according to header: %" GST_TIME_FORMAT,
1587 GST_TIME_ARGS (stream->hdr_duration));
1588 if (stream->hdr_duration == 0)
1589 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1594 case GST_RIFF_TAG_strf:
1596 gboolean res = FALSE;
1599 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
1603 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
1606 switch (stream->strh->type) {
1607 case GST_RIFF_FCC_vids:
1608 stream->is_vbr = TRUE;
1609 res = gst_riff_parse_strf_vids (element, sub,
1610 &stream->strf.vids, &stream->extradata);
1612 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
1614 case GST_RIFF_FCC_auds:
1615 stream->is_vbr = (stream->strh->samplesize == 0)
1616 && stream->strh->scale > 1;
1618 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
1619 &stream->extradata);
1621 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
1622 stream->is_vbr, res);
1624 case GST_RIFF_FCC_iavs:
1625 stream->is_vbr = TRUE;
1626 res = gst_riff_parse_strf_iavs (element, sub,
1627 &stream->strf.iavs, &stream->extradata);
1629 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
1631 case GST_RIFF_FCC_txts:
1632 /* nothing to parse here */
1633 stream->is_vbr = (stream->strh->samplesize == 0)
1634 && (stream->strh->scale > 1);
1638 GST_ERROR_OBJECT (avi,
1639 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
1640 GST_FOURCC_ARGS (stream->strh->type));
1644 gst_buffer_unref (sub);
1652 case GST_RIFF_TAG_vprp:
1655 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
1659 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
1663 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
1667 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
1668 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
1669 /* not considered fatal */
1677 case GST_RIFF_TAG_strd:
1678 if (stream->initdata)
1679 gst_buffer_unref (stream->initdata);
1680 stream->initdata = sub;
1683 case GST_RIFF_TAG_strn:
1684 g_free (stream->name);
1687 g_strndup ((gchar *) GST_BUFFER_DATA (sub),
1688 (gsize) GST_BUFFER_SIZE (sub));
1689 gst_buffer_unref (sub);
1692 stream->name = g_strdup ("");
1694 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
1697 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
1698 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
1699 '0' + avi->num_streams % 10)) {
1700 g_free (stream->indexes);
1701 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
1702 stream->superindex = TRUE;
1706 GST_WARNING_OBJECT (avi,
1707 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
1708 GST_FOURCC_ARGS (tag));
1710 case GST_RIFF_TAG_JUNK:
1714 gst_buffer_unref (sub);
1720 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
1725 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
1729 /* get class to figure out the template */
1730 klass = GST_ELEMENT_GET_CLASS (avi);
1732 /* we now have all info, let´s set up a pad and a caps and be done */
1733 /* create stream name + pad */
1734 switch (stream->strh->type) {
1735 case GST_RIFF_FCC_vids:{
1738 fourcc = (stream->strf.vids->compression) ?
1739 stream->strf.vids->compression : stream->strh->fcc_handler;
1740 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
1741 templ = gst_element_class_get_pad_template (klass, "video_%02d");
1742 caps = gst_riff_create_video_caps (fourcc, stream->strh,
1743 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
1745 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
1746 GST_TYPE_FOURCC, fourcc, NULL);
1747 } else if (got_vprp && vprp) {
1748 guint32 aspect_n, aspect_d;
1751 aspect_n = vprp->aspect >> 16;
1752 aspect_d = vprp->aspect & 0xffff;
1753 /* calculate the pixel aspect ratio using w/h and aspect ratio */
1754 n = aspect_n * stream->strf.vids->height;
1755 d = aspect_d * stream->strf.vids->width;
1757 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1759 /* very local, not needed elsewhere */
1763 tag_name = GST_TAG_VIDEO_CODEC;
1764 avi->num_v_streams++;
1767 case GST_RIFF_FCC_auds:{
1768 padname = g_strdup_printf ("audio_%02d", avi->num_a_streams);
1769 templ = gst_element_class_get_pad_template (klass, "audio_%02d");
1770 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
1771 stream->strh, stream->strf.auds, stream->extradata,
1772 stream->initdata, &codec_name);
1774 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
1775 G_TYPE_INT, stream->strf.auds->format, NULL);
1777 tag_name = GST_TAG_AUDIO_CODEC;
1778 avi->num_a_streams++;
1781 case GST_RIFF_FCC_iavs:{
1782 guint32 fourcc = stream->strh->fcc_handler;
1784 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
1785 templ = gst_element_class_get_pad_template (klass, "video_%02d");
1786 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
1787 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
1789 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
1790 GST_TYPE_FOURCC, fourcc, NULL);
1792 tag_name = GST_TAG_VIDEO_CODEC;
1793 avi->num_v_streams++;
1796 case GST_RIFF_FCC_txts:{
1797 padname = g_strdup_printf ("subtitle_%02d", avi->num_t_streams);
1798 templ = gst_element_class_get_pad_template (klass, "subtitle_%02d");
1799 caps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
1801 avi->num_t_streams++;
1805 g_assert_not_reached ();
1808 /* no caps means no stream */
1810 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
1814 GST_DEBUG_OBJECT (element, "codec-name=%s",
1815 (codec_name ? codec_name : "NULL"));
1816 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
1818 /* set proper settings and add it */
1820 gst_object_unref (stream->pad);
1821 pad = stream->pad = gst_pad_new_from_template (templ, padname);
1822 stream->last_flow = GST_FLOW_OK;
1823 stream->discont = TRUE;
1826 gst_pad_use_fixed_caps (pad);
1828 gst_pad_set_formats_function (pad,
1829 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
1830 gst_pad_set_event_mask_function (pad,
1831 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
1833 gst_pad_set_event_function (pad,
1834 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
1835 gst_pad_set_query_type_function (pad,
1836 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_query_types));
1837 gst_pad_set_query_function (pad,
1838 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
1840 gst_pad_set_convert_function (pad,
1841 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
1844 stream->num = avi->num_streams;
1845 stream->total_bytes = 0;
1846 stream->total_frames = 0;
1847 stream->total_blocks = 0;
1848 stream->current_frame = 0;
1849 stream->current_byte = 0;
1850 gst_pad_set_element_private (pad, stream);
1852 gst_pad_set_caps (pad, caps);
1853 gst_pad_set_active (pad, TRUE);
1854 gst_element_add_pad (GST_ELEMENT (avi), pad);
1855 GST_LOG_OBJECT (element, "Added pad %s with caps %" GST_PTR_FORMAT,
1856 GST_PAD_NAME (pad), caps);
1857 gst_caps_unref (caps);
1861 if (!stream->taglist)
1862 stream->taglist = gst_tag_list_new ();
1864 avi->got_tags = TRUE;
1866 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
1868 g_free (codec_name);
1871 gst_buffer_unref (buf);
1878 /* unref any mem that may be in use */
1880 gst_buffer_unref (buf);
1882 gst_buffer_unref (sub);
1884 g_free (codec_name);
1885 g_free (stream->strh);
1886 g_free (stream->strf.data);
1887 g_free (stream->name);
1888 g_free (stream->indexes);
1889 if (stream->initdata)
1890 gst_buffer_unref (stream->initdata);
1891 if (stream->extradata)
1892 gst_buffer_unref (stream->extradata);
1893 memset (stream, 0, sizeof (avi_stream_context));
1900 * gst_avi_demux_parse_odml:
1901 * @avi: calling element (used for debug/error).
1902 * @buf: input buffer to be used for parsing.
1904 * Read an openDML-2.0 extension header. Fills in the frame number
1905 * in the avi demuxer object when reading succeeds.
1908 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
1912 GstBuffer *sub = NULL;
1914 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
1917 case GST_RIFF_TAG_dmlh:{
1918 gst_riff_dmlh dmlh, *_dmlh;
1921 /* sub == NULL is possible and means an empty buffer */
1922 size = sub ? GST_BUFFER_SIZE (sub) : 0;
1925 if (size < sizeof (gst_riff_dmlh)) {
1926 GST_ERROR_OBJECT (avi,
1927 "DMLH entry is too small (%d bytes, %d needed)",
1928 size, (int) sizeof (gst_riff_dmlh));
1931 _dmlh = (gst_riff_dmlh *) GST_BUFFER_DATA (sub);
1932 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
1934 GST_INFO_OBJECT (avi, "dmlh tag found: totalframes: %u",
1937 avi->avih->tot_frames = dmlh.totalframes;
1942 GST_WARNING_OBJECT (avi,
1943 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
1944 GST_FOURCC_ARGS (tag));
1946 case GST_RIFF_TAG_JUNK:
1948 /* skip and move to next chunk */
1950 gst_buffer_unref (sub);
1957 gst_buffer_unref (buf);
1961 * Sort helper for index entries that sorts by index time.
1962 * If times are equal we sort by stream number.
1965 sort (gst_avi_index_entry * a, gst_avi_index_entry * b)
1969 else if (a->ts < b->ts)
1972 return a->stream_nr - b->stream_nr;
1976 * gst_avi_demux_parse_index:
1977 * @avi: calling element (used for debugging/errors).
1978 * @buf: buffer containing the full index.
1979 * @entries_list: list (returned by this function) containing the index
1980 * entries parsed from the buffer. The first in the list
1981 * is also a pointer to the allocated data and should be
1982 * free'ed at some point.
1984 * Read index entries from the provided buffer. Takes ownership of @buf.
1985 * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
1988 gst_avi_demux_parse_index (GstAviDemux * avi,
1989 GstBuffer * buf, GList ** _entries_list)
1991 guint64 pos_before = avi->offset;
1992 gst_avi_index_entry *entries = NULL;
1994 GList *entries_list = NULL;
1997 #ifndef GST_DISABLE_GST_DEBUG
1998 gulong _nr_keyframes = 0;
2001 if (!buf || !GST_BUFFER_SIZE (buf)) {
2002 *_entries_list = NULL;
2003 GST_DEBUG ("empty index");
2005 gst_buffer_unref (buf);
2009 data = GST_BUFFER_DATA (buf);
2010 num = GST_BUFFER_SIZE (buf) / sizeof (gst_riff_index_entry);
2011 if (!(entries = g_try_new (gst_avi_index_entry, num)))
2014 GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2016 for (i = 0, n = 0; i < num; i++) {
2018 gst_riff_index_entry entry, *_entry;
2019 avi_stream_context *stream;
2021 gst_avi_index_entry *target;
2023 _entry = &((gst_riff_index_entry *) data)[i];
2024 entry.id = GST_READ_UINT32_LE (&_entry->id);
2025 entry.offset = GST_READ_UINT32_LE (&_entry->offset);
2026 entry.flags = GST_READ_UINT32_LE (&_entry->flags);
2027 entry.size = GST_READ_UINT32_LE (&_entry->size);
2028 target = &entries[n];
2030 if (entry.id == GST_RIFF_rec || entry.id == 0 ||
2031 (entry.offset == 0 && n > 0))
2034 stream_nr = CHUNKID_TO_STREAMNR (entry.id);
2035 if (stream_nr >= avi->num_streams) {
2036 GST_WARNING_OBJECT (avi,
2037 "Index entry %d has invalid stream nr %d", i, stream_nr);
2040 target->stream_nr = stream_nr;
2041 stream = &avi->stream[stream_nr];
2043 if (!stream->strh) {
2044 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2048 target->index_nr = i;
2050 (entry.flags & GST_RIFF_IF_KEYFRAME) ? GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME
2052 target->size = entry.size;
2053 target->offset = entry.offset + 8;
2055 /* figure out if the index is 0 based or relative to the MOVI start */
2057 if (target->offset < pos_before)
2058 avi->index_offset = pos_before + 8;
2060 avi->index_offset = 0;
2061 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2064 if (stream->strh->type == GST_RIFF_FCC_auds) {
2065 /* all audio frames are keyframes */
2066 target->flags |= GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
2068 #ifndef GST_DISABLE_GST_DEBUG
2069 if (target->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)
2073 /* stream duration unknown, now we can calculate it */
2074 if (stream->idx_duration == -1)
2075 stream->idx_duration = 0;
2078 target->ts = stream->idx_duration;
2079 if (stream->is_vbr) {
2080 /* VBR stream next timestamp */
2081 if (stream->strh->type == GST_RIFF_FCC_auds) {
2082 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
2083 stream->total_blocks + 1);
2085 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
2086 stream->total_frames + 1);
2089 /* constant rate stream */
2090 next_ts = avi_stream_convert_bytes_to_time_unchecked (stream,
2091 stream->total_bytes + target->size);
2093 /* duration is next - current */
2094 target->dur = next_ts - target->ts;
2096 /* stream position */
2097 target->bytes_before = stream->total_bytes;
2098 target->frames_before = stream->total_frames;
2100 stream->total_bytes += target->size;
2101 stream->total_frames++;
2102 if (stream->strh->type == GST_RIFF_FCC_auds) {
2103 if (stream->strf.auds->blockalign > 0)
2104 stream->total_blocks +=
2105 (target->size + stream->strf.auds->blockalign -
2106 1) / stream->strf.auds->blockalign;
2108 stream->total_blocks++;
2110 stream->idx_duration = next_ts;
2112 GST_LOG_OBJECT (avi,
2113 "Adding index entry %d (%6u), flags %02x, stream %d, size %u "
2114 ", offset %" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT ", dur %"
2116 target->index_nr, stream->total_frames - 1, target->flags,
2117 target->stream_nr, target->size, target->offset,
2118 GST_TIME_ARGS (target->ts), GST_TIME_ARGS (target->dur));
2119 entries_list = g_list_prepend (entries_list, target);
2124 GST_INFO_OBJECT (avi, "Parsed index, %6u/%6u entries, %5lu keyframes, "
2125 "entry size = %2u, total size = %10u", n, num, _nr_keyframes,
2126 (guint) sizeof (gst_avi_index_entry),
2127 (guint) (n * sizeof (gst_avi_index_entry)));
2129 gst_buffer_unref (buf);
2132 *_entries_list = g_list_reverse (entries_list);
2141 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2142 ("Cannot allocate memory for %u*%u=%u bytes",
2143 (guint) sizeof (gst_avi_index_entry), num,
2144 (guint) sizeof (gst_avi_index_entry) * num));
2145 gst_buffer_unref (buf);
2150 * gst_avi_demux_stream_index:
2151 * @avi: avi demuxer object.
2152 * @index: list of index entries, returned by this function.
2153 * @alloc_list: list of allocated data, returned by this function.
2155 * Seeks to index and reads it.
2158 gst_avi_demux_stream_index (GstAviDemux * avi,
2159 GList ** index, GList ** alloc_list)
2162 guint64 offset = avi->offset;
2167 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2172 /* get chunk information */
2173 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2174 if (res != GST_FLOW_OK)
2176 else if (GST_BUFFER_SIZE (buf) < 8)
2179 /* check tag first before blindy trying to read 'size' bytes */
2180 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2181 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2182 if (tag == GST_RIFF_TAG_LIST) {
2183 /* this is the movi tag */
2184 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2185 (8 + ((size + 1) & ~1)));
2186 offset += 8 + ((size + 1) & ~1);
2187 gst_buffer_unref (buf);
2188 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2189 if (res != GST_FLOW_OK)
2191 else if (GST_BUFFER_SIZE (buf) < 8)
2193 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2194 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2197 if (tag != GST_RIFF_TAG_idx1)
2202 gst_buffer_unref (buf);
2204 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2206 /* read chunk, advance offset */
2207 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2208 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2211 GST_DEBUG ("will parse index chunk size %u for tag %"
2212 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2214 gst_avi_demux_parse_index (avi, buf, index);
2216 *alloc_list = g_list_append (*alloc_list, (*index)->data);
2218 #ifndef GST_DISABLE_GST_DEBUG
2219 /* debug our indexes */
2222 avi_stream_context *stream;
2224 for (i = 0; i < avi->num_streams; i++) {
2225 stream = &avi->stream[i];
2226 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2227 i, stream->total_frames, stream->total_bytes);
2236 GST_DEBUG_OBJECT (avi,
2237 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2242 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2243 gst_buffer_unref (buf);
2248 GST_WARNING_OBJECT (avi,
2249 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2250 GST_FOURCC_ARGS (tag));
2251 gst_buffer_unref (buf);
2256 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2257 gst_buffer_unref (buf);
2264 * Sync to next data chunk.
2267 gst_avi_demux_skip (GstAviDemux * avi, gboolean prevent_eos)
2269 GstRiffRead *riff = GST_RIFF_READ (avi);
2272 guint64 pos, length;
2276 pos = gst_bytestream_tell (riff->bs);
2277 length = gst_bytestream_length (riff->bs);
2279 if (pos + 8 > length)
2282 if (gst_bytestream_peek_bytes (riff->bs, &data, 8) != 8)
2285 size = GST_READ_UINT32_LE (&data[4]);
2289 /* Note, we're going to skip which might involve seeks. Therefore,
2290 * we need 1 byte more! */
2291 if (pos + 8 + size >= length)
2295 return gst_riff_read_skip (riff);
2299 gst_avi_demux_sync (GstAviDemux * avi, guint32 * ret_tag, gboolean prevent_eos)
2301 GstRiffRead *riff = GST_RIFF_READ (avi);
2303 guint64 length = gst_bytestream_length (riff->bs);
2305 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2308 /* peek first (for the end of this 'list/movi' section) */
2309 if (!(tag = gst_riff_peek_tag (riff, &avi->level_up)))
2312 /* if we're at top-level, we didn't read the 'movi'
2313 * list tag yet. This can also be 'AVIX' in case of
2314 * openDML-2.0 AVI files. Lastly, it might be idx1,
2315 * in which case we skip it so we come at EOS. */
2317 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2320 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2324 case GST_RIFF_TAG_LIST:
2325 if (!(tag = gst_riff_peek_list (riff)))
2329 case GST_RIFF_LIST_AVIX:
2330 if (!gst_riff_read_list (riff, &tag))
2334 case GST_RIFF_LIST_movi:
2335 if (!gst_riff_read_list (riff, &tag))
2343 GST_WARNING ("Unknown list %" GST_FOURCC_FORMAT " before AVI data",
2344 GST_FOURCC_ARGS (tag));
2347 case GST_RIFF_TAG_JUNK:
2348 if (!gst_avi_demux_skip (avi, prevent_eos))
2355 if ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
2356 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9') {
2361 case GST_RIFF_TAG_idx1:
2362 case GST_RIFF_TAG_JUNK:
2363 if (!gst_avi_demux_skip (avi, prevent_eos)) {
2370 /* And then, we get the data */
2371 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2374 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2377 /* Support for rec-list files */
2379 case GST_RIFF_TAG_LIST:
2380 if (!(tag = gst_riff_peek_list (riff)))
2382 if (tag == GST_RIFF_rec) {
2383 /* Simply skip the list */
2384 if (!gst_riff_read_list (riff, &tag))
2386 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2391 case GST_RIFF_TAG_JUNK:
2392 gst_avi_demux_skip (avi, prevent_eos);
2404 * gst_avi_demux_peek_tag:
2406 * Returns the tag and size of the next chunk
2408 static GstFlowReturn
2409 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2412 GstFlowReturn res = GST_FLOW_OK;
2413 GstBuffer *buf = NULL;
2416 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2417 if (res != GST_FLOW_OK)
2420 bufsize = GST_BUFFER_SIZE (buf);
2424 *tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2425 *size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2427 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2428 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2429 *size, offset + 8, offset + 8 + (gint64) * size);
2431 gst_buffer_unref (buf);
2438 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2443 GST_DEBUG_OBJECT (avi, "got %d bytes which is <> 8 bytes", bufsize);
2444 res = GST_FLOW_ERROR;
2450 * gst_avi_demux_next_data_buffer:
2452 * Returns the offset and size of the next buffer
2453 * Position is the position of the buffer (after tag and size)
2455 static GstFlowReturn
2456 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2457 guint32 * tag, guint * size)
2459 guint64 off = *offset;
2464 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2465 if (res != GST_FLOW_OK)
2467 if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
2468 off += 8 + 4; /* skip tag + size + subtag */
2480 * gst_avi_demux_stream_scan:
2481 * @avi: calling element (used for debugging/errors).
2482 * @index: list of index entries, returned by this function.
2483 * @alloc_list: list of allocated data, returned by this function.
2485 * Scan the file for all chunks to "create" a new index.
2486 * Return value indicates if we can continue reading the stream. It
2487 * does not say anything about whether we created an index.
2492 gst_avi_demux_stream_scan (GstAviDemux * avi,
2493 GList ** index, GList ** alloc_list)
2496 gst_avi_index_entry *entry, *entries = NULL;
2497 avi_stream_context *stream;
2499 guint64 pos = avi->offset;
2504 guint index_size = 0;
2507 * - implement non-seekable source support.
2509 GST_DEBUG_OBJECT (avi,
2510 "Creating index %s existing index, starting at offset %" G_GUINT64_FORMAT,
2511 ((*index) ? "with" : "without"), pos);
2513 format = GST_FORMAT_BYTES;
2514 if (!gst_pad_query_peer_duration (avi->sinkpad, &format, &tmplength))
2520 entry = g_list_last (*index)->data;
2521 pos = entry->offset + avi->index_offset + entry->size;
2522 if (entry->size & 1)
2525 if (pos >= length) {
2526 GST_LOG_OBJECT (avi, "Complete index, we're done");
2530 GST_LOG_OBJECT (avi, "Incomplete index, seeking to last valid entry @ %"
2531 G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT " (%"
2532 G_GUINT64_FORMAT "+%u)", pos, length, entry->offset, entry->size);
2539 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
2540 if (G_UNLIKELY (res != GST_FLOW_OK))
2543 /* check valid stream */
2544 stream_nr = CHUNKID_TO_STREAMNR (tag);
2545 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2546 GST_WARNING_OBJECT (avi,
2547 "Index entry has invalid stream nr %d", stream_nr);
2551 stream = &avi->stream[stream_nr];
2552 if (G_UNLIKELY (stream->pad == NULL)) {
2553 GST_WARNING_OBJECT (avi,
2554 "Stream %d does not have an output pad, can't create new index",
2560 if (G_UNLIKELY (index_size % 1024 == 0)) {
2561 entries = g_new (gst_avi_index_entry, 1024);
2562 *alloc_list = g_list_prepend (*alloc_list, entries);
2564 entry = &entries[index_size % 1024];
2566 entry->index_nr = index_size++;
2567 entry->stream_nr = stream_nr;
2568 entry->flags = GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
2569 entry->offset = pos - avi->index_offset;
2572 /* timestamps, get timestamps of two consecutive frames to calculate
2573 * timestamp and duration. */
2574 format = GST_FORMAT_TIME;
2575 if (stream->is_vbr) {
2577 entry->ts = avi_stream_convert_frames_to_time_unchecked (stream,
2578 stream->total_frames);
2579 entry->dur = avi_stream_convert_frames_to_time_unchecked (stream,
2580 stream->total_frames + 1);
2582 /* constant rate stream */
2583 entry->ts = avi_stream_convert_bytes_to_time_unchecked (stream,
2584 stream->total_bytes);
2585 entry->dur = avi_stream_convert_bytes_to_time_unchecked (stream,
2586 stream->total_bytes + entry->size);
2588 entry->dur -= entry->ts;
2590 /* stream position */
2591 entry->bytes_before = stream->total_bytes;
2592 stream->total_bytes += entry->size;
2593 entry->frames_before = stream->total_frames;
2594 stream->total_frames++;
2595 stream->idx_duration = entry->ts + entry->dur;
2597 list = g_list_prepend (list, entry);
2598 GST_DEBUG_OBJECT (avi, "Added index entry %d (in stream: %d), offset %"
2599 G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT " for stream %d",
2600 index_size - 1, entry->frames_before, entry->offset,
2601 GST_TIME_ARGS (entry->ts), entry->stream_nr);
2604 /* update position */
2605 pos += GST_ROUND_UP_2 (size);
2606 if (G_UNLIKELY (pos > length)) {
2607 GST_WARNING_OBJECT (avi,
2608 "Stopping index lookup since we are further than EOF");
2613 /* FIXME: why is this disabled */
2615 while (gst_avi_demux_sync (avi, &tag, TRUE)) {
2616 guint stream_nr = CHUNKID_TO_STREAMNR (tag);
2618 GstFormat format = GST_FORMAT_TIME;
2620 if (stream_nr >= avi->num_streams)
2622 stream = &avi->stream[stream_nr];
2624 /* get chunk size */
2625 if (gst_bytestream_peek_bytes (riff->bs, &data, 8) != 8)
2629 entry->index_nr = index_size++;
2630 entry->stream_nr = stream_nr;
2631 entry->flags = GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
2632 entry->offset = gst_bytestream_tell (riff->bs) + 8 - avi->index_offset;
2633 entry->size = GST_READ_UINT32_LE (&data[4]);
2636 if (stream->is_vbr) {
2638 entry->ts = avi_stream_convert_frames_to_time_unchecked (stream,
2639 stream->total_frames);
2640 entry->dur = avi_stream_convert_frames_to_time_unchecked (stream,
2641 stream->total_frames + 1);
2643 /* constant rate stream */
2644 entry->ts = avi_stream_convert_bytes_to_time_unchecked (stream,
2645 stream->total_bytes);
2646 entry->dur = avi_stream_convert_bytes_to_time_unchecked (stream,
2647 stream->total_bytes + entry->size);
2649 entry->dur -= entry->ts;
2651 /* stream position */
2652 entry->bytes_before = stream->total_bytes;
2653 stream->total_bytes += entry->size;
2654 entry->frames_before = stream->total_frames;
2655 stream->total_frames++;
2657 list = g_list_prepend (list, entry);
2658 GST_DEBUG_OBJECT (avi, "Added index entry %d (in stream: %d), offset %"
2659 G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT " for stream %d",
2660 index_size - 1, entry->frames_before, entry->offset,
2661 GST_TIME_ARGS (entry->ts), entry->stream_nr);
2664 if (!gst_avi_demux_skip (avi, TRUE))
2668 if (!(event = gst_riff_read_seek (riff, pos))) {
2672 gst_event_unref (event);
2676 GST_DEBUG_OBJECT (avi, "index created, %d items", index_size);
2678 *index = g_list_concat (*index, g_list_reverse (list));
2684 * gst_avi_demux_massage_index:
2685 * @avi: calling element (used for debugging/errors).
2687 * We're going to go over each entry in the index and finetune
2688 * some things we don't like about AVI. For example, a single
2689 * chunk might be too long. Also, individual streams might be
2690 * out-of-sync. In the first case, we cut the chunk in several
2691 * smaller pieces. In the second case, we re-order chunk reading
2692 * order. The end result should be a smoother playing AVI.
2695 gst_avi_demux_massage_index (GstAviDemux * avi,
2696 GList * list, GList * alloc_list)
2698 gst_avi_index_entry *entry;
2699 avi_stream_context *stream;
2702 gint64 delay = G_GINT64_CONSTANT (0);
2704 GST_LOG_OBJECT (avi, "Starting index massage, nr_entries = %d",
2705 list ? g_list_length (list) : 0);
2708 #ifndef GST_DISABLE_GST_DEBUG
2709 guint num_added_total = 0;
2710 guint num_per_stream[GST_AVI_DEMUX_MAX_STREAMS] = { 0, };
2712 GST_LOG_OBJECT (avi,
2713 "I'm now going to cut large chunks into smaller pieces");
2715 /* cut chunks in small (seekable) pieces
2716 * FIXME: this should be a property where a value of
2717 * GST_CLOCK_TIME_NONE would disable the chunking
2719 #define MAX_DURATION (GST_SECOND / 2)
2720 for (i = 0; i < avi->num_streams; i++) {
2721 /* only chop streams that have exactly *one* chunk */
2722 if (avi->stream[i].total_frames != 1)
2725 for (node = list; node != NULL; node = node->next) {
2728 if (entry->stream_nr != i)
2731 /* check for max duration of a single buffer. I suppose that
2732 * the allocation of index entries could be improved. */
2733 stream = &avi->stream[entry->stream_nr];
2734 if (entry->dur > MAX_DURATION
2735 && stream->strh->type == GST_RIFF_FCC_auds) {
2737 gst_avi_index_entry *entries;
2738 guint old_size, num_added;
2741 /* cut in 1/10th of a second */
2742 ideal_size = stream->strf.auds->av_bps / 10;
2744 /* ensure chunk size is multiple of blockalign */
2745 if (stream->strf.auds->blockalign > 1)
2746 ideal_size -= ideal_size % stream->strf.auds->blockalign;
2749 old_size = entry->size;
2750 num_added = (entry->size - 1) / ideal_size;
2751 avi->index_size += num_added;
2752 entries = g_malloc (sizeof (gst_avi_index_entry) * num_added);
2753 alloc_list = g_list_prepend (alloc_list, entries);
2754 for (node2 = node->next; node2 != NULL; node2 = node2->next) {
2755 gst_avi_index_entry *entry2 = node2->data;
2757 entry2->index_nr += num_added;
2758 if (entry2->stream_nr == entry->stream_nr)
2759 entry2->frames_before += num_added;
2762 /* new sized index chunks */
2763 for (i = 0; i < num_added + 1; i++) {
2764 gst_avi_index_entry *entry2;
2769 entry2 = &entries[i - 1];
2770 list = g_list_insert_before (list, node->next, entry2);
2773 memcpy (entry2, entry, sizeof (gst_avi_index_entry));
2776 if (old_size >= ideal_size) {
2777 entry2->size = ideal_size;
2778 old_size -= ideal_size;
2780 entry2->size = old_size;
2783 entry2->dur = GST_SECOND * entry2->size / stream->strf.auds->av_bps;
2786 entry2->ts += entry->dur;
2787 entry2->offset += entry->size;
2788 entry2->bytes_before += entry->size;
2789 entry2->frames_before++;
2792 #ifndef GST_DISABLE_GST_DEBUG
2793 num_added_total += num_added;
2798 #ifndef GST_DISABLE_GST_DEBUG
2799 if (num_added_total)
2800 GST_LOG ("added %u new index entries", num_added_total);
2803 GST_LOG_OBJECT (avi, "I'm now going to reorder the index entries for time");
2805 /* re-order for time */
2806 list = g_list_sort (list, (GCompareFunc) sort);
2808 /* make a continous array out of the list */
2809 avi->index_size = g_list_length (list);
2810 avi->index_entries = g_try_new (gst_avi_index_entry, avi->index_size);
2811 if (!avi->index_entries)
2814 entry = (gst_avi_index_entry *) (list->data);
2817 GST_LOG_OBJECT (avi,
2818 "Building index array, nr_entries = %d (time offset = %"
2819 GST_TIME_FORMAT, avi->index_size, GST_TIME_ARGS (delay));
2821 for (i = 0, node = list; node != NULL; node = node->next, i++) {
2823 entry->index_nr = i;
2825 memcpy (&avi->index_entries[i], entry, sizeof (gst_avi_index_entry));
2826 #ifndef GST_DISABLE_GST_DEBUG
2827 num_per_stream[entry->stream_nr]++;
2830 GST_LOG_OBJECT (avi, "Sorted index entry %3d for stream %d of size %6u"
2831 " at offset %7" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT
2832 " dur %" GST_TIME_FORMAT,
2833 avi->index_entries[i].index_nr, entry->stream_nr, entry->size,
2834 entry->offset, GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (entry->dur));
2837 for (i = 0; i < avi->num_streams; i++) {
2838 stream = &avi->stream[i];
2839 stream->idx_duration -= delay;
2842 #ifndef GST_DISABLE_GST_DEBUG
2844 gchar str[GST_AVI_DEMUX_MAX_STREAMS * (1 + 6 + 2)];
2847 for (i = 0; i < avi->num_streams; i++) {
2848 if (!avi->stream[i].pad)
2850 pad_name = GST_OBJECT_NAME (avi->stream[i].pad);
2851 sprintf (&str[i * (1 + 6 + 2)], " %6u %c", num_per_stream[i],
2854 GST_LOG_OBJECT (avi, "indizies per stream:%20s", str);
2858 GST_LOG_OBJECT (avi, "Freeing original index list");
2859 /* all the node->data in list point to alloc_list chunks */
2864 g_list_foreach (alloc_list, (GFunc) g_free, NULL);
2865 g_list_free (alloc_list);
2867 #ifndef GST_DISABLE_GST_DEBUG
2868 for (i = 0; i < avi->num_streams; i++) {
2869 GST_LOG_OBJECT (avi, "Stream %d, %d frames, %8" G_GUINT64_FORMAT " bytes",
2870 i, avi->stream[i].total_frames, avi->stream[i].total_bytes);
2874 GST_LOG_OBJECT (avi, "Index massaging done");
2879 GST_WARNING_OBJECT (avi, "Out of memory for %" G_GSIZE_FORMAT " bytes",
2880 sizeof (gst_avi_index_entry) * avi->index_size);
2885 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
2890 total = GST_CLOCK_TIME_NONE;
2892 /* all streams start at a timestamp 0 */
2893 for (stream = 0; stream < avi->num_streams; stream++) {
2894 GstClockTime duration, hduration;
2895 avi_stream_context *streamc = &avi->stream[stream];
2896 gst_riff_strh *strh = streamc->strh;
2901 /* get header duration for the stream */
2902 hduration = streamc->hdr_duration;
2904 /* index duration calculated during parsing, invariant under massage */
2905 duration = streamc->idx_duration;
2907 /* now pick a good duration */
2908 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2909 /* index gave valid duration, use that */
2910 GST_INFO ("Stream %d duration according to index: %" GST_TIME_FORMAT,
2911 stream, GST_TIME_ARGS (duration));
2913 /* fall back to header info to calculate a duration */
2914 duration = hduration;
2916 /* set duration for the stream */
2917 streamc->duration = duration;
2919 /* find total duration */
2920 if (total == GST_CLOCK_TIME_NONE || duration > total)
2924 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
2925 /* now update the duration for those streams where we had none */
2926 for (stream = 0; stream < avi->num_streams; stream++) {
2927 avi_stream_context *streamc = &avi->stream[stream];
2929 if (!GST_CLOCK_TIME_IS_VALID (streamc->duration)
2930 || streamc->duration == 0) {
2931 streamc->duration = total;
2933 GST_INFO ("Stream %d duration according to total: %" GST_TIME_FORMAT,
2934 stream, GST_TIME_ARGS (total));
2939 /* and set the total duration in the segment. */
2940 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
2941 GST_TIME_ARGS (total));
2943 gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
2946 /* returns FALSE if there are no pads to deliver event to,
2947 * otherwise TRUE (whatever the outcome of event sending) */
2949 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
2951 gboolean result = FALSE;
2954 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
2955 GST_EVENT_TYPE_NAME (event), avi->num_streams);
2957 if (avi->num_streams) {
2958 for (i = 0; i < avi->num_streams; i++) {
2959 avi_stream_context *stream = &avi->stream[i];
2963 gst_pad_push_event (stream->pad, gst_event_ref (event));
2967 gst_event_unref (event);
2972 * Read AVI headers when streaming
2974 static GstFlowReturn
2975 gst_avi_demux_stream_header_push (GstAviDemux * avi)
2977 GstFlowReturn ret = GST_FLOW_OK;
2982 GstBuffer *buf = NULL, *sub = NULL;
2986 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
2988 switch (avi->header_state) {
2989 case GST_AVI_DEMUX_HEADER_TAG_LIST:
2990 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2991 avi->offset += 8 + ((size + 1) & ~1);
2992 if (tag != GST_RIFF_TAG_LIST)
2993 goto header_no_list;
2995 gst_adapter_flush (avi->adapter, 8);
2996 /* Find the 'hdrl' LIST tag */
2997 GST_DEBUG ("Reading %d bytes", size);
2998 buf = gst_adapter_take_buffer (avi->adapter, size);
3000 if (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl)
3001 goto header_no_hdrl;
3005 gst_adapter_flush (avi->adapter, 1);
3007 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
3009 /* the hdrl starts with a 'avih' header */
3010 if (!gst_riff_parse_chunk (GST_ELEMENT (avi), buf, &offset, &tag, &sub))
3011 goto header_no_avih;
3013 if (tag != GST_RIFF_TAG_avih)
3014 goto header_no_avih;
3016 if (!gst_avi_demux_parse_avih (GST_ELEMENT (avi), sub, &avi->avih))
3017 goto header_wrong_avih;
3019 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
3021 /* now, read the elements from the header until the end */
3022 while (gst_riff_parse_chunk (GST_ELEMENT (avi), buf, &offset, &tag,
3024 /* sub can be NULL on empty tags */
3029 case GST_RIFF_TAG_LIST:
3030 if (GST_BUFFER_SIZE (sub) < 4)
3033 switch (GST_READ_UINT32_LE (GST_BUFFER_DATA (sub))) {
3034 case GST_RIFF_LIST_strl:
3035 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3037 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3038 ("failed to parse stream, ignoring"));
3043 case GST_RIFF_LIST_odml:
3044 gst_avi_demux_parse_odml (avi, sub);
3048 GST_WARNING_OBJECT (avi,
3049 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3050 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA
3053 case GST_RIFF_TAG_JUNK:
3058 GST_WARNING_OBJECT (avi,
3059 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
3060 offset, GST_FOURCC_ARGS (tag));
3062 case GST_RIFF_TAG_JUNK:
3064 /* move to next chunk */
3066 gst_buffer_unref (sub);
3071 gst_buffer_unref (buf);
3072 GST_DEBUG ("elements parsed");
3074 /* check parsed streams */
3075 if (avi->num_streams == 0) {
3077 } else if (avi->num_streams != avi->avih->streams) {
3078 GST_WARNING_OBJECT (avi,
3079 "Stream header mentioned %d streams, but %d available",
3080 avi->avih->streams, avi->num_streams);
3082 GST_DEBUG ("Get junk and info next");
3083 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
3085 /* Need more data */
3089 case GST_AVI_DEMUX_HEADER_INFO:
3090 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
3092 if (gst_adapter_available (avi->adapter) < 12)
3095 data = gst_adapter_peek (avi->adapter, 12);
3096 tag = GST_READ_UINT32_LE (data);
3097 size = GST_READ_UINT32_LE (data + 4);
3098 ltag = GST_READ_UINT32_LE (data + 8);
3100 if (tag == GST_RIFF_TAG_LIST) {
3102 case GST_RIFF_LIST_movi:
3103 gst_adapter_flush (avi->adapter, 12);
3106 case GST_RIFF_LIST_INFO:
3107 GST_DEBUG ("Found INFO chunk");
3108 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3109 GST_DEBUG ("got size %d", size);
3111 gst_adapter_flush (avi->adapter, 12);
3113 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
3116 gst_adapter_flush (avi->adapter, 1);
3117 gst_riff_parse_info (GST_ELEMENT (avi), buf,
3119 gst_buffer_unref (buf);
3121 avi->offset += ((size + 1) & ~1) - 4;
3123 GST_DEBUG ("skipping INFO LIST prefix");
3126 /* Need more data */
3131 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3132 avi->offset += 8 + ((size + 1) & ~1);
3133 gst_adapter_flush (avi->adapter, 8 + ((size + 1) & ~1));
3134 // ??? goto iterate; ???
3136 /* Need more data */
3142 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3143 avi->offset += 8 + ((size + 1) & ~1);
3144 gst_adapter_flush (avi->adapter, 8 + ((size + 1) & ~1));
3147 /* Need more data */
3154 GST_WARNING ("unhandled header state: %d", avi->header_state);
3159 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3160 avi->num_streams, avi->stream[0].indexes);
3162 GST_DEBUG ("Found movi chunk. Starting to stream data");
3163 avi->state = GST_AVI_DEMUX_MOVI;
3166 /*GList *index = NULL, *alloc = NULL; */
3168 /* ######################## this need to be integrated with the state */
3169 /* create or read stream index (for seeking) */
3170 if (avi->stream[0].indexes != NULL) {
3171 gst_avi_demux_read_subindexes_push (avi, &index, &alloc);
3174 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX) {
3175 gst_avi_demux_stream_index (avi, &index, &alloc);
3177 /* some indexes are incomplete, continue streaming from there */
3179 gst_avi_demux_stream_scan (avi, &index, &alloc);
3182 /* this is a fatal error */
3186 if (!gst_avi_demux_massage_index (avi, index, alloc))
3189 gst_avi_demux_calculate_durations_from_index (avi);
3190 /* ######################## */
3193 /* create initial NEWSEGMENT event */
3194 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3195 stop = avi->segment.duration;
3197 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3199 if (avi->seek_event)
3200 gst_event_unref (avi->seek_event);
3201 avi->seek_event = gst_event_new_new_segment
3202 (FALSE, avi->segment.rate, GST_FORMAT_TIME,
3203 avi->segment.start, stop, avi->segment.start);
3205 /* at this point we know all the streams and we can signal the no more
3207 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3208 gst_element_no_more_pads (GST_ELEMENT (avi));
3215 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3216 return GST_FLOW_ERROR;
3220 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3221 ("Invalid AVI header (no LIST at start): %"
3222 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3223 return GST_FLOW_ERROR;
3227 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3228 ("Invalid AVI header (no hdrl at start): %"
3229 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3230 gst_buffer_unref (buf);
3231 return GST_FLOW_ERROR;
3235 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3236 ("Invalid AVI header (no avih at start): %"
3237 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3239 gst_buffer_unref (sub);
3241 gst_buffer_unref (buf);
3242 return GST_FLOW_ERROR;
3246 gst_buffer_unref (buf);
3247 return GST_FLOW_ERROR;
3252 * Read full AVI headers.
3254 static GstFlowReturn
3255 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
3258 GstBuffer *buf, *sub = NULL;
3260 GList *index = NULL, *alloc = NULL;
3263 GstElement *element = GST_ELEMENT_CAST (avi);
3265 /* the header consists of a 'hdrl' LIST tag */
3266 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3267 if (res != GST_FLOW_OK)
3268 goto pull_range_failed;
3269 else if (tag != GST_RIFF_TAG_LIST)
3271 else if (GST_BUFFER_SIZE (buf) < 4)
3274 GST_DEBUG_OBJECT (avi, "parsing headers");
3276 /* Find the 'hdrl' LIST tag */
3277 while (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl) {
3278 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
3279 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf))));
3282 gst_buffer_unref (buf);
3284 /* read new chunk */
3285 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3286 if (res != GST_FLOW_OK)
3287 goto pull_range_failed;
3288 else if (tag != GST_RIFF_TAG_LIST)
3290 else if (GST_BUFFER_SIZE (buf) < 4)
3294 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
3296 /* the hdrl starts with a 'avih' header */
3297 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
3299 else if (tag != GST_RIFF_TAG_avih)
3301 else if (!gst_avi_demux_parse_avih (element, sub, &avi->avih))
3304 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3306 /* now, read the elements from the header until the end */
3307 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
3308 /* sub can be NULL on empty tags */
3313 case GST_RIFF_TAG_LIST:
3318 if (GST_BUFFER_SIZE (sub) < 4)
3321 data = GST_BUFFER_DATA (sub);
3322 fourcc = GST_READ_UINT32_LE (data);
3325 case GST_RIFF_LIST_strl:
3326 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3327 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3328 ("failed to parse stream, ignoring"));
3333 case GST_RIFF_LIST_odml:
3334 gst_avi_demux_parse_odml (avi, sub);
3338 GST_WARNING_OBJECT (avi,
3339 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3340 GST_FOURCC_ARGS (fourcc));
3341 GST_MEMDUMP_OBJECT (avi, "Unknown list", GST_BUFFER_DATA (sub),
3342 GST_BUFFER_SIZE (sub));
3344 case GST_RIFF_TAG_JUNK:
3350 GST_WARNING_OBJECT (avi,
3351 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
3352 GST_FOURCC_ARGS (tag), offset);
3353 GST_MEMDUMP_OBJECT (avi, "Unknown tag", GST_BUFFER_DATA (sub),
3354 GST_BUFFER_SIZE (sub));
3356 case GST_RIFF_TAG_JUNK:
3359 gst_buffer_unref (sub);
3364 gst_buffer_unref (buf);
3365 GST_DEBUG ("elements parsed");
3367 /* check parsed streams */
3368 if (avi->num_streams == 0)
3370 else if (avi->num_streams != avi->avih->streams) {
3371 GST_WARNING_OBJECT (avi,
3372 "Stream header mentioned %d streams, but %d available",
3373 avi->avih->streams, avi->num_streams);
3376 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
3377 G_GUINT64_FORMAT, avi->offset);
3379 /* Now, find the data (i.e. skip all junk between header and data) */
3384 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
3385 if (res != GST_FLOW_OK) {
3386 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
3387 goto pull_range_failed;
3388 } else if (GST_BUFFER_SIZE (buf) < 12) {
3389 GST_DEBUG_OBJECT (avi, "got %d bytes which is less than 12 bytes",
3390 GST_BUFFER_SIZE (buf));
3391 gst_buffer_unref (buf);
3392 return GST_FLOW_ERROR;
3395 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
3396 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
3397 ltag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 8);
3399 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
3400 GST_FOURCC_ARGS (tag), size);
3401 GST_MEMDUMP ("Tag content", GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
3402 gst_buffer_unref (buf);
3405 case GST_RIFF_TAG_LIST:{
3407 case GST_RIFF_LIST_movi:
3408 GST_DEBUG_OBJECT (avi,
3409 "Reached the 'movi' tag, we're done with skipping");
3411 case GST_RIFF_LIST_INFO:
3413 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3415 if (res != GST_FLOW_OK) {
3416 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3417 goto pull_range_failed;
3419 GST_DEBUG ("got size %u", GST_BUFFER_SIZE (buf));
3421 GST_DEBUG ("skipping INFO LIST prefix");
3422 avi->offset += (4 - GST_ROUND_UP_2 (size));
3423 gst_buffer_unref (buf);
3427 sub = gst_buffer_create_sub (buf, 4, GST_BUFFER_SIZE (buf) - 4);
3428 gst_riff_parse_info (element, sub, &avi->globaltags);
3430 gst_buffer_unref (sub);
3433 gst_buffer_unref (buf);
3434 /* gst_riff_read_chunk() has already advanced avi->offset */
3437 GST_WARNING_OBJECT (avi,
3438 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3439 GST_FOURCC_ARGS (ltag));
3440 avi->offset += 8 + ((size + 1) & ~1);
3446 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3447 GST_FOURCC_ARGS (tag));
3449 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3450 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3451 avi->offset += 8 + ((size + 1) & ~1);
3457 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3458 avi->num_streams, avi->stream[0].indexes);
3460 /* create or read stream index (for seeking) */
3461 if (avi->stream[0].indexes != NULL) {
3462 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3463 gst_avi_demux_read_subindexes_pull (avi, &index, &alloc);
3466 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX) {
3467 gst_avi_demux_stream_index (avi, &index, &alloc);
3469 /* some indexes are incomplete, continue streaming from there */
3471 gst_avi_demux_stream_scan (avi, &index, &alloc);
3474 /* this is a fatal error */
3478 if (!gst_avi_demux_massage_index (avi, index, alloc))
3481 gst_avi_demux_calculate_durations_from_index (avi);
3483 /* create initial NEWSEGMENT event */
3484 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3485 stop = avi->segment.duration;
3487 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3489 if (avi->seek_event)
3490 gst_event_unref (avi->seek_event);
3491 avi->seek_event = gst_event_new_new_segment
3492 (FALSE, avi->segment.rate, GST_FORMAT_TIME,
3493 avi->segment.start, stop, avi->segment.start);
3495 /* at this point we know all the streams and we can signal the no more
3497 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3498 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3505 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3506 ("Invalid AVI header (no LIST at start): %"
3507 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3508 gst_buffer_unref (buf);
3509 return GST_FLOW_ERROR;
3513 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3514 ("Invalid AVI header (no hdrl at start): %"
3515 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3516 gst_buffer_unref (buf);
3517 return GST_FLOW_ERROR;
3521 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3522 ("Invalid AVI header (no avih at start): %"
3523 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3525 gst_buffer_unref (sub);
3526 gst_buffer_unref (buf);
3527 return GST_FLOW_ERROR;
3531 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3532 ("Invalid AVI header (cannot parse avih at start)"));
3533 gst_buffer_unref (buf);
3534 return GST_FLOW_ERROR;
3538 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3539 return GST_FLOW_ERROR;
3543 GST_WARNING ("file without or too big index");
3544 g_list_free (index);
3545 g_list_foreach (alloc, (GFunc) g_free, NULL);
3546 g_list_free (alloc);
3548 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3549 ("Could not get/create index"));
3550 return GST_FLOW_ERROR;
3554 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3555 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3556 return GST_FLOW_ERROR;
3561 * Do the actual seeking.
3564 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
3566 GstClockTime seek_time;
3568 gst_avi_index_entry *entry, *kentry;
3571 seek_time = segment->last_stop;
3572 keyframe = !!(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
3574 /* FIXME: if we seek in an openDML file, we will have multiple
3575 * primary levels. Seeking in between those will cause havoc. */
3577 /* save old position so we can see if we must mark a discont. */
3578 old_entry = avi->current_entry;
3580 /* get the entry for the requested position, which is always in last_stop.
3581 * we search the index entry for stream 0, since all entries are sorted by
3582 * time and stream we automagically are positioned for the other streams as
3583 * well. FIXME, this code assumes the main stream with keyframes is stream 0,
3584 * which is mostly correct... */
3585 if (!(entry = gst_avi_demux_index_entry_for_time (avi, 0, seek_time)))
3588 GST_DEBUG_OBJECT (avi,
3589 "Got requested entry %d [stream:%d / ts:%" GST_TIME_FORMAT
3590 " / duration:%" GST_TIME_FORMAT "]", entry->index_nr,
3591 entry->stream_nr, GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (entry->dur));
3593 /* check if we are already on a keyframe */
3594 if (!(entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)) {
3595 /* now go to the previous keyframe, this is where we should start
3597 if (!(kentry = gst_avi_demux_index_prev (avi, 0, entry->index_nr,
3598 GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME))) {
3602 /* we were on a keyframe */
3606 GST_DEBUG_OBJECT (avi,
3607 "Got keyframe entry %d [stream:%d / ts:%" GST_TIME_FORMAT
3608 " / duration:%" GST_TIME_FORMAT "]", kentry->index_nr,
3609 entry->stream_nr, GST_TIME_ARGS (kentry->ts),
3610 GST_TIME_ARGS (kentry->dur));
3612 /* we must start decoding at the keyframe */
3613 avi->current_entry = kentry->index_nr;
3615 if (segment->rate < 0.0) {
3616 gst_avi_index_entry *next_keyframe;
3618 /* Because we don't know the frame order we need to push from the prev keyframe
3619 * to the next keyframe. If there is a smart decoder downstream he will notice
3620 * that there are too many encoded frames send and return UNEXPECTED when there
3621 * are enough decoded frames to fill the segment.
3624 gst_avi_demux_index_next (avi, 0, kentry->index_nr,
3625 GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME);
3627 next_keyframe = gst_avi_demux_index_last (avi, 0);
3629 avi->reverse_start_index = kentry->index_nr;
3630 avi->reverse_stop_index = next_keyframe->index_nr;
3632 GST_DEBUG_OBJECT (avi, "reverse seek: start idx (%d) and stop idx (%d)",
3633 avi->reverse_start_index, avi->reverse_stop_index);
3637 /* when seeking to a keyframe, we update the result seek time
3638 * to the time of the keyframe. */
3639 seek_time = avi->index_entries[avi->current_entry].ts;
3643 /* if we changed position, mark a DISCONT on all streams */
3644 if (avi->current_entry != old_entry) {
3647 for (i = 0; i < avi->num_streams; i++) {
3648 avi->stream[i].discont = TRUE;
3652 GST_DEBUG_OBJECT (avi, "seek: %" GST_TIME_FORMAT
3653 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
3655 /* the seek time is also the last_stop and stream time */
3656 segment->last_stop = seek_time;
3657 segment->time = seek_time;
3663 /* we could not find an entry for the given time */
3664 GST_WARNING_OBJECT (avi,
3665 "Couldn't find AviIndexEntry for time:%" GST_TIME_FORMAT,
3666 GST_TIME_ARGS (seek_time));
3667 if (avi->current_entry >= avi->index_size && avi->index_size > 0)
3668 avi->current_entry = avi->index_size - 1;
3675 * Handle seek event.
3678 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
3683 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
3687 GstSegment seeksegment = { 0, };
3690 GST_DEBUG_OBJECT (avi, "doing seek with event");
3692 gst_event_parse_seek (event, &rate, &format, &flags,
3693 &cur_type, &cur, &stop_type, &stop);
3695 /* we have to have a format as the segment format. Try to convert
3697 if (format != GST_FORMAT_TIME) {
3698 GstFormat fmt = GST_FORMAT_TIME;
3699 gboolean res = TRUE;
3701 if (cur_type != GST_SEEK_TYPE_NONE)
3702 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
3703 if (res && stop_type != GST_SEEK_TYPE_NONE)
3704 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
3710 GST_DEBUG_OBJECT (avi,
3711 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
3712 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
3713 /* FIXME: can we do anything with rate!=1.0 */
3715 GST_DEBUG_OBJECT (avi, "doing seek without event");
3720 /* save flush flag */
3721 flush = flags & GST_SEEK_FLAG_FLUSH;
3724 GstEvent *event = gst_event_new_flush_start ();
3726 /* for a flushing seek, we send a flush_start on all pads. This will
3727 * eventually stop streaming with a WRONG_STATE. We can thus eventually
3728 * take the STREAM_LOCK. */
3729 GST_DEBUG_OBJECT (avi, "sending flush start");
3730 gst_avi_demux_push_event (avi, gst_event_ref (event));
3731 gst_pad_push_event (avi->sinkpad, event);
3733 /* a non-flushing seek, we PAUSE the task so that we can take the
3735 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
3736 gst_pad_pause_task (avi->sinkpad);
3739 /* wait for streaming to stop */
3740 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
3741 GST_PAD_STREAM_LOCK (avi->sinkpad);
3743 /* copy segment, we need this because we still need the old
3744 * segment when we close the current segment. */
3745 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
3748 GST_DEBUG_OBJECT (avi, "configuring seek");
3749 gst_segment_set_seek (&seeksegment, rate, format, flags,
3750 cur_type, cur, stop_type, stop, &update);
3753 /* do the seek, seeksegment.last_stop contains the new position, this
3754 * actually never fails. */
3755 gst_avi_demux_do_seek (avi, &seeksegment);
3760 GST_DEBUG_OBJECT (avi, "sending flush stop");
3761 gst_avi_demux_push_event (avi, gst_event_new_flush_stop ());
3762 gst_pad_push_event (avi->sinkpad, gst_event_new_flush_stop ());
3763 /* reset the last flow and mark discont, FLUSH is always DISCONT */
3764 for (i = 0; i < avi->num_streams; i++) {
3765 avi->stream[i].last_flow = GST_FLOW_OK;
3766 avi->stream[i].discont = TRUE;
3768 } else if (avi->segment_running) {
3771 /* we are running the current segment and doing a non-flushing seek,
3772 * close the segment first based on the last_stop. */
3773 GST_DEBUG_OBJECT (avi, "closing running segment %" G_GINT64_FORMAT
3774 " to %" G_GINT64_FORMAT, avi->segment.start, avi->segment.last_stop);
3775 seg = gst_event_new_new_segment (TRUE,
3776 avi->segment.rate, avi->segment.format,
3777 avi->segment.start, avi->segment.last_stop, avi->segment.time);
3778 gst_avi_demux_push_event (avi, seg);
3781 /* now update the real segment info */
3782 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
3784 /* post the SEGMENT_START message when we do segmented playback */
3785 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3786 gst_element_post_message (GST_ELEMENT (avi),
3787 gst_message_new_segment_start (GST_OBJECT (avi),
3788 avi->segment.format, avi->segment.last_stop));
3791 /* prepare for streaming again */
3792 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3793 stop = avi->segment.duration;
3795 /* queue the segment event for the streaming thread. */
3796 if (avi->seek_event)
3797 gst_event_unref (avi->seek_event);
3798 if (avi->segment.rate > 0.0) {
3799 avi->seek_event = gst_event_new_new_segment (FALSE,
3800 avi->segment.rate, avi->segment.format,
3801 avi->segment.last_stop, stop, avi->segment.time);
3803 avi->seek_event = gst_event_new_new_segment (FALSE,
3804 avi->segment.rate, avi->segment.format,
3805 avi->segment.start, avi->segment.last_stop, avi->segment.start);
3808 if (!avi->streaming) {
3809 avi->segment_running = TRUE;
3810 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
3813 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
3820 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
3826 * Helper for gst_avi_demux_invert()
3829 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
3831 memcpy (tmp, d1, bytes);
3832 memcpy (d1, d2, bytes);
3833 memcpy (d2, tmp, bytes);
3837 #define gst_avi_demux_is_uncompressed(fourcc) \
3838 (fourcc == GST_RIFF_DIB || \
3839 fourcc == GST_RIFF_rgb || \
3840 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW)
3843 * Invert DIB buffers... Takes existing buffer and
3844 * returns either the buffer or a new one (with old
3845 * one dereferenced).
3846 * FIXME: can't we preallocate tmp? and remember stride, bpp?
3849 gst_avi_demux_invert (avi_stream_context * stream, GstBuffer * buf)
3856 if (stream->strh->type != GST_RIFF_FCC_vids)
3859 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
3860 return buf; /* Ignore non DIB buffers */
3863 s = gst_caps_get_structure (GST_PAD_CAPS (stream->pad), 0);
3864 if (!gst_structure_get_int (s, "bpp", &bpp)) {
3865 GST_WARNING ("Failed to retrieve depth from caps");
3869 if (stream->strf.vids == NULL) {
3870 GST_WARNING ("Failed to retrieve vids for stream");
3874 h = stream->strf.vids->height;
3875 w = stream->strf.vids->width;
3876 stride = w * (bpp / 8);
3878 buf = gst_buffer_make_writable (buf);
3879 if (GST_BUFFER_SIZE (buf) < (stride * h)) {
3880 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
3884 tmp = g_malloc (stride);
3886 for (y = 0; y < h / 2; y++) {
3887 swap_line (GST_BUFFER_DATA (buf) + stride * y,
3888 GST_BUFFER_DATA (buf) + stride * (h - 1 - y), tmp, stride);
3897 * Returns the aggregated GstFlowReturn.
3899 static GstFlowReturn
3900 gst_avi_demux_combine_flows (GstAviDemux * avi, avi_stream_context * stream,
3905 /* store the value */
3906 stream->last_flow = ret;
3908 /* any other error that is not-linked can be returned right away */
3909 if (G_UNLIKELY (ret != GST_FLOW_NOT_LINKED))
3912 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3913 for (i = 0; i < avi->num_streams; i++) {
3914 avi_stream_context *ostream = &avi->stream[i];
3916 ret = ostream->last_flow;
3917 /* some other return value (must be SUCCESS but we can return
3918 * other values as well) */
3919 if (G_UNLIKELY (ret != GST_FLOW_NOT_LINKED))
3922 /* if we get here, all other pads were unlinked and we return
3923 * NOT_LINKED then */
3925 GST_LOG_OBJECT (avi, "combined return %s", gst_flow_get_name (ret));
3930 * prepare the avi element for a reverse jump to a prev keyframe
3931 * this function will return the start entry. if the function returns
3932 * NULL there was no prev keyframe.
3934 static gst_avi_index_entry *
3935 gst_avi_demux_step_reverse (GstAviDemux * avi)
3937 gst_avi_index_entry *entry;
3940 avi->reverse_stop_index = avi->reverse_start_index;
3942 gst_avi_demux_index_prev (avi, 0, avi->reverse_stop_index,
3943 GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME);
3945 GST_DEBUG_OBJECT (avi, "no valid index entry found index %d",
3946 avi->reverse_stop_index);
3949 avi->current_entry = avi->reverse_start_index = entry->index_nr;
3950 GST_DEBUG_OBJECT (avi,
3951 "reverse playback jump: start idx (%d) and stop idx (%d)",
3952 avi->reverse_start_index, avi->reverse_stop_index);
3953 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, entry->ts);
3954 for (i = 0; i < avi->num_streams; i++) {
3955 avi->stream[i].last_flow = GST_FLOW_OK;
3956 avi->stream[i].discont = TRUE;
3962 * Read data from one index entry
3964 static GstFlowReturn
3965 gst_avi_demux_process_next_entry (GstAviDemux * avi)
3967 GstFlowReturn res = GST_FLOW_OK;
3968 gboolean processed = FALSE;
3969 avi_stream_context *stream;
3970 gst_avi_index_entry *entry;
3974 /* see if we are at the end */
3975 if ((avi->segment.rate > 0 && avi->current_entry >= avi->index_size))
3978 /* get next entry, this will work as we checked for the index size above */
3979 entry = &avi->index_entries[avi->current_entry++];
3981 /* check for reverse playback */
3982 if (avi->segment.rate < 0 && avi->current_entry > avi->reverse_stop_index) {
3983 GST_LOG_OBJECT (avi, "stop_index %d reached", avi->reverse_stop_index);
3985 /* check if we have pushed enough data for this segment */
3986 if (avi->reverse_start_index == 0)
3987 goto eos_reverse_zero;
3988 if (avi->index_entries[avi->reverse_start_index].ts < avi->segment.start)
3989 goto eos_reverse_segment;
3991 if (!(entry = gst_avi_demux_step_reverse (avi)))
3994 avi->current_entry++;
3997 /* see if we have a valid stream, ignore if not
3998 * FIXME: can't we check this when building the index?
3999 * we check it in _parse_index(), _stream_scan()
4001 if (entry->stream_nr >= avi->num_streams) {
4002 GST_WARNING_OBJECT (avi,
4003 "Entry %d has non-existing stream nr %d",
4004 avi->current_entry - 1, entry->stream_nr);
4008 /* get stream now */
4009 stream = &avi->stream[entry->stream_nr];
4011 if (avi->segment.rate > 0.0) {
4012 /* only check this for fowards playback for now */
4013 if ((entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)
4014 && GST_CLOCK_TIME_IS_VALID (entry->ts)
4015 && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
4016 && (entry->ts > avi->segment.stop)) {
4021 /* skip empty entries */
4022 if (entry->size == 0 || !stream->pad) {
4023 GST_DEBUG_OBJECT (avi, "Skipping entry %d (%d, %p)",
4024 avi->current_entry - 1, entry->size, stream->pad);
4028 GST_LOG ("reading buffer (size=%d) from stream %d at current pos %"
4029 G_GUINT64_FORMAT " (%llx)", entry->size, entry->stream_nr,
4030 avi->index_offset + entry->offset, avi->index_offset + entry->offset);
4032 /* pull in the data */
4033 res = gst_pad_pull_range (avi->sinkpad, entry->offset +
4034 avi->index_offset, entry->size, &buf);
4035 if (res != GST_FLOW_OK)
4038 /* check for short buffers, this is EOS as well */
4039 if (GST_BUFFER_SIZE (buf) < entry->size)
4042 /* invert the picture if needed */
4043 buf = gst_avi_demux_invert (stream, buf);
4045 /* mark non-keyframes */
4046 if (!(entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME))
4047 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4049 GST_BUFFER_TIMESTAMP (buf) = entry->ts;
4050 GST_BUFFER_DURATION (buf) = entry->dur;
4051 if (stream->strh->type == GST_RIFF_FCC_vids) {
4052 if (stream->current_frame >= 0)
4053 GST_BUFFER_OFFSET (buf) = stream->current_frame;
4056 GstFormat fmt = GST_FORMAT_DEFAULT;
4058 if (gst_pad_query_convert (stream->pad, GST_FORMAT_TIME, entry->ts,
4060 GST_BUFFER_OFFSET (buf) = framenum;
4062 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4065 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4066 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
4067 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4069 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %d, offset %"
4070 G_GUINT64_FORMAT " and time %"
4071 GST_TIME_FORMAT " on pad %s",
4072 GST_BUFFER_SIZE (buf), GST_BUFFER_OFFSET (buf),
4073 GST_TIME_ARGS (entry->ts), GST_PAD_NAME (stream->pad));
4075 /* update current position in the segment */
4076 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, entry->ts);
4078 /* mark discont when pending */
4079 if (stream->discont) {
4080 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4081 stream->discont = FALSE;
4084 res = gst_pad_push (stream->pad, buf);
4086 /* mark as processed, we increment the frame and byte counters then
4087 * leave the while loop and return the GstFlowReturn */
4089 GST_DEBUG_OBJECT (avi, "Processed buffer %d: %s", entry->index_nr,
4090 gst_flow_get_name (res));
4092 if (avi->segment.rate < 0
4093 && entry->ts > avi->segment.stop && res == GST_FLOW_UNEXPECTED) {
4094 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
4095 * we are at the end of the segment, so we just need to jump
4096 * back to the previous section.
4098 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
4100 if (!(entry = gst_avi_demux_step_reverse (avi)))
4105 stream->current_frame = entry->frames_before;
4106 stream->current_byte = entry->bytes_before;
4112 res = gst_avi_demux_combine_flows (avi, stream, res);
4115 stream->current_frame = entry->frames_before + 1;
4116 stream->current_byte = entry->bytes_before + entry->size;
4117 } while (!processed);
4120 GST_DEBUG_OBJECT (avi, "returning %s", gst_flow_get_name (res));
4127 GST_LOG_OBJECT (avi, "Handled last index entry, setting EOS (%d > %d)",
4128 avi->current_entry, avi->index_size);
4129 /* we mark the first stream as EOS */
4130 res = GST_FLOW_UNEXPECTED;
4135 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
4136 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
4137 GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (avi->segment.stop));
4138 res = GST_FLOW_UNEXPECTED;
4143 GST_DEBUG_OBJECT (avi, "start_index was 0, setting EOS");
4144 res = GST_FLOW_UNEXPECTED;
4147 eos_reverse_segment:
4149 GST_DEBUG_OBJECT (avi, "full segment pushed, setting EOS");
4150 res = GST_FLOW_UNEXPECTED;
4155 GST_DEBUG_OBJECT (avi,
4156 "pull range failed: pos=%" G_GUINT64_FORMAT " size=%d",
4157 entry->offset + avi->index_offset, entry->size);
4162 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
4163 ", only got %d/%d bytes (truncated file?)", entry->offset +
4164 avi->index_offset, GST_BUFFER_SIZE (buf), entry->size);
4165 gst_buffer_unref (buf);
4166 res = GST_FLOW_UNEXPECTED;
4172 * Read data. If we have an index it delegates to
4173 * gst_avi_demux_process_next_entry().
4175 static GstFlowReturn
4176 gst_avi_demux_stream_data (GstAviDemux * avi)
4181 GstFlowReturn res = GST_FLOW_OK;
4182 GstFormat format = GST_FORMAT_TIME;
4184 /* if we have a avi->index_entries[], we don't want to read
4185 * the stream linearly, but seek to the next ts/index_entry. */
4186 if (G_LIKELY (avi->index_entries != NULL))
4187 return gst_avi_demux_process_next_entry (avi);
4189 if (G_UNLIKELY (avi->have_eos)) {
4190 /* Clean adapter, we're done */
4191 gst_adapter_clear (avi->adapter);
4196 if (!gst_avi_demux_sync (avi, &tag, FALSE))
4200 /* Iterate until need more data, so adapter won't grow too much */
4202 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
4206 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
4207 GST_FOURCC_ARGS (tag), size);
4209 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
4210 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
4211 GST_LOG ("Chunk ok");
4212 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
4213 GST_DEBUG ("Found sub-index tag");
4214 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4215 /* accept 0 size buffer here */
4216 avi->abort_buffering = FALSE;
4217 GST_DEBUG (" skipping %d bytes for now", size);
4218 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4221 } else if (tag == GST_RIFF_TAG_idx1) {
4222 GST_DEBUG ("Found index tag, stream done");
4223 avi->have_eos = TRUE;
4224 return GST_FLOW_UNEXPECTED;
4225 } else if (tag == GST_RIFF_TAG_LIST) {
4226 /* movi chunks might be grouped in rec list */
4227 if (gst_adapter_available (avi->adapter) >= 12) {
4228 GST_DEBUG ("Found LIST tag, skipping LIST header");
4229 gst_adapter_flush (avi->adapter, 12);
4233 } else if (tag == GST_RIFF_TAG_JUNK) {
4234 /* rec list might contain JUNK chunks */
4235 GST_DEBUG ("Found JUNK tag");
4236 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4237 /* accept 0 size buffer here */
4238 avi->abort_buffering = FALSE;
4239 GST_DEBUG (" skipping %d bytes for now", size);
4240 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4244 GST_DEBUG ("No more stream chunks, send EOS");
4245 avi->have_eos = TRUE;
4246 return GST_FLOW_UNEXPECTED;
4249 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
4250 /* supposedly one hopes to catch a nicer chunk later on ... */
4251 /* FIXME ?? give up here rather than possibly ending up going
4252 * through the whole file */
4253 if (avi->abort_buffering) {
4254 avi->abort_buffering = FALSE;
4255 gst_adapter_flush (avi->adapter, 8);
4259 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
4260 GST_FOURCC_ARGS (tag), size);
4262 stream_nr = CHUNKID_TO_STREAMNR (tag);
4264 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
4266 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
4267 stream_nr, GST_FOURCC_ARGS (tag));
4268 avi->offset += 8 + ((size + 1) & ~1);
4269 gst_adapter_flush (avi->adapter, 8 + ((size + 1) & ~1));
4271 avi_stream_context *stream;
4272 GstClockTime next_ts = 0;
4275 gst_adapter_flush (avi->adapter, 8);
4278 buf = gst_adapter_take_buffer (avi->adapter, ((size + 1) & ~1));
4279 /* patch the size */
4280 GST_BUFFER_SIZE (buf) = size;
4281 avi->offset += 8 + ((size + 1) & ~1);
4283 stream = &avi->stream[stream_nr];
4285 /* set delay (if any)
4286 if (stream->strh->init_frames == stream->current_frame &&
4288 stream->delay = next_ts;
4291 stream->current_frame++;
4292 stream->current_byte += size;
4294 /* parsing of corresponding header may have failed */
4295 if (G_UNLIKELY (!stream->pad)) {
4296 GST_WARNING_OBJECT (avi, "no pad for stream ID %" GST_FOURCC_FORMAT,
4297 GST_FOURCC_ARGS (tag));
4298 gst_buffer_unref (buf);
4300 GstClockTime dur_ts = 0;
4302 /* get time of this buffer */
4303 gst_pad_query_position (stream->pad, &format, (gint64 *) & next_ts);
4304 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4307 /* invert the picture if needed */
4308 buf = gst_avi_demux_invert (stream, buf);
4310 gst_pad_query_position (stream->pad, &format, (gint64 *) & dur_ts);
4311 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4314 GST_BUFFER_TIMESTAMP (buf) = next_ts;
4315 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
4316 if (stream->strh->type == GST_RIFF_FCC_vids)
4317 GST_BUFFER_OFFSET (buf) = stream->current_frame - 1;
4319 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4321 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4322 GST_DEBUG_OBJECT (avi,
4323 "Pushing buffer with time=%" GST_TIME_FORMAT
4324 ", offset %" G_GUINT64_FORMAT " and size %d over pad %s",
4325 GST_TIME_ARGS (next_ts), GST_BUFFER_OFFSET (buf), size,
4326 GST_PAD_NAME (stream->pad));
4328 /* update current position in the segment */
4329 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, next_ts);
4331 /* mark discont when pending */
4332 if (G_UNLIKELY (stream->discont)) {
4333 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4334 stream->discont = FALSE;
4336 res = gst_pad_push (stream->pad, buf);
4339 res = gst_avi_demux_combine_flows (avi, stream, res);
4340 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4341 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
4354 GST_DEBUG_OBJECT (avi, "format %s != GST_FORMAT_TIME",
4355 gst_format_get_name (format));
4356 res = GST_FLOW_ERROR;
4362 * Send pending tags.
4365 push_tag_lists (GstAviDemux * avi)
4372 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
4374 for (i = 0; i < avi->num_streams; i++)
4375 if (avi->stream[i].pad && avi->stream[i].taglist) {
4376 GST_DEBUG_OBJECT (avi->stream[i].pad, "Tags: %" GST_PTR_FORMAT,
4377 avi->stream[i].taglist);
4378 gst_element_found_tags_for_pad (GST_ELEMENT (avi), avi->stream[i].pad,
4379 avi->stream[i].taglist);
4380 avi->stream[i].taglist = NULL;
4383 if (avi->globaltags == NULL)
4384 avi->globaltags = gst_tag_list_new ();
4386 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE,
4387 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
4389 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, avi->globaltags);
4390 gst_element_found_tags (GST_ELEMENT (avi), avi->globaltags);
4391 avi->globaltags = NULL;
4392 avi->got_tags = FALSE;
4396 gst_avi_demux_loop (GstPad * pad)
4399 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4401 switch (avi->state) {
4402 case GST_AVI_DEMUX_START:
4403 if (G_UNLIKELY ((res =
4404 gst_avi_demux_stream_init_pull (avi)) != GST_FLOW_OK)) {
4405 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4408 avi->state = GST_AVI_DEMUX_HEADER;
4410 case GST_AVI_DEMUX_HEADER:
4411 if (G_UNLIKELY ((res =
4412 gst_avi_demux_stream_header_pull (avi)) != GST_FLOW_OK)) {
4413 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4416 avi->state = GST_AVI_DEMUX_MOVI;
4418 case GST_AVI_DEMUX_MOVI:
4419 if (G_UNLIKELY (avi->seek_event)) {
4420 gst_avi_demux_push_event (avi, avi->seek_event);
4421 avi->seek_event = NULL;
4423 if (G_UNLIKELY (avi->got_tags)) {
4424 push_tag_lists (avi);
4426 /* process each index entry in turn */
4427 res = gst_avi_demux_stream_data (avi);
4429 /* pause when error */
4430 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4431 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
4436 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
4437 res = GST_FLOW_ERROR;
4441 GST_LOG_OBJECT (avi, "state: %d res:%s", avi->state, gst_flow_get_name (res));
4447 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
4448 avi->segment_running = FALSE;
4449 gst_pad_pause_task (avi->sinkpad);
4451 if (GST_FLOW_IS_FATAL (res) || (res == GST_FLOW_NOT_LINKED)) {
4452 gboolean push_eos = TRUE;
4454 if (res == GST_FLOW_UNEXPECTED) {
4455 /* handle end-of-stream/segment */
4456 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4459 if ((stop = avi->segment.stop) == -1)
4460 stop = avi->segment.duration;
4462 GST_INFO_OBJECT (avi, "sending segment_done");
4464 gst_element_post_message
4466 gst_message_new_segment_done (GST_OBJECT (avi), GST_FORMAT_TIME,
4471 /* for fatal errors we post an error message */
4472 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
4473 (_("Internal data stream error.")),
4474 ("streaming stopped, reason %s", gst_flow_get_name (res)));
4477 GST_INFO_OBJECT (avi, "sending eos");
4478 if (!gst_avi_demux_push_event (avi, gst_event_new_eos ()) &&
4479 (res == GST_FLOW_UNEXPECTED)) {
4480 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
4481 (NULL), ("got eos but no streams (yet)"));
4488 static GstFlowReturn
4489 gst_avi_demux_chain (GstPad * pad, GstBuffer * buf)
4492 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4494 GST_DEBUG ("Store %d bytes in adapter", GST_BUFFER_SIZE (buf));
4495 gst_adapter_push (avi->adapter, buf);
4497 switch (avi->state) {
4498 case GST_AVI_DEMUX_START:
4499 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
4500 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4504 case GST_AVI_DEMUX_HEADER:
4505 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
4506 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4510 case GST_AVI_DEMUX_MOVI:
4511 if (G_UNLIKELY (avi->seek_event)) {
4512 gst_avi_demux_push_event (avi, avi->seek_event);
4513 avi->seek_event = NULL;
4515 if (G_UNLIKELY (avi->got_tags)) {
4516 push_tag_lists (avi);
4518 res = gst_avi_demux_stream_data (avi);
4521 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
4522 ("Illegal internal state"));
4523 res = GST_FLOW_ERROR;
4527 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
4528 gst_flow_get_name (res));
4530 if (G_UNLIKELY (avi->abort_buffering)) {
4531 avi->abort_buffering = FALSE;
4532 res = GST_FLOW_ERROR;
4533 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, NULL, ("unhandled buffer size"));
4540 gst_avi_demux_sink_activate (GstPad * sinkpad)
4542 if (gst_pad_check_pull_range (sinkpad)) {
4543 GST_DEBUG ("going to pull mode");
4544 return gst_pad_activate_pull (sinkpad, TRUE);
4546 GST_DEBUG ("going to push (streaming) mode");
4547 return gst_pad_activate_push (sinkpad, TRUE);
4552 gst_avi_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
4554 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (sinkpad));
4557 avi->segment_running = TRUE;
4558 avi->streaming = FALSE;
4559 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4562 avi->segment_running = FALSE;
4563 return gst_pad_stop_task (sinkpad);
4568 gst_avi_demux_activate_push (GstPad * pad, gboolean active)
4570 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (pad));
4573 GST_DEBUG ("avi: activating push/chain function");
4574 avi->streaming = TRUE;
4576 GST_DEBUG ("avi: deactivating push/chain function");
4582 static GstStateChangeReturn
4583 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
4585 GstStateChangeReturn ret;
4586 GstAviDemux *avi = GST_AVI_DEMUX (element);
4588 switch (transition) {
4589 case GST_STATE_CHANGE_READY_TO_PAUSED:
4590 avi->streaming = FALSE;
4591 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
4597 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4598 if (ret == GST_STATE_CHANGE_FAILURE)
4601 switch (transition) {
4602 case GST_STATE_CHANGE_PAUSED_TO_READY:
4603 gst_avi_demux_reset (avi);