gstavidemux.c 59.7 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 */
22
23
24
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
25
26
27
#include <string.h>

#include "gstavidemux.h"
28
#include "gstavimux.h"
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
29
30
31
32
33
34
35
36
37
38

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

enum {
  ARG_0,
  ARG_BITRATE,
39
40
  ARG_METADATA,
  ARG_STREAMINFO,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
41
42
43
  /* FILL ME */
};

44
GST_PAD_TEMPLATE_FACTORY (sink_templ,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
45
46
47
48
49
50
  "sink",
  GST_PAD_SINK,
  GST_PAD_ALWAYS,
  GST_CAPS_NEW (
    "avidemux_sink",
    "video/avi",
Wim Taymans's avatar
Wim Taymans committed
51
      NULL
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
52
  )
53
54
);

55
static void 	gst_avi_demux_base_init	        (gpointer g_class);
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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);

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

static const GstEventMask *
		gst_avi_demux_get_event_mask 	(GstPad      *pad);
static gboolean gst_avi_demux_handle_src_event 	(GstPad      *pad,
						 GstEvent    *event);
static const GstFormat *
		gst_avi_demux_get_src_formats 	(GstPad      *pad); 
static const GstQueryType *
		gst_avi_demux_get_src_query_types (GstPad    *pad);
static gboolean gst_avi_demux_handle_src_query 	(GstPad      *pad,
						 GstQueryType type, 
						 GstFormat   *format,
						 gint64      *value);
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
81

Wim Taymans's avatar
Wim Taymans committed
82
static GstElementStateReturn
83
		gst_avi_demux_change_state 	(GstElement  *element);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
84

85
86
87
88
static void     gst_avi_demux_get_property      (GObject     *object,
						 guint        prop_id, 	
						 GValue      *value,
						 GParamSpec  *pspec);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
89

90
91
92
93
94
95
96
static GstCaps * gst_avi_demux_audio_caps (guint16 codec_id,
    gst_riff_strf_auds *strf, GstAviDemux *avi_demux);
static GstCaps * gst_avi_demux_video_caps (guint32 codec_fcc,
    gst_riff_strh *strh, gst_riff_strf_vids *strf,
    GstAviDemux *avi_demux);
static GstCaps * gst_avi_demux_iavs_caps (void);

97
static GstPadTemplate *videosrctempl, *audiosrctempl;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
98
static GstElementClass *parent_class = NULL;
99
/*static guint gst_avi_demux_signals[LAST_SIGNAL] = { 0 }; */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
100
101
102
103
104
105
106
107
108

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),      
109
      gst_avi_demux_base_init,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
110
111
112
113
114
115
116
117
118
119
120
121
122
      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;
}

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
static void
gst_avi_demux_base_init (gpointer g_class)
{
  static GstElementDetails gst_avi_demux_details = GST_ELEMENT_DETAILS (
    "Avi demuxer",
    "Codec/Demuxer",
    "Demultiplex an avi file into audio and video",
    "Erik Walthinsen <omega@cse.ogi.edu>\n"
    "Wim Taymans <wim.taymans@chello.be>"
  );
  static guint32 vid_list[] = {
    GST_MAKE_FOURCC('I','4','2','0'),
    GST_MAKE_FOURCC('Y','U','Y','2'),
    GST_MAKE_FOURCC('M','J','P','G'),
    GST_MAKE_FOURCC('D','V','S','D'),
    GST_MAKE_FOURCC('W','M','V','1'),
    GST_MAKE_FOURCC('W','M','V','2'),
    GST_MAKE_FOURCC('M','P','G','4'),
    GST_MAKE_FOURCC('M','P','4','2'),
    GST_MAKE_FOURCC('M','P','4','3'),
    GST_MAKE_FOURCC('H','F','Y','U'),
    GST_MAKE_FOURCC('D','I','V','3'),
    GST_MAKE_FOURCC('M','P','E','G'),
    GST_MAKE_FOURCC('H','2','6','3'),
    GST_MAKE_FOURCC('D','I','V','X'),
    GST_MAKE_FOURCC('X','V','I','D'),
    GST_MAKE_FOURCC('3','I','V','1'),
    0 /* end */
  };
  static gint aud_list[] = {
    GST_RIFF_WAVE_FORMAT_MPEGL3,
    GST_RIFF_WAVE_FORMAT_MPEGL12,
    GST_RIFF_WAVE_FORMAT_PCM,
    GST_RIFF_WAVE_FORMAT_VORBIS1,
    GST_RIFF_WAVE_FORMAT_A52,
    GST_RIFF_WAVE_FORMAT_ALAW,
    GST_RIFF_WAVE_FORMAT_MULAW,
    -1 /* end */
  };
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
  gint i = 0;
  GstCaps *audcaps = NULL, *vidcaps = NULL, *temp;

  for (i = 0; aud_list[i] != -1; i++) {
    temp = gst_avi_demux_audio_caps (aud_list[i], NULL, NULL);
    audcaps = gst_caps_append (audcaps, temp);
  }
  audiosrctempl = gst_pad_template_new ("audio_%02d",
					GST_PAD_SRC,
					GST_PAD_SOMETIMES,
					audcaps, NULL);
  for (i = 0; vid_list[i] != 0; i++) {
    temp = gst_avi_demux_video_caps (vid_list[i], NULL, NULL, NULL);
    vidcaps = gst_caps_append (vidcaps, temp);
  }
  vidcaps = gst_caps_append (vidcaps,
			     gst_avi_demux_iavs_caps ());
  videosrctempl = gst_pad_template_new ("video_%02d",
					GST_PAD_SRC,
					GST_PAD_SOMETIMES,
					vidcaps, NULL);
  gst_element_class_add_pad_template (element_class, audiosrctempl);
  gst_element_class_add_pad_template (element_class, videosrctempl);
  gst_element_class_add_pad_template (element_class,
	GST_PAD_TEMPLATE_GET (sink_templ));
  gst_element_class_set_details (element_class, &gst_avi_demux_details);

}

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
192
193
194
195
196
197
198
199
200
201
202
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",
203
                       G_MINLONG, G_MAXLONG, 0, G_PARAM_READABLE)); /* CHECKME */
204
205
206
207
208
209
  g_object_class_install_property (gobject_class, ARG_METADATA,
    g_param_spec_boxed ("metadata", "Metadata", "Metadata",
                        GST_TYPE_CAPS, G_PARAM_READABLE));
  g_object_class_install_property (gobject_class, ARG_STREAMINFO,
    g_param_spec_boxed ("streaminfo", "Streaminfo", "Streaminfo",
                        GST_TYPE_CAPS, G_PARAM_READABLE));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
210
211
212
213

  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
214
215
  
  gstelement_class->change_state = gst_avi_demux_change_state;
216
  gstelement_class->send_event = gst_avi_demux_send_event;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
