gstavidemux.c 45.8 KB
Newer Older
1
2
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@temple-baptist.com>
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


21
/* #define GST_DEBUG_ENABLED */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
22
23
24
25
26
27
28
#include <string.h>

#include "gstavidemux.h"


/* elementfactory information */
static GstElementDetails gst_avi_demux_details = {
29
30
31
  "Avi demuxer",
  "Codec/Demuxer",
  "Demultiplex an avi file into audio and video",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
32
33
34
35
36
37
  VERSION,
  "Erik Walthinsen <omega@cse.ogi.edu>\n"
  "Wim Taymans <wim.taymans@tvd.be>",
  "(C) 1999",
};

38
static GstCaps* avi_type_find (GstBuffer *buf, gpointer private);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
39
40
41
42
43
44

/* typefactory for 'avi' */
static GstTypeDefinition avidefinition = {
  "avidemux_video/avi",
  "video/avi",
  ".avi",
45
  avi_type_find,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
};

/* AviDemux signals and args */
enum {
  /* FILL ME */
  LAST_SIGNAL
};

enum {
  ARG_0,
  ARG_BITRATE,
  /* FILL ME */
};

60
GST_PAD_TEMPLATE_FACTORY (sink_templ,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
61
62
63
64
65
66
  "sink",
  GST_PAD_SINK,
  GST_PAD_ALWAYS,
  GST_CAPS_NEW (
    "avidemux_sink",
     "video/avi",
67
      "format",    GST_PROPS_STRING ("AVI")
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
68
69
70
  )
)

71
GST_PAD_TEMPLATE_FACTORY (src_video_templ,
Wim Taymans's avatar
Wim Taymans committed
72
  "video_%02d",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
73
  GST_PAD_SRC,
74
  GST_PAD_SOMETIMES,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
75
  GST_CAPS_NEW (
Ronald S. Bultje's avatar
Ronald S. Bultje committed
76
    "avidemux_src_video",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
77
78
79
80
    "video/avi",
      "format",  GST_PROPS_LIST (
	           GST_PROPS_STRING ("strf_vids"),
	           GST_PROPS_STRING ("strf_iavs")
Ronald S. Bultje's avatar
Ronald S. Bultje committed
81
82
83
84
      		 ),
      "width",   GST_PROPS_INT_RANGE (16, 4096),
      "height",  GST_PROPS_INT_RANGE (16, 4096)

Ronald S. Bultje's avatar
Ronald S. Bultje committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  ),
  GST_CAPS_NEW (
    "avidemux_src_video",
    "video/raw",
      "format",  GST_PROPS_LIST (
                   GST_PROPS_FOURCC (GST_MAKE_FOURCC('Y','U','Y','2')),
                   GST_PROPS_FOURCC (GST_MAKE_FOURCC('I','4','2','0'))
                 ),
      "width",          GST_PROPS_INT_RANGE (16, 4096),
      "height",         GST_PROPS_INT_RANGE (16, 4096)
  ),
  GST_CAPS_NEW (
    "avidemux_src_video",
    "video/jpeg",
      "width",   GST_PROPS_INT_RANGE (16, 4096),
      "height",  GST_PROPS_INT_RANGE (16, 4096)
  ),
  GST_CAPS_NEW (
    "avidemux_src_video",
    "video/dv",
      "format",  GST_PROPS_LIST (
                   GST_PROPS_STRING ("NTSC"),
                   GST_PROPS_STRING ("PAL")
Ronald S. Bultje's avatar
Ronald S. Bultje committed
108
109
110
                 ),
      "width",   GST_PROPS_INT_RANGE (16, 4096),
      "height",  GST_PROPS_INT_RANGE (16, 4096)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
111
112
113
  )
)

114
GST_PAD_TEMPLATE_FACTORY (src_audio_templ,
Wim Taymans's avatar
Wim Taymans committed
115
  "audio_%02d",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
116
  GST_PAD_SRC,
117
  GST_PAD_SOMETIMES,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
118
  GST_CAPS_NEW (
Ronald S. Bultje's avatar
Ronald S. Bultje committed
119
    "avidemux_src_audio",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
120
    "video/avi",
Ronald S. Bultje's avatar
Ronald S. Bultje committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
      "format",  GST_PROPS_STRING ("strf_auds")
  ),
  GST_CAPS_NEW (
    "avidemux_src_audio",
    "audio/raw",
      "format",           GST_PROPS_STRING ("int"),
      "law",              GST_PROPS_INT (0),
      "endianness",       GST_PROPS_INT (G_BYTE_ORDER),
      "signed",           GST_PROPS_LIST (
      			    GST_PROPS_BOOLEAN (TRUE),
      			    GST_PROPS_BOOLEAN (FALSE)
			  ),
      "width",            GST_PROPS_LIST (
      			    GST_PROPS_INT (8),
      			    GST_PROPS_INT (16)
			  ),
      "depth",            GST_PROPS_LIST (
      			    GST_PROPS_INT (8),
      			    GST_PROPS_INT (16)
			  ),
      "rate",             GST_PROPS_INT_RANGE (11025, 44100),
      "channels",         GST_PROPS_INT_RANGE (1, 2)
  ),
  GST_CAPS_NEW (
    "avidemux_src_audio",
    "audio/mp3",
      NULL
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
148
149
150
151
152
153
154
155
  )
)

static void 	gst_avi_demux_class_init	(GstAviDemuxClass *klass);
static void 	gst_avi_demux_init		(GstAviDemux *avi_demux);

static void 	gst_avi_demux_loop 		(GstElement *element);

156
157
158
159
static gboolean gst_avi_demux_process_chunk 	(GstAviDemux *avi_demux, guint64 *filepos,
			    			 guint32 desired_tag,
			    			 gint rec_depth, guint32 *chunksize);

160
161
static gboolean gst_avi_demux_send_event 	(GstElement *element, GstEvent *event);

Wim Taymans's avatar
Wim Taymans committed
162
163
164
static gboolean gst_avi_demux_handle_src_event 	(GstPad *pad, GstEvent *event);
static gboolean gst_avi_demux_handle_src_query 	(GstPad *pad, GstPadQueryType type, 
						 GstFormat *format, gint64 *value);
165
166
static gboolean gst_avi_demux_src_convert 	(GstPad *pad, GstFormat src_format, gint64 src_value,
	                  			 GstFormat *dest_format, gint64 *dest_value);
Wim Taymans's avatar
Wim Taymans committed
167

Wim Taymans's avatar
Wim Taymans committed
168
169
170
static GstElementStateReturn
		gst_avi_demux_change_state 	(GstElement *element);

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
171
172
173
174
175
static void     gst_avi_demux_get_property      (GObject *object, guint prop_id, 	
						 GValue *value, GParamSpec *pspec);


static GstElementClass *parent_class = NULL;
176
/*static guint gst_avi_demux_signals[LAST_SIGNAL] = { 0 }; */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210

GType
gst_avi_demux_get_type(void) 
{
  static GType avi_demux_type = 0;

  if (!avi_demux_type) {
    static const GTypeInfo avi_demux_info = {
      sizeof(GstAviDemuxClass),      
      NULL,
      NULL,
      (GClassInitFunc)gst_avi_demux_class_init,
      NULL,
      NULL,
      sizeof(GstAviDemux),
      0,
      (GInstanceInitFunc)gst_avi_demux_init,
    };
    avi_demux_type = g_type_register_static(GST_TYPE_ELEMENT, "GstAviDemux", &avi_demux_info, 0);
  }
  return avi_demux_type;
}

static void
gst_avi_demux_class_init (GstAviDemuxClass *klass) 
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass*)klass;
  gstelement_class = (GstElementClass*)klass;

  g_object_class_install_property (G_OBJECT_CLASS(klass), ARG_BITRATE,
    g_param_spec_long ("bitrate","bitrate","bitrate",
211
                       G_MINLONG, G_MAXLONG, 0, G_PARAM_READABLE)); /* CHECKME */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
