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_finalize (GObject * object);
77 static void gst_avi_demux_reset (GstAviDemux * avi);
80 static const GstEventMask *gst_avi_demux_get_event_mask (GstPad * pad);
82 static gboolean gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event);
83 static gboolean gst_avi_demux_handle_sink_event (GstPad * pad,
85 static gboolean gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event);
88 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
90 static gboolean gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query);
91 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
92 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
94 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
95 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
97 static gboolean gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
99 static void gst_avi_demux_loop (GstPad * pad);
100 static gboolean gst_avi_demux_sink_activate (GstPad * sinkpad);
101 static gboolean gst_avi_demux_sink_activate_pull (GstPad * sinkpad,
103 static gboolean gst_avi_demux_activate_push (GstPad * pad, gboolean active);
104 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstBuffer * buf);
106 static void gst_avi_demux_set_index (GstElement * element, GstIndex * index);
107 static GstIndex *gst_avi_demux_get_index (GstElement * element);
108 static GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
109 GstStateChange transition);
110 static void gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi);
111 static void gst_avi_demux_get_buffer_info (GstAviDemux * avi,
112 GstAviStream * stream, guint entry_n, GstClockTime * timestamp,
113 GstClockTime * ts_end, guint64 * offset, guint64 * offset_end);
115 static void gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf);
117 /* GObject methods */
119 #define gst_avi_demux_parent_class parent_class
120 G_DEFINE_TYPE (GstAviDemux, gst_avi_demux, GST_TYPE_ELEMENT);
123 gst_avi_demux_class_init (GstAviDemuxClass * klass)
125 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
126 GObjectClass *gobject_class = (GObjectClass *) klass;
127 GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl;
128 GstCaps *audcaps, *vidcaps, *subcaps;
130 GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
131 0, "Demuxer for AVI streams");
133 gobject_class->finalize = gst_avi_demux_finalize;
135 gstelement_class->change_state =
136 GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
137 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_avi_demux_set_index);
138 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_avi_demux_get_index);
140 audcaps = gst_riff_create_audio_template_caps ();
141 gst_caps_append (audcaps, gst_caps_new_empty_simple ("audio/x-avi-unknown"));
142 audiosrctempl = gst_pad_template_new ("audio_%u",
143 GST_PAD_SRC, GST_PAD_SOMETIMES, audcaps);
145 vidcaps = gst_riff_create_video_template_caps ();
146 gst_caps_append (vidcaps, gst_riff_create_iavs_template_caps ());
147 gst_caps_append (vidcaps, gst_caps_new_empty_simple ("video/x-avi-unknown"));
148 videosrctempl = gst_pad_template_new ("video_%u",
149 GST_PAD_SRC, GST_PAD_SOMETIMES, vidcaps);
151 subcaps = gst_caps_new_empty_simple ("application/x-subtitle-avi");
152 subsrctempl = gst_pad_template_new ("subtitle_%u",
153 GST_PAD_SRC, GST_PAD_SOMETIMES, subcaps);
154 gst_element_class_add_pad_template (gstelement_class, audiosrctempl);
155 gst_element_class_add_pad_template (gstelement_class, videosrctempl);
156 gst_element_class_add_pad_template (gstelement_class, subsrctempl);
157 gst_element_class_add_pad_template (gstelement_class,
158 gst_static_pad_template_get (&sink_templ));
160 gst_element_class_set_details_simple (gstelement_class, "Avi demuxer",
162 "Demultiplex an avi file into audio and video",
163 "Erik Walthinsen <omega@cse.ogi.edu>, "
164 "Wim Taymans <wim.taymans@chello.be>, "
165 "Thijs Vermeir <thijsvermeir@gmail.com>");
169 gst_avi_demux_init (GstAviDemux * avi)
171 avi->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
172 gst_pad_set_activate_function (avi->sinkpad,
173 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate));
174 gst_pad_set_activatepull_function (avi->sinkpad,
175 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate_pull));
176 gst_pad_set_activatepush_function (avi->sinkpad,
177 GST_DEBUG_FUNCPTR (gst_avi_demux_activate_push));
178 gst_pad_set_chain_function (avi->sinkpad,
179 GST_DEBUG_FUNCPTR (gst_avi_demux_chain));
180 gst_pad_set_event_function (avi->sinkpad,
181 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_sink_event));
182 gst_element_add_pad (GST_ELEMENT_CAST (avi), avi->sinkpad);
184 avi->adapter = gst_adapter_new ();
186 gst_avi_demux_reset (avi);
190 gst_avi_demux_finalize (GObject * object)
192 GstAviDemux *avi = GST_AVI_DEMUX (object);
194 GST_DEBUG ("AVI: finalize");
196 g_object_unref (avi->adapter);
198 G_OBJECT_CLASS (parent_class)->finalize (object);
202 gst_avi_demux_reset_stream (GstAviDemux * avi, GstAviStream * stream)
204 g_free (stream->strh);
205 g_free (stream->strf.data);
206 g_free (stream->name);
207 g_free (stream->index);
208 g_free (stream->indexes);
209 if (stream->initdata)
210 gst_buffer_unref (stream->initdata);
211 if (stream->extradata)
212 gst_buffer_unref (stream->extradata);
214 if (stream->exposed) {
215 gst_pad_set_active (stream->pad, FALSE);
216 gst_element_remove_pad (GST_ELEMENT_CAST (avi), stream->pad);
218 gst_object_unref (stream->pad);
220 if (stream->taglist) {
221 gst_tag_list_free (stream->taglist);
222 stream->taglist = NULL;
224 memset (stream, 0, sizeof (GstAviStream));
228 gst_avi_demux_reset (GstAviDemux * avi)
232 GST_DEBUG ("AVI: reset");
234 for (i = 0; i < avi->num_streams; i++)
235 gst_avi_demux_reset_stream (avi, &avi->stream[i]);
237 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
238 avi->num_streams = 0;
239 avi->num_v_streams = 0;
240 avi->num_a_streams = 0;
241 avi->num_t_streams = 0;
242 avi->main_stream = -1;
244 avi->state = GST_AVI_DEMUX_START;
246 avi->building_index = FALSE;
248 avi->index_offset = 0;
252 if (avi->element_index)
253 gst_object_unref (avi->element_index);
254 avi->element_index = NULL;
256 if (avi->seg_event) {
257 gst_event_unref (avi->seg_event);
258 avi->seg_event = NULL;
260 if (avi->seek_event) {
261 gst_event_unref (avi->seek_event);
262 avi->seek_event = NULL;
266 gst_tag_list_free (avi->globaltags);
267 avi->globaltags = NULL;
269 avi->got_tags = TRUE; /* we always want to push global tags */
270 avi->have_eos = FALSE;
271 avi->seekable = TRUE;
273 gst_adapter_clear (avi->adapter);
275 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
279 /* GstElement methods */
282 static const GstFormat *
283 gst_avi_demux_get_src_formats (GstPad * pad)
285 GstAviStream *stream = gst_pad_get_element_private (pad);
287 static const GstFormat src_a_formats[] = {
293 static const GstFormat src_v_formats[] = {
299 return (stream->strh->type == GST_RIFF_FCC_auds ?
300 src_a_formats : src_v_formats);
304 /* assumes stream->strf.auds->av_bps != 0 */
305 static inline GstClockTime
306 avi_stream_convert_bytes_to_time_unchecked (GstAviStream * stream,
309 return gst_util_uint64_scale_int (bytes, GST_SECOND,
310 stream->strf.auds->av_bps);
313 static inline guint64
314 avi_stream_convert_time_to_bytes_unchecked (GstAviStream * stream,
317 return gst_util_uint64_scale_int (time, stream->strf.auds->av_bps,
321 /* assumes stream->strh->rate != 0 */
322 static inline GstClockTime
323 avi_stream_convert_frames_to_time_unchecked (GstAviStream * stream,
326 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
330 static inline guint64
331 avi_stream_convert_time_to_frames_unchecked (GstAviStream * stream,
334 return gst_util_uint64_scale (time, stream->strh->rate,
335 stream->strh->scale * GST_SECOND);
339 gst_avi_demux_src_convert (GstPad * pad,
340 GstFormat src_format,
341 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
343 GstAviStream *stream = gst_pad_get_element_private (pad);
347 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
348 ", dest_format:%s", gst_format_get_name (src_format), src_value,
349 gst_format_get_name (*dest_format));
351 if (G_UNLIKELY (src_format == *dest_format)) {
352 *dest_value = src_value;
355 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
359 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
360 (src_format == GST_FORMAT_BYTES
361 || *dest_format == GST_FORMAT_BYTES))) {
366 switch (src_format) {
367 case GST_FORMAT_TIME:
368 switch (*dest_format) {
369 case GST_FORMAT_BYTES:
370 *dest_value = gst_util_uint64_scale_int (src_value,
371 stream->strf.auds->av_bps, GST_SECOND);
373 case GST_FORMAT_DEFAULT:
375 gst_util_uint64_scale_round (src_value, stream->strh->rate,
376 stream->strh->scale * GST_SECOND);
383 case GST_FORMAT_BYTES:
384 switch (*dest_format) {
385 case GST_FORMAT_TIME:
386 if (stream->strf.auds->av_bps != 0) {
387 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
397 case GST_FORMAT_DEFAULT:
398 switch (*dest_format) {
399 case GST_FORMAT_TIME:
401 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
414 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
415 gst_format_get_name (*dest_format), *dest_value);
420 gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query)
423 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
425 GstAviStream *stream = gst_pad_get_element_private (pad);
427 if (!stream->strh || !stream->strf.data)
428 return gst_pad_query_default (pad, query);
430 switch (GST_QUERY_TYPE (query)) {
431 case GST_QUERY_POSITION:{
434 GST_DEBUG ("pos query for stream %u: frames %u, bytes %u",
435 stream->num, stream->current_entry, stream->current_total);
437 /* FIXME, this looks clumsy */
438 if (stream->strh->type == GST_RIFF_FCC_auds) {
439 if (stream->is_vbr) {
441 pos = gst_util_uint64_scale ((gint64) stream->current_entry *
442 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
443 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
444 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
445 } else if (stream->strf.auds->av_bps != 0) {
447 pos = gst_util_uint64_scale (stream->current_total, GST_SECOND,
448 (guint64) stream->strf.auds->av_bps);
449 GST_DEBUG_OBJECT (avi,
450 "CBR convert bytes %u, time %" GST_TIME_FORMAT,
451 stream->current_total, GST_TIME_ARGS (pos));
452 } else if (stream->idx_n != 0 && stream->total_bytes != 0) {
453 /* calculate timestamps based on percentage of length */
454 guint64 xlen = avi->avih->us_frame *
455 avi->avih->tot_frames * GST_USECOND;
457 if (stream->is_vbr) {
458 pos = gst_util_uint64_scale (xlen, stream->current_entry,
460 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
461 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
463 pos = gst_util_uint64_scale (xlen, stream->current_total,
464 stream->total_bytes);
465 GST_DEBUG_OBJECT (avi,
466 "CBR perc convert bytes %u, time %" GST_TIME_FORMAT,
467 stream->current_total, GST_TIME_ARGS (pos));
474 if (stream->strh->rate != 0) {
475 pos = gst_util_uint64_scale ((guint64) stream->current_entry *
476 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
478 pos = stream->current_entry * avi->avih->us_frame * GST_USECOND;
482 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
483 gst_query_set_position (query, GST_FORMAT_TIME, pos);
485 GST_WARNING ("pos query failed");
488 case GST_QUERY_DURATION:
491 GstClockTime duration;
493 /* only act on audio or video streams */
494 if (stream->strh->type != GST_RIFF_FCC_auds &&
495 stream->strh->type != GST_RIFF_FCC_vids) {
500 /* take stream duration, fall back to avih duration */
501 if ((duration = stream->duration) == -1)
502 duration = avi->duration;
504 gst_query_parse_duration (query, &fmt, NULL);
507 case GST_FORMAT_TIME:
508 gst_query_set_duration (query, fmt, duration);
510 case GST_FORMAT_DEFAULT:
513 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
516 if (stream->idx_n >= 0)
517 gst_query_set_duration (query, fmt, stream->idx_n);
518 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
519 duration, fmt, &dur))
520 gst_query_set_duration (query, fmt, dur);
529 case GST_QUERY_SEEKING:{
532 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
533 if (fmt == GST_FORMAT_TIME) {
534 gboolean seekable = TRUE;
536 if (avi->streaming) {
537 seekable = avi->seekable;
540 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
541 0, stream->duration);
546 case GST_QUERY_CONVERT:{
547 GstFormat src_fmt, dest_fmt;
548 gint64 src_val, dest_val;
550 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
551 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
553 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
555 res = gst_pad_query_default (pad, query);
559 res = gst_pad_query_default (pad, query);
563 gst_object_unref (avi);
568 static const GstEventMask *
569 gst_avi_demux_get_event_mask (GstPad * pad)
571 static const GstEventMask masks[] = {
572 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
581 gst_avi_demux_seek_streams (GstAviDemux * avi, guint64 offset, gboolean before)
583 GstAviStream *stream;
584 GstIndexEntry *entry;
586 gint64 val, min = offset;
588 for (i = 0; i < avi->num_streams; i++) {
589 stream = &avi->stream[i];
591 entry = gst_index_get_assoc_entry (avi->element_index, stream->index_id,
592 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
593 GST_ASSOCIATION_FLAG_NONE, GST_FORMAT_BYTES, offset);
597 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
598 GST_DEBUG_OBJECT (avi, "stream %d, previous entry at %"
599 G_GUINT64_FORMAT, i, val);
607 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
608 stream->current_entry = 0;
609 stream->current_total = 0;
613 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
614 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT,
617 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &val);
618 stream->current_total = val;
619 gst_index_entry_assoc_map (entry, GST_FORMAT_DEFAULT, &val);
620 stream->current_entry = val;
627 gst_avi_demux_index_entry_offset_search (GstAviIndexEntry * entry,
630 if (entry->offset < *offset)
632 else if (entry->offset > *offset)
638 gst_avi_demux_seek_streams_index (GstAviDemux * avi, guint64 offset,
641 GstAviStream *stream;
642 GstAviIndexEntry *entry;
644 gint64 val, min = offset;
647 for (i = 0; i < avi->num_streams; i++) {
648 stream = &avi->stream[i];
650 /* compensate for chunk header */
653 gst_util_array_binary_search (stream->index, stream->idx_n,
654 sizeof (GstAviIndexEntry),
655 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
656 before ? GST_SEARCH_MODE_BEFORE : GST_SEARCH_MODE_AFTER, &offset, NULL);
660 index = entry - stream->index;
664 val = stream->index[index].offset;
665 GST_DEBUG_OBJECT (avi,
666 "stream %d, previous entry at %" G_GUINT64_FORMAT, i, val);
674 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
675 stream->current_entry = 0;
676 stream->current_total = 0;
680 val = stream->index[index].offset - 8;
681 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT, i,
684 stream->current_total = stream->index[index].total;
685 stream->current_entry = index;
691 #define GST_AVI_SEEK_PUSH_DISPLACE (4 * GST_SECOND)
694 gst_avi_demux_handle_sink_event (GstPad * pad, GstEvent * event)
697 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
699 GST_DEBUG_OBJECT (avi,
700 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
702 switch (GST_EVENT_TYPE (event)) {
703 case GST_EVENT_SEGMENT:
705 gint64 boffset, offset = 0;
708 /* some debug output */
709 gst_event_copy_segment (event, &segment);
710 GST_DEBUG_OBJECT (avi, "received newsegment %" GST_SEGMENT_FORMAT,
713 /* chain will send initial newsegment after pads have been added */
714 if (avi->state != GST_AVI_DEMUX_MOVI) {
715 GST_DEBUG_OBJECT (avi, "still starting, eating event");
719 /* we only expect a BYTE segment, e.g. following a seek */
720 if (segment.format != GST_FORMAT_BYTES) {
721 GST_DEBUG_OBJECT (avi, "unsupported segment format, ignoring");
725 if (avi->have_index) {
726 GstAviIndexEntry *entry;
727 guint i = 0, index = 0, k = 0;
728 GstAviStream *stream;
730 /* compensate chunk header, stored index offset points after header */
731 boffset = segment.start + 8;
732 /* find which stream we're on */
734 stream = &avi->stream[i];
736 /* find the index for start bytes offset */
737 entry = gst_util_array_binary_search (stream->index,
738 stream->idx_n, sizeof (GstAviIndexEntry),
739 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
740 GST_SEARCH_MODE_AFTER, &boffset, NULL);
744 index = entry - stream->index;
746 /* we are on the stream with a chunk start offset closest to start */
747 if (!offset || stream->index[index].offset < offset) {
748 offset = stream->index[index].offset;
751 /* exact match needs no further searching */
752 if (stream->index[index].offset == segment.start)
754 } while (++i < avi->num_streams);
757 stream = &avi->stream[k];
759 /* so we have no idea what is to come, or where we are */
761 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
765 /* get the ts corresponding to start offset bytes for the stream */
766 gst_avi_demux_get_buffer_info (avi, stream, index,
767 (GstClockTime *) & segment.time, NULL, NULL, NULL);
768 } else if (avi->element_index) {
769 GstIndexEntry *entry;
771 /* Let's check if we have an index entry for this position */
772 entry = gst_index_get_assoc_entry (avi->element_index, avi->index_id,
773 GST_INDEX_LOOKUP_AFTER, GST_ASSOCIATION_FLAG_NONE,
774 GST_FORMAT_BYTES, segment.start);
776 /* we can not go where we have not yet been before ... */
778 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
782 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME,
783 (gint64 *) & segment.time);
784 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &offset);
786 GST_WARNING_OBJECT (avi, "no index data, forcing EOS");
790 segment.format = GST_FORMAT_TIME;
791 segment.start = segment.time;
792 segment.stop = GST_CLOCK_TIME_NONE;
793 segment.position = segment.start;
795 /* rescue duration */
796 segment.duration = avi->segment.duration;
798 /* set up segment and send downstream */
799 gst_segment_copy_into (&segment, &avi->segment);
801 GST_DEBUG_OBJECT (avi, "Pushing newseg %" GST_SEGMENT_FORMAT, &segment);
802 gst_avi_demux_push_event (avi, gst_event_new_segment (&segment));
804 GST_DEBUG_OBJECT (avi, "next chunk expected at %" G_GINT64_FORMAT,
807 /* adjust state for streaming thread accordingly */
809 gst_avi_demux_seek_streams_index (avi, offset, FALSE);
811 gst_avi_demux_seek_streams (avi, offset, FALSE);
813 /* set up streaming thread */
814 g_assert (offset >= boffset);
815 avi->offset = boffset;
816 avi->todrop = offset - boffset;
819 gst_event_unref (event);
824 avi->have_eos = TRUE;
829 if (avi->state != GST_AVI_DEMUX_MOVI) {
830 gst_event_unref (event);
831 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
832 (NULL), ("got eos and didn't receive a complete header object"));
833 } else if (!gst_avi_demux_push_event (avi, event)) {
834 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
835 (NULL), ("got eos but no streams (yet)"));
839 case GST_EVENT_FLUSH_STOP:
843 gst_adapter_clear (avi->adapter);
844 avi->have_eos = FALSE;
845 for (i = 0; i < avi->num_streams; i++) {
846 avi->stream[i].last_flow = GST_FLOW_OK;
847 avi->stream[i].discont = TRUE;
849 /* fall through to default case so that the event gets passed downstream */
852 res = gst_pad_event_default (pad, event);
856 gst_object_unref (avi);
862 gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event)
865 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
867 GST_DEBUG_OBJECT (avi,
868 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
870 switch (GST_EVENT_TYPE (event)) {
872 if (!avi->streaming) {
873 res = gst_avi_demux_handle_seek (avi, pad, event);
875 res = gst_avi_demux_handle_seek_push (avi, pad, event);
877 gst_event_unref (event);
880 case GST_EVENT_NAVIGATION:
882 gst_event_unref (event);
885 res = gst_pad_event_default (pad, event);
889 gst_object_unref (avi);
894 /* streaming helper (push) */
897 * gst_avi_demux_peek_chunk_info:
899 * @tag: holder for tag
900 * @size: holder for tag size
902 * Peek next chunk info (tag and size)
904 * Returns: TRUE when one chunk info has been got
907 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
909 const guint8 *data = NULL;
911 if (gst_adapter_available (avi->adapter) < 8)
914 data = gst_adapter_map (avi->adapter, 8);
915 *tag = GST_READ_UINT32_LE (data);
916 *size = GST_READ_UINT32_LE (data + 4);
917 gst_adapter_unmap (avi->adapter, 0);
923 * gst_avi_demux_peek_chunk:
925 * @tag: holder for tag
926 * @size: holder for tag size
928 * Peek enough data for one full chunk
930 * Returns: %TRUE when one chunk has been got
933 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
935 guint32 peek_size = 0;
938 if (!gst_avi_demux_peek_chunk_info (avi, tag, size))
941 /* size 0 -> empty data buffer would surprise most callers,
942 * large size -> do not bother trying to squeeze that into adapter,
943 * so we throw poor man's exception, which can be caught if caller really
944 * wants to handle 0 size chunk */
945 if (!(*size) || (*size) >= (1 << 30))
948 peek_size = (*size + 1) & ~1;
949 available = gst_adapter_available (avi->adapter);
951 GST_DEBUG_OBJECT (avi,
952 "Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
953 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
955 if (available < (8 + peek_size))
963 GST_INFO_OBJECT (avi, "Failed to peek");
968 GST_INFO_OBJECT (avi,
969 "Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT, *size,
970 GST_FOURCC_ARGS (*tag));
971 /* chain should give up */
972 avi->abort_buffering = TRUE;
977 GST_INFO_OBJECT (avi, "need more %d < %" G_GUINT32_FORMAT,
978 available, 8 + peek_size);
986 * gst_avi_demux_parse_file_header:
987 * @element: caller element (used for errors/debug).
988 * @buf: input data to be used for parsing.
990 * "Open" a RIFF/AVI file. The buffer should be at least 12
991 * bytes long. Takes ownership of @buf.
993 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
994 * Throws an error, caller should error out (fatal).
997 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
1002 stamp = gst_util_get_timestamp ();
1004 /* riff_parse posts an error */
1005 if (!gst_riff_parse_file_header (element, buf, &doctype))
1008 if (doctype != GST_RIFF_RIFF_AVI)
1011 stamp = gst_util_get_timestamp () - stamp;
1012 GST_DEBUG_OBJECT (element, "header parsing took %" GST_TIME_FORMAT,
1013 GST_TIME_ARGS (stamp));
1020 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
1021 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
1022 GST_FOURCC_ARGS (doctype)));
1028 * Read AVI file tag when streaming
1030 static GstFlowReturn
1031 gst_avi_demux_stream_init_push (GstAviDemux * avi)
1033 if (gst_adapter_available (avi->adapter) >= 12) {
1036 tmp = gst_adapter_take_buffer (avi->adapter, 12);
1038 GST_DEBUG ("Parsing avi header");
1039 if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), tmp)) {
1040 return GST_FLOW_ERROR;
1042 GST_DEBUG ("header ok");
1045 avi->state = GST_AVI_DEMUX_HEADER;
1053 static GstFlowReturn
1054 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
1057 GstBuffer *buf = NULL;
1059 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
1060 if (res != GST_FLOW_OK)
1062 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
1072 GST_DEBUG_OBJECT (avi, "error parsing file header");
1073 return GST_FLOW_ERROR;
1077 /* AVI header handling */
1079 * gst_avi_demux_parse_avih:
1080 * @avi: caller element (used for errors/debug).
1081 * @buf: input data to be used for parsing.
1082 * @avih: pointer to structure (filled in by function) containing
1083 * stream information (such as flags, number of streams, etc.).
1085 * Read 'avih' header. Discards buffer after use.
1087 * Returns: TRUE on success, FALSE otherwise. Throws an error if
1088 * the header is invalid. The caller should error out
1092 gst_avi_demux_parse_avih (GstAviDemux * avi,
1093 GstBuffer * buf, gst_riff_avih ** _avih)
1095 gst_riff_avih *avih;
1101 size = gst_buffer_get_size (buf);
1102 if (size < sizeof (gst_riff_avih))
1103 goto avih_too_small;
1105 avih = g_malloc (size);
1106 gst_buffer_extract (buf, 0, avih, size);
1108 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1109 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
1110 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
1111 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
1112 avih->flags = GUINT32_FROM_LE (avih->flags);
1113 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
1114 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
1115 avih->streams = GUINT32_FROM_LE (avih->streams);
1116 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
1117 avih->width = GUINT32_FROM_LE (avih->width);
1118 avih->height = GUINT32_FROM_LE (avih->height);
1119 avih->scale = GUINT32_FROM_LE (avih->scale);
1120 avih->rate = GUINT32_FROM_LE (avih->rate);
1121 avih->start = GUINT32_FROM_LE (avih->start);
1122 avih->length = GUINT32_FROM_LE (avih->length);
1126 GST_INFO_OBJECT (avi, "avih tag found:");
1127 GST_INFO_OBJECT (avi, " us_frame %u", avih->us_frame);
1128 GST_INFO_OBJECT (avi, " max_bps %u", avih->max_bps);
1129 GST_INFO_OBJECT (avi, " pad_gran %u", avih->pad_gran);
1130 GST_INFO_OBJECT (avi, " flags 0x%08x", avih->flags);
1131 GST_INFO_OBJECT (avi, " tot_frames %u", avih->tot_frames);
1132 GST_INFO_OBJECT (avi, " init_frames %u", avih->init_frames);
1133 GST_INFO_OBJECT (avi, " streams %u", avih->streams);
1134 GST_INFO_OBJECT (avi, " bufsize %u", avih->bufsize);
1135 GST_INFO_OBJECT (avi, " width %u", avih->width);
1136 GST_INFO_OBJECT (avi, " height %u", avih->height);
1137 GST_INFO_OBJECT (avi, " scale %u", avih->scale);
1138 GST_INFO_OBJECT (avi, " rate %u", avih->rate);
1139 GST_INFO_OBJECT (avi, " start %u", avih->start);
1140 GST_INFO_OBJECT (avi, " length %u", avih->length);
1143 gst_buffer_unref (buf);
1145 if (avih->us_frame != 0 && avih->tot_frames != 0)
1147 (guint64) avih->us_frame * (guint64) avih->tot_frames * 1000;
1149 avi->duration = GST_CLOCK_TIME_NONE;
1151 GST_INFO_OBJECT (avi, " header duration %" GST_TIME_FORMAT,
1152 GST_TIME_ARGS (avi->duration));
1159 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No buffer"));
1164 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
1165 ("Too small avih (%d available, %d needed)",
1166 size, (int) sizeof (gst_riff_avih)));
1167 gst_buffer_unref (buf);
1173 * gst_avi_demux_parse_superindex:
1174 * @avi: caller element (used for debugging/errors).
1175 * @buf: input data to use for parsing.
1176 * @locations: locations in the file (byte-offsets) that contain
1177 * the actual indexes (see get_avi_demux_parse_subindex()).
1178 * The array ends with GST_BUFFER_OFFSET_NONE.
1180 * Reads superindex (openDML-2 spec stuff) from the provided data.
1182 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
1183 * on error, but they are not fatal.
1186 gst_avi_demux_parse_superindex (GstAviDemux * avi,
1187 GstBuffer * buf, guint64 ** _indexes)
1198 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
1207 /* check type of index. The opendml2 specs state that
1208 * there should be 4 dwords per array entry. Type can be
1209 * either frame or field (and we don't care). */
1210 if (GST_READ_UINT16_LE (data) != 4 ||
1211 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
1212 GST_WARNING_OBJECT (avi,
1213 "Superindex for stream has unexpected "
1214 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1215 GST_READ_UINT16_LE (data), data[2], data[3]);
1216 bpe = GST_READ_UINT16_LE (data) * 4;
1218 num = GST_READ_UINT32_LE (&data[4]);
1220 GST_DEBUG_OBJECT (avi, "got %d indexes", num);
1222 /* this can't work out well ... */
1223 if (num > G_MAXUINT32 >> 1 || bpe < 8) {
1224 goto invalid_params;
1227 indexes = g_new (guint64, num + 1);
1228 for (i = 0; i < num; i++) {
1229 if (size < 24 + bpe * (i + 1))
1231 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
1232 GST_DEBUG_OBJECT (avi, "index %d at %" G_GUINT64_FORMAT, i, indexes[i]);
1234 indexes[i] = GST_BUFFER_OFFSET_NONE;
1235 *_indexes = indexes;
1237 gst_buffer_unmap (buf, data, size);
1238 gst_buffer_unref (buf);
1245 GST_ERROR_OBJECT (avi,
1246 "Not enough data to parse superindex (%d available, 24 needed)", size);
1248 gst_buffer_unmap (buf, data, size);
1249 gst_buffer_unref (buf);
1255 GST_ERROR_OBJECT (avi, "invalid index parameters (num = %d, bpe = %d)",
1257 gst_buffer_unmap (buf, data, size);
1258 gst_buffer_unref (buf);
1263 /* add an entry to the index of a stream. @num should be an estimate of the
1264 * total amount of index entries for all streams and is used to dynamically
1265 * allocate memory for the index entries. */
1266 static inline gboolean
1267 gst_avi_demux_add_index (GstAviDemux * avi, GstAviStream * stream,
1268 guint num, GstAviIndexEntry * entry)
1270 /* ensure index memory */
1271 if (G_UNLIKELY (stream->idx_n >= stream->idx_max)) {
1272 guint idx_max = stream->idx_max;
1273 GstAviIndexEntry *new_idx;
1275 /* we need to make some more room */
1277 /* initial size guess, assume each stream has an equal amount of entries,
1278 * overshoot with at least 8K */
1279 idx_max = (num / avi->num_streams) + (8192 / sizeof (GstAviIndexEntry));
1281 idx_max += 8192 / sizeof (GstAviIndexEntry);
1282 GST_DEBUG_OBJECT (avi, "expanded index from %u to %u",
1283 stream->idx_max, idx_max);
1285 new_idx = g_try_renew (GstAviIndexEntry, stream->index, idx_max);
1286 /* out of memory, if this fails stream->index is untouched. */
1287 if (G_UNLIKELY (!new_idx))
1290 stream->index = new_idx;
1291 stream->idx_max = idx_max;
1294 /* update entry total and stream stats. The entry total can be converted to
1295 * the timestamp of the entry easily. */
1296 if (stream->strh->type == GST_RIFF_FCC_auds) {
1299 if (stream->is_vbr) {
1300 entry->total = stream->total_blocks;
1302 entry->total = stream->total_bytes;
1304 blockalign = stream->strf.auds->blockalign;
1306 stream->total_blocks += DIV_ROUND_UP (entry->size, blockalign);
1308 stream->total_blocks++;
1310 if (stream->is_vbr) {
1311 entry->total = stream->idx_n;
1313 entry->total = stream->total_bytes;
1316 stream->total_bytes += entry->size;
1317 if (ENTRY_IS_KEYFRAME (entry))
1318 stream->n_keyframes++;
1321 GST_LOG_OBJECT (avi,
1322 "Adding stream %u, index entry %d, kf %d, size %u "
1323 ", offset %" G_GUINT64_FORMAT ", total %" G_GUINT64_FORMAT, stream->num,
1324 stream->idx_n, ENTRY_IS_KEYFRAME (entry), entry->size, entry->offset,
1326 stream->index[stream->idx_n++] = *entry;
1331 /* given @entry_n in @stream, calculate info such as timestamps and
1332 * offsets for the entry. */
1334 gst_avi_demux_get_buffer_info (GstAviDemux * avi, GstAviStream * stream,
1335 guint entry_n, GstClockTime * timestamp, GstClockTime * ts_end,
1336 guint64 * offset, guint64 * offset_end)
1338 GstAviIndexEntry *entry;
1340 entry = &stream->index[entry_n];
1342 if (stream->is_vbr) {
1343 /* VBR stream next timestamp */
1344 if (stream->strh->type == GST_RIFF_FCC_auds) {
1347 avi_stream_convert_frames_to_time_unchecked (stream, entry->total);
1349 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1354 avi_stream_convert_frames_to_time_unchecked (stream, entry_n);
1356 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1359 } else if (stream->strh->type == GST_RIFF_FCC_auds) {
1360 /* constant rate stream */
1363 avi_stream_convert_bytes_to_time_unchecked (stream, entry->total);
1365 *ts_end = avi_stream_convert_bytes_to_time_unchecked (stream,
1366 entry->total + entry->size);
1368 if (stream->strh->type == GST_RIFF_FCC_vids) {
1369 /* video offsets are the frame number */
1373 *offset_end = entry_n + 1;
1375 /* no offsets for audio */
1383 /* collect and debug stats about the indexes for all streams.
1384 * This method is also responsible for filling in the stream duration
1385 * as measured by the amount of index entries.
1387 * Returns TRUE if the index is not empty, else FALSE */
1389 gst_avi_demux_do_index_stats (GstAviDemux * avi)
1391 guint total_idx = 0;
1393 #ifndef GST_DISABLE_GST_DEBUG
1394 guint total_max = 0;
1397 /* get stream stats now */
1398 for (i = 0; i < avi->num_streams; i++) {
1399 GstAviStream *stream;
1401 if (G_UNLIKELY (!(stream = &avi->stream[i])))
1403 if (G_UNLIKELY (!stream->strh))
1405 if (G_UNLIKELY (!stream->index || stream->idx_n == 0))
1408 /* we interested in the end_ts of the last entry, which is the total
1409 * duration of this stream */
1410 gst_avi_demux_get_buffer_info (avi, stream, stream->idx_n - 1,
1411 NULL, &stream->idx_duration, NULL, NULL);
1413 total_idx += stream->idx_n;
1414 #ifndef GST_DISABLE_GST_DEBUG
1415 total_max += stream->idx_max;
1417 GST_INFO_OBJECT (avi, "Stream %d, dur %" GST_TIME_FORMAT ", %6u entries, "
1418 "%5u keyframes, entry size = %2u, total size = %10u, allocated %10u",
1419 i, GST_TIME_ARGS (stream->idx_duration), stream->idx_n,
1420 stream->n_keyframes, (guint) sizeof (GstAviIndexEntry),
1421 (guint) (stream->idx_n * sizeof (GstAviIndexEntry)),
1422 (guint) (stream->idx_max * sizeof (GstAviIndexEntry)));
1424 total_idx *= sizeof (GstAviIndexEntry);
1425 #ifndef GST_DISABLE_GST_DEBUG
1426 total_max *= sizeof (GstAviIndexEntry);
1428 GST_INFO_OBJECT (avi, "%u bytes for index vs %u ideally, %u wasted",
1429 total_max, total_idx, total_max - total_idx);
1431 if (total_idx == 0) {
1432 GST_WARNING_OBJECT (avi, "Index is empty !");
1439 * gst_avi_demux_parse_subindex:
1441 * @buf: input data to use for parsing.
1442 * @stream: stream context.
1443 * @entries_list: a list (returned by the function) containing all the
1444 * indexes parsed in this specific subindex. The first
1445 * entry is also a pointer to allocated memory that needs
1446 * to be free´ed. May be NULL if no supported indexes were
1449 * Reads superindex (openDML-2 spec stuff) from the provided data.
1450 * The buffer should contain a GST_RIFF_TAG_ix?? chunk.
1452 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1453 * throw an error, caller should bail out asap.
1456 gst_avi_demux_parse_subindex (GstAviDemux * avi, GstAviStream * stream,
1468 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
1473 /* We don't support index-data yet */
1475 goto not_implemented;
1477 /* check type of index. The opendml2 specs state that
1478 * there should be 4 dwords per array entry. Type can be
1479 * either frame or field (and we don't care). */
1480 bpe = (data[2] & 0x01) ? 12 : 8;
1481 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1482 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1483 GST_WARNING_OBJECT (avi,
1484 "Superindex for stream %d has unexpected "
1485 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1486 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1487 bpe = GST_READ_UINT16_LE (data) * 4;
1489 num = GST_READ_UINT32_LE (&data[4]);
1490 baseoff = GST_READ_UINT64_LE (&data[12]);
1492 /* If there's nothing, just return ! */
1496 GST_INFO_OBJECT (avi, "Parsing subindex, nr_entries = %6d", num);
1498 for (i = 0; i < num; i++) {
1499 GstAviIndexEntry entry;
1501 if (size < 24 + bpe * (i + 1))
1504 /* fill in offset and size. offset contains the keyframe flag in the
1506 entry.offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1507 entry.size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1509 if (stream->strh->type == GST_RIFF_FCC_auds) {
1510 /* all audio frames are keyframes */
1511 ENTRY_SET_KEYFRAME (&entry);
1513 /* else read flags */
1514 entry.flags = (entry.size & 0x80000000) ? 0 : GST_AVI_KEYFRAME;
1516 entry.size &= ~0x80000000;
1519 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
1523 gst_buffer_unmap (buf, data, size);
1524 gst_buffer_unref (buf);
1531 GST_ERROR_OBJECT (avi,
1532 "Not enough data to parse subindex (%d available, 24 needed)", size);
1533 goto done; /* continue */
1537 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1538 ("Subindex-is-data is not implemented"));
1539 gst_buffer_unmap (buf, data, size);
1540 gst_buffer_unref (buf);
1545 GST_DEBUG_OBJECT (avi, "the index is empty");
1546 goto done; /* continue */
1550 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1551 ("Cannot allocate memory for %u*%u=%u bytes",
1552 (guint) sizeof (GstAviIndexEntry), num,
1553 (guint) sizeof (GstAviIndexEntry) * num));
1554 gst_buffer_unmap (buf, data, size);
1555 gst_buffer_unref (buf);
1561 * Create and push a flushing seek event upstream
1564 perform_seek_to_offset (GstAviDemux * demux, guint64 offset)
1569 GST_DEBUG_OBJECT (demux, "Seeking to %" G_GUINT64_FORMAT, offset);
1572 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1573 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1574 GST_SEEK_TYPE_NONE, -1);
1576 res = gst_pad_push_event (demux->sinkpad, event);
1579 demux->offset = offset;
1584 * Read AVI index when streaming
1587 gst_avi_demux_read_subindexes_push (GstAviDemux * avi)
1589 guint32 tag = 0, size;
1590 GstBuffer *buf = NULL;
1593 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1595 if (avi->odml_subidxs[avi->odml_subidx] != avi->offset)
1598 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1601 /* this is the ODML chunk we expect */
1602 odml_stream = avi->odml_stream;
1604 if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + odml_stream / 10,
1605 '0' + odml_stream % 10)) &&
1606 (tag != GST_MAKE_FOURCC ('0' + odml_stream / 10,
1607 '0' + odml_stream % 10, 'i', 'x'))) {
1608 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1609 GST_FOURCC_ARGS (tag));
1613 avi->offset += 8 + GST_ROUND_UP_2 (size);
1614 /* flush chunk header so we get just the 'size' payload data */
1615 gst_adapter_flush (avi->adapter, 8);
1616 buf = gst_adapter_take_buffer (avi->adapter, size);
1618 if (!gst_avi_demux_parse_subindex (avi, &avi->stream[odml_stream], buf))
1621 /* we parsed the index, go to next subindex */
1624 if (avi->odml_subidxs[avi->odml_subidx] == GST_BUFFER_OFFSET_NONE) {
1625 /* we reached the end of the indexes for this stream, move to the next
1626 * stream to handle the first index */
1628 avi->odml_subidx = 0;
1630 if (avi->odml_stream < avi->num_streams) {
1631 /* there are more indexes */
1632 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
1634 /* we're done, get stream stats now */
1635 avi->have_index = gst_avi_demux_do_index_stats (avi);
1641 /* seek to next index */
1642 return perform_seek_to_offset (avi, avi->odml_subidxs[avi->odml_subidx]);
1649 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi)
1655 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1657 for (n = 0; n < avi->num_streams; n++) {
1658 GstAviStream *stream = &avi->stream[n];
1660 if (stream->indexes == NULL)
1663 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1664 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi), avi->sinkpad,
1665 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1667 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1668 '0' + stream->num % 10)) &&
1669 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1670 '0' + stream->num % 10, 'i', 'x'))) {
1671 /* Some ODML files (created by god knows what muxer) have a ##ix format
1672 * instead of the 'official' ix##. They are still valid though. */
1673 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1674 GST_FOURCC_ARGS (tag));
1675 gst_buffer_unref (buf);
1679 if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1683 g_free (stream->indexes);
1684 stream->indexes = NULL;
1686 /* get stream stats now */
1687 avi->have_index = gst_avi_demux_do_index_stats (avi);
1691 * gst_avi_demux_riff_parse_vprp:
1692 * @element: caller element (used for debugging/error).
1693 * @buf: input data to be used for parsing, stripped from header.
1694 * @vprp: a pointer (returned by this function) to a filled-in vprp
1695 * structure. Caller should free it.
1697 * Parses a video stream´s vprp. This function takes ownership of @buf.
1699 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1700 * should be skipped on error, but it is not fatal.
1703 gst_avi_demux_riff_parse_vprp (GstElement * element,
1704 GstBuffer * buf, gst_riff_vprp ** _vprp)
1706 gst_riff_vprp *vprp;
1710 g_return_val_if_fail (buf != NULL, FALSE);
1711 g_return_val_if_fail (_vprp != NULL, FALSE);
1713 size = gst_buffer_get_size (buf);
1715 if (size < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1718 vprp = g_malloc (size);
1719 gst_buffer_extract (buf, 0, vprp, size);
1721 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1722 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1723 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1724 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1725 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1726 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1727 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1728 vprp->width = GUINT32_FROM_LE (vprp->width);
1729 vprp->height = GUINT32_FROM_LE (vprp->height);
1730 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1734 /* calculate fields based on size */
1735 k = (size - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) / vprp->fields;
1736 if (vprp->fields > k) {
1737 GST_WARNING_OBJECT (element,
1738 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1741 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1742 GST_WARNING_OBJECT (element,
1743 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1744 GST_RIFF_VPRP_VIDEO_FIELDS);
1745 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1747 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1748 for (k = 0; k < vprp->fields; k++) {
1749 gst_riff_vprp_video_field_desc *fd;
1751 fd = &vprp->field_info[k];
1752 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1753 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1754 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1755 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1756 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1757 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1758 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1759 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1764 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1765 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1766 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1767 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1768 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1769 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1770 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1771 vprp->aspect & 0xffff);
1772 GST_INFO_OBJECT (element, " width %d", vprp->width);
1773 GST_INFO_OBJECT (element, " height %d", vprp->height);
1774 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1775 for (k = 0; k < vprp->fields; k++) {
1776 gst_riff_vprp_video_field_desc *fd;
1778 fd = &(vprp->field_info[k]);
1779 GST_INFO_OBJECT (element, " field %u description:", k);
1780 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1781 fd->compressed_bm_height);
1782 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1783 fd->compressed_bm_width);
1784 GST_INFO_OBJECT (element, " valid_bm_height %d",
1785 fd->valid_bm_height);
1786 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1787 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1788 fd->valid_bm_x_offset);
1789 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1790 fd->valid_bm_y_offset);
1791 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1792 fd->video_x_t_offset);
1793 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1796 gst_buffer_unref (buf);
1805 GST_ERROR_OBJECT (element,
1806 "Too small vprp (%d available, at least %d needed)",
1807 size, (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1808 gst_buffer_unref (buf);
1814 gst_avi_demux_expose_streams (GstAviDemux * avi, gboolean force)
1818 GST_DEBUG_OBJECT (avi, "force : %d", force);
1820 for (i = 0; i < avi->num_streams; i++) {
1821 GstAviStream *stream = &avi->stream[i];
1823 if (force || stream->idx_n != 0) {
1824 GST_LOG_OBJECT (avi, "Adding pad %s" GST_PTR_FORMAT,
1825 GST_PAD_NAME (stream->pad));
1826 gst_element_add_pad ((GstElement *) avi, stream->pad);
1828 if (avi->element_index)
1829 gst_index_get_writer_id (avi->element_index,
1830 GST_OBJECT_CAST (stream->pad), &stream->index_id);
1832 stream->exposed = TRUE;
1833 if (avi->main_stream == -1)
1834 avi->main_stream = i;
1836 GST_WARNING_OBJECT (avi, "Stream #%d doesn't have any entry, removing it",
1838 gst_avi_demux_reset_stream (avi, stream);
1843 /* buf contains LIST chunk data, and will be padded to even size,
1844 * since some buggy files do not account for the padding of chunks
1845 * within a LIST in the size of the LIST */
1847 gst_avi_demux_roundup_list (GstAviDemux * avi, GstBuffer ** buf)
1851 size = gst_buffer_get_size (*buf);
1853 if (G_UNLIKELY (size & 1)) {
1857 GST_DEBUG_OBJECT (avi, "rounding up dubious list size %d", size);
1858 obuf = gst_buffer_new_and_alloc (size + 1);
1860 data = gst_buffer_map (obuf, NULL, NULL, GST_MAP_WRITE);
1861 gst_buffer_extract (*buf, 0, data, size);
1862 /* assume 0 padding, at least makes outcome deterministic */
1864 gst_buffer_unmap (obuf, data, size + 1);
1865 gst_buffer_replace (buf, obuf);
1870 * gst_avi_demux_parse_stream:
1871 * @avi: calling element (used for debugging/errors).
1872 * @buf: input buffer used to parse the stream.
1874 * Parses all subchunks in a strl chunk (which defines a single
1875 * stream). Discards the buffer after use. This function will
1876 * increment the stream counter internally.
1878 * Returns: whether the stream was identified successfully.
1879 * Errors are not fatal. It does indicate the stream
1883 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
1885 GstAviStream *stream;
1886 GstElementClass *klass;
1887 GstPadTemplate *templ;
1888 GstBuffer *sub = NULL;
1891 gchar *codec_name = NULL, *padname = NULL;
1892 const gchar *tag_name;
1893 GstCaps *caps = NULL;
1895 GstElement *element;
1896 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
1897 gst_riff_vprp *vprp = NULL;
1899 element = GST_ELEMENT_CAST (avi);
1901 GST_DEBUG_OBJECT (avi, "Parsing stream");
1903 gst_avi_demux_roundup_list (avi, &buf);
1905 if (avi->num_streams >= GST_AVI_DEMUX_MAX_STREAMS) {
1906 GST_WARNING_OBJECT (avi,
1907 "maximum no of streams (%d) exceeded, ignoring stream",
1908 GST_AVI_DEMUX_MAX_STREAMS);
1909 gst_buffer_unref (buf);
1910 /* not a fatal error, let's say */
1914 stream = &avi->stream[avi->num_streams];
1916 /* initial settings */
1917 stream->idx_duration = GST_CLOCK_TIME_NONE;
1918 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1919 stream->duration = GST_CLOCK_TIME_NONE;
1921 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
1922 /* sub can be NULL if the chunk is empty */
1924 GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
1925 GST_FOURCC_ARGS (tag));
1929 case GST_RIFF_TAG_strh:
1931 gst_riff_strh *strh;
1934 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
1937 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
1938 /* ownership given away */
1940 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
1944 strh = stream->strh;
1945 /* sanity check; stream header frame rate matches global header
1947 if (stream->strh->type == GST_RIFF_FCC_vids) {
1949 GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
1951 s_dur = gst_util_uint64_scale (GST_SECOND, strh->scale, strh->rate);
1952 GST_DEBUG_OBJECT (avi, "verifying stream framerate %d/%d, "
1953 "frame duration = %d ms", strh->rate, strh->scale,
1954 (gint) (s_dur / GST_MSECOND));
1955 if (h_dur > (10 * GST_MSECOND) && (s_dur > 10 * h_dur)) {
1956 strh->rate = GST_SECOND / GST_USECOND;
1957 strh->scale = h_dur / GST_USECOND;
1958 GST_DEBUG_OBJECT (avi, "correcting stream framerate to %d/%d",
1959 strh->rate, strh->scale);
1962 /* determine duration as indicated by header */
1963 stream->hdr_duration = gst_util_uint64_scale ((guint64) strh->length *
1964 strh->scale, GST_SECOND, (guint64) strh->rate);
1965 GST_INFO ("Stream duration according to header: %" GST_TIME_FORMAT,
1966 GST_TIME_ARGS (stream->hdr_duration));
1967 if (stream->hdr_duration == 0)
1968 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1973 case GST_RIFF_TAG_strf:
1975 gboolean res = FALSE;
1978 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
1982 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
1985 switch (stream->strh->type) {
1986 case GST_RIFF_FCC_vids:
1987 stream->is_vbr = TRUE;
1988 res = gst_riff_parse_strf_vids (element, sub,
1989 &stream->strf.vids, &stream->extradata);
1991 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
1993 case GST_RIFF_FCC_auds:
1995 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
1996 &stream->extradata);
2000 stream->is_vbr = (stream->strh->samplesize == 0)
2001 && stream->strh->scale > 1
2002 && stream->strf.auds->blockalign != 1;
2003 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
2004 stream->is_vbr, res);
2005 /* we need these or we have no way to come up with timestamps */
2006 if ((!stream->is_vbr && !stream->strf.auds->av_bps) ||
2007 (stream->is_vbr && (!stream->strh->scale ||
2008 !stream->strh->rate))) {
2009 GST_WARNING_OBJECT (element,
2010 "invalid audio header, ignoring stream");
2013 /* some more sanity checks */
2014 if (stream->is_vbr) {
2015 if (stream->strf.auds->blockalign <= 4) {
2016 /* that would mean (too) many frames per chunk,
2017 * so not likely set as expected */
2018 GST_DEBUG_OBJECT (element,
2019 "suspicious blockalign %d for VBR audio; "
2020 "overriding to 1 frame per chunk",
2021 stream->strf.auds->blockalign);
2022 /* this should top any likely value */
2023 stream->strf.auds->blockalign = (1 << 12);
2027 case GST_RIFF_FCC_iavs:
2028 stream->is_vbr = TRUE;
2029 res = gst_riff_parse_strf_iavs (element, sub,
2030 &stream->strf.iavs, &stream->extradata);
2032 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
2034 case GST_RIFF_FCC_txts:
2035 /* nothing to parse here */
2036 stream->is_vbr = (stream->strh->samplesize == 0)
2037 && (stream->strh->scale > 1);
2041 GST_ERROR_OBJECT (avi,
2042 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
2043 GST_FOURCC_ARGS (stream->strh->type));
2047 gst_buffer_unref (sub);
2055 case GST_RIFF_TAG_vprp:
2058 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
2062 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
2066 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
2070 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
2071 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
2072 /* not considered fatal */
2080 case GST_RIFF_TAG_strd:
2081 if (stream->initdata)
2082 gst_buffer_unref (stream->initdata);
2083 stream->initdata = sub;
2086 case GST_RIFF_TAG_strn:
2087 g_free (stream->name);
2092 bdata = gst_buffer_map (sub, &bsize, NULL, GST_MAP_READ);
2093 stream->name = g_strndup (bdata, bsize);
2094 gst_buffer_unmap (sub, bdata, bsize);
2095 gst_buffer_unref (sub);
2098 stream->name = g_strdup ("");
2100 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
2103 gst_avi_demux_parse_idit (avi, sub);
2106 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
2107 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
2108 '0' + avi->num_streams % 10)) {
2109 g_free (stream->indexes);
2110 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
2111 stream->superindex = TRUE;
2115 GST_WARNING_OBJECT (avi,
2116 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
2117 GST_FOURCC_ARGS (tag));
2119 case GST_RIFF_TAG_JUNQ:
2120 case GST_RIFF_TAG_JUNK:
2124 gst_buffer_unref (sub);
2130 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
2135 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
2139 /* get class to figure out the template */
2140 klass = GST_ELEMENT_GET_CLASS (avi);
2142 /* we now have all info, let´s set up a pad and a caps and be done */
2143 /* create stream name + pad */
2144 switch (stream->strh->type) {
2145 case GST_RIFF_FCC_vids:{
2148 fourcc = (stream->strf.vids->compression) ?
2149 stream->strf.vids->compression : stream->strh->fcc_handler;
2150 padname = g_strdup_printf ("video_%u", avi->num_v_streams);
2151 templ = gst_element_class_get_pad_template (klass, "video_%u");
2152 caps = gst_riff_create_video_caps (fourcc, stream->strh,
2153 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
2155 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2156 G_TYPE_INT, fourcc, NULL);
2157 } else if (got_vprp && vprp) {
2158 guint32 aspect_n, aspect_d;
2161 aspect_n = vprp->aspect >> 16;
2162 aspect_d = vprp->aspect & 0xffff;
2163 /* calculate the pixel aspect ratio using w/h and aspect ratio */
2164 n = aspect_n * stream->strf.vids->height;
2165 d = aspect_d * stream->strf.vids->width;
2167 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
2169 /* very local, not needed elsewhere */
2173 tag_name = GST_TAG_VIDEO_CODEC;
2174 avi->num_v_streams++;
2177 case GST_RIFF_FCC_auds:{
2178 padname = g_strdup_printf ("audio_%u", avi->num_a_streams);
2179 templ = gst_element_class_get_pad_template (klass, "audio_%u");
2180 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
2181 stream->strh, stream->strf.auds, stream->extradata,
2182 stream->initdata, &codec_name);
2184 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
2185 G_TYPE_INT, stream->strf.auds->format, NULL);
2187 tag_name = GST_TAG_AUDIO_CODEC;
2188 avi->num_a_streams++;
2191 case GST_RIFF_FCC_iavs:{
2192 guint32 fourcc = stream->strh->fcc_handler;
2194 padname = g_strdup_printf ("video_%u", avi->num_v_streams);
2195 templ = gst_element_class_get_pad_template (klass, "video_%u");
2196 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
2197 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
2199 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2200 G_TYPE_INT, fourcc, NULL);
2202 tag_name = GST_TAG_VIDEO_CODEC;
2203 avi->num_v_streams++;
2206 case GST_RIFF_FCC_txts:{
2207 padname = g_strdup_printf ("subtitle_%u", avi->num_t_streams);
2208 templ = gst_element_class_get_pad_template (klass, "subtitle_%u");
2209 caps = gst_caps_new_empty_simple ("application/x-subtitle-avi");
2211 avi->num_t_streams++;
2215 g_return_val_if_reached (FALSE);
2218 /* no caps means no stream */
2220 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
2224 GST_DEBUG_OBJECT (element, "codec-name=%s",
2225 (codec_name ? codec_name : "NULL"));
2226 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
2228 /* set proper settings and add it */
2230 gst_object_unref (stream->pad);
2231 pad = stream->pad = gst_pad_new_from_template (templ, padname);
2234 gst_pad_use_fixed_caps (pad);
2236 gst_pad_set_formats_function (pad,
2237 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
2238 gst_pad_set_event_mask_function (pad,
2239 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
2241 gst_pad_set_event_function (pad,
2242 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
2243 gst_pad_set_query_function (pad,
2244 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
2246 gst_pad_set_convert_function (pad,
2247 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
2250 stream->num = avi->num_streams;
2252 stream->start_entry = 0;
2253 stream->step_entry = 0;
2254 stream->stop_entry = 0;
2256 stream->current_entry = -1;
2257 stream->current_total = 0;
2259 stream->last_flow = GST_FLOW_OK;
2260 stream->discont = TRUE;
2262 stream->total_bytes = 0;
2263 stream->total_blocks = 0;
2264 stream->n_keyframes = 0;
2267 stream->idx_max = 0;
2269 gst_pad_set_element_private (pad, stream);
2272 gst_pad_set_active (pad, TRUE);
2273 gst_pad_push_event (pad, gst_event_new_caps (caps));
2274 gst_caps_unref (caps);
2278 if (!stream->taglist)
2279 stream->taglist = gst_tag_list_new_empty ();
2281 avi->got_tags = TRUE;
2283 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
2285 g_free (codec_name);
2288 gst_buffer_unref (buf);
2295 /* unref any mem that may be in use */
2297 gst_buffer_unref (buf);
2299 gst_buffer_unref (sub);
2301 g_free (codec_name);
2302 gst_avi_demux_reset_stream (avi, stream);
2309 * gst_avi_demux_parse_odml:
2310 * @avi: calling element (used for debug/error).
2311 * @buf: input buffer to be used for parsing.
2313 * Read an openDML-2.0 extension header. Fills in the frame number
2314 * in the avi demuxer object when reading succeeds.
2317 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
2321 GstBuffer *sub = NULL;
2323 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
2326 case GST_RIFF_TAG_dmlh:{
2327 gst_riff_dmlh dmlh, *_dmlh;
2331 /* sub == NULL is possible and means an empty buffer */
2335 data = gst_buffer_map (sub, &size, NULL, GST_MAP_READ);
2338 if (size < sizeof (gst_riff_dmlh)) {
2339 GST_ERROR_OBJECT (avi,
2340 "DMLH entry is too small (%d bytes, %d needed)",
2341 size, (int) sizeof (gst_riff_dmlh));
2342 gst_buffer_unmap (sub, data, size);
2345 _dmlh = (gst_riff_dmlh *) data;
2346 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
2347 gst_buffer_unmap (sub, data, size);
2349 GST_INFO_OBJECT (avi, "dmlh tag found: totalframes: %u",
2352 avi->avih->tot_frames = dmlh.totalframes;
2357 GST_WARNING_OBJECT (avi,
2358 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
2359 GST_FOURCC_ARGS (tag));
2361 case GST_RIFF_TAG_JUNQ:
2362 case GST_RIFF_TAG_JUNK:
2364 /* skip and move to next chunk */
2366 gst_buffer_unref (sub);
2373 gst_buffer_unref (buf);
2378 gst_avi_demux_index_last (GstAviDemux * avi, GstAviStream * stream)
2380 return stream->idx_n;
2383 /* find a previous entry in the index with the given flags */
2385 gst_avi_demux_index_prev (GstAviDemux * avi, GstAviStream * stream,
2386 guint last, gboolean keyframe)
2388 GstAviIndexEntry *entry;
2391 for (i = last; i > 0; i--) {
2392 entry = &stream->index[i - 1];
2393 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2401 gst_avi_demux_index_next (GstAviDemux * avi, GstAviStream * stream,
2402 guint last, gboolean keyframe)
2404 GstAviIndexEntry *entry;
2407 for (i = last + 1; i < stream->idx_n; i++) {
2408 entry = &stream->index[i];
2409 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2413 return stream->idx_n - 1;
2417 gst_avi_demux_index_entry_search (GstAviIndexEntry * entry, guint64 * total)
2419 if (entry->total < *total)
2421 else if (entry->total > *total)
2427 * gst_avi_demux_index_for_time:
2429 * @stream: the stream
2430 * @time: a time position
2432 * Finds the index entry which time is less or equal than the requested time.
2433 * Try to avoid binary search when we can convert the time to an index
2434 * position directly (for example for video frames with a fixed duration).
2436 * Returns: the found position in the index.
2439 gst_avi_demux_index_for_time (GstAviDemux * avi,
2440 GstAviStream * stream, guint64 time)
2445 GST_LOG_OBJECT (avi, "search time:%" GST_TIME_FORMAT, GST_TIME_ARGS (time));
2447 /* easy (and common) cases */
2448 if (time == 0 || stream->idx_n == 0)
2450 if (time >= stream->idx_duration)
2451 return stream->idx_n - 1;
2453 /* figure out where we need to go. For that we convert the time to an
2454 * index entry or we convert it to a total and then do a binary search. */
2455 if (stream->is_vbr) {
2456 /* VBR stream next timestamp */
2457 if (stream->strh->type == GST_RIFF_FCC_auds) {
2458 total = avi_stream_convert_time_to_frames_unchecked (stream, time);
2460 index = avi_stream_convert_time_to_frames_unchecked (stream, time);
2463 /* constant rate stream */
2464 total = avi_stream_convert_time_to_bytes_unchecked (stream, time);
2468 GstAviIndexEntry *entry;
2470 /* no index, find index with binary search on total */
2471 GST_LOG_OBJECT (avi, "binary search for entry with total %"
2472 G_GUINT64_FORMAT, total);
2474 entry = gst_util_array_binary_search (stream->index,
2475 stream->idx_n, sizeof (GstAviIndexEntry),
2476 (GCompareDataFunc) gst_avi_demux_index_entry_search,
2477 GST_SEARCH_MODE_BEFORE, &total, NULL);
2479 if (entry == NULL) {
2480 GST_LOG_OBJECT (avi, "not found, assume index 0");
2483 index = entry - stream->index;
2484 GST_LOG_OBJECT (avi, "found at %u", index);
2487 GST_LOG_OBJECT (avi, "converted time to index %u", index);
2493 static inline GstAviStream *
2494 gst_avi_demux_stream_for_id (GstAviDemux * avi, guint32 id)
2497 GstAviStream *stream;
2499 /* get the stream for this entry */
2500 stream_nr = CHUNKID_TO_STREAMNR (id);
2501 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2502 GST_WARNING_OBJECT (avi, "invalid stream nr %d", stream_nr);
2505 stream = &avi->stream[stream_nr];
2506 if (G_UNLIKELY (!stream->strh)) {
2507 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2514 * gst_avi_demux_parse_index:
2515 * @avi: calling element (used for debugging/errors).
2516 * @buf: buffer containing the full index.
2518 * Read index entries from the provided buffer.
2519 * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
2522 gst_avi_demux_parse_index (GstAviDemux * avi, GstBuffer * buf)
2527 gst_riff_index_entry *index;
2529 GstAviStream *stream;
2530 GstAviIndexEntry entry;
2536 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
2538 stamp = gst_util_get_timestamp ();
2540 /* see how many items in the index */
2541 num = size / sizeof (gst_riff_index_entry);
2545 GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2547 index = (gst_riff_index_entry *) data;
2549 /* figure out if the index is 0 based or relative to the MOVI start */
2550 entry.offset = GST_READ_UINT32_LE (&index[0].offset);
2551 if (entry.offset < avi->offset) {
2552 avi->index_offset = avi->offset + 8;
2553 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2555 avi->index_offset = 0;
2556 GST_DEBUG ("index is 0 based");
2559 for (i = 0, n = 0; i < num; i++) {
2560 id = GST_READ_UINT32_LE (&index[i].id);
2561 entry.offset = GST_READ_UINT32_LE (&index[i].offset);
2563 /* some sanity checks */
2564 if (G_UNLIKELY (id == GST_RIFF_rec || id == 0 ||
2565 (entry.offset == 0 && n > 0)))
2568 /* get the stream for this entry */
2569 stream = gst_avi_demux_stream_for_id (avi, id);
2570 if (G_UNLIKELY (!stream))
2573 /* handle offset and size */
2574 entry.offset += avi->index_offset + 8;
2575 entry.size = GST_READ_UINT32_LE (&index[i].size);
2578 if (stream->strh->type == GST_RIFF_FCC_auds) {
2579 /* all audio frames are keyframes */
2580 ENTRY_SET_KEYFRAME (&entry);
2583 /* else read flags */
2584 flags = GST_READ_UINT32_LE (&index[i].flags);
2585 if (flags & GST_RIFF_IF_KEYFRAME) {
2586 ENTRY_SET_KEYFRAME (&entry);
2588 ENTRY_UNSET_KEYFRAME (&entry);
2593 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2598 gst_buffer_unmap (buf, data, size);
2599 gst_buffer_unref (buf);
2601 /* get stream stats now */
2602 avi->have_index = gst_avi_demux_do_index_stats (avi);
2604 stamp = gst_util_get_timestamp () - stamp;
2605 GST_DEBUG_OBJECT (avi, "index parsing took %" GST_TIME_FORMAT,
2606 GST_TIME_ARGS (stamp));
2613 GST_DEBUG_OBJECT (avi, "empty index");
2614 gst_buffer_unmap (buf, data, size);
2615 gst_buffer_unref (buf);
2620 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2621 ("Cannot allocate memory for %u*%u=%u bytes",
2622 (guint) sizeof (GstAviIndexEntry), num,
2623 (guint) sizeof (GstAviIndexEntry) * num));
2624 gst_buffer_unmap (buf, data, size);
2625 gst_buffer_unref (buf);
2631 * gst_avi_demux_stream_index:
2632 * @avi: avi demuxer object.
2634 * Seeks to index and reads it.
2637 gst_avi_demux_stream_index (GstAviDemux * avi)
2640 guint64 offset = avi->offset;
2647 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2649 /* get chunk information */
2650 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2651 if (res != GST_FLOW_OK)
2654 bdata = gst_buffer_map (buf, &bsize, NULL, GST_MAP_READ);
2658 /* check tag first before blindy trying to read 'size' bytes */
2659 tag = GST_READ_UINT32_LE (bdata);
2660 size = GST_READ_UINT32_LE (bdata + 4);
2661 if (tag == GST_RIFF_TAG_LIST) {
2662 /* this is the movi tag */
2663 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2664 (8 + GST_ROUND_UP_2 (size)));
2665 offset += 8 + GST_ROUND_UP_2 (size);
2666 gst_buffer_unmap (buf, bdata, bsize);
2667 gst_buffer_unref (buf);
2669 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2670 if (res != GST_FLOW_OK)
2673 bdata = gst_buffer_map (buf, &bsize, NULL, GST_MAP_READ);
2677 tag = GST_READ_UINT32_LE (bdata);
2678 size = GST_READ_UINT32_LE (bdata + 4);
2680 gst_buffer_unmap (buf, bdata, bsize);
2681 gst_buffer_unref (buf);
2683 if (tag != GST_RIFF_TAG_idx1)
2688 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2690 /* read chunk, advance offset */
2691 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2692 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2695 GST_DEBUG ("will parse index chunk size %u for tag %"
2696 GST_FOURCC_FORMAT, gst_buffer_get_size (buf), GST_FOURCC_ARGS (tag));
2698 gst_avi_demux_parse_index (avi, buf);
2700 #ifndef GST_DISABLE_GST_DEBUG
2701 /* debug our indexes */
2704 GstAviStream *stream;
2706 for (i = 0; i < avi->num_streams; i++) {
2707 stream = &avi->stream[i];
2708 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2709 i, stream->idx_n, stream->total_bytes);
2718 GST_DEBUG_OBJECT (avi,
2719 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2724 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2725 gst_buffer_unmap (buf, bdata, bsize);
2726 gst_buffer_unref (buf);
2731 GST_WARNING_OBJECT (avi,
2732 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2733 GST_FOURCC_ARGS (tag));
2738 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2744 * gst_avi_demux_stream_index_push:
2745 * @avi: avi demuxer object.
2750 gst_avi_demux_stream_index_push (GstAviDemux * avi)
2752 guint64 offset = avi->idx1_offset;
2757 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2759 /* get chunk information */
2760 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
2763 /* check tag first before blindly trying to read 'size' bytes */
2764 if (tag == GST_RIFF_TAG_LIST) {
2765 /* this is the movi tag */
2766 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2767 (8 + GST_ROUND_UP_2 (size)));
2768 avi->idx1_offset = offset + 8 + GST_ROUND_UP_2 (size);
2769 /* issue seek to allow chain function to handle it and return! */
2770 perform_seek_to_offset (avi, avi->idx1_offset);
2774 if (tag != GST_RIFF_TAG_idx1)
2777 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2779 /* flush chunk header */
2780 gst_adapter_flush (avi->adapter, 8);
2781 /* read chunk payload */
2782 buf = gst_adapter_take_buffer (avi->adapter, size);
2785 /* advance offset */
2786 offset += 8 + GST_ROUND_UP_2 (size);
2788 GST_DEBUG ("will parse index chunk size %u for tag %"
2789 GST_FOURCC_FORMAT, gst_buffer_get_size (buf), GST_FOURCC_ARGS (tag));
2791 avi->offset = avi->first_movi_offset;
2792 gst_avi_demux_parse_index (avi, buf);
2794 #ifndef GST_DISABLE_GST_DEBUG
2795 /* debug our indexes */
2798 GstAviStream *stream;
2800 for (i = 0; i < avi->num_streams; i++) {
2801 stream = &avi->stream[i];
2802 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2803 i, stream->idx_n, stream->total_bytes);
2812 GST_DEBUG_OBJECT (avi,
2813 "taking data from adapter failed: pos=%" G_GUINT64_FORMAT " size=%u",
2819 GST_WARNING_OBJECT (avi,
2820 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2821 GST_FOURCC_ARGS (tag));
2827 * gst_avi_demux_peek_tag:
2829 * Returns the tag and size of the next chunk
2831 static GstFlowReturn
2832 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2835 GstFlowReturn res = GST_FLOW_OK;
2836 GstBuffer *buf = NULL;
2840 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2841 if (res != GST_FLOW_OK)
2844 bufdata = gst_buffer_map (buf, &bufsize, NULL, GST_MAP_READ);
2848 *tag = GST_READ_UINT32_LE (bufdata);
2849 *size = GST_READ_UINT32_LE (bufdata + 4);
2851 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2852 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2853 *size, offset + 8, offset + 8 + (gint64) * size);
2856 gst_buffer_unmap (buf, bufdata, bufsize);
2857 gst_buffer_unref (buf);
2864 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2869 GST_DEBUG_OBJECT (avi, "got %d bytes which is <> 8 bytes", bufsize);
2870 res = GST_FLOW_ERROR;
2876 * gst_avi_demux_next_data_buffer:
2878 * Returns the offset and size of the next buffer
2879 * Position is the position of the buffer (after tag and size)
2881 static GstFlowReturn
2882 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2883 guint32 * tag, guint * size)
2885 guint64 off = *offset;
2890 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2891 if (res != GST_FLOW_OK)
2893 if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
2894 off += 8 + 4; /* skip tag + size + subtag */
2906 * gst_avi_demux_stream_scan:
2907 * @avi: calling element (used for debugging/errors).
2909 * Scan the file for all chunks to "create" a new index.
2913 gst_avi_demux_stream_scan (GstAviDemux * avi)
2916 GstAviStream *stream;
2924 * - implement non-seekable source support.
2926 GST_DEBUG_OBJECT (avi, "Creating index");
2928 /* get the size of the file */
2929 if (!gst_pad_query_peer_duration (avi->sinkpad, GST_FORMAT_BYTES, &tmplength))
2933 /* guess the total amount of entries we expect */
2937 GstAviIndexEntry entry;
2940 /* start reading data buffers to find the id and offset */
2941 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
2942 if (G_UNLIKELY (res != GST_FLOW_OK))
2946 stream = gst_avi_demux_stream_for_id (avi, tag);
2947 if (G_UNLIKELY (!stream))
2950 /* we can't figure out the keyframes, assume they all are */
2951 entry.flags = GST_AVI_KEYFRAME;
2955 /* and add to the index of this stream */
2956 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2960 /* update position */
2961 pos += GST_ROUND_UP_2 (size);
2962 if (G_UNLIKELY (pos > length)) {
2963 GST_WARNING_OBJECT (avi,
2964 "Stopping index lookup since we are further than EOF");
2970 avi->have_index = gst_avi_demux_do_index_stats (avi);
2977 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2978 ("Cannot allocate memory for %u*%u=%u bytes",
2979 (guint) sizeof (GstAviIndexEntry), num,
2980 (guint) sizeof (GstAviIndexEntry) * num));
2986 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
2990 GstAviStream *stream;
2992 total = GST_CLOCK_TIME_NONE;
2994 /* all streams start at a timestamp 0 */
2995 for (i = 0; i < avi->num_streams; i++) {
2996 GstClockTime duration, hduration;
2997 gst_riff_strh *strh;
2999 stream = &avi->stream[i];
3000 if (G_UNLIKELY (!stream || !stream->idx_n || !(strh = stream->strh)))
3003 /* get header duration for the stream */
3004 hduration = stream->hdr_duration;
3005 /* index duration calculated during parsing */
3006 duration = stream->idx_duration;
3008 /* now pick a good duration */
3009 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3010 /* index gave valid duration, use that */
3011 GST_INFO ("Stream %p duration according to index: %" GST_TIME_FORMAT,
3012 stream, GST_TIME_ARGS (duration));
3014 /* fall back to header info to calculate a duration */
3015 duration = hduration;
3017 GST_INFO ("Setting duration of stream #%d to %" GST_TIME_FORMAT,
3018 i, GST_TIME_ARGS (duration));
3019 /* set duration for the stream */
3020 stream->duration = duration;
3022 /* find total duration */
3023 if (total == GST_CLOCK_TIME_NONE ||
3024 (GST_CLOCK_TIME_IS_VALID (duration) && duration > total))
3028 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
3029 /* now update the duration for those streams where we had none */
3030 for (i = 0; i < avi->num_streams; i++) {
3031 stream = &avi->stream[i];
3033 if (!GST_CLOCK_TIME_IS_VALID (stream->duration)
3034 || stream->duration == 0) {
3035 stream->duration = total;
3037 GST_INFO ("Stream %p duration according to total: %" GST_TIME_FORMAT,
3038 stream, GST_TIME_ARGS (total));
3043 /* and set the total duration in the segment. */
3044 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
3045 GST_TIME_ARGS (total));
3047 avi->segment.duration = total;
3050 /* returns FALSE if there are no pads to deliver event to,
3051 * otherwise TRUE (whatever the outcome of event sending),
3052 * takes ownership of the event. */
3054 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
3056 gboolean result = FALSE;
3059 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
3060 GST_EVENT_TYPE_NAME (event), avi->num_streams);
3062 for (i = 0; i < avi->num_streams; i++) {
3063 GstAviStream *stream = &avi->stream[i];
3067 gst_pad_push_event (stream->pad, gst_event_ref (event));
3070 gst_event_unref (event);
3075 gst_avi_demux_check_seekability (GstAviDemux * avi)
3078 gboolean seekable = FALSE;
3079 gint64 start = -1, stop = -1;
3081 query = gst_query_new_seeking (GST_FORMAT_BYTES);
3082 if (!gst_pad_peer_query (avi->sinkpad, query)) {
3083 GST_DEBUG_OBJECT (avi, "seeking query failed");
3087 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
3089 /* try harder to query upstream size if we didn't get it the first time */
3090 if (seekable && stop == -1) {
3091 GST_DEBUG_OBJECT (avi, "doing duration query to fix up unset stop");
3092 gst_pad_query_peer_duration (avi->sinkpad, GST_FORMAT_BYTES, &stop);
3095 /* if upstream doesn't know the size, it's likely that it's not seekable in
3096 * practice even if it technically may be seekable */
3097 if (seekable && (start != 0 || stop <= start)) {
3098 GST_DEBUG_OBJECT (avi, "seekable but unknown start/stop -> disable");
3103 GST_INFO_OBJECT (avi, "seekable: %d (%" G_GUINT64_FORMAT " - %"
3104 G_GUINT64_FORMAT ")", seekable, start, stop);
3105 avi->seekable = seekable;
3107 gst_query_unref (query);
3111 * Read AVI headers when streaming
3113 static GstFlowReturn
3114 gst_avi_demux_stream_header_push (GstAviDemux * avi)
3116 GstFlowReturn ret = GST_FLOW_OK;
3121 GstBuffer *buf = NULL, *sub = NULL;
3124 GstTagList *tags = NULL;
3127 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
3129 switch (avi->header_state) {
3130 case GST_AVI_DEMUX_HEADER_TAG_LIST:
3131 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3132 avi->offset += 8 + GST_ROUND_UP_2 (size);
3133 if (tag != GST_RIFF_TAG_LIST)
3134 goto header_no_list;
3136 gst_adapter_flush (avi->adapter, 8);
3137 /* Find the 'hdrl' LIST tag */
3138 GST_DEBUG ("Reading %d bytes", size);
3139 buf = gst_adapter_take_buffer (avi->adapter, size);
3141 gst_buffer_extract (buf, 0, fourcc, 4);
3143 if (GST_READ_UINT32_LE (fourcc) != GST_RIFF_LIST_hdrl)
3144 goto header_no_hdrl;
3148 gst_adapter_flush (avi->adapter, 1);
3150 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
3152 gst_avi_demux_roundup_list (avi, &buf);
3154 /* the hdrl starts with a 'avih' header */
3155 if (!gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3157 goto header_no_avih;
3159 if (tag != GST_RIFF_TAG_avih)
3160 goto header_no_avih;
3162 if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3163 goto header_wrong_avih;
3165 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
3167 /* now, read the elements from the header until the end */
3168 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3170 /* sub can be NULL on empty tags */
3175 case GST_RIFF_TAG_LIST:
3176 if (gst_buffer_get_size (sub) < 4)
3179 gst_buffer_extract (sub, 0, fourcc, 4);
3181 switch (GST_READ_UINT32_LE (fourcc)) {
3182 case GST_RIFF_LIST_strl:
3183 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3185 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3186 ("failed to parse stream, ignoring"));
3191 case GST_RIFF_LIST_odml:
3192 gst_avi_demux_parse_odml (avi, sub);
3196 GST_WARNING_OBJECT (avi,
3197 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3198 GST_FOURCC_ARGS (GST_READ_UINT32_LE (fourcc)));
3200 case GST_RIFF_TAG_JUNQ:
3201 case GST_RIFF_TAG_JUNK:
3206 gst_avi_demux_parse_idit (avi, sub);
3209 GST_WARNING_OBJECT (avi,
3210 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
3211 offset, GST_FOURCC_ARGS (tag));
3213 case GST_RIFF_TAG_JUNQ:
3214 case GST_RIFF_TAG_JUNK:
3216 /* move to next chunk */
3218 gst_buffer_unref (sub);
3223 gst_buffer_unref (buf);
3224 GST_DEBUG ("elements parsed");
3226 /* check parsed streams */
3227 if (avi->num_streams == 0) {
3229 } else if (avi->num_streams != avi->avih->streams) {
3230 GST_WARNING_OBJECT (avi,
3231 "Stream header mentioned %d streams, but %d available",
3232 avi->avih->streams, avi->num_streams);
3234 GST_DEBUG ("Get junk and info next");
3235 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
3237 /* Need more data */
3241 case GST_AVI_DEMUX_HEADER_INFO:
3242 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
3244 if (gst_adapter_available (avi->adapter) < 12)
3247 data = gst_adapter_map (avi->adapter, 12);
3248 tag = GST_READ_UINT32_LE (data);
3249 size = GST_READ_UINT32_LE (data + 4);
3250 ltag = GST_READ_UINT32_LE (data + 8);
3251 gst_adapter_unmap (avi->adapter, 0);
3253 if (tag == GST_RIFF_TAG_LIST) {
3255 case GST_RIFF_LIST_movi:
3256 gst_adapter_flush (avi->adapter, 12);
3257 if (!avi->first_movi_offset)
3258 avi->first_movi_offset = avi->offset;
3260 avi->idx1_offset = avi->offset + size - 4;
3262 case GST_RIFF_LIST_INFO:
3263 GST_DEBUG ("Found INFO chunk");
3264 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3265 GST_DEBUG ("got size %d", size);
3267 gst_adapter_flush (avi->adapter, 12);
3269 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
3272 gst_adapter_flush (avi->adapter, 1);
3273 gst_riff_parse_info (GST_ELEMENT_CAST (avi), buf, &tags);
3275 if (avi->globaltags) {
3276 gst_tag_list_insert (avi->globaltags, tags,
3277 GST_TAG_MERGE_REPLACE);
3279 avi->globaltags = tags;
3283 gst_buffer_unref (buf);
3285 avi->offset += GST_ROUND_UP_2 (size) - 4;
3287 GST_DEBUG ("skipping INFO LIST prefix");
3290 /* Need more data */
3295 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3296 avi->offset += 8 + GST_ROUND_UP_2 (size);
3297 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3298 // ??? goto iterate; ???
3300 /* Need more data */
3306 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3307 avi->offset += 8 + GST_ROUND_UP_2 (size);
3308 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3311 /* Need more data */
3318 GST_WARNING ("unhandled header state: %d", avi->header_state);
3323 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3324 avi->num_streams, avi->stream[0].indexes);
3326 GST_DEBUG ("Found movi chunk. Starting to stream data");
3327 avi->state = GST_AVI_DEMUX_MOVI;
3329 /* no indexes in push mode, but it still sets some variables */
3330 gst_avi_demux_calculate_durations_from_index (avi);
3332 gst_avi_demux_expose_streams (avi, TRUE);
3334 /* prepare all streams for index 0 */
3335 for (i = 0; i < avi->num_streams; i++)
3336 avi->stream[i].current_entry = 0;
3338 /* create initial NEWSEGMENT event */
3340 gst_event_unref (avi->seg_event);
3341 avi->seg_event = gst_event_new_segment (&avi->segment);
3343 gst_avi_demux_check_seekability (avi);
3345 /* at this point we know all the streams and we can signal the no more
3347 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3348 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3355 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3356 return GST_FLOW_ERROR;
3360 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3361 ("Invalid AVI header (no LIST at start): %"
3362 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3363 return GST_FLOW_ERROR;
3367 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3368 ("Invalid AVI header (no hdrl at start): %"
3369 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3370 gst_buffer_unref (buf);
3371 return GST_FLOW_ERROR;
3375 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3376 ("Invalid AVI header (no avih at start): %"
3377 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3379 gst_buffer_unref (sub);
3381 gst_buffer_unref (buf);
3382 return GST_FLOW_ERROR;
3386 gst_buffer_unref (buf);
3387 return GST_FLOW_ERROR;
3392 gst_avi_demux_add_date_tag (GstAviDemux * avi, gint y, gint m, gint d,
3393 gint h, gint min, gint s)
3398 date = g_date_new_dmy (d, m, y);
3399 if (!g_date_valid (date)) {
3401 GST_WARNING_OBJECT (avi, "Refusing to add invalid date %d-%d-%d", y, m, d);
3406 dt = gst_date_time_new_local_time (y, m, d, h, min, s);
3408 if (avi->globaltags == NULL)
3409 avi->globaltags = gst_tag_list_new_empty ();
3411 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE, date,
3415 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE_TIME,
3417 gst_date_time_unref (dt);
3422 gst_avi_demux_parse_idit_nums_only (GstAviDemux * avi, gchar * data)
3425 gint hr = 0, min = 0, sec = 0;
3428 GST_DEBUG ("data : '%s'", data);
3430 ret = sscanf (data, "%d:%d:%d %d:%d:%d", &y, &m, &d, &hr, &min, &sec);
3432 /* Attempt YYYY/MM/DD/ HH:MM variant (found in CASIO cameras) */
3433 ret = sscanf (data, "%04d/%02d/%02d/ %d:%d", &y, &m, &d, &hr, &min);
3435 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3439 gst_avi_demux_add_date_tag (avi, y, m, d, hr, min, sec);
3443 get_month_num (gchar * data, guint size)
3445 if (g_ascii_strncasecmp (data, "jan", 3) == 0) {
3447 } else if (g_ascii_strncasecmp (data, "feb", 3) == 0) {
3449 } else if (g_ascii_strncasecmp (data, "mar", 3) == 0) {
3451 } else if (g_ascii_strncasecmp (data, "apr", 3) == 0) {
3453 } else if (g_ascii_strncasecmp (data, "may", 3) == 0) {
3455 } else if (g_ascii_strncasecmp (data, "jun", 3) == 0) {
3457 } else if (g_ascii_strncasecmp (data, "jul", 3) == 0) {
3459 } else if (g_ascii_strncasecmp (data, "aug", 3) == 0) {
3461 } else if (g_ascii_strncasecmp (data, "sep", 3) == 0) {
3463 } else if (g_ascii_strncasecmp (data, "oct", 3) == 0) {
3465 } else if (g_ascii_strncasecmp (data, "nov", 3) == 0) {
3467 } else if (g_ascii_strncasecmp (data, "dec", 3) == 0) {
3475 gst_avi_demux_parse_idit_text (GstAviDemux * avi, gchar * data)
3477 gint year, month, day;
3478 gint hour, min, sec;
3483 ret = sscanf (data, "%3s %3s %d %d:%d:%d %d", weekday, monthstr, &day, &hour,
3486 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3489 month = get_month_num (monthstr, strlen (monthstr));
3490 gst_avi_demux_add_date_tag (avi, year, month, day, hour, min, sec);
3494 gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf)
3498 gchar *safedata = NULL;
3500 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
3503 * http://www.eden-foundation.org/products/code/film_date_stamp/index.html
3505 * This tag could be in one of the below formats
3506 * 2005:08:17 11:42:43
3507 * THU OCT 26 16:46:04 2006
3508 * Mon Mar 3 09:44:56 2008
3510 * FIXME: Our date tag doesn't include hours
3513 /* skip eventual initial whitespace */
3517 while (left > 0 && g_ascii_isspace (ptr[0])) {
3526 /* make a safe copy to add a \0 to the end of the string */
3527 safedata = g_strndup (ptr, left);
3529 /* test if the first char is a alpha or a number */
3530 if (g_ascii_isdigit (ptr[0])) {
3531 gst_avi_demux_parse_idit_nums_only (avi, safedata);
3534 } else if (g_ascii_isalpha (ptr[0])) {
3535 gst_avi_demux_parse_idit_text (avi, safedata);
3543 GST_WARNING_OBJECT (avi, "IDIT tag has no parsable info");
3544 gst_buffer_unmap (buf, data, size);
3548 * Read full AVI headers.
3550 static GstFlowReturn
3551 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
3554 GstBuffer *buf, *sub = NULL;
3557 GstElement *element = GST_ELEMENT_CAST (avi);
3559 GstTagList *tags = NULL;
3562 stamp = gst_util_get_timestamp ();
3564 /* the header consists of a 'hdrl' LIST tag */
3565 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3566 if (res != GST_FLOW_OK)
3567 goto pull_range_failed;
3568 else if (tag != GST_RIFF_TAG_LIST)
3570 else if (gst_buffer_get_size (buf) < 4)
3573 GST_DEBUG_OBJECT (avi, "parsing headers");
3575 /* Find the 'hdrl' LIST tag */
3576 gst_buffer_extract (buf, 0, fourcc, 4);
3577 while (GST_READ_UINT32_LE (fourcc) != GST_RIFF_LIST_hdrl) {
3578 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
3579 GST_FOURCC_ARGS (GST_READ_UINT32_LE (fourcc)));
3582 gst_buffer_unref (buf);
3584 /* read new chunk */
3585 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3586 if (res != GST_FLOW_OK)
3587 goto pull_range_failed;
3588 else if (tag != GST_RIFF_TAG_LIST)
3590 else if (gst_buffer_get_size (buf) < 4)
3592 gst_buffer_extract (buf, 0, fourcc, 4);
3595 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
3597 gst_avi_demux_roundup_list (avi, &buf);
3599 /* the hdrl starts with a 'avih' header */
3600 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
3602 else if (tag != GST_RIFF_TAG_avih)
3604 else if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3607 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3609 /* now, read the elements from the header until the end */
3610 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
3614 /* sub can be NULL on empty tags */
3618 data = gst_buffer_map (sub, &size, NULL, GST_MAP_READ);
3621 case GST_RIFF_TAG_LIST:
3625 switch (GST_READ_UINT32_LE (data)) {
3626 case GST_RIFF_LIST_strl:
3627 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3628 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3629 ("failed to parse stream, ignoring"));
3634 case GST_RIFF_LIST_odml:
3635 gst_avi_demux_parse_odml (avi, sub);
3638 case GST_RIFF_LIST_INFO:
3639 gst_buffer_resize (sub, 4, -1);
3640 gst_riff_parse_info (element, sub, &tags);
3642 if (avi->globaltags) {
3643 gst_tag_list_insert (avi->globaltags, tags,
3644 GST_TAG_MERGE_REPLACE);
3646 avi->globaltags = tags;
3652 GST_WARNING_OBJECT (avi,
3653 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3654 GST_FOURCC_ARGS (GST_READ_UINT32_LE (data)));
3655 GST_MEMDUMP_OBJECT (avi, "Unknown list", data, size);
3657 case GST_RIFF_TAG_JUNQ:
3658 case GST_RIFF_TAG_JUNK:
3663 gst_avi_demux_parse_idit (avi, sub);
3666 GST_WARNING_OBJECT (avi,
3667 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
3668 GST_FOURCC_ARGS (tag), offset);
3669 GST_MEMDUMP_OBJECT (avi, "Unknown tag", data, size);
3671 case GST_RIFF_TAG_JUNQ:
3672 case GST_RIFF_TAG_JUNK:
3675 gst_buffer_unmap (sub, data, size);
3676 gst_buffer_unref (sub);
3682 gst_buffer_unref (buf);
3683 GST_DEBUG ("elements parsed");
3685 /* check parsed streams */
3686 if (avi->num_streams == 0)
3688 else if (avi->num_streams != avi->avih->streams) {
3689 GST_WARNING_OBJECT (avi,
3690 "Stream header mentioned %d streams, but %d available",
3691 avi->avih->streams, avi->num_streams);
3694 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
3695 G_GUINT64_FORMAT, avi->offset);
3697 /* Now, find the data (i.e. skip all junk between header and data) */
3704 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
3705 if (res != GST_FLOW_OK) {
3706 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
3707 goto pull_range_failed;
3708 } else if (gst_buffer_get_size (buf) < 12) {
3709 GST_DEBUG_OBJECT (avi, "got %d bytes which is less than 12 bytes",
3710 gst_buffer_get_size (buf));
3711 gst_buffer_unref (buf);
3712 return GST_FLOW_ERROR;
3715 data = gst_buffer_map (buf, &bsize, NULL, GST_MAP_READ);
3716 tag = GST_READ_UINT32_LE (data);
3717 size = GST_READ_UINT32_LE (data + 4);
3718 ltag = GST_READ_UINT32_LE (data + 8);
3720 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
3721 GST_FOURCC_ARGS (tag), size);
3722 GST_MEMDUMP ("Tag content", data, bsize);
3723 gst_buffer_unmap (buf, data, bsize);
3724 gst_buffer_unref (buf);
3727 case GST_RIFF_TAG_LIST:{
3729 case GST_RIFF_LIST_movi:
3730 GST_DEBUG_OBJECT (avi,
3731 "Reached the 'movi' tag, we're done with skipping");
3733 case GST_RIFF_LIST_INFO:
3735 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3737 if (res != GST_FLOW_OK) {
3738 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3739 goto pull_range_failed;
3741 GST_DEBUG ("got size %u", gst_buffer_get_size (buf));
3743 GST_DEBUG ("skipping INFO LIST prefix");
3744 avi->offset += (4 - GST_ROUND_UP_2 (size));
3745 gst_buffer_unref (buf);
3749 sub = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, 4, -1);
3750 gst_riff_parse_info (element, sub, &tags);
3752 if (avi->globaltags) {
3753 gst_tag_list_insert (avi->globaltags, tags,
3754 GST_TAG_MERGE_REPLACE);
3756 avi->globaltags = tags;
3761 gst_buffer_unref (sub);
3764 gst_buffer_unref (buf);
3765 /* gst_riff_read_chunk() has already advanced avi->offset */
3768 GST_WARNING_OBJECT (avi,
3769 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3770 GST_FOURCC_ARGS (ltag));
3771 avi->offset += 8 + GST_ROUND_UP_2 (size);
3777 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3778 GST_FOURCC_ARGS (tag));
3780 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3781 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3782 /* Only get buffer for debugging if the memdump is needed */
3783 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= 9) {
3784 res = gst_pad_pull_range (avi->sinkpad, avi->offset, size, &buf);
3785 if (res != GST_FLOW_OK) {
3786 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3787 goto pull_range_failed;
3789 data = gst_buffer_map (buf, &bsize, NULL, GST_MAP_READ);
3790 GST_MEMDUMP ("Junk", data, bsize);
3791 gst_buffer_unmap (buf, data, bsize);
3792 gst_buffer_unref (buf);
3794 avi->offset += 8 + GST_ROUND_UP_2 (size);
3800 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3801 avi->num_streams, avi->stream[0].indexes);
3803 /* create or read stream index (for seeking) */
3804 if (avi->stream[0].indexes != NULL) {
3805 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3806 gst_avi_demux_read_subindexes_pull (avi);
3808 if (!avi->have_index) {
3809 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
3810 gst_avi_demux_stream_index (avi);
3812 /* still no index, scan */
3813 if (!avi->have_index) {
3814 gst_avi_demux_stream_scan (avi);
3816 /* still no index.. this is a fatal error for now.
3817 * FIXME, we should switch to plain push mode without seeking
3818 * instead of failing. */
3819 if (!avi->have_index)
3823 /* use the indexes now to construct nice durations */
3824 gst_avi_demux_calculate_durations_from_index (avi);
3826 gst_avi_demux_expose_streams (avi, FALSE);
3828 /* do initial seek to the default segment values */
3829 gst_avi_demux_do_seek (avi, &avi->segment);
3831 /* create initial NEWSEGMENT event */
3833 gst_event_unref (avi->seg_event);
3834 avi->seg_event = gst_event_new_segment (&avi->segment);
3836 stamp = gst_util_get_timestamp () - stamp;
3837 GST_DEBUG_OBJECT (avi, "pulling header took %" GST_TIME_FORMAT,
3838 GST_TIME_ARGS (stamp));
3840 /* at this point we know all the streams and we can signal the no more
3842 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3843 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3850 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3851 ("Invalid AVI header (no LIST at start): %"
3852 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3853 gst_buffer_unref (buf);
3854 return GST_FLOW_ERROR;
3858 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3859 ("Invalid AVI header (no hdrl at start): %"
3860 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3861 gst_buffer_unref (buf);
3862 return GST_FLOW_ERROR;
3866 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3867 ("Invalid AVI header (no avih at start): %"
3868 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3870 gst_buffer_unref (sub);
3871 gst_buffer_unref (buf);
3872 return GST_FLOW_ERROR;
3876 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3877 ("Invalid AVI header (cannot parse avih at start)"));
3878 gst_buffer_unref (buf);
3879 return GST_FLOW_ERROR;
3883 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3884 return GST_FLOW_ERROR;
3888 GST_WARNING ("file without or too big index");
3889 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3890 ("Could not get/create index"));
3891 return GST_FLOW_ERROR;
3895 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3896 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3897 return GST_FLOW_ERROR;
3901 /* move a stream to @index */
3903 gst_avi_demux_move_stream (GstAviDemux * avi, GstAviStream * stream,
3904 GstSegment * segment, guint index)
3906 GST_DEBUG_OBJECT (avi, "Move stream %d to %u", stream->num, index);
3908 if (segment->rate < 0.0) {
3910 /* Because we don't know the frame order we need to push from the prev keyframe
3911 * to the next keyframe. If there is a smart decoder downstream he will notice
3912 * that there are too many encoded frames send and return UNEXPECTED when there
3913 * are enough decoded frames to fill the segment. */
3914 next_key = gst_avi_demux_index_next (avi, stream, index, TRUE);
3916 /* FIXME, we go back to 0, we should look at segment.start. We will however
3917 * stop earlier when the see the timestamp < segment.start */
3918 stream->start_entry = 0;
3919 stream->step_entry = index;
3920 stream->current_entry = index;
3921 stream->stop_entry = next_key;
3923 GST_DEBUG_OBJECT (avi, "reverse seek: start %u, step %u, stop %u",
3924 stream->start_entry, stream->step_entry, stream->stop_entry);
3926 stream->start_entry = index;
3927 stream->step_entry = index;
3928 stream->stop_entry = gst_avi_demux_index_last (avi, stream);
3930 if (stream->current_entry != index) {
3931 GST_DEBUG_OBJECT (avi, "Move DISCONT from %u to %u",
3932 stream->current_entry, index);
3933 stream->current_entry = index;
3934 stream->discont = TRUE;
3937 /* update the buffer info */
3938 gst_avi_demux_get_buffer_info (avi, stream, index,
3939 &stream->current_timestamp, &stream->current_ts_end,
3940 &stream->current_offset, &stream->current_offset_end);
3942 GST_DEBUG_OBJECT (avi, "Moved to %u, ts %" GST_TIME_FORMAT
3943 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
3944 ", off_end %" G_GUINT64_FORMAT, index,
3945 GST_TIME_ARGS (stream->current_timestamp),
3946 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
3947 stream->current_offset_end);
3949 GST_DEBUG_OBJECT (avi, "Seeking to offset %" G_GUINT64_FORMAT,
3950 stream->index[index].offset);
3954 * Do the actual seeking.
3957 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
3959 GstClockTime seek_time;
3962 GstAviStream *stream;
3964 seek_time = segment->position;
3965 keyframe = ! !(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
3967 GST_DEBUG_OBJECT (avi, "seek to: %" GST_TIME_FORMAT
3968 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
3970 /* FIXME, this code assumes the main stream with keyframes is stream 0,
3971 * which is mostly correct... */
3972 stream = &avi->stream[avi->main_stream];
3974 /* get the entry index for the requested position */
3975 index = gst_avi_demux_index_for_time (avi, stream, seek_time);
3976 GST_DEBUG_OBJECT (avi, "Got entry %u", index);
3978 /* check if we are already on a keyframe */
3979 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
3980 GST_DEBUG_OBJECT (avi, "not keyframe, searching back");
3981 /* now go to the previous keyframe, this is where we should start
3983 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
3984 GST_DEBUG_OBJECT (avi, "previous keyframe at %u", index);
3987 /* move the main stream to this position */
3988 gst_avi_demux_move_stream (avi, stream, segment, index);
3991 /* when seeking to a keyframe, we update the result seek time
3992 * to the time of the keyframe. */
3993 seek_time = stream->current_timestamp;
3994 GST_DEBUG_OBJECT (avi, "keyframe adjusted to %" GST_TIME_FORMAT,
3995 GST_TIME_ARGS (seek_time));
3998 /* the seek time is also the position and stream time when going
4000 segment->position = seek_time;
4001 if (segment->rate > 0.0)
4002 segment->time = seek_time;
4004 /* now set DISCONT and align the other streams */
4005 for (i = 0; i < avi->num_streams; i++) {
4006 GstAviStream *ostream;
4008 ostream = &avi->stream[i];
4009 if ((ostream == stream) || (ostream->index == NULL))
4012 /* get the entry index for the requested position */
4013 index = gst_avi_demux_index_for_time (avi, ostream, seek_time);
4015 /* move to previous keyframe */
4016 if (!ENTRY_IS_KEYFRAME (&ostream->index[index]))
4017 index = gst_avi_demux_index_prev (avi, ostream, index, TRUE);
4019 gst_avi_demux_move_stream (avi, ostream, segment, index);
4021 GST_DEBUG_OBJECT (avi, "done seek to: %" GST_TIME_FORMAT,
4022 GST_TIME_ARGS (seek_time));
4028 * Handle seek event in pull mode.
4031 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4036 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4040 GstSegment seeksegment = { 0, };
4044 GST_DEBUG_OBJECT (avi, "doing seek with event");
4046 gst_event_parse_seek (event, &rate, &format, &flags,
4047 &cur_type, &cur, &stop_type, &stop);
4049 /* we have to have a format as the segment format. Try to convert
4051 if (format != GST_FORMAT_TIME) {
4052 gboolean res = TRUE;
4054 if (cur_type != GST_SEEK_TYPE_NONE)
4055 res = gst_pad_query_convert (pad, format, cur, GST_FORMAT_TIME, &cur);
4056 if (res && stop_type != GST_SEEK_TYPE_NONE)
4057 res = gst_pad_query_convert (pad, format, stop, GST_FORMAT_TIME, &stop);
4061 format = GST_FORMAT_TIME;
4063 GST_DEBUG_OBJECT (avi,
4064 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
4065 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
4066 /* FIXME: can we do anything with rate!=1.0 */
4068 GST_DEBUG_OBJECT (avi, "doing seek without event");
4073 /* save flush flag */
4074 flush = flags & GST_SEEK_FLAG_FLUSH;
4077 GstEvent *fevent = gst_event_new_flush_start ();
4079 /* for a flushing seek, we send a flush_start on all pads. This will
4080 * eventually stop streaming with a WRONG_STATE. We can thus eventually
4081 * take the STREAM_LOCK. */
4082 GST_DEBUG_OBJECT (avi, "sending flush start");
4083 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4084 gst_pad_push_event (avi->sinkpad, fevent);
4086 /* a non-flushing seek, we PAUSE the task so that we can take the
4088 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
4089 gst_pad_pause_task (avi->sinkpad);
4092 /* wait for streaming to stop */
4093 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
4094 GST_PAD_STREAM_LOCK (avi->sinkpad);
4096 /* copy segment, we need this because we still need the old
4097 * segment when we close the current segment. */
4098 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4101 GST_DEBUG_OBJECT (avi, "configuring seek");
4102 gst_segment_do_seek (&seeksegment, rate, format, flags,
4103 cur_type, cur, stop_type, stop, &update);
4105 /* do the seek, seeksegment.position contains the new position, this
4106 * actually never fails. */
4107 gst_avi_demux_do_seek (avi, &seeksegment);
4110 GstEvent *fevent = gst_event_new_flush_stop (TRUE);
4112 GST_DEBUG_OBJECT (avi, "sending flush stop");
4113 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4114 gst_pad_push_event (avi->sinkpad, fevent);
4117 /* now update the real segment info */
4118 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
4120 /* post the SEGMENT_START message when we do segmented playback */
4121 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4122 gst_element_post_message (GST_ELEMENT_CAST (avi),
4123 gst_message_new_segment_start (GST_OBJECT_CAST (avi),
4124 avi->segment.format, avi->segment.position));
4127 /* queue the segment event for the streaming thread. */
4129 gst_event_unref (avi->seg_event);
4130 avi->seg_event = gst_event_new_segment (&avi->segment);
4132 if (!avi->streaming) {
4133 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4136 /* reset the last flow and mark discont, seek is always DISCONT */
4137 for (i = 0; i < avi->num_streams; i++) {
4138 GST_DEBUG_OBJECT (avi, "marking DISCONT");
4139 avi->stream[i].last_flow = GST_FLOW_OK;
4140 avi->stream[i].discont = TRUE;
4142 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
4149 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4155 * Handle seek event in push mode.
4158 avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4163 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4166 GstAviStream *stream;
4170 GstSegment seeksegment;
4173 /* check we have the index */
4174 if (!avi->have_index) {
4175 GST_DEBUG_OBJECT (avi, "no seek index built, seek aborted.");
4178 GST_DEBUG_OBJECT (avi, "doing push-based seek with event");
4181 gst_event_parse_seek (event, &rate, &format, &flags,
4182 &cur_type, &cur, &stop_type, &stop);
4184 if (format != GST_FORMAT_TIME) {
4185 gboolean res = TRUE;
4187 if (cur_type != GST_SEEK_TYPE_NONE)
4188 res = gst_pad_query_convert (pad, format, cur, GST_FORMAT_TIME, &cur);
4189 if (res && stop_type != GST_SEEK_TYPE_NONE)
4190 res = gst_pad_query_convert (pad, format, stop, GST_FORMAT_TIME, &stop);
4192 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4196 format = GST_FORMAT_TIME;
4199 /* let gst_segment handle any tricky stuff */
4200 GST_DEBUG_OBJECT (avi, "configuring seek");
4201 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4202 gst_segment_do_seek (&seeksegment, rate, format, flags,
4203 cur_type, cur, stop_type, stop, &update);
4205 keyframe = ! !(flags & GST_SEEK_FLAG_KEY_UNIT);
4206 cur = seeksegment.position;
4208 GST_DEBUG_OBJECT (avi,
4209 "Seek requested: ts %" GST_TIME_FORMAT " stop %" GST_TIME_FORMAT
4210 ", kf %u, rate %lf", GST_TIME_ARGS (cur), GST_TIME_ARGS (stop), keyframe,
4214 GST_DEBUG_OBJECT (avi, "negative rate seek not supported in push mode");
4218 /* FIXME, this code assumes the main stream with keyframes is stream 0,
4219 * which is mostly correct... */
4220 str_num = avi->main_stream;
4221 stream = &avi->stream[str_num];
4223 /* get the entry index for the requested position */
4224 index = gst_avi_demux_index_for_time (avi, stream, cur);
4225 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT,
4226 str_num, index, GST_TIME_ARGS (cur));
4228 /* check if we are already on a keyframe */
4229 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
4230 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4231 /* now go to the previous keyframe, this is where we should start
4233 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4234 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", index);
4237 gst_avi_demux_get_buffer_info (avi, stream, index,
4238 &stream->current_timestamp, &stream->current_ts_end,
4239 &stream->current_offset, &stream->current_offset_end);
4241 /* re-use cur to be the timestamp of the seek as it _will_ be */
4242 cur = stream->current_timestamp;
4244 min_offset = stream->index[index].offset;
4245 avi->seek_kf_offset = min_offset - 8;
4247 GST_DEBUG_OBJECT (avi,
4248 "Seek to: ts %" GST_TIME_FORMAT " (on str %u, idx %u, offset %"
4249 G_GUINT64_FORMAT ")", GST_TIME_ARGS (stream->current_timestamp), str_num,
4252 for (n = 0; n < avi->num_streams; n++) {
4253 GstAviStream *str = &avi->stream[n];
4256 if (n == avi->main_stream)
4259 /* get the entry index for the requested position */
4260 idx = gst_avi_demux_index_for_time (avi, str, cur);
4261 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT, n,
4262 idx, GST_TIME_ARGS (cur));
4264 /* check if we are already on a keyframe */
4265 if (!ENTRY_IS_KEYFRAME (&str->index[idx])) {
4266 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4267 /* now go to the previous keyframe, this is where we should start
4269 idx = gst_avi_demux_index_prev (avi, str, idx, TRUE);
4270 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", idx);
4273 gst_avi_demux_get_buffer_info (avi, str, idx,
4274 &str->current_timestamp, &str->current_ts_end,
4275 &str->current_offset, &str->current_offset_end);
4277 if (str->index[idx].offset < min_offset) {
4278 min_offset = str->index[idx].offset;
4279 GST_DEBUG_OBJECT (avi,
4280 "Found an earlier offset at %" G_GUINT64_FORMAT ", str %u",
4288 GST_DEBUG_OBJECT (avi,
4289 "Seek performed: str %u, offset %" G_GUINT64_FORMAT ", idx %u, ts %"
4290 GST_TIME_FORMAT ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4291 ", off_end %" G_GUINT64_FORMAT, str_num, min_offset, index,
4292 GST_TIME_ARGS (stream->current_timestamp),
4293 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4294 stream->current_offset_end);
4296 /* index data refers to data, not chunk header (for pull mode convenience) */
4298 GST_DEBUG_OBJECT (avi, "seeking to chunk at offset %" G_GUINT64_FORMAT,
4301 if (!perform_seek_to_offset (avi, min_offset)) {
4302 GST_DEBUG_OBJECT (avi, "seek event failed!");
4310 * Handle whether we can perform the seek event or if we have to let the chain
4311 * function handle seeks to build the seek indexes first.
4314 gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
4317 /* check for having parsed index already */
4318 if (!avi->have_index) {
4320 gboolean building_index;
4322 GST_OBJECT_LOCK (avi);
4323 /* handle the seek event in the chain function */
4324 avi->state = GST_AVI_DEMUX_SEEK;
4326 /* copy the event */
4327 if (avi->seek_event)
4328 gst_event_unref (avi->seek_event);
4329 avi->seek_event = gst_event_ref (event);
4331 /* set the building_index flag so that only one thread can setup the
4332 * structures for index seeking. */
4333 building_index = avi->building_index;
4334 if (!building_index) {
4335 avi->building_index = TRUE;
4336 if (avi->stream[0].indexes) {
4337 avi->odml_stream = 0;
4338 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
4339 offset = avi->odml_subidxs[0];
4341 offset = avi->idx1_offset;
4344 GST_OBJECT_UNLOCK (avi);
4346 if (!building_index) {
4347 /* seek to the first subindex or legacy index */
4348 GST_INFO_OBJECT (avi,
4349 "Seeking to legacy index/first subindex at %" G_GUINT64_FORMAT,
4351 return perform_seek_to_offset (avi, offset);
4354 /* FIXME: we have to always return true so that we don't block the seek
4356 * Note: maybe it is OK to return true if we're still building the index */
4360 return avi_demux_handle_seek_push (avi, pad, event);
4364 * Helper for gst_avi_demux_invert()
4367 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
4369 memcpy (tmp, d1, bytes);
4370 memcpy (d1, d2, bytes);
4371 memcpy (d2, tmp, bytes);
4375 #define gst_avi_demux_is_uncompressed(fourcc) \
4377 (fourcc == GST_RIFF_DIB || \
4378 fourcc == GST_RIFF_rgb || \
4379 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW))
4382 * Invert DIB buffers... Takes existing buffer and
4383 * returns either the buffer or a new one (with old
4384 * one dereferenced).
4385 * FIXME: can't we preallocate tmp? and remember stride, bpp?
4388 gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
4393 guint8 *tmp = NULL, *data;
4397 if (stream->strh->type != GST_RIFF_FCC_vids)
4400 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
4401 return buf; /* Ignore non DIB buffers */
4404 caps = gst_pad_get_current_caps (stream->pad);
4405 s = gst_caps_get_structure (caps, 0);
4406 gst_caps_unref (caps);
4408 if (!gst_structure_get_int (s, "bpp", &bpp)) {
4409 GST_WARNING ("Failed to retrieve depth from caps");
4413 if (stream->strf.vids == NULL) {
4414 GST_WARNING ("Failed to retrieve vids for stream");
4418 h = stream->strf.vids->height;
4419 w = stream->strf.vids->width;
4420 stride = w * (bpp / 8);
4422 buf = gst_buffer_make_writable (buf);
4424 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READWRITE);
4425 if (size < (stride * h)) {
4426 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
4427 gst_buffer_unmap (buf, data, size);
4431 tmp = g_malloc (stride);
4433 for (y = 0; y < h / 2; y++) {
4434 swap_line (data + stride * y, data + stride * (h - 1 - y), tmp, stride);
4439 gst_buffer_unmap (buf, data, size);
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 :
4454 GST_ASSOCIATION_FLAG_DELTA_UNIT, GST_FORMAT_TIME, timestamp,
4455 GST_FORMAT_BYTES, offset, NULL);
4456 /* current_entry is DEFAULT (frame #) */
4457 gst_index_add_association (avi->element_index, stream->index_id,
4458 keyframe ? GST_ASSOCIATION_FLAG_KEY_UNIT :
4459 GST_ASSOCIATION_FLAG_DELTA_UNIT, GST_FORMAT_TIME, timestamp,
4460 GST_FORMAT_BYTES, offset, GST_FORMAT_DEFAULT, stream->current_entry,
4466 * Returns the aggregated GstFlowReturn.
4468 static GstFlowReturn
4469 gst_avi_demux_combine_flows (GstAviDemux * avi, GstAviStream * stream,
4473 gboolean unexpected = FALSE, not_linked = TRUE;
4475 /* store the value */
4476 stream->last_flow = ret;
4478 /* any other error that is not-linked or eos can be returned right away */
4479 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
4482 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4483 for (i = 0; i < avi->num_streams; i++) {
4484 GstAviStream *ostream = &avi->stream[i];
4486 ret = ostream->last_flow;
4487 /* no unexpected or unlinked, return */
4488 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
4491 /* we check to see if we have at least 1 unexpected or all unlinked */
4492 unexpected |= (ret == GST_FLOW_UNEXPECTED);
4493 not_linked &= (ret == GST_FLOW_NOT_LINKED);
4495 /* when we get here, we all have unlinked or unexpected */
4497 ret = GST_FLOW_NOT_LINKED;
4498 else if (unexpected)
4499 ret = GST_FLOW_UNEXPECTED;
4501 GST_LOG_OBJECT (avi, "combined %s to return %s",
4502 gst_flow_get_name (stream->last_flow), gst_flow_get_name (ret));
4506 /* move @stream to the next position in its index */
4507 static GstFlowReturn
4508 gst_avi_demux_advance (GstAviDemux * avi, GstAviStream * stream,
4511 guint old_entry, new_entry;
4513 old_entry = stream->current_entry;
4515 new_entry = old_entry + 1;
4517 /* see if we reached the end */
4518 if (new_entry >= stream->stop_entry) {
4519 if (avi->segment.rate < 0.0) {
4520 if (stream->step_entry == stream->start_entry) {
4521 /* we stepped all the way to the start, eos */
4522 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
4525 /* backwards, stop becomes step, find a new step */
4526 stream->stop_entry = stream->step_entry;
4527 stream->step_entry = gst_avi_demux_index_prev (avi, stream,
4528 stream->stop_entry, TRUE);
4530 GST_DEBUG_OBJECT (avi,
4531 "reverse playback jump: start %u, step %u, stop %u",
4532 stream->start_entry, stream->step_entry, stream->stop_entry);
4534 /* and start from the previous keyframe now */
4535 new_entry = stream->step_entry;
4538 GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
4543 if (new_entry != old_entry) {
4544 stream->current_entry = new_entry;
4545 stream->current_total = stream->index[new_entry].total;
4547 if (new_entry == old_entry + 1) {
4548 GST_DEBUG_OBJECT (avi, "moved forwards from %u to %u",
4549 old_entry, new_entry);
4550 /* we simply moved one step forwards, reuse current info */
4551 stream->current_timestamp = stream->current_ts_end;
4552 stream->current_offset = stream->current_offset_end;
4553 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4554 NULL, &stream->current_ts_end, NULL, &stream->current_offset_end);
4556 /* we moved DISCONT, full update */
4557 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4558 &stream->current_timestamp, &stream->current_ts_end,
4559 &stream->current_offset, &stream->current_offset_end);
4560 /* and MARK discont for this stream */
4561 stream->last_flow = GST_FLOW_OK;
4562 stream->discont = TRUE;
4563 GST_DEBUG_OBJECT (avi, "Moved from %u to %u, ts %" GST_TIME_FORMAT
4564 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4565 ", off_end %" G_GUINT64_FORMAT, old_entry, new_entry,
4566 GST_TIME_ARGS (stream->current_timestamp),
4567 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4568 stream->current_offset_end);
4576 GST_DEBUG_OBJECT (avi, "we are EOS");
4577 /* setting current_timestamp to -1 marks EOS */
4578 stream->current_timestamp = -1;
4579 return GST_FLOW_UNEXPECTED;
4583 /* find the stream with the lowest current position when going forwards or with
4584 * the highest position when going backwards, this is the stream
4585 * we should push from next */
4587 gst_avi_demux_find_next (GstAviDemux * avi, gfloat rate)
4589 guint64 min_time, max_time;
4590 guint stream_num, i;
4593 min_time = G_MAXUINT64;
4596 for (i = 0; i < avi->num_streams; i++) {
4598 GstAviStream *stream;
4600 stream = &avi->stream[i];
4602 /* ignore streams that finished */
4603 if (stream->last_flow == GST_FLOW_UNEXPECTED)
4606 position = stream->current_timestamp;
4608 /* position of -1 is EOS */
4609 if (position != -1) {
4610 if (rate > 0.0 && position < min_time) {
4611 min_time = position;
4613 } else if (rate < 0.0 && position >= max_time) {
4614 max_time = position;
4622 static GstFlowReturn
4623 gst_avi_demux_loop_data (GstAviDemux * avi)
4625 GstFlowReturn ret = GST_FLOW_OK;
4627 GstAviStream *stream;
4628 gboolean processed = FALSE;
4630 guint64 offset, size;
4631 GstClockTime timestamp, duration;
4632 guint64 out_offset, out_offset_end;
4634 GstAviIndexEntry *entry;
4637 stream_num = gst_avi_demux_find_next (avi, avi->segment.rate);
4640 if (G_UNLIKELY (stream_num == -1)) {
4641 GST_DEBUG_OBJECT (avi, "all streams are EOS");
4645 /* we have the stream now */
4646 stream = &avi->stream[stream_num];
4648 /* skip streams without pads */
4650 GST_DEBUG_OBJECT (avi, "skipping entry from stream %d without pad",
4655 /* get the timing info for the entry */
4656 timestamp = stream->current_timestamp;
4657 duration = stream->current_ts_end - timestamp;
4658 out_offset = stream->current_offset;
4659 out_offset_end = stream->current_offset_end;
4661 /* get the entry data info */
4662 entry = &stream->index[stream->current_entry];
4663 offset = entry->offset;
4665 keyframe = ENTRY_IS_KEYFRAME (entry);
4667 /* skip empty entries */
4669 GST_DEBUG_OBJECT (avi, "Skipping entry %u (%" G_GUINT64_FORMAT ", %p)",
4670 stream->current_entry, size, stream->pad);
4674 if (avi->segment.rate > 0.0) {
4675 /* only check this for fowards playback for now */
4676 if (keyframe && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
4677 && (timestamp > avi->segment.stop)) {
4682 GST_LOG ("reading buffer (size=%" G_GUINT64_FORMAT "), stream %d, pos %"
4683 G_GUINT64_FORMAT " (0x%" G_GINT64_MODIFIER "x), kf %d", size,
4684 stream_num, offset, offset, keyframe);
4686 /* FIXME, check large chunks and cut them up */
4688 /* pull in the data */
4689 ret = gst_pad_pull_range (avi->sinkpad, offset, size, &buf);
4690 if (ret != GST_FLOW_OK)
4693 /* check for short buffers, this is EOS as well */
4694 if (gst_buffer_get_size (buf) < size)
4697 /* invert the picture if needed */
4698 buf = gst_avi_demux_invert (stream, buf);
4700 /* mark non-keyframes */
4702 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4704 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4706 GST_BUFFER_TIMESTAMP (buf) = timestamp;
4707 GST_BUFFER_DURATION (buf) = duration;
4708 GST_BUFFER_OFFSET (buf) = out_offset;
4709 GST_BUFFER_OFFSET_END (buf) = out_offset_end;
4711 /* mark discont when pending */
4712 if (stream->discont) {
4713 GST_DEBUG_OBJECT (avi, "setting DISCONT flag");
4714 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4715 stream->discont = FALSE;
4718 gst_avi_demux_add_assoc (avi, stream, timestamp, offset, keyframe);
4720 /* update current position in the segment */
4721 avi->segment.position = 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_get_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_get_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;
4800 if (G_UNLIKELY (avi->have_eos)) {
4801 /* Clean adapter, we're done */
4802 gst_adapter_clear (avi->adapter);
4803 return GST_FLOW_UNEXPECTED;
4806 if (G_UNLIKELY (avi->todrop)) {
4809 if ((drop = gst_adapter_available (avi->adapter))) {
4810 if (drop > avi->todrop)
4812 GST_DEBUG_OBJECT (avi, "Dropping %d bytes", drop);
4813 gst_adapter_flush (avi->adapter, drop);
4814 avi->todrop -= drop;
4815 avi->offset += drop;
4819 /* Iterate until need more data, so adapter won't grow too much */
4821 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
4825 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
4826 GST_FOURCC_ARGS (tag), size);
4828 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
4829 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
4830 GST_LOG ("Chunk ok");
4831 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
4832 GST_DEBUG ("Found sub-index tag");
4833 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4834 /* accept 0 size buffer here */
4835 avi->abort_buffering = FALSE;
4836 GST_DEBUG (" skipping %d bytes for now", size);
4837 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4840 } else if (tag == GST_RIFF_TAG_RIFF) {
4841 /* RIFF tags can appear in ODML files, just jump over them */
4842 if (gst_adapter_available (avi->adapter) >= 12) {
4843 GST_DEBUG ("Found RIFF tag, skipping RIFF header");
4844 gst_adapter_flush (avi->adapter, 12);
4848 } else if (tag == GST_RIFF_TAG_idx1) {
4849 GST_DEBUG ("Found index tag");
4850 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4851 /* accept 0 size buffer here */
4852 avi->abort_buffering = FALSE;
4853 GST_DEBUG (" skipping %d bytes for now", size);
4854 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4857 } else if (tag == GST_RIFF_TAG_LIST) {
4858 /* movi chunks might be grouped in rec list */
4859 if (gst_adapter_available (avi->adapter) >= 12) {
4860 GST_DEBUG ("Found LIST tag, skipping LIST header");
4861 gst_adapter_flush (avi->adapter, 12);
4865 } else if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ) {
4866 /* rec list might contain JUNK chunks */
4867 GST_DEBUG ("Found JUNK tag");
4868 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4869 /* accept 0 size buffer here */
4870 avi->abort_buffering = FALSE;
4871 GST_DEBUG (" skipping %d bytes for now", size);
4872 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4876 GST_DEBUG ("No more stream chunks, send EOS");
4877 avi->have_eos = TRUE;
4878 return GST_FLOW_UNEXPECTED;
4881 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
4882 /* supposedly one hopes to catch a nicer chunk later on ... */
4883 /* FIXME ?? give up here rather than possibly ending up going
4884 * through the whole file */
4885 if (avi->abort_buffering) {
4886 avi->abort_buffering = FALSE;
4888 gst_adapter_flush (avi->adapter, 8);
4895 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
4896 GST_FOURCC_ARGS (tag), size);
4898 stream_nr = CHUNKID_TO_STREAMNR (tag);
4900 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
4902 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
4903 stream_nr, GST_FOURCC_ARGS (tag));
4904 avi->offset += 8 + GST_ROUND_UP_2 (size);
4905 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4907 GstAviStream *stream;
4908 GstClockTime next_ts = 0;
4909 GstBuffer *buf = NULL;
4911 gboolean saw_desired_kf = stream_nr != avi->main_stream
4912 || avi->offset >= avi->seek_kf_offset;
4914 if (stream_nr == avi->main_stream && avi->offset == avi->seek_kf_offset) {
4915 GST_DEBUG_OBJECT (avi, "Desired keyframe reached");
4916 avi->seek_kf_offset = 0;
4919 if (saw_desired_kf) {
4920 gst_adapter_flush (avi->adapter, 8);
4923 buf = gst_adapter_take_buffer (avi->adapter, GST_ROUND_UP_2 (size));
4924 /* patch the size */
4925 gst_buffer_resize (buf, 0, size);
4930 GST_DEBUG_OBJECT (avi,
4931 "Desired keyframe not yet reached, flushing chunk");
4932 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4935 offset = avi->offset;
4936 avi->offset += 8 + GST_ROUND_UP_2 (size);
4938 stream = &avi->stream[stream_nr];
4940 /* set delay (if any)
4941 if (stream->strh->init_frames == stream->current_frame &&
4943 stream->delay = next_ts;
4946 /* parsing of corresponding header may have failed */
4947 if (G_UNLIKELY (!stream->pad)) {
4948 GST_WARNING_OBJECT (avi, "no pad for stream ID %" GST_FOURCC_FORMAT,
4949 GST_FOURCC_ARGS (tag));
4951 gst_buffer_unref (buf);
4953 /* get time of this buffer */
4954 gst_pad_query_position (stream->pad, GST_FORMAT_TIME,
4955 (gint64 *) & next_ts);
4957 gst_avi_demux_add_assoc (avi, stream, next_ts, offset, FALSE);
4959 /* increment our positions */
4960 stream->current_entry++;
4961 stream->current_total += size;
4963 /* update current position in the segment */
4964 avi->segment.position = next_ts;
4966 if (saw_desired_kf && buf) {
4967 GstClockTime dur_ts = 0;
4969 /* invert the picture if needed */
4970 buf = gst_avi_demux_invert (stream, buf);
4972 gst_pad_query_position (stream->pad, GST_FORMAT_TIME,
4973 (gint64 *) & dur_ts);
4975 GST_BUFFER_TIMESTAMP (buf) = next_ts;
4976 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
4977 if (stream->strh->type == GST_RIFF_FCC_vids) {
4978 GST_BUFFER_OFFSET (buf) = stream->current_entry - 1;
4979 GST_BUFFER_OFFSET_END (buf) = stream->current_entry;
4981 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4982 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
4985 GST_DEBUG_OBJECT (avi,
4986 "Pushing buffer with time=%" GST_TIME_FORMAT ", duration %"
4987 GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
4988 " and size %d over pad %s", GST_TIME_ARGS (next_ts),
4989 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
4990 GST_BUFFER_OFFSET (buf), size, GST_PAD_NAME (stream->pad));
4992 /* mark discont when pending */
4993 if (G_UNLIKELY (stream->discont)) {
4994 GST_DEBUG_OBJECT (avi, "Setting DISCONT");
4995 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4996 stream->discont = FALSE;
4998 res = gst_pad_push (stream->pad, buf);
5002 res = gst_avi_demux_combine_flows (avi, stream, res);
5003 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5004 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
5016 * Send pending tags.
5019 push_tag_lists (GstAviDemux * avi)
5027 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
5029 for (i = 0; i < avi->num_streams; i++) {
5030 GstAviStream *stream = &avi->stream[i];
5031 GstPad *pad = stream->pad;
5033 tags = stream->taglist;
5036 GST_DEBUG_OBJECT (pad, "Tags: %" GST_PTR_FORMAT, tags);
5038 gst_pad_push_event (pad, gst_event_new_tag (tags));
5039 stream->taglist = NULL;
5043 if (!(tags = avi->globaltags))
5044 tags = gst_tag_list_new_empty ();
5046 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
5047 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
5049 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, tags);
5050 gst_avi_demux_push_event (avi, gst_event_new_tag (tags));
5051 avi->globaltags = NULL;
5052 avi->got_tags = FALSE;
5056 gst_avi_demux_loop (GstPad * pad)
5059 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
5061 switch (avi->state) {
5062 case GST_AVI_DEMUX_START:
5063 res = gst_avi_demux_stream_init_pull (avi);
5064 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5065 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5068 avi->state = GST_AVI_DEMUX_HEADER;
5070 case GST_AVI_DEMUX_HEADER:
5071 res = gst_avi_demux_stream_header_pull (avi);
5072 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5073 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5076 avi->state = GST_AVI_DEMUX_MOVI;
5078 case GST_AVI_DEMUX_MOVI:
5079 if (G_UNLIKELY (avi->seg_event)) {
5080 gst_avi_demux_push_event (avi, avi->seg_event);
5081 avi->seg_event = NULL;
5083 if (G_UNLIKELY (avi->got_tags)) {
5084 push_tag_lists (avi);
5086 /* process each index entry in turn */
5087 res = gst_avi_demux_loop_data (avi);
5089 /* pause when error */
5090 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5091 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
5096 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
5097 res = GST_FLOW_ERROR;
5106 gboolean push_eos = FALSE;
5107 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
5108 gst_pad_pause_task (avi->sinkpad);
5110 if (res == GST_FLOW_UNEXPECTED) {
5111 /* handle end-of-stream/segment */
5112 /* so align our position with the end of it, if there is one
5113 * this ensures a subsequent will arrive at correct base/acc time */
5114 if (avi->segment.rate > 0.0 &&
5115 GST_CLOCK_TIME_IS_VALID (avi->segment.stop))
5116 avi->segment.position = avi->segment.stop;
5117 else if (avi->segment.rate < 0.0)
5118 avi->segment.position = avi->segment.start;
5119 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5122 if ((stop = avi->segment.stop) == -1)
5123 stop = avi->segment.duration;
5125 GST_INFO_OBJECT (avi, "sending segment_done");
5127 gst_element_post_message
5128 (GST_ELEMENT_CAST (avi),
5129 gst_message_new_segment_done (GST_OBJECT_CAST (avi),
5130 GST_FORMAT_TIME, stop));
5134 } else if (res == GST_FLOW_NOT_LINKED || res < GST_FLOW_UNEXPECTED) {
5135 /* for fatal errors we post an error message, wrong-state is
5136 * not fatal because it happens due to flushes and only means
5137 * that we should stop now. */
5138 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
5139 (_("Internal data stream error.")),
5140 ("streaming stopped, reason %s", gst_flow_get_name (res)));
5144 GST_INFO_OBJECT (avi, "sending eos");
5145 if (!gst_avi_demux_push_event (avi, gst_event_new_eos ()) &&
5146 (res == GST_FLOW_UNEXPECTED)) {
5147 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
5148 (NULL), ("got eos but no streams (yet)"));
5155 static GstFlowReturn
5156 gst_avi_demux_chain (GstPad * pad, GstBuffer * buf)
5159 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
5162 if (GST_BUFFER_IS_DISCONT (buf)) {
5163 GST_DEBUG_OBJECT (avi, "got DISCONT");
5164 gst_adapter_clear (avi->adapter);
5165 /* mark all streams DISCONT */
5166 for (i = 0; i < avi->num_streams; i++)
5167 avi->stream[i].discont = TRUE;
5170 GST_DEBUG ("Store %d bytes in adapter", gst_buffer_get_size (buf));
5171 gst_adapter_push (avi->adapter, buf);
5173 switch (avi->state) {
5174 case GST_AVI_DEMUX_START:
5175 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
5176 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5180 case GST_AVI_DEMUX_HEADER:
5181 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
5182 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5186 case GST_AVI_DEMUX_MOVI:
5187 if (G_UNLIKELY (avi->seg_event)) {
5188 gst_avi_demux_push_event (avi, avi->seg_event);
5189 avi->seg_event = NULL;
5191 if (G_UNLIKELY (avi->got_tags)) {
5192 push_tag_lists (avi);
5194 res = gst_avi_demux_stream_data (avi);
5196 case GST_AVI_DEMUX_SEEK:
5202 /* obtain and parse indexes */
5203 if (avi->stream[0].indexes && !gst_avi_demux_read_subindexes_push (avi))
5204 /* seek in subindex read function failed */
5207 if (!avi->stream[0].indexes && !avi->have_index
5208 && avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
5209 gst_avi_demux_stream_index_push (avi);
5211 if (avi->have_index) {
5212 /* use the indexes now to construct nice durations */
5213 gst_avi_demux_calculate_durations_from_index (avi);
5215 /* still parsing indexes */
5219 GST_OBJECT_LOCK (avi);
5220 event = avi->seek_event;
5221 avi->seek_event = NULL;
5222 GST_OBJECT_UNLOCK (avi);
5224 /* calculate and perform seek */
5225 if (!avi_demux_handle_seek_push (avi, avi->sinkpad, event))
5228 gst_event_unref (event);
5229 avi->state = GST_AVI_DEMUX_MOVI;
5233 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
5234 ("Illegal internal state"));
5235 res = GST_FLOW_ERROR;
5239 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
5240 gst_flow_get_name (res));
5242 if (G_UNLIKELY (avi->abort_buffering))
5243 goto abort_buffering;
5250 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("failed to read indexes"));
5251 return GST_FLOW_ERROR;
5255 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("push mode seek failed"));
5256 return GST_FLOW_ERROR;
5260 avi->abort_buffering = FALSE;
5261 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
5262 return GST_FLOW_ERROR;
5267 gst_avi_demux_sink_activate (GstPad * sinkpad)
5272 query = gst_query_new_scheduling ();
5274 if (!gst_pad_peer_query (sinkpad, query)) {
5275 gst_query_unref (query);
5279 gst_query_parse_scheduling (query, &pull_mode, NULL, NULL, NULL, NULL, NULL);
5280 gst_query_unref (query);
5285 GST_DEBUG_OBJECT (sinkpad, "activating pull");
5286 return gst_pad_activate_pull (sinkpad, TRUE);
5290 GST_DEBUG_OBJECT (sinkpad, "activating push");
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->streaming = FALSE;
5302 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
5305 return gst_pad_stop_task (sinkpad);
5310 gst_avi_demux_activate_push (GstPad * pad, gboolean active)
5312 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (pad));
5315 GST_DEBUG ("avi: activating push/chain function");
5316 avi->streaming = TRUE;
5318 /* create index for some push based seeking if not provided */
5319 GST_OBJECT_LOCK (avi);
5320 if (!avi->element_index) {
5321 GST_DEBUG_OBJECT (avi, "creating index");
5322 avi->element_index = gst_index_factory_make ("memindex");
5324 GST_OBJECT_UNLOCK (avi);
5325 /* object lock might be taken again */
5326 gst_index_get_writer_id (avi->element_index, GST_OBJECT_CAST (avi),
5330 GST_DEBUG ("avi: deactivating push/chain function");
5337 gst_avi_demux_set_index (GstElement * element, GstIndex * index)
5339 GstAviDemux *avi = GST_AVI_DEMUX (element);
5341 GST_OBJECT_LOCK (avi);
5342 if (avi->element_index)
5343 gst_object_unref (avi->element_index);
5345 avi->element_index = gst_object_ref (index);
5347 avi->element_index = NULL;
5349 GST_OBJECT_UNLOCK (avi);
5350 /* object lock might be taken again */
5352 gst_index_get_writer_id (index, GST_OBJECT_CAST (element), &avi->index_id);
5353 GST_DEBUG_OBJECT (avi, "Set index %" GST_PTR_FORMAT, avi->element_index);
5357 gst_avi_demux_get_index (GstElement * element)
5359 GstIndex *result = NULL;
5360 GstAviDemux *avi = GST_AVI_DEMUX (element);
5362 GST_OBJECT_LOCK (avi);
5363 if (avi->element_index)
5364 result = gst_object_ref (avi->element_index);
5365 GST_OBJECT_UNLOCK (avi);
5367 GST_DEBUG_OBJECT (avi, "Returning index %" GST_PTR_FORMAT, result);
5372 static GstStateChangeReturn
5373 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
5375 GstStateChangeReturn ret;
5376 GstAviDemux *avi = GST_AVI_DEMUX (element);
5378 switch (transition) {
5379 case GST_STATE_CHANGE_READY_TO_PAUSED:
5380 avi->streaming = FALSE;
5381 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
5387 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5388 if (ret == GST_STATE_CHANGE_FAILURE)
5391 switch (transition) {
5392 case GST_STATE_CHANGE_PAUSED_TO_READY:
5393 avi->have_index = FALSE;
5394 gst_avi_demux_reset (avi);