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;
1871 _entry = &((gst_riff_index_entry *) data)[i];
1872 entry.id = GST_READ_UINT32_LE (&_entry->id);
1873 entry.offset = GST_READ_UINT32_LE (&_entry->offset);
1874 entry.flags = GST_READ_UINT32_LE (&_entry->flags);
1875 entry.size = GST_READ_UINT32_LE (&_entry->size);
1876 target = &entries[n];
1878 if (entry.id == GST_RIFF_rec || entry.id == 0 ||
1879 (entry.offset == 0 && n > 0))
1882 stream_nr = CHUNKID_TO_STREAMNR (entry.id);
1883 if (stream_nr >= avi->num_streams) {
1884 GST_WARNING_OBJECT (avi,
1885 "Index entry %d has invalid stream nr %d", i, stream_nr);
1888 target->stream_nr = stream_nr;
1889 stream = &avi->stream[stream_nr];
1891 if (!stream->strh) {
1892 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
1896 target->index_nr = i;
1898 (entry.flags & GST_RIFF_IF_KEYFRAME) ? GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME
1900 target->size = entry.size;
1901 target->offset = entry.offset + 8;
1903 /* figure out if the index is 0 based or relative to the MOVI start */
1905 if (target->offset < pos_before)
1906 avi->index_offset = pos_before + 8;
1908 avi->index_offset = 0;
1909 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
1912 format = GST_FORMAT_TIME;
1913 if (stream->strh->type == GST_RIFF_FCC_auds) {
1914 /* all audio frames are keyframes */
1915 target->flags |= GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
1917 #ifndef GST_DISABLE_DEBUG
1918 if (target->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)
1922 /* stream duration unknown, now we can calculate it */
1923 if (stream->idx_duration == -1)
1924 stream->idx_duration = 0;
1927 target->ts = stream->idx_duration;
1928 if (stream->is_vbr) {
1929 /* VBR stream next timestamp */
1930 if (stream->strh->type == GST_RIFF_FCC_auds) {
1931 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
1932 stream->total_blocks + 1);
1934 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
1935 stream->total_frames + 1);
1938 /* constant rate stream */
1939 next_ts = avi_stream_convert_bytes_to_time_unchecked (stream,
1940 stream->total_bytes + target->size);
1942 /* duration is next - current */
1943 target->dur = next_ts - target->ts;
1945 /* stream position */
1946 target->bytes_before = stream->total_bytes;
1947 target->frames_before = stream->total_frames;
1949 stream->total_bytes += target->size;
1950 stream->total_frames++;
1951 if (stream->strh->type == GST_RIFF_FCC_auds) {
1952 if (stream->strf.auds->blockalign > 0)
1953 stream->total_blocks +=
1954 (target->size + stream->strf.auds->blockalign -
1955 1) / stream->strf.auds->blockalign;
1957 stream->total_blocks++;
1959 stream->idx_duration = next_ts;
1961 GST_LOG_OBJECT (avi,
1962 "Adding index entry %d (%6u), flags %02x, stream %d, size %u "
1963 ", offset %" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT ", dur %"
1965 target->index_nr, stream->total_frames - 1, target->flags,
1966 target->stream_nr, target->size, target->offset,
1967 GST_TIME_ARGS (target->ts), GST_TIME_ARGS (target->dur));
1968 entries_list = g_list_prepend (entries_list, target);
1973 GST_INFO ("Parsed index, %6d entries, %5ld keyframes, entry size = %2d, "
1974 "total size = %10d", num, _nr_keyframes,
1975 (gint) sizeof (gst_avi_index_entry),
1976 (gint) (num * sizeof (gst_avi_index_entry)));
1978 gst_buffer_unref (buf);
1981 *_entries_list = g_list_reverse (entries_list);
1990 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1991 ("Cannot allocate memory for %u*%u=%u bytes",
1992 (guint) sizeof (gst_avi_index_entry), num,
1993 (guint) sizeof (gst_avi_index_entry) * num));
1994 gst_buffer_unref (buf);
1999 * gst_avi_demux_stream_index:
2000 * @avi: avi demuxer object.
2001 * @index: list of index entries, returned by this function.
2002 * @alloc_list: list of allocated data, returned by this function.
2004 * Seeks to index and reads it.
2007 gst_avi_demux_stream_index (GstAviDemux * avi,
2008 GList ** index, GList ** alloc_list)
2011 guint64 offset = avi->offset;
2017 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2022 /* get chunk information */
2023 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2024 if (res != GST_FLOW_OK)
2026 else if (GST_BUFFER_SIZE (buf) < 8)
2029 /* check tag first before blindy trying to read 'size' bytes */
2030 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2031 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2032 if (tag == GST_RIFF_TAG_LIST) {
2033 /* this is the movi tag */
2034 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2035 (8 + ((size + 1) & ~1)));
2036 offset += 8 + ((size + 1) & ~1);
2037 gst_buffer_unref (buf);
2038 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2039 if (res != GST_FLOW_OK)
2041 else if (GST_BUFFER_SIZE (buf) < 8)
2043 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2044 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2047 if (tag != GST_RIFF_TAG_idx1)
2052 gst_buffer_unref (buf);
2054 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2056 /* read chunk, advance offset */
2057 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2058 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2061 GST_INFO ("will parse index chunk size %u for tag %"
2062 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2064 gst_avi_demux_parse_index (avi, buf, index);
2066 *alloc_list = g_list_append (*alloc_list, (*index)->data);
2068 /* debug our indexes */
2069 for (i = 0; i < avi->num_streams; i++) {
2070 avi_stream_context *stream;
2072 stream = &avi->stream[i];
2073 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2074 i, stream->total_frames, stream->total_bytes);
2081 GST_DEBUG_OBJECT (avi,
2082 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2087 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2088 gst_buffer_unref (buf);
2093 GST_WARNING_OBJECT (avi,
2094 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2095 GST_FOURCC_ARGS (tag));
2096 gst_buffer_unref (buf);
2101 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2102 gst_buffer_unref (buf);
2109 * Sync to next data chunk.
2112 gst_avi_demux_skip (GstAviDemux * avi, gboolean prevent_eos)
2114 GstRiffRead *riff = GST_RIFF_READ (avi);
2117 guint64 pos, length;
2121 pos = gst_bytestream_tell (riff->bs);
2122 length = gst_bytestream_length (riff->bs);
2124 if (pos + 8 > length)
2127 if (gst_bytestream_peek_bytes (riff->bs, &data, 8) != 8)
2130 size = GST_READ_UINT32_LE (&data[4]);
2134 /* Note, we're going to skip which might involve seeks. Therefore,
2135 * we need 1 byte more! */
2136 if (pos + 8 + size >= length)
2140 return gst_riff_read_skip (riff);
2144 gst_avi_demux_sync (GstAviDemux * avi, guint32 * ret_tag, gboolean prevent_eos)
2146 GstRiffRead *riff = GST_RIFF_READ (avi);
2148 guint64 length = gst_bytestream_length (riff->bs);
2150 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2153 /* peek first (for the end of this 'list/movi' section) */
2154 if (!(tag = gst_riff_peek_tag (riff, &avi->level_up)))
2157 /* if we're at top-level, we didn't read the 'movi'
2158 * list tag yet. This can also be 'AVIX' in case of
2159 * openDML-2.0 AVI files. Lastly, it might be idx1,
2160 * in which case we skip it so we come at EOS. */
2162 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2165 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2169 case GST_RIFF_TAG_LIST:
2170 if (!(tag = gst_riff_peek_list (riff)))
2174 case GST_RIFF_LIST_AVIX:
2175 if (!gst_riff_read_list (riff, &tag))
2179 case GST_RIFF_LIST_movi:
2180 if (!gst_riff_read_list (riff, &tag))
2188 GST_WARNING ("Unknown list %" GST_FOURCC_FORMAT " before AVI data",
2189 GST_FOURCC_ARGS (tag));
2192 case GST_RIFF_TAG_JUNK:
2193 if (!gst_avi_demux_skip (avi, prevent_eos))
2200 if ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
2201 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9') {
2206 case GST_RIFF_TAG_idx1:
2207 case GST_RIFF_TAG_JUNK:
2208 if (!gst_avi_demux_skip (avi, prevent_eos)) {
2215 /* And then, we get the data */
2216 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2219 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2222 /* Support for rec-list files */
2224 case GST_RIFF_TAG_LIST:
2225 if (!(tag = gst_riff_peek_list (riff)))
2227 if (tag == GST_RIFF_rec) {
2228 /* Simply skip the list */
2229 if (!gst_riff_read_list (riff, &tag))
2231 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2236 case GST_RIFF_TAG_JUNK:
2237 gst_avi_demux_skip (avi, prevent_eos);
2249 * gst_avi_demux_peek_tag:
2251 * Returns the tag and size of the next chunk
2253 static GstFlowReturn
2254 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2257 GstFlowReturn res = GST_FLOW_OK;
2258 GstBuffer *buf = NULL;
2261 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2262 if (res != GST_FLOW_OK)
2265 bufsize = GST_BUFFER_SIZE (buf);
2269 *tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2270 *size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2272 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2273 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2274 *size, offset + 8, offset + 8 + (gint64) * size);
2276 gst_buffer_unref (buf);
2283 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2288 GST_DEBUG_OBJECT (avi, "got %d bytes which is <> 8 bytes", bufsize);
2289 res = GST_FLOW_ERROR;
2295 * gst_avi_demux_next_data_buffer:
2297 * Returns the offset and size of the next buffer
2298 * Position is the position of the buffer (after tag and size)
2300 static GstFlowReturn
2301 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2302 guint32 * tag, guint * size)
2304 guint64 off = *offset;
2309 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2310 if (res != GST_FLOW_OK)
2312 if (*tag == GST_RIFF_TAG_LIST)
2313 off += 8 + 4; /* skip tag + size + subtag */
2325 * gst_avi_demux_stream_scan:
2326 * @avi: calling element (used for debugging/errors).
2327 * @index: list of index entries, returned by this function.
2328 * @alloc_list: list of allocated data, returned by this function.
2330 * Scan the file for all chunks to "create" a new index.
2331 * Return value indicates if we can continue reading the stream. It
2332 * does not say anything about whether we created an index.
2337 gst_avi_demux_stream_scan (GstAviDemux * avi,
2338 GList ** index, GList ** alloc_list)
2341 gst_avi_index_entry *entry, *entries = NULL;
2342 avi_stream_context *stream;
2344 guint64 pos = avi->offset;
2349 guint index_size = 0;
2352 * - implement non-seekable source support.
2354 GST_DEBUG_OBJECT (avi,
2355 "Creating index %s existing index, starting at offset %" G_GUINT64_FORMAT,
2356 ((*index) ? "with" : "without"), pos);
2358 format = GST_FORMAT_BYTES;
2359 if (!gst_pad_query_peer_duration (avi->sinkpad, &format, &tmplength))
2365 entry = g_list_last (*index)->data;
2366 pos = entry->offset + avi->index_offset + entry->size;
2367 if (entry->size & 1)
2370 if (pos >= length) {
2371 GST_LOG_OBJECT (avi, "Complete index, we're done");
2375 GST_LOG_OBJECT (avi, "Incomplete index, seeking to last valid entry @ %"
2376 G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT " (%"
2377 G_GUINT64_FORMAT "+%u)", pos, length, entry->offset, entry->size);
2384 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
2385 if (G_UNLIKELY (res != GST_FLOW_OK))
2388 /* check valid stream */
2389 stream_nr = CHUNKID_TO_STREAMNR (tag);
2390 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2391 GST_WARNING_OBJECT (avi,
2392 "Index entry has invalid stream nr %d", stream_nr);
2396 stream = &avi->stream[stream_nr];
2397 if (G_UNLIKELY (stream->pad == NULL)) {
2398 GST_WARNING_OBJECT (avi,
2399 "Stream %d does not have an output pad, can't create new index",
2405 if (G_UNLIKELY (index_size % 1024 == 0)) {
2406 entries = g_new (gst_avi_index_entry, 1024);
2407 *alloc_list = g_list_prepend (*alloc_list, entries);
2409 entry = &entries[index_size % 1024];
2411 entry->index_nr = index_size++;
2412 entry->stream_nr = stream_nr;
2413 entry->flags = GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
2414 entry->offset = pos - avi->index_offset;
2417 /* timestamps, get timestamps of two consecutive frames to calculate
2418 * timestamp and duration. */
2419 format = GST_FORMAT_TIME;
2420 if (stream->is_vbr) {
2422 entry->ts = avi_stream_convert_frames_to_time_unchecked (stream,
2423 stream->total_frames);
2424 entry->dur = avi_stream_convert_frames_to_time_unchecked (stream,
2425 stream->total_frames + 1);
2427 /* constant rate stream */
2428 entry->ts = avi_stream_convert_bytes_to_time_unchecked (stream,
2429 stream->total_bytes);
2430 entry->dur = avi_stream_convert_bytes_to_time_unchecked (stream,
2431 stream->total_bytes + entry->size);
2433 entry->dur -= entry->ts;
2435 /* stream position */
2436 entry->bytes_before = stream->total_bytes;
2437 stream->total_bytes += entry->size;
2438 entry->frames_before = stream->total_frames;
2439 stream->total_frames++;
2440 stream->idx_duration = entry->ts + entry->dur;
2442 list = g_list_prepend (list, entry);
2443 GST_DEBUG_OBJECT (avi, "Added index entry %d (in stream: %d), offset %"
2444 G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT " for stream %d",
2445 index_size - 1, entry->frames_before, entry->offset,
2446 GST_TIME_ARGS (entry->ts), entry->stream_nr);
2449 /* update position */
2450 pos += GST_ROUND_UP_2 (size);
2451 if (G_UNLIKELY (pos > length)) {
2452 GST_WARNING_OBJECT (avi,
2453 "Stopping index lookup since we are further than EOF");
2458 /* FIXME: why is this disabled */
2460 while (gst_avi_demux_sync (avi, &tag, TRUE)) {
2461 guint stream_nr = CHUNKID_TO_STREAMNR (tag);
2463 GstFormat format = GST_FORMAT_TIME;
2465 if (stream_nr >= avi->num_streams)
2467 stream = &avi->stream[stream_nr];
2469 /* get chunk size */
2470 if (gst_bytestream_peek_bytes (riff->bs, &data, 8) != 8)
2474 entry->index_nr = index_size++;
2475 entry->stream_nr = stream_nr;
2476 entry->flags = GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
2477 entry->offset = gst_bytestream_tell (riff->bs) + 8 - avi->index_offset;
2478 entry->size = GST_READ_UINT32_LE (&data[4]);
2481 if (stream->is_vbr) {
2483 entry->ts = avi_stream_convert_frames_to_time_unchecked (stream,
2484 stream->total_frames);
2485 entry->dur = avi_stream_convert_frames_to_time_unchecked (stream,
2486 stream->total_frames + 1);
2488 /* constant rate stream */
2489 entry->ts = avi_stream_convert_bytes_to_time_unchecked (stream,
2490 stream->total_bytes);
2491 entry->dur = avi_stream_convert_bytes_to_time_unchecked (stream,
2492 stream->total_bytes + entry->size);
2494 entry->dur -= entry->ts;
2496 /* stream position */
2497 entry->bytes_before = stream->total_bytes;
2498 stream->total_bytes += entry->size;
2499 entry->frames_before = stream->total_frames;
2500 stream->total_frames++;
2502 list = g_list_prepend (list, entry);
2503 GST_DEBUG_OBJECT (avi, "Added index entry %d (in stream: %d), offset %"
2504 G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT " for stream %d",
2505 index_size - 1, entry->frames_before, entry->offset,
2506 GST_TIME_ARGS (entry->ts), entry->stream_nr);
2509 if (!gst_avi_demux_skip (avi, TRUE))
2513 if (!(event = gst_riff_read_seek (riff, pos))) {
2517 gst_event_unref (event);
2521 GST_DEBUG_OBJECT (avi, "index created, %d items", index_size);
2523 *index = g_list_concat (*index, g_list_reverse (list));
2529 * gst_avi_demux_massage_index:
2530 * @avi: calling element (used for debugging/errors).
2532 * We're going to go over each entry in the index and finetune
2533 * some things we don't like about AVI. For example, a single
2534 * chunk might be too long. Also, individual streams might be
2535 * out-of-sync. In the first case, we cut the chunk in several
2536 * smaller pieces. In the second case, we re-order chunk reading
2537 * order. The end result should be a smoother playing AVI.
2540 gst_avi_demux_massage_index (GstAviDemux * avi,
2541 GList * list, GList * alloc_list)
2543 gst_avi_index_entry *entry;
2544 avi_stream_context *stream;
2547 gint64 delay = G_GINT64_CONSTANT (0);
2549 GST_LOG_OBJECT (avi, "Starting index massage, nr_entries = %d",
2550 list ? g_list_length (list) : 0);
2553 #ifndef GST_DISABLE_DEBUG
2554 guint num_added_total = 0;
2555 guint num_per_stream[GST_AVI_DEMUX_MAX_STREAMS] = { 0, };
2557 GST_LOG_OBJECT (avi,
2558 "I'm now going to cut large chunks into smaller pieces");
2560 /* cut chunks in small (seekable) pieces
2561 * FIXME: this should be a property where a value of
2562 * GST_CLOCK_TIME_NONE would disable the chunking
2564 #define MAX_DURATION (GST_SECOND / 2)
2565 for (i = 0; i < avi->num_streams; i++) {
2566 /* only chop streams that have exactly *one* chunk */
2567 if (avi->stream[i].total_frames != 1)
2570 for (node = list; node != NULL; node = node->next) {
2573 if (entry->stream_nr != i)
2576 /* check for max duration of a single buffer. I suppose that
2577 * the allocation of index entries could be improved. */
2578 stream = &avi->stream[entry->stream_nr];
2579 if (entry->dur > MAX_DURATION
2580 && stream->strh->type == GST_RIFF_FCC_auds) {
2582 gst_avi_index_entry *entries;
2583 guint old_size, num_added;
2586 /* cut in 1/10th of a second */
2587 ideal_size = stream->strf.auds->av_bps / 10;
2589 /* ensure chunk size is multiple of blockalign */
2590 if (stream->strf.auds->blockalign > 1)
2591 ideal_size -= ideal_size % stream->strf.auds->blockalign;
2594 old_size = entry->size;
2595 num_added = (entry->size - 1) / ideal_size;
2596 avi->index_size += num_added;
2597 entries = g_malloc (sizeof (gst_avi_index_entry) * num_added);
2598 alloc_list = g_list_prepend (alloc_list, entries);
2599 for (node2 = node->next; node2 != NULL; node2 = node2->next) {
2600 gst_avi_index_entry *entry2 = node2->data;
2602 entry2->index_nr += num_added;
2603 if (entry2->stream_nr == entry->stream_nr)
2604 entry2->frames_before += num_added;
2607 /* new sized index chunks */
2608 for (i = 0; i < num_added + 1; i++) {
2609 gst_avi_index_entry *entry2;
2614 entry2 = &entries[i - 1];
2615 list = g_list_insert_before (list, node->next, entry2);
2618 memcpy (entry2, entry, sizeof (gst_avi_index_entry));
2621 if (old_size >= ideal_size) {
2622 entry2->size = ideal_size;
2623 old_size -= ideal_size;
2625 entry2->size = old_size;
2628 entry2->dur = GST_SECOND * entry2->size / stream->strf.auds->av_bps;
2631 entry2->ts += entry->dur;
2632 entry2->offset += entry->size;
2633 entry2->bytes_before += entry->size;
2634 entry2->frames_before++;
2637 #ifndef GST_DISABLE_DEBUG
2638 num_added_total += num_added;
2643 #ifndef GST_DISABLE_DEBUG
2644 if (num_added_total)
2645 GST_LOG ("added %u new index entries", num_added_total);
2648 GST_LOG_OBJECT (avi, "I'm now going to reorder the index entries for time");
2650 /* re-order for time */
2651 list = g_list_sort (list, (GCompareFunc) sort);
2653 /* make a continous array out of the list */
2654 avi->index_size = g_list_length (list);
2655 avi->index_entries = g_try_new (gst_avi_index_entry, avi->index_size);
2656 if (!avi->index_entries)
2659 entry = (gst_avi_index_entry *) (list->data);
2662 GST_LOG_OBJECT (avi,
2663 "Building index array, nr_entries = %d (time offset = %"
2664 GST_TIME_FORMAT, avi->index_size, GST_TIME_ARGS (delay));
2666 for (i = 0, node = list; node != NULL; node = node->next, i++) {
2668 entry->index_nr = i;
2670 memcpy (&avi->index_entries[i], entry, sizeof (gst_avi_index_entry));
2671 #ifndef GST_DISABLE_DEBUG
2672 num_per_stream[entry->stream_nr]++;
2675 GST_LOG_OBJECT (avi, "Sorted index entry %3d for stream %d of size %6u"
2676 " at offset %7" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT
2677 " dur %" GST_TIME_FORMAT,
2678 avi->index_entries[i].index_nr, entry->stream_nr, entry->size,
2679 entry->offset, GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (entry->dur));
2682 for (i = 0; i < avi->num_streams; i++) {
2683 stream = &avi->stream[i];
2684 stream->idx_duration -= delay;
2687 #ifndef GST_DISABLE_DEBUG
2689 gchar str[GST_AVI_DEMUX_MAX_STREAMS * (1 + 6 + 2)];
2692 for (i = 0; i < avi->num_streams; i++) {
2693 if (!avi->stream[i].pad)
2695 pad_name = GST_OBJECT_NAME (avi->stream[i].pad);
2696 sprintf (&str[i * (1 + 6 + 2)], " %6u %c", num_per_stream[i],
2699 GST_LOG_OBJECT (avi, "indizies per stream:%20s", str);
2703 GST_LOG_OBJECT (avi, "Freeing original index list");
2704 /* all the node->data in list point to alloc_list chunks */
2709 g_list_foreach (alloc_list, (GFunc) g_free, NULL);
2710 g_list_free (alloc_list);
2712 #ifndef GST_DISABLE_GST_DEBUG
2713 for (i = 0; i < avi->num_streams; i++) {
2714 GST_LOG_OBJECT (avi, "Stream %d, %d frames, %8" G_GUINT64_FORMAT " bytes",
2715 i, avi->stream[i].total_frames, avi->stream[i].total_bytes);
2719 GST_LOG_OBJECT (avi, "Index massaging done");
2724 GST_WARNING_OBJECT (avi, "Out of memory for %" G_GSIZE_FORMAT " bytes",
2725 sizeof (gst_avi_index_entry) * avi->index_size);
2730 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
2732 gst_avi_index_entry *entry;
2736 total = GST_CLOCK_TIME_NONE;
2738 /* all streams start at a timestamp 0 */
2739 for (stream = 0; stream < avi->num_streams; stream++) {
2740 GstClockTime duration, hduration;
2741 avi_stream_context *streamc = &avi->stream[stream];
2742 gst_riff_strh *strh = streamc->strh;
2746 /* get header duration */
2747 hduration = gst_util_uint64_scale ((guint64) strh->length *
2748 strh->scale, GST_SECOND, (guint64) strh->rate);
2750 GST_INFO ("Stream %d duration according to header: %" GST_TIME_FORMAT,
2751 stream, GST_TIME_ARGS (hduration));
2754 hduration = GST_CLOCK_TIME_NONE;
2756 /* set duration for the stream */
2757 streamc->hdr_duration = hduration;
2759 /* get last index entry to get duration */
2760 if ((entry = gst_avi_demux_index_last (avi, stream)))
2761 duration = entry->ts + entry->dur;
2763 duration = GST_CLOCK_TIME_NONE;
2765 streamc->idx_duration = duration;
2767 /* now pick a good duration */
2768 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2769 /* index gave valid duration, use that */
2770 GST_INFO ("Stream %d duration according to index: %" GST_TIME_FORMAT,
2771 stream, GST_TIME_ARGS (duration));
2773 /* fall back to header info to calculate a duration */
2774 duration = hduration;
2776 /* set duration for the stream */
2777 streamc->duration = duration;
2779 /* find total duration */
2780 if (total == GST_CLOCK_TIME_NONE || duration > total)
2784 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
2785 /* now update the duration for those streams where we had none */
2786 for (stream = 0; stream < avi->num_streams; stream++) {
2787 avi_stream_context *streamc = &avi->stream[stream];
2789 if (!GST_CLOCK_TIME_IS_VALID (streamc->duration)
2790 || streamc->duration == 0) {
2791 streamc->duration = total;
2793 GST_INFO ("Stream %d duration according to total: %" GST_TIME_FORMAT,
2794 stream, GST_TIME_ARGS (total));
2799 /* and set the total duration in the segment. */
2800 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
2801 GST_TIME_ARGS (total));
2803 gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
2807 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
2809 gboolean result = FALSE;
2812 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
2813 GST_EVENT_TYPE_NAME (event), avi->num_streams);
2815 if (avi->num_streams) {
2816 for (i = 0; i < avi->num_streams; i++) {
2817 avi_stream_context *stream = &avi->stream[i];
2820 if (gst_pad_push_event (stream->pad, gst_event_ref (event)))
2825 gst_event_unref (event);
2830 * Read AVI headers when streaming
2832 static GstFlowReturn
2833 gst_avi_demux_stream_header_push (GstAviDemux * avi)
2835 GstFlowReturn ret = GST_FLOW_OK;
2840 GstBuffer *buf = NULL, *sub = NULL;
2844 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
2846 switch (avi->header_state) {
2847 case GST_AVI_DEMUX_HEADER_TAG_LIST:
2848 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2849 avi->offset += 8 + ((size + 1) & ~1);
2850 if (tag != GST_RIFF_TAG_LIST)
2851 goto header_no_list;
2853 gst_adapter_flush (avi->adapter, 8);
2854 /* Find the 'hdrl' LIST tag */
2855 GST_DEBUG ("Reading %d bytes", size);
2856 buf = gst_adapter_take_buffer (avi->adapter, size);
2858 if (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl)
2859 goto header_no_hdrl;
2861 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
2863 /* the hdrl starts with a 'avih' header */
2864 if (!gst_riff_parse_chunk (GST_ELEMENT (avi), buf, &offset, &tag, &sub))
2865 goto header_no_avih;
2867 if (tag != GST_RIFF_TAG_avih)
2868 goto header_no_avih;
2870 if (!gst_avi_demux_parse_avih (GST_ELEMENT (avi), sub, &avi->avih))
2871 goto header_wrong_avih;
2873 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
2875 /* now, read the elements from the header until the end */
2876 while (gst_riff_parse_chunk (GST_ELEMENT (avi), buf, &offset, &tag,
2878 /* sub can be NULL on empty tags */
2883 case GST_RIFF_TAG_LIST:
2884 if (GST_BUFFER_SIZE (sub) < 4)
2887 switch (GST_READ_UINT32_LE (GST_BUFFER_DATA (sub))) {
2888 case GST_RIFF_LIST_strl:
2889 if (!(gst_avi_demux_parse_stream (avi, sub))) {
2890 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
2891 ("failed to parse stream, ignoring"));
2895 case GST_RIFF_LIST_odml:
2896 gst_avi_demux_parse_odml (avi, sub);
2899 GST_WARNING_OBJECT (avi,
2900 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
2901 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA
2904 case GST_RIFF_TAG_JUNK:
2909 GST_WARNING_OBJECT (avi,
2910 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
2911 offset, GST_FOURCC_ARGS (tag));
2913 case GST_RIFF_TAG_JUNK:
2915 /* move to next chunk */
2916 gst_buffer_unref (sub);
2921 gst_buffer_unref (buf);
2922 GST_DEBUG ("elements parsed");
2924 /* check parsed streams */
2925 if (avi->num_streams == 0) {
2927 } else if (avi->num_streams != avi->avih->streams) {
2928 GST_WARNING_OBJECT (avi,
2929 "Stream header mentioned %d streams, but %d available",
2930 avi->avih->streams, avi->num_streams);
2932 GST_DEBUG ("Get junk and info next");
2933 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
2935 /* Need more data */
2939 case GST_AVI_DEMUX_HEADER_INFO:
2940 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
2942 if (gst_adapter_available (avi->adapter) < 12)
2945 data = gst_adapter_peek (avi->adapter, 12);
2946 tag = GST_READ_UINT32_LE (data);
2947 size = GST_READ_UINT32_LE (data + 4);
2948 ltag = GST_READ_UINT32_LE (data + 8);
2950 if (tag == GST_RIFF_TAG_LIST) {
2952 case GST_RIFF_LIST_movi:
2953 gst_adapter_flush (avi->adapter, 12);
2956 case GST_RIFF_LIST_INFO:
2957 GST_DEBUG ("Found INFO chunk");
2958 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2960 gst_adapter_flush (avi->adapter, 12);
2961 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
2962 gst_riff_parse_info (GST_ELEMENT (avi), buf, &avi->globaltags);
2963 gst_buffer_unref (buf);
2965 avi->offset += ((size + 1) & ~1) - 4;
2967 /* Need more data */
2972 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2973 avi->offset += 8 + ((size + 1) & ~1);
2974 gst_adapter_flush (avi->adapter, 8 + ((size + 1) & ~1));
2975 // ??? goto iterate; ???
2977 /* Need more data */
2983 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2984 avi->offset += 8 + ((size + 1) & ~1);
2985 gst_adapter_flush (avi->adapter, 8 + ((size + 1) & ~1));
2988 /* Need more data */
2995 GST_WARNING ("unhandled header state: %d", avi->header_state);
3000 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3001 avi->num_streams, avi->stream[0].indexes);
3003 GST_DEBUG ("Found movi chunk. Starting to stream data");
3004 avi->state = GST_AVI_DEMUX_MOVI;
3007 /*GList *index = NULL, *alloc = NULL; */
3009 /* ######################## this need to be integrated with the state */
3010 /* create or read stream index (for seeking) */
3011 if (avi->stream[0].indexes != NULL) {
3012 gst_avi_demux_read_subindexes_push (avi, &index, &alloc);
3015 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX) {
3016 gst_avi_demux_stream_index (avi, &index, &alloc);
3018 /* some indexes are incomplete, continue streaming from there */
3020 gst_avi_demux_stream_scan (avi, &index, &alloc);
3023 /* this is a fatal error */
3027 if (!gst_avi_demux_massage_index (avi, index, alloc))
3030 gst_avi_demux_calculate_durations_from_index (avi);
3031 /* ######################## */
3034 /* create initial NEWSEGMENT event */
3035 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3036 stop = avi->segment.duration;
3038 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3040 if (avi->seek_event)
3041 gst_event_unref (avi->seek_event);
3042 avi->seek_event = gst_event_new_new_segment
3043 (FALSE, avi->segment.rate, GST_FORMAT_TIME,
3044 avi->segment.start, stop, avi->segment.start);
3046 /* at this point we know all the streams and we can signal the no more
3048 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3049 gst_element_no_more_pads (GST_ELEMENT (avi));
3056 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3057 return GST_FLOW_ERROR;
3061 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3062 ("Invalid AVI header (no LIST at start): %"
3063 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3064 return GST_FLOW_ERROR;
3068 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3069 ("Invalid AVI header (no hdrl at start): %"
3070 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3071 gst_buffer_unref (buf);
3072 return GST_FLOW_ERROR;
3076 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3077 ("Invalid AVI header (no avih at start): %"
3078 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3080 gst_buffer_unref (sub);
3082 gst_buffer_unref (buf);
3083 return GST_FLOW_ERROR;
3087 gst_buffer_unref (buf);
3088 return GST_FLOW_ERROR;
3093 * Read full AVI headers.
3095 static GstFlowReturn
3096 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
3099 GstBuffer *buf, *sub = NULL;
3101 GList *index = NULL, *alloc = NULL;
3104 GstElement *element = GST_ELEMENT_CAST (avi);
3106 /* the header consists of a 'hdrl' LIST tag */
3107 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3108 if (res != GST_FLOW_OK)
3109 goto pull_range_failed;
3110 else if (tag != GST_RIFF_TAG_LIST)
3112 else if (GST_BUFFER_SIZE (buf) < 4)
3115 GST_DEBUG_OBJECT (avi, "parsing headers");
3117 /* Find the 'hdrl' LIST tag */
3118 while (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl) {
3119 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
3120 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf))));
3123 gst_buffer_unref (buf);
3125 /* read new chunk */
3126 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3127 if (res != GST_FLOW_OK)
3128 goto pull_range_failed;
3129 else if (tag != GST_RIFF_TAG_LIST)
3131 else if (GST_BUFFER_SIZE (buf) < 4)
3135 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
3137 /* the hdrl starts with a 'avih' header */
3138 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
3140 else if (tag != GST_RIFF_TAG_avih)
3142 else if (!gst_avi_demux_parse_avih (element, sub, &avi->avih))
3145 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3147 /* now, read the elements from the header until the end */
3148 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
3149 /* sub can be NULL on empty tags */
3154 case GST_RIFF_TAG_LIST:
3159 if (GST_BUFFER_SIZE (sub) < 4)
3162 data = GST_BUFFER_DATA (sub);
3163 fourcc = GST_READ_UINT32_LE (data);
3166 case GST_RIFF_LIST_strl:
3167 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3168 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3169 ("faile to parse stream, ignoring"));
3173 case GST_RIFF_LIST_odml:
3174 gst_avi_demux_parse_odml (avi, sub);
3177 GST_WARNING_OBJECT (avi,
3178 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3179 GST_FOURCC_ARGS (fourcc));
3180 GST_MEMDUMP_OBJECT (avi, "Unknown list", GST_BUFFER_DATA (sub),
3181 GST_BUFFER_SIZE (sub));
3183 case GST_RIFF_TAG_JUNK:
3189 GST_WARNING_OBJECT (avi,
3190 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
3191 GST_FOURCC_ARGS (tag), offset);
3192 GST_MEMDUMP_OBJECT (avi, "Unknown tag", GST_BUFFER_DATA (sub),
3193 GST_BUFFER_SIZE (sub));
3195 case GST_RIFF_TAG_JUNK:
3198 gst_buffer_unref (sub);
3203 gst_buffer_unref (buf);
3204 GST_DEBUG ("elements parsed");
3206 /* check parsed streams */
3207 if (avi->num_streams == 0)
3209 else if (avi->num_streams != avi->avih->streams) {
3210 GST_WARNING_OBJECT (avi,
3211 "Stream header mentioned %d streams, but %d available",
3212 avi->avih->streams, avi->num_streams);
3215 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
3216 G_GUINT64_FORMAT, avi->offset);
3218 /* Now, find the data (i.e. skip all junk between header and data) */
3223 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
3224 if (res != GST_FLOW_OK) {
3225 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
3226 goto pull_range_failed;
3227 } else if (GST_BUFFER_SIZE (buf) < 12) {
3228 GST_DEBUG_OBJECT (avi, "got %d bytes which is less than 12 bytes",
3229 GST_BUFFER_SIZE (buf));
3230 gst_buffer_unref (buf);
3231 return GST_FLOW_ERROR;
3234 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
3235 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
3236 ltag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 8);
3238 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
3239 GST_FOURCC_ARGS (tag), size);
3240 GST_MEMDUMP ("Tag content", GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
3241 gst_buffer_unref (buf);
3244 case GST_RIFF_TAG_LIST:{
3246 case GST_RIFF_LIST_movi:
3247 GST_DEBUG_OBJECT (avi,
3248 "Reached the 'movi' tag, we're done with skipping");
3250 case GST_RIFF_LIST_INFO:
3252 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3254 if (res != GST_FLOW_OK) {
3255 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3256 goto pull_range_failed;
3258 GST_DEBUG ("got size %u", GST_BUFFER_SIZE (buf));
3260 sub = gst_buffer_create_sub (buf, 4, GST_BUFFER_SIZE (buf) - 4);
3261 gst_riff_parse_info (element, sub, &avi->globaltags);
3263 gst_buffer_unref (sub);
3266 gst_buffer_unref (buf);
3267 /* gst_riff_read_chunk() has already advanced avi->offset */
3270 GST_WARNING_OBJECT (avi,
3271 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3272 GST_FOURCC_ARGS (ltag));
3273 avi->offset += 8 + ((size + 1) & ~1);
3279 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3280 GST_FOURCC_ARGS (tag));
3282 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3283 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3284 avi->offset += 8 + ((size + 1) & ~1);
3290 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3291 avi->num_streams, avi->stream[0].indexes);
3293 /* create or read stream index (for seeking) */
3294 if (avi->stream[0].indexes != NULL) {
3295 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3296 gst_avi_demux_read_subindexes_pull (avi, &index, &alloc);
3299 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX) {
3300 gst_avi_demux_stream_index (avi, &index, &alloc);
3302 /* some indexes are incomplete, continue streaming from there */
3304 gst_avi_demux_stream_scan (avi, &index, &alloc);
3307 /* this is a fatal error */
3311 if (!gst_avi_demux_massage_index (avi, index, alloc))
3314 gst_avi_demux_calculate_durations_from_index (avi);
3316 /* create initial NEWSEGMENT event */
3317 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3318 stop = avi->segment.duration;
3320 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3322 if (avi->seek_event)
3323 gst_event_unref (avi->seek_event);
3324 avi->seek_event = gst_event_new_new_segment
3325 (FALSE, avi->segment.rate, GST_FORMAT_TIME,
3326 avi->segment.start, stop, avi->segment.start);
3328 /* at this point we know all the streams and we can signal the no more
3330 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3331 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3338 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3339 ("Invalid AVI header (no LIST at start): %"
3340 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3341 gst_buffer_unref (buf);
3342 return GST_FLOW_ERROR;
3346 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3347 ("Invalid AVI header (no hdrl at start): %"
3348 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3349 gst_buffer_unref (buf);
3350 return GST_FLOW_ERROR;
3354 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3355 ("Invalid AVI header (no avih at start): %"
3356 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3358 gst_buffer_unref (sub);
3359 gst_buffer_unref (buf);
3360 return GST_FLOW_ERROR;
3364 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3365 ("Invalid AVI header (cannot parse avih at start)"));
3367 gst_buffer_unref (sub);
3368 gst_buffer_unref (buf);
3369 return GST_FLOW_ERROR;
3373 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3374 return GST_FLOW_ERROR;
3378 GST_WARNING ("file without or too big index");
3379 g_list_free (index);
3380 g_list_foreach (alloc, (GFunc) g_free, NULL);
3381 g_list_free (alloc);
3383 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3384 ("Could not get/create index"));
3385 return GST_FLOW_ERROR;
3389 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3390 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3391 return GST_FLOW_ERROR;
3396 * Do the actual seeking.
3399 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
3401 GstClockTime seek_time;
3403 gst_avi_index_entry *entry, *kentry;
3406 seek_time = segment->last_stop;
3407 keyframe = !!(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
3409 /* FIXME: if we seek in an openDML file, we will have multiple
3410 * primary levels. Seeking in between those will cause havoc. */
3412 /* save old position so we can see if we must mark a discont. */
3413 old_entry = avi->current_entry;
3415 /* get the entry for the requested position, which is always in last_stop.
3416 * we search the index entry for stream 0, since all entries are sorted by
3417 * time and stream we automagically are positioned for the other streams as
3418 * well. FIXME, this code assumes the main stream with keyframes is stream 0,
3419 * which is mostly correct... */
3420 if (!(entry = gst_avi_demux_index_entry_for_time (avi, 0, seek_time)))
3423 GST_DEBUG_OBJECT (avi,
3424 "Got requested entry %d [stream:%d / ts:%" GST_TIME_FORMAT
3425 " / duration:%" GST_TIME_FORMAT "]", entry->index_nr,
3426 entry->stream_nr, GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (entry->dur));
3428 /* check if we are already on a keyframe */
3429 if (!(entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)) {
3430 /* now go to the previous keyframe, this is where we should start
3432 if (!(kentry = gst_avi_demux_index_prev (avi, 0, entry->index_nr,
3433 GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME))) {
3437 /* we were on a keyframe */
3441 GST_DEBUG_OBJECT (avi,
3442 "Got keyframe entry %d [stream:%d / ts:%" GST_TIME_FORMAT
3443 " / duration:%" GST_TIME_FORMAT "]", kentry->index_nr,
3444 entry->stream_nr, GST_TIME_ARGS (kentry->ts),
3445 GST_TIME_ARGS (kentry->dur));
3447 /* we must start decoding at the keyframe */
3448 avi->current_entry = kentry->index_nr;
3450 if (segment->rate < 0.0) {
3451 gst_avi_index_entry *next_keyframe;
3453 /* Because we don't know the frame order we need to push from the prev keyframe
3454 * to the next keyframe. If there is a smart decoder downstream he will notice
3455 * that there are too many encoded frames send and return UNEXPECTED when there
3456 * are enough decoded frames to fill the segment.
3459 gst_avi_demux_index_next (avi, 0, kentry->index_nr,
3460 GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME);
3462 next_keyframe = gst_avi_demux_index_last (avi, 0);
3464 avi->reverse_start_index = kentry->index_nr;
3465 avi->reverse_stop_index = next_keyframe->index_nr;
3467 GST_DEBUG_OBJECT (avi, "reverse seek: start idx (%d) and stop idx (%d)",
3468 avi->reverse_start_index, avi->reverse_stop_index);
3472 /* when seeking to a keyframe, we update the result seek time
3473 * to the time of the keyframe. */
3474 seek_time = avi->index_entries[avi->current_entry].ts;
3478 /* if we changed position, mark a DISCONT on all streams */
3479 if (avi->current_entry != old_entry) {
3482 for (i = 0; i < avi->num_streams; i++) {
3483 avi->stream[i].discont = TRUE;
3487 GST_DEBUG_OBJECT (avi, "seek: %" GST_TIME_FORMAT
3488 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
3490 /* the seek time is also the last_stop and stream time */
3491 segment->last_stop = seek_time;
3492 segment->time = seek_time;
3498 /* we could not find an entry for the given time */
3499 GST_WARNING_OBJECT (avi,
3500 "Couldn't find AviIndexEntry for time:%" GST_TIME_FORMAT,
3501 GST_TIME_ARGS (seek_time));
3502 if (avi->current_entry >= avi->index_size && avi->index_size > 0)
3503 avi->current_entry = avi->index_size - 1;
3510 * Handle seek event.
3513 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
3518 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
3522 GstSegment seeksegment = { 0, };
3525 GST_DEBUG_OBJECT (avi, "doing seek with event");
3527 gst_event_parse_seek (event, &rate, &format, &flags,
3528 &cur_type, &cur, &stop_type, &stop);
3530 /* we have to have a format as the segment format. Try to convert
3532 if (format != GST_FORMAT_TIME) {
3533 GstFormat fmt = GST_FORMAT_TIME;
3534 gboolean res = TRUE;
3536 if (cur_type != GST_SEEK_TYPE_NONE)
3537 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
3538 if (res && stop_type != GST_SEEK_TYPE_NONE)
3539 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
3545 GST_DEBUG_OBJECT (avi,
3546 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
3547 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
3548 /* FIXME: can we do anything with rate!=1.0 */
3550 GST_DEBUG_OBJECT (avi, "doing seek without event");
3555 /* save flush flag */
3556 flush = flags & GST_SEEK_FLAG_FLUSH;
3559 GstEvent *event = gst_event_new_flush_start ();
3561 /* for a flushing seek, we send a flush_start on all pads. This will
3562 * eventually stop streaming with a WRONG_STATE. We can thus eventually
3563 * take the STREAM_LOCK. */
3564 GST_DEBUG_OBJECT (avi, "sending flush start");
3565 gst_avi_demux_push_event (avi, gst_event_ref (event));
3566 gst_pad_push_event (avi->sinkpad, event);
3568 /* a non-flushing seek, we PAUSE the task so that we can take the
3570 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
3571 gst_pad_pause_task (avi->sinkpad);
3574 /* wait for streaming to stop */
3575 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
3576 GST_PAD_STREAM_LOCK (avi->sinkpad);
3578 /* copy segment, we need this because we still need the old
3579 * segment when we close the current segment. */
3580 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
3583 GST_DEBUG_OBJECT (avi, "configuring seek");
3584 gst_segment_set_seek (&seeksegment, rate, format, flags,
3585 cur_type, cur, stop_type, stop, &update);
3588 /* do the seek, seeksegment.last_stop contains the new position, this
3589 * actually never fails. */
3590 gst_avi_demux_do_seek (avi, &seeksegment);
3595 GST_DEBUG_OBJECT (avi, "sending flush stop");
3596 gst_avi_demux_push_event (avi, gst_event_new_flush_stop ());
3597 gst_pad_push_event (avi->sinkpad, gst_event_new_flush_stop ());
3598 /* reset the last flow and mark discont, FLUSH is always DISCONT */
3599 for (i = 0; i < avi->num_streams; i++) {
3600 avi->stream[i].last_flow = GST_FLOW_OK;
3601 avi->stream[i].discont = TRUE;
3603 } else if (avi->segment_running) {
3606 /* we are running the current segment and doing a non-flushing seek,
3607 * close the segment first based on the last_stop. */
3608 GST_DEBUG_OBJECT (avi, "closing running segment %" G_GINT64_FORMAT
3609 " to %" G_GINT64_FORMAT, avi->segment.start, avi->segment.last_stop);
3610 seg = gst_event_new_new_segment (TRUE,
3611 avi->segment.rate, avi->segment.format,
3612 avi->segment.start, avi->segment.last_stop, avi->segment.time);
3613 gst_avi_demux_push_event (avi, seg);
3616 /* now update the real segment info */
3617 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
3619 /* post the SEGMENT_START message when we do segmented playback */
3620 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3621 gst_element_post_message (GST_ELEMENT (avi),
3622 gst_message_new_segment_start (GST_OBJECT (avi),
3623 avi->segment.format, avi->segment.last_stop));
3626 /* prepare for streaming again */
3627 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3628 stop = avi->segment.duration;
3630 /* queue the segment event for the streaming thread. */
3631 if (avi->seek_event)
3632 gst_event_unref (avi->seek_event);
3633 if (avi->segment.rate > 0.0) {
3634 avi->seek_event = gst_event_new_new_segment (FALSE,
3635 avi->segment.rate, avi->segment.format,
3636 avi->segment.last_stop, stop, avi->segment.time);
3638 avi->seek_event = gst_event_new_new_segment (FALSE,
3639 avi->segment.rate, avi->segment.format,
3640 avi->segment.start, avi->segment.last_stop, avi->segment.start);
3643 if (!avi->streaming) {
3644 avi->segment_running = TRUE;
3645 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
3648 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
3655 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
3661 * Helper for gst_avi_demux_invert()
3664 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
3666 memcpy (tmp, d1, bytes);
3667 memcpy (d1, d2, bytes);
3668 memcpy (d2, tmp, bytes);
3672 #define gst_avi_demux_is_uncompressed(fourcc) \
3673 (fourcc == GST_RIFF_DIB || \
3674 fourcc == GST_RIFF_rgb || \
3675 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW)
3678 * Invert DIB buffers... Takes existing buffer and
3679 * returns either the buffer or a new one (with old
3680 * one dereferenced).
3681 * FIXME: can't we preallocate tmp? and remember stride, bpp?
3684 gst_avi_demux_invert (avi_stream_context * stream, GstBuffer * buf)
3691 if (stream->strh->type != GST_RIFF_FCC_vids)
3694 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
3695 return buf; /* Ignore non DIB buffers */
3698 s = gst_caps_get_structure (GST_PAD_CAPS (stream->pad), 0);
3699 if (!gst_structure_get_int (s, "bpp", &bpp)) {
3700 GST_WARNING ("Failed to retrieve depth from caps");
3704 if (stream->strf.vids == NULL) {
3705 GST_WARNING ("Failed to retrieve vids for stream");
3709 h = stream->strf.vids->height;
3710 w = stream->strf.vids->width;
3711 stride = w * (bpp / 8);
3713 buf = gst_buffer_make_writable (buf);
3714 if (GST_BUFFER_SIZE (buf) < (stride * h)) {
3715 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
3719 tmp = g_malloc (stride);
3721 for (y = 0; y < h / 2; y++) {
3722 swap_line (GST_BUFFER_DATA (buf) + stride * y,
3723 GST_BUFFER_DATA (buf) + stride * (h - 1 - y), tmp, stride);
3732 * Returns the aggregated GstFlowReturn.
3734 static GstFlowReturn
3735 gst_avi_demux_combine_flows (GstAviDemux * avi, avi_stream_context * stream,
3740 /* store the value */
3741 stream->last_flow = ret;
3743 /* any other error that is not-linked can be returned right away */
3744 if (G_UNLIKELY (ret != GST_FLOW_NOT_LINKED))
3747 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3748 for (i = 0; i < avi->num_streams; i++) {
3749 avi_stream_context *ostream = &avi->stream[i];
3751 ret = ostream->last_flow;
3752 /* some other return value (must be SUCCESS but we can return
3753 * other values as well) */
3754 if (G_UNLIKELY (ret != GST_FLOW_NOT_LINKED))
3757 /* if we get here, all other pads were unlinked and we return
3758 * NOT_LINKED then */
3760 GST_LOG_OBJECT (avi, "combined return %s", gst_flow_get_name (ret));
3765 * prepare the avi element for a reverse jump to a prev keyframe
3766 * this function will return the start entry. if the function returns
3767 * NULL there was no prev keyframe.
3769 static gst_avi_index_entry *
3770 gst_avi_demux_step_reverse (GstAviDemux * avi)
3772 gst_avi_index_entry *entry;
3775 avi->reverse_stop_index = avi->reverse_start_index;
3777 gst_avi_demux_index_prev (avi, 0, avi->reverse_stop_index,
3778 GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME);
3780 GST_DEBUG_OBJECT (avi, "no valid index entry found index %d",
3781 avi->reverse_stop_index);
3784 avi->current_entry = avi->reverse_start_index = entry->index_nr;
3785 GST_DEBUG_OBJECT (avi,
3786 "reverse playback jump: start idx (%d) and stop idx (%d)",
3787 avi->reverse_start_index, avi->reverse_stop_index);
3788 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, entry->ts);
3789 for (i = 0; i < avi->num_streams; i++) {
3790 avi->stream[i].last_flow = GST_FLOW_OK;
3791 avi->stream[i].discont = TRUE;
3797 * Read data from one index entry
3799 static GstFlowReturn
3800 gst_avi_demux_process_next_entry (GstAviDemux * avi)
3802 GstFlowReturn res = GST_FLOW_OK;
3803 gboolean processed = FALSE;
3804 avi_stream_context *stream;
3805 gst_avi_index_entry *entry;
3809 /* see if we are at the end */
3810 if ((avi->segment.rate > 0 && avi->current_entry >= avi->index_size))
3813 /* get next entry, this will work as we checked for the index size above */
3814 entry = &avi->index_entries[avi->current_entry++];
3816 /* check for reverse playback */
3817 if (avi->segment.rate < 0 && avi->current_entry > avi->reverse_stop_index) {
3818 GST_LOG_OBJECT (avi, "stop_index %d reached", avi->reverse_stop_index);
3820 /* check if we have pushed enough data for this segment */
3821 if (avi->reverse_start_index == 0)
3822 goto eos_reverse_zero;
3823 if (avi->index_entries[avi->reverse_start_index].ts < avi->segment.start)
3824 goto eos_reverse_segment;
3826 if (!(entry = gst_avi_demux_step_reverse (avi)))
3829 avi->current_entry++;
3832 /* see if we have a valid stream, ignore if not
3833 * FIXME: can't we check this when building the index?
3834 * we check it in _parse_index(), _stream_scan()
3836 if (entry->stream_nr >= avi->num_streams) {
3837 GST_WARNING_OBJECT (avi,
3838 "Entry %d has non-existing stream nr %d",
3839 avi->current_entry - 1, entry->stream_nr);
3843 /* get stream now */
3844 stream = &avi->stream[entry->stream_nr];
3846 if (avi->segment.rate > 0.0) {
3847 /* only check this for fowards playback for now */
3848 if ((entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)
3849 && GST_CLOCK_TIME_IS_VALID (entry->ts)
3850 && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
3851 && (entry->ts > avi->segment.stop)) {
3856 /* skip empty entries */
3857 if (entry->size == 0 || !stream->pad) {
3858 GST_DEBUG_OBJECT (avi, "Skipping entry %d (%d, %p)",
3859 avi->current_entry - 1, entry->size, stream->pad);
3863 GST_LOG ("reading buffer (size=%d) from stream %d at current pos %"
3864 G_GUINT64_FORMAT " (%llx)", entry->size, entry->stream_nr,
3865 avi->index_offset + entry->offset, avi->index_offset + entry->offset);
3867 /* pull in the data */
3868 res = gst_pad_pull_range (avi->sinkpad, entry->offset +
3869 avi->index_offset, entry->size, &buf);
3870 if (res != GST_FLOW_OK)
3873 /* check for short buffers, this is EOS as well */
3874 if (GST_BUFFER_SIZE (buf) < entry->size)
3877 /* invert the picture if needed */
3878 buf = gst_avi_demux_invert (stream, buf);
3880 /* mark non-keyframes */
3881 if (!(entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME))
3882 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
3884 GST_BUFFER_TIMESTAMP (buf) = entry->ts;
3885 GST_BUFFER_DURATION (buf) = entry->dur;
3886 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
3887 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
3888 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
3890 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %d and time %"
3891 GST_TIME_FORMAT " on pad %s",
3892 GST_BUFFER_SIZE (buf), GST_TIME_ARGS (entry->ts),
3893 GST_PAD_NAME (stream->pad));
3895 /* update current position in the segment */
3896 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, entry->ts);
3898 /* mark discont when pending */
3899 if (stream->discont) {
3900 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
3901 stream->discont = FALSE;
3904 res = gst_pad_push (stream->pad, buf);
3906 /* mark as processed, we increment the frame and byte counters then
3907 * leave the while loop and return the GstFlowReturn */
3909 GST_DEBUG_OBJECT (avi, "Processed buffer %d: %s", entry->index_nr,
3910 gst_flow_get_name (res));
3912 if (avi->segment.rate < 0
3913 && entry->ts > avi->segment.stop && res == GST_FLOW_UNEXPECTED) {
3914 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
3915 * we are at the end of the segment, so we just need to jump
3916 * back to the previous section.
3918 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
3920 if (!(entry = gst_avi_demux_step_reverse (avi)))
3925 stream->current_frame = entry->frames_before;
3926 stream->current_byte = entry->bytes_before;
3932 res = gst_avi_demux_combine_flows (avi, stream, res);
3935 stream->current_frame = entry->frames_before + 1;
3936 stream->current_byte = entry->bytes_before + entry->size;
3937 } while (!processed);
3940 GST_DEBUG_OBJECT (avi, "returning %s", gst_flow_get_name (res));
3947 GST_LOG_OBJECT (avi, "Handled last index entry, setting EOS (%d > %d)",
3948 avi->current_entry, avi->index_size);
3949 /* we mark the first stream as EOS */
3950 res = GST_FLOW_UNEXPECTED;
3955 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
3956 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
3957 GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (avi->segment.stop));
3958 res = GST_FLOW_UNEXPECTED;
3963 GST_DEBUG_OBJECT (avi, "start_index was 0, setting EOS");
3964 res = GST_FLOW_UNEXPECTED;
3967 eos_reverse_segment:
3969 GST_DEBUG_OBJECT (avi, "full segment pushed, setting EOS");
3970 res = GST_FLOW_UNEXPECTED;
3975 GST_DEBUG_OBJECT (avi,
3976 "pull range failed: pos=%" G_GUINT64_FORMAT " size=%d",
3977 entry->offset + avi->index_offset, entry->size);
3982 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
3983 ", only got %d/%d bytes (truncated file?)", entry->offset +
3984 avi->index_offset, GST_BUFFER_SIZE (buf), entry->size);
3985 gst_buffer_unref (buf);
3986 res = GST_FLOW_UNEXPECTED;
3992 * Read data. If we have an index it delegates to
3993 * gst_avi_demux_process_next_entry().
3995 static GstFlowReturn
3996 gst_avi_demux_stream_data (GstAviDemux * avi)
4001 GstFlowReturn res = GST_FLOW_OK;
4002 GstFormat format = GST_FORMAT_TIME;
4004 /* if we have a avi->index_entries[], we don't want to read
4005 * the stream linearly, but seek to the next ts/index_entry. */
4006 if (G_LIKELY (avi->index_entries != NULL))
4007 return gst_avi_demux_process_next_entry (avi);
4009 if (G_UNLIKELY (avi->have_eos)) {
4010 /* Clean adapter, we're done */
4011 gst_adapter_clear (avi->adapter);
4016 if (!gst_avi_demux_sync (avi, &tag, FALSE))
4020 /* Iterate until need more data, so adapter won't grow too much */
4022 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
4026 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
4027 GST_FOURCC_ARGS (tag), size);
4029 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
4030 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
4031 GST_LOG ("Chunk ok");
4032 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
4033 GST_DEBUG ("Found sub-index tag");
4034 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
4035 if ((size > 0) && (size != -1)) {
4036 GST_DEBUG (" skipping %d bytes for now", size);
4037 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4041 } else if (tag == GST_RIFF_TAG_JUNK) {
4042 GST_DEBUG ("JUNK chunk, skipping");
4043 } else if (tag == GST_RIFF_TAG_idx1) {
4044 GST_DEBUG ("Found index tag, stream done");
4045 avi->have_eos = TRUE;
4046 return GST_FLOW_UNEXPECTED;
4047 } else if (tag == GST_RIFF_TAG_LIST) {
4048 /* movi chunks might be grouped in rec list */
4049 if (gst_adapter_available (avi->adapter) >= 12) {
4050 GST_DEBUG ("Found LIST tag, skipping LIST header");
4051 gst_adapter_flush (avi->adapter, 12);
4055 } else if (tag == GST_RIFF_TAG_JUNK) {
4056 /* rec list might contain JUNK chunks */
4057 GST_DEBUG ("Found JUNK tag");
4058 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
4059 if ((size > 0) && (size != -1)) {
4060 GST_DEBUG (" skipping %d bytes for now", size);
4061 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4067 GST_DEBUG ("No more stream chunks, send EOS");
4068 avi->have_eos = TRUE;
4069 return GST_FLOW_UNEXPECTED;
4072 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
4073 if ((size == 0) || (size == -1))
4074 gst_adapter_flush (avi->adapter, 8);
4077 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
4078 GST_FOURCC_ARGS (tag), size);
4080 stream_nr = CHUNKID_TO_STREAMNR (tag);
4082 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
4084 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
4085 stream_nr, GST_FOURCC_ARGS (tag));
4086 avi->offset += 8 + ((size + 1) & ~1);
4087 gst_adapter_flush (avi->adapter, 8 + ((size + 1) & ~1));
4089 avi_stream_context *stream;
4090 GstClockTime next_ts = 0;
4093 gst_adapter_flush (avi->adapter, 8);
4096 buf = gst_adapter_take_buffer (avi->adapter, ((size + 1) & ~1));
4097 /* patch the size */
4098 GST_BUFFER_SIZE (buf) = size;
4099 avi->offset += 8 + ((size + 1) & ~1);
4101 /* get time of this buffer */
4102 stream = &avi->stream[stream_nr];
4103 gst_pad_query_position (stream->pad, &format, (gint64 *) & next_ts);
4104 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4107 /* set delay (if any)
4108 if (stream->strh->init_frames == stream->current_frame &&
4110 stream->delay = next_ts;
4113 stream->current_frame++;
4114 stream->current_byte += size;
4116 if (G_UNLIKELY (!stream->pad)) {
4117 GST_WARNING ("No pad.");
4118 gst_buffer_unref (buf);
4120 GstClockTime dur_ts = 0;
4122 /* invert the picture if needed */
4123 buf = gst_avi_demux_invert (stream, buf);
4125 gst_pad_query_position (stream->pad, &format, (gint64 *) & dur_ts);
4126 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4129 GST_BUFFER_TIMESTAMP (buf) = next_ts;
4130 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
4131 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4132 GST_DEBUG_OBJECT (avi,
4133 "Pushing buffer with time=%" GST_TIME_FORMAT
4134 " and size %d over pad %s", GST_TIME_ARGS (next_ts), size,
4135 GST_PAD_NAME (stream->pad));
4137 /* update current position in the segment */
4138 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, next_ts);
4140 /* mark discont when pending */
4141 if (G_UNLIKELY (stream->discont)) {
4142 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4143 stream->discont = FALSE;
4145 res = gst_pad_push (stream->pad, buf);
4148 res = gst_avi_demux_combine_flows (avi, stream, res);
4149 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4150 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
4163 GST_DEBUG_OBJECT (avi, "format %s != GST_FORMAT_TIME",
4164 gst_format_get_name (format));
4165 res = GST_FLOW_ERROR;
4171 * Send pending tags.
4174 push_tag_lists (GstAviDemux * avi)
4181 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
4183 for (i = 0; i < avi->num_streams; i++)
4184 if (avi->stream[i].pad && avi->stream[i].taglist) {
4185 GST_DEBUG_OBJECT (avi->stream[i].pad, "Tags: %" GST_PTR_FORMAT,
4186 avi->stream[i].taglist);
4187 gst_element_found_tags_for_pad (GST_ELEMENT (avi), avi->stream[i].pad,
4188 avi->stream[i].taglist);
4189 avi->stream[i].taglist = NULL;
4192 if (avi->globaltags == NULL)
4193 avi->globaltags = gst_tag_list_new ();
4195 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE,
4196 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
4198 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, avi->globaltags);
4199 gst_element_found_tags (GST_ELEMENT (avi), avi->globaltags);
4200 avi->globaltags = NULL;
4201 avi->got_tags = FALSE;
4205 gst_avi_demux_loop (GstPad * pad)
4208 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4210 switch (avi->state) {
4211 case GST_AVI_DEMUX_START:
4212 if (G_UNLIKELY ((res =
4213 gst_avi_demux_stream_init_pull (avi)) != GST_FLOW_OK)) {
4214 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4217 avi->state = GST_AVI_DEMUX_HEADER;
4219 case GST_AVI_DEMUX_HEADER:
4220 if (G_UNLIKELY ((res =
4221 gst_avi_demux_stream_header_pull (avi)) != GST_FLOW_OK)) {
4222 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4225 avi->state = GST_AVI_DEMUX_MOVI;
4227 case GST_AVI_DEMUX_MOVI:
4228 if (G_UNLIKELY (avi->seek_event)) {
4229 gst_avi_demux_push_event (avi, avi->seek_event);
4230 avi->seek_event = NULL;
4232 if (G_UNLIKELY (avi->got_tags)) {
4233 push_tag_lists (avi);
4235 /* process each index entry in turn */
4236 res = gst_avi_demux_stream_data (avi);
4238 /* pause when error */
4239 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4240 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
4245 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
4246 res = GST_FLOW_ERROR;
4250 GST_LOG_OBJECT (avi, "state: %d res:%s", avi->state, gst_flow_get_name (res));
4256 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
4257 avi->segment_running = FALSE;
4258 gst_pad_pause_task (avi->sinkpad);
4260 if (GST_FLOW_IS_FATAL (res) || (res == GST_FLOW_NOT_LINKED)) {
4261 gboolean push_eos = TRUE;
4263 if (res == GST_FLOW_UNEXPECTED) {
4264 /* handle end-of-stream/segment */
4265 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4268 if ((stop = avi->segment.stop) == -1)
4269 stop = avi->segment.duration;
4271 GST_INFO_OBJECT (avi, "sending segment_done");
4273 gst_element_post_message
4275 gst_message_new_segment_done (GST_OBJECT (avi), GST_FORMAT_TIME,
4280 /* for fatal errors we post an error message */
4281 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
4282 (_("Internal data stream error.")),
4283 ("streaming stopped, reason %s", gst_flow_get_name (res)));
4286 GST_INFO_OBJECT (avi, "sending eos");
4287 gst_avi_demux_push_event (avi, gst_event_new_eos ());
4293 static GstFlowReturn
4294 gst_avi_demux_chain (GstPad * pad, GstBuffer * buf)
4297 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4299 GST_DEBUG ("Store %d bytes in adapter", GST_BUFFER_SIZE (buf));
4300 gst_adapter_push (avi->adapter, buf);
4302 switch (avi->state) {
4303 case GST_AVI_DEMUX_START:
4304 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
4305 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4309 case GST_AVI_DEMUX_HEADER:
4310 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
4311 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4315 case GST_AVI_DEMUX_MOVI:
4316 if (G_UNLIKELY (avi->seek_event)) {
4317 gst_avi_demux_push_event (avi, avi->seek_event);
4318 avi->seek_event = NULL;
4320 if (G_UNLIKELY (avi->got_tags)) {
4321 push_tag_lists (avi);
4323 res = gst_avi_demux_stream_data (avi);
4326 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
4327 ("Illegal internal state"));
4328 res = GST_FLOW_ERROR;
4332 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
4333 gst_flow_get_name (res));
4339 gst_avi_demux_sink_activate (GstPad * sinkpad)
4341 if (gst_pad_check_pull_range (sinkpad)) {
4342 GST_DEBUG ("going to pull mode");
4343 return gst_pad_activate_pull (sinkpad, TRUE);
4345 GST_DEBUG ("going to push (streaming) mode");
4346 return gst_pad_activate_push (sinkpad, TRUE);
4351 gst_avi_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
4353 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (sinkpad));
4356 avi->segment_running = TRUE;
4357 avi->streaming = FALSE;
4358 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4361 avi->segment_running = FALSE;
4362 return gst_pad_stop_task (sinkpad);
4367 gst_avi_demux_activate_push (GstPad * pad, gboolean active)
4369 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (pad));
4372 GST_DEBUG ("avi: activating push/chain function");
4373 avi->streaming = TRUE;
4375 GST_DEBUG ("avi: deactivating push/chain function");
4381 static GstStateChangeReturn
4382 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
4384 GstStateChangeReturn ret;
4385 GstAviDemux *avi = GST_AVI_DEMUX (element);
4387 switch (transition) {
4388 case GST_STATE_CHANGE_READY_TO_PAUSED:
4389 avi->streaming = FALSE;
4390 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
4396 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4397 if (ret == GST_STATE_CHANGE_FAILURE)
4400 switch (transition) {
4401 case GST_STATE_CHANGE_PAUSED_TO_READY:
4402 gst_avi_demux_reset (avi);