1 /***
2     This file is part of snapcast
3     Copyright (C) 2014-2020  Johannes Pohl
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 3 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
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 ***/
18 
19 #ifndef OGG_DECODER_H
20 #define OGG_DECODER_H
21 #include "decoder.hpp"
22 #ifdef HAS_TREMOR
23 #include <tremor/ivorbiscodec.h>
24 #else
25 #include <vorbis/codec.h>
26 #endif
27 #include <ogg/ogg.h>
28 
29 namespace decoder
30 {
31 
32 class OggDecoder : public Decoder
33 {
34 public:
35     OggDecoder();
36     ~OggDecoder() override;
37     bool decode(msg::PcmChunk* chunk) override;
38     SampleFormat setHeader(msg::CodecHeader* chunk) override;
39 
40 private:
41     bool decodePayload(msg::PcmChunk* chunk);
42     template <typename T, typename IN_TYPE>
43     T clip(const IN_TYPE& value, const T& lower, const T& upper) const
44     {
45         auto val = static_cast<int64_t>(value);
46         if (val > upper)
47             return upper;
48         if (val < lower)
49             return lower;
50         return static_cast<T>(value);
51     }
52 
53     ogg_sync_state oy;   /// sync and verify incoming physical bitstream
54     ogg_stream_state os; /// take physical pages, weld into a logical stream of packets
55     ogg_page og;         /// one Ogg bitstream page. Vorbis packets are inside
56     ogg_packet op;       /// one raw packet of data for decode
57 
58     vorbis_info vi;      /// struct that stores all the static vorbis bitstream settings
59     vorbis_comment vc;   /// struct that stores all the bitstream user comments
60     vorbis_dsp_state vd; /// central working state for the packet->PCM decoder
61     vorbis_block vb;     /// local working space for packet->PCM decode
62 
63     SampleFormat sampleFormat_;
64 };
65 
66 } // namespace decoder
67 
68 #endif
69