File: Decoder.xs

package info (click to toggle)
libogg-vorbis-decoder-perl 0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,560 kB
  • sloc: perl: 1,874; makefile: 2
file content (631 lines) | stat: -rw-r--r-- 12,686 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/* $Id: Decoder.xs 348 2005-07-14 02:32:46Z dsully $ */

#ifdef __cplusplus
"C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef __cplusplus
}
#endif

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <stdlib.h>

#ifdef _MSC_VER
# define alloca            _alloca
#endif

#include <vorbis/codec.h>
#include <vorbis/vorbisfile.h>

/* strlen the length automatically */
#define my_hv_store(a,b,c)   (void)hv_store(a,b,strlen(b),c,0)
#define my_hv_fetch(a,b)     hv_fetch(a,b,strlen(b),0)

#ifdef WORDS_BIGENDIAN
#define host_is_big_endian() TRUE
#else
#define host_is_big_endian() FALSE
#endif

int endian = host_is_big_endian();

static size_t ovcb_read(void *ptr, size_t size, size_t nmemb, void *datasource);
static int    ovcb_seek(void *datasource, ogg_int64_t offset, int whence);
static int    ovcb_close(void *datasource);
static long   ovcb_tell(void *datasource);

/* http://www.xiph.org/ogg/vorbis/doc/vorbisfile/ov_callbacks.html */
ov_callbacks vorbis_callbacks = {
	ovcb_read,
	ovcb_seek,
	ovcb_close,
	ovcb_tell
};

/* Allow multiple instances of the decoder object. Stuff each filehandle into (void*)stream */
typedef struct {
	int is_streaming;
	int bytes_streamed;
	int last_bitstream;
	PerlIO *stream;

} ocvb_datasource;

/* useful items from XMMS */
static size_t ovcb_read(void *ptr, size_t size, size_t nmemb, void *vdatasource) {

	size_t read_bytes = 0;

	ocvb_datasource *datasource = vdatasource;

	read_bytes = PerlIO_read(datasource->stream, ptr, size * nmemb);

	datasource->bytes_streamed += read_bytes;

	return read_bytes;
}

static int ovcb_seek(void *vdatasource, ogg_int64_t offset, int whence) {

	ocvb_datasource *datasource = vdatasource;

	if (datasource->is_streaming) {
		return -1;
	}

	/* For some reason PerlIO_seek fails miserably here. < 5.8.1 works */
	/* return PerlIO_seek(datasource->stream, offset, whence); */

	return fseek(PerlIO_findFILE(datasource->stream), offset, whence);
}

static int ovcb_close(void *vdatasource) {

	ocvb_datasource *datasource = vdatasource;

	return PerlIO_close(datasource->stream);
}

static long ovcb_tell(void *vdatasource) {

	ocvb_datasource *datasource = vdatasource;

	if (datasource->is_streaming) {
		return datasource->bytes_streamed;
	}

	return PerlIO_tell(datasource->stream);
}

/* Loads the commments from the stream and fills the object's hash */
void __read_comments(HV *self, OggVorbis_File *vf) {

	int i;
	char *half;

	/* XXX - rename these */
	HV *comments = newHV();
	SV *ts;
	AV *ta;

	vorbis_comment *vc = ov_comment(vf, -1);

	/* return early if there are no comments */
	if (!vc) return;

	for (i = 0; i < vc->comments; ++i) {

		half = strchr(vc->user_comments[i], '=');

		if (half == NULL) {
			warn("Comment \"%s\" missing \'=\', skipping...\n", vc->user_comments[i]);
			continue;
		}

		if (!hv_exists(comments, vc->user_comments[i], half - vc->user_comments[i])) {

			ta = newAV();
			ts = newRV_noinc((SV*) ta);

			(void)hv_store(comments, vc->user_comments[i], half - vc->user_comments[i], ts, 0);

		} else {

			ta = (AV*) SvRV(*(hv_fetch(comments, vc->user_comments[i], half - vc->user_comments[i], 0)));
		}

		av_push(ta, newSVpv(half + 1, 0));
	}

	my_hv_store(self, "COMMENTS", newRV_noinc((SV*) comments));
}

void __read_info(HV *self, OggVorbis_File *vf) {

	HV *info = newHV();

	vorbis_info *vi = ov_info(vf, -1);

	if (!vi) return;

	my_hv_store(info, "version", newSViv(vi->version));
	my_hv_store(info, "channels", newSViv(vi->channels));
	my_hv_store(info, "rate", newSViv(vi->rate));
	my_hv_store(info, "bitrate_upper", newSViv(vi->bitrate_upper));
	my_hv_store(info, "bitrate_nominal", newSViv(vi->bitrate_nominal));
	my_hv_store(info, "bitrate_lower", newSViv(vi->bitrate_lower));
	my_hv_store(info, "bitrate_window", newSViv(vi->bitrate_window));
	my_hv_store(info, "length", newSVnv(ov_time_total(vf, -1)));

	my_hv_store(self, "INFO", newRV_noinc((SV*) info));
}

