2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2006-12-04 05:45:47 +01:00
|
|
|
#include "_flac_common.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-03-18 04:29:25 +01:00
|
|
|
#ifdef HAVE_FLAC
|
|
|
|
|
2004-05-31 03:54:10 +02:00
|
|
|
#include "../utils.h"
|
|
|
|
#include "../log.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-12-04 05:45:47 +01:00
|
|
|
/* this code was based on flac123, from flac-tools */
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-26 08:27:02 +02:00
|
|
|
static flac_read_status flacRead(mpd_unused const flac_decoder * flacDec,
|
2006-12-04 05:45:47 +01:00
|
|
|
FLAC__byte buf[],
|
2007-01-14 05:25:24 +01:00
|
|
|
flac_read_status_size_t *bytes,
|
2006-12-04 05:45:47 +01:00
|
|
|
void *fdata)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
FlacData *data = (FlacData *) fdata;
|
2005-03-13 23:58:04 +01:00
|
|
|
size_t r;
|
|
|
|
|
|
|
|
while (1) {
|
2006-07-20 18:02:40 +02:00
|
|
|
r = readFromInputStream(data->inStream, (void *)buf, 1, *bytes);
|
2008-08-26 08:27:04 +02:00
|
|
|
if (r == 0 && !inputStreamAtEOF(data->inStream) &&
|
|
|
|
dc.command != DECODE_COMMAND_STOP)
|
2005-03-13 23:58:04 +01:00
|
|
|
my_usleep(10000);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*bytes = r;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-08-26 08:27:04 +02:00
|
|
|
if (r == 0 && dc.command != DECODE_COMMAND_STOP) {
|
2006-12-04 05:45:44 +01:00
|
|
|
if (inputStreamAtEOF(data->inStream))
|
2006-12-04 05:45:47 +01:00
|
|
|
return flac_read_status_eof;
|
2006-12-04 05:45:44 +01:00
|
|
|
else
|
2006-12-04 05:45:47 +01:00
|
|
|
return flac_read_status_abort;
|
2006-12-04 05:45:44 +01:00
|
|
|
}
|
2006-12-04 05:45:47 +01:00
|
|
|
return flac_read_status_continue;
|
2004-05-05 01:39:02 +02:00
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:02 +02:00
|
|
|
static flac_seek_status flacSeek(mpd_unused const flac_decoder * flacDec,
|
2006-12-04 05:45:47 +01:00
|
|
|
FLAC__uint64 offset,
|
|
|
|
void *fdata)
|
2004-05-05 01:39:02 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
FlacData *data = (FlacData *) fdata;
|
2004-05-05 01:39:02 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (seekInputStream(data->inStream, offset, SEEK_SET) < 0) {
|
2006-12-04 05:45:47 +01:00
|
|
|
return flac_seek_status_error;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2004-05-05 01:39:02 +02:00
|
|
|
|
2007-01-14 03:08:11 +01:00
|
|
|
return flac_seek_status_ok;
|
2004-05-05 01:39:02 +02:00
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:02 +02:00
|
|
|
static flac_tell_status flacTell(mpd_unused const flac_decoder * flacDec,
|
2006-12-04 05:45:47 +01:00
|
|
|
FLAC__uint64 * offset,
|
|
|
|
void *fdata)
|
2004-05-05 01:39:02 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
FlacData *data = (FlacData *) fdata;
|
2004-05-05 01:39:02 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
*offset = (long)(data->inStream->offset);
|
2004-05-05 01:39:02 +02:00
|
|
|
|
2006-12-04 05:45:47 +01:00
|
|
|
return flac_tell_status_ok;
|
2004-05-05 01:39:02 +02:00
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:02 +02:00
|
|
|
static flac_length_status flacLength(mpd_unused const flac_decoder * flacDec,
|
2006-12-04 05:45:47 +01:00
|
|
|
FLAC__uint64 * length,
|
|
|
|
void *fdata)
|
2004-05-05 01:39:02 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
FlacData *data = (FlacData *) fdata;
|
2004-05-05 01:39:02 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
*length = (size_t) (data->inStream->size);
|
2004-05-05 01:39:02 +02:00
|
|
|
|
2006-12-04 05:45:47 +01:00
|
|
|
return flac_length_status_ok;
|
2004-05-05 01:39:02 +02:00
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:02 +02:00
|
|
|
static FLAC__bool flacEOF(mpd_unused const flac_decoder * flacDec, void *fdata)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
FlacData *data = (FlacData *) fdata;
|
|
|
|
|
2006-03-16 07:52:46 +01:00
|
|
|
if (inputStreamAtEOF(data->inStream) == 1)
|
2006-07-20 18:02:40 +02:00
|
|
|
return true;
|
2006-03-16 07:52:46 +01:00
|
|
|
return false;
|
2004-05-05 01:39:02 +02:00
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:02 +02:00
|
|
|
static void flacError(mpd_unused const flac_decoder *dec,
|
2006-07-20 18:02:40 +02:00
|
|
|
FLAC__StreamDecoderErrorStatus status, void *fdata)
|
2004-05-05 01:39:02 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
flac_error_common_cb("flac", status, (FlacData *) fdata);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-12-04 05:45:47 +01:00
|
|
|
#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
|
|
|
|
static void flacPrintErroredState(FLAC__SeekableStreamDecoderState state)
|
|
|
|
{
|
|
|
|
const char *str = ""; /* "" to silence compiler warning */
|
|
|
|
switch (state) {
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_OK:
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_SEEKING:
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM:
|
|
|
|
return;
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
|
|
|
|
str = "allocation error";
|
|
|
|
break;
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR:
|
|
|
|
str = "read error";
|
|
|
|
break;
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR:
|
|
|
|
str = "seek error";
|
|
|
|
break;
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR:
|
|
|
|
str = "seekable stream error";
|
|
|
|
break;
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED:
|
|
|
|
str = "decoder already initialized";
|
|
|
|
break;
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK:
|
|
|
|
str = "invalid callback";
|
|
|
|
break;
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED:
|
|
|
|
str = "decoder uninitialized";
|
|
|
|
}
|
|
|
|
ERROR("flac %s\n", str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int flac_init(FLAC__SeekableStreamDecoder *dec,
|
|
|
|
FLAC__SeekableStreamDecoderReadCallback read_cb,
|
|
|
|
FLAC__SeekableStreamDecoderSeekCallback seek_cb,
|
|
|
|
FLAC__SeekableStreamDecoderTellCallback tell_cb,
|
|
|
|
FLAC__SeekableStreamDecoderLengthCallback length_cb,
|
|
|
|
FLAC__SeekableStreamDecoderEofCallback eof_cb,
|
|
|
|
FLAC__SeekableStreamDecoderWriteCallback write_cb,
|
|
|
|
FLAC__SeekableStreamDecoderMetadataCallback metadata_cb,
|
|
|
|
FLAC__SeekableStreamDecoderErrorCallback error_cb,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
int s = 1;
|
|
|
|
s &= FLAC__seekable_stream_decoder_set_read_callback(dec, read_cb);
|
|
|
|
s &= FLAC__seekable_stream_decoder_set_seek_callback(dec, seek_cb);
|
|
|
|
s &= FLAC__seekable_stream_decoder_set_tell_callback(dec, tell_cb);
|
|
|
|
s &= FLAC__seekable_stream_decoder_set_length_callback(dec, length_cb);
|
|
|
|
s &= FLAC__seekable_stream_decoder_set_eof_callback(dec, eof_cb);
|
|
|
|
s &= FLAC__seekable_stream_decoder_set_write_callback(dec, write_cb);
|
|
|
|
s &= FLAC__seekable_stream_decoder_set_metadata_callback(dec,
|
|
|
|
metadata_cb);
|
|
|
|
s &= FLAC__seekable_stream_decoder_set_metadata_respond(dec,
|
|
|
|
FLAC__METADATA_TYPE_VORBIS_COMMENT);
|
|
|
|
s &= FLAC__seekable_stream_decoder_set_error_callback(dec, error_cb);
|
|
|
|
s &= FLAC__seekable_stream_decoder_set_client_data(dec, data);
|
|
|
|
if (!s || (FLAC__seekable_stream_decoder_init(dec) !=
|
|
|
|
FLAC__SEEKABLE_STREAM_DECODER_OK))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#else /* FLAC_API_VERSION_CURRENT >= 7 */
|
2006-12-04 05:45:44 +01:00
|
|
|
static void flacPrintErroredState(FLAC__StreamDecoderState state)
|
2004-05-05 01:39:02 +02:00
|
|
|
{
|
2006-12-04 05:45:44 +01:00
|
|
|
const char *str = ""; /* "" to silence compiler warning */
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (state) {
|
2006-12-04 05:45:44 +01:00
|
|
|
case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
|
|
|
|
case FLAC__STREAM_DECODER_READ_METADATA:
|
|
|
|
case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
|
|
|
|
case FLAC__STREAM_DECODER_READ_FRAME:
|
|
|
|
case FLAC__STREAM_DECODER_END_OF_STREAM:
|
|
|
|
return;
|
|
|
|
case FLAC__STREAM_DECODER_OGG_ERROR:
|
|
|
|
str = "error in the Ogg layer";
|
2004-05-05 01:39:02 +02:00
|
|
|
break;
|
2006-12-04 05:45:44 +01:00
|
|
|
case FLAC__STREAM_DECODER_SEEK_ERROR:
|
|
|
|
str = "seek error";
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
2006-12-04 05:45:44 +01:00
|
|
|
case FLAC__STREAM_DECODER_ABORTED:
|
|
|
|
str = "decoder aborted by read";
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
2006-12-04 05:45:44 +01:00
|
|
|
case FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
|
|
|
|
str = "allocation error";
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
2006-12-04 05:45:44 +01:00
|
|
|
case FLAC__STREAM_DECODER_UNINITIALIZED:
|
|
|
|
str = "decoder uninitialized";
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-12-04 05:45:44 +01:00
|
|
|
ERROR("flac %s\n", str);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-12-04 05:45:47 +01:00
|
|
|
#endif /* FLAC_API_VERSION_CURRENT >= 7 */
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-26 08:27:02 +02:00
|
|
|
static void flacMetadata(mpd_unused const flac_decoder * dec,
|
2006-07-20 18:02:40 +02:00
|
|
|
const FLAC__StreamMetadata * block, void *vdata)
|
2004-05-05 01:39:02 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
flac_metadata_common_cb(block, (FlacData *) vdata);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:20:30 +02:00
|
|
|
static void flac_convert_stereo16(unsigned char *dest,
|
|
|
|
const FLAC__int32 * const buf[],
|
|
|
|
unsigned int position, unsigned int end)
|
|
|
|
{
|
|
|
|
for (; position < end; ++position) {
|
|
|
|
*(uint16_t*)dest = buf[0][position];
|
|
|
|
dest += 2;
|
|
|
|
*(uint16_t*)dest = buf[1][position];
|
|
|
|
dest += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-12 06:20:13 +02:00
|
|
|
static void flac_convert(unsigned char *dest,
|
2008-04-12 06:20:25 +02:00
|
|
|
unsigned int num_channels,
|
2008-04-12 06:20:13 +02:00
|
|
|
unsigned int bytes_per_sample,
|
|
|
|
const FLAC__int32 * const buf[],
|
|
|
|
unsigned int position, unsigned int end)
|
|
|
|
{
|
|
|
|
unsigned int c_chan, i;
|
|
|
|
FLAC__uint16 u16;
|
|
|
|
unsigned char *uc;
|
|
|
|
|
|
|
|
for (; position < end; ++position) {
|
2008-04-12 06:20:25 +02:00
|
|
|
for (c_chan = 0; c_chan < num_channels; c_chan++) {
|
2008-04-12 06:20:13 +02:00
|
|
|
u16 = buf[c_chan][position];
|
|
|
|
uc = (unsigned char *)&u16;
|
|
|
|
for (i = 0; i < bytes_per_sample; i++) {
|
|
|
|
*dest++ = *uc++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-04 05:45:47 +01:00
|
|
|
static FLAC__StreamDecoderWriteStatus flacWrite(const flac_decoder *dec,
|
|
|
|
const FLAC__Frame * frame,
|
2006-07-20 18:02:40 +02:00
|
|
|
const FLAC__int32 * const buf[],
|
|
|
|
void *vdata)
|
2004-05-05 01:39:02 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
FlacData *data = (FlacData *) vdata;
|
2004-02-24 00:41:20 +01:00
|
|
|
FLAC__uint32 samples = frame->header.blocksize;
|
2008-04-12 06:20:13 +02:00
|
|
|
unsigned int c_samp;
|
2008-04-12 06:20:25 +02:00
|
|
|
const unsigned int num_channels = frame->header.channels;
|
2008-08-26 08:27:05 +02:00
|
|
|
const unsigned int bytes_per_sample = (data->audio_format.bits / 8);
|
2008-04-12 06:20:06 +02:00
|
|
|
const unsigned int bytes_per_channel =
|
|
|
|
bytes_per_sample * frame->header.channels;
|
2008-04-12 06:20:19 +02:00
|
|
|
const unsigned int max_samples = FLAC_CHUNK_SIZE / bytes_per_channel;
|
|
|
|
unsigned int num_samples;
|
2004-02-26 04:21:24 +01:00
|
|
|
float timeChange;
|
|
|
|
FLAC__uint64 newPosition = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-08-26 08:27:05 +02:00
|
|
|
assert(data->audio_format.bits > 0);
|
2008-04-12 06:19:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
timeChange = ((float)samples) / frame->header.sample_rate;
|
|
|
|
data->time += timeChange;
|
|
|
|
|
2006-12-04 05:45:47 +01:00
|
|
|
flac_get_decode_position(dec, &newPosition);
|
2008-04-12 06:20:50 +02:00
|
|
|
if (data->position && newPosition >= data->position) {
|
2008-04-12 06:19:55 +02:00
|
|
|
assert(timeChange >= 0);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
data->bitRate =
|
|
|
|
((newPosition - data->position) * 8.0 / timeChange)
|
|
|
|
/ 1000 + 0.5;
|
2004-02-26 04:21:24 +01:00
|
|
|
}
|
|
|
|
data->position = newPosition;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-04-12 06:20:13 +02:00
|
|
|
for (c_samp = 0; c_samp < frame->header.blocksize;
|
|
|
|
c_samp += num_samples) {
|
|
|
|
num_samples = frame->header.blocksize - c_samp;
|
|
|
|
if (num_samples > max_samples)
|
|
|
|
num_samples = max_samples;
|
|
|
|
|
2008-04-12 06:20:34 +02:00
|
|
|
if (num_channels == 2 && bytes_per_sample == 2)
|
2008-04-12 06:20:30 +02:00
|
|
|
flac_convert_stereo16(data->chunk + data->chunk_length,
|
|
|
|
buf, c_samp,
|
|
|
|
c_samp + num_samples);
|
|
|
|
else
|
|
|
|
flac_convert(data->chunk + data->chunk_length,
|
|
|
|
num_channels, bytes_per_sample, buf,
|
|
|
|
c_samp, c_samp + num_samples);
|
2008-04-12 06:20:40 +02:00
|
|
|
data->chunk_length = num_samples * bytes_per_channel;
|
2008-04-12 06:20:13 +02:00
|
|
|
|
|
|
|
if (flacSendChunk(data) < 0) {
|
|
|
|
return
|
|
|
|
FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
2008-04-12 06:20:06 +02:00
|
|
|
}
|
2008-04-12 06:20:13 +02:00
|
|
|
data->chunk_length = 0;
|
2008-08-26 08:27:04 +02:00
|
|
|
if (dc.command == DECODE_COMMAND_SEEK) {
|
2008-04-12 06:20:13 +02:00
|
|
|
return
|
|
|
|
FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static MpdTag *flacMetadataDup(char *file, int *vorbisCommentFound)
|
|
|
|
{
|
|
|
|
MpdTag *ret = NULL;
|
|
|
|
FLAC__Metadata_SimpleIterator *it;
|
|
|
|
FLAC__StreamMetadata *block = NULL;
|
2004-05-31 03:54:10 +02:00
|
|
|
|
|
|
|
*vorbisCommentFound = 0;
|
|
|
|
|
|
|
|
it = FLAC__metadata_simple_iterator_new();
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!FLAC__metadata_simple_iterator_init(it, file, 1, 0)) {
|
2007-08-28 07:01:19 +02:00
|
|
|
const char *err;
|
|
|
|
FLAC_API FLAC__Metadata_SimpleIteratorStatus s;
|
|
|
|
|
|
|
|
s = FLAC__metadata_simple_iterator_status(it);
|
|
|
|
|
|
|
|
switch (s) { /* slightly more human-friendly messages: */
|
2006-07-20 18:02:40 +02:00
|
|
|
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT:
|
2007-08-28 07:01:19 +02:00
|
|
|
err = "illegal input";
|
2006-07-20 18:02:40 +02:00
|
|
|
break;
|
|
|
|
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE:
|
2007-08-28 07:01:19 +02:00
|
|
|
err = "error opening file";
|
2006-07-20 18:02:40 +02:00
|
|
|
break;
|
|
|
|
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE:
|
2007-08-28 07:01:19 +02:00
|
|
|
err = "not a FLAC file";
|
2006-07-20 18:02:40 +02:00
|
|
|
break;
|
|
|
|
default:
|
2007-08-28 07:01:19 +02:00
|
|
|
err = FLAC__Metadata_SimpleIteratorStatusString[s];
|
2005-09-08 23:08:02 +02:00
|
|
|
}
|
2007-08-28 07:01:19 +02:00
|
|
|
DEBUG("flacMetadataDup: Reading '%s' "
|
|
|
|
"metadata gave the following error: %s\n",
|
|
|
|
file, err);
|
2004-05-31 03:54:10 +02:00
|
|
|
FLAC__metadata_simple_iterator_delete(it);
|
|
|
|
return ret;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2004-05-31 03:54:10 +02:00
|
|
|
do {
|
|
|
|
block = FLAC__metadata_simple_iterator_get_block(it);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!block)
|
|
|
|
break;
|
|
|
|
if (block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
|
2004-11-10 22:58:27 +01:00
|
|
|
ret = copyVorbisCommentBlockToMpdTag(block, ret);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ret)
|
|
|
|
*vorbisCommentFound = 1;
|
|
|
|
} else if (block->type == FLAC__METADATA_TYPE_STREAMINFO) {
|
|
|
|
if (!ret)
|
|
|
|
ret = newMpdTag();
|
2004-05-31 03:54:10 +02:00
|
|
|
ret->time = ((float)block->data.stream_info.
|
2006-07-20 18:02:40 +02:00
|
|
|
total_samples) /
|
|
|
|
block->data.stream_info.sample_rate + 0.5;
|
2004-05-31 03:54:10 +02:00
|
|
|
}
|
|
|
|
FLAC__metadata_object_delete(block);
|
2006-07-20 18:02:40 +02:00
|
|
|
} while (FLAC__metadata_simple_iterator_next(it));
|
2004-05-31 03:54:10 +02:00
|
|
|
|
|
|
|
FLAC__metadata_simple_iterator_delete(it);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static MpdTag *flacTagDup(char *file)
|
|
|
|
{
|
|
|
|
MpdTag *ret = NULL;
|
2004-05-31 03:54:10 +02:00
|
|
|
int foundVorbisComment = 0;
|
|
|
|
|
2004-05-31 04:21:06 +02:00
|
|
|
ret = flacMetadataDup(file, &foundVorbisComment);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!ret) {
|
|
|
|
DEBUG("flacTagDup: Failed to grab information from: %s\n",
|
|
|
|
file);
|
2005-09-08 23:08:02 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!foundVorbisComment) {
|
|
|
|
MpdTag *temp = id3Dup(file);
|
|
|
|
if (temp) {
|
2004-05-31 03:54:10 +02:00
|
|
|
temp->time = ret->time;
|
|
|
|
freeMpdTag(ret);
|
|
|
|
ret = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:04 +02:00
|
|
|
static int flac_decode_internal(struct decoder * decoder,
|
|
|
|
InputStream * inStream, int is_ogg)
|
2006-12-04 05:45:41 +01:00
|
|
|
{
|
2006-12-04 05:45:47 +01:00
|
|
|
flac_decoder *flacDec;
|
2006-12-04 05:45:41 +01:00
|
|
|
FlacData data;
|
2006-12-04 05:45:50 +01:00
|
|
|
const char *err = NULL;
|
2006-12-04 05:45:41 +01:00
|
|
|
|
2006-12-04 05:45:47 +01:00
|
|
|
if (!(flacDec = flac_new()))
|
2006-12-04 05:45:44 +01:00
|
|
|
return -1;
|
2008-08-26 08:27:04 +02:00
|
|
|
init_FlacData(&data, decoder, inStream);
|
2007-12-07 00:01:28 +01:00
|
|
|
|
2008-01-26 13:46:26 +01:00
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
2007-12-07 00:01:28 +01:00
|
|
|
if(!FLAC__stream_decoder_set_metadata_respond(flacDec, FLAC__METADATA_TYPE_VORBIS_COMMENT))
|
|
|
|
{
|
|
|
|
DEBUG(__FILE__": Failed to set metadata respond\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2006-12-04 05:45:50 +01:00
|
|
|
if (is_ogg) {
|
|
|
|
if (!flac_ogg_init(flacDec, flacRead, flacSeek, flacTell,
|
|
|
|
flacLength, flacEOF, flacWrite, flacMetadata,
|
|
|
|
flacError, (void *)&data)) {
|
|
|
|
err = "doing Ogg init()";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!flac_init(flacDec, flacRead, flacSeek, flacTell,
|
|
|
|
flacLength, flacEOF, flacWrite, flacMetadata,
|
|
|
|
flacError, (void *)&data)) {
|
|
|
|
err = "doing init()";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (!flac_process_metadata(flacDec)) {
|
|
|
|
err = "problem reading metadata";
|
|
|
|
goto fail;
|
|
|
|
}
|
2006-12-04 05:45:41 +01:00
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:05 +02:00
|
|
|
decoder_initialized(decoder, &data.audio_format);
|
2006-12-04 05:45:41 +01:00
|
|
|
|
|
|
|
while (1) {
|
2006-12-04 05:45:47 +01:00
|
|
|
if (!flac_process_single(flacDec))
|
2006-12-04 05:45:44 +01:00
|
|
|
break;
|
2006-12-04 05:45:47 +01:00
|
|
|
if (flac_get_state(flacDec) == flac_decoder_eof)
|
2006-12-04 05:45:41 +01:00
|
|
|
break;
|
2008-08-26 08:27:04 +02:00
|
|
|
if (dc.command == DECODE_COMMAND_SEEK) {
|
2008-04-13 03:16:03 +02:00
|
|
|
FLAC__uint64 sampleToSeek = dc.seekWhere *
|
2008-08-26 08:27:05 +02:00
|
|
|
data.audio_format.sampleRate + 0.5;
|
2006-12-04 05:45:47 +01:00
|
|
|
if (flac_seek_absolute(flacDec, sampleToSeek)) {
|
2008-08-26 08:27:05 +02:00
|
|
|
decoder_clear(decoder);
|
2006-12-04 05:45:41 +01:00
|
|
|
data.time = ((float)sampleToSeek) /
|
2008-08-26 08:27:05 +02:00
|
|
|
data.audio_format.sampleRate;
|
2006-12-04 05:45:41 +01:00
|
|
|
data.position = 0;
|
|
|
|
} else
|
2008-04-13 03:16:03 +02:00
|
|
|
dc.seekError = 1;
|
2008-08-26 08:27:04 +02:00
|
|
|
dc_command_finished();
|
2006-12-04 05:45:41 +01:00
|
|
|
}
|
|
|
|
}
|
2008-08-26 08:27:04 +02:00
|
|
|
if (dc.command != DECODE_COMMAND_STOP) {
|
2006-12-04 05:45:47 +01:00
|
|
|
flacPrintErroredState(flac_get_state(flacDec));
|
|
|
|
flac_finish(flacDec);
|
2006-12-04 05:45:41 +01:00
|
|
|
}
|
|
|
|
/* send last little bit */
|
2008-08-26 08:27:04 +02:00
|
|
|
if (data.chunk_length > 0 && dc.command != DECODE_COMMAND_STOP) {
|
2006-12-04 05:45:41 +01:00
|
|
|
flacSendChunk(&data);
|
2008-08-26 08:27:05 +02:00
|
|
|
decoder_flush(decoder);
|
2006-12-04 05:45:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (data.replayGainInfo)
|
|
|
|
freeReplayGainInfo(data.replayGainInfo);
|
|
|
|
|
|
|
|
if (flacDec)
|
2006-12-04 05:45:47 +01:00
|
|
|
flac_delete(flacDec);
|
2006-12-04 05:45:41 +01:00
|
|
|
|
2006-12-04 05:45:50 +01:00
|
|
|
if (err) {
|
|
|
|
ERROR("flac %s\n", err);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:04 +02:00
|
|
|
static int flac_decode(struct decoder * decoder, InputStream * inStream)
|
2006-12-04 05:45:50 +01:00
|
|
|
{
|
2008-08-26 08:27:04 +02:00
|
|
|
return flac_decode_internal(decoder, inStream, 0);
|
2006-12-04 05:45:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
|
|
|
|
# define flac_plugin_init NULL
|
|
|
|
#else /* FLAC_API_VERSION_CURRENT >= 7 */
|
|
|
|
/* some of this stuff is duplicated from oggflac_plugin.c */
|
|
|
|
extern InputPlugin oggflacPlugin;
|
|
|
|
|
|
|
|
static MpdTag *oggflac_tag_dup(char *file)
|
|
|
|
{
|
|
|
|
MpdTag *ret = NULL;
|
|
|
|
FLAC__Metadata_Iterator *it;
|
|
|
|
FLAC__StreamMetadata *block;
|
|
|
|
FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
|
|
|
|
|
|
|
|
if (!(FLAC__metadata_chain_read_ogg(chain, file)))
|
|
|
|
goto out;
|
|
|
|
it = FLAC__metadata_iterator_new();
|
|
|
|
FLAC__metadata_iterator_init(it, chain);
|
|
|
|
do {
|
|
|
|
if (!(block = FLAC__metadata_iterator_get_block(it)))
|
|
|
|
break;
|
|
|
|
if (block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
|
|
|
|
ret = copyVorbisCommentBlockToMpdTag(block, ret);
|
|
|
|
} else if (block->type == FLAC__METADATA_TYPE_STREAMINFO) {
|
|
|
|
if (!ret)
|
|
|
|
ret = newMpdTag();
|
|
|
|
ret->time = ((float)block->data.stream_info.
|
|
|
|
total_samples) /
|
|
|
|
block->data.stream_info.sample_rate + 0.5;
|
|
|
|
}
|
|
|
|
} while (FLAC__metadata_iterator_next(it));
|
|
|
|
FLAC__metadata_iterator_delete(it);
|
|
|
|
out:
|
|
|
|
FLAC__metadata_chain_delete(chain);
|
2006-12-04 05:45:41 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:04 +02:00
|
|
|
static int oggflac_decode(struct decoder *decoder, InputStream * inStream)
|
2006-12-04 05:45:50 +01:00
|
|
|
{
|
2008-08-26 08:27:04 +02:00
|
|
|
return flac_decode_internal(decoder, inStream, 1);
|
2006-12-04 05:45:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int oggflac_try_decode(InputStream * inStream)
|
|
|
|
{
|
|
|
|
return (ogg_stream_type_detect(inStream) == FLAC) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static const char *oggflac_suffixes[] = { "ogg", "oga", NULL };
|
|
|
|
static const char *oggflac_mime_types[] = { "audio/x-flac+ogg",
|
|
|
|
"application/ogg",
|
|
|
|
"application/x-ogg",
|
|
|
|
NULL };
|
2006-12-04 05:45:50 +01:00
|
|
|
|
|
|
|
static int flac_plugin_init(void)
|
|
|
|
{
|
|
|
|
if (!FLAC_API_SUPPORTS_OGG_FLAC) {
|
|
|
|
DEBUG("libFLAC does not support OggFLAC\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
DEBUG("libFLAC supports OggFLAC, initializing OggFLAC support\n");
|
|
|
|
assert(oggflacPlugin.name == NULL);
|
|
|
|
oggflacPlugin.name = "oggflac";
|
|
|
|
oggflacPlugin.tryDecodeFunc = oggflac_try_decode;
|
|
|
|
oggflacPlugin.streamDecodeFunc = oggflac_decode;
|
|
|
|
oggflacPlugin.tagDupFunc = oggflac_tag_dup;
|
|
|
|
oggflacPlugin.streamTypes = INPUT_PLUGIN_STREAM_URL |
|
|
|
|
INPUT_PLUGIN_STREAM_FILE;
|
|
|
|
oggflacPlugin.suffixes = oggflac_suffixes;
|
|
|
|
oggflacPlugin.mimeTypes = oggflac_mime_types;
|
|
|
|
loadInputPlugin(&oggflacPlugin);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* FLAC_API_VERSION_CURRENT >= 7 */
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static const char *flacSuffixes[] = { "flac", NULL };
|
|
|
|
static const char *flac_mime_types[] = { "audio/x-flac",
|
|
|
|
"application/x-flac",
|
|
|
|
NULL };
|
2004-05-31 03:54:10 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
InputPlugin flacPlugin = {
|
2006-03-16 07:52:46 +01:00
|
|
|
"flac",
|
2006-12-04 05:45:50 +01:00
|
|
|
flac_plugin_init,
|
2004-05-31 22:59:55 +02:00
|
|
|
NULL,
|
2005-01-30 00:46:06 +01:00
|
|
|
NULL,
|
2006-03-16 07:52:46 +01:00
|
|
|
flac_decode,
|
|
|
|
NULL,
|
|
|
|
flacTagDup,
|
|
|
|
INPUT_PLUGIN_STREAM_URL | INPUT_PLUGIN_STREAM_FILE,
|
|
|
|
flacSuffixes,
|
|
|
|
flac_mime_types
|
2004-05-31 03:54:10 +02:00
|
|
|
};
|
|
|
|
|
2006-07-20 20:53:56 +02:00
|
|
|
#else /* !HAVE_FLAC */
|
2004-05-31 03:54:10 +02:00
|
|
|
|
2007-01-14 04:07:53 +01:00
|
|
|
InputPlugin flacPlugin;
|
2004-05-31 03:54:10 +02:00
|
|
|
|
2006-07-20 20:53:56 +02:00
|
|
|
#endif /* HAVE_FLAC */
|