2 ** Copyright (C) 2004, 2005 Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** Copyright (C) 2004 Tobias Gehrig <tgehrig@ira.uka.de>
5 ** This program is free software ; you can redistribute it and/or modify
6 ** it under the terms of the GNU Lesser General Public License as published by
7 ** the Free Software Foundation ; either version 2.1 of the License, or
8 ** (at your option) any later version.
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY ; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU Lesser General Public License for more details.
15 ** You should have received a copy of the GNU Lesser General Public License
16 ** along with this program ; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #ifndef HAVE_FLAC_ALL_H
35 flac_open (SF_PRIVATE *psf)
37 return SFE_UNIMPLEMENTED ;
47 #include "float_cast.h"
49 /*------------------------------------------------------------------------------
50 ** Private static functions.
53 #define ENC_BUFFER_SIZE 4096
56 { PFLAC_PCM_SHORT = 0,
63 { FLAC__SeekableStreamDecoder *fsd ;
64 FLAC__SeekableStreamEncoder *fse ;
67 unsigned pos, len, remain ;
69 const FLAC__int32 * const * wbuffer ;
70 FLAC__int32 * rbuffer [FLAC__MAX_CHANNELS] ;
72 FLAC__int32* encbuffer ;
75 const FLAC__Frame *frame ;
76 FLAC__bool bufferbackup ;
79 static sf_count_t flac_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
80 static int flac_close (SF_PRIVATE *psf) ;
82 static int flac_enc_init (SF_PRIVATE *psf) ;
83 static int flac_read_header (SF_PRIVATE *psf) ;
85 static sf_count_t flac_read_flac2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
86 static sf_count_t flac_read_flac2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
87 static sf_count_t flac_read_flac2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
88 static sf_count_t flac_read_flac2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
90 static sf_count_t flac_write_s2flac (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
91 static sf_count_t flac_write_i2flac (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
92 static sf_count_t flac_write_f2flac (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
93 static sf_count_t flac_write_d2flac (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
95 static void f2flac8_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
96 static void f2flac16_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
97 static void f2flac24_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
98 static void f2flac8_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
99 static void f2flac16_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
100 static void f2flac24_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
101 static void d2flac8_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
102 static void d2flac16_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
103 static void d2flac24_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
104 static void d2flac8_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
105 static void d2flac16_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
106 static void d2flac24_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
108 static int flac_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
110 /* Decoder Callbacks */
111 static FLAC__SeekableStreamDecoderReadStatus sf_flac_read_callback (const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer [], unsigned *bytes, void *client_data) ;
112 static FLAC__SeekableStreamDecoderSeekStatus sf_flac_seek_callback (const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data) ;
113 static FLAC__SeekableStreamDecoderTellStatus sf_flac_tell_callback (const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) ;
114 static FLAC__SeekableStreamDecoderLengthStatus sf_flac_length_callback (const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data) ;
115 static FLAC__bool sf_flac_eof_callback (const FLAC__SeekableStreamDecoder *decoder, void *client_data) ;
116 static FLAC__StreamDecoderWriteStatus sf_flac_write_callback (const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer [], void *client_data) ;
117 static void sf_flac_meta_callback (const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) ;
118 static void sf_flac_error_callback (const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) ;
120 /* Encoder Callbacks */
121 static FLAC__SeekableStreamEncoderSeekStatus sf_flac_enc_seek_callback (const FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data) ;
122 #ifdef HAVE_FLAC_1_1_1
123 static FLAC__SeekableStreamEncoderTellStatus sf_flac_enc_tell_callback (const FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data) ;
125 static FLAC__StreamEncoderWriteStatus sf_flac_enc_write_callback (const FLAC__SeekableStreamEncoder *encoder, const FLAC__byte buffer [], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data) ;
127 static const int legal_sample_rates [] =
128 { 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000
132 s2flac8_array (const short *src, FLAC__int32 *dest, int count)
133 { while (--count >= 0)
134 dest [count] = src [count] >> 8 ;
135 } /* s2flac8_array */
138 s2flac16_array (const short *src, FLAC__int32 *dest, int count)
139 { while (--count >= 0)
140 dest [count] = src [count] ;
141 } /* s2flac16_array */
144 s2flac24_array (const short *src, FLAC__int32 *dest, int count)
145 { while (--count >= 0)
146 dest [count] = src [count] << 8 ;
147 } /* s2flac24_array */
150 i2flac8_array (const int *src, FLAC__int32 *dest, int count)
151 { while (--count >= 0)
152 dest [count] = src [count] >> 24 ;
153 } /* i2flac8_array */
156 i2flac16_array (const int *src, FLAC__int32 *dest, int count)
159 dest [count] = src [count] >> 16 ;
160 } /* i2flac16_array */
163 i2flac24_array (const int *src, FLAC__int32 *dest, int count)
164 { while (--count >= 0)
165 dest [count] = src [count] >> 8 ;
166 } /* i2flac24_array */
169 flac_buffer_copy (SF_PRIVATE *psf)
170 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
171 const FLAC__Frame *frame = pflac->frame ;
172 const FLAC__int32* const *buffer = pflac->wbuffer ;
173 unsigned i = 0, j, offset ;
175 if (pflac->ptr == NULL)
177 ** Not sure why this code is here and not elsewhere.
178 ** Removing it causes valgrind errors.
180 pflac->bufferbackup = SF_TRUE ;
181 for (i = 0 ; i < frame->header.channels ; i++)
182 { if (pflac->rbuffer [i] == NULL)
183 pflac->rbuffer [i] = calloc (frame->header.blocksize, sizeof (FLAC__int32)) ;
184 memcpy (pflac->rbuffer [i], buffer [i], frame->header.blocksize * sizeof (FLAC__int32)) ;
186 pflac->wbuffer = (const FLAC__int32* const*) pflac->rbuffer ;
191 switch (pflac->pcmtype)
192 { case PFLAC_PCM_SHORT :
193 { short *retpcm = ((short*) pflac->ptr) ;
194 int shift = 16 - frame->header.bits_per_sample ;
196 { shift = abs (shift) ;
197 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
198 { offset = pflac->pos + i * frame->header.channels ;
199 for (j = 0 ; j < frame->header.channels ; j++)
200 retpcm [offset + j] = buffer [j][pflac->bufferpos] >> shift ;
201 pflac->remain -= frame->header.channels ;
206 { for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
207 { offset = pflac->pos + i * frame->header.channels ;
209 if (pflac->bufferpos >= frame->header.blocksize)
212 for (j = 0 ; j < frame->header.channels ; j++)
213 retpcm [offset + j] = (buffer [j][pflac->bufferpos]) << shift ;
215 pflac->remain -= frame->header.channels ;
223 { int *retpcm = ((int*) pflac->ptr) ;
224 int shift = 32 - frame->header.bits_per_sample ;
225 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
226 { offset = pflac->pos + i * frame->header.channels ;
228 if (pflac->bufferpos >= frame->header.blocksize)
231 for (j = 0 ; j < frame->header.channels ; j++)
232 retpcm [offset + j] = buffer [j][pflac->bufferpos] << shift ;
233 pflac->remain -= frame->header.channels ;
239 case PFLAC_PCM_FLOAT :
240 { float *retpcm = ((float*) pflac->ptr) ;
241 float norm = (psf->norm_float == SF_TRUE) ? 1.0 / (1 << (frame->header.bits_per_sample - 1)) : 1.0 ;
243 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
244 { offset = pflac->pos + i * frame->header.channels ;
246 if (pflac->bufferpos >= frame->header.blocksize)
249 for (j = 0 ; j < frame->header.channels ; j++)
250 retpcm [offset + j] = buffer [j][pflac->bufferpos] * norm ;
251 pflac->remain -= frame->header.channels ;
257 case PFLAC_PCM_DOUBLE :
258 { double *retpcm = ((double*) pflac->ptr) ;
259 double norm = (psf->norm_double == SF_TRUE) ? 1.0 / (1 << (frame->header.bits_per_sample - 1)) : 1.0 ;
261 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
262 { offset = pflac->pos + i * frame->header.channels ;
264 if (pflac->bufferpos >= frame->header.blocksize)
267 for (j = 0 ; j < frame->header.channels ; j++)
268 retpcm [offset + j] = buffer [j][pflac->bufferpos] * norm ;
269 pflac->remain -= frame->header.channels ;
279 offset = i * frame->header.channels ;
280 pflac->pos += i * frame->header.channels ;
283 } /* flac_buffer_copy */
286 static FLAC__SeekableStreamDecoderReadStatus
287 sf_flac_read_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), FLAC__byte buffer [], unsigned *bytes, void *client_data)
288 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
290 *bytes = psf_fread (buffer, 1, *bytes, psf) ;
291 if (*bytes > 0 && psf->error == 0)
292 return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK ;
294 return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR ;
295 } /* sf_flac_read_callback */
297 static FLAC__SeekableStreamDecoderSeekStatus
298 sf_flac_seek_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), FLAC__uint64 absolute_byte_offset, void *client_data)
299 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
301 psf_fseek (psf, absolute_byte_offset, SEEK_SET) ;
303 return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR ;
305 return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK ;
306 } /* sf_flac_seek_callback */
308 static FLAC__SeekableStreamDecoderTellStatus
309 sf_flac_tell_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), FLAC__uint64 *absolute_byte_offset, void *client_data)
310 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
312 *absolute_byte_offset = psf_ftell (psf) ;
314 return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR ;
316 return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK ;
317 } /* sf_flac_tell_callback */
319 static FLAC__SeekableStreamDecoderLengthStatus
320 sf_flac_length_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), FLAC__uint64 *stream_length, void *client_data)
321 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
323 if ((*stream_length = psf->filelength) == 0)
324 return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR ;
326 return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK ;
327 } /* sf_flac_length_callback */
330 sf_flac_eof_callback (const FLAC__SeekableStreamDecoder *UNUSED (decoder), void *client_data)
331 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
333 if (psf_ftell (psf) == psf->filelength)
337 } /* sf_flac_eof_callback */
339 static FLAC__StreamDecoderWriteStatus
340 sf_flac_write_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), const FLAC__Frame *frame, const FLAC__int32 * const buffer [], void *client_data)
341 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
342 FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
344 pflac->frame = frame ;
345 pflac->bufferpos = 0 ;
347 pflac->bufferbackup = SF_FALSE ;
348 pflac->wbuffer = buffer ;
350 flac_buffer_copy (psf) ;
352 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE ;
353 } /* sf_flac_write_callback */
356 sf_flac_meta_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), const FLAC__StreamMetadata *metadata, void *client_data)
357 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
359 switch (metadata->type)
360 { case FLAC__METADATA_TYPE_STREAMINFO :
361 psf->sf.channels = metadata->data.stream_info.channels ;
362 psf->sf.samplerate = metadata->data.stream_info.sample_rate ;
363 psf->sf.frames = metadata->data.stream_info.total_samples ;
365 switch (metadata->data.stream_info.bits_per_sample)
367 psf->sf.format |= SF_FORMAT_PCM_S8 ;
370 psf->sf.format |= SF_FORMAT_PCM_16 ;
373 psf->sf.format |= SF_FORMAT_PCM_24 ;
376 psf_log_printf (psf, "sf_flac_meta_callback : bits_per_sample %d not yet implemented.\n", metadata->data.stream_info.bits_per_sample) ;
382 psf_log_printf (psf, "sf_flac_meta_callback : metadata-type %d not yet implemented.\n", metadata->type) ;
387 } /* sf_flac_meta_callback */
390 sf_flac_error_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), FLAC__StreamDecoderErrorStatus status, void *client_data)
391 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
393 psf_log_printf (psf, "ERROR : %s\n", FLAC__StreamDecoderErrorStatusString [status]) ;
396 { case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC :
397 psf->error = SFE_FLAC_LOST_SYNC ;
399 case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER :
400 psf->error = SFE_FLAC_BAD_HEADER ;
403 psf->error = SFE_FLAC_UNKOWN_ERROR ;
408 } /* sf_flac_error_callback */
410 static FLAC__SeekableStreamEncoderSeekStatus
411 sf_flac_enc_seek_callback (const FLAC__SeekableStreamEncoder * UNUSED (encoder), FLAC__uint64 absolute_byte_offset, void *client_data)
412 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
414 psf_fseek (psf, absolute_byte_offset, SEEK_SET) ;
416 return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_ERROR ;
418 return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK ;
419 } /* sf_flac_enc_seek_callback */
421 #ifdef HAVE_FLAC_1_1_1
422 static FLAC__SeekableStreamEncoderTellStatus
423 sf_flac_enc_tell_callback (const FLAC__SeekableStreamEncoder *UNUSED (encoder), FLAC__uint64 *absolute_byte_offset, void *client_data)
424 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
426 *absolute_byte_offset = psf_ftell (psf) ;
428 return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_ERROR ;
430 return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_OK ;
431 } /* sf_flac_enc_tell_callback */
434 static FLAC__StreamEncoderWriteStatus
435 sf_flac_enc_write_callback (const FLAC__SeekableStreamEncoder * UNUSED (encoder), const FLAC__byte buffer [], unsigned bytes, unsigned UNUSED (samples), unsigned UNUSED (current_frame), void *client_data)
436 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
438 if (psf_fwrite (buffer, 1, bytes, psf) == bytes && psf->error == 0)
439 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK ;
441 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR ;
442 } /* sf_flac_enc_write_callback */
444 /*------------------------------------------------------------------------------
449 flac_open (SF_PRIVATE *psf)
453 FLAC_PRIVATE* pflac = calloc (1, sizeof (FLAC_PRIVATE)) ;
456 if (psf->mode == SFM_RDWR)
457 return SFE_UNIMPLEMENTED ;
459 if (psf->mode == SFM_READ)
460 { if ((error = flac_read_header (psf)))
464 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
466 if (psf->mode == SFM_WRITE)
467 { if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
468 return SFE_BAD_OPEN_FORMAT ;
470 psf->endian = SF_ENDIAN_BIG ;
472 if ((error = flac_enc_init (psf)))
476 psf->datalength = psf->filelength ;
477 psf->dataoffset = 0 ;
478 psf->blockwidth = 0 ;
481 psf->container_close = flac_close ;
482 psf->seek = flac_seek ;
483 psf->command = flac_command ;
485 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
488 { case SF_FORMAT_PCM_S8 : /* 8-bit FLAC. */
489 case SF_FORMAT_PCM_16 : /* 16-bit FLAC. */
490 case SF_FORMAT_PCM_24 : /* 24-bit FLAC. */
491 error = flac_init (psf) ;
494 default : return SFE_UNIMPLEMENTED ;
500 /*------------------------------------------------------------------------------
504 flac_close (SF_PRIVATE *psf)
505 { FLAC_PRIVATE* pflac ;
508 if ((pflac = (FLAC_PRIVATE*) psf->fdata) == NULL)
511 if (psf->mode == SFM_WRITE)
512 { FLAC__seekable_stream_encoder_finish (pflac->fse) ;
513 FLAC__seekable_stream_encoder_delete (pflac->fse) ;
514 if (pflac->encbuffer)
515 free (pflac->encbuffer) ;
518 if (psf->mode == SFM_READ)
519 { FLAC__seekable_stream_decoder_finish (pflac->fsd) ;
520 FLAC__seekable_stream_decoder_delete (pflac->fsd) ;
523 for (k = 0 ; k < ARRAY_LEN (pflac->rbuffer) ; k++)
524 free (pflac->rbuffer [k]) ;
533 flac_enc_init (SF_PRIVATE *psf)
534 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
539 for (k = 0 ; k < ARRAY_LEN (legal_sample_rates) ; k++)
540 if (psf->sf.samplerate == legal_sample_rates [k])
546 return SFE_FLAC_BAD_SAMPLE_RATE ;
548 psf_fseek (psf, 0, SEEK_SET) ;
549 if ((pflac->fse = FLAC__seekable_stream_encoder_new ()) == NULL)
550 return SFE_FLAC_NEW_DECODER ;
551 FLAC__seekable_stream_encoder_set_write_callback (pflac->fse, sf_flac_enc_write_callback) ;
552 FLAC__seekable_stream_encoder_set_seek_callback (pflac->fse, sf_flac_enc_seek_callback) ;
554 #ifdef HAVE_FLAC_1_1_1
555 FLAC__seekable_stream_encoder_set_tell_callback (pflac->fse, sf_flac_enc_tell_callback) ;
557 FLAC__seekable_stream_encoder_set_client_data (pflac->fse, psf) ;
558 FLAC__seekable_stream_encoder_set_channels (pflac->fse, psf->sf.channels) ;
559 FLAC__seekable_stream_encoder_set_sample_rate (pflac->fse, psf->sf.samplerate) ;
561 switch (psf->sf.format & SF_FORMAT_SUBMASK)
562 { case SF_FORMAT_PCM_S8 :
565 case SF_FORMAT_PCM_16 :
568 case SF_FORMAT_PCM_24 :
577 FLAC__seekable_stream_encoder_set_bits_per_sample (pflac->fse, bps) ;
579 if ((bps = FLAC__seekable_stream_encoder_init (pflac->fse)) != FLAC__SEEKABLE_STREAM_DECODER_OK)
580 { psf_log_printf (psf, "Error : FLAC encoder init returned error : %s\n", FLAC__seekable_stream_encoder_get_resolved_state_string (pflac->fse)) ;
581 return SFE_FLAC_INIT_DECODER ;
585 psf->dataoffset = psf_ftell (psf) ;
586 pflac->encbuffer = calloc (ENC_BUFFER_SIZE, sizeof (FLAC__int32)) ;
589 } /* flac_enc_init */
592 flac_read_header (SF_PRIVATE *psf)
593 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
595 psf_fseek (psf, 0, SEEK_SET) ;
596 if ((pflac->fsd = FLAC__seekable_stream_decoder_new ()) == NULL)
597 return SFE_FLAC_NEW_DECODER ;
599 FLAC__seekable_stream_decoder_set_read_callback (pflac->fsd, sf_flac_read_callback) ;
600 FLAC__seekable_stream_decoder_set_seek_callback (pflac->fsd, sf_flac_seek_callback) ;
601 FLAC__seekable_stream_decoder_set_tell_callback (pflac->fsd, sf_flac_tell_callback) ;
602 FLAC__seekable_stream_decoder_set_length_callback (pflac->fsd, sf_flac_length_callback) ;
603 FLAC__seekable_stream_decoder_set_eof_callback (pflac->fsd, sf_flac_eof_callback) ;
604 FLAC__seekable_stream_decoder_set_write_callback (pflac->fsd, sf_flac_write_callback) ;
605 FLAC__seekable_stream_decoder_set_metadata_callback (pflac->fsd, sf_flac_meta_callback) ;
606 FLAC__seekable_stream_decoder_set_error_callback (pflac->fsd, sf_flac_error_callback) ;
607 FLAC__seekable_stream_decoder_set_client_data (pflac->fsd, psf) ;
609 if (FLAC__seekable_stream_decoder_init (pflac->fsd) != FLAC__SEEKABLE_STREAM_DECODER_OK)
610 return SFE_FLAC_INIT_DECODER ;
612 FLAC__seekable_stream_decoder_process_until_end_of_metadata (pflac->fsd) ;
614 { FLAC__uint64 position ;
615 FLAC__seekable_stream_decoder_get_decode_position (pflac->fsd, &position) ;
616 psf->dataoffset = position ;
620 } /* flac_read_header */
623 flac_command (SF_PRIVATE *psf, int command, void *data, int datasize)
625 /* Avoid compiler warnings. */
628 datasize = datasize ;
638 flac_init (SF_PRIVATE *psf)
640 if (psf->mode == SFM_RDWR)
641 return SFE_BAD_MODE_RW ;
643 if (psf->mode == SFM_READ)
644 { psf->read_short = flac_read_flac2s ;
645 psf->read_int = flac_read_flac2i ;
646 psf->read_float = flac_read_flac2f ;
647 psf->read_double = flac_read_flac2d ;
650 if (psf->mode == SFM_WRITE)
651 { psf->write_short = flac_write_s2flac ;
652 psf->write_int = flac_write_i2flac ;
653 psf->write_float = flac_write_f2flac ;
654 psf->write_double = flac_write_d2flac ;
658 psf->blockwidth = psf->sf.channels ;
660 if (psf->filelength > psf->dataoffset)
661 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset : psf->filelength - psf->dataoffset ;
663 psf->datalength = 0 ;
669 flac_read_loop (SF_PRIVATE *psf, unsigned len)
670 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
674 pflac->remain = len ;
675 if (pflac->frame != NULL && pflac->bufferpos < pflac->frame->header.blocksize)
676 flac_buffer_copy (psf) ;
678 while (pflac->pos < pflac->len)
679 { if (FLAC__seekable_stream_decoder_process_single (pflac->fsd) == 0)
681 if (FLAC__seekable_stream_decoder_get_state (pflac->fsd) != FLAC__SEEKABLE_STREAM_DECODER_OK)
688 } /* flac_read_loop */
691 flac_read_flac2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
692 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
693 sf_count_t total = 0, current ;
696 pflac->pcmtype = PFLAC_PCM_SHORT ;
699 { pflac->ptr = ptr + total ;
700 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
701 current = flac_read_loop (psf, readlen) ;
708 } /* flac_read_flac2s */
711 flac_read_flac2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
712 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
713 sf_count_t total = 0, current ;
716 pflac->pcmtype = PFLAC_PCM_INT ;
719 { pflac->ptr = ptr + total ;
720 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
721 current = flac_read_loop (psf, readlen) ;
728 } /* flac_read_flac2i */
731 flac_read_flac2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
732 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
733 sf_count_t total = 0, current ;
736 pflac->pcmtype = PFLAC_PCM_FLOAT ;
739 { pflac->ptr = ptr + total ;
740 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
741 current = flac_read_loop (psf, readlen) ;
748 } /* flac_read_flac2f */
751 flac_read_flac2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
752 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
753 sf_count_t total = 0, current ;
756 pflac->pcmtype = PFLAC_PCM_DOUBLE ;
759 { pflac->ptr = ptr + total ;
760 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
761 current = flac_read_loop (psf, readlen) ;
768 } /* flac_read_flac2d */
771 flac_write_s2flac (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
772 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
773 void (*convert) (const short *, FLAC__int32 *, int) ;
774 int bufferlen, writecount, thiswrite ;
775 sf_count_t total = 0 ;
776 FLAC__int32* buffer = pflac->encbuffer ;
778 switch (psf->sf.format & SF_FORMAT_SUBMASK)
779 { case SF_FORMAT_PCM_S8 :
780 convert = s2flac8_array ;
782 case SF_FORMAT_PCM_16 :
783 convert = s2flac16_array ;
785 case SF_FORMAT_PCM_24 :
786 convert = s2flac24_array ;
792 bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
793 bufferlen *= psf->sf.channels ;
796 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
797 convert (ptr + total, buffer, writecount) ;
798 if (FLAC__seekable_stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
799 thiswrite = writecount ;
803 if (thiswrite < writecount)
810 } /* flac_write_s2flac */
813 flac_write_i2flac (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
814 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
815 void (*convert) (const int *, FLAC__int32 *, int) ;
816 int bufferlen, writecount, thiswrite ;
817 sf_count_t total = 0 ;
818 FLAC__int32* buffer = pflac->encbuffer ;
820 switch (psf->sf.format & SF_FORMAT_SUBMASK)
821 { case SF_FORMAT_PCM_S8 :
822 convert = i2flac8_array ;
824 case SF_FORMAT_PCM_16 :
825 convert = i2flac16_array ;
827 case SF_FORMAT_PCM_24 :
828 convert = i2flac24_array ;
834 bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
835 bufferlen *= psf->sf.channels ;
838 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
839 convert (ptr + total, buffer, writecount) ;
840 if (FLAC__seekable_stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
841 thiswrite = writecount ;
845 if (thiswrite < writecount)
852 } /* flac_write_i2flac */
855 flac_write_f2flac (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
856 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
857 void (*convert) (const float *, FLAC__int32 *, int, int) ;
858 int bufferlen, writecount, thiswrite ;
859 sf_count_t total = 0 ;
860 FLAC__int32* buffer = pflac->encbuffer ;
862 switch (psf->sf.format & SF_FORMAT_SUBMASK)
863 { case SF_FORMAT_PCM_S8 :
864 convert = (psf->add_clipping) ? f2flac8_clip_array : f2flac8_array ;
866 case SF_FORMAT_PCM_16 :
867 convert = (psf->add_clipping) ? f2flac16_clip_array : f2flac16_array ;
869 case SF_FORMAT_PCM_24 :
870 convert = (psf->add_clipping) ? f2flac24_clip_array : f2flac24_array ;
876 bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
877 bufferlen *= psf->sf.channels ;
880 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
881 convert (ptr + total, buffer, writecount, psf->norm_float) ;
882 if (FLAC__seekable_stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
883 thiswrite = writecount ;
887 if (thiswrite < writecount)
894 } /* flac_write_f2flac */
897 f2flac8_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
898 { float normfact, scaled_value ;
900 normfact = normalize ? (8.0 * 0x10) : 1.0 ;
903 { scaled_value = src [count] * normfact ;
904 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7F))
905 { dest [count] = 0x7F ;
908 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10))
909 { dest [count] = 0x80 ;
912 dest [count] = lrintf (scaled_value) ;
916 } /* f2flac8_clip_array */
919 f2flac16_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
921 float normfact, scaled_value ;
923 normfact = normalize ? (8.0 * 0x1000) : 1.0 ;
925 while (--count >= 0) {
926 scaled_value = src [count] * normfact ;
927 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFF)) {
928 dest [count] = 0x7FFF ;
931 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000)) {
932 dest [count] = 0x8000 ;
935 dest [count] = lrintf (scaled_value) ;
937 } /* f2flac16_clip_array */
940 f2flac24_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
941 { float normfact, scaled_value ;
943 normfact = normalize ? (8.0 * 0x100000) : 1.0 ;
946 { scaled_value = src [count] * normfact ;
947 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFF))
948 { dest [count] = 0x7FFFFF ;
952 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x100000))
953 { dest [count] = 0x800000 ;
956 dest [count] = lrintf (scaled_value) ;
960 } /* f2flac24_clip_array */
963 f2flac8_array (const float *src, FLAC__int32 *dest, int count, int normalize)
964 { float normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
967 dest [count] = lrintf (src [count] * normfact) ;
968 } /* f2flac8_array */
971 f2flac16_array (const float *src, FLAC__int32 *dest, int count, int normalize)
972 { float normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
975 dest [count] = lrintf (src [count] * normfact) ;
976 } /* f2flac16_array */
979 f2flac24_array (const float *src, FLAC__int32 *dest, int count, int normalize)
980 { float normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
983 dest [count] = lrintf (src [count] * normfact) ;
984 } /* f2flac24_array */
987 flac_write_d2flac (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
988 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
989 void (*convert) (const double *, FLAC__int32 *, int, int) ;
990 int bufferlen, writecount, thiswrite ;
991 sf_count_t total = 0 ;
992 FLAC__int32* buffer = pflac->encbuffer ;
994 switch (psf->sf.format & SF_FORMAT_SUBMASK)
995 { case SF_FORMAT_PCM_S8 :
996 convert = (psf->add_clipping) ? d2flac8_clip_array : d2flac8_array ;
998 case SF_FORMAT_PCM_16 :
999 convert = (psf->add_clipping) ? d2flac16_clip_array : d2flac16_array ;
1001 case SF_FORMAT_PCM_24 :
1002 convert = (psf->add_clipping) ? d2flac24_clip_array : d2flac24_array ;
1008 bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
1009 bufferlen *= psf->sf.channels ;
1012 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1013 convert (ptr + total, buffer, writecount, psf->norm_double) ;
1014 if (FLAC__seekable_stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
1015 thiswrite = writecount ;
1018 total += thiswrite ;
1019 if (thiswrite < writecount)
1026 } /* flac_write_d2flac */
1029 d2flac8_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1030 { double normfact, scaled_value ;
1032 normfact = normalize ? (8.0 * 0x10) : 1.0 ;
1034 while (--count >= 0)
1035 { scaled_value = src [count] * normfact ;
1036 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7F))
1037 { dest [count] = 0x7F ;
1040 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10))
1041 { dest [count] = 0x80 ;
1044 dest [count] = lrint (scaled_value) ;
1048 } /* d2flac8_clip_array */
1051 d2flac16_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1052 { double normfact, scaled_value ;
1054 normfact = normalize ? (8.0 * 0x1000) : 1.0 ;
1056 while (--count >= 0)
1057 { scaled_value = src [count] * normfact ;
1058 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFF))
1059 { dest [count] = 0x7FFF ;
1062 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000))
1063 { dest [count] = 0x8000 ;
1066 dest [count] = lrint (scaled_value) ;
1070 } /* d2flac16_clip_array */
1073 d2flac24_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1074 { double normfact, scaled_value ;
1076 normfact = normalize ? (8.0 * 0x100000) : 1.0 ;
1078 while (--count >= 0)
1079 { scaled_value = src [count] * normfact ;
1080 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFF))
1081 { dest [count] = 0x7FFFFF ;
1084 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x100000))
1085 { dest [count] = 0x800000 ;
1088 dest [count] = lrint (scaled_value) ;
1092 } /* d2flac24_clip_array */
1095 d2flac8_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1096 { double normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
1098 while (--count >= 0)
1099 dest [count] = lrint (src [count] * normfact) ;
1100 } /* d2flac8_array */
1103 d2flac16_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1104 { double normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
1106 while (--count >= 0)
1107 dest [count] = lrint (src [count] * normfact) ;
1108 } /* d2flac16_array */
1111 d2flac24_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1112 { double normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
1114 while (--count >= 0)
1115 dest [count] = lrint (src [count] * normfact) ;
1116 } /* d2flac24_array */
1119 flac_seek (SF_PRIVATE *psf, int UNUSED (mode), sf_count_t offset)
1120 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
1125 if (psf->dataoffset < 0)
1126 { psf->error = SFE_BAD_SEEK ;
1127 return ((sf_count_t) -1) ;
1130 pflac->frame = NULL ;
1132 if (psf->mode == SFM_READ)
1133 { FLAC__uint64 position ;
1134 if (FLAC__seekable_stream_decoder_seek_absolute (pflac->fsd, offset))
1135 { FLAC__seekable_stream_decoder_get_decode_position (pflac->fsd, &position) ;
1139 return ((sf_count_t) -1) ;
1142 /* Seeking in write mode not yet supported. */
1143 psf->error = SFE_BAD_SEEK ;
1145 return ((sf_count_t) -1) ;
1151 ** Do not edit or modify anything in this comment block.
1152 ** The arch-tag line is a file identity tag for the GNU Arch
1153 ** revision control system.
1155 ** arch-tag: 46d49617-ebff-42b4-8f66-a0e428147360