2 * Copyright (C) <1999> Erik Walthinsen <omega@temple-baptist.com>
3 * Copyright (C) <2006> Nokia Corporation (contact <stefan.kost@nokia.com>)
4 * Copyright (C) <2009-2010> STEricsson <benjamin.gaignard@stericsson.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 /* Element-Checklist-Version: 5 */
24 * SECTION:element-avidemux
26 * Demuxes an .avi file into raw or compressed audio and/or video streams.
28 * This element supports both push and pull-based scheduling, depending on the
29 * capabilities of the upstream elements.
32 * <title>Example launch line</title>
34 * gst-launch filesrc location=test.avi ! avidemux name=demux demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink demux.video_00 ! queue ! decodebin ! ffmpegcolorspace ! videoscale ! autovideosink
35 * ]| Play (parse and decode) an .avi file and try to output it to
36 * an automatically detected soundcard and videosink. If the AVI file contains
37 * compressed audio or video data, this will only work if you have the
38 * right decoder elements/plugins installed.
41 * Last reviewed on 2006-12-29 (0.10.6)
51 #include "gst/riff/riff-media.h"
52 #include "gstavidemux.h"
54 #include <gst/gst-i18n-plugin.h>
55 #include <gst/base/gstadapter.h>
58 #define DIV_ROUND_UP(s,v) (((s) + ((v)-1)) / (v))
60 #define GST_AVI_KEYFRAME 1
61 #define ENTRY_IS_KEYFRAME(e) ((e)->flags == GST_AVI_KEYFRAME)
62 #define ENTRY_SET_KEYFRAME(e) ((e)->flags = GST_AVI_KEYFRAME)
63 #define ENTRY_UNSET_KEYFRAME(e) ((e)->flags = 0)
66 GST_DEBUG_CATEGORY_STATIC (avidemux_debug);
67 #define GST_CAT_DEFAULT avidemux_debug
69 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
72 GST_STATIC_CAPS ("video/x-msvideo")
75 static void gst_avi_demux_base_init (GstAviDemuxClass * klass);
76 static void gst_avi_demux_class_init (GstAviDemuxClass * klass);
77 static void gst_avi_demux_init (GstAviDemux * avi);
78 static void gst_avi_demux_finalize (GObject * object);
80 static void gst_avi_demux_reset (GstAviDemux * avi);
83 static const GstEventMask *gst_avi_demux_get_event_mask (GstPad * pad);
85 static gboolean gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event);
86 static gboolean gst_avi_demux_handle_sink_event (GstPad * pad,
88 static gboolean gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event);
91 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
93 static const GstQueryType *gst_avi_demux_get_src_query_types (GstPad * pad);
94 static gboolean gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query);
95 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
96 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
98 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
99 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
101 static gboolean gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
103 static void gst_avi_demux_loop (GstPad * pad);
104 static gboolean gst_avi_demux_sink_activate (GstPad * sinkpad);
105 static gboolean gst_avi_demux_sink_activate_pull (GstPad * sinkpad,
107 static gboolean gst_avi_demux_activate_push (GstPad * pad, gboolean active);
108 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstBuffer * buf);
110 static void gst_avi_demux_set_index (GstElement * element, GstIndex * index);
111 static GstIndex *gst_avi_demux_get_index (GstElement * element);
112 static GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
113 GstStateChange transition);
114 static void gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi);
115 static void gst_avi_demux_get_buffer_info (GstAviDemux * avi,
116 GstAviStream * stream, guint entry_n, GstClockTime * timestamp,
117 GstClockTime * ts_end, guint64 * offset, guint64 * offset_end);
119 static void gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf);
121 static GstElementClass *parent_class = NULL;
123 /* GObject methods */
126 gst_avi_demux_get_type (void)
128 static GType avi_demux_type = 0;
130 if (!avi_demux_type) {
131 static const GTypeInfo avi_demux_info = {
132 sizeof (GstAviDemuxClass),
133 (GBaseInitFunc) gst_avi_demux_base_init,
135 (GClassInitFunc) gst_avi_demux_class_init,
138 sizeof (GstAviDemux),
140 (GInstanceInitFunc) gst_avi_demux_init,
144 g_type_register_static (GST_TYPE_ELEMENT,
145 "GstAviDemux", &avi_demux_info, 0);
148 return avi_demux_type;
152 gst_avi_demux_base_init (GstAviDemuxClass * klass)
154 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
155 GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl;
156 GstCaps *audcaps, *vidcaps, *subcaps;
158 audcaps = gst_riff_create_audio_template_caps ();
159 gst_caps_append (audcaps, gst_caps_new_simple ("audio/x-avi-unknown", NULL));
160 audiosrctempl = gst_pad_template_new ("audio_%02d",
161 GST_PAD_SRC, GST_PAD_SOMETIMES, audcaps);
163 vidcaps = gst_riff_create_video_template_caps ();
164 gst_caps_append (vidcaps, gst_riff_create_iavs_template_caps ());
165 gst_caps_append (vidcaps, gst_caps_new_simple ("video/x-avi-unknown", NULL));
166 videosrctempl = gst_pad_template_new ("video_%02d",
167 GST_PAD_SRC, GST_PAD_SOMETIMES, vidcaps);
169 subcaps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
170 subsrctempl = gst_pad_template_new ("subtitle_%02d",
171 GST_PAD_SRC, GST_PAD_SOMETIMES, subcaps);
172 gst_element_class_add_pad_template (element_class, audiosrctempl);
173 gst_element_class_add_pad_template (element_class, videosrctempl);
174 gst_element_class_add_pad_template (element_class, subsrctempl);
175 gst_element_class_add_pad_template (element_class,
176 gst_static_pad_template_get (&sink_templ));
177 gst_element_class_set_details_simple (element_class, "Avi demuxer",
179 "Demultiplex an avi file into audio and video",
180 "Erik Walthinsen <omega@cse.ogi.edu>, "
181 "Wim Taymans <wim.taymans@chello.be>, "
182 "Thijs Vermeir <thijsvermeir@gmail.com>");
186 gst_avi_demux_class_init (GstAviDemuxClass * klass)
188 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
189 GObjectClass *gobject_class = (GObjectClass *) klass;
191 GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
192 0, "Demuxer for AVI streams");
194 parent_class = g_type_class_peek_parent (klass);
196 gobject_class->finalize = gst_avi_demux_finalize;
197 gstelement_class->change_state =
198 GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
200 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_avi_demux_set_index);
201 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_avi_demux_get_index);
205 gst_avi_demux_init (GstAviDemux * avi)
207 avi->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
208 gst_pad_set_activate_function (avi->sinkpad,
209 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate));
210 gst_pad_set_activatepull_function (avi->sinkpad,
211 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate_pull));
212 gst_pad_set_activatepush_function (avi->sinkpad,
213 GST_DEBUG_FUNCPTR (gst_avi_demux_activate_push));
214 gst_pad_set_chain_function (avi->sinkpad,
215 GST_DEBUG_FUNCPTR (gst_avi_demux_chain));
216 gst_pad_set_event_function (avi->sinkpad,
217 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_sink_event));
218 gst_element_add_pad (GST_ELEMENT_CAST (avi), avi->sinkpad);
220 avi->adapter = gst_adapter_new ();
222 gst_avi_demux_reset (avi);
226 gst_avi_demux_finalize (GObject * object)
228 GstAviDemux *avi = GST_AVI_DEMUX (object);
230 GST_DEBUG ("AVI: finalize");
232 g_object_unref (avi->adapter);
234 G_OBJECT_CLASS (parent_class)->finalize (object);
238 gst_avi_demux_reset_stream (GstAviDemux * avi, GstAviStream * stream)
240 g_free (stream->strh);
241 g_free (stream->strf.data);
242 g_free (stream->name);
243 g_free (stream->index);
244 g_free (stream->indexes);
245 if (stream->initdata)
246 gst_buffer_unref (stream->initdata);
247 if (stream->extradata)
248 gst_buffer_unref (stream->extradata);
250 if (stream->exposed) {
251 gst_pad_set_active (stream->pad, FALSE);
252 gst_element_remove_pad (GST_ELEMENT_CAST (avi), stream->pad);
254 gst_object_unref (stream->pad);
256 if (stream->taglist) {
257 gst_tag_list_free (stream->taglist);
258 stream->taglist = NULL;
260 memset (stream, 0, sizeof (GstAviStream));
264 gst_avi_demux_reset (GstAviDemux * avi)
268 GST_DEBUG ("AVI: reset");
270 for (i = 0; i < avi->num_streams; i++)
271 gst_avi_demux_reset_stream (avi, &avi->stream[i]);
273 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
274 avi->num_streams = 0;
275 avi->num_v_streams = 0;
276 avi->num_a_streams = 0;
277 avi->num_t_streams = 0;
278 avi->main_stream = -1;
280 avi->state = GST_AVI_DEMUX_START;
282 avi->building_index = FALSE;
284 avi->index_offset = 0;
288 if (avi->element_index)
289 gst_object_unref (avi->element_index);
290 avi->element_index = NULL;
292 if (avi->seg_event) {
293 gst_event_unref (avi->seg_event);
294 avi->seg_event = NULL;
296 if (avi->seek_event) {
297 gst_event_unref (avi->seek_event);
298 avi->seek_event = NULL;
302 gst_tag_list_free (avi->globaltags);
303 avi->globaltags = NULL;
305 avi->got_tags = TRUE; /* we always want to push global tags */
306 avi->have_eos = FALSE;
307 avi->seekable = TRUE;
309 gst_adapter_clear (avi->adapter);
311 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
315 /* GstElement methods */
318 static const GstFormat *
319 gst_avi_demux_get_src_formats (GstPad * pad)
321 GstAviStream *stream = gst_pad_get_element_private (pad);
323 static const GstFormat src_a_formats[] = {
329 static const GstFormat src_v_formats[] = {
335 return (stream->strh->type == GST_RIFF_FCC_auds ?
336 src_a_formats : src_v_formats);
340 /* assumes stream->strf.auds->av_bps != 0 */
341 static inline GstClockTime
342 avi_stream_convert_bytes_to_time_unchecked (GstAviStream * stream,
345 return gst_util_uint64_scale_int (bytes, GST_SECOND,
346 stream->strf.auds->av_bps);
349 static inline guint64
350 avi_stream_convert_time_to_bytes_unchecked (GstAviStream * stream,
353 return gst_util_uint64_scale_int (time, stream->strf.auds->av_bps,
357 /* assumes stream->strh->rate != 0 */
358 static inline GstClockTime
359 avi_stream_convert_frames_to_time_unchecked (GstAviStream * stream,
362 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
366 static inline guint64
367 avi_stream_convert_time_to_frames_unchecked (GstAviStream * stream,
370 return gst_util_uint64_scale (time, stream->strh->rate,
371 stream->strh->scale * GST_SECOND);
375 gst_avi_demux_src_convert (GstPad * pad,
376 GstFormat src_format,
377 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
379 GstAviStream *stream = gst_pad_get_element_private (pad);
383 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
384 ", dest_format:%s", gst_format_get_name (src_format), src_value,
385 gst_format_get_name (*dest_format));
387 if (G_UNLIKELY (src_format == *dest_format)) {
388 *dest_value = src_value;
391 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
395 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
396 (src_format == GST_FORMAT_BYTES
397 || *dest_format == GST_FORMAT_BYTES))) {
402 switch (src_format) {
403 case GST_FORMAT_TIME:
404 switch (*dest_format) {
405 case GST_FORMAT_BYTES:
406 *dest_value = gst_util_uint64_scale_int (src_value,
407 stream->strf.auds->av_bps, GST_SECOND);
409 case GST_FORMAT_DEFAULT:
411 gst_util_uint64_scale_round (src_value, stream->strh->rate,
412 stream->strh->scale * GST_SECOND);
419 case GST_FORMAT_BYTES:
420 switch (*dest_format) {
421 case GST_FORMAT_TIME:
422 if (stream->strf.auds->av_bps != 0) {
423 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
433 case GST_FORMAT_DEFAULT:
434 switch (*dest_format) {
435 case GST_FORMAT_TIME:
437 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
450 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
451 gst_format_get_name (*dest_format), *dest_value);
455 static const GstQueryType *
456 gst_avi_demux_get_src_query_types (GstPad * pad)
458 static const GstQueryType src_types[] = {
470 gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query)
473 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
475 GstAviStream *stream = gst_pad_get_element_private (pad);
477 if (!stream->strh || !stream->strf.data)
478 return gst_pad_query_default (pad, query);
480 switch (GST_QUERY_TYPE (query)) {
481 case GST_QUERY_POSITION:{
484 GST_DEBUG ("pos query for stream %u: frames %u, bytes %u",
485 stream->num, stream->current_entry, stream->current_total);
487 /* FIXME, this looks clumsy */
488 if (stream->strh->type == GST_RIFF_FCC_auds) {
489 if (stream->is_vbr) {
491 pos = gst_util_uint64_scale ((gint64) stream->current_entry *
492 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
493 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
494 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
495 } else if (stream->strf.auds->av_bps != 0) {
497 pos = gst_util_uint64_scale (stream->current_total, GST_SECOND,
498 (guint64) stream->strf.auds->av_bps);
499 GST_DEBUG_OBJECT (avi,
500 "CBR convert bytes %u, time %" GST_TIME_FORMAT,
501 stream->current_total, GST_TIME_ARGS (pos));
502 } else if (stream->idx_n != 0 && stream->total_bytes != 0) {
503 /* calculate timestamps based on percentage of length */
504 guint64 xlen = avi->avih->us_frame *
505 avi->avih->tot_frames * GST_USECOND;
507 if (stream->is_vbr) {
508 pos = gst_util_uint64_scale (xlen, stream->current_entry,
510 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
511 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
513 pos = gst_util_uint64_scale (xlen, stream->current_total,
514 stream->total_bytes);
515 GST_DEBUG_OBJECT (avi,
516 "CBR perc convert bytes %u, time %" GST_TIME_FORMAT,
517 stream->current_total, GST_TIME_ARGS (pos));
524 if (stream->strh->rate != 0) {
525 pos = gst_util_uint64_scale ((guint64) stream->current_entry *
526 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
528 pos = stream->current_entry * avi->avih->us_frame * GST_USECOND;
532 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
533 gst_query_set_position (query, GST_FORMAT_TIME, pos);
535 GST_WARNING ("pos query failed");
538 case GST_QUERY_DURATION:
541 GstClockTime duration;
543 /* only act on audio or video streams */
544 if (stream->strh->type != GST_RIFF_FCC_auds &&
545 stream->strh->type != GST_RIFF_FCC_vids) {
550 /* take stream duration, fall back to avih duration */
551 if ((duration = stream->duration) == -1)
552 duration = avi->duration;
554 gst_query_parse_duration (query, &fmt, NULL);
557 case GST_FORMAT_TIME:
558 gst_query_set_duration (query, fmt, duration);
560 case GST_FORMAT_DEFAULT:
563 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
566 if (stream->idx_n >= 0)
567 gst_query_set_duration (query, fmt, stream->idx_n);
568 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
569 duration, &fmt, &dur))
570 gst_query_set_duration (query, fmt, dur);
579 case GST_QUERY_SEEKING:{
582 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
583 if (fmt == GST_FORMAT_TIME) {
584 gboolean seekable = TRUE;
586 if (avi->streaming) {
587 seekable = avi->seekable;
590 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
591 0, stream->duration);
596 case GST_QUERY_CONVERT:{
597 GstFormat src_fmt, dest_fmt;
598 gint64 src_val, dest_val;
600 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
601 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
603 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
605 res = gst_pad_query_default (pad, query);
609 res = gst_pad_query_default (pad, query);
613 gst_object_unref (avi);
618 static const GstEventMask *
619 gst_avi_demux_get_event_mask (GstPad * pad)
621 static const GstEventMask masks[] = {
622 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
631 gst_avi_demux_seek_streams (GstAviDemux * avi, guint64 offset, gboolean before)
633 GstAviStream *stream;
634 GstIndexEntry *entry;
636 gint64 val, min = offset;
638 for (i = 0; i < avi->num_streams; i++) {
639 stream = &avi->stream[i];
641 entry = gst_index_get_assoc_entry (avi->element_index, stream->index_id,
642 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
643 GST_ASSOCIATION_FLAG_NONE, GST_FORMAT_BYTES, offset);
647 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
648 GST_DEBUG_OBJECT (avi, "stream %d, previous entry at %"
649 G_GUINT64_FORMAT, i, val);
657 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
658 stream->current_entry = 0;
659 stream->current_total = 0;
663 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
664 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT,
667 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &val);
668 stream->current_total = val;
669 gst_index_entry_assoc_map (entry, GST_FORMAT_DEFAULT, &val);
670 stream->current_entry = val;
677 gst_avi_demux_index_entry_offset_search (GstAviIndexEntry * entry,
680 if (entry->offset < *offset)
682 else if (entry->offset > *offset)
688 gst_avi_demux_seek_streams_index (GstAviDemux * avi, guint64 offset,
691 GstAviStream *stream;
692 GstAviIndexEntry *entry;
694 gint64 val, min = offset;
697 for (i = 0; i < avi->num_streams; i++) {
698 stream = &avi->stream[i];
700 /* compensate for chunk header */
703 gst_util_array_binary_search (stream->index, stream->idx_n,
704 sizeof (GstAviIndexEntry),
705 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
706 before ? GST_SEARCH_MODE_BEFORE : GST_SEARCH_MODE_AFTER, &offset, NULL);
710 index = entry - stream->index;
714 val = stream->index[index].offset;
715 GST_DEBUG_OBJECT (avi,
716 "stream %d, previous entry at %" G_GUINT64_FORMAT, i, val);
724 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
725 stream->current_entry = 0;
726 stream->current_total = 0;
730 val = stream->index[index].offset - 8;
731 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT, i,
734 stream->current_total = stream->index[index].total;
735 stream->current_entry = index;
741 #define GST_AVI_SEEK_PUSH_DISPLACE (4 * GST_SECOND)
744 gst_avi_demux_handle_sink_event (GstPad * pad, GstEvent * event)
747 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
749 GST_DEBUG_OBJECT (avi,
750 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
752 switch (GST_EVENT_TYPE (event)) {
753 case GST_EVENT_NEWSEGMENT:
757 gint64 start, stop, time, offset = 0;
761 /* some debug output */
762 gst_segment_init (&segment, GST_FORMAT_UNDEFINED);
763 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
764 &start, &stop, &time);
765 gst_segment_set_newsegment_full (&segment, update, rate, arate, format,
767 GST_DEBUG_OBJECT (avi,
768 "received format %d newsegment %" GST_SEGMENT_FORMAT, format,
771 /* chain will send initial newsegment after pads have been added */
772 if (avi->state != GST_AVI_DEMUX_MOVI) {
773 GST_DEBUG_OBJECT (avi, "still starting, eating event");
777 /* we only expect a BYTE segment, e.g. following a seek */
778 if (format != GST_FORMAT_BYTES) {
779 GST_DEBUG_OBJECT (avi, "unsupported segment format, ignoring");
783 if (avi->have_index) {
784 GstAviIndexEntry *entry;
785 guint i = 0, index = 0, k = 0;
786 GstAviStream *stream;
788 /* compensate chunk header, stored index offset points after header */
790 /* find which stream we're on */
792 stream = &avi->stream[i];
794 /* find the index for start bytes offset */
795 entry = gst_util_array_binary_search (stream->index,
796 stream->idx_n, sizeof (GstAviIndexEntry),
797 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
798 GST_SEARCH_MODE_AFTER, &start, NULL);
802 index = entry - stream->index;
804 /* we are on the stream with a chunk start offset closest to start */
805 if (!offset || stream->index[index].offset < offset) {
806 offset = stream->index[index].offset;
809 /* exact match needs no further searching */
810 if (stream->index[index].offset == start)
812 } while (++i < avi->num_streams);
815 stream = &avi->stream[k];
817 /* so we have no idea what is to come, or where we are */
819 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
823 /* get the ts corresponding to start offset bytes for the stream */
824 gst_avi_demux_get_buffer_info (avi, stream, index,
825 (GstClockTime *) & time, NULL, NULL, NULL);
826 } else if (avi->element_index) {
827 GstIndexEntry *entry;
829 /* Let's check if we have an index entry for this position */
830 entry = gst_index_get_assoc_entry (avi->element_index, avi->index_id,
831 GST_INDEX_LOOKUP_AFTER, GST_ASSOCIATION_FLAG_NONE,
832 GST_FORMAT_BYTES, start);
834 /* we can not go where we have not yet been before ... */
836 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
840 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &time);
841 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &offset);
843 GST_WARNING_OBJECT (avi, "no index data, forcing EOS");
847 stop = GST_CLOCK_TIME_NONE;
849 /* set up segment and send downstream */
850 gst_segment_set_newsegment_full (&avi->segment, update, rate, arate,
851 GST_FORMAT_TIME, time, stop, time);
852 GST_DEBUG_OBJECT (avi, "Pushing newseg update %d, rate %g, "
853 "applied rate %g, format %d, start %" G_GINT64_FORMAT ", "
854 "stop %" G_GINT64_FORMAT, update, rate, arate, GST_FORMAT_TIME,
856 gst_avi_demux_push_event (avi,
857 gst_event_new_new_segment_full (update, rate, arate, GST_FORMAT_TIME,
860 GST_DEBUG_OBJECT (avi, "next chunk expected at %" G_GINT64_FORMAT, start);
862 /* adjust state for streaming thread accordingly */
864 gst_avi_demux_seek_streams_index (avi, offset, FALSE);
866 gst_avi_demux_seek_streams (avi, offset, FALSE);
868 /* set up streaming thread */
869 g_assert (offset >= start);
871 avi->todrop = offset - start;
874 gst_event_unref (event);
879 avi->have_eos = TRUE;
884 if (avi->state != GST_AVI_DEMUX_MOVI) {
885 gst_event_unref (event);
886 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
887 (NULL), ("got eos and didn't receive a complete header object"));
888 } else if (!gst_avi_demux_push_event (avi, event)) {
889 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
890 (NULL), ("got eos but no streams (yet)"));
894 case GST_EVENT_FLUSH_STOP:
898 gst_adapter_clear (avi->adapter);
899 avi->have_eos = FALSE;
900 for (i = 0; i < avi->num_streams; i++) {
901 avi->stream[i].last_flow = GST_FLOW_OK;
902 avi->stream[i].discont = TRUE;
904 /* fall through to default case so that the event gets passed downstream */
907 res = gst_pad_event_default (pad, event);
911 gst_object_unref (avi);
917 gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event)
920 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
922 GST_DEBUG_OBJECT (avi,
923 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
925 switch (GST_EVENT_TYPE (event)) {
927 if (!avi->streaming) {
928 res = gst_avi_demux_handle_seek (avi, pad, event);
930 res = gst_avi_demux_handle_seek_push (avi, pad, event);
932 gst_event_unref (event);
935 case GST_EVENT_NAVIGATION:
937 gst_event_unref (event);
940 res = gst_pad_event_default (pad, event);
944 gst_object_unref (avi);
949 /* streaming helper (push) */
952 * gst_avi_demux_peek_chunk_info:
954 * @tag: holder for tag
955 * @size: holder for tag size
957 * Peek next chunk info (tag and size)
959 * Returns: TRUE when one chunk info has been got
962 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
964 const guint8 *data = NULL;
966 if (gst_adapter_available (avi->adapter) < 8)
969 data = gst_adapter_peek (avi->adapter, 8);
970 *tag = GST_READ_UINT32_LE (data);
971 *size = GST_READ_UINT32_LE (data + 4);
977 * gst_avi_demux_peek_chunk:
979 * @tag: holder for tag
980 * @size: holder for tag size
982 * Peek enough data for one full chunk
984 * Returns: %TRUE when one chunk has been got
987 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
989 guint32 peek_size = 0;
992 if (!gst_avi_demux_peek_chunk_info (avi, tag, size))
995 /* size 0 -> empty data buffer would surprise most callers,
996 * large size -> do not bother trying to squeeze that into adapter,
997 * so we throw poor man's exception, which can be caught if caller really
998 * wants to handle 0 size chunk */
999 if (!(*size) || (*size) >= (1 << 30))
1002 peek_size = (*size + 1) & ~1;
1003 available = gst_adapter_available (avi->adapter);
1005 GST_DEBUG_OBJECT (avi,
1006 "Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
1007 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
1009 if (available < (8 + peek_size))
1017 GST_INFO_OBJECT (avi, "Failed to peek");
1022 GST_INFO_OBJECT (avi,
1023 "Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT, *size,
1024 GST_FOURCC_ARGS (*tag));
1025 /* chain should give up */
1026 avi->abort_buffering = TRUE;
1031 GST_INFO_OBJECT (avi, "need more %d < %" G_GUINT32_FORMAT,
1032 available, 8 + peek_size);
1040 * gst_avi_demux_parse_file_header:
1041 * @element: caller element (used for errors/debug).
1042 * @buf: input data to be used for parsing.
1044 * "Open" a RIFF/AVI file. The buffer should be at least 12
1045 * bytes long. Takes ownership of @buf.
1047 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
1048 * Throws an error, caller should error out (fatal).
1051 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
1056 stamp = gst_util_get_timestamp ();
1058 /* riff_parse posts an error */
1059 if (!gst_riff_parse_file_header (element, buf, &doctype))
1062 if (doctype != GST_RIFF_RIFF_AVI)
1065 stamp = gst_util_get_timestamp () - stamp;
1066 GST_DEBUG_OBJECT (element, "header parsing took %" GST_TIME_FORMAT,
1067 GST_TIME_ARGS (stamp));
1074 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
1075 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
1076 GST_FOURCC_ARGS (doctype)));
1082 * Read AVI file tag when streaming
1084 static GstFlowReturn
1085 gst_avi_demux_stream_init_push (GstAviDemux * avi)
1087 if (gst_adapter_available (avi->adapter) >= 12) {
1090 tmp = gst_adapter_take_buffer (avi->adapter, 12);
1092 GST_DEBUG ("Parsing avi header");
1093 if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), tmp)) {
1094 return GST_FLOW_ERROR;
1096 GST_DEBUG ("header ok");
1099 avi->state = GST_AVI_DEMUX_HEADER;
1107 static GstFlowReturn
1108 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
1111 GstBuffer *buf = NULL;
1113 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
1114 if (res != GST_FLOW_OK)
1116 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
1126 GST_DEBUG_OBJECT (avi, "error parsing file header");
1127 return GST_FLOW_ERROR;
1131 /* AVI header handling */
1133 * gst_avi_demux_parse_avih:
1134 * @avi: caller element (used for errors/debug).
1135 * @buf: input data to be used for parsing.
1136 * @avih: pointer to structure (filled in by function) containing
1137 * stream information (such as flags, number of streams, etc.).
1139 * Read 'avih' header. Discards buffer after use.
1141 * Returns: TRUE on success, FALSE otherwise. Throws an error if
1142 * the header is invalid. The caller should error out
1146 gst_avi_demux_parse_avih (GstAviDemux * avi,
1147 GstBuffer * buf, gst_riff_avih ** _avih)
1149 gst_riff_avih *avih;
1154 if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_avih))
1155 goto avih_too_small;
1157 avih = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1159 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1160 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
1161 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
1162 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
1163 avih->flags = GUINT32_FROM_LE (avih->flags);
1164 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
1165 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
1166 avih->streams = GUINT32_FROM_LE (avih->streams);
1167 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
1168 avih->width = GUINT32_FROM_LE (avih->width);
1169 avih->height = GUINT32_FROM_LE (avih->height);
1170 avih->scale = GUINT32_FROM_LE (avih->scale);
1171 avih->rate = GUINT32_FROM_LE (avih->rate);
1172 avih->start = GUINT32_FROM_LE (avih->start);
1173 avih->length = GUINT32_FROM_LE (avih->length);
1177 GST_INFO_OBJECT (avi, "avih tag found:");
1178 GST_INFO_OBJECT (avi, " us_frame %u", avih->us_frame);
1179 GST_INFO_OBJECT (avi, " max_bps %u", avih->max_bps);
1180 GST_INFO_OBJECT (avi, " pad_gran %u", avih->pad_gran);
1181 GST_INFO_OBJECT (avi, " flags 0x%08x", avih->flags);
1182 GST_INFO_OBJECT (avi, " tot_frames %u", avih->tot_frames);
1183 GST_INFO_OBJECT (avi, " init_frames %u", avih->init_frames);
1184 GST_INFO_OBJECT (avi, " streams %u", avih->streams);
1185 GST_INFO_OBJECT (avi, " bufsize %u", avih->bufsize);
1186 GST_INFO_OBJECT (avi, " width %u", avih->width);
1187 GST_INFO_OBJECT (avi, " height %u", avih->height);
1188 GST_INFO_OBJECT (avi, " scale %u", avih->scale);
1189 GST_INFO_OBJECT (avi, " rate %u", avih->rate);
1190 GST_INFO_OBJECT (avi, " start %u", avih->start);
1191 GST_INFO_OBJECT (avi, " length %u", avih->length);
1194 gst_buffer_unref (buf);
1196 if (avih->us_frame != 0 && avih->tot_frames != 0)
1198 (guint64) avih->us_frame * (guint64) avih->tot_frames * 1000;
1200 avi->duration = GST_CLOCK_TIME_NONE;
1202 GST_INFO_OBJECT (avi, " header duration %" GST_TIME_FORMAT,
1203 GST_TIME_ARGS (avi->duration));
1210 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No buffer"));
1215 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
1216 ("Too small avih (%d available, %d needed)",
1217 GST_BUFFER_SIZE (buf), (int) sizeof (gst_riff_avih)));
1218 gst_buffer_unref (buf);
1224 * gst_avi_demux_parse_superindex:
1225 * @avi: caller element (used for debugging/errors).
1226 * @buf: input data to use for parsing.
1227 * @locations: locations in the file (byte-offsets) that contain
1228 * the actual indexes (see get_avi_demux_parse_subindex()).
1229 * The array ends with GST_BUFFER_OFFSET_NONE.
1231 * Reads superindex (openDML-2 spec stuff) from the provided data.
1233 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
1234 * on error, but they are not fatal.
1237 gst_avi_demux_parse_superindex (GstAviDemux * avi,
1238 GstBuffer * buf, guint64 ** _indexes)
1248 size = buf ? GST_BUFFER_SIZE (buf) : 0;
1252 data = GST_BUFFER_DATA (buf);
1254 /* check type of index. The opendml2 specs state that
1255 * there should be 4 dwords per array entry. Type can be
1256 * either frame or field (and we don't care). */
1257 if (GST_READ_UINT16_LE (data) != 4 ||
1258 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
1259 GST_WARNING_OBJECT (avi,
1260 "Superindex for stream has unexpected "
1261 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1262 GST_READ_UINT16_LE (data), data[2], data[3]);
1263 bpe = GST_READ_UINT16_LE (data) * 4;
1265 num = GST_READ_UINT32_LE (&data[4]);
1267 GST_DEBUG_OBJECT (avi, "got %d indexes", num);
1269 indexes = g_new (guint64, num + 1);
1270 for (i = 0; i < num; i++) {
1271 if (size < 24 + bpe * (i + 1))
1273 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
1274 GST_DEBUG_OBJECT (avi, "index %d at %" G_GUINT64_FORMAT, i, indexes[i]);
1276 indexes[i] = GST_BUFFER_OFFSET_NONE;
1277 *_indexes = indexes;
1279 gst_buffer_unref (buf);
1286 GST_ERROR_OBJECT (avi,
1287 "Not enough data to parse superindex (%d available, 24 needed)", size);
1289 gst_buffer_unref (buf);
1294 /* add an entry to the index of a stream. @num should be an estimate of the
1295 * total amount of index entries for all streams and is used to dynamically
1296 * allocate memory for the index entries. */
1297 static inline gboolean
1298 gst_avi_demux_add_index (GstAviDemux * avi, GstAviStream * stream,
1299 guint num, GstAviIndexEntry * entry)
1301 /* ensure index memory */
1302 if (G_UNLIKELY (stream->idx_n >= stream->idx_max)) {
1303 guint idx_max = stream->idx_max;
1304 GstAviIndexEntry *new_idx;
1306 /* we need to make some more room */
1308 /* initial size guess, assume each stream has an equal amount of entries,
1309 * overshoot with at least 8K */
1310 idx_max = (num / avi->num_streams) + (8192 / sizeof (GstAviIndexEntry));
1312 idx_max += 8192 / sizeof (GstAviIndexEntry);
1313 GST_DEBUG_OBJECT (avi, "expanded index from %u to %u",
1314 stream->idx_max, idx_max);
1316 new_idx = g_try_renew (GstAviIndexEntry, stream->index, idx_max);
1317 /* out of memory, if this fails stream->index is untouched. */
1318 if (G_UNLIKELY (!new_idx))
1321 stream->index = new_idx;
1322 stream->idx_max = idx_max;
1325 /* update entry total and stream stats. The entry total can be converted to
1326 * the timestamp of the entry easily. */
1327 if (stream->strh->type == GST_RIFF_FCC_auds) {
1330 if (stream->is_vbr) {
1331 entry->total = stream->total_blocks;
1333 entry->total = stream->total_bytes;
1335 blockalign = stream->strf.auds->blockalign;
1337 stream->total_blocks += DIV_ROUND_UP (entry->size, blockalign);
1339 stream->total_blocks++;
1341 if (stream->is_vbr) {
1342 entry->total = stream->idx_n;
1344 entry->total = stream->total_bytes;
1347 stream->total_bytes += entry->size;
1348 if (ENTRY_IS_KEYFRAME (entry))
1349 stream->n_keyframes++;
1352 GST_LOG_OBJECT (avi,
1353 "Adding stream %u, index entry %d, kf %d, size %u "
1354 ", offset %" G_GUINT64_FORMAT ", total %" G_GUINT64_FORMAT, stream->num,
1355 stream->idx_n, ENTRY_IS_KEYFRAME (entry), entry->size, entry->offset,
1357 stream->index[stream->idx_n++] = *entry;
1362 /* given @entry_n in @stream, calculate info such as timestamps and
1363 * offsets for the entry. */
1365 gst_avi_demux_get_buffer_info (GstAviDemux * avi, GstAviStream * stream,
1366 guint entry_n, GstClockTime * timestamp, GstClockTime * ts_end,
1367 guint64 * offset, guint64 * offset_end)
1369 GstAviIndexEntry *entry;
1371 entry = &stream->index[entry_n];
1373 if (stream->is_vbr) {
1374 /* VBR stream next timestamp */
1375 if (stream->strh->type == GST_RIFF_FCC_auds) {
1378 avi_stream_convert_frames_to_time_unchecked (stream, entry->total);
1380 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1385 avi_stream_convert_frames_to_time_unchecked (stream, entry_n);
1387 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1390 } else if (stream->strh->type == GST_RIFF_FCC_auds) {
1391 /* constant rate stream */
1394 avi_stream_convert_bytes_to_time_unchecked (stream, entry->total);
1396 *ts_end = avi_stream_convert_bytes_to_time_unchecked (stream,
1397 entry->total + entry->size);
1399 if (stream->strh->type == GST_RIFF_FCC_vids) {
1400 /* video offsets are the frame number */
1404 *offset_end = entry_n + 1;
1406 /* no offsets for audio */
1414 /* collect and debug stats about the indexes for all streams.
1415 * This method is also responsible for filling in the stream duration
1416 * as measured by the amount of index entries.
1418 * Returns TRUE if the index is not empty, else FALSE */
1420 gst_avi_demux_do_index_stats (GstAviDemux * avi)
1422 guint total_idx = 0;
1424 #ifndef GST_DISABLE_GST_DEBUG
1425 guint total_max = 0;
1428 /* get stream stats now */
1429 for (i = 0; i < avi->num_streams; i++) {
1430 GstAviStream *stream;
1432 if (G_UNLIKELY (!(stream = &avi->stream[i])))
1434 if (G_UNLIKELY (!stream->strh))
1436 if (G_UNLIKELY (!stream->index || stream->idx_n == 0))
1439 /* we interested in the end_ts of the last entry, which is the total
1440 * duration of this stream */
1441 gst_avi_demux_get_buffer_info (avi, stream, stream->idx_n - 1,
1442 NULL, &stream->idx_duration, NULL, NULL);
1444 total_idx += stream->idx_n;
1445 #ifndef GST_DISABLE_GST_DEBUG
1446 total_max += stream->idx_max;
1448 GST_INFO_OBJECT (avi, "Stream %d, dur %" GST_TIME_FORMAT ", %6u entries, "
1449 "%5u keyframes, entry size = %2u, total size = %10u, allocated %10u",
1450 i, GST_TIME_ARGS (stream->idx_duration), stream->idx_n,
1451 stream->n_keyframes, (guint) sizeof (GstAviIndexEntry),
1452 (guint) (stream->idx_n * sizeof (GstAviIndexEntry)),
1453 (guint) (stream->idx_max * sizeof (GstAviIndexEntry)));
1455 total_idx *= sizeof (GstAviIndexEntry);
1456 #ifndef GST_DISABLE_GST_DEBUG
1457 total_max *= sizeof (GstAviIndexEntry);
1459 GST_INFO_OBJECT (avi, "%u bytes for index vs %u ideally, %u wasted",
1460 total_max, total_idx, total_max - total_idx);
1462 if (total_idx == 0) {
1463 GST_WARNING_OBJECT (avi, "Index is empty !");
1470 * gst_avi_demux_parse_subindex:
1472 * @buf: input data to use for parsing.
1473 * @stream: stream context.
1474 * @entries_list: a list (returned by the function) containing all the
1475 * indexes parsed in this specific subindex. The first
1476 * entry is also a pointer to allocated memory that needs
1477 * to be free´ed. May be NULL if no supported indexes were
1480 * Reads superindex (openDML-2 spec stuff) from the provided data.
1481 * The buffer should contain a GST_RIFF_TAG_ix?? chunk.
1483 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1484 * throw an error, caller should bail out asap.
1487 gst_avi_demux_parse_subindex (GstAviDemux * avi, GstAviStream * stream,
1499 size = GST_BUFFER_SIZE (buf);
1505 data = GST_BUFFER_DATA (buf);
1507 /* We don't support index-data yet */
1509 goto not_implemented;
1511 /* check type of index. The opendml2 specs state that
1512 * there should be 4 dwords per array entry. Type can be
1513 * either frame or field (and we don't care). */
1514 bpe = (data[2] & 0x01) ? 12 : 8;
1515 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1516 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1517 GST_WARNING_OBJECT (avi,
1518 "Superindex for stream %d has unexpected "
1519 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1520 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1521 bpe = GST_READ_UINT16_LE (data) * 4;
1523 num = GST_READ_UINT32_LE (&data[4]);
1524 baseoff = GST_READ_UINT64_LE (&data[12]);
1526 /* If there's nothing, just return ! */
1530 GST_INFO_OBJECT (avi, "Parsing subindex, nr_entries = %6d", num);
1532 for (i = 0; i < num; i++) {
1533 GstAviIndexEntry entry;
1535 if (size < 24 + bpe * (i + 1))
1538 /* fill in offset and size. offset contains the keyframe flag in the
1540 entry.offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1541 entry.size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1543 if (stream->strh->type == GST_RIFF_FCC_auds) {
1544 /* all audio frames are keyframes */
1545 ENTRY_SET_KEYFRAME (&entry);
1547 /* else read flags */
1548 entry.flags = (entry.size & 0x80000000) ? 0 : GST_AVI_KEYFRAME;
1550 entry.size &= ~0x80000000;
1553 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
1556 gst_buffer_unref (buf);
1563 GST_ERROR_OBJECT (avi,
1564 "Not enough data to parse subindex (%d available, 24 needed)", size);
1565 gst_buffer_unref (buf);
1566 return TRUE; /* continue */
1570 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1571 ("Subindex-is-data is not implemented"));
1572 gst_buffer_unref (buf);
1577 GST_DEBUG_OBJECT (avi, "the index is empty");
1578 gst_buffer_unref (buf);
1583 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1584 ("Cannot allocate memory for %u*%u=%u bytes",
1585 (guint) sizeof (GstAviIndexEntry), num,
1586 (guint) sizeof (GstAviIndexEntry) * num));
1587 gst_buffer_unref (buf);
1593 * Create and push a flushing seek event upstream
1596 perform_seek_to_offset (GstAviDemux * demux, guint64 offset)
1601 GST_DEBUG_OBJECT (demux, "Seeking to %" G_GUINT64_FORMAT, offset);
1604 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1605 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1606 GST_SEEK_TYPE_NONE, -1);
1608 res = gst_pad_push_event (demux->sinkpad, event);
1611 demux->offset = offset;
1616 * Read AVI index when streaming
1619 gst_avi_demux_read_subindexes_push (GstAviDemux * avi)
1621 guint32 tag = 0, size;
1622 GstBuffer *buf = NULL;
1625 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1627 if (avi->odml_subidxs[avi->odml_subidx] != avi->offset)
1630 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1633 /* this is the ODML chunk we expect */
1634 odml_stream = avi->odml_stream;
1636 if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + odml_stream / 10,
1637 '0' + odml_stream % 10)) &&
1638 (tag != GST_MAKE_FOURCC ('0' + odml_stream / 10,
1639 '0' + odml_stream % 10, 'i', 'x'))) {
1640 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1641 GST_FOURCC_ARGS (tag));
1645 avi->offset += 8 + GST_ROUND_UP_2 (size);
1646 /* flush chunk header so we get just the 'size' payload data */
1647 gst_adapter_flush (avi->adapter, 8);
1648 buf = gst_adapter_take_buffer (avi->adapter, size);
1650 if (!gst_avi_demux_parse_subindex (avi, &avi->stream[odml_stream], buf))
1653 /* we parsed the index, go to next subindex */
1656 if (avi->odml_subidxs[avi->odml_subidx] == GST_BUFFER_OFFSET_NONE) {
1657 /* we reached the end of the indexes for this stream, move to the next
1658 * stream to handle the first index */
1660 avi->odml_subidx = 0;
1662 if (avi->odml_stream < avi->num_streams) {
1663 /* there are more indexes */
1664 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
1666 /* we're done, get stream stats now */
1667 avi->have_index = gst_avi_demux_do_index_stats (avi);
1673 /* seek to next index */
1674 return perform_seek_to_offset (avi, avi->odml_subidxs[avi->odml_subidx]);
1681 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi)
1687 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1689 for (n = 0; n < avi->num_streams; n++) {
1690 GstAviStream *stream = &avi->stream[n];
1692 if (stream->indexes == NULL)
1695 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1696 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi), avi->sinkpad,
1697 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1699 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1700 '0' + stream->num % 10)) &&
1701 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1702 '0' + stream->num % 10, 'i', 'x'))) {
1703 /* Some ODML files (created by god knows what muxer) have a ##ix format
1704 * instead of the 'official' ix##. They are still valid though. */
1705 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1706 GST_FOURCC_ARGS (tag));
1707 gst_buffer_unref (buf);
1711 if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1715 g_free (stream->indexes);
1716 stream->indexes = NULL;
1718 /* get stream stats now */
1719 avi->have_index = gst_avi_demux_do_index_stats (avi);
1723 * gst_avi_demux_riff_parse_vprp:
1724 * @element: caller element (used for debugging/error).
1725 * @buf: input data to be used for parsing, stripped from header.
1726 * @vprp: a pointer (returned by this function) to a filled-in vprp
1727 * structure. Caller should free it.
1729 * Parses a video stream´s vprp. This function takes ownership of @buf.
1731 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1732 * should be skipped on error, but it is not fatal.
1735 gst_avi_demux_riff_parse_vprp (GstElement * element,
1736 GstBuffer * buf, gst_riff_vprp ** _vprp)
1738 gst_riff_vprp *vprp;
1741 g_return_val_if_fail (buf != NULL, FALSE);
1742 g_return_val_if_fail (_vprp != NULL, FALSE);
1744 if (GST_BUFFER_SIZE (buf) < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1747 vprp = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1749 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1750 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1751 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1752 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1753 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1754 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1755 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1756 vprp->width = GUINT32_FROM_LE (vprp->width);
1757 vprp->height = GUINT32_FROM_LE (vprp->height);
1758 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1762 /* calculate fields based on size */
1763 k = (GST_BUFFER_SIZE (buf) - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) /
1765 if (vprp->fields > k) {
1766 GST_WARNING_OBJECT (element,
1767 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1770 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1771 GST_WARNING_OBJECT (element,
1772 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1773 GST_RIFF_VPRP_VIDEO_FIELDS);
1774 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1776 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1777 for (k = 0; k < vprp->fields; k++) {
1778 gst_riff_vprp_video_field_desc *fd;
1780 fd = &vprp->field_info[k];
1781 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1782 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1783 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1784 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1785 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1786 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1787 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1788 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1793 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1794 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1795 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1796 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1797 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1798 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1799 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1800 vprp->aspect & 0xffff);
1801 GST_INFO_OBJECT (element, " width %d", vprp->width);
1802 GST_INFO_OBJECT (element, " height %d", vprp->height);
1803 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1804 for (k = 0; k < vprp->fields; k++) {
1805 gst_riff_vprp_video_field_desc *fd;
1807 fd = &(vprp->field_info[k]);
1808 GST_INFO_OBJECT (element, " field %u description:", k);
1809 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1810 fd->compressed_bm_height);
1811 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1812 fd->compressed_bm_width);
1813 GST_INFO_OBJECT (element, " valid_bm_height %d",
1814 fd->valid_bm_height);
1815 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1816 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1817 fd->valid_bm_x_offset);
1818 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1819 fd->valid_bm_y_offset);
1820 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1821 fd->video_x_t_offset);
1822 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1825 gst_buffer_unref (buf);
1834 GST_ERROR_OBJECT (element,
1835 "Too small vprp (%d available, at least %d needed)",
1836 GST_BUFFER_SIZE (buf),
1837 (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1838 gst_buffer_unref (buf);
1844 gst_avi_demux_expose_streams (GstAviDemux * avi, gboolean force)
1848 GST_DEBUG_OBJECT (avi, "force : %d", force);
1850 for (i = 0; i < avi->num_streams; i++) {
1851 GstAviStream *stream = &avi->stream[i];
1853 if (force || stream->idx_n != 0) {
1854 GST_LOG_OBJECT (avi, "Added pad %s with caps %" GST_PTR_FORMAT,
1855 GST_PAD_NAME (stream->pad), GST_PAD_CAPS (stream->pad));
1856 gst_element_add_pad ((GstElement *) avi, stream->pad);
1857 stream->exposed = TRUE;
1858 if (avi->main_stream == -1)
1859 avi->main_stream = i;
1861 GST_WARNING_OBJECT (avi, "Stream #%d doesn't have any entry, removing it",
1863 gst_avi_demux_reset_stream (avi, stream);
1869 * gst_avi_demux_parse_stream:
1870 * @avi: calling element (used for debugging/errors).
1871 * @buf: input buffer used to parse the stream.
1873 * Parses all subchunks in a strl chunk (which defines a single
1874 * stream). Discards the buffer after use. This function will
1875 * increment the stream counter internally.
1877 * Returns: whether the stream was identified successfully.
1878 * Errors are not fatal. It does indicate the stream
1882 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
1884 GstAviStream *stream;
1885 GstElementClass *klass;
1886 GstPadTemplate *templ;
1887 GstBuffer *sub = NULL;
1890 gchar *codec_name = NULL, *padname = NULL;
1891 const gchar *tag_name;
1892 GstCaps *caps = NULL;
1894 GstElement *element;
1895 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
1896 gst_riff_vprp *vprp = NULL;
1898 element = GST_ELEMENT_CAST (avi);
1900 GST_DEBUG_OBJECT (avi, "Parsing stream");
1902 if (avi->num_streams >= GST_AVI_DEMUX_MAX_STREAMS) {
1903 GST_WARNING_OBJECT (avi,
1904 "maximum no of streams (%d) exceeded, ignoring stream",
1905 GST_AVI_DEMUX_MAX_STREAMS);
1906 gst_buffer_unref (buf);
1907 /* not a fatal error, let's say */
1911 stream = &avi->stream[avi->num_streams];
1913 /* initial settings */
1914 stream->idx_duration = GST_CLOCK_TIME_NONE;
1915 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1916 stream->duration = GST_CLOCK_TIME_NONE;
1918 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
1919 /* sub can be NULL if the chunk is empty */
1921 GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
1922 GST_FOURCC_ARGS (tag));
1926 case GST_RIFF_TAG_strh:
1928 gst_riff_strh *strh;
1931 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
1934 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
1935 /* ownership given away */
1937 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
1941 strh = stream->strh;
1942 /* sanity check; stream header frame rate matches global header
1944 if (stream->strh->type == GST_RIFF_FCC_vids) {
1946 GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
1948 s_dur = gst_util_uint64_scale (GST_SECOND, strh->scale, strh->rate);
1949 GST_DEBUG_OBJECT (avi, "verifying stream framerate %d/%d, "
1950 "frame duration = %d ms", strh->rate, strh->scale,
1951 (gint) (s_dur / GST_MSECOND));
1952 if (h_dur > (10 * GST_MSECOND) && (s_dur > 10 * h_dur)) {
1953 strh->rate = GST_SECOND / GST_USECOND;
1954 strh->scale = h_dur / GST_USECOND;
1955 GST_DEBUG_OBJECT (avi, "correcting stream framerate to %d/%d",
1956 strh->rate, strh->scale);
1959 /* determine duration as indicated by header */
1960 stream->hdr_duration = gst_util_uint64_scale ((guint64) strh->length *
1961 strh->scale, GST_SECOND, (guint64) strh->rate);
1962 GST_INFO ("Stream duration according to header: %" GST_TIME_FORMAT,
1963 GST_TIME_ARGS (stream->hdr_duration));
1964 if (stream->hdr_duration == 0)
1965 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1970 case GST_RIFF_TAG_strf:
1972 gboolean res = FALSE;
1975 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
1979 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
1982 switch (stream->strh->type) {
1983 case GST_RIFF_FCC_vids:
1984 stream->is_vbr = TRUE;
1985 res = gst_riff_parse_strf_vids (element, sub,
1986 &stream->strf.vids, &stream->extradata);
1988 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
1990 case GST_RIFF_FCC_auds:
1992 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
1993 &stream->extradata);
1994 stream->is_vbr = (stream->strh->samplesize == 0)
1995 && stream->strh->scale > 1
1996 && stream->strf.auds->blockalign != 1;
1998 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
1999 stream->is_vbr, res);
2000 /* we need these or we have no way to come up with timestamps */
2001 if ((!stream->is_vbr && !stream->strf.auds->av_bps) ||
2002 (stream->is_vbr && (!stream->strh->scale ||
2003 !stream->strh->rate))) {
2004 GST_WARNING_OBJECT (element,
2005 "invalid audio header, ignoring stream");
2009 case GST_RIFF_FCC_iavs:
2010 stream->is_vbr = TRUE;
2011 res = gst_riff_parse_strf_iavs (element, sub,
2012 &stream->strf.iavs, &stream->extradata);
2014 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
2016 case GST_RIFF_FCC_txts:
2017 /* nothing to parse here */
2018 stream->is_vbr = (stream->strh->samplesize == 0)
2019 && (stream->strh->scale > 1);
2023 GST_ERROR_OBJECT (avi,
2024 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
2025 GST_FOURCC_ARGS (stream->strh->type));
2029 gst_buffer_unref (sub);
2037 case GST_RIFF_TAG_vprp:
2040 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
2044 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
2048 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
2052 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
2053 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
2054 /* not considered fatal */
2062 case GST_RIFF_TAG_strd:
2063 if (stream->initdata)
2064 gst_buffer_unref (stream->initdata);
2065 stream->initdata = sub;
2068 case GST_RIFF_TAG_strn:
2069 g_free (stream->name);
2072 g_strndup ((gchar *) GST_BUFFER_DATA (sub),
2073 (gsize) GST_BUFFER_SIZE (sub));
2074 gst_buffer_unref (sub);
2077 stream->name = g_strdup ("");
2079 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
2082 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
2083 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
2084 '0' + avi->num_streams % 10)) {
2085 g_free (stream->indexes);
2086 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
2087 stream->superindex = TRUE;
2091 GST_WARNING_OBJECT (avi,
2092 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
2093 GST_FOURCC_ARGS (tag));
2095 case GST_RIFF_TAG_JUNQ:
2096 case GST_RIFF_TAG_JUNK:
2100 gst_buffer_unref (sub);
2106 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
2111 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
2115 /* get class to figure out the template */
2116 klass = GST_ELEMENT_GET_CLASS (avi);
2118 /* we now have all info, let´s set up a pad and a caps and be done */
2119 /* create stream name + pad */
2120 switch (stream->strh->type) {
2121 case GST_RIFF_FCC_vids:{
2124 fourcc = (stream->strf.vids->compression) ?
2125 stream->strf.vids->compression : stream->strh->fcc_handler;
2126 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
2127 templ = gst_element_class_get_pad_template (klass, "video_%02d");
2128 caps = gst_riff_create_video_caps (fourcc, stream->strh,
2129 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
2131 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2132 GST_TYPE_FOURCC, fourcc, NULL);
2133 } else if (got_vprp && vprp) {
2134 guint32 aspect_n, aspect_d;
2137 aspect_n = vprp->aspect >> 16;
2138 aspect_d = vprp->aspect & 0xffff;
2139 /* calculate the pixel aspect ratio using w/h and aspect ratio */
2140 n = aspect_n * stream->strf.vids->height;
2141 d = aspect_d * stream->strf.vids->width;
2143 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
2145 /* very local, not needed elsewhere */
2149 tag_name = GST_TAG_VIDEO_CODEC;
2150 avi->num_v_streams++;
2153 case GST_RIFF_FCC_auds:{
2154 padname = g_strdup_printf ("audio_%02d", avi->num_a_streams);
2155 templ = gst_element_class_get_pad_template (klass, "audio_%02d");
2156 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
2157 stream->strh, stream->strf.auds, stream->extradata,
2158 stream->initdata, &codec_name);
2160 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
2161 G_TYPE_INT, stream->strf.auds->format, NULL);
2163 tag_name = GST_TAG_AUDIO_CODEC;
2164 avi->num_a_streams++;
2167 case GST_RIFF_FCC_iavs:{
2168 guint32 fourcc = stream->strh->fcc_handler;
2170 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
2171 templ = gst_element_class_get_pad_template (klass, "video_%02d");
2172 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
2173 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
2175 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2176 GST_TYPE_FOURCC, fourcc, NULL);
2178 tag_name = GST_TAG_VIDEO_CODEC;
2179 avi->num_v_streams++;
2182 case GST_RIFF_FCC_txts:{
2183 padname = g_strdup_printf ("subtitle_%02d", avi->num_t_streams);
2184 templ = gst_element_class_get_pad_template (klass, "subtitle_%02d");
2185 caps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
2187 avi->num_t_streams++;
2191 g_assert_not_reached ();
2194 /* no caps means no stream */
2196 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
2200 GST_DEBUG_OBJECT (element, "codec-name=%s",
2201 (codec_name ? codec_name : "NULL"));
2202 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
2204 /* set proper settings and add it */
2206 gst_object_unref (stream->pad);
2207 pad = stream->pad = gst_pad_new_from_template (templ, padname);
2210 gst_pad_use_fixed_caps (pad);
2212 gst_pad_set_formats_function (pad,
2213 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
2214 gst_pad_set_event_mask_function (pad,
2215 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
2217 gst_pad_set_event_function (pad,
2218 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
2219 gst_pad_set_query_type_function (pad,
2220 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_query_types));
2221 gst_pad_set_query_function (pad,
2222 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
2224 gst_pad_set_convert_function (pad,
2225 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
2228 if (avi->element_index)
2229 gst_index_get_writer_id (avi->element_index, GST_OBJECT_CAST (stream->pad),
2232 stream->num = avi->num_streams;
2234 stream->start_entry = 0;
2235 stream->step_entry = 0;
2236 stream->stop_entry = 0;
2238 stream->current_entry = -1;
2239 stream->current_total = 0;
2241 stream->last_flow = GST_FLOW_OK;
2242 stream->discont = TRUE;
2244 stream->total_bytes = 0;
2245 stream->total_blocks = 0;
2246 stream->n_keyframes = 0;
2249 stream->idx_max = 0;
2251 gst_pad_set_element_private (pad, stream);
2254 gst_pad_set_caps (pad, caps);
2255 gst_pad_set_active (pad, TRUE);
2256 gst_caps_unref (caps);
2260 if (!stream->taglist)
2261 stream->taglist = gst_tag_list_new ();
2263 avi->got_tags = TRUE;
2265 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
2267 g_free (codec_name);
2270 gst_buffer_unref (buf);
2277 /* unref any mem that may be in use */
2279 gst_buffer_unref (buf);
2281 gst_buffer_unref (sub);
2283 g_free (codec_name);
2284 gst_avi_demux_reset_stream (avi, stream);
2291 * gst_avi_demux_parse_odml:
2292 * @avi: calling element (used for debug/error).
2293 * @buf: input buffer to be used for parsing.
2295 * Read an openDML-2.0 extension header. Fills in the frame number
2296 * in the avi demuxer object when reading succeeds.
2299 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
2303 GstBuffer *sub = NULL;
2305 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
2308 case GST_RIFF_TAG_dmlh:{
2309 gst_riff_dmlh dmlh, *_dmlh;
2312 /* sub == NULL is possible and means an empty buffer */
2313 size = sub ? GST_BUFFER_SIZE (sub) : 0;
2316 if (size < sizeof (gst_riff_dmlh)) {
2317 GST_ERROR_OBJECT (avi,
2318 "DMLH entry is too small (%d bytes, %d needed)",
2319 size, (int) sizeof (gst_riff_dmlh));
2322 _dmlh = (gst_riff_dmlh *) GST_BUFFER_DATA (sub);
2323 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
2325 GST_INFO_OBJECT (avi, "dmlh tag found: totalframes: %u",
2328 avi->avih->tot_frames = dmlh.totalframes;
2333 GST_WARNING_OBJECT (avi,
2334 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
2335 GST_FOURCC_ARGS (tag));
2337 case GST_RIFF_TAG_JUNQ:
2338 case GST_RIFF_TAG_JUNK:
2340 /* skip and move to next chunk */
2342 gst_buffer_unref (sub);
2349 gst_buffer_unref (buf);
2354 gst_avi_demux_index_last (GstAviDemux * avi, GstAviStream * stream)
2356 return stream->idx_n - 1;
2359 /* find a previous entry in the index with the given flags */
2361 gst_avi_demux_index_prev (GstAviDemux * avi, GstAviStream * stream,
2362 guint last, gboolean keyframe)
2364 GstAviIndexEntry *entry;
2367 for (i = last; i > 0; i--) {
2368 entry = &stream->index[i - 1];
2369 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2377 gst_avi_demux_index_next (GstAviDemux * avi, GstAviStream * stream,
2378 guint last, gboolean keyframe)
2380 GstAviIndexEntry *entry;
2383 for (i = last + 1; i < stream->idx_n; i++) {
2384 entry = &stream->index[i];
2385 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2389 return stream->idx_n - 1;
2393 gst_avi_demux_index_entry_search (GstAviIndexEntry * entry, guint64 * total)
2395 if (entry->total < *total)
2397 else if (entry->total > *total)
2403 * gst_avi_demux_index_for_time:
2405 * @stream: the stream
2406 * @time: a time position
2408 * Finds the index entry which time is less or equal than the requested time.
2409 * Try to avoid binary search when we can convert the time to an index
2410 * position directly (for example for video frames with a fixed duration).
2412 * Returns: the found position in the index.
2415 gst_avi_demux_index_for_time (GstAviDemux * avi,
2416 GstAviStream * stream, guint64 time)
2421 GST_LOG_OBJECT (avi, "search time:%" GST_TIME_FORMAT, GST_TIME_ARGS (time));
2423 /* easy (and common) cases */
2424 if (time == 0 || stream->idx_n == 0)
2426 if (time >= stream->idx_duration)
2427 return stream->idx_n - 1;
2429 /* figure out where we need to go. For that we convert the time to an
2430 * index entry or we convert it to a total and then do a binary search. */
2431 if (stream->is_vbr) {
2432 /* VBR stream next timestamp */
2433 if (stream->strh->type == GST_RIFF_FCC_auds) {
2434 total = avi_stream_convert_time_to_frames_unchecked (stream, time);
2436 index = avi_stream_convert_time_to_frames_unchecked (stream, time);
2439 /* constant rate stream */
2440 total = avi_stream_convert_time_to_bytes_unchecked (stream, time);
2444 GstAviIndexEntry *entry;
2446 /* no index, find index with binary search on total */
2447 GST_LOG_OBJECT (avi, "binary search for entry with total %"
2448 G_GUINT64_FORMAT, total);
2450 entry = gst_util_array_binary_search (stream->index,
2451 stream->idx_n, sizeof (GstAviIndexEntry),
2452 (GCompareDataFunc) gst_avi_demux_index_entry_search,
2453 GST_SEARCH_MODE_BEFORE, &total, NULL);
2455 if (entry == NULL) {
2456 GST_LOG_OBJECT (avi, "not found, assume index 0");
2459 index = entry - stream->index;
2460 GST_LOG_OBJECT (avi, "found at %u", index);
2463 GST_LOG_OBJECT (avi, "converted time to index %u", index);
2469 static inline GstAviStream *
2470 gst_avi_demux_stream_for_id (GstAviDemux * avi, guint32 id)
2473 GstAviStream *stream;
2475 /* get the stream for this entry */
2476 stream_nr = CHUNKID_TO_STREAMNR (id);
2477 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2478 GST_WARNING_OBJECT (avi, "invalid stream nr %d", stream_nr);
2481 stream = &avi->stream[stream_nr];
2482 if (G_UNLIKELY (!stream->strh)) {
2483 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2490 * gst_avi_demux_parse_index:
2491 * @avi: calling element (used for debugging/errors).
2492 * @buf: buffer containing the full index.
2494 * Read index entries from the provided buffer.
2495 * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
2498 gst_avi_demux_parse_index (GstAviDemux * avi, GstBuffer * buf)
2504 gst_riff_index_entry *index;
2506 GstAviStream *stream;
2507 GstAviIndexEntry entry;
2513 data = GST_BUFFER_DATA (buf);
2514 size = GST_BUFFER_SIZE (buf);
2516 stamp = gst_util_get_timestamp ();
2518 /* see how many items in the index */
2519 num = size / sizeof (gst_riff_index_entry);
2523 GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2525 index = (gst_riff_index_entry *) data;
2526 pos_before = avi->offset;
2528 /* figure out if the index is 0 based or relative to the MOVI start */
2529 entry.offset = GST_READ_UINT32_LE (&index[0].offset);
2530 if (entry.offset < avi->offset) {
2531 avi->index_offset = avi->offset + 8;
2532 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2534 avi->index_offset = 0;
2535 GST_DEBUG ("index is 0 based");
2538 for (i = 0, n = 0; i < num; i++) {
2539 id = GST_READ_UINT32_LE (&index[i].id);
2540 entry.offset = GST_READ_UINT32_LE (&index[i].offset);
2542 /* some sanity checks */
2543 if (G_UNLIKELY (id == GST_RIFF_rec || id == 0 ||
2544 (entry.offset == 0 && n > 0)))
2547 /* get the stream for this entry */
2548 stream = gst_avi_demux_stream_for_id (avi, id);
2549 if (G_UNLIKELY (!stream))
2552 /* handle offset and size */
2553 entry.offset += avi->index_offset + 8;
2554 entry.size = GST_READ_UINT32_LE (&index[i].size);
2557 if (stream->strh->type == GST_RIFF_FCC_auds) {
2558 /* all audio frames are keyframes */
2559 ENTRY_SET_KEYFRAME (&entry);
2562 /* else read flags */
2563 flags = GST_READ_UINT32_LE (&index[i].flags);
2564 if (flags & GST_RIFF_IF_KEYFRAME) {
2565 ENTRY_SET_KEYFRAME (&entry);
2567 ENTRY_UNSET_KEYFRAME (&entry);
2572 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2577 gst_buffer_unref (buf);
2579 /* get stream stats now */
2580 avi->have_index = gst_avi_demux_do_index_stats (avi);
2582 stamp = gst_util_get_timestamp () - stamp;
2583 GST_DEBUG_OBJECT (avi, "index parsing took %" GST_TIME_FORMAT,
2584 GST_TIME_ARGS (stamp));
2591 GST_DEBUG_OBJECT (avi, "empty index");
2592 gst_buffer_unref (buf);
2597 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2598 ("Cannot allocate memory for %u*%u=%u bytes",
2599 (guint) sizeof (GstAviIndexEntry), num,
2600 (guint) sizeof (GstAviIndexEntry) * num));
2601 gst_buffer_unref (buf);
2607 * gst_avi_demux_stream_index:
2608 * @avi: avi demuxer object.
2610 * Seeks to index and reads it.
2613 gst_avi_demux_stream_index (GstAviDemux * avi)
2616 guint64 offset = avi->offset;
2621 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2623 /* get chunk information */
2624 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2625 if (res != GST_FLOW_OK)
2627 else if (GST_BUFFER_SIZE (buf) < 8)
2630 /* check tag first before blindy trying to read 'size' bytes */
2631 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2632 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2633 if (tag == GST_RIFF_TAG_LIST) {
2634 /* this is the movi tag */
2635 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2636 (8 + GST_ROUND_UP_2 (size)));
2637 offset += 8 + GST_ROUND_UP_2 (size);
2638 gst_buffer_unref (buf);
2639 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2640 if (res != GST_FLOW_OK)
2642 else if (GST_BUFFER_SIZE (buf) < 8)
2644 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2645 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2648 if (tag != GST_RIFF_TAG_idx1)
2653 gst_buffer_unref (buf);
2655 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2657 /* read chunk, advance offset */
2658 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2659 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2662 GST_DEBUG ("will parse index chunk size %u for tag %"
2663 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2665 gst_avi_demux_parse_index (avi, buf);
2667 #ifndef GST_DISABLE_GST_DEBUG
2668 /* debug our indexes */
2671 GstAviStream *stream;
2673 for (i = 0; i < avi->num_streams; i++) {
2674 stream = &avi->stream[i];
2675 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2676 i, stream->idx_n, stream->total_bytes);
2685 GST_DEBUG_OBJECT (avi,
2686 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2691 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2692 gst_buffer_unref (buf);
2697 GST_WARNING_OBJECT (avi,
2698 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2699 GST_FOURCC_ARGS (tag));
2700 gst_buffer_unref (buf);
2705 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2706 gst_buffer_unref (buf);
2712 * gst_avi_demux_stream_index_push:
2713 * @avi: avi demuxer object.
2718 gst_avi_demux_stream_index_push (GstAviDemux * avi)
2720 guint64 offset = avi->idx1_offset;
2725 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2727 /* get chunk information */
2728 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
2731 /* check tag first before blindly trying to read 'size' bytes */
2732 if (tag == GST_RIFF_TAG_LIST) {
2733 /* this is the movi tag */
2734 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2735 (8 + GST_ROUND_UP_2 (size)));
2736 avi->idx1_offset = offset + 8 + GST_ROUND_UP_2 (size);
2737 /* issue seek to allow chain function to handle it and return! */
2738 perform_seek_to_offset (avi, avi->idx1_offset);
2742 if (tag != GST_RIFF_TAG_idx1)
2745 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2747 /* flush chunk header */
2748 gst_adapter_flush (avi->adapter, 8);
2749 /* read chunk payload */
2750 buf = gst_adapter_take_buffer (avi->adapter, size);
2753 /* advance offset */
2754 offset += 8 + GST_ROUND_UP_2 (size);
2756 GST_DEBUG ("will parse index chunk size %u for tag %"
2757 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2759 avi->offset = avi->first_movi_offset - 8;
2760 gst_avi_demux_parse_index (avi, buf);
2762 #ifndef GST_DISABLE_GST_DEBUG
2763 /* debug our indexes */
2766 GstAviStream *stream;
2768 for (i = 0; i < avi->num_streams; i++) {
2769 stream = &avi->stream[i];
2770 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2771 i, stream->idx_n, stream->total_bytes);
2780 GST_DEBUG_OBJECT (avi,
2781 "taking data from adapter failed: pos=%" G_GUINT64_FORMAT " size=%u",
2787 GST_WARNING_OBJECT (avi,
2788 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2789 GST_FOURCC_ARGS (tag));
2795 * gst_avi_demux_peek_tag:
2797 * Returns the tag and size of the next chunk
2799 static GstFlowReturn
2800 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2803 GstFlowReturn res = GST_FLOW_OK;
2804 GstBuffer *buf = NULL;
2808 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2809 if (res != GST_FLOW_OK)
2812 bufsize = GST_BUFFER_SIZE (buf);
2816 bufdata = GST_BUFFER_DATA (buf);
2818 *tag = GST_READ_UINT32_LE (bufdata);
2819 *size = GST_READ_UINT32_LE (bufdata + 4);
2821 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2822 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2823 *size, offset + 8, offset + 8 + (gint64) * size);
2826 gst_buffer_unref (buf);
2833 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2838 GST_DEBUG_OBJECT (avi, "got %d bytes which is <> 8 bytes", bufsize);
2839 res = GST_FLOW_ERROR;
2845 * gst_avi_demux_next_data_buffer:
2847 * Returns the offset and size of the next buffer
2848 * Position is the position of the buffer (after tag and size)
2850 static GstFlowReturn
2851 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2852 guint32 * tag, guint * size)
2854 guint64 off = *offset;
2859 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2860 if (res != GST_FLOW_OK)
2862 if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
2863 off += 8 + 4; /* skip tag + size + subtag */
2875 * gst_avi_demux_stream_scan:
2876 * @avi: calling element (used for debugging/errors).
2878 * Scan the file for all chunks to "create" a new index.
2882 gst_avi_demux_stream_scan (GstAviDemux * avi)
2885 GstAviStream *stream;
2894 * - implement non-seekable source support.
2896 GST_DEBUG_OBJECT (avi, "Creating index");
2898 /* get the size of the file */
2899 format = GST_FORMAT_BYTES;
2900 if (!gst_pad_query_peer_duration (avi->sinkpad, &format, &tmplength))
2904 /* guess the total amount of entries we expect */
2908 GstAviIndexEntry entry;
2911 /* start reading data buffers to find the id and offset */
2912 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
2913 if (G_UNLIKELY (res != GST_FLOW_OK))
2917 stream = gst_avi_demux_stream_for_id (avi, tag);
2918 if (G_UNLIKELY (!stream))
2921 /* we can't figure out the keyframes, assume they all are */
2922 entry.flags = GST_AVI_KEYFRAME;
2926 /* and add to the index of this stream */
2927 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2931 /* update position */
2932 pos += GST_ROUND_UP_2 (size);
2933 if (G_UNLIKELY (pos > length)) {
2934 GST_WARNING_OBJECT (avi,
2935 "Stopping index lookup since we are further than EOF");
2941 avi->have_index = gst_avi_demux_do_index_stats (avi);
2948 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2949 ("Cannot allocate memory for %u*%u=%u bytes",
2950 (guint) sizeof (GstAviIndexEntry), num,
2951 (guint) sizeof (GstAviIndexEntry) * num));
2957 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
2961 GstAviStream *stream;
2963 total = GST_CLOCK_TIME_NONE;
2965 /* all streams start at a timestamp 0 */
2966 for (i = 0; i < avi->num_streams; i++) {
2967 GstClockTime duration, hduration;
2968 gst_riff_strh *strh;
2970 stream = &avi->stream[i];
2971 if (G_UNLIKELY (!stream || !stream->idx_n || !(strh = stream->strh)))
2974 /* get header duration for the stream */
2975 hduration = stream->hdr_duration;
2976 /* index duration calculated during parsing */
2977 duration = stream->idx_duration;
2979 /* now pick a good duration */
2980 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2981 /* index gave valid duration, use that */
2982 GST_INFO ("Stream %p duration according to index: %" GST_TIME_FORMAT,
2983 stream, GST_TIME_ARGS (duration));
2985 /* fall back to header info to calculate a duration */
2986 duration = hduration;
2988 GST_INFO ("Setting duration of stream #%d to %" GST_TIME_FORMAT,
2989 i, GST_TIME_ARGS (duration));
2990 /* set duration for the stream */
2991 stream->duration = duration;
2993 /* find total duration */
2994 if (total == GST_CLOCK_TIME_NONE ||
2995 (GST_CLOCK_TIME_IS_VALID (duration) && duration > total))
2999 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
3000 /* now update the duration for those streams where we had none */
3001 for (i = 0; i < avi->num_streams; i++) {
3002 stream = &avi->stream[i];
3004 if (!GST_CLOCK_TIME_IS_VALID (stream->duration)
3005 || stream->duration == 0) {
3006 stream->duration = total;
3008 GST_INFO ("Stream %p duration according to total: %" GST_TIME_FORMAT,
3009 stream, GST_TIME_ARGS (total));
3014 /* and set the total duration in the segment. */
3015 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
3016 GST_TIME_ARGS (total));
3018 gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
3021 /* returns FALSE if there are no pads to deliver event to,
3022 * otherwise TRUE (whatever the outcome of event sending),
3023 * takes ownership of the event. */
3025 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
3027 gboolean result = FALSE;
3030 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
3031 GST_EVENT_TYPE_NAME (event), avi->num_streams);
3033 for (i = 0; i < avi->num_streams; i++) {
3034 GstAviStream *stream = &avi->stream[i];
3038 gst_pad_push_event (stream->pad, gst_event_ref (event));
3041 gst_event_unref (event);
3046 gst_avi_demux_check_seekability (GstAviDemux * avi)
3049 gboolean seekable = FALSE;
3050 gint64 start = -1, stop = -1;
3052 query = gst_query_new_seeking (GST_FORMAT_BYTES);
3053 if (!gst_pad_peer_query (avi->sinkpad, query)) {
3054 GST_DEBUG_OBJECT (avi, "seeking query failed");
3058 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
3060 /* try harder to query upstream size if we didn't get it the first time */
3061 if (seekable && stop == -1) {
3062 GstFormat fmt = GST_FORMAT_BYTES;
3064 GST_DEBUG_OBJECT (avi, "doing duration query to fix up unset stop");
3065 gst_pad_query_peer_duration (avi->sinkpad, &fmt, &stop);
3068 /* if upstream doesn't know the size, it's likely that it's not seekable in
3069 * practice even if it technically may be seekable */
3070 if (seekable && (start != 0 || stop <= start)) {
3071 GST_DEBUG_OBJECT (avi, "seekable but unknown start/stop -> disable");
3076 GST_INFO_OBJECT (avi, "seekable: %d (%" G_GUINT64_FORMAT " - %"
3077 G_GUINT64_FORMAT ")", seekable, start, stop);
3078 avi->seekable = seekable;
3080 gst_query_unref (query);
3084 * Read AVI headers when streaming
3086 static GstFlowReturn
3087 gst_avi_demux_stream_header_push (GstAviDemux * avi)
3089 GstFlowReturn ret = GST_FLOW_OK;
3094 GstBuffer *buf = NULL, *sub = NULL;
3098 GstTagList *tags = NULL;
3100 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
3102 switch (avi->header_state) {
3103 case GST_AVI_DEMUX_HEADER_TAG_LIST:
3104 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3105 avi->offset += 8 + GST_ROUND_UP_2 (size);
3106 if (tag != GST_RIFF_TAG_LIST)
3107 goto header_no_list;
3109 gst_adapter_flush (avi->adapter, 8);
3110 /* Find the 'hdrl' LIST tag */
3111 GST_DEBUG ("Reading %d bytes", size);
3112 buf = gst_adapter_take_buffer (avi->adapter, size);
3114 if (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl)
3115 goto header_no_hdrl;
3119 gst_adapter_flush (avi->adapter, 1);
3121 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
3123 /* the hdrl starts with a 'avih' header */
3124 if (!gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3126 goto header_no_avih;
3128 if (tag != GST_RIFF_TAG_avih)
3129 goto header_no_avih;
3131 if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3132 goto header_wrong_avih;
3134 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
3136 /* now, read the elements from the header until the end */
3137 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3139 /* sub can be NULL on empty tags */
3144 case GST_RIFF_TAG_LIST:
3145 if (GST_BUFFER_SIZE (sub) < 4)
3148 switch (GST_READ_UINT32_LE (GST_BUFFER_DATA (sub))) {
3149 case GST_RIFF_LIST_strl:
3150 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3152 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3153 ("failed to parse stream, ignoring"));
3158 case GST_RIFF_LIST_odml:
3159 gst_avi_demux_parse_odml (avi, sub);
3163 GST_WARNING_OBJECT (avi,
3164 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3165 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA
3168 case GST_RIFF_TAG_JUNQ:
3169 case GST_RIFF_TAG_JUNK:
3174 gst_avi_demux_parse_idit (avi, sub);
3177 GST_WARNING_OBJECT (avi,
3178 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
3179 offset, GST_FOURCC_ARGS (tag));
3181 case GST_RIFF_TAG_JUNQ:
3182 case GST_RIFF_TAG_JUNK:
3184 /* move to next chunk */
3186 gst_buffer_unref (sub);
3191 gst_buffer_unref (buf);
3192 GST_DEBUG ("elements parsed");
3194 /* check parsed streams */
3195 if (avi->num_streams == 0) {
3197 } else if (avi->num_streams != avi->avih->streams) {
3198 GST_WARNING_OBJECT (avi,
3199 "Stream header mentioned %d streams, but %d available",
3200 avi->avih->streams, avi->num_streams);
3202 GST_DEBUG ("Get junk and info next");
3203 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
3205 /* Need more data */
3209 case GST_AVI_DEMUX_HEADER_INFO:
3210 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
3212 if (gst_adapter_available (avi->adapter) < 12)
3215 data = gst_adapter_peek (avi->adapter, 12);
3216 tag = GST_READ_UINT32_LE (data);
3217 size = GST_READ_UINT32_LE (data + 4);
3218 ltag = GST_READ_UINT32_LE (data + 8);
3220 if (tag == GST_RIFF_TAG_LIST) {
3222 case GST_RIFF_LIST_movi:
3223 gst_adapter_flush (avi->adapter, 12);
3224 if (!avi->first_movi_offset)
3225 avi->first_movi_offset = avi->offset;
3227 avi->idx1_offset = avi->offset + size - 4;
3229 case GST_RIFF_LIST_INFO:
3230 GST_DEBUG ("Found INFO chunk");
3231 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3232 GST_DEBUG ("got size %d", size);
3234 gst_adapter_flush (avi->adapter, 12);
3236 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
3239 gst_adapter_flush (avi->adapter, 1);
3240 gst_riff_parse_info (GST_ELEMENT_CAST (avi), buf, &tags);
3242 if (avi->globaltags) {
3243 gst_tag_list_insert (avi->globaltags, tags,
3244 GST_TAG_MERGE_REPLACE);
3246 avi->globaltags = tags;
3250 gst_buffer_unref (buf);
3252 avi->offset += GST_ROUND_UP_2 (size) - 4;
3254 GST_DEBUG ("skipping INFO LIST prefix");
3257 /* Need more data */
3262 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3263 avi->offset += 8 + GST_ROUND_UP_2 (size);
3264 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3265 // ??? goto iterate; ???
3267 /* Need more data */
3273 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3274 avi->offset += 8 + GST_ROUND_UP_2 (size);
3275 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3278 /* Need more data */
3285 GST_WARNING ("unhandled header state: %d", avi->header_state);
3290 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3291 avi->num_streams, avi->stream[0].indexes);
3293 GST_DEBUG ("Found movi chunk. Starting to stream data");
3294 avi->state = GST_AVI_DEMUX_MOVI;
3296 /* no indexes in push mode, but it still sets some variables */
3297 gst_avi_demux_calculate_durations_from_index (avi);
3299 gst_avi_demux_expose_streams (avi, TRUE);
3301 /* prepare all streams for index 0 */
3302 for (i = 0; i < avi->num_streams; i++)
3303 avi->stream[i].current_entry = 0;
3305 /* create initial NEWSEGMENT event */
3306 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3307 stop = avi->segment.duration;
3309 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3312 gst_event_unref (avi->seg_event);
3313 avi->seg_event = gst_event_new_new_segment_full
3314 (FALSE, avi->segment.rate, avi->segment.applied_rate, GST_FORMAT_TIME,
3315 avi->segment.start, stop, avi->segment.time);
3317 gst_avi_demux_check_seekability (avi);
3319 /* at this point we know all the streams and we can signal the no more
3321 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3322 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3329 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3330 return GST_FLOW_ERROR;
3334 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3335 ("Invalid AVI header (no LIST at start): %"
3336 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3337 return GST_FLOW_ERROR;
3341 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3342 ("Invalid AVI header (no hdrl at start): %"
3343 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3344 gst_buffer_unref (buf);
3345 return GST_FLOW_ERROR;
3349 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3350 ("Invalid AVI header (no avih at start): %"
3351 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3353 gst_buffer_unref (sub);
3355 gst_buffer_unref (buf);
3356 return GST_FLOW_ERROR;
3360 gst_buffer_unref (buf);
3361 return GST_FLOW_ERROR;
3366 gst_avi_demux_add_date_tag (GstAviDemux * avi, gint y, gint m, gint d)
3369 date = g_date_new_dmy (d, m, y);
3370 if (!g_date_valid (date)) {
3372 GST_WARNING_OBJECT (avi, "Refusing to add invalid date %d-%d-%d", y, m, d);
3377 if (avi->globaltags == NULL)
3378 avi->globaltags = gst_tag_list_new ();
3380 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE, date,
3386 gst_avi_demux_parse_idit_nums_only (GstAviDemux * avi, gchar * data)
3391 ret = sscanf (data, "%d:%d:%d", &y, &m, &d);
3393 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3396 gst_avi_demux_add_date_tag (avi, y, m, d);
3400 get_month_num (gchar * data, guint size)
3402 if (g_ascii_strncasecmp (data, "jan", 3) == 0) {
3404 } else if (g_ascii_strncasecmp (data, "feb", 3) == 0) {
3406 } else if (g_ascii_strncasecmp (data, "mar", 3) == 0) {
3408 } else if (g_ascii_strncasecmp (data, "apr", 3) == 0) {
3410 } else if (g_ascii_strncasecmp (data, "may", 3) == 0) {
3412 } else if (g_ascii_strncasecmp (data, "jun", 3) == 0) {
3414 } else if (g_ascii_strncasecmp (data, "jul", 3) == 0) {
3416 } else if (g_ascii_strncasecmp (data, "aug", 3) == 0) {
3418 } else if (g_ascii_strncasecmp (data, "sep", 3) == 0) {
3420 } else if (g_ascii_strncasecmp (data, "oct", 3) == 0) {
3422 } else if (g_ascii_strncasecmp (data, "nov", 3) == 0) {
3424 } else if (g_ascii_strncasecmp (data, "dec", 3) == 0) {
3432 gst_avi_demux_parse_idit_text (GstAviDemux * avi, gchar * data)
3434 gint year, month, day;
3435 gint hour, min, sec;
3440 ret = sscanf (data, "%3s %3s %d %d:%d:%d %d", weekday, monthstr, &day, &hour,
3443 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3446 month = get_month_num (monthstr, strlen (monthstr));
3447 gst_avi_demux_add_date_tag (avi, year, month, day);
3451 gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf)
3453 gchar *data = (gchar *) GST_BUFFER_DATA (buf);
3454 guint size = GST_BUFFER_SIZE (buf);
3455 gchar *safedata = NULL;
3459 * http://www.eden-foundation.org/products/code/film_date_stamp/index.html
3461 * This tag could be in one of the below formats
3462 * 2005:08:17 11:42:43
3463 * THU OCT 26 16:46:04 2006
3464 * Mon Mar 3 09:44:56 2008
3466 * FIXME: Our date tag doesn't include hours
3469 /* skip eventual initial whitespace */
3470 while (size > 0 && g_ascii_isspace (data[0])) {
3479 /* make a safe copy to add a \0 to the end of the string */
3480 safedata = g_strndup (data, size);
3482 /* test if the first char is a alpha or a number */
3483 if (g_ascii_isdigit (data[0])) {
3484 gst_avi_demux_parse_idit_nums_only (avi, safedata);
3487 } else if (g_ascii_isalpha (data[0])) {
3488 gst_avi_demux_parse_idit_text (avi, safedata);
3496 GST_WARNING_OBJECT (avi, "IDIT tag has no parsable info");
3500 * Read full AVI headers.
3502 static GstFlowReturn
3503 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
3506 GstBuffer *buf, *sub = NULL;
3510 GstElement *element = GST_ELEMENT_CAST (avi);
3512 GstTagList *tags = NULL;
3514 stamp = gst_util_get_timestamp ();
3516 /* the header consists of a 'hdrl' LIST tag */
3517 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3518 if (res != GST_FLOW_OK)
3519 goto pull_range_failed;
3520 else if (tag != GST_RIFF_TAG_LIST)
3522 else if (GST_BUFFER_SIZE (buf) < 4)
3525 GST_DEBUG_OBJECT (avi, "parsing headers");
3527 /* Find the 'hdrl' LIST tag */
3528 while (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl) {
3529 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
3530 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf))));
3533 gst_buffer_unref (buf);
3535 /* read new chunk */
3536 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3537 if (res != GST_FLOW_OK)
3538 goto pull_range_failed;
3539 else if (tag != GST_RIFF_TAG_LIST)
3541 else if (GST_BUFFER_SIZE (buf) < 4)
3545 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
3547 /* the hdrl starts with a 'avih' header */
3548 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
3550 else if (tag != GST_RIFF_TAG_avih)
3552 else if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3555 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3557 /* now, read the elements from the header until the end */
3558 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
3559 /* sub can be NULL on empty tags */
3564 case GST_RIFF_TAG_LIST:
3569 if (GST_BUFFER_SIZE (sub) < 4)
3572 data = GST_BUFFER_DATA (sub);
3573 fourcc = GST_READ_UINT32_LE (data);
3576 case GST_RIFF_LIST_strl:
3577 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3578 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3579 ("failed to parse stream, ignoring"));
3584 case GST_RIFF_LIST_odml:
3585 gst_avi_demux_parse_odml (avi, sub);
3588 case GST_RIFF_LIST_INFO:
3589 GST_BUFFER_DATA (sub) = data + 4;
3590 GST_BUFFER_SIZE (sub) -= 4;
3591 gst_riff_parse_info (element, sub, &tags);
3593 if (avi->globaltags) {
3594 gst_tag_list_insert (avi->globaltags, tags,
3595 GST_TAG_MERGE_REPLACE);
3597 avi->globaltags = tags;
3603 GST_WARNING_OBJECT (avi,
3604 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3605 GST_FOURCC_ARGS (fourcc));
3606 GST_MEMDUMP_OBJECT (avi, "Unknown list", GST_BUFFER_DATA (sub),
3607 GST_BUFFER_SIZE (sub));
3609 case GST_RIFF_TAG_JUNQ:
3610 case GST_RIFF_TAG_JUNK:
3616 gst_avi_demux_parse_idit (avi, sub);
3619 GST_WARNING_OBJECT (avi,
3620 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
3621 GST_FOURCC_ARGS (tag), offset);
3622 GST_MEMDUMP_OBJECT (avi, "Unknown tag", GST_BUFFER_DATA (sub),
3623 GST_BUFFER_SIZE (sub));
3625 case GST_RIFF_TAG_JUNQ:
3626 case GST_RIFF_TAG_JUNK:
3629 gst_buffer_unref (sub);
3634 gst_buffer_unref (buf);
3635 GST_DEBUG ("elements parsed");
3637 /* check parsed streams */
3638 if (avi->num_streams == 0)
3640 else if (avi->num_streams != avi->avih->streams) {
3641 GST_WARNING_OBJECT (avi,
3642 "Stream header mentioned %d streams, but %d available",
3643 avi->avih->streams, avi->num_streams);
3646 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
3647 G_GUINT64_FORMAT, avi->offset);
3649 /* Now, find the data (i.e. skip all junk between header and data) */
3655 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
3656 if (res != GST_FLOW_OK) {
3657 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
3658 goto pull_range_failed;
3659 } else if (GST_BUFFER_SIZE (buf) < 12) {
3660 GST_DEBUG_OBJECT (avi, "got %d bytes which is less than 12 bytes",
3661 GST_BUFFER_SIZE (buf));
3662 gst_buffer_unref (buf);
3663 return GST_FLOW_ERROR;
3666 data = GST_BUFFER_DATA (buf);
3668 tag = GST_READ_UINT32_LE (data);
3669 size = GST_READ_UINT32_LE (data + 4);
3670 ltag = GST_READ_UINT32_LE (data + 8);
3672 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
3673 GST_FOURCC_ARGS (tag), size);
3674 GST_MEMDUMP ("Tag content", data, GST_BUFFER_SIZE (buf));
3675 gst_buffer_unref (buf);
3678 case GST_RIFF_TAG_LIST:{
3680 case GST_RIFF_LIST_movi:
3681 GST_DEBUG_OBJECT (avi,
3682 "Reached the 'movi' tag, we're done with skipping");
3684 case GST_RIFF_LIST_INFO:
3686 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3688 if (res != GST_FLOW_OK) {
3689 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3690 goto pull_range_failed;
3692 GST_DEBUG ("got size %u", GST_BUFFER_SIZE (buf));
3694 GST_DEBUG ("skipping INFO LIST prefix");
3695 avi->offset += (4 - GST_ROUND_UP_2 (size));
3696 gst_buffer_unref (buf);
3700 sub = gst_buffer_create_sub (buf, 4, GST_BUFFER_SIZE (buf) - 4);
3701 gst_riff_parse_info (element, sub, &tags);
3703 if (avi->globaltags) {
3704 gst_tag_list_insert (avi->globaltags, tags,
3705 GST_TAG_MERGE_REPLACE);
3707 avi->globaltags = tags;
3712 gst_buffer_unref (sub);
3715 gst_buffer_unref (buf);
3716 /* gst_riff_read_chunk() has already advanced avi->offset */
3719 GST_WARNING_OBJECT (avi,
3720 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3721 GST_FOURCC_ARGS (ltag));
3722 avi->offset += 8 + GST_ROUND_UP_2 (size);
3728 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3729 GST_FOURCC_ARGS (tag));
3731 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3732 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3733 /* Only get buffer for debugging if the memdump is needed */
3734 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= 9) {
3735 res = gst_pad_pull_range (avi->sinkpad, avi->offset, size, &buf);
3736 if (res != GST_FLOW_OK) {
3737 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3738 goto pull_range_failed;
3740 GST_MEMDUMP ("Junk", GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
3741 gst_buffer_unref (buf);
3743 avi->offset += 8 + GST_ROUND_UP_2 (size);
3749 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3750 avi->num_streams, avi->stream[0].indexes);
3752 /* create or read stream index (for seeking) */
3753 if (avi->stream[0].indexes != NULL) {
3754 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3755 gst_avi_demux_read_subindexes_pull (avi);
3757 if (!avi->have_index) {
3758 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
3759 gst_avi_demux_stream_index (avi);
3761 /* still no index, scan */
3762 if (!avi->have_index) {
3763 gst_avi_demux_stream_scan (avi);
3765 /* still no index.. this is a fatal error for now.
3766 * FIXME, we should switch to plain push mode without seeking
3767 * instead of failing. */
3768 if (!avi->have_index)
3772 /* use the indexes now to construct nice durations */
3773 gst_avi_demux_calculate_durations_from_index (avi);
3775 gst_avi_demux_expose_streams (avi, FALSE);
3777 /* create initial NEWSEGMENT event */
3778 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3779 stop = avi->segment.duration;
3781 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3783 /* do initial seek to the default segment values */
3784 gst_avi_demux_do_seek (avi, &avi->segment);
3786 /* prepare initial segment */
3788 gst_event_unref (avi->seg_event);
3789 avi->seg_event = gst_event_new_new_segment_full
3790 (FALSE, avi->segment.rate, avi->segment.applied_rate, GST_FORMAT_TIME,
3791 avi->segment.start, stop, avi->segment.time);
3793 stamp = gst_util_get_timestamp () - stamp;
3794 GST_DEBUG_OBJECT (avi, "pulling header took %" GST_TIME_FORMAT,
3795 GST_TIME_ARGS (stamp));
3797 /* at this point we know all the streams and we can signal the no more
3799 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3800 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3807 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3808 ("Invalid AVI header (no LIST at start): %"
3809 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3810 gst_buffer_unref (buf);
3811 return GST_FLOW_ERROR;
3815 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3816 ("Invalid AVI header (no hdrl at start): %"
3817 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3818 gst_buffer_unref (buf);
3819 return GST_FLOW_ERROR;
3823 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3824 ("Invalid AVI header (no avih at start): %"
3825 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3827 gst_buffer_unref (sub);
3828 gst_buffer_unref (buf);
3829 return GST_FLOW_ERROR;
3833 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3834 ("Invalid AVI header (cannot parse avih at start)"));
3835 gst_buffer_unref (buf);
3836 return GST_FLOW_ERROR;
3840 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3841 return GST_FLOW_ERROR;
3845 GST_WARNING ("file without or too big index");
3846 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3847 ("Could not get/create index"));
3848 return GST_FLOW_ERROR;
3852 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3853 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3854 return GST_FLOW_ERROR;
3858 /* move a stream to @index */
3860 gst_avi_demux_move_stream (GstAviDemux * avi, GstAviStream * stream,
3861 GstSegment * segment, guint index)
3863 GST_DEBUG_OBJECT (avi, "Move stream %d to %u", stream->num, index);
3865 if (segment->rate < 0.0) {
3867 /* Because we don't know the frame order we need to push from the prev keyframe
3868 * to the next keyframe. If there is a smart decoder downstream he will notice
3869 * that there are too many encoded frames send and return UNEXPECTED when there
3870 * are enough decoded frames to fill the segment. */
3871 next_key = gst_avi_demux_index_next (avi, stream, index, TRUE);
3873 /* FIXME, we go back to 0, we should look at segment.start. We will however
3874 * stop earlier when the see the timestamp < segment.start */
3875 stream->start_entry = 0;
3876 stream->step_entry = index;
3877 stream->current_entry = index;
3878 stream->stop_entry = next_key;
3880 GST_DEBUG_OBJECT (avi, "reverse seek: start %u, step %u, stop %u",
3881 stream->start_entry, stream->step_entry, stream->stop_entry);
3883 stream->start_entry = index;
3884 stream->step_entry = index;
3885 stream->stop_entry = gst_avi_demux_index_last (avi, stream);
3887 if (stream->current_entry != index) {
3888 GST_DEBUG_OBJECT (avi, "Move DISCONT from %u to %u",
3889 stream->current_entry, index);
3890 stream->current_entry = index;
3891 stream->discont = TRUE;
3894 /* update the buffer info */
3895 gst_avi_demux_get_buffer_info (avi, stream, index,
3896 &stream->current_timestamp, &stream->current_ts_end,
3897 &stream->current_offset, &stream->current_offset_end);
3899 GST_DEBUG_OBJECT (avi, "Moved to %u, ts %" GST_TIME_FORMAT
3900 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
3901 ", off_end %" G_GUINT64_FORMAT, index,
3902 GST_TIME_ARGS (stream->current_timestamp),
3903 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
3904 stream->current_offset_end);
3906 GST_DEBUG_OBJECT (avi, "Seeking to offset %" G_GUINT64_FORMAT,
3907 stream->index[index].offset);
3911 * Do the actual seeking.
3914 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
3916 GstClockTime seek_time;
3919 GstAviStream *stream;
3921 seek_time = segment->last_stop;
3922 keyframe = !!(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
3924 GST_DEBUG_OBJECT (avi, "seek to: %" GST_TIME_FORMAT
3925 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
3927 /* FIXME, this code assumes the main stream with keyframes is stream 0,
3928 * which is mostly correct... */
3929 stream = &avi->stream[avi->main_stream];
3931 /* get the entry index for the requested position */
3932 index = gst_avi_demux_index_for_time (avi, stream, seek_time);
3933 GST_DEBUG_OBJECT (avi, "Got entry %u", index);
3935 /* check if we are already on a keyframe */
3936 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
3937 GST_DEBUG_OBJECT (avi, "not keyframe, searching back");
3938 /* now go to the previous keyframe, this is where we should start
3940 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
3941 GST_DEBUG_OBJECT (avi, "previous keyframe at %u", index);
3944 /* move the main stream to this position */
3945 gst_avi_demux_move_stream (avi, stream, segment, index);
3948 /* when seeking to a keyframe, we update the result seek time
3949 * to the time of the keyframe. */
3950 seek_time = stream->current_timestamp;
3951 GST_DEBUG_OBJECT (avi, "keyframe adjusted to %" GST_TIME_FORMAT,
3952 GST_TIME_ARGS (seek_time));
3955 /* the seek time is also the last_stop and stream time when going
3957 segment->last_stop = seek_time;
3958 if (segment->rate > 0.0)
3959 segment->time = seek_time;
3961 /* now set DISCONT and align the other streams */
3962 for (i = 0; i < avi->num_streams; i++) {
3963 GstAviStream *ostream;
3965 ostream = &avi->stream[i];
3966 if ((ostream == stream) || (ostream->index == NULL))
3969 /* get the entry index for the requested position */
3970 index = gst_avi_demux_index_for_time (avi, ostream, seek_time);
3972 /* move to previous keyframe */
3973 if (!ENTRY_IS_KEYFRAME (&ostream->index[index]))
3974 index = gst_avi_demux_index_prev (avi, ostream, index, TRUE);
3976 gst_avi_demux_move_stream (avi, ostream, segment, index);
3978 GST_DEBUG_OBJECT (avi, "done seek to: %" GST_TIME_FORMAT,
3979 GST_TIME_ARGS (seek_time));
3985 * Handle seek event in pull mode.
3988 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
3993 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
3997 GstSegment seeksegment = { 0, };
4001 GST_DEBUG_OBJECT (avi, "doing seek with event");
4003 gst_event_parse_seek (event, &rate, &format, &flags,
4004 &cur_type, &cur, &stop_type, &stop);
4006 /* we have to have a format as the segment format. Try to convert
4008 if (format != GST_FORMAT_TIME) {
4009 GstFormat fmt = GST_FORMAT_TIME;
4010 gboolean res = TRUE;
4012 if (cur_type != GST_SEEK_TYPE_NONE)
4013 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
4014 if (res && stop_type != GST_SEEK_TYPE_NONE)
4015 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
4021 GST_DEBUG_OBJECT (avi,
4022 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
4023 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
4024 /* FIXME: can we do anything with rate!=1.0 */
4026 GST_DEBUG_OBJECT (avi, "doing seek without event");
4031 /* save flush flag */
4032 flush = flags & GST_SEEK_FLAG_FLUSH;
4035 GstEvent *fevent = gst_event_new_flush_start ();
4037 /* for a flushing seek, we send a flush_start on all pads. This will
4038 * eventually stop streaming with a WRONG_STATE. We can thus eventually
4039 * take the STREAM_LOCK. */
4040 GST_DEBUG_OBJECT (avi, "sending flush start");
4041 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4042 gst_pad_push_event (avi->sinkpad, fevent);
4044 /* a non-flushing seek, we PAUSE the task so that we can take the
4046 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
4047 gst_pad_pause_task (avi->sinkpad);
4050 /* wait for streaming to stop */
4051 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
4052 GST_PAD_STREAM_LOCK (avi->sinkpad);
4054 /* copy segment, we need this because we still need the old
4055 * segment when we close the current segment. */
4056 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4059 GST_DEBUG_OBJECT (avi, "configuring seek");
4060 gst_segment_set_seek (&seeksegment, rate, format, flags,
4061 cur_type, cur, stop_type, stop, &update);
4063 /* do the seek, seeksegment.last_stop contains the new position, this
4064 * actually never fails. */
4065 gst_avi_demux_do_seek (avi, &seeksegment);
4068 GstEvent *fevent = gst_event_new_flush_stop ();
4070 GST_DEBUG_OBJECT (avi, "sending flush stop");
4071 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4072 gst_pad_push_event (avi->sinkpad, fevent);
4073 } else if (avi->segment_running) {
4076 /* we are running the current segment and doing a non-flushing seek,
4077 * close the segment first based on the last_stop. */
4078 GST_DEBUG_OBJECT (avi, "closing running segment %" G_GINT64_FORMAT
4079 " to %" G_GINT64_FORMAT, avi->segment.start, avi->segment.last_stop);
4080 seg = gst_event_new_new_segment_full (TRUE,
4081 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
4082 avi->segment.start, avi->segment.last_stop, avi->segment.time);
4083 gst_avi_demux_push_event (avi, seg);
4086 /* now update the real segment info */
4087 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
4089 /* post the SEGMENT_START message when we do segmented playback */
4090 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4091 gst_element_post_message (GST_ELEMENT_CAST (avi),
4092 gst_message_new_segment_start (GST_OBJECT_CAST (avi),
4093 avi->segment.format, avi->segment.last_stop));
4096 /* prepare for streaming again */
4097 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
4098 stop = avi->segment.duration;
4100 /* queue the segment event for the streaming thread. */
4102 gst_event_unref (avi->seg_event);
4103 if (avi->segment.rate > 0.0) {
4104 /* forwards goes from last_stop to stop */
4105 avi->seg_event = gst_event_new_new_segment_full (FALSE,
4106 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
4107 avi->segment.last_stop, stop, avi->segment.time);
4109 /* reverse goes from start to last_stop */
4110 avi->seg_event = gst_event_new_new_segment_full (FALSE,
4111 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
4112 avi->segment.start, avi->segment.last_stop, avi->segment.time);
4115 if (!avi->streaming) {
4116 avi->segment_running = TRUE;
4117 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4120 /* reset the last flow and mark discont, seek is always DISCONT */
4121 for (i = 0; i < avi->num_streams; i++) {
4122 GST_DEBUG_OBJECT (avi, "marking DISCONT");
4123 avi->stream[i].last_flow = GST_FLOW_OK;
4124 avi->stream[i].discont = TRUE;
4126 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
4133 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4139 * Handle seek event in push mode.
4142 avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4147 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4150 GstAviStream *stream;
4154 GstSegment seeksegment;
4157 /* check we have the index */
4158 if (!avi->have_index) {
4159 GST_DEBUG_OBJECT (avi, "no seek index built, seek aborted.");
4162 GST_DEBUG_OBJECT (avi, "doing push-based seek with event");
4165 gst_event_parse_seek (event, &rate, &format, &flags,
4166 &cur_type, &cur, &stop_type, &stop);
4168 if (format != GST_FORMAT_TIME) {
4169 GstFormat fmt = GST_FORMAT_TIME;
4170 gboolean res = TRUE;
4172 if (cur_type != GST_SEEK_TYPE_NONE)
4173 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
4174 if (res && stop_type != GST_SEEK_TYPE_NONE)
4175 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
4177 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4184 /* let gst_segment handle any tricky stuff */
4185 GST_DEBUG_OBJECT (avi, "configuring seek");
4186 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4187 gst_segment_set_seek (&seeksegment, rate, format, flags,
4188 cur_type, cur, stop_type, stop, &update);
4190 keyframe = !!(flags & GST_SEEK_FLAG_KEY_UNIT);
4191 cur = seeksegment.last_stop;
4193 GST_DEBUG_OBJECT (avi,
4194 "Seek requested: ts %" GST_TIME_FORMAT " stop %" GST_TIME_FORMAT
4195 ", kf %u, rate %lf", GST_TIME_ARGS (cur), GST_TIME_ARGS (stop), keyframe,
4199 GST_DEBUG_OBJECT (avi, "negative rate seek not supported in push mode");
4203 /* FIXME, this code assumes the main stream with keyframes is stream 0,
4204 * which is mostly correct... */
4205 str_num = avi->main_stream;
4206 stream = &avi->stream[str_num];
4208 /* get the entry index for the requested position */
4209 index = gst_avi_demux_index_for_time (avi, stream, cur);
4210 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT,
4211 str_num, index, GST_TIME_ARGS (cur));
4213 /* check if we are already on a keyframe */
4214 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
4215 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4216 /* now go to the previous keyframe, this is where we should start
4218 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4219 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", index);
4222 gst_avi_demux_get_buffer_info (avi, stream, index,
4223 &stream->current_timestamp, &stream->current_ts_end,
4224 &stream->current_offset, &stream->current_offset_end);
4226 /* re-use cur to be the timestamp of the seek as it _will_ be */
4227 cur = stream->current_timestamp;
4229 min_offset = stream->index[index].offset;
4230 avi->seek_kf_offset = min_offset - 8;
4232 GST_DEBUG_OBJECT (avi,
4233 "Seek to: ts %" GST_TIME_FORMAT " (on str %u, idx %u, offset %"
4234 G_GUINT64_FORMAT ")", GST_TIME_ARGS (stream->current_timestamp), str_num,
4237 for (n = 0; n < avi->num_streams; n++) {
4238 GstAviStream *str = &avi->stream[n];
4241 if (n == avi->main_stream)
4244 /* get the entry index for the requested position */
4245 idx = gst_avi_demux_index_for_time (avi, str, cur);
4246 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT, n,
4247 idx, GST_TIME_ARGS (cur));
4249 /* check if we are already on a keyframe */
4250 if (!ENTRY_IS_KEYFRAME (&str->index[idx])) {
4251 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4252 /* now go to the previous keyframe, this is where we should start
4254 idx = gst_avi_demux_index_prev (avi, str, idx, TRUE);
4255 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", idx);
4258 gst_avi_demux_get_buffer_info (avi, str, idx,
4259 &str->current_timestamp, &str->current_ts_end,
4260 &str->current_offset, &str->current_offset_end);
4262 if (str->index[idx].offset < min_offset) {
4263 min_offset = str->index[idx].offset;
4264 GST_DEBUG_OBJECT (avi,
4265 "Found an earlier offset at %" G_GUINT64_FORMAT ", str %u",
4273 GST_DEBUG_OBJECT (avi,
4274 "Seek performed: str %u, offset %" G_GUINT64_FORMAT ", idx %u, ts %"
4275 GST_TIME_FORMAT ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4276 ", off_end %" G_GUINT64_FORMAT, str_num, min_offset, index,
4277 GST_TIME_ARGS (stream->current_timestamp),
4278 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4279 stream->current_offset_end);
4281 /* index data refers to data, not chunk header (for pull mode convenience) */
4283 GST_DEBUG_OBJECT (avi, "seeking to chunk at offset %" G_GUINT64_FORMAT,
4286 if (!perform_seek_to_offset (avi, min_offset)) {
4287 GST_DEBUG_OBJECT (avi, "seek event failed!");
4295 * Handle whether we can perform the seek event or if we have to let the chain
4296 * function handle seeks to build the seek indexes first.
4299 gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
4302 /* check for having parsed index already */
4303 if (!avi->have_index) {
4305 gboolean building_index;
4307 GST_OBJECT_LOCK (avi);
4308 /* handle the seek event in the chain function */
4309 avi->state = GST_AVI_DEMUX_SEEK;
4311 /* copy the event */
4312 if (avi->seek_event)
4313 gst_event_unref (avi->seek_event);
4314 avi->seek_event = gst_event_ref (event);
4316 /* set the building_index flag so that only one thread can setup the
4317 * structures for index seeking. */
4318 building_index = avi->building_index;
4319 if (!building_index) {
4320 avi->building_index = TRUE;
4321 if (avi->stream[0].indexes) {
4322 avi->odml_stream = 0;
4323 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
4324 offset = avi->odml_subidxs[0];
4326 offset = avi->idx1_offset;
4329 GST_OBJECT_UNLOCK (avi);
4331 if (!building_index) {
4332 /* seek to the first subindex or legacy index */
4333 GST_INFO_OBJECT (avi,
4334 "Seeking to legacy index/first subindex at %" G_GUINT64_FORMAT,
4336 return perform_seek_to_offset (avi, offset);
4339 /* FIXME: we have to always return true so that we don't block the seek
4341 * Note: maybe it is OK to return true if we're still building the index */
4345 return avi_demux_handle_seek_push (avi, pad, event);
4349 * Helper for gst_avi_demux_invert()
4352 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
4354 memcpy (tmp, d1, bytes);
4355 memcpy (d1, d2, bytes);
4356 memcpy (d2, tmp, bytes);
4360 #define gst_avi_demux_is_uncompressed(fourcc) \
4361 (fourcc == GST_RIFF_DIB || \
4362 fourcc == GST_RIFF_rgb || \
4363 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW)
4366 * Invert DIB buffers... Takes existing buffer and
4367 * returns either the buffer or a new one (with old
4368 * one dereferenced).
4369 * FIXME: can't we preallocate tmp? and remember stride, bpp?
4372 gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
4379 if (stream->strh->type != GST_RIFF_FCC_vids)
4382 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
4383 return buf; /* Ignore non DIB buffers */
4386 s = gst_caps_get_structure (GST_PAD_CAPS (stream->pad), 0);
4387 if (!gst_structure_get_int (s, "bpp", &bpp)) {
4388 GST_WARNING ("Failed to retrieve depth from caps");
4392 if (stream->strf.vids == NULL) {
4393 GST_WARNING ("Failed to retrieve vids for stream");
4397 h = stream->strf.vids->height;
4398 w = stream->strf.vids->width;
4399 stride = w * (bpp / 8);
4401 buf = gst_buffer_make_writable (buf);
4402 if (GST_BUFFER_SIZE (buf) < (stride * h)) {
4403 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
4407 tmp = g_malloc (stride);
4409 for (y = 0; y < h / 2; y++) {
4410 swap_line (GST_BUFFER_DATA (buf) + stride * y,
4411 GST_BUFFER_DATA (buf) + stride * (h - 1 - y), tmp, stride);
4420 gst_avi_demux_add_assoc (GstAviDemux * avi, GstAviStream * stream,
4421 GstClockTime timestamp, guint64 offset, gboolean keyframe)
4423 /* do not add indefinitely for open-ended streaming */
4424 if (G_UNLIKELY (avi->element_index && avi->seekable)) {
4425 GST_LOG_OBJECT (avi, "adding association %" GST_TIME_FORMAT "-> %"
4426 G_GUINT64_FORMAT, GST_TIME_ARGS (timestamp), offset);
4427 gst_index_add_association (avi->element_index, avi->index_id,
4428 keyframe ? GST_ASSOCIATION_FLAG_KEY_UNIT : GST_ASSOCIATION_FLAG_NONE,
4429 GST_FORMAT_TIME, timestamp, GST_FORMAT_BYTES, offset, NULL);
4430 /* well, current_total determines TIME and entry DEFAULT (frame #) ... */
4431 gst_index_add_association (avi->element_index, stream->index_id,
4432 GST_ASSOCIATION_FLAG_NONE,
4433 GST_FORMAT_TIME, stream->current_total, GST_FORMAT_BYTES, offset,
4434 GST_FORMAT_DEFAULT, stream->current_entry, NULL);
4439 * Returns the aggregated GstFlowReturn.
4441 static GstFlowReturn
4442 gst_avi_demux_combine_flows (GstAviDemux * avi, GstAviStream * stream,
4446 gboolean unexpected = FALSE, not_linked = TRUE;
4448 /* store the value */
4449 stream->last_flow = ret;
4451 /* any other error that is not-linked or eos can be returned right away */
4452 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
4455 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4456 for (i = 0; i < avi->num_streams; i++) {
4457 GstAviStream *ostream = &avi->stream[i];
4459 ret = ostream->last_flow;
4460 /* no unexpected or unlinked, return */
4461 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
4464 /* we check to see if we have at least 1 unexpected or all unlinked */
4465 unexpected |= (ret == GST_FLOW_UNEXPECTED);
4466 not_linked &= (ret == GST_FLOW_NOT_LINKED);
4468 /* when we get here, we all have unlinked or unexpected */
4470 ret = GST_FLOW_NOT_LINKED;
4471 else if (unexpected)
4472 ret = GST_FLOW_UNEXPECTED;
4474 GST_LOG_OBJECT (avi, "combined %s to return %s",
4475 gst_flow_get_name (stream->last_flow), gst_flow_get_name (ret));
4479 /* move @stream to the next position in its index */
4480 static GstFlowReturn
4481 gst_avi_demux_advance (GstAviDemux * avi, GstAviStream * stream,
4484 guint old_entry, new_entry;
4486 old_entry = stream->current_entry;
4488 new_entry = old_entry + 1;
4490 /* see if we reached the end */
4491 if (new_entry > stream->stop_entry) {
4492 if (avi->segment.rate < 0.0) {
4493 if (stream->step_entry == stream->start_entry) {
4494 /* we stepped all the way to the start, eos */
4495 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
4498 /* backwards, stop becomes step, find a new step */
4499 stream->stop_entry = stream->step_entry;
4500 stream->step_entry = gst_avi_demux_index_prev (avi, stream,
4501 stream->stop_entry, TRUE);
4503 GST_DEBUG_OBJECT (avi,
4504 "reverse playback jump: start %u, step %u, stop %u",
4505 stream->start_entry, stream->step_entry, stream->stop_entry);
4507 /* and start from the previous keyframe now */
4508 new_entry = stream->step_entry;
4511 GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
4516 if (new_entry != old_entry) {
4517 stream->current_entry = new_entry;
4518 stream->current_total = stream->index[new_entry].total;
4520 if (new_entry == old_entry + 1) {
4521 GST_DEBUG_OBJECT (avi, "moved forwards from %u to %u",
4522 old_entry, new_entry);
4523 /* we simply moved one step forwards, reuse current info */
4524 stream->current_timestamp = stream->current_ts_end;
4525 stream->current_offset = stream->current_offset_end;
4526 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4527 NULL, &stream->current_ts_end, NULL, &stream->current_offset_end);
4529 /* we moved DISCONT, full update */
4530 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4531 &stream->current_timestamp, &stream->current_ts_end,
4532 &stream->current_offset, &stream->current_offset_end);
4533 /* and MARK discont for this stream */
4534 stream->last_flow = GST_FLOW_OK;
4535 stream->discont = TRUE;
4536 GST_DEBUG_OBJECT (avi, "Moved from %u to %u, ts %" GST_TIME_FORMAT
4537 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4538 ", off_end %" G_GUINT64_FORMAT, old_entry, new_entry,
4539 GST_TIME_ARGS (stream->current_timestamp),
4540 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4541 stream->current_offset_end);
4549 GST_DEBUG_OBJECT (avi, "we are EOS");
4550 /* setting current_timestamp to -1 marks EOS */
4551 stream->current_timestamp = -1;
4552 return GST_FLOW_UNEXPECTED;
4556 /* find the stream with the lowest current position when going forwards or with
4557 * the highest position when going backwards, this is the stream
4558 * we should push from next */
4560 gst_avi_demux_find_next (GstAviDemux * avi, gfloat rate)
4562 guint64 min_time, max_time;
4563 guint stream_num, i;
4566 min_time = G_MAXUINT64;
4569 for (i = 0; i < avi->num_streams; i++) {
4571 GstAviStream *stream;
4573 stream = &avi->stream[i];
4575 /* ignore streams that finished */
4576 if (stream->last_flow == GST_FLOW_UNEXPECTED)
4579 position = stream->current_timestamp;
4581 /* position of -1 is EOS */
4582 if (position != -1) {
4583 if (rate > 0.0 && position < min_time) {
4584 min_time = position;
4586 } else if (rate < 0.0 && position >= max_time) {
4587 max_time = position;
4595 static GstFlowReturn
4596 gst_avi_demux_loop_data (GstAviDemux * avi)
4598 GstFlowReturn ret = GST_FLOW_OK;
4600 GstAviStream *stream;
4601 gboolean processed = FALSE;
4603 guint64 offset, size;
4604 GstClockTime timestamp, duration;
4605 guint64 out_offset, out_offset_end;
4607 GstAviIndexEntry *entry;
4610 stream_num = gst_avi_demux_find_next (avi, avi->segment.rate);
4613 if (G_UNLIKELY (stream_num == -1)) {
4614 GST_DEBUG_OBJECT (avi, "all streams are EOS");
4618 /* we have the stream now */
4619 stream = &avi->stream[stream_num];
4621 /* skip streams without pads */
4623 GST_DEBUG_OBJECT (avi, "skipping entry from stream %d without pad",
4628 /* get the timing info for the entry */
4629 timestamp = stream->current_timestamp;
4630 duration = stream->current_ts_end - timestamp;
4631 out_offset = stream->current_offset;
4632 out_offset_end = stream->current_offset_end;
4634 /* get the entry data info */
4635 entry = &stream->index[stream->current_entry];
4636 offset = entry->offset;
4638 keyframe = ENTRY_IS_KEYFRAME (entry);
4640 /* skip empty entries */
4642 GST_DEBUG_OBJECT (avi, "Skipping entry %u (%" G_GUINT64_FORMAT ", %p)",
4643 stream->current_entry, size, stream->pad);
4647 if (avi->segment.rate > 0.0) {
4648 /* only check this for fowards playback for now */
4649 if (keyframe && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
4650 && (timestamp > avi->segment.stop)) {
4655 GST_LOG ("reading buffer (size=%" G_GUINT64_FORMAT "), stream %d, pos %"
4656 G_GUINT64_FORMAT " (0x%" G_GINT64_MODIFIER "x), kf %d", size,
4657 stream_num, offset, offset, keyframe);
4659 /* FIXME, check large chunks and cut them up */
4661 /* pull in the data */
4662 ret = gst_pad_pull_range (avi->sinkpad, offset, size, &buf);
4663 if (ret != GST_FLOW_OK)
4666 /* check for short buffers, this is EOS as well */
4667 if (GST_BUFFER_SIZE (buf) < size)
4670 /* invert the picture if needed */
4671 buf = gst_avi_demux_invert (stream, buf);
4673 /* mark non-keyframes */
4675 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4677 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4679 GST_BUFFER_TIMESTAMP (buf) = timestamp;
4680 GST_BUFFER_DURATION (buf) = duration;
4681 GST_BUFFER_OFFSET (buf) = out_offset;
4682 GST_BUFFER_OFFSET_END (buf) = out_offset_end;
4684 /* mark discont when pending */
4685 if (stream->discont) {
4686 GST_DEBUG_OBJECT (avi, "setting DISCONT flag");
4687 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4688 stream->discont = FALSE;
4691 gst_avi_demux_add_assoc (avi, stream, timestamp, offset, keyframe);
4693 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4695 /* update current position in the segment */
4696 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, timestamp);
4698 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %u, ts %"
4699 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4700 ", off_end %" G_GUINT64_FORMAT,
4701 GST_BUFFER_SIZE (buf), GST_TIME_ARGS (timestamp),
4702 GST_TIME_ARGS (duration), out_offset, out_offset_end);
4704 ret = gst_pad_push (stream->pad, buf);
4706 /* mark as processed, we increment the frame and byte counters then
4707 * leave the while loop and return the GstFlowReturn */
4710 if (avi->segment.rate < 0) {
4711 if (timestamp > avi->segment.stop && ret == GST_FLOW_UNEXPECTED) {
4712 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
4713 * we are at the end of the segment, so we just need to jump
4714 * back to the previous section. */
4715 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
4720 /* move to next item */
4721 ret = gst_avi_demux_advance (avi, stream, ret);
4724 ret = gst_avi_demux_combine_flows (avi, stream, ret);
4725 } while (!processed);
4733 GST_DEBUG_OBJECT (avi, "No samples left for any streams - EOS");
4734 ret = GST_FLOW_UNEXPECTED;
4739 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
4740 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
4741 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (avi->segment.stop));
4742 ret = GST_FLOW_UNEXPECTED;
4743 /* move to next stream */
4748 GST_DEBUG_OBJECT (avi, "pull range failed: pos=%" G_GUINT64_FORMAT
4749 " size=%" G_GUINT64_FORMAT, offset, size);
4754 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
4755 ", only got %d/%" G_GUINT64_FORMAT " bytes (truncated file?)", offset,
4756 GST_BUFFER_SIZE (buf), size);
4757 gst_buffer_unref (buf);
4758 ret = GST_FLOW_UNEXPECTED;
4764 * Read data. If we have an index it delegates to
4765 * gst_avi_demux_process_next_entry().
4767 static GstFlowReturn
4768 gst_avi_demux_stream_data (GstAviDemux * avi)
4773 GstFlowReturn res = GST_FLOW_OK;
4774 GstFormat format = GST_FORMAT_TIME;
4776 if (G_UNLIKELY (avi->have_eos)) {
4777 /* Clean adapter, we're done */
4778 gst_adapter_clear (avi->adapter);
4779 return GST_FLOW_UNEXPECTED;
4782 if (G_UNLIKELY (avi->todrop)) {
4785 if ((drop = gst_adapter_available (avi->adapter))) {
4786 if (drop > avi->todrop)
4788 GST_DEBUG_OBJECT (avi, "Dropping %d bytes", drop);
4789 gst_adapter_flush (avi->adapter, drop);
4790 avi->todrop -= drop;
4791 avi->offset += drop;
4795 /* Iterate until need more data, so adapter won't grow too much */
4797 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
4801 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
4802 GST_FOURCC_ARGS (tag), size);
4804 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
4805 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
4806 GST_LOG ("Chunk ok");
4807 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
4808 GST_DEBUG ("Found sub-index tag");
4809 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4810 /* accept 0 size buffer here */
4811 avi->abort_buffering = FALSE;
4812 GST_DEBUG (" skipping %d bytes for now", size);
4813 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4816 } else if (tag == GST_RIFF_TAG_RIFF) {
4817 /* RIFF tags can appear in ODML files, just jump over them */
4818 if (gst_adapter_available (avi->adapter) >= 12) {
4819 GST_DEBUG ("Found RIFF tag, skipping RIFF header");
4820 gst_adapter_flush (avi->adapter, 12);
4824 } else if (tag == GST_RIFF_TAG_idx1) {
4825 GST_DEBUG ("Found index tag");
4826 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4827 /* accept 0 size buffer here */
4828 avi->abort_buffering = FALSE;
4829 GST_DEBUG (" skipping %d bytes for now", size);
4830 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4833 } else if (tag == GST_RIFF_TAG_LIST) {
4834 /* movi chunks might be grouped in rec list */
4835 if (gst_adapter_available (avi->adapter) >= 12) {
4836 GST_DEBUG ("Found LIST tag, skipping LIST header");
4837 gst_adapter_flush (avi->adapter, 12);
4841 } else if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ) {
4842 /* rec list might contain JUNK chunks */
4843 GST_DEBUG ("Found JUNK tag");
4844 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4845 /* accept 0 size buffer here */
4846 avi->abort_buffering = FALSE;
4847 GST_DEBUG (" skipping %d bytes for now", size);
4848 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4852 GST_DEBUG ("No more stream chunks, send EOS");
4853 avi->have_eos = TRUE;
4854 return GST_FLOW_UNEXPECTED;
4857 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
4858 /* supposedly one hopes to catch a nicer chunk later on ... */
4859 /* FIXME ?? give up here rather than possibly ending up going
4860 * through the whole file */
4861 if (avi->abort_buffering) {
4862 avi->abort_buffering = FALSE;
4863 gst_adapter_flush (avi->adapter, 8);
4867 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
4868 GST_FOURCC_ARGS (tag), size);
4870 stream_nr = CHUNKID_TO_STREAMNR (tag);
4872 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
4874 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
4875 stream_nr, GST_FOURCC_ARGS (tag));
4876 avi->offset += 8 + GST_ROUND_UP_2 (size);
4877 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4879 GstAviStream *stream;
4880 GstClockTime next_ts = 0;
4881 GstBuffer *buf = NULL;
4883 gboolean saw_desired_kf = stream_nr != avi->main_stream
4884 || avi->offset >= avi->seek_kf_offset;
4886 if (stream_nr == avi->main_stream && avi->offset == avi->seek_kf_offset) {
4887 GST_DEBUG_OBJECT (avi, "Desired keyframe reached");
4888 avi->seek_kf_offset = 0;
4891 if (saw_desired_kf) {
4892 gst_adapter_flush (avi->adapter, 8);
4894 buf = gst_adapter_take_buffer (avi->adapter, GST_ROUND_UP_2 (size));
4895 /* patch the size */
4896 GST_BUFFER_SIZE (buf) = size;
4898 GST_DEBUG_OBJECT (avi,
4899 "Desired keyframe not yet reached, flushing chunk");
4900 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4903 offset = avi->offset;
4904 avi->offset += 8 + GST_ROUND_UP_2 (size);
4906 stream = &avi->stream[stream_nr];
4908 /* set delay (if any)
4909 if (stream->strh->init_frames == stream->current_frame &&
4911 stream->delay = next_ts;
4914 /* parsing of corresponding header may have failed */
4915 if (G_UNLIKELY (!stream->pad)) {
4916 GST_WARNING_OBJECT (avi, "no pad for stream ID %" GST_FOURCC_FORMAT,
4917 GST_FOURCC_ARGS (tag));
4919 gst_buffer_unref (buf);
4921 /* get time of this buffer */
4922 gst_pad_query_position (stream->pad, &format, (gint64 *) & next_ts);
4923 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4926 gst_avi_demux_add_assoc (avi, stream, next_ts, offset, FALSE);
4928 /* increment our positions */
4929 stream->current_entry++;
4930 stream->current_total += size;
4932 /* update current position in the segment */
4933 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, next_ts);
4935 if (saw_desired_kf && buf) {
4936 GstClockTime dur_ts = 0;
4938 /* invert the picture if needed */
4939 buf = gst_avi_demux_invert (stream, buf);
4941 gst_pad_query_position (stream->pad, &format, (gint64 *) & dur_ts);
4942 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4945 GST_BUFFER_TIMESTAMP (buf) = next_ts;
4946 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
4947 if (stream->strh->type == GST_RIFF_FCC_vids) {
4948 GST_BUFFER_OFFSET (buf) = stream->current_entry - 1;
4949 GST_BUFFER_OFFSET_END (buf) = stream->current_entry;
4951 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4952 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
4955 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4956 GST_DEBUG_OBJECT (avi,
4957 "Pushing buffer with time=%" GST_TIME_FORMAT ", duration %"
4958 GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
4959 " and size %d over pad %s", GST_TIME_ARGS (next_ts),
4960 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
4961 GST_BUFFER_OFFSET (buf), size, GST_PAD_NAME (stream->pad));
4963 /* mark discont when pending */
4964 if (G_UNLIKELY (stream->discont)) {
4965 GST_DEBUG_OBJECT (avi, "Setting DISCONT");
4966 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4967 stream->discont = FALSE;
4969 res = gst_pad_push (stream->pad, buf);
4972 res = gst_avi_demux_combine_flows (avi, stream, res);
4973 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4974 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
4988 GST_DEBUG_OBJECT (avi, "format %s != GST_FORMAT_TIME",
4989 gst_format_get_name (format));
4990 res = GST_FLOW_ERROR;
4996 * Send pending tags.
4999 push_tag_lists (GstAviDemux * avi)
5007 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
5009 for (i = 0; i < avi->num_streams; i++) {
5010 GstAviStream *stream = &avi->stream[i];
5011 GstPad *pad = stream->pad;
5013 tags = stream->taglist;
5016 GST_DEBUG_OBJECT (pad, "Tags: %" GST_PTR_FORMAT, tags);
5018 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (avi), pad, tags);
5019 stream->taglist = NULL;
5023 if (!(tags = avi->globaltags))
5024 tags = gst_tag_list_new ();
5026 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
5027 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
5029 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, tags);
5030 gst_element_found_tags (GST_ELEMENT_CAST (avi), tags);
5031 avi->globaltags = NULL;
5032 avi->got_tags = FALSE;
5036 gst_avi_demux_loop (GstPad * pad)
5039 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
5041 switch (avi->state) {
5042 case GST_AVI_DEMUX_START:
5043 res = gst_avi_demux_stream_init_pull (avi);
5044 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5045 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5048 avi->state = GST_AVI_DEMUX_HEADER;
5050 case GST_AVI_DEMUX_HEADER:
5051 res = gst_avi_demux_stream_header_pull (avi);
5052 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5053 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5056 avi->state = GST_AVI_DEMUX_MOVI;
5058 case GST_AVI_DEMUX_MOVI:
5059 if (G_UNLIKELY (avi->seg_event)) {
5060 gst_avi_demux_push_event (avi, avi->seg_event);
5061 avi->seg_event = NULL;
5063 if (G_UNLIKELY (avi->got_tags)) {
5064 push_tag_lists (avi);
5066 /* process each index entry in turn */
5067 res = gst_avi_demux_loop_data (avi);
5069 /* pause when error */
5070 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5071 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
5076 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
5077 res = GST_FLOW_ERROR;
5085 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
5086 avi->segment_running = FALSE;
5087 gst_pad_pause_task (avi->sinkpad);
5089 if (GST_FLOW_IS_FATAL (res) || (res == GST_FLOW_NOT_LINKED)) {
5090 gboolean push_eos = TRUE;
5092 if (res == GST_FLOW_UNEXPECTED) {
5093 /* handle end-of-stream/segment */
5094 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5097 if ((stop = avi->segment.stop) == -1)
5098 stop = avi->segment.duration;
5100 GST_INFO_OBJECT (avi, "sending segment_done");
5102 gst_element_post_message
5103 (GST_ELEMENT_CAST (avi),
5104 gst_message_new_segment_done (GST_OBJECT_CAST (avi),
5105 GST_FORMAT_TIME, stop));
5109 /* for fatal errors we post an error message */
5110 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
5111 (_("Internal data stream error.")),
5112 ("streaming stopped, reason %s", gst_flow_get_name (res)));
5115 GST_INFO_OBJECT (avi, "sending eos");
5116 if (!gst_avi_demux_push_event (avi, gst_event_new_eos ()) &&
5117 (res == GST_FLOW_UNEXPECTED)) {
5118 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
5119 (NULL), ("got eos but no streams (yet)"));
5126 static GstFlowReturn
5127 gst_avi_demux_chain (GstPad * pad, GstBuffer * buf)
5130 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
5133 if (GST_BUFFER_IS_DISCONT (buf)) {
5134 GST_DEBUG_OBJECT (avi, "got DISCONT");
5135 gst_adapter_clear (avi->adapter);
5136 /* mark all streams DISCONT */
5137 for (i = 0; i < avi->num_streams; i++)
5138 avi->stream[i].discont = TRUE;
5141 GST_DEBUG ("Store %d bytes in adapter", GST_BUFFER_SIZE (buf));
5142 gst_adapter_push (avi->adapter, buf);
5144 switch (avi->state) {
5145 case GST_AVI_DEMUX_START:
5146 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
5147 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5151 case GST_AVI_DEMUX_HEADER:
5152 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
5153 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5157 case GST_AVI_DEMUX_MOVI:
5158 if (G_UNLIKELY (avi->seg_event)) {
5159 gst_avi_demux_push_event (avi, avi->seg_event);
5160 avi->seg_event = NULL;
5162 if (G_UNLIKELY (avi->got_tags)) {
5163 push_tag_lists (avi);
5165 res = gst_avi_demux_stream_data (avi);
5167 case GST_AVI_DEMUX_SEEK:
5173 /* obtain and parse indexes */
5174 if (avi->stream[0].indexes && !gst_avi_demux_read_subindexes_push (avi))
5175 /* seek in subindex read function failed */
5178 if (!avi->stream[0].indexes && !avi->have_index
5179 && avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
5180 gst_avi_demux_stream_index_push (avi);
5182 if (avi->have_index) {
5183 /* use the indexes now to construct nice durations */
5184 gst_avi_demux_calculate_durations_from_index (avi);
5186 /* still parsing indexes */
5190 GST_OBJECT_LOCK (avi);
5191 event = avi->seek_event;
5192 avi->seek_event = NULL;
5193 GST_OBJECT_UNLOCK (avi);
5195 /* calculate and perform seek */
5196 if (!avi_demux_handle_seek_push (avi, avi->sinkpad, event))
5199 gst_event_unref (event);
5200 avi->state = GST_AVI_DEMUX_MOVI;
5204 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
5205 ("Illegal internal state"));
5206 res = GST_FLOW_ERROR;
5210 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
5211 gst_flow_get_name (res));
5213 if (G_UNLIKELY (avi->abort_buffering))
5214 goto abort_buffering;
5221 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("failed to read indexes"));
5222 return GST_FLOW_ERROR;
5226 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("push mode seek failed"));
5227 return GST_FLOW_ERROR;
5231 avi->abort_buffering = FALSE;
5232 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
5233 return GST_FLOW_ERROR;
5238 gst_avi_demux_sink_activate (GstPad * sinkpad)
5240 if (gst_pad_check_pull_range (sinkpad)) {
5241 GST_DEBUG ("going to pull mode");
5242 return gst_pad_activate_pull (sinkpad, TRUE);
5244 GST_DEBUG ("going to push (streaming) mode");
5245 return gst_pad_activate_push (sinkpad, TRUE);
5250 gst_avi_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
5252 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (sinkpad));
5255 avi->segment_running = TRUE;
5256 avi->streaming = FALSE;
5257 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
5260 avi->segment_running = FALSE;
5261 return gst_pad_stop_task (sinkpad);
5266 gst_avi_demux_activate_push (GstPad * pad, gboolean active)
5268 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (pad));
5271 GST_DEBUG ("avi: activating push/chain function");
5272 avi->streaming = TRUE;
5274 /* create index for some push based seeking if not provided */
5275 GST_OBJECT_LOCK (avi);
5276 if (!avi->element_index) {
5277 GST_DEBUG_OBJECT (avi, "creating index");
5278 avi->element_index = gst_index_factory_make ("memindex");
5280 GST_OBJECT_UNLOCK (avi);
5281 /* object lock might be taken again */
5282 gst_index_get_writer_id (avi->element_index, GST_OBJECT_CAST (avi),
5286 GST_DEBUG ("avi: deactivating push/chain function");
5293 gst_avi_demux_set_index (GstElement * element, GstIndex * index)
5295 GstAviDemux *avi = GST_AVI_DEMUX (element);
5297 GST_OBJECT_LOCK (avi);
5298 if (avi->element_index)
5299 gst_object_unref (avi->element_index);
5301 avi->element_index = gst_object_ref (index);
5303 avi->element_index = NULL;
5305 GST_OBJECT_UNLOCK (avi);
5306 /* object lock might be taken again */
5308 gst_index_get_writer_id (index, GST_OBJECT_CAST (element), &avi->index_id);
5309 GST_DEBUG_OBJECT (avi, "Set index %" GST_PTR_FORMAT, avi->element_index);
5313 gst_avi_demux_get_index (GstElement * element)
5315 GstIndex *result = NULL;
5316 GstAviDemux *avi = GST_AVI_DEMUX (element);
5318 GST_OBJECT_LOCK (avi);
5319 if (avi->element_index)
5320 result = gst_object_ref (avi->element_index);
5321 GST_OBJECT_UNLOCK (avi);
5323 GST_DEBUG_OBJECT (avi, "Returning index %" GST_PTR_FORMAT, result);
5328 static GstStateChangeReturn
5329 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
5331 GstStateChangeReturn ret;
5332 GstAviDemux *avi = GST_AVI_DEMUX (element);
5334 switch (transition) {
5335 case GST_STATE_CHANGE_READY_TO_PAUSED:
5336 avi->streaming = FALSE;
5337 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
5343 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5344 if (ret == GST_STATE_CHANGE_FAILURE)
5347 switch (transition) {
5348 case GST_STATE_CHANGE_PAUSED_TO_READY:
5349 gst_avi_demux_reset (avi);