2 ** Copyright (C) 2004-2014 Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** Copyright (C) 2004 Tobias Gehrig <tgehrig@ira.uka.de>
5 ** This program is free software ; you can redistribute it and/or modify
6 ** it under the terms of the GNU Lesser General Public License as published by
7 ** the Free Software Foundation ; either version 2.1 of the License, or
8 ** (at your option) any later version.
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY ; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU Lesser General Public License for more details.
15 ** You should have received a copy of the GNU Lesser General Public License
16 ** along with this program ; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #if HAVE_EXTERNAL_LIBS
34 #include <FLAC/stream_decoder.h>
35 #include <FLAC/stream_encoder.h>
36 #include <FLAC/metadata.h>
38 /*------------------------------------------------------------------------------
39 ** Private static functions.
42 #define FLAC_DEFAULT_COMPRESSION_LEVEL 5
44 #define ENC_BUFFER_SIZE 8192
47 { PFLAC_PCM_SHORT = 50,
55 FLAC__StreamDecoder *fsd ;
56 FLAC__StreamEncoder *fse ;
60 unsigned pos, len, remain ;
62 FLAC__StreamMetadata *metadata ;
64 const FLAC__int32 * const * wbuffer ;
65 FLAC__int32 * rbuffer [FLAC__MAX_CHANNELS] ;
67 FLAC__int32* encbuffer ;
70 const FLAC__Frame *frame ;
71 FLAC__bool bufferbackup ;
73 unsigned compression ;
81 static sf_count_t flac_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
82 static int flac_byterate (SF_PRIVATE *psf) ;
83 static int flac_close (SF_PRIVATE *psf) ;
85 static int flac_enc_init (SF_PRIVATE *psf) ;
86 static int flac_read_header (SF_PRIVATE *psf) ;
88 static sf_count_t flac_read_flac2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
89 static sf_count_t flac_read_flac2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
90 static sf_count_t flac_read_flac2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
91 static sf_count_t flac_read_flac2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
93 static sf_count_t flac_write_s2flac (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
94 static sf_count_t flac_write_i2flac (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
95 static sf_count_t flac_write_f2flac (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
96 static sf_count_t flac_write_d2flac (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
98 static void f2flac8_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
99 static void f2flac16_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
100 static void f2flac24_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
101 static void f2flac8_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
102 static void f2flac16_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
103 static void f2flac24_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize) ;
104 static void d2flac8_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
105 static void d2flac16_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
106 static void d2flac24_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
107 static void d2flac8_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
108 static void d2flac16_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
109 static void d2flac24_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize) ;
111 static int flac_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
113 /* Decoder Callbacks */
114 static FLAC__StreamDecoderReadStatus sf_flac_read_callback (const FLAC__StreamDecoder *decoder, FLAC__byte buffer [], size_t *bytes, void *client_data) ;
115 static FLAC__StreamDecoderSeekStatus sf_flac_seek_callback (const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data) ;
116 static FLAC__StreamDecoderTellStatus sf_flac_tell_callback (const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) ;
117 static FLAC__StreamDecoderLengthStatus sf_flac_length_callback (const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data) ;
118 static FLAC__bool sf_flac_eof_callback (const FLAC__StreamDecoder *decoder, void *client_data) ;
119 static FLAC__StreamDecoderWriteStatus sf_flac_write_callback (const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer [], void *client_data) ;
120 static void sf_flac_meta_callback (const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) ;
121 static void sf_flac_error_callback (const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) ;
123 /* Encoder Callbacks */
124 static FLAC__StreamEncoderSeekStatus sf_flac_enc_seek_callback (const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data) ;
125 static FLAC__StreamEncoderTellStatus sf_flac_enc_tell_callback (const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data) ;
126 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) ;
129 s2flac8_array (const short *src, FLAC__int32 *dest, int count)
130 { while (--count >= 0)
131 dest [count] = src [count] >> 8 ;
132 } /* s2flac8_array */
135 s2flac16_array (const short *src, FLAC__int32 *dest, int count)
136 { while (--count >= 0)
137 dest [count] = src [count] ;
138 } /* s2flac16_array */
141 s2flac24_array (const short *src, FLAC__int32 *dest, int count)
142 { while (--count >= 0)
143 dest [count] = src [count] << 8 ;
144 } /* s2flac24_array */
147 i2flac8_array (const int *src, FLAC__int32 *dest, int count)
148 { while (--count >= 0)
149 dest [count] = src [count] >> 24 ;
150 } /* i2flac8_array */
153 i2flac16_array (const int *src, FLAC__int32 *dest, int count)
156 dest [count] = src [count] >> 16 ;
157 } /* i2flac16_array */
160 i2flac24_array (const int *src, FLAC__int32 *dest, int count)
161 { while (--count >= 0)
162 dest [count] = src [count] >> 8 ;
163 } /* i2flac24_array */
166 flac_buffer_copy (SF_PRIVATE *psf)
167 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
168 const FLAC__Frame *frame = pflac->frame ;
169 const FLAC__int32* const *buffer = pflac->wbuffer ;
170 unsigned i = 0, j, offset ;
173 ** frame->header.blocksize is variable and we're using a constant blocksize
174 ** of FLAC__MAX_BLOCK_SIZE.
175 ** Check our assumptions here.
177 if (frame->header.blocksize > FLAC__MAX_BLOCK_SIZE)
178 { psf_log_printf (psf, "Ooops : frame->header.blocksize (%d) > FLAC__MAX_BLOCK_SIZE (%d)\n", __func__, __LINE__, frame->header.blocksize, FLAC__MAX_BLOCK_SIZE) ;
179 psf->error = SFE_INTERNAL ;
183 if (pflac->ptr == NULL)
185 ** Not sure why this code is here and not elsewhere.
186 ** Removing it causes valgrind errors.
188 pflac->bufferbackup = SF_TRUE ;
189 for (i = 0 ; i < frame->header.channels ; i++)
191 if (pflac->rbuffer [i] == NULL)
192 pflac->rbuffer [i] = calloc (FLAC__MAX_BLOCK_SIZE, sizeof (FLAC__int32)) ;
194 memcpy (pflac->rbuffer [i], buffer [i], frame->header.blocksize * sizeof (FLAC__int32)) ;
196 pflac->wbuffer = (const FLAC__int32* const*) pflac->rbuffer ;
201 switch (pflac->pcmtype)
202 { case PFLAC_PCM_SHORT :
203 { short *retpcm = (short*) pflac->ptr ;
204 int shift = 16 - frame->header.bits_per_sample ;
206 { shift = abs (shift) ;
207 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
208 { offset = pflac->pos + i * frame->header.channels ;
210 if (pflac->bufferpos >= frame->header.blocksize)
213 for (j = 0 ; j < frame->header.channels ; j++)
214 retpcm [offset + j] = buffer [j][pflac->bufferpos] >> shift ;
215 pflac->remain -= frame->header.channels ;
220 { for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
221 { offset = pflac->pos + i * frame->header.channels ;
223 if (pflac->bufferpos >= frame->header.blocksize)
226 for (j = 0 ; j < frame->header.channels ; j++)
227 retpcm [offset + j] = ((uint16_t) buffer [j][pflac->bufferpos]) << shift ;
229 pflac->remain -= frame->header.channels ;
237 { int *retpcm = (int*) pflac->ptr ;
238 int shift = 32 - frame->header.bits_per_sample ;
239 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
240 { offset = pflac->pos + i * frame->header.channels ;
242 if (pflac->bufferpos >= frame->header.blocksize)
245 for (j = 0 ; j < frame->header.channels ; j++)
246 retpcm [offset + j] = ((uint32_t) buffer [j][pflac->bufferpos]) << shift ;
247 pflac->remain -= frame->header.channels ;
253 case PFLAC_PCM_FLOAT :
254 { float *retpcm = (float*) pflac->ptr ;
255 float norm = (psf->norm_float == SF_TRUE) ? 1.0 / (1 << (frame->header.bits_per_sample - 1)) : 1.0 ;
257 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
258 { offset = pflac->pos + i * frame->header.channels ;
260 if (pflac->bufferpos >= frame->header.blocksize)
263 for (j = 0 ; j < frame->header.channels ; j++)
264 retpcm [offset + j] = buffer [j][pflac->bufferpos] * norm ;
265 pflac->remain -= frame->header.channels ;
271 case PFLAC_PCM_DOUBLE :
272 { double *retpcm = (double*) pflac->ptr ;
273 double norm = (psf->norm_double == SF_TRUE) ? 1.0 / (1 << (frame->header.bits_per_sample - 1)) : 1.0 ;
275 for (i = 0 ; i < frame->header.blocksize && pflac->remain > 0 ; i++)
276 { offset = pflac->pos + i * frame->header.channels ;
278 if (pflac->bufferpos >= frame->header.blocksize)
281 for (j = 0 ; j < frame->header.channels ; j++)
282 retpcm [offset + j] = buffer [j][pflac->bufferpos] * norm ;
283 pflac->remain -= frame->header.channels ;
293 offset = i * frame->header.channels ;
294 pflac->pos += i * frame->header.channels ;
297 } /* flac_buffer_copy */
300 static FLAC__StreamDecoderReadStatus
301 sf_flac_read_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__byte buffer [], size_t *bytes, void *client_data)
302 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
304 *bytes = psf_fread (buffer, 1, *bytes, psf) ;
305 if (*bytes > 0 && psf->error == 0)
306 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE ;
308 return FLAC__STREAM_DECODER_READ_STATUS_ABORT ;
309 } /* sf_flac_read_callback */
311 static FLAC__StreamDecoderSeekStatus
312 sf_flac_seek_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__uint64 absolute_byte_offset, void *client_data)
313 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
315 psf_fseek (psf, absolute_byte_offset, SEEK_SET) ;
317 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR ;
319 return FLAC__STREAM_DECODER_SEEK_STATUS_OK ;
320 } /* sf_flac_seek_callback */
322 static FLAC__StreamDecoderTellStatus
323 sf_flac_tell_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__uint64 *absolute_byte_offset, void *client_data)
324 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
326 *absolute_byte_offset = psf_ftell (psf) ;
328 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR ;
330 return FLAC__STREAM_DECODER_TELL_STATUS_OK ;
331 } /* sf_flac_tell_callback */
333 static FLAC__StreamDecoderLengthStatus
334 sf_flac_length_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__uint64 *stream_length, void *client_data)
335 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
337 if ((*stream_length = psf->filelength) == 0)
338 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR ;
340 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK ;
341 } /* sf_flac_length_callback */
344 sf_flac_eof_callback (const FLAC__StreamDecoder *UNUSED (decoder), void *client_data)
345 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
347 if (psf_ftell (psf) == psf->filelength)
351 } /* sf_flac_eof_callback */
353 static FLAC__StreamDecoderWriteStatus
354 sf_flac_write_callback (const FLAC__StreamDecoder * UNUSED (decoder), const FLAC__Frame *frame, const FLAC__int32 * const buffer [], void *client_data)
355 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
356 FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
358 pflac->frame = frame ;
359 pflac->bufferpos = 0 ;
361 pflac->bufferbackup = SF_FALSE ;
362 pflac->wbuffer = buffer ;
364 flac_buffer_copy (psf) ;
366 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE ;
367 } /* sf_flac_write_callback */
370 sf_flac_meta_get_vorbiscomments (SF_PRIVATE *psf, const FLAC__StreamMetadata *metadata)
372 { { "title", SF_STR_TITLE },
373 { "copyright", SF_STR_COPYRIGHT },
374 { "software", SF_STR_SOFTWARE },
375 { "artist", SF_STR_ARTIST },
376 { "comment", SF_STR_COMMENT },
377 { "date", SF_STR_DATE },
378 { "album", SF_STR_ALBUM },
379 { "license", SF_STR_LICENSE },
380 { "tracknumber", SF_STR_TRACKNUMBER },
381 { "genre", SF_STR_GENRE }
384 const char *value, *cptr ;
387 for (k = 0 ; k < ARRAY_LEN (tags) ; k++)
388 { tag_num = FLAC__metadata_object_vorbiscomment_find_entry_from (metadata, 0, tags [k].tag) ;
393 value = (const char*) metadata->data.vorbis_comment.comments [tag_num].entry ;
394 if ((cptr = strchr (value, '=')) != NULL)
397 psf_log_printf (psf, " %-10s : %s\n", tags [k].tag, value) ;
398 psf_store_string (psf, tags [k].type, value) ;
402 } /* sf_flac_meta_get_vorbiscomments */
405 sf_flac_meta_callback (const FLAC__StreamDecoder * UNUSED (decoder), const FLAC__StreamMetadata *metadata, void *client_data)
406 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
409 switch (metadata->type)
410 { case FLAC__METADATA_TYPE_STREAMINFO :
411 psf->sf.channels = metadata->data.stream_info.channels ;
412 psf->sf.samplerate = metadata->data.stream_info.sample_rate ;
413 psf->sf.frames = metadata->data.stream_info.total_samples ;
415 psf_log_printf (psf, "FLAC Stream Metadata\n Channels : %d\n Sample rate : %d\n", psf->sf.channels, psf->sf.samplerate) ;
417 if (psf->sf.frames == 0)
418 { psf_log_printf (psf, " Frames : 0 (bumping to SF_COUNT_MAX)\n") ;
419 psf->sf.frames = SF_COUNT_MAX ;
422 psf_log_printf (psf, " Frames : %D\n", psf->sf.frames) ;
424 switch (metadata->data.stream_info.bits_per_sample)
426 psf->sf.format |= SF_FORMAT_PCM_S8 ;
430 psf->sf.format |= SF_FORMAT_PCM_16 ;
434 psf->sf.format |= SF_FORMAT_PCM_24 ;
438 psf_log_printf (psf, "sf_flac_meta_callback : bits_per_sample %d not yet implemented.\n", metadata->data.stream_info.bits_per_sample) ;
443 psf_log_printf (psf, " Bit width : %d\n", bitwidth) ;
446 case FLAC__METADATA_TYPE_VORBIS_COMMENT :
447 psf_log_printf (psf, "Vorbis Comment Metadata\n") ;
448 sf_flac_meta_get_vorbiscomments (psf, metadata) ;
451 case FLAC__METADATA_TYPE_PADDING :
452 psf_log_printf (psf, "Padding Metadata\n") ;
455 case FLAC__METADATA_TYPE_APPLICATION :
456 psf_log_printf (psf, "Application Metadata\n") ;
459 case FLAC__METADATA_TYPE_SEEKTABLE :
460 psf_log_printf (psf, "Seektable Metadata\n") ;
463 case FLAC__METADATA_TYPE_CUESHEET :
464 psf_log_printf (psf, "Cuesheet Metadata\n") ;
467 case FLAC__METADATA_TYPE_PICTURE :
468 psf_log_printf (psf, "Picture Metadata\n") ;
471 case FLAC__METADATA_TYPE_UNDEFINED :
472 psf_log_printf (psf, "Undefined Metadata\n") ;
476 psf_log_printf (psf, "sf_flac_meta_callback : metadata-type %d not yet implemented.\n", metadata->type) ;
481 } /* sf_flac_meta_callback */
484 sf_flac_error_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__StreamDecoderErrorStatus status, void *client_data)
485 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
487 psf_log_printf (psf, "ERROR : %s\n", FLAC__StreamDecoderErrorStatusString [status]) ;
490 { case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC :
491 psf->error = SFE_FLAC_LOST_SYNC ;
493 case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER :
494 psf->error = SFE_FLAC_BAD_HEADER ;
497 psf->error = SFE_FLAC_UNKOWN_ERROR ;
502 } /* sf_flac_error_callback */
504 static FLAC__StreamEncoderSeekStatus
505 sf_flac_enc_seek_callback (const FLAC__StreamEncoder * UNUSED (encoder), FLAC__uint64 absolute_byte_offset, void *client_data)
506 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
508 psf_fseek (psf, absolute_byte_offset, SEEK_SET) ;
510 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR ;
512 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK ;
513 } /* sf_flac_enc_seek_callback */
515 static FLAC__StreamEncoderTellStatus
516 sf_flac_enc_tell_callback (const FLAC__StreamEncoder *UNUSED (encoder), FLAC__uint64 *absolute_byte_offset, void *client_data)
517 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
519 *absolute_byte_offset = psf_ftell (psf) ;
521 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR ;
523 return FLAC__STREAM_ENCODER_TELL_STATUS_OK ;
524 } /* sf_flac_enc_tell_callback */
526 static FLAC__StreamEncoderWriteStatus
527 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)
528 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
530 if (psf_fwrite (buffer, 1, bytes, psf) == (sf_count_t) bytes && psf->error == 0)
531 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK ;
533 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR ;
534 } /* sf_flac_enc_write_callback */
537 flac_write_strings (SF_PRIVATE *psf, FLAC_PRIVATE* pflac)
538 { FLAC__StreamMetadata_VorbisComment_Entry entry ;
539 int k, string_count = 0 ;
541 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
542 { if (psf->strings.data [k].type != 0)
546 if (string_count == 0)
549 if (pflac->metadata == NULL && (pflac->metadata = FLAC__metadata_object_new (FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL)
550 { psf_log_printf (psf, "FLAC__metadata_object_new returned NULL\n") ;
554 for (k = 0 ; k < SF_MAX_STRINGS && psf->strings.data [k].type != 0 ; k++)
555 { const char * key, * value ;
557 switch (psf->strings.data [k].type)
558 { case SF_STR_SOFTWARE :
564 case SF_STR_COPYRIGHT :
570 case SF_STR_COMMENT :
579 case SF_STR_LICENSE :
582 case SF_STR_TRACKNUMBER :
583 key = "tracknumber" ;
592 value = psf->strings.storage + psf->strings.data [k].offset ;
594 FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair (&entry, key, value) ;
595 FLAC__metadata_object_vorbiscomment_append_comment (pflac->metadata, entry, /* copy */ SF_FALSE) ;
598 if (! FLAC__stream_encoder_set_metadata (pflac->fse, &pflac->metadata, 1))
599 { printf ("%s %d : fail\n", __func__, __LINE__) ;
604 } /* flac_write_strings */
607 flac_write_header (SF_PRIVATE *psf, int UNUSED (calc_length))
608 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
611 flac_write_strings (psf, pflac) ;
613 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)
614 { psf_log_printf (psf, "Error : FLAC encoder init returned error : %s\n", FLAC__StreamEncoderInitStatusString [err]) ;
615 return SFE_FLAC_INIT_DECODER ;
619 psf->dataoffset = psf_ftell (psf) ;
620 pflac->encbuffer = calloc (ENC_BUFFER_SIZE, sizeof (FLAC__int32)) ;
623 } /* flac_write_header */
625 /*------------------------------------------------------------------------------
630 flac_open (SF_PRIVATE *psf)
634 FLAC_PRIVATE* pflac = calloc (1, sizeof (FLAC_PRIVATE)) ;
635 psf->codec_data = pflac ;
637 /* Set the default value here. Over-ridden later if necessary. */
638 pflac->compression = FLAC_DEFAULT_COMPRESSION_LEVEL ;
641 if (psf->file.mode == SFM_RDWR)
642 return SFE_BAD_MODE_RW ;
644 if (psf->file.mode == SFM_READ)
645 { if ((error = flac_read_header (psf)))
649 subformat = SF_CODEC (psf->sf.format) ;
651 if (psf->file.mode == SFM_WRITE)
652 { if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_FLAC)
653 return SFE_BAD_OPEN_FORMAT ;
655 psf->endian = SF_ENDIAN_BIG ;
656 psf->sf.seekable = 0 ;
658 psf->strings.flags = SF_STR_ALLOW_START ;
660 if ((error = flac_enc_init (psf)))
663 psf->write_header = flac_write_header ;
666 psf->datalength = psf->filelength ;
667 psf->dataoffset = 0 ;
669 psf->container_close = flac_close ;
670 psf->seek = flac_seek ;
671 psf->byterate = flac_byterate ;
673 psf->command = flac_command ;
676 { case SF_FORMAT_PCM_S8 : /* 8-bit FLAC. */
677 case SF_FORMAT_PCM_16 : /* 16-bit FLAC. */
678 case SF_FORMAT_PCM_24 : /* 24-bit FLAC. */
679 error = flac_init (psf) ;
682 default : return SFE_UNIMPLEMENTED ;
688 /*------------------------------------------------------------------------------
692 flac_close (SF_PRIVATE *psf)
693 { FLAC_PRIVATE* pflac ;
696 if ((pflac = (FLAC_PRIVATE*) psf->codec_data) == NULL)
699 if (pflac->metadata != NULL)
700 FLAC__metadata_object_delete (pflac->metadata) ;
702 if (psf->file.mode == SFM_WRITE)
703 { FLAC__stream_encoder_finish (pflac->fse) ;
704 FLAC__stream_encoder_delete (pflac->fse) ;
706 if (pflac->encbuffer)
707 free (pflac->encbuffer) ;
710 if (psf->file.mode == SFM_READ)
711 { FLAC__stream_decoder_finish (pflac->fsd) ;
712 FLAC__stream_decoder_delete (pflac->fsd) ;
715 for (k = 0 ; k < ARRAY_LEN (pflac->rbuffer) ; k++)
716 free (pflac->rbuffer [k]) ;
719 psf->codec_data = NULL ;
725 flac_enc_init (SF_PRIVATE *psf)
726 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
729 /* To cite the flac FAQ at
730 ** http://flac.sourceforge.net/faq.html#general__samples
731 ** "FLAC supports linear sample rates from 1Hz - 655350Hz in 1Hz
734 if (psf->sf.samplerate < 1 || psf->sf.samplerate > 655350)
735 { psf_log_printf (psf, "flac sample rate out of range.\n", psf->sf.samplerate) ;
736 return SFE_FLAC_BAD_SAMPLE_RATE ;
739 psf_fseek (psf, 0, SEEK_SET) ;
741 switch (SF_CODEC (psf->sf.format))
742 { case SF_FORMAT_PCM_S8 :
745 case SF_FORMAT_PCM_16 :
748 case SF_FORMAT_PCM_24 :
758 FLAC__stream_encoder_delete (pflac->fse) ;
759 if ((pflac->fse = FLAC__stream_encoder_new ()) == NULL)
760 return SFE_FLAC_NEW_DECODER ;
762 if (! FLAC__stream_encoder_set_channels (pflac->fse, psf->sf.channels))
763 { psf_log_printf (psf, "FLAC__stream_encoder_set_channels (%d) return false.\n", psf->sf.channels) ;
764 return SFE_FLAC_INIT_DECODER ;
767 if (! FLAC__stream_encoder_set_sample_rate (pflac->fse, psf->sf.samplerate))
768 { psf_log_printf (psf, "FLAC__stream_encoder_set_sample_rate (%d) returned false.\n", psf->sf.samplerate) ;
769 return SFE_FLAC_BAD_SAMPLE_RATE ;
772 if (! FLAC__stream_encoder_set_bits_per_sample (pflac->fse, bps))
773 { psf_log_printf (psf, "FLAC__stream_encoder_set_bits_per_sample (%d) return false.\n", bps) ;
774 return SFE_FLAC_INIT_DECODER ;
777 if (! FLAC__stream_encoder_set_compression_level (pflac->fse, pflac->compression))
778 { psf_log_printf (psf, "FLAC__stream_encoder_set_compression_level (%d) return false.\n", pflac->compression) ;
779 return SFE_FLAC_INIT_DECODER ;
783 } /* flac_enc_init */
786 flac_read_header (SF_PRIVATE *psf)
787 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
789 psf_fseek (psf, 0, SEEK_SET) ;
791 FLAC__stream_decoder_delete (pflac->fsd) ;
792 if ((pflac->fsd = FLAC__stream_decoder_new ()) == NULL)
793 return SFE_FLAC_NEW_DECODER ;
795 FLAC__stream_decoder_set_metadata_respond_all (pflac->fsd) ;
797 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)
798 return SFE_FLAC_INIT_DECODER ;
800 FLAC__stream_decoder_process_until_end_of_metadata (pflac->fsd) ;
802 psf_log_printf (psf, "End\n") ;
805 { FLAC__uint64 position ;
807 FLAC__stream_decoder_get_decode_position (pflac->fsd, &position) ;
808 psf->dataoffset = position ;
812 } /* flac_read_header */
815 flac_command (SF_PRIVATE * psf, int command, void * data, int datasize)
816 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
820 { case SFC_SET_COMPRESSION_LEVEL :
821 if (data == NULL || datasize != sizeof (double))
824 if (psf->have_written)
827 /* FLAC compression level is in the range [0, 8] while libsndfile takes
828 ** values in the range [0.0, 1.0]. Massage the libsndfile value here.
830 quality = (*((double *) data)) * 8.0 ;
832 pflac->compression = lrint (SF_MAX (0.0, SF_MIN (8.0, quality))) ;
834 psf_log_printf (psf, "%s : Setting SFC_SET_COMPRESSION_LEVEL to %u.\n", __func__, pflac->compression) ;
836 if (flac_enc_init (psf))
849 flac_init (SF_PRIVATE *psf)
851 if (psf->file.mode == SFM_RDWR)
852 return SFE_BAD_MODE_RW ;
854 if (psf->file.mode == SFM_READ)
855 { psf->read_short = flac_read_flac2s ;
856 psf->read_int = flac_read_flac2i ;
857 psf->read_float = flac_read_flac2f ;
858 psf->read_double = flac_read_flac2d ;
861 if (psf->file.mode == SFM_WRITE)
862 { psf->write_short = flac_write_s2flac ;
863 psf->write_int = flac_write_i2flac ;
864 psf->write_float = flac_write_f2flac ;
865 psf->write_double = flac_write_d2flac ;
868 if (psf->filelength > psf->dataoffset)
869 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset : psf->filelength - psf->dataoffset ;
871 psf->datalength = 0 ;
877 flac_read_loop (SF_PRIVATE *psf, unsigned len)
878 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
882 pflac->remain = len ;
883 if (pflac->frame != NULL && pflac->bufferpos < pflac->frame->header.blocksize)
884 flac_buffer_copy (psf) ;
886 while (pflac->pos < pflac->len)
887 { if (FLAC__stream_decoder_process_single (pflac->fsd) == 0)
889 if (FLAC__stream_decoder_get_state (pflac->fsd) >= FLAC__STREAM_DECODER_END_OF_STREAM)
896 } /* flac_read_loop */
899 flac_read_flac2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
900 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
901 sf_count_t total = 0, current ;
904 pflac->pcmtype = PFLAC_PCM_SHORT ;
907 { pflac->ptr = ptr + total ;
908 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
909 current = flac_read_loop (psf, readlen) ;
916 } /* flac_read_flac2s */
919 flac_read_flac2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
920 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
921 sf_count_t total = 0, current ;
924 pflac->pcmtype = PFLAC_PCM_INT ;
927 { pflac->ptr = ptr + total ;
928 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
929 current = flac_read_loop (psf, readlen) ;
936 } /* flac_read_flac2i */
939 flac_read_flac2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
940 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
941 sf_count_t total = 0, current ;
944 pflac->pcmtype = PFLAC_PCM_FLOAT ;
947 { pflac->ptr = ptr + total ;
948 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
949 current = flac_read_loop (psf, readlen) ;
956 } /* flac_read_flac2f */
959 flac_read_flac2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
960 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
961 sf_count_t total = 0, current ;
964 pflac->pcmtype = PFLAC_PCM_DOUBLE ;
967 { pflac->ptr = ptr + total ;
968 readlen = (len - total > 0x1000000) ? 0x1000000 : (unsigned) (len - total) ;
969 current = flac_read_loop (psf, readlen) ;
976 } /* flac_read_flac2d */
979 flac_write_s2flac (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
980 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
981 void (*convert) (const short *, FLAC__int32 *, int) ;
982 int bufferlen, writecount, thiswrite ;
983 sf_count_t total = 0 ;
984 FLAC__int32* buffer = pflac->encbuffer ;
986 switch (SF_CODEC (psf->sf.format))
987 { case SF_FORMAT_PCM_S8 :
988 convert = s2flac8_array ;
990 case SF_FORMAT_PCM_16 :
991 convert = s2flac16_array ;
993 case SF_FORMAT_PCM_24 :
994 convert = s2flac24_array ;
1000 bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
1001 bufferlen *= psf->sf.channels ;
1004 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1005 convert (ptr + total, buffer, writecount) ;
1006 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount / psf->sf.channels))
1007 thiswrite = writecount ;
1010 total += thiswrite ;
1011 if (thiswrite < writecount)
1018 } /* flac_write_s2flac */
1021 flac_write_i2flac (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
1022 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
1023 void (*convert) (const int *, FLAC__int32 *, int) ;
1024 int bufferlen, writecount, thiswrite ;
1025 sf_count_t total = 0 ;
1026 FLAC__int32* buffer = pflac->encbuffer ;
1028 switch (SF_CODEC (psf->sf.format))
1029 { case SF_FORMAT_PCM_S8 :
1030 convert = i2flac8_array ;
1032 case SF_FORMAT_PCM_16 :
1033 convert = i2flac16_array ;
1035 case SF_FORMAT_PCM_24 :
1036 convert = i2flac24_array ;
1042 bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
1043 bufferlen *= psf->sf.channels ;
1046 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1047 convert (ptr + total, buffer, writecount) ;
1048 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount / psf->sf.channels))
1049 thiswrite = writecount ;
1052 total += thiswrite ;
1053 if (thiswrite < writecount)
1060 } /* flac_write_i2flac */
1063 flac_write_f2flac (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
1064 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
1065 void (*convert) (const float *, FLAC__int32 *, int, int) ;
1066 int bufferlen, writecount, thiswrite ;
1067 sf_count_t total = 0 ;
1068 FLAC__int32* buffer = pflac->encbuffer ;
1070 switch (SF_CODEC (psf->sf.format))
1071 { case SF_FORMAT_PCM_S8 :
1072 convert = (psf->add_clipping) ? f2flac8_clip_array : f2flac8_array ;
1074 case SF_FORMAT_PCM_16 :
1075 convert = (psf->add_clipping) ? f2flac16_clip_array : f2flac16_array ;
1077 case SF_FORMAT_PCM_24 :
1078 convert = (psf->add_clipping) ? f2flac24_clip_array : f2flac24_array ;
1084 bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
1085 bufferlen *= psf->sf.channels ;
1088 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1089 convert (ptr + total, buffer, writecount, psf->norm_float) ;
1090 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount / psf->sf.channels))
1091 thiswrite = writecount ;
1094 total += thiswrite ;
1095 if (thiswrite < writecount)
1102 } /* flac_write_f2flac */
1105 f2flac8_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1106 { float normfact, scaled_value ;
1108 normfact = normalize ? (8.0 * 0x10) : 1.0 ;
1110 while (--count >= 0)
1111 { scaled_value = src [count] * normfact ;
1112 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7F))
1113 { dest [count] = 0x7F ;
1116 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10))
1117 { dest [count] = 0x80 ;
1120 dest [count] = lrintf (scaled_value) ;
1124 } /* f2flac8_clip_array */
1127 f2flac16_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1128 { float normfact, scaled_value ;
1130 normfact = normalize ? (8.0 * 0x1000) : 1.0 ;
1132 while (--count >= 0)
1133 { scaled_value = src [count] * normfact ;
1134 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFF))
1135 { dest [count] = 0x7FFF ;
1138 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000))
1139 { dest [count] = 0x8000 ;
1142 dest [count] = lrintf (scaled_value) ;
1144 } /* f2flac16_clip_array */
1147 f2flac24_clip_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1148 { float normfact, scaled_value ;
1150 normfact = normalize ? (8.0 * 0x100000) : 1.0 ;
1152 while (--count >= 0)
1153 { scaled_value = src [count] * normfact ;
1154 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFF))
1155 { dest [count] = 0x7FFFFF ;
1159 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x100000))
1160 { dest [count] = 0x800000 ;
1163 dest [count] = lrintf (scaled_value) ;
1167 } /* f2flac24_clip_array */
1170 f2flac8_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1171 { float normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
1173 while (--count >= 0)
1174 dest [count] = lrintf (src [count] * normfact) ;
1175 } /* f2flac8_array */
1178 f2flac16_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1179 { float normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
1181 while (--count >= 0)
1182 dest [count] = lrintf (src [count] * normfact) ;
1183 } /* f2flac16_array */
1186 f2flac24_array (const float *src, FLAC__int32 *dest, int count, int normalize)
1187 { float normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
1189 while (--count >= 0)
1190 dest [count] = lrintf (src [count] * normfact) ;
1191 } /* f2flac24_array */
1194 flac_write_d2flac (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
1195 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
1196 void (*convert) (const double *, FLAC__int32 *, int, int) ;
1197 int bufferlen, writecount, thiswrite ;
1198 sf_count_t total = 0 ;
1199 FLAC__int32* buffer = pflac->encbuffer ;
1201 switch (SF_CODEC (psf->sf.format))
1202 { case SF_FORMAT_PCM_S8 :
1203 convert = (psf->add_clipping) ? d2flac8_clip_array : d2flac8_array ;
1205 case SF_FORMAT_PCM_16 :
1206 convert = (psf->add_clipping) ? d2flac16_clip_array : d2flac16_array ;
1208 case SF_FORMAT_PCM_24 :
1209 convert = (psf->add_clipping) ? d2flac24_clip_array : d2flac24_array ;
1215 bufferlen = ENC_BUFFER_SIZE / (sizeof (FLAC__int32) * psf->sf.channels) ;
1216 bufferlen *= psf->sf.channels ;
1219 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1220 convert (ptr + total, buffer, writecount, psf->norm_double) ;
1221 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount / psf->sf.channels))
1222 thiswrite = writecount ;
1225 total += thiswrite ;
1226 if (thiswrite < writecount)
1233 } /* flac_write_d2flac */
1236 d2flac8_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1237 { double normfact, scaled_value ;
1239 normfact = normalize ? (8.0 * 0x10) : 1.0 ;
1241 while (--count >= 0)
1242 { scaled_value = src [count] * normfact ;
1243 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7F))
1244 { dest [count] = 0x7F ;
1247 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10))
1248 { dest [count] = 0x80 ;
1251 dest [count] = lrint (scaled_value) ;
1255 } /* d2flac8_clip_array */
1258 d2flac16_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1259 { double normfact, scaled_value ;
1261 normfact = normalize ? (8.0 * 0x1000) : 1.0 ;
1263 while (--count >= 0)
1264 { scaled_value = src [count] * normfact ;
1265 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFF))
1266 { dest [count] = 0x7FFF ;
1269 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x1000))
1270 { dest [count] = 0x8000 ;
1273 dest [count] = lrint (scaled_value) ;
1277 } /* d2flac16_clip_array */
1280 d2flac24_clip_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1281 { double normfact, scaled_value ;
1283 normfact = normalize ? (8.0 * 0x100000) : 1.0 ;
1285 while (--count >= 0)
1286 { scaled_value = src [count] * normfact ;
1287 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFF))
1288 { dest [count] = 0x7FFFFF ;
1291 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x100000))
1292 { dest [count] = 0x800000 ;
1295 dest [count] = lrint (scaled_value) ;
1299 } /* d2flac24_clip_array */
1302 d2flac8_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1303 { double normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
1305 while (--count >= 0)
1306 dest [count] = lrint (src [count] * normfact) ;
1307 } /* d2flac8_array */
1310 d2flac16_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1311 { double normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
1313 while (--count >= 0)
1314 dest [count] = lrint (src [count] * normfact) ;
1315 } /* d2flac16_array */
1318 d2flac24_array (const double *src, FLAC__int32 *dest, int count, int normalize)
1319 { double normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
1321 while (--count >= 0)
1322 dest [count] = lrint (src [count] * normfact) ;
1323 } /* d2flac24_array */
1326 flac_seek (SF_PRIVATE *psf, int UNUSED (mode), sf_count_t offset)
1327 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
1332 if (psf->dataoffset < 0)
1333 { psf->error = SFE_BAD_SEEK ;
1334 return ((sf_count_t) -1) ;
1337 pflac->frame = NULL ;
1339 if (psf->file.mode == SFM_READ)
1340 { if (FLAC__stream_decoder_seek_absolute (pflac->fsd, offset))
1343 if (offset == psf->sf.frames)
1345 ** If we've been asked to seek to the very end of the file, libFLAC
1346 ** will return an error. However, we know the length of the file so
1347 ** instead of returning an error, we can return the offset.
1352 psf->error = SFE_BAD_SEEK ;
1353 return ((sf_count_t) -1) ;
1356 /* Seeking in write mode not yet supported. */
1357 psf->error = SFE_BAD_SEEK ;
1359 return ((sf_count_t) -1) ;
1363 flac_byterate (SF_PRIVATE *psf)
1365 if (psf->file.mode == SFM_READ)
1366 return (psf->datalength * psf->sf.samplerate) / psf->sf.frames ;
1369 } /* flac_byterate */
1372 #else /* HAVE_EXTERNAL_LIBS */
1375 flac_open (SF_PRIVATE *psf)
1377 psf_log_printf (psf, "This version of libsndfile was compiled without FLAC support.\n") ;
1378 return SFE_UNIMPLEMENTED ;