2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 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-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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 22:44:23 +02:00
|
|
|
bool
|
2016-07-10 21:50:38 +02:00
|
|
|
FlacDecoder::Initialize(unsigned sample_rate, unsigned bits_per_sample,
|
|
|
|
unsigned channels, FLAC__uint64 total_frames)
|
2016-07-08 22:44:23 +02:00
|
|
|
{
|
|
|
|
assert(!initialized);
|
|
|
|
assert(!unsupported);
|
|
|
|
|
|
|
|
::Error error;
|
|
|
|
if (!audio_format_init_checked(audio_format,
|
|
|
|
sample_rate,
|
|
|
|
flac_sample_format(bits_per_sample),
|
|
|
|
channels, error)) {
|
|
|
|
LogError(error);
|
|
|
|
unsupported = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame_size = audio_format.GetFrameSize();
|
|
|
|
|
|
|
|
const auto duration = total_frames > 0
|
|
|
|
? SignedSongTime::FromScale<uint64_t>(total_frames,
|
|
|
|
audio_format.sample_rate)
|
|
|
|
: SignedSongTime::Negative();
|
|
|
|
|
2016-07-10 21:53:47 +02:00
|
|
|
decoder_initialized(*GetDecoder(), audio_format,
|
|
|
|
GetInputStream().IsSeekable(),
|
2016-07-08 22:44:23 +02:00
|
|
|
duration);
|
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-01-06 09:00:32 +01:00
|
|
|
static void
|
2016-07-10 21:50:38 +02:00
|
|
|
flac_got_stream_info(FlacDecoder *data,
|
2010-01-06 09:00:32 +01:00
|
|
|
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
|
|
|
|
2016-07-08 22:44:23 +02:00
|
|
|
data->Initialize(stream_info->sample_rate,
|
|
|
|
stream_info->bits_per_sample,
|
|
|
|
stream_info->channels,
|
|
|
|
stream_info->total_samples);
|
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,
|
2016-07-10 21:50:38 +02:00
|
|
|
FlacDecoder *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:
|
2014-09-24 22:34:08 +02:00
|
|
|
if (flac_parse_replay_gain(rgi, block->data.vorbis_comment))
|
2016-07-10 21:53:47 +02:00
|
|
|
decoder_replay_gain(*data->GetDecoder(), &rgi);
|
2012-09-25 11:08:16 +02:00
|
|
|
|
2016-07-10 21:53:47 +02:00
|
|
|
decoder_mixramp(*data->GetDecoder(),
|
2014-09-24 22:50:28 +02:00
|
|
|
flac_parse_mixramp(block->data.vorbis_comment));
|
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
|
2016-07-10 21:50:38 +02:00
|
|
|
flac_got_first_frame(FlacDecoder *data, const FLAC__FrameHeader *header)
|
2010-01-06 09:54:09 +01:00
|
|
|
{
|
|
|
|
if (data->unsupported)
|
|
|
|
return false;
|
|
|
|
|
2016-07-08 22:44:23 +02:00
|
|
|
return data->Initialize(header->sample_rate,
|
|
|
|
header->bits_per_sample,
|
|
|
|
header->channels,
|
|
|
|
/* unknown duration */
|
|
|
|
0);
|
2010-01-06 09:54:09 +01:00
|
|
|
}
|
|
|
|
|
2016-07-10 21:47:17 +02:00
|
|
|
FLAC__uint64
|
|
|
|
FlacDecoder::GetDeltaPosition(const FLAC__StreamDecoder &sd)
|
|
|
|
{
|
|
|
|
FLAC__uint64 nbytes;
|
|
|
|
if (!FLAC__stream_decoder_get_decode_position(&sd, &nbytes))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (position > 0 && nbytes > position) {
|
|
|
|
nbytes -= position;
|
|
|
|
position += nbytes;
|
|
|
|
} else {
|
|
|
|
position = nbytes;
|
|
|
|
nbytes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2008-09-23 23:59:54 +02:00
|
|
|
FLAC__StreamDecoderWriteStatus
|
2016-07-10 21:50:38 +02:00
|
|
|
flac_common_write(FlacDecoder *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;
|
|
|
|
|
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);
|
|
|
|
|
2016-07-08 22:40:31 +02:00
|
|
|
unsigned bit_rate = nbytes * 8 * frame->header.sample_rate /
|
|
|
|
(1000 * frame->header.blocksize);
|
2009-11-11 19:52:14 +01:00
|
|
|
|
2016-07-10 21:53:47 +02:00
|
|
|
auto cmd = decoder_data(*data->GetDecoder(), data->GetInputStream(),
|
2013-09-27 12:11:37 +02:00
|
|
|
buffer, buffer_size,
|
|
|
|
bit_rate);
|
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;
|
|
|
|
}
|