1 /* Gstreamer
2  * Copyright (C) <2011> Intel Corporation
3  * Copyright (C) <2011> Collabora Ltd.
4  * Copyright (C) <2011> Thibault Saunier <thibault.saunier@collabora.com>
5  *
6  * Some bits C-c,C-v'ed and s/4/3 from h264parse and videoparsers/h264parse.c:
7  *    Copyright (C) <2010> Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
8  *    Copyright (C) <2010> Collabora Multimedia
9  *    Copyright (C) <2010> Nokia Corporation
10  *
11  *    (C) 2005 Michal Benes <michal.benes@itonis.tv>
12  *    (C) 2008 Wim Taymans <wim.taymans@gmail.com>
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Library General Public
16  * License as published by the Free Software Foundation; either
17  * version 2 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Library General Public License for more details.
23  *
24  * You should have received a copy of the GNU Library General Public
25  * License along with this library; if not, write to the
26  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
27  * Boston, MA 02110-1301, USA.
28  */
29 
30 /**
31  * Common code for NAL parsing from h264 and h265 parsers.
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #  include "config.h"
36 #endif
37 
38 #include "nalutils.h"
39 
40 /* Compute Ceil(Log2(v)) */
41 /* Derived from branchless code for integer log2(v) from:
42    <http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog> */
43 guint
ceil_log2(guint32 v)44 ceil_log2 (guint32 v)
45 {
46   guint r, shift;
47 
48   v--;
49   r = (v > 0xFFFF) << 4;
50   v >>= r;
51   shift = (v > 0xFF) << 3;
52   v >>= shift;
53   r |= shift;
54   shift = (v > 0xF) << 2;
55   v >>= shift;
56   r |= shift;
57   shift = (v > 0x3) << 1;
58   v >>= shift;
59   r |= shift;
60   r |= (v >> 1);
61   return r + 1;
62 }
63 
64 /****** Nal parser ******/
65 
66 void
nal_reader_init(NalReader * nr,const guint8 * data,guint size)67 nal_reader_init (NalReader * nr, const guint8 * data, guint size)
68 {
69   nr->data = data;
70   nr->size = size;
71   nr->n_epb = 0;
72 
73   nr->byte = 0;
74   nr->bits_in_cache = 0;
75   /* fill with something other than 0 to detect emulation prevention bytes */
76   nr->first_byte = 0xff;
77   nr->cache = 0xff;
78 }
79 
80 gboolean
nal_reader_read(NalReader * nr,guint nbits)81 nal_reader_read (NalReader * nr, guint nbits)
82 {
83   if (G_UNLIKELY (nr->byte * 8 + (nbits - nr->bits_in_cache) > nr->size * 8)) {
84     GST_DEBUG ("Can not read %u bits, bits in cache %u, Byte * 8 %u, size in "
85         "bits %u", nbits, nr->bits_in_cache, nr->byte * 8, nr->size * 8);
86     return FALSE;
87   }
88 
89   while (nr->bits_in_cache < nbits) {
90     guint8 byte;
91     gboolean check_three_byte;
92 
93     check_three_byte = TRUE;
94   next_byte:
95     if (G_UNLIKELY (nr->byte >= nr->size))
96       return FALSE;
97 
98     byte = nr->data[nr->byte++];
99 
100     /* check if the byte is a emulation_prevention_three_byte */
101     if (check_three_byte && byte == 0x03 && nr->first_byte == 0x00 &&
102         ((nr->cache & 0xff) == 0)) {
103       /* next byte goes unconditionally to the cache, even if it's 0x03 */
104       check_three_byte = FALSE;
105       nr->n_epb++;
106       goto next_byte;
107     }
108     nr->cache = (nr->cache << 8) | nr->first_byte;
109     nr->first_byte = byte;
110     nr->bits_in_cache += 8;
111   }
112 
113   return TRUE;
114 }
115 
116 /* Skips the specified amount of bits. This is only suitable to a
117    cacheable number of bits */
118 gboolean
nal_reader_skip(NalReader * nr,guint nbits)119 nal_reader_skip (NalReader * nr, guint nbits)
120 {
121   g_assert (nbits <= 8 * sizeof (nr->cache));
122 
123   if (G_UNLIKELY (!nal_reader_read (nr, nbits)))
124     return FALSE;
125 
126   nr->bits_in_cache -= nbits;
127 
128   return TRUE;
129 }
130 
131 /* Generic version to skip any number of bits */
132 gboolean
nal_reader_skip_long(NalReader * nr,guint nbits)133 nal_reader_skip_long (NalReader * nr, guint nbits)
134 {
135   /* Leave out enough bits in the cache once we are finished */
136   const guint skip_size = 4 * sizeof (nr->cache);
137   guint remaining = nbits;
138 
139   nbits %= skip_size;
140   while (remaining > 0) {
141     if (!nal_reader_skip (nr, nbits))
142       return FALSE;
143     remaining -= nbits;
144     nbits = skip_size;
145   }
146   return TRUE;
147 }
148 
149 guint
nal_reader_get_pos(const NalReader * nr)150 nal_reader_get_pos (const NalReader * nr)
151 {
152   return nr->byte * 8 - nr->bits_in_cache;
153 }
154 
155 guint
nal_reader_get_remaining(const NalReader * nr)156 nal_reader_get_remaining (const NalReader * nr)
157 {
158   return (nr->size - nr->byte) * 8 + nr->bits_in_cache;
159 }
160 
161 guint
nal_reader_get_epb_count(const NalReader * nr)162 nal_reader_get_epb_count (const NalReader * nr)
163 {
164   return nr->n_epb;
165 }
166 
167 #define NAL_READER_READ_BITS(bits) \
168 gboolean \
169 nal_reader_get_bits_uint##bits (NalReader *nr, guint##bits *val, guint nbits) \
170 { \
171   guint shift; \
172   \
173   if (!nal_reader_read (nr, nbits)) \
174     return FALSE; \
175   \
176   /* bring the required bits down and truncate */ \
177   shift = nr->bits_in_cache - nbits; \
178   *val = nr->first_byte >> shift; \
179   \
180   *val |= nr->cache << (8 - shift); \
181   /* mask out required bits */ \
182   if (nbits < bits) \
183     *val &= ((guint##bits)1 << nbits) - 1; \
184   \
185   nr->bits_in_cache = shift; \
186   \
187   return TRUE; \
188 } \
189 
190 NAL_READER_READ_BITS (8);
191 NAL_READER_READ_BITS (16);
192 NAL_READER_READ_BITS (32);
193 
194 #define NAL_READER_PEEK_BITS(bits) \
195 gboolean \
196 nal_reader_peek_bits_uint##bits (const NalReader *nr, guint##bits *val, guint nbits) \
197 { \
198   NalReader tmp; \
199   \
200   tmp = *nr; \
201   return nal_reader_get_bits_uint##bits (&tmp, val, nbits); \
202 }
203 
204 NAL_READER_PEEK_BITS (8);
205 
206 gboolean
nal_reader_get_ue(NalReader * nr,guint32 * val)207 nal_reader_get_ue (NalReader * nr, guint32 * val)
208 {
209   guint i = 0;
210   guint8 bit;
211   guint32 value;
212 
213   if (G_UNLIKELY (!nal_reader_get_bits_uint8 (nr, &bit, 1)))
214     return FALSE;
215 
216   while (bit == 0) {
217     i++;
218     if (G_UNLIKELY (!nal_reader_get_bits_uint8 (nr, &bit, 1)))
219       return FALSE;
220   }
221 
222   if (G_UNLIKELY (i > 31))
223     return FALSE;
224 
225   if (G_UNLIKELY (!nal_reader_get_bits_uint32 (nr, &value, i)))
226     return FALSE;
227 
228   *val = (1 << i) - 1 + value;
229 
230   return TRUE;
231 }
232 
233 gboolean
nal_reader_get_se(NalReader * nr,gint32 * val)234 nal_reader_get_se (NalReader * nr, gint32 * val)
235 {
236   guint32 value;
237 
238   if (G_UNLIKELY (!nal_reader_get_ue (nr, &value)))
239     return FALSE;
240 
241   if (value % 2)
242     *val = (value / 2) + 1;
243   else
244     *val = -(value / 2);
245 
246   return TRUE;
247 }
248 
249 gboolean
nal_reader_is_byte_aligned(NalReader * nr)250 nal_reader_is_byte_aligned (NalReader * nr)
251 {
252   if (nr->bits_in_cache != 0)
253     return FALSE;
254   return TRUE;
255 }
256 
257 gboolean
nal_reader_has_more_data(NalReader * nr)258 nal_reader_has_more_data (NalReader * nr)
259 {
260   NalReader nr_tmp;
261   guint remaining, nbits;
262   guint8 rbsp_stop_one_bit, zero_bits;
263 
264   remaining = nal_reader_get_remaining (nr);
265   if (remaining == 0)
266     return FALSE;
267 
268   nr_tmp = *nr;
269   nr = &nr_tmp;
270 
271   /* The spec defines that more_rbsp_data() searches for the last bit
272      equal to 1, and that it is the rbsp_stop_one_bit. Subsequent bits
273      until byte boundary is reached shall be zero.
274 
275      This means that more_rbsp_data() is FALSE if the next bit is 1
276      and the remaining bits until byte boundary are zero. One way to
277      be sure that this bit was the very last one, is that every other
278      bit after we reached byte boundary are also set to zero.
279      Otherwise, if the next bit is 0 or if there are non-zero bits
280      afterwards, then then we have more_rbsp_data() */
281   if (!nal_reader_get_bits_uint8 (nr, &rbsp_stop_one_bit, 1))
282     return FALSE;
283   if (!rbsp_stop_one_bit)
284     return TRUE;
285 
286   nbits = --remaining % 8;
287   while (remaining > 0) {
288     if (!nal_reader_get_bits_uint8 (nr, &zero_bits, nbits))
289       return FALSE;
290     if (zero_bits != 0)
291       return TRUE;
292     remaining -= nbits;
293     nbits = 8;
294   }
295   return FALSE;
296 }
297 
298 /***********  end of nal parser ***************/
299 
300 gint
scan_for_start_codes(const guint8 * data,guint size)301 scan_for_start_codes (const guint8 * data, guint size)
302 {
303   GstByteReader br;
304   gst_byte_reader_init (&br, data, size);
305 
306   /* NALU not empty, so we can at least expect 1 (even 2) bytes following sc */
307   return gst_byte_reader_masked_scan_uint32 (&br, 0xffffff00, 0x00000100,
308       0, size);
309 }
310