]> git.sesse.net Git - vlc/blob - modules/demux/xiph.h
mediacodec: Simplify the GetOutput function
[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 static inline unsigned int xiph_CountHeaders( const void *extra )
28 {
29     return *( (const uint8_t*) extra ) + 1;
30 }
31
32 static inline int xiph_SplitHeaders(unsigned packet_size[], void *packet[], unsigned *packet_count,
33                                     unsigned extra_size, void *extra)
34 {
35     uint8_t *current = extra;
36     const uint8_t *end = &current[extra_size];
37     if (extra_size < 1)
38         return VLC_EGENERIC;
39
40     /* Parse the packet count and their sizes */
41     const unsigned count = xiph_CountHeaders( current++ );
42     if (packet_count)
43         *packet_count = count;
44     unsigned size = 0;
45     for (unsigned i = 0; i < count - 1; i++) {
46         packet_size[i] = 0;
47         for (;;) {
48             if (current >= end)
49                 return VLC_EGENERIC;
50             packet_size[i] += *current;
51             if (*current++ != 255)
52                 break;
53         }
54         size += packet_size[i];
55     }
56     if (end - current < size)
57         return VLC_EGENERIC;
58     packet_size[count - 1] = end - current - size;
59
60     for (unsigned i = 0; i < count; i++)
61         if (packet_size[i] > 0) {
62             packet[i] = current;
63             current += packet_size[i];
64         }
65
66     return VLC_SUCCESS;
67 }
68
69 static inline int xiph_PackHeaders(int *extra_size, void **extra,
70                                    unsigned packet_size[], const void *packet[], unsigned packet_count )
71 {
72     if (packet_count <= 0 || packet_count > XIPH_MAX_HEADER_COUNT)
73         return VLC_EGENERIC;
74
75     /* Compute the size needed for the whole extra data */
76     unsigned payload_size = 0;
77     unsigned header_size = 1;
78     for (unsigned i = 0; i < packet_count; i++) {
79         payload_size += packet_size[i];
80         if (i < packet_count - 1)
81             header_size += 1 + packet_size[i] / 255;
82     }
83
84     /* */
85     *extra_size = header_size + payload_size;
86     *extra = malloc(*extra_size);
87     if (*extra == NULL)
88         return VLC_ENOMEM;
89
90     /* Write the header */
91     uint8_t *current = (uint8_t*)*extra;
92     *current++ = packet_count - 1;
93     for (unsigned i = 0; i < packet_count - 1; i++) {
94         unsigned t = packet_size[i];
95         for (;;) {
96             if (t >= 255) {
97                 *current++ = 255;
98                 t -= 255;
99             } else {
100                 *current++ = t;
101                 break;
102             }
103         }
104     }
105
106     /* Copy the payloads */
107     for (unsigned i = 0; i < packet_count; i++) {
108         if (packet_size[i] > 0) {
109             memcpy(current, packet[i], packet_size[i]);
110             current += packet_size[i];
111         }
112     }
113     assert(current == (uint8_t*)*extra + *extra_size);
114     return VLC_SUCCESS;
115 }
116
117 static inline int xiph_AppendHeaders(int *extra_size, void **extra,
118                                      unsigned size, const void *data)
119 {
120     unsigned packet_size[XIPH_MAX_HEADER_COUNT];
121     void *packet[XIPH_MAX_HEADER_COUNT];
122     unsigned count;
123     if (*extra_size > 0 && *extra) {
124         if (xiph_SplitHeaders(packet_size, packet, &count, *extra_size, *extra))
125             return VLC_EGENERIC;
126     } else {
127         count = 0;
128     }
129     if (count >= XIPH_MAX_HEADER_COUNT)
130         return VLC_EGENERIC;
131
132     void *old = *extra;
133
134     packet_size[count] = size;
135     packet[count]      = (void*)data;
136     if (xiph_PackHeaders(extra_size, extra, packet_size,
137                          (const void **)packet, count + 1)) {
138         *extra_size = 0;
139         *extra      = NULL;
140     }
141
142     free(old);
143
144     if (*extra_size <= 0)
145         return VLC_EGENERIC;
146     return VLC_SUCCESS;
147 }
148