gstavidemux.c 33.5 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
};

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

enum {
  ARG_0,
  ARG_BITRATE,
  ARG_MEDIA_TIME,
  ARG_CURRENT_TIME,
59
  ARG_FRAME_RATE,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
60
61
62
  /* FILL ME */
};

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

74
GST_PAD_TEMPLATE_FACTORY (src_video_templ,
Wim Taymans's avatar
Wim Taymans committed
75
  "video_%02d",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
76
  GST_PAD_SRC,
77
  GST_PAD_SOMETIMES,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
78
  GST_CAPS_NEW (
Ronald S. Bultje's avatar
Ronald S. Bultje committed
79
    "avidemux_src_video",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
80
81
82
83
    "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
84
85
86
87
      		 ),
      "width",   GST_PROPS_INT_RANGE (16, 4096),
      "height",  GST_PROPS_INT_RANGE (16, 4096)

Ronald S. Bultje's avatar
Ronald S. Bultje committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  ),
  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
111
112
113
                 ),
      "width",   GST_PROPS_INT_RANGE (16, 4096),
      "height",  GST_PROPS_INT_RANGE (16, 4096)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
114
115
116
  )
)

117
GST_PAD_TEMPLATE_FACTORY (src_audio_templ,
Wim Taymans's avatar
Wim Taymans committed
118
  "audio_%02d",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
119
  GST_PAD_SRC,
120
  GST_PAD_SOMETIMES,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
121
  GST_CAPS_NEW (
Ronald S. Bultje's avatar
Ronald S. Bultje committed
122
    "avidemux_src_audio",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
123
    "video/avi",
Ronald S. Bultje's avatar
Ronald S. Bultje committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
      "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
151
152
153
154
155
156
157
158
  )
)

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);

Wim Taymans's avatar
Wim Taymans committed
159
160
161
static GstElementStateReturn
		gst_avi_demux_change_state 	(GstElement *element);

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
162
163
164
165
166
static void     gst_avi_demux_get_property      (GObject *object, guint prop_id, 	
						 GValue *value, GParamSpec *pspec);


static GstElementClass *parent_class = NULL;
167
/*static guint gst_avi_demux_signals[LAST_SIGNAL] = { 0 }; */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
168
169
170
171
172
173
174
175
176
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

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",
202
                       G_MINLONG, G_MAXLONG, 0, G_PARAM_READABLE)); /* CHECKME */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
203
204
  g_object_class_install_property (G_OBJECT_CLASS(klass), ARG_MEDIA_TIME,
    g_param_spec_long ("media_time","media_time","media_time",
205
                       G_MINLONG, G_MAXLONG, 0, G_PARAM_READABLE)); /* CHECKME */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
206
207
  g_object_class_install_property (G_OBJECT_CLASS(klass), ARG_CURRENT_TIME,
    g_param_spec_long ("current_time","current_time","current_time",
208
                       G_MINLONG, G_MAXLONG, 0, G_PARAM_READABLE)); /* CHECKME */
209
  g_object_class_install_property (G_OBJECT_CLASS(klass), ARG_FRAME_RATE,
210
211
    g_param_spec_int ("frame-rate","frame rate","Current (non-averaged) frame rate",
                      0, G_MAXINT, 0, G_PARAM_READABLE));
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;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
218
219
220
221
222
223
224
225
226
227
}

