1 /******************************************************************************
2  *
3  * Copyright (C) 2018 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 
21 /**
22 ******************************************************************************
23 *
24 * @file
25 *  ihevce_bitstream.h
26 *
27 * @brief
28 *  This file contains encoder bitstream engine related structures and
29 *  interface prototypes
30 *
31 * @author
32 *  ittiam
33 *
34 ******************************************************************************
35 */
36 
37 #ifndef _IHEVCE_BITSTREAM_H_
38 #define _IHEVCE_BITSTREAM_H_
39 
40 /*****************************************************************************/
41 /* Constant Macros                                                           */
42 /*****************************************************************************/
43 
44 /**
45 ******************************************************************************
46 *  @brief      defines the maximum number of bits in a bitstream word
47 ******************************************************************************
48 */
49 #define WORD_SIZE 32
50 
51 /**
52 ******************************************************************************
53 *  @brief  The number of consecutive zero bytes for emulation prevention check
54 ******************************************************************************
55 */
56 #define EPB_ZERO_BYTES 2
57 
58 /**
59 ******************************************************************************
60 *  @brief  Emulation prevention insertion byte
61 ******************************************************************************
62 */
63 #define EPB_BYTE 0x03
64 
65 /**
66 ******************************************************************************
67 *  @brief  Maximum number of NALs in a frame
68 ******************************************************************************
69 */
70 #define MAX_NALS_IN_AU 256
71 
72 /*****************************************************************************/
73 /* Function Macros                                                           */
74 /*****************************************************************************/
75 
76 /**
77 ******************************************************************************
78 *  @brief   Macro to check if emulation prevention byte insertion is required
79 ******************************************************************************
80 */
81 #define INSERT_EPB(zero_run, next_byte) ((zero_run) == EPB_ZERO_BYTES) && (0 == ((next_byte)&0xFC))
82 
83 /**
84 ******************************************************************************
85 *  @brief   returns bits required to code a value
86 ******************************************************************************
87 */
88 #define UE_LENGTH(bits, x)                                                                         \
89     {                                                                                              \
90         UWORD32 r_bit;                                                                             \
91         GETRANGE(r_bit, x + 1)                                                                     \
92         bits = (((r_bit - 1) << 1) + 1);                                                           \
93     }
94 
95 /**
96 ******************************************************************************
97 *  @brief  Inserts 1 byte and Emulation Prevention Byte(if any) into bitstream
98 *          Increments the stream offset and zero run correspondingly
99 ******************************************************************************
100 */
101 #define PUTBYTE_EPB(ptr, off, byte, zero_run)                                                      \
102     {                                                                                              \
103         if(INSERT_EPB(zero_run, byte))                                                             \
104         {                                                                                          \
105             ptr[off] = EPB_BYTE;                                                                   \
106             off++;                                                                                 \
107             zero_run = 0;                                                                          \
108         }                                                                                          \
109                                                                                                    \
110         ptr[off] = byte;                                                                           \
111         off++;                                                                                     \
112         zero_run = byte ? 0 : zero_run + 1;                                                        \
113     }
114 
115 /**
116 ******************************************************************************
117 *  @brief  Ensures Byte alignment of the slice header
118 ******************************************************************************
119 */
120 
121 #define BYTE_ALIGNMENT(ps_bitstrm) ihevce_put_rbsp_trailing_bits(ps_bitstrm)
122 
123 /*****************************************************************************/
124 /* Structures                                                                */
125 /*****************************************************************************/
126 
127 /**
128 ******************************************************************************
129 *  @brief      Bitstream context for encoder
130 ******************************************************************************
131 */
132 typedef struct bitstrm
133 {
134     /** points to start of stream buffer.    */
135     UWORD8 *pu1_strm_buffer;
136 
137     /**
138    *  max bitstream size (in bytes).
139    *  Encoded stream shall not exceed this size.
140    */
141     UWORD32 u4_max_strm_size;
142 
143     /**
144   `*  byte offset (w.r.t pu1_strm_buffer) where next byte would be written
145    *  Bitstream engine makes sure it would not corrupt data beyond
146    *  u4_max_strm_size bytes
147                                */
148     UWORD32 u4_strm_buf_offset;
149 
150     /**
151    *  current bitstream word; It is a scratch word containing max of
152    *  WORD_SIZE bits. Will be copied to stream buffer when the word is
153    *  full
154    */
155     UWORD32 u4_cur_word;
156 
157     /**
158    *  signifies number of bits available in u4_cur_word
159    *  bits from msb to i4_bits_left_in_cw of u4_cur_word have already been
160    *  inserted next bits would be inserted from pos [i4_bits_left_in_cw-1]
161    *  Range of this variable [1 : WORD_SIZE]
162    */
163     WORD32 i4_bits_left_in_cw;
164 
165     /**
166    *  signifies the number of consecutive zero bytes propogated from previous
167    *  word. It is used for emulation prevention byte insertion in the stream
168    */
169     WORD32 i4_zero_bytes_run;
170 
171     /** Total number of NAL units in the output buffer; Shall not exceed
172    * MAX_NALS_IN_AU */
173     WORD32 i4_num_nal;
174 
175     /** Pointer to start of each NAL unit in the output buffer */
176     UWORD8 *apu1_nal_start[MAX_NALS_IN_AU];
177 
178 } bitstrm_t;
179 
180 /*****************************************************************************/
181 /* Extern Function Declarations                                              */
182 /*****************************************************************************/
183 
184 IHEVCE_ERROR_T
185     ihevce_bitstrm_init(bitstrm_t *ps_bitstrm, UWORD8 *pu1_bitstrm_buf, UWORD32 u4_max_bitstrm_size);
186 
187 IHEVCE_ERROR_T ihevce_put_bits(bitstrm_t *ps_bitstrm, UWORD32 u4_code_val, WORD32 code_len);
188 
189 IHEVCE_ERROR_T ihevce_put_bit(bitstrm_t *ps_bitstrm, UWORD32 u4_code_val);
190 
191 IHEVCE_ERROR_T ihevce_put_rbsp_trailing_bits(bitstrm_t *ps_bitstrm);
192 
193 IHEVCE_ERROR_T ihevce_put_uev(bitstrm_t *ps_bitstrm, UWORD32 u4_code_num);
194 
195 IHEVCE_ERROR_T ihevce_put_sev(bitstrm_t *ps_bitstrm, WORD32 syntax_elem);
196 
197 IHEVCE_ERROR_T
198     ihevce_put_nal_start_code_prefix(bitstrm_t *ps_bitstrm, WORD32 insert_leading_zero_8bits);
199 
200 #endif /* _IHEVCE_BITSTREAM_H_ */
201