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,
81 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
83 static const GstQueryType *gst_avi_demux_get_src_query_types (GstPad * pad);
84 static gboolean gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query);
85 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
86 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
88 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
89 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
91 static void gst_avi_demux_loop (GstPad * pad);
92 static gboolean gst_avi_demux_sink_activate (GstPad * sinkpad);
93 static gboolean gst_avi_demux_sink_activate_pull (GstPad * sinkpad,
95 static gboolean gst_avi_demux_activate_push (GstPad * pad, gboolean active);
96 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstBuffer * buf);
98 static GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
99 GstStateChange transition);
101 static GstElementClass *parent_class = NULL;
103 /* GObject methods */
106 gst_avi_demux_get_type (void)
108 static GType avi_demux_type = 0;
110 if (!avi_demux_type) {
111 static const GTypeInfo avi_demux_info = {
112 sizeof (GstAviDemuxClass),
113 (GBaseInitFunc) gst_avi_demux_base_init,
115 (GClassInitFunc) gst_avi_demux_class_init,
118 sizeof (GstAviDemux),
120 (GInstanceInitFunc) gst_avi_demux_init,
124 g_type_register_static (GST_TYPE_ELEMENT,
125 "GstAviDemux", &avi_demux_info, 0);
128 return avi_demux_type;
132 gst_avi_demux_base_init (GstAviDemuxClass * klass)
134 static const GstElementDetails gst_avi_demux_details =
135 GST_ELEMENT_DETAILS ("Avi demuxer",
137 "Demultiplex an avi file into audio and video",
138 "Erik Walthinsen <omega@cse.ogi.edu>\n"
139 "Wim Taymans <wim.taymans@chello.be>\n"
140 "Ronald Bultje <rbultje@ronald.bitfreak.net>\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, gst_avi_demux_sink_activate);
189 gst_pad_set_activatepull_function (avi->sinkpad,
190 gst_avi_demux_sink_activate_pull);
191 gst_pad_set_activatepush_function (avi->sinkpad, gst_avi_demux_activate_push);
192 gst_pad_set_chain_function (avi->sinkpad, gst_avi_demux_chain);
193 gst_pad_set_event_function (avi->sinkpad, gst_avi_demux_handle_sink_event);
194 gst_element_add_pad (GST_ELEMENT (avi), avi->sinkpad);
196 avi->adapter = gst_adapter_new ();
198 gst_avi_demux_reset (avi);
202 gst_avi_demux_finalize (GObject * object)
204 GstAviDemux *avi = GST_AVI_DEMUX (object);
206 GST_DEBUG ("AVI: finalize");
208 g_object_unref (avi->adapter);
210 G_OBJECT_CLASS (parent_class)->finalize (object);
214 gst_avi_demux_reset (GstAviDemux * avi)
218 GST_DEBUG ("AVI: reset");
220 for (i = 0; i < avi->num_streams; i++) {
221 g_free (avi->stream[i].strh);
222 g_free (avi->stream[i].strf.data);
223 if (avi->stream[i].name)
224 g_free (avi->stream[i].name);
225 if (avi->stream[i].initdata)
226 gst_buffer_unref (avi->stream[i].initdata);
227 if (avi->stream[i].extradata)
228 gst_buffer_unref (avi->stream[i].extradata);
229 if (avi->stream[i].pad) {
230 gst_pad_set_active (avi->stream[i].pad, FALSE);
231 gst_element_remove_pad (GST_ELEMENT (avi), avi->stream[i].pad);
233 if (avi->stream[i].taglist) {
234 gst_tag_list_free (avi->stream[i].taglist);
235 avi->stream[i].taglist = NULL;
238 memset (&avi->stream, 0, sizeof (avi->stream));
240 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
241 avi->num_streams = 0;
242 avi->num_v_streams = 0;
243 avi->num_a_streams = 0;
244 avi->num_t_streams = 0;
246 avi->state = GST_AVI_DEMUX_START;
249 g_free (avi->index_entries);
250 avi->index_entries = NULL;
252 avi->index_offset = 0;
253 avi->current_entry = 0;
257 if (avi->seek_event) {
258 gst_event_unref (avi->seek_event);
259 avi->seek_event = NULL;
263 gst_tag_list_free (avi->globaltags);
264 avi->globaltags = NULL;
266 avi->got_tags = TRUE; /* we always want to push global tags */
267 avi->have_eos = FALSE;
269 gst_adapter_clear (avi->adapter);
271 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
275 static gst_avi_index_entry *
276 gst_avi_demux_index_last (GstAviDemux * avi, gint stream_nr)
279 gst_avi_index_entry *result = NULL;
281 for (i = avi->index_size - 1; i >= 0; i--) {
282 if (avi->index_entries[i].stream_nr == stream_nr) {
283 result = &avi->index_entries[i];
290 static gst_avi_index_entry *
291 gst_avi_demux_index_next (GstAviDemux * avi, gint stream_nr, gint last,
295 gst_avi_index_entry *result = NULL, *entry;
297 for (i = last + 1; i < avi->index_size; i++) {
298 entry = &avi->index_entries[i];
300 if (entry->stream_nr != stream_nr)
303 if ((entry->flags & flags) == flags) {
311 static gst_avi_index_entry *
312 gst_avi_demux_index_prev (GstAviDemux * avi, gint stream_nr, gint last,
316 gst_avi_index_entry *result = NULL, *entry;
318 for (i = last - 1; i >= 0; i--) {
319 entry = &avi->index_entries[i];
321 if (entry->stream_nr != stream_nr)
324 if ((entry->flags & flags) == flags) {
333 gst_avi_demux_index_entry_search (gst_avi_index_entry * entry, guint64 * time)
335 if (entry->ts < *time)
337 else if (entry->ts > *time)
343 * gst_avi_index_entry:
345 * @stream_nr: stream number
346 * @time: seek time position
348 * Finds the index entry which time is less or equal than the requested time.
350 * Returns: the found index entry or %NULL
352 static gst_avi_index_entry *
353 gst_avi_demux_index_entry_for_time (GstAviDemux * avi,
354 gint stream_nr, guint64 time)
356 gst_avi_index_entry *entry = NULL;
359 GST_LOG_OBJECT (avi, "stream_nr:%d , time:%" GST_TIME_FORMAT,
360 stream_nr, GST_TIME_ARGS (time));
362 entry = gst_util_array_binary_search (avi->index_entries,
364 sizeof (gst_avi_index_entry),
365 (GCompareDataFunc) gst_avi_demux_index_entry_search,
366 GST_SEARCH_MODE_BEFORE, &time, NULL);
368 n = entry - avi->index_entries;
370 entry = &avi->index_entries[0];
372 while (entry->stream_nr != stream_nr && n < avi->index_size - 1) {
374 entry = &avi->index_entries[n];
376 } else if (entry->stream_nr != stream_nr) {
377 while (entry->stream_nr != stream_nr && n > 0) {
379 entry = &avi->index_entries[n];
384 "best at entry %u / ts:%" GST_TIME_FORMAT " / dur:%" GST_TIME_FORMAT
385 " flags:%02x", n, GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (entry->dur),
391 /* GstElement methods */
394 static const GstFormat *
395 gst_avi_demux_get_src_formats (GstPad * pad)
397 avi_stream_context *stream = gst_pad_get_element_private (pad);
399 static const GstFormat src_a_formats[] = {
405 static const GstFormat src_v_formats[] = {
411 return (stream->strh->type == GST_RIFF_FCC_auds ?
412 src_a_formats : src_v_formats);
416 /* assumes stream->strf.auds->av_bps != 0 */
417 static inline GstClockTime
418 avi_stream_convert_bytes_to_time_unchecked (avi_stream_context * stream,
421 return gst_util_uint64_scale (bytes, GST_SECOND, stream->strf.auds->av_bps);
424 /* assumes stream->strh->rate != 0 */
425 static inline GstClockTime
426 avi_stream_convert_frames_to_time_unchecked (avi_stream_context * stream,
429 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
434 gst_avi_demux_src_convert (GstPad * pad,
435 GstFormat src_format,
436 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
438 avi_stream_context *stream = gst_pad_get_element_private (pad);
442 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
443 ", dest_format:%s", gst_format_get_name (src_format), src_value,
444 gst_format_get_name (*dest_format));
446 if (G_UNLIKELY (src_format == *dest_format)) {
447 *dest_value = src_value;
450 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
454 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
455 (src_format == GST_FORMAT_BYTES
456 || *dest_format == GST_FORMAT_BYTES))) {
461 switch (src_format) {
462 case GST_FORMAT_TIME:
463 switch (*dest_format) {
464 case GST_FORMAT_BYTES:
465 *dest_value = gst_util_uint64_scale (src_value,
466 (guint64) stream->strf.auds->av_bps, GST_SECOND);
468 case GST_FORMAT_DEFAULT:
472 *dest_value = gst_util_uint64_scale (src_value, stream->strh->rate,
473 stream->strh->scale * GST_SECOND);
475 /* Attempt to round to nearest integer: if the difference is more
476 * than 0.5 (less than -0.5), it means that gst_util_uint64_scale()
477 * just truncated an integer, while it had to be rounded
479 error = *dest_value * GST_SECOND -
480 src_value * stream->strh->rate / stream->strh->scale;
490 case GST_FORMAT_BYTES:
491 switch (*dest_format) {
492 case GST_FORMAT_TIME:
493 if (stream->strf.auds->av_bps != 0) {
494 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
504 case GST_FORMAT_DEFAULT:
505 switch (*dest_format) {
506 case GST_FORMAT_TIME:
508 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
521 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
522 gst_format_get_name (*dest_format), *dest_value);
526 static const GstQueryType *
527 gst_avi_demux_get_src_query_types (GstPad * pad)
529 static const GstQueryType src_types[] = {
541 gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query)
544 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
546 avi_stream_context *stream = gst_pad_get_element_private (pad);
548 if (!stream->strh || !stream->strf.data)
549 return gst_pad_query_default (pad, query);
551 switch (GST_QUERY_TYPE (query)) {
552 case GST_QUERY_POSITION:{
555 GST_DEBUG ("pos query for stream %d: frames %d, bytes %" G_GUINT64_FORMAT,
556 stream->num, stream->current_frame, stream->current_byte);
558 if (stream->strh->type == GST_RIFF_FCC_auds) {
559 if (stream->is_vbr) {
561 pos = gst_util_uint64_scale ((gint64) stream->current_frame *
562 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
563 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
564 GST_TIME_FORMAT, stream->current_frame, GST_TIME_ARGS (pos));
565 } else if (stream->strf.auds->av_bps != 0) {
567 pos = gst_util_uint64_scale (stream->current_byte, GST_SECOND,
568 (guint64) stream->strf.auds->av_bps);
569 GST_DEBUG_OBJECT (avi,
570 "CBR convert bytes %" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT,
571 stream->current_byte, GST_TIME_ARGS (pos));
572 } else if (stream->total_frames != 0 && stream->total_bytes != 0) {
573 /* calculate timestamps based on percentage of length */
574 guint64 xlen = avi->avih->us_frame *
575 avi->avih->tot_frames * GST_USECOND;
577 if (stream->is_vbr) {
578 pos = gst_util_uint64_scale (xlen, stream->current_frame,
579 stream->total_frames);
580 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
581 GST_TIME_FORMAT, stream->current_frame, GST_TIME_ARGS (pos));
583 pos = gst_util_uint64_scale (xlen, stream->current_byte,
584 stream->total_bytes);
585 GST_DEBUG_OBJECT (avi, "CBR perc convert bytes %" G_GUINT64_FORMAT
586 ", time %" GST_TIME_FORMAT, stream->current_byte,
587 GST_TIME_ARGS (pos));
594 if (stream->strh->rate != 0) {
595 pos = gst_util_uint64_scale ((guint64) stream->current_frame *
596 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
598 pos = stream->current_frame * avi->avih->us_frame * GST_USECOND;
602 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
603 gst_query_set_position (query, GST_FORMAT_TIME, pos);
605 GST_WARNING ("pos query failed");
608 case GST_QUERY_DURATION:
612 if (stream->strh->type != GST_RIFF_FCC_auds &&
613 stream->strh->type != GST_RIFF_FCC_vids) {
618 gst_query_parse_duration (query, &fmt, NULL);
621 case GST_FORMAT_TIME:
622 gst_query_set_duration (query, fmt, stream->duration);
624 case GST_FORMAT_DEFAULT:
627 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
628 stream->total_frames);
630 if (stream->total_frames >= 0)
631 gst_query_set_duration (query, fmt, stream->total_frames);
632 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
633 stream->duration, &fmt, &dur))
634 gst_query_set_duration (query, fmt, dur);
643 case GST_QUERY_SEEKING:{
646 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
647 if (fmt == GST_FORMAT_TIME) {
648 gboolean seekable = TRUE;
650 if (avi->streaming) {
653 if (avi->index_entries == NULL) {
655 /* FIXME: when building index_entried, count keyframes
656 if (!(avi->key_frame_ct > 1))
662 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
663 0, stream->duration);
668 case GST_QUERY_CONVERT:{
669 GstFormat src_fmt, dest_fmt;
670 gint64 src_val, dest_val;
672 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
673 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
675 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
677 res = gst_pad_query_default (pad, query);
681 res = gst_pad_query_default (pad, query);
685 gst_object_unref (avi);
690 static const GstEventMask *
691 gst_avi_demux_get_event_mask (GstPad * pad)
693 static const GstEventMask masks[] = {
694 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
703 gst_avi_demux_handle_sink_event (GstPad * pad, GstEvent * event)
706 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
708 GST_DEBUG_OBJECT (avi,
709 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
711 switch (GST_EVENT_TYPE (event)) {
712 case GST_EVENT_NEWSEGMENT:
713 /* Drop NEWSEGMENT events, new ones are generated later */
714 gst_event_unref (event);
717 res = gst_pad_event_default (pad, event);
721 gst_object_unref (avi);
727 gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event)
730 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
732 GST_DEBUG_OBJECT (avi,
733 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
735 switch (GST_EVENT_TYPE (event)) {
737 /* handle seeking only in pull mode */
738 if (!avi->streaming) {
739 res = gst_avi_demux_handle_seek (avi, pad, event);
740 gst_event_unref (event);
742 res = gst_pad_event_default (pad, event);
746 case GST_EVENT_NAVIGATION:
748 gst_event_unref (event);
751 res = gst_pad_event_default (pad, event);
755 gst_object_unref (avi);
760 /* streaming helper (push) */
763 * gst_avi_demux_peek_chunk_info:
765 * @tag: holder for tag
766 * @size: holder for tag size
768 * Peek next chunk info (tag and size)
770 * Returns: TRUE when one chunk info has been got
773 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
775 const guint8 *data = NULL;
777 if (gst_adapter_available (avi->adapter) < 8) {
781 data = gst_adapter_peek (avi->adapter, 8);
782 *tag = GST_READ_UINT32_LE (data);
783 *size = GST_READ_UINT32_LE (data + 4);
789 * gst_avi_demux_peek_chunk:
791 * @tag: holder for tag
792 * @size: holder for tag size
794 * Peek enough data for one full chunk
796 * Returns: %TRUE when one chunk has been got
799 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
801 guint32 peek_size = 0;
804 if (!gst_avi_demux_peek_chunk_info (avi, tag, size)) {
807 /* FIXME: shouldn't this check go to gst_avi_demux_peek_chunk_info() already */
808 if (!(*size) || (*size) == -1) {
809 GST_INFO ("Invalid chunk size %d for tag %" GST_FOURCC_FORMAT,
810 *size, GST_FOURCC_ARGS (*tag));
813 peek_size = (*size + 1) & ~1;
814 available = gst_adapter_available (avi->adapter);
816 GST_DEBUG ("Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
817 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
819 if (available >= (8 + peek_size)) {
829 * gst_avi_demux_parse_file_header:
830 * @element: caller element (used for errors/debug).
831 * @buf: input data to be used for parsing.
833 * "Open" a RIFF/AVI file. The buffer should be at least 12
834 * bytes long. Takes ownership of @buf.
836 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
837 * Throws an error, caller should error out (fatal).
840 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
844 /* riff_parse posts an error */
845 if (!gst_riff_parse_file_header (element, buf, &doctype))
848 if (doctype != GST_RIFF_RIFF_AVI)
856 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
857 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
858 GST_FOURCC_ARGS (doctype)));
864 * Read AVI file tag when streaming
867 gst_avi_demux_stream_init_push (GstAviDemux * avi)
869 if (gst_adapter_available (avi->adapter) >= 12) {
872 tmp = gst_adapter_take_buffer (avi->adapter, 12);
874 GST_DEBUG ("Parsing avi header");
875 if (!gst_avi_demux_parse_file_header (GST_ELEMENT (avi), tmp)) {
876 return GST_FLOW_ERROR;
878 GST_DEBUG ("header ok");
881 avi->state = GST_AVI_DEMUX_HEADER;
890 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
893 GstBuffer *buf = NULL;
895 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
896 if (res != GST_FLOW_OK)
898 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
908 GST_DEBUG_OBJECT (avi, "error parsing file header");
909 return GST_FLOW_ERROR;
913 /* AVI header handling */
916 * gst_avi_demux_parse_avih:
917 * @element: caller element (used for errors/debug).
918 * @buf: input data to be used for parsing.
919 * @avih: pointer to structure (filled in by function) containing
920 * stream information (such as flags, number of streams, etc.).
922 * Read 'avih' header. Discards buffer after use.
924 * Returns: TRUE on success, FALSE otherwise. Throws an error if
925 * the header is invalid. The caller should error out
929 gst_avi_demux_parse_avih (GstElement * element,
930 GstBuffer * buf, gst_riff_avih ** _avih)
937 if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_avih))
940 avih = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
942 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
943 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
944 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
945 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
946 avih->flags = GUINT32_FROM_LE (avih->flags);
947 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
948 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
949 avih->streams = GUINT32_FROM_LE (avih->streams);
950 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
951 avih->width = GUINT32_FROM_LE (avih->width);
952 avih->height = GUINT32_FROM_LE (avih->height);
953 avih->scale = GUINT32_FROM_LE (avih->scale);
954 avih->rate = GUINT32_FROM_LE (avih->rate);
955 avih->start = GUINT32_FROM_LE (avih->start);
956 avih->length = GUINT32_FROM_LE (avih->length);
960 GST_INFO_OBJECT (element, "avih tag found:");
961 GST_INFO_OBJECT (element, " us_frame %u", avih->us_frame);
962 GST_INFO_OBJECT (element, " max_bps %u", avih->max_bps);
963 GST_INFO_OBJECT (element, " pad_gran %u", avih->pad_gran);
964 GST_INFO_OBJECT (element, " flags 0x%08x", avih->flags);
965 GST_INFO_OBJECT (element, " tot_frames %u", avih->tot_frames);
966 GST_INFO_OBJECT (element, " init_frames %u", avih->init_frames);
967 GST_INFO_OBJECT (element, " streams %u", avih->streams);
968 GST_INFO_OBJECT (element, " bufsize %u", avih->bufsize);
969 GST_INFO_OBJECT (element, " width %u", avih->width);
970 GST_INFO_OBJECT (element, " height %u", avih->height);
971 GST_INFO_OBJECT (element, " scale %u", avih->scale);
972 GST_INFO_OBJECT (element, " rate %u", avih->rate);
973 GST_INFO_OBJECT (element, " start %u", avih->start);
974 GST_INFO_OBJECT (element, " length %u", avih->length);
977 gst_buffer_unref (buf);
984 GST_ELEMENT_ERROR (element, STREAM, DEMUX, (NULL), ("No buffer"));
989 GST_ELEMENT_ERROR (element, STREAM, DEMUX, (NULL),
990 ("Too small avih (%d available, %d needed)",
991 GST_BUFFER_SIZE (buf), (int) sizeof (gst_riff_avih)));
992 gst_buffer_unref (buf);
998 * gst_avi_demux_parse_superindex:
999 * @avi: caller element (used for debugging/errors).
1000 * @buf: input data to use for parsing.
1001 * @locations: locations in the file (byte-offsets) that contain
1002 * the actual indexes (see get_avi_demux_parse_subindex()).
1003 * The array ends with GST_BUFFER_OFFSET_NONE.
1005 * Reads superindex (openDML-2 spec stuff) from the provided data.
1007 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
1008 * on error, but they are not fatal.
1011 gst_avi_demux_parse_superindex (GstAviDemux * avi,
1012 GstBuffer * buf, guint64 ** _indexes)
1022 size = buf ? GST_BUFFER_SIZE (buf) : 0;
1026 data = GST_BUFFER_DATA (buf);
1028 /* check type of index. The opendml2 specs state that
1029 * there should be 4 dwords per array entry. Type can be
1030 * either frame or field (and we don't care). */
1031 if (GST_READ_UINT16_LE (data) != 4 ||
1032 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
1033 GST_WARNING_OBJECT (avi,
1034 "Superindex for stream has unexpected "
1035 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1036 GST_READ_UINT16_LE (data), data[2], data[3]);
1037 bpe = GST_READ_UINT16_LE (data) * 4;
1039 num = GST_READ_UINT32_LE (&data[4]);
1041 indexes = g_new (guint64, num + 1);
1042 for (i = 0; i < num; i++) {
1043 if (size < 24 + bpe * (i + 1))
1045 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
1047 indexes[i] = GST_BUFFER_OFFSET_NONE;
1048 *_indexes = indexes;
1050 gst_buffer_unref (buf);
1057 GST_ERROR_OBJECT (avi,
1058 "Not enough data to parse superindex (%d available, 24 needed)", size);
1060 gst_buffer_unref (buf);
1066 * gst_avi_demux_parse_subindex:
1068 * @buf: input data to use for parsing.
1069 * @stream: stream context.
1070 * @entries_list: a list (returned by the function) containing all the
1071 * indexes parsed in this specific subindex. The first
1072 * entry is also a pointer to allocated memory that needs
1073 * to be free´ed. May be NULL if no supported indexes were
1076 * Reads superindex (openDML-2 spec stuff) from the provided data.
1077 * The buffer will be discarded after use.
1079 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1080 * throw an error, caller should bail out asap.
1083 gst_avi_demux_parse_subindex (GstAviDemux * avi,
1084 GstBuffer * buf, avi_stream_context * stream, GList ** _entries_list)
1086 guint8 *data = GST_BUFFER_DATA (buf);
1090 gst_avi_index_entry *entries, *entry;
1091 GList *entries_list = NULL;
1094 *_entries_list = NULL;
1096 size = buf ? GST_BUFFER_SIZE (buf) : 0;
1102 /* We don't support index-data yet */
1104 goto not_implemented;
1106 /* check type of index. The opendml2 specs state that
1107 * there should be 4 dwords per array entry. Type can be
1108 * either frame or field (and we don't care). */
1109 bpe = (data[2] & 0x01) ? 12 : 8;
1110 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1111 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1112 GST_WARNING_OBJECT (avi,
1113 "Superindex for stream %d has unexpected "
1114 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1115 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1116 bpe = GST_READ_UINT16_LE (data) * 4;
1118 num = GST_READ_UINT32_LE (&data[4]);
1119 baseoff = GST_READ_UINT64_LE (&data[12]);
1121 /* If there's nothing, just return ! */
1125 if (!(entries = g_try_new (gst_avi_index_entry, num)))
1128 for (i = 0; i < num; i++) {
1131 entry = &entries[i];
1133 if (size < 24 + bpe * (i + 1))
1137 entry->offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1138 entry->size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1140 (entry->size & 0x80000000) ? 0 : GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
1141 entry->size &= ~0x80000000;
1142 entry->index_nr = i;
1143 entry->stream_nr = stream->num;
1145 /* stream duration unknown, now we can calculate it */
1146 if (stream->idx_duration == -1)
1147 stream->idx_duration = 0;
1150 entry->ts = stream->idx_duration;
1151 if (stream->is_vbr) {
1152 /* VBR stream next timestamp */
1153 if (stream->strh->type == GST_RIFF_FCC_auds) {
1154 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
1155 stream->total_blocks + 1);
1157 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
1158 stream->total_frames + 1);
1161 /* CBR get next timestamp */
1162 next_ts = avi_stream_convert_bytes_to_time_unchecked (stream,
1163 stream->total_bytes + entry->size);
1165 /* duration is next - current */
1166 entry->dur = next_ts - entry->ts;
1168 /* stream position */
1169 entry->bytes_before = stream->total_bytes;
1170 entry->frames_before = stream->total_frames;
1172 stream->total_bytes += entry->size;
1173 stream->total_frames++;
1174 if (stream->strh->type == GST_RIFF_FCC_auds) {
1175 if (stream->strf.auds->blockalign > 0)
1176 stream->total_blocks +=
1177 (entry->size + stream->strf.auds->blockalign -
1178 1) / stream->strf.auds->blockalign;
1180 stream->total_blocks++;
1182 stream->idx_duration = next_ts;
1184 entries_list = g_list_prepend (entries_list, entry);
1187 GST_LOG_OBJECT (avi, "Read %d index entries", i);
1189 gst_buffer_unref (buf);
1192 *_entries_list = g_list_reverse (entries_list);
1202 GST_ERROR_OBJECT (avi,
1203 "Not enough data to parse subindex (%d available, 24 needed)", size);
1205 gst_buffer_unref (buf);
1206 return TRUE; /* continue */
1210 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1211 ("Subindex-is-data is not implemented"));
1212 gst_buffer_unref (buf);
1217 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1218 ("Cannot allocate memory for %u*%u=%u bytes",
1219 (guint) sizeof (gst_avi_index_entry), num,
1220 (guint) sizeof (gst_avi_index_entry) * num));
1221 gst_buffer_unref (buf);
1228 * Read AVI index when streaming
1231 gst_avi_demux_read_subindexes_push (GstAviDemux * avi,
1232 GList ** index, GList ** alloc_list)
1235 guint32 tag = 0, size;
1236 GstBuffer *buf = NULL;
1239 GST_DEBUG_OBJECT (avi, "gst_avi_demux_read_subindexes_push for %d streams",
1242 for (n = 0; n < avi->num_streams; n++) {
1243 avi_stream_context *stream = &avi->stream[n];
1245 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1246 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1248 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1249 '0' + stream->num % 10)) &&
1250 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1251 '0' + stream->num % 10, 'i', 'x'))) {
1252 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1253 GST_FOURCC_ARGS (tag));
1257 avi->offset += 8 + ((size + 1) & ~1);
1259 buf = gst_buffer_new ();
1260 GST_BUFFER_DATA (buf) = gst_adapter_take (avi->adapter, size);
1261 GST_BUFFER_SIZE (buf) = size;
1263 if (!gst_avi_demux_parse_subindex (avi, buf, stream, &list))
1266 GST_DEBUG_OBJECT (avi, " adding %d entries", g_list_length (list));
1267 *alloc_list = g_list_append (*alloc_list, list->data);
1268 *index = g_list_concat (*index, list);
1272 g_free (stream->indexes);
1273 stream->indexes = NULL;
1275 GST_DEBUG_OBJECT (avi, "index %s", ((*index) ? "!= 0" : "== 0"));
1283 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi,
1284 GList ** index, GList ** alloc_list)
1291 GST_DEBUG_OBJECT (avi, "gst_avi_demux_read_subindexes_pull for %d streams",
1294 for (n = 0; n < avi->num_streams; n++) {
1295 avi_stream_context *stream = &avi->stream[n];
1297 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1298 if (gst_riff_read_chunk (GST_ELEMENT (avi), avi->sinkpad,
1299 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1301 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1302 '0' + stream->num % 10)) &&
1303 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1304 '0' + stream->num % 10, 'i', 'x'))) {
1305 /* Some ODML files (created by god knows what muxer) have a ##ix format
1306 * instead of the 'official' ix##. They are still valid though. */
1307 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1308 GST_FOURCC_ARGS (tag));
1309 gst_buffer_unref (buf);
1313 if (!gst_avi_demux_parse_subindex (avi, buf, stream, &list))
1316 GST_DEBUG_OBJECT (avi, " adding %5d entries, total %2d %5d",
1317 g_list_length (list), g_list_length (*alloc_list),
1318 g_list_length (*index));
1319 *alloc_list = g_list_append (*alloc_list, list->data);
1320 *index = g_list_concat (*index, list);
1324 g_free (stream->indexes);
1325 stream->indexes = NULL;
1327 GST_DEBUG_OBJECT (avi, "index %s", ((*index) ? "!= 0" : "== 0"));
1331 * gst_avi_demux_riff_parse_vprp:
1332 * @element: caller element (used for debugging/error).
1333 * @buf: input data to be used for parsing, stripped from header.
1334 * @vprp: a pointer (returned by this function) to a filled-in vprp
1335 * structure. Caller should free it.
1337 * Parses a video stream´s vprp. This function takes ownership of @buf.
1339 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1340 * should be skipped on error, but it is not fatal.
1343 gst_avi_demux_riff_parse_vprp (GstElement * element,
1344 GstBuffer * buf, gst_riff_vprp ** _vprp)
1346 gst_riff_vprp *vprp;
1349 g_return_val_if_fail (buf != NULL, FALSE);
1350 g_return_val_if_fail (_vprp != NULL, FALSE);
1352 if (GST_BUFFER_SIZE (buf) < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1355 vprp = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1357 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1358 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1359 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1360 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1361 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1362 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1363 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1364 vprp->width = GUINT32_FROM_LE (vprp->width);
1365 vprp->height = GUINT32_FROM_LE (vprp->height);
1366 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1370 /* calculate fields based on size */
1371 k = (GST_BUFFER_SIZE (buf) - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) /
1373 if (vprp->fields > k) {
1374 GST_WARNING_OBJECT (element,
1375 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1378 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1379 GST_WARNING_OBJECT (element,
1380 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1381 GST_RIFF_VPRP_VIDEO_FIELDS);
1382 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1384 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1385 for (k = 0; k < vprp->fields; k++) {
1386 gst_riff_vprp_video_field_desc *fd;
1388 fd = &vprp->field_info[k];
1389 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1390 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1391 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1392 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1393 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1394 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1395 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1396 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1401 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1402 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1403 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1404 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1405 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1406 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1407 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1408 vprp->aspect & 0xffff);
1409 GST_INFO_OBJECT (element, " width %d", vprp->width);
1410 GST_INFO_OBJECT (element, " height %d", vprp->height);
1411 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1412 for (k = 0; k < vprp->fields; k++) {
1413 gst_riff_vprp_video_field_desc *fd;
1415 fd = &(vprp->field_info[k]);
1416 GST_INFO_OBJECT (element, " field %u description:", k);
1417 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1418 fd->compressed_bm_height);
1419 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1420 fd->compressed_bm_width);
1421 GST_INFO_OBJECT (element, " valid_bm_height %d",
1422 fd->valid_bm_height);
1423 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1424 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1425 fd->valid_bm_x_offset);
1426 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1427 fd->valid_bm_y_offset);
1428 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1429 fd->video_x_t_offset);
1430 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1433 gst_buffer_unref (buf);
1442 GST_ERROR_OBJECT (element,
1443 "Too small vprp (%d available, at least %d needed)",
1444 GST_BUFFER_SIZE (buf),
1445 (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1446 gst_buffer_unref (buf);
1452 * gst_avi_demux_parse_stream:
1453 * @avi: calling element (used for debugging/errors).
1454 * @buf: input buffer used to parse the stream.
1456 * Parses all subchunks in a strl chunk (which defines a single
1457 * stream). Discards the buffer after use. This function will
1458 * increment the stream counter internally.
1460 * Returns: whether the stream was identified successfully.
1461 * Errors are not fatal. It does indicate the stream
1465 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
1467 avi_stream_context *stream = &avi->stream[avi->num_streams];
1468 GstElementClass *klass;
1469 GstPadTemplate *templ;
1470 GstBuffer *sub = NULL;
1473 gchar *codec_name = NULL, *padname = NULL;
1474 const gchar *tag_name;
1475 GstCaps *caps = NULL;
1477 GstElement *element;
1478 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
1479 gst_riff_vprp *vprp = NULL;
1481 element = GST_ELEMENT_CAST (avi);
1483 GST_DEBUG_OBJECT (avi, "Parsing stream");
1485 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
1486 /* sub can be NULL if the chunk is empty */
1488 case GST_RIFF_TAG_strh:
1490 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
1493 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
1494 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
1499 case GST_RIFF_TAG_strf:
1501 gboolean res = FALSE;
1504 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
1508 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
1511 switch (stream->strh->type) {
1512 case GST_RIFF_FCC_vids:
1513 stream->is_vbr = TRUE;
1514 res = gst_riff_parse_strf_vids (element, sub,
1515 &stream->strf.vids, &stream->extradata);
1516 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
1518 case GST_RIFF_FCC_auds:
1519 stream->is_vbr = (stream->strh->samplesize == 0)
1520 && stream->strh->scale > 1;
1522 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
1523 &stream->extradata);
1524 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
1525 stream->is_vbr, res);
1527 case GST_RIFF_FCC_iavs:
1528 stream->is_vbr = TRUE;
1529 res = gst_riff_parse_strf_iavs (element, sub,
1530 &stream->strf.iavs, &stream->extradata);
1531 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
1533 case GST_RIFF_FCC_txts:
1534 /* nothing to parse here */
1535 stream->is_vbr = (stream->strh->samplesize == 0)
1536 && (stream->strh->scale > 1);
1540 GST_ERROR_OBJECT (avi,
1541 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
1542 GST_FOURCC_ARGS (stream->strh->type));
1550 case GST_RIFF_TAG_vprp:
1553 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
1557 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
1561 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
1565 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
1566 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
1567 /* not considered fatal */
1574 case GST_RIFF_TAG_strd:
1575 if (stream->initdata)
1576 gst_buffer_unref (stream->initdata);
1577 stream->initdata = sub;
1579 case GST_RIFF_TAG_strn:
1580 g_free (stream->name);
1583 g_strndup ((gchar *) GST_BUFFER_DATA (sub),
1584 (gsize) GST_BUFFER_SIZE (sub));
1585 gst_buffer_unref (sub);
1588 stream->name = g_strdup ("");
1590 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
1593 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
1594 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
1595 '0' + avi->num_streams % 10)) {
1596 g_free (stream->indexes);
1597 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
1598 stream->superindex = TRUE;
1601 GST_WARNING_OBJECT (avi,
1602 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
1603 GST_FOURCC_ARGS (tag));
1605 case GST_RIFF_TAG_JUNK:
1607 gst_buffer_unref (sub);
1615 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
1620 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
1624 /* get class to figure out the template */
1625 klass = GST_ELEMENT_GET_CLASS (avi);
1627 /* we now have all info, let´s set up a pad and a caps and be done */
1628 /* create stream name + pad */
1629 switch (stream->strh->type) {
1630 case GST_RIFF_FCC_vids:{
1633 fourcc = (stream->strf.vids->compression) ?
1634 stream->strf.vids->compression : stream->strh->fcc_handler;
1635 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
1636 templ = gst_element_class_get_pad_template (klass, "video_%02d");
1637 caps = gst_riff_create_video_caps (fourcc, stream->strh,
1638 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
1640 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
1641 GST_TYPE_FOURCC, fourcc, NULL);
1642 } else if (got_vprp && vprp) {
1643 guint32 aspect_n, aspect_d;
1646 aspect_n = vprp->aspect >> 16;
1647 aspect_d = vprp->aspect & 0xffff;
1648 /* calculate the pixel aspect ratio using w/h and aspect ratio */
1649 n = aspect_n * stream->strf.vids->height;
1650 d = aspect_d * stream->strf.vids->width;
1652 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1654 /* very local, not needed elsewhere */
1658 tag_name = GST_TAG_VIDEO_CODEC;
1659 avi->num_v_streams++;
1662 case GST_RIFF_FCC_auds:{
1663 padname = g_strdup_printf ("audio_%02d", avi->num_a_streams);
1664 templ = gst_element_class_get_pad_template (klass, "audio_%02d");
1665 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
1666 stream->strh, stream->strf.auds, stream->extradata,
1667 stream->initdata, &codec_name);
1669 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
1670 G_TYPE_INT, stream->strf.auds->format, NULL);
1672 tag_name = GST_TAG_AUDIO_CODEC;
1673 avi->num_a_streams++;
1676 case GST_RIFF_FCC_iavs:{
1677 guint32 fourcc = stream->strh->fcc_handler;
1679 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
1680 templ = gst_element_class_get_pad_template (klass, "video_%02d");
1681 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
1682 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
1684 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
1685 GST_TYPE_FOURCC, fourcc, NULL);
1687 tag_name = GST_TAG_VIDEO_CODEC;
1688 avi->num_v_streams++;
1691 case GST_RIFF_FCC_txts:{
1692 padname = g_strdup_printf ("subtitle_%02d", avi->num_t_streams);
1693 templ = gst_element_class_get_pad_template (klass, "subtitle_%02d");
1694 caps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
1696 avi->num_t_streams++;
1700 g_assert_not_reached ();
1703 /* no caps means no stream */
1705 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
1709 GST_DEBUG_OBJECT (element, "codec-name=%s",
1710 (codec_name ? codec_name : "NULL"));
1711 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
1713 /* set proper settings and add it */
1715 gst_object_unref (stream->pad);
1716 pad = stream->pad = gst_pad_new_from_template (templ, padname);
1717 stream->last_flow = GST_FLOW_OK;
1718 stream->discont = TRUE;
1719 stream->idx_duration = GST_CLOCK_TIME_NONE;
1720 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1721 stream->duration = GST_CLOCK_TIME_NONE;
1724 gst_pad_use_fixed_caps (pad);
1726 gst_pad_set_formats_function (pad,
1727 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
1728 gst_pad_set_event_mask_function (pad,
1729 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
1731 gst_pad_set_event_function (pad,
1732 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
1733 gst_pad_set_query_type_function (pad,
1734 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_query_types));
1735 gst_pad_set_query_function (pad,
1736 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
1738 gst_pad_set_convert_function (pad,
1739 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
1742 stream->num = avi->num_streams;
1743 stream->total_bytes = 0;
1744 stream->total_frames = 0;
1745 stream->total_blocks = 0;
1746 stream->current_frame = 0;
1747 stream->current_byte = 0;
1748 gst_pad_set_element_private (pad, stream);
1750 gst_pad_set_caps (pad, caps);
1751 gst_pad_set_active (pad, TRUE);
1752 gst_element_add_pad (GST_ELEMENT (avi), pad);
1753 GST_LOG_OBJECT (element, "Added pad %s with caps %" GST_PTR_FORMAT,
1754 GST_PAD_NAME (pad), caps);
1755 gst_caps_unref (caps);
1759 if (!stream->taglist)
1760 stream->taglist = gst_tag_list_new ();
1762 avi->got_tags = TRUE;
1764 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
1766 g_free (codec_name);
1774 /* unref any mem that may be in use */
1776 gst_buffer_unref (buf);
1778 gst_buffer_unref (sub);
1780 g_free (codec_name);
1781 g_free (stream->strh);
1782 g_free (stream->strf.data);
1783 g_free (stream->name);
1784 g_free (stream->indexes);
1785 if (stream->initdata)
1786 gst_buffer_unref (stream->initdata);
1787 if (stream->extradata)
1788 gst_buffer_unref (stream->extradata);
1789 memset (stream, 0, sizeof (avi_stream_context));
1796 * gst_avi_demux_parse_odml:
1797 * @avi: calling element (used for debug/error).
1798 * @buf: input buffer to be used for parsing.
1800 * Read an openDML-2.0 extension header. Fills in the frame number
1801 * in the avi demuxer object when reading succeeds.
1804 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
1808 GstBuffer *sub = NULL;
1810 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
1813 case GST_RIFF_TAG_dmlh:{
1814 gst_riff_dmlh dmlh, *_dmlh;
1817 /* sub == NULL is possible and means an empty buffer */
1818 size = sub ? GST_BUFFER_SIZE (sub) : 0;
1821 if (size < sizeof (gst_riff_dmlh)) {
1822 GST_ERROR_OBJECT (avi,
1823 "DMLH entry is too small (%d bytes, %d needed)",
1824 size, (int) sizeof (gst_riff_dmlh));
1827 _dmlh = (gst_riff_dmlh *) GST_BUFFER_DATA (sub);
1828 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
1830 GST_INFO_OBJECT (avi, "dmlh tag found:");
1831 GST_INFO_OBJECT (avi, " totalframes: %u", dmlh.totalframes);
1833 avi->avih->tot_frames = dmlh.totalframes;
1838 GST_WARNING_OBJECT (avi,
1839 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
1840 GST_FOURCC_ARGS (tag));
1842 case GST_RIFF_TAG_JUNK:
1844 /* skip and move to next chunk */
1846 gst_buffer_unref (sub);
1853 gst_buffer_unref (buf);
1857 * Sort helper for index entries that sorts by index time.
1858 * If times are equal we sort by stream number.
1861 sort (gst_avi_index_entry * a, gst_avi_index_entry * b)
1865 else if (a->ts < b->ts)
1868 return a->stream_nr - b->stream_nr;
1872 * gst_avi_demux_parse_index:
1873 * @avi: calling element (used for debugging/errors).
1874 * @buf: buffer containing the full index.
1875 * @entries_list: list (returned by this function) containing the index
1876 * entries parsed from the buffer. The first in the list
1877 * is also a pointer to the allocated data and should be
1878 * free'ed at some point.
1880 * Read index entries from the provided buffer. Takes ownership of @buf.
1883 gst_avi_demux_parse_index (GstAviDemux * avi,
1884 GstBuffer * buf, GList ** _entries_list)
1886 guint64 pos_before = avi->offset;
1887 gst_avi_index_entry *entries = NULL;
1889 GList *entries_list = NULL;
1892 #ifndef GST_DISABLE_GST_DEBUG
1893 gulong _nr_keyframes = 0;
1896 if (!buf || !GST_BUFFER_SIZE (buf)) {
1897 *_entries_list = NULL;
1898 GST_DEBUG ("empty index");
1900 gst_buffer_unref (buf);
1904 data = GST_BUFFER_DATA (buf);
1905 num = GST_BUFFER_SIZE (buf) / sizeof (gst_riff_index_entry);
1906 if (!(entries = g_try_new (gst_avi_index_entry, num)))
1909 GST_INFO ("Parsing index, nr_entries = %6d", num);
1911 for (i = 0, n = 0; i < num; i++) {
1913 gst_riff_index_entry entry, *_entry;
1914 avi_stream_context *stream;
1916 gst_avi_index_entry *target;
1918 _entry = &((gst_riff_index_entry *) data)[i];
1919 entry.id = GST_READ_UINT32_LE (&_entry->id);
1920 entry.offset = GST_READ_UINT32_LE (&_entry->offset);
1921 entry.flags = GST_READ_UINT32_LE (&_entry->flags);
1922 entry.size = GST_READ_UINT32_LE (&_entry->size);
1923 target = &entries[n];
1925 if (entry.id == GST_RIFF_rec || entry.id == 0 ||
1926 (entry.offset == 0 && n > 0))
1929 stream_nr = CHUNKID_TO_STREAMNR (entry.id);
1930 if (stream_nr >= avi->num_streams) {
1931 GST_WARNING_OBJECT (avi,
1932 "Index entry %d has invalid stream nr %d", i, stream_nr);
1935 target->stream_nr = stream_nr;
1936 stream = &avi->stream[stream_nr];
1938 if (!stream->strh) {
1939 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
1943 target->index_nr = i;
1945 (entry.flags & GST_RIFF_IF_KEYFRAME) ? GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME
1947 target->size = entry.size;
1948 target->offset = entry.offset + 8;
1950 /* figure out if the index is 0 based or relative to the MOVI start */
1952 if (target->offset < pos_before)
1953 avi->index_offset = pos_before + 8;
1955 avi->index_offset = 0;
1956 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
1959 if (stream->strh->type == GST_RIFF_FCC_auds) {
1960 /* all audio frames are keyframes */
1961 target->flags |= GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
1963 #ifndef GST_DISABLE_GST_DEBUG
1964 if (target->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)
1968 /* stream duration unknown, now we can calculate it */
1969 if (stream->idx_duration == -1)
1970 stream->idx_duration = 0;
1973 target->ts = stream->idx_duration;
1974 if (stream->is_vbr) {
1975 /* VBR stream next timestamp */
1976 if (stream->strh->type == GST_RIFF_FCC_auds) {
1977 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
1978 stream->total_blocks + 1);
1980 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
1981 stream->total_frames + 1);
1984 /* constant rate stream */
1985 next_ts = avi_stream_convert_bytes_to_time_unchecked (stream,
1986 stream->total_bytes + target->size);
1988 /* duration is next - current */
1989 target->dur = next_ts - target->ts;
1991 /* stream position */
1992 target->bytes_before = stream->total_bytes;
1993 target->frames_before = stream->total_frames;
1995 stream->total_bytes += target->size;
1996 stream->total_frames++;
1997 if (stream->strh->type == GST_RIFF_FCC_auds) {
1998 if (stream->strf.auds->blockalign > 0)
1999 stream->total_blocks +=
2000 (target->size + stream->strf.auds->blockalign -
2001 1) / stream->strf.auds->blockalign;
2003 stream->total_blocks++;
2005 stream->idx_duration = next_ts;
2007 GST_LOG_OBJECT (avi,
2008 "Adding index entry %d (%6u), flags %02x, stream %d, size %u "
2009 ", offset %" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT ", dur %"
2011 target->index_nr, stream->total_frames - 1, target->flags,
2012 target->stream_nr, target->size, target->offset,
2013 GST_TIME_ARGS (target->ts), GST_TIME_ARGS (target->dur));
2014 entries_list = g_list_prepend (entries_list, target);
2019 GST_INFO ("Parsed index, %6d entries, %5ld keyframes, entry size = %2d, "
2020 "total size = %10d", num, _nr_keyframes,
2021 (gint) sizeof (gst_avi_index_entry),
2022 (gint) (num * sizeof (gst_avi_index_entry)));
2024 gst_buffer_unref (buf);
2027 *_entries_list = g_list_reverse (entries_list);
2036 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2037 ("Cannot allocate memory for %u*%u=%u bytes",
2038 (guint) sizeof (gst_avi_index_entry), num,
2039 (guint) sizeof (gst_avi_index_entry) * num));
2040 gst_buffer_unref (buf);
2045 * gst_avi_demux_stream_index:
2046 * @avi: avi demuxer object.
2047 * @index: list of index entries, returned by this function.
2048 * @alloc_list: list of allocated data, returned by this function.
2050 * Seeks to index and reads it.
2053 gst_avi_demux_stream_index (GstAviDemux * avi,
2054 GList ** index, GList ** alloc_list)
2057 guint64 offset = avi->offset;
2063 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2068 /* get chunk information */
2069 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2070 if (res != GST_FLOW_OK)
2072 else if (GST_BUFFER_SIZE (buf) < 8)
2075 /* check tag first before blindy trying to read 'size' bytes */
2076 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2077 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2078 if (tag == GST_RIFF_TAG_LIST) {
2079 /* this is the movi tag */
2080 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2081 (8 + ((size + 1) & ~1)));
2082 offset += 8 + ((size + 1) & ~1);
2083 gst_buffer_unref (buf);
2084 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2085 if (res != GST_FLOW_OK)
2087 else if (GST_BUFFER_SIZE (buf) < 8)
2089 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2090 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2093 if (tag != GST_RIFF_TAG_idx1)
2098 gst_buffer_unref (buf);
2100 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2102 /* read chunk, advance offset */
2103 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2104 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2107 GST_INFO ("will parse index chunk size %u for tag %"
2108 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2110 gst_avi_demux_parse_index (avi, buf, index);
2112 *alloc_list = g_list_append (*alloc_list, (*index)->data);
2114 /* debug our indexes */
2115 for (i = 0; i < avi->num_streams; i++) {
2116 avi_stream_context *stream;
2118 stream = &avi->stream[i];
2119 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2120 i, stream->total_frames, stream->total_bytes);
2127 GST_DEBUG_OBJECT (avi,
2128 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2133 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2134 gst_buffer_unref (buf);
2139 GST_WARNING_OBJECT (avi,
2140 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2141 GST_FOURCC_ARGS (tag));
2142 gst_buffer_unref (buf);
2147 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2148 gst_buffer_unref (buf);
2155 * Sync to next data chunk.
2158 gst_avi_demux_skip (GstAviDemux * avi, gboolean prevent_eos)
2160 GstRiffRead *riff = GST_RIFF_READ (avi);
2163 guint64 pos, length;
2167 pos = gst_bytestream_tell (riff->bs);
2168 length = gst_bytestream_length (riff->bs);
2170 if (pos + 8 > length)
2173 if (gst_bytestream_peek_bytes (riff->bs, &data, 8) != 8)
2176 size = GST_READ_UINT32_LE (&data[4]);
2180 /* Note, we're going to skip which might involve seeks. Therefore,
2181 * we need 1 byte more! */
2182 if (pos + 8 + size >= length)
2186 return gst_riff_read_skip (riff);
2190 gst_avi_demux_sync (GstAviDemux * avi, guint32 * ret_tag, gboolean prevent_eos)
2192 GstRiffRead *riff = GST_RIFF_READ (avi);
2194 guint64 length = gst_bytestream_length (riff->bs);
2196 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2199 /* peek first (for the end of this 'list/movi' section) */
2200 if (!(tag = gst_riff_peek_tag (riff, &avi->level_up)))
2203 /* if we're at top-level, we didn't read the 'movi'
2204 * list tag yet. This can also be 'AVIX' in case of
2205 * openDML-2.0 AVI files. Lastly, it might be idx1,
2206 * in which case we skip it so we come at EOS. */
2208 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2211 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2215 case GST_RIFF_TAG_LIST:
2216 if (!(tag = gst_riff_peek_list (riff)))
2220 case GST_RIFF_LIST_AVIX:
2221 if (!gst_riff_read_list (riff, &tag))
2225 case GST_RIFF_LIST_movi:
2226 if (!gst_riff_read_list (riff, &tag))
2234 GST_WARNING ("Unknown list %" GST_FOURCC_FORMAT " before AVI data",
2235 GST_FOURCC_ARGS (tag));
2238 case GST_RIFF_TAG_JUNK:
2239 if (!gst_avi_demux_skip (avi, prevent_eos))
2246 if ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
2247 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9') {
2252 case GST_RIFF_TAG_idx1:
2253 case GST_RIFF_TAG_JUNK:
2254 if (!gst_avi_demux_skip (avi, prevent_eos)) {
2261 /* And then, we get the data */
2262 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2265 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2268 /* Support for rec-list files */
2270 case GST_RIFF_TAG_LIST:
2271 if (!(tag = gst_riff_peek_list (riff)))
2273 if (tag == GST_RIFF_rec) {
2274 /* Simply skip the list */
2275 if (!gst_riff_read_list (riff, &tag))
2277 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2282 case GST_RIFF_TAG_JUNK:
2283 gst_avi_demux_skip (avi, prevent_eos);
2295 * gst_avi_demux_peek_tag:
2297 * Returns the tag and size of the next chunk
2299 static GstFlowReturn
2300 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2303 GstFlowReturn res = GST_FLOW_OK;
2304 GstBuffer *buf = NULL;
2307 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2308 if (res != GST_FLOW_OK)
2311 bufsize = GST_BUFFER_SIZE (buf);
2315 *tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2316 *size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2318 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2319 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2320 *size, offset + 8, offset + 8 + (gint64) * size);
2322 gst_buffer_unref (buf);
2329 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2334 GST_DEBUG_OBJECT (avi, "got %d bytes which is <> 8 bytes", bufsize);
2335 res = GST_FLOW_ERROR;
2341 * gst_avi_demux_next_data_buffer:
2343 * Returns the offset and size of the next buffer
2344 * Position is the position of the buffer (after tag and size)
2346 static GstFlowReturn
2347 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2348 guint32 * tag, guint * size)
2350 guint64 off = *offset;
2355 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2356 if (res != GST_FLOW_OK)
2358 if (*tag == GST_RIFF_TAG_LIST)
2359 off += 8 + 4; /* skip tag + size + subtag */
2371 * gst_avi_demux_stream_scan:
2372 * @avi: calling element (used for debugging/errors).
2373 * @index: list of index entries, returned by this function.
2374 * @alloc_list: list of allocated data, returned by this function.
2376 * Scan the file for all chunks to "create" a new index.
2377 * Return value indicates if we can continue reading the stream. It
2378 * does not say anything about whether we created an index.
2383 gst_avi_demux_stream_scan (GstAviDemux * avi,
2384 GList ** index, GList ** alloc_list)
2387 gst_avi_index_entry *entry, *entries = NULL;
2388 avi_stream_context *stream;
2390 guint64 pos = avi->offset;
2395 guint index_size = 0;
2398 * - implement non-seekable source support.
2400 GST_DEBUG_OBJECT (avi,
2401 "Creating index %s existing index, starting at offset %" G_GUINT64_FORMAT,
2402 ((*index) ? "with" : "without"), pos);
2404 format = GST_FORMAT_BYTES;
2405 if (!gst_pad_query_peer_duration (avi->sinkpad, &format, &tmplength))
2411 entry = g_list_last (*index)->data;
2412 pos = entry->offset + avi->index_offset + entry->size;
2413 if (entry->size & 1)
2416 if (pos >= length) {
2417 GST_LOG_OBJECT (avi, "Complete index, we're done");
2421 GST_LOG_OBJECT (avi, "Incomplete index, seeking to last valid entry @ %"
2422 G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT " (%"
2423 G_GUINT64_FORMAT "+%u)", pos, length, entry->offset, entry->size);
2430 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
2431 if (G_UNLIKELY (res != GST_FLOW_OK))
2434 /* check valid stream */
2435 stream_nr = CHUNKID_TO_STREAMNR (tag);
2436 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2437 GST_WARNING_OBJECT (avi,
2438 "Index entry has invalid stream nr %d", stream_nr);
2442 stream = &avi->stream[stream_nr];
2443 if (G_UNLIKELY (stream->pad == NULL)) {
2444 GST_WARNING_OBJECT (avi,
2445 "Stream %d does not have an output pad, can't create new index",
2451 if (G_UNLIKELY (index_size % 1024 == 0)) {
2452 entries = g_new (gst_avi_index_entry, 1024);
2453 *alloc_list = g_list_prepend (*alloc_list, entries);
2455 entry = &entries[index_size % 1024];
2457 entry->index_nr = index_size++;
2458 entry->stream_nr = stream_nr;
2459 entry->flags = GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
2460 entry->offset = pos - avi->index_offset;
2463 /* timestamps, get timestamps of two consecutive frames to calculate
2464 * timestamp and duration. */
2465 format = GST_FORMAT_TIME;
2466 if (stream->is_vbr) {
2468 entry->ts = avi_stream_convert_frames_to_time_unchecked (stream,
2469 stream->total_frames);
2470 entry->dur = avi_stream_convert_frames_to_time_unchecked (stream,
2471 stream->total_frames + 1);
2473 /* constant rate stream */
2474 entry->ts = avi_stream_convert_bytes_to_time_unchecked (stream,
2475 stream->total_bytes);
2476 entry->dur = avi_stream_convert_bytes_to_time_unchecked (stream,
2477 stream->total_bytes + entry->size);
2479 entry->dur -= entry->ts;
2481 /* stream position */
2482 entry->bytes_before = stream->total_bytes;
2483 stream->total_bytes += entry->size;
2484 entry->frames_before = stream->total_frames;
2485 stream->total_frames++;
2486 stream->idx_duration = entry->ts + entry->dur;
2488 list = g_list_prepend (list, entry);
2489 GST_DEBUG_OBJECT (avi, "Added index entry %d (in stream: %d), offset %"
2490 G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT " for stream %d",
2491 index_size - 1, entry->frames_before, entry->offset,
2492 GST_TIME_ARGS (entry->ts), entry->stream_nr);
2495 /* update position */
2496 pos += GST_ROUND_UP_2 (size);
2497 if (G_UNLIKELY (pos > length)) {
2498 GST_WARNING_OBJECT (avi,
2499 "Stopping index lookup since we are further than EOF");
2504 /* FIXME: why is this disabled */
2506 while (gst_avi_demux_sync (avi, &tag, TRUE)) {
2507 guint stream_nr = CHUNKID_TO_STREAMNR (tag);
2509 GstFormat format = GST_FORMAT_TIME;
2511 if (stream_nr >= avi->num_streams)
2513 stream = &avi->stream[stream_nr];
2515 /* get chunk size */
2516 if (gst_bytestream_peek_bytes (riff->bs, &data, 8) != 8)
2520 entry->index_nr = index_size++;
2521 entry->stream_nr = stream_nr;
2522 entry->flags = GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
2523 entry->offset = gst_bytestream_tell (riff->bs) + 8 - avi->index_offset;
2524 entry->size = GST_READ_UINT32_LE (&data[4]);
2527 if (stream->is_vbr) {
2529 entry->ts = avi_stream_convert_frames_to_time_unchecked (stream,
2530 stream->total_frames);
2531 entry->dur = avi_stream_convert_frames_to_time_unchecked (stream,
2532 stream->total_frames + 1);
2534 /* constant rate stream */
2535 entry->ts = avi_stream_convert_bytes_to_time_unchecked (stream,
2536 stream->total_bytes);
2537 entry->dur = avi_stream_convert_bytes_to_time_unchecked (stream,
2538 stream->total_bytes + entry->size);
2540 entry->dur -= entry->ts;
2542 /* stream position */
2543 entry->bytes_before = stream->total_bytes;
2544 stream->total_bytes += entry->size;
2545 entry->frames_before = stream->total_frames;
2546 stream->total_frames++;
2548 list = g_list_prepend (list, entry);
2549 GST_DEBUG_OBJECT (avi, "Added index entry %d (in stream: %d), offset %"
2550 G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT " for stream %d",
2551 index_size - 1, entry->frames_before, entry->offset,
2552 GST_TIME_ARGS (entry->ts), entry->stream_nr);
2555 if (!gst_avi_demux_skip (avi, TRUE))
2559 if (!(event = gst_riff_read_seek (riff, pos))) {
2563 gst_event_unref (event);
2567 GST_DEBUG_OBJECT (avi, "index created, %d items", index_size);
2569 *index = g_list_concat (*index, g_list_reverse (list));
2575 * gst_avi_demux_massage_index:
2576 * @avi: calling element (used for debugging/errors).
2578 * We're going to go over each entry in the index and finetune
2579 * some things we don't like about AVI. For example, a single
2580 * chunk might be too long. Also, individual streams might be
2581 * out-of-sync. In the first case, we cut the chunk in several
2582 * smaller pieces. In the second case, we re-order chunk reading
2583 * order. The end result should be a smoother playing AVI.
2586 gst_avi_demux_massage_index (GstAviDemux * avi,
2587 GList * list, GList * alloc_list)
2589 gst_avi_index_entry *entry;
2590 avi_stream_context *stream;
2593 gint64 delay = G_GINT64_CONSTANT (0);
2595 GST_LOG_OBJECT (avi, "Starting index massage, nr_entries = %d",
2596 list ? g_list_length (list) : 0);
2599 #ifndef GST_DISABLE_GST_DEBUG
2600 guint num_added_total = 0;
2601 guint num_per_stream[GST_AVI_DEMUX_MAX_STREAMS] = { 0, };
2603 GST_LOG_OBJECT (avi,
2604 "I'm now going to cut large chunks into smaller pieces");
2606 /* cut chunks in small (seekable) pieces
2607 * FIXME: this should be a property where a value of
2608 * GST_CLOCK_TIME_NONE would disable the chunking
2610 #define MAX_DURATION (GST_SECOND / 2)
2611 for (i = 0; i < avi->num_streams; i++) {
2612 /* only chop streams that have exactly *one* chunk */
2613 if (avi->stream[i].total_frames != 1)
2616 for (node = list; node != NULL; node = node->next) {
2619 if (entry->stream_nr != i)
2622 /* check for max duration of a single buffer. I suppose that
2623 * the allocation of index entries could be improved. */
2624 stream = &avi->stream[entry->stream_nr];
2625 if (entry->dur > MAX_DURATION
2626 && stream->strh->type == GST_RIFF_FCC_auds) {
2628 gst_avi_index_entry *entries;
2629 guint old_size, num_added;
2632 /* cut in 1/10th of a second */
2633 ideal_size = stream->strf.auds->av_bps / 10;
2635 /* ensure chunk size is multiple of blockalign */
2636 if (stream->strf.auds->blockalign > 1)
2637 ideal_size -= ideal_size % stream->strf.auds->blockalign;
2640 old_size = entry->size;
2641 num_added = (entry->size - 1) / ideal_size;
2642 avi->index_size += num_added;
2643 entries = g_malloc (sizeof (gst_avi_index_entry) * num_added);
2644 alloc_list = g_list_prepend (alloc_list, entries);
2645 for (node2 = node->next; node2 != NULL; node2 = node2->next) {
2646 gst_avi_index_entry *entry2 = node2->data;
2648 entry2->index_nr += num_added;
2649 if (entry2->stream_nr == entry->stream_nr)
2650 entry2->frames_before += num_added;
2653 /* new sized index chunks */
2654 for (i = 0; i < num_added + 1; i++) {
2655 gst_avi_index_entry *entry2;
2660 entry2 = &entries[i - 1];
2661 list = g_list_insert_before (list, node->next, entry2);
2664 memcpy (entry2, entry, sizeof (gst_avi_index_entry));
2667 if (old_size >= ideal_size) {
2668 entry2->size = ideal_size;
2669 old_size -= ideal_size;
2671 entry2->size = old_size;
2674 entry2->dur = GST_SECOND * entry2->size / stream->strf.auds->av_bps;
2677 entry2->ts += entry->dur;
2678 entry2->offset += entry->size;
2679 entry2->bytes_before += entry->size;
2680 entry2->frames_before++;
2683 #ifndef GST_DISABLE_GST_DEBUG
2684 num_added_total += num_added;
2689 #ifndef GST_DISABLE_GST_DEBUG
2690 if (num_added_total)
2691 GST_LOG ("added %u new index entries", num_added_total);
2694 GST_LOG_OBJECT (avi, "I'm now going to reorder the index entries for time");
2696 /* re-order for time */
2697 list = g_list_sort (list, (GCompareFunc) sort);
2699 /* make a continous array out of the list */
2700 avi->index_size = g_list_length (list);
2701 avi->index_entries = g_try_new (gst_avi_index_entry, avi->index_size);
2702 if (!avi->index_entries)
2705 entry = (gst_avi_index_entry *) (list->data);
2708 GST_LOG_OBJECT (avi,
2709 "Building index array, nr_entries = %d (time offset = %"
2710 GST_TIME_FORMAT, avi->index_size, GST_TIME_ARGS (delay));
2712 for (i = 0, node = list; node != NULL; node = node->next, i++) {
2714 entry->index_nr = i;
2716 memcpy (&avi->index_entries[i], entry, sizeof (gst_avi_index_entry));
2717 #ifndef GST_DISABLE_GST_DEBUG
2718 num_per_stream[entry->stream_nr]++;
2721 GST_LOG_OBJECT (avi, "Sorted index entry %3d for stream %d of size %6u"
2722 " at offset %7" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT
2723 " dur %" GST_TIME_FORMAT,
2724 avi->index_entries[i].index_nr, entry->stream_nr, entry->size,
2725 entry->offset, GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (entry->dur));
2728 for (i = 0; i < avi->num_streams; i++) {
2729 stream = &avi->stream[i];
2730 stream->idx_duration -= delay;
2733 #ifndef GST_DISABLE_GST_DEBUG
2735 gchar str[GST_AVI_DEMUX_MAX_STREAMS * (1 + 6 + 2)];
2738 for (i = 0; i < avi->num_streams; i++) {
2739 if (!avi->stream[i].pad)
2741 pad_name = GST_OBJECT_NAME (avi->stream[i].pad);
2742 sprintf (&str[i * (1 + 6 + 2)], " %6u %c", num_per_stream[i],
2745 GST_LOG_OBJECT (avi, "indizies per stream:%20s", str);
2749 GST_LOG_OBJECT (avi, "Freeing original index list");
2750 /* all the node->data in list point to alloc_list chunks */
2755 g_list_foreach (alloc_list, (GFunc) g_free, NULL);
2756 g_list_free (alloc_list);
2758 #ifndef GST_DISABLE_GST_DEBUG
2759 for (i = 0; i < avi->num_streams; i++) {
2760 GST_LOG_OBJECT (avi, "Stream %d, %d frames, %8" G_GUINT64_FORMAT " bytes",
2761 i, avi->stream[i].total_frames, avi->stream[i].total_bytes);
2765 GST_LOG_OBJECT (avi, "Index massaging done");
2770 GST_WARNING_OBJECT (avi, "Out of memory for %" G_GSIZE_FORMAT " bytes",
2771 sizeof (gst_avi_index_entry) * avi->index_size);
2776 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
2778 gst_avi_index_entry *entry;
2782 total = GST_CLOCK_TIME_NONE;
2784 /* all streams start at a timestamp 0 */
2785 for (stream = 0; stream < avi->num_streams; stream++) {
2786 GstClockTime duration, hduration;
2787 avi_stream_context *streamc = &avi->stream[stream];
2788 gst_riff_strh *strh = streamc->strh;
2792 /* get header duration */
2793 hduration = gst_util_uint64_scale ((guint64) strh->length *
2794 strh->scale, GST_SECOND, (guint64) strh->rate);
2796 GST_INFO ("Stream %d duration according to header: %" GST_TIME_FORMAT,
2797 stream, GST_TIME_ARGS (hduration));
2800 hduration = GST_CLOCK_TIME_NONE;
2802 /* set duration for the stream */
2803 streamc->hdr_duration = hduration;
2805 /* get last index entry to get duration */
2806 if ((entry = gst_avi_demux_index_last (avi, stream)))
2807 duration = entry->ts + entry->dur;
2809 duration = GST_CLOCK_TIME_NONE;
2811 streamc->idx_duration = duration;
2813 /* now pick a good duration */
2814 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2815 /* index gave valid duration, use that */
2816 GST_INFO ("Stream %d duration according to index: %" GST_TIME_FORMAT,
2817 stream, GST_TIME_ARGS (duration));
2819 /* fall back to header info to calculate a duration */
2820 duration = hduration;
2822 /* set duration for the stream */
2823 streamc->duration = duration;
2825 /* find total duration */
2826 if (total == GST_CLOCK_TIME_NONE || duration > total)
2830 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
2831 /* now update the duration for those streams where we had none */
2832 for (stream = 0; stream < avi->num_streams; stream++) {
2833 avi_stream_context *streamc = &avi->stream[stream];
2835 if (!GST_CLOCK_TIME_IS_VALID (streamc->duration)
2836 || streamc->duration == 0) {
2837 streamc->duration = total;
2839 GST_INFO ("Stream %d duration according to total: %" GST_TIME_FORMAT,
2840 stream, GST_TIME_ARGS (total));
2845 /* and set the total duration in the segment. */
2846 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
2847 GST_TIME_ARGS (total));
2849 gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
2853 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
2855 gboolean result = FALSE;
2858 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
2859 GST_EVENT_TYPE_NAME (event), avi->num_streams);
2861 if (avi->num_streams) {
2862 for (i = 0; i < avi->num_streams; i++) {
2863 avi_stream_context *stream = &avi->stream[i];
2866 if (gst_pad_push_event (stream->pad, gst_event_ref (event)))
2871 gst_event_unref (event);
2876 * Read AVI headers when streaming
2878 static GstFlowReturn
2879 gst_avi_demux_stream_header_push (GstAviDemux * avi)
2881 GstFlowReturn ret = GST_FLOW_OK;
2886 GstBuffer *buf = NULL, *sub = NULL;
2890 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
2892 switch (avi->header_state) {
2893 case GST_AVI_DEMUX_HEADER_TAG_LIST:
2894 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2895 avi->offset += 8 + ((size + 1) & ~1);
2896 if (tag != GST_RIFF_TAG_LIST)
2897 goto header_no_list;
2899 gst_adapter_flush (avi->adapter, 8);
2900 /* Find the 'hdrl' LIST tag */
2901 GST_DEBUG ("Reading %d bytes", size);
2902 buf = gst_adapter_take_buffer (avi->adapter, size);
2904 if (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl)
2905 goto header_no_hdrl;
2907 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
2909 /* the hdrl starts with a 'avih' header */
2910 if (!gst_riff_parse_chunk (GST_ELEMENT (avi), buf, &offset, &tag, &sub))
2911 goto header_no_avih;
2913 if (tag != GST_RIFF_TAG_avih)
2914 goto header_no_avih;
2916 if (!gst_avi_demux_parse_avih (GST_ELEMENT (avi), sub, &avi->avih))
2917 goto header_wrong_avih;
2919 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
2921 /* now, read the elements from the header until the end */
2922 while (gst_riff_parse_chunk (GST_ELEMENT (avi), buf, &offset, &tag,
2924 /* sub can be NULL on empty tags */
2929 case GST_RIFF_TAG_LIST:
2930 if (GST_BUFFER_SIZE (sub) < 4)
2933 switch (GST_READ_UINT32_LE (GST_BUFFER_DATA (sub))) {
2934 case GST_RIFF_LIST_strl:
2935 if (!(gst_avi_demux_parse_stream (avi, sub))) {
2936 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
2937 ("failed to parse stream, ignoring"));
2941 case GST_RIFF_LIST_odml:
2942 gst_avi_demux_parse_odml (avi, sub);
2945 GST_WARNING_OBJECT (avi,
2946 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
2947 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA
2950 case GST_RIFF_TAG_JUNK:
2955 GST_WARNING_OBJECT (avi,
2956 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
2957 offset, GST_FOURCC_ARGS (tag));
2959 case GST_RIFF_TAG_JUNK:
2961 /* move to next chunk */
2962 gst_buffer_unref (sub);
2967 gst_buffer_unref (buf);
2968 GST_DEBUG ("elements parsed");
2970 /* check parsed streams */
2971 if (avi->num_streams == 0) {
2973 } else if (avi->num_streams != avi->avih->streams) {
2974 GST_WARNING_OBJECT (avi,
2975 "Stream header mentioned %d streams, but %d available",
2976 avi->avih->streams, avi->num_streams);
2978 GST_DEBUG ("Get junk and info next");
2979 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
2981 /* Need more data */
2985 case GST_AVI_DEMUX_HEADER_INFO:
2986 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
2988 if (gst_adapter_available (avi->adapter) < 12)
2991 data = gst_adapter_peek (avi->adapter, 12);
2992 tag = GST_READ_UINT32_LE (data);
2993 size = GST_READ_UINT32_LE (data + 4);
2994 ltag = GST_READ_UINT32_LE (data + 8);
2996 if (tag == GST_RIFF_TAG_LIST) {
2998 case GST_RIFF_LIST_movi:
2999 gst_adapter_flush (avi->adapter, 12);
3002 case GST_RIFF_LIST_INFO:
3003 GST_DEBUG ("Found INFO chunk");
3004 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3006 gst_adapter_flush (avi->adapter, 12);
3007 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
3008 gst_riff_parse_info (GST_ELEMENT (avi), buf, &avi->globaltags);
3009 gst_buffer_unref (buf);
3011 avi->offset += ((size + 1) & ~1) - 4;
3013 /* Need more data */
3018 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3019 avi->offset += 8 + ((size + 1) & ~1);
3020 gst_adapter_flush (avi->adapter, 8 + ((size + 1) & ~1));
3021 // ??? goto iterate; ???
3023 /* Need more data */
3029 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3030 avi->offset += 8 + ((size + 1) & ~1);
3031 gst_adapter_flush (avi->adapter, 8 + ((size + 1) & ~1));
3034 /* Need more data */
3041 GST_WARNING ("unhandled header state: %d", avi->header_state);
3046 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3047 avi->num_streams, avi->stream[0].indexes);
3049 GST_DEBUG ("Found movi chunk. Starting to stream data");
3050 avi->state = GST_AVI_DEMUX_MOVI;
3053 /*GList *index = NULL, *alloc = NULL; */
3055 /* ######################## this need to be integrated with the state */
3056 /* create or read stream index (for seeking) */
3057 if (avi->stream[0].indexes != NULL) {
3058 gst_avi_demux_read_subindexes_push (avi, &index, &alloc);
3061 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX) {
3062 gst_avi_demux_stream_index (avi, &index, &alloc);
3064 /* some indexes are incomplete, continue streaming from there */
3066 gst_avi_demux_stream_scan (avi, &index, &alloc);
3069 /* this is a fatal error */
3073 if (!gst_avi_demux_massage_index (avi, index, alloc))
3076 gst_avi_demux_calculate_durations_from_index (avi);
3077 /* ######################## */
3080 /* create initial NEWSEGMENT event */
3081 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3082 stop = avi->segment.duration;
3084 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3086 if (avi->seek_event)
3087 gst_event_unref (avi->seek_event);
3088 avi->seek_event = gst_event_new_new_segment
3089 (FALSE, avi->segment.rate, GST_FORMAT_TIME,
3090 avi->segment.start, stop, avi->segment.start);
3092 /* at this point we know all the streams and we can signal the no more
3094 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3095 gst_element_no_more_pads (GST_ELEMENT (avi));
3102 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3103 return GST_FLOW_ERROR;
3107 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3108 ("Invalid AVI header (no LIST at start): %"
3109 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3110 return GST_FLOW_ERROR;
3114 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3115 ("Invalid AVI header (no hdrl at start): %"
3116 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3117 gst_buffer_unref (buf);
3118 return GST_FLOW_ERROR;
3122 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3123 ("Invalid AVI header (no avih at start): %"
3124 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3126 gst_buffer_unref (sub);
3128 gst_buffer_unref (buf);
3129 return GST_FLOW_ERROR;
3133 gst_buffer_unref (buf);
3134 return GST_FLOW_ERROR;
3139 * Read full AVI headers.
3141 static GstFlowReturn
3142 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
3145 GstBuffer *buf, *sub = NULL;
3147 GList *index = NULL, *alloc = NULL;
3150 GstElement *element = GST_ELEMENT_CAST (avi);
3152 /* the header consists of a 'hdrl' LIST tag */
3153 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3154 if (res != GST_FLOW_OK)
3155 goto pull_range_failed;
3156 else if (tag != GST_RIFF_TAG_LIST)
3158 else if (GST_BUFFER_SIZE (buf) < 4)
3161 GST_DEBUG_OBJECT (avi, "parsing headers");
3163 /* Find the 'hdrl' LIST tag */
3164 while (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl) {
3165 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
3166 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf))));
3169 gst_buffer_unref (buf);
3171 /* read new chunk */
3172 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3173 if (res != GST_FLOW_OK)
3174 goto pull_range_failed;
3175 else if (tag != GST_RIFF_TAG_LIST)
3177 else if (GST_BUFFER_SIZE (buf) < 4)
3181 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
3183 /* the hdrl starts with a 'avih' header */
3184 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
3186 else if (tag != GST_RIFF_TAG_avih)
3188 else if (!gst_avi_demux_parse_avih (element, sub, &avi->avih))
3191 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3193 /* now, read the elements from the header until the end */
3194 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
3195 /* sub can be NULL on empty tags */
3200 case GST_RIFF_TAG_LIST:
3205 if (GST_BUFFER_SIZE (sub) < 4)
3208 data = GST_BUFFER_DATA (sub);
3209 fourcc = GST_READ_UINT32_LE (data);
3212 case GST_RIFF_LIST_strl:
3213 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3214 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3215 ("faile to parse stream, ignoring"));
3219 case GST_RIFF_LIST_odml:
3220 gst_avi_demux_parse_odml (avi, sub);
3223 GST_WARNING_OBJECT (avi,
3224 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3225 GST_FOURCC_ARGS (fourcc));
3226 GST_MEMDUMP_OBJECT (avi, "Unknown list", GST_BUFFER_DATA (sub),
3227 GST_BUFFER_SIZE (sub));
3229 case GST_RIFF_TAG_JUNK:
3235 GST_WARNING_OBJECT (avi,
3236 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
3237 GST_FOURCC_ARGS (tag), offset);
3238 GST_MEMDUMP_OBJECT (avi, "Unknown tag", GST_BUFFER_DATA (sub),
3239 GST_BUFFER_SIZE (sub));
3241 case GST_RIFF_TAG_JUNK:
3244 gst_buffer_unref (sub);
3249 gst_buffer_unref (buf);
3250 GST_DEBUG ("elements parsed");
3252 /* check parsed streams */
3253 if (avi->num_streams == 0)
3255 else if (avi->num_streams != avi->avih->streams) {
3256 GST_WARNING_OBJECT (avi,
3257 "Stream header mentioned %d streams, but %d available",
3258 avi->avih->streams, avi->num_streams);
3261 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
3262 G_GUINT64_FORMAT, avi->offset);
3264 /* Now, find the data (i.e. skip all junk between header and data) */
3269 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
3270 if (res != GST_FLOW_OK) {
3271 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
3272 goto pull_range_failed;
3273 } else if (GST_BUFFER_SIZE (buf) < 12) {
3274 GST_DEBUG_OBJECT (avi, "got %d bytes which is less than 12 bytes",
3275 GST_BUFFER_SIZE (buf));
3276 gst_buffer_unref (buf);
3277 return GST_FLOW_ERROR;
3280 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
3281 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
3282 ltag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 8);
3284 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
3285 GST_FOURCC_ARGS (tag), size);
3286 GST_MEMDUMP ("Tag content", GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
3287 gst_buffer_unref (buf);
3290 case GST_RIFF_TAG_LIST:{
3292 case GST_RIFF_LIST_movi:
3293 GST_DEBUG_OBJECT (avi,
3294 "Reached the 'movi' tag, we're done with skipping");
3296 case GST_RIFF_LIST_INFO:
3298 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3300 if (res != GST_FLOW_OK) {
3301 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3302 goto pull_range_failed;
3304 GST_DEBUG ("got size %u", GST_BUFFER_SIZE (buf));
3306 sub = gst_buffer_create_sub (buf, 4, GST_BUFFER_SIZE (buf) - 4);
3307 gst_riff_parse_info (element, sub, &avi->globaltags);
3309 gst_buffer_unref (sub);
3312 gst_buffer_unref (buf);
3313 /* gst_riff_read_chunk() has already advanced avi->offset */
3316 GST_WARNING_OBJECT (avi,
3317 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3318 GST_FOURCC_ARGS (ltag));
3319 avi->offset += 8 + ((size + 1) & ~1);
3325 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3326 GST_FOURCC_ARGS (tag));
3328 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3329 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3330 avi->offset += 8 + ((size + 1) & ~1);
3336 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3337 avi->num_streams, avi->stream[0].indexes);
3339 /* create or read stream index (for seeking) */
3340 if (avi->stream[0].indexes != NULL) {
3341 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3342 gst_avi_demux_read_subindexes_pull (avi, &index, &alloc);
3345 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX) {
3346 gst_avi_demux_stream_index (avi, &index, &alloc);
3348 /* some indexes are incomplete, continue streaming from there */
3350 gst_avi_demux_stream_scan (avi, &index, &alloc);
3353 /* this is a fatal error */
3357 if (!gst_avi_demux_massage_index (avi, index, alloc))
3360 gst_avi_demux_calculate_durations_from_index (avi);
3362 /* create initial NEWSEGMENT event */
3363 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3364 stop = avi->segment.duration;
3366 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3368 if (avi->seek_event)
3369 gst_event_unref (avi->seek_event);
3370 avi->seek_event = gst_event_new_new_segment
3371 (FALSE, avi->segment.rate, GST_FORMAT_TIME,
3372 avi->segment.start, stop, avi->segment.start);
3374 /* at this point we know all the streams and we can signal the no more
3376 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3377 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3384 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3385 ("Invalid AVI header (no LIST at start): %"
3386 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3387 gst_buffer_unref (buf);
3388 return GST_FLOW_ERROR;
3392 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3393 ("Invalid AVI header (no hdrl at start): %"
3394 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3395 gst_buffer_unref (buf);
3396 return GST_FLOW_ERROR;
3400 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3401 ("Invalid AVI header (no avih at start): %"
3402 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3404 gst_buffer_unref (sub);
3405 gst_buffer_unref (buf);
3406 return GST_FLOW_ERROR;
3410 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3411 ("Invalid AVI header (cannot parse avih at start)"));
3413 gst_buffer_unref (sub);
3414 gst_buffer_unref (buf);
3415 return GST_FLOW_ERROR;
3419 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3420 return GST_FLOW_ERROR;
3424 GST_WARNING ("file without or too big index");
3425 g_list_free (index);
3426 g_list_foreach (alloc, (GFunc) g_free, NULL);
3427 g_list_free (alloc);
3429 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3430 ("Could not get/create index"));
3431 return GST_FLOW_ERROR;
3435 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3436 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3437 return GST_FLOW_ERROR;
3442 * Do the actual seeking.
3445 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
3447 GstClockTime seek_time;
3449 gst_avi_index_entry *entry, *kentry;
3452 seek_time = segment->last_stop;
3453 keyframe = !!(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
3455 /* FIXME: if we seek in an openDML file, we will have multiple
3456 * primary levels. Seeking in between those will cause havoc. */
3458 /* save old position so we can see if we must mark a discont. */
3459 old_entry = avi->current_entry;
3461 /* get the entry for the requested position, which is always in last_stop.
3462 * we search the index entry for stream 0, since all entries are sorted by
3463 * time and stream we automagically are positioned for the other streams as
3464 * well. FIXME, this code assumes the main stream with keyframes is stream 0,
3465 * which is mostly correct... */
3466 if (!(entry = gst_avi_demux_index_entry_for_time (avi, 0, seek_time)))
3469 GST_DEBUG_OBJECT (avi,
3470 "Got requested entry %d [stream:%d / ts:%" GST_TIME_FORMAT
3471 " / duration:%" GST_TIME_FORMAT "]", entry->index_nr,
3472 entry->stream_nr, GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (entry->dur));
3474 /* check if we are already on a keyframe */
3475 if (!(entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)) {
3476 /* now go to the previous keyframe, this is where we should start
3478 if (!(kentry = gst_avi_demux_index_prev (avi, 0, entry->index_nr,
3479 GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME))) {
3483 /* we were on a keyframe */
3487 GST_DEBUG_OBJECT (avi,
3488 "Got keyframe entry %d [stream:%d / ts:%" GST_TIME_FORMAT
3489 " / duration:%" GST_TIME_FORMAT "]", kentry->index_nr,
3490 entry->stream_nr, GST_TIME_ARGS (kentry->ts),
3491 GST_TIME_ARGS (kentry->dur));
3493 /* we must start decoding at the keyframe */
3494 avi->current_entry = kentry->index_nr;
3496 if (segment->rate < 0.0) {
3497 gst_avi_index_entry *next_keyframe;
3499 /* Because we don't know the frame order we need to push from the prev keyframe
3500 * to the next keyframe. If there is a smart decoder downstream he will notice
3501 * that there are too many encoded frames send and return UNEXPECTED when there
3502 * are enough decoded frames to fill the segment.
3505 gst_avi_demux_index_next (avi, 0, kentry->index_nr,
3506 GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME);
3508 next_keyframe = gst_avi_demux_index_last (avi, 0);
3510 avi->reverse_start_index = kentry->index_nr;
3511 avi->reverse_stop_index = next_keyframe->index_nr;
3513 GST_DEBUG_OBJECT (avi, "reverse seek: start idx (%d) and stop idx (%d)",
3514 avi->reverse_start_index, avi->reverse_stop_index);
3518 /* when seeking to a keyframe, we update the result seek time
3519 * to the time of the keyframe. */
3520 seek_time = avi->index_entries[avi->current_entry].ts;
3524 /* if we changed position, mark a DISCONT on all streams */
3525 if (avi->current_entry != old_entry) {
3528 for (i = 0; i < avi->num_streams; i++) {
3529 avi->stream[i].discont = TRUE;
3533 GST_DEBUG_OBJECT (avi, "seek: %" GST_TIME_FORMAT
3534 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
3536 /* the seek time is also the last_stop and stream time */
3537 segment->last_stop = seek_time;
3538 segment->time = seek_time;
3544 /* we could not find an entry for the given time */
3545 GST_WARNING_OBJECT (avi,
3546 "Couldn't find AviIndexEntry for time:%" GST_TIME_FORMAT,
3547 GST_TIME_ARGS (seek_time));
3548 if (avi->current_entry >= avi->index_size && avi->index_size > 0)
3549 avi->current_entry = avi->index_size - 1;
3556 * Handle seek event.
3559 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
3564 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
3568 GstSegment seeksegment = { 0, };
3571 GST_DEBUG_OBJECT (avi, "doing seek with event");
3573 gst_event_parse_seek (event, &rate, &format, &flags,
3574 &cur_type, &cur, &stop_type, &stop);
3576 /* we have to have a format as the segment format. Try to convert
3578 if (format != GST_FORMAT_TIME) {
3579 GstFormat fmt = GST_FORMAT_TIME;
3580 gboolean res = TRUE;
3582 if (cur_type != GST_SEEK_TYPE_NONE)
3583 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
3584 if (res && stop_type != GST_SEEK_TYPE_NONE)
3585 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
3591 GST_DEBUG_OBJECT (avi,
3592 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
3593 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
3594 /* FIXME: can we do anything with rate!=1.0 */
3596 GST_DEBUG_OBJECT (avi, "doing seek without event");
3601 /* save flush flag */
3602 flush = flags & GST_SEEK_FLAG_FLUSH;
3605 GstEvent *event = gst_event_new_flush_start ();
3607 /* for a flushing seek, we send a flush_start on all pads. This will
3608 * eventually stop streaming with a WRONG_STATE. We can thus eventually
3609 * take the STREAM_LOCK. */
3610 GST_DEBUG_OBJECT (avi, "sending flush start");
3611 gst_avi_demux_push_event (avi, gst_event_ref (event));
3612 gst_pad_push_event (avi->sinkpad, event);
3614 /* a non-flushing seek, we PAUSE the task so that we can take the
3616 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
3617 gst_pad_pause_task (avi->sinkpad);
3620 /* wait for streaming to stop */
3621 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
3622 GST_PAD_STREAM_LOCK (avi->sinkpad);
3624 /* copy segment, we need this because we still need the old
3625 * segment when we close the current segment. */
3626 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
3629 GST_DEBUG_OBJECT (avi, "configuring seek");
3630 gst_segment_set_seek (&seeksegment, rate, format, flags,
3631 cur_type, cur, stop_type, stop, &update);
3634 /* do the seek, seeksegment.last_stop contains the new position, this
3635 * actually never fails. */
3636 gst_avi_demux_do_seek (avi, &seeksegment);
3641 GST_DEBUG_OBJECT (avi, "sending flush stop");
3642 gst_avi_demux_push_event (avi, gst_event_new_flush_stop ());
3643 gst_pad_push_event (avi->sinkpad, gst_event_new_flush_stop ());
3644 /* reset the last flow and mark discont, FLUSH is always DISCONT */
3645 for (i = 0; i < avi->num_streams; i++) {
3646 avi->stream[i].last_flow = GST_FLOW_OK;
3647 avi->stream[i].discont = TRUE;
3649 } else if (avi->segment_running) {
3652 /* we are running the current segment and doing a non-flushing seek,
3653 * close the segment first based on the last_stop. */
3654 GST_DEBUG_OBJECT (avi, "closing running segment %" G_GINT64_FORMAT
3655 " to %" G_GINT64_FORMAT, avi->segment.start, avi->segment.last_stop);
3656 seg = gst_event_new_new_segment (TRUE,
3657 avi->segment.rate, avi->segment.format,
3658 avi->segment.start, avi->segment.last_stop, avi->segment.time);
3659 gst_avi_demux_push_event (avi, seg);
3662 /* now update the real segment info */
3663 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
3665 /* post the SEGMENT_START message when we do segmented playback */
3666 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3667 gst_element_post_message (GST_ELEMENT (avi),
3668 gst_message_new_segment_start (GST_OBJECT (avi),
3669 avi->segment.format, avi->segment.last_stop));
3672 /* prepare for streaming again */
3673 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3674 stop = avi->segment.duration;
3676 /* queue the segment event for the streaming thread. */
3677 if (avi->seek_event)
3678 gst_event_unref (avi->seek_event);
3679 if (avi->segment.rate > 0.0) {
3680 avi->seek_event = gst_event_new_new_segment (FALSE,
3681 avi->segment.rate, avi->segment.format,
3682 avi->segment.last_stop, stop, avi->segment.time);
3684 avi->seek_event = gst_event_new_new_segment (FALSE,
3685 avi->segment.rate, avi->segment.format,
3686 avi->segment.start, avi->segment.last_stop, avi->segment.start);
3689 if (!avi->streaming) {
3690 avi->segment_running = TRUE;
3691 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
3694 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
3701 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
3707 * Helper for gst_avi_demux_invert()
3710 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
3712 memcpy (tmp, d1, bytes);
3713 memcpy (d1, d2, bytes);
3714 memcpy (d2, tmp, bytes);
3718 #define gst_avi_demux_is_uncompressed(fourcc) \
3719 (fourcc == GST_RIFF_DIB || \
3720 fourcc == GST_RIFF_rgb || \
3721 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW)
3724 * Invert DIB buffers... Takes existing buffer and
3725 * returns either the buffer or a new one (with old
3726 * one dereferenced).
3727 * FIXME: can't we preallocate tmp? and remember stride, bpp?
3730 gst_avi_demux_invert (avi_stream_context * stream, GstBuffer * buf)
3737 if (stream->strh->type != GST_RIFF_FCC_vids)
3740 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
3741 return buf; /* Ignore non DIB buffers */
3744 s = gst_caps_get_structure (GST_PAD_CAPS (stream->pad), 0);
3745 if (!gst_structure_get_int (s, "bpp", &bpp)) {
3746 GST_WARNING ("Failed to retrieve depth from caps");
3750 if (stream->strf.vids == NULL) {
3751 GST_WARNING ("Failed to retrieve vids for stream");
3755 h = stream->strf.vids->height;
3756 w = stream->strf.vids->width;
3757 stride = w * (bpp / 8);
3759 buf = gst_buffer_make_writable (buf);
3760 if (GST_BUFFER_SIZE (buf) < (stride * h)) {
3761 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
3765 tmp = g_malloc (stride);
3767 for (y = 0; y < h / 2; y++) {
3768 swap_line (GST_BUFFER_DATA (buf) + stride * y,
3769 GST_BUFFER_DATA (buf) + stride * (h - 1 - y), tmp, stride);
3778 * Returns the aggregated GstFlowReturn.
3780 static GstFlowReturn
3781 gst_avi_demux_combine_flows (GstAviDemux * avi, avi_stream_context * stream,
3786 /* store the value */
3787 stream->last_flow = ret;
3789 /* any other error that is not-linked can be returned right away */
3790 if (G_UNLIKELY (ret != GST_FLOW_NOT_LINKED))
3793 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3794 for (i = 0; i < avi->num_streams; i++) {
3795 avi_stream_context *ostream = &avi->stream[i];
3797 ret = ostream->last_flow;
3798 /* some other return value (must be SUCCESS but we can return
3799 * other values as well) */
3800 if (G_UNLIKELY (ret != GST_FLOW_NOT_LINKED))
3803 /* if we get here, all other pads were unlinked and we return
3804 * NOT_LINKED then */
3806 GST_LOG_OBJECT (avi, "combined return %s", gst_flow_get_name (ret));
3811 * prepare the avi element for a reverse jump to a prev keyframe
3812 * this function will return the start entry. if the function returns
3813 * NULL there was no prev keyframe.
3815 static gst_avi_index_entry *
3816 gst_avi_demux_step_reverse (GstAviDemux * avi)
3818 gst_avi_index_entry *entry;
3821 avi->reverse_stop_index = avi->reverse_start_index;
3823 gst_avi_demux_index_prev (avi, 0, avi->reverse_stop_index,
3824 GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME);
3826 GST_DEBUG_OBJECT (avi, "no valid index entry found index %d",
3827 avi->reverse_stop_index);
3830 avi->current_entry = avi->reverse_start_index = entry->index_nr;
3831 GST_DEBUG_OBJECT (avi,
3832 "reverse playback jump: start idx (%d) and stop idx (%d)",
3833 avi->reverse_start_index, avi->reverse_stop_index);
3834 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, entry->ts);
3835 for (i = 0; i < avi->num_streams; i++) {
3836 avi->stream[i].last_flow = GST_FLOW_OK;
3837 avi->stream[i].discont = TRUE;
3843 * Read data from one index entry
3845 static GstFlowReturn
3846 gst_avi_demux_process_next_entry (GstAviDemux * avi)
3848 GstFlowReturn res = GST_FLOW_OK;
3849 gboolean processed = FALSE;
3850 avi_stream_context *stream;
3851 gst_avi_index_entry *entry;
3855 /* see if we are at the end */
3856 if ((avi->segment.rate > 0 && avi->current_entry >= avi->index_size))
3859 /* get next entry, this will work as we checked for the index size above */
3860 entry = &avi->index_entries[avi->current_entry++];
3862 /* check for reverse playback */
3863 if (avi->segment.rate < 0 && avi->current_entry > avi->reverse_stop_index) {
3864 GST_LOG_OBJECT (avi, "stop_index %d reached", avi->reverse_stop_index);
3866 /* check if we have pushed enough data for this segment */
3867 if (avi->reverse_start_index == 0)
3868 goto eos_reverse_zero;
3869 if (avi->index_entries[avi->reverse_start_index].ts < avi->segment.start)
3870 goto eos_reverse_segment;
3872 if (!(entry = gst_avi_demux_step_reverse (avi)))
3875 avi->current_entry++;
3878 /* see if we have a valid stream, ignore if not
3879 * FIXME: can't we check this when building the index?
3880 * we check it in _parse_index(), _stream_scan()
3882 if (entry->stream_nr >= avi->num_streams) {
3883 GST_WARNING_OBJECT (avi,
3884 "Entry %d has non-existing stream nr %d",
3885 avi->current_entry - 1, entry->stream_nr);
3889 /* get stream now */
3890 stream = &avi->stream[entry->stream_nr];
3892 if (avi->segment.rate > 0.0) {
3893 /* only check this for fowards playback for now */
3894 if ((entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)
3895 && GST_CLOCK_TIME_IS_VALID (entry->ts)
3896 && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
3897 && (entry->ts > avi->segment.stop)) {
3902 /* skip empty entries */
3903 if (entry->size == 0 || !stream->pad) {
3904 GST_DEBUG_OBJECT (avi, "Skipping entry %d (%d, %p)",
3905 avi->current_entry - 1, entry->size, stream->pad);
3909 GST_LOG ("reading buffer (size=%d) from stream %d at current pos %"
3910 G_GUINT64_FORMAT " (%llx)", entry->size, entry->stream_nr,
3911 avi->index_offset + entry->offset, avi->index_offset + entry->offset);
3913 /* pull in the data */
3914 res = gst_pad_pull_range (avi->sinkpad, entry->offset +
3915 avi->index_offset, entry->size, &buf);
3916 if (res != GST_FLOW_OK)
3919 /* check for short buffers, this is EOS as well */
3920 if (GST_BUFFER_SIZE (buf) < entry->size)
3923 /* invert the picture if needed */
3924 buf = gst_avi_demux_invert (stream, buf);
3926 /* mark non-keyframes */
3927 if (!(entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME))
3928 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
3930 GST_BUFFER_TIMESTAMP (buf) = entry->ts;
3931 GST_BUFFER_DURATION (buf) = entry->dur;
3932 if (stream->strh->type == GST_RIFF_FCC_vids) {
3933 if (stream->current_frame >= 0)
3934 GST_BUFFER_OFFSET (buf) = stream->current_frame;
3937 GstFormat fmt = GST_FORMAT_DEFAULT;
3939 if (gst_pad_query_convert (stream->pad, GST_FORMAT_TIME, entry->ts,
3941 GST_BUFFER_OFFSET (buf) = framenum;
3943 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
3946 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
3947 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
3948 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
3950 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %d, offset %"
3951 G_GUINT64_FORMAT " and time %"
3952 GST_TIME_FORMAT " on pad %s",
3953 GST_BUFFER_SIZE (buf), GST_BUFFER_OFFSET (buf),
3954 GST_TIME_ARGS (entry->ts), GST_PAD_NAME (stream->pad));
3956 /* update current position in the segment */
3957 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, entry->ts);
3959 /* mark discont when pending */
3960 if (stream->discont) {
3961 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
3962 stream->discont = FALSE;
3965 res = gst_pad_push (stream->pad, buf);
3967 /* mark as processed, we increment the frame and byte counters then
3968 * leave the while loop and return the GstFlowReturn */
3970 GST_DEBUG_OBJECT (avi, "Processed buffer %d: %s", entry->index_nr,
3971 gst_flow_get_name (res));
3973 if (avi->segment.rate < 0
3974 && entry->ts > avi->segment.stop && res == GST_FLOW_UNEXPECTED) {
3975 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
3976 * we are at the end of the segment, so we just need to jump
3977 * back to the previous section.
3979 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
3981 if (!(entry = gst_avi_demux_step_reverse (avi)))
3986 stream->current_frame = entry->frames_before;
3987 stream->current_byte = entry->bytes_before;
3993 res = gst_avi_demux_combine_flows (avi, stream, res);
3996 stream->current_frame = entry->frames_before + 1;
3997 stream->current_byte = entry->bytes_before + entry->size;
3998 } while (!processed);
4001 GST_DEBUG_OBJECT (avi, "returning %s", gst_flow_get_name (res));
4008 GST_LOG_OBJECT (avi, "Handled last index entry, setting EOS (%d > %d)",
4009 avi->current_entry, avi->index_size);
4010 /* we mark the first stream as EOS */
4011 res = GST_FLOW_UNEXPECTED;
4016 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
4017 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
4018 GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (avi->segment.stop));
4019 res = GST_FLOW_UNEXPECTED;
4024 GST_DEBUG_OBJECT (avi, "start_index was 0, setting EOS");
4025 res = GST_FLOW_UNEXPECTED;
4028 eos_reverse_segment:
4030 GST_DEBUG_OBJECT (avi, "full segment pushed, setting EOS");
4031 res = GST_FLOW_UNEXPECTED;
4036 GST_DEBUG_OBJECT (avi,
4037 "pull range failed: pos=%" G_GUINT64_FORMAT " size=%d",
4038 entry->offset + avi->index_offset, entry->size);
4043 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
4044 ", only got %d/%d bytes (truncated file?)", entry->offset +
4045 avi->index_offset, GST_BUFFER_SIZE (buf), entry->size);
4046 gst_buffer_unref (buf);
4047 res = GST_FLOW_UNEXPECTED;
4053 * Read data. If we have an index it delegates to
4054 * gst_avi_demux_process_next_entry().
4056 static GstFlowReturn
4057 gst_avi_demux_stream_data (GstAviDemux * avi)
4062 GstFlowReturn res = GST_FLOW_OK;
4063 GstFormat format = GST_FORMAT_TIME;
4065 /* if we have a avi->index_entries[], we don't want to read
4066 * the stream linearly, but seek to the next ts/index_entry. */
4067 if (G_LIKELY (avi->index_entries != NULL))
4068 return gst_avi_demux_process_next_entry (avi);
4070 if (G_UNLIKELY (avi->have_eos)) {
4071 /* Clean adapter, we're done */
4072 gst_adapter_clear (avi->adapter);
4077 if (!gst_avi_demux_sync (avi, &tag, FALSE))
4081 /* Iterate until need more data, so adapter won't grow too much */
4083 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
4087 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
4088 GST_FOURCC_ARGS (tag), size);
4090 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
4091 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
4092 GST_LOG ("Chunk ok");
4093 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
4094 GST_DEBUG ("Found sub-index tag");
4095 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
4096 if ((size > 0) && (size != -1)) {
4097 GST_DEBUG (" skipping %d bytes for now", size);
4098 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4102 } else if (tag == GST_RIFF_TAG_JUNK) {
4103 GST_DEBUG ("JUNK chunk, skipping");
4104 } else if (tag == GST_RIFF_TAG_idx1) {
4105 GST_DEBUG ("Found index tag, stream done");
4106 avi->have_eos = TRUE;
4107 return GST_FLOW_UNEXPECTED;
4108 } else if (tag == GST_RIFF_TAG_LIST) {
4109 /* movi chunks might be grouped in rec list */
4110 if (gst_adapter_available (avi->adapter) >= 12) {
4111 GST_DEBUG ("Found LIST tag, skipping LIST header");
4112 gst_adapter_flush (avi->adapter, 12);
4116 } else if (tag == GST_RIFF_TAG_JUNK) {
4117 /* rec list might contain JUNK chunks */
4118 GST_DEBUG ("Found JUNK tag");
4119 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
4120 if ((size > 0) && (size != -1)) {
4121 GST_DEBUG (" skipping %d bytes for now", size);
4122 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4128 GST_DEBUG ("No more stream chunks, send EOS");
4129 avi->have_eos = TRUE;
4130 return GST_FLOW_UNEXPECTED;
4133 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
4134 if ((size == 0) || (size == -1))
4135 gst_adapter_flush (avi->adapter, 8);
4138 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
4139 GST_FOURCC_ARGS (tag), size);
4141 stream_nr = CHUNKID_TO_STREAMNR (tag);
4143 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
4145 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
4146 stream_nr, GST_FOURCC_ARGS (tag));
4147 avi->offset += 8 + ((size + 1) & ~1);
4148 gst_adapter_flush (avi->adapter, 8 + ((size + 1) & ~1));
4150 avi_stream_context *stream;
4151 GstClockTime next_ts = 0;
4154 gst_adapter_flush (avi->adapter, 8);
4157 buf = gst_adapter_take_buffer (avi->adapter, ((size + 1) & ~1));
4158 /* patch the size */
4159 GST_BUFFER_SIZE (buf) = size;
4160 avi->offset += 8 + ((size + 1) & ~1);
4162 /* get time of this buffer */
4163 stream = &avi->stream[stream_nr];
4164 gst_pad_query_position (stream->pad, &format, (gint64 *) & next_ts);
4165 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4168 /* set delay (if any)
4169 if (stream->strh->init_frames == stream->current_frame &&
4171 stream->delay = next_ts;
4174 stream->current_frame++;
4175 stream->current_byte += size;
4177 if (G_UNLIKELY (!stream->pad)) {
4178 GST_WARNING ("No pad.");
4179 gst_buffer_unref (buf);
4181 GstClockTime dur_ts = 0;
4183 /* invert the picture if needed */
4184 buf = gst_avi_demux_invert (stream, buf);
4186 gst_pad_query_position (stream->pad, &format, (gint64 *) & dur_ts);
4187 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4190 GST_BUFFER_TIMESTAMP (buf) = next_ts;
4191 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
4192 if (stream->strh->type == GST_RIFF_FCC_vids)
4193 GST_BUFFER_OFFSET (buf) = stream->current_frame - 1;
4195 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4197 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4198 GST_DEBUG_OBJECT (avi,
4199 "Pushing buffer with time=%" GST_TIME_FORMAT
4200 ", offset %" G_GUINT64_FORMAT " and size %d over pad %s",
4201 GST_TIME_ARGS (next_ts), GST_BUFFER_OFFSET (buf), size,
4202 GST_PAD_NAME (stream->pad));
4204 /* update current position in the segment */
4205 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, next_ts);
4207 /* mark discont when pending */
4208 if (G_UNLIKELY (stream->discont)) {
4209 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4210 stream->discont = FALSE;
4212 res = gst_pad_push (stream->pad, buf);
4215 res = gst_avi_demux_combine_flows (avi, stream, res);
4216 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4217 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
4230 GST_DEBUG_OBJECT (avi, "format %s != GST_FORMAT_TIME",
4231 gst_format_get_name (format));
4232 res = GST_FLOW_ERROR;
4238 * Send pending tags.
4241 push_tag_lists (GstAviDemux * avi)
4248 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
4250 for (i = 0; i < avi->num_streams; i++)
4251 if (avi->stream[i].pad && avi->stream[i].taglist) {
4252 GST_DEBUG_OBJECT (avi->stream[i].pad, "Tags: %" GST_PTR_FORMAT,
4253 avi->stream[i].taglist);
4254 gst_element_found_tags_for_pad (GST_ELEMENT (avi), avi->stream[i].pad,
4255 avi->stream[i].taglist);
4256 avi->stream[i].taglist = NULL;
4259 if (avi->globaltags == NULL)
4260 avi->globaltags = gst_tag_list_new ();
4262 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE,
4263 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
4265 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, avi->globaltags);
4266 gst_element_found_tags (GST_ELEMENT (avi), avi->globaltags);
4267 avi->globaltags = NULL;
4268 avi->got_tags = FALSE;
4272 gst_avi_demux_loop (GstPad * pad)
4275 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4277 switch (avi->state) {
4278 case GST_AVI_DEMUX_START:
4279 if (G_UNLIKELY ((res =
4280 gst_avi_demux_stream_init_pull (avi)) != GST_FLOW_OK)) {
4281 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4284 avi->state = GST_AVI_DEMUX_HEADER;
4286 case GST_AVI_DEMUX_HEADER:
4287 if (G_UNLIKELY ((res =
4288 gst_avi_demux_stream_header_pull (avi)) != GST_FLOW_OK)) {
4289 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4292 avi->state = GST_AVI_DEMUX_MOVI;
4294 case GST_AVI_DEMUX_MOVI:
4295 if (G_UNLIKELY (avi->seek_event)) {
4296 gst_avi_demux_push_event (avi, avi->seek_event);
4297 avi->seek_event = NULL;
4299 if (G_UNLIKELY (avi->got_tags)) {
4300 push_tag_lists (avi);
4302 /* process each index entry in turn */
4303 res = gst_avi_demux_stream_data (avi);
4305 /* pause when error */
4306 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4307 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
4312 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
4313 res = GST_FLOW_ERROR;
4317 GST_LOG_OBJECT (avi, "state: %d res:%s", avi->state, gst_flow_get_name (res));
4323 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
4324 avi->segment_running = FALSE;
4325 gst_pad_pause_task (avi->sinkpad);
4327 if (GST_FLOW_IS_FATAL (res) || (res == GST_FLOW_NOT_LINKED)) {
4328 gboolean push_eos = TRUE;
4330 if (res == GST_FLOW_UNEXPECTED) {
4331 /* handle end-of-stream/segment */
4332 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4335 if ((stop = avi->segment.stop) == -1)
4336 stop = avi->segment.duration;
4338 GST_INFO_OBJECT (avi, "sending segment_done");
4340 gst_element_post_message
4342 gst_message_new_segment_done (GST_OBJECT (avi), GST_FORMAT_TIME,
4347 /* for fatal errors we post an error message */
4348 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
4349 (_("Internal data stream error.")),
4350 ("streaming stopped, reason %s", gst_flow_get_name (res)));
4353 GST_INFO_OBJECT (avi, "sending eos");
4354 gst_avi_demux_push_event (avi, gst_event_new_eos ());
4360 static GstFlowReturn
4361 gst_avi_demux_chain (GstPad * pad, GstBuffer * buf)
4364 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4366 GST_DEBUG ("Store %d bytes in adapter", GST_BUFFER_SIZE (buf));
4367 gst_adapter_push (avi->adapter, buf);
4369 switch (avi->state) {
4370 case GST_AVI_DEMUX_START:
4371 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
4372 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4376 case GST_AVI_DEMUX_HEADER:
4377 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
4378 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4382 case GST_AVI_DEMUX_MOVI:
4383 if (G_UNLIKELY (avi->seek_event)) {
4384 gst_avi_demux_push_event (avi, avi->seek_event);
4385 avi->seek_event = NULL;
4387 if (G_UNLIKELY (avi->got_tags)) {
4388 push_tag_lists (avi);
4390 res = gst_avi_demux_stream_data (avi);
4393 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
4394 ("Illegal internal state"));
4395 res = GST_FLOW_ERROR;
4399 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
4400 gst_flow_get_name (res));
4406 gst_avi_demux_sink_activate (GstPad * sinkpad)
4408 if (gst_pad_check_pull_range (sinkpad)) {
4409 GST_DEBUG ("going to pull mode");
4410 return gst_pad_activate_pull (sinkpad, TRUE);
4412 GST_DEBUG ("going to push (streaming) mode");
4413 return gst_pad_activate_push (sinkpad, TRUE);
4418 gst_avi_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
4420 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (sinkpad));
4423 avi->segment_running = TRUE;
4424 avi->streaming = FALSE;
4425 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4428 avi->segment_running = FALSE;
4429 return gst_pad_stop_task (sinkpad);
4434 gst_avi_demux_activate_push (GstPad * pad, gboolean active)
4436 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (pad));
4439 GST_DEBUG ("avi: activating push/chain function");
4440 avi->streaming = TRUE;
4442 GST_DEBUG ("avi: deactivating push/chain function");
4448 static GstStateChangeReturn
4449 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
4451 GstStateChangeReturn ret;
4452 GstAviDemux *avi = GST_AVI_DEMUX (element);
4454 switch (transition) {
4455 case GST_STATE_CHANGE_READY_TO_PAUSED:
4456 avi->streaming = FALSE;
4457 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
4463 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4464 if (ret == GST_STATE_CHANGE_FAILURE)
4467 switch (transition) {
4468 case GST_STATE_CHANGE_PAUSED_TO_READY:
4469 gst_avi_demux_reset (avi);