212
213
214
215

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
  
  gobject_class->get_property = gst_avi_demux_get_property;
Wim Taymans's avatar
Wim Taymans committed
216
217
  
  gstelement_class->change_state = gst_avi_demux_change_state;
218
  gstelement_class->send_event = gst_avi_demux_send_event;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
219
220
221
222
223
224
225
226
}

static void 
gst_avi_demux_init (GstAviDemux *avi_demux) 
{
  GST_FLAG_SET (avi_demux, GST_ELEMENT_EVENT_AWARE);
				
  avi_demux->sinkpad = gst_pad_new_from_template (
227
		  GST_PAD_TEMPLATE_GET (sink_templ), "sink");
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
228
229
230
231
  gst_element_add_pad (GST_ELEMENT (avi_demux), avi_demux->sinkpad);

  gst_element_set_loop_function (GST_ELEMENT (avi_demux), gst_avi_demux_loop);

Wim Taymans's avatar
Wim Taymans committed
232
233
234
  avi_demux->num_streams = 0;
  avi_demux->num_v_streams = 0;
  avi_demux->num_a_streams = 0;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
235
236
  avi_demux->index_entries = NULL;
  avi_demux->index_size = 0;
Wim Taymans's avatar
Wim Taymans committed
237
  avi_demux->seek_pending = 0;
238
  avi_demux->restart = FALSE;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
239
240
241
242

}

static GstCaps*
243
avi_type_find (GstBuffer *buf,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
244
245
246
247
248
              gpointer private)
{
  gchar *data = GST_BUFFER_DATA (buf);
  GstCaps *new;

249
  GST_DEBUG (0,"avi_demux: typefind");
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
250
251
252
253
254
255

  if (GUINT32_FROM_LE (((guint32 *)data)[0]) != GST_RIFF_TAG_RIFF)
    return NULL;
  if (GUINT32_FROM_LE (((guint32 *)data)[2]) != GST_RIFF_RIFF_AVI)
    return NULL;

256
  new = GST_CAPS_NEW ("avi_type_find",
257
		      "video/avi", 
258
		        "format", GST_PROPS_STRING ("AVI"));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
259
260
261
262
263
264
265
266

  return new;
}

static gboolean
gst_avi_demux_avih (GstAviDemux *avi_demux)
{
  gst_riff_avih *avih;
267
  guint32       got_bytes;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
268
269
  GstByteStream *bs = avi_demux->bs;

270
271
  got_bytes = gst_bytestream_peek_bytes (bs, (guint8**)&avih, sizeof (gst_riff_avih));
  if (got_bytes == sizeof (gst_riff_avih)) {
Wim Taymans's avatar
Wim Taymans committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
    avi_demux->avih.us_frame 	= GUINT32_FROM_LE (avih->us_frame);
    avi_demux->avih.max_bps 	= GUINT32_FROM_LE (avih->max_bps);
    avi_demux->avih.pad_gran 	= GUINT32_FROM_LE (avih->pad_gran);
    avi_demux->avih.flags 	= GUINT32_FROM_LE (avih->flags);
    avi_demux->avih.tot_frames 	= GUINT32_FROM_LE (avih->tot_frames);
    avi_demux->avih.init_frames = GUINT32_FROM_LE (avih->init_frames);
    avi_demux->avih.streams 	= GUINT32_FROM_LE (avih->streams);
    avi_demux->avih.bufsize 	= GUINT32_FROM_LE (avih->bufsize);
    avi_demux->avih.width 	= GUINT32_FROM_LE (avih->width);
    avi_demux->avih.height 	= GUINT32_FROM_LE (avih->height);
    avi_demux->avih.scale 	= GUINT32_FROM_LE (avih->scale);
    avi_demux->avih.rate 	= GUINT32_FROM_LE (avih->rate);
    avi_demux->avih.start 	= GUINT32_FROM_LE (avih->start);
    avi_demux->avih.length 	= GUINT32_FROM_LE (avih->length);

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
287
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: avih tag found");
Wim Taymans's avatar
Wim Taymans committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  us_frame    %d", avi_demux->avih.us_frame);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  max_bps     %d", avi_demux->avih.max_bps);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  pad_gran    %d", avi_demux->avih.pad_gran);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  flags       0x%08x", avi_demux->avih.flags);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  tot_frames  %d", avi_demux->avih.tot_frames);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  init_frames %d", avi_demux->avih.init_frames);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  streams     %d", avi_demux->avih.streams);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  bufsize     %d", avi_demux->avih.bufsize);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  width       %d", avi_demux->avih.width);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  height      %d", avi_demux->avih.height);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  scale       %d", avi_demux->avih.scale);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  rate        %d", avi_demux->avih.rate);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  start       %d", avi_demux->avih.start);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  length      %d", avi_demux->avih.length);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
302
303
304
305
306
307
308
309
310
311
312

    return TRUE;
  }
  return FALSE;
}

static gboolean 
gst_avi_demux_strh (GstAviDemux *avi_demux)
{
  gst_riff_strh *strh;
  GstByteStream *bs = avi_demux->bs;
313
  guint32       got_bytes;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
314

315
316
  got_bytes = gst_bytestream_peek_bytes (bs, (guint8**)&strh, sizeof (gst_riff_strh));
  if (got_bytes == sizeof (gst_riff_strh)) {
Wim Taymans's avatar
Wim Taymans committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
    avi_stream_context *target;

    avi_demux->fcc_type = GUINT32_FROM_LE (strh->type);

    target = &avi_demux->stream[avi_demux->num_streams];

    target->num = avi_demux->num_streams;

    target->strh.type 		= avi_demux->fcc_type;
    target->strh.fcc_handler 	= GUINT32_FROM_LE (strh->fcc_handler);
    target->strh.flags		= GUINT32_FROM_LE (strh->flags);
    target->strh.priority	= GUINT32_FROM_LE (strh->priority);
    target->strh.init_frames	= GUINT32_FROM_LE (strh->init_frames);
    target->strh.scale		= GUINT32_FROM_LE (strh->scale);
    target->strh.rate		= GUINT32_FROM_LE (strh->rate);
    target->strh.start		= GUINT32_FROM_LE (strh->start);
    target->strh.length		= GUINT32_FROM_LE (strh->length);
    target->strh.bufsize	= GUINT32_FROM_LE (strh->bufsize);
    target->strh.quality	= GUINT32_FROM_LE (strh->quality);
    target->strh.samplesize	= GUINT32_FROM_LE (strh->samplesize);

    if (!target->strh.scale)
      target->strh.scale = 1; /* avoid division by zero */
    if (!target->strh.rate)
      target->strh.rate = 1; /* avoid division by zero */

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
343
344
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: strh tag found");
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  type        0x%08x (%s)", 
Wim Taymans's avatar
Wim Taymans committed
345
  		  target->strh.type, gst_riff_id_to_fourcc (strh->type));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
346
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  fcc_handler 0x%08x (%s)", 
Wim Taymans's avatar
Wim Taymans committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
		  target->strh.fcc_handler, gst_riff_id_to_fourcc (strh->fcc_handler));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  flags       0x%08x", strh->flags);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  priority    %d", target->strh.priority);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  init_frames %d", target->strh.init_frames);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  scale       %d", target->strh.scale);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  rate        %d", target->strh.rate);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  start       %d", target->strh.start);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  length      %d", target->strh.length);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  bufsize     %d", target->strh.bufsize);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  quality     %d", target->strh.quality);
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  samplesize  %d", target->strh.samplesize);

    target->delay = 0LL;
    target->total_bytes = 0LL;
    target->total_frames = 0;

    target->skip = 0;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
364

365
366
    avi_demux->avih.bufsize = MAX (avi_demux->avih.bufsize, target->strh.bufsize);

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
367
368
369
370
371
372
373
374
375
376
377
    return TRUE;
  }
  return FALSE;
}

