gstavidemux.c 38.6 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
 *
 * 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.
 */

20
21
22
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
23

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
24
25
#include <string.h>

26
#include "gst/riff/riff-media.h"
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
27
#include "gstavidemux.h"
28
#include "avi-ids.h"
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
29

30
31
32
GST_DEBUG_CATEGORY_STATIC (avidemux_debug);
#define GST_CAT_DEFAULT avidemux_debug

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
33
34
35
36
37
38
39
40
/* AviDemux signals and args */
enum {
  /* FILL ME */
  LAST_SIGNAL
};

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

David Schleef's avatar
David Schleef committed
45
46
static GstStaticPadTemplate sink_templ =
GST_STATIC_PAD_TEMPLATE (
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
47
48
49
  "sink",
  GST_PAD_SINK,
  GST_PAD_ALWAYS,
David Schleef's avatar
David Schleef committed
50
  GST_STATIC_CAPS ("video/x-msvideo")
51
52
);

53
static void 	gst_avi_demux_base_init	        (GstAviDemuxClass *klass);
54
static void 	gst_avi_demux_class_init	(GstAviDemuxClass *klass);
55
static void 	gst_avi_demux_init		(GstAviDemux *avi);
56

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

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

84
85
86
87
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
88

89
static GstRiffReadClass *parent_class = NULL;
90
/*static guint gst_avi_demux_signals[LAST_SIGNAL] = { 0 }; */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
91
92
93
94
95
96
97
98

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

  if (!avi_demux_type) {
    static const GTypeInfo avi_demux_info = {
99
100
      sizeof (GstAviDemuxClass),      
      (GBaseInitFunc) gst_avi_demux_base_init,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
101
      NULL,
102
      (GClassInitFunc) gst_avi_demux_class_init,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
103
104
      NULL,
      NULL,
105
      sizeof (GstAviDemux),
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
106
      0,
107
      (GInstanceInitFunc) gst_avi_demux_init,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
108
    };
109
110
111
112
113

    avi_demux_type =
	g_type_register_static (GST_TYPE_RIFF_READ,
				"GstAviDemux",
				&avi_demux_info, 0);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
114
  }
115

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
116
117
118
  return avi_demux_type;
}

119
static void
120
gst_avi_demux_base_init (GstAviDemuxClass *klass)
121
122
123
124
125
126
{
  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"
127
128
    "Wim Taymans <wim.taymans@chello.be>\n"
    "Ronald Bultje <rbultje@ronald.bitfreak.net>"
129
  );
130
131
132
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
  GstPadTemplate *videosrctempl, *audiosrctempl;
  GstCaps *audcaps, *vidcaps;
133

134
  audcaps = gst_riff_create_audio_template_caps ();
135
136
137
  audiosrctempl = gst_pad_template_new ("audio_%02d",
					GST_PAD_SRC,
					GST_PAD_SOMETIMES,
David Schleef's avatar
David Schleef committed
138
					audcaps);
139

David Schleef's avatar
David Schleef committed
140
141
  vidcaps = gst_riff_create_video_template_caps ();
  gst_caps_append (vidcaps, gst_riff_create_iavs_template_caps ());
142
143
144
  videosrctempl = gst_pad_template_new ("video_%02d",
					GST_PAD_SRC,
					GST_PAD_SOMETIMES,
David Schleef's avatar
David Schleef committed
145
					vidcaps);
146

147
148
149
  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,
David Schleef's avatar
David Schleef committed
150
      gst_static_pad_template_get (&sink_templ));
151
152
153
  gst_element_class_set_details (element_class, &gst_avi_demux_details);
}

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
154
155
156
157
158
159
160
161
162
static void
gst_avi_demux_class_init (GstAviDemuxClass *klass) 
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

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

163
164
165
  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
166

167
168
169
170
  GST_DEBUG_CATEGORY_INIT (avidemux_debug, "avidemux",
			   0, "Demuxer for AVI streams");

  parent_class = g_type_class_ref (GST_TYPE_RIFF_READ);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
171
172
  
  gobject_class->get_property = gst_avi_demux_get_property;
Wim Taymans's avatar
Wim Taymans committed
173
174
  
  gstelement_class->change_state = gst_avi_demux_change_state;
175
  gstelement_class->send_event = gst_avi_demux_send_event;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
176
177
178
}

static void 
179
gst_avi_demux_init (GstAviDemux *avi) 
180
{
181
  GST_FLAG_SET (avi, GST_ELEMENT_EVENT_AWARE);
182

183
  avi->sinkpad = gst_pad_new_from_template (
David Schleef's avatar
David Schleef committed
184
	gst_static_pad_template_get (&sink_templ), "sink");
185
186
  gst_element_add_pad (GST_ELEMENT (avi), avi->sinkpad);
  GST_RIFF_READ (avi)->sinkpad = avi->sinkpad;
Wim Taymans's avatar
Wim Taymans committed
187

188
189
  gst_element_set_loop_function (GST_ELEMENT (avi), gst_avi_demux_loop);
  gst_avi_demux_reset (avi);
Wim Taymans's avatar
Wim Taymans committed
190

191
192
193
  avi->streaminfo = NULL;
  avi->index_entries = NULL;
  memset (&avi->stream, 0, sizeof (avi->stream));
Wim Taymans's avatar
Wim Taymans committed
194
195
}

