• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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);
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 
clear()46    inline void clear()
47    {
48       m_iBitsToGo     = 32;
49       m_uiOffset      = 0;
50       m_uintEncBuffer = 0;
51    };
52 
53    void append_byte_stream(d3d12_video_encoder_bitstream *pStream);
54 
set_start_code_prevention(bool bSCP)55    void set_start_code_prevention(bool bSCP)
56    {
57       m_bPreventStartCode = bSCP;
58    }
get_bits_count()59    int32_t get_bits_count()
60    {
61       return m_uiOffset * 8 + (32 - m_iBitsToGo);
62    }
get_byte_count()63    int32_t get_byte_count()
64    {
65       return m_uiOffset + ((32 - m_iBitsToGo) >> 3);
66    }
get_bitstream_buffer()67    uint8_t *get_bitstream_buffer()
68    {
69       return m_pBitsBuffer;
70    }
is_byte_aligned()71    bool is_byte_aligned()
72    {
73       if (m_bBufferOverflow) {
74          m_iBitsToGo = 32;
75       }
76       return !(m_iBitsToGo & 7);
77    }
get_num_bits_for_byte_align()78    int32_t get_num_bits_for_byte_align()
79    {
80       return (m_iBitsToGo & 7);
81    }
get_start_code_prevention_status()82    bool get_start_code_prevention_status()
83    {
84       return m_bPreventStartCode;
85    }
86    bool verify_buffer(uint32_t uiBytesToWrite);
87 
88  public:
89    bool m_bBufferOverflow;
90    bool m_bAllowReallocate;
91 
92  private:
93    void    write_byte_start_code_prevention(uint8_t u8Val);
94    bool    reallocate_buffer();
95    int32_t get_exp_golomb0_code_len(uint32_t uiVal);
96 
97    const uint8_t m_iLog_2_N[256] = {
98       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,
99       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,
100       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,
101       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,
102       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,
103       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,
104       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
105    };
106 
107  private:
108    uint8_t *m_pBitsBuffer;
109    uint32_t m_uiBitsBufferSize;
110    uint32_t m_uiOffset;
111 
112    bool     m_bExternalBuffer;
113    uint32_t m_uintEncBuffer;
114    int32_t  m_iBitsToGo;
115 
116    bool m_bPreventStartCode;
117 };
118 
119 #endif
120