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 */
};

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

56
static void 	gst_avi_demux_base_init	        (GstAviDemuxClass *klass);
57
static void 	gst_avi_demux_class_init	(GstAviDemuxClass *klass);
58
static void 	gst_avi_demux_init		(GstAviDemux *avi);
59

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

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

87
88
89
90
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
91

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

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

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

    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
117
  }
118

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
119
120
121
  return avi_demux_type;
}

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

137
  audcaps = gst_riff_create_audio_template_caps ();
138
139
140
141
  audiosrctempl = gst_pad_template_new ("audio_%02d",
					GST_PAD_SRC,
					GST_PAD_SOMETIMES,
					audcaps, NULL);
142
143
144
145

  vidcaps = gst_caps_append (
		gst_riff_create_video_template_caps (),
		gst_riff_create_iavs_template_caps ());
146
147
148
149
  videosrctempl = gst_pad_template_new ("video_%02d",
					GST_PAD_SRC,
					GST_PAD_SOMETIMES,
					vidcaps, NULL);
150

151
152
153
154
155
156
157
  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
158
159
160
161
162
163
164
165
166
static void
gst_avi_demux_class_init (GstAviDemuxClass *klass) 
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

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

167
168
169
  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
170

171
172
173
174
  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
175
176
  
  gobject_class->get_property = gst_avi_demux_get_property;
Wim Taymans's avatar
Wim Taymans committed
177
178
  
  gstelement_class->change_state = gst_avi_demux_change_state;
179
  gstelement_class->send_event = gst_avi_demux_send_event;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
180
181
182
}

static void 
183
gst_avi_demux_init (GstAviDemux *avi) 
184
{
185
  GST_FLAG_SET (avi, GST_ELEMENT_EVENT_AWARE);
186

187
188
189
190
  avi->sinkpad = gst_pad_new_from_template (
	GST_PAD_TEMPLATE_GET (sink_templ), "sink");
  gst_element_add_pad (GST_ELEMENT (avi), avi->sinkpad);
  GST_RIFF_READ (avi)->sinkpad = avi->sinkpad;
Wim Taymans's avatar
Wim Taymans committed
191

192
193
  gst_element_set_loop_function (GST_ELEMENT (avi), gst_avi_demux_loop);
  gst_avi_demux_reset (avi);
Wim Taymans's avatar
Wim Taymans committed
194

195
196
197
  avi->streaminfo = NULL;
  avi->index_entries = NULL;
  memset (&avi->stream, 0, sizeof (avi->stream));
Wim Taymans's avatar
Wim Taymans committed
198
199
}

200
static void
201
gst_avi_demux_reset (GstAviDemux *avi)
202
{
203
  gint i;
204

205
206
207
208
209
  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));
210

211
212
213
  avi->num_streams = 0;
  avi->num_v_streams = 0;
  avi->num_a_streams = 0;
214

215
216
  avi->state = GST_AVI_DEMUX_START;
  avi->level_up = 0;
217

218
219
220
  if (avi->index_entries) {
    g_free (avi->index_entries);
    avi->index_entries = NULL;
221
  }
222
  avi->index_size = 0;
223

224
225
  avi->num_frames = 0;
  avi->us_per_frame = 0;
226

227
  avi->seek_offset = (guint64) -1;
228

229
  gst_caps_replace (&avi->streaminfo, NULL);
230
231
232
}

static void
233
gst_avi_demux_streaminfo (GstAviDemux *avi)
234
235
236
237
238
239
240
{
  GstProps *props;

  props = gst_props_empty_new ();

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

241
242
243
244
  gst_caps_replace_sink (&avi->streaminfo,
		  	 gst_caps_new ("avi_streaminfo",
                                       "application/x-gst-streaminfo",
                                       props));
245

246
  /*g_object_notify(G_OBJECT(avi), "streaminfo");*/
247
248
}

249
250
251
252
253
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
254
{
255
256
  gint i;
  gst_avi_index_entry *entry = NULL;
257

258
259
  for (i = start; i < avi->index_size; i++) {
    entry = &avi->index_entries[i];
260

261
    if (entry->stream_nr == stream_nr && (entry->flags & flags) == flags) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
262
      break;
263
264
    }
  }
265

266
267
  return entry;
}
268

269
270
271
272
273
274
275
276
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;
277

278
279
280
281
282
  i = -1;
  do {
    entry = gst_avi_demux_index_next (avi, stream_nr, i + 1, flags);
    if (!entry)
      return NULL;
283

284
    i = entry->index_nr;
285

286
287
288
289
    if (entry->ts <= time) {
      last_entry = entry;
    }
  } while (entry->ts <= time);
290

291
292
  return last_entry;
}
293