196
static void
197
gst_avi_demux_reset (GstAviDemux *avi)
198
{
199
  gint i;
200

201
202
203
204
205
  for (i = 0; i < avi->num_streams; i++) {
    g_free (avi->stream[i].strh);
    gst_element_remove_pad (GST_ELEMENT (avi), avi->stream[i].pad);
  }
  memset (&avi->stream, 0, sizeof (avi->stream));
206

207
208
209
  avi->num_streams = 0;
  avi->num_v_streams = 0;
  avi->num_a_streams = 0;
210

211
212
  avi->state = GST_AVI_DEMUX_START;
  avi->level_up = 0;
213

214
215
216
  if (avi->index_entries) {
    g_free (avi->index_entries);
    avi->index_entries = NULL;
217
  }
218
  avi->index_size = 0;
219

220
221
  avi->num_frames = 0;
  avi->us_per_frame = 0;
222

223
  avi->seek_offset = (guint64) -1;
224

225
  gst_caps_replace (&avi->streaminfo, NULL);
226
227
228
}

static void
229
gst_avi_demux_streaminfo (GstAviDemux *avi)
230
231
232
{
  /* compression formats are added later - a bit hacky */

David Schleef's avatar
David Schleef committed
233
234
  gst_caps_replace (&avi->streaminfo,
      gst_caps_new_simple ("application/x-gst-streaminfo", NULL));
235

236
  /*g_object_notify(G_OBJECT(avi), "streaminfo");*/
237
238
}

239
240
241
242
243
static gst_avi_index_entry *
gst_avi_demux_index_next (GstAviDemux *avi,
			  gint         stream_nr,
			  gint         start,
			  guint32      flags)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
244
{
245
246
  gint i;
  gst_avi_index_entry *entry = NULL;
247

248
249
  for (i = start; i < avi->index_size; i++) {
    entry = &avi->index_entries[i];
250

251
    if (entry->stream_nr == stream_nr && (entry->flags & flags) == flags) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
252
      break;
253
254
    }
  }
255

256
257
  return entry;
}
258

259
260
261
262
263
264
265
266
static gst_avi_index_entry *
gst_avi_demux_index_entry_for_time (GstAviDemux *avi,
				    gint         stream_nr,
				    guint64      time,
				    guint32      flags)
{
  gst_avi_index_entry *entry = NULL, *last_entry = NULL;
  gint i;
267

268
269
270
271
272
  i = -1;
  do {
    entry = gst_avi_demux_index_next (avi, stream_nr, i + 1, flags);
    if (!entry)
      return NULL;
273

274
    i = entry->index_nr;
275

276
277
278
279
    if (entry->ts <= time) {
      last_entry = entry;
    }
  } while (entry->ts <= time);
280

281
282
  return last_entry;
}
283

284
285
286
287
288
289
290
291
static gst_avi_index_entry *
gst_avi_demux_index_entry_for_byte (GstAviDemux *avi,
				    gint         stream_nr,
				    guint64      byte,
				    guint32      flags)
{
  gst_avi_index_entry *entry = NULL, *last_entry = NULL;
  gint i;
292

293
294
295
296
297
  i = -1;
  do {
    entry = gst_avi_demux_index_next (avi, stream_nr, i + 1, flags);
    if (!entry)
      return NULL;
298

299
    i = entry->index_nr;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
300

301
302
303
304
    if (entry->bytes_before <= byte) {
      last_entry = entry;
    }
  } while (entry->bytes_before <= byte);
305

306
  return last_entry;
307
308
}

309
310
311
312
313
static gst_avi_index_entry *
gst_avi_demux_index_entry_for_frame (GstAviDemux *avi,
				     gint         stream_nr,
				     guint32      frame,
				     guint32      flags)
314
{
315
316
  gst_avi_index_entry *entry = NULL, *last_entry = NULL;
  gint i;
317

318
319
320
321
322
  i = -1;
  do {
    entry = gst_avi_demux_index_next (avi, stream_nr, i + 1, flags);
    if (!entry)
      return NULL;
323

324
    i = entry->index_nr;
325

326
327
328
329
    if (entry->frames_before <= frame) {
      last_entry = entry;
    }
  } while (entry->frames_before <= frame);
330

331
  return last_entry;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
332
333
}

334
335
static const GstFormat *
gst_avi_demux_get_src_formats (GstPad *pad) 
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
336
{
337
  avi_stream_context *stream = gst_pad_get_element_private (pad);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
338

339
340
341
342
343
344
345
346
347
348
349
  static const GstFormat src_a_formats[] = {
    GST_FORMAT_TIME,
    GST_FORMAT_BYTES,
    GST_FORMAT_DEFAULT,
    0
  };
  static const GstFormat src_v_formats[] = {
    GST_FORMAT_TIME,
    GST_FORMAT_DEFAULT,
    0
  };
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
350

351
352
353
  return (stream->strh->type == GST_RIFF_FCC_auds ?
	  src_a_formats : src_v_formats);
}
354

