vorbisfile.c 28.9 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * 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.
 */

#include <string.h>
#include <gst/gst.h>
#include <vorbis/codec.h>
#include <vorbis/vorbisfile.h>
Wim Taymans's avatar
Wim Taymans committed
24
#include <gst/bytestream/bytestream.h>
Wim Taymans's avatar
Wim Taymans committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

#define GST_TYPE_VORBISFILE \
  (vorbisfile_get_type())
#define GST_VORBISFILE(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_VORBISFILE,VorbisFile))
#define GST_VORBISFILE_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_VORBISFILE,VorbisFileClass))
#define GST_IS_VORBISFILE(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_VORBISFILE))
#define GST_IS_VORBISFILE_CLASS(obj) \
  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_VORBISFILE))

typedef struct _VorbisFile VorbisFile;
typedef struct _VorbisFileClass VorbisFileClass;

struct _VorbisFile {
  GstElement element;

  GstPad *sinkpad,*srcpad;
  GstByteStream *bs;

  OggVorbis_File vf;
  gint current_section;

  gboolean restart;
  gboolean need_discont;
51
52
53
  gboolean get_metadata;		/* TRUE if we request metadata */
  gboolean have_metadata;	/* TRUE if we already have this */
  gboolean have_metadata_technical;	/* TRUE if we already have this */
Wim Taymans's avatar
Wim Taymans committed
54
55
56
57
58
59
60
61
62
63
64
  gboolean eos;
  gboolean seek_pending;
  gint64 seek_value;
  GstFormat seek_format;
  gboolean seek_accurate;

  gboolean may_eos;
  int16_t convsize;
  guint64 total_out;
  guint64 total_bytes;
  guint64 offset;
65
66
67
68
69
70
71
72
73
74
75
76
77
78

  int link;			/* stream id of logical bitstream */
  int streams;			/* number of streams in physical bitstream */
  int version;
  int channels;
  long samplerate;
  long bitrate;

  long bitrate_upper;
  long bitrate_nominal;
  long bitrate_lower;
  /* long bitrate_window; 	not set in 1.0 */

  GstCaps *metadata;
Wim Taymans's avatar
Wim Taymans committed
79
80
81
82
};

struct _VorbisFileClass {
  GstElementClass parent_class;
83
84
85

    /* signals */
    void (*have_metadata) (GstElement *element);
Wim Taymans's avatar
Wim Taymans committed
86
87
};

88
GType vorbisfile_get_type (void);
Wim Taymans's avatar
Wim Taymans committed
89

Wim Taymans's avatar
Wim Taymans committed
90
extern GstPadTemplate *gst_vorbisdec_src_template, *gst_vorbisdec_sink_template;
Wim Taymans's avatar
Wim Taymans committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

/* elementfactory information */
GstElementDetails vorbisfile_details = 
{
  "Ogg Vorbis decoder",
  "Codec/Audio/Decoder",
  "Decodes OGG Vorbis audio using the vorbisfile API",
  VERSION,
  "Monty <monty@xiph.org>, " 
  "Wim Taymans <wim.taymans@chello.be>",
  "(C) 2000",
};

/* VorbisFile signals and args */
enum
{
  /* FILL ME */
108
109
110
111
112
113
  SIGNAL_HAVE_METADATA,		/* FIXME: maybe separate out in
				    - descriptive
				    - technical
				      - easy
				      - hard (expensive)
				*/
Wim Taymans's avatar
Wim Taymans committed
114
115
116
117
118
119
  LAST_SIGNAL
};

enum
{
  ARG_0,
120
  ARG_METADATA,
Wim Taymans's avatar
Wim Taymans committed
121
122
123
124
  ARG_VENDOR,
  ARG_VERSION,
  ARG_CHANNELS,
  ARG_RATE,
125
126
127
  ARG_STREAMS,
  ARG_LINK,
  ARG_BITRATE,
Wim Taymans's avatar
Wim Taymans committed
128
129
130
131
132
133
  ARG_BITRATE_UPPER,
  ARG_BITRATE_NOMINAL,
  ARG_BITRATE_LOWER,
  ARG_BITRATE_WINDOW,
};

134
135
136
static void
		gst_vorbisfile_class_init 	(VorbisFileClass *klass);
static void 	gst_vorbisfile_init 		(VorbisFile *vorbisfile);
Wim Taymans's avatar
Wim Taymans committed
137
138

static GstElementStateReturn
139
140
141
142
143
144
145
146
147
		gst_vorbisfile_change_state 	(GstElement *element);

static const 
GstFormat* 	gst_vorbisfile_get_formats 	(GstPad *pad);
static gboolean gst_vorbisfile_src_convert 	(GstPad *pad, 
		                                 GstFormat src_format, 
						 gint64 src_value,
		           			 GstFormat *dest_format, 
						 gint64 *dest_value);
148
static const GstPadQueryType*
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
		gst_vorbisfile_get_query_types 	(GstPad *pad);

static gboolean gst_vorbisfile_src_query 	(GstPad *pad, 
		                                 GstPadQueryType type,
		        	 		 GstFormat *format, 
						 gint64 *value);
static const 
GstEventMask*	gst_vorbisfile_get_event_masks 	(GstPad *pad);
static gboolean gst_vorbisfile_src_event 	(GstPad *pad, GstEvent *event);

