FlacCommon.hxx 2.91 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17 18 19 20 21
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/*
 * Common data structures and functions used by FLAC and OggFLAC
22 23
 */

24 25
#ifndef MPD_FLAC_COMMON_HXX
#define MPD_FLAC_COMMON_HXX
26

27
#include "FlacInput.hxx"
28
#include "FlacPcm.hxx"
29
#include "../DecoderAPI.hxx"
30
#include "util/ConstBuffer.hxx"
31

32
#include <FLAC/stream_decoder.h>
33

34
struct FlacDecoder : public FlacInput {
35
	/**
36
	 * Has DecoderClient::Ready() been called yet?
37
	 */
38
	bool initialized = false;
39 40

	/**
41 42
	 * Does the FLAC file contain an unsupported audio format?
	 */
43
	bool unsupported = false;
44

45 46 47 48 49 50
	/**
	 * The kbit_rate parameter for the next
	 * DecoderBridge::SubmitData() call.
	 */
	uint16_t kbit_rate;

51
	FlacPcmImport pcm_import;
52 53

	/**
54 55
	 * End of last frame's position within the stream.  This is
	 * used for bit rate calculations.
56
	 */
57
	FLAC__uint64 position = 0;
58

Max Kellermann's avatar
Max Kellermann committed
59
	Tag tag;
Max Kellermann's avatar
Max Kellermann committed
60

61 62 63 64 65 66 67
	/**
	 * Decoded PCM data obtained by our libFLAC write callback.
	 * If this is non-empty, then DecoderBridge::SubmitData()
	 * should be called.
	 */
	ConstBuffer<void> chunk = nullptr;

68 69
	FlacDecoder(DecoderClient &_client, InputStream &_input_stream)
		:FlacInput(_input_stream, &_client) {}
70 71

	/**
72
	 * Wrapper for DecoderClient::Ready().
73 74 75
	 */
	bool Initialize(unsigned sample_rate, unsigned bits_per_sample,
			unsigned channels, FLAC__uint64 total_frames);
76

77 78 79 80 81 82
	void OnMetadata(const FLAC__StreamMetadata &metadata);

	FLAC__StreamDecoderWriteStatus OnWrite(const FLAC__Frame &frame,
					       const FLAC__int32 *const buf[],
					       FLAC__uint64 nbytes);

83 84 85 86 87
	/**
	 * Calculate the delta (in bytes) between the last frame and
	 * the current frame.
	 */
	FLAC__uint64 GetDeltaPosition(const FLAC__StreamDecoder &sd);
88

89 90 91
private:
	void OnStreamInfo(const FLAC__StreamMetadata_StreamInfo &stream_info);
	void OnVorbisComment(const FLAC__StreamMetadata_VorbisComment &vc);
Max Kellermann's avatar
Max Kellermann committed
92

93
	/**
94
	 * This function attempts to call DecoderClient::Ready() in case there
95 96 97 98 99 100 101
	 * 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);
};
102

Avuton Olrich's avatar
Avuton Olrich committed
103
#endif /* _FLAC_COMMON_H */