2 * Copyright (C) <1999> Erik Walthinsen <omega@temple-baptist.com>
3 * Copyright (C) <2006> Nokia Corporation (contact <stefan.kost@nokia.com>)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 /* Element-Checklist-Version: 5 */
23 * SECTION:element-avidemux
25 * Demuxes an .avi file into raw or compressed audio and/or video streams.
27 * This element supports both push and pull-based scheduling, depending on the
28 * capabilities of the upstream elements.
31 * <title>Example launch line</title>
33 * gst-launch filesrc location=test.avi ! avidemux name=demux demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink demux.video_00 ! queue ! decodebin ! ffmpegcolorspace ! videoscale ! autovideosink
34 * ]| Play (parse and decode) an .avi file and try to output it to
35 * an automatically detected soundcard and videosink. If the AVI file contains
36 * compressed audio or video data, this will only work if you have the
37 * right decoder elements/plugins installed.
40 * Last reviewed on 2006-12-29 (0.10.6)
49 #include "gst/riff/riff-media.h"
50 #include "gstavidemux.h"
52 #include <gst/gst-i18n-plugin.h>
53 #include <gst/base/gstadapter.h>
56 #define DIV_ROUND_UP(s,v) (((s) + ((v)-1)) / (v))
58 GST_DEBUG_CATEGORY_STATIC (avidemux_debug);
59 #define GST_CAT_DEFAULT avidemux_debug
61 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
64 GST_STATIC_CAPS ("video/x-msvideo")
67 static void gst_avi_demux_base_init (GstAviDemuxClass * klass);
68 static void gst_avi_demux_class_init (GstAviDemuxClass * klass);
69 static void gst_avi_demux_init (GstAviDemux * avi);
70 static void gst_avi_demux_finalize (GObject * object);
72 static void gst_avi_demux_reset (GstAviDemux * avi);
75 static const GstEventMask *gst_avi_demux_get_event_mask (GstPad * pad);
77 static gboolean gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event);
78 static gboolean gst_avi_demux_handle_sink_event (GstPad * pad,
80 static gboolean gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event);
83 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
85 static const GstQueryType *gst_avi_demux_get_src_query_types (GstPad * pad);
86 static gboolean gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query);
87 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
88 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
90 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
91 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
93 static gboolean gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
95 static void gst_avi_demux_loop (GstPad * pad);
96 static gboolean gst_avi_demux_sink_activate (GstPad * sinkpad);
97 static gboolean gst_avi_demux_sink_activate_pull (GstPad * sinkpad,
99 static gboolean gst_avi_demux_activate_push (GstPad * pad, gboolean active);
100 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstBuffer * buf);
102 static void gst_avi_demux_set_index (GstElement * element, GstIndex * index);
103 static GstIndex *gst_avi_demux_get_index (GstElement * element);
104 static GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
105 GstStateChange transition);
107 static GstElementClass *parent_class = NULL;
109 /* GObject methods */
112 gst_avi_demux_get_type (void)
114 static GType avi_demux_type = 0;
116 if (!avi_demux_type) {
117 static const GTypeInfo avi_demux_info = {
118 sizeof (GstAviDemuxClass),
119 (GBaseInitFunc) gst_avi_demux_base_init,
121 (GClassInitFunc) gst_avi_demux_class_init,
124 sizeof (GstAviDemux),
126 (GInstanceInitFunc) gst_avi_demux_init,
130 g_type_register_static (GST_TYPE_ELEMENT,
131 "GstAviDemux", &avi_demux_info, 0);
134 return avi_demux_type;
138 gst_avi_demux_base_init (GstAviDemuxClass * klass)
140 static const GstElementDetails gst_avi_demux_details =
141 GST_ELEMENT_DETAILS ("Avi demuxer",
143 "Demultiplex an avi file into audio and video",
144 "Erik Walthinsen <omega@cse.ogi.edu>\n"
145 "Wim Taymans <wim.taymans@chello.be>\n"
146 "Thijs Vermeir <thijsvermeir@gmail.com>");
147 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
148 GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl;
149 GstCaps *audcaps, *vidcaps, *subcaps;
151 audcaps = gst_riff_create_audio_template_caps ();
152 gst_caps_append (audcaps, gst_caps_new_simple ("audio/x-avi-unknown", NULL));
153 audiosrctempl = gst_pad_template_new ("audio_%02d",
154 GST_PAD_SRC, GST_PAD_SOMETIMES, audcaps);
156 vidcaps = gst_riff_create_video_template_caps ();
157 gst_caps_append (vidcaps, gst_riff_create_iavs_template_caps ());
158 gst_caps_append (vidcaps, gst_caps_new_simple ("video/x-avi-unknown", NULL));
159 videosrctempl = gst_pad_template_new ("video_%02d",
160 GST_PAD_SRC, GST_PAD_SOMETIMES, vidcaps);
162 subcaps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
163 subsrctempl = gst_pad_template_new ("subtitle_%02d",
164 GST_PAD_SRC, GST_PAD_SOMETIMES, subcaps);
165 gst_element_class_add_pad_template (element_class, audiosrctempl);
166 gst_element_class_add_pad_template (element_class, videosrctempl);
167 gst_element_class_add_pad_template (element_class, subsrctempl);
168 gst_element_class_add_pad_template (element_class,
169 gst_static_pad_template_get (&sink_templ));
170 gst_element_class_set_details (element_class, &gst_avi_demux_details);
174 gst_avi_demux_class_init (GstAviDemuxClass * klass)
176 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
177 GObjectClass *gobject_class = (GObjectClass *) klass;
179 GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
180 0, "Demuxer for AVI streams");
182 parent_class = g_type_class_peek_parent (klass);
184 gobject_class->finalize = gst_avi_demux_finalize;
185 gstelement_class->change_state =
186 GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
188 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_avi_demux_set_index);
189 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_avi_demux_get_index);
193 gst_avi_demux_init (GstAviDemux * avi)
195 avi->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
196 gst_pad_set_activate_function (avi->sinkpad,
197 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate));
198 gst_pad_set_activatepull_function (avi->sinkpad,
199 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate_pull));
200 gst_pad_set_activatepush_function (avi->sinkpad,
201 GST_DEBUG_FUNCPTR (gst_avi_demux_activate_push));
202 gst_pad_set_chain_function (avi->sinkpad,
203 GST_DEBUG_FUNCPTR (gst_avi_demux_chain));
204 gst_pad_set_event_function (avi->sinkpad,
205 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_sink_event));
206 gst_element_add_pad (GST_ELEMENT (avi), avi->sinkpad);
208 avi->adapter = gst_adapter_new ();
210 gst_avi_demux_reset (avi);
214 gst_avi_demux_finalize (GObject * object)
216 GstAviDemux *avi = GST_AVI_DEMUX (object);
218 GST_DEBUG ("AVI: finalize");
220 g_object_unref (avi->adapter);
222 G_OBJECT_CLASS (parent_class)->finalize (object);
226 gst_avi_demux_reset_stream (GstAviDemux * avi, GstAviStream * stream)
228 g_free (stream->strh);
229 g_free (stream->strf.data);
230 g_free (stream->name);
231 g_free (stream->index);
232 g_free (stream->indexes);
233 if (stream->initdata)
234 gst_buffer_unref (stream->initdata);
235 if (stream->extradata)
236 gst_buffer_unref (stream->extradata);
238 if (stream->exposed) {
239 gst_pad_set_active (stream->pad, FALSE);
240 gst_element_remove_pad (GST_ELEMENT (avi), stream->pad);
242 gst_object_unref (stream->pad);
244 if (stream->taglist) {
245 gst_tag_list_free (stream->taglist);
246 stream->taglist = NULL;
248 memset (stream, 0, sizeof (GstAviStream));
252 gst_avi_demux_reset (GstAviDemux * avi)
256 GST_DEBUG ("AVI: reset");
258 for (i = 0; i < avi->num_streams; i++)
259 gst_avi_demux_reset_stream (avi, &avi->stream[i]);
261 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
262 avi->num_streams = 0;
263 avi->num_v_streams = 0;
264 avi->num_a_streams = 0;
265 avi->num_t_streams = 0;
267 avi->state = GST_AVI_DEMUX_START;
270 avi->index_offset = 0;
274 if (avi->element_index)
275 gst_object_unref (avi->element_index);
276 avi->element_index = NULL;
278 if (avi->seek_event) {
279 gst_event_unref (avi->seek_event);
280 avi->seek_event = NULL;
284 gst_tag_list_free (avi->globaltags);
285 avi->globaltags = NULL;
287 avi->got_tags = TRUE; /* we always want to push global tags */
288 avi->have_eos = FALSE;
289 avi->seekable = TRUE;
291 gst_adapter_clear (avi->adapter);
293 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
297 /* GstElement methods */
300 static const GstFormat *
301 gst_avi_demux_get_src_formats (GstPad * pad)
303 GstAviStream *stream = gst_pad_get_element_private (pad);
305 static const GstFormat src_a_formats[] = {
311 static const GstFormat src_v_formats[] = {
317 return (stream->strh->type == GST_RIFF_FCC_auds ?
318 src_a_formats : src_v_formats);
322 /* assumes stream->strf.auds->av_bps != 0 */
323 static inline GstClockTime
324 avi_stream_convert_bytes_to_time_unchecked (GstAviStream * stream,
327 return gst_util_uint64_scale_int (bytes, GST_SECOND,
328 stream->strf.auds->av_bps);
331 static inline guint64
332 avi_stream_convert_time_to_bytes_unchecked (GstAviStream * stream,
335 return gst_util_uint64_scale_int (time, stream->strf.auds->av_bps,
339 /* assumes stream->strh->rate != 0 */
340 static inline GstClockTime
341 avi_stream_convert_frames_to_time_unchecked (GstAviStream * stream,
344 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
348 static inline guint64
349 avi_stream_convert_time_to_frames_unchecked (GstAviStream * stream,
352 return gst_util_uint64_scale (time, stream->strh->rate,
353 stream->strh->scale * GST_SECOND);
357 gst_avi_demux_src_convert (GstPad * pad,
358 GstFormat src_format,
359 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
361 GstAviStream *stream = gst_pad_get_element_private (pad);
365 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
366 ", dest_format:%s", gst_format_get_name (src_format), src_value,
367 gst_format_get_name (*dest_format));
369 if (G_UNLIKELY (src_format == *dest_format)) {
370 *dest_value = src_value;
373 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
377 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
378 (src_format == GST_FORMAT_BYTES
379 || *dest_format == GST_FORMAT_BYTES))) {
384 switch (src_format) {
385 case GST_FORMAT_TIME:
386 switch (*dest_format) {
387 case GST_FORMAT_BYTES:
388 *dest_value = gst_util_uint64_scale_int (src_value,
389 stream->strf.auds->av_bps, GST_SECOND);
391 case GST_FORMAT_DEFAULT:
393 gst_util_uint64_scale_round (src_value, stream->strh->rate,
394 stream->strh->scale * GST_SECOND);
401 case GST_FORMAT_BYTES:
402 switch (*dest_format) {
403 case GST_FORMAT_TIME:
404 if (stream->strf.auds->av_bps != 0) {
405 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
415 case GST_FORMAT_DEFAULT:
416 switch (*dest_format) {
417 case GST_FORMAT_TIME:
419 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
432 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
433 gst_format_get_name (*dest_format), *dest_value);
437 static const GstQueryType *
438 gst_avi_demux_get_src_query_types (GstPad * pad)
440 static const GstQueryType src_types[] = {
452 gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query)
455 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
457 GstAviStream *stream = gst_pad_get_element_private (pad);
459 if (!stream->strh || !stream->strf.data)
460 return gst_pad_query_default (pad, query);
462 switch (GST_QUERY_TYPE (query)) {
463 case GST_QUERY_POSITION:{
466 GST_DEBUG ("pos query for stream %u: frames %u, bytes %u",
467 stream->num, stream->current_entry, stream->current_total);
469 /* FIXME, this looks clumsy */
470 if (stream->strh->type == GST_RIFF_FCC_auds) {
471 if (stream->is_vbr) {
473 pos = gst_util_uint64_scale ((gint64) stream->current_entry *
474 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
475 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
476 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
477 } else if (stream->strf.auds->av_bps != 0) {
479 pos = gst_util_uint64_scale (stream->current_total, GST_SECOND,
480 (guint64) stream->strf.auds->av_bps);
481 GST_DEBUG_OBJECT (avi,
482 "CBR convert bytes %u, time %" GST_TIME_FORMAT,
483 stream->current_total, GST_TIME_ARGS (pos));
484 } else if (stream->idx_n != 0 && stream->total_bytes != 0) {
485 /* calculate timestamps based on percentage of length */
486 guint64 xlen = avi->avih->us_frame *
487 avi->avih->tot_frames * GST_USECOND;
489 if (stream->is_vbr) {
490 pos = gst_util_uint64_scale (xlen, stream->current_entry,
492 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
493 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
495 pos = gst_util_uint64_scale (xlen, stream->current_total,
496 stream->total_bytes);
497 GST_DEBUG_OBJECT (avi,
498 "CBR perc convert bytes %u, time %" GST_TIME_FORMAT,
499 stream->current_total, GST_TIME_ARGS (pos));
506 if (stream->strh->rate != 0) {
507 pos = gst_util_uint64_scale ((guint64) stream->current_entry *
508 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
510 pos = stream->current_entry * avi->avih->us_frame * GST_USECOND;
514 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
515 gst_query_set_position (query, GST_FORMAT_TIME, pos);
517 GST_WARNING ("pos query failed");
520 case GST_QUERY_DURATION:
524 if (stream->strh->type != GST_RIFF_FCC_auds &&
525 stream->strh->type != GST_RIFF_FCC_vids) {
530 gst_query_parse_duration (query, &fmt, NULL);
533 case GST_FORMAT_TIME:
534 gst_query_set_duration (query, fmt, stream->duration);
536 case GST_FORMAT_DEFAULT:
539 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
542 if (stream->idx_n >= 0)
543 gst_query_set_duration (query, fmt, stream->idx_n);
544 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
545 stream->duration, &fmt, &dur))
546 gst_query_set_duration (query, fmt, dur);
555 case GST_QUERY_SEEKING:{
558 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
559 if (fmt == GST_FORMAT_TIME) {
560 gboolean seekable = TRUE;
562 if (avi->streaming) {
563 seekable = avi->seekable;
566 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
567 0, stream->duration);
572 case GST_QUERY_CONVERT:{
573 GstFormat src_fmt, dest_fmt;
574 gint64 src_val, dest_val;
576 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
577 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
579 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
581 res = gst_pad_query_default (pad, query);
585 res = gst_pad_query_default (pad, query);
589 gst_object_unref (avi);
594 static const GstEventMask *
595 gst_avi_demux_get_event_mask (GstPad * pad)
597 static const GstEventMask masks[] = {
598 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
607 gst_avi_demux_seek_streams (GstAviDemux * avi, guint64 offset, gboolean before)
609 GstAviStream *stream;
610 GstIndexEntry *entry;
612 gint64 val, min = offset;
614 for (i = 0; i < avi->num_streams; i++) {
615 stream = &avi->stream[i];
617 entry = gst_index_get_assoc_entry (avi->element_index, stream->index_id,
618 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
619 GST_ASSOCIATION_FLAG_NONE, GST_FORMAT_BYTES, offset);
623 GST_DEBUG_OBJECT (avi, "stream %d, previous entry at %"
624 G_GUINT64_FORMAT, i, val);
625 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
633 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
634 stream->current_entry = 0;
635 stream->current_total = 0;
639 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
640 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT,
643 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &val);
644 stream->current_total = val;
645 gst_index_entry_assoc_map (entry, GST_FORMAT_DEFAULT, &val);
646 stream->current_entry = val;
652 #define GST_AVI_SEEK_PUSH_DISPLACE (4 * GST_SECOND)
655 gst_avi_demux_handle_sink_event (GstPad * pad, GstEvent * event)
658 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
660 GST_DEBUG_OBJECT (avi,
661 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
663 switch (GST_EVENT_TYPE (event)) {
664 case GST_EVENT_NEWSEGMENT:
668 gint64 start, stop, time, offset = 0;
671 GstIndexEntry *entry;
673 /* some debug output */
674 gst_segment_init (&segment, GST_FORMAT_UNDEFINED);
675 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
676 &start, &stop, &time);
677 gst_segment_set_newsegment_full (&segment, update, rate, arate, format,
679 GST_DEBUG_OBJECT (avi,
680 "received format %d newsegment %" GST_SEGMENT_FORMAT, format,
683 /* chain will send initial newsegment after pads have been added */
684 if (avi->state != GST_AVI_DEMUX_MOVI) {
685 GST_DEBUG_OBJECT (avi, "still starting, eating event");
689 /* we only expect a BYTE segment, e.g. following a seek */
690 if (format != GST_FORMAT_BYTES) {
691 GST_DEBUG_OBJECT (avi, "unsupported segment format, ignoring");
695 if (!avi->element_index) {
696 GST_WARNING_OBJECT (avi, "no index data, forcing EOS");
700 /* Let's check if we have an index entry for this position */
701 entry = gst_index_get_assoc_entry (avi->element_index, avi->index_id,
702 GST_INDEX_LOOKUP_AFTER, GST_ASSOCIATION_FLAG_NONE,
703 GST_FORMAT_BYTES, start);
705 /* we can not go where we have not yet been before ... */
707 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
712 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &time);
713 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &start);
714 stop = GST_CLOCK_TIME_NONE;
716 /* compensate for slack */
718 time += GST_AVI_SEEK_PUSH_DISPLACE;
720 /* set up segment and send downstream */
721 gst_segment_set_newsegment_full (&avi->segment, update, rate, arate,
722 GST_FORMAT_TIME, time, stop, time);
723 GST_DEBUG_OBJECT (avi, "Pushing newseg update %d, rate %g, "
724 "applied rate %g, format %d, start %" G_GINT64_FORMAT ", "
725 "stop %" G_GINT64_FORMAT, update, rate, arate, GST_FORMAT_TIME,
727 gst_avi_demux_push_event (avi,
728 gst_event_new_new_segment_full (update, rate, arate, GST_FORMAT_TIME,
731 GST_DEBUG_OBJECT (avi, "next chunk expected at %" G_GINT64_FORMAT, start);
733 /* adjust state for streaming thread accordingly */
734 avi->offset = offset;
735 gst_avi_demux_seek_streams (avi, offset, FALSE);
737 /* set up streaming thread */
738 avi->offset = offset;
739 avi->todrop = start - offset;
742 /* in any case, clear leftover in current segment, if any */
743 gst_adapter_clear (avi->adapter);
744 gst_event_unref (event);
749 avi->have_eos = TRUE;
754 if (avi->state != GST_AVI_DEMUX_MOVI) {
755 gst_event_unref (event);
756 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
757 (NULL), ("got eos and didn't receive a complete header object"));
758 } else if (!gst_avi_demux_push_event (avi, event)) {
759 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
760 (NULL), ("got eos but no streams (yet)"));
765 res = gst_pad_event_default (pad, event);
769 gst_object_unref (avi);
775 gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event)
778 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
780 GST_DEBUG_OBJECT (avi,
781 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
783 switch (GST_EVENT_TYPE (event)) {
785 /* handle seeking only in pull mode */
786 if (!avi->streaming) {
787 res = gst_avi_demux_handle_seek (avi, pad, event);
789 res = gst_avi_demux_handle_seek_push (avi, pad, event);
791 gst_event_unref (event);
794 case GST_EVENT_NAVIGATION:
796 gst_event_unref (event);
799 res = gst_pad_event_default (pad, event);
803 gst_object_unref (avi);
808 /* streaming helper (push) */
811 * gst_avi_demux_peek_chunk_info:
813 * @tag: holder for tag
814 * @size: holder for tag size
816 * Peek next chunk info (tag and size)
818 * Returns: TRUE when one chunk info has been got
821 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
823 const guint8 *data = NULL;
825 if (gst_adapter_available (avi->adapter) < 8)
828 data = gst_adapter_peek (avi->adapter, 8);
829 *tag = GST_READ_UINT32_LE (data);
830 *size = GST_READ_UINT32_LE (data + 4);
836 * gst_avi_demux_peek_chunk:
838 * @tag: holder for tag
839 * @size: holder for tag size
841 * Peek enough data for one full chunk
843 * Returns: %TRUE when one chunk has been got
846 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
848 guint32 peek_size = 0;
851 if (!gst_avi_demux_peek_chunk_info (avi, tag, size))
854 /* size 0 -> empty data buffer would surprise most callers,
855 * large size -> do not bother trying to squeeze that into adapter,
856 * so we throw poor man's exception, which can be caught if caller really
857 * wants to handle 0 size chunk */
858 if (!(*size) || (*size) >= (1 << 30))
861 peek_size = (*size + 1) & ~1;
862 available = gst_adapter_available (avi->adapter);
864 GST_DEBUG_OBJECT (avi,
865 "Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
866 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
868 if (available < (8 + peek_size))
876 GST_INFO_OBJECT (avi, "Failed to peek");
881 GST_INFO_OBJECT (avi,
882 "Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT, *size,
883 GST_FOURCC_ARGS (*tag));
884 /* chain should give up */
885 avi->abort_buffering = TRUE;
890 GST_INFO_OBJECT (avi, "need more %d < %" G_GUINT32_FORMAT,
891 available, 8 + peek_size);
899 * gst_avi_demux_parse_file_header:
900 * @element: caller element (used for errors/debug).
901 * @buf: input data to be used for parsing.
903 * "Open" a RIFF/AVI file. The buffer should be at least 12
904 * bytes long. Takes ownership of @buf.
906 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
907 * Throws an error, caller should error out (fatal).
910 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
915 stamp = gst_util_get_timestamp ();
917 /* riff_parse posts an error */
918 if (!gst_riff_parse_file_header (element, buf, &doctype))
921 if (doctype != GST_RIFF_RIFF_AVI)
924 stamp = gst_util_get_timestamp () - stamp;
925 GST_DEBUG_OBJECT (element, "parsing header %" GST_TIME_FORMAT,
926 GST_TIME_ARGS (stamp));
933 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
934 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
935 GST_FOURCC_ARGS (doctype)));
941 * Read AVI file tag when streaming
944 gst_avi_demux_stream_init_push (GstAviDemux * avi)
946 if (gst_adapter_available (avi->adapter) >= 12) {
949 tmp = gst_adapter_take_buffer (avi->adapter, 12);
951 GST_DEBUG ("Parsing avi header");
952 if (!gst_avi_demux_parse_file_header (GST_ELEMENT (avi), tmp)) {
953 return GST_FLOW_ERROR;
955 GST_DEBUG ("header ok");
958 avi->state = GST_AVI_DEMUX_HEADER;
967 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
970 GstBuffer *buf = NULL;
972 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
973 if (res != GST_FLOW_OK)
975 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
985 GST_DEBUG_OBJECT (avi, "error parsing file header");
986 return GST_FLOW_ERROR;
990 /* AVI header handling */
993 * gst_avi_demux_parse_avih:
994 * @element: caller element (used for errors/debug).
995 * @buf: input data to be used for parsing.
996 * @avih: pointer to structure (filled in by function) containing
997 * stream information (such as flags, number of streams, etc.).
999 * Read 'avih' header. Discards buffer after use.
1001 * Returns: TRUE on success, FALSE otherwise. Throws an error if
1002 * the header is invalid. The caller should error out
1006 gst_avi_demux_parse_avih (GstElement * element,
1007 GstBuffer * buf, gst_riff_avih ** _avih)
1009 gst_riff_avih *avih;
1014 if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_avih))
1015 goto avih_too_small;
1017 avih = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1019 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1020 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
1021 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
1022 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
1023 avih->flags = GUINT32_FROM_LE (avih->flags);
1024 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
1025 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
1026 avih->streams = GUINT32_FROM_LE (avih->streams);
1027 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
1028 avih->width = GUINT32_FROM_LE (avih->width);
1029 avih->height = GUINT32_FROM_LE (avih->height);
1030 avih->scale = GUINT32_FROM_LE (avih->scale);
1031 avih->rate = GUINT32_FROM_LE (avih->rate);
1032 avih->start = GUINT32_FROM_LE (avih->start);
1033 avih->length = GUINT32_FROM_LE (avih->length);
1037 GST_INFO_OBJECT (element, "avih tag found:");
1038 GST_INFO_OBJECT (element, " us_frame %u", avih->us_frame);
1039 GST_INFO_OBJECT (element, " max_bps %u", avih->max_bps);
1040 GST_INFO_OBJECT (element, " pad_gran %u", avih->pad_gran);
1041 GST_INFO_OBJECT (element, " flags 0x%08x", avih->flags);
1042 GST_INFO_OBJECT (element, " tot_frames %u", avih->tot_frames);
1043 GST_INFO_OBJECT (element, " init_frames %u", avih->init_frames);
1044 GST_INFO_OBJECT (element, " streams %u", avih->streams);
1045 GST_INFO_OBJECT (element, " bufsize %u", avih->bufsize);
1046 GST_INFO_OBJECT (element, " width %u", avih->width);
1047 GST_INFO_OBJECT (element, " height %u", avih->height);
1048 GST_INFO_OBJECT (element, " scale %u", avih->scale);
1049 GST_INFO_OBJECT (element, " rate %u", avih->rate);
1050 GST_INFO_OBJECT (element, " start %u", avih->start);
1051 GST_INFO_OBJECT (element, " length %u", avih->length);
1054 gst_buffer_unref (buf);
1061 GST_ELEMENT_ERROR (element, STREAM, DEMUX, (NULL), ("No buffer"));
1066 GST_ELEMENT_ERROR (element, STREAM, DEMUX, (NULL),
1067 ("Too small avih (%d available, %d needed)",
1068 GST_BUFFER_SIZE (buf), (int) sizeof (gst_riff_avih)));
1069 gst_buffer_unref (buf);
1075 * gst_avi_demux_parse_superindex:
1076 * @avi: caller element (used for debugging/errors).
1077 * @buf: input data to use for parsing.
1078 * @locations: locations in the file (byte-offsets) that contain
1079 * the actual indexes (see get_avi_demux_parse_subindex()).
1080 * The array ends with GST_BUFFER_OFFSET_NONE.
1082 * Reads superindex (openDML-2 spec stuff) from the provided data.
1084 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
1085 * on error, but they are not fatal.
1088 gst_avi_demux_parse_superindex (GstAviDemux * avi,
1089 GstBuffer * buf, guint64 ** _indexes)
1099 size = buf ? GST_BUFFER_SIZE (buf) : 0;
1103 data = GST_BUFFER_DATA (buf);
1105 /* check type of index. The opendml2 specs state that
1106 * there should be 4 dwords per array entry. Type can be
1107 * either frame or field (and we don't care). */
1108 if (GST_READ_UINT16_LE (data) != 4 ||
1109 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
1110 GST_WARNING_OBJECT (avi,
1111 "Superindex for stream has unexpected "
1112 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1113 GST_READ_UINT16_LE (data), data[2], data[3]);
1114 bpe = GST_READ_UINT16_LE (data) * 4;
1116 num = GST_READ_UINT32_LE (&data[4]);
1118 GST_DEBUG_OBJECT (avi, "got %d indexes", num);
1120 indexes = g_new (guint64, num + 1);
1121 for (i = 0; i < num; i++) {
1122 if (size < 24 + bpe * (i + 1))
1124 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
1125 GST_DEBUG_OBJECT (avi, "index %d at %" G_GUINT64_FORMAT, i, indexes[i]);
1127 indexes[i] = GST_BUFFER_OFFSET_NONE;
1128 *_indexes = indexes;
1130 gst_buffer_unref (buf);
1137 GST_ERROR_OBJECT (avi,
1138 "Not enough data to parse superindex (%d available, 24 needed)", size);
1140 gst_buffer_unref (buf);
1145 /* add an entry to the index of a stream. @num should be an estimate of the
1146 * total amount of index entries for all streams and is used to dynamically
1147 * allocate memory for the index entries. */
1148 static inline gboolean
1149 gst_avi_demux_add_index (GstAviDemux * avi, GstAviStream * stream,
1150 guint num, GstAviIndexEntry * entry)
1152 /* ensure index memory */
1153 if (G_UNLIKELY (stream->idx_n >= stream->idx_max)) {
1154 guint idx_max = stream->idx_max;
1155 GstAviIndexEntry *new_idx;
1157 /* we need to make some more room */
1159 /* initial size guess, assume each stream has an equal amount of entries,
1160 * overshoot with at least 8K */
1161 idx_max = (num / avi->num_streams) + (8192 / sizeof (GstAviIndexEntry));
1163 idx_max += 8192 / sizeof (GstAviIndexEntry);
1164 GST_DEBUG_OBJECT (avi, "expanded index from %u to %u",
1165 stream->idx_max, idx_max);
1167 new_idx = g_try_renew (GstAviIndexEntry, stream->index, idx_max);
1168 /* out of memory, if this fails stream->index is untouched. */
1169 if (G_UNLIKELY (!new_idx))
1172 stream->index = new_idx;
1173 stream->idx_max = idx_max;
1176 /* update entry total and stream stats. The entry total can be converted to
1177 * the timestamp of the entry easily. */
1178 if (stream->strh->type == GST_RIFF_FCC_auds) {
1181 if (stream->is_vbr) {
1182 entry->total = stream->total_blocks;
1184 entry->total = stream->total_bytes;
1186 blockalign = stream->strf.auds->blockalign;
1188 stream->total_blocks += DIV_ROUND_UP (entry->size, blockalign);
1190 stream->total_blocks++;
1192 if (stream->is_vbr) {
1193 entry->total = stream->idx_n;
1195 entry->total = stream->total_bytes;
1198 stream->total_bytes += entry->size;
1199 if (ENTRY_IS_KEYFRAME (entry))
1200 stream->n_keyframes++;
1203 GST_LOG_OBJECT (avi,
1204 "Adding stream %u, index entry %d, kf %d, size %u "
1205 ", offset %" G_GUINT64_FORMAT ", total %" G_GUINT64_FORMAT, stream->num,
1206 stream->idx_n, ENTRY_IS_KEYFRAME (entry), entry->size, entry->offset,
1208 stream->index[stream->idx_n++] = *entry;
1213 /* given @entry_n in @stream, calculate info such as timestamps and
1214 * offsets for the entry. */
1216 gst_avi_demux_get_buffer_info (GstAviDemux * avi, GstAviStream * stream,
1217 guint entry_n, GstClockTime * timestamp, GstClockTime * ts_end,
1218 guint64 * offset, guint64 * offset_end)
1220 GstAviIndexEntry *entry;
1222 entry = &stream->index[entry_n];
1224 if (stream->is_vbr) {
1225 /* VBR stream next timestamp */
1226 if (stream->strh->type == GST_RIFF_FCC_auds) {
1229 avi_stream_convert_frames_to_time_unchecked (stream, entry->total);
1231 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1236 avi_stream_convert_frames_to_time_unchecked (stream, entry_n);
1238 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1242 /* constant rate stream */
1245 avi_stream_convert_bytes_to_time_unchecked (stream, entry->total);
1247 *ts_end = avi_stream_convert_bytes_to_time_unchecked (stream,
1248 entry->total + entry->size);
1250 if (stream->strh->type == GST_RIFF_FCC_vids) {
1251 /* video offsets are the frame number */
1255 *offset_end = entry_n + 1;
1257 /* no offsets for audio */
1265 /* collect and debug stats about the indexes for all streams.
1266 * This method is also responsible for filling in the stream duration
1267 * as measured by the amount of index entries.
1269 * Returns TRUE if the index is not empty, else FALSE */
1271 gst_avi_demux_do_index_stats (GstAviDemux * avi)
1274 #ifndef GST_DISABLE_GST_DEBUG
1275 guint total_idx = 0, total_max = 0;
1278 /* get stream stats now */
1279 for (i = 0; i < avi->num_streams; i++) {
1280 GstAviStream *stream;
1282 if (G_UNLIKELY (!(stream = &avi->stream[i])))
1284 if (G_UNLIKELY (!stream->strh))
1286 if (G_UNLIKELY (!stream->index || stream->idx_n == 0))
1289 /* we interested in the end_ts of the last entry, which is the total
1290 * duration of this stream */
1291 gst_avi_demux_get_buffer_info (avi, stream, stream->idx_n - 1,
1292 NULL, &stream->idx_duration, NULL, NULL);
1294 #ifndef GST_DISABLE_GST_DEBUG
1295 total_idx += stream->idx_n;
1296 total_max += stream->idx_max;
1298 GST_INFO_OBJECT (avi, "Stream %d, dur %" GST_TIME_FORMAT ", %6u entries, "
1299 "%5u keyframes, entry size = %2u, total size = %10u, allocated %10u",
1300 i, GST_TIME_ARGS (stream->idx_duration), stream->idx_n,
1301 stream->n_keyframes, (guint) sizeof (GstAviIndexEntry),
1302 (guint) (stream->idx_n * sizeof (GstAviIndexEntry)),
1303 (guint) (stream->idx_max * sizeof (GstAviIndexEntry)));
1305 #ifndef GST_DISABLE_GST_DEBUG
1306 total_idx *= sizeof (GstAviIndexEntry);
1307 total_max *= sizeof (GstAviIndexEntry);
1309 GST_INFO_OBJECT (avi, "%u bytes for index vs %u ideally, %u wasted",
1310 total_max, total_idx, total_max - total_idx);
1312 if (total_idx == 0) {
1313 GST_WARNING_OBJECT (avi, "Index is empty !");
1320 * gst_avi_demux_parse_subindex:
1322 * @buf: input data to use for parsing.
1323 * @stream: stream context.
1324 * @entries_list: a list (returned by the function) containing all the
1325 * indexes parsed in this specific subindex. The first
1326 * entry is also a pointer to allocated memory that needs
1327 * to be free´ed. May be NULL if no supported indexes were
1330 * Reads superindex (openDML-2 spec stuff) from the provided data.
1331 * The buffer should contain a GST_RIFF_TAG_ix?? chunk.
1333 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1334 * throw an error, caller should bail out asap.
1337 gst_avi_demux_parse_subindex (GstAviDemux * avi, GstAviStream * stream,
1349 size = GST_BUFFER_SIZE (buf);
1355 data = GST_BUFFER_DATA (buf);
1357 /* We don't support index-data yet */
1359 goto not_implemented;
1361 /* check type of index. The opendml2 specs state that
1362 * there should be 4 dwords per array entry. Type can be
1363 * either frame or field (and we don't care). */
1364 bpe = (data[2] & 0x01) ? 12 : 8;
1365 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1366 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1367 GST_WARNING_OBJECT (avi,
1368 "Superindex for stream %d has unexpected "
1369 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1370 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1371 bpe = GST_READ_UINT16_LE (data) * 4;
1373 num = GST_READ_UINT32_LE (&data[4]);
1374 baseoff = GST_READ_UINT64_LE (&data[12]);
1376 /* If there's nothing, just return ! */
1380 GST_INFO_OBJECT (avi, "Parsing subindex, nr_entries = %6d", num);
1382 for (i = 0; i < num; i++) {
1383 GstAviIndexEntry entry;
1385 if (size < 24 + bpe * (i + 1))
1388 /* fill in offset and size. offset contains the keyframe flag in the
1390 entry.offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1391 entry.size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1393 if (stream->strh->type == GST_RIFF_FCC_auds) {
1394 /* all audio frames are keyframes */
1395 ENTRY_SET_KEYFRAME (&entry);
1397 /* else read flags */
1398 entry.flags = (entry.size & 0x80000000) ? 0 : GST_AVI_KEYFRAME;
1400 entry.size &= ~0x80000000;
1403 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
1406 gst_buffer_unref (buf);
1413 GST_ERROR_OBJECT (avi,
1414 "Not enough data to parse subindex (%d available, 24 needed)", size);
1415 gst_buffer_unref (buf);
1416 return TRUE; /* continue */
1420 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1421 ("Subindex-is-data is not implemented"));
1422 gst_buffer_unref (buf);
1427 GST_DEBUG_OBJECT (avi, "the index is empty");
1428 gst_buffer_unref (buf);
1433 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1434 ("Cannot allocate memory for %u*%u=%u bytes",
1435 (guint) sizeof (GstAviIndexEntry), num,
1436 (guint) sizeof (GstAviIndexEntry) * num));
1437 gst_buffer_unref (buf);
1444 * Read AVI index when streaming
1447 gst_avi_demux_read_subindexes_push (GstAviDemux * avi)
1449 guint32 tag = 0, size;
1450 GstBuffer *buf = NULL;
1453 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1455 for (n = 0; n < avi->num_streams; n++) {
1456 GstAviStream *stream = &avi->stream[n];
1458 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1459 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1461 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1462 '0' + stream->num % 10)) &&
1463 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1464 '0' + stream->num % 10, 'i', 'x'))) {
1465 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1466 GST_FOURCC_ARGS (tag));
1470 avi->offset += 8 + GST_ROUND_UP_2 (size);
1472 buf = gst_buffer_new ();
1473 GST_BUFFER_DATA (buf) = gst_adapter_take (avi->adapter, size);
1474 GST_BUFFER_SIZE (buf) = size;
1476 if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1480 g_free (stream->indexes);
1481 stream->indexes = NULL;
1483 /* get stream stats now */
1484 avi->have_index = gst_avi_demux_do_index_stats (avi);
1492 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi)
1498 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1500 for (n = 0; n < avi->num_streams; n++) {
1501 GstAviStream *stream = &avi->stream[n];
1503 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1504 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi), avi->sinkpad,
1505 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1507 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1508 '0' + stream->num % 10)) &&
1509 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1510 '0' + stream->num % 10, 'i', 'x'))) {
1511 /* Some ODML files (created by god knows what muxer) have a ##ix format
1512 * instead of the 'official' ix##. They are still valid though. */
1513 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1514 GST_FOURCC_ARGS (tag));
1515 gst_buffer_unref (buf);
1519 if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1523 g_free (stream->indexes);
1524 stream->indexes = NULL;
1526 /* get stream stats now */
1527 avi->have_index = gst_avi_demux_do_index_stats (avi);
1531 * gst_avi_demux_riff_parse_vprp:
1532 * @element: caller element (used for debugging/error).
1533 * @buf: input data to be used for parsing, stripped from header.
1534 * @vprp: a pointer (returned by this function) to a filled-in vprp
1535 * structure. Caller should free it.
1537 * Parses a video stream´s vprp. This function takes ownership of @buf.
1539 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1540 * should be skipped on error, but it is not fatal.
1543 gst_avi_demux_riff_parse_vprp (GstElement * element,
1544 GstBuffer * buf, gst_riff_vprp ** _vprp)
1546 gst_riff_vprp *vprp;
1549 g_return_val_if_fail (buf != NULL, FALSE);
1550 g_return_val_if_fail (_vprp != NULL, FALSE);
1552 if (GST_BUFFER_SIZE (buf) < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1555 vprp = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1557 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1558 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1559 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1560 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1561 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1562 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1563 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1564 vprp->width = GUINT32_FROM_LE (vprp->width);
1565 vprp->height = GUINT32_FROM_LE (vprp->height);
1566 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1570 /* calculate fields based on size */
1571 k = (GST_BUFFER_SIZE (buf) - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) /
1573 if (vprp->fields > k) {
1574 GST_WARNING_OBJECT (element,
1575 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1578 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1579 GST_WARNING_OBJECT (element,
1580 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1581 GST_RIFF_VPRP_VIDEO_FIELDS);
1582 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1584 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1585 for (k = 0; k < vprp->fields; k++) {
1586 gst_riff_vprp_video_field_desc *fd;
1588 fd = &vprp->field_info[k];
1589 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1590 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1591 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1592 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1593 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1594 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1595 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1596 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1601 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1602 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1603 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1604 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1605 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1606 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1607 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1608 vprp->aspect & 0xffff);
1609 GST_INFO_OBJECT (element, " width %d", vprp->width);
1610 GST_INFO_OBJECT (element, " height %d", vprp->height);
1611 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1612 for (k = 0; k < vprp->fields; k++) {
1613 gst_riff_vprp_video_field_desc *fd;
1615 fd = &(vprp->field_info[k]);
1616 GST_INFO_OBJECT (element, " field %u description:", k);
1617 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1618 fd->compressed_bm_height);
1619 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1620 fd->compressed_bm_width);
1621 GST_INFO_OBJECT (element, " valid_bm_height %d",
1622 fd->valid_bm_height);
1623 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1624 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1625 fd->valid_bm_x_offset);
1626 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1627 fd->valid_bm_y_offset);
1628 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1629 fd->video_x_t_offset);
1630 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1633 gst_buffer_unref (buf);
1642 GST_ERROR_OBJECT (element,
1643 "Too small vprp (%d available, at least %d needed)",
1644 GST_BUFFER_SIZE (buf),
1645 (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1646 gst_buffer_unref (buf);
1652 gst_avi_demux_expose_streams (GstAviDemux * avi, gboolean force)
1656 GST_DEBUG_OBJECT (avi, "force : %d", force);
1658 for (i = 0; i < avi->num_streams; i++) {
1659 GstAviStream *stream = &avi->stream[i];
1661 if (force || stream->idx_n != 0) {
1662 GST_LOG_OBJECT (avi, "Added pad %s with caps %" GST_PTR_FORMAT,
1663 GST_PAD_NAME (stream->pad), GST_PAD_CAPS (stream->pad));
1664 gst_element_add_pad ((GstElement *) avi, stream->pad);
1665 stream->exposed = TRUE;
1667 GST_WARNING_OBJECT (avi, "Stream #%d doesn't have any entry, removing it",
1669 gst_avi_demux_reset_stream (avi, stream);
1675 * gst_avi_demux_parse_stream:
1676 * @avi: calling element (used for debugging/errors).
1677 * @buf: input buffer used to parse the stream.
1679 * Parses all subchunks in a strl chunk (which defines a single
1680 * stream). Discards the buffer after use. This function will
1681 * increment the stream counter internally.
1683 * Returns: whether the stream was identified successfully.
1684 * Errors are not fatal. It does indicate the stream
1688 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
1690 GstAviStream *stream;
1691 GstElementClass *klass;
1692 GstPadTemplate *templ;
1693 GstBuffer *sub = NULL;
1696 gchar *codec_name = NULL, *padname = NULL;
1697 const gchar *tag_name;
1698 GstCaps *caps = NULL;
1700 GstElement *element;
1701 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
1702 gst_riff_vprp *vprp = NULL;
1704 element = GST_ELEMENT_CAST (avi);
1706 GST_DEBUG_OBJECT (avi, "Parsing stream");
1708 if (avi->num_streams >= GST_AVI_DEMUX_MAX_STREAMS) {
1709 GST_WARNING_OBJECT (avi,
1710 "maximum no of streams (%d) exceeded, ignoring stream",
1711 GST_AVI_DEMUX_MAX_STREAMS);
1712 gst_buffer_unref (buf);
1713 /* not a fatal error, let's say */
1717 stream = &avi->stream[avi->num_streams];
1719 /* initial settings */
1720 stream->idx_duration = GST_CLOCK_TIME_NONE;
1721 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1722 stream->duration = GST_CLOCK_TIME_NONE;
1724 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
1725 /* sub can be NULL if the chunk is empty */
1727 GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
1728 GST_FOURCC_ARGS (tag));
1732 case GST_RIFF_TAG_strh:
1734 gst_riff_strh *strh;
1737 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
1740 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
1741 /* ownership given away */
1743 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
1747 strh = stream->strh;
1748 /* sanity check; stream header frame rate matches global header
1750 if (stream->strh->type == GST_RIFF_FCC_vids) {
1752 GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
1754 s_dur = gst_util_uint64_scale (GST_SECOND, strh->scale, strh->rate);
1755 GST_DEBUG_OBJECT (avi, "verifying stream framerate %d/%d, "
1756 "frame duration = %d ms", strh->rate, strh->scale,
1757 (gint) (s_dur / GST_MSECOND));
1758 if (h_dur > (10 * GST_MSECOND) && (s_dur > 10 * h_dur)) {
1759 strh->rate = GST_SECOND / GST_USECOND;
1760 strh->scale = h_dur / GST_USECOND;
1761 GST_DEBUG_OBJECT (avi, "correcting stream framerate to %d/%d",
1762 strh->rate, strh->scale);
1765 /* determine duration as indicated by header */
1766 stream->hdr_duration = gst_util_uint64_scale ((guint64) strh->length *
1767 strh->scale, GST_SECOND, (guint64) strh->rate);
1768 GST_INFO ("Stream duration according to header: %" GST_TIME_FORMAT,
1769 GST_TIME_ARGS (stream->hdr_duration));
1770 if (stream->hdr_duration == 0)
1771 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1776 case GST_RIFF_TAG_strf:
1778 gboolean res = FALSE;
1781 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
1785 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
1788 switch (stream->strh->type) {
1789 case GST_RIFF_FCC_vids:
1790 stream->is_vbr = TRUE;
1791 res = gst_riff_parse_strf_vids (element, sub,
1792 &stream->strf.vids, &stream->extradata);
1794 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
1796 case GST_RIFF_FCC_auds:
1797 stream->is_vbr = (stream->strh->samplesize == 0)
1798 && stream->strh->scale > 1;
1800 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
1801 &stream->extradata);
1803 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
1804 stream->is_vbr, res);
1806 case GST_RIFF_FCC_iavs:
1807 stream->is_vbr = TRUE;
1808 res = gst_riff_parse_strf_iavs (element, sub,
1809 &stream->strf.iavs, &stream->extradata);
1811 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
1813 case GST_RIFF_FCC_txts:
1814 /* nothing to parse here */
1815 stream->is_vbr = (stream->strh->samplesize == 0)
1816 && (stream->strh->scale > 1);
1820 GST_ERROR_OBJECT (avi,
1821 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
1822 GST_FOURCC_ARGS (stream->strh->type));
1826 gst_buffer_unref (sub);
1834 case GST_RIFF_TAG_vprp:
1837 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
1841 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
1845 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
1849 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
1850 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
1851 /* not considered fatal */
1859 case GST_RIFF_TAG_strd:
1860 if (stream->initdata)
1861 gst_buffer_unref (stream->initdata);
1862 stream->initdata = sub;
1865 case GST_RIFF_TAG_strn:
1866 g_free (stream->name);
1869 g_strndup ((gchar *) GST_BUFFER_DATA (sub),
1870 (gsize) GST_BUFFER_SIZE (sub));
1871 gst_buffer_unref (sub);
1874 stream->name = g_strdup ("");
1876 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
1879 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
1880 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
1881 '0' + avi->num_streams % 10)) {
1882 g_free (stream->indexes);
1883 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
1884 stream->superindex = TRUE;
1888 GST_WARNING_OBJECT (avi,
1889 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
1890 GST_FOURCC_ARGS (tag));
1892 case GST_RIFF_TAG_JUNK:
1896 gst_buffer_unref (sub);
1902 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
1907 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
1911 /* get class to figure out the template */
1912 klass = GST_ELEMENT_GET_CLASS (avi);
1914 /* we now have all info, let´s set up a pad and a caps and be done */
1915 /* create stream name + pad */
1916 switch (stream->strh->type) {
1917 case GST_RIFF_FCC_vids:{
1920 fourcc = (stream->strf.vids->compression) ?
1921 stream->strf.vids->compression : stream->strh->fcc_handler;
1922 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
1923 templ = gst_element_class_get_pad_template (klass, "video_%02d");
1924 caps = gst_riff_create_video_caps (fourcc, stream->strh,
1925 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
1927 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
1928 GST_TYPE_FOURCC, fourcc, NULL);
1929 } else if (got_vprp && vprp) {
1930 guint32 aspect_n, aspect_d;
1933 aspect_n = vprp->aspect >> 16;
1934 aspect_d = vprp->aspect & 0xffff;
1935 /* calculate the pixel aspect ratio using w/h and aspect ratio */
1936 n = aspect_n * stream->strf.vids->height;
1937 d = aspect_d * stream->strf.vids->width;
1939 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1941 /* very local, not needed elsewhere */
1945 tag_name = GST_TAG_VIDEO_CODEC;
1946 avi->num_v_streams++;
1949 case GST_RIFF_FCC_auds:{
1950 padname = g_strdup_printf ("audio_%02d", avi->num_a_streams);
1951 templ = gst_element_class_get_pad_template (klass, "audio_%02d");
1952 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
1953 stream->strh, stream->strf.auds, stream->extradata,
1954 stream->initdata, &codec_name);
1956 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
1957 G_TYPE_INT, stream->strf.auds->format, NULL);
1959 tag_name = GST_TAG_AUDIO_CODEC;
1960 avi->num_a_streams++;
1963 case GST_RIFF_FCC_iavs:{
1964 guint32 fourcc = stream->strh->fcc_handler;
1966 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
1967 templ = gst_element_class_get_pad_template (klass, "video_%02d");
1968 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
1969 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
1971 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
1972 GST_TYPE_FOURCC, fourcc, NULL);
1974 tag_name = GST_TAG_VIDEO_CODEC;
1975 avi->num_v_streams++;
1978 case GST_RIFF_FCC_txts:{
1979 padname = g_strdup_printf ("subtitle_%02d", avi->num_t_streams);
1980 templ = gst_element_class_get_pad_template (klass, "subtitle_%02d");
1981 caps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
1983 avi->num_t_streams++;
1987 g_assert_not_reached ();
1990 /* no caps means no stream */
1992 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
1996 GST_DEBUG_OBJECT (element, "codec-name=%s",
1997 (codec_name ? codec_name : "NULL"));
1998 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
2000 /* set proper settings and add it */
2002 gst_object_unref (stream->pad);
2003 pad = stream->pad = gst_pad_new_from_template (templ, padname);
2006 gst_pad_use_fixed_caps (pad);
2008 gst_pad_set_formats_function (pad,
2009 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
2010 gst_pad_set_event_mask_function (pad,
2011 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
2013 gst_pad_set_event_function (pad,
2014 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
2015 gst_pad_set_query_type_function (pad,
2016 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_query_types));
2017 gst_pad_set_query_function (pad,
2018 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
2020 gst_pad_set_convert_function (pad,
2021 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
2024 if (avi->element_index)
2025 gst_index_get_writer_id (avi->element_index, GST_OBJECT (stream->pad),
2028 stream->num = avi->num_streams;
2030 stream->start_entry = 0;
2031 stream->step_entry = 0;
2032 stream->stop_entry = 0;
2034 stream->current_entry = -1;
2035 stream->current_total = 0;
2037 stream->last_flow = GST_FLOW_OK;
2038 stream->discont = TRUE;
2040 stream->total_bytes = 0;
2041 stream->total_blocks = 0;
2042 stream->n_keyframes = 0;
2045 stream->idx_max = 0;
2047 gst_pad_set_element_private (pad, stream);
2050 gst_pad_set_caps (pad, caps);
2051 gst_pad_set_active (pad, TRUE);
2052 gst_caps_unref (caps);
2056 if (!stream->taglist)
2057 stream->taglist = gst_tag_list_new ();
2059 avi->got_tags = TRUE;
2061 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
2063 g_free (codec_name);
2066 gst_buffer_unref (buf);
2073 /* unref any mem that may be in use */
2075 gst_buffer_unref (buf);
2077 gst_buffer_unref (sub);
2079 g_free (codec_name);
2080 gst_avi_demux_reset_stream (avi, stream);
2087 * gst_avi_demux_parse_odml:
2088 * @avi: calling element (used for debug/error).
2089 * @buf: input buffer to be used for parsing.
2091 * Read an openDML-2.0 extension header. Fills in the frame number
2092 * in the avi demuxer object when reading succeeds.
2095 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
2099 GstBuffer *sub = NULL;
2101 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
2104 case GST_RIFF_TAG_dmlh:{
2105 gst_riff_dmlh dmlh, *_dmlh;
2108 /* sub == NULL is possible and means an empty buffer */
2109 size = sub ? GST_BUFFER_SIZE (sub) : 0;
2112 if (size < sizeof (gst_riff_dmlh)) {
2113 GST_ERROR_OBJECT (avi,
2114 "DMLH entry is too small (%d bytes, %d needed)",
2115 size, (int) sizeof (gst_riff_dmlh));
2118 _dmlh = (gst_riff_dmlh *) GST_BUFFER_DATA (sub);
2119 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
2121 GST_INFO_OBJECT (avi, "dmlh tag found: totalframes: %u",
2124 avi->avih->tot_frames = dmlh.totalframes;
2129 GST_WARNING_OBJECT (avi,
2130 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
2131 GST_FOURCC_ARGS (tag));
2133 case GST_RIFF_TAG_JUNK:
2135 /* skip and move to next chunk */
2137 gst_buffer_unref (sub);
2144 gst_buffer_unref (buf);
2149 gst_avi_demux_index_last (GstAviDemux * avi, GstAviStream * stream)
2151 return stream->idx_n - 1;
2154 /* find a previous entry in the index with the given flags */
2156 gst_avi_demux_index_prev (GstAviDemux * avi, GstAviStream * stream,
2157 guint last, gboolean keyframe)
2159 GstAviIndexEntry *entry;
2162 for (i = last; i > 0; i--) {
2163 entry = &stream->index[i - 1];
2164 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2172 gst_avi_demux_index_next (GstAviDemux * avi, GstAviStream * stream,
2173 guint last, gboolean keyframe)
2175 GstAviIndexEntry *entry;
2178 for (i = last + 1; i < stream->idx_n; i++) {
2179 entry = &stream->index[i];
2180 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2184 return stream->idx_n - 1;
2188 gst_avi_demux_index_entry_search (GstAviIndexEntry * entry, guint64 * total)
2190 if (entry->total < *total)
2192 else if (entry->total > *total)
2198 * gst_avi_demux_index_for_time:
2200 * @stream: the stream
2201 * @time: a time position
2203 * Finds the index entry which time is less or equal than the requested time.
2204 * Try to avoid binary search when we can convert the time to an index
2205 * position directly (for example for video frames with a fixed duration).
2207 * Returns: the found position in the index.
2210 gst_avi_demux_index_for_time (GstAviDemux * avi,
2211 GstAviStream * stream, guint64 time)
2216 GST_LOG_OBJECT (avi, "search time:%" GST_TIME_FORMAT, GST_TIME_ARGS (time));
2218 /* easy (and common) cases */
2219 if (time == 0 || stream->idx_n == 0)
2221 if (time >= stream->idx_duration)
2222 return stream->idx_n - 1;
2224 /* figure out where we need to go. For that we convert the time to an
2225 * index entry or we convert it to a total and then do a binary search. */
2226 if (stream->is_vbr) {
2227 /* VBR stream next timestamp */
2228 if (stream->strh->type == GST_RIFF_FCC_auds) {
2229 total = avi_stream_convert_time_to_frames_unchecked (stream, time);
2231 index = avi_stream_convert_time_to_frames_unchecked (stream, time);
2234 /* constant rate stream */
2235 total = avi_stream_convert_time_to_bytes_unchecked (stream, time);
2239 GstAviIndexEntry *entry;
2241 /* no index, find index with binary search on total */
2242 GST_LOG_OBJECT (avi, "binary search for entry with total %"
2243 G_GUINT64_FORMAT, total);
2245 entry = gst_util_array_binary_search (stream->index,
2246 stream->idx_n, sizeof (GstAviIndexEntry),
2247 (GCompareDataFunc) gst_avi_demux_index_entry_search,
2248 GST_SEARCH_MODE_BEFORE, &total, NULL);
2250 if (entry == NULL) {
2251 GST_LOG_OBJECT (avi, "not found, assume index 0");
2254 index = entry - stream->index;
2255 GST_LOG_OBJECT (avi, "found at %u", index);
2258 GST_LOG_OBJECT (avi, "converted time to index %u", index);
2264 static inline GstAviStream *
2265 gst_avi_demux_stream_for_id (GstAviDemux * avi, guint32 id)
2268 GstAviStream *stream;
2270 /* get the stream for this entry */
2271 stream_nr = CHUNKID_TO_STREAMNR (id);
2272 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2273 GST_WARNING_OBJECT (avi, "invalid stream nr %d", stream_nr);
2276 stream = &avi->stream[stream_nr];
2277 if (G_UNLIKELY (!stream->strh)) {
2278 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2285 * gst_avi_demux_parse_index:
2286 * @avi: calling element (used for debugging/errors).
2287 * @buf: buffer containing the full index.
2289 * Read index entries from the provided buffer.
2290 * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
2293 gst_avi_demux_parse_index (GstAviDemux * avi, GstBuffer * buf)
2299 gst_riff_index_entry *index;
2301 GstAviStream *stream;
2302 GstAviIndexEntry entry;
2308 data = GST_BUFFER_DATA (buf);
2309 size = GST_BUFFER_SIZE (buf);
2311 stamp = gst_util_get_timestamp ();
2313 /* see how many items in the index */
2314 num = size / sizeof (gst_riff_index_entry);
2318 GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2320 index = (gst_riff_index_entry *) data;
2321 pos_before = avi->offset;
2323 /* figure out if the index is 0 based or relative to the MOVI start */
2324 entry.offset = GST_READ_UINT32_LE (&index[0].offset);
2325 if (entry.offset < avi->offset) {
2326 avi->index_offset = avi->offset + 8;
2327 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2329 avi->index_offset = 0;
2330 GST_DEBUG ("index is 0 based");
2333 for (i = 0, n = 0; i < num; i++) {
2334 id = GST_READ_UINT32_LE (&index[i].id);
2335 entry.offset = GST_READ_UINT32_LE (&index[i].offset);
2337 /* some sanity checks */
2338 if (G_UNLIKELY (id == GST_RIFF_rec || id == 0 ||
2339 (entry.offset == 0 && n > 0)))
2342 /* get the stream for this entry */
2343 stream = gst_avi_demux_stream_for_id (avi, id);
2344 if (G_UNLIKELY (!stream))
2347 /* handle offset and size */
2348 entry.offset += avi->index_offset + 8;
2349 entry.size = GST_READ_UINT32_LE (&index[i].size);
2352 if (stream->strh->type == GST_RIFF_FCC_auds) {
2353 /* all audio frames are keyframes */
2354 ENTRY_SET_KEYFRAME (&entry);
2357 /* else read flags */
2358 flags = GST_READ_UINT32_LE (&index[i].flags);
2359 if (flags & GST_RIFF_IF_KEYFRAME) {
2360 ENTRY_SET_KEYFRAME (&entry);
2362 ENTRY_UNSET_KEYFRAME (&entry);
2367 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2372 gst_buffer_unref (buf);
2374 /* get stream stats now */
2375 avi->have_index = gst_avi_demux_do_index_stats (avi);
2377 stamp = gst_util_get_timestamp () - stamp;
2378 GST_DEBUG_OBJECT (avi, "parsing index %" GST_TIME_FORMAT,
2379 GST_TIME_ARGS (stamp));
2386 GST_DEBUG_OBJECT (avi, "empty index");
2387 gst_buffer_unref (buf);
2392 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2393 ("Cannot allocate memory for %u*%u=%u bytes",
2394 (guint) sizeof (GstAviIndexEntry), num,
2395 (guint) sizeof (GstAviIndexEntry) * num));
2396 gst_buffer_unref (buf);
2402 * gst_avi_demux_stream_index:
2403 * @avi: avi demuxer object.
2405 * Seeks to index and reads it.
2408 gst_avi_demux_stream_index (GstAviDemux * avi)
2411 guint64 offset = avi->offset;
2416 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2418 /* get chunk information */
2419 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2420 if (res != GST_FLOW_OK)
2422 else if (GST_BUFFER_SIZE (buf) < 8)
2425 /* check tag first before blindy trying to read 'size' bytes */
2426 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2427 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2428 if (tag == GST_RIFF_TAG_LIST) {
2429 /* this is the movi tag */
2430 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2431 (8 + GST_ROUND_UP_2 (size)));
2432 offset += 8 + GST_ROUND_UP_2 (size);
2433 gst_buffer_unref (buf);
2434 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2435 if (res != GST_FLOW_OK)
2437 else if (GST_BUFFER_SIZE (buf) < 8)
2439 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2440 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2443 if (tag != GST_RIFF_TAG_idx1)
2448 gst_buffer_unref (buf);
2450 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2452 /* read chunk, advance offset */
2453 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2454 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2457 GST_DEBUG ("will parse index chunk size %u for tag %"
2458 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2460 gst_avi_demux_parse_index (avi, buf);
2462 #ifndef GST_DISABLE_GST_DEBUG
2463 /* debug our indexes */
2466 GstAviStream *stream;
2468 for (i = 0; i < avi->num_streams; i++) {
2469 stream = &avi->stream[i];
2470 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2471 i, stream->idx_n, stream->total_bytes);
2480 GST_DEBUG_OBJECT (avi,
2481 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2486 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2487 gst_buffer_unref (buf);
2492 GST_WARNING_OBJECT (avi,
2493 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2494 GST_FOURCC_ARGS (tag));
2495 gst_buffer_unref (buf);
2500 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2501 gst_buffer_unref (buf);
2507 * gst_avi_demux_peek_tag:
2509 * Returns the tag and size of the next chunk
2511 static GstFlowReturn
2512 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2515 GstFlowReturn res = GST_FLOW_OK;
2516 GstBuffer *buf = NULL;
2520 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2521 if (res != GST_FLOW_OK)
2524 bufsize = GST_BUFFER_SIZE (buf);
2528 bufdata = GST_BUFFER_DATA (buf);
2530 *tag = GST_READ_UINT32_LE (bufdata);
2531 *size = GST_READ_UINT32_LE (bufdata + 4);
2533 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2534 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2535 *size, offset + 8, offset + 8 + (gint64) * size);
2538 gst_buffer_unref (buf);
2545 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2550 GST_DEBUG_OBJECT (avi, "got %d bytes which is <> 8 bytes", bufsize);
2551 res = GST_FLOW_ERROR;
2557 * gst_avi_demux_next_data_buffer:
2559 * Returns the offset and size of the next buffer
2560 * Position is the position of the buffer (after tag and size)
2562 static GstFlowReturn
2563 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2564 guint32 * tag, guint * size)
2566 guint64 off = *offset;
2571 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2572 if (res != GST_FLOW_OK)
2574 if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
2575 off += 8 + 4; /* skip tag + size + subtag */
2587 * gst_avi_demux_stream_scan:
2588 * @avi: calling element (used for debugging/errors).
2590 * Scan the file for all chunks to "create" a new index.
2594 gst_avi_demux_stream_scan (GstAviDemux * avi)
2597 GstAviStream *stream;
2606 * - implement non-seekable source support.
2608 GST_DEBUG_OBJECT (avi, "Creating index");
2610 /* get the size of the file */
2611 format = GST_FORMAT_BYTES;
2612 if (!gst_pad_query_peer_duration (avi->sinkpad, &format, &tmplength))
2616 /* guess the total amount of entries we expect */
2620 GstAviIndexEntry entry;
2623 /* start reading data buffers to find the id and offset */
2624 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
2625 if (G_UNLIKELY (res != GST_FLOW_OK))
2629 stream = gst_avi_demux_stream_for_id (avi, tag);
2630 if (G_UNLIKELY (!stream))
2633 /* we can't figure out the keyframes, assume they all are */
2634 entry.flags = GST_AVI_KEYFRAME;
2638 /* and add to the index of this stream */
2639 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2643 /* update position */
2644 pos += GST_ROUND_UP_2 (size);
2645 if (G_UNLIKELY (pos > length)) {
2646 GST_WARNING_OBJECT (avi,
2647 "Stopping index lookup since we are further than EOF");
2653 avi->have_index = gst_avi_demux_do_index_stats (avi);
2661 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2662 ("Cannot allocate memory for %u*%u=%u bytes",
2663 (guint) sizeof (GstAviIndexEntry), num,
2664 (guint) sizeof (GstAviIndexEntry) * num));
2670 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
2674 GstAviStream *stream;
2676 total = GST_CLOCK_TIME_NONE;
2678 /* all streams start at a timestamp 0 */
2679 for (i = 0; i < avi->num_streams; i++) {
2680 GstClockTime duration, hduration;
2681 gst_riff_strh *strh;
2683 stream = &avi->stream[i];
2684 if (G_UNLIKELY (!stream || !(strh = stream->strh)))
2687 /* get header duration for the stream */
2688 hduration = stream->hdr_duration;
2689 /* index duration calculated during parsing */
2690 duration = stream->idx_duration;
2692 /* now pick a good duration */
2693 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2694 /* index gave valid duration, use that */
2695 GST_INFO ("Stream %p duration according to index: %" GST_TIME_FORMAT,
2696 stream, GST_TIME_ARGS (duration));
2698 /* fall back to header info to calculate a duration */
2699 duration = hduration;
2701 /* set duration for the stream */
2702 stream->duration = duration;
2704 /* find total duration */
2705 if (total == GST_CLOCK_TIME_NONE || duration > total)
2709 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
2710 /* now update the duration for those streams where we had none */
2711 for (i = 0; i < avi->num_streams; i++) {
2712 stream = &avi->stream[i];
2714 if (!GST_CLOCK_TIME_IS_VALID (stream->duration)
2715 || stream->duration == 0) {
2716 stream->duration = total;
2718 GST_INFO ("Stream %p duration according to total: %" GST_TIME_FORMAT,
2719 stream, GST_TIME_ARGS (total));
2724 /* and set the total duration in the segment. */
2725 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
2726 GST_TIME_ARGS (total));
2728 gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
2731 /* returns FALSE if there are no pads to deliver event to,
2732 * otherwise TRUE (whatever the outcome of event sending),
2733 * takes ownership of the event. */
2735 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
2737 gboolean result = FALSE;
2740 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
2741 GST_EVENT_TYPE_NAME (event), avi->num_streams);
2743 for (i = 0; i < avi->num_streams; i++) {
2744 GstAviStream *stream = &avi->stream[i];
2748 gst_pad_push_event (stream->pad, gst_event_ref (event));
2751 gst_event_unref (event);
2756 gst_avi_demux_check_seekability (GstAviDemux * avi)
2759 gboolean seekable = FALSE;
2760 gint64 start = -1, stop = -1;
2762 query = gst_query_new_seeking (GST_FORMAT_BYTES);
2763 if (!gst_pad_peer_query (avi->sinkpad, query)) {
2764 GST_DEBUG_OBJECT (avi, "seeking query failed");
2768 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
2770 /* try harder to query upstream size if we didn't get it the first time */
2771 if (seekable && stop == -1) {
2772 GstFormat fmt = GST_FORMAT_BYTES;
2774 GST_DEBUG_OBJECT (avi, "doing duration query to fix up unset stop");
2775 gst_pad_query_peer_duration (avi->sinkpad, &fmt, &stop);
2778 /* if upstream doesn't know the size, it's likely that it's not seekable in
2779 * practice even if it technically may be seekable */
2780 if (seekable && (start != 0 || stop <= start)) {
2781 GST_DEBUG_OBJECT (avi, "seekable but unknown start/stop -> disable");
2785 if (!avi->element_index) {
2786 GST_DEBUG_OBJECT (avi, "no index");
2791 GST_INFO_OBJECT (avi, "seekable: %d (%" G_GUINT64_FORMAT " - %"
2792 G_GUINT64_FORMAT ")", seekable, start, stop);
2793 avi->seekable = seekable;
2795 gst_query_unref (query);
2799 * Read AVI headers when streaming
2801 static GstFlowReturn
2802 gst_avi_demux_stream_header_push (GstAviDemux * avi)
2804 GstFlowReturn ret = GST_FLOW_OK;
2809 GstBuffer *buf = NULL, *sub = NULL;
2813 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
2815 switch (avi->header_state) {
2816 case GST_AVI_DEMUX_HEADER_TAG_LIST:
2817 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2818 avi->offset += 8 + GST_ROUND_UP_2 (size);
2819 if (tag != GST_RIFF_TAG_LIST)
2820 goto header_no_list;
2822 gst_adapter_flush (avi->adapter, 8);
2823 /* Find the 'hdrl' LIST tag */
2824 GST_DEBUG ("Reading %d bytes", size);
2825 buf = gst_adapter_take_buffer (avi->adapter, size);
2827 if (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl)
2828 goto header_no_hdrl;
2832 gst_adapter_flush (avi->adapter, 1);
2834 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
2836 /* the hdrl starts with a 'avih' header */
2837 if (!gst_riff_parse_chunk (GST_ELEMENT (avi), buf, &offset, &tag, &sub))
2838 goto header_no_avih;
2840 if (tag != GST_RIFF_TAG_avih)
2841 goto header_no_avih;
2843 if (!gst_avi_demux_parse_avih (GST_ELEMENT (avi), sub, &avi->avih))
2844 goto header_wrong_avih;
2846 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
2848 /* now, read the elements from the header until the end */
2849 while (gst_riff_parse_chunk (GST_ELEMENT (avi), buf, &offset, &tag,
2851 /* sub can be NULL on empty tags */
2856 case GST_RIFF_TAG_LIST:
2857 if (GST_BUFFER_SIZE (sub) < 4)
2860 switch (GST_READ_UINT32_LE (GST_BUFFER_DATA (sub))) {
2861 case GST_RIFF_LIST_strl:
2862 if (!(gst_avi_demux_parse_stream (avi, sub))) {
2864 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
2865 ("failed to parse stream, ignoring"));
2870 case GST_RIFF_LIST_odml:
2871 gst_avi_demux_parse_odml (avi, sub);
2875 GST_WARNING_OBJECT (avi,
2876 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
2877 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA
2880 case GST_RIFF_TAG_JUNK:
2885 GST_WARNING_OBJECT (avi,
2886 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
2887 offset, GST_FOURCC_ARGS (tag));
2889 case GST_RIFF_TAG_JUNK:
2891 /* move to next chunk */
2893 gst_buffer_unref (sub);
2898 gst_buffer_unref (buf);
2899 GST_DEBUG ("elements parsed");
2901 /* check parsed streams */
2902 if (avi->num_streams == 0) {
2904 } else if (avi->num_streams != avi->avih->streams) {
2905 GST_WARNING_OBJECT (avi,
2906 "Stream header mentioned %d streams, but %d available",
2907 avi->avih->streams, avi->num_streams);
2909 GST_DEBUG ("Get junk and info next");
2910 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
2912 /* Need more data */
2916 case GST_AVI_DEMUX_HEADER_INFO:
2917 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
2919 if (gst_adapter_available (avi->adapter) < 12)
2922 data = gst_adapter_peek (avi->adapter, 12);
2923 tag = GST_READ_UINT32_LE (data);
2924 size = GST_READ_UINT32_LE (data + 4);
2925 ltag = GST_READ_UINT32_LE (data + 8);
2927 if (tag == GST_RIFF_TAG_LIST) {
2929 case GST_RIFF_LIST_movi:
2930 gst_adapter_flush (avi->adapter, 12);
2933 case GST_RIFF_LIST_INFO:
2934 GST_DEBUG ("Found INFO chunk");
2935 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2936 GST_DEBUG ("got size %d", size);
2938 gst_adapter_flush (avi->adapter, 12);
2940 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
2943 gst_adapter_flush (avi->adapter, 1);
2944 gst_riff_parse_info (GST_ELEMENT (avi), buf,
2946 gst_buffer_unref (buf);
2948 avi->offset += GST_ROUND_UP_2 (size) - 4;
2950 GST_DEBUG ("skipping INFO LIST prefix");
2953 /* Need more data */
2958 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2959 avi->offset += 8 + GST_ROUND_UP_2 (size);
2960 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
2961 // ??? goto iterate; ???
2963 /* Need more data */
2969 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2970 avi->offset += 8 + GST_ROUND_UP_2 (size);
2971 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
2974 /* Need more data */
2981 GST_WARNING ("unhandled header state: %d", avi->header_state);
2986 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
2987 avi->num_streams, avi->stream[0].indexes);
2989 GST_DEBUG ("Found movi chunk. Starting to stream data");
2990 avi->state = GST_AVI_DEMUX_MOVI;
2992 /* no indexes in push mode, but it still sets some variables */
2993 gst_avi_demux_calculate_durations_from_index (avi);
2995 gst_avi_demux_expose_streams (avi, TRUE);
2997 /* create initial NEWSEGMENT event */
2998 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
2999 stop = avi->segment.duration;
3001 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3003 if (avi->seek_event)
3004 gst_event_unref (avi->seek_event);
3005 avi->seek_event = gst_event_new_new_segment
3006 (FALSE, avi->segment.rate, GST_FORMAT_TIME,
3007 avi->segment.start, stop, avi->segment.time);
3009 gst_avi_demux_check_seekability (avi);
3011 /* at this point we know all the streams and we can signal the no more
3013 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3014 gst_element_no_more_pads (GST_ELEMENT (avi));
3021 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3022 return GST_FLOW_ERROR;
3026 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3027 ("Invalid AVI header (no LIST at start): %"
3028 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3029 return GST_FLOW_ERROR;
3033 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3034 ("Invalid AVI header (no hdrl at start): %"
3035 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3036 gst_buffer_unref (buf);
3037 return GST_FLOW_ERROR;
3041 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3042 ("Invalid AVI header (no avih at start): %"
3043 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3045 gst_buffer_unref (sub);
3047 gst_buffer_unref (buf);
3048 return GST_FLOW_ERROR;
3052 gst_buffer_unref (buf);
3053 return GST_FLOW_ERROR;
3058 * Read full AVI headers.
3060 static GstFlowReturn
3061 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
3064 GstBuffer *buf, *sub = NULL;
3068 GstElement *element = GST_ELEMENT_CAST (avi);
3071 stamp = gst_util_get_timestamp ();
3073 /* the header consists of a 'hdrl' LIST tag */
3074 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3075 if (res != GST_FLOW_OK)
3076 goto pull_range_failed;
3077 else if (tag != GST_RIFF_TAG_LIST)
3079 else if (GST_BUFFER_SIZE (buf) < 4)
3082 GST_DEBUG_OBJECT (avi, "parsing headers");
3084 /* Find the 'hdrl' LIST tag */
3085 while (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl) {
3086 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
3087 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf))));
3090 gst_buffer_unref (buf);
3092 /* read new chunk */
3093 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3094 if (res != GST_FLOW_OK)
3095 goto pull_range_failed;
3096 else if (tag != GST_RIFF_TAG_LIST)
3098 else if (GST_BUFFER_SIZE (buf) < 4)
3102 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
3104 /* the hdrl starts with a 'avih' header */
3105 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
3107 else if (tag != GST_RIFF_TAG_avih)
3109 else if (!gst_avi_demux_parse_avih (element, sub, &avi->avih))
3112 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3114 /* now, read the elements from the header until the end */
3115 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
3116 /* sub can be NULL on empty tags */
3121 case GST_RIFF_TAG_LIST:
3126 if (GST_BUFFER_SIZE (sub) < 4)
3129 data = GST_BUFFER_DATA (sub);
3130 fourcc = GST_READ_UINT32_LE (data);
3133 case GST_RIFF_LIST_strl:
3134 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3135 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3136 ("failed to parse stream, ignoring"));
3141 case GST_RIFF_LIST_odml:
3142 gst_avi_demux_parse_odml (avi, sub);
3146 GST_WARNING_OBJECT (avi,
3147 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3148 GST_FOURCC_ARGS (fourcc));
3149 GST_MEMDUMP_OBJECT (avi, "Unknown list", GST_BUFFER_DATA (sub),
3150 GST_BUFFER_SIZE (sub));
3152 case GST_RIFF_TAG_JUNK:
3158 GST_WARNING_OBJECT (avi,
3159 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
3160 GST_FOURCC_ARGS (tag), offset);
3161 GST_MEMDUMP_OBJECT (avi, "Unknown tag", GST_BUFFER_DATA (sub),
3162 GST_BUFFER_SIZE (sub));
3164 case GST_RIFF_TAG_JUNK:
3167 gst_buffer_unref (sub);
3172 gst_buffer_unref (buf);
3173 GST_DEBUG ("elements parsed");
3175 /* check parsed streams */
3176 if (avi->num_streams == 0)
3178 else if (avi->num_streams != avi->avih->streams) {
3179 GST_WARNING_OBJECT (avi,
3180 "Stream header mentioned %d streams, but %d available",
3181 avi->avih->streams, avi->num_streams);
3184 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
3185 G_GUINT64_FORMAT, avi->offset);
3187 /* Now, find the data (i.e. skip all junk between header and data) */
3193 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
3194 if (res != GST_FLOW_OK) {
3195 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
3196 goto pull_range_failed;
3197 } else if (GST_BUFFER_SIZE (buf) < 12) {
3198 GST_DEBUG_OBJECT (avi, "got %d bytes which is less than 12 bytes",
3199 GST_BUFFER_SIZE (buf));
3200 gst_buffer_unref (buf);
3201 return GST_FLOW_ERROR;
3204 data = GST_BUFFER_DATA (buf);
3206 tag = GST_READ_UINT32_LE (data);
3207 size = GST_READ_UINT32_LE (data + 4);
3208 ltag = GST_READ_UINT32_LE (data + 8);
3210 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
3211 GST_FOURCC_ARGS (tag), size);
3212 GST_MEMDUMP ("Tag content", data, GST_BUFFER_SIZE (buf));
3213 gst_buffer_unref (buf);
3216 case GST_RIFF_TAG_LIST:{
3218 case GST_RIFF_LIST_movi:
3219 GST_DEBUG_OBJECT (avi,
3220 "Reached the 'movi' tag, we're done with skipping");
3222 case GST_RIFF_LIST_INFO:
3224 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3226 if (res != GST_FLOW_OK) {
3227 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3228 goto pull_range_failed;
3230 GST_DEBUG ("got size %u", GST_BUFFER_SIZE (buf));
3232 GST_DEBUG ("skipping INFO LIST prefix");
3233 avi->offset += (4 - GST_ROUND_UP_2 (size));
3234 gst_buffer_unref (buf);
3238 sub = gst_buffer_create_sub (buf, 4, GST_BUFFER_SIZE (buf) - 4);
3239 gst_riff_parse_info (element, sub, &avi->globaltags);
3241 gst_buffer_unref (sub);
3244 gst_buffer_unref (buf);
3245 /* gst_riff_read_chunk() has already advanced avi->offset */
3248 GST_WARNING_OBJECT (avi,
3249 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3250 GST_FOURCC_ARGS (ltag));
3251 avi->offset += 8 + GST_ROUND_UP_2 (size);
3257 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3258 GST_FOURCC_ARGS (tag));
3260 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3261 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3262 avi->offset += 8 + GST_ROUND_UP_2 (size);
3268 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3269 avi->num_streams, avi->stream[0].indexes);
3271 /* create or read stream index (for seeking) */
3272 if (avi->stream[0].indexes != NULL) {
3273 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3274 gst_avi_demux_read_subindexes_pull (avi);
3276 if (!avi->have_index) {
3277 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
3278 gst_avi_demux_stream_index (avi);
3280 /* still no index, scan */
3281 if (!avi->have_index) {
3282 gst_avi_demux_stream_scan (avi);
3284 /* still no index.. this is a fatal error for now.
3285 * FIXME, we should switch to plain push mode without seeking
3286 * instead of failing. */
3287 if (!avi->have_index)
3291 /* use the indexes now to construct nice durations */
3292 gst_avi_demux_calculate_durations_from_index (avi);
3294 gst_avi_demux_expose_streams (avi, FALSE);
3296 /* create initial NEWSEGMENT event */
3297 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3298 stop = avi->segment.duration;
3300 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3302 /* do initial seek to the default segment values */
3303 gst_avi_demux_do_seek (avi, &avi->segment);
3304 /* prepare initial segment */
3305 if (avi->seek_event)
3306 gst_event_unref (avi->seek_event);
3307 avi->seek_event = gst_event_new_new_segment
3308 (FALSE, avi->segment.rate, GST_FORMAT_TIME,
3309 avi->segment.start, stop, avi->segment.time);
3311 stamp = gst_util_get_timestamp () - stamp;
3312 GST_DEBUG_OBJECT (avi, "pulling header %" GST_TIME_FORMAT,
3313 GST_TIME_ARGS (stamp));
3315 /* at this point we know all the streams and we can signal the no more
3317 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3318 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3325 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3326 ("Invalid AVI header (no LIST at start): %"
3327 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3328 gst_buffer_unref (buf);
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);
3346 gst_buffer_unref (buf);
3347 return GST_FLOW_ERROR;
3351 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3352 ("Invalid AVI header (cannot parse avih at start)"));
3353 gst_buffer_unref (buf);
3354 return GST_FLOW_ERROR;
3358 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3359 return GST_FLOW_ERROR;
3363 GST_WARNING ("file without or too big index");
3364 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3365 ("Could not get/create index"));
3366 return GST_FLOW_ERROR;
3370 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3371 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3372 return GST_FLOW_ERROR;
3376 /* move a stream to @index */
3378 gst_avi_demux_move_stream (GstAviDemux * avi, GstAviStream * stream,
3379 GstSegment * segment, guint index)
3381 GST_DEBUG_OBJECT (avi, "Move stream %d to %u", stream->num, index);
3383 if (segment->rate < 0.0) {
3385 /* Because we don't know the frame order we need to push from the prev keyframe
3386 * to the next keyframe. If there is a smart decoder downstream he will notice
3387 * that there are too many encoded frames send and return UNEXPECTED when there
3388 * are enough decoded frames to fill the segment. */
3389 next_key = gst_avi_demux_index_next (avi, stream, index, TRUE);
3391 /* FIXME, we go back to 0, we should look at segment.start. We will however
3392 * stop earlier when the see the timestamp < segment.start */
3393 stream->start_entry = 0;
3394 stream->step_entry = index;
3395 stream->current_entry = index;
3396 stream->stop_entry = next_key;
3398 GST_DEBUG_OBJECT (avi, "reverse seek: start %u, step %u, stop %u",
3399 stream->start_entry, stream->step_entry, stream->stop_entry);
3401 stream->start_entry = index;
3402 stream->step_entry = index;
3403 stream->stop_entry = gst_avi_demux_index_last (avi, stream);
3405 if (stream->current_entry != index) {
3406 GST_DEBUG_OBJECT (avi, "Move DISCONT from %u to %u",
3407 stream->current_entry, index);
3408 stream->current_entry = index;
3409 stream->discont = TRUE;
3412 /* update the buffer info */
3413 gst_avi_demux_get_buffer_info (avi, stream, index,
3414 &stream->current_timestamp, &stream->current_ts_end,
3415 &stream->current_offset, &stream->current_offset_end);
3417 GST_DEBUG_OBJECT (avi, "Moved to %u, ts %" GST_TIME_FORMAT
3418 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
3419 ", off_end %" G_GUINT64_FORMAT, index,
3420 GST_TIME_ARGS (stream->current_timestamp),
3421 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
3422 stream->current_offset_end);
3426 * Do the actual seeking.
3429 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
3431 GstClockTime seek_time;
3434 GstAviStream *stream;
3436 seek_time = segment->last_stop;
3437 keyframe = !!(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
3439 GST_DEBUG_OBJECT (avi, "seek to: %" GST_TIME_FORMAT
3440 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
3442 /* FIXME, this code assumes the main stream with keyframes is stream 0,
3443 * which is mostly correct... */
3444 stream = &avi->stream[0];
3446 /* get the entry index for the requested position */
3447 index = gst_avi_demux_index_for_time (avi, stream, seek_time);
3448 GST_DEBUG_OBJECT (avi, "Got entry %u", index);
3450 /* check if we are already on a keyframe */
3451 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
3452 GST_DEBUG_OBJECT (avi, "not keyframe, searching back");
3453 /* now go to the previous keyframe, this is where we should start
3455 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
3456 GST_DEBUG_OBJECT (avi, "previous keyframe at %u", index);
3459 /* move the main stream to this position */
3460 gst_avi_demux_move_stream (avi, stream, segment, index);
3463 /* when seeking to a keyframe, we update the result seek time
3464 * to the time of the keyframe. */
3465 seek_time = stream->current_timestamp;
3466 GST_DEBUG_OBJECT (avi, "keyframe adjusted to %" GST_TIME_FORMAT,
3467 GST_TIME_ARGS (seek_time));
3470 /* the seek time is also the last_stop and stream time when going
3472 segment->last_stop = seek_time;
3473 if (segment->rate > 0.0)
3474 segment->time = seek_time;
3476 /* now set DISCONT and align the other streams */
3477 for (i = 0; i < avi->num_streams; i++) {
3478 GstAviStream *ostream;
3480 ostream = &avi->stream[i];
3481 if (ostream == stream)
3484 /* get the entry index for the requested position */
3485 index = gst_avi_demux_index_for_time (avi, ostream, seek_time);
3487 /* move to previous keyframe */
3488 if (!ENTRY_IS_KEYFRAME (&ostream->index[index]))
3489 index = gst_avi_demux_index_prev (avi, ostream, index, TRUE);
3491 gst_avi_demux_move_stream (avi, ostream, segment, index);
3493 GST_DEBUG_OBJECT (avi, "done seek to: %" GST_TIME_FORMAT,
3494 GST_TIME_ARGS (seek_time));
3500 * Handle seek event.
3503 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
3508 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
3512 GstSegment seeksegment = { 0, };
3516 GST_DEBUG_OBJECT (avi, "doing seek with event");
3518 gst_event_parse_seek (event, &rate, &format, &flags,
3519 &cur_type, &cur, &stop_type, &stop);
3521 /* we have to have a format as the segment format. Try to convert
3523 if (format != GST_FORMAT_TIME) {
3524 GstFormat fmt = GST_FORMAT_TIME;
3525 gboolean res = TRUE;
3527 if (cur_type != GST_SEEK_TYPE_NONE)
3528 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
3529 if (res && stop_type != GST_SEEK_TYPE_NONE)
3530 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
3536 GST_DEBUG_OBJECT (avi,
3537 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
3538 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
3539 /* FIXME: can we do anything with rate!=1.0 */
3541 GST_DEBUG_OBJECT (avi, "doing seek without event");
3546 /* save flush flag */
3547 flush = flags & GST_SEEK_FLAG_FLUSH;
3550 GstEvent *fevent = gst_event_new_flush_start ();
3552 /* for a flushing seek, we send a flush_start on all pads. This will
3553 * eventually stop streaming with a WRONG_STATE. We can thus eventually
3554 * take the STREAM_LOCK. */
3555 GST_DEBUG_OBJECT (avi, "sending flush start");
3556 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
3557 gst_pad_push_event (avi->sinkpad, fevent);
3559 /* a non-flushing seek, we PAUSE the task so that we can take the
3561 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
3562 gst_pad_pause_task (avi->sinkpad);
3565 /* wait for streaming to stop */
3566 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
3567 GST_PAD_STREAM_LOCK (avi->sinkpad);
3569 /* copy segment, we need this because we still need the old
3570 * segment when we close the current segment. */
3571 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
3574 GST_DEBUG_OBJECT (avi, "configuring seek");
3575 gst_segment_set_seek (&seeksegment, rate, format, flags,
3576 cur_type, cur, stop_type, stop, &update);
3578 /* do the seek, seeksegment.last_stop contains the new position, this
3579 * actually never fails. */
3580 gst_avi_demux_do_seek (avi, &seeksegment);
3583 GstEvent *fevent = gst_event_new_flush_stop ();
3585 GST_DEBUG_OBJECT (avi, "sending flush stop");
3586 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
3587 gst_pad_push_event (avi->sinkpad, fevent);
3589 /* reset the last flow and mark discont, FLUSH is always DISCONT */
3590 for (i = 0; i < avi->num_streams; i++) {
3591 avi->stream[i].last_flow = GST_FLOW_OK;
3592 avi->stream[i].discont = TRUE;
3594 } else if (avi->segment_running) {
3597 /* we are running the current segment and doing a non-flushing seek,
3598 * close the segment first based on the last_stop. */
3599 GST_DEBUG_OBJECT (avi, "closing running segment %" G_GINT64_FORMAT
3600 " to %" G_GINT64_FORMAT, avi->segment.start, avi->segment.last_stop);
3601 seg = gst_event_new_new_segment (TRUE,
3602 avi->segment.rate, avi->segment.format,
3603 avi->segment.start, avi->segment.last_stop, avi->segment.time);
3604 gst_avi_demux_push_event (avi, seg);
3607 /* now update the real segment info */
3608 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
3610 /* post the SEGMENT_START message when we do segmented playback */
3611 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3612 gst_element_post_message (GST_ELEMENT (avi),
3613 gst_message_new_segment_start (GST_OBJECT (avi),
3614 avi->segment.format, avi->segment.last_stop));
3617 /* prepare for streaming again */
3618 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3619 stop = avi->segment.duration;
3621 /* queue the segment event for the streaming thread. */
3622 if (avi->seek_event)
3623 gst_event_unref (avi->seek_event);
3624 if (avi->segment.rate > 0.0) {
3625 /* forwards goes from last_stop to stop */
3626 avi->seek_event = gst_event_new_new_segment (FALSE,
3627 avi->segment.rate, avi->segment.format,
3628 avi->segment.last_stop, stop, avi->segment.time);
3630 /* reverse goes from start to last_stop */
3631 avi->seek_event = gst_event_new_new_segment (FALSE,
3632 avi->segment.rate, avi->segment.format,
3633 avi->segment.start, avi->segment.last_stop, avi->segment.time);
3636 if (!avi->streaming) {
3637 avi->segment_running = TRUE;
3638 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
3641 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
3648 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
3654 gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
3660 GstSeekType cur_type, stop_type;
3665 GstIndexEntry *entry;
3667 GST_DEBUG_OBJECT (avi, "doing push-based seek");
3669 gst_event_parse_seek (event, &rate, &format, &flags,
3670 &cur_type, &cur, &stop_type, &stop);
3672 if (stop_type != GST_SEEK_TYPE_NONE)
3673 goto unsupported_seek;
3676 /* only forward streaming and seeking is possible */
3678 goto unsupported_seek;
3681 if (format != GST_FORMAT_TIME)
3682 goto unsupported_format;
3684 /* not really advisable otherwise */
3685 if ((flags & GST_SEEK_FLAG_FLUSH) == 0)
3686 goto unsupported_seek;
3688 /* should have index, let's check anyway */
3689 if (!avi->element_index)
3692 /* find reasonable corresponding BYTE position
3693 * note that we have no keyframe info in push mode whatsoever,
3694 * so we can not cater for related stuff in any way */
3696 /* some slack aiming for a keyframe */
3697 if (cur < GST_AVI_SEEK_PUSH_DISPLACE)
3700 cur -= GST_AVI_SEEK_PUSH_DISPLACE;
3702 entry = gst_index_get_assoc_entry (avi->element_index, avi->index_id,
3703 GST_INDEX_LOOKUP_BEFORE, GST_ASSOCIATION_FLAG_NONE, GST_FORMAT_TIME, cur);
3708 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &byte_cur);
3709 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &time);
3711 GST_DEBUG_OBJECT (avi, "found index entry for %" GST_TIME_FORMAT
3712 " at %" GST_TIME_FORMAT ", located at offset %" G_GINT64_FORMAT,
3713 GST_TIME_ARGS (cur), GST_TIME_ARGS (time), byte_cur);
3715 /* adjust offset to be in each stream's region */
3716 byte_cur = gst_avi_demux_seek_streams (avi, byte_cur, TRUE);
3718 /* let's not try push seeking if target and entry are too far apart */
3719 if (cur - time > 30 * GST_SECOND)
3722 GST_DEBUG_OBJECT (avi, "Pushing BYTE seek rate %g, "
3723 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, byte_cur,
3725 /* BYTE seek event */
3726 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, byte_cur,
3728 res = gst_pad_push_event (avi->sinkpad, event);
3735 GST_DEBUG_OBJECT (avi, "could not determine byte position to seek to, "
3741 GST_DEBUG_OBJECT (avi, "unsupported seek, seek aborted.");
3746 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
3752 * Helper for gst_avi_demux_invert()
3755 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
3757 memcpy (tmp, d1, bytes);
3758 memcpy (d1, d2, bytes);
3759 memcpy (d2, tmp, bytes);
3763 #define gst_avi_demux_is_uncompressed(fourcc) \
3764 (fourcc == GST_RIFF_DIB || \
3765 fourcc == GST_RIFF_rgb || \
3766 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW)
3769 * Invert DIB buffers... Takes existing buffer and
3770 * returns either the buffer or a new one (with old
3771 * one dereferenced).
3772 * FIXME: can't we preallocate tmp? and remember stride, bpp?
3775 gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
3782 if (stream->strh->type != GST_RIFF_FCC_vids)
3785 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
3786 return buf; /* Ignore non DIB buffers */
3789 s = gst_caps_get_structure (GST_PAD_CAPS (stream->pad), 0);
3790 if (!gst_structure_get_int (s, "bpp", &bpp)) {
3791 GST_WARNING ("Failed to retrieve depth from caps");
3795 if (stream->strf.vids == NULL) {
3796 GST_WARNING ("Failed to retrieve vids for stream");
3800 h = stream->strf.vids->height;
3801 w = stream->strf.vids->width;
3802 stride = w * (bpp / 8);
3804 buf = gst_buffer_make_writable (buf);
3805 if (GST_BUFFER_SIZE (buf) < (stride * h)) {
3806 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
3810 tmp = g_malloc (stride);
3812 for (y = 0; y < h / 2; y++) {
3813 swap_line (GST_BUFFER_DATA (buf) + stride * y,
3814 GST_BUFFER_DATA (buf) + stride * (h - 1 - y), tmp, stride);
3823 gst_avi_demux_add_assoc (GstAviDemux * avi, GstAviStream * stream,
3824 GstClockTime timestamp, guint64 offset, gboolean keyframe)
3826 /* do not add indefinitely for open-ended streaming */
3827 if (G_UNLIKELY (avi->element_index && avi->seekable)) {
3828 GST_LOG_OBJECT (avi, "adding association %" GST_TIME_FORMAT "-> %"
3829 G_GUINT64_FORMAT, GST_TIME_ARGS (timestamp), offset);
3830 gst_index_add_association (avi->element_index, avi->index_id,
3831 keyframe ? GST_ASSOCIATION_FLAG_KEY_UNIT : GST_ASSOCIATION_FLAG_NONE,
3832 GST_FORMAT_TIME, timestamp, GST_FORMAT_BYTES, offset, NULL);
3833 /* well, current_total determines TIME and entry DEFAULT (frame #) ... */
3834 gst_index_add_association (avi->element_index, stream->index_id,
3835 GST_ASSOCIATION_FLAG_NONE,
3836 GST_FORMAT_TIME, stream->current_total, GST_FORMAT_BYTES, offset,
3837 GST_FORMAT_DEFAULT, stream->current_entry, NULL);
3842 * Returns the aggregated GstFlowReturn.
3844 static GstFlowReturn
3845 gst_avi_demux_combine_flows (GstAviDemux * avi, GstAviStream * stream,
3849 gboolean unexpected = FALSE, not_linked = TRUE;
3851 /* store the value */
3852 stream->last_flow = ret;
3854 /* any other error that is not-linked or eos can be returned right away */
3855 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
3858 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3859 for (i = 0; i < avi->num_streams; i++) {
3860 GstAviStream *ostream = &avi->stream[i];
3862 ret = ostream->last_flow;
3863 /* no unexpected or unlinked, return */
3864 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
3867 /* we check to see if we have at least 1 unexpected or all unlinked */
3868 unexpected |= (ret == GST_FLOW_UNEXPECTED);
3869 not_linked &= (ret == GST_FLOW_NOT_LINKED);
3871 /* when we get here, we all have unlinked or unexpected */
3873 ret = GST_FLOW_NOT_LINKED;
3874 else if (unexpected)
3875 ret = GST_FLOW_UNEXPECTED;
3877 GST_LOG_OBJECT (avi, "combined %s to return %s",
3878 gst_flow_get_name (stream->last_flow), gst_flow_get_name (ret));
3882 /* move @stream to the next position in its index */
3883 static GstFlowReturn
3884 gst_avi_demux_advance (GstAviDemux * avi, GstAviStream * stream,
3887 guint old_entry, new_entry;
3889 old_entry = stream->current_entry;
3891 new_entry = old_entry + 1;
3893 /* see if we reached the end */
3894 if (new_entry >= stream->stop_entry) {
3895 if (avi->segment.rate < 0.0) {
3896 if (stream->step_entry == stream->start_entry) {
3897 /* we stepped all the way to the start, eos */
3898 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
3901 /* backwards, stop becomes step, find a new step */
3902 stream->stop_entry = stream->step_entry;
3903 stream->step_entry = gst_avi_demux_index_prev (avi, stream,
3904 stream->stop_entry, TRUE);
3906 GST_DEBUG_OBJECT (avi,
3907 "reverse playback jump: start %u, step %u, stop %u",
3908 stream->start_entry, stream->step_entry, stream->stop_entry);
3910 /* and start from the previous keyframe now */
3911 new_entry = stream->step_entry;
3914 GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
3919 if (new_entry != old_entry) {
3920 stream->current_entry = new_entry;
3921 stream->current_total = stream->index[new_entry].total;
3923 if (new_entry == old_entry + 1) {
3924 GST_DEBUG_OBJECT (avi, "moved forwards from %u to %u",
3925 old_entry, new_entry);
3926 /* we simply moved one step forwards, reuse current info */
3927 stream->current_timestamp = stream->current_ts_end;
3928 stream->current_offset = stream->current_offset_end;
3929 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
3930 NULL, &stream->current_ts_end, NULL, &stream->current_offset_end);
3932 /* we moved DISCONT, full update */
3933 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
3934 &stream->current_timestamp, &stream->current_ts_end,
3935 &stream->current_offset, &stream->current_offset_end);
3936 /* and MARK discont for this stream */
3937 stream->last_flow = GST_FLOW_OK;
3938 stream->discont = TRUE;
3939 GST_DEBUG_OBJECT (avi, "Moved from %u to %u, ts %" GST_TIME_FORMAT
3940 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
3941 ", off_end %" G_GUINT64_FORMAT, old_entry, new_entry,
3942 GST_TIME_ARGS (stream->current_timestamp),
3943 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
3944 stream->current_offset_end);
3952 GST_DEBUG_OBJECT (avi, "we are EOS");
3953 /* setting current_timestamp to -1 marks EOS */
3954 stream->current_timestamp = -1;
3955 return GST_FLOW_UNEXPECTED;
3959 /* find the stream with the lowest current position when going forwards or with
3960 * the highest position when going backwards, this is the stream
3961 * we should push from next */
3963 gst_avi_demux_find_next (GstAviDemux * avi, gfloat rate)
3965 guint64 min_time, max_time;
3966 guint stream_num, i;
3969 min_time = G_MAXUINT64;
3972 for (i = 0; i < avi->num_streams; i++) {
3974 GstAviStream *stream;
3976 stream = &avi->stream[i];
3977 position = stream->current_timestamp;
3979 /* position of -1 is EOS */
3980 if (position != -1) {
3981 if (rate > 0.0 && position < min_time) {
3982 min_time = position;
3984 } else if (rate < 0.0 && position >= max_time) {
3985 max_time = position;
3993 static GstFlowReturn
3994 gst_avi_demux_loop_data (GstAviDemux * avi)
3996 GstFlowReturn ret = GST_FLOW_OK;
3998 GstAviStream *stream;
3999 gboolean processed = FALSE;
4001 guint64 offset, size;
4002 GstClockTime timestamp, duration;
4003 guint64 out_offset, out_offset_end;
4005 GstAviIndexEntry *entry;
4008 stream_num = gst_avi_demux_find_next (avi, avi->segment.rate);
4011 if (G_UNLIKELY (stream_num == -1)) {
4012 GST_DEBUG_OBJECT (avi, "all streams are EOS");
4016 /* we have the stream now */
4017 stream = &avi->stream[stream_num];
4019 /* skip streams without pads */
4021 GST_DEBUG_OBJECT (avi, "skipping entry from stream %d without pad",
4026 /* get the timing info for the entry */
4027 timestamp = stream->current_timestamp;
4028 duration = stream->current_ts_end - timestamp;
4029 out_offset = stream->current_offset;
4030 out_offset_end = stream->current_offset_end;
4032 /* get the entry data info */
4033 entry = &stream->index[stream->current_entry];
4034 offset = entry->offset;
4036 keyframe = ENTRY_IS_KEYFRAME (entry);
4038 /* skip empty entries */
4040 GST_DEBUG_OBJECT (avi, "Skipping entry %u (%" G_GUINT64_FORMAT ", %p)",
4041 stream->current_entry, size, stream->pad);
4045 if (avi->segment.rate > 0.0) {
4046 /* only check this for fowards playback for now */
4047 if (keyframe && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
4048 && (timestamp > avi->segment.stop)) {
4053 GST_LOG ("reading buffer (size=%" G_GUINT64_FORMAT "), stream %d, pos %"
4054 G_GUINT64_FORMAT " (0x%" G_GINT64_MODIFIER "x), kf %d", size,
4055 stream_num, offset, offset, keyframe);
4057 /* FIXME, check large chunks and cut them up */
4059 /* pull in the data */
4060 ret = gst_pad_pull_range (avi->sinkpad, offset, size, &buf);
4061 if (ret != GST_FLOW_OK)
4064 /* check for short buffers, this is EOS as well */
4065 if (GST_BUFFER_SIZE (buf) < size)
4068 /* invert the picture if needed */
4069 buf = gst_avi_demux_invert (stream, buf);
4071 /* mark non-keyframes */
4073 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4075 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4077 GST_BUFFER_TIMESTAMP (buf) = timestamp;
4078 GST_BUFFER_DURATION (buf) = duration;
4079 GST_BUFFER_OFFSET (buf) = out_offset;
4080 GST_BUFFER_OFFSET_END (buf) = out_offset_end;
4082 /* mark discont when pending */
4083 if (stream->discont) {
4084 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4085 stream->discont = FALSE;
4088 gst_avi_demux_add_assoc (avi, stream, timestamp, offset, keyframe);
4090 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4092 /* update current position in the segment */
4093 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, timestamp);
4095 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %u, ts %"
4096 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4097 ", off_end %" G_GUINT64_FORMAT,
4098 GST_BUFFER_SIZE (buf), GST_TIME_ARGS (timestamp),
4099 GST_TIME_ARGS (duration), out_offset, out_offset_end);
4101 ret = gst_pad_push (stream->pad, buf);
4103 /* mark as processed, we increment the frame and byte counters then
4104 * leave the while loop and return the GstFlowReturn */
4107 if (avi->segment.rate < 0) {
4108 if (timestamp > avi->segment.stop && ret == GST_FLOW_UNEXPECTED) {
4109 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
4110 * we are at the end of the segment, so we just need to jump
4111 * back to the previous section. */
4112 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
4117 /* move to next item */
4118 ret = gst_avi_demux_advance (avi, stream, ret);
4121 ret = gst_avi_demux_combine_flows (avi, stream, ret);
4122 } while (!processed);
4130 GST_DEBUG_OBJECT (avi, "No samples left for any streams - EOS");
4131 ret = GST_FLOW_UNEXPECTED;
4136 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
4137 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
4138 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (avi->segment.stop));
4139 ret = GST_FLOW_UNEXPECTED;
4144 GST_DEBUG_OBJECT (avi, "pull range failed: pos=%" G_GUINT64_FORMAT
4145 " size=%" G_GUINT64_FORMAT, offset, size);
4150 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
4151 ", only got %d/%" G_GUINT64_FORMAT " bytes (truncated file?)", offset,
4152 GST_BUFFER_SIZE (buf), size);
4153 gst_buffer_unref (buf);
4154 ret = GST_FLOW_UNEXPECTED;
4160 * Read data. If we have an index it delegates to
4161 * gst_avi_demux_process_next_entry().
4163 static GstFlowReturn
4164 gst_avi_demux_stream_data (GstAviDemux * avi)
4169 GstFlowReturn res = GST_FLOW_OK;
4170 GstFormat format = GST_FORMAT_TIME;
4172 if (G_UNLIKELY (avi->have_eos)) {
4173 /* Clean adapter, we're done */
4174 gst_adapter_clear (avi->adapter);
4175 return GST_FLOW_UNEXPECTED;
4178 if (G_UNLIKELY (avi->todrop)) {
4181 if ((drop = gst_adapter_available (avi->adapter))) {
4182 if (drop > avi->todrop)
4184 GST_DEBUG_OBJECT (avi, "Dropping %d bytes", drop);
4185 gst_adapter_flush (avi->adapter, drop);
4186 avi->todrop -= drop;
4187 avi->offset += drop;
4191 /* Iterate until need more data, so adapter won't grow too much */
4193 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
4197 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
4198 GST_FOURCC_ARGS (tag), size);
4200 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
4201 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
4202 GST_LOG ("Chunk ok");
4203 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
4204 GST_DEBUG ("Found sub-index tag");
4205 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4206 /* accept 0 size buffer here */
4207 avi->abort_buffering = FALSE;
4208 GST_DEBUG (" skipping %d bytes for now", size);
4209 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4212 } else if (tag == GST_RIFF_TAG_idx1) {
4213 GST_DEBUG ("Found index tag, stream done");
4214 avi->have_eos = TRUE;
4215 return GST_FLOW_UNEXPECTED;
4216 } else if (tag == GST_RIFF_TAG_LIST) {
4217 /* movi chunks might be grouped in rec list */
4218 if (gst_adapter_available (avi->adapter) >= 12) {
4219 GST_DEBUG ("Found LIST tag, skipping LIST header");
4220 gst_adapter_flush (avi->adapter, 12);
4224 } else if (tag == GST_RIFF_TAG_JUNK) {
4225 /* rec list might contain JUNK chunks */
4226 GST_DEBUG ("Found JUNK tag");
4227 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4228 /* accept 0 size buffer here */
4229 avi->abort_buffering = FALSE;
4230 GST_DEBUG (" skipping %d bytes for now", size);
4231 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4235 GST_DEBUG ("No more stream chunks, send EOS");
4236 avi->have_eos = TRUE;
4237 return GST_FLOW_UNEXPECTED;
4240 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
4241 /* supposedly one hopes to catch a nicer chunk later on ... */
4242 /* FIXME ?? give up here rather than possibly ending up going
4243 * through the whole file */
4244 if (avi->abort_buffering) {
4245 avi->abort_buffering = FALSE;
4246 gst_adapter_flush (avi->adapter, 8);
4250 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
4251 GST_FOURCC_ARGS (tag), size);
4253 stream_nr = CHUNKID_TO_STREAMNR (tag);
4255 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
4257 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
4258 stream_nr, GST_FOURCC_ARGS (tag));
4259 avi->offset += 8 + GST_ROUND_UP_2 (size);
4260 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4262 GstAviStream *stream;
4263 GstClockTime next_ts = 0;
4267 gst_adapter_flush (avi->adapter, 8);
4270 buf = gst_adapter_take_buffer (avi->adapter, GST_ROUND_UP_2 (size));
4271 /* patch the size */
4272 GST_BUFFER_SIZE (buf) = size;
4273 offset = avi->offset;
4274 avi->offset += 8 + GST_ROUND_UP_2 (size);
4276 stream = &avi->stream[stream_nr];
4278 /* set delay (if any)
4279 if (stream->strh->init_frames == stream->current_frame &&
4281 stream->delay = next_ts;
4284 /* parsing of corresponding header may have failed */
4285 if (G_UNLIKELY (!stream->pad)) {
4286 GST_WARNING_OBJECT (avi, "no pad for stream ID %" GST_FOURCC_FORMAT,
4287 GST_FOURCC_ARGS (tag));
4288 gst_buffer_unref (buf);
4290 GstClockTime dur_ts = 0;
4292 /* get time of this buffer */
4293 gst_pad_query_position (stream->pad, &format, (gint64 *) & next_ts);
4294 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4297 gst_avi_demux_add_assoc (avi, stream, next_ts, offset, FALSE);
4299 /* increment our positions */
4300 stream->current_entry++;
4301 stream->current_total += size;
4303 /* invert the picture if needed */
4304 buf = gst_avi_demux_invert (stream, buf);
4306 gst_pad_query_position (stream->pad, &format, (gint64 *) & dur_ts);
4307 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4310 GST_BUFFER_TIMESTAMP (buf) = next_ts;
4311 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
4312 if (stream->strh->type == GST_RIFF_FCC_vids) {
4313 GST_BUFFER_OFFSET (buf) = stream->current_entry - 1;
4314 GST_BUFFER_OFFSET_END (buf) = stream->current_entry;
4316 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4317 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
4320 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4321 GST_DEBUG_OBJECT (avi,
4322 "Pushing buffer with time=%" GST_TIME_FORMAT ", duration %"
4323 GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
4324 " and size %d over pad %s", GST_TIME_ARGS (next_ts),
4325 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), GST_BUFFER_OFFSET (buf),
4326 size, GST_PAD_NAME (stream->pad));
4328 /* update current position in the segment */
4329 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, next_ts);
4331 /* mark discont when pending */
4332 if (G_UNLIKELY (stream->discont)) {
4333 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4334 stream->discont = FALSE;
4336 res = gst_pad_push (stream->pad, buf);
4339 res = gst_avi_demux_combine_flows (avi, stream, res);
4340 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4341 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
4354 GST_DEBUG_OBJECT (avi, "format %s != GST_FORMAT_TIME",
4355 gst_format_get_name (format));
4356 res = GST_FLOW_ERROR;
4362 * Send pending tags.
4365 push_tag_lists (GstAviDemux * avi)
4373 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
4375 for (i = 0; i < avi->num_streams; i++) {
4376 GstAviStream *stream = &avi->stream[i];
4377 GstPad *pad = stream->pad;
4379 tags = stream->taglist;
4382 GST_DEBUG_OBJECT (pad, "Tags: %" GST_PTR_FORMAT, tags);
4384 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (avi), pad, tags);
4385 stream->taglist = NULL;
4389 if (!(tags = avi->globaltags))
4390 tags = gst_tag_list_new ();
4392 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
4393 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
4395 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, tags);
4396 gst_element_found_tags (GST_ELEMENT_CAST (avi), tags);
4397 avi->globaltags = NULL;
4398 avi->got_tags = FALSE;
4402 gst_avi_demux_loop (GstPad * pad)
4405 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4407 switch (avi->state) {
4408 case GST_AVI_DEMUX_START:
4409 res = gst_avi_demux_stream_init_pull (avi);
4410 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4411 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4414 avi->state = GST_AVI_DEMUX_HEADER;
4416 case GST_AVI_DEMUX_HEADER:
4417 res = gst_avi_demux_stream_header_pull (avi);
4418 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4419 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4422 avi->state = GST_AVI_DEMUX_MOVI;
4424 case GST_AVI_DEMUX_MOVI:
4425 if (G_UNLIKELY (avi->seek_event)) {
4426 gst_avi_demux_push_event (avi, avi->seek_event);
4427 avi->seek_event = NULL;
4429 if (G_UNLIKELY (avi->got_tags)) {
4430 push_tag_lists (avi);
4432 /* process each index entry in turn */
4433 res = gst_avi_demux_loop_data (avi);
4435 /* pause when error */
4436 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4437 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
4442 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
4443 res = GST_FLOW_ERROR;
4451 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
4452 avi->segment_running = FALSE;
4453 gst_pad_pause_task (avi->sinkpad);
4455 if (GST_FLOW_IS_FATAL (res) || (res == GST_FLOW_NOT_LINKED)) {
4456 gboolean push_eos = TRUE;
4458 if (res == GST_FLOW_UNEXPECTED) {
4459 /* handle end-of-stream/segment */
4460 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4463 if ((stop = avi->segment.stop) == -1)
4464 stop = avi->segment.duration;
4466 GST_INFO_OBJECT (avi, "sending segment_done");
4468 gst_element_post_message
4470 gst_message_new_segment_done (GST_OBJECT (avi), GST_FORMAT_TIME,
4475 /* for fatal errors we post an error message */
4476 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
4477 (_("Internal data stream error.")),
4478 ("streaming stopped, reason %s", gst_flow_get_name (res)));
4481 GST_INFO_OBJECT (avi, "sending eos");
4482 if (!gst_avi_demux_push_event (avi, gst_event_new_eos ()) &&
4483 (res == GST_FLOW_UNEXPECTED)) {
4484 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
4485 (NULL), ("got eos but no streams (yet)"));
4492 static GstFlowReturn
4493 gst_avi_demux_chain (GstPad * pad, GstBuffer * buf)
4496 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4498 GST_DEBUG ("Store %d bytes in adapter", GST_BUFFER_SIZE (buf));
4499 gst_adapter_push (avi->adapter, buf);
4501 switch (avi->state) {
4502 case GST_AVI_DEMUX_START:
4503 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
4504 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4508 case GST_AVI_DEMUX_HEADER:
4509 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
4510 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4514 case GST_AVI_DEMUX_MOVI:
4515 if (G_UNLIKELY (avi->seek_event)) {
4516 gst_avi_demux_push_event (avi, avi->seek_event);
4517 avi->seek_event = NULL;
4519 if (G_UNLIKELY (avi->got_tags)) {
4520 push_tag_lists (avi);
4522 res = gst_avi_demux_stream_data (avi);
4525 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
4526 ("Illegal internal state"));
4527 res = GST_FLOW_ERROR;
4531 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
4532 gst_flow_get_name (res));
4534 if (G_UNLIKELY (avi->abort_buffering)) {
4535 avi->abort_buffering = FALSE;
4536 res = GST_FLOW_ERROR;
4537 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
4544 gst_avi_demux_sink_activate (GstPad * sinkpad)
4546 if (gst_pad_check_pull_range (sinkpad)) {
4547 GST_DEBUG ("going to pull mode");
4548 return gst_pad_activate_pull (sinkpad, TRUE);
4550 GST_DEBUG ("going to push (streaming) mode");
4551 return gst_pad_activate_push (sinkpad, TRUE);
4556 gst_avi_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
4558 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (sinkpad));
4561 avi->segment_running = TRUE;
4562 avi->streaming = FALSE;
4563 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4566 avi->segment_running = FALSE;
4567 return gst_pad_stop_task (sinkpad);
4572 gst_avi_demux_activate_push (GstPad * pad, gboolean active)
4574 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (pad));
4577 GST_DEBUG ("avi: activating push/chain function");
4578 avi->streaming = TRUE;
4580 /* create index for some push based seeking if not provided */
4581 GST_OBJECT_LOCK (avi);
4582 if (!avi->element_index) {
4583 GST_DEBUG_OBJECT (avi, "creating index");
4584 avi->element_index = gst_index_factory_make ("memindex");
4586 GST_OBJECT_UNLOCK (avi);
4587 /* object lock might be taken again */
4588 gst_index_get_writer_id (avi->element_index, GST_OBJECT (avi),
4592 GST_DEBUG ("avi: deactivating push/chain function");
4599 gst_avi_demux_set_index (GstElement * element, GstIndex * index)
4601 GstAviDemux *avi = GST_AVI_DEMUX (element);
4603 GST_OBJECT_LOCK (avi);
4604 if (avi->element_index)
4605 gst_object_unref (avi->element_index);
4607 avi->element_index = gst_object_ref (index);
4609 avi->element_index = NULL;
4611 GST_OBJECT_UNLOCK (avi);
4612 /* object lock might be taken again */
4614 gst_index_get_writer_id (index, GST_OBJECT (element), &avi->index_id);
4615 GST_DEBUG_OBJECT (avi, "Set index %" GST_PTR_FORMAT, avi->element_index);
4619 gst_avi_demux_get_index (GstElement * element)
4621 GstIndex *result = NULL;
4622 GstAviDemux *avi = GST_AVI_DEMUX (element);
4624 GST_OBJECT_LOCK (avi);
4625 if (avi->element_index)
4626 result = gst_object_ref (avi->element_index);
4627 GST_OBJECT_UNLOCK (avi);
4629 GST_DEBUG_OBJECT (avi, "Returning index %" GST_PTR_FORMAT, result);
4634 static GstStateChangeReturn
4635 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
4637 GstStateChangeReturn ret;
4638 GstAviDemux *avi = GST_AVI_DEMUX (element);
4640 switch (transition) {
4641 case GST_STATE_CHANGE_READY_TO_PAUSED:
4642 avi->streaming = FALSE;
4643 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
4649 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4650 if (ret == GST_STATE_CHANGE_FAILURE)
4653 switch (transition) {
4654 case GST_STATE_CHANGE_PAUSED_TO_READY:
4655 gst_avi_demux_reset (avi);