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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
21 /* Element-Checklist-Version: 5 */
24 * SECTION:element-avidemux
27 * Demuxes an .avi file into raw or compressed audio and/or video streams.
29 * This element supports both push and pull-based scheduling, depending on the
30 * capabilities of the upstream elements.
32 * ## Example launch line
34 * gst-launch-1.0 filesrc location=test.avi ! avidemux name=demux demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink demux.video_00 ! queue ! decodebin ! videoconvert ! 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.
49 #include "gst/riff/riff-media.h"
50 #include "gstavielements.h"
51 #include "gstavidemux.h"
53 #include <gst/gst-i18n-plugin.h>
54 #include <gst/base/gstadapter.h>
55 #include <gst/tag/tag.h>
57 #define DIV_ROUND_UP(s,v) (((s) + ((v)-1)) / (v))
59 #define GST_AVI_KEYFRAME (1 << 0)
60 #define ENTRY_IS_KEYFRAME(e) ((e)->flags == GST_AVI_KEYFRAME)
61 #define ENTRY_SET_KEYFRAME(e) ((e)->flags = GST_AVI_KEYFRAME)
62 #define ENTRY_UNSET_KEYFRAME(e) ((e)->flags = 0)
65 GST_DEBUG_CATEGORY_STATIC (avidemux_debug);
66 #define GST_CAT_DEFAULT avidemux_debug
68 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
71 GST_STATIC_CAPS ("video/x-msvideo")
74 #ifndef GST_DISABLE_GST_DEBUG
75 static const char *const snap_types[2][2] = {
77 {"before", "nearest"},
81 static void gst_avi_demux_finalize (GObject * object);
83 static void gst_avi_demux_reset (GstAviDemux * avi);
86 static const GstEventMask *gst_avi_demux_get_event_mask (GstPad * pad);
88 static gboolean gst_avi_demux_handle_src_event (GstPad * pad,
89 GstObject * parent, GstEvent * event);
90 static gboolean gst_avi_demux_handle_sink_event (GstPad * pad,
91 GstObject * parent, GstEvent * event);
92 static gboolean gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event);
95 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
97 static gboolean gst_avi_demux_handle_src_query (GstPad * pad,
98 GstObject * parent, GstQuery * query);
99 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
100 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
102 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment,
104 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
106 static gboolean gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
108 static void gst_avi_demux_loop (GstPad * pad);
109 static gboolean gst_avi_demux_sink_activate (GstPad * sinkpad,
111 static gboolean gst_avi_demux_sink_activate_mode (GstPad * sinkpad,
112 GstObject * parent, GstPadMode mode, gboolean active);
113 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstObject * parent,
116 static void gst_avi_demux_set_index (GstElement * element, GstIndex * index);
117 static GstIndex *gst_avi_demux_get_index (GstElement * element);
119 static GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
120 GstStateChange transition);
121 static void gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi);
122 static void gst_avi_demux_get_buffer_info (GstAviDemux * avi,
123 GstAviStream * stream, guint entry_n, GstClockTime * timestamp,
124 GstClockTime * ts_end, guint64 * offset, guint64 * offset_end);
126 static void gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf);
127 static void gst_avi_demux_parse_strd (GstAviDemux * avi, GstBuffer * buf);
129 static void parse_tag_value (GstAviDemux * avi, GstTagList * taglist,
130 const gchar * type, guint8 * ptr, guint tsize);
132 /* GObject methods */
134 #define gst_avi_demux_parent_class parent_class
135 G_DEFINE_TYPE (GstAviDemux, gst_avi_demux, GST_TYPE_ELEMENT);
136 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (avidemux, "avidemux", GST_RANK_PRIMARY,
137 GST_TYPE_AVI_DEMUX, avi_element_init (plugin));
140 gst_avi_demux_class_init (GstAviDemuxClass * klass)
142 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
143 GObjectClass *gobject_class = (GObjectClass *) klass;
144 GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl, *subpicsrctempl;
145 GstCaps *audcaps, *vidcaps, *subcaps, *subpiccaps;
147 GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
148 0, "Demuxer for AVI streams");
150 gobject_class->finalize = gst_avi_demux_finalize;
152 gstelement_class->change_state =
153 GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
155 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_avi_demux_set_index);
156 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_avi_demux_get_index);
159 audcaps = gst_riff_create_audio_template_caps ();
160 gst_caps_append (audcaps, gst_caps_new_empty_simple ("audio/x-avi-unknown"));
161 audiosrctempl = gst_pad_template_new ("audio_%u",
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_empty_simple ("video/x-avi-unknown"));
167 videosrctempl = gst_pad_template_new ("video_%u",
168 GST_PAD_SRC, GST_PAD_SOMETIMES, vidcaps);
170 subcaps = gst_caps_new_empty_simple ("application/x-subtitle-avi");
171 subsrctempl = gst_pad_template_new ("subtitle_%u",
172 GST_PAD_SRC, GST_PAD_SOMETIMES, subcaps);
173 subpiccaps = gst_caps_new_empty_simple ("subpicture/x-xsub");
174 subpicsrctempl = gst_pad_template_new ("subpicture_%u",
175 GST_PAD_SRC, GST_PAD_SOMETIMES, subpiccaps);
176 gst_element_class_add_pad_template (gstelement_class, audiosrctempl);
177 gst_element_class_add_pad_template (gstelement_class, videosrctempl);
178 gst_element_class_add_pad_template (gstelement_class, subsrctempl);
179 gst_element_class_add_pad_template (gstelement_class, subpicsrctempl);
180 gst_element_class_add_static_pad_template (gstelement_class, &sink_templ);
182 gst_caps_unref (audcaps);
183 gst_caps_unref (vidcaps);
184 gst_caps_unref (subcaps);
185 gst_caps_unref (subpiccaps);
187 gst_element_class_set_static_metadata (gstelement_class, "Avi demuxer",
189 "Demultiplex an avi file into audio and video",
190 "Erik Walthinsen <omega@cse.ogi.edu>, "
191 "Wim Taymans <wim.taymans@chello.be>, "
192 "Thijs Vermeir <thijsvermeir@gmail.com>");
196 gst_avi_demux_init (GstAviDemux * avi)
198 avi->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
199 gst_pad_set_activate_function (avi->sinkpad,
200 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate));
201 gst_pad_set_activatemode_function (avi->sinkpad,
202 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate_mode));
203 gst_pad_set_chain_function (avi->sinkpad,
204 GST_DEBUG_FUNCPTR (gst_avi_demux_chain));
205 gst_pad_set_event_function (avi->sinkpad,
206 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_sink_event));
207 gst_element_add_pad (GST_ELEMENT_CAST (avi), avi->sinkpad);
209 avi->adapter = gst_adapter_new ();
210 avi->flowcombiner = gst_flow_combiner_new ();
212 gst_avi_demux_reset (avi);
214 GST_OBJECT_FLAG_SET (avi, GST_ELEMENT_FLAG_INDEXABLE);
218 gst_avi_demux_finalize (GObject * object)
220 GstAviDemux *avi = GST_AVI_DEMUX (object);
222 GST_DEBUG ("AVI: finalize");
224 g_object_unref (avi->adapter);
225 gst_flow_combiner_free (avi->flowcombiner);
227 G_OBJECT_CLASS (parent_class)->finalize (object);
231 gst_avi_demux_reset_stream (GstAviDemux * avi, GstAviStream * stream)
233 g_free (stream->strh);
234 g_free (stream->strf.data);
235 g_free (stream->name);
236 g_free (stream->index);
237 g_free (stream->indexes);
238 if (stream->initdata)
239 gst_buffer_unref (stream->initdata);
240 if (stream->extradata)
241 gst_buffer_unref (stream->extradata);
242 if (stream->rgb8_palette)
243 gst_buffer_unref (stream->rgb8_palette);
245 if (stream->exposed) {
246 gst_pad_set_active (stream->pad, FALSE);
247 gst_element_remove_pad (GST_ELEMENT_CAST (avi), stream->pad);
248 gst_flow_combiner_remove_pad (avi->flowcombiner, stream->pad);
250 gst_object_unref (stream->pad);
252 if (stream->taglist) {
253 gst_tag_list_unref (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->num_sp_streams = 0;
275 avi->main_stream = -1;
277 avi->have_group_id = FALSE;
278 avi->group_id = G_MAXUINT;
280 avi->state = GST_AVI_DEMUX_START;
282 avi->building_index = FALSE;
284 avi->index_offset = 0;
289 if (avi->element_index)
290 gst_object_unref (avi->element_index);
291 avi->element_index = NULL;
294 if (avi->seg_event) {
295 gst_event_unref (avi->seg_event);
296 avi->seg_event = NULL;
298 if (avi->seek_event) {
299 gst_event_unref (avi->seek_event);
300 avi->seek_event = NULL;
304 gst_tag_list_unref (avi->globaltags);
305 avi->globaltags = NULL;
307 avi->got_tags = TRUE; /* we always want to push global tags */
308 avi->have_eos = FALSE;
309 avi->seekable = TRUE;
311 gst_adapter_clear (avi->adapter);
313 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
314 avi->segment_seqnum = 0;
318 /* GstElement methods */
321 static const GstFormat *
322 gst_avi_demux_get_src_formats (GstPad * pad)
324 GstAviStream *stream = gst_pad_get_element_private (pad);
326 static const GstFormat src_a_formats[] = {
332 static const GstFormat src_v_formats[] = {
338 return (stream->strh->type == GST_RIFF_FCC_auds ?
339 src_a_formats : src_v_formats);
343 /* assumes stream->strf.auds->av_bps != 0 */
344 static inline GstClockTime
345 avi_stream_convert_bytes_to_time_unchecked (GstAviStream * stream,
348 return gst_util_uint64_scale_int (bytes, GST_SECOND,
349 stream->strf.auds->av_bps);
352 static inline guint64
353 avi_stream_convert_time_to_bytes_unchecked (GstAviStream * stream,
356 return gst_util_uint64_scale_int (time, stream->strf.auds->av_bps,
360 /* assumes stream->strh->rate != 0 */
361 static inline GstClockTime
362 avi_stream_convert_frames_to_time_unchecked (GstAviStream * stream,
365 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
369 static inline guint64
370 avi_stream_convert_time_to_frames_unchecked (GstAviStream * stream,
373 return gst_util_uint64_scale (time, stream->strh->rate,
374 stream->strh->scale * GST_SECOND);
378 gst_avi_demux_src_convert (GstPad * pad,
379 GstFormat src_format,
380 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
382 GstAviStream *stream = gst_pad_get_element_private (pad);
386 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
387 ", dest_format:%s", gst_format_get_name (src_format), src_value,
388 gst_format_get_name (*dest_format));
390 if (G_UNLIKELY (src_format == *dest_format)) {
391 *dest_value = src_value;
394 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
398 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
399 (src_format == GST_FORMAT_BYTES
400 || *dest_format == GST_FORMAT_BYTES))) {
405 switch (src_format) {
406 case GST_FORMAT_TIME:
407 switch (*dest_format) {
408 case GST_FORMAT_BYTES:
409 *dest_value = gst_util_uint64_scale_int (src_value,
410 stream->strf.auds->av_bps, GST_SECOND);
412 case GST_FORMAT_DEFAULT:
414 gst_util_uint64_scale_round (src_value, stream->strh->rate,
415 stream->strh->scale * GST_SECOND);
422 case GST_FORMAT_BYTES:
423 switch (*dest_format) {
424 case GST_FORMAT_TIME:
425 if (stream->strf.auds->av_bps != 0) {
426 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
436 case GST_FORMAT_DEFAULT:
437 switch (*dest_format) {
438 case GST_FORMAT_TIME:
440 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
453 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
454 gst_format_get_name (*dest_format), *dest_value);
459 gst_avi_demux_handle_src_query (GstPad * pad, GstObject * parent,
463 GstAviDemux *avi = GST_AVI_DEMUX (parent);
465 GstAviStream *stream = gst_pad_get_element_private (pad);
467 if (!stream->strh || !stream->strf.data)
468 return gst_pad_query_default (pad, parent, query);
470 switch (GST_QUERY_TYPE (query)) {
471 case GST_QUERY_POSITION:{
474 GST_DEBUG ("pos query for stream %u: frames %u, bytes %u",
475 stream->num, stream->current_entry, stream->current_total);
477 /* FIXME, this looks clumsy */
478 if (stream->strh->type == GST_RIFF_FCC_auds) {
479 if (stream->is_vbr) {
481 pos = avi_stream_convert_frames_to_time_unchecked (stream,
482 stream->current_entry);
483 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
484 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
485 } else if (stream->strf.auds->av_bps != 0) {
487 pos = avi_stream_convert_bytes_to_time_unchecked (stream,
488 stream->current_total);
489 GST_DEBUG_OBJECT (avi,
490 "CBR convert bytes %u, time %" GST_TIME_FORMAT,
491 stream->current_total, GST_TIME_ARGS (pos));
492 } else if (stream->idx_n != 0 && stream->total_bytes != 0) {
493 /* calculate timestamps based on percentage of length */
494 guint64 xlen = avi->avih->us_frame *
495 avi->avih->tot_frames * GST_USECOND;
497 pos = gst_util_uint64_scale (xlen, stream->current_total,
498 stream->total_bytes);
499 GST_DEBUG_OBJECT (avi,
500 "CBR perc convert bytes %u, time %" GST_TIME_FORMAT,
501 stream->current_total, GST_TIME_ARGS (pos));
507 if (stream->strh->rate != 0) {
508 pos = gst_util_uint64_scale ((guint64) stream->current_entry *
509 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
511 pos = stream->current_entry * avi->avih->us_frame * GST_USECOND;
515 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
516 gst_query_set_position (query, GST_FORMAT_TIME, pos);
518 GST_WARNING ("pos query failed");
521 case GST_QUERY_DURATION:
524 GstClockTime duration;
526 /* only act on audio or video streams */
527 if (stream->strh->type != GST_RIFF_FCC_auds &&
528 stream->strh->type != GST_RIFF_FCC_vids &&
529 stream->strh->type != GST_RIFF_FCC_iavs) {
534 /* take stream duration, fall back to avih duration */
535 if ((duration = stream->duration) == -1)
536 if ((duration = stream->hdr_duration) == -1)
537 duration = avi->duration;
539 gst_query_parse_duration (query, &fmt, NULL);
542 case GST_FORMAT_TIME:
543 gst_query_set_duration (query, fmt, duration);
545 case GST_FORMAT_DEFAULT:
548 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
551 if (stream->idx_n > 0)
552 gst_query_set_duration (query, fmt, stream->idx_n);
553 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
554 duration, fmt, &dur))
555 gst_query_set_duration (query, fmt, dur);
564 case GST_QUERY_SEEKING:{
567 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
568 if (fmt == GST_FORMAT_TIME) {
569 gboolean seekable = TRUE;
571 if (avi->streaming) {
572 seekable = avi->seekable;
575 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
576 0, stream->duration);
581 case GST_QUERY_CONVERT:{
582 GstFormat src_fmt, dest_fmt;
583 gint64 src_val, dest_val;
585 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
586 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
588 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
590 res = gst_pad_query_default (pad, parent, query);
593 case GST_QUERY_SEGMENT:
598 format = avi->segment.format;
601 gst_segment_to_stream_time (&avi->segment, format,
603 if ((stop = avi->segment.stop) == -1)
604 stop = avi->segment.duration;
606 stop = gst_segment_to_stream_time (&avi->segment, format, stop);
608 gst_query_set_segment (query, avi->segment.rate, format, start, stop);
613 res = gst_pad_query_default (pad, parent, query);
621 static const GstEventMask *
622 gst_avi_demux_get_event_mask (GstPad * pad)
624 static const GstEventMask masks[] = {
625 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
635 gst_avi_demux_seek_streams (GstAviDemux * avi, guint64 offset, gboolean before)
637 GstAviStream *stream;
638 GstIndexEntry *entry;
640 gint64 val, min = offset;
642 for (i = 0; i < avi->num_streams; i++) {
643 stream = &avi->stream[i];
645 entry = gst_index_get_assoc_entry (avi->element_index, stream->index_id,
646 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
647 GST_ASSOCIATION_FLAG_NONE, GST_FORMAT_BYTES, offset);
651 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
652 GST_DEBUG_OBJECT (avi, "stream %d, previous entry at %"
653 G_GUINT64_FORMAT, i, val);
661 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
662 stream->current_entry = 0;
663 stream->current_total = 0;
667 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
668 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT,
671 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &val);
672 stream->current_total = val;
673 gst_index_entry_assoc_map (entry, GST_FORMAT_DEFAULT, &val);
674 stream->current_entry = val;
682 gst_avi_demux_index_entry_offset_search (GstAviIndexEntry * entry,
685 if (entry->offset < *offset)
687 else if (entry->offset > *offset)
693 gst_avi_demux_seek_streams_index (GstAviDemux * avi, guint64 offset,
696 GstAviStream *stream;
697 GstAviIndexEntry *entry;
699 gint64 val, min = offset;
702 for (i = 0; i < avi->num_streams; i++) {
703 stream = &avi->stream[i];
705 /* compensate for chunk header */
708 gst_util_array_binary_search (stream->index, stream->idx_n,
709 sizeof (GstAviIndexEntry),
710 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
711 before ? GST_SEARCH_MODE_BEFORE : GST_SEARCH_MODE_AFTER, &offset, NULL);
715 index = entry - stream->index;
719 val = stream->index[index].offset;
720 GST_DEBUG_OBJECT (avi,
721 "stream %d, previous entry at %" G_GUINT64_FORMAT, i, val);
729 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
730 stream->current_entry = 0;
731 stream->current_total = 0;
735 val = stream->index[index].offset - 8;
736 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT, i,
739 stream->current_total = stream->index[index].total;
740 stream->current_entry = index;
746 #define GST_AVI_SEEK_PUSH_DISPLACE (4 * GST_SECOND)
749 gst_avi_demux_handle_sink_event (GstPad * pad, GstObject * parent,
753 GstAviDemux *avi = GST_AVI_DEMUX (parent);
755 GST_DEBUG_OBJECT (avi,
756 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
758 switch (GST_EVENT_TYPE (event)) {
759 case GST_EVENT_SEGMENT:
761 gint64 boffset, offset = 0;
763 GstEvent *segment_event;
765 /* some debug output */
766 gst_event_copy_segment (event, &segment);
767 GST_DEBUG_OBJECT (avi, "received newsegment %" GST_SEGMENT_FORMAT,
770 /* chain will send initial newsegment after pads have been added */
771 if (avi->state != GST_AVI_DEMUX_MOVI) {
772 GST_DEBUG_OBJECT (avi, "still starting, eating event");
776 /* we only expect a BYTE segment, e.g. following a seek */
777 if (segment.format != GST_FORMAT_BYTES) {
778 GST_DEBUG_OBJECT (avi, "unsupported segment format, ignoring");
782 if (avi->have_index) {
783 GstAviIndexEntry *entry;
784 guint i = 0, index = 0, k = 0;
785 GstAviStream *stream;
787 /* compensate chunk header, stored index offset points after header */
788 boffset = segment.start + 8;
789 /* find which stream we're on */
791 stream = &avi->stream[i];
793 /* find the index for start bytes offset */
794 entry = gst_util_array_binary_search (stream->index,
795 stream->idx_n, sizeof (GstAviIndexEntry),
796 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
797 GST_SEARCH_MODE_AFTER, &boffset, NULL);
801 index = entry - stream->index;
803 /* we are on the stream with a chunk start offset closest to start */
804 if (!offset || stream->index[index].offset < offset) {
805 offset = stream->index[index].offset;
808 /* exact match needs no further searching */
809 if (stream->index[index].offset == boffset)
811 } while (++i < avi->num_streams);
814 stream = &avi->stream[k];
816 /* so we have no idea what is to come, or where we are */
818 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
822 /* get the ts corresponding to start offset bytes for the stream */
823 gst_avi_demux_get_buffer_info (avi, stream, index,
824 (GstClockTime *) & segment.time, NULL, NULL, NULL);
826 } else if (avi->element_index) {
827 GstIndexEntry *entry;
829 /* Let's check if we have an index entry for this position */
830 entry = gst_index_get_assoc_entry (avi->element_index, avi->index_id,
831 GST_INDEX_LOOKUP_AFTER, GST_ASSOCIATION_FLAG_NONE,
832 GST_FORMAT_BYTES, segment.start);
834 /* we can not go where we have not yet been before ... */
836 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
840 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME,
841 (gint64 *) & segment.time);
842 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &offset);
845 GST_WARNING_OBJECT (avi, "no index data, forcing EOS");
849 segment.format = GST_FORMAT_TIME;
850 segment.start = segment.time;
851 segment.stop = GST_CLOCK_TIME_NONE;
852 segment.position = segment.start;
854 /* rescue duration */
855 segment.duration = avi->segment.duration;
857 /* set up segment and send downstream */
858 gst_segment_copy_into (&segment, &avi->segment);
860 GST_DEBUG_OBJECT (avi, "Pushing newseg %" GST_SEGMENT_FORMAT, &segment);
861 avi->segment_seqnum = gst_event_get_seqnum (event);
862 segment_event = gst_event_new_segment (&segment);
863 gst_event_set_seqnum (segment_event, gst_event_get_seqnum (event));
864 gst_avi_demux_push_event (avi, segment_event);
866 GST_DEBUG_OBJECT (avi, "next chunk expected at %" G_GINT64_FORMAT,
869 /* adjust state for streaming thread accordingly */
871 gst_avi_demux_seek_streams_index (avi, offset, FALSE);
874 gst_avi_demux_seek_streams (avi, offset, FALSE);
877 /* set up streaming thread */
878 g_assert (offset >= boffset);
879 avi->offset = boffset;
880 avi->todrop = offset - boffset;
883 gst_event_unref (event);
888 avi->have_eos = TRUE;
893 if (avi->state != GST_AVI_DEMUX_MOVI) {
894 gst_event_unref (event);
895 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
896 (NULL), ("got eos and didn't receive a complete header object"));
897 } else if (!gst_avi_demux_push_event (avi, event)) {
898 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
899 (NULL), ("got eos but no streams (yet)"));
903 case GST_EVENT_FLUSH_STOP:
907 gst_adapter_clear (avi->adapter);
908 avi->have_eos = FALSE;
909 for (i = 0; i < avi->num_streams; i++) {
910 avi->stream[i].discont = TRUE;
912 /* fall through to default case so that the event gets passed downstream */
915 res = gst_pad_event_default (pad, parent, event);
923 gst_avi_demux_handle_src_event (GstPad * pad, GstObject * parent,
927 GstAviDemux *avi = GST_AVI_DEMUX (parent);
929 GST_DEBUG_OBJECT (avi,
930 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
932 switch (GST_EVENT_TYPE (event)) {
934 if (!avi->streaming) {
935 res = gst_avi_demux_handle_seek (avi, pad, event);
937 res = gst_avi_demux_handle_seek_push (avi, pad, event);
939 gst_event_unref (event);
942 res = gst_pad_event_default (pad, parent, event);
949 /* streaming helper (push) */
952 * gst_avi_demux_peek_chunk_info:
954 * @tag: holder for tag
955 * @size: holder for tag size
957 * Peek next chunk info (tag and size)
959 * Returns: TRUE when one chunk info has been got
962 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
964 const guint8 *data = NULL;
966 if (gst_adapter_available (avi->adapter) < 8)
969 data = gst_adapter_map (avi->adapter, 8);
970 *tag = GST_READ_UINT32_LE (data);
971 *size = GST_READ_UINT32_LE (data + 4);
972 gst_adapter_unmap (avi->adapter);
978 * gst_avi_demux_peek_chunk:
980 * @tag: holder for tag
981 * @size: holder for tag size
983 * Peek enough data for one full chunk
985 * Returns: %TRUE when one chunk has been got
988 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
990 guint32 peek_size = 0;
993 if (!gst_avi_demux_peek_chunk_info (avi, tag, size))
996 /* size 0 -> empty data buffer would surprise most callers,
997 * large size -> do not bother trying to squeeze that into adapter,
998 * so we throw poor man's exception, which can be caught if caller really
999 * wants to handle 0 size chunk */
1000 if (!(*size) || (*size) >= (1 << 30))
1003 peek_size = (*size + 1) & ~1;
1004 available = gst_adapter_available (avi->adapter);
1006 GST_DEBUG_OBJECT (avi,
1007 "Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
1008 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
1010 if (available < (8 + peek_size))
1018 GST_INFO_OBJECT (avi, "Failed to peek");
1023 GST_INFO_OBJECT (avi,
1024 "Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT, *size,
1025 GST_FOURCC_ARGS (*tag));
1026 /* chain should give up */
1027 avi->abort_buffering = TRUE;
1032 GST_INFO_OBJECT (avi, "need more %d < %" G_GUINT32_FORMAT,
1033 available, 8 + peek_size);
1041 * gst_avi_demux_parse_file_header:
1042 * @element: caller element (used for errors/debug).
1043 * @buf: input data to be used for parsing.
1045 * "Open" a RIFF/AVI file. The buffer should be at least 12
1046 * bytes long. Takes ownership of @buf.
1048 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
1049 * Throws an error, caller should error out (fatal).
1052 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
1057 stamp = gst_util_get_timestamp ();
1059 /* riff_parse posts an error */
1060 if (!gst_riff_parse_file_header (element, buf, &doctype))
1063 if (doctype != GST_RIFF_RIFF_AVI)
1066 stamp = gst_util_get_timestamp () - stamp;
1067 GST_DEBUG_OBJECT (element, "header parsing took %" GST_TIME_FORMAT,
1068 GST_TIME_ARGS (stamp));
1075 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
1076 ("File is not an AVI file: 0x%" G_GINT32_MODIFIER "x", doctype));
1082 * Read AVI file tag when streaming
1084 static GstFlowReturn
1085 gst_avi_demux_stream_init_push (GstAviDemux * avi)
1087 if (gst_adapter_available (avi->adapter) >= 12) {
1090 tmp = gst_adapter_take_buffer (avi->adapter, 12);
1092 GST_DEBUG ("Parsing avi header");
1093 if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), tmp)) {
1094 return GST_FLOW_ERROR;
1096 GST_DEBUG ("header ok");
1099 avi->state = GST_AVI_DEMUX_HEADER;
1107 static GstFlowReturn
1108 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
1111 GstBuffer *buf = NULL;
1113 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
1114 if (res != GST_FLOW_OK)
1116 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
1126 GST_DEBUG_OBJECT (avi, "error parsing file header");
1127 return GST_FLOW_ERROR;
1131 /* AVI header handling */
1133 * gst_avi_demux_parse_avih:
1134 * @avi: caller element (used for errors/debug).
1135 * @buf: input data to be used for parsing.
1136 * @avih: pointer to structure (filled in by function) containing
1137 * stream information (such as flags, number of streams, etc.).
1139 * Read 'avih' header. Discards buffer after use.
1141 * Returns: TRUE on success, FALSE otherwise. Throws an error if
1142 * the header is invalid. The caller should error out
1146 gst_avi_demux_parse_avih (GstAviDemux * avi,
1147 GstBuffer * buf, gst_riff_avih ** _avih)
1149 gst_riff_avih *avih;
1155 size = gst_buffer_get_size (buf);
1156 if (size < sizeof (gst_riff_avih))
1157 goto avih_too_small;
1159 avih = g_malloc (size);
1160 gst_buffer_extract (buf, 0, avih, size);
1162 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1163 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
1164 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
1165 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
1166 avih->flags = GUINT32_FROM_LE (avih->flags);
1167 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
1168 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
1169 avih->streams = GUINT32_FROM_LE (avih->streams);
1170 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
1171 avih->width = GUINT32_FROM_LE (avih->width);
1172 avih->height = GUINT32_FROM_LE (avih->height);
1173 avih->scale = GUINT32_FROM_LE (avih->scale);
1174 avih->rate = GUINT32_FROM_LE (avih->rate);
1175 avih->start = GUINT32_FROM_LE (avih->start);
1176 avih->length = GUINT32_FROM_LE (avih->length);
1180 GST_INFO_OBJECT (avi, "avih tag found:");
1181 GST_INFO_OBJECT (avi, " us_frame %u", avih->us_frame);
1182 GST_INFO_OBJECT (avi, " max_bps %u", avih->max_bps);
1183 GST_INFO_OBJECT (avi, " pad_gran %u", avih->pad_gran);
1184 GST_INFO_OBJECT (avi, " flags 0x%08x", avih->flags);
1185 GST_INFO_OBJECT (avi, " tot_frames %u", avih->tot_frames);
1186 GST_INFO_OBJECT (avi, " init_frames %u", avih->init_frames);
1187 GST_INFO_OBJECT (avi, " streams %u", avih->streams);
1188 GST_INFO_OBJECT (avi, " bufsize %u", avih->bufsize);
1189 GST_INFO_OBJECT (avi, " width %u", avih->width);
1190 GST_INFO_OBJECT (avi, " height %u", avih->height);
1191 GST_INFO_OBJECT (avi, " scale %u", avih->scale);
1192 GST_INFO_OBJECT (avi, " rate %u", avih->rate);
1193 GST_INFO_OBJECT (avi, " start %u", avih->start);
1194 GST_INFO_OBJECT (avi, " length %u", avih->length);
1197 gst_buffer_unref (buf);
1199 if (avih->us_frame != 0 && avih->tot_frames != 0)
1201 (guint64) avih->us_frame * (guint64) avih->tot_frames * 1000;
1203 avi->duration = GST_CLOCK_TIME_NONE;
1205 GST_INFO_OBJECT (avi, " header duration %" GST_TIME_FORMAT,
1206 GST_TIME_ARGS (avi->duration));
1213 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No buffer"));
1218 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
1219 ("Too small avih (%" G_GSIZE_FORMAT " available, %d needed)",
1220 size, (int) sizeof (gst_riff_avih)));
1221 gst_buffer_unref (buf);
1227 * gst_avi_demux_parse_superindex:
1228 * @avi: caller element (used for debugging/errors).
1229 * @buf: input data to use for parsing.
1230 * @locations: locations in the file (byte-offsets) that contain
1231 * the actual indexes (see get_avi_demux_parse_subindex()).
1232 * The array ends with GST_BUFFER_OFFSET_NONE.
1234 * Reads superindex (openDML-2 spec stuff) from the provided data.
1236 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
1237 * on error, but they are not fatal.
1240 gst_avi_demux_parse_superindex (GstAviDemux * avi,
1241 GstBuffer * buf, guint64 ** _indexes)
1253 gst_buffer_map (buf, &map, GST_MAP_READ);
1264 /* check type of index. The opendml2 specs state that
1265 * there should be 4 dwords per array entry. Type can be
1266 * either frame or field (and we don't care). */
1267 if (GST_READ_UINT16_LE (data) != 4 ||
1268 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
1269 GST_WARNING_OBJECT (avi,
1270 "Superindex for stream has unexpected "
1271 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1272 GST_READ_UINT16_LE (data), data[2], data[3]);
1273 bpe = GST_READ_UINT16_LE (data) * 4;
1275 num = GST_READ_UINT32_LE (&data[4]);
1277 GST_DEBUG_OBJECT (avi, "got %d indexes", num);
1279 /* this can't work out well ... */
1280 if (num > G_MAXUINT32 >> 1 || bpe < 8) {
1281 goto invalid_params;
1284 indexes = g_new (guint64, num + 1);
1285 for (i = 0; i < num; i++) {
1286 if (size < 24 + bpe * (i + 1))
1288 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
1289 GST_DEBUG_OBJECT (avi, "index %d at %" G_GUINT64_FORMAT, i, indexes[i]);
1291 indexes[i] = GST_BUFFER_OFFSET_NONE;
1292 *_indexes = indexes;
1294 gst_buffer_unmap (buf, &map);
1295 gst_buffer_unref (buf);
1302 GST_ERROR_OBJECT (avi,
1303 "Not enough data to parse superindex (%" G_GSIZE_FORMAT
1304 " available, 24 needed)", size);
1306 gst_buffer_unmap (buf, &map);
1307 gst_buffer_unref (buf);
1313 GST_ERROR_OBJECT (avi, "invalid index parameters (num = %d, bpe = %d)",
1315 gst_buffer_unmap (buf, &map);
1316 gst_buffer_unref (buf);
1321 /* add an entry to the index of a stream. @num should be an estimate of the
1322 * total amount of index entries for all streams and is used to dynamically
1323 * allocate memory for the index entries. */
1324 static inline gboolean
1325 gst_avi_demux_add_index (GstAviDemux * avi, GstAviStream * stream,
1326 guint num, GstAviIndexEntry * entry)
1328 /* ensure index memory */
1329 if (G_UNLIKELY (stream->idx_n >= stream->idx_max)) {
1330 guint idx_max = stream->idx_max;
1331 GstAviIndexEntry *new_idx;
1333 /* we need to make some more room */
1335 /* initial size guess, assume each stream has an equal amount of entries,
1336 * overshoot with at least 8K */
1337 idx_max = (num / avi->num_streams) + (8192 / sizeof (GstAviIndexEntry));
1339 idx_max += 8192 / sizeof (GstAviIndexEntry);
1340 GST_DEBUG_OBJECT (avi, "expanded index from %u to %u",
1341 stream->idx_max, idx_max);
1343 new_idx = g_try_renew (GstAviIndexEntry, stream->index, idx_max);
1344 /* out of memory, if this fails stream->index is untouched. */
1345 if (G_UNLIKELY (!new_idx))
1348 stream->index = new_idx;
1349 stream->idx_max = idx_max;
1352 /* update entry total and stream stats. The entry total can be converted to
1353 * the timestamp of the entry easily. */
1354 if (stream->strh->type == GST_RIFF_FCC_auds) {
1357 if (stream->is_vbr) {
1358 entry->total = stream->total_blocks;
1360 entry->total = stream->total_bytes;
1362 blockalign = stream->strf.auds->blockalign;
1364 stream->total_blocks += DIV_ROUND_UP (entry->size, blockalign);
1366 stream->total_blocks++;
1368 if (stream->is_vbr) {
1369 entry->total = stream->idx_n;
1371 entry->total = stream->total_bytes;
1374 stream->total_bytes += entry->size;
1375 if (ENTRY_IS_KEYFRAME (entry))
1376 stream->n_keyframes++;
1379 GST_LOG_OBJECT (avi,
1380 "Adding stream %u, index entry %d, kf %d, size %u "
1381 ", offset %" G_GUINT64_FORMAT ", total %" G_GUINT64_FORMAT, stream->num,
1382 stream->idx_n, ENTRY_IS_KEYFRAME (entry), entry->size, entry->offset,
1384 stream->index[stream->idx_n++] = *entry;
1389 /* given @entry_n in @stream, calculate info such as timestamps and
1390 * offsets for the entry. */
1392 gst_avi_demux_get_buffer_info (GstAviDemux * avi, GstAviStream * stream,
1393 guint entry_n, GstClockTime * timestamp, GstClockTime * ts_end,
1394 guint64 * offset, guint64 * offset_end)
1396 GstAviIndexEntry *entry;
1398 entry = &stream->index[entry_n];
1400 if (stream->is_vbr) {
1401 /* VBR stream next timestamp */
1402 if (stream->strh->type == GST_RIFF_FCC_auds) {
1405 avi_stream_convert_frames_to_time_unchecked (stream, entry->total);
1408 if (G_LIKELY (entry_n + 1 < stream->idx_n))
1409 size = stream->index[entry_n + 1].total - entry->total;
1410 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1411 entry->total + size);
1416 avi_stream_convert_frames_to_time_unchecked (stream, entry_n);
1418 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1421 } else if (stream->strh->type == GST_RIFF_FCC_auds) {
1422 /* constant rate stream */
1425 avi_stream_convert_bytes_to_time_unchecked (stream, entry->total);
1427 *ts_end = avi_stream_convert_bytes_to_time_unchecked (stream,
1428 entry->total + entry->size);
1430 if (stream->strh->type == GST_RIFF_FCC_vids) {
1431 /* video offsets are the frame number */
1435 *offset_end = entry_n + 1;
1437 /* no offsets for audio */
1445 /* collect and debug stats about the indexes for all streams.
1446 * This method is also responsible for filling in the stream duration
1447 * as measured by the amount of index entries.
1449 * Returns TRUE if the index is not empty, else FALSE */
1451 gst_avi_demux_do_index_stats (GstAviDemux * avi)
1453 guint total_idx = 0;
1455 #ifndef GST_DISABLE_GST_DEBUG
1456 guint total_max = 0;
1459 /* get stream stats now */
1460 for (i = 0; i < avi->num_streams; i++) {
1461 GstAviStream *stream;
1463 if (G_UNLIKELY (!(stream = &avi->stream[i])))
1465 if (G_UNLIKELY (!stream->strh))
1467 if (G_UNLIKELY (!stream->index || stream->idx_n == 0))
1470 /* we interested in the end_ts of the last entry, which is the total
1471 * duration of this stream */
1472 gst_avi_demux_get_buffer_info (avi, stream, stream->idx_n - 1,
1473 NULL, &stream->idx_duration, NULL, NULL);
1475 total_idx += stream->idx_n;
1476 #ifndef GST_DISABLE_GST_DEBUG
1477 total_max += stream->idx_max;
1479 GST_INFO_OBJECT (avi, "Stream %d, dur %" GST_TIME_FORMAT ", %6u entries, "
1480 "%5u keyframes, entry size = %2u, total size = %10u, allocated %10u",
1481 i, GST_TIME_ARGS (stream->idx_duration), stream->idx_n,
1482 stream->n_keyframes, (guint) sizeof (GstAviIndexEntry),
1483 (guint) (stream->idx_n * sizeof (GstAviIndexEntry)),
1484 (guint) (stream->idx_max * sizeof (GstAviIndexEntry)));
1486 /* knowing all that we do, that also includes avg bitrate */
1487 if (!stream->taglist) {
1488 stream->taglist = gst_tag_list_new_empty ();
1490 if (stream->total_bytes && stream->idx_duration)
1491 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_REPLACE,
1493 (guint) gst_util_uint64_scale (stream->total_bytes * 8,
1494 GST_SECOND, stream->idx_duration), NULL);
1496 total_idx *= sizeof (GstAviIndexEntry);
1497 #ifndef GST_DISABLE_GST_DEBUG
1498 total_max *= sizeof (GstAviIndexEntry);
1500 GST_INFO_OBJECT (avi, "%u bytes for index vs %u ideally, %u wasted",
1501 total_max, total_idx, total_max - total_idx);
1503 if (total_idx == 0) {
1504 GST_WARNING_OBJECT (avi, "Index is empty !");
1511 * gst_avi_demux_parse_subindex:
1513 * @buf: input data to use for parsing.
1514 * @stream: stream context.
1515 * @entries_list: a list (returned by the function) containing all the
1516 * indexes parsed in this specific subindex. The first
1517 * entry is also a pointer to allocated memory that needs
1518 * to be free´ed. May be NULL if no supported indexes were
1521 * Reads superindex (openDML-2 spec stuff) from the provided data.
1522 * The buffer should contain a GST_RIFF_TAG_ix?? chunk.
1524 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1525 * throw an error, caller should bail out asap.
1528 gst_avi_demux_parse_subindex (GstAviDemux * avi, GstAviStream * stream,
1540 gst_buffer_map (buf, &map, GST_MAP_READ);
1547 /* We don't support index-data yet */
1549 goto not_implemented;
1551 /* check type of index. The opendml2 specs state that
1552 * there should be 4 dwords per array entry. Type can be
1553 * either frame or field (and we don't care). */
1554 bpe = (data[2] & 0x01) ? 12 : 8;
1555 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1556 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1557 GST_WARNING_OBJECT (avi,
1558 "Superindex for stream %d has unexpected "
1559 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1560 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1561 bpe = GST_READ_UINT16_LE (data) * 4;
1563 num = GST_READ_UINT32_LE (&data[4]);
1564 baseoff = GST_READ_UINT64_LE (&data[12]);
1566 /* If there's nothing, just return ! */
1570 GST_INFO_OBJECT (avi, "Parsing subindex, nr_entries = %6d", num);
1572 for (i = 0; i < num; i++) {
1573 GstAviIndexEntry entry;
1575 if (map.size < 24 + bpe * (i + 1))
1578 /* fill in offset and size. offset contains the keyframe flag in the
1580 entry.offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1581 entry.size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1583 if (stream->strh->type == GST_RIFF_FCC_auds) {
1584 /* all audio frames are keyframes */
1585 ENTRY_SET_KEYFRAME (&entry);
1587 /* else read flags */
1588 entry.flags = (entry.size & 0x80000000) ? 0 : GST_AVI_KEYFRAME;
1590 entry.size &= ~0x80000000;
1593 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
1597 gst_buffer_unmap (buf, &map);
1598 gst_buffer_unref (buf);
1605 GST_ERROR_OBJECT (avi,
1606 "Not enough data to parse subindex (%" G_GSIZE_FORMAT
1607 " available, 24 needed)", map.size);
1608 goto done; /* continue */
1612 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1613 ("Subindex-is-data is not implemented"));
1614 gst_buffer_unmap (buf, &map);
1615 gst_buffer_unref (buf);
1620 GST_DEBUG_OBJECT (avi, "the index is empty");
1621 goto done; /* continue */
1625 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1626 ("Cannot allocate memory for %u*%u=%u bytes",
1627 (guint) sizeof (GstAviIndexEntry), num,
1628 (guint) sizeof (GstAviIndexEntry) * num));
1629 gst_buffer_unmap (buf, &map);
1630 gst_buffer_unref (buf);
1636 * Create and push a flushing seek event upstream
1639 perform_seek_to_offset (GstAviDemux * demux, guint64 offset, guint32 seqnum)
1644 GST_DEBUG_OBJECT (demux, "Seeking to %" G_GUINT64_FORMAT, offset);
1647 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1648 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1649 GST_SEEK_TYPE_NONE, -1);
1650 gst_event_set_seqnum (event, seqnum);
1651 res = gst_pad_push_event (demux->sinkpad, event);
1654 demux->offset = offset;
1659 * Read AVI index when streaming
1662 gst_avi_demux_read_subindexes_push (GstAviDemux * avi)
1664 guint32 tag = 0, size;
1665 GstBuffer *buf = NULL;
1668 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1670 if (avi->odml_subidxs[avi->odml_subidx] != avi->offset)
1673 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1676 /* this is the ODML chunk we expect */
1677 odml_stream = avi->odml_stream;
1679 if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + odml_stream / 10,
1680 '0' + odml_stream % 10)) &&
1681 (tag != GST_MAKE_FOURCC ('0' + odml_stream / 10,
1682 '0' + odml_stream % 10, 'i', 'x'))) {
1683 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1684 GST_FOURCC_ARGS (tag));
1688 avi->offset += 8 + GST_ROUND_UP_2 (size);
1689 /* flush chunk header so we get just the 'size' payload data */
1690 gst_adapter_flush (avi->adapter, 8);
1691 buf = gst_adapter_take_buffer (avi->adapter, size);
1693 if (!gst_avi_demux_parse_subindex (avi, &avi->stream[odml_stream], buf))
1696 /* we parsed the index, go to next subindex */
1699 if (avi->odml_subidxs[avi->odml_subidx] == GST_BUFFER_OFFSET_NONE) {
1700 /* we reached the end of the indexes for this stream, move to the next
1701 * stream to handle the first index */
1703 avi->odml_subidx = 0;
1705 if (avi->odml_stream < avi->num_streams) {
1706 /* there are more indexes */
1707 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
1709 /* we're done, get stream stats now */
1710 avi->have_index = gst_avi_demux_do_index_stats (avi);
1716 /* seek to next index */
1717 return perform_seek_to_offset (avi, avi->odml_subidxs[avi->odml_subidx],
1718 avi->segment_seqnum);
1725 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi)
1731 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1733 for (n = 0; n < avi->num_streams; n++) {
1734 GstAviStream *stream = &avi->stream[n];
1736 if (stream->indexes == NULL)
1739 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1740 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi), avi->sinkpad,
1741 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1743 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1744 '0' + stream->num % 10)) &&
1745 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1746 '0' + stream->num % 10, 'i', 'x'))) {
1747 /* Some ODML files (created by god knows what muxer) have a ##ix format
1748 * instead of the 'official' ix##. They are still valid though. */
1749 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1750 GST_FOURCC_ARGS (tag));
1751 gst_buffer_unref (buf);
1755 if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1759 g_free (stream->indexes);
1760 stream->indexes = NULL;
1762 /* get stream stats now */
1763 avi->have_index = gst_avi_demux_do_index_stats (avi);
1767 * gst_avi_demux_riff_parse_vprp:
1768 * @element: caller element (used for debugging/error).
1769 * @buf: input data to be used for parsing, stripped from header.
1770 * @vprp: a pointer (returned by this function) to a filled-in vprp
1771 * structure. Caller should free it.
1773 * Parses a video stream´s vprp. This function takes ownership of @buf.
1775 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1776 * should be skipped on error, but it is not fatal.
1779 gst_avi_demux_riff_parse_vprp (GstElement * element,
1780 GstBuffer * buf, gst_riff_vprp ** _vprp)
1782 gst_riff_vprp *vprp;
1786 g_return_val_if_fail (buf != NULL, FALSE);
1787 g_return_val_if_fail (_vprp != NULL, FALSE);
1789 size = gst_buffer_get_size (buf);
1791 if (size < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1794 vprp = g_malloc (size);
1795 gst_buffer_extract (buf, 0, vprp, size);
1797 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1798 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1799 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1800 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1801 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1802 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1803 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1804 vprp->width = GUINT32_FROM_LE (vprp->width);
1805 vprp->height = GUINT32_FROM_LE (vprp->height);
1806 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1810 /* calculate fields based on size */
1811 k = (size - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) / vprp->fields;
1812 if (vprp->fields > k) {
1813 GST_WARNING_OBJECT (element,
1814 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1817 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1818 GST_WARNING_OBJECT (element,
1819 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1820 GST_RIFF_VPRP_VIDEO_FIELDS);
1821 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1823 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1824 for (k = 0; k < vprp->fields; k++) {
1825 gst_riff_vprp_video_field_desc *fd;
1827 fd = &vprp->field_info[k];
1828 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1829 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1830 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1831 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1832 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1833 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1834 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1835 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1840 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1841 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1842 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1843 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1844 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1845 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1846 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1847 vprp->aspect & 0xffff);
1848 GST_INFO_OBJECT (element, " width %d", vprp->width);
1849 GST_INFO_OBJECT (element, " height %d", vprp->height);
1850 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1851 for (k = 0; k < vprp->fields; k++) {
1852 gst_riff_vprp_video_field_desc *fd;
1854 fd = &(vprp->field_info[k]);
1855 GST_INFO_OBJECT (element, " field %u description:", k);
1856 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1857 fd->compressed_bm_height);
1858 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1859 fd->compressed_bm_width);
1860 GST_INFO_OBJECT (element, " valid_bm_height %d",
1861 fd->valid_bm_height);
1862 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
1863 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
1864 fd->valid_bm_x_offset);
1865 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
1866 fd->valid_bm_y_offset);
1867 GST_INFO_OBJECT (element, " video_x_t_offset %d",
1868 fd->video_x_t_offset);
1869 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
1872 gst_buffer_unref (buf);
1881 GST_ERROR_OBJECT (element,
1882 "Too small vprp (%" G_GSIZE_FORMAT " available, at least %d needed)",
1883 size, (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
1884 gst_buffer_unref (buf);
1890 gst_avi_demux_expose_streams (GstAviDemux * avi, gboolean force)
1894 GST_DEBUG_OBJECT (avi, "force : %d", force);
1896 for (i = 0; i < avi->num_streams; i++) {
1897 GstAviStream *stream = &avi->stream[i];
1899 if (force || stream->idx_n != 0) {
1900 GST_LOG_OBJECT (avi, "Adding pad %s", GST_PAD_NAME (stream->pad));
1901 gst_element_add_pad ((GstElement *) avi, stream->pad);
1902 gst_flow_combiner_add_pad (avi->flowcombiner, stream->pad);
1905 if (avi->element_index)
1906 gst_index_get_writer_id (avi->element_index,
1907 GST_OBJECT_CAST (stream->pad), &stream->index_id);
1910 stream->exposed = TRUE;
1911 if (avi->main_stream == -1)
1912 avi->main_stream = i;
1914 GST_WARNING_OBJECT (avi, "Stream #%d doesn't have any entry, removing it",
1916 gst_avi_demux_reset_stream (avi, stream);
1921 /* buf contains LIST chunk data, and will be padded to even size,
1922 * since some buggy files do not account for the padding of chunks
1923 * within a LIST in the size of the LIST */
1925 gst_avi_demux_roundup_list (GstAviDemux * avi, GstBuffer ** buf)
1929 size = gst_buffer_get_size (*buf);
1931 if (G_UNLIKELY (size & 1)) {
1935 GST_DEBUG_OBJECT (avi, "rounding up dubious list size %" G_GSIZE_FORMAT,
1937 obuf = gst_buffer_new_and_alloc (size + 1);
1939 gst_buffer_map (obuf, &map, GST_MAP_WRITE);
1940 gst_buffer_extract (*buf, 0, map.data, size);
1941 /* assume 0 padding, at least makes outcome deterministic */
1943 gst_buffer_unmap (obuf, &map);
1944 gst_buffer_replace (buf, obuf);
1949 gst_avi_demux_check_caps (GstAviDemux * avi, GstAviStream * stream,
1956 caps = gst_caps_make_writable (caps);
1958 s = gst_caps_get_structure (caps, 0);
1959 if (gst_structure_has_name (s, "video/x-raw")) {
1960 stream->is_raw = TRUE;
1961 stream->alignment = 32;
1962 if (!gst_structure_has_field (s, "pixel-aspect-ratio"))
1963 gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1965 if (gst_structure_has_field_typed (s, "palette_data", GST_TYPE_BUFFER)) {
1966 gst_structure_get (s, "palette_data", GST_TYPE_BUFFER,
1967 &stream->rgb8_palette, NULL);
1968 gst_structure_remove_field (s, "palette_data");
1971 } else if (!gst_structure_has_name (s, "video/x-h264")) {
1975 GST_DEBUG_OBJECT (avi, "checking caps %" GST_PTR_FORMAT, caps);
1977 /* some muxers put invalid bytestream stuff in h264 extra data */
1978 val = gst_structure_get_value (s, "codec_data");
1979 if (val && (buf = gst_value_get_buffer (val))) {
1984 gst_buffer_map (buf, &map, GST_MAP_READ);
1988 guint32 h = GST_READ_UINT32_BE (data);
1989 gst_buffer_unmap (buf, &map);
1991 /* can hardly be valid AVC codec data */
1992 GST_DEBUG_OBJECT (avi,
1993 "discarding invalid codec_data containing byte-stream");
1994 /* so do not pretend to downstream that it is packetized avc */
1995 gst_structure_remove_field (s, "codec_data");
1996 /* ... but rather properly parsed bytestream */
1997 gst_structure_set (s, "stream-format", G_TYPE_STRING, "byte-stream",
1998 "alignment", G_TYPE_STRING, "au", NULL);
2001 gst_buffer_unmap (buf, &map);
2009 * gst_avi_demux_parse_stream:
2010 * @avi: calling element (used for debugging/errors).
2011 * @buf: input buffer used to parse the stream.
2013 * Parses all subchunks in a strl chunk (which defines a single
2014 * stream). Discards the buffer after use. This function will
2015 * increment the stream counter internally.
2017 * Returns: whether the stream was identified successfully.
2018 * Errors are not fatal. It does indicate the stream
2022 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
2024 GstAviStream *stream;
2025 GstElementClass *klass;
2026 GstPadTemplate *templ;
2027 GstBuffer *sub = NULL;
2030 gchar *codec_name = NULL, *padname = NULL;
2031 const gchar *tag_name;
2032 GstCaps *caps = NULL;
2034 GstElement *element;
2035 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
2036 gst_riff_vprp *vprp = NULL;
2040 gboolean sparse = FALSE;
2042 element = GST_ELEMENT_CAST (avi);
2044 GST_DEBUG_OBJECT (avi, "Parsing stream");
2046 gst_avi_demux_roundup_list (avi, &buf);
2048 if (avi->num_streams >= GST_AVI_DEMUX_MAX_STREAMS) {
2049 GST_WARNING_OBJECT (avi,
2050 "maximum no of streams (%d) exceeded, ignoring stream",
2051 GST_AVI_DEMUX_MAX_STREAMS);
2052 gst_buffer_unref (buf);
2053 /* not a fatal error, let's say */
2057 stream = &avi->stream[avi->num_streams];
2059 /* initial settings */
2060 stream->idx_duration = GST_CLOCK_TIME_NONE;
2061 stream->hdr_duration = GST_CLOCK_TIME_NONE;
2062 stream->duration = GST_CLOCK_TIME_NONE;
2064 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
2065 /* sub can be NULL if the chunk is empty */
2067 GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
2068 GST_FOURCC_ARGS (tag));
2072 case GST_RIFF_TAG_strh:
2074 gst_riff_strh *strh;
2077 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
2080 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
2081 /* ownership given away */
2083 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
2087 strh = stream->strh;
2088 /* sanity check; stream header frame rate matches global header
2090 if (stream->strh->type == GST_RIFF_FCC_vids) {
2092 GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
2094 s_dur = gst_util_uint64_scale (GST_SECOND, strh->scale, strh->rate);
2095 GST_DEBUG_OBJECT (avi, "verifying stream framerate %d/%d, "
2096 "frame duration = %d ms", strh->rate, strh->scale,
2097 (gint) (s_dur / GST_MSECOND));
2098 if (h_dur > (10 * GST_MSECOND) && (s_dur > 10 * h_dur)) {
2099 strh->rate = GST_SECOND / GST_USECOND;
2100 strh->scale = h_dur / GST_USECOND;
2101 GST_DEBUG_OBJECT (avi, "correcting stream framerate to %d/%d",
2102 strh->rate, strh->scale);
2105 /* determine duration as indicated by header */
2106 stream->hdr_duration = gst_util_uint64_scale ((guint64) strh->length *
2107 strh->scale, GST_SECOND, (guint64) strh->rate);
2108 GST_INFO ("Stream duration according to header: %" GST_TIME_FORMAT,
2109 GST_TIME_ARGS (stream->hdr_duration));
2110 if (stream->hdr_duration == 0)
2111 stream->hdr_duration = GST_CLOCK_TIME_NONE;
2116 case GST_RIFF_TAG_strf:
2118 gboolean res = FALSE;
2121 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
2125 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
2128 switch (stream->strh->type) {
2129 case GST_RIFF_FCC_vids:
2130 stream->is_vbr = TRUE;
2131 res = gst_riff_parse_strf_vids (element, sub,
2132 &stream->strf.vids, &stream->extradata);
2134 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
2136 case GST_RIFF_FCC_auds:
2138 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
2139 &stream->extradata);
2143 stream->is_vbr = (stream->strh->samplesize == 0)
2144 && stream->strh->scale > 1
2145 && stream->strf.auds->blockalign != 1;
2146 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
2147 stream->is_vbr, res);
2148 /* we need these or we have no way to come up with timestamps */
2149 if ((!stream->is_vbr && !stream->strf.auds->av_bps) ||
2150 (stream->is_vbr && (!stream->strh->scale ||
2151 !stream->strh->rate))) {
2152 GST_WARNING_OBJECT (element,
2153 "invalid audio header, ignoring stream");
2156 /* some more sanity checks */
2157 if (stream->is_vbr) {
2158 if (stream->strf.auds->blockalign <= 4) {
2159 /* that would mean (too) many frames per chunk,
2160 * so not likely set as expected */
2161 GST_DEBUG_OBJECT (element,
2162 "suspicious blockalign %d for VBR audio; "
2163 "overriding to 1 frame per chunk",
2164 stream->strf.auds->blockalign);
2165 /* this should top any likely value */
2166 stream->strf.auds->blockalign = (1 << 12);
2170 case GST_RIFF_FCC_iavs:
2171 stream->is_vbr = TRUE;
2172 res = gst_riff_parse_strf_iavs (element, sub,
2173 &stream->strf.iavs, &stream->extradata);
2175 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
2177 case GST_RIFF_FCC_txts:
2178 /* nothing to parse here */
2179 stream->is_vbr = (stream->strh->samplesize == 0)
2180 && (stream->strh->scale > 1);
2184 GST_ERROR_OBJECT (avi,
2185 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
2186 GST_FOURCC_ARGS (stream->strh->type));
2190 gst_buffer_unref (sub);
2198 case GST_RIFF_TAG_vprp:
2201 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
2205 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
2209 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
2213 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
2214 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
2215 /* not considered fatal */
2223 case GST_RIFF_TAG_strd:
2224 if (stream->initdata)
2225 gst_buffer_unref (stream->initdata);
2226 stream->initdata = sub;
2228 gst_avi_demux_parse_strd (avi, sub);
2232 case GST_RIFF_TAG_strn:
2234 gchar *stream_name = NULL;
2236 gst_buffer_map (sub, &map, GST_MAP_READ);
2238 if (avi->globaltags == NULL)
2239 avi->globaltags = gst_tag_list_new_empty ();
2240 parse_tag_value (avi, avi->globaltags, GST_TAG_TITLE,
2241 map.data, map.size);
2243 if (gst_tag_list_get_string (avi->globaltags, GST_TAG_TITLE,
2245 GST_DEBUG_OBJECT (avi, "stream name: %s", stream_name);
2246 g_free (stream->name);
2247 stream->name = stream_name;
2250 gst_buffer_unmap (sub, &map);
2251 gst_buffer_unref (sub);
2256 gst_avi_demux_parse_idit (avi, sub);
2259 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
2260 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
2261 '0' + avi->num_streams % 10)) {
2262 g_free (stream->indexes);
2263 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
2264 stream->superindex = TRUE;
2268 GST_WARNING_OBJECT (avi,
2269 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
2270 GST_FOURCC_ARGS (tag));
2271 /* Only get buffer for debugging if the memdump is needed */
2272 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= 9) {
2275 gst_buffer_map (sub, &map, GST_MAP_READ);
2276 GST_MEMDUMP_OBJECT (avi, "Unknown stream header tag", map.data,
2278 gst_buffer_unmap (sub, &map);
2281 case GST_RIFF_TAG_JUNQ:
2282 case GST_RIFF_TAG_JUNK:
2286 gst_buffer_unref (sub);
2292 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
2297 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
2301 /* get class to figure out the template */
2302 klass = GST_ELEMENT_GET_CLASS (avi);
2304 /* we now have all info, let´s set up a pad and a caps and be done */
2305 /* create stream name + pad */
2306 switch (stream->strh->type) {
2307 case GST_RIFF_FCC_vids:{
2310 fourcc = (stream->strf.vids->compression) ?
2311 stream->strf.vids->compression : stream->strh->fcc_handler;
2312 caps = gst_riff_create_video_caps (fourcc, stream->strh,
2313 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
2315 /* DXSB is XSUB, and it is placed inside a vids */
2316 if (!caps || (fourcc != GST_MAKE_FOURCC ('D', 'X', 'S', 'B') &&
2317 fourcc != GST_MAKE_FOURCC ('D', 'X', 'S', 'A'))) {
2318 padname = g_strdup_printf ("video_%u", avi->num_v_streams);
2319 templ = gst_element_class_get_pad_template (klass, "video_%u");
2321 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2322 G_TYPE_INT, fourcc, NULL);
2323 } else if (got_vprp && vprp) {
2324 guint32 aspect_n, aspect_d;
2327 aspect_n = vprp->aspect >> 16;
2328 aspect_d = vprp->aspect & 0xffff;
2329 /* calculate the pixel aspect ratio using w/h and aspect ratio */
2330 n = aspect_n * stream->strf.vids->height;
2331 d = aspect_d * stream->strf.vids->width;
2333 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
2336 caps = gst_avi_demux_check_caps (avi, stream, caps);
2337 tag_name = GST_TAG_VIDEO_CODEC;
2338 avi->num_v_streams++;
2340 padname = g_strdup_printf ("subpicture_%u", avi->num_sp_streams);
2341 templ = gst_element_class_get_pad_template (klass, "subpicture_%u");
2343 avi->num_sp_streams++;
2348 case GST_RIFF_FCC_auds:{
2349 /* FIXME: Do something with the channel reorder map */
2350 padname = g_strdup_printf ("audio_%u", avi->num_a_streams);
2351 templ = gst_element_class_get_pad_template (klass, "audio_%u");
2352 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
2353 stream->strh, stream->strf.auds, stream->extradata,
2354 stream->initdata, &codec_name, NULL);
2356 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
2357 G_TYPE_INT, stream->strf.auds->format, NULL);
2359 tag_name = GST_TAG_AUDIO_CODEC;
2360 avi->num_a_streams++;
2363 case GST_RIFF_FCC_iavs:{
2364 guint32 fourcc = stream->strh->fcc_handler;
2366 padname = g_strdup_printf ("video_%u", avi->num_v_streams);
2367 templ = gst_element_class_get_pad_template (klass, "video_%u");
2368 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
2369 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
2371 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2372 G_TYPE_INT, fourcc, NULL);
2374 tag_name = GST_TAG_VIDEO_CODEC;
2375 avi->num_v_streams++;
2378 case GST_RIFF_FCC_txts:{
2379 padname = g_strdup_printf ("subtitle_%u", avi->num_t_streams);
2380 templ = gst_element_class_get_pad_template (klass, "subtitle_%u");
2381 caps = gst_caps_new_empty_simple ("application/x-subtitle-avi");
2383 avi->num_t_streams++;
2388 g_return_val_if_reached (FALSE);
2391 /* no caps means no stream */
2393 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
2397 GST_DEBUG_OBJECT (element, "codec-name=%s", codec_name ? codec_name : "NULL");
2398 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
2400 /* set proper settings and add it */
2402 gst_object_unref (stream->pad);
2403 pad = stream->pad = gst_pad_new_from_template (templ, padname);
2406 gst_pad_use_fixed_caps (pad);
2408 gst_pad_set_formats_function (pad,
2409 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
2410 gst_pad_set_event_mask_function (pad,
2411 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
2413 gst_pad_set_event_function (pad,
2414 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
2415 gst_pad_set_query_function (pad,
2416 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
2418 gst_pad_set_convert_function (pad,
2419 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
2422 stream->num = avi->num_streams;
2424 stream->start_entry = 0;
2425 stream->step_entry = 0;
2426 stream->stop_entry = 0;
2428 stream->current_entry = -1;
2429 stream->current_total = 0;
2431 stream->discont = TRUE;
2433 stream->total_bytes = 0;
2434 stream->total_blocks = 0;
2435 stream->n_keyframes = 0;
2438 stream->idx_max = 0;
2440 gst_pad_set_element_private (pad, stream);
2443 gst_pad_set_active (pad, TRUE);
2445 gst_pad_create_stream_id_printf (pad, GST_ELEMENT_CAST (avi), "%03u",
2448 event = gst_pad_get_sticky_event (avi->sinkpad, GST_EVENT_STREAM_START, 0);
2450 if (gst_event_parse_group_id (event, &avi->group_id))
2451 avi->have_group_id = TRUE;
2453 avi->have_group_id = FALSE;
2454 gst_event_unref (event);
2455 } else if (!avi->have_group_id) {
2456 avi->have_group_id = TRUE;
2457 avi->group_id = gst_util_group_id_next ();
2460 event = gst_event_new_stream_start (stream_id);
2461 if (avi->have_group_id)
2462 gst_event_set_group_id (event, avi->group_id);
2464 gst_event_set_stream_flags (event, GST_STREAM_FLAG_SPARSE);
2466 gst_pad_push_event (pad, event);
2468 gst_pad_set_caps (pad, caps);
2469 gst_caps_unref (caps);
2472 if (codec_name && tag_name) {
2473 if (!stream->taglist)
2474 stream->taglist = gst_tag_list_new_empty ();
2476 avi->got_tags = TRUE;
2478 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
2483 g_free (codec_name);
2484 gst_buffer_unref (buf);
2491 /* unref any mem that may be in use */
2493 gst_buffer_unref (buf);
2495 gst_buffer_unref (sub);
2497 g_free (codec_name);
2498 gst_avi_demux_reset_stream (avi, stream);
2505 * gst_avi_demux_parse_odml:
2506 * @avi: calling element (used for debug/error).
2507 * @buf: input buffer to be used for parsing.
2509 * Read an openDML-2.0 extension header. Fills in the frame number
2510 * in the avi demuxer object when reading succeeds.
2513 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
2517 GstBuffer *sub = NULL;
2519 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
2522 case GST_RIFF_TAG_dmlh:{
2523 gst_riff_dmlh dmlh, *_dmlh;
2526 /* sub == NULL is possible and means an empty buffer */
2530 gst_buffer_map (sub, &map, GST_MAP_READ);
2533 if (map.size < sizeof (gst_riff_dmlh)) {
2534 GST_ERROR_OBJECT (avi,
2535 "DMLH entry is too small (%" G_GSIZE_FORMAT " bytes, %d needed)",
2536 map.size, (int) sizeof (gst_riff_dmlh));
2537 gst_buffer_unmap (sub, &map);
2540 _dmlh = (gst_riff_dmlh *) map.data;
2541 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
2542 gst_buffer_unmap (sub, &map);
2544 GST_INFO_OBJECT (avi, "dmlh tag found: totalframes: %u",
2547 avi->avih->tot_frames = dmlh.totalframes;
2552 GST_WARNING_OBJECT (avi,
2553 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
2554 GST_FOURCC_ARGS (tag));
2555 /* Only get buffer for debugging if the memdump is needed */
2556 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= 9) {
2559 gst_buffer_map (sub, &map, GST_MAP_READ);
2560 GST_MEMDUMP_OBJECT (avi, "Unknown ODML tag", map.data, map.size);
2561 gst_buffer_unmap (sub, &map);
2564 case GST_RIFF_TAG_JUNQ:
2565 case GST_RIFF_TAG_JUNK:
2567 /* skip and move to next chunk */
2569 gst_buffer_unref (sub);
2576 gst_buffer_unref (buf);
2581 gst_avi_demux_index_last (GstAviDemux * avi, GstAviStream * stream)
2583 return stream->idx_n;
2586 /* find a previous entry in the index with the given flags */
2588 gst_avi_demux_index_prev (GstAviDemux * avi, GstAviStream * stream,
2589 guint last, gboolean keyframe)
2591 GstAviIndexEntry *entry;
2594 for (i = last; i > 0; i--) {
2595 entry = &stream->index[i - 1];
2596 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2604 gst_avi_demux_index_next (GstAviDemux * avi, GstAviStream * stream,
2605 guint last, gboolean keyframe)
2607 GstAviIndexEntry *entry;
2610 for (i = last + 1; i < stream->idx_n; i++) {
2611 entry = &stream->index[i];
2612 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2616 return stream->idx_n - 1;
2620 gst_avi_demux_index_entry_search (GstAviIndexEntry * entry, guint64 * total)
2622 if (entry->total < *total)
2624 else if (entry->total > *total)
2630 * gst_avi_demux_index_for_time:
2632 * @stream: the stream
2633 * @time: a time position
2634 * @next: whether to look for entry before or after @time
2636 * Finds the index entry which time is less/more or equal than the requested time.
2637 * Try to avoid binary search when we can convert the time to an index
2638 * position directly (for example for video frames with a fixed duration).
2640 * Returns: the found position in the index.
2643 gst_avi_demux_index_for_time (GstAviDemux * avi,
2644 GstAviStream * stream, guint64 time, gboolean next)
2649 GST_LOG_OBJECT (avi, "search time:%" GST_TIME_FORMAT, GST_TIME_ARGS (time));
2651 /* easy (and common) cases */
2652 if (time == 0 || stream->idx_n == 0)
2654 if (time >= stream->idx_duration)
2655 return stream->idx_n - 1;
2657 /* figure out where we need to go. For that we convert the time to an
2658 * index entry or we convert it to a total and then do a binary search. */
2659 if (stream->is_vbr) {
2660 /* VBR stream next timestamp */
2661 if (stream->strh->type == GST_RIFF_FCC_auds) {
2662 total = avi_stream_convert_time_to_frames_unchecked (stream, time);
2664 index = avi_stream_convert_time_to_frames_unchecked (stream, time);
2665 /* this entry typically undershoots the target time,
2666 * so check a bit more if next needed */
2667 if (next && index != -1) {
2668 GstClockTime itime =
2669 avi_stream_convert_frames_to_time_unchecked (stream, index);
2670 if (itime < time && index + 1 < stream->idx_n)
2674 } else if (stream->strh->type == GST_RIFF_FCC_auds) {
2675 /* constant rate stream */
2676 total = avi_stream_convert_time_to_bytes_unchecked (stream, time);
2681 GstAviIndexEntry *entry;
2683 /* no index, find index with binary search on total */
2684 GST_LOG_OBJECT (avi, "binary search for entry with total %"
2685 G_GUINT64_FORMAT, total);
2687 entry = gst_util_array_binary_search (stream->index,
2688 stream->idx_n, sizeof (GstAviIndexEntry),
2689 (GCompareDataFunc) gst_avi_demux_index_entry_search,
2690 next ? GST_SEARCH_MODE_AFTER : GST_SEARCH_MODE_BEFORE, &total, NULL);
2692 if (entry == NULL) {
2693 GST_LOG_OBJECT (avi, "not found, assume index 0");
2696 index = entry - stream->index;
2697 GST_LOG_OBJECT (avi, "found at %u", index);
2700 GST_LOG_OBJECT (avi, "converted time to index %u", index);
2706 static inline GstAviStream *
2707 gst_avi_demux_stream_for_id (GstAviDemux * avi, guint32 id)
2710 GstAviStream *stream;
2712 /* get the stream for this entry */
2713 stream_nr = CHUNKID_TO_STREAMNR (id);
2714 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2715 GST_WARNING_OBJECT (avi,
2716 "invalid stream nr %d (0x%08x, %" GST_FOURCC_FORMAT ")", stream_nr, id,
2717 GST_FOURCC_ARGS (id));
2720 stream = &avi->stream[stream_nr];
2721 if (G_UNLIKELY (!stream->strh)) {
2722 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2729 * gst_avi_demux_parse_index:
2730 * @avi: calling element (used for debugging/errors).
2731 * @buf: buffer containing the full index.
2733 * Read index entries from the provided buffer.
2734 * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
2737 gst_avi_demux_parse_index (GstAviDemux * avi, GstBuffer * buf)
2741 gst_riff_index_entry *index;
2743 GstAviStream *stream;
2744 GstAviIndexEntry entry;
2750 gst_buffer_map (buf, &map, GST_MAP_READ);
2752 stamp = gst_util_get_timestamp ();
2754 /* see how many items in the index */
2755 num = map.size / sizeof (gst_riff_index_entry);
2759 GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2761 index = (gst_riff_index_entry *) map.data;
2763 /* figure out if the index is 0 based or relative to the MOVI start */
2764 entry.offset = GST_READ_UINT32_LE (&index[0].offset);
2765 if (entry.offset < avi->offset) {
2766 avi->index_offset = avi->offset + 8;
2767 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2769 avi->index_offset = 0;
2770 GST_DEBUG ("index is 0 based");
2773 for (i = 0, n = 0; i < num; i++) {
2774 id = GST_READ_UINT32_LE (&index[i].id);
2775 entry.offset = GST_READ_UINT32_LE (&index[i].offset);
2777 /* some sanity checks */
2778 if (G_UNLIKELY (id == GST_RIFF_rec || id == 0 ||
2779 (entry.offset == 0 && n > 0)))
2782 /* get the stream for this entry */
2783 stream = gst_avi_demux_stream_for_id (avi, id);
2784 if (G_UNLIKELY (!stream))
2787 /* handle offset and size */
2788 entry.offset += avi->index_offset + 8;
2789 entry.size = GST_READ_UINT32_LE (&index[i].size);
2792 if (stream->strh->type == GST_RIFF_FCC_auds) {
2793 /* all audio frames are keyframes */
2794 ENTRY_SET_KEYFRAME (&entry);
2795 } else if (stream->strh->type == GST_RIFF_FCC_vids &&
2796 stream->strf.vids->compression == GST_RIFF_DXSB) {
2797 /* all xsub frames are keyframes */
2798 ENTRY_SET_KEYFRAME (&entry);
2801 /* else read flags */
2802 flags = GST_READ_UINT32_LE (&index[i].flags);
2803 if (flags & GST_RIFF_IF_KEYFRAME) {
2804 ENTRY_SET_KEYFRAME (&entry);
2806 ENTRY_UNSET_KEYFRAME (&entry);
2811 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2816 gst_buffer_unmap (buf, &map);
2817 gst_buffer_unref (buf);
2819 /* get stream stats now */
2820 avi->have_index = gst_avi_demux_do_index_stats (avi);
2822 stamp = gst_util_get_timestamp () - stamp;
2823 GST_DEBUG_OBJECT (avi, "index parsing took %" GST_TIME_FORMAT,
2824 GST_TIME_ARGS (stamp));
2831 GST_DEBUG_OBJECT (avi, "empty index");
2832 gst_buffer_unmap (buf, &map);
2833 gst_buffer_unref (buf);
2838 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2839 ("Cannot allocate memory for %u*%u=%u bytes",
2840 (guint) sizeof (GstAviIndexEntry), num,
2841 (guint) sizeof (GstAviIndexEntry) * num));
2842 gst_buffer_unmap (buf, &map);
2843 gst_buffer_unref (buf);
2849 * gst_avi_demux_stream_index:
2850 * @avi: avi demuxer object.
2852 * Seeks to index and reads it.
2855 gst_avi_demux_stream_index (GstAviDemux * avi)
2858 guint64 offset = avi->offset;
2859 GstBuffer *buf = NULL;
2864 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2866 /* get chunk information */
2867 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2868 if (res != GST_FLOW_OK)
2871 gst_buffer_map (buf, &map, GST_MAP_READ);
2875 /* check tag first before blindly trying to read 'size' bytes */
2876 tag = GST_READ_UINT32_LE (map.data);
2877 size = GST_READ_UINT32_LE (map.data + 4);
2878 if (tag == GST_RIFF_TAG_LIST) {
2879 /* this is the movi tag */
2880 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2881 (8 + GST_ROUND_UP_2 (size)));
2882 offset += 8 + GST_ROUND_UP_2 (size);
2883 gst_buffer_unmap (buf, &map);
2884 gst_buffer_unref (buf);
2887 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2888 if (res != GST_FLOW_OK)
2891 gst_buffer_map (buf, &map, GST_MAP_READ);
2895 tag = GST_READ_UINT32_LE (map.data);
2896 size = GST_READ_UINT32_LE (map.data + 4);
2898 gst_buffer_unmap (buf, &map);
2899 gst_buffer_unref (buf);
2901 if (tag != GST_RIFF_TAG_idx1)
2906 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2908 /* read chunk, advance offset */
2909 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2910 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2913 GST_DEBUG ("will parse index chunk size %" G_GSIZE_FORMAT " for tag %"
2914 GST_FOURCC_FORMAT, gst_buffer_get_size (buf), GST_FOURCC_ARGS (tag));
2916 gst_avi_demux_parse_index (avi, buf);
2918 #ifndef GST_DISABLE_GST_DEBUG
2919 /* debug our indexes */
2922 GstAviStream *stream;
2924 for (i = 0; i < avi->num_streams; i++) {
2925 stream = &avi->stream[i];
2926 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2927 i, stream->idx_n, stream->total_bytes);
2936 GST_DEBUG_OBJECT (avi,
2937 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
2942 GST_DEBUG_OBJECT (avi, "Buffer is too small");
2943 gst_buffer_unmap (buf, &map);
2944 gst_buffer_unref (buf);
2949 GST_WARNING_OBJECT (avi,
2950 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
2951 GST_FOURCC_ARGS (tag));
2956 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
2962 * gst_avi_demux_stream_index_push:
2963 * @avi: avi demuxer object.
2968 gst_avi_demux_stream_index_push (GstAviDemux * avi)
2970 guint64 offset = avi->idx1_offset;
2975 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2977 /* get chunk information */
2978 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
2981 /* check tag first before blindly trying to read 'size' bytes */
2982 if (tag == GST_RIFF_TAG_LIST) {
2983 /* this is the movi tag */
2984 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2985 (8 + GST_ROUND_UP_2 (size)));
2986 avi->idx1_offset = offset + 8 + GST_ROUND_UP_2 (size);
2987 /* issue seek to allow chain function to handle it and return! */
2988 perform_seek_to_offset (avi, avi->idx1_offset, avi->segment_seqnum);
2992 if (tag != GST_RIFF_TAG_idx1)
2995 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2997 /* flush chunk header */
2998 gst_adapter_flush (avi->adapter, 8);
2999 /* read chunk payload */
3000 buf = gst_adapter_take_buffer (avi->adapter, size);
3003 /* advance offset */
3004 offset += 8 + GST_ROUND_UP_2 (size);
3006 GST_DEBUG ("will parse index chunk size %" G_GSIZE_FORMAT " for tag %"
3007 GST_FOURCC_FORMAT, gst_buffer_get_size (buf), GST_FOURCC_ARGS (tag));
3009 avi->offset = avi->first_movi_offset;
3010 gst_avi_demux_parse_index (avi, buf);
3012 #ifndef GST_DISABLE_GST_DEBUG
3013 /* debug our indexes */
3016 GstAviStream *stream;
3018 for (i = 0; i < avi->num_streams; i++) {
3019 stream = &avi->stream[i];
3020 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
3021 i, stream->idx_n, stream->total_bytes);
3030 GST_DEBUG_OBJECT (avi,
3031 "taking data from adapter failed: pos=%" G_GUINT64_FORMAT " size=%u",
3037 GST_WARNING_OBJECT (avi,
3038 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
3039 GST_FOURCC_ARGS (tag));
3045 * gst_avi_demux_peek_tag:
3047 * Returns the tag and size of the next chunk
3049 static GstFlowReturn
3050 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
3054 GstBuffer *buf = NULL;
3057 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
3058 if (res != GST_FLOW_OK)
3061 gst_buffer_map (buf, &map, GST_MAP_READ);
3065 *tag = GST_READ_UINT32_LE (map.data);
3066 *size = GST_READ_UINT32_LE (map.data + 4);
3068 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
3069 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
3070 *size, offset + 8, offset + 8 + (gint64) * size);
3073 gst_buffer_unmap (buf, &map);
3074 gst_buffer_unref (buf);
3081 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
3086 GST_DEBUG_OBJECT (avi, "got %" G_GSIZE_FORMAT " bytes which is <> 8 bytes",
3088 res = GST_FLOW_ERROR;
3094 * gst_avi_demux_next_data_buffer:
3096 * Returns the offset and size of the next buffer
3097 * Position is the position of the buffer (after tag and size)
3099 static GstFlowReturn
3100 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
3101 guint32 * tag, guint * size)
3103 guint64 off = *offset;
3108 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
3109 if (res != GST_FLOW_OK)
3111 if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
3112 off += 8 + 4; /* skip tag + size + subtag */
3124 * gst_avi_demux_stream_scan:
3125 * @avi: calling element (used for debugging/errors).
3127 * Scan the file for all chunks to "create" a new index.
3131 gst_avi_demux_stream_scan (GstAviDemux * avi)
3134 GstAviStream *stream;
3142 * - implement non-seekable source support.
3144 GST_DEBUG_OBJECT (avi, "Creating index");
3146 /* get the size of the file */
3147 if (!gst_pad_peer_query_duration (avi->sinkpad, GST_FORMAT_BYTES, &tmplength))
3151 /* guess the total amount of entries we expect */
3155 GstAviIndexEntry entry;
3158 /* start reading data buffers to find the id and offset */
3159 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
3160 if (G_UNLIKELY (res != GST_FLOW_OK))
3164 stream = gst_avi_demux_stream_for_id (avi, tag);
3165 if (G_UNLIKELY (!stream))
3168 /* we can't figure out the keyframes, assume they all are */
3169 entry.flags = GST_AVI_KEYFRAME;
3173 /* and add to the index of this stream */
3174 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
3178 /* update position */
3179 pos += GST_ROUND_UP_2 (size);
3180 if (G_UNLIKELY (pos > length)) {
3181 GST_WARNING_OBJECT (avi,
3182 "Stopping index lookup since we are further than EOF");
3188 avi->have_index = gst_avi_demux_do_index_stats (avi);
3195 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
3196 ("Cannot allocate memory for %u*%u=%u bytes",
3197 (guint) sizeof (GstAviIndexEntry), num,
3198 (guint) sizeof (GstAviIndexEntry) * num));
3204 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
3208 GstAviStream *stream;
3210 total = GST_CLOCK_TIME_NONE;
3212 /* all streams start at a timestamp 0 */
3213 for (i = 0; i < avi->num_streams; i++) {
3214 GstClockTime duration, hduration;
3215 gst_riff_strh *strh;
3217 stream = &avi->stream[i];
3218 if (G_UNLIKELY (!stream || !stream->idx_n || !(strh = stream->strh)))
3221 /* get header duration for the stream */
3222 hduration = stream->hdr_duration;
3223 /* index duration calculated during parsing */
3224 duration = stream->idx_duration;
3226 /* now pick a good duration */
3227 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3228 /* index gave valid duration, use that */
3229 GST_INFO ("Stream %p duration according to index: %" GST_TIME_FORMAT,
3230 stream, GST_TIME_ARGS (duration));
3232 /* fall back to header info to calculate a duration */
3233 duration = hduration;
3235 GST_INFO ("Setting duration of stream #%d to %" GST_TIME_FORMAT,
3236 i, GST_TIME_ARGS (duration));
3237 /* set duration for the stream */
3238 stream->duration = duration;
3240 /* find total duration */
3241 if (total == GST_CLOCK_TIME_NONE ||
3242 (GST_CLOCK_TIME_IS_VALID (duration) && duration > total))
3246 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
3247 /* now update the duration for those streams where we had none */
3248 for (i = 0; i < avi->num_streams; i++) {
3249 stream = &avi->stream[i];
3251 if (!GST_CLOCK_TIME_IS_VALID (stream->duration)
3252 || stream->duration == 0) {
3253 stream->duration = total;
3255 GST_INFO ("Stream %p duration according to total: %" GST_TIME_FORMAT,
3256 stream, GST_TIME_ARGS (total));
3261 /* and set the total duration in the segment. */
3262 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
3263 GST_TIME_ARGS (total));
3265 avi->segment.duration = total;
3268 /* returns FALSE if there are no pads to deliver event to,
3269 * otherwise TRUE (whatever the outcome of event sending),
3270 * takes ownership of the event. */
3272 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
3274 gboolean result = FALSE;
3277 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
3278 GST_EVENT_TYPE_NAME (event), avi->num_streams);
3280 for (i = 0; i < avi->num_streams; i++) {
3281 GstAviStream *stream = &avi->stream[i];
3285 gst_pad_push_event (stream->pad, gst_event_ref (event));
3288 gst_event_unref (event);
3293 gst_avi_demux_check_seekability (GstAviDemux * avi)
3296 gboolean seekable = FALSE;
3297 gint64 start = -1, stop = -1;
3299 query = gst_query_new_seeking (GST_FORMAT_BYTES);
3300 if (!gst_pad_peer_query (avi->sinkpad, query)) {
3301 GST_DEBUG_OBJECT (avi, "seeking query failed");
3305 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
3307 /* try harder to query upstream size if we didn't get it the first time */
3308 if (seekable && stop == -1) {
3309 GST_DEBUG_OBJECT (avi, "doing duration query to fix up unset stop");
3310 gst_pad_peer_query_duration (avi->sinkpad, GST_FORMAT_BYTES, &stop);
3313 /* if upstream doesn't know the size, it's likely that it's not seekable in
3314 * practice even if it technically may be seekable */
3315 if (seekable && (start != 0 || stop <= start)) {
3316 GST_DEBUG_OBJECT (avi, "seekable but unknown start/stop -> disable");
3321 GST_INFO_OBJECT (avi, "seekable: %d (%" G_GUINT64_FORMAT " - %"
3322 G_GUINT64_FORMAT ")", seekable, start, stop);
3323 avi->seekable = seekable;
3325 gst_query_unref (query);
3329 * Read AVI headers when streaming
3331 static GstFlowReturn
3332 gst_avi_demux_stream_header_push (GstAviDemux * avi)
3334 GstFlowReturn ret = GST_FLOW_OK;
3339 GstBuffer *buf = NULL, *sub = NULL;
3342 GstTagList *tags = NULL;
3345 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
3347 switch (avi->header_state) {
3348 case GST_AVI_DEMUX_HEADER_TAG_LIST:
3350 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3351 avi->offset += 8 + GST_ROUND_UP_2 (size);
3352 if (tag != GST_RIFF_TAG_LIST)
3353 goto header_no_list;
3355 gst_adapter_flush (avi->adapter, 8);
3356 /* Find the 'hdrl' LIST tag */
3357 GST_DEBUG ("Reading %d bytes", size);
3358 buf = gst_adapter_take_buffer (avi->adapter, size);
3360 gst_buffer_extract (buf, 0, fourcc, 4);
3362 if (GST_READ_UINT32_LE (fourcc) != GST_RIFF_LIST_hdrl) {
3363 GST_WARNING_OBJECT (avi, "Invalid AVI header (no hdrl at start): %"
3364 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag));
3365 gst_buffer_unref (buf);
3371 gst_adapter_flush (avi->adapter, 1);
3373 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
3375 gst_avi_demux_roundup_list (avi, &buf);
3377 /* the hdrl starts with a 'avih' header */
3378 if (!gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3380 goto header_no_avih;
3382 if (tag != GST_RIFF_TAG_avih)
3383 goto header_no_avih;
3385 if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3386 goto header_wrong_avih;
3388 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
3390 /* now, read the elements from the header until the end */
3391 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3393 /* sub can be NULL on empty tags */
3398 case GST_RIFF_TAG_LIST:
3399 if (gst_buffer_get_size (sub) < 4)
3402 gst_buffer_extract (sub, 0, fourcc, 4);
3404 switch (GST_READ_UINT32_LE (fourcc)) {
3405 case GST_RIFF_LIST_strl:
3406 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3408 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3409 ("failed to parse stream, ignoring"));
3414 case GST_RIFF_LIST_odml:
3415 gst_avi_demux_parse_odml (avi, sub);
3419 GST_WARNING_OBJECT (avi,
3420 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3421 GST_FOURCC_ARGS (GST_READ_UINT32_LE (fourcc)));
3423 case GST_RIFF_TAG_JUNQ:
3424 case GST_RIFF_TAG_JUNK:
3429 gst_avi_demux_parse_idit (avi, sub);
3432 GST_WARNING_OBJECT (avi,
3433 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header",
3434 GST_FOURCC_ARGS (tag));
3435 /* Only get buffer for debugging if the memdump is needed */
3436 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= 9) {
3439 gst_buffer_map (sub, &map, GST_MAP_READ);
3440 GST_MEMDUMP_OBJECT (avi, "Unknown tag", map.data, map.size);
3441 gst_buffer_unmap (sub, &map);
3444 case GST_RIFF_TAG_JUNQ:
3445 case GST_RIFF_TAG_JUNK:
3447 /* move to next chunk */
3449 gst_buffer_unref (sub);
3454 gst_buffer_unref (buf);
3455 GST_DEBUG ("elements parsed");
3457 /* check parsed streams */
3458 if (avi->num_streams == 0) {
3460 } else if (avi->num_streams != avi->avih->streams) {
3461 GST_WARNING_OBJECT (avi,
3462 "Stream header mentioned %d streams, but %d available",
3463 avi->avih->streams, avi->num_streams);
3465 GST_DEBUG ("Get junk and info next");
3466 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
3468 /* Need more data */
3472 case GST_AVI_DEMUX_HEADER_INFO:
3473 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
3475 if (gst_adapter_available (avi->adapter) < 12)
3478 data = gst_adapter_map (avi->adapter, 12);
3479 tag = GST_READ_UINT32_LE (data);
3480 size = GST_READ_UINT32_LE (data + 4);
3481 ltag = GST_READ_UINT32_LE (data + 8);
3482 gst_adapter_unmap (avi->adapter);
3484 if (tag == GST_RIFF_TAG_LIST) {
3486 case GST_RIFF_LIST_movi:
3487 gst_adapter_flush (avi->adapter, 12);
3488 if (!avi->first_movi_offset)
3489 avi->first_movi_offset = avi->offset;
3491 avi->idx1_offset = avi->offset + size - 4;
3493 case GST_RIFF_LIST_INFO:
3494 GST_DEBUG ("Found INFO chunk");
3495 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3496 GST_DEBUG ("got size %d", size);
3498 gst_adapter_flush (avi->adapter, 12);
3500 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
3503 gst_adapter_flush (avi->adapter, 1);
3504 gst_riff_parse_info (GST_ELEMENT_CAST (avi), buf, &tags);
3506 if (avi->globaltags) {
3507 gst_tag_list_insert (avi->globaltags, tags,
3508 GST_TAG_MERGE_REPLACE);
3509 gst_tag_list_unref (tags);
3511 avi->globaltags = tags;
3515 gst_buffer_unref (buf);
3517 avi->offset += GST_ROUND_UP_2 (size) - 4;
3519 GST_DEBUG ("skipping INFO LIST prefix");
3522 /* Need more data */
3527 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
3528 /* accept 0 size buffer here */
3529 avi->abort_buffering = FALSE;
3530 avi->offset += 8 + GST_ROUND_UP_2 (size);
3531 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3533 /* Need more data */
3539 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
3540 /* accept 0 size buffer here */
3541 avi->abort_buffering = FALSE;
3542 avi->offset += 8 + GST_ROUND_UP_2 (size);
3543 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3545 /* Need more data */
3552 GST_WARNING ("unhandled header state: %d", avi->header_state);
3557 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3558 avi->num_streams, avi->stream[0].indexes);
3560 GST_DEBUG ("Found movi chunk. Starting to stream data");
3561 avi->state = GST_AVI_DEMUX_MOVI;
3563 /* no indexes in push mode, but it still sets some variables */
3564 gst_avi_demux_calculate_durations_from_index (avi);
3566 gst_avi_demux_expose_streams (avi, TRUE);
3568 /* prepare all streams for index 0 */
3569 for (i = 0; i < avi->num_streams; i++)
3570 avi->stream[i].current_entry = 0;
3572 /* create initial NEWSEGMENT event */
3574 gst_event_unref (avi->seg_event);
3575 avi->seg_event = gst_event_new_segment (&avi->segment);
3576 if (avi->segment_seqnum)
3577 gst_event_set_seqnum (avi->seg_event, avi->segment_seqnum);
3579 gst_avi_demux_check_seekability (avi);
3581 /* at this point we know all the streams and we can signal the no more
3583 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3584 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3591 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3592 return GST_FLOW_ERROR;
3596 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3597 ("Invalid AVI header (no LIST at start): %"
3598 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3599 return GST_FLOW_ERROR;
3603 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3604 ("Invalid AVI header (no avih at start): %"
3605 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3607 gst_buffer_unref (sub);
3609 gst_buffer_unref (buf);
3610 return GST_FLOW_ERROR;
3614 gst_buffer_unref (buf);
3615 return GST_FLOW_ERROR;
3620 gst_avi_demux_add_date_tag (GstAviDemux * avi, gint y, gint m, gint d,
3621 gint h, gint min, gint s)
3626 date = g_date_new_dmy (d, m, y);
3627 if (!g_date_valid (date)) {
3629 GST_WARNING_OBJECT (avi, "Refusing to add invalid date %d-%d-%d", y, m, d);
3634 dt = gst_date_time_new_local_time (y, m, d, h, min, s);
3636 if (avi->globaltags == NULL)
3637 avi->globaltags = gst_tag_list_new_empty ();
3639 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE, date,
3643 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE_TIME,
3645 gst_date_time_unref (dt);
3650 gst_avi_demux_parse_idit_nums_only (GstAviDemux * avi, gchar * data)
3653 gint hr = 0, min = 0, sec = 0;
3656 GST_DEBUG ("data : '%s'", data);
3658 ret = sscanf (data, "%d:%d:%d %d:%d:%d", &y, &m, &d, &hr, &min, &sec);
3660 /* Attempt YYYY/MM/DD/ HH:MM variant (found in CASIO cameras) */
3661 ret = sscanf (data, "%04d/%02d/%02d/ %d:%d", &y, &m, &d, &hr, &min);
3663 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3667 gst_avi_demux_add_date_tag (avi, y, m, d, hr, min, sec);
3671 get_month_num (gchar * data, guint size)
3673 if (g_ascii_strncasecmp (data, "jan", 3) == 0) {
3675 } else if (g_ascii_strncasecmp (data, "feb", 3) == 0) {
3677 } else if (g_ascii_strncasecmp (data, "mar", 3) == 0) {
3679 } else if (g_ascii_strncasecmp (data, "apr", 3) == 0) {
3681 } else if (g_ascii_strncasecmp (data, "may", 3) == 0) {
3683 } else if (g_ascii_strncasecmp (data, "jun", 3) == 0) {
3685 } else if (g_ascii_strncasecmp (data, "jul", 3) == 0) {
3687 } else if (g_ascii_strncasecmp (data, "aug", 3) == 0) {
3689 } else if (g_ascii_strncasecmp (data, "sep", 3) == 0) {
3691 } else if (g_ascii_strncasecmp (data, "oct", 3) == 0) {
3693 } else if (g_ascii_strncasecmp (data, "nov", 3) == 0) {
3695 } else if (g_ascii_strncasecmp (data, "dec", 3) == 0) {
3703 gst_avi_demux_parse_idit_text (GstAviDemux * avi, gchar * data)
3705 gint year, month, day;
3706 gint hour, min, sec;
3711 ret = sscanf (data, "%3s %3s %d %d:%d:%d %d", weekday, monthstr, &day, &hour,
3714 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3717 month = get_month_num (monthstr, strlen (monthstr));
3718 gst_avi_demux_add_date_tag (avi, year, month, day, hour, min, sec);
3722 gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf)
3727 gchar *safedata = NULL;
3729 gst_buffer_map (buf, &map, GST_MAP_READ);
3732 * http://www.eden-foundation.org/products/code/film_date_stamp/index.html
3734 * This tag could be in one of the below formats
3735 * 2005:08:17 11:42:43
3736 * THU OCT 26 16:46:04 2006
3737 * Mon Mar 3 09:44:56 2008
3739 * FIXME: Our date tag doesn't include hours
3742 /* skip eventual initial whitespace */
3743 ptr = (gchar *) map.data;
3746 while (left > 0 && g_ascii_isspace (ptr[0])) {
3755 /* make a safe copy to add a \0 to the end of the string */
3756 safedata = g_strndup (ptr, left);
3758 /* test if the first char is a alpha or a number */
3759 if (g_ascii_isdigit (ptr[0])) {
3760 gst_avi_demux_parse_idit_nums_only (avi, safedata);
3762 gst_buffer_unmap (buf, &map);
3764 } else if (g_ascii_isalpha (ptr[0])) {
3765 gst_avi_demux_parse_idit_text (avi, safedata);
3767 gst_buffer_unmap (buf, &map);
3774 GST_WARNING_OBJECT (avi, "IDIT tag has no parsable info");
3775 gst_buffer_unmap (buf, &map);
3779 parse_tag_value (GstAviDemux * avi, GstTagList * taglist, const gchar * type,
3780 guint8 * ptr, guint tsize)
3782 static const gchar *env_vars[] = { "GST_AVI_TAG_ENCODING",
3783 "GST_RIFF_TAG_ENCODING", "GST_TAG_ENCODING", NULL
3788 tag_type = gst_tag_get_type (type);
3789 val = gst_tag_freeform_string_to_utf8 ((gchar *) ptr, tsize, env_vars);
3792 if (tag_type == G_TYPE_STRING) {
3793 gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND, type, val, NULL);
3795 GValue tag_val = { 0, };
3797 g_value_init (&tag_val, tag_type);
3798 if (gst_value_deserialize (&tag_val, val)) {
3799 gst_tag_list_add_value (taglist, GST_TAG_MERGE_APPEND, type, &tag_val);
3801 GST_WARNING_OBJECT (avi, "could not deserialize '%s' into a "
3802 "tag %s of type %s", val, type, g_type_name (tag_type));
3804 g_value_unset (&tag_val);
3808 GST_WARNING_OBJECT (avi, "could not extract %s tag", type);
3813 gst_avi_demux_parse_strd (GstAviDemux * avi, GstBuffer * buf)
3818 gst_buffer_map (buf, &map, GST_MAP_READ);
3820 guint8 *ptr = map.data;
3821 gsize left = map.size;
3824 * http://www.eden-foundation.org/products/code/film_date_stamp/index.html
3826 tag = GST_READ_UINT32_LE (ptr);
3827 if ((tag == GST_MAKE_FOURCC ('A', 'V', 'I', 'F')) && (map.size > 98)) {
3832 if (!memcmp (ptr, "FUJIFILM", 8)) {
3833 GST_MEMDUMP_OBJECT (avi, "fujifim tag", ptr, 48);
3838 while (ptr[sub_size] && sub_size < left)
3841 if (avi->globaltags == NULL)
3842 avi->globaltags = gst_tag_list_new_empty ();
3844 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_APPEND,
3845 GST_TAG_DEVICE_MANUFACTURER, "FUJIFILM", NULL);
3846 parse_tag_value (avi, avi->globaltags, GST_TAG_DEVICE_MODEL, ptr,
3849 while (ptr[sub_size] == '\0' && sub_size < left)
3855 while (ptr[sub_size] && sub_size < left)
3862 parse_tag_value (avi, avi->globaltags, GST_TAG_DATE_TIME, ptr,
3867 gst_buffer_unmap (buf, &map);
3871 * gst_avi_demux_parse_ncdt:
3872 * @element: caller element (used for debugging/error).
3873 * @buf: input data to be used for parsing, stripped from header.
3874 * @taglist: a pointer to a taglist (returned by this function)
3875 * containing information about this stream. May be
3876 * NULL if no supported tags were found.
3878 * Parses Nikon metadata from input data.
3881 gst_avi_demux_parse_ncdt (GstAviDemux * avi, GstBuffer * buf,
3882 GstTagList ** _taglist)
3890 GstTagList *taglist;
3892 g_return_if_fail (_taglist != NULL);
3898 gst_buffer_map (buf, &info, GST_MAP_READ);
3900 taglist = gst_tag_list_new_empty ();
3906 tag = GST_READ_UINT32_LE (ptr);
3907 tsize = GST_READ_UINT32_LE (ptr + 4);
3909 GST_MEMDUMP_OBJECT (avi, "tag chunk", ptr, MIN (tsize + 8, left));
3914 GST_DEBUG_OBJECT (avi, "tag %" GST_FOURCC_FORMAT ", size %u",
3915 GST_FOURCC_ARGS (tag), tsize);
3918 GST_WARNING_OBJECT (avi,
3919 "Tagsize %d is larger than available data %" G_GSIZE_FORMAT,
3924 /* find out the type of metadata */
3926 case GST_RIFF_LIST_nctg:
3928 guint16 sub_tag = GST_READ_UINT16_LE (ptr);
3929 guint16 sub_size = GST_READ_UINT16_LE (ptr + 2);
3935 if (sub_size > tsize)
3938 GST_DEBUG_OBJECT (avi, "sub-tag %u, size %u", sub_tag, sub_size);
3939 /* http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/Nikon.html#NCTG
3940 * for some reason the sub_tag has a +2 offset
3943 case 0x03: /* Make */
3944 type = GST_TAG_DEVICE_MANUFACTURER;
3946 case 0x04: /* Model */
3947 type = GST_TAG_DEVICE_MODEL;
3949 /* TODO: 0x05: is software version, like V1.0 */
3950 case 0x06: /* Software */
3951 type = GST_TAG_ENCODER;
3953 case 0x13: /* CreationDate */
3954 type = GST_TAG_DATE_TIME;
3966 if (type != NULL && ptr[0] != '\0') {
3967 GST_DEBUG_OBJECT (avi, "mapped tag %u to tag %s", sub_tag, type);
3969 parse_tag_value (avi, taglist, type, ptr, sub_size);
3979 GST_WARNING_OBJECT (avi,
3980 "Unknown ncdt (metadata) tag entry %" GST_FOURCC_FORMAT,
3981 GST_FOURCC_ARGS (tag));
3982 GST_MEMDUMP_OBJECT (avi, "Unknown ncdt", ptr, tsize);
3996 if (!gst_tag_list_is_empty (taglist)) {
3997 GST_INFO_OBJECT (avi, "extracted tags: %" GST_PTR_FORMAT, taglist);
3998 *_taglist = taglist;
4001 gst_tag_list_unref (taglist);
4003 gst_buffer_unmap (buf, &info);
4009 * Read full AVI headers.
4011 static GstFlowReturn
4012 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
4015 GstBuffer *buf, *sub = NULL;
4018 GstElement *element = GST_ELEMENT_CAST (avi);
4020 GstTagList *tags = NULL;
4023 stamp = gst_util_get_timestamp ();
4025 /* the header consists of a 'hdrl' LIST tag */
4026 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
4027 if (res != GST_FLOW_OK)
4028 goto pull_range_failed;
4029 else if (tag != GST_RIFF_TAG_LIST)
4031 else if (gst_buffer_get_size (buf) < 4)
4034 GST_DEBUG_OBJECT (avi, "parsing headers");
4036 /* Find the 'hdrl' LIST tag */
4037 gst_buffer_extract (buf, 0, fourcc, 4);
4038 while (GST_READ_UINT32_LE (fourcc) != GST_RIFF_LIST_hdrl) {
4039 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
4040 GST_FOURCC_ARGS (GST_READ_UINT32_LE (fourcc)));
4043 gst_buffer_unref (buf);
4045 /* read new chunk */
4046 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
4047 if (res != GST_FLOW_OK)
4048 goto pull_range_failed;
4049 else if (tag != GST_RIFF_TAG_LIST)
4051 else if (gst_buffer_get_size (buf) < 4)
4053 gst_buffer_extract (buf, 0, fourcc, 4);
4056 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
4058 gst_avi_demux_roundup_list (avi, &buf);
4060 /* the hdrl starts with a 'avih' header */
4061 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
4063 else if (tag != GST_RIFF_TAG_avih)
4065 else if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
4068 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
4070 /* now, read the elements from the header until the end */
4071 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
4074 /* sub can be NULL on empty tags */
4078 gst_buffer_map (sub, &map, GST_MAP_READ);
4081 case GST_RIFF_TAG_LIST:
4085 switch (GST_READ_UINT32_LE (map.data)) {
4086 case GST_RIFF_LIST_strl:
4087 gst_buffer_unmap (sub, &map);
4088 if (!(gst_avi_demux_parse_stream (avi, sub))) {
4089 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
4090 ("failed to parse stream, ignoring"));
4095 case GST_RIFF_LIST_odml:
4096 gst_buffer_unmap (sub, &map);
4097 gst_avi_demux_parse_odml (avi, sub);
4100 case GST_RIFF_LIST_INFO:
4101 gst_buffer_unmap (sub, &map);
4102 gst_buffer_resize (sub, 4, -1);
4103 gst_riff_parse_info (element, sub, &tags);
4105 if (avi->globaltags) {
4106 gst_tag_list_insert (avi->globaltags, tags,
4107 GST_TAG_MERGE_REPLACE);
4108 gst_tag_list_unref (tags);
4110 avi->globaltags = tags;
4114 gst_buffer_unref (sub);
4117 case GST_RIFF_LIST_ncdt:
4118 gst_buffer_unmap (sub, &map);
4119 gst_buffer_resize (sub, 4, -1);
4120 gst_avi_demux_parse_ncdt (avi, sub, &tags);
4122 if (avi->globaltags) {
4123 gst_tag_list_insert (avi->globaltags, tags,
4124 GST_TAG_MERGE_REPLACE);
4125 gst_tag_list_unref (tags);
4127 avi->globaltags = tags;
4131 gst_buffer_unref (sub);
4135 GST_WARNING_OBJECT (avi,
4136 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
4137 GST_FOURCC_ARGS (GST_READ_UINT32_LE (map.data)));
4138 GST_MEMDUMP_OBJECT (avi, "Unknown list", map.data, map.size);
4140 case GST_RIFF_TAG_JUNQ:
4141 case GST_RIFF_TAG_JUNK:
4146 gst_avi_demux_parse_idit (avi, sub);
4149 GST_WARNING_OBJECT (avi,
4150 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header",
4151 GST_FOURCC_ARGS (tag));
4152 GST_MEMDUMP_OBJECT (avi, "Unknown tag", map.data, map.size);
4154 case GST_RIFF_TAG_JUNQ:
4155 case GST_RIFF_TAG_JUNK:
4158 gst_buffer_unmap (sub, &map);
4159 gst_buffer_unref (sub);
4165 gst_buffer_unref (buf);
4166 GST_DEBUG ("elements parsed");
4168 /* check parsed streams */
4169 if (avi->num_streams == 0)
4171 else if (avi->num_streams != avi->avih->streams) {
4172 GST_WARNING_OBJECT (avi,
4173 "Stream header mentioned %d streams, but %d available",
4174 avi->avih->streams, avi->num_streams);
4177 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
4178 G_GUINT64_FORMAT, avi->offset);
4180 /* Now, find the data (i.e. skip all junk between header and data) */
4187 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
4188 if (res != GST_FLOW_OK) {
4189 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
4190 goto pull_range_failed;
4191 } else if (gst_buffer_get_size (buf) < 12) {
4192 GST_DEBUG_OBJECT (avi,
4193 "got %" G_GSIZE_FORMAT " bytes which is less than 12 bytes",
4194 gst_buffer_get_size (buf));
4195 gst_buffer_unref (buf);
4196 return GST_FLOW_ERROR;
4199 gst_buffer_map (buf, &map, GST_MAP_READ);
4200 tag = GST_READ_UINT32_LE (map.data);
4201 size = GST_READ_UINT32_LE (map.data + 4);
4202 ltag = GST_READ_UINT32_LE (map.data + 8);
4204 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
4205 GST_FOURCC_ARGS (tag), size);
4206 GST_MEMDUMP ("Tag content", map.data, map.size);
4207 gst_buffer_unmap (buf, &map);
4208 gst_buffer_unref (buf);
4211 case GST_RIFF_TAG_LIST:{
4213 case GST_RIFF_LIST_movi:
4214 GST_DEBUG_OBJECT (avi,
4215 "Reached the 'movi' tag, we're done with skipping");
4217 case GST_RIFF_LIST_INFO:
4219 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
4221 if (res != GST_FLOW_OK) {
4222 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
4223 goto pull_range_failed;
4225 GST_DEBUG ("got size %" G_GSIZE_FORMAT, gst_buffer_get_size (buf));
4227 GST_DEBUG ("skipping INFO LIST prefix");
4228 avi->offset += (4 - GST_ROUND_UP_2 (size));
4229 gst_buffer_unref (buf);
4233 sub = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, 4, -1);
4234 gst_riff_parse_info (element, sub, &tags);
4236 if (avi->globaltags) {
4237 gst_tag_list_insert (avi->globaltags, tags,
4238 GST_TAG_MERGE_REPLACE);
4239 gst_tag_list_unref (tags);
4241 avi->globaltags = tags;
4246 gst_buffer_unref (sub);
4249 gst_buffer_unref (buf);
4250 /* gst_riff_read_chunk() has already advanced avi->offset */
4252 case GST_RIFF_LIST_ncdt:
4254 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
4256 if (res != GST_FLOW_OK) {
4257 GST_DEBUG_OBJECT (avi, "couldn't read ncdt chunk");
4258 goto pull_range_failed;
4260 GST_DEBUG ("got size %" G_GSIZE_FORMAT, gst_buffer_get_size (buf));
4262 GST_DEBUG ("skipping ncdt LIST prefix");
4263 avi->offset += (4 - GST_ROUND_UP_2 (size));
4264 gst_buffer_unref (buf);
4268 sub = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, 4, -1);
4269 gst_avi_demux_parse_ncdt (avi, sub, &tags);
4271 if (avi->globaltags) {
4272 gst_tag_list_insert (avi->globaltags, tags,
4273 GST_TAG_MERGE_REPLACE);
4274 gst_tag_list_unref (tags);
4276 avi->globaltags = tags;
4281 gst_buffer_unref (sub);
4284 gst_buffer_unref (buf);
4285 /* gst_riff_read_chunk() has already advanced avi->offset */
4288 GST_WARNING_OBJECT (avi,
4289 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
4290 GST_FOURCC_ARGS (ltag));
4291 avi->offset += 8 + GST_ROUND_UP_2 (size);
4297 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
4298 GST_FOURCC_ARGS (tag));
4300 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
4301 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
4302 /* Only get buffer for debugging if the memdump is needed */
4303 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= 9) {
4305 res = gst_pad_pull_range (avi->sinkpad, avi->offset, size, &buf);
4306 if (res != GST_FLOW_OK) {
4307 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
4308 goto pull_range_failed;
4310 gst_buffer_map (buf, &map, GST_MAP_READ);
4311 GST_MEMDUMP ("Junk", map.data, map.size);
4312 gst_buffer_unmap (buf, &map);
4313 gst_buffer_unref (buf);
4315 avi->offset += 8 + GST_ROUND_UP_2 (size);
4321 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
4322 avi->num_streams, avi->stream[0].indexes);
4324 /* create or read stream index (for seeking) */
4325 if (avi->stream[0].indexes != NULL) {
4326 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
4327 gst_avi_demux_read_subindexes_pull (avi);
4329 if (!avi->have_index) {
4330 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
4331 gst_avi_demux_stream_index (avi);
4333 /* still no index, scan */
4334 if (!avi->have_index) {
4335 gst_avi_demux_stream_scan (avi);
4337 /* still no index.. this is a fatal error for now.
4338 * FIXME, we should switch to plain push mode without seeking
4339 * instead of failing. */
4340 if (!avi->have_index)
4344 /* use the indexes now to construct nice durations */
4345 gst_avi_demux_calculate_durations_from_index (avi);
4347 gst_avi_demux_expose_streams (avi, FALSE);
4349 /* do initial seek to the default segment values */
4350 gst_avi_demux_do_seek (avi, &avi->segment, 0);
4352 /* create initial NEWSEGMENT event */
4354 gst_event_unref (avi->seg_event);
4355 avi->seg_event = gst_event_new_segment (&avi->segment);
4356 if (avi->segment_seqnum)
4357 gst_event_set_seqnum (avi->seg_event, avi->segment_seqnum);
4359 stamp = gst_util_get_timestamp () - stamp;
4360 GST_DEBUG_OBJECT (avi, "pulling header took %" GST_TIME_FORMAT,
4361 GST_TIME_ARGS (stamp));
4363 /* at this point we know all the streams and we can signal the no more
4365 GST_DEBUG_OBJECT (avi, "signaling no more pads");
4366 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
4373 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
4374 ("Invalid AVI header (no LIST at start): %"
4375 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
4376 gst_buffer_unref (buf);
4377 return GST_FLOW_ERROR;
4381 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
4382 ("Invalid AVI header (no hdrl at start): %"
4383 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
4384 gst_buffer_unref (buf);
4385 return GST_FLOW_ERROR;
4389 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
4390 ("Invalid AVI header (no avih at start): %"
4391 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
4393 gst_buffer_unref (sub);
4394 gst_buffer_unref (buf);
4395 return GST_FLOW_ERROR;
4399 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
4400 ("Invalid AVI header (cannot parse avih at start)"));
4401 gst_buffer_unref (buf);
4402 return GST_FLOW_ERROR;
4406 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
4407 return GST_FLOW_ERROR;
4411 GST_WARNING ("file without or too big index");
4412 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
4413 ("Could not get/create index"));
4414 return GST_FLOW_ERROR;
4418 if (res == GST_FLOW_FLUSHING)
4420 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
4421 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
4426 /* move a stream to @index */
4428 gst_avi_demux_move_stream (GstAviDemux * avi, GstAviStream * stream,
4429 GstSegment * segment, guint index)
4431 GST_DEBUG_OBJECT (avi, "Move stream %d to %u", stream->num, index);
4433 if (segment->rate < 0.0) {
4435 /* Because we don't know the frame order we need to push from the prev keyframe
4436 * to the next keyframe. If there is a smart decoder downstream he will notice
4437 * that there are too many encoded frames send and return EOS when there
4438 * are enough decoded frames to fill the segment. */
4439 next_key = gst_avi_demux_index_next (avi, stream, index, TRUE);
4441 /* FIXME, we go back to 0, we should look at segment.start. We will however
4442 * stop earlier when the see the timestamp < segment.start */
4443 stream->start_entry = 0;
4444 stream->step_entry = index;
4445 stream->current_entry = index;
4446 stream->stop_entry = next_key;
4448 GST_DEBUG_OBJECT (avi, "reverse seek: start %u, step %u, stop %u",
4449 stream->start_entry, stream->step_entry, stream->stop_entry);
4451 stream->start_entry = index;
4452 stream->step_entry = index;
4453 stream->stop_entry = gst_avi_demux_index_last (avi, stream);
4455 if (stream->current_entry != index) {
4456 GST_DEBUG_OBJECT (avi, "Move DISCONT from %u to %u",
4457 stream->current_entry, index);
4458 stream->current_entry = index;
4459 stream->discont = TRUE;
4462 /* update the buffer info */
4463 gst_avi_demux_get_buffer_info (avi, stream, index,
4464 &stream->current_timestamp, &stream->current_ts_end,
4465 &stream->current_offset, &stream->current_offset_end);
4467 GST_DEBUG_OBJECT (avi, "Moved to %u, ts %" GST_TIME_FORMAT
4468 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4469 ", off_end %" G_GUINT64_FORMAT, index,
4470 GST_TIME_ARGS (stream->current_timestamp),
4471 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4472 stream->current_offset_end);
4474 GST_DEBUG_OBJECT (avi, "Seeking to offset %" G_GUINT64_FORMAT,
4475 stream->index[index].offset);
4479 * Do the actual seeking.
4482 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment,
4485 GstClockTime seek_time;
4486 gboolean keyframe, before, after;
4488 GstAviStream *stream;
4491 seek_time = segment->position;
4492 keyframe = ! !(flags & GST_SEEK_FLAG_KEY_UNIT);
4493 before = ! !(flags & GST_SEEK_FLAG_SNAP_BEFORE);
4494 after = ! !(flags & GST_SEEK_FLAG_SNAP_AFTER);
4496 GST_DEBUG_OBJECT (avi, "seek to: %" GST_TIME_FORMAT
4497 " keyframe seeking:%d, %s", GST_TIME_ARGS (seek_time), keyframe,
4498 snap_types[before ? 1 : 0][after ? 1 : 0]);
4500 /* FIXME, this code assumes the main stream with keyframes is stream 0,
4501 * which is mostly correct... */
4502 stream = &avi->stream[avi->main_stream];
4504 next = after && !before;
4505 if (segment->rate < 0)
4508 /* get the entry index for the requested position */
4509 index = gst_avi_demux_index_for_time (avi, stream, seek_time, next);
4510 GST_DEBUG_OBJECT (avi, "Got entry %u", index);
4514 /* check if we are already on a keyframe */
4515 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
4517 GST_DEBUG_OBJECT (avi, "not keyframe, searching forward");
4518 /* now go to the next keyframe, this is where we should start
4520 index = gst_avi_demux_index_next (avi, stream, index, TRUE);
4521 GST_DEBUG_OBJECT (avi, "next keyframe at %u", index);
4523 GST_DEBUG_OBJECT (avi, "not keyframe, searching back");
4524 /* now go to the previous keyframe, this is where we should start
4526 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4527 GST_DEBUG_OBJECT (avi, "previous keyframe at %u", index);
4531 /* move the main stream to this position */
4532 gst_avi_demux_move_stream (avi, stream, segment, index);
4535 /* when seeking to a keyframe, we update the result seek time
4536 * to the time of the keyframe. */
4537 seek_time = stream->current_timestamp;
4538 GST_DEBUG_OBJECT (avi, "keyframe adjusted to %" GST_TIME_FORMAT,
4539 GST_TIME_ARGS (seek_time));
4540 /* the seek time is always the position ... */
4541 segment->position = seek_time;
4542 /* ... and start and stream time when going forwards,
4543 * otherwise only stop time */
4544 if (segment->rate > 0.0)
4545 segment->start = segment->time = seek_time;
4547 segment->stop = seek_time;
4550 /* now set DISCONT and align the other streams */
4551 for (i = 0; i < avi->num_streams; i++) {
4552 GstAviStream *ostream;
4554 ostream = &avi->stream[i];
4555 if ((ostream == stream) || (ostream->index == NULL))
4558 /* get the entry index for the requested position */
4559 index = gst_avi_demux_index_for_time (avi, ostream, seek_time, FALSE);
4563 /* move to previous keyframe */
4564 if (!ENTRY_IS_KEYFRAME (&ostream->index[index]))
4565 index = gst_avi_demux_index_prev (avi, ostream, index, TRUE);
4567 gst_avi_demux_move_stream (avi, ostream, segment, index);
4569 GST_DEBUG_OBJECT (avi, "done seek to: %" GST_TIME_FORMAT,
4570 GST_TIME_ARGS (seek_time));
4576 * Handle seek event in pull mode.
4579 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4584 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4588 GstSegment seeksegment = { 0, };
4593 GST_DEBUG_OBJECT (avi, "doing seek with event");
4595 gst_event_parse_seek (event, &rate, &format, &flags,
4596 &cur_type, &cur, &stop_type, &stop);
4597 seqnum = gst_event_get_seqnum (event);
4599 /* we have to have a format as the segment format. Try to convert
4601 if (format != GST_FORMAT_TIME) {
4602 gboolean res = TRUE;
4604 if (cur_type != GST_SEEK_TYPE_NONE)
4605 res = gst_pad_query_convert (pad, format, cur, GST_FORMAT_TIME, &cur);
4606 if (res && stop_type != GST_SEEK_TYPE_NONE)
4607 res = gst_pad_query_convert (pad, format, stop, GST_FORMAT_TIME, &stop);
4611 format = GST_FORMAT_TIME;
4613 GST_DEBUG_OBJECT (avi,
4614 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
4615 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
4616 /* FIXME: can we do anything with rate!=1.0 */
4618 GST_DEBUG_OBJECT (avi, "doing seek without event");
4623 /* save flush flag */
4624 flush = flags & GST_SEEK_FLAG_FLUSH;
4627 GstEvent *fevent = gst_event_new_flush_start ();
4630 gst_event_set_seqnum (fevent, seqnum);
4631 /* for a flushing seek, we send a flush_start on all pads. This will
4632 * eventually stop streaming with a WRONG_STATE. We can thus eventually
4633 * take the STREAM_LOCK. */
4634 GST_DEBUG_OBJECT (avi, "sending flush start");
4635 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4636 gst_pad_push_event (avi->sinkpad, fevent);
4638 /* a non-flushing seek, we PAUSE the task so that we can take the
4640 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
4641 gst_pad_pause_task (avi->sinkpad);
4644 /* wait for streaming to stop */
4645 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
4646 GST_PAD_STREAM_LOCK (avi->sinkpad);
4648 /* copy segment, we need this because we still need the old
4649 * segment when we close the current segment. */
4650 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4653 GST_DEBUG_OBJECT (avi, "configuring seek");
4654 gst_segment_do_seek (&seeksegment, rate, format, flags,
4655 cur_type, cur, stop_type, stop, &update);
4657 /* do the seek, seeksegment.position contains the new position, this
4658 * actually never fails. */
4659 gst_avi_demux_do_seek (avi, &seeksegment, flags);
4662 GstEvent *fevent = gst_event_new_flush_stop (TRUE);
4665 gst_event_set_seqnum (fevent, seqnum);
4667 GST_DEBUG_OBJECT (avi, "sending flush stop");
4668 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4669 gst_pad_push_event (avi->sinkpad, fevent);
4672 /* now update the real segment info */
4673 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
4675 /* post the SEGMENT_START message when we do segmented playback */
4676 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4677 GstMessage *segment_start_msg =
4678 gst_message_new_segment_start (GST_OBJECT_CAST (avi),
4679 avi->segment.format, avi->segment.position);
4681 gst_message_set_seqnum (segment_start_msg, seqnum);
4682 gst_element_post_message (GST_ELEMENT_CAST (avi), segment_start_msg);
4685 /* queue the segment event for the streaming thread. */
4687 gst_event_unref (avi->seg_event);
4688 avi->seg_event = gst_event_new_segment (&avi->segment);
4690 gst_event_set_seqnum (avi->seg_event, seqnum);
4691 avi->segment_seqnum = seqnum;
4693 if (!avi->streaming) {
4694 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4695 avi->sinkpad, NULL);
4697 /* reset the last flow and mark discont, seek is always DISCONT */
4698 for (i = 0; i < avi->num_streams; i++) {
4699 GST_DEBUG_OBJECT (avi, "marking DISCONT");
4700 avi->stream[i].discont = TRUE;
4702 /* likewise for the whole new segment */
4703 gst_flow_combiner_reset (avi->flowcombiner);
4704 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
4711 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4717 * Handle seek event in push mode.
4720 avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4725 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4727 gboolean keyframe, before, after, next;
4728 GstAviStream *stream;
4732 GstSegment seeksegment;
4735 /* check we have the index */
4736 if (!avi->have_index) {
4737 GST_DEBUG_OBJECT (avi, "no seek index built, seek aborted.");
4740 GST_DEBUG_OBJECT (avi, "doing push-based seek with event");
4743 gst_event_parse_seek (event, &rate, &format, &flags,
4744 &cur_type, &cur, &stop_type, &stop);
4746 if (format != GST_FORMAT_TIME) {
4747 gboolean res = TRUE;
4749 if (cur_type != GST_SEEK_TYPE_NONE)
4750 res = gst_pad_query_convert (pad, format, cur, GST_FORMAT_TIME, &cur);
4751 if (res && stop_type != GST_SEEK_TYPE_NONE)
4752 res = gst_pad_query_convert (pad, format, stop, GST_FORMAT_TIME, &stop);
4754 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4758 format = GST_FORMAT_TIME;
4761 /* let gst_segment handle any tricky stuff */
4762 GST_DEBUG_OBJECT (avi, "configuring seek");
4763 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4764 gst_segment_do_seek (&seeksegment, rate, format, flags,
4765 cur_type, cur, stop_type, stop, &update);
4767 keyframe = ! !(flags & GST_SEEK_FLAG_KEY_UNIT);
4768 cur = seeksegment.position;
4769 before = ! !(flags & GST_SEEK_FLAG_SNAP_BEFORE);
4770 after = ! !(flags & GST_SEEK_FLAG_SNAP_AFTER);
4772 GST_DEBUG_OBJECT (avi,
4773 "Seek requested: ts %" GST_TIME_FORMAT " stop %" GST_TIME_FORMAT
4774 ", kf %u, %s, rate %lf", GST_TIME_ARGS (cur), GST_TIME_ARGS (stop),
4775 keyframe, snap_types[before ? 1 : 0][after ? 1 : 0], rate);
4778 GST_DEBUG_OBJECT (avi, "negative rate seek not supported in push mode");
4782 /* FIXME, this code assumes the main stream with keyframes is stream 0,
4783 * which is mostly correct... */
4784 str_num = avi->main_stream;
4785 stream = &avi->stream[str_num];
4787 next = after && !before;
4788 if (seeksegment.rate < 0)
4791 /* get the entry index for the requested position */
4792 index = gst_avi_demux_index_for_time (avi, stream, cur, next);
4793 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT,
4794 str_num, index, GST_TIME_ARGS (cur));
4798 /* check if we are already on a keyframe */
4799 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
4801 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching forward");
4802 /* now go to the next keyframe, this is where we should start
4804 index = gst_avi_demux_index_next (avi, stream, index, TRUE);
4805 GST_DEBUG_OBJECT (avi, "Found next keyframe at %u", index);
4807 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4808 /* now go to the previous keyframe, this is where we should start
4810 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4811 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", index);
4815 gst_avi_demux_get_buffer_info (avi, stream, index,
4816 &stream->current_timestamp, &stream->current_ts_end,
4817 &stream->current_offset, &stream->current_offset_end);
4819 /* re-use cur to be the timestamp of the seek as it _will_ be */
4820 cur = stream->current_timestamp;
4822 min_offset = stream->index[index].offset;
4823 avi->seek_kf_offset = min_offset - 8;
4825 GST_DEBUG_OBJECT (avi,
4826 "Seek to: ts %" GST_TIME_FORMAT " (on str %u, idx %u, offset %"
4827 G_GUINT64_FORMAT ")", GST_TIME_ARGS (stream->current_timestamp), str_num,
4830 for (n = 0; n < avi->num_streams; n++) {
4831 GstAviStream *str = &avi->stream[n];
4834 if (n == avi->main_stream)
4837 /* get the entry index for the requested position */
4838 idx = gst_avi_demux_index_for_time (avi, str, cur, FALSE);
4839 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT, n,
4840 idx, GST_TIME_ARGS (cur));
4844 /* check if we are already on a keyframe */
4845 if (!ENTRY_IS_KEYFRAME (&str->index[idx])) {
4847 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching forward");
4848 /* now go to the next keyframe, this is where we should start
4850 idx = gst_avi_demux_index_next (avi, str, idx, TRUE);
4851 GST_DEBUG_OBJECT (avi, "Found next keyframe at %u", idx);
4853 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4854 /* now go to the previous keyframe, this is where we should start
4856 idx = gst_avi_demux_index_prev (avi, str, idx, TRUE);
4857 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", idx);
4861 gst_avi_demux_get_buffer_info (avi, str, idx,
4862 &str->current_timestamp, &str->current_ts_end,
4863 &str->current_offset, &str->current_offset_end);
4865 if (str->index[idx].offset < min_offset) {
4866 min_offset = str->index[idx].offset;
4867 GST_DEBUG_OBJECT (avi,
4868 "Found an earlier offset at %" G_GUINT64_FORMAT ", str %u",
4876 GST_DEBUG_OBJECT (avi,
4877 "Seek performed: str %u, offset %" G_GUINT64_FORMAT ", idx %u, ts %"
4878 GST_TIME_FORMAT ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4879 ", off_end %" G_GUINT64_FORMAT, str_num, min_offset, index,
4880 GST_TIME_ARGS (stream->current_timestamp),
4881 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4882 stream->current_offset_end);
4884 /* index data refers to data, not chunk header (for pull mode convenience) */
4886 GST_DEBUG_OBJECT (avi, "seeking to chunk at offset %" G_GUINT64_FORMAT,
4889 if (!perform_seek_to_offset (avi, min_offset, gst_event_get_seqnum (event))) {
4890 GST_DEBUG_OBJECT (avi, "seek event failed!");
4898 * Handle whether we can perform the seek event or if we have to let the chain
4899 * function handle seeks to build the seek indexes first.
4902 gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
4905 /* check for having parsed index already */
4906 if (!avi->have_index) {
4908 gboolean building_index;
4910 GST_OBJECT_LOCK (avi);
4911 /* handle the seek event in the chain function */
4912 avi->state = GST_AVI_DEMUX_SEEK;
4914 /* copy the event */
4915 if (avi->seek_event)
4916 gst_event_unref (avi->seek_event);
4917 avi->seek_event = gst_event_ref (event);
4919 /* set the building_index flag so that only one thread can setup the
4920 * structures for index seeking. */
4921 building_index = avi->building_index;
4922 if (!building_index) {
4923 avi->building_index = TRUE;
4924 if (avi->stream[0].indexes) {
4925 avi->odml_stream = 0;
4926 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
4927 offset = avi->odml_subidxs[0];
4929 offset = avi->idx1_offset;
4932 GST_OBJECT_UNLOCK (avi);
4934 if (!building_index) {
4935 /* seek to the first subindex or legacy index */
4936 GST_INFO_OBJECT (avi,
4937 "Seeking to legacy index/first subindex at %" G_GUINT64_FORMAT,
4939 return perform_seek_to_offset (avi, offset, gst_event_get_seqnum (event));
4942 /* FIXME: we have to always return true so that we don't block the seek
4944 * Note: maybe it is OK to return true if we're still building the index */
4948 return avi_demux_handle_seek_push (avi, pad, event);
4952 * Helper for gst_avi_demux_invert()
4955 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
4957 memcpy (tmp, d1, bytes);
4958 memcpy (d1, d2, bytes);
4959 memcpy (d2, tmp, bytes);
4963 #define gst_avi_demux_is_uncompressed(fourcc) \
4964 (fourcc == GST_RIFF_DIB || \
4965 fourcc == GST_RIFF_rgb || \
4966 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW)
4969 * Invert DIB buffers... Takes existing buffer and
4970 * returns either the buffer or a new one (with old
4971 * one dereferenced).
4972 * FIXME: can't we preallocate tmp? and remember stride, bpp?
4975 gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
4983 if (stream->strh->type != GST_RIFF_FCC_vids)
4986 if (stream->strf.vids == NULL) {
4987 GST_WARNING ("Failed to retrieve vids for stream");
4991 fourcc = (stream->strf.vids->compression) ?
4992 stream->strf.vids->compression : stream->strh->fcc_handler;
4993 if (!gst_avi_demux_is_uncompressed (fourcc)) {
4994 return buf; /* Ignore non DIB buffers */
4997 /* raw rgb data is stored topdown, but instead of inverting the buffer, */
4998 /* some tools just negate the height field in the header (e.g. ffmpeg) */
4999 if (((gint32) stream->strf.vids->height) < 0)
5002 h = stream->strf.vids->height;
5003 w = stream->strf.vids->width;
5004 bpp = stream->strf.vids->bit_cnt ? stream->strf.vids->bit_cnt : 8;
5005 stride = GST_ROUND_UP_4 (w * (bpp / 8));
5007 buf = gst_buffer_make_writable (buf);
5009 gst_buffer_map (buf, &map, GST_MAP_READWRITE);
5010 if (map.size < (stride * h)) {
5011 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
5012 gst_buffer_unmap (buf, &map);
5016 tmp = g_malloc (stride);
5018 for (y = 0; y < h / 2; y++) {
5019 swap_line (map.data + stride * y, map.data + stride * (h - 1 - y), tmp,
5025 gst_buffer_unmap (buf, &map);
5027 /* append palette to paletted RGB8 buffer data */
5028 if (stream->rgb8_palette != NULL)
5029 buf = gst_buffer_append (buf, gst_buffer_ref (stream->rgb8_palette));
5036 gst_avi_demux_add_assoc (GstAviDemux * avi, GstAviStream * stream,
5037 GstClockTime timestamp, guint64 offset, gboolean keyframe)
5039 /* do not add indefinitely for open-ended streaming */
5040 if (G_UNLIKELY (avi->element_index && avi->seekable)) {
5041 GST_LOG_OBJECT (avi, "adding association %" GST_TIME_FORMAT "-> %"
5042 G_GUINT64_FORMAT, GST_TIME_ARGS (timestamp), offset);
5043 gst_index_add_association (avi->element_index, avi->index_id,
5044 keyframe ? GST_ASSOCIATION_FLAG_KEY_UNIT :
5045 GST_ASSOCIATION_FLAG_DELTA_UNIT, GST_FORMAT_TIME, timestamp,
5046 GST_FORMAT_BYTES, offset, NULL);
5047 /* current_entry is DEFAULT (frame #) */
5048 gst_index_add_association (avi->element_index, stream->index_id,
5049 keyframe ? GST_ASSOCIATION_FLAG_KEY_UNIT :
5050 GST_ASSOCIATION_FLAG_DELTA_UNIT, GST_FORMAT_TIME, timestamp,
5051 GST_FORMAT_BYTES, offset, GST_FORMAT_DEFAULT, stream->current_entry,
5058 * Returns the aggregated GstFlowReturn.
5060 static GstFlowReturn
5061 gst_avi_demux_combine_flows (GstAviDemux * avi, GstAviStream * stream,
5064 GST_LOG_OBJECT (avi, "Stream %s:%s flow return: %s",
5065 GST_DEBUG_PAD_NAME (stream->pad), gst_flow_get_name (ret));
5066 ret = gst_flow_combiner_update_pad_flow (avi->flowcombiner, stream->pad, ret);
5067 GST_LOG_OBJECT (avi, "combined to return %s", gst_flow_get_name (ret));
5072 /* move @stream to the next position in its index */
5073 static GstFlowReturn
5074 gst_avi_demux_advance (GstAviDemux * avi, GstAviStream * stream,
5077 guint old_entry, new_entry;
5079 old_entry = stream->current_entry;
5081 new_entry = old_entry + 1;
5083 /* see if we reached the end */
5084 if (new_entry >= stream->stop_entry) {
5085 if (avi->segment.rate < 0.0) {
5086 if (stream->step_entry == stream->start_entry) {
5087 /* we stepped all the way to the start, eos */
5088 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
5091 /* backwards, stop becomes step, find a new step */
5092 stream->stop_entry = stream->step_entry;
5093 stream->step_entry = gst_avi_demux_index_prev (avi, stream,
5094 stream->stop_entry, TRUE);
5096 GST_DEBUG_OBJECT (avi,
5097 "reverse playback jump: start %u, step %u, stop %u",
5098 stream->start_entry, stream->step_entry, stream->stop_entry);
5100 /* and start from the previous keyframe now */
5101 new_entry = stream->step_entry;
5104 GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
5109 if (new_entry != old_entry) {
5110 stream->current_entry = new_entry;
5111 stream->current_total = stream->index[new_entry].total;
5113 if (new_entry == old_entry + 1) {
5114 GST_DEBUG_OBJECT (avi, "moved forwards from %u to %u",
5115 old_entry, new_entry);
5116 /* we simply moved one step forwards, reuse current info */
5117 stream->current_timestamp = stream->current_ts_end;
5118 stream->current_offset = stream->current_offset_end;
5119 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
5120 NULL, &stream->current_ts_end, NULL, &stream->current_offset_end);
5122 /* we moved DISCONT, full update */
5123 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
5124 &stream->current_timestamp, &stream->current_ts_end,
5125 &stream->current_offset, &stream->current_offset_end);
5126 /* and MARK discont for this stream */
5127 stream->discont = TRUE;
5128 GST_DEBUG_OBJECT (avi, "Moved from %u to %u, ts %" GST_TIME_FORMAT
5129 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
5130 ", off_end %" G_GUINT64_FORMAT, old_entry, new_entry,
5131 GST_TIME_ARGS (stream->current_timestamp),
5132 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
5133 stream->current_offset_end);
5141 GST_DEBUG_OBJECT (avi, "we are EOS");
5142 /* setting current_timestamp to -1 marks EOS */
5143 stream->current_timestamp = -1;
5144 return GST_FLOW_EOS;
5148 /* find the stream with the lowest current position when going forwards or with
5149 * the highest position when going backwards, this is the stream
5150 * we should push from next */
5152 gst_avi_demux_find_next (GstAviDemux * avi, gfloat rate)
5154 guint64 min_time, max_time;
5155 guint stream_num, i;
5158 min_time = G_MAXUINT64;
5161 for (i = 0; i < avi->num_streams; i++) {
5163 GstAviStream *stream;
5165 stream = &avi->stream[i];
5167 /* ignore streams that finished */
5168 if (stream->pad && GST_PAD_LAST_FLOW_RETURN (stream->pad) == GST_FLOW_EOS)
5171 position = stream->current_timestamp;
5173 /* position of -1 is EOS */
5174 if (position != -1) {
5175 if (rate > 0.0 && position < min_time) {
5176 min_time = position;
5178 } else if (rate < 0.0 && position >= max_time) {
5179 max_time = position;
5188 gst_avi_demux_align_buffer (GstAviDemux * demux,
5189 GstBuffer * buffer, gsize alignment)
5193 gst_buffer_map (buffer, &map, GST_MAP_READ);
5195 if (map.size < sizeof (guintptr)) {
5196 gst_buffer_unmap (buffer, &map);
5200 if (((guintptr) map.data) & (alignment - 1)) {
5201 GstBuffer *new_buffer;
5202 GstAllocationParams params = { 0, alignment - 1, 0, 0, };
5204 new_buffer = gst_buffer_new_allocate (NULL,
5205 gst_buffer_get_size (buffer), ¶ms);
5207 /* Copy data "by hand", so ensure alignment is kept: */
5208 gst_buffer_fill (new_buffer, 0, map.data, map.size);
5210 gst_buffer_copy_into (new_buffer, buffer, GST_BUFFER_COPY_METADATA, 0, -1);
5211 GST_DEBUG_OBJECT (demux,
5212 "We want output aligned on %" G_GSIZE_FORMAT ", reallocated",
5215 gst_buffer_unmap (buffer, &map);
5216 gst_buffer_unref (buffer);
5221 gst_buffer_unmap (buffer, &map);
5225 static GstFlowReturn
5226 gst_avi_demux_loop_data (GstAviDemux * avi)
5228 GstFlowReturn ret = GST_FLOW_OK;
5230 GstAviStream *stream;
5231 gboolean processed = FALSE;
5233 guint64 offset, size;
5234 GstClockTime timestamp, duration;
5235 guint64 out_offset, out_offset_end;
5237 GstAviIndexEntry *entry;
5240 stream_num = gst_avi_demux_find_next (avi, avi->segment.rate);
5243 if (G_UNLIKELY (stream_num == -1)) {
5244 GST_DEBUG_OBJECT (avi, "all streams are EOS");
5248 /* we have the stream now */
5249 stream = &avi->stream[stream_num];
5251 /* skip streams without pads */
5253 GST_DEBUG_OBJECT (avi, "skipping entry from stream %d without pad",
5258 /* get the timing info for the entry */
5259 timestamp = stream->current_timestamp;
5260 duration = stream->current_ts_end - timestamp;
5261 out_offset = stream->current_offset;
5262 out_offset_end = stream->current_offset_end;
5264 /* get the entry data info */
5265 entry = &stream->index[stream->current_entry];
5266 offset = entry->offset;
5268 keyframe = ENTRY_IS_KEYFRAME (entry);
5270 /* skip empty entries */
5272 GST_DEBUG_OBJECT (avi, "Skipping entry %u (%" G_GUINT64_FORMAT ", %p)",
5273 stream->current_entry, size, stream->pad);
5277 if (avi->segment.rate > 0.0) {
5278 /* only check this for forwards playback for now */
5279 if (keyframe && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
5280 && (timestamp > avi->segment.stop)) {
5284 if (keyframe && GST_CLOCK_TIME_IS_VALID (avi->segment.start)
5285 && (timestamp < avi->segment.start))
5289 GST_LOG ("reading buffer (size=%" G_GUINT64_FORMAT "), stream %d, pos %"
5290 G_GUINT64_FORMAT " (0x%" G_GINT64_MODIFIER "x), kf %d", size,
5291 stream_num, offset, offset, keyframe);
5293 /* FIXME, check large chunks and cut them up */
5295 /* pull in the data */
5297 ret = gst_pad_pull_range (avi->sinkpad, offset, size, &buf);
5298 if (ret != GST_FLOW_OK)
5301 /* check for short buffers, this is EOS as well */
5302 if (gst_buffer_get_size (buf) < size)
5305 /* invert the picture if needed, and append palette for RGB8P */
5306 buf = gst_avi_demux_invert (stream, buf);
5308 /* mark non-keyframes */
5309 if (keyframe || stream->is_raw) {
5310 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
5311 GST_BUFFER_PTS (buf) = timestamp;
5313 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
5314 GST_BUFFER_PTS (buf) = GST_CLOCK_TIME_NONE;
5317 GST_BUFFER_DTS (buf) = timestamp;
5319 GST_BUFFER_DURATION (buf) = duration;
5320 GST_BUFFER_OFFSET (buf) = out_offset;
5321 GST_BUFFER_OFFSET_END (buf) = out_offset_end;
5323 /* mark discont when pending */
5324 if (stream->discont) {
5325 GST_DEBUG_OBJECT (avi, "setting DISCONT flag");
5326 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
5327 stream->discont = FALSE;
5329 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
5332 gst_avi_demux_add_assoc (avi, stream, timestamp, offset, keyframe);
5335 /* update current position in the segment */
5336 avi->segment.position = timestamp;
5338 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %" G_GSIZE_FORMAT ", ts %"
5339 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
5340 ", off_end %" G_GUINT64_FORMAT,
5341 gst_buffer_get_size (buf), GST_TIME_ARGS (timestamp),
5342 GST_TIME_ARGS (duration), out_offset, out_offset_end);
5344 if (stream->alignment > 1)
5345 buf = gst_avi_demux_align_buffer (avi, buf, stream->alignment);
5346 ret = gst_pad_push (stream->pad, buf);
5348 /* mark as processed, we increment the frame and byte counters then
5349 * leave the while loop and return the GstFlowReturn */
5352 if (avi->segment.rate < 0) {
5353 if (timestamp > avi->segment.stop && ret == GST_FLOW_EOS) {
5354 /* In reverse playback we can get a GST_FLOW_EOS when
5355 * we are at the end of the segment, so we just need to jump
5356 * back to the previous section. */
5357 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
5362 /* move to next item */
5363 ret = gst_avi_demux_advance (avi, stream, ret);
5366 ret = gst_avi_demux_combine_flows (avi, stream, ret);
5367 } while (!processed);
5375 GST_DEBUG_OBJECT (avi, "No samples left for any streams - EOS");
5381 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
5382 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
5383 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (avi->segment.stop));
5385 /* move to next stream */
5390 GST_DEBUG_OBJECT (avi, "pull range failed: pos=%" G_GUINT64_FORMAT
5391 " size=%" G_GUINT64_FORMAT, offset, size);
5396 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
5397 ", only got %" G_GSIZE_FORMAT "/%" G_GUINT64_FORMAT
5398 " bytes (truncated file?)", offset, gst_buffer_get_size (buf), size);
5399 gst_buffer_unref (buf);
5406 * Read data. If we have an index it delegates to
5407 * gst_avi_demux_process_next_entry().
5409 static GstFlowReturn
5410 gst_avi_demux_stream_data (GstAviDemux * avi)
5415 GstFlowReturn res = GST_FLOW_OK;
5417 if (G_UNLIKELY (avi->have_eos)) {
5418 /* Clean adapter, we're done */
5419 gst_adapter_clear (avi->adapter);
5420 return GST_FLOW_EOS;
5423 if (G_UNLIKELY (avi->todrop)) {
5426 if ((drop = gst_adapter_available (avi->adapter))) {
5427 if (drop > avi->todrop)
5429 GST_DEBUG_OBJECT (avi, "Dropping %d bytes", drop);
5430 gst_adapter_flush (avi->adapter, drop);
5431 avi->todrop -= drop;
5432 avi->offset += drop;
5436 /* Iterate until need more data, so adapter won't grow too much */
5438 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
5442 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
5443 GST_FOURCC_ARGS (tag), size);
5445 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
5446 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
5447 GST_LOG ("Chunk ok");
5448 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
5449 GST_DEBUG ("Found sub-index tag");
5450 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
5451 /* accept 0 size buffer here */
5452 avi->abort_buffering = FALSE;
5453 GST_DEBUG (" skipping %d bytes for now", size);
5454 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
5457 } else if (tag == GST_RIFF_TAG_RIFF) {
5458 /* RIFF tags can appear in ODML files, just jump over them */
5459 if (gst_adapter_available (avi->adapter) >= 12) {
5460 GST_DEBUG ("Found RIFF tag, skipping RIFF header");
5461 gst_adapter_flush (avi->adapter, 12);
5465 } else if (tag == GST_RIFF_TAG_idx1) {
5466 GST_DEBUG ("Found index tag");
5467 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
5468 /* accept 0 size buffer here */
5469 avi->abort_buffering = FALSE;
5470 GST_DEBUG (" skipping %d bytes for now", size);
5471 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
5474 } else if (tag == GST_RIFF_TAG_LIST) {
5475 /* movi chunks might be grouped in rec list */
5476 if (gst_adapter_available (avi->adapter) >= 12) {
5477 GST_DEBUG ("Found LIST tag, skipping LIST header");
5478 gst_adapter_flush (avi->adapter, 12);
5482 } else if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ) {
5483 /* rec list might contain JUNK chunks */
5484 GST_DEBUG ("Found JUNK tag");
5485 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
5486 /* accept 0 size buffer here */
5487 avi->abort_buffering = FALSE;
5488 GST_DEBUG (" skipping %d bytes for now", size);
5489 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
5493 GST_DEBUG ("No more stream chunks, send EOS");
5494 avi->have_eos = TRUE;
5495 return GST_FLOW_EOS;
5498 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
5499 /* supposedly one hopes to catch a nicer chunk later on ... */
5500 /* FIXME ?? give up here rather than possibly ending up going
5501 * through the whole file */
5502 if (avi->abort_buffering) {
5503 avi->abort_buffering = FALSE;
5505 gst_adapter_flush (avi->adapter, 8);
5512 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
5513 GST_FOURCC_ARGS (tag), size);
5515 stream_nr = CHUNKID_TO_STREAMNR (tag);
5517 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
5519 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
5520 stream_nr, GST_FOURCC_ARGS (tag));
5521 avi->offset += 8 + GST_ROUND_UP_2 (size);
5522 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
5524 GstAviStream *stream;
5525 GstClockTime next_ts = 0;
5526 GstBuffer *buf = NULL;
5530 gboolean saw_desired_kf = stream_nr != avi->main_stream
5531 || avi->offset >= avi->seek_kf_offset;
5533 if (stream_nr == avi->main_stream && avi->offset == avi->seek_kf_offset) {
5534 GST_DEBUG_OBJECT (avi, "Desired keyframe reached");
5535 avi->seek_kf_offset = 0;
5538 if (saw_desired_kf) {
5539 gst_adapter_flush (avi->adapter, 8);
5542 buf = gst_adapter_take_buffer (avi->adapter, GST_ROUND_UP_2 (size));
5543 /* patch the size */
5544 gst_buffer_resize (buf, 0, size);
5549 GST_DEBUG_OBJECT (avi,
5550 "Desired keyframe not yet reached, flushing chunk");
5551 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
5555 offset = avi->offset;
5557 avi->offset += 8 + GST_ROUND_UP_2 (size);
5559 stream = &avi->stream[stream_nr];
5561 /* set delay (if any)
5562 if (stream->strh->init_frames == stream->current_frame &&
5564 stream->delay = next_ts;
5567 /* parsing of corresponding header may have failed */
5568 if (G_UNLIKELY (!stream->pad)) {
5569 GST_WARNING_OBJECT (avi, "no pad for stream ID %" GST_FOURCC_FORMAT,
5570 GST_FOURCC_ARGS (tag));
5572 gst_buffer_unref (buf);
5574 /* get time of this buffer */
5575 gst_pad_query_position (stream->pad, GST_FORMAT_TIME,
5576 (gint64 *) & next_ts);
5579 gst_avi_demux_add_assoc (avi, stream, next_ts, offset, FALSE);
5582 /* increment our positions */
5583 stream->current_entry++;
5584 /* as in pull mode, 'total' is either bytes (CBR) or frames (VBR) */
5585 if (stream->strh->type == GST_RIFF_FCC_auds && stream->is_vbr) {
5586 gint blockalign = stream->strf.auds->blockalign;
5588 stream->current_total += DIV_ROUND_UP (size, blockalign);
5590 stream->current_total++;
5592 stream->current_total += size;
5594 GST_LOG_OBJECT (avi, "current entry %u, total %u",
5595 stream->current_entry, stream->current_total);
5597 /* update current position in the segment */
5598 avi->segment.position = next_ts;
5600 if (saw_desired_kf && buf) {
5601 GstClockTime dur_ts = 0;
5603 /* invert the picture if needed, and append palette for RGB8P */
5604 buf = gst_avi_demux_invert (stream, buf);
5606 gst_pad_query_position (stream->pad, GST_FORMAT_TIME,
5607 (gint64 *) & dur_ts);
5609 GST_BUFFER_DTS (buf) = next_ts;
5610 GST_BUFFER_PTS (buf) = GST_CLOCK_TIME_NONE;
5611 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
5612 if (stream->strh->type == GST_RIFF_FCC_vids) {
5613 GST_BUFFER_OFFSET (buf) = stream->current_entry - 1;
5614 GST_BUFFER_OFFSET_END (buf) = stream->current_entry;
5616 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
5617 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
5620 GST_DEBUG_OBJECT (avi,
5621 "Pushing buffer with time=%" GST_TIME_FORMAT ", duration %"
5622 GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
5623 " and size %d over pad %s", GST_TIME_ARGS (next_ts),
5624 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
5625 GST_BUFFER_OFFSET (buf), size, GST_PAD_NAME (stream->pad));
5627 /* mark discont when pending */
5628 if (G_UNLIKELY (stream->discont)) {
5629 GST_DEBUG_OBJECT (avi, "Setting DISCONT");
5630 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
5631 stream->discont = FALSE;
5633 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
5636 if (stream->alignment > 1)
5637 buf = gst_avi_demux_align_buffer (avi, buf, stream->alignment);
5638 res = gst_pad_push (stream->pad, buf);
5642 res = gst_avi_demux_combine_flows (avi, stream, res);
5643 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5644 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
5656 * Send pending tags.
5659 push_tag_lists (GstAviDemux * avi)
5667 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
5669 for (i = 0; i < avi->num_streams; i++) {
5670 GstAviStream *stream = &avi->stream[i];
5671 GstPad *pad = stream->pad;
5673 tags = stream->taglist;
5676 GST_DEBUG_OBJECT (pad, "Tags: %" GST_PTR_FORMAT, tags);
5678 gst_pad_push_event (pad, gst_event_new_tag (tags));
5679 stream->taglist = NULL;
5683 if (!(tags = avi->globaltags))
5684 tags = gst_tag_list_new_empty ();
5686 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
5687 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
5689 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, tags);
5690 gst_tag_list_set_scope (tags, GST_TAG_SCOPE_GLOBAL);
5691 gst_avi_demux_push_event (avi, gst_event_new_tag (tags));
5692 avi->globaltags = NULL;
5693 avi->got_tags = FALSE;
5697 gst_avi_demux_loop (GstPad * pad)
5700 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
5702 switch (avi->state) {
5703 case GST_AVI_DEMUX_START:
5704 res = gst_avi_demux_stream_init_pull (avi);
5705 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5706 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5709 avi->state = GST_AVI_DEMUX_HEADER;
5711 case GST_AVI_DEMUX_HEADER:
5712 res = gst_avi_demux_stream_header_pull (avi);
5713 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5714 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5717 avi->state = GST_AVI_DEMUX_MOVI;
5719 case GST_AVI_DEMUX_MOVI:
5720 if (G_UNLIKELY (avi->seg_event)) {
5721 gst_avi_demux_push_event (avi, avi->seg_event);
5722 avi->seg_event = NULL;
5724 if (G_UNLIKELY (avi->got_tags)) {
5725 push_tag_lists (avi);
5727 /* process each index entry in turn */
5728 res = gst_avi_demux_loop_data (avi);
5730 /* pause when error */
5731 if (G_UNLIKELY (res != GST_FLOW_OK)) {
5732 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
5737 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
5738 res = GST_FLOW_ERROR;
5747 gboolean push_eos = FALSE;
5748 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
5749 gst_pad_pause_task (avi->sinkpad);
5751 if (res == GST_FLOW_EOS) {
5752 /* handle end-of-stream/segment */
5753 /* so align our position with the end of it, if there is one
5754 * this ensures a subsequent will arrive at correct base/acc time */
5755 if (avi->segment.rate > 0.0 &&
5756 GST_CLOCK_TIME_IS_VALID (avi->segment.stop))
5757 avi->segment.position = avi->segment.stop;
5758 else if (avi->segment.rate < 0.0)
5759 avi->segment.position = avi->segment.start;
5760 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5765 if ((stop = avi->segment.stop) == -1)
5766 stop = avi->segment.duration;
5768 GST_INFO_OBJECT (avi, "sending segment_done");
5771 gst_message_new_segment_done (GST_OBJECT_CAST (avi),
5772 GST_FORMAT_TIME, stop);
5773 if (avi->segment_seqnum)
5774 gst_message_set_seqnum (msg, avi->segment_seqnum);
5775 gst_element_post_message (GST_ELEMENT_CAST (avi), msg);
5777 event = gst_event_new_segment_done (GST_FORMAT_TIME, stop);
5778 if (avi->segment_seqnum)
5779 gst_event_set_seqnum (event, avi->segment_seqnum);
5780 gst_avi_demux_push_event (avi, event);
5784 } else if (res == GST_FLOW_NOT_LINKED || res < GST_FLOW_EOS) {
5785 /* for fatal errors we post an error message, wrong-state is
5786 * not fatal because it happens due to flushes and only means
5787 * that we should stop now. */
5788 GST_ELEMENT_FLOW_ERROR (avi, res);
5794 GST_INFO_OBJECT (avi, "sending eos");
5795 event = gst_event_new_eos ();
5796 if (avi->segment_seqnum)
5797 gst_event_set_seqnum (event, avi->segment_seqnum);
5798 if (!gst_avi_demux_push_event (avi, event) && (res == GST_FLOW_EOS)) {
5799 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
5800 (NULL), ("got eos but no streams (yet)"));
5807 static GstFlowReturn
5808 gst_avi_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
5811 GstAviDemux *avi = GST_AVI_DEMUX (parent);
5814 if (GST_BUFFER_IS_DISCONT (buf)) {
5815 GST_DEBUG_OBJECT (avi, "got DISCONT");
5816 gst_adapter_clear (avi->adapter);
5817 /* mark all streams DISCONT */
5818 for (i = 0; i < avi->num_streams; i++)
5819 avi->stream[i].discont = TRUE;
5822 GST_DEBUG ("Store %" G_GSIZE_FORMAT " bytes in adapter",
5823 gst_buffer_get_size (buf));
5824 gst_adapter_push (avi->adapter, buf);
5826 switch (avi->state) {
5827 case GST_AVI_DEMUX_START:
5828 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
5829 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
5833 case GST_AVI_DEMUX_HEADER:
5834 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
5835 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
5839 case GST_AVI_DEMUX_MOVI:
5840 if (G_UNLIKELY (avi->seg_event)) {
5841 gst_avi_demux_push_event (avi, avi->seg_event);
5842 avi->seg_event = NULL;
5844 if (G_UNLIKELY (avi->got_tags)) {
5845 push_tag_lists (avi);
5847 res = gst_avi_demux_stream_data (avi);
5849 case GST_AVI_DEMUX_SEEK:
5855 /* obtain and parse indexes */
5856 if (avi->stream[0].indexes && !gst_avi_demux_read_subindexes_push (avi))
5857 /* seek in subindex read function failed */
5860 if (!avi->stream[0].indexes && !avi->have_index
5861 && avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
5862 gst_avi_demux_stream_index_push (avi);
5864 if (avi->have_index) {
5865 /* use the indexes now to construct nice durations */
5866 gst_avi_demux_calculate_durations_from_index (avi);
5868 /* still parsing indexes */
5872 GST_OBJECT_LOCK (avi);
5873 event = avi->seek_event;
5874 avi->seek_event = NULL;
5875 GST_OBJECT_UNLOCK (avi);
5877 /* calculate and perform seek */
5878 if (!avi_demux_handle_seek_push (avi, avi->sinkpad, event)) {
5879 gst_event_unref (event);
5883 gst_event_unref (event);
5884 avi->state = GST_AVI_DEMUX_MOVI;
5888 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
5889 ("Illegal internal state"));
5890 res = GST_FLOW_ERROR;
5894 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
5895 gst_flow_get_name (res));
5897 if (G_UNLIKELY (avi->abort_buffering))
5898 goto abort_buffering;
5905 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("failed to read indexes"));
5906 return GST_FLOW_ERROR;
5910 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("push mode seek failed"));
5911 return GST_FLOW_ERROR;
5915 avi->abort_buffering = FALSE;
5916 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
5917 return GST_FLOW_ERROR;
5922 gst_avi_demux_sink_activate (GstPad * sinkpad, GstObject * parent)
5927 query = gst_query_new_scheduling ();
5929 if (!gst_pad_peer_query (sinkpad, query)) {
5930 gst_query_unref (query);
5934 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
5935 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
5936 gst_query_unref (query);
5941 GST_DEBUG_OBJECT (sinkpad, "activating pull");
5942 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
5946 GST_DEBUG_OBJECT (sinkpad, "activating push");
5947 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
5952 gst_avi_demux_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
5953 GstPadMode mode, gboolean active)
5956 GstAviDemux *avi = GST_AVI_DEMUX (parent);
5959 case GST_PAD_MODE_PULL:
5961 avi->streaming = FALSE;
5962 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
5965 res = gst_pad_stop_task (sinkpad);
5968 case GST_PAD_MODE_PUSH:
5970 GST_DEBUG ("avi: activating push/chain function");
5971 avi->streaming = TRUE;
5973 GST_DEBUG ("avi: deactivating push/chain function");
5986 gst_avi_demux_set_index (GstElement * element, GstIndex * index)
5988 GstAviDemux *avi = GST_AVI_DEMUX (element);
5990 GST_OBJECT_LOCK (avi);
5991 if (avi->element_index)
5992 gst_object_unref (avi->element_index);
5994 avi->element_index = gst_object_ref (index);
5996 avi->element_index = NULL;
5998 GST_OBJECT_UNLOCK (avi);
5999 /* object lock might be taken again */
6001 gst_index_get_writer_id (index, GST_OBJECT_CAST (element), &avi->index_id);
6002 GST_DEBUG_OBJECT (avi, "Set index %" GST_PTR_FORMAT, avi->element_index);
6006 gst_avi_demux_get_index (GstElement * element)
6008 GstIndex *result = NULL;
6009 GstAviDemux *avi = GST_AVI_DEMUX (element);
6011 GST_OBJECT_LOCK (avi);
6012 if (avi->element_index)
6013 result = gst_object_ref (avi->element_index);
6014 GST_OBJECT_UNLOCK (avi);
6016 GST_DEBUG_OBJECT (avi, "Returning index %" GST_PTR_FORMAT, result);
6022 static GstStateChangeReturn
6023 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
6025 GstStateChangeReturn ret;
6026 GstAviDemux *avi = GST_AVI_DEMUX (element);
6028 switch (transition) {
6029 case GST_STATE_CHANGE_READY_TO_PAUSED:
6030 avi->streaming = FALSE;
6031 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
6037 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
6038 if (ret == GST_STATE_CHANGE_FAILURE)
6041 switch (transition) {
6042 case GST_STATE_CHANGE_PAUSED_TO_READY:
6043 avi->have_index = FALSE;
6044 gst_avi_demux_reset (avi);