MODULE = Ogg::Vorbis::Decoder PACKAGE = Ogg::Vorbis::Decoder

SV*
open(class, path)
	char *class;
	SV   *path;

	CODE:
	int ret;

	/* Create our new self and a ref to it - all of these are cleaned up
	 * in DESTROY by ov_clear() and safefree() */

	HV *self = newHV();
	SV *obj_ref = newRV_noinc((SV*) self);

	/* holder for the VF itself */
	OggVorbis_File *vf = (OggVorbis_File *) safemalloc(sizeof(OggVorbis_File));

	/* our stash for streams */
	ocvb_datasource *datasource = (ocvb_datasource *) safemalloc(sizeof(ocvb_datasource));
	memset(datasource, 0, sizeof(ocvb_datasource));

	/* check and see if a pathname was passed in, otherwise it might be a
	 * IO::Socket subclass, or even a *FH Glob */
	if (SvOK(path) && (SvTYPE(SvRV(path)) != SVt_PVGV)) {

		if ((datasource->stream = PerlIO_open((char*)SvPV_nolen(path), "r")) == NULL) {
			safefree(vf);
			printf("failed on open: [%d] - [%s]\n", errno, strerror(errno));
			XSRETURN_UNDEF;
		}

		datasource->is_streaming = 0;

	} else if (SvOK(path)) {

		/* Did we get a Glob, or a IO::Socket subclass?
		 *
		 * XXX This should really be a class method so the caller
		 * can tell us if it's streaming or not. But how to do this on
		 * a per object basis without changing open()s arugments. That
		 * may be the easiest/only way. XXX
		 *
		 */

		if (sv_isobject(path) && sv_derived_from(path, "IO::Socket")) {
			datasource->is_streaming = 1;
		} else {
			datasource->is_streaming = 0;
		}

		/* dereference and get the SV* that contains the Magic & FH,
		 * then pull the fd from the PerlIO object */
		datasource->stream = IoIFP(GvIOp(SvRV(path)));

	} else {

		XSRETURN_UNDEF;
	}

	if ((ret = ov_open_callbacks((void*)datasource, vf, NULL, 0, vorbis_callbacks)) < 0) {

		warn("Failed on registering callbacks: [%d]\n", ret);
		printf("failed on open: [%d] - [%s]\n", errno, strerror(errno));
		ov_clear(vf);
		XSRETURN_UNDEF;
        }

	datasource->bytes_streamed = 0;
	datasource->last_bitstream = -1;

	/* initalize bitrate, channels, etc */
	__read_info(self, vf);

	/* Values stored at base level */
	my_hv_store(self, "PATH", newSVsv(path));
	my_hv_store(self, "VFILE", newSViv((IV) vf));
	my_hv_store(self, "BSTREAM", newSViv(0));
	my_hv_store(self, "READCOMMENTS", newSViv(1));

	/* Bless the hashref to create a class object */
	sv_bless(obj_ref, gv_stashpv(class, FALSE));

	RETVAL = obj_ref;

	OUTPUT:
	RETVAL

long
read(obj, buffer, nbytes = 4096, word = 2, sgned = 1)
	SV* obj;
	SV* buffer;
	int nbytes;
	int word;
	int sgned;

	ALIAS:
	sysread = 1

	CODE:
	{
	int bytes = 0;
	int total_bytes_read = 0;
	int read_comments = 0;
	int old_bitstream, cur_bitstream;

	char *readBuffer = alloca(nbytes);

	/* for replay gain */
	/* not yet.. */
	int use_rg = 0;
	float ***pcm = NULL;

	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	if (!vf) XSRETURN_UNDEF;

	if (ix) {
		/* empty */
	}

	/* See http://www.xiph.org/ogg/vorbis/doc/vorbisfile/ov_read.html for
	 * a description of the bitstream parameter. This allows streaming
	 * without a hack like icy-metaint */
	cur_bitstream = (int) SvIV(*(my_hv_fetch(self, "BSTREAM")));
	old_bitstream = cur_bitstream;

	/* When we get a new bitstream, re-read the comment fields */
	read_comments = (int)SvIV(*(my_hv_fetch(self, "READCOMMENTS")));

	/* The nbytes argument to ov_read is only a limit, not a request. So
	 * read until we hit the requested number of bytes */
	while (nbytes > 0) {

		if (use_rg) {

                	bytes = ov_read_float(vf, pcm, nbytes, &cur_bitstream);

                	if (bytes > 0) {
                        	/* bytes = vorbis_process_replaygain(pcm, bytes, channels, readBuffer, rg_scale); */
			}

		} else {

			bytes = ov_read(vf, readBuffer, nbytes, endian, word, sgned, &cur_bitstream);
		}

		if (bytes && read_comments != 0) {
			__read_comments(self, vf);
			read_comments = 0;
		}

		if (bytes == 0) {
			/* eof */
			break;

		} else if (bytes == OV_HOLE || bytes == OV_EBADLINK) {
			/* error in stream, but we don't care, move along */

		} else if (bytes < 0 && errno == EINTR) {
			/* try to re-read, same as above */

		} else if (bytes < 0) {
			/* error */
			break;

		} else {

			total_bytes_read += bytes;
			readBuffer += bytes;
			nbytes  -= bytes;

			/* did we enter a new logical bitstream? */
			if (old_bitstream != cur_bitstream && old_bitstream != -1) {

				__read_info(self, vf);
				read_comments = 1;
				break;
			}
		}
	}

	/* update with our new bitstream */
	sv_setiv(*my_hv_fetch(self, "BSTREAM"), (IV) cur_bitstream);
	sv_setiv(*my_hv_fetch(self, "READCOMMENTS"), (IV)read_comments);

	/* copy the buffer into our passed SV* */
	sv_setpvn(buffer, readBuffer-total_bytes_read, total_bytes_read);

	if (bytes < 0) XSRETURN_UNDEF;

	RETVAL = total_bytes_read;

	}

	OUTPUT:
	RETVAL