static void 	gst_vorbisfile_get_property 	(GObject *object, 
		            			 guint prop_id, 
						 GValue *value, 
						 GParamSpec *pspec);
static void 	gst_vorbisfile_set_property 	(GObject *object, 
		            			 guint prop_id, 
						 const GValue *value, 
						 GParamSpec *pspec);

static void 	gst_vorbisfile_loop 		(GstElement *element);
Wim Taymans's avatar
Wim Taymans committed
169
170

static GstElementClass *parent_class = NULL;
171
static guint gst_vorbisfile_signals[LAST_SIGNAL] = { 0 };
Wim Taymans's avatar
Wim Taymans committed
172
173
174
175
176
177
178
179

GType
vorbisfile_get_type (void)
{
  static GType vorbisfile_type = 0;

  if (!vorbisfile_type) {
    static const GTypeInfo vorbisfile_info = {
180
181
182
      sizeof (VorbisFileClass), NULL, NULL,
      (GClassInitFunc) gst_vorbisfile_class_init, NULL, NULL,
      sizeof (VorbisFile), 0,
Wim Taymans's avatar
Wim Taymans committed
183
184
185
      (GInstanceInitFunc) gst_vorbisfile_init,
    };

186
187
    vorbisfile_type = g_type_register_static (GST_TYPE_ELEMENT, "VorbisFile", 
		                              &vorbisfile_info, 0);
Wim Taymans's avatar
Wim Taymans committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  }
  return vorbisfile_type;
}

static void
gst_vorbisfile_class_init (VorbisFileClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

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

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);

203
204
205
  g_object_class_install_property (gobject_class, ARG_METADATA,
    g_param_spec_boxed ("metadata", "Metadata", "Metadata",
                         GST_TYPE_CAPS, G_PARAM_READABLE));
Wim Taymans's avatar
Wim Taymans committed
206
  g_object_class_install_property (gobject_class, ARG_VENDOR,
207
208
    g_param_spec_string ("vendor", "Vendor", 
	                 "The vendor for this vorbis stream",
Wim Taymans's avatar
Wim Taymans committed
209
210
211
212
213
214
215
                         "", G_PARAM_READABLE));
  g_object_class_install_property (gobject_class, ARG_VERSION,
    g_param_spec_int ("version", "Version", "The version",
                       0, G_MAXINT, 0, G_PARAM_READABLE));
  g_object_class_install_property (gobject_class, ARG_CHANNELS,
    g_param_spec_int ("channels", "Channels", "The number of channels",
                       0, G_MAXINT, 0, G_PARAM_READABLE));
216
217
218
219
220
221
222
223
224
  g_object_class_install_property (gobject_class, ARG_STREAMS,
    g_param_spec_int ("streams", "Streams", 
	              "Number of logical bitstreams in the physical bitstream",
                       0, G_MAXINT, 0, G_PARAM_READABLE));
  g_object_class_install_property (gobject_class, ARG_LINK,
    g_param_spec_int ("link", "Link", 
	              "The link id of the current logical bitstream",
                       -1, G_MAXINT, 0, G_PARAM_READWRITE));
   g_object_class_install_property (gobject_class, ARG_RATE,
Wim Taymans's avatar
Wim Taymans committed
225
226
    g_param_spec_int ("rate", "Rate", "The samplerate",
                       0, G_MAXINT, 0, G_PARAM_READABLE));
227
228
229
230
231
  g_object_class_install_property (gobject_class, ARG_BITRATE,
    g_param_spec_int ("bitrate", "Bitrate", 
	              "(average) bitrate in bits per second",
                       0, G_MAXINT, 0, G_PARAM_READABLE));
   g_object_class_install_property (gobject_class, ARG_BITRATE_UPPER,
Wim Taymans's avatar
Wim Taymans committed
232
233
234
235
236
237
238
239
240
241
242
243
244
    g_param_spec_int ("bitrate_upper", "bitrate_upper", "bitrate_upper",
                       0, G_MAXINT, 0, G_PARAM_READABLE));
  g_object_class_install_property (gobject_class, ARG_BITRATE_NOMINAL,
    g_param_spec_int ("bitrate_nominal", "bitrate_nominal", "bitrate_nominal",
                       0, G_MAXINT, 0, G_PARAM_READABLE));
  g_object_class_install_property (gobject_class, ARG_BITRATE_LOWER,
    g_param_spec_int ("bitrate_lower", "bitrate_lower", "bitrate_lower",
                       0, G_MAXINT, 0, G_PARAM_READABLE));
  g_object_class_install_property (gobject_class, ARG_BITRATE_WINDOW,
    g_param_spec_int ("bitrate_window", "bitrate_window", "bitrate_window",
                       0, G_MAXINT, 0, G_PARAM_READABLE));

  gobject_class->get_property = gst_vorbisfile_get_property;
245
246
247
248
249
250
251
  gobject_class->set_property = gst_vorbisfile_set_property;

  gst_vorbisfile_signals[SIGNAL_HAVE_METADATA] =
    g_signal_new ("have_metadata", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (VorbisFileClass, have_metadata), 
		  NULL, NULL, 
		  g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
Wim Taymans's avatar
Wim Taymans committed
252
253
254
255
256
257
258

  gstelement_class->change_state = gst_vorbisfile_change_state;
}

