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

This commit is contained in:
Max Kellermann 2016-07-08 22:44:23 +02:00
parent 475ac76a5f
commit 68064f1aa6
3 changed files with 47 additions and 53 deletions

View File

@ -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 static void
flac_got_stream_info(struct flac_data *data, flac_got_stream_info(struct flac_data *data,
const FLAC__StreamMetadata_StreamInfo *stream_info) const FLAC__StreamMetadata_StreamInfo *stream_info)
@ -66,20 +98,10 @@ flac_got_stream_info(struct flac_data *data,
if (data->initialized || data->unsupported) if (data->initialized || data->unsupported)
return; return;
Error error; data->Initialize(stream_info->sample_rate,
if (!audio_format_init_checked(data->audio_format, stream_info->bits_per_sample,
stream_info->sample_rate, stream_info->channels,
flac_sample_format(stream_info->bits_per_sample), stream_info->total_samples);
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;
} }
void flac_metadata_common_cb(const FLAC__StreamMetadata * block, 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) if (data->unsupported)
return false; return false;
Error error; return data->Initialize(header->sample_rate,
if (!audio_format_init_checked(data->audio_format, header->bits_per_sample,
header->sample_rate, header->channels,
flac_sample_format(header->bits_per_sample), /* unknown duration */
header->channels, error)) { 0);
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;
} }
FLAC__StreamDecoderWriteStatus FLAC__StreamDecoderWriteStatus

View File

@ -54,13 +54,6 @@ struct flac_data : public FlacInput {
*/ */
AudioFormat audio_format; 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 * End of last frame's position within the stream. This is
* used for bit rate calculations. * used for bit rate calculations.
@ -73,6 +66,12 @@ struct flac_data : public FlacInput {
Tag tag; Tag tag;
flac_data(Decoder &decoder, InputStream &input_stream); 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, void flac_metadata_common_cb(const FLAC__StreamMetadata * block,

View File

@ -141,15 +141,6 @@ flac_decoder_initialize(struct flac_data *data, FLAC__StreamDecoder *sd)
if (data->initialized) { if (data->initialized) {
/* done */ /* 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; return true;
} }