decoder/faad: convert to C++

This commit is contained in:
Max Kellermann 2013-04-17 22:33:59 +02:00
parent 31bc94160a
commit 257b42b87f
4 changed files with 80 additions and 51 deletions

View File

@ -537,7 +537,8 @@ libdecoder_plugins_a_SOURCES += \
endif endif
if HAVE_FAAD if HAVE_FAAD
libdecoder_plugins_a_SOURCES += src/decoder/faad_decoder_plugin.c libdecoder_plugins_a_SOURCES += \
src/decoder/FaadDecoderPlugin.cxx src/decoder/FaadDecoderPlugin.hxx
endif endif
if HAVE_XIPH if HAVE_XIPH

View File

@ -32,6 +32,7 @@
#include "decoder/WavpackDecoderPlugin.hxx" #include "decoder/WavpackDecoderPlugin.hxx"
#include "decoder/FfmpegDecoderPlugin.hxx" #include "decoder/FfmpegDecoderPlugin.hxx"
#include "decoder/GmeDecoderPlugin.hxx" #include "decoder/GmeDecoderPlugin.hxx"
#include "decoder/FaadDecoderPlugin.hxx"
#include <glib.h> #include <glib.h>
@ -41,7 +42,6 @@ extern const struct decoder_plugin mad_decoder_plugin;
extern const struct decoder_plugin mpg123_decoder_plugin; extern const struct decoder_plugin mpg123_decoder_plugin;
extern const struct decoder_plugin sndfile_decoder_plugin; extern const struct decoder_plugin sndfile_decoder_plugin;
extern const struct decoder_plugin audiofile_decoder_plugin; extern const struct decoder_plugin audiofile_decoder_plugin;
extern const struct decoder_plugin faad_decoder_plugin;
extern const struct decoder_plugin mpcdec_decoder_plugin; extern const struct decoder_plugin mpcdec_decoder_plugin;
extern const struct decoder_plugin modplug_decoder_plugin; extern const struct decoder_plugin modplug_decoder_plugin;
extern const struct decoder_plugin mikmod_decoder_plugin; extern const struct decoder_plugin mikmod_decoder_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,8 +18,11 @@
*/ */
#include "config.h" #include "config.h"
#include "FaadDecoderPlugin.hxx"
#include "decoder_api.h" #include "decoder_api.h"
extern "C" {
#include "decoder_buffer.h" #include "decoder_buffer.h"
}
#include "audio_check.h" #include "audio_check.h"
#include "tag_handler.h" #include "tag_handler.h"
@ -72,13 +75,13 @@ adts_check_frame(const unsigned char *data)
static size_t static size_t
adts_find_frame(struct decoder_buffer *buffer) adts_find_frame(struct decoder_buffer *buffer)
{ {
const unsigned char *data, *p;
size_t length, frame_length; size_t length, frame_length;
bool ret; bool ret;
while (true) { while (true) {
data = decoder_buffer_read(buffer, &length); const uint8_t *data = (const uint8_t *)
if (data == NULL || length < 8) { decoder_buffer_read(buffer, &length);
if (data == nullptr || length < 8) {
/* not enough data yet */ /* not enough data yet */
ret = decoder_buffer_fill(buffer); ret = decoder_buffer_fill(buffer);
if (!ret) if (!ret)
@ -89,8 +92,8 @@ adts_find_frame(struct decoder_buffer *buffer)
} }
/* find the 0xff marker */ /* find the 0xff marker */
p = memchr(data, 0xff, length); const uint8_t *p = (const uint8_t *)memchr(data, 0xff, length);
if (p == NULL) { if (p == nullptr) {
/* no marker - discard the buffer */ /* no marker - discard the buffer */
decoder_buffer_consume(buffer, length); decoder_buffer_consume(buffer, length);
continue; continue;
@ -120,8 +123,9 @@ adts_find_frame(struct decoder_buffer *buffer)
/* not enough data; discard this frame /* not enough data; discard this frame
to prevent a possible buffer to prevent a possible buffer
overflow */ overflow */
data = decoder_buffer_read(buffer, &length); data = (const uint8_t *)
if (data != NULL) decoder_buffer_read(buffer, &length);
if (data != nullptr)
decoder_buffer_consume(buffer, length); decoder_buffer_consume(buffer, length);
} }
@ -148,11 +152,10 @@ adts_song_duration(struct decoder_buffer *buffer)
if (frames == 0) { if (frames == 0) {
const unsigned char *data;
size_t buffer_length; size_t buffer_length;
const uint8_t *data = (const uint8_t *)
data = decoder_buffer_read(buffer, &buffer_length); decoder_buffer_read(buffer, &buffer_length);
assert(data != NULL); assert(data != nullptr);
assert(frame_length <= buffer_length); assert(frame_length <= buffer_length);
sample_rate = adts_sample_rates[(data[2] & 0x3c) >> 2]; sample_rate = adts_sample_rates[(data[2] & 0x3c) >> 2];
@ -173,7 +176,6 @@ faad_song_duration(struct decoder_buffer *buffer, struct input_stream *is)
{ {
size_t fileread; size_t fileread;
size_t tagsize; size_t tagsize;
const unsigned char *data;
size_t length; size_t length;
bool success; bool success;
@ -181,8 +183,9 @@ faad_song_duration(struct decoder_buffer *buffer, struct input_stream *is)
fileread = size >= 0 ? size : 0; fileread = size >= 0 ? size : 0;
decoder_buffer_fill(buffer); decoder_buffer_fill(buffer);
data = decoder_buffer_read(buffer, &length); const uint8_t *data = (const uint8_t *)
if (data == NULL) decoder_buffer_read(buffer, &length);
if (data == nullptr)
return -1; return -1;
tagsize = 0; tagsize = 0;
@ -199,8 +202,8 @@ faad_song_duration(struct decoder_buffer *buffer, struct input_stream *is)
if (!success) if (!success)
return -1; return -1;
data = decoder_buffer_read(buffer, &length); data = (const uint8_t *)decoder_buffer_read(buffer, &length);
if (data == NULL) if (data == nullptr)
return -1; return -1;
} }
@ -209,10 +212,10 @@ faad_song_duration(struct decoder_buffer *buffer, struct input_stream *is)
/* obtain the duration from the ADTS header */ /* obtain the duration from the ADTS header */
float song_length = adts_song_duration(buffer); float song_length = adts_song_duration(buffer);
input_stream_lock_seek(is, tagsize, SEEK_SET, NULL); input_stream_lock_seek(is, tagsize, SEEK_SET, nullptr);
data = decoder_buffer_read(buffer, &length); data = (const uint8_t *)decoder_buffer_read(buffer, &length);
if (data != NULL) if (data != nullptr)
decoder_buffer_consume(buffer, length); decoder_buffer_consume(buffer, length);
decoder_buffer_fill(buffer); decoder_buffer_fill(buffer);
@ -248,12 +251,6 @@ static bool
faad_decoder_init(NeAACDecHandle decoder, struct decoder_buffer *buffer, faad_decoder_init(NeAACDecHandle decoder, struct decoder_buffer *buffer,
struct audio_format *audio_format, GError **error_r) struct audio_format *audio_format, GError **error_r)
{ {
union {
/* deconst hack for libfaad */
const void *in;
void *out;
} u;
size_t length;
int32_t nbytes; int32_t nbytes;
uint32_t sample_rate; uint32_t sample_rate;
uint8_t channels; uint8_t channels;
@ -266,14 +263,18 @@ faad_decoder_init(NeAACDecHandle decoder, struct decoder_buffer *buffer,
uint32_t *sample_rate_p = &sample_rate; uint32_t *sample_rate_p = &sample_rate;
#endif #endif
u.in = decoder_buffer_read(buffer, &length); size_t length;
if (u.in == NULL) { const unsigned char *data = (const unsigned char *)
decoder_buffer_read(buffer, &length);
if (data == nullptr) {
g_set_error(error_r, faad_decoder_quark(), 0, g_set_error(error_r, faad_decoder_quark(), 0,
"Empty file"); "Empty file");
return false; return false;
} }
nbytes = NeAACDecInit(decoder, u.out, nbytes = NeAACDecInit(decoder,
/* deconst hack, libfaad requires this */
const_cast<unsigned char *>(data),
length, length,
sample_rate_p, &channels); sample_rate_p, &channels);
if (nbytes < 0) { if (nbytes < 0) {
@ -296,19 +297,16 @@ static const void *
faad_decoder_decode(NeAACDecHandle decoder, struct decoder_buffer *buffer, faad_decoder_decode(NeAACDecHandle decoder, struct decoder_buffer *buffer,
NeAACDecFrameInfo *frame_info) NeAACDecFrameInfo *frame_info)
{ {
union {
/* deconst hack for libfaad */
const void *in;
void *out;
} u;
size_t length; size_t length;
const unsigned char *data = (const unsigned char *)
u.in = decoder_buffer_read(buffer, &length); decoder_buffer_read(buffer, &length);
if (u.in == NULL) if (data == nullptr)
return NULL; return nullptr;
return NeAACDecDecode(decoder, frame_info, return NeAACDecDecode(decoder, frame_info,
u.out, length); /* deconst hack, libfaad requires this */
const_cast<unsigned char *>(data),
length);
} }
/** /**
@ -322,7 +320,7 @@ faad_get_file_time_float(struct input_stream *is)
struct decoder_buffer *buffer; struct decoder_buffer *buffer;
float length; float length;
buffer = decoder_buffer_new(NULL, is, buffer = decoder_buffer_new(nullptr, is,
FAAD_MIN_STREAMSIZE * AAC_MAX_CHANNELS); FAAD_MIN_STREAMSIZE * AAC_MAX_CHANNELS);
length = faad_song_duration(buffer, is); length = faad_song_duration(buffer, is);
@ -339,7 +337,7 @@ faad_get_file_time_float(struct input_stream *is)
decoder_buffer_fill(buffer); decoder_buffer_fill(buffer);
ret = faad_decoder_init(decoder, buffer, &audio_format, NULL); ret = faad_decoder_init(decoder, buffer, &audio_format, nullptr);
if (ret) if (ret)
length = 0; length = 0;
@ -371,7 +369,7 @@ faad_get_file_time(struct input_stream *is)
static void static void
faad_stream_decode(struct decoder *mpd_decoder, struct input_stream *is) faad_stream_decode(struct decoder *mpd_decoder, struct input_stream *is)
{ {
GError *error = NULL; GError *error = nullptr;
float total_time = 0; float total_time = 0;
struct audio_format audio_format; struct audio_format audio_format;
bool ret; bool ret;
@ -487,15 +485,20 @@ faad_scan_stream(struct input_stream *is,
return true; return true;
} }
static const char *const faad_suffixes[] = { "aac", NULL }; static const char *const faad_suffixes[] = { "aac", nullptr };
static const char *const faad_mime_types[] = { static const char *const faad_mime_types[] = {
"audio/aac", "audio/aacp", NULL "audio/aac", "audio/aacp", nullptr
}; };
const struct decoder_plugin faad_decoder_plugin = { const struct decoder_plugin faad_decoder_plugin = {
.name = "faad", "faad",
.stream_decode = faad_stream_decode, nullptr,
.scan_stream = faad_scan_stream, nullptr,
.suffixes = faad_suffixes, faad_stream_decode,
.mime_types = faad_mime_types, nullptr,
nullptr,
faad_scan_stream,
nullptr,
faad_suffixes,
faad_mime_types,
}; };

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_FAAD_DECODER_PLUGIN_HXX
#define MPD_FAAD_DECODER_PLUGIN_HXX
extern const struct decoder_plugin faad_decoder_plugin;
#endif