294
295
296
297
298
299
300
301
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;
302

303
304
305
306
307
  i = -1;
  do {
    entry = gst_avi_demux_index_next (avi, stream_nr, i + 1, flags);
    if (!entry)
      return NULL;
308

309
    i = entry->index_nr;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
310

311
312
313
314
    if (entry->bytes_before <= byte) {
      last_entry = entry;
    }
  } while (entry->bytes_before <= byte);
315

316
  return last_entry;
317
318
}

319
320
321
322
323
static gst_avi_index_entry *
gst_avi_demux_index_entry_for_frame (GstAviDemux *avi,
				     gint         stream_nr,
				     guint32      frame,
				     guint32      flags)
324
{
325
326
  gst_avi_index_entry *entry = NULL, *last_entry = NULL;
  gint i;
327

328
329
330
331
332
  i = -1;
  do {
    entry = gst_avi_demux_index_next (avi, stream_nr, i + 1, flags);
    if (!entry)
      return NULL;
333

334
    i = entry->index_nr;
335

336
337
338
339
    if (entry->frames_before <= frame) {
      last_entry = entry;
    }
  } while (entry->frames_before <= frame);
340

341
  return last_entry;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
342
343
}

344
345
static const GstFormat *
gst_avi_demux_get_src_formats (GstPad *pad) 
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
346
{
347
  avi_stream_context *stream = gst_pad_get_element_private (pad);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
348

349
350
351
352
353
354
355
356
357
358
359
  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
360

361
362
363
  return (stream->strh->type == GST_RIFF_FCC_auds ?
	  src_a_formats : src_v_formats);
}
364

365
366
367
368
369
370
371
372
373
374
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);
375

376
377
378
379
  if (stream->strh->type != GST_RIFF_FCC_auds && 
      (src_format == GST_FORMAT_BYTES ||
       *dest_format == GST_FORMAT_BYTES))
    return FALSE;
380

381
382
383
384
385
386
387
388
389
390
391
392
393
394
  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;
395
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
396
      break;
397
398
399
400
401
402
403
404
    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;
405
406
      }
      break;
407
408
409
410
411
412
413
414
415
    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;
416
      }
Wim Taymans's avatar
Wim Taymans committed
417
418
      break;
    default:
419
      res = FALSE;
420
  }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
421

422
  return res;
423
424
}

425
426
static const GstQueryType *
gst_avi_demux_get_src_query_types (GstPad *pad) 
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
427
{
428
429
430
431
432
  static const GstQueryType src_types[] = {
    GST_QUERY_TOTAL,
    GST_QUERY_POSITION,
    0
  };
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
433

434
  return src_types;
Wim Taymans's avatar
Wim Taymans committed
435
436
}

437
438
439
440
441
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
442
{
443
444
445
  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
446
447

  switch (type) {
Wim Taymans's avatar
Wim Taymans committed
448
    case GST_QUERY_TOTAL:
Wim Taymans's avatar
Wim Taymans committed
449
450
      switch (*format) {
        case GST_FORMAT_TIME:
451
452
          *value = (((gfloat) stream->strh->scale) * stream->strh->length /
			stream->strh->rate) * GST_SECOND;
Wim Taymans's avatar
Wim Taymans committed
453
454
	  break;
        case GST_FORMAT_BYTES:
455
          if (stream->strh->type == GST_RIFF_FCC_auds) {
456
457
            *value = stream->total_bytes;
	  }
Wim Taymans's avatar
Wim Taymans committed
458
459
460
	  else
	    res = FALSE;
	  break;
Wim Taymans's avatar
Wim Taymans committed
461
        case GST_FORMAT_DEFAULT:
462
463
464
465
          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
466
467
468
469
470
471
472
473
	  else
	    res = FALSE;
	  break;
	default:
          res = FALSE;
	  break;
      }
      break;
Wim Taymans's avatar
Wim Taymans committed
474
    case GST_QUERY_POSITION:
Wim Taymans's avatar
Wim Taymans committed
475
476
      switch (*format) {
        case GST_FORMAT_TIME:
477
478
479
480
          if (stream->strh->samplesize &&
	      stream->strh->type == GST_RIFF_FCC_auds) {
            *value = ((gfloat) stream->current_byte) * GST_SECOND /
			stream->strh->rate;
481
482
	  }
	  else {
483
484
            *value = (((gfloat) stream->current_frame) * stream->strh->scale /
			stream->strh->rate) * GST_SECOND;
485
	  }
Wim Taymans's avatar
Wim Taymans committed
486
487
488
489
	  break;
        case GST_FORMAT_BYTES:
          *value = stream->current_byte;
	  break;
Wim Taymans's avatar
Wim Taymans committed
490
        case GST_FORMAT_DEFAULT:
491
492
493
          if (stream->strh->samplesize &&
	      stream->strh->type == GST_RIFF_FCC_auds) 
            *value = stream->current_byte * stream->strh->samplesize;
494
	  else 
Wim Taymans's avatar
Wim Taymans committed
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
            *value = stream->current_frame;
	  break;
	default:
          res = FALSE;
	  break;
      }
      break;
    default:
      res = FALSE;
      break;
  }

  return res;
}

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

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

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

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

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

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

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

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

  return min_index;
}

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

  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
