Mercurial > mplayer.hg
annotate libmpdemux/ebml.c @ 32096:06519a2a9c77
cosmetics: Reformat in K&R style, prettyprinting.
author | diego |
---|---|
date | Sun, 12 Sep 2010 11:22:42 +0000 |
parents | 9175a9a22051 |
children | e626f90df47e |
rev | line source |
---|---|
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1 /* |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2 * native ebml reader for the Matroska demuxer |
29238
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
3 * copyright (c) 2004 Aurelien Jacobs <aurel@gnuage.org> |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
4 * based on the one written by Ronald Bultje for gstreamer |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
5 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
6 * This file is part of MPlayer. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
7 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
8 * MPlayer is free software; you can redistribute it and/or modify |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
9 * it under the terms of the GNU General Public License as published by |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
10 * the Free Software Foundation; either version 2 of the License, or |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
11 * (at your option) any later version. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
12 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
13 * MPlayer is distributed in the hope that it will be useful, |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
16 * GNU General Public License for more details. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
17 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
18 * You should have received a copy of the GNU General Public License along |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
19 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
22605
diff
changeset
|
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
21 */ |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
22 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
23 #include "config.h" |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
24 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
25 #include <stdlib.h> |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
26 |
22605
4d81dbdf46b9
Add explicit location for headers from the stream/ directory.
diego
parents:
21507
diff
changeset
|
27 #include "stream/stream.h" |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
28 #include "ebml.h" |
21372 | 29 #include "libavutil/common.h" |
21507
fa99b3d31d13
Hack around libavutil/bswap.h compilation problems due to always_inline undefined.
reimar
parents:
21388
diff
changeset
|
30 #include "mpbswap.h" |
21388
be5f168e42ae
Use av_int2flt/av_int2dbl to read float values. This is simpler and more
reimar
parents:
21372
diff
changeset
|
31 #include "libavutil/intfloat_readwrite.h" |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
32 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
33 |
18671 | 34 #ifndef SIZE_MAX |
35 #define SIZE_MAX ((size_t)-1) | |
36 #endif | |
37 | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
38 /* |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
39 * Read: the element content data ID. |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
40 * Return: the ID. |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
41 */ |
31177 | 42 uint32_t ebml_read_id(stream_t *s, int *length) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
43 { |
31177 | 44 int i, len_mask = 0x80; |
45 uint32_t id; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
46 |
31177 | 47 for (i = 0, id = stream_read_char(s); i < 4 && !(id & len_mask); i++) |
48 len_mask >>= 1; | |
49 if (i >= 4) | |
50 return EBML_ID_INVALID; | |
51 if (length) | |
52 *length = i + 1; | |
53 while (i--) | |
54 id = (id << 8) | stream_read_char(s); | |
55 return id; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
56 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
57 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
58 /* |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
59 * Read a variable length unsigned int. |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
60 */ |
31177 | 61 uint64_t ebml_read_vlen_uint(uint8_t *buffer, int *length) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
62 { |
31177 | 63 int i, j, num_ffs = 0, len_mask = 0x80; |
64 uint64_t num; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
65 |
31177 | 66 for (i = 0, num = *buffer++; i < 8 && !(num & len_mask); i++) |
67 len_mask >>= 1; | |
68 if (i >= 8) | |
69 return EBML_UINT_INVALID; | |
70 j = i + 1; | |
71 if (length) | |
72 *length = j; | |
73 if ((int) (num &= (len_mask - 1)) == len_mask - 1) | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
74 num_ffs++; |
31177 | 75 while (i--) { |
76 num = (num << 8) | *buffer++; | |
77 if ((num & 0xFF) == 0xFF) | |
78 num_ffs++; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
79 } |
31177 | 80 if (j == num_ffs) |
81 return EBML_UINT_INVALID; | |
82 return num; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
83 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
84 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
85 /* |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
86 * Read a variable length signed int. |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
87 */ |
31177 | 88 int64_t ebml_read_vlen_int(uint8_t *buffer, int *length) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
89 { |
31177 | 90 uint64_t unum; |
91 int l; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
92 |
31177 | 93 /* read as unsigned number first */ |
94 unum = ebml_read_vlen_uint(buffer, &l); | |
95 if (unum == EBML_UINT_INVALID) | |
96 return EBML_INT_INVALID; | |
97 if (length) | |
98 *length = l; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
99 |
31177 | 100 return unum - ((1 << ((7 * l) - 1)) - 1); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
101 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
102 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
103 /* |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
104 * Read: element content length. |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
105 */ |
31177 | 106 uint64_t ebml_read_length(stream_t *s, int *length) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
107 { |
31177 | 108 int i, j, num_ffs = 0, len_mask = 0x80; |
109 uint64_t len; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
110 |
31177 | 111 for (i = 0, len = stream_read_char(s); i < 8 && !(len & len_mask); i++) |
112 len_mask >>= 1; | |
113 if (i >= 8) | |
114 return EBML_UINT_INVALID; | |
115 j = i + 1; | |
116 if (length) | |
117 *length = j; | |
118 if ((int) (len &= (len_mask - 1)) == len_mask - 1) | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
119 num_ffs++; |
31177 | 120 while (i--) { |
121 len = (len << 8) | stream_read_char(s); | |
122 if ((len & 0xFF) == 0xFF) | |
123 num_ffs++; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
124 } |
31177 | 125 if (j == num_ffs) |
126 return EBML_UINT_INVALID; | |
127 return len; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
128 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
129 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
130 /* |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
131 * Read the next element as an unsigned int. |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
132 */ |
31177 | 133 uint64_t ebml_read_uint(stream_t *s, uint64_t *length) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
134 { |
31177 | 135 uint64_t len, value = 0; |
136 int l; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
137 |
31177 | 138 len = ebml_read_length(s, &l); |
139 if (len == EBML_UINT_INVALID || len < 1 || len > 8) | |
140 return EBML_UINT_INVALID; | |
141 if (length) | |
142 *length = len + l; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
143 |
31177 | 144 while (len--) |
145 value = (value << 8) | stream_read_char(s); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
146 |
31177 | 147 return value; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
148 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
149 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
150 /* |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
151 * Read the next element as a signed int. |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
152 */ |
31177 | 153 int64_t ebml_read_int(stream_t *s, uint64_t *length) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
154 { |
31177 | 155 int64_t value = 0; |
156 uint64_t len; | |
157 int l; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
158 |
31177 | 159 len = ebml_read_length(s, &l); |
160 if (len == EBML_UINT_INVALID || len < 1 || len > 8) | |
161 return EBML_INT_INVALID; | |
162 if (length) | |
163 *length = len + l; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
164 |
31177 | 165 len--; |
166 l = stream_read_char(s); | |
167 if (l & 0x80) | |
168 value = -1; | |
169 value = (value << 8) | l; | |
170 while (len--) | |
171 value = (value << 8) | stream_read_char(s); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
172 |
31177 | 173 return value; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
174 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
175 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
176 /* |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
177 * Read the next element as a float. |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
178 */ |
31177 | 179 long double ebml_read_float(stream_t *s, uint64_t *length) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
180 { |
31177 | 181 long double value; |
182 uint64_t len; | |
183 int l; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
184 |
31177 | 185 len = ebml_read_length(s, &l); |
186 switch (len) { | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
187 case 4: |
21388
be5f168e42ae
Use av_int2flt/av_int2dbl to read float values. This is simpler and more
reimar
parents:
21372
diff
changeset
|
188 value = av_int2flt(stream_read_dword(s)); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
189 break; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
190 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
191 case 8: |
21388
be5f168e42ae
Use av_int2flt/av_int2dbl to read float values. This is simpler and more
reimar
parents:
21372
diff
changeset
|
192 value = av_int2dbl(stream_read_qword(s)); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
193 break; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
194 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
195 default: |
31177 | 196 return EBML_FLOAT_INVALID; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
197 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
198 |
31177 | 199 if (length) |
200 *length = len + l; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
201 |
31177 | 202 return value; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
203 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
204 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
205 /* |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
206 * Read the next element as an ASCII string. |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
207 */ |
31177 | 208 char *ebml_read_ascii(stream_t *s, uint64_t *length) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
209 { |
31177 | 210 uint64_t len; |
211 char *str; | |
212 int l; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
213 |
31177 | 214 len = ebml_read_length(s, &l); |
215 if (len == EBML_UINT_INVALID) | |
216 return NULL; | |
217 if (len > SIZE_MAX - 1) | |
218 return NULL; | |
219 if (length) | |
220 *length = len + l; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
221 |
31177 | 222 str = malloc(len + 1); |
223 if (stream_read(s, str, len) != (int) len) { | |
224 free(str); | |
225 return NULL; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
226 } |
31177 | 227 str[len] = '\0'; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
228 |
31177 | 229 return str; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
230 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
231 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
232 /* |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
233 * Read the next element as a UTF-8 string. |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
234 */ |
31177 | 235 char *ebml_read_utf8(stream_t *s, uint64_t *length) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
236 { |
31177 | 237 return ebml_read_ascii(s, length); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
238 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
239 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
240 /* |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
241 * Skip the next element. |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
242 */ |
31177 | 243 int ebml_read_skip(stream_t *s, uint64_t *length) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
244 { |
31177 | 245 uint64_t len; |
246 int l; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
247 |
31177 | 248 len = ebml_read_length(s, &l); |
249 if (len == EBML_UINT_INVALID) | |
250 return 1; | |
251 if (length) | |
252 *length = len + l; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
253 |
31177 | 254 stream_skip(s, len); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
255 |
31177 | 256 return 0; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
257 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
258 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
259 /* |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
260 * Read the next element, but only the header. The contents |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
261 * are supposed to be sub-elements which can be read separately. |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
262 */ |
31177 | 263 uint32_t ebml_read_master(stream_t *s, uint64_t *length) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
264 { |
31177 | 265 uint64_t len; |
266 uint32_t id; | |
267 | |
268 id = ebml_read_id(s, NULL); | |
269 if (id == EBML_ID_INVALID) | |
270 return id; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
271 |
31177 | 272 len = ebml_read_length(s, NULL); |
273 if (len == EBML_UINT_INVALID) | |
274 return EBML_ID_INVALID; | |
275 if (length) | |
276 *length = len; | |
277 | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
278 return id; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
279 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
280 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
281 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
282 /* |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
283 * Read an EBML header. |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
284 */ |
31177 | 285 char *ebml_read_header(stream_t *s, int *version) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
286 { |
31177 | 287 uint64_t length, l, num; |
288 uint32_t id; | |
289 char *str = NULL; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
290 |
31177 | 291 if (ebml_read_master(s, &length) != EBML_ID_HEADER) |
292 return 0; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
293 |
31177 | 294 if (version) |
295 *version = 1; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
296 |
31177 | 297 while (length > 0) { |
298 id = ebml_read_id(s, NULL); | |
299 if (id == EBML_ID_INVALID) | |
300 return NULL; | |
301 length -= 2; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
302 |
31177 | 303 switch (id) { |
304 /* is our read version uptodate? */ | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
305 case EBML_ID_EBMLREADVERSION: |
31177 | 306 num = ebml_read_uint(s, &l); |
307 if (num != EBML_VERSION) | |
308 return NULL; | |
309 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
310 |
31177 | 311 /* we only handle 8 byte lengths at max */ |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
312 case EBML_ID_EBMLMAXSIZELENGTH: |
31177 | 313 num = ebml_read_uint(s, &l); |
314 if (num != sizeof(uint64_t)) | |
315 return NULL; | |
316 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
317 |
31177 | 318 /* we handle 4 byte IDs at max */ |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
319 case EBML_ID_EBMLMAXIDLENGTH: |
31177 | 320 num = ebml_read_uint(s, &l); |
321 if (num != sizeof(uint32_t)) | |
322 return NULL; | |
323 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
324 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
325 case EBML_ID_DOCTYPE: |
31177 | 326 str = ebml_read_ascii(s, &l); |
327 if (str == NULL) | |
328 return NULL; | |
329 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
330 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
331 case EBML_ID_DOCTYPEREADVERSION: |
31177 | 332 num = ebml_read_uint(s, &l); |
333 if (num == EBML_UINT_INVALID) | |
334 return NULL; | |
335 if (version) | |
336 *version = num; | |
337 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
338 |
31177 | 339 /* we ignore these two, they don't tell us anything we care about */ |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
340 case EBML_ID_VOID: |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
341 case EBML_ID_EBMLVERSION: |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
342 case EBML_ID_DOCTYPEVERSION: |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
343 default: |
31177 | 344 if (ebml_read_skip(s, &l)) |
345 return NULL; | |
346 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
347 } |
31177 | 348 length -= l; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
349 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
350 |
31177 | 351 return str; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
352 } |