217
218
219
220
221
222
223
224
}

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 (
225
		  GST_PAD_TEMPLATE_GET (sink_templ), "sink");
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
226
227
228
229
230
231
232
233
234
  gst_element_add_pad (GST_ELEMENT (avi_demux), avi_demux->sinkpad);

  gst_element_set_loop_function (GST_ELEMENT (avi_demux), gst_avi_demux_loop);
}

static gboolean
gst_avi_demux_avih (GstAviDemux *avi_demux)
{
  gst_riff_avih *avih;
Benjamin Otte's avatar
Benjamin Otte committed
235
  guint8 *avihdata;
236
237
238
  GstByteStream  *bs = avi_demux->bs;
  guint32 got_bytes;

Benjamin Otte's avatar
Benjamin Otte committed
239
240
  got_bytes = gst_bytestream_peek_bytes (bs, &avihdata, sizeof (gst_riff_avih));
  avih = (gst_riff_avih *) avihdata;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
241

242
  if (got_bytes == sizeof (gst_riff_avih)) {
Wim Taymans's avatar
Wim Taymans committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    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);

258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
    GST_INFO ( "gst_avi_demux: avih tag found");
    GST_INFO ( "gst_avi_demux:  us_frame    %d", avi_demux->avih.us_frame);
    GST_INFO ( "gst_avi_demux:  max_bps     %d", avi_demux->avih.max_bps);
    GST_INFO ( "gst_avi_demux:  pad_gran    %d", avi_demux->avih.pad_gran);
    GST_INFO ( "gst_avi_demux:  flags       0x%08x", avi_demux->avih.flags);
    GST_INFO ( "gst_avi_demux:  tot_frames  %d", avi_demux->avih.tot_frames);
    GST_INFO ( "gst_avi_demux:  init_frames %d", avi_demux->avih.init_frames);
    GST_INFO ( "gst_avi_demux:  streams     %d", avi_demux->avih.streams);
    GST_INFO ( "gst_avi_demux:  bufsize     %d", avi_demux->avih.bufsize);
    GST_INFO ( "gst_avi_demux:  width       %d", avi_demux->avih.width);
    GST_INFO ( "gst_avi_demux:  height      %d", avi_demux->avih.height);
    GST_INFO ( "gst_avi_demux:  scale       %d", avi_demux->avih.scale);
    GST_INFO ( "gst_avi_demux:  rate        %d", avi_demux->avih.rate);
    GST_INFO ( "gst_avi_demux:  start       %d", avi_demux->avih.start);
    GST_INFO ( "gst_avi_demux:  length      %d", avi_demux->avih.length);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
273
274
275
276
277
278
279
280
281
282

    return TRUE;
  }
  return FALSE;
}

static gboolean 
gst_avi_demux_strh (GstAviDemux *avi_demux)
{
  gst_riff_strh *strh;
Benjamin Otte's avatar
Benjamin Otte committed
283
  guint8 *strhdata;
284
285
286
  GstByteStream  *bs = avi_demux->bs;
  guint32 got_bytes;

Benjamin Otte's avatar
Benjamin Otte committed
287
288
  got_bytes = gst_bytestream_peek_bytes (bs, &strhdata, sizeof (gst_riff_strh));
  strh = (gst_riff_strh *) strhdata;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
289

290
  if (got_bytes == sizeof (gst_riff_strh)) {
Wim Taymans's avatar
Wim Taymans committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
    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 */

317
318
    GST_INFO ( "gst_avi_demux: strh tag found");
    GST_INFO ( "gst_avi_demux:  type        0x%08x (%s)", 
Wim Taymans's avatar
Wim Taymans committed
319
  		  target->strh.type, gst_riff_id_to_fourcc (strh->type));
320
    GST_INFO ( "gst_avi_demux:  fcc_handler 0x%08x (%s)", 
Wim Taymans's avatar
Wim Taymans committed
321
		  target->strh.fcc_handler, gst_riff_id_to_fourcc (strh->fcc_handler));
322
323
324
325
326
327
328
329
330
331
    GST_INFO ( "gst_avi_demux:  flags       0x%08x", strh->flags);
    GST_INFO ( "gst_avi_demux:  priority    %d", target->strh.priority);
    GST_INFO ( "gst_avi_demux:  init_frames %d", target->strh.init_frames);
    GST_INFO ( "gst_avi_demux:  scale       %d", target->strh.scale);
    GST_INFO ( "gst_avi_demux:  rate        %d", target->strh.rate);
    GST_INFO ( "gst_avi_demux:  start       %d", target->strh.start);
    GST_INFO ( "gst_avi_demux:  length      %d", target->strh.length);
    GST_INFO ( "gst_avi_demux:  bufsize     %d", target->strh.bufsize);
    GST_INFO ( "gst_avi_demux:  quality     %d", target->strh.quality);
    GST_INFO ( "gst_avi_demux:  samplesize  %d", target->strh.samplesize);
Wim Taymans's avatar
Wim Taymans committed
332
333
334
335

    target->delay = 0LL;
    target->total_bytes = 0LL;
    target->total_frames = 0;
Wim Taymans's avatar
Wim Taymans committed
336
    target->end_pos = -1;
337
338
339
    target->current_frame = 0;
    target->current_byte = 0;
    target->need_flush = FALSE;
Wim Taymans's avatar
Wim Taymans committed
340
    target->skip = 0;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
341

342
343
    avi_demux->avih.bufsize = MAX (avi_demux->avih.bufsize, target->strh.bufsize);

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
344
345
346
347
348
    return TRUE;
  }
  return FALSE;
}

349
350
351
352
static void
gst_avi_demux_dmlh (GstAviDemux *avi_demux)
{
  gst_riff_dmlh *dmlh;
Benjamin Otte's avatar
Benjamin Otte committed
353
  guint8 *dmlhdata;
354
  GstByteStream  *bs = avi_demux->bs;
355
356
  guint32 got_bytes;

Benjamin Otte's avatar
Benjamin Otte committed
357
358
  got_bytes = gst_bytestream_peek_bytes (bs, &dmlhdata, sizeof (gst_riff_dmlh));
  dmlh = (gst_riff_dmlh *) dmlhdata;
359
360
}

Wim Taymans's avatar
Wim Taymans committed
361
362
363
364
static void
gst_avi_demux_strn (GstAviDemux *avi_demux, gint len)
{
  gchar *name;
Benjamin Otte's avatar
Benjamin Otte committed
365
  guint8 *namedata;
Wim Taymans's avatar
Wim Taymans committed
366
367
368
  GstByteStream  *bs = avi_demux->bs;
  guint32 got_bytes;

Benjamin Otte's avatar
Benjamin Otte committed
369
370
  got_bytes = gst_bytestream_peek_bytes (bs, &namedata, len);
  name = (gchar *) namedata;
Wim Taymans's avatar
Wim Taymans committed
371
372
373
  if (got_bytes != len)
    return;

374
  GST_DEBUG ("Stream name: \"%s\"", name);
Wim Taymans's avatar
Wim Taymans committed
375
376
}