static void 
gst_avi_demux_strf_vids (GstAviDemux *avi_demux)
{
  gst_riff_strf_vids *strf;
  GstPad *srcpad;
  GstByteStream *bs = avi_demux->bs;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
378
  GstCaps *newcaps = NULL, *capslist = NULL;
Wim Taymans's avatar
Wim Taymans committed
379
  avi_stream_context *stream;
380
  guint32       got_bytes;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
381

382
  got_bytes = gst_bytestream_peek_bytes (bs, (guint8**)&strf, sizeof (gst_riff_strf_vids));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398

  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: strf tag found in context vids");
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  size        %d", GUINT32_FROM_LE (strf->size));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  width       %d", GUINT32_FROM_LE (strf->width));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  height      %d", GUINT32_FROM_LE (strf->height));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  planes      %d", GUINT16_FROM_LE (strf->planes));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  bit_cnt     %d", GUINT16_FROM_LE (strf->bit_cnt));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  compression 0x%08x (%s)", 
		  GUINT32_FROM_LE (strf->compression), gst_riff_id_to_fourcc (strf->compression));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  image_size  %d", GUINT32_FROM_LE (strf->image_size));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  xpels_meter %d", GUINT32_FROM_LE (strf->xpels_meter));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  ypels_meter %d", GUINT32_FROM_LE (strf->ypels_meter));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  num_colors  %d", GUINT32_FROM_LE (strf->num_colors));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  imp_colors  %d", GUINT32_FROM_LE (strf->imp_colors));

  srcpad =  gst_pad_new_from_template (
399
		  GST_PAD_TEMPLATE_GET (src_video_templ), g_strdup_printf ("video_%02d", 
Wim Taymans's avatar
Wim Taymans committed
400
			  avi_demux->num_v_streams));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
401

Ronald S. Bultje's avatar
Ronald S. Bultje committed
402
403
  capslist = gst_caps_append(NULL, GST_CAPS_NEW (
			  "avidemux_video_src",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
404
405
406
407
408
409
410
411
412
413
414
415
			  "video/avi",
			    "format",    	GST_PROPS_STRING ("strf_vids"),
			      "size", 		GST_PROPS_INT (GUINT32_FROM_LE (strf->size)),
			      "width", 		GST_PROPS_INT (GUINT32_FROM_LE (strf->width)),
			      "height", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->height)),
			      "planes", 	GST_PROPS_INT (GUINT16_FROM_LE (strf->planes)),
			      "bit_cnt", 	GST_PROPS_INT (GUINT16_FROM_LE (strf->bit_cnt)),
			      "compression", 	GST_PROPS_FOURCC (GUINT32_FROM_LE (strf->compression)),
			      "image_size", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->image_size)),
			      "xpels_meter", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->xpels_meter)),
			      "ypels_meter", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->ypels_meter)),
			      "num_colors", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->num_colors)),
416
417
			      "imp_colors", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->imp_colors))
			      ));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
418

Ronald S. Bultje's avatar
Ronald S. Bultje committed
419
  /* let's try some gstreamer-like mime-type caps */
Ronald S. Bultje's avatar
Ronald S. Bultje committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
  switch (GUINT32_FROM_LE(strf->compression))
  {
    case GST_MAKE_FOURCC('I','4','2','0'):
    case GST_MAKE_FOURCC('Y','U','Y','2'):
      newcaps = GST_CAPS_NEW (
                  "avidemux_video_src",
                  "video/raw",
                    "format",  GST_PROPS_FOURCC(GUINT32_FROM_LE(strf->compression)),
                    "width",   GST_PROPS_INT(strf->width),
                    "height",  GST_PROPS_INT(strf->height)
                );
      break;
    case GST_MAKE_FOURCC('M','J','P','G'):
      newcaps = GST_CAPS_NEW (
                  "avidemux_video_src",
                  "video/jpeg",
                    "width",   GST_PROPS_INT(strf->width),
                    "height",  GST_PROPS_INT(strf->height)
                );
      break;
    case GST_MAKE_FOURCC('d','v','s','d'):
      newcaps = GST_CAPS_NEW (
                  "avidemux_video_src",
                  "video/dv",
                    "format",  GST_PROPS_STRING("NTSC"), /* FIXME??? */
                    "width",   GST_PROPS_INT(strf->width),
                    "height",  GST_PROPS_INT(strf->height)
                );
      break;
  }

Wim Taymans's avatar
Wim Taymans committed
451
  if (newcaps) capslist = gst_caps_append (capslist, newcaps);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
452

Wim Taymans's avatar
Wim Taymans committed
453
454
455
  gst_pad_try_set_caps (srcpad, capslist);
  gst_pad_set_event_function (srcpad, gst_avi_demux_handle_src_event);
  gst_pad_set_query_function (srcpad, gst_avi_demux_handle_src_query);
456
  gst_pad_set_convert_function (srcpad, gst_avi_demux_src_convert);
Wim Taymans's avatar
Wim Taymans committed
457
458
459
460
461
462

  stream = &avi_demux->stream[avi_demux->num_streams];
  stream->pad = srcpad;
  gst_pad_set_element_private (srcpad, stream);
  avi_demux->num_streams++;
  avi_demux->num_v_streams++;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
463

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
464
465
466
467
468
469
470
471
472
  gst_element_add_pad (GST_ELEMENT (avi_demux), srcpad);
}

static void 
gst_avi_demux_strf_auds (GstAviDemux *avi_demux)
{
  gst_riff_strf_auds *strf;
  GstPad *srcpad;
  GstByteStream *bs = avi_demux->bs;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
473
  GstCaps *newcaps = NULL, *capslist = NULL;
Wim Taymans's avatar
Wim Taymans committed
474
  avi_stream_context *stream;
475
  guint32       got_bytes;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
476

477
  got_bytes = gst_bytestream_peek_bytes (bs, (guint8**)&strf, sizeof (gst_riff_strf_auds));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
478
479
480
481
482
483
484
485
486
487

  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: strf tag found in context auds");
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  format      %d", GUINT16_FROM_LE (strf->format));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  channels    %d", GUINT16_FROM_LE (strf->channels));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  rate        %d", GUINT32_FROM_LE (strf->rate));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  av_bps      %d", GUINT32_FROM_LE (strf->av_bps));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  blockalign  %d", GUINT16_FROM_LE (strf->blockalign));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  size        %d", GUINT16_FROM_LE (strf->size));

  srcpad =  gst_pad_new_from_template (
488
		  GST_PAD_TEMPLATE_GET (src_audio_templ), g_strdup_printf ("audio_%02d", 
Wim Taymans's avatar
Wim Taymans committed
489
			  avi_demux->num_a_streams));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
490

Ronald S. Bultje's avatar
Ronald S. Bultje committed
491
492
  capslist = gst_caps_append(NULL, GST_CAPS_NEW (
			  "avidemux_audio_src",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
493
494
495
496
497
498
499
			  "video/avi",
			    "format",    	GST_PROPS_STRING ("strf_auds"),
			      "fmt", 		GST_PROPS_INT (GUINT16_FROM_LE (strf->format)),
			      "channels", 	GST_PROPS_INT (GUINT16_FROM_LE (strf->channels)),
			      "rate", 		GST_PROPS_INT (GUINT32_FROM_LE (strf->rate)),
			      "av_bps",		GST_PROPS_INT (GUINT32_FROM_LE (strf->av_bps)),
			      "blockalign",	GST_PROPS_INT (GUINT16_FROM_LE (strf->blockalign)),
500
501
			      "size",		GST_PROPS_INT (GUINT16_FROM_LE (strf->size))
			  ));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
502

