1 /*****************************************************************************
2 * xiph.h: Xiph helpers
3 *****************************************************************************
4 * Copyright (C) 2010 Laurent Aimar
5 * $Id: 06600484d515cb0e6ed9581c8ccce0f87d44e5ac $
6 *
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
23
24 #include <assert.h>
25 #include <limits.h>
26 #define XIPH_MAX_HEADER_COUNT (256)
27
28 /* Temp ffmpeg vorbis format */
xiph_IsLavcFormat(const void * extra,unsigned i_extra,vlc_fourcc_t i_codec)29 static inline bool xiph_IsLavcFormat(const void *extra, unsigned i_extra,
30 vlc_fourcc_t i_codec)
31 {
32 switch(i_codec)
33 {
34 case VLC_CODEC_VORBIS:
35 return i_extra >= 6 && GetWBE(extra) == 30;
36 case VLC_CODEC_THEORA:
37 return i_extra >= 6 && GetWBE(extra) == 42;
38 default:
39 return false;
40 }
41 }
42
xiph_CountLavcHeaders(const void * p_extra,unsigned i_extra)43 static inline unsigned xiph_CountLavcHeaders(const void *p_extra, unsigned i_extra)
44 {
45 const uint8_t *p = (const uint8_t*) p_extra;
46 const uint8_t *p_end = &p[i_extra];
47 /* Check headers count */
48 for (int i=0; i<3; i++)
49 {
50 if(p_end - p < 2)
51 return 0;
52 uint16_t i_size = GetWBE(p);
53 if(&p[2U + i_size] > p_end)
54 return 0;
55 p += 2 + i_size;
56 }
57 return 3;
58 }
59
xiph_CountHeaders(const void * p_extra,unsigned i_extra)60 static inline unsigned xiph_CountHeaders(const void *p_extra, unsigned i_extra)
61 {
62 const uint8_t *p = (const uint8_t*) p_extra;
63 if (!i_extra)
64 return 0;
65 /* First byte is headers count */
66 if(1U + *p > i_extra)
67 return 0;
68 return *p + 1;
69 }
70
xiph_CountUnknownHeaders(const void * p_extra,unsigned i_extra,vlc_fourcc_t i_codec)71 static inline unsigned xiph_CountUnknownHeaders(const void *p_extra, unsigned i_extra,
72 vlc_fourcc_t i_codec)
73 {
74 if (xiph_IsLavcFormat(p_extra, i_extra, i_codec))
75 return xiph_CountLavcHeaders(p_extra, i_extra);
76 else
77 return xiph_CountHeaders(p_extra, i_extra);
78 }
79
xiph_SplitLavcHeaders(unsigned packet_size[],const void * packet[],unsigned * packet_count,unsigned i_extra,const void * p_extra)80 static inline int xiph_SplitLavcHeaders(unsigned packet_size[],
81 const void *packet[], unsigned *packet_count,
82 unsigned i_extra, const void *p_extra)
83 {
84 const uint8_t *current = (const uint8_t *)p_extra;
85 const uint8_t *end = ¤t[i_extra];
86 if (i_extra < 2)
87 return VLC_EGENERIC;
88 /* Parse the packet count and their sizes */
89 const unsigned count = xiph_CountLavcHeaders(current, i_extra);
90 if(count == 0)
91 return VLC_EGENERIC;
92 if (packet_count)
93 *packet_count = count;
94 /* count is trusted here (xiph_CountHeaders) */
95 for (unsigned i=0; i < count; i++)
96 {
97 /* each payload is prefixed by word size */
98 packet_size[i] = GetWBE(current);
99 if(¤t[2U + packet_size[i]] > end)
100 return VLC_EGENERIC;
101 packet[i] = current + 2;
102 current += packet_size[i] + 2;
103 }
104 return VLC_SUCCESS;
105 }
106
xiph_SplitHeaders(unsigned packet_size[],const void * packet[],unsigned * packet_count,unsigned i_extra,const void * p_extra)107 static inline int xiph_SplitHeaders(unsigned packet_size[],
108 const void *packet[], unsigned *packet_count,
109 unsigned i_extra, const void *p_extra)
110 {
111 const uint8_t *current = (const uint8_t *)p_extra;
112 const uint8_t *end = ¤t[i_extra];
113 if (i_extra < 1)
114 return VLC_EGENERIC;
115
116 /* Parse the packet count and their sizes */
117 const unsigned count = xiph_CountHeaders(current, i_extra);
118 if(count == 0)
119 return VLC_EGENERIC;
120
121 if (packet_count)
122 *packet_count = count;
123
124 /* - 1 byte (N-1) packets
125 * - N-1 variable length payload sizes
126 * - N-1 payloads
127 * - Nth packet (remaining) */
128
129 /* skip count byte header */
130 ++current;
131 /* read sizes for N-1 packets */
132 unsigned total_payload_minus_last = 0;
133 for (unsigned i = 0; i < count - 1; i++)
134 {
135 packet_size[i] = 0;
136 for (;;) {
137 if (current >= end)
138 return VLC_EGENERIC;
139 packet_size[i] += *current;
140 if (*current++ != 255)
141 break;
142 }
143 if(UINT_MAX - total_payload_minus_last < packet_size[i])
144 return VLC_EGENERIC;
145 total_payload_minus_last += packet_size[i];
146 }
147 if(current + total_payload_minus_last > end)
148 return VLC_EGENERIC;
149 /* set pointers for N-1 packets */
150 for (unsigned i = 0; i < count - 1; i++)
151 {
152 packet[i] = current;
153 current += packet_size[i];
154 }
155 /* Last packet is remaining size */
156 packet_size[count - 1] = end - current;
157 packet[count - 1] = current;
158
159 return VLC_SUCCESS;
160 }
161
xiph_PackHeaders(int * extra_size,void ** extra,unsigned packet_size[],const void * const packet[],unsigned packet_count)162 static inline int xiph_PackHeaders(int *extra_size, void **extra,
163 unsigned packet_size[],
164 const void *const packet[],
165 unsigned packet_count)
166 {
167 if (packet_count <= 0 || packet_count > XIPH_MAX_HEADER_COUNT)
168 return VLC_EGENERIC;
169
170 /* Compute the size needed for the whole extra data */
171 unsigned payload_size = 0;
172 unsigned header_size = 1;
173 for (unsigned i = 0; i < packet_count; i++) {
174 payload_size += packet_size[i];
175 if (i < packet_count - 1)
176 header_size += 1 + packet_size[i] / 255;
177 }
178
179 /* */
180 *extra_size = header_size + payload_size;
181 *extra = malloc(*extra_size);
182 if (*extra == NULL)
183 return VLC_ENOMEM;
184
185 /* Write the header */
186 uint8_t *current = (uint8_t*)*extra;
187 *current++ = packet_count - 1;
188 for (unsigned i = 0; i < packet_count - 1; i++) {
189 unsigned t = packet_size[i];
190 for (;;) {
191 if (t >= 255) {
192 *current++ = 255;
193 t -= 255;
194 } else {
195 *current++ = t;
196 break;
197 }
198 }
199 }
200
201 /* Copy the payloads */
202 for (unsigned i = 0; i < packet_count; i++) {
203 if (packet_size[i] > 0) {
204 memcpy(current, packet[i], packet_size[i]);
205 current += packet_size[i];
206 }
207 }
208 assert(current == (uint8_t*)*extra + *extra_size);
209 return VLC_SUCCESS;
210 }
211
xiph_AppendHeaders(int * extra_size,void ** extra,unsigned size,const void * data)212 static inline int xiph_AppendHeaders(int *extra_size, void **extra,
213 unsigned size, const void *data)
214 {
215 unsigned packet_size[XIPH_MAX_HEADER_COUNT];
216 const void *packet[XIPH_MAX_HEADER_COUNT];
217 unsigned count;
218
219 if (*extra_size > 0 && *extra) {
220 if (xiph_SplitHeaders(packet_size, packet, &count, *extra_size, *extra))
221 return VLC_EGENERIC;
222 } else {
223 count = 0;
224 }
225 if (count >= XIPH_MAX_HEADER_COUNT)
226 return VLC_EGENERIC;
227
228 void *old = *extra;
229
230 packet_size[count] = size;
231 packet[count] = (void*)data;
232 if (xiph_PackHeaders(extra_size, extra, packet_size,
233 packet, count + 1)) {
234 *extra_size = 0;
235 *extra = NULL;
236 }
237
238 free(old);
239
240 if (*extra_size <= 0)
241 return VLC_EGENERIC;
242 return VLC_SUCCESS;
243 }
244