static void 
gst_avi_demux_init (GstAviDemux *avi_demux) 
{
  guint i;

  GST_FLAG_SET (avi_demux, GST_ELEMENT_EVENT_AWARE);
				
  avi_demux->sinkpad = gst_pad_new_from_template (
228
		  GST_PAD_TEMPLATE_GET (sink_templ), "sink");
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
229
230
231
232
233
234
235
  gst_element_add_pad (GST_ELEMENT (avi_demux), avi_demux->sinkpad);

  gst_element_set_loop_function (GST_ELEMENT (avi_demux), gst_avi_demux_loop);

  avi_demux->state = GST_AVI_DEMUX_UNKNOWN;
  avi_demux->num_audio_pads = 0;
  avi_demux->num_video_pads = 0;
236
  /*avi_demux->next_time = 500000; */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
237
  avi_demux->next_time = 0;
Wim Taymans's avatar
Wim Taymans committed
238
  avi_demux->init_audio = 0;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
239
240
241
242
243
  avi_demux->flags = 0;
  avi_demux->index_entries = NULL;
  avi_demux->index_size = 0;
  avi_demux->resync_offset = 0;

244
  /*GST_FLAG_SET( GST_OBJECT (avi_demux), GST_ELEMENT_NO_SEEK); */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
245
246
247
248
249
250
251
252
253
254

  for(i=0; i<GST_AVI_DEMUX_MAX_AUDIO_PADS; i++) 
    avi_demux->audio_pad[i] = NULL;

  for(i=0; i<GST_AVI_DEMUX_MAX_VIDEO_PADS; i++) 
    avi_demux->video_pad[i] = NULL;

}

static GstCaps*
255
avi_type_find (GstBuffer *buf,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
256
257
258
259
260
              gpointer private)
{
  gchar *data = GST_BUFFER_DATA (buf);
  GstCaps *new;

261
  GST_DEBUG (0,"avi_demux: typefind");
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
262
263
264
265
266
267

  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;

268
  new = GST_CAPS_NEW ("avi_type_find",
269
		      "video/avi", 
270
		        "format", GST_PROPS_STRING ("AVI"));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
271
272
273
274
275
276
277
278

  return new;
}

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

282
283
  got_bytes = gst_bytestream_peek_bytes (bs, (guint8**)&avih, sizeof (gst_riff_avih));
  if (got_bytes == sizeof (gst_riff_avih)) {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: avih tag found");
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  us_frame    %d", GUINT32_FROM_LE (avih->us_frame));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  max_bps     %d", GUINT32_FROM_LE (avih->max_bps));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  pad_gran    %d", GUINT32_FROM_LE (avih->pad_gran));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  flags       0x%08x", GUINT32_FROM_LE (avih->flags));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  tot_frames  %d", GUINT32_FROM_LE (avih->tot_frames));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  init_frames %d", GUINT32_FROM_LE (avih->init_frames));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  streams     %d", GUINT32_FROM_LE (avih->streams));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  bufsize     %d", GUINT32_FROM_LE (avih->bufsize));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  width       %d", GUINT32_FROM_LE (avih->width));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  height      %d", GUINT32_FROM_LE (avih->height));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  scale       %d", GUINT32_FROM_LE (avih->scale));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  rate        %d", GUINT32_FROM_LE (avih->rate));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  start       %d", GUINT32_FROM_LE (avih->start));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  length      %d", GUINT32_FROM_LE (avih->length));

    avi_demux->time_interval = GUINT32_FROM_LE (avih->us_frame);
    avi_demux->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
    avi_demux->flags = GUINT32_FROM_LE (avih->flags);

    return TRUE;
  }
  return FALSE;
}

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

316
317
  got_bytes = gst_bytestream_peek_bytes (bs, (guint8**)&strh, sizeof (gst_riff_strh));
  if (got_bytes == sizeof (gst_riff_strh)) {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
    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)", 
  		  GUINT32_FROM_LE (strh->type), gst_riff_id_to_fourcc (strh->type));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  fcc_handler 0x%08x (%s)", 
		  GUINT32_FROM_LE (strh->fcc_handler), gst_riff_id_to_fourcc (strh->fcc_handler));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  flags       0x%08x", GUINT32_FROM_LE (strh->flags));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  priority    %d", GUINT32_FROM_LE (strh->priority));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  init_frames %d", GUINT32_FROM_LE (strh->init_frames));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  scale       %d", GUINT32_FROM_LE (strh->scale));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  rate        %d", GUINT32_FROM_LE (strh->rate));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  start       %d", GUINT32_FROM_LE (strh->start));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  length      %d", GUINT32_FROM_LE (strh->length));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  bufsize     %d", GUINT32_FROM_LE (strh->bufsize));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  quality     %d", GUINT32_FROM_LE (strh->quality));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  samplesize  %d", GUINT32_FROM_LE (strh->samplesize));

    avi_demux->fcc_type = GUINT32_FROM_LE (strh->type);
