Shine encoding plugin

This encoding plugin features a fixed-point mp3 encoder,
with faster encoding on architectures without a FPU.

Right now the encoder is limited to stereo and 16 bit depth.
The bitrate and sample rate can be modified in audio_output.

audio_output {
        type            "httpd"
        name            "My shine stream"
        encoder         "shine"
        port            "8000"
        format          "44100:16:2"
        bitrate         "320"  # default: 128
}
This commit is contained in:
Andrée Ekroth 2014-01-12 21:40:29 +01:00 committed by Max Kellermann
parent 65ebfb16c9
commit ea771c17c5
7 changed files with 343 additions and 0 deletions

View File

@ -722,6 +722,7 @@ libencoder_plugins_a_CPPFLAGS = $(AM_CPPFLAGS) \
$(TWOLAME_CFLAGS) \
$(patsubst -I%/FLAC,-I%,$(FLAC_CFLAGS)) \
$(OPUS_CFLAGS) \
$(SHINE_CFLAGS) \
$(VORBISENC_CFLAGS)
ENCODER_LIBS = \
@ -730,6 +731,7 @@ ENCODER_LIBS = \
$(TWOLAME_LIBS) \
$(FLAC_LIBS) \
$(OPUS_LIBS) \
$(SHINE_LIBS) \
$(VORBISENC_LIBS)
libencoder_plugins_a_SOURCES = \
@ -780,6 +782,12 @@ libencoder_plugins_a_SOURCES += \
src/encoder/FlacEncoderPlugin.cxx src/encoder/FlacEncoderPlugin.hxx
endif
if ENABLE_SHINE_ENCODER
libencoder_plugins_a_SOURCES += \
src/encoder/ShineEncoderPlugin.cxx \
src/encoder/ShineEncoderPlugin.hxx
endif
else
ENCODER_LIBS =
endif

2
NEWS
View File

@ -14,6 +14,8 @@ ver 0.19 (not yet released)
- smbclient: new input plugin
* filter
- volume: improved software volume dithering
* encoder:
- shine: new encoder plugin
* new resampler option using libsoxr
ver 0.18.7 (not yet released)

View File