Ronald S. Bultje's avatar
Ronald S. Bultje committed
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
  /* let's try some gstreamer-formatted mime types */
  switch (GUINT16_FROM_LE(strf->format))
  {
    case 0x0050:
    case 0x0055: /* mp3 */
      newcaps = gst_caps_new ("avidemux_audio_src",
                              "audio/mp3",
                                NULL);
      break;
    case 0x0001: /* PCM/wav */
      newcaps = gst_caps_new ("avidemux_audio_src",
                              "audio/raw",
                              gst_props_new (
                                "format",     GST_PROPS_STRING ("int"),
                                "law",        GST_PROPS_INT (0),
                                "endianness", GST_PROPS_INT (G_BYTE_ORDER),
                                "signed",     GST_PROPS_BOOLEAN ((GUINT16_FROM_LE (strf->size) != 8)),
                                "width",      GST_PROPS_INT ((GUINT16_FROM_LE (strf->blockalign)*8) /
Wim Taymans's avatar
Wim Taymans committed
521
                                                              GUINT16_FROM_LE (strf->channels)),
Ronald S. Bultje's avatar
Ronald S. Bultje committed
522
523
524
525
526
527
528
529
530
531
                                "depth",      GST_PROPS_INT (GUINT16_FROM_LE (strf->size)),
                                "rate",       GST_PROPS_INT (GUINT32_FROM_LE (strf->rate)),
                                "channels",   GST_PROPS_INT (GUINT16_FROM_LE (strf->channels)),
                                NULL
                              ));
      break;
  }

  if (newcaps) capslist = gst_caps_append(capslist, newcaps);

Wim Taymans's avatar
Wim Taymans committed
532

Ronald S. Bultje's avatar
Ronald S. Bultje committed
533
  gst_pad_try_set_caps(srcpad, capslist);
Wim Taymans's avatar
Wim Taymans committed
534
535
  gst_pad_set_event_function (srcpad, gst_avi_demux_handle_src_event);
  gst_pad_set_query_function (srcpad, gst_avi_demux_handle_src_query);
536
  gst_pad_set_convert_function (srcpad, gst_avi_demux_src_convert);
Wim Taymans's avatar
Wim Taymans committed
537
538
539
540
541
542

  stream = &avi_demux->stream[avi_demux->num_streams];
  stream->pad = srcpad;
  gst_pad_set_element_private (srcpad, stream);
  avi_demux->num_streams++;
  avi_demux->num_a_streams++;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
543

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
544
545
546
547
548
549
550
551
552
  gst_element_add_pad (GST_ELEMENT (avi_demux), srcpad);
}

static void 
gst_avi_demux_strf_iavs (GstAviDemux *avi_demux)
{
  gst_riff_strf_iavs *strf;
  GstPad *srcpad;
  GstByteStream *bs = avi_demux->bs;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
553
  GstCaps *newcaps = NULL, *capslist = NULL;
Wim Taymans's avatar
Wim Taymans committed
554
  avi_stream_context *stream;
555
  guint32       got_bytes;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
556

557
  got_bytes = gst_bytestream_peek_bytes (bs, (guint8**)&strf, sizeof (gst_riff_strf_iavs));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
558
559
560
561
562
563
564
565
566
567
568
569

  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: strf tag found in context iavs");
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  DVAAuxSrc   %08x", GUINT32_FROM_LE (strf->DVAAuxSrc));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  DVAAuxCtl   %08x", GUINT32_FROM_LE (strf->DVAAuxCtl));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  DVAAuxSrc1  %08x", GUINT32_FROM_LE (strf->DVAAuxSrc1));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  DVAAuxCtl1  %08x", GUINT32_FROM_LE (strf->DVAAuxCtl1));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  DVVAuxSrc   %08x", GUINT32_FROM_LE (strf->DVVAuxSrc));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  DVVAuxCtl   %08x", GUINT32_FROM_LE (strf->DVVAuxCtl));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  DVReserved1 %08x", GUINT32_FROM_LE (strf->DVReserved1));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  DVReserved2 %08x", GUINT32_FROM_LE (strf->DVReserved2));

  srcpad =  gst_pad_new_from_template (
570
		  GST_PAD_TEMPLATE_GET (src_video_templ), g_strdup_printf ("video_%02d", 
Wim Taymans's avatar
Wim Taymans committed
571
			  avi_demux->num_v_streams));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
572

Ronald S. Bultje's avatar
Ronald S. Bultje committed
573
574
  capslist = gst_caps_append(NULL, GST_CAPS_NEW (
			  "avidemux_video_src",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
575
576
577
578
579
580
581
582
583
			  "video/avi",
			    "format",    	GST_PROPS_STRING ("strf_iavs"),
                              "DVAAuxSrc", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVAAuxSrc)),
                              "DVAAuxCtl", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVAAuxCtl)),
                              "DVAAuxSrc1", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVAAuxSrc1)),
                              "DVAAuxCtl1", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVAAuxCtl1)),
                              "DVVAuxSrc", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVVAuxSrc)),
                              "DVVAuxCtl", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVVAuxCtl)),
                              "DVReserved1", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVReserved1)),
584
585
                              "DVReserved2", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVReserved2))
			 ));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
586

Ronald S. Bultje's avatar
Ronald S. Bultje committed
587
588
589
590
591
592
593
594
  newcaps = gst_caps_new ("avi_type_dv", 
                          "video/dv", 
                          gst_props_new (
                            "format",		GST_PROPS_STRING ("NTSC"), /* FIXME??? */
                            NULL));

  if (newcaps) capslist = gst_caps_append(capslist, newcaps);

Ronald S. Bultje's avatar
Ronald S. Bultje committed
595
  gst_pad_try_set_caps(srcpad, capslist);
Wim Taymans's avatar
Wim Taymans committed
596
597
  gst_pad_set_event_function (srcpad, gst_avi_demux_handle_src_event);
  gst_pad_set_query_function (srcpad, gst_avi_demux_handle_src_query);
598
  gst_pad_set_convert_function (srcpad, gst_avi_demux_src_convert);
Wim Taymans's avatar
Wim Taymans committed
599
600
601
602
603
604

  stream = &avi_demux->stream[avi_demux->num_streams];
  stream->pad = srcpad;
  gst_pad_set_element_private (srcpad, stream);
  avi_demux->num_streams++;
  avi_demux->num_v_streams++;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
605

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
606
607
608
609
  gst_element_add_pad (GST_ELEMENT (avi_demux), srcpad);
}

static void
Wim Taymans's avatar
Wim Taymans committed
610
611
gst_avi_debug_entry (const gchar *prefix, gst_avi_index_entry *entry)
{
Wim Taymans's avatar
Wim Taymans committed
612
  GST_DEBUG (0, "%s: %05d %d %08llx %05d %14lld %08x %08x (%d) %08x", prefix, entry->index_nr, entry->stream_nr, 
Wim Taymans's avatar
Wim Taymans committed
613
614
		  entry->bytes_before, entry->frames_before, entry->ts, entry->flags, entry->offset, 
		  entry->offset, entry->size);
Wim Taymans's avatar
Wim Taymans committed
615
616
617
618
}

static void
gst_avi_demux_parse_index (GstAviDemux *avi_demux,
Wim Taymans's avatar
Wim Taymans committed
619
		          gulong filepos, gulong offset)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
