2 * Copyright (C) <1999> Erik Walthinsen <omega@temple-baptist.com>
3 * Copyright (C) <2006> Nokia Corporation (contact <stefan.kost@nokia.com>)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 /* Element-Checklist-Version: 5 */
23 * SECTION:element-avidemux
25 * Demuxes an .avi file into raw or compressed audio and/or video streams.
27 * This element supports both push and pull-based scheduling, depending on the
28 * capabilities of the upstream elements.
31 * <title>Example launch line</title>
33 * gst-launch filesrc location=test.avi ! avidemux name=demux demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink demux.video_00 ! queue ! decodebin ! ffmpegcolorspace ! videoscale ! autovideosink
34 * ]| Play (parse and decode) an .avi file and try to output it to
35 * an automatically detected soundcard and videosink. If the AVI file contains
36 * compressed audio or video data, this will only work if you have the
37 * right decoder elements/plugins installed.
40 * Last reviewed on 2006-12-29 (0.10.6)
49 #include "gst/riff/riff-media.h"
50 #include "gstavidemux.h"
52 #include <gst/gst-i18n-plugin.h>
53 #include <gst/base/gstadapter.h>
56 #define DIV_ROUND_UP(s,v) ((s) + ((v)-1) / (v))
58 GST_DEBUG_CATEGORY_STATIC (avidemux_debug);
59 #define GST_CAT_DEFAULT avidemux_debug
60 GST_DEBUG_CATEGORY_STATIC (GST_CAT_PERFORMANCE);
62 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_EVENT);
64 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
67 GST_STATIC_CAPS ("video/x-msvideo")
70 static void gst_avi_demux_base_init (GstAviDemuxClass * klass);
71 static void gst_avi_demux_class_init (GstAviDemuxClass * klass);
72 static void gst_avi_demux_init (GstAviDemux * avi);
73 static void gst_avi_demux_finalize (GObject * object);
75 static void gst_avi_demux_reset (GstAviDemux * avi);
78 static const GstEventMask *gst_avi_demux_get_event_mask (GstPad * pad);
80 static gboolean gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event);
81 static gboolean gst_avi_demux_handle_sink_event (GstPad * pad,
83 static gboolean gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event);
86 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
88 static const GstQueryType *gst_avi_demux_get_src_query_types (GstPad * pad);
89 static gboolean gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query);
90 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
91 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
93 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
94 static gboolean gst_avi_demux_handle_seek (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 GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
104 GstStateChange transition);
106 static GstElementClass *parent_class = NULL;
108 /* GObject methods */
111 gst_avi_demux_get_type (void)
113 static GType avi_demux_type = 0;
115 if (!avi_demux_type) {
116 static const GTypeInfo avi_demux_info = {
117 sizeof (GstAviDemuxClass),
118 (GBaseInitFunc) gst_avi_demux_base_init,
120 (GClassInitFunc) gst_avi_demux_class_init,
123 sizeof (GstAviDemux),
125 (GInstanceInitFunc) gst_avi_demux_init,
129 g_type_register_static (GST_TYPE_ELEMENT,
130 "GstAviDemux", &avi_demux_info, 0);
133 return avi_demux_type;
137 gst_avi_demux_base_init (GstAviDemuxClass * klass)
139 static const GstElementDetails gst_avi_demux_details =
140 GST_ELEMENT_DETAILS ("Avi demuxer",
142 "Demultiplex an avi file into audio and video",
143 "Erik Walthinsen <omega@cse.ogi.edu>\n"
144 "Wim Taymans <wim.taymans@chello.be>\n"
145 "Thijs Vermeir <thijsvermeir@gmail.com>");
146 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
147 GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl;
148 GstCaps *audcaps, *vidcaps, *subcaps;
150 audcaps = gst_riff_create_audio_template_caps ();
151 gst_caps_append (audcaps, gst_caps_new_simple ("audio/x-avi-unknown", NULL));
152 audiosrctempl = gst_pad_template_new ("audio_%02d",
153 GST_PAD_SRC, GST_PAD_SOMETIMES, audcaps);
155 vidcaps = gst_riff_create_video_template_caps ();
156 gst_caps_append (vidcaps, gst_riff_create_iavs_template_caps ());
157 gst_caps_append (vidcaps, gst_caps_new_simple ("video/x-avi-unknown", NULL));
158 videosrctempl = gst_pad_template_new ("video_%02d",
159 GST_PAD_SRC, GST_PAD_SOMETIMES, vidcaps);
161 subcaps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
162 subsrctempl = gst_pad_template_new ("subtitle_%02d",
163 GST_PAD_SRC, GST_PAD_SOMETIMES, subcaps);
164 gst_element_class_add_pad_template (element_class, audiosrctempl);
165 gst_element_class_add_pad_template (element_class, videosrctempl);
166 gst_element_class_add_pad_template (element_class, subsrctempl);
167 gst_element_class_add_pad_template (element_class,
168 gst_static_pad_template_get (&sink_templ));
169 gst_element_class_set_details (element_class, &gst_avi_demux_details);
173 gst_avi_demux_class_init (GstAviDemuxClass * klass)
175 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
176 GObjectClass *gobject_class = (GObjectClass *) klass;
178 GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
179 0, "Demuxer for AVI streams");
180 GST_DEBUG_CATEGORY_GET (GST_CAT_PERFORMANCE, "GST_PERFORMANCE");
182 parent_class = g_type_class_peek_parent (klass);
184 gobject_class->finalize = gst_avi_demux_finalize;
185 gstelement_class->change_state =
186 GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
190 gst_avi_demux_init (GstAviDemux * avi)
192 avi->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
193 gst_pad_set_activate_function (avi->sinkpad,
194 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate));
195 gst_pad_set_activatepull_function (avi->sinkpad,
196 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate_pull));
197 gst_pad_set_activatepush_function (avi->sinkpad,
198 GST_DEBUG_FUNCPTR (gst_avi_demux_activate_push));
199 gst_pad_set_chain_function (avi->sinkpad,
200 GST_DEBUG_FUNCPTR (gst_avi_demux_chain));
201 gst_pad_set_event_function (avi->sinkpad,
202 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_sink_event));
203 gst_element_add_pad (GST_ELEMENT (avi), avi->sinkpad);
205 avi->adapter = gst_adapter_new ();
207 gst_avi_demux_reset (avi);
211 gst_avi_demux_finalize (GObject * object)
213 GstAviDemux *avi = GST_AVI_DEMUX (object);
215 GST_DEBUG ("AVI: finalize");
217 g_object_unref (avi->adapter);
219 G_OBJECT_CLASS (parent_class)->finalize (object);
223 gst_avi_demux_reset (GstAviDemux * avi)
227 GST_DEBUG ("AVI: reset");
229 for (i = 0; i < avi->num_streams; i++) {
230 g_free (avi->stream[i].strh);
231 g_free (avi->stream[i].strf.data);
232 if (avi->stream[i].name)
233 g_free (avi->stream[i].name);
234 if (avi->stream[i].initdata)
235 gst_buffer_unref (avi->stream[i].initdata);
236 if (avi->stream[i].extradata)
237 gst_buffer_unref (avi->stream[i].extradata);
238 if (avi->stream[i].pad) {
239 gst_pad_set_active (avi->stream[i].pad, FALSE);
240 gst_element_remove_pad (GST_ELEMENT (avi), avi->stream[i].pad);
242 if (avi->stream[i].taglist) {
243 gst_tag_list_free (avi->stream[i].taglist);
244 avi->stream[i].taglist = NULL;
247 memset (&avi->stream, 0, sizeof (avi->stream));
249 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
250 avi->num_streams = 0;
251 avi->num_v_streams = 0;
252 avi->num_a_streams = 0;
253 avi->num_t_streams = 0;
255 avi->state = GST_AVI_DEMUX_START;
258 //g_free (avi->index_entries);
259 //avi->index_entries = NULL;
260 //avi->index_size = 0;
261 avi->index_offset = 0;
265 if (avi->seek_event) {
266 gst_event_unref (avi->seek_event);
267 avi->seek_event = NULL;
271 gst_tag_list_free (avi->globaltags);
272 avi->globaltags = NULL;
274 avi->got_tags = TRUE; /* we always want to push global tags */
275 avi->have_eos = FALSE;
277 gst_adapter_clear (avi->adapter);
279 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
283 /* GstElement methods */
286 static const GstFormat *
287 gst_avi_demux_get_src_formats (GstPad * pad)
289 GstAviStream *stream = gst_pad_get_element_private (pad);
291 static const GstFormat src_a_formats[] = {
297 static const GstFormat src_v_formats[] = {
303 return (stream->strh->type == GST_RIFF_FCC_auds ?
304 src_a_formats : src_v_formats);
308 /* assumes stream->strf.auds->av_bps != 0 */
309 static inline GstClockTime
310 avi_stream_convert_bytes_to_time_unchecked (GstAviStream * stream,
313 return gst_util_uint64_scale (bytes, GST_SECOND, stream->strf.auds->av_bps);
316 static inline guint64
317 avi_stream_convert_time_to_bytes_unchecked (GstAviStream * stream,
320 return gst_util_uint64_scale (time, stream->strf.auds->av_bps, GST_SECOND);
323 /* assumes stream->strh->rate != 0 */
324 static inline GstClockTime
325 avi_stream_convert_frames_to_time_unchecked (GstAviStream * stream,
328 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
332 static inline guint64
333 avi_stream_convert_time_to_frames_unchecked (GstAviStream * stream,
336 return gst_util_uint64_scale (time, stream->strh->rate,
337 stream->strh->scale * GST_SECOND);
341 gst_avi_demux_src_convert (GstPad * pad,
342 GstFormat src_format,
343 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
345 GstAviStream *stream = gst_pad_get_element_private (pad);
349 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
350 ", dest_format:%s", gst_format_get_name (src_format), src_value,
351 gst_format_get_name (*dest_format));
353 if (G_UNLIKELY (src_format == *dest_format)) {
354 *dest_value = src_value;
357 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
361 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
362 (src_format == GST_FORMAT_BYTES
363 || *dest_format == GST_FORMAT_BYTES))) {
368 switch (src_format) {
369 case GST_FORMAT_TIME:
370 switch (*dest_format) {
371 case GST_FORMAT_BYTES:
372 *dest_value = gst_util_uint64_scale (src_value,
373 (guint64) stream->strf.auds->av_bps, GST_SECOND);
375 case GST_FORMAT_DEFAULT:
377 gst_util_uint64_scale_round (src_value, stream->strh->rate,
378 stream->strh->scale * GST_SECOND);
385 case GST_FORMAT_BYTES:
386 switch (*dest_format) {
387 case GST_FORMAT_TIME:
388 if (stream->strf.auds->av_bps != 0) {
389 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
399 case GST_FORMAT_DEFAULT:
400 switch (*dest_format) {
401 case GST_FORMAT_TIME:
403 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
416 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
417 gst_format_get_name (*dest_format), *dest_value);
421 static const GstQueryType *
422 gst_avi_demux_get_src_query_types (GstPad * pad)
424 static const GstQueryType src_types[] = {
436 gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query)
439 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
441 GstAviStream *stream = gst_pad_get_element_private (pad);
443 if (!stream->strh || !stream->strf.data)
444 return gst_pad_query_default (pad, query);
446 switch (GST_QUERY_TYPE (query)) {
447 case GST_QUERY_POSITION:{
450 GST_DEBUG ("pos query for stream %d: frames %d, bytes %" G_GUINT64_FORMAT,
451 stream->num, stream->current_frame, stream->current_byte);
453 if (stream->strh->type == GST_RIFF_FCC_auds) {
454 if (stream->is_vbr) {
456 pos = gst_util_uint64_scale ((gint64) stream->current_frame *
457 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
458 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
459 GST_TIME_FORMAT, stream->current_frame, GST_TIME_ARGS (pos));
460 } else if (stream->strf.auds->av_bps != 0) {
462 pos = gst_util_uint64_scale (stream->current_byte, GST_SECOND,
463 (guint64) stream->strf.auds->av_bps);
464 GST_DEBUG_OBJECT (avi,
465 "CBR convert bytes %" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT,
466 stream->current_byte, GST_TIME_ARGS (pos));
467 } else if (stream->idx_n != 0 && stream->total_bytes != 0) {
468 /* calculate timestamps based on percentage of length */
469 guint64 xlen = avi->avih->us_frame *
470 avi->avih->tot_frames * GST_USECOND;
472 if (stream->is_vbr) {
473 pos = gst_util_uint64_scale (xlen, stream->current_frame,
475 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
476 GST_TIME_FORMAT, stream->current_frame, GST_TIME_ARGS (pos));
478 pos = gst_util_uint64_scale (xlen, stream->current_byte,
479 stream->total_bytes);
480 GST_DEBUG_OBJECT (avi, "CBR perc convert bytes %" G_GUINT64_FORMAT
481 ", time %" GST_TIME_FORMAT, stream->current_byte,
482 GST_TIME_ARGS (pos));
489 if (stream->strh->rate != 0) {
490 pos = gst_util_uint64_scale ((guint64) stream->current_frame *
491 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
493 pos = stream->current_frame * avi->avih->us_frame * GST_USECOND;
497 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
498 gst_query_set_position (query, GST_FORMAT_TIME, pos);
500 GST_WARNING ("pos query failed");
503 case GST_QUERY_DURATION:
507 if (stream->strh->type != GST_RIFF_FCC_auds &&
508 stream->strh->type != GST_RIFF_FCC_vids) {
513 gst_query_parse_duration (query, &fmt, NULL);
516 case GST_FORMAT_TIME:
517 gst_query_set_duration (query, fmt, stream->duration);
519 case GST_FORMAT_DEFAULT:
522 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
525 if (stream->idx_n >= 0)
526 gst_query_set_duration (query, fmt, stream->idx_n);
527 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
528 stream->duration, &fmt, &dur))
529 gst_query_set_duration (query, fmt, dur);
538 case GST_QUERY_SEEKING:{
541 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
542 if (fmt == GST_FORMAT_TIME) {
543 gboolean seekable = TRUE;
545 if (avi->streaming) {
549 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
550 0, stream->duration);
555 case GST_QUERY_CONVERT:{
556 GstFormat src_fmt, dest_fmt;
557 gint64 src_val, dest_val;
559 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
560 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
562 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
564 res = gst_pad_query_default (pad, query);
568 res = gst_pad_query_default (pad, query);
572 gst_object_unref (avi);
577 static const GstEventMask *
578 gst_avi_demux_get_event_mask (GstPad * pad)
580 static const GstEventMask masks[] = {
581 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
590 gst_avi_demux_handle_sink_event (GstPad * pad, GstEvent * event)
593 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
595 GST_DEBUG_OBJECT (avi,
596 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
598 switch (GST_EVENT_TYPE (event)) {
599 case GST_EVENT_NEWSEGMENT:
600 /* Drop NEWSEGMENT events, new ones are generated later */
601 gst_event_unref (event);
605 if (avi->state != GST_AVI_DEMUX_MOVI) {
606 gst_event_unref (event);
607 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
608 (NULL), ("got eos and didn't receive a complete header object"));
609 } else if (!gst_avi_demux_push_event (avi, event)) {
610 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
611 (NULL), ("got eos but no streams (yet)"));
616 res = gst_pad_event_default (pad, event);
620 gst_object_unref (avi);
626 gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event)
629 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
631 GST_DEBUG_OBJECT (avi,
632 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
634 switch (GST_EVENT_TYPE (event)) {
636 /* handle seeking only in pull mode */
637 if (!avi->streaming) {
638 res = gst_avi_demux_handle_seek (avi, pad, event);
639 gst_event_unref (event);
641 res = gst_pad_event_default (pad, event);
645 case GST_EVENT_NAVIGATION:
647 gst_event_unref (event);
650 res = gst_pad_event_default (pad, event);
654 gst_object_unref (avi);
659 /* streaming helper (push) */
662 * gst_avi_demux_peek_chunk_info:
664 * @tag: holder for tag
665 * @size: holder for tag size
667 * Peek next chunk info (tag and size)
669 * Returns: TRUE when one chunk info has been got
672 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
674 const guint8 *data = NULL;
676 if (gst_adapter_available (avi->adapter) < 8)
679 data = gst_adapter_peek (avi->adapter, 8);
680 *tag = GST_READ_UINT32_LE (data);
681 *size = GST_READ_UINT32_LE (data + 4);
687 * gst_avi_demux_peek_chunk:
689 * @tag: holder for tag
690 * @size: holder for tag size
692 * Peek enough data for one full chunk
694 * Returns: %TRUE when one chunk has been got
697 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
699 guint32 peek_size = 0;
702 if (!gst_avi_demux_peek_chunk_info (avi, tag, size))
705 /* size 0 -> empty data buffer would surprise most callers,
706 * large size -> do not bother trying to squeeze that into adapter,
707 * so we throw poor man's exception, which can be caught if caller really
708 * wants to handle 0 size chunk */
709 if (!(*size) || (*size) >= (1 << 30))
712 peek_size = (*size + 1) & ~1;
713 available = gst_adapter_available (avi->adapter);
715 GST_DEBUG_OBJECT (avi,
716 "Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
717 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
719 if (available < (8 + peek_size))
727 GST_INFO_OBJECT (avi, "Failed to peek");
732 GST_INFO_OBJECT (avi,
733 "Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT, *size,
734 GST_FOURCC_ARGS (*tag));
735 /* chain should give up */
736 avi->abort_buffering = TRUE;
741 GST_INFO_OBJECT (avi, "need more %d < %" G_GUINT32_FORMAT,
742 available, 8 + peek_size);
750 * gst_avi_demux_parse_file_header:
751 * @element: caller element (used for errors/debug).
752 * @buf: input data to be used for parsing.
754 * "Open" a RIFF/AVI file. The buffer should be at least 12
755 * bytes long. Takes ownership of @buf.
757 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
758 * Throws an error, caller should error out (fatal).
761 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
766 stamp = gst_util_get_timestamp ();
768 /* riff_parse posts an error */
769 if (!gst_riff_parse_file_header (element, buf, &doctype))
772 if (doctype != GST_RIFF_RIFF_AVI)
775 stamp = gst_util_get_timestamp () - stamp;
776 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "parsing header %" GST_TIME_FORMAT,
777 GST_TIME_ARGS (stamp));
784 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
785 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
786 GST_FOURCC_ARGS (doctype)));
792 * Read AVI file tag when streaming
795 gst_avi_demux_stream_init_push (GstAviDemux * avi)
797 if (gst_adapter_available (avi->adapter) >= 12) {
800 tmp = gst_adapter_take_buffer (avi->adapter, 12);
802 GST_DEBUG ("Parsing avi header");
803 if (!gst_avi_demux_parse_file_header (GST_ELEMENT (avi), tmp)) {
804 return GST_FLOW_ERROR;
806 GST_DEBUG ("header ok");
809 avi->state = GST_AVI_DEMUX_HEADER;
818 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
821 GstBuffer *buf = NULL;
823 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
824 if (res != GST_FLOW_OK)
826 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
836 GST_DEBUG_OBJECT (avi, "error parsing file header");
837 return GST_FLOW_ERROR;
841 /* AVI header handling */
844 * gst_avi_demux_parse_avih:
845 * @element: caller element (used for errors/debug).
846 * @buf: input data to be used for parsing.
847 * @avih: pointer to structure (filled in by function) containing
848 * stream information (such as flags, number of streams, etc.).
850 * Read 'avih' header. Discards buffer after use.
852 * Returns: TRUE on success, FALSE otherwise. Throws an error if
853 * the header is invalid. The caller should error out
857 gst_avi_demux_parse_avih (GstElement * element,
858 GstBuffer * buf, gst_riff_avih ** _avih)
865 if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_avih))
868 avih = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
870 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
871 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
872 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
873 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
874 avih->flags = GUINT32_FROM_LE (avih->flags);
875 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
876 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
877 avih->streams = GUINT32_FROM_LE (avih->streams);
878 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
879 avih->width = GUINT32_FROM_LE (avih->width);
880 avih->height = GUINT32_FROM_LE (avih->height);
881 avih->scale = GUINT32_FROM_LE (avih->scale);
882 avih->rate = GUINT32_FROM_LE (avih->rate);
883 avih->start = GUINT32_FROM_LE (avih->start);
884 avih->length = GUINT32_FROM_LE (avih->length);
888 GST_INFO_OBJECT (element, "avih tag found:");
889 GST_INFO_OBJECT (element, " us_frame %u", avih->us_frame);
890 GST_INFO_OBJECT (element, " max_bps %u", avih->max_bps);
891 GST_INFO_OBJECT (element, " pad_gran %u", avih->pad_gran);
892 GST_INFO_OBJECT (element, " flags 0x%08x", avih->flags);
893 GST_INFO_OBJECT (element, " tot_frames %u", avih->tot_frames);
894 GST_INFO_OBJECT (element, " init_frames %u", avih->init_frames);
895 GST_INFO_OBJECT (element, " streams %u", avih->streams);
896 GST_INFO_OBJECT (element, " bufsize %u", avih->bufsize);
897 GST_INFO_OBJECT (element, " width %u", avih->width);
898 GST_INFO_OBJECT (element, " height %u", avih->height);
899 GST_INFO_OBJECT (element, " scale %u", avih->scale);
900 GST_INFO_OBJECT (element, " rate %u", avih->rate);
901 GST_INFO_OBJECT (element, " start %u", avih->start);
902 GST_INFO_OBJECT (element, " length %u", avih->length);
905 gst_buffer_unref (buf);
912 GST_ELEMENT_ERROR (element, STREAM, DEMUX, (NULL), ("No buffer"));
917 GST_ELEMENT_ERROR (element, STREAM, DEMUX, (NULL),
918 ("Too small avih (%d available, %d needed)",
919 GST_BUFFER_SIZE (buf), (int) sizeof (gst_riff_avih)));
920 gst_buffer_unref (buf);
926 * gst_avi_demux_parse_superindex:
927 * @avi: caller element (used for debugging/errors).
928 * @buf: input data to use for parsing.
929 * @locations: locations in the file (byte-offsets) that contain
930 * the actual indexes (see get_avi_demux_parse_subindex()).
931 * The array ends with GST_BUFFER_OFFSET_NONE.
933 * Reads superindex (openDML-2 spec stuff) from the provided data.
935 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
936 * on error, but they are not fatal.
939 gst_avi_demux_parse_superindex (GstAviDemux * avi,
940 GstBuffer * buf, guint64 ** _indexes)
950 size = buf ? GST_BUFFER_SIZE (buf) : 0;
954 data = GST_BUFFER_DATA (buf);
956 /* check type of index. The opendml2 specs state that
957 * there should be 4 dwords per array entry. Type can be
958 * either frame or field (and we don't care). */
959 if (GST_READ_UINT16_LE (data) != 4 ||
960 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
961 GST_WARNING_OBJECT (avi,
962 "Superindex for stream has unexpected "
963 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
964 GST_READ_UINT16_LE (data), data[2], data[3]);
965 bpe = GST_READ_UINT16_LE (data) * 4;
967 num = GST_READ_UINT32_LE (&data[4]);
969 indexes = g_new (guint64, num + 1);
970 for (i = 0; i < num; i++) {
971 if (size < 24 + bpe * (i + 1))
973 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
975 indexes[i] = GST_BUFFER_OFFSET_NONE;
978 gst_buffer_unref (buf);
985 GST_ERROR_OBJECT (avi,
986 "Not enough data to parse superindex (%d available, 24 needed)", size);
988 gst_buffer_unref (buf);
995 * gst_avi_demux_parse_subindex:
997 * @buf: input data to use for parsing.
998 * @stream: stream context.
999 * @entries_list: a list (returned by the function) containing all the
1000 * indexes parsed in this specific subindex. The first
1001 * entry is also a pointer to allocated memory that needs
1002 * to be free´ed. May be NULL if no supported indexes were
1005 * Reads superindex (openDML-2 spec stuff) from the provided data.
1006 * The buffer will be discarded after use.
1007 * The buffer should contain a GST_RIFF_TAG_ix?? chunk.
1009 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1010 * throw an error, caller should bail out asap.
1013 gst_avi_demux_parse_subindex (GstAviDemux * avi,
1014 GstBuffer * buf, GstAviStream * stream, GList ** _entries_list)
1016 guint8 *data = GST_BUFFER_DATA (buf);
1020 gst_avi_index_entry *entries, *entry;
1021 GList *entries_list = NULL;
1024 #ifndef GST_DISABLE_GST_DEBUG
1025 gulong _nr_keyframes = 0;
1028 *_entries_list = NULL;
1030 size = buf ? GST_BUFFER_SIZE (buf) : 0;
1036 /* We don't support index-data yet */
1038 goto not_implemented;
1040 /* check type of index. The opendml2 specs state that
1041 * there should be 4 dwords per array entry. Type can be
1042 * either frame or field (and we don't care). */
1043 bpe = (data[2] & 0x01) ? 12 : 8;
1044 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1045 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1046 GST_WARNING_OBJECT (avi,
1047 "Superindex for stream %d has unexpected "
1048 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1049 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1050 bpe = GST_READ_UINT16_LE (data) * 4;
1052 num = GST_READ_UINT32_LE (&data[4]);
1053 baseoff = GST_READ_UINT64_LE (&data[12]);
1055 /* If there's nothing, just return ! */
1059 if (!(entries = g_try_new (gst_avi_index_entry, num)))
1062 GST_INFO_OBJECT (avi, "Parsing subindex, nr_entries = %6d", num);
1064 for (i = 0; i < num; i++) {
1067 entry = &entries[i];
1069 if (size < 24 + bpe * (i + 1))
1073 entry->offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1074 entry->size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1076 (entry->size & 0x80000000) ? 0 : GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
1077 entry->size &= ~0x80000000;
1078 entry->index_nr = i;
1079 entry->stream_nr = stream->num;
1081 if (stream->strh->type == GST_RIFF_FCC_auds) {
1082 /* all audio frames are keyframes */
1083 entry->flags |= GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
1085 #ifndef GST_DISABLE_GST_DEBUG
1086 if (entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)
1090 /* stream duration unknown, now we can calculate it */
1091 if (stream->idx_duration == -1)
1092 stream->idx_duration = 0;
1095 entry->ts = stream->idx_duration;
1096 if (stream->is_vbr) {
1097 /* VBR stream next timestamp */
1098 if (stream->strh->type == GST_RIFF_FCC_auds) {
1099 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
1100 stream->total_blocks + 1);
1102 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
1106 /* CBR get next timestamp */
1107 next_ts = avi_stream_convert_bytes_to_time_unchecked (stream,
1108 stream->total_bytes + entry->size);
1110 /* duration is next - current */
1111 entry->dur = next_ts - entry->ts;
1113 /* stream position */
1114 entry->bytes_before = stream->total_bytes;
1115 entry->frames_before = stream->idx_n;
1117 stream->total_bytes += entry->size;
1119 if (stream->strh->type == GST_RIFF_FCC_auds) {
1120 if (stream->strf.auds->blockalign > 0)
1121 stream->total_blocks +=
1122 (entry->size + stream->strf.auds->blockalign -
1123 1) / stream->strf.auds->blockalign;
1125 stream->total_blocks++;
1127 stream->idx_duration = next_ts;
1129 entries_list = g_list_prepend (entries_list, entry);
1132 GST_INFO_OBJECT (avi, "Parsed index, %6u/%6u entries, %5lu keyframes, "
1133 "entry size = %2u, total size = %10d", i, num, _nr_keyframes,
1134 (gint) sizeof (gst_avi_index_entry),
1135 (gint) (i * sizeof (gst_avi_index_entry)));
1137 gst_buffer_unref (buf);
1140 *_entries_list = g_list_reverse (entries_list);
1150 GST_ERROR_OBJECT (avi,
1151 "Not enough data to parse subindex (%d available, 24 needed)", size);
1153 gst_buffer_unref (buf);
1154 return TRUE; /* continue */
1158 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1159 ("Subindex-is-data is not implemented"));
1160 gst_buffer_unref (buf);
1165 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1166 ("Cannot allocate memory for %u*%u=%u bytes",
1167 (guint) sizeof (gst_avi_index_entry), num,
1168 (guint) sizeof (gst_avi_index_entry) * num));
1169 gst_buffer_unref (buf);
1177 * Read AVI index when streaming
1180 gst_avi_demux_read_subindexes_push (GstAviDemux * avi,
1181 GList ** index, GList ** alloc_list)
1184 guint32 tag = 0, size;
1185 GstBuffer *buf = NULL;
1188 GST_DEBUG_OBJECT (avi, "gst_avi_demux_read_subindexes_push for %d streams",
1191 for (n = 0; n < avi->num_streams; n++) {
1192 GstAviStream *stream = &avi->stream[n];
1194 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1195 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1197 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1198 '0' + stream->num % 10)) &&
1199 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1200 '0' + stream->num % 10, 'i', 'x'))) {
1201 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1202 GST_FOURCC_ARGS (tag));
1206 avi->offset += 8 + GST_ROUND_UP_2 (size);
1208 buf = gst_buffer_new ();
1209 GST_BUFFER_DATA (buf) = gst_adapter_take (avi->adapter, size);
1210 GST_BUFFER_SIZE (buf) = size;
1212 if (!gst_avi_demux_parse_subindex (avi, buf, stream, &list))
1215 GST_DEBUG_OBJECT (avi, " adding %d entries", g_list_length (list));
1216 *alloc_list = g_list_append (*alloc_list, list->data);
1217 *index = g_list_concat (*index, list);
1221 g_free (stream->indexes);
1222 stream->indexes = NULL;
1224 GST_DEBUG_OBJECT (avi, "index %s", ((*index) ? "!= 0" : "== 0"));
1233 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi,
1234 GList ** index, GList ** alloc_list)
1241 GST_DEBUG_OBJECT (avi, "gst_avi_demux_read_subindexes_pull for %d streams",
1244 for (n = 0; n < avi->num_streams; n++) {
1245 GstAviStream *stream = &avi->stream[n];
1247 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1248 if (gst_riff_read_chunk (GST_ELEMENT (avi), avi->sinkpad,
1249 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1251 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1252 '0' + stream->num % 10)) &&
1253 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1254 '0' + stream->num % 10, 'i', 'x'))) {
1255 /* Some ODML files (created by god knows what muxer) have a ##ix format
1256 * instead of the 'official' ix##. They are still valid though. */
1257 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1258 GST_FOURCC_ARGS (tag));
1259 gst_buffer_unref (buf);
1263 if (!gst_avi_demux_parse_subindex (avi, buf, stream, &list))
1266 GST_DEBUG_OBJECT (avi, " adding %5d entries, total %2d %5d",
1267 g_list_length (list), g_list_length (*alloc_list),
1268 g_list_length (*index));
1269 *alloc_list = g_list_append (*alloc_list, list->data);
1270 *index = g_list_concat (*index, list);
1274 g_free (stream->indexes);
1275 stream->indexes = NULL;
1277 GST_DEBUG_OBJECT (avi, "index %s", ((*index) ? "!= 0" : "== 0"));
1282 * gst_avi_demux_riff_parse_vprp:
1283 * @element: caller element (used for debugging/error).
1284 * @buf: input data to be used for parsing, stripped from header.
1285 * @vprp: a pointer (returned by this function) to a filled-in vprp
1286 * structure. Caller should free it.
1288 * Parses a video stream´s vprp. This function takes ownership of @buf.
1290 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1291 * should be skipped on error, but it is not fatal.
1294 gst_avi_demux_riff_parse_vprp (GstElement * element,
1295 GstBuffer * buf, gst_riff_vprp ** _vprp)
1297 gst_riff_vprp *vprp;
1300 g_return_val_if_fail (buf != NULL, FALSE);
1301 g_return_val_if_fail (_vprp != NULL, FALSE);
1303 if (GST_BUFFER_SIZE (buf) < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1306 vprp = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1308 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1309 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1310 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1311 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1312 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1313 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1314 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1315 vprp->width = GUINT32_FROM_LE (vprp->width);
1316 vprp->height = GUINT32_FROM_LE (vprp->height);
1317 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1321 /* calculate fields based on size */
1322 k = (GST_BUFFER_SIZE (buf) - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) /
1324 if (vprp->fields > k) {
1325 GST_WARNING_OBJECT (element,
1326 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1329 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1330 GST_WARNING_OBJECT (element,
1331 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1332 GST_RIFF_VPRP_VIDEO_FIELDS);
1333 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1335 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1336 for (k = 0; k < vprp->fields; k++) {
1337 gst_riff_vprp_video_field_desc *fd;
1339 fd = &vprp->field_info[k];
1340 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1341 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1342 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1343 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1344 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1345 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1346 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1347 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1352 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1353 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1354 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1355 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1356 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1357 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1358 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1359 vprp->aspect & 0xffff);
1360 GST_INFO_OBJECT (element, " width %d", vprp->width);
1361 GST_INFO_OBJECT (element, " height %d", vprp->height);
1362 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1363 for (k = 0; k < vprp->fields; k++) {
1364 gst_riff_vprp_video_field_desc *fd;
1366 fd = &(vprp->field_info[k]);
1367 GST_INFO_OBJECT (element, " field %u description:", k);
1368 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1369 fd->compressed_bm_height);
1370 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1371 fd->compressed_bm_width);
1372 GST_INFO_OBJECT (element, " valid_bm_height %d",
1373 fd->valid_bm_height);
1374 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1375 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1376 fd->valid_bm_x_offset);
1377 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1378 fd->valid_bm_y_offset);
1379 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1380 fd->video_x_t_offset);
1381 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1384 gst_buffer_unref (buf);
1393 GST_ERROR_OBJECT (element,
1394 "Too small vprp (%d available, at least %d needed)",
1395 GST_BUFFER_SIZE (buf),
1396 (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1397 gst_buffer_unref (buf);
1403 * gst_avi_demux_parse_stream:
1404 * @avi: calling element (used for debugging/errors).
1405 * @buf: input buffer used to parse the stream.
1407 * Parses all subchunks in a strl chunk (which defines a single
1408 * stream). Discards the buffer after use. This function will
1409 * increment the stream counter internally.
1411 * Returns: whether the stream was identified successfully.
1412 * Errors are not fatal. It does indicate the stream
1416 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
1418 GstAviStream *stream;
1419 GstElementClass *klass;
1420 GstPadTemplate *templ;
1421 GstBuffer *sub = NULL;
1424 gchar *codec_name = NULL, *padname = NULL;
1425 const gchar *tag_name;
1426 GstCaps *caps = NULL;
1428 GstElement *element;
1429 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
1430 gst_riff_vprp *vprp = NULL;
1432 element = GST_ELEMENT_CAST (avi);
1434 GST_DEBUG_OBJECT (avi, "Parsing stream");
1436 if (avi->num_streams >= GST_AVI_DEMUX_MAX_STREAMS) {
1437 GST_WARNING_OBJECT (avi,
1438 "maximum no of streams (%d) exceeded, ignoring stream",
1439 GST_AVI_DEMUX_MAX_STREAMS);
1440 gst_buffer_unref (buf);
1441 /* not a fatal error, let's say */
1445 stream = &avi->stream[avi->num_streams];
1447 /* initial settings */
1448 stream->idx_duration = GST_CLOCK_TIME_NONE;
1449 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1450 stream->duration = GST_CLOCK_TIME_NONE;
1452 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
1453 /* sub can be NULL if the chunk is empty */
1455 GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
1456 GST_FOURCC_ARGS (tag));
1460 case GST_RIFF_TAG_strh:
1462 gst_riff_strh *strh;
1465 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
1468 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
1469 /* ownership given away */
1471 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
1475 strh = stream->strh;
1476 /* sanity check; stream header frame rate matches global header
1478 if (stream->strh->type == GST_RIFF_FCC_vids) {
1480 GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
1482 s_dur = gst_util_uint64_scale (GST_SECOND, strh->scale, strh->rate);
1483 GST_DEBUG_OBJECT (avi, "verifying stream framerate %d/%d, "
1484 "frame duration = %d ms", strh->rate, strh->scale,
1485 (gint) (s_dur / GST_MSECOND));
1486 if (h_dur > (10 * GST_MSECOND) && (s_dur > 10 * h_dur)) {
1487 strh->rate = GST_SECOND / GST_USECOND;
1488 strh->scale = h_dur / GST_USECOND;
1489 GST_DEBUG_OBJECT (avi, "correcting stream framerate to %d/%d",
1490 strh->rate, strh->scale);
1493 /* determine duration as indicated by header */
1494 stream->hdr_duration = gst_util_uint64_scale ((guint64) strh->length *
1495 strh->scale, GST_SECOND, (guint64) strh->rate);
1496 GST_INFO ("Stream duration according to header: %" GST_TIME_FORMAT,
1497 GST_TIME_ARGS (stream->hdr_duration));
1498 if (stream->hdr_duration == 0)
1499 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1504 case GST_RIFF_TAG_strf:
1506 gboolean res = FALSE;
1509 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
1513 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
1516 switch (stream->strh->type) {
1517 case GST_RIFF_FCC_vids:
1518 stream->is_vbr = TRUE;
1519 res = gst_riff_parse_strf_vids (element, sub,
1520 &stream->strf.vids, &stream->extradata);
1522 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
1524 case GST_RIFF_FCC_auds:
1525 stream->is_vbr = (stream->strh->samplesize == 0)
1526 && stream->strh->scale > 1;
1528 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
1529 &stream->extradata);
1531 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
1532 stream->is_vbr, res);
1534 case GST_RIFF_FCC_iavs:
1535 stream->is_vbr = TRUE;
1536 res = gst_riff_parse_strf_iavs (element, sub,
1537 &stream->strf.iavs, &stream->extradata);
1539 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
1541 case GST_RIFF_FCC_txts:
1542 /* nothing to parse here */
1543 stream->is_vbr = (stream->strh->samplesize == 0)
1544 && (stream->strh->scale > 1);
1548 GST_ERROR_OBJECT (avi,
1549 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
1550 GST_FOURCC_ARGS (stream->strh->type));
1554 gst_buffer_unref (sub);
1562 case GST_RIFF_TAG_vprp:
1565 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
1569 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
1573 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
1577 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
1578 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
1579 /* not considered fatal */
1587 case GST_RIFF_TAG_strd:
1588 if (stream->initdata)
1589 gst_buffer_unref (stream->initdata);
1590 stream->initdata = sub;
1593 case GST_RIFF_TAG_strn:
1594 g_free (stream->name);
1597 g_strndup ((gchar *) GST_BUFFER_DATA (sub),
1598 (gsize) GST_BUFFER_SIZE (sub));
1599 gst_buffer_unref (sub);
1602 stream->name = g_strdup ("");
1604 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
1607 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
1608 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
1609 '0' + avi->num_streams % 10)) {
1610 g_free (stream->indexes);
1611 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
1612 stream->superindex = TRUE;
1616 GST_WARNING_OBJECT (avi,
1617 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
1618 GST_FOURCC_ARGS (tag));
1620 case GST_RIFF_TAG_JUNK:
1624 gst_buffer_unref (sub);
1630 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
1635 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
1639 /* get class to figure out the template */
1640 klass = GST_ELEMENT_GET_CLASS (avi);
1642 /* we now have all info, let´s set up a pad and a caps and be done */
1643 /* create stream name + pad */
1644 switch (stream->strh->type) {
1645 case GST_RIFF_FCC_vids:{
1648 fourcc = (stream->strf.vids->compression) ?
1649 stream->strf.vids->compression : stream->strh->fcc_handler;
1650 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
1651 templ = gst_element_class_get_pad_template (klass, "video_%02d");
1652 caps = gst_riff_create_video_caps (fourcc, stream->strh,
1653 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
1655 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
1656 GST_TYPE_FOURCC, fourcc, NULL);
1657 } else if (got_vprp && vprp) {
1658 guint32 aspect_n, aspect_d;
1661 aspect_n = vprp->aspect >> 16;
1662 aspect_d = vprp->aspect & 0xffff;
1663 /* calculate the pixel aspect ratio using w/h and aspect ratio */
1664 n = aspect_n * stream->strf.vids->height;
1665 d = aspect_d * stream->strf.vids->width;
1667 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1669 /* very local, not needed elsewhere */
1673 tag_name = GST_TAG_VIDEO_CODEC;
1674 avi->num_v_streams++;
1677 case GST_RIFF_FCC_auds:{
1678 padname = g_strdup_printf ("audio_%02d", avi->num_a_streams);
1679 templ = gst_element_class_get_pad_template (klass, "audio_%02d");
1680 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
1681 stream->strh, stream->strf.auds, stream->extradata,
1682 stream->initdata, &codec_name);
1684 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
1685 G_TYPE_INT, stream->strf.auds->format, NULL);
1687 tag_name = GST_TAG_AUDIO_CODEC;
1688 avi->num_a_streams++;
1691 case GST_RIFF_FCC_iavs:{
1692 guint32 fourcc = stream->strh->fcc_handler;
1694 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
1695 templ = gst_element_class_get_pad_template (klass, "video_%02d");
1696 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
1697 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
1699 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
1700 GST_TYPE_FOURCC, fourcc, NULL);
1702 tag_name = GST_TAG_VIDEO_CODEC;
1703 avi->num_v_streams++;
1706 case GST_RIFF_FCC_txts:{
1707 padname = g_strdup_printf ("subtitle_%02d", avi->num_t_streams);
1708 templ = gst_element_class_get_pad_template (klass, "subtitle_%02d");
1709 caps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
1711 avi->num_t_streams++;
1715 g_assert_not_reached ();
1718 /* no caps means no stream */
1720 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
1724 GST_DEBUG_OBJECT (element, "codec-name=%s",
1725 (codec_name ? codec_name : "NULL"));
1726 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
1728 /* set proper settings and add it */
1730 gst_object_unref (stream->pad);
1731 pad = stream->pad = gst_pad_new_from_template (templ, padname);
1732 stream->last_flow = GST_FLOW_OK;
1733 stream->discont = TRUE;
1736 gst_pad_use_fixed_caps (pad);
1738 gst_pad_set_formats_function (pad,
1739 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
1740 gst_pad_set_event_mask_function (pad,
1741 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
1743 gst_pad_set_event_function (pad,
1744 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
1745 gst_pad_set_query_type_function (pad,
1746 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_query_types));
1747 gst_pad_set_query_function (pad,
1748 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
1750 gst_pad_set_convert_function (pad,
1751 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
1754 stream->num = avi->num_streams;
1755 stream->total_bytes = 0;
1757 stream->total_blocks = 0;
1758 stream->current_frame = 0;
1759 stream->current_byte = 0;
1760 gst_pad_set_element_private (pad, stream);
1762 gst_pad_set_caps (pad, caps);
1763 gst_pad_set_active (pad, TRUE);
1764 gst_element_add_pad (GST_ELEMENT (avi), pad);
1765 GST_LOG_OBJECT (element, "Added pad %s with caps %" GST_PTR_FORMAT,
1766 GST_PAD_NAME (pad), caps);
1767 gst_caps_unref (caps);
1771 if (!stream->taglist)
1772 stream->taglist = gst_tag_list_new ();
1774 avi->got_tags = TRUE;
1776 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
1778 g_free (codec_name);
1781 gst_buffer_unref (buf);
1788 /* unref any mem that may be in use */
1790 gst_buffer_unref (buf);
1792 gst_buffer_unref (sub);
1794 g_free (codec_name);
1795 g_free (stream->strh);
1796 g_free (stream->strf.data);
1797 g_free (stream->name);
1798 g_free (stream->indexes);
1799 if (stream->initdata)
1800 gst_buffer_unref (stream->initdata);
1801 if (stream->extradata)
1802 gst_buffer_unref (stream->extradata);
1803 memset (stream, 0, sizeof (GstAviStream));
1810 * gst_avi_demux_parse_odml:
1811 * @avi: calling element (used for debug/error).
1812 * @buf: input buffer to be used for parsing.
1814 * Read an openDML-2.0 extension header. Fills in the frame number
1815 * in the avi demuxer object when reading succeeds.
1818 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
1822 GstBuffer *sub = NULL;
1824 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
1827 case GST_RIFF_TAG_dmlh:{
1828 gst_riff_dmlh dmlh, *_dmlh;
1831 /* sub == NULL is possible and means an empty buffer */
1832 size = sub ? GST_BUFFER_SIZE (sub) : 0;
1835 if (size < sizeof (gst_riff_dmlh)) {
1836 GST_ERROR_OBJECT (avi,
1837 "DMLH entry is too small (%d bytes, %d needed)",
1838 size, (int) sizeof (gst_riff_dmlh));
1841 _dmlh = (gst_riff_dmlh *) GST_BUFFER_DATA (sub);
1842 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
1844 GST_INFO_OBJECT (avi, "dmlh tag found: totalframes: %u",
1847 avi->avih->tot_frames = dmlh.totalframes;
1852 GST_WARNING_OBJECT (avi,
1853 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
1854 GST_FOURCC_ARGS (tag));
1856 case GST_RIFF_TAG_JUNK:
1858 /* skip and move to next chunk */
1860 gst_buffer_unref (sub);
1867 gst_buffer_unref (buf);
1872 gst_avi_demux_index_last (GstAviDemux * avi, GstAviStream * stream)
1874 return stream->idx_n - 1;
1877 /* find a previous entry in the index with the given flags */
1879 gst_avi_demux_index_prev (GstAviDemux * avi, GstAviStream * stream,
1880 guint last, gboolean keyframe)
1882 GstAviIndexEntry *entry;
1885 for (i = last; i > 0; i--) {
1886 entry = &stream->index[i - 1];
1887 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
1895 gst_avi_demux_index_next (GstAviDemux * avi, GstAviStream * stream,
1896 guint last, gboolean keyframe)
1898 GstAviIndexEntry *entry;
1901 for (i = last + 1; i < stream->idx_n; i++) {
1902 entry = &stream->index[i];
1903 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
1907 return stream->idx_n - 1;
1911 gst_avi_demux_index_entry_search (GstAviIndexEntry * entry, guint64 * total)
1913 if (entry->total < *total)
1915 else if (entry->total > *total)
1921 * gst_avi_index_entry:
1923 * @stream: the stream
1924 * @time: seek time position
1926 * Finds the index entry which time is less or equal than the requested time.
1928 * Returns: the found position in the index.
1931 gst_avi_demux_index_for_time (GstAviDemux * avi,
1932 GstAviStream * stream, guint64 time)
1937 GST_LOG_OBJECT (avi, "search time:%" GST_TIME_FORMAT, GST_TIME_ARGS (time));
1939 /* easy (and common) cases */
1940 if (time == 0 || stream->idx_n == 0)
1942 if (time >= stream->idx_duration)
1943 return stream->idx_n - 1;
1945 /* figure out where we need to go. For that we convert the time to an
1946 * index entry or we convert it to a total and then do a binary search. */
1947 if (stream->is_vbr) {
1948 /* VBR stream next timestamp */
1949 if (stream->strh->type == GST_RIFF_FCC_auds) {
1950 total = avi_stream_convert_time_to_frames_unchecked (stream, time);
1952 index = avi_stream_convert_time_to_frames_unchecked (stream, time);
1955 /* constant rate stream */
1956 total = avi_stream_convert_time_to_bytes_unchecked (stream, time);
1960 GstAviIndexEntry *entry;
1962 /* no index, find index with binary search on total */
1963 GST_LOG_OBJECT (avi, "binary search for entry with total %"
1964 G_GUINT64_FORMAT, total);
1966 entry = gst_util_array_binary_search (stream->index,
1967 stream->idx_n, sizeof (GstAviIndexEntry),
1968 (GCompareDataFunc) gst_avi_demux_index_entry_search,
1969 GST_SEARCH_MODE_BEFORE, &total, NULL);
1971 if (entry == NULL) {
1972 GST_LOG_OBJECT (avi, "not found, assume index 0");
1975 index = entry - stream->index;
1976 GST_LOG_OBJECT (avi, "found at %u", index);
1984 gst_avi_demux_get_entry_info (GstAviDemux * avi, GstAviStream * stream,
1985 guint entry_n, GstClockTime * timestamp, GstClockTime * duration,
1986 guint64 * offset, guint64 * size, gboolean * keyframe)
1988 GstAviIndexEntry *entry;
1989 GstClockTime next_ts = 0, ts = 0;
1991 entry = &stream->index[entry_n];
1993 if (stream->is_vbr) {
1994 /* VBR stream next timestamp */
1995 if (stream->strh->type == GST_RIFF_FCC_auds) {
1996 if (timestamp || duration)
1997 ts = avi_stream_convert_frames_to_time_unchecked (stream, entry->total);
1999 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
2000 entry->total + entry->size);
2002 if (timestamp || duration)
2003 ts = avi_stream_convert_frames_to_time_unchecked (stream, entry_n);
2005 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
2009 /* constant rate stream */
2010 if (timestamp || duration)
2011 ts = avi_stream_convert_bytes_to_time_unchecked (stream, entry->total);
2013 next_ts = avi_stream_convert_bytes_to_time_unchecked (stream,
2014 entry->total + entry->size);
2019 *duration = next_ts - ts;
2022 *offset = entry->offset;
2024 *size = entry->size;
2026 *keyframe = ENTRY_IS_KEYFRAME (entry);
2031 * gst_avi_demux_parse_index:
2032 * @avi: calling element (used for debugging/errors).
2033 * @buf: buffer containing the full index.
2035 * Read index entries from the provided buffer.
2036 * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
2039 gst_avi_demux_parse_index (GstAviDemux * avi, GstBuffer * buf)
2045 gst_riff_index_entry *index;
2047 GstAviStream *stream;
2048 #ifndef GST_DISABLE_GST_DEBUG
2049 guint total_idx = 0, total_max = 0;
2055 data = GST_BUFFER_DATA (buf);
2056 size = GST_BUFFER_SIZE (buf);
2058 stamp = gst_util_get_timestamp ();
2060 num = size / sizeof (gst_riff_index_entry);
2064 index = (gst_riff_index_entry *) data;
2066 pos_before = avi->offset;
2067 GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2069 for (i = 0, n = 0; i < num; i++) {
2070 GstAviIndexEntry entry;
2074 id = GST_READ_UINT32_LE (&index[i].id);
2075 entry.offset = GST_READ_UINT32_LE (&index[i].offset);
2077 /* some sanity checks */
2078 if (G_UNLIKELY (id == GST_RIFF_rec || id == 0 ||
2079 (entry.offset == 0 && n > 0)))
2082 /* figure out if the index is 0 based or relative to the MOVI start */
2083 if (G_UNLIKELY (n == 0)) {
2084 if (entry.offset < pos_before)
2085 avi->index_offset = pos_before + 8;
2087 avi->index_offset = 0;
2088 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2091 /* get the stream for this entry */
2092 stream_nr = CHUNKID_TO_STREAMNR (id);
2093 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2094 GST_WARNING_OBJECT (avi,
2095 "Index entry %d has invalid stream nr %d", i, stream_nr);
2098 stream = &avi->stream[stream_nr];
2099 if (G_UNLIKELY (!stream->strh)) {
2100 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2105 if (stream->strh->type == GST_RIFF_FCC_auds) {
2106 /* all audio frames are keyframes */
2107 ENTRY_SET_KEYFRAME (&entry);
2108 stream->n_keyframes++;
2111 /* else read flags */
2112 flags = GST_READ_UINT32_LE (&index[i].flags);
2113 if (flags & GST_RIFF_IF_KEYFRAME) {
2114 ENTRY_SET_KEYFRAME (&entry);
2115 stream->n_keyframes++;
2117 ENTRY_UNSET_KEYFRAME (&entry);
2122 entry.size = GST_READ_UINT32_LE (&index[i].size);
2125 entry.total = stream->total_bytes;
2126 stream->total_bytes += entry.size;
2127 if (stream->strh->type == GST_RIFF_FCC_auds) {
2128 gint blockalign = stream->strf.auds->blockalign;
2130 stream->total_blocks += DIV_ROUND_UP (entry.size, blockalign);
2132 stream->total_blocks++;
2135 /* add to the index */
2136 if (G_UNLIKELY (stream->idx_n >= stream->idx_max)) {
2137 /* we need to make some more room */
2138 if (stream->idx_max == 0) {
2139 /* initial size guess, assume each stream has an equal amount of entries,
2140 * overshoot with at least 8K */
2142 (num / avi->num_streams) + (8192 / sizeof (GstAviIndexEntry));
2144 stream->idx_max += 8192 / sizeof (GstAviIndexEntry);
2145 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "expanded index to %u",
2148 stream->index = g_try_renew (GstAviIndexEntry, stream->index,
2150 if (G_UNLIKELY (!stream->index))
2154 GST_LOG_OBJECT (avi,
2155 "Adding stream %d, index entry %d, kf %d, size %u "
2156 ", offset %" G_GUINT64_FORMAT ", total %" G_GUINT64_FORMAT, stream_nr,
2157 stream->idx_n, ENTRY_IS_KEYFRAME (&entry), entry.size, entry.offset,
2161 stream->index[stream->idx_n++] = entry;
2165 /* get stream stats now */
2166 for (i = 0; i < avi->num_streams; i++) {
2167 GstAviIndexEntry *entry;
2170 if (G_UNLIKELY (!(stream = &avi->stream[i])))
2172 if (G_UNLIKELY (!stream->strh))
2174 if (G_UNLIKELY (!stream->index || stream->idx_n == 0))
2177 entry = &stream->index[stream->idx_n - 1];
2178 total = entry->total + entry->size;
2180 /* calculate duration */
2181 if (stream->is_vbr) {
2182 /* VBR stream next timestamp */
2183 if (stream->strh->type == GST_RIFF_FCC_auds) {
2184 stream->idx_duration =
2185 avi_stream_convert_frames_to_time_unchecked (stream, total);
2187 stream->idx_duration =
2188 avi_stream_convert_frames_to_time_unchecked (stream, stream->idx_n);
2191 /* constant rate stream */
2192 stream->idx_duration = avi_stream_convert_bytes_to_time_unchecked (stream,
2195 #ifndef GST_DISABLE_GST_DEBUG
2196 total_idx += stream->idx_n;
2197 total_max += stream->idx_max;
2199 GST_INFO_OBJECT (avi, "Stream %d, dur %" GST_TIME_FORMAT ", %6u entries, "
2200 "%5lu keyframes, entry size = %2u, total size = %10u, allocated %10u",
2201 i, GST_TIME_ARGS (stream->idx_duration), stream->idx_n,
2202 stream->n_keyframes, (guint) sizeof (GstAviIndexEntry),
2203 (guint) (stream->idx_n * sizeof (GstAviIndexEntry)),
2204 (guint) (stream->idx_max * sizeof (GstAviIndexEntry)));
2206 #ifndef GST_DISABLE_GST_DEBUG
2207 total_idx *= sizeof (GstAviIndexEntry);
2208 total_max *= sizeof (GstAviIndexEntry);
2210 GST_INFO_OBJECT (avi, "%u bytes for index vs %u ideally, %u wasted",
2211 total_max, total_idx, total_max - total_idx);
2213 stamp = gst_util_get_timestamp () - stamp;
2214 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "parsing index %" GST_TIME_FORMAT,
2215 GST_TIME_ARGS (stamp));
2222 GST_DEBUG_OBJECT (avi, "empty index");
2227 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2228 ("Cannot allocate memory for %u*%u=%u bytes",
2229 (guint) sizeof (GstAviIndexEntry), num,
2230 (guint) sizeof (GstAviIndexEntry) * num));
2237 * gst_avi_demux_parse_index:
2238 * @avi: calling element (used for debugging/errors).
2239 * @buf: buffer containing the full index.
2240 * @entries_list: list (returned by this function) containing the index
2241 * entries parsed from the buffer. The first in the list
2242 * is also a pointer to the allocated data and should be
2243 * free'ed at some point.
2245 * Read index entries from the provided buffer. Takes ownership of @buf.
2246 * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
2249 gst_avi_demux_parse_index (GstAviDemux * avi,
2250 GstBuffer * buf, GList ** _entries_list)
2252 guint64 pos_before = avi->offset;
2253 gst_avi_index_entry *entries = NULL;
2255 GList *entries_list = NULL;
2257 #ifndef GST_DISABLE_GST_DEBUG
2258 gulong _nr_keyframes = 0;
2262 if (!buf || !GST_BUFFER_SIZE (buf)) {
2263 *_entries_list = NULL;
2264 GST_DEBUG ("empty index");
2266 gst_buffer_unref (buf);
2270 stamp = gst_util_get_timestamp ();
2272 data = GST_BUFFER_DATA (buf);
2273 num = GST_BUFFER_SIZE (buf) / sizeof (gst_riff_index_entry);
2274 if (!(entries = g_try_new (gst_avi_index_entry, num)))
2277 GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2279 for (i = 0, n = 0; i < num; i++) {
2281 gst_riff_index_entry entry, *_entry;
2282 GstAviStream *stream;
2284 gst_avi_index_entry *target;
2286 _entry = &((gst_riff_index_entry *) data)[i];
2287 entry.id = GST_READ_UINT32_LE (&_entry->id);
2288 entry.offset = GST_READ_UINT32_LE (&_entry->offset);
2289 entry.flags = GST_READ_UINT32_LE (&_entry->flags);
2290 entry.size = GST_READ_UINT32_LE (&_entry->size);
2291 target = &entries[n];
2293 if (entry.id == GST_RIFF_rec || entry.id == 0 ||
2294 (entry.offset == 0 && n > 0))
2297 stream_nr = CHUNKID_TO_STREAMNR (entry.id);
2298 if (stream_nr >= avi->num_streams) {
2299 GST_WARNING_OBJECT (avi,
2300 "Index entry %d has invalid stream nr %d", i, stream_nr);
2303 target->stream_nr = stream_nr;
2304 stream = &avi->stream[stream_nr];
2306 if (!stream->strh) {
2307 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2311 target->index_nr = i;
2313 (entry.flags & GST_RIFF_IF_KEYFRAME) ? GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME
2315 target->size = entry.size;
2316 target->offset = entry.offset + 8;
2318 /* figure out if the index is 0 based or relative to the MOVI start */
2320 if (target->offset < pos_before)
2321 avi->index_offset = pos_before + 8;
2323 avi->index_offset = 0;
2324 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2327 if (stream->strh->type == GST_RIFF_FCC_auds) {
2328 /* all audio frames are keyframes */
2329 target->flags |= GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
2331 #ifndef GST_DISABLE_GST_DEBUG
2332 if (target->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)
2336 /* stream duration unknown, now we can calculate it */
2337 if (stream->idx_duration == -1)
2338 stream->idx_duration = 0;
2341 target->ts = stream->idx_duration;
2342 if (stream->is_vbr) {
2343 /* VBR stream next timestamp */
2344 if (stream->strh->type == GST_RIFF_FCC_auds) {
2345 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
2346 stream->total_blocks + 1);
2348 next_ts = avi_stream_convert_frames_to_time_unchecked (stream,
2352 /* constant rate stream */
2353 next_ts = avi_stream_convert_bytes_to_time_unchecked (stream,
2354 stream->total_bytes + target->size);
2356 /* duration is next - current */
2357 target->dur = next_ts - target->ts;
2359 /* stream position */
2360 target->bytes_before = stream->total_bytes;
2361 target->frames_before = stream->idx_n;
2363 stream->total_bytes += target->size;
2365 if (stream->strh->type == GST_RIFF_FCC_auds) {
2366 if (stream->strf.auds->blockalign > 0)
2367 stream->total_blocks +=
2368 (target->size + stream->strf.auds->blockalign -
2369 1) / stream->strf.auds->blockalign;
2371 stream->total_blocks++;
2373 stream->idx_duration = next_ts;
2375 GST_LOG_OBJECT (avi,
2376 "Adding index entry %d (%6u), flags %02x, stream %d, size %u "
2377 ", offset %" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT ", dur %"
2379 target->index_nr, stream->idx_n - 1, target->flags,
2380 target->stream_nr, target->size, target->offset,
2381 GST_TIME_ARGS (target->ts), GST_TIME_ARGS (target->dur));
2382 entries_list = g_list_prepend (entries_list, target);
2387 GST_INFO_OBJECT (avi, "Parsed index, %6u/%6u entries, %5lu keyframes, "
2388 "entry size = %2u, total size = %10u", n, num, _nr_keyframes,
2389 (guint) sizeof (gst_avi_index_entry),
2390 (guint) (n * sizeof (gst_avi_index_entry)));
2392 gst_buffer_unref (buf);
2395 *_entries_list = g_list_reverse (entries_list);
2400 stamp = gst_util_get_timestamp () - stamp;
2401 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "parsing index %" GST_TIME_FORMAT,
2402 GST_TIME_ARGS (stamp));
2409 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2410 ("Cannot allocate memory for %u*%u=%u bytes",
2411 (guint) sizeof (gst_avi_index_entry), num,
2412 (guint) sizeof (gst_avi_index_entry) * num));
2413 gst_buffer_unref (buf);
2419 * gst_avi_demux_stream_index:
2420 * @avi: avi demuxer object.
2422 * Seeks to index and reads it.
2425 gst_avi_demux_stream_index (GstAviDemux * avi)
2428 guint64 offset = avi->offset;
2433 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2435 /* get chunk information */
2436 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2437 if (res != GST_FLOW_OK)
2439 else if (GST_BUFFER_SIZE (buf) < 8)
2442 /* check tag first before blindy trying to read 'size' bytes */
2443 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2444 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2445 if (tag == GST_RIFF_TAG_LIST) {
2446 /* this is the movi tag */
2447 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2448 (8 + GST_ROUND_UP_2 (size)));
2449 offset += 8 + GST_ROUND_UP_2 (size);
2450 gst_buffer_unref (buf);
2451 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2452 if (res != GST_FLOW_OK)
2454 else if (GST_BUFFER_SIZE (buf) < 8)
2456 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2457 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2460 if (tag != GST_RIFF_TAG_idx1)
2465 gst_buffer_unref (buf);
2467 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2469 /* read chunk, advance offset */
2470 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2471 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2474 GST_DEBUG ("will parse index chunk size %u for tag %"
2475 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2477 gst_avi_demux_parse_index (avi, buf);
2478 gst_buffer_unref (buf);
2480 #ifndef GST_DISABLE_GST_DEBUG
2481 /* debug our indexes */
2484 GstAviStream *stream;
2486 for (i = 0; i < avi->num_streams; i++) {
2487 stream = &avi->stream[i];
2488 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2489 i, stream->idx_n, stream->total_bytes);
2498 GST_DEBUG_OBJECT (avi,
2499 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2504 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2505 gst_buffer_unref (buf);
2510 GST_WARNING_OBJECT (avi,
2511 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2512 GST_FOURCC_ARGS (tag));
2513 gst_buffer_unref (buf);
2518 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2519 gst_buffer_unref (buf);
2526 * Sync to next data chunk.
2529 gst_avi_demux_skip (GstAviDemux * avi, gboolean prevent_eos)
2531 GstRiffRead *riff = GST_RIFF_READ (avi);
2534 guint64 pos, length;
2538 pos = gst_bytestream_tell (riff->bs);
2539 length = gst_bytestream_length (riff->bs);
2541 if (pos + 8 > length)
2544 if (gst_bytestream_peek_bytes (riff->bs, &data, 8) != 8)
2547 size = GST_READ_UINT32_LE (&data[4]);
2551 /* Note, we're going to skip which might involve seeks. Therefore,
2552 * we need 1 byte more! */
2553 if (pos + 8 + size >= length)
2557 return gst_riff_read_skip (riff);
2561 gst_avi_demux_sync (GstAviDemux * avi, guint32 * ret_tag, gboolean prevent_eos)
2563 GstRiffRead *riff = GST_RIFF_READ (avi);
2565 guint64 length = gst_bytestream_length (riff->bs);
2567 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2570 /* peek first (for the end of this 'list/movi' section) */
2571 if (!(tag = gst_riff_peek_tag (riff, &avi->level_up)))
2574 /* if we're at top-level, we didn't read the 'movi'
2575 * list tag yet. This can also be 'AVIX' in case of
2576 * openDML-2.0 AVI files. Lastly, it might be idx1,
2577 * in which case we skip it so we come at EOS. */
2579 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2582 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2586 case GST_RIFF_TAG_LIST:
2587 if (!(tag = gst_riff_peek_list (riff)))
2591 case GST_RIFF_LIST_AVIX:
2592 if (!gst_riff_read_list (riff, &tag))
2596 case GST_RIFF_LIST_movi:
2597 if (!gst_riff_read_list (riff, &tag))
2605 GST_WARNING ("Unknown list %" GST_FOURCC_FORMAT " before AVI data",
2606 GST_FOURCC_ARGS (tag));
2609 case GST_RIFF_TAG_JUNK:
2610 if (!gst_avi_demux_skip (avi, prevent_eos))
2617 if ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
2618 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9') {
2623 case GST_RIFF_TAG_idx1:
2624 case GST_RIFF_TAG_JUNK:
2625 if (!gst_avi_demux_skip (avi, prevent_eos)) {
2632 /* And then, we get the data */
2633 if (prevent_eos && gst_bytestream_tell (riff->bs) + 12 >= length)
2636 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2639 /* Support for rec-list files */
2641 case GST_RIFF_TAG_LIST:
2642 if (!(tag = gst_riff_peek_list (riff)))
2644 if (tag == GST_RIFF_rec) {
2645 /* Simply skip the list */
2646 if (!gst_riff_read_list (riff, &tag))
2648 if (!(tag = gst_riff_peek_tag (riff, NULL)))
2653 case GST_RIFF_TAG_JUNK:
2654 gst_avi_demux_skip (avi, prevent_eos);
2667 * gst_avi_demux_peek_tag:
2669 * Returns the tag and size of the next chunk
2671 static GstFlowReturn
2672 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2675 GstFlowReturn res = GST_FLOW_OK;
2676 GstBuffer *buf = NULL;
2679 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2680 if (res != GST_FLOW_OK)
2683 bufsize = GST_BUFFER_SIZE (buf);
2687 *tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2688 *size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2690 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2691 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2692 *size, offset + 8, offset + 8 + (gint64) * size);
2694 gst_buffer_unref (buf);
2701 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2706 GST_DEBUG_OBJECT (avi, "got %d bytes which is <> 8 bytes", bufsize);
2707 res = GST_FLOW_ERROR;
2715 * gst_avi_demux_next_data_buffer:
2717 * Returns the offset and size of the next buffer
2718 * Position is the position of the buffer (after tag and size)
2720 static GstFlowReturn
2721 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2722 guint32 * tag, guint * size)
2724 guint64 off = *offset;
2729 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2730 if (res != GST_FLOW_OK)
2732 if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
2733 off += 8 + 4; /* skip tag + size + subtag */
2747 * gst_avi_demux_stream_scan:
2748 * @avi: calling element (used for debugging/errors).
2749 * @index: list of index entries, returned by this function.
2750 * @alloc_list: list of allocated data, returned by this function.
2752 * Scan the file for all chunks to "create" a new index.
2753 * Return value indicates if we can continue reading the stream. It
2754 * does not say anything about whether we created an index.
2759 gst_avi_demux_stream_scan (GstAviDemux * avi,
2760 GList ** index, GList ** alloc_list)
2763 gst_avi_index_entry *entry, *entries = NULL;
2764 GstAviStream *stream;
2766 guint64 pos = avi->offset;
2771 guint index_size = 0;
2774 * - implement non-seekable source support.
2776 GST_DEBUG_OBJECT (avi,
2777 "Creating index %s existing index, starting at offset %" G_GUINT64_FORMAT,
2778 ((*index) ? "with" : "without"), pos);
2780 format = GST_FORMAT_BYTES;
2781 if (!gst_pad_query_peer_duration (avi->sinkpad, &format, &tmplength))
2787 entry = g_list_last (*index)->data;
2788 pos = entry->offset + avi->index_offset + entry->size;
2789 if (entry->size & 1)
2792 if (pos >= length) {
2793 GST_LOG_OBJECT (avi, "Complete index, we're done");
2797 GST_LOG_OBJECT (avi, "Incomplete index, seeking to last valid entry @ %"
2798 G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT " (%"
2799 G_GUINT64_FORMAT "+%u)", pos, length, entry->offset, entry->size);
2806 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
2807 if (G_UNLIKELY (res != GST_FLOW_OK))
2810 /* check valid stream */
2811 stream_nr = CHUNKID_TO_STREAMNR (tag);
2812 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2813 GST_WARNING_OBJECT (avi,
2814 "Index entry has invalid stream nr %d", stream_nr);
2818 stream = &avi->stream[stream_nr];
2819 if (G_UNLIKELY (stream->pad == NULL)) {
2820 GST_WARNING_OBJECT (avi,
2821 "Stream %d does not have an output pad, can't create new index",
2827 if (G_UNLIKELY (index_size % 1024 == 0)) {
2828 entries = g_new (gst_avi_index_entry, 1024);
2829 *alloc_list = g_list_prepend (*alloc_list, entries);
2831 entry = &entries[index_size % 1024];
2833 entry->index_nr = index_size++;
2834 entry->stream_nr = stream_nr;
2835 entry->flags = GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
2836 entry->offset = pos - avi->index_offset;
2839 /* timestamps, get timestamps of two consecutive frames to calculate
2840 * timestamp and duration. */
2841 format = GST_FORMAT_TIME;
2842 if (stream->is_vbr) {
2844 entry->ts = avi_stream_convert_frames_to_time_unchecked (stream,
2846 entry->dur = avi_stream_convert_frames_to_time_unchecked (stream,
2849 /* constant rate stream */
2850 entry->ts = avi_stream_convert_bytes_to_time_unchecked (stream,
2851 stream->total_bytes);
2852 entry->dur = avi_stream_convert_bytes_to_time_unchecked (stream,
2853 stream->total_bytes + entry->size);
2855 entry->dur -= entry->ts;
2857 /* stream position */
2858 entry->bytes_before = stream->total_bytes;
2859 stream->total_bytes += entry->size;
2860 entry->frames_before = stream->idx_n;
2862 stream->idx_duration = entry->ts + entry->dur;
2864 list = g_list_prepend (list, entry);
2865 GST_DEBUG_OBJECT (avi, "Added index entry %d (in stream: %d), offset %"
2866 G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT " for stream %d",
2867 index_size - 1, entry->frames_before, entry->offset,
2868 GST_TIME_ARGS (entry->ts), entry->stream_nr);
2871 /* update position */
2872 pos += GST_ROUND_UP_2 (size);
2873 if (G_UNLIKELY (pos > length)) {
2874 GST_WARNING_OBJECT (avi,
2875 "Stopping index lookup since we are further than EOF");
2880 /* FIXME: why is this disabled */
2882 while (gst_avi_demux_sync (avi, &tag, TRUE)) {
2883 guint stream_nr = CHUNKID_TO_STREAMNR (tag);
2885 GstFormat format = GST_FORMAT_TIME;
2887 if (stream_nr >= avi->num_streams)
2889 stream = &avi->stream[stream_nr];
2891 /* get chunk size */
2892 if (gst_bytestream_peek_bytes (riff->bs, &data, 8) != 8)
2896 entry->index_nr = index_size++;
2897 entry->stream_nr = stream_nr;
2898 entry->flags = GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME;
2899 entry->offset = gst_bytestream_tell (riff->bs) + 8 - avi->index_offset;
2900 entry->size = GST_READ_UINT32_LE (&data[4]);
2903 if (stream->is_vbr) {
2905 entry->ts = avi_stream_convert_frames_to_time_unchecked (stream,
2907 entry->dur = avi_stream_convert_frames_to_time_unchecked (stream,
2910 /* constant rate stream */
2911 entry->ts = avi_stream_convert_bytes_to_time_unchecked (stream,
2912 stream->total_bytes);
2913 entry->dur = avi_stream_convert_bytes_to_time_unchecked (stream,
2914 stream->total_bytes + entry->size);
2916 entry->dur -= entry->ts;
2918 /* stream position */
2919 entry->bytes_before = stream->total_bytes;
2920 stream->total_bytes += entry->size;
2921 entry->frames_before = stream->idx_n;
2924 list = g_list_prepend (list, entry);
2925 GST_DEBUG_OBJECT (avi, "Added index entry %d (in stream: %d), offset %"
2926 G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT " for stream %d",
2927 index_size - 1, entry->frames_before, entry->offset,
2928 GST_TIME_ARGS (entry->ts), entry->stream_nr);
2931 if (!gst_avi_demux_skip (avi, TRUE))
2935 if (!(event = gst_riff_read_seek (riff, pos))) {
2939 gst_event_unref (event);
2943 GST_DEBUG_OBJECT (avi, "index created, %d items", index_size);
2945 *index = g_list_concat (*index, g_list_reverse (list));
2953 * gst_avi_demux_massage_index:
2954 * @avi: calling element (used for debugging/errors).
2956 * We're going to go over each entry in the index and finetune
2957 * some things we don't like about AVI. For example, a single
2958 * chunk might be too long. Also, individual streams might be
2959 * out-of-sync. In the first case, we cut the chunk in several
2960 * smaller pieces. In the second case, we re-order chunk reading
2961 * order. The end result should be a smoother playing AVI.
2964 gst_avi_demux_massage_index (GstAviDemux * avi,
2965 GList * list, GList * alloc_list)
2967 gst_avi_index_entry *entry;
2968 GstAviStream *stream;
2971 gint64 delay = G_GINT64_CONSTANT (0);
2974 stamp = gst_util_get_timestamp ();
2976 GST_LOG_OBJECT (avi, "Starting index massage, nr_entries = %d",
2977 list ? g_list_length (list) : 0);
2980 #ifndef GST_DISABLE_GST_DEBUG
2981 guint num_added_total = 0;
2982 guint num_per_stream[GST_AVI_DEMUX_MAX_STREAMS] = { 0, };
2984 GST_LOG_OBJECT (avi,
2985 "I'm now going to cut large chunks into smaller pieces");
2987 /* cut chunks in small (seekable) pieces
2988 * FIXME: this should be a property where a value of
2989 * GST_CLOCK_TIME_NONE would disable the chunking
2991 #define MAX_DURATION (GST_SECOND / 2)
2992 for (i = 0; i < avi->num_streams; i++) {
2993 /* only chop streams that have exactly *one* chunk */
2994 if (avi->stream[i].idx_n != 1)
2997 for (node = list; node != NULL; node = node->next) {
3000 if (entry->stream_nr != i)
3003 /* check for max duration of a single buffer. I suppose that
3004 * the allocation of index entries could be improved. */
3005 stream = &avi->stream[entry->stream_nr];
3006 if (entry->dur > MAX_DURATION
3007 && stream->strh->type == GST_RIFF_FCC_auds) {
3009 gst_avi_index_entry *entries;
3010 guint old_size, num_added;
3013 /* cut in 1/10th of a second */
3014 ideal_size = stream->strf.auds->av_bps / 10;
3016 /* ensure chunk size is multiple of blockalign */
3017 if (stream->strf.auds->blockalign > 1)
3018 ideal_size -= ideal_size % stream->strf.auds->blockalign;
3021 old_size = entry->size;
3022 num_added = (entry->size - 1) / ideal_size;
3023 avi->index_size += num_added;
3024 entries = g_malloc (sizeof (gst_avi_index_entry) * num_added);
3025 alloc_list = g_list_prepend (alloc_list, entries);
3026 for (node2 = node->next; node2 != NULL; node2 = node2->next) {
3027 gst_avi_index_entry *entry2 = node2->data;
3029 entry2->index_nr += num_added;
3030 if (entry2->stream_nr == entry->stream_nr)
3031 entry2->frames_before += num_added;
3034 /* new sized index chunks */
3035 for (i = 0; i < num_added + 1; i++) {
3036 gst_avi_index_entry *entry2;
3041 entry2 = &entries[i - 1];
3042 list = g_list_insert_before (list, node->next, entry2);
3045 memcpy (entry2, entry, sizeof (gst_avi_index_entry));
3048 if (old_size >= ideal_size) {
3049 entry2->size = ideal_size;
3050 old_size -= ideal_size;
3052 entry2->size = old_size;
3055 entry2->dur = GST_SECOND * entry2->size / stream->strf.auds->av_bps;
3058 entry2->ts += entry->dur;
3059 entry2->offset += entry->size;
3060 entry2->bytes_before += entry->size;
3061 entry2->frames_before++;
3064 #ifndef GST_DISABLE_GST_DEBUG
3065 num_added_total += num_added;
3070 #ifndef GST_DISABLE_GST_DEBUG
3071 if (num_added_total)
3072 GST_LOG ("added %u new index entries", num_added_total);
3075 GST_LOG_OBJECT (avi, "I'm now going to reorder the index entries for time");
3077 /* re-order for time */
3078 list = g_list_sort (list, (GCompareFunc) sort);
3080 /* make a continous array out of the list */
3081 avi->index_size = g_list_length (list);
3082 avi->index_entries = g_try_new (gst_avi_index_entry, avi->index_size);
3083 if (!avi->index_entries)
3086 entry = (gst_avi_index_entry *) (list->data);
3089 GST_LOG_OBJECT (avi,
3090 "Building index array, nr_entries = %d (time offset = %"
3091 GST_TIME_FORMAT, avi->index_size, GST_TIME_ARGS (delay));
3093 for (i = 0, node = list; node != NULL; node = node->next, i++) {
3095 entry->index_nr = i;
3097 memcpy (&avi->index_entries[i], entry, sizeof (gst_avi_index_entry));
3098 #ifndef GST_DISABLE_GST_DEBUG
3099 num_per_stream[entry->stream_nr]++;
3102 GST_LOG_OBJECT (avi, "Sorted index entry %3d for stream %d of size %6u"
3103 " at offset %7" G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT
3104 " dur %" GST_TIME_FORMAT,
3105 avi->index_entries[i].index_nr, entry->stream_nr, entry->size,
3106 entry->offset, GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (entry->dur));
3109 for (i = 0; i < avi->num_streams; i++) {
3110 stream = &avi->stream[i];
3111 stream->idx_duration -= delay;
3114 #ifndef GST_DISABLE_GST_DEBUG
3116 gchar str[GST_AVI_DEMUX_MAX_STREAMS * (1 + 6 + 2)];
3119 for (i = 0; i < avi->num_streams; i++) {
3120 if (!avi->stream[i].pad)
3122 pad_name = GST_OBJECT_NAME (avi->stream[i].pad);
3123 sprintf (&str[i * (1 + 6 + 2)], " %6u %c", num_per_stream[i],
3126 GST_LOG_OBJECT (avi, "indizies per stream:%20s", str);
3130 GST_LOG_OBJECT (avi, "Freeing original index list");
3131 /* all the node->data in list point to alloc_list chunks */
3136 g_list_foreach (alloc_list, (GFunc) g_free, NULL);
3137 g_list_free (alloc_list);
3139 #ifndef GST_DISABLE_GST_DEBUG
3140 for (i = 0; i < avi->num_streams; i++) {
3141 GST_LOG_OBJECT (avi, "Stream %d, %d frames, %8" G_GUINT64_FORMAT " bytes",
3142 i, avi->stream[i].idx_n, avi->stream[i].total_bytes);
3146 GST_LOG_OBJECT (avi, "Index massaging done");
3148 stamp = gst_util_get_timestamp () - stamp;
3150 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "massaging index %" GST_TIME_FORMAT,
3151 GST_TIME_ARGS (stamp));
3158 GST_WARNING_OBJECT (avi, "Out of memory for %" G_GSIZE_FORMAT " bytes",
3159 sizeof (gst_avi_index_entry) * avi->index_size);
3166 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
3171 total = GST_CLOCK_TIME_NONE;
3173 /* all streams start at a timestamp 0 */
3174 for (stream = 0; stream < avi->num_streams; stream++) {
3175 GstClockTime duration, hduration;
3176 GstAviStream *streamc = &avi->stream[stream];
3177 gst_riff_strh *strh = streamc->strh;
3182 /* get header duration for the stream */
3183 hduration = streamc->hdr_duration;
3185 /* index duration calculated during parsing, invariant under massage */
3186 duration = streamc->idx_duration;
3188 /* now pick a good duration */
3189 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3190 /* index gave valid duration, use that */
3191 GST_INFO ("Stream %d duration according to index: %" GST_TIME_FORMAT,
3192 stream, GST_TIME_ARGS (duration));
3194 /* fall back to header info to calculate a duration */
3195 duration = hduration;
3197 /* set duration for the stream */
3198 streamc->duration = duration;
3200 /* find total duration */
3201 if (total == GST_CLOCK_TIME_NONE || duration > total)
3205 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
3206 /* now update the duration for those streams where we had none */
3207 for (stream = 0; stream < avi->num_streams; stream++) {
3208 GstAviStream *streamc = &avi->stream[stream];
3210 if (!GST_CLOCK_TIME_IS_VALID (streamc->duration)
3211 || streamc->duration == 0) {
3212 streamc->duration = total;
3214 GST_INFO ("Stream %d duration according to total: %" GST_TIME_FORMAT,
3215 stream, GST_TIME_ARGS (total));
3220 /* and set the total duration in the segment. */
3221 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
3222 GST_TIME_ARGS (total));
3224 gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
3227 /* returns FALSE if there are no pads to deliver event to,
3228 * otherwise TRUE (whatever the outcome of event sending) */
3230 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
3232 gboolean result = FALSE;
3235 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
3236 GST_EVENT_TYPE_NAME (event), avi->num_streams);
3238 if (avi->num_streams) {
3239 for (i = 0; i < avi->num_streams; i++) {
3240 GstAviStream *stream = &avi->stream[i];
3244 gst_pad_push_event (stream->pad, gst_event_ref (event));
3248 gst_event_unref (event);
3253 * Read AVI headers when streaming
3255 static GstFlowReturn
3256 gst_avi_demux_stream_header_push (GstAviDemux * avi)
3258 GstFlowReturn ret = GST_FLOW_OK;
3263 GstBuffer *buf = NULL, *sub = NULL;
3267 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
3269 switch (avi->header_state) {
3270 case GST_AVI_DEMUX_HEADER_TAG_LIST:
3271 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3272 avi->offset += 8 + GST_ROUND_UP_2 (size);
3273 if (tag != GST_RIFF_TAG_LIST)
3274 goto header_no_list;
3276 gst_adapter_flush (avi->adapter, 8);
3277 /* Find the 'hdrl' LIST tag */
3278 GST_DEBUG ("Reading %d bytes", size);
3279 buf = gst_adapter_take_buffer (avi->adapter, size);
3281 if (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl)
3282 goto header_no_hdrl;
3286 gst_adapter_flush (avi->adapter, 1);
3288 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
3290 /* the hdrl starts with a 'avih' header */
3291 if (!gst_riff_parse_chunk (GST_ELEMENT (avi), buf, &offset, &tag, &sub))
3292 goto header_no_avih;
3294 if (tag != GST_RIFF_TAG_avih)
3295 goto header_no_avih;
3297 if (!gst_avi_demux_parse_avih (GST_ELEMENT (avi), sub, &avi->avih))
3298 goto header_wrong_avih;
3300 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
3302 /* now, read the elements from the header until the end */
3303 while (gst_riff_parse_chunk (GST_ELEMENT (avi), buf, &offset, &tag,
3305 /* sub can be NULL on empty tags */
3310 case GST_RIFF_TAG_LIST:
3311 if (GST_BUFFER_SIZE (sub) < 4)
3314 switch (GST_READ_UINT32_LE (GST_BUFFER_DATA (sub))) {
3315 case GST_RIFF_LIST_strl:
3316 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3318 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3319 ("failed to parse stream, ignoring"));
3324 case GST_RIFF_LIST_odml:
3325 gst_avi_demux_parse_odml (avi, sub);
3329 GST_WARNING_OBJECT (avi,
3330 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3331 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA
3334 case GST_RIFF_TAG_JUNK:
3339 GST_WARNING_OBJECT (avi,
3340 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
3341 offset, GST_FOURCC_ARGS (tag));
3343 case GST_RIFF_TAG_JUNK:
3345 /* move to next chunk */
3347 gst_buffer_unref (sub);
3352 gst_buffer_unref (buf);
3353 GST_DEBUG ("elements parsed");
3355 /* check parsed streams */
3356 if (avi->num_streams == 0) {
3358 } else if (avi->num_streams != avi->avih->streams) {
3359 GST_WARNING_OBJECT (avi,
3360 "Stream header mentioned %d streams, but %d available",
3361 avi->avih->streams, avi->num_streams);
3363 GST_DEBUG ("Get junk and info next");
3364 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
3366 /* Need more data */
3370 case GST_AVI_DEMUX_HEADER_INFO:
3371 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
3373 if (gst_adapter_available (avi->adapter) < 12)
3376 data = gst_adapter_peek (avi->adapter, 12);
3377 tag = GST_READ_UINT32_LE (data);
3378 size = GST_READ_UINT32_LE (data + 4);
3379 ltag = GST_READ_UINT32_LE (data + 8);
3381 if (tag == GST_RIFF_TAG_LIST) {
3383 case GST_RIFF_LIST_movi:
3384 gst_adapter_flush (avi->adapter, 12);
3387 case GST_RIFF_LIST_INFO:
3388 GST_DEBUG ("Found INFO chunk");
3389 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3390 GST_DEBUG ("got size %d", size);
3392 gst_adapter_flush (avi->adapter, 12);
3394 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
3397 gst_adapter_flush (avi->adapter, 1);
3398 gst_riff_parse_info (GST_ELEMENT (avi), buf,
3400 gst_buffer_unref (buf);
3402 avi->offset += GST_ROUND_UP_2 (size) - 4;
3404 GST_DEBUG ("skipping INFO LIST prefix");
3407 /* Need more data */
3412 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3413 avi->offset += 8 + GST_ROUND_UP_2 (size);
3414 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3415 // ??? goto iterate; ???
3417 /* Need more data */
3423 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3424 avi->offset += 8 + GST_ROUND_UP_2 (size);
3425 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3428 /* Need more data */
3435 GST_WARNING ("unhandled header state: %d", avi->header_state);
3440 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3441 avi->num_streams, avi->stream[0].indexes);
3443 GST_DEBUG ("Found movi chunk. Starting to stream data");
3444 avi->state = GST_AVI_DEMUX_MOVI;
3447 /*GList *index = NULL, *alloc = NULL; */
3449 /* ######################## this need to be integrated with the state */
3450 /* create or read stream index (for seeking) */
3451 if (avi->stream[0].indexes != NULL) {
3452 gst_avi_demux_read_subindexes_push (avi, &index, &alloc);
3455 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX) {
3456 gst_avi_demux_stream_index (avi, &index, &alloc);
3458 /* some indexes are incomplete, continue streaming from there */
3460 gst_avi_demux_stream_scan (avi, &index, &alloc);
3463 /* this is a fatal error */
3467 if (!gst_avi_demux_massage_index (avi, index, alloc))
3470 gst_avi_demux_calculate_durations_from_index (avi);
3471 /* ######################## */
3474 /* create initial NEWSEGMENT event */
3475 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3476 stop = avi->segment.duration;
3478 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3480 if (avi->seek_event)
3481 gst_event_unref (avi->seek_event);
3482 avi->seek_event = gst_event_new_new_segment
3483 (FALSE, avi->segment.rate, GST_FORMAT_TIME,
3484 avi->segment.start, stop, avi->segment.start);
3486 /* at this point we know all the streams and we can signal the no more
3488 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3489 gst_element_no_more_pads (GST_ELEMENT (avi));
3496 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3497 return GST_FLOW_ERROR;
3501 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3502 ("Invalid AVI header (no LIST at start): %"
3503 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3504 return GST_FLOW_ERROR;
3508 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3509 ("Invalid AVI header (no hdrl at start): %"
3510 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3511 gst_buffer_unref (buf);
3512 return GST_FLOW_ERROR;
3516 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3517 ("Invalid AVI header (no avih at start): %"
3518 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3520 gst_buffer_unref (sub);
3522 gst_buffer_unref (buf);
3523 return GST_FLOW_ERROR;
3527 gst_buffer_unref (buf);
3528 return GST_FLOW_ERROR;
3533 * Read full AVI headers.
3535 static GstFlowReturn
3536 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
3539 GstBuffer *buf, *sub = NULL;
3542 GList *index = NULL, *alloc = NULL;
3546 GstElement *element = GST_ELEMENT_CAST (avi);
3549 stamp = gst_util_get_timestamp ();
3551 /* the header consists of a 'hdrl' LIST tag */
3552 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3553 if (res != GST_FLOW_OK)
3554 goto pull_range_failed;
3555 else if (tag != GST_RIFF_TAG_LIST)
3557 else if (GST_BUFFER_SIZE (buf) < 4)
3560 GST_DEBUG_OBJECT (avi, "parsing headers");
3562 /* Find the 'hdrl' LIST tag */
3563 while (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl) {
3564 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
3565 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf))));
3568 gst_buffer_unref (buf);
3570 /* read new chunk */
3571 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3572 if (res != GST_FLOW_OK)
3573 goto pull_range_failed;
3574 else if (tag != GST_RIFF_TAG_LIST)
3576 else if (GST_BUFFER_SIZE (buf) < 4)
3580 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
3582 /* the hdrl starts with a 'avih' header */
3583 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
3585 else if (tag != GST_RIFF_TAG_avih)
3587 else if (!gst_avi_demux_parse_avih (element, sub, &avi->avih))
3590 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3592 /* now, read the elements from the header until the end */
3593 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
3594 /* sub can be NULL on empty tags */
3599 case GST_RIFF_TAG_LIST:
3604 if (GST_BUFFER_SIZE (sub) < 4)
3607 data = GST_BUFFER_DATA (sub);
3608 fourcc = GST_READ_UINT32_LE (data);
3611 case GST_RIFF_LIST_strl:
3612 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3613 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3614 ("failed to parse stream, ignoring"));
3619 case GST_RIFF_LIST_odml:
3620 gst_avi_demux_parse_odml (avi, sub);
3624 GST_WARNING_OBJECT (avi,
3625 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3626 GST_FOURCC_ARGS (fourcc));
3627 GST_MEMDUMP_OBJECT (avi, "Unknown list", GST_BUFFER_DATA (sub),
3628 GST_BUFFER_SIZE (sub));
3630 case GST_RIFF_TAG_JUNK:
3636 GST_WARNING_OBJECT (avi,
3637 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
3638 GST_FOURCC_ARGS (tag), offset);
3639 GST_MEMDUMP_OBJECT (avi, "Unknown tag", GST_BUFFER_DATA (sub),
3640 GST_BUFFER_SIZE (sub));
3642 case GST_RIFF_TAG_JUNK:
3645 gst_buffer_unref (sub);
3650 gst_buffer_unref (buf);
3651 GST_DEBUG ("elements parsed");
3653 /* check parsed streams */
3654 if (avi->num_streams == 0)
3656 else if (avi->num_streams != avi->avih->streams) {
3657 GST_WARNING_OBJECT (avi,
3658 "Stream header mentioned %d streams, but %d available",
3659 avi->avih->streams, avi->num_streams);
3662 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
3663 G_GUINT64_FORMAT, avi->offset);
3665 /* Now, find the data (i.e. skip all junk between header and data) */
3670 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
3671 if (res != GST_FLOW_OK) {
3672 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
3673 goto pull_range_failed;
3674 } else if (GST_BUFFER_SIZE (buf) < 12) {
3675 GST_DEBUG_OBJECT (avi, "got %d bytes which is less than 12 bytes",
3676 GST_BUFFER_SIZE (buf));
3677 gst_buffer_unref (buf);
3678 return GST_FLOW_ERROR;
3681 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
3682 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
3683 ltag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 8);
3685 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
3686 GST_FOURCC_ARGS (tag), size);
3687 GST_MEMDUMP ("Tag content", GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
3688 gst_buffer_unref (buf);
3691 case GST_RIFF_TAG_LIST:{
3693 case GST_RIFF_LIST_movi:
3694 GST_DEBUG_OBJECT (avi,
3695 "Reached the 'movi' tag, we're done with skipping");
3697 case GST_RIFF_LIST_INFO:
3699 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3701 if (res != GST_FLOW_OK) {
3702 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3703 goto pull_range_failed;
3705 GST_DEBUG ("got size %u", GST_BUFFER_SIZE (buf));
3707 GST_DEBUG ("skipping INFO LIST prefix");
3708 avi->offset += (4 - GST_ROUND_UP_2 (size));
3709 gst_buffer_unref (buf);
3713 sub = gst_buffer_create_sub (buf, 4, GST_BUFFER_SIZE (buf) - 4);
3714 gst_riff_parse_info (element, sub, &avi->globaltags);
3716 gst_buffer_unref (sub);
3719 gst_buffer_unref (buf);
3720 /* gst_riff_read_chunk() has already advanced avi->offset */
3723 GST_WARNING_OBJECT (avi,
3724 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3725 GST_FOURCC_ARGS (ltag));
3726 avi->offset += 8 + GST_ROUND_UP_2 (size);
3732 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3733 GST_FOURCC_ARGS (tag));
3735 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3736 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3737 avi->offset += 8 + GST_ROUND_UP_2 (size);
3743 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3744 avi->num_streams, avi->stream[0].indexes);
3747 /* create or read stream index (for seeking) */
3748 if (avi->stream[0].indexes != NULL) {
3749 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3750 gst_avi_demux_read_subindexes_pull (avi, &index, &alloc);
3754 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX) {
3755 gst_avi_demux_stream_index (avi);
3758 /* some indexes are incomplete, continue streaming from there */
3760 gst_avi_demux_stream_scan (avi, &index, &alloc);
3763 /* this is a fatal error */
3768 gst_avi_demux_calculate_durations_from_index (avi);
3770 /* create initial NEWSEGMENT event */
3771 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3772 stop = avi->segment.duration;
3774 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3776 /* do initial seek to the configured segment values */
3777 gst_avi_demux_do_seek (avi, &avi->segment);
3779 if (avi->seek_event)
3780 gst_event_unref (avi->seek_event);
3781 avi->seek_event = gst_event_new_new_segment
3782 (FALSE, avi->segment.rate, GST_FORMAT_TIME,
3783 avi->segment.start, stop, avi->segment.start);
3785 stamp = gst_util_get_timestamp () - stamp;
3786 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "pulling header %" GST_TIME_FORMAT,
3787 GST_TIME_ARGS (stamp));
3789 /* at this point we know all the streams and we can signal the no more
3791 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3792 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3799 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3800 ("Invalid AVI header (no LIST at start): %"
3801 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3802 gst_buffer_unref (buf);
3803 return GST_FLOW_ERROR;
3807 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3808 ("Invalid AVI header (no hdrl at start): %"
3809 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3810 gst_buffer_unref (buf);
3811 return GST_FLOW_ERROR;
3815 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3816 ("Invalid AVI header (no avih at start): %"
3817 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3819 gst_buffer_unref (sub);
3820 gst_buffer_unref (buf);
3821 return GST_FLOW_ERROR;
3825 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3826 ("Invalid AVI header (cannot parse avih at start)"));
3827 gst_buffer_unref (buf);
3828 return GST_FLOW_ERROR;
3832 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3833 return GST_FLOW_ERROR;
3838 GST_WARNING ("file without or too big index");
3839 g_list_free (index);
3840 g_list_foreach (alloc, (GFunc) g_free, NULL);
3841 g_list_free (alloc);
3843 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3844 ("Could not get/create index"));
3845 return GST_FLOW_ERROR;
3850 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3851 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3852 return GST_FLOW_ERROR;
3856 /* move a stream to an offset */
3858 gst_avi_demux_move_stream (GstAviDemux * avi, GstAviStream * stream,
3859 GstSegment * segment, guint index)
3861 GST_DEBUG_OBJECT (avi, "Move stream %d to %u", stream->num, index);
3863 if (segment->rate < 0.0) {
3865 /* Because we don't know the frame order we need to push from the prev keyframe
3866 * to the next keyframe. If there is a smart decoder downstream he will notice
3867 * that there are too many encoded frames send and return UNEXPECTED when there
3868 * are enough decoded frames to fill the segment. */
3869 next_key = gst_avi_demux_index_next (avi, stream, index, TRUE);
3871 stream->start_entry = 0;
3872 stream->step_entry = index;
3873 stream->current_entry = index;
3874 stream->stop_entry = next_key;
3876 GST_DEBUG_OBJECT (avi, "reverse seek: start %u, step %u, stop %u",
3877 stream->start_entry, stream->step_entry, stream->stop_entry);
3879 stream->start_entry = index;
3880 stream->step_entry = index;
3881 stream->stop_entry = gst_avi_demux_index_last (avi, stream);
3883 if (stream->current_entry != index) {
3884 stream->current_entry = index;
3885 stream->discont = TRUE;
3890 * Do the actual seeking.
3893 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
3895 GstClockTime seek_time;
3898 GstAviStream *stream;
3899 GstClockTime timestamp, duration;
3902 seek_time = segment->last_stop;
3903 keyframe = !!(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
3905 /* FIXME, this code assumes the main stream with keyframes is stream 0,
3906 * which is mostly correct... */
3907 stream = &avi->stream[0];
3909 /* get the entry index for the requested position */
3910 index = gst_avi_demux_index_for_time (avi, stream, seek_time);
3912 /* take a look at the entry info */
3913 gst_avi_demux_get_entry_info (avi, stream, index,
3914 NULL, NULL, NULL, NULL, &kentry);
3916 GST_DEBUG_OBJECT (avi, "Got entry %u", index);
3918 /* check if we are already on a keyframe */
3920 GST_DEBUG_OBJECT (avi, "not keyframe, searching back");
3921 /* now go to the previous keyframe, this is where we should start
3923 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
3924 GST_DEBUG_OBJECT (avi, "previous keyframe at %u", index);
3927 /* take a look at the final entry */
3928 gst_avi_demux_get_entry_info (avi, stream, index,
3929 ×tamp, &duration, NULL, NULL, NULL);
3931 GST_DEBUG_OBJECT (avi,
3932 "Got keyframe entry %d [ts:%" GST_TIME_FORMAT
3933 " / duration:%" GST_TIME_FORMAT "]", index,
3934 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
3937 /* when seeking to a keyframe, we update the result seek time
3938 * to the time of the keyframe. */
3939 seek_time = timestamp;
3942 /* move the main stream */
3943 gst_avi_demux_move_stream (avi, stream, segment, index);
3945 /* now set DISCONT and align the other streams */
3946 for (i = 0; i < avi->num_streams; i++) {
3947 GstAviStream *ostream;
3949 ostream = &avi->stream[i];
3950 if (ostream == stream)
3953 /* get the entry index for the requested position */
3954 index = gst_avi_demux_index_for_time (avi, ostream, seek_time);
3956 gst_avi_demux_get_entry_info (avi, ostream, index,
3957 NULL, NULL, NULL, NULL, &kentry);
3959 index = gst_avi_demux_index_prev (avi, ostream, index, TRUE);
3961 gst_avi_demux_move_stream (avi, ostream, segment, index);
3964 GST_DEBUG_OBJECT (avi, "seek: %" GST_TIME_FORMAT
3965 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
3967 /* the seek time is also the last_stop and stream time */
3968 segment->last_stop = seek_time;
3969 segment->time = seek_time;
3975 * Handle seek event.
3978 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
3983 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
3987 GstSegment seeksegment = { 0, };
3990 GST_DEBUG_OBJECT (avi, "doing seek with event");
3992 gst_event_parse_seek (event, &rate, &format, &flags,
3993 &cur_type, &cur, &stop_type, &stop);
3995 /* we have to have a format as the segment format. Try to convert
3997 if (format != GST_FORMAT_TIME) {
3998 GstFormat fmt = GST_FORMAT_TIME;
3999 gboolean res = TRUE;
4001 if (cur_type != GST_SEEK_TYPE_NONE)
4002 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
4003 if (res && stop_type != GST_SEEK_TYPE_NONE)
4004 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
4010 GST_DEBUG_OBJECT (avi,
4011 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
4012 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
4013 /* FIXME: can we do anything with rate!=1.0 */
4015 GST_DEBUG_OBJECT (avi, "doing seek without event");
4020 /* save flush flag */
4021 flush = flags & GST_SEEK_FLAG_FLUSH;
4024 GstEvent *event = gst_event_new_flush_start ();
4026 /* for a flushing seek, we send a flush_start on all pads. This will
4027 * eventually stop streaming with a WRONG_STATE. We can thus eventually
4028 * take the STREAM_LOCK. */
4029 GST_DEBUG_OBJECT (avi, "sending flush start");
4030 gst_avi_demux_push_event (avi, gst_event_ref (event));
4031 gst_pad_push_event (avi->sinkpad, event);
4033 /* a non-flushing seek, we PAUSE the task so that we can take the
4035 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
4036 gst_pad_pause_task (avi->sinkpad);
4039 /* wait for streaming to stop */
4040 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
4041 GST_PAD_STREAM_LOCK (avi->sinkpad);
4043 /* copy segment, we need this because we still need the old
4044 * segment when we close the current segment. */
4045 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4048 GST_DEBUG_OBJECT (avi, "configuring seek");
4049 gst_segment_set_seek (&seeksegment, rate, format, flags,
4050 cur_type, cur, stop_type, stop, &update);
4052 /* do the seek, seeksegment.last_stop contains the new position, this
4053 * actually never fails. */
4054 gst_avi_demux_do_seek (avi, &seeksegment);
4059 GST_DEBUG_OBJECT (avi, "sending flush stop");
4060 gst_avi_demux_push_event (avi, gst_event_new_flush_stop ());
4061 gst_pad_push_event (avi->sinkpad, gst_event_new_flush_stop ());
4062 /* reset the last flow and mark discont, FLUSH is always DISCONT */
4063 for (i = 0; i < avi->num_streams; i++) {
4064 avi->stream[i].last_flow = GST_FLOW_OK;
4065 avi->stream[i].discont = TRUE;
4067 } else if (avi->segment_running) {
4070 /* we are running the current segment and doing a non-flushing seek,
4071 * close the segment first based on the last_stop. */
4072 GST_DEBUG_OBJECT (avi, "closing running segment %" G_GINT64_FORMAT
4073 " to %" G_GINT64_FORMAT, avi->segment.start, avi->segment.last_stop);
4074 seg = gst_event_new_new_segment (TRUE,
4075 avi->segment.rate, avi->segment.format,
4076 avi->segment.start, avi->segment.last_stop, avi->segment.time);
4077 gst_avi_demux_push_event (avi, seg);
4080 /* now update the real segment info */
4081 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
4083 /* post the SEGMENT_START message when we do segmented playback */
4084 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4085 gst_element_post_message (GST_ELEMENT (avi),
4086 gst_message_new_segment_start (GST_OBJECT (avi),
4087 avi->segment.format, avi->segment.last_stop));
4090 /* prepare for streaming again */
4091 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
4092 stop = avi->segment.duration;
4094 /* queue the segment event for the streaming thread. */
4095 if (avi->seek_event)
4096 gst_event_unref (avi->seek_event);
4097 if (avi->segment.rate > 0.0) {
4098 avi->seek_event = gst_event_new_new_segment (FALSE,
4099 avi->segment.rate, avi->segment.format,
4100 avi->segment.last_stop, stop, avi->segment.time);
4102 avi->seek_event = gst_event_new_new_segment (FALSE,
4103 avi->segment.rate, avi->segment.format,
4104 avi->segment.start, avi->segment.last_stop, avi->segment.start);
4107 if (!avi->streaming) {
4108 avi->segment_running = TRUE;
4109 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4112 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
4119 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4125 * Helper for gst_avi_demux_invert()
4128 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
4130 memcpy (tmp, d1, bytes);
4131 memcpy (d1, d2, bytes);
4132 memcpy (d2, tmp, bytes);
4136 #define gst_avi_demux_is_uncompressed(fourcc) \
4137 (fourcc == GST_RIFF_DIB || \
4138 fourcc == GST_RIFF_rgb || \
4139 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW)
4142 * Invert DIB buffers... Takes existing buffer and
4143 * returns either the buffer or a new one (with old
4144 * one dereferenced).
4145 * FIXME: can't we preallocate tmp? and remember stride, bpp?
4148 gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
4155 if (stream->strh->type != GST_RIFF_FCC_vids)
4158 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
4159 return buf; /* Ignore non DIB buffers */
4162 s = gst_caps_get_structure (GST_PAD_CAPS (stream->pad), 0);
4163 if (!gst_structure_get_int (s, "bpp", &bpp)) {
4164 GST_WARNING ("Failed to retrieve depth from caps");
4168 if (stream->strf.vids == NULL) {
4169 GST_WARNING ("Failed to retrieve vids for stream");
4173 h = stream->strf.vids->height;
4174 w = stream->strf.vids->width;
4175 stride = w * (bpp / 8);
4177 buf = gst_buffer_make_writable (buf);
4178 if (GST_BUFFER_SIZE (buf) < (stride * h)) {
4179 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
4183 tmp = g_malloc (stride);
4185 for (y = 0; y < h / 2; y++) {
4186 swap_line (GST_BUFFER_DATA (buf) + stride * y,
4187 GST_BUFFER_DATA (buf) + stride * (h - 1 - y), tmp, stride);
4196 * Returns the aggregated GstFlowReturn.
4198 static GstFlowReturn
4199 gst_avi_demux_combine_flows (GstAviDemux * avi, GstAviStream * stream,
4204 /* store the value */
4205 stream->last_flow = ret;
4207 /* any other error that is not-linked can be returned right away */
4208 if (G_UNLIKELY (ret != GST_FLOW_NOT_LINKED))
4211 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4212 for (i = 0; i < avi->num_streams; i++) {
4213 GstAviStream *ostream = &avi->stream[i];
4215 ret = ostream->last_flow;
4216 /* some other return value (must be SUCCESS but we can return
4217 * other values as well) */
4218 if (G_UNLIKELY (ret != GST_FLOW_NOT_LINKED))
4221 /* if we get here, all other pads were unlinked and we return
4222 * NOT_LINKED then */
4224 GST_LOG_OBJECT (avi, "combined return %s", gst_flow_get_name (ret));
4230 * Read data from one index entry
4232 static GstFlowReturn
4233 gst_avi_demux_process_next_entry (GstAviDemux * avi)
4235 GstFlowReturn res = GST_FLOW_OK;
4236 gboolean processed = FALSE;
4237 GstAviStream *stream;
4238 gst_avi_index_entry *entry;
4242 /* see if we are at the end */
4243 if ((avi->segment.rate > 0 && avi->current_entry >= avi->index_size))
4246 /* get next entry, this will work as we checked for the index size above */
4247 entry = &avi->index_entries[avi->current_entry++];
4249 /* check for reverse playback */
4250 if (avi->segment.rate < 0 && avi->current_entry > avi->reverse_stop_index) {
4251 GST_LOG_OBJECT (avi, "stop_index %d reached", avi->reverse_stop_index);
4253 /* check if we have pushed enough data for this segment */
4254 if (avi->reverse_start_index == 0)
4255 goto eos_reverse_zero;
4256 if (avi->index_entries[avi->reverse_start_index].ts < avi->segment.start)
4257 goto eos_reverse_segment;
4259 if (!(entry = gst_avi_demux_step_reverse (avi)))
4262 avi->current_entry++;
4265 /* see if we have a valid stream, ignore if not
4266 * FIXME: can't we check this when building the index?
4267 * we check it in _parse_index(), _stream_scan()
4269 if (entry->stream_nr >= avi->num_streams) {
4270 GST_WARNING_OBJECT (avi,
4271 "Entry %d has non-existing stream nr %d",
4272 avi->current_entry - 1, entry->stream_nr);
4276 /* get stream now */
4277 stream = &avi->stream[entry->stream_nr];
4279 if (avi->segment.rate > 0.0) {
4280 /* only check this for fowards playback for now */
4281 if ((entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME)
4282 && GST_CLOCK_TIME_IS_VALID (entry->ts)
4283 && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
4284 && (entry->ts > avi->segment.stop)) {
4289 /* skip empty entries */
4290 if (entry->size == 0 || !stream->pad) {
4291 GST_DEBUG_OBJECT (avi, "Skipping entry %d (%d, %p)",
4292 avi->current_entry - 1, entry->size, stream->pad);
4296 GST_LOG ("reading buffer (size=%d) from stream %d at current pos %"
4297 G_GUINT64_FORMAT " (%llx)", entry->size, entry->stream_nr,
4298 avi->index_offset + entry->offset, avi->index_offset + entry->offset);
4300 /* pull in the data */
4301 res = gst_pad_pull_range (avi->sinkpad, entry->offset +
4302 avi->index_offset, entry->size, &buf);
4303 if (res != GST_FLOW_OK)
4306 /* check for short buffers, this is EOS as well */
4307 if (GST_BUFFER_SIZE (buf) < entry->size)
4310 /* invert the picture if needed */
4311 buf = gst_avi_demux_invert (stream, buf);
4313 /* mark non-keyframes */
4314 if (!(entry->flags & GST_AVI_INDEX_ENTRY_FLAG_KEYFRAME))
4315 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4317 GST_BUFFER_TIMESTAMP (buf) = entry->ts;
4318 GST_BUFFER_DURATION (buf) = entry->dur;
4319 if (stream->strh->type == GST_RIFF_FCC_vids) {
4320 if (stream->current_frame >= 0)
4321 GST_BUFFER_OFFSET (buf) = stream->current_frame;
4324 GstFormat fmt = GST_FORMAT_DEFAULT;
4326 if (gst_pad_query_convert (stream->pad, GST_FORMAT_TIME, entry->ts,
4328 GST_BUFFER_OFFSET (buf) = framenum;
4330 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4333 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4334 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
4335 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4337 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %d, offset %"
4338 G_GUINT64_FORMAT " and time %"
4339 GST_TIME_FORMAT " on pad %s",
4340 GST_BUFFER_SIZE (buf), GST_BUFFER_OFFSET (buf),
4341 GST_TIME_ARGS (entry->ts), GST_PAD_NAME (stream->pad));
4343 /* update current position in the segment */
4344 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, entry->ts);
4346 /* mark discont when pending */
4347 if (stream->discont) {
4348 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4349 stream->discont = FALSE;
4352 res = gst_pad_push (stream->pad, buf);
4354 /* mark as processed, we increment the frame and byte counters then
4355 * leave the while loop and return the GstFlowReturn */
4357 GST_DEBUG_OBJECT (avi, "Processed buffer %d: %s", entry->index_nr,
4358 gst_flow_get_name (res));
4360 if (avi->segment.rate < 0
4361 && entry->ts > avi->segment.stop && res == GST_FLOW_UNEXPECTED) {
4362 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
4363 * we are at the end of the segment, so we just need to jump
4364 * back to the previous section.
4366 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
4368 if (!(entry = gst_avi_demux_step_reverse (avi)))
4373 stream->current_frame = entry->frames_before;
4374 stream->current_byte = entry->bytes_before;
4380 res = gst_avi_demux_combine_flows (avi, stream, res);
4383 stream->current_frame = entry->frames_before + 1;
4384 stream->current_byte = entry->bytes_before + entry->size;
4385 } while (!processed);
4388 GST_DEBUG_OBJECT (avi, "returning %s", gst_flow_get_name (res));
4395 GST_LOG_OBJECT (avi, "Handled last index entry, setting EOS (%d > %d)",
4396 avi->current_entry, avi->index_size);
4397 /* we mark the first stream as EOS */
4398 res = GST_FLOW_UNEXPECTED;
4403 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
4404 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
4405 GST_TIME_ARGS (entry->ts), GST_TIME_ARGS (avi->segment.stop));
4406 res = GST_FLOW_UNEXPECTED;
4411 GST_DEBUG_OBJECT (avi, "start_index was 0, setting EOS");
4412 res = GST_FLOW_UNEXPECTED;
4415 eos_reverse_segment:
4417 GST_DEBUG_OBJECT (avi, "full segment pushed, setting EOS");
4418 res = GST_FLOW_UNEXPECTED;
4423 GST_DEBUG_OBJECT (avi,
4424 "pull range failed: pos=%" G_GUINT64_FORMAT " size=%d",
4425 entry->offset + avi->index_offset, entry->size);
4430 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
4431 ", only got %d/%d bytes (truncated file?)", entry->offset +
4432 avi->index_offset, GST_BUFFER_SIZE (buf), entry->size);
4433 gst_buffer_unref (buf);
4434 res = GST_FLOW_UNEXPECTED;
4440 /* move @stream to the next position in its index */
4441 static GstFlowReturn
4442 gst_avi_demux_advance (GstAviDemux * avi, GstAviStream * stream,
4447 /* move to the next entry */
4448 stream->current_entry++;
4449 stream->current_frame++;
4451 /* see if we reached the end */
4452 if (stream->current_entry >= stream->stop_entry) {
4453 if (avi->segment.rate < 0.0) {
4454 if (stream->step_entry == stream->start_entry) {
4455 /* we stepped all the way to the start, eos */
4456 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
4459 /* backwards, stop becomes step, find a new step */
4460 stream->stop_entry = stream->step_entry;
4461 stream->step_entry = gst_avi_demux_index_prev (avi, stream,
4462 stream->stop_entry, TRUE);
4463 stream->current_entry = stream->step_entry;
4465 GST_DEBUG_OBJECT (avi,
4466 "reverse playback jump: start %u, step %u, stop %u",
4467 stream->start_entry, stream->step_entry, stream->stop_entry);
4470 for (i = 0; i < avi->num_streams; i++) {
4471 avi->stream[i].last_flow = GST_FLOW_OK;
4472 avi->stream[i].discont = TRUE;
4476 GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
4485 GST_DEBUG_OBJECT (avi, "we are EOS");
4486 /* setting current_time to -1 marks EOS */
4487 stream->current_time = -1;
4488 return GST_FLOW_UNEXPECTED;
4492 static GstFlowReturn
4493 gst_avi_demux_loop_data (GstAviDemux * avi)
4495 GstFlowReturn ret = GST_FLOW_OK;
4497 guint stream_num, i;
4498 GstAviStream *stream;
4499 gboolean processed = FALSE;
4501 guint64 offset, size;
4502 GstClockTime timestamp, duration;
4506 /* first find the stream with the lowest current position, this is the one
4507 * we should push from next */
4508 min_time = G_MAXUINT64;
4510 for (i = 0; i < avi->num_streams; i++) {
4513 stream = &avi->stream[i];
4514 position = stream->current_time;
4516 /* position of -1 is EOS */
4517 if (position != -1 && position < min_time) {
4518 min_time = position;
4524 if (G_UNLIKELY (stream_num == -1)) {
4525 GST_DEBUG_OBJECT (avi, "all streams are EOS");
4529 /* we have the stream now */
4530 stream = &avi->stream[stream_num];
4532 /* skip streams without pads */
4534 GST_DEBUG_OBJECT (avi, "skipping entry from stream %d without pad",
4539 /* get the timing info for the entry */
4540 gst_avi_demux_get_entry_info (avi, stream, stream->current_entry,
4541 ×tamp, &duration, &offset, &size, &keyframe);
4543 /* skip empty entries */
4545 GST_DEBUG_OBJECT (avi, "Skipping entry %d (%d, %p)",
4546 stream->current_entry, size, stream->pad);
4550 if (avi->segment.rate > 0.0) {
4551 /* only check this for fowards playback for now */
4552 if (keyframe && GST_CLOCK_TIME_IS_VALID (timestamp)
4553 && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
4554 && (timestamp > avi->segment.stop)) {
4559 /* correct for index offset */
4560 offset += avi->index_offset + 8;
4562 GST_LOG ("reading buffer (size=%d) from stream %d at current pos %"
4563 G_GUINT64_FORMAT " (%llx)", size, stream_num, offset, offset);
4565 /* pull in the data */
4566 ret = gst_pad_pull_range (avi->sinkpad, offset, size, &buf);
4567 if (ret != GST_FLOW_OK)
4570 /* check for short buffers, this is EOS as well */
4571 if (GST_BUFFER_SIZE (buf) < size)
4574 /* invert the picture if needed */
4575 buf = gst_avi_demux_invert (stream, buf);
4577 /* mark non-keyframes */
4579 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4581 GST_BUFFER_TIMESTAMP (buf) = timestamp;
4582 GST_BUFFER_DURATION (buf) = duration;
4583 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4584 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
4586 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4588 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %d, offset %"
4589 G_GUINT64_FORMAT " and time %"
4590 GST_TIME_FORMAT " on pad %s",
4591 GST_BUFFER_SIZE (buf), GST_BUFFER_OFFSET (buf),
4592 GST_TIME_ARGS (timestamp), GST_PAD_NAME (stream->pad));
4594 /* update current position in the segment */
4595 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, timestamp);
4597 /* mark discont when pending */
4598 if (stream->discont) {
4599 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4600 stream->discont = FALSE;
4603 ret = gst_pad_push (stream->pad, buf);
4605 /* mark as processed, we increment the frame and byte counters then
4606 * leave the while loop and return the GstFlowReturn */
4608 GST_DEBUG_OBJECT (avi, "Processed buffer %d: %s", stream->current_entry,
4609 gst_flow_get_name (ret));
4611 if (avi->segment.rate < 0) {
4612 if (timestamp > avi->segment.stop && ret == GST_FLOW_UNEXPECTED) {
4613 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
4614 * we are at the end of the segment, so we just need to jump
4615 * back to the previous section. */
4616 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
4621 ret = gst_avi_demux_advance (avi, stream, ret);
4624 ret = gst_avi_demux_combine_flows (avi, stream, ret);
4625 } while (!processed);
4633 GST_DEBUG_OBJECT (avi, "No samples left for any streams - EOS");
4634 ret = GST_FLOW_UNEXPECTED;
4639 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
4640 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
4641 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (avi->segment.stop));
4642 ret = GST_FLOW_UNEXPECTED;
4647 GST_DEBUG_OBJECT (avi, "pull range failed: pos=%" G_GUINT64_FORMAT
4648 " size=%d", offset, size);
4653 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
4654 ", only got %d/%d bytes (truncated file?)", offset,
4655 GST_BUFFER_SIZE (buf), size);
4656 gst_buffer_unref (buf);
4657 ret = GST_FLOW_UNEXPECTED;
4663 GST_DEBUG_OBJECT (avi, "No samples left for stream");
4664 /* EOS will be raised if all are EOS */
4672 * Read data. If we have an index it delegates to
4673 * gst_avi_demux_process_next_entry().
4675 static GstFlowReturn
4676 gst_avi_demux_stream_data (GstAviDemux * avi)
4681 GstFlowReturn res = GST_FLOW_OK;
4682 GstFormat format = GST_FORMAT_TIME;
4684 if (G_UNLIKELY (avi->have_eos)) {
4685 /* Clean adapter, we're done */
4686 gst_adapter_clear (avi->adapter);
4690 /* Iterate until need more data, so adapter won't grow too much */
4692 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
4696 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
4697 GST_FOURCC_ARGS (tag), size);
4699 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
4700 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
4701 GST_LOG ("Chunk ok");
4702 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
4703 GST_DEBUG ("Found sub-index tag");
4704 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4705 /* accept 0 size buffer here */
4706 avi->abort_buffering = FALSE;
4707 GST_DEBUG (" skipping %d bytes for now", size);
4708 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4711 } else if (tag == GST_RIFF_TAG_idx1) {
4712 GST_DEBUG ("Found index tag, stream done");
4713 avi->have_eos = TRUE;
4714 return GST_FLOW_UNEXPECTED;
4715 } else if (tag == GST_RIFF_TAG_LIST) {
4716 /* movi chunks might be grouped in rec list */
4717 if (gst_adapter_available (avi->adapter) >= 12) {
4718 GST_DEBUG ("Found LIST tag, skipping LIST header");
4719 gst_adapter_flush (avi->adapter, 12);
4723 } else if (tag == GST_RIFF_TAG_JUNK) {
4724 /* rec list might contain JUNK chunks */
4725 GST_DEBUG ("Found JUNK tag");
4726 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4727 /* accept 0 size buffer here */
4728 avi->abort_buffering = FALSE;
4729 GST_DEBUG (" skipping %d bytes for now", size);
4730 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4734 GST_DEBUG ("No more stream chunks, send EOS");
4735 avi->have_eos = TRUE;
4736 return GST_FLOW_UNEXPECTED;
4739 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
4740 /* supposedly one hopes to catch a nicer chunk later on ... */
4741 /* FIXME ?? give up here rather than possibly ending up going
4742 * through the whole file */
4743 if (avi->abort_buffering) {
4744 avi->abort_buffering = FALSE;
4745 gst_adapter_flush (avi->adapter, 8);
4749 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
4750 GST_FOURCC_ARGS (tag), size);
4752 stream_nr = CHUNKID_TO_STREAMNR (tag);
4754 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
4756 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
4757 stream_nr, GST_FOURCC_ARGS (tag));
4758 avi->offset += 8 + GST_ROUND_UP_2 (size);
4759 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4761 GstAviStream *stream;
4762 GstClockTime next_ts = 0;
4765 gst_adapter_flush (avi->adapter, 8);
4768 buf = gst_adapter_take_buffer (avi->adapter, GST_ROUND_UP_2 (size));
4769 /* patch the size */
4770 GST_BUFFER_SIZE (buf) = size;
4771 avi->offset += 8 + GST_ROUND_UP_2 (size);
4773 stream = &avi->stream[stream_nr];
4775 /* set delay (if any)
4776 if (stream->strh->init_frames == stream->current_frame &&
4778 stream->delay = next_ts;
4781 /* parsing of corresponding header may have failed */
4782 if (G_UNLIKELY (!stream->pad)) {
4783 GST_WARNING_OBJECT (avi, "no pad for stream ID %" GST_FOURCC_FORMAT,
4784 GST_FOURCC_ARGS (tag));
4785 gst_buffer_unref (buf);
4787 GstClockTime dur_ts = 0;
4789 /* get time of this buffer */
4790 gst_pad_query_position (stream->pad, &format, (gint64 *) & next_ts);
4791 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4794 stream->current_frame++;
4795 stream->current_byte += size;
4797 /* invert the picture if needed */
4798 buf = gst_avi_demux_invert (stream, buf);
4800 gst_pad_query_position (stream->pad, &format, (gint64 *) & dur_ts);
4801 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4804 GST_BUFFER_TIMESTAMP (buf) = next_ts;
4805 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
4806 if (stream->strh->type == GST_RIFF_FCC_vids)
4807 GST_BUFFER_OFFSET (buf) = stream->current_frame - 1;
4809 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4811 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4812 GST_DEBUG_OBJECT (avi,
4813 "Pushing buffer with time=%" GST_TIME_FORMAT ", duration %"
4814 GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
4815 " and size %d over pad %s", GST_TIME_ARGS (next_ts),
4816 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), GST_BUFFER_OFFSET (buf),
4817 size, GST_PAD_NAME (stream->pad));
4819 /* update current position in the segment */
4820 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, next_ts);
4822 /* mark discont when pending */
4823 if (G_UNLIKELY (stream->discont)) {
4824 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4825 stream->discont = FALSE;
4827 res = gst_pad_push (stream->pad, buf);
4830 res = gst_avi_demux_combine_flows (avi, stream, res);
4831 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4832 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
4845 GST_DEBUG_OBJECT (avi, "format %s != GST_FORMAT_TIME",
4846 gst_format_get_name (format));
4847 res = GST_FLOW_ERROR;
4853 * Send pending tags.
4856 push_tag_lists (GstAviDemux * avi)
4863 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
4865 for (i = 0; i < avi->num_streams; i++)
4866 if (avi->stream[i].pad && avi->stream[i].taglist) {
4867 GST_DEBUG_OBJECT (avi->stream[i].pad, "Tags: %" GST_PTR_FORMAT,
4868 avi->stream[i].taglist);
4869 gst_element_found_tags_for_pad (GST_ELEMENT (avi), avi->stream[i].pad,
4870 avi->stream[i].taglist);
4871 avi->stream[i].taglist = NULL;
4874 if (avi->globaltags == NULL)
4875 avi->globaltags = gst_tag_list_new ();
4877 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE,
4878 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
4880 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, avi->globaltags);
4881 gst_element_found_tags (GST_ELEMENT (avi), avi->globaltags);
4882 avi->globaltags = NULL;
4883 avi->got_tags = FALSE;
4887 gst_avi_demux_loop (GstPad * pad)
4890 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4892 switch (avi->state) {
4893 case GST_AVI_DEMUX_START:
4894 if (G_UNLIKELY ((res =
4895 gst_avi_demux_stream_init_pull (avi)) != GST_FLOW_OK)) {
4896 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4899 avi->state = GST_AVI_DEMUX_HEADER;
4901 case GST_AVI_DEMUX_HEADER:
4902 if (G_UNLIKELY ((res =
4903 gst_avi_demux_stream_header_pull (avi)) != GST_FLOW_OK)) {
4904 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4907 avi->state = GST_AVI_DEMUX_MOVI;
4909 case GST_AVI_DEMUX_MOVI:
4910 if (G_UNLIKELY (avi->seek_event)) {
4911 gst_avi_demux_push_event (avi, avi->seek_event);
4912 avi->seek_event = NULL;
4914 if (G_UNLIKELY (avi->got_tags)) {
4915 push_tag_lists (avi);
4917 /* process each index entry in turn */
4918 res = gst_avi_demux_loop_data (avi);
4920 /* pause when error */
4921 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4922 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
4927 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
4928 res = GST_FLOW_ERROR;
4932 GST_LOG_OBJECT (avi, "state: %d res:%s", avi->state, gst_flow_get_name (res));
4938 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
4939 avi->segment_running = FALSE;
4940 gst_pad_pause_task (avi->sinkpad);
4942 if (GST_FLOW_IS_FATAL (res) || (res == GST_FLOW_NOT_LINKED)) {
4943 gboolean push_eos = TRUE;
4945 if (res == GST_FLOW_UNEXPECTED) {
4946 /* handle end-of-stream/segment */
4947 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4950 if ((stop = avi->segment.stop) == -1)
4951 stop = avi->segment.duration;
4953 GST_INFO_OBJECT (avi, "sending segment_done");
4955 gst_element_post_message
4957 gst_message_new_segment_done (GST_OBJECT (avi), GST_FORMAT_TIME,
4962 /* for fatal errors we post an error message */
4963 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
4964 (_("Internal data stream error.")),
4965 ("streaming stopped, reason %s", gst_flow_get_name (res)));
4968 GST_INFO_OBJECT (avi, "sending eos");
4969 if (!gst_avi_demux_push_event (avi, gst_event_new_eos ()) &&
4970 (res == GST_FLOW_UNEXPECTED)) {
4971 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
4972 (NULL), ("got eos but no streams (yet)"));
4979 static GstFlowReturn
4980 gst_avi_demux_chain (GstPad * pad, GstBuffer * buf)
4983 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4985 GST_DEBUG ("Store %d bytes in adapter", GST_BUFFER_SIZE (buf));
4986 gst_adapter_push (avi->adapter, buf);
4988 switch (avi->state) {
4989 case GST_AVI_DEMUX_START:
4990 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
4991 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4995 case GST_AVI_DEMUX_HEADER:
4996 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
4997 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5001 case GST_AVI_DEMUX_MOVI:
5002 if (G_UNLIKELY (avi->seek_event)) {
5003 gst_avi_demux_push_event (avi, avi->seek_event);
5004 avi->seek_event = NULL;
5006 if (G_UNLIKELY (avi->got_tags)) {
5007 push_tag_lists (avi);
5009 res = gst_avi_demux_stream_data (avi);
5012 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
5013 ("Illegal internal state"));
5014 res = GST_FLOW_ERROR;
5018 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
5019 gst_flow_get_name (res));
5021 if (G_UNLIKELY (avi->abort_buffering)) {
5022 avi->abort_buffering = FALSE;
5023 res = GST_FLOW_ERROR;
5024 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, NULL, ("unhandled buffer size"));
5031 gst_avi_demux_sink_activate (GstPad * sinkpad)
5033 if (gst_pad_check_pull_range (sinkpad)) {
5034 GST_DEBUG ("going to pull mode");
5035 return gst_pad_activate_pull (sinkpad, TRUE);
5037 GST_DEBUG ("going to push (streaming) mode");
5038 return gst_pad_activate_push (sinkpad, TRUE);
5043 gst_avi_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
5045 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (sinkpad));
5048 avi->segment_running = TRUE;
5049 avi->streaming = FALSE;
5050 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
5053 avi->segment_running = FALSE;
5054 return gst_pad_stop_task (sinkpad);
5059 gst_avi_demux_activate_push (GstPad * pad, gboolean active)
5061 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (pad));
5064 GST_DEBUG ("avi: activating push/chain function");
5065 avi->streaming = TRUE;
5067 GST_DEBUG ("avi: deactivating push/chain function");
5073 static GstStateChangeReturn
5074 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
5076 GstStateChangeReturn ret;
5077 GstAviDemux *avi = GST_AVI_DEMUX (element);
5079 switch (transition) {
5080 case GST_STATE_CHANGE_READY_TO_PAUSED:
5081 avi->streaming = FALSE;
5082 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
5088 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5089 if (ret == GST_STATE_CHANGE_FAILURE)
5092 switch (transition) {
5093 case GST_STATE_CHANGE_PAUSED_TO_READY:
5094 gst_avi_demux_reset (avi);