2
* Copyright (C) 2003-2013 The Music Player Daemon Project
3
* http://www.musicpd.org
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
#include "FlacEncoderPlugin.hxx"
22
#include "EncoderAPI.hxx"
23
#include "AudioFormat.hxx"
24
#include "pcm/PcmBuffer.hxx"
25
#include "ConfigError.hxx"
26
#include "util/Error.hxx"
27
#include "util/Domain.hxx"
28
#include "util/fifo_buffer.h"
31
#include "util/growing_fifo.h"
37
#include <FLAC/stream_encoder.h>
39
#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
40
#error libFLAC is too old
46
AudioFormat audio_format;
49
FLAC__StreamEncoder *fse;
51
PcmBuffer expand_buffer;
54
* This buffer will hold encoded data from libFLAC until it is
55
* picked up with flac_encoder_read().
57
struct fifo_buffer *output_buffer;
59
flac_encoder():encoder(flac_encoder_plugin) {}
62
static constexpr Domain flac_encoder_domain("vorbis_encoder");
65
flac_encoder_configure(struct flac_encoder *encoder, const config_param ¶m,
66
gcc_unused Error &error)
68
encoder->compression = param.GetBlockValue("compression", 5u);
74
flac_encoder_init(const config_param ¶m, Error &error)
76
flac_encoder *encoder = new flac_encoder();
78
/* load configuration from "param" */
79
if (!flac_encoder_configure(encoder, param, error)) {
80
/* configuration has failed, roll back and return error */
85
return &encoder->encoder;
89
flac_encoder_finish(Encoder *_encoder)
91
struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
93
/* the real libFLAC cleanup was already performed by
94
flac_encoder_close(), so no real work here */
99
flac_encoder_setup(struct flac_encoder *encoder, unsigned bits_per_sample,
102
if ( !FLAC__stream_encoder_set_compression_level(encoder->fse,
103
encoder->compression)) {
104
error.Format(config_domain,
105
"error setting flac compression to %d",
106
encoder->compression);
110
if ( !FLAC__stream_encoder_set_channels(encoder->fse,
111
encoder->audio_format.channels)) {
112
error.Format(config_domain,
113
"error setting flac channels num to %d",
114
encoder->audio_format.channels);
117
if ( !FLAC__stream_encoder_set_bits_per_sample(encoder->fse,
119
error.Format(config_domain,
120
"error setting flac bit format to %d",
124
if ( !FLAC__stream_encoder_set_sample_rate(encoder->fse,
125
encoder->audio_format.sample_rate)) {
126
error.Format(config_domain,
127
"error setting flac sample rate to %d",
128
encoder->audio_format.sample_rate);
134
static FLAC__StreamEncoderWriteStatus
135
flac_write_callback(gcc_unused const FLAC__StreamEncoder *fse,
136
const FLAC__byte data[],
138
gcc_unused unsigned samples,
139
gcc_unused unsigned current_frame, void *client_data)
141
struct flac_encoder *encoder = (struct flac_encoder *) client_data;
143
//transfer data to buffer
144
growing_fifo_append(&encoder->output_buffer, data, bytes);
146
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
150
flac_encoder_close(Encoder *_encoder)
152
struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
154
FLAC__stream_encoder_delete(encoder->fse);
156
encoder->expand_buffer.Clear();
157
fifo_buffer_free(encoder->output_buffer);
161
flac_encoder_open(Encoder *_encoder, AudioFormat &audio_format, Error &error)
163
struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
164
unsigned bits_per_sample;
166
encoder->audio_format = audio_format;
168
/* FIXME: flac should support 32bit as well */
169
switch (audio_format.format) {
170
case SampleFormat::S8:
174
case SampleFormat::S16:
175
bits_per_sample = 16;
178
case SampleFormat::S24_P32:
179
bits_per_sample = 24;
183
bits_per_sample = 24;
184
audio_format.format = SampleFormat::S24_P32;
187
/* allocate the encoder */
188
encoder->fse = FLAC__stream_encoder_new();
189
if (encoder->fse == nullptr) {
190
error.Set(flac_encoder_domain, "flac_new() failed");
194
if (!flac_encoder_setup(encoder, bits_per_sample, error)) {
195
FLAC__stream_encoder_delete(encoder->fse);
199
encoder->output_buffer = growing_fifo_new();
201
/* this immediately outputs data through callback */
204
FLAC__StreamEncoderInitStatus init_status;
206
init_status = FLAC__stream_encoder_init_stream(encoder->fse,
208
nullptr, nullptr, nullptr, encoder);
210
if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
211
error.Format(flac_encoder_domain,
212
"failed to initialize encoder: %s\n",
213
FLAC__StreamEncoderInitStatusString[init_status]);
214
flac_encoder_close(_encoder);
224
flac_encoder_flush(Encoder *_encoder, gcc_unused Error &error)
226
struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
228
(void) FLAC__stream_encoder_finish(encoder->fse);
233
pcm8_to_flac(int32_t *out, const int8_t *in, unsigned num_samples)
235
while (num_samples > 0) {
242
pcm16_to_flac(int32_t *out, const int16_t *in, unsigned num_samples)
244
while (num_samples > 0) {
251
flac_encoder_write(Encoder *_encoder,
252
const void *data, size_t length,
253
gcc_unused Error &error)
255
struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
256
unsigned num_frames, num_samples;
258
const void *buffer = nullptr;
260
/* format conversion */
262
num_frames = length / encoder->audio_format.GetFrameSize();
263
num_samples = num_frames * encoder->audio_format.channels;
265
switch (encoder->audio_format.format) {
266
case SampleFormat::S8:
267
exbuffer = encoder->expand_buffer.Get(length * 4);
268
pcm8_to_flac((int32_t *)exbuffer, (const int8_t *)data,
273
case SampleFormat::S16:
274
exbuffer = encoder->expand_buffer.Get(length * 2);
275
pcm16_to_flac((int32_t *)exbuffer, (const int16_t *)data,
280
case SampleFormat::S24_P32:
281
case SampleFormat::S32:
282
/* nothing need to be done; format is the same for
283
both mpd and libFLAC */
291
/* feed samples to encoder */
293
if (!FLAC__stream_encoder_process_interleaved(encoder->fse,
294
(const FLAC__int32 *)buffer,
296
error.Set(flac_encoder_domain, "flac encoder process failed");
304
flac_encoder_read(Encoder *_encoder, void *dest, size_t length)
306
struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
309
const char *src = (const char *)
310
fifo_buffer_read(encoder->output_buffer, &max_length);
314
if (length > max_length)
317
memcpy(dest, src, length);
318
fifo_buffer_consume(encoder->output_buffer, length);
323
flac_encoder_get_mime_type(gcc_unused Encoder *_encoder)
328
const EncoderPlugin flac_encoder_plugin = {
340
flac_encoder_get_mime_type,