static void
gst_vorbisfile_init (VorbisFile * vorbisfile)
{
259
260
  vorbisfile->sinkpad = gst_pad_new_from_template (gst_vorbisdec_sink_template,
		                                   "sink");
Wim Taymans's avatar
Wim Taymans committed
261
  gst_element_add_pad (GST_ELEMENT (vorbisfile), vorbisfile->sinkpad);
262
  gst_pad_set_convert_function (vorbisfile->sinkpad, NULL);
Wim Taymans's avatar
Wim Taymans committed
263
264

  gst_element_set_loop_function (GST_ELEMENT (vorbisfile), gst_vorbisfile_loop);
265
266
  vorbisfile->srcpad = gst_pad_new_from_template (gst_vorbisdec_src_template, 
		                                  "src");
Wim Taymans's avatar
Wim Taymans committed
267
  gst_element_add_pad (GST_ELEMENT (vorbisfile), vorbisfile->srcpad);
268
  gst_pad_set_formats_function (vorbisfile->srcpad, gst_vorbisfile_get_formats);
269
270
  gst_pad_set_query_type_function (vorbisfile->srcpad, 
		                   gst_vorbisfile_get_query_types);
Wim Taymans's avatar
Wim Taymans committed
271
  gst_pad_set_query_function (vorbisfile->srcpad, gst_vorbisfile_src_query);
272
273
  gst_pad_set_event_mask_function (vorbisfile->srcpad, 
		                   gst_vorbisfile_get_event_masks);
Wim Taymans's avatar
Wim Taymans committed
274
  gst_pad_set_event_function (vorbisfile->srcpad, gst_vorbisfile_src_event);
275
  gst_pad_set_convert_function (vorbisfile->srcpad, gst_vorbisfile_src_convert);
Wim Taymans's avatar
Wim Taymans committed
276
277
278
279
280
281
282

  vorbisfile->convsize = 4096;
  vorbisfile->total_out = 0;
  vorbisfile->total_bytes = 0;
  vorbisfile->offset = 0;
  vorbisfile->seek_pending = 0;
  vorbisfile->need_discont = FALSE;
283
284
285
  vorbisfile->get_metadata = TRUE;
  vorbisfile->link = -1;	/* by default, ask for entire bitstream */
  vorbisfile->metadata = NULL;
Wim Taymans's avatar
Wim Taymans committed
286
287
}

288
289
290
/* the next four functions are the ov callbacks we provide to vorbisfile
 * which interface between GStreamer's handling of the data flow and
 * vorbis's needs */
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
317
318
319
320
321
322
323
324
static size_t
gst_vorbisfile_read (void *ptr, size_t size, size_t nmemb, void *datasource)
{
  guint32 got_bytes = 0;
  guint8 *data;
  size_t read_size = size * nmemb;

  VorbisFile *vorbisfile = GST_VORBISFILE (datasource);

  GST_DEBUG (0, "read %d", read_size);

  /* make sure we don't go to EOS */
  if (!vorbisfile->may_eos && vorbisfile->total_bytes && 
       vorbisfile->offset + read_size > vorbisfile->total_bytes) 
  {
    read_size = vorbisfile->total_bytes - vorbisfile->offset;
  }

  if (read_size == 0 || vorbisfile->eos)
    return 0;
  
  while (got_bytes == 0) {
    got_bytes = gst_bytestream_peek_bytes (vorbisfile->bs, &data, read_size);
    if (got_bytes < read_size) {
      GstEvent *event;
      guint32 avail;
    
      gst_bytestream_get_status (vorbisfile->bs, &avail, &event); 

      switch (GST_EVENT_TYPE (event)) {
	case GST_EVENT_EOS:
	  GST_DEBUG (0, "eos");
          vorbisfile->eos = TRUE;
          if (avail == 0) {
325
            gst_event_unref (event);
Wim Taymans's avatar
Wim Taymans committed
326
327
328
329
330
331
332
333
334
            return 0;
	  }
	  break;
	case GST_EVENT_DISCONTINUOUS:
	  GST_DEBUG (0, "discont");
	  vorbisfile->need_discont = TRUE;
	default:
          break;
      }
335
      gst_event_unref (event);
Wim Taymans's avatar
Wim Taymans committed
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
      if (avail > 0) 
        got_bytes = gst_bytestream_peek_bytes (vorbisfile->bs, &data, avail);
      else
	got_bytes = 0;
    }
  }

  memcpy (ptr, data, got_bytes);
  gst_bytestream_flush_fast (vorbisfile->bs, got_bytes);

  vorbisfile->offset += got_bytes;

  return got_bytes / size;
}

static int
gst_vorbisfile_seek (void *datasource, int64_t offset, int whence)
{
  VorbisFile *vorbisfile = GST_VORBISFILE (datasource);
  GstSeekType method;
  guint64 pending_offset = vorbisfile->offset;
  gboolean need_total = FALSE;


  if (!vorbisfile->vf.seekable) {
    return -1;
  }
  
  GST_DEBUG (0, "seek %lld %d", offset, whence);

  if (whence == SEEK_SET) {
    method = GST_SEEK_METHOD_SET;
    pending_offset = offset;
  }
  else if (whence == SEEK_CUR) {
    method = GST_SEEK_METHOD_CUR;
    pending_offset += offset;
  }
  else if (whence == SEEK_END) {
    method = GST_SEEK_METHOD_END;
    need_total = TRUE;
    pending_offset = vorbisfile->total_bytes - offset;
  }
  else 
    return -1;
  
  if (!gst_bytestream_seek (vorbisfile->bs, offset, method))
    return -1;

  vorbisfile->offset = pending_offset;
  if (need_total)
    vorbisfile->total_bytes = gst_bytestream_tell (vorbisfile->bs) + offset;

  return 0;
}

