File: Decoder.xs

package info (click to toggle)
libaudio-flac-decoder-perl 0.3+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 356 kB
  • ctags: 47
  • sloc: ansic: 479; perl: 58; sh: 51; makefile: 2
file content (731 lines) | stat: -rw-r--r-- 20,576 bytes parent folder | download
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
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
/* $Id: Decoder.xs,v 1.2 2004/07/18 03:40:10 daniel Exp $ */

/* This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Chunks of this code have been borrowed and influenced 
 * by flac/decode.c and the flac XMMS plugin.
 *
 */

#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>

#include <FLAC/all.h>
#include "include/common.h"
#include "include/dither.h"
#include "include/replaygain_synthesis.h"

#ifdef _MSC_VER
# define alloca            _alloca
#endif

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

/* Create some generic (and shorter) names for these types. */
typedef FLAC__StreamDecoder decoder_t;
typedef FLAC__StreamDecoderReadStatus read_status_t;

#define FLACdecoder_new()                       FLAC__stream_decoder_new()
#define FLACdecoder_init(a,b,c,d,e,f,g,h,i,j)   FLAC__stream_decoder_init_stream(a,b,c,d,e,f,g,h,i,j)
#define FLACdecoder_process_metadata(x)         FLAC__stream_decoder_process_until_end_of_metadata(x)
#define FLACdecoder_process_single(x)           FLAC__stream_decoder_process_single(x)
#define FLACdecoder_finish(x)                   FLAC__stream_decoder_finish(x)
#define FLACdecoder_delete(x)                   FLAC__stream_decoder_delete(x)
#define FLACdecoder_set_read_callback(x, y)     FLAC__stream_decoder_set_read_callback(x, y)
#define FLACdecoder_set_write_callback(x, y)    FLAC__stream_decoder_set_write_callback(x, y)
#define FLACdecoder_set_metadata_callback(x, y) FLAC__stream_decoder_set_metadata_callback(x, y)
#define FLACdecoder_set_error_callback(x, y)    FLAC__stream_decoder_set_error_callback(x, y)
#define FLACdecoder_set_client_data(x, y)       FLAC__stream_decoder_set_client_data(x, y)
#define FLACdecoder_set_seek_callback(x, y)     FLAC__stream_decoder_set_seek_callback(x, y)
#define FLACdecoder_set_tell_callback(x, y)     FLAC__stream_decoder_set_tell_callback(x, y)
#define FLACdecoder_set_length_callback(x, y)   FLAC__stream_decoder_set_length_callback(x, y)
#define FLACdecoder_set_eof_callback(x, y)      FLAC__stream_decoder_set_eof_callback(x, y)
#define FLACdecoder_seek_absolute(x, y)         FLAC__stream_decoder_seek_absolute(x, y)

#define FLACdecoder_get_state(x)                FLAC__stream_decoder_get_state(x)
#define FLACdecoder_get_channels(x)             FLAC__stream_decoder_get_channels(x)
#define FLACdecoder_get_blocksize(x)		FLAC__stream_decoder_get_blocksize(x)
#define FLACdecoder_get_sample_rate(x)		FLAC__stream_decoder_get_sample_rate(x)
#define FLACdecoder_get_bits_per_sample(x)	FLAC__stream_decoder_get_bits_per_sample(x)
#define FLACdecoder_get_decode_position(x, y)   FLAC__stream_decoder_get_decode_position(x, y)

#define SAMPLES_PER_WRITE 512

typedef struct {
	/* i.e. specification string started with + or - */
	FLAC__bool is_relative;
	FLAC__bool value_is_samples;
	union {
		double seconds;
		FLAC__int64 samples;
	} value;
} SkipUntilSpecification;

