ShineEncoderPlugin.cxx 6.18 KB
Newer Older
Andrée Ekroth's avatar
Andrée Ekroth committed
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#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;

48 49
	size_t frame_size;
	size_t input_pos;
Andrée Ekroth's avatar
Andrée Ekroth committed
50 51 52 53
	int16_t *stereo[CHANNELS];

	Manual<DynamicFifoBuffer<uint8_t>> output_buffer;

54
	ShineEncoder():encoder(shine_encoder_plugin){}
Andrée Ekroth's avatar
Andrée Ekroth committed
55

56
	bool Configure(const ConfigBlock &block, Error &error);
Andrée Ekroth's avatar
Andrée Ekroth committed
57 58 59

	bool Setup(Error &error);

60
	bool WriteChunk(bool flush);
Andrée Ekroth's avatar
Andrée Ekroth committed
61 62 63 64 65
};

static constexpr Domain shine_encoder_domain("shine_encoder");

inline bool
66
ShineEncoder::Configure(const ConfigBlock &block, gcc_unused Error &error)
Andrée Ekroth's avatar
Andrée Ekroth committed
67 68
{
	shine_set_config_mpeg_defaults(&config.mpeg);
69
	config.mpeg.bitr = block.GetBlockValue("bitrate", 128);
Andrée Ekroth's avatar
Andrée Ekroth committed
70 71 72 73 74

	return true;
}

static Encoder *
75
shine_encoder_init(const ConfigBlock &block, Error &error)
Andrée Ekroth's avatar
Andrée Ekroth committed
76 77 78
{
	ShineEncoder *encoder = new ShineEncoder();

79 80
	/* load configuration from "block" */
	if (!encoder->Configure(block, error)) {
Andrée Ekroth's avatar
Andrée Ekroth committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
		/* 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)
{
	config.mpeg.mode = audio_format.channels == 2 ? STEREO : MONO;
	config.wave.samplerate = audio_format.sample_rate;
102 103
	config.wave.channels =
		audio_format.channels == 2 ? PCM_STEREO : PCM_MONO;
Andrée Ekroth's avatar
Andrée Ekroth committed
104 105 106

	if (shine_check_config(config.wave.samplerate, config.mpeg.bitr) < 0) {
		error.Format(config_domain,
107 108 109
			     "error configuring shine. "
			     "samplerate %d and bitrate %d configuration"
			     " not supported.",
Andrée Ekroth's avatar
Andrée Ekroth committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
			     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;

142 143
	encoder->stereo[0] = new int16_t[encoder->frame_size];
	encoder->stereo[1] = new int16_t[encoder->frame_size];
144 145 146
	/* workaround for bug:
	   https://github.com/savonet/shine/issues/11 */
	encoder->input_pos = SHINE_MAX_SAMPLES + 1;
147

Andrée Ekroth's avatar
Andrée Ekroth committed
148 149 150 151 152 153 154 155 156 157
	encoder->output_buffer.Construct(BUFFER_INIT_SIZE);

	return true;
}

static void
shine_encoder_close(Encoder *_encoder)
{
	ShineEncoder *encoder = (ShineEncoder *)_encoder;

158 159 160 161 162 163
	if (encoder->input_pos > SHINE_MAX_SAMPLES) {
		/* write zero chunk */
		encoder->input_pos = 0;
		encoder->WriteChunk(true);
	}

Andrée Ekroth's avatar
Andrée Ekroth committed
164
	shine_close(encoder->shine);
165 166
	delete[] encoder->stereo[0];
	delete[] encoder->stereo[1];
Andrée Ekroth's avatar
Andrée Ekroth committed
167 168 169 170
	encoder->output_buffer.Destruct();
}

bool
171
ShineEncoder::WriteChunk(bool flush)
Andrée Ekroth's avatar
Andrée Ekroth committed
172
{
173
	if (flush || input_pos == frame_size) {
Andrée Ekroth's avatar
Andrée Ekroth committed
174 175
		if (flush) {
			/* fill remaining with 0s */
176 177
			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
178 179 180
			}
		}

181
		int written;
182 183
		const uint8_t *out =
			shine_encode_buffer(shine, stereo, &written);
Andrée Ekroth's avatar
Andrée Ekroth committed
184 185

		if (written > 0)
186 187 188
			output_buffer->Append(out, written);

		input_pos = 0;
Andrée Ekroth's avatar
Andrée Ekroth committed
189 190 191 192 193 194 195 196 197 198 199 200
	}

	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;
201 202 203
	length /= sizeof(*data) * encoder->audio_format.channels;
	size_t written = 0;

204 205 206 207
	if (encoder->input_pos > SHINE_MAX_SAMPLES) {
		encoder->input_pos = 0;
	}

208 209 210 211 212 213 214 215 216 217 218 219
	/* write all data to de-interleaved buffers */
	while (written < length) {
		for (;
		     written < length
			     && encoder->input_pos < encoder->frame_size;
		     written++, encoder->input_pos++) {
			const size_t base =
				written * encoder->audio_format.channels;
			encoder->stereo[0][encoder->input_pos] = data[base];
			encoder->stereo[1][encoder->input_pos] = data[base + 1];
		}
		/* write if chunk is filled */
220
		encoder->WriteChunk(false);
221
	}
Andrée Ekroth's avatar
Andrée Ekroth committed
222

223
	return true;
Andrée Ekroth's avatar
Andrée Ekroth committed
224 225 226 227 228 229 230
}

static bool
shine_encoder_flush(Encoder *_encoder, gcc_unused Error &error)
{
	ShineEncoder *encoder = (ShineEncoder *)_encoder;

231
	/* flush buffers and flush shine */
232
	encoder->WriteChunk(true);
233 234

	int written;
Andrée Ekroth's avatar
Andrée Ekroth committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	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,
};