Patched libsndfile to work with FLAC >= 1.1.4
[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 /* FLAC 1.1.3 has FLAC_API_VERSION_CURRENT == 8 */
50 #if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT < 8
51 #define LEGACY_FLAC
52 #else
53 #undef LEGACY_FLAC
54 #endif
55
56 /*------------------------------------------------------------------------------
57 ** Private static functions.
58 */
59
60 #define ENC_BUFFER_SIZE 4096
61
62 typedef enum
63 {       PFLAC_PCM_SHORT = 0,
64         PFLAC_PCM_INT = 1,
65         PFLAC_PCM_FLOAT = 2,
66         PFLAC_PCM_DOUBLE = 3
67 } PFLAC_PCM ;
68
69 typedef struct
70 {
71 #ifdef LEGACY_FLAC
72         FLAC__SeekableStreamDecoder *fsd ;
73         FLAC__SeekableStreamEncoder *fse ;
74 #else
75         FLAC__StreamDecoder *fsd ;
76         FLAC__StreamEncoder *fse ;
77 #endif
78         
79         PFLAC_PCM pcmtype ;
80         void* ptr ;
81         unsigned pos, len, remain ;
82
83         const FLAC__int32 * const * wbuffer ;
84         FLAC__int32 * rbuffer [FLAC__MAX_CHANNELS] ;
85
86         FLAC__int32* encbuffer ;
87         unsigned bufferpos ;
88
89         const FLAC__Frame *frame ;
90         FLAC__bool bufferbackup ;
91 } FLAC_PRIVATE ;
92
93 static sf_count_t       flac_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
94 static int                      flac_close (SF_PRIVATE *psf) ;
95
96 static int                      flac_enc_init (SF_PRIVATE *psf) ;
97 static int                      flac_read_header (SF_PRIVATE *psf) ;
98
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) ;
103
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) ;
108
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) ;
121
122 static int flac_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
123
124 /* Decoder Callbacks */
125 #ifdef LEGACY_FLAC
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) ;
134 #else
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) ;
143 #endif
144
145 /* Encoder Callbacks */
146 #ifdef LEGACY_FLAC
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) ;
150 #endif
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) ;
152 #else
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) ;
156 #endif
157
158 static const int legal_sample_rates [] =
159 {       8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000
160 } ;
161
162 static void
163 s2flac8_array (const short *src, FLAC__int32 *dest, int count)
164 {       while (--count >= 0)
165                 dest [count] = src [count] >> 8 ;
166 } /* s2flac8_array */
167
168 static void
169 s2flac16_array (const short *src, FLAC__int32 *dest, int count)
170 {       while (--count >= 0)
171                 dest [count] = src [count] ;
172 } /* s2flac16_array */
173
174 static void
175 s2flac24_array (const short *src, FLAC__int32 *dest, int count)
176 {       while (--count >= 0)
177                 dest [count] = src [count] << 8 ;
178 } /* s2flac24_array */
179
180 static void
181 i2flac8_array (const int *src, FLAC__int32 *dest, int count)
182 {       while (--count >= 0)
183                 dest [count] = src [count] >> 24 ;
184 } /* i2flac8_array */
185
186 static void
187 i2flac16_array (const int *src, FLAC__int32 *dest, int count)
188 {
189   while (--count >= 0)
190     dest [count] = src [count] >> 16 ;
191 } /* i2flac16_array */
192
193 static void
194 i2flac24_array (const int *src, FLAC__int32 *dest, int count)
195 {       while (--count >= 0)
196                 dest [count] = src [count] >> 8 ;
197 } /* i2flac24_array */
198
199 static sf_count_t
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 ;
205
206         if (pflac->ptr == NULL)
207         {       /*
208                 **      Not sure why this code is here and not elsewhere.
209                 **      Removing it causes valgrind errors.
210                 */
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)) ;
216                         } ;
217                 pflac->wbuffer = (const FLAC__int32* const*) pflac->rbuffer ;
218
219                 return 0 ;
220                 } ;
221
222         switch (pflac->pcmtype)
223         {       case PFLAC_PCM_SHORT :
224                         {       short *retpcm = ((short*) pflac->ptr) ;
225                                 int shift = 16 - frame->header.bits_per_sample ;
226                                 if (shift < 0)
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 ;
233                                                 pflac->bufferpos++ ;
234                                                 }
235                                         }
236                                 else
237                                 {       for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
238                                         {       offset = pflac->pos + i * frame->header.channels ;
239
240                                                 if (pflac->bufferpos >= frame->header.blocksize)
241                                                         break ;
242
243                                                 for (j = 0 ; j < frame->header.channels ; j++)
244                                                         retpcm [offset + j] = (buffer [j][pflac->bufferpos]) << shift ;
245
246                                                 pflac->remain -= frame->header.channels ;
247                                                 pflac->bufferpos++ ;
248                                                 } ;
249                                         } ;
250                                 } ;
251                         break ;
252
253                 case PFLAC_PCM_INT :
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 ;
258
259                                         if (pflac->bufferpos >= frame->header.blocksize)
260                                                 break ;
261
262                                         for (j = 0 ; j < frame->header.channels ; j++)
263                                                 retpcm [offset + j] = buffer [j][pflac->bufferpos] << shift ;
264                                         pflac->remain -= frame->header.channels ;
265                                         pflac->bufferpos++ ;
266                                         } ;
267                                 } ;
268                         break ;
269
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 ;
273
274                                 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
275                                 {       offset = pflac->pos + i * frame->header.channels ;
276
277                                         if (pflac->bufferpos >= frame->header.blocksize)
278                                                 break ;
279
280                                         for (j = 0 ; j < frame->header.channels ; j++)
281                                                 retpcm [offset + j] = buffer [j][pflac->bufferpos] * norm ;
282                                         pflac->remain -= frame->header.channels ;
283                                         pflac->bufferpos++ ;
284                                         } ;
285                                 } ;
286                         break ;
287
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 ;
291
292                                 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
293                                 {       offset = pflac->pos + i * frame->header.channels ;
294
295                                         if (pflac->bufferpos >= frame->header.blocksize)
296                                                 break ;
297
298                                         for (j = 0 ; j < frame->header.channels ; j++)
299                                                 retpcm [offset + j] = buffer [j][pflac->bufferpos] * norm ;
300                                         pflac->remain -= frame->header.channels ;
301                                         pflac->bufferpos++ ;
302                                         } ;
303                                 } ;
304                         break ;
305
306                 default :
307                         return 0 ;
308                 } ;
309
310         offset = i * frame->header.channels ;
311         pflac->pos += i * frame->header.channels ;
312
313         return offset ;
314 } /* flac_buffer_copy */
315
316
317 #ifdef LEGACY_FLAC
318 static FLAC__SeekableStreamDecoderReadStatus
319 sf_flac_read_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), FLAC__byte buffer [], unsigned *bytes, void *client_data)
320 #else
321 static FLAC__StreamDecoderReadStatus
322 sf_flac_read_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__byte buffer [], size_t *bytes, void *client_data)
323 #endif
324 {       SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
325
326         *bytes = psf_fread (buffer, 1, *bytes, psf) ;
327         if (*bytes > 0 && psf->error == 0)
328 #ifdef LEGACY_FLAC
329                 return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK ;
330
331     return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR ;
332 #else
333                 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE ;
334
335     return FLAC__STREAM_DECODER_READ_STATUS_ABORT ;
336 #endif
337 } /* sf_flac_read_callback */
338
339 #ifdef LEGACY_FLAC
340 static FLAC__SeekableStreamDecoderSeekStatus
341 sf_flac_seek_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), FLAC__uint64 absolute_byte_offset, void *client_data)
342 #else
343 static FLAC__StreamDecoderSeekStatus
344 sf_flac_seek_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__uint64 absolute_byte_offset, void *client_data)
345 #endif
346 {       SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
347
348         psf_fseek (psf, absolute_byte_offset, SEEK_SET) ;
349         if (psf->error)
350 #ifdef LEGACY_FLAC
351                 return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR ;
352
353         return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK ;
354 #else
355                 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR ;
356
357         return FLAC__STREAM_DECODER_SEEK_STATUS_OK ;
358 #endif
359 } /* sf_flac_seek_callback */
360
361 #ifdef LEGACY_FLAC
362 static FLAC__SeekableStreamDecoderTellStatus
363 sf_flac_tell_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), FLAC__uint64 *absolute_byte_offset, void *client_data)
364 #else
365 static FLAC__StreamDecoderTellStatus
366 sf_flac_tell_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__uint64 *absolute_byte_offset, void *client_data)
367 #endif
368 {       SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
369
370         *absolute_byte_offset = psf_ftell (psf) ;
371         if (psf->error)
372 #ifdef LEGACY_FLAC
373                 return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR ;
374
375         return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK ;
376 #else
377                 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR ;
378
379         return FLAC__STREAM_DECODER_TELL_STATUS_OK ;
380 #endif
381 } /* sf_flac_tell_callback */
382
383 #ifdef LEGACY_FLAC
384 static FLAC__SeekableStreamDecoderLengthStatus
385 sf_flac_length_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), FLAC__uint64 *stream_length, void *client_data)
386 #else
387 static FLAC__StreamDecoderLengthStatus
388 sf_flac_length_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__uint64 *stream_length, void *client_data)
389 #endif
390 {       SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
391
392         if ((*stream_length = psf->filelength) == 0)
393 #ifdef LEGACY_FLAC
394                 return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR ;
395
396         return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK ;
397 #else
398                 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR ;
399
400         return FLAC__STREAM_DECODER_LENGTH_STATUS_OK ;
401 #endif
402 } /* sf_flac_length_callback */
403
404 static FLAC__bool
405 #ifdef LEGACY_FLAC
406 sf_flac_eof_callback (const FLAC__SeekableStreamDecoder *UNUSED (decoder), void *client_data)
407 #else
408 sf_flac_eof_callback (const FLAC__StreamDecoder *UNUSED (decoder), void *client_data)
409 #endif
410 {       SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
411
412         if (psf_ftell (psf) == psf->filelength)
413                 return SF_TRUE ;
414
415     return SF_FALSE ;
416 } /* sf_flac_eof_callback */
417
418 static FLAC__StreamDecoderWriteStatus
419 #ifdef LEGACY_FLAC
420 sf_flac_write_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), const FLAC__Frame *frame, const FLAC__int32 * const buffer [], void *client_data)
421 #else
422 sf_flac_write_callback (const FLAC__StreamDecoder * UNUSED (decoder), const FLAC__Frame *frame, const FLAC__int32 * const buffer [], void *client_data)
423 #endif
424 {       SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
425         FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
426
427         pflac->frame = frame ;
428         pflac->bufferpos = 0 ;
429
430         pflac->bufferbackup = SF_FALSE ;
431         pflac->wbuffer = buffer ;
432
433         flac_buffer_copy (psf) ;
434
435         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE ;
436 } /* sf_flac_write_callback */
437
438 static void
439 #ifdef LEGACY_FLAC
440 sf_flac_meta_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), const FLAC__StreamMetadata *metadata, void *client_data)
441 #else
442 sf_flac_meta_callback (const FLAC__StreamDecoder * UNUSED (decoder), const FLAC__StreamMetadata *metadata, void *client_data)
443 #endif
444 {       SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
445
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 ;
451
452                         switch (metadata->data.stream_info.bits_per_sample)
453                         {       case 8 :
454                                         psf->sf.format |= SF_FORMAT_PCM_S8 ;
455                                         break ;
456                                 case 16 :
457                                         psf->sf.format |= SF_FORMAT_PCM_16 ;
458                                         break ;
459                                 case 24 :
460                                         psf->sf.format |= SF_FORMAT_PCM_24 ;
461                                         break ;
462                                 default :
463                                         psf_log_printf (psf, "sf_flac_meta_callback : bits_per_sample %d not yet implemented.\n", metadata->data.stream_info.bits_per_sample) ;
464                                         break ;
465                                 } ;
466                         break ;
467
468                 default :
469                         psf_log_printf (psf, "sf_flac_meta_callback : metadata-type %d not yet implemented.\n", metadata->type) ;
470                 break ;
471                 } ;
472
473         return ;
474 } /* sf_flac_meta_callback */
475
476 static void
477 #ifdef LEGACY_FLAC
478 sf_flac_error_callback (const FLAC__SeekableStreamDecoder * UNUSED (decoder), FLAC__StreamDecoderErrorStatus status, void *client_data)
479 #else
480 sf_flac_error_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__StreamDecoderErrorStatus status, void *client_data)
481 #endif
482 {       SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
483
484         psf_log_printf (psf, "ERROR : %s\n", FLAC__StreamDecoderErrorStatusString [status]) ;
485
486         switch (status)
487         {       case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC :
488                         psf->error = SFE_FLAC_LOST_SYNC ;
489                         break ;
490                 case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER :
491                         psf->error = SFE_FLAC_BAD_HEADER ;
492                         break ;
493                 default :
494                         psf->error = SFE_FLAC_UNKOWN_ERROR ;
495                         break ;
496                 } ;
497
498         return ;
499 } /* sf_flac_error_callback */
500
501 #ifdef LEGACY_FLAC
502 static FLAC__SeekableStreamEncoderSeekStatus
503 sf_flac_enc_seek_callback (const FLAC__SeekableStreamEncoder * UNUSED (encoder), FLAC__uint64 absolute_byte_offset, void *client_data)
504 #else
505 static FLAC__StreamEncoderSeekStatus
506 sf_flac_enc_seek_callback (const FLAC__StreamEncoder * UNUSED (encoder), FLAC__uint64 absolute_byte_offset, void *client_data)
507 #endif
508 {       SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
509
510         psf_fseek (psf, absolute_byte_offset, SEEK_SET) ;
511         if (psf->error)
512 #ifdef LEGACY_FLAC
513                 return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_ERROR ;
514
515     return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK ;
516 #else
517                 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR ;
518
519     return FLAC__STREAM_ENCODER_SEEK_STATUS_OK ;
520 #endif
521 } /* sf_flac_enc_seek_callback */
522
523 #ifdef LEGACY_FLAC
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 ;
528
529         *absolute_byte_offset = psf_ftell (psf) ;
530         if (psf->error)
531                 return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_ERROR ;
532
533         return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_OK ;
534 } /* sf_flac_enc_tell_callback */
535 #endif
536 #else
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 ;
540
541         *absolute_byte_offset = psf_ftell (psf) ;
542         if (psf->error)
543                 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR ;
544
545         return FLAC__STREAM_ENCODER_TELL_STATUS_OK ;
546 } /* sf_flac_enc_tell_callback */
547 #endif
548
549 static FLAC__StreamEncoderWriteStatus
550 #ifdef LEGACY_FLAC
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)
552 #else
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)
554 #endif
555 {       SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
556
557         if (psf_fwrite (buffer, 1, bytes, psf) == bytes && psf->error == 0)
558                 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK ;
559
560         return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR ;
561 } /* sf_flac_enc_write_callback */
562
563 /*------------------------------------------------------------------------------
564 ** Public function.
565 */
566
567 int
568 flac_open       (SF_PRIVATE *psf)
569 {       int             subformat ;
570         int             error = 0 ;
571
572         FLAC_PRIVATE* pflac = calloc (1, sizeof (FLAC_PRIVATE)) ;
573         psf->fdata = pflac ;
574
575         if (psf->mode == SFM_RDWR)
576                 return SFE_UNIMPLEMENTED ;
577
578         if (psf->mode == SFM_READ)
579         {       if ((error = flac_read_header (psf)))
580                         return error ;
581                 } ;
582
583         subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
584
585         if (psf->mode == SFM_WRITE)
586         {       if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
587                         return  SFE_BAD_OPEN_FORMAT ;
588
589                 psf->endian = SF_ENDIAN_BIG ;
590
591                 if ((error = flac_enc_init (psf)))
592                         return error ;
593                 } ;
594
595         psf->datalength = psf->filelength ;
596         psf->dataoffset = 0 ;
597         psf->blockwidth = 0 ;
598         psf->bytewidth = 1 ;
599
600         psf->container_close = flac_close ;
601         psf->seek = flac_seek ;
602         psf->command = flac_command ;
603
604         psf->blockwidth = psf->bytewidth * psf->sf.channels ;
605
606         switch (subformat)
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) ;
611                         break ;
612
613                 default : return SFE_UNIMPLEMENTED ;
614                 } ;
615
616         return error ;
617 } /* flac_open */
618
619 /*------------------------------------------------------------------------------
620 */
621
622 static int
623 flac_close      (SF_PRIVATE *psf)
624 {       FLAC_PRIVATE* pflac ;
625         int k ;
626
627         if ((pflac = (FLAC_PRIVATE*) psf->fdata) == NULL)
628                 return 0 ;
629
630         if (psf->mode == SFM_WRITE)
631         {
632 #ifdef LEGACY_FLAC
633                 FLAC__seekable_stream_encoder_finish (pflac->fse) ;
634                 FLAC__seekable_stream_encoder_delete (pflac->fse) ;
635 #else
636                 FLAC__stream_encoder_finish (pflac->fse) ;
637                 FLAC__stream_encoder_delete (pflac->fse) ;
638 #endif
639                 if (pflac->encbuffer)
640                         free (pflac->encbuffer) ;
641                 } ;
642
643         if (psf->mode == SFM_READ)
644         {
645 #ifdef LEGACY_FLAC
646                 FLAC__seekable_stream_decoder_finish (pflac->fsd) ;
647                 FLAC__seekable_stream_decoder_delete (pflac->fsd) ;
648 #else
649                 FLAC__stream_decoder_finish (pflac->fsd) ;
650                 FLAC__stream_decoder_delete (pflac->fsd) ;
651 #endif
652                 } ;
653
654         for (k = 0 ; k < ARRAY_LEN (pflac->rbuffer) ; k++)
655                 free (pflac->rbuffer [k]) ;
656
657         free (pflac) ;
658         psf->fdata = NULL ;
659
660         return 0 ;
661 } /* flac_close */
662
663 static int
664 flac_enc_init (SF_PRIVATE *psf)
665 {       FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
666         unsigned bps ;
667         int k, found ;
668
669         found = 0 ;
670         for (k = 0 ; k < ARRAY_LEN (legal_sample_rates) ; k++)
671                 if (psf->sf.samplerate == legal_sample_rates [k])
672                 {       found = 1 ;
673                         break ;
674                         } ;
675
676         if (found == 0)
677                 return SFE_FLAC_BAD_SAMPLE_RATE ;
678
679         psf_fseek (psf, 0, SEEK_SET) ;
680
681         switch (psf->sf.format & SF_FORMAT_SUBMASK)
682         {       case SF_FORMAT_PCM_S8 :
683                         bps = 8 ;
684                         break ;
685                 case SF_FORMAT_PCM_16 :
686                         bps = 16 ;
687                         break ;
688                 case SF_FORMAT_PCM_24 :
689                         bps = 24 ;
690                         break ;
691
692                 default :
693                         bps = 0 ;
694                         break ;
695                 } ;
696
697 #ifdef LEGACY_FLAC
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) ;
702
703 #ifdef HAVE_FLAC_1_1_1
704         FLAC__seekable_stream_encoder_set_tell_callback (pflac->fse, sf_flac_enc_tell_callback) ;
705 #endif
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) ;
710
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 ;
714                 } ;
715 #else
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) ;
721
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 ;
725                 } ;
726 #endif
727
728         if (psf->error == 0)
729                 psf->dataoffset = psf_ftell (psf) ;
730         pflac->encbuffer = calloc (ENC_BUFFER_SIZE, sizeof (FLAC__int32)) ;
731
732         return psf->error ;
733 } /* flac_enc_init */
734
735 static int
736 flac_read_header (SF_PRIVATE *psf)
737 {       FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
738
739         psf_fseek (psf, 0, SEEK_SET) ;
740 #ifdef LEGACY_FLAC
741         if ((pflac->fsd = FLAC__seekable_stream_decoder_new ()) == NULL)
742                 return SFE_FLAC_NEW_DECODER ;
743
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) ;
753
754         if (FLAC__seekable_stream_decoder_init (pflac->fsd) != FLAC__SEEKABLE_STREAM_DECODER_OK)
755                 return SFE_FLAC_INIT_DECODER ;
756
757         FLAC__seekable_stream_decoder_process_until_end_of_metadata (pflac->fsd) ;
758 #else
759         if ((pflac->fsd = FLAC__stream_decoder_new ()) == NULL)
760                 return SFE_FLAC_NEW_DECODER ;
761
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 ;
764
765         FLAC__stream_decoder_process_until_end_of_metadata (pflac->fsd) ;
766 #endif
767         if (psf->error == 0)
768         {       FLAC__uint64 position ;
769 #ifdef LEGACY_FLAC
770                 FLAC__seekable_stream_decoder_get_decode_position (pflac->fsd, &position) ;
771 #else
772                 FLAC__stream_decoder_get_decode_position (pflac->fsd, &position) ;
773 #endif
774                 psf->dataoffset = position ;
775                 } ;
776
777         return psf->error ;
778 } /* flac_read_header */
779
780 static int
781 flac_command (SF_PRIVATE *psf, int command, void *data, int datasize)
782 {
783         /* Avoid compiler warnings. */
784         psf = psf ;
785         data = data ;
786         datasize = datasize ;
787
788         switch (command)
789         {       default : break ;
790                 } ;
791
792         return 0 ;
793 } /* flac_command */
794
795 int
796 flac_init (SF_PRIVATE *psf)
797 {
798         if (psf->mode == SFM_RDWR)
799                 return SFE_BAD_MODE_RW ;
800
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 ;
806                 } ;
807
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 ;
813                 } ;
814
815         psf->bytewidth = 1 ;
816         psf->blockwidth = psf->sf.channels ;
817
818         if (psf->filelength > psf->dataoffset)
819                 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset : psf->filelength - psf->dataoffset ;
820         else
821                 psf->datalength = 0 ;
822
823         return 0 ;
824 } /* flac_init */
825
826 static unsigned
827 flac_read_loop (SF_PRIVATE *psf, unsigned len)
828 {       FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
829
830         pflac->pos = 0 ;
831         pflac->len = len ;
832         pflac->remain = len ;
833         if (pflac->frame != NULL && pflac->bufferpos < pflac->frame->header.blocksize)
834                 flac_buffer_copy (psf) ;
835
836         while (pflac->pos < pflac->len)
837         {
838 #ifdef LEGACY_FLAC
839                 if (FLAC__seekable_stream_decoder_process_single (pflac->fsd) == 0)
840                         break ;
841                 if (FLAC__seekable_stream_decoder_get_state (pflac->fsd) != FLAC__SEEKABLE_STREAM_DECODER_OK)
842                         break ;
843 #else
844                 if (FLAC__stream_decoder_process_single (pflac->fsd) == 0)
845                         break ;
846                 if (FLAC__stream_decoder_get_state (pflac->fsd) >= FLAC__STREAM_DECODER_END_OF_STREAM)
847                         break ;
848 #endif
849                 } ;
850
851         pflac->ptr = NULL ;
852
853         return pflac->pos ;
854 } /* flac_read_loop */
855
856 static sf_count_t
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 ;
860         unsigned readlen ;
861
862         pflac->pcmtype = PFLAC_PCM_SHORT ;
863
864         while (total < len)
865         {       pflac->ptr = ptr + total ;
866                 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
867                 current = flac_read_loop (psf, readlen) ;
868                 if (current == 0)
869                         break ;
870                 total += current ;
871                 } ;
872
873         return total ;
874 } /* flac_read_flac2s */
875
876 static sf_count_t
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 ;
880         unsigned readlen ;
881
882         pflac->pcmtype = PFLAC_PCM_INT ;
883
884         while (total < len)
885         {       pflac->ptr = ptr + total ;
886                 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
887                 current = flac_read_loop (psf, readlen) ;
888                 if (current == 0)
889                         break ;
890                 total += current ;
891                 } ;
892
893         return total ;
894 } /* flac_read_flac2i */
895
896 static sf_count_t
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 ;
900         unsigned readlen ;
901
902         pflac->pcmtype = PFLAC_PCM_FLOAT ;
903
904         while (total < len)
905         {       pflac->ptr = ptr + total ;
906                 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
907                 current = flac_read_loop (psf, readlen) ;
908                 if (current == 0)
909                         break ;
910                 total += current ;
911                 } ;
912
913         return total ;
914 } /* flac_read_flac2f */
915
916 static sf_count_t
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 ;
920         unsigned readlen ;
921
922         pflac->pcmtype = PFLAC_PCM_DOUBLE ;
923
924         while (total < len)
925         {       pflac->ptr = ptr + total ;
926                 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
927                 current = flac_read_loop (psf, readlen) ;
928                 if (current == 0)
929                         break ;
930                 total += current ;
931                 } ;
932
933         return total ;
934 } /* flac_read_flac2d */
935
936 static sf_count_t
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 ;
943
944         switch (psf->sf.format & SF_FORMAT_SUBMASK)
945         {       case SF_FORMAT_PCM_S8 :
946                         convert = s2flac8_array ;
947                         break ;
948                 case SF_FORMAT_PCM_16 :
949                         convert = s2flac16_array ;
950                         break ;
951                         case SF_FORMAT_PCM_24 :
952                         convert = s2flac24_array ;
953                         break ;
954                 default :
955                         return -1 ;
956                 } ;
957
958         bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
959         bufferlen *= psf->sf.channels ;
960
961         while (len > 0)
962         {       writecount = (len >= bufferlen) ? bufferlen : (int) len ;
963                 convert (ptr + total, buffer, writecount) ;
964 #ifdef LEGACY_FLAC
965                 if (FLAC__seekable_stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
966 #else
967                 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
968 #endif
969                         thiswrite = writecount ;
970                 else
971                         break ;
972                 total += thiswrite ;
973                 if (thiswrite < writecount)
974                         break ;
975
976                 len -= thiswrite ;
977                 } ;
978
979         return total ;
980 } /* flac_write_s2flac */
981
982 static sf_count_t
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 ;
989
990         switch (psf->sf.format & SF_FORMAT_SUBMASK)
991         {       case SF_FORMAT_PCM_S8 :
992                         convert = i2flac8_array ;
993                         break ;
994                 case SF_FORMAT_PCM_16 :
995                         convert = i2flac16_array ;
996                         break ;
997                 case SF_FORMAT_PCM_24 :
998                         convert = i2flac24_array ;
999                         break ;
1000                 default :
1001                         return -1 ;
1002                 } ;
1003
1004         bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
1005         bufferlen *= psf->sf.channels ;
1006
1007         while (len > 0)
1008         {       writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1009                 convert (ptr + total, buffer, writecount) ;
1010 #ifdef LEGACY_FLAC
1011                 if (FLAC__seekable_stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
1012 #else
1013                 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
1014 #endif
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_i2flac */
1027
1028 static sf_count_t
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 ;
1035
1036         switch (psf->sf.format & SF_FORMAT_SUBMASK)
1037         {       case SF_FORMAT_PCM_S8 :
1038                         convert = (psf->add_clipping) ? f2flac8_clip_array : f2flac8_array ;
1039                         break ;
1040                 case SF_FORMAT_PCM_16 :
1041                         convert = (psf->add_clipping) ? f2flac16_clip_array : f2flac16_array ;
1042                         break ;
1043                 case SF_FORMAT_PCM_24 :
1044                         convert = (psf->add_clipping) ? f2flac24_clip_array : f2flac24_array ;
1045                         break ;
1046                 default :
1047                         return -1 ;
1048                 } ;
1049
1050         bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
1051         bufferlen *= psf->sf.channels ;
1052
1053         while (len > 0)
1054         {       writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1055                 convert (ptr + total, buffer, writecount, psf->norm_float) ;
1056 #ifdef LEGACY_FLAC
1057                 if (FLAC__seekable_stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
1058 #else
1059                 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
1060 #endif
1061                         thiswrite = writecount ;
1062                 else
1063                         break ;
1064                 total += thiswrite ;
1065                 if (thiswrite < writecount)
1066                         break ;
1067
1068                 len -= thiswrite ;
1069                 } ;
1070
1071         return total ;
1072 } /* flac_write_f2flac */
1073
1074 static void
1075 f2flac8_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1076 {       float normfact, scaled_value ;
1077
1078         normfact = normalize ? (8.0 * 0x10) : 1.0 ;
1079
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 ;
1084                         continue ;
1085                         } ;
1086                 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10))
1087                 {       dest [count] = 0x80 ;
1088                         continue ;
1089                         } ;
1090                 dest [count] = lrintf (scaled_value) ;
1091                 } ;
1092
1093         return ;
1094 } /* f2flac8_clip_array */
1095
1096 static void
1097 f2flac16_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1098 {
1099   float normfact, scaled_value ;
1100
1101   normfact = normalize ? (8.0 * 0x1000) : 1.0 ;
1102
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 ;
1107       continue ;
1108     }
1109     if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000)) {
1110       dest [count] = 0x8000 ;
1111       continue ;
1112     }
1113     dest [count] = lrintf (scaled_value) ;
1114   }
1115 } /* f2flac16_clip_array */
1116
1117 static void
1118 f2flac24_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1119 {       float normfact, scaled_value ;
1120
1121         normfact = normalize ? (8.0 * 0x100000) : 1.0 ;
1122
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 ;
1127                         continue ;
1128                         } ;
1129
1130                 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x100000))
1131                 {       dest [count] = 0x800000 ;
1132                         continue ;
1133                         }
1134                 dest [count] = lrintf (scaled_value) ;
1135                 } ;
1136
1137         return ;
1138 } /* f2flac24_clip_array */
1139
1140 static void
1141 f2flac8_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1142 {       float normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
1143
1144         while (--count >= 0)
1145                 dest [count] = lrintf (src [count] * normfact) ;
1146 } /* f2flac8_array */
1147
1148 static void
1149 f2flac16_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1150 {       float normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
1151
1152         while (--count >= 0)
1153                 dest [count] = lrintf (src [count] * normfact) ;
1154 } /* f2flac16_array */
1155
1156 static void
1157 f2flac24_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1158 {       float normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
1159
1160         while (--count >= 0)
1161                 dest [count] = lrintf (src [count] * normfact) ;
1162 } /* f2flac24_array */
1163
1164 static sf_count_t
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 ;
1171
1172         switch (psf->sf.format & SF_FORMAT_SUBMASK)
1173         {       case SF_FORMAT_PCM_S8 :
1174                         convert = (psf->add_clipping) ? d2flac8_clip_array : d2flac8_array ;
1175                         break ;
1176                 case SF_FORMAT_PCM_16 :
1177                         convert = (psf->add_clipping) ? d2flac16_clip_array : d2flac16_array ;
1178                         break ;
1179                 case SF_FORMAT_PCM_24 :
1180                         convert = (psf->add_clipping) ? d2flac24_clip_array : d2flac24_array ;
1181                         break ;
1182                 default :
1183                         return -1 ;
1184                 } ;
1185
1186         bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
1187         bufferlen *= psf->sf.channels ;
1188
1189         while (len > 0)
1190         {       writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1191                 convert (ptr + total, buffer, writecount, psf->norm_double) ;
1192 #ifdef LEGACY_FLAC
1193                 if (FLAC__seekable_stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
1194 #else
1195                 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
1196 #endif
1197                         thiswrite = writecount ;
1198                 else
1199                         break ;
1200                 total += thiswrite ;
1201                 if (thiswrite < writecount)
1202                         break ;
1203
1204                 len -= thiswrite ;
1205                 } ;
1206
1207         return total ;
1208 } /* flac_write_d2flac */
1209
1210 static void
1211 d2flac8_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1212 {       double normfact, scaled_value ;
1213
1214         normfact = normalize ? (8.0 * 0x10) : 1.0 ;
1215
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 ;
1220                         continue ;
1221                         } ;
1222                 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10))
1223                 {       dest [count] = 0x80 ;
1224                         continue ;
1225                         } ;
1226                 dest [count] = lrint (scaled_value) ;
1227                 } ;
1228
1229         return ;
1230 } /* d2flac8_clip_array */
1231
1232 static void
1233 d2flac16_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1234 {       double normfact, scaled_value ;
1235
1236         normfact = normalize ? (8.0 * 0x1000) : 1.0 ;
1237
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 ;
1242                         continue ;
1243                         } ;
1244                 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000))
1245                 {       dest [count] = 0x8000 ;
1246                         continue ;
1247                         } ;
1248                 dest [count] = lrint (scaled_value) ;
1249                 } ;
1250
1251         return ;
1252 } /* d2flac16_clip_array */
1253
1254 static void
1255 d2flac24_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1256 {       double normfact, scaled_value ;
1257
1258         normfact = normalize ? (8.0 * 0x100000) : 1.0 ;
1259
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 ;
1264                         continue ;
1265                         } ;
1266                 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x100000))
1267                 {       dest [count] = 0x800000 ;
1268                         continue ;
1269                         } ;
1270                 dest [count] = lrint (scaled_value) ;
1271                 } ;
1272
1273         return ;
1274 } /* d2flac24_clip_array */
1275
1276 static void
1277 d2flac8_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1278 {       double normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
1279
1280         while (--count >= 0)
1281                 dest [count] = lrint (src [count] * normfact) ;
1282 } /* d2flac8_array */
1283
1284 static void
1285 d2flac16_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1286 {       double normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
1287
1288         while (--count >= 0)
1289                 dest [count] = lrint (src [count] * normfact) ;
1290 } /* d2flac16_array */
1291
1292 static void
1293 d2flac24_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1294 {       double normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
1295
1296         while (--count >= 0)
1297                 dest [count] = lrint (src [count] * normfact) ;
1298 } /* d2flac24_array */
1299
1300 static sf_count_t
1301 flac_seek (SF_PRIVATE *psf, int UNUSED (mode), sf_count_t offset)
1302 {       FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->fdata ;
1303
1304         if (pflac == NULL)
1305                 return 0 ;
1306
1307         if (psf->dataoffset < 0)
1308         {       psf->error = SFE_BAD_SEEK ;
1309                 return ((sf_count_t) -1) ;
1310                 } ;
1311
1312         pflac->frame = NULL ;
1313
1314         if (psf->mode == SFM_READ)
1315         {       FLAC__uint64 position ;
1316 #ifdef LEGACY_FLAC
1317                 if (FLAC__seekable_stream_decoder_seek_absolute (pflac->fsd, offset))
1318                 {       FLAC__seekable_stream_decoder_get_decode_position (pflac->fsd, &position) ;
1319                         return offset ;
1320                         } ;
1321 #else
1322                 if (FLAC__stream_decoder_seek_absolute (pflac->fsd, offset))
1323                 {       FLAC__stream_decoder_get_decode_position (pflac->fsd, &position) ;
1324                         return offset ;
1325                         } ;
1326 #endif
1327
1328                 return ((sf_count_t) -1) ;
1329                 } ;
1330
1331         /* Seeking in write mode not yet supported. */
1332         psf->error = SFE_BAD_SEEK ;
1333
1334         return ((sf_count_t) -1) ;
1335 } /* flac_seek */
1336
1337 #endif
1338
1339 /*
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.
1343 **
1344 ** arch-tag: 46d49617-ebff-42b4-8f66-a0e428147360
1345 */