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)
50 #include "gst/riff/riff-media.h"
51 #include "gstavidemux.h"
53 #include <gst/gst-i18n-plugin.h>
54 #include <gst/base/gstadapter.h>
57 #define DIV_ROUND_UP(s,v) (((s) + ((v)-1)) / (v))
59 GST_DEBUG_CATEGORY_STATIC (avidemux_debug);
60 #define GST_CAT_DEFAULT avidemux_debug
62 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
65 GST_STATIC_CAPS ("video/x-msvideo")
68 static void gst_avi_demux_base_init (GstAviDemuxClass * klass);
69 static void gst_avi_demux_class_init (GstAviDemuxClass * klass);
70 static void gst_avi_demux_init (GstAviDemux * avi);
71 static void gst_avi_demux_finalize (GObject * object);
73 static void gst_avi_demux_reset (GstAviDemux * avi);
76 static const GstEventMask *gst_avi_demux_get_event_mask (GstPad * pad);
78 static gboolean gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event);
79 static gboolean gst_avi_demux_handle_sink_event (GstPad * pad,
81 static gboolean gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event);
84 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
86 static const GstQueryType *gst_avi_demux_get_src_query_types (GstPad * pad);
87 static gboolean gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query);
88 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
89 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
91 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
92 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
94 static gboolean gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
96 static void gst_avi_demux_loop (GstPad * pad);
97 static gboolean gst_avi_demux_sink_activate (GstPad * sinkpad);
98 static gboolean gst_avi_demux_sink_activate_pull (GstPad * sinkpad,
100 static gboolean gst_avi_demux_activate_push (GstPad * pad, gboolean active);
101 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstBuffer * buf);
103 static void gst_avi_demux_set_index (GstElement * element, GstIndex * index);
104 static GstIndex *gst_avi_demux_get_index (GstElement * element);
105 static GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
106 GstStateChange transition);
108 static void gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf);
110 static GstElementClass *parent_class = NULL;
112 /* GObject methods */
115 gst_avi_demux_get_type (void)
117 static GType avi_demux_type = 0;
119 if (!avi_demux_type) {
120 static const GTypeInfo avi_demux_info = {
121 sizeof (GstAviDemuxClass),
122 (GBaseInitFunc) gst_avi_demux_base_init,
124 (GClassInitFunc) gst_avi_demux_class_init,
127 sizeof (GstAviDemux),
129 (GInstanceInitFunc) gst_avi_demux_init,
133 g_type_register_static (GST_TYPE_ELEMENT,
134 "GstAviDemux", &avi_demux_info, 0);
137 return avi_demux_type;
141 gst_avi_demux_base_init (GstAviDemuxClass * klass)
143 static const GstElementDetails gst_avi_demux_details =
144 GST_ELEMENT_DETAILS ("Avi demuxer",
146 "Demultiplex an avi file into audio and video",
147 "Erik Walthinsen <omega@cse.ogi.edu>\n"
148 "Wim Taymans <wim.taymans@chello.be>\n"
149 "Thijs Vermeir <thijsvermeir@gmail.com>");
150 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
151 GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl;
152 GstCaps *audcaps, *vidcaps, *subcaps;
154 audcaps = gst_riff_create_audio_template_caps ();
155 gst_caps_append (audcaps, gst_caps_new_simple ("audio/x-avi-unknown", NULL));
156 audiosrctempl = gst_pad_template_new ("audio_%02d",
157 GST_PAD_SRC, GST_PAD_SOMETIMES, audcaps);
159 vidcaps = gst_riff_create_video_template_caps ();
160 gst_caps_append (vidcaps, gst_riff_create_iavs_template_caps ());
161 gst_caps_append (vidcaps, gst_caps_new_simple ("video/x-avi-unknown", NULL));
162 videosrctempl = gst_pad_template_new ("video_%02d",
163 GST_PAD_SRC, GST_PAD_SOMETIMES, vidcaps);
165 subcaps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
166 subsrctempl = gst_pad_template_new ("subtitle_%02d",
167 GST_PAD_SRC, GST_PAD_SOMETIMES, subcaps);
168 gst_element_class_add_pad_template (element_class, audiosrctempl);
169 gst_element_class_add_pad_template (element_class, videosrctempl);
170 gst_element_class_add_pad_template (element_class, subsrctempl);
171 gst_element_class_add_pad_template (element_class,
172 gst_static_pad_template_get (&sink_templ));
173 gst_element_class_set_details (element_class, &gst_avi_demux_details);
177 gst_avi_demux_class_init (GstAviDemuxClass * klass)
179 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
180 GObjectClass *gobject_class = (GObjectClass *) klass;
182 GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
183 0, "Demuxer for AVI streams");
185 parent_class = g_type_class_peek_parent (klass);
187 gobject_class->finalize = gst_avi_demux_finalize;
188 gstelement_class->change_state =
189 GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
191 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_avi_demux_set_index);
192 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_avi_demux_get_index);
196 gst_avi_demux_init (GstAviDemux * avi)
198 avi->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
199 gst_pad_set_activate_function (avi->sinkpad,
200 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate));
201 gst_pad_set_activatepull_function (avi->sinkpad,
202 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate_pull));
203 gst_pad_set_activatepush_function (avi->sinkpad,
204 GST_DEBUG_FUNCPTR (gst_avi_demux_activate_push));
205 gst_pad_set_chain_function (avi->sinkpad,
206 GST_DEBUG_FUNCPTR (gst_avi_demux_chain));
207 gst_pad_set_event_function (avi->sinkpad,
208 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_sink_event));
209 gst_element_add_pad (GST_ELEMENT_CAST (avi), avi->sinkpad);
211 avi->adapter = gst_adapter_new ();
213 gst_avi_demux_reset (avi);
217 gst_avi_demux_finalize (GObject * object)
219 GstAviDemux *avi = GST_AVI_DEMUX (object);
221 GST_DEBUG ("AVI: finalize");
223 g_object_unref (avi->adapter);
225 G_OBJECT_CLASS (parent_class)->finalize (object);
229 gst_avi_demux_reset_stream (GstAviDemux * avi, GstAviStream * stream)
231 g_free (stream->strh);
232 g_free (stream->strf.data);
233 g_free (stream->name);
234 g_free (stream->index);
235 g_free (stream->indexes);
236 if (stream->initdata)
237 gst_buffer_unref (stream->initdata);
238 if (stream->extradata)
239 gst_buffer_unref (stream->extradata);
241 if (stream->exposed) {
242 gst_pad_set_active (stream->pad, FALSE);
243 gst_element_remove_pad (GST_ELEMENT_CAST (avi), stream->pad);
245 gst_object_unref (stream->pad);
247 if (stream->taglist) {
248 gst_tag_list_free (stream->taglist);
249 stream->taglist = NULL;
251 memset (stream, 0, sizeof (GstAviStream));
255 gst_avi_demux_reset (GstAviDemux * avi)
259 GST_DEBUG ("AVI: reset");
261 for (i = 0; i < avi->num_streams; i++)
262 gst_avi_demux_reset_stream (avi, &avi->stream[i]);
264 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
265 avi->num_streams = 0;
266 avi->num_v_streams = 0;
267 avi->num_a_streams = 0;
268 avi->num_t_streams = 0;
269 avi->main_stream = -1;
271 avi->state = GST_AVI_DEMUX_START;
274 avi->index_offset = 0;
278 if (avi->element_index)
279 gst_object_unref (avi->element_index);
280 avi->element_index = NULL;
282 if (avi->seg_event) {
283 gst_event_unref (avi->seg_event);
284 avi->seg_event = NULL;
288 gst_tag_list_free (avi->globaltags);
289 avi->globaltags = NULL;
291 avi->got_tags = TRUE; /* we always want to push global tags */
292 avi->have_eos = FALSE;
293 avi->seekable = TRUE;
295 gst_adapter_clear (avi->adapter);
297 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
301 /* GstElement methods */
304 static const GstFormat *
305 gst_avi_demux_get_src_formats (GstPad * pad)
307 GstAviStream *stream = gst_pad_get_element_private (pad);
309 static const GstFormat src_a_formats[] = {
315 static const GstFormat src_v_formats[] = {
321 return (stream->strh->type == GST_RIFF_FCC_auds ?
322 src_a_formats : src_v_formats);
326 /* assumes stream->strf.auds->av_bps != 0 */
327 static inline GstClockTime
328 avi_stream_convert_bytes_to_time_unchecked (GstAviStream * stream,
331 return gst_util_uint64_scale_int (bytes, GST_SECOND,
332 stream->strf.auds->av_bps);
335 static inline guint64
336 avi_stream_convert_time_to_bytes_unchecked (GstAviStream * stream,
339 return gst_util_uint64_scale_int (time, stream->strf.auds->av_bps,
343 /* assumes stream->strh->rate != 0 */
344 static inline GstClockTime
345 avi_stream_convert_frames_to_time_unchecked (GstAviStream * stream,
348 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
352 static inline guint64
353 avi_stream_convert_time_to_frames_unchecked (GstAviStream * stream,
356 return gst_util_uint64_scale (time, stream->strh->rate,
357 stream->strh->scale * GST_SECOND);
361 gst_avi_demux_src_convert (GstPad * pad,
362 GstFormat src_format,
363 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
365 GstAviStream *stream = gst_pad_get_element_private (pad);
369 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
370 ", dest_format:%s", gst_format_get_name (src_format), src_value,
371 gst_format_get_name (*dest_format));
373 if (G_UNLIKELY (src_format == *dest_format)) {
374 *dest_value = src_value;
377 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
381 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
382 (src_format == GST_FORMAT_BYTES
383 || *dest_format == GST_FORMAT_BYTES))) {
388 switch (src_format) {
389 case GST_FORMAT_TIME:
390 switch (*dest_format) {
391 case GST_FORMAT_BYTES:
392 *dest_value = gst_util_uint64_scale_int (src_value,
393 stream->strf.auds->av_bps, GST_SECOND);
395 case GST_FORMAT_DEFAULT:
397 gst_util_uint64_scale_round (src_value, stream->strh->rate,
398 stream->strh->scale * GST_SECOND);
405 case GST_FORMAT_BYTES:
406 switch (*dest_format) {
407 case GST_FORMAT_TIME:
408 if (stream->strf.auds->av_bps != 0) {
409 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
419 case GST_FORMAT_DEFAULT:
420 switch (*dest_format) {
421 case GST_FORMAT_TIME:
423 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
436 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
437 gst_format_get_name (*dest_format), *dest_value);
441 static const GstQueryType *
442 gst_avi_demux_get_src_query_types (GstPad * pad)
444 static const GstQueryType src_types[] = {
456 gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query)
459 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
461 GstAviStream *stream = gst_pad_get_element_private (pad);
463 if (!stream->strh || !stream->strf.data)
464 return gst_pad_query_default (pad, query);
466 switch (GST_QUERY_TYPE (query)) {
467 case GST_QUERY_POSITION:{
470 GST_DEBUG ("pos query for stream %u: frames %u, bytes %u",
471 stream->num, stream->current_entry, stream->current_total);
473 /* FIXME, this looks clumsy */
474 if (stream->strh->type == GST_RIFF_FCC_auds) {
475 if (stream->is_vbr) {
477 pos = gst_util_uint64_scale ((gint64) stream->current_entry *
478 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
479 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
480 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
481 } else if (stream->strf.auds->av_bps != 0) {
483 pos = gst_util_uint64_scale (stream->current_total, GST_SECOND,
484 (guint64) stream->strf.auds->av_bps);
485 GST_DEBUG_OBJECT (avi,
486 "CBR convert bytes %u, time %" GST_TIME_FORMAT,
487 stream->current_total, GST_TIME_ARGS (pos));
488 } else if (stream->idx_n != 0 && stream->total_bytes != 0) {
489 /* calculate timestamps based on percentage of length */
490 guint64 xlen = avi->avih->us_frame *
491 avi->avih->tot_frames * GST_USECOND;
493 if (stream->is_vbr) {
494 pos = gst_util_uint64_scale (xlen, stream->current_entry,
496 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
497 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
499 pos = gst_util_uint64_scale (xlen, stream->current_total,
500 stream->total_bytes);
501 GST_DEBUG_OBJECT (avi,
502 "CBR perc convert bytes %u, time %" GST_TIME_FORMAT,
503 stream->current_total, GST_TIME_ARGS (pos));
510 if (stream->strh->rate != 0) {
511 pos = gst_util_uint64_scale ((guint64) stream->current_entry *
512 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
514 pos = stream->current_entry * avi->avih->us_frame * GST_USECOND;
518 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
519 gst_query_set_position (query, GST_FORMAT_TIME, pos);
521 GST_WARNING ("pos query failed");
524 case GST_QUERY_DURATION:
527 GstClockTime duration;
529 /* only act on audio or video streams */
530 if (stream->strh->type != GST_RIFF_FCC_auds &&
531 stream->strh->type != GST_RIFF_FCC_vids) {
536 /* take stream duration, fall back to avih duration */
537 if ((duration = stream->duration) == -1)
538 duration = avi->duration;
540 gst_query_parse_duration (query, &fmt, NULL);
543 case GST_FORMAT_TIME:
544 gst_query_set_duration (query, fmt, duration);
546 case GST_FORMAT_DEFAULT:
549 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
552 if (stream->idx_n >= 0)
553 gst_query_set_duration (query, fmt, stream->idx_n);
554 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
555 duration, &fmt, &dur))
556 gst_query_set_duration (query, fmt, dur);
565 case GST_QUERY_SEEKING:{
568 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
569 if (fmt == GST_FORMAT_TIME) {
570 gboolean seekable = TRUE;
572 if (avi->streaming) {
573 seekable = avi->seekable;
576 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
577 0, stream->duration);
582 case GST_QUERY_CONVERT:{
583 GstFormat src_fmt, dest_fmt;
584 gint64 src_val, dest_val;
586 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
587 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
589 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
591 res = gst_pad_query_default (pad, query);
595 res = gst_pad_query_default (pad, query);
599 gst_object_unref (avi);
604 static const GstEventMask *
605 gst_avi_demux_get_event_mask (GstPad * pad)
607 static const GstEventMask masks[] = {
608 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
617 gst_avi_demux_seek_streams (GstAviDemux * avi, guint64 offset, gboolean before)
619 GstAviStream *stream;
620 GstIndexEntry *entry;
622 gint64 val, min = offset;
624 for (i = 0; i < avi->num_streams; i++) {
625 stream = &avi->stream[i];
627 entry = gst_index_get_assoc_entry (avi->element_index, stream->index_id,
628 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
629 GST_ASSOCIATION_FLAG_NONE, GST_FORMAT_BYTES, offset);
633 GST_DEBUG_OBJECT (avi, "stream %d, previous entry at %"
634 G_GUINT64_FORMAT, i, val);
635 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
643 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
644 stream->current_entry = 0;
645 stream->current_total = 0;
649 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
650 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT,
653 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &val);
654 stream->current_total = val;
655 gst_index_entry_assoc_map (entry, GST_FORMAT_DEFAULT, &val);
656 stream->current_entry = val;
662 #define GST_AVI_SEEK_PUSH_DISPLACE (4 * GST_SECOND)
665 gst_avi_demux_handle_sink_event (GstPad * pad, GstEvent * event)
668 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
670 GST_DEBUG_OBJECT (avi,
671 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
673 switch (GST_EVENT_TYPE (event)) {
674 case GST_EVENT_NEWSEGMENT:
678 gint64 start, stop, time, offset = 0;
681 GstIndexEntry *entry;
683 /* some debug output */
684 gst_segment_init (&segment, GST_FORMAT_UNDEFINED);
685 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
686 &start, &stop, &time);
687 gst_segment_set_newsegment_full (&segment, update, rate, arate, format,
689 GST_DEBUG_OBJECT (avi,
690 "received format %d newsegment %" GST_SEGMENT_FORMAT, format,
693 /* chain will send initial newsegment after pads have been added */
694 if (avi->state != GST_AVI_DEMUX_MOVI) {
695 GST_DEBUG_OBJECT (avi, "still starting, eating event");
699 /* we only expect a BYTE segment, e.g. following a seek */
700 if (format != GST_FORMAT_BYTES) {
701 GST_DEBUG_OBJECT (avi, "unsupported segment format, ignoring");
705 if (!avi->element_index) {
706 GST_WARNING_OBJECT (avi, "no index data, forcing EOS");
710 /* Let's check if we have an index entry for this position */
711 entry = gst_index_get_assoc_entry (avi->element_index, avi->index_id,
712 GST_INDEX_LOOKUP_AFTER, GST_ASSOCIATION_FLAG_NONE,
713 GST_FORMAT_BYTES, start);
715 /* we can not go where we have not yet been before ... */
717 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
722 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &time);
723 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &start);
724 stop = GST_CLOCK_TIME_NONE;
726 /* compensate for slack */
728 time += GST_AVI_SEEK_PUSH_DISPLACE;
730 /* set up segment and send downstream */
731 gst_segment_set_newsegment_full (&avi->segment, update, rate, arate,
732 GST_FORMAT_TIME, time, stop, time);
733 GST_DEBUG_OBJECT (avi, "Pushing newseg update %d, rate %g, "
734 "applied rate %g, format %d, start %" G_GINT64_FORMAT ", "
735 "stop %" G_GINT64_FORMAT, update, rate, arate, GST_FORMAT_TIME,
737 gst_avi_demux_push_event (avi,
738 gst_event_new_new_segment_full (update, rate, arate, GST_FORMAT_TIME,
741 GST_DEBUG_OBJECT (avi, "next chunk expected at %" G_GINT64_FORMAT, start);
743 /* adjust state for streaming thread accordingly */
744 avi->offset = offset;
745 gst_avi_demux_seek_streams (avi, offset, FALSE);
747 /* set up streaming thread */
748 avi->offset = offset;
749 avi->todrop = start - offset;
752 /* in any case, clear leftover in current segment, if any */
753 gst_adapter_clear (avi->adapter);
754 gst_event_unref (event);
759 avi->have_eos = TRUE;
764 if (avi->state != GST_AVI_DEMUX_MOVI) {
765 gst_event_unref (event);
766 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
767 (NULL), ("got eos and didn't receive a complete header object"));
768 } else if (!gst_avi_demux_push_event (avi, event)) {
769 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
770 (NULL), ("got eos but no streams (yet)"));
774 case GST_EVENT_FLUSH_STOP:
778 gst_adapter_clear (avi->adapter);
779 avi->have_eos = FALSE;
780 for (i = 0; i < avi->num_streams; i++) {
781 avi->stream[i].last_flow = GST_FLOW_OK;
782 avi->stream[i].discont = TRUE;
784 /* fall through to default case so that the event gets passed downstream */
787 res = gst_pad_event_default (pad, event);
791 gst_object_unref (avi);
797 gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event)
800 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
802 GST_DEBUG_OBJECT (avi,
803 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
805 switch (GST_EVENT_TYPE (event)) {
807 /* handle seeking only in pull mode */
808 if (!avi->streaming) {
809 res = gst_avi_demux_handle_seek (avi, pad, event);
811 res = gst_avi_demux_handle_seek_push (avi, pad, event);
813 gst_event_unref (event);
816 case GST_EVENT_NAVIGATION:
818 gst_event_unref (event);
821 res = gst_pad_event_default (pad, event);
825 gst_object_unref (avi);
830 /* streaming helper (push) */
833 * gst_avi_demux_peek_chunk_info:
835 * @tag: holder for tag
836 * @size: holder for tag size
838 * Peek next chunk info (tag and size)
840 * Returns: TRUE when one chunk info has been got
843 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
845 const guint8 *data = NULL;
847 if (gst_adapter_available (avi->adapter) < 8)
850 data = gst_adapter_peek (avi->adapter, 8);
851 *tag = GST_READ_UINT32_LE (data);
852 *size = GST_READ_UINT32_LE (data + 4);
858 * gst_avi_demux_peek_chunk:
860 * @tag: holder for tag
861 * @size: holder for tag size
863 * Peek enough data for one full chunk
865 * Returns: %TRUE when one chunk has been got
868 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
870 guint32 peek_size = 0;
873 if (!gst_avi_demux_peek_chunk_info (avi, tag, size))
876 /* size 0 -> empty data buffer would surprise most callers,
877 * large size -> do not bother trying to squeeze that into adapter,
878 * so we throw poor man's exception, which can be caught if caller really
879 * wants to handle 0 size chunk */
880 if (!(*size) || (*size) >= (1 << 30))
883 peek_size = (*size + 1) & ~1;
884 available = gst_adapter_available (avi->adapter);
886 GST_DEBUG_OBJECT (avi,
887 "Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
888 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
890 if (available < (8 + peek_size))
898 GST_INFO_OBJECT (avi, "Failed to peek");
903 GST_INFO_OBJECT (avi,
904 "Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT, *size,
905 GST_FOURCC_ARGS (*tag));
906 /* chain should give up */
907 avi->abort_buffering = TRUE;
912 GST_INFO_OBJECT (avi, "need more %d < %" G_GUINT32_FORMAT,
913 available, 8 + peek_size);
921 * gst_avi_demux_parse_file_header:
922 * @element: caller element (used for errors/debug).
923 * @buf: input data to be used for parsing.
925 * "Open" a RIFF/AVI file. The buffer should be at least 12
926 * bytes long. Takes ownership of @buf.
928 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
929 * Throws an error, caller should error out (fatal).
932 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
937 stamp = gst_util_get_timestamp ();
939 /* riff_parse posts an error */
940 if (!gst_riff_parse_file_header (element, buf, &doctype))
943 if (doctype != GST_RIFF_RIFF_AVI)
946 stamp = gst_util_get_timestamp () - stamp;
947 GST_DEBUG_OBJECT (element, "header parsing took %" GST_TIME_FORMAT,
948 GST_TIME_ARGS (stamp));
955 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
956 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
957 GST_FOURCC_ARGS (doctype)));
963 * Read AVI file tag when streaming
966 gst_avi_demux_stream_init_push (GstAviDemux * avi)
968 if (gst_adapter_available (avi->adapter) >= 12) {
971 tmp = gst_adapter_take_buffer (avi->adapter, 12);
973 GST_DEBUG ("Parsing avi header");
974 if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), tmp)) {
975 return GST_FLOW_ERROR;
977 GST_DEBUG ("header ok");
980 avi->state = GST_AVI_DEMUX_HEADER;
989 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
992 GstBuffer *buf = NULL;
994 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
995 if (res != GST_FLOW_OK)
997 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
1007 GST_DEBUG_OBJECT (avi, "error parsing file header");
1008 return GST_FLOW_ERROR;
1012 /* AVI header handling */
1015 * gst_avi_demux_parse_avih:
1016 * @avi: caller element (used for errors/debug).
1017 * @buf: input data to be used for parsing.
1018 * @avih: pointer to structure (filled in by function) containing
1019 * stream information (such as flags, number of streams, etc.).
1021 * Read 'avih' header. Discards buffer after use.
1023 * Returns: TRUE on success, FALSE otherwise. Throws an error if
1024 * the header is invalid. The caller should error out
1028 gst_avi_demux_parse_avih (GstAviDemux * avi,
1029 GstBuffer * buf, gst_riff_avih ** _avih)
1031 gst_riff_avih *avih;
1036 if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_avih))
1037 goto avih_too_small;
1039 avih = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1041 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1042 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
1043 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
1044 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
1045 avih->flags = GUINT32_FROM_LE (avih->flags);
1046 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
1047 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
1048 avih->streams = GUINT32_FROM_LE (avih->streams);
1049 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
1050 avih->width = GUINT32_FROM_LE (avih->width);
1051 avih->height = GUINT32_FROM_LE (avih->height);
1052 avih->scale = GUINT32_FROM_LE (avih->scale);
1053 avih->rate = GUINT32_FROM_LE (avih->rate);
1054 avih->start = GUINT32_FROM_LE (avih->start);
1055 avih->length = GUINT32_FROM_LE (avih->length);
1059 GST_INFO_OBJECT (avi, "avih tag found:");
1060 GST_INFO_OBJECT (avi, " us_frame %u", avih->us_frame);
1061 GST_INFO_OBJECT (avi, " max_bps %u", avih->max_bps);
1062 GST_INFO_OBJECT (avi, " pad_gran %u", avih->pad_gran);
1063 GST_INFO_OBJECT (avi, " flags 0x%08x", avih->flags);
1064 GST_INFO_OBJECT (avi, " tot_frames %u", avih->tot_frames);
1065 GST_INFO_OBJECT (avi, " init_frames %u", avih->init_frames);
1066 GST_INFO_OBJECT (avi, " streams %u", avih->streams);
1067 GST_INFO_OBJECT (avi, " bufsize %u", avih->bufsize);
1068 GST_INFO_OBJECT (avi, " width %u", avih->width);
1069 GST_INFO_OBJECT (avi, " height %u", avih->height);
1070 GST_INFO_OBJECT (avi, " scale %u", avih->scale);
1071 GST_INFO_OBJECT (avi, " rate %u", avih->rate);
1072 GST_INFO_OBJECT (avi, " start %u", avih->start);
1073 GST_INFO_OBJECT (avi, " length %u", avih->length);
1076 gst_buffer_unref (buf);
1078 if (avih->us_frame != 0 && avih->tot_frames != 0)
1080 (guint64) avih->us_frame * (guint64) avih->tot_frames * 1000;
1082 avi->duration = GST_CLOCK_TIME_NONE;
1084 GST_INFO_OBJECT (avi, " header duration %" GST_TIME_FORMAT,
1085 GST_TIME_ARGS (avi->duration));
1092 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No buffer"));
1097 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
1098 ("Too small avih (%d available, %d needed)",
1099 GST_BUFFER_SIZE (buf), (int) sizeof (gst_riff_avih)));
1100 gst_buffer_unref (buf);
1106 * gst_avi_demux_parse_superindex:
1107 * @avi: caller element (used for debugging/errors).
1108 * @buf: input data to use for parsing.
1109 * @locations: locations in the file (byte-offsets) that contain
1110 * the actual indexes (see get_avi_demux_parse_subindex()).
1111 * The array ends with GST_BUFFER_OFFSET_NONE.
1113 * Reads superindex (openDML-2 spec stuff) from the provided data.
1115 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
1116 * on error, but they are not fatal.
1119 gst_avi_demux_parse_superindex (GstAviDemux * avi,
1120 GstBuffer * buf, guint64 ** _indexes)
1130 size = buf ? GST_BUFFER_SIZE (buf) : 0;
1134 data = GST_BUFFER_DATA (buf);
1136 /* check type of index. The opendml2 specs state that
1137 * there should be 4 dwords per array entry. Type can be
1138 * either frame or field (and we don't care). */
1139 if (GST_READ_UINT16_LE (data) != 4 ||
1140 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
1141 GST_WARNING_OBJECT (avi,
1142 "Superindex for stream has unexpected "
1143 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1144 GST_READ_UINT16_LE (data), data[2], data[3]);
1145 bpe = GST_READ_UINT16_LE (data) * 4;
1147 num = GST_READ_UINT32_LE (&data[4]);
1149 GST_DEBUG_OBJECT (avi, "got %d indexes", num);
1151 indexes = g_new (guint64, num + 1);
1152 for (i = 0; i < num; i++) {
1153 if (size < 24 + bpe * (i + 1))
1155 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
1156 GST_DEBUG_OBJECT (avi, "index %d at %" G_GUINT64_FORMAT, i, indexes[i]);
1158 indexes[i] = GST_BUFFER_OFFSET_NONE;
1159 *_indexes = indexes;
1161 gst_buffer_unref (buf);
1168 GST_ERROR_OBJECT (avi,
1169 "Not enough data to parse superindex (%d available, 24 needed)", size);
1171 gst_buffer_unref (buf);
1176 /* add an entry to the index of a stream. @num should be an estimate of the
1177 * total amount of index entries for all streams and is used to dynamically
1178 * allocate memory for the index entries. */
1179 static inline gboolean
1180 gst_avi_demux_add_index (GstAviDemux * avi, GstAviStream * stream,
1181 guint num, GstAviIndexEntry * entry)
1183 /* ensure index memory */
1184 if (G_UNLIKELY (stream->idx_n >= stream->idx_max)) {
1185 guint idx_max = stream->idx_max;
1186 GstAviIndexEntry *new_idx;
1188 /* we need to make some more room */
1190 /* initial size guess, assume each stream has an equal amount of entries,
1191 * overshoot with at least 8K */
1192 idx_max = (num / avi->num_streams) + (8192 / sizeof (GstAviIndexEntry));
1194 idx_max += 8192 / sizeof (GstAviIndexEntry);
1195 GST_DEBUG_OBJECT (avi, "expanded index from %u to %u",
1196 stream->idx_max, idx_max);
1198 new_idx = g_try_renew (GstAviIndexEntry, stream->index, idx_max);
1199 /* out of memory, if this fails stream->index is untouched. */
1200 if (G_UNLIKELY (!new_idx))
1203 stream->index = new_idx;
1204 stream->idx_max = idx_max;
1207 /* update entry total and stream stats. The entry total can be converted to
1208 * the timestamp of the entry easily. */
1209 if (stream->strh->type == GST_RIFF_FCC_auds) {
1212 if (stream->is_vbr) {
1213 entry->total = stream->total_blocks;
1215 entry->total = stream->total_bytes;
1217 blockalign = stream->strf.auds->blockalign;
1219 stream->total_blocks += DIV_ROUND_UP (entry->size, blockalign);
1221 stream->total_blocks++;
1223 if (stream->is_vbr) {
1224 entry->total = stream->idx_n;
1226 entry->total = stream->total_bytes;
1229 stream->total_bytes += entry->size;
1230 if (ENTRY_IS_KEYFRAME (entry))
1231 stream->n_keyframes++;
1234 GST_LOG_OBJECT (avi,
1235 "Adding stream %u, index entry %d, kf %d, size %u "
1236 ", offset %" G_GUINT64_FORMAT ", total %" G_GUINT64_FORMAT, stream->num,
1237 stream->idx_n, ENTRY_IS_KEYFRAME (entry), entry->size, entry->offset,
1239 stream->index[stream->idx_n++] = *entry;
1244 /* given @entry_n in @stream, calculate info such as timestamps and
1245 * offsets for the entry. */
1247 gst_avi_demux_get_buffer_info (GstAviDemux * avi, GstAviStream * stream,
1248 guint entry_n, GstClockTime * timestamp, GstClockTime * ts_end,
1249 guint64 * offset, guint64 * offset_end)
1251 GstAviIndexEntry *entry;
1253 entry = &stream->index[entry_n];
1255 if (stream->is_vbr) {
1256 /* VBR stream next timestamp */
1257 if (stream->strh->type == GST_RIFF_FCC_auds) {
1260 avi_stream_convert_frames_to_time_unchecked (stream, entry->total);
1262 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1267 avi_stream_convert_frames_to_time_unchecked (stream, entry_n);
1269 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1272 } else if (stream->strh->type == GST_RIFF_FCC_auds) {
1273 /* constant rate stream */
1276 avi_stream_convert_bytes_to_time_unchecked (stream, entry->total);
1278 *ts_end = avi_stream_convert_bytes_to_time_unchecked (stream,
1279 entry->total + entry->size);
1281 if (stream->strh->type == GST_RIFF_FCC_vids) {
1282 /* video offsets are the frame number */
1286 *offset_end = entry_n + 1;
1288 /* no offsets for audio */
1296 /* collect and debug stats about the indexes for all streams.
1297 * This method is also responsible for filling in the stream duration
1298 * as measured by the amount of index entries.
1300 * Returns TRUE if the index is not empty, else FALSE */
1302 gst_avi_demux_do_index_stats (GstAviDemux * avi)
1304 guint total_idx = 0;
1306 #ifndef GST_DISABLE_GST_DEBUG
1307 guint total_max = 0;
1310 /* get stream stats now */
1311 for (i = 0; i < avi->num_streams; i++) {
1312 GstAviStream *stream;
1314 if (G_UNLIKELY (!(stream = &avi->stream[i])))
1316 if (G_UNLIKELY (!stream->strh))
1318 if (G_UNLIKELY (!stream->index || stream->idx_n == 0))
1321 /* we interested in the end_ts of the last entry, which is the total
1322 * duration of this stream */
1323 gst_avi_demux_get_buffer_info (avi, stream, stream->idx_n - 1,
1324 NULL, &stream->idx_duration, NULL, NULL);
1326 total_idx += stream->idx_n;
1327 #ifndef GST_DISABLE_GST_DEBUG
1328 total_max += stream->idx_max;
1330 GST_INFO_OBJECT (avi, "Stream %d, dur %" GST_TIME_FORMAT ", %6u entries, "
1331 "%5u keyframes, entry size = %2u, total size = %10u, allocated %10u",
1332 i, GST_TIME_ARGS (stream->idx_duration), stream->idx_n,
1333 stream->n_keyframes, (guint) sizeof (GstAviIndexEntry),
1334 (guint) (stream->idx_n * sizeof (GstAviIndexEntry)),
1335 (guint) (stream->idx_max * sizeof (GstAviIndexEntry)));
1337 total_idx *= sizeof (GstAviIndexEntry);
1338 #ifndef GST_DISABLE_GST_DEBUG
1339 total_max *= sizeof (GstAviIndexEntry);
1341 GST_INFO_OBJECT (avi, "%u bytes for index vs %u ideally, %u wasted",
1342 total_max, total_idx, total_max - total_idx);
1344 if (total_idx == 0) {
1345 GST_WARNING_OBJECT (avi, "Index is empty !");
1352 * gst_avi_demux_parse_subindex:
1354 * @buf: input data to use for parsing.
1355 * @stream: stream context.
1356 * @entries_list: a list (returned by the function) containing all the
1357 * indexes parsed in this specific subindex. The first
1358 * entry is also a pointer to allocated memory that needs
1359 * to be free´ed. May be NULL if no supported indexes were
1362 * Reads superindex (openDML-2 spec stuff) from the provided data.
1363 * The buffer should contain a GST_RIFF_TAG_ix?? chunk.
1365 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1366 * throw an error, caller should bail out asap.
1369 gst_avi_demux_parse_subindex (GstAviDemux * avi, GstAviStream * stream,
1381 size = GST_BUFFER_SIZE (buf);
1387 data = GST_BUFFER_DATA (buf);
1389 /* We don't support index-data yet */
1391 goto not_implemented;
1393 /* check type of index. The opendml2 specs state that
1394 * there should be 4 dwords per array entry. Type can be
1395 * either frame or field (and we don't care). */
1396 bpe = (data[2] & 0x01) ? 12 : 8;
1397 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1398 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1399 GST_WARNING_OBJECT (avi,
1400 "Superindex for stream %d has unexpected "
1401 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1402 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1403 bpe = GST_READ_UINT16_LE (data) * 4;
1405 num = GST_READ_UINT32_LE (&data[4]);
1406 baseoff = GST_READ_UINT64_LE (&data[12]);
1408 /* If there's nothing, just return ! */
1412 GST_INFO_OBJECT (avi, "Parsing subindex, nr_entries = %6d", num);
1414 for (i = 0; i < num; i++) {
1415 GstAviIndexEntry entry;
1417 if (size < 24 + bpe * (i + 1))
1420 /* fill in offset and size. offset contains the keyframe flag in the
1422 entry.offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1423 entry.size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1425 if (stream->strh->type == GST_RIFF_FCC_auds) {
1426 /* all audio frames are keyframes */
1427 ENTRY_SET_KEYFRAME (&entry);
1429 /* else read flags */
1430 entry.flags = (entry.size & 0x80000000) ? 0 : GST_AVI_KEYFRAME;
1432 entry.size &= ~0x80000000;
1435 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
1438 gst_buffer_unref (buf);
1445 GST_ERROR_OBJECT (avi,
1446 "Not enough data to parse subindex (%d available, 24 needed)", size);
1447 gst_buffer_unref (buf);
1448 return TRUE; /* continue */
1452 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1453 ("Subindex-is-data is not implemented"));
1454 gst_buffer_unref (buf);
1459 GST_DEBUG_OBJECT (avi, "the index is empty");
1460 gst_buffer_unref (buf);
1465 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1466 ("Cannot allocate memory for %u*%u=%u bytes",
1467 (guint) sizeof (GstAviIndexEntry), num,
1468 (guint) sizeof (GstAviIndexEntry) * num));
1469 gst_buffer_unref (buf);
1476 * Read AVI index when streaming
1479 gst_avi_demux_read_subindexes_push (GstAviDemux * avi)
1481 guint32 tag = 0, size;
1482 GstBuffer *buf = NULL;
1485 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1487 for (n = 0; n < avi->num_streams; n++) {
1488 GstAviStream *stream = &avi->stream[n];
1490 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1491 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1493 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1494 '0' + stream->num % 10)) &&
1495 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1496 '0' + stream->num % 10, 'i', 'x'))) {
1497 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1498 GST_FOURCC_ARGS (tag));
1502 avi->offset += 8 + GST_ROUND_UP_2 (size);
1504 buf = gst_buffer_new ();
1505 GST_BUFFER_DATA (buf) = gst_adapter_take (avi->adapter, size);
1506 GST_BUFFER_SIZE (buf) = size;
1508 if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1512 g_free (stream->indexes);
1513 stream->indexes = NULL;
1515 /* get stream stats now */
1516 avi->have_index = gst_avi_demux_do_index_stats (avi);
1524 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi)
1530 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1532 for (n = 0; n < avi->num_streams; n++) {
1533 GstAviStream *stream = &avi->stream[n];
1535 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1536 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi), avi->sinkpad,
1537 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1539 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1540 '0' + stream->num % 10)) &&
1541 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1542 '0' + stream->num % 10, 'i', 'x'))) {
1543 /* Some ODML files (created by god knows what muxer) have a ##ix format
1544 * instead of the 'official' ix##. They are still valid though. */
1545 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1546 GST_FOURCC_ARGS (tag));
1547 gst_buffer_unref (buf);
1551 if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1555 g_free (stream->indexes);
1556 stream->indexes = NULL;
1558 /* get stream stats now */
1559 avi->have_index = gst_avi_demux_do_index_stats (avi);
1563 * gst_avi_demux_riff_parse_vprp:
1564 * @element: caller element (used for debugging/error).
1565 * @buf: input data to be used for parsing, stripped from header.
1566 * @vprp: a pointer (returned by this function) to a filled-in vprp
1567 * structure. Caller should free it.
1569 * Parses a video stream´s vprp. This function takes ownership of @buf.
1571 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1572 * should be skipped on error, but it is not fatal.
1575 gst_avi_demux_riff_parse_vprp (GstElement * element,
1576 GstBuffer * buf, gst_riff_vprp ** _vprp)
1578 gst_riff_vprp *vprp;
1581 g_return_val_if_fail (buf != NULL, FALSE);
1582 g_return_val_if_fail (_vprp != NULL, FALSE);
1584 if (GST_BUFFER_SIZE (buf) < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1587 vprp = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1589 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1590 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1591 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1592 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1593 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1594 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1595 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1596 vprp->width = GUINT32_FROM_LE (vprp->width);
1597 vprp->height = GUINT32_FROM_LE (vprp->height);
1598 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1602 /* calculate fields based on size */
1603 k = (GST_BUFFER_SIZE (buf) - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) /
1605 if (vprp->fields > k) {
1606 GST_WARNING_OBJECT (element,
1607 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1610 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1611 GST_WARNING_OBJECT (element,
1612 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1613 GST_RIFF_VPRP_VIDEO_FIELDS);
1614 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1616 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1617 for (k = 0; k < vprp->fields; k++) {
1618 gst_riff_vprp_video_field_desc *fd;
1620 fd = &vprp->field_info[k];
1621 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1622 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1623 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1624 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1625 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1626 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1627 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1628 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1633 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1634 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1635 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1636 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1637 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1638 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1639 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1640 vprp->aspect & 0xffff);
1641 GST_INFO_OBJECT (element, " width %d", vprp->width);
1642 GST_INFO_OBJECT (element, " height %d", vprp->height);
1643 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1644 for (k = 0; k < vprp->fields; k++) {
1645 gst_riff_vprp_video_field_desc *fd;
1647 fd = &(vprp->field_info[k]);
1648 GST_INFO_OBJECT (element, " field %u description:", k);
1649 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1650 fd->compressed_bm_height);
1651 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1652 fd->compressed_bm_width);
1653 GST_INFO_OBJECT (element, " valid_bm_height %d",
1654 fd->valid_bm_height);
1655 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1656 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1657 fd->valid_bm_x_offset);
1658 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1659 fd->valid_bm_y_offset);
1660 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1661 fd->video_x_t_offset);
1662 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1665 gst_buffer_unref (buf);
1674 GST_ERROR_OBJECT (element,
1675 "Too small vprp (%d available, at least %d needed)",
1676 GST_BUFFER_SIZE (buf),
1677 (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1678 gst_buffer_unref (buf);
1684 gst_avi_demux_expose_streams (GstAviDemux * avi, gboolean force)
1688 GST_DEBUG_OBJECT (avi, "force : %d", force);
1690 for (i = 0; i < avi->num_streams; i++) {
1691 GstAviStream *stream = &avi->stream[i];
1693 if (force || stream->idx_n != 0) {
1694 GST_LOG_OBJECT (avi, "Added pad %s with caps %" GST_PTR_FORMAT,
1695 GST_PAD_NAME (stream->pad), GST_PAD_CAPS (stream->pad));
1696 gst_element_add_pad ((GstElement *) avi, stream->pad);
1697 stream->exposed = TRUE;
1698 if (avi->main_stream == -1)
1699 avi->main_stream = i;
1701 GST_WARNING_OBJECT (avi, "Stream #%d doesn't have any entry, removing it",
1703 gst_avi_demux_reset_stream (avi, stream);
1709 * gst_avi_demux_parse_stream:
1710 * @avi: calling element (used for debugging/errors).
1711 * @buf: input buffer used to parse the stream.
1713 * Parses all subchunks in a strl chunk (which defines a single
1714 * stream). Discards the buffer after use. This function will
1715 * increment the stream counter internally.
1717 * Returns: whether the stream was identified successfully.
1718 * Errors are not fatal. It does indicate the stream
1722 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
1724 GstAviStream *stream;
1725 GstElementClass *klass;
1726 GstPadTemplate *templ;
1727 GstBuffer *sub = NULL;
1730 gchar *codec_name = NULL, *padname = NULL;
1731 const gchar *tag_name;
1732 GstCaps *caps = NULL;
1734 GstElement *element;
1735 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
1736 gst_riff_vprp *vprp = NULL;
1738 element = GST_ELEMENT_CAST (avi);
1740 GST_DEBUG_OBJECT (avi, "Parsing stream");
1742 if (avi->num_streams >= GST_AVI_DEMUX_MAX_STREAMS) {
1743 GST_WARNING_OBJECT (avi,
1744 "maximum no of streams (%d) exceeded, ignoring stream",
1745 GST_AVI_DEMUX_MAX_STREAMS);
1746 gst_buffer_unref (buf);
1747 /* not a fatal error, let's say */
1751 stream = &avi->stream[avi->num_streams];
1753 /* initial settings */
1754 stream->idx_duration = GST_CLOCK_TIME_NONE;
1755 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1756 stream->duration = GST_CLOCK_TIME_NONE;
1758 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
1759 /* sub can be NULL if the chunk is empty */
1761 GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
1762 GST_FOURCC_ARGS (tag));
1766 case GST_RIFF_TAG_strh:
1768 gst_riff_strh *strh;
1771 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
1774 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
1775 /* ownership given away */
1777 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
1781 strh = stream->strh;
1782 /* sanity check; stream header frame rate matches global header
1784 if (stream->strh->type == GST_RIFF_FCC_vids) {
1786 GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
1788 s_dur = gst_util_uint64_scale (GST_SECOND, strh->scale, strh->rate);
1789 GST_DEBUG_OBJECT (avi, "verifying stream framerate %d/%d, "
1790 "frame duration = %d ms", strh->rate, strh->scale,
1791 (gint) (s_dur / GST_MSECOND));
1792 if (h_dur > (10 * GST_MSECOND) && (s_dur > 10 * h_dur)) {
1793 strh->rate = GST_SECOND / GST_USECOND;
1794 strh->scale = h_dur / GST_USECOND;
1795 GST_DEBUG_OBJECT (avi, "correcting stream framerate to %d/%d",
1796 strh->rate, strh->scale);
1799 /* determine duration as indicated by header */
1800 stream->hdr_duration = gst_util_uint64_scale ((guint64) strh->length *
1801 strh->scale, GST_SECOND, (guint64) strh->rate);
1802 GST_INFO ("Stream duration according to header: %" GST_TIME_FORMAT,
1803 GST_TIME_ARGS (stream->hdr_duration));
1804 if (stream->hdr_duration == 0)
1805 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1810 case GST_RIFF_TAG_strf:
1812 gboolean res = FALSE;
1815 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
1819 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
1822 switch (stream->strh->type) {
1823 case GST_RIFF_FCC_vids:
1824 stream->is_vbr = TRUE;
1825 res = gst_riff_parse_strf_vids (element, sub,
1826 &stream->strf.vids, &stream->extradata);
1828 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
1830 case GST_RIFF_FCC_auds:
1831 stream->is_vbr = (stream->strh->samplesize == 0)
1832 && stream->strh->scale > 1;
1834 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
1835 &stream->extradata);
1837 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
1838 stream->is_vbr, res);
1840 case GST_RIFF_FCC_iavs:
1841 stream->is_vbr = TRUE;
1842 res = gst_riff_parse_strf_iavs (element, sub,
1843 &stream->strf.iavs, &stream->extradata);
1845 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
1847 case GST_RIFF_FCC_txts:
1848 /* nothing to parse here */
1849 stream->is_vbr = (stream->strh->samplesize == 0)
1850 && (stream->strh->scale > 1);
1854 GST_ERROR_OBJECT (avi,
1855 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
1856 GST_FOURCC_ARGS (stream->strh->type));
1860 gst_buffer_unref (sub);
1868 case GST_RIFF_TAG_vprp:
1871 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
1875 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
1879 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
1883 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
1884 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
1885 /* not considered fatal */
1893 case GST_RIFF_TAG_strd:
1894 if (stream->initdata)
1895 gst_buffer_unref (stream->initdata);
1896 stream->initdata = sub;
1899 case GST_RIFF_TAG_strn:
1900 g_free (stream->name);
1903 g_strndup ((gchar *) GST_BUFFER_DATA (sub),
1904 (gsize) GST_BUFFER_SIZE (sub));
1905 gst_buffer_unref (sub);
1908 stream->name = g_strdup ("");
1910 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
1913 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
1914 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
1915 '0' + avi->num_streams % 10)) {
1916 g_free (stream->indexes);
1917 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
1918 stream->superindex = TRUE;
1922 GST_WARNING_OBJECT (avi,
1923 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
1924 GST_FOURCC_ARGS (tag));
1926 case GST_RIFF_TAG_JUNK:
1930 gst_buffer_unref (sub);
1936 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
1941 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
1945 /* get class to figure out the template */
1946 klass = GST_ELEMENT_GET_CLASS (avi);
1948 /* we now have all info, let´s set up a pad and a caps and be done */
1949 /* create stream name + pad */
1950 switch (stream->strh->type) {
1951 case GST_RIFF_FCC_vids:{
1954 fourcc = (stream->strf.vids->compression) ?
1955 stream->strf.vids->compression : stream->strh->fcc_handler;
1956 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
1957 templ = gst_element_class_get_pad_template (klass, "video_%02d");
1958 caps = gst_riff_create_video_caps (fourcc, stream->strh,
1959 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
1961 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
1962 GST_TYPE_FOURCC, fourcc, NULL);
1963 } else if (got_vprp && vprp) {
1964 guint32 aspect_n, aspect_d;
1967 aspect_n = vprp->aspect >> 16;
1968 aspect_d = vprp->aspect & 0xffff;
1969 /* calculate the pixel aspect ratio using w/h and aspect ratio */
1970 n = aspect_n * stream->strf.vids->height;
1971 d = aspect_d * stream->strf.vids->width;
1973 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1975 /* very local, not needed elsewhere */
1979 tag_name = GST_TAG_VIDEO_CODEC;
1980 avi->num_v_streams++;
1983 case GST_RIFF_FCC_auds:{
1984 padname = g_strdup_printf ("audio_%02d", avi->num_a_streams);
1985 templ = gst_element_class_get_pad_template (klass, "audio_%02d");
1986 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
1987 stream->strh, stream->strf.auds, stream->extradata,
1988 stream->initdata, &codec_name);
1990 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
1991 G_TYPE_INT, stream->strf.auds->format, NULL);
1993 tag_name = GST_TAG_AUDIO_CODEC;
1994 avi->num_a_streams++;
1997 case GST_RIFF_FCC_iavs:{
1998 guint32 fourcc = stream->strh->fcc_handler;
2000 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
2001 templ = gst_element_class_get_pad_template (klass, "video_%02d");
2002 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
2003 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
2005 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2006 GST_TYPE_FOURCC, fourcc, NULL);
2008 tag_name = GST_TAG_VIDEO_CODEC;
2009 avi->num_v_streams++;
2012 case GST_RIFF_FCC_txts:{
2013 padname = g_strdup_printf ("subtitle_%02d", avi->num_t_streams);
2014 templ = gst_element_class_get_pad_template (klass, "subtitle_%02d");
2015 caps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
2017 avi->num_t_streams++;
2021 g_assert_not_reached ();
2024 /* no caps means no stream */
2026 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
2030 GST_DEBUG_OBJECT (element, "codec-name=%s",
2031 (codec_name ? codec_name : "NULL"));
2032 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
2034 /* set proper settings and add it */
2036 gst_object_unref (stream->pad);
2037 pad = stream->pad = gst_pad_new_from_template (templ, padname);
2040 gst_pad_use_fixed_caps (pad);
2042 gst_pad_set_formats_function (pad,
2043 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
2044 gst_pad_set_event_mask_function (pad,
2045 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
2047 gst_pad_set_event_function (pad,
2048 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
2049 gst_pad_set_query_type_function (pad,
2050 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_query_types));
2051 gst_pad_set_query_function (pad,
2052 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
2054 gst_pad_set_convert_function (pad,
2055 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
2058 if (avi->element_index)
2059 gst_index_get_writer_id (avi->element_index, GST_OBJECT_CAST (stream->pad),
2062 stream->num = avi->num_streams;
2064 stream->start_entry = 0;
2065 stream->step_entry = 0;
2066 stream->stop_entry = 0;
2068 stream->current_entry = -1;
2069 stream->current_total = 0;
2071 stream->last_flow = GST_FLOW_OK;
2072 stream->discont = TRUE;
2074 stream->total_bytes = 0;
2075 stream->total_blocks = 0;
2076 stream->n_keyframes = 0;
2079 stream->idx_max = 0;
2081 gst_pad_set_element_private (pad, stream);
2084 gst_pad_set_caps (pad, caps);
2085 gst_pad_set_active (pad, TRUE);
2086 gst_caps_unref (caps);
2090 if (!stream->taglist)
2091 stream->taglist = gst_tag_list_new ();
2093 avi->got_tags = TRUE;
2095 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
2097 g_free (codec_name);
2100 gst_buffer_unref (buf);
2107 /* unref any mem that may be in use */
2109 gst_buffer_unref (buf);
2111 gst_buffer_unref (sub);
2113 g_free (codec_name);
2114 gst_avi_demux_reset_stream (avi, stream);
2121 * gst_avi_demux_parse_odml:
2122 * @avi: calling element (used for debug/error).
2123 * @buf: input buffer to be used for parsing.
2125 * Read an openDML-2.0 extension header. Fills in the frame number
2126 * in the avi demuxer object when reading succeeds.
2129 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
2133 GstBuffer *sub = NULL;
2135 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
2138 case GST_RIFF_TAG_dmlh:{
2139 gst_riff_dmlh dmlh, *_dmlh;
2142 /* sub == NULL is possible and means an empty buffer */
2143 size = sub ? GST_BUFFER_SIZE (sub) : 0;
2146 if (size < sizeof (gst_riff_dmlh)) {
2147 GST_ERROR_OBJECT (avi,
2148 "DMLH entry is too small (%d bytes, %d needed)",
2149 size, (int) sizeof (gst_riff_dmlh));
2152 _dmlh = (gst_riff_dmlh *) GST_BUFFER_DATA (sub);
2153 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
2155 GST_INFO_OBJECT (avi, "dmlh tag found: totalframes: %u",
2158 avi->avih->tot_frames = dmlh.totalframes;
2163 GST_WARNING_OBJECT (avi,
2164 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
2165 GST_FOURCC_ARGS (tag));
2167 case GST_RIFF_TAG_JUNK:
2169 /* skip and move to next chunk */
2171 gst_buffer_unref (sub);
2178 gst_buffer_unref (buf);
2183 gst_avi_demux_index_last (GstAviDemux * avi, GstAviStream * stream)
2185 return stream->idx_n - 1;
2188 /* find a previous entry in the index with the given flags */
2190 gst_avi_demux_index_prev (GstAviDemux * avi, GstAviStream * stream,
2191 guint last, gboolean keyframe)
2193 GstAviIndexEntry *entry;
2196 for (i = last; i > 0; i--) {
2197 entry = &stream->index[i - 1];
2198 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2206 gst_avi_demux_index_next (GstAviDemux * avi, GstAviStream * stream,
2207 guint last, gboolean keyframe)
2209 GstAviIndexEntry *entry;
2212 for (i = last + 1; i < stream->idx_n; i++) {
2213 entry = &stream->index[i];
2214 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2218 return stream->idx_n - 1;
2222 gst_avi_demux_index_entry_search (GstAviIndexEntry * entry, guint64 * total)
2224 if (entry->total < *total)
2226 else if (entry->total > *total)
2232 * gst_avi_demux_index_for_time:
2234 * @stream: the stream
2235 * @time: a time position
2237 * Finds the index entry which time is less or equal than the requested time.
2238 * Try to avoid binary search when we can convert the time to an index
2239 * position directly (for example for video frames with a fixed duration).
2241 * Returns: the found position in the index.
2244 gst_avi_demux_index_for_time (GstAviDemux * avi,
2245 GstAviStream * stream, guint64 time)
2250 GST_LOG_OBJECT (avi, "search time:%" GST_TIME_FORMAT, GST_TIME_ARGS (time));
2252 /* easy (and common) cases */
2253 if (time == 0 || stream->idx_n == 0)
2255 if (time >= stream->idx_duration)
2256 return stream->idx_n - 1;
2258 /* figure out where we need to go. For that we convert the time to an
2259 * index entry or we convert it to a total and then do a binary search. */
2260 if (stream->is_vbr) {
2261 /* VBR stream next timestamp */
2262 if (stream->strh->type == GST_RIFF_FCC_auds) {
2263 total = avi_stream_convert_time_to_frames_unchecked (stream, time);
2265 index = avi_stream_convert_time_to_frames_unchecked (stream, time);
2268 /* constant rate stream */
2269 total = avi_stream_convert_time_to_bytes_unchecked (stream, time);
2273 GstAviIndexEntry *entry;
2275 /* no index, find index with binary search on total */
2276 GST_LOG_OBJECT (avi, "binary search for entry with total %"
2277 G_GUINT64_FORMAT, total);
2279 entry = gst_util_array_binary_search (stream->index,
2280 stream->idx_n, sizeof (GstAviIndexEntry),
2281 (GCompareDataFunc) gst_avi_demux_index_entry_search,
2282 GST_SEARCH_MODE_BEFORE, &total, NULL);
2284 if (entry == NULL) {
2285 GST_LOG_OBJECT (avi, "not found, assume index 0");
2288 index = entry - stream->index;
2289 GST_LOG_OBJECT (avi, "found at %u", index);
2292 GST_LOG_OBJECT (avi, "converted time to index %u", index);
2298 static inline GstAviStream *
2299 gst_avi_demux_stream_for_id (GstAviDemux * avi, guint32 id)
2302 GstAviStream *stream;
2304 /* get the stream for this entry */
2305 stream_nr = CHUNKID_TO_STREAMNR (id);
2306 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2307 GST_WARNING_OBJECT (avi, "invalid stream nr %d", stream_nr);
2310 stream = &avi->stream[stream_nr];
2311 if (G_UNLIKELY (!stream->strh)) {
2312 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2319 * gst_avi_demux_parse_index:
2320 * @avi: calling element (used for debugging/errors).
2321 * @buf: buffer containing the full index.
2323 * Read index entries from the provided buffer.
2324 * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
2327 gst_avi_demux_parse_index (GstAviDemux * avi, GstBuffer * buf)
2333 gst_riff_index_entry *index;
2335 GstAviStream *stream;
2336 GstAviIndexEntry entry;
2342 data = GST_BUFFER_DATA (buf);
2343 size = GST_BUFFER_SIZE (buf);
2345 stamp = gst_util_get_timestamp ();
2347 /* see how many items in the index */
2348 num = size / sizeof (gst_riff_index_entry);
2352 GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2354 index = (gst_riff_index_entry *) data;
2355 pos_before = avi->offset;
2357 /* figure out if the index is 0 based or relative to the MOVI start */
2358 entry.offset = GST_READ_UINT32_LE (&index[0].offset);
2359 if (entry.offset < avi->offset) {
2360 avi->index_offset = avi->offset + 8;
2361 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2363 avi->index_offset = 0;
2364 GST_DEBUG ("index is 0 based");
2367 for (i = 0, n = 0; i < num; i++) {
2368 id = GST_READ_UINT32_LE (&index[i].id);
2369 entry.offset = GST_READ_UINT32_LE (&index[i].offset);
2371 /* some sanity checks */
2372 if (G_UNLIKELY (id == GST_RIFF_rec || id == 0 ||
2373 (entry.offset == 0 && n > 0)))
2376 /* get the stream for this entry */
2377 stream = gst_avi_demux_stream_for_id (avi, id);
2378 if (G_UNLIKELY (!stream))
2381 /* handle offset and size */
2382 entry.offset += avi->index_offset + 8;
2383 entry.size = GST_READ_UINT32_LE (&index[i].size);
2386 if (stream->strh->type == GST_RIFF_FCC_auds) {
2387 /* all audio frames are keyframes */
2388 ENTRY_SET_KEYFRAME (&entry);
2391 /* else read flags */
2392 flags = GST_READ_UINT32_LE (&index[i].flags);
2393 if (flags & GST_RIFF_IF_KEYFRAME) {
2394 ENTRY_SET_KEYFRAME (&entry);
2396 ENTRY_UNSET_KEYFRAME (&entry);
2401 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2406 gst_buffer_unref (buf);
2408 /* get stream stats now */
2409 avi->have_index = gst_avi_demux_do_index_stats (avi);
2411 stamp = gst_util_get_timestamp () - stamp;
2412 GST_DEBUG_OBJECT (avi, "index parsing took %" GST_TIME_FORMAT,
2413 GST_TIME_ARGS (stamp));
2420 GST_DEBUG_OBJECT (avi, "empty index");
2421 gst_buffer_unref (buf);
2426 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2427 ("Cannot allocate memory for %u*%u=%u bytes",
2428 (guint) sizeof (GstAviIndexEntry), num,
2429 (guint) sizeof (GstAviIndexEntry) * num));
2430 gst_buffer_unref (buf);
2436 * gst_avi_demux_stream_index:
2437 * @avi: avi demuxer object.
2439 * Seeks to index and reads it.
2442 gst_avi_demux_stream_index (GstAviDemux * avi)
2445 guint64 offset = avi->offset;
2450 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2452 /* get chunk information */
2453 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2454 if (res != GST_FLOW_OK)
2456 else if (GST_BUFFER_SIZE (buf) < 8)
2459 /* check tag first before blindy trying to read 'size' bytes */
2460 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2461 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2462 if (tag == GST_RIFF_TAG_LIST) {
2463 /* this is the movi tag */
2464 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2465 (8 + GST_ROUND_UP_2 (size)));
2466 offset += 8 + GST_ROUND_UP_2 (size);
2467 gst_buffer_unref (buf);
2468 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2469 if (res != GST_FLOW_OK)
2471 else if (GST_BUFFER_SIZE (buf) < 8)
2473 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2474 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2477 if (tag != GST_RIFF_TAG_idx1)
2482 gst_buffer_unref (buf);
2484 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2486 /* read chunk, advance offset */
2487 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2488 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2491 GST_DEBUG ("will parse index chunk size %u for tag %"
2492 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2494 gst_avi_demux_parse_index (avi, buf);
2496 #ifndef GST_DISABLE_GST_DEBUG
2497 /* debug our indexes */
2500 GstAviStream *stream;
2502 for (i = 0; i < avi->num_streams; i++) {
2503 stream = &avi->stream[i];
2504 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2505 i, stream->idx_n, stream->total_bytes);
2514 GST_DEBUG_OBJECT (avi,
2515 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2520 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2521 gst_buffer_unref (buf);
2526 GST_WARNING_OBJECT (avi,
2527 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2528 GST_FOURCC_ARGS (tag));
2529 gst_buffer_unref (buf);
2534 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2535 gst_buffer_unref (buf);
2541 * gst_avi_demux_peek_tag:
2543 * Returns the tag and size of the next chunk
2545 static GstFlowReturn
2546 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2549 GstFlowReturn res = GST_FLOW_OK;
2550 GstBuffer *buf = NULL;
2554 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2555 if (res != GST_FLOW_OK)
2558 bufsize = GST_BUFFER_SIZE (buf);
2562 bufdata = GST_BUFFER_DATA (buf);
2564 *tag = GST_READ_UINT32_LE (bufdata);
2565 *size = GST_READ_UINT32_LE (bufdata + 4);
2567 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2568 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2569 *size, offset + 8, offset + 8 + (gint64) * size);
2572 gst_buffer_unref (buf);
2579 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2584 GST_DEBUG_OBJECT (avi, "got %d bytes which is <> 8 bytes", bufsize);
2585 res = GST_FLOW_ERROR;
2591 * gst_avi_demux_next_data_buffer:
2593 * Returns the offset and size of the next buffer
2594 * Position is the position of the buffer (after tag and size)
2596 static GstFlowReturn
2597 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2598 guint32 * tag, guint * size)
2600 guint64 off = *offset;
2605 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2606 if (res != GST_FLOW_OK)
2608 if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
2609 off += 8 + 4; /* skip tag + size + subtag */
2621 * gst_avi_demux_stream_scan:
2622 * @avi: calling element (used for debugging/errors).
2624 * Scan the file for all chunks to "create" a new index.
2628 gst_avi_demux_stream_scan (GstAviDemux * avi)
2631 GstAviStream *stream;
2640 * - implement non-seekable source support.
2642 GST_DEBUG_OBJECT (avi, "Creating index");
2644 /* get the size of the file */
2645 format = GST_FORMAT_BYTES;
2646 if (!gst_pad_query_peer_duration (avi->sinkpad, &format, &tmplength))
2650 /* guess the total amount of entries we expect */
2654 GstAviIndexEntry entry;
2657 /* start reading data buffers to find the id and offset */
2658 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
2659 if (G_UNLIKELY (res != GST_FLOW_OK))
2663 stream = gst_avi_demux_stream_for_id (avi, tag);
2664 if (G_UNLIKELY (!stream))
2667 /* we can't figure out the keyframes, assume they all are */
2668 entry.flags = GST_AVI_KEYFRAME;
2672 /* and add to the index of this stream */
2673 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2677 /* update position */
2678 pos += GST_ROUND_UP_2 (size);
2679 if (G_UNLIKELY (pos > length)) {
2680 GST_WARNING_OBJECT (avi,
2681 "Stopping index lookup since we are further than EOF");
2687 avi->have_index = gst_avi_demux_do_index_stats (avi);
2695 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2696 ("Cannot allocate memory for %u*%u=%u bytes",
2697 (guint) sizeof (GstAviIndexEntry), num,
2698 (guint) sizeof (GstAviIndexEntry) * num));
2704 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
2708 GstAviStream *stream;
2710 total = GST_CLOCK_TIME_NONE;
2712 /* all streams start at a timestamp 0 */
2713 for (i = 0; i < avi->num_streams; i++) {
2714 GstClockTime duration, hduration;
2715 gst_riff_strh *strh;
2717 stream = &avi->stream[i];
2718 if (G_UNLIKELY (!stream || !stream->idx_n || !(strh = stream->strh)))
2721 /* get header duration for the stream */
2722 hduration = stream->hdr_duration;
2723 /* index duration calculated during parsing */
2724 duration = stream->idx_duration;
2726 /* now pick a good duration */
2727 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2728 /* index gave valid duration, use that */
2729 GST_INFO ("Stream %p duration according to index: %" GST_TIME_FORMAT,
2730 stream, GST_TIME_ARGS (duration));
2732 /* fall back to header info to calculate a duration */
2733 duration = hduration;
2735 GST_INFO ("Setting duration of stream #%d to %" GST_TIME_FORMAT,
2736 i, GST_TIME_ARGS (duration));
2737 /* set duration for the stream */
2738 stream->duration = duration;
2740 /* find total duration */
2741 if (total == GST_CLOCK_TIME_NONE ||
2742 (GST_CLOCK_TIME_IS_VALID (duration) && duration > total))
2746 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
2747 /* now update the duration for those streams where we had none */
2748 for (i = 0; i < avi->num_streams; i++) {
2749 stream = &avi->stream[i];
2751 if (!GST_CLOCK_TIME_IS_VALID (stream->duration)
2752 || stream->duration == 0) {
2753 stream->duration = total;
2755 GST_INFO ("Stream %p duration according to total: %" GST_TIME_FORMAT,
2756 stream, GST_TIME_ARGS (total));
2761 /* and set the total duration in the segment. */
2762 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
2763 GST_TIME_ARGS (total));
2765 gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
2768 /* returns FALSE if there are no pads to deliver event to,
2769 * otherwise TRUE (whatever the outcome of event sending),
2770 * takes ownership of the event. */
2772 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
2774 gboolean result = FALSE;
2777 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
2778 GST_EVENT_TYPE_NAME (event), avi->num_streams);
2780 for (i = 0; i < avi->num_streams; i++) {
2781 GstAviStream *stream = &avi->stream[i];
2785 gst_pad_push_event (stream->pad, gst_event_ref (event));
2788 gst_event_unref (event);
2793 gst_avi_demux_check_seekability (GstAviDemux * avi)
2796 gboolean seekable = FALSE;
2797 gint64 start = -1, stop = -1;
2799 query = gst_query_new_seeking (GST_FORMAT_BYTES);
2800 if (!gst_pad_peer_query (avi->sinkpad, query)) {
2801 GST_DEBUG_OBJECT (avi, "seeking query failed");
2805 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
2807 /* try harder to query upstream size if we didn't get it the first time */
2808 if (seekable && stop == -1) {
2809 GstFormat fmt = GST_FORMAT_BYTES;
2811 GST_DEBUG_OBJECT (avi, "doing duration query to fix up unset stop");
2812 gst_pad_query_peer_duration (avi->sinkpad, &fmt, &stop);
2815 /* if upstream doesn't know the size, it's likely that it's not seekable in
2816 * practice even if it technically may be seekable */
2817 if (seekable && (start != 0 || stop <= start)) {
2818 GST_DEBUG_OBJECT (avi, "seekable but unknown start/stop -> disable");
2822 if (!avi->element_index) {
2823 GST_DEBUG_OBJECT (avi, "no index");
2828 GST_INFO_OBJECT (avi, "seekable: %d (%" G_GUINT64_FORMAT " - %"
2829 G_GUINT64_FORMAT ")", seekable, start, stop);
2830 avi->seekable = seekable;
2832 gst_query_unref (query);
2836 * Read AVI headers when streaming
2838 static GstFlowReturn
2839 gst_avi_demux_stream_header_push (GstAviDemux * avi)
2841 GstFlowReturn ret = GST_FLOW_OK;
2846 GstBuffer *buf = NULL, *sub = NULL;
2850 GstTagList *tags = NULL;
2852 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
2854 switch (avi->header_state) {
2855 case GST_AVI_DEMUX_HEADER_TAG_LIST:
2856 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2857 avi->offset += 8 + GST_ROUND_UP_2 (size);
2858 if (tag != GST_RIFF_TAG_LIST)
2859 goto header_no_list;
2861 gst_adapter_flush (avi->adapter, 8);
2862 /* Find the 'hdrl' LIST tag */
2863 GST_DEBUG ("Reading %d bytes", size);
2864 buf = gst_adapter_take_buffer (avi->adapter, size);
2866 if (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl)
2867 goto header_no_hdrl;
2871 gst_adapter_flush (avi->adapter, 1);
2873 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
2875 /* the hdrl starts with a 'avih' header */
2876 if (!gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
2878 goto header_no_avih;
2880 if (tag != GST_RIFF_TAG_avih)
2881 goto header_no_avih;
2883 if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
2884 goto header_wrong_avih;
2886 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
2888 /* now, read the elements from the header until the end */
2889 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
2891 /* sub can be NULL on empty tags */
2896 case GST_RIFF_TAG_LIST:
2897 if (GST_BUFFER_SIZE (sub) < 4)
2900 switch (GST_READ_UINT32_LE (GST_BUFFER_DATA (sub))) {
2901 case GST_RIFF_LIST_strl:
2902 if (!(gst_avi_demux_parse_stream (avi, sub))) {
2904 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
2905 ("failed to parse stream, ignoring"));
2910 case GST_RIFF_LIST_odml:
2911 gst_avi_demux_parse_odml (avi, sub);
2915 GST_WARNING_OBJECT (avi,
2916 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
2917 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA
2920 case GST_RIFF_TAG_JUNK:
2925 gst_avi_demux_parse_idit (avi, sub);
2928 GST_WARNING_OBJECT (avi,
2929 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
2930 offset, GST_FOURCC_ARGS (tag));
2932 case GST_RIFF_TAG_JUNK:
2934 /* move to next chunk */
2936 gst_buffer_unref (sub);
2941 gst_buffer_unref (buf);
2942 GST_DEBUG ("elements parsed");
2944 /* check parsed streams */
2945 if (avi->num_streams == 0) {
2947 } else if (avi->num_streams != avi->avih->streams) {
2948 GST_WARNING_OBJECT (avi,
2949 "Stream header mentioned %d streams, but %d available",
2950 avi->avih->streams, avi->num_streams);
2952 GST_DEBUG ("Get junk and info next");
2953 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
2955 /* Need more data */
2959 case GST_AVI_DEMUX_HEADER_INFO:
2960 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
2962 if (gst_adapter_available (avi->adapter) < 12)
2965 data = gst_adapter_peek (avi->adapter, 12);
2966 tag = GST_READ_UINT32_LE (data);
2967 size = GST_READ_UINT32_LE (data + 4);
2968 ltag = GST_READ_UINT32_LE (data + 8);
2970 if (tag == GST_RIFF_TAG_LIST) {
2972 case GST_RIFF_LIST_movi:
2973 gst_adapter_flush (avi->adapter, 12);
2976 case GST_RIFF_LIST_INFO:
2977 GST_DEBUG ("Found INFO chunk");
2978 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
2979 GST_DEBUG ("got size %d", size);
2981 gst_adapter_flush (avi->adapter, 12);
2983 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
2986 gst_adapter_flush (avi->adapter, 1);
2987 gst_riff_parse_info (GST_ELEMENT_CAST (avi), buf, &tags);
2989 if (avi->globaltags) {
2990 gst_tag_list_insert (avi->globaltags, tags,
2991 GST_TAG_MERGE_REPLACE);
2993 avi->globaltags = tags;
2997 gst_buffer_unref (buf);
2999 avi->offset += GST_ROUND_UP_2 (size) - 4;
3001 GST_DEBUG ("skipping INFO LIST prefix");
3004 /* Need more data */
3009 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3010 avi->offset += 8 + GST_ROUND_UP_2 (size);
3011 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3012 // ??? goto iterate; ???
3014 /* Need more data */
3020 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3021 avi->offset += 8 + GST_ROUND_UP_2 (size);
3022 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3025 /* Need more data */
3032 GST_WARNING ("unhandled header state: %d", avi->header_state);
3037 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3038 avi->num_streams, avi->stream[0].indexes);
3040 GST_DEBUG ("Found movi chunk. Starting to stream data");
3041 avi->state = GST_AVI_DEMUX_MOVI;
3043 /* no indexes in push mode, but it still sets some variables */
3044 gst_avi_demux_calculate_durations_from_index (avi);
3046 gst_avi_demux_expose_streams (avi, TRUE);
3048 /* prepare all streams for index 0 */
3049 for (i = 0; i < avi->num_streams; i++)
3050 avi->stream[i].current_entry = 0;
3052 /* create initial NEWSEGMENT event */
3053 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3054 stop = avi->segment.duration;
3056 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3059 gst_event_unref (avi->seg_event);
3060 avi->seg_event = gst_event_new_new_segment_full
3061 (FALSE, avi->segment.rate, avi->segment.applied_rate, GST_FORMAT_TIME,
3062 avi->segment.start, stop, avi->segment.time);
3064 gst_avi_demux_check_seekability (avi);
3066 /* at this point we know all the streams and we can signal the no more
3068 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3069 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3076 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3077 return GST_FLOW_ERROR;
3081 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3082 ("Invalid AVI header (no LIST at start): %"
3083 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3084 return GST_FLOW_ERROR;
3088 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3089 ("Invalid AVI header (no hdrl at start): %"
3090 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3091 gst_buffer_unref (buf);
3092 return GST_FLOW_ERROR;
3096 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3097 ("Invalid AVI header (no avih at start): %"
3098 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3100 gst_buffer_unref (sub);
3102 gst_buffer_unref (buf);
3103 return GST_FLOW_ERROR;
3107 gst_buffer_unref (buf);
3108 return GST_FLOW_ERROR;
3113 gst_avi_demux_add_date_tag (GstAviDemux * avi, gint y, gint m, gint d)
3116 date = g_date_new_dmy (d, m, y);
3117 if (!g_date_valid (date)) {
3119 GST_WARNING_OBJECT (avi, "Refusing to add invalid date %d-%d-%d", y, m, d);
3124 if (avi->globaltags == NULL)
3125 avi->globaltags = gst_tag_list_new ();
3127 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE, date,
3133 gst_avi_demux_parse_idit_nums_only (GstAviDemux * avi, gchar * data)
3138 ret = sscanf (data, "%d:%d:%d", &y, &m, &d);
3140 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3143 gst_avi_demux_add_date_tag (avi, y, m, d);
3147 get_month_num (gchar * data, guint size)
3149 if (g_ascii_strncasecmp (data, "jan", 3) == 0) {
3151 } else if (g_ascii_strncasecmp (data, "feb", 3) == 0) {
3153 } else if (g_ascii_strncasecmp (data, "mar", 3) == 0) {
3155 } else if (g_ascii_strncasecmp (data, "apr", 3) == 0) {
3157 } else if (g_ascii_strncasecmp (data, "may", 3) == 0) {
3159 } else if (g_ascii_strncasecmp (data, "jun", 3) == 0) {
3161 } else if (g_ascii_strncasecmp (data, "jul", 3) == 0) {
3163 } else if (g_ascii_strncasecmp (data, "aug", 3) == 0) {
3165 } else if (g_ascii_strncasecmp (data, "sep", 3) == 0) {
3167 } else if (g_ascii_strncasecmp (data, "oct", 3) == 0) {
3169 } else if (g_ascii_strncasecmp (data, "nov", 3) == 0) {
3171 } else if (g_ascii_strncasecmp (data, "dec", 3) == 0) {
3179 gst_avi_demux_parse_idit_text (GstAviDemux * avi, gchar * data)
3181 gint year, month, day;
3182 gint hour, min, sec;
3187 ret = sscanf (data, "%3s %3s %d %d:%d:%d %d", weekday, monthstr, &day, &hour,
3190 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3193 month = get_month_num (monthstr, strlen (monthstr));
3194 gst_avi_demux_add_date_tag (avi, year, month, day);
3198 gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf)
3200 gchar *data = (gchar *) GST_BUFFER_DATA (buf);
3201 guint size = GST_BUFFER_SIZE (buf);
3202 gchar *safedata = NULL;
3206 * http://www.eden-foundation.org/products/code/film_date_stamp/index.html
3208 * This tag could be in one of the below formats
3209 * 2005:08:17 11:42:43
3210 * THU OCT 26 16:46:04 2006
3211 * Mon Mar 3 09:44:56 2008
3213 * FIXME: Our date tag doesn't include hours
3216 /* skip eventual initial whitespace */
3217 while (size > 0 && g_ascii_isspace (data[0])) {
3226 /* make a safe copy to add a \0 to the end of the string */
3227 safedata = g_strndup (data, size);
3229 /* test if the first char is a alpha or a number */
3230 if (g_ascii_isdigit (data[0])) {
3231 gst_avi_demux_parse_idit_nums_only (avi, safedata);
3234 } else if (g_ascii_isalpha (data[0])) {
3235 gst_avi_demux_parse_idit_text (avi, safedata);
3243 GST_WARNING_OBJECT (avi, "IDIT tag has no parsable info");
3247 * Read full AVI headers.
3249 static GstFlowReturn
3250 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
3253 GstBuffer *buf, *sub = NULL;
3257 GstElement *element = GST_ELEMENT_CAST (avi);
3259 GstTagList *tags = NULL;
3261 stamp = gst_util_get_timestamp ();
3263 /* the header consists of a 'hdrl' LIST tag */
3264 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3265 if (res != GST_FLOW_OK)
3266 goto pull_range_failed;
3267 else if (tag != GST_RIFF_TAG_LIST)
3269 else if (GST_BUFFER_SIZE (buf) < 4)
3272 GST_DEBUG_OBJECT (avi, "parsing headers");
3274 /* Find the 'hdrl' LIST tag */
3275 while (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl) {
3276 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
3277 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf))));
3280 gst_buffer_unref (buf);
3282 /* read new chunk */
3283 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3284 if (res != GST_FLOW_OK)
3285 goto pull_range_failed;
3286 else if (tag != GST_RIFF_TAG_LIST)
3288 else if (GST_BUFFER_SIZE (buf) < 4)
3292 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
3294 /* the hdrl starts with a 'avih' header */
3295 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
3297 else if (tag != GST_RIFF_TAG_avih)
3299 else if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3302 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3304 /* now, read the elements from the header until the end */
3305 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
3306 /* sub can be NULL on empty tags */
3311 case GST_RIFF_TAG_LIST:
3316 if (GST_BUFFER_SIZE (sub) < 4)
3319 data = GST_BUFFER_DATA (sub);
3320 fourcc = GST_READ_UINT32_LE (data);
3323 case GST_RIFF_LIST_strl:
3324 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3325 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3326 ("failed to parse stream, ignoring"));
3331 case GST_RIFF_LIST_odml:
3332 gst_avi_demux_parse_odml (avi, sub);
3335 case GST_RIFF_LIST_INFO:
3336 GST_BUFFER_DATA (sub) = data + 4;
3337 GST_BUFFER_SIZE (sub) -= 4;
3338 gst_riff_parse_info (element, sub, &tags);
3340 if (avi->globaltags) {
3341 gst_tag_list_insert (avi->globaltags, tags,
3342 GST_TAG_MERGE_REPLACE);
3344 avi->globaltags = tags;
3350 GST_WARNING_OBJECT (avi,
3351 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3352 GST_FOURCC_ARGS (fourcc));
3353 GST_MEMDUMP_OBJECT (avi, "Unknown list", GST_BUFFER_DATA (sub),
3354 GST_BUFFER_SIZE (sub));
3356 case GST_RIFF_TAG_JUNK:
3362 gst_avi_demux_parse_idit (avi, sub);
3365 GST_WARNING_OBJECT (avi,
3366 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
3367 GST_FOURCC_ARGS (tag), offset);
3368 GST_MEMDUMP_OBJECT (avi, "Unknown tag", GST_BUFFER_DATA (sub),
3369 GST_BUFFER_SIZE (sub));
3371 case GST_RIFF_TAG_JUNK:
3374 gst_buffer_unref (sub);
3379 gst_buffer_unref (buf);
3380 GST_DEBUG ("elements parsed");
3382 /* check parsed streams */
3383 if (avi->num_streams == 0)
3385 else if (avi->num_streams != avi->avih->streams) {
3386 GST_WARNING_OBJECT (avi,
3387 "Stream header mentioned %d streams, but %d available",
3388 avi->avih->streams, avi->num_streams);
3391 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
3392 G_GUINT64_FORMAT, avi->offset);
3394 /* Now, find the data (i.e. skip all junk between header and data) */
3400 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
3401 if (res != GST_FLOW_OK) {
3402 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
3403 goto pull_range_failed;
3404 } else if (GST_BUFFER_SIZE (buf) < 12) {
3405 GST_DEBUG_OBJECT (avi, "got %d bytes which is less than 12 bytes",
3406 GST_BUFFER_SIZE (buf));
3407 gst_buffer_unref (buf);
3408 return GST_FLOW_ERROR;
3411 data = GST_BUFFER_DATA (buf);
3413 tag = GST_READ_UINT32_LE (data);
3414 size = GST_READ_UINT32_LE (data + 4);
3415 ltag = GST_READ_UINT32_LE (data + 8);
3417 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
3418 GST_FOURCC_ARGS (tag), size);
3419 GST_MEMDUMP ("Tag content", data, GST_BUFFER_SIZE (buf));
3420 gst_buffer_unref (buf);
3423 case GST_RIFF_TAG_LIST:{
3425 case GST_RIFF_LIST_movi:
3426 GST_DEBUG_OBJECT (avi,
3427 "Reached the 'movi' tag, we're done with skipping");
3429 case GST_RIFF_LIST_INFO:
3431 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3433 if (res != GST_FLOW_OK) {
3434 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3435 goto pull_range_failed;
3437 GST_DEBUG ("got size %u", GST_BUFFER_SIZE (buf));
3439 GST_DEBUG ("skipping INFO LIST prefix");
3440 avi->offset += (4 - GST_ROUND_UP_2 (size));
3441 gst_buffer_unref (buf);
3445 sub = gst_buffer_create_sub (buf, 4, GST_BUFFER_SIZE (buf) - 4);
3446 gst_riff_parse_info (element, sub, &tags);
3448 if (avi->globaltags) {
3449 gst_tag_list_insert (avi->globaltags, tags,
3450 GST_TAG_MERGE_REPLACE);
3452 avi->globaltags = tags;
3457 gst_buffer_unref (sub);
3460 gst_buffer_unref (buf);
3461 /* gst_riff_read_chunk() has already advanced avi->offset */
3464 GST_WARNING_OBJECT (avi,
3465 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3466 GST_FOURCC_ARGS (ltag));
3467 avi->offset += 8 + GST_ROUND_UP_2 (size);
3473 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3474 GST_FOURCC_ARGS (tag));
3476 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3477 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3478 /* Only get buffer for debugging if the memdump is needed */
3479 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= 9) {
3480 res = gst_pad_pull_range (avi->sinkpad, avi->offset, size, &buf);
3481 if (res != GST_FLOW_OK) {
3482 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3483 goto pull_range_failed;
3485 GST_MEMDUMP ("Junk", GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
3486 gst_buffer_unref (buf);
3488 avi->offset += 8 + GST_ROUND_UP_2 (size);
3494 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3495 avi->num_streams, avi->stream[0].indexes);
3497 /* create or read stream index (for seeking) */
3498 if (avi->stream[0].indexes != NULL) {
3499 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3500 gst_avi_demux_read_subindexes_pull (avi);
3502 if (!avi->have_index) {
3503 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
3504 gst_avi_demux_stream_index (avi);
3506 /* still no index, scan */
3507 if (!avi->have_index) {
3508 gst_avi_demux_stream_scan (avi);
3510 /* still no index.. this is a fatal error for now.
3511 * FIXME, we should switch to plain push mode without seeking
3512 * instead of failing. */
3513 if (!avi->have_index)
3517 /* use the indexes now to construct nice durations */
3518 gst_avi_demux_calculate_durations_from_index (avi);
3520 gst_avi_demux_expose_streams (avi, FALSE);
3522 /* create initial NEWSEGMENT event */
3523 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3524 stop = avi->segment.duration;
3526 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3528 /* do initial seek to the default segment values */
3529 gst_avi_demux_do_seek (avi, &avi->segment);
3531 /* prepare initial segment */
3533 gst_event_unref (avi->seg_event);
3534 avi->seg_event = gst_event_new_new_segment_full
3535 (FALSE, avi->segment.rate, avi->segment.applied_rate, GST_FORMAT_TIME,
3536 avi->segment.start, stop, avi->segment.time);
3538 stamp = gst_util_get_timestamp () - stamp;
3539 GST_DEBUG_OBJECT (avi, "pulling header took %" GST_TIME_FORMAT,
3540 GST_TIME_ARGS (stamp));
3542 /* at this point we know all the streams and we can signal the no more
3544 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3545 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3552 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3553 ("Invalid AVI header (no LIST at start): %"
3554 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3555 gst_buffer_unref (buf);
3556 return GST_FLOW_ERROR;
3560 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3561 ("Invalid AVI header (no hdrl at start): %"
3562 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3563 gst_buffer_unref (buf);
3564 return GST_FLOW_ERROR;
3568 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3569 ("Invalid AVI header (no avih at start): %"
3570 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3572 gst_buffer_unref (sub);
3573 gst_buffer_unref (buf);
3574 return GST_FLOW_ERROR;
3578 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3579 ("Invalid AVI header (cannot parse avih at start)"));
3580 gst_buffer_unref (buf);
3581 return GST_FLOW_ERROR;
3585 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3586 return GST_FLOW_ERROR;
3590 GST_WARNING ("file without or too big index");
3591 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3592 ("Could not get/create index"));
3593 return GST_FLOW_ERROR;
3597 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3598 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3599 return GST_FLOW_ERROR;
3603 /* move a stream to @index */
3605 gst_avi_demux_move_stream (GstAviDemux * avi, GstAviStream * stream,
3606 GstSegment * segment, guint index)
3608 GST_DEBUG_OBJECT (avi, "Move stream %d to %u", stream->num, index);
3610 if (segment->rate < 0.0) {
3612 /* Because we don't know the frame order we need to push from the prev keyframe
3613 * to the next keyframe. If there is a smart decoder downstream he will notice
3614 * that there are too many encoded frames send and return UNEXPECTED when there
3615 * are enough decoded frames to fill the segment. */
3616 next_key = gst_avi_demux_index_next (avi, stream, index, TRUE);
3618 /* FIXME, we go back to 0, we should look at segment.start. We will however
3619 * stop earlier when the see the timestamp < segment.start */
3620 stream->start_entry = 0;
3621 stream->step_entry = index;
3622 stream->current_entry = index;
3623 stream->stop_entry = next_key;
3625 GST_DEBUG_OBJECT (avi, "reverse seek: start %u, step %u, stop %u",
3626 stream->start_entry, stream->step_entry, stream->stop_entry);
3628 stream->start_entry = index;
3629 stream->step_entry = index;
3630 stream->stop_entry = gst_avi_demux_index_last (avi, stream);
3632 if (stream->current_entry != index) {
3633 GST_DEBUG_OBJECT (avi, "Move DISCONT from %u to %u",
3634 stream->current_entry, index);
3635 stream->current_entry = index;
3636 stream->discont = TRUE;
3639 /* update the buffer info */
3640 gst_avi_demux_get_buffer_info (avi, stream, index,
3641 &stream->current_timestamp, &stream->current_ts_end,
3642 &stream->current_offset, &stream->current_offset_end);
3644 GST_DEBUG_OBJECT (avi, "Moved to %u, ts %" GST_TIME_FORMAT
3645 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
3646 ", off_end %" G_GUINT64_FORMAT, index,
3647 GST_TIME_ARGS (stream->current_timestamp),
3648 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
3649 stream->current_offset_end);
3653 * Do the actual seeking.
3656 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
3658 GstClockTime seek_time;
3661 GstAviStream *stream;
3663 seek_time = segment->last_stop;
3664 keyframe = !!(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
3666 GST_DEBUG_OBJECT (avi, "seek to: %" GST_TIME_FORMAT
3667 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
3669 /* FIXME, this code assumes the main stream with keyframes is stream 0,
3670 * which is mostly correct... */
3671 stream = &avi->stream[avi->main_stream];
3673 /* get the entry index for the requested position */
3674 index = gst_avi_demux_index_for_time (avi, stream, seek_time);
3675 GST_DEBUG_OBJECT (avi, "Got entry %u", index);
3677 /* check if we are already on a keyframe */
3678 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
3679 GST_DEBUG_OBJECT (avi, "not keyframe, searching back");
3680 /* now go to the previous keyframe, this is where we should start
3682 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
3683 GST_DEBUG_OBJECT (avi, "previous keyframe at %u", index);
3686 /* move the main stream to this position */
3687 gst_avi_demux_move_stream (avi, stream, segment, index);
3690 /* when seeking to a keyframe, we update the result seek time
3691 * to the time of the keyframe. */
3692 seek_time = stream->current_timestamp;
3693 GST_DEBUG_OBJECT (avi, "keyframe adjusted to %" GST_TIME_FORMAT,
3694 GST_TIME_ARGS (seek_time));
3697 /* the seek time is also the last_stop and stream time when going
3699 segment->last_stop = seek_time;
3700 if (segment->rate > 0.0)
3701 segment->time = seek_time;
3703 /* now set DISCONT and align the other streams */
3704 for (i = 0; i < avi->num_streams; i++) {
3705 GstAviStream *ostream;
3707 ostream = &avi->stream[i];
3708 if ((ostream == stream) || (ostream->index == NULL))
3711 /* get the entry index for the requested position */
3712 index = gst_avi_demux_index_for_time (avi, ostream, seek_time);
3714 /* move to previous keyframe */
3715 if (!ENTRY_IS_KEYFRAME (&ostream->index[index]))
3716 index = gst_avi_demux_index_prev (avi, ostream, index, TRUE);
3718 gst_avi_demux_move_stream (avi, ostream, segment, index);
3720 GST_DEBUG_OBJECT (avi, "done seek to: %" GST_TIME_FORMAT,
3721 GST_TIME_ARGS (seek_time));
3727 * Handle seek event.
3730 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
3735 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
3739 GstSegment seeksegment = { 0, };
3743 GST_DEBUG_OBJECT (avi, "doing seek with event");
3745 gst_event_parse_seek (event, &rate, &format, &flags,
3746 &cur_type, &cur, &stop_type, &stop);
3748 /* we have to have a format as the segment format. Try to convert
3750 if (format != GST_FORMAT_TIME) {
3751 GstFormat fmt = GST_FORMAT_TIME;
3752 gboolean res = TRUE;
3754 if (cur_type != GST_SEEK_TYPE_NONE)
3755 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
3756 if (res && stop_type != GST_SEEK_TYPE_NONE)
3757 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
3763 GST_DEBUG_OBJECT (avi,
3764 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
3765 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
3766 /* FIXME: can we do anything with rate!=1.0 */
3768 GST_DEBUG_OBJECT (avi, "doing seek without event");
3773 /* save flush flag */
3774 flush = flags & GST_SEEK_FLAG_FLUSH;
3777 GstEvent *fevent = gst_event_new_flush_start ();
3779 /* for a flushing seek, we send a flush_start on all pads. This will
3780 * eventually stop streaming with a WRONG_STATE. We can thus eventually
3781 * take the STREAM_LOCK. */
3782 GST_DEBUG_OBJECT (avi, "sending flush start");
3783 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
3784 gst_pad_push_event (avi->sinkpad, fevent);
3786 /* a non-flushing seek, we PAUSE the task so that we can take the
3788 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
3789 gst_pad_pause_task (avi->sinkpad);
3792 /* wait for streaming to stop */
3793 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
3794 GST_PAD_STREAM_LOCK (avi->sinkpad);
3796 /* copy segment, we need this because we still need the old
3797 * segment when we close the current segment. */
3798 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
3801 GST_DEBUG_OBJECT (avi, "configuring seek");
3802 gst_segment_set_seek (&seeksegment, rate, format, flags,
3803 cur_type, cur, stop_type, stop, &update);
3805 /* do the seek, seeksegment.last_stop contains the new position, this
3806 * actually never fails. */
3807 gst_avi_demux_do_seek (avi, &seeksegment);
3810 GstEvent *fevent = gst_event_new_flush_stop ();
3812 GST_DEBUG_OBJECT (avi, "sending flush stop");
3813 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
3814 gst_pad_push_event (avi->sinkpad, fevent);
3816 /* reset the last flow and mark discont, FLUSH is always DISCONT */
3817 for (i = 0; i < avi->num_streams; i++) {
3818 avi->stream[i].last_flow = GST_FLOW_OK;
3819 avi->stream[i].discont = TRUE;
3821 } else if (avi->segment_running) {
3824 /* we are running the current segment and doing a non-flushing seek,
3825 * close the segment first based on the last_stop. */
3826 GST_DEBUG_OBJECT (avi, "closing running segment %" G_GINT64_FORMAT
3827 " to %" G_GINT64_FORMAT, avi->segment.start, avi->segment.last_stop);
3828 seg = gst_event_new_new_segment_full (TRUE,
3829 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
3830 avi->segment.start, avi->segment.last_stop, avi->segment.time);
3831 gst_avi_demux_push_event (avi, seg);
3834 /* now update the real segment info */
3835 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
3837 /* post the SEGMENT_START message when we do segmented playback */
3838 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3839 gst_element_post_message (GST_ELEMENT_CAST (avi),
3840 gst_message_new_segment_start (GST_OBJECT_CAST (avi),
3841 avi->segment.format, avi->segment.last_stop));
3844 /* prepare for streaming again */
3845 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3846 stop = avi->segment.duration;
3848 /* queue the segment event for the streaming thread. */
3850 gst_event_unref (avi->seg_event);
3851 if (avi->segment.rate > 0.0) {
3852 /* forwards goes from last_stop to stop */
3853 avi->seg_event = gst_event_new_new_segment_full (FALSE,
3854 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
3855 avi->segment.last_stop, stop, avi->segment.time);
3857 /* reverse goes from start to last_stop */
3858 avi->seg_event = gst_event_new_new_segment_full (FALSE,
3859 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
3860 avi->segment.start, avi->segment.last_stop, avi->segment.time);
3863 if (!avi->streaming) {
3864 avi->segment_running = TRUE;
3865 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
3868 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
3875 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
3881 gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
3887 GstSeekType cur_type, stop_type;
3892 GstIndexEntry *entry;
3894 GST_DEBUG_OBJECT (avi, "doing push-based seek");
3896 gst_event_parse_seek (event, &rate, &format, &flags,
3897 &cur_type, &cur, &stop_type, &stop);
3899 if (stop_type != GST_SEEK_TYPE_NONE)
3900 goto unsupported_seek;
3903 /* only forward streaming and seeking is possible */
3905 goto unsupported_seek;
3908 if (format != GST_FORMAT_TIME)
3909 goto unsupported_format;
3911 /* not really advisable otherwise */
3912 if ((flags & GST_SEEK_FLAG_FLUSH) == 0)
3913 goto unsupported_seek;
3915 /* should have index, let's check anyway */
3916 if (!avi->element_index)
3919 /* find reasonable corresponding BYTE position
3920 * note that we have no keyframe info in push mode whatsoever,
3921 * so we can not cater for related stuff in any way */
3923 /* some slack aiming for a keyframe */
3924 if (cur < GST_AVI_SEEK_PUSH_DISPLACE)
3927 cur -= GST_AVI_SEEK_PUSH_DISPLACE;
3929 entry = gst_index_get_assoc_entry (avi->element_index, avi->index_id,
3930 GST_INDEX_LOOKUP_BEFORE, GST_ASSOCIATION_FLAG_NONE, GST_FORMAT_TIME, cur);
3935 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &byte_cur);
3936 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &time);
3938 GST_DEBUG_OBJECT (avi, "found index entry for %" GST_TIME_FORMAT
3939 " at %" GST_TIME_FORMAT ", located at offset %" G_GINT64_FORMAT,
3940 GST_TIME_ARGS (cur), GST_TIME_ARGS (time), byte_cur);
3942 /* adjust offset to be in each stream's region */
3943 byte_cur = gst_avi_demux_seek_streams (avi, byte_cur, TRUE);
3945 /* let's not try push seeking if target and entry are too far apart */
3946 if (cur - time > 30 * GST_SECOND)
3949 GST_DEBUG_OBJECT (avi, "Pushing BYTE seek rate %g, "
3950 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, byte_cur,
3952 /* BYTE seek event */
3953 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, byte_cur,
3955 res = gst_pad_push_event (avi->sinkpad, event);
3962 GST_DEBUG_OBJECT (avi, "could not determine byte position to seek to, "
3968 GST_DEBUG_OBJECT (avi, "unsupported seek, seek aborted.");
3973 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
3979 * Helper for gst_avi_demux_invert()
3982 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
3984 memcpy (tmp, d1, bytes);
3985 memcpy (d1, d2, bytes);
3986 memcpy (d2, tmp, bytes);
3990 #define gst_avi_demux_is_uncompressed(fourcc) \
3991 (fourcc == GST_RIFF_DIB || \
3992 fourcc == GST_RIFF_rgb || \
3993 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW)
3996 * Invert DIB buffers... Takes existing buffer and
3997 * returns either the buffer or a new one (with old
3998 * one dereferenced).
3999 * FIXME: can't we preallocate tmp? and remember stride, bpp?
4002 gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
4009 if (stream->strh->type != GST_RIFF_FCC_vids)
4012 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
4013 return buf; /* Ignore non DIB buffers */
4016 s = gst_caps_get_structure (GST_PAD_CAPS (stream->pad), 0);
4017 if (!gst_structure_get_int (s, "bpp", &bpp)) {
4018 GST_WARNING ("Failed to retrieve depth from caps");
4022 if (stream->strf.vids == NULL) {
4023 GST_WARNING ("Failed to retrieve vids for stream");
4027 h = stream->strf.vids->height;
4028 w = stream->strf.vids->width;
4029 stride = w * (bpp / 8);
4031 buf = gst_buffer_make_writable (buf);
4032 if (GST_BUFFER_SIZE (buf) < (stride * h)) {
4033 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
4037 tmp = g_malloc (stride);
4039 for (y = 0; y < h / 2; y++) {
4040 swap_line (GST_BUFFER_DATA (buf) + stride * y,
4041 GST_BUFFER_DATA (buf) + stride * (h - 1 - y), tmp, stride);
4050 gst_avi_demux_add_assoc (GstAviDemux * avi, GstAviStream * stream,
4051 GstClockTime timestamp, guint64 offset, gboolean keyframe)
4053 /* do not add indefinitely for open-ended streaming */
4054 if (G_UNLIKELY (avi->element_index && avi->seekable)) {
4055 GST_LOG_OBJECT (avi, "adding association %" GST_TIME_FORMAT "-> %"
4056 G_GUINT64_FORMAT, GST_TIME_ARGS (timestamp), offset);
4057 gst_index_add_association (avi->element_index, avi->index_id,
4058 keyframe ? GST_ASSOCIATION_FLAG_KEY_UNIT : GST_ASSOCIATION_FLAG_NONE,
4059 GST_FORMAT_TIME, timestamp, GST_FORMAT_BYTES, offset, NULL);
4060 /* well, current_total determines TIME and entry DEFAULT (frame #) ... */
4061 gst_index_add_association (avi->element_index, stream->index_id,
4062 GST_ASSOCIATION_FLAG_NONE,
4063 GST_FORMAT_TIME, stream->current_total, GST_FORMAT_BYTES, offset,
4064 GST_FORMAT_DEFAULT, stream->current_entry, NULL);
4069 * Returns the aggregated GstFlowReturn.
4071 static GstFlowReturn
4072 gst_avi_demux_combine_flows (GstAviDemux * avi, GstAviStream * stream,
4076 gboolean unexpected = FALSE, not_linked = TRUE;
4078 /* store the value */
4079 stream->last_flow = ret;
4081 /* any other error that is not-linked or eos can be returned right away */
4082 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
4085 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4086 for (i = 0; i < avi->num_streams; i++) {
4087 GstAviStream *ostream = &avi->stream[i];
4089 ret = ostream->last_flow;
4090 /* no unexpected or unlinked, return */
4091 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
4094 /* we check to see if we have at least 1 unexpected or all unlinked */
4095 unexpected |= (ret == GST_FLOW_UNEXPECTED);
4096 not_linked &= (ret == GST_FLOW_NOT_LINKED);
4098 /* when we get here, we all have unlinked or unexpected */
4100 ret = GST_FLOW_NOT_LINKED;
4101 else if (unexpected)
4102 ret = GST_FLOW_UNEXPECTED;
4104 GST_LOG_OBJECT (avi, "combined %s to return %s",
4105 gst_flow_get_name (stream->last_flow), gst_flow_get_name (ret));
4109 /* move @stream to the next position in its index */
4110 static GstFlowReturn
4111 gst_avi_demux_advance (GstAviDemux * avi, GstAviStream * stream,
4114 guint old_entry, new_entry;
4116 old_entry = stream->current_entry;
4118 new_entry = old_entry + 1;
4120 /* see if we reached the end */
4121 if (new_entry > stream->stop_entry) {
4122 if (avi->segment.rate < 0.0) {
4123 if (stream->step_entry == stream->start_entry) {
4124 /* we stepped all the way to the start, eos */
4125 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
4128 /* backwards, stop becomes step, find a new step */
4129 stream->stop_entry = stream->step_entry;
4130 stream->step_entry = gst_avi_demux_index_prev (avi, stream,
4131 stream->stop_entry, TRUE);
4133 GST_DEBUG_OBJECT (avi,
4134 "reverse playback jump: start %u, step %u, stop %u",
4135 stream->start_entry, stream->step_entry, stream->stop_entry);
4137 /* and start from the previous keyframe now */
4138 new_entry = stream->step_entry;
4141 GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
4146 if (new_entry != old_entry) {
4147 stream->current_entry = new_entry;
4148 stream->current_total = stream->index[new_entry].total;
4150 if (new_entry == old_entry + 1) {
4151 GST_DEBUG_OBJECT (avi, "moved forwards from %u to %u",
4152 old_entry, new_entry);
4153 /* we simply moved one step forwards, reuse current info */
4154 stream->current_timestamp = stream->current_ts_end;
4155 stream->current_offset = stream->current_offset_end;
4156 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4157 NULL, &stream->current_ts_end, NULL, &stream->current_offset_end);
4159 /* we moved DISCONT, full update */
4160 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4161 &stream->current_timestamp, &stream->current_ts_end,
4162 &stream->current_offset, &stream->current_offset_end);
4163 /* and MARK discont for this stream */
4164 stream->last_flow = GST_FLOW_OK;
4165 stream->discont = TRUE;
4166 GST_DEBUG_OBJECT (avi, "Moved from %u to %u, ts %" GST_TIME_FORMAT
4167 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4168 ", off_end %" G_GUINT64_FORMAT, old_entry, new_entry,
4169 GST_TIME_ARGS (stream->current_timestamp),
4170 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4171 stream->current_offset_end);
4179 GST_DEBUG_OBJECT (avi, "we are EOS");
4180 /* setting current_timestamp to -1 marks EOS */
4181 stream->current_timestamp = -1;
4182 return GST_FLOW_UNEXPECTED;
4186 /* find the stream with the lowest current position when going forwards or with
4187 * the highest position when going backwards, this is the stream
4188 * we should push from next */
4190 gst_avi_demux_find_next (GstAviDemux * avi, gfloat rate)
4192 guint64 min_time, max_time;
4193 guint stream_num, i;
4196 min_time = G_MAXUINT64;
4199 for (i = 0; i < avi->num_streams; i++) {
4201 GstAviStream *stream;
4203 stream = &avi->stream[i];
4204 position = stream->current_timestamp;
4206 /* position of -1 is EOS */
4207 if (position != -1) {
4208 if (rate > 0.0 && position < min_time) {
4209 min_time = position;
4211 } else if (rate < 0.0 && position >= max_time) {
4212 max_time = position;
4220 static GstFlowReturn
4221 gst_avi_demux_loop_data (GstAviDemux * avi)
4223 GstFlowReturn ret = GST_FLOW_OK;
4225 GstAviStream *stream;
4226 gboolean processed = FALSE;
4228 guint64 offset, size;
4229 GstClockTime timestamp, duration;
4230 guint64 out_offset, out_offset_end;
4232 GstAviIndexEntry *entry;
4235 stream_num = gst_avi_demux_find_next (avi, avi->segment.rate);
4238 if (G_UNLIKELY (stream_num == -1)) {
4239 GST_DEBUG_OBJECT (avi, "all streams are EOS");
4243 /* we have the stream now */
4244 stream = &avi->stream[stream_num];
4246 /* skip streams without pads */
4248 GST_DEBUG_OBJECT (avi, "skipping entry from stream %d without pad",
4253 /* get the timing info for the entry */
4254 timestamp = stream->current_timestamp;
4255 duration = stream->current_ts_end - timestamp;
4256 out_offset = stream->current_offset;
4257 out_offset_end = stream->current_offset_end;
4259 /* get the entry data info */
4260 entry = &stream->index[stream->current_entry];
4261 offset = entry->offset;
4263 keyframe = ENTRY_IS_KEYFRAME (entry);
4265 /* skip empty entries */
4267 GST_DEBUG_OBJECT (avi, "Skipping entry %u (%" G_GUINT64_FORMAT ", %p)",
4268 stream->current_entry, size, stream->pad);
4272 if (avi->segment.rate > 0.0) {
4273 /* only check this for fowards playback for now */
4274 if (keyframe && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
4275 && (timestamp > avi->segment.stop)) {
4280 GST_LOG ("reading buffer (size=%" G_GUINT64_FORMAT "), stream %d, pos %"
4281 G_GUINT64_FORMAT " (0x%" G_GINT64_MODIFIER "x), kf %d", size,
4282 stream_num, offset, offset, keyframe);
4284 /* FIXME, check large chunks and cut them up */
4286 /* pull in the data */
4287 ret = gst_pad_pull_range (avi->sinkpad, offset, size, &buf);
4288 if (ret != GST_FLOW_OK)
4291 /* check for short buffers, this is EOS as well */
4292 if (GST_BUFFER_SIZE (buf) < size)
4295 /* invert the picture if needed */
4296 buf = gst_avi_demux_invert (stream, buf);
4298 /* mark non-keyframes */
4300 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4302 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4304 GST_BUFFER_TIMESTAMP (buf) = timestamp;
4305 GST_BUFFER_DURATION (buf) = duration;
4306 GST_BUFFER_OFFSET (buf) = out_offset;
4307 GST_BUFFER_OFFSET_END (buf) = out_offset_end;
4309 /* mark discont when pending */
4310 if (stream->discont) {
4311 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4312 stream->discont = FALSE;
4315 gst_avi_demux_add_assoc (avi, stream, timestamp, offset, keyframe);
4317 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4319 /* update current position in the segment */
4320 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, timestamp);
4322 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %u, ts %"
4323 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4324 ", off_end %" G_GUINT64_FORMAT,
4325 GST_BUFFER_SIZE (buf), GST_TIME_ARGS (timestamp),
4326 GST_TIME_ARGS (duration), out_offset, out_offset_end);
4328 ret = gst_pad_push (stream->pad, buf);
4330 /* mark as processed, we increment the frame and byte counters then
4331 * leave the while loop and return the GstFlowReturn */
4334 if (avi->segment.rate < 0) {
4335 if (timestamp > avi->segment.stop && ret == GST_FLOW_UNEXPECTED) {
4336 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
4337 * we are at the end of the segment, so we just need to jump
4338 * back to the previous section. */
4339 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
4344 /* move to next item */
4345 ret = gst_avi_demux_advance (avi, stream, ret);
4348 ret = gst_avi_demux_combine_flows (avi, stream, ret);
4349 } while (!processed);
4357 GST_DEBUG_OBJECT (avi, "No samples left for any streams - EOS");
4358 ret = GST_FLOW_UNEXPECTED;
4363 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
4364 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
4365 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (avi->segment.stop));
4366 ret = GST_FLOW_UNEXPECTED;
4371 GST_DEBUG_OBJECT (avi, "pull range failed: pos=%" G_GUINT64_FORMAT
4372 " size=%" G_GUINT64_FORMAT, offset, size);
4377 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
4378 ", only got %d/%" G_GUINT64_FORMAT " bytes (truncated file?)", offset,
4379 GST_BUFFER_SIZE (buf), size);
4380 gst_buffer_unref (buf);
4381 ret = GST_FLOW_UNEXPECTED;
4387 * Read data. If we have an index it delegates to
4388 * gst_avi_demux_process_next_entry().
4390 static GstFlowReturn
4391 gst_avi_demux_stream_data (GstAviDemux * avi)
4396 GstFlowReturn res = GST_FLOW_OK;
4397 GstFormat format = GST_FORMAT_TIME;
4399 if (G_UNLIKELY (avi->have_eos)) {
4400 /* Clean adapter, we're done */
4401 gst_adapter_clear (avi->adapter);
4402 return GST_FLOW_UNEXPECTED;
4405 if (G_UNLIKELY (avi->todrop)) {
4408 if ((drop = gst_adapter_available (avi->adapter))) {
4409 if (drop > avi->todrop)
4411 GST_DEBUG_OBJECT (avi, "Dropping %d bytes", drop);
4412 gst_adapter_flush (avi->adapter, drop);
4413 avi->todrop -= drop;
4414 avi->offset += drop;
4418 /* Iterate until need more data, so adapter won't grow too much */
4420 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
4424 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
4425 GST_FOURCC_ARGS (tag), size);
4427 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
4428 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
4429 GST_LOG ("Chunk ok");
4430 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
4431 GST_DEBUG ("Found sub-index tag");
4432 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4433 /* accept 0 size buffer here */
4434 avi->abort_buffering = FALSE;
4435 GST_DEBUG (" skipping %d bytes for now", size);
4436 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4439 } else if (tag == GST_RIFF_TAG_idx1) {
4440 GST_DEBUG ("Found index tag, stream done");
4441 avi->have_eos = TRUE;
4442 return GST_FLOW_UNEXPECTED;
4443 } else if (tag == GST_RIFF_TAG_LIST) {
4444 /* movi chunks might be grouped in rec list */
4445 if (gst_adapter_available (avi->adapter) >= 12) {
4446 GST_DEBUG ("Found LIST tag, skipping LIST header");
4447 gst_adapter_flush (avi->adapter, 12);
4451 } else if (tag == GST_RIFF_TAG_JUNK) {
4452 /* rec list might contain JUNK chunks */
4453 GST_DEBUG ("Found JUNK tag");
4454 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4455 /* accept 0 size buffer here */
4456 avi->abort_buffering = FALSE;
4457 GST_DEBUG (" skipping %d bytes for now", size);
4458 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4462 GST_DEBUG ("No more stream chunks, send EOS");
4463 avi->have_eos = TRUE;
4464 return GST_FLOW_UNEXPECTED;
4467 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
4468 /* supposedly one hopes to catch a nicer chunk later on ... */
4469 /* FIXME ?? give up here rather than possibly ending up going
4470 * through the whole file */
4471 if (avi->abort_buffering) {
4472 avi->abort_buffering = FALSE;
4473 gst_adapter_flush (avi->adapter, 8);
4477 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
4478 GST_FOURCC_ARGS (tag), size);
4480 stream_nr = CHUNKID_TO_STREAMNR (tag);
4482 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
4484 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
4485 stream_nr, GST_FOURCC_ARGS (tag));
4486 avi->offset += 8 + GST_ROUND_UP_2 (size);
4487 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4489 GstAviStream *stream;
4490 GstClockTime next_ts = 0;
4494 gst_adapter_flush (avi->adapter, 8);
4497 buf = gst_adapter_take_buffer (avi->adapter, GST_ROUND_UP_2 (size));
4498 /* patch the size */
4499 GST_BUFFER_SIZE (buf) = size;
4500 offset = avi->offset;
4501 avi->offset += 8 + GST_ROUND_UP_2 (size);
4503 stream = &avi->stream[stream_nr];
4505 /* set delay (if any)
4506 if (stream->strh->init_frames == stream->current_frame &&
4508 stream->delay = next_ts;
4511 /* parsing of corresponding header may have failed */
4512 if (G_UNLIKELY (!stream->pad)) {
4513 GST_WARNING_OBJECT (avi, "no pad for stream ID %" GST_FOURCC_FORMAT,
4514 GST_FOURCC_ARGS (tag));
4515 gst_buffer_unref (buf);
4517 GstClockTime dur_ts = 0;
4519 /* get time of this buffer */
4520 gst_pad_query_position (stream->pad, &format, (gint64 *) & next_ts);
4521 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4524 gst_avi_demux_add_assoc (avi, stream, next_ts, offset, FALSE);
4526 /* increment our positions */
4527 stream->current_entry++;
4528 stream->current_total += size;
4530 /* invert the picture if needed */
4531 buf = gst_avi_demux_invert (stream, buf);
4533 gst_pad_query_position (stream->pad, &format, (gint64 *) & dur_ts);
4534 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4537 GST_BUFFER_TIMESTAMP (buf) = next_ts;
4538 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
4539 if (stream->strh->type == GST_RIFF_FCC_vids) {
4540 GST_BUFFER_OFFSET (buf) = stream->current_entry - 1;
4541 GST_BUFFER_OFFSET_END (buf) = stream->current_entry;
4543 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4544 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
4547 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4548 GST_DEBUG_OBJECT (avi,
4549 "Pushing buffer with time=%" GST_TIME_FORMAT ", duration %"
4550 GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
4551 " and size %d over pad %s", GST_TIME_ARGS (next_ts),
4552 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf),
4553 size, GST_PAD_NAME (stream->pad));
4555 /* update current position in the segment */
4556 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, next_ts);
4558 /* mark discont when pending */
4559 if (G_UNLIKELY (stream->discont)) {
4560 GST_DEBUG_OBJECT (avi, "Setting DISCONT");
4561 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4562 stream->discont = FALSE;
4564 res = gst_pad_push (stream->pad, buf);
4567 res = gst_avi_demux_combine_flows (avi, stream, res);
4568 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4569 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
4582 GST_DEBUG_OBJECT (avi, "format %s != GST_FORMAT_TIME",
4583 gst_format_get_name (format));
4584 res = GST_FLOW_ERROR;
4590 * Send pending tags.
4593 push_tag_lists (GstAviDemux * avi)
4601 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
4603 for (i = 0; i < avi->num_streams; i++) {
4604 GstAviStream *stream = &avi->stream[i];
4605 GstPad *pad = stream->pad;
4607 tags = stream->taglist;
4610 GST_DEBUG_OBJECT (pad, "Tags: %" GST_PTR_FORMAT, tags);
4612 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (avi), pad, tags);
4613 stream->taglist = NULL;
4617 if (!(tags = avi->globaltags))
4618 tags = gst_tag_list_new ();
4620 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
4621 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
4623 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, tags);
4624 gst_element_found_tags (GST_ELEMENT_CAST (avi), tags);
4625 avi->globaltags = NULL;
4626 avi->got_tags = FALSE;
4630 gst_avi_demux_loop (GstPad * pad)
4633 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4635 switch (avi->state) {
4636 case GST_AVI_DEMUX_START:
4637 res = gst_avi_demux_stream_init_pull (avi);
4638 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4639 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4642 avi->state = GST_AVI_DEMUX_HEADER;
4644 case GST_AVI_DEMUX_HEADER:
4645 res = gst_avi_demux_stream_header_pull (avi);
4646 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4647 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4650 avi->state = GST_AVI_DEMUX_MOVI;
4652 case GST_AVI_DEMUX_MOVI:
4653 if (G_UNLIKELY (avi->seg_event)) {
4654 gst_avi_demux_push_event (avi, avi->seg_event);
4655 avi->seg_event = NULL;
4657 if (G_UNLIKELY (avi->got_tags)) {
4658 push_tag_lists (avi);
4660 /* process each index entry in turn */
4661 res = gst_avi_demux_loop_data (avi);
4663 /* pause when error */
4664 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4665 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
4670 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
4671 res = GST_FLOW_ERROR;
4679 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
4680 avi->segment_running = FALSE;
4681 gst_pad_pause_task (avi->sinkpad);
4683 if (GST_FLOW_IS_FATAL (res) || (res == GST_FLOW_NOT_LINKED)) {
4684 gboolean push_eos = TRUE;
4686 if (res == GST_FLOW_UNEXPECTED) {
4687 /* handle end-of-stream/segment */
4688 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4691 if ((stop = avi->segment.stop) == -1)
4692 stop = avi->segment.duration;
4694 GST_INFO_OBJECT (avi, "sending segment_done");
4696 gst_element_post_message
4697 (GST_ELEMENT_CAST (avi),
4698 gst_message_new_segment_done (GST_OBJECT_CAST (avi),
4699 GST_FORMAT_TIME, stop));
4703 /* for fatal errors we post an error message */
4704 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
4705 (_("Internal data stream error.")),
4706 ("streaming stopped, reason %s", gst_flow_get_name (res)));
4709 GST_INFO_OBJECT (avi, "sending eos");
4710 if (!gst_avi_demux_push_event (avi, gst_event_new_eos ()) &&
4711 (res == GST_FLOW_UNEXPECTED)) {
4712 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
4713 (NULL), ("got eos but no streams (yet)"));
4720 static GstFlowReturn
4721 gst_avi_demux_chain (GstPad * pad, GstBuffer * buf)
4724 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4726 GST_DEBUG ("Store %d bytes in adapter", GST_BUFFER_SIZE (buf));
4727 gst_adapter_push (avi->adapter, buf);
4729 switch (avi->state) {
4730 case GST_AVI_DEMUX_START:
4731 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
4732 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4736 case GST_AVI_DEMUX_HEADER:
4737 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
4738 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4742 case GST_AVI_DEMUX_MOVI:
4743 if (G_UNLIKELY (avi->seg_event)) {
4744 gst_avi_demux_push_event (avi, avi->seg_event);
4745 avi->seg_event = NULL;
4747 if (G_UNLIKELY (avi->got_tags)) {
4748 push_tag_lists (avi);
4750 res = gst_avi_demux_stream_data (avi);
4753 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
4754 ("Illegal internal state"));
4755 res = GST_FLOW_ERROR;
4759 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
4760 gst_flow_get_name (res));
4762 if (G_UNLIKELY (avi->abort_buffering)) {
4763 avi->abort_buffering = FALSE;
4764 res = GST_FLOW_ERROR;
4765 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
4772 gst_avi_demux_sink_activate (GstPad * sinkpad)
4774 if (gst_pad_check_pull_range (sinkpad)) {
4775 GST_DEBUG ("going to pull mode");
4776 return gst_pad_activate_pull (sinkpad, TRUE);
4778 GST_DEBUG ("going to push (streaming) mode");
4779 return gst_pad_activate_push (sinkpad, TRUE);
4784 gst_avi_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
4786 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (sinkpad));
4789 avi->segment_running = TRUE;
4790 avi->streaming = FALSE;
4791 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4794 avi->segment_running = FALSE;
4795 return gst_pad_stop_task (sinkpad);
4800 gst_avi_demux_activate_push (GstPad * pad, gboolean active)
4802 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (pad));
4805 GST_DEBUG ("avi: activating push/chain function");
4806 avi->streaming = TRUE;
4808 /* create index for some push based seeking if not provided */
4809 GST_OBJECT_LOCK (avi);
4810 if (!avi->element_index) {
4811 GST_DEBUG_OBJECT (avi, "creating index");
4812 avi->element_index = gst_index_factory_make ("memindex");
4814 GST_OBJECT_UNLOCK (avi);
4815 /* object lock might be taken again */
4816 gst_index_get_writer_id (avi->element_index, GST_OBJECT_CAST (avi),
4820 GST_DEBUG ("avi: deactivating push/chain function");
4827 gst_avi_demux_set_index (GstElement * element, GstIndex * index)
4829 GstAviDemux *avi = GST_AVI_DEMUX (element);
4831 GST_OBJECT_LOCK (avi);
4832 if (avi->element_index)
4833 gst_object_unref (avi->element_index);
4835 avi->element_index = gst_object_ref (index);
4837 avi->element_index = NULL;
4839 GST_OBJECT_UNLOCK (avi);
4840 /* object lock might be taken again */
4842 gst_index_get_writer_id (index, GST_OBJECT_CAST (element), &avi->index_id);
4843 GST_DEBUG_OBJECT (avi, "Set index %" GST_PTR_FORMAT, avi->element_index);
4847 gst_avi_demux_get_index (GstElement * element)
4849 GstIndex *result = NULL;
4850 GstAviDemux *avi = GST_AVI_DEMUX (element);
4852 GST_OBJECT_LOCK (avi);
4853 if (avi->element_index)
4854 result = gst_object_ref (avi->element_index);
4855 GST_OBJECT_UNLOCK (avi);
4857 GST_DEBUG_OBJECT (avi, "Returning index %" GST_PTR_FORMAT, result);
4862 static GstStateChangeReturn
4863 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
4865 GstStateChangeReturn ret;
4866 GstAviDemux *avi = GST_AVI_DEMUX (element);
4868 switch (transition) {
4869 case GST_STATE_CHANGE_READY_TO_PAUSED:
4870 avi->streaming = FALSE;
4871 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
4877 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4878 if (ret == GST_STATE_CHANGE_FAILURE)
4881 switch (transition) {
4882 case GST_STATE_CHANGE_PAUSED_TO_READY:
4883 gst_avi_demux_reset (avi);