2 * Copyright (C) <1999> Erik Walthinsen <omega@temple-baptist.com>
3 * Copyright (C) <2006> Nokia Corporation (contact <stefan.kost@nokia.com>)
4 * Copyright (C) <2009-2010> STEricsson <benjamin.gaignard@stericsson.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 /* Element-Checklist-Version: 5 */
24 * SECTION:element-avidemux
26 * Demuxes an .avi file into raw or compressed audio and/or video streams.
28 * This element supports both push and pull-based scheduling, depending on the
29 * capabilities of the upstream elements.
32 * <title>Example launch line</title>
34 * gst-launch filesrc location=test.avi ! avidemux name=demux demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink demux.video_00 ! queue ! decodebin ! ffmpegcolorspace ! videoscale ! autovideosink
35 * ]| Play (parse and decode) an .avi file and try to output it to
36 * an automatically detected soundcard and videosink. If the AVI file contains
37 * compressed audio or video data, this will only work if you have the
38 * right decoder elements/plugins installed.
41 * Last reviewed on 2006-12-29 (0.10.6)
51 #include "gst/riff/riff-media.h"
52 #include "gstavidemux.h"
54 #include <gst/gst-i18n-plugin.h>
55 #include <gst/base/gstadapter.h>
58 #define DIV_ROUND_UP(s,v) (((s) + ((v)-1)) / (v))
60 GST_DEBUG_CATEGORY_STATIC (avidemux_debug);
61 #define GST_CAT_DEFAULT avidemux_debug
63 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
66 GST_STATIC_CAPS ("video/x-msvideo")
69 static void gst_avi_demux_base_init (GstAviDemuxClass * klass);
70 static void gst_avi_demux_class_init (GstAviDemuxClass * klass);
71 static void gst_avi_demux_init (GstAviDemux * avi);
72 static void gst_avi_demux_finalize (GObject * object);
74 static void gst_avi_demux_reset (GstAviDemux * avi);
77 static const GstEventMask *gst_avi_demux_get_event_mask (GstPad * pad);
79 static gboolean gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event);
80 static gboolean gst_avi_demux_handle_sink_event (GstPad * pad,
82 static gboolean gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event);
85 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
87 static const GstQueryType *gst_avi_demux_get_src_query_types (GstPad * pad);
88 static gboolean gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query);
89 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
90 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
92 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
93 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
95 static gboolean gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
97 static void gst_avi_demux_loop (GstPad * pad);
98 static gboolean gst_avi_demux_sink_activate (GstPad * sinkpad);
99 static gboolean gst_avi_demux_sink_activate_pull (GstPad * sinkpad,
101 static gboolean gst_avi_demux_activate_push (GstPad * pad, gboolean active);
102 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstBuffer * buf);
104 static void gst_avi_demux_set_index (GstElement * element, GstIndex * index);
105 static GstIndex *gst_avi_demux_get_index (GstElement * element);
106 static GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
107 GstStateChange transition);
108 static void gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi);
109 static void gst_avi_demux_get_buffer_info (GstAviDemux * avi,
110 GstAviStream * stream, guint entry_n, GstClockTime * timestamp,
111 GstClockTime * ts_end, guint64 * offset, guint64 * offset_end);
113 static void gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf);
115 static GstElementClass *parent_class = NULL;
117 /* GObject methods */
120 gst_avi_demux_get_type (void)
122 static GType avi_demux_type = 0;
124 if (!avi_demux_type) {
125 static const GTypeInfo avi_demux_info = {
126 sizeof (GstAviDemuxClass),
127 (GBaseInitFunc) gst_avi_demux_base_init,
129 (GClassInitFunc) gst_avi_demux_class_init,
132 sizeof (GstAviDemux),
134 (GInstanceInitFunc) gst_avi_demux_init,
138 g_type_register_static (GST_TYPE_ELEMENT,
139 "GstAviDemux", &avi_demux_info, 0);
142 return avi_demux_type;
146 gst_avi_demux_base_init (GstAviDemuxClass * klass)
148 static const GstElementDetails gst_avi_demux_details =
149 GST_ELEMENT_DETAILS ("Avi demuxer",
151 "Demultiplex an avi file into audio and video",
152 "Erik Walthinsen <omega@cse.ogi.edu>\n"
153 "Wim Taymans <wim.taymans@chello.be>\n"
154 "Thijs Vermeir <thijsvermeir@gmail.com>");
155 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
156 GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl;
157 GstCaps *audcaps, *vidcaps, *subcaps;
159 audcaps = gst_riff_create_audio_template_caps ();
160 gst_caps_append (audcaps, gst_caps_new_simple ("audio/x-avi-unknown", NULL));
161 audiosrctempl = gst_pad_template_new ("audio_%02d",
162 GST_PAD_SRC, GST_PAD_SOMETIMES, audcaps);
164 vidcaps = gst_riff_create_video_template_caps ();
165 gst_caps_append (vidcaps, gst_riff_create_iavs_template_caps ());
166 gst_caps_append (vidcaps, gst_caps_new_simple ("video/x-avi-unknown", NULL));
167 videosrctempl = gst_pad_template_new ("video_%02d",
168 GST_PAD_SRC, GST_PAD_SOMETIMES, vidcaps);
170 subcaps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
171 subsrctempl = gst_pad_template_new ("subtitle_%02d",
172 GST_PAD_SRC, GST_PAD_SOMETIMES, subcaps);
173 gst_element_class_add_pad_template (element_class, audiosrctempl);
174 gst_element_class_add_pad_template (element_class, videosrctempl);
175 gst_element_class_add_pad_template (element_class, subsrctempl);
176 gst_element_class_add_pad_template (element_class,
177 gst_static_pad_template_get (&sink_templ));
178 gst_element_class_set_details (element_class, &gst_avi_demux_details);
182 gst_avi_demux_class_init (GstAviDemuxClass * klass)
184 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
185 GObjectClass *gobject_class = (GObjectClass *) klass;
187 GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
188 0, "Demuxer for AVI streams");
190 parent_class = g_type_class_peek_parent (klass);
192 gobject_class->finalize = gst_avi_demux_finalize;
193 gstelement_class->change_state =
194 GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
196 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_avi_demux_set_index);
197 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_avi_demux_get_index);
201 gst_avi_demux_init (GstAviDemux * avi)
203 avi->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
204 gst_pad_set_activate_function (avi->sinkpad,
205 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate));
206 gst_pad_set_activatepull_function (avi->sinkpad,
207 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate_pull));
208 gst_pad_set_activatepush_function (avi->sinkpad,
209 GST_DEBUG_FUNCPTR (gst_avi_demux_activate_push));
210 gst_pad_set_chain_function (avi->sinkpad,
211 GST_DEBUG_FUNCPTR (gst_avi_demux_chain));
212 gst_pad_set_event_function (avi->sinkpad,
213 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_sink_event));
214 gst_element_add_pad (GST_ELEMENT_CAST (avi), avi->sinkpad);
216 avi->adapter = gst_adapter_new ();
218 gst_avi_demux_reset (avi);
222 gst_avi_demux_finalize (GObject * object)
224 GstAviDemux *avi = GST_AVI_DEMUX (object);
226 GST_DEBUG ("AVI: finalize");
228 g_object_unref (avi->adapter);
230 G_OBJECT_CLASS (parent_class)->finalize (object);
234 gst_avi_demux_reset_stream (GstAviDemux * avi, GstAviStream * stream)
236 g_free (stream->strh);
237 g_free (stream->strf.data);
238 g_free (stream->name);
239 g_free (stream->index);
240 g_free (stream->indexes);
241 if (stream->initdata)
242 gst_buffer_unref (stream->initdata);
243 if (stream->extradata)
244 gst_buffer_unref (stream->extradata);
246 if (stream->exposed) {
247 gst_pad_set_active (stream->pad, FALSE);
248 gst_element_remove_pad (GST_ELEMENT_CAST (avi), stream->pad);
250 gst_object_unref (stream->pad);
252 if (stream->taglist) {
253 gst_tag_list_free (stream->taglist);
254 stream->taglist = NULL;
256 memset (stream, 0, sizeof (GstAviStream));
260 gst_avi_demux_reset (GstAviDemux * avi)
264 GST_DEBUG ("AVI: reset");
266 for (i = 0; i < avi->num_streams; i++)
267 gst_avi_demux_reset_stream (avi, &avi->stream[i]);
269 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
270 avi->num_streams = 0;
271 avi->num_v_streams = 0;
272 avi->num_a_streams = 0;
273 avi->num_t_streams = 0;
274 avi->main_stream = -1;
276 avi->state = GST_AVI_DEMUX_START;
279 avi->index_offset = 0;
283 if (avi->element_index)
284 gst_object_unref (avi->element_index);
285 avi->element_index = NULL;
287 if (avi->seg_event) {
288 gst_event_unref (avi->seg_event);
289 avi->seg_event = NULL;
293 gst_tag_list_free (avi->globaltags);
294 avi->globaltags = NULL;
296 avi->got_tags = TRUE; /* we always want to push global tags */
297 avi->have_eos = FALSE;
298 avi->seekable = TRUE;
300 gst_adapter_clear (avi->adapter);
302 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
306 /* GstElement methods */
309 static const GstFormat *
310 gst_avi_demux_get_src_formats (GstPad * pad)
312 GstAviStream *stream = gst_pad_get_element_private (pad);
314 static const GstFormat src_a_formats[] = {
320 static const GstFormat src_v_formats[] = {
326 return (stream->strh->type == GST_RIFF_FCC_auds ?
327 src_a_formats : src_v_formats);
331 /* assumes stream->strf.auds->av_bps != 0 */
332 static inline GstClockTime
333 avi_stream_convert_bytes_to_time_unchecked (GstAviStream * stream,
336 return gst_util_uint64_scale_int (bytes, GST_SECOND,
337 stream->strf.auds->av_bps);
340 static inline guint64
341 avi_stream_convert_time_to_bytes_unchecked (GstAviStream * stream,
344 return gst_util_uint64_scale_int (time, stream->strf.auds->av_bps,
348 /* assumes stream->strh->rate != 0 */
349 static inline GstClockTime
350 avi_stream_convert_frames_to_time_unchecked (GstAviStream * stream,
353 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
357 static inline guint64
358 avi_stream_convert_time_to_frames_unchecked (GstAviStream * stream,
361 return gst_util_uint64_scale (time, stream->strh->rate,
362 stream->strh->scale * GST_SECOND);
366 gst_avi_demux_src_convert (GstPad * pad,
367 GstFormat src_format,
368 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
370 GstAviStream *stream = gst_pad_get_element_private (pad);
374 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
375 ", dest_format:%s", gst_format_get_name (src_format), src_value,
376 gst_format_get_name (*dest_format));
378 if (G_UNLIKELY (src_format == *dest_format)) {
379 *dest_value = src_value;
382 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
386 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
387 (src_format == GST_FORMAT_BYTES
388 || *dest_format == GST_FORMAT_BYTES))) {
393 switch (src_format) {
394 case GST_FORMAT_TIME:
395 switch (*dest_format) {
396 case GST_FORMAT_BYTES:
397 *dest_value = gst_util_uint64_scale_int (src_value,
398 stream->strf.auds->av_bps, GST_SECOND);
400 case GST_FORMAT_DEFAULT:
402 gst_util_uint64_scale_round (src_value, stream->strh->rate,
403 stream->strh->scale * GST_SECOND);
410 case GST_FORMAT_BYTES:
411 switch (*dest_format) {
412 case GST_FORMAT_TIME:
413 if (stream->strf.auds->av_bps != 0) {
414 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
424 case GST_FORMAT_DEFAULT:
425 switch (*dest_format) {
426 case GST_FORMAT_TIME:
428 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
441 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
442 gst_format_get_name (*dest_format), *dest_value);
446 static const GstQueryType *
447 gst_avi_demux_get_src_query_types (GstPad * pad)
449 static const GstQueryType src_types[] = {
461 gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query)
464 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
466 GstAviStream *stream = gst_pad_get_element_private (pad);
468 if (!stream->strh || !stream->strf.data)
469 return gst_pad_query_default (pad, query);
471 switch (GST_QUERY_TYPE (query)) {
472 case GST_QUERY_POSITION:{
475 GST_DEBUG ("pos query for stream %u: frames %u, bytes %u",
476 stream->num, stream->current_entry, stream->current_total);
478 /* FIXME, this looks clumsy */
479 if (stream->strh->type == GST_RIFF_FCC_auds) {
480 if (stream->is_vbr) {
482 pos = gst_util_uint64_scale ((gint64) stream->current_entry *
483 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
484 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
485 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
486 } else if (stream->strf.auds->av_bps != 0) {
488 pos = gst_util_uint64_scale (stream->current_total, GST_SECOND,
489 (guint64) stream->strf.auds->av_bps);
490 GST_DEBUG_OBJECT (avi,
491 "CBR convert bytes %u, time %" GST_TIME_FORMAT,
492 stream->current_total, GST_TIME_ARGS (pos));
493 } else if (stream->idx_n != 0 && stream->total_bytes != 0) {
494 /* calculate timestamps based on percentage of length */
495 guint64 xlen = avi->avih->us_frame *
496 avi->avih->tot_frames * GST_USECOND;
498 if (stream->is_vbr) {
499 pos = gst_util_uint64_scale (xlen, stream->current_entry,
501 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
502 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
504 pos = gst_util_uint64_scale (xlen, stream->current_total,
505 stream->total_bytes);
506 GST_DEBUG_OBJECT (avi,
507 "CBR perc convert bytes %u, time %" GST_TIME_FORMAT,
508 stream->current_total, GST_TIME_ARGS (pos));
515 if (stream->strh->rate != 0) {
516 pos = gst_util_uint64_scale ((guint64) stream->current_entry *
517 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
519 pos = stream->current_entry * avi->avih->us_frame * GST_USECOND;
523 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
524 gst_query_set_position (query, GST_FORMAT_TIME, pos);
526 GST_WARNING ("pos query failed");
529 case GST_QUERY_DURATION:
532 GstClockTime duration;
534 /* only act on audio or video streams */
535 if (stream->strh->type != GST_RIFF_FCC_auds &&
536 stream->strh->type != GST_RIFF_FCC_vids) {
541 /* take stream duration, fall back to avih duration */
542 if ((duration = stream->duration) == -1)
543 duration = avi->duration;
545 gst_query_parse_duration (query, &fmt, NULL);
548 case GST_FORMAT_TIME:
549 gst_query_set_duration (query, fmt, duration);
551 case GST_FORMAT_DEFAULT:
554 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
557 if (stream->idx_n >= 0)
558 gst_query_set_duration (query, fmt, stream->idx_n);
559 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
560 duration, &fmt, &dur))
561 gst_query_set_duration (query, fmt, dur);
570 case GST_QUERY_SEEKING:{
573 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
574 if (fmt == GST_FORMAT_TIME) {
575 gboolean seekable = TRUE;
577 if (avi->streaming) {
578 seekable = avi->seekable;
581 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
582 0, stream->duration);
587 case GST_QUERY_CONVERT:{
588 GstFormat src_fmt, dest_fmt;
589 gint64 src_val, dest_val;
591 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
592 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
594 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
596 res = gst_pad_query_default (pad, query);
600 res = gst_pad_query_default (pad, query);
604 gst_object_unref (avi);
609 static const GstEventMask *
610 gst_avi_demux_get_event_mask (GstPad * pad)
612 static const GstEventMask masks[] = {
613 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
622 gst_avi_demux_seek_streams (GstAviDemux * avi, guint64 offset, gboolean before)
624 GstAviStream *stream;
625 GstIndexEntry *entry;
627 gint64 val, min = offset;
629 for (i = 0; i < avi->num_streams; i++) {
630 stream = &avi->stream[i];
632 entry = gst_index_get_assoc_entry (avi->element_index, stream->index_id,
633 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
634 GST_ASSOCIATION_FLAG_NONE, GST_FORMAT_BYTES, offset);
638 GST_DEBUG_OBJECT (avi, "stream %d, previous entry at %"
639 G_GUINT64_FORMAT, i, val);
640 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
648 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
649 stream->current_entry = 0;
650 stream->current_total = 0;
654 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
655 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT,
658 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &val);
659 stream->current_total = val;
660 gst_index_entry_assoc_map (entry, GST_FORMAT_DEFAULT, &val);
661 stream->current_entry = val;
668 gst_avi_demux_index_entry_offset_search (GstAviIndexEntry * entry,
671 if (entry->offset < *offset)
673 else if (entry->offset > *offset)
679 gst_avi_demux_seek_streams_index (GstAviDemux * avi, guint64 offset,
682 GstAviStream *stream;
683 GstAviIndexEntry *entry;
685 gint64 val, min = offset;
688 for (i = 0; i < avi->num_streams; i++) {
689 stream = &avi->stream[i];
692 gst_util_array_binary_search (stream->index, stream->idx_n,
693 sizeof (GstAviIndexEntry),
694 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
695 before ? GST_SEARCH_MODE_BEFORE : GST_SEARCH_MODE_AFTER, &offset, NULL);
698 index = entry - stream->index;
702 GST_DEBUG_OBJECT (avi,
703 "stream %d, previous entry at %" G_GUINT64_FORMAT, i, val);
704 val = stream->index[index].offset;
712 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
713 stream->current_entry = 0;
714 stream->current_total = 0;
718 val = stream->index[index].offset;
719 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT, i,
722 gst_avi_demux_get_buffer_info (avi, stream, index, (GstClockTime *) & val,
724 stream->current_total = val;
725 stream->current_entry = index;
731 #define GST_AVI_SEEK_PUSH_DISPLACE (4 * GST_SECOND)
734 gst_avi_demux_handle_sink_event (GstPad * pad, GstEvent * event)
737 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
739 GST_DEBUG_OBJECT (avi,
740 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
742 switch (GST_EVENT_TYPE (event)) {
743 case GST_EVENT_NEWSEGMENT:
747 gint64 start, stop, time, offset = 0;
751 /* some debug output */
752 gst_segment_init (&segment, GST_FORMAT_UNDEFINED);
753 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
754 &start, &stop, &time);
755 gst_segment_set_newsegment_full (&segment, update, rate, arate, format,
757 GST_DEBUG_OBJECT (avi,
758 "received format %d newsegment %" GST_SEGMENT_FORMAT, format,
761 /* chain will send initial newsegment after pads have been added */
762 if (avi->state != GST_AVI_DEMUX_MOVI) {
763 GST_DEBUG_OBJECT (avi, "still starting, eating event");
767 /* we only expect a BYTE segment, e.g. following a seek */
768 if (format != GST_FORMAT_BYTES) {
769 GST_DEBUG_OBJECT (avi, "unsupported segment format, ignoring");
773 if (avi->have_index) {
774 GstAviIndexEntry *entry;
776 /* FIXME, this code assumes the main stream with keyframes is stream 0,
777 * which is mostly correct... */
778 GstAviStream *stream = &avi->stream[avi->main_stream];
780 /* find the index for start bytes offset, calculate the corresponding
781 * time and (reget?) start offset in bytes */
782 entry = gst_util_array_binary_search (stream->index,
783 stream->idx_n, sizeof (GstAviIndexEntry),
784 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
785 GST_SEARCH_MODE_BEFORE, &start, NULL);
790 index = entry - stream->index;
793 start = stream->index[index].offset;
794 gst_avi_demux_get_buffer_info (avi, stream, index,
795 (GstClockTime *) & time, NULL, NULL, NULL);
796 } else if (avi->element_index) {
797 GstIndexEntry *entry;
799 /* Let's check if we have an index entry for this position */
800 entry = gst_index_get_assoc_entry (avi->element_index, avi->index_id,
801 GST_INDEX_LOOKUP_AFTER, GST_ASSOCIATION_FLAG_NONE,
802 GST_FORMAT_BYTES, start);
804 /* we can not go where we have not yet been before ... */
806 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
810 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &time);
811 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &start);
813 GST_WARNING_OBJECT (avi, "no index data, forcing EOS");
818 stop = GST_CLOCK_TIME_NONE;
820 /* set up segment and send downstream */
821 gst_segment_set_newsegment_full (&avi->segment, update, rate, arate,
822 GST_FORMAT_TIME, time, stop, time);
823 GST_DEBUG_OBJECT (avi, "Pushing newseg update %d, rate %g, "
824 "applied rate %g, format %d, start %" G_GINT64_FORMAT ", "
825 "stop %" G_GINT64_FORMAT, update, rate, arate, GST_FORMAT_TIME,
827 gst_avi_demux_push_event (avi,
828 gst_event_new_new_segment_full (update, rate, arate, GST_FORMAT_TIME,
831 GST_DEBUG_OBJECT (avi, "next chunk expected at %" G_GINT64_FORMAT, start);
833 /* adjust state for streaming thread accordingly */
834 avi->offset = offset;
836 gst_avi_demux_seek_streams_index (avi, offset, FALSE);
838 gst_avi_demux_seek_streams (avi, offset, FALSE);
840 /* set up streaming thread */
841 avi->offset = offset;
842 avi->todrop = start - offset;
845 gst_event_unref (event);
850 avi->have_eos = TRUE;
855 if (avi->state != GST_AVI_DEMUX_MOVI) {
856 gst_event_unref (event);
857 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
858 (NULL), ("got eos and didn't receive a complete header object"));
859 } else if (!gst_avi_demux_push_event (avi, event)) {
860 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
861 (NULL), ("got eos but no streams (yet)"));
865 case GST_EVENT_FLUSH_STOP:
869 gst_adapter_clear (avi->adapter);
870 avi->have_eos = FALSE;
871 for (i = 0; i < avi->num_streams; i++) {
872 avi->stream[i].last_flow = GST_FLOW_OK;
873 avi->stream[i].discont = TRUE;
875 /* fall through to default case so that the event gets passed downstream */
878 res = gst_pad_event_default (pad, event);
882 gst_object_unref (avi);
888 gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event)
891 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
893 GST_DEBUG_OBJECT (avi,
894 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
896 switch (GST_EVENT_TYPE (event)) {
898 /* handle seeking only in pull mode */
899 if (!avi->streaming) {
900 res = gst_avi_demux_handle_seek (avi, pad, event);
902 res = gst_avi_demux_handle_seek_push (avi, pad, event);
904 gst_event_unref (event);
907 case GST_EVENT_NAVIGATION:
909 gst_event_unref (event);
912 res = gst_pad_event_default (pad, event);
916 gst_object_unref (avi);
921 /* streaming helper (push) */
924 * gst_avi_demux_peek_chunk_info:
926 * @tag: holder for tag
927 * @size: holder for tag size
929 * Peek next chunk info (tag and size)
931 * Returns: TRUE when one chunk info has been got
934 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
936 const guint8 *data = NULL;
938 if (gst_adapter_available (avi->adapter) < 8)
941 data = gst_adapter_peek (avi->adapter, 8);
942 *tag = GST_READ_UINT32_LE (data);
943 *size = GST_READ_UINT32_LE (data + 4);
949 * gst_avi_demux_peek_chunk:
951 * @tag: holder for tag
952 * @size: holder for tag size
954 * Peek enough data for one full chunk
956 * Returns: %TRUE when one chunk has been got
959 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
961 guint32 peek_size = 0;
964 if (!gst_avi_demux_peek_chunk_info (avi, tag, size))
967 /* size 0 -> empty data buffer would surprise most callers,
968 * large size -> do not bother trying to squeeze that into adapter,
969 * so we throw poor man's exception, which can be caught if caller really
970 * wants to handle 0 size chunk */
971 if (!(*size) || (*size) >= (1 << 30))
974 peek_size = (*size + 1) & ~1;
975 available = gst_adapter_available (avi->adapter);
977 GST_DEBUG_OBJECT (avi,
978 "Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
979 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
981 if (available < (8 + peek_size))
989 GST_INFO_OBJECT (avi, "Failed to peek");
994 GST_INFO_OBJECT (avi,
995 "Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT, *size,
996 GST_FOURCC_ARGS (*tag));
997 /* chain should give up */
998 avi->abort_buffering = TRUE;
1003 GST_INFO_OBJECT (avi, "need more %d < %" G_GUINT32_FORMAT,
1004 available, 8 + peek_size);
1012 * gst_avi_demux_parse_file_header:
1013 * @element: caller element (used for errors/debug).
1014 * @buf: input data to be used for parsing.
1016 * "Open" a RIFF/AVI file. The buffer should be at least 12
1017 * bytes long. Takes ownership of @buf.
1019 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
1020 * Throws an error, caller should error out (fatal).
1023 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
1028 stamp = gst_util_get_timestamp ();
1030 /* riff_parse posts an error */
1031 if (!gst_riff_parse_file_header (element, buf, &doctype))
1034 if (doctype != GST_RIFF_RIFF_AVI)
1037 stamp = gst_util_get_timestamp () - stamp;
1038 GST_DEBUG_OBJECT (element, "header parsing took %" GST_TIME_FORMAT,
1039 GST_TIME_ARGS (stamp));
1046 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
1047 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
1048 GST_FOURCC_ARGS (doctype)));
1054 * Read AVI file tag when streaming
1056 static GstFlowReturn
1057 gst_avi_demux_stream_init_push (GstAviDemux * avi)
1059 if (gst_adapter_available (avi->adapter) >= 12) {
1062 tmp = gst_adapter_take_buffer (avi->adapter, 12);
1064 GST_DEBUG ("Parsing avi header");
1065 if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), tmp)) {
1066 return GST_FLOW_ERROR;
1068 GST_DEBUG ("header ok");
1071 avi->state = GST_AVI_DEMUX_HEADER;
1079 static GstFlowReturn
1080 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
1083 GstBuffer *buf = NULL;
1085 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
1086 if (res != GST_FLOW_OK)
1088 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
1098 GST_DEBUG_OBJECT (avi, "error parsing file header");
1099 return GST_FLOW_ERROR;
1103 /* AVI header handling */
1106 * gst_avi_demux_parse_avih:
1107 * @avi: caller element (used for errors/debug).
1108 * @buf: input data to be used for parsing.
1109 * @avih: pointer to structure (filled in by function) containing
1110 * stream information (such as flags, number of streams, etc.).
1112 * Read 'avih' header. Discards buffer after use.
1114 * Returns: TRUE on success, FALSE otherwise. Throws an error if
1115 * the header is invalid. The caller should error out
1119 gst_avi_demux_parse_avih (GstAviDemux * avi,
1120 GstBuffer * buf, gst_riff_avih ** _avih)
1122 gst_riff_avih *avih;
1127 if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_avih))
1128 goto avih_too_small;
1130 avih = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1132 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1133 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
1134 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
1135 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
1136 avih->flags = GUINT32_FROM_LE (avih->flags);
1137 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
1138 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
1139 avih->streams = GUINT32_FROM_LE (avih->streams);
1140 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
1141 avih->width = GUINT32_FROM_LE (avih->width);
1142 avih->height = GUINT32_FROM_LE (avih->height);
1143 avih->scale = GUINT32_FROM_LE (avih->scale);
1144 avih->rate = GUINT32_FROM_LE (avih->rate);
1145 avih->start = GUINT32_FROM_LE (avih->start);
1146 avih->length = GUINT32_FROM_LE (avih->length);
1150 GST_INFO_OBJECT (avi, "avih tag found:");
1151 GST_INFO_OBJECT (avi, " us_frame %u", avih->us_frame);
1152 GST_INFO_OBJECT (avi, " max_bps %u", avih->max_bps);
1153 GST_INFO_OBJECT (avi, " pad_gran %u", avih->pad_gran);
1154 GST_INFO_OBJECT (avi, " flags 0x%08x", avih->flags);
1155 GST_INFO_OBJECT (avi, " tot_frames %u", avih->tot_frames);
1156 GST_INFO_OBJECT (avi, " init_frames %u", avih->init_frames);
1157 GST_INFO_OBJECT (avi, " streams %u", avih->streams);
1158 GST_INFO_OBJECT (avi, " bufsize %u", avih->bufsize);
1159 GST_INFO_OBJECT (avi, " width %u", avih->width);
1160 GST_INFO_OBJECT (avi, " height %u", avih->height);
1161 GST_INFO_OBJECT (avi, " scale %u", avih->scale);
1162 GST_INFO_OBJECT (avi, " rate %u", avih->rate);
1163 GST_INFO_OBJECT (avi, " start %u", avih->start);
1164 GST_INFO_OBJECT (avi, " length %u", avih->length);
1167 gst_buffer_unref (buf);
1169 if (avih->us_frame != 0 && avih->tot_frames != 0)
1171 (guint64) avih->us_frame * (guint64) avih->tot_frames * 1000;
1173 avi->duration = GST_CLOCK_TIME_NONE;
1175 GST_INFO_OBJECT (avi, " header duration %" GST_TIME_FORMAT,
1176 GST_TIME_ARGS (avi->duration));
1183 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No buffer"));
1188 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
1189 ("Too small avih (%d available, %d needed)",
1190 GST_BUFFER_SIZE (buf), (int) sizeof (gst_riff_avih)));
1191 gst_buffer_unref (buf);
1197 * gst_avi_demux_parse_superindex:
1198 * @avi: caller element (used for debugging/errors).
1199 * @buf: input data to use for parsing.
1200 * @locations: locations in the file (byte-offsets) that contain
1201 * the actual indexes (see get_avi_demux_parse_subindex()).
1202 * The array ends with GST_BUFFER_OFFSET_NONE.
1204 * Reads superindex (openDML-2 spec stuff) from the provided data.
1206 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
1207 * on error, but they are not fatal.
1210 gst_avi_demux_parse_superindex (GstAviDemux * avi,
1211 GstBuffer * buf, guint64 ** _indexes)
1221 size = buf ? GST_BUFFER_SIZE (buf) : 0;
1225 data = GST_BUFFER_DATA (buf);
1227 /* check type of index. The opendml2 specs state that
1228 * there should be 4 dwords per array entry. Type can be
1229 * either frame or field (and we don't care). */
1230 if (GST_READ_UINT16_LE (data) != 4 ||
1231 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
1232 GST_WARNING_OBJECT (avi,
1233 "Superindex for stream has unexpected "
1234 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1235 GST_READ_UINT16_LE (data), data[2], data[3]);
1236 bpe = GST_READ_UINT16_LE (data) * 4;
1238 num = GST_READ_UINT32_LE (&data[4]);
1240 GST_DEBUG_OBJECT (avi, "got %d indexes", num);
1242 indexes = g_new (guint64, num + 1);
1243 for (i = 0; i < num; i++) {
1244 if (size < 24 + bpe * (i + 1))
1246 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
1247 GST_DEBUG_OBJECT (avi, "index %d at %" G_GUINT64_FORMAT, i, indexes[i]);
1249 indexes[i] = GST_BUFFER_OFFSET_NONE;
1250 *_indexes = indexes;
1252 gst_buffer_unref (buf);
1259 GST_ERROR_OBJECT (avi,
1260 "Not enough data to parse superindex (%d available, 24 needed)", size);
1262 gst_buffer_unref (buf);
1267 /* add an entry to the index of a stream. @num should be an estimate of the
1268 * total amount of index entries for all streams and is used to dynamically
1269 * allocate memory for the index entries. */
1270 static inline gboolean
1271 gst_avi_demux_add_index (GstAviDemux * avi, GstAviStream * stream,
1272 guint num, GstAviIndexEntry * entry)
1274 /* ensure index memory */
1275 if (G_UNLIKELY (stream->idx_n >= stream->idx_max)) {
1276 guint idx_max = stream->idx_max;
1277 GstAviIndexEntry *new_idx;
1279 /* we need to make some more room */
1281 /* initial size guess, assume each stream has an equal amount of entries,
1282 * overshoot with at least 8K */
1283 idx_max = (num / avi->num_streams) + (8192 / sizeof (GstAviIndexEntry));
1285 idx_max += 8192 / sizeof (GstAviIndexEntry);
1286 GST_DEBUG_OBJECT (avi, "expanded index from %u to %u",
1287 stream->idx_max, idx_max);
1289 new_idx = g_try_renew (GstAviIndexEntry, stream->index, idx_max);
1290 /* out of memory, if this fails stream->index is untouched. */
1291 if (G_UNLIKELY (!new_idx))
1294 stream->index = new_idx;
1295 stream->idx_max = idx_max;
1298 /* update entry total and stream stats. The entry total can be converted to
1299 * the timestamp of the entry easily. */
1300 if (stream->strh->type == GST_RIFF_FCC_auds) {
1303 if (stream->is_vbr) {
1304 entry->total = stream->total_blocks;
1306 entry->total = stream->total_bytes;
1308 blockalign = stream->strf.auds->blockalign;
1310 stream->total_blocks += DIV_ROUND_UP (entry->size, blockalign);
1312 stream->total_blocks++;
1314 if (stream->is_vbr) {
1315 entry->total = stream->idx_n;
1317 entry->total = stream->total_bytes;
1320 stream->total_bytes += entry->size;
1321 if (ENTRY_IS_KEYFRAME (entry))
1322 stream->n_keyframes++;
1325 GST_LOG_OBJECT (avi,
1326 "Adding stream %u, index entry %d, kf %d, size %u "
1327 ", offset %" G_GUINT64_FORMAT ", total %" G_GUINT64_FORMAT, stream->num,
1328 stream->idx_n, ENTRY_IS_KEYFRAME (entry), entry->size, entry->offset,
1330 stream->index[stream->idx_n++] = *entry;
1335 /* given @entry_n in @stream, calculate info such as timestamps and
1336 * offsets for the entry. */
1338 gst_avi_demux_get_buffer_info (GstAviDemux * avi, GstAviStream * stream,
1339 guint entry_n, GstClockTime * timestamp, GstClockTime * ts_end,
1340 guint64 * offset, guint64 * offset_end)
1342 GstAviIndexEntry *entry;
1344 entry = &stream->index[entry_n];
1346 if (stream->is_vbr) {
1347 /* VBR stream next timestamp */
1348 if (stream->strh->type == GST_RIFF_FCC_auds) {
1351 avi_stream_convert_frames_to_time_unchecked (stream, entry->total);
1353 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1358 avi_stream_convert_frames_to_time_unchecked (stream, entry_n);
1360 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1363 } else if (stream->strh->type == GST_RIFF_FCC_auds) {
1364 /* constant rate stream */
1367 avi_stream_convert_bytes_to_time_unchecked (stream, entry->total);
1369 *ts_end = avi_stream_convert_bytes_to_time_unchecked (stream,
1370 entry->total + entry->size);
1372 if (stream->strh->type == GST_RIFF_FCC_vids) {
1373 /* video offsets are the frame number */
1377 *offset_end = entry_n + 1;
1379 /* no offsets for audio */
1387 /* collect and debug stats about the indexes for all streams.
1388 * This method is also responsible for filling in the stream duration
1389 * as measured by the amount of index entries.
1391 * Returns TRUE if the index is not empty, else FALSE */
1393 gst_avi_demux_do_index_stats (GstAviDemux * avi)
1395 guint total_idx = 0;
1397 #ifndef GST_DISABLE_GST_DEBUG
1398 guint total_max = 0;
1401 /* get stream stats now */
1402 for (i = 0; i < avi->num_streams; i++) {
1403 GstAviStream *stream;
1405 if (G_UNLIKELY (!(stream = &avi->stream[i])))
1407 if (G_UNLIKELY (!stream->strh))
1409 if (G_UNLIKELY (!stream->index || stream->idx_n == 0))
1412 /* we interested in the end_ts of the last entry, which is the total
1413 * duration of this stream */
1414 gst_avi_demux_get_buffer_info (avi, stream, stream->idx_n - 1,
1415 NULL, &stream->idx_duration, NULL, NULL);
1417 total_idx += stream->idx_n;
1418 #ifndef GST_DISABLE_GST_DEBUG
1419 total_max += stream->idx_max;
1421 GST_INFO_OBJECT (avi, "Stream %d, dur %" GST_TIME_FORMAT ", %6u entries, "
1422 "%5u keyframes, entry size = %2u, total size = %10u, allocated %10u",
1423 i, GST_TIME_ARGS (stream->idx_duration), stream->idx_n,
1424 stream->n_keyframes, (guint) sizeof (GstAviIndexEntry),
1425 (guint) (stream->idx_n * sizeof (GstAviIndexEntry)),
1426 (guint) (stream->idx_max * sizeof (GstAviIndexEntry)));
1428 total_idx *= sizeof (GstAviIndexEntry);
1429 #ifndef GST_DISABLE_GST_DEBUG
1430 total_max *= sizeof (GstAviIndexEntry);
1432 GST_INFO_OBJECT (avi, "%u bytes for index vs %u ideally, %u wasted",
1433 total_max, total_idx, total_max - total_idx);
1435 if (total_idx == 0) {
1436 GST_WARNING_OBJECT (avi, "Index is empty !");
1443 * gst_avi_demux_parse_subindex:
1445 * @buf: input data to use for parsing.
1446 * @stream: stream context.
1447 * @entries_list: a list (returned by the function) containing all the
1448 * indexes parsed in this specific subindex. The first
1449 * entry is also a pointer to allocated memory that needs
1450 * to be free´ed. May be NULL if no supported indexes were
1453 * Reads superindex (openDML-2 spec stuff) from the provided data.
1454 * The buffer should contain a GST_RIFF_TAG_ix?? chunk.
1456 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1457 * throw an error, caller should bail out asap.
1460 gst_avi_demux_parse_subindex (GstAviDemux * avi, GstAviStream * stream,
1472 size = GST_BUFFER_SIZE (buf);
1478 data = GST_BUFFER_DATA (buf);
1480 /* We don't support index-data yet */
1482 goto not_implemented;
1484 /* check type of index. The opendml2 specs state that
1485 * there should be 4 dwords per array entry. Type can be
1486 * either frame or field (and we don't care). */
1487 bpe = (data[2] & 0x01) ? 12 : 8;
1488 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1489 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1490 GST_WARNING_OBJECT (avi,
1491 "Superindex for stream %d has unexpected "
1492 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1493 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1494 bpe = GST_READ_UINT16_LE (data) * 4;
1496 num = GST_READ_UINT32_LE (&data[4]);
1497 baseoff = GST_READ_UINT64_LE (&data[12]);
1499 /* If there's nothing, just return ! */
1503 GST_INFO_OBJECT (avi, "Parsing subindex, nr_entries = %6d", num);
1505 for (i = 0; i < num; i++) {
1506 GstAviIndexEntry entry;
1508 if (size < 24 + bpe * (i + 1))
1511 /* fill in offset and size. offset contains the keyframe flag in the
1513 entry.offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1514 entry.size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1516 if (stream->strh->type == GST_RIFF_FCC_auds) {
1517 /* all audio frames are keyframes */
1518 ENTRY_SET_KEYFRAME (&entry);
1520 /* else read flags */
1521 entry.flags = (entry.size & 0x80000000) ? 0 : GST_AVI_KEYFRAME;
1523 entry.size &= ~0x80000000;
1526 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
1529 gst_buffer_unref (buf);
1536 GST_ERROR_OBJECT (avi,
1537 "Not enough data to parse subindex (%d available, 24 needed)", size);
1538 gst_buffer_unref (buf);
1539 return TRUE; /* continue */
1543 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1544 ("Subindex-is-data is not implemented"));
1545 gst_buffer_unref (buf);
1550 GST_DEBUG_OBJECT (avi, "the index is empty");
1551 gst_buffer_unref (buf);
1556 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1557 ("Cannot allocate memory for %u*%u=%u bytes",
1558 (guint) sizeof (GstAviIndexEntry), num,
1559 (guint) sizeof (GstAviIndexEntry) * num));
1560 gst_buffer_unref (buf);
1566 * Create and push a flushing seek event upstream
1569 avi_demux_do_push_seek (GstAviDemux * demux, guint64 offset)
1574 GST_DEBUG_OBJECT (demux, "Seeking to %" G_GUINT64_FORMAT, offset);
1577 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1578 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1579 GST_SEEK_TYPE_NONE, -1);
1581 res = gst_pad_push_event (demux->sinkpad, event);
1584 demux->offset = offset;
1589 * Read AVI index when streaming
1592 gst_avi_demux_read_subindexes_push (GstAviDemux * avi)
1594 guint32 tag = 0, size;
1595 GstBuffer *buf = NULL;
1597 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1599 if (avi->odml_subidxs[avi->odml_subidx] != avi->offset)
1602 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1605 if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + avi->odml_stream / 10,
1606 '0' + avi->odml_stream % 10)) &&
1607 (tag != GST_MAKE_FOURCC ('0' + avi->odml_stream / 10,
1608 '0' + avi->odml_stream % 10, 'i', 'x'))) {
1609 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1610 GST_FOURCC_ARGS (tag));
1614 avi->offset += 8 + GST_ROUND_UP_2 (size);
1615 /* flush chunk header so we get just the 'size' payload data */
1616 gst_adapter_flush (avi->adapter, 8);
1617 buf = gst_adapter_take_buffer (avi->adapter, size);
1619 if (!gst_avi_demux_parse_subindex (avi, &avi->stream[avi->odml_stream], buf))
1622 if (avi->odml_subidxs[++avi->odml_subidx] == GST_BUFFER_OFFSET_NONE) {
1623 avi->odml_subidx = 0;
1624 if (++avi->odml_stream < avi->num_streams) {
1625 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
1627 /* get stream stats now */
1628 avi->have_index = gst_avi_demux_do_index_stats (avi);
1634 return avi_demux_do_push_seek (avi, avi->odml_subidxs[avi->odml_subidx]);
1641 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi)
1647 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1649 for (n = 0; n < avi->num_streams; n++) {
1650 GstAviStream *stream = &avi->stream[n];
1652 if (stream->indexes == NULL)
1655 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1656 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi), avi->sinkpad,
1657 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1659 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1660 '0' + stream->num % 10)) &&
1661 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1662 '0' + stream->num % 10, 'i', 'x'))) {
1663 /* Some ODML files (created by god knows what muxer) have a ##ix format
1664 * instead of the 'official' ix##. They are still valid though. */
1665 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1666 GST_FOURCC_ARGS (tag));
1667 gst_buffer_unref (buf);
1671 if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1675 g_free (stream->indexes);
1676 stream->indexes = NULL;
1678 /* get stream stats now */
1679 avi->have_index = gst_avi_demux_do_index_stats (avi);
1683 * gst_avi_demux_riff_parse_vprp:
1684 * @element: caller element (used for debugging/error).
1685 * @buf: input data to be used for parsing, stripped from header.
1686 * @vprp: a pointer (returned by this function) to a filled-in vprp
1687 * structure. Caller should free it.
1689 * Parses a video stream´s vprp. This function takes ownership of @buf.
1691 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1692 * should be skipped on error, but it is not fatal.
1695 gst_avi_demux_riff_parse_vprp (GstElement * element,
1696 GstBuffer * buf, gst_riff_vprp ** _vprp)
1698 gst_riff_vprp *vprp;
1701 g_return_val_if_fail (buf != NULL, FALSE);
1702 g_return_val_if_fail (_vprp != NULL, FALSE);
1704 if (GST_BUFFER_SIZE (buf) < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1707 vprp = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1709 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1710 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1711 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1712 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1713 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1714 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1715 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1716 vprp->width = GUINT32_FROM_LE (vprp->width);
1717 vprp->height = GUINT32_FROM_LE (vprp->height);
1718 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1722 /* calculate fields based on size */
1723 k = (GST_BUFFER_SIZE (buf) - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) /
1725 if (vprp->fields > k) {
1726 GST_WARNING_OBJECT (element,
1727 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1730 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1731 GST_WARNING_OBJECT (element,
1732 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1733 GST_RIFF_VPRP_VIDEO_FIELDS);
1734 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1736 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1737 for (k = 0; k < vprp->fields; k++) {
1738 gst_riff_vprp_video_field_desc *fd;
1740 fd = &vprp->field_info[k];
1741 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1742 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1743 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1744 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1745 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1746 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1747 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1748 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1753 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1754 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1755 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1756 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1757 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1758 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1759 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1760 vprp->aspect & 0xffff);
1761 GST_INFO_OBJECT (element, " width %d", vprp->width);
1762 GST_INFO_OBJECT (element, " height %d", vprp->height);
1763 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1764 for (k = 0; k < vprp->fields; k++) {
1765 gst_riff_vprp_video_field_desc *fd;
1767 fd = &(vprp->field_info[k]);
1768 GST_INFO_OBJECT (element, " field %u description:", k);
1769 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1770 fd->compressed_bm_height);
1771 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1772 fd->compressed_bm_width);
1773 GST_INFO_OBJECT (element, " valid_bm_height %d",
1774 fd->valid_bm_height);
1775 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1776 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1777 fd->valid_bm_x_offset);
1778 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1779 fd->valid_bm_y_offset);
1780 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1781 fd->video_x_t_offset);
1782 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1785 gst_buffer_unref (buf);
1794 GST_ERROR_OBJECT (element,
1795 "Too small vprp (%d available, at least %d needed)",
1796 GST_BUFFER_SIZE (buf),
1797 (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1798 gst_buffer_unref (buf);
1804 gst_avi_demux_expose_streams (GstAviDemux * avi, gboolean force)
1808 GST_DEBUG_OBJECT (avi, "force : %d", force);
1810 for (i = 0; i < avi->num_streams; i++) {
1811 GstAviStream *stream = &avi->stream[i];
1813 if (force || stream->idx_n != 0) {
1814 GST_LOG_OBJECT (avi, "Added pad %s with caps %" GST_PTR_FORMAT,
1815 GST_PAD_NAME (stream->pad), GST_PAD_CAPS (stream->pad));
1816 gst_element_add_pad ((GstElement *) avi, stream->pad);
1817 stream->exposed = TRUE;
1818 if (avi->main_stream == -1)
1819 avi->main_stream = i;
1821 GST_WARNING_OBJECT (avi, "Stream #%d doesn't have any entry, removing it",
1823 gst_avi_demux_reset_stream (avi, stream);
1829 * gst_avi_demux_parse_stream:
1830 * @avi: calling element (used for debugging/errors).
1831 * @buf: input buffer used to parse the stream.
1833 * Parses all subchunks in a strl chunk (which defines a single
1834 * stream). Discards the buffer after use. This function will
1835 * increment the stream counter internally.
1837 * Returns: whether the stream was identified successfully.
1838 * Errors are not fatal. It does indicate the stream
1842 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
1844 GstAviStream *stream;
1845 GstElementClass *klass;
1846 GstPadTemplate *templ;
1847 GstBuffer *sub = NULL;
1850 gchar *codec_name = NULL, *padname = NULL;
1851 const gchar *tag_name;
1852 GstCaps *caps = NULL;
1854 GstElement *element;
1855 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
1856 gst_riff_vprp *vprp = NULL;
1858 element = GST_ELEMENT_CAST (avi);
1860 GST_DEBUG_OBJECT (avi, "Parsing stream");
1862 if (avi->num_streams >= GST_AVI_DEMUX_MAX_STREAMS) {
1863 GST_WARNING_OBJECT (avi,
1864 "maximum no of streams (%d) exceeded, ignoring stream",
1865 GST_AVI_DEMUX_MAX_STREAMS);
1866 gst_buffer_unref (buf);
1867 /* not a fatal error, let's say */
1871 stream = &avi->stream[avi->num_streams];
1873 /* initial settings */
1874 stream->idx_duration = GST_CLOCK_TIME_NONE;
1875 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1876 stream->duration = GST_CLOCK_TIME_NONE;
1878 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
1879 /* sub can be NULL if the chunk is empty */
1881 GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
1882 GST_FOURCC_ARGS (tag));
1886 case GST_RIFF_TAG_strh:
1888 gst_riff_strh *strh;
1891 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
1894 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
1895 /* ownership given away */
1897 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
1901 strh = stream->strh;
1902 /* sanity check; stream header frame rate matches global header
1904 if (stream->strh->type == GST_RIFF_FCC_vids) {
1906 GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
1908 s_dur = gst_util_uint64_scale (GST_SECOND, strh->scale, strh->rate);
1909 GST_DEBUG_OBJECT (avi, "verifying stream framerate %d/%d, "
1910 "frame duration = %d ms", strh->rate, strh->scale,
1911 (gint) (s_dur / GST_MSECOND));
1912 if (h_dur > (10 * GST_MSECOND) && (s_dur > 10 * h_dur)) {
1913 strh->rate = GST_SECOND / GST_USECOND;
1914 strh->scale = h_dur / GST_USECOND;
1915 GST_DEBUG_OBJECT (avi, "correcting stream framerate to %d/%d",
1916 strh->rate, strh->scale);
1919 /* determine duration as indicated by header */
1920 stream->hdr_duration = gst_util_uint64_scale ((guint64) strh->length *
1921 strh->scale, GST_SECOND, (guint64) strh->rate);
1922 GST_INFO ("Stream duration according to header: %" GST_TIME_FORMAT,
1923 GST_TIME_ARGS (stream->hdr_duration));
1924 if (stream->hdr_duration == 0)
1925 stream->hdr_duration = GST_CLOCK_TIME_NONE;
1930 case GST_RIFF_TAG_strf:
1932 gboolean res = FALSE;
1935 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
1939 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
1942 switch (stream->strh->type) {
1943 case GST_RIFF_FCC_vids:
1944 stream->is_vbr = TRUE;
1945 res = gst_riff_parse_strf_vids (element, sub,
1946 &stream->strf.vids, &stream->extradata);
1948 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
1950 case GST_RIFF_FCC_auds:
1951 stream->is_vbr = (stream->strh->samplesize == 0)
1952 && stream->strh->scale > 1;
1954 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
1955 &stream->extradata);
1957 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
1958 stream->is_vbr, res);
1960 case GST_RIFF_FCC_iavs:
1961 stream->is_vbr = TRUE;
1962 res = gst_riff_parse_strf_iavs (element, sub,
1963 &stream->strf.iavs, &stream->extradata);
1965 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
1967 case GST_RIFF_FCC_txts:
1968 /* nothing to parse here */
1969 stream->is_vbr = (stream->strh->samplesize == 0)
1970 && (stream->strh->scale > 1);
1974 GST_ERROR_OBJECT (avi,
1975 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
1976 GST_FOURCC_ARGS (stream->strh->type));
1980 gst_buffer_unref (sub);
1988 case GST_RIFF_TAG_vprp:
1991 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
1995 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
1999 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
2003 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
2004 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
2005 /* not considered fatal */
2013 case GST_RIFF_TAG_strd:
2014 if (stream->initdata)
2015 gst_buffer_unref (stream->initdata);
2016 stream->initdata = sub;
2019 case GST_RIFF_TAG_strn:
2020 g_free (stream->name);
2023 g_strndup ((gchar *) GST_BUFFER_DATA (sub),
2024 (gsize) GST_BUFFER_SIZE (sub));
2025 gst_buffer_unref (sub);
2028 stream->name = g_strdup ("");
2030 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
2033 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
2034 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
2035 '0' + avi->num_streams % 10)) {
2036 g_free (stream->indexes);
2037 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
2038 stream->superindex = TRUE;
2042 GST_WARNING_OBJECT (avi,
2043 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
2044 GST_FOURCC_ARGS (tag));
2046 case GST_RIFF_TAG_JUNQ:
2047 case GST_RIFF_TAG_JUNK:
2051 gst_buffer_unref (sub);
2057 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
2062 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
2066 /* get class to figure out the template */
2067 klass = GST_ELEMENT_GET_CLASS (avi);
2069 /* we now have all info, let´s set up a pad and a caps and be done */
2070 /* create stream name + pad */
2071 switch (stream->strh->type) {
2072 case GST_RIFF_FCC_vids:{
2075 fourcc = (stream->strf.vids->compression) ?
2076 stream->strf.vids->compression : stream->strh->fcc_handler;
2077 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
2078 templ = gst_element_class_get_pad_template (klass, "video_%02d");
2079 caps = gst_riff_create_video_caps (fourcc, stream->strh,
2080 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
2082 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2083 GST_TYPE_FOURCC, fourcc, NULL);
2084 } else if (got_vprp && vprp) {
2085 guint32 aspect_n, aspect_d;
2088 aspect_n = vprp->aspect >> 16;
2089 aspect_d = vprp->aspect & 0xffff;
2090 /* calculate the pixel aspect ratio using w/h and aspect ratio */
2091 n = aspect_n * stream->strf.vids->height;
2092 d = aspect_d * stream->strf.vids->width;
2094 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
2096 /* very local, not needed elsewhere */
2100 tag_name = GST_TAG_VIDEO_CODEC;
2101 avi->num_v_streams++;
2104 case GST_RIFF_FCC_auds:{
2105 padname = g_strdup_printf ("audio_%02d", avi->num_a_streams);
2106 templ = gst_element_class_get_pad_template (klass, "audio_%02d");
2107 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
2108 stream->strh, stream->strf.auds, stream->extradata,
2109 stream->initdata, &codec_name);
2111 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
2112 G_TYPE_INT, stream->strf.auds->format, NULL);
2114 tag_name = GST_TAG_AUDIO_CODEC;
2115 avi->num_a_streams++;
2118 case GST_RIFF_FCC_iavs:{
2119 guint32 fourcc = stream->strh->fcc_handler;
2121 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
2122 templ = gst_element_class_get_pad_template (klass, "video_%02d");
2123 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
2124 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
2126 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2127 GST_TYPE_FOURCC, fourcc, NULL);
2129 tag_name = GST_TAG_VIDEO_CODEC;
2130 avi->num_v_streams++;
2133 case GST_RIFF_FCC_txts:{
2134 padname = g_strdup_printf ("subtitle_%02d", avi->num_t_streams);
2135 templ = gst_element_class_get_pad_template (klass, "subtitle_%02d");
2136 caps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
2138 avi->num_t_streams++;
2142 g_assert_not_reached ();
2145 /* no caps means no stream */
2147 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
2151 GST_DEBUG_OBJECT (element, "codec-name=%s",
2152 (codec_name ? codec_name : "NULL"));
2153 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
2155 /* set proper settings and add it */
2157 gst_object_unref (stream->pad);
2158 pad = stream->pad = gst_pad_new_from_template (templ, padname);
2161 gst_pad_use_fixed_caps (pad);
2163 gst_pad_set_formats_function (pad,
2164 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
2165 gst_pad_set_event_mask_function (pad,
2166 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
2168 gst_pad_set_event_function (pad,
2169 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
2170 gst_pad_set_query_type_function (pad,
2171 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_query_types));
2172 gst_pad_set_query_function (pad,
2173 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
2175 gst_pad_set_convert_function (pad,
2176 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
2179 if (avi->element_index)
2180 gst_index_get_writer_id (avi->element_index, GST_OBJECT_CAST (stream->pad),
2183 stream->num = avi->num_streams;
2185 stream->start_entry = 0;
2186 stream->step_entry = 0;
2187 stream->stop_entry = 0;
2189 stream->current_entry = -1;
2190 stream->current_total = 0;
2192 stream->last_flow = GST_FLOW_OK;
2193 stream->discont = TRUE;
2195 stream->total_bytes = 0;
2196 stream->total_blocks = 0;
2197 stream->n_keyframes = 0;
2200 stream->idx_max = 0;
2202 gst_pad_set_element_private (pad, stream);
2205 gst_pad_set_caps (pad, caps);
2206 gst_pad_set_active (pad, TRUE);
2207 gst_caps_unref (caps);
2211 if (!stream->taglist)
2212 stream->taglist = gst_tag_list_new ();
2214 avi->got_tags = TRUE;
2216 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
2218 g_free (codec_name);
2221 gst_buffer_unref (buf);
2228 /* unref any mem that may be in use */
2230 gst_buffer_unref (buf);
2232 gst_buffer_unref (sub);
2234 g_free (codec_name);
2235 gst_avi_demux_reset_stream (avi, stream);
2242 * gst_avi_demux_parse_odml:
2243 * @avi: calling element (used for debug/error).
2244 * @buf: input buffer to be used for parsing.
2246 * Read an openDML-2.0 extension header. Fills in the frame number
2247 * in the avi demuxer object when reading succeeds.
2250 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
2254 GstBuffer *sub = NULL;
2256 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
2259 case GST_RIFF_TAG_dmlh:{
2260 gst_riff_dmlh dmlh, *_dmlh;
2263 /* sub == NULL is possible and means an empty buffer */
2264 size = sub ? GST_BUFFER_SIZE (sub) : 0;
2267 if (size < sizeof (gst_riff_dmlh)) {
2268 GST_ERROR_OBJECT (avi,
2269 "DMLH entry is too small (%d bytes, %d needed)",
2270 size, (int) sizeof (gst_riff_dmlh));
2273 _dmlh = (gst_riff_dmlh *) GST_BUFFER_DATA (sub);
2274 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
2276 GST_INFO_OBJECT (avi, "dmlh tag found: totalframes: %u",
2279 avi->avih->tot_frames = dmlh.totalframes;
2284 GST_WARNING_OBJECT (avi,
2285 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
2286 GST_FOURCC_ARGS (tag));
2288 case GST_RIFF_TAG_JUNQ:
2289 case GST_RIFF_TAG_JUNK:
2291 /* skip and move to next chunk */
2293 gst_buffer_unref (sub);
2300 gst_buffer_unref (buf);
2305 gst_avi_demux_index_last (GstAviDemux * avi, GstAviStream * stream)
2307 return stream->idx_n - 1;
2310 /* find a previous entry in the index with the given flags */
2312 gst_avi_demux_index_prev (GstAviDemux * avi, GstAviStream * stream,
2313 guint last, gboolean keyframe)
2315 GstAviIndexEntry *entry;
2318 for (i = last; i > 0; i--) {
2319 entry = &stream->index[i - 1];
2320 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2328 gst_avi_demux_index_next (GstAviDemux * avi, GstAviStream * stream,
2329 guint last, gboolean keyframe)
2331 GstAviIndexEntry *entry;
2334 for (i = last + 1; i < stream->idx_n; i++) {
2335 entry = &stream->index[i];
2336 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2340 return stream->idx_n - 1;
2344 gst_avi_demux_index_entry_search (GstAviIndexEntry * entry, guint64 * total)
2346 if (entry->total < *total)
2348 else if (entry->total > *total)
2354 * gst_avi_demux_index_for_time:
2356 * @stream: the stream
2357 * @time: a time position
2359 * Finds the index entry which time is less or equal than the requested time.
2360 * Try to avoid binary search when we can convert the time to an index
2361 * position directly (for example for video frames with a fixed duration).
2363 * Returns: the found position in the index.
2366 gst_avi_demux_index_for_time (GstAviDemux * avi,
2367 GstAviStream * stream, guint64 time)
2372 GST_LOG_OBJECT (avi, "search time:%" GST_TIME_FORMAT, GST_TIME_ARGS (time));
2374 /* easy (and common) cases */
2375 if (time == 0 || stream->idx_n == 0)
2377 if (time >= stream->idx_duration)
2378 return stream->idx_n - 1;
2380 /* figure out where we need to go. For that we convert the time to an
2381 * index entry or we convert it to a total and then do a binary search. */
2382 if (stream->is_vbr) {
2383 /* VBR stream next timestamp */
2384 if (stream->strh->type == GST_RIFF_FCC_auds) {
2385 total = avi_stream_convert_time_to_frames_unchecked (stream, time);
2387 index = avi_stream_convert_time_to_frames_unchecked (stream, time);
2390 /* constant rate stream */
2391 total = avi_stream_convert_time_to_bytes_unchecked (stream, time);
2395 GstAviIndexEntry *entry;
2397 /* no index, find index with binary search on total */
2398 GST_LOG_OBJECT (avi, "binary search for entry with total %"
2399 G_GUINT64_FORMAT, total);
2401 entry = gst_util_array_binary_search (stream->index,
2402 stream->idx_n, sizeof (GstAviIndexEntry),
2403 (GCompareDataFunc) gst_avi_demux_index_entry_search,
2404 GST_SEARCH_MODE_BEFORE, &total, NULL);
2406 if (entry == NULL) {
2407 GST_LOG_OBJECT (avi, "not found, assume index 0");
2410 index = entry - stream->index;
2411 GST_LOG_OBJECT (avi, "found at %u", index);
2414 GST_LOG_OBJECT (avi, "converted time to index %u", index);
2420 static inline GstAviStream *
2421 gst_avi_demux_stream_for_id (GstAviDemux * avi, guint32 id)
2424 GstAviStream *stream;
2426 /* get the stream for this entry */
2427 stream_nr = CHUNKID_TO_STREAMNR (id);
2428 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2429 GST_WARNING_OBJECT (avi, "invalid stream nr %d", stream_nr);
2432 stream = &avi->stream[stream_nr];
2433 if (G_UNLIKELY (!stream->strh)) {
2434 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2441 * gst_avi_demux_parse_index:
2442 * @avi: calling element (used for debugging/errors).
2443 * @buf: buffer containing the full index.
2445 * Read index entries from the provided buffer.
2446 * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
2449 gst_avi_demux_parse_index (GstAviDemux * avi, GstBuffer * buf)
2455 gst_riff_index_entry *index;
2457 GstAviStream *stream;
2458 GstAviIndexEntry entry;
2464 data = GST_BUFFER_DATA (buf);
2465 size = GST_BUFFER_SIZE (buf);
2467 stamp = gst_util_get_timestamp ();
2469 /* see how many items in the index */
2470 num = size / sizeof (gst_riff_index_entry);
2474 GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2476 index = (gst_riff_index_entry *) data;
2477 pos_before = avi->offset;
2479 /* figure out if the index is 0 based or relative to the MOVI start */
2480 entry.offset = GST_READ_UINT32_LE (&index[0].offset);
2481 if (entry.offset < avi->offset) {
2482 avi->index_offset = avi->offset + 8;
2483 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2485 avi->index_offset = 0;
2486 GST_DEBUG ("index is 0 based");
2489 for (i = 0, n = 0; i < num; i++) {
2490 id = GST_READ_UINT32_LE (&index[i].id);
2491 entry.offset = GST_READ_UINT32_LE (&index[i].offset);
2493 /* some sanity checks */
2494 if (G_UNLIKELY (id == GST_RIFF_rec || id == 0 ||
2495 (entry.offset == 0 && n > 0)))
2498 /* get the stream for this entry */
2499 stream = gst_avi_demux_stream_for_id (avi, id);
2500 if (G_UNLIKELY (!stream))
2503 /* handle offset and size */
2504 entry.offset += avi->index_offset + 8;
2505 entry.size = GST_READ_UINT32_LE (&index[i].size);
2508 if (stream->strh->type == GST_RIFF_FCC_auds) {
2509 /* all audio frames are keyframes */
2510 ENTRY_SET_KEYFRAME (&entry);
2513 /* else read flags */
2514 flags = GST_READ_UINT32_LE (&index[i].flags);
2515 if (flags & GST_RIFF_IF_KEYFRAME) {
2516 ENTRY_SET_KEYFRAME (&entry);
2518 ENTRY_UNSET_KEYFRAME (&entry);
2523 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2528 gst_buffer_unref (buf);
2530 /* get stream stats now */
2531 avi->have_index = gst_avi_demux_do_index_stats (avi);
2533 stamp = gst_util_get_timestamp () - stamp;
2534 GST_DEBUG_OBJECT (avi, "index parsing took %" GST_TIME_FORMAT,
2535 GST_TIME_ARGS (stamp));
2542 GST_DEBUG_OBJECT (avi, "empty index");
2543 gst_buffer_unref (buf);
2548 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2549 ("Cannot allocate memory for %u*%u=%u bytes",
2550 (guint) sizeof (GstAviIndexEntry), num,
2551 (guint) sizeof (GstAviIndexEntry) * num));
2552 gst_buffer_unref (buf);
2558 * gst_avi_demux_stream_index:
2559 * @avi: avi demuxer object.
2561 * Seeks to index and reads it.
2564 gst_avi_demux_stream_index (GstAviDemux * avi)
2567 guint64 offset = avi->offset;
2572 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2574 /* get chunk information */
2575 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2576 if (res != GST_FLOW_OK)
2578 else if (GST_BUFFER_SIZE (buf) < 8)
2581 /* check tag first before blindy trying to read 'size' bytes */
2582 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2583 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2584 if (tag == GST_RIFF_TAG_LIST) {
2585 /* this is the movi tag */
2586 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2587 (8 + GST_ROUND_UP_2 (size)));
2588 offset += 8 + GST_ROUND_UP_2 (size);
2589 gst_buffer_unref (buf);
2590 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2591 if (res != GST_FLOW_OK)
2593 else if (GST_BUFFER_SIZE (buf) < 8)
2595 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2596 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2599 if (tag != GST_RIFF_TAG_idx1)
2604 gst_buffer_unref (buf);
2606 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2608 /* read chunk, advance offset */
2609 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2610 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2613 GST_DEBUG ("will parse index chunk size %u for tag %"
2614 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2616 gst_avi_demux_parse_index (avi, buf);
2618 #ifndef GST_DISABLE_GST_DEBUG
2619 /* debug our indexes */
2622 GstAviStream *stream;
2624 for (i = 0; i < avi->num_streams; i++) {
2625 stream = &avi->stream[i];
2626 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2627 i, stream->idx_n, stream->total_bytes);
2636 GST_DEBUG_OBJECT (avi,
2637 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2642 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2643 gst_buffer_unref (buf);
2648 GST_WARNING_OBJECT (avi,
2649 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2650 GST_FOURCC_ARGS (tag));
2651 gst_buffer_unref (buf);
2656 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2657 gst_buffer_unref (buf);
2663 * gst_avi_demux_stream_index_push:
2664 * @avi: avi demuxer object.
2669 gst_avi_demux_stream_index_push (GstAviDemux * avi)
2671 guint64 offset = avi->idx1_offset;
2676 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2678 /* get chunk information */
2679 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
2682 /* check tag first before blindly trying to read 'size' bytes */
2683 if (tag == GST_RIFF_TAG_LIST) {
2684 /* this is the movi tag */
2685 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2686 (8 + GST_ROUND_UP_2 (size)));
2687 avi->idx1_offset = offset + 8 + GST_ROUND_UP_2 (size);
2688 /* issue seek to allow chain function to handle it and return! */
2689 avi_demux_do_push_seek (avi, avi->idx1_offset);
2693 if (tag != GST_RIFF_TAG_idx1)
2696 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2698 /* flush chunk header */
2699 gst_adapter_flush (avi->adapter, 8);
2700 /* read chunk payload */
2701 buf = gst_adapter_take_buffer (avi->adapter, size);
2704 /* advance offset */
2705 offset += 8 + GST_ROUND_UP_2 (size);
2707 GST_DEBUG ("will parse index chunk size %u for tag %"
2708 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2710 avi->offset = avi->first_movi_offset - 8;
2711 gst_avi_demux_parse_index (avi, buf);
2713 #ifndef GST_DISABLE_GST_DEBUG
2714 /* debug our indexes */
2717 GstAviStream *stream;
2719 for (i = 0; i < avi->num_streams; i++) {
2720 stream = &avi->stream[i];
2721 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2722 i, stream->idx_n, stream->total_bytes);
2731 GST_DEBUG_OBJECT (avi,
2732 "taking data from adapter failed: pos=%" G_GUINT64_FORMAT " size=%u",
2738 GST_WARNING_OBJECT (avi,
2739 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2740 GST_FOURCC_ARGS (tag));
2746 * gst_avi_demux_peek_tag:
2748 * Returns the tag and size of the next chunk
2750 static GstFlowReturn
2751 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
2754 GstFlowReturn res = GST_FLOW_OK;
2755 GstBuffer *buf = NULL;
2759 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2760 if (res != GST_FLOW_OK)
2763 bufsize = GST_BUFFER_SIZE (buf);
2767 bufdata = GST_BUFFER_DATA (buf);
2769 *tag = GST_READ_UINT32_LE (bufdata);
2770 *size = GST_READ_UINT32_LE (bufdata + 4);
2772 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
2773 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
2774 *size, offset + 8, offset + 8 + (gint64) * size);
2777 gst_buffer_unref (buf);
2784 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
2789 GST_DEBUG_OBJECT (avi, "got %d bytes which is <> 8 bytes", bufsize);
2790 res = GST_FLOW_ERROR;
2796 * gst_avi_demux_next_data_buffer:
2798 * Returns the offset and size of the next buffer
2799 * Position is the position of the buffer (after tag and size)
2801 static GstFlowReturn
2802 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
2803 guint32 * tag, guint * size)
2805 guint64 off = *offset;
2810 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
2811 if (res != GST_FLOW_OK)
2813 if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
2814 off += 8 + 4; /* skip tag + size + subtag */
2826 * gst_avi_demux_stream_scan:
2827 * @avi: calling element (used for debugging/errors).
2829 * Scan the file for all chunks to "create" a new index.
2833 gst_avi_demux_stream_scan (GstAviDemux * avi)
2836 GstAviStream *stream;
2845 * - implement non-seekable source support.
2847 GST_DEBUG_OBJECT (avi, "Creating index");
2849 /* get the size of the file */
2850 format = GST_FORMAT_BYTES;
2851 if (!gst_pad_query_peer_duration (avi->sinkpad, &format, &tmplength))
2855 /* guess the total amount of entries we expect */
2859 GstAviIndexEntry entry;
2862 /* start reading data buffers to find the id and offset */
2863 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
2864 if (G_UNLIKELY (res != GST_FLOW_OK))
2868 stream = gst_avi_demux_stream_for_id (avi, tag);
2869 if (G_UNLIKELY (!stream))
2872 /* we can't figure out the keyframes, assume they all are */
2873 entry.flags = GST_AVI_KEYFRAME;
2877 /* and add to the index of this stream */
2878 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2882 /* update position */
2883 pos += GST_ROUND_UP_2 (size);
2884 if (G_UNLIKELY (pos > length)) {
2885 GST_WARNING_OBJECT (avi,
2886 "Stopping index lookup since we are further than EOF");
2892 avi->have_index = gst_avi_demux_do_index_stats (avi);
2900 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2901 ("Cannot allocate memory for %u*%u=%u bytes",
2902 (guint) sizeof (GstAviIndexEntry), num,
2903 (guint) sizeof (GstAviIndexEntry) * num));
2909 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
2913 GstAviStream *stream;
2915 total = GST_CLOCK_TIME_NONE;
2917 /* all streams start at a timestamp 0 */
2918 for (i = 0; i < avi->num_streams; i++) {
2919 GstClockTime duration, hduration;
2920 gst_riff_strh *strh;
2922 stream = &avi->stream[i];
2923 if (G_UNLIKELY (!stream || !stream->idx_n || !(strh = stream->strh)))
2926 /* get header duration for the stream */
2927 hduration = stream->hdr_duration;
2928 /* index duration calculated during parsing */
2929 duration = stream->idx_duration;
2931 /* now pick a good duration */
2932 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2933 /* index gave valid duration, use that */
2934 GST_INFO ("Stream %p duration according to index: %" GST_TIME_FORMAT,
2935 stream, GST_TIME_ARGS (duration));
2937 /* fall back to header info to calculate a duration */
2938 duration = hduration;
2940 GST_INFO ("Setting duration of stream #%d to %" GST_TIME_FORMAT,
2941 i, GST_TIME_ARGS (duration));
2942 /* set duration for the stream */
2943 stream->duration = duration;
2945 /* find total duration */
2946 if (total == GST_CLOCK_TIME_NONE ||
2947 (GST_CLOCK_TIME_IS_VALID (duration) && duration > total))
2951 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
2952 /* now update the duration for those streams where we had none */
2953 for (i = 0; i < avi->num_streams; i++) {
2954 stream = &avi->stream[i];
2956 if (!GST_CLOCK_TIME_IS_VALID (stream->duration)
2957 || stream->duration == 0) {
2958 stream->duration = total;
2960 GST_INFO ("Stream %p duration according to total: %" GST_TIME_FORMAT,
2961 stream, GST_TIME_ARGS (total));
2966 /* and set the total duration in the segment. */
2967 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
2968 GST_TIME_ARGS (total));
2970 gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
2973 /* returns FALSE if there are no pads to deliver event to,
2974 * otherwise TRUE (whatever the outcome of event sending),
2975 * takes ownership of the event. */
2977 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
2979 gboolean result = FALSE;
2982 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
2983 GST_EVENT_TYPE_NAME (event), avi->num_streams);
2985 for (i = 0; i < avi->num_streams; i++) {
2986 GstAviStream *stream = &avi->stream[i];
2990 gst_pad_push_event (stream->pad, gst_event_ref (event));
2993 gst_event_unref (event);
2998 gst_avi_demux_check_seekability (GstAviDemux * avi)
3001 gboolean seekable = FALSE;
3002 gint64 start = -1, stop = -1;
3004 query = gst_query_new_seeking (GST_FORMAT_BYTES);
3005 if (!gst_pad_peer_query (avi->sinkpad, query)) {
3006 GST_DEBUG_OBJECT (avi, "seeking query failed");
3010 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
3012 /* try harder to query upstream size if we didn't get it the first time */
3013 if (seekable && stop == -1) {
3014 GstFormat fmt = GST_FORMAT_BYTES;
3016 GST_DEBUG_OBJECT (avi, "doing duration query to fix up unset stop");
3017 gst_pad_query_peer_duration (avi->sinkpad, &fmt, &stop);
3020 /* if upstream doesn't know the size, it's likely that it's not seekable in
3021 * practice even if it technically may be seekable */
3022 if (seekable && (start != 0 || stop <= start)) {
3023 GST_DEBUG_OBJECT (avi, "seekable but unknown start/stop -> disable");
3027 if (!avi->element_index) {
3028 GST_DEBUG_OBJECT (avi, "no index");
3033 GST_INFO_OBJECT (avi, "seekable: %d (%" G_GUINT64_FORMAT " - %"
3034 G_GUINT64_FORMAT ")", seekable, start, stop);
3035 avi->seekable = seekable;
3037 gst_query_unref (query);
3041 * Read AVI headers when streaming
3043 static GstFlowReturn
3044 gst_avi_demux_stream_header_push (GstAviDemux * avi)
3046 GstFlowReturn ret = GST_FLOW_OK;
3051 GstBuffer *buf = NULL, *sub = NULL;
3055 GstTagList *tags = NULL;
3057 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
3059 switch (avi->header_state) {
3060 case GST_AVI_DEMUX_HEADER_TAG_LIST:
3061 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3062 avi->offset += 8 + GST_ROUND_UP_2 (size);
3063 if (tag != GST_RIFF_TAG_LIST)
3064 goto header_no_list;
3066 gst_adapter_flush (avi->adapter, 8);
3067 /* Find the 'hdrl' LIST tag */
3068 GST_DEBUG ("Reading %d bytes", size);
3069 buf = gst_adapter_take_buffer (avi->adapter, size);
3071 if (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl)
3072 goto header_no_hdrl;
3076 gst_adapter_flush (avi->adapter, 1);
3078 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
3080 /* the hdrl starts with a 'avih' header */
3081 if (!gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3083 goto header_no_avih;
3085 if (tag != GST_RIFF_TAG_avih)
3086 goto header_no_avih;
3088 if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3089 goto header_wrong_avih;
3091 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
3093 /* now, read the elements from the header until the end */
3094 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3096 /* sub can be NULL on empty tags */
3101 case GST_RIFF_TAG_LIST:
3102 if (GST_BUFFER_SIZE (sub) < 4)
3105 switch (GST_READ_UINT32_LE (GST_BUFFER_DATA (sub))) {
3106 case GST_RIFF_LIST_strl:
3107 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3109 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3110 ("failed to parse stream, ignoring"));
3115 case GST_RIFF_LIST_odml:
3116 gst_avi_demux_parse_odml (avi, sub);
3120 GST_WARNING_OBJECT (avi,
3121 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3122 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA
3125 case GST_RIFF_TAG_JUNQ:
3126 case GST_RIFF_TAG_JUNK:
3131 gst_avi_demux_parse_idit (avi, sub);
3134 GST_WARNING_OBJECT (avi,
3135 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
3136 offset, GST_FOURCC_ARGS (tag));
3138 case GST_RIFF_TAG_JUNQ:
3139 case GST_RIFF_TAG_JUNK:
3141 /* move to next chunk */
3143 gst_buffer_unref (sub);
3148 gst_buffer_unref (buf);
3149 GST_DEBUG ("elements parsed");
3151 /* check parsed streams */
3152 if (avi->num_streams == 0) {
3154 } else if (avi->num_streams != avi->avih->streams) {
3155 GST_WARNING_OBJECT (avi,
3156 "Stream header mentioned %d streams, but %d available",
3157 avi->avih->streams, avi->num_streams);
3159 GST_DEBUG ("Get junk and info next");
3160 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
3162 /* Need more data */
3166 case GST_AVI_DEMUX_HEADER_INFO:
3167 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
3169 if (gst_adapter_available (avi->adapter) < 12)
3172 data = gst_adapter_peek (avi->adapter, 12);
3173 tag = GST_READ_UINT32_LE (data);
3174 size = GST_READ_UINT32_LE (data + 4);
3175 ltag = GST_READ_UINT32_LE (data + 8);
3177 if (tag == GST_RIFF_TAG_LIST) {
3179 case GST_RIFF_LIST_movi:
3180 gst_adapter_flush (avi->adapter, 12);
3181 if (!avi->first_movi_offset)
3182 avi->first_movi_offset = avi->offset;
3184 avi->idx1_offset = avi->offset + size - 4;
3186 case GST_RIFF_LIST_INFO:
3187 GST_DEBUG ("Found INFO chunk");
3188 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3189 GST_DEBUG ("got size %d", size);
3191 gst_adapter_flush (avi->adapter, 12);
3193 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
3196 gst_adapter_flush (avi->adapter, 1);
3197 gst_riff_parse_info (GST_ELEMENT_CAST (avi), buf, &tags);
3199 if (avi->globaltags) {
3200 gst_tag_list_insert (avi->globaltags, tags,
3201 GST_TAG_MERGE_REPLACE);
3203 avi->globaltags = tags;
3207 gst_buffer_unref (buf);
3209 avi->offset += GST_ROUND_UP_2 (size) - 4;
3211 GST_DEBUG ("skipping INFO LIST prefix");
3214 /* Need more data */
3219 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3220 avi->offset += 8 + GST_ROUND_UP_2 (size);
3221 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3222 // ??? goto iterate; ???
3224 /* Need more data */
3230 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3231 avi->offset += 8 + GST_ROUND_UP_2 (size);
3232 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3235 /* Need more data */
3242 GST_WARNING ("unhandled header state: %d", avi->header_state);
3247 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3248 avi->num_streams, avi->stream[0].indexes);
3250 GST_DEBUG ("Found movi chunk. Starting to stream data");
3251 avi->state = GST_AVI_DEMUX_MOVI;
3253 /* no indexes in push mode, but it still sets some variables */
3254 gst_avi_demux_calculate_durations_from_index (avi);
3256 gst_avi_demux_expose_streams (avi, TRUE);
3258 /* prepare all streams for index 0 */
3259 for (i = 0; i < avi->num_streams; i++)
3260 avi->stream[i].current_entry = 0;
3262 /* create initial NEWSEGMENT event */
3263 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3264 stop = avi->segment.duration;
3266 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3269 gst_event_unref (avi->seg_event);
3270 avi->seg_event = gst_event_new_new_segment_full
3271 (FALSE, avi->segment.rate, avi->segment.applied_rate, GST_FORMAT_TIME,
3272 avi->segment.start, stop, avi->segment.time);
3274 gst_avi_demux_check_seekability (avi);
3276 /* at this point we know all the streams and we can signal the no more
3278 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3279 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3286 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3287 return GST_FLOW_ERROR;
3291 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3292 ("Invalid AVI header (no LIST at start): %"
3293 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3294 return GST_FLOW_ERROR;
3298 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3299 ("Invalid AVI header (no hdrl at start): %"
3300 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3301 gst_buffer_unref (buf);
3302 return GST_FLOW_ERROR;
3306 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3307 ("Invalid AVI header (no avih at start): %"
3308 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3310 gst_buffer_unref (sub);
3312 gst_buffer_unref (buf);
3313 return GST_FLOW_ERROR;
3317 gst_buffer_unref (buf);
3318 return GST_FLOW_ERROR;
3323 gst_avi_demux_add_date_tag (GstAviDemux * avi, gint y, gint m, gint d)
3326 date = g_date_new_dmy (d, m, y);
3327 if (!g_date_valid (date)) {
3329 GST_WARNING_OBJECT (avi, "Refusing to add invalid date %d-%d-%d", y, m, d);
3334 if (avi->globaltags == NULL)
3335 avi->globaltags = gst_tag_list_new ();
3337 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE, date,
3343 gst_avi_demux_parse_idit_nums_only (GstAviDemux * avi, gchar * data)
3348 ret = sscanf (data, "%d:%d:%d", &y, &m, &d);
3350 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3353 gst_avi_demux_add_date_tag (avi, y, m, d);
3357 get_month_num (gchar * data, guint size)
3359 if (g_ascii_strncasecmp (data, "jan", 3) == 0) {
3361 } else if (g_ascii_strncasecmp (data, "feb", 3) == 0) {
3363 } else if (g_ascii_strncasecmp (data, "mar", 3) == 0) {
3365 } else if (g_ascii_strncasecmp (data, "apr", 3) == 0) {
3367 } else if (g_ascii_strncasecmp (data, "may", 3) == 0) {
3369 } else if (g_ascii_strncasecmp (data, "jun", 3) == 0) {
3371 } else if (g_ascii_strncasecmp (data, "jul", 3) == 0) {
3373 } else if (g_ascii_strncasecmp (data, "aug", 3) == 0) {
3375 } else if (g_ascii_strncasecmp (data, "sep", 3) == 0) {
3377 } else if (g_ascii_strncasecmp (data, "oct", 3) == 0) {
3379 } else if (g_ascii_strncasecmp (data, "nov", 3) == 0) {
3381 } else if (g_ascii_strncasecmp (data, "dec", 3) == 0) {
3389 gst_avi_demux_parse_idit_text (GstAviDemux * avi, gchar * data)
3391 gint year, month, day;
3392 gint hour, min, sec;
3397 ret = sscanf (data, "%3s %3s %d %d:%d:%d %d", weekday, monthstr, &day, &hour,
3400 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3403 month = get_month_num (monthstr, strlen (monthstr));
3404 gst_avi_demux_add_date_tag (avi, year, month, day);
3408 gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf)
3410 gchar *data = (gchar *) GST_BUFFER_DATA (buf);
3411 guint size = GST_BUFFER_SIZE (buf);
3412 gchar *safedata = NULL;
3416 * http://www.eden-foundation.org/products/code/film_date_stamp/index.html
3418 * This tag could be in one of the below formats
3419 * 2005:08:17 11:42:43
3420 * THU OCT 26 16:46:04 2006
3421 * Mon Mar 3 09:44:56 2008
3423 * FIXME: Our date tag doesn't include hours
3426 /* skip eventual initial whitespace */
3427 while (size > 0 && g_ascii_isspace (data[0])) {
3436 /* make a safe copy to add a \0 to the end of the string */
3437 safedata = g_strndup (data, size);
3439 /* test if the first char is a alpha or a number */
3440 if (g_ascii_isdigit (data[0])) {
3441 gst_avi_demux_parse_idit_nums_only (avi, safedata);
3444 } else if (g_ascii_isalpha (data[0])) {
3445 gst_avi_demux_parse_idit_text (avi, safedata);
3453 GST_WARNING_OBJECT (avi, "IDIT tag has no parsable info");
3457 * Read full AVI headers.
3459 static GstFlowReturn
3460 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
3463 GstBuffer *buf, *sub = NULL;
3467 GstElement *element = GST_ELEMENT_CAST (avi);
3469 GstTagList *tags = NULL;
3471 stamp = gst_util_get_timestamp ();
3473 /* the header consists of a 'hdrl' LIST tag */
3474 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3475 if (res != GST_FLOW_OK)
3476 goto pull_range_failed;
3477 else if (tag != GST_RIFF_TAG_LIST)
3479 else if (GST_BUFFER_SIZE (buf) < 4)
3482 GST_DEBUG_OBJECT (avi, "parsing headers");
3484 /* Find the 'hdrl' LIST tag */
3485 while (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl) {
3486 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
3487 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf))));
3490 gst_buffer_unref (buf);
3492 /* read new chunk */
3493 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
3494 if (res != GST_FLOW_OK)
3495 goto pull_range_failed;
3496 else if (tag != GST_RIFF_TAG_LIST)
3498 else if (GST_BUFFER_SIZE (buf) < 4)
3502 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
3504 /* the hdrl starts with a 'avih' header */
3505 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
3507 else if (tag != GST_RIFF_TAG_avih)
3509 else if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3512 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3514 /* now, read the elements from the header until the end */
3515 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
3516 /* sub can be NULL on empty tags */
3521 case GST_RIFF_TAG_LIST:
3526 if (GST_BUFFER_SIZE (sub) < 4)
3529 data = GST_BUFFER_DATA (sub);
3530 fourcc = GST_READ_UINT32_LE (data);
3533 case GST_RIFF_LIST_strl:
3534 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3535 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3536 ("failed to parse stream, ignoring"));
3541 case GST_RIFF_LIST_odml:
3542 gst_avi_demux_parse_odml (avi, sub);
3545 case GST_RIFF_LIST_INFO:
3546 GST_BUFFER_DATA (sub) = data + 4;
3547 GST_BUFFER_SIZE (sub) -= 4;
3548 gst_riff_parse_info (element, sub, &tags);
3550 if (avi->globaltags) {
3551 gst_tag_list_insert (avi->globaltags, tags,
3552 GST_TAG_MERGE_REPLACE);
3554 avi->globaltags = tags;
3560 GST_WARNING_OBJECT (avi,
3561 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3562 GST_FOURCC_ARGS (fourcc));
3563 GST_MEMDUMP_OBJECT (avi, "Unknown list", GST_BUFFER_DATA (sub),
3564 GST_BUFFER_SIZE (sub));
3566 case GST_RIFF_TAG_JUNQ:
3567 case GST_RIFF_TAG_JUNK:
3573 gst_avi_demux_parse_idit (avi, sub);
3576 GST_WARNING_OBJECT (avi,
3577 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
3578 GST_FOURCC_ARGS (tag), offset);
3579 GST_MEMDUMP_OBJECT (avi, "Unknown tag", GST_BUFFER_DATA (sub),
3580 GST_BUFFER_SIZE (sub));
3582 case GST_RIFF_TAG_JUNQ:
3583 case GST_RIFF_TAG_JUNK:
3586 gst_buffer_unref (sub);
3591 gst_buffer_unref (buf);
3592 GST_DEBUG ("elements parsed");
3594 /* check parsed streams */
3595 if (avi->num_streams == 0)
3597 else if (avi->num_streams != avi->avih->streams) {
3598 GST_WARNING_OBJECT (avi,
3599 "Stream header mentioned %d streams, but %d available",
3600 avi->avih->streams, avi->num_streams);
3603 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
3604 G_GUINT64_FORMAT, avi->offset);
3606 /* Now, find the data (i.e. skip all junk between header and data) */
3612 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
3613 if (res != GST_FLOW_OK) {
3614 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
3615 goto pull_range_failed;
3616 } else if (GST_BUFFER_SIZE (buf) < 12) {
3617 GST_DEBUG_OBJECT (avi, "got %d bytes which is less than 12 bytes",
3618 GST_BUFFER_SIZE (buf));
3619 gst_buffer_unref (buf);
3620 return GST_FLOW_ERROR;
3623 data = GST_BUFFER_DATA (buf);
3625 tag = GST_READ_UINT32_LE (data);
3626 size = GST_READ_UINT32_LE (data + 4);
3627 ltag = GST_READ_UINT32_LE (data + 8);
3629 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
3630 GST_FOURCC_ARGS (tag), size);
3631 GST_MEMDUMP ("Tag content", data, GST_BUFFER_SIZE (buf));
3632 gst_buffer_unref (buf);
3635 case GST_RIFF_TAG_LIST:{
3637 case GST_RIFF_LIST_movi:
3638 GST_DEBUG_OBJECT (avi,
3639 "Reached the 'movi' tag, we're done with skipping");
3641 case GST_RIFF_LIST_INFO:
3643 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
3645 if (res != GST_FLOW_OK) {
3646 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3647 goto pull_range_failed;
3649 GST_DEBUG ("got size %u", GST_BUFFER_SIZE (buf));
3651 GST_DEBUG ("skipping INFO LIST prefix");
3652 avi->offset += (4 - GST_ROUND_UP_2 (size));
3653 gst_buffer_unref (buf);
3657 sub = gst_buffer_create_sub (buf, 4, GST_BUFFER_SIZE (buf) - 4);
3658 gst_riff_parse_info (element, sub, &tags);
3660 if (avi->globaltags) {
3661 gst_tag_list_insert (avi->globaltags, tags,
3662 GST_TAG_MERGE_REPLACE);
3664 avi->globaltags = tags;
3669 gst_buffer_unref (sub);
3672 gst_buffer_unref (buf);
3673 /* gst_riff_read_chunk() has already advanced avi->offset */
3676 GST_WARNING_OBJECT (avi,
3677 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
3678 GST_FOURCC_ARGS (ltag));
3679 avi->offset += 8 + GST_ROUND_UP_2 (size);
3685 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
3686 GST_FOURCC_ARGS (tag));
3688 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
3689 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
3690 /* Only get buffer for debugging if the memdump is needed */
3691 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= 9) {
3692 res = gst_pad_pull_range (avi->sinkpad, avi->offset, size, &buf);
3693 if (res != GST_FLOW_OK) {
3694 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
3695 goto pull_range_failed;
3697 GST_MEMDUMP ("Junk", GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
3698 gst_buffer_unref (buf);
3700 avi->offset += 8 + GST_ROUND_UP_2 (size);
3706 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3707 avi->num_streams, avi->stream[0].indexes);
3709 /* create or read stream index (for seeking) */
3710 if (avi->stream[0].indexes != NULL) {
3711 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
3712 gst_avi_demux_read_subindexes_pull (avi);
3714 if (!avi->have_index) {
3715 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
3716 gst_avi_demux_stream_index (avi);
3718 /* still no index, scan */
3719 if (!avi->have_index) {
3720 gst_avi_demux_stream_scan (avi);
3722 /* still no index.. this is a fatal error for now.
3723 * FIXME, we should switch to plain push mode without seeking
3724 * instead of failing. */
3725 if (!avi->have_index)
3729 /* use the indexes now to construct nice durations */
3730 gst_avi_demux_calculate_durations_from_index (avi);
3732 gst_avi_demux_expose_streams (avi, FALSE);
3734 /* create initial NEWSEGMENT event */
3735 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3736 stop = avi->segment.duration;
3738 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3740 /* do initial seek to the default segment values */
3741 gst_avi_demux_do_seek (avi, &avi->segment);
3743 /* prepare initial segment */
3745 gst_event_unref (avi->seg_event);
3746 avi->seg_event = gst_event_new_new_segment_full
3747 (FALSE, avi->segment.rate, avi->segment.applied_rate, GST_FORMAT_TIME,
3748 avi->segment.start, stop, avi->segment.time);
3750 stamp = gst_util_get_timestamp () - stamp;
3751 GST_DEBUG_OBJECT (avi, "pulling header took %" GST_TIME_FORMAT,
3752 GST_TIME_ARGS (stamp));
3754 /* at this point we know all the streams and we can signal the no more
3756 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3757 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3764 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3765 ("Invalid AVI header (no LIST at start): %"
3766 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3767 gst_buffer_unref (buf);
3768 return GST_FLOW_ERROR;
3772 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3773 ("Invalid AVI header (no hdrl at start): %"
3774 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3775 gst_buffer_unref (buf);
3776 return GST_FLOW_ERROR;
3780 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3781 ("Invalid AVI header (no avih at start): %"
3782 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3784 gst_buffer_unref (sub);
3785 gst_buffer_unref (buf);
3786 return GST_FLOW_ERROR;
3790 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3791 ("Invalid AVI header (cannot parse avih at start)"));
3792 gst_buffer_unref (buf);
3793 return GST_FLOW_ERROR;
3797 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3798 return GST_FLOW_ERROR;
3802 GST_WARNING ("file without or too big index");
3803 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3804 ("Could not get/create index"));
3805 return GST_FLOW_ERROR;
3809 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3810 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
3811 return GST_FLOW_ERROR;
3815 /* move a stream to @index */
3817 gst_avi_demux_move_stream (GstAviDemux * avi, GstAviStream * stream,
3818 GstSegment * segment, guint index)
3820 GST_DEBUG_OBJECT (avi, "Move stream %d to %u", stream->num, index);
3822 if (segment->rate < 0.0) {
3824 /* Because we don't know the frame order we need to push from the prev keyframe
3825 * to the next keyframe. If there is a smart decoder downstream he will notice
3826 * that there are too many encoded frames send and return UNEXPECTED when there
3827 * are enough decoded frames to fill the segment. */
3828 next_key = gst_avi_demux_index_next (avi, stream, index, TRUE);
3830 /* FIXME, we go back to 0, we should look at segment.start. We will however
3831 * stop earlier when the see the timestamp < segment.start */
3832 stream->start_entry = 0;
3833 stream->step_entry = index;
3834 stream->current_entry = index;
3835 stream->stop_entry = next_key;
3837 GST_DEBUG_OBJECT (avi, "reverse seek: start %u, step %u, stop %u",
3838 stream->start_entry, stream->step_entry, stream->stop_entry);
3840 stream->start_entry = index;
3841 stream->step_entry = index;
3842 stream->stop_entry = gst_avi_demux_index_last (avi, stream);
3844 if (stream->current_entry != index) {
3845 GST_DEBUG_OBJECT (avi, "Move DISCONT from %u to %u",
3846 stream->current_entry, index);
3847 stream->current_entry = index;
3848 stream->discont = TRUE;
3851 /* update the buffer info */
3852 gst_avi_demux_get_buffer_info (avi, stream, index,
3853 &stream->current_timestamp, &stream->current_ts_end,
3854 &stream->current_offset, &stream->current_offset_end);
3856 GST_DEBUG_OBJECT (avi, "Moved to %u, ts %" GST_TIME_FORMAT
3857 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
3858 ", off_end %" G_GUINT64_FORMAT, index,
3859 GST_TIME_ARGS (stream->current_timestamp),
3860 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
3861 stream->current_offset_end);
3863 GST_DEBUG_OBJECT (avi, "Seeking to offset %" G_GUINT64_FORMAT,
3864 stream->index[index].offset);
3868 * Do the actual seeking.
3871 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
3873 GstClockTime seek_time;
3876 GstAviStream *stream;
3878 seek_time = segment->last_stop;
3879 keyframe = !!(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
3881 GST_DEBUG_OBJECT (avi, "seek to: %" GST_TIME_FORMAT
3882 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
3884 /* FIXME, this code assumes the main stream with keyframes is stream 0,
3885 * which is mostly correct... */
3886 stream = &avi->stream[avi->main_stream];
3888 /* get the entry index for the requested position */
3889 index = gst_avi_demux_index_for_time (avi, stream, seek_time);
3890 GST_DEBUG_OBJECT (avi, "Got entry %u", index);
3892 /* check if we are already on a keyframe */
3893 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
3894 GST_DEBUG_OBJECT (avi, "not keyframe, searching back");
3895 /* now go to the previous keyframe, this is where we should start
3897 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
3898 GST_DEBUG_OBJECT (avi, "previous keyframe at %u", index);
3901 /* move the main stream to this position */
3902 gst_avi_demux_move_stream (avi, stream, segment, index);
3905 /* when seeking to a keyframe, we update the result seek time
3906 * to the time of the keyframe. */
3907 seek_time = stream->current_timestamp;
3908 GST_DEBUG_OBJECT (avi, "keyframe adjusted to %" GST_TIME_FORMAT,
3909 GST_TIME_ARGS (seek_time));
3912 /* the seek time is also the last_stop and stream time when going
3914 segment->last_stop = seek_time;
3915 if (segment->rate > 0.0)
3916 segment->time = seek_time;
3918 /* now set DISCONT and align the other streams */
3919 for (i = 0; i < avi->num_streams; i++) {
3920 GstAviStream *ostream;
3922 ostream = &avi->stream[i];
3923 if ((ostream == stream) || (ostream->index == NULL))
3926 /* get the entry index for the requested position */
3927 index = gst_avi_demux_index_for_time (avi, ostream, seek_time);
3929 /* move to previous keyframe */
3930 if (!ENTRY_IS_KEYFRAME (&ostream->index[index]))
3931 index = gst_avi_demux_index_prev (avi, ostream, index, TRUE);
3933 gst_avi_demux_move_stream (avi, ostream, segment, index);
3935 GST_DEBUG_OBJECT (avi, "done seek to: %" GST_TIME_FORMAT,
3936 GST_TIME_ARGS (seek_time));
3942 * Handle seek event.
3945 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
3950 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
3954 GstSegment seeksegment = { 0, };
3958 GST_DEBUG_OBJECT (avi, "doing seek with event");
3960 gst_event_parse_seek (event, &rate, &format, &flags,
3961 &cur_type, &cur, &stop_type, &stop);
3963 /* we have to have a format as the segment format. Try to convert
3965 if (format != GST_FORMAT_TIME) {
3966 GstFormat fmt = GST_FORMAT_TIME;
3967 gboolean res = TRUE;
3969 if (cur_type != GST_SEEK_TYPE_NONE)
3970 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
3971 if (res && stop_type != GST_SEEK_TYPE_NONE)
3972 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
3978 GST_DEBUG_OBJECT (avi,
3979 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
3980 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
3981 /* FIXME: can we do anything with rate!=1.0 */
3983 GST_DEBUG_OBJECT (avi, "doing seek without event");
3988 /* save flush flag */
3989 flush = flags & GST_SEEK_FLAG_FLUSH;
3992 GstEvent *fevent = gst_event_new_flush_start ();
3994 /* for a flushing seek, we send a flush_start on all pads. This will
3995 * eventually stop streaming with a WRONG_STATE. We can thus eventually
3996 * take the STREAM_LOCK. */
3997 GST_DEBUG_OBJECT (avi, "sending flush start");
3998 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
3999 gst_pad_push_event (avi->sinkpad, fevent);
4001 /* a non-flushing seek, we PAUSE the task so that we can take the
4003 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
4004 gst_pad_pause_task (avi->sinkpad);
4007 /* wait for streaming to stop */
4008 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
4009 GST_PAD_STREAM_LOCK (avi->sinkpad);
4011 /* copy segment, we need this because we still need the old
4012 * segment when we close the current segment. */
4013 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4016 GST_DEBUG_OBJECT (avi, "configuring seek");
4017 gst_segment_set_seek (&seeksegment, rate, format, flags,
4018 cur_type, cur, stop_type, stop, &update);
4020 /* do the seek, seeksegment.last_stop contains the new position, this
4021 * actually never fails. */
4022 gst_avi_demux_do_seek (avi, &seeksegment);
4025 GstEvent *fevent = gst_event_new_flush_stop ();
4027 GST_DEBUG_OBJECT (avi, "sending flush stop");
4028 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4029 gst_pad_push_event (avi->sinkpad, fevent);
4031 /* reset the last flow and mark discont, FLUSH is always DISCONT */
4032 for (i = 0; i < avi->num_streams; i++) {
4033 GST_DEBUG_OBJECT (avi, "marking DISCONT");
4034 avi->stream[i].last_flow = GST_FLOW_OK;
4035 avi->stream[i].discont = TRUE;
4037 } else if (avi->segment_running) {
4040 /* we are running the current segment and doing a non-flushing seek,
4041 * close the segment first based on the last_stop. */
4042 GST_DEBUG_OBJECT (avi, "closing running segment %" G_GINT64_FORMAT
4043 " to %" G_GINT64_FORMAT, avi->segment.start, avi->segment.last_stop);
4044 seg = gst_event_new_new_segment_full (TRUE,
4045 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
4046 avi->segment.start, avi->segment.last_stop, avi->segment.time);
4047 gst_avi_demux_push_event (avi, seg);
4050 /* now update the real segment info */
4051 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
4053 /* post the SEGMENT_START message when we do segmented playback */
4054 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4055 gst_element_post_message (GST_ELEMENT_CAST (avi),
4056 gst_message_new_segment_start (GST_OBJECT_CAST (avi),
4057 avi->segment.format, avi->segment.last_stop));
4060 /* prepare for streaming again */
4061 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
4062 stop = avi->segment.duration;
4064 /* queue the segment event for the streaming thread. */
4066 gst_event_unref (avi->seg_event);
4067 if (avi->segment.rate > 0.0) {
4068 /* forwards goes from last_stop to stop */
4069 avi->seg_event = gst_event_new_new_segment_full (FALSE,
4070 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
4071 avi->segment.last_stop, stop, avi->segment.time);
4073 /* reverse goes from start to last_stop */
4074 avi->seg_event = gst_event_new_new_segment_full (FALSE,
4075 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
4076 avi->segment.start, avi->segment.last_stop, avi->segment.time);
4079 if (!avi->streaming) {
4080 avi->segment_running = TRUE;
4081 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4084 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
4091 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4097 * Handle seek event.
4100 avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4105 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4108 GstAviStream *stream;
4113 /* check we have the index */
4114 if (!avi->have_index) {
4115 GST_DEBUG_OBJECT (avi, "no seek index built, seek aborted.");
4118 GST_DEBUG_OBJECT (avi, "doing push-based seek with event");
4121 gst_event_parse_seek (event, &rate, &format, &flags,
4122 &cur_type, &cur, &stop_type, &stop);
4124 if (format != GST_FORMAT_TIME) {
4125 GstFormat fmt = GST_FORMAT_TIME;
4126 gboolean res = TRUE;
4128 if (cur_type != GST_SEEK_TYPE_NONE)
4129 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
4130 if (res && stop_type != GST_SEEK_TYPE_NONE)
4131 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
4133 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4139 GST_DEBUG_OBJECT (avi,
4140 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
4141 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
4142 /* FIXME: can we do anything with rate!=1.0 */
4144 keyframe = !!(flags & GST_SEEK_FLAG_KEY_UNIT);
4146 GST_DEBUG_OBJECT (avi, "seek to: %" GST_TIME_FORMAT
4147 " keyframe seeking:%d", GST_TIME_ARGS (cur), keyframe);
4149 /* FIXME, this code assumes the main stream with keyframes is stream 0,
4150 * which is mostly correct... */
4151 str_num = avi->main_stream;
4152 stream = &avi->stream[avi->main_stream];
4154 /* get the entry index for the requested position */
4155 index = gst_avi_demux_index_for_time (avi, stream, cur);
4156 GST_DEBUG_OBJECT (avi, "Got entry %u", index);
4158 /* check if we are already on a keyframe */
4159 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
4160 GST_DEBUG_OBJECT (avi, "not keyframe, searching back");
4161 /* now go to the previous keyframe, this is where we should start
4163 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4164 GST_DEBUG_OBJECT (avi, "previous keyframe at %u", index);
4167 gst_avi_demux_get_buffer_info (avi, stream, index,
4168 &stream->current_timestamp, &stream->current_ts_end,
4169 &stream->current_offset, &stream->current_offset_end);
4171 min_offset = stream->current_offset;
4172 for (n = 0; n < avi->num_streams; n++) {
4173 GstAviStream *str = &avi->stream[n];
4177 if (n == avi->main_stream)
4180 /* get the entry index for the requested position */
4181 idx = gst_avi_demux_index_for_time (avi, str, stream->current_timestamp);
4182 GST_DEBUG_OBJECT (avi, "Got entry %u", idx);
4184 /* check if we are already on a keyframe */
4185 if (!ENTRY_IS_KEYFRAME (&str->index[idx])) {
4186 GST_DEBUG_OBJECT (avi, "not keyframe, searching back");
4187 /* now go to the previous keyframe, this is where we should start
4189 idx = gst_avi_demux_index_prev (avi, str, idx, TRUE);
4190 GST_DEBUG_OBJECT (avi, "previous keyframe at %u", idx);
4193 gst_avi_demux_get_buffer_info (avi, str, idx, NULL, NULL, &off, NULL);
4194 if (off < min_offset) {
4195 GST_DEBUG_OBJECT (avi,
4196 "Found an earlier offset at %" G_GUINT64_FORMAT ", str %u", off, n);
4204 gst_avi_demux_get_buffer_info (avi, stream, index,
4205 &stream->current_timestamp, &stream->current_ts_end,
4206 &stream->current_offset, &stream->current_offset_end);
4208 GST_DEBUG_OBJECT (avi, "Moved to str %u, idx %u, ts %" GST_TIME_FORMAT
4209 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4210 ", off_end %" G_GUINT64_FORMAT, str_num, index,
4211 GST_TIME_ARGS (stream->current_timestamp),
4212 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4213 stream->current_offset_end);
4215 GST_DEBUG_OBJECT (avi, "Seeking to offset %" G_GUINT64_FORMAT,
4216 stream->index[index].offset);
4218 if (!avi_demux_do_push_seek (avi,
4219 stream->index[index].offset - (avi->stream[0].indexes ? 8 : 0))) {
4220 GST_DEBUG_OBJECT (avi, "seek event failed!");
4228 * Handle whether we can perform the seek event or if we have to let the chain
4229 * function handle seeks to build the seek indexes first.
4232 gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
4235 /* check for having parsed index already */
4236 if (!avi->have_index) {
4239 /* handle the seek event in the chain function */
4240 avi->state = GST_AVI_DEMUX_SEEK;
4242 /* copy the event */
4243 if (avi->event_seek)
4244 gst_event_unref (avi->event_seek);
4245 avi->event_seek = gst_event_ref (event);
4247 if (!avi->building_index) {
4248 avi->building_index = 1;
4249 if (avi->stream[0].indexes) {
4250 avi->odml_stream = 0;
4251 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
4252 offset = avi->odml_subidxs[0];
4254 offset = avi->idx1_offset;
4257 /* seek to the first subindex or legacy index */
4258 GST_INFO_OBJECT (avi,
4259 "Seeking to legacy index/first subindex at %" G_GUINT64_FORMAT,
4261 return avi_demux_do_push_seek (avi, offset);
4264 /* FIXME: we have to always return true so that we don't block the seek
4266 * Note: maybe it is OK to return true if we're still building the index */
4270 return avi_demux_handle_seek_push (avi, pad, event);
4274 * Helper for gst_avi_demux_invert()
4277 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
4279 memcpy (tmp, d1, bytes);
4280 memcpy (d1, d2, bytes);
4281 memcpy (d2, tmp, bytes);
4285 #define gst_avi_demux_is_uncompressed(fourcc) \
4286 (fourcc == GST_RIFF_DIB || \
4287 fourcc == GST_RIFF_rgb || \
4288 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW)
4291 * Invert DIB buffers... Takes existing buffer and
4292 * returns either the buffer or a new one (with old
4293 * one dereferenced).
4294 * FIXME: can't we preallocate tmp? and remember stride, bpp?
4297 gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
4304 if (stream->strh->type != GST_RIFF_FCC_vids)
4307 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
4308 return buf; /* Ignore non DIB buffers */
4311 s = gst_caps_get_structure (GST_PAD_CAPS (stream->pad), 0);
4312 if (!gst_structure_get_int (s, "bpp", &bpp)) {
4313 GST_WARNING ("Failed to retrieve depth from caps");
4317 if (stream->strf.vids == NULL) {
4318 GST_WARNING ("Failed to retrieve vids for stream");
4322 h = stream->strf.vids->height;
4323 w = stream->strf.vids->width;
4324 stride = w * (bpp / 8);
4326 buf = gst_buffer_make_writable (buf);
4327 if (GST_BUFFER_SIZE (buf) < (stride * h)) {
4328 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
4332 tmp = g_malloc (stride);
4334 for (y = 0; y < h / 2; y++) {
4335 swap_line (GST_BUFFER_DATA (buf) + stride * y,
4336 GST_BUFFER_DATA (buf) + stride * (h - 1 - y), tmp, stride);
4345 gst_avi_demux_add_assoc (GstAviDemux * avi, GstAviStream * stream,
4346 GstClockTime timestamp, guint64 offset, gboolean keyframe)
4348 /* do not add indefinitely for open-ended streaming */
4349 if (G_UNLIKELY (avi->element_index && avi->seekable)) {
4350 GST_LOG_OBJECT (avi, "adding association %" GST_TIME_FORMAT "-> %"
4351 G_GUINT64_FORMAT, GST_TIME_ARGS (timestamp), offset);
4352 gst_index_add_association (avi->element_index, avi->index_id,
4353 keyframe ? GST_ASSOCIATION_FLAG_KEY_UNIT : GST_ASSOCIATION_FLAG_NONE,
4354 GST_FORMAT_TIME, timestamp, GST_FORMAT_BYTES, offset, NULL);
4355 /* well, current_total determines TIME and entry DEFAULT (frame #) ... */
4356 gst_index_add_association (avi->element_index, stream->index_id,
4357 GST_ASSOCIATION_FLAG_NONE,
4358 GST_FORMAT_TIME, stream->current_total, GST_FORMAT_BYTES, offset,
4359 GST_FORMAT_DEFAULT, stream->current_entry, NULL);
4364 * Returns the aggregated GstFlowReturn.
4366 static GstFlowReturn
4367 gst_avi_demux_combine_flows (GstAviDemux * avi, GstAviStream * stream,
4371 gboolean unexpected = FALSE, not_linked = TRUE;
4373 /* store the value */
4374 stream->last_flow = ret;
4376 /* any other error that is not-linked or eos can be returned right away */
4377 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
4380 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4381 for (i = 0; i < avi->num_streams; i++) {
4382 GstAviStream *ostream = &avi->stream[i];
4384 ret = ostream->last_flow;
4385 /* no unexpected or unlinked, return */
4386 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
4389 /* we check to see if we have at least 1 unexpected or all unlinked */
4390 unexpected |= (ret == GST_FLOW_UNEXPECTED);
4391 not_linked &= (ret == GST_FLOW_NOT_LINKED);
4393 /* when we get here, we all have unlinked or unexpected */
4395 ret = GST_FLOW_NOT_LINKED;
4396 else if (unexpected)
4397 ret = GST_FLOW_UNEXPECTED;
4399 GST_LOG_OBJECT (avi, "combined %s to return %s",
4400 gst_flow_get_name (stream->last_flow), gst_flow_get_name (ret));
4404 /* move @stream to the next position in its index */
4405 static GstFlowReturn
4406 gst_avi_demux_advance (GstAviDemux * avi, GstAviStream * stream,
4409 guint old_entry, new_entry;
4411 old_entry = stream->current_entry;
4413 new_entry = old_entry + 1;
4415 /* see if we reached the end */
4416 if (new_entry > stream->stop_entry) {
4417 if (avi->segment.rate < 0.0) {
4418 if (stream->step_entry == stream->start_entry) {
4419 /* we stepped all the way to the start, eos */
4420 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
4423 /* backwards, stop becomes step, find a new step */
4424 stream->stop_entry = stream->step_entry;
4425 stream->step_entry = gst_avi_demux_index_prev (avi, stream,
4426 stream->stop_entry, TRUE);
4428 GST_DEBUG_OBJECT (avi,
4429 "reverse playback jump: start %u, step %u, stop %u",
4430 stream->start_entry, stream->step_entry, stream->stop_entry);
4432 /* and start from the previous keyframe now */
4433 new_entry = stream->step_entry;
4436 GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
4441 if (new_entry != old_entry) {
4442 stream->current_entry = new_entry;
4443 stream->current_total = stream->index[new_entry].total;
4445 if (new_entry == old_entry + 1) {
4446 GST_DEBUG_OBJECT (avi, "moved forwards from %u to %u",
4447 old_entry, new_entry);
4448 /* we simply moved one step forwards, reuse current info */
4449 stream->current_timestamp = stream->current_ts_end;
4450 stream->current_offset = stream->current_offset_end;
4451 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4452 NULL, &stream->current_ts_end, NULL, &stream->current_offset_end);
4454 /* we moved DISCONT, full update */
4455 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
4456 &stream->current_timestamp, &stream->current_ts_end,
4457 &stream->current_offset, &stream->current_offset_end);
4458 /* and MARK discont for this stream */
4459 stream->last_flow = GST_FLOW_OK;
4460 stream->discont = TRUE;
4461 GST_DEBUG_OBJECT (avi, "Moved from %u to %u, ts %" GST_TIME_FORMAT
4462 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4463 ", off_end %" G_GUINT64_FORMAT, old_entry, new_entry,
4464 GST_TIME_ARGS (stream->current_timestamp),
4465 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4466 stream->current_offset_end);
4474 GST_DEBUG_OBJECT (avi, "we are EOS");
4475 /* setting current_timestamp to -1 marks EOS */
4476 stream->current_timestamp = -1;
4477 return GST_FLOW_UNEXPECTED;
4481 /* find the stream with the lowest current position when going forwards or with
4482 * the highest position when going backwards, this is the stream
4483 * we should push from next */
4485 gst_avi_demux_find_next (GstAviDemux * avi, gfloat rate)
4487 guint64 min_time, max_time;
4488 guint stream_num, i;
4491 min_time = G_MAXUINT64;
4494 for (i = 0; i < avi->num_streams; i++) {
4496 GstAviStream *stream;
4498 stream = &avi->stream[i];
4500 /* ignore streams that finished */
4501 if (stream->last_flow == GST_FLOW_UNEXPECTED)
4504 position = stream->current_timestamp;
4506 /* position of -1 is EOS */
4507 if (position != -1) {
4508 if (rate > 0.0 && position < min_time) {
4509 min_time = position;
4511 } else if (rate < 0.0 && position >= max_time) {
4512 max_time = position;
4520 static GstFlowReturn
4521 gst_avi_demux_loop_data (GstAviDemux * avi)
4523 GstFlowReturn ret = GST_FLOW_OK;
4525 GstAviStream *stream;
4526 gboolean processed = FALSE;
4528 guint64 offset, size;
4529 GstClockTime timestamp, duration;
4530 guint64 out_offset, out_offset_end;
4532 GstAviIndexEntry *entry;
4535 stream_num = gst_avi_demux_find_next (avi, avi->segment.rate);
4538 if (G_UNLIKELY (stream_num == -1)) {
4539 GST_DEBUG_OBJECT (avi, "all streams are EOS");
4543 /* we have the stream now */
4544 stream = &avi->stream[stream_num];
4546 /* skip streams without pads */
4548 GST_DEBUG_OBJECT (avi, "skipping entry from stream %d without pad",
4553 /* get the timing info for the entry */
4554 timestamp = stream->current_timestamp;
4555 duration = stream->current_ts_end - timestamp;
4556 out_offset = stream->current_offset;
4557 out_offset_end = stream->current_offset_end;
4559 /* get the entry data info */
4560 entry = &stream->index[stream->current_entry];
4561 offset = entry->offset;
4563 keyframe = ENTRY_IS_KEYFRAME (entry);
4565 /* skip empty entries */
4567 GST_DEBUG_OBJECT (avi, "Skipping entry %u (%" G_GUINT64_FORMAT ", %p)",
4568 stream->current_entry, size, stream->pad);
4572 if (avi->segment.rate > 0.0) {
4573 /* only check this for fowards playback for now */
4574 if (keyframe && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
4575 && (timestamp > avi->segment.stop)) {
4580 GST_LOG ("reading buffer (size=%" G_GUINT64_FORMAT "), stream %d, pos %"
4581 G_GUINT64_FORMAT " (0x%" G_GINT64_MODIFIER "x), kf %d", size,
4582 stream_num, offset, offset, keyframe);
4584 /* FIXME, check large chunks and cut them up */
4586 /* pull in the data */
4587 ret = gst_pad_pull_range (avi->sinkpad, offset, size, &buf);
4588 if (ret != GST_FLOW_OK)
4591 /* check for short buffers, this is EOS as well */
4592 if (GST_BUFFER_SIZE (buf) < size)
4595 /* invert the picture if needed */
4596 buf = gst_avi_demux_invert (stream, buf);
4598 /* mark non-keyframes */
4600 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4602 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
4604 GST_BUFFER_TIMESTAMP (buf) = timestamp;
4605 GST_BUFFER_DURATION (buf) = duration;
4606 GST_BUFFER_OFFSET (buf) = out_offset;
4607 GST_BUFFER_OFFSET_END (buf) = out_offset_end;
4609 /* mark discont when pending */
4610 if (stream->discont) {
4611 GST_DEBUG_OBJECT (avi, "setting DISCONT flag");
4612 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4613 stream->discont = FALSE;
4616 gst_avi_demux_add_assoc (avi, stream, timestamp, offset, keyframe);
4618 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4620 /* update current position in the segment */
4621 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, timestamp);
4623 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %u, ts %"
4624 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4625 ", off_end %" G_GUINT64_FORMAT,
4626 GST_BUFFER_SIZE (buf), GST_TIME_ARGS (timestamp),
4627 GST_TIME_ARGS (duration), out_offset, out_offset_end);
4629 ret = gst_pad_push (stream->pad, buf);
4631 /* mark as processed, we increment the frame and byte counters then
4632 * leave the while loop and return the GstFlowReturn */
4635 if (avi->segment.rate < 0) {
4636 if (timestamp > avi->segment.stop && ret == GST_FLOW_UNEXPECTED) {
4637 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
4638 * we are at the end of the segment, so we just need to jump
4639 * back to the previous section. */
4640 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
4645 /* move to next item */
4646 ret = gst_avi_demux_advance (avi, stream, ret);
4649 ret = gst_avi_demux_combine_flows (avi, stream, ret);
4650 } while (!processed);
4658 GST_DEBUG_OBJECT (avi, "No samples left for any streams - EOS");
4659 ret = GST_FLOW_UNEXPECTED;
4664 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
4665 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
4666 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (avi->segment.stop));
4667 ret = GST_FLOW_UNEXPECTED;
4668 /* move to next stream */
4673 GST_DEBUG_OBJECT (avi, "pull range failed: pos=%" G_GUINT64_FORMAT
4674 " size=%" G_GUINT64_FORMAT, offset, size);
4679 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
4680 ", only got %d/%" G_GUINT64_FORMAT " bytes (truncated file?)", offset,
4681 GST_BUFFER_SIZE (buf), size);
4682 gst_buffer_unref (buf);
4683 ret = GST_FLOW_UNEXPECTED;
4689 * Read data. If we have an index it delegates to
4690 * gst_avi_demux_process_next_entry().
4692 static GstFlowReturn
4693 gst_avi_demux_stream_data (GstAviDemux * avi)
4698 GstFlowReturn res = GST_FLOW_OK;
4699 GstFormat format = GST_FORMAT_TIME;
4701 if (G_UNLIKELY (avi->have_eos)) {
4702 /* Clean adapter, we're done */
4703 gst_adapter_clear (avi->adapter);
4704 return GST_FLOW_UNEXPECTED;
4707 if (G_UNLIKELY (avi->todrop)) {
4710 if ((drop = gst_adapter_available (avi->adapter))) {
4711 if (drop > avi->todrop)
4713 GST_DEBUG_OBJECT (avi, "Dropping %d bytes", drop);
4714 gst_adapter_flush (avi->adapter, drop);
4715 avi->todrop -= drop;
4716 avi->offset += drop;
4720 /* Iterate until need more data, so adapter won't grow too much */
4722 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
4726 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
4727 GST_FOURCC_ARGS (tag), size);
4729 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
4730 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
4731 GST_LOG ("Chunk ok");
4732 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
4733 GST_DEBUG ("Found sub-index tag");
4734 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4735 /* accept 0 size buffer here */
4736 avi->abort_buffering = FALSE;
4737 GST_DEBUG (" skipping %d bytes for now", size);
4738 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4741 } else if (tag == GST_RIFF_TAG_RIFF) {
4742 /* RIFF tags can appear in ODML files, just jump over them */
4743 if (gst_adapter_available (avi->adapter) >= 12) {
4744 GST_DEBUG ("Found RIFF tag, skipping RIFF header");
4745 gst_adapter_flush (avi->adapter, 12);
4749 } else if (tag == GST_RIFF_TAG_idx1) {
4750 GST_DEBUG ("Found index tag");
4751 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4752 /* accept 0 size buffer here */
4753 avi->abort_buffering = FALSE;
4754 GST_DEBUG (" skipping %d bytes for now", size);
4755 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4758 } else if (tag == GST_RIFF_TAG_LIST) {
4759 /* movi chunks might be grouped in rec list */
4760 if (gst_adapter_available (avi->adapter) >= 12) {
4761 GST_DEBUG ("Found LIST tag, skipping LIST header");
4762 gst_adapter_flush (avi->adapter, 12);
4766 } else if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ) {
4767 /* rec list might contain JUNK chunks */
4768 GST_DEBUG ("Found JUNK tag");
4769 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
4770 /* accept 0 size buffer here */
4771 avi->abort_buffering = FALSE;
4772 GST_DEBUG (" skipping %d bytes for now", size);
4773 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4777 GST_DEBUG ("No more stream chunks, send EOS");
4778 avi->have_eos = TRUE;
4779 return GST_FLOW_UNEXPECTED;
4782 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
4783 /* supposedly one hopes to catch a nicer chunk later on ... */
4784 /* FIXME ?? give up here rather than possibly ending up going
4785 * through the whole file */
4786 if (avi->abort_buffering) {
4787 avi->abort_buffering = FALSE;
4788 gst_adapter_flush (avi->adapter, 8);
4792 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
4793 GST_FOURCC_ARGS (tag), size);
4795 stream_nr = CHUNKID_TO_STREAMNR (tag);
4797 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
4799 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
4800 stream_nr, GST_FOURCC_ARGS (tag));
4801 avi->offset += 8 + GST_ROUND_UP_2 (size);
4802 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
4804 GstAviStream *stream;
4805 GstClockTime next_ts = 0;
4809 gst_adapter_flush (avi->adapter, 8);
4812 buf = gst_adapter_take_buffer (avi->adapter, GST_ROUND_UP_2 (size));
4813 /* patch the size */
4814 GST_BUFFER_SIZE (buf) = size;
4815 offset = avi->offset;
4816 avi->offset += 8 + GST_ROUND_UP_2 (size);
4818 stream = &avi->stream[stream_nr];
4820 /* set delay (if any)
4821 if (stream->strh->init_frames == stream->current_frame &&
4823 stream->delay = next_ts;
4826 /* parsing of corresponding header may have failed */
4827 if (G_UNLIKELY (!stream->pad)) {
4828 GST_WARNING_OBJECT (avi, "no pad for stream ID %" GST_FOURCC_FORMAT,
4829 GST_FOURCC_ARGS (tag));
4830 gst_buffer_unref (buf);
4832 GstClockTime dur_ts = 0;
4834 /* get time of this buffer */
4835 gst_pad_query_position (stream->pad, &format, (gint64 *) & next_ts);
4836 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4839 gst_avi_demux_add_assoc (avi, stream, next_ts, offset, FALSE);
4841 /* increment our positions */
4842 stream->current_entry++;
4843 stream->current_total += size;
4845 /* invert the picture if needed */
4846 buf = gst_avi_demux_invert (stream, buf);
4848 gst_pad_query_position (stream->pad, &format, (gint64 *) & dur_ts);
4849 if (G_UNLIKELY (format != GST_FORMAT_TIME))
4852 GST_BUFFER_TIMESTAMP (buf) = next_ts;
4853 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
4854 if (stream->strh->type == GST_RIFF_FCC_vids) {
4855 GST_BUFFER_OFFSET (buf) = stream->current_entry - 1;
4856 GST_BUFFER_OFFSET_END (buf) = stream->current_entry;
4858 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
4859 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
4862 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
4863 GST_DEBUG_OBJECT (avi,
4864 "Pushing buffer with time=%" GST_TIME_FORMAT ", duration %"
4865 GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
4866 " and size %d over pad %s", GST_TIME_ARGS (next_ts),
4867 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf),
4868 size, GST_PAD_NAME (stream->pad));
4870 /* update current position in the segment */
4871 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, next_ts);
4873 /* mark discont when pending */
4874 if (G_UNLIKELY (stream->discont)) {
4875 GST_DEBUG_OBJECT (avi, "Setting DISCONT");
4876 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4877 stream->discont = FALSE;
4879 res = gst_pad_push (stream->pad, buf);
4882 res = gst_avi_demux_combine_flows (avi, stream, res);
4883 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4884 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
4897 GST_DEBUG_OBJECT (avi, "format %s != GST_FORMAT_TIME",
4898 gst_format_get_name (format));
4899 res = GST_FLOW_ERROR;
4905 * Send pending tags.
4908 push_tag_lists (GstAviDemux * avi)
4916 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
4918 for (i = 0; i < avi->num_streams; i++) {
4919 GstAviStream *stream = &avi->stream[i];
4920 GstPad *pad = stream->pad;
4922 tags = stream->taglist;
4925 GST_DEBUG_OBJECT (pad, "Tags: %" GST_PTR_FORMAT, tags);
4927 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (avi), pad, tags);
4928 stream->taglist = NULL;
4932 if (!(tags = avi->globaltags))
4933 tags = gst_tag_list_new ();
4935 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
4936 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
4938 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, tags);
4939 gst_element_found_tags (GST_ELEMENT_CAST (avi), tags);
4940 avi->globaltags = NULL;
4941 avi->got_tags = FALSE;
4945 gst_avi_demux_loop (GstPad * pad)
4948 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
4950 switch (avi->state) {
4951 case GST_AVI_DEMUX_START:
4952 res = gst_avi_demux_stream_init_pull (avi);
4953 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4954 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
4957 avi->state = GST_AVI_DEMUX_HEADER;
4959 case GST_AVI_DEMUX_HEADER:
4960 res = gst_avi_demux_stream_header_pull (avi);
4961 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4962 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
4965 avi->state = GST_AVI_DEMUX_MOVI;
4967 case GST_AVI_DEMUX_MOVI:
4968 if (G_UNLIKELY (avi->seg_event)) {
4969 gst_avi_demux_push_event (avi, avi->seg_event);
4970 avi->seg_event = NULL;
4972 if (G_UNLIKELY (avi->got_tags)) {
4973 push_tag_lists (avi);
4975 /* process each index entry in turn */
4976 res = gst_avi_demux_loop_data (avi);
4978 /* pause when error */
4979 if (G_UNLIKELY (res != GST_FLOW_OK)) {
4980 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
4985 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
4986 res = GST_FLOW_ERROR;
4994 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
4995 avi->segment_running = FALSE;
4996 gst_pad_pause_task (avi->sinkpad);
4998 if (GST_FLOW_IS_FATAL (res) || (res == GST_FLOW_NOT_LINKED)) {
4999 gboolean push_eos = TRUE;
5001 if (res == GST_FLOW_UNEXPECTED) {
5002 /* handle end-of-stream/segment */
5003 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5006 if ((stop = avi->segment.stop) == -1)
5007 stop = avi->segment.duration;
5009 GST_INFO_OBJECT (avi, "sending segment_done");
5011 gst_element_post_message
5012 (GST_ELEMENT_CAST (avi),
5013 gst_message_new_segment_done (GST_OBJECT_CAST (avi),
5014 GST_FORMAT_TIME, stop));
5018 /* for fatal errors we post an error message */
5019 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
5020 (_("Internal data stream error.")),
5021 ("streaming stopped, reason %s", gst_flow_get_name (res)));
5024 GST_INFO_OBJECT (avi, "sending eos");
5025 if (!gst_avi_demux_push_event (avi, gst_event_new_eos ()) &&
5026 (res == GST_FLOW_UNEXPECTED)) {
5027 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
5028 (NULL), ("got eos but no streams (yet)"));
5035 static GstFlowReturn
5036 gst_avi_demux_chain (GstPad * pad, GstBuffer * buf)
5039 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
5042 if (GST_BUFFER_IS_DISCONT (buf)) {
5043 GST_DEBUG_OBJECT (avi, "got DISCONT");
5044 gst_adapter_clear (avi->adapter);
5045 /* mark all streams DISCONT */
5046 for (i = 0; i < avi->num_streams; i++)
5047 avi->stream[i].discont = TRUE;
5050 if (GST_BUFFER_IS_DISCONT (buf))
5051 gst_adapter_clear (avi->adapter);
5053 GST_DEBUG ("Store %d bytes in adapter", GST_BUFFER_SIZE (buf));
5054 gst_adapter_push (avi->adapter, buf);
5056 switch (avi->state) {
5057 case GST_AVI_DEMUX_START:
5058 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
5059 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5063 case GST_AVI_DEMUX_HEADER:
5064 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
5065 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5069 case GST_AVI_DEMUX_MOVI:
5070 if (G_UNLIKELY (avi->seg_event)) {
5071 gst_avi_demux_push_event (avi, avi->seg_event);
5072 avi->seg_event = NULL;
5074 if (G_UNLIKELY (avi->got_tags)) {
5075 push_tag_lists (avi);
5077 res = gst_avi_demux_stream_data (avi);
5079 case GST_AVI_DEMUX_SEEK:
5082 /* obtain and parse indexes */
5083 if (avi->stream[0].indexes && !gst_avi_demux_read_subindexes_push (avi)) {
5084 /* seek in subindex read function failed */
5085 res = GST_FLOW_ERROR;
5088 if (!avi->stream[0].indexes && !avi->have_index
5089 && avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
5090 gst_avi_demux_stream_index_push (avi);
5092 if (avi->have_index) {
5093 /* use the indexes now to construct nice durations */
5094 gst_avi_demux_calculate_durations_from_index (avi);
5096 /* still parsing indexes */
5100 /* calculate and perform seek */
5101 if (!avi_demux_handle_seek_push (avi, avi->sinkpad, avi->event_seek)) {
5102 GST_WARNING ("Push mode seek failed");
5103 res = GST_FLOW_ERROR;
5105 gst_event_unref (avi->event_seek);
5106 avi->state = GST_AVI_DEMUX_MOVI;
5109 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
5110 ("Illegal internal state"));
5111 res = GST_FLOW_ERROR;
5115 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
5116 gst_flow_get_name (res));
5118 if (G_UNLIKELY (avi->abort_buffering)) {
5119 avi->abort_buffering = FALSE;
5120 res = GST_FLOW_ERROR;
5121 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
5128 gst_avi_demux_sink_activate (GstPad * sinkpad)
5130 if (gst_pad_check_pull_range (sinkpad)) {
5131 GST_DEBUG ("going to pull mode");
5132 return gst_pad_activate_pull (sinkpad, TRUE);
5134 GST_DEBUG ("going to push (streaming) mode");
5135 return gst_pad_activate_push (sinkpad, TRUE);
5140 gst_avi_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
5142 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (sinkpad));
5145 avi->segment_running = TRUE;
5146 avi->streaming = FALSE;
5147 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
5150 avi->segment_running = FALSE;
5151 return gst_pad_stop_task (sinkpad);
5156 gst_avi_demux_activate_push (GstPad * pad, gboolean active)
5158 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (pad));
5161 GST_DEBUG ("avi: activating push/chain function");
5162 avi->streaming = TRUE;
5164 /* create index for some push based seeking if not provided */
5165 GST_OBJECT_LOCK (avi);
5166 if (!avi->element_index) {
5167 GST_DEBUG_OBJECT (avi, "creating index");
5168 avi->element_index = gst_index_factory_make ("memindex");
5170 GST_OBJECT_UNLOCK (avi);
5171 /* object lock might be taken again */
5172 gst_index_get_writer_id (avi->element_index, GST_OBJECT_CAST (avi),
5176 GST_DEBUG ("avi: deactivating push/chain function");
5183 gst_avi_demux_set_index (GstElement * element, GstIndex * index)
5185 GstAviDemux *avi = GST_AVI_DEMUX (element);
5187 GST_OBJECT_LOCK (avi);
5188 if (avi->element_index)
5189 gst_object_unref (avi->element_index);
5191 avi->element_index = gst_object_ref (index);
5193 avi->element_index = NULL;
5195 GST_OBJECT_UNLOCK (avi);
5196 /* object lock might be taken again */
5198 gst_index_get_writer_id (index, GST_OBJECT_CAST (element), &avi->index_id);
5199 GST_DEBUG_OBJECT (avi, "Set index %" GST_PTR_FORMAT, avi->element_index);
5203 gst_avi_demux_get_index (GstElement * element)
5205 GstIndex *result = NULL;
5206 GstAviDemux *avi = GST_AVI_DEMUX (element);
5208 GST_OBJECT_LOCK (avi);
5209 if (avi->element_index)
5210 result = gst_object_ref (avi->element_index);
5211 GST_OBJECT_UNLOCK (avi);
5213 GST_DEBUG_OBJECT (avi, "Returning index %" GST_PTR_FORMAT, result);
5218 static GstStateChangeReturn
5219 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
5221 GstStateChangeReturn ret;
5222 GstAviDemux *avi = GST_AVI_DEMUX (element);
5224 switch (transition) {
5225 case GST_STATE_CHANGE_READY_TO_PAUSED:
5226 avi->streaming = FALSE;
5227 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
5233 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5234 if (ret == GST_STATE_CHANGE_FAILURE)
5237 switch (transition) {
5238 case GST_STATE_CHANGE_PAUSED_TO_READY:
5239 gst_avi_demux_reset (avi);