flac_encoder.c 8.99 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 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"
#include "encoder_api.h"
#include "encoder_plugin.h"
#include "audio_format.h"
#include "pcm_buffer.h"
25 26
#include "fifo_buffer.h"
#include "growing_fifo.h"
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

#include <assert.h>
#include <string.h>

#include <FLAC/stream_encoder.h>

struct flac_encoder {
	struct encoder encoder;

	struct audio_format audio_format;
	unsigned compression;

	FLAC__StreamEncoder *fse;

	struct pcm_buffer expand_buffer;

43 44 45 46 47
	/**
	 * This buffer will hold encoded data from libFLAC until it is
	 * picked up with flac_encoder_read().
	 */
	struct fifo_buffer *output_buffer;
48 49 50 51
};

extern const struct encoder_plugin flac_encoder_plugin;

52

53 54 55 56 57 58 59 60 61 62
static inline GQuark
flac_encoder_quark(void)
{
	return g_quark_from_static_string("flac_encoder");
}

static bool
flac_encoder_configure(struct flac_encoder *encoder,
		const struct config_param *param, G_GNUC_UNUSED GError **error)
{
63
	encoder->compression = config_get_block_unsigned(param,
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
						"compression", 5);

	return true;
}

static struct encoder *
flac_encoder_init(const struct config_param *param, GError **error)
{
	struct flac_encoder *encoder;

	encoder = g_new(struct flac_encoder, 1);
	encoder_struct_init(&encoder->encoder, &flac_encoder_plugin);

	/* load configuration from "param" */
	if (!flac_encoder_configure(encoder, param, error)) {
		/* configuration has failed, roll back and return error */
		g_free(encoder);
		return NULL;
	}

	return &encoder->encoder;
}

static void
flac_encoder_finish(struct encoder *_encoder)
{
	struct flac_encoder *encoder = (struct flac_encoder *)_encoder;

	/* the real libFLAC cleanup was already performed by
	   flac_encoder_close(), so no real work here */
	g_free(encoder);
}

static bool
98 99
flac_encoder_setup(struct flac_encoder *encoder, unsigned bits_per_sample,
		   GError **error)
100
{
101 102
#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
#else
103 104 105 106 107 108 109
	if ( !FLAC__stream_encoder_set_compression_level(encoder->fse,
					encoder->compression)) {
		g_set_error(error, flac_encoder_quark(), 0,
			    "error setting flac compression to %d",
			    encoder->compression);
		return false;
	}
110
#endif
111 112 113 114 115 116 117 118
	if ( !FLAC__stream_encoder_set_channels(encoder->fse,
					encoder->audio_format.channels)) {
		g_set_error(error, flac_encoder_quark(), 0,
			    "error setting flac channels num to %d",
			    encoder->audio_format.channels);
		return false;
	}
	if ( !FLAC__stream_encoder_set_bits_per_sample(encoder->fse,
119
							bits_per_sample)) {
120 121
		g_set_error(error, flac_encoder_quark(), 0,
			    "error setting flac bit format to %d",
122
			    bits_per_sample);
123 124 125 126 127 128 129 130 131 132 133 134 135 136
		return false;
	}
	if ( !FLAC__stream_encoder_set_sample_rate(encoder->fse,
					encoder->audio_format.sample_rate)) {
		g_set_error(error, flac_encoder_quark(), 0,
			    "error setting flac sample rate to %d",
			    encoder->audio_format.sample_rate);
		return false;
	}
	return true;
}

static FLAC__StreamEncoderWriteStatus
flac_write_callback(G_GNUC_UNUSED const FLAC__StreamEncoder *fse,
137 138 139 140 141 142 143
		    const FLAC__byte data[],
#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
		    unsigned bytes,
#else
		    size_t bytes,
#endif
		    G_GNUC_UNUSED unsigned samples,
144 145 146 147 148
	G_GNUC_UNUSED unsigned current_frame, void *client_data)
{
	struct flac_encoder *encoder = (struct flac_encoder *) client_data;

	//transfer data to buffer
149
	growing_fifo_append(&encoder->output_buffer, data, bytes);
150 151 152 153

	return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
}

154 155 156 157 158 159 160 161
static void
flac_encoder_close(struct encoder *_encoder)
{
	struct flac_encoder *encoder = (struct flac_encoder *)_encoder;

	FLAC__stream_encoder_delete(encoder->fse);

	pcm_buffer_deinit(&encoder->expand_buffer);
162
	fifo_buffer_free(encoder->output_buffer);
163 164
}