static int
gst_vorbisfile_close (void *datasource)
{
  GST_DEBUG (0, "close");
  return 0;
}

static long
gst_vorbisfile_tell (void *datasource)
{
  VorbisFile *vorbisfile = GST_VORBISFILE (datasource);
  long result;

  result = gst_bytestream_tell (vorbisfile->bs);

  GST_DEBUG (0, "tell %ld", result);

  return result;
}

ov_callbacks vorbisfile_ov_callbacks = 
{
  gst_vorbisfile_read,
  gst_vorbisfile_seek,
  gst_vorbisfile_close,
  gst_vorbisfile_tell,
};

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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/* retrieve the comment field and put tags in metadata GstCaps
 * returns TRUE if caps were still valid or could be set,
 * FALSE if they couldn't be read somehow */
static gboolean
gst_vorbisfile_get_metadata (VorbisFile *vorbisfile)
{
  OggVorbis_File *vf = &vorbisfile->vf;
  char **ptr;
  int link = vorbisfile->link;
  GstProps *props = NULL;
  GstPropsEntry *entry;
  gchar *name, *value;

  /* check if we need to update or not */
  if (vorbisfile->have_metadata)
    return TRUE;

  /* clear old one */
  if (vorbisfile->metadata) {
    gst_caps_unref (vorbisfile->metadata);
    vorbisfile->metadata = NULL;
  }

  ptr = ov_comment (vf, link)->user_comments;
  if (! (*ptr)) return FALSE;
  props = gst_props_empty_new ();

  while (*ptr) {
    value = strstr (*ptr, "=");
    if (value) {
      *value = 0;
      ++value;
      name = *ptr;
      entry = gst_props_entry_new (name, GST_PROPS_STRING_TYPE, value);
      gst_props_add_entry (props, (GstPropsEntry *) entry);
    }
    ++ptr;
  }

  vorbisfile->have_metadata = TRUE;
  vorbisfile->metadata = gst_caps_new ("vorbisfile_metadata",
		                       "application/x-gst-metadata",
		                       props);
  return TRUE;
}

/* retrieve techical metadata and update vorbisfile with it */
/* FIXME: write me */
/* FIXME: make this less more expensive; only update it if the
 * link id has changed */
