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 GST_DEBUG_CATEGORY_STATIC (avidemux_debug);
61 #define GST_CAT_DEFAULT avidemux_debug
63 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
66 GST_STATIC_CAPS ("video/x-msvideo")
69 static void gst_avi_demux_base_init (GstAviDemuxClass * klass);
70 static void gst_avi_demux_class_init (GstAviDemuxClass * klass);
71 static void gst_avi_demux_init (GstAviDemux * avi);
72 static void gst_avi_demux_finalize (GObject * object);
74 static void gst_avi_demux_reset (GstAviDemux * avi);
77 static const GstEventMask *gst_avi_demux_get_event_mask (GstPad * pad);
79 static gboolean gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event);
80 static gboolean gst_avi_demux_handle_sink_event (GstPad * pad,
82 static gboolean gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event);
85 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
87 static const GstQueryType *gst_avi_demux_get_src_query_types (GstPad * pad);
88 static gboolean gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query);
89 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
90 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
92 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
93 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
95 static gboolean gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
97 static void gst_avi_demux_loop (GstPad * pad);
98 static gboolean gst_avi_demux_sink_activate (GstPad * sinkpad);
99 static gboolean gst_avi_demux_sink_activate_pull (GstPad * sinkpad,
101 static gboolean gst_avi_demux_activate_push (GstPad * pad, gboolean active);
102 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstBuffer * buf);
104 static void gst_avi_demux_set_index (GstElement * element, GstIndex * index);
105 static GstIndex *gst_avi_demux_get_index (GstElement * element);
106 static GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
107 GstStateChange transition);
108 static void gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi);
109 static void gst_avi_demux_get_buffer_info (GstAviDemux * avi,
110 GstAviStream * stream, guint entry_n, GstClockTime * timestamp,
111 GstClockTime * ts_end, guint64 * offset, guint64 * offset_end);
113 static void gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf);
115 static GstElementClass *parent_class = NULL;
117 /* GObject methods */
120 gst_avi_demux_get_type (void)
122 static GType avi_demux_type = 0;
124 if (!avi_demux_type) {
125 static const GTypeInfo avi_demux_info = {
126 sizeof (GstAviDemuxClass),
127 (GBaseInitFunc) gst_avi_demux_base_init,
129 (GClassInitFunc) gst_avi_demux_class_init,
132 sizeof (GstAviDemux),
134 (GInstanceInitFunc) gst_avi_demux_init,
138 g_type_register_static (GST_TYPE_ELEMENT,
139 "GstAviDemux", &avi_demux_info, 0);
142 return avi_demux_type;
146 gst_avi_demux_base_init (GstAviDemuxClass * klass)
148 static const GstElementDetails gst_avi_demux_details =
149 GST_ELEMENT_DETAILS ("Avi demuxer",
151 "Demultiplex an avi file into audio and video",
152 "Erik Walthinsen <omega@cse.ogi.edu>\n"
153 "Wim Taymans <wim.taymans@chello.be>\n"
154 "Thijs Vermeir <thijsvermeir@gmail.com>");
155 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
156 GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl;
157 GstCaps *audcaps, *vidcaps, *subcaps;
159 audcaps = gst_riff_create_audio_template_caps ();
160 gst_caps_append (audcaps, gst_caps_new_simple ("audio/x-avi-unknown", NULL));
161 audiosrctempl = gst_pad_template_new ("audio_%02d",
162 GST_PAD_SRC, GST_PAD_SOMETIMES, audcaps);
164 vidcaps = gst_riff_create_video_template_caps ();
165 gst_caps_append (vidcaps, gst_riff_create_iavs_template_caps ());
166 gst_caps_append (vidcaps, gst_caps_new_simple ("video/x-avi-unknown", NULL));
167 videosrctempl = gst_pad_template_new ("video_%02d",
168 GST_PAD_SRC, GST_PAD_SOMETIMES, vidcaps);
170 subcaps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
171 subsrctempl = gst_pad_template_new ("subtitle_%02d",
172 GST_PAD_SRC, GST_PAD_SOMETIMES, subcaps);
173 gst_element_class_add_pad_template (element_class, audiosrctempl);
174 gst_element_class_add_pad_template (element_class, videosrctempl);
175 gst_element_class_add_pad_template (element_class, subsrctempl);
176 gst_element_class_add_pad_template (element_class,
177 gst_static_pad_template_get (&sink_templ));
178 gst_element_class_set_details (element_class, &gst_avi_demux_details);
182 gst_avi_demux_class_init (GstAviDemuxClass * klass)
184 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
185 GObjectClass *gobject_class = (GObjectClass *) klass;
187 GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
188 0, "Demuxer for AVI streams");
190 parent_class = g_type_class_peek_parent (klass);
192 gobject_class->finalize = gst_avi_demux_finalize;
193 gstelement_class->change_state =
194 GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
196 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_avi_demux_set_index);
197 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_avi_demux_get_index);
201 gst_avi_demux_init (GstAviDemux * avi)
203 avi->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
204 gst_pad_set_activate_function (avi->sinkpad,
205 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate));
206 gst_pad_set_activatepull_function (avi->sinkpad,
207 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate_pull));
208 gst_pad_set_activatepush_function (avi->sinkpad,
209 GST_DEBUG_FUNCPTR (gst_avi_demux_activate_push));
210 gst_pad_set_chain_function (avi->sinkpad,
211 GST_DEBUG_FUNCPTR (gst_avi_demux_chain));
212 gst_pad_set_event_function (avi->sinkpad,
213 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_sink_event));
214 gst_element_add_pad (GST_ELEMENT_CAST (avi), avi->sinkpad);
216 avi->adapter = gst_adapter_new ();
218 gst_avi_demux_reset (avi);
222 gst_avi_demux_finalize (GObject * object)
224 GstAviDemux *avi = GST_AVI_DEMUX (object);
226 GST_DEBUG ("AVI: finalize");
228 g_object_unref (avi->adapter);
230 G_OBJECT_CLASS (parent_class)->finalize (object);
234 gst_avi_demux_reset_stream (GstAviDemux * avi, GstAviStream * stream)
236 g_free (stream->strh);
237 g_free (stream->strf.data);
238 g_free (stream->name);
239 g_free (stream->index);
240 g_free (stream->indexes);
241 if (stream->initdata)
242 gst_buffer_unref (stream->initdata);
243 if (stream->extradata)
244 gst_buffer_unref (stream->extradata);
246 if (stream->exposed) {
247 gst_pad_set_active (stream->pad, FALSE);
248 gst_element_remove_pad (GST_ELEMENT_CAST (avi), stream->pad);
250 gst_object_unref (stream->pad);
252 if (stream->taglist) {
253 gst_tag_list_free (stream->taglist);
254 stream->taglist = NULL;
256 memset (stream, 0, sizeof (GstAviStream));
260 gst_avi_demux_reset (GstAviDemux * avi)
264 GST_DEBUG ("AVI: reset");
266 for (i = 0; i < avi->num_streams; i++)
267 gst_avi_demux_reset_stream (avi, &avi->stream[i]);
269 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
270 avi->num_streams = 0;
271 avi->num_v_streams = 0;
272 avi->num_a_streams = 0;
273 avi->num_t_streams = 0;
274 avi->main_stream = -1;
276 avi->state = GST_AVI_DEMUX_START;
278 avi->building_index = FALSE;
280 avi->index_offset = 0;
284 if (avi->element_index)
285 gst_object_unref (avi->element_index);
286 avi->element_index = NULL;
288 if (avi->seg_event) {
289 gst_event_unref (avi->seg_event);
290 avi->seg_event = NULL;
292 if (avi->seek_event) {
293 gst_event_unref (avi->seek_event);
294 avi->seek_event = NULL;
298 gst_tag_list_free (avi->globaltags);
299 avi->globaltags = NULL;
301 avi->got_tags = TRUE; /* we always want to push global tags */
302 avi->have_eos = FALSE;
303 avi->seekable = TRUE;
305 gst_adapter_clear (avi->adapter);
307 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
311 /* GstElement methods */
314 static const GstFormat *
315 gst_avi_demux_get_src_formats (GstPad * pad)
317 GstAviStream *stream = gst_pad_get_element_private (pad);
319 static const GstFormat src_a_formats[] = {
325 static const GstFormat src_v_formats[] = {
331 return (stream->strh->type == GST_RIFF_FCC_auds ?
332 src_a_formats : src_v_formats);
336 /* assumes stream->strf.auds->av_bps != 0 */
337 static inline GstClockTime
338 avi_stream_convert_bytes_to_time_unchecked (GstAviStream * stream,
341 return gst_util_uint64_scale_int (bytes, GST_SECOND,
342 stream->strf.auds->av_bps);
345 static inline guint64
346 avi_stream_convert_time_to_bytes_unchecked (GstAviStream * stream,
349 return gst_util_uint64_scale_int (time, stream->strf.auds->av_bps,
353 /* assumes stream->strh->rate != 0 */
354 static inline GstClockTime
355 avi_stream_convert_frames_to_time_unchecked (GstAviStream * stream,
358 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
362 static inline guint64
363 avi_stream_convert_time_to_frames_unchecked (GstAviStream * stream,
366 return gst_util_uint64_scale (time, stream->strh->rate,
367 stream->strh->scale * GST_SECOND);
371 gst_avi_demux_src_convert (GstPad * pad,
372 GstFormat src_format,
373 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
375 GstAviStream *stream = gst_pad_get_element_private (pad);
379 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
380 ", dest_format:%s", gst_format_get_name (src_format), src_value,
381 gst_format_get_name (*dest_format));
383 if (G_UNLIKELY (src_format == *dest_format)) {
384 *dest_value = src_value;
387 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
391 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
392 (src_format == GST_FORMAT_BYTES
393 || *dest_format == GST_FORMAT_BYTES))) {
398 switch (src_format) {
399 case GST_FORMAT_TIME:
400 switch (*dest_format) {
401 case GST_FORMAT_BYTES:
402 *dest_value = gst_util_uint64_scale_int (src_value,
403 stream->strf.auds->av_bps, GST_SECOND);
405 case GST_FORMAT_DEFAULT:
407 gst_util_uint64_scale_round (src_value, stream->strh->rate,
408 stream->strh->scale * GST_SECOND);
415 case GST_FORMAT_BYTES:
416 switch (*dest_format) {
417 case GST_FORMAT_TIME:
418 if (stream->strf.auds->av_bps != 0) {
419 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
429 case GST_FORMAT_DEFAULT:
430 switch (*dest_format) {
431 case GST_FORMAT_TIME:
433 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
446 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
447 gst_format_get_name (*dest_format), *dest_value);
451 static const GstQueryType *
452 gst_avi_demux_get_src_query_types (GstPad * pad)
454 static const GstQueryType src_types[] = {
466 gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query)
469 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
471 GstAviStream *stream = gst_pad_get_element_private (pad);
473 if (!stream->strh || !stream->strf.data)
474 return gst_pad_query_default (pad, query);
476 switch (GST_QUERY_TYPE (query)) {
477 case GST_QUERY_POSITION:{
480 GST_DEBUG ("pos query for stream %u: frames %u, bytes %u",
481 stream->num, stream->current_entry, stream->current_total);
483 /* FIXME, this looks clumsy */
484 if (stream->strh->type == GST_RIFF_FCC_auds) {
485 if (stream->is_vbr) {
487 pos = gst_util_uint64_scale ((gint64) stream->current_entry *
488 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
489 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
490 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
491 } else if (stream->strf.auds->av_bps != 0) {
493 pos = gst_util_uint64_scale (stream->current_total, GST_SECOND,
494 (guint64) stream->strf.auds->av_bps);
495 GST_DEBUG_OBJECT (avi,
496 "CBR convert bytes %u, time %" GST_TIME_FORMAT,
497 stream->current_total, GST_TIME_ARGS (pos));
498 } else if (stream->idx_n != 0 && stream->total_bytes != 0) {
499 /* calculate timestamps based on percentage of length */
500 guint64 xlen = avi->avih->us_frame *
501 avi->avih->tot_frames * GST_USECOND;
503 if (stream->is_vbr) {
504 pos = gst_util_uint64_scale (xlen, stream->current_entry,
506 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
507 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
509 pos = gst_util_uint64_scale (xlen, stream->current_total,
510 stream->total_bytes);
511 GST_DEBUG_OBJECT (avi,
512 "CBR perc convert bytes %u, time %" GST_TIME_FORMAT,
513 stream->current_total, GST_TIME_ARGS (pos));
520 if (stream->strh->rate != 0) {
521 pos = gst_util_uint64_scale ((guint64) stream->current_entry *
522 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
524 pos = stream->current_entry * avi->avih->us_frame * GST_USECOND;
528 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
529 gst_query_set_position (query, GST_FORMAT_TIME, pos);
531 GST_WARNING ("pos query failed");
534 case GST_QUERY_DURATION:
537 GstClockTime duration;
539 /* only act on audio or video streams */
540 if (stream->strh->type != GST_RIFF_FCC_auds &&
541 stream->strh->type != GST_RIFF_FCC_vids) {
546 /* take stream duration, fall back to avih duration */
547 if ((duration = stream->duration) == -1)
548 duration = avi->duration;
550 gst_query_parse_duration (query, &fmt, NULL);
553 case GST_FORMAT_TIME:
554 gst_query_set_duration (query, fmt, duration);
556 case GST_FORMAT_DEFAULT:
559 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
562 if (stream->idx_n >= 0)
563 gst_query_set_duration (query, fmt, stream->idx_n);
564 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
565 duration, &fmt, &dur))
566 gst_query_set_duration (query, fmt, dur);
575 case GST_QUERY_SEEKING:{
578 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
579 if (fmt == GST_FORMAT_TIME) {
580 gboolean seekable = TRUE;
582 if (avi->streaming) {
583 seekable = avi->seekable;
586 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
587 0, stream->duration);
592 case GST_QUERY_CONVERT:{
593 GstFormat src_fmt, dest_fmt;
594 gint64 src_val, dest_val;
596 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
597 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
599 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
601 res = gst_pad_query_default (pad, query);
605 res = gst_pad_query_default (pad, query);
609 gst_object_unref (avi);
614 static const GstEventMask *
615 gst_avi_demux_get_event_mask (GstPad * pad)
617 static const GstEventMask masks[] = {
618 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
627 gst_avi_demux_seek_streams (GstAviDemux * avi, guint64 offset, gboolean before)
629 GstAviStream *stream;
630 GstIndexEntry *entry;
632 gint64 val, min = offset;
634 for (i = 0; i < avi->num_streams; i++) {
635 stream = &avi->stream[i];
637 entry = gst_index_get_assoc_entry (avi->element_index, stream->index_id,
638 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
639 GST_ASSOCIATION_FLAG_NONE, GST_FORMAT_BYTES, offset);
643 GST_DEBUG_OBJECT (avi, "stream %d, previous entry at %"
644 G_GUINT64_FORMAT, i, val);
645 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
653 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
654 stream->current_entry = 0;
655 stream->current_total = 0;
659 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
660 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT,
663 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &val);
664 stream->current_total = val;
665 gst_index_entry_assoc_map (entry, GST_FORMAT_DEFAULT, &val);
666 stream->current_entry = val;
673 gst_avi_demux_index_entry_offset_search (GstAviIndexEntry * entry,
676 if (entry->offset < *offset)
678 else if (entry->offset > *offset)
684 gst_avi_demux_seek_streams_index (GstAviDemux * avi, guint64 offset,
687 GstAviStream *stream;
688 GstAviIndexEntry *entry;
690 gint64 val, min = offset;
693 for (i = 0; i < avi->num_streams; i++) {
694 stream = &avi->stream[i];
696 /* compensate for chunk header */
699 gst_util_array_binary_search (stream->index, stream->idx_n,
700 sizeof (GstAviIndexEntry),
701 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
702 before ? GST_SEARCH_MODE_BEFORE : GST_SEARCH_MODE_AFTER, &offset, NULL);
706 index = entry - stream->index;
710 GST_DEBUG_OBJECT (avi,
711 "stream %d, previous entry at %" G_GUINT64_FORMAT, i, val);
712 val = stream->index[index].offset;
720 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
721 stream->current_entry = 0;
722 stream->current_total = 0;
726 val = stream->index[index].offset - 8;
727 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT, i,
730 stream->current_total = stream->index[index].total;
731 stream->current_entry = index;
737 #define GST_AVI_SEEK_PUSH_DISPLACE (4 * GST_SECOND)
740 gst_avi_demux_handle_sink_event (GstPad * pad, GstEvent * event)
743 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
745 GST_DEBUG_OBJECT (avi,
746 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
748 switch (GST_EVENT_TYPE (event)) {
749 case GST_EVENT_NEWSEGMENT:
753 gint64 start, stop, time, offset = 0;
757 /* some debug output */
758 gst_segment_init (&segment, GST_FORMAT_UNDEFINED);
759 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
760 &start, &stop, &time);
761 gst_segment_set_newsegment_full (&segment, update, rate, arate, format,
763 GST_DEBUG_OBJECT (avi,
764 "received format %d newsegment %" GST_SEGMENT_FORMAT, format,
767 /* chain will send initial newsegment after pads have been added */
768 if (avi->state != GST_AVI_DEMUX_MOVI) {
769 GST_DEBUG_OBJECT (avi, "still starting, eating event");
773 /* we only expect a BYTE segment, e.g. following a seek */
774 if (format != GST_FORMAT_BYTES) {
775 GST_DEBUG_OBJECT (avi, "unsupported segment format, ignoring");
779 if (avi->have_index) {
780 GstAviIndexEntry *entry;
781 guint i = 0, index = 0, k = 0;
782 GstAviStream *stream;
784 /* compensate chunk header, stored index offset points after header */
786 /* find which stream we're on */
788 stream = &avi->stream[i];
790 /* find the index for start bytes offset */
791 entry = gst_util_array_binary_search (stream->index,
792 stream->idx_n, sizeof (GstAviIndexEntry),
793 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
794 GST_SEARCH_MODE_AFTER, &start, NULL);
798 index = entry - stream->index;
800 /* we are on the stream with a chunk start offset closest to start */
801 if (!offset || stream->index[index].offset < offset) {
802 offset = stream->index[index].offset;
805 /* exact match needs no further searching */
806 if (stream->index[index].offset == start)
808 } while (++i < avi->num_streams);
811 stream = &avi->stream[k];
813 /* so we have no idea what is to come, or where we are */
815 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
819 /* get the ts corresponding to start offset bytes for the stream */
820 gst_avi_demux_get_buffer_info (avi, stream, index,
821 (GstClockTime *) & time, NULL, NULL, NULL);
822 } else if (avi->element_index) {
823 GstIndexEntry *entry;
825 /* Let's check if we have an index entry for this position */
826 entry = gst_index_get_assoc_entry (avi->element_index, avi->index_id,
827 GST_INDEX_LOOKUP_AFTER, GST_ASSOCIATION_FLAG_NONE,
828 GST_FORMAT_BYTES, start);
830 /* we can not go where we have not yet been before ... */
832 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
836 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &time);
837 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &offset);
839 GST_WARNING_OBJECT (avi, "no index data, forcing EOS");
843 stop = GST_CLOCK_TIME_NONE;
845 /* set up segment and send downstream */
846 gst_segment_set_newsegment_full (&avi->segment, update, rate, arate,
847 GST_FORMAT_TIME, time, stop, time);
848 GST_DEBUG_OBJECT (avi, "Pushing newseg update %d, rate %g, "
849 "applied rate %g, format %d, start %" G_GINT64_FORMAT ", "
850 "stop %" G_GINT64_FORMAT, update, rate, arate, GST_FORMAT_TIME,
852 gst_avi_demux_push_event (avi,
853 gst_event_new_new_segment_full (update, rate, arate, GST_FORMAT_TIME,
856 GST_DEBUG_OBJECT (avi, "next chunk expected at %" G_GINT64_FORMAT, start);
858 /* adjust state for streaming thread accordingly */
860 gst_avi_demux_seek_streams_index (avi, offset, FALSE);
862 gst_avi_demux_seek_streams (avi, offset, FALSE);
864 /* set up streaming thread */
865 g_assert (offset >= start);
867 avi->todrop = offset - start;
870 gst_event_unref (event);
875 avi->have_eos = TRUE;
880 if (avi->state != GST_AVI_DEMUX_MOVI) {
881 gst_event_unref (event);
882 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
883 (NULL), ("got eos and didn't receive a complete header object"));
884 } else if (!gst_avi_demux_push_event (avi, event)) {
885 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
886 (NULL), ("got eos but no streams (yet)"));
890 case GST_EVENT_FLUSH_STOP:
894 gst_adapter_clear (avi->adapter);
895 avi->have_eos = FALSE;
896 for (i = 0; i < avi->num_streams; i++) {
897 avi->stream[i].last_flow = GST_FLOW_OK;
898 avi->stream[i].discont = TRUE;
900 /* fall through to default case so that the event gets passed downstream */
903 res = gst_pad_event_default (pad, event);
907 gst_object_unref (avi);
913 gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event)
916 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
918 GST_DEBUG_OBJECT (avi,
919 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
921 switch (GST_EVENT_TYPE (event)) {
923 if (!avi->streaming) {
924 res = gst_avi_demux_handle_seek (avi, pad, event);
926 res = gst_avi_demux_handle_seek_push (avi, pad, event);
928 gst_event_unref (event);
931 case GST_EVENT_NAVIGATION:
933 gst_event_unref (event);
936 res = gst_pad_event_default (pad, event);
940 gst_object_unref (avi);
945 /* streaming helper (push) */
948 * gst_avi_demux_peek_chunk_info:
950 * @tag: holder for tag
951 * @size: holder for tag size
953 * Peek next chunk info (tag and size)
955 * Returns: TRUE when one chunk info has been got
958 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
960 const guint8 *data = NULL;
962 if (gst_adapter_available (avi->adapter) < 8)
965 data = gst_adapter_peek (avi->adapter, 8);
966 *tag = GST_READ_UINT32_LE (data);
967 *size = GST_READ_UINT32_LE (data + 4);
973 * gst_avi_demux_peek_chunk:
975 * @tag: holder for tag
976 * @size: holder for tag size
978 * Peek enough data for one full chunk
980 * Returns: %TRUE when one chunk has been got
983 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
985 guint32 peek_size = 0;
988 if (!gst_avi_demux_peek_chunk_info (avi, tag, size))
991 /* size 0 -> empty data buffer would surprise most callers,
992 * large size -> do not bother trying to squeeze that into adapter,
993 * so we throw poor man's exception, which can be caught if caller really
994 * wants to handle 0 size chunk */
995 if (!(*size) || (*size) >= (1 << 30))
998 peek_size = (*size + 1) & ~1;
999 available = gst_adapter_available (avi->adapter);
1001 GST_DEBUG_OBJECT (avi,
1002 "Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
1003 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
1005 if (available < (8 + peek_size))
1013 GST_INFO_OBJECT (avi, "Failed to peek");
1018 GST_INFO_OBJECT (avi,
1019 "Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT, *size,
1020 GST_FOURCC_ARGS (*tag));
1021 /* chain should give up */
1022 avi->abort_buffering = TRUE;
1027 GST_INFO_OBJECT (avi, "need more %d < %" G_GUINT32_FORMAT,
1028 available, 8 + peek_size);
1036 * gst_avi_demux_parse_file_header:
1037 * @element: caller element (used for errors/debug).
1038 * @buf: input data to be used for parsing.
1040 * "Open" a RIFF/AVI file. The buffer should be at least 12
1041 * bytes long. Takes ownership of @buf.
1043 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
1044 * Throws an error, caller should error out (fatal).
1047 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
1052 stamp = gst_util_get_timestamp ();
1054 /* riff_parse posts an error */
1055 if (!gst_riff_parse_file_header (element, buf, &doctype))
1058 if (doctype != GST_RIFF_RIFF_AVI)
1061 stamp = gst_util_get_timestamp () - stamp;
1062 GST_DEBUG_OBJECT (element, "header parsing took %" GST_TIME_FORMAT,
1063 GST_TIME_ARGS (stamp));
1070 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
1071 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
1072 GST_FOURCC_ARGS (doctype)));
1078 * Read AVI file tag when streaming
1080 static GstFlowReturn
1081 gst_avi_demux_stream_init_push (GstAviDemux * avi)
1083 if (gst_adapter_available (avi->adapter) >= 12) {
1086 tmp = gst_adapter_take_buffer (avi->adapter, 12);
1088 GST_DEBUG ("Parsing avi header");
1089 if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), tmp)) {
1090 return GST_FLOW_ERROR;
1092 GST_DEBUG ("header ok");
1095 avi->state = GST_AVI_DEMUX_HEADER;
1103 static GstFlowReturn
1104 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
1107 GstBuffer *buf = NULL;
1109 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
1110 if (res != GST_FLOW_OK)
1112 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
1122 GST_DEBUG_OBJECT (avi, "error parsing file header");
1123 return GST_FLOW_ERROR;
1127 /* AVI header handling */
1129 * gst_avi_demux_parse_avih:
1130 * @avi: caller element (used for errors/debug).
1131 * @buf: input data to be used for parsing.
1132 * @avih: pointer to structure (filled in by function) containing
1133 * stream information (such as flags, number of streams, etc.).
1135 * Read 'avih' header. Discards buffer after use.
1137 * Returns: TRUE on success, FALSE otherwise. Throws an error if
1138 * the header is invalid. The caller should error out
1142 gst_avi_demux_parse_avih (GstAviDemux * avi,
1143 GstBuffer * buf, gst_riff_avih ** _avih)
1145 gst_riff_avih *avih;
1150 if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_avih))
1151 goto avih_too_small;
1153 avih = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1155 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1156 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
1157 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
1158 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
1159 avih->flags = GUINT32_FROM_LE (avih->flags);
1160 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
1161 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
1162 avih->streams = GUINT32_FROM_LE (avih->streams);
1163 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
1164 avih->width = GUINT32_FROM_LE (avih->width);
1165 avih->height = GUINT32_FROM_LE (avih->height);
1166 avih->scale = GUINT32_FROM_LE (avih->scale);
1167 avih->rate = GUINT32_FROM_LE (avih->rate);
1168 avih->start = GUINT32_FROM_LE (avih->start);
1169 avih->length = GUINT32_FROM_LE (avih->length);
1173 GST_INFO_OBJECT (avi, "avih tag found:");
1174 GST_INFO_OBJECT (avi, " us_frame %u", avih->us_frame);
1175 GST_INFO_OBJECT (avi, " max_bps %u", avih->max_bps);
1176 GST_INFO_OBJECT (avi, " pad_gran %u", avih->pad_gran);
1177 GST_INFO_OBJECT (avi, " flags 0x%08x", avih->flags);
1178 GST_INFO_OBJECT (avi, " tot_frames %u", avih->tot_frames);
1179 GST_INFO_OBJECT (avi, " init_frames %u", avih->init_frames);
1180 GST_INFO_OBJECT (avi, " streams %u", avih->streams);
1181 GST_INFO_OBJECT (avi, " bufsize %u", avih->bufsize);
1182 GST_INFO_OBJECT (avi, " width %u", avih->width);
1183 GST_INFO_OBJECT (avi, " height %u", avih->height);
1184 GST_INFO_OBJECT (avi, " scale %u", avih->scale);
1185 GST_INFO_OBJECT (avi, " rate %u", avih->rate);
1186 GST_INFO_OBJECT (avi, " start %u", avih->start);
1187 GST_INFO_OBJECT (avi, " length %u", avih->length);
1190 gst_buffer_unref (buf);
1192 if (avih->us_frame != 0 && avih->tot_frames != 0)
1194 (guint64) avih->us_frame * (guint64) avih->tot_frames * 1000;
1196 avi->duration = GST_CLOCK_TIME_NONE;
1198 GST_INFO_OBJECT (avi, " header duration %" GST_TIME_FORMAT,
1199 GST_TIME_ARGS (avi->duration));
1206 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No buffer"));
1211 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
1212 ("Too small avih (%d available, %d needed)",
1213 GST_BUFFER_SIZE (buf), (int) sizeof (gst_riff_avih)));
1214 gst_buffer_unref (buf);
1220 * gst_avi_demux_parse_superindex:
1221 * @avi: caller element (used for debugging/errors).
1222 * @buf: input data to use for parsing.
1223 * @locations: locations in the file (byte-offsets) that contain
1224 * the actual indexes (see get_avi_demux_parse_subindex()).
1225 * The array ends with GST_BUFFER_OFFSET_NONE.
1227 * Reads superindex (openDML-2 spec stuff) from the provided data.
1229 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
1230 * on error, but they are not fatal.
1233 gst_avi_demux_parse_superindex (GstAviDemux * avi,
1234 GstBuffer * buf, guint64 ** _indexes)
1244 size = buf ? GST_BUFFER_SIZE (buf) : 0;
1248 data = GST_BUFFER_DATA (buf);
1250 /* check type of index. The opendml2 specs state that
1251 * there should be 4 dwords per array entry. Type can be
1252 * either frame or field (and we don't care). */
1253 if (GST_READ_UINT16_LE (data) != 4 ||
1254 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
1255 GST_WARNING_OBJECT (avi,
1256 "Superindex for stream has unexpected "
1257 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1258 GST_READ_UINT16_LE (data), data[2], data[3]);
1259 bpe = GST_READ_UINT16_LE (data) * 4;
1261 num = GST_READ_UINT32_LE (&data[4]);
1263 GST_DEBUG_OBJECT (avi, "got %d indexes", num);
1265 indexes = g_new (guint64, num + 1);
1266 for (i = 0; i < num; i++) {
1267 if (size < 24 + bpe * (i + 1))
1269 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
1270 GST_DEBUG_OBJECT (avi, "index %d at %" G_GUINT64_FORMAT, i, indexes[i]);
1272 indexes[i] = GST_BUFFER_OFFSET_NONE;
1273 *_indexes = indexes;
1275 gst_buffer_unref (buf);
1282 GST_ERROR_OBJECT (avi,
1283 "Not enough data to parse superindex (%d available, 24 needed)", size);
1285 gst_buffer_unref (buf);
1290 /* add an entry to the index of a stream. @num should be an estimate of the
1291 * total amount of index entries for all streams and is used to dynamically
1292 * allocate memory for the index entries. */
1293 static inline gboolean
1294 gst_avi_demux_add_index (GstAviDemux * avi, GstAviStream * stream,
1295 guint num, GstAviIndexEntry * entry)
1297 /* ensure index memory */
1298 if (G_UNLIKELY (stream->idx_n >= stream->idx_max)) {
1299 guint idx_max = stream->idx_max;
1300 GstAviIndexEntry *new_idx;
1302 /* we need to make some more room */
1304 /* initial size guess, assume each stream has an equal amount of entries,
1305 * overshoot with at least 8K */
1306 idx_max = (num / avi->num_streams) + (8192 / sizeof (GstAviIndexEntry));
1308 idx_max += 8192 / sizeof (GstAviIndexEntry);
1309 GST_DEBUG_OBJECT (avi, "expanded index from %u to %u",
1310 stream->idx_max, idx_max);
1312 new_idx = g_try_renew (GstAviIndexEntry, stream->index, idx_max);
1313 /* out of memory, if this fails stream->index is untouched. */
1314 if (G_UNLIKELY (!new_idx))
1317 stream->index = new_idx;
1318 stream->idx_max = idx_max;
1321 /* update entry total and stream stats. The entry total can be converted to
1322 * the timestamp of the entry easily. */
1323 if (stream->strh->type == GST_RIFF_FCC_auds) {
1326 if (stream->is_vbr) {
1327 entry->total = stream->total_blocks;
1329 entry->total = stream->total_bytes;
1331 blockalign = stream->strf.auds->blockalign;
1333 stream->total_blocks += DIV_ROUND_UP (entry->size, blockalign);
1335 stream->total_blocks++;
1337 if (stream->is_vbr) {
1338 entry->total = stream->idx_n;
1340 entry->total = stream->total_bytes;
1343 stream->total_bytes += entry->size;
1344 if (ENTRY_IS_KEYFRAME (entry))
1345 stream->n_keyframes++;
1348 GST_LOG_OBJECT (avi,
1349 "Adding stream %u, index entry %d, kf %d, size %u "
1350 ", offset %" G_GUINT64_FORMAT ", total %" G_GUINT64_FORMAT, stream->num,
1351 stream->idx_n, ENTRY_IS_KEYFRAME (entry), entry->size, entry->offset,
1353 stream->index[stream->idx_n++] = *entry;
1358 /* given @entry_n in @stream, calculate info such as timestamps and
1359 * offsets for the entry. */
1361 gst_avi_demux_get_buffer_info (GstAviDemux * avi, GstAviStream * stream,
1362 guint entry_n, GstClockTime * timestamp, GstClockTime * ts_end,
1363 guint64 * offset, guint64 * offset_end)
1365 GstAviIndexEntry *entry;
1367 entry = &stream->index[entry_n];
1369 if (stream->is_vbr) {
1370 /* VBR stream next timestamp */
1371 if (stream->strh->type == GST_RIFF_FCC_auds) {
1374 avi_stream_convert_frames_to_time_unchecked (stream, entry->total);
1376 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1381 avi_stream_convert_frames_to_time_unchecked (stream, entry_n);
1383 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1386 } else if (stream->strh->type == GST_RIFF_FCC_auds) {
1387 /* constant rate stream */
1390 avi_stream_convert_bytes_to_time_unchecked (stream, entry->total);
1392 *ts_end = avi_stream_convert_bytes_to_time_unchecked (stream,
1393 entry->total + entry->size);
1395 if (stream->strh->type == GST_RIFF_FCC_vids) {
1396 /* video offsets are the frame number */
1400 *offset_end = entry_n + 1;
1402 /* no offsets for audio */
1410 /* collect and debug stats about the indexes for all streams.
1411 * This method is also responsible for filling in the stream duration
1412 * as measured by the amount of index entries.
1414 * Returns TRUE if the index is not empty, else FALSE */
1416 gst_avi_demux_do_index_stats (GstAviDemux * avi)
1418 guint total_idx = 0;
1420 #ifndef GST_DISABLE_GST_DEBUG
1421 guint total_max = 0;
1424 /* get stream stats now */
1425 for (i = 0; i < avi->num_streams; i++) {
1426 GstAviStream *stream;
1428 if (G_UNLIKELY (!(stream = &avi->stream[i])))
1430 if (G_UNLIKELY (!stream->strh))
1432 if (G_UNLIKELY (!stream->index || stream->idx_n == 0))
1435 /* we interested in the end_ts of the last entry, which is the total
1436 * duration of this stream */
1437 gst_avi_demux_get_buffer_info (avi, stream, stream->idx_n - 1,
1438 NULL, &stream->idx_duration, NULL, NULL);
1440 total_idx += stream->idx_n;
1441 #ifndef GST_DISABLE_GST_DEBUG
1442 total_max += stream->idx_max;
1444 GST_INFO_OBJECT (avi, "Stream %d, dur %" GST_TIME_FORMAT ", %6u entries, "
1445 "%5u keyframes, entry size = %2u, total size = %10u, allocated %10u",
1446 i, GST_TIME_ARGS (stream->idx_duration), stream->idx_n,
1447 stream->n_keyframes, (guint) sizeof (GstAviIndexEntry),
1448 (guint) (stream->idx_n * sizeof (GstAviIndexEntry)),
1449 (guint) (stream->idx_max * sizeof (GstAviIndexEntry)));
1451 total_idx *= sizeof (GstAviIndexEntry);
1452 #ifndef GST_DISABLE_GST_DEBUG
1453 total_max *= sizeof (GstAviIndexEntry);
1455 GST_INFO_OBJECT (avi, "%u bytes for index vs %u ideally, %u wasted",
1456 total_max, total_idx, total_max - total_idx);
1458 if (total_idx == 0) {
1459 GST_WARNING_OBJECT (avi, "Index is empty !");
1466 * gst_avi_demux_parse_subindex:
1468 * @buf: input data to use for parsing.
1469 * @stream: stream context.
1470 * @entries_list: a list (returned by the function) containing all the
1471 * indexes parsed in this specific subindex. The first
1472 * entry is also a pointer to allocated memory that needs
1473 * to be free´ed. May be NULL if no supported indexes were
1476 * Reads superindex (openDML-2 spec stuff) from the provided data.
1477 * The buffer should contain a GST_RIFF_TAG_ix?? chunk.
1479 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1480 * throw an error, caller should bail out asap.
1483 gst_avi_demux_parse_subindex (GstAviDemux * avi, GstAviStream * stream,
1495 size = GST_BUFFER_SIZE (buf);
1501 data = GST_BUFFER_DATA (buf);
1503 /* We don't support index-data yet */
1505 goto not_implemented;
1507 /* check type of index. The opendml2 specs state that
1508 * there should be 4 dwords per array entry. Type can be
1509 * either frame or field (and we don't care). */
1510 bpe = (data[2] & 0x01) ? 12 : 8;
1511 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1512 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1513 GST_WARNING_OBJECT (avi,
1514 "Superindex for stream %d has unexpected "
1515 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1516 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1517 bpe = GST_READ_UINT16_LE (data) * 4;
1519 num = GST_READ_UINT32_LE (&data[4]);
1520 baseoff = GST_READ_UINT64_LE (&data[12]);
1522 /* If there's nothing, just return ! */
1526 GST_INFO_OBJECT (avi, "Parsing subindex, nr_entries = %6d", num);
1528 for (i = 0; i < num; i++) {
1529 GstAviIndexEntry entry;
1531 if (size < 24 + bpe * (i + 1))
1534 /* fill in offset and size. offset contains the keyframe flag in the
1536 entry.offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1537 entry.size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1539 if (stream->strh->type == GST_RIFF_FCC_auds) {
1540 /* all audio frames are keyframes */
1541 ENTRY_SET_KEYFRAME (&entry);
1543 /* else read flags */
1544 entry.flags = (entry.size & 0x80000000) ? 0 : GST_AVI_KEYFRAME;
1546 entry.size &= ~0x80000000;
1549 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
1552 gst_buffer_unref (buf);
1559 GST_ERROR_OBJECT (avi,
1560 "Not enough data to parse subindex (%d available, 24 needed)", size);
1561 gst_buffer_unref (buf);
1562 return TRUE; /* continue */
1566 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1567 ("Subindex-is-data is not implemented"));
1568 gst_buffer_unref (buf);
1573 GST_DEBUG_OBJECT (avi, "the index is empty");
1574 gst_buffer_unref (buf);
1579 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1580 ("Cannot allocate memory for %u*%u=%u bytes",
1581 (guint) sizeof (GstAviIndexEntry), num,
1582 (guint) sizeof (GstAviIndexEntry) * num));
1583 gst_buffer_unref (buf);
1589 * Create and push a flushing seek event upstream
1592 perform_seek_to_offset (GstAviDemux * demux, guint64 offset)
1597 GST_DEBUG_OBJECT (demux, "Seeking to %" G_GUINT64_FORMAT, offset);
1600 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1601 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1602 GST_SEEK_TYPE_NONE, -1);
1604 res = gst_pad_push_event (demux->sinkpad, event);
1607 demux->offset = offset;
1612 * Read AVI index when streaming
1615 gst_avi_demux_read_subindexes_push (GstAviDemux * avi)
1617 guint32 tag = 0, size;
1618 GstBuffer *buf = NULL;
1621 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1623 if (avi->odml_subidxs[avi->odml_subidx] != avi->offset)
1626 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1629 /* this is the ODML chunk we expect */
1630 odml_stream = avi->odml_stream;
1632 if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + odml_stream / 10,
1633 '0' + odml_stream % 10)) &&
1634 (tag != GST_MAKE_FOURCC ('0' + odml_stream / 10,
1635 '0' + odml_stream % 10, 'i', 'x'))) {
1636 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1637 GST_FOURCC_ARGS (tag));
1641 avi->offset += 8 + GST_ROUND_UP_2 (size);
1642 /* flush chunk header so we get just the 'size' payload data */
1643 gst_adapter_flush (avi->adapter, 8);
1644 buf = gst_adapter_take_buffer (avi->adapter, size);
1646 if (!gst_avi_demux_parse_subindex (avi, &avi->stream[odml_stream], buf))
1649 /* we parsed the index, go to next subindex */
1652 if (avi->odml_subidxs[avi->odml_subidx] == GST_BUFFER_OFFSET_NONE) {
1653 /* we reached the end of the indexes for this stream, move to the next
1654 * stream to handle the first index */
1656 avi->odml_subidx = 0;
1658 if (avi->odml_stream < avi->num_streams) {
1659 /* there are more indexes */
1660 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
1662 /* we're done, get stream stats now */
1663 avi->have_index = gst_avi_demux_do_index_stats (avi);
1669 /* seek to next index */
1670 return perform_seek_to_offset (avi, avi->odml_subidxs[avi->odml_subidx]);
1677 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi)
1683 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1685 for (n = 0; n < avi->num_streams; n++) {
1686 GstAviStream *stream = &avi->stream[n];
1688 if (stream->indexes == NULL)
1691 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1692 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi), avi->sinkpad,
1693 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1695 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1696 '0' + stream->num % 10)) &&
1697 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1698 '0' + stream->num % 10, 'i', 'x'))) {
1699 /* Some ODML files (created by god knows what muxer) have a ##ix format
1700 * instead of the 'official' ix##. They are still valid though. */
1701 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1702 GST_FOURCC_ARGS (tag));
1703 gst_buffer_unref (buf);
1707 if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1711 g_free (stream->indexes);
1712 stream->indexes = NULL;
1714 /* get stream stats now */
1715 avi->have_index = gst_avi_demux_do_index_stats (avi);
1719 * gst_avi_demux_riff_parse_vprp:
1720 * @element: caller element (used for debugging/error).
1721 * @buf: input data to be used for parsing, stripped from header.
1722 * @vprp: a pointer (returned by this function) to a filled-in vprp
1723 * structure. Caller should free it.
1725 * Parses a video stream´s vprp. This function takes ownership of @buf.
1727 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1728 * should be skipped on error, but it is not fatal.
1731 gst_avi_demux_riff_parse_vprp (GstElement * element,
1732 GstBuffer * buf, gst_riff_vprp ** _vprp)
1734 gst_riff_vprp *vprp;
1737 g_return_val_if_fail (buf != NULL, FALSE);
1738 g_return_val_if_fail (_vprp != NULL, FALSE);
1740 if (GST_BUFFER_SIZE (buf) < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1743 vprp = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1745 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1746 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1747 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1748 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1749 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1750 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1751 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1752 vprp->width = GUINT32_FROM_LE (vprp->width);
1753 vprp->height = GUINT32_FROM_LE (vprp->height);
1754 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1758 /* calculate fields based on size */
1759 k = (GST_BUFFER_SIZE (buf) - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) /
1761 if (vprp->fields > k) {
1762 GST_WARNING_OBJECT (element,
1763 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1766 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1767 GST_WARNING_OBJECT (element,
1768 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1769 GST_RIFF_VPRP_VIDEO_FIELDS);
1770 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1772 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1773 for (k = 0; k < vprp->fields; k++) {
1774 gst_riff_vprp_video_field_desc *fd;
1776 fd = &vprp->field_info[k];
1777 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1778 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1779 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1780 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1781 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1782 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1783 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1784 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1789 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1790 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1791 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1792 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1793 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1794 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1795 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1796 vprp->aspect & 0xffff);
1797 GST_INFO_OBJECT (element, " width %d", vprp->width);
1798 GST_INFO_OBJECT (element, " height %d", vprp->height);
1799 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1800 for (k = 0; k < vprp->fields; k++) {
1801 gst_riff_vprp_video_field_desc *fd;
1803 fd = &(vprp->field_info[k]);
1804 GST_INFO_OBJECT (element, " field %u description:", k);
1805 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1806 fd->compressed_bm_height);
1807 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1808 fd->compressed_bm_width);
1809 GST_INFO_OBJECT (element, " valid_bm_height %d",
1810 fd->valid_bm_height);
1811 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1812 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1813 fd->valid_bm_x_offset);
1814 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1815 fd->valid_bm_y_offset);
1816 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1817 fd->video_x_t_offset);
1818 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1821 gst_buffer_unref (buf);
1830 GST_ERROR_OBJECT (element,
1831 "Too small vprp (%d available, at least %d needed)",
1832 GST_BUFFER_SIZE (buf),
1833 (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1834 gst_buffer_unref (buf);
1840 gst_avi_demux_expose_streams (GstAviDemux * avi, gboolean force)
1844 GST_DEBUG_OBJECT (avi, "force : %d", force);
1846 for (i = 0; i < avi->num_streams; i++) {
1847 GstAviStream *stream = &avi->stream[i];
1849 if (force || stream->idx_n != 0) {
1850 GST_LOG_OBJECT (avi, "Added pad %s with caps %" GST_PTR_FORMAT,
1851 GST_PAD_NAME (stream->pad), GST_PAD_CAPS (stream->pad));
1852 gst_element_add_pad ((GstElement *) avi, stream->pad);
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);
1865 * gst_avi_demux_parse_stream:
1866 * @avi: calling element (used for debugging/errors).
1867 * @buf: input buffer used to parse the stream.
1869 * Parses all subchunks in a strl chunk (which defines a single
1870 * stream). Discards the buffer after use. This function will
1871 * increment the stream counter internally.
1873 * Returns: whether the stream was identified successfully.
1874 * Errors are not fatal. It does indicate the stream
1878 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
1880 GstAviStream *stream;
1881 GstElementClass *klass;
1882 GstPadTemplate *templ;
1883 GstBuffer *sub = NULL;
1886 gchar *codec_name = NULL, *padname = NULL;
1887 const gchar *tag_name;
1888 GstCaps *caps = NULL;
1890 GstElement *element;
1891 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
1892 gst_riff_vprp *vprp = NULL;
1894 element = GST_ELEMENT_CAST (avi);
1896 GST_DEBUG_OBJECT (avi, "Parsing stream");
1898 if (avi->num_streams >= GST_AVI_DEMUX_MAX_STREAMS) {
1899 GST_WARNING_OBJECT (avi,
1900 "maximum no of streams (%d) exceeded, ignoring stream",
1901 GST_AVI_DEMUX_MAX_STREAMS);
1902 gst_buffer_unref (buf);
1903 /* not a fatal error, let's say */
1907 stream = &avi->stream[avi->num_streams];
1909 /* initial settings */
1910 stream->idx_duration = GST_CLOCK_TIME_NONE;
1911 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1912 stream->duration = GST_CLOCK_TIME_NONE;
1914 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
1915 /* sub can be NULL if the chunk is empty */
1917 GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
1918 GST_FOURCC_ARGS (tag));
1922 case GST_RIFF_TAG_strh:
1924 gst_riff_strh *strh;
1927 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
1930 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
1931 /* ownership given away */
1933 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
1937 strh = stream->strh;
1938 /* sanity check; stream header frame rate matches global header
1940 if (stream->strh->type == GST_RIFF_FCC_vids) {
1942 GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
1944 s_dur = gst_util_uint64_scale (GST_SECOND, strh->scale, strh->rate);
1945 GST_DEBUG_OBJECT (avi, "verifying stream framerate %d/%d, "
1946 "frame duration = %d ms", strh->rate, strh->scale,
1947 (gint) (s_dur / GST_MSECOND));
1948 if (h_dur > (10 * GST_MSECOND) && (s_dur > 10 * h_dur)) {
1949 strh->rate = GST_SECOND / GST_USECOND;
1950 strh->scale = h_dur / GST_USECOND;
1951 GST_DEBUG_OBJECT (avi, "correcting stream framerate to %d/%d",
1952 strh->rate, strh->scale);
1955 /* determine duration as indicated by header */
1956 stream->hdr_duration = gst_util_uint64_scale ((guint64) strh->length *
1957 strh->scale, GST_SECOND, (guint64) strh->rate);
1958 GST_INFO ("Stream duration according to header: %" GST_TIME_FORMAT,
1959 GST_TIME_ARGS (stream->hdr_duration));
1960 if (stream->hdr_duration == 0)
1961 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1966 case GST_RIFF_TAG_strf:
1968 gboolean res = FALSE;
1971 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
1975 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
1978 switch (stream->strh->type) {
1979 case GST_RIFF_FCC_vids:
1980 stream->is_vbr = TRUE;
1981 res = gst_riff_parse_strf_vids (element, sub,
1982 &stream->strf.vids, &stream->extradata);
1984 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
1986 case GST_RIFF_FCC_auds:
1987 stream->is_vbr = (stream->strh->samplesize == 0)
1988 && stream->strh->scale > 1;
1990 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
1991 &stream->extradata);
1993 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
1994 stream->is_vbr, res);
1996 case GST_RIFF_FCC_iavs:
1997 stream->is_vbr = TRUE;
1998 res = gst_riff_parse_strf_iavs (element, sub,
1999 &stream->strf.iavs, &stream->extradata);
2001 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
2003 case GST_RIFF_FCC_txts:
2004 /* nothing to parse here */
2005 stream->is_vbr = (stream->strh->samplesize == 0)
2006 && (stream->strh->scale > 1);
2010 GST_ERROR_OBJECT (avi,
2011 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
2012 GST_FOURCC_ARGS (stream->strh->type));
2016 gst_buffer_unref (sub);
2024 case GST_RIFF_TAG_vprp:
2027 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
2031 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
2035 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
2039 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
2040 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
2041 /* not considered fatal */
2049 case GST_RIFF_TAG_strd:
2050 if (stream->initdata)
2051 gst_buffer_unref (stream->initdata);
2052 stream->initdata = sub;
2055 case GST_RIFF_TAG_strn:
2056 g_free (stream->name);
2059 g_strndup ((gchar *) GST_BUFFER_DATA (sub),
2060 (gsize) GST_BUFFER_SIZE (sub));
2061 gst_buffer_unref (sub);
2064 stream->name = g_strdup ("");
2066 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
2069 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
2070 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
2071 '0' + avi->num_streams % 10)) {
2072 g_free (stream->indexes);
2073 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
2074 stream->superindex = TRUE;
2078 GST_WARNING_OBJECT (avi,
2079 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
2080 GST_FOURCC_ARGS (tag));
2082 case GST_RIFF_TAG_JUNQ:
2083 case GST_RIFF_TAG_JUNK:
2087 gst_buffer_unref (sub);
2093 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
2098 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
2102 /* get class to figure out the template */
2103 klass = GST_ELEMENT_GET_CLASS (avi);
2105 /* we now have all info, let´s set up a pad and a caps and be done */
2106 /* create stream name + pad */
2107 switch (stream->strh->type) {
2108 case GST_RIFF_FCC_vids:{
2111 fourcc = (stream->strf.vids->compression) ?
2112 stream->strf.vids->compression : stream->strh->fcc_handler;
2113 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
2114 templ = gst_element_class_get_pad_template (klass, "video_%02d");
2115 caps = gst_riff_create_video_caps (fourcc, stream->strh,
2116 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
2118 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2119 GST_TYPE_FOURCC, fourcc, NULL);
2120 } else if (got_vprp && vprp) {
2121 guint32 aspect_n, aspect_d;
2124 aspect_n = vprp->aspect >> 16;
2125 aspect_d = vprp->aspect & 0xffff;
2126 /* calculate the pixel aspect ratio using w/h and aspect ratio */
2127 n = aspect_n * stream->strf.vids->height;
2128 d = aspect_d * stream->strf.vids->width;
2130 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
2132 /* very local, not needed elsewhere */
2136 tag_name = GST_TAG_VIDEO_CODEC;
2137 avi->num_v_streams++;
2140 case GST_RIFF_FCC_auds:{
2141 padname = g_strdup_printf ("audio_%02d", avi->num_a_streams);
2142 templ = gst_element_class_get_pad_template (klass, "audio_%02d");
2143 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
2144 stream->strh, stream->strf.auds, stream->extradata,
2145 stream->initdata, &codec_name);
2147 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
2148 G_TYPE_INT, stream->strf.auds->format, NULL);
2150 tag_name = GST_TAG_AUDIO_CODEC;
2151 avi->num_a_streams++;
2154 case GST_RIFF_FCC_iavs:{
2155 guint32 fourcc = stream->strh->fcc_handler;
2157 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
2158 templ = gst_element_class_get_pad_template (klass, "video_%02d");
2159 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
2160 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
2162 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2163 GST_TYPE_FOURCC, fourcc, NULL);
2165 tag_name = GST_TAG_VIDEO_CODEC;
2166 avi->num_v_streams++;
2169 case GST_RIFF_FCC_txts:{
2170 padname = g_strdup_printf ("subtitle_%02d", avi->num_t_streams);
2171 templ = gst_element_class_get_pad_template (klass, "subtitle_%02d");
2172 caps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
2174 avi->num_t_streams++;
2178 g_assert_not_reached ();
2181 /* no caps means no stream */
2183 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
2187 GST_DEBUG_OBJECT (element, "codec-name=%s",
2188 (codec_name ? codec_name : "NULL"));
2189 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
2191 /* set proper settings and add it */
2193 gst_object_unref (stream->pad);
2194 pad = stream->pad = gst_pad_new_from_template (templ, padname);
2197 gst_pad_use_fixed_caps (pad);
2199 gst_pad_set_formats_function (pad,
2200 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
2201 gst_pad_set_event_mask_function (pad,
2202 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
2204 gst_pad_set_event_function (pad,
2205 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
2206 gst_pad_set_query_type_function (pad,
2207 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_query_types));
2208 gst_pad_set_query_function (pad,
2209 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
2211 gst_pad_set_convert_function (pad,
2212 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
2215 if (avi->element_index)
2216 gst_index_get_writer_id (avi->element_index, GST_OBJECT_CAST (stream->pad),
2219 stream->num = avi->num_streams;
2221 stream->start_entry = 0;
2222 stream->step_entry = 0;
2223 stream->stop_entry = 0;
2225 stream->current_entry = -1;
2226 stream->current_total = 0;
2228 stream->last_flow = GST_FLOW_OK;
2229 stream->discont = TRUE;
2231 stream->total_bytes = 0;
2232 stream->total_blocks = 0;
2233 stream->n_keyframes = 0;
2236 stream->idx_max = 0;
2238 gst_pad_set_element_private (pad, stream);
2241 gst_pad_set_caps (pad, caps);
2242 gst_pad_set_active (pad, TRUE);
2243 gst_caps_unref (caps);
2247 if (!stream->taglist)
2248 stream->taglist = gst_tag_list_new ();
2250 avi->got_tags = TRUE;
2252 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
2254 g_free (codec_name);
2257 gst_buffer_unref (buf);
2264 /* unref any mem that may be in use */
2266 gst_buffer_unref (buf);
2268 gst_buffer_unref (sub);
2270 g_free (codec_name);
2271 gst_avi_demux_reset_stream (avi, stream);
2278 * gst_avi_demux_parse_odml:
2279 * @avi: calling element (used for debug/error).
2280 * @buf: input buffer to be used for parsing.
2282 * Read an openDML-2.0 extension header. Fills in the frame number
2283 * in the avi demuxer object when reading succeeds.
2286 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
2290 GstBuffer *sub = NULL;
2292 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
2295 case GST_RIFF_TAG_dmlh:{
2296 gst_riff_dmlh dmlh, *_dmlh;
2299 /* sub == NULL is possible and means an empty buffer */
2300 size = sub ? GST_BUFFER_SIZE (sub) : 0;
2303 if (size < sizeof (gst_riff_dmlh)) {
2304 GST_ERROR_OBJECT (avi,
2305 "DMLH entry is too small (%d bytes, %d needed)",
2306 size, (int) sizeof (gst_riff_dmlh));
2309 _dmlh = (gst_riff_dmlh *) GST_BUFFER_DATA (sub);
2310 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
2312 GST_INFO_OBJECT (avi, "dmlh tag found: totalframes: %u",
2315 avi->avih->tot_frames = dmlh.totalframes;
2320 GST_WARNING_OBJECT (avi,
2321 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
2322 GST_FOURCC_ARGS (tag));
2324 case GST_RIFF_TAG_JUNQ:
2325 case GST_RIFF_TAG_JUNK:
2327 /* skip and move to next chunk */
2329 gst_buffer_unref (sub);
2336 gst_buffer_unref (buf);
2341 gst_avi_demux_index_last (GstAviDemux * avi, GstAviStream * stream)
2343 return stream->idx_n - 1;
2346 /* find a previous entry in the index with the given flags */
2348 gst_avi_demux_index_prev (GstAviDemux * avi, GstAviStream * stream,
2349 guint last, gboolean keyframe)
2351 GstAviIndexEntry *entry;
2354 for (i = last; i > 0; i--) {
2355 entry = &stream->index[i - 1];
2356 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2364 gst_avi_demux_index_next (GstAviDemux * avi, GstAviStream * stream,
2365 guint last, gboolean keyframe)
2367 GstAviIndexEntry *entry;
2370 for (i = last + 1; i < stream->idx_n; i++) {
2371 entry = &stream->index[i];
2372 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2376 return stream->idx_n - 1;
2380 gst_avi_demux_index_entry_search (GstAviIndexEntry * entry, guint64 * total)
2382 if (entry->total < *total)
2384 else if (entry->total > *total)
2390 * gst_avi_demux_index_for_time:
2392 * @stream: the stream
2393 * @time: a time position
2395 * Finds the index entry which time is less or equal than the requested time.
2396 * Try to avoid binary search when we can convert the time to an index
2397 * position directly (for example for video frames with a fixed duration).
2399 * Returns: the found position in the index.
2402 gst_avi_demux_index_for_time (GstAviDemux * avi,
2403 GstAviStream * stream, guint64 time)
2408 GST_LOG_OBJECT (avi, "search time:%" GST_TIME_FORMAT, GST_TIME_ARGS (time));
2410 /* easy (and common) cases */
2411 if (time == 0 || stream->idx_n == 0)
2413 if (time >= stream->idx_duration)
2414 return stream->idx_n - 1;
2416 /* figure out where we need to go. For that we convert the time to an
2417 * index entry or we convert it to a total and then do a binary search. */
2418 if (stream->is_vbr) {
2419 /* VBR stream next timestamp */
2420 if (stream->strh->type == GST_RIFF_FCC_auds) {
2421 total = avi_stream_convert_time_to_frames_unchecked (stream, time);
2423 index = avi_stream_convert_time_to_frames_unchecked (stream, time);
2426 /* constant rate stream */
2427 total = avi_stream_convert_time_to_bytes_unchecked (stream, time);
2431 GstAviIndexEntry *entry;
2433 /* no index, find index with binary search on total */
2434 GST_LOG_OBJECT (avi, "binary search for entry with total %"
2435 G_GUINT64_FORMAT, total);
2437 entry = gst_util_array_binary_search (stream->index,
2438 stream->idx_n, sizeof (GstAviIndexEntry),
2439 (GCompareDataFunc) gst_avi_demux_index_entry_search,
2440 GST_SEARCH_MODE_BEFORE, &total, NULL);
2442 if (entry == NULL) {
2443 GST_LOG_OBJECT (avi, "not found, assume index 0");
2446 index = entry - stream->index;
2447 GST_LOG_OBJECT (avi, "found at %u", index);
2450 GST_LOG_OBJECT (avi, "converted time to index %u", index);
2456 static inline GstAviStream *
2457 gst_avi_demux_stream_for_id (GstAviDemux * avi, guint32 id)
2460 GstAviStream *stream;
2462 /* get the stream for this entry */
2463 stream_nr = CHUNKID_TO_STREAMNR (id);
2464 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2465 GST_WARNING_OBJECT (avi, "invalid stream nr %d", stream_nr);
2468 stream = &avi->stream[stream_nr];
2469 if (G_UNLIKELY (!stream->strh)) {
2470 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2477 * gst_avi_demux_parse_index:
2478 * @avi: calling element (used for debugging/errors).
2479 * @buf: buffer containing the full index.
2481 * Read index entries from the provided buffer.
2482 * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
2485 gst_avi_demux_parse_index (GstAviDemux * avi, GstBuffer * buf)
2491 gst_riff_index_entry *index;
2493 GstAviStream *stream;
2494 GstAviIndexEntry entry;
2500 data = GST_BUFFER_DATA (buf);
2501 size = GST_BUFFER_SIZE (buf);
2503 stamp = gst_util_get_timestamp ();
2505 /* see how many items in the index */
2506 num = size / sizeof (gst_riff_index_entry);
2510 GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2512 index = (gst_riff_index_entry *) data;
2513 pos_before = avi->offset;
2515 /* figure out if the index is 0 based or relative to the MOVI start */
2516 entry.offset = GST_READ_UINT32_LE (&index[0].offset);
2517 if (entry.offset < avi->offset) {
2518 avi->index_offset = avi->offset + 8;
2519 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2521 avi->index_offset = 0;
2522 GST_DEBUG ("index is 0 based");
2525 for (i = 0, n = 0; i < num; i++) {
2526 id = GST_READ_UINT32_LE (&index[i].id);
2527 entry.offset = GST_READ_UINT32_LE (&index[i].offset);
2529 /* some sanity checks */
2530 if (G_UNLIKELY (id == GST_RIFF_rec || id == 0 ||
2531 (entry.offset == 0 && n > 0)))
2534 /* get the stream for this entry */
2535 stream = gst_avi_demux_stream_for_id (avi, id);
2536 if (G_UNLIKELY (!stream))
2539 /* handle offset and size */
2540 entry.offset += avi->index_offset + 8;
2541 entry.size = GST_READ_UINT32_LE (&index[i].size);
2544 if (stream->strh->type == GST_RIFF_FCC_auds) {
2545 /* all audio frames are keyframes */
2546 ENTRY_SET_KEYFRAME (&entry);
2549 /* else read flags */
2550 flags = GST_READ_UINT32_LE (&index[i].flags);
2551 if (flags & GST_RIFF_IF_KEYFRAME) {
2552 ENTRY_SET_KEYFRAME (&entry);
2554 ENTRY_UNSET_KEYFRAME (&entry);
2559 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2564 gst_buffer_unref (buf);
2566 /* get stream stats now */
2567 avi->have_index = gst_avi_demux_do_index_stats (avi);
2569 stamp = gst_util_get_timestamp () - stamp;
2570 GST_DEBUG_OBJECT (avi, "index parsing took %" GST_TIME_FORMAT,
2571 GST_TIME_ARGS (stamp));
2578 GST_DEBUG_OBJECT (avi, "empty index");
2579 gst_buffer_unref (buf);
2584 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2585 ("Cannot allocate memory for %u*%u=%u bytes",
2586 (guint) sizeof (GstAviIndexEntry), num,
2587 (guint) sizeof (GstAviIndexEntry) * num));
2588 gst_buffer_unref (buf);
2594 * gst_avi_demux_stream_index:
2595 * @avi: avi demuxer object.
2597 * Seeks to index and reads it.
2600 gst_avi_demux_stream_index (GstAviDemux * avi)
2603 guint64 offset = avi->offset;
2608 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2610 /* get chunk information */
2611 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2612 if (res != GST_FLOW_OK)
2614 else if (GST_BUFFER_SIZE (buf) < 8)
2617 /* check tag first before blindy trying to read 'size' bytes */
2618 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2619 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2620 if (tag == GST_RIFF_TAG_LIST) {
2621 /* this is the movi tag */
2622 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2623 (8 + GST_ROUND_UP_2 (size)));
2624 offset += 8 + GST_ROUND_UP_2 (size);
2625 gst_buffer_unref (buf);
2626 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2627 if (res != GST_FLOW_OK)
2629 else if (GST_BUFFER_SIZE (buf) < 8)
2631 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2632 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2635 if (tag != GST_RIFF_TAG_idx1)
2640 gst_buffer_unref (buf);
2642 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2644 /* read chunk, advance offset */
2645 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2646 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2649 GST_DEBUG ("will parse index chunk size %u for tag %"
2650 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2652 gst_avi_demux_parse_index (avi, buf);
2654 #ifndef GST_DISABLE_GST_DEBUG
2655 /* debug our indexes */
2658 GstAviStream *stream;
2660 for (i = 0; i < avi->num_streams; i++) {
2661 stream = &avi->stream[i];
2662 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2663 i, stream->idx_n, stream->total_bytes);
2672 GST_DEBUG_OBJECT (avi,
2673 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2678 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2679 gst_buffer_unref (buf);
2684 GST_WARNING_OBJECT (avi,
2685 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2686 GST_FOURCC_ARGS (tag));
2687 gst_buffer_unref (buf);
2692 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2693 gst_buffer_unref (buf);
2699 * gst_avi_demux_stream_index_push:
2700 * @avi: avi demuxer object.
2705 gst_avi_demux_stream_index_push (GstAviDemux * avi)
2707 guint64 offset = avi->idx1_offset;
2712 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2714 /* get chunk information */
2715 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
2718 /* check tag first before blindly trying to read 'size' bytes */
2719 if (tag == GST_RIFF_TAG_LIST) {
2720 /* this is the movi tag */
2721 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2722 (8 + GST_ROUND_UP_2 (size)));
2723 avi->idx1_offset = offset + 8 + GST_ROUND_UP_2 (size);
2724 /* issue seek to allow chain function to handle it and return! */
2725 perform_seek_to_offset (avi, avi->idx1_offset);
2729 if (tag != GST_RIFF_TAG_idx1)
2732 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2734 /* flush chunk header */
2735 gst_adapter_flush (avi->adapter, 8);
2736 /* read chunk payload */
2737 buf = gst_adapter_take_buffer (avi->adapter, size);
2740 /* advance offset */
2741 offset += 8 + GST_ROUND_UP_2 (size);
2743 GST_DEBUG ("will parse index chunk size %u for tag %"
2744 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2746 avi->offset = avi->first_movi_offset - 8;
2747 gst_avi_demux_parse_index (avi, buf);
2749 #ifndef GST_DISABLE_GST_DEBUG
2750 /* debug our indexes */
2753 GstAviStream *stream;
2755 for (i = 0; i < avi->num_streams; i++) {
2756 stream = &avi->stream[i];
2757 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2758 i, stream->idx_n, stream->total_bytes);
2767 GST_DEBUG_OBJECT (avi,
2768 "taking data from adapter failed: pos=%" G_GUINT64_FORMAT " size=%u",
2774 GST_WARNING_OBJECT (avi,
2775 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2776 GST_FOURCC_ARGS (tag));
2782 * gst_avi_demux_peek_tag:
2784 * Returns the tag and size of the next chunk
2786 static GstFlowReturn
2787 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2790 GstFlowReturn res = GST_FLOW_OK;
2791 GstBuffer *buf = NULL;
2795 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2796 if (res != GST_FLOW_OK)
2799 bufsize = GST_BUFFER_SIZE (buf);
2803 bufdata = GST_BUFFER_DATA (buf);
2805 *tag = GST_READ_UINT32_LE (bufdata);
2806 *size = GST_READ_UINT32_LE (bufdata + 4);
2808 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2809 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2810 *size, offset + 8, offset + 8 + (gint64) * size);
2813 gst_buffer_unref (buf);
2820 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2825 GST_DEBUG_OBJECT (avi, "got %d bytes which is <> 8 bytes", bufsize);
2826 res = GST_FLOW_ERROR;
2832 * gst_avi_demux_next_data_buffer:
2834 * Returns the offset and size of the next buffer
2835 * Position is the position of the buffer (after tag and size)
2837 static GstFlowReturn
2838 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2839 guint32 * tag, guint * size)
2841 guint64 off = *offset;
2846 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2847 if (res != GST_FLOW_OK)
2849 if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
2850 off += 8 + 4; /* skip tag + size + subtag */
2862 * gst_avi_demux_stream_scan:
2863 * @avi: calling element (used for debugging/errors).
2865 * Scan the file for all chunks to "create" a new index.
2869 gst_avi_demux_stream_scan (GstAviDemux * avi)
2872 GstAviStream *stream;
2881 * - implement non-seekable source support.
2883 GST_DEBUG_OBJECT (avi, "Creating index");
2885 /* get the size of the file */
2886 format = GST_FORMAT_BYTES;
2887 if (!gst_pad_query_peer_duration (avi->sinkpad, &format, &tmplength))
2891 /* guess the total amount of entries we expect */
2895 GstAviIndexEntry entry;
2898 /* start reading data buffers to find the id and offset */
2899 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
2900 if (G_UNLIKELY (res != GST_FLOW_OK))
2904 stream = gst_avi_demux_stream_for_id (avi, tag);
2905 if (G_UNLIKELY (!stream))
2908 /* we can't figure out the keyframes, assume they all are */
2909 entry.flags = GST_AVI_KEYFRAME;
2913 /* and add to the index of this stream */
2914 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2918 /* update position */
2919 pos += GST_ROUND_UP_2 (size);
2920 if (G_UNLIKELY (pos > length)) {
2921 GST_WARNING_OBJECT (avi,
2922 "Stopping index lookup since we are further than EOF");
2928 avi->have_index = gst_avi_demux_do_index_stats (avi);
2935 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2936 ("Cannot allocate memory for %u*%u=%u bytes",
2937 (guint) sizeof (GstAviIndexEntry), num,
2938 (guint) sizeof (GstAviIndexEntry) * num));
2944 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
2948 GstAviStream *stream;
2950 total = GST_CLOCK_TIME_NONE;
2952 /* all streams start at a timestamp 0 */
2953 for (i = 0; i < avi->num_streams; i++) {
2954 GstClockTime duration, hduration;
2955 gst_riff_strh *strh;
2957 stream = &avi->stream[i];
2958 if (G_UNLIKELY (!stream || !stream->idx_n || !(strh = stream->strh)))
2961 /* get header duration for the stream */
2962 hduration = stream->hdr_duration;
2963 /* index duration calculated during parsing */
2964 duration = stream->idx_duration;
2966 /* now pick a good duration */
2967 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2968 /* index gave valid duration, use that */
2969 GST_INFO ("Stream %p duration according to index: %" GST_TIME_FORMAT,
2970 stream, GST_TIME_ARGS (duration));
2972 /* fall back to header info to calculate a duration */
2973 duration = hduration;
2975 GST_INFO ("Setting duration of stream #%d to %" GST_TIME_FORMAT,
2976 i, GST_TIME_ARGS (duration));
2977 /* set duration for the stream */
2978 stream->duration = duration;
2980 /* find total duration */
2981 if (total == GST_CLOCK_TIME_NONE ||
2982 (GST_CLOCK_TIME_IS_VALID (duration) && duration > total))
2986 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
2987 /* now update the duration for those streams where we had none */
2988 for (i = 0; i < avi->num_streams; i++) {
2989 stream = &avi->stream[i];
2991 if (!GST_CLOCK_TIME_IS_VALID (stream->duration)
2992 || stream->duration == 0) {
2993 stream->duration = total;
2995 GST_INFO ("Stream %p duration according to total: %" GST_TIME_FORMAT,
2996 stream, GST_TIME_ARGS (total));
3001 /* and set the total duration in the segment. */
3002 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
3003 GST_TIME_ARGS (total));
3005 gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
3008 /* returns FALSE if there are no pads to deliver event to,
3009 * otherwise TRUE (whatever the outcome of event sending),
3010 * takes ownership of the event. */
3012 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
3014 gboolean result = FALSE;
3017 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
3018 GST_EVENT_TYPE_NAME (event), avi->num_streams);
3020 for (i = 0; i < avi->num_streams; i++) {
3021 GstAviStream *stream = &avi->stream[i];
3025 gst_pad_push_event (stream->pad, gst_event_ref (event));
3028 gst_event_unref (event);
3033 gst_avi_demux_check_seekability (GstAviDemux * avi)
3036 gboolean seekable = FALSE;
3037 gint64 start = -1, stop = -1;
3039 query = gst_query_new_seeking (GST_FORMAT_BYTES);
3040 if (!gst_pad_peer_query (avi->sinkpad, query)) {
3041 GST_DEBUG_OBJECT (avi, "seeking query failed");
3045 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
3047 /* try harder to query upstream size if we didn't get it the first time */
3048 if (seekable && stop == -1) {
3049 GstFormat fmt = GST_FORMAT_BYTES;
3051 GST_DEBUG_OBJECT (avi, "doing duration query to fix up unset stop");
3052 gst_pad_query_peer_duration (avi->sinkpad, &fmt, &stop);
3055 /* if upstream doesn't know the size, it's likely that it's not seekable in
3056 * practice even if it technically may be seekable */
3057 if (seekable && (start != 0 || stop <= start)) {
3058 GST_DEBUG_OBJECT (avi, "seekable but unknown start/stop -> disable");
3062 if (!avi->element_index) {
3063 GST_DEBUG_OBJECT (avi, "no index");
3068 GST_INFO_OBJECT (avi, "seekable: %d (%" G_GUINT64_FORMAT " - %"
3069 G_GUINT64_FORMAT ")", seekable, start, stop);
3070 avi->seekable = seekable;
3072 gst_query_unref (query);
3076 * Read AVI headers when streaming
3078 static GstFlowReturn
3079 gst_avi_demux_stream_header_push (GstAviDemux * avi)
3081 GstFlowReturn ret = GST_FLOW_OK;
3086 GstBuffer *buf = NULL, *sub = NULL;
3090 GstTagList *tags = NULL;
3092 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
3094 switch (avi->header_state) {
3095 case GST_AVI_DEMUX_HEADER_TAG_LIST:
3096 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3097 avi->offset += 8 + GST_ROUND_UP_2 (size);
3098 if (tag != GST_RIFF_TAG_LIST)
3099 goto header_no_list;
3101 gst_adapter_flush (avi->adapter, 8);
3102 /* Find the 'hdrl' LIST tag */
3103 GST_DEBUG ("Reading %d bytes", size);
3104 buf = gst_adapter_take_buffer (avi->adapter, size);
3106 if (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl)
3107 goto header_no_hdrl;
3111 gst_adapter_flush (avi->adapter, 1);
3113 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
3115 /* the hdrl starts with a 'avih' header */
3116 if (!gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3118 goto header_no_avih;
3120 if (tag != GST_RIFF_TAG_avih)
3121 goto header_no_avih;
3123 if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3124 goto header_wrong_avih;
3126 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
3128 /* now, read the elements from the header until the end */
3129 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3131 /* sub can be NULL on empty tags */
3136 case GST_RIFF_TAG_LIST:
3137 if (GST_BUFFER_SIZE (sub) < 4)
3140 switch (GST_READ_UINT32_LE (GST_BUFFER_DATA (sub))) {
3141 case GST_RIFF_LIST_strl:
3142 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3144 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3145 ("failed to parse stream, ignoring"));
3150 case GST_RIFF_LIST_odml:
3151 gst_avi_demux_parse_odml (avi, sub);
3155 GST_WARNING_OBJECT (avi,
3156 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3157 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA
3160 case GST_RIFF_TAG_JUNQ:
3161 case GST_RIFF_TAG_JUNK:
3166 gst_avi_demux_parse_idit (avi, sub);
3169 GST_WARNING_OBJECT (avi,
3170 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
3171 offset, GST_FOURCC_ARGS (tag));
3173 case GST_RIFF_TAG_JUNQ:
3174 case GST_RIFF_TAG_JUNK:
3176 /* move to next chunk */
3178 gst_buffer_unref (sub);
3183 gst_buffer_unref (buf);
3184 GST_DEBUG ("elements parsed");
3186 /* check parsed streams */
3187 if (avi->num_streams == 0) {
3189 } else if (avi->num_streams != avi->avih->streams) {
3190 GST_WARNING_OBJECT (avi,
3191 "Stream header mentioned %d streams, but %d available",
3192 avi->avih->streams, avi->num_streams);
3194 GST_DEBUG ("Get junk and info next");
3195 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
3197 /* Need more data */
3201 case GST_AVI_DEMUX_HEADER_INFO:
3202 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
3204 if (gst_adapter_available (avi->adapter) < 12)
3207 data = gst_adapter_peek (avi->adapter, 12);
3208 tag = GST_READ_UINT32_LE (data);
3209 size = GST_READ_UINT32_LE (data + 4);
3210 ltag = GST_READ_UINT32_LE (data + 8);
3212 if (tag == GST_RIFF_TAG_LIST) {
3214 case GST_RIFF_LIST_movi:
3215 gst_adapter_flush (avi->adapter, 12);
3216 if (!avi->first_movi_offset)
3217 avi->first_movi_offset = avi->offset;
3219 avi->idx1_offset = avi->offset + size - 4;
3221 case GST_RIFF_LIST_INFO:
3222 GST_DEBUG ("Found INFO chunk");
3223 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3224 GST_DEBUG ("got size %d", size);
3226 gst_adapter_flush (avi->adapter, 12);
3228 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
3231 gst_adapter_flush (avi->adapter, 1);
3232 gst_riff_parse_info (GST_ELEMENT_CAST (avi), buf, &tags);
3234 if (avi->globaltags) {
3235 gst_tag_list_insert (avi->globaltags, tags,
3236 GST_TAG_MERGE_REPLACE);
3238 avi->globaltags = tags;
3242 gst_buffer_unref (buf);
3244 avi->offset += GST_ROUND_UP_2 (size) - 4;
3246 GST_DEBUG ("skipping INFO LIST prefix");
3249 /* Need more data */
3254 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3255 avi->offset += 8 + GST_ROUND_UP_2 (size);
3256 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3257 // ??? goto iterate; ???
3259 /* Need more data */
3265 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3266 avi->offset += 8 + GST_ROUND_UP_2 (size);
3267 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3270 /* Need more data */
3277 GST_WARNING ("unhandled header state: %d", avi->header_state);
3282 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3283 avi->num_streams, avi->stream[0].indexes);
3285 GST_DEBUG ("Found movi chunk. Starting to stream data");
3286 avi->state = GST_AVI_DEMUX_MOVI;
3288 /* no indexes in push mode, but it still sets some variables */
3289 gst_avi_demux_calculate_durations_from_index (avi);
3291 gst_avi_demux_expose_streams (avi, TRUE);
3293 /* prepare all streams for index 0 */
3294 for (i = 0; i < avi->num_streams; i++)
3295 avi->stream[i].current_entry = 0;
3297 /* create initial NEWSEGMENT event */
3298 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3299 stop = avi->segment.duration;
3301 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3304 gst_event_unref (avi->seg_event);
3305 avi->seg_event = gst_event_new_new_segment_full
3306 (FALSE, avi->segment.rate, avi->segment.applied_rate, GST_FORMAT_TIME,
3307 avi->segment.start, stop, avi->segment.time);
3309 gst_avi_demux_check_seekability (avi);
3311 /* at this point we know all the streams and we can signal the no more
3313 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3314 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3321 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3322 return GST_FLOW_ERROR;
3326 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3327 ("Invalid AVI header (no LIST at start): %"
3328 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3329 return GST_FLOW_ERROR;
3333 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3334 ("Invalid AVI header (no hdrl at start): %"
3335 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3336 gst_buffer_unref (buf);
3337 return GST_FLOW_ERROR;
3341 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3342 ("Invalid AVI header (no avih at start): %"
3343 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3345 gst_buffer_unref (sub);
3347 gst_buffer_unref (buf);
3348 return GST_FLOW_ERROR;
3352 gst_buffer_unref (buf);
3353 return GST_FLOW_ERROR;
3358 gst_avi_demux_add_date_tag (GstAviDemux * avi, gint y, gint m, gint d)
3361 date = g_date_new_dmy (d, m, y);
3362 if (!g_date_valid (date)) {
3364 GST_WARNING_OBJECT (avi, "Refusing to add invalid date %d-%d-%d", y, m, d);
3369 if (avi->globaltags == NULL)
3370 avi->globaltags = gst_tag_list_new ();
3372 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE, date,
3378 gst_avi_demux_parse_idit_nums_only (GstAviDemux * avi, gchar * data)
3383 ret = sscanf (data, "%d:%d:%d", &y, &m, &d);
3385 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3388 gst_avi_demux_add_date_tag (avi, y, m, d);
3392 get_month_num (gchar * data, guint size)
3394 if (g_ascii_strncasecmp (data, "jan", 3) == 0) {
3396 } else if (g_ascii_strncasecmp (data, "feb", 3) == 0) {
3398 } else if (g_ascii_strncasecmp (data, "mar", 3) == 0) {
3400 } else if (g_ascii_strncasecmp (data, "apr", 3) == 0) {
3402 } else if (g_ascii_strncasecmp (data, "may", 3) == 0) {
3404 } else if (g_ascii_strncasecmp (data, "jun", 3) == 0) {
3406 } else if (g_ascii_strncasecmp (data, "jul", 3) == 0) {
3408 } else if (g_ascii_strncasecmp (data, "aug", 3) == 0) {
3410 } else if (g_ascii_strncasecmp (data, "sep", 3) == 0) {
3412 } else if (g_ascii_strncasecmp (data, "oct", 3) == 0) {
3414 } else if (g_ascii_strncasecmp (data, "nov", 3) == 0) {
3416 } else if (g_ascii_strncasecmp (data, "dec", 3) == 0) {
3424 gst_avi_demux_parse_idit_text (GstAviDemux * avi, gchar * data)
3426 gint year, month, day;
3427 gint hour, min, sec;
3432 ret = sscanf (data, "%3s %3s %d %d:%d:%d %d", weekday, monthstr, &day, &hour,
3435 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3438 month = get_month_num (monthstr, strlen (monthstr));
3439 gst_avi_demux_add_date_tag (avi, year, month, day);
3443 gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf)
3445 gchar *data = (gchar *) GST_BUFFER_DATA (buf);
3446 guint size = GST_BUFFER_SIZE (buf);
3447 gchar *safedata = NULL;
3451 * http://www.eden-foundation.org/products/code/film_date_stamp/index.html
3453 * This tag could be in one of the below formats
3454 * 2005:08:17 11:42:43
3455 * THU OCT 26 16:46:04 2006
3456 * Mon Mar 3 09:44:56 2008
3458 * FIXME: Our date tag doesn't include hours
3461 /* skip eventual initial whitespace */
3462 while (size > 0 && g_ascii_isspace (data[0])) {
3471 /* make a safe copy to add a \0 to the end of the string */
3472 safedata = g_strndup (data, size);
3474 /* test if the first char is a alpha or a number */
3475 if (g_ascii_isdigit (data[0])) {
3476 gst_avi_demux_parse_idit_nums_only (avi, safedata);
3479 } else if (g_ascii_isalpha (data[0])) {
3480 gst_avi_demux_parse_idit_text (avi, safedata);
3488 GST_WARNING_OBJECT (avi, "IDIT tag has no parsable info");
3492 * Read full AVI headers.
3494 static GstFlowReturn
3495 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
3498 GstBuffer *buf, *sub = NULL;
3502 GstElement *element = GST_ELEMENT_CAST (avi);
3504 GstTagList *tags = NULL;
3506 stamp = gst_util_get_timestamp ();
3508 /* the header consists of a 'hdrl' LIST tag */
3509 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3510 if (res != GST_FLOW_OK)
3511 goto pull_range_failed;
3512 else if (tag != GST_RIFF_TAG_LIST)
3514 else if (GST_BUFFER_SIZE (buf) < 4)
3517 GST_DEBUG_OBJECT (avi, "parsing headers");
3519 /* Find the 'hdrl' LIST tag */
3520 while (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl) {
3521 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
3522 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf))));
3525 gst_buffer_unref (buf);
3527 /* read new chunk */
3528 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3529 if (res != GST_FLOW_OK)
3530 goto pull_range_failed;
3531 else if (tag != GST_RIFF_TAG_LIST)
3533 else if (GST_BUFFER_SIZE (buf) < 4)
3537 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
3539 /* the hdrl starts with a 'avih' header */
3540 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
3542 else if (tag != GST_RIFF_TAG_avih)
3544 else if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3547 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3549 /* now, read the elements from the header until the end */
3550 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
3551 /* sub can be NULL on empty tags */
3556 case GST_RIFF_TAG_LIST:
3561 if (GST_BUFFER_SIZE (sub) < 4)
3564 data = GST_BUFFER_DATA (sub);
3565 fourcc = GST_READ_UINT32_LE (data);
3568 case GST_RIFF_LIST_strl:
3569 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3570 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3571 ("failed to parse stream, ignoring"));
3576 case GST_RIFF_LIST_odml:
3577 gst_avi_demux_parse_odml (avi, sub);
3580 case GST_RIFF_LIST_INFO:
3581 GST_BUFFER_DATA (sub) = data + 4;
3582 GST_BUFFER_SIZE (sub) -= 4;
3583 gst_riff_parse_info (element, sub, &tags);
3585 if (avi->globaltags) {
3586 gst_tag_list_insert (avi->globaltags, tags,
3587 GST_TAG_MERGE_REPLACE);
3589 avi->globaltags = tags;
3595 GST_WARNING_OBJECT (avi,
3596 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3597 GST_FOURCC_ARGS (fourcc));
3598 GST_MEMDUMP_OBJECT (avi, "Unknown list", GST_BUFFER_DATA (sub),
3599 GST_BUFFER_SIZE (sub));
3601 case GST_RIFF_TAG_JUNQ:
3602 case GST_RIFF_TAG_JUNK:
3608 gst_avi_demux_parse_idit (avi, sub);
3611 GST_WARNING_OBJECT (avi,
3612 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
3613 GST_FOURCC_ARGS (tag), offset);
3614 GST_MEMDUMP_OBJECT (avi, "Unknown tag", GST_BUFFER_DATA (sub),
3615 GST_BUFFER_SIZE (sub));
3617 case GST_RIFF_TAG_JUNQ:
3618 case GST_RIFF_TAG_JUNK:
3621 gst_buffer_unref (sub);
3626 gst_buffer_unref (buf);
3627 GST_DEBUG ("elements parsed");
3629 /* check parsed streams */
3630 if (avi->num_streams == 0)
3632 else if (avi->num_streams != avi->avih->streams) {
3633 GST_WARNING_OBJECT (avi,
3634 "Stream header mentioned %d streams, but %d available",
3635 avi->avih->streams, avi->num_streams);
3638 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
3639 G_GUINT64_FORMAT, avi->offset);
3641 /* Now, find the data (i.e. skip all junk between header and data) */
3647 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
3648 if (res != GST_FLOW_OK) {
3649 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
3650 goto pull_range_failed;
3651 } else if (GST_BUFFER_SIZE (buf) < 12) {
3652 GST_DEBUG_OBJECT (avi, "got %d bytes which is less than 12 bytes",
3653 GST_BUFFER_SIZE (buf));
3654 gst_buffer_unref (buf);
3655 return GST_FLOW_ERROR;
3658 data = GST_BUFFER_DATA (buf);
3660 tag = GST_READ_UINT32_LE (data);
3661 size = GST_READ_UINT32_LE (data + 4);
3662 ltag = GST_READ_UINT32_LE (data + 8);
3664 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
3665 GST_FOURCC_ARGS (tag), size);
3666 GST_MEMDUMP ("Tag content", data, GST_BUFFER_SIZE (buf));
3667 gst_buffer_unref (buf);
3670 case GST_RIFF_TAG_LIST:{
3672 case GST_RIFF_LIST_movi:
3673 GST_DEBUG_OBJECT (avi,
3674 "Reached the 'movi' tag, we're done with skipping");
3676 case GST_RIFF_LIST_INFO:
3678 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3680 if (res != GST_FLOW_OK) {
3681 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3682 goto pull_range_failed;
3684 GST_DEBUG ("got size %u", GST_BUFFER_SIZE (buf));
3686 GST_DEBUG ("skipping INFO LIST prefix");
3687 avi->offset += (4 - GST_ROUND_UP_2 (size));
3688 gst_buffer_unref (buf);
3692 sub = gst_buffer_create_sub (buf, 4, GST_BUFFER_SIZE (buf) - 4);
3693 gst_riff_parse_info (element, sub, &tags);
3695 if (avi->globaltags) {
3696 gst_tag_list_insert (avi->globaltags, tags,
3697 GST_TAG_MERGE_REPLACE);
3699 avi->globaltags = tags;
3704 gst_buffer_unref (sub);
3707 gst_buffer_unref (buf);
3708 /* gst_riff_read_chunk() has already advanced avi->offset */
3711 GST_WARNING_OBJECT (avi,
3712 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3713 GST_FOURCC_ARGS (ltag));
3714 avi->offset += 8 + GST_ROUND_UP_2 (size);
3720 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3721 GST_FOURCC_ARGS (tag));
3723 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3724 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3725 /* Only get buffer for debugging if the memdump is needed */
3726 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= 9) {
3727 res = gst_pad_pull_range (avi->sinkpad, avi->offset, size, &buf);
3728 if (res != GST_FLOW_OK) {
3729 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3730 goto pull_range_failed;
3732 GST_MEMDUMP ("Junk", GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
3733 gst_buffer_unref (buf);
3735 avi->offset += 8 + GST_ROUND_UP_2 (size);
3741 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3742 avi->num_streams, avi->stream[0].indexes);
3744 /* create or read stream index (for seeking) */
3745 if (avi->stream[0].indexes != NULL) {
3746 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3747 gst_avi_demux_read_subindexes_pull (avi);
3749 if (!avi->have_index) {
3750 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
3751 gst_avi_demux_stream_index (avi);
3753 /* still no index, scan */
3754 if (!avi->have_index) {
3755 gst_avi_demux_stream_scan (avi);
3757 /* still no index.. this is a fatal error for now.
3758 * FIXME, we should switch to plain push mode without seeking
3759 * instead of failing. */
3760 if (!avi->have_index)
3764 /* use the indexes now to construct nice durations */
3765 gst_avi_demux_calculate_durations_from_index (avi);
3767 gst_avi_demux_expose_streams (avi, FALSE);
3769 /* create initial NEWSEGMENT event */
3770 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3771 stop = avi->segment.duration;
3773 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3775 /* do initial seek to the default segment values */
3776 gst_avi_demux_do_seek (avi, &avi->segment);
3778 /* prepare initial segment */
3780 gst_event_unref (avi->seg_event);
3781 avi->seg_event = gst_event_new_new_segment_full
3782 (FALSE, avi->segment.rate, avi->segment.applied_rate, GST_FORMAT_TIME,
3783 avi->segment.start, stop, avi->segment.time);
3785 stamp = gst_util_get_timestamp () - stamp;
3786 GST_DEBUG_OBJECT (avi, "pulling header took %" GST_TIME_FORMAT,
3787 GST_TIME_ARGS (stamp));
3789 /* at this point we know all the streams and we can signal the no more
3791 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3792 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3799 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3800 ("Invalid AVI header (no LIST at start): %"
3801 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3802 gst_buffer_unref (buf);
3803 return GST_FLOW_ERROR;
3807 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3808 ("Invalid AVI header (no hdrl at start): %"
3809 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3810 gst_buffer_unref (buf);
3811 return GST_FLOW_ERROR;
3815 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3816 ("Invalid AVI header (no avih at start): %"
3817 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3819 gst_buffer_unref (sub);
3820 gst_buffer_unref (buf);
3821 return GST_FLOW_ERROR;
3825 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3826 ("Invalid AVI header (cannot parse avih at start)"));
3827 gst_buffer_unref (buf);
3828 return GST_FLOW_ERROR;
3832 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3833 return GST_FLOW_ERROR;
3837 GST_WARNING ("file without or too big index");
3838 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3839 ("Could not get/create index"));
3840 return GST_FLOW_ERROR;
3844 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3845 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3846 return GST_FLOW_ERROR;
3850 /* move a stream to @index */
3852 gst_avi_demux_move_stream (GstAviDemux * avi, GstAviStream * stream,
3853 GstSegment * segment, guint index)
3855 GST_DEBUG_OBJECT (avi, "Move stream %d to %u", stream->num, index);
3857 if (segment->rate < 0.0) {
3859 /* Because we don't know the frame order we need to push from the prev keyframe
3860 * to the next keyframe. If there is a smart decoder downstream he will notice
3861 * that there are too many encoded frames send and return UNEXPECTED when there
3862 * are enough decoded frames to fill the segment. */
3863 next_key = gst_avi_demux_index_next (avi, stream, index, TRUE);
3865 /* FIXME, we go back to 0, we should look at segment.start. We will however
3866 * stop earlier when the see the timestamp < segment.start */
3867 stream->start_entry = 0;
3868 stream->step_entry = index;
3869 stream->current_entry = index;
3870 stream->stop_entry = next_key;
3872 GST_DEBUG_OBJECT (avi, "reverse seek: start %u, step %u, stop %u",
3873 stream->start_entry, stream->step_entry, stream->stop_entry);
3875 stream->start_entry = index;
3876 stream->step_entry = index;
3877 stream->stop_entry = gst_avi_demux_index_last (avi, stream);
3879 if (stream->current_entry != index) {
3880 GST_DEBUG_OBJECT (avi, "Move DISCONT from %u to %u",
3881 stream->current_entry, index);
3882 stream->current_entry = index;
3883 stream->discont = TRUE;
3886 /* update the buffer info */
3887 gst_avi_demux_get_buffer_info (avi, stream, index,
3888 &stream->current_timestamp, &stream->current_ts_end,
3889 &stream->current_offset, &stream->current_offset_end);
3891 GST_DEBUG_OBJECT (avi, "Moved to %u, ts %" GST_TIME_FORMAT
3892 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
3893 ", off_end %" G_GUINT64_FORMAT, index,
3894 GST_TIME_ARGS (stream->current_timestamp),
3895 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
3896 stream->current_offset_end);
3898 GST_DEBUG_OBJECT (avi, "Seeking to offset %" G_GUINT64_FORMAT,
3899 stream->index[index].offset);
3903 * Do the actual seeking.
3906 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
3908 GstClockTime seek_time;
3911 GstAviStream *stream;
3913 seek_time = segment->last_stop;
3914 keyframe = !!(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
3916 GST_DEBUG_OBJECT (avi, "seek to: %" GST_TIME_FORMAT
3917 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
3919 /* FIXME, this code assumes the main stream with keyframes is stream 0,
3920 * which is mostly correct... */
3921 stream = &avi->stream[avi->main_stream];
3923 /* get the entry index for the requested position */
3924 index = gst_avi_demux_index_for_time (avi, stream, seek_time);
3925 GST_DEBUG_OBJECT (avi, "Got entry %u", index);
3927 /* check if we are already on a keyframe */
3928 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
3929 GST_DEBUG_OBJECT (avi, "not keyframe, searching back");
3930 /* now go to the previous keyframe, this is where we should start
3932 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
3933 GST_DEBUG_OBJECT (avi, "previous keyframe at %u", index);
3936 /* move the main stream to this position */
3937 gst_avi_demux_move_stream (avi, stream, segment, index);
3940 /* when seeking to a keyframe, we update the result seek time
3941 * to the time of the keyframe. */
3942 seek_time = stream->current_timestamp;
3943 GST_DEBUG_OBJECT (avi, "keyframe adjusted to %" GST_TIME_FORMAT,
3944 GST_TIME_ARGS (seek_time));
3947 /* the seek time is also the last_stop and stream time when going
3949 segment->last_stop = seek_time;
3950 if (segment->rate > 0.0)
3951 segment->time = seek_time;
3953 /* now set DISCONT and align the other streams */
3954 for (i = 0; i < avi->num_streams; i++) {
3955 GstAviStream *ostream;
3957 ostream = &avi->stream[i];
3958 if ((ostream == stream) || (ostream->index == NULL))
3961 /* get the entry index for the requested position */
3962 index = gst_avi_demux_index_for_time (avi, ostream, seek_time);
3964 /* move to previous keyframe */
3965 if (!ENTRY_IS_KEYFRAME (&ostream->index[index]))
3966 index = gst_avi_demux_index_prev (avi, ostream, index, TRUE);
3968 gst_avi_demux_move_stream (avi, ostream, segment, index);
3970 GST_DEBUG_OBJECT (avi, "done seek to: %" GST_TIME_FORMAT,
3971 GST_TIME_ARGS (seek_time));
3977 * Handle seek event in pull mode.
3980 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
3985 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
3989 GstSegment seeksegment = { 0, };
3993 GST_DEBUG_OBJECT (avi, "doing seek with event");
3995 gst_event_parse_seek (event, &rate, &format, &flags,
3996 &cur_type, &cur, &stop_type, &stop);
3998 /* we have to have a format as the segment format. Try to convert
4000 if (format != GST_FORMAT_TIME) {
4001 GstFormat fmt = GST_FORMAT_TIME;
4002 gboolean res = TRUE;
4004 if (cur_type != GST_SEEK_TYPE_NONE)
4005 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
4006 if (res && stop_type != GST_SEEK_TYPE_NONE)
4007 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
4013 GST_DEBUG_OBJECT (avi,
4014 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
4015 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
4016 /* FIXME: can we do anything with rate!=1.0 */
4018 GST_DEBUG_OBJECT (avi, "doing seek without event");
4023 /* save flush flag */
4024 flush = flags & GST_SEEK_FLAG_FLUSH;
4027 GstEvent *fevent = gst_event_new_flush_start ();
4029 /* for a flushing seek, we send a flush_start on all pads. This will
4030 * eventually stop streaming with a WRONG_STATE. We can thus eventually
4031 * take the STREAM_LOCK. */
4032 GST_DEBUG_OBJECT (avi, "sending flush start");
4033 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4034 gst_pad_push_event (avi->sinkpad, fevent);
4036 /* a non-flushing seek, we PAUSE the task so that we can take the
4038 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
4039 gst_pad_pause_task (avi->sinkpad);
4042 /* wait for streaming to stop */
4043 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
4044 GST_PAD_STREAM_LOCK (avi->sinkpad);
4046 /* copy segment, we need this because we still need the old
4047 * segment when we close the current segment. */
4048 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4051 GST_DEBUG_OBJECT (avi, "configuring seek");
4052 gst_segment_set_seek (&seeksegment, rate, format, flags,
4053 cur_type, cur, stop_type, stop, &update);
4055 /* do the seek, seeksegment.last_stop contains the new position, this
4056 * actually never fails. */
4057 gst_avi_demux_do_seek (avi, &seeksegment);
4060 GstEvent *fevent = gst_event_new_flush_stop ();
4062 GST_DEBUG_OBJECT (avi, "sending flush stop");
4063 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4064 gst_pad_push_event (avi->sinkpad, fevent);
4065 } else if (avi->segment_running) {
4068 /* we are running the current segment and doing a non-flushing seek,
4069 * close the segment first based on the last_stop. */
4070 GST_DEBUG_OBJECT (avi, "closing running segment %" G_GINT64_FORMAT
4071 " to %" G_GINT64_FORMAT, avi->segment.start, avi->segment.last_stop);
4072 seg = gst_event_new_new_segment_full (TRUE,
4073 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
4074 avi->segment.start, avi->segment.last_stop, avi->segment.time);
4075 gst_avi_demux_push_event (avi, seg);
4078 /* now update the real segment info */
4079 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
4081 /* post the SEGMENT_START message when we do segmented playback */
4082 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4083 gst_element_post_message (GST_ELEMENT_CAST (avi),
4084 gst_message_new_segment_start (GST_OBJECT_CAST (avi),
4085 avi->segment.format, avi->segment.last_stop));
4088 /* prepare for streaming again */
4089 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
4090 stop = avi->segment.duration;
4092 /* queue the segment event for the streaming thread. */
4094 gst_event_unref (avi->seg_event);
4095 if (avi->segment.rate > 0.0) {
4096 /* forwards goes from last_stop to stop */
4097 avi->seg_event = gst_event_new_new_segment_full (FALSE,
4098 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
4099 avi->segment.last_stop, stop, avi->segment.time);
4101 /* reverse goes from start to last_stop */
4102 avi->seg_event = gst_event_new_new_segment_full (FALSE,
4103 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
4104 avi->segment.start, avi->segment.last_stop, avi->segment.time);
4107 if (!avi->streaming) {
4108 avi->segment_running = TRUE;
4109 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4112 /* reset the last flow and mark discont, seek is always DISCONT */
4113 for (i = 0; i < avi->num_streams; i++) {
4114 GST_DEBUG_OBJECT (avi, "marking DISCONT");
4115 avi->stream[i].last_flow = GST_FLOW_OK;
4116 avi->stream[i].discont = TRUE;
4118 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
4125 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4131 * Handle seek event in push mode.
4134 avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4139 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4142 GstAviStream *stream;
4146 GstSegment seeksegment;
4149 /* check we have the index */
4150 if (!avi->have_index) {
4151 GST_DEBUG_OBJECT (avi, "no seek index built, seek aborted.");
4154 GST_DEBUG_OBJECT (avi, "doing push-based seek with event");
4157 gst_event_parse_seek (event, &rate, &format, &flags,
4158 &cur_type, &cur, &stop_type, &stop);
4160 if (format != GST_FORMAT_TIME) {
4161 GstFormat fmt = GST_FORMAT_TIME;
4162 gboolean res = TRUE;
4164 if (cur_type != GST_SEEK_TYPE_NONE)
4165 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
4166 if (res && stop_type != GST_SEEK_TYPE_NONE)
4167 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
4169 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4176 /* let gst_segment handle any tricky stuff */
4177 GST_DEBUG_OBJECT (avi, "configuring seek");
4178 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4179 gst_segment_set_seek (&seeksegment, rate, format, flags,
4180 cur_type, cur, stop_type, stop, &update);
4182 keyframe = !!(flags & GST_SEEK_FLAG_KEY_UNIT);
4183 cur = seeksegment.last_stop;
4185 GST_DEBUG_OBJECT (avi,
4186 "Seek requested: ts %" GST_TIME_FORMAT " stop %" GST_TIME_FORMAT
4187 ", kf %u, rate %lf", GST_TIME_ARGS (cur), GST_TIME_ARGS (stop), keyframe,
4191 GST_DEBUG_OBJECT (avi, "negative rate seek not supported in push mode");
4195 /* FIXME, this code assumes the main stream with keyframes is stream 0,
4196 * which is mostly correct... */
4197 str_num = avi->main_stream;
4198 stream = &avi->stream[str_num];
4200 /* get the entry index for the requested position */
4201 index = gst_avi_demux_index_for_time (avi, stream, cur);
4202 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT,
4203 str_num, index, GST_TIME_ARGS (cur));
4205 /* check if we are already on a keyframe */
4206 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
4207 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4208 /* now go to the previous keyframe, this is where we should start
4210 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4211 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", index);
4214 gst_avi_demux_get_buffer_info (avi, stream, index,
4215 &stream->current_timestamp, &stream->current_ts_end,
4216 &stream->current_offset, &stream->current_offset_end);
4218 /* re-use cur to be the timestamp of the seek as it _will_ be */
4219 cur = stream->current_timestamp;
4221 min_offset = stream->index[index].offset;
4222 avi->seek_kf_offset = min_offset - 8;
4224 GST_DEBUG_OBJECT (avi,
4225 "Seek to: ts %" GST_TIME_FORMAT " (on str %u, idx %u, offset %"
4226 G_GUINT64_FORMAT ")", GST_TIME_ARGS (stream->current_timestamp), str_num,
4229 for (n = 0; n < avi->num_streams; n++) {
4230 GstAviStream *str = &avi->stream[n];
4233 if (n == avi->main_stream)
4236 /* get the entry index for the requested position */
4237 idx = gst_avi_demux_index_for_time (avi, str, cur);
4238 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT, n,
4239 idx, GST_TIME_ARGS (cur));
4241 /* check if we are already on a keyframe */
4242 if (!ENTRY_IS_KEYFRAME (&str->index[idx])) {
4243 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4244 /* now go to the previous keyframe, this is where we should start
4246 idx = gst_avi_demux_index_prev (avi, str, idx, TRUE);
4247 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", idx);
4250 gst_avi_demux_get_buffer_info (avi, str, idx,
4251 &str->current_timestamp, &str->current_ts_end,
4252 &str->current_offset, &str->current_offset_end);
4254 if (str->index[idx].offset < min_offset) {
4255 min_offset = str->index[idx].offset;
4256 GST_DEBUG_OBJECT (avi,
4257 "Found an earlier offset at %" G_GUINT64_FORMAT ", str %u",
4265 GST_DEBUG_OBJECT (avi,
4266 "Seek performed: str %u, offset %" G_GUINT64_FORMAT ", idx %u, ts %"
4267 GST_TIME_FORMAT ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4268 ", off_end %" G_GUINT64_FORMAT, str_num, min_offset, index,
4269 GST_TIME_ARGS (stream->current_timestamp),
4270 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4271 stream->current_offset_end);
4273 /* index data refers to data, not chunk header (for pull mode convenience) */
4275 GST_DEBUG_OBJECT (avi, "seeking to chunk at offset %" G_GUINT64_FORMAT,
4278 if (!perform_seek_to_offset (avi, min_offset)) {
4279 GST_DEBUG_OBJECT (avi, "seek event failed!");
4287 * Handle whether we can perform the seek event or if we have to let the chain
4288 * function handle seeks to build the seek indexes first.
4291 gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
4294 /* check for having parsed index already */
4295 if (!avi->have_index) {
4297 gboolean building_index;
4299 GST_OBJECT_LOCK (avi);
4300 /* handle the seek event in the chain function */
4301 avi->state = GST_AVI_DEMUX_SEEK;
4303 /* copy the event */
4304 if (avi->seek_event)
4305 gst_event_unref (avi->seek_event);
4306 avi->seek_event = gst_event_ref (event);
4308 /* set the building_index flag so that only one thread can setup the
4309 * structures for index seeking. */
4310 building_index = avi->building_index;
4311 if (!building_index) {
4312 avi->building_index = TRUE;
4313 if (avi->stream[0].indexes) {
4314 avi->odml_stream = 0;
4315 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
4316 offset = avi->odml_subidxs[0];
4318 offset = avi->idx1_offset;
4321 GST_OBJECT_UNLOCK (avi);
4323 if (!building_index) {
4324 /* seek to the first subindex or legacy index */
4325 GST_INFO_OBJECT (avi,
4326 "Seeking to legacy index/first subindex at %" G_GUINT64_FORMAT,
4328 return perform_seek_to_offset (avi, offset);
4331 /* FIXME: we have to always return true so that we don't block the seek
4333 * Note: maybe it is OK to return true if we're still building the index */
4337 return avi_demux_handle_seek_push (avi, pad, event);
4341 * Helper for gst_avi_demux_invert()
4344 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
4346 memcpy (tmp, d1, bytes);
4347 memcpy (d1, d2, bytes);
4348 memcpy (d2, tmp, bytes);
4352 #define gst_avi_demux_is_uncompressed(fourcc) \
4353 (fourcc == GST_RIFF_DIB || \
4354 fourcc == GST_RIFF_rgb || \
4355 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW)
4358 * Invert DIB buffers... Takes existing buffer and
4359 * returns either the buffer or a new one (with old
4360 * one dereferenced).
4361 * FIXME: can't we preallocate tmp? and remember stride, bpp?
4364 gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
4371 if (stream->strh->type != GST_RIFF_FCC_vids)
4374 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
4375 return buf; /* Ignore non DIB buffers */
4378 s = gst_caps_get_structure (GST_PAD_CAPS (stream->pad), 0);
4379 if (!gst_structure_get_int (s, "bpp", &bpp)) {
4380 GST_WARNING ("Failed to retrieve depth from caps");
4384 if (stream->strf.vids == NULL) {
4385 GST_WARNING ("Failed to retrieve vids for stream");
4389 h = stream->strf.vids->height;
4390 w = stream->strf.vids->width;
4391 stride = w * (bpp / 8);
4393 buf = gst_buffer_make_writable (buf);
4394 if (GST_BUFFER_SIZE (buf) < (stride * h)) {
4395 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
4399 tmp = g_malloc (stride);
4401 for (y = 0; y < h / 2; y++) {
4402 swap_line (GST_BUFFER_DATA (buf) + stride * y,
4403 GST_BUFFER_DATA (buf) + stride * (h - 1 - y), tmp, stride);
4412 gst_avi_demux_add_assoc (GstAviDemux * avi, GstAviStream * stream,
4413 GstClockTime timestamp, guint64 offset, gboolean keyframe)
4415 /* do not add indefinitely for open-ended streaming */
4416 if (G_UNLIKELY (avi->element_index && avi->seekable)) {
4417 GST_LOG_OBJECT (avi, "adding association %" GST_TIME_FORMAT "-> %"
4418 G_GUINT64_FORMAT, GST_TIME_ARGS (timestamp), offset);
4419 gst_index_add_association (avi->element_index, avi->index_id,
4420 keyframe ? GST_ASSOCIATION_FLAG_KEY_UNIT : GST_ASSOCIATION_FLAG_NONE,
4421 GST_FORMAT_TIME, timestamp, GST_FORMAT_BYTES, offset, NULL);
4422 /* well, current_total determines TIME and entry DEFAULT (frame #) ... */
4423 gst_index_add_association (avi->element_index, stream->index_id,
4424 GST_ASSOCIATION_FLAG_NONE,
4425 GST_FORMAT_TIME, stream->current_total, GST_FORMAT_BYTES, offset,
4426 GST_FORMAT_DEFAULT, stream->current_entry, NULL);
4431 * Returns the aggregated GstFlowReturn.
4433 static GstFlowReturn
4434 gst_avi_demux_combine_flows (GstAviDemux * avi, GstAviStream * stream,
4438 gboolean unexpected = FALSE, not_linked = TRUE;
4440 /* store the value */
4441 stream->last_flow = ret;
4443 /* any other error that is not-linked or eos can be returned right away */
4444 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
4447 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4448 for (i = 0; i < avi->num_streams; i++) {
4449 GstAviStream *ostream = &avi->stream[i];
4451 ret = ostream->last_flow;
4452 /* no unexpected or unlinked, return */
4453 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
4456 /* we check to see if we have at least 1 unexpected or all unlinked */
4457 unexpected |= (ret == GST_FLOW_UNEXPECTED);
4458 not_linked &= (ret == GST_FLOW_NOT_LINKED);
4460 /* when we get here, we all have unlinked or unexpected */
4462 ret = GST_FLOW_NOT_LINKED;
4463 else if (unexpected)
4464 ret = GST_FLOW_UNEXPECTED;
4466 GST_LOG_OBJECT (avi, "combined %s to return %s",
4467 gst_flow_get_name (stream->last_flow), gst_flow_get_name (ret));
4471 /* move @stream to the next position in its index */
4472 static GstFlowReturn
4473 gst_avi_demux_advance (GstAviDemux * avi, GstAviStream * stream,
4476 guint old_entry, new_entry;
4478 old_entry = stream->current_entry;
4480 new_entry = old_entry + 1;
4482 /* see if we reached the end */
4483 if (new_entry > stream->stop_entry) {
4484 if (avi->segment.rate < 0.0) {
4485 if (stream->step_entry == stream->start_entry) {
4486 /* we stepped all the way to the start, eos */
4487 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
4490 /* backwards, stop becomes step, find a new step */
4491 stream->stop_entry = stream->step_entry;
4492 stream->step_entry = gst_avi_demux_index_prev (avi, stream,
4493 stream->stop_entry, TRUE);
4495 GST_DEBUG_OBJECT (avi,
4496 "reverse playback jump: start %u, step %u, stop %u",
4497 stream->start_entry, stream->step_entry, stream->stop_entry);
4499 /* and start from the previous keyframe now */
4500 new_entry = stream->step_entry;
4503 GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
4508 if (new_entry != old_entry) {
4509 stream->current_entry = new_entry;
4510 stream->current_total = stream->index[new_entry].total;
4512 if (new_entry == old_entry + 1) {
4513 GST_DEBUG_OBJECT (avi, "moved forwards from %u to %u",
4514 old_entry, new_entry);
4515 /* we simply moved one step forwards, reuse current info */
4516 stream->current_timestamp = stream->current_ts_end;
4517 stream->current_offset = stream->current_offset_end;
4518 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4519 NULL, &stream->current_ts_end, NULL, &stream->current_offset_end);
4521 /* we moved DISCONT, full update */
4522 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4523 &stream->current_timestamp, &stream->current_ts_end,
4524 &stream->current_offset, &stream->current_offset_end);
4525 /* and MARK discont for this stream */
4526 stream->last_flow = GST_FLOW_OK;
4527 stream->discont = TRUE;
4528 GST_DEBUG_OBJECT (avi, "Moved from %u to %u, ts %" GST_TIME_FORMAT
4529 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4530 ", off_end %" G_GUINT64_FORMAT, old_entry, new_entry,
4531 GST_TIME_ARGS (stream->current_timestamp),
4532 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4533 stream->current_offset_end);
4541 GST_DEBUG_OBJECT (avi, "we are EOS");
4542 /* setting current_timestamp to -1 marks EOS */
4543 stream->current_timestamp = -1;
4544 return GST_FLOW_UNEXPECTED;
4548 /* find the stream with the lowest current position when going forwards or with
4549 * the highest position when going backwards, this is the stream
4550 * we should push from next */
4552 gst_avi_demux_find_next (GstAviDemux * avi, gfloat rate)
4554 guint64 min_time, max_time;
4555 guint stream_num, i;
4558 min_time = G_MAXUINT64;
4561 for (i = 0; i < avi->num_streams; i++) {
4563 GstAviStream *stream;
4565 stream = &avi->stream[i];
4567 /* ignore streams that finished */
4568 if (stream->last_flow == GST_FLOW_UNEXPECTED)
4571 position = stream->current_timestamp;
4573 /* position of -1 is EOS */
4574 if (position != -1) {
4575 if (rate > 0.0 && position < min_time) {
4576 min_time = position;
4578 } else if (rate < 0.0 && position >= max_time) {
4579 max_time = position;
4587 static GstFlowReturn
4588 gst_avi_demux_loop_data (GstAviDemux * avi)
4590 GstFlowReturn ret = GST_FLOW_OK;
4592 GstAviStream *stream;
4593 gboolean processed = FALSE;
4595 guint64 offset, size;
4596 GstClockTime timestamp, duration;
4597 guint64 out_offset, out_offset_end;
4599 GstAviIndexEntry *entry;
4602 stream_num = gst_avi_demux_find_next (avi, avi->segment.rate);
4605 if (G_UNLIKELY (stream_num == -1)) {
4606 GST_DEBUG_OBJECT (avi, "all streams are EOS");
4610 /* we have the stream now */
4611 stream = &avi->stream[stream_num];
4613 /* skip streams without pads */
4615 GST_DEBUG_OBJECT (avi, "skipping entry from stream %d without pad",
4620 /* get the timing info for the entry */
4621 timestamp = stream->current_timestamp;
4622 duration = stream->current_ts_end - timestamp;
4623 out_offset = stream->current_offset;
4624 out_offset_end = stream->current_offset_end;
4626 /* get the entry data info */
4627 entry = &stream->index[stream->current_entry];
4628 offset = entry->offset;
4630 keyframe = ENTRY_IS_KEYFRAME (entry);
4632 /* skip empty entries */
4634 GST_DEBUG_OBJECT (avi, "Skipping entry %u (%" G_GUINT64_FORMAT ", %p)",
4635 stream->current_entry, size, stream->pad);
4639 if (avi->segment.rate > 0.0) {
4640 /* only check this for fowards playback for now */
4641 if (keyframe && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
4642 && (timestamp > avi->segment.stop)) {
4647 GST_LOG ("reading buffer (size=%" G_GUINT64_FORMAT "), stream %d, pos %"
4648 G_GUINT64_FORMAT " (0x%" G_GINT64_MODIFIER "x), kf %d", size,
4649 stream_num, offset, offset, keyframe);
4651 /* FIXME, check large chunks and cut them up */
4653 /* pull in the data */
4654 ret = gst_pad_pull_range (avi->sinkpad, offset, size, &buf);
4655 if (ret != GST_FLOW_OK)
4658 /* check for short buffers, this is EOS as well */
4659 if (GST_BUFFER_SIZE (buf) < size)
4662 /* invert the picture if needed */
4663 buf = gst_avi_demux_invert (stream, buf);
4665 /* mark non-keyframes */
4667 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4669 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4671 GST_BUFFER_TIMESTAMP (buf) = timestamp;
4672 GST_BUFFER_DURATION (buf) = duration;
4673 GST_BUFFER_OFFSET (buf) = out_offset;
4674 GST_BUFFER_OFFSET_END (buf) = out_offset_end;
4676 /* mark discont when pending */
4677 if (stream->discont) {
4678 GST_DEBUG_OBJECT (avi, "setting DISCONT flag");
4679 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4680 stream->discont = FALSE;
4683 gst_avi_demux_add_assoc (avi, stream, timestamp, offset, keyframe);
4685 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4687 /* update current position in the segment */
4688 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, timestamp);
4690 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %u, ts %"
4691 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4692 ", off_end %" G_GUINT64_FORMAT,
4693 GST_BUFFER_SIZE (buf), GST_TIME_ARGS (timestamp),
4694 GST_TIME_ARGS (duration), out_offset, out_offset_end);
4696 ret = gst_pad_push (stream->pad, buf);
4698 /* mark as processed, we increment the frame and byte counters then
4699 * leave the while loop and return the GstFlowReturn */
4702 if (avi->segment.rate < 0) {
4703 if (timestamp > avi->segment.stop && ret == GST_FLOW_UNEXPECTED) {
4704 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
4705 * we are at the end of the segment, so we just need to jump
4706 * back to the previous section. */
4707 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
4712 /* move to next item */
4713 ret = gst_avi_demux_advance (avi, stream, ret);
4716 ret = gst_avi_demux_combine_flows (avi, stream, ret);
4717 } while (!processed);
4725 GST_DEBUG_OBJECT (avi, "No samples left for any streams - EOS");
4726 ret = GST_FLOW_UNEXPECTED;
4731 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
4732 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
4733 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (avi->segment.stop));
4734 ret = GST_FLOW_UNEXPECTED;
4735 /* move to next stream */
4740 GST_DEBUG_OBJECT (avi, "pull range failed: pos=%" G_GUINT64_FORMAT
4741 " size=%" G_GUINT64_FORMAT, offset, size);
4746 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
4747 ", only got %d/%" G_GUINT64_FORMAT " bytes (truncated file?)", offset,
4748 GST_BUFFER_SIZE (buf), size);
4749 gst_buffer_unref (buf);
4750 ret = GST_FLOW_UNEXPECTED;
4756 * Read data. If we have an index it delegates to
4757 * gst_avi_demux_process_next_entry().
4759 static GstFlowReturn
4760 gst_avi_demux_stream_data (GstAviDemux * avi)
4765 GstFlowReturn res = GST_FLOW_OK;
4766 GstFormat format = GST_FORMAT_TIME;
4768 if (G_UNLIKELY (avi->have_eos)) {
4769 /* Clean adapter, we're done */
4770 gst_adapter_clear (avi->adapter);
4771 return GST_FLOW_UNEXPECTED;
4774 if (G_UNLIKELY (avi->todrop)) {
4777 if ((drop = gst_adapter_available (avi->adapter))) {
4778 if (drop > avi->todrop)
4780 GST_DEBUG_OBJECT (avi, "Dropping %d bytes", drop);
4781 gst_adapter_flush (avi->adapter, drop);
4782 avi->todrop -= drop;
4783 avi->offset += drop;
4787 /* Iterate until need more data, so adapter won't grow too much */
4789 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
4793 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
4794 GST_FOURCC_ARGS (tag), size);
4796 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
4797 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
4798 GST_LOG ("Chunk ok");
4799 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
4800 GST_DEBUG ("Found sub-index tag");
4801 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4802 /* accept 0 size buffer here */
4803 avi->abort_buffering = FALSE;
4804 GST_DEBUG (" skipping %d bytes for now", size);
4805 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4808 } else if (tag == GST_RIFF_TAG_RIFF) {
4809 /* RIFF tags can appear in ODML files, just jump over them */
4810 if (gst_adapter_available (avi->adapter) >= 12) {
4811 GST_DEBUG ("Found RIFF tag, skipping RIFF header");
4812 gst_adapter_flush (avi->adapter, 12);
4816 } else if (tag == GST_RIFF_TAG_idx1) {
4817 GST_DEBUG ("Found index tag");
4818 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4819 /* accept 0 size buffer here */
4820 avi->abort_buffering = FALSE;
4821 GST_DEBUG (" skipping %d bytes for now", size);
4822 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4825 } else if (tag == GST_RIFF_TAG_LIST) {
4826 /* movi chunks might be grouped in rec list */
4827 if (gst_adapter_available (avi->adapter) >= 12) {
4828 GST_DEBUG ("Found LIST tag, skipping LIST header");
4829 gst_adapter_flush (avi->adapter, 12);
4833 } else if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ) {
4834 /* rec list might contain JUNK chunks */
4835 GST_DEBUG ("Found JUNK tag");
4836 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4837 /* accept 0 size buffer here */
4838 avi->abort_buffering = FALSE;
4839 GST_DEBUG (" skipping %d bytes for now", size);
4840 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4844 GST_DEBUG ("No more stream chunks, send EOS");
4845 avi->have_eos = TRUE;
4846 return GST_FLOW_UNEXPECTED;
4849 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
4850 /* supposedly one hopes to catch a nicer chunk later on ... */
4851 /* FIXME ?? give up here rather than possibly ending up going
4852 * through the whole file */
4853 if (avi->abort_buffering) {
4854 avi->abort_buffering = FALSE;
4855 gst_adapter_flush (avi->adapter, 8);
4859 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
4860 GST_FOURCC_ARGS (tag), size);
4862 stream_nr = CHUNKID_TO_STREAMNR (tag);
4864 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
4866 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
4867 stream_nr, GST_FOURCC_ARGS (tag));
4868 avi->offset += 8 + GST_ROUND_UP_2 (size);
4869 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4871 GstAviStream *stream;
4872 GstClockTime next_ts = 0;
4873 GstBuffer *buf = NULL;
4875 gboolean saw_desired_kf = stream_nr != avi->main_stream
4876 || avi->offset >= avi->seek_kf_offset;
4878 if (stream_nr == avi->main_stream && avi->offset == avi->seek_kf_offset) {
4879 GST_DEBUG_OBJECT (avi, "Desired keyframe reached");
4880 avi->seek_kf_offset = 0;
4883 if (saw_desired_kf) {
4884 gst_adapter_flush (avi->adapter, 8);
4886 buf = gst_adapter_take_buffer (avi->adapter, GST_ROUND_UP_2 (size));
4887 /* patch the size */
4888 GST_BUFFER_SIZE (buf) = size;
4890 GST_DEBUG_OBJECT (avi,
4891 "Desired keyframe not yet reached, flushing chunk");
4892 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4895 offset = avi->offset;
4896 avi->offset += 8 + GST_ROUND_UP_2 (size);
4898 stream = &avi->stream[stream_nr];
4900 /* set delay (if any)
4901 if (stream->strh->init_frames == stream->current_frame &&
4903 stream->delay = next_ts;
4906 /* parsing of corresponding header may have failed */
4907 if (G_UNLIKELY (!stream->pad)) {
4908 GST_WARNING_OBJECT (avi, "no pad for stream ID %" GST_FOURCC_FORMAT,
4909 GST_FOURCC_ARGS (tag));
4911 gst_buffer_unref (buf);
4913 /* get time of this buffer */
4914 gst_pad_query_position (stream->pad, &format, (gint64 *) & next_ts);
4915 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4918 gst_avi_demux_add_assoc (avi, stream, next_ts, offset, FALSE);
4920 /* increment our positions */
4921 stream->current_entry++;
4922 stream->current_total += size;
4924 /* update current position in the segment */
4925 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, next_ts);
4927 if (saw_desired_kf && buf) {
4928 GstClockTime dur_ts = 0;
4930 /* invert the picture if needed */
4931 buf = gst_avi_demux_invert (stream, buf);
4933 gst_pad_query_position (stream->pad, &format, (gint64 *) & dur_ts);
4934 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4937 GST_BUFFER_TIMESTAMP (buf) = next_ts;
4938 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
4939 if (stream->strh->type == GST_RIFF_FCC_vids) {
4940 GST_BUFFER_OFFSET (buf) = stream->current_entry - 1;
4941 GST_BUFFER_OFFSET_END (buf) = stream->current_entry;
4943 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4944 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
4947 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4948 GST_DEBUG_OBJECT (avi,
4949 "Pushing buffer with time=%" GST_TIME_FORMAT ", duration %"
4950 GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
4951 " and size %d over pad %s", GST_TIME_ARGS (next_ts),
4952 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
4953 GST_BUFFER_OFFSET (buf), size, GST_PAD_NAME (stream->pad));
4955 /* mark discont when pending */
4956 if (G_UNLIKELY (stream->discont)) {
4957 GST_DEBUG_OBJECT (avi, "Setting DISCONT");
4958 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4959 stream->discont = FALSE;
4961 res = gst_pad_push (stream->pad, buf);
4964 res = gst_avi_demux_combine_flows (avi, stream, res);
4965 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4966 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
4980 GST_DEBUG_OBJECT (avi, "format %s != GST_FORMAT_TIME",
4981 gst_format_get_name (format));
4982 res = GST_FLOW_ERROR;
4988 * Send pending tags.
4991 push_tag_lists (GstAviDemux * avi)
4999 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
5001 for (i = 0; i < avi->num_streams; i++) {
5002 GstAviStream *stream = &avi->stream[i];
5003 GstPad *pad = stream->pad;
5005 tags = stream->taglist;
5008 GST_DEBUG_OBJECT (pad, "Tags: %" GST_PTR_FORMAT, tags);
5010 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (avi), pad, tags);
5011 stream->taglist = NULL;
5015 if (!(tags = avi->globaltags))
5016 tags = gst_tag_list_new ();
5018 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
5019 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
5021 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, tags);
5022 gst_element_found_tags (GST_ELEMENT_CAST (avi), tags);
5023 avi->globaltags = NULL;
5024 avi->got_tags = FALSE;
5028 gst_avi_demux_loop (GstPad * pad)
5031 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
5033 switch (avi->state) {
5034 case GST_AVI_DEMUX_START:
5035 res = gst_avi_demux_stream_init_pull (avi);
5036 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5037 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5040 avi->state = GST_AVI_DEMUX_HEADER;
5042 case GST_AVI_DEMUX_HEADER:
5043 res = gst_avi_demux_stream_header_pull (avi);
5044 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5045 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5048 avi->state = GST_AVI_DEMUX_MOVI;
5050 case GST_AVI_DEMUX_MOVI:
5051 if (G_UNLIKELY (avi->seg_event)) {
5052 gst_avi_demux_push_event (avi, avi->seg_event);
5053 avi->seg_event = NULL;
5055 if (G_UNLIKELY (avi->got_tags)) {
5056 push_tag_lists (avi);
5058 /* process each index entry in turn */
5059 res = gst_avi_demux_loop_data (avi);
5061 /* pause when error */
5062 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5063 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
5068 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
5069 res = GST_FLOW_ERROR;
5077 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
5078 avi->segment_running = FALSE;
5079 gst_pad_pause_task (avi->sinkpad);
5081 if (GST_FLOW_IS_FATAL (res) || (res == GST_FLOW_NOT_LINKED)) {
5082 gboolean push_eos = TRUE;
5084 if (res == GST_FLOW_UNEXPECTED) {
5085 /* handle end-of-stream/segment */
5086 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5089 if ((stop = avi->segment.stop) == -1)
5090 stop = avi->segment.duration;
5092 GST_INFO_OBJECT (avi, "sending segment_done");
5094 gst_element_post_message
5095 (GST_ELEMENT_CAST (avi),
5096 gst_message_new_segment_done (GST_OBJECT_CAST (avi),
5097 GST_FORMAT_TIME, stop));
5101 /* for fatal errors we post an error message */
5102 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
5103 (_("Internal data stream error.")),
5104 ("streaming stopped, reason %s", gst_flow_get_name (res)));
5107 GST_INFO_OBJECT (avi, "sending eos");
5108 if (!gst_avi_demux_push_event (avi, gst_event_new_eos ()) &&
5109 (res == GST_FLOW_UNEXPECTED)) {
5110 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
5111 (NULL), ("got eos but no streams (yet)"));
5118 static GstFlowReturn
5119 gst_avi_demux_chain (GstPad * pad, GstBuffer * buf)
5122 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
5125 if (GST_BUFFER_IS_DISCONT (buf)) {
5126 GST_DEBUG_OBJECT (avi, "got DISCONT");
5127 gst_adapter_clear (avi->adapter);
5128 /* mark all streams DISCONT */
5129 for (i = 0; i < avi->num_streams; i++)
5130 avi->stream[i].discont = TRUE;
5133 GST_DEBUG ("Store %d bytes in adapter", GST_BUFFER_SIZE (buf));
5134 gst_adapter_push (avi->adapter, buf);
5136 switch (avi->state) {
5137 case GST_AVI_DEMUX_START:
5138 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
5139 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5143 case GST_AVI_DEMUX_HEADER:
5144 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
5145 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5149 case GST_AVI_DEMUX_MOVI:
5150 if (G_UNLIKELY (avi->seg_event)) {
5151 gst_avi_demux_push_event (avi, avi->seg_event);
5152 avi->seg_event = NULL;
5154 if (G_UNLIKELY (avi->got_tags)) {
5155 push_tag_lists (avi);
5157 res = gst_avi_demux_stream_data (avi);
5159 case GST_AVI_DEMUX_SEEK:
5165 /* obtain and parse indexes */
5166 if (avi->stream[0].indexes && !gst_avi_demux_read_subindexes_push (avi))
5167 /* seek in subindex read function failed */
5170 if (!avi->stream[0].indexes && !avi->have_index
5171 && avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
5172 gst_avi_demux_stream_index_push (avi);
5174 if (avi->have_index) {
5175 /* use the indexes now to construct nice durations */
5176 gst_avi_demux_calculate_durations_from_index (avi);
5178 /* still parsing indexes */
5182 GST_OBJECT_LOCK (avi);
5183 event = avi->seek_event;
5184 avi->seek_event = NULL;
5185 GST_OBJECT_UNLOCK (avi);
5187 /* calculate and perform seek */
5188 if (!avi_demux_handle_seek_push (avi, avi->sinkpad, event))
5191 gst_event_unref (event);
5192 avi->state = GST_AVI_DEMUX_MOVI;
5196 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
5197 ("Illegal internal state"));
5198 res = GST_FLOW_ERROR;
5202 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
5203 gst_flow_get_name (res));
5205 if (G_UNLIKELY (avi->abort_buffering))
5206 goto abort_buffering;
5213 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("failed to read indexes"));
5214 return GST_FLOW_ERROR;
5218 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("push mode seek failed"));
5219 return GST_FLOW_ERROR;
5223 avi->abort_buffering = FALSE;
5224 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
5225 return GST_FLOW_ERROR;
5230 gst_avi_demux_sink_activate (GstPad * sinkpad)
5232 if (gst_pad_check_pull_range (sinkpad)) {
5233 GST_DEBUG ("going to pull mode");
5234 return gst_pad_activate_pull (sinkpad, TRUE);
5236 GST_DEBUG ("going to push (streaming) mode");
5237 return gst_pad_activate_push (sinkpad, TRUE);
5242 gst_avi_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
5244 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (sinkpad));
5247 avi->segment_running = TRUE;
5248 avi->streaming = FALSE;
5249 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
5252 avi->segment_running = FALSE;
5253 return gst_pad_stop_task (sinkpad);
5258 gst_avi_demux_activate_push (GstPad * pad, gboolean active)
5260 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (pad));
5263 GST_DEBUG ("avi: activating push/chain function");
5264 avi->streaming = TRUE;
5266 /* create index for some push based seeking if not provided */
5267 GST_OBJECT_LOCK (avi);
5268 if (!avi->element_index) {
5269 GST_DEBUG_OBJECT (avi, "creating index");
5270 avi->element_index = gst_index_factory_make ("memindex");
5272 GST_OBJECT_UNLOCK (avi);
5273 /* object lock might be taken again */
5274 gst_index_get_writer_id (avi->element_index, GST_OBJECT_CAST (avi),
5278 GST_DEBUG ("avi: deactivating push/chain function");
5285 gst_avi_demux_set_index (GstElement * element, GstIndex * index)
5287 GstAviDemux *avi = GST_AVI_DEMUX (element);
5289 GST_OBJECT_LOCK (avi);
5290 if (avi->element_index)
5291 gst_object_unref (avi->element_index);
5293 avi->element_index = gst_object_ref (index);
5295 avi->element_index = NULL;
5297 GST_OBJECT_UNLOCK (avi);
5298 /* object lock might be taken again */
5300 gst_index_get_writer_id (index, GST_OBJECT_CAST (element), &avi->index_id);
5301 GST_DEBUG_OBJECT (avi, "Set index %" GST_PTR_FORMAT, avi->element_index);
5305 gst_avi_demux_get_index (GstElement * element)
5307 GstIndex *result = NULL;
5308 GstAviDemux *avi = GST_AVI_DEMUX (element);
5310 GST_OBJECT_LOCK (avi);
5311 if (avi->element_index)
5312 result = gst_object_ref (avi->element_index);
5313 GST_OBJECT_UNLOCK (avi);
5315 GST_DEBUG_OBJECT (avi, "Returning index %" GST_PTR_FORMAT, result);
5320 static GstStateChangeReturn
5321 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
5323 GstStateChangeReturn ret;
5324 GstAviDemux *avi = GST_AVI_DEMUX (element);
5326 switch (transition) {
5327 case GST_STATE_CHANGE_READY_TO_PAUSED:
5328 avi->streaming = FALSE;
5329 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
5335 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5336 if (ret == GST_STATE_CHANGE_FAILURE)
5339 switch (transition) {
5340 case GST_STATE_CHANGE_PAUSED_TO_READY:
5341 gst_avi_demux_reset (avi);