355
356
357
358
359
360
361
362
363
364
static gboolean
gst_avi_demux_src_convert (GstPad    *pad,
			   GstFormat  src_format,
			   gint64     src_value,
	                   GstFormat *dest_format,
			   gint64    *dest_value)
{
  gboolean res = TRUE;
  /*GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));*/
  avi_stream_context *stream = gst_pad_get_element_private (pad);
365

366
367
368
369
  if (stream->strh->type != GST_RIFF_FCC_auds && 
      (src_format == GST_FORMAT_BYTES ||
       *dest_format == GST_FORMAT_BYTES))
    return FALSE;
370

371
372
373
374
375
376
377
378
379
380
381
382
383
384
  switch (src_format) {
    case GST_FORMAT_TIME:
      switch (*dest_format) {
	case GST_FORMAT_BYTES:
          *dest_value = src_value * stream->strh->rate /
			(stream->strh->scale * GST_SECOND);
          break;
        case GST_FORMAT_DEFAULT:
          *dest_value = src_value * stream->strh->rate /
			(stream->strh->scale * GST_SECOND);
          break;
	default:
	  res = FALSE;
	  break;
385
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
386
      break;
387
388
389
390
391
392
393
394
    case GST_FORMAT_BYTES:
      switch (*dest_format) {
	case GST_FORMAT_TIME:
          *dest_value = ((gfloat) src_value) * GST_SECOND / stream->strh->rate;
	  break;
	default:
	  res = FALSE;
	  break;
395
396
      }
      break;
397
398
399
400
401
402
403
404
405
    case GST_FORMAT_DEFAULT:
      switch (*dest_format) {
	case GST_FORMAT_TIME:
          *dest_value = ((((gfloat) src_value) * stream->strh->scale)  /
			stream->strh->rate) * GST_SECOND;
	  break;
	default:
	  res = FALSE;
	  break;
406
      }
Wim Taymans's avatar
Wim Taymans committed
407
408
      break;
    default:
409
      res = FALSE;
410
  }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
411

412
  return res;
413
414
}

415
416
static const GstQueryType *
gst_avi_demux_get_src_query_types (GstPad *pad) 
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
417
{
418
419
420
421
422
  static const GstQueryType src_types[] = {
    GST_QUERY_TOTAL,
    GST_QUERY_POSITION,
    0
  };
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
423

424
  return src_types;
Wim Taymans's avatar
Wim Taymans committed
425
426
}

427
428
429
430
431
static gboolean
gst_avi_demux_handle_src_query (GstPad      *pad,
				GstQueryType type, 
				GstFormat   *format,
				gint64      *value)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
432
{
433
434
435
  gboolean res = TRUE;
  /*GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));*/
  avi_stream_context *stream = gst_pad_get_element_private (pad);
Wim Taymans's avatar
Wim Taymans committed
436
437

  switch (type) {
Wim Taymans's avatar
Wim Taymans committed
438
    case GST_QUERY_TOTAL:
Wim Taymans's avatar
Wim Taymans committed
439
440
      switch (*format) {
        case GST_FORMAT_TIME:
441
442
          *value = (((gfloat) stream->strh->scale) * stream->strh->length /
			stream->strh->rate) * GST_SECOND;
Wim Taymans's avatar
Wim Taymans committed
443
444
	  break;
        case GST_FORMAT_BYTES:
445
          if (stream->strh->type == GST_RIFF_FCC_auds) {
446
447
            *value = stream->total_bytes;
	  }
Wim Taymans's avatar
Wim Taymans committed
448
449
450
	  else
	    res = FALSE;
	  break;
Wim Taymans's avatar
Wim Taymans committed
451
        case GST_FORMAT_DEFAULT:
452
453
454
455
          if (stream->strh->type == GST_RIFF_FCC_auds)
            *value = stream->strh->length * stream->strh->samplesize;
	  else if (stream->strh->type == GST_RIFF_FCC_vids)
            *value = stream->strh->length;
Wim Taymans's avatar
Wim Taymans committed
456
457
458
459
460
461
462
463
	  else
	    res = FALSE;
	  break;
	default:
          res = FALSE;
	  break;
      }
      break;
Wim Taymans's avatar
Wim Taymans committed
464
    case GST_QUERY_POSITION:
Wim Taymans's avatar
Wim Taymans committed
465
466
      switch (*format) {
        case GST_FORMAT_TIME:
467
468
469
470
          if (stream->strh->samplesize &&
	      stream->strh->type == GST_RIFF_FCC_auds) {
            *value = ((gfloat) stream->current_byte) * GST_SECOND /
			stream->strh->rate;
471
472
	  }
	  else {
473
474
            *value = (((gfloat) stream->current_frame) * stream->strh->scale /
			stream->strh->rate) * GST_SECOND;
475
	  }
Wim Taymans's avatar
Wim Taymans committed
476
477
478
479
	  break;
        case GST_FORMAT_BYTES:
          *value = stream->current_byte;
	  break;
Wim Taymans's avatar
Wim Taymans committed
480
        case GST_FORMAT_DEFAULT:
481
482
483
          if (stream->strh->samplesize &&
	      stream->strh->type == GST_RIFF_FCC_auds) 
            *value = stream->current_byte * stream->strh->samplesize;
484
	  else 
Wim Taymans's avatar
Wim Taymans committed
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
            *value = stream->current_frame;
	  break;
	default:
          res = FALSE;
	  break;
      }
      break;
    default:
      res = FALSE;
      break;
  }

  return res;
}

500
501
502
503
504
505
506
507
static GstCaps *
gst_avi_demux_src_getcaps (GstPad *pad)
{
  avi_stream_context *stream = gst_pad_get_element_private (pad);

  return gst_caps_copy (stream->caps);
}

Wim Taymans's avatar
Wim Taymans committed
508
static gint32
509
510
gst_avi_demux_sync_streams (GstAviDemux *avi,
			    guint64      time)
Wim Taymans's avatar
Wim Taymans committed
511
512
{
  gint i;
Wim Taymans's avatar
Wim Taymans committed
513
  guint32 min_index = G_MAXUINT;
Wim Taymans's avatar
Wim Taymans committed
514
515
516
  avi_stream_context *stream;
  gst_avi_index_entry *entry;

517
518
  for (i = 0; i < avi->num_streams; i++) {
    stream = &avi->stream[i];
Wim Taymans's avatar
Wim Taymans committed
519

520
    GST_DEBUG ("finding %d for time %" G_GINT64_FORMAT, i, time);
Wim Taymans's avatar
Wim Taymans committed
521

522
523
    entry = gst_avi_demux_index_entry_for_time (avi, stream->num, time,
						GST_RIFF_IF_KEYFRAME);
Wim Taymans's avatar
Wim Taymans committed
524
525
526
527
    if (entry) {
      min_index = MIN (entry->index_nr, min_index);
    }
  }
528
  GST_DEBUG ("first index at %d", min_index);
Wim Taymans's avatar
Wim Taymans committed
529
530
531
  
  /* now we know the entry we need to sync on. calculate number of frames to
   * skip fro there on and the stream stats */
532
  for (i = 0; i < avi->num_streams; i++) {
Wim Taymans's avatar
Wim Taymans committed
533
    gst_avi_index_entry *next_entry;
534
    stream = &avi->stream[i];
Wim Taymans's avatar
Wim Taymans committed
535
536

    /* next entry */
537
538
    next_entry = gst_avi_demux_index_next (avi, stream->num,
					   min_index, 0);
Wim Taymans's avatar
Wim Taymans committed
539
    /* next entry with keyframe */
540
541
    entry = gst_avi_demux_index_next (avi, stream->num, min_index,
				      GST_RIFF_IF_KEYFRAME);
Wim Taymans's avatar
Wim Taymans committed
542
543
544
545
546

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

547
    GST_DEBUG ("%d skip %d", stream->num, stream->skip);
Wim Taymans's avatar
Wim Taymans committed
548
  }
549

550
  GST_DEBUG ("final index at %d", min_index);
Wim Taymans's avatar
Wim Taymans committed
551
552
553
554

  return min_index;
}

555
static gboolean
556
557
gst_avi_demux_send_event (GstElement *element,
			  GstEvent   *event)
558
{
Wim Taymans's avatar
Wim Taymans committed
559
  const GList *pads;
560
561
562
563
564
565
566

  pads = gst_element_get_pad_list (element);

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

    if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
Wim Taymans's avatar
Wim Taymans committed
567
568
569
570
571
      /* we ref the event here as we might have to try again if the event
       * failed on this pad */
      gst_event_ref (event);
      if (gst_avi_demux_handle_src_event (pad, event)) {
	gst_event_unref (event);
572

Wim Taymans's avatar
Wim Taymans committed
573
574
	return TRUE;
      }
575
576
577
578
579
    }
    
    pads = g_list_next (pads);
  }
  