620
621
622
{
  GstBuffer *buf;
  gulong index_size;
623
  guint32 got_bytes;
Wim Taymans's avatar
Wim Taymans committed
624
625
  gint i;
  gst_riff_index_entry *entry;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
626

Wim Taymans's avatar
Wim Taymans committed
627
  if (!gst_bytestream_seek (avi_demux->bs, filepos + offset, GST_SEEK_METHOD_SET)) {
Wim Taymans's avatar
Wim Taymans committed
628
629
630
    GST_INFO (GST_CAT_PLUGIN_INFO, "avidemux: could not seek to index");
    return;
  }
631
632
  got_bytes = gst_bytestream_read (avi_demux->bs, &buf, 8);
  while (got_bytes < 8) {
Wim Taymans's avatar
Wim Taymans committed
633
634
635
636
    guint32 remaining;
    GstEvent *event;
  
    gst_bytestream_get_status (avi_demux->bs, &remaining, &event);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
637

638
    got_bytes = gst_bytestream_read (avi_demux->bs, &buf, 8);
Wim Taymans's avatar
Wim Taymans committed
639
640
641
  }
		  
  if (GST_BUFFER_OFFSET (buf) != filepos + offset || GST_BUFFER_SIZE (buf) != 8) {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
642
    GST_INFO (GST_CAT_PLUGIN_INFO, "avidemux: could not get index");
Wim Taymans's avatar
Wim Taymans committed
643
    goto end;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
644
645
646
647
  }

  if (gst_riff_fourcc_to_id (GST_BUFFER_DATA (buf)) != GST_RIFF_TAG_idx1) {
    GST_INFO (GST_CAT_PLUGIN_INFO, "avidemux: no index found");
Wim Taymans's avatar
Wim Taymans committed
648
    goto end;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
649
650
651
  }

  index_size = GUINT32_FROM_LE(*(guint32 *)(GST_BUFFER_DATA (buf) + 4));
Wim Taymans's avatar
Wim Taymans committed
652
  gst_buffer_unref (buf);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
653

654
655
  gst_bytestream_size_hint (avi_demux->bs, index_size);

656
  got_bytes = gst_bytestream_read (avi_demux->bs, &buf, index_size);
Wim Taymans's avatar
Wim Taymans committed
657
658
659
660
  if (got_bytes < index_size) {
    GST_INFO (GST_CAT_PLUGIN_INFO, "avidemux: error reading index");
    goto end;
  }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
661
662
663
664

  avi_demux->index_size = index_size/sizeof(gst_riff_index_entry);
  GST_INFO (GST_CAT_PLUGIN_INFO, "avidemux: index size %lu", avi_demux->index_size);

Wim Taymans's avatar
Wim Taymans committed
665
666
667
668
669
670
671
672
  avi_demux->index_entries = g_malloc (avi_demux->index_size * sizeof (gst_avi_index_entry));

  entry = (gst_riff_index_entry *) GST_BUFFER_DATA (buf);

  for (i = 0; i < avi_demux->index_size; i++) {
    avi_stream_context *stream;
    gint stream_nr;
    gst_avi_index_entry *target = &avi_demux->index_entries[i];
673
    GstFormat format;
Wim Taymans's avatar
Wim Taymans committed
674
675

    stream_nr = CHUNKID_TO_STREAMNR (entry[i].id);
Wim Taymans's avatar
Wim Taymans committed
676
677
678
679
680
    if (stream_nr > avi_demux->num_streams || stream_nr < 0) {
      avi_demux->index_entries[i].stream_nr = -1;
      continue;
    }

Wim Taymans's avatar
Wim Taymans committed
681
682
683
684
685
686
687
    target->stream_nr = stream_nr;
    stream = &avi_demux->stream[stream_nr];

    target->index_nr = i;
    target->flags    = entry[i].flags;
    target->size     = entry[i].size;
    target->offset   = entry[i].offset;
Wim Taymans's avatar
Wim Taymans committed
688

689
690
691
692
693
694
695
696
    /* figure out if the index is 0 based or relative to the MOVI start */
    if (i == 0) {
      if (target->offset < filepos)
	avi_demux->index_offset = filepos;
      else
	avi_demux->index_offset = 0;
    }

Wim Taymans's avatar
Wim Taymans committed
697
698
699
    target->bytes_before = stream->total_bytes;
    target->frames_before = stream->total_frames;

700
    format = GST_FORMAT_TIME;
Wim Taymans's avatar
Wim Taymans committed
701
    if (stream->strh.type == GST_RIFF_FCC_auds) {
Wim Taymans's avatar
Wim Taymans committed
702
703
      /* all audio frames are keyframes */
      target->flags |= GST_RIFF_IF_KEYFRAME;
704
    }
Wim Taymans's avatar
Wim Taymans committed
705
      
706
707
    /* constant rate stream */
    if (stream->strh.samplesize) {
708
709
      gst_pad_convert (stream->pad, GST_FORMAT_BYTES, stream->total_bytes,
		                 &format, &target->ts);
Wim Taymans's avatar
Wim Taymans committed
710
    }
711
    /* VBR stream */
Wim Taymans's avatar
Wim Taymans committed
712
    else {
713
714
      gst_pad_convert (stream->pad, GST_FORMAT_UNITS, stream->total_frames,
		                 &format, &target->ts);
Wim Taymans's avatar
Wim Taymans committed
715
716
717
718
719
720
    }
    gst_avi_debug_entry ("index", target);

    stream->total_bytes += target->size;
    stream->total_frames++;
  }
721
722
723
724
725
726
  for (i = 0; i < avi_demux->num_streams; i++) {
    avi_stream_context *stream;

    stream = &avi_demux->stream[i];
    GST_DEBUG (GST_CAT_PLUGIN_INFO, "stream %i: %d frames, %lld bytes", i, stream->total_frames, stream->total_bytes);
  }
Wim Taymans's avatar
Wim Taymans committed
727
  gst_buffer_unref (buf);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
728

Wim Taymans's avatar
Wim Taymans committed
729
end:
730
  GST_DEBUG (GST_CAT_PLUGIN_INFO, "index offset at %08lx", filepos);
Wim Taymans's avatar
Wim Taymans committed
731
732

  if (!gst_bytestream_seek (avi_demux->bs, filepos, GST_SEEK_METHOD_SET)) {
Wim Taymans's avatar
Wim Taymans committed
733
734
    GST_INFO (GST_CAT_PLUGIN_INFO, "avidemux: could not seek back to movi");
    return;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
735
736
737
  }
}

Wim Taymans's avatar
Wim Taymans committed
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
static gst_avi_index_entry*
gst_avi_demux_index_next (GstAviDemux *avi_demux, gint stream_nr, gint start, guint32 flags)
{
  gint i;
  gst_avi_index_entry *entry = NULL;

  for (i = start; i < avi_demux->index_size; i++) {
    entry = &avi_demux->index_entries[i];

    if (entry->stream_nr == stream_nr && (entry->flags & flags) == flags) {
      break;
    }
  }

  return entry;
}

static gst_avi_index_entry*
gst_avi_demux_index_entry_for_time (GstAviDemux *avi_demux, gint stream_nr, guint64 time, guint32 flags)
{
  gst_avi_index_entry *entry = NULL, *last_entry = NULL;
  gint i;

  i = -1;
  do {
    entry = gst_avi_demux_index_next (avi_demux, stream_nr, i + 1, flags);
    if (!entry)
      return NULL;

    i = entry->index_nr;

    if (entry->ts <= time) {
      last_entry = entry;
    }
  }
  while (entry->ts <= time);

  return last_entry;
}

778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
static gboolean
gst_avi_demux_src_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
	                   GstFormat *dest_format, gint64 *dest_value)
{
  gboolean res = TRUE;
  avi_stream_context *stream = gst_pad_get_element_private (pad);

  switch (src_format) {
    case GST_FORMAT_TIME:
      switch (*dest_format) {
	case GST_FORMAT_BYTES:
          if (stream->strh.type == GST_RIFF_FCC_auds)
            *dest_value = src_value * stream->strh.rate / (stream->strh.scale * GST_SECOND);
	  else
	    res = FALSE;
          break;
        case GST_FORMAT_DEFAULT:
          *dest_format = GST_FORMAT_UNITS;
	case GST_FORMAT_UNITS:
          *dest_value = src_value * stream->strh.rate / (stream->strh.scale * GST_SECOND);
          break;
	default:
	  res = FALSE;
	  break;
      }
      break;
    case GST_FORMAT_BYTES:
      switch (*dest_format) {
	case GST_FORMAT_TIME:
          if (stream->strh.type == GST_RIFF_FCC_auds)
808
            *dest_value = src_value * GST_SECOND * stream->strh.scale  / stream->strh.rate;
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
	  else
	    res = FALSE;
	default:
	  res = FALSE;
	  break;
      }
      break;
    case GST_FORMAT_UNITS:
      switch (*dest_format) {
	case GST_FORMAT_TIME:
          *dest_value = src_value * GST_SECOND * stream->strh.scale / stream->strh.rate;
	  break;
	default:
	  res = FALSE;
	  break;
      }
      break;
    default:
      res = FALSE;
  }

  return res;
}

