upload tizen1.0 source
[framework/multimedia/gst-plugins-good0.10.git] / gst / avi / gstavidemux.c
1 /* GStreamer
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>
5  *
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.
10  *
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.
15  *
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.
20  */
21 /* Element-Checklist-Version: 5 */
22
23 /**
24  * SECTION:element-avidemux
25  *
26  * Demuxes an .avi file into raw or compressed audio and/or video streams.
27  *
28  * This element supports both push and pull-based scheduling, depending on the
29  * capabilities of the upstream elements.
30  *
31  * <refsect2>
32  * <title>Example launch line</title>
33  * |[
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.
39  * </refsect2>
40  *
41  * Last reviewed on 2006-12-29 (0.10.6)
42  */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <string.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51
52 #include <dlfcn.h>
53
54 #include "gst/riff/riff-media.h"
55 #include "gstavidemux.h"
56 #include "avi-ids.h"
57 #include <gst/gst-i18n-plugin.h>
58 #include <gst/base/gstadapter.h>
59
60
61 #define DIV_ROUND_UP(s,v) (((s) + ((v)-1)) / (v))
62
63 #define GST_AVI_KEYFRAME 1
64 #ifdef AVIDEMUX_MODIFICATION
65 #define GST_AVI_NON_KEYFRAME 0
66 #endif
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)
70
71
72 GST_DEBUG_CATEGORY_STATIC (avidemux_debug);
73 #define GST_CAT_DEFAULT avidemux_debug
74
75 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
76     GST_PAD_SINK,
77     GST_PAD_ALWAYS,
78     GST_STATIC_CAPS ("video/x-msvideo")
79     );
80
81 #ifdef AVIDEMUX_MODIFICATION
82 /*Modification: Frame type enumeraions used to generat the index-table */
83 typedef enum
84 {
85     H264_NUT_UNKNOWN = 0,
86     H264_NUT_SLICE = 1,
87     H264_NUT_DPA = 2,
88     H264_NUT_DPB = 3,
89     H264_NUT_DPC = 4,
90     H264_NUT_IDR = 5,
91     H264_NUT_SEI = 6,
92     H264_NUT_SPS = 7,
93     H264_NUT_PPS = 8,
94     H264_NUT_AUD = 9,
95     H264_NUT_EOSEQ = 10,
96     H264_NUT_EOSTREAM = 11,
97     H264_NUT_FILL = 12,
98     H264_NUT_MIXED = 24,
99 } eH264NalType;
100 #endif
101
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);
106
107 static void gst_avi_demux_reset (GstAviDemux * avi);
108
109 #if 0
110 static const GstEventMask *gst_avi_demux_get_event_mask (GstPad * pad);
111 #endif
112 static gboolean gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event);
113 static gboolean gst_avi_demux_handle_sink_event (GstPad * pad,
114     GstEvent * event);
115 static gboolean gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event);
116
117 #if 0
118 static const GstFormat *gst_avi_demux_get_src_formats (GstPad * pad);
119 #endif
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);
124
125 static gboolean gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment);
126 static gboolean gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad,
127     GstEvent * event);
128 static gboolean gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
129     GstEvent * event);
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,
133     gboolean active);
134 static gboolean gst_avi_demux_activate_push (GstPad * pad, gboolean active);
135 static GstFlowReturn gst_avi_demux_chain (GstPad * pad, GstBuffer * buf);
136
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);
145
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 */
149 static int
150 gst_avi_demux_find_frame_type (GstAviStream *stream, GstBuffer *buf, int *frame_type);
151 #endif
152
153
154 static GstElementClass *parent_class = NULL;
155
156 #ifdef DIVX_DRM
157
158 typedef enum drmErrorCodes
159 {
160         DRM_SUCCESS = 0,
161         DRM_NOT_AUTHORIZED,
162         DRM_NOT_REGISTERED,
163         DRM_RENTAL_EXPIRED,
164         DRM_GENERAL_ERROR,
165         DRM_NEVER_REGISTERED,
166 } drmErrorCodes_t;
167
168
169 #define DIVX_SDK_PLUGIN_NAME "libmm_divxsdk.so"
170
171 static gboolean init_divx_plugin (GstAviDemux * avi)
172 {
173         char *error;
174         avi->divx_handle = dlopen (DIVX_SDK_PLUGIN_NAME, RTLD_LAZY);
175         if (!avi->divx_handle) {
176                 GST_ERROR ("dlopen failed [%s]", dlerror());
177                 return FALSE;
178         }
179         GST_DEBUG("dlopen success");
180
181         avi->divx_init = dlsym (avi->divx_handle, "divx_init");
182         if ((error = dlerror()) != NULL) {
183                 GST_ERROR ("[%s][%d]", __func__, __LINE__);
184                 goto DL_ERROR;
185         }
186
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);
190                 goto DL_ERROR;
191         }
192
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) {
196                 goto DL_ERROR;
197         }
198
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__);
202                 goto DL_ERROR;
203         }
204
205         avi->divx_finalize = dlsym (avi->divx_handle, "divx_finalize");
206         if ((error = dlerror()) != NULL) {
207                 GST_ERROR ("[%s][%d]", __func__, __LINE__);
208                 goto DL_ERROR;
209         }
210
211         return TRUE;
212
213 DL_ERROR:
214         GST_ERROR ("error : %s", error);
215         dlclose(avi->divx_handle);
216         avi->divx_handle = NULL;
217         return FALSE;
218 }
219
220
221 /* ---------------------------------------------------- DIVX DRM Code : Start -----------------------------------------------------------------*/
222 static gboolean
223 gst_avi_demux_init_divx_drm (GstAviDemux * avi, uint8_t* drm_info)
224 {
225         int error_code = 0;
226
227         if (init_divx_plugin (avi) == FALSE)    {
228                 GST_ERROR_OBJECT (avi, "Loading plugin failed....");
229                 return FALSE;
230         }
231         
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);
235         } else {
236                 GST_ERROR_OBJECT (avi,"%s  failed to init... error code = %d \n", __func__, error_code);
237                 return FALSE;
238         }
239         
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);
243         } else {
244                 GST_ERROR_OBJECT (avi,"%s  failed to commit... error code = %d \n", __func__, error_code);
245                 return FALSE;
246         }
247
248         return TRUE;
249 }
250
251 static gboolean
252 gst_avi_demux_send_divx_tag (GstAviDemux * avi)
253 {
254         gboolean ret = 0;
255         GstTagList *tags = NULL;
256         GST_ERROR_OBJECT (avi, "*********** posting divx drm  tags!!!!!!!!!!!!!!!!!!");
257         tags =  gst_tag_list_new_full ("drm_divx", "1", NULL);
258         if (tags) {
259                 ret = gst_avi_demux_push_event (avi, gst_event_new_tag (tags) );
260                 GST_ERROR_OBJECT (avi, "*********** posting tags returns [%d] !!!!!!!!!!!!!!!!!!", ret);
261         }
262         return ret;
263 }
264 /* ---------------------------------------------------- DIVX DRM Code : End  -----------------------------------------------------------------*/
265 #endif // DIVX_DRM
266 /* GObject methods */
267
268 GType
269 gst_avi_demux_get_type (void)
270 {
271   static GType avi_demux_type = 0;
272
273   if (!avi_demux_type) {
274     static const GTypeInfo avi_demux_info = {
275       sizeof (GstAviDemuxClass),
276       (GBaseInitFunc) gst_avi_demux_base_init,
277       NULL,
278       (GClassInitFunc) gst_avi_demux_class_init,
279       NULL,
280       NULL,
281       sizeof (GstAviDemux),
282       0,
283       (GInstanceInitFunc) gst_avi_demux_init,
284     };
285
286     avi_demux_type =
287         g_type_register_static (GST_TYPE_ELEMENT,
288         "GstAviDemux", &avi_demux_info, 0);
289   }
290
291   return avi_demux_type;
292 }
293
294 static void
295 gst_avi_demux_base_init (GstAviDemuxClass * klass)
296 {
297   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
298   GstPadTemplate *videosrctempl, *audiosrctempl, *subsrctempl;
299   GstCaps *audcaps, *vidcaps, *subcaps;
300
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);
305
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);
311
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",
321       "Codec/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>");
326 }
327
328 static void
329 gst_avi_demux_class_init (GstAviDemuxClass * klass)
330 {
331   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
332   GObjectClass *gobject_class = (GObjectClass *) klass;
333
334   GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
335       0, "Demuxer for AVI streams");
336
337   parent_class = g_type_class_peek_parent (klass);
338
339   gobject_class->finalize = gst_avi_demux_finalize;
340   gstelement_class->change_state =
341       GST_DEBUG_FUNCPTR (gst_avi_demux_change_state);
342
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);
345
346 #ifdef DIVX_DRM
347         gst_tag_register ("drm_divx", GST_TAG_FLAG_META,
348                         G_TYPE_STRING,
349                         _("DRM DivX"),
350                         _("a tag that is specific to DivX DRM File"),
351                         NULL);
352 #endif
353 }
354
355 static void
356 gst_avi_demux_init (GstAviDemux * avi)
357 {
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);
370
371   avi->adapter = gst_adapter_new ();
372
373   gst_avi_demux_reset (avi);
374 }
375
376 static void
377 gst_avi_demux_finalize (GObject * object)
378 {
379   GstAviDemux *avi = GST_AVI_DEMUX (object);
380
381   GST_DEBUG ("AVI: finalize");
382
383   g_object_unref (avi->adapter);
384
385   G_OBJECT_CLASS (parent_class)->finalize (object);
386 }
387
388 static void
389 gst_avi_demux_reset_stream (GstAviDemux * avi, GstAviStream * stream)
390 {
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);
400   if (stream->pad) {
401     if (stream->exposed) {
402       gst_pad_set_active (stream->pad, FALSE);
403       gst_element_remove_pad (GST_ELEMENT_CAST (avi), stream->pad);
404     } else
405       gst_object_unref (stream->pad);
406   }
407   if (stream->taglist) {
408     gst_tag_list_free (stream->taglist);
409     stream->taglist = NULL;
410   }
411   memset (stream, 0, sizeof (GstAviStream));
412 }
413
414 static void
415 gst_avi_demux_reset (GstAviDemux * avi)
416 {
417   gint i;
418
419   GST_DEBUG ("AVI: reset");
420
421   for (i = 0; i < avi->num_streams; i++)
422     gst_avi_demux_reset_stream (avi, &avi->stream[i]);
423
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;
430
431   avi->state = GST_AVI_DEMUX_START;
432   avi->offset = 0;
433   avi->building_index = FALSE;
434
435   avi->index_offset = 0;
436   g_free (avi->avih);
437   avi->avih = NULL;
438
439   if (avi->element_index)
440     gst_object_unref (avi->element_index);
441   avi->element_index = NULL;
442
443   if (avi->close_seg_event) {
444     gst_event_unref (avi->close_seg_event);
445     avi->close_seg_event = NULL;
446   }
447   if (avi->seg_event) {
448     gst_event_unref (avi->seg_event);
449     avi->seg_event = NULL;
450   }
451   if (avi->seek_event) {
452     gst_event_unref (avi->seek_event);
453     avi->seek_event = NULL;
454   }
455
456   if (avi->globaltags)
457     gst_tag_list_free (avi->globaltags);
458   avi->globaltags = NULL;
459
460   avi->got_tags = TRUE;         /* we always want to push global tags */
461   avi->have_eos = FALSE;
462   avi->seekable = TRUE;
463
464   gst_adapter_clear (avi->adapter);
465
466   gst_segment_init (&avi->segment, GST_FORMAT_TIME);
467
468 #ifdef DIVX_DRM
469   if(avi->drmContext)
470   {
471           avi->divx_finalize (avi->drmContext);
472           free(avi->drmContext);
473           avi->drmContext = NULL;
474   }
475
476   if (avi->divx_handle)
477   {
478         dlclose(avi->divx_handle);
479         avi->divx_handle = NULL;
480   }
481 #endif
482 }
483
484
485 /* GstElement methods */
486
487 #if 0
488 static const GstFormat *
489 gst_avi_demux_get_src_formats (GstPad * pad)
490 {
491   GstAviStream *stream = gst_pad_get_element_private (pad);
492
493   static const GstFormat src_a_formats[] = {
494     GST_FORMAT_TIME,
495     GST_FORMAT_BYTES,
496     GST_FORMAT_DEFAULT,
497     0
498   };
499   static const GstFormat src_v_formats[] = {
500     GST_FORMAT_TIME,
501     GST_FORMAT_DEFAULT,
502     0
503   };
504
505   return (stream->strh->type == GST_RIFF_FCC_auds ?
506       src_a_formats : src_v_formats);
507 }
508 #endif
509
510 /* assumes stream->strf.auds->av_bps != 0 */
511 static inline GstClockTime
512 avi_stream_convert_bytes_to_time_unchecked (GstAviStream * stream,
513     guint64 bytes)
514 {
515   return gst_util_uint64_scale_int (bytes, GST_SECOND,
516       stream->strf.auds->av_bps);
517 }
518
519 static inline guint64
520 avi_stream_convert_time_to_bytes_unchecked (GstAviStream * stream,
521     GstClockTime time)
522 {
523   return gst_util_uint64_scale_int (time, stream->strf.auds->av_bps,
524       GST_SECOND);
525 }
526
527 /* assumes stream->strh->rate != 0 */
528 static inline GstClockTime
529 avi_stream_convert_frames_to_time_unchecked (GstAviStream * stream,
530     guint64 frames)
531 {
532   return gst_util_uint64_scale (frames, stream->strh->scale * GST_SECOND,
533       stream->strh->rate);
534 }
535
536 static inline guint64
537 avi_stream_convert_time_to_frames_unchecked (GstAviStream * stream,
538     GstClockTime time)
539 {
540   return gst_util_uint64_scale (time, stream->strh->rate,
541       stream->strh->scale * GST_SECOND);
542 }
543
544 static gboolean
545 gst_avi_demux_src_convert (GstPad * pad,
546     GstFormat src_format,
547     gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
548 {
549   GstAviStream *stream = gst_pad_get_element_private (pad);
550   gboolean res = TRUE;
551
552   GST_LOG_OBJECT (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));
556
557   if (G_UNLIKELY (src_format == *dest_format)) {
558     *dest_value = src_value;
559     goto done;
560   }
561   if (G_UNLIKELY (!stream->strh || !stream->strf.data)) {
562     res = FALSE;
563     goto done;
564   }
565   if (G_UNLIKELY (stream->strh->type == GST_RIFF_FCC_vids &&
566           (src_format == GST_FORMAT_BYTES
567               || *dest_format == GST_FORMAT_BYTES))) {
568     res = FALSE;
569     goto done;
570   }
571
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);
578           break;
579         case GST_FORMAT_DEFAULT:
580           *dest_value =
581               gst_util_uint64_scale_round (src_value, stream->strh->rate,
582               stream->strh->scale * GST_SECOND);
583           break;
584         default:
585           res = FALSE;
586           break;
587       }
588       break;
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,
594                 src_value);
595           } else
596             res = FALSE;
597           break;
598         default:
599           res = FALSE;
600           break;
601       }
602       break;
603     case GST_FORMAT_DEFAULT:
604       switch (*dest_format) {
605         case GST_FORMAT_TIME:
606           *dest_value =
607               avi_stream_convert_frames_to_time_unchecked (stream, src_value);
608           break;
609         default:
610           res = FALSE;
611           break;
612       }
613       break;
614     default:
615       res = FALSE;
616   }
617
618 done:
619   GST_LOG_OBJECT (pad,
620       "Returning res:%d dest_format:%s dest_value:%" G_GUINT64_FORMAT, res,
621       gst_format_get_name (*dest_format), *dest_value);
622   return res;
623 }
624
625 static const GstQueryType *
626 gst_avi_demux_get_src_query_types (GstPad * pad)
627 {
628   static const GstQueryType src_types[] = {
629     GST_QUERY_POSITION,
630     GST_QUERY_DURATION,
631     GST_QUERY_SEEKING,
632     GST_QUERY_CONVERT,
633     0
634   };
635
636   return src_types;
637 }
638
639 static gboolean
640 gst_avi_demux_handle_src_query (GstPad * pad, GstQuery * query)
641 {
642   gboolean res = TRUE;
643   GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
644
645   GstAviStream *stream = gst_pad_get_element_private (pad);
646
647   if (!stream->strh || !stream->strf.data)
648     return gst_pad_query_default (pad, query);
649
650   switch (GST_QUERY_TYPE (query)) {
651     case GST_QUERY_POSITION:{
652       gint64 pos = 0;
653
654       GST_DEBUG ("pos query for stream %u: frames %u, bytes %u",
655           stream->num, stream->current_entry, stream->current_total);
656
657       /* FIXME, this looks clumsy */
658       if (stream->strh->type == GST_RIFF_FCC_auds) {
659         if (stream->is_vbr) {
660           /* 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) {
666           /* CBR */
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;
676
677           if (stream->is_vbr) {
678             pos = gst_util_uint64_scale (xlen, stream->current_entry,
679                 stream->idx_n);
680             GST_DEBUG_OBJECT (avi, "VBR perc convert frame %u, time %"
681                 GST_TIME_FORMAT, stream->current_entry, GST_TIME_ARGS (pos));
682           } else {
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));
688           }
689         } else {
690           /* we don't know */
691           res = FALSE;
692         }
693       } else {
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);
697         } else {
698           pos = stream->current_entry * avi->avih->us_frame * GST_USECOND;
699         }
700       }
701       if (res) {
702         GST_DEBUG ("pos query : %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
703         gst_query_set_position (query, GST_FORMAT_TIME, pos);
704       } else
705         GST_WARNING ("pos query failed");
706       break;
707     }
708     case GST_QUERY_DURATION:
709     {
710       GstFormat fmt;
711       GstClockTime duration;
712
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) {
716         res = FALSE;
717         break;
718       }
719
720       /* take stream duration, fall back to avih duration */
721       if ((duration = stream->duration) == -1)
722         duration = avi->duration;
723
724       gst_query_parse_duration (query, &fmt, NULL);
725
726       switch (fmt) {
727         case GST_FORMAT_TIME:
728           gst_query_set_duration (query, fmt, duration);
729           break;
730         case GST_FORMAT_DEFAULT:
731         {
732           gint64 dur;
733           GST_DEBUG_OBJECT (query, "total frames is %" G_GUINT32_FORMAT,
734               stream->idx_n);
735
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);
741           break;
742         }
743         default:
744           res = FALSE;
745           break;
746       }
747       break;
748     }
749     case GST_QUERY_SEEKING:{
750       GstFormat fmt;
751
752       gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
753       if (fmt == GST_FORMAT_TIME) {
754         gboolean seekable = TRUE;
755
756         if (avi->streaming) {
757           seekable = avi->seekable;
758         }
759
760         gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
761             0, stream->duration);
762         res = TRUE;
763       }
764       break;
765     }
766     case GST_QUERY_CONVERT:{
767       GstFormat src_fmt, dest_fmt;
768       gint64 src_val, dest_val;
769
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,
772                   &dest_val)))
773         gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
774       else
775         res = gst_pad_query_default (pad, query);
776       break;
777     }
778     default:
779       res = gst_pad_query_default (pad, query);
780       break;
781   }
782
783   gst_object_unref (avi);
784   return res;
785 }
786
787 #if 0
788 static const GstEventMask *
789 gst_avi_demux_get_event_mask (GstPad * pad)
790 {
791   static const GstEventMask masks[] = {
792     {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT},
793     {0,}
794   };
795
796   return masks;
797 }
798 #endif
799
800 static guint64
801 gst_avi_demux_seek_streams (GstAviDemux * avi, guint64 offset, gboolean before)
802 {
803   GstAviStream *stream;
804   GstIndexEntry *entry;
805   gint i;
806   gint64 val, min = offset;
807
808   for (i = 0; i < avi->num_streams; i++) {
809     stream = &avi->stream[i];
810
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);
814
815     if (before) {
816       if (entry) {
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);
820         if (val < min)
821           min = val;
822       }
823       continue;
824     }
825
826     if (!entry) {
827       GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
828       stream->current_entry = 0;
829       stream->current_total = 0;
830       continue;
831     }
832
833     gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &val);
834     GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT,
835         i, val);
836
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;
841   }
842
843   return min;
844 }
845
846 static guint
847 gst_avi_demux_index_entry_offset_search (GstAviIndexEntry * entry,
848     guint64 * offset)
849 {
850   if (entry->offset < *offset)
851     return -1;
852   else if (entry->offset > *offset)
853     return 1;
854   return 0;
855 }
856
857 static guint64
858 gst_avi_demux_seek_streams_index (GstAviDemux * avi, guint64 offset,
859     gboolean before)
860 {
861   GstAviStream *stream;
862   GstAviIndexEntry *entry;
863   gint i;
864   gint64 val, min = offset;
865   guint index = 0;
866
867   for (i = 0; i < avi->num_streams; i++) {
868     stream = &avi->stream[i];
869
870     /* compensate for chunk header */
871     offset += 8;
872     entry =
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);
877     offset -= 8;
878
879     if (entry)
880       index = entry - stream->index;
881
882     if (before) {
883       if (entry) {
884         val = stream->index[index].offset;
885         GST_DEBUG_OBJECT (avi,
886             "stream %d, previous entry at %" G_GUINT64_FORMAT, i, val);
887         if (val < min)
888           min = val;
889       }
890       continue;
891     }
892
893     if (!entry) {
894       GST_DEBUG_OBJECT (avi, "no position for stream %d, assuming at start", i);
895       stream->current_entry = 0;
896       stream->current_total = 0;
897       continue;
898     }
899
900     val = stream->index[index].offset - 8;
901     GST_DEBUG_OBJECT (avi, "stream %d, next entry at %" G_GUINT64_FORMAT, i,
902         val);
903
904     stream->current_total = stream->index[index].total;
905     stream->current_entry = index;
906   }
907
908   return min;
909 }
910
911 #define GST_AVI_SEEK_PUSH_DISPLACE     (4 * GST_SECOND)
912
913 static gboolean
914 gst_avi_demux_handle_sink_event (GstPad * pad, GstEvent * event)
915 {
916   gboolean res = TRUE;
917   GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
918
919   GST_DEBUG_OBJECT (avi,
920       "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
921
922   switch (GST_EVENT_TYPE (event)) {
923     case GST_EVENT_NEWSEGMENT:
924     {
925       GstFormat format;
926       gdouble rate, arate;
927       gint64 start, stop, time, offset = 0;
928       gboolean update;
929       GstSegment segment;
930
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,
936           start, stop, time);
937       GST_DEBUG_OBJECT (avi,
938           "received format %d newsegment %" GST_SEGMENT_FORMAT, format,
939           &segment);
940
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");
944         goto exit;
945       }
946
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");
950         goto exit;
951       }
952
953       if (avi->have_index) {
954         GstAviIndexEntry *entry;
955         guint i = 0, index = 0, k = 0;
956         GstAviStream *stream;
957
958         /* compensate chunk header, stored index offset points after header */
959         start += 8;
960         /* find which stream we're on */
961         do {
962           stream = &avi->stream[i];
963
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);
969
970           if (entry == NULL)
971             continue;
972           index = entry - stream->index;
973
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;
977             k = i;
978           }
979           /* exact match needs no further searching */
980           if (stream->index[index].offset == start)
981             break;
982         } while (++i < avi->num_streams);
983         start -= 8;
984         offset -= 8;
985         stream = &avi->stream[k];
986
987         /* so we have no idea what is to come, or where we are */
988         if (!offset) {
989           GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
990           goto eos;
991         }
992
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;
998
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);
1003
1004         /* we can not go where we have not yet been before ... */
1005         if (!entry) {
1006           GST_WARNING_OBJECT (avi, "insufficient index data, forcing EOS");
1007           goto eos;
1008         }
1009
1010         gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &time);
1011         gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &offset);
1012       } else {
1013         GST_WARNING_OBJECT (avi, "no index data, forcing EOS");
1014         goto eos;
1015       }
1016
1017       stop = GST_CLOCK_TIME_NONE;
1018
1019       /* compensate for slack */
1020       if (time)
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,
1028           time, stop);
1029       gst_avi_demux_push_event (avi,
1030           gst_event_new_new_segment_full (update, rate, arate, GST_FORMAT_TIME,
1031               time, stop, time));
1032
1033       GST_DEBUG_OBJECT (avi, "next chunk expected at %" G_GINT64_FORMAT, start);
1034
1035       /* adjust state for streaming thread accordingly */
1036       if (avi->have_index)
1037         gst_avi_demux_seek_streams_index (avi, offset, FALSE);
1038       else
1039         gst_avi_demux_seek_streams (avi, offset, FALSE);
1040
1041       /* set up streaming thread */
1042       g_assert (offset >= start);
1043       avi->offset = start;
1044       avi->todrop = offset - start;
1045
1046     exit:
1047       gst_event_unref (event);
1048       res = TRUE;
1049       break;
1050     eos:
1051       /* set up for EOS */
1052       avi->have_eos = TRUE;
1053       goto exit;
1054     }
1055     case GST_EVENT_EOS:
1056     {
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)"));
1064       }
1065       break;
1066     }
1067     case GST_EVENT_FLUSH_STOP:
1068     {
1069       gint i;
1070
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;
1076       }
1077       /* fall through to default case so that the event gets passed downstream */
1078     }
1079     default:
1080       res = gst_pad_event_default (pad, event);
1081       break;
1082   }
1083
1084   gst_object_unref (avi);
1085
1086   return res;
1087 }
1088
1089 static gboolean
1090 gst_avi_demux_handle_src_event (GstPad * pad, GstEvent * event)
1091 {
1092   gboolean res = TRUE;
1093   GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
1094
1095   GST_DEBUG_OBJECT (avi,
1096       "have event type %s: %p on src pad", GST_EVENT_TYPE_NAME (event), event);
1097
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);
1102       } else {
1103         res = gst_avi_demux_handle_seek_push (avi, pad, event);
1104       }
1105       gst_event_unref (event);
1106       break;
1107     case GST_EVENT_QOS:
1108     case GST_EVENT_NAVIGATION:
1109       res = FALSE;
1110       gst_event_unref (event);
1111       break;
1112     default:
1113       res = gst_pad_event_default (pad, event);
1114       break;
1115   }
1116
1117   gst_object_unref (avi);
1118
1119   return res;
1120 }
1121
1122 /* streaming helper (push) */
1123
1124 /*
1125  * gst_avi_demux_peek_chunk_info:
1126  * @avi: Avi object
1127  * @tag: holder for tag
1128  * @size: holder for tag size
1129  *
1130  * Peek next chunk info (tag and size)
1131  *
1132  * Returns: TRUE when one chunk info has been got
1133  */
1134 static gboolean
1135 gst_avi_demux_peek_chunk_info (GstAviDemux * avi, guint32 * tag, guint32 * size)
1136 {
1137   const guint8 *data = NULL;
1138
1139   if (gst_adapter_available (avi->adapter) < 8)
1140     return FALSE;
1141
1142   data = gst_adapter_peek (avi->adapter, 8);
1143   *tag = GST_READ_UINT32_LE (data);
1144   *size = GST_READ_UINT32_LE (data + 4);
1145
1146   return TRUE;
1147 }
1148
1149 /*
1150  * gst_avi_demux_peek_chunk:
1151  * @avi: Avi object
1152  * @tag: holder for tag
1153  * @size: holder for tag size
1154  *
1155  * Peek enough data for one full chunk
1156  *
1157  * Returns: %TRUE when one chunk has been got
1158  */
1159 static gboolean
1160 gst_avi_demux_peek_chunk (GstAviDemux * avi, guint32 * tag, guint32 * size)
1161 {
1162   guint32 peek_size = 0;
1163   gint available;
1164
1165   if (!gst_avi_demux_peek_chunk_info (avi, tag, size))
1166     goto peek_failed;
1167
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))
1173     goto strange_size;
1174
1175   peek_size = (*size + 1) & ~1;
1176   available = gst_adapter_available (avi->adapter);
1177
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);
1181
1182   if (available < (8 + peek_size))
1183     goto need_more;
1184
1185   return TRUE;
1186
1187   /* ERRORS */
1188 peek_failed:
1189   {
1190     GST_INFO_OBJECT (avi, "Failed to peek");
1191     return FALSE;
1192   }
1193 strange_size:
1194   {
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;
1200     return FALSE;
1201   }
1202 need_more:
1203   {
1204     GST_INFO_OBJECT (avi, "need more %d < %" G_GUINT32_FORMAT,
1205         available, 8 + peek_size);
1206     return FALSE;
1207   }
1208 }
1209
1210 /* AVI init */
1211
1212 /*
1213  * gst_avi_demux_parse_file_header:
1214  * @element: caller element (used for errors/debug).
1215  * @buf: input data to be used for parsing.
1216  *
1217  * "Open" a RIFF/AVI file. The buffer should be at least 12
1218  * bytes long. Takes ownership of @buf.
1219  *
1220  * Returns: TRUE if the file is a RIFF/AVI file, FALSE otherwise.
1221  *          Throws an error, caller should error out (fatal).
1222  */
1223 static gboolean
1224 gst_avi_demux_parse_file_header (GstElement * element, GstBuffer * buf)
1225 {
1226   guint32 doctype;
1227   GstClockTime stamp;
1228
1229   stamp = gst_util_get_timestamp ();
1230
1231   /* riff_parse posts an error */
1232   if (!gst_riff_parse_file_header (element, buf, &doctype))
1233     return FALSE;
1234
1235   if (doctype != GST_RIFF_RIFF_AVI)
1236     goto not_avi;
1237
1238   stamp = gst_util_get_timestamp () - stamp;
1239   GST_DEBUG_OBJECT (element, "header parsing took %" GST_TIME_FORMAT,
1240       GST_TIME_ARGS (stamp));
1241
1242   return TRUE;
1243
1244   /* ERRORS */
1245 not_avi:
1246   {
1247     GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
1248         ("File is not an AVI file: %" GST_FOURCC_FORMAT,
1249             GST_FOURCC_ARGS (doctype)));
1250     return FALSE;
1251   }
1252 }
1253
1254 /*
1255  * Read AVI file tag when streaming
1256  */
1257 static GstFlowReturn
1258 gst_avi_demux_stream_init_push (GstAviDemux * avi)
1259 {
1260   if (gst_adapter_available (avi->adapter) >= 12) {
1261     GstBuffer *tmp;
1262
1263     tmp = gst_adapter_take_buffer (avi->adapter, 12);
1264
1265     GST_DEBUG ("Parsing avi header");
1266     if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), tmp)) {
1267       return GST_FLOW_ERROR;
1268     }
1269     GST_DEBUG ("header ok");
1270     avi->offset += 12;
1271
1272     avi->state = GST_AVI_DEMUX_HEADER;
1273   }
1274   return GST_FLOW_OK;
1275 }
1276
1277 /*
1278  * Read AVI file tag
1279  */
1280 static GstFlowReturn
1281 gst_avi_demux_stream_init_pull (GstAviDemux * avi)
1282 {
1283   GstFlowReturn res;
1284   GstBuffer *buf = NULL;
1285
1286   res = gst_pad_pull_range (avi->sinkpad, avi->offset, 12, &buf);
1287   if (res != GST_FLOW_OK)
1288     return res;
1289   else if (!gst_avi_demux_parse_file_header (GST_ELEMENT_CAST (avi), buf))
1290     goto wrong_header;
1291
1292   avi->offset += 12;
1293
1294   return GST_FLOW_OK;
1295
1296   /* ERRORS */
1297 wrong_header:
1298   {
1299     GST_DEBUG_OBJECT (avi, "error parsing file header");
1300     return GST_FLOW_ERROR;
1301   }
1302 }
1303
1304 /* AVI header handling */
1305 /*
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.).
1311  *
1312  * Read 'avih' header. Discards buffer after use.
1313  *
1314  * Returns: TRUE on success, FALSE otherwise. Throws an error if
1315  *          the header is invalid. The caller should error out
1316  *          (fatal).
1317  */
1318 static gboolean
1319 gst_avi_demux_parse_avih (GstAviDemux * avi,
1320     GstBuffer * buf, gst_riff_avih ** _avih)
1321 {
1322   gst_riff_avih *avih;
1323
1324   if (buf == NULL)
1325     goto no_buffer;
1326
1327   if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_avih))
1328     goto avih_too_small;
1329
1330   avih = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1331
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);
1347 #endif
1348
1349   /* debug stuff */
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);
1365
1366   *_avih = avih;
1367   gst_buffer_unref (buf);
1368
1369   if (avih->us_frame != 0 && avih->tot_frames != 0)
1370     avi->duration =
1371         (guint64) avih->us_frame * (guint64) avih->tot_frames * 1000;
1372   else
1373     avi->duration = GST_CLOCK_TIME_NONE;
1374
1375   GST_INFO_OBJECT (avi, " header duration  %" GST_TIME_FORMAT,
1376       GST_TIME_ARGS (avi->duration));
1377
1378   return TRUE;
1379
1380   /* ERRORS */
1381 no_buffer:
1382   {
1383     GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No buffer"));
1384     return FALSE;
1385   }
1386 avih_too_small:
1387   {
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);
1392     return FALSE;
1393   }
1394 }
1395
1396 /*
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.
1403  *
1404  * Reads superindex (openDML-2 spec stuff) from the provided data.
1405  *
1406  * Returns: TRUE on success, FALSE otherwise. Indexes should be skipped
1407  *          on error, but they are not fatal.
1408  */
1409 static gboolean
1410 gst_avi_demux_parse_superindex (GstAviDemux * avi,
1411     GstBuffer * buf, guint64 ** _indexes)
1412 {
1413   guint8 *data;
1414   guint16 bpe = 16;
1415   guint32 num, i;
1416   guint64 *indexes;
1417   guint size;
1418
1419   *_indexes = NULL;
1420
1421   size = buf ? GST_BUFFER_SIZE (buf) : 0;
1422   if (size < 24)
1423     goto too_small;
1424
1425   data = GST_BUFFER_DATA (buf);
1426
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;
1437   }
1438   num = GST_READ_UINT32_LE (&data[4]);
1439
1440   GST_DEBUG_OBJECT (avi, "got %d indexes", num);
1441
1442   /* this can't work out well ... */
1443   if (num > G_MAXUINT32 >> 1 || bpe < 8) {
1444     goto invalid_params;
1445   }
1446
1447   indexes = g_new (guint64, num + 1);
1448   for (i = 0; i < num; i++) {
1449     if (size < 24 + bpe * (i + 1))
1450       break;
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]);
1453   }
1454   indexes[i] = GST_BUFFER_OFFSET_NONE;
1455   *_indexes = indexes;
1456
1457   gst_buffer_unref (buf);
1458
1459   return TRUE;
1460
1461   /* ERRORS */
1462 too_small:
1463   {
1464     GST_ERROR_OBJECT (avi,
1465         "Not enough data to parse superindex (%d available, 24 needed)", size);
1466     if (buf)
1467       gst_buffer_unref (buf);
1468     return FALSE;
1469   }
1470 invalid_params:
1471   {
1472     GST_ERROR_OBJECT (avi, "invalid index parameters (num = %d, bpe = %d)",
1473         num, bpe);
1474     if (buf)
1475       gst_buffer_unref (buf);
1476     return FALSE;
1477   }
1478 }
1479
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)
1486 {
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;
1491
1492     /* we need to make some more room */
1493     if (idx_max == 0) {
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));
1497     } else {
1498       idx_max += 8192 / sizeof (GstAviIndexEntry);
1499       GST_DEBUG_OBJECT (avi, "expanded index from %u to %u",
1500           stream->idx_max, idx_max);
1501     }
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))
1505       return FALSE;
1506     /* use new index */
1507     stream->index = new_idx;
1508     stream->idx_max = idx_max;
1509   }
1510
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) {
1514     gint blockalign;
1515
1516     if (stream->is_vbr) {
1517       entry->total = stream->total_blocks;
1518     } else {
1519       entry->total = stream->total_bytes;
1520     }
1521     blockalign = stream->strf.auds->blockalign;
1522     if (blockalign > 0)
1523       stream->total_blocks += DIV_ROUND_UP (entry->size, blockalign);
1524     else
1525       stream->total_blocks++;
1526   } else {
1527     if (stream->is_vbr) {
1528       entry->total = stream->idx_n;
1529     } else {
1530       entry->total = stream->total_bytes;
1531     }
1532   }
1533   stream->total_bytes += entry->size;
1534   if (ENTRY_IS_KEYFRAME (entry))
1535     stream->n_keyframes++;
1536
1537   /* and add */
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,
1542       entry->total);
1543   stream->index[stream->idx_n++] = *entry;
1544
1545   return TRUE;
1546 }
1547
1548 /* given @entry_n in @stream, calculate info such as timestamps and
1549  * offsets for the entry. */
1550 static void
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)
1554 {
1555   GstAviIndexEntry *entry;
1556
1557   entry = &stream->index[entry_n];
1558
1559   if (stream->is_vbr) {
1560     /* VBR stream next timestamp */
1561     if (stream->strh->type == GST_RIFF_FCC_auds) {
1562       if (timestamp)
1563         *timestamp =
1564             avi_stream_convert_frames_to_time_unchecked (stream, entry->total);
1565       if (ts_end)
1566         *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1567             entry->total + 1);
1568     } else {
1569       if (timestamp)
1570         *timestamp =
1571             avi_stream_convert_frames_to_time_unchecked (stream, entry_n);
1572       if (ts_end)
1573         *ts_end = avi_stream_convert_frames_to_time_unchecked (stream,
1574             entry_n + 1);
1575     }
1576   } else if (stream->strh->type == GST_RIFF_FCC_auds) {
1577     /* constant rate stream */
1578     if (timestamp)
1579       *timestamp =
1580           avi_stream_convert_bytes_to_time_unchecked (stream, entry->total);
1581     if (ts_end)
1582       *ts_end = avi_stream_convert_bytes_to_time_unchecked (stream,
1583           entry->total + entry->size);
1584   }
1585   if (stream->strh->type == GST_RIFF_FCC_vids) {
1586     /* video offsets are the frame number */
1587     if (offset)
1588       *offset = entry_n;
1589     if (offset_end)
1590       *offset_end = entry_n + 1;
1591   } else {
1592     /* no offsets for audio */
1593     if (offset)
1594       *offset = -1;
1595     if (offset_end)
1596       *offset_end = -1;
1597   }
1598 }
1599
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.
1603  *
1604  * Returns TRUE if the index is not empty, else FALSE */
1605 static gboolean
1606 gst_avi_demux_do_index_stats (GstAviDemux * avi)
1607 {
1608   guint total_idx = 0;
1609   guint i;
1610 #ifndef GST_DISABLE_GST_DEBUG
1611   guint total_max = 0;
1612 #endif
1613
1614   /* get stream stats now */
1615   for (i = 0; i < avi->num_streams; i++) {
1616     GstAviStream *stream;
1617
1618     if (G_UNLIKELY (!(stream = &avi->stream[i])))
1619       continue;
1620     if (G_UNLIKELY (!stream->strh))
1621       continue;
1622     if (G_UNLIKELY (!stream->index || stream->idx_n == 0))
1623       continue;
1624
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);
1629
1630     total_idx += stream->idx_n;
1631 #ifndef GST_DISABLE_GST_DEBUG
1632     total_max += stream->idx_max;
1633 #endif
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)));
1640   }
1641   total_idx *= sizeof (GstAviIndexEntry);
1642 #ifndef GST_DISABLE_GST_DEBUG
1643   total_max *= sizeof (GstAviIndexEntry);
1644 #endif
1645   GST_INFO_OBJECT (avi, "%u bytes for index vs %u ideally, %u wasted",
1646       total_max, total_idx, total_max - total_idx);
1647
1648   if (total_idx == 0) {
1649     GST_WARNING_OBJECT (avi, "Index is empty !");
1650     return FALSE;
1651   }
1652   return TRUE;
1653 }
1654
1655 /*
1656  * gst_avi_demux_parse_subindex:
1657  * @avi: Avi object
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
1664  *           found.
1665  *
1666  * Reads superindex (openDML-2 spec stuff) from the provided data.
1667  * The buffer should contain a GST_RIFF_TAG_ix?? chunk.
1668  *
1669  * Returns: TRUE on success, FALSE otherwise. Errors are fatal, we
1670  *          throw an error, caller should bail out asap.
1671  */
1672 static gboolean
1673 gst_avi_demux_parse_subindex (GstAviDemux * avi, GstAviStream * stream,
1674     GstBuffer * buf)
1675 {
1676   guint8 *data;
1677   guint16 bpe;
1678   guint32 num, i;
1679   guint64 baseoff;
1680   guint size;
1681
1682   if (!buf)
1683     return TRUE;
1684
1685   size = GST_BUFFER_SIZE (buf);
1686
1687   /* check size */
1688   if (size < 24)
1689     goto too_small;
1690
1691   data = GST_BUFFER_DATA (buf);
1692
1693   /* We don't support index-data yet */
1694   if (data[3] & 0x80)
1695     goto not_implemented;
1696
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;
1708   }
1709   num = GST_READ_UINT32_LE (&data[4]);
1710   baseoff = GST_READ_UINT64_LE (&data[12]);
1711
1712   /* If there's nothing, just return ! */
1713   if (num == 0)
1714     goto empty_index;
1715
1716   GST_INFO_OBJECT (avi, "Parsing subindex, nr_entries = %6d", num);
1717
1718   for (i = 0; i < num; i++) {
1719     GstAviIndexEntry entry;
1720
1721     if (size < 24 + bpe * (i + 1))
1722       break;
1723
1724     /* fill in offset and size. offset contains the keyframe flag in the
1725      * upper bit*/
1726     entry.offset = baseoff + GST_READ_UINT32_LE (&data[24 + bpe * i]);
1727     entry.size = GST_READ_UINT32_LE (&data[24 + bpe * i + 4]);
1728     /* handle flags */
1729     if (stream->strh->type == GST_RIFF_FCC_auds) {
1730       /* all audio frames are keyframes */
1731       ENTRY_SET_KEYFRAME (&entry);
1732     } else {
1733       /* else read flags */
1734       entry.flags = (entry.size & 0x80000000) ? 0 : GST_AVI_KEYFRAME;
1735     }
1736     entry.size &= ~0x80000000;
1737
1738     /* and add */
1739     if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
1740       goto out_of_mem;
1741   }
1742   gst_buffer_unref (buf);
1743
1744   return TRUE;
1745
1746   /* ERRORS */
1747 too_small:
1748   {
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 */
1753   }
1754 not_implemented:
1755   {
1756     GST_ELEMENT_ERROR (avi, STREAM, NOT_IMPLEMENTED, (NULL),
1757         ("Subindex-is-data is not implemented"));
1758     gst_buffer_unref (buf);
1759     return FALSE;
1760   }
1761 empty_index:
1762   {
1763     GST_DEBUG_OBJECT (avi, "the index is empty");
1764     gst_buffer_unref (buf);
1765     return TRUE;
1766   }
1767 out_of_mem:
1768   {
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);
1774     return FALSE;
1775   }
1776 }
1777
1778 /*
1779  * Create and push a flushing seek event upstream
1780  */
1781 static gboolean
1782 perform_seek_to_offset (GstAviDemux * demux, guint64 offset)
1783 {
1784   GstEvent *event;
1785   gboolean res = 0;
1786
1787   GST_DEBUG_OBJECT (demux, "Seeking to %" G_GUINT64_FORMAT, offset);
1788
1789   event =
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);
1793
1794   res = gst_pad_push_event (demux->sinkpad, event);
1795
1796   if (res)
1797     demux->offset = offset;
1798   return res;
1799 }
1800
1801 /*
1802  * Read AVI index when streaming
1803  */
1804 static gboolean
1805 gst_avi_demux_read_subindexes_push (GstAviDemux * avi)
1806 {
1807   guint32 tag = 0, size;
1808   GstBuffer *buf = NULL;
1809   guint odml_stream;
1810
1811   GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1812
1813   if (avi->odml_subidxs[avi->odml_subidx] != avi->offset)
1814     return FALSE;
1815
1816   if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
1817     return TRUE;
1818
1819   /* this is the ODML chunk we expect */
1820   odml_stream = avi->odml_stream;
1821
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));
1828     return FALSE;
1829   }
1830
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);
1835
1836   if (!gst_avi_demux_parse_subindex (avi, &avi->stream[odml_stream], buf))
1837     return FALSE;
1838
1839   /* we parsed the index, go to next subindex */
1840   avi->odml_subidx++;
1841
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 */
1845     avi->odml_stream++;
1846     avi->odml_subidx = 0;
1847
1848     if (avi->odml_stream < avi->num_streams) {
1849       /* there are more indexes */
1850       avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
1851     } else {
1852       /* we're done, get stream stats now */
1853       avi->have_index = gst_avi_demux_do_index_stats (avi);
1854
1855       return TRUE;
1856     }
1857   }
1858
1859   /* seek to next index */
1860   return perform_seek_to_offset (avi, avi->odml_subidxs[avi->odml_subidx]);
1861 }
1862
1863 /*
1864  * Read AVI index
1865  */
1866 static void
1867 gst_avi_demux_read_subindexes_pull (GstAviDemux * avi)
1868 {
1869   guint32 tag;
1870   GstBuffer *buf;
1871   gint i, n;
1872
1873   GST_DEBUG_OBJECT (avi, "read subindexes for %d streams", avi->num_streams);
1874
1875   for (n = 0; n < avi->num_streams; n++) {
1876     GstAviStream *stream = &avi->stream[n];
1877
1878     if (stream->indexes == NULL)
1879       continue;
1880
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)
1884         continue;
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);
1894         continue;
1895       }
1896
1897       if (!gst_avi_demux_parse_subindex (avi, stream, buf))
1898         continue;
1899     }
1900
1901     g_free (stream->indexes);
1902     stream->indexes = NULL;
1903   }
1904   /* get stream stats now */
1905   avi->have_index = gst_avi_demux_do_index_stats (avi);
1906 }
1907
1908 /*
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.
1914  *
1915  * Parses a video stream´s vprp. This function takes ownership of @buf.
1916  *
1917  * Returns: TRUE if parsing succeeded, otherwise FALSE. The stream
1918  *          should be skipped on error, but it is not fatal.
1919  */
1920 static gboolean
1921 gst_avi_demux_riff_parse_vprp (GstElement * element,
1922     GstBuffer * buf, gst_riff_vprp ** _vprp)
1923 {
1924   gst_riff_vprp *vprp;
1925   gint k;
1926
1927   g_return_val_if_fail (buf != NULL, FALSE);
1928   g_return_val_if_fail (_vprp != NULL, FALSE);
1929
1930   if (GST_BUFFER_SIZE (buf) < G_STRUCT_OFFSET (gst_riff_vprp, field_info))
1931     goto too_small;
1932
1933   vprp = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1934
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);
1945 #endif
1946
1947   /* size checking */
1948   /* calculate fields based on size */
1949   k = (GST_BUFFER_SIZE (buf) - G_STRUCT_OFFSET (gst_riff_vprp, field_info)) /
1950       vprp->fields;
1951   if (vprp->fields > k) {
1952     GST_WARNING_OBJECT (element,
1953         "vprp header indicated %d fields, only %d available", vprp->fields, k);
1954     vprp->fields = k;
1955   }
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;
1961   }
1962 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
1963   for (k = 0; k < vprp->fields; k++) {
1964     gst_riff_vprp_video_field_desc *fd;
1965
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);
1975   }
1976 #endif
1977
1978   /* debug */
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;
1992
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);
2009   }
2010
2011   gst_buffer_unref (buf);
2012
2013   *_vprp = vprp;
2014
2015   return TRUE;
2016
2017   /* ERRORS */
2018 too_small:
2019   {
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);
2025     return FALSE;
2026   }
2027 }
2028
2029 static void
2030 gst_avi_demux_expose_streams (GstAviDemux * avi, gboolean force)
2031 {
2032   guint i;
2033
2034   GST_DEBUG_OBJECT (avi, "force : %d", force);
2035
2036   for (i = 0; i < avi->num_streams; i++) {
2037     GstAviStream *stream = &avi->stream[i];
2038
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);
2043
2044       if (avi->element_index)
2045         gst_index_get_writer_id (avi->element_index,
2046             GST_OBJECT_CAST (stream->pad), &stream->index_id);
2047
2048       stream->exposed = TRUE;
2049       if (avi->main_stream == -1)
2050         avi->main_stream = i;
2051     } else {
2052       GST_WARNING_OBJECT (avi, "Stream #%d doesn't have any entry, removing it",
2053           i);
2054       gst_avi_demux_reset_stream (avi, stream);
2055     }
2056   }
2057 }
2058
2059
2060 #ifdef DIVX_DRM
2061 typedef struct _gst_riff_strd {
2062   guint32 version;
2063   guint32 drm_size;
2064 } gst_riff_strd;
2065
2066
2067 static gboolean
2068 gst_riff_parse_strd (GstAviDemux * avi,
2069     GstBuffer * buf)
2070 {
2071   g_return_val_if_fail (buf != NULL, FALSE);
2072
2073   if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_strd))
2074     goto too_small;
2075
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);
2078
2079   return  gst_avi_demux_init_divx_drm (avi, GST_BUFFER_DATA(buf)+sizeof(gst_riff_strd));
2080
2081   /* ERRORS */
2082 too_small:
2083   {
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);
2088     return FALSE;
2089   }
2090 }
2091 #endif // DIVX_DRM
2092
2093
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 */
2097 static inline void
2098 gst_avi_demux_roundup_list (GstAviDemux * avi, GstBuffer ** buf)
2099 {
2100   if (G_UNLIKELY (GST_BUFFER_SIZE (*buf) & 1)) {
2101     GstBuffer *obuf;
2102
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);
2109   }
2110 }
2111
2112 /*
2113  * gst_avi_demux_parse_stream:
2114  * @avi: calling element (used for debugging/errors).
2115  * @buf: input buffer used to parse the stream.
2116  *
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.
2120  *
2121  * Returns: whether the stream was identified successfully.
2122  *          Errors are not fatal. It does indicate the stream
2123  *          was skipped.
2124  */
2125 static gboolean
2126 gst_avi_demux_parse_stream (GstAviDemux * avi, GstBuffer * buf)
2127 {
2128   GstAviStream *stream;
2129   GstElementClass *klass;
2130   GstPadTemplate *templ = NULL;
2131   GstBuffer *sub = NULL;
2132   guint offset = 4;
2133   guint32 tag = 0;
2134   gchar *codec_name = NULL, *padname = NULL;
2135   const gchar *tag_name = NULL;
2136   GstCaps *caps = NULL;
2137   GstPad *pad;
2138   GstElement *element;
2139   gboolean got_strh = FALSE, got_strf = FALSE, got_vprp = FALSE;
2140   gst_riff_vprp *vprp = NULL;
2141
2142   element = GST_ELEMENT_CAST (avi);
2143
2144   GST_DEBUG_OBJECT (avi, "Parsing stream");
2145
2146   gst_avi_demux_roundup_list (avi, &buf);
2147
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 */
2154     return TRUE;
2155   }
2156
2157   stream = &avi->stream[avi->num_streams];
2158
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;
2163
2164   while (gst_riff_parse_chunk (element, buf, &offset, &tag, &sub)) {
2165     /* sub can be NULL if the chunk is empty */
2166     if (sub == NULL) {
2167       GST_DEBUG_OBJECT (avi, "ignoring empty chunk %" GST_FOURCC_FORMAT,
2168           GST_FOURCC_ARGS (tag));
2169       continue;
2170     }
2171     switch (tag) {
2172       case GST_RIFF_TAG_strh:
2173       {
2174         gst_riff_strh *strh;
2175
2176         if (got_strh) {
2177           GST_WARNING_OBJECT (avi, "Ignoring additional strh chunk");
2178           break;
2179         }
2180         if (!gst_riff_parse_strh (element, sub, &stream->strh)) {
2181           /* ownership given away */
2182           sub = NULL;
2183           GST_WARNING_OBJECT (avi, "Failed to parse strh chunk");
2184           goto fail;
2185         }
2186         sub = NULL;
2187         strh = stream->strh;
2188         /* sanity check; stream header frame rate matches global header
2189          * frame duration */
2190         if (stream->strh->type == GST_RIFF_FCC_vids) {
2191           GstClockTime s_dur;
2192           GstClockTime h_dur = avi->avih->us_frame * GST_USECOND;
2193
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);
2203           }
2204         }
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;
2212
2213         got_strh = TRUE;
2214         break;
2215       }
2216       case GST_RIFF_TAG_strf:
2217       {
2218         gboolean res = FALSE;
2219
2220         if (got_strf) {
2221           GST_WARNING_OBJECT (avi, "Ignoring additional strf chunk");
2222           break;
2223         }
2224         if (!got_strh) {
2225           GST_ERROR_OBJECT (avi, "Found strf chunk before strh chunk");
2226           goto fail;
2227         }
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);
2233             sub = NULL;
2234             GST_DEBUG_OBJECT (element, "marking video as VBR, res %d", res);
2235             break;
2236           case GST_RIFF_FCC_auds:
2237             res =
2238                 gst_riff_parse_strf_auds (element, sub, &stream->strf.auds,
2239                 &stream->extradata);
2240             sub = NULL;
2241             if (!res)
2242               break;
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");
2254               goto fail;
2255             }
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);
2267               }
2268             }
2269             break;
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);
2274             sub = NULL;
2275             GST_DEBUG_OBJECT (element, "marking iavs as VBR, res %d", res);
2276             break;
2277           case GST_RIFF_FCC_txts:
2278             /* nothing to parse here */
2279             stream->is_vbr = (stream->strh->samplesize == 0)
2280                 && (stream->strh->scale > 1);
2281             res = TRUE;
2282             break;
2283           default:
2284             GST_ERROR_OBJECT (avi,
2285                 "Don´t know how to handle stream type %" GST_FOURCC_FORMAT,
2286                 GST_FOURCC_ARGS (stream->strh->type));
2287             break;
2288         }
2289         if (sub) {
2290           gst_buffer_unref (sub);
2291           sub = NULL;
2292         }
2293         if (!res)
2294           goto fail;
2295         got_strf = TRUE;
2296         break;
2297       }
2298       case GST_RIFF_TAG_vprp:
2299       {
2300         if (got_vprp) {
2301           GST_WARNING_OBJECT (avi, "Ignoring additional vprp chunk");
2302           break;
2303         }
2304         if (!got_strh) {
2305           GST_ERROR_OBJECT (avi, "Found vprp chunk before strh chunk");
2306           goto fail;
2307         }
2308         if (!got_strf) {
2309           GST_ERROR_OBJECT (avi, "Found vprp chunk before strf chunk");
2310           goto fail;
2311         }
2312
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 */
2316           g_free (vprp);
2317           vprp = NULL;
2318         } else
2319           got_vprp = TRUE;
2320         sub = NULL;
2321         break;
2322       }
2323       case GST_RIFF_TAG_strd:
2324 #ifdef DIVX_DRM
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")        );
2330                   return FALSE;
2331           }
2332 #endif
2333
2334         if (stream->initdata)
2335           gst_buffer_unref (stream->initdata);
2336         stream->initdata = sub;
2337         sub = NULL;
2338         break;
2339       case GST_RIFF_TAG_strn:
2340         g_free (stream->name);
2341         if (sub != NULL) {
2342           stream->name =
2343               g_strndup ((gchar *) GST_BUFFER_DATA (sub),
2344               (gsize) GST_BUFFER_SIZE (sub));
2345           gst_buffer_unref (sub);
2346           sub = NULL;
2347         } else {
2348           stream->name = g_strdup ("");
2349         }
2350         GST_DEBUG_OBJECT (avi, "stream name: %s", stream->name);
2351         break;
2352       case GST_RIFF_IDIT:
2353         gst_avi_demux_parse_idit (avi, sub);
2354         break;
2355       default:
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;
2362           sub = NULL;
2363           break;
2364         }
2365         GST_WARNING_OBJECT (avi,
2366             "Unknown stream header tag %" GST_FOURCC_FORMAT ", ignoring",
2367             GST_FOURCC_ARGS (tag));
2368         /* fall-through */
2369       case GST_RIFF_TAG_JUNQ:
2370       case GST_RIFF_TAG_JUNK:
2371         break;
2372     }
2373     if (sub != NULL) {
2374       gst_buffer_unref (sub);
2375       sub = NULL;
2376     }
2377   }
2378
2379   if (!got_strh) {
2380     GST_WARNING_OBJECT (avi, "Failed to find strh chunk");
2381     goto fail;
2382   }
2383
2384   if (!got_strf) {
2385     GST_WARNING_OBJECT (avi, "Failed to find strf chunk");
2386     goto fail;
2387   }
2388
2389   /* get class to figure out the template */
2390   klass = GST_ELEMENT_GET_CLASS (avi);
2391
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:{
2396       guint32 fourcc;
2397
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);
2404       if (!caps) {
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;
2409         gint n, d;
2410
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;
2416         if (n && d)
2417           gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
2418               n, d, NULL);
2419         /* very local, not needed elsewhere */
2420         g_free (vprp);
2421         vprp = NULL;
2422       }
2423       tag_name = GST_TAG_VIDEO_CODEC;
2424       avi->num_v_streams++;
2425       break;
2426     }
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);
2433       if (!caps) {
2434         caps = gst_caps_new_simple ("audio/x-avi-unknown", "codec_id",
2435             G_TYPE_INT, stream->strf.auds->format, NULL);
2436       }
2437       tag_name = GST_TAG_AUDIO_CODEC;
2438       avi->num_a_streams++;
2439       break;
2440     }
2441     case GST_RIFF_FCC_iavs:{
2442       guint32 fourcc = stream->strh->fcc_handler;
2443
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);
2448       if (!caps) {
2449         caps = gst_caps_new_simple ("video/x-avi-unknown", "fourcc",
2450             GST_TYPE_FOURCC, fourcc, NULL);
2451       }
2452       tag_name = GST_TAG_VIDEO_CODEC;
2453       avi->num_v_streams++;
2454       break;
2455     }
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);
2460       tag_name = NULL;
2461       avi->num_t_streams++;
2462       break;
2463     }
2464     default:
2465       g_assert_not_reached ();
2466   }
2467
2468   /* no caps means no stream */
2469   if (!caps) {
2470     GST_ERROR_OBJECT (element, "Did not find caps for stream %s", padname);
2471     goto fail;
2472   }
2473
2474   GST_DEBUG_OBJECT (element, "codec-name=%s",
2475       (codec_name ? codec_name : "NULL"));
2476   GST_DEBUG_OBJECT (element, "caps=%" GST_PTR_FORMAT, caps);
2477
2478   /* set proper settings and add it */
2479   if (stream->pad)
2480     gst_object_unref (stream->pad);
2481   pad = stream->pad = gst_pad_new_from_template (templ, padname);
2482   g_free (padname);
2483
2484   gst_pad_use_fixed_caps (pad);
2485 #if 0
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));
2490 #endif
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));
2497 #if 0
2498   gst_pad_set_convert_function (pad,
2499       GST_DEBUG_FUNCPTR (gst_avi_demux_src_convert));
2500 #endif
2501
2502   if (avi->element_index)
2503     gst_index_get_writer_id (avi->element_index, GST_OBJECT_CAST (stream->pad),
2504         &stream->index_id);
2505
2506   stream->num = avi->num_streams;
2507
2508   stream->start_entry = 0;
2509   stream->step_entry = 0;
2510   stream->stop_entry = 0;
2511
2512   stream->current_entry = -1;
2513   stream->current_total = 0;
2514
2515   stream->last_flow = GST_FLOW_OK;
2516   stream->discont = TRUE;
2517
2518   stream->total_bytes = 0;
2519   stream->total_blocks = 0;
2520   stream->n_keyframes = 0;
2521
2522   stream->idx_n = 0;
2523   stream->idx_max = 0;
2524
2525   gst_pad_set_element_private (pad, stream);
2526   avi->num_streams++;
2527
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));
2532
2533 #if 1
2534   {
2535           GstStructure *tempstruct;
2536           GstStructure *structure;
2537           
2538           tempstruct = gst_caps_get_structure(caps, 0);
2539
2540           if(!strcmp(gst_structure_get_name (tempstruct), "audio/x-ac3"))
2541           {
2542
2543               structure = gst_structure_copy(tempstruct);
2544
2545               gst_structure_set(structure, "framed", G_TYPE_BOOLEAN, TRUE, NULL);
2546
2547               gst_caps_remove_structure(caps, 0);
2548
2549               gst_caps_merge_structure(caps, structure);
2550
2551           }
2552           else if(!strcmp(gst_structure_get_name (tempstruct), "audio/mpeg"))
2553           {
2554
2555               structure = gst_structure_copy(tempstruct);
2556
2557               gst_structure_set(structure, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
2558
2559               gst_caps_remove_structure(caps, 0);
2560
2561               gst_caps_merge_structure(caps, structure);
2562
2563           }
2564
2565   }
2566
2567 #endif
2568
2569 #endif
2570   gst_pad_set_caps (pad, caps);
2571   gst_pad_set_active (pad, TRUE);
2572   gst_caps_unref (caps);
2573
2574   /* make tags */
2575   if (codec_name) {
2576     if (!stream->taglist)
2577       stream->taglist = gst_tag_list_new ();
2578
2579     avi->got_tags = TRUE;
2580
2581     gst_tag_list_add (stream->taglist, GST_TAG_MERGE_APPEND, tag_name,
2582         codec_name, NULL);
2583     g_free (codec_name);
2584   }
2585
2586   gst_buffer_unref (buf);
2587
2588   return TRUE;
2589
2590   /* ERRORS */
2591 fail:
2592   {
2593     /* unref any mem that may be in use */
2594     if (buf)
2595       gst_buffer_unref (buf);
2596     if (sub)
2597       gst_buffer_unref (sub);
2598     g_free (vprp);
2599     g_free (codec_name);
2600     gst_avi_demux_reset_stream (avi, stream);
2601     avi->num_streams++;
2602     return FALSE;
2603   }
2604 }
2605
2606 /*
2607  * gst_avi_demux_parse_odml:
2608  * @avi: calling element (used for debug/error).
2609  * @buf: input buffer to be used for parsing.
2610  *
2611  * Read an openDML-2.0 extension header. Fills in the frame number
2612  * in the avi demuxer object when reading succeeds.
2613  */
2614 static void
2615 gst_avi_demux_parse_odml (GstAviDemux * avi, GstBuffer * buf)
2616 {
2617   guint32 tag = 0;
2618   guint offset = 4;
2619   GstBuffer *sub = NULL;
2620
2621   while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
2622           &sub)) {
2623     switch (tag) {
2624       case GST_RIFF_TAG_dmlh:{
2625         gst_riff_dmlh dmlh, *_dmlh;
2626         guint size;
2627
2628         /* sub == NULL is possible and means an empty buffer */
2629         size = sub ? GST_BUFFER_SIZE (sub) : 0;
2630
2631         /* check size */
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));
2636           goto next;
2637         }
2638         _dmlh = (gst_riff_dmlh *) GST_BUFFER_DATA (sub);
2639         dmlh.totalframes = GST_READ_UINT32_LE (&_dmlh->totalframes);
2640
2641         GST_INFO_OBJECT (avi, "dmlh tag found: totalframes: %u",
2642             dmlh.totalframes);
2643
2644         avi->avih->tot_frames = dmlh.totalframes;
2645         goto next;
2646       }
2647
2648       default:
2649         GST_WARNING_OBJECT (avi,
2650             "Unknown tag %" GST_FOURCC_FORMAT " in ODML header",
2651             GST_FOURCC_ARGS (tag));
2652         /* fall-through */
2653       case GST_RIFF_TAG_JUNQ:
2654       case GST_RIFF_TAG_JUNK:
2655       next:
2656         /* skip and move to next chunk */
2657         if (sub) {
2658           gst_buffer_unref (sub);
2659           sub = NULL;
2660         }
2661         break;
2662     }
2663   }
2664   if (buf)
2665     gst_buffer_unref (buf);
2666 }
2667
2668 /* Index helper */
2669 static guint
2670 gst_avi_demux_index_last (GstAviDemux * avi, GstAviStream * stream)
2671 {
2672   return stream->idx_n;
2673 }
2674
2675 /* find a previous entry in the index with the given flags */
2676 static guint
2677 gst_avi_demux_index_prev (GstAviDemux * avi, GstAviStream * stream,
2678     guint last, gboolean keyframe)
2679 {
2680   GstAviIndexEntry *entry;
2681   guint i;
2682
2683   for (i = last; i > 0; i--) {
2684     entry = &stream->index[i - 1];
2685     if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2686       return i - 1;
2687     }
2688   }
2689   return 0;
2690 }
2691
2692 static guint
2693 gst_avi_demux_index_next (GstAviDemux * avi, GstAviStream * stream,
2694     guint last, gboolean keyframe)
2695 {
2696   GstAviIndexEntry *entry;
2697   gint i;
2698
2699   for (i = last + 1; i < stream->idx_n; i++) {
2700     entry = &stream->index[i];
2701     if (!keyframe || ENTRY_IS_KEYFRAME (entry)) {
2702       return i;
2703     }
2704   }
2705   return stream->idx_n - 1;
2706 }
2707
2708 static guint
2709 gst_avi_demux_index_entry_search (GstAviIndexEntry * entry, guint64 * total)
2710 {
2711   if (entry->total < *total)
2712     return -1;
2713   else if (entry->total > *total)
2714     return 1;
2715   return 0;
2716 }
2717
2718 /*
2719  * gst_avi_demux_index_for_time:
2720  * @avi: Avi object
2721  * @stream: the stream
2722  * @time: a time position
2723  *
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).
2727  *
2728  * Returns: the found position in the index.
2729  */
2730 static guint
2731 gst_avi_demux_index_for_time (GstAviDemux * avi,
2732     GstAviStream * stream, guint64 time)
2733 {
2734   guint index = -1;
2735   guint64 total;
2736
2737   GST_LOG_OBJECT (avi, "search time:%" GST_TIME_FORMAT, GST_TIME_ARGS (time));
2738
2739   /* easy (and common) cases */
2740   if (time == 0 || stream->idx_n == 0)
2741     return 0;
2742   if (time >= stream->idx_duration)
2743     return stream->idx_n - 1;
2744
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);
2751     } else {
2752       index = avi_stream_convert_time_to_frames_unchecked (stream, time);
2753     }
2754   } else {
2755     /* constant rate stream */
2756     total = avi_stream_convert_time_to_bytes_unchecked (stream, time);
2757   }
2758
2759   if (index == -1) {
2760     GstAviIndexEntry *entry;
2761
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);
2765
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);
2770
2771     if (entry == NULL) {
2772       GST_LOG_OBJECT (avi, "not found, assume index 0");
2773       index = 0;
2774     } else {
2775       index = entry - stream->index;
2776       GST_LOG_OBJECT (avi, "found at %u", index);
2777     }
2778   } else {
2779     GST_LOG_OBJECT (avi, "converted time to index %u", index);
2780   }
2781
2782   return index;
2783 }
2784
2785 static inline GstAviStream *
2786 gst_avi_demux_stream_for_id (GstAviDemux * avi, guint32 id)
2787 {
2788   guint stream_nr;
2789   GstAviStream *stream;
2790
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);
2795     return NULL;
2796   }
2797   stream = &avi->stream[stream_nr];
2798   if (G_UNLIKELY (!stream->strh)) {
2799     GST_WARNING_OBJECT (avi, "Unhandled stream %d, skipping", stream_nr);
2800     return NULL;
2801   }
2802   return stream;
2803 }
2804
2805 /*
2806  * gst_avi_demux_parse_index:
2807  * @avi: calling element (used for debugging/errors).
2808  * @buf: buffer containing the full index.
2809  *
2810  * Read index entries from the provided buffer.
2811  * The buffer should contain a GST_RIFF_TAG_idx1 chunk.
2812  */
2813 static gboolean
2814 gst_avi_demux_parse_index (GstAviDemux * avi, GstBuffer * buf)
2815 {
2816   guint64 pos_before;
2817   guint8 *data;
2818   guint size;
2819   guint i, num, n;
2820   gst_riff_index_entry *index;
2821   GstClockTime stamp;
2822   GstAviStream *stream;
2823   GstAviIndexEntry entry;
2824   guint32 id;
2825
2826   if (!buf)
2827     return FALSE;
2828
2829   data = GST_BUFFER_DATA (buf);
2830   size = GST_BUFFER_SIZE (buf);
2831
2832   stamp = gst_util_get_timestamp ();
2833
2834   /* see how many items in the index */
2835   num = size / sizeof (gst_riff_index_entry);
2836   if (num == 0)
2837     goto empty_list;
2838
2839   GST_INFO_OBJECT (avi, "Parsing index, nr_entries = %6d", num);
2840
2841   index = (gst_riff_index_entry *) data;
2842   pos_before = avi->offset;
2843
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);
2849   } else {
2850     avi->index_offset = 0;
2851     GST_DEBUG ("index is 0 based");
2852   }
2853
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);
2857
2858     /* some sanity checks */
2859     if (G_UNLIKELY (id == GST_RIFF_rec || id == 0 ||
2860             (entry.offset == 0 && n > 0)))
2861       continue;
2862
2863         if ( id == GST_MAKE_FOURCC('0','0','d','d')  )
2864         {
2865                 GST_DEBUG("Skipping Encrypt data chunk");
2866                 continue;
2867         }
2868
2869
2870     /* get the stream for this entry */
2871     stream = gst_avi_demux_stream_for_id (avi, id);
2872     if (G_UNLIKELY (!stream))
2873       continue;
2874
2875     /* handle offset and size */
2876     entry.offset += avi->index_offset + 8;
2877     entry.size = GST_READ_UINT32_LE (&index[i].size);
2878
2879     /* handle flags */
2880     if (stream->strh->type == GST_RIFF_FCC_auds) {
2881       /* all audio frames are keyframes */
2882       ENTRY_SET_KEYFRAME (&entry);
2883     } else {
2884       guint32 flags;
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);
2889       } else {
2890         ENTRY_UNSET_KEYFRAME (&entry);
2891       }
2892     }
2893
2894     /* and add */
2895     if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
2896       goto out_of_mem;
2897
2898     n++;
2899   }
2900   gst_buffer_unref (buf);
2901
2902   /* get stream stats now */
2903   avi->have_index = gst_avi_demux_do_index_stats (avi);
2904
2905   stamp = gst_util_get_timestamp () - stamp;
2906   GST_DEBUG_OBJECT (avi, "index parsing took %" GST_TIME_FORMAT,
2907       GST_TIME_ARGS (stamp));
2908
2909   return TRUE;
2910
2911   /* ERRORS */
2912 empty_list:
2913   {
2914     GST_DEBUG_OBJECT (avi, "empty index");
2915     gst_buffer_unref (buf);
2916     return FALSE;
2917   }
2918 out_of_mem:
2919   {
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);
2925     return FALSE;
2926   }
2927 }
2928
2929 /*
2930  * gst_avi_demux_stream_index:
2931  * @avi: avi demuxer object.
2932  *
2933  * Seeks to index and reads it.
2934  */
2935 static void
2936 gst_avi_demux_stream_index (GstAviDemux * avi)
2937 {
2938   GstFlowReturn res;
2939   guint64 offset = avi->offset;
2940   GstBuffer *buf;
2941   guint32 tag;
2942   guint32 size;
2943
2944   GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
2945
2946   /* get chunk information */
2947   res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
2948   if (res != GST_FLOW_OK)
2949     goto pull_failed;
2950   else if (GST_BUFFER_SIZE (buf) < 8)
2951     goto too_small;
2952
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)
2964       goto pull_failed;
2965     else if (GST_BUFFER_SIZE (buf) < 8)
2966       goto too_small;
2967     tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
2968     size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
2969   }
2970
2971   if (tag != GST_RIFF_TAG_idx1)
2972     goto no_index;
2973   if (!size)
2974     goto zero_index;
2975
2976   gst_buffer_unref (buf);
2977
2978   GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
2979
2980   /* read chunk, advance offset */
2981   if (gst_riff_read_chunk (GST_ELEMENT_CAST (avi),
2982           avi->sinkpad, &offset, &tag, &buf) != GST_FLOW_OK)
2983     return;
2984
2985   GST_DEBUG ("will parse index chunk size %u for tag %"
2986       GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
2987
2988   gst_avi_demux_parse_index (avi, buf);
2989
2990 #ifndef GST_DISABLE_GST_DEBUG
2991   /* debug our indexes */
2992   {
2993     gint i;
2994     GstAviStream *stream;
2995
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);
3000     }
3001   }
3002 #endif
3003   return;
3004
3005   /* ERRORS */
3006 pull_failed:
3007   {
3008     GST_DEBUG_OBJECT (avi,
3009         "pull range failed: pos=%" G_GUINT64_FORMAT " size=8", offset);
3010     return;
3011   }
3012 too_small:
3013   {
3014     GST_DEBUG_OBJECT (avi, "Buffer is too small");
3015     gst_buffer_unref (buf);
3016     return;
3017   }
3018 no_index:
3019   {
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);
3024     return;
3025   }
3026 zero_index:
3027   {
3028     GST_WARNING_OBJECT (avi, "Empty index data (idx1) after movi chunk");
3029     gst_buffer_unref (buf);
3030     return;
3031   }
3032 }
3033
3034 /*
3035  * gst_avi_demux_stream_index_push:
3036  * @avi: avi demuxer object.
3037  *
3038  * Read index.
3039  */
3040 static void
3041 gst_avi_demux_stream_index_push (GstAviDemux * avi)
3042 {
3043   guint64 offset = avi->idx1_offset;
3044   GstBuffer *buf;
3045   guint32 tag;
3046   guint32 size;
3047
3048   GST_DEBUG ("demux stream index at offset %" G_GUINT64_FORMAT, offset);
3049
3050   /* get chunk information */
3051   if (!gst_avi_demux_peek_chunk (avi, &tag, &size))
3052     return;
3053
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);
3062     return;
3063   }
3064
3065   if (tag != GST_RIFF_TAG_idx1)
3066     goto no_index;
3067
3068   GST_DEBUG ("index found at offset %" G_GUINT64_FORMAT, offset);
3069
3070   /* flush chunk header */
3071   gst_adapter_flush (avi->adapter, 8);
3072   /* read chunk payload */
3073   buf = gst_adapter_take_buffer (avi->adapter, size);
3074   if (!buf)
3075     goto pull_failed;
3076   /* advance offset */
3077   offset += 8 + GST_ROUND_UP_2 (size);
3078
3079   GST_DEBUG ("will parse index chunk size %u for tag %"
3080       GST_FOURCC_FORMAT, GST_BUFFER_SIZE (buf), GST_FOURCC_ARGS (tag));
3081
3082   avi->offset = avi->first_movi_offset;
3083   gst_avi_demux_parse_index (avi, buf);
3084
3085 #ifndef GST_DISABLE_GST_DEBUG
3086   /* debug our indexes */
3087   {
3088     gint i;
3089     GstAviStream *stream;
3090
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);
3095     }
3096   }
3097 #endif
3098   return;
3099
3100   /* ERRORS */
3101 pull_failed:
3102   {
3103     GST_DEBUG_OBJECT (avi,
3104         "taking data from adapter failed: pos=%" G_GUINT64_FORMAT " size=%u",
3105         offset, size);
3106     return;
3107   }
3108 no_index:
3109   {
3110     GST_WARNING_OBJECT (avi,
3111         "No index data (idx1) after movi chunk, but %" GST_FOURCC_FORMAT,
3112         GST_FOURCC_ARGS (tag));
3113     return;
3114   }
3115 }
3116
3117 /*
3118  * gst_avi_demux_peek_tag:
3119  *
3120  * Returns the tag and size of the next chunk
3121  */
3122 static GstFlowReturn
3123 gst_avi_demux_peek_tag (GstAviDemux * avi, guint64 offset, guint32 * tag,
3124     guint * size)
3125 {
3126   GstFlowReturn res = GST_FLOW_OK;
3127   GstBuffer *buf = NULL;
3128   guint bufsize;
3129   guint8 *bufdata;
3130
3131   res = gst_pad_pull_range (avi->sinkpad, offset, 8, &buf);
3132   if (res != GST_FLOW_OK)
3133     goto pull_failed;
3134
3135   bufsize = GST_BUFFER_SIZE (buf);
3136   if (bufsize != 8)
3137     goto wrong_size;
3138
3139   bufdata = GST_BUFFER_DATA (buf);
3140
3141   *tag = GST_READ_UINT32_LE (bufdata);
3142   *size = GST_READ_UINT32_LE (bufdata + 4);
3143
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);
3147
3148 done:
3149   gst_buffer_unref (buf);
3150
3151   return res;
3152
3153   /* ERRORS */
3154 pull_failed:
3155   {
3156     GST_DEBUG_OBJECT (avi, "pull_ranged returned %s", gst_flow_get_name (res));
3157     return res;
3158   }
3159 wrong_size:
3160   {
3161     GST_DEBUG_OBJECT (avi, "got %d bytes which is <> 8 bytes", bufsize);
3162     res = GST_FLOW_ERROR;
3163     goto done;
3164   }
3165 }
3166
3167 /*
3168  * gst_avi_demux_next_data_buffer:
3169  *
3170  * Returns the offset and size of the next buffer
3171  * Position is the position of the buffer (after tag and size)
3172  */
3173 static GstFlowReturn
3174 gst_avi_demux_next_data_buffer (GstAviDemux * avi, guint64 * offset,
3175     guint32 * tag, guint * size)
3176 {
3177   guint64 off = *offset;
3178   guint _size = 0;
3179   GstFlowReturn res;
3180
3181   do {
3182     res = gst_avi_demux_peek_tag (avi, off, tag, &_size);
3183     if (res != GST_FLOW_OK)
3184       break;
3185     if (*tag == GST_RIFF_TAG_LIST || *tag == GST_RIFF_TAG_RIFF)
3186       off += 8 + 4;             /* skip tag + size + subtag */
3187     else {
3188       *offset = off + 8;
3189       *size = _size;
3190       break;
3191     }
3192   } while (TRUE);
3193
3194   return res;
3195 }
3196
3197 #ifdef AVIDEMUX_MODIFICATION
3198 /*Modification: Added function to find out the frame_type for index-table generation */
3199
3200 static int
3201 gst_avi_demux_find_frame_type (GstAviStream *stream, GstBuffer *buf, int *frame_type)
3202 {
3203     unsigned char *buff = GST_BUFFER_DATA (buf);
3204     unsigned int buff_len = GST_BUFFER_SIZE (buf);
3205
3206     switch (stream->strh->fcc_handler)  
3207     {
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', ' '):
3227             {
3228                 int idx = 0;
3229                 for (idx=0; idx<(buff_len-3); idx++)
3230                 {
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)
3234                         break;
3235                 }
3236             
3237                 if (idx==(buff_len))
3238                 {
3239                     GST_ERROR ("Invalid input stream : There isn't any VOP header");
3240                     return -1;
3241                 }
3242             
3243                 if ((buff[idx] == 0x00) && (buff[idx+1] == 0x00) && (buff[idx+2] == 0x01))
3244                 {
3245                     if(buff[idx+3] == 0xB6)
3246                     {
3247                         switch (buff[4] & 0xC0)
3248                         {
3249                             case 0x00:
3250                                 GST_DEBUG ("Found Key-Frame");
3251                                   *frame_type = GST_AVI_KEYFRAME;
3252                                 break;
3253                             default:
3254                                 GST_DEBUG ("Found Non-Key frame.. value = %x", buff[4]);
3255                                 *frame_type = GST_AVI_NON_KEYFRAME;
3256                                 break;        
3257                         }
3258                     }
3259                     else if (buff[3] == 0xB0)
3260                     {
3261                         *frame_type = GST_AVI_KEYFRAME;
3262                     }
3263                 }
3264             }
3265             break;
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'):
3272             {
3273                 /* TODO:  H263 Frame Parsing */
3274                 *frame_type = GST_AVI_KEYFRAME;
3275             }
3276             break;
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'):
3283             {
3284                 gint idx = 0;
3285                 gint nalu_type = H264_NUT_UNKNOWN;
3286                 /* H264 Frame Parsing */
3287                 do
3288                 {
3289                     if (buff[idx+0] == 0x00 &&
3290                         buff[idx+1] == 0x00 &&
3291                      ((buff [idx+2] == 0x01) || ((buff [idx+2] == 0x00) && (buff [idx+3] == 0x01))))
3292                     {
3293                         if (buff [idx+2] == 0x01)
3294                         {
3295                             nalu_type = buff[idx +3] & 0x1f;
3296                         }
3297                         else if ((buff [idx+2] == 0x00) && (buff [idx+3] == 0x01))
3298                         {
3299                             nalu_type = buff[idx +4] & 0x1f;
3300                         }
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))
3305                         {
3306                               GST_DEBUG ("Skipping NALU SPS/PPS/SEI/AUD...");
3307                         }
3308                         else if (nalu_type == H264_NUT_IDR)
3309                            {
3310                               GST_DEBUG ("Found KEY frame...\n");
3311                               *frame_type = GST_AVI_KEYFRAME;
3312                               break;
3313                         }
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))
3320                         {
3321                               *frame_type = GST_AVI_NON_KEYFRAME;
3322                               break;
3323                         }
3324                         else
3325                         {
3326                               GST_DEBUG ("Unknown frame type, val = %d...", *frame_type);
3327                               *frame_type = GST_AVI_NON_KEYFRAME;
3328                               break;
3329                         }
3330                         idx ++;
3331                     }
3332                     else
3333                     {
3334                         idx++;
3335                     }
3336                 }while (idx < (buff_len - 4));
3337             }
3338             break;
3339          default:
3340                 //naveen: default make all frames as key frames
3341                 *frame_type = GST_AVI_KEYFRAME;
3342             break;
3343     }
3344         
3345     return 0;
3346         
3347 }
3348 #endif
3349
3350 /*
3351  * gst_avi_demux_stream_scan:
3352  * @avi: calling element (used for debugging/errors).
3353  *
3354  * Scan the file for all chunks to "create" a new index.
3355  * pull-range based
3356  */
3357 static gboolean
3358 gst_avi_demux_stream_scan (GstAviDemux * avi)
3359 {
3360   GstFlowReturn res;
3361   GstAviStream *stream;
3362   GstFormat format;
3363   guint64 pos = 0;
3364   guint64 length;
3365   gint64 tmplength;
3366   guint32 tag = 0;
3367   guint num;
3368
3369   /* FIXME:
3370    * - implement non-seekable source support.
3371    */
3372   GST_DEBUG_OBJECT (avi, "Creating index");
3373
3374   /* get the size of the file */
3375   format = GST_FORMAT_BYTES;
3376   if (!gst_pad_query_peer_duration (avi->sinkpad, &format, &tmplength))
3377     return FALSE;
3378   length = tmplength;
3379
3380   /* guess the total amount of entries we expect */
3381   num = 16000;
3382
3383   while (TRUE) {
3384     GstAviIndexEntry entry;
3385     guint size = 0;
3386 #ifdef AVIDEMUX_MODIFICATION
3387     gint frame_type = GST_AVI_KEYFRAME;
3388 #endif
3389
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))
3393       break;
3394
3395     /* get stream */
3396     stream = gst_avi_demux_stream_for_id (avi, tag);
3397     if (G_UNLIKELY (!stream))
3398       goto next;
3399 #ifdef AVIDEMUX_MODIFICATION
3400     /*Modification: Added logic to generate the index table with key frames */
3401     
3402     /* naveen: generating index table with key frames */
3403     if (stream->strh->type == GST_RIFF_FCC_vids)
3404     {
3405         GstBuffer *buf = NULL;
3406         int ret = -1;
3407
3408         res = gst_pad_pull_range (avi->sinkpad, pos, size, &buf);
3409         if (res != GST_FLOW_OK)
3410         {
3411             gst_buffer_unref (buf);
3412             GST_ERROR ("[Naveen] Pull failed....\n\n");
3413              break;
3414         }
3415         
3416         ret = gst_avi_demux_find_frame_type (stream, buf, &frame_type);
3417          if (ret == -1)
3418                 break;
3419
3420         gst_buffer_unref (buf);
3421     }
3422         
3423     entry.flags = frame_type;
3424 #endif
3425     entry.offset = pos;
3426     entry.size = size;
3427
3428     /* and add to the index of this stream */
3429     if (G_UNLIKELY (!gst_avi_demux_add_index (avi, stream, num, &entry)))
3430       goto out_of_mem;
3431
3432   next:
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");
3438       break;
3439     }
3440   }
3441
3442   /* collect stats */
3443   avi->have_index = gst_avi_demux_do_index_stats (avi);
3444
3445   return TRUE;
3446
3447   /* ERRORS */
3448 out_of_mem:
3449   {
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));
3454     return FALSE;
3455   }
3456 }
3457
3458 static void
3459 gst_avi_demux_calculate_durations_from_index (GstAviDemux * avi)
3460 {
3461   guint i;
3462   GstClockTime total;
3463   GstAviStream *stream;
3464
3465   total = GST_CLOCK_TIME_NONE;
3466
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;
3471
3472     stream = &avi->stream[i];
3473     if (G_UNLIKELY (!stream || !stream->idx_n || !(strh = stream->strh)))
3474       continue;
3475
3476     /* get header duration for the stream */
3477     hduration = stream->hdr_duration;
3478     /* index duration calculated during parsing */
3479     duration = stream->idx_duration;
3480
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));
3486     } else {
3487       /* fall back to header info to calculate a duration */
3488       duration = hduration;
3489     }
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;
3494
3495     /* find total duration */
3496     if (total == GST_CLOCK_TIME_NONE ||
3497         (GST_CLOCK_TIME_IS_VALID (duration) && duration > total))
3498       total = duration;
3499   }
3500
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];
3505
3506       if (!GST_CLOCK_TIME_IS_VALID (stream->duration)
3507           || stream->duration == 0) {
3508         stream->duration = total;
3509
3510         GST_INFO ("Stream %p duration according to total: %" GST_TIME_FORMAT,
3511             stream, GST_TIME_ARGS (total));
3512       }
3513     }
3514   }
3515
3516   /* and set the total duration in the segment. */
3517   GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
3518       GST_TIME_ARGS (total));
3519
3520   gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
3521 }
3522
3523 #ifdef AVIDEMUX_MODIFICATION
3524 static void
3525 gst_avi_demux_calculate_durations_from_strh (GstAviDemux * avi)
3526 {
3527   guint i;
3528   GstClockTime total;
3529   GstAviStream *stream;
3530
3531   total = GST_CLOCK_TIME_NONE;
3532
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;
3537
3538     stream = &avi->stream[i];
3539
3540     if (G_UNLIKELY (!stream || !(strh = stream->strh)))
3541       continue;
3542
3543     /* get header duration for the stream */
3544     hduration = stream->hdr_duration;
3545
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));
3550     }   
3551         
3552     GST_INFO ("Setting duration of stream #%d to %" GST_TIME_FORMAT,
3553         i, GST_TIME_ARGS (hduration));
3554         
3555     /* set duration for the stream */
3556     stream->duration = hduration;
3557
3558     /* find total duration */
3559     if (total == GST_CLOCK_TIME_NONE ||
3560         (GST_CLOCK_TIME_IS_VALID (hduration) && hduration > total))
3561       total = hduration;
3562   }
3563
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];
3568
3569       if (!GST_CLOCK_TIME_IS_VALID (stream->duration)
3570           || stream->duration == 0) {
3571         stream->duration = total;
3572
3573         GST_INFO ("Stream %p duration according to total: %" GST_TIME_FORMAT,
3574             stream, GST_TIME_ARGS (total));
3575       }
3576     }
3577   }
3578
3579   /* and set the total duration in the segment. */
3580   GST_INFO ("Setting total duration to: %" GST_TIME_FORMAT,
3581       GST_TIME_ARGS (total));
3582
3583   gst_segment_set_duration (&avi->segment, GST_FORMAT_TIME, total);
3584 }
3585 #endif
3586
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. */
3590 static gboolean
3591 gst_avi_demux_push_event (GstAviDemux * avi, GstEvent * event)
3592 {
3593   gboolean result = FALSE;
3594   gint i;
3595
3596   GST_DEBUG_OBJECT (avi, "sending %s event to %d streams",
3597       GST_EVENT_TYPE_NAME (event), avi->num_streams);
3598
3599   for (i = 0; i < avi->num_streams; i++) {
3600     GstAviStream *stream = &avi->stream[i];
3601
3602     if (stream->pad) {
3603       result = TRUE;
3604       gst_pad_push_event (stream->pad, gst_event_ref (event));
3605     }
3606   }
3607   gst_event_unref (event);
3608   return result;
3609 }
3610
3611 static void
3612 gst_avi_demux_check_seekability (GstAviDemux * avi)
3613 {
3614   GstQuery *query;
3615   gboolean seekable = FALSE;
3616   gint64 start = -1, stop = -1;
3617
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");
3621     goto done;
3622   }
3623
3624   gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
3625
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;
3629
3630     GST_DEBUG_OBJECT (avi, "doing duration query to fix up unset stop");
3631     gst_pad_query_peer_duration (avi->sinkpad, &fmt, &stop);
3632   }
3633
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");
3638     seekable = FALSE;
3639   }
3640
3641   if (!avi->element_index) {
3642     GST_DEBUG_OBJECT (avi, "no index");
3643     seekable = FALSE;
3644   }
3645
3646 done:
3647   GST_INFO_OBJECT (avi, "seekable: %d (%" G_GUINT64_FORMAT " - %"
3648       G_GUINT64_FORMAT ")", seekable, start, stop);
3649   avi->seekable = seekable;
3650
3651   gst_query_unref (query);
3652 }
3653
3654 /*
3655  * Read AVI headers when streaming
3656  */
3657 static GstFlowReturn
3658 gst_avi_demux_stream_header_push (GstAviDemux * avi)
3659 {
3660   GstFlowReturn ret = GST_FLOW_OK;
3661   guint32 tag = 0;
3662   guint32 ltag = 0;
3663   guint32 size = 0;
3664   const guint8 *data;
3665   GstBuffer *buf = NULL, *sub = NULL;
3666   guint offset = 4;
3667   gint64 stop;
3668   gint i;
3669   GstTagList *tags = NULL;
3670
3671   GST_DEBUG ("Reading and parsing avi headers: %d", avi->header_state);
3672
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;
3679
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);
3684
3685         if (GST_READ_UINT32_LE (GST_BUFFER_DATA (buf)) != GST_RIFF_LIST_hdrl)
3686           goto header_no_hdrl;
3687
3688         /* mind padding */
3689         if (size & 1)
3690           gst_adapter_flush (avi->adapter, 1);
3691
3692         GST_DEBUG ("'hdrl' LIST tag found. Parsing next chunk");
3693
3694         gst_avi_demux_roundup_list (avi, &buf);
3695
3696         /* the hdrl starts with a 'avih' header */
3697         if (!gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3698                 &sub))
3699           goto header_no_avih;
3700
3701         if (tag != GST_RIFF_TAG_avih)
3702           goto header_no_avih;
3703
3704         if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
3705           goto header_wrong_avih;
3706
3707         GST_DEBUG_OBJECT (avi, "AVI header ok, reading elemnts from header");
3708
3709         /* now, read the elements from the header until the end */
3710         while (gst_riff_parse_chunk (GST_ELEMENT_CAST (avi), buf, &offset, &tag,
3711                 &sub)) {
3712           /* sub can be NULL on empty tags */
3713           if (!sub)
3714             continue;
3715
3716           switch (tag) {
3717             case GST_RIFF_TAG_LIST:
3718               if (GST_BUFFER_SIZE (sub) < 4)
3719                 goto next;
3720
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))) {
3724                     sub = NULL;
3725                     GST_ELEMENT_WARNING (avi, STREAM, DEMUX, (NULL),
3726                         ("failed to parse stream, ignoring"));
3727                     goto next;
3728                   }
3729                   sub = NULL;
3730                   goto next;
3731                 case GST_RIFF_LIST_odml:
3732                   gst_avi_demux_parse_odml (avi, sub);
3733                   sub = NULL;
3734                   break;
3735                 default:
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
3739                               (sub))));
3740                   /* fall-through */
3741                 case GST_RIFF_TAG_JUNQ:
3742                 case GST_RIFF_TAG_JUNK:
3743                   goto next;
3744               }
3745               break;
3746             case GST_RIFF_IDIT:
3747               gst_avi_demux_parse_idit (avi, sub);
3748               goto next;
3749             default:
3750               GST_WARNING_OBJECT (avi,
3751                   "Unknown off %d tag %" GST_FOURCC_FORMAT " in AVI header",
3752                   offset, GST_FOURCC_ARGS (tag));
3753               /* fall-through */
3754             case GST_RIFF_TAG_JUNQ:
3755             case GST_RIFF_TAG_JUNK:
3756             next:
3757               /* move to next chunk */
3758               if (sub)
3759                 gst_buffer_unref (sub);
3760               sub = NULL;
3761               break;
3762           }
3763         }
3764         gst_buffer_unref (buf);
3765         GST_DEBUG ("elements parsed");
3766
3767         /* check parsed streams */
3768         if (avi->num_streams == 0) {
3769           goto no_streams;
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);
3774         }
3775         GST_DEBUG ("Get junk and info next");
3776         avi->header_state = GST_AVI_DEMUX_HEADER_INFO;
3777       } else {
3778         /* Need more data */
3779         return ret;
3780       }
3781       /* fall-though */
3782     case GST_AVI_DEMUX_HEADER_INFO:
3783       GST_DEBUG_OBJECT (avi, "skipping junk between header and data ...");
3784       while (TRUE) {
3785         if (gst_adapter_available (avi->adapter) < 12)
3786           return GST_FLOW_OK;
3787
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);
3792
3793         if (tag == GST_RIFF_TAG_LIST) {
3794           switch (ltag) {
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;
3799               avi->offset += 12;
3800               avi->idx1_offset = avi->offset + size - 4;
3801               goto skipping_done;
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);
3806                 avi->offset += 12;
3807                 gst_adapter_flush (avi->adapter, 12);
3808                 if (size > 4) {
3809                   buf = gst_adapter_take_buffer (avi->adapter, size - 4);
3810                   /* mind padding */
3811                   if (size & 1)
3812                     gst_adapter_flush (avi->adapter, 1);
3813                   gst_riff_parse_info (GST_ELEMENT_CAST (avi), buf, &tags);
3814                   if (tags) {
3815                     if (avi->globaltags) {
3816                       gst_tag_list_insert (avi->globaltags, tags,
3817                           GST_TAG_MERGE_REPLACE);
3818                     } else {
3819                       avi->globaltags = tags;
3820                     }
3821                   }
3822                   tags = NULL;
3823                   gst_buffer_unref (buf);
3824
3825                   avi->offset += GST_ROUND_UP_2 (size) - 4;
3826                 } else {
3827                   GST_DEBUG ("skipping INFO LIST prefix");
3828                 }
3829               } else {
3830                 /* Need more data */
3831                 return GST_FLOW_OK;
3832               }
3833               break;
3834             default:
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; ???
3839               } else {
3840                 /* Need more data */
3841                 return GST_FLOW_OK;
3842               }
3843               break;
3844           }
3845         } else {
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));
3849             //goto iterate;
3850           } else {
3851             /* Need more data */
3852             return GST_FLOW_OK;
3853           }
3854         }
3855       }
3856       break;
3857     default:
3858       GST_WARNING ("unhandled header state: %d", avi->header_state);
3859       break;
3860   }
3861 skipping_done:
3862
3863   GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
3864       avi->num_streams, avi->stream[0].indexes);
3865
3866   GST_DEBUG ("Found movi chunk. Starting to stream data");
3867   avi->state = GST_AVI_DEMUX_MOVI;
3868
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);
3872 #else
3873   /* no indexes in push mode, but it still sets some variables */
3874   gst_avi_demux_calculate_durations_from_index (avi);
3875 #endif
3876
3877   gst_avi_demux_expose_streams (avi, TRUE);
3878
3879   /* prepare all streams for index 0 */
3880   for (i = 0; i < avi->num_streams; i++)
3881     avi->stream[i].current_entry = 0;
3882
3883   /* create initial NEWSEGMENT event */
3884   if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
3885     stop = avi->segment.duration;
3886
3887   GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
3888
3889   if (avi->seg_event)
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);
3894
3895   gst_avi_demux_check_seekability (avi);
3896
3897   /* at this point we know all the streams and we can signal the no more
3898    * pads signal */
3899   GST_DEBUG_OBJECT (avi, "signaling no more pads");
3900   gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
3901
3902   return GST_FLOW_OK;
3903
3904   /* ERRORS */
3905 no_streams:
3906   {
3907     GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
3908     return GST_FLOW_ERROR;
3909   }
3910 header_no_list:
3911   {
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;
3916   }
3917 header_no_hdrl:
3918   {
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;
3924   }
3925 header_no_avih:
3926   {
3927     GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
3928         ("Invalid AVI header (no avih at start): %"
3929             GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
3930     if (sub)
3931       gst_buffer_unref (sub);
3932
3933     gst_buffer_unref (buf);
3934     return GST_FLOW_ERROR;
3935   }
3936 header_wrong_avih:
3937   {
3938     gst_buffer_unref (buf);
3939     return GST_FLOW_ERROR;
3940   }
3941 }
3942
3943 static void
3944 gst_avi_demux_add_date_tag (GstAviDemux * avi, gint y, gint m, gint d,
3945     gint h, gint min, gint s)
3946 {
3947   GDate *date;
3948   GstDateTime *dt;
3949
3950   date = g_date_new_dmy (d, m, y);
3951   if (!g_date_valid (date)) {
3952     /* bogus date */
3953     GST_WARNING_OBJECT (avi, "Refusing to add invalid date %d-%d-%d", y, m, d);
3954     g_date_free (date);
3955     return;
3956   }
3957
3958   dt = gst_date_time_new_local_time (y, m, d, h, min, s);
3959
3960   if (avi->globaltags == NULL)
3961     avi->globaltags = gst_tag_list_new ();
3962
3963   gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE, date,
3964       NULL);
3965   g_date_free (date);
3966   if (dt) {
3967     gst_tag_list_add (avi->globaltags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE_TIME,
3968         dt, NULL);
3969     gst_date_time_unref (dt);
3970   }
3971 }
3972
3973 static void
3974 gst_avi_demux_parse_idit_nums_only (GstAviDemux * avi, gchar * data)
3975 {
3976   gint y, m, d;
3977   gint hr = 0, min = 0, sec = 0;
3978   gint ret;
3979
3980   GST_DEBUG ("data : '%s'", data);
3981
3982   ret = sscanf (data, "%d:%d:%d %d:%d:%d", &y, &m, &d, &hr, &min, &sec);
3983   if (ret < 3) {
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);
3986     if (ret < 3) {
3987       GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
3988       return;
3989     }
3990   }
3991   gst_avi_demux_add_date_tag (avi, y, m, d, hr, min, sec);
3992 }
3993
3994 static gint
3995 get_month_num (gchar * data, guint size)
3996 {
3997   if (g_ascii_strncasecmp (data, "jan", 3) == 0) {
3998     return 1;
3999   } else if (g_ascii_strncasecmp (data, "feb", 3) == 0) {
4000     return 2;
4001   } else if (g_ascii_strncasecmp (data, "mar", 3) == 0) {
4002     return 3;
4003   } else if (g_ascii_strncasecmp (data, "apr", 3) == 0) {
4004     return 4;
4005   } else if (g_ascii_strncasecmp (data, "may", 3) == 0) {
4006     return 5;
4007   } else if (g_ascii_strncasecmp (data, "jun", 3) == 0) {
4008     return 6;
4009   } else if (g_ascii_strncasecmp (data, "jul", 3) == 0) {
4010     return 7;
4011   } else if (g_ascii_strncasecmp (data, "aug", 3) == 0) {
4012     return 8;
4013   } else if (g_ascii_strncasecmp (data, "sep", 3) == 0) {
4014     return 9;
4015   } else if (g_ascii_strncasecmp (data, "oct", 3) == 0) {
4016     return 10;
4017   } else if (g_ascii_strncasecmp (data, "nov", 3) == 0) {
4018     return 11;
4019   } else if (g_ascii_strncasecmp (data, "dec", 3) == 0) {
4020     return 12;
4021   }
4022
4023   return 0;
4024 }
4025
4026 static void
4027 gst_avi_demux_parse_idit_text (GstAviDemux * avi, gchar * data)
4028 {
4029   gint year, month, day;
4030   gint hour, min, sec;
4031   gint ret;
4032   gchar weekday[4];
4033   gchar monthstr[4];
4034
4035   ret = sscanf (data, "%3s %3s %d %d:%d:%d %d", weekday, monthstr, &day, &hour,
4036       &min, &sec, &year);
4037   if (ret != 7) {
4038     GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
4039     return;
4040   }
4041   month = get_month_num (monthstr, strlen (monthstr));
4042   gst_avi_demux_add_date_tag (avi, year, month, day, hour, min, sec);
4043 }
4044
4045 static void
4046 gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf)
4047 {
4048   gchar *data = (gchar *) GST_BUFFER_DATA (buf);
4049   guint size = GST_BUFFER_SIZE (buf);
4050   gchar *safedata = NULL;
4051
4052   /*
4053    * According to:
4054    * http://www.eden-foundation.org/products/code/film_date_stamp/index.html
4055    *
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
4060    *
4061    * FIXME: Our date tag doesn't include hours
4062    */
4063
4064   /* skip eventual initial whitespace */
4065   while (size > 0 && g_ascii_isspace (data[0])) {
4066     data++;
4067     size--;
4068   }
4069
4070   if (size == 0) {
4071     goto non_parsable;
4072   }
4073
4074   /* make a safe copy to add a \0 to the end of the string */
4075   safedata = g_strndup (data, size);
4076
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);
4080     g_free (safedata);
4081     return;
4082   } else if (g_ascii_isalpha (data[0])) {
4083     gst_avi_demux_parse_idit_text (avi, safedata);
4084     g_free (safedata);
4085     return;
4086   }
4087
4088   g_free (safedata);
4089
4090 non_parsable:
4091   GST_WARNING_OBJECT (avi, "IDIT tag has no parsable info");
4092 }
4093
4094 /*
4095  * Read full AVI headers.
4096  */
4097 static GstFlowReturn
4098 gst_avi_demux_stream_header_pull (GstAviDemux * avi)
4099 {
4100   GstFlowReturn res;
4101   GstBuffer *buf, *sub = NULL;
4102   guint32 tag;
4103   guint offset = 4;
4104   gint64 stop;
4105   GstElement *element = GST_ELEMENT_CAST (avi);
4106   GstClockTime stamp;
4107   GstTagList *tags = NULL;
4108
4109   stamp = gst_util_get_timestamp ();
4110
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)
4116     goto no_list;
4117   else if (GST_BUFFER_SIZE (buf) < 4)
4118     goto no_header;
4119
4120   GST_DEBUG_OBJECT (avi, "parsing headers");
4121
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))));
4126
4127     /* Eat up */
4128     gst_buffer_unref (buf);
4129
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)
4135       goto no_list;
4136     else if (GST_BUFFER_SIZE (buf) < 4)
4137       goto no_header;
4138   }
4139
4140   GST_DEBUG_OBJECT (avi, "hdrl LIST tag found");
4141
4142   gst_avi_demux_roundup_list (avi, &buf);
4143
4144   /* the hdrl starts with a 'avih' header */
4145   if (!gst_riff_parse_chunk (element, buf, &offset, &tag, &sub))
4146     goto no_avih;
4147   else if (tag != GST_RIFF_TAG_avih)
4148     goto no_avih;
4149   else if (!gst_avi_demux_parse_avih (avi, sub, &avi->avih))
4150     goto invalid_avih;
4151
4152   GST_DEBUG_OBJECT (avi, "AVI header ok, reading elements from header");
4153
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 */
4157     if (!sub)
4158       continue;
4159
4160     switch (tag) {
4161       case GST_RIFF_TAG_LIST:
4162       {
4163         guint8 *data;
4164         guint32 fourcc;
4165
4166         if (GST_BUFFER_SIZE (sub) < 4)
4167           goto next;
4168
4169         data = GST_BUFFER_DATA (sub);
4170         fourcc = GST_READ_UINT32_LE (data);
4171
4172         switch (fourcc) {
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"));
4177               sub = NULL;
4178 #ifdef DIVX_DRM
4179               goto invalid_stream;
4180 #endif
4181             }
4182             sub = NULL;
4183             goto next;
4184           case GST_RIFF_LIST_odml:
4185             gst_avi_demux_parse_odml (avi, sub);
4186             sub = NULL;
4187             break;
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);
4192             if (tags) {
4193               if (avi->globaltags) {
4194                 gst_tag_list_insert (avi->globaltags, tags,
4195                     GST_TAG_MERGE_REPLACE);
4196               } else {
4197                 avi->globaltags = tags;
4198               }
4199             }
4200             tags = NULL;
4201             break;
4202           default:
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));
4208             /* fall-through */
4209           case GST_RIFF_TAG_JUNQ:
4210           case GST_RIFF_TAG_JUNK:
4211             goto next;
4212         }
4213         break;
4214       }
4215       case GST_RIFF_IDIT:
4216         gst_avi_demux_parse_idit (avi, sub);
4217         goto next;
4218       default:
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));
4224         /* fall-through */
4225       case GST_RIFF_TAG_JUNQ:
4226       case GST_RIFF_TAG_JUNK:
4227       next:
4228         if (sub)
4229           gst_buffer_unref (sub);
4230         sub = NULL;
4231         break;
4232     }
4233   }
4234   gst_buffer_unref (buf);
4235   GST_DEBUG ("elements parsed");
4236
4237   /* check parsed streams */
4238   if (avi->num_streams == 0)
4239     goto no_streams;
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);
4244   }
4245
4246   GST_DEBUG_OBJECT (avi, "skipping junk between header and data, offset=%"
4247       G_GUINT64_FORMAT, avi->offset);
4248
4249   /* Now, find the data (i.e. skip all junk between header and data) */
4250   do {
4251     guint size;
4252     guint8 *data;
4253     guint32 tag, ltag;
4254
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;
4264     }
4265
4266     data = GST_BUFFER_DATA (buf);
4267
4268     tag = GST_READ_UINT32_LE (data);
4269     size = GST_READ_UINT32_LE (data + 4);
4270     ltag = GST_READ_UINT32_LE (data + 8);
4271
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);
4276
4277     switch (tag) {
4278       case GST_RIFF_TAG_LIST:{
4279         switch (ltag) {
4280           case GST_RIFF_LIST_movi:
4281             GST_DEBUG_OBJECT (avi,
4282                 "Reached the 'movi' tag, we're done with skipping");
4283             goto skipping_done;
4284           case GST_RIFF_LIST_INFO:
4285             res =
4286                 gst_riff_read_chunk (element, avi->sinkpad, &avi->offset, &tag,
4287                 &buf);
4288             if (res != GST_FLOW_OK) {
4289               GST_DEBUG_OBJECT (avi, "couldn't read INFO chunk");
4290               goto pull_range_failed;
4291             }
4292             GST_DEBUG ("got size %u", GST_BUFFER_SIZE (buf));
4293             if (size < 4) {
4294               GST_DEBUG ("skipping INFO LIST prefix");
4295               avi->offset += (4 - GST_ROUND_UP_2 (size));
4296               gst_buffer_unref (buf);
4297               continue;
4298             }
4299
4300             sub = gst_buffer_create_sub (buf, 4, GST_BUFFER_SIZE (buf) - 4);
4301             gst_riff_parse_info (element, sub, &tags);
4302             if (tags) {
4303               if (avi->globaltags) {
4304                 gst_tag_list_insert (avi->globaltags, tags,
4305                     GST_TAG_MERGE_REPLACE);
4306               } else {
4307                 avi->globaltags = tags;
4308               }
4309             }
4310             tags = NULL;
4311             if (sub) {
4312               gst_buffer_unref (sub);
4313               sub = NULL;
4314             }
4315             gst_buffer_unref (buf);
4316             /* gst_riff_read_chunk() has already advanced avi->offset */
4317             break;
4318           default:
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);
4323             break;
4324         }
4325       }
4326         break;
4327       default:
4328         GST_WARNING_OBJECT (avi, "Skipping unknown tag %" GST_FOURCC_FORMAT,
4329             GST_FOURCC_ARGS (tag));
4330         /* Fall-through */
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;
4339           }
4340           GST_MEMDUMP ("Junk", GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
4341           gst_buffer_unref (buf);
4342         }
4343         avi->offset += 8 + GST_ROUND_UP_2 (size);
4344         break;
4345     }
4346   } while (1);
4347 skipping_done:
4348
4349   GST_DEBUG_OBJECT (avi, "skipping done ... (streams=%u, stream[0].indexes=%p)",
4350       avi->num_streams, avi->stream[0].indexes);
4351
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);
4356   }
4357   if (!avi->have_index) {
4358     if (avi->avih->flags & GST_RIFF_AVIH_HASINDEX)
4359       gst_avi_demux_stream_index (avi);
4360
4361     /* still no index, scan */
4362     if (!avi->have_index) {
4363       gst_avi_demux_stream_scan (avi);
4364
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)
4369         goto no_index;
4370     }
4371   }
4372   /* use the indexes now to construct nice durations */
4373   gst_avi_demux_calculate_durations_from_index (avi);
4374
4375   gst_avi_demux_expose_streams (avi, FALSE);
4376
4377   /* create initial NEWSEGMENT event */
4378   if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
4379     stop = avi->segment.duration;
4380
4381   GST_DEBUG_OBJECT (avi, "segment stop %" G_GINT64_FORMAT, stop);
4382
4383   /* do initial seek to the default segment values */
4384   gst_avi_demux_do_seek (avi, &avi->segment);
4385
4386   /* prepare initial segment */
4387   if (avi->seg_event)
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);
4392
4393   stamp = gst_util_get_timestamp () - stamp;
4394   GST_DEBUG_OBJECT (avi, "pulling header took %" GST_TIME_FORMAT,
4395       GST_TIME_ARGS (stamp));
4396
4397   /* at this point we know all the streams and we can signal the no more
4398    * pads signal */
4399   GST_DEBUG_OBJECT (avi, "signaling no more pads");
4400   gst_element_no_more_pads (GST_ELEMENT_CAST (avi));
4401
4402   return GST_FLOW_OK;
4403
4404   /* ERRORS */
4405 no_list:
4406   {
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;
4412   }
4413 no_header:
4414   {
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;
4420   }
4421 no_avih:
4422   {
4423     GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL),
4424         ("Invalid AVI header (no avih at start): %"
4425             GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
4426     if (sub)
4427       gst_buffer_unref (sub);
4428     gst_buffer_unref (buf);
4429     return GST_FLOW_ERROR;
4430   }
4431 invalid_avih:
4432   {
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;
4437   }
4438 no_streams:
4439   {
4440     GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("No streams found"));
4441     return GST_FLOW_ERROR;
4442   }
4443 no_index:
4444   {
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;
4449   }
4450 pull_range_failed:
4451   {
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;
4455   }
4456 #ifdef DIVX_DRM
4457 invalid_stream:
4458   {
4459           gst_buffer_unref(buf);
4460           return GST_FLOW_ERROR;
4461   }
4462 #endif
4463 }
4464
4465 /* move a stream to @index */
4466 static void
4467 gst_avi_demux_move_stream (GstAviDemux * avi, GstAviStream * stream,
4468     GstSegment * segment, guint index)
4469 {
4470   GST_DEBUG_OBJECT (avi, "Move stream %d to %u", stream->num, index);
4471
4472   if (segment->rate < 0.0) {
4473     guint next_key;
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);
4479
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;
4486
4487     GST_DEBUG_OBJECT (avi, "reverse seek: start %u, step %u, stop %u",
4488         stream->start_entry, stream->step_entry, stream->stop_entry);
4489   } else {
4490     stream->start_entry = index;
4491     stream->step_entry = index;
4492     stream->stop_entry = gst_avi_demux_index_last (avi, stream);
4493   }
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;
4499   }
4500
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);
4505
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);
4512
4513   GST_DEBUG_OBJECT (avi, "Seeking to offset %" G_GUINT64_FORMAT,
4514       stream->index[index].offset);
4515 }
4516
4517 /*
4518  * Do the actual seeking.
4519  */
4520 static gboolean
4521 gst_avi_demux_do_seek (GstAviDemux * avi, GstSegment * segment)
4522 {
4523   GstClockTime seek_time;
4524   gboolean keyframe;
4525   guint i, index;
4526   GstAviStream *stream;
4527
4528   seek_time = segment->last_stop;
4529   keyframe = ! !(segment->flags & GST_SEEK_FLAG_KEY_UNIT);
4530
4531   GST_DEBUG_OBJECT (avi, "seek to: %" GST_TIME_FORMAT
4532       " keyframe seeking:%d", GST_TIME_ARGS (seek_time), keyframe);
4533
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];
4537
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)
4544 #endif
4545 {
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
4550      * decoding from. */
4551     index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4552     GST_DEBUG_OBJECT (avi, "previous keyframe at %u", index);
4553   }
4554 }
4555   /* move the main stream to this position */
4556   gst_avi_demux_move_stream (avi, stream, segment, index);
4557
4558   if (keyframe) {
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));
4564   }
4565
4566   /* the seek time is also the last_stop and stream time when going
4567    * forwards */
4568   segment->last_stop = seek_time;
4569   if (segment->rate > 0.0)
4570   {
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;
4577 #else
4578     segment->time = seek_time;
4579 #endif
4580   }
4581
4582   /* now set DISCONT and align the other streams */
4583   for (i = 0; i < avi->num_streams; i++) {
4584     GstAviStream *ostream;
4585
4586     ostream = &avi->stream[i];
4587     if ((ostream == stream) || (ostream->index == NULL))
4588       continue;
4589
4590     /* get the entry index for the requested position */
4591     index = gst_avi_demux_index_for_time (avi, ostream, seek_time);
4592
4593 #ifdef AVIDEMUX_MODIFICATION
4594 /*Modification: Conditions added to support trickplay */
4595         if(segment->rate<=1.0 &&  segment->rate>=0.0)
4596 #endif
4597         {
4598             /* move to previous keyframe */
4599             if (!ENTRY_IS_KEYFRAME (&ostream->index[index]))
4600               index = gst_avi_demux_index_prev (avi, ostream, index, TRUE);
4601         }
4602
4603     gst_avi_demux_move_stream (avi, ostream, segment, index);
4604   }
4605   GST_DEBUG_OBJECT (avi, "done seek to: %" GST_TIME_FORMAT,
4606       GST_TIME_ARGS (seek_time));
4607
4608   return TRUE;
4609 }
4610
4611 /*
4612  * Handle seek event in pull mode.
4613  */
4614 static gboolean
4615 gst_avi_demux_handle_seek (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4616 {
4617   gdouble rate;
4618   GstFormat format;
4619   GstSeekFlags flags;
4620   GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4621   gint64 cur, stop;
4622   gboolean flush;
4623   gboolean update;
4624   GstSegment seeksegment = { 0, };
4625   gint i;
4626
4627   if (event) {
4628     GST_DEBUG_OBJECT (avi, "doing seek with event");
4629
4630     gst_event_parse_seek (event, &rate, &format, &flags,
4631         &cur_type, &cur, &stop_type, &stop);
4632
4633     /* we have to have a format as the segment format. Try to convert
4634      * if not. */
4635     if (format != GST_FORMAT_TIME) {
4636       GstFormat fmt = GST_FORMAT_TIME;
4637       gboolean res = TRUE;
4638
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);
4643       if (!res)
4644         goto no_format;
4645
4646       format = fmt;
4647     }
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 */
4652   } else {
4653     GST_DEBUG_OBJECT (avi, "doing seek without event");
4654     flags = 0;
4655     rate = 1.0;
4656   }
4657
4658   /* save flush flag */
4659   flush = flags & GST_SEEK_FLAG_FLUSH;
4660
4661   if (flush) {
4662     GstEvent *fevent = gst_event_new_flush_start ();
4663
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);
4670   } else {
4671     /* a non-flushing seek, we PAUSE the task so that we can take the
4672      * STREAM_LOCK */
4673     GST_DEBUG_OBJECT (avi, "non flushing seek, pausing task");
4674     gst_pad_pause_task (avi->sinkpad);
4675   }
4676
4677   /* wait for streaming to stop */
4678   GST_DEBUG_OBJECT (avi, "wait for streaming to stop");
4679   GST_PAD_STREAM_LOCK (avi->sinkpad);
4680
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));
4684
4685   if (event) {
4686     GST_DEBUG_OBJECT (avi, "configuring seek");
4687     gst_segment_set_seek (&seeksegment, rate, format, flags,
4688         cur_type, cur, stop_type, stop, &update);
4689   }
4690   /* do the seek, seeksegment.last_stop contains the new position, this
4691    * actually never fails. */
4692   gst_avi_demux_do_seek (avi, &seeksegment);
4693
4694   gst_event_replace (&avi->close_seg_event, NULL);
4695   if (flush) {
4696     GstEvent *fevent = gst_event_new_flush_stop ();
4697
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);
4709   }
4710
4711   /* now update the real segment info */
4712   memcpy (&avi->segment, &seeksegment, sizeof (GstSegment));
4713
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));
4719   }
4720
4721   /* prepare for streaming again */
4722   if ((stop = avi->segment.stop) == GST_CLOCK_TIME_NONE)
4723     stop = avi->segment.duration;
4724
4725   /* queue the segment event for the streaming thread. */
4726   if (avi->seg_event)
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);
4733   } else {
4734 #ifdef AVIDEMUX_MODIFICATION
4735
4736         avi->segment.start = 0;
4737         avi->segment.time = 0;
4738 #endif
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);
4743   }
4744
4745   if (!avi->streaming) {
4746     avi->segment_running = TRUE;
4747     gst_pad_start_task (avi->sinkpad, (GstTaskFunction) gst_avi_demux_loop,
4748         avi->sinkpad);
4749   }
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;
4755   }
4756   GST_PAD_STREAM_UNLOCK (avi->sinkpad);
4757
4758   return TRUE;
4759
4760   /* ERRORS */
4761 no_format:
4762   {
4763     GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4764     return FALSE;
4765   }
4766 }
4767
4768 /*
4769  * Handle seek event in push mode.
4770  */
4771 static gboolean
4772 avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad, GstEvent * event)
4773 {
4774   gdouble rate;
4775   GstFormat format;
4776   GstSeekFlags flags;
4777   GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
4778   gint64 cur, stop;
4779   gboolean keyframe;
4780   GstAviStream *stream;
4781   guint index;
4782   guint n, str_num;
4783   guint64 min_offset;
4784   GstSegment seeksegment;
4785   gboolean update;
4786
4787   /* check we have the index */
4788   if (!avi->have_index) {
4789     GST_DEBUG_OBJECT (avi, "no seek index built, seek aborted.");
4790     return FALSE;
4791   } else {
4792     GST_DEBUG_OBJECT (avi, "doing push-based seek with event");
4793   }
4794
4795   gst_event_parse_seek (event, &rate, &format, &flags,
4796       &cur_type, &cur, &stop_type, &stop);
4797
4798         /* some slack aiming for a keyframe */
4799         if (cur < GST_AVI_SEEK_PUSH_DISPLACE)
4800         cur = 0;
4801         else
4802         cur -= GST_AVI_SEEK_PUSH_DISPLACE;
4803
4804   if (format != GST_FORMAT_TIME) {
4805     GstFormat fmt = GST_FORMAT_TIME;
4806     gboolean res = TRUE;
4807
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);
4812     if (!res) {
4813       GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
4814       return FALSE;
4815     }
4816
4817     format = fmt;
4818   }
4819
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);
4825
4826   keyframe = ! !(flags & GST_SEEK_FLAG_KEY_UNIT);
4827   cur = seeksegment.last_stop;
4828
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,
4832       rate);
4833
4834   if (rate < 0) {
4835     GST_DEBUG_OBJECT (avi, "negative rate seek not supported in push mode");
4836     return FALSE;
4837   }
4838
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];
4843
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));
4848
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
4853      * decoding from. */
4854     index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
4855     GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", index);
4856   }
4857
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);
4861
4862   /* re-use cur to be the timestamp of the seek as it _will_ be */
4863   cur = stream->current_timestamp;
4864
4865   min_offset = stream->index[index].offset;
4866   avi->seek_kf_offset = min_offset - 8;
4867
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,
4871       index, min_offset);
4872
4873   for (n = 0; n < avi->num_streams; n++) {
4874     GstAviStream *str = &avi->stream[n];
4875     guint idx;
4876
4877     if (n == avi->main_stream)
4878       continue;
4879
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));
4884
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
4889        * decoding from. */
4890       idx = gst_avi_demux_index_prev (avi, str, idx, TRUE);
4891       GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", idx);
4892     }
4893
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);
4897
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",
4902           min_offset, n);
4903       str_num = n;
4904       stream = str;
4905       index = idx;
4906     }
4907   }
4908
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);
4916
4917   /* index data refers to data, not chunk header (for pull mode convenience) */
4918   min_offset -= 8;
4919   GST_DEBUG_OBJECT (avi, "seeking to chunk at offset %" G_GUINT64_FORMAT,
4920       min_offset);
4921
4922   if (!perform_seek_to_offset (avi, min_offset)) {
4923     GST_DEBUG_OBJECT (avi, "seek event failed!");
4924     return FALSE;
4925   }
4926
4927   return TRUE;
4928 }
4929
4930 /*
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.
4933  */
4934 static gboolean
4935 gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
4936     GstEvent * event)
4937 {
4938   /* check for having parsed index already */
4939   if (!avi->have_index) {
4940     guint64 offset = 0;
4941     gboolean building_index;
4942
4943     GST_OBJECT_LOCK (avi);
4944     /* handle the seek event in the chain function */
4945     avi->state = GST_AVI_DEMUX_SEEK;
4946
4947     /* copy the event */
4948     if (avi->seek_event)
4949       gst_event_unref (avi->seek_event);
4950     avi->seek_event = gst_event_ref (event);
4951
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];
4961       } else {
4962         offset = avi->idx1_offset;
4963       }
4964     }
4965     GST_OBJECT_UNLOCK (avi);
4966
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,
4971           offset);
4972       return perform_seek_to_offset (avi, offset);
4973     }
4974
4975     /* FIXME: we have to always return true so that we don't block the seek
4976      * thread.
4977      * Note: maybe it is OK to return true if we're still building the index */
4978     return TRUE;
4979   }
4980
4981   return avi_demux_handle_seek_push (avi, pad, event);
4982 }
4983
4984 /*
4985  * Helper for gst_avi_demux_invert()
4986  */
4987 static inline void
4988 swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
4989 {
4990   memcpy (tmp, d1, bytes);
4991   memcpy (d1, d2, bytes);
4992   memcpy (d2, tmp, bytes);
4993 }
4994
4995
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)
5000
5001 /*
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?
5006  */
5007 static GstBuffer *
5008 gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
5009 {
5010   GstStructure *s;
5011   gint y, w, h;
5012   gint bpp, stride;
5013   guint8 *tmp = NULL;
5014
5015   if (stream->strh->type != GST_RIFF_FCC_vids)
5016     return buf;
5017
5018   if (!gst_avi_demux_is_uncompressed (stream->strh->fcc_handler)) {
5019     return buf;                 /* Ignore non DIB buffers */
5020   }
5021
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");
5025     return buf;
5026   }
5027
5028   if (stream->strf.vids == NULL) {
5029     GST_WARNING ("Failed to retrieve vids for stream");
5030     return buf;
5031   }
5032
5033   h = stream->strf.vids->height;
5034   w = stream->strf.vids->width;
5035   stride = w * (bpp / 8);
5036
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");
5040     return buf;
5041   }
5042
5043   tmp = g_malloc (stride);
5044
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);
5048   }
5049
5050   g_free (tmp);
5051
5052   return buf;
5053 }
5054
5055 static void
5056 gst_avi_demux_add_assoc (GstAviDemux * avi, GstAviStream * stream,
5057     GstClockTime timestamp, guint64 offset, gboolean keyframe)
5058 {
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);
5071   }
5072 }
5073
5074 /*
5075  * Returns the aggregated GstFlowReturn.
5076  */
5077 static GstFlowReturn
5078 gst_avi_demux_combine_flows (GstAviDemux * avi, GstAviStream * stream,
5079     GstFlowReturn ret)
5080 {
5081   guint i;
5082   gboolean unexpected = FALSE, not_linked = TRUE;
5083
5084   /* store the value */
5085   stream->last_flow = ret;
5086
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))
5089     goto done;
5090
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];
5094
5095     ret = ostream->last_flow;
5096     /* no unexpected or unlinked, return */
5097     if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
5098       goto done;
5099
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);
5103   }
5104   /* when we get here, we all have unlinked or unexpected */
5105   if (not_linked)
5106     ret = GST_FLOW_NOT_LINKED;
5107   else if (unexpected)
5108     ret = GST_FLOW_UNEXPECTED;
5109 done:
5110   GST_LOG_OBJECT (avi, "combined %s to return %s",
5111       gst_flow_get_name (stream->last_flow), gst_flow_get_name (ret));
5112   return ret;
5113 }
5114
5115 #ifdef AVIDEMUX_MODIFICATION
5116 /*Modification: Added functions to update the stream while doing the trickplay*/
5117
5118 /* move @stream to the next position in its index */
5119 static GstFlowReturn
5120 gst_avi_demux_update_backward (GstAviDemux * avi, GstAviStream * stream,
5121     GstFlowReturn ret)
5122 {
5123   int old_entry, new_entry;
5124
5125   old_entry = stream->current_entry;
5126   /* move forwards */
5127   new_entry = stream->next_kindex;
5128
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);
5133         goto eos;
5134       }
5135
5136   if (new_entry != old_entry) {
5137     stream->current_entry = new_entry;
5138     stream->current_total = stream->index[new_entry].total;
5139
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);
5153   }
5154   return ret;
5155
5156   /* ERROR */
5157 eos:
5158   {
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;
5163   }
5164 }
5165
5166 /* move @stream to the next position in its index */
5167 static GstFlowReturn
5168 gst_avi_demux_update (GstAviDemux * avi, GstAviStream * stream,
5169     GstFlowReturn ret)
5170 {
5171   guint old_entry, new_entry;
5172
5173   old_entry = stream->current_entry;
5174   /* move forwards */
5175   new_entry = stream->next_kindex;
5176
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);
5183         goto eos;
5184       }
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);
5189
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);
5193
5194       /* and start from the previous keyframe now */
5195       new_entry = stream->step_entry;
5196     } else {
5197       /* EOS */
5198       GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
5199       goto eos;
5200     }
5201   }
5202
5203   if (new_entry != old_entry) {
5204     stream->current_entry = new_entry;
5205     stream->current_total = stream->index[new_entry].total;
5206
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);
5215     } else {
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);
5229     }
5230   }
5231   return ret;
5232
5233   /* ERROR */
5234 eos:
5235   {
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;
5240   }
5241 }
5242
5243 /* move @stream to the next position in its index */
5244 static GstFlowReturn
5245 gst_avi_demux_previous (GstAviDemux * avi, GstAviStream * stream,
5246     GstFlowReturn ret)
5247 {
5248   int old_entry, new_entry;
5249
5250   old_entry = stream->current_entry;
5251   /* move forwards */
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);
5258         goto eos;
5259       }
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;
5264
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);
5278   }
5279   return ret;
5280
5281   /* ERROR */
5282 eos:
5283   {
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;
5288   }
5289 }
5290 #endif
5291
5292 /* move @stream to the next position in its index */
5293 static GstFlowReturn
5294 gst_avi_demux_advance (GstAviDemux * avi, GstAviStream * stream,
5295     GstFlowReturn ret)
5296 {
5297   guint old_entry, new_entry;
5298
5299   old_entry = stream->current_entry;
5300   /* move forwards */
5301   new_entry = old_entry + 1;
5302
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);
5309         goto eos;
5310       }
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);
5315
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);
5319
5320       /* and start from the previous keyframe now */
5321       new_entry = stream->step_entry;
5322     } else {
5323       /* EOS */
5324       GST_DEBUG_OBJECT (avi, "forward reached stop %u", stream->stop_entry);
5325       goto eos;
5326     }
5327   }
5328
5329   if (new_entry != old_entry) {
5330     stream->current_entry = new_entry;
5331     stream->current_total = stream->index[new_entry].total;
5332
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);
5341     } else {
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);
5355     }
5356   }
5357   return ret;
5358
5359   /* ERROR */
5360 eos:
5361   {
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;
5366   }
5367 }
5368
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 */
5372 static gint
5373 gst_avi_demux_find_next (GstAviDemux * avi, gfloat rate)
5374 {
5375   guint64 min_time, max_time;
5376   guint stream_num, i;
5377
5378   max_time = 0;
5379   min_time = G_MAXUINT64;
5380   stream_num = -1;
5381
5382   for (i = 0; i < avi->num_streams; i++) {
5383     guint64 position;
5384     GstAviStream *stream;
5385
5386     stream = &avi->stream[i];
5387
5388     /* ignore streams that finished */
5389     if (stream->last_flow == GST_FLOW_UNEXPECTED)
5390       continue;
5391
5392     position = stream->current_timestamp;
5393
5394     /* position of -1 is EOS */
5395     if (position != -1) {
5396       if (rate > 0.0 && position < min_time) {
5397         min_time = position;
5398         stream_num = i;
5399       } else if (rate < 0.0 && position >= max_time) {
5400         max_time = position;
5401         stream_num = i;
5402       }
5403     }
5404   }
5405   return stream_num;
5406 }
5407
5408 static GstFlowReturn
5409 gst_avi_demux_loop_data (GstAviDemux * avi)
5410 {
5411   GstFlowReturn ret = GST_FLOW_OK;
5412   guint stream_num;
5413   GstAviStream *stream;
5414   gboolean processed = FALSE;
5415   GstBuffer *buf;
5416   guint64 offset, size;
5417   GstClockTime timestamp, duration;
5418   guint64 out_offset, out_offset_end;
5419   gboolean keyframe;
5420   GstAviIndexEntry *entry;
5421 #ifdef AVIDEMUX_MODIFICATION
5422 gdouble minusone = -1;
5423 #endif
5424   do {
5425 #ifdef AVIDEMUX_MODIFICATION
5426         timestamp=0;
5427 #endif
5428     stream_num = gst_avi_demux_find_next (avi, avi->segment.rate);
5429
5430     /* all are EOS */
5431     if (G_UNLIKELY (stream_num == -1)) {
5432       GST_DEBUG_OBJECT (avi, "all streams are EOS");
5433       goto eos;
5434     }
5435
5436     /* we have the stream now */
5437     stream = &avi->stream[stream_num];
5438
5439     /* skip streams without pads */
5440     if (!stream->pad) {
5441       GST_DEBUG_OBJECT (avi, "skipping entry from stream %d without pad",
5442           stream_num);
5443       goto next;
5444     }
5445
5446 #ifdef AVIDEMUX_MODIFICATION
5447 /*Modification: Added trickplay functionality to achieve upto 64x speed play*/
5448
5449         if(avi->segment.rate>1.0)
5450         {
5451                 guint64 next_kindex_timestamp;
5452
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))
5456                 {
5457
5458                         GST_DEBUG_OBJECT (avi, "All keyframes case video forward bfore :%d", stream->current_entry);
5459
5460                         stream->next_kindex= stream->current_entry+avi->segment.rate;
5461
5462                         if(stream->next_kindex>stream->idx_n)
5463                                 stream->next_kindex=stream->idx_n;
5464
5465                         gst_avi_demux_update (avi, stream, ret);
5466             
5467             /* Checking the EOS condition in Trickplay*/
5468                         if(stream->current_timestamp == -1)
5469                         {
5470                                 GST_WARNING_OBJECT(avi, "eos in Trickplay");
5471                                 goto eos;
5472                         }
5473
5474
5475                         GST_DEBUG_OBJECT (avi, "All key frames after :%d", stream->next_kindex);
5476                 
5477                 }
5478                 else if(stream->strh->type == GST_RIFF_FCC_vids)
5479                 {
5480                         if(!stream->next_kindex)
5481                         {
5482                                 stream->next_kindex = stream->prev_kindex = stream->current_entry;
5483                                 
5484                                 while(1)
5485                                 {
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);
5487                                         
5488                                         if((stream->next_kindex+1)>=stream->idx_n)
5489                                         {
5490                                                 GST_DEBUG_OBJECT(avi,"eos");
5491                                                 break;
5492                                         }
5493                                         stream->next_kindex = gst_avi_demux_index_next(avi, stream, stream->next_kindex+1, TRUE);
5494                                         if(stream->next_kindex<0)
5495                                         {
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);
5497
5498                                                 stream->prev_kindex=stream->next_kindex=0;
5499                                                 break;
5500                                         }
5501                                         
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;
5506
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)
5514                                                 break;
5515                                 }
5516                                 
5517                                 stream->discont = TRUE;
5518
5519                         }
5520                         else
5521                         {
5522                                 stream->samples_to_show_bet_kframes--;
5523
5524                                 if(!stream->samples_to_show_bet_kframes)
5525                                 {
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;
5531                                         ret = GST_FLOW_OK;
5532                                       processed = TRUE;
5533                                         goto next;
5534                                 }
5535                                         
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);
5543                                 
5544                         }
5545                 }
5546         }
5547         else if(avi->segment.rate<0)
5548         {
5549                 /*      ***********Kishore***********
5550                 only I frames Displayed 
5551                 */
5552                 if(stream->strh->type == GST_RIFF_FCC_vids)
5553                 {
5554                         guint64 time_position;
5555                         guint64 duration;
5556                         int index;
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;
5564                         else
5565                                 time_position=0;
5566
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);
5570
5571                         stream->next_kindex = gst_avi_demux_index_prev(avi, stream, index, TRUE);
5572
5573                 }
5574                 else
5575                 {
5576                         GST_DEBUG_OBJECT (avi, "audio time backward bfore :%d", stream->current_entry);
5577                 
5578                         stream->next_kindex= stream->current_entry-(minusone *avi->segment.rate);                               
5579
5580                         if(stream->next_kindex<0)
5581                                 stream->next_kindex=0;
5582
5583                         GST_DEBUG_OBJECT (avi, "audio time backward after :%d", stream->next_kindex);
5584                 }
5585                 
5586                 gst_avi_demux_update_backward(avi, stream, ret);
5587
5588                 stream->discont = TRUE;
5589         }
5590 #endif
5591     /* get the timing info for the entry */
5592 #ifdef AVIDEMUX_MODIFICATION
5593         if(!timestamp)
5594 #endif
5595         {
5596             timestamp = stream->current_timestamp;
5597             duration = stream->current_ts_end - timestamp;
5598         }
5599     out_offset = stream->current_offset;
5600     out_offset_end = stream->current_offset_end;
5601
5602     /* get the entry data info */
5603     entry = &stream->index[stream->current_entry];
5604     offset = entry->offset;
5605     size = entry->size;
5606     keyframe = ENTRY_IS_KEYFRAME (entry);
5607
5608     /* skip empty entries */
5609     if (size == 0) {
5610       GST_DEBUG_OBJECT (avi, "Skipping entry %u (%" G_GUINT64_FORMAT ", %p)",
5611           stream->current_entry, size, stream->pad);
5612       goto next;
5613     }
5614
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)) {
5619         goto eos_stop;
5620       }
5621     }
5622
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);
5626
5627     /* FIXME, check large chunks and cut them up */
5628
5629     /* pull in the data */
5630     ret = gst_pad_pull_range (avi->sinkpad, offset, size, &buf);
5631     if (ret != GST_FLOW_OK)
5632       goto pull_failed;
5633
5634     /* check for short buffers, this is EOS as well */
5635     if (GST_BUFFER_SIZE (buf) < size)
5636       goto short_buffer;
5637
5638     /* invert the picture if needed */
5639     buf = gst_avi_demux_invert (stream, buf);
5640
5641     /* mark non-keyframes */
5642     if (keyframe)
5643       GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
5644     else
5645       GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
5646
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;
5651
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;
5657     }
5658
5659     gst_avi_demux_add_assoc (avi, stream, timestamp, offset, keyframe);
5660
5661     gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
5662
5663     /* update current position in the segment */
5664     gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, timestamp);
5665
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);
5671
5672 #ifdef DIVX_DRM
5673
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
5678
5679         if (avi->drmContext)// this is drm
5680         {
5681                 GstBuffer* encrypted_buf = NULL;
5682
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));
5686                                 } else  {
5687                                         GST_ERROR_OBJECT (avi, "drmDecryptAudio () Failed : buffer = %d", GST_BUFFER_SIZE(buf));
5688                                 }
5689                                 ret = gst_pad_push (stream->pad, buf);
5690
5691                 } else if (stream->strh->type == GST_RIFF_FCC_vids) {   /* Video Stream */
5692
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");
5699                         } else {
5700                                 guint8 tempBuffer[256] = { 0, };
5701                                 guint32 tempBufferLength = 0;
5702                                 int ret;
5703
5704                                 ret = avi->divx_prepare_video_bitstream (avi->drmContext,
5705                                                 GST_BUFFER_DATA(dd_chunk_buf)+(CHUNK_ID_LEN+CHUNK_SIZE_LEN),
5706                                                 DD_CHUNK_DATA_LEN,
5707                                                 tempBuffer,
5708                                                 &tempBufferLength );
5709
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);
5718
5719                                                 /* relase buf because we will push encrypted_buf instead of buf */
5720                                                 gst_buffer_unref (buf);
5721                                         } else {
5722                                                 GST_ERROR_OBJECT (avi, "gst_buffer_new_and_alloc() failed!!!!");
5723                                         }
5724                                 } else {
5725                                         GST_ERROR_OBJECT (avi, "divx_prepare_video_bitstream failed!!!! ret = [%d]", ret);
5726                                 }
5727                         }
5728
5729                         /* Release DD-chunk Buffer */
5730                         if (dd_chunk_buf)
5731                                 gst_buffer_unref (dd_chunk_buf);
5732
5733                         /* Push encrypted_buf if is valid, otherwise push original buffer */
5734                         if (encrypted_buf)
5735                                 ret = gst_pad_push (stream->pad, encrypted_buf);
5736                         else
5737                                 ret = gst_pad_push (stream->pad, buf);
5738                 }
5739         } else {
5740                  /* This is normal file */
5741                  ret = gst_pad_push (stream->pad, buf);
5742         }
5743 #else
5744     ret = gst_pad_push (stream->pad, buf);
5745 #endif // DIVX_DRM
5746
5747     /* mark as processed, we increment the frame and byte counters then
5748      * leave the while loop and return the GstFlowReturn */
5749     processed = TRUE;
5750
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");
5757         ret = GST_FLOW_OK;
5758       }
5759     }
5760   next:
5761     /* move to next item */
5762 #ifdef AVIDEMUX_MODIFICATION
5763     if (avi->segment.rate > 0) {        
5764         ret = gst_avi_demux_advance (avi, stream, ret);
5765     }
5766     else {
5767         ret = gst_avi_demux_previous (avi, stream, ret);
5768     }
5769     
5770     /* Checking the EOS condition in Trickplay : naveen*/
5771     if((stream->current_timestamp == -1) && (avi->segment.rate != 1.0))
5772     {
5773         GST_WARNING_OBJECT(avi, "eos in Trickplay");
5774         goto eos;
5775     }
5776 #else
5777     ret = gst_avi_demux_advance (avi, stream, ret);
5778 #endif
5779     /* combine flows */
5780     ret = gst_avi_demux_combine_flows (avi, stream, ret);
5781   } while (!processed);
5782
5783 beach:
5784   return ret;
5785
5786   /* special cases */
5787 eos:
5788   {
5789     GST_DEBUG_OBJECT (avi, "No samples left for any streams - EOS");
5790     ret = GST_FLOW_UNEXPECTED;
5791     goto beach;
5792   }
5793 eos_stop:
5794   {
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 */
5800     goto next;
5801   }
5802 pull_failed:
5803   {
5804     GST_DEBUG_OBJECT (avi, "pull range failed: pos=%" G_GUINT64_FORMAT
5805         " size=%" G_GUINT64_FORMAT, offset, size);
5806     goto beach;
5807   }
5808 short_buffer:
5809   {
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;
5815     goto beach;
5816   }
5817 }
5818
5819 /*
5820  * Read data. If we have an index it delegates to
5821  * gst_avi_demux_process_next_entry().
5822  */
5823 static GstFlowReturn
5824 gst_avi_demux_stream_data (GstAviDemux * avi)
5825 {
5826   guint32 tag = 0;
5827   guint32 size = 0;
5828   gint stream_nr = 0;
5829   GstFlowReturn res = GST_FLOW_OK;
5830   GstFormat format = GST_FORMAT_TIME;
5831
5832   if (G_UNLIKELY (avi->have_eos)) {
5833     /* Clean adapter, we're done */
5834     gst_adapter_clear (avi->adapter);
5835     return GST_FLOW_UNEXPECTED;
5836   }
5837
5838   if (G_UNLIKELY (avi->todrop)) {
5839     guint drop;
5840
5841     if ((drop = gst_adapter_available (avi->adapter))) {
5842       if (drop > avi->todrop)
5843         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;
5848     }
5849   }
5850
5851   /* Iterate until need more data, so adapter won't grow too much */
5852   while (1) {
5853     if (G_UNLIKELY (!gst_avi_demux_peek_chunk_info (avi, &tag, &size))) {
5854       return GST_FLOW_OK;
5855     }
5856
5857     GST_DEBUG ("Trying chunk (%" GST_FOURCC_FORMAT "), size %d",
5858         GST_FOURCC_ARGS (tag), size);
5859
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));
5870       }
5871       return GST_FLOW_OK;
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);
5877         continue;
5878       }
5879       return GST_FLOW_OK;
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));
5887       }
5888       return GST_FLOW_OK;
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);
5894         continue;
5895       }
5896       return GST_FLOW_OK;
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));
5905       }
5906       return GST_FLOW_OK;
5907     } else {
5908       GST_DEBUG ("No more stream chunks, send EOS");
5909       avi->have_eos = TRUE;
5910       return GST_FLOW_UNEXPECTED;
5911     }
5912
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;
5919         if (size) {
5920           gst_adapter_flush (avi->adapter, 8);
5921           return GST_FLOW_OK;
5922         }
5923       } else {
5924         return GST_FLOW_OK;
5925       }
5926     }
5927     GST_DEBUG ("chunk ID %" GST_FOURCC_FORMAT ", size %u",
5928         GST_FOURCC_ARGS (tag), size);
5929
5930     stream_nr = CHUNKID_TO_STREAMNR (tag);
5931
5932     if (G_UNLIKELY (stream_nr < 0 || stream_nr >= avi->num_streams)) {
5933       /* recoverable */
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));
5938     } else {
5939       GstAviStream *stream;
5940       GstClockTime next_ts = 0;
5941       GstBuffer *buf = NULL;
5942       guint64 offset;
5943       gboolean saw_desired_kf = stream_nr != avi->main_stream
5944           || avi->offset >= avi->seek_kf_offset;
5945
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;
5949       }
5950
5951       if (saw_desired_kf) {
5952         gst_adapter_flush (avi->adapter, 8);
5953         /* get buffer */
5954         if (size) {
5955           buf = gst_adapter_take_buffer (avi->adapter, GST_ROUND_UP_2 (size));
5956           /* patch the size */
5957           GST_BUFFER_SIZE (buf) = size;
5958         } else {
5959           buf = NULL;
5960         }
5961       } else {
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));
5965       }
5966
5967       offset = avi->offset;
5968       avi->offset += 8 + GST_ROUND_UP_2 (size);
5969
5970       stream = &avi->stream[stream_nr];
5971
5972       /* set delay (if any)
5973          if (stream->strh->init_frames == stream->current_frame &&
5974          stream->delay == 0)
5975          stream->delay = next_ts;
5976        */
5977
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));
5982         if (buf)
5983           gst_buffer_unref (buf);
5984       } else {
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))
5988           goto wrong_format;
5989
5990         gst_avi_demux_add_assoc (avi, stream, next_ts, offset, FALSE);
5991
5992         /* increment our positions */
5993         stream->current_entry++;
5994         stream->current_total += size;
5995
5996         /* update current position in the segment */
5997         gst_segment_set_last_stop (&avi->segment, GST_FORMAT_TIME, next_ts);
5998
5999         if (saw_desired_kf && buf) {
6000           GstClockTime dur_ts = 0;
6001
6002           /* invert the picture if needed */
6003           buf = gst_avi_demux_invert (stream, buf);
6004
6005           gst_pad_query_position (stream->pad, &format, (gint64 *) & dur_ts);
6006           if (G_UNLIKELY (format != GST_FORMAT_TIME))
6007             goto wrong_format;
6008
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;
6014           } else {
6015             GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
6016             GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
6017           }
6018
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));
6026
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;
6032           }
6033           res = gst_pad_push (stream->pad, buf);
6034           buf = NULL;
6035
6036           /* combine flows */
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));
6040             return res;
6041           }
6042         }
6043       }
6044     }
6045   }
6046
6047 done:
6048   return res;
6049
6050   /* ERRORS */
6051 wrong_format:
6052   {
6053     GST_DEBUG_OBJECT (avi, "format %s != GST_FORMAT_TIME",
6054         gst_format_get_name (format));
6055     res = GST_FLOW_ERROR;
6056     goto done;
6057   }
6058 }
6059
6060 /*
6061  * Send pending tags.
6062  */
6063 static void
6064 push_tag_lists (GstAviDemux * avi)
6065 {
6066   guint i;
6067   GstTagList *tags;
6068
6069   if (!avi->got_tags)
6070     return;
6071
6072   GST_DEBUG_OBJECT (avi, "Pushing pending tag lists");
6073
6074   for (i = 0; i < avi->num_streams; i++) {
6075     GstAviStream *stream = &avi->stream[i];
6076     GstPad *pad = stream->pad;
6077
6078     tags = stream->taglist;
6079
6080     if (pad && tags) {
6081       GST_DEBUG_OBJECT (pad, "Tags: %" GST_PTR_FORMAT, tags);
6082
6083       gst_element_found_tags_for_pad (GST_ELEMENT_CAST (avi), pad, tags);
6084       stream->taglist = NULL;
6085     }
6086   }
6087
6088   if (!(tags = avi->globaltags))
6089     tags = gst_tag_list_new ();
6090
6091   gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
6092       GST_TAG_CONTAINER_FORMAT, "AVI", NULL);
6093
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;
6098 }
6099
6100 static void
6101 gst_avi_demux_loop (GstPad * pad)
6102 {
6103   GstFlowReturn res;
6104   GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
6105
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));
6111         goto pause;
6112       }
6113       avi->state = GST_AVI_DEMUX_HEADER;
6114       /* fall-through */
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));
6119         goto pause;
6120       }
6121 #ifdef DIVX_DRM
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);
6125 #endif
6126
6127       avi->state = GST_AVI_DEMUX_MOVI;
6128       break;
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;
6133       }
6134       if (G_UNLIKELY (avi->seg_event)) {
6135         gst_avi_demux_push_event (avi, avi->seg_event);
6136         avi->seg_event = NULL;
6137       }
6138       if (G_UNLIKELY (avi->got_tags)) {
6139         push_tag_lists (avi);
6140       }
6141       /* process each index entry in turn */
6142       res = gst_avi_demux_loop_data (avi);
6143
6144       /* pause when error */
6145       if (G_UNLIKELY (res != GST_FLOW_OK)) {
6146         GST_INFO ("stream_movi flow: %s", gst_flow_get_name (res));
6147         goto pause;
6148       }
6149       break;
6150     default:
6151       GST_ERROR_OBJECT (avi, "unknown state %d", avi->state);
6152       res = GST_FLOW_ERROR;
6153       goto pause;
6154   }
6155
6156   return;
6157
6158   /* ERRORS */
6159 pause:{
6160
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);
6165
6166
6167     if (res == GST_FLOW_UNEXPECTED) {
6168       /* handle end-of-stream/segment */
6169       if (avi->segment.flags & GST_SEEK_FLAG_SEGMENT) {
6170         gint64 stop;
6171
6172         if ((stop = avi->segment.stop) == -1)
6173           stop = avi->segment.duration;
6174
6175         GST_INFO_OBJECT (avi, "sending segment_done");
6176
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));
6181       } else {
6182         push_eos = TRUE;
6183       }
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)));
6191       push_eos = TRUE;
6192     }
6193     if (push_eos) {
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)"));
6199       }
6200     }
6201   }
6202 }
6203
6204
6205 static GstFlowReturn
6206 gst_avi_demux_chain (GstPad * pad, GstBuffer * buf)
6207 {
6208   GstFlowReturn res;
6209   GstAviDemux *avi = GST_AVI_DEMUX (GST_PAD_PARENT (pad));
6210   gint i;
6211
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;
6218   }
6219
6220   GST_DEBUG ("Store %d bytes in adapter", GST_BUFFER_SIZE (buf));
6221   gst_adapter_push (avi->adapter, buf);
6222
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));
6227         break;
6228       }
6229       break;
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));
6233         break;
6234       }
6235       break;
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;
6240       }
6241       if (G_UNLIKELY (avi->seg_event)) {
6242         gst_avi_demux_push_event (avi, avi->seg_event);
6243         avi->seg_event = NULL;
6244       }
6245       if (G_UNLIKELY (avi->got_tags)) {
6246         push_tag_lists (avi);
6247       }
6248       res = gst_avi_demux_stream_data (avi);
6249       break;
6250     case GST_AVI_DEMUX_SEEK:
6251     {
6252       GstEvent *event;
6253
6254       res = GST_FLOW_OK;
6255
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 */
6259         goto index_failed;
6260
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);
6264
6265       if (avi->have_index) {
6266         /* use the indexes now to construct nice durations */
6267         gst_avi_demux_calculate_durations_from_index (avi);
6268       } else {
6269         /* still parsing indexes */
6270         break;
6271       }
6272
6273       GST_OBJECT_LOCK (avi);
6274       event = avi->seek_event;
6275       avi->seek_event = NULL;
6276       GST_OBJECT_UNLOCK (avi);
6277
6278       /* calculate and perform seek */
6279       if (!avi_demux_handle_seek_push (avi, avi->sinkpad, event))
6280         goto seek_failed;
6281
6282       gst_event_unref (event);
6283       avi->state = GST_AVI_DEMUX_MOVI;
6284       break;
6285     }
6286     default:
6287       GST_ELEMENT_ERROR (avi, STREAM, FAILED, (NULL),
6288           ("Illegal internal state"));
6289       res = GST_FLOW_ERROR;
6290       break;
6291   }
6292
6293   GST_DEBUG_OBJECT (avi, "state: %d res:%s", avi->state,
6294       gst_flow_get_name (res));
6295
6296   if (G_UNLIKELY (avi->abort_buffering))
6297     goto abort_buffering;
6298
6299   return res;
6300
6301   /* ERRORS */
6302 index_failed:
6303   {
6304     GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("failed to read indexes"));
6305     return GST_FLOW_ERROR;
6306   }
6307 seek_failed:
6308   {
6309     GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("push mode seek failed"));
6310     return GST_FLOW_ERROR;
6311   }
6312 abort_buffering:
6313   {
6314     avi->abort_buffering = FALSE;
6315     GST_ELEMENT_ERROR (avi, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
6316     return GST_FLOW_ERROR;
6317   }
6318 }
6319
6320 static gboolean
6321 gst_avi_demux_sink_activate (GstPad * sinkpad)
6322 {
6323   if (gst_pad_check_pull_range (sinkpad)) {
6324     GST_DEBUG ("going to pull mode");
6325     return gst_pad_activate_pull (sinkpad, TRUE);
6326   } else {
6327     GST_DEBUG ("going to push (streaming) mode");
6328     return gst_pad_activate_push (sinkpad, TRUE);
6329   }
6330 }
6331
6332 static gboolean
6333 gst_avi_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
6334 {
6335   GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (sinkpad));
6336
6337   if (active) {
6338     avi->segment_running = TRUE;
6339     avi->streaming = FALSE;
6340     return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_avi_demux_loop,
6341         sinkpad);
6342   } else {
6343     avi->segment_running = FALSE;
6344     return gst_pad_stop_task (sinkpad);
6345   }
6346 }
6347
6348 static gboolean
6349 gst_avi_demux_activate_push (GstPad * pad, gboolean active)
6350 {
6351   GstAviDemux *avi = GST_AVI_DEMUX (GST_OBJECT_PARENT (pad));
6352
6353   if (active) {
6354     GST_DEBUG ("avi: activating push/chain function");
6355     avi->streaming = TRUE;
6356 #if 0
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");
6362     }
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),
6366         &avi->index_id);
6367 #endif
6368   } else {
6369     GST_DEBUG ("avi: deactivating push/chain function");
6370   }
6371
6372   return TRUE;
6373 }
6374
6375 static void
6376 gst_avi_demux_set_index (GstElement * element, GstIndex * index)
6377 {
6378   GstAviDemux *avi = GST_AVI_DEMUX (element);
6379
6380   GST_OBJECT_LOCK (avi);
6381   if (avi->element_index)
6382     gst_object_unref (avi->element_index);
6383   if (index) {
6384     avi->element_index = gst_object_ref (index);
6385   } else {
6386     avi->element_index = NULL;
6387   }
6388   GST_OBJECT_UNLOCK (avi);
6389   /* object lock might be taken again */
6390   if (index)
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);
6393 }
6394
6395 static GstIndex *
6396 gst_avi_demux_get_index (GstElement * element)
6397 {
6398   GstIndex *result = NULL;
6399   GstAviDemux *avi = GST_AVI_DEMUX (element);
6400
6401   GST_OBJECT_LOCK (avi);
6402   if (avi->element_index)
6403     result = gst_object_ref (avi->element_index);
6404   GST_OBJECT_UNLOCK (avi);
6405
6406   GST_DEBUG_OBJECT (avi, "Returning index %" GST_PTR_FORMAT, result);
6407
6408   return result;
6409 }
6410
6411 static GstStateChangeReturn
6412 gst_avi_demux_change_state (GstElement * element, GstStateChange transition)
6413 {
6414   GstStateChangeReturn ret;
6415   GstAviDemux *avi = GST_AVI_DEMUX (element);
6416
6417   switch (transition) {
6418     case GST_STATE_CHANGE_READY_TO_PAUSED:
6419       avi->streaming = FALSE;
6420       gst_segment_init (&avi->segment, GST_FORMAT_TIME);
6421       break;
6422     default:
6423       break;
6424   }
6425
6426   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
6427   if (ret == GST_STATE_CHANGE_FAILURE)
6428     goto done;
6429
6430   switch (transition) {
6431     case GST_STATE_CHANGE_PAUSED_TO_READY:
6432       avi->have_index = FALSE;
6433       gst_avi_demux_reset (avi);
6434       break;
6435     default:
6436       break;
6437   }
6438
6439 done:
6440   return ret;
6441 }