377
378
379
380
381
static void
gst_avi_demux_metadata (GstAviDemux *avi_demux, gint len)
{
  guint32 got_bytes;
  GstByteStream  *bs = avi_demux->bs;
382
  gst_riff_chunk *temp_chunk, chunk;
Benjamin Otte's avatar
Benjamin Otte committed
383
  guint8 *tempdata;
384
385
386
387
388
389
390
  gchar *name, *type;
  GstProps *props;
  GstPropsEntry *entry;

  props = gst_props_empty_new ();

  while (len > 0) {
Benjamin Otte's avatar
Benjamin Otte committed
391
392
393
    got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
    temp_chunk = (gst_riff_chunk *) tempdata;
    
394
395
396
397
398
399
    /* fixup for our big endian friends */
    chunk.id = GUINT32_FROM_LE (temp_chunk->id);
    chunk.size = GUINT32_FROM_LE (temp_chunk->size);

    gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
    if (got_bytes != sizeof (gst_riff_chunk))
400
      return;
401
    len -= sizeof (gst_riff_chunk);
402
403

    /* don't care about empty entries - move on */
404
    if (chunk.size == 0)
405
406
      continue;

Benjamin Otte's avatar
Benjamin Otte committed
407
408
    got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, chunk.size);
    name = (gchar *) tempdata;
409
410
    gst_bytestream_flush (bs, (chunk.size + 1) & ~1);
    if (got_bytes != chunk.size)
411
      return;
412
    len -= ((chunk.size + 1) & ~1);
413
414

    /* we now have an info string in 'name' of type 'chunk.id' - find 'type' */
415
    switch (chunk.id) {
416
417
418
419
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
      case GST_RIFF_INFO_IARL:
        type = "Location";
        break;
      case GST_RIFF_INFO_IART:
        type = "Artist";
        break;
      case GST_RIFF_INFO_ICMS:
        type = "Commissioner";
        break;
      case GST_RIFF_INFO_ICMT:
        type = "Comment";
        break;
      case GST_RIFF_INFO_ICOP:
        type = "Copyright";
        break;
      case GST_RIFF_INFO_ICRD:
        type = "Creation Date";
        break;
      case GST_RIFF_INFO_ICRP:
        type = "Cropped";
        break;
      case GST_RIFF_INFO_IDIM:
        type = "Dimensions";
        break;
      case GST_RIFF_INFO_IDPI:
        type = "Dots per Inch";
        break;
      case GST_RIFF_INFO_IENG:
        type = "Engineer";
        break;
      case GST_RIFF_INFO_IGNR:
        type = "Genre";
        break;
      case GST_RIFF_INFO_IKEY:
        type = "Keywords";
        break;
      case GST_RIFF_INFO_ILGT:
        type = "Lightness";
        break;
      case GST_RIFF_INFO_IMED:
        type = "Medium";
        break;
      case GST_RIFF_INFO_INAM:
        type = "Title"; /* "Name" */
        break;
      case GST_RIFF_INFO_IPLT:
        type = "Palette";
        break;
      case GST_RIFF_INFO_IPRD:
        type = "Product";
        break;
      case GST_RIFF_INFO_ISBJ:
        type = "Subject";
        break;
      case GST_RIFF_INFO_ISFT:
471
        type = "Encoder"; /* "Software" */
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
        break;
      case GST_RIFF_INFO_ISHP:
        type = "Sharpness";
        break;
      case GST_RIFF_INFO_ISRC:
        type = "Source";
        break;
      case GST_RIFF_INFO_ISRF:
        type = "Source Form";
        break;
      case GST_RIFF_INFO_ITCH:
        type = "Technician";
        break;
      default:
	type = NULL;
	break;
    }

    if (type) {
      /* create props entry */
492
      entry = gst_props_entry_new (type, GST_PROPS_STRING (name));
493
494
495
496
497
498
      gst_props_add_entry (props, entry);
    }
  }

  gst_props_debug(props);

499
500
501
502
  gst_caps_replace_sink (&avi_demux->metadata,
		         gst_caps_new("avi_metadata",
                                      "application/x-gst-metadata",
                                        props));
503
504
505
506
507
508
509
510
511
512
513
514
515

  g_object_notify(G_OBJECT(avi_demux), "metadata");
}

static void
gst_avi_demux_streaminfo (GstAviDemux *avi_demux)
{
  GstProps *props;

  props = gst_props_empty_new ();

  /* compression formats are added later - a bit hacky */

516
517
518
519
  gst_caps_replace_sink (&avi_demux->streaminfo,
		  	 gst_caps_new("avi_streaminfo",
                                      "application/x-gst-streaminfo",
                                      props));
520
521
522
523

  /*g_object_notify(G_OBJECT(avi_demux), "streaminfo");*/
}

524
525
/* video/audio pad/caps stuff */

526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#ifdef G_HAVE_ISO_VARARGS

#define GST_AVI_VID_CAPS_NEW(name, mimetype, ...)		\
	(strf != NULL) ?					\
	GST_CAPS_NEW (name,					\
		      mimetype,					\
		      "width",  GST_PROPS_INT (width),		\
		      "height", GST_PROPS_INT (height),		\
		      "framerate", GST_PROPS_FLOAT (framerate), \
		      __VA_ARGS__)				\
	:							\
	GST_CAPS_NEW (name,					\
		      mimetype,					\
		      "width",  GST_PROPS_INT_RANGE (16, 4096),	\
		      "height", GST_PROPS_INT_RANGE (16, 4096),	\
		      "framerate", GST_PROPS_FLOAT_RANGE (0., G_MAXFLOAT), \
		      __VA_ARGS__)

#elif defined(G_HAVE_GNUC_VARARGS)