Wim Taymans's avatar
Wim Taymans committed
580
  gst_event_unref (event);
581

582
583
584
  return FALSE;
}

585
static const GstEventMask *
586
587
588
589
590
591
592
593
594
595
gst_avi_demux_get_event_mask (GstPad *pad)
{
  static const GstEventMask masks[] = {
    { GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_KEY_UNIT },
    { 0, }
  };

  return masks;
}
	
Wim Taymans's avatar
Wim Taymans committed
596
static gboolean
597
598
gst_avi_demux_handle_src_event (GstPad   *pad,
				GstEvent *event)
Wim Taymans's avatar
Wim Taymans committed
599
600
{
  gboolean res = TRUE;
601
  GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
Wim Taymans's avatar
Wim Taymans committed
602
603
604
605
606
607
  avi_stream_context *stream;
  
  stream = gst_pad_get_element_private (pad);

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_SEEK:
608
609
610
      GST_DEBUG ("seek format %d, %08x", GST_EVENT_SEEK_FORMAT (event),
		 stream->strh->type);

Wim Taymans's avatar
Wim Taymans committed
611
612
      switch (GST_EVENT_SEEK_FORMAT (event)) {
	case GST_FORMAT_BYTES:
Wim Taymans's avatar
Wim Taymans committed
613
	case GST_FORMAT_DEFAULT:
614
615
	case GST_FORMAT_TIME: {
	  gst_avi_index_entry *seek_entry, *entry = NULL;
Wim Taymans's avatar
Wim Taymans committed
616
617
618
619
620
	  gint64 desired_offset = GST_EVENT_SEEK_OFFSET (event);
	  guint32 flags;
          guint64 min_index;

	  /* no seek on audio yet */
621
	  if (stream->strh->type == GST_RIFF_FCC_auds) {
Wim Taymans's avatar
Wim Taymans committed
622
623
624
	    res = FALSE;
	    goto done;
	  }
625
          GST_DEBUG ("seeking to %" G_GINT64_FORMAT, desired_offset);
Wim Taymans's avatar
Wim Taymans committed
626
627

          flags = GST_RIFF_IF_KEYFRAME;
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
          switch (GST_EVENT_SEEK_FORMAT (event)) {
	    case GST_FORMAT_BYTES:
              entry = gst_avi_demux_index_entry_for_byte (avi, stream->num,
							  desired_offset,
							  flags);
              break;
	    case GST_FORMAT_DEFAULT:
              entry = gst_avi_demux_index_entry_for_frame (avi, stream->num,
							   desired_offset,
							   flags);
              break;
	    case GST_FORMAT_TIME:
              entry = gst_avi_demux_index_entry_for_time (avi, stream->num,
							  desired_offset,
							  flags);
              break;
          }
Wim Taymans's avatar
Wim Taymans committed
645
646

	  if (entry) {
647
648
649
650
651
652
653
654
655
	    min_index = gst_avi_demux_sync_streams (avi, entry->ts);
            seek_entry = &avi->index_entries[min_index];

	    avi->seek_offset = seek_entry->offset + avi->index_offset;
            avi->last_seek = entry->ts;
	  } else {
            GST_DEBUG ("no index entry found for format=%d value=%"
		       G_GINT64_FORMAT, GST_EVENT_SEEK_FORMAT (event),
		       desired_offset);
Wim Taymans's avatar
Wim Taymans committed
656
657
658
659
660
661
662
663
664
665
666
667
668
	    res = FALSE;
	  }
	  break;
	}
	default:
	  res = FALSE;
          break;
      }
      break;
    default:
      res = FALSE;
      break;
  }
