xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/d3d12/d3d12_video_encoder_bitstream.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © Microsoft Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #ifndef D3D12_VIDEO_ENC_BITSTREAM_H
25 #define D3D12_VIDEO_ENC_BITSTREAM_H
26 
27 #include "d3d12_video_types.h"
28 
29 class d3d12_video_encoder_bitstream
30 {
31  public:
32    d3d12_video_encoder_bitstream();
33    ~d3d12_video_encoder_bitstream();
34 
35  public:
36    void get_current_buffer_position_and_size(uint8_t **ppCurrBufPos, int32_t *pdwLeftBufSize);
37    void inc_current_offset(int32_t dwOffset);
38    bool create_bitstream(uint32_t uiInitBufferSize);
39    void setup_bitstream(uint32_t uiInitBufferSize, uint8_t *pBuffer, size_t initial_byte_offset);
40    void attach(uint8_t *pBitsBuffer, uint32_t uiBufferSize);
41    void put_bits(int32_t uiBitsCount, uint32_t iBitsVal);
42    void flush();
43    void exp_Golomb_ue(uint32_t uiVal);
44    void exp_Golomb_se(int32_t iVal);
45    void put_aligning_bits();
46    void put_trailing_bits();
47    void put_su_bits(uint16_t uiBitsCount, int32_t iBitsVal);
48    void put_ns_bits(uint16_t uiBitsCount, uint32_t iBitsVal);
49    uint16_t calculate_su_bits(uint16_t uiBitsCount, int32_t iBitsVal);
50    void put_le_bytes(size_t uiBytesCount, uint32_t iBitsVal);
51    void put_leb128_bytes(uint64_t iBitsVal);
52 
clear()53    inline void clear()
54    {
55       m_iBitsToGo = 32;
56       m_uiOffset = 0;
57       m_uintEncBuffer = 0;
58    };
59 
60    void append_byte_stream(d3d12_video_encoder_bitstream *pStream);
61 
set_start_code_prevention(bool bSCP)62    void set_start_code_prevention(bool bSCP)
63    {
64       m_bPreventStartCode = bSCP;
65    }
get_bits_count()66    int32_t get_bits_count()
67    {
68       return m_uiOffset * 8 + (32 - m_iBitsToGo);
69    }
get_byte_count()70    int32_t get_byte_count()
71    {
72       return m_uiOffset + ((32 - m_iBitsToGo) >> 3);
73    }
get_bitstream_buffer()74    uint8_t *get_bitstream_buffer()
75    {
76       return m_pBitsBuffer;
77    }
is_byte_aligned()78    bool is_byte_aligned()
79    {
80       if (m_bBufferOverflow) {
81          m_iBitsToGo = 32;
82       }
83       return !(m_iBitsToGo & 7);
84    }
get_num_bits_for_byte_align()85    int32_t get_num_bits_for_byte_align()
86    {
87       return (m_iBitsToGo & 7);
88    }
get_start_code_prevention_status()89    bool get_start_code_prevention_status()
90    {
91       return m_bPreventStartCode;
92    }
93    bool verify_buffer(uint32_t uiBytesToWrite);
94 
95  public:
96    bool m_bBufferOverflow;
97    bool m_bAllowReallocate;
98 
99  private:
100    void write_byte_start_code_prevention(uint8_t u8Val);
101    bool reallocate_buffer();
102    int32_t get_exp_golomb0_code_len(uint32_t uiVal);
103 
104    const uint8_t m_iLog_2_N[256] = {
105       0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5,
106       5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
107       6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
108       6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
109       7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
110       7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
111       7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
112    };
113 
114  private:
115    uint8_t *m_pBitsBuffer;
116    uint32_t m_uiBitsBufferSize;
117    uint32_t m_uiOffset;
118 
119    bool m_bExternalBuffer;
120    uint32_t m_uintEncBuffer;
121    int32_t m_iBitsToGo;
122 
123    bool m_bPreventStartCode;
124 };
125 
126 #endif
127