pulling trunk
[ardour.git] / libs / libsndfile / src / flac.c
1 /*
2 ** Copyright (C) 2004, 2005 Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** Copyright (C) 2004 Tobias Gehrig <tgehrig@ira.uka.de>
4 **
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.
9 **
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.
14 **
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.
18 */
19
20 #include        "sfconfig.h"
21
22 #include        <stdio.h>
23 #include        <stdlib.h>
24 #include        <fcntl.h>
25 #include        <string.h>
26 #include        <ctype.h>
27
28 #include        "sndfile.h"
29 #include        "common.h"
30
31
32 #ifndef HAVE_FLAC_ALL_H
33
34 int
35 flac_open (SF_PRIVATE *psf)
36 {       if (psf)
37                 return SFE_UNIMPLEMENTED ;
38         return (psf && 0) ;
39 } /* flac_open */
40
41
42 #else
43
44 #include        <FLAC/all.h>
45
46 #include        "sfendian.h"
47 #include        "float_cast.h"
48
49 /*------------------------------------------------------------------------------
50 ** Private static functions.
51 */
52
53 #define ENC_BUFFER_SIZE 4096
54
55 typedef enum
56 {       PFLAC_PCM_SHORT = 0,
57         PFLAC_PCM_INT = 1,
58         PFLAC_PCM_FLOAT = 2,
59         PFLAC_PCM_DOUBLE = 3
60 } PFLAC_PCM ;
61
62 typedef struct
63 {       FLAC__SeekableStreamDecoder *fsd ;
64         FLAC__SeekableStreamEncoder *fse ;
65         PFLAC_PCM pcmtype ;
66         void* ptr ;
67         unsigned pos, len, remain ;
68
69         const FLAC__int32 * const * wbuffer ;
70         FLAC__int32 * rbuffer [FLAC__MAX_CHANNELS] ;
71
72         FLAC__int32* encbuffer ;
73         unsigned bufferpos ;
74
75         const FLAC__Frame *frame ;
76         FLAC__bool bufferbackup ;
77 } FLAC_PRIVATE ;
78
79 static sf_count_t       flac_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
80 static int                      flac_close (SF_PRIVATE *psf) ;
81
82 static int                      flac_enc_init (SF_PRIVATE *psf) ;
83 static int                      flac_read_header (SF_PRIVATE *psf) ;
84
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) ;
89
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) ;
94
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) ;
107
108 static int flac_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
109
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) ;
119
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) ;
124 #endif
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) ;
126
127 static const int legal_sample_rates [] =
128 {       8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000
129 } ;
130
131 static inline void
132 s2flac8_array (const short *src, FLAC__int32 *dest, int count)
133 {       while (--count >= 0)
134                 dest [count] = src [count] >> 8 ;
135 } /* s2flac8_array */
136
137 static inline void
138 s2flac16_array (const short *src, FLAC__int32 *dest, int count)
139 {       while (--count >= 0)
140                 dest [count] = src [count] ;
141 } /* s2flac16_array */
142
143 static inline void
144 s2flac24_array (const short *src, FLAC__int32 *dest, int count)
145 {       while (--count >= 0)
146                 dest [count] = src [count] << 8 ;
147 } /* s2flac24_array */
148
149 static inline void
150 i2flac8_array (const int *src, FLAC__int32 *dest, int count)
151 {       while (--count >= 0)
152                 dest [count] = src [count] >> 24 ;
153 } /* i2flac8_array */
154
155 static inline void
156 i2flac16_array (const int *src, FLAC__int32 *dest, int count)
157 {
158   while (--count >= 0)
159     dest [count] = src [count] >> 16 ;
160 } /* i2flac16_array */
161
162 static inline void
163 i2flac24_array (const int *src, FLAC__int32 *dest, int count)
164 {       while (--count >= 0)
165                 dest [count] = src [count] >> 8 ;
166 } /* i2flac24_array */
167
168 static sf_count_t
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 ;
174
175         if (pflac->ptr == NULL)
176         {       /*
177                 **      Not sure why this code is here and not elsewhere.
178                 **      Removing it causes valgrind errors.
179                 */
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)) ;
185                         } ;
186                 pflac->wbuffer = (const FLAC__int32* const*) pflac->rbuffer ;
187
188                 return 0 ;
189                 } ;
190
191         switch (pflac->pcmtype)
192         {       case PFLAC_PCM_SHORT :
193                         {       short *retpcm = ((short*) pflac->ptr) ;
194                                 int shift = 16 - frame->header.bits_per_sample ;
195                                 if (shift < 0)
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 ;
202                                                 pflac->bufferpos++ ;
203                                                 }
204                                         }
205                                 else
206                                 {       for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
207                                         {       offset = pflac->pos + i * frame->header.channels ;
208
209                                                 if (pflac->bufferpos >= frame->header.blocksize)
210                                                         break ;
211
212                                                 for (j = 0 ; j < frame->header.channels ; j++)
213                                                         retpcm [offset + j] = (buffer [j][pflac->bufferpos]) << shift ;
214
215                                                 pflac->remain -= frame->header.channels ;
216                                                 pflac->bufferpos++ ;
217                                                 } ;
218                                         } ;
219                                 } ;
220                         break ;
221
222                 case PFLAC_PCM_INT :
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 ;
227
228                                         if (pflac->bufferpos >= frame->header.blocksize)
229                                                 break ;
230
231                                         for (j = 0 ; j < frame->header.channels ; j++)
232                                                 retpcm [offset + j] = buffer [j][pflac->bufferpos] << shift ;
233                                         pflac->remain -= frame->header.channels ;
234                                         pflac->bufferpos++ ;
235                                         } ;
236                                 } ;
237                         break ;
238
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 ;
242
243                                 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
244                                 {       offset = pflac->pos + i * frame->header.channels ;
245
246                                         if (pflac->bufferpos >= frame->header.blocksize)
247                                                 break ;
248
249                                         for (j = 0 ; j < frame->header.channels ; j++)
250                                                 retpcm [offset + j] = buffer [j][pflac->bufferpos] * norm ;
251                                         pflac->remain -= frame->header.channels ;
252                                         pflac->bufferpos++ ;
253                                         } ;
254                                 } ;
255                         break ;
256
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 ;
260
261                                 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
262                                 {       offset = pflac->pos + i * frame->header.channels ;
263
264                                         if (pflac->bufferpos >= frame->header.blocksize)
265                                                 break ;
266
267                                         for (j = 0 ; j < frame->header.channels ; j++)
268                                                 retpcm [offset + j] = buffer [j][pflac->bufferpos] * norm ;
269                                         pflac->remain -= frame->header.channels ;
270                                         pflac->bufferpos++ ;
271                                         } ;
272                                 } ;
273                         break ;
274
275                 default :
276                         return 0 ;
277                 } ;
278
279         offset = i * frame->header.channels ;
280         pflac->pos += i * frame->header.channels ;
281
282         return offset ;
283 } /* flac_buffer_copy */
284
285
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 ;
289
290         *bytes = psf_fread (buffer, 1, *bytes, psf) ;
291         if (*bytes > 0 && psf->error == 0)
292                 return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK ;
293
294     return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR ;
295 } /* sf_flac_read_callback */
296
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 ;
300
301         psf_fseek (psf, absolute_byte_offset, SEEK_SET) ;
302         if (psf->error)
303                 return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR ;
304
305         return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK ;
306 } /* sf_flac_seek_callback */
307
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 ;
311
312         *absolute_byte_offset = psf_ftell (psf) ;
313         if (psf->error)
314                 return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR ;
315
316         return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK ;
317 } /* sf_flac_tell_callback */
318
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 ;
322
323         if ((*stream_length = psf->filelength) == 0)
324                 return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR ;
325
326         return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK ;
327 } /* sf_flac_length_callback */
328
329 static FLAC__bool
330 sf_flac_eof_callback (const FLAC__SeekableStreamDecoder *UNUSED (decoder), void *client_data)
331 {       SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
332
333         if (psf_ftell (psf) == psf->filelength)
334                 return SF_TRUE ;
335
336     return SF_FALSE ;
337 } /* sf_flac_eof_callback */
338
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 ;
343
344         pflac->frame = frame ;
345         pflac->bufferpos = 0 ;
346
347         pflac->bufferbackup = SF_FALSE ;
348         pflac->wbuffer = buffer ;
349
350         flac_buffer_copy (psf) ;
351
352         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE ;
353 } /* sf_flac_write_callback */
354
355 static void
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 ;
358
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 ;
364
365                         switch (metadata->data.stream_info.bits_per_sample)
366                         {       case 8 :
367                                         psf->sf.format |= SF_FORMAT_PCM_S8 ;
368                                         break ;
369                                 case 16 :
370                                         psf->sf.format |= SF_FORMAT_PCM_16 ;
371                                         break ;
372                                 case 24 :
373                                         psf->sf.format |= SF_FORMAT_PCM_24 ;
374                                         break ;
375                                 default :
376                                         psf_log_printf (psf, "sf_flac_meta_callback : bits_per_sample %d not yet implemented.\n", metadata->data.stream_info.bits_per_sample) ;
377                                         break ;
378                                 } ;
379                         break ;
380
381                 default :
382                         psf_log_printf (psf, "sf_flac_meta_callback : metadata-type %d not yet implemented.\n", metadata->type) ;
383                 break ;
384                 } ;
385
386         return ;
387 } /* sf_flac_meta_callback */
388
389 static void
390 sf_flac_error_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), FLAC__StreamDecoderErrorStatus status, void *client_data)
391 {       SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
392
393         psf_log_printf (psf, "ERROR : %s\n", FLAC__StreamDecoderErrorStatusString [status]) ;
394
395         switch (status)
396         {       case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC :
397                         psf->error = SFE_FLAC_LOST_SYNC ;
398                         break ;
399                 case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER :
400                         psf->error = SFE_FLAC_BAD_HEADER ;
401                         break ;
402                 default :
403                         psf->error = SFE_FLAC_UNKOWN_ERROR ;
404                         break ;
405                 } ;
406
407         return ;
408 } /* sf_flac_error_callback */
409
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 ;
413
414         psf_fseek (psf, absolute_byte_offset, SEEK_SET) ;
415         if (psf->error)
416                 return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_ERROR ;
417
418     return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK ;
419 } /* sf_flac_enc_seek_callback */
420
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 ;
425
426         *absolute_byte_offset = psf_ftell (psf) ;
427         if (psf->error)
428                 return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_ERROR ;
429
430         return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_OK ;
431 } /* sf_flac_enc_tell_callback */
432 #endif
433
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 ;
437
438         if (psf_fwrite (buffer, 1, bytes, psf) == bytes && psf->error == 0)
439                 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK ;
440
441         return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR ;
442 } /* sf_flac_enc_write_callback */
443
444 /*------------------------------------------------------------------------------
445 ** Public function.
446 */
447
448 int
449 flac_open       (SF_PRIVATE *psf)
450 {       int             subformat ;
451         int             error = 0 ;
452
453         FLAC_PRIVATE* pflac = calloc (1, sizeof (FLAC_PRIVATE)) ;
454         psf->fdata = pflac ;
455
456         if (psf->mode == SFM_RDWR)
457                 return SFE_UNIMPLEMENTED ;
458
459         if (psf->mode == SFM_READ)
460         {       if ((error = flac_read_header (psf)))
461                         return error ;
462                 } ;
463
464         subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
465
466         if (psf->mode == SFM_WRITE)
467         {       if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
468                         return  SFE_BAD_OPEN_FORMAT ;
469
470                 psf->endian = SF_ENDIAN_BIG ;
471
472                 if ((error = flac_enc_init (psf)))
473                         return error ;
474                 } ;
475
476         psf->datalength = psf->filelength ;
477         psf->dataoffset = 0 ;
478         psf->blockwidth = 0 ;
479         psf->bytewidth = 1 ;
480
481         psf->container_close = flac_close ;
482         psf->seek = flac_seek ;
483         psf->command = flac_command ;
484
485         psf->blockwidth = psf->bytewidth * psf->sf.channels ;
486
487         switch (subformat)
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) ;
492                         break ;
493
494                 default : return SFE_UNIMPLEMENTED ;
495                 } ;
496
497         return error ;
498 } /* flac_open */
499
500 /*------------------------------------------------------------------------------
501 */
502
503 static int
504 flac_close      (SF_PRIVATE *psf)
505 {       FLAC_PRIVATE* pflac ;
506         int k ;
507
508         if ((pflac = (FLAC_PRIVATE*) psf->fdata) == NULL)
509                 return 0 ;
510
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) ;
516                 } ;
517
518         if (psf->mode == SFM_READ)
519         {       FLAC__seekable_stream_decoder_finish (pflac->fsd) ;
520                 FLAC__seekable_stream_decoder_delete (pflac->fsd) ;
521                 } ;
522
523         for (k = 0 ; k < ARRAY_LEN (pflac->rbuffer) ; k++)
524                 free (pflac->rbuffer [k]) ;
525
526         free (pflac) ;
527         psf->fdata = NULL ;
528
529         return 0 ;
530 } /* flac_close */
531
532 static int
533 flac_enc_init (SF_PRIVATE *psf)
534 {       FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
535         unsigned bps ;
536         int k, found ;
537
538         found = 0 ;
539         for (k = 0 ; k < ARRAY_LEN (legal_sample_rates) ; k++)
540                 if (psf->sf.samplerate == legal_sample_rates [k])
541                 {       found = 1 ;
542                         break ;
543                         } ;
544
545         if (found == 0)
546                 return SFE_FLAC_BAD_SAMPLE_RATE ;
547
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) ;
553
554 #ifdef HAVE_FLAC_1_1_1
555         FLAC__seekable_stream_encoder_set_tell_callback (pflac->fse, sf_flac_enc_tell_callback) ;
556 #endif
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) ;
560
561         switch (psf->sf.format & SF_FORMAT_SUBMASK)
562         {       case SF_FORMAT_PCM_S8 :
563                         bps = 8 ;
564                         break ;
565                 case SF_FORMAT_PCM_16 :
566                         bps = 16 ;
567                         break ;
568                 case SF_FORMAT_PCM_24 :
569                         bps = 24 ;
570                         break ;
571
572                 default :
573                         bps = 0 ;
574                         break ;
575                 } ;
576
577         FLAC__seekable_stream_encoder_set_bits_per_sample (pflac->fse, bps) ;
578
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 ;
582                 } ;
583
584         if (psf->error == 0)
585                 psf->dataoffset = psf_ftell (psf) ;
586         pflac->encbuffer = calloc (ENC_BUFFER_SIZE, sizeof (FLAC__int32)) ;
587
588         return psf->error ;
589 } /* flac_enc_init */
590
591 static int
592 flac_read_header (SF_PRIVATE *psf)
593 {       FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
594
595         psf_fseek (psf, 0, SEEK_SET) ;
596         if ((pflac->fsd = FLAC__seekable_stream_decoder_new ()) == NULL)
597                 return SFE_FLAC_NEW_DECODER ;
598
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) ;
608
609         if (FLAC__seekable_stream_decoder_init (pflac->fsd) != FLAC__SEEKABLE_STREAM_DECODER_OK)
610                 return SFE_FLAC_INIT_DECODER ;
611
612         FLAC__seekable_stream_decoder_process_until_end_of_metadata (pflac->fsd) ;
613         if (psf->error == 0)
614         {       FLAC__uint64 position ;
615                 FLAC__seekable_stream_decoder_get_decode_position (pflac->fsd, &position) ;
616                 psf->dataoffset = position ;
617                 } ;
618
619         return psf->error ;
620 } /* flac_read_header */
621
622 static int
623 flac_command (SF_PRIVATE *psf, int command, void *data, int datasize)
624 {
625         /* Avoid compiler warnings. */
626         psf = psf ;
627         data = data ;
628         datasize = datasize ;
629
630         switch (command)
631         {       default : break ;
632                 } ;
633
634         return 0 ;
635 } /* flac_command */
636
637 int
638 flac_init (SF_PRIVATE *psf)
639 {
640         if (psf->mode == SFM_RDWR)
641                 return SFE_BAD_MODE_RW ;
642
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 ;
648                 } ;
649
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 ;
655                 } ;
656
657         psf->bytewidth = 1 ;
658         psf->blockwidth = psf->sf.channels ;
659
660         if (psf->filelength > psf->dataoffset)
661                 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset : psf->filelength - psf->dataoffset ;
662         else
663                 psf->datalength = 0 ;
664
665         return 0 ;
666 } /* flac_init */
667
668 static unsigned
669 flac_read_loop (SF_PRIVATE *psf, unsigned len)
670 {       FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
671
672         pflac->pos = 0 ;
673         pflac->len = len ;
674         pflac->remain = len ;
675         if (pflac->frame != NULL && pflac->bufferpos < pflac->frame->header.blocksize)
676                 flac_buffer_copy (psf) ;
677
678         while (pflac->pos < pflac->len)
679         {       if (FLAC__seekable_stream_decoder_process_single (pflac->fsd) == 0)
680                         break ;
681                 if (FLAC__seekable_stream_decoder_get_state (pflac->fsd) != FLAC__SEEKABLE_STREAM_DECODER_OK)
682                         break ;
683                 } ;
684
685         pflac->ptr = NULL ;
686
687         return pflac->pos ;
688 } /* flac_read_loop */
689
690 static sf_count_t
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 ;
694         unsigned readlen ;
695
696         pflac->pcmtype = PFLAC_PCM_SHORT ;
697
698         while (total < len)
699         {       pflac->ptr = ptr + total ;
700                 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
701                 current = flac_read_loop (psf, readlen) ;
702                 if (current == 0)
703                         break ;
704                 total += current ;
705                 } ;
706
707         return total ;
708 } /* flac_read_flac2s */
709
710 static sf_count_t
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 ;
714         unsigned readlen ;
715
716         pflac->pcmtype = PFLAC_PCM_INT ;
717
718         while (total < len)
719         {       pflac->ptr = ptr + total ;
720                 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
721                 current = flac_read_loop (psf, readlen) ;
722                 if (current == 0)
723                         break ;
724                 total += current ;
725                 } ;
726
727         return total ;
728 } /* flac_read_flac2i */
729
730 static sf_count_t
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 ;
734         unsigned readlen ;
735
736         pflac->pcmtype = PFLAC_PCM_FLOAT ;
737
738         while (total < len)
739         {       pflac->ptr = ptr + total ;
740                 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
741                 current = flac_read_loop (psf, readlen) ;
742                 if (current == 0)
743                         break ;
744                 total += current ;
745                 } ;
746
747         return total ;
748 } /* flac_read_flac2f */
749
750 static sf_count_t
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 ;
754         unsigned readlen ;
755
756         pflac->pcmtype = PFLAC_PCM_DOUBLE ;
757
758         while (total < len)
759         {       pflac->ptr = ptr + total ;
760                 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
761                 current = flac_read_loop (psf, readlen) ;
762                 if (current == 0)
763                         break ;
764                 total += current ;
765                 } ;
766
767         return total ;
768 } /* flac_read_flac2d */
769
770 static sf_count_t
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 ;
777
778         switch (psf->sf.format & SF_FORMAT_SUBMASK)
779         {       case SF_FORMAT_PCM_S8 :
780                         convert = s2flac8_array ;
781                         break ;
782                 case SF_FORMAT_PCM_16 :
783                         convert = s2flac16_array ;
784                         break ;
785                         case SF_FORMAT_PCM_24 :
786                         convert = s2flac24_array ;
787                         break ;
788                 default :
789                         return -1 ;
790                 } ;
791
792         bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
793         bufferlen *= psf->sf.channels ;
794
795         while (len > 0)
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 ;
800                 else
801                         break ;
802                 total += thiswrite ;
803                 if (thiswrite < writecount)
804                         break ;
805
806                 len -= thiswrite ;
807                 } ;
808
809         return total ;
810 } /* flac_write_s2flac */
811
812 static sf_count_t
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 ;
819
820         switch (psf->sf.format & SF_FORMAT_SUBMASK)
821         {       case SF_FORMAT_PCM_S8 :
822                         convert = i2flac8_array ;
823                         break ;
824                 case SF_FORMAT_PCM_16 :
825                         convert = i2flac16_array ;
826                         break ;
827                 case SF_FORMAT_PCM_24 :
828                         convert = i2flac24_array ;
829                         break ;
830                 default :
831                         return -1 ;
832                 } ;
833
834         bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
835         bufferlen *= psf->sf.channels ;
836
837         while (len > 0)
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 ;
842                 else
843                         break ;
844                 total += thiswrite ;
845                 if (thiswrite < writecount)
846                         break ;
847
848                 len -= thiswrite ;
849                 } ;
850
851         return total ;
852 } /* flac_write_i2flac */
853
854 static sf_count_t
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 ;
861
862         switch (psf->sf.format & SF_FORMAT_SUBMASK)
863         {       case SF_FORMAT_PCM_S8 :
864                         convert = (psf->add_clipping) ? f2flac8_clip_array : f2flac8_array ;
865                         break ;
866                 case SF_FORMAT_PCM_16 :
867                         convert = (psf->add_clipping) ? f2flac16_clip_array : f2flac16_array ;
868                         break ;
869                 case SF_FORMAT_PCM_24 :
870                         convert = (psf->add_clipping) ? f2flac24_clip_array : f2flac24_array ;
871                         break ;
872                 default :
873                         return -1 ;
874                 } ;
875
876         bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
877         bufferlen *= psf->sf.channels ;
878
879         while (len > 0)
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 ;
884                 else
885                         break ;
886                 total += thiswrite ;
887                 if (thiswrite < writecount)
888                         break ;
889
890                 len -= thiswrite ;
891                 } ;
892
893         return total ;
894 } /* flac_write_f2flac */
895
896 static void
897 f2flac8_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
898 {       float normfact, scaled_value ;
899
900         normfact = normalize ? (8.0 * 0x10) : 1.0 ;
901
902         while (--count >= 0)
903         {       scaled_value = src [count] * normfact ;
904                 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7F))
905                 {       dest [count] = 0x7F ;
906                         continue ;
907                         } ;
908                 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10))
909                 {       dest [count] = 0x80 ;
910                         continue ;
911                         } ;
912                 dest [count] = lrintf (scaled_value) ;
913                 } ;
914
915         return ;
916 } /* f2flac8_clip_array */
917
918 static void
919 f2flac16_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
920 {
921   float normfact, scaled_value ;
922
923   normfact = normalize ? (8.0 * 0x1000) : 1.0 ;
924
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 ;
929       continue ;
930     }
931     if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000)) {
932       dest [count] = 0x8000 ;
933       continue ;
934     }
935     dest [count] = lrintf (scaled_value) ;
936   }
937 } /* f2flac16_clip_array */
938
939 static void
940 f2flac24_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
941 {       float normfact, scaled_value ;
942
943         normfact = normalize ? (8.0 * 0x100000) : 1.0 ;
944
945         while (--count >= 0)
946         {       scaled_value = src [count] * normfact ;
947                 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFF))
948                 {       dest [count] = 0x7FFFFF ;
949                         continue ;
950                         } ;
951
952                 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x100000))
953                 {       dest [count] = 0x800000 ;
954                         continue ;
955                         }
956                 dest [count] = lrintf (scaled_value) ;
957                 } ;
958
959         return ;
960 } /* f2flac24_clip_array */
961
962 static void
963 f2flac8_array (const float *src, FLAC__int32 *dest, int count, int normalize)
964 {       float normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
965
966         while (--count >= 0)
967                 dest [count] = lrintf (src [count] * normfact) ;
968 } /* f2flac8_array */
969
970 static void
971 f2flac16_array (const float *src, FLAC__int32 *dest, int count, int normalize)
972 {       float normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
973
974         while (--count >= 0)
975                 dest [count] = lrintf (src [count] * normfact) ;
976 } /* f2flac16_array */
977
978 static void
979 f2flac24_array (const float *src, FLAC__int32 *dest, int count, int normalize)
980 {       float normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
981
982         while (--count >= 0)
983                 dest [count] = lrintf (src [count] * normfact) ;
984 } /* f2flac24_array */
985
986 static sf_count_t
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 ;
993
994         switch (psf->sf.format & SF_FORMAT_SUBMASK)
995         {       case SF_FORMAT_PCM_S8 :
996                         convert = (psf->add_clipping) ? d2flac8_clip_array : d2flac8_array ;
997                         break ;
998                 case SF_FORMAT_PCM_16 :
999                         convert = (psf->add_clipping) ? d2flac16_clip_array : d2flac16_array ;
1000                         break ;
1001                 case SF_FORMAT_PCM_24 :
1002                         convert = (psf->add_clipping) ? d2flac24_clip_array : d2flac24_array ;
1003                         break ;
1004                 default :
1005                         return -1 ;
1006                 } ;
1007
1008         bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
1009         bufferlen *= psf->sf.channels ;
1010
1011         while (len > 0)
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 ;
1016                 else
1017                         break ;
1018                 total += thiswrite ;
1019                 if (thiswrite < writecount)
1020                         break ;
1021
1022                 len -= thiswrite ;
1023                 } ;
1024
1025         return total ;
1026 } /* flac_write_d2flac */
1027
1028 static void
1029 d2flac8_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1030 {       double normfact, scaled_value ;
1031
1032         normfact = normalize ? (8.0 * 0x10) : 1.0 ;
1033
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 ;
1038                         continue ;
1039                         } ;
1040                 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10))
1041                 {       dest [count] = 0x80 ;
1042                         continue ;
1043                         } ;
1044                 dest [count] = lrint (scaled_value) ;
1045                 } ;
1046
1047         return ;
1048 } /* d2flac8_clip_array */
1049
1050 static void
1051 d2flac16_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1052 {       double normfact, scaled_value ;
1053
1054         normfact = normalize ? (8.0 * 0x1000) : 1.0 ;
1055
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 ;
1060                         continue ;
1061                         } ;
1062                 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000))
1063                 {       dest [count] = 0x8000 ;
1064                         continue ;
1065                         } ;
1066                 dest [count] = lrint (scaled_value) ;
1067                 } ;
1068
1069         return ;
1070 } /* d2flac16_clip_array */
1071
1072 static void
1073 d2flac24_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1074 {       double normfact, scaled_value ;
1075
1076         normfact = normalize ? (8.0 * 0x100000) : 1.0 ;
1077
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 ;
1082                         continue ;
1083                         } ;
1084                 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x100000))
1085                 {       dest [count] = 0x800000 ;
1086                         continue ;
1087                         } ;
1088                 dest [count] = lrint (scaled_value) ;
1089                 } ;
1090
1091         return ;
1092 } /* d2flac24_clip_array */
1093
1094 static void
1095 d2flac8_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1096 {       double normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
1097
1098         while (--count >= 0)
1099                 dest [count] = lrint (src [count] * normfact) ;
1100 } /* d2flac8_array */
1101
1102 static void
1103 d2flac16_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1104 {       double normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
1105
1106         while (--count >= 0)
1107                 dest [count] = lrint (src [count] * normfact) ;
1108 } /* d2flac16_array */
1109
1110 static void
1111 d2flac24_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1112 {       double normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
1113
1114         while (--count >= 0)
1115                 dest [count] = lrint (src [count] * normfact) ;
1116 } /* d2flac24_array */
1117
1118 static sf_count_t
1119 flac_seek (SF_PRIVATE *psf, int UNUSED (mode), sf_count_t offset)
1120 {       FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
1121
1122         if (pflac == NULL)
1123                 return 0 ;
1124
1125         if (psf->dataoffset < 0)
1126         {       psf->error = SFE_BAD_SEEK ;
1127                 return ((sf_count_t) -1) ;
1128                 } ;
1129
1130         pflac->frame = NULL ;
1131
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) ;
1136                         return offset ;
1137                         } ;
1138
1139                 return ((sf_count_t) -1) ;
1140                 } ;
1141
1142         /* Seeking in write mode not yet supported. */
1143         psf->error = SFE_BAD_SEEK ;
1144
1145         return ((sf_count_t) -1) ;
1146 } /* flac_seek */
1147
1148 #endif
1149
1150 /*
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.
1154 **
1155 ** arch-tag: 46d49617-ebff-42b4-8f66-a0e428147360
1156 */