decoder/flac: move functions into struct FlacDecoder

This commit is contained in:
Max Kellermann 2016-07-11 22:45:29 +02:00
parent 85b6a52662
commit d6ce2e3671
3 changed files with 69 additions and 65 deletions

View File

@ -82,40 +82,43 @@ FlacDecoder::Initialize(unsigned sample_rate, unsigned bits_per_sample,
return true; return true;
} }
static void inline void
flac_got_stream_info(FlacDecoder *data, FlacDecoder::OnStreamInfo(const FLAC__StreamMetadata_StreamInfo &stream_info)
const FLAC__StreamMetadata_StreamInfo *stream_info)
{ {
if (data->initialized || data->unsupported) if (initialized)
return; return;
data->Initialize(stream_info->sample_rate, Initialize(stream_info.sample_rate,
stream_info->bits_per_sample, stream_info.bits_per_sample,
stream_info->channels, stream_info.channels,
stream_info->total_samples); stream_info.total_samples);
} }
void flac_metadata_common_cb(const FLAC__StreamMetadata * block, inline void
FlacDecoder *data) FlacDecoder::OnVorbisComment(const FLAC__StreamMetadata_VorbisComment &vc)
{ {
if (data->unsupported) ReplayGainInfo rgi;
if (flac_parse_replay_gain(rgi, vc))
decoder_replay_gain(*GetDecoder(), &rgi);
decoder_mixramp(*GetDecoder(), flac_parse_mixramp(vc));
tag = flac_vorbis_comments_to_tag(&vc);
}
void
FlacDecoder::OnMetadata(const FLAC__StreamMetadata &metadata)
{
if (unsupported)
return; return;
ReplayGainInfo rgi; switch (metadata.type) {
switch (block->type) {
case FLAC__METADATA_TYPE_STREAMINFO: case FLAC__METADATA_TYPE_STREAMINFO:
flac_got_stream_info(data, &block->data.stream_info); OnStreamInfo(metadata.data.stream_info);
break; break;
case FLAC__METADATA_TYPE_VORBIS_COMMENT: case FLAC__METADATA_TYPE_VORBIS_COMMENT:
if (flac_parse_replay_gain(rgi, block->data.vorbis_comment)) OnVorbisComment(metadata.data.vorbis_comment);
decoder_replay_gain(*data->GetDecoder(), &rgi);
decoder_mixramp(*data->GetDecoder(),
flac_parse_mixramp(block->data.vorbis_comment));
data->tag = flac_vorbis_comments_to_tag(&block->data.vorbis_comment);
break; break;
default: default:
@ -123,24 +126,17 @@ void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
} }
} }
/** inline bool
* This function attempts to call decoder_initialized() in case there FlacDecoder::OnFirstFrame(const FLAC__FrameHeader &header)
* was no STREAMINFO block. This is allowed for nonseekable streams,
* where the server sends us only a part of the file, without
* providing the STREAMINFO block from the beginning of the file
* (e.g. when seeking with SqueezeBox Server).
*/
static bool
flac_got_first_frame(FlacDecoder *data, const FLAC__FrameHeader *header)
{ {
if (data->unsupported) if (unsupported)
return false; return false;
return data->Initialize(header->sample_rate, return Initialize(header.sample_rate,
header->bits_per_sample, header.bits_per_sample,
header->channels, header.channels,
/* unknown duration */ /* unknown duration */
0); 0);
} }
FLAC__uint64 FLAC__uint64
@ -162,27 +158,25 @@ FlacDecoder::GetDeltaPosition(const FLAC__StreamDecoder &sd)
} }
FLAC__StreamDecoderWriteStatus FLAC__StreamDecoderWriteStatus
flac_common_write(FlacDecoder *data, const FLAC__Frame * frame, FlacDecoder::OnWrite(const FLAC__Frame &frame,
const FLAC__int32 *const buf[], const FLAC__int32 *const buf[],
FLAC__uint64 nbytes) FLAC__uint64 nbytes)
{ {
void *buffer; if (!initialized && !OnFirstFrame(frame.header))
if (!data->initialized && !flac_got_first_frame(data, &frame->header))
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
size_t buffer_size = frame->header.blocksize * data->frame_size; size_t buffer_size = frame.header.blocksize * frame_size;
buffer = data->buffer.Get(buffer_size); void *data = buffer.Get(buffer_size);
flac_convert(buffer, frame->header.channels, flac_convert(data, frame.header.channels,
data->audio_format.format, buf, audio_format.format, buf,
0, frame->header.blocksize); 0, frame.header.blocksize);
unsigned bit_rate = nbytes * 8 * frame->header.sample_rate / unsigned bit_rate = nbytes * 8 * frame.header.sample_rate /
(1000 * frame->header.blocksize); (1000 * frame.header.blocksize);
auto cmd = decoder_data(*data->GetDecoder(), data->GetInputStream(), auto cmd = decoder_data(*GetDecoder(), GetInputStream(),
buffer, buffer_size, data, buffer_size,
bit_rate); bit_rate);
switch (cmd) { switch (cmd) {
case DecoderCommand::NONE: case DecoderCommand::NONE:

View File

@ -72,19 +72,30 @@ struct FlacDecoder : public FlacInput {
bool Initialize(unsigned sample_rate, unsigned bits_per_sample, bool Initialize(unsigned sample_rate, unsigned bits_per_sample,
unsigned channels, FLAC__uint64 total_frames); unsigned channels, FLAC__uint64 total_frames);
void OnMetadata(const FLAC__StreamMetadata &metadata);
FLAC__StreamDecoderWriteStatus OnWrite(const FLAC__Frame &frame,
const FLAC__int32 *const buf[],
FLAC__uint64 nbytes);
/** /**
* Calculate the delta (in bytes) between the last frame and * Calculate the delta (in bytes) between the last frame and
* the current frame. * the current frame.
*/ */
FLAC__uint64 GetDeltaPosition(const FLAC__StreamDecoder &sd); FLAC__uint64 GetDeltaPosition(const FLAC__StreamDecoder &sd);
private:
void OnStreamInfo(const FLAC__StreamMetadata_StreamInfo &stream_info);
void OnVorbisComment(const FLAC__StreamMetadata_VorbisComment &vc);
/**
* This function attempts to call decoder_initialized() in case there
* was no STREAMINFO block. This is allowed for nonseekable streams,
* where the server sends us only a part of the file, without
* providing the STREAMINFO block from the beginning of the file
* (e.g. when seeking with SqueezeBox Server).
*/
bool OnFirstFrame(const FLAC__FrameHeader &header);
}; };
void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
FlacDecoder *data);
FLAC__StreamDecoderWriteStatus
flac_common_write(FlacDecoder *data, const FLAC__Frame * frame,
const FLAC__int32 *const buf[],
FLAC__uint64 nbytes);
#endif /* _FLAC_COMMON_H */ #endif /* _FLAC_COMMON_H */

View File

@ -57,17 +57,16 @@ static void flacPrintErroredState(FLAC__StreamDecoderState state)
static void flacMetadata(gcc_unused const FLAC__StreamDecoder * dec, static void flacMetadata(gcc_unused const FLAC__StreamDecoder * dec,
const FLAC__StreamMetadata * block, void *vdata) const FLAC__StreamMetadata * block, void *vdata)
{ {
flac_metadata_common_cb(block, (FlacDecoder *) vdata); auto &fd = *(FlacDecoder *)vdata;
fd.OnMetadata(*block);
} }
static FLAC__StreamDecoderWriteStatus static FLAC__StreamDecoderWriteStatus
flac_write_cb(const FLAC__StreamDecoder *dec, const FLAC__Frame *frame, flac_write_cb(const FLAC__StreamDecoder *dec, const FLAC__Frame *frame,
const FLAC__int32 *const buf[], void *vdata) const FLAC__int32 *const buf[], void *vdata)
{ {
FlacDecoder *data = (FlacDecoder *) vdata; auto &fd = *(FlacDecoder *)vdata;
return fd.OnWrite(*frame, buf, fd.GetDeltaPosition(*dec));
return flac_common_write(data, frame, buf,
data->GetDeltaPosition(*dec));
} }
static bool static bool