/* Allow multiple instances of the decoder object. Stuff each filehandle into (void*)stream */
typedef struct {
	int abort_flag;
	int bytes_streamed;
	int is_streaming;
	FLAC__uint64 stream_length;
	void *buffer;
	PerlIO *stream;
	decoder_t *decoder;
	FLAC__bool has_replaygain;

	/* (24/8) for max bytes per sample */
	FLAC__byte sample_buffer[SAMPLES_PER_WRITE * FLAC__MAX_SUPPORTED_CHANNELS * (24/8)];
	FLAC__int32 reservoir[FLAC__MAX_BLOCK_SIZE * 2 * FLAC__MAX_SUPPORTED_CHANNELS];
	FLAC__uint64 decode_position_last;
	FLAC__uint64 decode_position_frame_last;
	FLAC__uint64 decode_position_frame;
	unsigned buffer_size;
	FLAC__int64 total_samples;
        unsigned bps;
        unsigned channels;
        FLAC__int64 sample_rate;
        FLAC__int64 length_in_msec;
	unsigned wide_samples_in_reservoir;
	SkipUntilSpecification skip_specification;
	SkipUntilSpecification until_specification;

} flac_datasource;

/* start all the callbacks here. */
static void meta_callback(
	const decoder_t *decoder,
	const FLAC__StreamMetadata *metadata, void *client_data) {

	flac_datasource *datasource = (flac_datasource *)client_data;

	if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {

		FLAC__uint64 skip, until;

		/* flac__utils_canonicalize_skip_until_specification(decoder_session->skip_specification, decoder_session->sample_rate);
		FLAC__ASSERT(datasource->skip_specification->value.samples >= 0); */
		skip = (FLAC__uint64)datasource->skip_specification.value.samples;

                /* remember, metadata->data.stream_info.total_samples can be 0, meaning 'unknown' */
                if (metadata->data.stream_info.total_samples > 0 && skip >= metadata->data.stream_info.total_samples) {
                        warn("ERROR trying to skip more samples than in stream\n");
                        datasource->abort_flag = true;
                        return;

                } else if (metadata->data.stream_info.total_samples == 0 && skip > 0) {
                        warn("ERROR, can't skip when FLAC metadata has total sample count of 0\n");
                        datasource->abort_flag = true;
                        return;
                }

		datasource->bps		    = metadata->data.stream_info.bits_per_sample;
		datasource->channels        = metadata->data.stream_info.channels;
		datasource->sample_rate     = metadata->data.stream_info.sample_rate;
		datasource->total_samples   = metadata->data.stream_info.total_samples - skip;

		datasource->length_in_msec  = datasource->total_samples * 10 / (datasource->sample_rate / 100);

		/* if (!canonicalize_until_specification(
			datasource->until_specification, datasource->inbasefilename,
			datasource>n->sample_rate, skip, metadata->data.stream_info.total_samples)) {
                        datasource->abort_flag = true;
                        return;
                } */

                FLAC__ASSERT(datasource->until_specification.value.samples >= 0);
                until = (FLAC__uint64)datasource->until_specification.value.samples;

                if (until > 0) {
                        datasource->total_samples -= (metadata->data.stream_info.total_samples - until);
		}

                if (datasource->bps != 8 && datasource->bps != 16 && datasource->bps != 24) {
                        warn("ERROR: bits per sample is not 8/16/24\n");
                        datasource->abort_flag = true;
                        return;
                }
	}
}

static void error_callback(
	const decoder_t *decoder,
	FLAC__StreamDecoderErrorStatus status, void *client_data) {

	/* flac_datasource *datasource = (flac_datasource *)client_data; */

	warn("FLAC decoder error_callback: %s\n", status);
}

static FLAC__StreamDecoderSeekStatus seek_callback(
	const decoder_t *decoder,
	FLAC__uint64 absolute_byte_offset, void *client_data) {

	flac_datasource *datasource = (flac_datasource *)client_data;

	/* can't seek on a socket */
	if (datasource->is_streaming) {
		return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
	}

	if (PerlIO_seek(datasource->stream, absolute_byte_offset, SEEK_SET) >= 0) {
		return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
	}

	return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
}

static FLAC__StreamDecoderTellStatus tell_callback(
	const decoder_t *decoder,
	FLAC__uint64 *absolute_byte_offset, void *client_data) {

	flac_datasource *datasource = (flac_datasource *)client_data;
	FLAC__uint64 pos = -1;
	
	/* can't tell on a socket */
	if (datasource->is_streaming) {
		return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
	}

	pos = PerlIO_tell(datasource->stream);

	if (pos < 0) {
		return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
	}

	*absolute_byte_offset = pos;
	return FLAC__STREAM_DECODER_TELL_STATUS_OK;
}

