1 // Copyright 2016 The Draco Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // File provides direct encoding of bits with arithmetic encoder interface.
16 #ifndef DRACO_COMPRESSION_BIT_CODERS_FOLDED_INTEGER_BIT_DECODER_H_
17 #define DRACO_COMPRESSION_BIT_CODERS_FOLDED_INTEGER_BIT_DECODER_H_
18 
19 #include <vector>
20 
21 #include "draco/core/decoder_buffer.h"
22 
23 namespace draco {
24 
25 // See FoldedBit32Encoder for more details.
26 template <class BitDecoderT>
27 class FoldedBit32Decoder {
28  public:
FoldedBit32Decoder()29   FoldedBit32Decoder() {}
~FoldedBit32Decoder()30   ~FoldedBit32Decoder() {}
31 
32   // Sets |source_buffer| as the buffer to decode bits from.
StartDecoding(DecoderBuffer * source_buffer)33   bool StartDecoding(DecoderBuffer *source_buffer) {
34     for (int i = 0; i < 32; i++) {
35       if (!folded_number_decoders_[i].StartDecoding(source_buffer)) {
36         return false;
37       }
38     }
39     return bit_decoder_.StartDecoding(source_buffer);
40   }
41 
42   // Decode one bit. Returns true if the bit is a 1, otherwise false.
DecodeNextBit()43   bool DecodeNextBit() { return bit_decoder_.DecodeNextBit(); }
44 
45   // Decode the next |nbits| and return the sequence in |value|. |nbits| must be
46   // > 0 and <= 32.
DecodeLeastSignificantBits32(int nbits,uint32_t * value)47   void DecodeLeastSignificantBits32(int nbits, uint32_t *value) {
48     uint32_t result = 0;
49     for (int i = 0; i < nbits; ++i) {
50       const bool bit = folded_number_decoders_[i].DecodeNextBit();
51       result = (result << 1) + bit;
52     }
53     *value = result;
54   }
55 
EndDecoding()56   void EndDecoding() {
57     for (int i = 0; i < 32; i++) {
58       folded_number_decoders_[i].EndDecoding();
59     }
60     bit_decoder_.EndDecoding();
61   }
62 
63  private:
Clear()64   void Clear() {
65     for (int i = 0; i < 32; i++) {
66       folded_number_decoders_[i].Clear();
67     }
68     bit_decoder_.Clear();
69   }
70 
71   std::array<BitDecoderT, 32> folded_number_decoders_;
72   BitDecoderT bit_decoder_;
73 };
74 
75 }  // namespace draco
76 
77 #endif  // DRACO_COMPRESSION_BIT_CODERS_FOLDED_INTEGER_BIT_DECODER_H_
78