Wim Taymans's avatar
Wim Taymans committed
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
static gboolean
gst_avi_demux_handle_src_query (GstPad *pad, GstPadQueryType type, 
				GstFormat *format, gint64 *value)
{
  gboolean res = TRUE;
  //GstAviDemux *avi_demux = GST_AVI_DEMUX (gst_pad_get_parent (pad));
  avi_stream_context *stream = gst_pad_get_element_private (pad);

  switch (type) {
    case GST_PAD_QUERY_TOTAL:
      switch (*format) {
        case GST_FORMAT_DEFAULT:
          *format = GST_FORMAT_TIME;
          /* fall through */
        case GST_FORMAT_TIME:
848
          *value = GST_SECOND * stream->strh.scale * stream->strh.length / stream->strh.rate;
Wim Taymans's avatar
Wim Taymans committed
849
850
851
852
853
854
855
	  break;
        case GST_FORMAT_BYTES:
          if (stream->strh.type == GST_RIFF_FCC_auds)
            *value = stream->strh.length;
	  else
	    res = FALSE;
	  break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
856
        case GST_FORMAT_UNITS:
Wim Taymans's avatar
Wim Taymans committed
857
858
          if (stream->strh.type == GST_RIFF_FCC_auds)
            *value = stream->strh.length * stream->strh.samplesize;
859
	  else if (stream->strh.type == GST_RIFF_FCC_vids)
Wim Taymans's avatar
Wim Taymans committed
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
            *value = stream->strh.length;
	  else
	    res = FALSE;
	  break;
	default:
          res = FALSE;
	  break;
      }
      break;
    case GST_PAD_QUERY_POSITION:
      switch (*format) {
        case GST_FORMAT_DEFAULT:
          *format = GST_FORMAT_TIME;
          /* fall through */
        case GST_FORMAT_TIME:
875
          if (stream->strh.samplesize) {
876
            *value = stream->current_byte * GST_SECOND * stream->strh.scale / stream->strh.rate;
877
878
879
880
	  }
	  else {
            *value = stream->current_frame * GST_SECOND * stream->strh.scale / stream->strh.rate;
	  }
Wim Taymans's avatar
Wim Taymans committed
881
882
883
884
	  break;
        case GST_FORMAT_BYTES:
          *value = stream->current_byte;
	  break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
885
        case GST_FORMAT_UNITS:
886
          if (stream->strh.samplesize) 
Wim Taymans's avatar
Wim Taymans committed
887
            *value = stream->current_byte * stream->strh.samplesize;
888
	  else 
Wim Taymans's avatar
Wim Taymans committed
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
            *value = stream->current_frame;
	  break;
	default:
          res = FALSE;
	  break;
      }
      break;
    default:
      res = FALSE;
      break;
  }

  return res;
}

static gint32
gst_avi_demux_sync_streams (GstAviDemux *avi_demux, guint64 time)
{
  gint i;
Wim Taymans's avatar
Wim Taymans committed
908
  guint32 min_index = G_MAXUINT;
Wim Taymans's avatar
Wim Taymans committed
909
910
911
912
913
914
  avi_stream_context *stream;
  gst_avi_index_entry *entry;

  for (i = 0; i < avi_demux->num_streams; i++) {
    stream = &avi_demux->stream[i];

915
    GST_DEBUG (0, "finding %d for time %lld", i, time);
Wim Taymans's avatar
Wim Taymans committed
916
917
918
919
920
921
922
923

    entry = gst_avi_demux_index_entry_for_time (avi_demux, stream->num, time, GST_RIFF_IF_KEYFRAME);
    if (entry) {
      gst_avi_debug_entry ("sync entry", entry);

      min_index = MIN (entry->index_nr, min_index);
    }
  }
Wim Taymans's avatar
Wim Taymans committed
924
  GST_DEBUG (0, "first index at %d", min_index);
Wim Taymans's avatar
Wim Taymans committed
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
  
  /* now we know the entry we need to sync on. calculate number of frames to
   * skip fro there on and the stream stats */
  for (i = 0; i < avi_demux->num_streams; i++) {
    gst_avi_index_entry *next_entry;
    stream = &avi_demux->stream[i];

    /* next entry */
    next_entry = gst_avi_demux_index_next (avi_demux, stream->num, min_index, 0);
    /* next entry with keyframe */
    entry = gst_avi_demux_index_next (avi_demux, stream->num, min_index, GST_RIFF_IF_KEYFRAME);
    gst_avi_debug_entry ("final sync", entry);

    stream->current_byte = next_entry->bytes_before;
    stream->current_frame = next_entry->frames_before;
    stream->skip = entry->frames_before - next_entry->frames_before;

942
    GST_DEBUG (0, "%d skip %d", stream->num, stream->skip);
Wim Taymans's avatar
Wim Taymans committed
943
  }
Wim Taymans's avatar
Wim Taymans committed
944
  GST_DEBUG (0, "final index at %d", min_index);
Wim Taymans's avatar
Wim Taymans committed
945
946
947
948

  return min_index;
}

949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
static gboolean
gst_avi_demux_send_event (GstElement *element, GstEvent *event)
{
  GList *pads;

  pads = gst_element_get_pad_list (element);

  while (pads) { 
    GstPad *pad = GST_PAD (pads->data);

    if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
      return gst_avi_demux_handle_src_event (pad, event);
    }
    
    pads = g_list_next (pads);
  }
  
  return FALSE;
}

