aboutsummaryrefslogtreecommitdiffstats
path: root/src/decoder/_flac_common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/decoder/_flac_common.c')
-rw-r--r--src/decoder/_flac_common.c124
1 files changed, 69 insertions, 55 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;