ShineEncoderPlugin.cxx 5 KB
Newer Older
Andrée Ekroth's avatar
Andrée Ekroth committed
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
Andrée Ekroth's avatar
Andrée Ekroth committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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"
22
#include "../EncoderAPI.hxx"
Andrée Ekroth's avatar
Andrée Ekroth committed
23
#include "AudioFormat.hxx"
24
#include "config/ConfigError.hxx"
Andrée Ekroth's avatar
Andrée Ekroth committed
25
#include "util/DynamicFifoBuffer.hxx"
26
#include "util/RuntimeError.hxx"
Andrée Ekroth's avatar
Andrée Ekroth committed
27 28 29 30 31 32 33 34 35

extern "C"
{
#include <shine/layer3.h>
}

static constexpr size_t BUFFER_INIT_SIZE = 8192;
static constexpr unsigned CHANNELS = 2;

36 37
class ShineEncoder final : public Encoder {
	const AudioFormat audio_format;
Andrée Ekroth's avatar
Andrée Ekroth committed
38

39
	const shine_t shine;
Andrée Ekroth's avatar
Andrée Ekroth committed
40

41
	const size_t frame_size;
Andrée Ekroth's avatar
Andrée Ekroth committed
42

43 44 45
	/* workaround for bug:
	   https://github.com/savonet/shine/issues/11 */
	size_t input_pos = SHINE_MAX_SAMPLES + 1;
Andrée Ekroth's avatar
Andrée Ekroth committed
46 47 48

	int16_t *stereo[CHANNELS];

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
	DynamicFifoBuffer<uint8_t> output_buffer;

public:
	ShineEncoder(AudioFormat _audio_format, shine_t _shine)
		:Encoder(false),
		 audio_format(_audio_format), shine(_shine),
		 frame_size(shine_samples_per_pass(shine)),
		 stereo{new int16_t[frame_size], new int16_t[frame_size]},
		 output_buffer(BUFFER_INIT_SIZE) {}

	~ShineEncoder() override {
		if (input_pos > SHINE_MAX_SAMPLES) {
			/* write zero chunk */
			input_pos = 0;
			WriteChunk(true);
		}

		shine_close(shine);
		delete[] stereo[0];
		delete[] stereo[1];
	}

	bool WriteChunk(bool flush);

	/* virtual methods from class Encoder */
74 75
	void End() override {
		return Flush();
76 77
	}

78
	void Flush() override;
79

80
	void Write(const void *data, size_t length) override;
81 82 83 84 85

	size_t Read(void *dest, size_t length) override {
		return output_buffer.Read((uint8_t *)dest, length);
	}
};
Andrée Ekroth's avatar
Andrée Ekroth committed
86

87
class PreparedShineEncoder final : public PreparedEncoder {
88 89
	shine_config_t config;

90
public:
91
	PreparedShineEncoder(const ConfigBlock &block);
Andrée Ekroth's avatar
Andrée Ekroth committed
92

93
	/* virtual methods from class PreparedEncoder */
94
	Encoder *Open(AudioFormat &audio_format) override;
95 96 97 98

	const char *GetMimeType() const override {
		return  "audio/mpeg";
	}
Andrée Ekroth's avatar
Andrée Ekroth committed
99 100
};

101
PreparedShineEncoder::PreparedShineEncoder(const ConfigBlock &block)
Andrée Ekroth's avatar
Andrée Ekroth committed
102 103
{
	shine_set_config_mpeg_defaults(&config.mpeg);
104
	config.mpeg.bitr = block.GetBlockValue("bitrate", 128);
Andrée Ekroth's avatar
Andrée Ekroth committed
105 106
}

107
static PreparedEncoder *
108
shine_encoder_init(const ConfigBlock &block)
Andrée Ekroth's avatar
Andrée Ekroth committed
109
{
110
	return new PreparedShineEncoder(block);
Andrée Ekroth's avatar
Andrée Ekroth committed
111 112
}

