2009-11-17 19:41:35 +01:00
|
|
|
/*
|
2019-06-17 11:17:30 +02:00
|
|
|
* Copyright 2003-2019 The Music Player Daemon Project
|
2009-11-17 19:41:35 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-07-29 08:04:34 +02:00
|
|
|
#include "FlacEncoderPlugin.hxx"
|
2014-01-23 23:09:14 +01:00
|
|
|
#include "../EncoderAPI.hxx"
|
2013-08-03 21:00:50 +02:00
|
|
|
#include "AudioFormat.hxx"
|
2019-06-17 11:10:33 +02:00
|
|
|
#include "pcm/Buffer.hxx"
|
2018-07-16 19:50:07 +02:00
|
|
|
#include "config/Domain.hxx"
|
2013-12-05 11:09:04 +01:00
|
|
|
#include "util/DynamicFifoBuffer.hxx"
|
2016-11-07 09:20:12 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
2009-11-17 19:41:35 +01:00
|
|
|
|
|
|
|
#include <FLAC/stream_encoder.h>
|
|
|
|
|
2012-10-02 10:04:44 +02:00
|
|
|
#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
|
|
|
|
#error libFLAC is too old
|
|
|
|
#endif
|
|
|
|
|
2016-05-04 09:31:21 +02:00
|
|
|
class FlacEncoder final : public Encoder {
|
|
|
|
const AudioFormat audio_format;
|
2009-11-17 19:41:35 +01:00
|
|
|
|
2016-05-04 09:31:21 +02:00
|
|
|
FLAC__StreamEncoder *const fse;
|
2009-11-17 19:41:35 +01:00
|
|
|
|
2013-07-29 08:10:10 +02:00
|
|
|
PcmBuffer expand_buffer;
|
2009-11-17 19:41:35 +01:00
|
|
|
|
2011-11-28 08:00:10 +01:00
|
|
|
/**
|
|
|
|
* This buffer will hold encoded data from libFLAC until it is
|
|
|
|
* picked up with flac_encoder_read().
|
|
|
|
*/
|
2016-05-04 09:31:21 +02:00
|
|
|
DynamicFifoBuffer<uint8_t> output_buffer;
|
|
|
|
|
|
|
|
public:
|
2016-11-07 09:20:12 +01:00
|
|
|
FlacEncoder(AudioFormat _audio_format, FLAC__StreamEncoder *_fse);
|
2016-05-04 09:31:21 +02:00
|
|
|
|
|
|
|
~FlacEncoder() override {
|
|
|
|
FLAC__stream_encoder_delete(fse);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual methods from class Encoder */
|
2016-11-07 09:20:12 +01:00
|
|
|
void End() override {
|
2016-05-04 09:31:21 +02:00
|
|
|
(void) FLAC__stream_encoder_finish(fse);
|
|
|
|
}
|
2009-11-17 19:41:35 +01:00
|
|
|
|
2016-11-07 09:20:12 +01:00
|
|
|
void Flush() override {
|
2016-05-04 09:31:21 +02:00
|
|
|
(void) FLAC__stream_encoder_finish(fse);
|
|
|
|
}
|
|
|
|
|
2016-11-07 09:20:12 +01:00
|
|
|
void Write(const void *data, size_t length) override;
|
2016-05-04 09:31:21 +02:00
|
|
|
|
|
|
|
size_t Read(void *dest, size_t length) override {
|
|
|
|
return output_buffer.Read((uint8_t *)dest, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static FLAC__StreamEncoderWriteStatus WriteCallback(const FLAC__StreamEncoder *,
|
|
|
|
const FLAC__byte data[],
|
|
|
|
size_t bytes,
|
|
|
|
gcc_unused unsigned samples,
|
|
|
|
gcc_unused unsigned current_frame,
|
|
|
|
void *client_data) {
|
|
|
|
auto &encoder = *(FlacEncoder *)client_data;
|
|
|
|
encoder.output_buffer.Append((const uint8_t *)data, bytes);
|
|
|
|
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-04 18:29:31 +02:00
|
|
|
class PreparedFlacEncoder final : public PreparedEncoder {
|
2016-10-28 21:29:01 +02:00
|
|
|
const unsigned compression;
|
2016-05-04 09:31:21 +02:00
|
|
|
|
2016-05-04 18:29:31 +02:00
|
|
|
public:
|
2016-10-28 21:29:01 +02:00
|
|
|
PreparedFlacEncoder(const ConfigBlock &block);
|
2016-05-04 18:29:31 +02:00
|
|
|
|
|
|
|
/* virtual methods from class PreparedEncoder */
|
2016-11-07 09:20:12 +01:00
|
|
|
Encoder *Open(AudioFormat &audio_format) override;
|
2016-05-04 18:29:31 +02:00
|
|
|
|
|
|
|
const char *GetMimeType() const override {
|
|
|
|
return "audio/flac";
|
|
|
|
}
|
2013-07-30 09:04:05 +02:00
|
|
|
};
|
2009-12-03 14:53:30 +01:00
|
|
|
|
2016-10-28 21:29:01 +02:00
|
|
|
PreparedFlacEncoder::PreparedFlacEncoder(const ConfigBlock &block)
|
|
|
|
:compression(block.GetBlockValue("compression", 5u))
|
2009-11-17 19:41:35 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-04 09:31:21 +02:00
|
|
|
static PreparedEncoder *
|
2016-10-28 21:29:01 +02:00
|
|
|
flac_encoder_init(const ConfigBlock &block)
|
2009-11-17 19:41:35 +01:00
|
|
|
{
|
2016-10-28 21:29:01 +02:00
|
|
|
return new PreparedFlacEncoder(block);
|
2009-11-17 19:41:35 +01:00
|
|
|
}
|
|
|
|
|
2016-11-07 09:20:12 +01:00
|
|
|
static void
|
2016-05-04 09:31:21 +02:00
|
|
|
flac_encoder_setup(FLAC__StreamEncoder *fse, unsigned compression,
|
2016-11-07 09:20:12 +01:00
|
|
|
const AudioFormat &audio_format, unsigned bits_per_sample)
|
2009-11-17 19:41:35 +01:00
|
|
|
{
|
2016-11-07 09:20:12 +01:00
|
|
|
if (!FLAC__stream_encoder_set_compression_level(fse, compression))
|
|
|
|
throw FormatRuntimeError("error setting flac compression to %d",
|
|
|
|
compression);
|
2012-10-02 10:04:44 +02:00
|
|
|
|
2016-11-07 09:20:12 +01:00
|
|
|
if (!FLAC__stream_encoder_set_channels(fse, audio_format.channels))
|
|
|
|
throw FormatRuntimeError("error setting flac channels num to %d",
|
|
|
|
audio_format.channels);
|
2016-05-04 09:31:21 +02:00
|
|
|
|
2016-11-07 09:20:12 +01:00
|
|
|
if (!FLAC__stream_encoder_set_bits_per_sample(fse, bits_per_sample))
|
|
|
|
throw FormatRuntimeError("error setting flac bit format to %d",
|
|
|
|
bits_per_sample);
|
2016-05-04 09:31:21 +02:00
|
|
|
|
|
|
|
if (!FLAC__stream_encoder_set_sample_rate(fse,
|
2016-11-07 09:20:12 +01:00
|
|
|
audio_format.sample_rate))
|
|
|
|
throw FormatRuntimeError("error setting flac sample rate to %d",
|
|
|
|
audio_format.sample_rate);
|
2009-11-17 19:41:35 +01:00
|
|
|
}
|
|
|
|
|
2016-11-07 09:20:12 +01:00
|
|
|
FlacEncoder::FlacEncoder(AudioFormat _audio_format, FLAC__StreamEncoder *_fse)
|
|
|
|
:Encoder(false),
|
|
|
|
audio_format(_audio_format), fse(_fse),
|
|
|
|
output_buffer(8192)
|
2009-11-17 19:41:35 +01:00
|
|
|
{
|
2016-05-04 09:31:21 +02:00
|
|
|
/* this immediately outputs data through callback */
|
2009-11-17 19:41:35 +01:00
|
|
|
|
2016-05-04 09:31:21 +02:00
|
|
|
auto init_status =
|
|
|
|
FLAC__stream_encoder_init_stream(fse,
|
|
|
|
WriteCallback,
|
|
|
|
nullptr, nullptr, nullptr,
|
|
|
|
this);
|
2009-12-03 14:53:30 +01:00
|
|
|
|
2016-11-07 09:20:12 +01:00
|
|
|
if (init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
|
|
|
|
throw FormatRuntimeError("failed to initialize encoder: %s\n",
|
|
|
|
FLAC__StreamEncoderInitStatusString[init_status]);
|
2009-12-03 14:53:30 +01:00
|
|
|
}
|
|
|
|
|
2016-05-04 18:29:31 +02:00
|
|
|
Encoder *
|
2016-11-07 09:20:12 +01:00
|
|
|
PreparedFlacEncoder::Open(AudioFormat &audio_format)
|
2009-11-17 19:41:35 +01:00
|
|
|
{
|
2009-11-10 17:11:34 +01:00
|
|
|
unsigned bits_per_sample;
|
2009-11-17 19:41:35 +01:00
|
|
|
|
|
|
|
/* FIXME: flac should support 32bit as well */
|
2013-08-03 21:00:50 +02:00
|
|
|
switch (audio_format.format) {
|
|
|
|
case SampleFormat::S8:
|
2009-11-10 17:11:34 +01:00
|
|
|
bits_per_sample = 8;
|
|
|
|
break;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S16:
|
2009-11-10 17:11:34 +01:00
|
|
|
bits_per_sample = 16;
|
|
|
|
break;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S24_P32:
|
2009-11-10 17:11:34 +01:00
|
|
|
bits_per_sample = 24;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
bits_per_sample = 24;
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.format = SampleFormat::S24_P32;
|
2009-11-10 17:11:34 +01:00
|
|
|
}
|
2009-11-17 19:41:35 +01:00
|
|
|
|
|
|
|
/* allocate the encoder */
|
2016-05-04 09:31:21 +02:00
|
|
|
auto fse = FLAC__stream_encoder_new();
|
2016-11-07 09:20:12 +01:00
|
|
|
if (fse == nullptr)
|
|
|
|
throw std::runtime_error("FLAC__stream_encoder_new() failed");
|
2009-11-17 19:41:35 +01:00
|
|
|
|
2016-11-07 09:20:12 +01:00
|
|
|
try {
|
|
|
|
flac_encoder_setup(fse, compression,
|
|
|
|
audio_format, bits_per_sample);
|
|
|
|
} catch (...) {
|
2016-05-04 09:31:21 +02:00
|
|
|
FLAC__stream_encoder_delete(fse);
|
2016-11-07 09:20:12 +01:00
|
|
|
throw;
|
2009-11-17 19:41:35 +01:00
|
|
|
}
|
|
|
|
|
2016-11-07 09:20:12 +01:00
|
|
|
return new FlacEncoder(audio_format, fse);
|
2009-11-17 19:41:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
pcm8_to_flac(int32_t *out, const int8_t *in, unsigned num_samples)
|
|
|
|
{
|
|
|
|
while (num_samples > 0) {
|
|
|
|
*out++ = *in++;
|
|
|
|
--num_samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
pcm16_to_flac(int32_t *out, const int16_t *in, unsigned num_samples)
|
|
|
|
{
|
|
|
|
while (num_samples > 0) {
|
|
|
|
*out++ = *in++;
|
|
|
|
--num_samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 09:20:12 +01:00
|
|
|
void
|
|
|
|
FlacEncoder::Write(const void *data, size_t length)
|
2009-11-17 19:41:35 +01:00
|
|
|
{
|
|
|
|
void *exbuffer;
|
2013-07-29 08:04:34 +02:00
|
|
|
const void *buffer = nullptr;
|
2009-11-17 19:41:35 +01:00
|
|
|
|
|
|
|
/* format conversion */
|
|
|
|
|
2016-05-04 09:31:21 +02:00
|
|
|
const unsigned num_frames = length / audio_format.GetFrameSize();
|
|
|
|
const unsigned num_samples = num_frames * audio_format.channels;
|
2009-11-17 19:41:35 +01:00
|
|
|
|
2016-05-04 09:31:21 +02:00
|
|
|
switch (audio_format.format) {
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S8:
|
2016-05-04 09:31:21 +02:00
|
|
|
exbuffer = expand_buffer.Get(length * 4);
|
2013-07-29 08:04:34 +02:00
|
|
|
pcm8_to_flac((int32_t *)exbuffer, (const int8_t *)data,
|
|
|
|
num_samples);
|
2009-11-17 19:41:35 +01:00
|
|
|
buffer = exbuffer;
|
|
|
|
break;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S16:
|
2016-05-04 09:31:21 +02:00
|
|
|
exbuffer = expand_buffer.Get(length * 2);
|
2013-07-29 08:04:34 +02:00
|
|
|
pcm16_to_flac((int32_t *)exbuffer, (const int16_t *)data,
|
|
|
|
num_samples);
|
2009-11-17 19:41:35 +01:00
|
|
|
buffer = exbuffer;
|
|
|
|
break;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S24_P32:
|
|
|
|
case SampleFormat::S32:
|
2009-11-10 17:11:34 +01:00
|
|
|
/* nothing need to be done; format is the same for
|
|
|
|
both mpd and libFLAC */
|
2009-11-17 19:41:35 +01:00
|
|
|
buffer = data;
|
|
|
|
break;
|
2013-08-03 21:00:50 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
gcc_unreachable();
|
2009-11-17 19:41:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* feed samples to encoder */
|
|
|
|
|
2016-05-04 09:31:21 +02:00
|
|
|
if (!FLAC__stream_encoder_process_interleaved(fse,
|
2013-07-29 08:04:34 +02:00
|
|
|
(const FLAC__int32 *)buffer,
|
2016-11-07 09:20:12 +01:00
|
|
|
num_frames))
|
|
|
|
throw std::runtime_error("flac encoder process failed");
|
2009-11-17 19:41:35 +01:00
|
|
|
}
|
|
|
|
|
2013-07-30 09:04:05 +02:00
|
|
|
const EncoderPlugin flac_encoder_plugin = {
|
2013-07-29 08:04:34 +02:00
|
|
|
"flac",
|
|
|
|
flac_encoder_init,
|
2009-11-17 19:41:35 +01:00
|
|
|
};
|
2009-12-03 14:53:30 +01:00
|
|
|
|