Wim Taymans's avatar
Wim Taymans committed
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
    if (strh->type == GST_RIFF_FCC_auds) {
      guint32 scale;
      
      scale = GUINT32_FROM_LE (strh->scale);
      avi_demux->init_audio = GUINT32_FROM_LE (strh->init_frames);
      if (!scale)
        scale = 1;
      avi_demux->audio_rate = GUINT32_FROM_LE (strh->rate) / scale;
    }
    else if (strh->type == GST_RIFF_FCC_vids) {
      guint32 scale;
      
      scale = GUINT32_FROM_LE (strh->scale);
      if (!scale)
        scale = 1;
350
351
      avi_demux->frame_rate = (gint) GUINT32_FROM_LE (strh->rate) / scale;
      g_object_notify (G_OBJECT (avi_demux), "frame-rate");
Wim Taymans's avatar
Wim Taymans committed
352
    }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
353
354
355
356
357
358
359
360
361
362
363
364

    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
365
  GstCaps *newcaps = NULL, *capslist = NULL;
366
  guint32       got_bytes;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
367

368
  got_bytes = gst_bytestream_peek_bytes (bs, (guint8**)&strf, sizeof (gst_riff_strf_vids));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384

  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 (
385
		  GST_PAD_TEMPLATE_GET (src_video_templ), g_strdup_printf ("video_%02d", 
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
386
387
			  avi_demux->num_video_pads));

Ronald S. Bultje's avatar
Ronald S. Bultje committed
388
389
  capslist = gst_caps_append(NULL, GST_CAPS_NEW (
			  "avidemux_video_src",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
390
391
392
393
394
395
396
397
398
399
400
401
			  "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)),
402
403
			      "imp_colors", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->imp_colors))
			      ));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
404

Ronald S. Bultje's avatar
Ronald S. Bultje committed
405
  /* let's try some gstreamer-like mime-type caps */
Ronald S. Bultje's avatar
Ronald S. Bultje committed
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
  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;
  }

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
439
  gst_pad_try_set_caps(srcpad, capslist);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
440

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
441
442
443
444
445
446
447
448
449
450
  avi_demux->video_pad[avi_demux->num_video_pads++] = srcpad;
  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
451
  GstCaps *newcaps = NULL, *capslist = NULL;
452
  guint32       got_bytes;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
453

454
  got_bytes = gst_bytestream_peek_bytes (bs, (guint8**)&strf, sizeof (gst_riff_strf_auds));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
455
456
457
458
459
460
461
462
463
464

  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 (
465
		  GST_PAD_TEMPLATE_GET (src_audio_templ), g_strdup_printf ("audio_%02d", 
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
466
467
			  avi_demux->num_audio_pads));

Ronald S. Bultje's avatar
Ronald S. Bultje committed
468
469
  capslist = gst_caps_append(NULL, GST_CAPS_NEW (
			  "avidemux_audio_src",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
470
471
472
473
474
475
476
			  "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)),
477
478
			      "size",		GST_PROPS_INT (GUINT16_FROM_LE (strf->size))
			  ));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
479