Wim Taymans's avatar
Wim Taymans committed
969
970
971
972
973
974
975
976
977
978
979
980
981
static gboolean
gst_avi_demux_handle_src_event (GstPad *pad, GstEvent *event)
{
  gboolean res = TRUE;
  GstAviDemux *avi_demux = GST_AVI_DEMUX (gst_pad_get_parent (pad));
  avi_stream_context *stream;
  
  stream = gst_pad_get_element_private (pad);

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_SEEK:
      switch (GST_EVENT_SEEK_FORMAT (event)) {
	case GST_FORMAT_BYTES:
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
982
	case GST_FORMAT_UNITS:
Wim Taymans's avatar
Wim Taymans committed
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
	  break;
	case GST_FORMAT_TIME:
        {
	  gst_avi_index_entry *seek_entry, *entry;
	  gint64 desired_offset = GST_EVENT_SEEK_OFFSET (event);
	  guint32 flags;
          guint64 min_index;

	  /* no seek on audio yet */
	  if (stream->strh.type == GST_RIFF_FCC_auds)
	    return FALSE;

          flags = GST_RIFF_IF_KEYFRAME;

          entry = gst_avi_demux_index_entry_for_time (avi_demux, stream->num, desired_offset, GST_RIFF_IF_KEYFRAME);
	  if (entry) {
            desired_offset = entry->ts;
	    min_index = gst_avi_demux_sync_streams (avi_demux, desired_offset);
            seek_entry = &avi_demux->index_entries[min_index];
	    
Wim Taymans's avatar
Wim Taymans committed
1003
1004
            gst_avi_debug_entry ("syncing to entry", seek_entry);
	    
Wim Taymans's avatar
Wim Taymans committed
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
	    avi_demux->seek_offset = seek_entry->offset + avi_demux->index_offset;
            avi_demux->seek_pending = TRUE;
	    avi_demux->last_seek = seek_entry->ts;
	  }
	  else {
	    res = FALSE;
	  }
	  break;
	}
	default:
	  res = FALSE;
          break;
      }
      break;
    default:
      res = FALSE;
      break;
  }
  
  return res;
}

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1027
static gboolean
Wim Taymans's avatar
Wim Taymans committed
1028
gst_avi_demux_handle_sink_event (GstAviDemux *avi_demux)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
{
  guint32 remaining;
  GstEvent *event;
  GstEventType type;
  
  gst_bytestream_get_status (avi_demux->bs, &remaining, &event);

  type = event? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;

  switch (type) {
    case GST_EVENT_EOS:
Wim Taymans's avatar
Wim Taymans committed
1040
      gst_pad_event_default (avi_demux->sinkpad, event);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1041
1042
      break;
    case GST_EVENT_FLUSH:
1043
      g_warning ("flush event");
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1044
1045
      break;
    case GST_EVENT_DISCONTINUOUS:
Wim Taymans's avatar
Wim Taymans committed
1046
1047
1048
1049
1050
1051
    {
      gint i;

      for (i = 0; i < avi_demux->num_streams; i++) {
        avi_stream_context *stream = &avi_demux->stream[i];

Wim Taymans's avatar
Wim Taymans committed
1052
1053
	GST_DEBUG (GST_CAT_EVENT, "sending discont on %d %lld + %lld = %lld", i, 
			avi_demux->last_seek, stream->delay, avi_demux->last_seek + stream->delay);
Wim Taymans's avatar
Wim Taymans committed
1054
        event = gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME, 
Wim Taymans's avatar
Wim Taymans committed
1055
			avi_demux->last_seek + stream->delay , NULL);
Wim Taymans's avatar
Wim Taymans committed
1056
1057
	gst_pad_push (stream->pad, GST_BUFFER (event));
      }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1058
      break;
Wim Taymans's avatar
Wim Taymans committed
1059
    }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1060
    default:
1061
      g_warning ("unhandled event %d", type);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1062
1063
1064
1065
1066
1067
      break;
  }

  return TRUE;
}

Wim Taymans's avatar
Wim Taymans committed
1068
static inline gboolean 
Wim Taymans's avatar
Wim Taymans committed
1069
gst_avi_demux_read_chunk (GstAviDemux *avi_demux, guint32 *id, guint32 *size)
Wim Taymans's avatar
Wim Taymans committed
1070
1071
1072
{
  gst_riff_chunk *chunk;
  GstByteStream *bs = avi_demux->bs;
1073
  guint32       got_bytes;
Wim Taymans's avatar
Wim Taymans committed
1074

Wim Taymans's avatar
Wim Taymans committed
1075
  do {
1076
1077
    got_bytes = gst_bytestream_peek_bytes (bs, (guint8**)&chunk, sizeof (gst_riff_chunk));
    if (got_bytes == sizeof (gst_riff_chunk)) {
Wim Taymans's avatar
Wim Taymans committed
1078
1079
      *id =   GUINT32_FROM_LE (chunk->id);
      *size = GUINT32_FROM_LE (chunk->size);
Wim Taymans's avatar
Wim Taymans committed
1080

Wim Taymans's avatar
Wim Taymans committed
1081
      gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
Wim Taymans's avatar
Wim Taymans committed
1082

Wim Taymans's avatar
Wim Taymans committed
1083
1084
      return TRUE;
    }
Wim Taymans's avatar
Wim Taymans committed
1085
  } while (gst_avi_demux_handle_sink_event (avi_demux));
Wim Taymans's avatar
Wim Taymans committed
1086
1087

  return TRUE;
Wim Taymans's avatar
Wim Taymans committed
1088
1089
}

