blob: 9f73f69945c02eb0dadb5aa453263242fb267183 [file] [log] [blame]
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301/******************************************************************************
2 *
3 * Copyright (C) 2015 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* @file
24* ih264e_bitstream.c
25*
26* @brief
27* This file contains function definitions related to bitstream generation
28*
29* @author
30* ittiam
31*
32* @par List of Functions:
33* - ih264e_bitstrm_init()
34* - ih264e_put_bits()
35* - ih264e_put_bit()
36* - ih264e_put_rbsp_trailing_bits()
37* - ih264e_put_uev()
38* - ih264e_put_sev()
39* - ih264e_put_nal_start_code_prefix()
40*
41******************************************************************************
42*/
43
44/*****************************************************************************/
45/* File Includes */
46/*****************************************************************************/
47
48/* System include files */
49#include <stdio.h>
50#include <string.h>
51#include <stdlib.h>
52#include <assert.h>
53#include <stdarg.h>
54#include <math.h>
55
56/* User include files */
57#include "ih264e_config.h"
58#include "ih264_typedefs.h"
59#include "ih264_platform_macros.h"
60#include "ih264_debug.h"
61#include "ih264e_error.h"
62#include "ih264e_bitstream.h"
63#include "ih264_defs.h"
64#include "ih264_macros.h"
65
66
67/*****************************************************************************/
68/* Function Definitions */
69/*****************************************************************************/
70
71/**
72******************************************************************************
73*
74* @brief Initializes the encoder bitstream engine
75*
76* @par Description
77* This routine needs to be called at start of slice/frame encode
78*
79* @param[in] ps_bitstrm
80* pointer to bitstream context (handle)
81*
82* @param[in] p1_bitstrm_buf
83* bitstream buffer pointer where the encoded stream is generated in byte order
84*
85* @param[in] u4_max_bitstrm_size
86* indicates maximum bitstream buffer size. (in bytes)
87* If actual stream size exceeds the maximum size, encoder should
88* 1. Not corrupt data beyond u4_max_bitstrm_size bytes
89* 2. Report an error back to application indicating overflow
90*
91* @return success or failure error code
92*
93******************************************************************************
94*/
95IH264E_ERROR_T ih264e_bitstrm_init(bitstrm_t *ps_bitstrm,
96 UWORD8 *pu1_bitstrm_buf,
97 UWORD32 u4_max_bitstrm_size)
98{
99 ps_bitstrm->pu1_strm_buffer = pu1_bitstrm_buf;
Harinarayanan K Kd2d469e2015-06-19 14:45:44 +0530100 ps_bitstrm->u4_max_strm_size = u4_max_bitstrm_size;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530101
102 /* Default init values for other members of bitstream context */
103 ps_bitstrm->u4_strm_buf_offset = 0;
104 ps_bitstrm->u4_cur_word = 0;
105 ps_bitstrm->i4_bits_left_in_cw = WORD_SIZE;
106 ps_bitstrm->i4_zero_bytes_run = 0;
107
108 return(IH264E_SUCCESS);
109}
110
111/**
112******************************************************************************
113*
114* @brief puts a code with specified number of bits into the bitstream
115*
116* @par Description
117* inserts code_len number of bits from lsb of code_val into the
118* bitstream. updates context members like u4_cur_word, u4_strm_buf_offset and
119* i4_bits_left_in_cw. If the total words (u4_strm_buf_offset) exceeds max
120* available size (u4_max_strm_size), returns error without corrupting data
121* beyond it
122*
123* @param[in] ps_bitstrm
124* pointer to bitstream context (handle)
125*
126* @param[in] u4_code_val
127* code value that needs to be inserted in the stream.
128*
129* @param[in] code_len
130* indicates code length (in bits) of code_val that would be inserted in
131* bitstream buffer size. Range of length[1:WORD_SIZE]
132*
133* @remarks Assumptions: all bits from bit position code_len to msb of
134* code_val shall be zero
135*
136* @return success or failure error code
137*
138******************************************************************************
139*/
140IH264E_ERROR_T ih264e_put_bits(bitstrm_t *ps_bitstrm,
141 UWORD32 u4_code_val,
142 WORD32 code_len)
143{
144 UWORD32 u4_cur_word = ps_bitstrm->u4_cur_word;
145 WORD32 bits_left_in_cw = ps_bitstrm->i4_bits_left_in_cw;
146
147
148 /* check assumptions made in the module */
149 ASSERT(code_len > 0 && code_len <= WORD_SIZE);
150
151 if(code_len < WORD_SIZE)
152 ASSERT((u4_code_val >> code_len) == 0);
153
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530154 /* sanity check on the bitstream engine state */
155 ASSERT(bits_left_in_cw > 0 && bits_left_in_cw <= WORD_SIZE);
156
157 ASSERT(ps_bitstrm->i4_zero_bytes_run <= EPB_ZERO_BYTES);
158
159 ASSERT(ps_bitstrm->pu1_strm_buffer != NULL);
160
161
162 if(bits_left_in_cw > code_len)
163 {
164 /*******************************************************************/
165 /* insert the code in local bitstream word and return */
166 /* code is inserted in position of bits left (post decrement) */
167 /*******************************************************************/
168 bits_left_in_cw -= code_len;
169 u4_cur_word |= (u4_code_val << bits_left_in_cw);
170
171 ps_bitstrm->u4_cur_word = u4_cur_word;
172 ps_bitstrm->i4_bits_left_in_cw = bits_left_in_cw;
173
174 return(IH264E_SUCCESS);
175 }
176 else
177 {
178 /********************************************************************/
179 /* 1. insert partial code corresponding to bits left in cur word */
180 /* 2. flush all the bits of cur word to bitstream */
181 /* 3. insert emulation prevention bytes while flushing the bits */
182 /* 4. insert remaining bits of code starting from msb of cur word */
183 /* 5. update bitsleft in current word and stream buffer offset */
184 /********************************************************************/
Neelkamal Semwal10910bf2021-03-10 10:03:39 +0530185 IH264E_ERROR_T status = IH264E_SUCCESS;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530186 WORD32 i, rem_bits = (code_len - bits_left_in_cw);
187
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530188 /* insert parital code corresponding to bits left in cur word */
189 u4_cur_word |= u4_code_val >> rem_bits;
190
191 for(i = WORD_SIZE; i > 0; i -= 8)
192 {
193 /* flush the bits in cur word byte by byte and copy to stream */
194 UWORD8 u1_next_byte = (u4_cur_word >> (i-8)) & 0xFF;
195
Neelkamal Semwal10910bf2021-03-10 10:03:39 +0530196 status |= ih264e_put_byte_epb(ps_bitstrm, u1_next_byte);
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530197 }
198
199 /* insert the remaining bits from code val into current word */
200 u4_cur_word = rem_bits ? (u4_code_val << (WORD_SIZE - rem_bits)) : 0;
201
202 /* update the state variables and return success */
203 ps_bitstrm->u4_cur_word = u4_cur_word;
204 ps_bitstrm->i4_bits_left_in_cw = WORD_SIZE - rem_bits;
Neelkamal Semwal10910bf2021-03-10 10:03:39 +0530205 return (status);
206
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530207 }
208}
209
210/**
211******************************************************************************
212*
213* @brief inserts a 1-bit code into the bitstream
214*
215* @par Description
216* inserts 1bit lsb of code_val into the bitstream
217* updates context members like u4_cur_word, u4_strm_buf_offset and
218* i4_bits_left_in_cw. If the total words (u4_strm_buf_offset) exceeds max
219* available size (u4_max_strm_size), returns error without corrupting data
220* beyond it
221*
222* @param[in] ps_bitstrm
223* pointer to bitstream context (handle)
224*
225* @param[in] u4_code_val
226* code value that needs to be inserted in the stream.
227*
228* @remarks Assumptions: all bits from bit position 1 to msb of code_val
229* shall be zero
230*
231* @return success or failure error code
232*
233******************************************************************************
234*/
235IH264E_ERROR_T ih264e_put_bit(bitstrm_t *ps_bitstrm, UWORD32 u4_code_val)
236{
237 /* call the put bits function for 1 bit and return */
238 return(ih264e_put_bits(ps_bitstrm, u4_code_val, 1));
239}
240
241/**
242******************************************************************************
243*
244* @brief inserts rbsp trailing bits at the end of stream buffer (NAL)
245*
246* @par Description
247* inserts rbsp trailing bits, updates context members like u4_cur_word and
248* i4_bits_left_in_cw and flushes the same in the bitstream buffer. If the
249* total words (u4_strm_buf_offset) exceeds max available size
250* (u4_max_strm_size), returns error without corrupting data beyond it
251*
252* @param[in] ps_bitstrm
253* pointer to bitstream context (handle)
254*
255* @return success or failure error code
256*
257******************************************************************************
258*/
259IH264E_ERROR_T ih264e_put_rbsp_trailing_bits(bitstrm_t *ps_bitstrm)
260{
261 WORD32 i;
262 UWORD32 u4_cur_word = ps_bitstrm->u4_cur_word;
263 WORD32 bits_left_in_cw = ps_bitstrm->i4_bits_left_in_cw;
264 WORD32 bytes_left_in_cw = (bits_left_in_cw - 1) >> 3;
Neelkamal Semwal10910bf2021-03-10 10:03:39 +0530265 IH264E_ERROR_T status = IH264E_SUCCESS;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530266
267 /* insert a 1 at the end of current word and flush all the bits */
268 u4_cur_word |= (1 << (bits_left_in_cw - 1));
269
270 /* get the bits to be inserted in msbdb of the word */
271 //u4_cur_word <<= (WORD_SIZE - bytes_left_in_cw + 1);
272
273 for(i = WORD_SIZE; i > (bytes_left_in_cw*8); i -= 8)
274 {
275 /* flush the bits in cur word byte by byte and copy to stream */
276 UWORD8 u1_next_byte = (u4_cur_word >> (i-8)) & 0xFF;
277
Neelkamal Semwal10910bf2021-03-10 10:03:39 +0530278 status |= ih264e_put_byte_epb(ps_bitstrm, u1_next_byte);
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530279 }
280
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530281 /* Default init values for scratch variables of bitstream context */
282 ps_bitstrm->u4_cur_word = 0;
283 ps_bitstrm->i4_bits_left_in_cw = WORD_SIZE;
284 ps_bitstrm->i4_zero_bytes_run = 0;
285
Neelkamal Semwal10910bf2021-03-10 10:03:39 +0530286 return (status);
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530287}
288
289/**
290******************************************************************************
291*
292* @brief puts exponential golomb code of a unsigned integer into bitstream
293*
294* @par Description
295* computes uev code for given syntax element and inserts the same into
296* bitstream by calling ih264e_put_bits() interface.
297*
298* @param[in] ps_bitstrm
299* pointer to bitstream context (handle)
300*
301* @param[in] u4_code_num
302* unsigned integer input whose golomb code is written in stream
303*
304* @remarks Assumptions: code value can be represented in less than 16bits
305*
306* @return success or failure error code
307*
308******************************************************************************
309*/
310IH264E_ERROR_T ih264e_put_uev(bitstrm_t *ps_bitstrm, UWORD32 u4_code_num)
311{
312 UWORD32 u4_bit_str, u4_range;
313 IH264E_ERROR_T e_error;
314
315 /* convert the codenum to exp-golomb bit code: Table 9-2 JCTVC-J1003_d7 */
316 u4_bit_str = u4_code_num + 1;
317
318 /* get range of the bit string and put using put_bits() */
319 GETRANGE(u4_range, u4_bit_str);
320
321 e_error = ih264e_put_bits(ps_bitstrm, u4_bit_str, (2 * u4_range - 1));
322
323 return(e_error);
324}
325
326/**
327******************************************************************************
328*
329* @brief puts exponential golomb code of a signed integer into bitstream
330*
331* @par Description
332* computes sev code for given syntax element and inserts the same into
333* bitstream by calling ih264e_put_bits() interface.
334*
335* @param[in] ps_bitstrm
336* pointer to bitstream context (handle)
337*
338* @param[in] syntax_elem
339* signed integer input whose golomb code is written in stream
340*
341* @remarks Assumptions: code value can be represented in less than 16bits
342*
343* @return success or failure error code
344*
345******************************************************************************
346*/
347IH264E_ERROR_T ih264e_put_sev(bitstrm_t *ps_bitstrm, WORD32 syntax_elem)
348{
349 UWORD32 u4_code_num, u4_bit_str, u4_range;
350 IH264E_ERROR_T e_error;
351
352 /************************************************************************/
353 /* convert the codenum to exp-golomb bit code for signed syntax element */
354 /* See Table9-2 and Table 9-3 of standard JCTVC-J1003_d7 */
355 /************************************************************************/
356 if(syntax_elem <= 0)
357 {
358 /* codeNum for non-positive integer = 2*abs(x) : Table9-3 */
359 u4_code_num = ((-syntax_elem) << 1);
360 }
361 else
362 {
363 /* codeNum for positive integer = 2x-1 : Table9-3 */
364 u4_code_num = (syntax_elem << 1) - 1;
365 }
366
367 /* convert the codenum to exp-golomb bit code: Table 9-2 JCTVC-J1003_d7 */
368 u4_bit_str = u4_code_num + 1;
369
370 /* get range of the bit string and put using put_bits() */
371 GETRANGE(u4_range, u4_bit_str);
372
373 e_error = ih264e_put_bits(ps_bitstrm, u4_bit_str, (2 * u4_range - 1));
374
375 return(e_error);
376}
377
378/**
379******************************************************************************
380*
381* @brief insert NAL start code prefix (0x000001) into bitstream with an option
382* of inserting leading_zero_8bits (which makes startcode prefix as 0x00000001)
383*
384* @par Description
385* Although start code prefix could have been put by calling ih264e_put_bits(),
386* ih264e_put_nal_start_code_prefix() is specially added to make sure emulation
387* prevention insertion is not done for the NAL start code prefix which will
388* surely happen otherwise by calling ih264e_put_bits() interface.
389*
390* @param[in] ps_bitstrm
391* pointer to bitstream context (handle)
392*
393* @param[in] insert_leading_zero_8bits
394* flag indicating if one more zero bytes needs to prefixed before start code
395*
396* @return success or failure error code
397*
398******************************************************************************
399*/
400IH264E_ERROR_T ih264e_put_nal_start_code_prefix(bitstrm_t *ps_bitstrm,
401 WORD32 insert_leading_zero_8bits)
402{
403 UWORD32 u4_strm_buf_offset = ps_bitstrm->u4_strm_buf_offset;
404 UWORD8* pu1_strm_buf = ps_bitstrm->pu1_strm_buffer;
405
406 /* Bitstream buffer overflow check assuming worst case of 4 bytes */
407 if((u4_strm_buf_offset + 4) >= ps_bitstrm->u4_max_strm_size)
408 {
409 return(IH264E_BITSTREAM_BUFFER_OVERFLOW);
410 }
411
412 /* Insert leading zero 8 bits conditionally */
413 if(insert_leading_zero_8bits)
414 {
415 pu1_strm_buf[u4_strm_buf_offset] = 0x00;
416 u4_strm_buf_offset++;
417 }
418
419 /* Insert NAL start code prefix 0x00 00 01 */
420 pu1_strm_buf[u4_strm_buf_offset] = 0x00;
421 u4_strm_buf_offset++;
422
423 pu1_strm_buf[u4_strm_buf_offset] = 0x00;
424 u4_strm_buf_offset++;
425
426 pu1_strm_buf[u4_strm_buf_offset] = 0x01;
427 u4_strm_buf_offset++;
428
429 /* update the stream offset */
430 ps_bitstrm->u4_strm_buf_offset = u4_strm_buf_offset;
431
432 return (IH264E_SUCCESS);
433}
434