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 0 /* HAVE_EXTERNAL_XIPH_LIBS : Disable for TIZEN */
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 ** READ_LOOP_MAX_LEN is the maximum 'len' that will be passed to
48 ** flac_read_loop(). This is somewhat arbitrary, but must be less
49 ** than (UINT_MAX - FLAC__MAX_CHANNELS * FLAC__MAX_BLOCK_SIZE) to
50 ** avoid overflows, and must also be a multiple of the number of
51 ** channels (which is between 1 and 8.)
53 #define READ_LOOP_MAX_LEN (0x10000 * 3 * 5 * 7)
56 { PFLAC_PCM_SHORT = 50,
64 FLAC__StreamDecoder *fsd ;
65 FLAC__StreamEncoder *fse ;
69 unsigned pos, len, remain ;
71 FLAC__StreamMetadata *metadata ;
73 const int32_t * const * wbuffer ;
74 int32_t * rbuffer [FLAC__MAX_CHANNELS] ;
79 const FLAC__Frame *frame ;
81 unsigned compression ;
90 static sf_count_t flac_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
91 static int flac_byterate (SF_PRIVATE *psf) ;
92 static int flac_close (SF_PRIVATE *psf) ;
94 static int flac_enc_init (SF_PRIVATE *psf) ;
95 static int flac_read_header (SF_PRIVATE *psf) ;
97 static sf_count_t flac_read_flac2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
98 static sf_count_t flac_read_flac2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
99 static sf_count_t flac_read_flac2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
100 static sf_count_t flac_read_flac2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
102 static sf_count_t flac_write_s2flac (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
103 static sf_count_t flac_write_i2flac (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
104 static sf_count_t flac_write_f2flac (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
105 static sf_count_t flac_write_d2flac (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
107 static void f2flac8_array (const float *src, int32_t *dest, int count, int normalize) ;
108 static void f2flac16_array (const float *src, int32_t *dest, int count, int normalize) ;
109 static void f2flac24_array (const float *src, int32_t *dest, int count, int normalize) ;
110 static void f2flac8_clip_array (const float *src, int32_t *dest, int count, int normalize) ;
111 static void f2flac16_clip_array (const float *src, int32_t *dest, int count, int normalize) ;
112 static void f2flac24_clip_array (const float *src, int32_t *dest, int count, int normalize) ;
113 static void d2flac8_array (const double *src, int32_t *dest, int count, int normalize) ;
114 static void d2flac16_array (const double *src, int32_t *dest, int count, int normalize) ;
115 static void d2flac24_array (const double *src, int32_t *dest, int count, int normalize) ;
116 static void d2flac8_clip_array (const double *src, int32_t *dest, int count, int normalize) ;
117 static void d2flac16_clip_array (const double *src, int32_t *dest, int count, int normalize) ;
118 static void d2flac24_clip_array (const double *src, int32_t *dest, int count, int normalize) ;
120 static int flac_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
122 /* Decoder Callbacks */
123 static FLAC__StreamDecoderReadStatus sf_flac_read_callback (const FLAC__StreamDecoder *decoder, FLAC__byte buffer [], size_t *bytes, void *client_data) ;
124 static FLAC__StreamDecoderSeekStatus sf_flac_seek_callback (const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data) ;
125 static FLAC__StreamDecoderTellStatus sf_flac_tell_callback (const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) ;
126 static FLAC__StreamDecoderLengthStatus sf_flac_length_callback (const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data) ;
127 static FLAC__bool sf_flac_eof_callback (const FLAC__StreamDecoder *decoder, void *client_data) ;
128 static FLAC__StreamDecoderWriteStatus sf_flac_write_callback (const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32_t * const buffer [], void *client_data) ;
129 static void sf_flac_meta_callback (const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) ;
130 static void sf_flac_error_callback (const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) ;
132 /* Encoder Callbacks */
133 static FLAC__StreamEncoderSeekStatus sf_flac_enc_seek_callback (const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data) ;
134 static FLAC__StreamEncoderTellStatus sf_flac_enc_tell_callback (const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data) ;
135 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) ;
138 s2flac8_array (const short *src, int32_t *dest, int count)
139 { for (int i = 0 ; i < count ; i++)
140 dest [i] = src [i] >> 8 ;
141 } /* s2flac8_array */
144 s2flac16_array (const short *src, int32_t *dest, int count)
145 { for (int i = 0 ; i < count ; i++)
147 } /* s2flac16_array */
150 s2flac24_array (const short *src, int32_t *dest, int count)
151 { for (int i = 0 ; i < count ; i++)
152 dest [i] = src [i] << 8 ;
153 } /* s2flac24_array */
156 i2flac8_array (const int *src, int32_t *dest, int count)
157 { for (int i = 0 ; i < count ; i++)
158 dest [i] = src [i] >> 24 ;
159 } /* i2flac8_array */
162 i2flac16_array (const int *src, int32_t *dest, int count)
164 for (int i = 0 ; i < count ; i++)
165 dest [i] = src [i] >> 16 ;
166 } /* i2flac16_array */
169 i2flac24_array (const int *src, int32_t *dest, int count)
170 { for (int i = 0 ; i < count ; i++)
171 dest [i] = src [i] >> 8 ;
172 } /* i2flac24_array */
175 flac_buffer_copy (SF_PRIVATE *psf)
176 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
177 const FLAC__Frame *frame = pflac->frame ;
178 const int32_t* const *buffer = pflac->wbuffer ;
179 unsigned i = 0, j, offset, channels, len ;
181 if (psf->sf.channels != (int) frame->header.channels)
182 { psf_log_printf (psf, "Error: FLAC frame changed from %d to %d channels\n"
183 "Nothing to do but to error out.\n" ,
184 psf->sf.channels, frame->header.channels) ;
185 psf->error = SFE_FLAC_CHANNEL_COUNT_CHANGED ;
190 ** frame->header.blocksize is variable and we're using a constant blocksize
191 ** of FLAC__MAX_BLOCK_SIZE.
192 ** Check our assumptions here.
194 if (frame->header.blocksize > FLAC__MAX_BLOCK_SIZE)
195 { psf_log_printf (psf, "Ooops : frame->header.blocksize (%d) > FLAC__MAX_BLOCK_SIZE (%d)\n", __func__, __LINE__, frame->header.blocksize, FLAC__MAX_BLOCK_SIZE) ;
196 psf->error = SFE_INTERNAL ;
200 if (frame->header.channels > FLAC__MAX_CHANNELS)
201 psf_log_printf (psf, "Ooops : frame->header.channels (%d) > FLAC__MAX_BLOCK_SIZE (%d)\n", __func__, __LINE__, frame->header.channels, FLAC__MAX_CHANNELS) ;
203 channels = SF_MIN (frame->header.channels, FLAC__MAX_CHANNELS) ;
205 if (pflac->ptr == NULL)
207 ** This pointer is reset to NULL each time the current frame has been
208 ** decoded. Somehow its used during encoding and decoding.
210 for (i = 0 ; i < channels ; i++)
212 if (pflac->rbuffer [i] == NULL)
213 pflac->rbuffer [i] = calloc (FLAC__MAX_BLOCK_SIZE, sizeof (int32_t)) ;
215 memcpy (pflac->rbuffer [i], buffer [i], frame->header.blocksize * sizeof (int32_t)) ;
217 pflac->wbuffer = (const int32_t* const*) pflac->rbuffer ;
222 len = SF_MIN (pflac->len, frame->header.blocksize) ;
224 if (pflac->remain % channels != 0)
225 { psf_log_printf (psf, "Error: pflac->remain %u channels %u\n", pflac->remain, channels) ;
229 switch (pflac->pcmtype)
230 { case PFLAC_PCM_SHORT :
231 { short *retpcm = (short*) pflac->ptr ;
232 int shift = 16 - frame->header.bits_per_sample ;
234 { shift = abs (shift) ;
235 for (i = 0 ; i < len && pflac->remain > 0 ; i++)
236 { offset = pflac->pos + i * channels ;
238 if (pflac->bufferpos >= frame->header.blocksize)
241 if (offset + channels > pflac->len)
244 for (j = 0 ; j < channels ; j++)
245 retpcm [offset + j] = buffer [j][pflac->bufferpos] >> shift ;
246 pflac->remain -= channels ;
247 pflac->bufferpos ++ ;
251 { for (i = 0 ; i < len && pflac->remain > 0 ; i++)
252 { offset = pflac->pos + i * channels ;
254 if (pflac->bufferpos >= frame->header.blocksize)
257 if (offset + channels > pflac->len)
260 for (j = 0 ; j < channels ; j++)
261 retpcm [offset + j] = ((uint16_t) buffer [j][pflac->bufferpos]) << shift ;
263 pflac->remain -= channels ;
264 pflac->bufferpos ++ ;
271 { int *retpcm = (int*) pflac->ptr ;
272 int shift = 32 - frame->header.bits_per_sample ;
273 for (i = 0 ; i < len && pflac->remain > 0 ; i++)
274 { offset = pflac->pos + i * channels ;
276 if (pflac->bufferpos >= frame->header.blocksize)
279 if (offset + channels > pflac->len)
282 for (j = 0 ; j < channels ; j++)
283 retpcm [offset + j] = ((uint32_t) buffer [j][pflac->bufferpos]) << shift ;
284 pflac->remain -= channels ;
290 case PFLAC_PCM_FLOAT :
291 { float *retpcm = (float*) pflac->ptr ;
292 float norm = (psf->norm_float == SF_TRUE) ? 1.0 / (1 << (frame->header.bits_per_sample - 1)) : 1.0 ;
294 for (i = 0 ; i < len && pflac->remain > 0 ; i++)
295 { offset = pflac->pos + i * channels ;
297 if (pflac->bufferpos >= frame->header.blocksize)
300 if (offset + channels > pflac->len)
303 for (j = 0 ; j < channels ; j++)
304 retpcm [offset + j] = buffer [j][pflac->bufferpos] * norm ;
305 pflac->remain -= channels ;
311 case PFLAC_PCM_DOUBLE :
312 { double *retpcm = (double*) pflac->ptr ;
313 double norm = (psf->norm_double == SF_TRUE) ? 1.0 / (1 << (frame->header.bits_per_sample - 1)) : 1.0 ;
315 for (i = 0 ; i < len && pflac->remain > 0 ; i++)
316 { offset = pflac->pos + i * channels ;
318 if (pflac->bufferpos >= frame->header.blocksize)
321 if (offset + channels > pflac->len)
324 for (j = 0 ; j < channels ; j++)
325 retpcm [offset + j] = buffer [j][pflac->bufferpos] * norm ;
326 pflac->remain -= channels ;
336 offset = i * channels ;
337 pflac->pos += i * channels ;
340 } /* flac_buffer_copy */
343 static FLAC__StreamDecoderReadStatus
344 sf_flac_read_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__byte buffer [], size_t *bytes, void *client_data)
345 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
347 *bytes = psf_fread (buffer, 1, *bytes, psf) ;
348 if (*bytes > 0 && psf->error == 0)
349 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE ;
351 return FLAC__STREAM_DECODER_READ_STATUS_ABORT ;
352 } /* sf_flac_read_callback */
354 static FLAC__StreamDecoderSeekStatus
355 sf_flac_seek_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__uint64 absolute_byte_offset, void *client_data)
356 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
358 psf_fseek (psf, absolute_byte_offset, SEEK_SET) ;
360 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR ;
362 return FLAC__STREAM_DECODER_SEEK_STATUS_OK ;
363 } /* sf_flac_seek_callback */
365 static FLAC__StreamDecoderTellStatus
366 sf_flac_tell_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__uint64 *absolute_byte_offset, void *client_data)
367 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
369 *absolute_byte_offset = psf_ftell (psf) ;
371 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR ;
373 return FLAC__STREAM_DECODER_TELL_STATUS_OK ;
374 } /* sf_flac_tell_callback */
376 static FLAC__StreamDecoderLengthStatus
377 sf_flac_length_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__uint64 *stream_length, void *client_data)
378 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
380 if ((*stream_length = psf->filelength) == 0)
381 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR ;
383 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK ;
384 } /* sf_flac_length_callback */
387 sf_flac_eof_callback (const FLAC__StreamDecoder *UNUSED (decoder), void *client_data)
388 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
390 if (psf_ftell (psf) == psf->filelength)
394 } /* sf_flac_eof_callback */
396 static FLAC__StreamDecoderWriteStatus
397 sf_flac_write_callback (const FLAC__StreamDecoder * UNUSED (decoder), const FLAC__Frame *frame, const int32_t * const buffer [], void *client_data)
398 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
399 FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
401 pflac->frame = frame ;
402 pflac->bufferpos = 0 ;
404 pflac->wbuffer = buffer ;
406 flac_buffer_copy (psf) ;
408 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE ;
409 } /* sf_flac_write_callback */
412 sf_flac_meta_get_vorbiscomments (SF_PRIVATE *psf, const FLAC__StreamMetadata *metadata)
413 { static FLAC_TAG tags [] =
414 { { "title", SF_STR_TITLE },
415 { "copyright", SF_STR_COPYRIGHT },
416 { "software", SF_STR_SOFTWARE },
417 { "artist", SF_STR_ARTIST },
418 { "comment", SF_STR_COMMENT },
419 { "date", SF_STR_DATE },
420 { "album", SF_STR_ALBUM },
421 { "license", SF_STR_LICENSE },
422 { "tracknumber", SF_STR_TRACKNUMBER },
423 { "genre", SF_STR_GENRE }
426 const char *value, *cptr ;
429 for (k = 0 ; k < ARRAY_LEN (tags) ; k++)
430 { tag_num = FLAC__metadata_object_vorbiscomment_find_entry_from (metadata, 0, tags [k].tag) ;
435 value = (const char*) metadata->data.vorbis_comment.comments [tag_num].entry ;
436 if ((cptr = strchr (value, '=')) != NULL)
439 psf_log_printf (psf, " %-12s : %s\n", tags [k].tag, value) ;
440 psf_store_string (psf, tags [k].type, value) ;
444 } /* sf_flac_meta_get_vorbiscomments */
447 sf_flac_meta_callback (const FLAC__StreamDecoder * UNUSED (decoder), const FLAC__StreamMetadata *metadata, void *client_data)
448 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
451 switch (metadata->type)
452 { case FLAC__METADATA_TYPE_STREAMINFO :
453 if (psf->sf.channels > 0 && psf->sf.channels != (int) metadata->data.stream_info.channels)
454 { psf_log_printf (psf, "Error: FLAC stream changed from %d to %d channels\n"
455 "Nothing to do but to error out.\n" ,
456 psf->sf.channels, metadata->data.stream_info.channels) ;
457 psf->error = SFE_FLAC_CHANNEL_COUNT_CHANGED ;
461 if (psf->sf.channels > 0 && psf->sf.samplerate != (int) metadata->data.stream_info.sample_rate)
462 { psf_log_printf (psf, "Warning: FLAC stream changed sample rates from %d to %d.\n"
463 "Carrying on as if nothing happened.",
464 psf->sf.samplerate, metadata->data.stream_info.sample_rate) ;
466 psf->sf.channels = metadata->data.stream_info.channels ;
467 psf->sf.samplerate = metadata->data.stream_info.sample_rate ;
468 psf->sf.frames = metadata->data.stream_info.total_samples ;
470 psf_log_printf (psf, "FLAC Stream Metadata\n Channels : %d\n Sample rate : %d\n", psf->sf.channels, psf->sf.samplerate) ;
472 if (psf->sf.frames == 0)
473 { psf_log_printf (psf, " Frames : 0 (bumping to SF_COUNT_MAX)\n") ;
474 psf->sf.frames = SF_COUNT_MAX ;
477 psf_log_printf (psf, " Frames : %D\n", psf->sf.frames) ;
479 switch (metadata->data.stream_info.bits_per_sample)
481 psf->sf.format |= SF_FORMAT_PCM_S8 ;
485 psf->sf.format |= SF_FORMAT_PCM_16 ;
489 psf->sf.format |= SF_FORMAT_PCM_24 ;
493 psf_log_printf (psf, "sf_flac_meta_callback : bits_per_sample %d not yet implemented.\n", metadata->data.stream_info.bits_per_sample) ;
498 psf_log_printf (psf, " Bit width : %d\n", bitwidth) ;
501 case FLAC__METADATA_TYPE_VORBIS_COMMENT :
502 psf_log_printf (psf, "Vorbis Comment Metadata\n") ;
503 sf_flac_meta_get_vorbiscomments (psf, metadata) ;
506 case FLAC__METADATA_TYPE_PADDING :
507 psf_log_printf (psf, "Padding Metadata\n") ;
510 case FLAC__METADATA_TYPE_APPLICATION :
511 psf_log_printf (psf, "Application Metadata\n") ;
514 case FLAC__METADATA_TYPE_SEEKTABLE :
515 psf_log_printf (psf, "Seektable Metadata\n") ;
518 case FLAC__METADATA_TYPE_CUESHEET :
519 psf_log_printf (psf, "Cuesheet Metadata\n") ;
522 case FLAC__METADATA_TYPE_PICTURE :
523 psf_log_printf (psf, "Picture Metadata\n") ;
526 case FLAC__METADATA_TYPE_UNDEFINED :
527 psf_log_printf (psf, "Undefined Metadata\n") ;
531 psf_log_printf (psf, "sf_flac_meta_callback : metadata-type %d not yet implemented.\n", metadata->type) ;
536 } /* sf_flac_meta_callback */
539 sf_flac_error_callback (const FLAC__StreamDecoder * UNUSED (decoder), FLAC__StreamDecoderErrorStatus status, void *client_data)
540 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
542 psf_log_printf (psf, "ERROR : %s\n", FLAC__StreamDecoderErrorStatusString [status]) ;
545 { case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC :
546 psf->error = SFE_FLAC_LOST_SYNC ;
548 case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER :
549 psf->error = SFE_FLAC_BAD_HEADER ;
552 psf->error = SFE_FLAC_UNKOWN_ERROR ;
557 } /* sf_flac_error_callback */
559 static FLAC__StreamEncoderSeekStatus
560 sf_flac_enc_seek_callback (const FLAC__StreamEncoder * UNUSED (encoder), FLAC__uint64 absolute_byte_offset, void *client_data)
561 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
563 psf_fseek (psf, absolute_byte_offset, SEEK_SET) ;
565 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR ;
567 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK ;
568 } /* sf_flac_enc_seek_callback */
570 static FLAC__StreamEncoderTellStatus
571 sf_flac_enc_tell_callback (const FLAC__StreamEncoder *UNUSED (encoder), FLAC__uint64 *absolute_byte_offset, void *client_data)
572 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
574 *absolute_byte_offset = psf_ftell (psf) ;
576 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR ;
578 return FLAC__STREAM_ENCODER_TELL_STATUS_OK ;
579 } /* sf_flac_enc_tell_callback */
581 static FLAC__StreamEncoderWriteStatus
582 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)
583 { SF_PRIVATE *psf = (SF_PRIVATE*) client_data ;
585 if (psf_fwrite (buffer, 1, bytes, psf) == (sf_count_t) bytes && psf->error == 0)
586 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK ;
588 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR ;
589 } /* sf_flac_enc_write_callback */
592 flac_write_strings (SF_PRIVATE *psf, FLAC_PRIVATE* pflac)
593 { FLAC__StreamMetadata_VorbisComment_Entry entry ;
594 int k, string_count = 0 ;
596 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
597 { if (psf->strings.data [k].type != 0)
601 if (string_count == 0)
604 if (pflac->metadata == NULL && (pflac->metadata = FLAC__metadata_object_new (FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL)
605 { psf_log_printf (psf, "FLAC__metadata_object_new returned NULL\n") ;
609 for (k = 0 ; k < SF_MAX_STRINGS && psf->strings.data [k].type != 0 ; k++)
610 { const char * key, * value ;
612 switch (psf->strings.data [k].type)
613 { case SF_STR_SOFTWARE :
619 case SF_STR_COPYRIGHT :
625 case SF_STR_COMMENT :
634 case SF_STR_LICENSE :
637 case SF_STR_TRACKNUMBER :
638 key = "tracknumber" ;
647 value = psf->strings.storage + psf->strings.data [k].offset ;
649 FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair (&entry, key, value) ;
650 FLAC__metadata_object_vorbiscomment_append_comment (pflac->metadata, entry, /* copy */ SF_FALSE) ;
653 if (! FLAC__stream_encoder_set_metadata (pflac->fse, &pflac->metadata, 1))
654 { printf ("%s %d : fail\n", __func__, __LINE__) ;
659 } /* flac_write_strings */
662 flac_write_header (SF_PRIVATE *psf, int UNUSED (calc_length))
663 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
666 flac_write_strings (psf, pflac) ;
668 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)
669 { psf_log_printf (psf, "Error : FLAC encoder init returned error : %s\n", FLAC__StreamEncoderInitStatusString [err]) ;
670 return SFE_FLAC_INIT_DECODER ;
674 psf->dataoffset = psf_ftell (psf) ;
675 pflac->encbuffer = calloc (ENC_BUFFER_SIZE, sizeof (int32_t)) ;
677 /* can only call init_stream once */
678 psf->write_header = NULL ;
681 } /* flac_write_header */
683 /*------------------------------------------------------------------------------
688 flac_open (SF_PRIVATE *psf)
692 FLAC_PRIVATE* pflac = calloc (1, sizeof (FLAC_PRIVATE)) ;
693 psf->codec_data = pflac ;
695 /* Set the default value here. Over-ridden later if necessary. */
696 pflac->compression = FLAC_DEFAULT_COMPRESSION_LEVEL ;
698 if (psf->file.mode == SFM_RDWR)
699 return SFE_BAD_MODE_RW ;
701 if (psf->file.mode == SFM_READ)
702 { if ((error = flac_read_header (psf)))
706 subformat = SF_CODEC (psf->sf.format) ;
708 if (psf->file.mode == SFM_WRITE)
709 { if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_FLAC)
710 return SFE_BAD_OPEN_FORMAT ;
712 psf->endian = SF_ENDIAN_BIG ;
713 psf->sf.seekable = 0 ;
715 psf->strings.flags = SF_STR_ALLOW_START ;
717 if ((error = flac_enc_init (psf)))
720 /* In an ideal world we would write the header at this point. Unfortunately
721 ** that would prevent string metadata being added so we have to hold off.
724 psf->write_header = flac_write_header ;
727 psf->datalength = psf->filelength ;
728 psf->dataoffset = 0 ;
730 psf->container_close = flac_close ;
731 psf->seek = flac_seek ;
732 psf->byterate = flac_byterate ;
734 psf->command = flac_command ;
737 { case SF_FORMAT_PCM_S8 : /* 8-bit FLAC. */
738 case SF_FORMAT_PCM_16 : /* 16-bit FLAC. */
739 case SF_FORMAT_PCM_24 : /* 24-bit FLAC. */
740 error = flac_init (psf) ;
743 default : return SFE_UNIMPLEMENTED ;
749 /*------------------------------------------------------------------------------
753 flac_close (SF_PRIVATE *psf)
754 { FLAC_PRIVATE* pflac ;
757 if ((pflac = (FLAC_PRIVATE*) psf->codec_data) == NULL)
760 if (pflac->metadata != NULL)
761 FLAC__metadata_object_delete (pflac->metadata) ;
763 if (psf->file.mode == SFM_WRITE)
764 { FLAC__stream_encoder_finish (pflac->fse) ;
765 FLAC__stream_encoder_delete (pflac->fse) ;
766 free (pflac->encbuffer) ;
769 if (psf->file.mode == SFM_READ)
770 { FLAC__stream_decoder_finish (pflac->fsd) ;
771 FLAC__stream_decoder_delete (pflac->fsd) ;
774 for (k = 0 ; k < ARRAY_LEN (pflac->rbuffer) ; k++)
775 free (pflac->rbuffer [k]) ;
778 psf->codec_data = NULL ;
784 flac_enc_init (SF_PRIVATE *psf)
785 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
788 /* To cite the flac FAQ at
789 ** http://flac.sourceforge.net/faq.html#general__samples
790 ** "FLAC supports linear sample rates from 1Hz - 655350Hz in 1Hz
793 if (psf->sf.samplerate < 1 || psf->sf.samplerate > 655350)
794 { psf_log_printf (psf, "flac sample rate out of range.\n", psf->sf.samplerate) ;
795 return SFE_FLAC_BAD_SAMPLE_RATE ;
798 psf_fseek (psf, 0, SEEK_SET) ;
800 switch (SF_CODEC (psf->sf.format))
801 { case SF_FORMAT_PCM_S8 :
804 case SF_FORMAT_PCM_16 :
807 case SF_FORMAT_PCM_24 :
817 FLAC__stream_encoder_delete (pflac->fse) ;
818 if ((pflac->fse = FLAC__stream_encoder_new ()) == NULL)
819 return SFE_FLAC_NEW_DECODER ;
821 if (! FLAC__stream_encoder_set_channels (pflac->fse, psf->sf.channels))
822 { psf_log_printf (psf, "FLAC__stream_encoder_set_channels (%d) return false.\n", psf->sf.channels) ;
823 return SFE_FLAC_INIT_DECODER ;
826 if (! FLAC__stream_encoder_set_sample_rate (pflac->fse, psf->sf.samplerate))
827 { psf_log_printf (psf, "FLAC__stream_encoder_set_sample_rate (%d) returned false.\n", psf->sf.samplerate) ;
828 return SFE_FLAC_BAD_SAMPLE_RATE ;
831 if (! FLAC__stream_encoder_set_bits_per_sample (pflac->fse, bps))
832 { psf_log_printf (psf, "FLAC__stream_encoder_set_bits_per_sample (%d) return false.\n", bps) ;
833 return SFE_FLAC_INIT_DECODER ;
836 if (! FLAC__stream_encoder_set_compression_level (pflac->fse, pflac->compression))
837 { psf_log_printf (psf, "FLAC__stream_encoder_set_compression_level (%d) return false.\n", pflac->compression) ;
838 return SFE_FLAC_INIT_DECODER ;
842 } /* flac_enc_init */
845 flac_read_header (SF_PRIVATE *psf)
846 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
848 psf_fseek (psf, 0, SEEK_SET) ;
850 FLAC__stream_decoder_delete (pflac->fsd) ;
851 if ((pflac->fsd = FLAC__stream_decoder_new ()) == NULL)
852 return SFE_FLAC_NEW_DECODER ;
854 FLAC__stream_decoder_set_metadata_respond_all (pflac->fsd) ;
856 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)
857 return SFE_FLAC_INIT_DECODER ;
859 FLAC__stream_decoder_process_until_end_of_metadata (pflac->fsd) ;
861 psf_log_printf (psf, "End\n") ;
864 FLAC__stream_decoder_delete (pflac->fsd) ;
866 { FLAC__uint64 position ;
868 FLAC__stream_decoder_get_decode_position (pflac->fsd, &position) ;
869 psf->dataoffset = position ;
873 } /* flac_read_header */
876 flac_command (SF_PRIVATE * psf, int command, void * data, int datasize)
877 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
881 { case SFC_SET_COMPRESSION_LEVEL :
882 if (data == NULL || datasize != sizeof (double))
885 if (psf->have_written)
888 /* FLAC compression level is in the range [0, 8] while libsndfile takes
889 ** values in the range [0.0, 1.0]. Massage the libsndfile value here.
891 quality = (*((double *) data)) * 8.0 ;
893 pflac->compression = psf_lrint (SF_MAX (0.0, SF_MIN (8.0, quality))) ;
895 psf_log_printf (psf, "%s : Setting SFC_SET_COMPRESSION_LEVEL to %u.\n", __func__, pflac->compression) ;
897 if (flac_enc_init (psf))
910 flac_init (SF_PRIVATE *psf)
912 if (psf->file.mode == SFM_RDWR)
913 return SFE_BAD_MODE_RW ;
915 if (psf->file.mode == SFM_READ)
916 { psf->read_short = flac_read_flac2s ;
917 psf->read_int = flac_read_flac2i ;
918 psf->read_float = flac_read_flac2f ;
919 psf->read_double = flac_read_flac2d ;
922 if (psf->file.mode == SFM_WRITE)
923 { psf->write_short = flac_write_s2flac ;
924 psf->write_int = flac_write_i2flac ;
925 psf->write_float = flac_write_f2flac ;
926 psf->write_double = flac_write_d2flac ;
929 if (psf->filelength > psf->dataoffset)
930 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset : psf->filelength - psf->dataoffset ;
932 psf->datalength = 0 ;
938 flac_read_loop (SF_PRIVATE *psf, unsigned len)
939 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
940 FLAC__StreamDecoderState state ;
944 pflac->remain = len ;
946 state = FLAC__stream_decoder_get_state (pflac->fsd) ;
947 if (state > FLAC__STREAM_DECODER_END_OF_STREAM)
948 { psf_log_printf (psf, "FLAC__stream_decoder_get_state returned %s\n", FLAC__StreamDecoderStateString [state]) ;
949 /* Current frame is busted, so NULL the pointer. */
950 pflac->frame = NULL ;
953 /* First copy data that has already been decoded and buffered. */
954 if (pflac->frame != NULL && pflac->bufferpos < pflac->frame->header.blocksize)
955 flac_buffer_copy (psf) ;
957 /* Decode some more. */
958 while (pflac->pos < pflac->len)
959 { if (FLAC__stream_decoder_process_single (pflac->fsd) == 0)
960 { psf_log_printf (psf, "FLAC__stream_decoder_process_single returned false\n") ;
961 /* Current frame is busted, so NULL the pointer. */
962 pflac->frame = NULL ;
965 state = FLAC__stream_decoder_get_state (pflac->fsd) ;
966 if (state >= FLAC__STREAM_DECODER_END_OF_STREAM)
967 { psf_log_printf (psf, "FLAC__stream_decoder_get_state returned %s\n", FLAC__StreamDecoderStateString [state]) ;
968 /* Current frame is busted, so NULL the pointer. */
969 pflac->frame = NULL ;
977 } /* flac_read_loop */
980 flac_read_flac2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
981 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
982 sf_count_t total = 0, current ;
985 pflac->pcmtype = PFLAC_PCM_SHORT ;
988 { pflac->ptr = ptr + total ;
989 readlen = (len - total > READ_LOOP_MAX_LEN) ? READ_LOOP_MAX_LEN : (unsigned) (len - total) ;
990 current = flac_read_loop (psf, readlen) ;
997 } /* flac_read_flac2s */
1000 flac_read_flac2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
1001 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
1002 sf_count_t total = 0, current ;
1005 pflac->pcmtype = PFLAC_PCM_INT ;
1008 { pflac->ptr = ptr + total ;
1009 readlen = (len - total > READ_LOOP_MAX_LEN) ? READ_LOOP_MAX_LEN : (unsigned) (len - total) ;
1010 current = flac_read_loop (psf, readlen) ;
1017 } /* flac_read_flac2i */
1020 flac_read_flac2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
1021 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
1022 sf_count_t total = 0, current ;
1025 pflac->pcmtype = PFLAC_PCM_FLOAT ;
1028 { pflac->ptr = ptr + total ;
1029 readlen = (len - total > READ_LOOP_MAX_LEN) ? READ_LOOP_MAX_LEN : (unsigned) (len - total) ;
1030 current = flac_read_loop (psf, readlen) ;
1037 } /* flac_read_flac2f */
1040 flac_read_flac2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
1041 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
1042 sf_count_t total = 0, current ;
1045 pflac->pcmtype = PFLAC_PCM_DOUBLE ;
1048 { pflac->ptr = ptr + total ;
1049 readlen = (len - total > READ_LOOP_MAX_LEN) ? READ_LOOP_MAX_LEN : (unsigned) (len - total) ;
1051 current = flac_read_loop (psf, readlen) ;
1058 } /* flac_read_flac2d */
1061 flac_write_s2flac (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
1062 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
1063 void (*convert) (const short *, int32_t *, int) ;
1064 int bufferlen, writecount, thiswrite ;
1065 sf_count_t total = 0 ;
1066 int32_t* buffer = pflac->encbuffer ;
1068 switch (SF_CODEC (psf->sf.format))
1069 { case SF_FORMAT_PCM_S8 :
1070 convert = s2flac8_array ;
1072 case SF_FORMAT_PCM_16 :
1073 convert = s2flac16_array ;
1075 case SF_FORMAT_PCM_24 :
1076 convert = s2flac24_array ;
1082 bufferlen = ENC_BUFFER_SIZE / (sizeof (int32_t) * psf->sf.channels) ;
1083 bufferlen *= psf->sf.channels ;
1086 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1087 convert (ptr + total, buffer, writecount) ;
1088 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount / psf->sf.channels))
1089 thiswrite = writecount ;
1092 total += thiswrite ;
1093 if (thiswrite < writecount)
1100 } /* flac_write_s2flac */
1103 flac_write_i2flac (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
1104 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
1105 void (*convert) (const int *, int32_t *, int) ;
1106 int bufferlen, writecount, thiswrite ;
1107 sf_count_t total = 0 ;
1108 int32_t* buffer = pflac->encbuffer ;
1110 switch (SF_CODEC (psf->sf.format))
1111 { case SF_FORMAT_PCM_S8 :
1112 convert = i2flac8_array ;
1114 case SF_FORMAT_PCM_16 :
1115 convert = i2flac16_array ;
1117 case SF_FORMAT_PCM_24 :
1118 convert = i2flac24_array ;
1124 bufferlen = ENC_BUFFER_SIZE / (sizeof (int32_t) * psf->sf.channels) ;
1125 bufferlen *= psf->sf.channels ;
1128 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1129 convert (ptr + total, buffer, writecount) ;
1130 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount / psf->sf.channels))
1131 thiswrite = writecount ;
1134 total += thiswrite ;
1135 if (thiswrite < writecount)
1142 } /* flac_write_i2flac */
1145 flac_write_f2flac (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
1146 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
1147 void (*convert) (const float *, int32_t *, int, int) ;
1148 int bufferlen, writecount, thiswrite ;
1149 sf_count_t total = 0 ;
1150 int32_t* buffer = pflac->encbuffer ;
1152 switch (SF_CODEC (psf->sf.format))
1153 { case SF_FORMAT_PCM_S8 :
1154 convert = (psf->add_clipping) ? f2flac8_clip_array : f2flac8_array ;
1156 case SF_FORMAT_PCM_16 :
1157 convert = (psf->add_clipping) ? f2flac16_clip_array : f2flac16_array ;
1159 case SF_FORMAT_PCM_24 :
1160 convert = (psf->add_clipping) ? f2flac24_clip_array : f2flac24_array ;
1166 bufferlen = ENC_BUFFER_SIZE / (sizeof (int32_t) * psf->sf.channels) ;
1167 bufferlen *= psf->sf.channels ;
1170 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1171 convert (ptr + total, buffer, writecount, psf->norm_float) ;
1172 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount / psf->sf.channels))
1173 thiswrite = writecount ;
1176 total += thiswrite ;
1177 if (thiswrite < writecount)
1184 } /* flac_write_f2flac */
1187 f2flac8_clip_array (const float *src, int32_t *dest, int count, int normalize)
1188 { float normfact, scaled_value ;
1190 normfact = normalize ? (8.0 * 0x10) : 1.0 ;
1192 for (int i = 0 ; i < count ; i++)
1193 { scaled_value = src [i] * normfact ;
1194 if (scaled_value >= (1.0 * 0x7F))
1198 if (scaled_value <= (-8.0 * 0x10))
1199 { dest [i] = -0x80 ;
1202 dest [i] = psf_lrintf (scaled_value) ;
1206 } /* f2flac8_clip_array */
1209 f2flac16_clip_array (const float *src, int32_t *dest, int count, int normalize)
1210 { float normfact, scaled_value ;
1212 normfact = normalize ? (8.0 * 0x1000) : 1.0 ;
1214 for (int i = 0 ; i < count ; i++)
1215 { scaled_value = src [i] * normfact ;
1216 if (scaled_value >= (1.0 * 0x7FFF))
1217 { dest [i] = 0x7FFF ;
1220 if (scaled_value <= (-8.0 * 0x1000))
1221 { dest [i] = -0x8000 ;
1224 dest [i] = psf_lrintf (scaled_value) ;
1226 } /* f2flac16_clip_array */
1229 f2flac24_clip_array (const float *src, int32_t *dest, int count, int normalize)
1230 { float normfact, scaled_value ;
1232 normfact = normalize ? (8.0 * 0x100000) : 1.0 ;
1234 for (int i = 0 ; i < count ; i++)
1235 { scaled_value = src [i] * normfact ;
1236 if (scaled_value >= (1.0 * 0x7FFFFF))
1237 { dest [i] = 0x7FFFFF ;
1241 if (scaled_value <= (-8.0 * 0x100000))
1242 { dest [i] = -0x800000 ;
1245 dest [i] = psf_lrintf (scaled_value) ;
1249 } /* f2flac24_clip_array */
1252 f2flac8_array (const float *src, int32_t *dest, int count, int normalize)
1253 { float normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
1255 for (int i = 0 ; i < count ; i++)
1256 dest [i] = psf_lrintf (src [i] * normfact) ;
1257 } /* f2flac8_array */
1260 f2flac16_array (const float *src, int32_t *dest, int count, int normalize)
1261 { float normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
1263 for (int i = 0 ; i < count ; i++)
1264 dest [i] = psf_lrintf (src [i] * normfact) ;
1265 } /* f2flac16_array */
1268 f2flac24_array (const float *src, int32_t *dest, int count, int normalize)
1269 { float normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
1271 for (int i = 0 ; i < count ; i++)
1272 dest [i] = psf_lrintf (src [i] * normfact) ;
1273 } /* f2flac24_array */
1276 flac_write_d2flac (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
1277 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
1278 void (*convert) (const double *, int32_t *, int, int) ;
1279 int bufferlen, writecount, thiswrite ;
1280 sf_count_t total = 0 ;
1281 int32_t* buffer = pflac->encbuffer ;
1283 switch (SF_CODEC (psf->sf.format))
1284 { case SF_FORMAT_PCM_S8 :
1285 convert = (psf->add_clipping) ? d2flac8_clip_array : d2flac8_array ;
1287 case SF_FORMAT_PCM_16 :
1288 convert = (psf->add_clipping) ? d2flac16_clip_array : d2flac16_array ;
1290 case SF_FORMAT_PCM_24 :
1291 convert = (psf->add_clipping) ? d2flac24_clip_array : d2flac24_array ;
1297 bufferlen = ENC_BUFFER_SIZE / (sizeof (int32_t) * psf->sf.channels) ;
1298 bufferlen *= psf->sf.channels ;
1301 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1302 convert (ptr + total, buffer, writecount, psf->norm_double) ;
1303 if (FLAC__stream_encoder_process_interleaved (pflac->fse, buffer, writecount / psf->sf.channels))
1304 thiswrite = writecount ;
1307 total += thiswrite ;
1308 if (thiswrite < writecount)
1315 } /* flac_write_d2flac */
1318 d2flac8_clip_array (const double *src, int32_t *dest, int count, int normalize)
1319 { double normfact, scaled_value ;
1321 normfact = normalize ? (8.0 * 0x10) : 1.0 ;
1323 for (int i = 0 ; i < count ; i++)
1324 { scaled_value = src [i] * normfact ;
1325 if (scaled_value >= (1.0 * 0x7F))
1329 if (scaled_value <= (-8.0 * 0x10))
1330 { dest [i] = -0x80 ;
1333 dest [i] = psf_lrint (scaled_value) ;
1337 } /* d2flac8_clip_array */
1340 d2flac16_clip_array (const double *src, int32_t *dest, int count, int normalize)
1341 { double normfact, scaled_value ;
1343 normfact = normalize ? (8.0 * 0x1000) : 1.0 ;
1345 for (int i = 0 ; i < count ; i++)
1346 { scaled_value = src [i] * normfact ;
1347 if (scaled_value >= (1.0 * 0x7FFF))
1348 { dest [i] = 0x7FFF ;
1351 if (scaled_value <= (-8.0 * 0x1000))
1352 { dest [i] = -0x8000 ;
1355 dest [i] = psf_lrint (scaled_value) ;
1359 } /* d2flac16_clip_array */
1362 d2flac24_clip_array (const double *src, int32_t *dest, int count, int normalize)
1363 { double normfact, scaled_value ;
1365 normfact = normalize ? (8.0 * 0x100000) : 1.0 ;
1367 for (int i = 0 ; i < count ; i++)
1368 { scaled_value = src [i] * normfact ;
1369 if (scaled_value >= (1.0 * 0x7FFFFF))
1370 { dest [i] = 0x7FFFFF ;
1373 if (scaled_value <= (-8.0 * 0x100000))
1374 { dest [i] = -0x800000 ;
1377 dest [i] = psf_lrint (scaled_value) ;
1381 } /* d2flac24_clip_array */
1384 d2flac8_array (const double *src, int32_t *dest, int count, int normalize)
1385 { double normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
1387 for (int i = 0 ; i < count ; i++)
1388 dest [i] = psf_lrint (src [i] * normfact) ;
1389 } /* d2flac8_array */
1392 d2flac16_array (const double *src, int32_t *dest, int count, int normalize)
1393 { double normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
1395 for (int i = 0 ; i < count ; i++)
1396 dest [i] = psf_lrint (src [i] * normfact) ;
1397 } /* d2flac16_array */
1400 d2flac24_array (const double *src, int32_t *dest, int count, int normalize)
1401 { double normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
1403 for (int i = 0 ; i < count ; i++)
1404 dest [i] = psf_lrint (src [i] * normfact) ;
1405 } /* d2flac24_array */
1408 flac_seek (SF_PRIVATE *psf, int UNUSED (mode), sf_count_t offset)
1409 { FLAC_PRIVATE* pflac = (FLAC_PRIVATE*) psf->codec_data ;
1414 if (psf->dataoffset < 0)
1415 { psf->error = SFE_BAD_SEEK ;
1416 return ((sf_count_t) -1) ;
1419 pflac->frame = NULL ;
1421 if (psf->file.mode == SFM_READ)
1422 { if (FLAC__stream_decoder_seek_absolute (pflac->fsd, offset))
1425 if (offset == psf->sf.frames)
1427 ** If we've been asked to seek to the very end of the file, libFLAC
1428 ** will return an error. However, we know the length of the file so
1429 ** instead of returning an error, we can return the offset.
1434 psf->error = SFE_BAD_SEEK ;
1435 return ((sf_count_t) -1) ;
1438 /* Seeking in write mode not yet supported. */
1439 psf->error = SFE_BAD_SEEK ;
1441 return ((sf_count_t) -1) ;
1445 flac_byterate (SF_PRIVATE *psf)
1447 if (psf->file.mode == SFM_READ)
1448 return (psf->datalength * psf->sf.samplerate) / psf->sf.frames ;
1451 } /* flac_byterate */
1454 #else /* HAVE_EXTERNAL_XIPH_LIBS */
1457 flac_open (SF_PRIVATE *psf)
1459 psf_log_printf (psf, "This version of libsndfile was compiled without FLAC support.\n") ;
1460 return SFE_UNIMPLEMENTED ;