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 filesrc location=test.avi ! avidemux name=demux demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink demux.video_00 ! queue ! decodebin ! ffmpegcolorspace ! 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 static void gst_avi_demux_base_init (GstAviDemuxClass * klass);
76 static void gst_avi_demux_class_init (GstAviDemuxClass * klass);
77 static void gst_avi_demux_init (GstAviDemux * avi);
78 static void gst_avi_demux_finalize (GObject * object);
80 static void gst_avi_demux_reset (GstAviDemux * avi);
83 static const GstEventMask *gst_avi_demux_get_event_mask (GstPad * pad);
85 static gboolean gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event);
86 static gboolean gst_avi_demux_handle_sink_event (GstPad * pad,
88 static gboolean gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event);
91 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
93 static const GstQueryType *gst_avi_demux_get_src_query_types (GstPad * pad);
94 static gboolean gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query);
95 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
96 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
98 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
99 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
101 static gboolean gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
103 static void gst_avi_demux_loop (GstPad * pad);
104 static gboolean gst_avi_demux_sink_activate (GstPad * sinkpad);
105 static gboolean gst_avi_demux_sink_activate_pull (GstPad * sinkpad,
107 static gboolean gst_avi_demux_activate_push (GstPad * pad, gboolean active);
108 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstBuffer * buf);
110 static void gst_avi_demux_set_index (GstElement * element, GstIndex * index);
111 static GstIndex *gst_avi_demux_get_index (GstElement * element);
112 static GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
113 GstStateChange transition);
114 static void gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi);
115 static void gst_avi_demux_get_buffer_info (GstAviDemux * avi,
116 GstAviStream * stream, guint entry_n, GstClockTime * timestamp,
117 GstClockTime * ts_end, guint64 * offset, guint64 * offset_end);
119 static void gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf);
121 static GstElementClass *parent_class = NULL;
123 /* GObject methods */
126 gst_avi_demux_get_type (void)
128 static GType avi_demux_type = 0;
130 if (!avi_demux_type) {
131 static const GTypeInfo avi_demux_info = {
132 sizeof (GstAviDemuxClass),
133 (GBaseInitFunc) gst_avi_demux_base_init,
135 (GClassInitFunc) gst_avi_demux_class_init,
138 sizeof (GstAviDemux),
140 (GInstanceInitFunc) gst_avi_demux_init,
144 g_type_register_static (GST_TYPE_ELEMENT,
145 "GstAviDemux", &avi_demux_info, 0);
148 return avi_demux_type;
152 gst_avi_demux_base_init (GstAviDemuxClass * klass)
154 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
155 GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl;
156 GstCaps *audcaps, *vidcaps, *subcaps;
158 audcaps = gst_riff_create_audio_template_caps ();
159 gst_caps_append (audcaps, gst_caps_new_simple ("audio/x-avi-unknown", NULL));
160 audiosrctempl = gst_pad_template_new ("audio_%02d",
161 GST_PAD_SRC, GST_PAD_SOMETIMES, audcaps);
163 vidcaps = gst_riff_create_video_template_caps ();
164 gst_caps_append (vidcaps, gst_riff_create_iavs_template_caps ());
165 gst_caps_append (vidcaps, gst_caps_new_simple ("video/x-avi-unknown", NULL));
166 videosrctempl = gst_pad_template_new ("video_%02d",
167 GST_PAD_SRC, GST_PAD_SOMETIMES, vidcaps);
169 subcaps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
170 subsrctempl = gst_pad_template_new ("subtitle_%02d",
171 GST_PAD_SRC, GST_PAD_SOMETIMES, subcaps);
172 gst_element_class_add_pad_template (element_class, audiosrctempl);
173 gst_element_class_add_pad_template (element_class, videosrctempl);
174 gst_element_class_add_pad_template (element_class, subsrctempl);
175 gst_element_class_add_pad_template (element_class,
176 gst_static_pad_template_get (&sink_templ));
177 gst_element_class_set_details_simple (element_class, "Avi demuxer",
179 "Demultiplex an avi file into audio and video",
180 "Erik Walthinsen <omega@cse.ogi.edu>, "
181 "Wim Taymans <wim.taymans@chello.be>, "
182 "Thijs Vermeir <thijsvermeir@gmail.com>");
186 gst_avi_demux_class_init (GstAviDemuxClass * klass)
188 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
189 GObjectClass *gobject_class = (GObjectClass *) klass;
191 GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
192 0, "Demuxer for AVI streams");
194 parent_class = g_type_class_peek_parent (klass);
196 gobject_class->finalize = gst_avi_demux_finalize;
197 gstelement_class->change_state =
198 GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
200 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_avi_demux_set_index);
201 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_avi_demux_get_index);
205 gst_avi_demux_init (GstAviDemux * avi)
207 avi->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
208 gst_pad_set_activate_function (avi->sinkpad,
209 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate));
210 gst_pad_set_activatepull_function (avi->sinkpad,
211 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate_pull));
212 gst_pad_set_activatepush_function (avi->sinkpad,
213 GST_DEBUG_FUNCPTR (gst_avi_demux_activate_push));
214 gst_pad_set_chain_function (avi->sinkpad,
215 GST_DEBUG_FUNCPTR (gst_avi_demux_chain));
216 gst_pad_set_event_function (avi->sinkpad,
217 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_sink_event));
218 gst_element_add_pad (GST_ELEMENT_CAST (avi), avi->sinkpad);
220 avi->adapter = gst_adapter_new ();
222 gst_avi_demux_reset (avi);
226 gst_avi_demux_finalize (GObject * object)
228 GstAviDemux *avi = GST_AVI_DEMUX (object);
230 GST_DEBUG ("AVI: finalize");
232 g_object_unref (avi->adapter);
234 G_OBJECT_CLASS (parent_class)->finalize (object);
238 gst_avi_demux_reset_stream (GstAviDemux * avi, GstAviStream * stream)
240 g_free (stream->strh);
241 g_free (stream->strf.data);
242 g_free (stream->name);
243 g_free (stream->index);
244 g_free (stream->indexes);
245 if (stream->initdata)
246 gst_buffer_unref (stream->initdata);
247 if (stream->extradata)
248 gst_buffer_unref (stream->extradata);
250 if (stream->exposed) {
251 gst_pad_set_active (stream->pad, FALSE);
252 gst_element_remove_pad (GST_ELEMENT_CAST (avi), stream->pad);
254 gst_object_unref (stream->pad);
256 if (stream->taglist) {
257 gst_tag_list_free (stream->taglist);
258 stream->taglist = NULL;
260 memset (stream, 0, sizeof (GstAviStream));
264 gst_avi_demux_reset (GstAviDemux * avi)
268 GST_DEBUG ("AVI: reset");
270 for (i = 0; i < avi->num_streams; i++)
271 gst_avi_demux_reset_stream (avi, &avi->stream[i]);
273 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
274 avi->num_streams = 0;
275 avi->num_v_streams = 0;
276 avi->num_a_streams = 0;
277 avi->num_t_streams = 0;
278 avi->main_stream = -1;
280 avi->state = GST_AVI_DEMUX_START;
282 avi->building_index = FALSE;
284 avi->index_offset = 0;
288 if (avi->element_index)
289 gst_object_unref (avi->element_index);
290 avi->element_index = NULL;
292 if (avi->close_seg_event) {
293 gst_event_unref (avi->close_seg_event);
294 avi->close_seg_event = NULL;
296 if (avi->seg_event) {
297 gst_event_unref (avi->seg_event);
298 avi->seg_event = NULL;
300 if (avi->seek_event) {
301 gst_event_unref (avi->seek_event);
302 avi->seek_event = NULL;
306 gst_tag_list_free (avi->globaltags);
307 avi->globaltags = NULL;
309 avi->got_tags = TRUE; /* we always want to push global tags */
310 avi->have_eos = FALSE;
311 avi->seekable = TRUE;
313 gst_adapter_clear (avi->adapter);
315 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
319 /* GstElement methods */
322 static const GstFormat *
323 gst_avi_demux_get_src_formats (GstPad * pad)
325 GstAviStream *stream = gst_pad_get_element_private (pad);
327 static const GstFormat src_a_formats[] = {
333 static const GstFormat src_v_formats[] = {
339 return (stream->strh->type == GST_RIFF_FCC_auds ?
340 src_a_formats : src_v_formats);
344 /* assumes stream->strf.auds->av_bps != 0 */
345 static inline GstClockTime
346 avi_stream_convert_bytes_to_time_unchecked (GstAviStream * stream,
349 return gst_util_uint64_scale_int (bytes, GST_SECOND,
350 stream->strf.auds->av_bps);
353 static inline guint64
354 avi_stream_convert_time_to_bytes_unchecked (GstAviStream * stream,
357 return gst_util_uint64_scale_int (time, stream->strf.auds->av_bps,
361 /* assumes stream->strh->rate != 0 */
362 static inline GstClockTime
363 avi_stream_convert_frames_to_time_unchecked (GstAviStream * stream,
366 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
370 static inline guint64
371 avi_stream_convert_time_to_frames_unchecked (GstAviStream * stream,
374 return gst_util_uint64_scale (time, stream->strh->rate,
375 stream->strh->scale * GST_SECOND);
379 gst_avi_demux_src_convert (GstPad * pad,
380 GstFormat src_format,
381 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
383 GstAviStream *stream = gst_pad_get_element_private (pad);
387 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
388 ", dest_format:%s", gst_format_get_name (src_format), src_value,
389 gst_format_get_name (*dest_format));
391 if (G_UNLIKELY (src_format == *dest_format)) {
392 *dest_value = src_value;
395 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
399 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
400 (src_format == GST_FORMAT_BYTES
401 || *dest_format == GST_FORMAT_BYTES))) {
406 switch (src_format) {
407 case GST_FORMAT_TIME:
408 switch (*dest_format) {
409 case GST_FORMAT_BYTES:
410 *dest_value = gst_util_uint64_scale_int (src_value,
411 stream->strf.auds->av_bps, GST_SECOND);
413 case GST_FORMAT_DEFAULT:
415 gst_util_uint64_scale_round (src_value, stream->strh->rate,
416 stream->strh->scale * GST_SECOND);
423 case GST_FORMAT_BYTES:
424 switch (*dest_format) {
425 case GST_FORMAT_TIME:
426 if (stream->strf.auds->av_bps != 0) {
427 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
437 case GST_FORMAT_DEFAULT:
438 switch (*dest_format) {
439 case GST_FORMAT_TIME:
441 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
454 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
455 gst_format_get_name (*dest_format), *dest_value);
459 static const GstQueryType *
460 gst_avi_demux_get_src_query_types (GstPad * pad)
462 static const GstQueryType src_types[] = {
474 gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query)
477 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
479 GstAviStream *stream = gst_pad_get_element_private (pad);
481 if (!stream->strh || !stream->strf.data)
482 return gst_pad_query_default (pad, query);
484 switch (GST_QUERY_TYPE (query)) {
485 case GST_QUERY_POSITION:{
488 GST_DEBUG ("pos query for stream %u: frames %u, bytes %u",
489 stream->num, stream->current_entry, stream->current_total);
491 /* FIXME, this looks clumsy */
492 if (stream->strh->type == GST_RIFF_FCC_auds) {
493 if (stream->is_vbr) {
495 pos = gst_util_uint64_scale ((gint64) stream->current_entry *
496 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
497 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
498 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
499 } else if (stream->strf.auds->av_bps != 0) {
501 pos = gst_util_uint64_scale (stream->current_total, GST_SECOND,
502 (guint64) stream->strf.auds->av_bps);
503 GST_DEBUG_OBJECT (avi,
504 "CBR convert bytes %u, time %" GST_TIME_FORMAT,
505 stream->current_total, GST_TIME_ARGS (pos));
506 } else if (stream->idx_n != 0 && stream->total_bytes != 0) {
507 /* calculate timestamps based on percentage of length */
508 guint64 xlen = avi->avih->us_frame *
509 avi->avih->tot_frames * GST_USECOND;
511 if (stream->is_vbr) {
512 pos = gst_util_uint64_scale (xlen, stream->current_entry,
514 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
515 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
517 pos = gst_util_uint64_scale (xlen, stream->current_total,
518 stream->total_bytes);
519 GST_DEBUG_OBJECT (avi,
520 "CBR perc convert bytes %u, time %" GST_TIME_FORMAT,
521 stream->current_total, GST_TIME_ARGS (pos));
528 if (stream->strh->rate != 0) {
529 pos = gst_util_uint64_scale ((guint64) stream->current_entry *
530 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
532 pos = stream->current_entry * avi->avih->us_frame * GST_USECOND;
536 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
537 gst_query_set_position (query, GST_FORMAT_TIME, pos);
539 GST_WARNING ("pos query failed");
542 case GST_QUERY_DURATION:
545 GstClockTime duration;
547 /* only act on audio or video streams */
548 if (stream->strh->type != GST_RIFF_FCC_auds &&
549 stream->strh->type != GST_RIFF_FCC_vids) {
554 /* take stream duration, fall back to avih duration */
555 if ((duration = stream->duration) == -1)
556 duration = avi->duration;
558 gst_query_parse_duration (query, &fmt, NULL);
561 case GST_FORMAT_TIME:
562 gst_query_set_duration (query, fmt, duration);
564 case GST_FORMAT_DEFAULT:
567 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
570 if (stream->idx_n >= 0)
571 gst_query_set_duration (query, fmt, stream->idx_n);
572 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
573 duration, &fmt, &dur))
574 gst_query_set_duration (query, fmt, dur);
583 case GST_QUERY_SEEKING:{
586 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
587 if (fmt == GST_FORMAT_TIME) {
588 gboolean seekable = TRUE;
590 if (avi->streaming) {
591 seekable = avi->seekable;
594 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
595 0, stream->duration);
600 case GST_QUERY_CONVERT:{
601 GstFormat src_fmt, dest_fmt;
602 gint64 src_val, dest_val;
604 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
605 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
607 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
609 res = gst_pad_query_default (pad, query);
613 res = gst_pad_query_default (pad, query);
617 gst_object_unref (avi);
622 static const GstEventMask *
623 gst_avi_demux_get_event_mask (GstPad * pad)
625 static const GstEventMask masks[] = {
626 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
635 gst_avi_demux_seek_streams (GstAviDemux * avi, guint64 offset, gboolean before)
637 GstAviStream *stream;
638 GstIndexEntry *entry;
640 gint64 val, min = offset;
642 for (i = 0; i < avi->num_streams; i++) {
643 stream = &avi->stream[i];
645 entry = gst_index_get_assoc_entry (avi->element_index, stream->index_id,
646 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
647 GST_ASSOCIATION_FLAG_NONE, GST_FORMAT_BYTES, offset);
651 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
652 GST_DEBUG_OBJECT (avi, "stream %d, previous entry at %"
653 G_GUINT64_FORMAT, i, val);
661 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
662 stream->current_entry = 0;
663 stream->current_total = 0;
667 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
668 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT,
671 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &val);
672 stream->current_total = val;
673 gst_index_entry_assoc_map (entry, GST_FORMAT_DEFAULT, &val);
674 stream->current_entry = val;
681 gst_avi_demux_index_entry_offset_search (GstAviIndexEntry * entry,
684 if (entry->offset < *offset)
686 else if (entry->offset > *offset)
692 gst_avi_demux_seek_streams_index (GstAviDemux * avi, guint64 offset,
695 GstAviStream *stream;
696 GstAviIndexEntry *entry;
698 gint64 val, min = offset;
701 for (i = 0; i < avi->num_streams; i++) {
702 stream = &avi->stream[i];
704 /* compensate for chunk header */
707 gst_util_array_binary_search (stream->index, stream->idx_n,
708 sizeof (GstAviIndexEntry),
709 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
710 before ? GST_SEARCH_MODE_BEFORE : GST_SEARCH_MODE_AFTER, &offset, NULL);
714 index = entry - stream->index;
718 val = stream->index[index].offset;
719 GST_DEBUG_OBJECT (avi,
720 "stream %d, previous entry at %" G_GUINT64_FORMAT, i, val);
728 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
729 stream->current_entry = 0;
730 stream->current_total = 0;
734 val = stream->index[index].offset - 8;
735 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT, i,
738 stream->current_total = stream->index[index].total;
739 stream->current_entry = index;
745 #define GST_AVI_SEEK_PUSH_DISPLACE (4 * GST_SECOND)
748 gst_avi_demux_handle_sink_event (GstPad * pad, GstEvent * event)
751 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
753 GST_DEBUG_OBJECT (avi,
754 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
756 switch (GST_EVENT_TYPE (event)) {
757 case GST_EVENT_NEWSEGMENT:
761 gint64 start, stop, time, offset = 0;
765 /* some debug output */
766 gst_segment_init (&segment, GST_FORMAT_UNDEFINED);
767 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
768 &start, &stop, &time);
769 gst_segment_set_newsegment_full (&segment, update, rate, arate, format,
771 GST_DEBUG_OBJECT (avi,
772 "received format %d newsegment %" GST_SEGMENT_FORMAT, format,
775 /* chain will send initial newsegment after pads have been added */
776 if (avi->state != GST_AVI_DEMUX_MOVI) {
777 GST_DEBUG_OBJECT (avi, "still starting, eating event");
781 /* we only expect a BYTE segment, e.g. following a seek */
782 if (format != GST_FORMAT_BYTES) {
783 GST_DEBUG_OBJECT (avi, "unsupported segment format, ignoring");
787 if (avi->have_index) {
788 GstAviIndexEntry *entry;
789 guint i = 0, index = 0, k = 0;
790 GstAviStream *stream;
792 /* compensate chunk header, stored index offset points after header */
794 /* find which stream we're on */
796 stream = &avi->stream[i];
798 /* find the index for start bytes offset */
799 entry = gst_util_array_binary_search (stream->index,
800 stream->idx_n, sizeof (GstAviIndexEntry),
801 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
802 GST_SEARCH_MODE_AFTER, &start, NULL);
806 index = entry - stream->index;
808 /* we are on the stream with a chunk start offset closest to start */
809 if (!offset || stream->index[index].offset < offset) {
810 offset = stream->index[index].offset;
813 /* exact match needs no further searching */
814 if (stream->index[index].offset == start)
816 } while (++i < avi->num_streams);
819 stream = &avi->stream[k];
821 /* so we have no idea what is to come, or where we are */
823 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
827 /* get the ts corresponding to start offset bytes for the stream */
828 gst_avi_demux_get_buffer_info (avi, stream, index,
829 (GstClockTime *) & time, NULL, NULL, NULL);
830 } else if (avi->element_index) {
831 GstIndexEntry *entry;
833 /* Let's check if we have an index entry for this position */
834 entry = gst_index_get_assoc_entry (avi->element_index, avi->index_id,
835 GST_INDEX_LOOKUP_AFTER, GST_ASSOCIATION_FLAG_NONE,
836 GST_FORMAT_BYTES, start);
838 /* we can not go where we have not yet been before ... */
840 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
844 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &time);
845 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &offset);
847 GST_WARNING_OBJECT (avi, "no index data, forcing EOS");
851 stop = GST_CLOCK_TIME_NONE;
853 /* set up segment and send downstream */
854 gst_segment_set_newsegment_full (&avi->segment, update, rate, arate,
855 GST_FORMAT_TIME, time, stop, time);
856 GST_DEBUG_OBJECT (avi, "Pushing newseg update %d, rate %g, "
857 "applied rate %g, format %d, start %" G_GINT64_FORMAT ", "
858 "stop %" G_GINT64_FORMAT, update, rate, arate, GST_FORMAT_TIME,
860 gst_avi_demux_push_event (avi,
861 gst_event_new_new_segment_full (update, rate, arate, GST_FORMAT_TIME,
864 GST_DEBUG_OBJECT (avi, "next chunk expected at %" G_GINT64_FORMAT, start);
866 /* adjust state for streaming thread accordingly */
868 gst_avi_demux_seek_streams_index (avi, offset, FALSE);
870 gst_avi_demux_seek_streams (avi, offset, FALSE);
872 /* set up streaming thread */
873 g_assert (offset >= start);
875 avi->todrop = offset - start;
878 gst_event_unref (event);
883 avi->have_eos = TRUE;
888 if (avi->state != GST_AVI_DEMUX_MOVI) {
889 gst_event_unref (event);
890 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
891 (NULL), ("got eos and didn't receive a complete header object"));
892 } else if (!gst_avi_demux_push_event (avi, event)) {
893 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
894 (NULL), ("got eos but no streams (yet)"));
898 case GST_EVENT_FLUSH_STOP:
902 gst_adapter_clear (avi->adapter);
903 avi->have_eos = FALSE;
904 for (i = 0; i < avi->num_streams; i++) {
905 avi->stream[i].last_flow = GST_FLOW_OK;
906 avi->stream[i].discont = TRUE;
908 /* fall through to default case so that the event gets passed downstream */
911 res = gst_pad_event_default (pad, event);
915 gst_object_unref (avi);
921 gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event)
924 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
926 GST_DEBUG_OBJECT (avi,
927 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
929 switch (GST_EVENT_TYPE (event)) {
931 if (!avi->streaming) {
932 res = gst_avi_demux_handle_seek (avi, pad, event);
934 res = gst_avi_demux_handle_seek_push (avi, pad, event);
936 gst_event_unref (event);
939 case GST_EVENT_NAVIGATION:
941 gst_event_unref (event);
944 res = gst_pad_event_default (pad, event);
948 gst_object_unref (avi);
953 /* streaming helper (push) */
956 * gst_avi_demux_peek_chunk_info:
958 * @tag: holder for tag
959 * @size: holder for tag size
961 * Peek next chunk info (tag and size)
963 * Returns: TRUE when one chunk info has been got
966 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
968 const guint8 *data = NULL;
970 if (gst_adapter_available (avi->adapter) < 8)
973 data = gst_adapter_peek (avi->adapter, 8);
974 *tag = GST_READ_UINT32_LE (data);
975 *size = GST_READ_UINT32_LE (data + 4);
981 * gst_avi_demux_peek_chunk:
983 * @tag: holder for tag
984 * @size: holder for tag size
986 * Peek enough data for one full chunk
988 * Returns: %TRUE when one chunk has been got
991 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
993 guint32 peek_size = 0;
996 if (!gst_avi_demux_peek_chunk_info (avi, tag, size))
999 /* size 0 -> empty data buffer would surprise most callers,
1000 * large size -> do not bother trying to squeeze that into adapter,
1001 * so we throw poor man's exception, which can be caught if caller really
1002 * wants to handle 0 size chunk */
1003 if (!(*size) || (*size) >= (1 << 30))
1006 peek_size = (*size + 1) & ~1;
1007 available = gst_adapter_available (avi->adapter);
1009 GST_DEBUG_OBJECT (avi,
1010 "Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
1011 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
1013 if (available < (8 + peek_size))
1021 GST_INFO_OBJECT (avi, "Failed to peek");
1026 GST_INFO_OBJECT (avi,
1027 "Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT, *size,
1028 GST_FOURCC_ARGS (*tag));
1029 /* chain should give up */
1030 avi->abort_buffering = TRUE;
1035 GST_INFO_OBJECT (avi, "need more %d < %" G_GUINT32_FORMAT,
1036 available, 8 + peek_size);
1044 * gst_avi_demux_parse_file_header:
1045 * @element: caller element (used for errors/debug).
1046 * @buf: input data to be used for parsing.
1048 * "Open" a RIFF/AVI file. The buffer should be at least 12
1049 * bytes long. Takes ownership of @buf.
1051 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
1052 * Throws an error, caller should error out (fatal).
1055 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
1060 stamp = gst_util_get_timestamp ();
1062 /* riff_parse posts an error */
1063 if (!gst_riff_parse_file_header (element, buf, &doctype))
1066 if (doctype != GST_RIFF_RIFF_AVI)
1069 stamp = gst_util_get_timestamp () - stamp;
1070 GST_DEBUG_OBJECT (element, "header parsing took %" GST_TIME_FORMAT,
1071 GST_TIME_ARGS (stamp));
1078 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
1079 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
1080 GST_FOURCC_ARGS (doctype)));
1086 * Read AVI file tag when streaming
1088 static GstFlowReturn
1089 gst_avi_demux_stream_init_push (GstAviDemux * avi)
1091 if (gst_adapter_available (avi->adapter) >= 12) {
1094 tmp = gst_adapter_take_buffer (avi->adapter, 12);
1096 GST_DEBUG ("Parsing avi header");
1097 if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), tmp)) {
1098 return GST_FLOW_ERROR;
1100 GST_DEBUG ("header ok");
1103 avi->state = GST_AVI_DEMUX_HEADER;
1111 static GstFlowReturn
1112 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
1115 GstBuffer *buf = NULL;
1117 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
1118 if (res != GST_FLOW_OK)
1120 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
1130 GST_DEBUG_OBJECT (avi, "error parsing file header");
1131 return GST_FLOW_ERROR;
1135 /* AVI header handling */
1137 * gst_avi_demux_parse_avih:
1138 * @avi: caller element (used for errors/debug).
1139 * @buf: input data to be used for parsing.
1140 * @avih: pointer to structure (filled in by function) containing
1141 * stream information (such as flags, number of streams, etc.).
1143 * Read 'avih' header. Discards buffer after use.
1145 * Returns: TRUE on success, FALSE otherwise. Throws an error if
1146 * the header is invalid. The caller should error out
1150 gst_avi_demux_parse_avih (GstAviDemux * avi,
1151 GstBuffer * buf, gst_riff_avih ** _avih)
1153 gst_riff_avih *avih;
1158 if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_avih))
1159 goto avih_too_small;
1161 avih = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1163 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1164 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
1165 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
1166 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
1167 avih->flags = GUINT32_FROM_LE (avih->flags);
1168 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
1169 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
1170 avih->streams = GUINT32_FROM_LE (avih->streams);
1171 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
1172 avih->width = GUINT32_FROM_LE (avih->width);
1173 avih->height = GUINT32_FROM_LE (avih->height);
1174 avih->scale = GUINT32_FROM_LE (avih->scale);
1175 avih->rate = GUINT32_FROM_LE (avih->rate);
1176 avih->start = GUINT32_FROM_LE (avih->start);
1177 avih->length = GUINT32_FROM_LE (avih->length);
1181 GST_INFO_OBJECT (avi, "avih tag found:");
1182 GST_INFO_OBJECT (avi, " us_frame %u", avih->us_frame);
1183 GST_INFO_OBJECT (avi, " max_bps %u", avih->max_bps);
1184 GST_INFO_OBJECT (avi, " pad_gran %u", avih->pad_gran);
1185 GST_INFO_OBJECT (avi, " flags 0x%08x", avih->flags);
1186 GST_INFO_OBJECT (avi, " tot_frames %u", avih->tot_frames);
1187 GST_INFO_OBJECT (avi, " init_frames %u", avih->init_frames);
1188 GST_INFO_OBJECT (avi, " streams %u", avih->streams);
1189 GST_INFO_OBJECT (avi, " bufsize %u", avih->bufsize);
1190 GST_INFO_OBJECT (avi, " width %u", avih->width);
1191 GST_INFO_OBJECT (avi, " height %u", avih->height);
1192 GST_INFO_OBJECT (avi, " scale %u", avih->scale);
1193 GST_INFO_OBJECT (avi, " rate %u", avih->rate);
1194 GST_INFO_OBJECT (avi, " start %u", avih->start);
1195 GST_INFO_OBJECT (avi, " length %u", avih->length);
1198 gst_buffer_unref (buf);
1200 if (avih->us_frame != 0 && avih->tot_frames != 0)
1202 (guint64) avih->us_frame * (guint64) avih->tot_frames * 1000;
1204 avi->duration = GST_CLOCK_TIME_NONE;
1206 GST_INFO_OBJECT (avi, " header duration %" GST_TIME_FORMAT,
1207 GST_TIME_ARGS (avi->duration));
1214 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No buffer"));
1219 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
1220 ("Too small avih (%d available, %d needed)",
1221 GST_BUFFER_SIZE (buf), (int) sizeof (gst_riff_avih)));
1222 gst_buffer_unref (buf);
1228 * gst_avi_demux_parse_superindex:
1229 * @avi: caller element (used for debugging/errors).
1230 * @buf: input data to use for parsing.
1231 * @locations: locations in the file (byte-offsets) that contain
1232 * the actual indexes (see get_avi_demux_parse_subindex()).
1233 * The array ends with GST_BUFFER_OFFSET_NONE.
1235 * Reads superindex (openDML-2 spec stuff) from the provided data.
1237 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
1238 * on error, but they are not fatal.
1241 gst_avi_demux_parse_superindex (GstAviDemux * avi,
1242 GstBuffer * buf, guint64 ** _indexes)
1252 size = buf ? GST_BUFFER_SIZE (buf) : 0;
1256 data = GST_BUFFER_DATA (buf);
1258 /* check type of index. The opendml2 specs state that
1259 * there should be 4 dwords per array entry. Type can be
1260 * either frame or field (and we don't care). */
1261 if (GST_READ_UINT16_LE (data) != 4 ||
1262 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
1263 GST_WARNING_OBJECT (avi,
1264 "Superindex for stream has unexpected "
1265 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1266 GST_READ_UINT16_LE (data), data[2], data[3]);
1267 bpe = GST_READ_UINT16_LE (data) * 4;
1269 num = GST_READ_UINT32_LE (&data[4]);
1271 GST_DEBUG_OBJECT (avi, "got %d indexes", num);
1273 indexes = g_new (guint64, num + 1);
1274 for (i = 0; i < num; i++) {
1275 if (size < 24 + bpe * (i + 1))
1277 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
1278 GST_DEBUG_OBJECT (avi, "index %d at %" G_GUINT64_FORMAT, i, indexes[i]);
1280 indexes[i] = GST_BUFFER_OFFSET_NONE;
1281 *_indexes = indexes;
1283 gst_buffer_unref (buf);
1290 GST_ERROR_OBJECT (avi,
1291 "Not enough data to parse superindex (%d available, 24 needed)", size);
1293 gst_buffer_unref (buf);
1298 /* add an entry to the index of a stream. @num should be an estimate of the
1299 * total amount of index entries for all streams and is used to dynamically
1300 * allocate memory for the index entries. */
1301 static inline gboolean
1302 gst_avi_demux_add_index (GstAviDemux * avi, GstAviStream * stream,
1303 guint num, GstAviIndexEntry * entry)
1305 /* ensure index memory */
1306 if (G_UNLIKELY (stream->idx_n >= stream->idx_max)) {
1307 guint idx_max = stream->idx_max;
1308 GstAviIndexEntry *new_idx;
1310 /* we need to make some more room */
1312 /* initial size guess, assume each stream has an equal amount of entries,
1313 * overshoot with at least 8K */
1314 idx_max = (num / avi->num_streams) + (8192 / sizeof (GstAviIndexEntry));
1316 idx_max += 8192 / sizeof (GstAviIndexEntry);
1317 GST_DEBUG_OBJECT (avi, "expanded index from %u to %u",
1318 stream->idx_max, idx_max);
1320 new_idx = g_try_renew (GstAviIndexEntry, stream->index, idx_max);
1321 /* out of memory, if this fails stream->index is untouched. */
1322 if (G_UNLIKELY (!new_idx))
1325 stream->index = new_idx;
1326 stream->idx_max = idx_max;
1329 /* update entry total and stream stats. The entry total can be converted to
1330 * the timestamp of the entry easily. */
1331 if (stream->strh->type == GST_RIFF_FCC_auds) {
1334 if (stream->is_vbr) {
1335 entry->total = stream->total_blocks;
1337 entry->total = stream->total_bytes;
1339 blockalign = stream->strf.auds->blockalign;
1341 stream->total_blocks += DIV_ROUND_UP (entry->size, blockalign);
1343 stream->total_blocks++;
1345 if (stream->is_vbr) {
1346 entry->total = stream->idx_n;
1348 entry->total = stream->total_bytes;
1351 stream->total_bytes += entry->size;
1352 if (ENTRY_IS_KEYFRAME (entry))
1353 stream->n_keyframes++;
1356 GST_LOG_OBJECT (avi,
1357 "Adding stream %u, index entry %d, kf %d, size %u "
1358 ", offset %" G_GUINT64_FORMAT ", total %" G_GUINT64_FORMAT, stream->num,
1359 stream->idx_n, ENTRY_IS_KEYFRAME (entry), entry->size, entry->offset,
1361 stream->index[stream->idx_n++] = *entry;
1366 /* given @entry_n in @stream, calculate info such as timestamps and
1367 * offsets for the entry. */
1369 gst_avi_demux_get_buffer_info (GstAviDemux * avi, GstAviStream * stream,
1370 guint entry_n, GstClockTime * timestamp, GstClockTime * ts_end,
1371 guint64 * offset, guint64 * offset_end)
1373 GstAviIndexEntry *entry;
1375 entry = &stream->index[entry_n];
1377 if (stream->is_vbr) {
1378 /* VBR stream next timestamp */
1379 if (stream->strh->type == GST_RIFF_FCC_auds) {
1382 avi_stream_convert_frames_to_time_unchecked (stream, entry->total);
1384 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1389 avi_stream_convert_frames_to_time_unchecked (stream, entry_n);
1391 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1394 } else if (stream->strh->type == GST_RIFF_FCC_auds) {
1395 /* constant rate stream */
1398 avi_stream_convert_bytes_to_time_unchecked (stream, entry->total);
1400 *ts_end = avi_stream_convert_bytes_to_time_unchecked (stream,
1401 entry->total + entry->size);
1403 if (stream->strh->type == GST_RIFF_FCC_vids) {
1404 /* video offsets are the frame number */
1408 *offset_end = entry_n + 1;
1410 /* no offsets for audio */
1418 /* collect and debug stats about the indexes for all streams.
1419 * This method is also responsible for filling in the stream duration
1420 * as measured by the amount of index entries.
1422 * Returns TRUE if the index is not empty, else FALSE */
1424 gst_avi_demux_do_index_stats (GstAviDemux * avi)
1426 guint total_idx = 0;
1428 #ifndef GST_DISABLE_GST_DEBUG
1429 guint total_max = 0;
1432 /* get stream stats now */
1433 for (i = 0; i < avi->num_streams; i++) {
1434 GstAviStream *stream;
1436 if (G_UNLIKELY (!(stream = &avi->stream[i])))
1438 if (G_UNLIKELY (!stream->strh))
1440 if (G_UNLIKELY (!stream->index || stream->idx_n == 0))
1443 /* we interested in the end_ts of the last entry, which is the total
1444 * duration of this stream */
1445 gst_avi_demux_get_buffer_info (avi, stream, stream->idx_n - 1,
1446 NULL, &stream->idx_duration, NULL, NULL);
1448 total_idx += stream->idx_n;
1449 #ifndef GST_DISABLE_GST_DEBUG
1450 total_max += stream->idx_max;
1452 GST_INFO_OBJECT (avi, "Stream %d, dur %" GST_TIME_FORMAT ", %6u entries, "
1453 "%5u keyframes, entry size = %2u, total size = %10u, allocated %10u",
1454 i, GST_TIME_ARGS (stream->idx_duration), stream->idx_n,
1455 stream->n_keyframes, (guint) sizeof (GstAviIndexEntry),
1456 (guint) (stream->idx_n * sizeof (GstAviIndexEntry)),
1457 (guint) (stream->idx_max * sizeof (GstAviIndexEntry)));
1459 total_idx *= sizeof (GstAviIndexEntry);
1460 #ifndef GST_DISABLE_GST_DEBUG
1461 total_max *= sizeof (GstAviIndexEntry);
1463 GST_INFO_OBJECT (avi, "%u bytes for index vs %u ideally, %u wasted",
1464 total_max, total_idx, total_max - total_idx);
1466 if (total_idx == 0) {
1467 GST_WARNING_OBJECT (avi, "Index is empty !");
1474 * gst_avi_demux_parse_subindex:
1476 * @buf: input data to use for parsing.
1477 * @stream: stream context.
1478 * @entries_list: a list (returned by the function) containing all the
1479 * indexes parsed in this specific subindex. The first
1480 * entry is also a pointer to allocated memory that needs
1481 * to be free´ed. May be NULL if no supported indexes were
1484 * Reads superindex (openDML-2 spec stuff) from the provided data.
1485 * The buffer should contain a GST_RIFF_TAG_ix?? chunk.
1487 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1488 * throw an error, caller should bail out asap.
1491 gst_avi_demux_parse_subindex (GstAviDemux * avi, GstAviStream * stream,
1503 size = GST_BUFFER_SIZE (buf);
1509 data = GST_BUFFER_DATA (buf);
1511 /* We don't support index-data yet */
1513 goto not_implemented;
1515 /* check type of index. The opendml2 specs state that
1516 * there should be 4 dwords per array entry. Type can be
1517 * either frame or field (and we don't care). */
1518 bpe = (data[2] & 0x01) ? 12 : 8;
1519 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1520 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1521 GST_WARNING_OBJECT (avi,
1522 "Superindex for stream %d has unexpected "
1523 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1524 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1525 bpe = GST_READ_UINT16_LE (data) * 4;
1527 num = GST_READ_UINT32_LE (&data[4]);
1528 baseoff = GST_READ_UINT64_LE (&data[12]);
1530 /* If there's nothing, just return ! */
1534 GST_INFO_OBJECT (avi, "Parsing subindex, nr_entries = %6d", num);
1536 for (i = 0; i < num; i++) {
1537 GstAviIndexEntry entry;
1539 if (size < 24 + bpe * (i + 1))
1542 /* fill in offset and size. offset contains the keyframe flag in the
1544 entry.offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1545 entry.size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1547 if (stream->strh->type == GST_RIFF_FCC_auds) {
1548 /* all audio frames are keyframes */
1549 ENTRY_SET_KEYFRAME (&entry);
1551 /* else read flags */
1552 entry.flags = (entry.size & 0x80000000) ? 0 : GST_AVI_KEYFRAME;
1554 entry.size &= ~0x80000000;
1557 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
1560 gst_buffer_unref (buf);
1567 GST_ERROR_OBJECT (avi,
1568 "Not enough data to parse subindex (%d available, 24 needed)", size);
1569 gst_buffer_unref (buf);
1570 return TRUE; /* continue */
1574 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1575 ("Subindex-is-data is not implemented"));
1576 gst_buffer_unref (buf);
1581 GST_DEBUG_OBJECT (avi, "the index is empty");
1582 gst_buffer_unref (buf);
1587 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1588 ("Cannot allocate memory for %u*%u=%u bytes",
1589 (guint) sizeof (GstAviIndexEntry), num,
1590 (guint) sizeof (GstAviIndexEntry) * num));
1591 gst_buffer_unref (buf);
1597 * Create and push a flushing seek event upstream
1600 perform_seek_to_offset (GstAviDemux * demux, guint64 offset)
1605 GST_DEBUG_OBJECT (demux, "Seeking to %" G_GUINT64_FORMAT, offset);
1608 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1609 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1610 GST_SEEK_TYPE_NONE, -1);
1612 res = gst_pad_push_event (demux->sinkpad, event);
1615 demux->offset = offset;
1620 * Read AVI index when streaming
1623 gst_avi_demux_read_subindexes_push (GstAviDemux * avi)
1625 guint32 tag = 0, size;
1626 GstBuffer *buf = NULL;
1629 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1631 if (avi->odml_subidxs[avi->odml_subidx] != avi->offset)
1634 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1637 /* this is the ODML chunk we expect */
1638 odml_stream = avi->odml_stream;
1640 if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + odml_stream / 10,
1641 '0' + odml_stream % 10)) &&
1642 (tag != GST_MAKE_FOURCC ('0' + odml_stream / 10,
1643 '0' + odml_stream % 10, 'i', 'x'))) {
1644 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1645 GST_FOURCC_ARGS (tag));
1649 avi->offset += 8 + GST_ROUND_UP_2 (size);
1650 /* flush chunk header so we get just the 'size' payload data */
1651 gst_adapter_flush (avi->adapter, 8);
1652 buf = gst_adapter_take_buffer (avi->adapter, size);
1654 if (!gst_avi_demux_parse_subindex (avi, &avi->stream[odml_stream], buf))
1657 /* we parsed the index, go to next subindex */
1660 if (avi->odml_subidxs[avi->odml_subidx] == GST_BUFFER_OFFSET_NONE) {
1661 /* we reached the end of the indexes for this stream, move to the next
1662 * stream to handle the first index */
1664 avi->odml_subidx = 0;
1666 if (avi->odml_stream < avi->num_streams) {
1667 /* there are more indexes */
1668 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
1670 /* we're done, get stream stats now */
1671 avi->have_index = gst_avi_demux_do_index_stats (avi);
1677 /* seek to next index */
1678 return perform_seek_to_offset (avi, avi->odml_subidxs[avi->odml_subidx]);
1685 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi)
1691 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1693 for (n = 0; n < avi->num_streams; n++) {
1694 GstAviStream *stream = &avi->stream[n];
1696 if (stream->indexes == NULL)
1699 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1700 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi), avi->sinkpad,
1701 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1703 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1704 '0' + stream->num % 10)) &&
1705 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1706 '0' + stream->num % 10, 'i', 'x'))) {
1707 /* Some ODML files (created by god knows what muxer) have a ##ix format
1708 * instead of the 'official' ix##. They are still valid though. */
1709 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1710 GST_FOURCC_ARGS (tag));
1711 gst_buffer_unref (buf);
1715 if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1719 g_free (stream->indexes);
1720 stream->indexes = NULL;
1722 /* get stream stats now */
1723 avi->have_index = gst_avi_demux_do_index_stats (avi);
1727 * gst_avi_demux_riff_parse_vprp:
1728 * @element: caller element (used for debugging/error).
1729 * @buf: input data to be used for parsing, stripped from header.
1730 * @vprp: a pointer (returned by this function) to a filled-in vprp
1731 * structure. Caller should free it.
1733 * Parses a video stream´s vprp. This function takes ownership of @buf.
1735 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1736 * should be skipped on error, but it is not fatal.
1739 gst_avi_demux_riff_parse_vprp (GstElement * element,
1740 GstBuffer * buf, gst_riff_vprp ** _vprp)
1742 gst_riff_vprp *vprp;
1745 g_return_val_if_fail (buf != NULL, FALSE);
1746 g_return_val_if_fail (_vprp != NULL, FALSE);
1748 if (GST_BUFFER_SIZE (buf) < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1751 vprp = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1753 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1754 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1755 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1756 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1757 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1758 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1759 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1760 vprp->width = GUINT32_FROM_LE (vprp->width);
1761 vprp->height = GUINT32_FROM_LE (vprp->height);
1762 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1766 /* calculate fields based on size */
1767 k = (GST_BUFFER_SIZE (buf) - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) /
1769 if (vprp->fields > k) {
1770 GST_WARNING_OBJECT (element,
1771 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1774 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1775 GST_WARNING_OBJECT (element,
1776 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1777 GST_RIFF_VPRP_VIDEO_FIELDS);
1778 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1780 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1781 for (k = 0; k < vprp->fields; k++) {
1782 gst_riff_vprp_video_field_desc *fd;
1784 fd = &vprp->field_info[k];
1785 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1786 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1787 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1788 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1789 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1790 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1791 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1792 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1797 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1798 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1799 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1800 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1801 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1802 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1803 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1804 vprp->aspect & 0xffff);
1805 GST_INFO_OBJECT (element, " width %d", vprp->width);
1806 GST_INFO_OBJECT (element, " height %d", vprp->height);
1807 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1808 for (k = 0; k < vprp->fields; k++) {
1809 gst_riff_vprp_video_field_desc *fd;
1811 fd = &(vprp->field_info[k]);
1812 GST_INFO_OBJECT (element, " field %u description:", k);
1813 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1814 fd->compressed_bm_height);
1815 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1816 fd->compressed_bm_width);
1817 GST_INFO_OBJECT (element, " valid_bm_height %d",
1818 fd->valid_bm_height);
1819 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1820 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1821 fd->valid_bm_x_offset);
1822 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1823 fd->valid_bm_y_offset);
1824 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1825 fd->video_x_t_offset);
1826 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1829 gst_buffer_unref (buf);
1838 GST_ERROR_OBJECT (element,
1839 "Too small vprp (%d available, at least %d needed)",
1840 GST_BUFFER_SIZE (buf),
1841 (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1842 gst_buffer_unref (buf);
1848 gst_avi_demux_expose_streams (GstAviDemux * avi, gboolean force)
1852 GST_DEBUG_OBJECT (avi, "force : %d", force);
1854 for (i = 0; i < avi->num_streams; i++) {
1855 GstAviStream *stream = &avi->stream[i];
1857 if (force || stream->idx_n != 0) {
1858 GST_LOG_OBJECT (avi, "Added pad %s with caps %" GST_PTR_FORMAT,
1859 GST_PAD_NAME (stream->pad), GST_PAD_CAPS (stream->pad));
1860 gst_element_add_pad ((GstElement *) avi, stream->pad);
1861 stream->exposed = TRUE;
1862 if (avi->main_stream == -1)
1863 avi->main_stream = i;
1865 GST_WARNING_OBJECT (avi, "Stream #%d doesn't have any entry, removing it",
1867 gst_avi_demux_reset_stream (avi, stream);
1873 * gst_avi_demux_parse_stream:
1874 * @avi: calling element (used for debugging/errors).
1875 * @buf: input buffer used to parse the stream.
1877 * Parses all subchunks in a strl chunk (which defines a single
1878 * stream). Discards the buffer after use. This function will
1879 * increment the stream counter internally.
1881 * Returns: whether the stream was identified successfully.
1882 * Errors are not fatal. It does indicate the stream
1886 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
1888 GstAviStream *stream;
1889 GstElementClass *klass;
1890 GstPadTemplate *templ;
1891 GstBuffer *sub = NULL;
1894 gchar *codec_name = NULL, *padname = NULL;
1895 const gchar *tag_name;
1896 GstCaps *caps = NULL;
1898 GstElement *element;
1899 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
1900 gst_riff_vprp *vprp = NULL;
1902 element = GST_ELEMENT_CAST (avi);
1904 GST_DEBUG_OBJECT (avi, "Parsing stream");
1906 if (avi->num_streams >= GST_AVI_DEMUX_MAX_STREAMS) {
1907 GST_WARNING_OBJECT (avi,
1908 "maximum no of streams (%d) exceeded, ignoring stream",
1909 GST_AVI_DEMUX_MAX_STREAMS);
1910 gst_buffer_unref (buf);
1911 /* not a fatal error, let's say */
1915 stream = &avi->stream[avi->num_streams];
1917 /* initial settings */
1918 stream->idx_duration = GST_CLOCK_TIME_NONE;
1919 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1920 stream->duration = GST_CLOCK_TIME_NONE;
1922 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
1923 /* sub can be NULL if the chunk is empty */
1925 GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
1926 GST_FOURCC_ARGS (tag));
1930 case GST_RIFF_TAG_strh:
1932 gst_riff_strh *strh;
1935 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
1938 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
1939 /* ownership given away */
1941 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
1945 strh = stream->strh;
1946 /* sanity check; stream header frame rate matches global header
1948 if (stream->strh->type == GST_RIFF_FCC_vids) {
1950 GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
1952 s_dur = gst_util_uint64_scale (GST_SECOND, strh->scale, strh->rate);
1953 GST_DEBUG_OBJECT (avi, "verifying stream framerate %d/%d, "
1954 "frame duration = %d ms", strh->rate, strh->scale,
1955 (gint) (s_dur / GST_MSECOND));
1956 if (h_dur > (10 * GST_MSECOND) && (s_dur > 10 * h_dur)) {
1957 strh->rate = GST_SECOND / GST_USECOND;
1958 strh->scale = h_dur / GST_USECOND;
1959 GST_DEBUG_OBJECT (avi, "correcting stream framerate to %d/%d",
1960 strh->rate, strh->scale);
1963 /* determine duration as indicated by header */
1964 stream->hdr_duration = gst_util_uint64_scale ((guint64) strh->length *
1965 strh->scale, GST_SECOND, (guint64) strh->rate);
1966 GST_INFO ("Stream duration according to header: %" GST_TIME_FORMAT,
1967 GST_TIME_ARGS (stream->hdr_duration));
1968 if (stream->hdr_duration == 0)
1969 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1974 case GST_RIFF_TAG_strf:
1976 gboolean res = FALSE;
1979 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
1983 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
1986 switch (stream->strh->type) {
1987 case GST_RIFF_FCC_vids:
1988 stream->is_vbr = TRUE;
1989 res = gst_riff_parse_strf_vids (element, sub,
1990 &stream->strf.vids, &stream->extradata);
1992 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
1994 case GST_RIFF_FCC_auds:
1996 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
1997 &stream->extradata);
1998 stream->is_vbr = (stream->strh->samplesize == 0)
1999 && stream->strh->scale > 1
2000 && stream->strf.auds->blockalign != 1;
2002 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
2003 stream->is_vbr, res);
2004 /* we need these or we have no way to come up with timestamps */
2005 if ((!stream->is_vbr && !stream->strf.auds->av_bps) ||
2006 (stream->is_vbr && (!stream->strh->scale ||
2007 !stream->strh->rate))) {
2008 GST_WARNING_OBJECT (element,
2009 "invalid audio header, ignoring stream");
2012 /* some more sanity checks */
2013 if (stream->is_vbr) {
2014 if (stream->strf.auds->blockalign <= 4) {
2015 /* that would mean (too) many frames per chunk,
2016 * so not likely set as expected */
2017 GST_DEBUG_OBJECT (element,
2018 "suspicious blockalign %d for VBR audio; "
2019 "overriding to 1 frame per chunk",
2020 stream->strf.auds->blockalign);
2021 /* this should top any likely value */
2022 stream->strf.auds->blockalign = (1 << 12);
2026 case GST_RIFF_FCC_iavs:
2027 stream->is_vbr = TRUE;
2028 res = gst_riff_parse_strf_iavs (element, sub,
2029 &stream->strf.iavs, &stream->extradata);
2031 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
2033 case GST_RIFF_FCC_txts:
2034 /* nothing to parse here */
2035 stream->is_vbr = (stream->strh->samplesize == 0)
2036 && (stream->strh->scale > 1);
2040 GST_ERROR_OBJECT (avi,
2041 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
2042 GST_FOURCC_ARGS (stream->strh->type));
2046 gst_buffer_unref (sub);
2054 case GST_RIFF_TAG_vprp:
2057 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
2061 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
2065 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
2069 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
2070 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
2071 /* not considered fatal */
2079 case GST_RIFF_TAG_strd:
2080 if (stream->initdata)
2081 gst_buffer_unref (stream->initdata);
2082 stream->initdata = sub;
2085 case GST_RIFF_TAG_strn:
2086 g_free (stream->name);
2089 g_strndup ((gchar *) GST_BUFFER_DATA (sub),
2090 (gsize) GST_BUFFER_SIZE (sub));
2091 gst_buffer_unref (sub);
2094 stream->name = g_strdup ("");
2096 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
2099 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
2100 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
2101 '0' + avi->num_streams % 10)) {
2102 g_free (stream->indexes);
2103 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
2104 stream->superindex = TRUE;
2108 GST_WARNING_OBJECT (avi,
2109 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
2110 GST_FOURCC_ARGS (tag));
2112 case GST_RIFF_TAG_JUNQ:
2113 case GST_RIFF_TAG_JUNK:
2117 gst_buffer_unref (sub);
2123 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
2128 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
2132 /* get class to figure out the template */
2133 klass = GST_ELEMENT_GET_CLASS (avi);
2135 /* we now have all info, let´s set up a pad and a caps and be done */
2136 /* create stream name + pad */
2137 switch (stream->strh->type) {
2138 case GST_RIFF_FCC_vids:{
2141 fourcc = (stream->strf.vids->compression) ?
2142 stream->strf.vids->compression : stream->strh->fcc_handler;
2143 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
2144 templ = gst_element_class_get_pad_template (klass, "video_%02d");
2145 caps = gst_riff_create_video_caps (fourcc, stream->strh,
2146 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
2148 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2149 GST_TYPE_FOURCC, fourcc, NULL);
2150 } else if (got_vprp && vprp) {
2151 guint32 aspect_n, aspect_d;
2154 aspect_n = vprp->aspect >> 16;
2155 aspect_d = vprp->aspect & 0xffff;
2156 /* calculate the pixel aspect ratio using w/h and aspect ratio */
2157 n = aspect_n * stream->strf.vids->height;
2158 d = aspect_d * stream->strf.vids->width;
2160 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
2162 /* very local, not needed elsewhere */
2166 tag_name = GST_TAG_VIDEO_CODEC;
2167 avi->num_v_streams++;
2170 case GST_RIFF_FCC_auds:{
2171 padname = g_strdup_printf ("audio_%02d", avi->num_a_streams);
2172 templ = gst_element_class_get_pad_template (klass, "audio_%02d");
2173 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
2174 stream->strh, stream->strf.auds, stream->extradata,
2175 stream->initdata, &codec_name);
2177 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
2178 G_TYPE_INT, stream->strf.auds->format, NULL);
2180 tag_name = GST_TAG_AUDIO_CODEC;
2181 avi->num_a_streams++;
2184 case GST_RIFF_FCC_iavs:{
2185 guint32 fourcc = stream->strh->fcc_handler;
2187 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
2188 templ = gst_element_class_get_pad_template (klass, "video_%02d");
2189 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
2190 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
2192 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2193 GST_TYPE_FOURCC, fourcc, NULL);
2195 tag_name = GST_TAG_VIDEO_CODEC;
2196 avi->num_v_streams++;
2199 case GST_RIFF_FCC_txts:{
2200 padname = g_strdup_printf ("subtitle_%02d", avi->num_t_streams);
2201 templ = gst_element_class_get_pad_template (klass, "subtitle_%02d");
2202 caps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
2204 avi->num_t_streams++;
2208 g_assert_not_reached ();
2211 /* no caps means no stream */
2213 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
2217 GST_DEBUG_OBJECT (element, "codec-name=%s",
2218 (codec_name ? codec_name : "NULL"));
2219 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
2221 /* set proper settings and add it */
2223 gst_object_unref (stream->pad);
2224 pad = stream->pad = gst_pad_new_from_template (templ, padname);
2227 gst_pad_use_fixed_caps (pad);
2229 gst_pad_set_formats_function (pad,
2230 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
2231 gst_pad_set_event_mask_function (pad,
2232 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
2234 gst_pad_set_event_function (pad,
2235 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
2236 gst_pad_set_query_type_function (pad,
2237 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_query_types));
2238 gst_pad_set_query_function (pad,
2239 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
2241 gst_pad_set_convert_function (pad,
2242 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
2245 if (avi->element_index)
2246 gst_index_get_writer_id (avi->element_index, GST_OBJECT_CAST (stream->pad),
2249 stream->num = avi->num_streams;
2251 stream->start_entry = 0;
2252 stream->step_entry = 0;
2253 stream->stop_entry = 0;
2255 stream->current_entry = -1;
2256 stream->current_total = 0;
2258 stream->last_flow = GST_FLOW_OK;
2259 stream->discont = TRUE;
2261 stream->total_bytes = 0;
2262 stream->total_blocks = 0;
2263 stream->n_keyframes = 0;
2266 stream->idx_max = 0;
2268 gst_pad_set_element_private (pad, stream);
2271 gst_pad_set_caps (pad, caps);
2272 gst_pad_set_active (pad, TRUE);
2273 gst_caps_unref (caps);
2277 if (!stream->taglist)
2278 stream->taglist = gst_tag_list_new ();
2280 avi->got_tags = TRUE;
2282 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
2284 g_free (codec_name);
2287 gst_buffer_unref (buf);
2294 /* unref any mem that may be in use */
2296 gst_buffer_unref (buf);
2298 gst_buffer_unref (sub);
2300 g_free (codec_name);
2301 gst_avi_demux_reset_stream (avi, stream);
2308 * gst_avi_demux_parse_odml:
2309 * @avi: calling element (used for debug/error).
2310 * @buf: input buffer to be used for parsing.
2312 * Read an openDML-2.0 extension header. Fills in the frame number
2313 * in the avi demuxer object when reading succeeds.
2316 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
2320 GstBuffer *sub = NULL;
2322 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
2325 case GST_RIFF_TAG_dmlh:{
2326 gst_riff_dmlh dmlh, *_dmlh;
2329 /* sub == NULL is possible and means an empty buffer */
2330 size = sub ? GST_BUFFER_SIZE (sub) : 0;
2333 if (size < sizeof (gst_riff_dmlh)) {
2334 GST_ERROR_OBJECT (avi,
2335 "DMLH entry is too small (%d bytes, %d needed)",
2336 size, (int) sizeof (gst_riff_dmlh));
2339 _dmlh = (gst_riff_dmlh *) GST_BUFFER_DATA (sub);
2340 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
2342 GST_INFO_OBJECT (avi, "dmlh tag found: totalframes: %u",
2345 avi->avih->tot_frames = dmlh.totalframes;
2350 GST_WARNING_OBJECT (avi,
2351 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
2352 GST_FOURCC_ARGS (tag));
2354 case GST_RIFF_TAG_JUNQ:
2355 case GST_RIFF_TAG_JUNK:
2357 /* skip and move to next chunk */
2359 gst_buffer_unref (sub);
2366 gst_buffer_unref (buf);
2371 gst_avi_demux_index_last (GstAviDemux * avi, GstAviStream * stream)
2373 return stream->idx_n;
2376 /* find a previous entry in the index with the given flags */
2378 gst_avi_demux_index_prev (GstAviDemux * avi, GstAviStream * stream,
2379 guint last, gboolean keyframe)
2381 GstAviIndexEntry *entry;
2384 for (i = last; i > 0; i--) {
2385 entry = &stream->index[i - 1];
2386 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2394 gst_avi_demux_index_next (GstAviDemux * avi, GstAviStream * stream,
2395 guint last, gboolean keyframe)
2397 GstAviIndexEntry *entry;
2400 for (i = last + 1; i < stream->idx_n; i++) {
2401 entry = &stream->index[i];
2402 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2406 return stream->idx_n - 1;
2410 gst_avi_demux_index_entry_search (GstAviIndexEntry * entry, guint64 * total)
2412 if (entry->total < *total)
2414 else if (entry->total > *total)
2420 * gst_avi_demux_index_for_time:
2422 * @stream: the stream
2423 * @time: a time position
2425 * Finds the index entry which time is less or equal than the requested time.
2426 * Try to avoid binary search when we can convert the time to an index
2427 * position directly (for example for video frames with a fixed duration).
2429 * Returns: the found position in the index.
2432 gst_avi_demux_index_for_time (GstAviDemux * avi,
2433 GstAviStream * stream, guint64 time)
2438 GST_LOG_OBJECT (avi, "search time:%" GST_TIME_FORMAT, GST_TIME_ARGS (time));
2440 /* easy (and common) cases */
2441 if (time == 0 || stream->idx_n == 0)
2443 if (time >= stream->idx_duration)
2444 return stream->idx_n - 1;
2446 /* figure out where we need to go. For that we convert the time to an
2447 * index entry or we convert it to a total and then do a binary search. */
2448 if (stream->is_vbr) {
2449 /* VBR stream next timestamp */
2450 if (stream->strh->type == GST_RIFF_FCC_auds) {
2451 total = avi_stream_convert_time_to_frames_unchecked (stream, time);
2453 index = avi_stream_convert_time_to_frames_unchecked (stream, time);
2456 /* constant rate stream */
2457 total = avi_stream_convert_time_to_bytes_unchecked (stream, time);
2461 GstAviIndexEntry *entry;
2463 /* no index, find index with binary search on total */
2464 GST_LOG_OBJECT (avi, "binary search for entry with total %"
2465 G_GUINT64_FORMAT, total);
2467 entry = gst_util_array_binary_search (stream->index,
2468 stream->idx_n, sizeof (GstAviIndexEntry),
2469 (GCompareDataFunc) gst_avi_demux_index_entry_search,
2470 GST_SEARCH_MODE_BEFORE, &total, NULL);
2472 if (entry == NULL) {
2473 GST_LOG_OBJECT (avi, "not found, assume index 0");
2476 index = entry - stream->index;
2477 GST_LOG_OBJECT (avi, "found at %u", index);
2480 GST_LOG_OBJECT (avi, "converted time to index %u", index);
2486 static inline GstAviStream *
2487 gst_avi_demux_stream_for_id (GstAviDemux * avi, guint32 id)
2490 GstAviStream *stream;
2492 /* get the stream for this entry */
2493 stream_nr = CHUNKID_TO_STREAMNR (id);
2494 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2495 GST_WARNING_OBJECT (avi, "invalid stream nr %d", stream_nr);
2498 stream = &avi->stream[stream_nr];
2499 if (G_UNLIKELY (!stream->strh)) {
2500 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2507 * gst_avi_demux_parse_index:
2508 * @avi: calling element (used for debugging/errors).
2509 * @buf: buffer containing the full index.
2511 * Read index entries from the provided buffer.
2512 * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
2515 gst_avi_demux_parse_index (GstAviDemux * avi, GstBuffer * buf)
2521 gst_riff_index_entry *index;
2523 GstAviStream *stream;
2524 GstAviIndexEntry entry;
2530 data = GST_BUFFER_DATA (buf);
2531 size = GST_BUFFER_SIZE (buf);
2533 stamp = gst_util_get_timestamp ();
2535 /* see how many items in the index */
2536 num = size / sizeof (gst_riff_index_entry);
2540 GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2542 index = (gst_riff_index_entry *) data;
2543 pos_before = avi->offset;
2545 /* figure out if the index is 0 based or relative to the MOVI start */
2546 entry.offset = GST_READ_UINT32_LE (&index[0].offset);
2547 if (entry.offset < avi->offset) {
2548 avi->index_offset = avi->offset + 8;
2549 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2551 avi->index_offset = 0;
2552 GST_DEBUG ("index is 0 based");
2555 for (i = 0, n = 0; i < num; i++) {
2556 id = GST_READ_UINT32_LE (&index[i].id);
2557 entry.offset = GST_READ_UINT32_LE (&index[i].offset);
2559 /* some sanity checks */
2560 if (G_UNLIKELY (id == GST_RIFF_rec || id == 0 ||
2561 (entry.offset == 0 && n > 0)))
2564 /* get the stream for this entry */
2565 stream = gst_avi_demux_stream_for_id (avi, id);
2566 if (G_UNLIKELY (!stream))
2569 /* handle offset and size */
2570 entry.offset += avi->index_offset + 8;
2571 entry.size = GST_READ_UINT32_LE (&index[i].size);
2574 if (stream->strh->type == GST_RIFF_FCC_auds) {
2575 /* all audio frames are keyframes */
2576 ENTRY_SET_KEYFRAME (&entry);
2579 /* else read flags */
2580 flags = GST_READ_UINT32_LE (&index[i].flags);
2581 if (flags & GST_RIFF_IF_KEYFRAME) {
2582 ENTRY_SET_KEYFRAME (&entry);
2584 ENTRY_UNSET_KEYFRAME (&entry);
2589 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2594 gst_buffer_unref (buf);
2596 /* get stream stats now */
2597 avi->have_index = gst_avi_demux_do_index_stats (avi);
2599 stamp = gst_util_get_timestamp () - stamp;
2600 GST_DEBUG_OBJECT (avi, "index parsing took %" GST_TIME_FORMAT,
2601 GST_TIME_ARGS (stamp));
2608 GST_DEBUG_OBJECT (avi, "empty index");
2609 gst_buffer_unref (buf);
2614 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2615 ("Cannot allocate memory for %u*%u=%u bytes",
2616 (guint) sizeof (GstAviIndexEntry), num,
2617 (guint) sizeof (GstAviIndexEntry) * num));
2618 gst_buffer_unref (buf);
2624 * gst_avi_demux_stream_index:
2625 * @avi: avi demuxer object.
2627 * Seeks to index and reads it.
2630 gst_avi_demux_stream_index (GstAviDemux * avi)
2633 guint64 offset = avi->offset;
2638 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2640 /* get chunk information */
2641 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2642 if (res != GST_FLOW_OK)
2644 else if (GST_BUFFER_SIZE (buf) < 8)
2647 /* check tag first before blindy trying to read 'size' bytes */
2648 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2649 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2650 if (tag == GST_RIFF_TAG_LIST) {
2651 /* this is the movi tag */
2652 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2653 (8 + GST_ROUND_UP_2 (size)));
2654 offset += 8 + GST_ROUND_UP_2 (size);
2655 gst_buffer_unref (buf);
2656 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2657 if (res != GST_FLOW_OK)
2659 else if (GST_BUFFER_SIZE (buf) < 8)
2661 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2662 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2665 if (tag != GST_RIFF_TAG_idx1)
2670 gst_buffer_unref (buf);
2672 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2674 /* read chunk, advance offset */
2675 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2676 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2679 GST_DEBUG ("will parse index chunk size %u for tag %"
2680 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2682 gst_avi_demux_parse_index (avi, buf);
2684 #ifndef GST_DISABLE_GST_DEBUG
2685 /* debug our indexes */
2688 GstAviStream *stream;
2690 for (i = 0; i < avi->num_streams; i++) {
2691 stream = &avi->stream[i];
2692 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2693 i, stream->idx_n, stream->total_bytes);
2702 GST_DEBUG_OBJECT (avi,
2703 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2708 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2709 gst_buffer_unref (buf);
2714 GST_WARNING_OBJECT (avi,
2715 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2716 GST_FOURCC_ARGS (tag));
2717 gst_buffer_unref (buf);
2722 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2723 gst_buffer_unref (buf);
2729 * gst_avi_demux_stream_index_push:
2730 * @avi: avi demuxer object.
2735 gst_avi_demux_stream_index_push (GstAviDemux * avi)
2737 guint64 offset = avi->idx1_offset;
2742 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2744 /* get chunk information */
2745 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
2748 /* check tag first before blindly trying to read 'size' bytes */
2749 if (tag == GST_RIFF_TAG_LIST) {
2750 /* this is the movi tag */
2751 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2752 (8 + GST_ROUND_UP_2 (size)));
2753 avi->idx1_offset = offset + 8 + GST_ROUND_UP_2 (size);
2754 /* issue seek to allow chain function to handle it and return! */
2755 perform_seek_to_offset (avi, avi->idx1_offset);
2759 if (tag != GST_RIFF_TAG_idx1)
2762 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2764 /* flush chunk header */
2765 gst_adapter_flush (avi->adapter, 8);
2766 /* read chunk payload */
2767 buf = gst_adapter_take_buffer (avi->adapter, size);
2770 /* advance offset */
2771 offset += 8 + GST_ROUND_UP_2 (size);
2773 GST_DEBUG ("will parse index chunk size %u for tag %"
2774 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2776 avi->offset = avi->first_movi_offset;
2777 gst_avi_demux_parse_index (avi, buf);
2779 #ifndef GST_DISABLE_GST_DEBUG
2780 /* debug our indexes */
2783 GstAviStream *stream;
2785 for (i = 0; i < avi->num_streams; i++) {
2786 stream = &avi->stream[i];
2787 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2788 i, stream->idx_n, stream->total_bytes);
2797 GST_DEBUG_OBJECT (avi,
2798 "taking data from adapter failed: pos=%" G_GUINT64_FORMAT " size=%u",
2804 GST_WARNING_OBJECT (avi,
2805 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2806 GST_FOURCC_ARGS (tag));
2812 * gst_avi_demux_peek_tag:
2814 * Returns the tag and size of the next chunk
2816 static GstFlowReturn
2817 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2820 GstFlowReturn res = GST_FLOW_OK;
2821 GstBuffer *buf = NULL;
2825 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2826 if (res != GST_FLOW_OK)
2829 bufsize = GST_BUFFER_SIZE (buf);
2833 bufdata = GST_BUFFER_DATA (buf);
2835 *tag = GST_READ_UINT32_LE (bufdata);
2836 *size = GST_READ_UINT32_LE (bufdata + 4);
2838 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2839 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2840 *size, offset + 8, offset + 8 + (gint64) * size);
2843 gst_buffer_unref (buf);
2850 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2855 GST_DEBUG_OBJECT (avi, "got %d bytes which is <> 8 bytes", bufsize);
2856 res = GST_FLOW_ERROR;
2862 * gst_avi_demux_next_data_buffer:
2864 * Returns the offset and size of the next buffer
2865 * Position is the position of the buffer (after tag and size)
2867 static GstFlowReturn
2868 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2869 guint32 * tag, guint * size)
2871 guint64 off = *offset;
2876 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2877 if (res != GST_FLOW_OK)
2879 if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
2880 off += 8 + 4; /* skip tag + size + subtag */
2892 * gst_avi_demux_stream_scan:
2893 * @avi: calling element (used for debugging/errors).
2895 * Scan the file for all chunks to "create" a new index.
2899 gst_avi_demux_stream_scan (GstAviDemux * avi)
2902 GstAviStream *stream;
2911 * - implement non-seekable source support.
2913 GST_DEBUG_OBJECT (avi, "Creating index");
2915 /* get the size of the file */
2916 format = GST_FORMAT_BYTES;
2917 if (!gst_pad_query_peer_duration (avi->sinkpad, &format, &tmplength))
2921 /* guess the total amount of entries we expect */
2925 GstAviIndexEntry entry;
2928 /* start reading data buffers to find the id and offset */
2929 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
2930 if (G_UNLIKELY (res != GST_FLOW_OK))
2934 stream = gst_avi_demux_stream_for_id (avi, tag);
2935 if (G_UNLIKELY (!stream))
2938 /* we can't figure out the keyframes, assume they all are */
2939 entry.flags = GST_AVI_KEYFRAME;
2943 /* and add to the index of this stream */
2944 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2948 /* update position */
2949 pos += GST_ROUND_UP_2 (size);
2950 if (G_UNLIKELY (pos > length)) {
2951 GST_WARNING_OBJECT (avi,
2952 "Stopping index lookup since we are further than EOF");
2958 avi->have_index = gst_avi_demux_do_index_stats (avi);
2965 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2966 ("Cannot allocate memory for %u*%u=%u bytes",
2967 (guint) sizeof (GstAviIndexEntry), num,
2968 (guint) sizeof (GstAviIndexEntry) * num));
2974 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
2978 GstAviStream *stream;
2980 total = GST_CLOCK_TIME_NONE;
2982 /* all streams start at a timestamp 0 */
2983 for (i = 0; i < avi->num_streams; i++) {
2984 GstClockTime duration, hduration;
2985 gst_riff_strh *strh;
2987 stream = &avi->stream[i];
2988 if (G_UNLIKELY (!stream || !stream->idx_n || !(strh = stream->strh)))
2991 /* get header duration for the stream */
2992 hduration = stream->hdr_duration;
2993 /* index duration calculated during parsing */
2994 duration = stream->idx_duration;
2996 /* now pick a good duration */
2997 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2998 /* index gave valid duration, use that */
2999 GST_INFO ("Stream %p duration according to index: %" GST_TIME_FORMAT,
3000 stream, GST_TIME_ARGS (duration));
3002 /* fall back to header info to calculate a duration */
3003 duration = hduration;
3005 GST_INFO ("Setting duration of stream #%d to %" GST_TIME_FORMAT,
3006 i, GST_TIME_ARGS (duration));
3007 /* set duration for the stream */
3008 stream->duration = duration;
3010 /* find total duration */
3011 if (total == GST_CLOCK_TIME_NONE ||
3012 (GST_CLOCK_TIME_IS_VALID (duration) && duration > total))
3016 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
3017 /* now update the duration for those streams where we had none */
3018 for (i = 0; i < avi->num_streams; i++) {
3019 stream = &avi->stream[i];
3021 if (!GST_CLOCK_TIME_IS_VALID (stream->duration)
3022 || stream->duration == 0) {
3023 stream->duration = total;
3025 GST_INFO ("Stream %p duration according to total: %" GST_TIME_FORMAT,
3026 stream, GST_TIME_ARGS (total));
3031 /* and set the total duration in the segment. */
3032 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
3033 GST_TIME_ARGS (total));
3035 gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
3038 /* returns FALSE if there are no pads to deliver event to,
3039 * otherwise TRUE (whatever the outcome of event sending),
3040 * takes ownership of the event. */
3042 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
3044 gboolean result = FALSE;
3047 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
3048 GST_EVENT_TYPE_NAME (event), avi->num_streams);
3050 for (i = 0; i < avi->num_streams; i++) {
3051 GstAviStream *stream = &avi->stream[i];
3055 gst_pad_push_event (stream->pad, gst_event_ref (event));
3058 gst_event_unref (event);
3063 gst_avi_demux_check_seekability (GstAviDemux * avi)
3066 gboolean seekable = FALSE;
3067 gint64 start = -1, stop = -1;
3069 query = gst_query_new_seeking (GST_FORMAT_BYTES);
3070 if (!gst_pad_peer_query (avi->sinkpad, query)) {
3071 GST_DEBUG_OBJECT (avi, "seeking query failed");
3075 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
3077 /* try harder to query upstream size if we didn't get it the first time */
3078 if (seekable && stop == -1) {
3079 GstFormat fmt = GST_FORMAT_BYTES;
3081 GST_DEBUG_OBJECT (avi, "doing duration query to fix up unset stop");
3082 gst_pad_query_peer_duration (avi->sinkpad, &fmt, &stop);
3085 /* if upstream doesn't know the size, it's likely that it's not seekable in
3086 * practice even if it technically may be seekable */
3087 if (seekable && (start != 0 || stop <= start)) {
3088 GST_DEBUG_OBJECT (avi, "seekable but unknown start/stop -> disable");
3093 GST_INFO_OBJECT (avi, "seekable: %d (%" G_GUINT64_FORMAT " - %"
3094 G_GUINT64_FORMAT ")", seekable, start, stop);
3095 avi->seekable = seekable;
3097 gst_query_unref (query);
3101 * Read AVI headers when streaming
3103 static GstFlowReturn
3104 gst_avi_demux_stream_header_push (GstAviDemux * avi)
3106 GstFlowReturn ret = GST_FLOW_OK;
3111 GstBuffer *buf = NULL, *sub = NULL;
3115 GstTagList *tags = NULL;
3117 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
3119 switch (avi->header_state) {
3120 case GST_AVI_DEMUX_HEADER_TAG_LIST:
3121 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3122 avi->offset += 8 + GST_ROUND_UP_2 (size);
3123 if (tag != GST_RIFF_TAG_LIST)
3124 goto header_no_list;
3126 gst_adapter_flush (avi->adapter, 8);
3127 /* Find the 'hdrl' LIST tag */
3128 GST_DEBUG ("Reading %d bytes", size);
3129 buf = gst_adapter_take_buffer (avi->adapter, size);
3131 if (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl)
3132 goto header_no_hdrl;
3136 gst_adapter_flush (avi->adapter, 1);
3138 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
3140 /* the hdrl starts with a 'avih' header */
3141 if (!gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3143 goto header_no_avih;
3145 if (tag != GST_RIFF_TAG_avih)
3146 goto header_no_avih;
3148 if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3149 goto header_wrong_avih;
3151 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
3153 /* now, read the elements from the header until the end */
3154 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3156 /* sub can be NULL on empty tags */
3161 case GST_RIFF_TAG_LIST:
3162 if (GST_BUFFER_SIZE (sub) < 4)
3165 switch (GST_READ_UINT32_LE (GST_BUFFER_DATA (sub))) {
3166 case GST_RIFF_LIST_strl:
3167 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3169 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3170 ("failed to parse stream, ignoring"));
3175 case GST_RIFF_LIST_odml:
3176 gst_avi_demux_parse_odml (avi, sub);
3180 GST_WARNING_OBJECT (avi,
3181 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3182 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA
3185 case GST_RIFF_TAG_JUNQ:
3186 case GST_RIFF_TAG_JUNK:
3191 gst_avi_demux_parse_idit (avi, sub);
3194 GST_WARNING_OBJECT (avi,
3195 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
3196 offset, GST_FOURCC_ARGS (tag));
3198 case GST_RIFF_TAG_JUNQ:
3199 case GST_RIFF_TAG_JUNK:
3201 /* move to next chunk */
3203 gst_buffer_unref (sub);
3208 gst_buffer_unref (buf);
3209 GST_DEBUG ("elements parsed");
3211 /* check parsed streams */
3212 if (avi->num_streams == 0) {
3214 } else if (avi->num_streams != avi->avih->streams) {
3215 GST_WARNING_OBJECT (avi,
3216 "Stream header mentioned %d streams, but %d available",
3217 avi->avih->streams, avi->num_streams);
3219 GST_DEBUG ("Get junk and info next");
3220 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
3222 /* Need more data */
3226 case GST_AVI_DEMUX_HEADER_INFO:
3227 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
3229 if (gst_adapter_available (avi->adapter) < 12)
3232 data = gst_adapter_peek (avi->adapter, 12);
3233 tag = GST_READ_UINT32_LE (data);
3234 size = GST_READ_UINT32_LE (data + 4);
3235 ltag = GST_READ_UINT32_LE (data + 8);
3237 if (tag == GST_RIFF_TAG_LIST) {
3239 case GST_RIFF_LIST_movi:
3240 gst_adapter_flush (avi->adapter, 12);
3241 if (!avi->first_movi_offset)
3242 avi->first_movi_offset = avi->offset;
3244 avi->idx1_offset = avi->offset + size - 4;
3246 case GST_RIFF_LIST_INFO:
3247 GST_DEBUG ("Found INFO chunk");
3248 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3249 GST_DEBUG ("got size %d", size);
3251 gst_adapter_flush (avi->adapter, 12);
3253 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
3256 gst_adapter_flush (avi->adapter, 1);
3257 gst_riff_parse_info (GST_ELEMENT_CAST (avi), buf, &tags);
3259 if (avi->globaltags) {
3260 gst_tag_list_insert (avi->globaltags, tags,
3261 GST_TAG_MERGE_REPLACE);
3263 avi->globaltags = tags;
3267 gst_buffer_unref (buf);
3269 avi->offset += GST_ROUND_UP_2 (size) - 4;
3271 GST_DEBUG ("skipping INFO LIST prefix");
3274 /* Need more data */
3279 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3280 avi->offset += 8 + GST_ROUND_UP_2 (size);
3281 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3282 // ??? goto iterate; ???
3284 /* Need more data */
3290 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3291 avi->offset += 8 + GST_ROUND_UP_2 (size);
3292 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3295 /* Need more data */
3302 GST_WARNING ("unhandled header state: %d", avi->header_state);
3307 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3308 avi->num_streams, avi->stream[0].indexes);
3310 GST_DEBUG ("Found movi chunk. Starting to stream data");
3311 avi->state = GST_AVI_DEMUX_MOVI;
3313 /* no indexes in push mode, but it still sets some variables */
3314 gst_avi_demux_calculate_durations_from_index (avi);
3316 gst_avi_demux_expose_streams (avi, TRUE);
3318 /* prepare all streams for index 0 */
3319 for (i = 0; i < avi->num_streams; i++)
3320 avi->stream[i].current_entry = 0;
3322 /* create initial NEWSEGMENT event */
3323 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3324 stop = avi->segment.duration;
3326 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3329 gst_event_unref (avi->seg_event);
3330 avi->seg_event = gst_event_new_new_segment_full
3331 (FALSE, avi->segment.rate, avi->segment.applied_rate, GST_FORMAT_TIME,
3332 avi->segment.start, stop, avi->segment.time);
3334 gst_avi_demux_check_seekability (avi);
3336 /* at this point we know all the streams and we can signal the no more
3338 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3339 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3346 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3347 return GST_FLOW_ERROR;
3351 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3352 ("Invalid AVI header (no LIST at start): %"
3353 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3354 return GST_FLOW_ERROR;
3358 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3359 ("Invalid AVI header (no hdrl at start): %"
3360 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3361 gst_buffer_unref (buf);
3362 return GST_FLOW_ERROR;
3366 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3367 ("Invalid AVI header (no avih at start): %"
3368 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3370 gst_buffer_unref (sub);
3372 gst_buffer_unref (buf);
3373 return GST_FLOW_ERROR;
3377 gst_buffer_unref (buf);
3378 return GST_FLOW_ERROR;
3383 gst_avi_demux_add_date_tag (GstAviDemux * avi, gint y, gint m, gint d,
3384 gint h, gint min, gint s)
3389 date = g_date_new_dmy (d, m, y);
3390 if (!g_date_valid (date)) {
3392 GST_WARNING_OBJECT (avi, "Refusing to add invalid date %d-%d-%d", y, m, d);
3397 dt = gst_date_time_new_local_time (y, m, d, h, min, s);
3399 if (avi->globaltags == NULL)
3400 avi->globaltags = gst_tag_list_new ();
3402 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE, date,
3406 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE_TIME,
3408 gst_date_time_unref (dt);
3413 gst_avi_demux_parse_idit_nums_only (GstAviDemux * avi, gchar * data)
3418 ret = sscanf (data, "%d:%d:%d", &y, &m, &d);
3420 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3423 gst_avi_demux_add_date_tag (avi, y, m, d, 0, 0, 0);
3427 get_month_num (gchar * data, guint size)
3429 if (g_ascii_strncasecmp (data, "jan", 3) == 0) {
3431 } else if (g_ascii_strncasecmp (data, "feb", 3) == 0) {
3433 } else if (g_ascii_strncasecmp (data, "mar", 3) == 0) {
3435 } else if (g_ascii_strncasecmp (data, "apr", 3) == 0) {
3437 } else if (g_ascii_strncasecmp (data, "may", 3) == 0) {
3439 } else if (g_ascii_strncasecmp (data, "jun", 3) == 0) {
3441 } else if (g_ascii_strncasecmp (data, "jul", 3) == 0) {
3443 } else if (g_ascii_strncasecmp (data, "aug", 3) == 0) {
3445 } else if (g_ascii_strncasecmp (data, "sep", 3) == 0) {
3447 } else if (g_ascii_strncasecmp (data, "oct", 3) == 0) {
3449 } else if (g_ascii_strncasecmp (data, "nov", 3) == 0) {
3451 } else if (g_ascii_strncasecmp (data, "dec", 3) == 0) {
3459 gst_avi_demux_parse_idit_text (GstAviDemux * avi, gchar * data)
3461 gint year, month, day;
3462 gint hour, min, sec;
3467 ret = sscanf (data, "%3s %3s %d %d:%d:%d %d", weekday, monthstr, &day, &hour,
3470 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3473 month = get_month_num (monthstr, strlen (monthstr));
3474 gst_avi_demux_add_date_tag (avi, year, month, day, hour, min, sec);
3478 gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf)
3480 gchar *data = (gchar *) GST_BUFFER_DATA (buf);
3481 guint size = GST_BUFFER_SIZE (buf);
3482 gchar *safedata = NULL;
3486 * http://www.eden-foundation.org/products/code/film_date_stamp/index.html
3488 * This tag could be in one of the below formats
3489 * 2005:08:17 11:42:43
3490 * THU OCT 26 16:46:04 2006
3491 * Mon Mar 3 09:44:56 2008
3493 * FIXME: Our date tag doesn't include hours
3496 /* skip eventual initial whitespace */
3497 while (size > 0 && g_ascii_isspace (data[0])) {
3506 /* make a safe copy to add a \0 to the end of the string */
3507 safedata = g_strndup (data, size);
3509 /* test if the first char is a alpha or a number */
3510 if (g_ascii_isdigit (data[0])) {
3511 gst_avi_demux_parse_idit_nums_only (avi, safedata);
3514 } else if (g_ascii_isalpha (data[0])) {
3515 gst_avi_demux_parse_idit_text (avi, safedata);
3523 GST_WARNING_OBJECT (avi, "IDIT tag has no parsable info");
3527 * Read full AVI headers.
3529 static GstFlowReturn
3530 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
3533 GstBuffer *buf, *sub = NULL;
3537 GstElement *element = GST_ELEMENT_CAST (avi);
3539 GstTagList *tags = NULL;
3541 stamp = gst_util_get_timestamp ();
3543 /* the header consists of a 'hdrl' LIST tag */
3544 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3545 if (res != GST_FLOW_OK)
3546 goto pull_range_failed;
3547 else if (tag != GST_RIFF_TAG_LIST)
3549 else if (GST_BUFFER_SIZE (buf) < 4)
3552 GST_DEBUG_OBJECT (avi, "parsing headers");
3554 /* Find the 'hdrl' LIST tag */
3555 while (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl) {
3556 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
3557 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf))));
3560 gst_buffer_unref (buf);
3562 /* read new chunk */
3563 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3564 if (res != GST_FLOW_OK)
3565 goto pull_range_failed;
3566 else if (tag != GST_RIFF_TAG_LIST)
3568 else if (GST_BUFFER_SIZE (buf) < 4)
3572 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
3574 /* the hdrl starts with a 'avih' header */
3575 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
3577 else if (tag != GST_RIFF_TAG_avih)
3579 else if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3582 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3584 /* now, read the elements from the header until the end */
3585 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
3586 /* sub can be NULL on empty tags */
3591 case GST_RIFF_TAG_LIST:
3596 if (GST_BUFFER_SIZE (sub) < 4)
3599 data = GST_BUFFER_DATA (sub);
3600 fourcc = GST_READ_UINT32_LE (data);
3603 case GST_RIFF_LIST_strl:
3604 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3605 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3606 ("failed to parse stream, ignoring"));
3611 case GST_RIFF_LIST_odml:
3612 gst_avi_demux_parse_odml (avi, sub);
3615 case GST_RIFF_LIST_INFO:
3616 GST_BUFFER_DATA (sub) = data + 4;
3617 GST_BUFFER_SIZE (sub) -= 4;
3618 gst_riff_parse_info (element, sub, &tags);
3620 if (avi->globaltags) {
3621 gst_tag_list_insert (avi->globaltags, tags,
3622 GST_TAG_MERGE_REPLACE);
3624 avi->globaltags = tags;
3630 GST_WARNING_OBJECT (avi,
3631 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3632 GST_FOURCC_ARGS (fourcc));
3633 GST_MEMDUMP_OBJECT (avi, "Unknown list", GST_BUFFER_DATA (sub),
3634 GST_BUFFER_SIZE (sub));
3636 case GST_RIFF_TAG_JUNQ:
3637 case GST_RIFF_TAG_JUNK:
3643 gst_avi_demux_parse_idit (avi, sub);
3646 GST_WARNING_OBJECT (avi,
3647 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
3648 GST_FOURCC_ARGS (tag), offset);
3649 GST_MEMDUMP_OBJECT (avi, "Unknown tag", GST_BUFFER_DATA (sub),
3650 GST_BUFFER_SIZE (sub));
3652 case GST_RIFF_TAG_JUNQ:
3653 case GST_RIFF_TAG_JUNK:
3656 gst_buffer_unref (sub);
3661 gst_buffer_unref (buf);
3662 GST_DEBUG ("elements parsed");
3664 /* check parsed streams */
3665 if (avi->num_streams == 0)
3667 else if (avi->num_streams != avi->avih->streams) {
3668 GST_WARNING_OBJECT (avi,
3669 "Stream header mentioned %d streams, but %d available",
3670 avi->avih->streams, avi->num_streams);
3673 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
3674 G_GUINT64_FORMAT, avi->offset);
3676 /* Now, find the data (i.e. skip all junk between header and data) */
3682 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
3683 if (res != GST_FLOW_OK) {
3684 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
3685 goto pull_range_failed;
3686 } else if (GST_BUFFER_SIZE (buf) < 12) {
3687 GST_DEBUG_OBJECT (avi, "got %d bytes which is less than 12 bytes",
3688 GST_BUFFER_SIZE (buf));
3689 gst_buffer_unref (buf);
3690 return GST_FLOW_ERROR;
3693 data = GST_BUFFER_DATA (buf);
3695 tag = GST_READ_UINT32_LE (data);
3696 size = GST_READ_UINT32_LE (data + 4);
3697 ltag = GST_READ_UINT32_LE (data + 8);
3699 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
3700 GST_FOURCC_ARGS (tag), size);
3701 GST_MEMDUMP ("Tag content", data, GST_BUFFER_SIZE (buf));
3702 gst_buffer_unref (buf);
3705 case GST_RIFF_TAG_LIST:{
3707 case GST_RIFF_LIST_movi:
3708 GST_DEBUG_OBJECT (avi,
3709 "Reached the 'movi' tag, we're done with skipping");
3711 case GST_RIFF_LIST_INFO:
3713 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3715 if (res != GST_FLOW_OK) {
3716 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3717 goto pull_range_failed;
3719 GST_DEBUG ("got size %u", GST_BUFFER_SIZE (buf));
3721 GST_DEBUG ("skipping INFO LIST prefix");
3722 avi->offset += (4 - GST_ROUND_UP_2 (size));
3723 gst_buffer_unref (buf);
3727 sub = gst_buffer_create_sub (buf, 4, GST_BUFFER_SIZE (buf) - 4);
3728 gst_riff_parse_info (element, sub, &tags);
3730 if (avi->globaltags) {
3731 gst_tag_list_insert (avi->globaltags, tags,
3732 GST_TAG_MERGE_REPLACE);
3734 avi->globaltags = tags;
3739 gst_buffer_unref (sub);
3742 gst_buffer_unref (buf);
3743 /* gst_riff_read_chunk() has already advanced avi->offset */
3746 GST_WARNING_OBJECT (avi,
3747 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3748 GST_FOURCC_ARGS (ltag));
3749 avi->offset += 8 + GST_ROUND_UP_2 (size);
3755 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3756 GST_FOURCC_ARGS (tag));
3758 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3759 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3760 /* Only get buffer for debugging if the memdump is needed */
3761 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= 9) {
3762 res = gst_pad_pull_range (avi->sinkpad, avi->offset, size, &buf);
3763 if (res != GST_FLOW_OK) {
3764 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3765 goto pull_range_failed;
3767 GST_MEMDUMP ("Junk", GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
3768 gst_buffer_unref (buf);
3770 avi->offset += 8 + GST_ROUND_UP_2 (size);
3776 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3777 avi->num_streams, avi->stream[0].indexes);
3779 /* create or read stream index (for seeking) */
3780 if (avi->stream[0].indexes != NULL) {
3781 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3782 gst_avi_demux_read_subindexes_pull (avi);
3784 if (!avi->have_index) {
3785 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
3786 gst_avi_demux_stream_index (avi);
3788 /* still no index, scan */
3789 if (!avi->have_index) {
3790 gst_avi_demux_stream_scan (avi);
3792 /* still no index.. this is a fatal error for now.
3793 * FIXME, we should switch to plain push mode without seeking
3794 * instead of failing. */
3795 if (!avi->have_index)
3799 /* use the indexes now to construct nice durations */
3800 gst_avi_demux_calculate_durations_from_index (avi);
3802 gst_avi_demux_expose_streams (avi, FALSE);
3804 /* create initial NEWSEGMENT event */
3805 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3806 stop = avi->segment.duration;
3808 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3810 /* do initial seek to the default segment values */
3811 gst_avi_demux_do_seek (avi, &avi->segment);
3813 /* prepare initial segment */
3815 gst_event_unref (avi->seg_event);
3816 avi->seg_event = gst_event_new_new_segment_full
3817 (FALSE, avi->segment.rate, avi->segment.applied_rate, GST_FORMAT_TIME,
3818 avi->segment.start, stop, avi->segment.time);
3820 stamp = gst_util_get_timestamp () - stamp;
3821 GST_DEBUG_OBJECT (avi, "pulling header took %" GST_TIME_FORMAT,
3822 GST_TIME_ARGS (stamp));
3824 /* at this point we know all the streams and we can signal the no more
3826 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3827 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3834 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3835 ("Invalid AVI header (no LIST at start): %"
3836 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3837 gst_buffer_unref (buf);
3838 return GST_FLOW_ERROR;
3842 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3843 ("Invalid AVI header (no hdrl at start): %"
3844 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3845 gst_buffer_unref (buf);
3846 return GST_FLOW_ERROR;
3850 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3851 ("Invalid AVI header (no avih at start): %"
3852 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3854 gst_buffer_unref (sub);
3855 gst_buffer_unref (buf);
3856 return GST_FLOW_ERROR;
3860 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3861 ("Invalid AVI header (cannot parse avih at start)"));
3862 gst_buffer_unref (buf);
3863 return GST_FLOW_ERROR;
3867 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3868 return GST_FLOW_ERROR;
3872 GST_WARNING ("file without or too big index");
3873 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3874 ("Could not get/create index"));
3875 return GST_FLOW_ERROR;
3879 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3880 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3881 return GST_FLOW_ERROR;
3885 /* move a stream to @index */
3887 gst_avi_demux_move_stream (GstAviDemux * avi, GstAviStream * stream,
3888 GstSegment * segment, guint index)
3890 GST_DEBUG_OBJECT (avi, "Move stream %d to %u", stream->num, index);
3892 if (segment->rate < 0.0) {
3894 /* Because we don't know the frame order we need to push from the prev keyframe
3895 * to the next keyframe. If there is a smart decoder downstream he will notice
3896 * that there are too many encoded frames send and return UNEXPECTED when there
3897 * are enough decoded frames to fill the segment. */
3898 next_key = gst_avi_demux_index_next (avi, stream, index, TRUE);
3900 /* FIXME, we go back to 0, we should look at segment.start. We will however
3901 * stop earlier when the see the timestamp < segment.start */
3902 stream->start_entry = 0;
3903 stream->step_entry = index;
3904 stream->current_entry = index;
3905 stream->stop_entry = next_key;
3907 GST_DEBUG_OBJECT (avi, "reverse seek: start %u, step %u, stop %u",
3908 stream->start_entry, stream->step_entry, stream->stop_entry);
3910 stream->start_entry = index;
3911 stream->step_entry = index;
3912 stream->stop_entry = gst_avi_demux_index_last (avi, stream);
3914 if (stream->current_entry != index) {
3915 GST_DEBUG_OBJECT (avi, "Move DISCONT from %u to %u",
3916 stream->current_entry, index);
3917 stream->current_entry = index;
3918 stream->discont = TRUE;
3921 /* update the buffer info */
3922 gst_avi_demux_get_buffer_info (avi, stream, index,
3923 &stream->current_timestamp, &stream->current_ts_end,
3924 &stream->current_offset, &stream->current_offset_end);
3926 GST_DEBUG_OBJECT (avi, "Moved to %u, ts %" GST_TIME_FORMAT
3927 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
3928 ", off_end %" G_GUINT64_FORMAT, index,
3929 GST_TIME_ARGS (stream->current_timestamp),
3930 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
3931 stream->current_offset_end);
3933 GST_DEBUG_OBJECT (avi, "Seeking to offset %" G_GUINT64_FORMAT,
3934 stream->index[index].offset);
3938 * Do the actual seeking.
3941 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
3943 GstClockTime seek_time;
3946 GstAviStream *stream;
3948 seek_time = segment->last_stop;
3949 keyframe = ! !(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
3951 GST_DEBUG_OBJECT (avi, "seek to: %" GST_TIME_FORMAT
3952 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
3954 /* FIXME, this code assumes the main stream with keyframes is stream 0,
3955 * which is mostly correct... */
3956 stream = &avi->stream[avi->main_stream];
3958 /* get the entry index for the requested position */
3959 index = gst_avi_demux_index_for_time (avi, stream, seek_time);
3960 GST_DEBUG_OBJECT (avi, "Got entry %u", index);
3962 /* check if we are already on a keyframe */
3963 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
3964 GST_DEBUG_OBJECT (avi, "not keyframe, searching back");
3965 /* now go to the previous keyframe, this is where we should start
3967 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
3968 GST_DEBUG_OBJECT (avi, "previous keyframe at %u", index);
3971 /* move the main stream to this position */
3972 gst_avi_demux_move_stream (avi, stream, segment, index);
3975 /* when seeking to a keyframe, we update the result seek time
3976 * to the time of the keyframe. */
3977 seek_time = stream->current_timestamp;
3978 GST_DEBUG_OBJECT (avi, "keyframe adjusted to %" GST_TIME_FORMAT,
3979 GST_TIME_ARGS (seek_time));
3982 /* the seek time is also the last_stop and stream time when going
3984 segment->last_stop = seek_time;
3985 if (segment->rate > 0.0)
3986 segment->time = seek_time;
3988 /* now set DISCONT and align the other streams */
3989 for (i = 0; i < avi->num_streams; i++) {
3990 GstAviStream *ostream;
3992 ostream = &avi->stream[i];
3993 if ((ostream == stream) || (ostream->index == NULL))
3996 /* get the entry index for the requested position */
3997 index = gst_avi_demux_index_for_time (avi, ostream, seek_time);
3999 /* move to previous keyframe */
4000 if (!ENTRY_IS_KEYFRAME (&ostream->index[index]))
4001 index = gst_avi_demux_index_prev (avi, ostream, index, TRUE);
4003 gst_avi_demux_move_stream (avi, ostream, segment, index);
4005 GST_DEBUG_OBJECT (avi, "done seek to: %" GST_TIME_FORMAT,
4006 GST_TIME_ARGS (seek_time));
4012 * Handle seek event in pull mode.
4015 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4020 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4024 GstSegment seeksegment = { 0, };
4028 GST_DEBUG_OBJECT (avi, "doing seek with event");
4030 gst_event_parse_seek (event, &rate, &format, &flags,
4031 &cur_type, &cur, &stop_type, &stop);
4033 /* we have to have a format as the segment format. Try to convert
4035 if (format != GST_FORMAT_TIME) {
4036 GstFormat fmt = GST_FORMAT_TIME;
4037 gboolean res = TRUE;
4039 if (cur_type != GST_SEEK_TYPE_NONE)
4040 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
4041 if (res && stop_type != GST_SEEK_TYPE_NONE)
4042 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
4048 GST_DEBUG_OBJECT (avi,
4049 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
4050 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
4051 /* FIXME: can we do anything with rate!=1.0 */
4053 GST_DEBUG_OBJECT (avi, "doing seek without event");
4058 /* save flush flag */
4059 flush = flags & GST_SEEK_FLAG_FLUSH;
4062 GstEvent *fevent = gst_event_new_flush_start ();
4064 /* for a flushing seek, we send a flush_start on all pads. This will
4065 * eventually stop streaming with a WRONG_STATE. We can thus eventually
4066 * take the STREAM_LOCK. */
4067 GST_DEBUG_OBJECT (avi, "sending flush start");
4068 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4069 gst_pad_push_event (avi->sinkpad, fevent);
4071 /* a non-flushing seek, we PAUSE the task so that we can take the
4073 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
4074 gst_pad_pause_task (avi->sinkpad);
4077 /* wait for streaming to stop */
4078 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
4079 GST_PAD_STREAM_LOCK (avi->sinkpad);
4081 /* copy segment, we need this because we still need the old
4082 * segment when we close the current segment. */
4083 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4086 GST_DEBUG_OBJECT (avi, "configuring seek");
4087 gst_segment_set_seek (&seeksegment, rate, format, flags,
4088 cur_type, cur, stop_type, stop, &update);
4090 /* do the seek, seeksegment.last_stop contains the new position, this
4091 * actually never fails. */
4092 gst_avi_demux_do_seek (avi, &seeksegment);
4094 gst_event_replace (&avi->close_seg_event, NULL);
4096 GstEvent *fevent = gst_event_new_flush_stop ();
4098 GST_DEBUG_OBJECT (avi, "sending flush stop");
4099 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4100 gst_pad_push_event (avi->sinkpad, fevent);
4101 } else if (avi->segment_running) {
4102 /* we are running the current segment and doing a non-flushing seek,
4103 * close the segment first based on the last_stop. */
4104 GST_DEBUG_OBJECT (avi, "closing running segment %" G_GINT64_FORMAT
4105 " to %" G_GINT64_FORMAT, avi->segment.start, avi->segment.last_stop);
4106 avi->close_seg_event = gst_event_new_new_segment_full (TRUE,
4107 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
4108 avi->segment.start, avi->segment.last_stop, avi->segment.time);
4111 /* now update the real segment info */
4112 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
4114 /* post the SEGMENT_START message when we do segmented playback */
4115 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4116 gst_element_post_message (GST_ELEMENT_CAST (avi),
4117 gst_message_new_segment_start (GST_OBJECT_CAST (avi),
4118 avi->segment.format, avi->segment.last_stop));
4121 /* prepare for streaming again */
4122 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
4123 stop = avi->segment.duration;
4125 /* queue the segment event for the streaming thread. */
4127 gst_event_unref (avi->seg_event);
4128 if (avi->segment.rate > 0.0) {
4129 /* forwards goes from last_stop to stop */
4130 avi->seg_event = gst_event_new_new_segment_full (FALSE,
4131 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
4132 avi->segment.last_stop, stop, avi->segment.time);
4134 /* reverse goes from start to last_stop */
4135 avi->seg_event = gst_event_new_new_segment_full (FALSE,
4136 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
4137 avi->segment.start, avi->segment.last_stop, avi->segment.time);
4140 if (!avi->streaming) {
4141 avi->segment_running = TRUE;
4142 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4145 /* reset the last flow and mark discont, seek is always DISCONT */
4146 for (i = 0; i < avi->num_streams; i++) {
4147 GST_DEBUG_OBJECT (avi, "marking DISCONT");
4148 avi->stream[i].last_flow = GST_FLOW_OK;
4149 avi->stream[i].discont = TRUE;
4151 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
4158 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4164 * Handle seek event in push mode.
4167 avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4172 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4175 GstAviStream *stream;
4179 GstSegment seeksegment;
4182 /* check we have the index */
4183 if (!avi->have_index) {
4184 GST_DEBUG_OBJECT (avi, "no seek index built, seek aborted.");
4187 GST_DEBUG_OBJECT (avi, "doing push-based seek with event");
4190 gst_event_parse_seek (event, &rate, &format, &flags,
4191 &cur_type, &cur, &stop_type, &stop);
4193 if (format != GST_FORMAT_TIME) {
4194 GstFormat fmt = GST_FORMAT_TIME;
4195 gboolean res = TRUE;
4197 if (cur_type != GST_SEEK_TYPE_NONE)
4198 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
4199 if (res && stop_type != GST_SEEK_TYPE_NONE)
4200 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
4202 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4209 /* let gst_segment handle any tricky stuff */
4210 GST_DEBUG_OBJECT (avi, "configuring seek");
4211 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4212 gst_segment_set_seek (&seeksegment, rate, format, flags,
4213 cur_type, cur, stop_type, stop, &update);
4215 keyframe = ! !(flags & GST_SEEK_FLAG_KEY_UNIT);
4216 cur = seeksegment.last_stop;
4218 GST_DEBUG_OBJECT (avi,
4219 "Seek requested: ts %" GST_TIME_FORMAT " stop %" GST_TIME_FORMAT
4220 ", kf %u, rate %lf", GST_TIME_ARGS (cur), GST_TIME_ARGS (stop), keyframe,
4224 GST_DEBUG_OBJECT (avi, "negative rate seek not supported in push mode");
4228 /* FIXME, this code assumes the main stream with keyframes is stream 0,
4229 * which is mostly correct... */
4230 str_num = avi->main_stream;
4231 stream = &avi->stream[str_num];
4233 /* get the entry index for the requested position */
4234 index = gst_avi_demux_index_for_time (avi, stream, cur);
4235 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT,
4236 str_num, index, GST_TIME_ARGS (cur));
4238 /* check if we are already on a keyframe */
4239 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
4240 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4241 /* now go to the previous keyframe, this is where we should start
4243 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4244 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", index);
4247 gst_avi_demux_get_buffer_info (avi, stream, index,
4248 &stream->current_timestamp, &stream->current_ts_end,
4249 &stream->current_offset, &stream->current_offset_end);
4251 /* re-use cur to be the timestamp of the seek as it _will_ be */
4252 cur = stream->current_timestamp;
4254 min_offset = stream->index[index].offset;
4255 avi->seek_kf_offset = min_offset - 8;
4257 GST_DEBUG_OBJECT (avi,
4258 "Seek to: ts %" GST_TIME_FORMAT " (on str %u, idx %u, offset %"
4259 G_GUINT64_FORMAT ")", GST_TIME_ARGS (stream->current_timestamp), str_num,
4262 for (n = 0; n < avi->num_streams; n++) {
4263 GstAviStream *str = &avi->stream[n];
4266 if (n == avi->main_stream)
4269 /* get the entry index for the requested position */
4270 idx = gst_avi_demux_index_for_time (avi, str, cur);
4271 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT, n,
4272 idx, GST_TIME_ARGS (cur));
4274 /* check if we are already on a keyframe */
4275 if (!ENTRY_IS_KEYFRAME (&str->index[idx])) {
4276 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4277 /* now go to the previous keyframe, this is where we should start
4279 idx = gst_avi_demux_index_prev (avi, str, idx, TRUE);
4280 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", idx);
4283 gst_avi_demux_get_buffer_info (avi, str, idx,
4284 &str->current_timestamp, &str->current_ts_end,
4285 &str->current_offset, &str->current_offset_end);
4287 if (str->index[idx].offset < min_offset) {
4288 min_offset = str->index[idx].offset;
4289 GST_DEBUG_OBJECT (avi,
4290 "Found an earlier offset at %" G_GUINT64_FORMAT ", str %u",
4298 GST_DEBUG_OBJECT (avi,
4299 "Seek performed: str %u, offset %" G_GUINT64_FORMAT ", idx %u, ts %"
4300 GST_TIME_FORMAT ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4301 ", off_end %" G_GUINT64_FORMAT, str_num, min_offset, index,
4302 GST_TIME_ARGS (stream->current_timestamp),
4303 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4304 stream->current_offset_end);
4306 /* index data refers to data, not chunk header (for pull mode convenience) */
4308 GST_DEBUG_OBJECT (avi, "seeking to chunk at offset %" G_GUINT64_FORMAT,
4311 if (!perform_seek_to_offset (avi, min_offset)) {
4312 GST_DEBUG_OBJECT (avi, "seek event failed!");
4320 * Handle whether we can perform the seek event or if we have to let the chain
4321 * function handle seeks to build the seek indexes first.
4324 gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
4327 /* check for having parsed index already */
4328 if (!avi->have_index) {
4330 gboolean building_index;
4332 GST_OBJECT_LOCK (avi);
4333 /* handle the seek event in the chain function */
4334 avi->state = GST_AVI_DEMUX_SEEK;
4336 /* copy the event */
4337 if (avi->seek_event)
4338 gst_event_unref (avi->seek_event);
4339 avi->seek_event = gst_event_ref (event);
4341 /* set the building_index flag so that only one thread can setup the
4342 * structures for index seeking. */
4343 building_index = avi->building_index;
4344 if (!building_index) {
4345 avi->building_index = TRUE;
4346 if (avi->stream[0].indexes) {
4347 avi->odml_stream = 0;
4348 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
4349 offset = avi->odml_subidxs[0];
4351 offset = avi->idx1_offset;
4354 GST_OBJECT_UNLOCK (avi);
4356 if (!building_index) {
4357 /* seek to the first subindex or legacy index */
4358 GST_INFO_OBJECT (avi,
4359 "Seeking to legacy index/first subindex at %" G_GUINT64_FORMAT,
4361 return perform_seek_to_offset (avi, offset);
4364 /* FIXME: we have to always return true so that we don't block the seek
4366 * Note: maybe it is OK to return true if we're still building the index */
4370 return avi_demux_handle_seek_push (avi, pad, event);
4374 * Helper for gst_avi_demux_invert()
4377 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
4379 memcpy (tmp, d1, bytes);
4380 memcpy (d1, d2, bytes);
4381 memcpy (d2, tmp, bytes);
4385 #define gst_avi_demux_is_uncompressed(fourcc) \
4386 (fourcc == GST_RIFF_DIB || \
4387 fourcc == GST_RIFF_rgb || \
4388 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW)
4391 * Invert DIB buffers... Takes existing buffer and
4392 * returns either the buffer or a new one (with old
4393 * one dereferenced).
4394 * FIXME: can't we preallocate tmp? and remember stride, bpp?
4397 gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
4404 if (stream->strh->type != GST_RIFF_FCC_vids)
4407 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
4408 return buf; /* Ignore non DIB buffers */
4411 s = gst_caps_get_structure (GST_PAD_CAPS (stream->pad), 0);
4412 if (!gst_structure_get_int (s, "bpp", &bpp)) {
4413 GST_WARNING ("Failed to retrieve depth from caps");
4417 if (stream->strf.vids == NULL) {
4418 GST_WARNING ("Failed to retrieve vids for stream");
4422 h = stream->strf.vids->height;
4423 w = stream->strf.vids->width;
4424 stride = w * (bpp / 8);
4426 buf = gst_buffer_make_writable (buf);
4427 if (GST_BUFFER_SIZE (buf) < (stride * h)) {
4428 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
4432 tmp = g_malloc (stride);
4434 for (y = 0; y < h / 2; y++) {
4435 swap_line (GST_BUFFER_DATA (buf) + stride * y,
4436 GST_BUFFER_DATA (buf) + stride * (h - 1 - y), tmp, stride);
4445 gst_avi_demux_add_assoc (GstAviDemux * avi, GstAviStream * stream,
4446 GstClockTime timestamp, guint64 offset, gboolean keyframe)
4448 /* do not add indefinitely for open-ended streaming */
4449 if (G_UNLIKELY (avi->element_index && avi->seekable)) {
4450 GST_LOG_OBJECT (avi, "adding association %" GST_TIME_FORMAT "-> %"
4451 G_GUINT64_FORMAT, GST_TIME_ARGS (timestamp), offset);
4452 gst_index_add_association (avi->element_index, avi->index_id,
4453 keyframe ? GST_ASSOCIATION_FLAG_KEY_UNIT : GST_ASSOCIATION_FLAG_NONE,
4454 GST_FORMAT_TIME, timestamp, GST_FORMAT_BYTES, offset, NULL);
4455 /* well, current_total determines TIME and entry DEFAULT (frame #) ... */
4456 gst_index_add_association (avi->element_index, stream->index_id,
4457 GST_ASSOCIATION_FLAG_NONE,
4458 GST_FORMAT_TIME, stream->current_total, GST_FORMAT_BYTES, offset,
4459 GST_FORMAT_DEFAULT, stream->current_entry, NULL);
4464 * Returns the aggregated GstFlowReturn.
4466 static GstFlowReturn
4467 gst_avi_demux_combine_flows (GstAviDemux * avi, GstAviStream * stream,
4471 gboolean unexpected = FALSE, not_linked = TRUE;
4473 /* store the value */
4474 stream->last_flow = ret;
4476 /* any other error that is not-linked or eos can be returned right away */
4477 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
4480 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4481 for (i = 0; i < avi->num_streams; i++) {
4482 GstAviStream *ostream = &avi->stream[i];
4484 ret = ostream->last_flow;
4485 /* no unexpected or unlinked, return */
4486 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
4489 /* we check to see if we have at least 1 unexpected or all unlinked */
4490 unexpected |= (ret == GST_FLOW_UNEXPECTED);
4491 not_linked &= (ret == GST_FLOW_NOT_LINKED);
4493 /* when we get here, we all have unlinked or unexpected */
4495 ret = GST_FLOW_NOT_LINKED;
4496 else if (unexpected)
4497 ret = GST_FLOW_UNEXPECTED;
4499 GST_LOG_OBJECT (avi, "combined %s to return %s",
4500 gst_flow_get_name (stream->last_flow), gst_flow_get_name (ret));
4504 /* move @stream to the next position in its index */
4505 static GstFlowReturn
4506 gst_avi_demux_advance (GstAviDemux * avi, GstAviStream * stream,
4509 guint old_entry, new_entry;
4511 old_entry = stream->current_entry;
4513 new_entry = old_entry + 1;
4515 /* see if we reached the end */
4516 if (new_entry >= stream->stop_entry) {
4517 if (avi->segment.rate < 0.0) {
4518 if (stream->step_entry == stream->start_entry) {
4519 /* we stepped all the way to the start, eos */
4520 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
4523 /* backwards, stop becomes step, find a new step */
4524 stream->stop_entry = stream->step_entry;
4525 stream->step_entry = gst_avi_demux_index_prev (avi, stream,
4526 stream->stop_entry, TRUE);
4528 GST_DEBUG_OBJECT (avi,
4529 "reverse playback jump: start %u, step %u, stop %u",
4530 stream->start_entry, stream->step_entry, stream->stop_entry);
4532 /* and start from the previous keyframe now */
4533 new_entry = stream->step_entry;
4536 GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
4541 if (new_entry != old_entry) {
4542 stream->current_entry = new_entry;
4543 stream->current_total = stream->index[new_entry].total;
4545 if (new_entry == old_entry + 1) {
4546 GST_DEBUG_OBJECT (avi, "moved forwards from %u to %u",
4547 old_entry, new_entry);
4548 /* we simply moved one step forwards, reuse current info */
4549 stream->current_timestamp = stream->current_ts_end;
4550 stream->current_offset = stream->current_offset_end;
4551 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4552 NULL, &stream->current_ts_end, NULL, &stream->current_offset_end);
4554 /* we moved DISCONT, full update */
4555 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4556 &stream->current_timestamp, &stream->current_ts_end,
4557 &stream->current_offset, &stream->current_offset_end);
4558 /* and MARK discont for this stream */
4559 stream->last_flow = GST_FLOW_OK;
4560 stream->discont = TRUE;
4561 GST_DEBUG_OBJECT (avi, "Moved from %u to %u, ts %" GST_TIME_FORMAT
4562 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4563 ", off_end %" G_GUINT64_FORMAT, old_entry, new_entry,
4564 GST_TIME_ARGS (stream->current_timestamp),
4565 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4566 stream->current_offset_end);
4574 GST_DEBUG_OBJECT (avi, "we are EOS");
4575 /* setting current_timestamp to -1 marks EOS */
4576 stream->current_timestamp = -1;
4577 return GST_FLOW_UNEXPECTED;
4581 /* find the stream with the lowest current position when going forwards or with
4582 * the highest position when going backwards, this is the stream
4583 * we should push from next */
4585 gst_avi_demux_find_next (GstAviDemux * avi, gfloat rate)
4587 guint64 min_time, max_time;
4588 guint stream_num, i;
4591 min_time = G_MAXUINT64;
4594 for (i = 0; i < avi->num_streams; i++) {
4596 GstAviStream *stream;
4598 stream = &avi->stream[i];
4600 /* ignore streams that finished */
4601 if (stream->last_flow == GST_FLOW_UNEXPECTED)
4604 position = stream->current_timestamp;
4606 /* position of -1 is EOS */
4607 if (position != -1) {
4608 if (rate > 0.0 && position < min_time) {
4609 min_time = position;
4611 } else if (rate < 0.0 && position >= max_time) {
4612 max_time = position;
4620 static GstFlowReturn
4621 gst_avi_demux_loop_data (GstAviDemux * avi)
4623 GstFlowReturn ret = GST_FLOW_OK;
4625 GstAviStream *stream;
4626 gboolean processed = FALSE;
4628 guint64 offset, size;
4629 GstClockTime timestamp, duration;
4630 guint64 out_offset, out_offset_end;
4632 GstAviIndexEntry *entry;
4635 stream_num = gst_avi_demux_find_next (avi, avi->segment.rate);
4638 if (G_UNLIKELY (stream_num == -1)) {
4639 GST_DEBUG_OBJECT (avi, "all streams are EOS");
4643 /* we have the stream now */
4644 stream = &avi->stream[stream_num];
4646 /* skip streams without pads */
4648 GST_DEBUG_OBJECT (avi, "skipping entry from stream %d without pad",
4653 /* get the timing info for the entry */
4654 timestamp = stream->current_timestamp;
4655 duration = stream->current_ts_end - timestamp;
4656 out_offset = stream->current_offset;
4657 out_offset_end = stream->current_offset_end;
4659 /* get the entry data info */
4660 entry = &stream->index[stream->current_entry];
4661 offset = entry->offset;
4663 keyframe = ENTRY_IS_KEYFRAME (entry);
4665 /* skip empty entries */
4667 GST_DEBUG_OBJECT (avi, "Skipping entry %u (%" G_GUINT64_FORMAT ", %p)",
4668 stream->current_entry, size, stream->pad);
4672 if (avi->segment.rate > 0.0) {
4673 /* only check this for fowards playback for now */
4674 if (keyframe && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
4675 && (timestamp > avi->segment.stop)) {
4680 GST_LOG ("reading buffer (size=%" G_GUINT64_FORMAT "), stream %d, pos %"
4681 G_GUINT64_FORMAT " (0x%" G_GINT64_MODIFIER "x), kf %d", size,
4682 stream_num, offset, offset, keyframe);
4684 /* FIXME, check large chunks and cut them up */
4686 /* pull in the data */
4687 ret = gst_pad_pull_range (avi->sinkpad, offset, size, &buf);
4688 if (ret != GST_FLOW_OK)
4691 /* check for short buffers, this is EOS as well */
4692 if (GST_BUFFER_SIZE (buf) < size)
4695 /* invert the picture if needed */
4696 buf = gst_avi_demux_invert (stream, buf);
4698 /* mark non-keyframes */
4700 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4702 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4704 GST_BUFFER_TIMESTAMP (buf) = timestamp;
4705 GST_BUFFER_DURATION (buf) = duration;
4706 GST_BUFFER_OFFSET (buf) = out_offset;
4707 GST_BUFFER_OFFSET_END (buf) = out_offset_end;
4709 /* mark discont when pending */
4710 if (stream->discont) {
4711 GST_DEBUG_OBJECT (avi, "setting DISCONT flag");
4712 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4713 stream->discont = FALSE;
4716 gst_avi_demux_add_assoc (avi, stream, timestamp, offset, keyframe);
4718 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4720 /* update current position in the segment */
4721 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, timestamp);
4723 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %u, ts %"
4724 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4725 ", off_end %" G_GUINT64_FORMAT,
4726 GST_BUFFER_SIZE (buf), GST_TIME_ARGS (timestamp),
4727 GST_TIME_ARGS (duration), out_offset, out_offset_end);
4729 ret = gst_pad_push (stream->pad, buf);
4731 /* mark as processed, we increment the frame and byte counters then
4732 * leave the while loop and return the GstFlowReturn */
4735 if (avi->segment.rate < 0) {
4736 if (timestamp > avi->segment.stop && ret == GST_FLOW_UNEXPECTED) {
4737 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
4738 * we are at the end of the segment, so we just need to jump
4739 * back to the previous section. */
4740 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
4745 /* move to next item */
4746 ret = gst_avi_demux_advance (avi, stream, ret);
4749 ret = gst_avi_demux_combine_flows (avi, stream, ret);
4750 } while (!processed);
4758 GST_DEBUG_OBJECT (avi, "No samples left for any streams - EOS");
4759 ret = GST_FLOW_UNEXPECTED;
4764 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
4765 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
4766 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (avi->segment.stop));
4767 ret = GST_FLOW_UNEXPECTED;
4768 /* move to next stream */
4773 GST_DEBUG_OBJECT (avi, "pull range failed: pos=%" G_GUINT64_FORMAT
4774 " size=%" G_GUINT64_FORMAT, offset, size);
4779 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
4780 ", only got %d/%" G_GUINT64_FORMAT " bytes (truncated file?)", offset,
4781 GST_BUFFER_SIZE (buf), size);
4782 gst_buffer_unref (buf);
4783 ret = GST_FLOW_UNEXPECTED;
4789 * Read data. If we have an index it delegates to
4790 * gst_avi_demux_process_next_entry().
4792 static GstFlowReturn
4793 gst_avi_demux_stream_data (GstAviDemux * avi)
4798 GstFlowReturn res = GST_FLOW_OK;
4799 GstFormat format = GST_FORMAT_TIME;
4801 if (G_UNLIKELY (avi->have_eos)) {
4802 /* Clean adapter, we're done */
4803 gst_adapter_clear (avi->adapter);
4804 return GST_FLOW_UNEXPECTED;
4807 if (G_UNLIKELY (avi->todrop)) {
4810 if ((drop = gst_adapter_available (avi->adapter))) {
4811 if (drop > avi->todrop)
4813 GST_DEBUG_OBJECT (avi, "Dropping %d bytes", drop);
4814 gst_adapter_flush (avi->adapter, drop);
4815 avi->todrop -= drop;
4816 avi->offset += drop;
4820 /* Iterate until need more data, so adapter won't grow too much */
4822 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
4826 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
4827 GST_FOURCC_ARGS (tag), size);
4829 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
4830 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
4831 GST_LOG ("Chunk ok");
4832 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
4833 GST_DEBUG ("Found sub-index tag");
4834 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4835 /* accept 0 size buffer here */
4836 avi->abort_buffering = FALSE;
4837 GST_DEBUG (" skipping %d bytes for now", size);
4838 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4841 } else if (tag == GST_RIFF_TAG_RIFF) {
4842 /* RIFF tags can appear in ODML files, just jump over them */
4843 if (gst_adapter_available (avi->adapter) >= 12) {
4844 GST_DEBUG ("Found RIFF tag, skipping RIFF header");
4845 gst_adapter_flush (avi->adapter, 12);
4849 } else if (tag == GST_RIFF_TAG_idx1) {
4850 GST_DEBUG ("Found index tag");
4851 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4852 /* accept 0 size buffer here */
4853 avi->abort_buffering = FALSE;
4854 GST_DEBUG (" skipping %d bytes for now", size);
4855 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4858 } else if (tag == GST_RIFF_TAG_LIST) {
4859 /* movi chunks might be grouped in rec list */
4860 if (gst_adapter_available (avi->adapter) >= 12) {
4861 GST_DEBUG ("Found LIST tag, skipping LIST header");
4862 gst_adapter_flush (avi->adapter, 12);
4866 } else if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ) {
4867 /* rec list might contain JUNK chunks */
4868 GST_DEBUG ("Found JUNK tag");
4869 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4870 /* accept 0 size buffer here */
4871 avi->abort_buffering = FALSE;
4872 GST_DEBUG (" skipping %d bytes for now", size);
4873 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4877 GST_DEBUG ("No more stream chunks, send EOS");
4878 avi->have_eos = TRUE;
4879 return GST_FLOW_UNEXPECTED;
4882 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
4883 /* supposedly one hopes to catch a nicer chunk later on ... */
4884 /* FIXME ?? give up here rather than possibly ending up going
4885 * through the whole file */
4886 if (avi->abort_buffering) {
4887 avi->abort_buffering = FALSE;
4889 gst_adapter_flush (avi->adapter, 8);
4896 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
4897 GST_FOURCC_ARGS (tag), size);
4899 stream_nr = CHUNKID_TO_STREAMNR (tag);
4901 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
4903 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
4904 stream_nr, GST_FOURCC_ARGS (tag));
4905 avi->offset += 8 + GST_ROUND_UP_2 (size);
4906 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4908 GstAviStream *stream;
4909 GstClockTime next_ts = 0;
4910 GstBuffer *buf = NULL;
4912 gboolean saw_desired_kf = stream_nr != avi->main_stream
4913 || avi->offset >= avi->seek_kf_offset;
4915 if (stream_nr == avi->main_stream && avi->offset == avi->seek_kf_offset) {
4916 GST_DEBUG_OBJECT (avi, "Desired keyframe reached");
4917 avi->seek_kf_offset = 0;
4920 if (saw_desired_kf) {
4921 gst_adapter_flush (avi->adapter, 8);
4924 buf = gst_adapter_take_buffer (avi->adapter, GST_ROUND_UP_2 (size));
4925 /* patch the size */
4926 GST_BUFFER_SIZE (buf) = size;
4931 GST_DEBUG_OBJECT (avi,
4932 "Desired keyframe not yet reached, flushing chunk");
4933 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4936 offset = avi->offset;
4937 avi->offset += 8 + GST_ROUND_UP_2 (size);
4939 stream = &avi->stream[stream_nr];
4941 /* set delay (if any)
4942 if (stream->strh->init_frames == stream->current_frame &&
4944 stream->delay = next_ts;
4947 /* parsing of corresponding header may have failed */
4948 if (G_UNLIKELY (!stream->pad)) {
4949 GST_WARNING_OBJECT (avi, "no pad for stream ID %" GST_FOURCC_FORMAT,
4950 GST_FOURCC_ARGS (tag));
4952 gst_buffer_unref (buf);
4954 /* get time of this buffer */
4955 gst_pad_query_position (stream->pad, &format, (gint64 *) & next_ts);
4956 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4959 gst_avi_demux_add_assoc (avi, stream, next_ts, offset, FALSE);
4961 /* increment our positions */
4962 stream->current_entry++;
4963 stream->current_total += size;
4965 /* update current position in the segment */
4966 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, next_ts);
4968 if (saw_desired_kf && buf) {
4969 GstClockTime dur_ts = 0;
4971 /* invert the picture if needed */
4972 buf = gst_avi_demux_invert (stream, buf);
4974 gst_pad_query_position (stream->pad, &format, (gint64 *) & dur_ts);
4975 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4978 GST_BUFFER_TIMESTAMP (buf) = next_ts;
4979 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
4980 if (stream->strh->type == GST_RIFF_FCC_vids) {
4981 GST_BUFFER_OFFSET (buf) = stream->current_entry - 1;
4982 GST_BUFFER_OFFSET_END (buf) = stream->current_entry;
4984 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4985 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
4988 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4989 GST_DEBUG_OBJECT (avi,
4990 "Pushing buffer with time=%" GST_TIME_FORMAT ", duration %"
4991 GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
4992 " and size %d over pad %s", GST_TIME_ARGS (next_ts),
4993 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
4994 GST_BUFFER_OFFSET (buf), size, GST_PAD_NAME (stream->pad));
4996 /* mark discont when pending */
4997 if (G_UNLIKELY (stream->discont)) {
4998 GST_DEBUG_OBJECT (avi, "Setting DISCONT");
4999 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
5000 stream->discont = FALSE;
5002 res = gst_pad_push (stream->pad, buf);
5006 res = gst_avi_demux_combine_flows (avi, stream, res);
5007 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5008 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
5022 GST_DEBUG_OBJECT (avi, "format %s != GST_FORMAT_TIME",
5023 gst_format_get_name (format));
5024 res = GST_FLOW_ERROR;
5030 * Send pending tags.
5033 push_tag_lists (GstAviDemux * avi)
5041 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
5043 for (i = 0; i < avi->num_streams; i++) {
5044 GstAviStream *stream = &avi->stream[i];
5045 GstPad *pad = stream->pad;
5047 tags = stream->taglist;
5050 GST_DEBUG_OBJECT (pad, "Tags: %" GST_PTR_FORMAT, tags);
5052 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (avi), pad, tags);
5053 stream->taglist = NULL;
5057 if (!(tags = avi->globaltags))
5058 tags = gst_tag_list_new ();
5060 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
5061 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
5063 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, tags);
5064 gst_element_found_tags (GST_ELEMENT_CAST (avi), tags);
5065 avi->globaltags = NULL;
5066 avi->got_tags = FALSE;
5070 gst_avi_demux_loop (GstPad * pad)
5073 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
5075 switch (avi->state) {
5076 case GST_AVI_DEMUX_START:
5077 res = gst_avi_demux_stream_init_pull (avi);
5078 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5079 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5082 avi->state = GST_AVI_DEMUX_HEADER;
5084 case GST_AVI_DEMUX_HEADER:
5085 res = gst_avi_demux_stream_header_pull (avi);
5086 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5087 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5090 avi->state = GST_AVI_DEMUX_MOVI;
5092 case GST_AVI_DEMUX_MOVI:
5093 if (G_UNLIKELY (avi->close_seg_event)) {
5094 gst_avi_demux_push_event (avi, avi->close_seg_event);
5095 avi->close_seg_event = NULL;
5097 if (G_UNLIKELY (avi->seg_event)) {
5098 gst_avi_demux_push_event (avi, avi->seg_event);
5099 avi->seg_event = NULL;
5101 if (G_UNLIKELY (avi->got_tags)) {
5102 push_tag_lists (avi);
5104 /* process each index entry in turn */
5105 res = gst_avi_demux_loop_data (avi);
5107 /* pause when error */
5108 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5109 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
5114 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
5115 res = GST_FLOW_ERROR;
5124 gboolean push_eos = FALSE;
5125 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
5126 avi->segment_running = FALSE;
5127 gst_pad_pause_task (avi->sinkpad);
5130 if (res == GST_FLOW_UNEXPECTED) {
5131 /* handle end-of-stream/segment */
5132 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5135 if ((stop = avi->segment.stop) == -1)
5136 stop = avi->segment.duration;
5138 GST_INFO_OBJECT (avi, "sending segment_done");
5140 gst_element_post_message
5141 (GST_ELEMENT_CAST (avi),
5142 gst_message_new_segment_done (GST_OBJECT_CAST (avi),
5143 GST_FORMAT_TIME, stop));
5147 } else if (res == GST_FLOW_NOT_LINKED || res < GST_FLOW_UNEXPECTED) {
5148 /* for fatal errors we post an error message, wrong-state is
5149 * not fatal because it happens due to flushes and only means
5150 * that we should stop now. */
5151 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
5152 (_("Internal data stream error.")),
5153 ("streaming stopped, reason %s", gst_flow_get_name (res)));
5157 GST_INFO_OBJECT (avi, "sending eos");
5158 if (!gst_avi_demux_push_event (avi, gst_event_new_eos ()) &&
5159 (res == GST_FLOW_UNEXPECTED)) {
5160 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
5161 (NULL), ("got eos but no streams (yet)"));
5168 static GstFlowReturn
5169 gst_avi_demux_chain (GstPad * pad, GstBuffer * buf)
5172 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
5175 if (GST_BUFFER_IS_DISCONT (buf)) {
5176 GST_DEBUG_OBJECT (avi, "got DISCONT");
5177 gst_adapter_clear (avi->adapter);
5178 /* mark all streams DISCONT */
5179 for (i = 0; i < avi->num_streams; i++)
5180 avi->stream[i].discont = TRUE;
5183 GST_DEBUG ("Store %d bytes in adapter", GST_BUFFER_SIZE (buf));
5184 gst_adapter_push (avi->adapter, buf);
5186 switch (avi->state) {
5187 case GST_AVI_DEMUX_START:
5188 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
5189 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5193 case GST_AVI_DEMUX_HEADER:
5194 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
5195 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5199 case GST_AVI_DEMUX_MOVI:
5200 if (G_UNLIKELY (avi->close_seg_event)) {
5201 gst_avi_demux_push_event (avi, avi->close_seg_event);
5202 avi->close_seg_event = NULL;
5204 if (G_UNLIKELY (avi->seg_event)) {
5205 gst_avi_demux_push_event (avi, avi->seg_event);
5206 avi->seg_event = NULL;
5208 if (G_UNLIKELY (avi->got_tags)) {
5209 push_tag_lists (avi);
5211 res = gst_avi_demux_stream_data (avi);
5213 case GST_AVI_DEMUX_SEEK:
5219 /* obtain and parse indexes */
5220 if (avi->stream[0].indexes && !gst_avi_demux_read_subindexes_push (avi))
5221 /* seek in subindex read function failed */
5224 if (!avi->stream[0].indexes && !avi->have_index
5225 && avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
5226 gst_avi_demux_stream_index_push (avi);
5228 if (avi->have_index) {
5229 /* use the indexes now to construct nice durations */
5230 gst_avi_demux_calculate_durations_from_index (avi);
5232 /* still parsing indexes */
5236 GST_OBJECT_LOCK (avi);
5237 event = avi->seek_event;
5238 avi->seek_event = NULL;
5239 GST_OBJECT_UNLOCK (avi);
5241 /* calculate and perform seek */
5242 if (!avi_demux_handle_seek_push (avi, avi->sinkpad, event))
5245 gst_event_unref (event);
5246 avi->state = GST_AVI_DEMUX_MOVI;
5250 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
5251 ("Illegal internal state"));
5252 res = GST_FLOW_ERROR;
5256 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
5257 gst_flow_get_name (res));
5259 if (G_UNLIKELY (avi->abort_buffering))
5260 goto abort_buffering;
5267 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("failed to read indexes"));
5268 return GST_FLOW_ERROR;
5272 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("push mode seek failed"));
5273 return GST_FLOW_ERROR;
5277 avi->abort_buffering = FALSE;
5278 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
5279 return GST_FLOW_ERROR;
5284 gst_avi_demux_sink_activate (GstPad * sinkpad)
5286 if (gst_pad_check_pull_range (sinkpad)) {
5287 GST_DEBUG ("going to pull mode");
5288 return gst_pad_activate_pull (sinkpad, TRUE);
5290 GST_DEBUG ("going to push (streaming) mode");
5291 return gst_pad_activate_push (sinkpad, TRUE);
5296 gst_avi_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
5298 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (sinkpad));
5301 avi->segment_running = TRUE;
5302 avi->streaming = FALSE;
5303 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
5306 avi->segment_running = FALSE;
5307 return gst_pad_stop_task (sinkpad);
5312 gst_avi_demux_activate_push (GstPad * pad, gboolean active)
5314 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (pad));
5317 GST_DEBUG ("avi: activating push/chain function");
5318 avi->streaming = TRUE;
5320 /* create index for some push based seeking if not provided */
5321 GST_OBJECT_LOCK (avi);
5322 if (!avi->element_index) {
5323 GST_DEBUG_OBJECT (avi, "creating index");
5324 avi->element_index = gst_index_factory_make ("memindex");
5326 GST_OBJECT_UNLOCK (avi);
5327 /* object lock might be taken again */
5328 gst_index_get_writer_id (avi->element_index, GST_OBJECT_CAST (avi),
5332 GST_DEBUG ("avi: deactivating push/chain function");
5339 gst_avi_demux_set_index (GstElement * element, GstIndex * index)
5341 GstAviDemux *avi = GST_AVI_DEMUX (element);
5343 GST_OBJECT_LOCK (avi);
5344 if (avi->element_index)
5345 gst_object_unref (avi->element_index);
5347 avi->element_index = gst_object_ref (index);
5349 avi->element_index = NULL;
5351 GST_OBJECT_UNLOCK (avi);
5352 /* object lock might be taken again */
5354 gst_index_get_writer_id (index, GST_OBJECT_CAST (element), &avi->index_id);
5355 GST_DEBUG_OBJECT (avi, "Set index %" GST_PTR_FORMAT, avi->element_index);
5359 gst_avi_demux_get_index (GstElement * element)
5361 GstIndex *result = NULL;
5362 GstAviDemux *avi = GST_AVI_DEMUX (element);
5364 GST_OBJECT_LOCK (avi);
5365 if (avi->element_index)
5366 result = gst_object_ref (avi->element_index);
5367 GST_OBJECT_UNLOCK (avi);
5369 GST_DEBUG_OBJECT (avi, "Returning index %" GST_PTR_FORMAT, result);
5374 static GstStateChangeReturn
5375 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
5377 GstStateChangeReturn ret;
5378 GstAviDemux *avi = GST_AVI_DEMUX (element);
5380 switch (transition) {
5381 case GST_STATE_CHANGE_READY_TO_PAUSED:
5382 avi->streaming = FALSE;
5383 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
5389 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5390 if (ret == GST_STATE_CHANGE_FAILURE)
5393 switch (transition) {
5394 case GST_STATE_CHANGE_PAUSED_TO_READY:
5395 avi->have_index = FALSE;
5396 gst_avi_demux_reset (avi);