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,
83 GstObject * parent, GstEvent * event);
84 static gboolean gst_avi_demux_handle_sink_event (GstPad * pad,
85 GstObject * parent, GstEvent * event);
86 static gboolean gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event);
89 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
91 static gboolean gst_avi_demux_handle_src_query (GstPad * pad,
92 GstObject * parent, GstQuery * query);
93 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
94 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
96 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
97 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
99 static gboolean gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
101 static void gst_avi_demux_loop (GstPad * pad);
102 static gboolean gst_avi_demux_sink_activate (GstPad * sinkpad,
104 static gboolean gst_avi_demux_sink_activate_mode (GstPad * sinkpad,
105 GstObject * parent, GstPadMode mode, gboolean active);
106 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstObject * parent,
109 static void gst_avi_demux_set_index (GstElement * element, GstIndex * index);
110 static GstIndex *gst_avi_demux_get_index (GstElement * element);
112 static GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
113 GstStateChange transition);
114 static void gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi);
115 static void gst_avi_demux_get_buffer_info (GstAviDemux * avi,
116 GstAviStream * stream, guint entry_n, GstClockTime * timestamp,
117 GstClockTime * ts_end, guint64 * offset, guint64 * offset_end);
119 static void gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf);
121 /* GObject methods */
123 #define gst_avi_demux_parent_class parent_class
124 G_DEFINE_TYPE (GstAviDemux, gst_avi_demux, GST_TYPE_ELEMENT);
127 gst_avi_demux_class_init (GstAviDemuxClass * klass)
129 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
130 GObjectClass *gobject_class = (GObjectClass *) klass;
131 GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl;
132 GstCaps *audcaps, *vidcaps, *subcaps;
134 GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
135 0, "Demuxer for AVI streams");
137 gobject_class->finalize = gst_avi_demux_finalize;
139 gstelement_class->change_state =
140 GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
142 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_avi_demux_set_index);
143 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_avi_demux_get_index);
146 audcaps = gst_riff_create_audio_template_caps ();
147 gst_caps_append (audcaps, gst_caps_new_empty_simple ("audio/x-avi-unknown"));
148 audiosrctempl = gst_pad_template_new ("audio_%u",
149 GST_PAD_SRC, GST_PAD_SOMETIMES, audcaps);
151 vidcaps = gst_riff_create_video_template_caps ();
152 gst_caps_append (vidcaps, gst_riff_create_iavs_template_caps ());
153 gst_caps_append (vidcaps, gst_caps_new_empty_simple ("video/x-avi-unknown"));
154 videosrctempl = gst_pad_template_new ("video_%u",
155 GST_PAD_SRC, GST_PAD_SOMETIMES, vidcaps);
157 subcaps = gst_caps_new_empty_simple ("application/x-subtitle-avi");
158 subsrctempl = gst_pad_template_new ("subtitle_%u",
159 GST_PAD_SRC, GST_PAD_SOMETIMES, subcaps);
160 gst_element_class_add_pad_template (gstelement_class, audiosrctempl);
161 gst_element_class_add_pad_template (gstelement_class, videosrctempl);
162 gst_element_class_add_pad_template (gstelement_class, subsrctempl);
163 gst_element_class_add_pad_template (gstelement_class,
164 gst_static_pad_template_get (&sink_templ));
166 gst_element_class_set_details_simple (gstelement_class, "Avi demuxer",
168 "Demultiplex an avi file into audio and video",
169 "Erik Walthinsen <omega@cse.ogi.edu>, "
170 "Wim Taymans <wim.taymans@chello.be>, "
171 "Thijs Vermeir <thijsvermeir@gmail.com>");
175 gst_avi_demux_init (GstAviDemux * avi)
177 avi->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
178 gst_pad_set_activate_function (avi->sinkpad,
179 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate));
180 gst_pad_set_activatemode_function (avi->sinkpad,
181 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate_mode));
182 gst_pad_set_chain_function (avi->sinkpad,
183 GST_DEBUG_FUNCPTR (gst_avi_demux_chain));
184 gst_pad_set_event_function (avi->sinkpad,
185 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_sink_event));
186 gst_element_add_pad (GST_ELEMENT_CAST (avi), avi->sinkpad);
188 avi->adapter = gst_adapter_new ();
190 gst_avi_demux_reset (avi);
192 GST_OBJECT_FLAG_SET (avi, GST_ELEMENT_FLAG_INDEXABLE);
196 gst_avi_demux_finalize (GObject * object)
198 GstAviDemux *avi = GST_AVI_DEMUX (object);
200 GST_DEBUG ("AVI: finalize");
202 g_object_unref (avi->adapter);
204 G_OBJECT_CLASS (parent_class)->finalize (object);
208 gst_avi_demux_reset_stream (GstAviDemux * avi, GstAviStream * stream)
210 g_free (stream->strh);
211 g_free (stream->strf.data);
212 g_free (stream->name);
213 g_free (stream->index);
214 g_free (stream->indexes);
215 if (stream->initdata)
216 gst_buffer_unref (stream->initdata);
217 if (stream->extradata)
218 gst_buffer_unref (stream->extradata);
220 if (stream->exposed) {
221 gst_pad_set_active (stream->pad, FALSE);
222 gst_element_remove_pad (GST_ELEMENT_CAST (avi), stream->pad);
224 gst_object_unref (stream->pad);
226 if (stream->taglist) {
227 gst_tag_list_free (stream->taglist);
228 stream->taglist = NULL;
230 memset (stream, 0, sizeof (GstAviStream));
234 gst_avi_demux_reset (GstAviDemux * avi)
238 GST_DEBUG ("AVI: reset");
240 for (i = 0; i < avi->num_streams; i++)
241 gst_avi_demux_reset_stream (avi, &avi->stream[i]);
243 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
244 avi->num_streams = 0;
245 avi->num_v_streams = 0;
246 avi->num_a_streams = 0;
247 avi->num_t_streams = 0;
248 avi->main_stream = -1;
250 avi->state = GST_AVI_DEMUX_START;
252 avi->building_index = FALSE;
254 avi->index_offset = 0;
259 if (avi->element_index)
260 gst_object_unref (avi->element_index);
261 avi->element_index = NULL;
264 if (avi->seg_event) {
265 gst_event_unref (avi->seg_event);
266 avi->seg_event = NULL;
268 if (avi->seek_event) {
269 gst_event_unref (avi->seek_event);
270 avi->seek_event = NULL;
274 gst_tag_list_free (avi->globaltags);
275 avi->globaltags = NULL;
277 avi->got_tags = TRUE; /* we always want to push global tags */
278 avi->have_eos = FALSE;
279 avi->seekable = TRUE;
281 gst_adapter_clear (avi->adapter);
283 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
287 /* GstElement methods */
290 static const GstFormat *
291 gst_avi_demux_get_src_formats (GstPad * pad)
293 GstAviStream *stream = gst_pad_get_element_private (pad);
295 static const GstFormat src_a_formats[] = {
301 static const GstFormat src_v_formats[] = {
307 return (stream->strh->type == GST_RIFF_FCC_auds ?
308 src_a_formats : src_v_formats);
312 /* assumes stream->strf.auds->av_bps != 0 */
313 static inline GstClockTime
314 avi_stream_convert_bytes_to_time_unchecked (GstAviStream * stream,
317 return gst_util_uint64_scale_int (bytes, GST_SECOND,
318 stream->strf.auds->av_bps);
321 static inline guint64
322 avi_stream_convert_time_to_bytes_unchecked (GstAviStream * stream,
325 return gst_util_uint64_scale_int (time, stream->strf.auds->av_bps,
329 /* assumes stream->strh->rate != 0 */
330 static inline GstClockTime
331 avi_stream_convert_frames_to_time_unchecked (GstAviStream * stream,
334 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
338 static inline guint64
339 avi_stream_convert_time_to_frames_unchecked (GstAviStream * stream,
342 return gst_util_uint64_scale (time, stream->strh->rate,
343 stream->strh->scale * GST_SECOND);
347 gst_avi_demux_src_convert (GstPad * pad,
348 GstFormat src_format,
349 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
351 GstAviStream *stream = gst_pad_get_element_private (pad);
355 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
356 ", dest_format:%s", gst_format_get_name (src_format), src_value,
357 gst_format_get_name (*dest_format));
359 if (G_UNLIKELY (src_format == *dest_format)) {
360 *dest_value = src_value;
363 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
367 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
368 (src_format == GST_FORMAT_BYTES
369 || *dest_format == GST_FORMAT_BYTES))) {
374 switch (src_format) {
375 case GST_FORMAT_TIME:
376 switch (*dest_format) {
377 case GST_FORMAT_BYTES:
378 *dest_value = gst_util_uint64_scale_int (src_value,
379 stream->strf.auds->av_bps, GST_SECOND);
381 case GST_FORMAT_DEFAULT:
383 gst_util_uint64_scale_round (src_value, stream->strh->rate,
384 stream->strh->scale * GST_SECOND);
391 case GST_FORMAT_BYTES:
392 switch (*dest_format) {
393 case GST_FORMAT_TIME:
394 if (stream->strf.auds->av_bps != 0) {
395 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
405 case GST_FORMAT_DEFAULT:
406 switch (*dest_format) {
407 case GST_FORMAT_TIME:
409 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
422 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
423 gst_format_get_name (*dest_format), *dest_value);
428 gst_avi_demux_handle_src_query (GstPad * pad, GstObject * parent,
432 GstAviDemux *avi = GST_AVI_DEMUX (parent);
434 GstAviStream *stream = gst_pad_get_element_private (pad);
436 if (!stream->strh || !stream->strf.data)
437 return gst_pad_query_default (pad, parent, query);
439 switch (GST_QUERY_TYPE (query)) {
440 case GST_QUERY_POSITION:{
443 GST_DEBUG ("pos query for stream %u: frames %u, bytes %u",
444 stream->num, stream->current_entry, stream->current_total);
446 /* FIXME, this looks clumsy */
447 if (stream->strh->type == GST_RIFF_FCC_auds) {
448 if (stream->is_vbr) {
450 pos = gst_util_uint64_scale ((gint64) stream->current_entry *
451 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
452 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
453 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
454 } else if (stream->strf.auds->av_bps != 0) {
456 pos = gst_util_uint64_scale (stream->current_total, GST_SECOND,
457 (guint64) stream->strf.auds->av_bps);
458 GST_DEBUG_OBJECT (avi,
459 "CBR convert bytes %u, time %" GST_TIME_FORMAT,
460 stream->current_total, GST_TIME_ARGS (pos));
461 } else if (stream->idx_n != 0 && stream->total_bytes != 0) {
462 /* calculate timestamps based on percentage of length */
463 guint64 xlen = avi->avih->us_frame *
464 avi->avih->tot_frames * GST_USECOND;
466 if (stream->is_vbr) {
467 pos = gst_util_uint64_scale (xlen, stream->current_entry,
469 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
470 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
472 pos = gst_util_uint64_scale (xlen, stream->current_total,
473 stream->total_bytes);
474 GST_DEBUG_OBJECT (avi,
475 "CBR perc convert bytes %u, time %" GST_TIME_FORMAT,
476 stream->current_total, GST_TIME_ARGS (pos));
483 if (stream->strh->rate != 0) {
484 pos = gst_util_uint64_scale ((guint64) stream->current_entry *
485 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
487 pos = stream->current_entry * avi->avih->us_frame * GST_USECOND;
491 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
492 gst_query_set_position (query, GST_FORMAT_TIME, pos);
494 GST_WARNING ("pos query failed");
497 case GST_QUERY_DURATION:
500 GstClockTime duration;
502 /* only act on audio or video streams */
503 if (stream->strh->type != GST_RIFF_FCC_auds &&
504 stream->strh->type != GST_RIFF_FCC_vids) {
509 /* take stream duration, fall back to avih duration */
510 if ((duration = stream->duration) == -1)
511 duration = avi->duration;
513 gst_query_parse_duration (query, &fmt, NULL);
516 case GST_FORMAT_TIME:
517 gst_query_set_duration (query, fmt, duration);
519 case GST_FORMAT_DEFAULT:
522 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
525 if (stream->idx_n > 0)
526 gst_query_set_duration (query, fmt, stream->idx_n);
527 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
528 duration, fmt, &dur))
529 gst_query_set_duration (query, fmt, dur);
538 case GST_QUERY_SEEKING:{
541 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
542 if (fmt == GST_FORMAT_TIME) {
543 gboolean seekable = TRUE;
545 if (avi->streaming) {
546 seekable = avi->seekable;
549 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
550 0, stream->duration);
555 case GST_QUERY_CONVERT:{
556 GstFormat src_fmt, dest_fmt;
557 gint64 src_val, dest_val;
559 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
560 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
562 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
564 res = gst_pad_query_default (pad, parent, query);
568 res = gst_pad_query_default (pad, parent, query);
576 static const GstEventMask *
577 gst_avi_demux_get_event_mask (GstPad * pad)
579 static const GstEventMask masks[] = {
580 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
590 gst_avi_demux_seek_streams (GstAviDemux * avi, guint64 offset, gboolean before)
592 GstAviStream *stream;
593 GstIndexEntry *entry;
595 gint64 val, min = offset;
597 for (i = 0; i < avi->num_streams; i++) {
598 stream = &avi->stream[i];
600 entry = gst_index_get_assoc_entry (avi->element_index, stream->index_id,
601 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
602 GST_ASSOCIATION_FLAG_NONE, GST_FORMAT_BYTES, offset);
606 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
607 GST_DEBUG_OBJECT (avi, "stream %d, previous entry at %"
608 G_GUINT64_FORMAT, i, val);
616 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
617 stream->current_entry = 0;
618 stream->current_total = 0;
622 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
623 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT,
626 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &val);
627 stream->current_total = val;
628 gst_index_entry_assoc_map (entry, GST_FORMAT_DEFAULT, &val);
629 stream->current_entry = val;
637 gst_avi_demux_index_entry_offset_search (GstAviIndexEntry * entry,
640 if (entry->offset < *offset)
642 else if (entry->offset > *offset)
648 gst_avi_demux_seek_streams_index (GstAviDemux * avi, guint64 offset,
651 GstAviStream *stream;
652 GstAviIndexEntry *entry;
654 gint64 val, min = offset;
657 for (i = 0; i < avi->num_streams; i++) {
658 stream = &avi->stream[i];
660 /* compensate for chunk header */
663 gst_util_array_binary_search (stream->index, stream->idx_n,
664 sizeof (GstAviIndexEntry),
665 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
666 before ? GST_SEARCH_MODE_BEFORE : GST_SEARCH_MODE_AFTER, &offset, NULL);
670 index = entry - stream->index;
674 val = stream->index[index].offset;
675 GST_DEBUG_OBJECT (avi,
676 "stream %d, previous entry at %" G_GUINT64_FORMAT, i, val);
684 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
685 stream->current_entry = 0;
686 stream->current_total = 0;
690 val = stream->index[index].offset - 8;
691 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT, i,
694 stream->current_total = stream->index[index].total;
695 stream->current_entry = index;
701 #define GST_AVI_SEEK_PUSH_DISPLACE (4 * GST_SECOND)
704 gst_avi_demux_handle_sink_event (GstPad * pad, GstObject * parent,
708 GstAviDemux *avi = GST_AVI_DEMUX (parent);
710 GST_DEBUG_OBJECT (avi,
711 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
713 switch (GST_EVENT_TYPE (event)) {
714 case GST_EVENT_SEGMENT:
716 gint64 boffset, offset = 0;
719 /* some debug output */
720 gst_event_copy_segment (event, &segment);
721 GST_DEBUG_OBJECT (avi, "received newsegment %" GST_SEGMENT_FORMAT,
724 /* chain will send initial newsegment after pads have been added */
725 if (avi->state != GST_AVI_DEMUX_MOVI) {
726 GST_DEBUG_OBJECT (avi, "still starting, eating event");
730 /* we only expect a BYTE segment, e.g. following a seek */
731 if (segment.format != GST_FORMAT_BYTES) {
732 GST_DEBUG_OBJECT (avi, "unsupported segment format, ignoring");
736 if (avi->have_index) {
737 GstAviIndexEntry *entry;
738 guint i = 0, index = 0, k = 0;
739 GstAviStream *stream;
741 /* compensate chunk header, stored index offset points after header */
742 boffset = segment.start + 8;
743 /* find which stream we're on */
745 stream = &avi->stream[i];
747 /* find the index for start bytes offset */
748 entry = gst_util_array_binary_search (stream->index,
749 stream->idx_n, sizeof (GstAviIndexEntry),
750 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
751 GST_SEARCH_MODE_AFTER, &boffset, NULL);
755 index = entry - stream->index;
757 /* we are on the stream with a chunk start offset closest to start */
758 if (!offset || stream->index[index].offset < offset) {
759 offset = stream->index[index].offset;
762 /* exact match needs no further searching */
763 if (stream->index[index].offset == segment.start)
765 } while (++i < avi->num_streams);
768 stream = &avi->stream[k];
770 /* so we have no idea what is to come, or where we are */
772 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
776 /* get the ts corresponding to start offset bytes for the stream */
777 gst_avi_demux_get_buffer_info (avi, stream, index,
778 (GstClockTime *) & segment.time, NULL, NULL, NULL);
780 } else if (avi->element_index) {
781 GstIndexEntry *entry;
783 /* Let's check if we have an index entry for this position */
784 entry = gst_index_get_assoc_entry (avi->element_index, avi->index_id,
785 GST_INDEX_LOOKUP_AFTER, GST_ASSOCIATION_FLAG_NONE,
786 GST_FORMAT_BYTES, segment.start);
788 /* we can not go where we have not yet been before ... */
790 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
794 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME,
795 (gint64 *) & segment.time);
796 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &offset);
799 GST_WARNING_OBJECT (avi, "no index data, forcing EOS");
803 segment.format = GST_FORMAT_TIME;
804 segment.start = segment.time;
805 segment.stop = GST_CLOCK_TIME_NONE;
806 segment.position = segment.start;
808 /* rescue duration */
809 segment.duration = avi->segment.duration;
811 /* set up segment and send downstream */
812 gst_segment_copy_into (&segment, &avi->segment);
814 GST_DEBUG_OBJECT (avi, "Pushing newseg %" GST_SEGMENT_FORMAT, &segment);
815 gst_avi_demux_push_event (avi, gst_event_new_segment (&segment));
817 GST_DEBUG_OBJECT (avi, "next chunk expected at %" G_GINT64_FORMAT,
820 /* adjust state for streaming thread accordingly */
822 gst_avi_demux_seek_streams_index (avi, offset, FALSE);
825 gst_avi_demux_seek_streams (avi, offset, FALSE);
828 /* set up streaming thread */
829 g_assert (offset >= boffset);
830 avi->offset = boffset;
831 avi->todrop = offset - boffset;
834 gst_event_unref (event);
839 avi->have_eos = TRUE;
844 if (avi->state != GST_AVI_DEMUX_MOVI) {
845 gst_event_unref (event);
846 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
847 (NULL), ("got eos and didn't receive a complete header object"));
848 } else if (!gst_avi_demux_push_event (avi, event)) {
849 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
850 (NULL), ("got eos but no streams (yet)"));
854 case GST_EVENT_FLUSH_STOP:
858 gst_adapter_clear (avi->adapter);
859 avi->have_eos = FALSE;
860 for (i = 0; i < avi->num_streams; i++) {
861 avi->stream[i].last_flow = GST_FLOW_OK;
862 avi->stream[i].discont = TRUE;
864 /* fall through to default case so that the event gets passed downstream */
867 res = gst_pad_event_default (pad, parent, event);
875 gst_avi_demux_handle_src_event (GstPad * pad, GstObject * parent,
879 GstAviDemux *avi = GST_AVI_DEMUX (parent);
881 GST_DEBUG_OBJECT (avi,
882 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
884 switch (GST_EVENT_TYPE (event)) {
886 if (!avi->streaming) {
887 res = gst_avi_demux_handle_seek (avi, pad, event);
889 res = gst_avi_demux_handle_seek_push (avi, pad, event);
891 gst_event_unref (event);
894 case GST_EVENT_NAVIGATION:
896 gst_event_unref (event);
899 res = gst_pad_event_default (pad, parent, event);
906 /* streaming helper (push) */
909 * gst_avi_demux_peek_chunk_info:
911 * @tag: holder for tag
912 * @size: holder for tag size
914 * Peek next chunk info (tag and size)
916 * Returns: TRUE when one chunk info has been got
919 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
921 const guint8 *data = NULL;
923 if (gst_adapter_available (avi->adapter) < 8)
926 data = gst_adapter_map (avi->adapter, 8);
927 *tag = GST_READ_UINT32_LE (data);
928 *size = GST_READ_UINT32_LE (data + 4);
929 gst_adapter_unmap (avi->adapter);
935 * gst_avi_demux_peek_chunk:
937 * @tag: holder for tag
938 * @size: holder for tag size
940 * Peek enough data for one full chunk
942 * Returns: %TRUE when one chunk has been got
945 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
947 guint32 peek_size = 0;
950 if (!gst_avi_demux_peek_chunk_info (avi, tag, size))
953 /* size 0 -> empty data buffer would surprise most callers,
954 * large size -> do not bother trying to squeeze that into adapter,
955 * so we throw poor man's exception, which can be caught if caller really
956 * wants to handle 0 size chunk */
957 if (!(*size) || (*size) >= (1 << 30))
960 peek_size = (*size + 1) & ~1;
961 available = gst_adapter_available (avi->adapter);
963 GST_DEBUG_OBJECT (avi,
964 "Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
965 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
967 if (available < (8 + peek_size))
975 GST_INFO_OBJECT (avi, "Failed to peek");
980 GST_INFO_OBJECT (avi,
981 "Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT, *size,
982 GST_FOURCC_ARGS (*tag));
983 /* chain should give up */
984 avi->abort_buffering = TRUE;
989 GST_INFO_OBJECT (avi, "need more %d < %" G_GUINT32_FORMAT,
990 available, 8 + peek_size);
998 * gst_avi_demux_parse_file_header:
999 * @element: caller element (used for errors/debug).
1000 * @buf: input data to be used for parsing.
1002 * "Open" a RIFF/AVI file. The buffer should be at least 12
1003 * bytes long. Takes ownership of @buf.
1005 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
1006 * Throws an error, caller should error out (fatal).
1009 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
1014 stamp = gst_util_get_timestamp ();
1016 /* riff_parse posts an error */
1017 if (!gst_riff_parse_file_header (element, buf, &doctype))
1020 if (doctype != GST_RIFF_RIFF_AVI)
1023 stamp = gst_util_get_timestamp () - stamp;
1024 GST_DEBUG_OBJECT (element, "header parsing took %" GST_TIME_FORMAT,
1025 GST_TIME_ARGS (stamp));
1032 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
1033 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
1034 GST_FOURCC_ARGS (doctype)));
1040 * Read AVI file tag when streaming
1042 static GstFlowReturn
1043 gst_avi_demux_stream_init_push (GstAviDemux * avi)
1045 if (gst_adapter_available (avi->adapter) >= 12) {
1048 tmp = gst_adapter_take_buffer (avi->adapter, 12);
1050 GST_DEBUG ("Parsing avi header");
1051 if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), tmp)) {
1052 return GST_FLOW_ERROR;
1054 GST_DEBUG ("header ok");
1057 avi->state = GST_AVI_DEMUX_HEADER;
1065 static GstFlowReturn
1066 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
1069 GstBuffer *buf = NULL;
1071 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
1072 if (res != GST_FLOW_OK)
1074 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
1084 GST_DEBUG_OBJECT (avi, "error parsing file header");
1085 return GST_FLOW_ERROR;
1089 /* AVI header handling */
1091 * gst_avi_demux_parse_avih:
1092 * @avi: caller element (used for errors/debug).
1093 * @buf: input data to be used for parsing.
1094 * @avih: pointer to structure (filled in by function) containing
1095 * stream information (such as flags, number of streams, etc.).
1097 * Read 'avih' header. Discards buffer after use.
1099 * Returns: TRUE on success, FALSE otherwise. Throws an error if
1100 * the header is invalid. The caller should error out
1104 gst_avi_demux_parse_avih (GstAviDemux * avi,
1105 GstBuffer * buf, gst_riff_avih ** _avih)
1107 gst_riff_avih *avih;
1113 size = gst_buffer_get_size (buf);
1114 if (size < sizeof (gst_riff_avih))
1115 goto avih_too_small;
1117 avih = g_malloc (size);
1118 gst_buffer_extract (buf, 0, avih, size);
1120 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1121 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
1122 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
1123 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
1124 avih->flags = GUINT32_FROM_LE (avih->flags);
1125 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
1126 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
1127 avih->streams = GUINT32_FROM_LE (avih->streams);
1128 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
1129 avih->width = GUINT32_FROM_LE (avih->width);
1130 avih->height = GUINT32_FROM_LE (avih->height);
1131 avih->scale = GUINT32_FROM_LE (avih->scale);
1132 avih->rate = GUINT32_FROM_LE (avih->rate);
1133 avih->start = GUINT32_FROM_LE (avih->start);
1134 avih->length = GUINT32_FROM_LE (avih->length);
1138 GST_INFO_OBJECT (avi, "avih tag found:");
1139 GST_INFO_OBJECT (avi, " us_frame %u", avih->us_frame);
1140 GST_INFO_OBJECT (avi, " max_bps %u", avih->max_bps);
1141 GST_INFO_OBJECT (avi, " pad_gran %u", avih->pad_gran);
1142 GST_INFO_OBJECT (avi, " flags 0x%08x", avih->flags);
1143 GST_INFO_OBJECT (avi, " tot_frames %u", avih->tot_frames);
1144 GST_INFO_OBJECT (avi, " init_frames %u", avih->init_frames);
1145 GST_INFO_OBJECT (avi, " streams %u", avih->streams);
1146 GST_INFO_OBJECT (avi, " bufsize %u", avih->bufsize);
1147 GST_INFO_OBJECT (avi, " width %u", avih->width);
1148 GST_INFO_OBJECT (avi, " height %u", avih->height);
1149 GST_INFO_OBJECT (avi, " scale %u", avih->scale);
1150 GST_INFO_OBJECT (avi, " rate %u", avih->rate);
1151 GST_INFO_OBJECT (avi, " start %u", avih->start);
1152 GST_INFO_OBJECT (avi, " length %u", avih->length);
1155 gst_buffer_unref (buf);
1157 if (avih->us_frame != 0 && avih->tot_frames != 0)
1159 (guint64) avih->us_frame * (guint64) avih->tot_frames * 1000;
1161 avi->duration = GST_CLOCK_TIME_NONE;
1163 GST_INFO_OBJECT (avi, " header duration %" GST_TIME_FORMAT,
1164 GST_TIME_ARGS (avi->duration));
1171 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No buffer"));
1176 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
1177 ("Too small avih (%" G_GSIZE_FORMAT " available, %d needed)",
1178 size, (int) sizeof (gst_riff_avih)));
1179 gst_buffer_unref (buf);
1185 * gst_avi_demux_parse_superindex:
1186 * @avi: caller element (used for debugging/errors).
1187 * @buf: input data to use for parsing.
1188 * @locations: locations in the file (byte-offsets) that contain
1189 * the actual indexes (see get_avi_demux_parse_subindex()).
1190 * The array ends with GST_BUFFER_OFFSET_NONE.
1192 * Reads superindex (openDML-2 spec stuff) from the provided data.
1194 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
1195 * on error, but they are not fatal.
1198 gst_avi_demux_parse_superindex (GstAviDemux * avi,
1199 GstBuffer * buf, guint64 ** _indexes)
1211 gst_buffer_map (buf, &map, GST_MAP_READ);
1222 /* check type of index. The opendml2 specs state that
1223 * there should be 4 dwords per array entry. Type can be
1224 * either frame or field (and we don't care). */
1225 if (GST_READ_UINT16_LE (data) != 4 ||
1226 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
1227 GST_WARNING_OBJECT (avi,
1228 "Superindex for stream has unexpected "
1229 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1230 GST_READ_UINT16_LE (data), data[2], data[3]);
1231 bpe = GST_READ_UINT16_LE (data) * 4;
1233 num = GST_READ_UINT32_LE (&data[4]);
1235 GST_DEBUG_OBJECT (avi, "got %d indexes", num);
1237 /* this can't work out well ... */
1238 if (num > G_MAXUINT32 >> 1 || bpe < 8) {
1239 goto invalid_params;
1242 indexes = g_new (guint64, num + 1);
1243 for (i = 0; i < num; i++) {
1244 if (size < 24 + bpe * (i + 1))
1246 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
1247 GST_DEBUG_OBJECT (avi, "index %d at %" G_GUINT64_FORMAT, i, indexes[i]);
1249 indexes[i] = GST_BUFFER_OFFSET_NONE;
1250 *_indexes = indexes;
1252 gst_buffer_unmap (buf, &map);
1253 gst_buffer_unref (buf);
1260 GST_ERROR_OBJECT (avi,
1261 "Not enough data to parse superindex (%" G_GSIZE_FORMAT
1262 " available, 24 needed)", size);
1264 gst_buffer_unmap (buf, &map);
1265 gst_buffer_unref (buf);
1271 GST_ERROR_OBJECT (avi, "invalid index parameters (num = %d, bpe = %d)",
1273 gst_buffer_unmap (buf, &map);
1274 gst_buffer_unref (buf);
1279 /* add an entry to the index of a stream. @num should be an estimate of the
1280 * total amount of index entries for all streams and is used to dynamically
1281 * allocate memory for the index entries. */
1282 static inline gboolean
1283 gst_avi_demux_add_index (GstAviDemux * avi, GstAviStream * stream,
1284 guint num, GstAviIndexEntry * entry)
1286 /* ensure index memory */
1287 if (G_UNLIKELY (stream->idx_n >= stream->idx_max)) {
1288 guint idx_max = stream->idx_max;
1289 GstAviIndexEntry *new_idx;
1291 /* we need to make some more room */
1293 /* initial size guess, assume each stream has an equal amount of entries,
1294 * overshoot with at least 8K */
1295 idx_max = (num / avi->num_streams) + (8192 / sizeof (GstAviIndexEntry));
1297 idx_max += 8192 / sizeof (GstAviIndexEntry);
1298 GST_DEBUG_OBJECT (avi, "expanded index from %u to %u",
1299 stream->idx_max, idx_max);
1301 new_idx = g_try_renew (GstAviIndexEntry, stream->index, idx_max);
1302 /* out of memory, if this fails stream->index is untouched. */
1303 if (G_UNLIKELY (!new_idx))
1306 stream->index = new_idx;
1307 stream->idx_max = idx_max;
1310 /* update entry total and stream stats. The entry total can be converted to
1311 * the timestamp of the entry easily. */
1312 if (stream->strh->type == GST_RIFF_FCC_auds) {
1315 if (stream->is_vbr) {
1316 entry->total = stream->total_blocks;
1318 entry->total = stream->total_bytes;
1320 blockalign = stream->strf.auds->blockalign;
1322 stream->total_blocks += DIV_ROUND_UP (entry->size, blockalign);
1324 stream->total_blocks++;
1326 if (stream->is_vbr) {
1327 entry->total = stream->idx_n;
1329 entry->total = stream->total_bytes;
1332 stream->total_bytes += entry->size;
1333 if (ENTRY_IS_KEYFRAME (entry))
1334 stream->n_keyframes++;
1337 GST_LOG_OBJECT (avi,
1338 "Adding stream %u, index entry %d, kf %d, size %u "
1339 ", offset %" G_GUINT64_FORMAT ", total %" G_GUINT64_FORMAT, stream->num,
1340 stream->idx_n, ENTRY_IS_KEYFRAME (entry), entry->size, entry->offset,
1342 stream->index[stream->idx_n++] = *entry;
1347 /* given @entry_n in @stream, calculate info such as timestamps and
1348 * offsets for the entry. */
1350 gst_avi_demux_get_buffer_info (GstAviDemux * avi, GstAviStream * stream,
1351 guint entry_n, GstClockTime * timestamp, GstClockTime * ts_end,
1352 guint64 * offset, guint64 * offset_end)
1354 GstAviIndexEntry *entry;
1356 entry = &stream->index[entry_n];
1358 if (stream->is_vbr) {
1359 /* VBR stream next timestamp */
1360 if (stream->strh->type == GST_RIFF_FCC_auds) {
1363 avi_stream_convert_frames_to_time_unchecked (stream, entry->total);
1365 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1370 avi_stream_convert_frames_to_time_unchecked (stream, entry_n);
1372 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1375 } else if (stream->strh->type == GST_RIFF_FCC_auds) {
1376 /* constant rate stream */
1379 avi_stream_convert_bytes_to_time_unchecked (stream, entry->total);
1381 *ts_end = avi_stream_convert_bytes_to_time_unchecked (stream,
1382 entry->total + entry->size);
1384 if (stream->strh->type == GST_RIFF_FCC_vids) {
1385 /* video offsets are the frame number */
1389 *offset_end = entry_n + 1;
1391 /* no offsets for audio */
1399 /* collect and debug stats about the indexes for all streams.
1400 * This method is also responsible for filling in the stream duration
1401 * as measured by the amount of index entries.
1403 * Returns TRUE if the index is not empty, else FALSE */
1405 gst_avi_demux_do_index_stats (GstAviDemux * avi)
1407 guint total_idx = 0;
1409 #ifndef GST_DISABLE_GST_DEBUG
1410 guint total_max = 0;
1413 /* get stream stats now */
1414 for (i = 0; i < avi->num_streams; i++) {
1415 GstAviStream *stream;
1417 if (G_UNLIKELY (!(stream = &avi->stream[i])))
1419 if (G_UNLIKELY (!stream->strh))
1421 if (G_UNLIKELY (!stream->index || stream->idx_n == 0))
1424 /* we interested in the end_ts of the last entry, which is the total
1425 * duration of this stream */
1426 gst_avi_demux_get_buffer_info (avi, stream, stream->idx_n - 1,
1427 NULL, &stream->idx_duration, NULL, NULL);
1429 total_idx += stream->idx_n;
1430 #ifndef GST_DISABLE_GST_DEBUG
1431 total_max += stream->idx_max;
1433 GST_INFO_OBJECT (avi, "Stream %d, dur %" GST_TIME_FORMAT ", %6u entries, "
1434 "%5u keyframes, entry size = %2u, total size = %10u, allocated %10u",
1435 i, GST_TIME_ARGS (stream->idx_duration), stream->idx_n,
1436 stream->n_keyframes, (guint) sizeof (GstAviIndexEntry),
1437 (guint) (stream->idx_n * sizeof (GstAviIndexEntry)),
1438 (guint) (stream->idx_max * sizeof (GstAviIndexEntry)));
1440 total_idx *= sizeof (GstAviIndexEntry);
1441 #ifndef GST_DISABLE_GST_DEBUG
1442 total_max *= sizeof (GstAviIndexEntry);
1444 GST_INFO_OBJECT (avi, "%u bytes for index vs %u ideally, %u wasted",
1445 total_max, total_idx, total_max - total_idx);
1447 if (total_idx == 0) {
1448 GST_WARNING_OBJECT (avi, "Index is empty !");
1455 * gst_avi_demux_parse_subindex:
1457 * @buf: input data to use for parsing.
1458 * @stream: stream context.
1459 * @entries_list: a list (returned by the function) containing all the
1460 * indexes parsed in this specific subindex. The first
1461 * entry is also a pointer to allocated memory that needs
1462 * to be free´ed. May be NULL if no supported indexes were
1465 * Reads superindex (openDML-2 spec stuff) from the provided data.
1466 * The buffer should contain a GST_RIFF_TAG_ix?? chunk.
1468 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1469 * throw an error, caller should bail out asap.
1472 gst_avi_demux_parse_subindex (GstAviDemux * avi, GstAviStream * stream,
1484 gst_buffer_map (buf, &map, GST_MAP_READ);
1491 /* We don't support index-data yet */
1493 goto not_implemented;
1495 /* check type of index. The opendml2 specs state that
1496 * there should be 4 dwords per array entry. Type can be
1497 * either frame or field (and we don't care). */
1498 bpe = (data[2] & 0x01) ? 12 : 8;
1499 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1500 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1501 GST_WARNING_OBJECT (avi,
1502 "Superindex for stream %d has unexpected "
1503 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1504 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1505 bpe = GST_READ_UINT16_LE (data) * 4;
1507 num = GST_READ_UINT32_LE (&data[4]);
1508 baseoff = GST_READ_UINT64_LE (&data[12]);
1510 /* If there's nothing, just return ! */
1514 GST_INFO_OBJECT (avi, "Parsing subindex, nr_entries = %6d", num);
1516 for (i = 0; i < num; i++) {
1517 GstAviIndexEntry entry;
1519 if (map.size < 24 + bpe * (i + 1))
1522 /* fill in offset and size. offset contains the keyframe flag in the
1524 entry.offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1525 entry.size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1527 if (stream->strh->type == GST_RIFF_FCC_auds) {
1528 /* all audio frames are keyframes */
1529 ENTRY_SET_KEYFRAME (&entry);
1531 /* else read flags */
1532 entry.flags = (entry.size & 0x80000000) ? 0 : GST_AVI_KEYFRAME;
1534 entry.size &= ~0x80000000;
1537 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
1541 gst_buffer_unmap (buf, &map);
1542 gst_buffer_unref (buf);
1549 GST_ERROR_OBJECT (avi,
1550 "Not enough data to parse subindex (%" G_GSIZE_FORMAT
1551 " available, 24 needed)", map.size);
1552 goto done; /* continue */
1556 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1557 ("Subindex-is-data is not implemented"));
1558 gst_buffer_unmap (buf, &map);
1559 gst_buffer_unref (buf);
1564 GST_DEBUG_OBJECT (avi, "the index is empty");
1565 goto done; /* continue */
1569 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1570 ("Cannot allocate memory for %u*%u=%u bytes",
1571 (guint) sizeof (GstAviIndexEntry), num,
1572 (guint) sizeof (GstAviIndexEntry) * num));
1573 gst_buffer_unmap (buf, &map);
1574 gst_buffer_unref (buf);
1580 * Create and push a flushing seek event upstream
1583 perform_seek_to_offset (GstAviDemux * demux, guint64 offset)
1588 GST_DEBUG_OBJECT (demux, "Seeking to %" G_GUINT64_FORMAT, offset);
1591 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1592 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1593 GST_SEEK_TYPE_NONE, -1);
1595 res = gst_pad_push_event (demux->sinkpad, event);
1598 demux->offset = offset;
1603 * Read AVI index when streaming
1606 gst_avi_demux_read_subindexes_push (GstAviDemux * avi)
1608 guint32 tag = 0, size;
1609 GstBuffer *buf = NULL;
1612 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1614 if (avi->odml_subidxs[avi->odml_subidx] != avi->offset)
1617 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1620 /* this is the ODML chunk we expect */
1621 odml_stream = avi->odml_stream;
1623 if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + odml_stream / 10,
1624 '0' + odml_stream % 10)) &&
1625 (tag != GST_MAKE_FOURCC ('0' + odml_stream / 10,
1626 '0' + odml_stream % 10, 'i', 'x'))) {
1627 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1628 GST_FOURCC_ARGS (tag));
1632 avi->offset += 8 + GST_ROUND_UP_2 (size);
1633 /* flush chunk header so we get just the 'size' payload data */
1634 gst_adapter_flush (avi->adapter, 8);
1635 buf = gst_adapter_take_buffer (avi->adapter, size);
1637 if (!gst_avi_demux_parse_subindex (avi, &avi->stream[odml_stream], buf))
1640 /* we parsed the index, go to next subindex */
1643 if (avi->odml_subidxs[avi->odml_subidx] == GST_BUFFER_OFFSET_NONE) {
1644 /* we reached the end of the indexes for this stream, move to the next
1645 * stream to handle the first index */
1647 avi->odml_subidx = 0;
1649 if (avi->odml_stream < avi->num_streams) {
1650 /* there are more indexes */
1651 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
1653 /* we're done, get stream stats now */
1654 avi->have_index = gst_avi_demux_do_index_stats (avi);
1660 /* seek to next index */
1661 return perform_seek_to_offset (avi, avi->odml_subidxs[avi->odml_subidx]);
1668 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi)
1674 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1676 for (n = 0; n < avi->num_streams; n++) {
1677 GstAviStream *stream = &avi->stream[n];
1679 if (stream->indexes == NULL)
1682 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1683 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi), avi->sinkpad,
1684 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1686 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1687 '0' + stream->num % 10)) &&
1688 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1689 '0' + stream->num % 10, 'i', 'x'))) {
1690 /* Some ODML files (created by god knows what muxer) have a ##ix format
1691 * instead of the 'official' ix##. They are still valid though. */
1692 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1693 GST_FOURCC_ARGS (tag));
1694 gst_buffer_unref (buf);
1698 if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1702 g_free (stream->indexes);
1703 stream->indexes = NULL;
1705 /* get stream stats now */
1706 avi->have_index = gst_avi_demux_do_index_stats (avi);
1710 * gst_avi_demux_riff_parse_vprp:
1711 * @element: caller element (used for debugging/error).
1712 * @buf: input data to be used for parsing, stripped from header.
1713 * @vprp: a pointer (returned by this function) to a filled-in vprp
1714 * structure. Caller should free it.
1716 * Parses a video stream´s vprp. This function takes ownership of @buf.
1718 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1719 * should be skipped on error, but it is not fatal.
1722 gst_avi_demux_riff_parse_vprp (GstElement * element,
1723 GstBuffer * buf, gst_riff_vprp ** _vprp)
1725 gst_riff_vprp *vprp;
1729 g_return_val_if_fail (buf != NULL, FALSE);
1730 g_return_val_if_fail (_vprp != NULL, FALSE);
1732 size = gst_buffer_get_size (buf);
1734 if (size < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1737 vprp = g_malloc (size);
1738 gst_buffer_extract (buf, 0, vprp, size);
1740 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1741 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1742 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1743 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1744 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1745 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1746 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1747 vprp->width = GUINT32_FROM_LE (vprp->width);
1748 vprp->height = GUINT32_FROM_LE (vprp->height);
1749 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1753 /* calculate fields based on size */
1754 k = (size - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) / vprp->fields;
1755 if (vprp->fields > k) {
1756 GST_WARNING_OBJECT (element,
1757 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1760 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1761 GST_WARNING_OBJECT (element,
1762 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1763 GST_RIFF_VPRP_VIDEO_FIELDS);
1764 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1766 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1767 for (k = 0; k < vprp->fields; k++) {
1768 gst_riff_vprp_video_field_desc *fd;
1770 fd = &vprp->field_info[k];
1771 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1772 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1773 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1774 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1775 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1776 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1777 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1778 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1783 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1784 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1785 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1786 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1787 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1788 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1789 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1790 vprp->aspect & 0xffff);
1791 GST_INFO_OBJECT (element, " width %d", vprp->width);
1792 GST_INFO_OBJECT (element, " height %d", vprp->height);
1793 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1794 for (k = 0; k < vprp->fields; k++) {
1795 gst_riff_vprp_video_field_desc *fd;
1797 fd = &(vprp->field_info[k]);
1798 GST_INFO_OBJECT (element, " field %u description:", k);
1799 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1800 fd->compressed_bm_height);
1801 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1802 fd->compressed_bm_width);
1803 GST_INFO_OBJECT (element, " valid_bm_height %d",
1804 fd->valid_bm_height);
1805 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1806 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1807 fd->valid_bm_x_offset);
1808 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1809 fd->valid_bm_y_offset);
1810 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1811 fd->video_x_t_offset);
1812 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1815 gst_buffer_unref (buf);
1824 GST_ERROR_OBJECT (element,
1825 "Too small vprp (%" G_GSIZE_FORMAT " available, at least %d needed)",
1826 size, (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1827 gst_buffer_unref (buf);
1833 gst_avi_demux_expose_streams (GstAviDemux * avi, gboolean force)
1837 GST_DEBUG_OBJECT (avi, "force : %d", force);
1839 for (i = 0; i < avi->num_streams; i++) {
1840 GstAviStream *stream = &avi->stream[i];
1842 if (force || stream->idx_n != 0) {
1843 GST_LOG_OBJECT (avi, "Adding pad %s" GST_PTR_FORMAT,
1844 GST_PAD_NAME (stream->pad));
1845 gst_element_add_pad ((GstElement *) avi, stream->pad);
1848 if (avi->element_index)
1849 gst_index_get_writer_id (avi->element_index,
1850 GST_OBJECT_CAST (stream->pad), &stream->index_id);
1853 stream->exposed = TRUE;
1854 if (avi->main_stream == -1)
1855 avi->main_stream = i;
1857 GST_WARNING_OBJECT (avi, "Stream #%d doesn't have any entry, removing it",
1859 gst_avi_demux_reset_stream (avi, stream);
1864 /* buf contains LIST chunk data, and will be padded to even size,
1865 * since some buggy files do not account for the padding of chunks
1866 * within a LIST in the size of the LIST */
1868 gst_avi_demux_roundup_list (GstAviDemux * avi, GstBuffer ** buf)
1872 size = gst_buffer_get_size (*buf);
1874 if (G_UNLIKELY (size & 1)) {
1878 GST_DEBUG_OBJECT (avi, "rounding up dubious list size %" G_GSIZE_FORMAT,
1880 obuf = gst_buffer_new_and_alloc (size + 1);
1882 gst_buffer_map (obuf, &map, GST_MAP_WRITE);
1883 gst_buffer_extract (*buf, 0, map.data, size);
1884 /* assume 0 padding, at least makes outcome deterministic */
1886 gst_buffer_unmap (obuf, &map);
1887 gst_buffer_replace (buf, obuf);
1892 * gst_avi_demux_parse_stream:
1893 * @avi: calling element (used for debugging/errors).
1894 * @buf: input buffer used to parse the stream.
1896 * Parses all subchunks in a strl chunk (which defines a single
1897 * stream). Discards the buffer after use. This function will
1898 * increment the stream counter internally.
1900 * Returns: whether the stream was identified successfully.
1901 * Errors are not fatal. It does indicate the stream
1905 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
1907 GstAviStream *stream;
1908 GstElementClass *klass;
1909 GstPadTemplate *templ;
1910 GstBuffer *sub = NULL;
1913 gchar *codec_name = NULL, *padname = NULL;
1914 const gchar *tag_name;
1915 GstCaps *caps = NULL;
1917 GstElement *element;
1918 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
1919 gst_riff_vprp *vprp = NULL;
1921 element = GST_ELEMENT_CAST (avi);
1923 GST_DEBUG_OBJECT (avi, "Parsing stream");
1925 gst_avi_demux_roundup_list (avi, &buf);
1927 if (avi->num_streams >= GST_AVI_DEMUX_MAX_STREAMS) {
1928 GST_WARNING_OBJECT (avi,
1929 "maximum no of streams (%d) exceeded, ignoring stream",
1930 GST_AVI_DEMUX_MAX_STREAMS);
1931 gst_buffer_unref (buf);
1932 /* not a fatal error, let's say */
1936 stream = &avi->stream[avi->num_streams];
1938 /* initial settings */
1939 stream->idx_duration = GST_CLOCK_TIME_NONE;
1940 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1941 stream->duration = GST_CLOCK_TIME_NONE;
1943 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
1944 /* sub can be NULL if the chunk is empty */
1946 GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
1947 GST_FOURCC_ARGS (tag));
1951 case GST_RIFF_TAG_strh:
1953 gst_riff_strh *strh;
1956 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
1959 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
1960 /* ownership given away */
1962 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
1966 strh = stream->strh;
1967 /* sanity check; stream header frame rate matches global header
1969 if (stream->strh->type == GST_RIFF_FCC_vids) {
1971 GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
1973 s_dur = gst_util_uint64_scale (GST_SECOND, strh->scale, strh->rate);
1974 GST_DEBUG_OBJECT (avi, "verifying stream framerate %d/%d, "
1975 "frame duration = %d ms", strh->rate, strh->scale,
1976 (gint) (s_dur / GST_MSECOND));
1977 if (h_dur > (10 * GST_MSECOND) && (s_dur > 10 * h_dur)) {
1978 strh->rate = GST_SECOND / GST_USECOND;
1979 strh->scale = h_dur / GST_USECOND;
1980 GST_DEBUG_OBJECT (avi, "correcting stream framerate to %d/%d",
1981 strh->rate, strh->scale);
1984 /* determine duration as indicated by header */
1985 stream->hdr_duration = gst_util_uint64_scale ((guint64) strh->length *
1986 strh->scale, GST_SECOND, (guint64) strh->rate);
1987 GST_INFO ("Stream duration according to header: %" GST_TIME_FORMAT,
1988 GST_TIME_ARGS (stream->hdr_duration));
1989 if (stream->hdr_duration == 0)
1990 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1995 case GST_RIFF_TAG_strf:
1997 gboolean res = FALSE;
2000 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
2004 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
2007 switch (stream->strh->type) {
2008 case GST_RIFF_FCC_vids:
2009 stream->is_vbr = TRUE;
2010 res = gst_riff_parse_strf_vids (element, sub,
2011 &stream->strf.vids, &stream->extradata);
2013 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
2015 case GST_RIFF_FCC_auds:
2017 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
2018 &stream->extradata);
2022 stream->is_vbr = (stream->strh->samplesize == 0)
2023 && stream->strh->scale > 1
2024 && stream->strf.auds->blockalign != 1;
2025 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
2026 stream->is_vbr, res);
2027 /* we need these or we have no way to come up with timestamps */
2028 if ((!stream->is_vbr && !stream->strf.auds->av_bps) ||
2029 (stream->is_vbr && (!stream->strh->scale ||
2030 !stream->strh->rate))) {
2031 GST_WARNING_OBJECT (element,
2032 "invalid audio header, ignoring stream");
2035 /* some more sanity checks */
2036 if (stream->is_vbr) {
2037 if (stream->strf.auds->blockalign <= 4) {
2038 /* that would mean (too) many frames per chunk,
2039 * so not likely set as expected */
2040 GST_DEBUG_OBJECT (element,
2041 "suspicious blockalign %d for VBR audio; "
2042 "overriding to 1 frame per chunk",
2043 stream->strf.auds->blockalign);
2044 /* this should top any likely value */
2045 stream->strf.auds->blockalign = (1 << 12);
2049 case GST_RIFF_FCC_iavs:
2050 stream->is_vbr = TRUE;
2051 res = gst_riff_parse_strf_iavs (element, sub,
2052 &stream->strf.iavs, &stream->extradata);
2054 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
2056 case GST_RIFF_FCC_txts:
2057 /* nothing to parse here */
2058 stream->is_vbr = (stream->strh->samplesize == 0)
2059 && (stream->strh->scale > 1);
2063 GST_ERROR_OBJECT (avi,
2064 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
2065 GST_FOURCC_ARGS (stream->strh->type));
2069 gst_buffer_unref (sub);
2077 case GST_RIFF_TAG_vprp:
2080 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
2084 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
2088 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
2092 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
2093 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
2094 /* not considered fatal */
2102 case GST_RIFF_TAG_strd:
2103 if (stream->initdata)
2104 gst_buffer_unref (stream->initdata);
2105 stream->initdata = sub;
2108 case GST_RIFF_TAG_strn:
2109 g_free (stream->name);
2113 gst_buffer_map (sub, &map, GST_MAP_READ);
2114 stream->name = g_strndup ((gchar *) map.data, map.size);
2115 gst_buffer_unmap (sub, &map);
2116 gst_buffer_unref (sub);
2119 stream->name = g_strdup ("");
2121 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
2124 gst_avi_demux_parse_idit (avi, sub);
2127 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
2128 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
2129 '0' + avi->num_streams % 10)) {
2130 g_free (stream->indexes);
2131 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
2132 stream->superindex = TRUE;
2136 GST_WARNING_OBJECT (avi,
2137 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
2138 GST_FOURCC_ARGS (tag));
2140 case GST_RIFF_TAG_JUNQ:
2141 case GST_RIFF_TAG_JUNK:
2145 gst_buffer_unref (sub);
2151 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
2156 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
2160 /* get class to figure out the template */
2161 klass = GST_ELEMENT_GET_CLASS (avi);
2163 /* we now have all info, let´s set up a pad and a caps and be done */
2164 /* create stream name + pad */
2165 switch (stream->strh->type) {
2166 case GST_RIFF_FCC_vids:{
2169 fourcc = (stream->strf.vids->compression) ?
2170 stream->strf.vids->compression : stream->strh->fcc_handler;
2171 padname = g_strdup_printf ("video_%u", avi->num_v_streams);
2172 templ = gst_element_class_get_pad_template (klass, "video_%u");
2173 caps = gst_riff_create_video_caps (fourcc, stream->strh,
2174 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
2176 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2177 G_TYPE_INT, fourcc, NULL);
2178 } else if (got_vprp && vprp) {
2179 guint32 aspect_n, aspect_d;
2182 aspect_n = vprp->aspect >> 16;
2183 aspect_d = vprp->aspect & 0xffff;
2184 /* calculate the pixel aspect ratio using w/h and aspect ratio */
2185 n = aspect_n * stream->strf.vids->height;
2186 d = aspect_d * stream->strf.vids->width;
2188 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
2190 /* very local, not needed elsewhere */
2194 tag_name = GST_TAG_VIDEO_CODEC;
2195 avi->num_v_streams++;
2198 case GST_RIFF_FCC_auds:{
2199 /* FIXME: Do something with the channel reorder map */
2200 padname = g_strdup_printf ("audio_%u", avi->num_a_streams);
2201 templ = gst_element_class_get_pad_template (klass, "audio_%u");
2202 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
2203 stream->strh, stream->strf.auds, stream->extradata,
2204 stream->initdata, &codec_name, NULL);
2206 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
2207 G_TYPE_INT, stream->strf.auds->format, NULL);
2209 tag_name = GST_TAG_AUDIO_CODEC;
2210 avi->num_a_streams++;
2213 case GST_RIFF_FCC_iavs:{
2214 guint32 fourcc = stream->strh->fcc_handler;
2216 padname = g_strdup_printf ("video_%u", avi->num_v_streams);
2217 templ = gst_element_class_get_pad_template (klass, "video_%u");
2218 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
2219 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
2221 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2222 G_TYPE_INT, fourcc, NULL);
2224 tag_name = GST_TAG_VIDEO_CODEC;
2225 avi->num_v_streams++;
2228 case GST_RIFF_FCC_txts:{
2229 padname = g_strdup_printf ("subtitle_%u", avi->num_t_streams);
2230 templ = gst_element_class_get_pad_template (klass, "subtitle_%u");
2231 caps = gst_caps_new_empty_simple ("application/x-subtitle-avi");
2233 avi->num_t_streams++;
2237 g_return_val_if_reached (FALSE);
2240 /* no caps means no stream */
2242 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
2246 GST_DEBUG_OBJECT (element, "codec-name=%s",
2247 (codec_name ? codec_name : "NULL"));
2248 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
2250 /* set proper settings and add it */
2252 gst_object_unref (stream->pad);
2253 pad = stream->pad = gst_pad_new_from_template (templ, padname);
2256 gst_pad_use_fixed_caps (pad);
2258 gst_pad_set_formats_function (pad,
2259 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
2260 gst_pad_set_event_mask_function (pad,
2261 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
2263 gst_pad_set_event_function (pad,
2264 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
2265 gst_pad_set_query_function (pad,
2266 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
2268 gst_pad_set_convert_function (pad,
2269 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
2272 stream->num = avi->num_streams;
2274 stream->start_entry = 0;
2275 stream->step_entry = 0;
2276 stream->stop_entry = 0;
2278 stream->current_entry = -1;
2279 stream->current_total = 0;
2281 stream->last_flow = GST_FLOW_OK;
2282 stream->discont = TRUE;
2284 stream->total_bytes = 0;
2285 stream->total_blocks = 0;
2286 stream->n_keyframes = 0;
2289 stream->idx_max = 0;
2291 gst_pad_set_element_private (pad, stream);
2294 gst_pad_set_active (pad, TRUE);
2295 gst_pad_push_event (pad, gst_event_new_caps (caps));
2296 gst_caps_unref (caps);
2300 if (!stream->taglist)
2301 stream->taglist = gst_tag_list_new_empty ();
2303 avi->got_tags = TRUE;
2305 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
2307 g_free (codec_name);
2310 gst_buffer_unref (buf);
2317 /* unref any mem that may be in use */
2319 gst_buffer_unref (buf);
2321 gst_buffer_unref (sub);
2323 g_free (codec_name);
2324 gst_avi_demux_reset_stream (avi, stream);
2331 * gst_avi_demux_parse_odml:
2332 * @avi: calling element (used for debug/error).
2333 * @buf: input buffer to be used for parsing.
2335 * Read an openDML-2.0 extension header. Fills in the frame number
2336 * in the avi demuxer object when reading succeeds.
2339 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
2343 GstBuffer *sub = NULL;
2345 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
2348 case GST_RIFF_TAG_dmlh:{
2349 gst_riff_dmlh dmlh, *_dmlh;
2352 /* sub == NULL is possible and means an empty buffer */
2356 gst_buffer_map (sub, &map, GST_MAP_READ);
2359 if (map.size < sizeof (gst_riff_dmlh)) {
2360 GST_ERROR_OBJECT (avi,
2361 "DMLH entry is too small (%" G_GSIZE_FORMAT " bytes, %d needed)",
2362 map.size, (int) sizeof (gst_riff_dmlh));
2363 gst_buffer_unmap (sub, &map);
2366 _dmlh = (gst_riff_dmlh *) map.data;
2367 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
2368 gst_buffer_unmap (sub, &map);
2370 GST_INFO_OBJECT (avi, "dmlh tag found: totalframes: %u",
2373 avi->avih->tot_frames = dmlh.totalframes;
2378 GST_WARNING_OBJECT (avi,
2379 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
2380 GST_FOURCC_ARGS (tag));
2382 case GST_RIFF_TAG_JUNQ:
2383 case GST_RIFF_TAG_JUNK:
2385 /* skip and move to next chunk */
2387 gst_buffer_unref (sub);
2394 gst_buffer_unref (buf);
2399 gst_avi_demux_index_last (GstAviDemux * avi, GstAviStream * stream)
2401 return stream->idx_n;
2404 /* find a previous entry in the index with the given flags */
2406 gst_avi_demux_index_prev (GstAviDemux * avi, GstAviStream * stream,
2407 guint last, gboolean keyframe)
2409 GstAviIndexEntry *entry;
2412 for (i = last; i > 0; i--) {
2413 entry = &stream->index[i - 1];
2414 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2422 gst_avi_demux_index_next (GstAviDemux * avi, GstAviStream * stream,
2423 guint last, gboolean keyframe)
2425 GstAviIndexEntry *entry;
2428 for (i = last + 1; i < stream->idx_n; i++) {
2429 entry = &stream->index[i];
2430 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2434 return stream->idx_n - 1;
2438 gst_avi_demux_index_entry_search (GstAviIndexEntry * entry, guint64 * total)
2440 if (entry->total < *total)
2442 else if (entry->total > *total)
2448 * gst_avi_demux_index_for_time:
2450 * @stream: the stream
2451 * @time: a time position
2453 * Finds the index entry which time is less or equal than the requested time.
2454 * Try to avoid binary search when we can convert the time to an index
2455 * position directly (for example for video frames with a fixed duration).
2457 * Returns: the found position in the index.
2460 gst_avi_demux_index_for_time (GstAviDemux * avi,
2461 GstAviStream * stream, guint64 time)
2466 GST_LOG_OBJECT (avi, "search time:%" GST_TIME_FORMAT, GST_TIME_ARGS (time));
2468 /* easy (and common) cases */
2469 if (time == 0 || stream->idx_n == 0)
2471 if (time >= stream->idx_duration)
2472 return stream->idx_n - 1;
2474 /* figure out where we need to go. For that we convert the time to an
2475 * index entry or we convert it to a total and then do a binary search. */
2476 if (stream->is_vbr) {
2477 /* VBR stream next timestamp */
2478 if (stream->strh->type == GST_RIFF_FCC_auds) {
2479 total = avi_stream_convert_time_to_frames_unchecked (stream, time);
2481 index = avi_stream_convert_time_to_frames_unchecked (stream, time);
2484 /* constant rate stream */
2485 total = avi_stream_convert_time_to_bytes_unchecked (stream, time);
2489 GstAviIndexEntry *entry;
2491 /* no index, find index with binary search on total */
2492 GST_LOG_OBJECT (avi, "binary search for entry with total %"
2493 G_GUINT64_FORMAT, total);
2495 entry = gst_util_array_binary_search (stream->index,
2496 stream->idx_n, sizeof (GstAviIndexEntry),
2497 (GCompareDataFunc) gst_avi_demux_index_entry_search,
2498 GST_SEARCH_MODE_BEFORE, &total, NULL);
2500 if (entry == NULL) {
2501 GST_LOG_OBJECT (avi, "not found, assume index 0");
2504 index = entry - stream->index;
2505 GST_LOG_OBJECT (avi, "found at %u", index);
2508 GST_LOG_OBJECT (avi, "converted time to index %u", index);
2514 static inline GstAviStream *
2515 gst_avi_demux_stream_for_id (GstAviDemux * avi, guint32 id)
2518 GstAviStream *stream;
2520 /* get the stream for this entry */
2521 stream_nr = CHUNKID_TO_STREAMNR (id);
2522 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2523 GST_WARNING_OBJECT (avi, "invalid stream nr %d", stream_nr);
2526 stream = &avi->stream[stream_nr];
2527 if (G_UNLIKELY (!stream->strh)) {
2528 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2535 * gst_avi_demux_parse_index:
2536 * @avi: calling element (used for debugging/errors).
2537 * @buf: buffer containing the full index.
2539 * Read index entries from the provided buffer.
2540 * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
2543 gst_avi_demux_parse_index (GstAviDemux * avi, GstBuffer * buf)
2547 gst_riff_index_entry *index;
2549 GstAviStream *stream;
2550 GstAviIndexEntry entry;
2556 gst_buffer_map (buf, &map, GST_MAP_READ);
2558 stamp = gst_util_get_timestamp ();
2560 /* see how many items in the index */
2561 num = map.size / sizeof (gst_riff_index_entry);
2565 GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2567 index = (gst_riff_index_entry *) map.data;
2569 /* figure out if the index is 0 based or relative to the MOVI start */
2570 entry.offset = GST_READ_UINT32_LE (&index[0].offset);
2571 if (entry.offset < avi->offset) {
2572 avi->index_offset = avi->offset + 8;
2573 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2575 avi->index_offset = 0;
2576 GST_DEBUG ("index is 0 based");
2579 for (i = 0, n = 0; i < num; i++) {
2580 id = GST_READ_UINT32_LE (&index[i].id);
2581 entry.offset = GST_READ_UINT32_LE (&index[i].offset);
2583 /* some sanity checks */
2584 if (G_UNLIKELY (id == GST_RIFF_rec || id == 0 ||
2585 (entry.offset == 0 && n > 0)))
2588 /* get the stream for this entry */
2589 stream = gst_avi_demux_stream_for_id (avi, id);
2590 if (G_UNLIKELY (!stream))
2593 /* handle offset and size */
2594 entry.offset += avi->index_offset + 8;
2595 entry.size = GST_READ_UINT32_LE (&index[i].size);
2598 if (stream->strh->type == GST_RIFF_FCC_auds) {
2599 /* all audio frames are keyframes */
2600 ENTRY_SET_KEYFRAME (&entry);
2603 /* else read flags */
2604 flags = GST_READ_UINT32_LE (&index[i].flags);
2605 if (flags & GST_RIFF_IF_KEYFRAME) {
2606 ENTRY_SET_KEYFRAME (&entry);
2608 ENTRY_UNSET_KEYFRAME (&entry);
2613 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2618 gst_buffer_unmap (buf, &map);
2619 gst_buffer_unref (buf);
2621 /* get stream stats now */
2622 avi->have_index = gst_avi_demux_do_index_stats (avi);
2624 stamp = gst_util_get_timestamp () - stamp;
2625 GST_DEBUG_OBJECT (avi, "index parsing took %" GST_TIME_FORMAT,
2626 GST_TIME_ARGS (stamp));
2633 GST_DEBUG_OBJECT (avi, "empty index");
2634 gst_buffer_unmap (buf, &map);
2635 gst_buffer_unref (buf);
2640 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2641 ("Cannot allocate memory for %u*%u=%u bytes",
2642 (guint) sizeof (GstAviIndexEntry), num,
2643 (guint) sizeof (GstAviIndexEntry) * num));
2644 gst_buffer_unmap (buf, &map);
2645 gst_buffer_unref (buf);
2651 * gst_avi_demux_stream_index:
2652 * @avi: avi demuxer object.
2654 * Seeks to index and reads it.
2657 gst_avi_demux_stream_index (GstAviDemux * avi)
2660 guint64 offset = avi->offset;
2666 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2668 /* get chunk information */
2669 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2670 if (res != GST_FLOW_OK)
2673 gst_buffer_map (buf, &map, GST_MAP_READ);
2677 /* check tag first before blindy trying to read 'size' bytes */
2678 tag = GST_READ_UINT32_LE (map.data);
2679 size = GST_READ_UINT32_LE (map.data + 4);
2680 if (tag == GST_RIFF_TAG_LIST) {
2681 /* this is the movi tag */
2682 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2683 (8 + GST_ROUND_UP_2 (size)));
2684 offset += 8 + GST_ROUND_UP_2 (size);
2685 gst_buffer_unmap (buf, &map);
2686 gst_buffer_unref (buf);
2688 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2689 if (res != GST_FLOW_OK)
2692 gst_buffer_map (buf, &map, GST_MAP_READ);
2696 tag = GST_READ_UINT32_LE (map.data);
2697 size = GST_READ_UINT32_LE (map.data + 4);
2699 gst_buffer_unmap (buf, &map);
2700 gst_buffer_unref (buf);
2702 if (tag != GST_RIFF_TAG_idx1)
2707 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2709 /* read chunk, advance offset */
2710 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2711 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2714 GST_DEBUG ("will parse index chunk size %" G_GSIZE_FORMAT " for tag %"
2715 GST_FOURCC_FORMAT, gst_buffer_get_size (buf), GST_FOURCC_ARGS (tag));
2717 gst_avi_demux_parse_index (avi, buf);
2719 #ifndef GST_DISABLE_GST_DEBUG
2720 /* debug our indexes */
2723 GstAviStream *stream;
2725 for (i = 0; i < avi->num_streams; i++) {
2726 stream = &avi->stream[i];
2727 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2728 i, stream->idx_n, stream->total_bytes);
2737 GST_DEBUG_OBJECT (avi,
2738 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2743 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2744 gst_buffer_unmap (buf, &map);
2745 gst_buffer_unref (buf);
2750 GST_WARNING_OBJECT (avi,
2751 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2752 GST_FOURCC_ARGS (tag));
2757 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2763 * gst_avi_demux_stream_index_push:
2764 * @avi: avi demuxer object.
2769 gst_avi_demux_stream_index_push (GstAviDemux * avi)
2771 guint64 offset = avi->idx1_offset;
2776 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2778 /* get chunk information */
2779 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
2782 /* check tag first before blindly trying to read 'size' bytes */
2783 if (tag == GST_RIFF_TAG_LIST) {
2784 /* this is the movi tag */
2785 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2786 (8 + GST_ROUND_UP_2 (size)));
2787 avi->idx1_offset = offset + 8 + GST_ROUND_UP_2 (size);
2788 /* issue seek to allow chain function to handle it and return! */
2789 perform_seek_to_offset (avi, avi->idx1_offset);
2793 if (tag != GST_RIFF_TAG_idx1)
2796 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2798 /* flush chunk header */
2799 gst_adapter_flush (avi->adapter, 8);
2800 /* read chunk payload */
2801 buf = gst_adapter_take_buffer (avi->adapter, size);
2804 /* advance offset */
2805 offset += 8 + GST_ROUND_UP_2 (size);
2807 GST_DEBUG ("will parse index chunk size %" G_GSIZE_FORMAT " for tag %"
2808 GST_FOURCC_FORMAT, gst_buffer_get_size (buf), GST_FOURCC_ARGS (tag));
2810 avi->offset = avi->first_movi_offset;
2811 gst_avi_demux_parse_index (avi, buf);
2813 #ifndef GST_DISABLE_GST_DEBUG
2814 /* debug our indexes */
2817 GstAviStream *stream;
2819 for (i = 0; i < avi->num_streams; i++) {
2820 stream = &avi->stream[i];
2821 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2822 i, stream->idx_n, stream->total_bytes);
2831 GST_DEBUG_OBJECT (avi,
2832 "taking data from adapter failed: pos=%" G_GUINT64_FORMAT " size=%u",
2838 GST_WARNING_OBJECT (avi,
2839 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2840 GST_FOURCC_ARGS (tag));
2846 * gst_avi_demux_peek_tag:
2848 * Returns the tag and size of the next chunk
2850 static GstFlowReturn
2851 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2854 GstFlowReturn res = GST_FLOW_OK;
2855 GstBuffer *buf = NULL;
2858 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2859 if (res != GST_FLOW_OK)
2862 gst_buffer_map (buf, &map, GST_MAP_READ);
2866 *tag = GST_READ_UINT32_LE (map.data);
2867 *size = GST_READ_UINT32_LE (map.data + 4);
2869 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2870 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2871 *size, offset + 8, offset + 8 + (gint64) * size);
2874 gst_buffer_unmap (buf, &map);
2875 gst_buffer_unref (buf);
2882 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2887 GST_DEBUG_OBJECT (avi, "got %" G_GSIZE_FORMAT " bytes which is <> 8 bytes",
2889 res = GST_FLOW_ERROR;
2895 * gst_avi_demux_next_data_buffer:
2897 * Returns the offset and size of the next buffer
2898 * Position is the position of the buffer (after tag and size)
2900 static GstFlowReturn
2901 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2902 guint32 * tag, guint * size)
2904 guint64 off = *offset;
2909 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2910 if (res != GST_FLOW_OK)
2912 if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
2913 off += 8 + 4; /* skip tag + size + subtag */
2925 * gst_avi_demux_stream_scan:
2926 * @avi: calling element (used for debugging/errors).
2928 * Scan the file for all chunks to "create" a new index.
2932 gst_avi_demux_stream_scan (GstAviDemux * avi)
2935 GstAviStream *stream;
2943 * - implement non-seekable source support.
2945 GST_DEBUG_OBJECT (avi, "Creating index");
2947 /* get the size of the file */
2948 if (!gst_pad_peer_query_duration (avi->sinkpad, GST_FORMAT_BYTES, &tmplength))
2952 /* guess the total amount of entries we expect */
2956 GstAviIndexEntry entry;
2959 /* start reading data buffers to find the id and offset */
2960 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
2961 if (G_UNLIKELY (res != GST_FLOW_OK))
2965 stream = gst_avi_demux_stream_for_id (avi, tag);
2966 if (G_UNLIKELY (!stream))
2969 /* we can't figure out the keyframes, assume they all are */
2970 entry.flags = GST_AVI_KEYFRAME;
2974 /* and add to the index of this stream */
2975 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2979 /* update position */
2980 pos += GST_ROUND_UP_2 (size);
2981 if (G_UNLIKELY (pos > length)) {
2982 GST_WARNING_OBJECT (avi,
2983 "Stopping index lookup since we are further than EOF");
2989 avi->have_index = gst_avi_demux_do_index_stats (avi);
2996 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2997 ("Cannot allocate memory for %u*%u=%u bytes",
2998 (guint) sizeof (GstAviIndexEntry), num,
2999 (guint) sizeof (GstAviIndexEntry) * num));
3005 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
3009 GstAviStream *stream;
3011 total = GST_CLOCK_TIME_NONE;
3013 /* all streams start at a timestamp 0 */
3014 for (i = 0; i < avi->num_streams; i++) {
3015 GstClockTime duration, hduration;
3016 gst_riff_strh *strh;
3018 stream = &avi->stream[i];
3019 if (G_UNLIKELY (!stream || !stream->idx_n || !(strh = stream->strh)))
3022 /* get header duration for the stream */
3023 hduration = stream->hdr_duration;
3024 /* index duration calculated during parsing */
3025 duration = stream->idx_duration;
3027 /* now pick a good duration */
3028 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3029 /* index gave valid duration, use that */
3030 GST_INFO ("Stream %p duration according to index: %" GST_TIME_FORMAT,
3031 stream, GST_TIME_ARGS (duration));
3033 /* fall back to header info to calculate a duration */
3034 duration = hduration;
3036 GST_INFO ("Setting duration of stream #%d to %" GST_TIME_FORMAT,
3037 i, GST_TIME_ARGS (duration));
3038 /* set duration for the stream */
3039 stream->duration = duration;
3041 /* find total duration */
3042 if (total == GST_CLOCK_TIME_NONE ||
3043 (GST_CLOCK_TIME_IS_VALID (duration) && duration > total))
3047 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
3048 /* now update the duration for those streams where we had none */
3049 for (i = 0; i < avi->num_streams; i++) {
3050 stream = &avi->stream[i];
3052 if (!GST_CLOCK_TIME_IS_VALID (stream->duration)
3053 || stream->duration == 0) {
3054 stream->duration = total;
3056 GST_INFO ("Stream %p duration according to total: %" GST_TIME_FORMAT,
3057 stream, GST_TIME_ARGS (total));
3062 /* and set the total duration in the segment. */
3063 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
3064 GST_TIME_ARGS (total));
3066 avi->segment.duration = total;
3069 /* returns FALSE if there are no pads to deliver event to,
3070 * otherwise TRUE (whatever the outcome of event sending),
3071 * takes ownership of the event. */
3073 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
3075 gboolean result = FALSE;
3078 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
3079 GST_EVENT_TYPE_NAME (event), avi->num_streams);
3081 for (i = 0; i < avi->num_streams; i++) {
3082 GstAviStream *stream = &avi->stream[i];
3086 gst_pad_push_event (stream->pad, gst_event_ref (event));
3089 gst_event_unref (event);
3094 gst_avi_demux_check_seekability (GstAviDemux * avi)
3097 gboolean seekable = FALSE;
3098 gint64 start = -1, stop = -1;
3100 query = gst_query_new_seeking (GST_FORMAT_BYTES);
3101 if (!gst_pad_peer_query (avi->sinkpad, query)) {
3102 GST_DEBUG_OBJECT (avi, "seeking query failed");
3106 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
3108 /* try harder to query upstream size if we didn't get it the first time */
3109 if (seekable && stop == -1) {
3110 GST_DEBUG_OBJECT (avi, "doing duration query to fix up unset stop");
3111 gst_pad_peer_query_duration (avi->sinkpad, GST_FORMAT_BYTES, &stop);
3114 /* if upstream doesn't know the size, it's likely that it's not seekable in
3115 * practice even if it technically may be seekable */
3116 if (seekable && (start != 0 || stop <= start)) {
3117 GST_DEBUG_OBJECT (avi, "seekable but unknown start/stop -> disable");
3122 GST_INFO_OBJECT (avi, "seekable: %d (%" G_GUINT64_FORMAT " - %"
3123 G_GUINT64_FORMAT ")", seekable, start, stop);
3124 avi->seekable = seekable;
3126 gst_query_unref (query);
3130 * Read AVI headers when streaming
3132 static GstFlowReturn
3133 gst_avi_demux_stream_header_push (GstAviDemux * avi)
3135 GstFlowReturn ret = GST_FLOW_OK;
3140 GstBuffer *buf = NULL, *sub = NULL;
3143 GstTagList *tags = NULL;
3146 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
3148 switch (avi->header_state) {
3149 case GST_AVI_DEMUX_HEADER_TAG_LIST:
3150 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3151 avi->offset += 8 + GST_ROUND_UP_2 (size);
3152 if (tag != GST_RIFF_TAG_LIST)
3153 goto header_no_list;
3155 gst_adapter_flush (avi->adapter, 8);
3156 /* Find the 'hdrl' LIST tag */
3157 GST_DEBUG ("Reading %d bytes", size);
3158 buf = gst_adapter_take_buffer (avi->adapter, size);
3160 gst_buffer_extract (buf, 0, fourcc, 4);
3162 if (GST_READ_UINT32_LE (fourcc) != GST_RIFF_LIST_hdrl)
3163 goto header_no_hdrl;
3167 gst_adapter_flush (avi->adapter, 1);
3169 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
3171 gst_avi_demux_roundup_list (avi, &buf);
3173 /* the hdrl starts with a 'avih' header */
3174 if (!gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3176 goto header_no_avih;
3178 if (tag != GST_RIFF_TAG_avih)
3179 goto header_no_avih;
3181 if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3182 goto header_wrong_avih;
3184 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
3186 /* now, read the elements from the header until the end */
3187 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3189 /* sub can be NULL on empty tags */
3194 case GST_RIFF_TAG_LIST:
3195 if (gst_buffer_get_size (sub) < 4)
3198 gst_buffer_extract (sub, 0, fourcc, 4);
3200 switch (GST_READ_UINT32_LE (fourcc)) {
3201 case GST_RIFF_LIST_strl:
3202 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3204 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3205 ("failed to parse stream, ignoring"));
3210 case GST_RIFF_LIST_odml:
3211 gst_avi_demux_parse_odml (avi, sub);
3215 GST_WARNING_OBJECT (avi,
3216 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3217 GST_FOURCC_ARGS (GST_READ_UINT32_LE (fourcc)));
3219 case GST_RIFF_TAG_JUNQ:
3220 case GST_RIFF_TAG_JUNK:
3225 gst_avi_demux_parse_idit (avi, sub);
3228 GST_WARNING_OBJECT (avi,
3229 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
3230 offset, GST_FOURCC_ARGS (tag));
3232 case GST_RIFF_TAG_JUNQ:
3233 case GST_RIFF_TAG_JUNK:
3235 /* move to next chunk */
3237 gst_buffer_unref (sub);
3242 gst_buffer_unref (buf);
3243 GST_DEBUG ("elements parsed");
3245 /* check parsed streams */
3246 if (avi->num_streams == 0) {
3248 } else if (avi->num_streams != avi->avih->streams) {
3249 GST_WARNING_OBJECT (avi,
3250 "Stream header mentioned %d streams, but %d available",
3251 avi->avih->streams, avi->num_streams);
3253 GST_DEBUG ("Get junk and info next");
3254 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
3256 /* Need more data */
3260 case GST_AVI_DEMUX_HEADER_INFO:
3261 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
3263 if (gst_adapter_available (avi->adapter) < 12)
3266 data = gst_adapter_map (avi->adapter, 12);
3267 tag = GST_READ_UINT32_LE (data);
3268 size = GST_READ_UINT32_LE (data + 4);
3269 ltag = GST_READ_UINT32_LE (data + 8);
3270 gst_adapter_unmap (avi->adapter);
3272 if (tag == GST_RIFF_TAG_LIST) {
3274 case GST_RIFF_LIST_movi:
3275 gst_adapter_flush (avi->adapter, 12);
3276 if (!avi->first_movi_offset)
3277 avi->first_movi_offset = avi->offset;
3279 avi->idx1_offset = avi->offset + size - 4;
3281 case GST_RIFF_LIST_INFO:
3282 GST_DEBUG ("Found INFO chunk");
3283 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3284 GST_DEBUG ("got size %d", size);
3286 gst_adapter_flush (avi->adapter, 12);
3288 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
3291 gst_adapter_flush (avi->adapter, 1);
3292 gst_riff_parse_info (GST_ELEMENT_CAST (avi), buf, &tags);
3294 if (avi->globaltags) {
3295 gst_tag_list_insert (avi->globaltags, tags,
3296 GST_TAG_MERGE_REPLACE);
3298 avi->globaltags = tags;
3302 gst_buffer_unref (buf);
3304 avi->offset += GST_ROUND_UP_2 (size) - 4;
3306 GST_DEBUG ("skipping INFO LIST prefix");
3309 /* Need more data */
3314 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3315 avi->offset += 8 + GST_ROUND_UP_2 (size);
3316 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3317 // ??? goto iterate; ???
3319 /* Need more data */
3325 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3326 avi->offset += 8 + GST_ROUND_UP_2 (size);
3327 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3330 /* Need more data */
3337 GST_WARNING ("unhandled header state: %d", avi->header_state);
3342 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3343 avi->num_streams, avi->stream[0].indexes);
3345 GST_DEBUG ("Found movi chunk. Starting to stream data");
3346 avi->state = GST_AVI_DEMUX_MOVI;
3348 /* no indexes in push mode, but it still sets some variables */
3349 gst_avi_demux_calculate_durations_from_index (avi);
3351 gst_avi_demux_expose_streams (avi, TRUE);
3353 /* prepare all streams for index 0 */
3354 for (i = 0; i < avi->num_streams; i++)
3355 avi->stream[i].current_entry = 0;
3357 /* create initial NEWSEGMENT event */
3359 gst_event_unref (avi->seg_event);
3360 avi->seg_event = gst_event_new_segment (&avi->segment);
3362 gst_avi_demux_check_seekability (avi);
3364 /* at this point we know all the streams and we can signal the no more
3366 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3367 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3374 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3375 return GST_FLOW_ERROR;
3379 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3380 ("Invalid AVI header (no LIST at start): %"
3381 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3382 return GST_FLOW_ERROR;
3386 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3387 ("Invalid AVI header (no hdrl at start): %"
3388 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3389 gst_buffer_unref (buf);
3390 return GST_FLOW_ERROR;
3394 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3395 ("Invalid AVI header (no avih at start): %"
3396 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3398 gst_buffer_unref (sub);
3400 gst_buffer_unref (buf);
3401 return GST_FLOW_ERROR;
3405 gst_buffer_unref (buf);
3406 return GST_FLOW_ERROR;
3411 gst_avi_demux_add_date_tag (GstAviDemux * avi, gint y, gint m, gint d,
3412 gint h, gint min, gint s)
3417 date = g_date_new_dmy (d, m, y);
3418 if (!g_date_valid (date)) {
3420 GST_WARNING_OBJECT (avi, "Refusing to add invalid date %d-%d-%d", y, m, d);
3425 dt = gst_date_time_new_local_time (y, m, d, h, min, s);
3427 if (avi->globaltags == NULL)
3428 avi->globaltags = gst_tag_list_new_empty ();
3430 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE, date,
3434 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE_TIME,
3436 gst_date_time_unref (dt);
3441 gst_avi_demux_parse_idit_nums_only (GstAviDemux * avi, gchar * data)
3444 gint hr = 0, min = 0, sec = 0;
3447 GST_DEBUG ("data : '%s'", data);
3449 ret = sscanf (data, "%d:%d:%d %d:%d:%d", &y, &m, &d, &hr, &min, &sec);
3451 /* Attempt YYYY/MM/DD/ HH:MM variant (found in CASIO cameras) */
3452 ret = sscanf (data, "%04d/%02d/%02d/ %d:%d", &y, &m, &d, &hr, &min);
3454 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3458 gst_avi_demux_add_date_tag (avi, y, m, d, hr, min, sec);
3462 get_month_num (gchar * data, guint size)
3464 if (g_ascii_strncasecmp (data, "jan", 3) == 0) {
3466 } else if (g_ascii_strncasecmp (data, "feb", 3) == 0) {
3468 } else if (g_ascii_strncasecmp (data, "mar", 3) == 0) {
3470 } else if (g_ascii_strncasecmp (data, "apr", 3) == 0) {
3472 } else if (g_ascii_strncasecmp (data, "may", 3) == 0) {
3474 } else if (g_ascii_strncasecmp (data, "jun", 3) == 0) {
3476 } else if (g_ascii_strncasecmp (data, "jul", 3) == 0) {
3478 } else if (g_ascii_strncasecmp (data, "aug", 3) == 0) {
3480 } else if (g_ascii_strncasecmp (data, "sep", 3) == 0) {
3482 } else if (g_ascii_strncasecmp (data, "oct", 3) == 0) {
3484 } else if (g_ascii_strncasecmp (data, "nov", 3) == 0) {
3486 } else if (g_ascii_strncasecmp (data, "dec", 3) == 0) {
3494 gst_avi_demux_parse_idit_text (GstAviDemux * avi, gchar * data)
3496 gint year, month, day;
3497 gint hour, min, sec;
3502 ret = sscanf (data, "%3s %3s %d %d:%d:%d %d", weekday, monthstr, &day, &hour,
3505 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3508 month = get_month_num (monthstr, strlen (monthstr));
3509 gst_avi_demux_add_date_tag (avi, year, month, day, hour, min, sec);
3513 gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf)
3518 gchar *safedata = NULL;
3520 gst_buffer_map (buf, &map, GST_MAP_READ);
3523 * http://www.eden-foundation.org/products/code/film_date_stamp/index.html
3525 * This tag could be in one of the below formats
3526 * 2005:08:17 11:42:43
3527 * THU OCT 26 16:46:04 2006
3528 * Mon Mar 3 09:44:56 2008
3530 * FIXME: Our date tag doesn't include hours
3533 /* skip eventual initial whitespace */
3534 ptr = (gchar *) map.data;
3537 while (left > 0 && g_ascii_isspace (ptr[0])) {
3546 /* make a safe copy to add a \0 to the end of the string */
3547 safedata = g_strndup (ptr, left);
3549 /* test if the first char is a alpha or a number */
3550 if (g_ascii_isdigit (ptr[0])) {
3551 gst_avi_demux_parse_idit_nums_only (avi, safedata);
3554 } else if (g_ascii_isalpha (ptr[0])) {
3555 gst_avi_demux_parse_idit_text (avi, safedata);
3563 GST_WARNING_OBJECT (avi, "IDIT tag has no parsable info");
3564 gst_buffer_unmap (buf, &map);
3568 * Read full AVI headers.
3570 static GstFlowReturn
3571 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
3574 GstBuffer *buf, *sub = NULL;
3577 GstElement *element = GST_ELEMENT_CAST (avi);
3579 GstTagList *tags = NULL;
3582 stamp = gst_util_get_timestamp ();
3584 /* the header consists of a 'hdrl' LIST tag */
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)
3593 GST_DEBUG_OBJECT (avi, "parsing headers");
3595 /* Find the 'hdrl' LIST tag */
3596 gst_buffer_extract (buf, 0, fourcc, 4);
3597 while (GST_READ_UINT32_LE (fourcc) != GST_RIFF_LIST_hdrl) {
3598 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
3599 GST_FOURCC_ARGS (GST_READ_UINT32_LE (fourcc)));
3602 gst_buffer_unref (buf);
3604 /* read new chunk */
3605 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3606 if (res != GST_FLOW_OK)
3607 goto pull_range_failed;
3608 else if (tag != GST_RIFF_TAG_LIST)
3610 else if (gst_buffer_get_size (buf) < 4)
3612 gst_buffer_extract (buf, 0, fourcc, 4);
3615 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
3617 gst_avi_demux_roundup_list (avi, &buf);
3619 /* the hdrl starts with a 'avih' header */
3620 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
3622 else if (tag != GST_RIFF_TAG_avih)
3624 else if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3627 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3629 /* now, read the elements from the header until the end */
3630 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
3633 /* sub can be NULL on empty tags */
3637 gst_buffer_map (sub, &map, GST_MAP_READ);
3640 case GST_RIFF_TAG_LIST:
3644 switch (GST_READ_UINT32_LE (map.data)) {
3645 case GST_RIFF_LIST_strl:
3646 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3647 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3648 ("failed to parse stream, ignoring"));
3653 case GST_RIFF_LIST_odml:
3654 gst_avi_demux_parse_odml (avi, sub);
3657 case GST_RIFF_LIST_INFO:
3658 gst_buffer_resize (sub, 4, -1);
3659 gst_riff_parse_info (element, sub, &tags);
3661 if (avi->globaltags) {
3662 gst_tag_list_insert (avi->globaltags, tags,
3663 GST_TAG_MERGE_REPLACE);
3665 avi->globaltags = tags;
3671 GST_WARNING_OBJECT (avi,
3672 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3673 GST_FOURCC_ARGS (GST_READ_UINT32_LE (map.data)));
3674 GST_MEMDUMP_OBJECT (avi, "Unknown list", map.data, map.size);
3676 case GST_RIFF_TAG_JUNQ:
3677 case GST_RIFF_TAG_JUNK:
3682 gst_avi_demux_parse_idit (avi, sub);
3685 GST_WARNING_OBJECT (avi,
3686 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
3687 GST_FOURCC_ARGS (tag), offset);
3688 GST_MEMDUMP_OBJECT (avi, "Unknown tag", map.data, map.size);
3690 case GST_RIFF_TAG_JUNQ:
3691 case GST_RIFF_TAG_JUNK:
3694 gst_buffer_unmap (sub, &map);
3695 gst_buffer_unref (sub);
3701 gst_buffer_unref (buf);
3702 GST_DEBUG ("elements parsed");
3704 /* check parsed streams */
3705 if (avi->num_streams == 0)
3707 else if (avi->num_streams != avi->avih->streams) {
3708 GST_WARNING_OBJECT (avi,
3709 "Stream header mentioned %d streams, but %d available",
3710 avi->avih->streams, avi->num_streams);
3713 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
3714 G_GUINT64_FORMAT, avi->offset);
3716 /* Now, find the data (i.e. skip all junk between header and data) */
3722 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
3723 if (res != GST_FLOW_OK) {
3724 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
3725 goto pull_range_failed;
3726 } else if (gst_buffer_get_size (buf) < 12) {
3727 GST_DEBUG_OBJECT (avi,
3728 "got %" G_GSIZE_FORMAT " bytes which is less than 12 bytes",
3729 gst_buffer_get_size (buf));
3730 gst_buffer_unref (buf);
3731 return GST_FLOW_ERROR;
3734 gst_buffer_map (buf, &map, GST_MAP_READ);
3735 tag = GST_READ_UINT32_LE (map.data);
3736 size = GST_READ_UINT32_LE (map.data + 4);
3737 ltag = GST_READ_UINT32_LE (map.data + 8);
3739 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
3740 GST_FOURCC_ARGS (tag), size);
3741 GST_MEMDUMP ("Tag content", map.data, map.size);
3742 gst_buffer_unmap (buf, &map);
3743 gst_buffer_unref (buf);
3746 case GST_RIFF_TAG_LIST:{
3748 case GST_RIFF_LIST_movi:
3749 GST_DEBUG_OBJECT (avi,
3750 "Reached the 'movi' tag, we're done with skipping");
3752 case GST_RIFF_LIST_INFO:
3754 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3756 if (res != GST_FLOW_OK) {
3757 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3758 goto pull_range_failed;
3760 GST_DEBUG ("got size %" G_GSIZE_FORMAT, gst_buffer_get_size (buf));
3762 GST_DEBUG ("skipping INFO LIST prefix");
3763 avi->offset += (4 - GST_ROUND_UP_2 (size));
3764 gst_buffer_unref (buf);
3768 sub = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, 4, -1);
3769 gst_riff_parse_info (element, sub, &tags);
3771 if (avi->globaltags) {
3772 gst_tag_list_insert (avi->globaltags, tags,
3773 GST_TAG_MERGE_REPLACE);
3775 avi->globaltags = tags;
3780 gst_buffer_unref (sub);
3783 gst_buffer_unref (buf);
3784 /* gst_riff_read_chunk() has already advanced avi->offset */
3787 GST_WARNING_OBJECT (avi,
3788 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3789 GST_FOURCC_ARGS (ltag));
3790 avi->offset += 8 + GST_ROUND_UP_2 (size);
3796 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3797 GST_FOURCC_ARGS (tag));
3799 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3800 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3801 /* Only get buffer for debugging if the memdump is needed */
3802 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= 9) {
3803 res = gst_pad_pull_range (avi->sinkpad, avi->offset, size, &buf);
3804 if (res != GST_FLOW_OK) {
3805 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3806 goto pull_range_failed;
3808 gst_buffer_map (buf, &map, GST_MAP_READ);
3809 GST_MEMDUMP ("Junk", map.data, map.size);
3810 gst_buffer_unmap (buf, &map);
3811 gst_buffer_unref (buf);
3813 avi->offset += 8 + GST_ROUND_UP_2 (size);
3819 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3820 avi->num_streams, avi->stream[0].indexes);
3822 /* create or read stream index (for seeking) */
3823 if (avi->stream[0].indexes != NULL) {
3824 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3825 gst_avi_demux_read_subindexes_pull (avi);
3827 if (!avi->have_index) {
3828 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
3829 gst_avi_demux_stream_index (avi);
3831 /* still no index, scan */
3832 if (!avi->have_index) {
3833 gst_avi_demux_stream_scan (avi);
3835 /* still no index.. this is a fatal error for now.
3836 * FIXME, we should switch to plain push mode without seeking
3837 * instead of failing. */
3838 if (!avi->have_index)
3842 /* use the indexes now to construct nice durations */
3843 gst_avi_demux_calculate_durations_from_index (avi);
3845 gst_avi_demux_expose_streams (avi, FALSE);
3847 /* do initial seek to the default segment values */
3848 gst_avi_demux_do_seek (avi, &avi->segment);
3850 /* create initial NEWSEGMENT event */
3852 gst_event_unref (avi->seg_event);
3853 avi->seg_event = gst_event_new_segment (&avi->segment);
3855 stamp = gst_util_get_timestamp () - stamp;
3856 GST_DEBUG_OBJECT (avi, "pulling header took %" GST_TIME_FORMAT,
3857 GST_TIME_ARGS (stamp));
3859 /* at this point we know all the streams and we can signal the no more
3861 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3862 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3869 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3870 ("Invalid AVI header (no LIST at start): %"
3871 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3872 gst_buffer_unref (buf);
3873 return GST_FLOW_ERROR;
3877 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3878 ("Invalid AVI header (no hdrl at start): %"
3879 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3880 gst_buffer_unref (buf);
3881 return GST_FLOW_ERROR;
3885 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3886 ("Invalid AVI header (no avih at start): %"
3887 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3889 gst_buffer_unref (sub);
3890 gst_buffer_unref (buf);
3891 return GST_FLOW_ERROR;
3895 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3896 ("Invalid AVI header (cannot parse avih at start)"));
3897 gst_buffer_unref (buf);
3898 return GST_FLOW_ERROR;
3902 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3903 return GST_FLOW_ERROR;
3907 GST_WARNING ("file without or too big index");
3908 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3909 ("Could not get/create index"));
3910 return GST_FLOW_ERROR;
3914 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3915 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3916 return GST_FLOW_ERROR;
3920 /* move a stream to @index */
3922 gst_avi_demux_move_stream (GstAviDemux * avi, GstAviStream * stream,
3923 GstSegment * segment, guint index)
3925 GST_DEBUG_OBJECT (avi, "Move stream %d to %u", stream->num, index);
3927 if (segment->rate < 0.0) {
3929 /* Because we don't know the frame order we need to push from the prev keyframe
3930 * to the next keyframe. If there is a smart decoder downstream he will notice
3931 * that there are too many encoded frames send and return EOS when there
3932 * are enough decoded frames to fill the segment. */
3933 next_key = gst_avi_demux_index_next (avi, stream, index, TRUE);
3935 /* FIXME, we go back to 0, we should look at segment.start. We will however
3936 * stop earlier when the see the timestamp < segment.start */
3937 stream->start_entry = 0;
3938 stream->step_entry = index;
3939 stream->current_entry = index;
3940 stream->stop_entry = next_key;
3942 GST_DEBUG_OBJECT (avi, "reverse seek: start %u, step %u, stop %u",
3943 stream->start_entry, stream->step_entry, stream->stop_entry);
3945 stream->start_entry = index;
3946 stream->step_entry = index;
3947 stream->stop_entry = gst_avi_demux_index_last (avi, stream);
3949 if (stream->current_entry != index) {
3950 GST_DEBUG_OBJECT (avi, "Move DISCONT from %u to %u",
3951 stream->current_entry, index);
3952 stream->current_entry = index;
3953 stream->discont = TRUE;
3956 /* update the buffer info */
3957 gst_avi_demux_get_buffer_info (avi, stream, index,
3958 &stream->current_timestamp, &stream->current_ts_end,
3959 &stream->current_offset, &stream->current_offset_end);
3961 GST_DEBUG_OBJECT (avi, "Moved to %u, ts %" GST_TIME_FORMAT
3962 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
3963 ", off_end %" G_GUINT64_FORMAT, index,
3964 GST_TIME_ARGS (stream->current_timestamp),
3965 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
3966 stream->current_offset_end);
3968 GST_DEBUG_OBJECT (avi, "Seeking to offset %" G_GUINT64_FORMAT,
3969 stream->index[index].offset);
3973 * Do the actual seeking.
3976 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
3978 GstClockTime seek_time;
3981 GstAviStream *stream;
3983 seek_time = segment->position;
3984 keyframe = ! !(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
3986 GST_DEBUG_OBJECT (avi, "seek to: %" GST_TIME_FORMAT
3987 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
3989 /* FIXME, this code assumes the main stream with keyframes is stream 0,
3990 * which is mostly correct... */
3991 stream = &avi->stream[avi->main_stream];
3993 /* get the entry index for the requested position */
3994 index = gst_avi_demux_index_for_time (avi, stream, seek_time);
3995 GST_DEBUG_OBJECT (avi, "Got entry %u", index);
3997 /* check if we are already on a keyframe */
3998 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
3999 GST_DEBUG_OBJECT (avi, "not keyframe, searching back");
4000 /* now go to the previous keyframe, this is where we should start
4002 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4003 GST_DEBUG_OBJECT (avi, "previous keyframe at %u", index);
4006 /* move the main stream to this position */
4007 gst_avi_demux_move_stream (avi, stream, segment, index);
4010 /* when seeking to a keyframe, we update the result seek time
4011 * to the time of the keyframe. */
4012 seek_time = stream->current_timestamp;
4013 GST_DEBUG_OBJECT (avi, "keyframe adjusted to %" GST_TIME_FORMAT,
4014 GST_TIME_ARGS (seek_time));
4017 /* the seek time is also the position and stream time when going
4019 segment->position = seek_time;
4020 if (segment->rate > 0.0)
4021 segment->time = seek_time;
4023 /* now set DISCONT and align the other streams */
4024 for (i = 0; i < avi->num_streams; i++) {
4025 GstAviStream *ostream;
4027 ostream = &avi->stream[i];
4028 if ((ostream == stream) || (ostream->index == NULL))
4031 /* get the entry index for the requested position */
4032 index = gst_avi_demux_index_for_time (avi, ostream, seek_time);
4034 /* move to previous keyframe */
4035 if (!ENTRY_IS_KEYFRAME (&ostream->index[index]))
4036 index = gst_avi_demux_index_prev (avi, ostream, index, TRUE);
4038 gst_avi_demux_move_stream (avi, ostream, segment, index);
4040 GST_DEBUG_OBJECT (avi, "done seek to: %" GST_TIME_FORMAT,
4041 GST_TIME_ARGS (seek_time));
4047 * Handle seek event in pull mode.
4050 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4055 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4059 GstSegment seeksegment = { 0, };
4063 GST_DEBUG_OBJECT (avi, "doing seek with event");
4065 gst_event_parse_seek (event, &rate, &format, &flags,
4066 &cur_type, &cur, &stop_type, &stop);
4068 /* we have to have a format as the segment format. Try to convert
4070 if (format != GST_FORMAT_TIME) {
4071 gboolean res = TRUE;
4073 if (cur_type != GST_SEEK_TYPE_NONE)
4074 res = gst_pad_query_convert (pad, format, cur, GST_FORMAT_TIME, &cur);
4075 if (res && stop_type != GST_SEEK_TYPE_NONE)
4076 res = gst_pad_query_convert (pad, format, stop, GST_FORMAT_TIME, &stop);
4080 format = GST_FORMAT_TIME;
4082 GST_DEBUG_OBJECT (avi,
4083 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
4084 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
4085 /* FIXME: can we do anything with rate!=1.0 */
4087 GST_DEBUG_OBJECT (avi, "doing seek without event");
4092 /* save flush flag */
4093 flush = flags & GST_SEEK_FLAG_FLUSH;
4096 GstEvent *fevent = gst_event_new_flush_start ();
4098 /* for a flushing seek, we send a flush_start on all pads. This will
4099 * eventually stop streaming with a WRONG_STATE. We can thus eventually
4100 * take the STREAM_LOCK. */
4101 GST_DEBUG_OBJECT (avi, "sending flush start");
4102 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4103 gst_pad_push_event (avi->sinkpad, fevent);
4105 /* a non-flushing seek, we PAUSE the task so that we can take the
4107 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
4108 gst_pad_pause_task (avi->sinkpad);
4111 /* wait for streaming to stop */
4112 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
4113 GST_PAD_STREAM_LOCK (avi->sinkpad);
4115 /* copy segment, we need this because we still need the old
4116 * segment when we close the current segment. */
4117 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4120 GST_DEBUG_OBJECT (avi, "configuring seek");
4121 gst_segment_do_seek (&seeksegment, rate, format, flags,
4122 cur_type, cur, stop_type, stop, &update);
4124 /* do the seek, seeksegment.position contains the new position, this
4125 * actually never fails. */
4126 gst_avi_demux_do_seek (avi, &seeksegment);
4129 GstEvent *fevent = gst_event_new_flush_stop (TRUE);
4131 GST_DEBUG_OBJECT (avi, "sending flush stop");
4132 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4133 gst_pad_push_event (avi->sinkpad, fevent);
4136 /* now update the real segment info */
4137 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
4139 /* post the SEGMENT_START message when we do segmented playback */
4140 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4141 gst_element_post_message (GST_ELEMENT_CAST (avi),
4142 gst_message_new_segment_start (GST_OBJECT_CAST (avi),
4143 avi->segment.format, avi->segment.position));
4146 /* queue the segment event for the streaming thread. */
4148 gst_event_unref (avi->seg_event);
4149 avi->seg_event = gst_event_new_segment (&avi->segment);
4151 if (!avi->streaming) {
4152 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4155 /* reset the last flow and mark discont, seek is always DISCONT */
4156 for (i = 0; i < avi->num_streams; i++) {
4157 GST_DEBUG_OBJECT (avi, "marking DISCONT");
4158 avi->stream[i].last_flow = GST_FLOW_OK;
4159 avi->stream[i].discont = TRUE;
4161 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
4168 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4174 * Handle seek event in push mode.
4177 avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4182 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4185 GstAviStream *stream;
4189 GstSegment seeksegment;
4192 /* check we have the index */
4193 if (!avi->have_index) {
4194 GST_DEBUG_OBJECT (avi, "no seek index built, seek aborted.");
4197 GST_DEBUG_OBJECT (avi, "doing push-based seek with event");
4200 gst_event_parse_seek (event, &rate, &format, &flags,
4201 &cur_type, &cur, &stop_type, &stop);
4203 if (format != GST_FORMAT_TIME) {
4204 gboolean res = TRUE;
4206 if (cur_type != GST_SEEK_TYPE_NONE)
4207 res = gst_pad_query_convert (pad, format, cur, GST_FORMAT_TIME, &cur);
4208 if (res && stop_type != GST_SEEK_TYPE_NONE)
4209 res = gst_pad_query_convert (pad, format, stop, GST_FORMAT_TIME, &stop);
4211 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4215 format = GST_FORMAT_TIME;
4218 /* let gst_segment handle any tricky stuff */
4219 GST_DEBUG_OBJECT (avi, "configuring seek");
4220 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4221 gst_segment_do_seek (&seeksegment, rate, format, flags,
4222 cur_type, cur, stop_type, stop, &update);
4224 keyframe = ! !(flags & GST_SEEK_FLAG_KEY_UNIT);
4225 cur = seeksegment.position;
4227 GST_DEBUG_OBJECT (avi,
4228 "Seek requested: ts %" GST_TIME_FORMAT " stop %" GST_TIME_FORMAT
4229 ", kf %u, rate %lf", GST_TIME_ARGS (cur), GST_TIME_ARGS (stop), keyframe,
4233 GST_DEBUG_OBJECT (avi, "negative rate seek not supported in push mode");
4237 /* FIXME, this code assumes the main stream with keyframes is stream 0,
4238 * which is mostly correct... */
4239 str_num = avi->main_stream;
4240 stream = &avi->stream[str_num];
4242 /* get the entry index for the requested position */
4243 index = gst_avi_demux_index_for_time (avi, stream, cur);
4244 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT,
4245 str_num, index, GST_TIME_ARGS (cur));
4247 /* check if we are already on a keyframe */
4248 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
4249 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4250 /* now go to the previous keyframe, this is where we should start
4252 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4253 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", index);
4256 gst_avi_demux_get_buffer_info (avi, stream, index,
4257 &stream->current_timestamp, &stream->current_ts_end,
4258 &stream->current_offset, &stream->current_offset_end);
4260 /* re-use cur to be the timestamp of the seek as it _will_ be */
4261 cur = stream->current_timestamp;
4263 min_offset = stream->index[index].offset;
4264 avi->seek_kf_offset = min_offset - 8;
4266 GST_DEBUG_OBJECT (avi,
4267 "Seek to: ts %" GST_TIME_FORMAT " (on str %u, idx %u, offset %"
4268 G_GUINT64_FORMAT ")", GST_TIME_ARGS (stream->current_timestamp), str_num,
4271 for (n = 0; n < avi->num_streams; n++) {
4272 GstAviStream *str = &avi->stream[n];
4275 if (n == avi->main_stream)
4278 /* get the entry index for the requested position */
4279 idx = gst_avi_demux_index_for_time (avi, str, cur);
4280 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT, n,
4281 idx, GST_TIME_ARGS (cur));
4283 /* check if we are already on a keyframe */
4284 if (!ENTRY_IS_KEYFRAME (&str->index[idx])) {
4285 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4286 /* now go to the previous keyframe, this is where we should start
4288 idx = gst_avi_demux_index_prev (avi, str, idx, TRUE);
4289 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", idx);
4292 gst_avi_demux_get_buffer_info (avi, str, idx,
4293 &str->current_timestamp, &str->current_ts_end,
4294 &str->current_offset, &str->current_offset_end);
4296 if (str->index[idx].offset < min_offset) {
4297 min_offset = str->index[idx].offset;
4298 GST_DEBUG_OBJECT (avi,
4299 "Found an earlier offset at %" G_GUINT64_FORMAT ", str %u",
4307 GST_DEBUG_OBJECT (avi,
4308 "Seek performed: str %u, offset %" G_GUINT64_FORMAT ", idx %u, ts %"
4309 GST_TIME_FORMAT ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4310 ", off_end %" G_GUINT64_FORMAT, str_num, min_offset, index,
4311 GST_TIME_ARGS (stream->current_timestamp),
4312 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4313 stream->current_offset_end);
4315 /* index data refers to data, not chunk header (for pull mode convenience) */
4317 GST_DEBUG_OBJECT (avi, "seeking to chunk at offset %" G_GUINT64_FORMAT,
4320 if (!perform_seek_to_offset (avi, min_offset)) {
4321 GST_DEBUG_OBJECT (avi, "seek event failed!");
4329 * Handle whether we can perform the seek event or if we have to let the chain
4330 * function handle seeks to build the seek indexes first.
4333 gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
4336 /* check for having parsed index already */
4337 if (!avi->have_index) {
4339 gboolean building_index;
4341 GST_OBJECT_LOCK (avi);
4342 /* handle the seek event in the chain function */
4343 avi->state = GST_AVI_DEMUX_SEEK;
4345 /* copy the event */
4346 if (avi->seek_event)
4347 gst_event_unref (avi->seek_event);
4348 avi->seek_event = gst_event_ref (event);
4350 /* set the building_index flag so that only one thread can setup the
4351 * structures for index seeking. */
4352 building_index = avi->building_index;
4353 if (!building_index) {
4354 avi->building_index = TRUE;
4355 if (avi->stream[0].indexes) {
4356 avi->odml_stream = 0;
4357 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
4358 offset = avi->odml_subidxs[0];
4360 offset = avi->idx1_offset;
4363 GST_OBJECT_UNLOCK (avi);
4365 if (!building_index) {
4366 /* seek to the first subindex or legacy index */
4367 GST_INFO_OBJECT (avi,
4368 "Seeking to legacy index/first subindex at %" G_GUINT64_FORMAT,
4370 return perform_seek_to_offset (avi, offset);
4373 /* FIXME: we have to always return true so that we don't block the seek
4375 * Note: maybe it is OK to return true if we're still building the index */
4379 return avi_demux_handle_seek_push (avi, pad, event);
4383 * Helper for gst_avi_demux_invert()
4386 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
4388 memcpy (tmp, d1, bytes);
4389 memcpy (d1, d2, bytes);
4390 memcpy (d2, tmp, bytes);
4394 #define gst_avi_demux_is_uncompressed(fourcc) \
4396 (fourcc == GST_RIFF_DIB || \
4397 fourcc == GST_RIFF_rgb || \
4398 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW))
4401 * Invert DIB buffers... Takes existing buffer and
4402 * returns either the buffer or a new one (with old
4403 * one dereferenced).
4404 * FIXME: can't we preallocate tmp? and remember stride, bpp?
4407 gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
4416 if (stream->strh->type != GST_RIFF_FCC_vids)
4419 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
4420 return buf; /* Ignore non DIB buffers */
4423 caps = gst_pad_get_current_caps (stream->pad);
4424 s = gst_caps_get_structure (caps, 0);
4425 gst_caps_unref (caps);
4427 if (!gst_structure_get_int (s, "bpp", &bpp)) {
4428 GST_WARNING ("Failed to retrieve depth from caps");
4432 if (stream->strf.vids == NULL) {
4433 GST_WARNING ("Failed to retrieve vids for stream");
4437 h = stream->strf.vids->height;
4438 w = stream->strf.vids->width;
4439 stride = GST_ROUND_UP_4 (w * (bpp / 8));
4441 buf = gst_buffer_make_writable (buf);
4443 gst_buffer_map (buf, &map, GST_MAP_READWRITE);
4444 if (map.size < (stride * h)) {
4445 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
4446 gst_buffer_unmap (buf, &map);
4450 tmp = g_malloc (stride);
4452 for (y = 0; y < h / 2; y++) {
4453 swap_line (map.data + stride * y, map.data + stride * (h - 1 - y), tmp,
4459 gst_buffer_unmap (buf, &map);
4466 gst_avi_demux_add_assoc (GstAviDemux * avi, GstAviStream * stream,
4467 GstClockTime timestamp, guint64 offset, gboolean keyframe)
4469 /* do not add indefinitely for open-ended streaming */
4470 if (G_UNLIKELY (avi->element_index && avi->seekable)) {
4471 GST_LOG_OBJECT (avi, "adding association %" GST_TIME_FORMAT "-> %"
4472 G_GUINT64_FORMAT, GST_TIME_ARGS (timestamp), offset);
4473 gst_index_add_association (avi->element_index, avi->index_id,
4474 keyframe ? GST_ASSOCIATION_FLAG_KEY_UNIT :
4475 GST_ASSOCIATION_FLAG_DELTA_UNIT, GST_FORMAT_TIME, timestamp,
4476 GST_FORMAT_BYTES, offset, NULL);
4477 /* current_entry is DEFAULT (frame #) */
4478 gst_index_add_association (avi->element_index, stream->index_id,
4479 keyframe ? GST_ASSOCIATION_FLAG_KEY_UNIT :
4480 GST_ASSOCIATION_FLAG_DELTA_UNIT, GST_FORMAT_TIME, timestamp,
4481 GST_FORMAT_BYTES, offset, GST_FORMAT_DEFAULT, stream->current_entry,
4488 * Returns the aggregated GstFlowReturn.
4490 static GstFlowReturn
4491 gst_avi_demux_combine_flows (GstAviDemux * avi, GstAviStream * stream,
4495 gboolean unexpected = FALSE, not_linked = TRUE;
4497 /* store the value */
4498 stream->last_flow = ret;
4500 /* any other error that is not-linked or eos can be returned right away */
4501 if (G_LIKELY (ret != GST_FLOW_EOS && ret != GST_FLOW_NOT_LINKED))
4504 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4505 for (i = 0; i < avi->num_streams; i++) {
4506 GstAviStream *ostream = &avi->stream[i];
4508 ret = ostream->last_flow;
4509 /* no unexpected or unlinked, return */
4510 if (G_LIKELY (ret != GST_FLOW_EOS && ret != GST_FLOW_NOT_LINKED))
4513 /* we check to see if we have at least 1 unexpected or all unlinked */
4514 unexpected |= (ret == GST_FLOW_EOS);
4515 not_linked &= (ret == GST_FLOW_NOT_LINKED);
4517 /* when we get here, we all have unlinked or unexpected */
4519 ret = GST_FLOW_NOT_LINKED;
4520 else if (unexpected)
4523 GST_LOG_OBJECT (avi, "combined %s to return %s",
4524 gst_flow_get_name (stream->last_flow), gst_flow_get_name (ret));
4528 /* move @stream to the next position in its index */
4529 static GstFlowReturn
4530 gst_avi_demux_advance (GstAviDemux * avi, GstAviStream * stream,
4533 guint old_entry, new_entry;
4535 old_entry = stream->current_entry;
4537 new_entry = old_entry + 1;
4539 /* see if we reached the end */
4540 if (new_entry >= stream->stop_entry) {
4541 if (avi->segment.rate < 0.0) {
4542 if (stream->step_entry == stream->start_entry) {
4543 /* we stepped all the way to the start, eos */
4544 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
4547 /* backwards, stop becomes step, find a new step */
4548 stream->stop_entry = stream->step_entry;
4549 stream->step_entry = gst_avi_demux_index_prev (avi, stream,
4550 stream->stop_entry, TRUE);
4552 GST_DEBUG_OBJECT (avi,
4553 "reverse playback jump: start %u, step %u, stop %u",
4554 stream->start_entry, stream->step_entry, stream->stop_entry);
4556 /* and start from the previous keyframe now */
4557 new_entry = stream->step_entry;
4560 GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
4565 if (new_entry != old_entry) {
4566 stream->current_entry = new_entry;
4567 stream->current_total = stream->index[new_entry].total;
4569 if (new_entry == old_entry + 1) {
4570 GST_DEBUG_OBJECT (avi, "moved forwards from %u to %u",
4571 old_entry, new_entry);
4572 /* we simply moved one step forwards, reuse current info */
4573 stream->current_timestamp = stream->current_ts_end;
4574 stream->current_offset = stream->current_offset_end;
4575 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4576 NULL, &stream->current_ts_end, NULL, &stream->current_offset_end);
4578 /* we moved DISCONT, full update */
4579 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4580 &stream->current_timestamp, &stream->current_ts_end,
4581 &stream->current_offset, &stream->current_offset_end);
4582 /* and MARK discont for this stream */
4583 stream->last_flow = GST_FLOW_OK;
4584 stream->discont = TRUE;
4585 GST_DEBUG_OBJECT (avi, "Moved from %u to %u, ts %" GST_TIME_FORMAT
4586 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4587 ", off_end %" G_GUINT64_FORMAT, old_entry, new_entry,
4588 GST_TIME_ARGS (stream->current_timestamp),
4589 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4590 stream->current_offset_end);
4598 GST_DEBUG_OBJECT (avi, "we are EOS");
4599 /* setting current_timestamp to -1 marks EOS */
4600 stream->current_timestamp = -1;
4601 return GST_FLOW_EOS;
4605 /* find the stream with the lowest current position when going forwards or with
4606 * the highest position when going backwards, this is the stream
4607 * we should push from next */
4609 gst_avi_demux_find_next (GstAviDemux * avi, gfloat rate)
4611 guint64 min_time, max_time;
4612 guint stream_num, i;
4615 min_time = G_MAXUINT64;
4618 for (i = 0; i < avi->num_streams; i++) {
4620 GstAviStream *stream;
4622 stream = &avi->stream[i];
4624 /* ignore streams that finished */
4625 if (stream->last_flow == GST_FLOW_EOS)
4628 position = stream->current_timestamp;
4630 /* position of -1 is EOS */
4631 if (position != -1) {
4632 if (rate > 0.0 && position < min_time) {
4633 min_time = position;
4635 } else if (rate < 0.0 && position >= max_time) {
4636 max_time = position;
4644 static GstFlowReturn
4645 gst_avi_demux_loop_data (GstAviDemux * avi)
4647 GstFlowReturn ret = GST_FLOW_OK;
4649 GstAviStream *stream;
4650 gboolean processed = FALSE;
4652 guint64 offset, size;
4653 GstClockTime timestamp, duration;
4654 guint64 out_offset, out_offset_end;
4656 GstAviIndexEntry *entry;
4659 stream_num = gst_avi_demux_find_next (avi, avi->segment.rate);
4662 if (G_UNLIKELY (stream_num == -1)) {
4663 GST_DEBUG_OBJECT (avi, "all streams are EOS");
4667 /* we have the stream now */
4668 stream = &avi->stream[stream_num];
4670 /* skip streams without pads */
4672 GST_DEBUG_OBJECT (avi, "skipping entry from stream %d without pad",
4677 /* get the timing info for the entry */
4678 timestamp = stream->current_timestamp;
4679 duration = stream->current_ts_end - timestamp;
4680 out_offset = stream->current_offset;
4681 out_offset_end = stream->current_offset_end;
4683 /* get the entry data info */
4684 entry = &stream->index[stream->current_entry];
4685 offset = entry->offset;
4687 keyframe = ENTRY_IS_KEYFRAME (entry);
4689 /* skip empty entries */
4691 GST_DEBUG_OBJECT (avi, "Skipping entry %u (%" G_GUINT64_FORMAT ", %p)",
4692 stream->current_entry, size, stream->pad);
4696 if (avi->segment.rate > 0.0) {
4697 /* only check this for fowards playback for now */
4698 if (keyframe && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
4699 && (timestamp > avi->segment.stop)) {
4704 GST_LOG ("reading buffer (size=%" G_GUINT64_FORMAT "), stream %d, pos %"
4705 G_GUINT64_FORMAT " (0x%" G_GINT64_MODIFIER "x), kf %d", size,
4706 stream_num, offset, offset, keyframe);
4708 /* FIXME, check large chunks and cut them up */
4710 /* pull in the data */
4711 ret = gst_pad_pull_range (avi->sinkpad, offset, size, &buf);
4712 if (ret != GST_FLOW_OK)
4715 /* check for short buffers, this is EOS as well */
4716 if (gst_buffer_get_size (buf) < size)
4719 /* invert the picture if needed */
4720 buf = gst_avi_demux_invert (stream, buf);
4722 /* mark non-keyframes */
4724 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4726 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4728 GST_BUFFER_TIMESTAMP (buf) = timestamp;
4729 GST_BUFFER_DURATION (buf) = duration;
4730 GST_BUFFER_OFFSET (buf) = out_offset;
4731 GST_BUFFER_OFFSET_END (buf) = out_offset_end;
4733 /* mark discont when pending */
4734 if (stream->discont) {
4735 GST_DEBUG_OBJECT (avi, "setting DISCONT flag");
4736 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4737 stream->discont = FALSE;
4740 gst_avi_demux_add_assoc (avi, stream, timestamp, offset, keyframe);
4743 /* update current position in the segment */
4744 avi->segment.position = timestamp;
4746 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %" G_GSIZE_FORMAT ", ts %"
4747 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4748 ", off_end %" G_GUINT64_FORMAT,
4749 gst_buffer_get_size (buf), GST_TIME_ARGS (timestamp),
4750 GST_TIME_ARGS (duration), out_offset, out_offset_end);
4752 ret = gst_pad_push (stream->pad, buf);
4754 /* mark as processed, we increment the frame and byte counters then
4755 * leave the while loop and return the GstFlowReturn */
4758 if (avi->segment.rate < 0) {
4759 if (timestamp > avi->segment.stop && ret == GST_FLOW_EOS) {
4760 /* In reverse playback we can get a GST_FLOW_EOS when
4761 * we are at the end of the segment, so we just need to jump
4762 * back to the previous section. */
4763 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
4768 /* move to next item */
4769 ret = gst_avi_demux_advance (avi, stream, ret);
4772 ret = gst_avi_demux_combine_flows (avi, stream, ret);
4773 } while (!processed);
4781 GST_DEBUG_OBJECT (avi, "No samples left for any streams - EOS");
4787 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
4788 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
4789 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (avi->segment.stop));
4791 /* move to next stream */
4796 GST_DEBUG_OBJECT (avi, "pull range failed: pos=%" G_GUINT64_FORMAT
4797 " size=%" G_GUINT64_FORMAT, offset, size);
4802 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
4803 ", only got %" G_GSIZE_FORMAT "/%" G_GUINT64_FORMAT
4804 " bytes (truncated file?)", offset, gst_buffer_get_size (buf), size);
4805 gst_buffer_unref (buf);
4812 * Read data. If we have an index it delegates to
4813 * gst_avi_demux_process_next_entry().
4815 static GstFlowReturn
4816 gst_avi_demux_stream_data (GstAviDemux * avi)
4821 GstFlowReturn res = GST_FLOW_OK;
4823 if (G_UNLIKELY (avi->have_eos)) {
4824 /* Clean adapter, we're done */
4825 gst_adapter_clear (avi->adapter);
4826 return GST_FLOW_EOS;
4829 if (G_UNLIKELY (avi->todrop)) {
4832 if ((drop = gst_adapter_available (avi->adapter))) {
4833 if (drop > avi->todrop)
4835 GST_DEBUG_OBJECT (avi, "Dropping %d bytes", drop);
4836 gst_adapter_flush (avi->adapter, drop);
4837 avi->todrop -= drop;
4838 avi->offset += drop;
4842 /* Iterate until need more data, so adapter won't grow too much */
4844 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
4848 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
4849 GST_FOURCC_ARGS (tag), size);
4851 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
4852 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
4853 GST_LOG ("Chunk ok");
4854 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
4855 GST_DEBUG ("Found sub-index tag");
4856 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4857 /* accept 0 size buffer here */
4858 avi->abort_buffering = FALSE;
4859 GST_DEBUG (" skipping %d bytes for now", size);
4860 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4863 } else if (tag == GST_RIFF_TAG_RIFF) {
4864 /* RIFF tags can appear in ODML files, just jump over them */
4865 if (gst_adapter_available (avi->adapter) >= 12) {
4866 GST_DEBUG ("Found RIFF tag, skipping RIFF header");
4867 gst_adapter_flush (avi->adapter, 12);
4871 } else if (tag == GST_RIFF_TAG_idx1) {
4872 GST_DEBUG ("Found index tag");
4873 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4874 /* accept 0 size buffer here */
4875 avi->abort_buffering = FALSE;
4876 GST_DEBUG (" skipping %d bytes for now", size);
4877 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4880 } else if (tag == GST_RIFF_TAG_LIST) {
4881 /* movi chunks might be grouped in rec list */
4882 if (gst_adapter_available (avi->adapter) >= 12) {
4883 GST_DEBUG ("Found LIST tag, skipping LIST header");
4884 gst_adapter_flush (avi->adapter, 12);
4888 } else if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ) {
4889 /* rec list might contain JUNK chunks */
4890 GST_DEBUG ("Found JUNK tag");
4891 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4892 /* accept 0 size buffer here */
4893 avi->abort_buffering = FALSE;
4894 GST_DEBUG (" skipping %d bytes for now", size);
4895 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4899 GST_DEBUG ("No more stream chunks, send EOS");
4900 avi->have_eos = TRUE;
4901 return GST_FLOW_EOS;
4904 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
4905 /* supposedly one hopes to catch a nicer chunk later on ... */
4906 /* FIXME ?? give up here rather than possibly ending up going
4907 * through the whole file */
4908 if (avi->abort_buffering) {
4909 avi->abort_buffering = FALSE;
4911 gst_adapter_flush (avi->adapter, 8);
4918 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
4919 GST_FOURCC_ARGS (tag), size);
4921 stream_nr = CHUNKID_TO_STREAMNR (tag);
4923 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
4925 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
4926 stream_nr, GST_FOURCC_ARGS (tag));
4927 avi->offset += 8 + GST_ROUND_UP_2 (size);
4928 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4930 GstAviStream *stream;
4931 GstClockTime next_ts = 0;
4932 GstBuffer *buf = NULL;
4936 gboolean saw_desired_kf = stream_nr != avi->main_stream
4937 || avi->offset >= avi->seek_kf_offset;
4939 if (stream_nr == avi->main_stream && avi->offset == avi->seek_kf_offset) {
4940 GST_DEBUG_OBJECT (avi, "Desired keyframe reached");
4941 avi->seek_kf_offset = 0;
4944 if (saw_desired_kf) {
4945 gst_adapter_flush (avi->adapter, 8);
4948 buf = gst_adapter_take_buffer (avi->adapter, GST_ROUND_UP_2 (size));
4949 /* patch the size */
4950 gst_buffer_resize (buf, 0, size);
4955 GST_DEBUG_OBJECT (avi,
4956 "Desired keyframe not yet reached, flushing chunk");
4957 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4961 offset = avi->offset;
4963 avi->offset += 8 + GST_ROUND_UP_2 (size);
4965 stream = &avi->stream[stream_nr];
4967 /* set delay (if any)
4968 if (stream->strh->init_frames == stream->current_frame &&
4970 stream->delay = next_ts;
4973 /* parsing of corresponding header may have failed */
4974 if (G_UNLIKELY (!stream->pad)) {
4975 GST_WARNING_OBJECT (avi, "no pad for stream ID %" GST_FOURCC_FORMAT,
4976 GST_FOURCC_ARGS (tag));
4978 gst_buffer_unref (buf);
4980 /* get time of this buffer */
4981 gst_pad_query_position (stream->pad, GST_FORMAT_TIME,
4982 (gint64 *) & next_ts);
4985 gst_avi_demux_add_assoc (avi, stream, next_ts, offset, FALSE);
4988 /* increment our positions */
4989 stream->current_entry++;
4990 stream->current_total += size;
4992 /* update current position in the segment */
4993 avi->segment.position = next_ts;
4995 if (saw_desired_kf && buf) {
4996 GstClockTime dur_ts = 0;
4998 /* invert the picture if needed */
4999 buf = gst_avi_demux_invert (stream, buf);
5001 gst_pad_query_position (stream->pad, GST_FORMAT_TIME,
5002 (gint64 *) & dur_ts);
5004 GST_BUFFER_TIMESTAMP (buf) = next_ts;
5005 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
5006 if (stream->strh->type == GST_RIFF_FCC_vids) {
5007 GST_BUFFER_OFFSET (buf) = stream->current_entry - 1;
5008 GST_BUFFER_OFFSET_END (buf) = stream->current_entry;
5010 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
5011 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
5014 GST_DEBUG_OBJECT (avi,
5015 "Pushing buffer with time=%" GST_TIME_FORMAT ", duration %"
5016 GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
5017 " and size %d over pad %s", GST_TIME_ARGS (next_ts),
5018 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
5019 GST_BUFFER_OFFSET (buf), size, GST_PAD_NAME (stream->pad));
5021 /* mark discont when pending */
5022 if (G_UNLIKELY (stream->discont)) {
5023 GST_DEBUG_OBJECT (avi, "Setting DISCONT");
5024 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
5025 stream->discont = FALSE;
5027 res = gst_pad_push (stream->pad, buf);
5031 res = gst_avi_demux_combine_flows (avi, stream, res);
5032 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5033 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
5045 * Send pending tags.
5048 push_tag_lists (GstAviDemux * avi)
5056 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
5058 for (i = 0; i < avi->num_streams; i++) {
5059 GstAviStream *stream = &avi->stream[i];
5060 GstPad *pad = stream->pad;
5062 tags = stream->taglist;
5065 GST_DEBUG_OBJECT (pad, "Tags: %" GST_PTR_FORMAT, tags);
5067 gst_pad_push_event (pad, gst_event_new_tag (tags));
5068 stream->taglist = NULL;
5072 if (!(tags = avi->globaltags))
5073 tags = gst_tag_list_new_empty ();
5075 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
5076 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
5078 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, tags);
5079 gst_avi_demux_push_event (avi, gst_event_new_tag (tags));
5080 avi->globaltags = NULL;
5081 avi->got_tags = FALSE;
5085 gst_avi_demux_loop (GstPad * pad)
5088 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
5090 switch (avi->state) {
5091 case GST_AVI_DEMUX_START:
5092 res = gst_avi_demux_stream_init_pull (avi);
5093 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5094 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5097 avi->state = GST_AVI_DEMUX_HEADER;
5099 case GST_AVI_DEMUX_HEADER:
5100 res = gst_avi_demux_stream_header_pull (avi);
5101 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5102 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5105 avi->state = GST_AVI_DEMUX_MOVI;
5107 case GST_AVI_DEMUX_MOVI:
5108 if (G_UNLIKELY (avi->seg_event)) {
5109 gst_avi_demux_push_event (avi, avi->seg_event);
5110 avi->seg_event = NULL;
5112 if (G_UNLIKELY (avi->got_tags)) {
5113 push_tag_lists (avi);
5115 /* process each index entry in turn */
5116 res = gst_avi_demux_loop_data (avi);
5118 /* pause when error */
5119 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5120 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
5125 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
5126 res = GST_FLOW_ERROR;
5135 gboolean push_eos = FALSE;
5136 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
5137 gst_pad_pause_task (avi->sinkpad);
5139 if (res == GST_FLOW_EOS) {
5140 /* handle end-of-stream/segment */
5141 /* so align our position with the end of it, if there is one
5142 * this ensures a subsequent will arrive at correct base/acc time */
5143 if (avi->segment.rate > 0.0 &&
5144 GST_CLOCK_TIME_IS_VALID (avi->segment.stop))
5145 avi->segment.position = avi->segment.stop;
5146 else if (avi->segment.rate < 0.0)
5147 avi->segment.position = avi->segment.start;
5148 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5151 if ((stop = avi->segment.stop) == -1)
5152 stop = avi->segment.duration;
5154 GST_INFO_OBJECT (avi, "sending segment_done");
5156 gst_element_post_message
5157 (GST_ELEMENT_CAST (avi),
5158 gst_message_new_segment_done (GST_OBJECT_CAST (avi),
5159 GST_FORMAT_TIME, stop));
5163 } else if (res == GST_FLOW_NOT_LINKED || res < GST_FLOW_EOS) {
5164 /* for fatal errors we post an error message, wrong-state is
5165 * not fatal because it happens due to flushes and only means
5166 * that we should stop now. */
5167 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
5168 (_("Internal data stream error.")),
5169 ("streaming stopped, reason %s", gst_flow_get_name (res)));
5173 GST_INFO_OBJECT (avi, "sending eos");
5174 if (!gst_avi_demux_push_event (avi, gst_event_new_eos ()) &&
5175 (res == GST_FLOW_EOS)) {
5176 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
5177 (NULL), ("got eos but no streams (yet)"));
5184 static GstFlowReturn
5185 gst_avi_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
5188 GstAviDemux *avi = GST_AVI_DEMUX (parent);
5191 if (GST_BUFFER_IS_DISCONT (buf)) {
5192 GST_DEBUG_OBJECT (avi, "got DISCONT");
5193 gst_adapter_clear (avi->adapter);
5194 /* mark all streams DISCONT */
5195 for (i = 0; i < avi->num_streams; i++)
5196 avi->stream[i].discont = TRUE;
5199 GST_DEBUG ("Store %" G_GSIZE_FORMAT " bytes in adapter",
5200 gst_buffer_get_size (buf));
5201 gst_adapter_push (avi->adapter, buf);
5203 switch (avi->state) {
5204 case GST_AVI_DEMUX_START:
5205 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
5206 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5210 case GST_AVI_DEMUX_HEADER:
5211 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
5212 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5216 case GST_AVI_DEMUX_MOVI:
5217 if (G_UNLIKELY (avi->seg_event)) {
5218 gst_avi_demux_push_event (avi, avi->seg_event);
5219 avi->seg_event = NULL;
5221 if (G_UNLIKELY (avi->got_tags)) {
5222 push_tag_lists (avi);
5224 res = gst_avi_demux_stream_data (avi);
5226 case GST_AVI_DEMUX_SEEK:
5232 /* obtain and parse indexes */
5233 if (avi->stream[0].indexes && !gst_avi_demux_read_subindexes_push (avi))
5234 /* seek in subindex read function failed */
5237 if (!avi->stream[0].indexes && !avi->have_index
5238 && avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
5239 gst_avi_demux_stream_index_push (avi);
5241 if (avi->have_index) {
5242 /* use the indexes now to construct nice durations */
5243 gst_avi_demux_calculate_durations_from_index (avi);
5245 /* still parsing indexes */
5249 GST_OBJECT_LOCK (avi);
5250 event = avi->seek_event;
5251 avi->seek_event = NULL;
5252 GST_OBJECT_UNLOCK (avi);
5254 /* calculate and perform seek */
5255 if (!avi_demux_handle_seek_push (avi, avi->sinkpad, event))
5258 gst_event_unref (event);
5259 avi->state = GST_AVI_DEMUX_MOVI;
5263 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
5264 ("Illegal internal state"));
5265 res = GST_FLOW_ERROR;
5269 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
5270 gst_flow_get_name (res));
5272 if (G_UNLIKELY (avi->abort_buffering))
5273 goto abort_buffering;
5280 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("failed to read indexes"));
5281 return GST_FLOW_ERROR;
5285 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("push mode seek failed"));
5286 return GST_FLOW_ERROR;
5290 avi->abort_buffering = FALSE;
5291 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
5292 return GST_FLOW_ERROR;
5297 gst_avi_demux_sink_activate (GstPad * sinkpad, GstObject * parent)
5302 query = gst_query_new_scheduling ();
5304 if (!gst_pad_peer_query (sinkpad, query)) {
5305 gst_query_unref (query);
5309 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
5310 gst_query_unref (query);
5315 GST_DEBUG_OBJECT (sinkpad, "activating pull");
5316 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
5320 GST_DEBUG_OBJECT (sinkpad, "activating push");
5321 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
5326 gst_avi_demux_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
5327 GstPadMode mode, gboolean active)
5330 GstAviDemux *avi = GST_AVI_DEMUX (parent);
5333 case GST_PAD_MODE_PULL:
5335 avi->streaming = FALSE;
5336 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
5339 res = gst_pad_stop_task (sinkpad);
5342 case GST_PAD_MODE_PUSH:
5344 GST_DEBUG ("avi: activating push/chain function");
5345 avi->streaming = TRUE;
5347 GST_DEBUG ("avi: deactivating push/chain function");
5360 gst_avi_demux_set_index (GstElement * element, GstIndex * index)
5362 GstAviDemux *avi = GST_AVI_DEMUX (element);
5364 GST_OBJECT_LOCK (avi);
5365 if (avi->element_index)
5366 gst_object_unref (avi->element_index);
5368 avi->element_index = gst_object_ref (index);
5370 avi->element_index = NULL;
5372 GST_OBJECT_UNLOCK (avi);
5373 /* object lock might be taken again */
5375 gst_index_get_writer_id (index, GST_OBJECT_CAST (element), &avi->index_id);
5376 GST_DEBUG_OBJECT (avi, "Set index %" GST_PTR_FORMAT, avi->element_index);
5380 gst_avi_demux_get_index (GstElement * element)
5382 GstIndex *result = NULL;
5383 GstAviDemux *avi = GST_AVI_DEMUX (element);
5385 GST_OBJECT_LOCK (avi);
5386 if (avi->element_index)
5387 result = gst_object_ref (avi->element_index);
5388 GST_OBJECT_UNLOCK (avi);
5390 GST_DEBUG_OBJECT (avi, "Returning index %" GST_PTR_FORMAT, result);
5396 static GstStateChangeReturn
5397 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
5399 GstStateChangeReturn ret;
5400 GstAviDemux *avi = GST_AVI_DEMUX (element);
5402 switch (transition) {
5403 case GST_STATE_CHANGE_READY_TO_PAUSED:
5404 avi->streaming = FALSE;
5405 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
5411 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5412 if (ret == GST_STATE_CHANGE_FAILURE)
5415 switch (transition) {
5416 case GST_STATE_CHANGE_PAUSED_TO_READY:
5417 avi->have_index = FALSE;
5418 gst_avi_demux_reset (avi);