Wim Taymans's avatar
Wim Taymans committed
669
670
671
672

done:
  gst_event_unref (event);

Wim Taymans's avatar
Wim Taymans committed
673
674
675
  return res;
}

676
677
678
679
680
681
/*
 * "Open" a RIFF file.
 */

gboolean
gst_avi_demux_stream_init (GstAviDemux *avi)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
682
{
683
684
  GstRiffRead *riff = GST_RIFF_READ (avi);
  guint32 doctype;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
685

686
687
688
  if (!gst_riff_read_header (riff, &doctype))
    return FALSE;
  if (doctype != GST_RIFF_RIFF_AVI) {
689
    gst_element_error (avi, STREAM, WRONG_TYPE, NULL, NULL);
690
691
    return FALSE;
  }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
692

693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
  return TRUE;
}

/*
 * Read 'avih' header.
 */

gboolean
gst_avi_demux_stream_avih (GstAviDemux *avi,
			   guint32     *flags,
			   guint32     *streams)
{
  GstRiffRead *riff = GST_RIFF_READ (avi);
  guint32 tag;
  GstBuffer *buf;
708
  gst_riff_avih avih, *_avih;
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724

  if (!gst_riff_read_data (riff, &tag, &buf))
    return FALSE;

  if (tag != GST_RIFF_TAG_avih) {
    g_warning ("Not a avih chunk");
    gst_buffer_unref (buf);
    return FALSE;
  }
  if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_avih)) {
    g_warning ("Too small avih (%d available, %d needed)",
	       GST_BUFFER_SIZE (buf), sizeof (gst_riff_avih));
    gst_buffer_unref (buf);
    return FALSE;
  }

725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
  _avih = (gst_riff_avih *) GST_BUFFER_DATA (buf);
  avih.us_frame    = GUINT32_FROM_LE (_avih->us_frame);
  avih.max_bps     = GUINT32_FROM_LE (_avih->max_bps);
  avih.pad_gran    = GUINT32_FROM_LE (_avih->pad_gran);
  avih.flags       = GUINT32_FROM_LE (_avih->flags);
  avih.tot_frames  = GUINT32_FROM_LE (_avih->tot_frames);
  avih.init_frames = GUINT32_FROM_LE (_avih->init_frames);
  avih.streams     = GUINT32_FROM_LE (_avih->streams);
  avih.bufsize     = GUINT32_FROM_LE (_avih->bufsize);
  avih.width       = GUINT32_FROM_LE (_avih->width);
  avih.height      = GUINT32_FROM_LE (_avih->height);
  avih.scale       = GUINT32_FROM_LE (_avih->scale);
  avih.rate        = GUINT32_FROM_LE (_avih->rate);
  avih.start       = GUINT32_FROM_LE (_avih->start);
  avih.length      = GUINT32_FROM_LE (_avih->length);
740
741
742

  /* debug stuff */
  GST_INFO ("avih tag found:");
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
  GST_INFO (" us_frame    %u",     avih.us_frame);
  GST_INFO (" max_bps     %u",     avih.max_bps);
  GST_INFO (" pad_gran    %u",     avih.pad_gran);
  GST_INFO (" flags       0x%08x", avih.flags);
  GST_INFO (" tot_frames  %u",     avih.tot_frames);
  GST_INFO (" init_frames %u",     avih.init_frames);
  GST_INFO (" streams     %u",     avih.streams);
  GST_INFO (" bufsize     %u",     avih.bufsize);
  GST_INFO (" width       %u",     avih.width);
  GST_INFO (" height      %u",     avih.height);
  GST_INFO (" scale       %u",     avih.scale);
  GST_INFO (" rate        %u",     avih.rate);
  GST_INFO (" start       %u",     avih.start);
  GST_INFO (" length      %u",     avih.length);

  avi->num_frames = avih.tot_frames;
  avi->us_per_frame = avih.us_frame;
  *streams = avih.streams;
  *flags = avih.flags;
762
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
796
797
798
799
800
801
802
803

  gst_buffer_unref (buf);

  return TRUE;
}

/*
 * Add a stream.
 */