1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
static gboolean
gst_avi_demux_process_movi (GstAviDemux *avi_demux, gint rec_depth, guint64 *filepos)
{
  guint32 subchunksize = 0;

  while (!avi_demux->restart) {	/* while not showed all: */
    if (avi_demux->seek_pending) {
      GST_DEBUG (0, "avidemux: seek pending to %lld %08llx", avi_demux->seek_offset, avi_demux->seek_offset);
      if (!gst_bytestream_seek (avi_demux->bs, avi_demux->seek_offset, GST_SEEK_METHOD_SET)) {
        GST_INFO (GST_CAT_PLUGIN_INFO, "avidemux: could not seek");
      }
      else {
        *filepos = avi_demux->seek_offset;
      }
      avi_demux->seek_pending = FALSE;
    }

    GST_INFO (GST_CAT_PLUGIN_INFO, "process chunk filepos %08llx", *filepos);
    /* recurse for subchunks of RIFF and LIST chunks: */
    if (!gst_avi_demux_process_chunk (avi_demux, filepos, 0,
			   rec_depth + 1, &subchunksize)) {
      return FALSE;
    }
    /* we are running in an infinite loop, we need to _yield 
     * from time to time */
    gst_element_yield (GST_ELEMENT (avi_demux));
  }
  return TRUE;
}

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1120
static gboolean
Wim Taymans's avatar
Wim Taymans committed
1121
gst_avi_demux_process_chunk (GstAviDemux *avi_demux, guint64 *filepos,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1122
1123
1124
1125
1126
1127
			     guint32 desired_tag,
			     gint rec_depth, guint32 *chunksize)
{
  guint32 chunkid;	
  GstByteStream *bs = avi_demux->bs;

Wim Taymans's avatar
Wim Taymans committed
1128
  if (!gst_avi_demux_read_chunk (avi_demux, &chunkid, chunksize)) {
1129
    g_print ("  *****  Error reading chunk at filepos 0x%08llx\n", *filepos);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1130
1131
1132
1133
    return FALSE;
  }
  if (desired_tag) {		/* do we have to test identity? */
    if (desired_tag != chunkid) {
Wim Taymans's avatar
Wim Taymans committed
1134
1135
1136
      g_print ("\n\n *** Error: Expected chunk '%s', found '%s'\n",
	      gst_riff_id_to_fourcc (desired_tag),
	      gst_riff_id_to_fourcc (chunkid));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1137
1138
1139
1140
1141
      return FALSE;
    }
  }

  GST_INFO (GST_CAT_PLUGIN_INFO, "chunkid %s, size %08x, filepos %08llx", 
1142
		  gst_riff_id_to_fourcc (chunkid), *chunksize, *filepos);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1143

1144
  *filepos += (sizeof (guint32) + sizeof (guint32));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1145
1146
1147
1148
1149
1150

  switch (chunkid) {
    case GST_RIFF_TAG_RIFF:
    case GST_RIFF_TAG_LIST:
    {
      guint32 datashowed;
1151
      guint32 subchunksize = 0;	/* size of a read subchunk             */
Wim Taymans's avatar
Wim Taymans committed
1152
      gchar *formtype;
1153
      guint32       got_bytes;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1154

1155
1156
      got_bytes = gst_bytestream_peek_bytes (bs, (guint8**)&formtype, sizeof (guint32));
      if (got_bytes < sizeof(guint32))
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1157
1158
	return FALSE;

Wim Taymans's avatar
Wim Taymans committed
1159
1160
      switch (GUINT32_FROM_LE (*((guint32*)formtype))) {
	case GST_RIFF_LIST_movi:
Wim Taymans's avatar
Wim Taymans committed
1161
	  gst_avi_demux_parse_index (avi_demux, *filepos, *chunksize);
Wim Taymans's avatar
Wim Taymans committed
1162
1163
1164
1165
1166
1167
          while (!gst_bytestream_flush (bs, sizeof (guint32))) {
            guint32 remaining;
            GstEvent *event;

            gst_bytestream_get_status (avi_demux->bs, &remaining, &event);
	  }
1168
1169
	  if  (avi_demux->avih.bufsize)
	    gst_bytestream_size_hint (avi_demux->bs, avi_demux->avih.bufsize);
1170
1171
1172

	  gst_avi_demux_process_movi (avi_demux, rec_depth, filepos);
	  goto done;
Wim Taymans's avatar
Wim Taymans committed
1173
1174
1175
1176
1177
1178
	default:
          /* flush the form type */
          gst_bytestream_flush_fast (bs, sizeof (guint32));
          break;
      }

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1179
      datashowed = sizeof (guint32);	/* we showed the form type      */
1180
      *filepos += datashowed;	/* for the rest of the routine  */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1181
1182
1183

      while (datashowed < *chunksize) {	/* while not showed all: */

Wim Taymans's avatar
Wim Taymans committed
1184
        GST_INFO (GST_CAT_PLUGIN_INFO, "process chunk filepos %08llx", *filepos);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1185
	/* recurse for subchunks of RIFF and LIST chunks: */
Wim Taymans's avatar
Wim Taymans committed
1186
1187
1188
	if (!gst_avi_demux_process_chunk (avi_demux, filepos, 0,
			   rec_depth + 1, &subchunksize)) {

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1189
1190
          g_print ("  *****  Error processing chunk at filepos 0x%08llxi %u %u\n",
                   *filepos, *chunksize, datashowed);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1191
	  return FALSE;
Wim Taymans's avatar
Wim Taymans committed
1192
	}
1193
1194
	if (avi_demux->restart)
          goto done;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1195

1196
	subchunksize = ((subchunksize + 1) & ~1);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1197

1198
	datashowed += (sizeof (guint32) + sizeof (guint32) + subchunksize);
Wim Taymans's avatar
Wim Taymans committed
1199
1200
        GST_INFO (GST_CAT_PLUGIN_INFO, "process chunk done filepos %08llx, subchunksize %08x", 
			*filepos, subchunksize);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1201
      }
1202
      if (datashowed != *chunksize) {
Wim Taymans's avatar
Wim Taymans committed
1203
	g_warning ("error parsing AVI %u %u", datashowed, *chunksize);
1204
1205
      }
      goto done;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
    }
    case GST_RIFF_TAG_avih:
      gst_avi_demux_avih (avi_demux);
      break;
    case GST_RIFF_TAG_strh:
    {
      gst_avi_demux_strh (avi_demux);
      break;
    }
    case GST_RIFF_TAG_strf:
      switch (avi_demux->fcc_type) {
        case GST_RIFF_FCC_vids:
          gst_avi_demux_strf_vids (avi_demux);
	  break;
        case GST_RIFF_FCC_auds:
          gst_avi_demux_strf_auds (avi_demux);
	  break;
        case GST_RIFF_FCC_iavs:
          gst_avi_demux_strf_iavs (avi_demux);
	  break;
	case GST_RIFF_FCC_pads:
	case GST_RIFF_FCC_txts:
	default:
Wim Taymans's avatar
Wim Taymans committed
1229
1230
          GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux_chain: strh type %s not supported", 
			  gst_riff_id_to_fourcc (avi_demux->fcc_type));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1231
1232
1233
1234
1235
1236
1237
1238
	  break;
      }
      break;
    case GST_RIFF_00dc:
    case GST_RIFF_00db:
    case GST_RIFF_00__:
    case GST_RIFF_01wb:
    {
Wim Taymans's avatar
Wim Taymans committed
1239
1240
      gint stream_id;
      avi_stream_context *stream;
1241
1242
      gint64 next_ts;
      GstFormat format;
Wim Taymans's avatar
Wim Taymans committed
1243
1244
1245
1246
1247
      
      stream_id = CHUNKID_TO_STREAMNR (chunkid);
		   
      stream = &avi_demux->stream[stream_id];

1248
      GST_DEBUG (0,"gst_avi_demux_chain: tag found %08x size %08x",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1249
1250
		    chunkid, *chunksize);

1251
1252
1253
      format = GST_FORMAT_TIME;
      gst_pad_query (stream->pad, GST_PAD_QUERY_POSITION, &format, &next_ts);

Wim Taymans's avatar
Wim Taymans committed
1254
      if (stream->strh.init_frames == stream->current_frame && stream->delay==0)
1255
	stream->delay = next_ts;
Wim Taymans's avatar
Wim Taymans committed
1256

Wim Taymans's avatar
Wim Taymans committed
1257
1258
1259
      stream->current_frame++;
      stream->current_byte += *chunksize;

Wim Taymans's avatar
Wim Taymans committed
1260
1261
1262
1263
1264
1265
1266
      if (stream->skip) {
	stream->skip--;
      }
      else {
        if (GST_PAD_IS_CONNECTED (stream->pad)) {
	  GstBuffer *buf;
          guint32   got_bytes;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1267

Wim Taymans's avatar
Wim Taymans committed
1268
1269
	  if (*chunksize) {
            got_bytes = gst_bytestream_peek (bs, &buf, *chunksize);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1270

1271
            GST_BUFFER_TIMESTAMP (buf) = next_ts;
Wim Taymans's avatar
Wim Taymans committed
1272

Wim Taymans's avatar
Wim Taymans committed
1273
1274
1275
1276
            if (stream->need_flush) {
               /* FIXME, do some flush event here */
              stream->need_flush = FALSE;
            }
Wim Taymans's avatar
Wim Taymans committed
1277
	    GST_DEBUG (0, "send stream %d: %lld %d %lld %08x", stream_id, next_ts, stream->current_frame - 1,
Wim Taymans's avatar
Wim Taymans committed
1278
			  stream->delay, *chunksize);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1279

Wim Taymans's avatar
Wim Taymans committed
1280
1281
1282
            gst_pad_push(stream->pad, buf);
	  }
        }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1283
      }
Wim Taymans's avatar
Wim Taymans committed
1284

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1285
1286
1287
1288
      *chunksize = (*chunksize + 1) & ~1;
      break;
    }
    default:
1289
      GST_DEBUG (0, "  *****  unknown chunkid %08x (%s)", chunkid, gst_riff_id_to_fourcc (chunkid));
1290
      *chunksize = (*chunksize + 1) & ~1;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1291
1292
1293
      break;
  }
  GST_INFO (GST_CAT_PLUGIN_INFO, "chunkid %s, flush %08x, filepos %08llx", 
1294
		  gst_riff_id_to_fourcc (chunkid), *chunksize, *filepos);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1295

Wim Taymans's avatar
Wim Taymans committed
1296
  *filepos += *chunksize;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1297
  if (!gst_bytestream_flush (bs, *chunksize)) {
Wim Taymans's avatar
Wim Taymans committed
1298
    return gst_avi_demux_handle_sink_event (avi_demux);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1299
1300
  }

1301
done:
Wim Taymans's avatar
Wim Taymans committed
1302

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1303
1304
1305
1306
1307
1308
1309
1310
  return TRUE;
}

static void
gst_avi_demux_loop (GstElement *element)
{
  GstAviDemux *avi_demux;
  guint32 chunksize;
1311
  guint64 filepos = 0;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1312
1313
1314
1315
1316
1317

  g_return_if_fail (element != NULL);
  g_return_if_fail (GST_IS_AVI_DEMUX (element));

  avi_demux = GST_AVI_DEMUX (element);

1318
1319
  avi_demux->restart = FALSE;

Wim Taymans's avatar
Wim Taymans committed
1320
  /* this is basically an infinite loop */
Wim Taymans's avatar
Wim Taymans committed
1321
  if (!gst_avi_demux_process_chunk (avi_demux, &filepos, GST_RIFF_TAG_RIFF, 0, &chunksize)) {
Wim Taymans's avatar
Wim Taymans committed
1322
    gst_element_error (element, "This doesn't appear to be an AVI file");
Wim Taymans's avatar
Wim Taymans committed
1323
    return;
Wim Taymans's avatar
Wim Taymans committed
1324
  }
1325
1326
1327
  if (!avi_demux->restart)
    /* if we exit the loop we are EOS */
    gst_pad_event_default (avi_demux->sinkpad, gst_event_new (GST_EVENT_EOS));