2 * Copyright (C) <1999> Erik Walthinsen <omega@temple-baptist.com>
3 * Copyright (C) <2006> Nokia Corporation (contact <stefan.kost@nokia.com>)
4 * Copyright (C) <2009-2010> STEricsson <benjamin.gaignard@stericsson.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 /* Element-Checklist-Version: 5 */
24 * SECTION:element-avidemux
26 * Demuxes an .avi file into raw or compressed audio and/or video streams.
28 * This element supports both push and pull-based scheduling, depending on the
29 * capabilities of the upstream elements.
32 * <title>Example launch line</title>
34 * gst-launch-1.0 filesrc location=test.avi ! avidemux name=demux demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink demux.video_00 ! queue ! decodebin ! videoconvert ! videoscale ! autovideosink
35 * ]| Play (parse and decode) an .avi file and try to output it to
36 * an automatically detected soundcard and videosink. If the AVI file contains
37 * compressed audio or video data, this will only work if you have the
38 * right decoder elements/plugins installed.
41 * Last reviewed on 2006-12-29 (0.10.6)
51 #include "gst/riff/riff-media.h"
52 #include "gstavidemux.h"
54 #include <gst/gst-i18n-plugin.h>
55 #include <gst/base/gstadapter.h>
58 #define DIV_ROUND_UP(s,v) (((s) + ((v)-1)) / (v))
60 #define GST_AVI_KEYFRAME 1
61 #define ENTRY_IS_KEYFRAME(e) ((e)->flags == GST_AVI_KEYFRAME)
62 #define ENTRY_SET_KEYFRAME(e) ((e)->flags = GST_AVI_KEYFRAME)
63 #define ENTRY_UNSET_KEYFRAME(e) ((e)->flags = 0)
66 GST_DEBUG_CATEGORY_STATIC (avidemux_debug);
67 #define GST_CAT_DEFAULT avidemux_debug
69 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
72 GST_STATIC_CAPS ("video/x-msvideo")
75 #ifndef GST_DISABLE_GST_DEBUG
76 static const char *const snap_types[2][2] = {
82 static void gst_avi_demux_finalize (GObject * object);
84 static void gst_avi_demux_reset (GstAviDemux * avi);
87 static const GstEventMask *gst_avi_demux_get_event_mask (GstPad * pad);
89 static gboolean gst_avi_demux_handle_src_event (GstPad * pad,
90 GstObject * parent, GstEvent * event);
91 static gboolean gst_avi_demux_handle_sink_event (GstPad * pad,
92 GstObject * parent, GstEvent * event);
93 static gboolean gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event);
96 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
98 static gboolean gst_avi_demux_handle_src_query (GstPad * pad,
99 GstObject * parent, GstQuery * query);
100 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
101 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
103 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
104 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
106 static gboolean gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
108 static void gst_avi_demux_loop (GstPad * pad);
109 static gboolean gst_avi_demux_sink_activate (GstPad * sinkpad,
111 static gboolean gst_avi_demux_sink_activate_mode (GstPad * sinkpad,
112 GstObject * parent, GstPadMode mode, gboolean active);
113 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstObject * parent,
116 static void gst_avi_demux_set_index (GstElement * element, GstIndex * index);
117 static GstIndex *gst_avi_demux_get_index (GstElement * element);
119 static GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
120 GstStateChange transition);
121 static void gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi);
122 static void gst_avi_demux_get_buffer_info (GstAviDemux * avi,
123 GstAviStream * stream, guint entry_n, GstClockTime * timestamp,
124 GstClockTime * ts_end, guint64 * offset, guint64 * offset_end);
126 static void gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf);
128 /* GObject methods */
130 #define gst_avi_demux_parent_class parent_class
131 G_DEFINE_TYPE (GstAviDemux, gst_avi_demux, GST_TYPE_ELEMENT);
134 gst_avi_demux_class_init (GstAviDemuxClass * klass)
136 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
137 GObjectClass *gobject_class = (GObjectClass *) klass;
138 GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl;
139 GstCaps *audcaps, *vidcaps, *subcaps;
141 GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
142 0, "Demuxer for AVI streams");
144 gobject_class->finalize = gst_avi_demux_finalize;
146 gstelement_class->change_state =
147 GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
149 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_avi_demux_set_index);
150 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_avi_demux_get_index);
153 audcaps = gst_riff_create_audio_template_caps ();
154 gst_caps_append (audcaps, gst_caps_new_empty_simple ("audio/x-avi-unknown"));
155 audiosrctempl = gst_pad_template_new ("audio_%u",
156 GST_PAD_SRC, GST_PAD_SOMETIMES, audcaps);
158 vidcaps = gst_riff_create_video_template_caps ();
159 gst_caps_append (vidcaps, gst_riff_create_iavs_template_caps ());
160 gst_caps_append (vidcaps, gst_caps_new_empty_simple ("video/x-avi-unknown"));
161 videosrctempl = gst_pad_template_new ("video_%u",
162 GST_PAD_SRC, GST_PAD_SOMETIMES, vidcaps);
164 subcaps = gst_caps_new_empty_simple ("application/x-subtitle-avi");
165 subsrctempl = gst_pad_template_new ("subtitle_%u",
166 GST_PAD_SRC, GST_PAD_SOMETIMES, subcaps);
167 gst_element_class_add_pad_template (gstelement_class, audiosrctempl);
168 gst_element_class_add_pad_template (gstelement_class, videosrctempl);
169 gst_element_class_add_pad_template (gstelement_class, subsrctempl);
170 gst_element_class_add_pad_template (gstelement_class,
171 gst_static_pad_template_get (&sink_templ));
173 gst_element_class_set_static_metadata (gstelement_class, "Avi demuxer",
175 "Demultiplex an avi file into audio and video",
176 "Erik Walthinsen <omega@cse.ogi.edu>, "
177 "Wim Taymans <wim.taymans@chello.be>, "
178 "Thijs Vermeir <thijsvermeir@gmail.com>");
182 gst_avi_demux_init (GstAviDemux * avi)
184 avi->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
185 gst_pad_set_activate_function (avi->sinkpad,
186 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate));
187 gst_pad_set_activatemode_function (avi->sinkpad,
188 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate_mode));
189 gst_pad_set_chain_function (avi->sinkpad,
190 GST_DEBUG_FUNCPTR (gst_avi_demux_chain));
191 gst_pad_set_event_function (avi->sinkpad,
192 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_sink_event));
193 gst_element_add_pad (GST_ELEMENT_CAST (avi), avi->sinkpad);
195 avi->adapter = gst_adapter_new ();
197 gst_avi_demux_reset (avi);
199 GST_OBJECT_FLAG_SET (avi, GST_ELEMENT_FLAG_INDEXABLE);
203 gst_avi_demux_finalize (GObject * object)
205 GstAviDemux *avi = GST_AVI_DEMUX (object);
207 GST_DEBUG ("AVI: finalize");
209 g_object_unref (avi->adapter);
211 G_OBJECT_CLASS (parent_class)->finalize (object);
215 gst_avi_demux_reset_stream (GstAviDemux * avi, GstAviStream * stream)
217 g_free (stream->strh);
218 g_free (stream->strf.data);
219 g_free (stream->name);
220 g_free (stream->index);
221 g_free (stream->indexes);
222 if (stream->initdata)
223 gst_buffer_unref (stream->initdata);
224 if (stream->extradata)
225 gst_buffer_unref (stream->extradata);
227 if (stream->exposed) {
228 gst_pad_set_active (stream->pad, FALSE);
229 gst_element_remove_pad (GST_ELEMENT_CAST (avi), stream->pad);
231 gst_object_unref (stream->pad);
233 if (stream->taglist) {
234 gst_tag_list_unref (stream->taglist);
235 stream->taglist = NULL;
237 memset (stream, 0, sizeof (GstAviStream));
241 gst_avi_demux_reset (GstAviDemux * avi)
245 GST_DEBUG ("AVI: reset");
247 for (i = 0; i < avi->num_streams; i++)
248 gst_avi_demux_reset_stream (avi, &avi->stream[i]);
250 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
251 avi->num_streams = 0;
252 avi->num_v_streams = 0;
253 avi->num_a_streams = 0;
254 avi->num_t_streams = 0;
255 avi->main_stream = -1;
257 avi->state = GST_AVI_DEMUX_START;
259 avi->building_index = FALSE;
261 avi->index_offset = 0;
266 if (avi->element_index)
267 gst_object_unref (avi->element_index);
268 avi->element_index = NULL;
271 if (avi->seg_event) {
272 gst_event_unref (avi->seg_event);
273 avi->seg_event = NULL;
275 if (avi->seek_event) {
276 gst_event_unref (avi->seek_event);
277 avi->seek_event = NULL;
281 gst_tag_list_unref (avi->globaltags);
282 avi->globaltags = NULL;
284 avi->got_tags = TRUE; /* we always want to push global tags */
285 avi->have_eos = FALSE;
286 avi->seekable = TRUE;
288 gst_adapter_clear (avi->adapter);
290 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
294 /* GstElement methods */
297 static const GstFormat *
298 gst_avi_demux_get_src_formats (GstPad * pad)
300 GstAviStream *stream = gst_pad_get_element_private (pad);
302 static const GstFormat src_a_formats[] = {
308 static const GstFormat src_v_formats[] = {
314 return (stream->strh->type == GST_RIFF_FCC_auds ?
315 src_a_formats : src_v_formats);
319 /* assumes stream->strf.auds->av_bps != 0 */
320 static inline GstClockTime
321 avi_stream_convert_bytes_to_time_unchecked (GstAviStream * stream,
324 return gst_util_uint64_scale_int (bytes, GST_SECOND,
325 stream->strf.auds->av_bps);
328 static inline guint64
329 avi_stream_convert_time_to_bytes_unchecked (GstAviStream * stream,
332 return gst_util_uint64_scale_int (time, stream->strf.auds->av_bps,
336 /* assumes stream->strh->rate != 0 */
337 static inline GstClockTime
338 avi_stream_convert_frames_to_time_unchecked (GstAviStream * stream,
341 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
345 static inline guint64
346 avi_stream_convert_time_to_frames_unchecked (GstAviStream * stream,
349 return gst_util_uint64_scale (time, stream->strh->rate,
350 stream->strh->scale * GST_SECOND);
354 gst_avi_demux_src_convert (GstPad * pad,
355 GstFormat src_format,
356 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
358 GstAviStream *stream = gst_pad_get_element_private (pad);
362 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
363 ", dest_format:%s", gst_format_get_name (src_format), src_value,
364 gst_format_get_name (*dest_format));
366 if (G_UNLIKELY (src_format == *dest_format)) {
367 *dest_value = src_value;
370 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
374 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
375 (src_format == GST_FORMAT_BYTES
376 || *dest_format == GST_FORMAT_BYTES))) {
381 switch (src_format) {
382 case GST_FORMAT_TIME:
383 switch (*dest_format) {
384 case GST_FORMAT_BYTES:
385 *dest_value = gst_util_uint64_scale_int (src_value,
386 stream->strf.auds->av_bps, GST_SECOND);
388 case GST_FORMAT_DEFAULT:
390 gst_util_uint64_scale_round (src_value, stream->strh->rate,
391 stream->strh->scale * GST_SECOND);
398 case GST_FORMAT_BYTES:
399 switch (*dest_format) {
400 case GST_FORMAT_TIME:
401 if (stream->strf.auds->av_bps != 0) {
402 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
412 case GST_FORMAT_DEFAULT:
413 switch (*dest_format) {
414 case GST_FORMAT_TIME:
416 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
429 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
430 gst_format_get_name (*dest_format), *dest_value);
435 gst_avi_demux_handle_src_query (GstPad * pad, GstObject * parent,
439 GstAviDemux *avi = GST_AVI_DEMUX (parent);
441 GstAviStream *stream = gst_pad_get_element_private (pad);
443 if (!stream->strh || !stream->strf.data)
444 return gst_pad_query_default (pad, parent, query);
446 switch (GST_QUERY_TYPE (query)) {
447 case GST_QUERY_POSITION:{
450 GST_DEBUG ("pos query for stream %u: frames %u, bytes %u",
451 stream->num, stream->current_entry, stream->current_total);
453 /* FIXME, this looks clumsy */
454 if (stream->strh->type == GST_RIFF_FCC_auds) {
455 if (stream->is_vbr) {
457 pos = gst_util_uint64_scale ((gint64) stream->current_entry *
458 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
459 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
460 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
461 } else if (stream->strf.auds->av_bps != 0) {
463 pos = gst_util_uint64_scale (stream->current_total, GST_SECOND,
464 (guint64) stream->strf.auds->av_bps);
465 GST_DEBUG_OBJECT (avi,
466 "CBR convert bytes %u, time %" GST_TIME_FORMAT,
467 stream->current_total, GST_TIME_ARGS (pos));
468 } else if (stream->idx_n != 0 && stream->total_bytes != 0) {
469 /* calculate timestamps based on percentage of length */
470 guint64 xlen = avi->avih->us_frame *
471 avi->avih->tot_frames * GST_USECOND;
473 if (stream->is_vbr) {
474 pos = gst_util_uint64_scale (xlen, stream->current_entry,
476 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
477 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
479 pos = gst_util_uint64_scale (xlen, stream->current_total,
480 stream->total_bytes);
481 GST_DEBUG_OBJECT (avi,
482 "CBR perc convert bytes %u, time %" GST_TIME_FORMAT,
483 stream->current_total, GST_TIME_ARGS (pos));
490 if (stream->strh->rate != 0) {
491 pos = gst_util_uint64_scale ((guint64) stream->current_entry *
492 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
494 pos = stream->current_entry * avi->avih->us_frame * GST_USECOND;
498 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
499 gst_query_set_position (query, GST_FORMAT_TIME, pos);
501 GST_WARNING ("pos query failed");
504 case GST_QUERY_DURATION:
507 GstClockTime duration;
509 /* only act on audio or video streams */
510 if (stream->strh->type != GST_RIFF_FCC_auds &&
511 stream->strh->type != GST_RIFF_FCC_vids) {
516 /* take stream duration, fall back to avih duration */
517 if ((duration = stream->duration) == -1)
518 duration = avi->duration;
520 gst_query_parse_duration (query, &fmt, NULL);
523 case GST_FORMAT_TIME:
524 gst_query_set_duration (query, fmt, duration);
526 case GST_FORMAT_DEFAULT:
529 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
532 if (stream->idx_n > 0)
533 gst_query_set_duration (query, fmt, stream->idx_n);
534 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
535 duration, fmt, &dur))
536 gst_query_set_duration (query, fmt, dur);
545 case GST_QUERY_SEEKING:{
548 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
549 if (fmt == GST_FORMAT_TIME) {
550 gboolean seekable = TRUE;
552 if (avi->streaming) {
553 seekable = avi->seekable;
556 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
557 0, stream->duration);
562 case GST_QUERY_CONVERT:{
563 GstFormat src_fmt, dest_fmt;
564 gint64 src_val, dest_val;
566 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
567 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
569 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
571 res = gst_pad_query_default (pad, parent, query);
575 res = gst_pad_query_default (pad, parent, query);
583 static const GstEventMask *
584 gst_avi_demux_get_event_mask (GstPad * pad)
586 static const GstEventMask masks[] = {
587 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
597 gst_avi_demux_seek_streams (GstAviDemux * avi, guint64 offset, gboolean before)
599 GstAviStream *stream;
600 GstIndexEntry *entry;
602 gint64 val, min = offset;
604 for (i = 0; i < avi->num_streams; i++) {
605 stream = &avi->stream[i];
607 entry = gst_index_get_assoc_entry (avi->element_index, stream->index_id,
608 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
609 GST_ASSOCIATION_FLAG_NONE, GST_FORMAT_BYTES, offset);
613 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
614 GST_DEBUG_OBJECT (avi, "stream %d, previous entry at %"
615 G_GUINT64_FORMAT, i, val);
623 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
624 stream->current_entry = 0;
625 stream->current_total = 0;
629 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
630 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT,
633 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &val);
634 stream->current_total = val;
635 gst_index_entry_assoc_map (entry, GST_FORMAT_DEFAULT, &val);
636 stream->current_entry = val;
644 gst_avi_demux_index_entry_offset_search (GstAviIndexEntry * entry,
647 if (entry->offset < *offset)
649 else if (entry->offset > *offset)
655 gst_avi_demux_seek_streams_index (GstAviDemux * avi, guint64 offset,
658 GstAviStream *stream;
659 GstAviIndexEntry *entry;
661 gint64 val, min = offset;
664 for (i = 0; i < avi->num_streams; i++) {
665 stream = &avi->stream[i];
667 /* compensate for chunk header */
670 gst_util_array_binary_search (stream->index, stream->idx_n,
671 sizeof (GstAviIndexEntry),
672 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
673 before ? GST_SEARCH_MODE_BEFORE : GST_SEARCH_MODE_AFTER, &offset, NULL);
677 index = entry - stream->index;
681 val = stream->index[index].offset;
682 GST_DEBUG_OBJECT (avi,
683 "stream %d, previous entry at %" G_GUINT64_FORMAT, i, val);
691 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
692 stream->current_entry = 0;
693 stream->current_total = 0;
697 val = stream->index[index].offset - 8;
698 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT, i,
701 stream->current_total = stream->index[index].total;
702 stream->current_entry = index;
708 #define GST_AVI_SEEK_PUSH_DISPLACE (4 * GST_SECOND)
711 gst_avi_demux_handle_sink_event (GstPad * pad, GstObject * parent,
715 GstAviDemux *avi = GST_AVI_DEMUX (parent);
717 GST_DEBUG_OBJECT (avi,
718 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
720 switch (GST_EVENT_TYPE (event)) {
721 case GST_EVENT_SEGMENT:
723 gint64 boffset, offset = 0;
726 /* some debug output */
727 gst_event_copy_segment (event, &segment);
728 GST_DEBUG_OBJECT (avi, "received newsegment %" GST_SEGMENT_FORMAT,
731 /* chain will send initial newsegment after pads have been added */
732 if (avi->state != GST_AVI_DEMUX_MOVI) {
733 GST_DEBUG_OBJECT (avi, "still starting, eating event");
737 /* we only expect a BYTE segment, e.g. following a seek */
738 if (segment.format != GST_FORMAT_BYTES) {
739 GST_DEBUG_OBJECT (avi, "unsupported segment format, ignoring");
743 if (avi->have_index) {
744 GstAviIndexEntry *entry;
745 guint i = 0, index = 0, k = 0;
746 GstAviStream *stream;
748 /* compensate chunk header, stored index offset points after header */
749 boffset = segment.start + 8;
750 /* find which stream we're on */
752 stream = &avi->stream[i];
754 /* find the index for start bytes offset */
755 entry = gst_util_array_binary_search (stream->index,
756 stream->idx_n, sizeof (GstAviIndexEntry),
757 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
758 GST_SEARCH_MODE_AFTER, &boffset, NULL);
762 index = entry - stream->index;
764 /* we are on the stream with a chunk start offset closest to start */
765 if (!offset || stream->index[index].offset < offset) {
766 offset = stream->index[index].offset;
769 /* exact match needs no further searching */
770 if (stream->index[index].offset == boffset)
772 } while (++i < avi->num_streams);
775 stream = &avi->stream[k];
777 /* so we have no idea what is to come, or where we are */
779 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
783 /* get the ts corresponding to start offset bytes for the stream */
784 gst_avi_demux_get_buffer_info (avi, stream, index,
785 (GstClockTime *) & segment.time, NULL, NULL, NULL);
787 } else if (avi->element_index) {
788 GstIndexEntry *entry;
790 /* Let's check if we have an index entry for this position */
791 entry = gst_index_get_assoc_entry (avi->element_index, avi->index_id,
792 GST_INDEX_LOOKUP_AFTER, GST_ASSOCIATION_FLAG_NONE,
793 GST_FORMAT_BYTES, segment.start);
795 /* we can not go where we have not yet been before ... */
797 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
801 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME,
802 (gint64 *) & segment.time);
803 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &offset);
806 GST_WARNING_OBJECT (avi, "no index data, forcing EOS");
810 segment.format = GST_FORMAT_TIME;
811 segment.start = segment.time;
812 segment.stop = GST_CLOCK_TIME_NONE;
813 segment.position = segment.start;
815 /* rescue duration */
816 segment.duration = avi->segment.duration;
818 /* set up segment and send downstream */
819 gst_segment_copy_into (&segment, &avi->segment);
821 GST_DEBUG_OBJECT (avi, "Pushing newseg %" GST_SEGMENT_FORMAT, &segment);
822 gst_avi_demux_push_event (avi, gst_event_new_segment (&segment));
824 GST_DEBUG_OBJECT (avi, "next chunk expected at %" G_GINT64_FORMAT,
827 /* adjust state for streaming thread accordingly */
829 gst_avi_demux_seek_streams_index (avi, offset, FALSE);
832 gst_avi_demux_seek_streams (avi, offset, FALSE);
835 /* set up streaming thread */
836 g_assert (offset >= boffset);
837 avi->offset = boffset;
838 avi->todrop = offset - boffset;
841 gst_event_unref (event);
846 avi->have_eos = TRUE;
851 if (avi->state != GST_AVI_DEMUX_MOVI) {
852 gst_event_unref (event);
853 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
854 (NULL), ("got eos and didn't receive a complete header object"));
855 } else if (!gst_avi_demux_push_event (avi, event)) {
856 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
857 (NULL), ("got eos but no streams (yet)"));
861 case GST_EVENT_FLUSH_STOP:
865 gst_adapter_clear (avi->adapter);
866 avi->have_eos = FALSE;
867 for (i = 0; i < avi->num_streams; i++) {
868 avi->stream[i].last_flow = GST_FLOW_OK;
869 avi->stream[i].discont = TRUE;
871 /* fall through to default case so that the event gets passed downstream */
874 res = gst_pad_event_default (pad, parent, event);
882 gst_avi_demux_handle_src_event (GstPad * pad, GstObject * parent,
886 GstAviDemux *avi = GST_AVI_DEMUX (parent);
888 GST_DEBUG_OBJECT (avi,
889 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
891 switch (GST_EVENT_TYPE (event)) {
893 if (!avi->streaming) {
894 res = gst_avi_demux_handle_seek (avi, pad, event);
896 res = gst_avi_demux_handle_seek_push (avi, pad, event);
898 gst_event_unref (event);
901 case GST_EVENT_NAVIGATION:
903 gst_event_unref (event);
906 res = gst_pad_event_default (pad, parent, event);
913 /* streaming helper (push) */
916 * gst_avi_demux_peek_chunk_info:
918 * @tag: holder for tag
919 * @size: holder for tag size
921 * Peek next chunk info (tag and size)
923 * Returns: TRUE when one chunk info has been got
926 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
928 const guint8 *data = NULL;
930 if (gst_adapter_available (avi->adapter) < 8)
933 data = gst_adapter_map (avi->adapter, 8);
934 *tag = GST_READ_UINT32_LE (data);
935 *size = GST_READ_UINT32_LE (data + 4);
936 gst_adapter_unmap (avi->adapter);
942 * gst_avi_demux_peek_chunk:
944 * @tag: holder for tag
945 * @size: holder for tag size
947 * Peek enough data for one full chunk
949 * Returns: %TRUE when one chunk has been got
952 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
954 guint32 peek_size = 0;
957 if (!gst_avi_demux_peek_chunk_info (avi, tag, size))
960 /* size 0 -> empty data buffer would surprise most callers,
961 * large size -> do not bother trying to squeeze that into adapter,
962 * so we throw poor man's exception, which can be caught if caller really
963 * wants to handle 0 size chunk */
964 if (!(*size) || (*size) >= (1 << 30))
967 peek_size = (*size + 1) & ~1;
968 available = gst_adapter_available (avi->adapter);
970 GST_DEBUG_OBJECT (avi,
971 "Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
972 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
974 if (available < (8 + peek_size))
982 GST_INFO_OBJECT (avi, "Failed to peek");
987 GST_INFO_OBJECT (avi,
988 "Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT, *size,
989 GST_FOURCC_ARGS (*tag));
990 /* chain should give up */
991 avi->abort_buffering = TRUE;
996 GST_INFO_OBJECT (avi, "need more %d < %" G_GUINT32_FORMAT,
997 available, 8 + peek_size);
1005 * gst_avi_demux_parse_file_header:
1006 * @element: caller element (used for errors/debug).
1007 * @buf: input data to be used for parsing.
1009 * "Open" a RIFF/AVI file. The buffer should be at least 12
1010 * bytes long. Takes ownership of @buf.
1012 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
1013 * Throws an error, caller should error out (fatal).
1016 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
1021 stamp = gst_util_get_timestamp ();
1023 /* riff_parse posts an error */
1024 if (!gst_riff_parse_file_header (element, buf, &doctype))
1027 if (doctype != GST_RIFF_RIFF_AVI)
1030 stamp = gst_util_get_timestamp () - stamp;
1031 GST_DEBUG_OBJECT (element, "header parsing took %" GST_TIME_FORMAT,
1032 GST_TIME_ARGS (stamp));
1039 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
1040 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
1041 GST_FOURCC_ARGS (doctype)));
1047 * Read AVI file tag when streaming
1049 static GstFlowReturn
1050 gst_avi_demux_stream_init_push (GstAviDemux * avi)
1052 if (gst_adapter_available (avi->adapter) >= 12) {
1055 tmp = gst_adapter_take_buffer (avi->adapter, 12);
1057 GST_DEBUG ("Parsing avi header");
1058 if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), tmp)) {
1059 return GST_FLOW_ERROR;
1061 GST_DEBUG ("header ok");
1064 avi->state = GST_AVI_DEMUX_HEADER;
1072 static GstFlowReturn
1073 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
1076 GstBuffer *buf = NULL;
1078 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
1079 if (res != GST_FLOW_OK)
1081 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
1091 GST_DEBUG_OBJECT (avi, "error parsing file header");
1092 return GST_FLOW_ERROR;
1096 /* AVI header handling */
1098 * gst_avi_demux_parse_avih:
1099 * @avi: caller element (used for errors/debug).
1100 * @buf: input data to be used for parsing.
1101 * @avih: pointer to structure (filled in by function) containing
1102 * stream information (such as flags, number of streams, etc.).
1104 * Read 'avih' header. Discards buffer after use.
1106 * Returns: TRUE on success, FALSE otherwise. Throws an error if
1107 * the header is invalid. The caller should error out
1111 gst_avi_demux_parse_avih (GstAviDemux * avi,
1112 GstBuffer * buf, gst_riff_avih ** _avih)
1114 gst_riff_avih *avih;
1120 size = gst_buffer_get_size (buf);
1121 if (size < sizeof (gst_riff_avih))
1122 goto avih_too_small;
1124 avih = g_malloc (size);
1125 gst_buffer_extract (buf, 0, avih, size);
1127 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1128 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
1129 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
1130 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
1131 avih->flags = GUINT32_FROM_LE (avih->flags);
1132 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
1133 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
1134 avih->streams = GUINT32_FROM_LE (avih->streams);
1135 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
1136 avih->width = GUINT32_FROM_LE (avih->width);
1137 avih->height = GUINT32_FROM_LE (avih->height);
1138 avih->scale = GUINT32_FROM_LE (avih->scale);
1139 avih->rate = GUINT32_FROM_LE (avih->rate);
1140 avih->start = GUINT32_FROM_LE (avih->start);
1141 avih->length = GUINT32_FROM_LE (avih->length);
1145 GST_INFO_OBJECT (avi, "avih tag found:");
1146 GST_INFO_OBJECT (avi, " us_frame %u", avih->us_frame);
1147 GST_INFO_OBJECT (avi, " max_bps %u", avih->max_bps);
1148 GST_INFO_OBJECT (avi, " pad_gran %u", avih->pad_gran);
1149 GST_INFO_OBJECT (avi, " flags 0x%08x", avih->flags);
1150 GST_INFO_OBJECT (avi, " tot_frames %u", avih->tot_frames);
1151 GST_INFO_OBJECT (avi, " init_frames %u", avih->init_frames);
1152 GST_INFO_OBJECT (avi, " streams %u", avih->streams);
1153 GST_INFO_OBJECT (avi, " bufsize %u", avih->bufsize);
1154 GST_INFO_OBJECT (avi, " width %u", avih->width);
1155 GST_INFO_OBJECT (avi, " height %u", avih->height);
1156 GST_INFO_OBJECT (avi, " scale %u", avih->scale);
1157 GST_INFO_OBJECT (avi, " rate %u", avih->rate);
1158 GST_INFO_OBJECT (avi, " start %u", avih->start);
1159 GST_INFO_OBJECT (avi, " length %u", avih->length);
1162 gst_buffer_unref (buf);
1164 if (avih->us_frame != 0 && avih->tot_frames != 0)
1166 (guint64) avih->us_frame * (guint64) avih->tot_frames * 1000;
1168 avi->duration = GST_CLOCK_TIME_NONE;
1170 GST_INFO_OBJECT (avi, " header duration %" GST_TIME_FORMAT,
1171 GST_TIME_ARGS (avi->duration));
1178 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No buffer"));
1183 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
1184 ("Too small avih (%" G_GSIZE_FORMAT " available, %d needed)",
1185 size, (int) sizeof (gst_riff_avih)));
1186 gst_buffer_unref (buf);
1192 * gst_avi_demux_parse_superindex:
1193 * @avi: caller element (used for debugging/errors).
1194 * @buf: input data to use for parsing.
1195 * @locations: locations in the file (byte-offsets) that contain
1196 * the actual indexes (see get_avi_demux_parse_subindex()).
1197 * The array ends with GST_BUFFER_OFFSET_NONE.
1199 * Reads superindex (openDML-2 spec stuff) from the provided data.
1201 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
1202 * on error, but they are not fatal.
1205 gst_avi_demux_parse_superindex (GstAviDemux * avi,
1206 GstBuffer * buf, guint64 ** _indexes)
1218 gst_buffer_map (buf, &map, GST_MAP_READ);
1229 /* check type of index. The opendml2 specs state that
1230 * there should be 4 dwords per array entry. Type can be
1231 * either frame or field (and we don't care). */
1232 if (GST_READ_UINT16_LE (data) != 4 ||
1233 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
1234 GST_WARNING_OBJECT (avi,
1235 "Superindex for stream has unexpected "
1236 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1237 GST_READ_UINT16_LE (data), data[2], data[3]);
1238 bpe = GST_READ_UINT16_LE (data) * 4;
1240 num = GST_READ_UINT32_LE (&data[4]);
1242 GST_DEBUG_OBJECT (avi, "got %d indexes", num);
1244 /* this can't work out well ... */
1245 if (num > G_MAXUINT32 >> 1 || bpe < 8) {
1246 goto invalid_params;
1249 indexes = g_new (guint64, num + 1);
1250 for (i = 0; i < num; i++) {
1251 if (size < 24 + bpe * (i + 1))
1253 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
1254 GST_DEBUG_OBJECT (avi, "index %d at %" G_GUINT64_FORMAT, i, indexes[i]);
1256 indexes[i] = GST_BUFFER_OFFSET_NONE;
1257 *_indexes = indexes;
1259 gst_buffer_unmap (buf, &map);
1260 gst_buffer_unref (buf);
1267 GST_ERROR_OBJECT (avi,
1268 "Not enough data to parse superindex (%" G_GSIZE_FORMAT
1269 " available, 24 needed)", size);
1271 gst_buffer_unmap (buf, &map);
1272 gst_buffer_unref (buf);
1278 GST_ERROR_OBJECT (avi, "invalid index parameters (num = %d, bpe = %d)",
1280 gst_buffer_unmap (buf, &map);
1281 gst_buffer_unref (buf);
1286 /* add an entry to the index of a stream. @num should be an estimate of the
1287 * total amount of index entries for all streams and is used to dynamically
1288 * allocate memory for the index entries. */
1289 static inline gboolean
1290 gst_avi_demux_add_index (GstAviDemux * avi, GstAviStream * stream,
1291 guint num, GstAviIndexEntry * entry)
1293 /* ensure index memory */
1294 if (G_UNLIKELY (stream->idx_n >= stream->idx_max)) {
1295 guint idx_max = stream->idx_max;
1296 GstAviIndexEntry *new_idx;
1298 /* we need to make some more room */
1300 /* initial size guess, assume each stream has an equal amount of entries,
1301 * overshoot with at least 8K */
1302 idx_max = (num / avi->num_streams) + (8192 / sizeof (GstAviIndexEntry));
1304 idx_max += 8192 / sizeof (GstAviIndexEntry);
1305 GST_DEBUG_OBJECT (avi, "expanded index from %u to %u",
1306 stream->idx_max, idx_max);
1308 new_idx = g_try_renew (GstAviIndexEntry, stream->index, idx_max);
1309 /* out of memory, if this fails stream->index is untouched. */
1310 if (G_UNLIKELY (!new_idx))
1313 stream->index = new_idx;
1314 stream->idx_max = idx_max;
1317 /* update entry total and stream stats. The entry total can be converted to
1318 * the timestamp of the entry easily. */
1319 if (stream->strh->type == GST_RIFF_FCC_auds) {
1322 if (stream->is_vbr) {
1323 entry->total = stream->total_blocks;
1325 entry->total = stream->total_bytes;
1327 blockalign = stream->strf.auds->blockalign;
1329 stream->total_blocks += DIV_ROUND_UP (entry->size, blockalign);
1331 stream->total_blocks++;
1333 if (stream->is_vbr) {
1334 entry->total = stream->idx_n;
1336 entry->total = stream->total_bytes;
1339 stream->total_bytes += entry->size;
1340 if (ENTRY_IS_KEYFRAME (entry))
1341 stream->n_keyframes++;
1344 GST_LOG_OBJECT (avi,
1345 "Adding stream %u, index entry %d, kf %d, size %u "
1346 ", offset %" G_GUINT64_FORMAT ", total %" G_GUINT64_FORMAT, stream->num,
1347 stream->idx_n, ENTRY_IS_KEYFRAME (entry), entry->size, entry->offset,
1349 stream->index[stream->idx_n++] = *entry;
1354 /* given @entry_n in @stream, calculate info such as timestamps and
1355 * offsets for the entry. */
1357 gst_avi_demux_get_buffer_info (GstAviDemux * avi, GstAviStream * stream,
1358 guint entry_n, GstClockTime * timestamp, GstClockTime * ts_end,
1359 guint64 * offset, guint64 * offset_end)
1361 GstAviIndexEntry *entry;
1363 entry = &stream->index[entry_n];
1365 if (stream->is_vbr) {
1366 /* VBR stream next timestamp */
1367 if (stream->strh->type == GST_RIFF_FCC_auds) {
1370 avi_stream_convert_frames_to_time_unchecked (stream, entry->total);
1372 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1377 avi_stream_convert_frames_to_time_unchecked (stream, entry_n);
1379 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1382 } else if (stream->strh->type == GST_RIFF_FCC_auds) {
1383 /* constant rate stream */
1386 avi_stream_convert_bytes_to_time_unchecked (stream, entry->total);
1388 *ts_end = avi_stream_convert_bytes_to_time_unchecked (stream,
1389 entry->total + entry->size);
1391 if (stream->strh->type == GST_RIFF_FCC_vids) {
1392 /* video offsets are the frame number */
1396 *offset_end = entry_n + 1;
1398 /* no offsets for audio */
1406 /* collect and debug stats about the indexes for all streams.
1407 * This method is also responsible for filling in the stream duration
1408 * as measured by the amount of index entries.
1410 * Returns TRUE if the index is not empty, else FALSE */
1412 gst_avi_demux_do_index_stats (GstAviDemux * avi)
1414 guint total_idx = 0;
1416 #ifndef GST_DISABLE_GST_DEBUG
1417 guint total_max = 0;
1420 /* get stream stats now */
1421 for (i = 0; i < avi->num_streams; i++) {
1422 GstAviStream *stream;
1424 if (G_UNLIKELY (!(stream = &avi->stream[i])))
1426 if (G_UNLIKELY (!stream->strh))
1428 if (G_UNLIKELY (!stream->index || stream->idx_n == 0))
1431 /* we interested in the end_ts of the last entry, which is the total
1432 * duration of this stream */
1433 gst_avi_demux_get_buffer_info (avi, stream, stream->idx_n - 1,
1434 NULL, &stream->idx_duration, NULL, NULL);
1436 total_idx += stream->idx_n;
1437 #ifndef GST_DISABLE_GST_DEBUG
1438 total_max += stream->idx_max;
1440 GST_INFO_OBJECT (avi, "Stream %d, dur %" GST_TIME_FORMAT ", %6u entries, "
1441 "%5u keyframes, entry size = %2u, total size = %10u, allocated %10u",
1442 i, GST_TIME_ARGS (stream->idx_duration), stream->idx_n,
1443 stream->n_keyframes, (guint) sizeof (GstAviIndexEntry),
1444 (guint) (stream->idx_n * sizeof (GstAviIndexEntry)),
1445 (guint) (stream->idx_max * sizeof (GstAviIndexEntry)));
1447 total_idx *= sizeof (GstAviIndexEntry);
1448 #ifndef GST_DISABLE_GST_DEBUG
1449 total_max *= sizeof (GstAviIndexEntry);
1451 GST_INFO_OBJECT (avi, "%u bytes for index vs %u ideally, %u wasted",
1452 total_max, total_idx, total_max - total_idx);
1454 if (total_idx == 0) {
1455 GST_WARNING_OBJECT (avi, "Index is empty !");
1462 * gst_avi_demux_parse_subindex:
1464 * @buf: input data to use for parsing.
1465 * @stream: stream context.
1466 * @entries_list: a list (returned by the function) containing all the
1467 * indexes parsed in this specific subindex. The first
1468 * entry is also a pointer to allocated memory that needs
1469 * to be free´ed. May be NULL if no supported indexes were
1472 * Reads superindex (openDML-2 spec stuff) from the provided data.
1473 * The buffer should contain a GST_RIFF_TAG_ix?? chunk.
1475 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1476 * throw an error, caller should bail out asap.
1479 gst_avi_demux_parse_subindex (GstAviDemux * avi, GstAviStream * stream,
1491 gst_buffer_map (buf, &map, GST_MAP_READ);
1498 /* We don't support index-data yet */
1500 goto not_implemented;
1502 /* check type of index. The opendml2 specs state that
1503 * there should be 4 dwords per array entry. Type can be
1504 * either frame or field (and we don't care). */
1505 bpe = (data[2] & 0x01) ? 12 : 8;
1506 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1507 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1508 GST_WARNING_OBJECT (avi,
1509 "Superindex for stream %d has unexpected "
1510 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1511 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1512 bpe = GST_READ_UINT16_LE (data) * 4;
1514 num = GST_READ_UINT32_LE (&data[4]);
1515 baseoff = GST_READ_UINT64_LE (&data[12]);
1517 /* If there's nothing, just return ! */
1521 GST_INFO_OBJECT (avi, "Parsing subindex, nr_entries = %6d", num);
1523 for (i = 0; i < num; i++) {
1524 GstAviIndexEntry entry;
1526 if (map.size < 24 + bpe * (i + 1))
1529 /* fill in offset and size. offset contains the keyframe flag in the
1531 entry.offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1532 entry.size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1534 if (stream->strh->type == GST_RIFF_FCC_auds) {
1535 /* all audio frames are keyframes */
1536 ENTRY_SET_KEYFRAME (&entry);
1538 /* else read flags */
1539 entry.flags = (entry.size & 0x80000000) ? 0 : GST_AVI_KEYFRAME;
1541 entry.size &= ~0x80000000;
1544 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
1548 gst_buffer_unmap (buf, &map);
1549 gst_buffer_unref (buf);
1556 GST_ERROR_OBJECT (avi,
1557 "Not enough data to parse subindex (%" G_GSIZE_FORMAT
1558 " available, 24 needed)", map.size);
1559 goto done; /* continue */
1563 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1564 ("Subindex-is-data is not implemented"));
1565 gst_buffer_unmap (buf, &map);
1566 gst_buffer_unref (buf);
1571 GST_DEBUG_OBJECT (avi, "the index is empty");
1572 goto done; /* continue */
1576 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1577 ("Cannot allocate memory for %u*%u=%u bytes",
1578 (guint) sizeof (GstAviIndexEntry), num,
1579 (guint) sizeof (GstAviIndexEntry) * num));
1580 gst_buffer_unmap (buf, &map);
1581 gst_buffer_unref (buf);
1587 * Create and push a flushing seek event upstream
1590 perform_seek_to_offset (GstAviDemux * demux, guint64 offset)
1595 GST_DEBUG_OBJECT (demux, "Seeking to %" G_GUINT64_FORMAT, offset);
1598 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1599 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1600 GST_SEEK_TYPE_NONE, -1);
1602 res = gst_pad_push_event (demux->sinkpad, event);
1605 demux->offset = offset;
1610 * Read AVI index when streaming
1613 gst_avi_demux_read_subindexes_push (GstAviDemux * avi)
1615 guint32 tag = 0, size;
1616 GstBuffer *buf = NULL;
1619 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1621 if (avi->odml_subidxs[avi->odml_subidx] != avi->offset)
1624 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1627 /* this is the ODML chunk we expect */
1628 odml_stream = avi->odml_stream;
1630 if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + odml_stream / 10,
1631 '0' + odml_stream % 10)) &&
1632 (tag != GST_MAKE_FOURCC ('0' + odml_stream / 10,
1633 '0' + odml_stream % 10, 'i', 'x'))) {
1634 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1635 GST_FOURCC_ARGS (tag));
1639 avi->offset += 8 + GST_ROUND_UP_2 (size);
1640 /* flush chunk header so we get just the 'size' payload data */
1641 gst_adapter_flush (avi->adapter, 8);
1642 buf = gst_adapter_take_buffer (avi->adapter, size);
1644 if (!gst_avi_demux_parse_subindex (avi, &avi->stream[odml_stream], buf))
1647 /* we parsed the index, go to next subindex */
1650 if (avi->odml_subidxs[avi->odml_subidx] == GST_BUFFER_OFFSET_NONE) {
1651 /* we reached the end of the indexes for this stream, move to the next
1652 * stream to handle the first index */
1654 avi->odml_subidx = 0;
1656 if (avi->odml_stream < avi->num_streams) {
1657 /* there are more indexes */
1658 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
1660 /* we're done, get stream stats now */
1661 avi->have_index = gst_avi_demux_do_index_stats (avi);
1667 /* seek to next index */
1668 return perform_seek_to_offset (avi, avi->odml_subidxs[avi->odml_subidx]);
1675 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi)
1681 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1683 for (n = 0; n < avi->num_streams; n++) {
1684 GstAviStream *stream = &avi->stream[n];
1686 if (stream->indexes == NULL)
1689 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1690 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi), avi->sinkpad,
1691 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1693 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1694 '0' + stream->num % 10)) &&
1695 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1696 '0' + stream->num % 10, 'i', 'x'))) {
1697 /* Some ODML files (created by god knows what muxer) have a ##ix format
1698 * instead of the 'official' ix##. They are still valid though. */
1699 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1700 GST_FOURCC_ARGS (tag));
1701 gst_buffer_unref (buf);
1705 if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1709 g_free (stream->indexes);
1710 stream->indexes = NULL;
1712 /* get stream stats now */
1713 avi->have_index = gst_avi_demux_do_index_stats (avi);
1717 * gst_avi_demux_riff_parse_vprp:
1718 * @element: caller element (used for debugging/error).
1719 * @buf: input data to be used for parsing, stripped from header.
1720 * @vprp: a pointer (returned by this function) to a filled-in vprp
1721 * structure. Caller should free it.
1723 * Parses a video stream´s vprp. This function takes ownership of @buf.
1725 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1726 * should be skipped on error, but it is not fatal.
1729 gst_avi_demux_riff_parse_vprp (GstElement * element,
1730 GstBuffer * buf, gst_riff_vprp ** _vprp)
1732 gst_riff_vprp *vprp;
1736 g_return_val_if_fail (buf != NULL, FALSE);
1737 g_return_val_if_fail (_vprp != NULL, FALSE);
1739 size = gst_buffer_get_size (buf);
1741 if (size < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1744 vprp = g_malloc (size);
1745 gst_buffer_extract (buf, 0, vprp, size);
1747 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1748 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1749 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1750 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1751 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1752 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1753 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1754 vprp->width = GUINT32_FROM_LE (vprp->width);
1755 vprp->height = GUINT32_FROM_LE (vprp->height);
1756 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1760 /* calculate fields based on size */
1761 k = (size - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) / vprp->fields;
1762 if (vprp->fields > k) {
1763 GST_WARNING_OBJECT (element,
1764 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1767 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1768 GST_WARNING_OBJECT (element,
1769 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1770 GST_RIFF_VPRP_VIDEO_FIELDS);
1771 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1773 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1774 for (k = 0; k < vprp->fields; k++) {
1775 gst_riff_vprp_video_field_desc *fd;
1777 fd = &vprp->field_info[k];
1778 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1779 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1780 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1781 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1782 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1783 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1784 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1785 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1790 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1791 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1792 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1793 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1794 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1795 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1796 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1797 vprp->aspect & 0xffff);
1798 GST_INFO_OBJECT (element, " width %d", vprp->width);
1799 GST_INFO_OBJECT (element, " height %d", vprp->height);
1800 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1801 for (k = 0; k < vprp->fields; k++) {
1802 gst_riff_vprp_video_field_desc *fd;
1804 fd = &(vprp->field_info[k]);
1805 GST_INFO_OBJECT (element, " field %u description:", k);
1806 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1807 fd->compressed_bm_height);
1808 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1809 fd->compressed_bm_width);
1810 GST_INFO_OBJECT (element, " valid_bm_height %d",
1811 fd->valid_bm_height);
1812 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1813 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1814 fd->valid_bm_x_offset);
1815 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1816 fd->valid_bm_y_offset);
1817 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1818 fd->video_x_t_offset);
1819 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1822 gst_buffer_unref (buf);
1831 GST_ERROR_OBJECT (element,
1832 "Too small vprp (%" G_GSIZE_FORMAT " available, at least %d needed)",
1833 size, (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1834 gst_buffer_unref (buf);
1840 gst_avi_demux_expose_streams (GstAviDemux * avi, gboolean force)
1844 GST_DEBUG_OBJECT (avi, "force : %d", force);
1846 for (i = 0; i < avi->num_streams; i++) {
1847 GstAviStream *stream = &avi->stream[i];
1849 if (force || stream->idx_n != 0) {
1850 GST_LOG_OBJECT (avi, "Adding pad %s" GST_PTR_FORMAT,
1851 GST_PAD_NAME (stream->pad));
1852 gst_element_add_pad ((GstElement *) avi, stream->pad);
1855 if (avi->element_index)
1856 gst_index_get_writer_id (avi->element_index,
1857 GST_OBJECT_CAST (stream->pad), &stream->index_id);
1860 stream->exposed = TRUE;
1861 if (avi->main_stream == -1)
1862 avi->main_stream = i;
1864 GST_WARNING_OBJECT (avi, "Stream #%d doesn't have any entry, removing it",
1866 gst_avi_demux_reset_stream (avi, stream);
1871 /* buf contains LIST chunk data, and will be padded to even size,
1872 * since some buggy files do not account for the padding of chunks
1873 * within a LIST in the size of the LIST */
1875 gst_avi_demux_roundup_list (GstAviDemux * avi, GstBuffer ** buf)
1879 size = gst_buffer_get_size (*buf);
1881 if (G_UNLIKELY (size & 1)) {
1885 GST_DEBUG_OBJECT (avi, "rounding up dubious list size %" G_GSIZE_FORMAT,
1887 obuf = gst_buffer_new_and_alloc (size + 1);
1889 gst_buffer_map (obuf, &map, GST_MAP_WRITE);
1890 gst_buffer_extract (*buf, 0, map.data, size);
1891 /* assume 0 padding, at least makes outcome deterministic */
1893 gst_buffer_unmap (obuf, &map);
1894 gst_buffer_replace (buf, obuf);
1899 gst_avi_demux_check_caps (GstAviDemux * avi, GstCaps * caps)
1905 s = gst_caps_get_structure (caps, 0);
1906 if (!gst_structure_has_name (s, "video/x-h264"))
1909 GST_DEBUG_OBJECT (avi, "checking caps %" GST_PTR_FORMAT, caps);
1911 /* some muxers put invalid bytestream stuff in h264 extra data */
1912 val = gst_structure_get_value (s, "codec_data");
1913 if (val && (buf = gst_value_get_buffer (val))) {
1918 gst_buffer_map (buf, &map, GST_MAP_READ);
1922 guint32 h = GST_READ_UINT32_BE (data);
1923 gst_buffer_unmap (buf, &map);
1925 /* can hardly be valid AVC codec data */
1926 GST_DEBUG_OBJECT (avi,
1927 "discarding invalid codec_data containing byte-stream");
1928 /* so do not pretend to downstream that it is packetized avc */
1929 gst_structure_remove_field (s, "codec_data");
1930 /* ... but rather properly parsed bytestream */
1931 gst_structure_set (s, "stream-format", G_TYPE_STRING, "byte-stream",
1932 "alignment", G_TYPE_STRING, "au", NULL);
1935 gst_buffer_unmap (buf, &map);
1943 * gst_avi_demux_parse_stream:
1944 * @avi: calling element (used for debugging/errors).
1945 * @buf: input buffer used to parse the stream.
1947 * Parses all subchunks in a strl chunk (which defines a single
1948 * stream). Discards the buffer after use. This function will
1949 * increment the stream counter internally.
1951 * Returns: whether the stream was identified successfully.
1952 * Errors are not fatal. It does indicate the stream
1956 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
1958 GstAviStream *stream;
1959 GstElementClass *klass;
1960 GstPadTemplate *templ;
1961 GstBuffer *sub = NULL;
1964 gchar *codec_name = NULL, *padname = NULL;
1965 const gchar *tag_name;
1966 GstCaps *caps = NULL;
1968 GstElement *element;
1969 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
1970 gst_riff_vprp *vprp = NULL;
1973 element = GST_ELEMENT_CAST (avi);
1975 GST_DEBUG_OBJECT (avi, "Parsing stream");
1977 gst_avi_demux_roundup_list (avi, &buf);
1979 if (avi->num_streams >= GST_AVI_DEMUX_MAX_STREAMS) {
1980 GST_WARNING_OBJECT (avi,
1981 "maximum no of streams (%d) exceeded, ignoring stream",
1982 GST_AVI_DEMUX_MAX_STREAMS);
1983 gst_buffer_unref (buf);
1984 /* not a fatal error, let's say */
1988 stream = &avi->stream[avi->num_streams];
1990 /* initial settings */
1991 stream->idx_duration = GST_CLOCK_TIME_NONE;
1992 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1993 stream->duration = GST_CLOCK_TIME_NONE;
1995 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
1996 /* sub can be NULL if the chunk is empty */
1998 GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
1999 GST_FOURCC_ARGS (tag));
2003 case GST_RIFF_TAG_strh:
2005 gst_riff_strh *strh;
2008 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
2011 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
2012 /* ownership given away */
2014 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
2018 strh = stream->strh;
2019 /* sanity check; stream header frame rate matches global header
2021 if (stream->strh->type == GST_RIFF_FCC_vids) {
2023 GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
2025 s_dur = gst_util_uint64_scale (GST_SECOND, strh->scale, strh->rate);
2026 GST_DEBUG_OBJECT (avi, "verifying stream framerate %d/%d, "
2027 "frame duration = %d ms", strh->rate, strh->scale,
2028 (gint) (s_dur / GST_MSECOND));
2029 if (h_dur > (10 * GST_MSECOND) && (s_dur > 10 * h_dur)) {
2030 strh->rate = GST_SECOND / GST_USECOND;
2031 strh->scale = h_dur / GST_USECOND;
2032 GST_DEBUG_OBJECT (avi, "correcting stream framerate to %d/%d",
2033 strh->rate, strh->scale);
2036 /* determine duration as indicated by header */
2037 stream->hdr_duration = gst_util_uint64_scale ((guint64) strh->length *
2038 strh->scale, GST_SECOND, (guint64) strh->rate);
2039 GST_INFO ("Stream duration according to header: %" GST_TIME_FORMAT,
2040 GST_TIME_ARGS (stream->hdr_duration));
2041 if (stream->hdr_duration == 0)
2042 stream->hdr_duration = GST_CLOCK_TIME_NONE;
2047 case GST_RIFF_TAG_strf:
2049 gboolean res = FALSE;
2052 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
2056 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
2059 switch (stream->strh->type) {
2060 case GST_RIFF_FCC_vids:
2061 stream->is_vbr = TRUE;
2062 res = gst_riff_parse_strf_vids (element, sub,
2063 &stream->strf.vids, &stream->extradata);
2065 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
2067 case GST_RIFF_FCC_auds:
2069 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
2070 &stream->extradata);
2074 stream->is_vbr = (stream->strh->samplesize == 0)
2075 && stream->strh->scale > 1
2076 && stream->strf.auds->blockalign != 1;
2077 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
2078 stream->is_vbr, res);
2079 /* we need these or we have no way to come up with timestamps */
2080 if ((!stream->is_vbr && !stream->strf.auds->av_bps) ||
2081 (stream->is_vbr && (!stream->strh->scale ||
2082 !stream->strh->rate))) {
2083 GST_WARNING_OBJECT (element,
2084 "invalid audio header, ignoring stream");
2087 /* some more sanity checks */
2088 if (stream->is_vbr) {
2089 if (stream->strf.auds->blockalign <= 4) {
2090 /* that would mean (too) many frames per chunk,
2091 * so not likely set as expected */
2092 GST_DEBUG_OBJECT (element,
2093 "suspicious blockalign %d for VBR audio; "
2094 "overriding to 1 frame per chunk",
2095 stream->strf.auds->blockalign);
2096 /* this should top any likely value */
2097 stream->strf.auds->blockalign = (1 << 12);
2101 case GST_RIFF_FCC_iavs:
2102 stream->is_vbr = TRUE;
2103 res = gst_riff_parse_strf_iavs (element, sub,
2104 &stream->strf.iavs, &stream->extradata);
2106 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
2108 case GST_RIFF_FCC_txts:
2109 /* nothing to parse here */
2110 stream->is_vbr = (stream->strh->samplesize == 0)
2111 && (stream->strh->scale > 1);
2115 GST_ERROR_OBJECT (avi,
2116 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
2117 GST_FOURCC_ARGS (stream->strh->type));
2121 gst_buffer_unref (sub);
2129 case GST_RIFF_TAG_vprp:
2132 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
2136 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
2140 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
2144 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
2145 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
2146 /* not considered fatal */
2154 case GST_RIFF_TAG_strd:
2155 if (stream->initdata)
2156 gst_buffer_unref (stream->initdata);
2157 stream->initdata = sub;
2160 case GST_RIFF_TAG_strn:
2161 g_free (stream->name);
2165 gst_buffer_map (sub, &map, GST_MAP_READ);
2166 stream->name = g_strndup ((gchar *) map.data, map.size);
2167 gst_buffer_unmap (sub, &map);
2168 gst_buffer_unref (sub);
2171 stream->name = g_strdup ("");
2173 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
2176 gst_avi_demux_parse_idit (avi, sub);
2179 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
2180 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
2181 '0' + avi->num_streams % 10)) {
2182 g_free (stream->indexes);
2183 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
2184 stream->superindex = TRUE;
2188 GST_WARNING_OBJECT (avi,
2189 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
2190 GST_FOURCC_ARGS (tag));
2192 case GST_RIFF_TAG_JUNQ:
2193 case GST_RIFF_TAG_JUNK:
2197 gst_buffer_unref (sub);
2203 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
2208 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
2212 /* get class to figure out the template */
2213 klass = GST_ELEMENT_GET_CLASS (avi);
2215 /* we now have all info, let´s set up a pad and a caps and be done */
2216 /* create stream name + pad */
2217 switch (stream->strh->type) {
2218 case GST_RIFF_FCC_vids:{
2221 fourcc = (stream->strf.vids->compression) ?
2222 stream->strf.vids->compression : stream->strh->fcc_handler;
2223 padname = g_strdup_printf ("video_%u", avi->num_v_streams);
2224 templ = gst_element_class_get_pad_template (klass, "video_%u");
2225 caps = gst_riff_create_video_caps (fourcc, stream->strh,
2226 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
2228 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2229 G_TYPE_INT, fourcc, NULL);
2230 } else if (got_vprp && vprp) {
2231 guint32 aspect_n, aspect_d;
2234 aspect_n = vprp->aspect >> 16;
2235 aspect_d = vprp->aspect & 0xffff;
2236 /* calculate the pixel aspect ratio using w/h and aspect ratio */
2237 n = aspect_n * stream->strf.vids->height;
2238 d = aspect_d * stream->strf.vids->width;
2240 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
2242 /* very local, not needed elsewhere */
2246 caps = gst_avi_demux_check_caps (avi, caps);
2247 tag_name = GST_TAG_VIDEO_CODEC;
2248 avi->num_v_streams++;
2251 case GST_RIFF_FCC_auds:{
2252 /* FIXME: Do something with the channel reorder map */
2253 padname = g_strdup_printf ("audio_%u", avi->num_a_streams);
2254 templ = gst_element_class_get_pad_template (klass, "audio_%u");
2255 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
2256 stream->strh, stream->strf.auds, stream->extradata,
2257 stream->initdata, &codec_name, NULL);
2259 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
2260 G_TYPE_INT, stream->strf.auds->format, NULL);
2262 tag_name = GST_TAG_AUDIO_CODEC;
2263 avi->num_a_streams++;
2266 case GST_RIFF_FCC_iavs:{
2267 guint32 fourcc = stream->strh->fcc_handler;
2269 padname = g_strdup_printf ("video_%u", avi->num_v_streams);
2270 templ = gst_element_class_get_pad_template (klass, "video_%u");
2271 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
2272 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
2274 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2275 G_TYPE_INT, fourcc, NULL);
2277 tag_name = GST_TAG_VIDEO_CODEC;
2278 avi->num_v_streams++;
2281 case GST_RIFF_FCC_txts:{
2282 padname = g_strdup_printf ("subtitle_%u", avi->num_t_streams);
2283 templ = gst_element_class_get_pad_template (klass, "subtitle_%u");
2284 caps = gst_caps_new_empty_simple ("application/x-subtitle-avi");
2286 avi->num_t_streams++;
2290 g_return_val_if_reached (FALSE);
2293 /* no caps means no stream */
2295 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
2299 GST_DEBUG_OBJECT (element, "codec-name=%s",
2300 (codec_name ? codec_name : "NULL"));
2301 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
2303 /* set proper settings and add it */
2305 gst_object_unref (stream->pad);
2306 pad = stream->pad = gst_pad_new_from_template (templ, padname);
2309 gst_pad_use_fixed_caps (pad);
2311 gst_pad_set_formats_function (pad,
2312 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
2313 gst_pad_set_event_mask_function (pad,
2314 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
2316 gst_pad_set_event_function (pad,
2317 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
2318 gst_pad_set_query_function (pad,
2319 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
2321 gst_pad_set_convert_function (pad,
2322 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
2325 stream->num = avi->num_streams;
2327 stream->start_entry = 0;
2328 stream->step_entry = 0;
2329 stream->stop_entry = 0;
2331 stream->current_entry = -1;
2332 stream->current_total = 0;
2334 stream->last_flow = GST_FLOW_OK;
2335 stream->discont = TRUE;
2337 stream->total_bytes = 0;
2338 stream->total_blocks = 0;
2339 stream->n_keyframes = 0;
2342 stream->idx_max = 0;
2344 gst_pad_set_element_private (pad, stream);
2347 gst_pad_set_active (pad, TRUE);
2349 gst_pad_create_stream_id_printf (pad, GST_ELEMENT_CAST (avi), "%u",
2351 gst_pad_push_event (pad, gst_event_new_stream_start (stream_id));
2353 gst_pad_set_caps (pad, caps);
2354 gst_caps_unref (caps);
2358 if (!stream->taglist)
2359 stream->taglist = gst_tag_list_new_empty ();
2361 avi->got_tags = TRUE;
2363 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
2365 g_free (codec_name);
2368 gst_buffer_unref (buf);
2375 /* unref any mem that may be in use */
2377 gst_buffer_unref (buf);
2379 gst_buffer_unref (sub);
2381 g_free (codec_name);
2382 gst_avi_demux_reset_stream (avi, stream);
2389 * gst_avi_demux_parse_odml:
2390 * @avi: calling element (used for debug/error).
2391 * @buf: input buffer to be used for parsing.
2393 * Read an openDML-2.0 extension header. Fills in the frame number
2394 * in the avi demuxer object when reading succeeds.
2397 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
2401 GstBuffer *sub = NULL;
2403 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
2406 case GST_RIFF_TAG_dmlh:{
2407 gst_riff_dmlh dmlh, *_dmlh;
2410 /* sub == NULL is possible and means an empty buffer */
2414 gst_buffer_map (sub, &map, GST_MAP_READ);
2417 if (map.size < sizeof (gst_riff_dmlh)) {
2418 GST_ERROR_OBJECT (avi,
2419 "DMLH entry is too small (%" G_GSIZE_FORMAT " bytes, %d needed)",
2420 map.size, (int) sizeof (gst_riff_dmlh));
2421 gst_buffer_unmap (sub, &map);
2424 _dmlh = (gst_riff_dmlh *) map.data;
2425 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
2426 gst_buffer_unmap (sub, &map);
2428 GST_INFO_OBJECT (avi, "dmlh tag found: totalframes: %u",
2431 avi->avih->tot_frames = dmlh.totalframes;
2436 GST_WARNING_OBJECT (avi,
2437 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
2438 GST_FOURCC_ARGS (tag));
2440 case GST_RIFF_TAG_JUNQ:
2441 case GST_RIFF_TAG_JUNK:
2443 /* skip and move to next chunk */
2445 gst_buffer_unref (sub);
2452 gst_buffer_unref (buf);
2457 gst_avi_demux_index_last (GstAviDemux * avi, GstAviStream * stream)
2459 return stream->idx_n;
2462 /* find a previous entry in the index with the given flags */
2464 gst_avi_demux_index_prev (GstAviDemux * avi, GstAviStream * stream,
2465 guint last, gboolean keyframe)
2467 GstAviIndexEntry *entry;
2470 for (i = last; i > 0; i--) {
2471 entry = &stream->index[i - 1];
2472 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2480 gst_avi_demux_index_next (GstAviDemux * avi, GstAviStream * stream,
2481 guint last, gboolean keyframe)
2483 GstAviIndexEntry *entry;
2486 for (i = last + 1; i < stream->idx_n; i++) {
2487 entry = &stream->index[i];
2488 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2492 return stream->idx_n - 1;
2496 gst_avi_demux_index_entry_search (GstAviIndexEntry * entry, guint64 * total)
2498 if (entry->total < *total)
2500 else if (entry->total > *total)
2506 * gst_avi_demux_index_for_time:
2508 * @stream: the stream
2509 * @time: a time position
2511 * Finds the index entry which time is less or equal than the requested time.
2512 * Try to avoid binary search when we can convert the time to an index
2513 * position directly (for example for video frames with a fixed duration).
2515 * Returns: the found position in the index.
2518 gst_avi_demux_index_for_time (GstAviDemux * avi,
2519 GstAviStream * stream, guint64 time)
2524 GST_LOG_OBJECT (avi, "search time:%" GST_TIME_FORMAT, GST_TIME_ARGS (time));
2526 /* easy (and common) cases */
2527 if (time == 0 || stream->idx_n == 0)
2529 if (time >= stream->idx_duration)
2530 return stream->idx_n - 1;
2532 /* figure out where we need to go. For that we convert the time to an
2533 * index entry or we convert it to a total and then do a binary search. */
2534 if (stream->is_vbr) {
2535 /* VBR stream next timestamp */
2536 if (stream->strh->type == GST_RIFF_FCC_auds) {
2537 total = avi_stream_convert_time_to_frames_unchecked (stream, time);
2539 index = avi_stream_convert_time_to_frames_unchecked (stream, time);
2542 /* constant rate stream */
2543 total = avi_stream_convert_time_to_bytes_unchecked (stream, time);
2547 GstAviIndexEntry *entry;
2549 /* no index, find index with binary search on total */
2550 GST_LOG_OBJECT (avi, "binary search for entry with total %"
2551 G_GUINT64_FORMAT, total);
2553 entry = gst_util_array_binary_search (stream->index,
2554 stream->idx_n, sizeof (GstAviIndexEntry),
2555 (GCompareDataFunc) gst_avi_demux_index_entry_search,
2556 GST_SEARCH_MODE_BEFORE, &total, NULL);
2558 if (entry == NULL) {
2559 GST_LOG_OBJECT (avi, "not found, assume index 0");
2562 index = entry - stream->index;
2563 GST_LOG_OBJECT (avi, "found at %u", index);
2566 GST_LOG_OBJECT (avi, "converted time to index %u", index);
2572 static inline GstAviStream *
2573 gst_avi_demux_stream_for_id (GstAviDemux * avi, guint32 id)
2576 GstAviStream *stream;
2578 /* get the stream for this entry */
2579 stream_nr = CHUNKID_TO_STREAMNR (id);
2580 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2581 GST_WARNING_OBJECT (avi, "invalid stream nr %d", stream_nr);
2584 stream = &avi->stream[stream_nr];
2585 if (G_UNLIKELY (!stream->strh)) {
2586 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2593 * gst_avi_demux_parse_index:
2594 * @avi: calling element (used for debugging/errors).
2595 * @buf: buffer containing the full index.
2597 * Read index entries from the provided buffer.
2598 * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
2601 gst_avi_demux_parse_index (GstAviDemux * avi, GstBuffer * buf)
2605 gst_riff_index_entry *index;
2607 GstAviStream *stream;
2608 GstAviIndexEntry entry;
2614 gst_buffer_map (buf, &map, GST_MAP_READ);
2616 stamp = gst_util_get_timestamp ();
2618 /* see how many items in the index */
2619 num = map.size / sizeof (gst_riff_index_entry);
2623 GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2625 index = (gst_riff_index_entry *) map.data;
2627 /* figure out if the index is 0 based or relative to the MOVI start */
2628 entry.offset = GST_READ_UINT32_LE (&index[0].offset);
2629 if (entry.offset < avi->offset) {
2630 avi->index_offset = avi->offset + 8;
2631 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2633 avi->index_offset = 0;
2634 GST_DEBUG ("index is 0 based");
2637 for (i = 0, n = 0; i < num; i++) {
2638 id = GST_READ_UINT32_LE (&index[i].id);
2639 entry.offset = GST_READ_UINT32_LE (&index[i].offset);
2641 /* some sanity checks */
2642 if (G_UNLIKELY (id == GST_RIFF_rec || id == 0 ||
2643 (entry.offset == 0 && n > 0)))
2646 /* get the stream for this entry */
2647 stream = gst_avi_demux_stream_for_id (avi, id);
2648 if (G_UNLIKELY (!stream))
2651 /* handle offset and size */
2652 entry.offset += avi->index_offset + 8;
2653 entry.size = GST_READ_UINT32_LE (&index[i].size);
2656 if (stream->strh->type == GST_RIFF_FCC_auds) {
2657 /* all audio frames are keyframes */
2658 ENTRY_SET_KEYFRAME (&entry);
2661 /* else read flags */
2662 flags = GST_READ_UINT32_LE (&index[i].flags);
2663 if (flags & GST_RIFF_IF_KEYFRAME) {
2664 ENTRY_SET_KEYFRAME (&entry);
2666 ENTRY_UNSET_KEYFRAME (&entry);
2671 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2676 gst_buffer_unmap (buf, &map);
2677 gst_buffer_unref (buf);
2679 /* get stream stats now */
2680 avi->have_index = gst_avi_demux_do_index_stats (avi);
2682 stamp = gst_util_get_timestamp () - stamp;
2683 GST_DEBUG_OBJECT (avi, "index parsing took %" GST_TIME_FORMAT,
2684 GST_TIME_ARGS (stamp));
2691 GST_DEBUG_OBJECT (avi, "empty index");
2692 gst_buffer_unmap (buf, &map);
2693 gst_buffer_unref (buf);
2698 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2699 ("Cannot allocate memory for %u*%u=%u bytes",
2700 (guint) sizeof (GstAviIndexEntry), num,
2701 (guint) sizeof (GstAviIndexEntry) * num));
2702 gst_buffer_unmap (buf, &map);
2703 gst_buffer_unref (buf);
2709 * gst_avi_demux_stream_index:
2710 * @avi: avi demuxer object.
2712 * Seeks to index and reads it.
2715 gst_avi_demux_stream_index (GstAviDemux * avi)
2718 guint64 offset = avi->offset;
2719 GstBuffer *buf = NULL;
2724 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2726 /* get chunk information */
2727 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2728 if (res != GST_FLOW_OK)
2731 gst_buffer_map (buf, &map, GST_MAP_READ);
2735 /* check tag first before blindy trying to read 'size' bytes */
2736 tag = GST_READ_UINT32_LE (map.data);
2737 size = GST_READ_UINT32_LE (map.data + 4);
2738 if (tag == GST_RIFF_TAG_LIST) {
2739 /* this is the movi tag */
2740 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2741 (8 + GST_ROUND_UP_2 (size)));
2742 offset += 8 + GST_ROUND_UP_2 (size);
2743 gst_buffer_unmap (buf, &map);
2744 gst_buffer_unref (buf);
2747 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2748 if (res != GST_FLOW_OK)
2751 gst_buffer_map (buf, &map, GST_MAP_READ);
2755 tag = GST_READ_UINT32_LE (map.data);
2756 size = GST_READ_UINT32_LE (map.data + 4);
2758 gst_buffer_unmap (buf, &map);
2759 gst_buffer_unref (buf);
2761 if (tag != GST_RIFF_TAG_idx1)
2766 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2768 /* read chunk, advance offset */
2769 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2770 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2773 GST_DEBUG ("will parse index chunk size %" G_GSIZE_FORMAT " for tag %"
2774 GST_FOURCC_FORMAT, gst_buffer_get_size (buf), GST_FOURCC_ARGS (tag));
2776 gst_avi_demux_parse_index (avi, buf);
2778 #ifndef GST_DISABLE_GST_DEBUG
2779 /* debug our indexes */
2782 GstAviStream *stream;
2784 for (i = 0; i < avi->num_streams; i++) {
2785 stream = &avi->stream[i];
2786 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2787 i, stream->idx_n, stream->total_bytes);
2796 GST_DEBUG_OBJECT (avi,
2797 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2802 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2803 gst_buffer_unmap (buf, &map);
2804 gst_buffer_unref (buf);
2809 GST_WARNING_OBJECT (avi,
2810 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2811 GST_FOURCC_ARGS (tag));
2816 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2822 * gst_avi_demux_stream_index_push:
2823 * @avi: avi demuxer object.
2828 gst_avi_demux_stream_index_push (GstAviDemux * avi)
2830 guint64 offset = avi->idx1_offset;
2835 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2837 /* get chunk information */
2838 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
2841 /* check tag first before blindly trying to read 'size' bytes */
2842 if (tag == GST_RIFF_TAG_LIST) {
2843 /* this is the movi tag */
2844 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2845 (8 + GST_ROUND_UP_2 (size)));
2846 avi->idx1_offset = offset + 8 + GST_ROUND_UP_2 (size);
2847 /* issue seek to allow chain function to handle it and return! */
2848 perform_seek_to_offset (avi, avi->idx1_offset);
2852 if (tag != GST_RIFF_TAG_idx1)
2855 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2857 /* flush chunk header */
2858 gst_adapter_flush (avi->adapter, 8);
2859 /* read chunk payload */
2860 buf = gst_adapter_take_buffer (avi->adapter, size);
2863 /* advance offset */
2864 offset += 8 + GST_ROUND_UP_2 (size);
2866 GST_DEBUG ("will parse index chunk size %" G_GSIZE_FORMAT " for tag %"
2867 GST_FOURCC_FORMAT, gst_buffer_get_size (buf), GST_FOURCC_ARGS (tag));
2869 avi->offset = avi->first_movi_offset;
2870 gst_avi_demux_parse_index (avi, buf);
2872 #ifndef GST_DISABLE_GST_DEBUG
2873 /* debug our indexes */
2876 GstAviStream *stream;
2878 for (i = 0; i < avi->num_streams; i++) {
2879 stream = &avi->stream[i];
2880 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2881 i, stream->idx_n, stream->total_bytes);
2890 GST_DEBUG_OBJECT (avi,
2891 "taking data from adapter failed: pos=%" G_GUINT64_FORMAT " size=%u",
2897 GST_WARNING_OBJECT (avi,
2898 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2899 GST_FOURCC_ARGS (tag));
2905 * gst_avi_demux_peek_tag:
2907 * Returns the tag and size of the next chunk
2909 static GstFlowReturn
2910 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2913 GstFlowReturn res = GST_FLOW_OK;
2914 GstBuffer *buf = NULL;
2917 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2918 if (res != GST_FLOW_OK)
2921 gst_buffer_map (buf, &map, GST_MAP_READ);
2925 *tag = GST_READ_UINT32_LE (map.data);
2926 *size = GST_READ_UINT32_LE (map.data + 4);
2928 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2929 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2930 *size, offset + 8, offset + 8 + (gint64) * size);
2933 gst_buffer_unmap (buf, &map);
2934 gst_buffer_unref (buf);
2941 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2946 GST_DEBUG_OBJECT (avi, "got %" G_GSIZE_FORMAT " bytes which is <> 8 bytes",
2948 res = GST_FLOW_ERROR;
2954 * gst_avi_demux_next_data_buffer:
2956 * Returns the offset and size of the next buffer
2957 * Position is the position of the buffer (after tag and size)
2959 static GstFlowReturn
2960 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2961 guint32 * tag, guint * size)
2963 guint64 off = *offset;
2968 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2969 if (res != GST_FLOW_OK)
2971 if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
2972 off += 8 + 4; /* skip tag + size + subtag */
2984 * gst_avi_demux_stream_scan:
2985 * @avi: calling element (used for debugging/errors).
2987 * Scan the file for all chunks to "create" a new index.
2991 gst_avi_demux_stream_scan (GstAviDemux * avi)
2994 GstAviStream *stream;
3002 * - implement non-seekable source support.
3004 GST_DEBUG_OBJECT (avi, "Creating index");
3006 /* get the size of the file */
3007 if (!gst_pad_peer_query_duration (avi->sinkpad, GST_FORMAT_BYTES, &tmplength))
3011 /* guess the total amount of entries we expect */
3015 GstAviIndexEntry entry;
3018 /* start reading data buffers to find the id and offset */
3019 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
3020 if (G_UNLIKELY (res != GST_FLOW_OK))
3024 stream = gst_avi_demux_stream_for_id (avi, tag);
3025 if (G_UNLIKELY (!stream))
3028 /* we can't figure out the keyframes, assume they all are */
3029 entry.flags = GST_AVI_KEYFRAME;
3033 /* and add to the index of this stream */
3034 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
3038 /* update position */
3039 pos += GST_ROUND_UP_2 (size);
3040 if (G_UNLIKELY (pos > length)) {
3041 GST_WARNING_OBJECT (avi,
3042 "Stopping index lookup since we are further than EOF");
3048 avi->have_index = gst_avi_demux_do_index_stats (avi);
3055 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
3056 ("Cannot allocate memory for %u*%u=%u bytes",
3057 (guint) sizeof (GstAviIndexEntry), num,
3058 (guint) sizeof (GstAviIndexEntry) * num));
3064 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
3068 GstAviStream *stream;
3070 total = GST_CLOCK_TIME_NONE;
3072 /* all streams start at a timestamp 0 */
3073 for (i = 0; i < avi->num_streams; i++) {
3074 GstClockTime duration, hduration;
3075 gst_riff_strh *strh;
3077 stream = &avi->stream[i];
3078 if (G_UNLIKELY (!stream || !stream->idx_n || !(strh = stream->strh)))
3081 /* get header duration for the stream */
3082 hduration = stream->hdr_duration;
3083 /* index duration calculated during parsing */
3084 duration = stream->idx_duration;
3086 /* now pick a good duration */
3087 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3088 /* index gave valid duration, use that */
3089 GST_INFO ("Stream %p duration according to index: %" GST_TIME_FORMAT,
3090 stream, GST_TIME_ARGS (duration));
3092 /* fall back to header info to calculate a duration */
3093 duration = hduration;
3095 GST_INFO ("Setting duration of stream #%d to %" GST_TIME_FORMAT,
3096 i, GST_TIME_ARGS (duration));
3097 /* set duration for the stream */
3098 stream->duration = duration;
3100 /* find total duration */
3101 if (total == GST_CLOCK_TIME_NONE ||
3102 (GST_CLOCK_TIME_IS_VALID (duration) && duration > total))
3106 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
3107 /* now update the duration for those streams where we had none */
3108 for (i = 0; i < avi->num_streams; i++) {
3109 stream = &avi->stream[i];
3111 if (!GST_CLOCK_TIME_IS_VALID (stream->duration)
3112 || stream->duration == 0) {
3113 stream->duration = total;
3115 GST_INFO ("Stream %p duration according to total: %" GST_TIME_FORMAT,
3116 stream, GST_TIME_ARGS (total));
3121 /* and set the total duration in the segment. */
3122 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
3123 GST_TIME_ARGS (total));
3125 avi->segment.duration = total;
3128 /* returns FALSE if there are no pads to deliver event to,
3129 * otherwise TRUE (whatever the outcome of event sending),
3130 * takes ownership of the event. */
3132 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
3134 gboolean result = FALSE;
3137 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
3138 GST_EVENT_TYPE_NAME (event), avi->num_streams);
3140 for (i = 0; i < avi->num_streams; i++) {
3141 GstAviStream *stream = &avi->stream[i];
3145 gst_pad_push_event (stream->pad, gst_event_ref (event));
3148 gst_event_unref (event);
3153 gst_avi_demux_check_seekability (GstAviDemux * avi)
3156 gboolean seekable = FALSE;
3157 gint64 start = -1, stop = -1;
3159 query = gst_query_new_seeking (GST_FORMAT_BYTES);
3160 if (!gst_pad_peer_query (avi->sinkpad, query)) {
3161 GST_DEBUG_OBJECT (avi, "seeking query failed");
3165 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
3167 /* try harder to query upstream size if we didn't get it the first time */
3168 if (seekable && stop == -1) {
3169 GST_DEBUG_OBJECT (avi, "doing duration query to fix up unset stop");
3170 gst_pad_peer_query_duration (avi->sinkpad, GST_FORMAT_BYTES, &stop);
3173 /* if upstream doesn't know the size, it's likely that it's not seekable in
3174 * practice even if it technically may be seekable */
3175 if (seekable && (start != 0 || stop <= start)) {
3176 GST_DEBUG_OBJECT (avi, "seekable but unknown start/stop -> disable");
3181 GST_INFO_OBJECT (avi, "seekable: %d (%" G_GUINT64_FORMAT " - %"
3182 G_GUINT64_FORMAT ")", seekable, start, stop);
3183 avi->seekable = seekable;
3185 gst_query_unref (query);
3189 * Read AVI headers when streaming
3191 static GstFlowReturn
3192 gst_avi_demux_stream_header_push (GstAviDemux * avi)
3194 GstFlowReturn ret = GST_FLOW_OK;
3199 GstBuffer *buf = NULL, *sub = NULL;
3202 GstTagList *tags = NULL;
3205 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
3207 switch (avi->header_state) {
3208 case GST_AVI_DEMUX_HEADER_TAG_LIST:
3209 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3210 avi->offset += 8 + GST_ROUND_UP_2 (size);
3211 if (tag != GST_RIFF_TAG_LIST)
3212 goto header_no_list;
3214 gst_adapter_flush (avi->adapter, 8);
3215 /* Find the 'hdrl' LIST tag */
3216 GST_DEBUG ("Reading %d bytes", size);
3217 buf = gst_adapter_take_buffer (avi->adapter, size);
3219 gst_buffer_extract (buf, 0, fourcc, 4);
3221 if (GST_READ_UINT32_LE (fourcc) != GST_RIFF_LIST_hdrl)
3222 goto header_no_hdrl;
3226 gst_adapter_flush (avi->adapter, 1);
3228 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
3230 gst_avi_demux_roundup_list (avi, &buf);
3232 /* the hdrl starts with a 'avih' header */
3233 if (!gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3235 goto header_no_avih;
3237 if (tag != GST_RIFF_TAG_avih)
3238 goto header_no_avih;
3240 if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3241 goto header_wrong_avih;
3243 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
3245 /* now, read the elements from the header until the end */
3246 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3248 /* sub can be NULL on empty tags */
3253 case GST_RIFF_TAG_LIST:
3254 if (gst_buffer_get_size (sub) < 4)
3257 gst_buffer_extract (sub, 0, fourcc, 4);
3259 switch (GST_READ_UINT32_LE (fourcc)) {
3260 case GST_RIFF_LIST_strl:
3261 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3263 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3264 ("failed to parse stream, ignoring"));
3269 case GST_RIFF_LIST_odml:
3270 gst_avi_demux_parse_odml (avi, sub);
3274 GST_WARNING_OBJECT (avi,
3275 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3276 GST_FOURCC_ARGS (GST_READ_UINT32_LE (fourcc)));
3278 case GST_RIFF_TAG_JUNQ:
3279 case GST_RIFF_TAG_JUNK:
3284 gst_avi_demux_parse_idit (avi, sub);
3287 GST_WARNING_OBJECT (avi,
3288 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
3289 offset, GST_FOURCC_ARGS (tag));
3291 case GST_RIFF_TAG_JUNQ:
3292 case GST_RIFF_TAG_JUNK:
3294 /* move to next chunk */
3296 gst_buffer_unref (sub);
3301 gst_buffer_unref (buf);
3302 GST_DEBUG ("elements parsed");
3304 /* check parsed streams */
3305 if (avi->num_streams == 0) {
3307 } else if (avi->num_streams != avi->avih->streams) {
3308 GST_WARNING_OBJECT (avi,
3309 "Stream header mentioned %d streams, but %d available",
3310 avi->avih->streams, avi->num_streams);
3312 GST_DEBUG ("Get junk and info next");
3313 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
3315 /* Need more data */
3319 case GST_AVI_DEMUX_HEADER_INFO:
3320 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
3322 if (gst_adapter_available (avi->adapter) < 12)
3325 data = gst_adapter_map (avi->adapter, 12);
3326 tag = GST_READ_UINT32_LE (data);
3327 size = GST_READ_UINT32_LE (data + 4);
3328 ltag = GST_READ_UINT32_LE (data + 8);
3329 gst_adapter_unmap (avi->adapter);
3331 if (tag == GST_RIFF_TAG_LIST) {
3333 case GST_RIFF_LIST_movi:
3334 gst_adapter_flush (avi->adapter, 12);
3335 if (!avi->first_movi_offset)
3336 avi->first_movi_offset = avi->offset;
3338 avi->idx1_offset = avi->offset + size - 4;
3340 case GST_RIFF_LIST_INFO:
3341 GST_DEBUG ("Found INFO chunk");
3342 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3343 GST_DEBUG ("got size %d", size);
3345 gst_adapter_flush (avi->adapter, 12);
3347 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
3350 gst_adapter_flush (avi->adapter, 1);
3351 gst_riff_parse_info (GST_ELEMENT_CAST (avi), buf, &tags);
3353 if (avi->globaltags) {
3354 gst_tag_list_insert (avi->globaltags, tags,
3355 GST_TAG_MERGE_REPLACE);
3357 avi->globaltags = tags;
3361 gst_buffer_unref (buf);
3363 avi->offset += GST_ROUND_UP_2 (size) - 4;
3365 GST_DEBUG ("skipping INFO LIST prefix");
3368 /* Need more data */
3373 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3374 avi->offset += 8 + GST_ROUND_UP_2 (size);
3375 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3377 /* Need more data */
3383 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3384 avi->offset += 8 + GST_ROUND_UP_2 (size);
3385 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3387 /* Need more data */
3394 GST_WARNING ("unhandled header state: %d", avi->header_state);
3399 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3400 avi->num_streams, avi->stream[0].indexes);
3402 GST_DEBUG ("Found movi chunk. Starting to stream data");
3403 avi->state = GST_AVI_DEMUX_MOVI;
3405 /* no indexes in push mode, but it still sets some variables */
3406 gst_avi_demux_calculate_durations_from_index (avi);
3408 gst_avi_demux_expose_streams (avi, TRUE);
3410 /* prepare all streams for index 0 */
3411 for (i = 0; i < avi->num_streams; i++)
3412 avi->stream[i].current_entry = 0;
3414 /* create initial NEWSEGMENT event */
3416 gst_event_unref (avi->seg_event);
3417 avi->seg_event = gst_event_new_segment (&avi->segment);
3419 gst_avi_demux_check_seekability (avi);
3421 /* at this point we know all the streams and we can signal the no more
3423 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3424 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3431 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3432 return GST_FLOW_ERROR;
3436 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3437 ("Invalid AVI header (no LIST at start): %"
3438 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3439 return GST_FLOW_ERROR;
3443 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3444 ("Invalid AVI header (no hdrl at start): %"
3445 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3446 gst_buffer_unref (buf);
3447 return GST_FLOW_ERROR;
3451 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3452 ("Invalid AVI header (no avih at start): %"
3453 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3455 gst_buffer_unref (sub);
3457 gst_buffer_unref (buf);
3458 return GST_FLOW_ERROR;
3462 gst_buffer_unref (buf);
3463 return GST_FLOW_ERROR;
3468 gst_avi_demux_add_date_tag (GstAviDemux * avi, gint y, gint m, gint d,
3469 gint h, gint min, gint s)
3474 date = g_date_new_dmy (d, m, y);
3475 if (!g_date_valid (date)) {
3477 GST_WARNING_OBJECT (avi, "Refusing to add invalid date %d-%d-%d", y, m, d);
3482 dt = gst_date_time_new_local_time (y, m, d, h, min, s);
3484 if (avi->globaltags == NULL)
3485 avi->globaltags = gst_tag_list_new_empty ();
3487 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE, date,
3491 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE_TIME,
3493 gst_date_time_unref (dt);
3498 gst_avi_demux_parse_idit_nums_only (GstAviDemux * avi, gchar * data)
3501 gint hr = 0, min = 0, sec = 0;
3504 GST_DEBUG ("data : '%s'", data);
3506 ret = sscanf (data, "%d:%d:%d %d:%d:%d", &y, &m, &d, &hr, &min, &sec);
3508 /* Attempt YYYY/MM/DD/ HH:MM variant (found in CASIO cameras) */
3509 ret = sscanf (data, "%04d/%02d/%02d/ %d:%d", &y, &m, &d, &hr, &min);
3511 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3515 gst_avi_demux_add_date_tag (avi, y, m, d, hr, min, sec);
3519 get_month_num (gchar * data, guint size)
3521 if (g_ascii_strncasecmp (data, "jan", 3) == 0) {
3523 } else if (g_ascii_strncasecmp (data, "feb", 3) == 0) {
3525 } else if (g_ascii_strncasecmp (data, "mar", 3) == 0) {
3527 } else if (g_ascii_strncasecmp (data, "apr", 3) == 0) {
3529 } else if (g_ascii_strncasecmp (data, "may", 3) == 0) {
3531 } else if (g_ascii_strncasecmp (data, "jun", 3) == 0) {
3533 } else if (g_ascii_strncasecmp (data, "jul", 3) == 0) {
3535 } else if (g_ascii_strncasecmp (data, "aug", 3) == 0) {
3537 } else if (g_ascii_strncasecmp (data, "sep", 3) == 0) {
3539 } else if (g_ascii_strncasecmp (data, "oct", 3) == 0) {
3541 } else if (g_ascii_strncasecmp (data, "nov", 3) == 0) {
3543 } else if (g_ascii_strncasecmp (data, "dec", 3) == 0) {
3551 gst_avi_demux_parse_idit_text (GstAviDemux * avi, gchar * data)
3553 gint year, month, day;
3554 gint hour, min, sec;
3559 ret = sscanf (data, "%3s %3s %d %d:%d:%d %d", weekday, monthstr, &day, &hour,
3562 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3565 month = get_month_num (monthstr, strlen (monthstr));
3566 gst_avi_demux_add_date_tag (avi, year, month, day, hour, min, sec);
3570 gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf)
3575 gchar *safedata = NULL;
3577 gst_buffer_map (buf, &map, GST_MAP_READ);
3580 * http://www.eden-foundation.org/products/code/film_date_stamp/index.html
3582 * This tag could be in one of the below formats
3583 * 2005:08:17 11:42:43
3584 * THU OCT 26 16:46:04 2006
3585 * Mon Mar 3 09:44:56 2008
3587 * FIXME: Our date tag doesn't include hours
3590 /* skip eventual initial whitespace */
3591 ptr = (gchar *) map.data;
3594 while (left > 0 && g_ascii_isspace (ptr[0])) {
3603 /* make a safe copy to add a \0 to the end of the string */
3604 safedata = g_strndup (ptr, left);
3606 /* test if the first char is a alpha or a number */
3607 if (g_ascii_isdigit (ptr[0])) {
3608 gst_avi_demux_parse_idit_nums_only (avi, safedata);
3611 } else if (g_ascii_isalpha (ptr[0])) {
3612 gst_avi_demux_parse_idit_text (avi, safedata);
3620 GST_WARNING_OBJECT (avi, "IDIT tag has no parsable info");
3621 gst_buffer_unmap (buf, &map);
3625 * Read full AVI headers.
3627 static GstFlowReturn
3628 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
3631 GstBuffer *buf, *sub = NULL;
3634 GstElement *element = GST_ELEMENT_CAST (avi);
3636 GstTagList *tags = NULL;
3639 stamp = gst_util_get_timestamp ();
3641 /* the header consists of a 'hdrl' LIST tag */
3642 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3643 if (res != GST_FLOW_OK)
3644 goto pull_range_failed;
3645 else if (tag != GST_RIFF_TAG_LIST)
3647 else if (gst_buffer_get_size (buf) < 4)
3650 GST_DEBUG_OBJECT (avi, "parsing headers");
3652 /* Find the 'hdrl' LIST tag */
3653 gst_buffer_extract (buf, 0, fourcc, 4);
3654 while (GST_READ_UINT32_LE (fourcc) != GST_RIFF_LIST_hdrl) {
3655 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
3656 GST_FOURCC_ARGS (GST_READ_UINT32_LE (fourcc)));
3659 gst_buffer_unref (buf);
3661 /* read new chunk */
3662 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3663 if (res != GST_FLOW_OK)
3664 goto pull_range_failed;
3665 else if (tag != GST_RIFF_TAG_LIST)
3667 else if (gst_buffer_get_size (buf) < 4)
3669 gst_buffer_extract (buf, 0, fourcc, 4);
3672 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
3674 gst_avi_demux_roundup_list (avi, &buf);
3676 /* the hdrl starts with a 'avih' header */
3677 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
3679 else if (tag != GST_RIFF_TAG_avih)
3681 else if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3684 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3686 /* now, read the elements from the header until the end */
3687 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
3690 /* sub can be NULL on empty tags */
3694 gst_buffer_map (sub, &map, GST_MAP_READ);
3697 case GST_RIFF_TAG_LIST:
3701 switch (GST_READ_UINT32_LE (map.data)) {
3702 case GST_RIFF_LIST_strl:
3703 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3704 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3705 ("failed to parse stream, ignoring"));
3710 case GST_RIFF_LIST_odml:
3711 gst_avi_demux_parse_odml (avi, sub);
3714 case GST_RIFF_LIST_INFO:
3715 gst_buffer_resize (sub, 4, -1);
3716 gst_riff_parse_info (element, sub, &tags);
3718 if (avi->globaltags) {
3719 gst_tag_list_insert (avi->globaltags, tags,
3720 GST_TAG_MERGE_REPLACE);
3722 avi->globaltags = tags;
3728 GST_WARNING_OBJECT (avi,
3729 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3730 GST_FOURCC_ARGS (GST_READ_UINT32_LE (map.data)));
3731 GST_MEMDUMP_OBJECT (avi, "Unknown list", map.data, map.size);
3733 case GST_RIFF_TAG_JUNQ:
3734 case GST_RIFF_TAG_JUNK:
3739 gst_avi_demux_parse_idit (avi, sub);
3742 GST_WARNING_OBJECT (avi,
3743 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
3744 GST_FOURCC_ARGS (tag), offset);
3745 GST_MEMDUMP_OBJECT (avi, "Unknown tag", map.data, map.size);
3747 case GST_RIFF_TAG_JUNQ:
3748 case GST_RIFF_TAG_JUNK:
3751 gst_buffer_unmap (sub, &map);
3752 gst_buffer_unref (sub);
3758 gst_buffer_unref (buf);
3759 GST_DEBUG ("elements parsed");
3761 /* check parsed streams */
3762 if (avi->num_streams == 0)
3764 else if (avi->num_streams != avi->avih->streams) {
3765 GST_WARNING_OBJECT (avi,
3766 "Stream header mentioned %d streams, but %d available",
3767 avi->avih->streams, avi->num_streams);
3770 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
3771 G_GUINT64_FORMAT, avi->offset);
3773 /* Now, find the data (i.e. skip all junk between header and data) */
3780 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
3781 if (res != GST_FLOW_OK) {
3782 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
3783 goto pull_range_failed;
3784 } else if (gst_buffer_get_size (buf) < 12) {
3785 GST_DEBUG_OBJECT (avi,
3786 "got %" G_GSIZE_FORMAT " bytes which is less than 12 bytes",
3787 gst_buffer_get_size (buf));
3788 gst_buffer_unref (buf);
3789 return GST_FLOW_ERROR;
3792 gst_buffer_map (buf, &map, GST_MAP_READ);
3793 tag = GST_READ_UINT32_LE (map.data);
3794 size = GST_READ_UINT32_LE (map.data + 4);
3795 ltag = GST_READ_UINT32_LE (map.data + 8);
3797 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
3798 GST_FOURCC_ARGS (tag), size);
3799 GST_MEMDUMP ("Tag content", map.data, map.size);
3800 gst_buffer_unmap (buf, &map);
3801 gst_buffer_unref (buf);
3804 case GST_RIFF_TAG_LIST:{
3806 case GST_RIFF_LIST_movi:
3807 GST_DEBUG_OBJECT (avi,
3808 "Reached the 'movi' tag, we're done with skipping");
3810 case GST_RIFF_LIST_INFO:
3812 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3814 if (res != GST_FLOW_OK) {
3815 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3816 goto pull_range_failed;
3818 GST_DEBUG ("got size %" G_GSIZE_FORMAT, gst_buffer_get_size (buf));
3820 GST_DEBUG ("skipping INFO LIST prefix");
3821 avi->offset += (4 - GST_ROUND_UP_2 (size));
3822 gst_buffer_unref (buf);
3826 sub = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, 4, -1);
3827 gst_riff_parse_info (element, sub, &tags);
3829 if (avi->globaltags) {
3830 gst_tag_list_insert (avi->globaltags, tags,
3831 GST_TAG_MERGE_REPLACE);
3833 avi->globaltags = tags;
3838 gst_buffer_unref (sub);
3841 gst_buffer_unref (buf);
3842 /* gst_riff_read_chunk() has already advanced avi->offset */
3845 GST_WARNING_OBJECT (avi,
3846 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3847 GST_FOURCC_ARGS (ltag));
3848 avi->offset += 8 + GST_ROUND_UP_2 (size);
3854 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3855 GST_FOURCC_ARGS (tag));
3857 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3858 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3859 /* Only get buffer for debugging if the memdump is needed */
3860 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= 9) {
3862 res = gst_pad_pull_range (avi->sinkpad, avi->offset, size, &buf);
3863 if (res != GST_FLOW_OK) {
3864 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3865 goto pull_range_failed;
3867 gst_buffer_map (buf, &map, GST_MAP_READ);
3868 GST_MEMDUMP ("Junk", map.data, map.size);
3869 gst_buffer_unmap (buf, &map);
3870 gst_buffer_unref (buf);
3872 avi->offset += 8 + GST_ROUND_UP_2 (size);
3878 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3879 avi->num_streams, avi->stream[0].indexes);
3881 /* create or read stream index (for seeking) */
3882 if (avi->stream[0].indexes != NULL) {
3883 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3884 gst_avi_demux_read_subindexes_pull (avi);
3886 if (!avi->have_index) {
3887 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
3888 gst_avi_demux_stream_index (avi);
3890 /* still no index, scan */
3891 if (!avi->have_index) {
3892 gst_avi_demux_stream_scan (avi);
3894 /* still no index.. this is a fatal error for now.
3895 * FIXME, we should switch to plain push mode without seeking
3896 * instead of failing. */
3897 if (!avi->have_index)
3901 /* use the indexes now to construct nice durations */
3902 gst_avi_demux_calculate_durations_from_index (avi);
3904 gst_avi_demux_expose_streams (avi, FALSE);
3906 /* do initial seek to the default segment values */
3907 gst_avi_demux_do_seek (avi, &avi->segment);
3909 /* create initial NEWSEGMENT event */
3911 gst_event_unref (avi->seg_event);
3912 avi->seg_event = gst_event_new_segment (&avi->segment);
3914 stamp = gst_util_get_timestamp () - stamp;
3915 GST_DEBUG_OBJECT (avi, "pulling header took %" GST_TIME_FORMAT,
3916 GST_TIME_ARGS (stamp));
3918 /* at this point we know all the streams and we can signal the no more
3920 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3921 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3928 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3929 ("Invalid AVI header (no LIST at start): %"
3930 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3931 gst_buffer_unref (buf);
3932 return GST_FLOW_ERROR;
3936 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3937 ("Invalid AVI header (no hdrl at start): %"
3938 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3939 gst_buffer_unref (buf);
3940 return GST_FLOW_ERROR;
3944 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3945 ("Invalid AVI header (no avih at start): %"
3946 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3948 gst_buffer_unref (sub);
3949 gst_buffer_unref (buf);
3950 return GST_FLOW_ERROR;
3954 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3955 ("Invalid AVI header (cannot parse avih at start)"));
3956 gst_buffer_unref (buf);
3957 return GST_FLOW_ERROR;
3961 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3962 return GST_FLOW_ERROR;
3966 GST_WARNING ("file without or too big index");
3967 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3968 ("Could not get/create index"));
3969 return GST_FLOW_ERROR;
3973 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3974 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3975 return GST_FLOW_ERROR;
3979 /* move a stream to @index */
3981 gst_avi_demux_move_stream (GstAviDemux * avi, GstAviStream * stream,
3982 GstSegment * segment, guint index)
3984 GST_DEBUG_OBJECT (avi, "Move stream %d to %u", stream->num, index);
3986 if (segment->rate < 0.0) {
3988 /* Because we don't know the frame order we need to push from the prev keyframe
3989 * to the next keyframe. If there is a smart decoder downstream he will notice
3990 * that there are too many encoded frames send and return EOS when there
3991 * are enough decoded frames to fill the segment. */
3992 next_key = gst_avi_demux_index_next (avi, stream, index, TRUE);
3994 /* FIXME, we go back to 0, we should look at segment.start. We will however
3995 * stop earlier when the see the timestamp < segment.start */
3996 stream->start_entry = 0;
3997 stream->step_entry = index;
3998 stream->current_entry = index;
3999 stream->stop_entry = next_key;
4001 GST_DEBUG_OBJECT (avi, "reverse seek: start %u, step %u, stop %u",
4002 stream->start_entry, stream->step_entry, stream->stop_entry);
4004 stream->start_entry = index;
4005 stream->step_entry = index;
4006 stream->stop_entry = gst_avi_demux_index_last (avi, stream);
4008 if (stream->current_entry != index) {
4009 GST_DEBUG_OBJECT (avi, "Move DISCONT from %u to %u",
4010 stream->current_entry, index);
4011 stream->current_entry = index;
4012 stream->discont = TRUE;
4015 /* update the buffer info */
4016 gst_avi_demux_get_buffer_info (avi, stream, index,
4017 &stream->current_timestamp, &stream->current_ts_end,
4018 &stream->current_offset, &stream->current_offset_end);
4020 GST_DEBUG_OBJECT (avi, "Moved to %u, ts %" GST_TIME_FORMAT
4021 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4022 ", off_end %" G_GUINT64_FORMAT, index,
4023 GST_TIME_ARGS (stream->current_timestamp),
4024 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4025 stream->current_offset_end);
4027 GST_DEBUG_OBJECT (avi, "Seeking to offset %" G_GUINT64_FORMAT,
4028 stream->index[index].offset);
4032 * Do the actual seeking.
4035 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
4037 GstClockTime seek_time;
4038 gboolean keyframe, before, after;
4040 GstAviStream *stream;
4042 seek_time = segment->position;
4043 keyframe = ! !(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
4044 before = ! !(segment->flags & GST_SEEK_FLAG_SNAP_BEFORE);
4045 after = ! !(segment->flags & GST_SEEK_FLAG_SNAP_AFTER);
4047 GST_DEBUG_OBJECT (avi, "seek to: %" GST_TIME_FORMAT
4048 " keyframe seeking:%d, %s", GST_TIME_ARGS (seek_time), keyframe,
4049 snap_types[before ? 1 : 0][after ? 1 : 0]);
4051 /* FIXME, this code assumes the main stream with keyframes is stream 0,
4052 * which is mostly correct... */
4053 stream = &avi->stream[avi->main_stream];
4055 /* get the entry index for the requested position */
4056 index = gst_avi_demux_index_for_time (avi, stream, seek_time);
4057 GST_DEBUG_OBJECT (avi, "Got entry %u", index);
4059 /* check if we are already on a keyframe */
4060 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
4063 next = after && !before;
4064 if (segment->rate < 0)
4068 GST_DEBUG_OBJECT (avi, "not keyframe, searching forward");
4069 /* now go to the next keyframe, this is where we should start
4071 index = gst_avi_demux_index_next (avi, stream, index, TRUE);
4072 GST_DEBUG_OBJECT (avi, "next keyframe at %u", index);
4074 GST_DEBUG_OBJECT (avi, "not keyframe, searching back");
4075 /* now go to the previous keyframe, this is where we should start
4077 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4078 GST_DEBUG_OBJECT (avi, "previous keyframe at %u", index);
4082 /* move the main stream to this position */
4083 gst_avi_demux_move_stream (avi, stream, segment, index);
4086 /* when seeking to a keyframe, we update the result seek time
4087 * to the time of the keyframe. */
4088 seek_time = stream->current_timestamp;
4089 GST_DEBUG_OBJECT (avi, "keyframe adjusted to %" GST_TIME_FORMAT,
4090 GST_TIME_ARGS (seek_time));
4093 /* the seek time is also the position and stream time when going
4095 segment->position = seek_time;
4096 if (segment->rate > 0.0)
4097 segment->time = seek_time;
4099 /* now set DISCONT and align the other streams */
4100 for (i = 0; i < avi->num_streams; i++) {
4101 GstAviStream *ostream;
4103 ostream = &avi->stream[i];
4104 if ((ostream == stream) || (ostream->index == NULL))
4107 /* get the entry index for the requested position */
4108 index = gst_avi_demux_index_for_time (avi, ostream, seek_time);
4110 /* move to previous keyframe */
4111 if (!ENTRY_IS_KEYFRAME (&ostream->index[index]))
4112 index = gst_avi_demux_index_prev (avi, ostream, index, TRUE);
4114 gst_avi_demux_move_stream (avi, ostream, segment, index);
4116 GST_DEBUG_OBJECT (avi, "done seek to: %" GST_TIME_FORMAT,
4117 GST_TIME_ARGS (seek_time));
4123 * Handle seek event in pull mode.
4126 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4131 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4135 GstSegment seeksegment = { 0, };
4139 GST_DEBUG_OBJECT (avi, "doing seek with event");
4141 gst_event_parse_seek (event, &rate, &format, &flags,
4142 &cur_type, &cur, &stop_type, &stop);
4144 /* we have to have a format as the segment format. Try to convert
4146 if (format != GST_FORMAT_TIME) {
4147 gboolean res = TRUE;
4149 if (cur_type != GST_SEEK_TYPE_NONE)
4150 res = gst_pad_query_convert (pad, format, cur, GST_FORMAT_TIME, &cur);
4151 if (res && stop_type != GST_SEEK_TYPE_NONE)
4152 res = gst_pad_query_convert (pad, format, stop, GST_FORMAT_TIME, &stop);
4156 format = GST_FORMAT_TIME;
4158 GST_DEBUG_OBJECT (avi,
4159 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
4160 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
4161 /* FIXME: can we do anything with rate!=1.0 */
4163 GST_DEBUG_OBJECT (avi, "doing seek without event");
4168 /* save flush flag */
4169 flush = flags & GST_SEEK_FLAG_FLUSH;
4172 GstEvent *fevent = gst_event_new_flush_start ();
4174 /* for a flushing seek, we send a flush_start on all pads. This will
4175 * eventually stop streaming with a WRONG_STATE. We can thus eventually
4176 * take the STREAM_LOCK. */
4177 GST_DEBUG_OBJECT (avi, "sending flush start");
4178 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4179 gst_pad_push_event (avi->sinkpad, fevent);
4181 /* a non-flushing seek, we PAUSE the task so that we can take the
4183 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
4184 gst_pad_pause_task (avi->sinkpad);
4187 /* wait for streaming to stop */
4188 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
4189 GST_PAD_STREAM_LOCK (avi->sinkpad);
4191 /* copy segment, we need this because we still need the old
4192 * segment when we close the current segment. */
4193 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4196 GST_DEBUG_OBJECT (avi, "configuring seek");
4197 gst_segment_do_seek (&seeksegment, rate, format, flags,
4198 cur_type, cur, stop_type, stop, &update);
4200 /* do the seek, seeksegment.position contains the new position, this
4201 * actually never fails. */
4202 gst_avi_demux_do_seek (avi, &seeksegment);
4205 GstEvent *fevent = gst_event_new_flush_stop (TRUE);
4207 GST_DEBUG_OBJECT (avi, "sending flush stop");
4208 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4209 gst_pad_push_event (avi->sinkpad, fevent);
4212 /* now update the real segment info */
4213 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
4215 /* post the SEGMENT_START message when we do segmented playback */
4216 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4217 gst_element_post_message (GST_ELEMENT_CAST (avi),
4218 gst_message_new_segment_start (GST_OBJECT_CAST (avi),
4219 avi->segment.format, avi->segment.position));
4222 /* queue the segment event for the streaming thread. */
4224 gst_event_unref (avi->seg_event);
4225 avi->seg_event = gst_event_new_segment (&avi->segment);
4227 if (!avi->streaming) {
4228 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4229 avi->sinkpad, NULL);
4231 /* reset the last flow and mark discont, seek is always DISCONT */
4232 for (i = 0; i < avi->num_streams; i++) {
4233 GST_DEBUG_OBJECT (avi, "marking DISCONT");
4234 avi->stream[i].last_flow = GST_FLOW_OK;
4235 avi->stream[i].discont = TRUE;
4237 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
4244 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4250 * Handle seek event in push mode.
4253 avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4258 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4260 gboolean keyframe, before, after;
4261 GstAviStream *stream;
4265 GstSegment seeksegment;
4268 /* check we have the index */
4269 if (!avi->have_index) {
4270 GST_DEBUG_OBJECT (avi, "no seek index built, seek aborted.");
4273 GST_DEBUG_OBJECT (avi, "doing push-based seek with event");
4276 gst_event_parse_seek (event, &rate, &format, &flags,
4277 &cur_type, &cur, &stop_type, &stop);
4279 if (format != GST_FORMAT_TIME) {
4280 gboolean res = TRUE;
4282 if (cur_type != GST_SEEK_TYPE_NONE)
4283 res = gst_pad_query_convert (pad, format, cur, GST_FORMAT_TIME, &cur);
4284 if (res && stop_type != GST_SEEK_TYPE_NONE)
4285 res = gst_pad_query_convert (pad, format, stop, GST_FORMAT_TIME, &stop);
4287 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4291 format = GST_FORMAT_TIME;
4294 /* let gst_segment handle any tricky stuff */
4295 GST_DEBUG_OBJECT (avi, "configuring seek");
4296 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4297 gst_segment_do_seek (&seeksegment, rate, format, flags,
4298 cur_type, cur, stop_type, stop, &update);
4300 keyframe = ! !(flags & GST_SEEK_FLAG_KEY_UNIT);
4301 cur = seeksegment.position;
4302 before = ! !(flags & GST_SEEK_FLAG_SNAP_BEFORE);
4303 after = ! !(flags & GST_SEEK_FLAG_SNAP_AFTER);
4305 GST_DEBUG_OBJECT (avi,
4306 "Seek requested: ts %" GST_TIME_FORMAT " stop %" GST_TIME_FORMAT
4307 ", kf %u, %s, rate %lf", GST_TIME_ARGS (cur), GST_TIME_ARGS (stop),
4308 keyframe, snap_types[before ? 1 : 0][after ? 1 : 0], rate);
4311 GST_DEBUG_OBJECT (avi, "negative rate seek not supported in push mode");
4315 /* FIXME, this code assumes the main stream with keyframes is stream 0,
4316 * which is mostly correct... */
4317 str_num = avi->main_stream;
4318 stream = &avi->stream[str_num];
4320 /* get the entry index for the requested position */
4321 index = gst_avi_demux_index_for_time (avi, stream, cur);
4322 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT,
4323 str_num, index, GST_TIME_ARGS (cur));
4325 /* check if we are already on a keyframe */
4326 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
4329 next = after && !before;
4330 if (seeksegment.rate < 0)
4334 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching forward");
4335 /* now go to the next keyframe, this is where we should start
4337 index = gst_avi_demux_index_next (avi, stream, index, TRUE);
4338 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", index);
4340 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4341 /* now go to the previous keyframe, this is where we should start
4343 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4344 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", index);
4348 gst_avi_demux_get_buffer_info (avi, stream, index,
4349 &stream->current_timestamp, &stream->current_ts_end,
4350 &stream->current_offset, &stream->current_offset_end);
4352 /* re-use cur to be the timestamp of the seek as it _will_ be */
4353 cur = stream->current_timestamp;
4355 min_offset = stream->index[index].offset;
4356 avi->seek_kf_offset = min_offset - 8;
4358 GST_DEBUG_OBJECT (avi,
4359 "Seek to: ts %" GST_TIME_FORMAT " (on str %u, idx %u, offset %"
4360 G_GUINT64_FORMAT ")", GST_TIME_ARGS (stream->current_timestamp), str_num,
4363 for (n = 0; n < avi->num_streams; n++) {
4364 GstAviStream *str = &avi->stream[n];
4367 if (n == avi->main_stream)
4370 /* get the entry index for the requested position */
4371 idx = gst_avi_demux_index_for_time (avi, str, cur);
4372 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT, n,
4373 idx, GST_TIME_ARGS (cur));
4375 /* check if we are already on a keyframe */
4376 if (!ENTRY_IS_KEYFRAME (&str->index[idx])) {
4377 if (after && !before) {
4378 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching forward");
4379 /* now go to the next keyframe, this is where we should start
4381 idx = gst_avi_demux_index_next (avi, str, idx, TRUE);
4382 GST_DEBUG_OBJECT (avi, "Found next keyframe at %u", idx);
4384 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4385 /* now go to the previous keyframe, this is where we should start
4387 idx = gst_avi_demux_index_prev (avi, str, idx, TRUE);
4388 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", idx);
4392 gst_avi_demux_get_buffer_info (avi, str, idx,
4393 &str->current_timestamp, &str->current_ts_end,
4394 &str->current_offset, &str->current_offset_end);
4396 if (str->index[idx].offset < min_offset) {
4397 min_offset = str->index[idx].offset;
4398 GST_DEBUG_OBJECT (avi,
4399 "Found an earlier offset at %" G_GUINT64_FORMAT ", str %u",
4407 GST_DEBUG_OBJECT (avi,
4408 "Seek performed: str %u, offset %" G_GUINT64_FORMAT ", idx %u, ts %"
4409 GST_TIME_FORMAT ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4410 ", off_end %" G_GUINT64_FORMAT, str_num, min_offset, index,
4411 GST_TIME_ARGS (stream->current_timestamp),
4412 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4413 stream->current_offset_end);
4415 /* index data refers to data, not chunk header (for pull mode convenience) */
4417 GST_DEBUG_OBJECT (avi, "seeking to chunk at offset %" G_GUINT64_FORMAT,
4420 if (!perform_seek_to_offset (avi, min_offset)) {
4421 GST_DEBUG_OBJECT (avi, "seek event failed!");
4429 * Handle whether we can perform the seek event or if we have to let the chain
4430 * function handle seeks to build the seek indexes first.
4433 gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
4436 /* check for having parsed index already */
4437 if (!avi->have_index) {
4439 gboolean building_index;
4441 GST_OBJECT_LOCK (avi);
4442 /* handle the seek event in the chain function */
4443 avi->state = GST_AVI_DEMUX_SEEK;
4445 /* copy the event */
4446 if (avi->seek_event)
4447 gst_event_unref (avi->seek_event);
4448 avi->seek_event = gst_event_ref (event);
4450 /* set the building_index flag so that only one thread can setup the
4451 * structures for index seeking. */
4452 building_index = avi->building_index;
4453 if (!building_index) {
4454 avi->building_index = TRUE;
4455 if (avi->stream[0].indexes) {
4456 avi->odml_stream = 0;
4457 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
4458 offset = avi->odml_subidxs[0];
4460 offset = avi->idx1_offset;
4463 GST_OBJECT_UNLOCK (avi);
4465 if (!building_index) {
4466 /* seek to the first subindex or legacy index */
4467 GST_INFO_OBJECT (avi,
4468 "Seeking to legacy index/first subindex at %" G_GUINT64_FORMAT,
4470 return perform_seek_to_offset (avi, offset);
4473 /* FIXME: we have to always return true so that we don't block the seek
4475 * Note: maybe it is OK to return true if we're still building the index */
4479 return avi_demux_handle_seek_push (avi, pad, event);
4483 * Helper for gst_avi_demux_invert()
4486 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
4488 memcpy (tmp, d1, bytes);
4489 memcpy (d1, d2, bytes);
4490 memcpy (d2, tmp, bytes);
4494 #define gst_avi_demux_is_uncompressed(fourcc) \
4495 (fourcc == GST_RIFF_DIB || \
4496 fourcc == GST_RIFF_rgb || \
4497 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW)
4500 * Invert DIB buffers... Takes existing buffer and
4501 * returns either the buffer or a new one (with old
4502 * one dereferenced).
4503 * FIXME: can't we preallocate tmp? and remember stride, bpp?
4506 gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
4516 if (stream->strh->type != GST_RIFF_FCC_vids)
4519 if (stream->strf.vids == NULL) {
4520 GST_WARNING ("Failed to retrieve vids for stream");
4524 fourcc = (stream->strf.vids->compression) ?
4525 stream->strf.vids->compression : stream->strh->fcc_handler;
4526 if (!gst_avi_demux_is_uncompressed (fourcc)) {
4527 return buf; /* Ignore non DIB buffers */
4530 caps = gst_pad_get_current_caps (stream->pad);
4531 s = gst_caps_get_structure (caps, 0);
4532 gst_caps_unref (caps);
4534 if (!gst_structure_get_int (s, "bpp", &bpp)) {
4535 GST_WARNING ("Failed to retrieve depth from caps");
4539 h = stream->strf.vids->height;
4540 w = stream->strf.vids->width;
4541 stride = GST_ROUND_UP_4 (w * (bpp / 8));
4543 buf = gst_buffer_make_writable (buf);
4545 gst_buffer_map (buf, &map, GST_MAP_READWRITE);
4546 if (map.size < (stride * h)) {
4547 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
4548 gst_buffer_unmap (buf, &map);
4552 tmp = g_malloc (stride);
4554 for (y = 0; y < h / 2; y++) {
4555 swap_line (map.data + stride * y, map.data + stride * (h - 1 - y), tmp,
4561 gst_buffer_unmap (buf, &map);
4568 gst_avi_demux_add_assoc (GstAviDemux * avi, GstAviStream * stream,
4569 GstClockTime timestamp, guint64 offset, gboolean keyframe)
4571 /* do not add indefinitely for open-ended streaming */
4572 if (G_UNLIKELY (avi->element_index && avi->seekable)) {
4573 GST_LOG_OBJECT (avi, "adding association %" GST_TIME_FORMAT "-> %"
4574 G_GUINT64_FORMAT, GST_TIME_ARGS (timestamp), offset);
4575 gst_index_add_association (avi->element_index, avi->index_id,
4576 keyframe ? GST_ASSOCIATION_FLAG_KEY_UNIT :
4577 GST_ASSOCIATION_FLAG_DELTA_UNIT, GST_FORMAT_TIME, timestamp,
4578 GST_FORMAT_BYTES, offset, NULL);
4579 /* current_entry is DEFAULT (frame #) */
4580 gst_index_add_association (avi->element_index, stream->index_id,
4581 keyframe ? GST_ASSOCIATION_FLAG_KEY_UNIT :
4582 GST_ASSOCIATION_FLAG_DELTA_UNIT, GST_FORMAT_TIME, timestamp,
4583 GST_FORMAT_BYTES, offset, GST_FORMAT_DEFAULT, stream->current_entry,
4590 * Returns the aggregated GstFlowReturn.
4592 static GstFlowReturn
4593 gst_avi_demux_combine_flows (GstAviDemux * avi, GstAviStream * stream,
4597 gboolean unexpected = FALSE, not_linked = TRUE;
4599 /* store the value */
4600 stream->last_flow = ret;
4602 /* any other error that is not-linked or eos can be returned right away */
4603 if (G_LIKELY (ret != GST_FLOW_EOS && ret != GST_FLOW_NOT_LINKED))
4606 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4607 for (i = 0; i < avi->num_streams; i++) {
4608 GstAviStream *ostream = &avi->stream[i];
4610 ret = ostream->last_flow;
4611 /* no unexpected or unlinked, return */
4612 if (G_LIKELY (ret != GST_FLOW_EOS && ret != GST_FLOW_NOT_LINKED))
4615 /* we check to see if we have at least 1 unexpected or all unlinked */
4616 unexpected |= (ret == GST_FLOW_EOS);
4617 not_linked &= (ret == GST_FLOW_NOT_LINKED);
4619 /* when we get here, we all have unlinked or unexpected */
4621 ret = GST_FLOW_NOT_LINKED;
4622 else if (unexpected)
4625 GST_LOG_OBJECT (avi, "combined %s to return %s",
4626 gst_flow_get_name (stream->last_flow), gst_flow_get_name (ret));
4630 /* move @stream to the next position in its index */
4631 static GstFlowReturn
4632 gst_avi_demux_advance (GstAviDemux * avi, GstAviStream * stream,
4635 guint old_entry, new_entry;
4637 old_entry = stream->current_entry;
4639 new_entry = old_entry + 1;
4641 /* see if we reached the end */
4642 if (new_entry >= stream->stop_entry) {
4643 if (avi->segment.rate < 0.0) {
4644 if (stream->step_entry == stream->start_entry) {
4645 /* we stepped all the way to the start, eos */
4646 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
4649 /* backwards, stop becomes step, find a new step */
4650 stream->stop_entry = stream->step_entry;
4651 stream->step_entry = gst_avi_demux_index_prev (avi, stream,
4652 stream->stop_entry, TRUE);
4654 GST_DEBUG_OBJECT (avi,
4655 "reverse playback jump: start %u, step %u, stop %u",
4656 stream->start_entry, stream->step_entry, stream->stop_entry);
4658 /* and start from the previous keyframe now */
4659 new_entry = stream->step_entry;
4662 GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
4667 if (new_entry != old_entry) {
4668 stream->current_entry = new_entry;
4669 stream->current_total = stream->index[new_entry].total;
4671 if (new_entry == old_entry + 1) {
4672 GST_DEBUG_OBJECT (avi, "moved forwards from %u to %u",
4673 old_entry, new_entry);
4674 /* we simply moved one step forwards, reuse current info */
4675 stream->current_timestamp = stream->current_ts_end;
4676 stream->current_offset = stream->current_offset_end;
4677 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4678 NULL, &stream->current_ts_end, NULL, &stream->current_offset_end);
4680 /* we moved DISCONT, full update */
4681 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4682 &stream->current_timestamp, &stream->current_ts_end,
4683 &stream->current_offset, &stream->current_offset_end);
4684 /* and MARK discont for this stream */
4685 stream->last_flow = GST_FLOW_OK;
4686 stream->discont = TRUE;
4687 GST_DEBUG_OBJECT (avi, "Moved from %u to %u, ts %" GST_TIME_FORMAT
4688 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4689 ", off_end %" G_GUINT64_FORMAT, old_entry, new_entry,
4690 GST_TIME_ARGS (stream->current_timestamp),
4691 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4692 stream->current_offset_end);
4700 GST_DEBUG_OBJECT (avi, "we are EOS");
4701 /* setting current_timestamp to -1 marks EOS */
4702 stream->current_timestamp = -1;
4703 return GST_FLOW_EOS;
4707 /* find the stream with the lowest current position when going forwards or with
4708 * the highest position when going backwards, this is the stream
4709 * we should push from next */
4711 gst_avi_demux_find_next (GstAviDemux * avi, gfloat rate)
4713 guint64 min_time, max_time;
4714 guint stream_num, i;
4717 min_time = G_MAXUINT64;
4720 for (i = 0; i < avi->num_streams; i++) {
4722 GstAviStream *stream;
4724 stream = &avi->stream[i];
4726 /* ignore streams that finished */
4727 if (stream->last_flow == GST_FLOW_EOS)
4730 position = stream->current_timestamp;
4732 /* position of -1 is EOS */
4733 if (position != -1) {
4734 if (rate > 0.0 && position < min_time) {
4735 min_time = position;
4737 } else if (rate < 0.0 && position >= max_time) {
4738 max_time = position;
4746 static GstFlowReturn
4747 gst_avi_demux_loop_data (GstAviDemux * avi)
4749 GstFlowReturn ret = GST_FLOW_OK;
4751 GstAviStream *stream;
4752 gboolean processed = FALSE;
4754 guint64 offset, size;
4755 GstClockTime timestamp, duration;
4756 guint64 out_offset, out_offset_end;
4758 GstAviIndexEntry *entry;
4761 stream_num = gst_avi_demux_find_next (avi, avi->segment.rate);
4764 if (G_UNLIKELY (stream_num == -1)) {
4765 GST_DEBUG_OBJECT (avi, "all streams are EOS");
4769 /* we have the stream now */
4770 stream = &avi->stream[stream_num];
4772 /* skip streams without pads */
4774 GST_DEBUG_OBJECT (avi, "skipping entry from stream %d without pad",
4779 /* get the timing info for the entry */
4780 timestamp = stream->current_timestamp;
4781 duration = stream->current_ts_end - timestamp;
4782 out_offset = stream->current_offset;
4783 out_offset_end = stream->current_offset_end;
4785 /* get the entry data info */
4786 entry = &stream->index[stream->current_entry];
4787 offset = entry->offset;
4789 keyframe = ENTRY_IS_KEYFRAME (entry);
4791 /* skip empty entries */
4793 GST_DEBUG_OBJECT (avi, "Skipping entry %u (%" G_GUINT64_FORMAT ", %p)",
4794 stream->current_entry, size, stream->pad);
4798 if (avi->segment.rate > 0.0) {
4799 /* only check this for fowards playback for now */
4800 if (keyframe && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
4801 && (timestamp > avi->segment.stop)) {
4806 GST_LOG ("reading buffer (size=%" G_GUINT64_FORMAT "), stream %d, pos %"
4807 G_GUINT64_FORMAT " (0x%" G_GINT64_MODIFIER "x), kf %d", size,
4808 stream_num, offset, offset, keyframe);
4810 /* FIXME, check large chunks and cut them up */
4812 /* pull in the data */
4814 ret = gst_pad_pull_range (avi->sinkpad, offset, size, &buf);
4815 if (ret != GST_FLOW_OK)
4818 /* check for short buffers, this is EOS as well */
4819 if (gst_buffer_get_size (buf) < size)
4822 /* invert the picture if needed */
4823 buf = gst_avi_demux_invert (stream, buf);
4825 /* mark non-keyframes */
4827 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4828 GST_BUFFER_PTS (buf) = timestamp;
4830 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4831 GST_BUFFER_PTS (buf) = GST_CLOCK_TIME_NONE;
4833 GST_BUFFER_DTS (buf) = timestamp;
4834 GST_BUFFER_DURATION (buf) = duration;
4835 GST_BUFFER_OFFSET (buf) = out_offset;
4836 GST_BUFFER_OFFSET_END (buf) = out_offset_end;
4838 /* mark discont when pending */
4839 if (stream->discont) {
4840 GST_DEBUG_OBJECT (avi, "setting DISCONT flag");
4841 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4842 stream->discont = FALSE;
4845 gst_avi_demux_add_assoc (avi, stream, timestamp, offset, keyframe);
4848 /* update current position in the segment */
4849 avi->segment.position = timestamp;
4851 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %" G_GSIZE_FORMAT ", ts %"
4852 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4853 ", off_end %" G_GUINT64_FORMAT,
4854 gst_buffer_get_size (buf), GST_TIME_ARGS (timestamp),
4855 GST_TIME_ARGS (duration), out_offset, out_offset_end);
4857 ret = gst_pad_push (stream->pad, buf);
4859 /* mark as processed, we increment the frame and byte counters then
4860 * leave the while loop and return the GstFlowReturn */
4863 if (avi->segment.rate < 0) {
4864 if (timestamp > avi->segment.stop && ret == GST_FLOW_EOS) {
4865 /* In reverse playback we can get a GST_FLOW_EOS when
4866 * we are at the end of the segment, so we just need to jump
4867 * back to the previous section. */
4868 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
4873 /* move to next item */
4874 ret = gst_avi_demux_advance (avi, stream, ret);
4877 ret = gst_avi_demux_combine_flows (avi, stream, ret);
4878 } while (!processed);
4886 GST_DEBUG_OBJECT (avi, "No samples left for any streams - EOS");
4892 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
4893 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
4894 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (avi->segment.stop));
4896 /* move to next stream */
4901 GST_DEBUG_OBJECT (avi, "pull range failed: pos=%" G_GUINT64_FORMAT
4902 " size=%" G_GUINT64_FORMAT, offset, size);
4907 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
4908 ", only got %" G_GSIZE_FORMAT "/%" G_GUINT64_FORMAT
4909 " bytes (truncated file?)", offset, gst_buffer_get_size (buf), size);
4910 gst_buffer_unref (buf);
4917 * Read data. If we have an index it delegates to
4918 * gst_avi_demux_process_next_entry().
4920 static GstFlowReturn
4921 gst_avi_demux_stream_data (GstAviDemux * avi)
4926 GstFlowReturn res = GST_FLOW_OK;
4928 if (G_UNLIKELY (avi->have_eos)) {
4929 /* Clean adapter, we're done */
4930 gst_adapter_clear (avi->adapter);
4931 return GST_FLOW_EOS;
4934 if (G_UNLIKELY (avi->todrop)) {
4937 if ((drop = gst_adapter_available (avi->adapter))) {
4938 if (drop > avi->todrop)
4940 GST_DEBUG_OBJECT (avi, "Dropping %d bytes", drop);
4941 gst_adapter_flush (avi->adapter, drop);
4942 avi->todrop -= drop;
4943 avi->offset += drop;
4947 /* Iterate until need more data, so adapter won't grow too much */
4949 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
4953 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
4954 GST_FOURCC_ARGS (tag), size);
4956 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
4957 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
4958 GST_LOG ("Chunk ok");
4959 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
4960 GST_DEBUG ("Found sub-index tag");
4961 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4962 /* accept 0 size buffer here */
4963 avi->abort_buffering = FALSE;
4964 GST_DEBUG (" skipping %d bytes for now", size);
4965 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4968 } else if (tag == GST_RIFF_TAG_RIFF) {
4969 /* RIFF tags can appear in ODML files, just jump over them */
4970 if (gst_adapter_available (avi->adapter) >= 12) {
4971 GST_DEBUG ("Found RIFF tag, skipping RIFF header");
4972 gst_adapter_flush (avi->adapter, 12);
4976 } else if (tag == GST_RIFF_TAG_idx1) {
4977 GST_DEBUG ("Found index tag");
4978 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4979 /* accept 0 size buffer here */
4980 avi->abort_buffering = FALSE;
4981 GST_DEBUG (" skipping %d bytes for now", size);
4982 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4985 } else if (tag == GST_RIFF_TAG_LIST) {
4986 /* movi chunks might be grouped in rec list */
4987 if (gst_adapter_available (avi->adapter) >= 12) {
4988 GST_DEBUG ("Found LIST tag, skipping LIST header");
4989 gst_adapter_flush (avi->adapter, 12);
4993 } else if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ) {
4994 /* rec list might contain JUNK chunks */
4995 GST_DEBUG ("Found JUNK tag");
4996 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4997 /* accept 0 size buffer here */
4998 avi->abort_buffering = FALSE;
4999 GST_DEBUG (" skipping %d bytes for now", size);
5000 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
5004 GST_DEBUG ("No more stream chunks, send EOS");
5005 avi->have_eos = TRUE;
5006 return GST_FLOW_EOS;
5009 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
5010 /* supposedly one hopes to catch a nicer chunk later on ... */
5011 /* FIXME ?? give up here rather than possibly ending up going
5012 * through the whole file */
5013 if (avi->abort_buffering) {
5014 avi->abort_buffering = FALSE;
5016 gst_adapter_flush (avi->adapter, 8);
5023 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
5024 GST_FOURCC_ARGS (tag), size);
5026 stream_nr = CHUNKID_TO_STREAMNR (tag);
5028 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
5030 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
5031 stream_nr, GST_FOURCC_ARGS (tag));
5032 avi->offset += 8 + GST_ROUND_UP_2 (size);
5033 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
5035 GstAviStream *stream;
5036 GstClockTime next_ts = 0;
5037 GstBuffer *buf = NULL;
5041 gboolean saw_desired_kf = stream_nr != avi->main_stream
5042 || avi->offset >= avi->seek_kf_offset;
5044 if (stream_nr == avi->main_stream && avi->offset == avi->seek_kf_offset) {
5045 GST_DEBUG_OBJECT (avi, "Desired keyframe reached");
5046 avi->seek_kf_offset = 0;
5049 if (saw_desired_kf) {
5050 gst_adapter_flush (avi->adapter, 8);
5053 buf = gst_adapter_take_buffer (avi->adapter, GST_ROUND_UP_2 (size));
5054 /* patch the size */
5055 gst_buffer_resize (buf, 0, size);
5060 GST_DEBUG_OBJECT (avi,
5061 "Desired keyframe not yet reached, flushing chunk");
5062 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
5066 offset = avi->offset;
5068 avi->offset += 8 + GST_ROUND_UP_2 (size);
5070 stream = &avi->stream[stream_nr];
5072 /* set delay (if any)
5073 if (stream->strh->init_frames == stream->current_frame &&
5075 stream->delay = next_ts;
5078 /* parsing of corresponding header may have failed */
5079 if (G_UNLIKELY (!stream->pad)) {
5080 GST_WARNING_OBJECT (avi, "no pad for stream ID %" GST_FOURCC_FORMAT,
5081 GST_FOURCC_ARGS (tag));
5083 gst_buffer_unref (buf);
5085 /* get time of this buffer */
5086 gst_pad_query_position (stream->pad, GST_FORMAT_TIME,
5087 (gint64 *) & next_ts);
5090 gst_avi_demux_add_assoc (avi, stream, next_ts, offset, FALSE);
5093 /* increment our positions */
5094 stream->current_entry++;
5095 stream->current_total += size;
5097 /* update current position in the segment */
5098 avi->segment.position = next_ts;
5100 if (saw_desired_kf && buf) {
5101 GstClockTime dur_ts = 0;
5103 /* invert the picture if needed */
5104 buf = gst_avi_demux_invert (stream, buf);
5106 gst_pad_query_position (stream->pad, GST_FORMAT_TIME,
5107 (gint64 *) & dur_ts);
5109 GST_BUFFER_DTS (buf) = next_ts;
5110 GST_BUFFER_PTS (buf) = GST_CLOCK_TIME_NONE;
5111 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
5112 if (stream->strh->type == GST_RIFF_FCC_vids) {
5113 GST_BUFFER_OFFSET (buf) = stream->current_entry - 1;
5114 GST_BUFFER_OFFSET_END (buf) = stream->current_entry;
5116 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
5117 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
5120 GST_DEBUG_OBJECT (avi,
5121 "Pushing buffer with time=%" GST_TIME_FORMAT ", duration %"
5122 GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
5123 " and size %d over pad %s", GST_TIME_ARGS (next_ts),
5124 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
5125 GST_BUFFER_OFFSET (buf), size, GST_PAD_NAME (stream->pad));
5127 /* mark discont when pending */
5128 if (G_UNLIKELY (stream->discont)) {
5129 GST_DEBUG_OBJECT (avi, "Setting DISCONT");
5130 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
5131 stream->discont = FALSE;
5133 res = gst_pad_push (stream->pad, buf);
5137 res = gst_avi_demux_combine_flows (avi, stream, res);
5138 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5139 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
5151 * Send pending tags.
5154 push_tag_lists (GstAviDemux * avi)
5162 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
5164 for (i = 0; i < avi->num_streams; i++) {
5165 GstAviStream *stream = &avi->stream[i];
5166 GstPad *pad = stream->pad;
5168 tags = stream->taglist;
5171 GST_DEBUG_OBJECT (pad, "Tags: %" GST_PTR_FORMAT, tags);
5173 gst_pad_push_event (pad, gst_event_new_tag (tags));
5174 stream->taglist = NULL;
5178 if (!(tags = avi->globaltags))
5179 tags = gst_tag_list_new_empty ();
5181 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
5182 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
5184 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, tags);
5185 gst_tag_list_set_scope (tags, GST_TAG_SCOPE_GLOBAL);
5186 gst_avi_demux_push_event (avi, gst_event_new_tag (tags));
5187 avi->globaltags = NULL;
5188 avi->got_tags = FALSE;
5192 gst_avi_demux_loop (GstPad * pad)
5195 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
5197 switch (avi->state) {
5198 case GST_AVI_DEMUX_START:
5199 res = gst_avi_demux_stream_init_pull (avi);
5200 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5201 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5204 avi->state = GST_AVI_DEMUX_HEADER;
5206 case GST_AVI_DEMUX_HEADER:
5207 res = gst_avi_demux_stream_header_pull (avi);
5208 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5209 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5212 avi->state = GST_AVI_DEMUX_MOVI;
5214 case GST_AVI_DEMUX_MOVI:
5215 if (G_UNLIKELY (avi->seg_event)) {
5216 gst_avi_demux_push_event (avi, avi->seg_event);
5217 avi->seg_event = NULL;
5219 if (G_UNLIKELY (avi->got_tags)) {
5220 push_tag_lists (avi);
5222 /* process each index entry in turn */
5223 res = gst_avi_demux_loop_data (avi);
5225 /* pause when error */
5226 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5227 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
5232 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
5233 res = GST_FLOW_ERROR;
5242 gboolean push_eos = FALSE;
5243 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
5244 gst_pad_pause_task (avi->sinkpad);
5246 if (res == GST_FLOW_EOS) {
5247 /* handle end-of-stream/segment */
5248 /* so align our position with the end of it, if there is one
5249 * this ensures a subsequent will arrive at correct base/acc time */
5250 if (avi->segment.rate > 0.0 &&
5251 GST_CLOCK_TIME_IS_VALID (avi->segment.stop))
5252 avi->segment.position = avi->segment.stop;
5253 else if (avi->segment.rate < 0.0)
5254 avi->segment.position = avi->segment.start;
5255 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5258 if ((stop = avi->segment.stop) == -1)
5259 stop = avi->segment.duration;
5261 GST_INFO_OBJECT (avi, "sending segment_done");
5263 gst_element_post_message
5264 (GST_ELEMENT_CAST (avi),
5265 gst_message_new_segment_done (GST_OBJECT_CAST (avi),
5266 GST_FORMAT_TIME, stop));
5267 gst_avi_demux_push_event (avi,
5268 gst_event_new_segment_done (GST_FORMAT_TIME, stop));
5272 } else if (res == GST_FLOW_NOT_LINKED || res < GST_FLOW_EOS) {
5273 /* for fatal errors we post an error message, wrong-state is
5274 * not fatal because it happens due to flushes and only means
5275 * that we should stop now. */
5276 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
5277 (_("Internal data stream error.")),
5278 ("streaming stopped, reason %s", gst_flow_get_name (res)));
5282 GST_INFO_OBJECT (avi, "sending eos");
5283 if (!gst_avi_demux_push_event (avi, gst_event_new_eos ()) &&
5284 (res == GST_FLOW_EOS)) {
5285 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
5286 (NULL), ("got eos but no streams (yet)"));
5293 static GstFlowReturn
5294 gst_avi_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
5297 GstAviDemux *avi = GST_AVI_DEMUX (parent);
5300 if (GST_BUFFER_IS_DISCONT (buf)) {
5301 GST_DEBUG_OBJECT (avi, "got DISCONT");
5302 gst_adapter_clear (avi->adapter);
5303 /* mark all streams DISCONT */
5304 for (i = 0; i < avi->num_streams; i++)
5305 avi->stream[i].discont = TRUE;
5308 GST_DEBUG ("Store %" G_GSIZE_FORMAT " bytes in adapter",
5309 gst_buffer_get_size (buf));
5310 gst_adapter_push (avi->adapter, buf);
5312 switch (avi->state) {
5313 case GST_AVI_DEMUX_START:
5314 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
5315 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5319 case GST_AVI_DEMUX_HEADER:
5320 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
5321 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5325 case GST_AVI_DEMUX_MOVI:
5326 if (G_UNLIKELY (avi->seg_event)) {
5327 gst_avi_demux_push_event (avi, avi->seg_event);
5328 avi->seg_event = NULL;
5330 if (G_UNLIKELY (avi->got_tags)) {
5331 push_tag_lists (avi);
5333 res = gst_avi_demux_stream_data (avi);
5335 case GST_AVI_DEMUX_SEEK:
5341 /* obtain and parse indexes */
5342 if (avi->stream[0].indexes && !gst_avi_demux_read_subindexes_push (avi))
5343 /* seek in subindex read function failed */
5346 if (!avi->stream[0].indexes && !avi->have_index
5347 && avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
5348 gst_avi_demux_stream_index_push (avi);
5350 if (avi->have_index) {
5351 /* use the indexes now to construct nice durations */
5352 gst_avi_demux_calculate_durations_from_index (avi);
5354 /* still parsing indexes */
5358 GST_OBJECT_LOCK (avi);
5359 event = avi->seek_event;
5360 avi->seek_event = NULL;
5361 GST_OBJECT_UNLOCK (avi);
5363 /* calculate and perform seek */
5364 if (!avi_demux_handle_seek_push (avi, avi->sinkpad, event))
5367 gst_event_unref (event);
5368 avi->state = GST_AVI_DEMUX_MOVI;
5372 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
5373 ("Illegal internal state"));
5374 res = GST_FLOW_ERROR;
5378 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
5379 gst_flow_get_name (res));
5381 if (G_UNLIKELY (avi->abort_buffering))
5382 goto abort_buffering;
5389 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("failed to read indexes"));
5390 return GST_FLOW_ERROR;
5394 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("push mode seek failed"));
5395 return GST_FLOW_ERROR;
5399 avi->abort_buffering = FALSE;
5400 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
5401 return GST_FLOW_ERROR;
5406 gst_avi_demux_sink_activate (GstPad * sinkpad, GstObject * parent)
5411 query = gst_query_new_scheduling ();
5413 if (!gst_pad_peer_query (sinkpad, query)) {
5414 gst_query_unref (query);
5418 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
5419 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
5420 gst_query_unref (query);
5425 GST_DEBUG_OBJECT (sinkpad, "activating pull");
5426 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
5430 GST_DEBUG_OBJECT (sinkpad, "activating push");
5431 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
5436 gst_avi_demux_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
5437 GstPadMode mode, gboolean active)
5440 GstAviDemux *avi = GST_AVI_DEMUX (parent);
5443 case GST_PAD_MODE_PULL:
5445 avi->streaming = FALSE;
5446 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
5449 res = gst_pad_stop_task (sinkpad);
5452 case GST_PAD_MODE_PUSH:
5454 GST_DEBUG ("avi: activating push/chain function");
5455 avi->streaming = TRUE;
5457 GST_DEBUG ("avi: deactivating push/chain function");
5470 gst_avi_demux_set_index (GstElement * element, GstIndex * index)
5472 GstAviDemux *avi = GST_AVI_DEMUX (element);
5474 GST_OBJECT_LOCK (avi);
5475 if (avi->element_index)
5476 gst_object_unref (avi->element_index);
5478 avi->element_index = gst_object_ref (index);
5480 avi->element_index = NULL;
5482 GST_OBJECT_UNLOCK (avi);
5483 /* object lock might be taken again */
5485 gst_index_get_writer_id (index, GST_OBJECT_CAST (element), &avi->index_id);
5486 GST_DEBUG_OBJECT (avi, "Set index %" GST_PTR_FORMAT, avi->element_index);
5490 gst_avi_demux_get_index (GstElement * element)
5492 GstIndex *result = NULL;
5493 GstAviDemux *avi = GST_AVI_DEMUX (element);
5495 GST_OBJECT_LOCK (avi);
5496 if (avi->element_index)
5497 result = gst_object_ref (avi->element_index);
5498 GST_OBJECT_UNLOCK (avi);
5500 GST_DEBUG_OBJECT (avi, "Returning index %" GST_PTR_FORMAT, result);
5506 static GstStateChangeReturn
5507 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
5509 GstStateChangeReturn ret;
5510 GstAviDemux *avi = GST_AVI_DEMUX (element);
5512 switch (transition) {
5513 case GST_STATE_CHANGE_READY_TO_PAUSED:
5514 avi->streaming = FALSE;
5515 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
5521 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5522 if (ret == GST_STATE_CHANGE_FAILURE)
5525 switch (transition) {
5526 case GST_STATE_CHANGE_PAUSED_TO_READY:
5527 avi->have_index = FALSE;
5528 gst_avi_demux_reset (avi);