2009-03-13 18:43:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2006-12-04 05:45:47 +01:00
|
|
|
#include "_flac_common.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-01-01 18:09:24 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <assert.h>
|
2008-11-24 10:33:08 +01:00
|
|
|
#include <unistd.h>
|
2008-10-08 10:49:29 +02:00
|
|
|
|
2009-03-08 20:16:53 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2009-03-31 22:07:20 +02:00
|
|
|
#ifdef HAVE_CUE /* libcue */
|
|
|
|
#include "../cue/cue_tag.h"
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
static flac_read_status
|
|
|
|
flac_read_cb(G_GNUC_UNUSED const flac_decoder *fd,
|
|
|
|
FLAC__byte buf[], flac_read_status_size_t *bytes,
|
|
|
|
void *fdata)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-15 19:50:28 +01:00
|
|
|
struct flac_data *data = fdata;
|
2005-03-13 23:58:04 +01:00
|
|
|
size_t r;
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
r = decoder_read(data->decoder, data->input_stream,
|
|
|
|
(void *)buf, *bytes);
|
2005-03-13 23:58:04 +01:00
|
|
|
*bytes = r;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-08-26 08:27:16 +02:00
|
|
|
if (r == 0) {
|
|
|
|
if (decoder_get_command(data->decoder) != DECODE_COMMAND_NONE ||
|
2009-01-15 19:50:28 +01:00
|
|
|
input_stream_eof(data->input_stream))
|
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
|
|
|
}
|
2008-08-26 08:27:15 +02:00
|
|
|
|
2006-12-04 05:45:47 +01:00
|
|
|
return flac_read_status_continue;
|
2004-05-05 01:39:02 +02:00
|
|
|
}
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
static flac_seek_status
|
|
|
|
flac_seek_cb(G_GNUC_UNUSED const flac_decoder *fd,
|
|
|
|
FLAC__uint64 offset, void *fdata)
|
2004-05-05 01:39:02 +02:00
|
|
|
{
|
2009-01-15 19:50:28 +01:00
|
|
|
struct flac_data *data = (struct flac_data *) fdata;
|
2004-05-05 01:39:02 +02:00
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
if (!input_stream_seek(data->input_stream, offset, SEEK_SET))
|
2006-12-04 05:45:47 +01:00
|
|
|
return flac_seek_status_error;
|
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
|
|
|
}
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
static flac_tell_status
|
|
|
|
flac_tell_cb(G_GNUC_UNUSED const flac_decoder *fd,
|
|
|
|
FLAC__uint64 * offset, void *fdata)
|
2004-05-05 01:39:02 +02:00
|
|
|
{
|
2009-01-15 19:50:28 +01:00
|
|
|
struct flac_data *data = (struct flac_data *) fdata;
|
2004-05-05 01:39:02 +02:00
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
*offset = (long)(data->input_stream->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
|
|
|
}
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
static flac_length_status
|
|
|
|
flac_length_cb(G_GNUC_UNUSED const flac_decoder *fd,
|
|
|
|
FLAC__uint64 * length, void *fdata)
|
2004-05-05 01:39:02 +02:00
|
|
|
{
|
2009-01-15 19:50:28 +01:00
|
|
|
struct flac_data *data = (struct flac_data *) fdata;
|
2004-05-05 01:39:02 +02:00
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
if (data->input_stream->size < 0)
|
2008-11-16 20:42:08 +01:00
|
|
|
return flac_length_status_unsupported;
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
*length = (size_t) (data->input_stream->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
|
|
|
}
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
static FLAC__bool
|
|
|
|
flac_eof_cb(G_GNUC_UNUSED const flac_decoder *fd, void *fdata)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-15 19:50:28 +01:00
|
|
|
struct flac_data *data = (struct flac_data *) fdata;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-08-26 08:27:16 +02:00
|
|
|
return (decoder_get_command(data->decoder) != DECODE_COMMAND_NONE &&
|
|
|
|
decoder_get_command(data->decoder) != DECODE_COMMAND_SEEK) ||
|
2009-01-15 19:50:28 +01:00
|
|
|
input_stream_eof(data->input_stream);
|
2004-05-05 01:39:02 +02:00
|
|
|
}
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
static void
|
|
|
|
flac_error_cb(G_GNUC_UNUSED const flac_decoder *fd,
|
|
|
|
FLAC__StreamDecoderErrorStatus status, void *fdata)
|
2004-05-05 01:39:02 +02:00
|
|
|
{
|
2009-01-15 19:50:28 +01:00
|
|
|
flac_error_common_cb("flac", status, (struct flac_data *) 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";
|
|
|
|
}
|
2008-11-21 20:15:50 +01:00
|
|
|
|
|
|
|
g_warning("%s\n", str);
|
2006-12-04 05:45:47 +01:00
|
|
|
}
|
|
|
|
|
2009-01-15 19:57:57 +01:00
|
|
|
static bool
|
|
|
|
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)
|
2006-12-04 05:45:47 +01:00
|
|
|
{
|
2009-01-15 19:57:57 +01:00
|
|
|
return FLAC__seekable_stream_decoder_set_read_callback(dec, read_cb) &&
|
|
|
|
FLAC__seekable_stream_decoder_set_seek_callback(dec, seek_cb) &&
|
|
|
|
FLAC__seekable_stream_decoder_set_tell_callback(dec, tell_cb) &&
|
|
|
|
FLAC__seekable_stream_decoder_set_length_callback(dec, length_cb) &&
|
|
|
|
FLAC__seekable_stream_decoder_set_eof_callback(dec, eof_cb) &&
|
|
|
|
FLAC__seekable_stream_decoder_set_write_callback(dec, write_cb) &&
|
|
|
|
FLAC__seekable_stream_decoder_set_metadata_callback(dec, metadata_cb) &&
|
|
|
|
FLAC__seekable_stream_decoder_set_metadata_respond(dec, FLAC__METADATA_TYPE_VORBIS_COMMENT) &&
|
|
|
|
FLAC__seekable_stream_decoder_set_error_callback(dec, error_cb) &&
|
|
|
|
FLAC__seekable_stream_decoder_set_client_data(dec, data) &&
|
|
|
|
FLAC__seekable_stream_decoder_init(dec) == FLAC__SEEKABLE_STREAM_DECODER_OK;
|
2006-12-04 05:45:47 +01:00
|
|
|
}
|
|
|
|
#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
|
|
|
}
|
2008-11-21 20:15:50 +01:00
|
|
|
|
|
|
|
g_warning("%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
|
|
|
|
2009-01-01 18:09:24 +01:00
|
|
|
static void flacMetadata(G_GNUC_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
|
|
|
{
|
2009-01-15 19:50:28 +01:00
|
|
|
flac_metadata_common_cb(block, (struct flac_data *) vdata);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
static FLAC__StreamDecoderWriteStatus
|
|
|
|
flac_write_cb(const flac_decoder *dec, const FLAC__Frame *frame,
|
|
|
|
const FLAC__int32 *const buf[], void *vdata)
|
2004-05-05 01:39:02 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
FLAC__uint32 samples = frame->header.blocksize;
|
2009-01-15 19:50:28 +01:00
|
|
|
struct flac_data *data = (struct flac_data *) vdata;
|
2004-02-26 04:21:24 +01:00
|
|
|
float timeChange;
|
|
|
|
FLAC__uint64 newPosition = 0;
|
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);
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
data->bit_rate =
|
2006-07-20 18:02:40 +02:00
|
|
|
((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-09-23 23:59:54 +02:00
|
|
|
return flac_common_write(data, frame, buf);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-30 08:38:54 +01:00
|
|
|
static struct tag *
|
2009-03-15 23:31:07 +01:00
|
|
|
flac_tag_load(const char *file, const char *char_tnum)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-15 22:43:39 +01:00
|
|
|
struct tag *tag;
|
2006-07-20 18:02:40 +02:00
|
|
|
FLAC__Metadata_SimpleIterator *it;
|
|
|
|
FLAC__StreamMetadata *block = NULL;
|
2004-05-31 03:54:10 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2008-11-21 20:15:50 +01:00
|
|
|
g_debug("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);
|
2009-01-15 22:43:39 +01:00
|
|
|
return NULL;
|
2004-05-31 03:54:10 +02:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2009-01-15 22:43:39 +01:00
|
|
|
tag = tag_new();
|
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) {
|
2009-03-15 23:31:07 +01:00
|
|
|
flac_vorbis_comments_to_tag(tag, char_tnum, block);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (block->type == FLAC__METADATA_TYPE_STREAMINFO) {
|
2009-01-15 22:43:39 +01:00
|
|
|
tag->time = ((float)block->data.stream_info.total_samples) /
|
2006-07-20 18:02:40 +02:00
|
|
|
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);
|
2009-01-15 22:43:39 +01:00
|
|
|
|
|
|
|
if (!tag_is_defined(tag)) {
|
|
|
|
tag_free(tag);
|
|
|
|
tag = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tag;
|
2004-05-31 03:54:10 +02:00
|
|
|
}
|
|
|
|
|
2009-03-08 20:16:53 +01:00
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
|
|
|
|
|
|
|
static struct tag *
|
|
|
|
flac_cue_tag_load(const char *file)
|
|
|
|
{
|
|
|
|
struct tag* tag = NULL;
|
|
|
|
char* char_tnum = NULL;
|
2009-03-31 22:07:20 +02:00
|
|
|
char* ptr = NULL;
|
2009-03-08 20:16:53 +01:00
|
|
|
unsigned int tnum = 0;
|
|
|
|
unsigned int sample_rate = 0;
|
|
|
|
FLAC__uint64 track_time = 0;
|
2009-03-31 22:07:20 +02:00
|
|
|
#ifdef HAVE_CUE /* libcue */
|
2009-10-16 17:39:17 +02:00
|
|
|
FLAC__StreamMetadata* vc;
|
2009-07-09 16:13:09 +02:00
|
|
|
char* cs_filename;
|
|
|
|
FILE* cs_file;
|
2009-03-31 22:07:20 +02:00
|
|
|
#endif /* libcue */
|
2009-03-08 20:16:53 +01:00
|
|
|
FLAC__StreamMetadata* si = FLAC__metadata_object_new(FLAC__METADATA_TYPE_STREAMINFO);
|
2009-10-16 17:39:17 +02:00
|
|
|
FLAC__StreamMetadata* cs;
|
2009-03-08 20:16:53 +01:00
|
|
|
|
|
|
|
tnum = flac_vtrack_tnum(file);
|
2009-03-15 23:31:07 +01:00
|
|
|
char_tnum = g_strdup_printf("%u", tnum);
|
2009-03-08 20:16:53 +01:00
|
|
|
|
2009-03-31 22:07:20 +02:00
|
|
|
ptr = strrchr(file, '/');
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
#ifdef HAVE_CUE /* libcue */
|
|
|
|
if (FLAC__metadata_get_tags(file, &vc))
|
|
|
|
{
|
2009-04-01 16:44:27 +02:00
|
|
|
for (unsigned i = 0; i < vc->data.vorbis_comment.num_comments;
|
|
|
|
i++)
|
2009-03-31 22:07:20 +02:00
|
|
|
{
|
|
|
|
if ((ptr = (char*)vc->data.vorbis_comment.comments[i].entry) != NULL)
|
|
|
|
{
|
|
|
|
if (g_ascii_strncasecmp(ptr, "cuesheet", 8) == 0)
|
|
|
|
{
|
|
|
|
while (*(++ptr) != '=');
|
|
|
|
tag = cue_tag_string( ++ptr,
|
|
|
|
tnum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-16 17:39:17 +02:00
|
|
|
|
2009-03-31 22:07:20 +02:00
|
|
|
FLAC__metadata_object_delete(vc);
|
|
|
|
}
|
2009-07-09 16:13:09 +02:00
|
|
|
|
|
|
|
if (tag == NULL) {
|
|
|
|
cs_filename = g_strconcat(file, ".cue", NULL);
|
|
|
|
|
|
|
|
cs_file = fopen(cs_filename, "rt");
|
|
|
|
g_free(cs_filename);
|
|
|
|
|
|
|
|
if (cs_file != NULL) {
|
|
|
|
tag = cue_tag_file(cs_file, tnum);
|
|
|
|
fclose(cs_file);
|
|
|
|
}
|
|
|
|
}
|
2009-03-31 22:07:20 +02:00
|
|
|
#endif /* libcue */
|
2009-03-08 20:16:53 +01:00
|
|
|
|
2009-03-31 22:07:20 +02:00
|
|
|
if (tag == NULL)
|
|
|
|
tag = flac_tag_load(file, char_tnum);
|
2009-03-08 20:16:53 +01:00
|
|
|
|
2009-10-13 16:12:45 +02:00
|
|
|
if (char_tnum != NULL) {
|
|
|
|
tag_add_item(tag, TAG_TRACK, char_tnum);
|
2009-03-08 20:16:53 +01:00
|
|
|
g_free(char_tnum);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FLAC__metadata_get_streaminfo(file, si))
|
|
|
|
{
|
|
|
|
sample_rate = si->data.stream_info.sample_rate;
|
2009-03-31 22:04:04 +02:00
|
|
|
FLAC__metadata_object_delete(si);
|
2009-03-08 20:16:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FLAC__metadata_get_cuesheet(file, &cs))
|
|
|
|
{
|
|
|
|
if (cs->data.cue_sheet.tracks != NULL
|
|
|
|
&& (tnum <= cs->data.cue_sheet.num_tracks - 1))
|
|
|
|
{
|
2009-03-31 22:06:28 +02:00
|
|
|
track_time = cs->data.cue_sheet.tracks[tnum].offset
|
2009-03-08 20:16:53 +01:00
|
|
|
- cs->data.cue_sheet.tracks[tnum - 1].offset;
|
|
|
|
}
|
|
|
|
FLAC__metadata_object_delete(cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sample_rate != 0)
|
|
|
|
{
|
|
|
|
tag->time = (unsigned int)(track_time/sample_rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* FLAC_API_VERSION_CURRENT >= 7 */
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
static struct tag *
|
|
|
|
flac_tag_dup(const char *file)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-03-08 20:16:53 +01:00
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(file, &st) < 0)
|
|
|
|
return flac_cue_tag_load(file);
|
|
|
|
else
|
|
|
|
#endif /* FLAC_API_VERSION_CURRENT >= 7 */
|
2009-03-15 23:31:07 +01:00
|
|
|
return flac_tag_load(file, NULL);
|
2004-05-31 03:54:10 +02:00
|
|
|
}
|
|
|
|
|
2008-11-11 17:13:44 +01:00
|
|
|
static void
|
2009-01-15 19:50:28 +01:00
|
|
|
flac_decode_internal(struct decoder * decoder,
|
|
|
|
struct input_stream *input_stream,
|
2008-10-30 08:38:54 +01:00
|
|
|
bool is_ogg)
|
2006-12-04 05:45:41 +01:00
|
|
|
{
|
2009-01-15 19:50:28 +01:00
|
|
|
flac_decoder *flac_dec;
|
|
|
|
struct flac_data data;
|
2009-03-01 14:07:23 +01:00
|
|
|
enum decoder_command cmd;
|
2006-12-04 05:45:50 +01:00
|
|
|
const char *err = NULL;
|
2006-12-04 05:45:41 +01:00
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
if (!(flac_dec = flac_new()))
|
2008-11-11 17:13:44 +01:00
|
|
|
return;
|
2009-01-15 19:50:28 +01:00
|
|
|
flac_data_init(&data, decoder, input_stream);
|
2009-07-06 22:09:30 +02:00
|
|
|
data.tag = tag_new();
|
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
|
2009-01-15 19:50:28 +01:00
|
|
|
if(!FLAC__stream_decoder_set_metadata_respond(flac_dec, FLAC__METADATA_TYPE_VORBIS_COMMENT))
|
2007-12-07 00:01:28 +01:00
|
|
|
{
|
2008-11-21 20:15:50 +01:00
|
|
|
g_debug("Failed to set metadata respond\n");
|
2007-12-07 00:01:28 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-12-04 05:45:50 +01:00
|
|
|
if (is_ogg) {
|
2009-01-15 19:50:28 +01:00
|
|
|
if (!flac_ogg_init(flac_dec, flac_read_cb,
|
|
|
|
flac_seek_cb, flac_tell_cb,
|
|
|
|
flac_length_cb, flac_eof_cb,
|
|
|
|
flac_write_cb, flacMetadata,
|
|
|
|
flac_error_cb, (void *)&data)) {
|
2006-12-04 05:45:50 +01:00
|
|
|
err = "doing Ogg init()";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
} else {
|
2009-01-15 19:50:28 +01:00
|
|
|
if (!flac_init(flac_dec, flac_read_cb,
|
|
|
|
flac_seek_cb, flac_tell_cb,
|
|
|
|
flac_length_cb, flac_eof_cb,
|
|
|
|
flac_write_cb, flacMetadata,
|
|
|
|
flac_error_cb, (void *)&data)) {
|
2006-12-04 05:45:50 +01:00
|
|
|
err = "doing init()";
|
|
|
|
goto fail;
|
|
|
|
}
|
2008-11-10 14:49:34 +01:00
|
|
|
}
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
if (!flac_process_metadata(flac_dec)) {
|
2008-11-10 14:49:34 +01:00
|
|
|
err = "problem reading metadata";
|
|
|
|
goto fail;
|
2006-12-04 05:45:41 +01:00
|
|
|
}
|
|
|
|
|
2008-11-21 20:27:30 +01:00
|
|
|
if (!audio_format_valid(&data.audio_format)) {
|
|
|
|
g_warning("Invalid audio format: %u:%u:%u\n",
|
|
|
|
data.audio_format.sample_rate,
|
|
|
|
data.audio_format.bits,
|
|
|
|
data.audio_format.channels);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2008-11-02 17:01:51 +01:00
|
|
|
decoder_initialized(decoder, &data.audio_format,
|
2009-01-15 19:50:28 +01:00
|
|
|
input_stream->seekable, data.total_time);
|
2006-12-04 05:45:41 +01:00
|
|
|
|
2008-10-30 08:38:54 +01:00
|
|
|
while (true) {
|
2009-03-01 14:07:23 +01:00
|
|
|
if (!tag_is_empty(data.tag)) {
|
|
|
|
cmd = decoder_tag(decoder, input_stream, data.tag);
|
|
|
|
tag_free(data.tag);
|
|
|
|
data.tag = tag_new();
|
|
|
|
} else
|
|
|
|
cmd = decoder_get_command(decoder);
|
|
|
|
|
|
|
|
if (cmd == DECODE_COMMAND_SEEK) {
|
2009-01-15 19:50:28 +01:00
|
|
|
FLAC__uint64 seek_sample = decoder_seek_where(decoder) *
|
2008-10-10 14:40:54 +02:00
|
|
|
data.audio_format.sample_rate + 0.5;
|
2009-01-15 19:50:28 +01:00
|
|
|
if (flac_seek_absolute(flac_dec, seek_sample)) {
|
|
|
|
data.time = ((float)seek_sample) /
|
2008-10-10 14:40:54 +02:00
|
|
|
data.audio_format.sample_rate;
|
2006-12-04 05:45:41 +01:00
|
|
|
data.position = 0;
|
2008-08-26 08:27:07 +02:00
|
|
|
decoder_command_finished(decoder);
|
2006-12-04 05:45:41 +01:00
|
|
|
} else
|
2008-08-26 08:27:07 +02:00
|
|
|
decoder_seek_error(decoder);
|
2009-03-05 18:20:43 +01:00
|
|
|
} else if (cmd == DECODE_COMMAND_STOP ||
|
|
|
|
flac_get_state(flac_dec) == flac_decoder_eof)
|
2008-08-26 08:27:15 +02:00
|
|
|
break;
|
2009-03-05 18:20:41 +01:00
|
|
|
|
|
|
|
if (!flac_process_single(flac_dec)) {
|
|
|
|
cmd = decoder_get_command(decoder);
|
|
|
|
if (cmd != DECODE_COMMAND_SEEK)
|
|
|
|
break;
|
|
|
|
}
|
2006-12-04 05:45:41 +01:00
|
|
|
}
|
2009-03-01 14:07:23 +01:00
|
|
|
if (cmd != DECODE_COMMAND_STOP) {
|
2009-01-15 19:50:28 +01:00
|
|
|
flacPrintErroredState(flac_get_state(flac_dec));
|
|
|
|
flac_finish(flac_dec);
|
2006-12-04 05:45:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
2009-01-15 19:50:28 +01:00
|
|
|
if (data.replay_gain_info)
|
|
|
|
replay_gain_info_free(data.replay_gain_info);
|
2006-12-04 05:45:41 +01:00
|
|
|
|
2009-03-01 14:07:23 +01:00
|
|
|
tag_free(data.tag);
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
if (flac_dec)
|
|
|
|
flac_delete(flac_dec);
|
2006-12-04 05:45:41 +01:00
|
|
|
|
2008-11-21 20:15:50 +01:00
|
|
|
if (err)
|
|
|
|
g_warning("%s\n", err);
|
2006-12-04 05:45:50 +01:00
|
|
|
}
|
|
|
|
|
2008-11-11 17:13:44 +01:00
|
|
|
static void
|
2009-01-15 19:50:28 +01:00
|
|
|
flac_decode(struct decoder * decoder, struct input_stream *input_stream)
|
2006-12-04 05:45:50 +01:00
|
|
|
{
|
2009-01-15 19:50:28 +01:00
|
|
|
flac_decode_internal(decoder, input_stream, false);
|
2006-12-04 05:45:50 +01:00
|
|
|
}
|
|
|
|
|
2009-03-08 20:16:53 +01:00
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Decode a flac file with embedded cue sheets
|
|
|
|
* @param const char* fname filename on fs
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
flac_container_decode(struct decoder* decoder,
|
|
|
|
const char* fname,
|
|
|
|
bool is_ogg)
|
|
|
|
{
|
|
|
|
unsigned int tnum = 0;
|
|
|
|
FLAC__uint64 t_start = 0;
|
|
|
|
FLAC__uint64 t_end = 0;
|
|
|
|
FLAC__uint64 track_time = 0;
|
|
|
|
FLAC__StreamMetadata* cs = NULL;
|
|
|
|
|
|
|
|
flac_decoder *flac_dec;
|
|
|
|
struct flac_data data;
|
|
|
|
const char *err = NULL;
|
|
|
|
|
|
|
|
char* pathname = g_strdup(fname);
|
|
|
|
char* slash = strrchr(pathname, '/');
|
|
|
|
*slash = '\0';
|
|
|
|
|
|
|
|
tnum = flac_vtrack_tnum(fname);
|
|
|
|
|
|
|
|
cs = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET);
|
|
|
|
|
|
|
|
FLAC__metadata_get_cuesheet(pathname, &cs);
|
|
|
|
|
|
|
|
if (cs != NULL)
|
|
|
|
{
|
|
|
|
if (cs->data.cue_sheet.tracks != NULL
|
|
|
|
&& (tnum <= cs->data.cue_sheet.num_tracks - 1))
|
|
|
|
{
|
|
|
|
t_start = cs->data.cue_sheet.tracks[tnum - 1].offset;
|
2009-03-28 10:13:44 +01:00
|
|
|
t_end = cs->data.cue_sheet.tracks[tnum].offset;
|
|
|
|
track_time = cs->data.cue_sheet.tracks[tnum].offset
|
2009-03-08 20:16:53 +01:00
|
|
|
- cs->data.cue_sheet.tracks[tnum - 1].offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
FLAC__metadata_object_delete(cs);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_free(pathname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flac_dec = flac_new()))
|
|
|
|
{
|
|
|
|
g_free(pathname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
flac_data_init(&data, decoder, NULL);
|
|
|
|
|
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
|
|
|
if(!FLAC__stream_decoder_set_metadata_respond(flac_dec, FLAC__METADATA_TYPE_VORBIS_COMMENT))
|
|
|
|
{
|
|
|
|
g_debug("Failed to set metadata respond\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
if (is_ogg)
|
|
|
|
{
|
|
|
|
if (FLAC__stream_decoder_init_ogg_file( flac_dec,
|
|
|
|
pathname,
|
|
|
|
flac_write_cb,
|
|
|
|
flacMetadata,
|
|
|
|
flac_error_cb,
|
|
|
|
(void*) &data )
|
|
|
|
!= FLAC__STREAM_DECODER_INIT_STATUS_OK )
|
|
|
|
{
|
|
|
|
err = "doing Ogg init()";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (FLAC__stream_decoder_init_file( flac_dec,
|
|
|
|
pathname,
|
|
|
|
flac_write_cb,
|
|
|
|
flacMetadata,
|
|
|
|
flac_error_cb,
|
|
|
|
(void*) &data )
|
|
|
|
!= FLAC__STREAM_DECODER_INIT_STATUS_OK )
|
|
|
|
{
|
|
|
|
err = "doing init()";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!flac_process_metadata(flac_dec))
|
|
|
|
{
|
|
|
|
err = "problem reading metadata";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!audio_format_valid(&data.audio_format))
|
|
|
|
{
|
|
|
|
g_warning("Invalid audio format: %u:%u:%u\n",
|
|
|
|
data.audio_format.sample_rate,
|
|
|
|
data.audio_format.bits,
|
|
|
|
data.audio_format.channels);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set track time (order is important: after stream init)
|
2009-03-28 10:13:44 +01:00
|
|
|
data.total_time = ((float)track_time / (float)data.audio_format.sample_rate);
|
2009-03-08 20:16:53 +01:00
|
|
|
data.position = 0;
|
|
|
|
|
|
|
|
decoder_initialized(decoder, &data.audio_format,
|
|
|
|
true, data.total_time);
|
|
|
|
|
|
|
|
// seek to song start (order is important: after decoder init)
|
|
|
|
flac_seek_absolute(flac_dec, (FLAC__uint64)t_start);
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (!flac_process_single(flac_dec))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// we only need to break at the end of track if we are in "cue mode"
|
|
|
|
if (data.time >= data.total_time)
|
|
|
|
{
|
|
|
|
flacPrintErroredState(flac_get_state(flac_dec));
|
|
|
|
flac_finish(flac_dec);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decoder_get_command(decoder) == DECODE_COMMAND_SEEK)
|
|
|
|
{
|
|
|
|
FLAC__uint64 seek_sample = t_start +
|
|
|
|
(decoder_seek_where(decoder) * data.audio_format.sample_rate);
|
|
|
|
|
|
|
|
//if (seek_sample >= t_start && seek_sample <= t_end && data.total_time > 30)
|
|
|
|
if (seek_sample >= t_start && seek_sample <= t_end)
|
|
|
|
{
|
|
|
|
if (flac_seek_absolute(flac_dec, (FLAC__uint64)seek_sample))
|
|
|
|
{
|
|
|
|
data.time = (float)(seek_sample - t_start) /
|
|
|
|
data.audio_format.sample_rate;
|
|
|
|
data.position = 0;
|
|
|
|
|
|
|
|
decoder_command_finished(decoder);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
decoder_seek_error(decoder);
|
|
|
|
//decoder_command_finished(decoder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (flac_get_state(flac_dec) == flac_decoder_eof)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decoder_get_command(decoder) != DECODE_COMMAND_STOP)
|
|
|
|
{
|
|
|
|
flacPrintErroredState(flac_get_state(flac_dec));
|
|
|
|
flac_finish(flac_dec);
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (pathname)
|
|
|
|
g_free(pathname);
|
|
|
|
|
|
|
|
if (data.replay_gain_info)
|
|
|
|
replay_gain_info_free(data.replay_gain_info);
|
|
|
|
|
|
|
|
if (flac_dec)
|
|
|
|
flac_delete(flac_dec);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
g_warning("%s\n", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Open a flac file for decoding
|
|
|
|
* @param const char* fname filename on fs
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
flac_filedecode_internal(struct decoder* decoder,
|
|
|
|
const char* fname,
|
|
|
|
bool is_ogg)
|
|
|
|
{
|
|
|
|
flac_decoder *flac_dec;
|
|
|
|
struct flac_data data;
|
|
|
|
const char *err = NULL;
|
|
|
|
unsigned int flac_err_state = 0;
|
|
|
|
|
|
|
|
if (!(flac_dec = flac_new()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
flac_data_init(&data, decoder, NULL);
|
|
|
|
|
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
|
|
|
if(!FLAC__stream_decoder_set_metadata_respond(flac_dec, FLAC__METADATA_TYPE_VORBIS_COMMENT))
|
|
|
|
{
|
|
|
|
g_debug("Failed to set metadata respond\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
if (is_ogg)
|
|
|
|
{
|
|
|
|
if ( (flac_err_state = FLAC__stream_decoder_init_ogg_file( flac_dec,
|
|
|
|
fname,
|
|
|
|
flac_write_cb,
|
|
|
|
flacMetadata,
|
|
|
|
flac_error_cb,
|
|
|
|
(void*) &data ))
|
|
|
|
== FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE)
|
|
|
|
{
|
|
|
|
flac_container_decode(decoder, fname, is_ogg);
|
|
|
|
}
|
|
|
|
else if (flac_err_state != FLAC__STREAM_DECODER_INIT_STATUS_OK)
|
|
|
|
{
|
|
|
|
err = "doing Ogg init()";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( (flac_err_state = FLAC__stream_decoder_init_file( flac_dec,
|
|
|
|
fname,
|
|
|
|
flac_write_cb,
|
|
|
|
flacMetadata,
|
|
|
|
flac_error_cb,
|
|
|
|
(void*) &data ))
|
|
|
|
== FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE)
|
|
|
|
{
|
|
|
|
flac_container_decode(decoder, fname, is_ogg);
|
|
|
|
}
|
|
|
|
else if (flac_err_state != FLAC__STREAM_DECODER_INIT_STATUS_OK)
|
|
|
|
{
|
|
|
|
err = "doing init()";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!flac_process_metadata(flac_dec))
|
|
|
|
{
|
|
|
|
err = "problem reading metadata";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!audio_format_valid(&data.audio_format))
|
|
|
|
{
|
|
|
|
g_warning("Invalid audio format: %u:%u:%u\n",
|
|
|
|
data.audio_format.sample_rate,
|
|
|
|
data.audio_format.bits,
|
|
|
|
data.audio_format.channels);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder_initialized(decoder, &data.audio_format,
|
|
|
|
true, data.total_time);
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (!flac_process_single(flac_dec))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (decoder_get_command(decoder) == DECODE_COMMAND_SEEK)
|
|
|
|
{
|
|
|
|
FLAC__uint64 seek_sample = decoder_seek_where(decoder) *
|
|
|
|
data.audio_format.sample_rate + 0.5;
|
|
|
|
if (flac_seek_absolute(flac_dec, seek_sample))
|
|
|
|
{
|
|
|
|
data.time = ((float)seek_sample) /
|
|
|
|
data.audio_format.sample_rate;
|
|
|
|
data.position = 0;
|
|
|
|
decoder_command_finished(decoder);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
decoder_seek_error(decoder);
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (flac_get_state(flac_dec) == flac_decoder_eof)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decoder_get_command(decoder) != DECODE_COMMAND_STOP)
|
|
|
|
{
|
|
|
|
flacPrintErroredState(flac_get_state(flac_dec));
|
|
|
|
flac_finish(flac_dec);
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (data.replay_gain_info)
|
|
|
|
replay_gain_info_free(data.replay_gain_info);
|
|
|
|
|
|
|
|
if (flac_dec)
|
|
|
|
flac_delete(flac_dec);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
g_warning("%s\n", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief wrapper function for
|
|
|
|
* flac_filedecode_internal method
|
|
|
|
* for decoding without ogg
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
flac_filedecode(struct decoder *decoder, const char *fname)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(fname, &st) < 0) {
|
|
|
|
flac_container_decode(decoder, fname, false);
|
|
|
|
} else
|
|
|
|
flac_filedecode_internal(decoder, fname, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* FLAC_API_VERSION_CURRENT >= 7 */
|
|
|
|
|
2008-11-10 14:39:42 +01:00
|
|
|
#ifndef HAVE_OGGFLAC
|
|
|
|
|
2008-11-09 22:01:47 +01:00
|
|
|
static bool
|
2009-02-15 18:34:14 +01:00
|
|
|
oggflac_init(G_GNUC_UNUSED const struct config_param *param)
|
2008-11-09 22:01:47 +01:00
|
|
|
{
|
2008-11-10 14:39:42 +01:00
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
2008-11-09 22:01:47 +01:00
|
|
|
return !!FLAC_API_SUPPORTS_OGG_FLAC;
|
2008-11-10 14:39:42 +01:00
|
|
|
#else
|
|
|
|
/* disable oggflac when libflac is too old */
|
|
|
|
return false;
|
|
|
|
#endif
|
2008-11-09 22:01:47 +01:00
|
|
|
}
|
|
|
|
|
2008-11-10 14:39:42 +01:00
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
static struct tag *
|
|
|
|
oggflac_tag_dup(const char *file)
|
2006-12-04 05:45:50 +01:00
|
|
|
{
|
2008-08-29 09:38:11 +02:00
|
|
|
struct tag *ret = NULL;
|
2006-12-04 05:45:50 +01:00
|
|
|
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);
|
2009-01-15 22:43:39 +01:00
|
|
|
|
|
|
|
ret = tag_new();
|
2006-12-04 05:45:50 +01:00
|
|
|
do {
|
|
|
|
if (!(block = FLAC__metadata_iterator_get_block(it)))
|
|
|
|
break;
|
|
|
|
if (block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
|
2009-03-15 23:31:07 +01:00
|
|
|
flac_vorbis_comments_to_tag(ret, NULL, block);
|
2006-12-04 05:45:50 +01:00
|
|
|
} else if (block->type == FLAC__METADATA_TYPE_STREAMINFO) {
|
|
|
|
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);
|
2009-01-15 22:43:39 +01:00
|
|
|
|
|
|
|
if (!tag_is_defined(ret)) {
|
|
|
|
tag_free(ret);
|
|
|
|
ret = NULL;
|
|
|
|
}
|
|
|
|
|
2006-12-04 05:45:50 +01:00
|
|
|
out:
|
|
|
|
FLAC__metadata_chain_delete(chain);
|
2006-12-04 05:45:41 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-11 17:13:44 +01:00
|
|
|
static void
|
2009-01-15 19:50:28 +01:00
|
|
|
oggflac_decode(struct decoder *decoder, struct input_stream *input_stream)
|
2006-12-04 05:45:50 +01:00
|
|
|
{
|
2009-01-15 19:50:28 +01:00
|
|
|
if (ogg_stream_type_detect(input_stream) != FLAC)
|
2008-11-11 17:13:44 +01:00
|
|
|
return;
|
2006-12-04 05:45:50 +01:00
|
|
|
|
2008-11-10 15:07:01 +01:00
|
|
|
/* rewind the stream, because ogg_stream_type_detect() has
|
|
|
|
moved it */
|
2009-01-15 19:50:28 +01:00
|
|
|
input_stream_seek(input_stream, 0, SEEK_SET);
|
2008-11-10 15:07:01 +01:00
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
flac_decode_internal(decoder, input_stream, true);
|
2006-12-04 05:45:50 +01:00
|
|
|
}
|
|
|
|
|
2008-11-01 14:55:23 +01:00
|
|
|
static const char *const oggflac_suffixes[] = { "ogg", "oga", NULL };
|
|
|
|
static const char *const oggflac_mime_types[] = {
|
|
|
|
"audio/x-flac+ogg",
|
|
|
|
"application/ogg",
|
|
|
|
"application/x-ogg",
|
|
|
|
NULL
|
|
|
|
};
|
2006-12-04 05:45:50 +01:00
|
|
|
|
2008-11-10 14:39:42 +01:00
|
|
|
#endif /* FLAC_API_VERSION_CURRENT >= 7 */
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
const struct decoder_plugin oggflac_decoder_plugin = {
|
2008-10-17 21:57:09 +02:00
|
|
|
.name = "oggflac",
|
2008-11-09 22:01:47 +01:00
|
|
|
.init = oggflac_init,
|
2008-11-10 14:39:42 +01:00
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
2008-10-17 21:57:09 +02:00
|
|
|
.stream_decode = oggflac_decode,
|
|
|
|
.tag_dup = oggflac_tag_dup,
|
|
|
|
.suffixes = oggflac_suffixes,
|
|
|
|
.mime_types = oggflac_mime_types
|
2008-11-10 14:39:42 +01:00
|
|
|
#endif
|
2008-10-17 21:57:09 +02:00
|
|
|
};
|
2006-12-04 05:45:50 +01:00
|
|
|
|
2008-11-10 14:39:42 +01:00
|
|
|
#endif /* HAVE_OGGFLAC */
|
2006-12-04 05:45:50 +01:00
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
static const char *const flac_suffixes[] = { "flac", NULL };
|
2008-11-01 14:55:23 +01:00
|
|
|
static const char *const flac_mime_types[] = {
|
|
|
|
"audio/x-flac", "application/x-flac", NULL
|
|
|
|
};
|
2004-05-31 03:54:10 +02:00
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
const struct decoder_plugin flac_decoder_plugin = {
|
2008-09-29 15:55:17 +02:00
|
|
|
.name = "flac",
|
|
|
|
.stream_decode = flac_decode,
|
2009-03-08 20:16:53 +01:00
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
|
|
|
.file_decode = flac_filedecode,
|
|
|
|
#endif /* FLAC_API_VERSION_CURRENT >= 7 */
|
2009-01-15 19:50:28 +01:00
|
|
|
.tag_dup = flac_tag_dup,
|
|
|
|
.suffixes = flac_suffixes,
|
2009-03-08 20:16:53 +01:00
|
|
|
.mime_types = flac_mime_types,
|
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
|
|
|
.container_scan = flac_cue_track,
|
|
|
|
#endif /* FLAC_API_VERSION_CURRENT >= 7 */
|
2004-05-31 03:54:10 +02:00
|
|
|
};
|