aboutsummaryrefslogtreecommitdiffstats
path: root/src/decoder
diff options
context:
space:
mode:
Diffstat (limited to 'src/decoder')
-rw-r--r--src/decoder/_flac_common.c124
-rw-r--r--src/decoder/_flac_common.h20
-rw-r--r--src/decoder/flac_plugin.c28
-rw-r--r--src/decoder/oggflac_plugin.c34
4 files changed, 105 insertions, 101 deletions
diff --git a/src/decoder/_flac_common.c b/src/decoder/_flac_common.c
index d8802a6a3..9b3272cf2 100644
--- a/src/decoder/_flac_common.c
+++ b/src/decoder/_flac_common.c
@@ -31,6 +31,8 @@ void
flac_data_init(struct flac_data *data, struct decoder * decoder,
struct input_stream *input_stream)
{
+ pcm_buffer_init(&data->buffer);
+
data->time = 0;
data->position = 0;
data->bit_rate = 0;
@@ -40,6 +42,18 @@ flac_data_init(struct flac_data *data, struct decoder * decoder,
data->tag = NULL;
}
+void
+flac_data_deinit(struct flac_data *data)
+{
+ pcm_buffer_deinit(&data->buffer);
+
+ if (data->replay_gain_info != NULL)
+ replay_gain_info_free(data->replay_gain_info);
+
+ if (data->tag != NULL)
+ tag_free(data->tag);
+}
+
static void
flac_find_float_comment(const FLAC__StreamMetadata *block,
const char *cmnt, float *fl, bool *found_r)
@@ -183,15 +197,31 @@ flac_parse_comment(struct tag *tag, const char *char_tnum,
return;
}
-void
+static void
flac_vorbis_comments_to_tag(struct tag *tag, const char *char_tnum,
- const FLAC__StreamMetadata *block)
+ const FLAC__StreamMetadata_VorbisComment *comment)
{
- FLAC__StreamMetadata_VorbisComment_Entry *comments =
- block->data.vorbis_comment.comments;
+ for (unsigned i = 0; i < comment->num_comments; ++i)
+ flac_parse_comment(tag, char_tnum, &comment->comments[i]);
+}
- for (unsigned i = block->data.vorbis_comment.num_comments; i > 0; --i)
- flac_parse_comment(tag, char_tnum, comments++);
+void
+flac_tag_apply_metadata(struct tag *tag, const char *track,
+ const FLAC__StreamMetadata *block)
+{
+ switch (block->type) {
+ case FLAC__METADATA_TYPE_VORBIS_COMMENT:
+ flac_vorbis_comments_to_tag(tag, track,
+ &block->data.vorbis_comment);
+ break;
+
+ case FLAC__METADATA_TYPE_STREAMINFO:
+ tag->time = flac_duration(&block->data.stream_info);
+ break;
+
+ default:
+ break;
+ }
}
void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
@@ -209,7 +239,8 @@ void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
flac_parse_replay_gain(block, data);
if (data->tag != NULL)
- flac_vorbis_comments_to_tag(data->tag, NULL, block);
+ flac_vorbis_comments_to_tag(data->tag, NULL,
+ &block->data.vorbis_comment);
default:
break;
@@ -290,14 +321,14 @@ flac_convert_8(int8_t *dest,
*dest++ = buf[c_chan][position];
}
-static void flac_convert(unsigned char *dest,
- unsigned int num_channels,
- unsigned int bytes_per_sample,
- const FLAC__int32 * const buf[],
- unsigned int position, unsigned int end)
+static void
+flac_convert(void *dest,
+ unsigned int num_channels, unsigned sample_format,
+ const FLAC__int32 *const buf[],
+ unsigned int position, unsigned int end)
{
- switch (bytes_per_sample) {
- case 2:
+ switch (sample_format) {
+ case 16:
if (num_channels == 2)
flac_convert_stereo16((int16_t*)dest, buf,
position, end);
@@ -306,12 +337,13 @@ static void flac_convert(unsigned char *dest,
position, end);
break;
- case 4:
+ case 24:
+ case 32:
flac_convert_32((int32_t*)dest, num_channels, buf,
position, end);
break;
- case 1:
+ case 8:
flac_convert_8((int8_t*)dest, num_channels, buf,
position, end);
break;
@@ -322,49 +354,31 @@ FLAC__StreamDecoderWriteStatus
flac_common_write(struct flac_data *data, const FLAC__Frame * frame,
const FLAC__int32 *const buf[])
{
- unsigned int c_samp;
- const unsigned int num_channels = frame->header.channels;
- const unsigned int bytes_per_sample =
- audio_format_sample_size(&data->audio_format);
- const unsigned int bytes_per_channel =
- bytes_per_sample * frame->header.channels;
- const unsigned int max_samples = FLAC_CHUNK_SIZE / bytes_per_channel;
- unsigned int num_samples;
enum decoder_command cmd;
+ size_t buffer_size = frame->header.blocksize *
+ audio_format_frame_size(&data->audio_format);
+ void *buffer;
+
+ buffer = pcm_buffer_get(&data->buffer, buffer_size);
+
+ flac_convert(buffer, data->audio_format.channels,
+ data->audio_format.bits, buf,
+ 0, frame->header.blocksize);
+
+ cmd = decoder_data(data->decoder, data->input_stream,
+ buffer, buffer_size,
+ data->time, data->bit_rate,
+ data->replay_gain_info);
+ switch (cmd) {
+ case DECODE_COMMAND_NONE:
+ case DECODE_COMMAND_START:
+ break;
- if (bytes_per_sample != 1 && bytes_per_sample != 2 &&
- bytes_per_sample != 4)
- /* exotic unsupported bit rate */
+ case DECODE_COMMAND_STOP:
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
- for (c_samp = 0; c_samp < frame->header.blocksize;
- c_samp += num_samples) {
- num_samples = frame->header.blocksize - c_samp;
- if (num_samples > max_samples)
- num_samples = max_samples;
-
- flac_convert(data->chunk,
- num_channels, bytes_per_sample, buf,
- c_samp, c_samp + num_samples);
-
- cmd = decoder_data(data->decoder, data->input_stream,
- data->chunk,
- num_samples * bytes_per_channel,
- data->time, data->bit_rate,
- data->replay_gain_info);
- switch (cmd) {
- case DECODE_COMMAND_NONE:
- case DECODE_COMMAND_START:
- break;
-
- case DECODE_COMMAND_STOP:
- return
- FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
-
- case DECODE_COMMAND_SEEK:
- return
- FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
- }
+ case DECODE_COMMAND_SEEK:
+ return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
diff --git a/src/decoder/_flac_common.h b/src/decoder/_flac_common.h
index 68de7e969..5f83acc70 100644
--- a/src/decoder/_flac_common.h
+++ b/src/decoder/_flac_common.h
@@ -24,7 +24,8 @@
#ifndef MPD_FLAC_COMMON_H
#define MPD_FLAC_COMMON_H
-#include "../decoder_api.h"
+#include "decoder_api.h"
+#include "pcm_buffer.h"
#include "config.h"
#include <glib.h>
@@ -145,7 +146,8 @@ typedef size_t flac_read_status_size_t;
#define FLAC_CHUNK_SIZE 4080
struct flac_data {
- unsigned char chunk[FLAC_CHUNK_SIZE];
+ struct pcm_buffer buffer;
+
float time;
unsigned int bit_rate;
struct audio_format audio_format;
@@ -157,11 +159,21 @@ struct flac_data {
struct tag *tag;
};
+static inline unsigned
+flac_duration(const FLAC__StreamMetadata_StreamInfo *stream_info)
+{
+ return (stream_info->total_samples + stream_info->sample_rate - 1) /
+ stream_info->sample_rate;
+}
+
/* initializes a given FlacData struct */
void
flac_data_init(struct flac_data *data, struct decoder * decoder,
struct input_stream *input_stream);
+void
+flac_data_deinit(struct flac_data *data);
+
void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
struct flac_data *data);
@@ -170,8 +182,8 @@ void flac_error_common_cb(const char *plugin,
struct flac_data *data);
void
-flac_vorbis_comments_to_tag(struct tag *tag, const char *char_tnum,
- const FLAC__StreamMetadata *block);
+flac_tag_apply_metadata(struct tag *tag, const char *track,
+ const FLAC__StreamMetadata *block);
FLAC__StreamDecoderWriteStatus
flac_common_write(struct flac_data *data, const FLAC__Frame * frame,
diff --git a/src/decoder/flac_plugin.c b/src/decoder/flac_plugin.c
index 9692ba49f..ee80d667a 100644
--- a/src/decoder/flac_plugin.c
+++ b/src/decoder/flac_plugin.c
@@ -268,12 +268,8 @@ flac_tag_load(const char *file, const char *char_tnum)
block = FLAC__metadata_simple_iterator_get_block(it);
if (!block)
break;
- if (block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
- flac_vorbis_comments_to_tag(tag, char_tnum, block);
- } else if (block->type == FLAC__METADATA_TYPE_STREAMINFO) {
- tag->time = ((float)block->data.stream_info.total_samples) /
- block->data.stream_info.sample_rate + 0.5;
- }
+
+ flac_tag_apply_metadata(tag, char_tnum, block);
FLAC__metadata_object_delete(block);
} while (FLAC__metadata_simple_iterator_next(it));
@@ -485,10 +481,7 @@ flac_decode_internal(struct decoder * decoder,
}
fail:
- if (data.replay_gain_info)
- replay_gain_info_free(data.replay_gain_info);
-
- tag_free(data.tag);
+ flac_data_deinit(&data);
if (flac_dec)
flac_delete(flac_dec);
@@ -670,8 +663,7 @@ fail:
if (pathname)
g_free(pathname);
- if (data.replay_gain_info)
- replay_gain_info_free(data.replay_gain_info);
+ flac_data_deinit(&data);
if (flac_dec)
flac_delete(flac_dec);
@@ -793,8 +785,7 @@ flac_filedecode_internal(struct decoder* decoder,
}
fail:
- if (data.replay_gain_info)
- replay_gain_info_free(data.replay_gain_info);
+ flac_data_deinit(&data);
if (flac_dec)
flac_delete(flac_dec);
@@ -853,13 +844,8 @@ oggflac_tag_dup(const char *file)
do {
if (!(block = FLAC__metadata_iterator_get_block(it)))
break;
- if (block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
- flac_vorbis_comments_to_tag(ret, NULL, block);
- } else if (block->type == FLAC__METADATA_TYPE_STREAMINFO) {
- ret->time = ((float)block->data.stream_info.
- total_samples) /
- block->data.stream_info.sample_rate + 0.5;
- }
+
+ flac_tag_apply_metadata(ret, NULL, block);
} while (FLAC__metadata_iterator_next(it));
FLAC__metadata_iterator_delete(it);
diff --git a/src/decoder/oggflac_plugin.c b/src/decoder/oggflac_plugin.c
index fedfdcb48..dff76be51 100644
--- a/src/decoder/oggflac_plugin.c
+++ b/src/decoder/oggflac_plugin.c
@@ -29,11 +29,8 @@
#include <assert.h>
#include <unistd.h>
-static void oggflac_cleanup(struct flac_data *data,
- OggFLAC__SeekableStreamDecoder * decoder)
+static void oggflac_cleanup(OggFLAC__SeekableStreamDecoder * decoder)
{
- if (data->replay_gain_info)
- replay_gain_info_free(data->replay_gain_info);
if (decoder)
OggFLAC__seekable_stream_decoder_delete(decoder);
}
@@ -173,17 +170,7 @@ static void of_metadata_dup_cb(G_GNUC_UNUSED const OggFLAC__SeekableStreamDecode
assert(data->tag != NULL);
- switch (block->type) {
- case FLAC__METADATA_TYPE_STREAMINFO:
- data->tag->time = ((float)block->data.stream_info.
- total_samples) /
- block->data.stream_info.sample_rate + 0.5;
- return;
- case FLAC__METADATA_TYPE_VORBIS_COMMENT:
- flac_vorbis_comments_to_tag(data->tag, NULL, block);
- default:
- break;
- }
+ flac_tag_apply_metadata(data->tag, NULL, block);
}
/* used by decode */
@@ -264,6 +251,7 @@ oggflac_tag_dup(const char *file)
struct input_stream input_stream;
OggFLAC__SeekableStreamDecoder *decoder;
struct flac_data data;
+ struct tag *tag;
if (!input_stream_open(&input_stream, file))
return NULL;
@@ -280,15 +268,18 @@ oggflac_tag_dup(const char *file)
* data.tag will be set or unset, that's all we care about */
decoder = full_decoder_init_and_read_metadata(&data, 1);
- oggflac_cleanup(&data, decoder);
+ oggflac_cleanup(decoder);
input_stream_close(&input_stream);
- if (!tag_is_defined(data.tag)) {
- tag_free(data.tag);
+ if (tag_is_defined(data.tag)) {
+ tag = data.tag;
data.tag = NULL;
- }
+ } else
+ tag = NULL;
+
+ flac_data_deinit(&data);
- return data.tag;
+ return tag;
}
static void
@@ -344,7 +335,8 @@ oggflac_decode(struct decoder * mpd_decoder, struct input_stream *input_stream)
}
fail:
- oggflac_cleanup(&data, decoder);
+ oggflac_cleanup(decoder);
+ flac_data_deinit(&data);
}
static const char *const oggflac_suffixes[] = { "ogg", "oga", NULL };