546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#define GST_AVI_VID_CAPS_NEW(name, mimetype, props...)		\
	(strf != NULL) ?					\
	GST_CAPS_NEW (name,					\
		      mimetype,					\
		      "width",  GST_PROPS_INT (width),		\
		      "height", GST_PROPS_INT (height),		\
		      "framerate", GST_PROPS_FLOAT (framerate), \
		      ##props)					\
	:							\
	GST_CAPS_NEW (name,					\
		      mimetype,					\
		      "width",  GST_PROPS_INT_RANGE (16, 4096),	\
		      "height", GST_PROPS_INT_RANGE (16, 4096),	\
		      "framerate", GST_PROPS_FLOAT_RANGE (0., G_MAXFLOAT), \
		      ##props)
561
#endif
562
563
564
565
566
567

static GstCaps *
gst_avi_demux_video_caps (guint32 codec_fcc,
			  gst_riff_strh *strh,
			  gst_riff_strf_vids *strf,
			  GstAviDemux *avi_demux)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
568
{
569
570
571
572
573
574
575
576
577
578
579
580
581
  GstCaps *caps = NULL;
  gchar *codecname = NULL;
  gint width = -1, height = -1;
  gdouble framerate = 0.;

  if (strf != NULL) {
    width = GUINT32_FROM_LE (strf->width);
    height = GUINT32_FROM_LE (strf->height);
  }
  if (strh != NULL) {
    framerate = 1. * GUINT32_FROM_LE (strh->rate) /
		GUINT32_FROM_LE (strh->scale); /* fps */
  }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
582

583
  switch (codec_fcc) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
584
585
    case GST_MAKE_FOURCC('I','4','2','0'):
    case GST_MAKE_FOURCC('Y','U','Y','2'):
586
587
588
589
      caps = GST_AVI_VID_CAPS_NEW (
                  "avidemux_video_src_raw",
                  "video/x-raw-yuv",
                    "format",  GST_PROPS_FOURCC (codec_fcc)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
590
                );
591
592
      codecname = g_strdup_printf("Raw Video (" GST_FOURCC_FORMAT ")",
                                  GST_FOURCC_ARGS(codec_fcc));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
593
      break;
594

595
596
597
598
    case GST_MAKE_FOURCC('M','J','P','G'): /* YUY2 MJPEG */
    case GST_MAKE_FOURCC('J','P','E','G'): /* generic (mostly RGB) MJPEG */
    case GST_MAKE_FOURCC('P','I','X','L'): /* Miro/Pinnacle fourccs */
    case GST_MAKE_FOURCC('V','I','X','L'): /* Miro/Pinnacle fourccs */
599
600
601
602
      caps = GST_AVI_VID_CAPS_NEW (
                  "avidemux_video_src_jpeg",
                  "video/x-jpeg",
                    NULL
Ronald S. Bultje's avatar
Ronald S. Bultje committed
603
                );
604
605
      codecname = g_strdup_printf("Motion-JPEG (" GST_FOURCC_FORMAT ")",
                                  GST_FOURCC_ARGS(codec_fcc));
606
      break;
607

608
    case GST_MAKE_FOURCC('H','F','Y','U'):
609
610
611
612
613
614
615
      caps = GST_AVI_VID_CAPS_NEW (
                  "avidemux_video_src_hfyu",
                  "video/x-huffyuv",
                    NULL
                );
      codecname = g_strdup_printf("HuffYUV (" GST_FOURCC_FORMAT ")",
                                  GST_FOURCC_ARGS(codec_fcc));
616
      break;
617

618
619
    case GST_MAKE_FOURCC('M','P','E','G'):
    case GST_MAKE_FOURCC('M','P','G','I'):
620
621
      caps = GST_AVI_VID_CAPS_NEW (
                  "avidemux_video_src_mpeg",
622
                  "video/mpeg",
623
624
                    "systemstream", GST_PROPS_BOOLEAN (FALSE),
		    "mpegversion", GST_PROPS_BOOLEAN (1)
625
                );
626
627
      codecname = g_strdup_printf("MPEG-1 (" GST_FOURCC_FORMAT ")",
                                  GST_FOURCC_ARGS(codec_fcc));
628
      break;
629

630
631
632
633
634
635
636
    case GST_MAKE_FOURCC('H','2','6','3'):
    case GST_MAKE_FOURCC('i','2','6','3'):
    case GST_MAKE_FOURCC('L','2','6','3'):
    case GST_MAKE_FOURCC('M','2','6','3'):
    case GST_MAKE_FOURCC('V','D','O','W'):
    case GST_MAKE_FOURCC('V','I','V','O'):
    case GST_MAKE_FOURCC('x','2','6','3'):
637
638
639
640
641
642
643
      caps = GST_AVI_VID_CAPS_NEW (
                  "avidemux_video_src_263",
                  "video/x-h263",
                    NULL
                );
      codecname = g_strdup_printf("H263-compatible (" GST_FOURCC_FORMAT ")",
                                  GST_FOURCC_ARGS(codec_fcc));
644
      break;
645

646
647
648
    case GST_MAKE_FOURCC('D','I','V','3'):
    case GST_MAKE_FOURCC('D','I','V','4'):
    case GST_MAKE_FOURCC('D','I','V','5'):
649
650
651
652
653
654
655
656
657
658
      caps = GST_AVI_VID_CAPS_NEW (
                  "avidemux_video_src_divx3",
                  "video/x-divx",
		    "divxversion", GST_PROPS_INT(3)
                );
      codecname = g_strdup_printf("DivX-3.x (" GST_FOURCC_FORMAT ")",
                                  GST_FOURCC_ARGS(codec_fcc));
      break;

    case GST_MAKE_FOURCC('d','i','v','x'):
659
660
    case GST_MAKE_FOURCC('D','I','V','X'):
    case GST_MAKE_FOURCC('D','X','5','0'):
661
662
663
664
      caps = GST_AVI_VID_CAPS_NEW (
                  "avidemux_video_src_divx5",
                  "video/x-divx",
		    "divxversion", GST_PROPS_INT(5)
665
                );
666
667
      codecname = g_strdup_printf("DivX 4.x/5.x (" GST_FOURCC_FORMAT ")",
                                  GST_FOURCC_ARGS(codec_fcc));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
668
      break;
669

670
671
    case GST_MAKE_FOURCC('X','V','I','D'):
    case GST_MAKE_FOURCC('x','v','i','d'):
672
      caps = GST_AVI_VID_CAPS_NEW (
673
                  "avidemux_video_src",
674
675
                  "video/x-xvid",
                    NULL
676
                );
677
678
      codecname = g_strdup_printf("XviD (" GST_FOURCC_FORMAT ")",
                                  GST_FOURCC_ARGS(codec_fcc));
679
      break;
680

681
    case GST_MAKE_FOURCC('M','P','G','4'):
682
683
684
685
686
687
688
689
690
      caps = GST_AVI_VID_CAPS_NEW (
                  "avidemux_video_src",
                  "video/x-msmpeg",
		    "msmpegversion", GST_PROPS_INT (41)
                );
      codecname = g_strdup_printf("MS MPEG-4.1 (" GST_FOURCC_FORMAT ")",
                                  GST_FOURCC_ARGS(codec_fcc));
      break;

691
    case GST_MAKE_FOURCC('M','P','4','2'):
692
693
694
695
696
697
698
699
700
      caps = GST_AVI_VID_CAPS_NEW (
                  "avidemux_video_src",
                  "video/x-msmpeg",
		    "msmpegversion", GST_PROPS_INT (42)
                );
      codecname = g_strdup_printf("MS MPEG-4.2 (" GST_FOURCC_FORMAT ")",
                                  GST_FOURCC_ARGS(codec_fcc));
      break;

701
    case GST_MAKE_FOURCC('M','P','4','3'):
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
      caps = GST_AVI_VID_CAPS_NEW (
                  "avidemux_video_src",
                  "video/x-msmpeg",
		    "msmpegversion", GST_PROPS_INT (43)
                );
      codecname = g_strdup_printf("MS MPEG-4.3 (" GST_FOURCC_FORMAT ")",
                                  GST_FOURCC_ARGS(codec_fcc));
      break;

    case GST_MAKE_FOURCC('3','I','V','1'):
    case GST_MAKE_FOURCC('3','I','V','2'):
      caps = GST_AVI_VID_CAPS_NEW (
		  "avidemux_video_src_3ivx",
		  "video/x-3ivx",
		    NULL
		);
      codecname = g_strdup_printf("3ivX (" GST_FOURCC_FORMAT ")",
				  GST_FOURCC_ARGS(codec_fcc));
720
      break;
721

722
    case GST_MAKE_FOURCC('D','V','S','D'):
Ronald S. Bultje's avatar
Ronald S. Bultje committed
723
    case GST_MAKE_FOURCC('d','v','s','d'):
724
      caps = GST_AVI_VID_CAPS_NEW (
Ronald S. Bultje's avatar
Ronald S. Bultje committed
725
                  "avidemux_video_src",
726
727
728
                  "video/x-dv",
                    "systemstream", GST_PROPS_BOOLEAN (FALSE)
                );
729
      codecname = g_strdup_printf("Digital Video type 2 (" GST_FOURCC_FORMAT ")",
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
                                  GST_FOURCC_ARGS(codec_fcc));
      break;

    case GST_MAKE_FOURCC('W','M','V','1'):
      caps = GST_AVI_VID_CAPS_NEW (
                  "avidemux_video_src_wmv1",
                  "video/x-wmv",
                    "wmvversion", GST_PROPS_INT (1)
                );
      codecname = g_strdup_printf("Windows Media Format 1 ("
				  GST_FOURCC_FORMAT ")",
				  GST_FOURCC_ARGS(codec_fcc));
      break;

    case GST_MAKE_FOURCC('W','M','V','2'):
      caps = GST_AVI_VID_CAPS_NEW (
                  "avidemux_video_src_wmv2",
                  "video/x-wmv",
                    "wmvversion", GST_PROPS_INT (2)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
749
                );
750
751
752
      codecname = g_strdup_printf("Windows Media Format 2 ("
				  GST_FOURCC_FORMAT ")",
				  GST_FOURCC_ARGS(codec_fcc));
753
      break;
754

755
    default:
756
757
      g_warning ("avidemux: unkown video format " GST_FOURCC_FORMAT,
		 GST_FOURCC_ARGS(codec_fcc));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
758
759
760
      break;
  }

761
  /* set video codec info on streaminfo caps */
762
  if (avi_demux != NULL && codecname != NULL) {
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
    GstPropsEntry *entry;
    entry = gst_props_entry_new("videocodec",
				GST_PROPS_STRING(codecname));
    gst_props_add_entry(avi_demux->streaminfo->properties, entry);
  }
  if (codecname != NULL) {
    g_free(codecname);
  }

  return caps;
}

static void 
gst_avi_demux_strf_vids (GstAviDemux *avi_demux)
{
  gst_riff_strf_vids *strf;
  gst_riff_strh *strh;
  guint8 *strfdata;
  GstPad *srcpad;
  GstCaps *caps = NULL;
  avi_stream_context *stream;
  GstByteStream  *bs = avi_demux->bs;
  guint32 got_bytes;
  gchar *padname;

  got_bytes = gst_bytestream_peek_bytes (bs, &strfdata, sizeof (gst_riff_strf_vids));
  strf = (gst_riff_strf_vids *) strfdata;
  if (got_bytes != sizeof (gst_riff_strf_vids))
    return;

  padname = g_strdup_printf ("video_%02d", avi_demux->num_v_streams);
  srcpad =  gst_pad_new_from_template (videosrctempl, padname);
  g_free (padname);
796

797
798
799
800
801
802
803
804
  /* let's try some gstreamer-like mime-type caps */
  strh = &avi_demux->stream[avi_demux->num_streams].strh;
  caps = gst_avi_demux_video_caps (GUINT32_FROM_LE(strf->compression),
				   strh, strf, avi_demux);

  if (caps != NULL) {
    gst_pad_try_set_caps (srcpad, caps);
  }
805
806
  gst_pad_set_formats_function (srcpad, gst_avi_demux_get_src_formats);
  gst_pad_set_event_mask_function (srcpad, gst_avi_demux_get_event_mask);
Wim Taymans's avatar
Wim Taymans committed
807
  gst_pad_set_event_function (srcpad, gst_avi_demux_handle_src_event);
808
  gst_pad_set_query_type_function (srcpad, gst_avi_demux_get_src_query_types);
Wim Taymans's avatar
Wim Taymans committed
809
  gst_pad_set_query_function (srcpad, gst_avi_demux_handle_src_query);
810
  gst_pad_set_convert_function (srcpad, gst_avi_demux_src_convert);
Wim Taymans's avatar
Wim Taymans committed
811
812
813
814
815
816

  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
817

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
818
819
820
  gst_element_add_pad (GST_ELEMENT (avi_demux), srcpad);
}

821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
#ifdef G_HAVE_ISO_VARARGS

#define GST_AVI_AUD_CAPS_NEW(name, mimetype, ...)			\
	(strf != NULL) ?						\
	GST_CAPS_NEW (name,						\
		      mimetype,						\
		      "rate",     GST_PROPS_INT (rate),			\
		      "channels", GST_PROPS_INT (channels),		\
		      __VA_ARGS__)					\
	:								\
	GST_CAPS_NEW (name,						\
		      mimetype,						\
		      "rate",     GST_PROPS_INT_RANGE (8000, 96000),	\
		      "channels", GST_PROPS_INT_RANGE (1, 2),		\
		      __VA_ARGS__)


#elif defined(G_HAVE_GNUC_VARARGS)

840
841
842
843
844
845
846
847
848
849
850
851
852
#define GST_AVI_AUD_CAPS_NEW(name, mimetype, props...)			\
	(strf != NULL) ?						\
	GST_CAPS_NEW (name,						\
		      mimetype,						\
		      "rate",     GST_PROPS_INT (rate),			\
		      "channels", GST_PROPS_INT (channels),		\
		      ##props)						\
	:								\
	GST_CAPS_NEW (name,						\
		      mimetype,						\
		      "rate",     GST_PROPS_INT_RANGE (8000, 96000),	\
		      "channels", GST_PROPS_INT_RANGE (1, 2),		\
		      ##props)
853
#endif
854
855
856
857
858

static GstCaps *
gst_avi_demux_audio_caps (guint16 codec_id,
			  gst_riff_strf_auds *strf,
			  GstAviDemux *avi_demux)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
859
{
860
861
862
  GstCaps *caps = NULL;
  gchar *codecname = NULL;
  gint rate = -1, channels = -1;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
863

864
865
866
867
  if (strf != NULL) {
    rate = GUINT32_FROM_LE (strf->rate);
    channels = GUINT16_FROM_LE (strf->channels);
  }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
868

869
870
871
872
873
874
875
876
  switch (codec_id) {
    case GST_RIFF_WAVE_FORMAT_MPEGL3: /* mp3 */
      caps = GST_AVI_AUD_CAPS_NEW ("avi_demux_audio_src_mp3",
				   "audio/mpeg",
				     "layer", GST_PROPS_INT (3));
      codecname = g_strdup_printf("MPEG-1 layer 3 audio (0x%04x)",
                                  codec_id);
      break;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
877

878
879
880
881
882
883
    case GST_RIFF_WAVE_FORMAT_MPEGL12: /* mp1 or mp2 */
      caps = GST_AVI_AUD_CAPS_NEW ("avi_demux_audio_src_mp12",
				   "audio/mpeg",
				     "layer", GST_PROPS_INT (2));
      codecname = g_strdup_printf("MPEG-1 layer 1/2 audio (0x%04x)",
                                  codec_id);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
884
      break;
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922

    case GST_RIFF_WAVE_FORMAT_PCM: /* PCM/wav */ {
      GstPropsEntry *width = NULL, *depth = NULL, *signedness = NULL;

      if (strf != NULL) {
        gint ba = GUINT16_FROM_LE (strf->blockalign);
        gint ch = GUINT16_FROM_LE (strf->channels);
        gint ws = GUINT16_FROM_LE (strf->size);

        width = gst_props_entry_new ("width",
				     GST_PROPS_INT (ba * 8 / ch));
        depth = gst_props_entry_new ("depth",
				     GST_PROPS_INT (ws));
        signedness = gst_props_entry_new ("signed",
					  GST_PROPS_BOOLEAN (ws != 8));
      } else {
        signedness = gst_props_entry_new ("signed",
					  GST_PROPS_LIST (
					    GST_PROPS_BOOLEAN (TRUE),
					    GST_PROPS_BOOLEAN (FALSE)));
        width = gst_props_entry_new ("width",
				     GST_PROPS_LIST (
				       GST_PROPS_INT (8),
				       GST_PROPS_INT (16)));
        depth = gst_props_entry_new ("depth",
				     GST_PROPS_LIST (
				       GST_PROPS_INT (8),
				       GST_PROPS_INT (16)));
      }

      caps = GST_AVI_AUD_CAPS_NEW ("avi_demux_audio_src_pcm",
				   "audio/x-raw-int",
				     "endianness",
				       GST_PROPS_INT (G_LITTLE_ENDIAN));
      gst_props_add_entry (caps->properties, width);
      gst_props_add_entry (caps->properties, depth);
      gst_props_add_entry (caps->properties, signedness);

923
      codecname = g_strdup_printf("Raw PCM/WAV (0x%04x)",
924
925
                                  codec_id);
    }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
926
      break;
927

928
    case GST_RIFF_WAVE_FORMAT_MULAW:
929
930
931
932
933
934
935
936
937
938
939
      if (strf != NULL && strf->size != 8) {
        g_warning ("invalid depth (%d) of mulaw audio, overwriting.",
		   strf->size);
      }
      caps = GST_AVI_AUD_CAPS_NEW ("avidemux_audio_src",
				   "audio/x-mulaw",
				     NULL);
      codecname = g_strdup_printf("A-law encoded (0x%04x)",
                                  codec_id);
      break;

940
    case GST_RIFF_WAVE_FORMAT_ALAW:
941
942
943
944
945
946
947
948
949
      if (strf != NULL && strf->size != 8) {
        g_warning ("invalid depth (%d) of alaw audio, overwriting.",
		   strf->size);
      }
      caps = GST_AVI_AUD_CAPS_NEW ("avidemux_audio_src",
				   "audio/x-alaw",
				     NULL);
      codecname = g_strdup_printf("A-law encoded (0x%04x)",
                                  codec_id);
950
      break;
951

952
953
954
955
956
957
    case GST_RIFF_WAVE_FORMAT_VORBIS1: /* ogg/vorbis mode 1 */
    case GST_RIFF_WAVE_FORMAT_VORBIS2: /* ogg/vorbis mode 2 */
    case GST_RIFF_WAVE_FORMAT_VORBIS3: /* ogg/vorbis mode 3 */
    case GST_RIFF_WAVE_FORMAT_VORBIS1PLUS: /* ogg/vorbis mode 1+ */
    case GST_RIFF_WAVE_FORMAT_VORBIS2PLUS: /* ogg/vorbis mode 2+ */
    case GST_RIFF_WAVE_FORMAT_VORBIS3PLUS: /* ogg/vorbis mode 3+ */
958
959
960
961
962
      caps = GST_AVI_AUD_CAPS_NEW ("asf_demux_audio_src_vorbis",
				   "audio/x-vorbis",
				     NULL);
      codecname = g_strdup_printf("Vorbis (0x%04x)",
                                  codec_id);
963
      break;
964

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
965
    case GST_RIFF_WAVE_FORMAT_A52:
966
967
968
969
970
      caps = GST_AVI_AUD_CAPS_NEW ("asf_demux_audio_src_ac3",
				   "audio/x-ac3",
				     NULL);
      codecname = g_strdup_printf("AC-3 (0x%04x)",
                                  codec_id);
Wim Taymans's avatar
Wim Taymans committed
971
      break;
972

Wim Taymans's avatar
Wim Taymans committed
973
    default:
974
975
      g_warning ("avidemux: unkown audio format 0x%04x",
		 codec_id);
Wim Taymans's avatar
Wim Taymans committed
976
      break;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
977
978
  }

979
  if (avi_demux != NULL && codecname != NULL) {
980
981
982
983
984
985
986
987
988
989
990
991
    /* set audio codec in streaminfo */
    GstPropsEntry *entry;
    entry = gst_props_entry_new("audiocodec",
				GST_PROPS_STRING(codecname));
    gst_props_add_entry(avi_demux->streaminfo->properties, entry);
  }
  if (codecname != NULL) {
    g_free (codecname);
  }

  return caps;
}
Ronald S. Bultje's avatar
Ronald S. Bultje committed
992

993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
static void 
gst_avi_demux_strf_auds (GstAviDemux *avi_demux)
{
  gst_riff_strf_auds *strf;
  guint8 *strfdata;
  GstPad *srcpad;
  GstCaps *caps = NULL;
  avi_stream_context *stream;
  GstByteStream  *bs = avi_demux->bs;
  guint32 got_bytes;
  gchar *padname;

  got_bytes = gst_bytestream_peek_bytes (bs, &strfdata, sizeof (gst_riff_strf_auds));
  strf = (gst_riff_strf_auds *) strfdata;
  if (got_bytes != sizeof (gst_riff_strf_auds))
    return;
Wim Taymans's avatar
Wim Taymans committed
1009

1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
  GST_INFO ( "gst_avi_demux: strf tag found in context auds");
  GST_INFO ( "gst_avi_demux:  format      %d", GUINT16_FROM_LE (strf->format));
  GST_INFO ( "gst_avi_demux:  channels    %d", GUINT16_FROM_LE (strf->channels));
  GST_INFO ( "gst_avi_demux:  rate        %d", GUINT32_FROM_LE (strf->rate));
  GST_INFO ( "gst_avi_demux:  av_bps      %d", GUINT32_FROM_LE (strf->av_bps));
  GST_INFO ( "gst_avi_demux:  blockalign  %d", GUINT16_FROM_LE (strf->blockalign));
  GST_INFO ( "gst_avi_demux:  size        %d", GUINT16_FROM_LE (strf->size));

  padname = g_strdup_printf ("audio_%02d", 
			     avi_demux->num_a_streams);
  srcpad =  gst_pad_new_from_template (audiosrctempl, padname);
  g_free (padname);

  caps = gst_avi_demux_audio_caps (GUINT16_FROM_LE (strf->format),
				   strf, avi_demux);

  if (caps != NULL) {
    gst_pad_try_set_caps(srcpad, caps);
  }
1029
1030
  gst_pad_set_formats_function (srcpad, gst_avi_demux_get_src_formats);
  gst_pad_set_event_mask_function (srcpad, gst_avi_demux_get_event_mask);
Wim Taymans's avatar
Wim Taymans committed
1031
  gst_pad_set_event_function (srcpad, gst_avi_demux_handle_src_event);
1032
  gst_pad_set_query_type_function (srcpad, gst_avi_demux_get_src_query_types);
Wim Taymans's avatar
Wim Taymans committed
1033
  gst_pad_set_query_function (srcpad, gst_avi_demux_handle_src_query);
1034
  gst_pad_set_convert_function (srcpad, gst_avi_demux_src_convert);
Wim Taymans's avatar
Wim Taymans committed
1035
1036
1037
1038
1039
1040

  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
1041

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1042
1043
1044
  gst_element_add_pad (GST_ELEMENT (avi_demux), srcpad);
}

1045
1046
1047
1048
1049
1050
1051
1052
static GstCaps *
gst_avi_demux_iavs_caps (void)
{
  return GST_CAPS_NEW ("avi_type_dv", 
                       "video/x-dv", 
                         "systemstream", GST_PROPS_BOOLEAN (TRUE));
}

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1053
1054
1055
1056
static void 
gst_avi_demux_strf_iavs (GstAviDemux *avi_demux)
{
  gst_riff_strf_iavs *strf;
Benjamin Otte's avatar
Benjamin Otte committed
1057
  guint8 *strfdata;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1058
  GstPad *srcpad;
1059
  GstCaps *caps = NULL;
Wim Taymans's avatar
Wim Taymans committed
1060
  avi_stream_context *stream;
1061
1062
  GstByteStream  *bs = avi_demux->bs;
  guint32 got_bytes;
1063
  gchar *padname;
1064
  GstPropsEntry *entry;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1065

Benjamin Otte's avatar
Benjamin Otte committed
1066
1067
  got_bytes = gst_bytestream_peek_bytes (bs, &strfdata, sizeof (gst_riff_strf_iavs));
  strf = (gst_riff_strf_iavs *) strfdata;
1068
1069
  if (got_bytes != sizeof (gst_riff_strf_iavs))
    return;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1070

1071
1072
1073
1074
1075
1076
1077
1078
1079
  GST_INFO ( "gst_avi_demux: strf tag found in context iavs");
  GST_INFO ( "gst_avi_demux:  DVAAuxSrc   %08x", GUINT32_FROM_LE (strf->DVAAuxSrc));
  GST_INFO ( "gst_avi_demux:  DVAAuxCtl   %08x", GUINT32_FROM_LE (strf->DVAAuxCtl));
  GST_INFO ( "gst_avi_demux:  DVAAuxSrc1  %08x", GUINT32_FROM_LE (strf->DVAAuxSrc1));
  GST_INFO ( "gst_avi_demux:  DVAAuxCtl1  %08x", GUINT32_FROM_LE (strf->DVAAuxCtl1));
  GST_INFO ( "gst_avi_demux:  DVVAuxSrc   %08x", GUINT32_FROM_LE (strf->DVVAuxSrc));
  GST_INFO ( "gst_avi_demux:  DVVAuxCtl   %08x", GUINT32_FROM_LE (strf->DVVAuxCtl));
  GST_INFO ( "gst_avi_demux:  DVReserved1 %08x", GUINT32_FROM_LE (strf->DVReserved1));
  GST_INFO ( "gst_avi_demux:  DVReserved2 %08x", GUINT32_FROM_LE (strf->DVReserved2));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1080

1081
1082
1083
1084
1085
1086
  padname = g_strdup_printf ("video_%02d", 
			     avi_demux->num_v_streams);
  srcpad =  gst_pad_new_from_template (videosrctempl, padname);
  g_free (padname);

  caps = gst_avi_demux_iavs_caps ();
1087
1088
1089
  entry = gst_props_entry_new("videocodec",
                              GST_PROPS_STRING("Digital Video type 1"));
  gst_props_add_entry(avi_demux->streaminfo->properties, entry);
1090
1091
1092
1093

  if (caps != NULL) {
    gst_pad_try_set_caps(srcpad, caps);
  }
1094
1095
  gst_pad_set_formats_function (srcpad, gst_avi_demux_get_src_formats);
  gst_pad_set_event_mask_function (srcpad, gst_avi_demux_get_event_mask);
Wim Taymans's avatar
Wim Taymans committed
1096
  gst_pad_set_event_function (srcpad, gst_avi_demux_handle_src_event);
1097
  gst_pad_set_query_type_function (srcpad, gst_avi_demux_get_src_query_types);
Wim Taymans's avatar
Wim Taymans committed
1098
  gst_pad_set_query_function (srcpad, gst_avi_demux_handle_src_query);
1099
  gst_pad_set_convert_function (srcpad, gst_avi_demux_src_convert);
Wim Taymans's avatar
Wim Taymans committed
1100
1101
1102
1103
1104
1105

  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
1106

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1107
1108
1109
1110
  gst_element_add_pad (GST_ELEMENT (avi_demux), srcpad);
}

static void
Wim Taymans's avatar
Wim Taymans committed
1111
1112
gst_avi_debug_entry (const gchar *prefix, gst_avi_index_entry *entry)
{
1113
  GST_DEBUG ("%s: %05d %d %08llx %05d %14" G_GINT64_FORMAT " %08x %08x (%d) %08x", 
Wim Taymans's avatar
Wim Taymans committed
1114
1115
1116
		  prefix, entry->index_nr, entry->stream_nr, 
		  (unsigned long long)entry->bytes_before, 
		  entry->frames_before, entry->ts, entry->flags, entry->offset, 
Wim Taymans's avatar
Wim Taymans committed
1117
		  entry->offset, entry->size);
Wim Taymans's avatar
Wim Taymans committed
1118
1119
1120
1121
}

static void
gst_avi_demux_parse_index (GstAviDemux *avi_demux,
Wim Taymans's avatar
Wim Taymans committed
1122
		          gulong filepos, gulong offset)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1123
1124
1125
{
  GstBuffer *buf;
  gulong index_size;
1126
  guint32 got_bytes;
Wim Taymans's avatar
Wim Taymans committed
1127
1128
  gint i;
  gst_riff_index_entry *entry;
Wim Taymans's avatar
Wim Taymans committed
1129
  guint32 id;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1130

Wim Taymans's avatar
Wim Taymans committed
1131
  if (!gst_bytestream_seek (avi_demux->bs, filepos + offset, GST_SEEK_METHOD_SET)) {
1132
    GST_INFO ( "avidemux: could not seek to index");
Wim Taymans's avatar
Wim Taymans committed
1133
1134
    return;
  }
1135
  do {
Wim Taymans's avatar
Wim Taymans committed
1136
1137
1138
    guint32 remaining;
    GstEvent *event;
  
1139
1140
1141
1142
    got_bytes = gst_bytestream_read (avi_demux->bs, &buf, 8);
    if (got_bytes == 8)
      break;

Wim Taymans's avatar
Wim Taymans committed
1143
    gst_bytestream_get_status (avi_demux->bs, &remaining, &event);
1144
    gst_event_unref (event);
1145
  } while (TRUE);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1146

Wim Taymans's avatar
Wim Taymans committed
1147
  if (GST_BUFFER_OFFSET (buf) != filepos + offset || GST_BUFFER_SIZE (buf) != 8) {
1148
    GST_INFO ( "avidemux: could not get index, got %" G_GINT64_FORMAT " %d, expected %ld", 
Wim Taymans's avatar
Wim Taymans committed
1149
		    GST_BUFFER_OFFSET (buf), GST_BUFFER_SIZE (buf), filepos + offset);
Wim Taymans's avatar
Wim Taymans committed
1150
    goto end;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1151
1152
  }

Wim Taymans's avatar
Wim Taymans committed
1153
1154
1155
  id = GUINT32_FROM_LE (*(guint32 *)GST_BUFFER_DATA (buf));

  if (id != GST_RIFF_TAG_idx1) {
1156
    GST_INFO ( "avidemux: no index found");
Wim Taymans's avatar
Wim Taymans committed
1157
    goto end;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1158
1159
1160
  }

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

1163
1164
  gst_bytestream_size_hint (avi_demux->bs, index_size);

1165
  got_bytes = gst_bytestream_read (avi_demux->bs, &buf, index_size);
Wim Taymans's avatar
Wim Taymans committed
1166
  if (got_bytes < index_size) {
1167
    GST_INFO ( "avidemux: error reading index");
Wim Taymans's avatar
Wim Taymans committed
1168
1169
    goto end;
  }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1170
1171

  avi_demux->index_size = index_size/sizeof(gst_riff_index_entry);
1172
  GST_INFO ( "avidemux: index size %lu", avi_demux->index_size);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1173

Wim Taymans's avatar
Wim Taymans committed
1174
1175
1176
1177
1178
1179
1180
1181
  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];
1182
    GstFormat format;
Wim Taymans's avatar
Wim Taymans committed
1183
    guint32 id;
Wim Taymans's avatar
Wim Taymans committed
1184

Wim Taymans's avatar
Wim Taymans committed
1185
1186
    id = GUINT32_FROM_LE (entry[i].id);
    stream_nr = CHUNKID_TO_STREAMNR (id);
Wim Taymans's avatar
Wim Taymans committed
1187
1188
1189
1190
1191
    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
1192
1193
1194
1195
    target->stream_nr = stream_nr;
    stream = &avi_demux->stream[stream_nr];

    target->index_nr = i;
Wim Taymans's avatar
Wim Taymans committed
1196
1197
1198
    target->flags    = GUINT32_FROM_LE (entry[i].flags);
    target->size     = GUINT32_FROM_LE (entry[i].size);
    target->offset   = GUINT32_FROM_LE (entry[i].offset);
Wim Taymans's avatar
Wim Taymans committed
1199

1200
1201
1202
    /* figure out if the index is 0 based or relative to the MOVI start */
    if (i == 0) {
      if (target->offset < filepos)
1203
	avi_demux->index_offset = filepos - 4;
1204
1205
1206
1207
      else
	avi_demux->index_offset = 0;
    }

Wim Taymans's avatar
Wim Taymans committed
1208
1209
1210
    target->bytes_before = stream->total_bytes;
    target->frames_before = stream->total_frames;

1211
    format = GST_FORMAT_TIME;
Wim Taymans's avatar
Wim Taymans committed
1212
    if (stream->strh.type == GST_RIFF_FCC_auds) {
Wim Taymans's avatar
Wim Taymans committed
1213
1214
      /* all audio frames are keyframes */
      target->flags |= GST_RIFF_IF_KEYFRAME;
1215
    }
Wim Taymans's avatar
Wim Taymans committed
1216
      
1217
    /* constant rate stream */
1218
    if (stream->strh.samplesize && stream->strh.type == GST_RIFF_FCC_auds) {
1219
1220
      gst_pad_convert (stream->pad, GST_FORMAT_BYTES, stream->total_bytes,
		                 &format, &target->ts);
Wim Taymans's avatar
Wim Taymans committed
1221
    }
1222
    /* VBR stream */
Wim Taymans's avatar
Wim Taymans committed
1223
    else {
Wim Taymans's avatar
Wim Taymans committed
1224
      gst_pad_convert (stream->pad, GST_FORMAT_DEFAULT, stream->total_frames,
1225
		                 &format, &target->ts);
Wim Taymans's avatar
Wim Taymans committed
1226
1227
1228
1229
1230
1231
    }
    gst_avi_debug_entry ("index", target);

    stream->total_bytes += target->size;
    stream->total_frames++;
  }
1232
1233
1234
1235
  for (i = 0; i < avi_demux->num_streams; i++) {
    avi_stream_context *stream;

    stream = &avi_demux->stream[i];
1236
    GST_DEBUG ("stream %i: %d frames, %" G_GINT64_FORMAT " bytes", 
1237
	       i, stream->total_frames, stream->total_bytes);
1238
  }
Wim Taymans's avatar
Wim Taymans committed
1239
  gst_buffer_unref (buf);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1240

Wim Taymans's avatar
Wim Taymans committed
1241
end:
1242
  GST_DEBUG ("index offset at %08lx", filepos);
Wim Taymans's avatar
Wim Taymans committed
1243
1244

  if (!gst_bytestream_seek (avi_demux->bs, filepos, GST_SEEK_METHOD_SET)) {