Ronald S. Bultje's avatar
Ronald S. Bultje committed
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
  /* 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) /
                                                                GUINT16_FROM_LE (strf->channels)),
                                "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);

Ronald S. Bultje's avatar
Ronald S. Bultje committed
509
  gst_pad_try_set_caps(srcpad, capslist);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
510

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
511
512
513
514
515
516
517
518
519
520
  avi_demux->audio_pad[avi_demux->num_audio_pads++] = srcpad;
  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
521
  GstCaps *newcaps = NULL, *capslist = NULL;
522
  guint32       got_bytes;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
523

524
  got_bytes = gst_bytestream_peek_bytes (bs, (guint8**)&strf, sizeof (gst_riff_strf_iavs));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
525
526
527
528
529
530
531
532
533
534
535
536

  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 (
537
		  GST_PAD_TEMPLATE_GET (src_video_templ), g_strdup_printf ("video_%02d", 
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
538
539
			  avi_demux->num_video_pads));

Ronald S. Bultje's avatar
Ronald S. Bultje committed
540
541
  capslist = gst_caps_append(NULL, GST_CAPS_NEW (
			  "avidemux_video_src",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
542
543
544
545
546
547
548
549
550
			  "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)),
551
552
                              "DVReserved2", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVReserved2))
			 ));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
553

Ronald S. Bultje's avatar
Ronald S. Bultje committed
554
555
556
557
558
559
560
561
  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
562
  gst_pad_try_set_caps(srcpad, capslist);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
563

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
564
565
566
567
568
569
  avi_demux->video_pad[avi_demux->num_video_pads++] = srcpad;
  gst_element_add_pad (GST_ELEMENT (avi_demux), srcpad);
}

static void
gst_avidemux_parse_index (GstAviDemux *avi_demux,
Wim Taymans's avatar
Wim Taymans committed
570
		          gulong filepos, gulong offset)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
571
572
573
{
  GstBuffer *buf;
  gulong index_size;
574
  guint32 got_bytes;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
575

Wim Taymans's avatar
Wim Taymans committed
576
577
578
579
  if (!gst_bytestream_seek (avi_demux->bs, GST_SEEK_BYTEOFFSET_SET, filepos + offset)) {
    GST_INFO (GST_CAT_PLUGIN_INFO, "avidemux: could not seek to index");
    return;
  }
580
581
  got_bytes = gst_bytestream_read (avi_demux->bs, &buf, 8);
  while (got_bytes < 8) {
Wim Taymans's avatar
Wim Taymans committed
582
583
584
585
    guint32 remaining;
    GstEvent *event;
  
    gst_bytestream_get_status (avi_demux->bs, &remaining, &event);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
586

587
    got_bytes = gst_bytestream_read (avi_demux->bs, &buf, 8);
Wim Taymans's avatar
Wim Taymans committed
588
589
590
  }
		  
  if (GST_BUFFER_OFFSET (buf) != filepos + offset || GST_BUFFER_SIZE (buf) != 8) {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
591
    GST_INFO (GST_CAT_PLUGIN_INFO, "avidemux: could not get index");
Wim Taymans's avatar
Wim Taymans committed
592
    goto end;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
593
594
595
596
  }

  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
597
    goto end;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
598
599
600
  }

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

603
  got_bytes = gst_bytestream_read (avi_demux->bs, &buf, index_size);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
604
605
606
607
608
609
610

  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);

  avi_demux->index_entries = g_malloc (GST_BUFFER_SIZE (buf));
  memcpy (avi_demux->index_entries, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
Wim Taymans's avatar
Wim Taymans committed
611
  gst_buffer_unref (buf);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
612

Wim Taymans's avatar
Wim Taymans committed
613
end:
Wim Taymans's avatar
Wim Taymans committed
614
615
616
  if (!gst_bytestream_seek (avi_demux->bs, GST_SEEK_BYTEOFFSET_SET, filepos)) {
    GST_INFO (GST_CAT_PLUGIN_INFO, "avidemux: could not seek back to movi");
    return;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
  }
}

static gboolean
gst_avidemux_handle_event (GstAviDemux *avi_demux)
{
  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
633
      gst_pad_event_default (avi_demux->sinkpad, event);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
      break;
    case GST_EVENT_SEEK:
      g_warning ("seek event\n");
      break;
    case GST_EVENT_FLUSH:
      g_warning ("flush event\n");
      break;
    case GST_EVENT_DISCONTINUOUS:
      g_warning ("discont event\n");
      break;
    default:
      g_warning ("unhandled event %d\n", type);
      break;
  }

  return TRUE;
}

Wim Taymans's avatar
Wim Taymans committed
652
653
654
655
656
static inline gboolean 
gst_avidemux_read_chunk (GstAviDemux *avi_demux, guint32 *id, guint32 *size)
{
  gst_riff_chunk *chunk;
  GstByteStream *bs = avi_demux->bs;
657
  guint32       got_bytes;
Wim Taymans's avatar
Wim Taymans committed
658

Wim Taymans's avatar
Wim Taymans committed
659
  do {
660
661
    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
662
663
      *id =   GUINT32_FROM_LE (chunk->id);
      *size = GUINT32_FROM_LE (chunk->size);
Wim Taymans's avatar
Wim Taymans committed
664

Wim Taymans's avatar
Wim Taymans committed
665
      gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
Wim Taymans's avatar
Wim Taymans committed
666

Wim Taymans's avatar
Wim Taymans committed
667
668
669
670
671
      return TRUE;
    }
  } while (gst_avidemux_handle_event (avi_demux));

  return TRUE;
Wim Taymans's avatar
Wim Taymans committed
672
673
}

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
674
static gboolean
675
gst_avidemux_process_chunk (GstAviDemux *avi_demux, guint64 *filepos,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
676
677
678
679
680
681
			     guint32 desired_tag,
			     gint rec_depth, guint32 *chunksize)
{
  guint32 chunkid;	
  GstByteStream *bs = avi_demux->bs;

Wim Taymans's avatar
Wim Taymans committed
682
  if (!gst_avidemux_read_chunk (avi_demux, &chunkid, chunksize)) {
683
    g_print ("  *****  Error reading chunk at filepos 0x%08llx\n", *filepos);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
684
685
686
687
    return FALSE;
  }
  if (desired_tag) {		/* do we have to test identity? */
    if (desired_tag != chunkid) {
Wim Taymans's avatar
Wim Taymans committed
688
689
690
      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
691
692
693
694
695
      return FALSE;
    }
  }

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