113
static shine_t
114
SetupShine(shine_config_t config, AudioFormat &audio_format)
Andrée Ekroth's avatar
Andrée Ekroth committed
115
{
116 117 118
	audio_format.format = SampleFormat::S16;
	audio_format.channels = CHANNELS;

Andrée Ekroth's avatar
Andrée Ekroth committed
119 120
	config.mpeg.mode = audio_format.channels == 2 ? STEREO : MONO;
	config.wave.samplerate = audio_format.sample_rate;
121 122
	config.wave.channels =
		audio_format.channels == 2 ? PCM_STEREO : PCM_MONO;
Andrée Ekroth's avatar
Andrée Ekroth committed
123

124 125 126 127 128 129
	if (shine_check_config(config.wave.samplerate, config.mpeg.bitr) < 0)
		throw FormatRuntimeError("error configuring shine. "
					 "samplerate %d and bitrate %d configuration"
					 " not supported.",
					 config.wave.samplerate,
					 config.mpeg.bitr);
Andrée Ekroth's avatar
Andrée Ekroth committed
130

131 132
	auto shine = shine_initialise(&config);
	if (!shine)
133
		throw std::runtime_error("error initializing shine");
Andrée Ekroth's avatar
Andrée Ekroth committed
134

135
	return shine;
Andrée Ekroth's avatar
Andrée Ekroth committed
136 137
}

138
Encoder *
139
PreparedShineEncoder::Open(AudioFormat &audio_format)
Andrée Ekroth's avatar
Andrée Ekroth committed
140
{
141
	auto shine = SetupShine(config, audio_format);
142
	return new ShineEncoder(audio_format, shine);
Andrée Ekroth's avatar
Andrée Ekroth committed
143 144 145
}

bool
146
ShineEncoder::WriteChunk(bool flush)
Andrée Ekroth's avatar
Andrée Ekroth committed
147
{
148
	if (flush || input_pos == frame_size) {
Andrée Ekroth's avatar
Andrée Ekroth committed
149 150
		if (flush) {
			/* fill remaining with 0s */
151 152
			for (; input_pos < frame_size; input_pos++) {
				stereo[0][input_pos] = stereo[1][input_pos] = 0;
Andrée Ekroth's avatar
Andrée Ekroth committed
153 154 155
			}
		}

156
		int written;
157 158
		const uint8_t *out =
			shine_encode_buffer(shine, stereo, &written);
Andrée Ekroth's avatar
Andrée Ekroth committed
159 160

		if (written > 0)
161
			output_buffer.Append(out, written);
162 163

		input_pos = 0;
Andrée Ekroth's avatar
Andrée Ekroth committed
164 165 166 167 168
	}

	return true;
}

169 170
void
ShineEncoder::Write(const void *_data, size_t length)
Andrée Ekroth's avatar
Andrée Ekroth committed
171 172
{
	const int16_t *data = (const int16_t*)_data;
173
	length /= sizeof(*data) * audio_format.channels;
174 175
	size_t written = 0;

176 177
	if (input_pos > SHINE_MAX_SAMPLES)
		input_pos = 0;
178

179 180 181
	/* write all data to de-interleaved buffers */
	while (written < length) {
		for (;
182 183
		     written < length && input_pos < frame_size;
		     written++, input_pos++) {
184
			const size_t base =
185 186 187
				written * audio_format.channels;
			stereo[0][input_pos] = data[base];
			stereo[1][input_pos] = data[base + 1];
188 189
		}
		/* write if chunk is filled */
190
		WriteChunk(false);
191
	}
Andrée Ekroth's avatar
Andrée Ekroth committed
192 193
}

194 195
void
ShineEncoder::Flush()
Andrée Ekroth's avatar
Andrée Ekroth committed
196
{
197
	/* flush buffers and flush shine */
198
	WriteChunk(true);
199 200

	int written;
201
	const uint8_t *data = shine_flush(shine, &written);
Andrée Ekroth's avatar
Andrée Ekroth committed
202 203

	if (written > 0)
204
		output_buffer.Append(data, written);
Andrée Ekroth's avatar
Andrée Ekroth committed
205 206 207 208 209 210
}

const EncoderPlugin shine_encoder_plugin = {
	"shine",
	shine_encoder_init,
};