1 /**************************************************************************
2 *
3 * Copyright 2013 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Christian König <[email protected]>
31 *
32 */
33
34 /*
35 * Functions for reading the raw byte sequence payload of H.264
36 */
37
38 #ifndef vl_rbsp_h
39 #define vl_rbsp_h
40
41 #include "util/vl_vlc.h"
42
43 struct vl_rbsp {
44 struct vl_vlc nal;
45 unsigned escaped;
46 unsigned removed;
47 bool emulation_bytes;
48 };
49
50 /**
51 * Initialize the RBSP object
52 */
vl_rbsp_init(struct vl_rbsp * rbsp,struct vl_vlc * nal,unsigned num_bits,bool emulation_bytes)53 static inline void vl_rbsp_init(struct vl_rbsp *rbsp, struct vl_vlc *nal, unsigned num_bits,
54 bool emulation_bytes)
55 {
56 unsigned valid, bits_left = vl_vlc_bits_left(nal);
57 int i;
58
59 /* copy the position */
60 rbsp->nal = *nal;
61
62 rbsp->escaped = 0;
63 rbsp->removed = 0;
64 rbsp->emulation_bytes = emulation_bytes;
65
66 if (!rbsp->emulation_bytes)
67 return;
68
69 /* search for the end of the NAL unit */
70 while (vl_vlc_search_byte(nal, num_bits, 0x00)) {
71 if (vl_vlc_peekbits(nal, 24) == 0x000001 ||
72 vl_vlc_peekbits(nal, 32) == 0x00000001) {
73 vl_vlc_limit(&rbsp->nal, bits_left - vl_vlc_bits_left(nal));
74 break;
75 }
76 vl_vlc_eatbits(nal, 8);
77 }
78
79 valid = vl_vlc_valid_bits(&rbsp->nal);
80 /* search for the emulation prevention three byte */
81 for (i = 24; i <= valid; i += 8) {
82 if ((vl_vlc_peekbits(&rbsp->nal, i) & 0xffffff) == 0x3) {
83 vl_vlc_removebits(&rbsp->nal, i - 8, 8);
84 i += 8;
85 }
86 }
87
88 valid = vl_vlc_valid_bits(&rbsp->nal);
89
90 rbsp->escaped = (valid >= 16) ? 16 : ((valid >= 8) ? 8 : 0);
91 }
92
93 /**
94 * Make at least 16 more bits available
95 */
vl_rbsp_fillbits(struct vl_rbsp * rbsp)96 static inline void vl_rbsp_fillbits(struct vl_rbsp *rbsp)
97 {
98 unsigned valid = vl_vlc_valid_bits(&rbsp->nal);
99 unsigned i, bits;
100
101 /* abort if we still have enough bits */
102 if (valid >= 32)
103 return;
104
105 vl_vlc_fillbits(&rbsp->nal);
106
107 /* nothing to do if no emulation prevention bytes in bitstream */
108 if (!rbsp->emulation_bytes)
109 return;
110
111 /* abort if we have less than 24 bits left in this nal */
112 if (vl_vlc_bits_left(&rbsp->nal) < 24)
113 return;
114
115 /* handle the already escaped bits */
116 valid -= rbsp->escaped;
117
118 /* search for the emulation prevention three byte */
119 rbsp->escaped = 16;
120 bits = vl_vlc_valid_bits(&rbsp->nal);
121 for (i = valid + 24; i <= bits; i += 8) {
122 if ((vl_vlc_peekbits(&rbsp->nal, i) & 0xffffff) == 0x3) {
123 vl_vlc_removebits(&rbsp->nal, i - 8, 8);
124 rbsp->escaped = bits - i;
125 bits -= 8;
126 rbsp->removed += 8;
127 i += 8;
128 }
129 }
130 }
131
132 /**
133 * Return an unsigned integer from the first n bits
134 */
vl_rbsp_u(struct vl_rbsp * rbsp,unsigned n)135 static inline unsigned vl_rbsp_u(struct vl_rbsp *rbsp, unsigned n)
136 {
137 if (n == 0)
138 return 0;
139
140 vl_rbsp_fillbits(rbsp);
141 if (n > 16)
142 vl_rbsp_fillbits(rbsp);
143 return vl_vlc_get_uimsbf(&rbsp->nal, n);
144 }
145
146 /**
147 * Return an unsigned exponential Golomb encoded integer
148 */
vl_rbsp_ue(struct vl_rbsp * rbsp)149 static inline unsigned vl_rbsp_ue(struct vl_rbsp *rbsp)
150 {
151 unsigned bits = 0;
152
153 vl_rbsp_fillbits(rbsp);
154 while (!vl_vlc_get_uimsbf(&rbsp->nal, 1)) {
155 ++bits;
156 if (bits == 16)
157 vl_rbsp_fillbits(rbsp);
158 }
159
160 return (1 << bits) - 1 + vl_rbsp_u(rbsp, bits);
161 }
162
163 /**
164 * Return an signed exponential Golomb encoded integer
165 */
vl_rbsp_se(struct vl_rbsp * rbsp)166 static inline signed vl_rbsp_se(struct vl_rbsp *rbsp)
167 {
168 signed codeNum = vl_rbsp_ue(rbsp);
169 if (codeNum & 1)
170 return (codeNum + 1) >> 1;
171 else
172 return -(codeNum >> 1);
173 }
174
175 /**
176 * Are more data available in the RBSP ?
177 */
vl_rbsp_more_data(struct vl_rbsp * rbsp)178 static inline bool vl_rbsp_more_data(struct vl_rbsp *rbsp)
179 {
180 unsigned bits, value;
181
182 if (vl_vlc_bits_left(&rbsp->nal) > 8)
183 return true;
184
185 bits = vl_vlc_valid_bits(&rbsp->nal);
186 value = vl_vlc_peekbits(&rbsp->nal, bits);
187 if (value == 0 || value == (1 << (bits - 1)))
188 return false;
189
190 return true;
191 }
192
193 #endif /* vl_rbsp_h */
194