698
  *filepos += (sizeof (guint32) + sizeof (guint32));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
699
700
701
702
703
704

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

709
710
      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
711
712
	return FALSE;

Wim Taymans's avatar
Wim Taymans committed
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
      switch (GUINT32_FROM_LE (*((guint32*)formtype))) {
	case GST_RIFF_LIST_movi:
	  gst_avidemux_parse_index (avi_demux, *filepos, *chunksize);
          while (!gst_bytestream_flush (bs, sizeof (guint32))) {
            guint32 remaining;
            GstEvent *event;

            gst_bytestream_get_status (avi_demux->bs, &remaining, &event);
	  }
          break;
	default:
          /* flush the form type */
          gst_bytestream_flush_fast (bs, sizeof (guint32));
          break;
      }

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
729
      datashowed = sizeof (guint32);	/* we showed the form type      */
730
      *filepos += datashowed;	/* for the rest of the routine  */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
731
732
733

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

Wim Taymans's avatar
Wim Taymans committed
734
        GST_INFO (GST_CAT_PLUGIN_INFO, "process chunk filepos %08llx", *filepos);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
735
	/* recurse for subchunks of RIFF and LIST chunks: */
736
	if (!gst_avidemux_process_chunk (avi_demux, filepos, 0,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
737
738
739
			   rec_depth + 1, &subchunksize))
	  return FALSE;

740
	subchunksize = ((subchunksize + 1) & ~1);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
741

742
	datashowed += (sizeof (guint32) + sizeof (guint32) + subchunksize);
Wim Taymans's avatar
Wim Taymans committed
743
744
        GST_INFO (GST_CAT_PLUGIN_INFO, "process chunk done filepos %08llx, subchunksize %08x", 
			*filepos, subchunksize);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
745
      }