static gboolean
gst_avi_demux_add_stream (GstAviDemux *avi)
{
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (avi);
  GstRiffRead *riff = GST_RIFF_READ (avi);
  guint32 tag;
  gst_riff_strh *strh;
  gchar *name = NULL, *padname = NULL;
  GstCaps *caps = NULL;
  GstPadTemplate *templ = NULL;
  GstPad *pad;
  avi_stream_context *stream;
  union {
    gst_riff_strf_vids *vids;
    gst_riff_strf_auds *auds;
    gst_riff_strf_iavs *iavs;
  } strf;

  /* the stream starts with a 'strh' header */
  if (!(tag = gst_riff_peek_tag (riff, NULL)))
    return FALSE;
  if (tag != GST_RIFF_TAG_strh) {
    g_warning ("Invalid stream header (no strh at begin)");
    goto skip_stream;
  }
  if (!gst_riff_read_strh (riff, &strh))
    return FALSE;

  /* then comes a 'strf' of that specific type */
  if (!(tag = gst_riff_peek_tag (riff, NULL)))
    return FALSE;
  if (tag != GST_RIFF_TAG_strf) {
804
805
    gst_element_error (avi, STREAM, DEMUX, NULL,
		       ("Invalid AVI header (no strf as second tag)"));
806
807
808
809
810
811
812
813
814
815
816
817
818
819
    goto skip_stream;
  }
  switch (strh->type) {
    case GST_RIFF_FCC_vids:
      if (!gst_riff_read_strf_vids (riff, &strf.vids))
        return FALSE;
      break;
    case GST_RIFF_FCC_auds:
      if (!gst_riff_read_strf_auds (riff, &strf.auds))
        return FALSE;
      break;
    case GST_RIFF_FCC_iavs:
      if (!gst_riff_read_strf_iavs (riff, &strf.iavs))
        return FALSE;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
820
      break;
821
822
823
824
825
    default:
      g_warning ("Unknown stream type " GST_FOURCC_FORMAT,
		 GST_FOURCC_ARGS (strh->type));
      goto skip_stream;
  }
Wim Taymans's avatar
Wim Taymans committed
826

827
828
829
830
831
832
833
834
  /* read other things */
  while (TRUE) {
    if (!(tag = gst_riff_peek_tag (riff, &avi->level_up)))
      return FALSE;
    else if (avi->level_up) {
      avi->level_up--;
      break;
    }
Wim Taymans's avatar
Wim Taymans committed
835

836
837
838
839
840
841
842
    switch (tag) {
      case GST_RIFF_TAG_strn:
        if (name)
          g_free (name);
        if (!gst_riff_read_ascii (riff, &tag, &name))
          return FALSE;
        break;
Wim Taymans's avatar
Wim Taymans committed
843

844
845
846
847
848
849
850
851
852
853
854
      default:
        GST_WARNING ("Unknown tag " GST_FOURCC_FORMAT " in AVI header",
		     GST_FOURCC_ARGS (tag));
        /* fall-through */

      case GST_RIFF_TAG_strd: /* what is this? */
      case GST_RIFF_TAG_JUNK:
        if (!gst_riff_read_skip (riff))
          return FALSE;
        break;
    }
Wim Taymans's avatar
Wim Taymans committed
855

856
857
    if (avi->level_up) {
      avi->level_up--;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
858
      break;
Wim Taymans's avatar
Wim Taymans committed
859
    }
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
  }

  /* create stream name + pad */
  switch (strh->type) {
    case GST_RIFF_FCC_vids:
      padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
      templ = gst_element_class_get_pad_template (klass, "video_%02d");
      caps = gst_riff_create_video_caps (strf.vids->compression, strh, strf.vids);
      g_free (strf.vids);
      avi->num_v_streams++;
      break;
    case GST_RIFF_FCC_auds:
      padname = g_strdup_printf ("audio_%02d", avi->num_a_streams);
      templ = gst_element_class_get_pad_template (klass, "audio_%02d");
      caps = gst_riff_create_audio_caps (strf.auds->format, strh, strf.auds);
      g_free (strf.auds);
      avi->num_a_streams++;
      break;
    case GST_RIFF_FCC_iavs:
      padname = g_strdup_printf ("video_%02d", avi->num_v_streams);
      templ = gst_element_class_get_pad_template (klass, "video_%02d");
      caps = gst_riff_create_iavs_caps (strh->fcc_handler, strh, strf.iavs);
      g_free (strf.iavs);
      avi->num_v_streams++;
      break;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
885
    default:
886
887
888
889
890
891
892
893
894
895
896
897
898
      g_assert (0);
  }

  /* set proper settings and add it */
  pad =  gst_pad_new_from_template (templ, padname);
  g_free (padname);

  gst_pad_set_formats_function (pad, gst_avi_demux_get_src_formats);
  gst_pad_set_event_mask_function (pad, gst_avi_demux_get_event_mask);
  gst_pad_set_event_function (pad, gst_avi_demux_handle_src_event);
  gst_pad_set_query_type_function (pad, gst_avi_demux_get_src_query_types);
  gst_pad_set_query_function (pad, gst_avi_demux_handle_src_query);
  gst_pad_set_convert_function (pad, gst_avi_demux_src_convert);
899
  gst_pad_set_getcaps_function (pad, gst_avi_demux_src_getcaps);
900
901

  stream = &avi->stream[avi->num_streams];
902
  stream->caps = caps ? caps : gst_caps_new_empty ();
903
904
905
906
907
908
909
910
911
912
913
914
915
  stream->pad = pad;
  stream->strh = strh;
  stream->num = avi->num_streams;
  stream->delay = 0LL;
  stream->total_bytes = 0LL;
  stream->total_frames = 0;
  stream->current_frame = 0;
  stream->current_byte = 0;
  stream->current_entry = -1;
  stream->skip = 0;
  gst_pad_set_element_private (pad, stream);
  avi->num_streams++;

916
  /* auto-negotiates */
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
  gst_element_add_pad (GST_ELEMENT (avi), pad);

  return TRUE;

skip_stream:
  while (TRUE) {
    if (!(tag = gst_riff_peek_tag (riff, &avi->level_up)))
      return FALSE;
    if (avi->level_up) {
      avi->level_up--;
      break;
    }
    if (!gst_riff_read_skip (riff))
      return FALSE;
  }

  /* add a "NULL" stream */
  avi->num_streams++;

  return TRUE; /* recoverable */
}

