2 * Copyright (C) <2011> Intel Corporation
3 * Copyright (C) <2011> Collabora Ltd.
4 * Copyright (C) <2011> Thibault Saunier <thibault.saunier@collabora.com>
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
11 * (C) 2005 Michal Benes <michal.benes@itonis.tv>
12 * (C) 2008 Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
31 * Common code for NAL parsing from h264 and h265 parsers.
38 #include "wfdnalutils.h"
40 /* Compute Ceil(Log2(v)) */
41 /* Derived from branchless code for integer log2(v) from:
42 <http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog> */
49 r = (v > 0xFFFF) << 4;
51 shift = (v > 0xFF) << 3;
54 shift = (v > 0xF) << 2;
57 shift = (v > 0x3) << 1;
64 /****** Nal parser ******/
67 wfd_nal_reader_init (WFDNalReader * nr, const guint8 * data, guint size)
74 nr->bits_in_cache = 0;
75 /* fill with something other than 0 to detect emulation prevention bytes */
76 nr->first_byte = 0xff;
81 wfd_nal_reader_read (WFDNalReader * nr, guint nbits)
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);
89 while (nr->bits_in_cache < nbits) {
91 gboolean check_three_byte;
93 check_three_byte = TRUE;
95 if (G_UNLIKELY (nr->byte >= nr->size))
98 byte = nr->data[nr->byte++];
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;
108 nr->cache = (nr->cache << 8) | nr->first_byte;
109 nr->first_byte = byte;
110 nr->bits_in_cache += 8;
116 /* Skips the specified amount of bits. This is only suitable to a
117 cacheable number of bits */
119 wfd_nal_reader_skip (WFDNalReader * nr, guint nbits)
121 g_assert (nbits <= 8 * sizeof (nr->cache));
123 if (G_UNLIKELY (!wfd_nal_reader_read (nr, nbits)))
126 nr->bits_in_cache -= nbits;
131 /* Generic version to skip any number of bits */
133 wfd_nal_reader_skip_long (WFDNalReader * nr, guint nbits)
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;
140 while (remaining > 0) {
141 if (!wfd_nal_reader_skip (nr, nbits))
150 wfd_nal_reader_get_pos (const WFDNalReader * nr)
152 return nr->byte * 8 - nr->bits_in_cache;
156 wfd_nal_reader_get_remaining (const WFDNalReader * nr)
158 return (nr->size - nr->byte) * 8 + nr->bits_in_cache;
162 wfd_nal_reader_get_epb_count (const WFDNalReader * nr)
167 #define WFD_NAL_READER_READ_BITS(bits) \
169 wfd_nal_reader_get_bits_uint##bits (WFDNalReader *nr, guint##bits *val, guint nbits) \
173 if (!wfd_nal_reader_read (nr, nbits)) \
176 /* bring the required bits down and truncate */ \
177 shift = nr->bits_in_cache - nbits; \
178 *val = nr->first_byte >> shift; \
180 *val |= nr->cache << (8 - shift); \
181 /* mask out required bits */ \
183 *val &= ((guint##bits)1 << nbits) - 1; \
185 nr->bits_in_cache = shift; \
190 WFD_NAL_READER_READ_BITS (8);
191 WFD_NAL_READER_READ_BITS (16);
192 WFD_NAL_READER_READ_BITS (32);
194 #define WFD_NAL_READER_PEEK_BITS(bits) \
196 wfd_nal_reader_peek_bits_uint##bits (const WFDNalReader *nr, guint##bits *val, guint nbits) \
201 return wfd_nal_reader_get_bits_uint##bits (&tmp, val, nbits); \
204 WFD_NAL_READER_PEEK_BITS (8);
207 wfd_nal_reader_get_ue (WFDNalReader * nr, guint32 * val)
213 if (G_UNLIKELY (!wfd_nal_reader_get_bits_uint8 (nr, &bit, 1))) {
221 ((!wfd_nal_reader_get_bits_uint8 (nr, &bit, 1)))
225 if (G_UNLIKELY (i > 32))
228 if (G_UNLIKELY (!wfd_nal_reader_get_bits_uint32 (nr, &value, i)))
231 *val = (1 << i) - 1 + value;
237 wfd_nal_reader_get_se (WFDNalReader * nr, gint32 * val)
241 if (G_UNLIKELY (!wfd_nal_reader_get_ue (nr, &value)))
245 *val = (value / 2) + 1;
253 wfd_nal_reader_is_byte_aligned (WFDNalReader * nr)
255 if (nr->bits_in_cache != 0)
261 wfd_nal_reader_has_more_data (WFDNalReader * nr)
264 guint remaining, nbits;
265 guint8 rbsp_stop_one_bit, zero_bits;
267 remaining = wfd_nal_reader_get_remaining (nr);
274 /* The spec defines that more_rbsp_data() searches for the last bit
275 equal to 1, and that it is the rbsp_stop_one_bit. Subsequent bits
276 until byte boundary is reached shall be zero.
278 This means that more_rbsp_data() is FALSE if the next bit is 1
279 and the remaining bits until byte boundary are zero. One way to
280 be sure that this bit was the very last one, is that every other
281 bit after we reached byte boundary are also set to zero.
282 Otherwise, if the next bit is 0 or if there are non-zero bits
283 afterwards, then then we have more_rbsp_data() */
284 if (!wfd_nal_reader_get_bits_uint8 (nr, &rbsp_stop_one_bit, 1))
286 if (!rbsp_stop_one_bit)
289 nbits = --remaining % 8;
290 while (remaining > 0) {
291 if (!wfd_nal_reader_get_bits_uint8 (nr, &zero_bits, nbits))
301 /*********** end of nal parser ***************/
304 scan_for_start_codes (const guint8 * data, guint size)
307 gst_byte_reader_init (&br, data, size);
309 /* NALU not empty, so we can at least expect 1 (even 2) bytes following sc */
310 return gst_byte_reader_masked_scan_uint32 (&br, 0xffffff00, 0x00000100,