static FLAC__StreamDecoderLengthStatus length_callback(
	const decoder_t *decoder,
	FLAC__uint64 *stream_length, void *client_data) {

	flac_datasource *datasource = (flac_datasource *)client_data;

	/* can't find the total length of a socket */
	if (datasource->is_streaming) {
		return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
	}

	*stream_length = datasource->stream_length;
	return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
}

static FLAC__bool eof_callback(
	const decoder_t *decoder, void *client_data) {

	flac_datasource *datasource = (flac_datasource *)client_data;
	FLAC__uint64 pos = 0;

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

	pos = PerlIO_tell(datasource->stream);

	if (pos >= 0 && pos >= datasource->stream_length) {
		/* printf("stream length: %d pos: %d\n", datasource->stream_length, pos); */
		return true;
	}

	return false;
}

static read_status_t read_callback(
	const decoder_t *decoder,
	FLAC__byte buffer[], size_t *bytes, void *client_data) {

	flac_datasource *datasource = (flac_datasource *)client_data;

	*bytes = PerlIO_read(datasource->stream, buffer, *bytes);

	datasource->buffer_size = *bytes;

	if (*bytes == 0)
		return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
	if (*bytes < 0)
		return FLAC__STREAM_DECODER_READ_STATUS_ABORT;

	return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}

static FLAC__StreamDecoderWriteStatus write_callback(
	const decoder_t *decoder,
	const FLAC__Frame *frame, const FLAC__int32 * const buffer[],
	void *client_data) {

	flac_datasource *datasource = (flac_datasource *)client_data;

	const unsigned channels     = frame->header.channels;
	const unsigned wide_samples = frame->header.blocksize;
	unsigned wide_sample, sample, channel;

	if (datasource->abort_flag) {
                return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
	}

	for (sample = datasource->wide_samples_in_reservoir * channels, 
		wide_sample = 0; wide_sample < wide_samples; wide_sample++) {

		for (channel = 0; channel < channels; channel++, sample++) {
			datasource->reservoir[sample] = buffer[channel][wide_sample];
		}
	}

	datasource->wide_samples_in_reservoir += wide_samples;

	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}

MODULE = Audio::FLAC::Decoder PACKAGE = Audio::FLAC::Decoder