static void
gst_vorbisfile_metadata_get_technical (VorbisFile *vorbisfile)
{
  OggVorbis_File *vf = &vorbisfile->vf;
  //vorbis_info *vi;
  //int link = vorbisfile->link;

  /* check if we need to update or not */
  if (vorbisfile->have_metadata_technical)
    return;

  /* FIXME: clear old one */

  /* get number of streams */
  vorbisfile->streams = ov_streams (vf);
}
Wim Taymans's avatar
Wim Taymans committed
486
487
488
489
static void
gst_vorbisfile_loop (GstElement *element)
{
  VorbisFile *vorbisfile = GST_VORBISFILE (element);
490
  OggVorbis_File *vf = &vorbisfile->vf;
Wim Taymans's avatar
Wim Taymans committed
491
492
  GstBuffer *outbuf;
  long ret;
Wim Taymans's avatar
Wim Taymans committed
493
494
  GstClockTime time;
  gint64 samples;
Wim Taymans's avatar
Wim Taymans committed
495

496
497
  /* this function needs to go first since you don't want to be messing
   * with an unset vf ;) */
Wim Taymans's avatar
Wim Taymans committed
498
499
500
501
502
  if (vorbisfile->restart) {
    vorbisfile->current_section = 0;
    vorbisfile->offset = 0;
    vorbisfile->total_bytes = 0;
    vorbisfile->may_eos = FALSE;
503
504
    vorbisfile->vf.seekable = gst_bytestream_seek (vorbisfile->bs, 0, 
		                                   GST_SEEK_METHOD_SET);
Wim Taymans's avatar
Wim Taymans committed
505

506
507
508
    /* open our custom vorbisfile data object with the callbacks we provide */
    if (ov_open_callbacks (vorbisfile, &vorbisfile->vf, NULL, 0, 
			   vorbisfile_ov_callbacks) < 0) {
Wim Taymans's avatar
Wim Taymans committed
509
510
511
512
513
514
515
      gst_element_error (element, "this is not a vorbis file");
      return;
    }
    vorbisfile->need_discont = TRUE;
    vorbisfile->restart = FALSE;
  }

516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
  /* get relevant data if asked for */
  if (vorbisfile->get_metadata) {
    vorbis_info *vi;
    double time_total;
    ogg_int64_t raw_total;
    int link = vorbisfile->link;

    if (gst_vorbisfile_get_metadata (vorbisfile))
      g_object_notify (G_OBJECT (vorbisfile), "metadata");


    /* get stream metadata */
    /* FIXME: due to a compiler bug, the actual vorbis bitrate function 
     * doesn't return a good number ... */
    vorbisfile->bitrate = ov_bitrate (vf, link);
    if (vorbisfile->bitrate == OV_EINVAL)
      g_warning ("OV_EINVAL: vf not open\n");
    if (vorbisfile->bitrate == OV_FALSE)
      g_warning ("OV_FALSE: non-seekable and no bitrate values set\n");

    /* ... so we calculate it by hand, which should be done better
     * by avoiding headers and stuff */
     time_total = ov_time_total (vf, link);
    raw_total = ov_raw_total (vf, link);
    g_print ("total time: %f\n", time_total);
    g_print ("abr: %d\n", 
	     (gint) ((guint64) raw_total / (guint64) time_total) * 8);
    vi = ov_info (vf, link);
    vorbisfile->version = vi->version;
    vorbisfile->samplerate = vi->rate;
    vorbisfile->channels = vi->channels;
    vorbisfile->bitrate_upper = vi->bitrate_upper;
    vorbisfile->bitrate_nominal = vi->bitrate_nominal;
    vorbisfile->bitrate_lower = vi->bitrate_lower;
    g_print ("Bitstream is %d channel, %ld Hz, %d version, %ld bitrate\n", 
	     vi->channels, vi->rate, vi->version, vorbisfile->bitrate);
    g_print ("bitrate: %f\n", (double) vorbisfile->bitrate);
    /* fire the signal saying we have metadata */
    g_signal_emit (G_OBJECT(vorbisfile), 
		   gst_vorbisfile_signals[SIGNAL_HAVE_METADATA], 0);
    vorbisfile->get_metadata = FALSE;
  }
 
Wim Taymans's avatar
Wim Taymans committed
559
  if (vorbisfile->seek_pending) {
560
561
562
    /* get time to seek to in seconds */
    gdouble seek_to = (gdouble) vorbisfile->seek_value / GST_SECOND;

Wim Taymans's avatar
Wim Taymans committed
563
564
565
    switch (vorbisfile->seek_format) {
      case GST_FORMAT_TIME:
	if (vorbisfile->seek_accurate) {
566
          if (ov_time_seek (&vorbisfile->vf, seek_to) == 0) {
Wim Taymans's avatar
Wim Taymans committed
567
568
569
570
            vorbisfile->need_discont = TRUE;
          }
        }
	else {
571
          if (ov_time_seek_page (&vorbisfile->vf, seek_to) == 0) {
Wim Taymans's avatar
Wim Taymans committed
572
573
574
575
            vorbisfile->need_discont = TRUE;
          }
	}
	break;
Wim Taymans's avatar
Wim Taymans committed
576
      case GST_FORMAT_UNITS:
Wim Taymans's avatar
Wim Taymans committed
577
	if (vorbisfile->seek_accurate) {
578
          if (ov_pcm_seek (&vorbisfile->vf, seek_to) == 0) {
Wim Taymans's avatar
Wim Taymans committed
579
580
581
582
            vorbisfile->need_discont = TRUE;
          }
        }
	else {
583
          if (ov_pcm_seek_page (&vorbisfile->vf, seek_to) == 0) {
Wim Taymans's avatar
Wim Taymans committed
584
585
586
587
588
            vorbisfile->need_discont = TRUE;
          }
	}
	break;
      default:
589
	g_warning ("unknown seek method, implement me !");
Wim Taymans's avatar
Wim Taymans committed
590
591
592
593
594
	break;
    }
    vorbisfile->seek_pending = FALSE;
  }

Wim Taymans's avatar
Wim Taymans committed
595
596
597
598
599
600
601
  outbuf = gst_buffer_new ();
  GST_BUFFER_DATA (outbuf) = g_malloc (4096);
  GST_BUFFER_SIZE (outbuf) = 4096;

  /* get current time for discont and buffer timestamp */
  time = (GstClockTime) (ov_time_tell (&vorbisfile->vf) * GST_SECOND);

602
603
604
605
  ret = ov_read (&vorbisfile->vf, 
		 GST_BUFFER_DATA (outbuf), GST_BUFFER_SIZE (outbuf), 
		 (G_BYTE_ORDER == G_LITTLE_ENDIAN ? 0 : 1), 
		 sizeof (gint16), 1, &vorbisfile->current_section);
Wim Taymans's avatar
Wim Taymans committed
606

Wim Taymans's avatar
Wim Taymans committed
607
608
609
  if (vorbisfile->need_discont) {
    GstEvent *discont;

Wim Taymans's avatar
Wim Taymans committed
610
611
    vorbisfile->need_discont = FALSE;

612
    /* if the pad is not usable, don't push it out */
Wim Taymans's avatar
Wim Taymans committed
613
614
615
    if (GST_PAD_IS_USABLE (vorbisfile->srcpad)) {
      /* get stream stats */
      samples = (gint64) (ov_pcm_tell (&vorbisfile->vf));
Wim Taymans's avatar
Wim Taymans committed
616

Wim Taymans's avatar
Wim Taymans committed
617
      discont = gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME, time, 
618
		    			     GST_FORMAT_UNITS, samples, NULL); 
Wim Taymans's avatar
Wim Taymans committed
619

Wim Taymans's avatar
Wim Taymans committed
620
621
      gst_pad_push (vorbisfile->srcpad, GST_BUFFER (discont));
    }
Wim Taymans's avatar
Wim Taymans committed
622
623
624
625
  }

  if (ret == 0) {
    GST_DEBUG (0, "eos");
626
627
628
    /* send EOS event */
    /*FIXME: should we do this or not ?
    ov_clear (&vorbisfile->vf);*/
Wim Taymans's avatar
Wim Taymans committed
629
    vorbisfile->restart = TRUE;
630
    gst_buffer_unref (outbuf);
631
    /* if the pad is not usable, don't push it out */
Wim Taymans's avatar
Wim Taymans committed
632
    if (GST_PAD_IS_USABLE (vorbisfile->srcpad)) 
633
634
      gst_pad_push (vorbisfile->srcpad, 
		    GST_BUFFER (gst_event_new (GST_EVENT_EOS)));
Wim Taymans's avatar
Wim Taymans committed
635
636
637
638
639
640
641
642
643
644
645
646
647
    gst_element_set_eos (element);
    return;
  }
  else if (ret < 0) {
    g_warning ("vorbisfile: decoding error");
    gst_buffer_unref (outbuf);
  }
  else {
    GST_BUFFER_SIZE (outbuf) = ret;

    if (!GST_PAD_CAPS (vorbisfile->srcpad)) {
      vorbis_info *vi = ov_info (&vorbisfile->vf, -1);
      
648
      if (gst_pad_try_set_caps (vorbisfile->srcpad,
Wim Taymans's avatar
Wim Taymans committed
649
                   GST_CAPS_NEW ("vorbisdec_src",
650
651
652
653
654
655
656
657
658
659
                                   "audio/raw",    
                                     "format",     GST_PROPS_STRING ("int"),
                                     "law",        GST_PROPS_INT (0),
                                     "endianness", GST_PROPS_INT (G_BYTE_ORDER),
                                     "signed",     GST_PROPS_BOOLEAN (TRUE),
                                     "width",      GST_PROPS_INT (16),
                                     "depth",      GST_PROPS_INT (16),
                                     "rate",       GST_PROPS_INT (vi->rate),
                                     "channels",   GST_PROPS_INT (vi->channels)
                                )) <= 0) 
660
661
      {
        gst_buffer_unref (outbuf);
662
663
	gst_element_error (GST_ELEMENT (vorbisfile), 
			   "could not negotiate format");
664
665
	return;
      }
Wim Taymans's avatar
Wim Taymans committed
666
667
668
    }

    vorbisfile->may_eos = TRUE;
669

Wim Taymans's avatar
Wim Taymans committed
670
    GST_BUFFER_TIMESTAMP (outbuf) = time;
671
672
673

    if (!vorbisfile->vf.seekable) {
      vorbisfile->total_bytes += GST_BUFFER_SIZE (outbuf);
674
    }
Wim Taymans's avatar
Wim Taymans committed
675
  
Wim Taymans's avatar
Wim Taymans committed
676
    if (GST_PAD_IS_USABLE (vorbisfile->srcpad)) 
677
      gst_pad_push (vorbisfile->srcpad, outbuf);
Wim Taymans's avatar
Wim Taymans committed
678
    else
679
      gst_buffer_unref (outbuf);
Wim Taymans's avatar
Wim Taymans committed
680
681
682
  }
}

