encoder/flac: convert to C++

This commit is contained in:
Max Kellermann 2013-07-29 08:04:34 +02:00
parent cac3c159bc
commit a1a97d663e
4 changed files with 59 additions and 24 deletions

View File

@ -669,7 +669,8 @@ libencoder_plugins_a_SOURCES += src/encoder/twolame_encoder.c
endif endif
if ENABLE_FLAC_ENCODER if ENABLE_FLAC_ENCODER
libencoder_plugins_a_SOURCES += src/encoder/flac_encoder.c libencoder_plugins_a_SOURCES += \
src/encoder/FlacEncoderPlugin.cxx src/encoder/FlacEncoderPlugin.hxx
endif endif
else else

View File

@ -22,6 +22,7 @@
#include "encoder_plugin.h" #include "encoder_plugin.h"
#include "encoder/VorbisEncoderPlugin.hxx" #include "encoder/VorbisEncoderPlugin.hxx"
#include "encoder/OpusEncoderPlugin.hxx" #include "encoder/OpusEncoderPlugin.hxx"
#include "encoder/FlacEncoderPlugin.hxx"
#include <string.h> #include <string.h>
@ -29,7 +30,6 @@ extern const struct encoder_plugin null_encoder_plugin;
extern const struct encoder_plugin lame_encoder_plugin; extern const struct encoder_plugin lame_encoder_plugin;
extern const struct encoder_plugin twolame_encoder_plugin; extern const struct encoder_plugin twolame_encoder_plugin;
extern const struct encoder_plugin wave_encoder_plugin; extern const struct encoder_plugin wave_encoder_plugin;
extern const struct encoder_plugin flac_encoder_plugin;
const struct encoder_plugin *const encoder_plugins[] = { const struct encoder_plugin *const encoder_plugins[] = {
&null_encoder_plugin, &null_encoder_plugin,

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2003-2011 The Music Player Daemon Project * Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -18,12 +18,16 @@
*/ */
#include "config.h" #include "config.h"
#include "FlacEncoderPlugin.hxx"
#include "encoder_api.h" #include "encoder_api.h"
#include "encoder_plugin.h" #include "encoder_plugin.h"
#include "audio_format.h" #include "audio_format.h"
#include "pcm/pcm_buffer.h" #include "pcm/pcm_buffer.h"
#include "util/fifo_buffer.h" #include "util/fifo_buffer.h"
extern "C" {
#include "util/growing_fifo.h" #include "util/growing_fifo.h"
}
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
@ -82,7 +86,7 @@ flac_encoder_init(const struct config_param *param, GError **error)
if (!flac_encoder_configure(encoder, param, error)) { if (!flac_encoder_configure(encoder, param, error)) {
/* configuration has failed, roll back and return error */ /* configuration has failed, roll back and return error */
g_free(encoder); g_free(encoder);
return NULL; return nullptr;
} }
return &encoder->encoder; return &encoder->encoder;
@ -190,7 +194,7 @@ flac_encoder_open(struct encoder *_encoder, struct audio_format *audio_format,
/* allocate the encoder */ /* allocate the encoder */
encoder->fse = FLAC__stream_encoder_new(); encoder->fse = FLAC__stream_encoder_new();
if (encoder->fse == NULL) { if (encoder->fse == nullptr) {
g_set_error(error, flac_encoder_quark(), 0, g_set_error(error, flac_encoder_quark(), 0,
"flac_new() failed"); "flac_new() failed");
return false; return false;
@ -212,7 +216,7 @@ flac_encoder_open(struct encoder *_encoder, struct audio_format *audio_format,
init_status = FLAC__stream_encoder_init_stream(encoder->fse, init_status = FLAC__stream_encoder_init_stream(encoder->fse,
flac_write_callback, flac_write_callback,
NULL, NULL, NULL, encoder); nullptr, nullptr, nullptr, encoder);
if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) { if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
g_set_error(error, flac_encoder_quark(), 0, g_set_error(error, flac_encoder_quark(), 0,
@ -262,7 +266,7 @@ flac_encoder_write(struct encoder *_encoder,
struct flac_encoder *encoder = (struct flac_encoder *)_encoder; struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
unsigned num_frames, num_samples; unsigned num_frames, num_samples;
void *exbuffer; void *exbuffer;
const void *buffer = NULL; const void *buffer = nullptr;
/* format conversion */ /* format conversion */
@ -272,13 +276,15 @@ flac_encoder_write(struct encoder *_encoder,
switch (encoder->audio_format.format) { switch (encoder->audio_format.format) {
case SAMPLE_FORMAT_S8: case SAMPLE_FORMAT_S8:
exbuffer = pcm_buffer_get(&encoder->expand_buffer, length*4); exbuffer = pcm_buffer_get(&encoder->expand_buffer, length*4);
pcm8_to_flac(exbuffer, data, num_samples); pcm8_to_flac((int32_t *)exbuffer, (const int8_t *)data,
num_samples);
buffer = exbuffer; buffer = exbuffer;
break; break;
case SAMPLE_FORMAT_S16: case SAMPLE_FORMAT_S16:
exbuffer = pcm_buffer_get(&encoder->expand_buffer, length*2); exbuffer = pcm_buffer_get(&encoder->expand_buffer, length*2);
pcm16_to_flac(exbuffer, data, num_samples); pcm16_to_flac((int32_t *)exbuffer, (const int16_t *)data,
num_samples);
buffer = exbuffer; buffer = exbuffer;
break; break;
@ -292,7 +298,8 @@ flac_encoder_write(struct encoder *_encoder,
/* feed samples to encoder */ /* feed samples to encoder */
if (!FLAC__stream_encoder_process_interleaved(encoder->fse, buffer, if (!FLAC__stream_encoder_process_interleaved(encoder->fse,
(const FLAC__int32 *)buffer,
num_frames)) { num_frames)) {
g_set_error(error, flac_encoder_quark(), 0, g_set_error(error, flac_encoder_quark(), 0,
"flac encoder process failed"); "flac encoder process failed");
@ -308,9 +315,9 @@ flac_encoder_read(struct encoder *_encoder, void *dest, size_t length)
struct flac_encoder *encoder = (struct flac_encoder *)_encoder; struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
size_t max_length; size_t max_length;
const char *src = fifo_buffer_read(encoder->output_buffer, const char *src = (const char *)
&max_length); fifo_buffer_read(encoder->output_buffer, &max_length);
if (src == NULL) if (src == nullptr)
return 0; return 0;
if (length > max_length) if (length > max_length)
@ -328,15 +335,17 @@ flac_encoder_get_mime_type(G_GNUC_UNUSED struct encoder *_encoder)
} }
const struct encoder_plugin flac_encoder_plugin = { const struct encoder_plugin flac_encoder_plugin = {
.name = "flac", "flac",
.init = flac_encoder_init, flac_encoder_init,
.finish = flac_encoder_finish, flac_encoder_finish,
.open = flac_encoder_open, flac_encoder_open,
.close = flac_encoder_close, flac_encoder_close,
.end = flac_encoder_flush, flac_encoder_flush,
.flush = flac_encoder_flush, flac_encoder_flush,
.write = flac_encoder_write, nullptr,
.read = flac_encoder_read, nullptr,
.get_mime_type = flac_encoder_get_mime_type, flac_encoder_write,
flac_encoder_read,
flac_encoder_get_mime_type,
}; };

View File

@ -0,0 +1,25 @@
/*
* Copyright (C) 2003-2013 The Music Player Daemon Project
* 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.
*/
#ifndef MPD_ENCODER_FLAC_HXX
#define MPD_ENCODER_FLAC_HXX
extern const struct encoder_plugin flac_encoder_plugin;
#endif