FlacEncoderPlugin.cxx 7.86 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
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 "config.h"
21
#include "FlacEncoderPlugin.hxx"
22
#include "../EncoderAPI.hxx"
23
#include "AudioFormat.hxx"
24
#include "pcm/PcmBuffer.hxx"
25
#include "config/ConfigError.hxx"
26 27
#include "util/Manual.hxx"
#include "util/DynamicFifoBuffer.hxx"
28 29
#include "util/Error.hxx"
#include "util/Domain.hxx"
30 31 32

#include <FLAC/stream_encoder.h>

33 34 35 36
#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
#error libFLAC is too old
#endif

37
struct flac_encoder {
38
	Encoder encoder;
39

40
	AudioFormat audio_format;
41 42 43 44
	unsigned compression;

	FLAC__StreamEncoder *fse;

45
	PcmBuffer expand_buffer;
46

47 48 49 50
	/**
	 * This buffer will hold encoded data from libFLAC until it is
	 * picked up with flac_encoder_read().
	 */
51
	Manual<DynamicFifoBuffer<uint8_t>> output_buffer;
52

53 54
	flac_encoder():encoder(flac_encoder_plugin) {}
};
55

56
static constexpr Domain flac_encoder_domain("vorbis_encoder");
57 58

static bool
59
flac_encoder_configure(struct flac_encoder *encoder, const config_param &param,
60
		       gcc_unused Error &error)
61
{
62
	encoder->compression = param.GetBlockValue("compression", 5u);
63 64 65 66

	return true;
}

67
static Encoder *
68
flac_encoder_init(const config_param &param, Error &error)
69
{
70
	flac_encoder *encoder = new flac_encoder();
71 72 73 74

	/* load configuration from "param" */
	if (!flac_encoder_configure(encoder, param, error)) {
		/* configuration has failed, roll back and return error */
75
		delete encoder;
76
		return nullptr;
77 78 79 80 81 82
	}

	return &encoder->encoder;
}

static void
83
flac_encoder_finish(Encoder *_encoder)
84 85 86 87 88
{
	struct flac_encoder *encoder = (struct flac_encoder *)_encoder;

	/* the real libFLAC cleanup was already performed by
	   flac_encoder_close(), so no real work here */
89
	delete encoder;
90 91 92
}

static bool
93
flac_encoder_setup(struct flac_encoder *encoder, unsigned bits_per_sample,
94
		   Error &error)
95 96 97
{
	if ( !FLAC__stream_encoder_set_compression_level(encoder->fse,
					encoder->compression)) {
98 99 100
		error.Format(config_domain,
			     "error setting flac compression to %d",
			     encoder->compression);
101 102
		return false;
	}
103

104 105
	if ( !FLAC__stream_encoder_set_channels(encoder->fse,
					encoder->audio_format.channels)) {
106 107 108
		error.Format(config_domain,
			     "error setting flac channels num to %d",
			     encoder->audio_format.channels);
109 110 111
		return false;
	}
	if ( !FLAC__stream_encoder_set_bits_per_sample(encoder->fse,
112
							bits_per_sample)) {
113 114 115
		error.Format(config_domain,
			     "error setting flac bit format to %d",
			     bits_per_sample);
116 117 118 119
		return false;
	}
	if ( !FLAC__stream_encoder_set_sample_rate(encoder->fse,
					encoder->audio_format.sample_rate)) {
120 121 122
		error.Format(config_domain,
			     "error setting flac sample rate to %d",
			     encoder->audio_format.sample_rate);
123 124 125 126 127 128
		return false;
	}
	return true;
}

static FLAC__StreamEncoderWriteStatus
129
flac_write_callback(gcc_unused const FLAC__StreamEncoder *fse,
130 131
		    const FLAC__byte data[],
		    size_t bytes,
132 133
		    gcc_unused unsigned samples,
		    gcc_unused unsigned current_frame, void *client_data)
134 135 136 137
{
	struct flac_encoder *encoder = (struct flac_encoder *) client_data;

	//transfer data to buffer
138
	encoder->output_buffer->Append((const uint8_t *)data, bytes);
139 140 141 142

	return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
}

143
static void
144
flac_encoder_close(Encoder *_encoder)
145 146 147 148 149
{
	struct flac_encoder *encoder = (struct flac_encoder *)_encoder;

	FLAC__stream_encoder_delete(encoder->fse);

150
	encoder->expand_buffer.Clear();
151
	encoder->output_buffer.Destruct();
152 153
}

