2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2009-11-11 16:37:42 +01:00
|
|
|
#include "config.h" /* must be first for large file support */
|
2006-12-04 05:45:47 +01:00
|
|
|
#include "_flac_common.h"
|
2009-11-11 16:43:34 +01:00
|
|
|
#include "flac_compat.h"
|
2009-11-11 07:50:40 +01:00
|
|
|
#include "flac_metadata.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-11-11 16:43:34 +01:00
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
|
|
|
#include "_ogg_common.h"
|
|
|
|
#endif
|
|
|
|
|
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>
|
|
|
|
|
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-11-11 16:59:28 +01:00
|
|
|
static FLAC__StreamDecoderReadStatus
|
|
|
|
flac_read_cb(G_GNUC_UNUSED const FLAC__StreamDecoder *fd,
|
2009-01-15 19:50:28 +01:00
|
|
|
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 ||
|
2011-09-14 21:46:41 +02:00
|
|
|
input_stream_lock_eof(data->input_stream))
|
2009-11-11 16:59:28 +01:00
|
|
|
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
|
2006-12-04 05:45:44 +01:00
|
|
|
else
|
2009-11-11 16:59:28 +01:00
|
|
|
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
|
2006-12-04 05:45:44 +01:00
|
|
|
}
|
2008-08-26 08:27:15 +02:00
|
|
|
|
2009-11-11 16:59:28 +01:00
|
|
|
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
|
2004-05-05 01:39:02 +02:00
|
|
|
}
|
|
|
|
|
2009-11-11 16:59:28 +01:00
|
|
|
static FLAC__StreamDecoderSeekStatus
|
|
|
|
flac_seek_cb(G_GNUC_UNUSED const FLAC__StreamDecoder *fd,
|
2009-01-15 19:50:28 +01:00
|
|
|
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-11-11 21:02:52 +01:00
|
|
|
if (!data->input_stream->seekable)
|
|
|
|
return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
if (!input_stream_lock_seek(data->input_stream, offset, SEEK_SET,
|
|
|
|
NULL))
|
2009-11-11 16:59:28 +01:00
|
|
|
return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
|
2004-05-05 01:39:02 +02:00
|
|
|
|
2009-11-11 16:59:28 +01:00
|
|
|
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
|
2004-05-05 01:39:02 +02:00
|
|
|
}
|
|
|
|
|
2009-11-11 16:59:28 +01:00
|
|
|
static FLAC__StreamDecoderTellStatus
|
|
|
|
flac_tell_cb(G_GNUC_UNUSED const FLAC__StreamDecoder *fd,
|
2009-01-15 19:50:28 +01:00
|
|
|
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-11-11 21:02:52 +01:00
|
|
|
if (!data->input_stream->seekable)
|
2011-03-23 22:31:40 +01:00
|
|
|
return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
|
2009-11-11 21:02:52 +01:00
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
*offset = (long)(data->input_stream->offset);
|
2004-05-05 01:39:02 +02:00
|
|
|
|
2009-11-11 16:59:28 +01:00
|
|
|
return FLAC__STREAM_DECODER_TELL_STATUS_OK;
|
2004-05-05 01:39:02 +02:00
|
|
|
}
|
|
|
|
|
2009-11-11 16:59:28 +01:00
|
|
|
static FLAC__StreamDecoderLengthStatus
|
|
|
|
flac_length_cb(G_GNUC_UNUSED const FLAC__StreamDecoder *fd,
|
2009-01-15 19:50:28 +01:00
|
|
|
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)
|
2009-11-11 16:59:28 +01:00
|
|
|
return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
|
2008-11-16 20:42:08 +01:00
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
*length = (size_t) (data->input_stream->size);
|
2004-05-05 01:39:02 +02:00
|
|
|
|
2009-11-11 16:59:28 +01:00
|
|
|
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
|
2004-05-05 01:39:02 +02:00
|
|
|
}
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
static FLAC__bool
|
2009-11-11 16:59:28 +01:00
|
|
|
flac_eof_cb(G_GNUC_UNUSED const FLAC__StreamDecoder *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) ||
|
2011-09-14 21:46:41 +02:00
|
|
|
input_stream_lock_eof(data->input_stream);
|
2004-05-05 01:39:02 +02:00
|
|
|
}
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
static void
|
2009-11-11 16:59:28 +01:00
|
|
|
flac_error_cb(G_GNUC_UNUSED const FLAC__StreamDecoder *fd,
|
2009-01-15 19:50:28 +01:00
|
|
|
FLAC__StreamDecoderErrorStatus status, void *fdata)
|
2004-05-05 01:39:02 +02:00
|
|
|
{
|
2012-02-11 12:26:45 +01:00
|
|
|
flac_error_common_cb(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)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_OK:
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_SEEKING:
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM:
|
|
|
|
return;
|
2012-02-11 12:26:45 +01:00
|
|
|
|
2006-12-04 05:45:47 +01:00
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR:
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR:
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR:
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED:
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK:
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED:
|
2012-02-11 12:26:45 +01:00
|
|
|
break;
|
2006-12-04 05:45:47 +01:00
|
|
|
}
|
2008-11-21 20:15:50 +01:00
|
|
|
|
2012-02-11 12:26:45 +01:00
|
|
|
g_warning("%s\n", FLAC__SeekableStreamDecoderStateString[state]);
|
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-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;
|
2012-02-11 12:26:45 +01:00
|
|
|
|
2006-12-04 05:45:44 +01:00
|
|
|
case FLAC__STREAM_DECODER_OGG_ERROR:
|
|
|
|
case FLAC__STREAM_DECODER_SEEK_ERROR:
|
|
|
|
case FLAC__STREAM_DECODER_ABORTED:
|
|
|
|
case FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
|
|
|
|
case FLAC__STREAM_DECODER_UNINITIALIZED:
|
2012-02-11 12:26:45 +01:00
|
|
|
break;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2008-11-21 20:15:50 +01:00
|
|
|
|
2012-02-11 12:26:45 +01:00
|
|
|
g_warning("%s\n", FLAC__StreamDecoderStateString[state]);
|
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-11-11 16:59:28 +01:00
|
|
|
static void flacMetadata(G_GNUC_UNUSED const FLAC__StreamDecoder * 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
|
2009-11-11 16:59:28 +01:00
|
|
|
flac_write_cb(const FLAC__StreamDecoder *dec, const FLAC__Frame *frame,
|
2009-01-15 19:50:28 +01:00
|
|
|
const FLAC__int32 *const buf[], void *vdata)
|
2004-05-05 01:39:02 +02:00
|
|
|
{
|
2009-01-15 19:50:28 +01:00
|
|
|
struct flac_data *data = (struct flac_data *) vdata;
|
2009-11-11 19:52:14 +01:00
|
|
|
FLAC__uint64 nbytes = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2009-11-11 19:52:14 +01:00
|
|
|
if (FLAC__stream_decoder_get_decode_position(dec, &nbytes)) {
|
|
|
|
if (data->position > 0 && nbytes > data->position) {
|
|
|
|
nbytes -= data->position;
|
|
|
|
data->position += nbytes;
|
|
|
|
} else {
|
|
|
|
data->position = nbytes;
|
|
|
|
nbytes = 0;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
nbytes = 0;
|
|
|
|
|
|
|
|
return flac_common_write(data, frame, buf, nbytes);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-01-06 08:04:57 +01:00
|
|
|
return flac_tag_load(file, NULL);
|
2004-05-31 03:54:10 +02:00
|
|
|
}
|
|
|
|
|
2009-11-11 18:56:10 +01:00
|
|
|
/**
|
|
|
|
* Some glue code around FLAC__stream_decoder_new().
|
|
|
|
*/
|
|
|
|
static FLAC__StreamDecoder *
|
|
|
|
flac_decoder_new(void)
|
|
|
|
{
|
|
|
|
FLAC__StreamDecoder *sd = FLAC__stream_decoder_new();
|
|
|
|
if (sd == NULL) {
|
|
|
|
g_warning("FLAC__stream_decoder_new() failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
|
|
|
if(!FLAC__stream_decoder_set_metadata_respond(sd, FLAC__METADATA_TYPE_VORBIS_COMMENT))
|
|
|
|
g_debug("FLAC__stream_decoder_set_metadata_respond() has failed");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return sd;
|
|
|
|
}
|
|
|
|
|
2009-11-11 19:01:31 +01:00
|
|
|
static bool
|
|
|
|
flac_decoder_initialize(struct flac_data *data, FLAC__StreamDecoder *sd,
|
2010-01-05 21:46:12 +01:00
|
|
|
FLAC__uint64 duration)
|
2009-11-11 19:01:31 +01:00
|
|
|
{
|
2010-01-05 21:46:12 +01:00
|
|
|
data->total_frames = duration;
|
2009-11-11 20:34:59 +01:00
|
|
|
|
2009-11-11 19:01:31 +01:00
|
|
|
if (!FLAC__stream_decoder_process_until_end_of_metadata(sd)) {
|
|
|
|
g_warning("problem reading metadata");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-21 18:21:47 +01:00
|
|
|
if (data->initialized) {
|
2010-01-06 09:54:09 +01:00
|
|
|
/* done */
|
2010-03-21 18:21:47 +01:00
|
|
|
decoder_initialized(data->decoder, &data->audio_format,
|
|
|
|
data->input_stream->seekable,
|
|
|
|
(float)data->total_frames /
|
|
|
|
(float)data->audio_format.sample_rate);
|
2010-01-06 09:54:09 +01:00
|
|
|
return true;
|
2010-03-21 18:21:47 +01:00
|
|
|
}
|
2010-01-06 09:54:09 +01:00
|
|
|
|
|
|
|
if (data->input_stream->seekable)
|
|
|
|
/* allow the workaround below only for nonseekable
|
|
|
|
streams*/
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* no stream_info packet found; try to initialize the decoder
|
|
|
|
from the first frame header */
|
|
|
|
FLAC__stream_decoder_process_single(sd);
|
2010-01-05 21:46:12 +01:00
|
|
|
return data->initialized;
|
2009-11-11 19:01:31 +01:00
|
|
|
}
|
|
|
|
|
2009-11-11 13:59:45 +01:00
|
|
|
static void
|
2009-11-11 16:59:28 +01:00
|
|
|
flac_decoder_loop(struct flac_data *data, FLAC__StreamDecoder *flac_dec,
|
2009-11-11 13:59:45 +01:00
|
|
|
FLAC__uint64 t_start, FLAC__uint64 t_end)
|
|
|
|
{
|
|
|
|
struct decoder *decoder = data->decoder;
|
|
|
|
enum decoder_command cmd;
|
|
|
|
|
2009-11-11 20:18:39 +01:00
|
|
|
data->first_frame = t_start;
|
|
|
|
|
2009-11-11 13:59:45 +01:00
|
|
|
while (true) {
|
|
|
|
if (data->tag != NULL && !tag_is_empty(data->tag)) {
|
|
|
|
cmd = decoder_tag(data->decoder, data->input_stream,
|
|
|
|
data->tag);
|
|
|
|
tag_free(data->tag);
|
|
|
|
data->tag = tag_new();
|
|
|
|
} else
|
|
|
|
cmd = decoder_get_command(decoder);
|
|
|
|
|
|
|
|
if (cmd == DECODE_COMMAND_SEEK) {
|
|
|
|
FLAC__uint64 seek_sample = t_start +
|
|
|
|
decoder_seek_where(decoder) *
|
2010-01-06 09:00:32 +01:00
|
|
|
data->audio_format.sample_rate;
|
2009-11-11 13:59:45 +01:00
|
|
|
if (seek_sample >= t_start &&
|
|
|
|
(t_end == 0 || seek_sample <= t_end) &&
|
2009-11-11 16:59:28 +01:00
|
|
|
FLAC__stream_decoder_seek_absolute(flac_dec, seek_sample)) {
|
2009-11-11 13:59:45 +01:00
|
|
|
data->next_frame = seek_sample;
|
|
|
|
data->position = 0;
|
|
|
|
decoder_command_finished(decoder);
|
|
|
|
} else
|
|
|
|
decoder_seek_error(decoder);
|
|
|
|
} else if (cmd == DECODE_COMMAND_STOP ||
|
2009-11-11 16:59:28 +01:00
|
|
|
FLAC__stream_decoder_get_state(flac_dec) == FLAC__STREAM_DECODER_END_OF_STREAM)
|
2009-11-11 13:59:45 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (t_end != 0 && data->next_frame >= t_end)
|
|
|
|
/* end of this sub track */
|
|
|
|
break;
|
|
|
|
|
2012-02-11 12:47:43 +01:00
|
|
|
if (!FLAC__stream_decoder_process_single(flac_dec) &&
|
|
|
|
decoder_get_command(decoder) == DECODE_COMMAND_NONE) {
|
|
|
|
/* a failure that was not triggered by a
|
|
|
|
decoder command */
|
|
|
|
flacPrintErroredState(FLAC__stream_decoder_get_state(flac_dec));
|
|
|
|
break;
|
2009-11-11 13:59:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-11 11:50:26 +01:00
|
|
|
static FLAC__StreamDecoderInitStatus
|
|
|
|
stream_init_oggflac(FLAC__StreamDecoder *flac_dec, struct flac_data *data)
|
|
|
|
{
|
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
|
|
|
return FLAC__stream_decoder_init_ogg_stream(flac_dec,
|
|
|
|
flac_read_cb,
|
|
|
|
flac_seek_cb,
|
|
|
|
flac_tell_cb,
|
|
|
|
flac_length_cb,
|
|
|
|
flac_eof_cb,
|
|
|
|
flac_write_cb,
|
|
|
|
flacMetadata,
|
|
|
|
flac_error_cb,
|
|
|
|
data);
|
|
|
|
#else
|
|
|
|
(void)flac_dec;
|
|
|
|
(void)data;
|
|
|
|
|
|
|
|
return FLAC__STREAM_DECODER_INIT_STATUS_ERROR;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static FLAC__StreamDecoderInitStatus
|
|
|
|
stream_init_flac(FLAC__StreamDecoder *flac_dec, struct flac_data *data)
|
|
|
|
{
|
|
|
|
return FLAC__stream_decoder_init_stream(flac_dec,
|
|
|
|
flac_read_cb, flac_seek_cb,
|
|
|
|
flac_tell_cb, flac_length_cb,
|
|
|
|
flac_eof_cb, flac_write_cb,
|
|
|
|
flacMetadata,
|
|
|
|
flac_error_cb,
|
|
|
|
data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static FLAC__StreamDecoderInitStatus
|
|
|
|
stream_init(FLAC__StreamDecoder *flac_dec, struct flac_data *data, bool is_ogg)
|
|
|
|
{
|
|
|
|
return is_ogg
|
|
|
|
? stream_init_oggflac(flac_dec, data)
|
|
|
|
: stream_init_flac(flac_dec, data);
|
|
|
|
}
|
|
|
|
|
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-11-11 16:59:28 +01:00
|
|
|
FLAC__StreamDecoder *flac_dec;
|
2009-01-15 19:50:28 +01:00
|
|
|
struct flac_data data;
|
2006-12-04 05:45:41 +01:00
|
|
|
|
2009-11-11 18:56:10 +01:00
|
|
|
flac_dec = flac_decoder_new();
|
|
|
|
if (flac_dec == NULL)
|
2008-11-11 17:13:44 +01:00
|
|
|
return;
|
2009-11-11 18:56:10 +01:00
|
|
|
|
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
|
|
|
|
2012-02-11 11:50:26 +01:00
|
|
|
FLAC__StreamDecoderInitStatus status =
|
|
|
|
stream_init(flac_dec, &data, is_ogg);
|
|
|
|
if (status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
|
|
|
|
flac_data_deinit(&data);
|
|
|
|
FLAC__stream_decoder_delete(flac_dec);
|
2009-11-11 17:01:14 +01:00
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
2012-02-11 11:50:26 +01:00
|
|
|
g_warning("%s", FLAC__StreamDecoderInitStatusString[status]);
|
2009-11-11 17:01:14 +01:00
|
|
|
#endif
|
2012-02-11 11:50:26 +01:00
|
|
|
return;
|
2008-11-10 14:49:34 +01:00
|
|
|
}
|
|
|
|
|
2010-01-05 21:46:12 +01:00
|
|
|
if (!flac_decoder_initialize(&data, flac_dec, 0)) {
|
2009-11-11 19:01:31 +01:00
|
|
|
flac_data_deinit(&data);
|
2012-02-11 12:56:52 +01:00
|
|
|
FLAC__stream_decoder_finish(flac_dec);
|
2009-11-11 19:01:31 +01:00
|
|
|
FLAC__stream_decoder_delete(flac_dec);
|
|
|
|
return;
|
2008-11-21 20:27:30 +01:00
|
|
|
}
|
|
|
|
|
2009-11-11 13:59:45 +01:00
|
|
|
flac_decoder_loop(&data, flac_dec, 0, 0);
|
2006-12-04 05:45:41 +01:00
|
|
|
|
2009-11-10 21:42:15 +01:00
|
|
|
flac_data_deinit(&data);
|
2012-02-11 12:56:52 +01:00
|
|
|
|
|
|
|
FLAC__stream_decoder_finish(flac_dec);
|
2009-11-11 16:59:28 +01:00
|
|
|
FLAC__stream_decoder_delete(flac_dec);
|
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
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2012-02-11 11:50:26 +01:00
|
|
|
if (!(FLAC__metadata_chain_read_ogg(chain, file))) {
|
|
|
|
FLAC__metadata_chain_delete(chain);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-12-04 05:45:50 +01:00
|
|
|
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;
|
2009-11-11 00:05:14 +01:00
|
|
|
|
|
|
|
flac_tag_apply_metadata(ret, NULL, block);
|
2006-12-04 05:45:50 +01:00
|
|
|
} 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
|
|
|
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 */
|
2011-09-14 21:46:41 +02:00
|
|
|
input_stream_lock_seek(input_stream, 0, SEEK_SET, NULL);
|
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[] = {
|
|
|
|
"application/ogg",
|
|
|
|
"application/x-ogg",
|
2009-12-29 22:33:46 +01:00
|
|
|
"audio/ogg",
|
|
|
|
"audio/x-flac+ogg",
|
|
|
|
"audio/x-ogg",
|
2008-11-01 14:55:23 +01:00
|
|
|
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[] = {
|
2009-12-29 22:33:46 +01:00
|
|
|
"application/flac",
|
|
|
|
"application/x-flac",
|
|
|
|
"audio/flac",
|
|
|
|
"audio/x-flac",
|
|
|
|
NULL
|
2008-11-01 14:55:23 +01:00
|
|
|
};
|
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-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,
|
2004-05-31 03:54:10 +02:00
|
|
|
};
|