2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2010-01-01 05:55:13 +01:00
|
|
|
* Copyright (C) 2003-2010 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2006-03-16 07:52:46 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Common data structures and functions used by FLAC and OggFLAC
|
2006-03-16 07:52:46 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2006-03-16 07:52:46 +01:00
|
|
|
#include "_flac_common.h"
|
2009-11-11 07:50:40 +01:00
|
|
|
#include "flac_metadata.h"
|
2009-11-11 07:59:22 +01:00
|
|
|
#include "flac_pcm.h"
|
2009-11-10 19:01:38 +01:00
|
|
|
#include "audio_check.h"
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2009-01-01 18:09:24 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2009-01-15 22:45:28 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
void
|
|
|
|
flac_data_init(struct flac_data *data, struct decoder * decoder,
|
|
|
|
struct input_stream *input_stream)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2009-11-10 21:46:10 +01:00
|
|
|
pcm_buffer_init(&data->buffer);
|
|
|
|
|
2010-01-06 09:00:32 +01:00
|
|
|
data->unsupported = false;
|
2010-01-05 21:46:12 +01:00
|
|
|
data->initialized = false;
|
|
|
|
data->total_frames = 0;
|
2009-11-11 20:18:39 +01:00
|
|
|
data->first_frame = 0;
|
2009-11-11 15:09:24 +01:00
|
|
|
data->next_frame = 0;
|
|
|
|
|
2006-03-16 07:52:46 +01:00
|
|
|
data->position = 0;
|
2008-08-26 08:27:04 +02:00
|
|
|
data->decoder = decoder;
|
2009-01-15 19:50:28 +01:00
|
|
|
data->input_stream = input_stream;
|
2006-03-16 07:52:46 +01:00
|
|
|
data->tag = NULL;
|
|
|
|
}
|
|
|
|
|
2009-11-10 21:42:15 +01:00
|
|
|
void
|
|
|
|
flac_data_deinit(struct flac_data *data)
|
|
|
|
{
|
2009-11-10 21:46:10 +01:00
|
|
|
pcm_buffer_deinit(&data->buffer);
|
|
|
|
|
2009-11-10 21:42:15 +01:00
|
|
|
if (data->tag != NULL)
|
|
|
|
tag_free(data->tag);
|
|
|
|
}
|
|
|
|
|
2009-11-10 17:11:34 +01:00
|
|
|
static enum sample_format
|
2010-01-06 09:55:20 +01:00
|
|
|
flac_sample_format(unsigned bits_per_sample)
|
2009-11-10 17:11:34 +01:00
|
|
|
{
|
2010-01-06 09:55:20 +01:00
|
|
|
switch (bits_per_sample) {
|
2009-11-10 17:11:34 +01:00
|
|
|
case 8:
|
|
|
|
return SAMPLE_FORMAT_S8;
|
|
|
|
|
|
|
|
case 16:
|
|
|
|
return SAMPLE_FORMAT_S16;
|
|
|
|
|
|
|
|
case 24:
|
|
|
|
return SAMPLE_FORMAT_S24_P32;
|
|
|
|
|
|
|
|
case 32:
|
|
|
|
return SAMPLE_FORMAT_S32;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return SAMPLE_FORMAT_UNDEFINED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-06 09:00:32 +01:00
|
|
|
static void
|
|
|
|
flac_got_stream_info(struct flac_data *data,
|
|
|
|
const FLAC__StreamMetadata_StreamInfo *stream_info)
|
|
|
|
{
|
2010-01-05 21:46:12 +01:00
|
|
|
if (data->initialized || data->unsupported)
|
2010-01-06 09:00:32 +01:00
|
|
|
return;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
2010-01-06 09:00:32 +01:00
|
|
|
GError *error = NULL;
|
|
|
|
if (!audio_format_init_checked(&data->audio_format,
|
|
|
|
stream_info->sample_rate,
|
2010-01-06 09:55:20 +01:00
|
|
|
flac_sample_format(stream_info->bits_per_sample),
|
2010-01-06 09:00:32 +01:00
|
|
|
stream_info->channels, &error)) {
|
2009-11-10 19:01:38 +01:00
|
|
|
g_warning("%s", error->message);
|
|
|
|
g_error_free(error);
|
2010-01-06 09:00:32 +01:00
|
|
|
data->unsupported = true;
|
|
|
|
return;
|
2009-11-11 20:34:59 +01:00
|
|
|
}
|
|
|
|
|
2010-01-06 09:00:32 +01:00
|
|
|
data->frame_size = audio_format_frame_size(&data->audio_format);
|
2009-11-11 20:34:59 +01:00
|
|
|
|
2010-01-05 21:46:12 +01:00
|
|
|
if (data->total_frames == 0)
|
|
|
|
data->total_frames = stream_info->total_samples;
|
|
|
|
|
|
|
|
decoder_initialized(data->decoder, &data->audio_format,
|
|
|
|
data->input_stream->seekable,
|
|
|
|
(float)data->total_frames /
|
|
|
|
(float)data->audio_format.sample_rate);
|
|
|
|
|
|
|
|
data->initialized = true;
|
2009-11-11 20:34:59 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
|
2009-01-15 19:50:28 +01:00
|
|
|
struct flac_data *data)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2010-01-06 09:00:32 +01:00
|
|
|
if (data->unsupported)
|
|
|
|
return;
|
|
|
|
|
2010-02-14 20:36:31 +01:00
|
|
|
struct replay_gain_info rgi;
|
2010-01-03 22:44:23 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (block->type) {
|
2006-03-16 07:52:46 +01:00
|
|
|
case FLAC__METADATA_TYPE_STREAMINFO:
|
2010-01-06 09:00:32 +01:00
|
|
|
flac_got_stream_info(data, &block->data.stream_info);
|
2006-03-16 07:52:46 +01:00
|
|
|
break;
|
2009-11-11 20:34:59 +01:00
|
|
|
|
2006-03-16 07:52:46 +01:00
|
|
|
case FLAC__METADATA_TYPE_VORBIS_COMMENT:
|
2010-02-14 20:36:31 +01:00
|
|
|
if (flac_parse_replay_gain(&rgi, block))
|
|
|
|
decoder_replay_gain(data->decoder, &rgi);
|
2009-03-01 14:07:23 +01:00
|
|
|
|
|
|
|
if (data->tag != NULL)
|
2009-11-10 21:58:19 +01:00
|
|
|
flac_vorbis_comments_to_tag(data->tag, NULL,
|
|
|
|
&block->data.vorbis_comment);
|
2009-03-01 14:07:23 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
default:
|
|
|
|
break;
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void flac_error_common_cb(const char *plugin,
|
|
|
|
const FLAC__StreamDecoderErrorStatus status,
|
2009-01-15 19:50:28 +01:00
|
|
|
struct flac_data *data)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2008-08-26 08:27:07 +02:00
|
|
|
if (decoder_get_command(data->decoder) == DECODE_COMMAND_STOP)
|
2006-07-20 18:02:40 +02:00
|
|
|
return;
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (status) {
|
2006-03-16 07:52:46 +01:00
|
|
|
case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
|
2008-11-21 20:15:50 +01:00
|
|
|
g_warning("%s lost sync\n", plugin);
|
2006-03-16 07:52:46 +01:00
|
|
|
break;
|
|
|
|
case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
|
2008-11-21 20:15:50 +01:00
|
|
|
g_warning("bad %s header\n", plugin);
|
2006-03-16 07:52:46 +01:00
|
|
|
break;
|
|
|
|
case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
|
2008-11-21 20:15:50 +01:00
|
|
|
g_warning("%s crc mismatch\n", plugin);
|
2006-03-16 07:52:46 +01:00
|
|
|
break;
|
|
|
|
default:
|
2008-11-21 20:15:50 +01:00
|
|
|
g_warning("unknown %s error\n", plugin);
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-06 09:54:09 +01:00
|
|
|
/**
|
|
|
|
* This function attempts to call decoder_initialized() in case there
|
|
|
|
* was no STREAMINFO block. This is allowed for nonseekable streams,
|
|
|
|
* where the server sends us only a part of the file, without
|
|
|
|
* providing the STREAMINFO block from the beginning of the file
|
|
|
|
* (e.g. when seeking with SqueezeBox Server).
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
flac_got_first_frame(struct flac_data *data, const FLAC__FrameHeader *header)
|
|
|
|
{
|
|
|
|
if (data->unsupported)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
GError *error = NULL;
|
|
|
|
if (!audio_format_init_checked(&data->audio_format,
|
|
|
|
header->sample_rate,
|
|
|
|
flac_sample_format(header->bits_per_sample),
|
|
|
|
header->channels, &error)) {
|
|
|
|
g_warning("%s", error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
data->unsupported = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->frame_size = audio_format_frame_size(&data->audio_format);
|
|
|
|
|
|
|
|
decoder_initialized(data->decoder, &data->audio_format,
|
|
|
|
data->input_stream->seekable,
|
|
|
|
(float)data->total_frames /
|
|
|
|
(float)data->audio_format.sample_rate);
|
|
|
|
|
|
|
|
data->initialized = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-23 23:59:54 +02:00
|
|
|
FLAC__StreamDecoderWriteStatus
|
2009-01-15 19:50:28 +01:00
|
|
|
flac_common_write(struct flac_data *data, const FLAC__Frame * frame,
|
2009-11-11 19:52:14 +01:00
|
|
|
const FLAC__int32 *const buf[],
|
|
|
|
FLAC__uint64 nbytes)
|
2008-09-23 23:59:54 +02:00
|
|
|
{
|
2008-09-23 23:59:55 +02:00
|
|
|
enum decoder_command cmd;
|
2009-11-10 21:46:10 +01:00
|
|
|
void *buffer;
|
2009-11-11 19:52:14 +01:00
|
|
|
unsigned bit_rate;
|
2009-11-10 21:46:10 +01:00
|
|
|
|
2010-01-06 09:54:09 +01:00
|
|
|
if (!data->initialized && !flac_got_first_frame(data, &frame->header))
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
|
|
|
|
|
|
size_t buffer_size = frame->header.blocksize * data->frame_size;
|
2009-11-10 21:46:10 +01:00
|
|
|
buffer = pcm_buffer_get(&data->buffer, buffer_size);
|
|
|
|
|
2009-11-11 20:26:56 +01:00
|
|
|
flac_convert(buffer, frame->header.channels,
|
2010-01-06 09:00:32 +01:00
|
|
|
data->audio_format.format, buf,
|
2009-11-10 21:46:10 +01:00
|
|
|
0, frame->header.blocksize);
|
|
|
|
|
2009-11-11 19:52:14 +01:00
|
|
|
if (nbytes > 0)
|
|
|
|
bit_rate = nbytes * 8 * frame->header.sample_rate /
|
|
|
|
(1000 * frame->header.blocksize);
|
|
|
|
else
|
|
|
|
bit_rate = 0;
|
|
|
|
|
2009-11-10 21:46:10 +01:00
|
|
|
cmd = decoder_data(data->decoder, data->input_stream,
|
|
|
|
buffer, buffer_size,
|
2010-01-03 22:44:23 +01:00
|
|
|
bit_rate);
|
2009-11-11 15:09:24 +01:00
|
|
|
data->next_frame += frame->header.blocksize;
|
2009-11-10 21:46:10 +01:00
|
|
|
switch (cmd) {
|
|
|
|
case DECODE_COMMAND_NONE:
|
|
|
|
case DECODE_COMMAND_START:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DECODE_COMMAND_STOP:
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
2008-09-23 23:59:54 +02:00
|
|
|
|
2009-11-10 21:46:10 +01:00
|
|
|
case DECODE_COMMAND_SEEK:
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
2008-09-23 23:59:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
|
|
|
}
|