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);
79 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
81 static const GstQueryType *gst_avi_demux_get_src_query_types (GstPad * pad);
82 static gboolean gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query);
83 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
84 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
86 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
87 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
89 static void gst_avi_demux_loop (GstPad * pad);
90 static gboolean gst_avi_demux_sink_activate (GstPad * sinkpad);
91 static gboolean gst_avi_demux_sink_activate_pull (GstPad * sinkpad,
93 static gboolean gst_avi_demux_activate_push (GstPad * pad, gboolean active);
94 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstBuffer * buf);
96 static GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
97 GstStateChange transition);
99 static GstElementClass *parent_class = NULL;
101 /* GObject methods */
104 gst_avi_demux_get_type (void)
106 static GType avi_demux_type = 0;
108 if (!avi_demux_type) {
109 static const GTypeInfo avi_demux_info = {
110 sizeof (GstAviDemuxClass),
111 (GBaseInitFunc) gst_avi_demux_base_init,
113 (GClassInitFunc) gst_avi_demux_class_init,
116 sizeof (GstAviDemux),
118 (GInstanceInitFunc) gst_avi_demux_init,
122 g_type_register_static (GST_TYPE_ELEMENT,
123 "GstAviDemux", &avi_demux_info, 0);
126 return avi_demux_type;
130 gst_avi_demux_base_init (GstAviDemuxClass * klass)
132 static const GstElementDetails gst_avi_demux_details =
133 GST_ELEMENT_DETAILS ("Avi demuxer",
135 "Demultiplex an avi file into audio and video",
136 "Erik Walthinsen <omega@cse.ogi.edu>\n"
137 "Wim Taymans <wim.taymans@chello.be>\n"
138 "Ronald Bultje <rbultje@ronald.bitfreak.net>\n"
139 "Thijs Vermeir <thijsvermeir@gmail.com>");
140 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
141 GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl;
142 GstCaps *audcaps, *vidcaps, *subcaps;
144 audcaps = gst_riff_create_audio_template_caps ();
145 gst_caps_append (audcaps, gst_caps_new_simple ("audio/x-avi-unknown", NULL));
146 audiosrctempl = gst_pad_template_new ("audio_%02d",
147 GST_PAD_SRC, GST_PAD_SOMETIMES, audcaps);
149 vidcaps = gst_riff_create_video_template_caps ();
150 gst_caps_append (vidcaps, gst_riff_create_iavs_template_caps ());
151 gst_caps_append (vidcaps, gst_caps_new_simple ("video/x-avi-unknown", NULL));
152 videosrctempl = gst_pad_template_new ("video_%02d",
153 GST_PAD_SRC, GST_PAD_SOMETIMES, vidcaps);
155 subcaps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
156 subsrctempl = gst_pad_template_new ("subtitle_%02d",
157 GST_PAD_SRC, GST_PAD_SOMETIMES, subcaps);
158 gst_element_class_add_pad_template (element_class, audiosrctempl);
159 gst_element_class_add_pad_template (element_class, videosrctempl);
160 gst_element_class_add_pad_template (element_class, subsrctempl);
161 gst_element_class_add_pad_template (element_class,
162 gst_static_pad_template_get (&sink_templ));
163 gst_element_class_set_details (element_class, &gst_avi_demux_details);
167 gst_avi_demux_class_init (GstAviDemuxClass * klass)
169 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
170 GObjectClass *gobject_class = (GObjectClass *) klass;
172 GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
173 0, "Demuxer for AVI streams");
175 parent_class = g_type_class_peek_parent (klass);
177 gobject_class->finalize = gst_avi_demux_finalize;
178 gstelement_class->change_state =
179 GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
183 gst_avi_demux_init (GstAviDemux * avi)
185 avi->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
186 gst_pad_set_activate_function (avi->sinkpad, gst_avi_demux_sink_activate);
187 gst_pad_set_activatepull_function (avi->sinkpad,
188 gst_avi_demux_sink_activate_pull);
189 gst_pad_set_activatepush_function (avi->sinkpad, gst_avi_demux_activate_push);
190 gst_pad_set_chain_function (avi->sinkpad, gst_avi_demux_chain);
191 gst_element_add_pad (GST_ELEMENT (avi), avi->sinkpad);
193 avi->adapter = gst_adapter_new ();
195 gst_avi_demux_reset (avi);
199 gst_avi_demux_finalize (GObject * object)
201 GstAviDemux *avi = GST_AVI_DEMUX (object);
203 GST_DEBUG ("AVI: finalize");
205 g_object_unref (avi->adapter);
207 G_OBJECT_CLASS (parent_class)->finalize (object);
211 gst_avi_demux_reset (GstAviDemux * avi)
215 GST_DEBUG ("AVI: reset");
217 for (i = 0; i < avi->num_streams; i++) {
218 g_free (avi->stream[i].strh);
219 g_free (avi->stream[i].strf.data);
220 if (avi->stream[i].name)
221 g_free (avi->stream[i].name);
222 if (avi->stream[i].initdata)
223 gst_buffer_unref (avi->stream[i].initdata);
224 if (avi->stream[i].extradata)
225 gst_buffer_unref (avi->stream[i].extradata);
226 if (avi->stream[i].pad) {
227 gst_pad_set_active (avi->stream[i].pad, FALSE);
228 gst_element_remove_pad (GST_ELEMENT (avi), avi->stream[i].pad);
230 if (avi->stream[i].taglist) {
231 gst_tag_list_free (avi->stream[i].taglist);
232 avi->stream[i].taglist = NULL;
235 memset (&avi->stream, 0, sizeof (avi->stream));
237 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
238 avi->num_streams = 0;
239 avi->num_v_streams = 0;
240 avi->num_a_streams = 0;
241 avi->num_t_streams = 0;
243 avi->state = GST_AVI_DEMUX_START;
246 g_free (avi->index_entries);
247 avi->index_entries = NULL;
249 avi->index_offset = 0;
250 avi->current_entry = 0;
254 if (avi->seek_event) {
255 gst_event_unref (avi->seek_event);
256 avi->seek_event = NULL;
260 gst_tag_list_free (avi->globaltags);
261 avi->globaltags = NULL;
263 avi->got_tags = TRUE; /* we always want to push global tags */
264 avi->have_eos = FALSE;
266 gst_adapter_clear (avi->adapter);
268 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
272 static gst_avi_index_entry *
273 gst_avi_demux_index_last (GstAviDemux * avi, gint stream_nr)
276 gst_avi_index_entry *result = NULL;
278 for (i = avi->index_size - 1; i >= 0; i--) {
279 if (avi->index_entries[i].stream_nr == stream_nr) {
280 result = &avi->index_entries[i];
287 static gst_avi_index_entry *
288 gst_avi_demux_index_next (GstAviDemux * avi, gint stream_nr, gint last,
292 gst_avi_index_entry *result = NULL, *entry;
294 for (i = last + 1; i < avi->index_size; i++) {
295 entry = &avi->index_entries[i];
297 if (entry->stream_nr != stream_nr)
300 if ((entry->flags & flags) == flags) {
308 static gst_avi_index_entry *
309 gst_avi_demux_index_prev (GstAviDemux * avi, gint stream_nr, gint last,
313 gst_avi_index_entry *result = NULL, *entry;
315 for (i = last - 1; i >= 0; i--) {
316 entry = &avi->index_entries[i];
318 if (entry->stream_nr != stream_nr)
321 if ((entry->flags & flags) == flags) {
330 gst_avi_demux_index_entry_search (gst_avi_index_entry * entry, guint64 * time)
332 if (entry->ts < *time)
334 else if (entry->ts > *time)
340 * gst_avi_index_entry:
342 * @stream_nr: stream number
343 * @time: seek time position
345 * Finds the index entry which time is less or equal than the requested time.
347 * Returns: the found index entry or %NULL
349 static gst_avi_index_entry *
350 gst_avi_demux_index_entry_for_time (GstAviDemux * avi,
351 gint stream_nr, guint64 time)
353 gst_avi_index_entry *entry = NULL;
356 GST_LOG_OBJECT (avi, "stream_nr:%d , time:%" GST_TIME_FORMAT,
357 stream_nr, GST_TIME_ARGS (time));
359 entry = gst_util_array_binary_search (avi->index_entries,
361 sizeof (gst_avi_index_entry),
362 (GCompareDataFunc) gst_avi_demux_index_entry_search,
363 GST_SEARCH_MODE_BEFORE, &time, NULL);
365 n = entry - avi->index_entries;
367 entry = &avi->index_entries[0];
369 while (entry->stream_nr != stream_nr && n < avi->index_size - 1) {
371 entry = &avi->index_entries[n];
373 } else if (entry->stream_nr != stream_nr) {
374 while (entry->stream_nr != stream_nr && n > 0) {
376 entry = &avi->index_entries[n];
381 "best at entry %u / ts:%" GST_TIME_FORMAT " / dur:%" GST_TIME_FORMAT
382 " flags:%02x", n, GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (entry->dur),
388 /* GstElement methods */
391 static const GstFormat *
392 gst_avi_demux_get_src_formats (GstPad * pad)
394 avi_stream_context *stream = gst_pad_get_element_private (pad);
396 static const GstFormat src_a_formats[] = {
402 static const GstFormat src_v_formats[] = {
408 return (stream->strh->type == GST_RIFF_FCC_auds ?
409 src_a_formats : src_v_formats);
413 /* assumes stream->strf.auds->av_bps != 0 */
414 static inline GstClockTime
415 avi_stream_convert_bytes_to_time_unchecked (avi_stream_context * stream,
418 return gst_util_uint64_scale (bytes, GST_SECOND, stream->strf.auds->av_bps);
421 /* assumes stream->strh->rate != 0 */
422 static inline GstClockTime
423 avi_stream_convert_frames_to_time_unchecked (avi_stream_context * stream,
426 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
431 gst_avi_demux_src_convert (GstPad * pad,
432 GstFormat src_format,
433 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
435 avi_stream_context *stream = gst_pad_get_element_private (pad);
439 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
440 ", dest_format:%s", gst_format_get_name (src_format), src_value,
441 gst_format_get_name (*dest_format));
443 if (G_UNLIKELY (src_format == *dest_format)) {
444 *dest_value = src_value;
447 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
451 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
452 (src_format == GST_FORMAT_BYTES
453 || *dest_format == GST_FORMAT_BYTES))) {
458 switch (src_format) {
459 case GST_FORMAT_TIME:
460 switch (*dest_format) {
461 case GST_FORMAT_BYTES:
462 *dest_value = gst_util_uint64_scale (src_value,
463 (guint64) stream->strf.auds->av_bps, GST_SECOND);
465 case GST_FORMAT_DEFAULT:
466 *dest_value = gst_util_uint64_scale (src_value, stream->strh->rate,
467 stream->strh->scale * GST_SECOND);
474 case GST_FORMAT_BYTES:
475 switch (*dest_format) {
476 case GST_FORMAT_TIME:
477 if (stream->strf.auds->av_bps != 0) {
478 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
488 case GST_FORMAT_DEFAULT:
489 switch (*dest_format) {
490 case GST_FORMAT_TIME:
492 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
505 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
506 gst_format_get_name (*dest_format), *dest_value);
510 static const GstQueryType *
511 gst_avi_demux_get_src_query_types (GstPad * pad)
513 static const GstQueryType src_types[] = {
525 gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query)
528 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
530 avi_stream_context *stream = gst_pad_get_element_private (pad);
532 if (!stream->strh || !stream->strf.data)
533 return gst_pad_query_default (pad, query);
535 switch (GST_QUERY_TYPE (query)) {
536 case GST_QUERY_POSITION:{
539 GST_DEBUG ("pos query for stream %d: frames %d, bytes %" G_GUINT64_FORMAT,
540 stream->num, stream->current_frame, stream->current_byte);
542 if (stream->strh->type == GST_RIFF_FCC_auds) {
543 if (stream->is_vbr) {
545 pos = gst_util_uint64_scale ((gint64) stream->current_frame *
546 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
547 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
548 GST_TIME_FORMAT, stream->current_frame, GST_TIME_ARGS (pos));
549 } else if (stream->strf.auds->av_bps != 0) {
551 pos = gst_util_uint64_scale (stream->current_byte, GST_SECOND,
552 (guint64) stream->strf.auds->av_bps);
553 GST_DEBUG_OBJECT (avi,
554 "CBR convert bytes %" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT,
555 stream->current_byte, GST_TIME_ARGS (pos));
556 } else if (stream->total_frames != 0 && stream->total_bytes != 0) {
557 /* calculate timestamps based on percentage of length */
558 guint64 xlen = avi->avih->us_frame *
559 avi->avih->tot_frames * GST_USECOND;
561 if (stream->is_vbr) {
562 pos = gst_util_uint64_scale (xlen, stream->current_frame,
563 stream->total_frames);
564 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
565 GST_TIME_FORMAT, stream->current_frame, GST_TIME_ARGS (pos));
567 pos = gst_util_uint64_scale (xlen, stream->current_byte,
568 stream->total_bytes);
569 GST_DEBUG_OBJECT (avi, "CBR perc convert bytes %" G_GUINT64_FORMAT
570 ", time %" GST_TIME_FORMAT, stream->current_byte,
571 GST_TIME_ARGS (pos));
578 if (stream->strh->rate != 0) {
579 pos = gst_util_uint64_scale ((guint64) stream->current_frame *
580 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
582 pos = stream->current_frame * avi->avih->us_frame * GST_USECOND;
586 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
587 gst_query_set_position (query, GST_FORMAT_TIME, pos);
589 GST_WARNING ("pos query failed");
592 case GST_QUERY_DURATION:
596 if (stream->strh->type != GST_RIFF_FCC_auds &&
597 stream->strh->type != GST_RIFF_FCC_vids) {
602 gst_query_parse_duration (query, &fmt, NULL);
605 case GST_FORMAT_TIME:
606 gst_query_set_duration (query, fmt, stream->duration);
608 case GST_FORMAT_DEFAULT:
609 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
610 stream->total_frames);
611 gst_query_set_duration (query, fmt, stream->total_frames);
619 case GST_QUERY_SEEKING:{
622 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
623 if (fmt == GST_FORMAT_TIME) {
624 gboolean seekable = TRUE;
626 if (avi->streaming) {
629 if (avi->index_entries == NULL) {
631 /* FIXME: when building index_entried, count keyframes
632 if (!(avi->key_frame_ct > 1))
638 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
639 0, stream->duration);
644 case GST_QUERY_CONVERT:{
645 GstFormat src_fmt, dest_fmt;
646 gint64 src_val, dest_val;
648 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
649 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
651 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
653 res = gst_pad_query_default (pad, query);
657 res = gst_pad_query_default (pad, query);
661 gst_object_unref (avi);
666 static const GstEventMask *
667 gst_avi_demux_get_event_mask (GstPad * pad)
669 static const GstEventMask masks[] = {
670 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
679 gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event)
682 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
684 GST_DEBUG_OBJECT (avi,
685 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
687 switch (GST_EVENT_TYPE (event)) {
689 /* handle seeking only in pull mode */
690 if (!avi->streaming) {
691 res = gst_avi_demux_handle_seek (avi, pad, event);
692 gst_event_unref (event);
694 res = gst_pad_event_default (pad, event);
698 case GST_EVENT_NAVIGATION:
700 gst_event_unref (event);
703 res = gst_pad_event_default (pad, event);
707 gst_object_unref (avi);
712 /* streaming helper (push) */
715 * gst_avi_demux_peek_chunk_info:
717 * @tag: holder for tag
718 * @size: holder for tag size
720 * Peek next chunk info (tag and size)
722 * Returns: TRUE when one chunk info has been got
725 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
727 const guint8 *data = NULL;
729 if (gst_adapter_available (avi->adapter) < 8) {
733 data = gst_adapter_peek (avi->adapter, 8);
734 *tag = GST_READ_UINT32_LE (data);
735 *size = GST_READ_UINT32_LE (data + 4);
741 * gst_avi_demux_peek_chunk:
743 * @tag: holder for tag
744 * @size: holder for tag size
746 * Peek enough data for one full chunk
748 * Returns: %TRUE when one chunk has been got
751 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
753 guint32 peek_size = 0;
756 if (!gst_avi_demux_peek_chunk_info (avi, tag, size)) {
759 /* FIXME: shouldn't this check go to gst_avi_demux_peek_chunk_info() already */
760 if (!(*size) || (*size) == -1) {
761 GST_INFO ("Invalid chunk size %d for tag %" GST_FOURCC_FORMAT,
762 *size, GST_FOURCC_ARGS (*tag));
765 peek_size = (*size + 1) & ~1;
766 available = gst_adapter_available (avi->adapter);
768 GST_DEBUG ("Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
769 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
771 if (available >= (8 + peek_size)) {
781 * gst_avi_demux_parse_file_header:
782 * @element: caller element (used for errors/debug).
783 * @buf: input data to be used for parsing.
785 * "Open" a RIFF/AVI file. The buffer should be at least 12
786 * bytes long. Takes ownership of @buf.
788 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
789 * Throws an error, caller should error out (fatal).
792 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
796 /* riff_parse posts an error */
797 if (!gst_riff_parse_file_header (element, buf, &doctype))
800 if (doctype != GST_RIFF_RIFF_AVI)
808 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
809 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
810 GST_FOURCC_ARGS (doctype)));
816 * Read AVI file tag when streaming
819 gst_avi_demux_stream_init_push (GstAviDemux * avi)
821 if (gst_adapter_available (avi->adapter) >= 12) {
824 tmp = gst_adapter_take_buffer (avi->adapter, 12);
826 GST_DEBUG ("Parsing avi header");
827 if (!gst_avi_demux_parse_file_header (GST_ELEMENT (avi), tmp)) {
828 return GST_FLOW_ERROR;
830 GST_DEBUG ("header ok");
833 avi->state = GST_AVI_DEMUX_HEADER;
842 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
845 GstBuffer *buf = NULL;
847 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
848 if (res != GST_FLOW_OK)
850 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
860 GST_DEBUG_OBJECT (avi, "error parsing file header");
861 return GST_FLOW_ERROR;
865 /* AVI header handling */
868 * gst_avi_demux_parse_avih:
869 * @element: caller element (used for errors/debug).
870 * @buf: input data to be used for parsing.
871 * @avih: pointer to structure (filled in by function) containing
872 * stream information (such as flags, number of streams, etc.).
874 * Read 'avih' header. Discards buffer after use.
876 * Returns: TRUE on success, FALSE otherwise. Throws an error if
877 * the header is invalid. The caller should error out
881 gst_avi_demux_parse_avih (GstElement * element,
882 GstBuffer * buf, gst_riff_avih ** _avih)
889 if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_avih))
892 avih = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
894 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
895 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
896 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
897 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
898 avih->flags = GUINT32_FROM_LE (avih->flags);
899 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
900 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
901 avih->streams = GUINT32_FROM_LE (avih->streams);
902 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
903 avih->width = GUINT32_FROM_LE (avih->width);
904 avih->height = GUINT32_FROM_LE (avih->height);
905 avih->scale = GUINT32_FROM_LE (avih->scale);
906 avih->rate = GUINT32_FROM_LE (avih->rate);
907 avih->start = GUINT32_FROM_LE (avih->start);
908 avih->length = GUINT32_FROM_LE (avih->length);
912 GST_INFO_OBJECT (element, "avih tag found:");
913 GST_INFO_OBJECT (element, " us_frame %u", avih->us_frame);
914 GST_INFO_OBJECT (element, " max_bps %u", avih->max_bps);
915 GST_INFO_OBJECT (element, " pad_gran %u", avih->pad_gran);
916 GST_INFO_OBJECT (element, " flags 0x%08x", avih->flags);
917 GST_INFO_OBJECT (element, " tot_frames %u", avih->tot_frames);
918 GST_INFO_OBJECT (element, " init_frames %u", avih->init_frames);
919 GST_INFO_OBJECT (element, " streams %u", avih->streams);
920 GST_INFO_OBJECT (element, " bufsize %u", avih->bufsize);
921 GST_INFO_OBJECT (element, " width %u", avih->width);
922 GST_INFO_OBJECT (element, " height %u", avih->height);
923 GST_INFO_OBJECT (element, " scale %u", avih->scale);
924 GST_INFO_OBJECT (element, " rate %u", avih->rate);
925 GST_INFO_OBJECT (element, " start %u", avih->start);
926 GST_INFO_OBJECT (element, " length %u", avih->length);
929 gst_buffer_unref (buf);
936 GST_ELEMENT_ERROR (element, STREAM, DEMUX, (NULL), ("No buffer"));
941 GST_ELEMENT_ERROR (element, STREAM, DEMUX, (NULL),
942 ("Too small avih (%d available, %d needed)",
943 GST_BUFFER_SIZE (buf), (int) sizeof (gst_riff_avih)));
944 gst_buffer_unref (buf);
950 * gst_avi_demux_parse_superindex:
951 * @avi: caller element (used for debugging/errors).
952 * @buf: input data to use for parsing.
953 * @locations: locations in the file (byte-offsets) that contain
954 * the actual indexes (see get_avi_demux_parse_subindex()).
955 * The array ends with GST_BUFFER_OFFSET_NONE.
957 * Reads superindex (openDML-2 spec stuff) from the provided data.
959 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
960 * on error, but they are not fatal.
963 gst_avi_demux_parse_superindex (GstAviDemux * avi,
964 GstBuffer * buf, guint64 ** _indexes)
974 size = buf ? GST_BUFFER_SIZE (buf) : 0;
978 data = GST_BUFFER_DATA (buf);
980 /* check type of index. The opendml2 specs state that
981 * there should be 4 dwords per array entry. Type can be
982 * either frame or field (and we don't care). */
983 if (GST_READ_UINT16_LE (data) != 4 ||
984 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
985 GST_WARNING_OBJECT (avi,
986 "Superindex for stream has unexpected "
987 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
988 GST_READ_UINT16_LE (data), data[2], data[3]);
989 bpe = GST_READ_UINT16_LE (data) * 4;
991 num = GST_READ_UINT32_LE (&data[4]);
993 indexes = g_new (guint64, num + 1);
994 for (i = 0; i < num; i++) {
995 if (size < 24 + bpe * (i + 1))
997 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
999 indexes[i] = GST_BUFFER_OFFSET_NONE;
1000 *_indexes = indexes;
1002 gst_buffer_unref (buf);
1009 GST_ERROR_OBJECT (avi,
1010 "Not enough data to parse superindex (%d available, 24 needed)", size);
1012 gst_buffer_unref (buf);
1018 * gst_avi_demux_parse_subindex:
1020 * @buf: input data to use for parsing.
1021 * @stream: stream context.
1022 * @entries_list: a list (returned by the function) containing all the
1023 * indexes parsed in this specific subindex. The first
1024 * entry is also a pointer to allocated memory that needs
1025 * to be free´ed. May be NULL if no supported indexes were
1028 * Reads superindex (openDML-2 spec stuff) from the provided data.
1029 * The buffer will be discarded after use.
1031 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1032 * throw an error, caller should bail out asap.
1035 gst_avi_demux_parse_subindex (GstAviDemux * avi,
1036 GstBuffer * buf, avi_stream_context * stream, GList ** _entries_list)
1038 guint8 *data = GST_BUFFER_DATA (buf);
1042 gst_avi_index_entry *entries, *entry;
1043 GList *entries_list = NULL;
1046 *_entries_list = NULL;
1048 size = buf ? GST_BUFFER_SIZE (buf) : 0;
1054 /* We don't support index-data yet */
1056 goto not_implemented;
1058 /* check type of index. The opendml2 specs state that
1059 * there should be 4 dwords per array entry. Type can be
1060 * either frame or field (and we don't care). */
1061 bpe = (data[2] & 0x01) ? 12 : 8;
1062 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1063 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1064 GST_WARNING_OBJECT (avi,
1065 "Superindex for stream %d has unexpected "
1066 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1067 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1068 bpe = GST_READ_UINT16_LE (data) * 4;
1070 num = GST_READ_UINT32_LE (&data[4]);
1071 baseoff = GST_READ_UINT64_LE (&data[12]);
1073 /* If there's nothing, just return ! */
1077 if (!(entries = g_try_new (gst_avi_index_entry, num)))
1080 for (i = 0; i < num; i++) {
1083 entry = &entries[i];
1085 if (size < 24 + bpe * (i + 1))
1089 entry->offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1090 entry->size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1092 (entry->size & 0x80000000) ? 0 : GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
1093 entry->size &= ~0x80000000;
1094 entry->index_nr = i;
1095 entry->stream_nr = stream->num;
1097 /* stream duration unknown, now we can calculate it */
1098 if (stream->idx_duration == -1)
1099 stream->idx_duration = 0;
1102 entry->ts = stream->idx_duration;
1103 if (stream->is_vbr) {
1104 /* VBR stream next timestamp */
1105 if (stream->strh->type == GST_RIFF_FCC_auds) {
1106 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
1107 stream->total_blocks + 1);
1109 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
1110 stream->total_frames + 1);
1113 /* CBR get next timestamp */
1114 next_ts = avi_stream_convert_bytes_to_time_unchecked (stream,
1115 stream->total_bytes + entry->size);
1117 /* duration is next - current */
1118 entry->dur = next_ts - entry->ts;
1120 /* stream position */
1121 entry->bytes_before = stream->total_bytes;
1122 entry->frames_before = stream->total_frames;
1124 stream->total_bytes += entry->size;
1125 stream->total_frames++;
1126 if (stream->strh->type == GST_RIFF_FCC_auds) {
1127 if (stream->strf.auds->blockalign > 0)
1128 stream->total_blocks +=
1129 (entry->size + stream->strf.auds->blockalign -
1130 1) / stream->strf.auds->blockalign;
1132 stream->total_blocks++;
1134 stream->idx_duration = next_ts;
1136 entries_list = g_list_prepend (entries_list, entry);
1139 GST_LOG_OBJECT (avi, "Read %d index entries", i);
1141 gst_buffer_unref (buf);
1144 *_entries_list = g_list_reverse (entries_list);
1154 GST_ERROR_OBJECT (avi,
1155 "Not enough data to parse subindex (%d available, 24 needed)", size);
1157 gst_buffer_unref (buf);
1158 return TRUE; /* continue */
1162 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1163 ("Subindex-is-data is not implemented"));
1164 gst_buffer_unref (buf);
1169 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1170 ("Cannot allocate memory for %u*%u=%u bytes",
1171 (guint) sizeof (gst_avi_index_entry), num,
1172 (guint) sizeof (gst_avi_index_entry) * num));
1173 gst_buffer_unref (buf);
1180 * Read AVI index when streaming
1183 gst_avi_demux_read_subindexes_push (GstAviDemux * avi,
1184 GList ** index, GList ** alloc_list)
1187 guint32 tag = 0, size;
1188 GstBuffer *buf = NULL;
1191 GST_DEBUG_OBJECT (avi, "gst_avi_demux_read_subindexes_push for %d streams",
1194 for (n = 0; n < avi->num_streams; n++) {
1195 avi_stream_context *stream = &avi->stream[n];
1197 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1198 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1200 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1201 '0' + stream->num % 10)) &&
1202 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1203 '0' + stream->num % 10, 'i', 'x'))) {
1204 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1205 GST_FOURCC_ARGS (tag));
1209 avi->offset += 8 + ((size + 1) & ~1);
1211 buf = gst_buffer_new ();
1212 GST_BUFFER_DATA (buf) = gst_adapter_take (avi->adapter, size);
1213 GST_BUFFER_SIZE (buf) = size;
1215 if (!gst_avi_demux_parse_subindex (avi, buf, stream, &list))
1218 GST_DEBUG_OBJECT (avi, " adding %d entries", g_list_length (list));
1219 *alloc_list = g_list_append (*alloc_list, list->data);
1220 *index = g_list_concat (*index, list);
1224 g_free (stream->indexes);
1225 stream->indexes = NULL;
1227 GST_DEBUG_OBJECT (avi, "index %s", ((*index) ? "!= 0" : "== 0"));
1235 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi,
1236 GList ** index, GList ** alloc_list)
1243 GST_DEBUG_OBJECT (avi, "gst_avi_demux_read_subindexes_pull for %d streams",
1246 for (n = 0; n < avi->num_streams; n++) {
1247 avi_stream_context *stream = &avi->stream[n];
1249 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1250 if (gst_riff_read_chunk (GST_ELEMENT (avi), avi->sinkpad,
1251 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1253 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1254 '0' + stream->num % 10)) &&
1255 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1256 '0' + stream->num % 10, 'i', 'x'))) {
1257 /* Some ODML files (created by god knows what muxer) have a ##ix format
1258 * instead of the 'official' ix##. They are still valid though. */
1259 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1260 GST_FOURCC_ARGS (tag));
1261 gst_buffer_unref (buf);
1265 if (!gst_avi_demux_parse_subindex (avi, buf, stream, &list))
1268 GST_DEBUG_OBJECT (avi, " adding %5d entries, total %2d %5d",
1269 g_list_length (list), g_list_length (*alloc_list),
1270 g_list_length (*index));
1271 *alloc_list = g_list_append (*alloc_list, list->data);
1272 *index = g_list_concat (*index, list);
1276 g_free (stream->indexes);
1277 stream->indexes = NULL;
1279 GST_DEBUG_OBJECT (avi, "index %s", ((*index) ? "!= 0" : "== 0"));
1283 * gst_avi_demux_riff_parse_vprp:
1284 * @element: caller element (used for debugging/error).
1285 * @buf: input data to be used for parsing, stripped from header.
1286 * @vprp: a pointer (returned by this function) to a filled-in vprp
1287 * structure. Caller should free it.
1289 * Parses a video stream´s vprp. This function takes ownership of @buf.
1291 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1292 * should be skipped on error, but it is not fatal.
1295 gst_avi_demux_riff_parse_vprp (GstElement * element,
1296 GstBuffer * buf, gst_riff_vprp ** _vprp)
1298 gst_riff_vprp *vprp;
1301 g_return_val_if_fail (buf != NULL, FALSE);
1302 g_return_val_if_fail (_vprp != NULL, FALSE);
1304 if (GST_BUFFER_SIZE (buf) < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1307 vprp = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1309 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1310 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1311 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1312 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1313 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1314 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1315 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1316 vprp->width = GUINT32_FROM_LE (vprp->width);
1317 vprp->height = GUINT32_FROM_LE (vprp->height);
1318 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1322 /* calculate fields based on size */
1323 k = (GST_BUFFER_SIZE (buf) - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) /
1325 if (vprp->fields > k) {
1326 GST_WARNING_OBJECT (element,
1327 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1330 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1331 GST_WARNING_OBJECT (element,
1332 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1333 GST_RIFF_VPRP_VIDEO_FIELDS);
1334 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1336 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1337 for (k = 0; k < vprp->fields; k++) {
1338 gst_riff_vprp_video_field_desc *fd;
1340 fd = &vprp->field_info[k];
1341 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1342 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1343 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1344 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1345 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1346 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1347 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1348 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1353 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1354 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1355 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1356 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1357 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1358 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1359 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1360 vprp->aspect & 0xffff);
1361 GST_INFO_OBJECT (element, " width %d", vprp->width);
1362 GST_INFO_OBJECT (element, " height %d", vprp->height);
1363 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1364 for (k = 0; k < vprp->fields; k++) {
1365 gst_riff_vprp_video_field_desc *fd;
1367 fd = &(vprp->field_info[k]);
1368 GST_INFO_OBJECT (element, " field %u description:", k);
1369 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1370 fd->compressed_bm_height);
1371 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1372 fd->compressed_bm_width);
1373 GST_INFO_OBJECT (element, " valid_bm_height %d",
1374 fd->valid_bm_height);
1375 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1376 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1377 fd->valid_bm_x_offset);
1378 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1379 fd->valid_bm_y_offset);
1380 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1381 fd->video_x_t_offset);
1382 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1385 gst_buffer_unref (buf);
1394 GST_ERROR_OBJECT (element,
1395 "Too small vprp (%d available, at least %d needed)",
1396 GST_BUFFER_SIZE (buf),
1397 (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1398 gst_buffer_unref (buf);
1404 * gst_avi_demux_parse_stream:
1405 * @avi: calling element (used for debugging/errors).
1406 * @buf: input buffer used to parse the stream.
1408 * Parses all subchunks in a strl chunk (which defines a single
1409 * stream). Discards the buffer after use. This function will
1410 * increment the stream counter internally.
1412 * Returns: whether the stream was identified successfully.
1413 * Errors are not fatal. It does indicate the stream
1417 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
1419 avi_stream_context *stream = &avi->stream[avi->num_streams];
1420 GstElementClass *klass;
1421 GstPadTemplate *templ;
1422 GstBuffer *sub = NULL;
1425 gchar *codec_name = NULL, *padname = NULL;
1426 const gchar *tag_name;
1427 GstCaps *caps = NULL;
1429 GstElement *element;
1430 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
1431 gst_riff_vprp *vprp = NULL;
1433 element = GST_ELEMENT_CAST (avi);
1435 GST_DEBUG_OBJECT (avi, "Parsing stream");
1437 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
1438 /* sub can be NULL if the chunk is empty */
1440 case GST_RIFF_TAG_strh:
1442 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
1445 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
1446 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
1451 case GST_RIFF_TAG_strf:
1453 gboolean res = FALSE;
1456 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
1460 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
1463 switch (stream->strh->type) {
1464 case GST_RIFF_FCC_vids:
1465 stream->is_vbr = TRUE;
1466 res = gst_riff_parse_strf_vids (element, sub,
1467 &stream->strf.vids, &stream->extradata);
1468 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
1470 case GST_RIFF_FCC_auds:
1471 stream->is_vbr = (stream->strh->samplesize == 0)
1472 && stream->strh->scale > 1;
1474 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
1475 &stream->extradata);
1476 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
1477 stream->is_vbr, res);
1479 case GST_RIFF_FCC_iavs:
1480 stream->is_vbr = TRUE;
1481 res = gst_riff_parse_strf_iavs (element, sub,
1482 &stream->strf.iavs, &stream->extradata);
1483 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
1485 case GST_RIFF_FCC_txts:
1486 /* nothing to parse here */
1487 stream->is_vbr = (stream->strh->samplesize == 0)
1488 && (stream->strh->scale > 1);
1492 GST_ERROR_OBJECT (avi,
1493 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
1494 GST_FOURCC_ARGS (stream->strh->type));
1502 case GST_RIFF_TAG_vprp:
1505 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
1509 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
1513 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
1517 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
1518 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
1519 /* not considered fatal */
1526 case GST_RIFF_TAG_strd:
1527 if (stream->initdata)
1528 gst_buffer_unref (stream->initdata);
1529 stream->initdata = sub;
1531 case GST_RIFF_TAG_strn:
1532 g_free (stream->name);
1535 g_strndup ((gchar *) GST_BUFFER_DATA (sub),
1536 (gsize) GST_BUFFER_SIZE (sub));
1537 gst_buffer_unref (sub);
1540 stream->name = g_strdup ("");
1542 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
1545 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
1546 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
1547 '0' + avi->num_streams % 10)) {
1548 g_free (stream->indexes);
1549 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
1550 stream->superindex = TRUE;
1553 GST_WARNING_OBJECT (avi,
1554 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
1555 GST_FOURCC_ARGS (tag));
1557 case GST_RIFF_TAG_JUNK:
1559 gst_buffer_unref (sub);
1567 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
1572 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
1576 /* get class to figure out the template */
1577 klass = GST_ELEMENT_GET_CLASS (avi);
1579 /* we now have all info, let´s set up a pad and a caps and be done */
1580 /* create stream name + pad */
1581 switch (stream->strh->type) {
1582 case GST_RIFF_FCC_vids:{
1585 fourcc = (stream->strf.vids->compression) ?
1586 stream->strf.vids->compression : stream->strh->fcc_handler;
1587 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
1588 templ = gst_element_class_get_pad_template (klass, "video_%02d");
1589 caps = gst_riff_create_video_caps (fourcc, stream->strh,
1590 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
1592 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
1593 GST_TYPE_FOURCC, fourcc, NULL);
1594 } else if (got_vprp && vprp) {
1595 guint32 aspect_n, aspect_d;
1598 aspect_n = vprp->aspect >> 16;
1599 aspect_d = vprp->aspect & 0xffff;
1600 /* calculate the pixel aspect ratio using w/h and aspect ratio */
1601 n = aspect_n * stream->strf.vids->height;
1602 d = aspect_d * stream->strf.vids->width;
1604 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1606 /* very local, not needed elsewhere */
1610 tag_name = GST_TAG_VIDEO_CODEC;
1611 avi->num_v_streams++;
1614 case GST_RIFF_FCC_auds:{
1615 padname = g_strdup_printf ("audio_%02d", avi->num_a_streams);
1616 templ = gst_element_class_get_pad_template (klass, "audio_%02d");
1617 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
1618 stream->strh, stream->strf.auds, stream->extradata,
1619 stream->initdata, &codec_name);
1621 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
1622 G_TYPE_INT, stream->strf.auds->format, NULL);
1624 tag_name = GST_TAG_AUDIO_CODEC;
1625 avi->num_a_streams++;
1628 case GST_RIFF_FCC_iavs:{
1629 guint32 fourcc = stream->strh->fcc_handler;
1631 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
1632 templ = gst_element_class_get_pad_template (klass, "video_%02d");
1633 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
1634 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
1636 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
1637 GST_TYPE_FOURCC, fourcc, NULL);
1639 tag_name = GST_TAG_VIDEO_CODEC;
1640 avi->num_v_streams++;
1643 case GST_RIFF_FCC_txts:{
1644 padname = g_strdup_printf ("subtitle_%02d", avi->num_t_streams);
1645 templ = gst_element_class_get_pad_template (klass, "subtitle_%02d");
1646 caps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
1648 avi->num_t_streams++;
1652 g_assert_not_reached ();
1655 /* no caps means no stream */
1657 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
1661 GST_DEBUG_OBJECT (element, "codec-name=%s",
1662 (codec_name ? codec_name : "NULL"));
1663 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
1665 /* set proper settings and add it */
1667 gst_object_unref (stream->pad);
1668 pad = stream->pad = gst_pad_new_from_template (templ, padname);
1669 stream->last_flow = GST_FLOW_OK;
1670 stream->discont = TRUE;
1671 stream->idx_duration = GST_CLOCK_TIME_NONE;
1672 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1673 stream->duration = GST_CLOCK_TIME_NONE;
1676 gst_pad_use_fixed_caps (pad);
1678 gst_pad_set_formats_function (pad,
1679 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
1680 gst_pad_set_event_mask_function (pad,
1681 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
1683 gst_pad_set_event_function (pad,
1684 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
1685 gst_pad_set_query_type_function (pad,
1686 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_query_types));
1687 gst_pad_set_query_function (pad,
1688 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
1690 gst_pad_set_convert_function (pad,
1691 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
1694 stream->num = avi->num_streams;
1695 stream->total_bytes = 0;
1696 stream->total_frames = 0;
1697 stream->total_blocks = 0;
1698 stream->current_frame = 0;
1699 stream->current_byte = 0;
1700 gst_pad_set_element_private (pad, stream);
1702 gst_pad_set_caps (pad, caps);
1703 gst_pad_set_active (pad, TRUE);
1704 gst_element_add_pad (GST_ELEMENT (avi), pad);
1705 GST_LOG_OBJECT (element, "Added pad %s with caps %" GST_PTR_FORMAT,
1706 GST_PAD_NAME (pad), caps);
1707 gst_caps_unref (caps);
1711 if (!stream->taglist)
1712 stream->taglist = gst_tag_list_new ();
1714 avi->got_tags = TRUE;
1716 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
1718 g_free (codec_name);
1726 /* unref any mem that may be in use */
1728 gst_buffer_unref (buf);
1730 gst_buffer_unref (sub);
1732 g_free (codec_name);
1733 g_free (stream->strh);
1734 g_free (stream->strf.data);
1735 g_free (stream->name);
1736 g_free (stream->indexes);
1737 if (stream->initdata)
1738 gst_buffer_unref (stream->initdata);
1739 if (stream->extradata)
1740 gst_buffer_unref (stream->extradata);
1741 memset (stream, 0, sizeof (avi_stream_context));
1748 * gst_avi_demux_parse_odml:
1749 * @avi: calling element (used for debug/error).
1750 * @buf: input buffer to be used for parsing.
1752 * Read an openDML-2.0 extension header. Fills in the frame number
1753 * in the avi demuxer object when reading succeeds.
1756 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
1760 GstBuffer *sub = NULL;
1762 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
1765 case GST_RIFF_TAG_dmlh:{
1766 gst_riff_dmlh dmlh, *_dmlh;
1769 /* sub == NULL is possible and means an empty buffer */
1770 size = sub ? GST_BUFFER_SIZE (sub) : 0;
1773 if (size < sizeof (gst_riff_dmlh)) {
1774 GST_ERROR_OBJECT (avi,
1775 "DMLH entry is too small (%d bytes, %d needed)",
1776 size, (int) sizeof (gst_riff_dmlh));
1779 _dmlh = (gst_riff_dmlh *) GST_BUFFER_DATA (sub);
1780 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
1782 GST_INFO_OBJECT (avi, "dmlh tag found:");
1783 GST_INFO_OBJECT (avi, " totalframes: %u", dmlh.totalframes);
1785 avi->avih->tot_frames = dmlh.totalframes;
1790 GST_WARNING_OBJECT (avi,
1791 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
1792 GST_FOURCC_ARGS (tag));
1794 case GST_RIFF_TAG_JUNK:
1796 /* skip and move to next chunk */
1798 gst_buffer_unref (sub);
1805 gst_buffer_unref (buf);
1809 * Sort helper for index entries that sorts by index time.
1810 * If times are equal we sort by stream number.
1813 sort (gst_avi_index_entry * a, gst_avi_index_entry * b)
1817 else if (a->ts < b->ts)
1820 return a->stream_nr - b->stream_nr;
1824 * gst_avi_demux_parse_index:
1825 * @avi: calling element (used for debugging/errors).
1826 * @buf: buffer containing the full index.
1827 * @entries_list: list (returned by this function) containing the index
1828 * entries parsed from the buffer. The first in the list
1829 * is also a pointer to the allocated data and should be
1830 * free'ed at some point.
1832 * Read index entries from the provided buffer. Takes ownership of @buf.
1835 gst_avi_demux_parse_index (GstAviDemux * avi,
1836 GstBuffer * buf, GList ** _entries_list)
1838 guint64 pos_before = avi->offset;
1839 gst_avi_index_entry *entries = NULL;
1841 GList *entries_list = NULL;
1844 #ifndef GST_DISABLE_DEBUG
1845 gulong _nr_keyframes = 0;
1848 if (!buf || !GST_BUFFER_SIZE (buf)) {
1849 *_entries_list = NULL;
1850 GST_DEBUG ("empty index");
1852 gst_buffer_unref (buf);
1856 data = GST_BUFFER_DATA (buf);
1857 num = GST_BUFFER_SIZE (buf) / sizeof (gst_riff_index_entry);
1858 if (!(entries = g_try_new (gst_avi_index_entry, num)))
1861 GST_INFO ("Parsing index, nr_entries = %6d", num);
1863 for (i = 0, n = 0; i < num; i++) {
1865 gst_riff_index_entry entry, *_entry;
1866 avi_stream_context *stream;
1868 gst_avi_index_entry *target;
1870 _entry = &((gst_riff_index_entry *) data)[i];
1871 entry.id = GST_READ_UINT32_LE (&_entry->id);
1872 entry.offset = GST_READ_UINT32_LE (&_entry->offset);
1873 entry.flags = GST_READ_UINT32_LE (&_entry->flags);
1874 entry.size = GST_READ_UINT32_LE (&_entry->size);
1875 target = &entries[n];
1877 if (entry.id == GST_RIFF_rec || entry.id == 0 ||
1878 (entry.offset == 0 && n > 0))
1881 stream_nr = CHUNKID_TO_STREAMNR (entry.id);
1882 if (stream_nr >= avi->num_streams) {
1883 GST_WARNING_OBJECT (avi,
1884 "Index entry %d has invalid stream nr %d", i, stream_nr);
1887 target->stream_nr = stream_nr;
1888 stream = &avi->stream[stream_nr];
1890 if (!stream->strh) {
1891 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
1895 target->index_nr = i;
1897 (entry.flags & GST_RIFF_IF_KEYFRAME) ? GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME
1899 target->size = entry.size;
1900 target->offset = entry.offset + 8;
1902 /* figure out if the index is 0 based or relative to the MOVI start */
1904 if (target->offset < pos_before)
1905 avi->index_offset = pos_before + 8;
1907 avi->index_offset = 0;
1908 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
1911 if (stream->strh->type == GST_RIFF_FCC_auds) {
1912 /* all audio frames are keyframes */
1913 target->flags |= GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
1915 #ifndef GST_DISABLE_DEBUG
1916 if (target->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)
1920 /* stream duration unknown, now we can calculate it */
1921 if (stream->idx_duration == -1)
1922 stream->idx_duration = 0;
1925 target->ts = stream->idx_duration;
1926 if (stream->is_vbr) {
1927 /* VBR stream next timestamp */
1928 if (stream->strh->type == GST_RIFF_FCC_auds) {
1929 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
1930 stream->total_blocks + 1);
1932 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
1933 stream->total_frames + 1);
1936 /* constant rate stream */
1937 next_ts = avi_stream_convert_bytes_to_time_unchecked (stream,
1938 stream->total_bytes + target->size);
1940 /* duration is next - current */
1941 target->dur = next_ts - target->ts;
1943 /* stream position */
1944 target->bytes_before = stream->total_bytes;
1945 target->frames_before = stream->total_frames;
1947 stream->total_bytes += target->size;
1948 stream->total_frames++;
1949 if (stream->strh->type == GST_RIFF_FCC_auds) {
1950 if (stream->strf.auds->blockalign > 0)
1951 stream->total_blocks +=
1952 (target->size + stream->strf.auds->blockalign -
1953 1) / stream->strf.auds->blockalign;
1955 stream->total_blocks++;
1957 stream->idx_duration = next_ts;
1959 GST_LOG_OBJECT (avi,
1960 "Adding index entry %d (%6u), flags %02x, stream %d, size %u "
1961 ", offset %" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT ", dur %"
1963 target->index_nr, stream->total_frames - 1, target->flags,
1964 target->stream_nr, target->size, target->offset,
1965 GST_TIME_ARGS (target->ts), GST_TIME_ARGS (target->dur));
1966 entries_list = g_list_prepend (entries_list, target);
1971 GST_INFO ("Parsed index, %6d entries, %5ld keyframes, entry size = %2d, "
1972 "total size = %10d", num, _nr_keyframes,
1973 (gint) sizeof (gst_avi_index_entry),
1974 (gint) (num * sizeof (gst_avi_index_entry)));
1976 gst_buffer_unref (buf);
1979 *_entries_list = g_list_reverse (entries_list);
1988 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1989 ("Cannot allocate memory for %u*%u=%u bytes",
1990 (guint) sizeof (gst_avi_index_entry), num,
1991 (guint) sizeof (gst_avi_index_entry) * num));
1992 gst_buffer_unref (buf);
1997 * gst_avi_demux_stream_index:
1998 * @avi: avi demuxer object.
1999 * @index: list of index entries, returned by this function.
2000 * @alloc_list: list of allocated data, returned by this function.
2002 * Seeks to index and reads it.
2005 gst_avi_demux_stream_index (GstAviDemux * avi,
2006 GList ** index, GList ** alloc_list)
2009 guint64 offset = avi->offset;
2015 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2020 /* get chunk information */
2021 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2022 if (res != GST_FLOW_OK)
2024 else if (GST_BUFFER_SIZE (buf) < 8)
2027 /* check tag first before blindy trying to read 'size' bytes */
2028 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2029 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2030 if (tag == GST_RIFF_TAG_LIST) {
2031 /* this is the movi tag */
2032 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2033 (8 + ((size + 1) & ~1)));
2034 offset += 8 + ((size + 1) & ~1);
2035 gst_buffer_unref (buf);
2036 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2037 if (res != GST_FLOW_OK)
2039 else if (GST_BUFFER_SIZE (buf) < 8)
2041 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2042 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2045 if (tag != GST_RIFF_TAG_idx1)
2050 gst_buffer_unref (buf);
2052 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2054 /* read chunk, advance offset */
2055 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2056 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2059 GST_INFO ("will parse index chunk size %u for tag %"
2060 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2062 gst_avi_demux_parse_index (avi, buf, index);
2064 *alloc_list = g_list_append (*alloc_list, (*index)->data);
2066 /* debug our indexes */
2067 for (i = 0; i < avi->num_streams; i++) {
2068 avi_stream_context *stream;
2070 stream = &avi->stream[i];
2071 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2072 i, stream->total_frames, stream->total_bytes);
2079 GST_DEBUG_OBJECT (avi,
2080 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2085 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2086 gst_buffer_unref (buf);
2091 GST_WARNING_OBJECT (avi,
2092 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2093 GST_FOURCC_ARGS (tag));
2094 gst_buffer_unref (buf);
2099 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2100 gst_buffer_unref (buf);
2107 * Sync to next data chunk.
2110 gst_avi_demux_skip (GstAviDemux * avi, gboolean prevent_eos)
2112 GstRiffRead *riff = GST_RIFF_READ (avi);
2115 guint64 pos, length;
2119 pos = gst_bytestream_tell (riff->bs);
2120 length = gst_bytestream_length (riff->bs);
2122 if (pos + 8 > length)
2125 if (gst_bytestream_peek_bytes (riff->bs, &data, 8) != 8)
2128 size = GST_READ_UINT32_LE (&data[4]);
2132 /* Note, we're going to skip which might involve seeks. Therefore,
2133 * we need 1 byte more! */
2134 if (pos + 8 + size >= length)
2138 return gst_riff_read_skip (riff);
2142 gst_avi_demux_sync (GstAviDemux * avi, guint32 * ret_tag, gboolean prevent_eos)
2144 GstRiffRead *riff = GST_RIFF_READ (avi);
2146 guint64 length = gst_bytestream_length (riff->bs);
2148 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2151 /* peek first (for the end of this 'list/movi' section) */
2152 if (!(tag = gst_riff_peek_tag (riff, &avi->level_up)))
2155 /* if we're at top-level, we didn't read the 'movi'
2156 * list tag yet. This can also be 'AVIX' in case of
2157 * openDML-2.0 AVI files. Lastly, it might be idx1,
2158 * in which case we skip it so we come at EOS. */
2160 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2163 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2167 case GST_RIFF_TAG_LIST:
2168 if (!(tag = gst_riff_peek_list (riff)))
2172 case GST_RIFF_LIST_AVIX:
2173 if (!gst_riff_read_list (riff, &tag))
2177 case GST_RIFF_LIST_movi:
2178 if (!gst_riff_read_list (riff, &tag))
2186 GST_WARNING ("Unknown list %" GST_FOURCC_FORMAT " before AVI data",
2187 GST_FOURCC_ARGS (tag));
2190 case GST_RIFF_TAG_JUNK:
2191 if (!gst_avi_demux_skip (avi, prevent_eos))
2198 if ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
2199 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9') {
2204 case GST_RIFF_TAG_idx1:
2205 case GST_RIFF_TAG_JUNK:
2206 if (!gst_avi_demux_skip (avi, prevent_eos)) {
2213 /* And then, we get the data */
2214 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2217 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2220 /* Support for rec-list files */
2222 case GST_RIFF_TAG_LIST:
2223 if (!(tag = gst_riff_peek_list (riff)))
2225 if (tag == GST_RIFF_rec) {
2226 /* Simply skip the list */
2227 if (!gst_riff_read_list (riff, &tag))
2229 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2234 case GST_RIFF_TAG_JUNK:
2235 gst_avi_demux_skip (avi, prevent_eos);
2247 * gst_avi_demux_peek_tag:
2249 * Returns the tag and size of the next chunk
2251 static GstFlowReturn
2252 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2255 GstFlowReturn res = GST_FLOW_OK;
2256 GstBuffer *buf = NULL;
2259 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2260 if (res != GST_FLOW_OK)
2263 bufsize = GST_BUFFER_SIZE (buf);
2267 *tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2268 *size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2270 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2271 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2272 *size, offset + 8, offset + 8 + (gint64) * size);
2274 gst_buffer_unref (buf);
2281 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2286 GST_DEBUG_OBJECT (avi, "got %d bytes which is <> 8 bytes", bufsize);
2287 res = GST_FLOW_ERROR;
2293 * gst_avi_demux_next_data_buffer:
2295 * Returns the offset and size of the next buffer
2296 * Position is the position of the buffer (after tag and size)
2298 static GstFlowReturn
2299 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2300 guint32 * tag, guint * size)
2302 guint64 off = *offset;
2307 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2308 if (res != GST_FLOW_OK)
2310 if (*tag == GST_RIFF_TAG_LIST)
2311 off += 8 + 4; /* skip tag + size + subtag */
2323 * gst_avi_demux_stream_scan:
2324 * @avi: calling element (used for debugging/errors).
2325 * @index: list of index entries, returned by this function.
2326 * @alloc_list: list of allocated data, returned by this function.
2328 * Scan the file for all chunks to "create" a new index.
2329 * Return value indicates if we can continue reading the stream. It
2330 * does not say anything about whether we created an index.
2335 gst_avi_demux_stream_scan (GstAviDemux * avi,
2336 GList ** index, GList ** alloc_list)
2339 gst_avi_index_entry *entry, *entries = NULL;
2340 avi_stream_context *stream;
2342 guint64 pos = avi->offset;
2347 guint index_size = 0;
2350 * - implement non-seekable source support.
2352 GST_DEBUG_OBJECT (avi,
2353 "Creating index %s existing index, starting at offset %" G_GUINT64_FORMAT,
2354 ((*index) ? "with" : "without"), pos);
2356 format = GST_FORMAT_BYTES;
2357 if (!gst_pad_query_peer_duration (avi->sinkpad, &format, &tmplength))
2363 entry = g_list_last (*index)->data;
2364 pos = entry->offset + avi->index_offset + entry->size;
2365 if (entry->size & 1)
2368 if (pos >= length) {
2369 GST_LOG_OBJECT (avi, "Complete index, we're done");
2373 GST_LOG_OBJECT (avi, "Incomplete index, seeking to last valid entry @ %"
2374 G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT " (%"
2375 G_GUINT64_FORMAT "+%u)", pos, length, entry->offset, entry->size);
2382 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
2383 if (G_UNLIKELY (res != GST_FLOW_OK))
2386 /* check valid stream */
2387 stream_nr = CHUNKID_TO_STREAMNR (tag);
2388 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2389 GST_WARNING_OBJECT (avi,
2390 "Index entry has invalid stream nr %d", stream_nr);
2394 stream = &avi->stream[stream_nr];
2395 if (G_UNLIKELY (stream->pad == NULL)) {
2396 GST_WARNING_OBJECT (avi,
2397 "Stream %d does not have an output pad, can't create new index",
2403 if (G_UNLIKELY (index_size % 1024 == 0)) {
2404 entries = g_new (gst_avi_index_entry, 1024);
2405 *alloc_list = g_list_prepend (*alloc_list, entries);
2407 entry = &entries[index_size % 1024];
2409 entry->index_nr = index_size++;
2410 entry->stream_nr = stream_nr;
2411 entry->flags = GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
2412 entry->offset = pos - avi->index_offset;
2415 /* timestamps, get timestamps of two consecutive frames to calculate
2416 * timestamp and duration. */
2417 format = GST_FORMAT_TIME;
2418 if (stream->is_vbr) {
2420 entry->ts = avi_stream_convert_frames_to_time_unchecked (stream,
2421 stream->total_frames);
2422 entry->dur = avi_stream_convert_frames_to_time_unchecked (stream,
2423 stream->total_frames + 1);
2425 /* constant rate stream */
2426 entry->ts = avi_stream_convert_bytes_to_time_unchecked (stream,
2427 stream->total_bytes);
2428 entry->dur = avi_stream_convert_bytes_to_time_unchecked (stream,
2429 stream->total_bytes + entry->size);
2431 entry->dur -= entry->ts;
2433 /* stream position */
2434 entry->bytes_before = stream->total_bytes;
2435 stream->total_bytes += entry->size;
2436 entry->frames_before = stream->total_frames;
2437 stream->total_frames++;
2438 stream->idx_duration = entry->ts + entry->dur;
2440 list = g_list_prepend (list, entry);
2441 GST_DEBUG_OBJECT (avi, "Added index entry %d (in stream: %d), offset %"
2442 G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT " for stream %d",
2443 index_size - 1, entry->frames_before, entry->offset,
2444 GST_TIME_ARGS (entry->ts), entry->stream_nr);
2447 /* update position */
2448 pos += GST_ROUND_UP_2 (size);
2449 if (G_UNLIKELY (pos > length)) {
2450 GST_WARNING_OBJECT (avi,
2451 "Stopping index lookup since we are further than EOF");
2456 /* FIXME: why is this disabled */
2458 while (gst_avi_demux_sync (avi, &tag, TRUE)) {
2459 guint stream_nr = CHUNKID_TO_STREAMNR (tag);
2461 GstFormat format = GST_FORMAT_TIME;
2463 if (stream_nr >= avi->num_streams)
2465 stream = &avi->stream[stream_nr];
2467 /* get chunk size */
2468 if (gst_bytestream_peek_bytes (riff->bs, &data, 8) != 8)
2472 entry->index_nr = index_size++;
2473 entry->stream_nr = stream_nr;
2474 entry->flags = GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
2475 entry->offset = gst_bytestream_tell (riff->bs) + 8 - avi->index_offset;
2476 entry->size = GST_READ_UINT32_LE (&data[4]);
2479 if (stream->is_vbr) {
2481 entry->ts = avi_stream_convert_frames_to_time_unchecked (stream,
2482 stream->total_frames);
2483 entry->dur = avi_stream_convert_frames_to_time_unchecked (stream,
2484 stream->total_frames + 1);
2486 /* constant rate stream */
2487 entry->ts = avi_stream_convert_bytes_to_time_unchecked (stream,
2488 stream->total_bytes);
2489 entry->dur = avi_stream_convert_bytes_to_time_unchecked (stream,
2490 stream->total_bytes + entry->size);
2492 entry->dur -= entry->ts;
2494 /* stream position */
2495 entry->bytes_before = stream->total_bytes;
2496 stream->total_bytes += entry->size;
2497 entry->frames_before = stream->total_frames;
2498 stream->total_frames++;
2500 list = g_list_prepend (list, entry);
2501 GST_DEBUG_OBJECT (avi, "Added index entry %d (in stream: %d), offset %"
2502 G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT " for stream %d",
2503 index_size - 1, entry->frames_before, entry->offset,
2504 GST_TIME_ARGS (entry->ts), entry->stream_nr);
2507 if (!gst_avi_demux_skip (avi, TRUE))
2511 if (!(event = gst_riff_read_seek (riff, pos))) {
2515 gst_event_unref (event);
2519 GST_DEBUG_OBJECT (avi, "index created, %d items", index_size);
2521 *index = g_list_concat (*index, g_list_reverse (list));
2527 * gst_avi_demux_massage_index:
2528 * @avi: calling element (used for debugging/errors).
2530 * We're going to go over each entry in the index and finetune
2531 * some things we don't like about AVI. For example, a single
2532 * chunk might be too long. Also, individual streams might be
2533 * out-of-sync. In the first case, we cut the chunk in several
2534 * smaller pieces. In the second case, we re-order chunk reading
2535 * order. The end result should be a smoother playing AVI.
2538 gst_avi_demux_massage_index (GstAviDemux * avi,
2539 GList * list, GList * alloc_list)
2541 gst_avi_index_entry *entry;
2542 avi_stream_context *stream;
2545 gint64 delay = G_GINT64_CONSTANT (0);
2547 GST_LOG_OBJECT (avi, "Starting index massage, nr_entries = %d",
2548 list ? g_list_length (list) : 0);
2551 #ifndef GST_DISABLE_DEBUG
2552 guint num_added_total = 0;
2553 guint num_per_stream[GST_AVI_DEMUX_MAX_STREAMS] = { 0, };
2555 GST_LOG_OBJECT (avi,
2556 "I'm now going to cut large chunks into smaller pieces");
2558 /* cut chunks in small (seekable) pieces
2559 * FIXME: this should be a property where a value of
2560 * GST_CLOCK_TIME_NONE would disable the chunking
2562 #define MAX_DURATION (GST_SECOND / 2)
2563 for (i = 0; i < avi->num_streams; i++) {
2564 /* only chop streams that have exactly *one* chunk */
2565 if (avi->stream[i].total_frames != 1)
2568 for (node = list; node != NULL; node = node->next) {
2571 if (entry->stream_nr != i)
2574 /* check for max duration of a single buffer. I suppose that
2575 * the allocation of index entries could be improved. */
2576 stream = &avi->stream[entry->stream_nr];
2577 if (entry->dur > MAX_DURATION
2578 && stream->strh->type == GST_RIFF_FCC_auds) {
2580 gst_avi_index_entry *entries;
2581 guint old_size, num_added;
2584 /* cut in 1/10th of a second */
2585 ideal_size = stream->strf.auds->av_bps / 10;
2587 /* ensure chunk size is multiple of blockalign */
2588 if (stream->strf.auds->blockalign > 1)
2589 ideal_size -= ideal_size % stream->strf.auds->blockalign;
2592 old_size = entry->size;
2593 num_added = (entry->size - 1) / ideal_size;
2594 avi->index_size += num_added;
2595 entries = g_malloc (sizeof (gst_avi_index_entry) * num_added);
2596 alloc_list = g_list_prepend (alloc_list, entries);
2597 for (node2 = node->next; node2 != NULL; node2 = node2->next) {
2598 gst_avi_index_entry *entry2 = node2->data;
2600 entry2->index_nr += num_added;
2601 if (entry2->stream_nr == entry->stream_nr)
2602 entry2->frames_before += num_added;
2605 /* new sized index chunks */
2606 for (i = 0; i < num_added + 1; i++) {
2607 gst_avi_index_entry *entry2;
2612 entry2 = &entries[i - 1];
2613 list = g_list_insert_before (list, node->next, entry2);
2616 memcpy (entry2, entry, sizeof (gst_avi_index_entry));
2619 if (old_size >= ideal_size) {
2620 entry2->size = ideal_size;
2621 old_size -= ideal_size;
2623 entry2->size = old_size;
2626 entry2->dur = GST_SECOND * entry2->size / stream->strf.auds->av_bps;
2629 entry2->ts += entry->dur;
2630 entry2->offset += entry->size;
2631 entry2->bytes_before += entry->size;
2632 entry2->frames_before++;
2635 #ifndef GST_DISABLE_DEBUG
2636 num_added_total += num_added;
2641 #ifndef GST_DISABLE_DEBUG
2642 if (num_added_total)
2643 GST_LOG ("added %u new index entries", num_added_total);
2646 GST_LOG_OBJECT (avi, "I'm now going to reorder the index entries for time");
2648 /* re-order for time */
2649 list = g_list_sort (list, (GCompareFunc) sort);
2651 /* make a continous array out of the list */
2652 avi->index_size = g_list_length (list);
2653 avi->index_entries = g_try_new (gst_avi_index_entry, avi->index_size);
2654 if (!avi->index_entries)
2657 entry = (gst_avi_index_entry *) (list->data);
2660 GST_LOG_OBJECT (avi,
2661 "Building index array, nr_entries = %d (time offset = %"
2662 GST_TIME_FORMAT, avi->index_size, GST_TIME_ARGS (delay));
2664 for (i = 0, node = list; node != NULL; node = node->next, i++) {
2666 entry->index_nr = i;
2668 memcpy (&avi->index_entries[i], entry, sizeof (gst_avi_index_entry));
2669 #ifndef GST_DISABLE_DEBUG
2670 num_per_stream[entry->stream_nr]++;
2673 GST_LOG_OBJECT (avi, "Sorted index entry %3d for stream %d of size %6u"
2674 " at offset %7" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT
2675 " dur %" GST_TIME_FORMAT,
2676 avi->index_entries[i].index_nr, entry->stream_nr, entry->size,
2677 entry->offset, GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (entry->dur));
2680 for (i = 0; i < avi->num_streams; i++) {
2681 stream = &avi->stream[i];
2682 stream->idx_duration -= delay;
2685 #ifndef GST_DISABLE_DEBUG
2687 gchar str[GST_AVI_DEMUX_MAX_STREAMS * (1 + 6 + 2)];
2690 for (i = 0; i < avi->num_streams; i++) {
2691 if (!avi->stream[i].pad)
2693 pad_name = GST_OBJECT_NAME (avi->stream[i].pad);
2694 sprintf (&str[i * (1 + 6 + 2)], " %6u %c", num_per_stream[i],
2697 GST_LOG_OBJECT (avi, "indizies per stream:%20s", str);
2701 GST_LOG_OBJECT (avi, "Freeing original index list");
2702 /* all the node->data in list point to alloc_list chunks */
2707 g_list_foreach (alloc_list, (GFunc) g_free, NULL);
2708 g_list_free (alloc_list);
2710 #ifndef GST_DISABLE_GST_DEBUG
2711 for (i = 0; i < avi->num_streams; i++) {
2712 GST_LOG_OBJECT (avi, "Stream %d, %d frames, %8" G_GUINT64_FORMAT " bytes",
2713 i, avi->stream[i].total_frames, avi->stream[i].total_bytes);
2717 GST_LOG_OBJECT (avi, "Index massaging done");
2722 GST_WARNING_OBJECT (avi, "Out of memory for %" G_GSIZE_FORMAT " bytes",
2723 sizeof (gst_avi_index_entry) * avi->index_size);
2728 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
2730 gst_avi_index_entry *entry;
2734 total = GST_CLOCK_TIME_NONE;
2736 /* all streams start at a timestamp 0 */
2737 for (stream = 0; stream < avi->num_streams; stream++) {
2738 GstClockTime duration, hduration;
2739 avi_stream_context *streamc = &avi->stream[stream];
2740 gst_riff_strh *strh = streamc->strh;
2744 /* get header duration */
2745 hduration = gst_util_uint64_scale ((guint64) strh->length *
2746 strh->scale, GST_SECOND, (guint64) strh->rate);
2748 GST_INFO ("Stream %d duration according to header: %" GST_TIME_FORMAT,
2749 stream, GST_TIME_ARGS (hduration));
2752 hduration = GST_CLOCK_TIME_NONE;
2754 /* set duration for the stream */
2755 streamc->hdr_duration = hduration;
2757 /* get last index entry to get duration */
2758 if ((entry = gst_avi_demux_index_last (avi, stream)))
2759 duration = entry->ts + entry->dur;
2761 duration = GST_CLOCK_TIME_NONE;
2763 streamc->idx_duration = duration;
2765 /* now pick a good duration */
2766 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2767 /* index gave valid duration, use that */
2768 GST_INFO ("Stream %d duration according to index: %" GST_TIME_FORMAT,
2769 stream, GST_TIME_ARGS (duration));
2771 /* fall back to header info to calculate a duration */
2772 duration = hduration;
2774 /* set duration for the stream */
2775 streamc->duration = duration;
2777 /* find total duration */
2778 if (total == GST_CLOCK_TIME_NONE || duration > total)
2782 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
2783 /* now update the duration for those streams where we had none */
2784 for (stream = 0; stream < avi->num_streams; stream++) {
2785 avi_stream_context *streamc = &avi->stream[stream];
2787 if (!GST_CLOCK_TIME_IS_VALID (streamc->duration)
2788 || streamc->duration == 0) {
2789 streamc->duration = total;
2791 GST_INFO ("Stream %d duration according to total: %" GST_TIME_FORMAT,
2792 stream, GST_TIME_ARGS (total));
2797 /* and set the total duration in the segment. */
2798 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
2799 GST_TIME_ARGS (total));
2801 gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
2805 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
2807 gboolean result = FALSE;
2810 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
2811 GST_EVENT_TYPE_NAME (event), avi->num_streams);
2813 if (avi->num_streams) {
2814 for (i = 0; i < avi->num_streams; i++) {
2815 avi_stream_context *stream = &avi->stream[i];
2818 if (gst_pad_push_event (stream->pad, gst_event_ref (event)))
2823 gst_event_unref (event);
2828 * Read AVI headers when streaming
2830 static GstFlowReturn
2831 gst_avi_demux_stream_header_push (GstAviDemux * avi)
2833 GstFlowReturn ret = GST_FLOW_OK;
2838 GstBuffer *buf = NULL, *sub = NULL;
2842 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
2844 switch (avi->header_state) {
2845 case GST_AVI_DEMUX_HEADER_TAG_LIST:
2846 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2847 avi->offset += 8 + ((size + 1) & ~1);
2848 if (tag != GST_RIFF_TAG_LIST)
2849 goto header_no_list;
2851 gst_adapter_flush (avi->adapter, 8);
2852 /* Find the 'hdrl' LIST tag */
2853 GST_DEBUG ("Reading %d bytes", size);
2854 buf = gst_adapter_take_buffer (avi->adapter, size);
2856 if (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl)
2857 goto header_no_hdrl;
2859 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
2861 /* the hdrl starts with a 'avih' header */
2862 if (!gst_riff_parse_chunk (GST_ELEMENT (avi), buf, &offset, &tag, &sub))
2863 goto header_no_avih;
2865 if (tag != GST_RIFF_TAG_avih)
2866 goto header_no_avih;
2868 if (!gst_avi_demux_parse_avih (GST_ELEMENT (avi), sub, &avi->avih))
2869 goto header_wrong_avih;
2871 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
2873 /* now, read the elements from the header until the end */
2874 while (gst_riff_parse_chunk (GST_ELEMENT (avi), buf, &offset, &tag,
2876 /* sub can be NULL on empty tags */
2881 case GST_RIFF_TAG_LIST:
2882 if (GST_BUFFER_SIZE (sub) < 4)
2885 switch (GST_READ_UINT32_LE (GST_BUFFER_DATA (sub))) {
2886 case GST_RIFF_LIST_strl:
2887 if (!(gst_avi_demux_parse_stream (avi, sub))) {
2888 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
2889 ("failed to parse stream, ignoring"));
2893 case GST_RIFF_LIST_odml:
2894 gst_avi_demux_parse_odml (avi, sub);
2897 GST_WARNING_OBJECT (avi,
2898 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
2899 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA
2902 case GST_RIFF_TAG_JUNK:
2907 GST_WARNING_OBJECT (avi,
2908 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
2909 offset, GST_FOURCC_ARGS (tag));
2911 case GST_RIFF_TAG_JUNK:
2913 /* move to next chunk */
2914 gst_buffer_unref (sub);
2919 gst_buffer_unref (buf);
2920 GST_DEBUG ("elements parsed");
2922 /* check parsed streams */
2923 if (avi->num_streams == 0) {
2925 } else if (avi->num_streams != avi->avih->streams) {
2926 GST_WARNING_OBJECT (avi,
2927 "Stream header mentioned %d streams, but %d available",
2928 avi->avih->streams, avi->num_streams);
2930 GST_DEBUG ("Get junk and info next");
2931 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
2933 /* Need more data */
2937 case GST_AVI_DEMUX_HEADER_INFO:
2938 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
2940 if (gst_adapter_available (avi->adapter) < 12)
2943 data = gst_adapter_peek (avi->adapter, 12);
2944 tag = GST_READ_UINT32_LE (data);
2945 size = GST_READ_UINT32_LE (data + 4);
2946 ltag = GST_READ_UINT32_LE (data + 8);
2948 if (tag == GST_RIFF_TAG_LIST) {
2950 case GST_RIFF_LIST_movi:
2951 gst_adapter_flush (avi->adapter, 12);
2954 case GST_RIFF_LIST_INFO:
2955 GST_DEBUG ("Found INFO chunk");
2956 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2958 gst_adapter_flush (avi->adapter, 12);
2959 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
2960 gst_riff_parse_info (GST_ELEMENT (avi), buf, &avi->globaltags);
2961 gst_buffer_unref (buf);
2963 avi->offset += ((size + 1) & ~1) - 4;
2965 /* Need more data */
2970 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2971 avi->offset += 8 + ((size + 1) & ~1);
2972 gst_adapter_flush (avi->adapter, 8 + ((size + 1) & ~1));
2973 // ??? goto iterate; ???
2975 /* Need more data */
2981 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2982 avi->offset += 8 + ((size + 1) & ~1);
2983 gst_adapter_flush (avi->adapter, 8 + ((size + 1) & ~1));
2986 /* Need more data */
2993 GST_WARNING ("unhandled header state: %d", avi->header_state);
2998 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
2999 avi->num_streams, avi->stream[0].indexes);
3001 GST_DEBUG ("Found movi chunk. Starting to stream data");
3002 avi->state = GST_AVI_DEMUX_MOVI;
3005 /*GList *index = NULL, *alloc = NULL; */
3007 /* ######################## this need to be integrated with the state */
3008 /* create or read stream index (for seeking) */
3009 if (avi->stream[0].indexes != NULL) {
3010 gst_avi_demux_read_subindexes_push (avi, &index, &alloc);
3013 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX) {
3014 gst_avi_demux_stream_index (avi, &index, &alloc);
3016 /* some indexes are incomplete, continue streaming from there */
3018 gst_avi_demux_stream_scan (avi, &index, &alloc);
3021 /* this is a fatal error */
3025 if (!gst_avi_demux_massage_index (avi, index, alloc))
3028 gst_avi_demux_calculate_durations_from_index (avi);
3029 /* ######################## */
3032 /* create initial NEWSEGMENT event */
3033 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3034 stop = avi->segment.duration;
3036 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3038 if (avi->seek_event)
3039 gst_event_unref (avi->seek_event);
3040 avi->seek_event = gst_event_new_new_segment
3041 (FALSE, avi->segment.rate, GST_FORMAT_TIME,
3042 avi->segment.start, stop, avi->segment.start);
3044 /* at this point we know all the streams and we can signal the no more
3046 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3047 gst_element_no_more_pads (GST_ELEMENT (avi));
3054 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3055 return GST_FLOW_ERROR;
3059 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3060 ("Invalid AVI header (no LIST at start): %"
3061 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3062 return GST_FLOW_ERROR;
3066 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3067 ("Invalid AVI header (no hdrl at start): %"
3068 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3069 gst_buffer_unref (buf);
3070 return GST_FLOW_ERROR;
3074 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3075 ("Invalid AVI header (no avih at start): %"
3076 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3078 gst_buffer_unref (sub);
3080 gst_buffer_unref (buf);
3081 return GST_FLOW_ERROR;
3085 gst_buffer_unref (buf);
3086 return GST_FLOW_ERROR;
3091 * Read full AVI headers.
3093 static GstFlowReturn
3094 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
3097 GstBuffer *buf, *sub = NULL;
3099 GList *index = NULL, *alloc = NULL;
3102 GstElement *element = GST_ELEMENT_CAST (avi);
3104 /* the header consists of a 'hdrl' LIST tag */
3105 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3106 if (res != GST_FLOW_OK)
3107 goto pull_range_failed;
3108 else if (tag != GST_RIFF_TAG_LIST)
3110 else if (GST_BUFFER_SIZE (buf) < 4)
3113 GST_DEBUG_OBJECT (avi, "parsing headers");
3115 /* Find the 'hdrl' LIST tag */
3116 while (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl) {
3117 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
3118 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf))));
3121 gst_buffer_unref (buf);
3123 /* read new chunk */
3124 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3125 if (res != GST_FLOW_OK)
3126 goto pull_range_failed;
3127 else if (tag != GST_RIFF_TAG_LIST)
3129 else if (GST_BUFFER_SIZE (buf) < 4)
3133 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
3135 /* the hdrl starts with a 'avih' header */
3136 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
3138 else if (tag != GST_RIFF_TAG_avih)
3140 else if (!gst_avi_demux_parse_avih (element, sub, &avi->avih))
3143 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3145 /* now, read the elements from the header until the end */
3146 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
3147 /* sub can be NULL on empty tags */
3152 case GST_RIFF_TAG_LIST:
3157 if (GST_BUFFER_SIZE (sub) < 4)
3160 data = GST_BUFFER_DATA (sub);
3161 fourcc = GST_READ_UINT32_LE (data);
3164 case GST_RIFF_LIST_strl:
3165 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3166 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3167 ("faile to parse stream, ignoring"));
3171 case GST_RIFF_LIST_odml:
3172 gst_avi_demux_parse_odml (avi, sub);
3175 GST_WARNING_OBJECT (avi,
3176 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3177 GST_FOURCC_ARGS (fourcc));
3178 GST_MEMDUMP_OBJECT (avi, "Unknown list", GST_BUFFER_DATA (sub),
3179 GST_BUFFER_SIZE (sub));
3181 case GST_RIFF_TAG_JUNK:
3187 GST_WARNING_OBJECT (avi,
3188 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
3189 GST_FOURCC_ARGS (tag), offset);
3190 GST_MEMDUMP_OBJECT (avi, "Unknown tag", GST_BUFFER_DATA (sub),
3191 GST_BUFFER_SIZE (sub));
3193 case GST_RIFF_TAG_JUNK:
3196 gst_buffer_unref (sub);
3201 gst_buffer_unref (buf);
3202 GST_DEBUG ("elements parsed");
3204 /* check parsed streams */
3205 if (avi->num_streams == 0)
3207 else if (avi->num_streams != avi->avih->streams) {
3208 GST_WARNING_OBJECT (avi,
3209 "Stream header mentioned %d streams, but %d available",
3210 avi->avih->streams, avi->num_streams);
3213 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
3214 G_GUINT64_FORMAT, avi->offset);
3216 /* Now, find the data (i.e. skip all junk between header and data) */
3221 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
3222 if (res != GST_FLOW_OK) {
3223 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
3224 goto pull_range_failed;
3225 } else if (GST_BUFFER_SIZE (buf) < 12) {
3226 GST_DEBUG_OBJECT (avi, "got %d bytes which is less than 12 bytes",
3227 GST_BUFFER_SIZE (buf));
3228 gst_buffer_unref (buf);
3229 return GST_FLOW_ERROR;
3232 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
3233 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
3234 ltag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 8);
3236 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
3237 GST_FOURCC_ARGS (tag), size);
3238 GST_MEMDUMP ("Tag content", GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
3239 gst_buffer_unref (buf);
3242 case GST_RIFF_TAG_LIST:{
3244 case GST_RIFF_LIST_movi:
3245 GST_DEBUG_OBJECT (avi,
3246 "Reached the 'movi' tag, we're done with skipping");
3248 case GST_RIFF_LIST_INFO:
3250 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3252 if (res != GST_FLOW_OK) {
3253 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3254 goto pull_range_failed;
3256 GST_DEBUG ("got size %u", GST_BUFFER_SIZE (buf));
3258 sub = gst_buffer_create_sub (buf, 4, GST_BUFFER_SIZE (buf) - 4);
3259 gst_riff_parse_info (element, sub, &avi->globaltags);
3261 gst_buffer_unref (sub);
3264 gst_buffer_unref (buf);
3265 /* gst_riff_read_chunk() has already advanced avi->offset */
3268 GST_WARNING_OBJECT (avi,
3269 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3270 GST_FOURCC_ARGS (ltag));
3271 avi->offset += 8 + ((size + 1) & ~1);
3277 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3278 GST_FOURCC_ARGS (tag));
3280 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3281 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3282 avi->offset += 8 + ((size + 1) & ~1);
3288 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3289 avi->num_streams, avi->stream[0].indexes);
3291 /* create or read stream index (for seeking) */
3292 if (avi->stream[0].indexes != NULL) {
3293 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3294 gst_avi_demux_read_subindexes_pull (avi, &index, &alloc);
3297 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX) {
3298 gst_avi_demux_stream_index (avi, &index, &alloc);
3300 /* some indexes are incomplete, continue streaming from there */
3302 gst_avi_demux_stream_scan (avi, &index, &alloc);
3305 /* this is a fatal error */
3309 if (!gst_avi_demux_massage_index (avi, index, alloc))
3312 gst_avi_demux_calculate_durations_from_index (avi);
3314 /* create initial NEWSEGMENT event */
3315 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3316 stop = avi->segment.duration;
3318 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3320 if (avi->seek_event)
3321 gst_event_unref (avi->seek_event);
3322 avi->seek_event = gst_event_new_new_segment
3323 (FALSE, avi->segment.rate, GST_FORMAT_TIME,
3324 avi->segment.start, stop, avi->segment.start);
3326 /* at this point we know all the streams and we can signal the no more
3328 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3329 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3336 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3337 ("Invalid AVI header (no LIST at start): %"
3338 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3339 gst_buffer_unref (buf);
3340 return GST_FLOW_ERROR;
3344 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3345 ("Invalid AVI header (no hdrl at start): %"
3346 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3347 gst_buffer_unref (buf);
3348 return GST_FLOW_ERROR;
3352 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3353 ("Invalid AVI header (no avih at start): %"
3354 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3356 gst_buffer_unref (sub);
3357 gst_buffer_unref (buf);
3358 return GST_FLOW_ERROR;
3362 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3363 ("Invalid AVI header (cannot parse avih at start)"));
3365 gst_buffer_unref (sub);
3366 gst_buffer_unref (buf);
3367 return GST_FLOW_ERROR;
3371 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3372 return GST_FLOW_ERROR;
3376 GST_WARNING ("file without or too big index");
3377 g_list_free (index);
3378 g_list_foreach (alloc, (GFunc) g_free, NULL);
3379 g_list_free (alloc);
3381 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3382 ("Could not get/create index"));
3383 return GST_FLOW_ERROR;
3387 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3388 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3389 return GST_FLOW_ERROR;
3394 * Do the actual seeking.
3397 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
3399 GstClockTime seek_time;
3401 gst_avi_index_entry *entry, *kentry;
3404 seek_time = segment->last_stop;
3405 keyframe = !!(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
3407 /* FIXME: if we seek in an openDML file, we will have multiple
3408 * primary levels. Seeking in between those will cause havoc. */
3410 /* save old position so we can see if we must mark a discont. */
3411 old_entry = avi->current_entry;
3413 /* get the entry for the requested position, which is always in last_stop.
3414 * we search the index entry for stream 0, since all entries are sorted by
3415 * time and stream we automagically are positioned for the other streams as
3416 * well. FIXME, this code assumes the main stream with keyframes is stream 0,
3417 * which is mostly correct... */
3418 if (!(entry = gst_avi_demux_index_entry_for_time (avi, 0, seek_time)))
3421 GST_DEBUG_OBJECT (avi,
3422 "Got requested entry %d [stream:%d / ts:%" GST_TIME_FORMAT
3423 " / duration:%" GST_TIME_FORMAT "]", entry->index_nr,
3424 entry->stream_nr, GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (entry->dur));
3426 /* check if we are already on a keyframe */
3427 if (!(entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)) {
3428 /* now go to the previous keyframe, this is where we should start
3430 if (!(kentry = gst_avi_demux_index_prev (avi, 0, entry->index_nr,
3431 GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME))) {
3435 /* we were on a keyframe */
3439 GST_DEBUG_OBJECT (avi,
3440 "Got keyframe entry %d [stream:%d / ts:%" GST_TIME_FORMAT
3441 " / duration:%" GST_TIME_FORMAT "]", kentry->index_nr,
3442 entry->stream_nr, GST_TIME_ARGS (kentry->ts),
3443 GST_TIME_ARGS (kentry->dur));
3445 /* we must start decoding at the keyframe */
3446 avi->current_entry = kentry->index_nr;
3448 if (segment->rate < 0.0) {
3449 gst_avi_index_entry *next_keyframe;
3451 /* Because we don't know the frame order we need to push from the prev keyframe
3452 * to the next keyframe. If there is a smart decoder downstream he will notice
3453 * that there are too many encoded frames send and return UNEXPECTED when there
3454 * are enough decoded frames to fill the segment.
3457 gst_avi_demux_index_next (avi, 0, kentry->index_nr,
3458 GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME);
3460 next_keyframe = gst_avi_demux_index_last (avi, 0);
3462 avi->reverse_start_index = kentry->index_nr;
3463 avi->reverse_stop_index = next_keyframe->index_nr;
3465 GST_DEBUG_OBJECT (avi, "reverse seek: start idx (%d) and stop idx (%d)",
3466 avi->reverse_start_index, avi->reverse_stop_index);
3470 /* when seeking to a keyframe, we update the result seek time
3471 * to the time of the keyframe. */
3472 seek_time = avi->index_entries[avi->current_entry].ts;
3476 /* if we changed position, mark a DISCONT on all streams */
3477 if (avi->current_entry != old_entry) {
3480 for (i = 0; i < avi->num_streams; i++) {
3481 avi->stream[i].discont = TRUE;
3485 GST_DEBUG_OBJECT (avi, "seek: %" GST_TIME_FORMAT
3486 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
3488 /* the seek time is also the last_stop and stream time */
3489 segment->last_stop = seek_time;
3490 segment->time = seek_time;
3496 /* we could not find an entry for the given time */
3497 GST_WARNING_OBJECT (avi,
3498 "Couldn't find AviIndexEntry for time:%" GST_TIME_FORMAT,
3499 GST_TIME_ARGS (seek_time));
3500 if (avi->current_entry >= avi->index_size && avi->index_size > 0)
3501 avi->current_entry = avi->index_size - 1;
3508 * Handle seek event.
3511 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
3516 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
3520 GstSegment seeksegment = { 0, };
3523 GST_DEBUG_OBJECT (avi, "doing seek with event");
3525 gst_event_parse_seek (event, &rate, &format, &flags,
3526 &cur_type, &cur, &stop_type, &stop);
3528 /* we have to have a format as the segment format. Try to convert
3530 if (format != GST_FORMAT_TIME) {
3531 GstFormat fmt = GST_FORMAT_TIME;
3532 gboolean res = TRUE;
3534 if (cur_type != GST_SEEK_TYPE_NONE)
3535 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
3536 if (res && stop_type != GST_SEEK_TYPE_NONE)
3537 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
3543 GST_DEBUG_OBJECT (avi,
3544 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
3545 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
3546 /* FIXME: can we do anything with rate!=1.0 */
3548 GST_DEBUG_OBJECT (avi, "doing seek without event");
3553 /* save flush flag */
3554 flush = flags & GST_SEEK_FLAG_FLUSH;
3557 GstEvent *event = gst_event_new_flush_start ();
3559 /* for a flushing seek, we send a flush_start on all pads. This will
3560 * eventually stop streaming with a WRONG_STATE. We can thus eventually
3561 * take the STREAM_LOCK. */
3562 GST_DEBUG_OBJECT (avi, "sending flush start");
3563 gst_avi_demux_push_event (avi, gst_event_ref (event));
3564 gst_pad_push_event (avi->sinkpad, event);
3566 /* a non-flushing seek, we PAUSE the task so that we can take the
3568 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
3569 gst_pad_pause_task (avi->sinkpad);
3572 /* wait for streaming to stop */
3573 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
3574 GST_PAD_STREAM_LOCK (avi->sinkpad);
3576 /* copy segment, we need this because we still need the old
3577 * segment when we close the current segment. */
3578 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
3581 GST_DEBUG_OBJECT (avi, "configuring seek");
3582 gst_segment_set_seek (&seeksegment, rate, format, flags,
3583 cur_type, cur, stop_type, stop, &update);
3586 /* do the seek, seeksegment.last_stop contains the new position, this
3587 * actually never fails. */
3588 gst_avi_demux_do_seek (avi, &seeksegment);
3593 GST_DEBUG_OBJECT (avi, "sending flush stop");
3594 gst_avi_demux_push_event (avi, gst_event_new_flush_stop ());
3595 gst_pad_push_event (avi->sinkpad, gst_event_new_flush_stop ());
3596 /* reset the last flow and mark discont, FLUSH is always DISCONT */
3597 for (i = 0; i < avi->num_streams; i++) {
3598 avi->stream[i].last_flow = GST_FLOW_OK;
3599 avi->stream[i].discont = TRUE;
3601 } else if (avi->segment_running) {
3604 /* we are running the current segment and doing a non-flushing seek,
3605 * close the segment first based on the last_stop. */
3606 GST_DEBUG_OBJECT (avi, "closing running segment %" G_GINT64_FORMAT
3607 " to %" G_GINT64_FORMAT, avi->segment.start, avi->segment.last_stop);
3608 seg = gst_event_new_new_segment (TRUE,
3609 avi->segment.rate, avi->segment.format,
3610 avi->segment.start, avi->segment.last_stop, avi->segment.time);
3611 gst_avi_demux_push_event (avi, seg);
3614 /* now update the real segment info */
3615 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
3617 /* post the SEGMENT_START message when we do segmented playback */
3618 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3619 gst_element_post_message (GST_ELEMENT (avi),
3620 gst_message_new_segment_start (GST_OBJECT (avi),
3621 avi->segment.format, avi->segment.last_stop));
3624 /* prepare for streaming again */
3625 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3626 stop = avi->segment.duration;
3628 /* queue the segment event for the streaming thread. */
3629 if (avi->seek_event)
3630 gst_event_unref (avi->seek_event);
3631 if (avi->segment.rate > 0.0) {
3632 avi->seek_event = gst_event_new_new_segment (FALSE,
3633 avi->segment.rate, avi->segment.format,
3634 avi->segment.last_stop, stop, avi->segment.time);
3636 avi->seek_event = gst_event_new_new_segment (FALSE,
3637 avi->segment.rate, avi->segment.format,
3638 avi->segment.start, avi->segment.last_stop, avi->segment.start);
3641 if (!avi->streaming) {
3642 avi->segment_running = TRUE;
3643 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
3646 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
3653 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
3659 * Helper for gst_avi_demux_invert()
3662 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
3664 memcpy (tmp, d1, bytes);
3665 memcpy (d1, d2, bytes);
3666 memcpy (d2, tmp, bytes);
3670 #define gst_avi_demux_is_uncompressed(fourcc) \
3671 (fourcc == GST_RIFF_DIB || \
3672 fourcc == GST_RIFF_rgb || \
3673 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW)
3676 * Invert DIB buffers... Takes existing buffer and
3677 * returns either the buffer or a new one (with old
3678 * one dereferenced).
3679 * FIXME: can't we preallocate tmp? and remember stride, bpp?
3682 gst_avi_demux_invert (avi_stream_context * stream, GstBuffer * buf)
3689 if (stream->strh->type != GST_RIFF_FCC_vids)
3692 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
3693 return buf; /* Ignore non DIB buffers */
3696 s = gst_caps_get_structure (GST_PAD_CAPS (stream->pad), 0);
3697 if (!gst_structure_get_int (s, "bpp", &bpp)) {
3698 GST_WARNING ("Failed to retrieve depth from caps");
3702 if (stream->strf.vids == NULL) {
3703 GST_WARNING ("Failed to retrieve vids for stream");
3707 h = stream->strf.vids->height;
3708 w = stream->strf.vids->width;
3709 stride = w * (bpp / 8);
3711 buf = gst_buffer_make_writable (buf);
3712 if (GST_BUFFER_SIZE (buf) < (stride * h)) {
3713 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
3717 tmp = g_malloc (stride);
3719 for (y = 0; y < h / 2; y++) {
3720 swap_line (GST_BUFFER_DATA (buf) + stride * y,
3721 GST_BUFFER_DATA (buf) + stride * (h - 1 - y), tmp, stride);
3730 * Returns the aggregated GstFlowReturn.
3732 static GstFlowReturn
3733 gst_avi_demux_combine_flows (GstAviDemux * avi, avi_stream_context * stream,
3738 /* store the value */
3739 stream->last_flow = ret;
3741 /* any other error that is not-linked can be returned right away */
3742 if (G_UNLIKELY (ret != GST_FLOW_NOT_LINKED))
3745 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3746 for (i = 0; i < avi->num_streams; i++) {
3747 avi_stream_context *ostream = &avi->stream[i];
3749 ret = ostream->last_flow;
3750 /* some other return value (must be SUCCESS but we can return
3751 * other values as well) */
3752 if (G_UNLIKELY (ret != GST_FLOW_NOT_LINKED))
3755 /* if we get here, all other pads were unlinked and we return
3756 * NOT_LINKED then */
3758 GST_LOG_OBJECT (avi, "combined return %s", gst_flow_get_name (ret));
3763 * prepare the avi element for a reverse jump to a prev keyframe
3764 * this function will return the start entry. if the function returns
3765 * NULL there was no prev keyframe.
3767 static gst_avi_index_entry *
3768 gst_avi_demux_step_reverse (GstAviDemux * avi)
3770 gst_avi_index_entry *entry;
3773 avi->reverse_stop_index = avi->reverse_start_index;
3775 gst_avi_demux_index_prev (avi, 0, avi->reverse_stop_index,
3776 GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME);
3778 GST_DEBUG_OBJECT (avi, "no valid index entry found index %d",
3779 avi->reverse_stop_index);
3782 avi->current_entry = avi->reverse_start_index = entry->index_nr;
3783 GST_DEBUG_OBJECT (avi,
3784 "reverse playback jump: start idx (%d) and stop idx (%d)",
3785 avi->reverse_start_index, avi->reverse_stop_index);
3786 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, entry->ts);
3787 for (i = 0; i < avi->num_streams; i++) {
3788 avi->stream[i].last_flow = GST_FLOW_OK;
3789 avi->stream[i].discont = TRUE;
3795 * Read data from one index entry
3797 static GstFlowReturn
3798 gst_avi_demux_process_next_entry (GstAviDemux * avi)
3800 GstFlowReturn res = GST_FLOW_OK;
3801 gboolean processed = FALSE;
3802 avi_stream_context *stream;
3803 gst_avi_index_entry *entry;
3807 /* see if we are at the end */
3808 if ((avi->segment.rate > 0 && avi->current_entry >= avi->index_size))
3811 /* get next entry, this will work as we checked for the index size above */
3812 entry = &avi->index_entries[avi->current_entry++];
3814 /* check for reverse playback */
3815 if (avi->segment.rate < 0 && avi->current_entry > avi->reverse_stop_index) {
3816 GST_LOG_OBJECT (avi, "stop_index %d reached", avi->reverse_stop_index);
3818 /* check if we have pushed enough data for this segment */
3819 if (avi->reverse_start_index == 0)
3820 goto eos_reverse_zero;
3821 if (avi->index_entries[avi->reverse_start_index].ts < avi->segment.start)
3822 goto eos_reverse_segment;
3824 if (!(entry = gst_avi_demux_step_reverse (avi)))
3827 avi->current_entry++;
3830 /* see if we have a valid stream, ignore if not
3831 * FIXME: can't we check this when building the index?
3832 * we check it in _parse_index(), _stream_scan()
3834 if (entry->stream_nr >= avi->num_streams) {
3835 GST_WARNING_OBJECT (avi,
3836 "Entry %d has non-existing stream nr %d",
3837 avi->current_entry - 1, entry->stream_nr);
3841 /* get stream now */
3842 stream = &avi->stream[entry->stream_nr];
3844 if (avi->segment.rate > 0.0) {
3845 /* only check this for fowards playback for now */
3846 if ((entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)
3847 && GST_CLOCK_TIME_IS_VALID (entry->ts)
3848 && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
3849 && (entry->ts > avi->segment.stop)) {
3854 /* skip empty entries */
3855 if (entry->size == 0 || !stream->pad) {
3856 GST_DEBUG_OBJECT (avi, "Skipping entry %d (%d, %p)",
3857 avi->current_entry - 1, entry->size, stream->pad);
3861 GST_LOG ("reading buffer (size=%d) from stream %d at current pos %"
3862 G_GUINT64_FORMAT " (%llx)", entry->size, entry->stream_nr,
3863 avi->index_offset + entry->offset, avi->index_offset + entry->offset);
3865 /* pull in the data */
3866 res = gst_pad_pull_range (avi->sinkpad, entry->offset +
3867 avi->index_offset, entry->size, &buf);
3868 if (res != GST_FLOW_OK)
3871 /* check for short buffers, this is EOS as well */
3872 if (GST_BUFFER_SIZE (buf) < entry->size)
3875 /* invert the picture if needed */
3876 buf = gst_avi_demux_invert (stream, buf);
3878 /* mark non-keyframes */
3879 if (!(entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME))
3880 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
3882 GST_BUFFER_TIMESTAMP (buf) = entry->ts;
3883 GST_BUFFER_DURATION (buf) = entry->dur;
3884 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
3885 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
3886 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
3888 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %d and time %"
3889 GST_TIME_FORMAT " on pad %s",
3890 GST_BUFFER_SIZE (buf), GST_TIME_ARGS (entry->ts),
3891 GST_PAD_NAME (stream->pad));
3893 /* update current position in the segment */
3894 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, entry->ts);
3896 /* mark discont when pending */
3897 if (stream->discont) {
3898 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
3899 stream->discont = FALSE;
3902 res = gst_pad_push (stream->pad, buf);
3904 /* mark as processed, we increment the frame and byte counters then
3905 * leave the while loop and return the GstFlowReturn */
3907 GST_DEBUG_OBJECT (avi, "Processed buffer %d: %s", entry->index_nr,
3908 gst_flow_get_name (res));
3910 if (avi->segment.rate < 0
3911 && entry->ts > avi->segment.stop && res == GST_FLOW_UNEXPECTED) {
3912 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
3913 * we are at the end of the segment, so we just need to jump
3914 * back to the previous section.
3916 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
3918 if (!(entry = gst_avi_demux_step_reverse (avi)))
3923 stream->current_frame = entry->frames_before;
3924 stream->current_byte = entry->bytes_before;
3930 res = gst_avi_demux_combine_flows (avi, stream, res);
3933 stream->current_frame = entry->frames_before + 1;
3934 stream->current_byte = entry->bytes_before + entry->size;
3935 } while (!processed);
3938 GST_DEBUG_OBJECT (avi, "returning %s", gst_flow_get_name (res));
3945 GST_LOG_OBJECT (avi, "Handled last index entry, setting EOS (%d > %d)",
3946 avi->current_entry, avi->index_size);
3947 /* we mark the first stream as EOS */
3948 res = GST_FLOW_UNEXPECTED;
3953 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
3954 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
3955 GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (avi->segment.stop));
3956 res = GST_FLOW_UNEXPECTED;
3961 GST_DEBUG_OBJECT (avi, "start_index was 0, setting EOS");
3962 res = GST_FLOW_UNEXPECTED;
3965 eos_reverse_segment:
3967 GST_DEBUG_OBJECT (avi, "full segment pushed, setting EOS");
3968 res = GST_FLOW_UNEXPECTED;
3973 GST_DEBUG_OBJECT (avi,
3974 "pull range failed: pos=%" G_GUINT64_FORMAT " size=%d",
3975 entry->offset + avi->index_offset, entry->size);
3980 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
3981 ", only got %d/%d bytes (truncated file?)", entry->offset +
3982 avi->index_offset, GST_BUFFER_SIZE (buf), entry->size);
3983 gst_buffer_unref (buf);
3984 res = GST_FLOW_UNEXPECTED;
3990 * Read data. If we have an index it delegates to
3991 * gst_avi_demux_process_next_entry().
3993 static GstFlowReturn
3994 gst_avi_demux_stream_data (GstAviDemux * avi)
3999 GstFlowReturn res = GST_FLOW_OK;
4000 GstFormat format = GST_FORMAT_TIME;
4002 /* if we have a avi->index_entries[], we don't want to read
4003 * the stream linearly, but seek to the next ts/index_entry. */
4004 if (G_LIKELY (avi->index_entries != NULL))
4005 return gst_avi_demux_process_next_entry (avi);
4007 if (G_UNLIKELY (avi->have_eos)) {
4008 /* Clean adapter, we're done */
4009 gst_adapter_clear (avi->adapter);
4014 if (!gst_avi_demux_sync (avi, &tag, FALSE))
4018 /* Iterate until need more data, so adapter won't grow too much */
4020 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
4024 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
4025 GST_FOURCC_ARGS (tag), size);
4027 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
4028 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
4029 GST_LOG ("Chunk ok");
4030 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
4031 GST_DEBUG ("Found sub-index tag");
4032 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
4033 if ((size > 0) && (size != -1)) {
4034 GST_DEBUG (" skipping %d bytes for now", size);
4035 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4039 } else if (tag == GST_RIFF_TAG_JUNK) {
4040 GST_DEBUG ("JUNK chunk, skipping");
4041 } else if (tag == GST_RIFF_TAG_idx1) {
4042 GST_DEBUG ("Found index tag, stream done");
4043 avi->have_eos = TRUE;
4044 return GST_FLOW_UNEXPECTED;
4045 } else if (tag == GST_RIFF_TAG_LIST) {
4046 /* movi chunks might be grouped in rec list */
4047 if (gst_adapter_available (avi->adapter) >= 12) {
4048 GST_DEBUG ("Found LIST tag, skipping LIST header");
4049 gst_adapter_flush (avi->adapter, 12);
4053 } else if (tag == GST_RIFF_TAG_JUNK) {
4054 /* rec list might contain JUNK chunks */
4055 GST_DEBUG ("Found JUNK tag");
4056 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
4057 if ((size > 0) && (size != -1)) {
4058 GST_DEBUG (" skipping %d bytes for now", size);
4059 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4065 GST_DEBUG ("No more stream chunks, send EOS");
4066 avi->have_eos = TRUE;
4067 return GST_FLOW_UNEXPECTED;
4070 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
4071 if ((size == 0) || (size == -1))
4072 gst_adapter_flush (avi->adapter, 8);
4075 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
4076 GST_FOURCC_ARGS (tag), size);
4078 stream_nr = CHUNKID_TO_STREAMNR (tag);
4080 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
4082 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
4083 stream_nr, GST_FOURCC_ARGS (tag));
4084 avi->offset += 8 + ((size + 1) & ~1);
4085 gst_adapter_flush (avi->adapter, 8 + ((size + 1) & ~1));
4087 avi_stream_context *stream;
4088 GstClockTime next_ts = 0;
4091 gst_adapter_flush (avi->adapter, 8);
4094 buf = gst_adapter_take_buffer (avi->adapter, ((size + 1) & ~1));
4095 /* patch the size */
4096 GST_BUFFER_SIZE (buf) = size;
4097 avi->offset += 8 + ((size + 1) & ~1);
4099 /* get time of this buffer */
4100 stream = &avi->stream[stream_nr];
4101 gst_pad_query_position (stream->pad, &format, (gint64 *) & next_ts);
4102 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4105 /* set delay (if any)
4106 if (stream->strh->init_frames == stream->current_frame &&
4108 stream->delay = next_ts;
4111 stream->current_frame++;
4112 stream->current_byte += size;
4114 if (G_UNLIKELY (!stream->pad)) {
4115 GST_WARNING ("No pad.");
4116 gst_buffer_unref (buf);
4118 GstClockTime dur_ts = 0;
4120 /* invert the picture if needed */
4121 buf = gst_avi_demux_invert (stream, buf);
4123 gst_pad_query_position (stream->pad, &format, (gint64 *) & dur_ts);
4124 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4127 GST_BUFFER_TIMESTAMP (buf) = next_ts;
4128 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
4129 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4130 GST_DEBUG_OBJECT (avi,
4131 "Pushing buffer with time=%" GST_TIME_FORMAT
4132 " and size %d over pad %s", GST_TIME_ARGS (next_ts), size,
4133 GST_PAD_NAME (stream->pad));
4135 /* update current position in the segment */
4136 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, next_ts);
4138 /* mark discont when pending */
4139 if (G_UNLIKELY (stream->discont)) {
4140 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4141 stream->discont = FALSE;
4143 res = gst_pad_push (stream->pad, buf);
4146 res = gst_avi_demux_combine_flows (avi, stream, res);
4147 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4148 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
4161 GST_DEBUG_OBJECT (avi, "format %s != GST_FORMAT_TIME",
4162 gst_format_get_name (format));
4163 res = GST_FLOW_ERROR;
4169 * Send pending tags.
4172 push_tag_lists (GstAviDemux * avi)
4179 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
4181 for (i = 0; i < avi->num_streams; i++)
4182 if (avi->stream[i].pad && avi->stream[i].taglist) {
4183 GST_DEBUG_OBJECT (avi->stream[i].pad, "Tags: %" GST_PTR_FORMAT,
4184 avi->stream[i].taglist);
4185 gst_element_found_tags_for_pad (GST_ELEMENT (avi), avi->stream[i].pad,
4186 avi->stream[i].taglist);
4187 avi->stream[i].taglist = NULL;
4190 if (avi->globaltags == NULL)
4191 avi->globaltags = gst_tag_list_new ();
4193 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE,
4194 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
4196 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, avi->globaltags);
4197 gst_element_found_tags (GST_ELEMENT (avi), avi->globaltags);
4198 avi->globaltags = NULL;
4199 avi->got_tags = FALSE;
4203 gst_avi_demux_loop (GstPad * pad)
4206 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4208 switch (avi->state) {
4209 case GST_AVI_DEMUX_START:
4210 if (G_UNLIKELY ((res =
4211 gst_avi_demux_stream_init_pull (avi)) != GST_FLOW_OK)) {
4212 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4215 avi->state = GST_AVI_DEMUX_HEADER;
4217 case GST_AVI_DEMUX_HEADER:
4218 if (G_UNLIKELY ((res =
4219 gst_avi_demux_stream_header_pull (avi)) != GST_FLOW_OK)) {
4220 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4223 avi->state = GST_AVI_DEMUX_MOVI;
4225 case GST_AVI_DEMUX_MOVI:
4226 if (G_UNLIKELY (avi->seek_event)) {
4227 gst_avi_demux_push_event (avi, avi->seek_event);
4228 avi->seek_event = NULL;
4230 if (G_UNLIKELY (avi->got_tags)) {
4231 push_tag_lists (avi);
4233 /* process each index entry in turn */
4234 res = gst_avi_demux_stream_data (avi);
4236 /* pause when error */
4237 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4238 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
4243 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
4244 res = GST_FLOW_ERROR;
4248 GST_LOG_OBJECT (avi, "state: %d res:%s", avi->state, gst_flow_get_name (res));
4254 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
4255 avi->segment_running = FALSE;
4256 gst_pad_pause_task (avi->sinkpad);
4258 if (GST_FLOW_IS_FATAL (res) || (res == GST_FLOW_NOT_LINKED)) {
4259 gboolean push_eos = TRUE;
4261 if (res == GST_FLOW_UNEXPECTED) {
4262 /* handle end-of-stream/segment */
4263 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4266 if ((stop = avi->segment.stop) == -1)
4267 stop = avi->segment.duration;
4269 GST_INFO_OBJECT (avi, "sending segment_done");
4271 gst_element_post_message
4273 gst_message_new_segment_done (GST_OBJECT (avi), GST_FORMAT_TIME,
4278 /* for fatal errors we post an error message */
4279 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
4280 (_("Internal data stream error.")),
4281 ("streaming stopped, reason %s", gst_flow_get_name (res)));
4284 GST_INFO_OBJECT (avi, "sending eos");
4285 gst_avi_demux_push_event (avi, gst_event_new_eos ());
4291 static GstFlowReturn
4292 gst_avi_demux_chain (GstPad * pad, GstBuffer * buf)
4295 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4297 GST_DEBUG ("Store %d bytes in adapter", GST_BUFFER_SIZE (buf));
4298 gst_adapter_push (avi->adapter, buf);
4300 switch (avi->state) {
4301 case GST_AVI_DEMUX_START:
4302 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
4303 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4307 case GST_AVI_DEMUX_HEADER:
4308 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
4309 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4313 case GST_AVI_DEMUX_MOVI:
4314 if (G_UNLIKELY (avi->seek_event)) {
4315 gst_avi_demux_push_event (avi, avi->seek_event);
4316 avi->seek_event = NULL;
4318 if (G_UNLIKELY (avi->got_tags)) {
4319 push_tag_lists (avi);
4321 res = gst_avi_demux_stream_data (avi);
4324 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
4325 ("Illegal internal state"));
4326 res = GST_FLOW_ERROR;
4330 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
4331 gst_flow_get_name (res));
4337 gst_avi_demux_sink_activate (GstPad * sinkpad)
4339 if (gst_pad_check_pull_range (sinkpad)) {
4340 GST_DEBUG ("going to pull mode");
4341 return gst_pad_activate_pull (sinkpad, TRUE);
4343 GST_DEBUG ("going to push (streaming) mode");
4344 return gst_pad_activate_push (sinkpad, TRUE);
4349 gst_avi_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
4351 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (sinkpad));
4354 avi->segment_running = TRUE;
4355 avi->streaming = FALSE;
4356 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4359 avi->segment_running = FALSE;
4360 return gst_pad_stop_task (sinkpad);
4365 gst_avi_demux_activate_push (GstPad * pad, gboolean active)
4367 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (pad));
4370 GST_DEBUG ("avi: activating push/chain function");
4371 avi->streaming = TRUE;
4373 GST_DEBUG ("avi: deactivating push/chain function");
4379 static GstStateChangeReturn
4380 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
4382 GstStateChangeReturn ret;
4383 GstAviDemux *avi = GST_AVI_DEMUX (element);
4385 switch (transition) {
4386 case GST_STATE_CHANGE_READY_TO_PAUSED:
4387 avi->streaming = FALSE;
4388 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
4394 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4395 if (ret == GST_STATE_CHANGE_FAILURE)
4398 switch (transition) {
4399 case GST_STATE_CHANGE_PAUSED_TO_READY:
4400 gst_avi_demux_reset (avi);