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 /* FLAC 1.1.3 has FLAC_API_VERSION_CURRENT == 8 */
50 #if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT < 8
56 /*------------------------------------------------------------------------------
57 ** Private static functions.
60 #define ENC_BUFFER_SIZE 4096
63 { PFLAC_PCM_SHORT = 0,
72 FLAC__SeekableStreamDecoder *fsd ;
73 FLAC__SeekableStreamEncoder *fse ;
75 FLAC__StreamDecoder *fsd ;
76 FLAC__StreamEncoder *fse ;
81 unsigned pos, len, remain ;
83 const FLAC__int32 * const * wbuffer ;
84 FLAC__int32 * rbuffer [FLAC__MAX_CHANNELS] ;
86 FLAC__int32* encbuffer ;
89 const FLAC__Frame *frame ;
90 FLAC__bool bufferbackup ;
93 static sf_count_t flac_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
94 static int flac_close (SF_PRIVATE *psf) ;
96 static int flac_enc_init (SF_PRIVATE *psf) ;
97 static int flac_read_header (SF_PRIVATE *psf) ;
99 static sf_count_t flac_read_flac2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
100 static sf_count_t flac_read_flac2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
101 static sf_count_t flac_read_flac2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
102 static sf_count_t flac_read_flac2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
104 static sf_count_t flac_write_s2flac (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
105 static sf_count_t flac_write_i2flac (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
106 static sf_count_t flac_write_f2flac (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
107 static sf_count_t flac_write_d2flac (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
109 static void f2flac8_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
110 static void f2flac16_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
111 static void f2flac24_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
112 static void f2flac8_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
113 static void f2flac16_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
114 static void f2flac24_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
115 static void d2flac8_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
116 static void d2flac16_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
117 static void d2flac24_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
118 static void d2flac8_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
119 static void d2flac16_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
120 static void d2flac24_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
122 static int flac_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
124 /* Decoder Callbacks */
126 static FLAC__SeekableStreamDecoderReadStatus sf_flac_read_callback (const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer [], unsigned *bytes, void *client_data) ;
127 static FLAC__SeekableStreamDecoderSeekStatus sf_flac_seek_callback (const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data) ;
128 static FLAC__SeekableStreamDecoderTellStatus sf_flac_tell_callback (const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) ;
129 static FLAC__SeekableStreamDecoderLengthStatus sf_flac_length_callback (const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data) ;
130 static FLAC__bool sf_flac_eof_callback (const FLAC__SeekableStreamDecoder *decoder, void *client_data) ;
131 static FLAC__StreamDecoderWriteStatus sf_flac_write_callback (const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer [], void *client_data) ;
132 static void sf_flac_meta_callback (const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) ;
133 static void sf_flac_error_callback (const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) ;
135 static FLAC__StreamDecoderReadStatus sf_flac_read_callback (const FLAC__StreamDecoder *decoder, FLAC__byte buffer [], size_t *bytes, void *client_data) ;
136 static FLAC__StreamDecoderSeekStatus sf_flac_seek_callback (const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data) ;
137 static FLAC__StreamDecoderTellStatus sf_flac_tell_callback (const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) ;
138 static FLAC__StreamDecoderLengthStatus sf_flac_length_callback (const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data) ;
139 static FLAC__bool sf_flac_eof_callback (const FLAC__StreamDecoder *decoder, void *client_data) ;
140 static FLAC__StreamDecoderWriteStatus sf_flac_write_callback (const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer [], void *client_data) ;
141 static void sf_flac_meta_callback (const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) ;
142 static void sf_flac_error_callback (const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) ;
145 /* Encoder Callbacks */
147 static FLAC__SeekableStreamEncoderSeekStatus sf_flac_enc_seek_callback (const FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data) ;
148 #ifdef HAVE_FLAC_1_1_1
149 static FLAC__SeekableStreamEncoderTellStatus sf_flac_enc_tell_callback (const FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data) ;
151 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) ;
153 static FLAC__StreamEncoderSeekStatus sf_flac_enc_seek_callback (const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data) ;
154 static FLAC__StreamEncoderTellStatus sf_flac_enc_tell_callback (const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data) ;
155 static FLAC__StreamEncoderWriteStatus sf_flac_enc_write_callback (const FLAC__StreamEncoder *encoder, const FLAC__byte buffer [], size_t bytes, unsigned samples, unsigned current_frame, void *client_data) ;
158 static const int legal_sample_rates [] =
159 { 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000
163 s2flac8_array (const short *src, FLAC__int32 *dest, int count)
164 { while (--count >= 0)
165 dest [count] = src [count] >> 8 ;
166 } /* s2flac8_array */
169 s2flac16_array (const short *src, FLAC__int32 *dest, int count)
170 { while (--count >= 0)
171 dest [count] = src [count] ;
172 } /* s2flac16_array */
175 s2flac24_array (const short *src, FLAC__int32 *dest, int count)
176 { while (--count >= 0)
177 dest [count] = src [count] << 8 ;
178 } /* s2flac24_array */
181 i2flac8_array (const int *src, FLAC__int32 *dest, int count)
182 { while (--count >= 0)
183 dest [count] = src [count] >> 24 ;
184 } /* i2flac8_array */
187 i2flac16_array (const int *src, FLAC__int32 *dest, int count)
190 dest [count] = src [count] >> 16 ;
191 } /* i2flac16_array */
194 i2flac24_array (const int *src, FLAC__int32 *dest, int count)
195 { while (--count >= 0)
196 dest [count] = src [count] >> 8 ;
197 } /* i2flac24_array */
200 flac_buffer_copy (SF_PRIVATE *psf)
201 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
202 const FLAC__Frame *frame = pflac->frame ;
203 const FLAC__int32* const *buffer = pflac->wbuffer ;
204 unsigned i = 0, j, offset ;
206 if (pflac->ptr == NULL)
208 ** Not sure why this code is here and not elsewhere.
209 ** Removing it causes valgrind errors.
211 pflac->bufferbackup = SF_TRUE ;
212 for (i = 0 ; i < frame->header.channels ; i++)
213 { if (pflac->rbuffer [i] == NULL)
214 pflac->rbuffer [i] = calloc (frame->header.blocksize, sizeof (FLAC__int32)) ;
215 memcpy (pflac->rbuffer [i], buffer [i], frame->header.blocksize * sizeof (FLAC__int32)) ;
217 pflac->wbuffer = (const FLAC__int32* const*) pflac->rbuffer ;
222 switch (pflac->pcmtype)
223 { case PFLAC_PCM_SHORT :
224 { short *retpcm = ((short*) pflac->ptr) ;
225 int shift = 16 - frame->header.bits_per_sample ;
227 { shift = abs (shift) ;
228 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
229 { offset = pflac->pos + i * frame->header.channels ;
230 for (j = 0 ; j < frame->header.channels ; j++)
231 retpcm [offset + j] = buffer [j][pflac->bufferpos] >> shift ;
232 pflac->remain -= frame->header.channels ;
237 { for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
238 { offset = pflac->pos + i * frame->header.channels ;
240 if (pflac->bufferpos >= frame->header.blocksize)
243 for (j = 0 ; j < frame->header.channels ; j++)
244 retpcm [offset + j] = (buffer [j][pflac->bufferpos]) << shift ;
246 pflac->remain -= frame->header.channels ;
254 { int *retpcm = ((int*) pflac->ptr) ;
255 int shift = 32 - frame->header.bits_per_sample ;
256 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
257 { offset = pflac->pos + i * frame->header.channels ;
259 if (pflac->bufferpos >= frame->header.blocksize)
262 for (j = 0 ; j < frame->header.channels ; j++)
263 retpcm [offset + j] = buffer [j][pflac->bufferpos] << shift ;
264 pflac->remain -= frame->header.channels ;
270 case PFLAC_PCM_FLOAT :
271 { float *retpcm = ((float*) pflac->ptr) ;
272 float norm = (psf->norm_float == SF_TRUE) ? 1.0 / (1 << (frame->header.bits_per_sample - 1)) : 1.0 ;
274 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
275 { offset = pflac->pos + i * frame->header.channels ;
277 if (pflac->bufferpos >= frame->header.blocksize)
280 for (j = 0 ; j < frame->header.channels ; j++)
281 retpcm [offset + j] = buffer [j][pflac->bufferpos] * norm ;
282 pflac->remain -= frame->header.channels ;
288 case PFLAC_PCM_DOUBLE :
289 { double *retpcm = ((double*) pflac->ptr) ;
290 double norm = (psf->norm_double == SF_TRUE) ? 1.0 / (1 << (frame->header.bits_per_sample - 1)) : 1.0 ;
292 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
293 { offset = pflac->pos + i * frame->header.channels ;
295 if (pflac->bufferpos >= frame->header.blocksize)
298 for (j = 0 ; j < frame->header.channels ; j++)
299 retpcm [offset + j] = buffer [j][pflac->bufferpos] * norm ;
300 pflac->remain -= frame->header.channels ;
310 offset = i * frame->header.channels ;
311 pflac->pos += i * frame->header.channels ;
314 } /* flac_buffer_copy */
318 static FLAC__SeekableStreamDecoderReadStatus
319 sf_flac_read_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), FLAC__byte buffer [], unsigned *bytes, void *client_data)
321 static FLAC__StreamDecoderReadStatus
322 sf_flac_read_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__byte buffer [], size_t *bytes, void *client_data)
324 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
326 *bytes = psf_fread (buffer, 1, *bytes, psf) ;
327 if (*bytes > 0 && psf->error == 0)
329 return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK ;
331 return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR ;
333 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE ;
335 return FLAC__STREAM_DECODER_READ_STATUS_ABORT ;
337 } /* sf_flac_read_callback */
340 static FLAC__SeekableStreamDecoderSeekStatus
341 sf_flac_seek_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), FLAC__uint64 absolute_byte_offset, void *client_data)
343 static FLAC__StreamDecoderSeekStatus
344 sf_flac_seek_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__uint64 absolute_byte_offset, void *client_data)
346 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
348 psf_fseek (psf, absolute_byte_offset, SEEK_SET) ;
351 return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR ;
353 return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK ;
355 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR ;
357 return FLAC__STREAM_DECODER_SEEK_STATUS_OK ;
359 } /* sf_flac_seek_callback */
362 static FLAC__SeekableStreamDecoderTellStatus
363 sf_flac_tell_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), FLAC__uint64 *absolute_byte_offset, void *client_data)
365 static FLAC__StreamDecoderTellStatus
366 sf_flac_tell_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__uint64 *absolute_byte_offset, void *client_data)
368 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
370 *absolute_byte_offset = psf_ftell (psf) ;
373 return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR ;
375 return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK ;
377 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR ;
379 return FLAC__STREAM_DECODER_TELL_STATUS_OK ;
381 } /* sf_flac_tell_callback */
384 static FLAC__SeekableStreamDecoderLengthStatus
385 sf_flac_length_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), FLAC__uint64 *stream_length, void *client_data)
387 static FLAC__StreamDecoderLengthStatus
388 sf_flac_length_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__uint64 *stream_length, void *client_data)
390 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
392 if ((*stream_length = psf->filelength) == 0)
394 return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR ;
396 return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK ;
398 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR ;
400 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK ;
402 } /* sf_flac_length_callback */
406 sf_flac_eof_callback (const FLAC__SeekableStreamDecoder *UNUSED (decoder), void *client_data)
408 sf_flac_eof_callback (const FLAC__StreamDecoder *UNUSED (decoder), void *client_data)
410 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
412 if (psf_ftell (psf) == psf->filelength)
416 } /* sf_flac_eof_callback */
418 static FLAC__StreamDecoderWriteStatus
420 sf_flac_write_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), const FLAC__Frame *frame, const FLAC__int32 * const buffer [], void *client_data)
422 sf_flac_write_callback (const FLAC__StreamDecoder * UNUSED (decoder), const FLAC__Frame *frame, const FLAC__int32 * const buffer [], void *client_data)
424 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
425 FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
427 pflac->frame = frame ;
428 pflac->bufferpos = 0 ;
430 pflac->bufferbackup = SF_FALSE ;
431 pflac->wbuffer = buffer ;
433 flac_buffer_copy (psf) ;
435 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE ;
436 } /* sf_flac_write_callback */
440 sf_flac_meta_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), const FLAC__StreamMetadata *metadata, void *client_data)
442 sf_flac_meta_callback (const FLAC__StreamDecoder * UNUSED (decoder), const FLAC__StreamMetadata *metadata, void *client_data)
444 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
446 switch (metadata->type)
447 { case FLAC__METADATA_TYPE_STREAMINFO :
448 psf->sf.channels = metadata->data.stream_info.channels ;
449 psf->sf.samplerate = metadata->data.stream_info.sample_rate ;
450 psf->sf.frames = metadata->data.stream_info.total_samples ;
452 switch (metadata->data.stream_info.bits_per_sample)
454 psf->sf.format |= SF_FORMAT_PCM_S8 ;
457 psf->sf.format |= SF_FORMAT_PCM_16 ;
460 psf->sf.format |= SF_FORMAT_PCM_24 ;
463 psf_log_printf (psf, "sf_flac_meta_callback : bits_per_sample %d not yet implemented.\n", metadata->data.stream_info.bits_per_sample) ;
469 psf_log_printf (psf, "sf_flac_meta_callback : metadata-type %d not yet implemented.\n", metadata->type) ;
474 } /* sf_flac_meta_callback */
478 sf_flac_error_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), FLAC__StreamDecoderErrorStatus status, void *client_data)
480 sf_flac_error_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__StreamDecoderErrorStatus status, void *client_data)
482 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
484 psf_log_printf (psf, "ERROR : %s\n", FLAC__StreamDecoderErrorStatusString [status]) ;
487 { case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC :
488 psf->error = SFE_FLAC_LOST_SYNC ;
490 case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER :
491 psf->error = SFE_FLAC_BAD_HEADER ;
494 psf->error = SFE_FLAC_UNKOWN_ERROR ;
499 } /* sf_flac_error_callback */
502 static FLAC__SeekableStreamEncoderSeekStatus
503 sf_flac_enc_seek_callback (const FLAC__SeekableStreamEncoder * UNUSED (encoder), FLAC__uint64 absolute_byte_offset, void *client_data)
505 static FLAC__StreamEncoderSeekStatus
506 sf_flac_enc_seek_callback (const FLAC__StreamEncoder * UNUSED (encoder), FLAC__uint64 absolute_byte_offset, void *client_data)
508 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
510 psf_fseek (psf, absolute_byte_offset, SEEK_SET) ;
513 return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_ERROR ;
515 return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK ;
517 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR ;
519 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK ;
521 } /* sf_flac_enc_seek_callback */
524 #ifdef HAVE_FLAC_1_1_1
525 static FLAC__SeekableStreamEncoderTellStatus
526 sf_flac_enc_tell_callback (const FLAC__SeekableStreamEncoder *UNUSED (encoder), FLAC__uint64 *absolute_byte_offset, void *client_data)
527 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
529 *absolute_byte_offset = psf_ftell (psf) ;
531 return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_ERROR ;
533 return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_OK ;
534 } /* sf_flac_enc_tell_callback */
537 static FLAC__StreamEncoderTellStatus
538 sf_flac_enc_tell_callback (const FLAC__StreamEncoder *UNUSED (encoder), FLAC__uint64 *absolute_byte_offset, void *client_data)
539 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
541 *absolute_byte_offset = psf_ftell (psf) ;
543 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR ;
545 return FLAC__STREAM_ENCODER_TELL_STATUS_OK ;
546 } /* sf_flac_enc_tell_callback */
549 static FLAC__StreamEncoderWriteStatus
551 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)
553 sf_flac_enc_write_callback (const FLAC__StreamEncoder * UNUSED (encoder), const FLAC__byte buffer [], size_t bytes, unsigned UNUSED (samples), unsigned UNUSED (current_frame), void *client_data)
555 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
557 if (psf_fwrite (buffer, 1, bytes, psf) == bytes && psf->error == 0)
558 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK ;
560 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR ;
561 } /* sf_flac_enc_write_callback */
563 /*------------------------------------------------------------------------------
568 flac_open (SF_PRIVATE *psf)
572 FLAC_PRIVATE* pflac = calloc (1, sizeof (FLAC_PRIVATE)) ;
575 if (psf->mode == SFM_RDWR)
576 return SFE_UNIMPLEMENTED ;
578 if (psf->mode == SFM_READ)
579 { if ((error = flac_read_header (psf)))
583 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
585 if (psf->mode == SFM_WRITE)
586 { if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
587 return SFE_BAD_OPEN_FORMAT ;
589 psf->endian = SF_ENDIAN_BIG ;
591 if ((error = flac_enc_init (psf)))
595 psf->datalength = psf->filelength ;
596 psf->dataoffset = 0 ;
597 psf->blockwidth = 0 ;
600 psf->container_close = flac_close ;
601 psf->seek = flac_seek ;
602 psf->command = flac_command ;
604 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
607 { case SF_FORMAT_PCM_S8 : /* 8-bit FLAC. */
608 case SF_FORMAT_PCM_16 : /* 16-bit FLAC. */
609 case SF_FORMAT_PCM_24 : /* 24-bit FLAC. */
610 error = flac_init (psf) ;
613 default : return SFE_UNIMPLEMENTED ;
619 /*------------------------------------------------------------------------------
623 flac_close (SF_PRIVATE *psf)
624 { FLAC_PRIVATE* pflac ;
627 if ((pflac = (FLAC_PRIVATE*) psf->fdata) == NULL)
630 if (psf->mode == SFM_WRITE)
633 FLAC__seekable_stream_encoder_finish (pflac->fse) ;
634 FLAC__seekable_stream_encoder_delete (pflac->fse) ;
636 FLAC__stream_encoder_finish (pflac->fse) ;
637 FLAC__stream_encoder_delete (pflac->fse) ;
639 if (pflac->encbuffer)
640 free (pflac->encbuffer) ;
643 if (psf->mode == SFM_READ)
646 FLAC__seekable_stream_decoder_finish (pflac->fsd) ;
647 FLAC__seekable_stream_decoder_delete (pflac->fsd) ;
649 FLAC__stream_decoder_finish (pflac->fsd) ;
650 FLAC__stream_decoder_delete (pflac->fsd) ;
654 for (k = 0 ; k < ARRAY_LEN (pflac->rbuffer) ; k++)
655 free (pflac->rbuffer [k]) ;
664 flac_enc_init (SF_PRIVATE *psf)
665 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
670 for (k = 0 ; k < ARRAY_LEN (legal_sample_rates) ; k++)
671 if (psf->sf.samplerate == legal_sample_rates [k])
677 return SFE_FLAC_BAD_SAMPLE_RATE ;
679 psf_fseek (psf, 0, SEEK_SET) ;
681 switch (psf->sf.format & SF_FORMAT_SUBMASK)
682 { case SF_FORMAT_PCM_S8 :
685 case SF_FORMAT_PCM_16 :
688 case SF_FORMAT_PCM_24 :
698 if ((pflac->fse = FLAC__seekable_stream_encoder_new ()) == NULL)
699 return SFE_FLAC_NEW_DECODER ;
700 FLAC__seekable_stream_encoder_set_write_callback (pflac->fse, sf_flac_enc_write_callback) ;
701 FLAC__seekable_stream_encoder_set_seek_callback (pflac->fse, sf_flac_enc_seek_callback) ;
703 #ifdef HAVE_FLAC_1_1_1
704 FLAC__seekable_stream_encoder_set_tell_callback (pflac->fse, sf_flac_enc_tell_callback) ;
706 FLAC__seekable_stream_encoder_set_client_data (pflac->fse, psf) ;
707 FLAC__seekable_stream_encoder_set_channels (pflac->fse, psf->sf.channels) ;
708 FLAC__seekable_stream_encoder_set_sample_rate (pflac->fse, psf->sf.samplerate) ;
709 FLAC__seekable_stream_encoder_set_bits_per_sample (pflac->fse, bps) ;
711 if ((bps = FLAC__seekable_stream_encoder_init (pflac->fse)) != FLAC__SEEKABLE_STREAM_DECODER_OK)
712 { psf_log_printf (psf, "Error : FLAC encoder init returned error : %s\n", FLAC__seekable_stream_encoder_get_resolved_state_string (pflac->fse)) ;
713 return SFE_FLAC_INIT_DECODER ;
716 if ((pflac->fse = FLAC__stream_encoder_new ()) == NULL)
717 return SFE_FLAC_NEW_DECODER ;
718 FLAC__stream_encoder_set_channels (pflac->fse, psf->sf.channels) ;
719 FLAC__stream_encoder_set_sample_rate (pflac->fse, psf->sf.samplerate) ;
720 FLAC__stream_encoder_set_bits_per_sample (pflac->fse, bps) ;
722 if ((bps = FLAC__stream_encoder_init_stream (pflac->fse, sf_flac_enc_write_callback, sf_flac_enc_seek_callback, sf_flac_enc_tell_callback, NULL, psf)) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
723 { psf_log_printf (psf, "Error : FLAC encoder init returned error : %s\n", FLAC__StreamEncoderInitStatusString[bps]) ;
724 return SFE_FLAC_INIT_DECODER ;
729 psf->dataoffset = psf_ftell (psf) ;
730 pflac->encbuffer = calloc (ENC_BUFFER_SIZE, sizeof (FLAC__int32)) ;
733 } /* flac_enc_init */
736 flac_read_header (SF_PRIVATE *psf)
737 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
739 psf_fseek (psf, 0, SEEK_SET) ;
741 if ((pflac->fsd = FLAC__seekable_stream_decoder_new ()) == NULL)
742 return SFE_FLAC_NEW_DECODER ;
744 FLAC__seekable_stream_decoder_set_read_callback (pflac->fsd, sf_flac_read_callback) ;
745 FLAC__seekable_stream_decoder_set_seek_callback (pflac->fsd, sf_flac_seek_callback) ;
746 FLAC__seekable_stream_decoder_set_tell_callback (pflac->fsd, sf_flac_tell_callback) ;
747 FLAC__seekable_stream_decoder_set_length_callback (pflac->fsd, sf_flac_length_callback) ;
748 FLAC__seekable_stream_decoder_set_eof_callback (pflac->fsd, sf_flac_eof_callback) ;
749 FLAC__seekable_stream_decoder_set_write_callback (pflac->fsd, sf_flac_write_callback) ;
750 FLAC__seekable_stream_decoder_set_metadata_callback (pflac->fsd, sf_flac_meta_callback) ;
751 FLAC__seekable_stream_decoder_set_error_callback (pflac->fsd, sf_flac_error_callback) ;
752 FLAC__seekable_stream_decoder_set_client_data (pflac->fsd, psf) ;
754 if (FLAC__seekable_stream_decoder_init (pflac->fsd) != FLAC__SEEKABLE_STREAM_DECODER_OK)
755 return SFE_FLAC_INIT_DECODER ;
757 FLAC__seekable_stream_decoder_process_until_end_of_metadata (pflac->fsd) ;
759 if ((pflac->fsd = FLAC__stream_decoder_new ()) == NULL)
760 return SFE_FLAC_NEW_DECODER ;
762 if (FLAC__stream_decoder_init_stream (pflac->fsd, sf_flac_read_callback, sf_flac_seek_callback, sf_flac_tell_callback, sf_flac_length_callback, sf_flac_eof_callback, sf_flac_write_callback, sf_flac_meta_callback, sf_flac_error_callback, psf) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
763 return SFE_FLAC_INIT_DECODER ;
765 FLAC__stream_decoder_process_until_end_of_metadata (pflac->fsd) ;
768 { FLAC__uint64 position ;
770 FLAC__seekable_stream_decoder_get_decode_position (pflac->fsd, &position) ;
772 FLAC__stream_decoder_get_decode_position (pflac->fsd, &position) ;
774 psf->dataoffset = position ;
778 } /* flac_read_header */
781 flac_command (SF_PRIVATE *psf, int command, void *data, int datasize)
783 /* Avoid compiler warnings. */
786 datasize = datasize ;
796 flac_init (SF_PRIVATE *psf)
798 if (psf->mode == SFM_RDWR)
799 return SFE_BAD_MODE_RW ;
801 if (psf->mode == SFM_READ)
802 { psf->read_short = flac_read_flac2s ;
803 psf->read_int = flac_read_flac2i ;
804 psf->read_float = flac_read_flac2f ;
805 psf->read_double = flac_read_flac2d ;
808 if (psf->mode == SFM_WRITE)
809 { psf->write_short = flac_write_s2flac ;
810 psf->write_int = flac_write_i2flac ;
811 psf->write_float = flac_write_f2flac ;
812 psf->write_double = flac_write_d2flac ;
816 psf->blockwidth = psf->sf.channels ;
818 if (psf->filelength > psf->dataoffset)
819 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset : psf->filelength - psf->dataoffset ;
821 psf->datalength = 0 ;
827 flac_read_loop (SF_PRIVATE *psf, unsigned len)
828 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
832 pflac->remain = len ;
833 if (pflac->frame != NULL && pflac->bufferpos < pflac->frame->header.blocksize)
834 flac_buffer_copy (psf) ;
836 while (pflac->pos < pflac->len)
839 if (FLAC__seekable_stream_decoder_process_single (pflac->fsd) == 0)
841 if (FLAC__seekable_stream_decoder_get_state (pflac->fsd) != FLAC__SEEKABLE_STREAM_DECODER_OK)
844 if (FLAC__stream_decoder_process_single (pflac->fsd) == 0)
846 if (FLAC__stream_decoder_get_state (pflac->fsd) >= FLAC__STREAM_DECODER_END_OF_STREAM)
854 } /* flac_read_loop */
857 flac_read_flac2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
858 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
859 sf_count_t total = 0, current ;
862 pflac->pcmtype = PFLAC_PCM_SHORT ;
865 { pflac->ptr = ptr + total ;
866 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
867 current = flac_read_loop (psf, readlen) ;
874 } /* flac_read_flac2s */
877 flac_read_flac2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
878 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
879 sf_count_t total = 0, current ;
882 pflac->pcmtype = PFLAC_PCM_INT ;
885 { pflac->ptr = ptr + total ;
886 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
887 current = flac_read_loop (psf, readlen) ;
894 } /* flac_read_flac2i */
897 flac_read_flac2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
898 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
899 sf_count_t total = 0, current ;
902 pflac->pcmtype = PFLAC_PCM_FLOAT ;
905 { pflac->ptr = ptr + total ;
906 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
907 current = flac_read_loop (psf, readlen) ;
914 } /* flac_read_flac2f */
917 flac_read_flac2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
918 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
919 sf_count_t total = 0, current ;
922 pflac->pcmtype = PFLAC_PCM_DOUBLE ;
925 { pflac->ptr = ptr + total ;
926 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
927 current = flac_read_loop (psf, readlen) ;
934 } /* flac_read_flac2d */
937 flac_write_s2flac (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
938 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
939 void (*convert) (const short *, FLAC__int32 *, int) ;
940 int bufferlen, writecount, thiswrite ;
941 sf_count_t total = 0 ;
942 FLAC__int32* buffer = pflac->encbuffer ;
944 switch (psf->sf.format & SF_FORMAT_SUBMASK)
945 { case SF_FORMAT_PCM_S8 :
946 convert = s2flac8_array ;
948 case SF_FORMAT_PCM_16 :
949 convert = s2flac16_array ;
951 case SF_FORMAT_PCM_24 :
952 convert = s2flac24_array ;
958 bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
959 bufferlen *= psf->sf.channels ;
962 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
963 convert (ptr + total, buffer, writecount) ;
965 if (FLAC__seekable_stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
967 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
969 thiswrite = writecount ;
973 if (thiswrite < writecount)
980 } /* flac_write_s2flac */
983 flac_write_i2flac (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
984 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
985 void (*convert) (const int *, FLAC__int32 *, int) ;
986 int bufferlen, writecount, thiswrite ;
987 sf_count_t total = 0 ;
988 FLAC__int32* buffer = pflac->encbuffer ;
990 switch (psf->sf.format & SF_FORMAT_SUBMASK)
991 { case SF_FORMAT_PCM_S8 :
992 convert = i2flac8_array ;
994 case SF_FORMAT_PCM_16 :
995 convert = i2flac16_array ;
997 case SF_FORMAT_PCM_24 :
998 convert = i2flac24_array ;
1004 bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
1005 bufferlen *= psf->sf.channels ;
1008 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1009 convert (ptr + total, buffer, writecount) ;
1011 if (FLAC__seekable_stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
1013 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
1015 thiswrite = writecount ;
1018 total += thiswrite ;
1019 if (thiswrite < writecount)
1026 } /* flac_write_i2flac */
1029 flac_write_f2flac (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
1030 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
1031 void (*convert) (const float *, FLAC__int32 *, int, int) ;
1032 int bufferlen, writecount, thiswrite ;
1033 sf_count_t total = 0 ;
1034 FLAC__int32* buffer = pflac->encbuffer ;
1036 switch (psf->sf.format & SF_FORMAT_SUBMASK)
1037 { case SF_FORMAT_PCM_S8 :
1038 convert = (psf->add_clipping) ? f2flac8_clip_array : f2flac8_array ;
1040 case SF_FORMAT_PCM_16 :
1041 convert = (psf->add_clipping) ? f2flac16_clip_array : f2flac16_array ;
1043 case SF_FORMAT_PCM_24 :
1044 convert = (psf->add_clipping) ? f2flac24_clip_array : f2flac24_array ;
1050 bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
1051 bufferlen *= psf->sf.channels ;
1054 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1055 convert (ptr + total, buffer, writecount, psf->norm_float) ;
1057 if (FLAC__seekable_stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
1059 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
1061 thiswrite = writecount ;
1064 total += thiswrite ;
1065 if (thiswrite < writecount)
1072 } /* flac_write_f2flac */
1075 f2flac8_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1076 { float normfact, scaled_value ;
1078 normfact = normalize ? (8.0 * 0x10) : 1.0 ;
1080 while (--count >= 0)
1081 { scaled_value = src [count] * normfact ;
1082 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7F))
1083 { dest [count] = 0x7F ;
1086 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10))
1087 { dest [count] = 0x80 ;
1090 dest [count] = lrintf (scaled_value) ;
1094 } /* f2flac8_clip_array */
1097 f2flac16_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1099 float normfact, scaled_value ;
1101 normfact = normalize ? (8.0 * 0x1000) : 1.0 ;
1103 while (--count >= 0) {
1104 scaled_value = src [count] * normfact ;
1105 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFF)) {
1106 dest [count] = 0x7FFF ;
1109 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000)) {
1110 dest [count] = 0x8000 ;
1113 dest [count] = lrintf (scaled_value) ;
1115 } /* f2flac16_clip_array */
1118 f2flac24_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1119 { float normfact, scaled_value ;
1121 normfact = normalize ? (8.0 * 0x100000) : 1.0 ;
1123 while (--count >= 0)
1124 { scaled_value = src [count] * normfact ;
1125 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFF))
1126 { dest [count] = 0x7FFFFF ;
1130 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x100000))
1131 { dest [count] = 0x800000 ;
1134 dest [count] = lrintf (scaled_value) ;
1138 } /* f2flac24_clip_array */
1141 f2flac8_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1142 { float normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
1144 while (--count >= 0)
1145 dest [count] = lrintf (src [count] * normfact) ;
1146 } /* f2flac8_array */
1149 f2flac16_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1150 { float normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
1152 while (--count >= 0)
1153 dest [count] = lrintf (src [count] * normfact) ;
1154 } /* f2flac16_array */
1157 f2flac24_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1158 { float normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
1160 while (--count >= 0)
1161 dest [count] = lrintf (src [count] * normfact) ;
1162 } /* f2flac24_array */
1165 flac_write_d2flac (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
1166 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
1167 void (*convert) (const double *, FLAC__int32 *, int, int) ;
1168 int bufferlen, writecount, thiswrite ;
1169 sf_count_t total = 0 ;
1170 FLAC__int32* buffer = pflac->encbuffer ;
1172 switch (psf->sf.format & SF_FORMAT_SUBMASK)
1173 { case SF_FORMAT_PCM_S8 :
1174 convert = (psf->add_clipping) ? d2flac8_clip_array : d2flac8_array ;
1176 case SF_FORMAT_PCM_16 :
1177 convert = (psf->add_clipping) ? d2flac16_clip_array : d2flac16_array ;
1179 case SF_FORMAT_PCM_24 :
1180 convert = (psf->add_clipping) ? d2flac24_clip_array : d2flac24_array ;
1186 bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
1187 bufferlen *= psf->sf.channels ;
1190 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1191 convert (ptr + total, buffer, writecount, psf->norm_double) ;
1193 if (FLAC__seekable_stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
1195 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
1197 thiswrite = writecount ;
1200 total += thiswrite ;
1201 if (thiswrite < writecount)
1208 } /* flac_write_d2flac */
1211 d2flac8_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1212 { double normfact, scaled_value ;
1214 normfact = normalize ? (8.0 * 0x10) : 1.0 ;
1216 while (--count >= 0)
1217 { scaled_value = src [count] * normfact ;
1218 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7F))
1219 { dest [count] = 0x7F ;
1222 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10))
1223 { dest [count] = 0x80 ;
1226 dest [count] = lrint (scaled_value) ;
1230 } /* d2flac8_clip_array */
1233 d2flac16_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1234 { double normfact, scaled_value ;
1236 normfact = normalize ? (8.0 * 0x1000) : 1.0 ;
1238 while (--count >= 0)
1239 { scaled_value = src [count] * normfact ;
1240 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFF))
1241 { dest [count] = 0x7FFF ;
1244 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000))
1245 { dest [count] = 0x8000 ;
1248 dest [count] = lrint (scaled_value) ;
1252 } /* d2flac16_clip_array */
1255 d2flac24_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1256 { double normfact, scaled_value ;
1258 normfact = normalize ? (8.0 * 0x100000) : 1.0 ;
1260 while (--count >= 0)
1261 { scaled_value = src [count] * normfact ;
1262 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFF))
1263 { dest [count] = 0x7FFFFF ;
1266 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x100000))
1267 { dest [count] = 0x800000 ;
1270 dest [count] = lrint (scaled_value) ;
1274 } /* d2flac24_clip_array */
1277 d2flac8_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1278 { double normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
1280 while (--count >= 0)
1281 dest [count] = lrint (src [count] * normfact) ;
1282 } /* d2flac8_array */
1285 d2flac16_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1286 { double normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
1288 while (--count >= 0)
1289 dest [count] = lrint (src [count] * normfact) ;
1290 } /* d2flac16_array */
1293 d2flac24_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1294 { double normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
1296 while (--count >= 0)
1297 dest [count] = lrint (src [count] * normfact) ;
1298 } /* d2flac24_array */
1301 flac_seek (SF_PRIVATE *psf, int UNUSED (mode), sf_count_t offset)
1302 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
1307 if (psf->dataoffset < 0)
1308 { psf->error = SFE_BAD_SEEK ;
1309 return ((sf_count_t) -1) ;
1312 pflac->frame = NULL ;
1314 if (psf->mode == SFM_READ)
1315 { FLAC__uint64 position ;
1317 if (FLAC__seekable_stream_decoder_seek_absolute (pflac->fsd, offset))
1318 { FLAC__seekable_stream_decoder_get_decode_position (pflac->fsd, &position) ;
1322 if (FLAC__stream_decoder_seek_absolute (pflac->fsd, offset))
1323 { FLAC__stream_decoder_get_decode_position (pflac->fsd, &position) ;
1328 return ((sf_count_t) -1) ;
1331 /* Seeking in write mode not yet supported. */
1332 psf->error = SFE_BAD_SEEK ;
1334 return ((sf_count_t) -1) ;
1340 ** Do not edit or modify anything in this comment block.
1341 ** The arch-tag line is a file identity tag for the GNU Arch
1342 ** revision control system.
1344 ** arch-tag: 46d49617-ebff-42b4-8f66-a0e428147360