569
570
571
572
573
      /* 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);
574

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

584
585
586
  return FALSE;
}

587
static const GstEventMask *
588
589
590
591
592
593
594
595
596
597
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
598
static gboolean
599
600
gst_avi_demux_handle_src_event (GstPad   *pad,
				GstEvent *event)
Wim Taymans's avatar
Wim Taymans committed
601
602
{
  gboolean res = TRUE;
603
  GstAviDemux *avi = GST_AVI_DEMUX (gst_pad_get_parent (pad));
Wim Taymans's avatar
Wim Taymans committed
604
605
606
607
608
609
  avi_stream_context *stream;
  
  stream = gst_pad_get_element_private (pad);

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

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

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

          flags = GST_RIFF_IF_KEYFRAME;
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
          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
647
648

	  if (entry) {
649
650
651
652
653
654
655
656
657
	    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
658
659
660
661
662
663
664
665
666
667
668
669
670
	    res = FALSE;
	  }
	  break;
	}
	default:
	  res = FALSE;
          break;
      }
      break;
    default:
      res = FALSE;
      break;
  }
Wim Taymans's avatar
Wim Taymans committed
671
672
673
674

done:
  gst_event_unref (event);

Wim Taymans's avatar
Wim Taymans committed
675
676
677
  return res;
}

678
679
680
681
682
683
/*
 * "Open" a RIFF file.
 */

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

688
689
690
691
692
693
  if (!gst_riff_read_header (riff, &doctype))
    return FALSE;
  if (doctype != GST_RIFF_RIFF_AVI) {
    gst_element_error (GST_ELEMENT (avi), "Not an AVI file");
    return FALSE;
  }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
694

695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
  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;
  gst_riff_avih *avih;

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

  avih = (gst_riff_avih *) GST_BUFFER_DATA (buf);

#if (G_BYTE_ORDER == G_BIG_ENDIAN)
  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);
#endif

  /* debug stuff */
  GST_INFO ("avih tag found:");
  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;

  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) {
    gst_element_error (GST_ELEMENT (avi),
		       "Invalid AVI header (no strf as second tag)");
    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
825
      break;
826
827
828
829
830
    default:
      g_warning ("Unknown stream type " GST_FOURCC_FORMAT,
		 GST_FOURCC_ARGS (strh->type));
      goto skip_stream;
  }
Wim Taymans's avatar
Wim Taymans committed
831

832
833
834
835
836
837
838
839
  /* 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
840

841
842
843
844
845
846
847
    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
848

849
850
851
852
853
854
855
856
857
858
859
      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
860

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

  /* 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
890
    default:
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
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
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
      g_assert (0);
  }

  /* set proper settings and add it */
  pad =  gst_pad_new_from_template (templ, padname);
  g_free (padname);
  if (caps != NULL)
    gst_pad_try_set_caps (pad, caps);

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

  stream = &avi->stream[avi->num_streams];
  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++;

  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: {
        gst_riff_dmlh *dmlh;
        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;
        }
        dmlh = (gst_riff_dmlh *) GST_BUFFER_DATA (buf);

#if (G_BYTE_ORDER == G_BIG_ENDIAN)
        dmlh->totalframes = GUINT32_FROM_LE (dmlh->totalframes);
#endif

        GST_INFO ("dmlh tag found:");
        GST_INFO (" totalframes: %u", dmlh->totalframes);

        avi->num_frames = dmlh->totalframes;
        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
1002
      break;
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
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
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
1117
1118
1119
1120
1121
1122
1123
    }
  }

  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++) {
    gst_riff_index_entry *entry;
    avi_stream_context *stream;
    gint stream_nr;
    gst_avi_index_entry *target;
    GstFormat format;

    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);
    target = &avi->index_entries[i];

    stream_nr = CHUNKID_TO_STREAMNR (entry->id);
    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;
    target->flags    = entry->flags;
    target->size     = entry->size;
    target->offset   = entry->offset;

    /* 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
1124
1125
  }

1126
1127
1128
1129
1130
1131
1132
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
1133
1134
  gst_event_unref (event);

1135
1136
  return TRUE;
}
Wim Taymans's avatar
Wim Taymans committed
1137

1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
/*
 * 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
1150
1151
}

1152
1153
1154
/*
 * Read full AVI headers.
 */
