2 * Copyright (C) <1999> Erik Walthinsen <omega@temple-baptist.com>
3 * Copyright (C) <2006> Nokia Corporation (contact <stefan.kost@nokia.com>)
4 * Copyright (C) <2009-2010> STEricsson <benjamin.gaignard@stericsson.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 /* Element-Checklist-Version: 5 */
24 * SECTION:element-avidemux
26 * Demuxes an .avi file into raw or compressed audio and/or video streams.
28 * This element supports both push and pull-based scheduling, depending on the
29 * capabilities of the upstream elements.
32 * <title>Example launch line</title>
34 * gst-launch filesrc location=test.avi ! avidemux name=demux demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink demux.video_00 ! queue ! decodebin ! ffmpegcolorspace ! videoscale ! autovideosink
35 * ]| Play (parse and decode) an .avi file and try to output it to
36 * an automatically detected soundcard and videosink. If the AVI file contains
37 * compressed audio or video data, this will only work if you have the
38 * right decoder elements/plugins installed.
41 * Last reviewed on 2006-12-29 (0.10.6)
54 #include "gst/riff/riff-media.h"
55 #include "gstavidemux.h"
57 #include <gst/gst-i18n-plugin.h>
58 #include <gst/base/gstadapter.h>
61 #define DIV_ROUND_UP(s,v) (((s) + ((v)-1)) / (v))
63 #define GST_AVI_KEYFRAME 1
64 #ifdef AVIDEMUX_MODIFICATION
65 #define GST_AVI_NON_KEYFRAME 0
67 #define ENTRY_IS_KEYFRAME(e) ((e)->flags == GST_AVI_KEYFRAME)
68 #define ENTRY_SET_KEYFRAME(e) ((e)->flags = GST_AVI_KEYFRAME)
69 #define ENTRY_UNSET_KEYFRAME(e) ((e)->flags = 0)
72 GST_DEBUG_CATEGORY_STATIC (avidemux_debug);
73 #define GST_CAT_DEFAULT avidemux_debug
75 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
78 GST_STATIC_CAPS ("video/x-msvideo")
81 #ifdef AVIDEMUX_MODIFICATION
82 /*Modification: Frame type enumeraions used to generat the index-table */
96 H264_NUT_EOSTREAM = 11,
102 static void gst_avi_demux_base_init (GstAviDemuxClass * klass);
103 static void gst_avi_demux_class_init (GstAviDemuxClass * klass);
104 static void gst_avi_demux_init (GstAviDemux * avi);
105 static void gst_avi_demux_finalize (GObject * object);
107 static void gst_avi_demux_reset (GstAviDemux * avi);
110 static const GstEventMask *gst_avi_demux_get_event_mask (GstPad * pad);
112 static gboolean gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event);
113 static gboolean gst_avi_demux_handle_sink_event (GstPad * pad,
115 static gboolean gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event);
118 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
120 static const GstQueryType *gst_avi_demux_get_src_query_types (GstPad * pad);
121 static gboolean gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query);
122 static gboolean gst_avi_demux_src_convert (GstPad * pad, GstFormat src_format,
123 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
125 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
126 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
128 static gboolean gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
130 static void gst_avi_demux_loop (GstPad * pad);
131 static gboolean gst_avi_demux_sink_activate (GstPad * sinkpad);
132 static gboolean gst_avi_demux_sink_activate_pull (GstPad * sinkpad,
134 static gboolean gst_avi_demux_activate_push (GstPad * pad, gboolean active);
135 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstBuffer * buf);
137 static void gst_avi_demux_set_index (GstElement * element, GstIndex * index);
138 static GstIndex *gst_avi_demux_get_index (GstElement * element);
139 static GstStateChangeReturn gst_avi_demux_change_state (GstElement * element,
140 GstStateChange transition);
141 static void gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi);
142 static void gst_avi_demux_get_buffer_info (GstAviDemux * avi,
143 GstAviStream * stream, guint entry_n, GstClockTime * timestamp,
144 GstClockTime * ts_end, guint64 * offset, guint64 * offset_end);
146 static void gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf);
147 #ifdef AVIDEMUX_MODIFICATION
148 /*Modification: Added function to find out the frame_type for index-table generation */
150 gst_avi_demux_find_frame_type (GstAviStream *stream, GstBuffer *buf, int *frame_type);
154 static GstElementClass *parent_class = NULL;
158 typedef enum drmErrorCodes
165 DRM_NEVER_REGISTERED,
169 #define DIVX_SDK_PLUGIN_NAME "libmm_divxsdk.so"
171 static gboolean init_divx_plugin (GstAviDemux * avi)
174 avi->divx_handle = dlopen (DIVX_SDK_PLUGIN_NAME, RTLD_LAZY);
175 if (!avi->divx_handle) {
176 GST_ERROR ("dlopen failed [%s]", dlerror());
179 GST_DEBUG("dlopen success");
181 avi->divx_init = dlsym (avi->divx_handle, "divx_init");
182 if ((error = dlerror()) != NULL) {
183 GST_ERROR ("[%s][%d]", __func__, __LINE__);
187 avi->divx_commit = dlsym (avi->divx_handle, "divx_commit");
188 if ((error = dlerror()) != NULL) {
189 GST_ERROR ("[%s][%d] %p", __func__, __LINE__, avi->divx_commit);
193 avi->divx_decrypt_audio = dlsym (avi->divx_handle, "divx_decrypt_audio");
194 GST_ERROR ("[%s][%d] %p", __func__, __LINE__, avi->divx_decrypt_audio);
195 if ((error = dlerror()) != NULL) {
199 avi->divx_prepare_video_bitstream = dlsym (avi->divx_handle, "divx_prepare_video_bitstream");
200 if ((error = dlerror()) != NULL) {
201 GST_ERROR ("[%s][%d]", __func__, __LINE__);
205 avi->divx_finalize = dlsym (avi->divx_handle, "divx_finalize");
206 if ((error = dlerror()) != NULL) {
207 GST_ERROR ("[%s][%d]", __func__, __LINE__);
214 GST_ERROR ("error : %s", error);
215 dlclose(avi->divx_handle);
216 avi->divx_handle = NULL;
221 /* ---------------------------------------------------- DIVX DRM Code : Start -----------------------------------------------------------------*/
223 gst_avi_demux_init_divx_drm (GstAviDemux * avi, uint8_t* drm_info)
227 if (init_divx_plugin (avi) == FALSE) {
228 GST_ERROR_OBJECT (avi, "Loading plugin failed....");
232 avi->drmContext = avi->divx_init (drm_info, &error_code);
233 if (avi->drmContext) {
234 GST_DEBUG_OBJECT (avi,"%s init success: drmContext = %p\n", __func__, avi->drmContext);
236 GST_ERROR_OBJECT (avi,"%s failed to init... error code = %d \n", __func__, error_code);
240 error_code = avi->divx_commit (avi->drmContext);
241 if (error_code == DRM_SUCCESS) {
242 GST_DEBUG_OBJECT (avi,"%s commit success: drmContext = %p\n", __func__, avi->drmContext);
244 GST_ERROR_OBJECT (avi,"%s failed to commit... error code = %d \n", __func__, error_code);
252 gst_avi_demux_send_divx_tag (GstAviDemux * avi)
255 GstTagList *tags = NULL;
256 GST_ERROR_OBJECT (avi, "*********** posting divx drm tags!!!!!!!!!!!!!!!!!!");
257 tags = gst_tag_list_new_full ("drm_divx", "1", NULL);
259 ret = gst_avi_demux_push_event (avi, gst_event_new_tag (tags) );
260 GST_ERROR_OBJECT (avi, "*********** posting tags returns [%d] !!!!!!!!!!!!!!!!!!", ret);
264 /* ---------------------------------------------------- DIVX DRM Code : End -----------------------------------------------------------------*/
266 /* GObject methods */
269 gst_avi_demux_get_type (void)
271 static GType avi_demux_type = 0;
273 if (!avi_demux_type) {
274 static const GTypeInfo avi_demux_info = {
275 sizeof (GstAviDemuxClass),
276 (GBaseInitFunc) gst_avi_demux_base_init,
278 (GClassInitFunc) gst_avi_demux_class_init,
281 sizeof (GstAviDemux),
283 (GInstanceInitFunc) gst_avi_demux_init,
287 g_type_register_static (GST_TYPE_ELEMENT,
288 "GstAviDemux", &avi_demux_info, 0);
291 return avi_demux_type;
295 gst_avi_demux_base_init (GstAviDemuxClass * klass)
297 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
298 GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl;
299 GstCaps *audcaps, *vidcaps, *subcaps;
301 audcaps = gst_riff_create_audio_template_caps ();
302 gst_caps_append (audcaps, gst_caps_new_simple ("audio/x-avi-unknown", NULL));
303 audiosrctempl = gst_pad_template_new ("audio_%02d",
304 GST_PAD_SRC, GST_PAD_SOMETIMES, audcaps);
306 vidcaps = gst_riff_create_video_template_caps ();
307 gst_caps_append (vidcaps, gst_riff_create_iavs_template_caps ());
308 gst_caps_append (vidcaps, gst_caps_new_simple ("video/x-avi-unknown", NULL));
309 videosrctempl = gst_pad_template_new ("video_%02d",
310 GST_PAD_SRC, GST_PAD_SOMETIMES, vidcaps);
312 subcaps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
313 subsrctempl = gst_pad_template_new ("subtitle_%02d",
314 GST_PAD_SRC, GST_PAD_SOMETIMES, subcaps);
315 gst_element_class_add_pad_template (element_class, audiosrctempl);
316 gst_element_class_add_pad_template (element_class, videosrctempl);
317 gst_element_class_add_pad_template (element_class, subsrctempl);
318 gst_element_class_add_pad_template (element_class,
319 gst_static_pad_template_get (&sink_templ));
320 gst_element_class_set_details_simple (element_class, "Avi demuxer",
322 "Demultiplex an avi file into audio and video",
323 "Erik Walthinsen <omega@cse.ogi.edu>, "
324 "Wim Taymans <wim.taymans@chello.be>, "
325 "Thijs Vermeir <thijsvermeir@gmail.com>");
329 gst_avi_demux_class_init (GstAviDemuxClass * klass)
331 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
332 GObjectClass *gobject_class = (GObjectClass *) klass;
334 GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
335 0, "Demuxer for AVI streams");
337 parent_class = g_type_class_peek_parent (klass);
339 gobject_class->finalize = gst_avi_demux_finalize;
340 gstelement_class->change_state =
341 GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
343 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_avi_demux_set_index);
344 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_avi_demux_get_index);
347 gst_tag_register ("drm_divx", GST_TAG_FLAG_META,
350 _("a tag that is specific to DivX DRM File"),
356 gst_avi_demux_init (GstAviDemux * avi)
358 avi->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
359 gst_pad_set_activate_function (avi->sinkpad,
360 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate));
361 gst_pad_set_activatepull_function (avi->sinkpad,
362 GST_DEBUG_FUNCPTR (gst_avi_demux_sink_activate_pull));
363 gst_pad_set_activatepush_function (avi->sinkpad,
364 GST_DEBUG_FUNCPTR (gst_avi_demux_activate_push));
365 gst_pad_set_chain_function (avi->sinkpad,
366 GST_DEBUG_FUNCPTR (gst_avi_demux_chain));
367 gst_pad_set_event_function (avi->sinkpad,
368 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_sink_event));
369 gst_element_add_pad (GST_ELEMENT_CAST (avi), avi->sinkpad);
371 avi->adapter = gst_adapter_new ();
373 gst_avi_demux_reset (avi);
377 gst_avi_demux_finalize (GObject * object)
379 GstAviDemux *avi = GST_AVI_DEMUX (object);
381 GST_DEBUG ("AVI: finalize");
383 g_object_unref (avi->adapter);
385 G_OBJECT_CLASS (parent_class)->finalize (object);
389 gst_avi_demux_reset_stream (GstAviDemux * avi, GstAviStream * stream)
391 g_free (stream->strh);
392 g_free (stream->strf.data);
393 g_free (stream->name);
394 g_free (stream->index);
395 g_free (stream->indexes);
396 if (stream->initdata)
397 gst_buffer_unref (stream->initdata);
398 if (stream->extradata)
399 gst_buffer_unref (stream->extradata);
401 if (stream->exposed) {
402 gst_pad_set_active (stream->pad, FALSE);
403 gst_element_remove_pad (GST_ELEMENT_CAST (avi), stream->pad);
405 gst_object_unref (stream->pad);
407 if (stream->taglist) {
408 gst_tag_list_free (stream->taglist);
409 stream->taglist = NULL;
411 memset (stream, 0, sizeof (GstAviStream));
415 gst_avi_demux_reset (GstAviDemux * avi)
419 GST_DEBUG ("AVI: reset");
421 for (i = 0; i < avi->num_streams; i++)
422 gst_avi_demux_reset_stream (avi, &avi->stream[i]);
424 avi->header_state = GST_AVI_DEMUX_HEADER_TAG_LIST;
425 avi->num_streams = 0;
426 avi->num_v_streams = 0;
427 avi->num_a_streams = 0;
428 avi->num_t_streams = 0;
429 avi->main_stream = -1;
431 avi->state = GST_AVI_DEMUX_START;
433 avi->building_index = FALSE;
435 avi->index_offset = 0;
439 if (avi->element_index)
440 gst_object_unref (avi->element_index);
441 avi->element_index = NULL;
443 if (avi->close_seg_event) {
444 gst_event_unref (avi->close_seg_event);
445 avi->close_seg_event = NULL;
447 if (avi->seg_event) {
448 gst_event_unref (avi->seg_event);
449 avi->seg_event = NULL;
451 if (avi->seek_event) {
452 gst_event_unref (avi->seek_event);
453 avi->seek_event = NULL;
457 gst_tag_list_free (avi->globaltags);
458 avi->globaltags = NULL;
460 avi->got_tags = TRUE; /* we always want to push global tags */
461 avi->have_eos = FALSE;
462 avi->seekable = TRUE;
464 gst_adapter_clear (avi->adapter);
466 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
471 avi->divx_finalize (avi->drmContext);
472 free(avi->drmContext);
473 avi->drmContext = NULL;
476 if (avi->divx_handle)
478 dlclose(avi->divx_handle);
479 avi->divx_handle = NULL;
485 /* GstElement methods */
488 static const GstFormat *
489 gst_avi_demux_get_src_formats (GstPad * pad)
491 GstAviStream *stream = gst_pad_get_element_private (pad);
493 static const GstFormat src_a_formats[] = {
499 static const GstFormat src_v_formats[] = {
505 return (stream->strh->type == GST_RIFF_FCC_auds ?
506 src_a_formats : src_v_formats);
510 /* assumes stream->strf.auds->av_bps != 0 */
511 static inline GstClockTime
512 avi_stream_convert_bytes_to_time_unchecked (GstAviStream * stream,
515 return gst_util_uint64_scale_int (bytes, GST_SECOND,
516 stream->strf.auds->av_bps);
519 static inline guint64
520 avi_stream_convert_time_to_bytes_unchecked (GstAviStream * stream,
523 return gst_util_uint64_scale_int (time, stream->strf.auds->av_bps,
527 /* assumes stream->strh->rate != 0 */
528 static inline GstClockTime
529 avi_stream_convert_frames_to_time_unchecked (GstAviStream * stream,
532 return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
536 static inline guint64
537 avi_stream_convert_time_to_frames_unchecked (GstAviStream * stream,
540 return gst_util_uint64_scale (time, stream->strh->rate,
541 stream->strh->scale * GST_SECOND);
545 gst_avi_demux_src_convert (GstPad * pad,
546 GstFormat src_format,
547 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
549 GstAviStream *stream = gst_pad_get_element_private (pad);
553 "Received src_format:%s, src_value:%" G_GUINT64_FORMAT
554 ", dest_format:%s", gst_format_get_name (src_format), src_value,
555 gst_format_get_name (*dest_format));
557 if (G_UNLIKELY (src_format == *dest_format)) {
558 *dest_value = src_value;
561 if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
565 if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
566 (src_format == GST_FORMAT_BYTES
567 || *dest_format == GST_FORMAT_BYTES))) {
572 switch (src_format) {
573 case GST_FORMAT_TIME:
574 switch (*dest_format) {
575 case GST_FORMAT_BYTES:
576 *dest_value = gst_util_uint64_scale_int (src_value,
577 stream->strf.auds->av_bps, GST_SECOND);
579 case GST_FORMAT_DEFAULT:
581 gst_util_uint64_scale_round (src_value, stream->strh->rate,
582 stream->strh->scale * GST_SECOND);
589 case GST_FORMAT_BYTES:
590 switch (*dest_format) {
591 case GST_FORMAT_TIME:
592 if (stream->strf.auds->av_bps != 0) {
593 *dest_value = avi_stream_convert_bytes_to_time_unchecked (stream,
603 case GST_FORMAT_DEFAULT:
604 switch (*dest_format) {
605 case GST_FORMAT_TIME:
607 avi_stream_convert_frames_to_time_unchecked (stream, src_value);
620 "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
621 gst_format_get_name (*dest_format), *dest_value);
625 static const GstQueryType *
626 gst_avi_demux_get_src_query_types (GstPad * pad)
628 static const GstQueryType src_types[] = {
640 gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query)
643 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
645 GstAviStream *stream = gst_pad_get_element_private (pad);
647 if (!stream->strh || !stream->strf.data)
648 return gst_pad_query_default (pad, query);
650 switch (GST_QUERY_TYPE (query)) {
651 case GST_QUERY_POSITION:{
654 GST_DEBUG ("pos query for stream %u: frames %u, bytes %u",
655 stream->num, stream->current_entry, stream->current_total);
657 /* FIXME, this looks clumsy */
658 if (stream->strh->type == GST_RIFF_FCC_auds) {
659 if (stream->is_vbr) {
661 pos = gst_util_uint64_scale ((gint64) stream->current_entry *
662 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
663 GST_DEBUG_OBJECT (avi, "VBR convert frame %u, time %"
664 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
665 } else if (stream->strf.auds->av_bps != 0) {
667 pos = gst_util_uint64_scale (stream->current_total, GST_SECOND,
668 (guint64) stream->strf.auds->av_bps);
669 GST_DEBUG_OBJECT (avi,
670 "CBR convert bytes %u, time %" GST_TIME_FORMAT,
671 stream->current_total, GST_TIME_ARGS (pos));
672 } else if (stream->idx_n != 0 && stream->total_bytes != 0) {
673 /* calculate timestamps based on percentage of length */
674 guint64 xlen = avi->avih->us_frame *
675 avi->avih->tot_frames * GST_USECOND;
677 if (stream->is_vbr) {
678 pos = gst_util_uint64_scale (xlen, stream->current_entry,
680 GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
681 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
683 pos = gst_util_uint64_scale (xlen, stream->current_total,
684 stream->total_bytes);
685 GST_DEBUG_OBJECT (avi,
686 "CBR perc convert bytes %u, time %" GST_TIME_FORMAT,
687 stream->current_total, GST_TIME_ARGS (pos));
694 if (stream->strh->rate != 0) {
695 pos = gst_util_uint64_scale ((guint64) stream->current_entry *
696 stream->strh->scale, GST_SECOND, (guint64) stream->strh->rate);
698 pos = stream->current_entry * avi->avih->us_frame * GST_USECOND;
702 GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
703 gst_query_set_position (query, GST_FORMAT_TIME, pos);
705 GST_WARNING ("pos query failed");
708 case GST_QUERY_DURATION:
711 GstClockTime duration;
713 /* only act on audio or video streams */
714 if (stream->strh->type != GST_RIFF_FCC_auds &&
715 stream->strh->type != GST_RIFF_FCC_vids) {
720 /* take stream duration, fall back to avih duration */
721 if ((duration = stream->duration) == -1)
722 duration = avi->duration;
724 gst_query_parse_duration (query, &fmt, NULL);
727 case GST_FORMAT_TIME:
728 gst_query_set_duration (query, fmt, duration);
730 case GST_FORMAT_DEFAULT:
733 GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
736 if (stream->idx_n >= 0)
737 gst_query_set_duration (query, fmt, stream->idx_n);
738 else if (gst_pad_query_convert (pad, GST_FORMAT_TIME,
739 duration, &fmt, &dur))
740 gst_query_set_duration (query, fmt, dur);
749 case GST_QUERY_SEEKING:{
752 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
753 if (fmt == GST_FORMAT_TIME) {
754 gboolean seekable = TRUE;
756 if (avi->streaming) {
757 seekable = avi->seekable;
760 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
761 0, stream->duration);
766 case GST_QUERY_CONVERT:{
767 GstFormat src_fmt, dest_fmt;
768 gint64 src_val, dest_val;
770 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
771 if ((res = gst_avi_demux_src_convert (pad, src_fmt, src_val, &dest_fmt,
773 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
775 res = gst_pad_query_default (pad, query);
779 res = gst_pad_query_default (pad, query);
783 gst_object_unref (avi);
788 static const GstEventMask *
789 gst_avi_demux_get_event_mask (GstPad * pad)
791 static const GstEventMask masks[] = {
792 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
801 gst_avi_demux_seek_streams (GstAviDemux * avi, guint64 offset, gboolean before)
803 GstAviStream *stream;
804 GstIndexEntry *entry;
806 gint64 val, min = offset;
808 for (i = 0; i < avi->num_streams; i++) {
809 stream = &avi->stream[i];
811 entry = gst_index_get_assoc_entry (avi->element_index, stream->index_id,
812 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
813 GST_ASSOCIATION_FLAG_NONE, GST_FORMAT_BYTES, offset);
817 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
818 GST_DEBUG_OBJECT (avi, "stream %d, previous entry at %"
819 G_GUINT64_FORMAT, i, val);
827 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
828 stream->current_entry = 0;
829 stream->current_total = 0;
833 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
834 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT,
837 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &val);
838 stream->current_total = val;
839 gst_index_entry_assoc_map (entry, GST_FORMAT_DEFAULT, &val);
840 stream->current_entry = val;
847 gst_avi_demux_index_entry_offset_search (GstAviIndexEntry * entry,
850 if (entry->offset < *offset)
852 else if (entry->offset > *offset)
858 gst_avi_demux_seek_streams_index (GstAviDemux * avi, guint64 offset,
861 GstAviStream *stream;
862 GstAviIndexEntry *entry;
864 gint64 val, min = offset;
867 for (i = 0; i < avi->num_streams; i++) {
868 stream = &avi->stream[i];
870 /* compensate for chunk header */
873 gst_util_array_binary_search (stream->index, stream->idx_n,
874 sizeof (GstAviIndexEntry),
875 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
876 before ? GST_SEARCH_MODE_BEFORE : GST_SEARCH_MODE_AFTER, &offset, NULL);
880 index = entry - stream->index;
884 val = stream->index[index].offset;
885 GST_DEBUG_OBJECT (avi,
886 "stream %d, previous entry at %" G_GUINT64_FORMAT, i, val);
894 GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
895 stream->current_entry = 0;
896 stream->current_total = 0;
900 val = stream->index[index].offset - 8;
901 GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT, i,
904 stream->current_total = stream->index[index].total;
905 stream->current_entry = index;
911 #define GST_AVI_SEEK_PUSH_DISPLACE (4 * GST_SECOND)
914 gst_avi_demux_handle_sink_event (GstPad * pad, GstEvent * event)
917 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
919 GST_DEBUG_OBJECT (avi,
920 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
922 switch (GST_EVENT_TYPE (event)) {
923 case GST_EVENT_NEWSEGMENT:
927 gint64 start, stop, time, offset = 0;
931 /* some debug output */
932 gst_segment_init (&segment, GST_FORMAT_UNDEFINED);
933 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
934 &start, &stop, &time);
935 gst_segment_set_newsegment_full (&segment, update, rate, arate, format,
937 GST_DEBUG_OBJECT (avi,
938 "received format %d newsegment %" GST_SEGMENT_FORMAT, format,
941 /* chain will send initial newsegment after pads have been added */
942 if (avi->state != GST_AVI_DEMUX_MOVI) {
943 GST_DEBUG_OBJECT (avi, "still starting, eating event");
947 /* we only expect a BYTE segment, e.g. following a seek */
948 if (format != GST_FORMAT_BYTES) {
949 GST_DEBUG_OBJECT (avi, "unsupported segment format, ignoring");
953 if (avi->have_index) {
954 GstAviIndexEntry *entry;
955 guint i = 0, index = 0, k = 0;
956 GstAviStream *stream;
958 /* compensate chunk header, stored index offset points after header */
960 /* find which stream we're on */
962 stream = &avi->stream[i];
964 /* find the index for start bytes offset */
965 entry = gst_util_array_binary_search (stream->index,
966 stream->idx_n, sizeof (GstAviIndexEntry),
967 (GCompareDataFunc) gst_avi_demux_index_entry_offset_search,
968 GST_SEARCH_MODE_AFTER, &start, NULL);
972 index = entry - stream->index;
974 /* we are on the stream with a chunk start offset closest to start */
975 if (!offset || stream->index[index].offset < offset) {
976 offset = stream->index[index].offset;
979 /* exact match needs no further searching */
980 if (stream->index[index].offset == start)
982 } while (++i < avi->num_streams);
985 stream = &avi->stream[k];
987 /* so we have no idea what is to come, or where we are */
989 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
993 /* get the ts corresponding to start offset bytes for the stream */
994 gst_avi_demux_get_buffer_info (avi, stream, index,
995 (GstClockTime *) & time, NULL, NULL, NULL);
996 } else if (avi->element_index) {
997 GstIndexEntry *entry;
999 /* Let's check if we have an index entry for this position */
1000 entry = gst_index_get_assoc_entry (avi->element_index, avi->index_id,
1001 GST_INDEX_LOOKUP_AFTER, GST_ASSOCIATION_FLAG_NONE,
1002 GST_FORMAT_BYTES, start);
1004 /* we can not go where we have not yet been before ... */
1006 GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
1010 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &time);
1011 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &offset);
1013 GST_WARNING_OBJECT (avi, "no index data, forcing EOS");
1017 stop = GST_CLOCK_TIME_NONE;
1019 /* compensate for slack */
1021 time += GST_AVI_SEEK_PUSH_DISPLACE;
1022 /* set up segment and send downstream */
1023 gst_segment_set_newsegment_full (&avi->segment, update, rate, arate,
1024 GST_FORMAT_TIME, time, stop, time);
1025 GST_DEBUG_OBJECT (avi, "Pushing newseg update %d, rate %g, "
1026 "applied rate %g, format %d, start %" G_GINT64_FORMAT ", "
1027 "stop %" G_GINT64_FORMAT, update, rate, arate, GST_FORMAT_TIME,
1029 gst_avi_demux_push_event (avi,
1030 gst_event_new_new_segment_full (update, rate, arate, GST_FORMAT_TIME,
1033 GST_DEBUG_OBJECT (avi, "next chunk expected at %" G_GINT64_FORMAT, start);
1035 /* adjust state for streaming thread accordingly */
1036 if (avi->have_index)
1037 gst_avi_demux_seek_streams_index (avi, offset, FALSE);
1039 gst_avi_demux_seek_streams (avi, offset, FALSE);
1041 /* set up streaming thread */
1042 g_assert (offset >= start);
1043 avi->offset = start;
1044 avi->todrop = offset - start;
1047 gst_event_unref (event);
1051 /* set up for EOS */
1052 avi->have_eos = TRUE;
1057 if (avi->state != GST_AVI_DEMUX_MOVI) {
1058 gst_event_unref (event);
1059 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
1060 (NULL), ("got eos and didn't receive a complete header object"));
1061 } else if (!gst_avi_demux_push_event (avi, event)) {
1062 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
1063 (NULL), ("got eos but no streams (yet)"));
1067 case GST_EVENT_FLUSH_STOP:
1071 gst_adapter_clear (avi->adapter);
1072 avi->have_eos = FALSE;
1073 for (i = 0; i < avi->num_streams; i++) {
1074 avi->stream[i].last_flow = GST_FLOW_OK;
1075 avi->stream[i].discont = TRUE;
1077 /* fall through to default case so that the event gets passed downstream */
1080 res = gst_pad_event_default (pad, event);
1084 gst_object_unref (avi);
1090 gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event)
1092 gboolean res = TRUE;
1093 GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
1095 GST_DEBUG_OBJECT (avi,
1096 "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
1098 switch (GST_EVENT_TYPE (event)) {
1099 case GST_EVENT_SEEK:
1100 if (!avi->streaming) {
1101 res = gst_avi_demux_handle_seek (avi, pad, event);
1103 res = gst_avi_demux_handle_seek_push (avi, pad, event);
1105 gst_event_unref (event);
1108 case GST_EVENT_NAVIGATION:
1110 gst_event_unref (event);
1113 res = gst_pad_event_default (pad, event);
1117 gst_object_unref (avi);
1122 /* streaming helper (push) */
1125 * gst_avi_demux_peek_chunk_info:
1127 * @tag: holder for tag
1128 * @size: holder for tag size
1130 * Peek next chunk info (tag and size)
1132 * Returns: TRUE when one chunk info has been got
1135 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
1137 const guint8 *data = NULL;
1139 if (gst_adapter_available (avi->adapter) < 8)
1142 data = gst_adapter_peek (avi->adapter, 8);
1143 *tag = GST_READ_UINT32_LE (data);
1144 *size = GST_READ_UINT32_LE (data + 4);
1150 * gst_avi_demux_peek_chunk:
1152 * @tag: holder for tag
1153 * @size: holder for tag size
1155 * Peek enough data for one full chunk
1157 * Returns: %TRUE when one chunk has been got
1160 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
1162 guint32 peek_size = 0;
1165 if (!gst_avi_demux_peek_chunk_info (avi, tag, size))
1168 /* size 0 -> empty data buffer would surprise most callers,
1169 * large size -> do not bother trying to squeeze that into adapter,
1170 * so we throw poor man's exception, which can be caught if caller really
1171 * wants to handle 0 size chunk */
1172 if (!(*size) || (*size) >= (1 << 30))
1175 peek_size = (*size + 1) & ~1;
1176 available = gst_adapter_available (avi->adapter);
1178 GST_DEBUG_OBJECT (avi,
1179 "Need to peek chunk of %d bytes to read chunk %" GST_FOURCC_FORMAT
1180 ", %d bytes available", *size, GST_FOURCC_ARGS (*tag), available);
1182 if (available < (8 + peek_size))
1190 GST_INFO_OBJECT (avi, "Failed to peek");
1195 GST_INFO_OBJECT (avi,
1196 "Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT, *size,
1197 GST_FOURCC_ARGS (*tag));
1198 /* chain should give up */
1199 avi->abort_buffering = TRUE;
1204 GST_INFO_OBJECT (avi, "need more %d < %" G_GUINT32_FORMAT,
1205 available, 8 + peek_size);
1213 * gst_avi_demux_parse_file_header:
1214 * @element: caller element (used for errors/debug).
1215 * @buf: input data to be used for parsing.
1217 * "Open" a RIFF/AVI file. The buffer should be at least 12
1218 * bytes long. Takes ownership of @buf.
1220 * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
1221 * Throws an error, caller should error out (fatal).
1224 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
1229 stamp = gst_util_get_timestamp ();
1231 /* riff_parse posts an error */
1232 if (!gst_riff_parse_file_header (element, buf, &doctype))
1235 if (doctype != GST_RIFF_RIFF_AVI)
1238 stamp = gst_util_get_timestamp () - stamp;
1239 GST_DEBUG_OBJECT (element, "header parsing took %" GST_TIME_FORMAT,
1240 GST_TIME_ARGS (stamp));
1247 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
1248 ("File is not an AVI file: %" GST_FOURCC_FORMAT,
1249 GST_FOURCC_ARGS (doctype)));
1255 * Read AVI file tag when streaming
1257 static GstFlowReturn
1258 gst_avi_demux_stream_init_push (GstAviDemux * avi)
1260 if (gst_adapter_available (avi->adapter) >= 12) {
1263 tmp = gst_adapter_take_buffer (avi->adapter, 12);
1265 GST_DEBUG ("Parsing avi header");
1266 if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), tmp)) {
1267 return GST_FLOW_ERROR;
1269 GST_DEBUG ("header ok");
1272 avi->state = GST_AVI_DEMUX_HEADER;
1280 static GstFlowReturn
1281 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
1284 GstBuffer *buf = NULL;
1286 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
1287 if (res != GST_FLOW_OK)
1289 else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
1299 GST_DEBUG_OBJECT (avi, "error parsing file header");
1300 return GST_FLOW_ERROR;
1304 /* AVI header handling */
1306 * gst_avi_demux_parse_avih:
1307 * @avi: caller element (used for errors/debug).
1308 * @buf: input data to be used for parsing.
1309 * @avih: pointer to structure (filled in by function) containing
1310 * stream information (such as flags, number of streams, etc.).
1312 * Read 'avih' header. Discards buffer after use.
1314 * Returns: TRUE on success, FALSE otherwise. Throws an error if
1315 * the header is invalid. The caller should error out
1319 gst_avi_demux_parse_avih (GstAviDemux * avi,
1320 GstBuffer * buf, gst_riff_avih ** _avih)
1322 gst_riff_avih *avih;
1327 if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_avih))
1328 goto avih_too_small;
1330 avih = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1332 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1333 avih->us_frame = GUINT32_FROM_LE (avih->us_frame);
1334 avih->max_bps = GUINT32_FROM_LE (avih->max_bps);
1335 avih->pad_gran = GUINT32_FROM_LE (avih->pad_gran);
1336 avih->flags = GUINT32_FROM_LE (avih->flags);
1337 avih->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
1338 avih->init_frames = GUINT32_FROM_LE (avih->init_frames);
1339 avih->streams = GUINT32_FROM_LE (avih->streams);
1340 avih->bufsize = GUINT32_FROM_LE (avih->bufsize);
1341 avih->width = GUINT32_FROM_LE (avih->width);
1342 avih->height = GUINT32_FROM_LE (avih->height);
1343 avih->scale = GUINT32_FROM_LE (avih->scale);
1344 avih->rate = GUINT32_FROM_LE (avih->rate);
1345 avih->start = GUINT32_FROM_LE (avih->start);
1346 avih->length = GUINT32_FROM_LE (avih->length);
1350 GST_INFO_OBJECT (avi, "avih tag found:");
1351 GST_INFO_OBJECT (avi, " us_frame %u", avih->us_frame);
1352 GST_INFO_OBJECT (avi, " max_bps %u", avih->max_bps);
1353 GST_INFO_OBJECT (avi, " pad_gran %u", avih->pad_gran);
1354 GST_INFO_OBJECT (avi, " flags 0x%08x", avih->flags);
1355 GST_INFO_OBJECT (avi, " tot_frames %u", avih->tot_frames);
1356 GST_INFO_OBJECT (avi, " init_frames %u", avih->init_frames);
1357 GST_INFO_OBJECT (avi, " streams %u", avih->streams);
1358 GST_INFO_OBJECT (avi, " bufsize %u", avih->bufsize);
1359 GST_INFO_OBJECT (avi, " width %u", avih->width);
1360 GST_INFO_OBJECT (avi, " height %u", avih->height);
1361 GST_INFO_OBJECT (avi, " scale %u", avih->scale);
1362 GST_INFO_OBJECT (avi, " rate %u", avih->rate);
1363 GST_INFO_OBJECT (avi, " start %u", avih->start);
1364 GST_INFO_OBJECT (avi, " length %u", avih->length);
1367 gst_buffer_unref (buf);
1369 if (avih->us_frame != 0 && avih->tot_frames != 0)
1371 (guint64) avih->us_frame * (guint64) avih->tot_frames * 1000;
1373 avi->duration = GST_CLOCK_TIME_NONE;
1375 GST_INFO_OBJECT (avi, " header duration %" GST_TIME_FORMAT,
1376 GST_TIME_ARGS (avi->duration));
1383 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No buffer"));
1388 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
1389 ("Too small avih (%d available, %d needed)",
1390 GST_BUFFER_SIZE (buf), (int) sizeof (gst_riff_avih)));
1391 gst_buffer_unref (buf);
1397 * gst_avi_demux_parse_superindex:
1398 * @avi: caller element (used for debugging/errors).
1399 * @buf: input data to use for parsing.
1400 * @locations: locations in the file (byte-offsets) that contain
1401 * the actual indexes (see get_avi_demux_parse_subindex()).
1402 * The array ends with GST_BUFFER_OFFSET_NONE.
1404 * Reads superindex (openDML-2 spec stuff) from the provided data.
1406 * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
1407 * on error, but they are not fatal.
1410 gst_avi_demux_parse_superindex (GstAviDemux * avi,
1411 GstBuffer * buf, guint64 ** _indexes)
1421 size = buf ? GST_BUFFER_SIZE (buf) : 0;
1425 data = GST_BUFFER_DATA (buf);
1427 /* check type of index. The opendml2 specs state that
1428 * there should be 4 dwords per array entry. Type can be
1429 * either frame or field (and we don't care). */
1430 if (GST_READ_UINT16_LE (data) != 4 ||
1431 (data[2] & 0xfe) != 0x0 || data[3] != 0x0) {
1432 GST_WARNING_OBJECT (avi,
1433 "Superindex for stream has unexpected "
1434 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1435 GST_READ_UINT16_LE (data), data[2], data[3]);
1436 bpe = GST_READ_UINT16_LE (data) * 4;
1438 num = GST_READ_UINT32_LE (&data[4]);
1440 GST_DEBUG_OBJECT (avi, "got %d indexes", num);
1442 /* this can't work out well ... */
1443 if (num > G_MAXUINT32 >> 1 || bpe < 8) {
1444 goto invalid_params;
1447 indexes = g_new (guint64, num + 1);
1448 for (i = 0; i < num; i++) {
1449 if (size < 24 + bpe * (i + 1))
1451 indexes[i] = GST_READ_UINT64_LE (&data[24 + bpe * i]);
1452 GST_DEBUG_OBJECT (avi, "index %d at %" G_GUINT64_FORMAT, i, indexes[i]);
1454 indexes[i] = GST_BUFFER_OFFSET_NONE;
1455 *_indexes = indexes;
1457 gst_buffer_unref (buf);
1464 GST_ERROR_OBJECT (avi,
1465 "Not enough data to parse superindex (%d available, 24 needed)", size);
1467 gst_buffer_unref (buf);
1472 GST_ERROR_OBJECT (avi, "invalid index parameters (num = %d, bpe = %d)",
1475 gst_buffer_unref (buf);
1480 /* add an entry to the index of a stream. @num should be an estimate of the
1481 * total amount of index entries for all streams and is used to dynamically
1482 * allocate memory for the index entries. */
1483 static inline gboolean
1484 gst_avi_demux_add_index (GstAviDemux * avi, GstAviStream * stream,
1485 guint num, GstAviIndexEntry * entry)
1487 /* ensure index memory */
1488 if (G_UNLIKELY (stream->idx_n >= stream->idx_max)) {
1489 guint idx_max = stream->idx_max;
1490 GstAviIndexEntry *new_idx;
1492 /* we need to make some more room */
1494 /* initial size guess, assume each stream has an equal amount of entries,
1495 * overshoot with at least 8K */
1496 idx_max = (num / avi->num_streams) + (8192 / sizeof (GstAviIndexEntry));
1498 idx_max += 8192 / sizeof (GstAviIndexEntry);
1499 GST_DEBUG_OBJECT (avi, "expanded index from %u to %u",
1500 stream->idx_max, idx_max);
1502 new_idx = g_try_renew (GstAviIndexEntry, stream->index, idx_max);
1503 /* out of memory, if this fails stream->index is untouched. */
1504 if (G_UNLIKELY (!new_idx))
1507 stream->index = new_idx;
1508 stream->idx_max = idx_max;
1511 /* update entry total and stream stats. The entry total can be converted to
1512 * the timestamp of the entry easily. */
1513 if (stream->strh->type == GST_RIFF_FCC_auds) {
1516 if (stream->is_vbr) {
1517 entry->total = stream->total_blocks;
1519 entry->total = stream->total_bytes;
1521 blockalign = stream->strf.auds->blockalign;
1523 stream->total_blocks += DIV_ROUND_UP (entry->size, blockalign);
1525 stream->total_blocks++;
1527 if (stream->is_vbr) {
1528 entry->total = stream->idx_n;
1530 entry->total = stream->total_bytes;
1533 stream->total_bytes += entry->size;
1534 if (ENTRY_IS_KEYFRAME (entry))
1535 stream->n_keyframes++;
1538 GST_LOG_OBJECT (avi,
1539 "Adding stream %u, index entry %d, kf %d, size %u "
1540 ", offset %" G_GUINT64_FORMAT ", total %" G_GUINT64_FORMAT, stream->num,
1541 stream->idx_n, ENTRY_IS_KEYFRAME (entry), entry->size, entry->offset,
1543 stream->index[stream->idx_n++] = *entry;
1548 /* given @entry_n in @stream, calculate info such as timestamps and
1549 * offsets for the entry. */
1551 gst_avi_demux_get_buffer_info (GstAviDemux * avi, GstAviStream * stream,
1552 guint entry_n, GstClockTime * timestamp, GstClockTime * ts_end,
1553 guint64 * offset, guint64 * offset_end)
1555 GstAviIndexEntry *entry;
1557 entry = &stream->index[entry_n];
1559 if (stream->is_vbr) {
1560 /* VBR stream next timestamp */
1561 if (stream->strh->type == GST_RIFF_FCC_auds) {
1564 avi_stream_convert_frames_to_time_unchecked (stream, entry->total);
1566 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1571 avi_stream_convert_frames_to_time_unchecked (stream, entry_n);
1573 *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1576 } else if (stream->strh->type == GST_RIFF_FCC_auds) {
1577 /* constant rate stream */
1580 avi_stream_convert_bytes_to_time_unchecked (stream, entry->total);
1582 *ts_end = avi_stream_convert_bytes_to_time_unchecked (stream,
1583 entry->total + entry->size);
1585 if (stream->strh->type == GST_RIFF_FCC_vids) {
1586 /* video offsets are the frame number */
1590 *offset_end = entry_n + 1;
1592 /* no offsets for audio */
1600 /* collect and debug stats about the indexes for all streams.
1601 * This method is also responsible for filling in the stream duration
1602 * as measured by the amount of index entries.
1604 * Returns TRUE if the index is not empty, else FALSE */
1606 gst_avi_demux_do_index_stats (GstAviDemux * avi)
1608 guint total_idx = 0;
1610 #ifndef GST_DISABLE_GST_DEBUG
1611 guint total_max = 0;
1614 /* get stream stats now */
1615 for (i = 0; i < avi->num_streams; i++) {
1616 GstAviStream *stream;
1618 if (G_UNLIKELY (!(stream = &avi->stream[i])))
1620 if (G_UNLIKELY (!stream->strh))
1622 if (G_UNLIKELY (!stream->index || stream->idx_n == 0))
1625 /* we interested in the end_ts of the last entry, which is the total
1626 * duration of this stream */
1627 gst_avi_demux_get_buffer_info (avi, stream, stream->idx_n - 1,
1628 NULL, &stream->idx_duration, NULL, NULL);
1630 total_idx += stream->idx_n;
1631 #ifndef GST_DISABLE_GST_DEBUG
1632 total_max += stream->idx_max;
1634 GST_INFO_OBJECT (avi, "Stream %d, dur %" GST_TIME_FORMAT ", %6u entries, "
1635 "%5u keyframes, entry size = %2u, total size = %10u, allocated %10u",
1636 i, GST_TIME_ARGS (stream->idx_duration), stream->idx_n,
1637 stream->n_keyframes, (guint) sizeof (GstAviIndexEntry),
1638 (guint) (stream->idx_n * sizeof (GstAviIndexEntry)),
1639 (guint) (stream->idx_max * sizeof (GstAviIndexEntry)));
1641 total_idx *= sizeof (GstAviIndexEntry);
1642 #ifndef GST_DISABLE_GST_DEBUG
1643 total_max *= sizeof (GstAviIndexEntry);
1645 GST_INFO_OBJECT (avi, "%u bytes for index vs %u ideally, %u wasted",
1646 total_max, total_idx, total_max - total_idx);
1648 if (total_idx == 0) {
1649 GST_WARNING_OBJECT (avi, "Index is empty !");
1656 * gst_avi_demux_parse_subindex:
1658 * @buf: input data to use for parsing.
1659 * @stream: stream context.
1660 * @entries_list: a list (returned by the function) containing all the
1661 * indexes parsed in this specific subindex. The first
1662 * entry is also a pointer to allocated memory that needs
1663 * to be free´ed. May be NULL if no supported indexes were
1666 * Reads superindex (openDML-2 spec stuff) from the provided data.
1667 * The buffer should contain a GST_RIFF_TAG_ix?? chunk.
1669 * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1670 * throw an error, caller should bail out asap.
1673 gst_avi_demux_parse_subindex (GstAviDemux * avi, GstAviStream * stream,
1685 size = GST_BUFFER_SIZE (buf);
1691 data = GST_BUFFER_DATA (buf);
1693 /* We don't support index-data yet */
1695 goto not_implemented;
1697 /* check type of index. The opendml2 specs state that
1698 * there should be 4 dwords per array entry. Type can be
1699 * either frame or field (and we don't care). */
1700 bpe = (data[2] & 0x01) ? 12 : 8;
1701 if (GST_READ_UINT16_LE (data) != bpe / 4 ||
1702 (data[2] & 0xfe) != 0x0 || data[3] != 0x1) {
1703 GST_WARNING_OBJECT (avi,
1704 "Superindex for stream %d has unexpected "
1705 "size_entry %d (bytes) or flags 0x%02x/0x%02x",
1706 stream->num, GST_READ_UINT16_LE (data), data[2], data[3]);
1707 bpe = GST_READ_UINT16_LE (data) * 4;
1709 num = GST_READ_UINT32_LE (&data[4]);
1710 baseoff = GST_READ_UINT64_LE (&data[12]);
1712 /* If there's nothing, just return ! */
1716 GST_INFO_OBJECT (avi, "Parsing subindex, nr_entries = %6d", num);
1718 for (i = 0; i < num; i++) {
1719 GstAviIndexEntry entry;
1721 if (size < 24 + bpe * (i + 1))
1724 /* fill in offset and size. offset contains the keyframe flag in the
1726 entry.offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1727 entry.size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1729 if (stream->strh->type == GST_RIFF_FCC_auds) {
1730 /* all audio frames are keyframes */
1731 ENTRY_SET_KEYFRAME (&entry);
1733 /* else read flags */
1734 entry.flags = (entry.size & 0x80000000) ? 0 : GST_AVI_KEYFRAME;
1736 entry.size &= ~0x80000000;
1739 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
1742 gst_buffer_unref (buf);
1749 GST_ERROR_OBJECT (avi,
1750 "Not enough data to parse subindex (%d available, 24 needed)", size);
1751 gst_buffer_unref (buf);
1752 return TRUE; /* continue */
1756 GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1757 ("Subindex-is-data is not implemented"));
1758 gst_buffer_unref (buf);
1763 GST_DEBUG_OBJECT (avi, "the index is empty");
1764 gst_buffer_unref (buf);
1769 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
1770 ("Cannot allocate memory for %u*%u=%u bytes",
1771 (guint) sizeof (GstAviIndexEntry), num,
1772 (guint) sizeof (GstAviIndexEntry) * num));
1773 gst_buffer_unref (buf);
1779 * Create and push a flushing seek event upstream
1782 perform_seek_to_offset (GstAviDemux * demux, guint64 offset)
1787 GST_DEBUG_OBJECT (demux, "Seeking to %" G_GUINT64_FORMAT, offset);
1790 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1791 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1792 GST_SEEK_TYPE_NONE, -1);
1794 res = gst_pad_push_event (demux->sinkpad, event);
1797 demux->offset = offset;
1802 * Read AVI index when streaming
1805 gst_avi_demux_read_subindexes_push (GstAviDemux * avi)
1807 guint32 tag = 0, size;
1808 GstBuffer *buf = NULL;
1811 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1813 if (avi->odml_subidxs[avi->odml_subidx] != avi->offset)
1816 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1819 /* this is the ODML chunk we expect */
1820 odml_stream = avi->odml_stream;
1822 if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + odml_stream / 10,
1823 '0' + odml_stream % 10)) &&
1824 (tag != GST_MAKE_FOURCC ('0' + odml_stream / 10,
1825 '0' + odml_stream % 10, 'i', 'x'))) {
1826 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1827 GST_FOURCC_ARGS (tag));
1831 avi->offset += 8 + GST_ROUND_UP_2 (size);
1832 /* flush chunk header so we get just the 'size' payload data */
1833 gst_adapter_flush (avi->adapter, 8);
1834 buf = gst_adapter_take_buffer (avi->adapter, size);
1836 if (!gst_avi_demux_parse_subindex (avi, &avi->stream[odml_stream], buf))
1839 /* we parsed the index, go to next subindex */
1842 if (avi->odml_subidxs[avi->odml_subidx] == GST_BUFFER_OFFSET_NONE) {
1843 /* we reached the end of the indexes for this stream, move to the next
1844 * stream to handle the first index */
1846 avi->odml_subidx = 0;
1848 if (avi->odml_stream < avi->num_streams) {
1849 /* there are more indexes */
1850 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
1852 /* we're done, get stream stats now */
1853 avi->have_index = gst_avi_demux_do_index_stats (avi);
1859 /* seek to next index */
1860 return perform_seek_to_offset (avi, avi->odml_subidxs[avi->odml_subidx]);
1867 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi)
1873 GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1875 for (n = 0; n < avi->num_streams; n++) {
1876 GstAviStream *stream = &avi->stream[n];
1878 if (stream->indexes == NULL)
1881 for (i = 0; stream->indexes[i] != GST_BUFFER_OFFSET_NONE; i++) {
1882 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi), avi->sinkpad,
1883 &stream->indexes[i], &tag, &buf) != GST_FLOW_OK)
1885 else if ((tag != GST_MAKE_FOURCC ('i', 'x', '0' + stream->num / 10,
1886 '0' + stream->num % 10)) &&
1887 (tag != GST_MAKE_FOURCC ('0' + stream->num / 10,
1888 '0' + stream->num % 10, 'i', 'x'))) {
1889 /* Some ODML files (created by god knows what muxer) have a ##ix format
1890 * instead of the 'official' ix##. They are still valid though. */
1891 GST_WARNING_OBJECT (avi, "Not an ix## chunk (%" GST_FOURCC_FORMAT ")",
1892 GST_FOURCC_ARGS (tag));
1893 gst_buffer_unref (buf);
1897 if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1901 g_free (stream->indexes);
1902 stream->indexes = NULL;
1904 /* get stream stats now */
1905 avi->have_index = gst_avi_demux_do_index_stats (avi);
1909 * gst_avi_demux_riff_parse_vprp:
1910 * @element: caller element (used for debugging/error).
1911 * @buf: input data to be used for parsing, stripped from header.
1912 * @vprp: a pointer (returned by this function) to a filled-in vprp
1913 * structure. Caller should free it.
1915 * Parses a video stream´s vprp. This function takes ownership of @buf.
1917 * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1918 * should be skipped on error, but it is not fatal.
1921 gst_avi_demux_riff_parse_vprp (GstElement * element,
1922 GstBuffer * buf, gst_riff_vprp ** _vprp)
1924 gst_riff_vprp *vprp;
1927 g_return_val_if_fail (buf != NULL, FALSE);
1928 g_return_val_if_fail (_vprp != NULL, FALSE);
1930 if (GST_BUFFER_SIZE (buf) < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1933 vprp = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1935 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1936 vprp->format_token = GUINT32_FROM_LE (vprp->format_token);
1937 vprp->standard = GUINT32_FROM_LE (vprp->standard);
1938 vprp->vert_rate = GUINT32_FROM_LE (vprp->vert_rate);
1939 vprp->hor_t_total = GUINT32_FROM_LE (vprp->hor_t_total);
1940 vprp->vert_lines = GUINT32_FROM_LE (vprp->vert_lines);
1941 vprp->aspect = GUINT32_FROM_LE (vprp->aspect);
1942 vprp->width = GUINT32_FROM_LE (vprp->width);
1943 vprp->height = GUINT32_FROM_LE (vprp->height);
1944 vprp->fields = GUINT32_FROM_LE (vprp->fields);
1948 /* calculate fields based on size */
1949 k = (GST_BUFFER_SIZE (buf) - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) /
1951 if (vprp->fields > k) {
1952 GST_WARNING_OBJECT (element,
1953 "vprp header indicated %d fields, only %d available", vprp->fields, k);
1956 if (vprp->fields > GST_RIFF_VPRP_VIDEO_FIELDS) {
1957 GST_WARNING_OBJECT (element,
1958 "vprp header indicated %d fields, at most %d supported", vprp->fields,
1959 GST_RIFF_VPRP_VIDEO_FIELDS);
1960 vprp->fields = GST_RIFF_VPRP_VIDEO_FIELDS;
1962 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1963 for (k = 0; k < vprp->fields; k++) {
1964 gst_riff_vprp_video_field_desc *fd;
1966 fd = &vprp->field_info[k];
1967 fd->compressed_bm_height = GUINT32_FROM_LE (fd->compressed_bm_height);
1968 fd->compressed_bm_width = GUINT32_FROM_LE (fd->compressed_bm_width);
1969 fd->valid_bm_height = GUINT32_FROM_LE (fd->valid_bm_height);
1970 fd->valid_bm_width = GUINT16_FROM_LE (fd->valid_bm_width);
1971 fd->valid_bm_x_offset = GUINT16_FROM_LE (fd->valid_bm_x_offset);
1972 fd->valid_bm_y_offset = GUINT32_FROM_LE (fd->valid_bm_y_offset);
1973 fd->video_x_t_offset = GUINT32_FROM_LE (fd->video_x_t_offset);
1974 fd->video_y_start = GUINT32_FROM_LE (fd->video_y_start);
1979 GST_INFO_OBJECT (element, "vprp tag found in context vids:");
1980 GST_INFO_OBJECT (element, " format_token %d", vprp->format_token);
1981 GST_INFO_OBJECT (element, " standard %d", vprp->standard);
1982 GST_INFO_OBJECT (element, " vert_rate %d", vprp->vert_rate);
1983 GST_INFO_OBJECT (element, " hor_t_total %d", vprp->hor_t_total);
1984 GST_INFO_OBJECT (element, " vert_lines %d", vprp->vert_lines);
1985 GST_INFO_OBJECT (element, " aspect %d:%d", vprp->aspect >> 16,
1986 vprp->aspect & 0xffff);
1987 GST_INFO_OBJECT (element, " width %d", vprp->width);
1988 GST_INFO_OBJECT (element, " height %d", vprp->height);
1989 GST_INFO_OBJECT (element, " fields %d", vprp->fields);
1990 for (k = 0; k < vprp->fields; k++) {
1991 gst_riff_vprp_video_field_desc *fd;
1993 fd = &(vprp->field_info[k]);
1994 GST_INFO_OBJECT (element, " field %u description:", k);
1995 GST_INFO_OBJECT (element, " compressed_bm_height %d",
1996 fd->compressed_bm_height);
1997 GST_INFO_OBJECT (element, " compressed_bm_width %d",
1998 fd->compressed_bm_width);
1999 GST_INFO_OBJECT (element, " valid_bm_height %d",
2000 fd->valid_bm_height);
2001 GST_INFO_OBJECT (element, " valid_bm_width %d", fd->valid_bm_width);
2002 GST_INFO_OBJECT (element, " valid_bm_x_offset %d",
2003 fd->valid_bm_x_offset);
2004 GST_INFO_OBJECT (element, " valid_bm_y_offset %d",
2005 fd->valid_bm_y_offset);
2006 GST_INFO_OBJECT (element, " video_x_t_offset %d",
2007 fd->video_x_t_offset);
2008 GST_INFO_OBJECT (element, " video_y_start %d", fd->video_y_start);
2011 gst_buffer_unref (buf);
2020 GST_ERROR_OBJECT (element,
2021 "Too small vprp (%d available, at least %d needed)",
2022 GST_BUFFER_SIZE (buf),
2023 (int) G_STRUCT_OFFSET (gst_riff_vprp, field_info));
2024 gst_buffer_unref (buf);
2030 gst_avi_demux_expose_streams (GstAviDemux * avi, gboolean force)
2034 GST_DEBUG_OBJECT (avi, "force : %d", force);
2036 for (i = 0; i < avi->num_streams; i++) {
2037 GstAviStream *stream = &avi->stream[i];
2039 if (force || stream->idx_n != 0) {
2040 GST_LOG_OBJECT (avi, "Added pad %s with caps %" GST_PTR_FORMAT,
2041 GST_PAD_NAME (stream->pad), GST_PAD_CAPS (stream->pad));
2042 gst_element_add_pad ((GstElement *) avi, stream->pad);
2044 if (avi->element_index)
2045 gst_index_get_writer_id (avi->element_index,
2046 GST_OBJECT_CAST (stream->pad), &stream->index_id);
2048 stream->exposed = TRUE;
2049 if (avi->main_stream == -1)
2050 avi->main_stream = i;
2052 GST_WARNING_OBJECT (avi, "Stream #%d doesn't have any entry, removing it",
2054 gst_avi_demux_reset_stream (avi, stream);
2061 typedef struct _gst_riff_strd {
2068 gst_riff_parse_strd (GstAviDemux * avi,
2071 g_return_val_if_fail (buf != NULL, FALSE);
2073 if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_strd))
2076 GST_DEBUG_OBJECT (avi, " version %d", ((gst_riff_strd*)GST_BUFFER_DATA(buf))->version);
2077 GST_DEBUG_OBJECT (avi, " drm_size %d", ((gst_riff_strd*)GST_BUFFER_DATA(buf))->drm_size);
2079 return gst_avi_demux_init_divx_drm (avi, GST_BUFFER_DATA(buf)+sizeof(gst_riff_strd));
2084 GST_ERROR_OBJECT (avi,
2085 "Too small strh (%d available, %d needed)",
2086 GST_BUFFER_SIZE (buf), (int) sizeof (gst_riff_strd));
2087 gst_buffer_unref (buf);
2094 /* buf contains LIST chunk data, and will be padded to even size,
2095 * since some buggy files do not account for the padding of chunks
2096 * within a LIST in the size of the LIST */
2098 gst_avi_demux_roundup_list (GstAviDemux * avi, GstBuffer ** buf)
2100 if (G_UNLIKELY (GST_BUFFER_SIZE (*buf) & 1)) {
2103 GST_DEBUG_OBJECT (avi, "rounding up dubious list size %d",
2104 GST_BUFFER_SIZE (*buf));
2105 obuf = gst_buffer_new_and_alloc (GST_BUFFER_SIZE (*buf) + 1);
2106 memcpy (GST_BUFFER_DATA (obuf), GST_BUFFER_DATA (*buf),
2107 GST_BUFFER_SIZE (*buf));
2108 gst_buffer_replace (buf, obuf);
2113 * gst_avi_demux_parse_stream:
2114 * @avi: calling element (used for debugging/errors).
2115 * @buf: input buffer used to parse the stream.
2117 * Parses all subchunks in a strl chunk (which defines a single
2118 * stream). Discards the buffer after use. This function will
2119 * increment the stream counter internally.
2121 * Returns: whether the stream was identified successfully.
2122 * Errors are not fatal. It does indicate the stream
2126 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
2128 GstAviStream *stream;
2129 GstElementClass *klass;
2130 GstPadTemplate *templ = NULL;
2131 GstBuffer *sub = NULL;
2134 gchar *codec_name = NULL, *padname = NULL;
2135 const gchar *tag_name = NULL;
2136 GstCaps *caps = NULL;
2138 GstElement *element;
2139 gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
2140 gst_riff_vprp *vprp = NULL;
2142 element = GST_ELEMENT_CAST (avi);
2144 GST_DEBUG_OBJECT (avi, "Parsing stream");
2146 gst_avi_demux_roundup_list (avi, &buf);
2148 if (avi->num_streams >= GST_AVI_DEMUX_MAX_STREAMS) {
2149 GST_WARNING_OBJECT (avi,
2150 "maximum no of streams (%d) exceeded, ignoring stream",
2151 GST_AVI_DEMUX_MAX_STREAMS);
2152 gst_buffer_unref (buf);
2153 /* not a fatal error, let's say */
2157 stream = &avi->stream[avi->num_streams];
2159 /* initial settings */
2160 stream->idx_duration = GST_CLOCK_TIME_NONE;
2161 stream->hdr_duration = GST_CLOCK_TIME_NONE;
2162 stream->duration = GST_CLOCK_TIME_NONE;
2164 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
2165 /* sub can be NULL if the chunk is empty */
2167 GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
2168 GST_FOURCC_ARGS (tag));
2172 case GST_RIFF_TAG_strh:
2174 gst_riff_strh *strh;
2177 GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
2180 if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
2181 /* ownership given away */
2183 GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
2187 strh = stream->strh;
2188 /* sanity check; stream header frame rate matches global header
2190 if (stream->strh->type == GST_RIFF_FCC_vids) {
2192 GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
2194 s_dur = gst_util_uint64_scale (GST_SECOND, strh->scale, strh->rate);
2195 GST_DEBUG_OBJECT (avi, "verifying stream framerate %d/%d, "
2196 "frame duration = %d ms", strh->rate, strh->scale,
2197 (gint) (s_dur / GST_MSECOND));
2198 if (h_dur > (10 * GST_MSECOND) && (s_dur > 10 * h_dur)) {
2199 strh->rate = GST_SECOND / GST_USECOND;
2200 strh->scale = h_dur / GST_USECOND;
2201 GST_DEBUG_OBJECT (avi, "correcting stream framerate to %d/%d",
2202 strh->rate, strh->scale);
2205 /* determine duration as indicated by header */
2206 stream->hdr_duration = gst_util_uint64_scale ((guint64) strh->length *
2207 strh->scale, GST_SECOND, (guint64) strh->rate);
2208 GST_INFO ("Stream duration according to header: %" GST_TIME_FORMAT,
2209 GST_TIME_ARGS (stream->hdr_duration));
2210 if (stream->hdr_duration == 0)
2211 stream->hdr_duration = GST_CLOCK_TIME_NONE;
2216 case GST_RIFF_TAG_strf:
2218 gboolean res = FALSE;
2221 GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
2225 GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
2228 switch (stream->strh->type) {
2229 case GST_RIFF_FCC_vids:
2230 stream->is_vbr = TRUE;
2231 res = gst_riff_parse_strf_vids (element, sub,
2232 &stream->strf.vids, &stream->extradata);
2234 GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
2236 case GST_RIFF_FCC_auds:
2238 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
2239 &stream->extradata);
2243 stream->is_vbr = (stream->strh->samplesize == 0)
2244 && stream->strh->scale > 1
2245 && stream->strf.auds->blockalign != 1;
2246 GST_DEBUG_OBJECT (element, "marking audio as VBR:%d, res %d",
2247 stream->is_vbr, res);
2248 /* we need these or we have no way to come up with timestamps */
2249 if ((!stream->is_vbr && !stream->strf.auds->av_bps) ||
2250 (stream->is_vbr && (!stream->strh->scale ||
2251 !stream->strh->rate))) {
2252 GST_WARNING_OBJECT (element,
2253 "invalid audio header, ignoring stream");
2256 /* some more sanity checks */
2257 if (stream->is_vbr) {
2258 if (stream->strf.auds->blockalign <= 4) {
2259 /* that would mean (too) many frames per chunk,
2260 * so not likely set as expected */
2261 GST_DEBUG_OBJECT (element,
2262 "suspicious blockalign %d for VBR audio; "
2263 "overriding to 1 frame per chunk",
2264 stream->strf.auds->blockalign);
2265 /* this should top any likely value */
2266 stream->strf.auds->blockalign = (1 << 12);
2270 case GST_RIFF_FCC_iavs:
2271 stream->is_vbr = TRUE;
2272 res = gst_riff_parse_strf_iavs (element, sub,
2273 &stream->strf.iavs, &stream->extradata);
2275 GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
2277 case GST_RIFF_FCC_txts:
2278 /* nothing to parse here */
2279 stream->is_vbr = (stream->strh->samplesize == 0)
2280 && (stream->strh->scale > 1);
2284 GST_ERROR_OBJECT (avi,
2285 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
2286 GST_FOURCC_ARGS (stream->strh->type));
2290 gst_buffer_unref (sub);
2298 case GST_RIFF_TAG_vprp:
2301 GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
2305 GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
2309 GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
2313 if (!gst_avi_demux_riff_parse_vprp (element, sub, &vprp)) {
2314 GST_WARNING_OBJECT (avi, "Failed to parse vprp chunk");
2315 /* not considered fatal */
2323 case GST_RIFF_TAG_strd:
2325 GST_DEBUG_OBJECT (avi, "******************* strd tag found:");
2326 if (gst_riff_parse_strd (avi, sub) == FALSE) {
2327 GST_ELEMENT_ERROR(avi, STREAM, DECRYPT,
2328 ("DivX initialization failed"),
2329 ("gst_avi_demux_init_divx_drm() failed") );
2334 if (stream->initdata)
2335 gst_buffer_unref (stream->initdata);
2336 stream->initdata = sub;
2339 case GST_RIFF_TAG_strn:
2340 g_free (stream->name);
2343 g_strndup ((gchar *) GST_BUFFER_DATA (sub),
2344 (gsize) GST_BUFFER_SIZE (sub));
2345 gst_buffer_unref (sub);
2348 stream->name = g_strdup ("");
2350 GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
2353 gst_avi_demux_parse_idit (avi, sub);
2356 if (tag == GST_MAKE_FOURCC ('i', 'n', 'd', 'x') ||
2357 tag == GST_MAKE_FOURCC ('i', 'x', '0' + avi->num_streams / 10,
2358 '0' + avi->num_streams % 10)) {
2359 g_free (stream->indexes);
2360 gst_avi_demux_parse_superindex (avi, sub, &stream->indexes);
2361 stream->superindex = TRUE;
2365 GST_WARNING_OBJECT (avi,
2366 "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
2367 GST_FOURCC_ARGS (tag));
2369 case GST_RIFF_TAG_JUNQ:
2370 case GST_RIFF_TAG_JUNK:
2374 gst_buffer_unref (sub);
2380 GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
2385 GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
2389 /* get class to figure out the template */
2390 klass = GST_ELEMENT_GET_CLASS (avi);
2392 /* we now have all info, let´s set up a pad and a caps and be done */
2393 /* create stream name + pad */
2394 switch (stream->strh->type) {
2395 case GST_RIFF_FCC_vids:{
2398 fourcc = (stream->strf.vids->compression) ?
2399 stream->strf.vids->compression : stream->strh->fcc_handler;
2400 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
2401 templ = gst_element_class_get_pad_template (klass, "video_%02d");
2402 caps = gst_riff_create_video_caps (fourcc, stream->strh,
2403 stream->strf.vids, stream->extradata, stream->initdata, &codec_name);
2405 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2406 GST_TYPE_FOURCC, fourcc, NULL);
2407 } else if (got_vprp && vprp) {
2408 guint32 aspect_n, aspect_d;
2411 aspect_n = vprp->aspect >> 16;
2412 aspect_d = vprp->aspect & 0xffff;
2413 /* calculate the pixel aspect ratio using w/h and aspect ratio */
2414 n = aspect_n * stream->strf.vids->height;
2415 d = aspect_d * stream->strf.vids->width;
2417 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
2419 /* very local, not needed elsewhere */
2423 tag_name = GST_TAG_VIDEO_CODEC;
2424 avi->num_v_streams++;
2427 case GST_RIFF_FCC_auds:{
2428 padname = g_strdup_printf ("audio_%02d", avi->num_a_streams);
2429 templ = gst_element_class_get_pad_template (klass, "audio_%02d");
2430 caps = gst_riff_create_audio_caps (stream->strf.auds->format,
2431 stream->strh, stream->strf.auds, stream->extradata,
2432 stream->initdata, &codec_name);
2434 caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
2435 G_TYPE_INT, stream->strf.auds->format, NULL);
2437 tag_name = GST_TAG_AUDIO_CODEC;
2438 avi->num_a_streams++;
2441 case GST_RIFF_FCC_iavs:{
2442 guint32 fourcc = stream->strh->fcc_handler;
2444 padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
2445 templ = gst_element_class_get_pad_template (klass, "video_%02d");
2446 caps = gst_riff_create_iavs_caps (fourcc, stream->strh,
2447 stream->strf.iavs, stream->extradata, stream->initdata, &codec_name);
2449 caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2450 GST_TYPE_FOURCC, fourcc, NULL);
2452 tag_name = GST_TAG_VIDEO_CODEC;
2453 avi->num_v_streams++;
2456 case GST_RIFF_FCC_txts:{
2457 padname = g_strdup_printf ("subtitle_%02d", avi->num_t_streams);
2458 templ = gst_element_class_get_pad_template (klass, "subtitle_%02d");
2459 caps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
2461 avi->num_t_streams++;
2465 g_assert_not_reached ();
2468 /* no caps means no stream */
2470 GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
2474 GST_DEBUG_OBJECT (element, "codec-name=%s",
2475 (codec_name ? codec_name : "NULL"));
2476 GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
2478 /* set proper settings and add it */
2480 gst_object_unref (stream->pad);
2481 pad = stream->pad = gst_pad_new_from_template (templ, padname);
2484 gst_pad_use_fixed_caps (pad);
2486 gst_pad_set_formats_function (pad,
2487 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_formats));
2488 gst_pad_set_event_mask_function (pad,
2489 GST_DEBUG_FUNCPTR (gst_avi_demux_get_event_mask));
2491 gst_pad_set_event_function (pad,
2492 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_event));
2493 gst_pad_set_query_type_function (pad,
2494 GST_DEBUG_FUNCPTR (gst_avi_demux_get_src_query_types));
2495 gst_pad_set_query_function (pad,
2496 GST_DEBUG_FUNCPTR (gst_avi_demux_handle_src_query));
2498 gst_pad_set_convert_function (pad,
2499 GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
2502 if (avi->element_index)
2503 gst_index_get_writer_id (avi->element_index, GST_OBJECT_CAST (stream->pad),
2506 stream->num = avi->num_streams;
2508 stream->start_entry = 0;
2509 stream->step_entry = 0;
2510 stream->stop_entry = 0;
2512 stream->current_entry = -1;
2513 stream->current_total = 0;
2515 stream->last_flow = GST_FLOW_OK;
2516 stream->discont = TRUE;
2518 stream->total_bytes = 0;
2519 stream->total_blocks = 0;
2520 stream->n_keyframes = 0;
2523 stream->idx_max = 0;
2525 gst_pad_set_element_private (pad, stream);
2528 #ifdef AVIDEMUX_MODIFICATION
2529 /*Modification: Temporary fix for deselcting the ac3parse element in the pipeline to
2530 avoid the trickplay issues */
2531 GST_INFO_OBJECT(avi, "prepared caps=> %s", gst_caps_to_string(caps));
2535 GstStructure *tempstruct;
2536 GstStructure *structure;
2538 tempstruct = gst_caps_get_structure(caps, 0);
2540 if(!strcmp(gst_structure_get_name (tempstruct), "audio/x-ac3"))
2543 structure = gst_structure_copy(tempstruct);
2545 gst_structure_set(structure, "framed", G_TYPE_BOOLEAN, TRUE, NULL);
2547 gst_caps_remove_structure(caps, 0);
2549 gst_caps_merge_structure(caps, structure);
2552 else if(!strcmp(gst_structure_get_name (tempstruct), "audio/mpeg"))
2555 structure = gst_structure_copy(tempstruct);
2557 gst_structure_set(structure, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
2559 gst_caps_remove_structure(caps, 0);
2561 gst_caps_merge_structure(caps, structure);
2570 gst_pad_set_caps (pad, caps);
2571 gst_pad_set_active (pad, TRUE);
2572 gst_caps_unref (caps);
2576 if (!stream->taglist)
2577 stream->taglist = gst_tag_list_new ();
2579 avi->got_tags = TRUE;
2581 gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
2583 g_free (codec_name);
2586 gst_buffer_unref (buf);
2593 /* unref any mem that may be in use */
2595 gst_buffer_unref (buf);
2597 gst_buffer_unref (sub);
2599 g_free (codec_name);
2600 gst_avi_demux_reset_stream (avi, stream);
2607 * gst_avi_demux_parse_odml:
2608 * @avi: calling element (used for debug/error).
2609 * @buf: input buffer to be used for parsing.
2611 * Read an openDML-2.0 extension header. Fills in the frame number
2612 * in the avi demuxer object when reading succeeds.
2615 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
2619 GstBuffer *sub = NULL;
2621 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
2624 case GST_RIFF_TAG_dmlh:{
2625 gst_riff_dmlh dmlh, *_dmlh;
2628 /* sub == NULL is possible and means an empty buffer */
2629 size = sub ? GST_BUFFER_SIZE (sub) : 0;
2632 if (size < sizeof (gst_riff_dmlh)) {
2633 GST_ERROR_OBJECT (avi,
2634 "DMLH entry is too small (%d bytes, %d needed)",
2635 size, (int) sizeof (gst_riff_dmlh));
2638 _dmlh = (gst_riff_dmlh *) GST_BUFFER_DATA (sub);
2639 dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
2641 GST_INFO_OBJECT (avi, "dmlh tag found: totalframes: %u",
2644 avi->avih->tot_frames = dmlh.totalframes;
2649 GST_WARNING_OBJECT (avi,
2650 "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
2651 GST_FOURCC_ARGS (tag));
2653 case GST_RIFF_TAG_JUNQ:
2654 case GST_RIFF_TAG_JUNK:
2656 /* skip and move to next chunk */
2658 gst_buffer_unref (sub);
2665 gst_buffer_unref (buf);
2670 gst_avi_demux_index_last (GstAviDemux * avi, GstAviStream * stream)
2672 return stream->idx_n;
2675 /* find a previous entry in the index with the given flags */
2677 gst_avi_demux_index_prev (GstAviDemux * avi, GstAviStream * stream,
2678 guint last, gboolean keyframe)
2680 GstAviIndexEntry *entry;
2683 for (i = last; i > 0; i--) {
2684 entry = &stream->index[i - 1];
2685 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2693 gst_avi_demux_index_next (GstAviDemux * avi, GstAviStream * stream,
2694 guint last, gboolean keyframe)
2696 GstAviIndexEntry *entry;
2699 for (i = last + 1; i < stream->idx_n; i++) {
2700 entry = &stream->index[i];
2701 if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2705 return stream->idx_n - 1;
2709 gst_avi_demux_index_entry_search (GstAviIndexEntry * entry, guint64 * total)
2711 if (entry->total < *total)
2713 else if (entry->total > *total)
2719 * gst_avi_demux_index_for_time:
2721 * @stream: the stream
2722 * @time: a time position
2724 * Finds the index entry which time is less or equal than the requested time.
2725 * Try to avoid binary search when we can convert the time to an index
2726 * position directly (for example for video frames with a fixed duration).
2728 * Returns: the found position in the index.
2731 gst_avi_demux_index_for_time (GstAviDemux * avi,
2732 GstAviStream * stream, guint64 time)
2737 GST_LOG_OBJECT (avi, "search time:%" GST_TIME_FORMAT, GST_TIME_ARGS (time));
2739 /* easy (and common) cases */
2740 if (time == 0 || stream->idx_n == 0)
2742 if (time >= stream->idx_duration)
2743 return stream->idx_n - 1;
2745 /* figure out where we need to go. For that we convert the time to an
2746 * index entry or we convert it to a total and then do a binary search. */
2747 if (stream->is_vbr) {
2748 /* VBR stream next timestamp */
2749 if (stream->strh->type == GST_RIFF_FCC_auds) {
2750 total = avi_stream_convert_time_to_frames_unchecked (stream, time);
2752 index = avi_stream_convert_time_to_frames_unchecked (stream, time);
2755 /* constant rate stream */
2756 total = avi_stream_convert_time_to_bytes_unchecked (stream, time);
2760 GstAviIndexEntry *entry;
2762 /* no index, find index with binary search on total */
2763 GST_LOG_OBJECT (avi, "binary search for entry with total %"
2764 G_GUINT64_FORMAT, total);
2766 entry = gst_util_array_binary_search (stream->index,
2767 stream->idx_n, sizeof (GstAviIndexEntry),
2768 (GCompareDataFunc) gst_avi_demux_index_entry_search,
2769 GST_SEARCH_MODE_BEFORE, &total, NULL);
2771 if (entry == NULL) {
2772 GST_LOG_OBJECT (avi, "not found, assume index 0");
2775 index = entry - stream->index;
2776 GST_LOG_OBJECT (avi, "found at %u", index);
2779 GST_LOG_OBJECT (avi, "converted time to index %u", index);
2785 static inline GstAviStream *
2786 gst_avi_demux_stream_for_id (GstAviDemux * avi, guint32 id)
2789 GstAviStream *stream;
2791 /* get the stream for this entry */
2792 stream_nr = CHUNKID_TO_STREAMNR (id);
2793 if (G_UNLIKELY (stream_nr >= avi->num_streams)) {
2794 GST_WARNING_OBJECT (avi, "invalid stream nr %d", stream_nr);
2797 stream = &avi->stream[stream_nr];
2798 if (G_UNLIKELY (!stream->strh)) {
2799 GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2806 * gst_avi_demux_parse_index:
2807 * @avi: calling element (used for debugging/errors).
2808 * @buf: buffer containing the full index.
2810 * Read index entries from the provided buffer.
2811 * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
2814 gst_avi_demux_parse_index (GstAviDemux * avi, GstBuffer * buf)
2820 gst_riff_index_entry *index;
2822 GstAviStream *stream;
2823 GstAviIndexEntry entry;
2829 data = GST_BUFFER_DATA (buf);
2830 size = GST_BUFFER_SIZE (buf);
2832 stamp = gst_util_get_timestamp ();
2834 /* see how many items in the index */
2835 num = size / sizeof (gst_riff_index_entry);
2839 GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2841 index = (gst_riff_index_entry *) data;
2842 pos_before = avi->offset;
2844 /* figure out if the index is 0 based or relative to the MOVI start */
2845 entry.offset = GST_READ_UINT32_LE (&index[0].offset);
2846 if (entry.offset < avi->offset) {
2847 avi->index_offset = avi->offset + 8;
2848 GST_DEBUG ("index_offset = %" G_GUINT64_FORMAT, avi->index_offset);
2850 avi->index_offset = 0;
2851 GST_DEBUG ("index is 0 based");
2854 for (i = 0, n = 0; i < num; i++) {
2855 id = GST_READ_UINT32_LE (&index[i].id);
2856 entry.offset = GST_READ_UINT32_LE (&index[i].offset);
2858 /* some sanity checks */
2859 if (G_UNLIKELY (id == GST_RIFF_rec || id == 0 ||
2860 (entry.offset == 0 && n > 0)))
2863 if ( id == GST_MAKE_FOURCC('0','0','d','d') )
2865 GST_DEBUG("Skipping Encrypt data chunk");
2870 /* get the stream for this entry */
2871 stream = gst_avi_demux_stream_for_id (avi, id);
2872 if (G_UNLIKELY (!stream))
2875 /* handle offset and size */
2876 entry.offset += avi->index_offset + 8;
2877 entry.size = GST_READ_UINT32_LE (&index[i].size);
2880 if (stream->strh->type == GST_RIFF_FCC_auds) {
2881 /* all audio frames are keyframes */
2882 ENTRY_SET_KEYFRAME (&entry);
2885 /* else read flags */
2886 flags = GST_READ_UINT32_LE (&index[i].flags);
2887 if (flags & GST_RIFF_IF_KEYFRAME) {
2888 ENTRY_SET_KEYFRAME (&entry);
2890 ENTRY_UNSET_KEYFRAME (&entry);
2895 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2900 gst_buffer_unref (buf);
2902 /* get stream stats now */
2903 avi->have_index = gst_avi_demux_do_index_stats (avi);
2905 stamp = gst_util_get_timestamp () - stamp;
2906 GST_DEBUG_OBJECT (avi, "index parsing took %" GST_TIME_FORMAT,
2907 GST_TIME_ARGS (stamp));
2914 GST_DEBUG_OBJECT (avi, "empty index");
2915 gst_buffer_unref (buf);
2920 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
2921 ("Cannot allocate memory for %u*%u=%u bytes",
2922 (guint) sizeof (GstAviIndexEntry), num,
2923 (guint) sizeof (GstAviIndexEntry) * num));
2924 gst_buffer_unref (buf);
2930 * gst_avi_demux_stream_index:
2931 * @avi: avi demuxer object.
2933 * Seeks to index and reads it.
2936 gst_avi_demux_stream_index (GstAviDemux * avi)
2939 guint64 offset = avi->offset;
2944 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2946 /* get chunk information */
2947 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2948 if (res != GST_FLOW_OK)
2950 else if (GST_BUFFER_SIZE (buf) < 8)
2953 /* check tag first before blindy trying to read 'size' bytes */
2954 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2955 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2956 if (tag == GST_RIFF_TAG_LIST) {
2957 /* this is the movi tag */
2958 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
2959 (8 + GST_ROUND_UP_2 (size)));
2960 offset += 8 + GST_ROUND_UP_2 (size);
2961 gst_buffer_unref (buf);
2962 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2963 if (res != GST_FLOW_OK)
2965 else if (GST_BUFFER_SIZE (buf) < 8)
2967 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2968 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2971 if (tag != GST_RIFF_TAG_idx1)
2976 gst_buffer_unref (buf);
2978 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2980 /* read chunk, advance offset */
2981 if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2982 avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2985 GST_DEBUG ("will parse index chunk size %u for tag %"
2986 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2988 gst_avi_demux_parse_index (avi, buf);
2990 #ifndef GST_DISABLE_GST_DEBUG
2991 /* debug our indexes */
2994 GstAviStream *stream;
2996 for (i = 0; i < avi->num_streams; i++) {
2997 stream = &avi->stream[i];
2998 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
2999 i, stream->idx_n, stream->total_bytes);
3008 GST_DEBUG_OBJECT (avi,
3009 "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
3014 GST_DEBUG_OBJECT (avi, "Buffer is too small");
3015 gst_buffer_unref (buf);
3020 GST_WARNING_OBJECT (avi,
3021 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
3022 GST_FOURCC_ARGS (tag));
3023 gst_buffer_unref (buf);
3028 GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
3029 gst_buffer_unref (buf);
3035 * gst_avi_demux_stream_index_push:
3036 * @avi: avi demuxer object.
3041 gst_avi_demux_stream_index_push (GstAviDemux * avi)
3043 guint64 offset = avi->idx1_offset;
3048 GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
3050 /* get chunk information */
3051 if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
3054 /* check tag first before blindly trying to read 'size' bytes */
3055 if (tag == GST_RIFF_TAG_LIST) {
3056 /* this is the movi tag */
3057 GST_DEBUG_OBJECT (avi, "skip LIST chunk, size %" G_GUINT32_FORMAT,
3058 (8 + GST_ROUND_UP_2 (size)));
3059 avi->idx1_offset = offset + 8 + GST_ROUND_UP_2 (size);
3060 /* issue seek to allow chain function to handle it and return! */
3061 perform_seek_to_offset (avi, avi->idx1_offset);
3065 if (tag != GST_RIFF_TAG_idx1)
3068 GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
3070 /* flush chunk header */
3071 gst_adapter_flush (avi->adapter, 8);
3072 /* read chunk payload */
3073 buf = gst_adapter_take_buffer (avi->adapter, size);
3076 /* advance offset */
3077 offset += 8 + GST_ROUND_UP_2 (size);
3079 GST_DEBUG ("will parse index chunk size %u for tag %"
3080 GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
3082 avi->offset = avi->first_movi_offset;
3083 gst_avi_demux_parse_index (avi, buf);
3085 #ifndef GST_DISABLE_GST_DEBUG
3086 /* debug our indexes */
3089 GstAviStream *stream;
3091 for (i = 0; i < avi->num_streams; i++) {
3092 stream = &avi->stream[i];
3093 GST_DEBUG_OBJECT (avi, "stream %u: %u frames, %" G_GINT64_FORMAT " bytes",
3094 i, stream->idx_n, stream->total_bytes);
3103 GST_DEBUG_OBJECT (avi,
3104 "taking data from adapter failed: pos=%" G_GUINT64_FORMAT " size=%u",
3110 GST_WARNING_OBJECT (avi,
3111 "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
3112 GST_FOURCC_ARGS (tag));
3118 * gst_avi_demux_peek_tag:
3120 * Returns the tag and size of the next chunk
3122 static GstFlowReturn
3123 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
3126 GstFlowReturn res = GST_FLOW_OK;
3127 GstBuffer *buf = NULL;
3131 res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
3132 if (res != GST_FLOW_OK)
3135 bufsize = GST_BUFFER_SIZE (buf);
3139 bufdata = GST_BUFFER_DATA (buf);
3141 *tag = GST_READ_UINT32_LE (bufdata);
3142 *size = GST_READ_UINT32_LE (bufdata + 4);
3144 GST_LOG_OBJECT (avi, "Tag[%" GST_FOURCC_FORMAT "] (size:%d) %"
3145 G_GINT64_FORMAT " -- %" G_GINT64_FORMAT, GST_FOURCC_ARGS (*tag),
3146 *size, offset + 8, offset + 8 + (gint64) * size);
3149 gst_buffer_unref (buf);
3156 GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
3161 GST_DEBUG_OBJECT (avi, "got %d bytes which is <> 8 bytes", bufsize);
3162 res = GST_FLOW_ERROR;
3168 * gst_avi_demux_next_data_buffer:
3170 * Returns the offset and size of the next buffer
3171 * Position is the position of the buffer (after tag and size)
3173 static GstFlowReturn
3174 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
3175 guint32 * tag, guint * size)
3177 guint64 off = *offset;
3182 res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
3183 if (res != GST_FLOW_OK)
3185 if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
3186 off += 8 + 4; /* skip tag + size + subtag */
3197 #ifdef AVIDEMUX_MODIFICATION
3198 /*Modification: Added function to find out the frame_type for index-table generation */
3201 gst_avi_demux_find_frame_type (GstAviStream *stream, GstBuffer *buf, int *frame_type)
3203 unsigned char *buff = GST_BUFFER_DATA (buf);
3204 unsigned int buff_len = GST_BUFFER_SIZE (buf);
3206 switch (stream->strh->fcc_handler)
3208 /* mpeg stream parsing case */
3209 case GST_MAKE_FOURCC ('X', 'V', 'I', 'D'):
3210 case GST_MAKE_FOURCC ('x', 'v', 'i', 'd'):
3211 case GST_MAKE_FOURCC ('D', 'X', '5', '0'):
3212 case GST_MAKE_FOURCC ('d', 'i', 'v', 'x'):
3213 case GST_MAKE_FOURCC ('D', 'I', 'V', 'X'):
3214 case GST_MAKE_FOURCC ('B', 'L', 'Z', '0'):
3215 case GST_MAKE_FOURCC ('F', 'M', 'P', '4'):
3216 case GST_MAKE_FOURCC ('U', 'M', 'P', '4'):
3217 case GST_MAKE_FOURCC ('F', 'F', 'D', 'S'):
3218 case GST_MAKE_FOURCC ('M', 'P', 'E', 'G'):
3219 case GST_MAKE_FOURCC ('M', 'P', 'G', 'I'):
3220 case GST_MAKE_FOURCC ('m', 'p', 'g', '1'):
3221 case GST_MAKE_FOURCC ('M', 'P', 'G', '1'):
3222 case GST_MAKE_FOURCC ('P', 'I', 'M', '1'):
3223 case GST_MAKE_FOURCC ('M', 'P', 'G', '2'):
3224 case GST_MAKE_FOURCC ('m', 'p', 'g', '2'):
3225 case GST_MAKE_FOURCC ('P', 'I', 'M', '2'):
3226 case GST_MAKE_FOURCC ('D', 'V', 'R', ' '):
3229 for (idx=0; idx<(buff_len-3); idx++)
3231 /* Find VOP start frame which should be in every frame */
3232 //GST_DEBUG ("~~~~~~~~~~~~~~~~~~~~%d : %x", idx, buff[idx]);
3233 if (buff[idx] == 0x00 && buff[idx+1] == 0x00 && buff[idx+2] == 0x01 && buff[idx+3] == 0xB6)
3237 if (idx==(buff_len))
3239 GST_ERROR ("Invalid input stream : There isn't any VOP header");
3243 if ((buff[idx] == 0x00) && (buff[idx+1] == 0x00) && (buff[idx+2] == 0x01))
3245 if(buff[idx+3] == 0xB6)
3247 switch (buff[4] & 0xC0)
3250 GST_DEBUG ("Found Key-Frame");
3251 *frame_type = GST_AVI_KEYFRAME;
3254 GST_DEBUG ("Found Non-Key frame.. value = %x", buff[4]);
3255 *frame_type = GST_AVI_NON_KEYFRAME;
3259 else if (buff[3] == 0xB0)
3261 *frame_type = GST_AVI_KEYFRAME;
3266 case GST_MAKE_FOURCC ('H', '2', '6', '3'):
3267 case GST_MAKE_FOURCC ('h', '2', '6', '3'):
3268 case GST_MAKE_FOURCC ('i', '2', '6', '3'):
3269 case GST_MAKE_FOURCC ('U', '2', '6', '3'):
3270 case GST_MAKE_FOURCC ('v', 'i', 'v', '1'):
3271 case GST_MAKE_FOURCC ('T', '2', '6', '3'):
3273 /* TODO: H263 Frame Parsing */
3274 *frame_type = GST_AVI_KEYFRAME;
3277 case GST_MAKE_FOURCC ('X', '2', '6', '4'):
3278 case GST_MAKE_FOURCC ('x', '2', '6', '4'):
3279 case GST_MAKE_FOURCC ('H', '2', '6', '4'):
3280 case GST_MAKE_FOURCC ('h', '2', '6', '4'):
3281 case GST_MAKE_FOURCC ('a', 'v', 'c', '1'):
3282 case GST_MAKE_FOURCC ('A', 'V', 'C', '1'):
3285 gint nalu_type = H264_NUT_UNKNOWN;
3286 /* H264 Frame Parsing */
3289 if (buff[idx+0] == 0x00 &&
3290 buff[idx+1] == 0x00 &&
3291 ((buff [idx+2] == 0x01) || ((buff [idx+2] == 0x00) && (buff [idx+3] == 0x01))))
3293 if (buff [idx+2] == 0x01)
3295 nalu_type = buff[idx +3] & 0x1f;
3297 else if ((buff [idx+2] == 0x00) && (buff [idx+3] == 0x01))
3299 nalu_type = buff[idx +4] & 0x1f;
3301 if ((nalu_type == H264_NUT_SPS) ||
3302 (nalu_type == H264_NUT_PPS) ||
3303 (nalu_type == H264_NUT_SEI) ||
3304 (nalu_type == H264_NUT_AUD))
3306 GST_DEBUG ("Skipping NALU SPS/PPS/SEI/AUD...");
3308 else if (nalu_type == H264_NUT_IDR)
3310 GST_DEBUG ("Found KEY frame...\n");
3311 *frame_type = GST_AVI_KEYFRAME;
3314 else if ((nalu_type == H264_NUT_SLICE) ||
3315 (nalu_type == H264_NUT_DPA) ||
3316 (nalu_type == H264_NUT_DPB) ||
3317 (nalu_type == H264_NUT_DPC) ||
3318 (nalu_type == H264_NUT_EOSEQ) ||
3319 (nalu_type == H264_NUT_EOSTREAM))
3321 *frame_type = GST_AVI_NON_KEYFRAME;
3326 GST_DEBUG ("Unknown frame type, val = %d...", *frame_type);
3327 *frame_type = GST_AVI_NON_KEYFRAME;
3336 }while (idx < (buff_len - 4));
3340 //naveen: default make all frames as key frames
3341 *frame_type = GST_AVI_KEYFRAME;
3351 * gst_avi_demux_stream_scan:
3352 * @avi: calling element (used for debugging/errors).
3354 * Scan the file for all chunks to "create" a new index.
3358 gst_avi_demux_stream_scan (GstAviDemux * avi)
3361 GstAviStream *stream;
3370 * - implement non-seekable source support.
3372 GST_DEBUG_OBJECT (avi, "Creating index");
3374 /* get the size of the file */
3375 format = GST_FORMAT_BYTES;
3376 if (!gst_pad_query_peer_duration (avi->sinkpad, &format, &tmplength))
3380 /* guess the total amount of entries we expect */
3384 GstAviIndexEntry entry;
3386 #ifdef AVIDEMUX_MODIFICATION
3387 gint frame_type = GST_AVI_KEYFRAME;
3390 /* start reading data buffers to find the id and offset */
3391 res = gst_avi_demux_next_data_buffer (avi, &pos, &tag, &size);
3392 if (G_UNLIKELY (res != GST_FLOW_OK))
3396 stream = gst_avi_demux_stream_for_id (avi, tag);
3397 if (G_UNLIKELY (!stream))
3399 #ifdef AVIDEMUX_MODIFICATION
3400 /*Modification: Added logic to generate the index table with key frames */
3402 /* naveen: generating index table with key frames */
3403 if (stream->strh->type == GST_RIFF_FCC_vids)
3405 GstBuffer *buf = NULL;
3408 res = gst_pad_pull_range (avi->sinkpad, pos, size, &buf);
3409 if (res != GST_FLOW_OK)
3411 gst_buffer_unref (buf);
3412 GST_ERROR ("[Naveen] Pull failed....\n\n");
3416 ret = gst_avi_demux_find_frame_type (stream, buf, &frame_type);
3420 gst_buffer_unref (buf);
3423 entry.flags = frame_type;
3428 /* and add to the index of this stream */
3429 if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
3433 /* update position */
3434 pos += GST_ROUND_UP_2 (size);
3435 if (G_UNLIKELY (pos > length)) {
3436 GST_WARNING_OBJECT (avi,
3437 "Stopping index lookup since we are further than EOF");
3443 avi->have_index = gst_avi_demux_do_index_stats (avi);
3450 GST_ELEMENT_ERROR (avi, RESOURCE, NO_SPACE_LEFT, (NULL),
3451 ("Cannot allocate memory for %u*%u=%u bytes",
3452 (guint) sizeof (GstAviIndexEntry), num,
3453 (guint) sizeof (GstAviIndexEntry) * num));
3459 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
3463 GstAviStream *stream;
3465 total = GST_CLOCK_TIME_NONE;
3467 /* all streams start at a timestamp 0 */
3468 for (i = 0; i < avi->num_streams; i++) {
3469 GstClockTime duration, hduration;
3470 gst_riff_strh *strh;
3472 stream = &avi->stream[i];
3473 if (G_UNLIKELY (!stream || !stream->idx_n || !(strh = stream->strh)))
3476 /* get header duration for the stream */
3477 hduration = stream->hdr_duration;
3478 /* index duration calculated during parsing */
3479 duration = stream->idx_duration;
3481 /* now pick a good duration */
3482 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3483 /* index gave valid duration, use that */
3484 GST_INFO ("Stream %p duration according to index: %" GST_TIME_FORMAT,
3485 stream, GST_TIME_ARGS (duration));
3487 /* fall back to header info to calculate a duration */
3488 duration = hduration;
3490 GST_INFO ("Setting duration of stream #%d to %" GST_TIME_FORMAT,
3491 i, GST_TIME_ARGS (duration));
3492 /* set duration for the stream */
3493 stream->duration = duration;
3495 /* find total duration */
3496 if (total == GST_CLOCK_TIME_NONE ||
3497 (GST_CLOCK_TIME_IS_VALID (duration) && duration > total))
3501 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
3502 /* now update the duration for those streams where we had none */
3503 for (i = 0; i < avi->num_streams; i++) {
3504 stream = &avi->stream[i];
3506 if (!GST_CLOCK_TIME_IS_VALID (stream->duration)
3507 || stream->duration == 0) {
3508 stream->duration = total;
3510 GST_INFO ("Stream %p duration according to total: %" GST_TIME_FORMAT,
3511 stream, GST_TIME_ARGS (total));
3516 /* and set the total duration in the segment. */
3517 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
3518 GST_TIME_ARGS (total));
3520 gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
3523 #ifdef AVIDEMUX_MODIFICATION
3525 gst_avi_demux_calculate_durations_from_strh (GstAviDemux * avi)
3529 GstAviStream *stream;
3531 total = GST_CLOCK_TIME_NONE;
3533 /* all streams start at a timestamp 0 */
3534 for (i = 0; i < avi->num_streams; i++) {
3535 GstClockTime hduration;
3536 gst_riff_strh *strh;
3538 stream = &avi->stream[i];
3540 if (G_UNLIKELY (!stream || !(strh = stream->strh)))
3543 /* get header duration for the stream */
3544 hduration = stream->hdr_duration;
3546 /* check duration */
3547 if (GST_CLOCK_TIME_IS_VALID (hduration)) {
3548 GST_INFO ("Stream %p duration according to strh: %" GST_TIME_FORMAT,
3549 stream, GST_TIME_ARGS (hduration));
3552 GST_INFO ("Setting duration of stream #%d to %" GST_TIME_FORMAT,
3553 i, GST_TIME_ARGS (hduration));
3555 /* set duration for the stream */
3556 stream->duration = hduration;
3558 /* find total duration */
3559 if (total == GST_CLOCK_TIME_NONE ||
3560 (GST_CLOCK_TIME_IS_VALID (hduration) && hduration > total))
3564 if (GST_CLOCK_TIME_IS_VALID (total) && (total > 0)) {
3565 /* now update the duration for those streams where we had none */
3566 for (i = 0; i < avi->num_streams; i++) {
3567 stream = &avi->stream[i];
3569 if (!GST_CLOCK_TIME_IS_VALID (stream->duration)
3570 || stream->duration == 0) {
3571 stream->duration = total;
3573 GST_INFO ("Stream %p duration according to total: %" GST_TIME_FORMAT,
3574 stream, GST_TIME_ARGS (total));
3579 /* and set the total duration in the segment. */
3580 GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
3581 GST_TIME_ARGS (total));
3583 gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
3587 /* returns FALSE if there are no pads to deliver event to,
3588 * otherwise TRUE (whatever the outcome of event sending),
3589 * takes ownership of the event. */
3591 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
3593 gboolean result = FALSE;
3596 GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
3597 GST_EVENT_TYPE_NAME (event), avi->num_streams);
3599 for (i = 0; i < avi->num_streams; i++) {
3600 GstAviStream *stream = &avi->stream[i];
3604 gst_pad_push_event (stream->pad, gst_event_ref (event));
3607 gst_event_unref (event);
3612 gst_avi_demux_check_seekability (GstAviDemux * avi)
3615 gboolean seekable = FALSE;
3616 gint64 start = -1, stop = -1;
3618 query = gst_query_new_seeking (GST_FORMAT_BYTES);
3619 if (!gst_pad_peer_query (avi->sinkpad, query)) {
3620 GST_DEBUG_OBJECT (avi, "seeking query failed");
3624 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
3626 /* try harder to query upstream size if we didn't get it the first time */
3627 if (seekable && stop == -1) {
3628 GstFormat fmt = GST_FORMAT_BYTES;
3630 GST_DEBUG_OBJECT (avi, "doing duration query to fix up unset stop");
3631 gst_pad_query_peer_duration (avi->sinkpad, &fmt, &stop);
3634 /* if upstream doesn't know the size, it's likely that it's not seekable in
3635 * practice even if it technically may be seekable */
3636 if (seekable && (start != 0 || stop <= start)) {
3637 GST_DEBUG_OBJECT (avi, "seekable but unknown start/stop -> disable");
3641 if (!avi->element_index) {
3642 GST_DEBUG_OBJECT (avi, "no index");
3647 GST_INFO_OBJECT (avi, "seekable: %d (%" G_GUINT64_FORMAT " - %"
3648 G_GUINT64_FORMAT ")", seekable, start, stop);
3649 avi->seekable = seekable;
3651 gst_query_unref (query);
3655 * Read AVI headers when streaming
3657 static GstFlowReturn
3658 gst_avi_demux_stream_header_push (GstAviDemux * avi)
3660 GstFlowReturn ret = GST_FLOW_OK;
3665 GstBuffer *buf = NULL, *sub = NULL;
3669 GstTagList *tags = NULL;
3671 GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
3673 switch (avi->header_state) {
3674 case GST_AVI_DEMUX_HEADER_TAG_LIST:
3675 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3676 avi->offset += 8 + GST_ROUND_UP_2 (size);
3677 if (tag != GST_RIFF_TAG_LIST)
3678 goto header_no_list;
3680 gst_adapter_flush (avi->adapter, 8);
3681 /* Find the 'hdrl' LIST tag */
3682 GST_DEBUG ("Reading %d bytes", size);
3683 buf = gst_adapter_take_buffer (avi->adapter, size);
3685 if (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl)
3686 goto header_no_hdrl;
3690 gst_adapter_flush (avi->adapter, 1);
3692 GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
3694 gst_avi_demux_roundup_list (avi, &buf);
3696 /* the hdrl starts with a 'avih' header */
3697 if (!gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3699 goto header_no_avih;
3701 if (tag != GST_RIFF_TAG_avih)
3702 goto header_no_avih;
3704 if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3705 goto header_wrong_avih;
3707 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
3709 /* now, read the elements from the header until the end */
3710 while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3712 /* sub can be NULL on empty tags */
3717 case GST_RIFF_TAG_LIST:
3718 if (GST_BUFFER_SIZE (sub) < 4)
3721 switch (GST_READ_UINT32_LE (GST_BUFFER_DATA (sub))) {
3722 case GST_RIFF_LIST_strl:
3723 if (!(gst_avi_demux_parse_stream (avi, sub))) {
3725 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3726 ("failed to parse stream, ignoring"));
3731 case GST_RIFF_LIST_odml:
3732 gst_avi_demux_parse_odml (avi, sub);
3736 GST_WARNING_OBJECT (avi,
3737 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
3738 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA
3741 case GST_RIFF_TAG_JUNQ:
3742 case GST_RIFF_TAG_JUNK:
3747 gst_avi_demux_parse_idit (avi, sub);
3750 GST_WARNING_OBJECT (avi,
3751 "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
3752 offset, GST_FOURCC_ARGS (tag));
3754 case GST_RIFF_TAG_JUNQ:
3755 case GST_RIFF_TAG_JUNK:
3757 /* move to next chunk */
3759 gst_buffer_unref (sub);
3764 gst_buffer_unref (buf);
3765 GST_DEBUG ("elements parsed");
3767 /* check parsed streams */
3768 if (avi->num_streams == 0) {
3770 } else if (avi->num_streams != avi->avih->streams) {
3771 GST_WARNING_OBJECT (avi,
3772 "Stream header mentioned %d streams, but %d available",
3773 avi->avih->streams, avi->num_streams);
3775 GST_DEBUG ("Get junk and info next");
3776 avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
3778 /* Need more data */
3782 case GST_AVI_DEMUX_HEADER_INFO:
3783 GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
3785 if (gst_adapter_available (avi->adapter) < 12)
3788 data = gst_adapter_peek (avi->adapter, 12);
3789 tag = GST_READ_UINT32_LE (data);
3790 size = GST_READ_UINT32_LE (data + 4);
3791 ltag = GST_READ_UINT32_LE (data + 8);
3793 if (tag == GST_RIFF_TAG_LIST) {
3795 case GST_RIFF_LIST_movi:
3796 gst_adapter_flush (avi->adapter, 12);
3797 if (!avi->first_movi_offset)
3798 avi->first_movi_offset = avi->offset;
3800 avi->idx1_offset = avi->offset + size - 4;
3802 case GST_RIFF_LIST_INFO:
3803 GST_DEBUG ("Found INFO chunk");
3804 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3805 GST_DEBUG ("got size %d", size);
3807 gst_adapter_flush (avi->adapter, 12);
3809 buf = gst_adapter_take_buffer (avi->adapter, size - 4);
3812 gst_adapter_flush (avi->adapter, 1);
3813 gst_riff_parse_info (GST_ELEMENT_CAST (avi), buf, &tags);
3815 if (avi->globaltags) {
3816 gst_tag_list_insert (avi->globaltags, tags,
3817 GST_TAG_MERGE_REPLACE);
3819 avi->globaltags = tags;
3823 gst_buffer_unref (buf);
3825 avi->offset += GST_ROUND_UP_2 (size) - 4;
3827 GST_DEBUG ("skipping INFO LIST prefix");
3830 /* Need more data */
3835 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3836 avi->offset += 8 + GST_ROUND_UP_2 (size);
3837 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3838 // ??? goto iterate; ???
3840 /* Need more data */
3846 if (gst_avi_demux_peek_chunk (avi, &tag, &size)) {
3847 avi->offset += 8 + GST_ROUND_UP_2 (size);
3848 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
3851 /* Need more data */
3858 GST_WARNING ("unhandled header state: %d", avi->header_state);
3863 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3864 avi->num_streams, avi->stream[0].indexes);
3866 GST_DEBUG ("Found movi chunk. Starting to stream data");
3867 avi->state = GST_AVI_DEMUX_MOVI;
3869 #ifdef AVIDEMUX_MODIFICATION
3870 /*no indexs in push mode, but it could be get from strh chunk */
3871 gst_avi_demux_calculate_durations_from_strh (avi);
3873 /* no indexes in push mode, but it still sets some variables */
3874 gst_avi_demux_calculate_durations_from_index (avi);
3877 gst_avi_demux_expose_streams (avi, TRUE);
3879 /* prepare all streams for index 0 */
3880 for (i = 0; i < avi->num_streams; i++)
3881 avi->stream[i].current_entry = 0;
3883 /* create initial NEWSEGMENT event */
3884 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3885 stop = avi->segment.duration;
3887 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3890 gst_event_unref (avi->seg_event);
3891 avi->seg_event = gst_event_new_new_segment_full
3892 (FALSE, avi->segment.rate, avi->segment.applied_rate, GST_FORMAT_TIME,
3893 avi->segment.start, stop, avi->segment.time);
3895 gst_avi_demux_check_seekability (avi);
3897 /* at this point we know all the streams and we can signal the no more
3899 GST_DEBUG_OBJECT (avi, "signaling no more pads");
3900 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3907 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3908 return GST_FLOW_ERROR;
3912 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3913 ("Invalid AVI header (no LIST at start): %"
3914 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3915 return GST_FLOW_ERROR;
3919 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3920 ("Invalid AVI header (no hdrl at start): %"
3921 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3922 gst_buffer_unref (buf);
3923 return GST_FLOW_ERROR;
3927 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3928 ("Invalid AVI header (no avih at start): %"
3929 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3931 gst_buffer_unref (sub);
3933 gst_buffer_unref (buf);
3934 return GST_FLOW_ERROR;
3938 gst_buffer_unref (buf);
3939 return GST_FLOW_ERROR;
3944 gst_avi_demux_add_date_tag (GstAviDemux * avi, gint y, gint m, gint d,
3945 gint h, gint min, gint s)
3950 date = g_date_new_dmy (d, m, y);
3951 if (!g_date_valid (date)) {
3953 GST_WARNING_OBJECT (avi, "Refusing to add invalid date %d-%d-%d", y, m, d);
3958 dt = gst_date_time_new_local_time (y, m, d, h, min, s);
3960 if (avi->globaltags == NULL)
3961 avi->globaltags = gst_tag_list_new ();
3963 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE, date,
3967 gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE_TIME,
3969 gst_date_time_unref (dt);
3974 gst_avi_demux_parse_idit_nums_only (GstAviDemux * avi, gchar * data)
3977 gint hr = 0, min = 0, sec = 0;
3980 GST_DEBUG ("data : '%s'", data);
3982 ret = sscanf (data, "%d:%d:%d %d:%d:%d", &y, &m, &d, &hr, &min, &sec);
3984 /* Attempt YYYY/MM/DD/ HH:MM variant (found in CASIO cameras) */
3985 ret = sscanf (data, "%04d/%02d/%02d/ %d:%d", &y, &m, &d, &hr, &min);
3987 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3991 gst_avi_demux_add_date_tag (avi, y, m, d, hr, min, sec);
3995 get_month_num (gchar * data, guint size)
3997 if (g_ascii_strncasecmp (data, "jan", 3) == 0) {
3999 } else if (g_ascii_strncasecmp (data, "feb", 3) == 0) {
4001 } else if (g_ascii_strncasecmp (data, "mar", 3) == 0) {
4003 } else if (g_ascii_strncasecmp (data, "apr", 3) == 0) {
4005 } else if (g_ascii_strncasecmp (data, "may", 3) == 0) {
4007 } else if (g_ascii_strncasecmp (data, "jun", 3) == 0) {
4009 } else if (g_ascii_strncasecmp (data, "jul", 3) == 0) {
4011 } else if (g_ascii_strncasecmp (data, "aug", 3) == 0) {
4013 } else if (g_ascii_strncasecmp (data, "sep", 3) == 0) {
4015 } else if (g_ascii_strncasecmp (data, "oct", 3) == 0) {
4017 } else if (g_ascii_strncasecmp (data, "nov", 3) == 0) {
4019 } else if (g_ascii_strncasecmp (data, "dec", 3) == 0) {
4027 gst_avi_demux_parse_idit_text (GstAviDemux * avi, gchar * data)
4029 gint year, month, day;
4030 gint hour, min, sec;
4035 ret = sscanf (data, "%3s %3s %d %d:%d:%d %d", weekday, monthstr, &day, &hour,
4038 GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
4041 month = get_month_num (monthstr, strlen (monthstr));
4042 gst_avi_demux_add_date_tag (avi, year, month, day, hour, min, sec);
4046 gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf)
4048 gchar *data = (gchar *) GST_BUFFER_DATA (buf);
4049 guint size = GST_BUFFER_SIZE (buf);
4050 gchar *safedata = NULL;
4054 * http://www.eden-foundation.org/products/code/film_date_stamp/index.html
4056 * This tag could be in one of the below formats
4057 * 2005:08:17 11:42:43
4058 * THU OCT 26 16:46:04 2006
4059 * Mon Mar 3 09:44:56 2008
4061 * FIXME: Our date tag doesn't include hours
4064 /* skip eventual initial whitespace */
4065 while (size > 0 && g_ascii_isspace (data[0])) {
4074 /* make a safe copy to add a \0 to the end of the string */
4075 safedata = g_strndup (data, size);
4077 /* test if the first char is a alpha or a number */
4078 if (g_ascii_isdigit (data[0])) {
4079 gst_avi_demux_parse_idit_nums_only (avi, safedata);
4082 } else if (g_ascii_isalpha (data[0])) {
4083 gst_avi_demux_parse_idit_text (avi, safedata);
4091 GST_WARNING_OBJECT (avi, "IDIT tag has no parsable info");
4095 * Read full AVI headers.
4097 static GstFlowReturn
4098 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
4101 GstBuffer *buf, *sub = NULL;
4105 GstElement *element = GST_ELEMENT_CAST (avi);
4107 GstTagList *tags = NULL;
4109 stamp = gst_util_get_timestamp ();
4111 /* the header consists of a 'hdrl' LIST tag */
4112 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
4113 if (res != GST_FLOW_OK)
4114 goto pull_range_failed;
4115 else if (tag != GST_RIFF_TAG_LIST)
4117 else if (GST_BUFFER_SIZE (buf) < 4)
4120 GST_DEBUG_OBJECT (avi, "parsing headers");
4122 /* Find the 'hdrl' LIST tag */
4123 while (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl) {
4124 GST_LOG_OBJECT (avi, "buffer contains %" GST_FOURCC_FORMAT,
4125 GST_FOURCC_ARGS (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf))));
4128 gst_buffer_unref (buf);
4130 /* read new chunk */
4131 res = gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag, &buf);
4132 if (res != GST_FLOW_OK)
4133 goto pull_range_failed;
4134 else if (tag != GST_RIFF_TAG_LIST)
4136 else if (GST_BUFFER_SIZE (buf) < 4)
4140 GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
4142 gst_avi_demux_roundup_list (avi, &buf);
4144 /* the hdrl starts with a 'avih' header */
4145 if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
4147 else if (tag != GST_RIFF_TAG_avih)
4149 else if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
4152 GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
4154 /* now, read the elements from the header until the end */
4155 while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
4156 /* sub can be NULL on empty tags */
4161 case GST_RIFF_TAG_LIST:
4166 if (GST_BUFFER_SIZE (sub) < 4)
4169 data = GST_BUFFER_DATA (sub);
4170 fourcc = GST_READ_UINT32_LE (data);
4173 case GST_RIFF_LIST_strl:
4174 if (!(gst_avi_demux_parse_stream (avi, sub))) {
4175 GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
4176 ("failed to parse stream, ignoring"));
4179 goto invalid_stream;
4184 case GST_RIFF_LIST_odml:
4185 gst_avi_demux_parse_odml (avi, sub);
4188 case GST_RIFF_LIST_INFO:
4189 GST_BUFFER_DATA (sub) = data + 4;
4190 GST_BUFFER_SIZE (sub) -= 4;
4191 gst_riff_parse_info (element, sub, &tags);
4193 if (avi->globaltags) {
4194 gst_tag_list_insert (avi->globaltags, tags,
4195 GST_TAG_MERGE_REPLACE);
4197 avi->globaltags = tags;
4203 GST_WARNING_OBJECT (avi,
4204 "Unknown list %" GST_FOURCC_FORMAT " in AVI header",
4205 GST_FOURCC_ARGS (fourcc));
4206 GST_MEMDUMP_OBJECT (avi, "Unknown list", GST_BUFFER_DATA (sub),
4207 GST_BUFFER_SIZE (sub));
4209 case GST_RIFF_TAG_JUNQ:
4210 case GST_RIFF_TAG_JUNK:
4216 gst_avi_demux_parse_idit (avi, sub);
4219 GST_WARNING_OBJECT (avi,
4220 "Unknown tag %" GST_FOURCC_FORMAT " in AVI header at off %d",
4221 GST_FOURCC_ARGS (tag), offset);
4222 GST_MEMDUMP_OBJECT (avi, "Unknown tag", GST_BUFFER_DATA (sub),
4223 GST_BUFFER_SIZE (sub));
4225 case GST_RIFF_TAG_JUNQ:
4226 case GST_RIFF_TAG_JUNK:
4229 gst_buffer_unref (sub);
4234 gst_buffer_unref (buf);
4235 GST_DEBUG ("elements parsed");
4237 /* check parsed streams */
4238 if (avi->num_streams == 0)
4240 else if (avi->num_streams != avi->avih->streams) {
4241 GST_WARNING_OBJECT (avi,
4242 "Stream header mentioned %d streams, but %d available",
4243 avi->avih->streams, avi->num_streams);
4246 GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
4247 G_GUINT64_FORMAT, avi->offset);
4249 /* Now, find the data (i.e. skip all junk between header and data) */
4255 res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
4256 if (res != GST_FLOW_OK) {
4257 GST_DEBUG_OBJECT (avi, "pull_range failure while looking for tags");
4258 goto pull_range_failed;
4259 } else if (GST_BUFFER_SIZE (buf) < 12) {
4260 GST_DEBUG_OBJECT (avi, "got %d bytes which is less than 12 bytes",
4261 GST_BUFFER_SIZE (buf));
4262 gst_buffer_unref (buf);
4263 return GST_FLOW_ERROR;
4266 data = GST_BUFFER_DATA (buf);
4268 tag = GST_READ_UINT32_LE (data);
4269 size = GST_READ_UINT32_LE (data + 4);
4270 ltag = GST_READ_UINT32_LE (data + 8);
4272 GST_DEBUG ("tag %" GST_FOURCC_FORMAT ", size %u",
4273 GST_FOURCC_ARGS (tag), size);
4274 GST_MEMDUMP ("Tag content", data, GST_BUFFER_SIZE (buf));
4275 gst_buffer_unref (buf);
4278 case GST_RIFF_TAG_LIST:{
4280 case GST_RIFF_LIST_movi:
4281 GST_DEBUG_OBJECT (avi,
4282 "Reached the 'movi' tag, we're done with skipping");
4284 case GST_RIFF_LIST_INFO:
4286 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
4288 if (res != GST_FLOW_OK) {
4289 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
4290 goto pull_range_failed;
4292 GST_DEBUG ("got size %u", GST_BUFFER_SIZE (buf));
4294 GST_DEBUG ("skipping INFO LIST prefix");
4295 avi->offset += (4 - GST_ROUND_UP_2 (size));
4296 gst_buffer_unref (buf);
4300 sub = gst_buffer_create_sub (buf, 4, GST_BUFFER_SIZE (buf) - 4);
4301 gst_riff_parse_info (element, sub, &tags);
4303 if (avi->globaltags) {
4304 gst_tag_list_insert (avi->globaltags, tags,
4305 GST_TAG_MERGE_REPLACE);
4307 avi->globaltags = tags;
4312 gst_buffer_unref (sub);
4315 gst_buffer_unref (buf);
4316 /* gst_riff_read_chunk() has already advanced avi->offset */
4319 GST_WARNING_OBJECT (avi,
4320 "Skipping unknown list tag %" GST_FOURCC_FORMAT,
4321 GST_FOURCC_ARGS (ltag));
4322 avi->offset += 8 + GST_ROUND_UP_2 (size);
4328 GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
4329 GST_FOURCC_ARGS (tag));
4331 case GST_MAKE_FOURCC ('J', 'U', 'N', 'Q'):
4332 case GST_MAKE_FOURCC ('J', 'U', 'N', 'K'):
4333 /* Only get buffer for debugging if the memdump is needed */
4334 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= 9) {
4335 res = gst_pad_pull_range (avi->sinkpad, avi->offset, size, &buf);
4336 if (res != GST_FLOW_OK) {
4337 GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
4338 goto pull_range_failed;
4340 GST_MEMDUMP ("Junk", GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
4341 gst_buffer_unref (buf);
4343 avi->offset += 8 + GST_ROUND_UP_2 (size);
4349 GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
4350 avi->num_streams, avi->stream[0].indexes);
4352 /* create or read stream index (for seeking) */
4353 if (avi->stream[0].indexes != NULL) {
4354 /* we read a super index already (gst_avi_demux_parse_superindex() ) */
4355 gst_avi_demux_read_subindexes_pull (avi);
4357 if (!avi->have_index) {
4358 if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
4359 gst_avi_demux_stream_index (avi);
4361 /* still no index, scan */
4362 if (!avi->have_index) {
4363 gst_avi_demux_stream_scan (avi);
4365 /* still no index.. this is a fatal error for now.
4366 * FIXME, we should switch to plain push mode without seeking
4367 * instead of failing. */
4368 if (!avi->have_index)
4372 /* use the indexes now to construct nice durations */
4373 gst_avi_demux_calculate_durations_from_index (avi);
4375 gst_avi_demux_expose_streams (avi, FALSE);
4377 /* create initial NEWSEGMENT event */
4378 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
4379 stop = avi->segment.duration;
4381 GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
4383 /* do initial seek to the default segment values */
4384 gst_avi_demux_do_seek (avi, &avi->segment);
4386 /* prepare initial segment */
4388 gst_event_unref (avi->seg_event);
4389 avi->seg_event = gst_event_new_new_segment_full
4390 (FALSE, avi->segment.rate, avi->segment.applied_rate, GST_FORMAT_TIME,
4391 avi->segment.start, stop, avi->segment.time);
4393 stamp = gst_util_get_timestamp () - stamp;
4394 GST_DEBUG_OBJECT (avi, "pulling header took %" GST_TIME_FORMAT,
4395 GST_TIME_ARGS (stamp));
4397 /* at this point we know all the streams and we can signal the no more
4399 GST_DEBUG_OBJECT (avi, "signaling no more pads");
4400 gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
4407 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
4408 ("Invalid AVI header (no LIST at start): %"
4409 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
4410 gst_buffer_unref (buf);
4411 return GST_FLOW_ERROR;
4415 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
4416 ("Invalid AVI header (no hdrl at start): %"
4417 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
4418 gst_buffer_unref (buf);
4419 return GST_FLOW_ERROR;
4423 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
4424 ("Invalid AVI header (no avih at start): %"
4425 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
4427 gst_buffer_unref (sub);
4428 gst_buffer_unref (buf);
4429 return GST_FLOW_ERROR;
4433 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
4434 ("Invalid AVI header (cannot parse avih at start)"));
4435 gst_buffer_unref (buf);
4436 return GST_FLOW_ERROR;
4440 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
4441 return GST_FLOW_ERROR;
4445 GST_WARNING ("file without or too big index");
4446 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
4447 ("Could not get/create index"));
4448 return GST_FLOW_ERROR;
4452 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
4453 ("pull_range flow reading header: %s", gst_flow_get_name (res)));
4454 return GST_FLOW_ERROR;
4459 gst_buffer_unref(buf);
4460 return GST_FLOW_ERROR;
4465 /* move a stream to @index */
4467 gst_avi_demux_move_stream (GstAviDemux * avi, GstAviStream * stream,
4468 GstSegment * segment, guint index)
4470 GST_DEBUG_OBJECT (avi, "Move stream %d to %u", stream->num, index);
4472 if (segment->rate < 0.0) {
4474 /* Because we don't know the frame order we need to push from the prev keyframe
4475 * to the next keyframe. If there is a smart decoder downstream he will notice
4476 * that there are too many encoded frames send and return UNEXPECTED when there
4477 * are enough decoded frames to fill the segment. */
4478 next_key = gst_avi_demux_index_next (avi, stream, index, TRUE);
4480 /* FIXME, we go back to 0, we should look at segment.start. We will however
4481 * stop earlier when the see the timestamp < segment.start */
4482 stream->start_entry = 0;
4483 stream->step_entry = index;
4484 stream->current_entry = index;
4485 stream->stop_entry = next_key;
4487 GST_DEBUG_OBJECT (avi, "reverse seek: start %u, step %u, stop %u",
4488 stream->start_entry, stream->step_entry, stream->stop_entry);
4490 stream->start_entry = index;
4491 stream->step_entry = index;
4492 stream->stop_entry = gst_avi_demux_index_last (avi, stream);
4494 if (stream->current_entry != index) {
4495 GST_DEBUG_OBJECT (avi, "Move DISCONT from %u to %u",
4496 stream->current_entry, index);
4497 stream->current_entry = index;
4498 stream->discont = TRUE;
4501 /* update the buffer info */
4502 gst_avi_demux_get_buffer_info (avi, stream, index,
4503 &stream->current_timestamp, &stream->current_ts_end,
4504 &stream->current_offset, &stream->current_offset_end);
4506 GST_DEBUG_OBJECT (avi, "Moved to %u, ts %" GST_TIME_FORMAT
4507 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4508 ", off_end %" G_GUINT64_FORMAT, index,
4509 GST_TIME_ARGS (stream->current_timestamp),
4510 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4511 stream->current_offset_end);
4513 GST_DEBUG_OBJECT (avi, "Seeking to offset %" G_GUINT64_FORMAT,
4514 stream->index[index].offset);
4518 * Do the actual seeking.
4521 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
4523 GstClockTime seek_time;
4526 GstAviStream *stream;
4528 seek_time = segment->last_stop;
4529 keyframe = ! !(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
4531 GST_DEBUG_OBJECT (avi, "seek to: %" GST_TIME_FORMAT
4532 " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
4534 /* FIXME, this code assumes the main stream with keyframes is stream 0,
4535 * which is mostly correct... */
4536 stream = &avi->stream[avi->main_stream];
4538 /* get the entry index for the requested position */
4539 index = gst_avi_demux_index_for_time (avi, stream, seek_time);
4540 GST_DEBUG_OBJECT (avi, "Got entry %u", index);
4541 #ifdef AVIDEMUX_MODIFICATION
4542 /*Modification: Conditions added to support trickplay*/
4543 if(segment->rate<=1.0 && segment->rate>=0.0)
4546 /* check if we are already on a keyframe */
4547 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
4548 GST_DEBUG_OBJECT (avi, "not keyframe, searching back");
4549 /* now go to the previous keyframe, this is where we should start
4551 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4552 GST_DEBUG_OBJECT (avi, "previous keyframe at %u", index);
4555 /* move the main stream to this position */
4556 gst_avi_demux_move_stream (avi, stream, segment, index);
4559 /* when seeking to a keyframe, we update the result seek time
4560 * to the time of the keyframe. */
4561 seek_time = stream->current_timestamp;
4562 GST_DEBUG_OBJECT (avi, "keyframe adjusted to %" GST_TIME_FORMAT,
4563 GST_TIME_ARGS (seek_time));
4566 /* the seek time is also the last_stop and stream time when going
4568 segment->last_stop = seek_time;
4569 if (segment->rate > 0.0)
4571 #ifndef AVIDEMUX_MODIFICATION
4572 /*initialization of rate params */ //Kishore
4573 stream->next_kindex=0;
4574 stream->prev_kindex=0;
4575 stream->total_samples_bet_2_keyframes=0;
4576 stream->audio_frame_count=0;
4578 segment->time = seek_time;
4582 /* now set DISCONT and align the other streams */
4583 for (i = 0; i < avi->num_streams; i++) {
4584 GstAviStream *ostream;
4586 ostream = &avi->stream[i];
4587 if ((ostream == stream) || (ostream->index == NULL))
4590 /* get the entry index for the requested position */
4591 index = gst_avi_demux_index_for_time (avi, ostream, seek_time);
4593 #ifdef AVIDEMUX_MODIFICATION
4594 /*Modification: Conditions added to support trickplay */
4595 if(segment->rate<=1.0 && segment->rate>=0.0)
4598 /* move to previous keyframe */
4599 if (!ENTRY_IS_KEYFRAME (&ostream->index[index]))
4600 index = gst_avi_demux_index_prev (avi, ostream, index, TRUE);
4603 gst_avi_demux_move_stream (avi, ostream, segment, index);
4605 GST_DEBUG_OBJECT (avi, "done seek to: %" GST_TIME_FORMAT,
4606 GST_TIME_ARGS (seek_time));
4612 * Handle seek event in pull mode.
4615 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4620 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4624 GstSegment seeksegment = { 0, };
4628 GST_DEBUG_OBJECT (avi, "doing seek with event");
4630 gst_event_parse_seek (event, &rate, &format, &flags,
4631 &cur_type, &cur, &stop_type, &stop);
4633 /* we have to have a format as the segment format. Try to convert
4635 if (format != GST_FORMAT_TIME) {
4636 GstFormat fmt = GST_FORMAT_TIME;
4637 gboolean res = TRUE;
4639 if (cur_type != GST_SEEK_TYPE_NONE)
4640 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
4641 if (res && stop_type != GST_SEEK_TYPE_NONE)
4642 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
4648 GST_DEBUG_OBJECT (avi,
4649 "seek requested: rate %g cur %" GST_TIME_FORMAT " stop %"
4650 GST_TIME_FORMAT, rate, GST_TIME_ARGS (cur), GST_TIME_ARGS (stop));
4651 /* FIXME: can we do anything with rate!=1.0 */
4653 GST_DEBUG_OBJECT (avi, "doing seek without event");
4658 /* save flush flag */
4659 flush = flags & GST_SEEK_FLAG_FLUSH;
4662 GstEvent *fevent = gst_event_new_flush_start ();
4664 /* for a flushing seek, we send a flush_start on all pads. This will
4665 * eventually stop streaming with a WRONG_STATE. We can thus eventually
4666 * take the STREAM_LOCK. */
4667 GST_DEBUG_OBJECT (avi, "sending flush start");
4668 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4669 gst_pad_push_event (avi->sinkpad, fevent);
4671 /* a non-flushing seek, we PAUSE the task so that we can take the
4673 GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
4674 gst_pad_pause_task (avi->sinkpad);
4677 /* wait for streaming to stop */
4678 GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
4679 GST_PAD_STREAM_LOCK (avi->sinkpad);
4681 /* copy segment, we need this because we still need the old
4682 * segment when we close the current segment. */
4683 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4686 GST_DEBUG_OBJECT (avi, "configuring seek");
4687 gst_segment_set_seek (&seeksegment, rate, format, flags,
4688 cur_type, cur, stop_type, stop, &update);
4690 /* do the seek, seeksegment.last_stop contains the new position, this
4691 * actually never fails. */
4692 gst_avi_demux_do_seek (avi, &seeksegment);
4694 gst_event_replace (&avi->close_seg_event, NULL);
4696 GstEvent *fevent = gst_event_new_flush_stop ();
4698 GST_DEBUG_OBJECT (avi, "sending flush stop");
4699 gst_avi_demux_push_event (avi, gst_event_ref (fevent));
4700 gst_pad_push_event (avi->sinkpad, fevent);
4701 } else if (avi->segment_running) {
4702 /* we are running the current segment and doing a non-flushing seek,
4703 * close the segment first based on the last_stop. */
4704 GST_DEBUG_OBJECT (avi, "closing running segment %" G_GINT64_FORMAT
4705 " to %" G_GINT64_FORMAT, avi->segment.start, avi->segment.last_stop);
4706 avi->close_seg_event = gst_event_new_new_segment_full (TRUE,
4707 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
4708 avi->segment.start, avi->segment.last_stop, avi->segment.time);
4711 /* now update the real segment info */
4712 memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
4714 /* post the SEGMENT_START message when we do segmented playback */
4715 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4716 gst_element_post_message (GST_ELEMENT_CAST (avi),
4717 gst_message_new_segment_start (GST_OBJECT_CAST (avi),
4718 avi->segment.format, avi->segment.last_stop));
4721 /* prepare for streaming again */
4722 if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
4723 stop = avi->segment.duration;
4725 /* queue the segment event for the streaming thread. */
4727 gst_event_unref (avi->seg_event);
4728 if (avi->segment.rate > 0.0) {
4729 /* forwards goes from last_stop to stop */
4730 avi->seg_event = gst_event_new_new_segment_full (FALSE,
4731 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
4732 avi->segment.last_stop, stop, avi->segment.time);
4734 #ifdef AVIDEMUX_MODIFICATION
4736 avi->segment.start = 0;
4737 avi->segment.time = 0;
4739 /* reverse goes from start to last_stop */
4740 avi->seg_event = gst_event_new_new_segment_full (FALSE,
4741 avi->segment.rate, avi->segment.applied_rate, avi->segment.format,
4742 avi->segment.start, avi->segment.last_stop, avi->segment.time);
4745 if (!avi->streaming) {
4746 avi->segment_running = TRUE;
4747 gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4750 /* reset the last flow and mark discont, seek is always DISCONT */
4751 for (i = 0; i < avi->num_streams; i++) {
4752 GST_DEBUG_OBJECT (avi, "marking DISCONT");
4753 avi->stream[i].last_flow = GST_FLOW_OK;
4754 avi->stream[i].discont = TRUE;
4756 GST_PAD_STREAM_UNLOCK (avi->sinkpad);
4763 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4769 * Handle seek event in push mode.
4772 avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4777 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4780 GstAviStream *stream;
4784 GstSegment seeksegment;
4787 /* check we have the index */
4788 if (!avi->have_index) {
4789 GST_DEBUG_OBJECT (avi, "no seek index built, seek aborted.");
4792 GST_DEBUG_OBJECT (avi, "doing push-based seek with event");
4795 gst_event_parse_seek (event, &rate, &format, &flags,
4796 &cur_type, &cur, &stop_type, &stop);
4798 /* some slack aiming for a keyframe */
4799 if (cur < GST_AVI_SEEK_PUSH_DISPLACE)
4802 cur -= GST_AVI_SEEK_PUSH_DISPLACE;
4804 if (format != GST_FORMAT_TIME) {
4805 GstFormat fmt = GST_FORMAT_TIME;
4806 gboolean res = TRUE;
4808 if (cur_type != GST_SEEK_TYPE_NONE)
4809 res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
4810 if (res && stop_type != GST_SEEK_TYPE_NONE)
4811 res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
4813 GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4820 /* let gst_segment handle any tricky stuff */
4821 GST_DEBUG_OBJECT (avi, "configuring seek");
4822 memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
4823 gst_segment_set_seek (&seeksegment, rate, format, flags,
4824 cur_type, cur, stop_type, stop, &update);
4826 keyframe = ! !(flags & GST_SEEK_FLAG_KEY_UNIT);
4827 cur = seeksegment.last_stop;
4829 GST_DEBUG_OBJECT (avi,
4830 "Seek requested: ts %" GST_TIME_FORMAT " stop %" GST_TIME_FORMAT
4831 ", kf %u, rate %lf", GST_TIME_ARGS (cur), GST_TIME_ARGS (stop), keyframe,
4835 GST_DEBUG_OBJECT (avi, "negative rate seek not supported in push mode");
4839 /* FIXME, this code assumes the main stream with keyframes is stream 0,
4840 * which is mostly correct... */
4841 str_num = avi->main_stream;
4842 stream = &avi->stream[str_num];
4844 /* get the entry index for the requested position */
4845 index = gst_avi_demux_index_for_time (avi, stream, cur);
4846 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT,
4847 str_num, index, GST_TIME_ARGS (cur));
4849 /* check if we are already on a keyframe */
4850 if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
4851 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4852 /* now go to the previous keyframe, this is where we should start
4854 index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4855 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", index);
4858 gst_avi_demux_get_buffer_info (avi, stream, index,
4859 &stream->current_timestamp, &stream->current_ts_end,
4860 &stream->current_offset, &stream->current_offset_end);
4862 /* re-use cur to be the timestamp of the seek as it _will_ be */
4863 cur = stream->current_timestamp;
4865 min_offset = stream->index[index].offset;
4866 avi->seek_kf_offset = min_offset - 8;
4868 GST_DEBUG_OBJECT (avi,
4869 "Seek to: ts %" GST_TIME_FORMAT " (on str %u, idx %u, offset %"
4870 G_GUINT64_FORMAT ")", GST_TIME_ARGS (stream->current_timestamp), str_num,
4873 for (n = 0; n < avi->num_streams; n++) {
4874 GstAviStream *str = &avi->stream[n];
4877 if (n == avi->main_stream)
4880 /* get the entry index for the requested position */
4881 idx = gst_avi_demux_index_for_time (avi, str, cur);
4882 GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT, n,
4883 idx, GST_TIME_ARGS (cur));
4885 /* check if we are already on a keyframe */
4886 if (!ENTRY_IS_KEYFRAME (&str->index[idx])) {
4887 GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
4888 /* now go to the previous keyframe, this is where we should start
4890 idx = gst_avi_demux_index_prev (avi, str, idx, TRUE);
4891 GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", idx);
4894 gst_avi_demux_get_buffer_info (avi, str, idx,
4895 &str->current_timestamp, &str->current_ts_end,
4896 &str->current_offset, &str->current_offset_end);
4898 if (str->index[idx].offset < min_offset) {
4899 min_offset = str->index[idx].offset;
4900 GST_DEBUG_OBJECT (avi,
4901 "Found an earlier offset at %" G_GUINT64_FORMAT ", str %u",
4909 GST_DEBUG_OBJECT (avi,
4910 "Seek performed: str %u, offset %" G_GUINT64_FORMAT ", idx %u, ts %"
4911 GST_TIME_FORMAT ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
4912 ", off_end %" G_GUINT64_FORMAT, str_num, min_offset, index,
4913 GST_TIME_ARGS (stream->current_timestamp),
4914 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
4915 stream->current_offset_end);
4917 /* index data refers to data, not chunk header (for pull mode convenience) */
4919 GST_DEBUG_OBJECT (avi, "seeking to chunk at offset %" G_GUINT64_FORMAT,
4922 if (!perform_seek_to_offset (avi, min_offset)) {
4923 GST_DEBUG_OBJECT (avi, "seek event failed!");
4931 * Handle whether we can perform the seek event or if we have to let the chain
4932 * function handle seeks to build the seek indexes first.
4935 gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
4938 /* check for having parsed index already */
4939 if (!avi->have_index) {
4941 gboolean building_index;
4943 GST_OBJECT_LOCK (avi);
4944 /* handle the seek event in the chain function */
4945 avi->state = GST_AVI_DEMUX_SEEK;
4947 /* copy the event */
4948 if (avi->seek_event)
4949 gst_event_unref (avi->seek_event);
4950 avi->seek_event = gst_event_ref (event);
4952 /* set the building_index flag so that only one thread can setup the
4953 * structures for index seeking. */
4954 building_index = avi->building_index;
4955 if (!building_index) {
4956 avi->building_index = TRUE;
4957 if (avi->stream[0].indexes) {
4958 avi->odml_stream = 0;
4959 avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
4960 offset = avi->odml_subidxs[0];
4962 offset = avi->idx1_offset;
4965 GST_OBJECT_UNLOCK (avi);
4967 if (!building_index) {
4968 /* seek to the first subindex or legacy index */
4969 GST_INFO_OBJECT (avi,
4970 "Seeking to legacy index/first subindex at %" G_GUINT64_FORMAT,
4972 return perform_seek_to_offset (avi, offset);
4975 /* FIXME: we have to always return true so that we don't block the seek
4977 * Note: maybe it is OK to return true if we're still building the index */
4981 return avi_demux_handle_seek_push (avi, pad, event);
4985 * Helper for gst_avi_demux_invert()
4988 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
4990 memcpy (tmp, d1, bytes);
4991 memcpy (d1, d2, bytes);
4992 memcpy (d2, tmp, bytes);
4996 #define gst_avi_demux_is_uncompressed(fourcc) \
4997 (fourcc == GST_RIFF_DIB || \
4998 fourcc == GST_RIFF_rgb || \
4999 fourcc == GST_RIFF_RGB || fourcc == GST_RIFF_RAW)
5002 * Invert DIB buffers... Takes existing buffer and
5003 * returns either the buffer or a new one (with old
5004 * one dereferenced).
5005 * FIXME: can't we preallocate tmp? and remember stride, bpp?
5008 gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
5015 if (stream->strh->type != GST_RIFF_FCC_vids)
5018 if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
5019 return buf; /* Ignore non DIB buffers */
5022 s = gst_caps_get_structure (GST_PAD_CAPS (stream->pad), 0);
5023 if (!gst_structure_get_int (s, "bpp", &bpp)) {
5024 GST_WARNING ("Failed to retrieve depth from caps");
5028 if (stream->strf.vids == NULL) {
5029 GST_WARNING ("Failed to retrieve vids for stream");
5033 h = stream->strf.vids->height;
5034 w = stream->strf.vids->width;
5035 stride = w * (bpp / 8);
5037 buf = gst_buffer_make_writable (buf);
5038 if (GST_BUFFER_SIZE (buf) < (stride * h)) {
5039 GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
5043 tmp = g_malloc (stride);
5045 for (y = 0; y < h / 2; y++) {
5046 swap_line (GST_BUFFER_DATA (buf) + stride * y,
5047 GST_BUFFER_DATA (buf) + stride * (h - 1 - y), tmp, stride);
5056 gst_avi_demux_add_assoc (GstAviDemux * avi, GstAviStream * stream,
5057 GstClockTime timestamp, guint64 offset, gboolean keyframe)
5059 /* do not add indefinitely for open-ended streaming */
5060 if (G_UNLIKELY (avi->element_index && avi->seekable)) {
5061 GST_LOG_OBJECT (avi, "adding association %" GST_TIME_FORMAT "-> %"
5062 G_GUINT64_FORMAT, GST_TIME_ARGS (timestamp), offset);
5063 gst_index_add_association (avi->element_index, avi->index_id,
5064 keyframe ? GST_ASSOCIATION_FLAG_KEY_UNIT : GST_ASSOCIATION_FLAG_NONE,
5065 GST_FORMAT_TIME, timestamp, GST_FORMAT_BYTES, offset, NULL);
5066 /* well, current_total determines TIME and entry DEFAULT (frame #) ... */
5067 gst_index_add_association (avi->element_index, stream->index_id,
5068 GST_ASSOCIATION_FLAG_NONE,
5069 GST_FORMAT_TIME, stream->current_total, GST_FORMAT_BYTES, offset,
5070 GST_FORMAT_DEFAULT, stream->current_entry, NULL);
5075 * Returns the aggregated GstFlowReturn.
5077 static GstFlowReturn
5078 gst_avi_demux_combine_flows (GstAviDemux * avi, GstAviStream * stream,
5082 gboolean unexpected = FALSE, not_linked = TRUE;
5084 /* store the value */
5085 stream->last_flow = ret;
5087 /* any other error that is not-linked or eos can be returned right away */
5088 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
5091 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
5092 for (i = 0; i < avi->num_streams; i++) {
5093 GstAviStream *ostream = &avi->stream[i];
5095 ret = ostream->last_flow;
5096 /* no unexpected or unlinked, return */
5097 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
5100 /* we check to see if we have at least 1 unexpected or all unlinked */
5101 unexpected |= (ret == GST_FLOW_UNEXPECTED);
5102 not_linked &= (ret == GST_FLOW_NOT_LINKED);
5104 /* when we get here, we all have unlinked or unexpected */
5106 ret = GST_FLOW_NOT_LINKED;
5107 else if (unexpected)
5108 ret = GST_FLOW_UNEXPECTED;
5110 GST_LOG_OBJECT (avi, "combined %s to return %s",
5111 gst_flow_get_name (stream->last_flow), gst_flow_get_name (ret));
5115 #ifdef AVIDEMUX_MODIFICATION
5116 /*Modification: Added functions to update the stream while doing the trickplay*/
5118 /* move @stream to the next position in its index */
5119 static GstFlowReturn
5120 gst_avi_demux_update_backward (GstAviDemux * avi, GstAviStream * stream,
5123 int old_entry, new_entry;
5125 old_entry = stream->current_entry;
5127 new_entry = stream->next_kindex;
5129 /* see if we reached the end */
5130 if (new_entry < (int)stream->start_entry) {
5131 /* we stepped all the way to the start, eos */
5132 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
5136 if (new_entry != old_entry) {
5137 stream->current_entry = new_entry;
5138 stream->current_total = stream->index[new_entry].total;
5140 /* we moved DISCONT, full update */
5141 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
5142 &stream->current_timestamp, &stream->current_ts_end,
5143 &stream->current_offset, &stream->current_offset_end);
5144 /* and MARK discont for this stream */
5145 stream->last_flow = GST_FLOW_OK;
5146 stream->discont = TRUE;
5147 GST_DEBUG_OBJECT (avi, "Moved from %u to %u, ts %" GST_TIME_FORMAT
5148 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
5149 ", off_end %" G_GUINT64_FORMAT, old_entry, new_entry,
5150 GST_TIME_ARGS (stream->current_timestamp),
5151 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
5152 stream->current_offset_end);
5159 GST_DEBUG_OBJECT (avi, "we are EOS");
5160 /* setting current_timestamp to -1 marks EOS */
5161 stream->current_timestamp = -1;
5162 return GST_FLOW_UNEXPECTED;
5166 /* move @stream to the next position in its index */
5167 static GstFlowReturn
5168 gst_avi_demux_update (GstAviDemux * avi, GstAviStream * stream,
5171 guint old_entry, new_entry;
5173 old_entry = stream->current_entry;
5175 new_entry = stream->next_kindex;
5177 /* see if we reached the end */
5178 if (new_entry > stream->stop_entry) {
5179 if (avi->segment.rate < 0.0) {
5180 if (stream->step_entry == stream->start_entry) {
5181 /* we stepped all the way to the start, eos */
5182 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
5185 /* backwards, stop becomes step, find a new step */
5186 stream->stop_entry = stream->step_entry;
5187 stream->step_entry = gst_avi_demux_index_prev (avi, stream,
5188 stream->stop_entry, TRUE);
5190 GST_DEBUG_OBJECT (avi,
5191 "reverse playback jump: start %u, step %u, stop %u",
5192 stream->start_entry, stream->step_entry, stream->stop_entry);
5194 /* and start from the previous keyframe now */
5195 new_entry = stream->step_entry;
5198 GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
5203 if (new_entry != old_entry) {
5204 stream->current_entry = new_entry;
5205 stream->current_total = stream->index[new_entry].total;
5207 if (new_entry == old_entry + 1) {
5208 GST_DEBUG_OBJECT (avi, "moved forwards from %u to %u",
5209 old_entry, new_entry);
5210 /* we simply moved one step forwards, reuse current info */
5211 stream->current_timestamp = stream->current_ts_end;
5212 stream->current_offset = stream->current_offset_end;
5213 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
5214 NULL, &stream->current_ts_end, NULL, &stream->current_offset_end);
5216 /* we moved DISCONT, full update */
5217 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
5218 &stream->current_timestamp, &stream->current_ts_end,
5219 &stream->current_offset, &stream->current_offset_end);
5220 /* and MARK discont for this stream */
5221 stream->last_flow = GST_FLOW_OK;
5222 stream->discont = TRUE;
5223 GST_DEBUG_OBJECT (avi, "Moved from %u to %u, ts %" GST_TIME_FORMAT
5224 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
5225 ", off_end %" G_GUINT64_FORMAT, old_entry, new_entry,
5226 GST_TIME_ARGS (stream->current_timestamp),
5227 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
5228 stream->current_offset_end);
5236 GST_DEBUG_OBJECT (avi, "we are EOS");
5237 /* setting current_timestamp to -1 marks EOS */
5238 stream->current_timestamp = -1;
5239 return GST_FLOW_UNEXPECTED;
5243 /* move @stream to the next position in its index */
5244 static GstFlowReturn
5245 gst_avi_demux_previous (GstAviDemux * avi, GstAviStream * stream,
5248 int old_entry, new_entry;
5250 old_entry = stream->current_entry;
5252 new_entry = old_entry - 1;
5253 GST_INFO("now at:%d, start:%d", new_entry, stream->start_entry);
5254 /* see if we reached the end */
5255 if (new_entry < (int)stream->start_entry) {
5256 /* we stepped all the way to the start, eos */
5257 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
5260 GST_INFO("%d", new_entry);
5261 if (new_entry != old_entry) {
5262 stream->current_entry = new_entry;
5263 stream->current_total = stream->index[new_entry].total;
5265 /* we moved DISCONT, full update */
5266 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
5267 &stream->current_timestamp, &stream->current_ts_end,
5268 &stream->current_offset, &stream->current_offset_end);
5269 /* and MARK discont for this stream */
5270 stream->last_flow = GST_FLOW_OK;
5271 stream->discont = TRUE;
5272 GST_DEBUG_OBJECT (avi, "Moved from %u to %u, ts %" GST_TIME_FORMAT
5273 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
5274 ", off_end %" G_GUINT64_FORMAT, old_entry, new_entry,
5275 GST_TIME_ARGS (stream->current_timestamp),
5276 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
5277 stream->current_offset_end);
5284 GST_DEBUG_OBJECT (avi, "we are EOS");
5285 /* setting current_timestamp to -1 marks EOS */
5286 stream->current_timestamp = -1;
5287 return GST_FLOW_UNEXPECTED;
5292 /* move @stream to the next position in its index */
5293 static GstFlowReturn
5294 gst_avi_demux_advance (GstAviDemux * avi, GstAviStream * stream,
5297 guint old_entry, new_entry;
5299 old_entry = stream->current_entry;
5301 new_entry = old_entry + 1;
5303 /* see if we reached the end */
5304 if (new_entry >= stream->stop_entry) {
5305 if (avi->segment.rate < 0.0) {
5306 if (stream->step_entry == stream->start_entry) {
5307 /* we stepped all the way to the start, eos */
5308 GST_DEBUG_OBJECT (avi, "reverse reached start %u", stream->start_entry);
5311 /* backwards, stop becomes step, find a new step */
5312 stream->stop_entry = stream->step_entry;
5313 stream->step_entry = gst_avi_demux_index_prev (avi, stream,
5314 stream->stop_entry, TRUE);
5316 GST_DEBUG_OBJECT (avi,
5317 "reverse playback jump: start %u, step %u, stop %u",
5318 stream->start_entry, stream->step_entry, stream->stop_entry);
5320 /* and start from the previous keyframe now */
5321 new_entry = stream->step_entry;
5324 GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
5329 if (new_entry != old_entry) {
5330 stream->current_entry = new_entry;
5331 stream->current_total = stream->index[new_entry].total;
5333 if (new_entry == old_entry + 1) {
5334 GST_DEBUG_OBJECT (avi, "moved forwards from %u to %u",
5335 old_entry, new_entry);
5336 /* we simply moved one step forwards, reuse current info */
5337 stream->current_timestamp = stream->current_ts_end;
5338 stream->current_offset = stream->current_offset_end;
5339 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
5340 NULL, &stream->current_ts_end, NULL, &stream->current_offset_end);
5342 /* we moved DISCONT, full update */
5343 gst_avi_demux_get_buffer_info (avi, stream, new_entry,
5344 &stream->current_timestamp, &stream->current_ts_end,
5345 &stream->current_offset, &stream->current_offset_end);
5346 /* and MARK discont for this stream */
5347 stream->last_flow = GST_FLOW_OK;
5348 stream->discont = TRUE;
5349 GST_DEBUG_OBJECT (avi, "Moved from %u to %u, ts %" GST_TIME_FORMAT
5350 ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
5351 ", off_end %" G_GUINT64_FORMAT, old_entry, new_entry,
5352 GST_TIME_ARGS (stream->current_timestamp),
5353 GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
5354 stream->current_offset_end);
5362 GST_DEBUG_OBJECT (avi, "we are EOS");
5363 /* setting current_timestamp to -1 marks EOS */
5364 stream->current_timestamp = -1;
5365 return GST_FLOW_UNEXPECTED;
5369 /* find the stream with the lowest current position when going forwards or with
5370 * the highest position when going backwards, this is the stream
5371 * we should push from next */
5373 gst_avi_demux_find_next (GstAviDemux * avi, gfloat rate)
5375 guint64 min_time, max_time;
5376 guint stream_num, i;
5379 min_time = G_MAXUINT64;
5382 for (i = 0; i < avi->num_streams; i++) {
5384 GstAviStream *stream;
5386 stream = &avi->stream[i];
5388 /* ignore streams that finished */
5389 if (stream->last_flow == GST_FLOW_UNEXPECTED)
5392 position = stream->current_timestamp;
5394 /* position of -1 is EOS */
5395 if (position != -1) {
5396 if (rate > 0.0 && position < min_time) {
5397 min_time = position;
5399 } else if (rate < 0.0 && position >= max_time) {
5400 max_time = position;
5408 static GstFlowReturn
5409 gst_avi_demux_loop_data (GstAviDemux * avi)
5411 GstFlowReturn ret = GST_FLOW_OK;
5413 GstAviStream *stream;
5414 gboolean processed = FALSE;
5416 guint64 offset, size;
5417 GstClockTime timestamp, duration;
5418 guint64 out_offset, out_offset_end;
5420 GstAviIndexEntry *entry;
5421 #ifdef AVIDEMUX_MODIFICATION
5422 gdouble minusone = -1;
5425 #ifdef AVIDEMUX_MODIFICATION
5428 stream_num = gst_avi_demux_find_next (avi, avi->segment.rate);
5431 if (G_UNLIKELY (stream_num == -1)) {
5432 GST_DEBUG_OBJECT (avi, "all streams are EOS");
5436 /* we have the stream now */
5437 stream = &avi->stream[stream_num];
5439 /* skip streams without pads */
5441 GST_DEBUG_OBJECT (avi, "skipping entry from stream %d without pad",
5446 #ifdef AVIDEMUX_MODIFICATION
5447 /*Modification: Added trickplay functionality to achieve upto 64x speed play*/
5449 if(avi->segment.rate>1.0)
5451 guint64 next_kindex_timestamp;
5453 GST_DEBUG_OBJECT (avi, "current index:%d, next key index:%d, total keyframes:%d", stream->current_entry, stream->next_kindex, stream->n_keyframes);
5454 if((stream->n_keyframes == stream->idx_n && stream->strh->type == GST_RIFF_FCC_vids) ||
5455 (stream->strh->type == GST_RIFF_FCC_auds && avi->segment.rate>4.0))
5458 GST_DEBUG_OBJECT (avi, "All keyframes case video forward bfore :%d", stream->current_entry);
5460 stream->next_kindex= stream->current_entry+avi->segment.rate;
5462 if(stream->next_kindex>stream->idx_n)
5463 stream->next_kindex=stream->idx_n;
5465 gst_avi_demux_update (avi, stream, ret);
5467 /* Checking the EOS condition in Trickplay*/
5468 if(stream->current_timestamp == -1)
5470 GST_WARNING_OBJECT(avi, "eos in Trickplay");
5475 GST_DEBUG_OBJECT (avi, "All key frames after :%d", stream->next_kindex);
5478 else if(stream->strh->type == GST_RIFF_FCC_vids)
5480 if(!stream->next_kindex)
5482 stream->next_kindex = stream->prev_kindex = stream->current_entry;
5486 GST_DEBUG_OBJECT (avi, "finding next key index:current index:%d, next key index:%d, total:%d", stream->current_entry, stream->next_kindex, stream->idx_n);
5488 if((stream->next_kindex+1)>=stream->idx_n)
5490 GST_DEBUG_OBJECT(avi,"eos");
5493 stream->next_kindex = gst_avi_demux_index_next(avi, stream, stream->next_kindex+1, TRUE);
5494 if(stream->next_kindex<0)
5496 GST_DEBUG_OBJECT (avi, "finding next key index:current index:%d, next key index:%d, total:%d", stream->current_entry, stream->next_kindex, stream->idx_n);
5498 stream->prev_kindex=stream->next_kindex=0;
5502 stream->total_samples_bet_2_keyframes = stream->next_kindex-stream->prev_kindex;
5503 next_kindex_timestamp = avi_stream_convert_frames_to_time_unchecked (stream, stream->next_kindex);
5504 stream->start_timestamp = avi_stream_convert_frames_to_time_unchecked (stream, stream->prev_kindex);
5505 stream->avg_duration_bet_2_keyframes=(next_kindex_timestamp-stream->start_timestamp)/stream->total_samples_bet_2_keyframes;
5507 stream->samples_to_show_bet_kframes = stream->total_samples_bet_2_keyframes/avi->segment.rate;
5508 GST_DEBUG_OBJECT (avi,"avg duration:%"GST_TIME_FORMAT"next:%"GST_TIME_FORMAT"prev:%"GST_TIME_FORMAT", %d, %d",
5509 GST_TIME_ARGS(stream->avg_duration_bet_2_keyframes),
5510 GST_TIME_ARGS(next_kindex_timestamp),
5511 GST_TIME_ARGS(stream->start_timestamp),
5512 stream->total_samples_bet_2_keyframes, stream->samples_to_show_bet_kframes);
5513 if(stream->samples_to_show_bet_kframes!=0)
5517 stream->discont = TRUE;
5522 stream->samples_to_show_bet_kframes--;
5524 if(!stream->samples_to_show_bet_kframes)
5526 stream->next_kindex--;
5527 gst_avi_demux_update (avi, stream, ret);
5528 stream->next_kindex=0;
5529 stream->samples_to_show_bet_kframes=0;
5530 stream->discont = TRUE;
5536 timestamp = stream->start_timestamp + (stream->current_entry-stream->prev_kindex)*avi->segment.rate*stream->avg_duration_bet_2_keyframes;
5537 duration = stream->avg_duration_bet_2_keyframes*avi->segment.rate;
5538 GST_DEBUG_OBJECT (avi,"current:%"GST_TIME_FORMAT"prev:%"GST_TIME_FORMAT"cal:%"GST_TIME_FORMAT", %d",
5539 GST_TIME_ARGS((stream->current_timestamp + stream->current_offset)),
5540 GST_TIME_ARGS(stream->start_timestamp),
5541 GST_TIME_ARGS(timestamp),
5542 stream->samples_to_show_bet_kframes);
5547 else if(avi->segment.rate<0)
5549 /* ***********Kishore***********
5550 only I frames Displayed
5552 if(stream->strh->type == GST_RIFF_FCC_vids)
5554 guint64 time_position;
5557 //sample = &stream->samples[stream->current_entry];
5558 time_position=stream->current_timestamp;//avi_stream_convert_frames_to_time_unchecked (stream, stream->current_entry);
5559 duration= stream->current_ts_end - time_position;
5560 GST_DEBUG_OBJECT (avi, "video time backward:%"GST_TIME_FORMAT"%d",
5561 GST_TIME_ARGS(time_position), stream->current_entry);
5562 if((time_position - (minusone *avi->segment.rate)*duration)>0)
5563 time_position -= (minusone *avi->segment.rate)*duration;
5567 GST_DEBUG_OBJECT (avi, "video time backward after:%"GST_TIME_FORMAT,
5568 GST_TIME_ARGS(time_position));
5569 index = gst_avi_demux_index_for_time(avi,stream,time_position);
5571 stream->next_kindex = gst_avi_demux_index_prev(avi, stream, index, TRUE);
5576 GST_DEBUG_OBJECT (avi, "audio time backward bfore :%d", stream->current_entry);
5578 stream->next_kindex= stream->current_entry-(minusone *avi->segment.rate);
5580 if(stream->next_kindex<0)
5581 stream->next_kindex=0;
5583 GST_DEBUG_OBJECT (avi, "audio time backward after :%d", stream->next_kindex);
5586 gst_avi_demux_update_backward(avi, stream, ret);
5588 stream->discont = TRUE;
5591 /* get the timing info for the entry */
5592 #ifdef AVIDEMUX_MODIFICATION
5596 timestamp = stream->current_timestamp;
5597 duration = stream->current_ts_end - timestamp;
5599 out_offset = stream->current_offset;
5600 out_offset_end = stream->current_offset_end;
5602 /* get the entry data info */
5603 entry = &stream->index[stream->current_entry];
5604 offset = entry->offset;
5606 keyframe = ENTRY_IS_KEYFRAME (entry);
5608 /* skip empty entries */
5610 GST_DEBUG_OBJECT (avi, "Skipping entry %u (%" G_GUINT64_FORMAT ", %p)",
5611 stream->current_entry, size, stream->pad);
5615 if (avi->segment.rate > 0.0) {
5616 /* only check this for fowards playback for now */
5617 if (keyframe && GST_CLOCK_TIME_IS_VALID (avi->segment.stop)
5618 && (timestamp > avi->segment.stop)) {
5623 GST_LOG ("reading buffer (size=%" G_GUINT64_FORMAT "), stream %d, pos %"
5624 G_GUINT64_FORMAT " (0x%" G_GINT64_MODIFIER "x), kf %d", size,
5625 stream_num, offset, offset, keyframe);
5627 /* FIXME, check large chunks and cut them up */
5629 /* pull in the data */
5630 ret = gst_pad_pull_range (avi->sinkpad, offset, size, &buf);
5631 if (ret != GST_FLOW_OK)
5634 /* check for short buffers, this is EOS as well */
5635 if (GST_BUFFER_SIZE (buf) < size)
5638 /* invert the picture if needed */
5639 buf = gst_avi_demux_invert (stream, buf);
5641 /* mark non-keyframes */
5643 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
5645 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
5647 GST_BUFFER_TIMESTAMP (buf) = timestamp;
5648 GST_BUFFER_DURATION (buf) = duration;
5649 GST_BUFFER_OFFSET (buf) = out_offset;
5650 GST_BUFFER_OFFSET_END (buf) = out_offset_end;
5652 /* mark discont when pending */
5653 if (stream->discont) {
5654 GST_DEBUG_OBJECT (avi, "setting DISCONT flag");
5655 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
5656 stream->discont = FALSE;
5659 gst_avi_demux_add_assoc (avi, stream, timestamp, offset, keyframe);
5661 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
5663 /* update current position in the segment */
5664 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, timestamp);
5666 GST_DEBUG_OBJECT (avi, "Pushing buffer of size %u, ts %"
5667 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
5668 ", off_end %" G_GUINT64_FORMAT,
5669 GST_BUFFER_SIZE (buf), GST_TIME_ARGS (timestamp),
5670 GST_TIME_ARGS (duration), out_offset, out_offset_end);
5674 #define CHUNK_ID_LEN 4
5675 #define CHUNK_SIZE_LEN 4
5676 #define DD_CHUNK_DATA_LEN 10
5677 #define DD_CHUNK_TOTAL_LEN CHUNK_ID_LEN+CHUNK_SIZE_LEN+DD_CHUNK_DATA_LEN
5679 if (avi->drmContext)// this is drm
5681 GstBuffer* encrypted_buf = NULL;
5683 if (stream->strh->type == GST_RIFF_FCC_auds) { /* Audio Stream */
5684 if (DRM_SUCCESS == avi->divx_decrypt_audio (avi->drmContext, GST_BUFFER_DATA(buf), GST_BUFFER_SIZE(buf))) {
5685 GST_DEBUG_OBJECT (avi, "drmDecryptAudio() Success : buffer = %d", GST_BUFFER_SIZE(buf));
5687 GST_ERROR_OBJECT (avi, "drmDecryptAudio () Failed : buffer = %d", GST_BUFFER_SIZE(buf));
5689 ret = gst_pad_push (stream->pad, buf);
5691 } else if (stream->strh->type == GST_RIFF_FCC_vids) { /* Video Stream */
5693 /* Read previous dd chunk */
5694 GstBuffer* dd_chunk_buf = NULL;
5695 if (GST_FLOW_OK != gst_pad_pull_range (avi->sinkpad,
5696 offset-(CHUNK_ID_LEN+CHUNK_SIZE_LEN+DD_CHUNK_TOTAL_LEN),
5697 DD_CHUNK_TOTAL_LEN, &dd_chunk_buf)) {
5698 GST_ERROR_OBJECT (avi, "pull range failed");
5700 guint8 tempBuffer[256] = { 0, };
5701 guint32 tempBufferLength = 0;
5704 ret = avi->divx_prepare_video_bitstream (avi->drmContext,
5705 GST_BUFFER_DATA(dd_chunk_buf)+(CHUNK_ID_LEN+CHUNK_SIZE_LEN),
5708 &tempBufferLength );
5710 if (ret == DRM_SUCCESS) {
5711 /* Create new buffer and copy retrieved tempBuffer and original buffer to created buffer */
5712 encrypted_buf = gst_buffer_new_and_alloc (GST_BUFFER_SIZE(buf)+tempBufferLength);
5713 if (encrypted_buf) {
5714 /* FIXME: Can be enhance merge buffer code */
5715 memcpy (GST_BUFFER_DATA(encrypted_buf), tempBuffer, tempBufferLength);
5716 memcpy (GST_BUFFER_DATA(encrypted_buf)+tempBufferLength, GST_BUFFER_DATA(buf), GST_BUFFER_SIZE(buf));
5717 gst_buffer_copy_metadata (encrypted_buf, buf, GST_BUFFER_COPY_ALL);
5719 /* relase buf because we will push encrypted_buf instead of buf */
5720 gst_buffer_unref (buf);
5722 GST_ERROR_OBJECT (avi, "gst_buffer_new_and_alloc() failed!!!!");
5725 GST_ERROR_OBJECT (avi, "divx_prepare_video_bitstream failed!!!! ret = [%d]", ret);
5729 /* Release DD-chunk Buffer */
5731 gst_buffer_unref (dd_chunk_buf);
5733 /* Push encrypted_buf if is valid, otherwise push original buffer */
5735 ret = gst_pad_push (stream->pad, encrypted_buf);
5737 ret = gst_pad_push (stream->pad, buf);
5740 /* This is normal file */
5741 ret = gst_pad_push (stream->pad, buf);
5744 ret = gst_pad_push (stream->pad, buf);
5747 /* mark as processed, we increment the frame and byte counters then
5748 * leave the while loop and return the GstFlowReturn */
5751 if (avi->segment.rate < 0) {
5752 if (timestamp > avi->segment.stop && ret == GST_FLOW_UNEXPECTED) {
5753 /* In reverse playback we can get a GST_FLOW_UNEXPECTED when
5754 * we are at the end of the segment, so we just need to jump
5755 * back to the previous section. */
5756 GST_DEBUG_OBJECT (avi, "downstream has reached end of segment");
5761 /* move to next item */
5762 #ifdef AVIDEMUX_MODIFICATION
5763 if (avi->segment.rate > 0) {
5764 ret = gst_avi_demux_advance (avi, stream, ret);
5767 ret = gst_avi_demux_previous (avi, stream, ret);
5770 /* Checking the EOS condition in Trickplay : naveen*/
5771 if((stream->current_timestamp == -1) && (avi->segment.rate != 1.0))
5773 GST_WARNING_OBJECT(avi, "eos in Trickplay");
5777 ret = gst_avi_demux_advance (avi, stream, ret);
5780 ret = gst_avi_demux_combine_flows (avi, stream, ret);
5781 } while (!processed);
5789 GST_DEBUG_OBJECT (avi, "No samples left for any streams - EOS");
5790 ret = GST_FLOW_UNEXPECTED;
5795 GST_LOG_OBJECT (avi, "Found keyframe after segment,"
5796 " setting EOS (%" GST_TIME_FORMAT " > %" GST_TIME_FORMAT ")",
5797 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (avi->segment.stop));
5798 ret = GST_FLOW_UNEXPECTED;
5799 /* move to next stream */
5804 GST_DEBUG_OBJECT (avi, "pull range failed: pos=%" G_GUINT64_FORMAT
5805 " size=%" G_GUINT64_FORMAT, offset, size);
5810 GST_WARNING_OBJECT (avi, "Short read at offset %" G_GUINT64_FORMAT
5811 ", only got %d/%" G_GUINT64_FORMAT " bytes (truncated file?)", offset,
5812 GST_BUFFER_SIZE (buf), size);
5813 gst_buffer_unref (buf);
5814 ret = GST_FLOW_UNEXPECTED;
5820 * Read data. If we have an index it delegates to
5821 * gst_avi_demux_process_next_entry().
5823 static GstFlowReturn
5824 gst_avi_demux_stream_data (GstAviDemux * avi)
5829 GstFlowReturn res = GST_FLOW_OK;
5830 GstFormat format = GST_FORMAT_TIME;
5832 if (G_UNLIKELY (avi->have_eos)) {
5833 /* Clean adapter, we're done */
5834 gst_adapter_clear (avi->adapter);
5835 return GST_FLOW_UNEXPECTED;
5838 if (G_UNLIKELY (avi->todrop)) {
5841 if ((drop = gst_adapter_available (avi->adapter))) {
5842 if (drop > avi->todrop)
5844 GST_DEBUG_OBJECT (avi, "Dropping %d bytes", drop);
5845 gst_adapter_flush (avi->adapter, drop);
5846 avi->todrop -= drop;
5847 avi->offset += drop;
5851 /* Iterate until need more data, so adapter won't grow too much */
5853 if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
5857 GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
5858 GST_FOURCC_ARGS (tag), size);
5860 if (G_LIKELY ((tag & 0xff) >= '0' && (tag & 0xff) <= '9' &&
5861 ((tag >> 8) & 0xff) >= '0' && ((tag >> 8) & 0xff) <= '9')) {
5862 GST_LOG ("Chunk ok");
5863 } else if ((tag & 0xffff) == (('x' << 8) | 'i')) {
5864 GST_DEBUG ("Found sub-index tag");
5865 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
5866 /* accept 0 size buffer here */
5867 avi->abort_buffering = FALSE;
5868 GST_DEBUG (" skipping %d bytes for now", size);
5869 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
5872 } else if (tag == GST_RIFF_TAG_RIFF) {
5873 /* RIFF tags can appear in ODML files, just jump over them */
5874 if (gst_adapter_available (avi->adapter) >= 12) {
5875 GST_DEBUG ("Found RIFF tag, skipping RIFF header");
5876 gst_adapter_flush (avi->adapter, 12);
5880 } else if (tag == GST_RIFF_TAG_idx1) {
5881 GST_DEBUG ("Found index tag");
5882 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
5883 /* accept 0 size buffer here */
5884 avi->abort_buffering = FALSE;
5885 GST_DEBUG (" skipping %d bytes for now", size);
5886 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
5889 } else if (tag == GST_RIFF_TAG_LIST) {
5890 /* movi chunks might be grouped in rec list */
5891 if (gst_adapter_available (avi->adapter) >= 12) {
5892 GST_DEBUG ("Found LIST tag, skipping LIST header");
5893 gst_adapter_flush (avi->adapter, 12);
5897 } else if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ) {
5898 /* rec list might contain JUNK chunks */
5899 GST_DEBUG ("Found JUNK tag");
5900 if (gst_avi_demux_peek_chunk (avi, &tag, &size) || size == 0) {
5901 /* accept 0 size buffer here */
5902 avi->abort_buffering = FALSE;
5903 GST_DEBUG (" skipping %d bytes for now", size);
5904 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
5908 GST_DEBUG ("No more stream chunks, send EOS");
5909 avi->have_eos = TRUE;
5910 return GST_FLOW_UNEXPECTED;
5913 if (G_UNLIKELY (!gst_avi_demux_peek_chunk (avi, &tag, &size))) {
5914 /* supposedly one hopes to catch a nicer chunk later on ... */
5915 /* FIXME ?? give up here rather than possibly ending up going
5916 * through the whole file */
5917 if (avi->abort_buffering) {
5918 avi->abort_buffering = FALSE;
5920 gst_adapter_flush (avi->adapter, 8);
5927 GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
5928 GST_FOURCC_ARGS (tag), size);
5930 stream_nr = CHUNKID_TO_STREAMNR (tag);
5932 if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
5934 GST_WARNING ("Invalid stream ID %d (%" GST_FOURCC_FORMAT ")",
5935 stream_nr, GST_FOURCC_ARGS (tag));
5936 avi->offset += 8 + GST_ROUND_UP_2 (size);
5937 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
5939 GstAviStream *stream;
5940 GstClockTime next_ts = 0;
5941 GstBuffer *buf = NULL;
5943 gboolean saw_desired_kf = stream_nr != avi->main_stream
5944 || avi->offset >= avi->seek_kf_offset;
5946 if (stream_nr == avi->main_stream && avi->offset == avi->seek_kf_offset) {
5947 GST_DEBUG_OBJECT (avi, "Desired keyframe reached");
5948 avi->seek_kf_offset = 0;
5951 if (saw_desired_kf) {
5952 gst_adapter_flush (avi->adapter, 8);
5955 buf = gst_adapter_take_buffer (avi->adapter, GST_ROUND_UP_2 (size));
5956 /* patch the size */
5957 GST_BUFFER_SIZE (buf) = size;
5962 GST_DEBUG_OBJECT (avi,
5963 "Desired keyframe not yet reached, flushing chunk");
5964 gst_adapter_flush (avi->adapter, 8 + GST_ROUND_UP_2 (size));
5967 offset = avi->offset;
5968 avi->offset += 8 + GST_ROUND_UP_2 (size);
5970 stream = &avi->stream[stream_nr];
5972 /* set delay (if any)
5973 if (stream->strh->init_frames == stream->current_frame &&
5975 stream->delay = next_ts;
5978 /* parsing of corresponding header may have failed */
5979 if (G_UNLIKELY (!stream->pad)) {
5980 GST_WARNING_OBJECT (avi, "no pad for stream ID %" GST_FOURCC_FORMAT,
5981 GST_FOURCC_ARGS (tag));
5983 gst_buffer_unref (buf);
5985 /* get time of this buffer */
5986 gst_pad_query_position (stream->pad, &format, (gint64 *) & next_ts);
5987 if (G_UNLIKELY (format != GST_FORMAT_TIME))
5990 gst_avi_demux_add_assoc (avi, stream, next_ts, offset, FALSE);
5992 /* increment our positions */
5993 stream->current_entry++;
5994 stream->current_total += size;
5996 /* update current position in the segment */
5997 gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, next_ts);
5999 if (saw_desired_kf && buf) {
6000 GstClockTime dur_ts = 0;
6002 /* invert the picture if needed */
6003 buf = gst_avi_demux_invert (stream, buf);
6005 gst_pad_query_position (stream->pad, &format, (gint64 *) & dur_ts);
6006 if (G_UNLIKELY (format != GST_FORMAT_TIME))
6009 GST_BUFFER_TIMESTAMP (buf) = next_ts;
6010 GST_BUFFER_DURATION (buf) = dur_ts - next_ts;
6011 if (stream->strh->type == GST_RIFF_FCC_vids) {
6012 GST_BUFFER_OFFSET (buf) = stream->current_entry - 1;
6013 GST_BUFFER_OFFSET_END (buf) = stream->current_entry;
6015 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
6016 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
6019 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
6020 GST_DEBUG_OBJECT (avi,
6021 "Pushing buffer with time=%" GST_TIME_FORMAT ", duration %"
6022 GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
6023 " and size %d over pad %s", GST_TIME_ARGS (next_ts),
6024 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
6025 GST_BUFFER_OFFSET (buf), size, GST_PAD_NAME (stream->pad));
6027 /* mark discont when pending */
6028 if (G_UNLIKELY (stream->discont)) {
6029 GST_DEBUG_OBJECT (avi, "Setting DISCONT");
6030 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
6031 stream->discont = FALSE;
6033 res = gst_pad_push (stream->pad, buf);
6037 res = gst_avi_demux_combine_flows (avi, stream, res);
6038 if (G_UNLIKELY (res != GST_FLOW_OK)) {
6039 GST_DEBUG ("Push failed; %s", gst_flow_get_name (res));
6053 GST_DEBUG_OBJECT (avi, "format %s != GST_FORMAT_TIME",
6054 gst_format_get_name (format));
6055 res = GST_FLOW_ERROR;
6061 * Send pending tags.
6064 push_tag_lists (GstAviDemux * avi)
6072 GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
6074 for (i = 0; i < avi->num_streams; i++) {
6075 GstAviStream *stream = &avi->stream[i];
6076 GstPad *pad = stream->pad;
6078 tags = stream->taglist;
6081 GST_DEBUG_OBJECT (pad, "Tags: %" GST_PTR_FORMAT, tags);
6083 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (avi), pad, tags);
6084 stream->taglist = NULL;
6088 if (!(tags = avi->globaltags))
6089 tags = gst_tag_list_new ();
6091 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
6092 GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
6094 GST_DEBUG_OBJECT (avi, "Global tags: %" GST_PTR_FORMAT, tags);
6095 gst_element_found_tags (GST_ELEMENT_CAST (avi), tags);
6096 avi->globaltags = NULL;
6097 avi->got_tags = FALSE;
6101 gst_avi_demux_loop (GstPad * pad)
6104 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
6106 switch (avi->state) {
6107 case GST_AVI_DEMUX_START:
6108 res = gst_avi_demux_stream_init_pull (avi);
6109 if (G_UNLIKELY (res != GST_FLOW_OK)) {
6110 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
6113 avi->state = GST_AVI_DEMUX_HEADER;
6115 case GST_AVI_DEMUX_HEADER:
6116 res = gst_avi_demux_stream_header_pull (avi);
6117 if (G_UNLIKELY (res != GST_FLOW_OK)) {
6118 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
6122 /* Send tag to decoder, so decoder can knows that this is divx drm file */
6123 if (avi->drmContext)
6124 gst_avi_demux_send_divx_tag (avi);
6127 avi->state = GST_AVI_DEMUX_MOVI;
6129 case GST_AVI_DEMUX_MOVI:
6130 if (G_UNLIKELY (avi->close_seg_event)) {
6131 gst_avi_demux_push_event (avi, avi->close_seg_event);
6132 avi->close_seg_event = NULL;
6134 if (G_UNLIKELY (avi->seg_event)) {
6135 gst_avi_demux_push_event (avi, avi->seg_event);
6136 avi->seg_event = NULL;
6138 if (G_UNLIKELY (avi->got_tags)) {
6139 push_tag_lists (avi);
6141 /* process each index entry in turn */
6142 res = gst_avi_demux_loop_data (avi);
6144 /* pause when error */
6145 if (G_UNLIKELY (res != GST_FLOW_OK)) {
6146 GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
6151 GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
6152 res = GST_FLOW_ERROR;
6161 gboolean push_eos = FALSE;
6162 GST_LOG_OBJECT (avi, "pausing task, reason %s", gst_flow_get_name (res));
6163 avi->segment_running = FALSE;
6164 gst_pad_pause_task (avi->sinkpad);
6167 if (res == GST_FLOW_UNEXPECTED) {
6168 /* handle end-of-stream/segment */
6169 if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
6172 if ((stop = avi->segment.stop) == -1)
6173 stop = avi->segment.duration;
6175 GST_INFO_OBJECT (avi, "sending segment_done");
6177 gst_element_post_message
6178 (GST_ELEMENT_CAST (avi),
6179 gst_message_new_segment_done (GST_OBJECT_CAST (avi),
6180 GST_FORMAT_TIME, stop));
6184 } else if (res == GST_FLOW_NOT_LINKED || res < GST_FLOW_UNEXPECTED) {
6185 /* for fatal errors we post an error message, wrong-state is
6186 * not fatal because it happens due to flushes and only means
6187 * that we should stop now. */
6188 GST_ELEMENT_ERROR (avi, STREAM, FAILED,
6189 (_("Internal data stream error.")),
6190 ("streaming stopped, reason %s", gst_flow_get_name (res)));
6194 GST_INFO_OBJECT (avi, "sending eos");
6195 if (!gst_avi_demux_push_event (avi, gst_event_new_eos ()) &&
6196 (res == GST_FLOW_UNEXPECTED)) {
6197 GST_ELEMENT_ERROR (avi, STREAM, DEMUX,
6198 (NULL), ("got eos but no streams (yet)"));
6205 static GstFlowReturn
6206 gst_avi_demux_chain (GstPad * pad, GstBuffer * buf)
6209 GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
6212 if (GST_BUFFER_IS_DISCONT (buf)) {
6213 GST_DEBUG_OBJECT (avi, "got DISCONT");
6214 gst_adapter_clear (avi->adapter);
6215 /* mark all streams DISCONT */
6216 for (i = 0; i < avi->num_streams; i++)
6217 avi->stream[i].discont = TRUE;
6220 GST_DEBUG ("Store %d bytes in adapter", GST_BUFFER_SIZE (buf));
6221 gst_adapter_push (avi->adapter, buf);
6223 switch (avi->state) {
6224 case GST_AVI_DEMUX_START:
6225 if ((res = gst_avi_demux_stream_init_push (avi)) != GST_FLOW_OK) {
6226 GST_WARNING ("stream_init flow: %s", gst_flow_get_name (res));
6230 case GST_AVI_DEMUX_HEADER:
6231 if ((res = gst_avi_demux_stream_header_push (avi)) != GST_FLOW_OK) {
6232 GST_WARNING ("stream_header flow: %s", gst_flow_get_name (res));
6236 case GST_AVI_DEMUX_MOVI:
6237 if (G_UNLIKELY (avi->close_seg_event)) {
6238 gst_avi_demux_push_event (avi, avi->close_seg_event);
6239 avi->close_seg_event = NULL;
6241 if (G_UNLIKELY (avi->seg_event)) {
6242 gst_avi_demux_push_event (avi, avi->seg_event);
6243 avi->seg_event = NULL;
6245 if (G_UNLIKELY (avi->got_tags)) {
6246 push_tag_lists (avi);
6248 res = gst_avi_demux_stream_data (avi);
6250 case GST_AVI_DEMUX_SEEK:
6256 /* obtain and parse indexes */
6257 if (avi->stream[0].indexes && !gst_avi_demux_read_subindexes_push (avi))
6258 /* seek in subindex read function failed */
6261 if (!avi->stream[0].indexes && !avi->have_index
6262 && avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
6263 gst_avi_demux_stream_index_push (avi);
6265 if (avi->have_index) {
6266 /* use the indexes now to construct nice durations */
6267 gst_avi_demux_calculate_durations_from_index (avi);
6269 /* still parsing indexes */
6273 GST_OBJECT_LOCK (avi);
6274 event = avi->seek_event;
6275 avi->seek_event = NULL;
6276 GST_OBJECT_UNLOCK (avi);
6278 /* calculate and perform seek */
6279 if (!avi_demux_handle_seek_push (avi, avi->sinkpad, event))
6282 gst_event_unref (event);
6283 avi->state = GST_AVI_DEMUX_MOVI;
6287 GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
6288 ("Illegal internal state"));
6289 res = GST_FLOW_ERROR;
6293 GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
6294 gst_flow_get_name (res));
6296 if (G_UNLIKELY (avi->abort_buffering))
6297 goto abort_buffering;
6304 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("failed to read indexes"));
6305 return GST_FLOW_ERROR;
6309 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("push mode seek failed"));
6310 return GST_FLOW_ERROR;
6314 avi->abort_buffering = FALSE;
6315 GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
6316 return GST_FLOW_ERROR;
6321 gst_avi_demux_sink_activate (GstPad * sinkpad)
6323 if (gst_pad_check_pull_range (sinkpad)) {
6324 GST_DEBUG ("going to pull mode");
6325 return gst_pad_activate_pull (sinkpad, TRUE);
6327 GST_DEBUG ("going to push (streaming) mode");
6328 return gst_pad_activate_push (sinkpad, TRUE);
6333 gst_avi_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
6335 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (sinkpad));
6338 avi->segment_running = TRUE;
6339 avi->streaming = FALSE;
6340 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
6343 avi->segment_running = FALSE;
6344 return gst_pad_stop_task (sinkpad);
6349 gst_avi_demux_activate_push (GstPad * pad, gboolean active)
6351 GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (pad));
6354 GST_DEBUG ("avi: activating push/chain function");
6355 avi->streaming = TRUE;
6357 /* create index for some push based seeking if not provided */
6358 GST_OBJECT_LOCK (avi);
6359 if (!avi->element_index) {
6360 GST_DEBUG_OBJECT (avi, "creating index");
6361 avi->element_index = gst_index_factory_make ("memindex");
6363 GST_OBJECT_UNLOCK (avi);
6364 /* object lock might be taken again */
6365 gst_index_get_writer_id (avi->element_index, GST_OBJECT_CAST (avi),
6369 GST_DEBUG ("avi: deactivating push/chain function");
6376 gst_avi_demux_set_index (GstElement * element, GstIndex * index)
6378 GstAviDemux *avi = GST_AVI_DEMUX (element);
6380 GST_OBJECT_LOCK (avi);
6381 if (avi->element_index)
6382 gst_object_unref (avi->element_index);
6384 avi->element_index = gst_object_ref (index);
6386 avi->element_index = NULL;
6388 GST_OBJECT_UNLOCK (avi);
6389 /* object lock might be taken again */
6391 gst_index_get_writer_id (index, GST_OBJECT_CAST (element), &avi->index_id);
6392 GST_DEBUG_OBJECT (avi, "Set index %" GST_PTR_FORMAT, avi->element_index);
6396 gst_avi_demux_get_index (GstElement * element)
6398 GstIndex *result = NULL;
6399 GstAviDemux *avi = GST_AVI_DEMUX (element);
6401 GST_OBJECT_LOCK (avi);
6402 if (avi->element_index)
6403 result = gst_object_ref (avi->element_index);
6404 GST_OBJECT_UNLOCK (avi);
6406 GST_DEBUG_OBJECT (avi, "Returning index %" GST_PTR_FORMAT, result);
6411 static GstStateChangeReturn
6412 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
6414 GstStateChangeReturn ret;
6415 GstAviDemux *avi = GST_AVI_DEMUX (element);
6417 switch (transition) {
6418 case GST_STATE_CHANGE_READY_TO_PAUSED:
6419 avi->streaming = FALSE;
6420 gst_segment_init (&avi->segment, GST_FORMAT_TIME);
6426 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
6427 if (ret == GST_STATE_CHANGE_FAILURE)
6430 switch (transition) {
6431 case GST_STATE_CHANGE_PAUSED_TO_READY:
6432 avi->have_index = FALSE;
6433 gst_avi_demux_reset (avi);