decoder/flac: moved code to flac_data_get_audio_format()

Remove the audio_format attribute, add "frame_size" instead.  The
audio_format initialization and check is moved both to
flac_data_get_audio_format().
This commit is contained in:
Max Kellermann 2009-11-11 20:34:59 +01:00
parent 08b139f37c
commit 37181c9181
4 changed files with 51 additions and 32 deletions

View File

@ -58,19 +58,41 @@ flac_data_deinit(struct flac_data *data)
tag_free(data->tag);
}
bool
flac_data_get_audio_format(struct flac_data *data,
struct audio_format *audio_format)
{
if (!data->have_stream_info) {
g_warning("no STREAMINFO packet found");
return false;
}
audio_format_init(audio_format, data->stream_info.sample_rate,
data->stream_info.bits_per_sample,
data->stream_info.channels);
if (!audio_format_valid(audio_format)) {
g_warning("Invalid audio format: %u:%u:%u\n",
audio_format->sample_rate,
audio_format->bits,
audio_format->channels);
return false;
}
data->frame_size = audio_format_frame_size(audio_format);
return true;
}
void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
struct flac_data *data)
{
const FLAC__StreamMetadata_StreamInfo *si = &(block->data.stream_info);
switch (block->type) {
case FLAC__METADATA_TYPE_STREAMINFO:
data->stream_info = block->data.stream_info;
data->have_stream_info = true;
audio_format_init(&data->audio_format, si->sample_rate,
si->bits_per_sample, si->channels);
break;
case FLAC__METADATA_TYPE_VORBIS_COMMENT:
if (data->replay_gain_info)
replay_gain_info_free(data->replay_gain_info);
@ -113,8 +135,7 @@ flac_common_write(struct flac_data *data, const FLAC__Frame * frame,
FLAC__uint64 nbytes)
{
enum decoder_command cmd;
size_t buffer_size = frame->header.blocksize *
audio_format_frame_size(&data->audio_format);
size_t buffer_size = frame->header.blocksize * data->frame_size;
void *buffer;
float position;
unsigned bit_rate;

View File

@ -38,6 +38,11 @@
struct flac_data {
struct pcm_buffer buffer;
/**
* The size of one frame in the output buffer.
*/
unsigned frame_size;
/**
* Is the #stream_info member valid?
*/
@ -62,7 +67,6 @@ struct flac_data {
*/
FLAC__uint64 next_frame;
struct audio_format audio_format;
FLAC__uint64 position;
struct decoder *decoder;
struct input_stream *input_stream;
@ -78,6 +82,17 @@ flac_data_init(struct flac_data *data, struct decoder * decoder,
void
flac_data_deinit(struct flac_data *data);
/**
* Obtains the audio format from the stream_info attribute, and copies
* it to the specified #audio_format object. This also updates the
* frame_size attribute.
*
* @return true on success, false the audio format is not supported
*/
bool
flac_data_get_audio_format(struct flac_data *data,
struct audio_format *audio_format);
void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
struct flac_data *data);

View File

@ -391,28 +391,20 @@ static bool
flac_decoder_initialize(struct flac_data *data, FLAC__StreamDecoder *sd,
bool seekable, FLAC__uint64 duration)
{
struct audio_format audio_format;
if (!FLAC__stream_decoder_process_until_end_of_metadata(sd)) {
g_warning("problem reading metadata");
return false;
}
if (!data->have_stream_info) {
g_warning("no STREAMINFO packet found");
if (!flac_data_get_audio_format(data, &audio_format))
return false;
}
if (!audio_format_valid(&data->audio_format)) {
g_warning("Invalid audio format: %u:%u:%u\n",
data->audio_format.sample_rate,
data->audio_format.bits,
data->audio_format.channels);
return false;
}
if (duration == 0)
duration = data->stream_info.total_samples;
decoder_initialized(data->decoder, &data->audio_format,
decoder_initialized(data->decoder, &audio_format,
seekable,
(float)duration /
(float)data->stream_info.sample_rate);

View File

@ -288,6 +288,7 @@ oggflac_decode(struct decoder * mpd_decoder, struct input_stream *input_stream)
{
OggFLAC__SeekableStreamDecoder *decoder = NULL;
struct flac_data data;
struct audio_format audio_format;
if (ogg_stream_type_detect(input_stream) != FLAC)
return;
@ -302,20 +303,10 @@ oggflac_decode(struct decoder * mpd_decoder, struct input_stream *input_stream)
goto fail;
}
if (!data.have_stream_info) {
g_warning("no STREAMINFO packet found");
if (!flac_data_get_audio_format(&data, &audio_format))
goto fail;
}
if (!audio_format_valid(&data.audio_format)) {
g_warning("Invalid audio format: %u:%u:%u\n",
data.audio_format.sample_rate,
data.audio_format.bits,
data.audio_format.channels);
goto fail;
}
decoder_initialized(mpd_decoder, &data.audio_format,
decoder_initialized(mpd_decoder, &audio_format,
input_stream->seekable,
(float)data.stream_info.total_samples /
(float)data.stream_info.sample_rate);