1 // Copyright (c) 2017 Intel Corporation
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #pragma once
22 
23 #include "umc_vp9_dec_defs.h"
24 
25 #ifdef MFX_ENABLE_VP9_VIDEO_DECODE
26 
27 #ifndef __UMC_VP9_BITSTREAM_H_
28 #define __UMC_VP9_BITSTREAM_H_
29 
30 namespace UMC_VP9_DECODER
31 {
32     class VP9DecoderFrame;
33     struct Loopfilter;
34 
35     class VP9Bitstream
36     {
37 
38     public:
39 
40         VP9Bitstream();
41         VP9Bitstream(uint8_t * const pb, const uint32_t maxsize);
42 
43         // Reset the bitstream with new data pointer
44         void Reset(uint8_t * const pb, const uint32_t maxsize);
45         // Reset the bitstream with new data pointer and bit offset
46         void Reset(uint8_t * const pb, int32_t offset, const uint32_t maxsize);
47 
48         // Returns number of decoded bytes since last reset
BytesDecoded()49         size_t BytesDecoded() const
50         {
51             return
52                 static_cast<size_t>(m_pbs - m_pbsBase) + (m_bitOffset / 8);
53         }
54 
55         // Returns number of decoded bits since last reset
BitsDecoded()56         size_t BitsDecoded() const
57         {
58             return
59                 static_cast<size_t>(m_pbs - m_pbsBase) * 8 + m_bitOffset;
60         }
61 
62         // Returns number of bytes left in bitstream array
BytesLeft()63         size_t BytesLeft() const
64         {
65             return
66                 (int32_t)m_maxBsSize - (int32_t) BytesDecoded();
67         }
68 
69         // Return bitstream array base address and size
70         void GetOrg(uint8_t **pbs, uint32_t *size);
71 
GetBit()72         uint32_t GetBit()
73         {
74             if (m_pbs >= m_pbsBase + m_maxBsSize)
75                 throw vp9_exception(UMC::UMC_ERR_NOT_ENOUGH_DATA);
76 
77             uint32_t const bit = (*m_pbs >> (7 - m_bitOffset)) & 1;
78             if (++m_bitOffset == 8)
79             {
80                 ++m_pbs;
81                 m_bitOffset = 0;
82             }
83 
84             return bit;
85         }
86 
87         uint32_t GetBits(uint32_t nbits);
88         uint32_t GetUe();
89         int32_t GetSe();
90 
91     protected:
92 
93         uint8_t* m_pbs;                                              // pointer to the current position of the buffer.
94         int32_t m_bitOffset;                                        // the bit position (0 to 31) in the dword pointed by m_pbs.
95         uint8_t* m_pbsBase;                                          // pointer to the first byte of the buffer.
96         uint32_t m_maxBsSize;                                        // maximum buffer size in bytes.
97     };
98 
99     inline
CheckSyncCode(VP9Bitstream * bs)100     bool CheckSyncCode(VP9Bitstream* bs)
101     {
102         return
103             bs->GetBits(8) == VP9_SYNC_CODE_0 &&
104             bs->GetBits(8) == VP9_SYNC_CODE_1 &&
105             bs->GetBits(8) == VP9_SYNC_CODE_2
106             ;
107     }
108 
109     void GetDisplaySize(VP9Bitstream*, VP9DecoderFrame*);
110     void GetFrameSize(VP9Bitstream*, VP9DecoderFrame*);
111     void GetBitDepthAndColorSpace(VP9Bitstream*, VP9DecoderFrame*);
112     void GetFrameSizeWithRefs(VP9Bitstream*, VP9DecoderFrame*);
113     void SetupLoopFilter(VP9Bitstream *pBs, Loopfilter*);
114 
115 } // namespace UMC_VP9_DECODER
116 
117 #endif // __UMC_VP9_BITSTREAM_H_
118 
119 #endif // MFX_ENABLE_VP9_VIDEO_DECODE
120