2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 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"
|
2013-05-05 14:19:04 +02:00
|
|
|
#include "FlacCommon.hxx"
|
|
|
|
#include "FlacMetadata.hxx"
|
|
|
|
#include "FlacPcm.hxx"
|
2013-07-29 07:50:08 +02:00
|
|
|
#include "CheckAudioFormat.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2009-01-01 18:09:24 +01:00
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
flac_data::flac_data(Decoder &_decoder,
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream &_input_stream)
|
2013-10-21 21:12:37 +02:00
|
|
|
:FlacInput(_input_stream, &_decoder),
|
2012-10-02 19:27:30 +02:00
|
|
|
initialized(false), unsupported(false),
|
2012-10-02 19:47:31 +02:00
|
|
|
total_frames(0), first_frame(0), next_frame(0), position(0),
|
2013-07-31 00:26:55 +02:00
|
|
|
decoder(_decoder), input_stream(_input_stream)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
static SampleFormat
|
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:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::S8;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
|
|
|
case 16:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::S16;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
|
|
|
case 24:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::S24_P32;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
|
|
|
case 32:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::S32;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
|
|
|
default:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::UNDEFINED;
|
2009-11-10 17:11:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2013-08-03 21:00:50 +02:00
|
|
|
if (!audio_format_init_checked(data->audio_format,
|
2010-01-06 09:00:32 +01:00
|
|
|
stream_info->sample_rate,
|
2010-01-06 09:55:20 +01:00
|
|
|
flac_sample_format(stream_info->bits_per_sample),
|
2013-08-10 18:02:44 +02:00
|
|
|
stream_info->channels, error)) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(error);
|
2010-01-06 09:00:32 +01:00
|
|
|
data->unsupported = true;
|
|
|
|
return;
|
2009-11-11 20:34:59 +01:00
|
|
|
}
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
data->frame_size = data->audio_format.GetFrameSize();
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2013-10-25 19:09:22 +02:00
|
|
|
ReplayGainInfo 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:
|
2013-10-25 19:09:22 +02:00
|
|
|
if (flac_parse_replay_gain(rgi, block))
|
2013-01-05 02:05:50 +01:00
|
|
|
decoder_replay_gain(data->decoder, &rgi);
|
2012-09-25 11:08:16 +02:00
|
|
|
|
2013-10-26 14:19:34 +02:00
|
|
|
decoder_mixramp(data->decoder, flac_parse_mixramp(block));
|
2009-03-01 14:07:23 +01:00
|
|
|
|
2014-01-08 19:42:04 +01:00
|
|
|
data->tag = flac_vorbis_comments_to_tag(&block->data.vorbis_comment);
|
|
|
|
break;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2013-08-03 21:00:50 +02:00
|
|
|
if (!audio_format_init_checked(data->audio_format,
|
2010-01-06 09:54:09 +01:00
|
|
|
header->sample_rate,
|
|
|
|
flac_sample_format(header->bits_per_sample),
|
2013-08-10 18:02:44 +02:00
|
|
|
header->channels, error)) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(error);
|
2010-01-06 09:54:09 +01:00
|
|
|
data->unsupported = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
data->frame_size = data->audio_format.GetFrameSize();
|
2010-01-06 09:54:09 +01:00
|
|
|
|
2014-08-29 20:52:39 +02:00
|
|
|
const auto duration = SongTime::FromScale<uint64_t>(data->total_frames,
|
|
|
|
data->audio_format.sample_rate);
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
decoder_initialized(data->decoder, data->audio_format,
|
2014-05-11 18:34:09 +02:00
|
|
|
data->input_stream.IsSeekable(),
|
2014-08-29 20:52:39 +02:00
|
|
|
duration);
|
2010-01-06 09:54:09 +01:00
|
|
|
|
|
|
|
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
|
|
|
{
|
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;
|
2013-07-29 08:10:10 +02:00
|
|
|
buffer = data->buffer.Get(buffer_size);
|
2009-11-10 21:46:10 +01:00
|
|
|
|
2009-11-11 20:26:56 +01:00
|
|
|
flac_convert(buffer, frame->header.channels,
|
2013-08-03 21:00:50 +02: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;
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
auto cmd = decoder_data(data->decoder, data->input_stream,
|
|
|
|
buffer, buffer_size,
|
|
|
|
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) {
|
2013-09-27 12:11:37 +02:00
|
|
|
case DecoderCommand::NONE:
|
|
|
|
case DecoderCommand::START:
|
2009-11-10 21:46:10 +01:00
|
|
|
break;
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
case DecoderCommand::STOP:
|
2009-11-10 21:46:10 +01:00
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
2008-09-23 23:59:54 +02:00
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
case DecoderCommand::SEEK:
|
2009-11-10 21:46:10 +01:00
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
2008-09-23 23:59:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
|
|
|
}
|