154
static bool
155
flac_encoder_open(Encoder *_encoder, AudioFormat &audio_format, Error &error)
156 157
{
	struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
158
	unsigned bits_per_sample;
159

160
	encoder->audio_format = audio_format;
161 162

	/* FIXME: flac should support 32bit as well */
163 164
	switch (audio_format.format) {
	case SampleFormat::S8:
165 166 167
		bits_per_sample = 8;
		break;

168
	case SampleFormat::S16:
169 170 171
		bits_per_sample = 16;
		break;

172
	case SampleFormat::S24_P32:
173 174 175 176 177
		bits_per_sample = 24;
		break;

	default:
		bits_per_sample = 24;
178
		audio_format.format = SampleFormat::S24_P32;
179
	}
180 181 182

	/* allocate the encoder */
	encoder->fse = FLAC__stream_encoder_new();
183
	if (encoder->fse == nullptr) {
184
		error.Set(flac_encoder_domain, "flac_new() failed");
185 186 187
		return false;
	}

188
	if (!flac_encoder_setup(encoder, bits_per_sample, error)) {
189 190 191 192
		FLAC__stream_encoder_delete(encoder->fse);
		return false;
	}

193
	encoder->output_buffer.Construct(8192);
194

195
	/* this immediately outputs data through callback */
196

197 198 199 200
	{
		FLAC__StreamEncoderInitStatus init_status;

		init_status = FLAC__stream_encoder_init_stream(encoder->fse,
201
			    flac_write_callback,
202
			    nullptr, nullptr, nullptr, encoder);
203

204
		if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
205 206 207
			error.Format(flac_encoder_domain,
				     "failed to initialize encoder: %s\n",
				     FLAC__StreamEncoderInitStatusString[init_status]);
208 209 210
			flac_encoder_close(_encoder);
			return false;
		}
211 212 213 214 215 216 217
	}

	return true;
}


static bool
218
flac_encoder_flush(Encoder *_encoder, gcc_unused Error &error)
219 220 221
{
	struct flac_encoder *encoder = (struct flac_encoder *)_encoder;

222 223
	(void) FLAC__stream_encoder_finish(encoder->fse);
	return true;
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
}

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
245
flac_encoder_write(Encoder *_encoder,
246
		   const void *data, size_t length,
247
		   gcc_unused Error &error)
248 249 250 251
{
	struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
	unsigned num_frames, num_samples;
	void *exbuffer;
252
	const void *buffer = nullptr;
253 254 255

	/* format conversion */

256
	num_frames = length / encoder->audio_format.GetFrameSize();
257 258
	num_samples = num_frames * encoder->audio_format.channels;

259
	switch (encoder->audio_format.format) {
260
	case SampleFormat::S8:
261
		exbuffer = encoder->expand_buffer.Get(length * 4);
262 263
		pcm8_to_flac((int32_t *)exbuffer, (const int8_t *)data,
			     num_samples);
264 265
		buffer = exbuffer;
		break;
266

267
	case SampleFormat::S16:
268
		exbuffer = encoder->expand_buffer.Get(length * 2);
269 270
		pcm16_to_flac((int32_t *)exbuffer, (const int16_t *)data,
			      num_samples);
271 272
		buffer = exbuffer;
		break;
273

274 275
	case SampleFormat::S24_P32:
	case SampleFormat::S32:
276 277
		/* nothing need to be done; format is the same for
		   both mpd and libFLAC */
278 279
		buffer = data;
		break;
280 281 282

	default:
		gcc_unreachable();
283 284 285 286
	}

	/* feed samples to encoder */

287 288 289
	if (!FLAC__stream_encoder_process_interleaved(encoder->fse,
						      (const FLAC__int32 *)buffer,
						      num_frames)) {
290
		error.Set(flac_encoder_domain, "flac encoder process failed");
291 292 293 294 295 296 297
		return false;
	}

	return true;
}

static size_t
298
flac_encoder_read(Encoder *_encoder, void *dest, size_t length)
299 300 301
{
	struct flac_encoder *encoder = (struct flac_encoder *)_encoder;

302
	return encoder->output_buffer->Read((uint8_t *)dest, length);
303 304
}

305
static const char *
306
flac_encoder_get_mime_type(gcc_unused Encoder *_encoder)
307
{
308
	return "audio/flac";
309 310
}

311
const EncoderPlugin flac_encoder_plugin = {
312 313 314 315 316 317 318 319 320 321 322 323
	"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,
324
};
325