/*
 * Read an openDML-2.0 extension header.
 */

static gboolean
gst_avi_demux_stream_odml (GstAviDemux *avi)
{
  GstRiffRead *riff = GST_RIFF_READ (avi);
  guint32 tag;

  /* read contents */
  while (TRUE) {
    if (!(tag = gst_riff_peek_tag (riff, &avi->level_up)))
      return FALSE;
    else if (avi->level_up) {
      avi->level_up--;
      break;
    }

    switch (tag) {
      case GST_RIFF_TAG_dmlh: {
960
        gst_riff_dmlh dmlh, *_dmlh;
961
962
963
964
965
966
967
968
969
970
        GstBuffer *buf;

        if (!gst_riff_read_data (riff, &tag, &buf))
          return FALSE;
        if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_dmlh)) {
          g_warning ("DMLH entry is too small (%d bytes, %d needed)",
		     GST_BUFFER_SIZE (buf), sizeof (gst_riff_dmlh));
          gst_buffer_unref (buf);
          break;
        }
971
972
        _dmlh = (gst_riff_dmlh *) GST_BUFFER_DATA (buf);
        dmlh.totalframes = GUINT32_FROM_LE (_dmlh->totalframes);
973
974

        GST_INFO ("dmlh tag found:");
975
        GST_INFO (" totalframes: %u", dmlh.totalframes);
976

977
        avi->num_frames = dmlh.totalframes;
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
        gst_buffer_unref (buf);
        break;
      }

      default:
        GST_WARNING ("Unknown tag " GST_FOURCC_FORMAT " in AVI header",
		     GST_FOURCC_ARGS (tag));
        /* fall-through */

      case GST_RIFF_TAG_JUNK:
        if (!gst_riff_read_skip (riff))
          return FALSE;
        break;
    }

    if (avi->level_up) {
      avi->level_up--;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
995
      break;
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
    }
  }

  return TRUE;
}

/*
 * Seek to index, read it, seek back.
 */

gboolean
gst_avi_demux_stream_index (GstAviDemux *avi)
{
  GstBuffer *buf = NULL;
  guint i;
  GstEvent *event;
  GstRiffRead *riff = GST_RIFF_READ (avi);
  guint64 pos_before, pos_after, length;
  guint32 tag;

  /* first, we need to know the current position (to seek back
   * when we're done) and the total length of the file. */
  length = gst_bytestream_length (riff->bs);
  pos_before = gst_bytestream_tell (riff->bs);

  /* skip movi */
  if (!gst_riff_read_skip (riff))
    return FALSE;

  /* assure that we've got data left */
  pos_after = gst_bytestream_tell (riff->bs);
  if (pos_after + 8 > length) {
    g_warning ("File said that it has an index, but there is no index data!");
    goto end;
  }

  /* assure that it's an index */
  if (!(tag = gst_riff_peek_tag (riff, NULL)))
    return FALSE;
  if (tag != GST_RIFF_TAG_idx1) {
    g_warning ("No index after data, but " GST_FOURCC_FORMAT,
	       GST_FOURCC_ARGS (tag));
    goto end;
  }

  /* read index */
  if (!gst_riff_read_data (riff, &tag, &buf))
    return FALSE;

  /* parse all entries */
  avi->index_size = GST_BUFFER_SIZE (buf) / sizeof (gst_riff_index_entry);
  avi->index_entries = g_malloc (avi->index_size * sizeof (gst_avi_index_entry));
  GST_INFO ("%u index entries", avi->index_size);

  for (i = 0; i < avi->index_size; i++) {
1051
    gst_riff_index_entry entry, *_entry;
1052
1053
1054
1055
1056
    avi_stream_context *stream;
    gint stream_nr;
    gst_avi_index_entry *target;
    GstFormat format;

1057
1058
1059
1060
1061
    _entry = &((gst_riff_index_entry *) GST_BUFFER_DATA (buf))[i];
    entry.id     = GUINT32_FROM_LE (_entry->id);
    entry.offset = GUINT32_FROM_LE (_entry->offset);
    entry.flags  = GUINT32_FROM_LE (_entry->flags);
    entry.size   = GUINT32_FROM_LE (_entry->size);
1062
1063
    target = &avi->index_entries[i];

1064
    stream_nr = CHUNKID_TO_STREAMNR (entry.id);
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
    if (stream_nr >= avi->num_streams || stream_nr < 0) {
      g_warning ("Index entry %d has invalid stream nr %d",
		 i, stream_nr);
      target->stream_nr = -1;
      continue;
    }
    target->stream_nr = stream_nr;
    stream = &avi->stream[stream_nr];

    target->index_nr = i;
1075
1076
1077
    target->flags    = entry.flags;
    target->size     = entry.size;
    target->offset   = entry.offset;
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116

    /* figure out if the index is 0 based or relative to the MOVI start */
    if (i == 0) {
      if (target->offset < pos_before)
	avi->index_offset = pos_before + 8;
      else
	avi->index_offset = 0;
    }

    target->bytes_before = stream->total_bytes;
    target->frames_before = stream->total_frames;

    format = GST_FORMAT_TIME;
    if (stream->strh->type == GST_RIFF_FCC_auds) {
      /* all audio frames are keyframes */
      target->flags |= GST_RIFF_IF_KEYFRAME;
    }
      
    if (stream->strh->samplesize && stream->strh->type == GST_RIFF_FCC_auds) {
      /* constant rate stream */
      gst_pad_convert (stream->pad, GST_FORMAT_BYTES,
		       stream->total_bytes, &format, &target->ts);
    } else {
      /* VBR stream */
      gst_pad_convert (stream->pad, GST_FORMAT_DEFAULT,
		       stream->total_frames, &format, &target->ts);
    }

    stream->total_bytes += target->size;
    stream->total_frames++;
  }

  /* debug our indexes */
  for (i = 0; i < avi->num_streams; i++) {
    avi_stream_context *stream;

    stream = &avi->stream[i];
    GST_DEBUG ("stream %u: %u frames, %" G_GINT64_FORMAT " bytes", 
	       i, stream->total_frames, stream->total_bytes);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1117
1118
  }

1119
1120
1121
1122
1123
1124
1125
end:
  if (buf)
    gst_buffer_unref (buf);

  /* seek back to the data */
  if (!(event = gst_riff_read_seek (riff, pos_before)))
    return FALSE;
Wim Taymans's avatar
Wim Taymans committed
1126
1127
  gst_event_unref (event);

1128
1129
  return TRUE;
}
Wim Taymans's avatar
Wim Taymans committed
1130