@ -449,6 +449,10 @@ AC_ARG_ENABLE(sidplay,
[enable C64 SID support via libsidplay2]),,
enable_sidplay=auto)
AC_ARG_ENABLE(shine-encoder,
AS_HELP_STRING([--enable-shine-encoder],
[enables shine encoder]),,
[enable_shine_encoder=auto])
AC_ARG_ENABLE(shout,
AS_HELP_STRING([--enable-shout],
@ -1252,6 +1256,7 @@ else
enable_vorbis_encoder=no
enable_lame_encoder=no
enable_twolame_encoder=no
enable_shine_encoder=no
enable_wave_encoder=no
enable_flac_encoder=no
fi
@ -1263,6 +1268,17 @@ if test x$enable_flac_encoder = xyes; then
fi
AM_CONDITIONAL(ENABLE_FLAC_ENCODER, test x$enable_flac_encoder = xyes)
dnl ------------------------------- Shine Encoder ------------------------------
MPD_AUTO_PKG(shine_encoder, SHINE, [shine >= 3],
[shine encoder], [libshine not found])
if test x$enable_shine_encoder = xyes; then
AC_DEFINE(ENABLE_SHINE_ENCODER, 1,
[Define to enable the shine encoder plugin])
fi
AM_CONDITIONAL(ENABLE_SHINE_ENCODER, test x$enable_shine_encoder = xyes)
dnl ---------------------------- Ogg Vorbis Encoder ---------------------------
MPD_AUTO_PKG(vorbis_encoder, VORBISENC, [vorbisenc],
[Ogg Vorbis encoder], [libvorbisenc not found])
@ -1306,6 +1322,7 @@ if test x$enable_vorbis_encoder != xno ||
test x$enable_lame_encoder != xno ||
test x$enable_twolame_encoder != xno ||
test x$enable_flac_encoder != xno ||
test x$enable_shine_encoder != xno ||
test x$enable_wave_encoder != xno; then
# at least one encoder plugin is enabled
enable_encoder=yes
@ -1717,6 +1734,7 @@ if
printf '\nStreaming encoder support:\n\t'
results(flac_encoder, [FLAC])
results(lame_encoder, [LAME])
results(shine_encoder, [Shine])
results(vorbis_encoder, [Ogg Vorbis])
results(opus, [Opus])
results(twolame_encoder, [TwoLAME])

View File

@ -1369,6 +1369,35 @@ systemctl start mpd.socket</programlisting>
</para>
</section>
<section>
<title><varname>shine</varname></title>
<para>
Encodes into MP3 using the shine library.
</para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>Setting</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>
<varname>bitrate</varname>
</entry>
<entry>
Sets the bit rate in kilobit per second.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</section>
<section>
<title><varname>twolame</varname></title>

View File

@ -25,6 +25,7 @@
#include "encoder/VorbisEncoderPlugin.hxx"
#include "encoder/OpusEncoderPlugin.hxx"
#include "encoder/FlacEncoderPlugin.hxx"
#include "encoder/ShineEncoderPlugin.hxx"
#include "encoder/LameEncoderPlugin.hxx"
#include "encoder/TwolameEncoderPlugin.hxx"
@ -49,6 +50,9 @@ const EncoderPlugin *const encoder_plugins[] = {
#endif
#ifdef ENABLE_FLAC_ENCODER
&flac_encoder_plugin,
#endif
#ifdef ENABLE_SHINE_ENCODER
&shine_encoder_plugin,
#endif
nullptr
};

View File

@ -0,0 +1,257 @@
/*
* Copyright (C) 2003-2014 The Music Player Daemon Project
* 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 "ShineEncoderPlugin.hxx"
#include "config.h"
#include "EncoderAPI.hxx"
#include "AudioFormat.hxx"
#include "ConfigError.hxx"
#include "util/Manual.hxx"
#include "util/NumberParser.hxx"
#include "util/DynamicFifoBuffer.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
extern "C"
{
#include <shine/layer3.h>
}
static constexpr size_t BUFFER_INIT_SIZE = 8192;
static constexpr unsigned CHANNELS = 2;
struct ShineEncoder {
Encoder encoder;
AudioFormat audio_format;
shine_t shine;
shine_config_t config;
uint16_t frame_size;
int16_t buffer[SHINE_MAX_SAMPLES * CHANNELS];
int16_t channels[CHANNELS][SHINE_MAX_SAMPLES];
int16_t *stereo[CHANNELS];
Manual<DynamicFifoBuffer<int16_t>> input_buffer;
size_t input_size;
Manual<DynamicFifoBuffer<uint8_t>> output_buffer;
ShineEncoder()
:encoder(shine_encoder_plugin),
stereo { channels[0], channels[1] },
input_size(0) {}
bool Configure(const config_param &param, Error &error);
bool Setup(Error &error);
bool WriteChunks(bool flush, Error &error);
};
static constexpr Domain shine_encoder_domain("shine_encoder");
inline bool
ShineEncoder::Configure(const config_param &param,
gcc_unused Error &error)
{
shine_set_config_mpeg_defaults(&config.mpeg);
config.mpeg.bitr = param.GetBlockValue("bitrate", 128);
return true;
}
static Encoder *
shine_encoder_init(const config_param &param, Error &error)
{
ShineEncoder *encoder = new ShineEncoder();
/* load configuration from "param" */
if (!encoder->Configure(param, error)) {
/* configuration has failed, roll back and return error */
delete encoder;
return nullptr;
}
return &encoder->encoder;
}
static void
shine_encoder_finish(Encoder *_encoder)
{
ShineEncoder *encoder = (ShineEncoder *)_encoder;
delete encoder;
}
inline bool
ShineEncoder::Setup(Error &error)
{
input_size = 0;
config.mpeg.mode = audio_format.channels == 2 ? STEREO : MONO;
config.wave.samplerate = audio_format.sample_rate;
config.wave.channels = audio_format.channels == 2 ? PCM_STEREO : PCM_MONO;
if (shine_check_config(config.wave.samplerate, config.mpeg.bitr) < 0) {
error.Format(config_domain,
"error configuring shine. samplerate %d and bitrate %d configuration not supported.",
config.wave.samplerate,
config.mpeg.bitr);
return false;
}
shine = shine_initialise(&config);
if (!shine) {
error.Format(config_domain,
"error initializing shine.");
return false;
}
frame_size = shine_samples_per_pass(shine);
return true;
}
static bool
shine_encoder_open(Encoder *_encoder, AudioFormat &audio_format, Error &error)
{
ShineEncoder *encoder = (ShineEncoder *)_encoder;
audio_format.format = SampleFormat::S16;
audio_format.channels = CHANNELS;
encoder->audio_format = audio_format;
if (!encoder->Setup(error))
return false;
encoder->input_buffer.Construct(BUFFER_INIT_SIZE);
encoder->output_buffer.Construct(BUFFER_INIT_SIZE);
return true;
}
static void
shine_encoder_close(Encoder *_encoder)
{
ShineEncoder *encoder = (ShineEncoder *)_encoder;
shine_close(encoder->shine);
encoder->input_buffer.Destruct();
encoder->output_buffer.Destruct();
}
bool
ShineEncoder::WriteChunks(bool flush, gcc_unused Error &error)
{
const size_t chunk_size = frame_size * audio_format.channels;
/*
* shine requires data in specific sizes, therefore we need to
* buffer the received data and encode in chunks.
*/
while (input_size >= chunk_size || (flush && input_size > 0)) {
long written;
const size_t read = input_buffer->Read(buffer, chunk_size);
input_size -= read;
/* de-interleave data */
for (size_t i = 0; i < read / audio_format.channels; i++) {
channels[0][i] = buffer[i * audio_format.channels];
channels[1][i] = buffer[i * audio_format.channels + 1];
}
if (flush) {
/* fill remaining with 0s */
for (size_t i = read / audio_format.channels; i < frame_size; i++) {
channels[0][i] = channels[1][i] = 0;
}
}
const uint8_t *out = shine_encode_buffer(shine, stereo, &written);
if (written > 0)
output_buffer->Append((const uint8_t *)out, written);
}
return true;
}
static bool
shine_encoder_write(Encoder *_encoder,
const void *_data, size_t length,
gcc_unused Error &error)
{
ShineEncoder *encoder = (ShineEncoder *)_encoder;
const int16_t *data = (const int16_t*)_data;
encoder->input_buffer->Append(data, length / sizeof(*data));
encoder->input_size += length / sizeof(*data);
return encoder->WriteChunks(false, error);
}
static bool
shine_encoder_flush(Encoder *_encoder, gcc_unused Error &error)
{
ShineEncoder *encoder = (ShineEncoder *)_encoder;
long written;
encoder->WriteChunks(true, error);
const uint8_t *data = shine_flush(encoder->shine, &written);
if (written > 0)
encoder->output_buffer->Append(data, written);
return true;
}
static size_t
shine_encoder_read(Encoder *_encoder, void *dest, size_t length)
{
ShineEncoder *encoder = (ShineEncoder *)_encoder;
return encoder->output_buffer->Read((uint8_t *)dest, length);
}
static const char *
shine_encoder_get_mime_type(gcc_unused Encoder *_encoder)
{
return "audio/mpeg";
}
const EncoderPlugin shine_encoder_plugin = {
"shine",
shine_encoder_init,
shine_encoder_finish,
shine_encoder_open,
shine_encoder_close,
shine_encoder_flush,
shine_encoder_flush,
nullptr,
nullptr,
shine_encoder_write,
shine_encoder_read,
shine_encoder_get_mime_type,
};

View File

@ -0,0 +1,25 @@
/*
* Copyright (C) 2003-2014 The Music Player Daemon Project
* 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.
*/
#ifndef MPD_ENCODER_SHINE_HXX
#define MPD_ENCODER_SHINE_HXX
extern const struct EncoderPlugin shine_encoder_plugin;
#endif