PROTOTYPES: DISABLE

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

	CODE:

	/* for setting the stream length */
	FLAC__uint64 pos;

	/* Create our new self and a ref to it - all of these are cleaned up in DESTROY */
	HV *self = newHV();
	SV *obj_ref = newRV_noinc((SV*) self);

	/* our stash for streams */
	flac_datasource *datasource = safemalloc(sizeof(flac_datasource));

	/* holder for the decoder itself */
	datasource->decoder = FLACdecoder_new();

	/* 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) {

			FLACdecoder_finish(datasource->decoder);
			FLACdecoder_delete(datasource->decoder);

			safefree(datasource);

			warn("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 (!datasource->is_streaming) {

		pos = PerlIO_tell(datasource->stream);

		if (PerlIO_seek(datasource->stream, 0, SEEK_END) != -1) {
		
			datasource->stream_length = PerlIO_tell(datasource->stream);

			if (PerlIO_seek(datasource->stream, pos, SEEK_SET) == -1) {

				FLACdecoder_finish(datasource->decoder);
				FLACdecoder_delete(datasource->decoder);

				safefree(datasource);

				warn("failed on seek to beginning: [%d] - [%s]\n", errno, strerror(errno));
				XSRETURN_UNDEF;
			}
		}
	}

	if (FLACdecoder_init(datasource->decoder,
	                     read_callback,
	                     seek_callback,
	                     tell_callback,
	                     length_callback,
	                     eof_callback,
	                     write_callback,
	                     meta_callback,
	                     error_callback,
	                     datasource) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {

		warn("Failed on initializing the decoder: [%d]\n", FLACdecoder_get_state(datasource->decoder));

		FLACdecoder_finish(datasource->decoder);
		FLACdecoder_delete(datasource->decoder);

		safefree(datasource);

		XSRETURN_UNDEF;
        }
	
	/* skip ahead to the pcm data */
	FLACdecoder_process_metadata(datasource->decoder);

	datasource->bytes_streamed = 0;
	datasource->decode_position_last = 0;
	datasource->decode_position_frame = 0;
	datasource->decode_position_frame_last = 0;

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

	/* Values stored at base level */
	my_hv_store(self, "PATH", newSVsv(path));
	my_hv_store(self, "DATASOURCE", newSViv((IV) datasource));
	/*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
sysread(obj, buffer, nbytes = 1024)
	SV* obj;
	SV* buffer;
	int nbytes;

	CODE:
	{

	int total_bytes_read = 0;
	unsigned blocksize   = 1;
	char *readBuffer     = alloca(nbytes);

	HV *self = (HV *) SvRV(obj);
	flac_datasource *datasource = (flac_datasource *) SvIV(*(my_hv_fetch(self, "DATASOURCE")));

	if (!datasource) XSRETURN_UNDEF;
	if (!datasource->decoder) XSRETURN_UNDEF;

	while (datasource->wide_samples_in_reservoir < SAMPLES_PER_WRITE) {

		unsigned s = datasource->wide_samples_in_reservoir;

		if (FLACdecoder_get_state(datasource->decoder) == FLAC__STREAM_DECODER_END_OF_STREAM ) {
			break;

		} else if (!FLACdecoder_process_single(datasource->decoder)) {

			warn("Audio::FLAC::Decoder - read error while processing frame.\n");
			break;
		}

		blocksize = datasource->wide_samples_in_reservoir - s;
		datasource->decode_position_frame_last = datasource->decode_position_frame;

		if (!FLACdecoder_get_decode_position(datasource->decoder, &datasource->decode_position_frame)) {
			datasource->decode_position_frame = 0;
		}
	}

	while (nbytes > 0) {

		if (datasource->wide_samples_in_reservoir <= 0) {

			break;

		} else {

			const unsigned channels = FLACdecoder_get_channels(datasource->decoder);
			const unsigned bps = FLACdecoder_get_bits_per_sample(datasource->decoder);
			const unsigned n = min(datasource->wide_samples_in_reservoir, SAMPLES_PER_WRITE);
			const unsigned delta = n * channels;
			unsigned i;
			FLAC__byte *out=datasource->sample_buffer;
			/*
			int bytes = (int)pack_pcm_signed_little_endian(
				datasource->sample_buffer, datasource->reservoir, n, channels, bps, bps
			);
			bps==bps (trivially) so all the fancy dithering stuff
			that pack_pcm_signed_little_endian might do will
			never get called.  Let's just do it inline
			*/
			int bytes;
			for(i=0; i<channels*n; i++) {
			    FLAC__int32 sample=(datasource->reservoir)[i];
			    switch(bps) {
			    case 8:
				out[0] = sample ^ 0x80;
				break;
			    case 24:
				out[2] = (FLAC__byte)(sample >> 16);
				/* fall through */
			    case 16:
				out[1] = (FLAC__byte)(sample >> 8);
				out[0] = (FLAC__byte)sample;
			    }
			    out+=bps/8;
			}
			bytes=(out-datasource->sample_buffer);
			for (i = delta; i < datasource->wide_samples_in_reservoir * channels; i++) {
				datasource->reservoir[i-delta] = datasource->reservoir[i];
			}

			datasource->wide_samples_in_reservoir -= n;

			readBuffer        = datasource->sample_buffer;

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

			datasource->decode_position_last = 
				datasource->decode_position_frame - 
				datasource->wide_samples_in_reservoir *
				(datasource->decode_position_frame - datasource->decode_position_frame_last) /
				blocksize;
		}
	}

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

	if (total_bytes_read < 0) XSRETURN_UNDEF;

	RETVAL = total_bytes_read;

	}

	OUTPUT:
	RETVAL

void
DESTROY (obj)
	SV* obj;

	CODE:
	HV *self = (HV *) SvRV(obj);
	flac_datasource *datasource = (flac_datasource *) SvIV(*(my_hv_fetch(self, "DATASOURCE")));

	FLACdecoder_finish(datasource->decoder);
	FLACdecoder_delete(datasource->decoder);

	safefree(datasource);

