Commit 68064f1a authored by Max Kellermann's avatar Max Kellermann

decoder/flac: move duplicate code to flac_data::Initialize()

parent 475ac76a
......@@ -59,6 +59,38 @@ flac_sample_format(unsigned bits_per_sample)
}
}
bool
flac_data::Initialize(unsigned sample_rate, unsigned bits_per_sample,
unsigned channels, FLAC__uint64 total_frames)
{
assert(!initialized);
assert(!unsupported);
::Error error;
if (!audio_format_init_checked(audio_format,
sample_rate,
flac_sample_format(bits_per_sample),
channels, error)) {
LogError(error);
unsupported = true;
return false;
}
frame_size = audio_format.GetFrameSize();
const auto duration = total_frames > 0
? SignedSongTime::FromScale<uint64_t>(total_frames,
audio_format.sample_rate)
: SignedSongTime::Negative();
decoder_initialized(decoder, audio_format,
input_stream.IsSeekable(),
duration);
initialized = true;
return true;
}
static void
flac_got_stream_info(struct flac_data *data,
const FLAC__StreamMetadata_StreamInfo *stream_info)
......@@ -66,20 +98,10 @@ flac_got_stream_info(struct flac_data *data,
if (data->initialized || data->unsupported)
return;
Error error;
if (!audio_format_init_checked(data->audio_format,
stream_info->sample_rate,
flac_sample_format(stream_info->bits_per_sample),
stream_info->channels, error)) {
LogError(error);
data->unsupported = true;
return;
}
data->frame_size = data->audio_format.GetFrameSize();
data->total_frames = stream_info->total_samples;
data->initialized = true;
data->Initialize(stream_info->sample_rate,
stream_info->bits_per_sample,
stream_info->channels,
stream_info->total_samples);
}
void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
......@@ -123,29 +145,11 @@ flac_got_first_frame(struct flac_data *data, const FLAC__FrameHeader *header)
if (data->unsupported)
return false;
Error error;
if (!audio_format_init_checked(data->audio_format,
header->sample_rate,
flac_sample_format(header->bits_per_sample),
header->channels, error)) {
LogError(error);
data->unsupported = true;
return false;
}
data->frame_size = data->audio_format.GetFrameSize();
const auto duration = SongTime::FromScale<uint64_t>(data->total_frames,
data->audio_format.sample_rate);
decoder_initialized(data->decoder, data->audio_format,
data->input_stream.IsSeekable(),
duration);
data->total_frames = 0; /* unkown duration */
data->initialized = true;
return true;
return data->Initialize(header->sample_rate,
header->bits_per_sample,
header->channels,
/* unknown duration */
0);
}
FLAC__StreamDecoderWriteStatus
......
......@@ -55,13 +55,6 @@ struct flac_data : public FlacInput {
AudioFormat audio_format;
/**
* The total number of frames in this song. 0 means unknown.
*
* This attribute is defined if "initialized" is true.
*/
FLAC__uint64 total_frames;
/**
* End of last frame's position within the stream. This is
* used for bit rate calculations.
*/
......@@ -73,6 +66,12 @@ struct flac_data : public FlacInput {
Tag tag;
flac_data(Decoder &decoder, InputStream &input_stream);
/**
* Wrapper for decoder_initialized().
*/
bool Initialize(unsigned sample_rate, unsigned bits_per_sample,
unsigned channels, FLAC__uint64 total_frames);
};
void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
......
......@@ -141,15 +141,6 @@ flac_decoder_initialize(struct flac_data *data, FLAC__StreamDecoder *sd)
if (data->initialized) {
/* done */
const auto duration2 = data->total_frames > 0
? SignedSongTime::FromScale<uint64_t>(data->total_frames,
data->audio_format.sample_rate)
: SignedSongTime::Negative();
decoder_initialized(data->decoder, data->audio_format,
data->input_stream.IsSeekable(),
duration2);
return true;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment