]> git.sesse.net Git - vlc/blob - modules/demux/xiph.h
Use var_InheritString for --decklink-video-connection.
[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
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, 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 int xiph_SplitHeaders(unsigned packet_size[], void *packet[], unsigned *packet_count,
28                                     unsigned extra_size, const void *extra)
29 {
30     const uint8_t *current = (const uint8_t*)extra;
31     const uint8_t *end = &current[extra_size];
32     if (extra_size < 1)
33         return VLC_EGENERIC;
34
35     /* Parse the packet count and their sizes */
36     const unsigned count = 1 + *current++;
37     if (packet_count)
38         *packet_count = count;
39     unsigned size = 0;
40     for (unsigned i = 0; i < count - 1; i++) {
41         packet_size[i] = 0;
42         for (;;) {
43             if (current >= end)
44                 return VLC_EGENERIC;
45             packet_size[i] += *current;
46             if (*current++ != 255)
47                 break;
48         }
49         size += packet_size[i];
50     }
51     if (end - current < size)
52         return VLC_EGENERIC;
53     packet_size[count - 1] = end - current - size;
54
55     /* Copy the payloads */
56     for (unsigned i = 0; i < count; i++) {
57         packet[i] = malloc(packet_size[i]);
58         if (!packet[i]) {
59             for (unsigned j = 0; j < i; j++)
60                 free(packet[j]);
61             return VLC_ENOMEM;
62         }
63         if (packet_size[i] > 0) {
64             memcpy(packet[i], current, packet_size[i]);
65             current += packet_size[i];
66         }
67     }
68     return VLC_SUCCESS;
69 }
70
71 static inline int xiph_PackHeaders(int *extra_size, void **extra,
72                                    unsigned packet_size[], void *packet[], unsigned packet_count )
73 {
74     if (packet_count <= 0 || packet_count > XIPH_MAX_HEADER_COUNT)
75         return VLC_EGENERIC;
76
77     /* Compute the size needed for the whole extra data */
78     unsigned payload_size = 0;
79     unsigned header_size = 1;
80     for (unsigned i = 0; i < packet_count; i++) {
81         payload_size += packet_size[i];
82         if (i < packet_count - 1)
83             header_size += 1 + packet_size[i] / 255;
84     }
85
86     /* */
87     *extra_size = header_size + payload_size;
88     *extra = malloc(*extra_size);
89     if (*extra == NULL)
90         return VLC_ENOMEM;
91
92     /* Write the header */
93     uint8_t *current = (uint8_t*)*extra;
94     *current++ = packet_count - 1;
95     for (unsigned i = 0; i < packet_count - 1; i++) {
96         unsigned t = packet_size[i];
97         for (;;) {
98             if (t >= 255) {
99                 *current++ = 255;
100                 t -= 255;
101             } else {
102                 *current++ = t;
103                 break;
104             }
105         }
106     }
107
108     /* Copy the payloads */
109     for (unsigned i = 0; i < packet_count; i++) {
110         if (packet_size[i] > 0) {
111             memcpy(current, packet[i], packet_size[i]);
112             current += packet_size[i];
113         }
114     }
115     assert(current == (uint8_t*)*extra + *extra_size);
116     return VLC_SUCCESS;
117 }
118
119 static inline int xiph_AppendHeaders(int *extra_size, void **extra,
120                                      unsigned size, const void *data)
121 {
122     unsigned packet_size[XIPH_MAX_HEADER_COUNT];
123     void *packet[XIPH_MAX_HEADER_COUNT];
124     unsigned count;
125     if (*extra_size > 0 && *extra) {
126         if (xiph_SplitHeaders(packet_size, packet, &count, *extra_size, *extra))
127             return VLC_EGENERIC;
128     } else {
129         count = 0;
130     }
131     if (count >= XIPH_MAX_HEADER_COUNT)
132         return VLC_EGENERIC;
133
134     free(*extra);
135
136     packet_size[count] = size;
137     packet[count]      = (void*)data;
138     if (xiph_PackHeaders(extra_size, extra, packet_size, packet, count + 1)) {
139         *extra_size = 0;
140         *extra      = NULL;
141     }
142     for (unsigned i = 0; i < count; i++)
143         free(packet[i]);
144
145     if (*extra_size <= 0)
146         return VLC_EGENERIC;
147     return VLC_SUCCESS;
148 }
149