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 <gst/base/gstbytereader.h>
39 #include <gst/base/gstbitreader.h>
40 #include <string.h>
41 
42 guint ceil_log2 (guint32 v);
43 
44 typedef struct
45 {
46   const guint8 *data;
47   guint size;
48 
49   guint n_epb;                  /* Number of emulation prevention bytes */
50   guint byte;                   /* Byte position */
51   guint bits_in_cache;          /* bitpos in the cache of next bit */
52   guint8 first_byte;
53   guint64 cache;                /* cached bytes */
54 } NalReader;
55 
56 G_GNUC_INTERNAL
57 void nal_reader_init (NalReader * nr, const guint8 * data, guint size);
58 
59 G_GNUC_INTERNAL
60 gboolean nal_reader_read (NalReader * nr, guint nbits);
61 
62 G_GNUC_INTERNAL
63 gboolean nal_reader_skip (NalReader * nr, guint nbits);
64 
65 G_GNUC_INTERNAL
66 gboolean nal_reader_skip_long (NalReader * nr, guint nbits);
67 
68 G_GNUC_INTERNAL
69 guint nal_reader_get_pos (const NalReader * nr);
70 
71 G_GNUC_INTERNAL
72 guint nal_reader_get_remaining (const NalReader * nr);
73 
74 G_GNUC_INTERNAL
75 guint nal_reader_get_epb_count (const NalReader * nr);
76 
77 G_GNUC_INTERNAL
78 gboolean nal_reader_is_byte_aligned (NalReader * nr);
79 
80 G_GNUC_INTERNAL
81 gboolean nal_reader_has_more_data (NalReader * nr);
82 
83 #define NAL_READER_READ_BITS_H(bits) \
84 G_GNUC_INTERNAL \
85 gboolean nal_reader_get_bits_uint##bits (NalReader *nr, guint##bits *val, guint nbits)
86 
87 NAL_READER_READ_BITS_H (8);
88 NAL_READER_READ_BITS_H (16);
89 NAL_READER_READ_BITS_H (32);
90 
91 #define NAL_READER_PEEK_BITS_H(bits) \
92 G_GNUC_INTERNAL \
93 gboolean nal_reader_peek_bits_uint##bits (const NalReader *nr, guint##bits *val, guint nbits)
94 
95 NAL_READER_PEEK_BITS_H (8);
96 
97 G_GNUC_INTERNAL
98 gboolean nal_reader_get_ue (NalReader * nr, guint32 * val);
99 
100 G_GNUC_INTERNAL
101 gboolean nal_reader_get_se (NalReader * nr, gint32 * val);
102 
103 #define CHECK_ALLOWED_MAX(val, max) { \
104   if (val > max) { \
105     GST_WARNING ("value greater than max. value: %d, max %d", \
106                      val, max); \
107     goto error; \
108   } \
109 }
110 
111 #define CHECK_ALLOWED(val, min, max) { \
112   if (val < min || val > max) { \
113     GST_WARNING ("value not in allowed range. value: %d, range %d-%d", \
114                      val, min, max); \
115     goto error; \
116   } \
117 }
118 
119 #define READ_UINT8(nr, val, nbits) { \
120   if (!nal_reader_get_bits_uint8 (nr, &val, nbits)) { \
121     GST_WARNING ("failed to read uint8, nbits: %d", nbits); \
122     goto error; \
123   } \
124 }
125 
126 #define READ_UINT16(nr, val, nbits) { \
127   if (!nal_reader_get_bits_uint16 (nr, &val, nbits)) { \
128   GST_WARNING ("failed to read uint16, nbits: %d", nbits); \
129     goto error; \
130   } \
131 }
132 
133 #define READ_UINT32(nr, val, nbits) { \
134   if (!nal_reader_get_bits_uint32 (nr, &val, nbits)) { \
135   GST_WARNING ("failed to read uint32, nbits: %d", nbits); \
136     goto error; \
137   } \
138 }
139 
140 #define READ_UINT64(nr, val, nbits) { \
141   if (!nal_reader_get_bits_uint64 (nr, &val, nbits)) { \
142     GST_WARNING ("failed to read uint32, nbits: %d", nbits); \
143     goto error; \
144   } \
145 }
146 
147 #define READ_UE(nr, val) { \
148   if (!nal_reader_get_ue (nr, &val)) { \
149     GST_WARNING ("failed to read UE"); \
150     goto error; \
151   } \
152 }
153 
154 #define READ_UE_ALLOWED(nr, val, min, max) { \
155   guint32 tmp; \
156   READ_UE (nr, tmp); \
157   CHECK_ALLOWED (tmp, min, max); \
158   val = tmp; \
159 }
160 
161 #define READ_UE_MAX(nr, val, max) { \
162   guint32 tmp; \
163   READ_UE (nr, tmp); \
164   CHECK_ALLOWED_MAX (tmp, max); \
165   val = tmp; \
166 }
167 
168 #define READ_SE(nr, val) { \
169   if (!nal_reader_get_se (nr, &val)) { \
170     GST_WARNING ("failed to read SE"); \
171     goto error; \
172   } \
173 }
174 
175 #define READ_SE_ALLOWED(nr, val, min, max) { \
176   gint32 tmp; \
177   READ_SE (nr, tmp); \
178   CHECK_ALLOWED (tmp, min, max); \
179   val = tmp; \
180 }
181 
182 G_GNUC_INTERNAL
183 gint scan_for_start_codes (const guint8 * data, guint size);
184