165 166 167 168 169
static bool
flac_encoder_open(struct encoder *_encoder, struct audio_format *audio_format,
		     GError **error)
{
	struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
170
	unsigned bits_per_sample;
171 172 173 174

	encoder->audio_format = *audio_format;

	/* FIXME: flac should support 32bit as well */
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	switch (audio_format->format) {
	case SAMPLE_FORMAT_S8:
		bits_per_sample = 8;
		break;

	case SAMPLE_FORMAT_S16:
		bits_per_sample = 16;
		break;

	case SAMPLE_FORMAT_S24_P32:
		bits_per_sample = 24;
		break;

	default:
		bits_per_sample = 24;
		audio_format->format = SAMPLE_FORMAT_S24_P32;
	}
192 193 194 195 196 197 198 199 200

	/* allocate the encoder */
	encoder->fse = FLAC__stream_encoder_new();
	if (encoder->fse == NULL) {
		g_set_error(error, flac_encoder_quark(), 0,
			    "flac_new() failed");
		return false;
	}

201
	if (!flac_encoder_setup(encoder, bits_per_sample, error)) {
202 203 204 205 206 207
		FLAC__stream_encoder_delete(encoder->fse);
		return false;
	}

	pcm_buffer_init(&encoder->expand_buffer);

208 209
	encoder->output_buffer = growing_fifo_new();

210
	/* this immediately outputs data through callback */
211

212 213 214 215 216 217 218 219 220 221 222
#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
	{
		FLAC__StreamEncoderState init_status;

		FLAC__stream_encoder_set_write_callback(encoder->fse,
					    flac_write_callback);

		init_status = FLAC__stream_encoder_init(encoder->fse);

		if (init_status != FLAC__STREAM_ENCODER_OK) {
			g_set_error(error, flac_encoder_quark(), 0,
223
			    "failed to initialize encoder: %s\n",
224 225 226 227 228 229 230 231 232 233
			    FLAC__StreamEncoderStateString[init_status]);
			flac_encoder_close(_encoder);
			return false;
		}
	}
#else
	{
		FLAC__StreamEncoderInitStatus init_status;

		init_status = FLAC__stream_encoder_init_stream(encoder->fse,
234 235 236
			    flac_write_callback,
			    NULL, NULL, NULL, encoder);

237 238
		if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
			g_set_error(error, flac_encoder_quark(), 0,
239
			    "failed to initialize encoder: %s\n",
240
			    FLAC__StreamEncoderInitStatusString[init_status]);
241 242 243
			flac_encoder_close(_encoder);
			return false;
		}
244
	}
245
#endif
246 247 248 249 250 251 252 253 254 255

	return true;
}


static bool
flac_encoder_flush(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
{
	struct flac_encoder *encoder = (struct flac_encoder *)_encoder;

256 257
	(void) FLAC__stream_encoder_finish(encoder->fse);
	return true;
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
}

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
flac_encoder_write(struct encoder *_encoder,
		      const void *data, size_t length,
		      G_GNUC_UNUSED GError **error)
{
	struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
	unsigned num_frames, num_samples;
	void *exbuffer;
	const void *buffer = NULL;

	/* format conversion */

	num_frames = length / audio_format_frame_size(&encoder->audio_format);
	num_samples = num_frames * encoder->audio_format.channels;

293 294
	switch (encoder->audio_format.format) {
	case SAMPLE_FORMAT_S8:
295 296 297 298
		exbuffer = pcm_buffer_get(&encoder->expand_buffer, length*4);
		pcm8_to_flac(exbuffer, data, num_samples);
		buffer = exbuffer;
		break;
299 300

	case SAMPLE_FORMAT_S16:
301 302 303 304
		exbuffer = pcm_buffer_get(&encoder->expand_buffer, length*2);
		pcm16_to_flac(exbuffer, data, num_samples);
		buffer = exbuffer;
		break;
305 306 307 308 309

	case SAMPLE_FORMAT_S24_P32:
	case SAMPLE_FORMAT_S32:
		/* nothing need to be done; format is the same for
		   both mpd and libFLAC */
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
		buffer = data;
		break;
	}

	/* feed samples to encoder */

	if (!FLAC__stream_encoder_process_interleaved(encoder->fse, buffer,
							num_frames)) {
		g_set_error(error, flac_encoder_quark(), 0,
			    "flac encoder process failed");
		return false;
	}

	return true;
}

static size_t
flac_encoder_read(struct encoder *_encoder, void *dest, size_t length)
{
	struct flac_encoder *encoder = (struct flac_encoder *)_encoder;

331 332 333 334 335
	size_t max_length;
	const char *src = fifo_buffer_read(encoder->output_buffer,
					   &max_length);
	if (src == NULL)
		return 0;
336

337 338
	if (length > max_length)
		length = max_length;
339

340 341
	memcpy(dest, src, length);
	fifo_buffer_consume(encoder->output_buffer, length);
342 343 344
	return length;
}

345 346 347
static const char *
flac_encoder_get_mime_type(G_GNUC_UNUSED struct encoder *_encoder)
{
348
	return "audio/flac";
349 350
}

351 352 353 354 355 356
const struct encoder_plugin flac_encoder_plugin = {
	.name = "flac",
	.init = flac_encoder_init,
	.finish = flac_encoder_finish,
	.open = flac_encoder_open,
	.close = flac_encoder_close,
357
	.end = flac_encoder_flush,
358 359 360
	.flush = flac_encoder_flush,
	.write = flac_encoder_write,
	.read = flac_encoder_read,
361
	.get_mime_type = flac_encoder_get_mime_type,
362
};
363