1155

1156
1157
gboolean
gst_avi_demux_stream_header (GstAviDemux *avi)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1158
{
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
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
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
  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) {
    gst_element_error (GST_ELEMENT (avi),
		       "Invalid AVI header (no LIST at start): "
		       GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag));
    return FALSE;
  }
  if (!gst_riff_read_list (riff, &tag))
    return FALSE;
  if (tag != GST_RIFF_LIST_hdrl) {
    gst_element_error (GST_ELEMENT (avi),
		       "Invalid AVI header (no hdrl at start): "
		       GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag));
    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) {
    gst_element_error (GST_ELEMENT (avi),
		       "Invalid AVI header (no avih at start): "
		       GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag));
    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;

          case GST_RIFF_LIST_INFO:
            if (!gst_riff_read_list (riff, &tag) ||
                !gst_riff_read_info (riff))
              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;
1252
1253
    }
  }
1254

1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
  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) {
      if (!gst_riff_read_skip (riff))
        return FALSE;
      continue;
1278
    }
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
    break;
  }

  /* create or read stream index (for seeking) */
  if (flags & GST_RIFF_AVIH_HASINDEX) {
    if (!gst_avi_demux_stream_index (avi))
      return FALSE;
  } else {
    if (!gst_avi_demux_stream_scan (avi))
      return FALSE;
  }

  return TRUE;
}

/*
 * Handle seek.
 */

static gboolean
gst_avi_demux_handle_seek (GstAviDemux *avi)
{
  GstRiffRead *riff = GST_RIFF_READ (avi);
  guint i;
  GstEvent *event;

  /* FIXME: if we seek in an openDML file, we will have multiple
   * primary levels. Seeking in between those will cause havoc. */
Wim Taymans's avatar
Wim Taymans committed
1307

1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
  if (!(event = gst_riff_read_seek (riff, avi->seek_offset)))
    return FALSE;
  gst_event_unref (event);

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

    if (GST_PAD_IS_USABLE (stream->pad)) {
      event = gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME, 
				avi->last_seek + stream->delay , NULL);
      gst_pad_push (stream->pad, GST_DATA (event));
    }
  }
1321

1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
  return TRUE;
}

/*
 * Read data.
 */

gboolean
gst_avi_demux_stream_data (GstAviDemux *avi)
{
  GstRiffRead *riff = GST_RIFF_READ (avi);
  guint32 tag;
  guint stream_nr;
  gst_avi_index_entry *entry;

  if (avi->seek_offset != (guint64) -1) {
    if (!gst_avi_demux_handle_seek (avi))
      return FALSE;
    avi->seek_offset = (guint64) -1;
  }

  /* peek first (for the end of this 'list/movi' section) */
  if (!(tag = gst_riff_peek_tag (riff, &avi->level_up)))
    return FALSE;

  /* if we're at top-level, we didn't read the 'movi'
   * list tag yet. This can also be 'AVIX' in case of
   * openDML-2.0 AVI files. Lastly, it might be idx1,
   * in which case we skip it so we come at EOS. */
  while (g_list_length (riff->level) < 2) {
    if (!(tag = gst_riff_peek_tag (riff, NULL)))
      return FALSE;

    switch (tag) {
1356
      case GST_RIFF_TAG_LIST:
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
        if (!(tag = gst_riff_peek_list (riff)))
          return FALSE;

        switch (tag) {
          case GST_RIFF_LIST_AVIX:
          case GST_RIFF_LIST_movi:
            if (!gst_riff_read_list (riff, &tag))
              return FALSE;
            /* we're now going to read buffers! */
            break;

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

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

1379
        break;
1380

1381
      default:
1382
1383
1384
1385
1386
1387
1388
1389
        GST_WARNING ("Unknown tag " GST_FOURCC_FORMAT " before AVI data",
		     GST_FOURCC_ARGS (tag));
        /* fall-through */

      case GST_RIFF_TAG_idx1:
      case GST_RIFF_TAG_JUNK:
        if (!gst_riff_read_skip (riff))
          return FALSE;
1390
        break;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1391
    }
1392
  }
1393

1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
  /* And then, we get the data */
  if (!(tag = gst_riff_peek_tag (riff, NULL)))
    return FALSE;
  stream_nr = CHUNKID_TO_STREAMNR (tag);
  if (stream_nr < 0 || stream_nr >= avi->num_streams) {
    /* recoverable */
    g_warning ("Invalid stream ID %d (" GST_FOURCC_FORMAT ")",
	       stream_nr, GST_FOURCC_ARGS (tag));
    if (!gst_riff_read_skip (riff))