1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
/*
 * Scan the file for all chunks to "create" a new index.
 */

gboolean
gst_avi_demux_stream_scan (GstAviDemux *avi)
{
  //GstRiffRead *riff = GST_RIFF_READ (avi);

  /* FIXME */

  return TRUE;
Wim Taymans's avatar
Wim Taymans committed
1143
1144
}

1145
1146
1147
/*
 * Read full AVI headers.
 */
1148

1149
1150
gboolean
gst_avi_demux_stream_header (GstAviDemux *avi)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1151
{
1152
1153
1154
1155
1156
1157
1158
  GstRiffRead *riff = GST_RIFF_READ (avi);
  guint32 tag, flags, streams;

  /* the header consists of a 'hdrl' LIST tag */
  if (!(tag = gst_riff_peek_tag (riff, NULL)))
    return FALSE;
  if (tag != GST_RIFF_TAG_LIST) {
1159
1160
1161
    gst_element_error (avi, STREAM, DEMUX, NULL,
		       ("Invalid AVI header (no LIST at start): "
		       GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1162
1163
1164
1165
1166
    return FALSE;
  }
  if (!gst_riff_read_list (riff, &tag))
    return FALSE;
  if (tag != GST_RIFF_LIST_hdrl) {
1167
1168
1169
    gst_element_error (avi, STREAM, DEMUX, NULL,
		       ("Invalid AVI header (no hdrl at start): "
		       GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1170
1171
1172
1173
1174
1175
1176
    return FALSE;
  }

  /* the hdrl starts with a 'avih' header */
  if (!(tag = gst_riff_peek_tag (riff, NULL)))
    return FALSE;
  if (tag != GST_RIFF_TAG_avih) {
1177
1178
1179
    gst_element_error (avi, STREAM, DEMUX, NULL,
		       ("Invalid AVI header (no avih at start): "
		       GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
    return FALSE;
  }
  if (!gst_avi_demux_stream_avih (avi, &flags, &streams))
    return FALSE;

  /* now, read the elements from the header until the end */
  while (TRUE) {
    if (!(tag = gst_riff_peek_tag (riff, &avi->level_up)))
      return FALSE;
    else if (avi->level_up) {
      avi->level_up--;
      break;
    }

    switch (tag) {
      case GST_RIFF_TAG_LIST:
        if (!(tag = gst_riff_peek_list (riff)))
          return FALSE;

        switch (tag) {
          case GST_RIFF_LIST_strl:
            if (!gst_riff_read_list (riff, &tag) ||
                !gst_avi_demux_add_stream (avi))
              return FALSE;
            break;

          case GST_RIFF_LIST_odml:
            if (!gst_riff_read_list (riff, &tag) ||
                !gst_avi_demux_stream_odml (avi))
              return FALSE;
            break;

          default:
            GST_WARNING ("Unknown list " GST_FOURCC_FORMAT " in AVI header",
			 GST_FOURCC_ARGS (tag));
            /* fall-through */

          case GST_RIFF_TAG_JUNK:
            if (!gst_riff_read_skip (riff))
              return FALSE;
            break;
        }

        break;

      default:
        GST_WARNING ("Unknown tag " GST_FOURCC_FORMAT " in AVI header",
		     GST_FOURCC_ARGS (tag));
        /* fall-through */

      case GST_RIFF_TAG_JUNK:
        if (!gst_riff_read_skip (riff))
          return FALSE;
        break;
    }

    if (avi->level_up) {
      avi->level_up--;
      break;
1239
1240
    }
  }
1241

1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
  if (avi->num_streams != streams) {
    g_warning ("Stream header mentioned %d streams, but %d available",
	       streams, avi->num_streams);
  }

  /* we've got streaminfo now */
  g_object_notify (G_OBJECT(avi), "streaminfo");

  /* Now, find the data (i.e. skip all junk between header and data) */
  while (1) {
    if (!(tag = gst_riff_peek_tag (riff, NULL)))
      return FALSE;
    if (tag != GST_RIFF_TAG_LIST) {
      if (!gst_riff_read_skip (riff))
        return FALSE;
      continue;
    }
    if (!(tag = gst_riff_peek_list (riff)))
      return FALSE;
    if (tag != GST_RIFF_LIST_movi) {