683
684
685
686
687
688
689
690
691
692
693
694
static const GstFormat*
gst_vorbisfile_get_formats (GstPad *pad)
{
  static const GstFormat formats[] = {
    GST_FORMAT_TIME,
    GST_FORMAT_BYTES,
    GST_FORMAT_UNITS,
    0
  };
  return formats;
}

695
696
/* handles conversion of location offsets between two formats based on
 * src caps */
697
static gboolean
698
699
gst_vorbisfile_src_convert (GstPad *pad, 
		            GstFormat src_format, gint64 src_value,
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
		            GstFormat *dest_format, gint64 *dest_value)
{
  gboolean res = TRUE;
  guint scale = 1;
  gint bytes_per_sample;
  VorbisFile *vorbisfile; 
  vorbis_info *vi;
  
  vorbisfile = GST_VORBISFILE (gst_pad_get_parent (pad));

  vi = ov_info (&vorbisfile->vf, -1);
  bytes_per_sample = vi->channels * 2;

  switch (src_format) {
    case GST_FORMAT_BYTES:
      switch (*dest_format) {
        case GST_FORMAT_UNITS:
          *dest_value = src_value / (vi->channels * 2);
          break;
        case GST_FORMAT_DEFAULT:
          *dest_format = GST_FORMAT_TIME;
        case GST_FORMAT_TIME:
        {
          gint byterate = bytes_per_sample * vi->rate;

          if (byterate == 0)
            return FALSE;
          *dest_value = src_value * GST_SECOND / byterate;
          break;
        }
        default:
          res = FALSE;
      }
    case GST_FORMAT_UNITS:
      switch (*dest_format) {
        case GST_FORMAT_BYTES:
	  *dest_value = src_value * bytes_per_sample;
          break;
        case GST_FORMAT_DEFAULT:
          *dest_format = GST_FORMAT_TIME;
        case GST_FORMAT_TIME:
	  if (vi->rate == 0)
	    return FALSE;
	  *dest_value = src_value * GST_SECOND / vi->rate;
          break;
        default:
          res = FALSE;
      }
      break;
    case GST_FORMAT_TIME:
      switch (*dest_format) {
        case GST_FORMAT_DEFAULT:
          *dest_format = GST_FORMAT_BYTES;
        case GST_FORMAT_BYTES:
	  scale = bytes_per_sample;
        case GST_FORMAT_UNITS:
	  *dest_value = src_value * scale * vi->rate / GST_SECOND;
          break;
        default:
          res = FALSE;
      }
      break;
    default:
      res = FALSE;
      break;
  }

  return res;
}