746
747
748
749
      if (datashowed != *chunksize) {
	g_warning ("error parsing AVI");
      }
      goto done;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
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
778
779
780
    }
    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:
          GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux_chain: strh type %s not supported", gst_riff_id_to_fourcc (avi_demux->fcc_type));
	  break;
      }
      break;
    case GST_RIFF_00dc:
    case GST_RIFF_00db:
    case GST_RIFF_00__:
    {
781
      GST_DEBUG (0,"gst_avi_demux_chain: tag found %08x size %08x",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
782
783
		    chunkid, *chunksize);

784
      if (GST_PAD_IS_CONNECTED (avi_demux->video_pad[0])) {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
785
	GstBuffer *buf;
786
        guint32   got_bytes;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
787
788

	if (*chunksize) {
789
          got_bytes = gst_bytestream_peek (bs, &buf, *chunksize);
Wim Taymans's avatar
Wim Taymans committed
790
791

          GST_BUFFER_TIMESTAMP (buf) = avi_demux->next_time;
Wim Taymans's avatar
Wim Taymans committed
792

Wim Taymans's avatar
Wim Taymans committed
793
794
795
796
797
798
799
          avi_demux->next_time += avi_demux->time_interval;

          if (avi_demux->video_need_flush[0]) {
             /* FIXME, do some flush event here */
            avi_demux->video_need_flush[0] = FALSE;
          }

800
          GST_DEBUG (0,"gst_avi_demux_chain: send video buffer %08x", *chunksize);
Wim Taymans's avatar
Wim Taymans committed
801
          gst_pad_push(avi_demux->video_pad[0], buf);
802
          GST_DEBUG (0,"gst_avi_demux_chain: sent video buffer %08x %p",
Wim Taymans's avatar
Wim Taymans committed
803
804
	    	      *chunksize, &avi_demux->video_pad[0]);
          avi_demux->current_frame++;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
805
806
807
808
809
810
811
	}
      }
      *chunksize = (*chunksize + 1) & ~1;
      break;
    }
    case GST_RIFF_01wb:
    {
812
      GST_DEBUG (0,"gst_avi_demux_chain: tag found %08x size %08x",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
813
814
		    chunkid, *chunksize);

Wim Taymans's avatar
Wim Taymans committed
815
      if (avi_demux->init_audio) {
816
	/*avi_demux->next_time += (*chunksize) * 1000000LL / avi_demux->audio_rate; */
Wim Taymans's avatar
Wim Taymans committed
817
818
819
	avi_demux->init_audio--;
      }

820
      if (GST_PAD_IS_CONNECTED (avi_demux->audio_pad[0])) {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
821
	GstBuffer *buf;
822
        guint32   got_bytes;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
823
824

	if (*chunksize) {
825
          got_bytes = gst_bytestream_peek (bs, &buf, *chunksize);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
826

Wim Taymans's avatar
Wim Taymans committed
827
828
          GST_BUFFER_TIMESTAMP (buf) = -1LL;

Wim Taymans's avatar
Wim Taymans committed
829
          if (avi_demux->audio_need_flush[0]) {
830
  	    GST_DEBUG (0,"audio flush");
Wim Taymans's avatar
Wim Taymans committed
831
832
833
            avi_demux->audio_need_flush[0] = FALSE;
            /* FIXME, do some flush event here */
          }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
834

835
          GST_DEBUG (0,"gst_avi_demux_chain: send audio buffer %08x", *chunksize);
Wim Taymans's avatar
Wim Taymans committed
836
          gst_pad_push (avi_demux->audio_pad[0], buf);
837
          GST_DEBUG (0,"gst_avi_demux_chain: sent audio buffer %08x", *chunksize);
Wim Taymans's avatar
Wim Taymans committed
838
	}
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
839
840
841
842
843
      }
      *chunksize = (*chunksize + 1) & ~1;
      break;
    }
    default:
844
      GST_DEBUG (0, "  *****  unknown chunkid %08x (%s)", chunkid, gst_riff_id_to_fourcc (chunkid));
845
      *chunksize = (*chunksize + 1) & ~1;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
846
847
848
      break;
  }
  GST_INFO (GST_CAT_PLUGIN_INFO, "chunkid %s, flush %08x, filepos %08llx", 
849
		  gst_riff_id_to_fourcc (chunkid), *chunksize, *filepos);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
850

Wim Taymans's avatar
Wim Taymans committed
851
  *filepos += *chunksize;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
852
853
854
855
  if (!gst_bytestream_flush (bs, *chunksize)) {
    return gst_avidemux_handle_event (avi_demux);
  }

856
done:
Wim Taymans's avatar
Wim Taymans committed
857
858
859
860
  /* we are running in an infinite loop, we need to _yield 
   * from time to time */
  gst_element_yield (GST_ELEMENT (avi_demux));

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
861
862
863
864
865
866
867
868
  return TRUE;
}

