7949b0a948fb3d8437f0dd44d3d599ff7f5f68f6
[Faustine.git] / interpretor / libsndfile-1.0.25 / src / flac.c
1 /*
2 ** Copyright (C) 2004-2011 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 #include <math.h>
28
29 #include "sndfile.h"
30 #include "common.h"
31
32 #if HAVE_EXTERNAL_LIBS
33
34 #include <FLAC/stream_decoder.h>
35 #include <FLAC/stream_encoder.h>
36 #include <FLAC/metadata.h>
37
38 /*------------------------------------------------------------------------------
39 ** Private static functions.
40 */
41
42 #define ENC_BUFFER_SIZE 8192
43
44 typedef enum
45 { PFLAC_PCM_SHORT = 50,
46 PFLAC_PCM_INT = 51,
47 PFLAC_PCM_FLOAT = 52,
48 PFLAC_PCM_DOUBLE = 53
49 } PFLAC_PCM ;
50
51 typedef struct
52 {
53 FLAC__StreamDecoder *fsd ;
54 FLAC__StreamEncoder *fse ;
55
56 PFLAC_PCM pcmtype ;
57 void* ptr ;
58 unsigned pos, len, remain ;
59
60 FLAC__StreamMetadata *metadata ;
61
62 const FLAC__int32 * const * wbuffer ;
63 FLAC__int32 * rbuffer [FLAC__MAX_CHANNELS] ;
64
65 FLAC__int32* encbuffer ;
66 unsigned bufferpos ;
67
68 const FLAC__Frame *frame ;
69 FLAC__bool bufferbackup ;
70 } FLAC_PRIVATE ;
71
72 typedef struct
73 { const char *tag ;
74 int type ;
75 } FLAC_TAG ;
76
77 static sf_count_t flac_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
78 static int flac_close (SF_PRIVATE *psf) ;
79
80 static int flac_enc_init (SF_PRIVATE *psf) ;
81 static int flac_read_header (SF_PRIVATE *psf) ;
82
83 static sf_count_t flac_read_flac2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
84 static sf_count_t flac_read_flac2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
85 static sf_count_t flac_read_flac2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
86 static sf_count_t flac_read_flac2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
87
88 static sf_count_t flac_write_s2flac (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
89 static sf_count_t flac_write_i2flac (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
90 static sf_count_t flac_write_f2flac (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
91 static sf_count_t flac_write_d2flac (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
92
93 static void f2flac8_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
94 static void f2flac16_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
95 static void f2flac24_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
96 static void f2flac8_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
97 static void f2flac16_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
98 static void f2flac24_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
99 static void d2flac8_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
100 static void d2flac16_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
101 static void d2flac24_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
102 static void d2flac8_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
103 static void d2flac16_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
104 static void d2flac24_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
105
106 static int flac_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
107
108 /* Decoder Callbacks */
109 static FLAC__StreamDecoderReadStatus sf_flac_read_callback (const FLAC__StreamDecoder *decoder, FLAC__byte buffer [], size_t *bytes, void *client_data) ;
110 static FLAC__StreamDecoderSeekStatus sf_flac_seek_callback (const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data) ;
111 static FLAC__StreamDecoderTellStatus sf_flac_tell_callback (const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) ;
112 static FLAC__StreamDecoderLengthStatus sf_flac_length_callback (const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data) ;
113 static FLAC__bool sf_flac_eof_callback (const FLAC__StreamDecoder *decoder, void *client_data) ;
114 static FLAC__StreamDecoderWriteStatus sf_flac_write_callback (const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer [], void *client_data) ;
115 static void sf_flac_meta_callback (const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) ;
116 static void sf_flac_error_callback (const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) ;
117
118 /* Encoder Callbacks */
119 static FLAC__StreamEncoderSeekStatus sf_flac_enc_seek_callback (const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data) ;
120 static FLAC__StreamEncoderTellStatus sf_flac_enc_tell_callback (const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data) ;
121 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) ;
122
123 static void
124 s2flac8_array (const short *src, FLAC__int32 *dest, int count)
125 { while (--count >= 0)
126 dest [count] = src [count] >> 8 ;
127 } /* s2flac8_array */
128
129 static void
130 s2flac16_array (const short *src, FLAC__int32 *dest, int count)
131 { while (--count >= 0)
132 dest [count] = src [count] ;
133 } /* s2flac16_array */
134
135 static void
136 s2flac24_array (const short *src, FLAC__int32 *dest, int count)
137 { while (--count >= 0)
138 dest [count] = src [count] << 8 ;
139 } /* s2flac24_array */
140
141 static void
142 i2flac8_array (const int *src, FLAC__int32 *dest, int count)
143 { while (--count >= 0)
144 dest [count] = src [count] >> 24 ;
145 } /* i2flac8_array */
146
147 static void
148 i2flac16_array (const int *src, FLAC__int32 *dest, int count)
149 {
150 while (--count >= 0)
151 dest [count] = src [count] >> 16 ;
152 } /* i2flac16_array */
153
154 static void
155 i2flac24_array (const int *src, FLAC__int32 *dest, int count)
156 { while (--count >= 0)
157 dest [count] = src [count] >> 8 ;
158 } /* i2flac24_array */
159
160 static sf_count_t
161 flac_buffer_copy (SF_PRIVATE *psf)
162 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
163 const FLAC__Frame *frame = pflac->frame ;
164 const FLAC__int32* const *buffer = pflac->wbuffer ;
165 unsigned i = 0, j, offset ;
166
167 /*
168 ** frame->header.blocksize is variable and we're using a constant blocksize
169 ** of FLAC__MAX_BLOCK_SIZE.
170 ** Check our assumptions here.
171 */
172 if (frame->header.blocksize > FLAC__MAX_BLOCK_SIZE)
173 { psf_log_printf (psf, "Ooops : frame->header.blocksize (%d) > FLAC__MAX_BLOCK_SIZE (%d)\n", __func__, __LINE__, frame->header.blocksize, FLAC__MAX_BLOCK_SIZE) ;
174 psf->error = SFE_INTERNAL ;
175 return 0 ;
176 } ;
177
178 if (pflac->ptr == NULL)
179 { /*
180 ** Not sure why this code is here and not elsewhere.
181 ** Removing it causes valgrind errors.
182 */
183 pflac->bufferbackup = SF_TRUE ;
184 for (i = 0 ; i < frame->header.channels ; i++)
185 {
186 if (pflac->rbuffer [i] == NULL)
187 pflac->rbuffer [i] = calloc (FLAC__MAX_BLOCK_SIZE, sizeof (FLAC__int32)) ;
188
189 memcpy (pflac->rbuffer [i], buffer [i], frame->header.blocksize * sizeof (FLAC__int32)) ;
190 } ;
191 pflac->wbuffer = (const FLAC__int32* const*) pflac->rbuffer ;
192
193 return 0 ;
194 } ;
195
196 switch (pflac->pcmtype)
197 { case PFLAC_PCM_SHORT :
198 { short *retpcm = (short*) pflac->ptr ;
199 int shift = 16 - frame->header.bits_per_sample ;
200 if (shift < 0)
201 { shift = abs (shift) ;
202 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
203 { offset = pflac->pos + i * frame->header.channels ;
204
205 if (pflac->bufferpos >= frame->header.blocksize)
206 break ;
207
208 for (j = 0 ; j < frame->header.channels ; j++)
209 retpcm [offset + j] = buffer [j][pflac->bufferpos] >> shift ;
210 pflac->remain -= frame->header.channels ;
211 pflac->bufferpos++ ;
212 }
213 }
214 else
215 { for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
216 { offset = pflac->pos + i * frame->header.channels ;
217
218 if (pflac->bufferpos >= frame->header.blocksize)
219 break ;
220
221 for (j = 0 ; j < frame->header.channels ; j++)
222 retpcm [offset + j] = (buffer [j][pflac->bufferpos]) << shift ;
223
224 pflac->remain -= frame->header.channels ;
225 pflac->bufferpos++ ;
226 } ;
227 } ;
228 } ;
229 break ;
230
231 case PFLAC_PCM_INT :
232 { int *retpcm = (int*) pflac->ptr ;
233 int shift = 32 - frame->header.bits_per_sample ;
234 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
235 { offset = pflac->pos + i * frame->header.channels ;
236
237 if (pflac->bufferpos >= frame->header.blocksize)
238 break ;
239
240 for (j = 0 ; j < frame->header.channels ; j++)
241 retpcm [offset + j] = buffer [j][pflac->bufferpos] << shift ;
242 pflac->remain -= frame->header.channels ;
243 pflac->bufferpos++ ;
244 } ;
245 } ;
246 break ;
247
248 case PFLAC_PCM_FLOAT :
249 { float *retpcm = (float*) pflac->ptr ;
250 float norm = (psf->norm_float == SF_TRUE) ? 1.0 / (1 << (frame->header.bits_per_sample - 1)) : 1.0 ;
251
252 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
253 { offset = pflac->pos + i * frame->header.channels ;
254
255 if (pflac->bufferpos >= frame->header.blocksize)
256 break ;
257
258 for (j = 0 ; j < frame->header.channels ; j++)
259 retpcm [offset + j] = buffer [j][pflac->bufferpos] * norm ;
260 pflac->remain -= frame->header.channels ;
261 pflac->bufferpos++ ;
262 } ;
263 } ;
264 break ;
265
266 case PFLAC_PCM_DOUBLE :
267 { double *retpcm = (double*) pflac->ptr ;
268 double norm = (psf->norm_double == SF_TRUE) ? 1.0 / (1 << (frame->header.bits_per_sample - 1)) : 1.0 ;
269
270 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
271 { offset = pflac->pos + i * frame->header.channels ;
272
273 if (pflac->bufferpos >= frame->header.blocksize)
274 break ;
275
276 for (j = 0 ; j < frame->header.channels ; j++)
277 retpcm [offset + j] = buffer [j][pflac->bufferpos] * norm ;
278 pflac->remain -= frame->header.channels ;
279 pflac->bufferpos++ ;
280 } ;
281 } ;
282 break ;
283
284 default :
285 return 0 ;
286 } ;
287
288 offset = i * frame->header.channels ;
289 pflac->pos += i * frame->header.channels ;
290
291 return offset ;
292 } /* flac_buffer_copy */
293
294
295 static FLAC__StreamDecoderReadStatus
296 sf_flac_read_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__byte buffer [], size_t *bytes, void *client_data)
297 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
298
299 *bytes = psf_fread (buffer, 1, *bytes, psf) ;
300 if (*bytes > 0 && psf->error == 0)
301 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE ;
302
303 return FLAC__STREAM_DECODER_READ_STATUS_ABORT ;
304 } /* sf_flac_read_callback */
305
306 static FLAC__StreamDecoderSeekStatus
307 sf_flac_seek_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__uint64 absolute_byte_offset, void *client_data)
308 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
309
310 psf_fseek (psf, absolute_byte_offset, SEEK_SET) ;
311 if (psf->error)
312 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR ;
313
314 return FLAC__STREAM_DECODER_SEEK_STATUS_OK ;
315 } /* sf_flac_seek_callback */
316
317 static FLAC__StreamDecoderTellStatus
318 sf_flac_tell_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__uint64 *absolute_byte_offset, void *client_data)
319 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
320
321 *absolute_byte_offset = psf_ftell (psf) ;
322 if (psf->error)
323 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR ;
324
325 return FLAC__STREAM_DECODER_TELL_STATUS_OK ;
326 } /* sf_flac_tell_callback */
327
328 static FLAC__StreamDecoderLengthStatus
329 sf_flac_length_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__uint64 *stream_length, void *client_data)
330 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
331
332 if ((*stream_length = psf->filelength) == 0)
333 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR ;
334
335 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK ;
336 } /* sf_flac_length_callback */
337
338 static FLAC__bool
339 sf_flac_eof_callback (const FLAC__StreamDecoder *UNUSED (decoder), void *client_data)
340 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
341
342 if (psf_ftell (psf) == psf->filelength)
343 return SF_TRUE ;
344
345 return SF_FALSE ;
346 } /* sf_flac_eof_callback */
347
348 static FLAC__StreamDecoderWriteStatus
349 sf_flac_write_callback (const FLAC__StreamDecoder * UNUSED (decoder), const FLAC__Frame *frame, const FLAC__int32 * const buffer [], void *client_data)
350 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
351 FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
352
353 pflac->frame = frame ;
354 pflac->bufferpos = 0 ;
355
356 pflac->bufferbackup = SF_FALSE ;
357 pflac->wbuffer = buffer ;
358
359 flac_buffer_copy (psf) ;
360
361 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE ;
362 } /* sf_flac_write_callback */
363
364 static void
365 sf_flac_meta_get_vorbiscomments (SF_PRIVATE *psf, const FLAC__StreamMetadata *metadata)
366 { FLAC_TAG tags [] =
367 { { "title", SF_STR_TITLE },
368 { "copyright", SF_STR_COPYRIGHT },
369 { "software", SF_STR_SOFTWARE },
370 { "artist", SF_STR_ARTIST },
371 { "comment", SF_STR_COMMENT },
372 { "date", SF_STR_DATE },
373 { "album", SF_STR_ALBUM },
374 { "license", SF_STR_LICENSE },
375 { "tracknumber", SF_STR_TRACKNUMBER },
376 { "genre", SF_STR_GENRE }
377 } ;
378
379 const char *value, *cptr ;
380 int k, tag_num ;
381
382 for (k = 0 ; k < ARRAY_LEN (tags) ; k++)
383 { tag_num = FLAC__metadata_object_vorbiscomment_find_entry_from (metadata, 0, tags [k].tag) ;
384
385 if (tag_num < 0)
386 continue ;
387
388 value = (const char*) metadata->data.vorbis_comment.comments [tag_num].entry ;
389 if ((cptr = strchr (value, '=')) != NULL)
390 value = cptr + 1 ;
391
392 psf_log_printf (psf, " %-10s : %s\n", tags [k].tag, value) ;
393 psf_store_string (psf, tags [k].type, value) ;
394 } ;
395
396 return ;
397 } /* sf_flac_meta_get_vorbiscomments */
398
399 static void
400 sf_flac_meta_callback (const FLAC__StreamDecoder * UNUSED (decoder), const FLAC__StreamMetadata *metadata, void *client_data)
401 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
402 int bitwidth = 0 ;
403
404 switch (metadata->type)
405 { case FLAC__METADATA_TYPE_STREAMINFO :
406 psf->sf.channels = metadata->data.stream_info.channels ;
407 psf->sf.samplerate = metadata->data.stream_info.sample_rate ;
408 psf->sf.frames = metadata->data.stream_info.total_samples ;
409
410 psf_log_printf (psf, "FLAC Stream Metadata\n Channels : %d\n Sample rate : %d\n", psf->sf.channels, psf->sf.samplerate) ;
411
412 if (psf->sf.frames == 0)
413 { psf_log_printf (psf, " Frames : 0 (bumping to SF_COUNT_MAX)\n") ;
414 psf->sf.frames = SF_COUNT_MAX ;
415 }
416 else
417 psf_log_printf (psf, " Frames : %D\n", psf->sf.frames) ;
418
419 switch (metadata->data.stream_info.bits_per_sample)
420 { case 8 :
421 psf->sf.format |= SF_FORMAT_PCM_S8 ;
422 bitwidth = 8 ;
423 break ;
424 case 16 :
425 psf->sf.format |= SF_FORMAT_PCM_16 ;
426 bitwidth = 16 ;
427 break ;
428 case 24 :
429 psf->sf.format |= SF_FORMAT_PCM_24 ;
430 bitwidth = 24 ;
431 break ;
432 default :
433 psf_log_printf (psf, "sf_flac_meta_callback : bits_per_sample %d not yet implemented.\n", metadata->data.stream_info.bits_per_sample) ;
434 break ;
435 } ;
436
437 if (bitwidth > 0)
438 psf_log_printf (psf, " Bit width : %d\n", bitwidth) ;
439 break ;
440
441 case FLAC__METADATA_TYPE_VORBIS_COMMENT :
442 psf_log_printf (psf, "Vorbis Comment Metadata\n") ;
443 sf_flac_meta_get_vorbiscomments (psf, metadata) ;
444 break ;
445
446 case FLAC__METADATA_TYPE_PADDING :
447 psf_log_printf (psf, "Padding Metadata\n") ;
448 break ;
449
450 case FLAC__METADATA_TYPE_APPLICATION :
451 psf_log_printf (psf, "Application Metadata\n") ;
452 break ;
453
454 case FLAC__METADATA_TYPE_SEEKTABLE :
455 psf_log_printf (psf, "Seektable Metadata\n") ;
456 break ;
457
458 case FLAC__METADATA_TYPE_CUESHEET :
459 psf_log_printf (psf, "Cuesheet Metadata\n") ;
460 break ;
461
462 case FLAC__METADATA_TYPE_PICTURE :
463 psf_log_printf (psf, "Picture Metadata\n") ;
464 break ;
465
466 case FLAC__METADATA_TYPE_UNDEFINED :
467 psf_log_printf (psf, "Undefined Metadata\n") ;
468 break ;
469
470 default :
471 psf_log_printf (psf, "sf_flac_meta_callback : metadata-type %d not yet implemented.\n", metadata->type) ;
472 break ;
473 } ;
474
475 return ;
476 } /* sf_flac_meta_callback */
477
478 static void
479 sf_flac_error_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__StreamDecoderErrorStatus status, void *client_data)
480 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
481
482 psf_log_printf (psf, "ERROR : %s\n", FLAC__StreamDecoderErrorStatusString [status]) ;
483
484 switch (status)
485 { case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC :
486 psf->error = SFE_FLAC_LOST_SYNC ;
487 break ;
488 case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER :
489 psf->error = SFE_FLAC_BAD_HEADER ;
490 break ;
491 default :
492 psf->error = SFE_FLAC_UNKOWN_ERROR ;
493 break ;
494 } ;
495
496 return ;
497 } /* sf_flac_error_callback */
498
499 static FLAC__StreamEncoderSeekStatus
500 sf_flac_enc_seek_callback (const FLAC__StreamEncoder * UNUSED (encoder), FLAC__uint64 absolute_byte_offset, void *client_data)
501 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
502
503 psf_fseek (psf, absolute_byte_offset, SEEK_SET) ;
504 if (psf->error)
505 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR ;
506
507 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK ;
508 } /* sf_flac_enc_seek_callback */
509
510 static FLAC__StreamEncoderTellStatus
511 sf_flac_enc_tell_callback (const FLAC__StreamEncoder *UNUSED (encoder), FLAC__uint64 *absolute_byte_offset, void *client_data)
512 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
513
514 *absolute_byte_offset = psf_ftell (psf) ;
515 if (psf->error)
516 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR ;
517
518 return FLAC__STREAM_ENCODER_TELL_STATUS_OK ;
519 } /* sf_flac_enc_tell_callback */
520
521 static FLAC__StreamEncoderWriteStatus
522 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)
523 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
524
525 if (psf_fwrite (buffer, 1, bytes, psf) == (sf_count_t) bytes && psf->error == 0)
526 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK ;
527
528 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR ;
529 } /* sf_flac_enc_write_callback */
530
531 static void
532 flac_write_strings (SF_PRIVATE *psf, FLAC_PRIVATE* pflac)
533 { FLAC__StreamMetadata_VorbisComment_Entry entry ;
534 int k, string_count = 0 ;
535
536 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
537 { if (psf->strings [k].type != 0)
538 string_count ++ ;
539 } ;
540
541 if (string_count == 0)
542 return ;
543
544 if (pflac->metadata == NULL && (pflac->metadata = FLAC__metadata_object_new (FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL)
545 { psf_log_printf (psf, "FLAC__metadata_object_new returned NULL\n") ;
546 return ;
547 } ;
548
549 for (k = 0 ; k < SF_MAX_STRINGS && psf->strings [k].type != 0 ; k++)
550 { const char * key, * value ;
551
552 switch (psf->strings [k].type)
553 { case SF_STR_SOFTWARE :
554 key = "software" ;
555 break ;
556 case SF_STR_TITLE :
557 key = "title" ;
558 break ;
559 case SF_STR_COPYRIGHT :
560 key = "copyright" ;
561 break ;
562 case SF_STR_ARTIST :
563 key = "artist" ;
564 break ;
565 case SF_STR_COMMENT :
566 key = "comment" ;
567 break ;
568 case SF_STR_DATE :
569 key = "date" ;
570 break ;
571 case SF_STR_ALBUM :
572 key = "album" ;
573 break ;
574 case SF_STR_LICENSE :
575 key = "license" ;
576 break ;
577 case SF_STR_TRACKNUMBER :
578 key = "tracknumber" ;
579 break ;
580 case SF_STR_GENRE :
581 key = "genre" ;
582 break ;
583 default :
584 continue ;
585 } ;
586
587 value = psf->strings [k].str ;
588
589 FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair (&entry, key, value) ;
590 FLAC__metadata_object_vorbiscomment_append_comment (pflac->metadata, entry, /* copy */ SF_FALSE) ;
591 } ;
592
593 if (! FLAC__stream_encoder_set_metadata (pflac->fse, &pflac->metadata, 1))
594 { printf ("%s %d : fail\n", __func__, __LINE__) ;
595 return ;
596 } ;
597
598 return ;
599 } /* flac_write_strings */
600
601 static int
602 flac_write_header (SF_PRIVATE *psf, int UNUSED (calc_length))
603 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
604 int err ;
605
606 flac_write_strings (psf, pflac) ;
607
608 if ((err = 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)
609 { psf_log_printf (psf, "Error : FLAC encoder init returned error : %s\n", FLAC__StreamEncoderInitStatusString [err]) ;
610 return SFE_FLAC_INIT_DECODER ;
611 } ;
612
613 if (psf->error == 0)
614 psf->dataoffset = psf_ftell (psf) ;
615 pflac->encbuffer = calloc (ENC_BUFFER_SIZE, sizeof (FLAC__int32)) ;
616
617 return psf->error ;
618 } /* flac_write_header */
619
620 /*------------------------------------------------------------------------------
621 ** Public function.
622 */
623
624 int
625 flac_open (SF_PRIVATE *psf)
626 { int subformat ;
627 int error = 0 ;
628
629 FLAC_PRIVATE* pflac = calloc (1, sizeof (FLAC_PRIVATE)) ;
630 psf->codec_data = pflac ;
631
632 if (psf->file.mode == SFM_RDWR)
633 return SFE_BAD_MODE_RW ;
634
635 if (psf->file.mode == SFM_READ)
636 { if ((error = flac_read_header (psf)))
637 return error ;
638 } ;
639
640 subformat = SF_CODEC (psf->sf.format) ;
641
642 if (psf->file.mode == SFM_WRITE)
643 { if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_FLAC)
644 return SFE_BAD_OPEN_FORMAT ;
645
646 psf->endian = SF_ENDIAN_BIG ;
647 psf->sf.seekable = 0 ;
648
649 psf->str_flags = SF_STR_ALLOW_START ;
650
651 if ((error = flac_enc_init (psf)))
652 return error ;
653
654 psf->write_header = flac_write_header ;
655 } ;
656
657 psf->datalength = psf->filelength ;
658 psf->dataoffset = 0 ;
659 psf->blockwidth = 0 ;
660 psf->bytewidth = 1 ;
661
662 psf->container_close = flac_close ;
663 psf->seek = flac_seek ;
664 psf->command = flac_command ;
665
666 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
667
668 switch (subformat)
669 { case SF_FORMAT_PCM_S8 : /* 8-bit FLAC. */
670 case SF_FORMAT_PCM_16 : /* 16-bit FLAC. */
671 case SF_FORMAT_PCM_24 : /* 24-bit FLAC. */
672 error = flac_init (psf) ;
673 break ;
674
675 default : return SFE_UNIMPLEMENTED ;
676 } ;
677
678 return error ;
679 } /* flac_open */
680
681 /*------------------------------------------------------------------------------
682 */
683
684 static int
685 flac_close (SF_PRIVATE *psf)
686 { FLAC_PRIVATE* pflac ;
687 int k ;
688
689 if ((pflac = (FLAC_PRIVATE*) psf->codec_data) == NULL)
690 return 0 ;
691
692 if (pflac->metadata != NULL)
693 FLAC__metadata_object_delete (pflac->metadata) ;
694
695 if (psf->file.mode == SFM_WRITE)
696 { FLAC__stream_encoder_finish (pflac->fse) ;
697 FLAC__stream_encoder_delete (pflac->fse) ;
698
699 if (pflac->encbuffer)
700 free (pflac->encbuffer) ;
701 } ;
702
703 if (psf->file.mode == SFM_READ)
704 { FLAC__stream_decoder_finish (pflac->fsd) ;
705 FLAC__stream_decoder_delete (pflac->fsd) ;
706 } ;
707
708 for (k = 0 ; k < ARRAY_LEN (pflac->rbuffer) ; k++)
709 free (pflac->rbuffer [k]) ;
710
711 free (pflac) ;
712 psf->codec_data = NULL ;
713
714 return 0 ;
715 } /* flac_close */
716
717 static int
718 flac_enc_init (SF_PRIVATE *psf)
719 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
720 unsigned bps ;
721
722 /* To cite the flac FAQ at
723 ** http://flac.sourceforge.net/faq.html#general__samples
724 ** "FLAC supports linear sample rates from 1Hz - 655350Hz in 1Hz
725 ** increments."
726 */
727 if ( psf->sf.samplerate < 1 || psf->sf.samplerate > 655350 )
728 { psf_log_printf (psf, "flac sample rate out of range.\n", psf->sf.samplerate) ;
729 return SFE_FLAC_BAD_SAMPLE_RATE ;
730 } ;
731
732 psf_fseek (psf, 0, SEEK_SET) ;
733
734 switch (SF_CODEC (psf->sf.format))
735 { case SF_FORMAT_PCM_S8 :
736 bps = 8 ;
737 break ;
738 case SF_FORMAT_PCM_16 :
739 bps = 16 ;
740 break ;
741 case SF_FORMAT_PCM_24 :
742 bps = 24 ;
743 break ;
744
745 default :
746 bps = 0 ;
747 break ;
748 } ;
749
750 if ((pflac->fse = FLAC__stream_encoder_new ()) == NULL)
751 return SFE_FLAC_NEW_DECODER ;
752
753 if (! FLAC__stream_encoder_set_channels (pflac->fse, psf->sf.channels))
754 { psf_log_printf (psf, "FLAC__stream_encoder_set_channels (%d) return false.\n", psf->sf.channels) ;
755 return SFE_FLAC_INIT_DECODER ;
756 } ;
757
758 if (! FLAC__stream_encoder_set_sample_rate (pflac->fse, psf->sf.samplerate))
759 { psf_log_printf (psf, "FLAC__stream_encoder_set_sample_rate (%d) returned false.\n", psf->sf.samplerate) ;
760 return SFE_FLAC_BAD_SAMPLE_RATE ;
761 } ;
762
763 if (! FLAC__stream_encoder_set_bits_per_sample (pflac->fse, bps))
764 { psf_log_printf (psf, "FLAC__stream_encoder_set_bits_per_sample (%d) return false.\n", bps) ;
765 return SFE_FLAC_INIT_DECODER ;
766 } ;
767
768 return 0 ;
769 } /* flac_enc_init */
770
771 static int
772 flac_read_header (SF_PRIVATE *psf)
773 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
774
775 psf_fseek (psf, 0, SEEK_SET) ;
776 if ((pflac->fsd = FLAC__stream_decoder_new ()) == NULL)
777 return SFE_FLAC_NEW_DECODER ;
778
779 FLAC__stream_decoder_set_metadata_respond_all (pflac->fsd) ;
780
781 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)
782 return SFE_FLAC_INIT_DECODER ;
783
784 FLAC__stream_decoder_process_until_end_of_metadata (pflac->fsd) ;
785
786 psf_log_printf (psf, "End\n") ;
787
788 if (psf->error == 0)
789 { FLAC__uint64 position ;
790
791 FLAC__stream_decoder_get_decode_position (pflac->fsd, &position) ;
792 psf->dataoffset = position ;
793 } ;
794
795 return psf->error ;
796 } /* flac_read_header */
797
798 static int
799 flac_command (SF_PRIVATE * UNUSED (psf), int UNUSED (command), void * UNUSED (data), int UNUSED (datasize))
800 {
801 return 0 ;
802 } /* flac_command */
803
804 int
805 flac_init (SF_PRIVATE *psf)
806 {
807 if (psf->file.mode == SFM_RDWR)
808 return SFE_BAD_MODE_RW ;
809
810 if (psf->file.mode == SFM_READ)
811 { psf->read_short = flac_read_flac2s ;
812 psf->read_int = flac_read_flac2i ;
813 psf->read_float = flac_read_flac2f ;
814 psf->read_double = flac_read_flac2d ;
815 } ;
816
817 if (psf->file.mode == SFM_WRITE)
818 { psf->write_short = flac_write_s2flac ;
819 psf->write_int = flac_write_i2flac ;
820 psf->write_float = flac_write_f2flac ;
821 psf->write_double = flac_write_d2flac ;
822 } ;
823
824 psf->bytewidth = 1 ;
825 psf->blockwidth = psf->sf.channels ;
826
827 if (psf->filelength > psf->dataoffset)
828 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset : psf->filelength - psf->dataoffset ;
829 else
830 psf->datalength = 0 ;
831
832 return 0 ;
833 } /* flac_init */
834
835 static unsigned
836 flac_read_loop (SF_PRIVATE *psf, unsigned len)
837 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
838
839 pflac->pos = 0 ;
840 pflac->len = len ;
841 pflac->remain = len ;
842 if (pflac->frame != NULL && pflac->bufferpos < pflac->frame->header.blocksize)
843 flac_buffer_copy (psf) ;
844
845 while (pflac->pos < pflac->len)
846 { if (FLAC__stream_decoder_process_single (pflac->fsd) == 0)
847 break ;
848 if (FLAC__stream_decoder_get_state (pflac->fsd) >= FLAC__STREAM_DECODER_END_OF_STREAM)
849 break ;
850 } ;
851
852 pflac->ptr = NULL ;
853
854 return pflac->pos ;
855 } /* flac_read_loop */
856
857 static sf_count_t
858 flac_read_flac2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
859 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
860 sf_count_t total = 0, current ;
861 unsigned readlen ;
862
863 pflac->pcmtype = PFLAC_PCM_SHORT ;
864
865 while (total < len)
866 { pflac->ptr = ptr + total ;
867 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
868 current = flac_read_loop (psf, readlen) ;
869 if (current == 0)
870 break ;
871 total += current ;
872 } ;
873
874 return total ;
875 } /* flac_read_flac2s */
876
877 static sf_count_t
878 flac_read_flac2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
879 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
880 sf_count_t total = 0, current ;
881 unsigned readlen ;
882
883 pflac->pcmtype = PFLAC_PCM_INT ;
884
885 while (total < len)
886 { pflac->ptr = ptr + total ;
887 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
888 current = flac_read_loop (psf, readlen) ;
889 if (current == 0)
890 break ;
891 total += current ;
892 } ;
893
894 return total ;
895 } /* flac_read_flac2i */
896
897 static sf_count_t
898 flac_read_flac2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
899 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
900 sf_count_t total = 0, current ;
901 unsigned readlen ;
902
903 pflac->pcmtype = PFLAC_PCM_FLOAT ;
904
905 while (total < len)
906 { pflac->ptr = ptr + total ;
907 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
908 current = flac_read_loop (psf, readlen) ;
909 if (current == 0)
910 break ;
911 total += current ;
912 } ;
913
914 return total ;
915 } /* flac_read_flac2f */
916
917 static sf_count_t
918 flac_read_flac2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
919 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
920 sf_count_t total = 0, current ;
921 unsigned readlen ;
922
923 pflac->pcmtype = PFLAC_PCM_DOUBLE ;
924
925 while (total < len)
926 { pflac->ptr = ptr + total ;
927 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
928 current = flac_read_loop (psf, readlen) ;
929 if (current == 0)
930 break ;
931 total += current ;
932 } ;
933
934 return total ;
935 } /* flac_read_flac2d */
936
937 static sf_count_t
938 flac_write_s2flac (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
939 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
940 void (*convert) (const short *, FLAC__int32 *, int) ;
941 int bufferlen, writecount, thiswrite ;
942 sf_count_t total = 0 ;
943 FLAC__int32* buffer = pflac->encbuffer ;
944
945 switch (SF_CODEC (psf->sf.format))
946 { case SF_FORMAT_PCM_S8 :
947 convert = s2flac8_array ;
948 break ;
949 case SF_FORMAT_PCM_16 :
950 convert = s2flac16_array ;
951 break ;
952 case SF_FORMAT_PCM_24 :
953 convert = s2flac24_array ;
954 break ;
955 default :
956 return -1 ;
957 } ;
958
959 bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
960 bufferlen *= psf->sf.channels ;
961
962 while (len > 0)
963 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
964 convert (ptr + total, buffer, writecount) ;
965 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
966 thiswrite = writecount ;
967 else
968 break ;
969 total += thiswrite ;
970 if (thiswrite < writecount)
971 break ;
972
973 len -= thiswrite ;
974 } ;
975
976 return total ;
977 } /* flac_write_s2flac */
978
979 static sf_count_t
980 flac_write_i2flac (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
981 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
982 void (*convert) (const int *, FLAC__int32 *, int) ;
983 int bufferlen, writecount, thiswrite ;
984 sf_count_t total = 0 ;
985 FLAC__int32* buffer = pflac->encbuffer ;
986
987 switch (SF_CODEC (psf->sf.format))
988 { case SF_FORMAT_PCM_S8 :
989 convert = i2flac8_array ;
990 break ;
991 case SF_FORMAT_PCM_16 :
992 convert = i2flac16_array ;
993 break ;
994 case SF_FORMAT_PCM_24 :
995 convert = i2flac24_array ;
996 break ;
997 default :
998 return -1 ;
999 } ;
1000
1001 bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
1002 bufferlen *= psf->sf.channels ;
1003
1004 while (len > 0)
1005 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1006 convert (ptr + total, buffer, writecount) ;
1007 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
1008 thiswrite = writecount ;
1009 else
1010 break ;
1011 total += thiswrite ;
1012 if (thiswrite < writecount)
1013 break ;
1014
1015 len -= thiswrite ;
1016 } ;
1017
1018 return total ;
1019 } /* flac_write_i2flac */
1020
1021 static sf_count_t
1022 flac_write_f2flac (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
1023 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
1024 void (*convert) (const float *, FLAC__int32 *, int, int) ;
1025 int bufferlen, writecount, thiswrite ;
1026 sf_count_t total = 0 ;
1027 FLAC__int32* buffer = pflac->encbuffer ;
1028
1029 switch (SF_CODEC (psf->sf.format))
1030 { case SF_FORMAT_PCM_S8 :
1031 convert = (psf->add_clipping) ? f2flac8_clip_array : f2flac8_array ;
1032 break ;
1033 case SF_FORMAT_PCM_16 :
1034 convert = (psf->add_clipping) ? f2flac16_clip_array : f2flac16_array ;
1035 break ;
1036 case SF_FORMAT_PCM_24 :
1037 convert = (psf->add_clipping) ? f2flac24_clip_array : f2flac24_array ;
1038 break ;
1039 default :
1040 return -1 ;
1041 } ;
1042
1043 bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
1044 bufferlen *= psf->sf.channels ;
1045
1046 while (len > 0)
1047 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1048 convert (ptr + total, buffer, writecount, psf->norm_float) ;
1049 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
1050 thiswrite = writecount ;
1051 else
1052 break ;
1053 total += thiswrite ;
1054 if (thiswrite < writecount)
1055 break ;
1056
1057 len -= thiswrite ;
1058 } ;
1059
1060 return total ;
1061 } /* flac_write_f2flac */
1062
1063 static void
1064 f2flac8_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1065 { float normfact, scaled_value ;
1066
1067 normfact = normalize ? (8.0 * 0x10) : 1.0 ;
1068
1069 while (--count >= 0)
1070 { scaled_value = src [count] * normfact ;
1071 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7F))
1072 { dest [count] = 0x7F ;
1073 continue ;
1074 } ;
1075 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10))
1076 { dest [count] = 0x80 ;
1077 continue ;
1078 } ;
1079 dest [count] = lrintf (scaled_value) ;
1080 } ;
1081
1082 return ;
1083 } /* f2flac8_clip_array */
1084
1085 static void
1086 f2flac16_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1087 {
1088 float normfact, scaled_value ;
1089
1090 normfact = normalize ? (8.0 * 0x1000) : 1.0 ;
1091
1092 while (--count >= 0) {
1093 scaled_value = src [count] * normfact ;
1094 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFF)) {
1095 dest [count] = 0x7FFF ;
1096 continue ;
1097 }
1098 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000)) {
1099 dest [count] = 0x8000 ;
1100 continue ;
1101 }
1102 dest [count] = lrintf (scaled_value) ;
1103 }
1104 } /* f2flac16_clip_array */
1105
1106 static void
1107 f2flac24_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1108 { float normfact, scaled_value ;
1109
1110 normfact = normalize ? (8.0 * 0x100000) : 1.0 ;
1111
1112 while (--count >= 0)
1113 { scaled_value = src [count] * normfact ;
1114 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFF))
1115 { dest [count] = 0x7FFFFF ;
1116 continue ;
1117 } ;
1118
1119 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x100000))
1120 { dest [count] = 0x800000 ;
1121 continue ;
1122 }
1123 dest [count] = lrintf (scaled_value) ;
1124 } ;
1125
1126 return ;
1127 } /* f2flac24_clip_array */
1128
1129 static void
1130 f2flac8_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1131 { float normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
1132
1133 while (--count >= 0)
1134 dest [count] = lrintf (src [count] * normfact) ;
1135 } /* f2flac8_array */
1136
1137 static void
1138 f2flac16_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1139 { float normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
1140
1141 while (--count >= 0)
1142 dest [count] = lrintf (src [count] * normfact) ;
1143 } /* f2flac16_array */
1144
1145 static void
1146 f2flac24_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1147 { float normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
1148
1149 while (--count >= 0)
1150 dest [count] = lrintf (src [count] * normfact) ;
1151 } /* f2flac24_array */
1152
1153 static sf_count_t
1154 flac_write_d2flac (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
1155 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
1156 void (*convert) (const double *, FLAC__int32 *, int, int) ;
1157 int bufferlen, writecount, thiswrite ;
1158 sf_count_t total = 0 ;
1159 FLAC__int32* buffer = pflac->encbuffer ;
1160
1161 switch (SF_CODEC (psf->sf.format))
1162 { case SF_FORMAT_PCM_S8 :
1163 convert = (psf->add_clipping) ? d2flac8_clip_array : d2flac8_array ;
1164 break ;
1165 case SF_FORMAT_PCM_16 :
1166 convert = (psf->add_clipping) ? d2flac16_clip_array : d2flac16_array ;
1167 break ;
1168 case SF_FORMAT_PCM_24 :
1169 convert = (psf->add_clipping) ? d2flac24_clip_array : d2flac24_array ;
1170 break ;
1171 default :
1172 return -1 ;
1173 } ;
1174
1175 bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
1176 bufferlen *= psf->sf.channels ;
1177
1178 while (len > 0)
1179 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1180 convert (ptr + total, buffer, writecount, psf->norm_double) ;
1181 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount/psf->sf.channels))
1182 thiswrite = writecount ;
1183 else
1184 break ;
1185 total += thiswrite ;
1186 if (thiswrite < writecount)
1187 break ;
1188
1189 len -= thiswrite ;
1190 } ;
1191
1192 return total ;
1193 } /* flac_write_d2flac */
1194
1195 static void
1196 d2flac8_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1197 { double normfact, scaled_value ;
1198
1199 normfact = normalize ? (8.0 * 0x10) : 1.0 ;
1200
1201 while (--count >= 0)
1202 { scaled_value = src [count] * normfact ;
1203 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7F))
1204 { dest [count] = 0x7F ;
1205 continue ;
1206 } ;
1207 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10))
1208 { dest [count] = 0x80 ;
1209 continue ;
1210 } ;
1211 dest [count] = lrint (scaled_value) ;
1212 } ;
1213
1214 return ;
1215 } /* d2flac8_clip_array */
1216
1217 static void
1218 d2flac16_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1219 { double normfact, scaled_value ;
1220
1221 normfact = normalize ? (8.0 * 0x1000) : 1.0 ;
1222
1223 while (--count >= 0)
1224 { scaled_value = src [count] * normfact ;
1225 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFF))
1226 { dest [count] = 0x7FFF ;
1227 continue ;
1228 } ;
1229 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000))
1230 { dest [count] = 0x8000 ;
1231 continue ;
1232 } ;
1233 dest [count] = lrint (scaled_value) ;
1234 } ;
1235
1236 return ;
1237 } /* d2flac16_clip_array */
1238
1239 static void
1240 d2flac24_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1241 { double normfact, scaled_value ;
1242
1243 normfact = normalize ? (8.0 * 0x100000) : 1.0 ;
1244
1245 while (--count >= 0)
1246 { scaled_value = src [count] * normfact ;
1247 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFF))
1248 { dest [count] = 0x7FFFFF ;
1249 continue ;
1250 } ;
1251 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x100000))
1252 { dest [count] = 0x800000 ;
1253 continue ;
1254 } ;
1255 dest [count] = lrint (scaled_value) ;
1256 } ;
1257
1258 return ;
1259 } /* d2flac24_clip_array */
1260
1261 static void
1262 d2flac8_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1263 { double normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
1264
1265 while (--count >= 0)
1266 dest [count] = lrint (src [count] * normfact) ;
1267 } /* d2flac8_array */
1268
1269 static void
1270 d2flac16_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1271 { double normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
1272
1273 while (--count >= 0)
1274 dest [count] = lrint (src [count] * normfact) ;
1275 } /* d2flac16_array */
1276
1277 static void
1278 d2flac24_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1279 { double normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
1280
1281 while (--count >= 0)
1282 dest [count] = lrint (src [count] * normfact) ;
1283 } /* d2flac24_array */
1284
1285 static sf_count_t
1286 flac_seek (SF_PRIVATE *psf, int UNUSED (mode), sf_count_t offset)
1287 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
1288
1289 if (pflac == NULL)
1290 return 0 ;
1291
1292 if (psf->dataoffset < 0)
1293 { psf->error = SFE_BAD_SEEK ;
1294 return ((sf_count_t) -1) ;
1295 } ;
1296
1297 pflac->frame = NULL ;
1298
1299 if (psf->file.mode == SFM_READ)
1300 { FLAC__uint64 position ;
1301 if (FLAC__stream_decoder_seek_absolute (pflac->fsd, offset))
1302 { FLAC__stream_decoder_get_decode_position (pflac->fsd, &position) ;
1303 return offset ;
1304 } ;
1305
1306 return ((sf_count_t) -1) ;
1307 } ;
1308
1309 /* Seeking in write mode not yet supported. */
1310 psf->error = SFE_BAD_SEEK ;
1311
1312 return ((sf_count_t) -1) ;
1313 } /* flac_seek */
1314
1315 #else /* HAVE_EXTERNAL_LIBS */
1316
1317 int
1318 flac_open (SF_PRIVATE *psf)
1319 {
1320 psf_log_printf (psf, "This version of libsndfile was compiled without FLAC support.\n") ;
1321 return SFE_UNIMPLEMENTED ;
1322 } /* flac_open */
1323
1324 #endif