static const GstPadQueryType*
gst_vorbisfile_get_query_types (GstPad *pad)
{
  static const GstPadQueryType types[] = {
    GST_PAD_QUERY_TOTAL,
    GST_PAD_QUERY_POSITION,
    0
  };
  return types;
}
780
781

/* handles queries for location in the stream in the requested format */
Wim Taymans's avatar
Wim Taymans committed
782
783
784
785
786
787
static gboolean
gst_vorbisfile_src_query (GstPad *pad, GstPadQueryType type,
		          GstFormat *format, gint64 *value)
{
  gboolean res = TRUE;
  VorbisFile *vorbisfile; 
788
789
  vorbis_info *vi;
  
Wim Taymans's avatar
Wim Taymans committed
790
791
  vorbisfile = GST_VORBISFILE (gst_pad_get_parent (pad));

792
793
  vi = ov_info (&vorbisfile->vf, -1);

Wim Taymans's avatar
Wim Taymans committed
794
795
796
797
  switch (type) {
    case GST_PAD_QUERY_TOTAL:
    {
      switch (*format) {
Wim Taymans's avatar
Wim Taymans committed
798
        case GST_FORMAT_UNITS:
799
800
801
802
          if (vorbisfile->vf.seekable)
	    *value = ov_pcm_total (&vorbisfile->vf, -1);
	  else
	    return FALSE;
Wim Taymans's avatar
Wim Taymans committed
803
804
	  break;
        case GST_FORMAT_BYTES:
805
806
807
808
          if (vorbisfile->vf.seekable)
	    *value = ov_pcm_total (&vorbisfile->vf, -1) * vi->channels * 2;
	  else
	    return FALSE;
Wim Taymans's avatar
Wim Taymans committed
809
810
811
812
813
	  break;
        case GST_FORMAT_DEFAULT:
          *format = GST_FORMAT_TIME;
          /* fall through */
        case GST_FORMAT_TIME:
814
815
816
817
          if (vorbisfile->vf.seekable)
	    *value = (gint64) (ov_time_total (&vorbisfile->vf, -1) * GST_SECOND);
	  else
	    return FALSE;
Wim Taymans's avatar
Wim Taymans committed
818
819
820
821
822
823
824
825
826
827
828
829
830
	  break;
	default:
          res = FALSE;
          break;
      }
      break;
    }
    case GST_PAD_QUERY_POSITION:
      switch (*format) {
        case GST_FORMAT_DEFAULT:
          *format = GST_FORMAT_TIME;
          /* fall through */
        case GST_FORMAT_TIME:
831
832
833
          if (vorbisfile->vf.seekable)
	    *value = (gint64) (ov_time_tell (&vorbisfile->vf) * GST_SECOND);
	  else
834
835
            *value = vorbisfile->total_bytes * GST_SECOND 
		                             / (vi->rate * vi->channels * 2);
836
837
838
839
840
841
	  break;
        case GST_FORMAT_BYTES:
          if (vorbisfile->vf.seekable)
	    *value = ov_pcm_tell (&vorbisfile->vf) * vi->channels * 2;
	  else
            *value = vorbisfile->total_bytes;
Wim Taymans's avatar
Wim Taymans committed
842
	  break;
Wim Taymans's avatar
Wim Taymans committed
843
        case GST_FORMAT_UNITS:
844
845
846
847
          if (vorbisfile->vf.seekable)
	    *value = ov_pcm_tell (&vorbisfile->vf);
	  else
            *value = vorbisfile->total_bytes / (vi->channels * 2);
Wim Taymans's avatar
Wim Taymans committed
848
849
850
851
852
853
854
855
856
857
858
859
860
	  break;
        default:
          res = FALSE;
          break;
      }
      break;
    default:
      res = FALSE;
      break;
  }

  return res;
}
861
862
863
864
865
866
867
868
869
870

static const GstEventMask*
gst_vorbisfile_get_event_masks (GstPad *pad)
{
  static const GstEventMask masks[] = {
    { GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_ACCURATE },
    { 0, }
  };
  return masks;
}
871
872

