2 * Copyright (C) <1999> Erik Walthinsen <omega@temple-baptist.com>
3 * Copyright (C) <2006> Nokia Corporation (contact <stefan.kost@nokia.com>)
4 * Copyright (C) <2009-2010> STEricsson <benjamin.gaignard@stericsson.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 /* Element-Checklist-Version: 5 */
24 * SECTION:element-avidemux
26 * Demuxes an .avi file into raw or compressed audio and/or video streams.
28 * This element supports both push and pull-based scheduling, depending on the
29 * capabilities of the upstream elements.
32 * <title>Example launch line</title>
34 * gst-launch filesrc location=test.avi ! avidemux name=demux demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink demux.video_00 ! queue ! decodebin ! ffmpegcolorspace ! videoscale ! autovideosink
35 * ]| Play (parse and decode) an .avi file and try to output it to
36 * an automatically detected soundcard and videosink. If the AVI file contains
37 * compressed audio or video data, this will only work if you have the
38 * right decoder elements/plugins installed.
41 * Last reviewed on 2006-12-29 (0.10.6)
51 #include "gst/riff/riff-media.h"
52 #include "gstavidemux.h"
54 #include <gst/gst-i18n-plugin.h>
55 #include <gst/base/gstadapter.h>
58 #define DIV_ROUND_UP(s,v) (((s) + ((v)-1)) / (v))
60 #define GST_AVI_KEYFRAME 1
61 #define ENTRY_IS_KEYFRAME(e) ((e)->flags == GST_AVI_KEYFRAME)
62 #define ENTRY_SET_KEYFRAME(e) ((e)->flags = GST_AVI_KEYFRAME)
63 #define ENTRY_UNSET_KEYFRAME(e) ((e)->flags = 0)
66 GST_DEBUG_CATEGORY_STATIC (avidemux_debug);
67 #define GST_CAT_DEFAULT avidemux_debug
69 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
72 GST_STATIC_CAPS ("video/x-msvideo")
75 static void gst_avi_demux_finalize (GObject * object);
77 static void gst_avi_demux_reset (GstAviDemux * avi);
80 static const GstEventMask *gst_avi_demux_get_event_mask (GstPad * pad);
82 static gboolean gst_avi_demux_handle_src_event (GstPad * pad,
83 GstObject * parent, GstEvent * event);
84 static gboolean gst_avi_demux_handle_sink_event (GstPad * pad,
85 GstObject * parent, GstEvent * event);
86 static gboolean gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event);
89 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
91 static gboolean gst_avi_demux_handle_src_query (GstPad * pad,
92 GstObject * parent, GstQuery * query);
93 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
94 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
96 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
97 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
99 static gboolean gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
101 static void gst_avi_demux_loop (GstPad * pad);
102 static gboolean gst_avi_demux_sink_activate (GstPad * sinkpad,
104 static gboolean gst_avi_demux_sink_activate_mode (GstPad * sinkpad,
105 GstObject * parent, GstPadMode mode, gboolean active);
106 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstObject * parent,
109 static void gst_avi_demux_set_index (GstElement * element, GstIndex * index);
110 static GstIndex *gst_avi_demux_get_index (GstElement * element);
112 static GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
113 GstStateChange transition);
114 static void gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi);
115 static void gst_avi_demux_get_buffer_info (GstAviDemux * avi,
116 GstAviStream * stream, guint entry_n, GstClockTime * timestamp,
117 GstClockTime * ts_end, guint64 * offset, guint64 * offset_end);
119 static void gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf);
121 /* GObject methods */
123 #define gst_avi_demux_parent_class parent_class
124 G_DEFINE_TYPE (GstAviDemux, gst_avi_demux, GST_TYPE_ELEMENT);
127 gst_avi_demux_class_init (GstAviDemuxClass * klass)
129 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
130 GObjectClass *gobject_class = (GObjectClass *) klass;
131 GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl;
132 GstCaps *audcaps, *vidcaps, *subcaps;
134 GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
135 0, "Demuxer for AVI streams");
137 gobject_class->finalize = gst_avi_demux_finalize;
139 gstelement_class->change_state =
140 GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
142 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_avi_demux_set_index);
143 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_avi_demux_get_index);
146 audcaps = gst_riff_create_audio_template_caps ();
147 gst_caps_append (audcaps, gst_caps_new_empty_simple ("audio/x-avi-unknown"));
148 audiosrctempl = gst_pad_template_new ("audio_%u",
149 GST_PAD_SRC, GST_PAD_SOMETIMES, audcaps);
151 vidcaps = gst_riff_create_video_template_caps ();
152 gst_caps_append (vidcaps, gst_riff_create_iavs_template_caps ());
153 gst_caps_append (vidcaps, gst_caps_new_empty_simple ("video/x-avi-unknown"));
154 videosrctempl = gst_pad_template_new ("video_%u",
155 GST_PAD_SRC, GST_PAD_SOMETIMES, vidcaps);
157 subcaps = gst_caps_new_empty_simple ("application/x-subtitle-avi");
158 subsrctempl = gst_pad_template_new ("subtitle_%u",
159 GST_PAD_SRC, GST_PAD_SOMETIMES, subcaps);
160 gst_element_class_add_pad_template (gstelement_class, audiosrctempl);
161 gst_element_class_add_pad_template (gstelement_class, videosrctempl);
162 gst_element_class_add_pad_template (gstelement_class, subsrctempl);
163 gst_element_class_add_pad_template (gstelement_class,
164 gst_static_pad_template_get (&sink_templ));
166 gst_element_class_set_static_metadata (gstelement_class, "Avi demuxer",
168 "Demultiplex an avi file into audio and video",
169 "Erik Walthinsen <omega@cse.ogi.edu>, "
170 "Wim Taymans <wim.taymans@chello.be>, "
171 "Thijs Vermeir <thijsvermeir@gmail.com>");
175 gst_avi_demux_init (GstAviDemux * avi)
177 avi->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
178 gst_pad_set_activate_function (avi->sinkpad,
179 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate));
180 gst_pad_set_activatemode_function (avi->sinkpad,
181 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate_mode));
182 gst_pad_set_chain_function (avi->sinkpad,
183 GST_DEBUG_FUNCPTR (gst_avi_demux_chain));
184 gst_pad_set_event_function (avi->sinkpad,
185 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_sink_event));
186 gst_element_add_pad (GST_ELEMENT_CAST (avi), avi->sinkpad);
188 avi->adapter = gst_adapter_new ();
190 gst_avi_demux_reset (avi);
192 GST_OBJECT_FLAG_SET (avi, GST_ELEMENT_FLAG_INDEXABLE);
196 gst_avi_demux_finalize (GObject * object)
198 GstAviDemux *avi = GST_AVI_DEMUX (object);
200 GST_DEBUG ("AVI: finalize");
202 g_object_unref (avi->adapter);
204 G_OBJECT_CLASS (parent_class)->finalize (object);
208 gst_avi_demux_reset_stream (GstAviDemux * avi, GstAviStream * stream)
210 g_free (stream->strh);
211 g_free (stream->strf.data);
212 g_free (stream->name);
213 g_free (stream->index);
214 g_free (stream->indexes);
215 if (stream->initdata)
216 gst_buffer_unref (stream->initdata);
217 if (stream->extradata)
218 gst_buffer_unref (stream->extradata);
220 if (stream->exposed) {
221 gst_pad_set_active (stream->pad, FALSE);
222 gst_element_remove_pad (GST_ELEMENT_CAST (avi), stream->pad);
224 gst_object_unref (stream->pad);
226 if (stream->taglist) {
227 gst_tag_list_free (stream->taglist);
228 stream->taglist = NULL;
230 memset (stream, 0, sizeof (GstAviStream));
234 gst_avi_demux_reset (GstAviDemux * avi)
238 GST_DEBUG ("AVI: reset");
240 for (i = 0; i < avi->num_streams; i++)
241 gst_avi_demux_reset_stream (avi, &avi->stream[i]);
243 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
244 avi->num_streams = 0;
245 avi->num_v_streams = 0;
246 avi->num_a_streams = 0;
247 avi->num_t_streams = 0;
248 avi->main_stream = -1;
250 avi->state = GST_AVI_DEMUX_START;
252 avi->building_index = FALSE;
254 avi->index_offset = 0;
259 if (avi->element_index)
260 gst_object_unref (avi->element_index);
261 avi->element_index = NULL;
264 if (avi->seg_event) {
265 gst_event_unref (avi->seg_event);
266 avi->seg_event = NULL;
268 if (avi->seek_event) {
269 gst_event_unref (avi->seek_event);
270 avi->seek_event = NULL;
274 gst_tag_list_free (avi->globaltags);
275 avi->globaltags = NULL;
277 avi->got_tags = TRUE; /* we always want to push global tags */
278 avi->have_eos = FALSE;
279 avi->seekable = TRUE;
281 gst_adapter_clear (avi->adapter);
283 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
287 /* GstElement methods */
290 static const GstFormat *
291 gst_avi_demux_get_src_formats (GstPad * pad)
293 GstAviStream *stream = gst_pad_get_element_private (pad);
295 static const GstFormat src_a_formats[] = {
301 static const GstFormat src_v_formats[] = {
307 return (stream->strh->type == GST_RIFF_FCC_auds ?
308 src_a_formats : src_v_formats);
312 /* assumes stream->strf.auds->av_bps != 0 */
313 static inline GstClockTime
314 avi_stream_convert_bytes_to_time_unchecked (GstAviStream * stream,
317 return gst_util_uint64_scale_int (bytes, GST_SECOND,
318 stream->strf.auds->av_bps);
321 static inline guint64
322 avi_stream_convert_time_to_bytes_unchecked (GstAviStream * stream,
325 return gst_util_uint64_scale_int (time, stream->strf.auds->av_bps,
329 /* assumes stream->strh->rate != 0 */
330 static inline GstClockTime
331 avi_stream_convert_frames_to_time_unchecked (GstAviStream * stream,
334 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
338 static inline guint64
339 avi_stream_convert_time_to_frames_unchecked (GstAviStream * stream,
342 return gst_util_uint64_scale (time, stream->strh->rate,
343 stream->strh->scale * GST_SECOND);
347 gst_avi_demux_src_convert (GstPad * pad,
348 GstFormat src_format,
349 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
351 GstAviStream *stream = gst_pad_get_element_private (pad);
355 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
356 ", dest_format:%s", gst_format_get_name (src_format), src_value,
357 gst_format_get_name (*dest_format));
359 if (G_UNLIKELY (src_format == *dest_format)) {
360 *dest_value = src_value;
363 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
367 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
368 (src_format == GST_FORMAT_BYTES
369 || *dest_format == GST_FORMAT_BYTES))) {
374 switch (src_format) {
375 case GST_FORMAT_TIME:
376 switch (*dest_format) {
377 case GST_FORMAT_BYTES:
378 *dest_value = gst_util_uint64_scale_int (src_value,
379 stream->strf.auds->av_bps, GST_SECOND);
381 case GST_FORMAT_DEFAULT:
383 gst_util_uint64_scale_round (src_value, stream->strh->rate,
384 stream->strh->scale * GST_SECOND);
391 case GST_FORMAT_BYTES:
392 switch (*dest_format) {
393 case GST_FORMAT_TIME:
394 if (stream->strf.auds->av_bps != 0) {
395 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
405 case GST_FORMAT_DEFAULT:
406 switch (*dest_format) {
407 case GST_FORMAT_TIME:
409 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
422 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
423 gst_format_get_name (*dest_format), *dest_value);
428 gst_avi_demux_handle_src_query (GstPad * pad, GstObject * parent,
432 GstAviDemux *avi = GST_AVI_DEMUX (parent);
434 GstAviStream *stream = gst_pad_get_element_private (pad);
436 if (!stream->strh || !stream->strf.data)
437 return gst_pad_query_default (pad, parent, query);
439 switch (GST_QUERY_TYPE (query)) {
440 case GST_QUERY_POSITION:{
443 GST_DEBUG ("pos query for stream %u: frames %u, bytes %u",
444 stream->num, stream->current_entry, stream->current_total);
446 /* FIXME, this looks clumsy */
447 if (stream->strh->type == GST_RIFF_FCC_auds) {
448 if (stream->is_vbr) {
450 pos = gst_util_uint64_scale ((gint64) stream->current_entry *
451 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
452 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
453 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
454 } else if (stream->strf.auds->av_bps != 0) {
456 pos = gst_util_uint64_scale (stream->current_total, GST_SECOND,
457 (guint64) stream->strf.auds->av_bps);
458 GST_DEBUG_OBJECT (avi,
459 "CBR convert bytes %u, time %" GST_TIME_FORMAT,
460 stream->current_total, GST_TIME_ARGS (pos));
461 } else if (stream->idx_n != 0 && stream->total_bytes != 0) {
462 /* calculate timestamps based on percentage of length */
463 guint64 xlen = avi->avih->us_frame *
464 avi->avih->tot_frames * GST_USECOND;
466 if (stream->is_vbr) {
467 pos = gst_util_uint64_scale (xlen, stream->current_entry,
469 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
470 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
472 pos = gst_util_uint64_scale (xlen, stream->current_total,
473 stream->total_bytes);
474 GST_DEBUG_OBJECT (avi,
475 "CBR perc convert bytes %u, time %" GST_TIME_FORMAT,
476 stream->current_total, GST_TIME_ARGS (pos));
483 if (stream->strh->rate != 0) {
484 pos = gst_util_uint64_scale ((guint64) stream->current_entry *
485 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
487 pos = stream->current_entry * avi->avih->us_frame * GST_USECOND;
491 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
492 gst_query_set_position (query, GST_FORMAT_TIME, pos);
494 GST_WARNING ("pos query failed");
497 case GST_QUERY_DURATION:
500 GstClockTime duration;
502 /* only act on audio or video streams */
503 if (stream->strh->type != GST_RIFF_FCC_auds &&
504 stream->strh->type != GST_RIFF_FCC_vids) {
509 /* take stream duration, fall back to avih duration */
510 if ((duration = stream->duration) == -1)
511 duration = avi->duration;
513 gst_query_parse_duration (query, &fmt, NULL);
516 case GST_FORMAT_TIME:
517 gst_query_set_duration (query, fmt, duration);
519 case GST_FORMAT_DEFAULT:
522 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
525 if (stream->idx_n > 0)
526 gst_query_set_duration (query, fmt, stream->idx_n);
527 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
528 duration, fmt, &dur))
529 gst_query_set_duration (query, fmt, dur);
538 case GST_QUERY_SEEKING:{
541 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
542 if (fmt == GST_FORMAT_TIME) {
543 gboolean seekable = TRUE;
545 if (avi->streaming) {
546 seekable = avi->seekable;
549 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
550 0, stream->duration);
555 case GST_QUERY_CONVERT:{
556 GstFormat src_fmt, dest_fmt;
557 gint64 src_val, dest_val;
559 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
560 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
562 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
564 res = gst_pad_query_default (pad, parent, query);
568 res = gst_pad_query_default (pad, parent, query);
576 static const GstEventMask *
577 gst_avi_demux_get_event_mask (GstPad * pad)
579 static const GstEventMask masks[] = {
580 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
590 gst_avi_demux_seek_streams (GstAviDemux * avi, guint64 offset, gboolean before)
592 GstAviStream *stream;
593 GstIndexEntry *entry;
595 gint64 val, min = offset;
597 for (i = 0; i < avi->num_streams; i++) {
598 stream = &avi->stream[i];
600 entry = gst_index_get_assoc_entry (avi->element_index, stream->index_id,
601 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
602 GST_ASSOCIATION_FLAG_NONE, GST_FORMAT_BYTES, offset);
606 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
607 GST_DEBUG_OBJECT (avi, "stream %d, previous entry at %"
608 G_GUINT64_FORMAT, i, val);
616 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
617 stream->current_entry = 0;
618 stream->current_total = 0;
622 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
623 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT,
626 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &val);
627 stream->current_total = val;
628 gst_index_entry_assoc_map (entry, GST_FORMAT_DEFAULT, &val);
629 stream->current_entry = val;
637 gst_avi_demux_index_entry_offset_search (GstAviIndexEntry * entry,
640 if (entry->offset < *offset)
642 else if (entry->offset > *offset)
648 gst_avi_demux_seek_streams_index (GstAviDemux * avi, guint64 offset,
651 GstAviStream *stream;
652 GstAviIndexEntry *entry;
654 gint64 val, min = offset;
657 for (i = 0; i < avi->num_streams; i++) {
658 stream = &avi->stream[i];
660 /* compensate for chunk header */
663 gst_util_array_binary_search (stream->index, stream->idx_n,
664 sizeof (GstAviIndexEntry),
665 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
666 before ? GST_SEARCH_MODE_BEFORE : GST_SEARCH_MODE_AFTER, &offset, NULL);
670 index = entry - stream->index;
674 val = stream->index[index].offset;
675 GST_DEBUG_OBJECT (avi,
676 "stream %d, previous entry at %" G_GUINT64_FORMAT, i, val);
684 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
685 stream->current_entry = 0;
686 stream->current_total = 0;
690 val = stream->index[index].offset - 8;
691 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT, i,
694 stream->current_total = stream->index[index].total;
695 stream->current_entry = index;
701 #define GST_AVI_SEEK_PUSH_DISPLACE (4 * GST_SECOND)
704 gst_avi_demux_handle_sink_event (GstPad * pad, GstObject * parent,
708 GstAviDemux *avi = GST_AVI_DEMUX (parent);
710 GST_DEBUG_OBJECT (avi,
711 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
713 switch (GST_EVENT_TYPE (event)) {
714 case GST_EVENT_SEGMENT:
716 gint64 boffset, offset = 0;
719 /* some debug output */
720 gst_event_copy_segment (event, &segment);
721 GST_DEBUG_OBJECT (avi, "received newsegment %" GST_SEGMENT_FORMAT,
724 /* chain will send initial newsegment after pads have been added */
725 if (avi->state != GST_AVI_DEMUX_MOVI) {
726 GST_DEBUG_OBJECT (avi, "still starting, eating event");
730 /* we only expect a BYTE segment, e.g. following a seek */
731 if (segment.format != GST_FORMAT_BYTES) {
732 GST_DEBUG_OBJECT (avi, "unsupported segment format, ignoring");
736 if (avi->have_index) {
737 GstAviIndexEntry *entry;
738 guint i = 0, index = 0, k = 0;
739 GstAviStream *stream;
741 /* compensate chunk header, stored index offset points after header */
742 boffset = segment.start + 8;
743 /* find which stream we're on */
745 stream = &avi->stream[i];
747 /* find the index for start bytes offset */
748 entry = gst_util_array_binary_search (stream->index,
749 stream->idx_n, sizeof (GstAviIndexEntry),
750 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
751 GST_SEARCH_MODE_AFTER, &boffset, NULL);
755 index = entry - stream->index;
757 /* we are on the stream with a chunk start offset closest to start */
758 if (!offset || stream->index[index].offset < offset) {
759 offset = stream->index[index].offset;
762 /* exact match needs no further searching */
763 if (stream->index[index].offset == segment.start)
765 } while (++i < avi->num_streams);
768 stream = &avi->stream[k];
770 /* so we have no idea what is to come, or where we are */
772 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
776 /* get the ts corresponding to start offset bytes for the stream */
777 gst_avi_demux_get_buffer_info (avi, stream, index,
778 (GstClockTime *) & segment.time, NULL, NULL, NULL);
780 } else if (avi->element_index) {
781 GstIndexEntry *entry;
783 /* Let's check if we have an index entry for this position */
784 entry = gst_index_get_assoc_entry (avi->element_index, avi->index_id,
785 GST_INDEX_LOOKUP_AFTER, GST_ASSOCIATION_FLAG_NONE,
786 GST_FORMAT_BYTES, segment.start);
788 /* we can not go where we have not yet been before ... */
790 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
794 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME,
795 (gint64 *) & segment.time);
796 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &offset);
799 GST_WARNING_OBJECT (avi, "no index data, forcing EOS");
803 segment.format = GST_FORMAT_TIME;
804 segment.start = segment.time;
805 segment.stop = GST_CLOCK_TIME_NONE;
806 segment.position = segment.start;
808 /* rescue duration */
809 segment.duration = avi->segment.duration;
811 /* set up segment and send downstream */
812 gst_segment_copy_into (&segment, &avi->segment);
814 GST_DEBUG_OBJECT (avi, "Pushing newseg %" GST_SEGMENT_FORMAT, &segment);
815 gst_avi_demux_push_event (avi, gst_event_new_segment (&segment));
817 GST_DEBUG_OBJECT (avi, "next chunk expected at %" G_GINT64_FORMAT,
820 /* adjust state for streaming thread accordingly */
822 gst_avi_demux_seek_streams_index (avi, offset, FALSE);
825 gst_avi_demux_seek_streams (avi, offset, FALSE);
828 /* set up streaming thread */
829 g_assert (offset >= boffset);
830 avi->offset = boffset;
831 avi->todrop = offset - boffset;
834 gst_event_unref (event);
839 avi->have_eos = TRUE;
844 if (avi->state != GST_AVI_DEMUX_MOVI) {
845 gst_event_unref (event);
846 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
847 (NULL), ("got eos and didn't receive a complete header object"));
848 } else if (!gst_avi_demux_push_event (avi, event)) {
849 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
850 (NULL), ("got eos but no streams (yet)"));
854 case GST_EVENT_FLUSH_STOP:
858 gst_adapter_clear (avi->adapter);
859 avi->have_eos = FALSE;
860 for (i = 0; i < avi->num_streams; i++) {
861 avi->stream[i].last_flow = GST_FLOW_OK;
862 avi->stream[i].discont = TRUE;
864 /* fall through to default case so that the event gets passed downstream */
867 res = gst_pad_event_default (pad, parent, event);
875 gst_avi_demux_handle_src_event (GstPad * pad, GstObject * parent,
879 GstAviDemux *avi = GST_AVI_DEMUX (parent);
881 GST_DEBUG_OBJECT (avi,
882 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
884 switch (GST_EVENT_TYPE (event)) {
886 if (!avi->streaming) {
887 res = gst_avi_demux_handle_seek (avi, pad, event);
889 res = gst_avi_demux_handle_seek_push (avi, pad, event);
891 gst_event_unref (event);
894 case GST_EVENT_NAVIGATION:
896 gst_event_unref (event);
899 res = gst_pad_event_default (pad, parent, event);
906 /* streaming helper (push) */
909 * gst_avi_demux_peek_chunk_info:
911 * @tag: holder for tag
912 * @size: holder for tag size
914 * Peek next chunk info (tag and size)
916 * Returns: TRUE when one chunk info has been got
919 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
921 const guint8 *data = NULL;
923 if (gst_adapter_available (avi->adapter) < 8)
926 data = gst_adapter_map (avi->adapter, 8);
927 *tag = GST_READ_UINT32_LE (data);
928 *size = GST_READ_UINT32_LE (data + 4);
929 gst_adapter_unmap (avi->adapter);
935 * gst_avi_demux_peek_chunk:
937 * @tag: holder for tag
938 * @size: holder for tag size
940 * Peek enough data for one full chunk
942 * Returns: %TRUE when one chunk has been got
945 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
947 guint32 peek_size = 0;
950 if (!gst_avi_demux_peek_chunk_info (avi, tag, size))
953 /* size 0 -> empty data buffer would surprise most callers,
954 * large size -> do not bother trying to squeeze that into adapter,
955 * so we throw poor man's exception, which can be caught if caller really
956 * wants to handle 0 size chunk */
957 if (!(*size) || (*size) >= (1 << 30))
960 peek_size = (*size + 1) & ~1;
961 available = gst_adapter_available (avi->adapter);
963 GST_DEBUG_OBJECT (avi,
964 "Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
965 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
967 if (available < (8 + peek_size))
975 GST_INFO_OBJECT (avi, "Failed to peek");
980 GST_INFO_OBJECT (avi,
981 "Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT, *size,
982 GST_FOURCC_ARGS (*tag));
983 /* chain should give up */
984 avi->abort_buffering = TRUE;
989 GST_INFO_OBJECT (avi, "need more %d < %" G_GUINT32_FORMAT,
990 available, 8 + peek_size);
998 * gst_avi_demux_parse_file_header:
999 * @element: caller element (used for errors/debug).
1000 * @buf: input data to be used for parsing.
1002 * "Open" a RIFF/AVI file. The buffer should be at least 12
1003 * bytes long. Takes ownership of @buf.
1005 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
1006 * Throws an error, caller should error out (fatal).
1009 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
1014 stamp = gst_util_get_timestamp ();
1016 /* riff_parse posts an error */
1017 if (!gst_riff_parse_file_header (element, buf, &doctype))
1020 if (doctype != GST_RIFF_RIFF_AVI)
1023 stamp = gst_util_get_timestamp () - stamp;
1024 GST_DEBUG_OBJECT (element, "header parsing took %" GST_TIME_FORMAT,
1025 GST_TIME_ARGS (stamp));
1032 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
1033 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
1034 GST_FOURCC_ARGS (doctype)));
1040 * Read AVI file tag when streaming
1042 static GstFlowReturn
1043 gst_avi_demux_stream_init_push (GstAviDemux * avi)
1045 if (gst_adapter_available (avi->adapter) >= 12) {
1048 tmp = gst_adapter_take_buffer (avi->adapter, 12);
1050 GST_DEBUG ("Parsing avi header");
1051 if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), tmp)) {
1052 return GST_FLOW_ERROR;
1054 GST_DEBUG ("header ok");
1057 avi->state = GST_AVI_DEMUX_HEADER;
1065 static GstFlowReturn
1066 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
1069 GstBuffer *buf = NULL;
1071 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
1072 if (res != GST_FLOW_OK)
1074 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
1084 GST_DEBUG_OBJECT (avi, "error parsing file header");
1085 return GST_FLOW_ERROR;
1089 /* AVI header handling */
1091 * gst_avi_demux_parse_avih:
1092 * @avi: caller element (used for errors/debug).
1093 * @buf: input data to be used for parsing.
1094 * @avih: pointer to structure (filled in by function) containing
1095 * stream information (such as flags, number of streams, etc.).
1097 * Read 'avih' header. Discards buffer after use.
1099 * Returns: TRUE on success, FALSE otherwise. Throws an error if
1100 * the header is invalid. The caller should error out
1104 gst_avi_demux_parse_avih (GstAviDemux * avi,
1105 GstBuffer * buf, gst_riff_avih ** _avih)
1107 gst_riff_avih *avih;
1113 size = gst_buffer_get_size (buf);
1114 if (size < sizeof (gst_riff_avih))
1115 goto avih_too_small;
1117 avih = g_malloc (size);
1118 gst_buffer_extract (buf, 0, avih, size);
1120 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1121 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
1122 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
1123 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
1124 avih->flags = GUINT32_FROM_LE (avih->flags);
1125 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
1126 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
1127 avih->streams = GUINT32_FROM_LE (avih->streams);
1128 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
1129 avih->width = GUINT32_FROM_LE (avih->width);
1130 avih->height = GUINT32_FROM_LE (avih->height);
1131 avih->scale = GUINT32_FROM_LE (avih->scale);
1132 avih->rate = GUINT32_FROM_LE (avih->rate);
1133 avih->start = GUINT32_FROM_LE (avih->start);
1134 avih->length = GUINT32_FROM_LE (avih->length);
1138 GST_INFO_OBJECT (avi, "avih tag found:");
1139 GST_INFO_OBJECT (avi, " us_frame %u", avih->us_frame);
1140 GST_INFO_OBJECT (avi, " max_bps %u", avih->max_bps);
1141 GST_INFO_OBJECT (avi, " pad_gran %u", avih->pad_gran);
1142 GST_INFO_OBJECT (avi, " flags 0x%08x", avih->flags);
1143 GST_INFO_OBJECT (avi, " tot_frames %u", avih->tot_frames);
1144 GST_INFO_OBJECT (avi, " init_frames %u", avih->init_frames);
1145 GST_INFO_OBJECT (avi, " streams %u", avih->streams);
1146 GST_INFO_OBJECT (avi, " bufsize %u", avih->bufsize);
1147 GST_INFO_OBJECT (avi, " width %u", avih->width);
1148 GST_INFO_OBJECT (avi, " height %u", avih->height);
1149 GST_INFO_OBJECT (avi, " scale %u", avih->scale);
1150 GST_INFO_OBJECT (avi, " rate %u", avih->rate);
1151 GST_INFO_OBJECT (avi, " start %u", avih->start);
1152 GST_INFO_OBJECT (avi, " length %u", avih->length);
1155 gst_buffer_unref (buf);
1157 if (avih->us_frame != 0 && avih->tot_frames != 0)
1159 (guint64) avih->us_frame * (guint64) avih->tot_frames * 1000;
1161 avi->duration = GST_CLOCK_TIME_NONE;
1163 GST_INFO_OBJECT (avi, " header duration %" GST_TIME_FORMAT,
1164 GST_TIME_ARGS (avi->duration));
1171 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No buffer"));
1176 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
1177 ("Too small avih (%" G_GSIZE_FORMAT " available, %d needed)",
1178 size, (int) sizeof (gst_riff_avih)));
1179 gst_buffer_unref (buf);
1185 * gst_avi_demux_parse_superindex:
1186 * @avi: caller element (used for debugging/errors).
1187 * @buf: input data to use for parsing.
1188 * @locations: locations in the file (byte-offsets) that contain
1189 * the actual indexes (see get_avi_demux_parse_subindex()).
1190 * The array ends with GST_BUFFER_OFFSET_NONE.
1192 * Reads superindex (openDML-2 spec stuff) from the provided data.
1194 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
1195 * on error, but they are not fatal.
1198 gst_avi_demux_parse_superindex (GstAviDemux * avi,
1199 GstBuffer * buf, guint64 ** _indexes)
1211 gst_buffer_map (buf, &map, GST_MAP_READ);
1222 /* check type of index. The opendml2 specs state that
1223 * there should be 4 dwords per array entry. Type can be
1224 * either frame or field (and we don't care). */
1225 if (GST_READ_UINT16_LE (data) != 4 ||
1226 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
1227 GST_WARNING_OBJECT (avi,
1228 "Superindex for stream has unexpected "
1229 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1230 GST_READ_UINT16_LE (data), data[2], data[3]);
1231 bpe = GST_READ_UINT16_LE (data) * 4;
1233 num = GST_READ_UINT32_LE (&data[4]);
1235 GST_DEBUG_OBJECT (avi, "got %d indexes", num);
1237 /* this can't work out well ... */
1238 if (num > G_MAXUINT32 >> 1 || bpe < 8) {
1239 goto invalid_params;
1242 indexes = g_new (guint64, num + 1);
1243 for (i = 0; i < num; i++) {
1244 if (size < 24 + bpe * (i + 1))
1246 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
1247 GST_DEBUG_OBJECT (avi, "index %d at %" G_GUINT64_FORMAT, i, indexes[i]);
1249 indexes[i] = GST_BUFFER_OFFSET_NONE;
1250 *_indexes = indexes;
1252 gst_buffer_unmap (buf, &map);
1253 gst_buffer_unref (buf);
1260 GST_ERROR_OBJECT (avi,
1261 "Not enough data to parse superindex (%" G_GSIZE_FORMAT
1262 " available, 24 needed)", size);
1264 gst_buffer_unmap (buf, &map);
1265 gst_buffer_unref (buf);
1271 GST_ERROR_OBJECT (avi, "invalid index parameters (num = %d, bpe = %d)",
1273 gst_buffer_unmap (buf, &map);
1274 gst_buffer_unref (buf);
1279 /* add an entry to the index of a stream. @num should be an estimate of the
1280 * total amount of index entries for all streams and is used to dynamically
1281 * allocate memory for the index entries. */
1282 static inline gboolean
1283 gst_avi_demux_add_index (GstAviDemux * avi, GstAviStream * stream,
1284 guint num, GstAviIndexEntry * entry)
1286 /* ensure index memory */
1287 if (G_UNLIKELY (stream->idx_n >= stream->idx_max)) {
1288 guint idx_max = stream->idx_max;
1289 GstAviIndexEntry *new_idx;
1291 /* we need to make some more room */
1293 /* initial size guess, assume each stream has an equal amount of entries,
1294 * overshoot with at least 8K */
1295 idx_max = (num / avi->num_streams) + (8192 / sizeof (GstAviIndexEntry));
1297 idx_max += 8192 / sizeof (GstAviIndexEntry);
1298 GST_DEBUG_OBJECT (avi, "expanded index from %u to %u",
1299 stream->idx_max, idx_max);
1301 new_idx = g_try_renew (GstAviIndexEntry, stream->index, idx_max);
1302 /* out of memory, if this fails stream->index is untouched. */
1303 if (G_UNLIKELY (!new_idx))
1306 stream->index = new_idx;
1307 stream->idx_max = idx_max;
1310 /* update entry total and stream stats. The entry total can be converted to
1311 * the timestamp of the entry easily. */
1312 if (stream->strh->type == GST_RIFF_FCC_auds) {
1315 if (stream->is_vbr) {
1316 entry->total = stream->total_blocks;
1318 entry->total = stream->total_bytes;
1320 blockalign = stream->strf.auds->blockalign;
1322 stream->total_blocks += DIV_ROUND_UP (entry->size, blockalign);
1324 stream->total_blocks++;
1326 if (stream->is_vbr) {
1327 entry->total = stream->idx_n;
1329 entry->total = stream->total_bytes;
1332 stream->total_bytes += entry->size;
1333 if (ENTRY_IS_KEYFRAME (entry))
1334 stream->n_keyframes++;
1337 GST_LOG_OBJECT (avi,
1338 "Adding stream %u, index entry %d, kf %d, size %u "
1339 ", offset %" G_GUINT64_FORMAT ", total %" G_GUINT64_FORMAT, stream->num,
1340 stream->idx_n, ENTRY_IS_KEYFRAME (entry), entry->size, entry->offset,
1342 stream->index[stream->idx_n++] = *entry;
1347 /* given @entry_n in @stream, calculate info such as timestamps and
1348 * offsets for the entry. */
1350 gst_avi_demux_get_buffer_info (GstAviDemux * avi, GstAviStream * stream,
1351 guint entry_n, GstClockTime * timestamp, GstClockTime * ts_end,
1352 guint64 * offset, guint64 * offset_end)
1354 GstAviIndexEntry *entry;
1356 entry = &stream->index[entry_n];
1358 if (stream->is_vbr) {
1359 /* VBR stream next timestamp */
1360 if (stream->strh->type == GST_RIFF_FCC_auds) {
1363 avi_stream_convert_frames_to_time_unchecked (stream, entry->total);
1365 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1370 avi_stream_convert_frames_to_time_unchecked (stream, entry_n);
1372 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1375 } else if (stream->strh->type == GST_RIFF_FCC_auds) {
1376 /* constant rate stream */
1379 avi_stream_convert_bytes_to_time_unchecked (stream, entry->total);
1381 *ts_end = avi_stream_convert_bytes_to_time_unchecked (stream,
1382 entry->total + entry->size);
1384 if (stream->strh->type == GST_RIFF_FCC_vids) {
1385 /* video offsets are the frame number */
1389 *offset_end = entry_n + 1;
1391 /* no offsets for audio */
1399 /* collect and debug stats about the indexes for all streams.
1400 * This method is also responsible for filling in the stream duration
1401 * as measured by the amount of index entries.
1403 * Returns TRUE if the index is not empty, else FALSE */
1405 gst_avi_demux_do_index_stats (GstAviDemux * avi)
1407 guint total_idx = 0;
1409 #ifndef GST_DISABLE_GST_DEBUG
1410 guint total_max = 0;
1413 /* get stream stats now */
1414 for (i = 0; i < avi->num_streams; i++) {
1415 GstAviStream *stream;
1417 if (G_UNLIKELY (!(stream = &avi->stream[i])))
1419 if (G_UNLIKELY (!stream->strh))
1421 if (G_UNLIKELY (!stream->index || stream->idx_n == 0))
1424 /* we interested in the end_ts of the last entry, which is the total
1425 * duration of this stream */
1426 gst_avi_demux_get_buffer_info (avi, stream, stream->idx_n - 1,
1427 NULL, &stream->idx_duration, NULL, NULL);
1429 total_idx += stream->idx_n;
1430 #ifndef GST_DISABLE_GST_DEBUG
1431 total_max += stream->idx_max;
1433 GST_INFO_OBJECT (avi, "Stream %d, dur %" GST_TIME_FORMAT ", %6u entries, "
1434 "%5u keyframes, entry size = %2u, total size = %10u, allocated %10u",
1435 i, GST_TIME_ARGS (stream->idx_duration), stream->idx_n,
1436 stream->n_keyframes, (guint) sizeof (GstAviIndexEntry),
1437 (guint) (stream->idx_n * sizeof (GstAviIndexEntry)),
1438 (guint) (stream->idx_max * sizeof (GstAviIndexEntry)));
1440 total_idx *= sizeof (GstAviIndexEntry);
1441 #ifndef GST_DISABLE_GST_DEBUG
1442 total_max *= sizeof (GstAviIndexEntry);
1444 GST_INFO_OBJECT (avi, "%u bytes for index vs %u ideally, %u wasted",
1445 total_max, total_idx, total_max - total_idx);
1447 if (total_idx == 0) {
1448 GST_WARNING_OBJECT (avi, "Index is empty !");
1455 * gst_avi_demux_parse_subindex:
1457 * @buf: input data to use for parsing.
1458 * @stream: stream context.
1459 * @entries_list: a list (returned by the function) containing all the
1460 * indexes parsed in this specific subindex. The first
1461 * entry is also a pointer to allocated memory that needs
1462 * to be free´ed. May be NULL if no supported indexes were
1465 * Reads superindex (openDML-2 spec stuff) from the provided data.
1466 * The buffer should contain a GST_RIFF_TAG_ix?? chunk.
1468 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1469 * throw an error, caller should bail out asap.
1472 gst_avi_demux_parse_subindex (GstAviDemux * avi, GstAviStream * stream,
1484 gst_buffer_map (buf, &map, GST_MAP_READ);
1491 /* We don't support index-data yet */
1493 goto not_implemented;
1495 /* check type of index. The opendml2 specs state that
1496 * there should be 4 dwords per array entry. Type can be
1497 * either frame or field (and we don't care). */
1498 bpe = (data[2] & 0x01) ? 12 : 8;
1499 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1500 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1501 GST_WARNING_OBJECT (avi,
1502 "Superindex for stream %d has unexpected "
1503 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1504 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1505 bpe = GST_READ_UINT16_LE (data) * 4;
1507 num = GST_READ_UINT32_LE (&data[4]);
1508 baseoff = GST_READ_UINT64_LE (&data[12]);
1510 /* If there's nothing, just return ! */
1514 GST_INFO_OBJECT (avi, "Parsing subindex, nr_entries = %6d", num);
1516 for (i = 0; i < num; i++) {
1517 GstAviIndexEntry entry;
1519 if (map.size < 24 + bpe * (i + 1))
1522 /* fill in offset and size. offset contains the keyframe flag in the
1524 entry.offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1525 entry.size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1527 if (stream->strh->type == GST_RIFF_FCC_auds) {
1528 /* all audio frames are keyframes */
1529 ENTRY_SET_KEYFRAME (&entry);
1531 /* else read flags */
1532 entry.flags = (entry.size & 0x80000000) ? 0 : GST_AVI_KEYFRAME;
1534 entry.size &= ~0x80000000;
1537 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
1541 gst_buffer_unmap (buf, &map);
1542 gst_buffer_unref (buf);
1549 GST_ERROR_OBJECT (avi,
1550 "Not enough data to parse subindex (%" G_GSIZE_FORMAT
1551 " available, 24 needed)", map.size);
1552 goto done; /* continue */
1556 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1557 ("Subindex-is-data is not implemented"));
1558 gst_buffer_unmap (buf, &map);
1559 gst_buffer_unref (buf);
1564 GST_DEBUG_OBJECT (avi, "the index is empty");
1565 goto done; /* continue */
1569 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1570 ("Cannot allocate memory for %u*%u=%u bytes",
1571 (guint) sizeof (GstAviIndexEntry), num,
1572 (guint) sizeof (GstAviIndexEntry) * num));
1573 gst_buffer_unmap (buf, &map);
1574 gst_buffer_unref (buf);
1580 * Create and push a flushing seek event upstream
1583 perform_seek_to_offset (GstAviDemux * demux, guint64 offset)
1588 GST_DEBUG_OBJECT (demux, "Seeking to %" G_GUINT64_FORMAT, offset);
1591 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1592 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1593 GST_SEEK_TYPE_NONE, -1);
1595 res = gst_pad_push_event (demux->sinkpad, event);
1598 demux->offset = offset;
1603 * Read AVI index when streaming
1606 gst_avi_demux_read_subindexes_push (GstAviDemux * avi)
1608 guint32 tag = 0, size;
1609 GstBuffer *buf = NULL;
1612 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1614 if (avi->odml_subidxs[avi->odml_subidx] != avi->offset)
1617 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1620 /* this is the ODML chunk we expect */
1621 odml_stream = avi->odml_stream;
1623 if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + odml_stream / 10,
1624 '0' + odml_stream % 10)) &&
1625 (tag != GST_MAKE_FOURCC ('0' + odml_stream / 10,
1626 '0' + odml_stream % 10, 'i', 'x'))) {
1627 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1628 GST_FOURCC_ARGS (tag));
1632 avi->offset += 8 + GST_ROUND_UP_2 (size);
1633 /* flush chunk header so we get just the 'size' payload data */
1634 gst_adapter_flush (avi->adapter, 8);
1635 buf = gst_adapter_take_buffer (avi->adapter, size);
1637 if (!gst_avi_demux_parse_subindex (avi, &avi->stream[odml_stream], buf))
1640 /* we parsed the index, go to next subindex */
1643 if (avi->odml_subidxs[avi->odml_subidx] == GST_BUFFER_OFFSET_NONE) {
1644 /* we reached the end of the indexes for this stream, move to the next
1645 * stream to handle the first index */
1647 avi->odml_subidx = 0;
1649 if (avi->odml_stream < avi->num_streams) {
1650 /* there are more indexes */
1651 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
1653 /* we're done, get stream stats now */
1654 avi->have_index = gst_avi_demux_do_index_stats (avi);
1660 /* seek to next index */
1661 return perform_seek_to_offset (avi, avi->odml_subidxs[avi->odml_subidx]);
1668 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi)
1674 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1676 for (n = 0; n < avi->num_streams; n++) {
1677 GstAviStream *stream = &avi->stream[n];
1679 if (stream->indexes == NULL)
1682 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1683 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi), avi->sinkpad,
1684 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1686 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1687 '0' + stream->num % 10)) &&
1688 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1689 '0' + stream->num % 10, 'i', 'x'))) {
1690 /* Some ODML files (created by god knows what muxer) have a ##ix format
1691 * instead of the 'official' ix##. They are still valid though. */
1692 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1693 GST_FOURCC_ARGS (tag));
1694 gst_buffer_unref (buf);
1698 if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1702 g_free (stream->indexes);
1703 stream->indexes = NULL;
1705 /* get stream stats now */
1706 avi->have_index = gst_avi_demux_do_index_stats (avi);
1710 * gst_avi_demux_riff_parse_vprp:
1711 * @element: caller element (used for debugging/error).
1712 * @buf: input data to be used for parsing, stripped from header.
1713 * @vprp: a pointer (returned by this function) to a filled-in vprp
1714 * structure. Caller should free it.
1716 * Parses a video stream´s vprp. This function takes ownership of @buf.
1718 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1719 * should be skipped on error, but it is not fatal.
1722 gst_avi_demux_riff_parse_vprp (GstElement * element,
1723 GstBuffer * buf, gst_riff_vprp ** _vprp)
1725 gst_riff_vprp *vprp;
1729 g_return_val_if_fail (buf != NULL, FALSE);
1730 g_return_val_if_fail (_vprp != NULL, FALSE);
1732 size = gst_buffer_get_size (buf);
1734 if (size < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1737 vprp = g_malloc (size);
1738 gst_buffer_extract (buf, 0, vprp, size);
1740 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1741 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1742 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1743 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1744 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1745 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1746 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1747 vprp->width = GUINT32_FROM_LE (vprp->width);
1748 vprp->height = GUINT32_FROM_LE (vprp->height);
1749 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1753 /* calculate fields based on size */
1754 k = (size - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) / vprp->fields;
1755 if (vprp->fields > k) {
1756 GST_WARNING_OBJECT (element,
1757 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1760 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1761 GST_WARNING_OBJECT (element,
1762 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1763 GST_RIFF_VPRP_VIDEO_FIELDS);
1764 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1766 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1767 for (k = 0; k < vprp->fields; k++) {
1768 gst_riff_vprp_video_field_desc *fd;
1770 fd = &vprp->field_info[k];
1771 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1772 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1773 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1774 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1775 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1776 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1777 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1778 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1783 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1784 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1785 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1786 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1787 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1788 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1789 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1790 vprp->aspect & 0xffff);
1791 GST_INFO_OBJECT (element, " width %d", vprp->width);
1792 GST_INFO_OBJECT (element, " height %d", vprp->height);
1793 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1794 for (k = 0; k < vprp->fields; k++) {
1795 gst_riff_vprp_video_field_desc *fd;
1797 fd = &(vprp->field_info[k]);
1798 GST_INFO_OBJECT (element, " field %u description:", k);
1799 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1800 fd->compressed_bm_height);
1801 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1802 fd->compressed_bm_width);
1803 GST_INFO_OBJECT (element, " valid_bm_height %d",
1804 fd->valid_bm_height);
1805 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1806 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1807 fd->valid_bm_x_offset);
1808 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1809 fd->valid_bm_y_offset);
1810 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1811 fd->video_x_t_offset);
1812 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1815 gst_buffer_unref (buf);
1824 GST_ERROR_OBJECT (element,
1825 "Too small vprp (%" G_GSIZE_FORMAT " available, at least %d needed)",
1826 size, (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1827 gst_buffer_unref (buf);
1833 gst_avi_demux_expose_streams (GstAviDemux * avi, gboolean force)
1837 GST_DEBUG_OBJECT (avi, "force : %d", force);
1839 for (i = 0; i < avi->num_streams; i++) {
1840 GstAviStream *stream = &avi->stream[i];
1842 if (force || stream->idx_n != 0) {
1843 GST_LOG_OBJECT (avi, "Adding pad %s" GST_PTR_FORMAT,
1844 GST_PAD_NAME (stream->pad));
1845 gst_element_add_pad ((GstElement *) avi, stream->pad);
1848 if (avi->element_index)
1849 gst_index_get_writer_id (avi->element_index,
1850 GST_OBJECT_CAST (stream->pad), &stream->index_id);
1853 stream->exposed = TRUE;
1854 if (avi->main_stream == -1)
1855 avi->main_stream = i;
1857 GST_WARNING_OBJECT (avi, "Stream #%d doesn't have any entry, removing it",
1859 gst_avi_demux_reset_stream (avi, stream);
1864 /* buf contains LIST chunk data, and will be padded to even size,
1865 * since some buggy files do not account for the padding of chunks
1866 * within a LIST in the size of the LIST */
1868 gst_avi_demux_roundup_list (GstAviDemux * avi, GstBuffer ** buf)
1872 size = gst_buffer_get_size (*buf);
1874 if (G_UNLIKELY (size & 1)) {
1878 GST_DEBUG_OBJECT (avi, "rounding up dubious list size %" G_GSIZE_FORMAT,
1880 obuf = gst_buffer_new_and_alloc (size + 1);
1882 gst_buffer_map (obuf, &map, GST_MAP_WRITE);
1883 gst_buffer_extract (*buf, 0, map.data, size);
1884 /* assume 0 padding, at least makes outcome deterministic */
1886 gst_buffer_unmap (obuf, &map);
1887 gst_buffer_replace (buf, obuf);
1892 * gst_avi_demux_parse_stream:
1893 * @avi: calling element (used for debugging/errors).
1894 * @buf: input buffer used to parse the stream.
1896 * Parses all subchunks in a strl chunk (which defines a single
1897 * stream). Discards the buffer after use. This function will
1898 * increment the stream counter internally.
1900 * Returns: whether the stream was identified successfully.
1901 * Errors are not fatal. It does indicate the stream
1905 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
1907 GstAviStream *stream;
1908 GstElementClass *klass;
1909 GstPadTemplate *templ;
1910 GstBuffer *sub = NULL;
1913 gchar *codec_name = NULL, *padname = NULL;
1914 const gchar *tag_name;
1915 GstCaps *caps = NULL;
1917 GstElement *element;
1918 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
1919 gst_riff_vprp *vprp = NULL;
1921 element = GST_ELEMENT_CAST (avi);
1923 GST_DEBUG_OBJECT (avi, "Parsing stream");
1925 gst_avi_demux_roundup_list (avi, &buf);
1927 if (avi->num_streams >= GST_AVI_DEMUX_MAX_STREAMS) {
1928 GST_WARNING_OBJECT (avi,
1929 "maximum no of streams (%d) exceeded, ignoring stream",
1930 GST_AVI_DEMUX_MAX_STREAMS);
1931 gst_buffer_unref (buf);
1932 /* not a fatal error, let's say */
1936 stream = &avi->stream[avi->num_streams];
1938 /* initial settings */
1939 stream->idx_duration = GST_CLOCK_TIME_NONE;
1940 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1941 stream->duration = GST_CLOCK_TIME_NONE;
1943 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
1944 /* sub can be NULL if the chunk is empty */
1946 GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
1947 GST_FOURCC_ARGS (tag));
1951 case GST_RIFF_TAG_strh:
1953 gst_riff_strh *strh;
1956 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
1959 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
1960 /* ownership given away */
1962 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
1966 strh = stream->strh;
1967 /* sanity check; stream header frame rate matches global header
1969 if (stream->strh->type == GST_RIFF_FCC_vids) {
1971 GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
1973 s_dur = gst_util_uint64_scale (GST_SECOND, strh->scale, strh->rate);
1974 GST_DEBUG_OBJECT (avi, "verifying stream framerate %d/%d, "
1975 "frame duration = %d ms", strh->rate, strh->scale,
1976 (gint) (s_dur / GST_MSECOND));
1977 if (h_dur > (10 * GST_MSECOND) && (s_dur > 10 * h_dur)) {
1978 strh->rate = GST_SECOND / GST_USECOND;
1979 strh->scale = h_dur / GST_USECOND;
1980 GST_DEBUG_OBJECT (avi, "correcting stream framerate to %d/%d",
1981 strh->rate, strh->scale);
1984 /* determine duration as indicated by header */
1985 stream->hdr_duration = gst_util_uint64_scale ((guint64) strh->length *
1986 strh->scale, GST_SECOND, (guint64) strh->rate);
1987 GST_INFO ("Stream duration according to header: %" GST_TIME_FORMAT,
1988 GST_TIME_ARGS (stream->hdr_duration));
1989 if (stream->hdr_duration == 0)
1990 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1995 case GST_RIFF_TAG_strf:
1997 gboolean res = FALSE;
2000 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
2004 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
2007 switch (stream->strh->type) {
2008 case GST_RIFF_FCC_vids:
2009 stream->is_vbr = TRUE;
2010 res = gst_riff_parse_strf_vids (element, sub,
2011 &stream->strf.vids, &stream->extradata);
2013 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
2015 case GST_RIFF_FCC_auds:
2017 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
2018 &stream->extradata);
2022 stream->is_vbr = (stream->strh->samplesize == 0)
2023 && stream->strh->scale > 1
2024 && stream->strf.auds->blockalign != 1;
2025 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
2026 stream->is_vbr, res);
2027 /* we need these or we have no way to come up with timestamps */
2028 if ((!stream->is_vbr && !stream->strf.auds->av_bps) ||
2029 (stream->is_vbr && (!stream->strh->scale ||
2030 !stream->strh->rate))) {
2031 GST_WARNING_OBJECT (element,
2032 "invalid audio header, ignoring stream");
2035 /* some more sanity checks */
2036 if (stream->is_vbr) {
2037 if (stream->strf.auds->blockalign <= 4) {
2038 /* that would mean (too) many frames per chunk,
2039 * so not likely set as expected */
2040 GST_DEBUG_OBJECT (element,
2041 "suspicious blockalign %d for VBR audio; "
2042 "overriding to 1 frame per chunk",
2043 stream->strf.auds->blockalign);
2044 /* this should top any likely value */
2045 stream->strf.auds->blockalign = (1 << 12);
2049 case GST_RIFF_FCC_iavs:
2050 stream->is_vbr = TRUE;
2051 res = gst_riff_parse_strf_iavs (element, sub,
2052 &stream->strf.iavs, &stream->extradata);
2054 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
2056 case GST_RIFF_FCC_txts:
2057 /* nothing to parse here */
2058 stream->is_vbr = (stream->strh->samplesize == 0)
2059 && (stream->strh->scale > 1);
2063 GST_ERROR_OBJECT (avi,
2064 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
2065 GST_FOURCC_ARGS (stream->strh->type));
2069 gst_buffer_unref (sub);
2077 case GST_RIFF_TAG_vprp:
2080 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
2084 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
2088 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
2092 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
2093 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
2094 /* not considered fatal */
2102 case GST_RIFF_TAG_strd:
2103 if (stream->initdata)
2104 gst_buffer_unref (stream->initdata);
2105 stream->initdata = sub;
2108 case GST_RIFF_TAG_strn:
2109 g_free (stream->name);
2113 gst_buffer_map (sub, &map, GST_MAP_READ);
2114 stream->name = g_strndup ((gchar *) map.data, map.size);
2115 gst_buffer_unmap (sub, &map);
2116 gst_buffer_unref (sub);
2119 stream->name = g_strdup ("");
2121 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
2124 gst_avi_demux_parse_idit (avi, sub);
2127 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
2128 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
2129 '0' + avi->num_streams % 10)) {
2130 g_free (stream->indexes);
2131 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
2132 stream->superindex = TRUE;
2136 GST_WARNING_OBJECT (avi,
2137 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
2138 GST_FOURCC_ARGS (tag));
2140 case GST_RIFF_TAG_JUNQ:
2141 case GST_RIFF_TAG_JUNK:
2145 gst_buffer_unref (sub);
2151 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
2156 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
2160 /* get class to figure out the template */
2161 klass = GST_ELEMENT_GET_CLASS (avi);
2163 /* we now have all info, let´s set up a pad and a caps and be done */
2164 /* create stream name + pad */
2165 switch (stream->strh->type) {
2166 case GST_RIFF_FCC_vids:{
2169 fourcc = (stream->strf.vids->compression) ?
2170 stream->strf.vids->compression : stream->strh->fcc_handler;
2171 padname = g_strdup_printf ("video_%u", avi->num_v_streams);
2172 templ = gst_element_class_get_pad_template (klass, "video_%u");
2173 caps = gst_riff_create_video_caps (fourcc, stream->strh,
2174 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
2176 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2177 G_TYPE_INT, fourcc, NULL);
2178 } else if (got_vprp && vprp) {
2179 guint32 aspect_n, aspect_d;
2182 aspect_n = vprp->aspect >> 16;
2183 aspect_d = vprp->aspect & 0xffff;
2184 /* calculate the pixel aspect ratio using w/h and aspect ratio */
2185 n = aspect_n * stream->strf.vids->height;
2186 d = aspect_d * stream->strf.vids->width;
2188 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
2190 /* very local, not needed elsewhere */
2194 tag_name = GST_TAG_VIDEO_CODEC;
2195 avi->num_v_streams++;
2198 case GST_RIFF_FCC_auds:{
2199 /* FIXME: Do something with the channel reorder map */
2200 padname = g_strdup_printf ("audio_%u", avi->num_a_streams);
2201 templ = gst_element_class_get_pad_template (klass, "audio_%u");
2202 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
2203 stream->strh, stream->strf.auds, stream->extradata,
2204 stream->initdata, &codec_name, NULL);
2206 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
2207 G_TYPE_INT, stream->strf.auds->format, NULL);
2209 tag_name = GST_TAG_AUDIO_CODEC;
2210 avi->num_a_streams++;
2213 case GST_RIFF_FCC_iavs:{
2214 guint32 fourcc = stream->strh->fcc_handler;
2216 padname = g_strdup_printf ("video_%u", avi->num_v_streams);
2217 templ = gst_element_class_get_pad_template (klass, "video_%u");
2218 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
2219 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
2221 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2222 G_TYPE_INT, fourcc, NULL);
2224 tag_name = GST_TAG_VIDEO_CODEC;
2225 avi->num_v_streams++;
2228 case GST_RIFF_FCC_txts:{
2229 padname = g_strdup_printf ("subtitle_%u", avi->num_t_streams);
2230 templ = gst_element_class_get_pad_template (klass, "subtitle_%u");
2231 caps = gst_caps_new_empty_simple ("application/x-subtitle-avi");
2233 avi->num_t_streams++;
2237 g_return_val_if_reached (FALSE);
2240 /* no caps means no stream */
2242 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
2246 GST_DEBUG_OBJECT (element, "codec-name=%s",
2247 (codec_name ? codec_name : "NULL"));
2248 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
2250 /* set proper settings and add it */
2252 gst_object_unref (stream->pad);
2253 pad = stream->pad = gst_pad_new_from_template (templ, padname);
2256 gst_pad_use_fixed_caps (pad);
2258 gst_pad_set_formats_function (pad,
2259 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
2260 gst_pad_set_event_mask_function (pad,
2261 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
2263 gst_pad_set_event_function (pad,
2264 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
2265 gst_pad_set_query_function (pad,
2266 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
2268 gst_pad_set_convert_function (pad,
2269 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
2272 stream->num = avi->num_streams;
2274 stream->start_entry = 0;
2275 stream->step_entry = 0;
2276 stream->stop_entry = 0;
2278 stream->current_entry = -1;
2279 stream->current_total = 0;
2281 stream->last_flow = GST_FLOW_OK;
2282 stream->discont = TRUE;
2284 stream->total_bytes = 0;
2285 stream->total_blocks = 0;
2286 stream->n_keyframes = 0;
2289 stream->idx_max = 0;
2291 gst_pad_set_element_private (pad, stream);
2294 gst_pad_set_active (pad, TRUE);
2295 gst_pad_push_event (pad, gst_event_new_caps (caps));
2296 gst_caps_unref (caps);
2300 if (!stream->taglist)
2301 stream->taglist = gst_tag_list_new_empty ();
2303 avi->got_tags = TRUE;
2305 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
2307 g_free (codec_name);
2310 gst_buffer_unref (buf);
2317 /* unref any mem that may be in use */
2319 gst_buffer_unref (buf);
2321 gst_buffer_unref (sub);
2323 g_free (codec_name);
2324 gst_avi_demux_reset_stream (avi, stream);
2331 * gst_avi_demux_parse_odml:
2332 * @avi: calling element (used for debug/error).
2333 * @buf: input buffer to be used for parsing.
2335 * Read an openDML-2.0 extension header. Fills in the frame number
2336 * in the avi demuxer object when reading succeeds.
2339 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
2343 GstBuffer *sub = NULL;
2345 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
2348 case GST_RIFF_TAG_dmlh:{
2349 gst_riff_dmlh dmlh, *_dmlh;
2352 /* sub == NULL is possible and means an empty buffer */
2356 gst_buffer_map (sub, &map, GST_MAP_READ);
2359 if (map.size < sizeof (gst_riff_dmlh)) {
2360 GST_ERROR_OBJECT (avi,
2361 "DMLH entry is too small (%" G_GSIZE_FORMAT " bytes, %d needed)",
2362 map.size, (int) sizeof (gst_riff_dmlh));
2363 gst_buffer_unmap (sub, &map);
2366 _dmlh = (gst_riff_dmlh *) map.data;
2367 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
2368 gst_buffer_unmap (sub, &map);
2370 GST_INFO_OBJECT (avi, "dmlh tag found: totalframes: %u",
2373 avi->avih->tot_frames = dmlh.totalframes;
2378 GST_WARNING_OBJECT (avi,
2379 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
2380 GST_FOURCC_ARGS (tag));
2382 case GST_RIFF_TAG_JUNQ:
2383 case GST_RIFF_TAG_JUNK:
2385 /* skip and move to next chunk */
2387 gst_buffer_unref (sub);
2394 gst_buffer_unref (buf);
2399 gst_avi_demux_index_last (GstAviDemux * avi, GstAviStream * stream)
2401 return stream->idx_n;
2404 /* find a previous entry in the index with the given flags */
2406 gst_avi_demux_index_prev (GstAviDemux * avi, GstAviStream * stream,
2407 guint last, gboolean keyframe)
2409 GstAviIndexEntry *entry;
2412 for (i = last; i > 0; i--) {
2413 entry = &stream->index[i - 1];
2414 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2422 gst_avi_demux_index_next (GstAviDemux * avi, GstAviStream * stream,
2423 guint last, gboolean keyframe)
2425 GstAviIndexEntry *entry;
2428 for (i = last + 1; i < stream->idx_n; i++) {
2429 entry = &stream->index[i];
2430 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2434 return stream->idx_n - 1;
2438 gst_avi_demux_index_entry_search (GstAviIndexEntry * entry, guint64 * total)
2440 if (entry->total < *total)
2442 else if (entry->total > *total)
2448 * gst_avi_demux_index_for_time:
2450 * @stream: the stream
2451 * @time: a time position
2453 * Finds the index entry which time is less or equal than the requested time.
2454 * Try to avoid binary search when we can convert the time to an index
2455 * position directly (for example for video frames with a fixed duration).
2457 * Returns: the found position in the index.
2460 gst_avi_demux_index_for_time (GstAviDemux * avi,
2461 GstAviStream * stream, guint64 time)
2466 GST_LOG_OBJECT (avi, "search time:%" GST_TIME_FORMAT, GST_TIME_ARGS (time));
2468 /* easy (and common) cases */
2469 if (time == 0 || stream->idx_n == 0)
2471 if (time >= stream->idx_duration)
2472 return stream->idx_n - 1;
2474 /* figure out where we need to go. For that we convert the time to an
2475 * index entry or we convert it to a total and then do a binary search. */
2476 if (stream->is_vbr) {
2477 /* VBR stream next timestamp */
2478 if (stream->strh->type == GST_RIFF_FCC_auds) {
2479 total = avi_stream_convert_time_to_frames_unchecked (stream, time);
2481 index = avi_stream_convert_time_to_frames_unchecked (stream, time);
2484 /* constant rate stream */
2485 total = avi_stream_convert_time_to_bytes_unchecked (stream, time);
2489 GstAviIndexEntry *entry;
2491 /* no index, find index with binary search on total */
2492 GST_LOG_OBJECT (avi, "binary search for entry with total %"
2493 G_GUINT64_FORMAT, total);
2495 entry = gst_util_array_binary_search (stream->index,
2496 stream->idx_n, sizeof (GstAviIndexEntry),
2497 (GCompareDataFunc) gst_avi_demux_index_entry_search,
2498 GST_SEARCH_MODE_BEFORE, &total, NULL);
2500 if (entry == NULL) {
2501 GST_LOG_OBJECT (avi, "not found, assume index 0");
2504 index = entry - stream->index;
2505 GST_LOG_OBJECT (avi, "found at %u", index);
2508 GST_LOG_OBJECT (avi, "converted time to index %u", index);
2514 static inline GstAviStream *
2515 gst_avi_demux_stream_for_id (GstAviDemux * avi, guint32 id)
2518 GstAviStream *stream;
2520 /* get the stream for this entry */
2521 stream_nr = CHUNKID_TO_STREAMNR (id);
2522 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2523 GST_WARNING_OBJECT (avi, "invalid stream nr %d", stream_nr);
2526 stream = &avi->stream[stream_nr];
2527 if (G_UNLIKELY (!stream->strh)) {
2528 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2535 * gst_avi_demux_parse_index:
2536 * @avi: calling element (used for debugging/errors).
2537 * @buf: buffer containing the full index.
2539 * Read index entries from the provided buffer.
2540 * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
2543 gst_avi_demux_parse_index (GstAviDemux * avi, GstBuffer * buf)
2547 gst_riff_index_entry *index;
2549 GstAviStream *stream;
2550 GstAviIndexEntry entry;
2556 gst_buffer_map (buf, &map, GST_MAP_READ);
2558 stamp = gst_util_get_timestamp ();
2560 /* see how many items in the index */
2561 num = map.size / sizeof (gst_riff_index_entry);
2565 GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2567 index = (gst_riff_index_entry *) map.data;
2569 /* figure out if the index is 0 based or relative to the MOVI start */
2570 entry.offset = GST_READ_UINT32_LE (&index[0].offset);
2571 if (entry.offset < avi->offset) {
2572 avi->index_offset = avi->offset + 8;
2573 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2575 avi->index_offset = 0;
2576 GST_DEBUG ("index is 0 based");
2579 for (i = 0, n = 0; i < num; i++) {
2580 id = GST_READ_UINT32_LE (&index[i].id);
2581 entry.offset = GST_READ_UINT32_LE (&index[i].offset);
2583 /* some sanity checks */
2584 if (G_UNLIKELY (id == GST_RIFF_rec || id == 0 ||
2585 (entry.offset == 0 && n > 0)))
2588 /* get the stream for this entry */
2589 stream = gst_avi_demux_stream_for_id (avi, id);
2590 if (G_UNLIKELY (!stream))
2593 /* handle offset and size */
2594 entry.offset += avi->index_offset + 8;
2595 entry.size = GST_READ_UINT32_LE (&index[i].size);
2598 if (stream->strh->type == GST_RIFF_FCC_auds) {
2599 /* all audio frames are keyframes */
2600 ENTRY_SET_KEYFRAME (&entry);
2603 /* else read flags */
2604 flags = GST_READ_UINT32_LE (&index[i].flags);
2605 if (flags & GST_RIFF_IF_KEYFRAME) {
2606 ENTRY_SET_KEYFRAME (&entry);
2608 ENTRY_UNSET_KEYFRAME (&entry);
2613 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2618 gst_buffer_unmap (buf, &map);
2619 gst_buffer_unref (buf);
2621 /* get stream stats now */
2622 avi->have_index = gst_avi_demux_do_index_stats (avi);
2624 stamp = gst_util_get_timestamp () - stamp;
2625 GST_DEBUG_OBJECT (avi, "index parsing took %" GST_TIME_FORMAT,
2626 GST_TIME_ARGS (stamp));
2633 GST_DEBUG_OBJECT (avi, "empty index");
2634 gst_buffer_unmap (buf, &map);
2635 gst_buffer_unref (buf);
2640 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2641 ("Cannot allocate memory for %u*%u=%u bytes",
2642 (guint) sizeof (GstAviIndexEntry), num,
2643 (guint) sizeof (GstAviIndexEntry) * num));
2644 gst_buffer_unmap (buf, &map);
2645 gst_buffer_unref (buf);
2651 * gst_avi_demux_stream_index:
2652 * @avi: avi demuxer object.
2654 * Seeks to index and reads it.
2657 gst_avi_demux_stream_index (GstAviDemux * avi)
2660 guint64 offset = avi->offset;
2661 GstBuffer *buf = NULL;
2666 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2668 /* get chunk information */
2669 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2670 if (res != GST_FLOW_OK)
2673 gst_buffer_map (buf, &map, GST_MAP_READ);
2677 /* check tag first before blindy trying to read 'size' bytes */
2678 tag = GST_READ_UINT32_LE (map.data);
2679 size = GST_READ_UINT32_LE (map.data + 4);
2680 if (tag == GST_RIFF_TAG_LIST) {
2681 /* this is the movi tag */
2682 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2683 (8 + GST_ROUND_UP_2 (size)));
2684 offset += 8 + GST_ROUND_UP_2 (size);
2685 gst_buffer_unmap (buf, &map);
2686 gst_buffer_unref (buf);
2689 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2690 if (res != GST_FLOW_OK)
2693 gst_buffer_map (buf, &map, GST_MAP_READ);
2697 tag = GST_READ_UINT32_LE (map.data);
2698 size = GST_READ_UINT32_LE (map.data + 4);
2700 gst_buffer_unmap (buf, &map);
2701 gst_buffer_unref (buf);
2703 if (tag != GST_RIFF_TAG_idx1)
2708 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2710 /* read chunk, advance offset */
2711 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2712 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2715 GST_DEBUG ("will parse index chunk size %" G_GSIZE_FORMAT " for tag %"
2716 GST_FOURCC_FORMAT, gst_buffer_get_size (buf), GST_FOURCC_ARGS (tag));
2718 gst_avi_demux_parse_index (avi, buf);
2720 #ifndef GST_DISABLE_GST_DEBUG
2721 /* debug our indexes */
2724 GstAviStream *stream;
2726 for (i = 0; i < avi->num_streams; i++) {
2727 stream = &avi->stream[i];
2728 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2729 i, stream->idx_n, stream->total_bytes);
2738 GST_DEBUG_OBJECT (avi,
2739 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2744 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2745 gst_buffer_unmap (buf, &map);
2746 gst_buffer_unref (buf);
2751 GST_WARNING_OBJECT (avi,
2752 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2753 GST_FOURCC_ARGS (tag));
2758 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2764 * gst_avi_demux_stream_index_push:
2765 * @avi: avi demuxer object.
2770 gst_avi_demux_stream_index_push (GstAviDemux * avi)
2772 guint64 offset = avi->idx1_offset;
2777 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2779 /* get chunk information */
2780 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
2783 /* check tag first before blindly trying to read 'size' bytes */
2784 if (tag == GST_RIFF_TAG_LIST) {
2785 /* this is the movi tag */
2786 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2787 (8 + GST_ROUND_UP_2 (size)));
2788 avi->idx1_offset = offset + 8 + GST_ROUND_UP_2 (size);
2789 /* issue seek to allow chain function to handle it and return! */
2790 perform_seek_to_offset (avi, avi->idx1_offset);
2794 if (tag != GST_RIFF_TAG_idx1)
2797 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2799 /* flush chunk header */
2800 gst_adapter_flush (avi->adapter, 8);
2801 /* read chunk payload */
2802 buf = gst_adapter_take_buffer (avi->adapter, size);
2805 /* advance offset */
2806 offset += 8 + GST_ROUND_UP_2 (size);
2808 GST_DEBUG ("will parse index chunk size %" G_GSIZE_FORMAT " for tag %"
2809 GST_FOURCC_FORMAT, gst_buffer_get_size (buf), GST_FOURCC_ARGS (tag));
2811 avi->offset = avi->first_movi_offset;
2812 gst_avi_demux_parse_index (avi, buf);
2814 #ifndef GST_DISABLE_GST_DEBUG
2815 /* debug our indexes */
2818 GstAviStream *stream;
2820 for (i = 0; i < avi->num_streams; i++) {
2821 stream = &avi->stream[i];
2822 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2823 i, stream->idx_n, stream->total_bytes);
2832 GST_DEBUG_OBJECT (avi,
2833 "taking data from adapter failed: pos=%" G_GUINT64_FORMAT " size=%u",
2839 GST_WARNING_OBJECT (avi,
2840 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2841 GST_FOURCC_ARGS (tag));
2847 * gst_avi_demux_peek_tag:
2849 * Returns the tag and size of the next chunk
2851 static GstFlowReturn
2852 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2855 GstFlowReturn res = GST_FLOW_OK;
2856 GstBuffer *buf = NULL;
2859 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2860 if (res != GST_FLOW_OK)
2863 gst_buffer_map (buf, &map, GST_MAP_READ);
2867 *tag = GST_READ_UINT32_LE (map.data);
2868 *size = GST_READ_UINT32_LE (map.data + 4);
2870 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2871 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2872 *size, offset + 8, offset + 8 + (gint64) * size);
2875 gst_buffer_unmap (buf, &map);
2876 gst_buffer_unref (buf);
2883 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2888 GST_DEBUG_OBJECT (avi, "got %" G_GSIZE_FORMAT " bytes which is <> 8 bytes",
2890 res = GST_FLOW_ERROR;
2896 * gst_avi_demux_next_data_buffer:
2898 * Returns the offset and size of the next buffer
2899 * Position is the position of the buffer (after tag and size)
2901 static GstFlowReturn
2902 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2903 guint32 * tag, guint * size)
2905 guint64 off = *offset;
2910 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2911 if (res != GST_FLOW_OK)
2913 if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
2914 off += 8 + 4; /* skip tag + size + subtag */
2926 * gst_avi_demux_stream_scan:
2927 * @avi: calling element (used for debugging/errors).
2929 * Scan the file for all chunks to "create" a new index.
2933 gst_avi_demux_stream_scan (GstAviDemux * avi)
2936 GstAviStream *stream;
2944 * - implement non-seekable source support.
2946 GST_DEBUG_OBJECT (avi, "Creating index");
2948 /* get the size of the file */
2949 if (!gst_pad_peer_query_duration (avi->sinkpad, GST_FORMAT_BYTES, &tmplength))
2953 /* guess the total amount of entries we expect */
2957 GstAviIndexEntry entry;
2960 /* start reading data buffers to find the id and offset */
2961 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
2962 if (G_UNLIKELY (res != GST_FLOW_OK))
2966 stream = gst_avi_demux_stream_for_id (avi, tag);
2967 if (G_UNLIKELY (!stream))
2970 /* we can't figure out the keyframes, assume they all are */
2971 entry.flags = GST_AVI_KEYFRAME;
2975 /* and add to the index of this stream */
2976 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2980 /* update position */
2981 pos += GST_ROUND_UP_2 (size);
2982 if (G_UNLIKELY (pos > length)) {
2983 GST_WARNING_OBJECT (avi,
2984 "Stopping index lookup since we are further than EOF");
2990 avi->have_index = gst_avi_demux_do_index_stats (avi);
2997 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2998 ("Cannot allocate memory for %u*%u=%u bytes",
2999 (guint) sizeof (GstAviIndexEntry), num,
3000 (guint) sizeof (GstAviIndexEntry) * num));
3006 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
3010 GstAviStream *stream;
3012 total = GST_CLOCK_TIME_NONE;
3014 /* all streams start at a timestamp 0 */
3015 for (i = 0; i < avi->num_streams; i++) {
3016 GstClockTime duration, hduration;
3017 gst_riff_strh *strh;
3019 stream = &avi->stream[i];
3020 if (G_UNLIKELY (!stream || !stream->idx_n || !(strh = stream->strh)))
3023 /* get header duration for the stream */
3024 hduration = stream->hdr_duration;
3025 /* index duration calculated during parsing */
3026 duration = stream->idx_duration;
3028 /* now pick a good duration */
3029 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3030 /* index gave valid duration, use that */
3031 GST_INFO ("Stream %p duration according to index: %" GST_TIME_FORMAT,
3032 stream, GST_TIME_ARGS (duration));
3034 /* fall back to header info to calculate a duration */
3035 duration = hduration;
3037 GST_INFO ("Setting duration of stream #%d to %" GST_TIME_FORMAT,
3038 i, GST_TIME_ARGS (duration));
3039 /* set duration for the stream */
3040 stream->duration = duration;
3042 /* find total duration */
3043 if (total == GST_CLOCK_TIME_NONE ||
3044 (GST_CLOCK_TIME_IS_VALID (duration) && duration > total))
3048 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
3049 /* now update the duration for those streams where we had none */
3050 for (i = 0; i < avi->num_streams; i++) {
3051 stream = &avi->stream[i];
3053 if (!GST_CLOCK_TIME_IS_VALID (stream->duration)
3054 || stream->duration == 0) {
3055 stream->duration = total;
3057 GST_INFO ("Stream %p duration according to total: %" GST_TIME_FORMAT,
3058 stream, GST_TIME_ARGS (total));
3063 /* and set the total duration in the segment. */
3064 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
3065 GST_TIME_ARGS (total));
3067 avi->segment.duration = total;
3070 /* returns FALSE if there are no pads to deliver event to,
3071 * otherwise TRUE (whatever the outcome of event sending),
3072 * takes ownership of the event. */
3074 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
3076 gboolean result = FALSE;
3079 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
3080 GST_EVENT_TYPE_NAME (event), avi->num_streams);
3082 for (i = 0; i < avi->num_streams; i++) {
3083 GstAviStream *stream = &avi->stream[i];
3087 gst_pad_push_event (stream->pad, gst_event_ref (event));
3090 gst_event_unref (event);
3095 gst_avi_demux_check_seekability (GstAviDemux * avi)
3098 gboolean seekable = FALSE;
3099 gint64 start = -1, stop = -1;
3101 query = gst_query_new_seeking (GST_FORMAT_BYTES);
3102 if (!gst_pad_peer_query (avi->sinkpad, query)) {
3103 GST_DEBUG_OBJECT (avi, "seeking query failed");
3107 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
3109 /* try harder to query upstream size if we didn't get it the first time */
3110 if (seekable && stop == -1) {
3111 GST_DEBUG_OBJECT (avi, "doing duration query to fix up unset stop");
3112 gst_pad_peer_query_duration (avi->sinkpad, GST_FORMAT_BYTES, &stop);
3115 /* if upstream doesn't know the size, it's likely that it's not seekable in
3116 * practice even if it technically may be seekable */
3117 if (seekable && (start != 0 || stop <= start)) {
3118 GST_DEBUG_OBJECT (avi, "seekable but unknown start/stop -> disable");
3123 GST_INFO_OBJECT (avi, "seekable: %d (%" G_GUINT64_FORMAT " - %"
3124 G_GUINT64_FORMAT ")", seekable, start, stop);
3125 avi->seekable = seekable;
3127 gst_query_unref (query);
3131 * Read AVI headers when streaming
3133 static GstFlowReturn
3134 gst_avi_demux_stream_header_push (GstAviDemux * avi)
3136 GstFlowReturn ret = GST_FLOW_OK;
3141 GstBuffer *buf = NULL, *sub = NULL;
3144 GstTagList *tags = NULL;
3147 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
3149 switch (avi->header_state) {
3150 case GST_AVI_DEMUX_HEADER_TAG_LIST:
3151 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3152 avi->offset += 8 + GST_ROUND_UP_2 (size);
3153 if (tag != GST_RIFF_TAG_LIST)
3154 goto header_no_list;
3156 gst_adapter_flush (avi->adapter, 8);
3157 /* Find the 'hdrl' LIST tag */
3158 GST_DEBUG ("Reading %d bytes", size);
3159 buf = gst_adapter_take_buffer (avi->adapter, size);
3161 gst_buffer_extract (buf, 0, fourcc, 4);
3163 if (GST_READ_UINT32_LE (fourcc) != GST_RIFF_LIST_hdrl)
3164 goto header_no_hdrl;
3168 gst_adapter_flush (avi->adapter, 1);
3170 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
3172 gst_avi_demux_roundup_list (avi, &buf);
3174 /* the hdrl starts with a 'avih' header */
3175 if (!gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3177 goto header_no_avih;
3179 if (tag != GST_RIFF_TAG_avih)
3180 goto header_no_avih;
3182 if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3183 goto header_wrong_avih;
3185 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
3187 /* now, read the elements from the header until the end */
3188 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3190 /* sub can be NULL on empty tags */
3195 case GST_RIFF_TAG_LIST:
3196 if (gst_buffer_get_size (sub) < 4)
3199 gst_buffer_extract (sub, 0, fourcc, 4);
3201 switch (GST_READ_UINT32_LE (fourcc)) {
3202 case GST_RIFF_LIST_strl:
3203 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3205 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3206 ("failed to parse stream, ignoring"));
3211 case GST_RIFF_LIST_odml:
3212 gst_avi_demux_parse_odml (avi, sub);
3216 GST_WARNING_OBJECT (avi,
3217 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3218 GST_FOURCC_ARGS (GST_READ_UINT32_LE (fourcc)));
3220 case GST_RIFF_TAG_JUNQ:
3221 case GST_RIFF_TAG_JUNK:
3226 gst_avi_demux_parse_idit (avi, sub);
3229 GST_WARNING_OBJECT (avi,
3230 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
3231 offset, GST_FOURCC_ARGS (tag));
3233 case GST_RIFF_TAG_JUNQ:
3234 case GST_RIFF_TAG_JUNK:
3236 /* move to next chunk */
3238 gst_buffer_unref (sub);
3243 gst_buffer_unref (buf);
3244 GST_DEBUG ("elements parsed");
3246 /* check parsed streams */
3247 if (avi->num_streams == 0) {
3249 } else if (avi->num_streams != avi->avih->streams) {
3250 GST_WARNING_OBJECT (avi,
3251 "Stream header mentioned %d streams, but %d available",
3252 avi->avih->streams, avi->num_streams);
3254 GST_DEBUG ("Get junk and info next");
3255 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
3257 /* Need more data */
3261 case GST_AVI_DEMUX_HEADER_INFO:
3262 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
3264 if (gst_adapter_available (avi->adapter) < 12)
3267 data = gst_adapter_map (avi->adapter, 12);
3268 tag = GST_READ_UINT32_LE (data);
3269 size = GST_READ_UINT32_LE (data + 4);
3270 ltag = GST_READ_UINT32_LE (data + 8);
3271 gst_adapter_unmap (avi->adapter);
3273 if (tag == GST_RIFF_TAG_LIST) {
3275 case GST_RIFF_LIST_movi:
3276 gst_adapter_flush (avi->adapter, 12);
3277 if (!avi->first_movi_offset)
3278 avi->first_movi_offset = avi->offset;
3280 avi->idx1_offset = avi->offset + size - 4;
3282 case GST_RIFF_LIST_INFO:
3283 GST_DEBUG ("Found INFO chunk");
3284 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3285 GST_DEBUG ("got size %d", size);
3287 gst_adapter_flush (avi->adapter, 12);
3289 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
3292 gst_adapter_flush (avi->adapter, 1);
3293 gst_riff_parse_info (GST_ELEMENT_CAST (avi), buf, &tags);
3295 if (avi->globaltags) {
3296 gst_tag_list_insert (avi->globaltags, tags,
3297 GST_TAG_MERGE_REPLACE);
3299 avi->globaltags = tags;
3303 gst_buffer_unref (buf);
3305 avi->offset += GST_ROUND_UP_2 (size) - 4;
3307 GST_DEBUG ("skipping INFO LIST prefix");
3310 /* Need more data */
3315 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3316 avi->offset += 8 + GST_ROUND_UP_2 (size);
3317 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3318 // ??? goto iterate; ???
3320 /* Need more data */
3326 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3327 avi->offset += 8 + GST_ROUND_UP_2 (size);
3328 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3331 /* Need more data */
3338 GST_WARNING ("unhandled header state: %d", avi->header_state);
3343 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3344 avi->num_streams, avi->stream[0].indexes);
3346 GST_DEBUG ("Found movi chunk. Starting to stream data");
3347 avi->state = GST_AVI_DEMUX_MOVI;
3349 /* no indexes in push mode, but it still sets some variables */
3350 gst_avi_demux_calculate_durations_from_index (avi);
3352 gst_avi_demux_expose_streams (avi, TRUE);
3354 /* prepare all streams for index 0 */
3355 for (i = 0; i < avi->num_streams; i++)
3356 avi->stream[i].current_entry = 0;
3358 /* create initial NEWSEGMENT event */
3360 gst_event_unref (avi->seg_event);
3361 avi->seg_event = gst_event_new_segment (&avi->segment);
3363 gst_avi_demux_check_seekability (avi);
3365 /* at this point we know all the streams and we can signal the no more
3367 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3368 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3375 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3376 return GST_FLOW_ERROR;
3380 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3381 ("Invalid AVI header (no LIST at start): %"
3382 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3383 return GST_FLOW_ERROR;
3387 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3388 ("Invalid AVI header (no hdrl at start): %"
3389 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3390 gst_buffer_unref (buf);
3391 return GST_FLOW_ERROR;
3395 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3396 ("Invalid AVI header (no avih at start): %"
3397 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3399 gst_buffer_unref (sub);
3401 gst_buffer_unref (buf);
3402 return GST_FLOW_ERROR;
3406 gst_buffer_unref (buf);
3407 return GST_FLOW_ERROR;
3412 gst_avi_demux_add_date_tag (GstAviDemux * avi, gint y, gint m, gint d,
3413 gint h, gint min, gint s)
3418 date = g_date_new_dmy (d, m, y);
3419 if (!g_date_valid (date)) {
3421 GST_WARNING_OBJECT (avi, "Refusing to add invalid date %d-%d-%d", y, m, d);
3426 dt = gst_date_time_new_local_time (y, m, d, h, min, s);
3428 if (avi->globaltags == NULL)
3429 avi->globaltags = gst_tag_list_new_empty ();
3431 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE, date,
3435 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE_TIME,
3437 gst_date_time_unref (dt);
3442 gst_avi_demux_parse_idit_nums_only (GstAviDemux * avi, gchar * data)
3445 gint hr = 0, min = 0, sec = 0;
3448 GST_DEBUG ("data : '%s'", data);
3450 ret = sscanf (data, "%d:%d:%d %d:%d:%d", &y, &m, &d, &hr, &min, &sec);
3452 /* Attempt YYYY/MM/DD/ HH:MM variant (found in CASIO cameras) */
3453 ret = sscanf (data, "%04d/%02d/%02d/ %d:%d", &y, &m, &d, &hr, &min);
3455 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3459 gst_avi_demux_add_date_tag (avi, y, m, d, hr, min, sec);
3463 get_month_num (gchar * data, guint size)
3465 if (g_ascii_strncasecmp (data, "jan", 3) == 0) {
3467 } else if (g_ascii_strncasecmp (data, "feb", 3) == 0) {
3469 } else if (g_ascii_strncasecmp (data, "mar", 3) == 0) {
3471 } else if (g_ascii_strncasecmp (data, "apr", 3) == 0) {
3473 } else if (g_ascii_strncasecmp (data, "may", 3) == 0) {
3475 } else if (g_ascii_strncasecmp (data, "jun", 3) == 0) {
3477 } else if (g_ascii_strncasecmp (data, "jul", 3) == 0) {
3479 } else if (g_ascii_strncasecmp (data, "aug", 3) == 0) {
3481 } else if (g_ascii_strncasecmp (data, "sep", 3) == 0) {
3483 } else if (g_ascii_strncasecmp (data, "oct", 3) == 0) {
3485 } else if (g_ascii_strncasecmp (data, "nov", 3) == 0) {
3487 } else if (g_ascii_strncasecmp (data, "dec", 3) == 0) {
3495 gst_avi_demux_parse_idit_text (GstAviDemux * avi, gchar * data)
3497 gint year, month, day;
3498 gint hour, min, sec;
3503 ret = sscanf (data, "%3s %3s %d %d:%d:%d %d", weekday, monthstr, &day, &hour,
3506 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3509 month = get_month_num (monthstr, strlen (monthstr));
3510 gst_avi_demux_add_date_tag (avi, year, month, day, hour, min, sec);
3514 gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf)
3519 gchar *safedata = NULL;
3521 gst_buffer_map (buf, &map, GST_MAP_READ);
3524 * http://www.eden-foundation.org/products/code/film_date_stamp/index.html
3526 * This tag could be in one of the below formats
3527 * 2005:08:17 11:42:43
3528 * THU OCT 26 16:46:04 2006
3529 * Mon Mar 3 09:44:56 2008
3531 * FIXME: Our date tag doesn't include hours
3534 /* skip eventual initial whitespace */
3535 ptr = (gchar *) map.data;
3538 while (left > 0 && g_ascii_isspace (ptr[0])) {
3547 /* make a safe copy to add a \0 to the end of the string */
3548 safedata = g_strndup (ptr, left);
3550 /* test if the first char is a alpha or a number */
3551 if (g_ascii_isdigit (ptr[0])) {
3552 gst_avi_demux_parse_idit_nums_only (avi, safedata);
3555 } else if (g_ascii_isalpha (ptr[0])) {
3556 gst_avi_demux_parse_idit_text (avi, safedata);
3564 GST_WARNING_OBJECT (avi, "IDIT tag has no parsable info");
3565 gst_buffer_unmap (buf, &map);
3569 * Read full AVI headers.
3571 static GstFlowReturn
3572 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
3575 GstBuffer *buf, *sub = NULL;
3578 GstElement *element = GST_ELEMENT_CAST (avi);
3580 GstTagList *tags = NULL;
3583 stamp = gst_util_get_timestamp ();
3585 /* the header consists of a 'hdrl' LIST tag */
3586 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3587 if (res != GST_FLOW_OK)
3588 goto pull_range_failed;
3589 else if (tag != GST_RIFF_TAG_LIST)
3591 else if (gst_buffer_get_size (buf) < 4)
3594 GST_DEBUG_OBJECT (avi, "parsing headers");
3596 /* Find the 'hdrl' LIST tag */
3597 gst_buffer_extract (buf, 0, fourcc, 4);
3598 while (GST_READ_UINT32_LE (fourcc) != GST_RIFF_LIST_hdrl) {
3599 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
3600 GST_FOURCC_ARGS (GST_READ_UINT32_LE (fourcc)));
3603 gst_buffer_unref (buf);
3605 /* read new chunk */
3606 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3607 if (res != GST_FLOW_OK)
3608 goto pull_range_failed;
3609 else if (tag != GST_RIFF_TAG_LIST)
3611 else if (gst_buffer_get_size (buf) < 4)
3613 gst_buffer_extract (buf, 0, fourcc, 4);
3616 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
3618 gst_avi_demux_roundup_list (avi, &buf);
3620 /* the hdrl starts with a 'avih' header */
3621 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
3623 else if (tag != GST_RIFF_TAG_avih)
3625 else if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3628 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3630 /* now, read the elements from the header until the end */
3631 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
3634 /* sub can be NULL on empty tags */
3638 gst_buffer_map (sub, &map, GST_MAP_READ);
3641 case GST_RIFF_TAG_LIST:
3645 switch (GST_READ_UINT32_LE (map.data)) {
3646 case GST_RIFF_LIST_strl:
3647 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3648 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3649 ("failed to parse stream, ignoring"));
3654 case GST_RIFF_LIST_odml:
3655 gst_avi_demux_parse_odml (avi, sub);
3658 case GST_RIFF_LIST_INFO:
3659 gst_buffer_resize (sub, 4, -1);
3660 gst_riff_parse_info (element, sub, &tags);
3662 if (avi->globaltags) {
3663 gst_tag_list_insert (avi->globaltags, tags,
3664 GST_TAG_MERGE_REPLACE);
3666 avi->globaltags = tags;
3672 GST_WARNING_OBJECT (avi,
3673 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3674 GST_FOURCC_ARGS (GST_READ_UINT32_LE (map.data)));
3675 GST_MEMDUMP_OBJECT (avi, "Unknown list", map.data, map.size);
3677 case GST_RIFF_TAG_JUNQ:
3678 case GST_RIFF_TAG_JUNK:
3683 gst_avi_demux_parse_idit (avi, sub);
3686 GST_WARNING_OBJECT (avi,
3687 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
3688 GST_FOURCC_ARGS (tag), offset);
3689 GST_MEMDUMP_OBJECT (avi, "Unknown tag", map.data, map.size);
3691 case GST_RIFF_TAG_JUNQ:
3692 case GST_RIFF_TAG_JUNK:
3695 gst_buffer_unmap (sub, &map);
3696 gst_buffer_unref (sub);
3702 gst_buffer_unref (buf);
3703 GST_DEBUG ("elements parsed");
3705 /* check parsed streams */
3706 if (avi->num_streams == 0)
3708 else if (avi->num_streams != avi->avih->streams) {
3709 GST_WARNING_OBJECT (avi,
3710 "Stream header mentioned %d streams, but %d available",
3711 avi->avih->streams, avi->num_streams);
3714 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
3715 G_GUINT64_FORMAT, avi->offset);
3717 /* Now, find the data (i.e. skip all junk between header and data) */
3724 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
3725 if (res != GST_FLOW_OK) {
3726 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
3727 goto pull_range_failed;
3728 } else if (gst_buffer_get_size (buf) < 12) {
3729 GST_DEBUG_OBJECT (avi,
3730 "got %" G_GSIZE_FORMAT " bytes which is less than 12 bytes",
3731 gst_buffer_get_size (buf));
3732 gst_buffer_unref (buf);
3733 return GST_FLOW_ERROR;
3736 gst_buffer_map (buf, &map, GST_MAP_READ);
3737 tag = GST_READ_UINT32_LE (map.data);
3738 size = GST_READ_UINT32_LE (map.data + 4);
3739 ltag = GST_READ_UINT32_LE (map.data + 8);
3741 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
3742 GST_FOURCC_ARGS (tag), size);
3743 GST_MEMDUMP ("Tag content", map.data, map.size);
3744 gst_buffer_unmap (buf, &map);
3745 gst_buffer_unref (buf);
3748 case GST_RIFF_TAG_LIST:{
3750 case GST_RIFF_LIST_movi:
3751 GST_DEBUG_OBJECT (avi,
3752 "Reached the 'movi' tag, we're done with skipping");
3754 case GST_RIFF_LIST_INFO:
3756 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3758 if (res != GST_FLOW_OK) {
3759 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3760 goto pull_range_failed;
3762 GST_DEBUG ("got size %" G_GSIZE_FORMAT, gst_buffer_get_size (buf));
3764 GST_DEBUG ("skipping INFO LIST prefix");
3765 avi->offset += (4 - GST_ROUND_UP_2 (size));
3766 gst_buffer_unref (buf);
3770 sub = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, 4, -1);
3771 gst_riff_parse_info (element, sub, &tags);
3773 if (avi->globaltags) {
3774 gst_tag_list_insert (avi->globaltags, tags,
3775 GST_TAG_MERGE_REPLACE);
3777 avi->globaltags = tags;
3782 gst_buffer_unref (sub);
3785 gst_buffer_unref (buf);
3786 /* gst_riff_read_chunk() has already advanced avi->offset */
3789 GST_WARNING_OBJECT (avi,
3790 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3791 GST_FOURCC_ARGS (ltag));
3792 avi->offset += 8 + GST_ROUND_UP_2 (size);
3798 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3799 GST_FOURCC_ARGS (tag));
3801 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3802 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3803 /* Only get buffer for debugging if the memdump is needed */
3804 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= 9) {
3806 res = gst_pad_pull_range (avi->sinkpad, avi->offset, size, &buf);
3807 if (res != GST_FLOW_OK) {
3808 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3809 goto pull_range_failed;
3811 gst_buffer_map (buf, &map, GST_MAP_READ);
3812 GST_MEMDUMP ("Junk", map.data, map.size);
3813 gst_buffer_unmap (buf, &map);
3814 gst_buffer_unref (buf);
3816 avi->offset += 8 + GST_ROUND_UP_2 (size);
3822 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3823 avi->num_streams, avi->stream[0].indexes);
3825 /* create or read stream index (for seeking) */
3826 if (avi->stream[0].indexes != NULL) {
3827 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3828 gst_avi_demux_read_subindexes_pull (avi);
3830 if (!avi->have_index) {
3831 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
3832 gst_avi_demux_stream_index (avi);
3834 /* still no index, scan */
3835 if (!avi->have_index) {
3836 gst_avi_demux_stream_scan (avi);
3838 /* still no index.. this is a fatal error for now.
3839 * FIXME, we should switch to plain push mode without seeking
3840 * instead of failing. */
3841 if (!avi->have_index)
3845 /* use the indexes now to construct nice durations */
3846 gst_avi_demux_calculate_durations_from_index (avi);
3848 gst_avi_demux_expose_streams (avi, FALSE);
3850 /* do initial seek to the default segment values */
3851 gst_avi_demux_do_seek (avi, &avi->segment);
3853 /* create initial NEWSEGMENT event */
3855 gst_event_unref (avi->seg_event);
3856 avi->seg_event = gst_event_new_segment (&avi->segment);
3858 stamp = gst_util_get_timestamp () - stamp;
3859 GST_DEBUG_OBJECT (avi, "pulling header took %" GST_TIME_FORMAT,
3860 GST_TIME_ARGS (stamp));
3862 /* at this point we know all the streams and we can signal the no more
3864 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3865 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3872 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3873 ("Invalid AVI header (no LIST at start): %"
3874 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3875 gst_buffer_unref (buf);
3876 return GST_FLOW_ERROR;
3880 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3881 ("Invalid AVI header (no hdrl at start): %"
3882 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3883 gst_buffer_unref (buf);
3884 return GST_FLOW_ERROR;
3888 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3889 ("Invalid AVI header (no avih at start): %"
3890 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3892 gst_buffer_unref (sub);
3893 gst_buffer_unref (buf);
3894 return GST_FLOW_ERROR;
3898 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3899 ("Invalid AVI header (cannot parse avih at start)"));
3900 gst_buffer_unref (buf);
3901 return GST_FLOW_ERROR;
3905 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3906 return GST_FLOW_ERROR;
3910 GST_WARNING ("file without or too big index");
3911 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3912 ("Could not get/create index"));
3913 return GST_FLOW_ERROR;
3917 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3918 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3919 return GST_FLOW_ERROR;
3923 /* move a stream to @index */
3925 gst_avi_demux_move_stream (GstAviDemux * avi, GstAviStream * stream,
3926 GstSegment * segment, guint index)
3928 GST_DEBUG_OBJECT (avi, "Move stream %d to %u", stream->num, index);
3930 if (segment->rate < 0.0) {
3932 /* Because we don't know the frame order we need to push from the prev keyframe
3933 * to the next keyframe. If there is a smart decoder downstream he will notice
3934 * that there are too many encoded frames send and return EOS when there
3935 * are enough decoded frames to fill the segment. */
3936 next_key = gst_avi_demux_index_next (avi, stream, index, TRUE);
3938 /* FIXME, we go back to 0, we should look at segment.start. We will however
3939 * stop earlier when the see the timestamp < segment.start */
3940 stream->start_entry = 0;
3941 stream->step_entry = index;
3942 stream->current_entry = index;
3943 stream->stop_entry = next_key;
3945 GST_DEBUG_OBJECT (avi, "reverse seek: start %u, step %u, stop %u",
3946 stream->start_entry, stream->step_entry, stream->stop_entry);
3948 stream->start_entry = index;
3949 stream->step_entry = index;
3950 stream->stop_entry = gst_avi_demux_index_last (avi, stream);
3952 if (stream->current_entry != index) {
3953 GST_DEBUG_OBJECT (avi, "Move DISCONT from %u to %u",
3954 stream->current_entry, index);
3955 stream->current_entry = index;
3956 stream->discont = TRUE;
3959 /* update the buffer info */
3960 gst_avi_demux_get_buffer_info (avi, stream, index,
3961 &stream->current_timestamp, &stream->current_ts_end,
3962 &stream->current_offset, &stream->current_offset_end);
3964 GST_DEBUG_OBJECT (avi, "Moved to %u, ts %" GST_TIME_FORMAT
3965 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
3966 ", off_end %" G_GUINT64_FORMAT, index,
3967 GST_TIME_ARGS (stream->current_timestamp),
3968 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
3969 stream->current_offset_end);
3971 GST_DEBUG_OBJECT (avi, "Seeking to offset %" G_GUINT64_FORMAT,
3972 stream->index[index].offset);
3976 * Do the actual seeking.
3979 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
3981 GstClockTime seek_time;
3984 GstAviStream *stream;
3986 seek_time = segment->position;
3987 keyframe = ! !(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
3989 GST_DEBUG_OBJECT (avi, "seek to: %" GST_TIME_FORMAT
3990 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
3992 /* FIXME, this code assumes the main stream with keyframes is stream 0,
3993 * which is mostly correct... */
3994 stream = &avi->stream[avi->main_stream];
3996 /* get the entry index for the requested position */
3997 index = gst_avi_demux_index_for_time (avi, stream, seek_time);
3998 GST_DEBUG_OBJECT (avi, "Got entry %u", index);
4000 /* check if we are already on a keyframe */
4001 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
4002 GST_DEBUG_OBJECT (avi, "not keyframe, searching back");
4003 /* now go to the previous keyframe, this is where we should start
4005 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4006 GST_DEBUG_OBJECT (avi, "previous keyframe at %u", index);
4009 /* move the main stream to this position */
4010 gst_avi_demux_move_stream (avi, stream, segment, index);
4013 /* when seeking to a keyframe, we update the result seek time
4014 * to the time of the keyframe. */
4015 seek_time = stream->current_timestamp;
4016 GST_DEBUG_OBJECT (avi, "keyframe adjusted to %" GST_TIME_FORMAT,
4017 GST_TIME_ARGS (seek_time));
4020 /* the seek time is also the position and stream time when going
4022 segment->position = seek_time;
4023 if (segment->rate > 0.0)
4024 segment->time = seek_time;
4026 /* now set DISCONT and align the other streams */
4027 for (i = 0; i < avi->num_streams; i++) {
4028 GstAviStream *ostream;
4030 ostream = &avi->stream[i];
4031 if ((ostream == stream) || (ostream->index == NULL))
4034 /* get the entry index for the requested position */
4035 index = gst_avi_demux_index_for_time (avi, ostream, seek_time);
4037 /* move to previous keyframe */
4038 if (!ENTRY_IS_KEYFRAME (&ostream->index[index]))
4039 index = gst_avi_demux_index_prev (avi, ostream, index, TRUE);
4041 gst_avi_demux_move_stream (avi, ostream, segment, index);
4043 GST_DEBUG_OBJECT (avi, "done seek to: %" GST_TIME_FORMAT,
4044 GST_TIME_ARGS (seek_time));
4050 * Handle seek event in pull mode.
4053 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4058 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4062 GstSegment seeksegment = { 0, };
4066 GST_DEBUG_OBJECT (avi, "doing seek with event");
4068 gst_event_parse_seek (event, &rate, &format, &flags,
4069 &cur_type, &cur, &stop_type, &stop);
4071 /* we have to have a format as the segment format. Try to convert
4073 if (format != GST_FORMAT_TIME) {
4074 gboolean res = TRUE;
4076 if (cur_type != GST_SEEK_TYPE_NONE)
4077 res = gst_pad_query_convert (pad, format, cur, GST_FORMAT_TIME, &cur);
4078 if (res && stop_type != GST_SEEK_TYPE_NONE)
4079 res = gst_pad_query_convert (pad, format, stop, GST_FORMAT_TIME, &stop);
4083 format = GST_FORMAT_TIME;
4085 GST_DEBUG_OBJECT (avi,
4086 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
4087 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
4088 /* FIXME: can we do anything with rate!=1.0 */
4090 GST_DEBUG_OBJECT (avi, "doing seek without event");
4095 /* save flush flag */
4096 flush = flags & GST_SEEK_FLAG_FLUSH;
4099 GstEvent *fevent = gst_event_new_flush_start ();
4101 /* for a flushing seek, we send a flush_start on all pads. This will
4102 * eventually stop streaming with a WRONG_STATE. We can thus eventually
4103 * take the STREAM_LOCK. */
4104 GST_DEBUG_OBJECT (avi, "sending flush start");
4105 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4106 gst_pad_push_event (avi->sinkpad, fevent);
4108 /* a non-flushing seek, we PAUSE the task so that we can take the
4110 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
4111 gst_pad_pause_task (avi->sinkpad);
4114 /* wait for streaming to stop */
4115 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
4116 GST_PAD_STREAM_LOCK (avi->sinkpad);
4118 /* copy segment, we need this because we still need the old
4119 * segment when we close the current segment. */
4120 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4123 GST_DEBUG_OBJECT (avi, "configuring seek");
4124 gst_segment_do_seek (&seeksegment, rate, format, flags,
4125 cur_type, cur, stop_type, stop, &update);
4127 /* do the seek, seeksegment.position contains the new position, this
4128 * actually never fails. */
4129 gst_avi_demux_do_seek (avi, &seeksegment);
4132 GstEvent *fevent = gst_event_new_flush_stop (TRUE);
4134 GST_DEBUG_OBJECT (avi, "sending flush stop");
4135 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4136 gst_pad_push_event (avi->sinkpad, fevent);
4139 /* now update the real segment info */
4140 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
4142 /* post the SEGMENT_START message when we do segmented playback */
4143 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4144 gst_element_post_message (GST_ELEMENT_CAST (avi),
4145 gst_message_new_segment_start (GST_OBJECT_CAST (avi),
4146 avi->segment.format, avi->segment.position));
4149 /* queue the segment event for the streaming thread. */
4151 gst_event_unref (avi->seg_event);
4152 avi->seg_event = gst_event_new_segment (&avi->segment);
4154 if (!avi->streaming) {
4155 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4158 /* reset the last flow and mark discont, seek is always DISCONT */
4159 for (i = 0; i < avi->num_streams; i++) {
4160 GST_DEBUG_OBJECT (avi, "marking DISCONT");
4161 avi->stream[i].last_flow = GST_FLOW_OK;
4162 avi->stream[i].discont = TRUE;
4164 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
4171 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4177 * Handle seek event in push mode.
4180 avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4185 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4188 GstAviStream *stream;
4192 GstSegment seeksegment;
4195 /* check we have the index */
4196 if (!avi->have_index) {
4197 GST_DEBUG_OBJECT (avi, "no seek index built, seek aborted.");
4200 GST_DEBUG_OBJECT (avi, "doing push-based seek with event");
4203 gst_event_parse_seek (event, &rate, &format, &flags,
4204 &cur_type, &cur, &stop_type, &stop);
4206 if (format != GST_FORMAT_TIME) {
4207 gboolean res = TRUE;
4209 if (cur_type != GST_SEEK_TYPE_NONE)
4210 res = gst_pad_query_convert (pad, format, cur, GST_FORMAT_TIME, &cur);
4211 if (res && stop_type != GST_SEEK_TYPE_NONE)
4212 res = gst_pad_query_convert (pad, format, stop, GST_FORMAT_TIME, &stop);
4214 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4218 format = GST_FORMAT_TIME;
4221 /* let gst_segment handle any tricky stuff */
4222 GST_DEBUG_OBJECT (avi, "configuring seek");
4223 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4224 gst_segment_do_seek (&seeksegment, rate, format, flags,
4225 cur_type, cur, stop_type, stop, &update);
4227 keyframe = ! !(flags & GST_SEEK_FLAG_KEY_UNIT);
4228 cur = seeksegment.position;
4230 GST_DEBUG_OBJECT (avi,
4231 "Seek requested: ts %" GST_TIME_FORMAT " stop %" GST_TIME_FORMAT
4232 ", kf %u, rate %lf", GST_TIME_ARGS (cur), GST_TIME_ARGS (stop), keyframe,
4236 GST_DEBUG_OBJECT (avi, "negative rate seek not supported in push mode");
4240 /* FIXME, this code assumes the main stream with keyframes is stream 0,
4241 * which is mostly correct... */
4242 str_num = avi->main_stream;
4243 stream = &avi->stream[str_num];
4245 /* get the entry index for the requested position */
4246 index = gst_avi_demux_index_for_time (avi, stream, cur);
4247 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT,
4248 str_num, index, GST_TIME_ARGS (cur));
4250 /* check if we are already on a keyframe */
4251 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
4252 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4253 /* now go to the previous keyframe, this is where we should start
4255 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4256 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", index);
4259 gst_avi_demux_get_buffer_info (avi, stream, index,
4260 &stream->current_timestamp, &stream->current_ts_end,
4261 &stream->current_offset, &stream->current_offset_end);
4263 /* re-use cur to be the timestamp of the seek as it _will_ be */
4264 cur = stream->current_timestamp;
4266 min_offset = stream->index[index].offset;
4267 avi->seek_kf_offset = min_offset - 8;
4269 GST_DEBUG_OBJECT (avi,
4270 "Seek to: ts %" GST_TIME_FORMAT " (on str %u, idx %u, offset %"
4271 G_GUINT64_FORMAT ")", GST_TIME_ARGS (stream->current_timestamp), str_num,
4274 for (n = 0; n < avi->num_streams; n++) {
4275 GstAviStream *str = &avi->stream[n];
4278 if (n == avi->main_stream)
4281 /* get the entry index for the requested position */
4282 idx = gst_avi_demux_index_for_time (avi, str, cur);
4283 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT, n,
4284 idx, GST_TIME_ARGS (cur));
4286 /* check if we are already on a keyframe */
4287 if (!ENTRY_IS_KEYFRAME (&str->index[idx])) {
4288 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4289 /* now go to the previous keyframe, this is where we should start
4291 idx = gst_avi_demux_index_prev (avi, str, idx, TRUE);
4292 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", idx);
4295 gst_avi_demux_get_buffer_info (avi, str, idx,
4296 &str->current_timestamp, &str->current_ts_end,
4297 &str->current_offset, &str->current_offset_end);
4299 if (str->index[idx].offset < min_offset) {
4300 min_offset = str->index[idx].offset;
4301 GST_DEBUG_OBJECT (avi,
4302 "Found an earlier offset at %" G_GUINT64_FORMAT ", str %u",
4310 GST_DEBUG_OBJECT (avi,
4311 "Seek performed: str %u, offset %" G_GUINT64_FORMAT ", idx %u, ts %"
4312 GST_TIME_FORMAT ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4313 ", off_end %" G_GUINT64_FORMAT, str_num, min_offset, index,
4314 GST_TIME_ARGS (stream->current_timestamp),
4315 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4316 stream->current_offset_end);
4318 /* index data refers to data, not chunk header (for pull mode convenience) */
4320 GST_DEBUG_OBJECT (avi, "seeking to chunk at offset %" G_GUINT64_FORMAT,
4323 if (!perform_seek_to_offset (avi, min_offset)) {
4324 GST_DEBUG_OBJECT (avi, "seek event failed!");
4332 * Handle whether we can perform the seek event or if we have to let the chain
4333 * function handle seeks to build the seek indexes first.
4336 gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
4339 /* check for having parsed index already */
4340 if (!avi->have_index) {
4342 gboolean building_index;
4344 GST_OBJECT_LOCK (avi);
4345 /* handle the seek event in the chain function */
4346 avi->state = GST_AVI_DEMUX_SEEK;
4348 /* copy the event */
4349 if (avi->seek_event)
4350 gst_event_unref (avi->seek_event);
4351 avi->seek_event = gst_event_ref (event);
4353 /* set the building_index flag so that only one thread can setup the
4354 * structures for index seeking. */
4355 building_index = avi->building_index;
4356 if (!building_index) {
4357 avi->building_index = TRUE;
4358 if (avi->stream[0].indexes) {
4359 avi->odml_stream = 0;
4360 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
4361 offset = avi->odml_subidxs[0];
4363 offset = avi->idx1_offset;
4366 GST_OBJECT_UNLOCK (avi);
4368 if (!building_index) {
4369 /* seek to the first subindex or legacy index */
4370 GST_INFO_OBJECT (avi,
4371 "Seeking to legacy index/first subindex at %" G_GUINT64_FORMAT,
4373 return perform_seek_to_offset (avi, offset);
4376 /* FIXME: we have to always return true so that we don't block the seek
4378 * Note: maybe it is OK to return true if we're still building the index */
4382 return avi_demux_handle_seek_push (avi, pad, event);
4386 * Helper for gst_avi_demux_invert()
4389 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
4391 memcpy (tmp, d1, bytes);
4392 memcpy (d1, d2, bytes);
4393 memcpy (d2, tmp, bytes);
4397 #define gst_avi_demux_is_uncompressed(fourcc) \
4399 (fourcc == GST_RIFF_DIB || \
4400 fourcc == GST_RIFF_rgb || \
4401 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW))
4404 * Invert DIB buffers... Takes existing buffer and
4405 * returns either the buffer or a new one (with old
4406 * one dereferenced).
4407 * FIXME: can't we preallocate tmp? and remember stride, bpp?
4410 gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
4419 if (stream->strh->type != GST_RIFF_FCC_vids)
4422 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
4423 return buf; /* Ignore non DIB buffers */
4426 caps = gst_pad_get_current_caps (stream->pad);
4427 s = gst_caps_get_structure (caps, 0);
4428 gst_caps_unref (caps);
4430 if (!gst_structure_get_int (s, "bpp", &bpp)) {
4431 GST_WARNING ("Failed to retrieve depth from caps");
4435 if (stream->strf.vids == NULL) {
4436 GST_WARNING ("Failed to retrieve vids for stream");
4440 h = stream->strf.vids->height;
4441 w = stream->strf.vids->width;
4442 stride = GST_ROUND_UP_4 (w * (bpp / 8));
4444 buf = gst_buffer_make_writable (buf);
4446 gst_buffer_map (buf, &map, GST_MAP_READWRITE);
4447 if (map.size < (stride * h)) {
4448 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
4449 gst_buffer_unmap (buf, &map);
4453 tmp = g_malloc (stride);
4455 for (y = 0; y < h / 2; y++) {
4456 swap_line (map.data + stride * y, map.data + stride * (h - 1 - y), tmp,
4462 gst_buffer_unmap (buf, &map);
4469 gst_avi_demux_add_assoc (GstAviDemux * avi, GstAviStream * stream,
4470 GstClockTime timestamp, guint64 offset, gboolean keyframe)
4472 /* do not add indefinitely for open-ended streaming */
4473 if (G_UNLIKELY (avi->element_index && avi->seekable)) {
4474 GST_LOG_OBJECT (avi, "adding association %" GST_TIME_FORMAT "-> %"
4475 G_GUINT64_FORMAT, GST_TIME_ARGS (timestamp), offset);
4476 gst_index_add_association (avi->element_index, avi->index_id,
4477 keyframe ? GST_ASSOCIATION_FLAG_KEY_UNIT :
4478 GST_ASSOCIATION_FLAG_DELTA_UNIT, GST_FORMAT_TIME, timestamp,
4479 GST_FORMAT_BYTES, offset, NULL);
4480 /* current_entry is DEFAULT (frame #) */
4481 gst_index_add_association (avi->element_index, stream->index_id,
4482 keyframe ? GST_ASSOCIATION_FLAG_KEY_UNIT :
4483 GST_ASSOCIATION_FLAG_DELTA_UNIT, GST_FORMAT_TIME, timestamp,
4484 GST_FORMAT_BYTES, offset, GST_FORMAT_DEFAULT, stream->current_entry,
4491 * Returns the aggregated GstFlowReturn.
4493 static GstFlowReturn
4494 gst_avi_demux_combine_flows (GstAviDemux * avi, GstAviStream * stream,
4498 gboolean unexpected = FALSE, not_linked = TRUE;
4500 /* store the value */
4501 stream->last_flow = ret;
4503 /* any other error that is not-linked or eos can be returned right away */
4504 if (G_LIKELY (ret != GST_FLOW_EOS && ret != GST_FLOW_NOT_LINKED))
4507 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4508 for (i = 0; i < avi->num_streams; i++) {
4509 GstAviStream *ostream = &avi->stream[i];
4511 ret = ostream->last_flow;
4512 /* no unexpected or unlinked, return */
4513 if (G_LIKELY (ret != GST_FLOW_EOS && ret != GST_FLOW_NOT_LINKED))
4516 /* we check to see if we have at least 1 unexpected or all unlinked */
4517 unexpected |= (ret == GST_FLOW_EOS);
4518 not_linked &= (ret == GST_FLOW_NOT_LINKED);
4520 /* when we get here, we all have unlinked or unexpected */
4522 ret = GST_FLOW_NOT_LINKED;
4523 else if (unexpected)
4526 GST_LOG_OBJECT (avi, "combined %s to return %s",
4527 gst_flow_get_name (stream->last_flow), gst_flow_get_name (ret));
4531 /* move @stream to the next position in its index */
4532 static GstFlowReturn
4533 gst_avi_demux_advance (GstAviDemux * avi, GstAviStream * stream,
4536 guint old_entry, new_entry;
4538 old_entry = stream->current_entry;
4540 new_entry = old_entry + 1;
4542 /* see if we reached the end */
4543 if (new_entry >= stream->stop_entry) {
4544 if (avi->segment.rate < 0.0) {
4545 if (stream->step_entry == stream->start_entry) {
4546 /* we stepped all the way to the start, eos */
4547 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
4550 /* backwards, stop becomes step, find a new step */
4551 stream->stop_entry = stream->step_entry;
4552 stream->step_entry = gst_avi_demux_index_prev (avi, stream,
4553 stream->stop_entry, TRUE);
4555 GST_DEBUG_OBJECT (avi,
4556 "reverse playback jump: start %u, step %u, stop %u",
4557 stream->start_entry, stream->step_entry, stream->stop_entry);
4559 /* and start from the previous keyframe now */
4560 new_entry = stream->step_entry;
4563 GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
4568 if (new_entry != old_entry) {
4569 stream->current_entry = new_entry;
4570 stream->current_total = stream->index[new_entry].total;
4572 if (new_entry == old_entry + 1) {
4573 GST_DEBUG_OBJECT (avi, "moved forwards from %u to %u",
4574 old_entry, new_entry);
4575 /* we simply moved one step forwards, reuse current info */
4576 stream->current_timestamp = stream->current_ts_end;
4577 stream->current_offset = stream->current_offset_end;
4578 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4579 NULL, &stream->current_ts_end, NULL, &stream->current_offset_end);
4581 /* we moved DISCONT, full update */
4582 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4583 &stream->current_timestamp, &stream->current_ts_end,
4584 &stream->current_offset, &stream->current_offset_end);
4585 /* and MARK discont for this stream */
4586 stream->last_flow = GST_FLOW_OK;
4587 stream->discont = TRUE;
4588 GST_DEBUG_OBJECT (avi, "Moved from %u to %u, ts %" GST_TIME_FORMAT
4589 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4590 ", off_end %" G_GUINT64_FORMAT, old_entry, new_entry,
4591 GST_TIME_ARGS (stream->current_timestamp),
4592 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4593 stream->current_offset_end);
4601 GST_DEBUG_OBJECT (avi, "we are EOS");
4602 /* setting current_timestamp to -1 marks EOS */
4603 stream->current_timestamp = -1;
4604 return GST_FLOW_EOS;
4608 /* find the stream with the lowest current position when going forwards or with
4609 * the highest position when going backwards, this is the stream
4610 * we should push from next */
4612 gst_avi_demux_find_next (GstAviDemux * avi, gfloat rate)
4614 guint64 min_time, max_time;
4615 guint stream_num, i;
4618 min_time = G_MAXUINT64;
4621 for (i = 0; i < avi->num_streams; i++) {
4623 GstAviStream *stream;
4625 stream = &avi->stream[i];
4627 /* ignore streams that finished */
4628 if (stream->last_flow == GST_FLOW_EOS)
4631 position = stream->current_timestamp;
4633 /* position of -1 is EOS */
4634 if (position != -1) {
4635 if (rate > 0.0 && position < min_time) {
4636 min_time = position;
4638 } else if (rate < 0.0 && position >= max_time) {
4639 max_time = position;
4647 static GstFlowReturn
4648 gst_avi_demux_loop_data (GstAviDemux * avi)
4650 GstFlowReturn ret = GST_FLOW_OK;
4652 GstAviStream *stream;
4653 gboolean processed = FALSE;
4655 guint64 offset, size;
4656 GstClockTime timestamp, duration;
4657 guint64 out_offset, out_offset_end;
4659 GstAviIndexEntry *entry;
4662 stream_num = gst_avi_demux_find_next (avi, avi->segment.rate);
4665 if (G_UNLIKELY (stream_num == -1)) {
4666 GST_DEBUG_OBJECT (avi, "all streams are EOS");
4670 /* we have the stream now */
4671 stream = &avi->stream[stream_num];
4673 /* skip streams without pads */
4675 GST_DEBUG_OBJECT (avi, "skipping entry from stream %d without pad",
4680 /* get the timing info for the entry */
4681 timestamp = stream->current_timestamp;
4682 duration = stream->current_ts_end - timestamp;
4683 out_offset = stream->current_offset;
4684 out_offset_end = stream->current_offset_end;
4686 /* get the entry data info */
4687 entry = &stream->index[stream->current_entry];
4688 offset = entry->offset;
4690 keyframe = ENTRY_IS_KEYFRAME (entry);
4692 /* skip empty entries */
4694 GST_DEBUG_OBJECT (avi, "Skipping entry %u (%" G_GUINT64_FORMAT ", %p)",
4695 stream->current_entry, size, stream->pad);
4699 if (avi->segment.rate > 0.0) {
4700 /* only check this for fowards playback for now */
4701 if (keyframe && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
4702 && (timestamp > avi->segment.stop)) {
4707 GST_LOG ("reading buffer (size=%" G_GUINT64_FORMAT "), stream %d, pos %"
4708 G_GUINT64_FORMAT " (0x%" G_GINT64_MODIFIER "x), kf %d", size,
4709 stream_num, offset, offset, keyframe);
4711 /* FIXME, check large chunks and cut them up */
4713 /* pull in the data */
4715 ret = gst_pad_pull_range (avi->sinkpad, offset, size, &buf);
4716 if (ret != GST_FLOW_OK)
4719 /* check for short buffers, this is EOS as well */
4720 if (gst_buffer_get_size (buf) < size)
4723 /* invert the picture if needed */
4724 buf = gst_avi_demux_invert (stream, buf);
4726 /* mark non-keyframes */
4728 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4729 GST_BUFFER_PTS (buf) = timestamp;
4731 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4732 GST_BUFFER_PTS (buf) = GST_CLOCK_TIME_NONE;
4734 GST_BUFFER_DTS (buf) = timestamp;
4735 GST_BUFFER_DURATION (buf) = duration;
4736 GST_BUFFER_OFFSET (buf) = out_offset;
4737 GST_BUFFER_OFFSET_END (buf) = out_offset_end;
4739 /* mark discont when pending */
4740 if (stream->discont) {
4741 GST_DEBUG_OBJECT (avi, "setting DISCONT flag");
4742 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4743 stream->discont = FALSE;
4746 gst_avi_demux_add_assoc (avi, stream, timestamp, offset, keyframe);
4749 /* update current position in the segment */
4750 avi->segment.position = timestamp;
4752 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %" G_GSIZE_FORMAT ", ts %"
4753 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4754 ", off_end %" G_GUINT64_FORMAT,
4755 gst_buffer_get_size (buf), GST_TIME_ARGS (timestamp),
4756 GST_TIME_ARGS (duration), out_offset, out_offset_end);
4758 ret = gst_pad_push (stream->pad, buf);
4760 /* mark as processed, we increment the frame and byte counters then
4761 * leave the while loop and return the GstFlowReturn */
4764 if (avi->segment.rate < 0) {
4765 if (timestamp > avi->segment.stop && ret == GST_FLOW_EOS) {
4766 /* In reverse playback we can get a GST_FLOW_EOS when
4767 * we are at the end of the segment, so we just need to jump
4768 * back to the previous section. */
4769 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
4774 /* move to next item */
4775 ret = gst_avi_demux_advance (avi, stream, ret);
4778 ret = gst_avi_demux_combine_flows (avi, stream, ret);
4779 } while (!processed);
4787 GST_DEBUG_OBJECT (avi, "No samples left for any streams - EOS");
4793 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
4794 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
4795 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (avi->segment.stop));
4797 /* move to next stream */
4802 GST_DEBUG_OBJECT (avi, "pull range failed: pos=%" G_GUINT64_FORMAT
4803 " size=%" G_GUINT64_FORMAT, offset, size);
4808 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
4809 ", only got %" G_GSIZE_FORMAT "/%" G_GUINT64_FORMAT
4810 " bytes (truncated file?)", offset, gst_buffer_get_size (buf), size);
4811 gst_buffer_unref (buf);
4818 * Read data. If we have an index it delegates to
4819 * gst_avi_demux_process_next_entry().
4821 static GstFlowReturn
4822 gst_avi_demux_stream_data (GstAviDemux * avi)
4827 GstFlowReturn res = GST_FLOW_OK;
4829 if (G_UNLIKELY (avi->have_eos)) {
4830 /* Clean adapter, we're done */
4831 gst_adapter_clear (avi->adapter);
4832 return GST_FLOW_EOS;
4835 if (G_UNLIKELY (avi->todrop)) {
4838 if ((drop = gst_adapter_available (avi->adapter))) {
4839 if (drop > avi->todrop)
4841 GST_DEBUG_OBJECT (avi, "Dropping %d bytes", drop);
4842 gst_adapter_flush (avi->adapter, drop);
4843 avi->todrop -= drop;
4844 avi->offset += drop;
4848 /* Iterate until need more data, so adapter won't grow too much */
4850 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
4854 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
4855 GST_FOURCC_ARGS (tag), size);
4857 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
4858 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
4859 GST_LOG ("Chunk ok");
4860 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
4861 GST_DEBUG ("Found sub-index tag");
4862 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4863 /* accept 0 size buffer here */
4864 avi->abort_buffering = FALSE;
4865 GST_DEBUG (" skipping %d bytes for now", size);
4866 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4869 } else if (tag == GST_RIFF_TAG_RIFF) {
4870 /* RIFF tags can appear in ODML files, just jump over them */
4871 if (gst_adapter_available (avi->adapter) >= 12) {
4872 GST_DEBUG ("Found RIFF tag, skipping RIFF header");
4873 gst_adapter_flush (avi->adapter, 12);
4877 } else if (tag == GST_RIFF_TAG_idx1) {
4878 GST_DEBUG ("Found index tag");
4879 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4880 /* accept 0 size buffer here */
4881 avi->abort_buffering = FALSE;
4882 GST_DEBUG (" skipping %d bytes for now", size);
4883 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4886 } else if (tag == GST_RIFF_TAG_LIST) {
4887 /* movi chunks might be grouped in rec list */
4888 if (gst_adapter_available (avi->adapter) >= 12) {
4889 GST_DEBUG ("Found LIST tag, skipping LIST header");
4890 gst_adapter_flush (avi->adapter, 12);
4894 } else if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ) {
4895 /* rec list might contain JUNK chunks */
4896 GST_DEBUG ("Found JUNK tag");
4897 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4898 /* accept 0 size buffer here */
4899 avi->abort_buffering = FALSE;
4900 GST_DEBUG (" skipping %d bytes for now", size);
4901 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4905 GST_DEBUG ("No more stream chunks, send EOS");
4906 avi->have_eos = TRUE;
4907 return GST_FLOW_EOS;
4910 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
4911 /* supposedly one hopes to catch a nicer chunk later on ... */
4912 /* FIXME ?? give up here rather than possibly ending up going
4913 * through the whole file */
4914 if (avi->abort_buffering) {
4915 avi->abort_buffering = FALSE;
4917 gst_adapter_flush (avi->adapter, 8);
4924 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
4925 GST_FOURCC_ARGS (tag), size);
4927 stream_nr = CHUNKID_TO_STREAMNR (tag);
4929 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
4931 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
4932 stream_nr, GST_FOURCC_ARGS (tag));
4933 avi->offset += 8 + GST_ROUND_UP_2 (size);
4934 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4936 GstAviStream *stream;
4937 GstClockTime next_ts = 0;
4938 GstBuffer *buf = NULL;
4942 gboolean saw_desired_kf = stream_nr != avi->main_stream
4943 || avi->offset >= avi->seek_kf_offset;
4945 if (stream_nr == avi->main_stream && avi->offset == avi->seek_kf_offset) {
4946 GST_DEBUG_OBJECT (avi, "Desired keyframe reached");
4947 avi->seek_kf_offset = 0;
4950 if (saw_desired_kf) {
4951 gst_adapter_flush (avi->adapter, 8);
4954 buf = gst_adapter_take_buffer (avi->adapter, GST_ROUND_UP_2 (size));
4955 /* patch the size */
4956 gst_buffer_resize (buf, 0, size);
4961 GST_DEBUG_OBJECT (avi,
4962 "Desired keyframe not yet reached, flushing chunk");
4963 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4967 offset = avi->offset;
4969 avi->offset += 8 + GST_ROUND_UP_2 (size);
4971 stream = &avi->stream[stream_nr];
4973 /* set delay (if any)
4974 if (stream->strh->init_frames == stream->current_frame &&
4976 stream->delay = next_ts;
4979 /* parsing of corresponding header may have failed */
4980 if (G_UNLIKELY (!stream->pad)) {
4981 GST_WARNING_OBJECT (avi, "no pad for stream ID %" GST_FOURCC_FORMAT,
4982 GST_FOURCC_ARGS (tag));
4984 gst_buffer_unref (buf);
4986 /* get time of this buffer */
4987 gst_pad_query_position (stream->pad, GST_FORMAT_TIME,
4988 (gint64 *) & next_ts);
4991 gst_avi_demux_add_assoc (avi, stream, next_ts, offset, FALSE);
4994 /* increment our positions */
4995 stream->current_entry++;
4996 stream->current_total += size;
4998 /* update current position in the segment */
4999 avi->segment.position = next_ts;
5001 if (saw_desired_kf && buf) {
5002 GstClockTime dur_ts = 0;
5004 /* invert the picture if needed */
5005 buf = gst_avi_demux_invert (stream, buf);
5007 gst_pad_query_position (stream->pad, GST_FORMAT_TIME,
5008 (gint64 *) & dur_ts);
5010 GST_BUFFER_DTS (buf) = next_ts;
5011 GST_BUFFER_PTS (buf) = GST_CLOCK_TIME_NONE;
5012 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
5013 if (stream->strh->type == GST_RIFF_FCC_vids) {
5014 GST_BUFFER_OFFSET (buf) = stream->current_entry - 1;
5015 GST_BUFFER_OFFSET_END (buf) = stream->current_entry;
5017 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
5018 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
5021 GST_DEBUG_OBJECT (avi,
5022 "Pushing buffer with time=%" GST_TIME_FORMAT ", duration %"
5023 GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
5024 " and size %d over pad %s", GST_TIME_ARGS (next_ts),
5025 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
5026 GST_BUFFER_OFFSET (buf), size, GST_PAD_NAME (stream->pad));
5028 /* mark discont when pending */
5029 if (G_UNLIKELY (stream->discont)) {
5030 GST_DEBUG_OBJECT (avi, "Setting DISCONT");
5031 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
5032 stream->discont = FALSE;
5034 res = gst_pad_push (stream->pad, buf);
5038 res = gst_avi_demux_combine_flows (avi, stream, res);
5039 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5040 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
5052 * Send pending tags.
5055 push_tag_lists (GstAviDemux * avi)
5063 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
5065 for (i = 0; i < avi->num_streams; i++) {
5066 GstAviStream *stream = &avi->stream[i];
5067 GstPad *pad = stream->pad;
5069 tags = stream->taglist;
5072 GST_DEBUG_OBJECT (pad, "Tags: %" GST_PTR_FORMAT, tags);
5074 gst_pad_push_event (pad, gst_event_new_tag (tags));
5075 stream->taglist = NULL;
5079 if (!(tags = avi->globaltags))
5080 tags = gst_tag_list_new_empty ();
5082 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
5083 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
5085 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, tags);
5086 gst_avi_demux_push_event (avi, gst_event_new_tag (tags));
5087 avi->globaltags = NULL;
5088 avi->got_tags = FALSE;
5092 gst_avi_demux_loop (GstPad * pad)
5095 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
5097 switch (avi->state) {
5098 case GST_AVI_DEMUX_START:
5099 res = gst_avi_demux_stream_init_pull (avi);
5100 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5101 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5104 avi->state = GST_AVI_DEMUX_HEADER;
5106 case GST_AVI_DEMUX_HEADER:
5107 res = gst_avi_demux_stream_header_pull (avi);
5108 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5109 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5112 avi->state = GST_AVI_DEMUX_MOVI;
5114 case GST_AVI_DEMUX_MOVI:
5115 if (G_UNLIKELY (avi->seg_event)) {
5116 gst_avi_demux_push_event (avi, avi->seg_event);
5117 avi->seg_event = NULL;
5119 if (G_UNLIKELY (avi->got_tags)) {
5120 push_tag_lists (avi);
5122 /* process each index entry in turn */
5123 res = gst_avi_demux_loop_data (avi);
5125 /* pause when error */
5126 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5127 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
5132 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
5133 res = GST_FLOW_ERROR;
5142 gboolean push_eos = FALSE;
5143 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
5144 gst_pad_pause_task (avi->sinkpad);
5146 if (res == GST_FLOW_EOS) {
5147 /* handle end-of-stream/segment */
5148 /* so align our position with the end of it, if there is one
5149 * this ensures a subsequent will arrive at correct base/acc time */
5150 if (avi->segment.rate > 0.0 &&
5151 GST_CLOCK_TIME_IS_VALID (avi->segment.stop))
5152 avi->segment.position = avi->segment.stop;
5153 else if (avi->segment.rate < 0.0)
5154 avi->segment.position = avi->segment.start;
5155 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5158 if ((stop = avi->segment.stop) == -1)
5159 stop = avi->segment.duration;
5161 GST_INFO_OBJECT (avi, "sending segment_done");
5163 gst_element_post_message
5164 (GST_ELEMENT_CAST (avi),
5165 gst_message_new_segment_done (GST_OBJECT_CAST (avi),
5166 GST_FORMAT_TIME, stop));
5170 } else if (res == GST_FLOW_NOT_LINKED || res < GST_FLOW_EOS) {
5171 /* for fatal errors we post an error message, wrong-state is
5172 * not fatal because it happens due to flushes and only means
5173 * that we should stop now. */
5174 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
5175 (_("Internal data stream error.")),
5176 ("streaming stopped, reason %s", gst_flow_get_name (res)));
5180 GST_INFO_OBJECT (avi, "sending eos");
5181 if (!gst_avi_demux_push_event (avi, gst_event_new_eos ()) &&
5182 (res == GST_FLOW_EOS)) {
5183 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
5184 (NULL), ("got eos but no streams (yet)"));
5191 static GstFlowReturn
5192 gst_avi_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
5195 GstAviDemux *avi = GST_AVI_DEMUX (parent);
5198 if (GST_BUFFER_IS_DISCONT (buf)) {
5199 GST_DEBUG_OBJECT (avi, "got DISCONT");
5200 gst_adapter_clear (avi->adapter);
5201 /* mark all streams DISCONT */
5202 for (i = 0; i < avi->num_streams; i++)
5203 avi->stream[i].discont = TRUE;
5206 GST_DEBUG ("Store %" G_GSIZE_FORMAT " bytes in adapter",
5207 gst_buffer_get_size (buf));
5208 gst_adapter_push (avi->adapter, buf);
5210 switch (avi->state) {
5211 case GST_AVI_DEMUX_START:
5212 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
5213 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5217 case GST_AVI_DEMUX_HEADER:
5218 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
5219 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5223 case GST_AVI_DEMUX_MOVI:
5224 if (G_UNLIKELY (avi->seg_event)) {
5225 gst_avi_demux_push_event (avi, avi->seg_event);
5226 avi->seg_event = NULL;
5228 if (G_UNLIKELY (avi->got_tags)) {
5229 push_tag_lists (avi);
5231 res = gst_avi_demux_stream_data (avi);
5233 case GST_AVI_DEMUX_SEEK:
5239 /* obtain and parse indexes */
5240 if (avi->stream[0].indexes && !gst_avi_demux_read_subindexes_push (avi))
5241 /* seek in subindex read function failed */
5244 if (!avi->stream[0].indexes && !avi->have_index
5245 && avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
5246 gst_avi_demux_stream_index_push (avi);
5248 if (avi->have_index) {
5249 /* use the indexes now to construct nice durations */
5250 gst_avi_demux_calculate_durations_from_index (avi);
5252 /* still parsing indexes */
5256 GST_OBJECT_LOCK (avi);
5257 event = avi->seek_event;
5258 avi->seek_event = NULL;
5259 GST_OBJECT_UNLOCK (avi);
5261 /* calculate and perform seek */
5262 if (!avi_demux_handle_seek_push (avi, avi->sinkpad, event))
5265 gst_event_unref (event);
5266 avi->state = GST_AVI_DEMUX_MOVI;
5270 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
5271 ("Illegal internal state"));
5272 res = GST_FLOW_ERROR;
5276 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
5277 gst_flow_get_name (res));
5279 if (G_UNLIKELY (avi->abort_buffering))
5280 goto abort_buffering;
5287 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("failed to read indexes"));
5288 return GST_FLOW_ERROR;
5292 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("push mode seek failed"));
5293 return GST_FLOW_ERROR;
5297 avi->abort_buffering = FALSE;
5298 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
5299 return GST_FLOW_ERROR;
5304 gst_avi_demux_sink_activate (GstPad * sinkpad, GstObject * parent)
5309 query = gst_query_new_scheduling ();
5311 if (!gst_pad_peer_query (sinkpad, query)) {
5312 gst_query_unref (query);
5316 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
5317 gst_query_unref (query);
5322 GST_DEBUG_OBJECT (sinkpad, "activating pull");
5323 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
5327 GST_DEBUG_OBJECT (sinkpad, "activating push");
5328 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
5333 gst_avi_demux_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
5334 GstPadMode mode, gboolean active)
5337 GstAviDemux *avi = GST_AVI_DEMUX (parent);
5340 case GST_PAD_MODE_PULL:
5342 avi->streaming = FALSE;
5343 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
5346 res = gst_pad_stop_task (sinkpad);
5349 case GST_PAD_MODE_PUSH:
5351 GST_DEBUG ("avi: activating push/chain function");
5352 avi->streaming = TRUE;
5354 GST_DEBUG ("avi: deactivating push/chain function");
5367 gst_avi_demux_set_index (GstElement * element, GstIndex * index)
5369 GstAviDemux *avi = GST_AVI_DEMUX (element);
5371 GST_OBJECT_LOCK (avi);
5372 if (avi->element_index)
5373 gst_object_unref (avi->element_index);
5375 avi->element_index = gst_object_ref (index);
5377 avi->element_index = NULL;
5379 GST_OBJECT_UNLOCK (avi);
5380 /* object lock might be taken again */
5382 gst_index_get_writer_id (index, GST_OBJECT_CAST (element), &avi->index_id);
5383 GST_DEBUG_OBJECT (avi, "Set index %" GST_PTR_FORMAT, avi->element_index);
5387 gst_avi_demux_get_index (GstElement * element)
5389 GstIndex *result = NULL;
5390 GstAviDemux *avi = GST_AVI_DEMUX (element);
5392 GST_OBJECT_LOCK (avi);
5393 if (avi->element_index)
5394 result = gst_object_ref (avi->element_index);
5395 GST_OBJECT_UNLOCK (avi);
5397 GST_DEBUG_OBJECT (avi, "Returning index %" GST_PTR_FORMAT, result);
5403 static GstStateChangeReturn
5404 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
5406 GstStateChangeReturn ret;
5407 GstAviDemux *avi = GST_AVI_DEMUX (element);
5409 switch (transition) {
5410 case GST_STATE_CHANGE_READY_TO_PAUSED:
5411 avi->streaming = FALSE;
5412 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
5418 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5419 if (ret == GST_STATE_CHANGE_FAILURE)
5422 switch (transition) {
5423 case GST_STATE_CHANGE_PAUSED_TO_READY:
5424 avi->have_index = FALSE;
5425 gst_avi_demux_reset (avi);