ShineEncoderPlugin.cxx 4.98 KB
Newer Older
Andrée Ekroth's avatar
Andrée Ekroth committed
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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
 * 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"
21
#include "../EncoderAPI.hxx"
Andrée Ekroth's avatar
Andrée Ekroth committed
22 23
#include "AudioFormat.hxx"
#include "util/DynamicFifoBuffer.hxx"
24
#include "util/RuntimeError.hxx"
Andrée Ekroth's avatar
Andrée Ekroth committed
25 26 27 28 29 30 31 32 33

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

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

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

37
	const shine_t shine;
Andrée Ekroth's avatar
Andrée Ekroth committed
38

39
	const size_t frame_size;
Andrée Ekroth's avatar
Andrée Ekroth committed
40

41 42 43
	/* 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
44 45 46

	int16_t *stereo[CHANNELS];

47 48 49
	DynamicFifoBuffer<uint8_t> output_buffer;

public:
50
	ShineEncoder(AudioFormat _audio_format, shine_t _shine) noexcept
51 52 53 54 55 56
		: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) {}

57
	~ShineEncoder() noexcept override {
58 59 60 61 62 63 64 65 66 67 68 69 70 71
		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 */
72 73
	void End() override {
		return Flush();
74 75
	}

76
	void Flush() override;
77

78
	void Write(const void *data, size_t length) override;
79

80
	size_t Read(void *dest, size_t length) noexcept override {
81 82 83
		return output_buffer.Read((uint8_t *)dest, length);
	}
};
Andrée Ekroth's avatar
Andrée Ekroth committed
84

85
class PreparedShineEncoder final : public PreparedEncoder {
86 87
	shine_config_t config;

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

91
	/* virtual methods from class PreparedEncoder */
92
	Encoder *Open(AudioFormat &audio_format) override;
93

94
	const char *GetMimeType() const noexcept override {
95 96
		return  "audio/mpeg";
	}
Andrée Ekroth's avatar
Andrée Ekroth committed
97 98
};

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

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

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

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

122 123 124 125 126 127
	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
128

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

133
	return shine;
Andrée Ekroth's avatar
Andrée Ekroth committed
134 135
}

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

bool
144
ShineEncoder::WriteChunk(bool flush)
Andrée Ekroth's avatar
Andrée Ekroth committed
145
{
146
	if (flush || input_pos == frame_size) {
Andrée Ekroth's avatar
Andrée Ekroth committed
147 148
		if (flush) {
			/* fill remaining with 0s */
149 150
			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
151 152 153
			}
		}

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

		if (written > 0)
159
			output_buffer.Append(out, written);
160 161

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

	return true;
}

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

174 175
	if (input_pos > SHINE_MAX_SAMPLES)
		input_pos = 0;
176

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

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

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

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

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