static void
gst_avi_demux_loop (GstElement *element)
{
  GstAviDemux *avi_demux;
  guint32 chunksize;
869
  guint64 filepos = 0;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
870
871
872
873
874
875

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

  avi_demux = GST_AVI_DEMUX (element);

Wim Taymans's avatar
Wim Taymans committed
876
  /* this is basically an infinite loop */
877
  if (!gst_avidemux_process_chunk (avi_demux, &filepos, GST_RIFF_TAG_RIFF, 0, &chunksize)) {
Wim Taymans's avatar
Wim Taymans committed
878
    gst_element_error (element, "This doesn't appear to be an AVI file");
Wim Taymans's avatar
Wim Taymans committed
879
    return;
Wim Taymans's avatar
Wim Taymans committed
880
  }
Wim Taymans's avatar
Wim Taymans committed
881
882
  /* if we exit the loop we are EOS */
  gst_pad_event_default (avi_demux->sinkpad, gst_event_new (GST_EVENT_EOS));
Wim Taymans's avatar
Wim Taymans committed
883
884
885
886
887
888
}

static GstElementStateReturn
gst_avi_demux_change_state (GstElement *element)
{
  GstAviDemux *avi_demux = GST_AVI_DEMUX (element);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
889

Wim Taymans's avatar
Wim Taymans committed
890
891
  switch (GST_STATE_TRANSITION (element)) {
    case GST_STATE_NULL_TO_READY:
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
892
      break;
Wim Taymans's avatar
Wim Taymans committed
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
    case GST_STATE_READY_TO_PAUSED:
      avi_demux->bs = gst_bytestream_new (avi_demux->sinkpad);
      break;
    case GST_STATE_PAUSED_TO_PLAYING:
      break;
    case GST_STATE_PLAYING_TO_PAUSED:
      break;
    case GST_STATE_PAUSED_TO_READY:
      gst_bytestream_destroy (avi_demux->bs);
      break;
    case GST_STATE_READY_TO_NULL:
      break;
    default:
      break;
  }

  parent_class->change_state (element);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
910

Wim Taymans's avatar
Wim Taymans committed
911
  return GST_STATE_SUCCESS;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
}

static void
gst_avi_demux_get_property (GObject *object, guint prop_id, GValue *value,
			    GParamSpec *pspec)
{
  GstAviDemux *src;

  g_return_if_fail (GST_IS_AVI_DEMUX (object));

  src = GST_AVI_DEMUX (object);

  switch (prop_id) {
    case ARG_BITRATE:
      break;
    case ARG_MEDIA_TIME:
      g_value_set_long (value, (src->tot_frames * src->time_interval) / 1000000);
      break;
    case ARG_CURRENT_TIME:
      g_value_set_long (value, (src->current_frame * src->time_interval) / 1000000);
      break;
933
934
935
    case ARG_FRAME_RATE:
      g_value_set_int (value, (src->frame_rate));
      break;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
    default:
      break;
  }
}

static gboolean
plugin_init (GModule *module, GstPlugin *plugin)
{
  GstElementFactory *factory;
  GstTypeFactory *type;

  /* this filter needs the riff parser */
  if (!gst_library_load ("gstbytestream")) {
    gst_info("avidemux: could not load support library: 'gstbytestream'\n");
    return FALSE;
  }
  if (!gst_library_load ("gstriff")) {
    gst_info("avidemux: could not load support library: 'gstriff'\n");
    return FALSE;
  }

  /* create an elementfactory for the avi_demux element */
958
  factory = gst_element_factory_new ("avidemux",GST_TYPE_AVI_DEMUX,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
959
960
961
                                    &gst_avi_demux_details);
  g_return_val_if_fail (factory != NULL, FALSE);

962
963
964
  gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (src_audio_templ));
  gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (src_video_templ));
  gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (sink_templ));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
965

966
  type = gst_type_factory_new (&avidefinition);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
967
968
969
970
971
972
973
974
975
976
977
978
979
980
  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (type));

  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));

  return TRUE;
}

GstPluginDesc plugin_desc = {
  GST_VERSION_MAJOR,
  GST_VERSION_MINOR,
  "avidemux",
  plugin_init
};