Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / media / filters / vp9_bool_decoder.h
1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef MEDIA_FILTERS_VP9_BOOL_DECODER_H_
6 #define MEDIA_FILTERS_VP9_BOOL_DECODER_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <memory>
12
13 #include "media/base/media_export.h"
14
15 namespace media {
16
17 class BitReader;
18
19 class MEDIA_EXPORT Vp9BoolDecoder {
20  public:
21   Vp9BoolDecoder();
22
23   Vp9BoolDecoder(const Vp9BoolDecoder&) = delete;
24   Vp9BoolDecoder& operator=(const Vp9BoolDecoder&) = delete;
25
26   ~Vp9BoolDecoder();
27
28   // |data| is the input buffer with |size| bytes.
29   // Returns true if read first marker bit successfully.
30   bool Initialize(const uint8_t* data, size_t size);
31
32   // Returns true if none of the reads since the last Initialize() call has
33   // gone beyond the end of available data.
34   bool IsValid() const { return valid_; }
35
36   // Reads one bit. B(p).
37   // If the read goes beyond the end of buffer, the return value is undefined.
38   bool ReadBool(int prob);
39
40   // Reads a literal. L(n).
41   // If the read goes beyond the end of buffer, the return value is undefined.
42   uint8_t ReadLiteral(int bits);
43
44   // Consumes padding bits up to end of data. Returns true if no
45   // padding bits or they are all zero.
46   bool ConsumePaddingBits();
47
48  private:
49   // The highest 8 bits of BigBool is actual "bool value". The remain bits
50   // are optimization of prefill buffer.
51   using BigBool = size_t;
52   // The size of "bool value" used for boolean decoding defined in spec.
53   const int kBoolSize = 8;
54   const int kBigBoolBitSize = sizeof(BigBool) * 8;
55
56   bool Fill();
57
58   std::unique_ptr<BitReader> reader_;
59
60   // Indicates if none of the reads since the last Initialize() call has gone
61   // beyond the end of available data.
62   bool valid_ = true;
63
64   BigBool bool_value_ = 0;
65
66   // Need to fill at least |count_to_fill_| bits. Negative value means extra
67   // bits pre-filled.
68   int count_to_fill_ = 0;
69   unsigned int bool_range_ = 0;
70 };
71
72 }  // namespace media
73
74 #endif  // MEDIA_FILTERS_VP9_BOOL_DECODER_H_