void
_read_info (obj)
	SV* obj;

	CODE:
	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	__read_info(self, vf);

void
_read_comments (obj)
	SV* obj;

	CODE:
	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	__read_comments(self, vf);

void
DESTROY (obj)
	SV* obj;

	CODE:
	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	ov_clear(vf);
	safefree(vf);

# For all of the below, see:
# http://www.xiph.org/ogg/vorbis/doc/vorbisfile/fileinfo.html for a
# description of the functions that duplicate the vorbisfile API.

int
raw_seek (obj, pos, whence = 0)
	SV* obj;
	long pos;
	int whence;

	CODE:
	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	/* XXX - handle whence */
	RETVAL = ov_raw_seek(vf, pos);

	OUTPUT:
	RETVAL

int
pcm_seek (obj, pos, page = 0)
	SV* obj;
	ogg_int64_t pos;
	int page;

	CODE:
	{

	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	if (page == 0) {
		RETVAL = ov_pcm_seek(vf, pos);
	} else {
		RETVAL = ov_pcm_seek_page(vf, pos);
	}

	}

	OUTPUT:
	RETVAL

int
time_seek (obj, pos, page = 0)
	SV* obj;
	double pos;
	int page;

	CODE:
	{

	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	if (page == 0) {
		RETVAL = ov_time_seek(vf, pos);
	} else {
		RETVAL = ov_time_seek_page(vf, pos);
	}

	}

	OUTPUT:
	RETVAL

long
bitrate (obj, i = -1)
	SV* obj;
	int i;

	CODE:
	{

	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	RETVAL = ov_bitrate(vf, i);

	}

	OUTPUT:
	RETVAL

long
bitrate_instant (obj)
	SV* obj;

	CODE:
	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	RETVAL = ov_bitrate_instant(vf);

	OUTPUT:
	RETVAL

long
streams (obj)
	SV* obj;

	CODE:
	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	RETVAL = ov_streams(vf);

	OUTPUT:
	RETVAL

long
seekable (obj)
	SV* obj;

	CODE:
	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	RETVAL = ov_seekable(vf);

	OUTPUT:
	RETVAL

long
serialnumber (obj, i = -1)
	SV* obj;
	int i;

	CODE:
	{

	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	RETVAL = ov_serialnumber(vf, i);
	}

	OUTPUT:
	RETVAL

IV
raw_total (obj, i = -1)
	SV* obj;
	int i;

	CODE:
	{

	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	RETVAL = ov_raw_total(vf, i);

	}

	OUTPUT:
	RETVAL

IV
pcm_total (obj, i = -1)
	SV* obj;
	int i;

	CODE:
	{

	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	RETVAL = ov_pcm_total(vf, i);

	}

	OUTPUT:
	RETVAL

double
time_total (obj, i = -1)
	SV* obj;
	int i;

	CODE:
	{

	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	RETVAL = ov_time_total(vf, i);

	}

	OUTPUT:
	RETVAL

IV
raw_tell (obj)
	SV* obj;

	CODE:
	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	RETVAL = ov_raw_tell(vf);

	OUTPUT:
	RETVAL

IV
pcm_tell (obj)
	SV* obj;

	CODE:
	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	RETVAL = ov_pcm_tell(vf);

	OUTPUT:
	RETVAL

double
time_tell (obj)
	SV* obj;

	CODE:
	HV *self = (HV *) SvRV(obj);
	OggVorbis_File *vf = (OggVorbis_File *) SvIV(*(my_hv_fetch(self, "VFILE")));

	RETVAL = ov_time_tell(vf);

	OUTPUT:
	RETVAL