Qt: do not show open options in both normal and advanced UI
[vlc.git] / modules / demux / xiph.h
blob0ce5dc4b2be2f4c44d4968b368151c6f6854f552
1 /*****************************************************************************
2 * xiph.h: Xiph helpers
3 *****************************************************************************
4 * Copyright (C) 2010 Laurent Aimar
5 * $Id$
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
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.
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.
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 *****************************************************************************/
24 #include <assert.h>
25 #define XIPH_MAX_HEADER_COUNT (256)
27 static inline int xiph_SplitHeaders(unsigned packet_size[], void *packet[], unsigned *packet_count,
28 unsigned extra_size, const void *extra)
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;
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;
49 size += packet_size[i];
51 if (end - current < (int)size)
52 return VLC_EGENERIC;
53 packet_size[count - 1] = end - current - size;
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;
63 if (packet_size[i] > 0) {
64 memcpy(packet[i], current, packet_size[i]);
65 current += packet_size[i];
68 return VLC_SUCCESS;
71 static inline int xiph_PackHeaders(int *extra_size, void **extra,
72 unsigned packet_size[], const void *packet[], unsigned packet_count )
74 if (packet_count <= 0 || packet_count > XIPH_MAX_HEADER_COUNT)
75 return VLC_EGENERIC;
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;
86 /* */
87 *extra_size = header_size + payload_size;
88 *extra = malloc(*extra_size);
89 if (*extra == NULL)
90 return VLC_ENOMEM;
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;
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];
115 assert(current == (uint8_t*)*extra + *extra_size);
116 return VLC_SUCCESS;
119 static inline int xiph_AppendHeaders(int *extra_size, void **extra,
120 unsigned size, const void *data)
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;
131 if (count >= XIPH_MAX_HEADER_COUNT)
132 return VLC_EGENERIC;
134 free(*extra);
136 packet_size[count] = size;
137 packet[count] = (void*)data;
138 if (xiph_PackHeaders(extra_size, extra, packet_size,
139 (const void **)packet, count + 1)) {
140 *extra_size = 0;
141 *extra = NULL;
143 for (unsigned i = 0; i < count; i++)
144 free(packet[i]);
146 if (*extra_size <= 0)
147 return VLC_EGENERIC;
148 return VLC_SUCCESS;