• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <string.h>
40 
41 /* Compute Ceil(Log2(v)) */
42 /* Derived from branchless code for integer log2(v) from:
43    <http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog> */
44 guint
ceil_log2(guint32 v)45 ceil_log2 (guint32 v)
46 {
47   guint r, shift;
48 
49   v--;
50   r = (v > 0xFFFF) << 4;
51   v >>= r;
52   shift = (v > 0xFF) << 3;
53   v >>= shift;
54   r |= shift;
55   shift = (v > 0xF) << 2;
56   v >>= shift;
57   r |= shift;
58   shift = (v > 0x3) << 1;
59   v >>= shift;
60   r |= shift;
61   r |= (v >> 1);
62   return r + 1;
63 }
64 
65 /****** Nal parser ******/
66 
67 void
nal_reader_init(NalReader * nr,const guint8 * data,guint size)68 nal_reader_init (NalReader * nr, const guint8 * data, guint size)
69 {
70   nr->data = data;
71   nr->size = size;
72   nr->n_epb = 0;
73 
74   nr->byte = 0;
75   nr->bits_in_cache = 0;
76   /* fill with something other than 0 to detect emulation prevention bytes */
77   nr->first_byte = 0xff;
78   nr->epb_cache = 0xff;
79   nr->cache = 0xff;
80 }
81 
82 gboolean
nal_reader_read(NalReader * nr,guint nbits)83 nal_reader_read (NalReader * nr, guint nbits)
84 {
85   if (G_UNLIKELY (nr->byte * 8 + (nbits - nr->bits_in_cache) > nr->size * 8)) {
86     GST_DEBUG ("Can not read %u bits, bits in cache %u, Byte * 8 %u, size in "
87         "bits %u", nbits, nr->bits_in_cache, nr->byte * 8, nr->size * 8);
88     return FALSE;
89   }
90 
91   while (nr->bits_in_cache < nbits) {
92     guint8 byte;
93 
94   next_byte:
95     if (G_UNLIKELY (nr->byte >= nr->size))
96       return FALSE;
97 
98     byte = nr->data[nr->byte++];
99     nr->epb_cache = (nr->epb_cache << 8) | byte;
100 
101     /* check if the byte is a emulation_prevention_three_byte */
102     if ((nr->epb_cache & 0xffffff) == 0x3) {
103       nr->n_epb++;
104       goto next_byte;
105     }
106     nr->cache = (nr->cache << 8) | nr->first_byte;
107     nr->first_byte = byte;
108     nr->bits_in_cache += 8;
109   }
110 
111   return TRUE;
112 }
113 
114 /* Skips the specified amount of bits. This is only suitable to a
115    cacheable number of bits */
116 gboolean
nal_reader_skip(NalReader * nr,guint nbits)117 nal_reader_skip (NalReader * nr, guint nbits)
118 {
119   g_assert (nbits <= 8 * sizeof (nr->cache));
120 
121   if (G_UNLIKELY (!nal_reader_read (nr, nbits)))
122     return FALSE;
123 
124   nr->bits_in_cache -= nbits;
125 
126   return TRUE;
127 }
128 
129 /* Generic version to skip any number of bits */
130 gboolean
nal_reader_skip_long(NalReader * nr,guint nbits)131 nal_reader_skip_long (NalReader * nr, guint nbits)
132 {
133   /* Leave out enough bits in the cache once we are finished */
134   const guint skip_size = 4 * sizeof (nr->cache);
135   guint remaining = nbits;
136 
137   nbits %= skip_size;
138   while (remaining > 0) {
139     if (!nal_reader_skip (nr, nbits))
140       return FALSE;
141     remaining -= nbits;
142     nbits = skip_size;
143   }
144   return TRUE;
145 }
146 
147 guint
nal_reader_get_pos(const NalReader * nr)148 nal_reader_get_pos (const NalReader * nr)
149 {
150   return nr->byte * 8 - nr->bits_in_cache;
151 }
152 
153 guint
nal_reader_get_remaining(const NalReader * nr)154 nal_reader_get_remaining (const NalReader * nr)
155 {
156   return (nr->size - nr->byte) * 8 + nr->bits_in_cache;
157 }
158 
159 guint
nal_reader_get_epb_count(const NalReader * nr)160 nal_reader_get_epb_count (const NalReader * nr)
161 {
162   return nr->n_epb;
163 }
164 
165 #define NAL_READER_READ_BITS(bits) \
166 gboolean \
167 nal_reader_get_bits_uint##bits (NalReader *nr, guint##bits *val, guint nbits) \
168 { \
169   guint shift; \
170   \
171   if (!nal_reader_read (nr, nbits)) \
172     return FALSE; \
173   \
174   /* bring the required bits down and truncate */ \
175   shift = nr->bits_in_cache - nbits; \
176   *val = nr->first_byte >> shift; \
177   \
178   *val |= nr->cache << (8 - shift); \
179   /* mask out required bits */ \
180   if (nbits < bits) \
181     *val &= ((guint##bits)1 << nbits) - 1; \
182   \
183   nr->bits_in_cache = shift; \
184   \
185   return TRUE; \
186 } \
187 
188 NAL_READER_READ_BITS (8);
189 NAL_READER_READ_BITS (16);
190 NAL_READER_READ_BITS (32);
191 
192 #define NAL_READER_PEEK_BITS(bits) \
193 gboolean \
194 nal_reader_peek_bits_uint##bits (const NalReader *nr, guint##bits *val, guint nbits) \
195 { \
196   NalReader tmp; \
197   \
198   tmp = *nr; \
199   return nal_reader_get_bits_uint##bits (&tmp, val, nbits); \
200 }
201 
202 NAL_READER_PEEK_BITS (8);
203 
204 gboolean
nal_reader_get_ue(NalReader * nr,guint32 * val)205 nal_reader_get_ue (NalReader * nr, guint32 * val)
206 {
207   guint i = 0;
208   guint8 bit;
209   guint32 value;
210 
211   if (G_UNLIKELY (!nal_reader_get_bits_uint8 (nr, &bit, 1)))
212     return FALSE;
213 
214   while (bit == 0) {
215     i++;
216     if (G_UNLIKELY (!nal_reader_get_bits_uint8 (nr, &bit, 1)))
217       return FALSE;
218   }
219 
220   if (G_UNLIKELY (i > 31))
221     return FALSE;
222 
223   if (G_UNLIKELY (!nal_reader_get_bits_uint32 (nr, &value, i)))
224     return FALSE;
225 
226   *val = (1 << i) - 1 + value;
227 
228   return TRUE;
229 }
230 
231 gboolean
nal_reader_get_se(NalReader * nr,gint32 * val)232 nal_reader_get_se (NalReader * nr, gint32 * val)
233 {
234   guint32 value;
235 
236   if (G_UNLIKELY (!nal_reader_get_ue (nr, &value)))
237     return FALSE;
238 
239   if (value % 2)
240     *val = (value / 2) + 1;
241   else
242     *val = -(value / 2);
243 
244   return TRUE;
245 }
246 
247 gboolean
nal_reader_is_byte_aligned(NalReader * nr)248 nal_reader_is_byte_aligned (NalReader * nr)
249 {
250   if (nr->bits_in_cache != 0)
251     return FALSE;
252   return TRUE;
253 }
254 
255 gboolean
nal_reader_has_more_data(NalReader * nr)256 nal_reader_has_more_data (NalReader * nr)
257 {
258   NalReader nr_tmp;
259   guint remaining, nbits;
260   guint8 rbsp_stop_one_bit, zero_bits;
261 
262   remaining = nal_reader_get_remaining (nr);
263   if (remaining == 0)
264     return FALSE;
265 
266   nr_tmp = *nr;
267   nr = &nr_tmp;
268 
269   /* The spec defines that more_rbsp_data() searches for the last bit
270      equal to 1, and that it is the rbsp_stop_one_bit. Subsequent bits
271      until byte boundary is reached shall be zero.
272 
273      This means that more_rbsp_data() is FALSE if the next bit is 1
274      and the remaining bits until byte boundary are zero. One way to
275      be sure that this bit was the very last one, is that every other
276      bit after we reached byte boundary are also set to zero.
277      Otherwise, if the next bit is 0 or if there are non-zero bits
278      afterwards, then then we have more_rbsp_data() */
279   if (!nal_reader_get_bits_uint8 (nr, &rbsp_stop_one_bit, 1))
280     return FALSE;
281   if (!rbsp_stop_one_bit)
282     return TRUE;
283 
284   nbits = --remaining % 8;
285   while (remaining > 0) {
286     if (!nal_reader_get_bits_uint8 (nr, &zero_bits, nbits))
287       return FALSE;
288     if (zero_bits != 0)
289       return TRUE;
290     remaining -= nbits;
291     nbits = 8;
292   }
293   return FALSE;
294 }
295 
296 /***********  end of nal parser ***************/
297 
298 gint
scan_for_start_codes(const guint8 * data,guint size)299 scan_for_start_codes (const guint8 * data, guint size)
300 {
301   GstByteReader br;
302   gst_byte_reader_init (&br, data, size);
303 
304   /* NALU not empty, so we can at least expect 1 (even 2) bytes following sc */
305   return gst_byte_reader_masked_scan_uint32 (&br, 0xffffff00, 0x00000100,
306       0, size);
307 }
308 
309 void
nal_writer_init(NalWriter * nw,guint nal_prefix_size,gboolean packetized)310 nal_writer_init (NalWriter * nw, guint nal_prefix_size, gboolean packetized)
311 {
312   g_return_if_fail (nw != NULL);
313   g_return_if_fail ((packetized && nal_prefix_size > 1 && nal_prefix_size < 5)
314       || (!packetized && (nal_prefix_size == 3 || nal_prefix_size == 4)));
315 
316   gst_bit_writer_init (&nw->bw);
317   nw->nal_prefix_size = nal_prefix_size;
318   nw->packetized = packetized;
319 }
320 
321 void
nal_writer_reset(NalWriter * nw)322 nal_writer_reset (NalWriter * nw)
323 {
324   g_return_if_fail (nw != NULL);
325 
326   gst_bit_writer_reset (&nw->bw);
327   memset (nw, 0, sizeof (NalWriter));
328 }
329 
330 gboolean
nal_writer_do_rbsp_trailing_bits(NalWriter * nw)331 nal_writer_do_rbsp_trailing_bits (NalWriter * nw)
332 {
333   g_return_val_if_fail (nw != NULL, FALSE);
334 
335   if (!gst_bit_writer_put_bits_uint8 (&nw->bw, 1, 1)) {
336     GST_WARNING ("Cannot put trailing bits");
337     return FALSE;
338   }
339 
340   if (!gst_bit_writer_align_bytes (&nw->bw, 0)) {
341     GST_WARNING ("Cannot put align bits");
342     return FALSE;
343   }
344 
345   return TRUE;
346 }
347 
348 GstMemory *
nal_writer_reset_and_get_memory(NalWriter * nw)349 nal_writer_reset_and_get_memory (NalWriter * nw)
350 {
351   GstBitWriter bw;
352   gint i;
353   guint8 *src, *dst;
354   gsize size;
355   GstMemory *ret = NULL;
356   gpointer data;
357 
358   g_return_val_if_fail (nw != NULL, NULL);
359 
360   if ((GST_BIT_WRITER_BIT_SIZE (&nw->bw) >> 3) == 0) {
361     GST_WARNING ("No written byte");
362     goto done;
363   }
364 
365   if ((GST_BIT_WRITER_BIT_SIZE (&nw->bw) & 0x7) != 0) {
366     GST_WARNING ("Written stream is not byte aligned");
367     if (!nal_writer_do_rbsp_trailing_bits (nw))
368       goto done;
369   }
370 
371   /* scan to put emulation_prevention_three_byte */
372   size = GST_BIT_WRITER_BIT_SIZE (&nw->bw) >> 3;
373   src = GST_BIT_WRITER_DATA (&nw->bw);
374 
375   gst_bit_writer_init_with_size (&bw, size + nw->nal_prefix_size, FALSE);
376   for (i = 0; i < nw->nal_prefix_size - 1; i++)
377     gst_bit_writer_put_bits_uint8 (&bw, 0, 8);
378   gst_bit_writer_put_bits_uint8 (&bw, 1, 8);
379 
380   for (i = 0; i < size; i++) {
381     guint pos = (GST_BIT_WRITER_BIT_SIZE (&bw) >> 3);
382     dst = GST_BIT_WRITER_DATA (&bw);
383     if (pos >= nw->nal_prefix_size + 2 &&
384         dst[pos - 2] == 0 && dst[pos - 1] == 0 && src[i] <= 0x3) {
385       gst_bit_writer_put_bits_uint8 (&bw, 0x3, 8);
386     }
387 
388     gst_bit_writer_put_bits_uint8 (&bw, src[i], 8);
389   }
390 
391   size = bw.bit_size >> 3;
392   data = gst_bit_writer_reset_and_get_data (&bw);
393   ret = gst_memory_new_wrapped (0, data, size, 0, size, data, g_free);
394 
395   if (nw->packetized) {
396     GstMapInfo info;
397 
398     gst_memory_map (ret, &info, GST_MAP_WRITE);
399 
400     size = info.size - nw->nal_prefix_size;
401 
402     switch (nw->nal_prefix_size) {
403       case 1:
404         GST_WRITE_UINT8 (info.data, size);
405         break;
406       case 2:
407         GST_WRITE_UINT16_BE (info.data, size);
408         break;
409       case 3:
410         GST_WRITE_UINT24_BE (info.data, size);
411         break;
412       case 4:
413         GST_WRITE_UINT32_BE (info.data, size);
414         break;
415       default:
416         g_assert_not_reached ();
417         break;
418     }
419 
420     gst_memory_unmap (ret, &info);
421   }
422 
423 done:
424   gst_bit_writer_reset (&nw->bw);
425 
426   return ret;
427 }
428 
429 gboolean
nal_writer_put_bits_uint8(NalWriter * nw,guint8 value,guint nbits)430 nal_writer_put_bits_uint8 (NalWriter * nw, guint8 value, guint nbits)
431 {
432   g_return_val_if_fail (nw != NULL, FALSE);
433 
434   if (!gst_bit_writer_put_bits_uint8 (&nw->bw, value, nbits))
435     return FALSE;
436 
437   return TRUE;
438 }
439 
440 gboolean
nal_writer_put_bits_uint16(NalWriter * nw,guint16 value,guint nbits)441 nal_writer_put_bits_uint16 (NalWriter * nw, guint16 value, guint nbits)
442 {
443   g_return_val_if_fail (nw != NULL, FALSE);
444 
445   if (!gst_bit_writer_put_bits_uint16 (&nw->bw, value, nbits))
446     return FALSE;
447 
448   return TRUE;
449 }
450 
451 gboolean
nal_writer_put_bits_uint32(NalWriter * nw,guint32 value,guint nbits)452 nal_writer_put_bits_uint32 (NalWriter * nw, guint32 value, guint nbits)
453 {
454   g_return_val_if_fail (nw != NULL, FALSE);
455 
456   if (!gst_bit_writer_put_bits_uint32 (&nw->bw, value, nbits))
457     return FALSE;
458 
459   return TRUE;
460 }
461 
462 gboolean
nal_writer_put_bytes(NalWriter * nw,const guint8 * data,guint nbytes)463 nal_writer_put_bytes (NalWriter * nw, const guint8 * data, guint nbytes)
464 {
465   g_return_val_if_fail (nw != NULL, FALSE);
466   g_return_val_if_fail (data != NULL, FALSE);
467   g_return_val_if_fail (nbytes != 0, FALSE);
468 
469   if (!gst_bit_writer_put_bytes (&nw->bw, data, nbytes))
470     return FALSE;
471 
472   return TRUE;
473 }
474 
475 gboolean
nal_writer_put_ue(NalWriter * nw,guint32 value)476 nal_writer_put_ue (NalWriter * nw, guint32 value)
477 {
478   guint leading_zeros;
479   guint rest;
480 
481   g_return_val_if_fail (nw != NULL, FALSE);
482 
483   count_exp_golomb_bits (value, &leading_zeros, &rest);
484 
485   /* write leading zeros */
486   if (leading_zeros) {
487     if (!nal_writer_put_bits_uint32 (nw, 0, leading_zeros))
488       return FALSE;
489   }
490 
491   /* write the rest */
492   if (!nal_writer_put_bits_uint32 (nw, value + 1, rest))
493     return FALSE;
494 
495   return TRUE;
496 }
497 
498 gboolean
count_exp_golomb_bits(guint32 value,guint * leading_zeros,guint * rest)499 count_exp_golomb_bits (guint32 value, guint * leading_zeros, guint * rest)
500 {
501   guint32 x;
502   guint count = 0;
503 
504   /* https://en.wikipedia.org/wiki/Exponential-Golomb_coding */
505   /* count bits of value + 1 */
506   x = value + 1;
507   while (x) {
508     count++;
509     x >>= 1;
510   }
511 
512   if (leading_zeros) {
513     if (count > 1)
514       *leading_zeros = count - 1;
515     else
516       *leading_zeros = 0;
517   }
518 
519   if (rest) {
520     *rest = count;
521   }
522 
523   return TRUE;
524 }
525