1 /*
2  * Copyright 2003-2021 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 /*
21  * Common data structures and functions used by FLAC and OggFLAC
22  */
23 
24 #ifndef MPD_FLAC_COMMON_HXX
25 #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 	 * Does the FLAC file contain an unsupported audio format?
42 	 */
43 	bool unsupported = false;
44 
45 	/**
46 	 * The kbit_rate parameter for the next
47 	 * DecoderBridge::SubmitData() call.
48 	 */
49 	uint16_t kbit_rate;
50 
51 	FlacPcmImport pcm_import;
52 
53 	/**
54 	 * End of last frame's position within the stream.  This is
55 	 * used for bit rate calculations.
56 	 */
57 	FLAC__uint64 position = 0;
58 
59 	Tag tag;
60 
61 	/**
62 	 * Decoded PCM data obtained by our libFLAC write callback.
63 	 * If this is non-empty, then DecoderBridge::SubmitData()
64 	 * should be called.
65 	 */
66 	ConstBuffer<void> chunk = nullptr;
67 
FlacDecoderFlacDecoder68 	FlacDecoder(DecoderClient &_client, InputStream &_input_stream)
69 		:FlacInput(_input_stream, &_client) {}
70 
71 	/**
72 	 * Wrapper for DecoderClient::Ready().
73 	 */
74 	bool Initialize(unsigned sample_rate, unsigned bits_per_sample,
75 			unsigned channels, FLAC__uint64 total_frames);
76 
77 	void OnMetadata(const FLAC__StreamMetadata &metadata);
78 
79 	FLAC__StreamDecoderWriteStatus OnWrite(const FLAC__Frame &frame,
80 					       const FLAC__int32 *const buf[],
81 					       FLAC__uint64 nbytes);
82 
83 	/**
84 	 * Calculate the delta (in bytes) between the last frame and
85 	 * the current frame.
86 	 */
87 	FLAC__uint64 GetDeltaPosition(const FLAC__StreamDecoder &sd);
88 
89 private:
90 	void OnStreamInfo(const FLAC__StreamMetadata_StreamInfo &stream_info);
91 	void OnVorbisComment(const FLAC__StreamMetadata_VorbisComment &vc);
92 
93 	/**
94 	 * This function attempts to call DecoderClient::Ready() in case there
95 	 * was no STREAMINFO block.  This is allowed for nonseekable streams,
96 	 * where the server sends us only a part of the file, without
97 	 * providing the STREAMINFO block from the beginning of the file
98 	 * (e.g. when seeking with SqueezeBox Server).
99 	 */
100 	bool OnFirstFrame(const FLAC__FrameHeader &header);
101 };
102 
103 #endif /* _FLAC_COMMON_H */
104