]> git.sesse.net Git - vlc/blob - modules/demux/xiph.h
decoder: do not wait for buffering when there is no data
[vlc] / modules / demux / xiph.h
1 /*****************************************************************************
2  * xiph.h: Xiph helpers
3  *****************************************************************************
4  * Copyright (C) 2010 Laurent Aimar
5  * $Id$
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 #define XIPH_MAX_HEADER_COUNT (256)
26
27 /* Temp ffmpeg vorbis format */
28 static inline bool xiph_IsOldFormat( const void *extra, unsigned int i_extra )
29 {
30     if ( i_extra >= 6 && GetWBE( extra ) == 30 )
31         return true;
32     else
33         return false;
34 }
35
36 static inline unsigned int xiph_CountHeaders( const void *extra, unsigned int i_extra )
37 {
38     const uint8_t *p_extra = (uint8_t*) extra;
39     if ( !i_extra ) return 0;
40     if ( xiph_IsOldFormat( extra, i_extra ) )
41     {
42         /* Check headers count */
43         unsigned int overall_len = 6;
44         for ( int i=0; i<3; i++ )
45         {
46             uint16_t i_size = GetWBE( extra );
47             p_extra += 2 + i_size;
48             if ( overall_len > i_extra - i_size )
49                 return 0;
50             overall_len += i_size;
51         }
52         return 3;
53     }
54     else
55     {
56         return *p_extra + 1;
57     }
58 }
59
60 static inline int xiph_SplitHeaders(unsigned packet_size[], void * packet[], unsigned *packet_count,
61                                     unsigned extra_size, const void *extra)
62 {
63     const uint8_t *current = (const uint8_t *)extra;
64     const uint8_t *end = &current[extra_size];
65     if (extra_size < 1)
66         return VLC_EGENERIC;
67
68     /* Parse the packet count and their sizes */
69     const unsigned count = xiph_CountHeaders( current++, extra_size );
70     if (packet_count)
71         *packet_count = count;
72
73     if ( xiph_IsOldFormat( extra, extra_size ) )
74     {
75         uint8_t *p_extra = (uint8_t*) extra;
76         unsigned int overall_len = count << 1;
77         for ( unsigned int i=0; i < count; i++ ) {
78             packet_size[i] = GetWBE( p_extra );
79             packet[i] = p_extra + 2;
80             p_extra += packet_size[i] + 2;
81             if (overall_len > extra_size - packet_size[i])
82                 return VLC_EGENERIC;
83             overall_len += packet_size[i];
84         }
85     }
86     else
87     {
88         int size = 0;
89         for (unsigned i = 0; i < count - 1; i++) {
90             packet_size[i] = 0;
91             for (;;) {
92                 if (current >= end)
93                     return VLC_EGENERIC;
94                 packet_size[i] += *current;
95                 if (*current++ != 255)
96                     break;
97             }
98             size += packet_size[i];
99         }
100         if (end - current < size)
101             return VLC_EGENERIC;
102         packet_size[count - 1] = end - current - size;
103
104         for (unsigned i = 0; i < count; i++)
105             if (packet_size[i] > 0) {
106                 packet[i] = (void *) current;
107                 current += packet_size[i];
108             }
109     }
110     return VLC_SUCCESS;
111 }
112
113 static inline int xiph_PackHeaders(int *extra_size, void **extra,
114                                    unsigned packet_size[], const void *packet[], unsigned packet_count )
115 {
116     if (packet_count <= 0 || packet_count > XIPH_MAX_HEADER_COUNT)
117         return VLC_EGENERIC;
118
119     /* Compute the size needed for the whole extra data */
120     unsigned payload_size = 0;
121     unsigned header_size = 1;
122     for (unsigned i = 0; i < packet_count; i++) {
123         payload_size += packet_size[i];
124         if (i < packet_count - 1)
125             header_size += 1 + packet_size[i] / 255;
126     }
127
128     /* */
129     *extra_size = header_size + payload_size;
130     *extra = malloc(*extra_size);
131     if (*extra == NULL)
132         return VLC_ENOMEM;
133
134     /* Write the header */
135     uint8_t *current = (uint8_t*)*extra;
136     *current++ = packet_count - 1;
137     for (unsigned i = 0; i < packet_count - 1; i++) {
138         unsigned t = packet_size[i];
139         for (;;) {
140             if (t >= 255) {
141                 *current++ = 255;
142                 t -= 255;
143             } else {
144                 *current++ = t;
145                 break;
146             }
147         }
148     }
149
150     /* Copy the payloads */
151     for (unsigned i = 0; i < packet_count; i++) {
152         if (packet_size[i] > 0) {
153             memcpy(current, packet[i], packet_size[i]);
154             current += packet_size[i];
155         }
156     }
157     assert(current == (uint8_t*)*extra + *extra_size);
158     return VLC_SUCCESS;
159 }
160
161 static inline int xiph_AppendHeaders(int *extra_size, void **extra,
162                                      unsigned size, const void *data)
163 {
164     unsigned packet_size[XIPH_MAX_HEADER_COUNT];
165     void *packet[XIPH_MAX_HEADER_COUNT];
166     unsigned count;
167     if (*extra_size > 0 && *extra) {
168         if (xiph_SplitHeaders(packet_size, packet, &count, *extra_size, *extra))
169             return VLC_EGENERIC;
170     } else {
171         count = 0;
172     }
173     if (count >= XIPH_MAX_HEADER_COUNT)
174         return VLC_EGENERIC;
175
176     void *old = *extra;
177
178     packet_size[count] = size;
179     packet[count]      = (void*)data;
180     if (xiph_PackHeaders(extra_size, extra, packet_size,
181                          (const void **)packet, count + 1)) {
182         *extra_size = 0;
183         *extra      = NULL;
184     }
185
186     free(old);
187
188     if (*extra_size <= 0)
189         return VLC_EGENERIC;
190     return VLC_SUCCESS;
191 }
192