/* handle events on src pad */
Wim Taymans's avatar
Wim Taymans committed
873
874
875
876
877
878
879
880
881
882
static gboolean
gst_vorbisfile_src_event (GstPad *pad, GstEvent *event)
{
  gboolean res = TRUE;
  VorbisFile *vorbisfile; 
		  
  vorbisfile = GST_VORBISFILE (gst_pad_get_parent (pad));

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_SEEK:
883
884
885
886
    {
      gint64 offset;
      vorbis_info *vi;
  
887
888
      if (!vorbisfile->vf.seekable) {
	gst_event_unref (event);
889
        return FALSE;
890
      }
891

892
893
      offset = GST_EVENT_SEEK_OFFSET (event);

Wim Taymans's avatar
Wim Taymans committed
894
895
896
      switch (GST_EVENT_SEEK_FORMAT (event)) {
	case GST_FORMAT_TIME:
	  vorbisfile->seek_pending = TRUE;
897
	  vorbisfile->seek_value = offset;
Wim Taymans's avatar
Wim Taymans committed
898
	  vorbisfile->seek_format = GST_FORMAT_TIME;
899
900
	  vorbisfile->seek_accurate = GST_EVENT_SEEK_FLAGS (event) 
		                    & GST_SEEK_FLAG_ACCURATE;
Wim Taymans's avatar
Wim Taymans committed
901
	  break;
902
903
904
905
906
907
908
909
	case GST_FORMAT_BYTES:
          vi = ov_info (&vorbisfile->vf, -1);
	  if (vi->channels * 2 == 0) {
	    res = FALSE;
	    goto done; 
	  }
          offset /= vi->channels * 2;
	  /* fallthrough */
Wim Taymans's avatar
Wim Taymans committed
910
	case GST_FORMAT_UNITS:
Wim Taymans's avatar
Wim Taymans committed
911
	  vorbisfile->seek_pending = TRUE;
912
	  vorbisfile->seek_value = offset;
Wim Taymans's avatar
Wim Taymans committed
913
	  vorbisfile->seek_format = GST_FORMAT_UNITS;
914
915
	  vorbisfile->seek_accurate = GST_EVENT_SEEK_FLAGS (event) 
		                    & GST_SEEK_FLAG_ACCURATE;
Wim Taymans's avatar
Wim Taymans committed
916
917
918
919
920
921
	  break;
	default:
	  res = FALSE;
	  break;
      }
      break;
922
    }
Wim Taymans's avatar
Wim Taymans committed
923
924
925
926
    default:
      res = FALSE;
      break;
  }
927
928

done:
929
  gst_event_unref (event);
Wim Taymans's avatar
Wim Taymans committed
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
  return res;
}

static GstElementStateReturn
gst_vorbisfile_change_state (GstElement *element)
{
  VorbisFile *vorbisfile = GST_VORBISFILE (element);
  
  switch (GST_STATE_TRANSITION (element)) {
    case GST_STATE_NULL_TO_READY:
    case GST_STATE_READY_TO_PAUSED:
      vorbisfile->restart = TRUE;
      vorbisfile->bs = gst_bytestream_new (vorbisfile->sinkpad);
      break;
    case GST_STATE_PAUSED_TO_PLAYING:
      vorbisfile->eos = FALSE;
    case GST_STATE_PLAYING_TO_PAUSED:
      break;
    case GST_STATE_PAUSED_TO_READY:
      ov_clear (&vorbisfile->vf);
      gst_bytestream_destroy (vorbisfile->bs);
      break;
    case GST_STATE_READY_TO_NULL:
    default:
      break;
  }

  if (GST_ELEMENT_CLASS (parent_class)->change_state)
    return GST_ELEMENT_CLASS (parent_class)->change_state (element);
  
  return GST_STATE_SUCCESS;
}

963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
static void
gst_vorbisfile_set_property (GObject *object, guint prop_id, 
		             const GValue *value, GParamSpec *pspec)
{
  VorbisFile *vorbisfile;
	      
  g_return_if_fail (GST_IS_VORBISFILE (object));

  vorbisfile = GST_VORBISFILE (object);

  switch (prop_id) {
    case ARG_METADATA:
      break;
    case ARG_LINK:
      vorbisfile->link = g_value_get_int (value);
      break;
    default:
      g_warning ("Unknown property id\n");
  }
}

Wim Taymans's avatar
Wim Taymans committed
984
static void 
985
986
gst_vorbisfile_get_property (GObject *object, guint prop_id, 
		             GValue *value, GParamSpec *pspec)
Wim Taymans's avatar
Wim Taymans committed
987
988
989
990
991
992
993
{
  VorbisFile *vorbisfile;
	      
  g_return_if_fail (GST_IS_VORBISFILE (object));

  vorbisfile = GST_VORBISFILE (object);

994
  /* FIXME: the reupdate could be expensive */
Wim Taymans's avatar
Wim Taymans committed
995
  switch (prop_id) {
996
997
998
    case ARG_METADATA:
      gst_vorbisfile_get_metadata (vorbisfile);
      g_value_set_boxed (value, vorbisfile->metadata);
Wim Taymans's avatar
Wim Taymans committed
999
1000
1001
1002
1003
1004
1005
1006
1007
      break;
    case ARG_VENDOR:
      break;
    case ARG_VERSION:
      break;
    case ARG_CHANNELS:
      break;
    case ARG_RATE:
      break;
1008
1009
1010
1011
1012
1013
1014
1015
    case ARG_STREAMS:
      gst_vorbisfile_metadata_get_technical (vorbisfile);
      g_value_set_int (value, vorbisfile->streams);
      break;
    case ARG_LINK:
      break;
    case ARG_BITRATE:
      break;
Wim Taymans's avatar
Wim Taymans committed
1016
1017
1018
1019
1020
1021
1022
1023
    case ARG_BITRATE_UPPER:
      break;
    case ARG_BITRATE_NOMINAL:
      break;
    case ARG_BITRATE_LOWER:
      break;
    case ARG_BITRATE_WINDOW:
      break;
1024
1025
    default:
      g_warning ("Unknown property id\n");
Wim Taymans's avatar
Wim Taymans committed
1026
1027
  }
}