1 /**************************************************************************
3 * Copyright 2013 Advanced Micro Devices, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Christian König <christian.koenig@amd.com>
35 * Functions for reading the raw byte sequence payload of H.264
41 #include "vl/vl_vlc.h"
49 * Initialize the RBSP object
51 static inline void vl_rbsp_init(struct vl_rbsp
*rbsp
, struct vl_vlc
*nal
, unsigned num_bits
)
53 unsigned valid
, bits_left
= vl_vlc_bits_left(nal
);
56 /* copy the position */
59 /* search for the end of the NAL unit */
60 while (vl_vlc_search_byte(nal
, num_bits
, 0x00)) {
61 if (vl_vlc_peekbits(nal
, 24) == 0x000001 ||
62 vl_vlc_peekbits(nal
, 32) == 0x00000001) {
63 vl_vlc_limit(&rbsp
->nal
, bits_left
- vl_vlc_bits_left(nal
));
66 vl_vlc_eatbits(nal
, 8);
69 valid
= vl_vlc_valid_bits(&rbsp
->nal
);
70 /* search for the emulation prevention three byte */
71 for (i
= 24; i
<= valid
; i
+= 8) {
72 if ((vl_vlc_peekbits(&rbsp
->nal
, i
) & 0xffffff) == 0x3) {
73 vl_vlc_removebits(&rbsp
->nal
, i
- 8, 8);
78 valid
= vl_vlc_valid_bits(&rbsp
->nal
);
80 rbsp
->escaped
= (valid
>= 16) ? 16 : ((valid
>= 8) ? 8 : 0);
84 * Make at least 16 more bits available
86 static inline void vl_rbsp_fillbits(struct vl_rbsp
*rbsp
)
88 unsigned valid
= vl_vlc_valid_bits(&rbsp
->nal
);
91 /* abort if we still have enough bits */
95 vl_vlc_fillbits(&rbsp
->nal
);
97 /* abort if we have less than 24 bits left in this nal */
98 if (vl_vlc_bits_left(&rbsp
->nal
) < 24)
101 /* check that we have enough bits left from the last fillbits */
102 assert(valid
>= rbsp
->escaped
);
104 /* handle the already escaped bits */
105 valid
-= rbsp
->escaped
;
107 /* search for the emulation prevention three byte */
109 bits
= vl_vlc_valid_bits(&rbsp
->nal
);
110 for (i
= valid
+ 24; i
<= bits
; i
+= 8) {
111 if ((vl_vlc_peekbits(&rbsp
->nal
, i
) & 0xffffff) == 0x3) {
112 vl_vlc_removebits(&rbsp
->nal
, i
- 8, 8);
113 rbsp
->escaped
= bits
- i
;
121 * Return an unsigned integer from the first n bits
123 static inline unsigned vl_rbsp_u(struct vl_rbsp
*rbsp
, unsigned n
)
128 vl_rbsp_fillbits(rbsp
);
129 return vl_vlc_get_uimsbf(&rbsp
->nal
, n
);
133 * Return an unsigned exponential Golomb encoded integer
135 static inline unsigned vl_rbsp_ue(struct vl_rbsp
*rbsp
)
139 vl_rbsp_fillbits(rbsp
);
140 while (!vl_vlc_get_uimsbf(&rbsp
->nal
, 1))
143 return (1 << bits
) - 1 + vl_rbsp_u(rbsp
, bits
);
147 * Return an signed exponential Golomb encoded integer
149 static inline signed vl_rbsp_se(struct vl_rbsp
*rbsp
)
151 signed codeNum
= vl_rbsp_ue(rbsp
);
153 return (codeNum
+ 1) >> 1;
155 return -(codeNum
>> 1);
159 * Are more data available in the RBSP ?
161 static inline bool vl_rbsp_more_data(struct vl_rbsp
*rbsp
)
163 unsigned bits
, value
;
165 if (vl_vlc_bits_left(&rbsp
->nal
) > 8)
168 bits
= vl_vlc_valid_bits(&rbsp
->nal
);
169 value
= vl_vlc_peekbits(&rbsp
->nal
, bits
);
170 if (value
== 0 || value
== (1 << (bits
- 1)))
176 #endif /* vl_rbsp_h */