2009-11-17 19:41:35 +01:00
|
|
|
/*
|
2013-07-29 08:04:34 +02:00
|
|
|
* Copyright (C) 2003-2013 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2013-07-29 08:04:34 +02:00
|
|
|
#include "FlacEncoderPlugin.hxx"
|
2009-11-17 19:41:35 +01:00
|
|
|
#include "encoder_api.h"
|
|
|
|
#include "encoder_plugin.h"
|
|
|
|
#include "audio_format.h"
|
2013-07-29 08:10:10 +02:00
|
|
|
#include "pcm/PcmBuffer.hxx"
|
2013-01-15 01:00:59 +01:00
|
|
|
#include "util/fifo_buffer.h"
|
2013-07-29 08:04:34 +02:00
|
|
|
|
|
|
|
extern "C" {
|
2013-01-15 01:00:59 +01:00
|
|
|
#include "util/growing_fifo.h"
|
2013-07-29 08:04:34 +02:00
|
|
|
}
|
2009-11-17 19:41:35 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2009-11-17 19:41:35 +01:00
|
|
|
struct flac_encoder {
|
|
|
|
struct encoder encoder;
|
|
|
|
|
|
|
|
struct audio_format audio_format;
|
|
|
|
unsigned compression;
|
|
|
|
|
|
|
|
FLAC__StreamEncoder *fse;
|
|
|
|
|
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().
|
|
|
|
*/
|
|
|
|
struct fifo_buffer *output_buffer;
|
2009-11-17 19:41:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
extern const struct encoder_plugin flac_encoder_plugin;
|
|
|
|
|
2009-12-03 14:53:30 +01:00
|
|
|
|
2009-11-17 19:41:35 +01:00
|
|
|
static inline GQuark
|
|
|
|
flac_encoder_quark(void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string("flac_encoder");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
flac_encoder_configure(struct flac_encoder *encoder,
|
|
|
|
const struct config_param *param, G_GNUC_UNUSED GError **error)
|
|
|
|
{
|
2011-02-04 10:39:21 +01:00
|
|
|
encoder->compression = config_get_block_unsigned(param,
|
2009-11-17 19:41:35 +01:00
|
|
|
"compression", 5);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct encoder *
|
|
|
|
flac_encoder_init(const struct config_param *param, GError **error)
|
|
|
|
{
|
|
|
|
struct flac_encoder *encoder;
|
|
|
|
|
|
|
|
encoder = g_new(struct flac_encoder, 1);
|
|
|
|
encoder_struct_init(&encoder->encoder, &flac_encoder_plugin);
|
|
|
|
|
|
|
|
/* load configuration from "param" */
|
|
|
|
if (!flac_encoder_configure(encoder, param, error)) {
|
|
|
|
/* configuration has failed, roll back and return error */
|
|
|
|
g_free(encoder);
|
2013-07-29 08:04:34 +02:00
|
|
|
return nullptr;
|
2009-11-17 19:41:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &encoder->encoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
flac_encoder_finish(struct encoder *_encoder)
|
|
|
|
{
|
|
|
|
struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
|
|
|
|
|
|
|
|
/* the real libFLAC cleanup was already performed by
|
|
|
|
flac_encoder_close(), so no real work here */
|
|
|
|
g_free(encoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2009-11-10 17:11:34 +01:00
|
|
|
flac_encoder_setup(struct flac_encoder *encoder, unsigned bits_per_sample,
|
|
|
|
GError **error)
|
2009-11-17 19:41:35 +01:00
|
|
|
{
|
|
|
|
if ( !FLAC__stream_encoder_set_compression_level(encoder->fse,
|
|
|
|
encoder->compression)) {
|
|
|
|
g_set_error(error, flac_encoder_quark(), 0,
|
|
|
|
"error setting flac compression to %d",
|
|
|
|
encoder->compression);
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-02 10:04:44 +02:00
|
|
|
|
2009-11-17 19:41:35 +01:00
|
|
|
if ( !FLAC__stream_encoder_set_channels(encoder->fse,
|
|
|
|
encoder->audio_format.channels)) {
|
|
|
|
g_set_error(error, flac_encoder_quark(), 0,
|
|
|
|
"error setting flac channels num to %d",
|
|
|
|
encoder->audio_format.channels);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( !FLAC__stream_encoder_set_bits_per_sample(encoder->fse,
|
2009-12-03 14:53:30 +01:00
|
|
|
bits_per_sample)) {
|
2009-11-17 19:41:35 +01:00
|
|
|
g_set_error(error, flac_encoder_quark(), 0,
|
|
|
|
"error setting flac bit format to %d",
|
2009-11-10 17:11:34 +01:00
|
|
|
bits_per_sample);
|
2009-11-17 19:41:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( !FLAC__stream_encoder_set_sample_rate(encoder->fse,
|
|
|
|
encoder->audio_format.sample_rate)) {
|
|
|
|
g_set_error(error, flac_encoder_quark(), 0,
|
|
|
|
"error setting flac sample rate to %d",
|
|
|
|
encoder->audio_format.sample_rate);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FLAC__StreamEncoderWriteStatus
|
|
|
|
flac_write_callback(G_GNUC_UNUSED const FLAC__StreamEncoder *fse,
|
2009-12-15 23:05:15 +01:00
|
|
|
const FLAC__byte data[],
|
|
|
|
size_t bytes,
|
|
|
|
G_GNUC_UNUSED unsigned samples,
|
2009-11-17 19:41:35 +01:00
|
|
|
G_GNUC_UNUSED unsigned current_frame, void *client_data)
|
|
|
|
{
|
|
|
|
struct flac_encoder *encoder = (struct flac_encoder *) client_data;
|
|
|
|
|
|
|
|
//transfer data to buffer
|
2011-11-28 08:00:10 +01:00
|
|
|
growing_fifo_append(&encoder->output_buffer, data, bytes);
|
2009-11-17 19:41:35 +01:00
|
|
|
|
|
|
|
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
2009-12-03 14:53:30 +01:00
|
|
|
static void
|
|
|
|
flac_encoder_close(struct encoder *_encoder)
|
|
|
|
{
|
|
|
|
struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
|
|
|
|
|
|
|
|
FLAC__stream_encoder_delete(encoder->fse);
|
|
|
|
|
2013-07-29 08:10:10 +02:00
|
|
|
encoder->expand_buffer.Clear();
|
2011-11-28 08:00:10 +01:00
|
|
|
fifo_buffer_free(encoder->output_buffer);
|
2009-12-03 14:53:30 +01:00
|
|
|
}
|
|
|
|
|
2009-11-17 19:41:35 +01:00
|
|
|
static bool
|
|
|
|
flac_encoder_open(struct encoder *_encoder, struct audio_format *audio_format,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
|
2009-11-10 17:11:34 +01:00
|
|
|
unsigned bits_per_sample;
|
2009-11-17 19:41:35 +01:00
|
|
|
|
|
|
|
encoder->audio_format = *audio_format;
|
|
|
|
|
|
|
|
/* FIXME: flac should support 32bit as well */
|
2009-11-10 17:11:34 +01:00
|
|
|
switch (audio_format->format) {
|
|
|
|
case SAMPLE_FORMAT_S8:
|
|
|
|
bits_per_sample = 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SAMPLE_FORMAT_S16:
|
|
|
|
bits_per_sample = 16;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SAMPLE_FORMAT_S24_P32:
|
|
|
|
bits_per_sample = 24;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
bits_per_sample = 24;
|
|
|
|
audio_format->format = SAMPLE_FORMAT_S24_P32;
|
|
|
|
}
|
2009-11-17 19:41:35 +01:00
|
|
|
|
|
|
|
/* allocate the encoder */
|
|
|
|
encoder->fse = FLAC__stream_encoder_new();
|
2013-07-29 08:04:34 +02:00
|
|
|
if (encoder->fse == nullptr) {
|
2009-11-17 19:41:35 +01:00
|
|
|
g_set_error(error, flac_encoder_quark(), 0,
|
|
|
|
"flac_new() failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-10 17:11:34 +01:00
|
|
|
if (!flac_encoder_setup(encoder, bits_per_sample, error)) {
|
2009-11-17 19:41:35 +01:00
|
|
|
FLAC__stream_encoder_delete(encoder->fse);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-28 08:00:10 +01:00
|
|
|
encoder->output_buffer = growing_fifo_new();
|
|
|
|
|
2011-03-31 21:43:53 +02:00
|
|
|
/* this immediately outputs data through callback */
|
2009-11-17 19:41:35 +01:00
|
|
|
|
2009-12-03 14:53:30 +01:00
|
|
|
{
|
|
|
|
FLAC__StreamEncoderInitStatus init_status;
|
|
|
|
|
|
|
|
init_status = FLAC__stream_encoder_init_stream(encoder->fse,
|
2009-11-17 19:41:35 +01:00
|
|
|
flac_write_callback,
|
2013-07-29 08:04:34 +02:00
|
|
|
nullptr, nullptr, nullptr, encoder);
|
2009-11-17 19:41:35 +01:00
|
|
|
|
2009-12-03 14:53:30 +01:00
|
|
|
if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
|
|
|
|
g_set_error(error, flac_encoder_quark(), 0,
|
2011-02-04 10:39:21 +01:00
|
|
|
"failed to initialize encoder: %s\n",
|
2009-11-17 19:41:35 +01:00
|
|
|
FLAC__StreamEncoderInitStatusString[init_status]);
|
2009-12-03 14:53:30 +01:00
|
|
|
flac_encoder_close(_encoder);
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-17 19:41:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
flac_encoder_flush(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
|
|
|
|
{
|
|
|
|
struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
|
|
|
|
|
2009-12-03 14:53:30 +01:00
|
|
|
(void) FLAC__stream_encoder_finish(encoder->fse);
|
|
|
|
return true;
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
flac_encoder_write(struct encoder *_encoder,
|
|
|
|
const void *data, size_t length,
|
|
|
|
G_GNUC_UNUSED GError **error)
|
|
|
|
{
|
|
|
|
struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
|
|
|
|
unsigned num_frames, num_samples;
|
|
|
|
void *exbuffer;
|
2013-07-29 08:04:34 +02:00
|
|
|
const void *buffer = nullptr;
|
2009-11-17 19:41:35 +01:00
|
|
|
|
|
|
|
/* format conversion */
|
|
|
|
|
|
|
|
num_frames = length / audio_format_frame_size(&encoder->audio_format);
|
|
|
|
num_samples = num_frames * encoder->audio_format.channels;
|
|
|
|
|
2009-11-10 17:11:34 +01:00
|
|
|
switch (encoder->audio_format.format) {
|
|
|
|
case SAMPLE_FORMAT_S8:
|
2013-07-29 08:10:10 +02:00
|
|
|
exbuffer = encoder->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
|
|
|
|
|
|
|
case SAMPLE_FORMAT_S16:
|
2013-07-29 08:10:10 +02:00
|
|
|
exbuffer = encoder->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
|
|
|
|
|
|
|
case SAMPLE_FORMAT_S24_P32:
|
|
|
|
case SAMPLE_FORMAT_S32:
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* feed samples to encoder */
|
|
|
|
|
2013-07-29 08:04:34 +02:00
|
|
|
if (!FLAC__stream_encoder_process_interleaved(encoder->fse,
|
|
|
|
(const FLAC__int32 *)buffer,
|
|
|
|
num_frames)) {
|
2009-11-17 19:41:35 +01:00
|
|
|
g_set_error(error, flac_encoder_quark(), 0,
|
|
|
|
"flac encoder process failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
flac_encoder_read(struct encoder *_encoder, void *dest, size_t length)
|
|
|
|
{
|
|
|
|
struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
|
|
|
|
|
2011-11-28 08:00:10 +01:00
|
|
|
size_t max_length;
|
2013-07-29 08:04:34 +02:00
|
|
|
const char *src = (const char *)
|
|
|
|
fifo_buffer_read(encoder->output_buffer, &max_length);
|
|
|
|
if (src == nullptr)
|
2011-11-28 08:00:10 +01:00
|
|
|
return 0;
|
2009-11-17 19:41:35 +01:00
|
|
|
|
2011-11-28 08:00:10 +01:00
|
|
|
if (length > max_length)
|
|
|
|
length = max_length;
|
2009-11-17 19:41:35 +01:00
|
|
|
|
2011-11-28 08:00:10 +01:00
|
|
|
memcpy(dest, src, length);
|
|
|
|
fifo_buffer_consume(encoder->output_buffer, length);
|
2009-11-17 19:41:35 +01:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2009-12-03 20:11:32 +01:00
|
|
|
static const char *
|
|
|
|
flac_encoder_get_mime_type(G_GNUC_UNUSED struct encoder *_encoder)
|
|
|
|
{
|
2009-12-03 20:43:13 +01:00
|
|
|
return "audio/flac";
|
2009-12-03 20:11:32 +01:00
|
|
|
}
|
|
|
|
|
2009-11-17 19:41:35 +01:00
|
|
|
const struct encoder_plugin flac_encoder_plugin = {
|
2013-07-29 08:04:34 +02:00
|
|
|
"flac",
|
|
|
|
flac_encoder_init,
|
|
|
|
flac_encoder_finish,
|
|
|
|
flac_encoder_open,
|
|
|
|
flac_encoder_close,
|
|
|
|
flac_encoder_flush,
|
|
|
|
flac_encoder_flush,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
flac_encoder_write,
|
|
|
|
flac_encoder_read,
|
|
|
|
flac_encoder_get_mime_type,
|
2009-11-17 19:41:35 +01:00
|
|
|
};
|
2009-12-03 14:53:30 +01:00
|
|
|
|