IV
channels (obj)
	SV* obj;

	CODE:
	HV *self = (HV *) SvRV(obj);
	flac_datasource *datasource = (flac_datasource *) SvIV(*(my_hv_fetch(self, "DATASOURCE")));

	RETVAL = FLACdecoder_get_channels(datasource->decoder);

	OUTPUT:
	RETVAL

IV
bits_per_sample (obj)
	SV* obj;

	CODE:
	HV *self = (HV *) SvRV(obj);
	flac_datasource *datasource = (flac_datasource *) SvIV(*(my_hv_fetch(self, "DATASOURCE")));

	RETVAL = FLACdecoder_get_bits_per_sample(datasource->decoder);

	OUTPUT:
	RETVAL

IV
sample_rate (obj)
	SV* obj;

	CODE:
	HV *self = (HV *) SvRV(obj);
	flac_datasource *datasource = (flac_datasource *) SvIV(*(my_hv_fetch(self, "DATASOURCE")));

	RETVAL = FLACdecoder_get_sample_rate(datasource->decoder);

	OUTPUT:
	RETVAL

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

	CODE:
	{

	HV *self = (HV *) SvRV(obj);
	flac_datasource *datasource = (flac_datasource *) SvIV(*(my_hv_fetch(self, "DATASOURCE")));

	/* can't seek on a socket. */
	if (datasource->is_streaming) {
		XSRETURN_UNDEF;
	}

	if (!FLAC__stream_decoder_reset(datasource->decoder)) {
		XSRETURN_UNDEF;
	}

	RETVAL = PerlIO_seek(datasource->stream, pos, whence);

	}

	OUTPUT:
	RETVAL

FLAC__uint64
raw_tell (obj)
	SV* obj;

	CODE:
	{

	HV *self = (HV *) SvRV(obj);
	flac_datasource *datasource = (flac_datasource *) SvIV(*(my_hv_fetch(self, "DATASOURCE")));

	FLAC__uint64 decode_position = 0;

	/* this is effectively doing a ftell() */
	if (!FLACdecoder_get_decode_position(datasource->decoder, &decode_position)) {
		decode_position = -1;
	}

	RETVAL = decode_position;

	}

	OUTPUT:
	RETVAL

IV
sample_seek (obj, sample)
	SV* obj;
	IV  sample;

	CODE:
	{

	HV *self = (HV *) SvRV(obj);
	flac_datasource *datasource = (flac_datasource *) SvIV(*(my_hv_fetch(self, "DATASOURCE")));

	RETVAL = FLACdecoder_seek_absolute(datasource->decoder, sample);

	}

	OUTPUT:
	RETVAL

FLAC__uint64
time_seek (obj, seconds)
	SV* obj;
	IV  seconds;

	CODE:
	{

	HV *self = (HV *) SvRV(obj);
	flac_datasource *datasource = (flac_datasource *) SvIV(*(my_hv_fetch(self, "DATASOURCE")));

	const double distance  = (double)seconds * 1000.0 / (double)datasource->length_in_msec;
	unsigned target_sample = (unsigned)(distance * (double)datasource->total_samples);

	if (FLACdecoder_seek_absolute(datasource->decoder, (FLAC__uint64)target_sample)) {

		if (!FLACdecoder_get_decode_position(datasource->decoder, &datasource->decode_position_frame)) {
			datasource->decode_position_frame = 0;
		}

		datasource->wide_samples_in_reservoir = 0;
	}

	RETVAL = datasource->decode_position_frame;

	}

	OUTPUT:
	RETVAL

FLAC__uint64
time_tell (obj)
	SV* obj;

	CODE:
	{

	HV *self = (HV *) SvRV(obj);
	flac_datasource *datasource = (flac_datasource *) SvIV(*(my_hv_fetch(self, "DATASOURCE")));

	FLAC__uint64 decode_position = 0;
	/* float time_position = 0; */

	if (!FLACdecoder_get_decode_position(datasource->decoder, &decode_position)) {

		decode_position = -1;

	} else {

		/* time_position = metadata->data.stream_info.total_samples * 10 / 
			(metadata->data.stream_info.sample_rate / 100);
		*/
	}

	RETVAL = decode_position;

	}

	OUTPUT:
	RETVAL