Commit | Line | Data |
---|---|---|
b061d892 | 1 | /* |
ff33c5c5 | 2 | * Matroska file demuxer |
5968d2dd | 3 | * Copyright (c) 2003-2008 The FFmpeg Project |
b061d892 DC |
4 | * |
5 | * This file is part of FFmpeg. | |
6 | * | |
7 | * FFmpeg is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2.1 of the License, or (at your option) any later version. | |
11 | * | |
12 | * FFmpeg is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public | |
18 | * License along with FFmpeg; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
22 | /** | |
6de4aece | 23 | * @file matroskadec.c |
b061d892 DC |
24 | * Matroska file demuxer |
25 | * by Ronald Bultje <rbultje@ronald.bitfreak.net> | |
26 | * with a little help from Moritz Bunkus <moritz@bunkus.org> | |
ff33c5c5 | 27 | * totally reworked by Aurelien Jacobs <aurel@gnuage.org> |
5968d2dd | 28 | * Specs available on the Matroska project page: http://www.matroska.org/. |
b061d892 DC |
29 | */ |
30 | ||
31 | #include "avformat.h" | |
32 | /* For codec_get_id(). */ | |
33 | #include "riff.h" | |
f009e36f | 34 | #include "isom.h" |
b061d892 | 35 | #include "matroska.h" |
7bfacd4e | 36 | #include "libavcodec/mpeg4audio.h" |
245976da | 37 | #include "libavutil/intfloat_readwrite.h" |
5f8e0227 | 38 | #include "libavutil/avstring.h" |
de3230fb | 39 | #include "libavutil/lzo.h" |
fbb878ce AJ |
40 | #ifdef CONFIG_ZLIB |
41 | #include <zlib.h> | |
42 | #endif | |
54dddf09 AJ |
43 | #ifdef CONFIG_BZLIB |
44 | #include <bzlib.h> | |
45 | #endif | |
b061d892 | 46 | |
789ed100 AJ |
47 | typedef enum { |
48 | EBML_NONE, | |
49 | EBML_UINT, | |
50 | EBML_FLOAT, | |
51 | EBML_STR, | |
52 | EBML_UTF8, | |
53 | EBML_BIN, | |
54 | EBML_NEST, | |
55 | EBML_PASS, | |
56 | EBML_STOP, | |
57 | } EbmlType; | |
58 | ||
59 | typedef const struct EbmlSyntax { | |
60 | uint32_t id; | |
61 | EbmlType type; | |
62 | int list_elem_size; | |
63 | int data_offset; | |
64 | union { | |
65 | uint64_t u; | |
66 | double f; | |
67 | const char *s; | |
68 | const struct EbmlSyntax *n; | |
69 | } def; | |
70 | } EbmlSyntax; | |
71 | ||
72 | typedef struct { | |
73 | int nb_elem; | |
74 | void *elem; | |
75 | } EbmlList; | |
76 | ||
77 | typedef struct { | |
78 | int size; | |
79 | uint8_t *data; | |
80 | int64_t pos; | |
81 | } EbmlBin; | |
82 | ||
63511324 AJ |
83 | typedef struct { |
84 | uint64_t version; | |
85 | uint64_t max_size; | |
86 | uint64_t id_length; | |
87 | char *doctype; | |
88 | uint64_t doctype_version; | |
89 | } Ebml; | |
90 | ||
2cbc8811 AJ |
91 | typedef struct { |
92 | uint64_t algo; | |
93 | EbmlBin settings; | |
94 | } MatroskaTrackCompression; | |
b061d892 | 95 | |
2cbc8811 AJ |
96 | typedef struct { |
97 | uint64_t scope; | |
98 | uint64_t type; | |
99 | MatroskaTrackCompression compression; | |
100 | } MatroskaTrackEncoding; | |
b061d892 | 101 | |
2cbc8811 AJ |
102 | typedef struct { |
103 | double frame_rate; | |
104 | uint64_t display_width; | |
105 | uint64_t display_height; | |
106 | uint64_t pixel_width; | |
107 | uint64_t pixel_height; | |
108 | uint64_t fourcc; | |
109 | } MatroskaTrackVideo; | |
b061d892 | 110 | |
2cbc8811 AJ |
111 | typedef struct { |
112 | double samplerate; | |
113 | double out_samplerate; | |
114 | uint64_t bitdepth; | |
115 | uint64_t channels; | |
116 | ||
117 | /* real audio header (extracted from extradata) */ | |
118 | int coded_framesize; | |
119 | int sub_packet_h; | |
120 | int frame_size; | |
121 | int sub_packet_size; | |
122 | int sub_packet_cnt; | |
123 | int pkt_cnt; | |
124 | uint8_t *buf; | |
125 | } MatroskaTrackAudio; | |
b061d892 | 126 | |
2cbc8811 AJ |
127 | typedef struct { |
128 | uint64_t num; | |
129 | uint64_t type; | |
130 | char *codec_id; | |
131 | EbmlBin codec_priv; | |
132 | char *language; | |
7ff97085 | 133 | double time_scale; |
b061d892 | 134 | uint64_t default_duration; |
4eff9743 | 135 | uint64_t flag_default; |
2cbc8811 AJ |
136 | MatroskaTrackVideo video; |
137 | MatroskaTrackAudio audio; | |
138 | EbmlList encodings; | |
fc4d335f AJ |
139 | |
140 | AVStream *stream; | |
b061d892 DC |
141 | } MatroskaTrack; |
142 | ||
e5929fdf | 143 | typedef struct { |
b414cb89 AJ |
144 | char *filename; |
145 | char *mime; | |
146 | EbmlBin bin; | |
147 | } MatroskaAttachement; | |
148 | ||
149 | typedef struct { | |
6bbd7c7b AJ |
150 | uint64_t start; |
151 | uint64_t end; | |
152 | uint64_t uid; | |
153 | char *title; | |
154 | } MatroskaChapter; | |
155 | ||
156 | typedef struct { | |
e5929fdf AJ |
157 | uint64_t track; |
158 | uint64_t pos; | |
159 | } MatroskaIndexPos; | |
160 | ||
161 | typedef struct { | |
162 | uint64_t time; | |
163 | EbmlList pos; | |
164 | } MatroskaIndex; | |
165 | ||
13b350a3 | 166 | typedef struct { |
44015c56 AJ |
167 | char *name; |
168 | char *string; | |
169 | EbmlList sub; | |
170 | } MatroskaTag; | |
171 | ||
172 | typedef struct { | |
13b350a3 AJ |
173 | uint64_t id; |
174 | uint64_t pos; | |
175 | } MatroskaSeekhead; | |
176 | ||
c171af9b | 177 | typedef struct { |
8d75b5a2 AJ |
178 | uint64_t start; |
179 | uint64_t length; | |
b061d892 DC |
180 | } MatroskaLevel; |
181 | ||
c171af9b | 182 | typedef struct { |
b061d892 DC |
183 | AVFormatContext *ctx; |
184 | ||
5968d2dd | 185 | /* EBML stuff */ |
b061d892 DC |
186 | int num_levels; |
187 | MatroskaLevel levels[EBML_MAX_DEPTH]; | |
188 | int level_up; | |
189 | ||
29708581 AJ |
190 | uint64_t time_scale; |
191 | double duration; | |
192 | char *title; | |
2cbc8811 | 193 | EbmlList tracks; |
b414cb89 | 194 | EbmlList attachments; |
6bbd7c7b | 195 | EbmlList chapters; |
e5929fdf | 196 | EbmlList index; |
44015c56 | 197 | EbmlList tags; |
13b350a3 | 198 | EbmlList seekhead; |
b061d892 | 199 | |
b061d892 DC |
200 | /* byte position of the segment inside the stream */ |
201 | offset_t segment_start; | |
202 | ||
5968d2dd | 203 | /* the packet queue */ |
b061d892 DC |
204 | AVPacket **packets; |
205 | int num_packets; | |
206 | ||
8d75b5a2 | 207 | int done; |
ce6f28bd | 208 | int has_cluster_id; |
b061d892 | 209 | |
b061d892 DC |
210 | /* What to skip before effectively reading a packet. */ |
211 | int skip_to_keyframe; | |
212 | AVStream *skip_to_stream; | |
213 | } MatroskaDemuxContext; | |
214 | ||
209472b4 AJ |
215 | typedef struct { |
216 | uint64_t duration; | |
217 | int64_t reference; | |
218 | EbmlBin bin; | |
219 | } MatroskaBlock; | |
220 | ||
221 | typedef struct { | |
222 | uint64_t timecode; | |
223 | EbmlList blocks; | |
224 | } MatroskaCluster; | |
225 | ||
4b3dc529 AJ |
226 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x)) |
227 | ||
63511324 AJ |
228 | static EbmlSyntax ebml_header[] = { |
229 | { EBML_ID_EBMLREADVERSION, EBML_UINT, 0, offsetof(Ebml,version), {.u=EBML_VERSION} }, | |
230 | { EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, offsetof(Ebml,max_size), {.u=8} }, | |
231 | { EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, offsetof(Ebml,id_length), {.u=4} }, | |
232 | { EBML_ID_DOCTYPE, EBML_STR, 0, offsetof(Ebml,doctype), {.s="(none)"} }, | |
233 | { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, offsetof(Ebml,doctype_version), {.u=1} }, | |
234 | { EBML_ID_EBMLVERSION, EBML_NONE }, | |
235 | { EBML_ID_DOCTYPEVERSION, EBML_NONE }, | |
63511324 AJ |
236 | { 0 } |
237 | }; | |
238 | ||
239 | static EbmlSyntax ebml_syntax[] = { | |
240 | { EBML_ID_HEADER, EBML_NEST, 0, 0, {.n=ebml_header} }, | |
241 | { 0 } | |
242 | }; | |
243 | ||
29708581 AJ |
244 | static EbmlSyntax matroska_info[] = { |
245 | { MATROSKA_ID_TIMECODESCALE, EBML_UINT, 0, offsetof(MatroskaDemuxContext,time_scale), {.u=1000000} }, | |
246 | { MATROSKA_ID_DURATION, EBML_FLOAT, 0, offsetof(MatroskaDemuxContext,duration) }, | |
247 | { MATROSKA_ID_TITLE, EBML_UTF8, 0, offsetof(MatroskaDemuxContext,title) }, | |
248 | { MATROSKA_ID_WRITINGAPP, EBML_NONE }, | |
249 | { MATROSKA_ID_MUXINGAPP, EBML_NONE }, | |
250 | { MATROSKA_ID_DATEUTC, EBML_NONE }, | |
251 | { MATROSKA_ID_SEGMENTUID, EBML_NONE }, | |
29708581 AJ |
252 | { 0 } |
253 | }; | |
254 | ||
2cbc8811 AJ |
255 | static EbmlSyntax matroska_track_video[] = { |
256 | { MATROSKA_ID_VIDEOFRAMERATE, EBML_FLOAT,0, offsetof(MatroskaTrackVideo,frame_rate) }, | |
257 | { MATROSKA_ID_VIDEODISPLAYWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_width) }, | |
258 | { MATROSKA_ID_VIDEODISPLAYHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_height) }, | |
259 | { MATROSKA_ID_VIDEOPIXELWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_width) }, | |
260 | { MATROSKA_ID_VIDEOPIXELHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_height) }, | |
261 | { MATROSKA_ID_VIDEOCOLORSPACE, EBML_UINT, 0, offsetof(MatroskaTrackVideo,fourcc) }, | |
5df3cc6f AJ |
262 | { MATROSKA_ID_VIDEOPIXELCROPB, EBML_NONE }, |
263 | { MATROSKA_ID_VIDEOPIXELCROPT, EBML_NONE }, | |
264 | { MATROSKA_ID_VIDEOPIXELCROPL, EBML_NONE }, | |
265 | { MATROSKA_ID_VIDEOPIXELCROPR, EBML_NONE }, | |
266 | { MATROSKA_ID_VIDEODISPLAYUNIT, EBML_NONE }, | |
2cbc8811 AJ |
267 | { MATROSKA_ID_VIDEOFLAGINTERLACED,EBML_NONE }, |
268 | { MATROSKA_ID_VIDEOSTEREOMODE, EBML_NONE }, | |
269 | { MATROSKA_ID_VIDEOASPECTRATIO, EBML_NONE }, | |
2cbc8811 AJ |
270 | { 0 } |
271 | }; | |
272 | ||
273 | static EbmlSyntax matroska_track_audio[] = { | |
274 | { MATROSKA_ID_AUDIOSAMPLINGFREQ, EBML_FLOAT,0, offsetof(MatroskaTrackAudio,samplerate), {.f=8000.0} }, | |
275 | { MATROSKA_ID_AUDIOOUTSAMPLINGFREQ,EBML_FLOAT,0,offsetof(MatroskaTrackAudio,out_samplerate) }, | |
276 | { MATROSKA_ID_AUDIOBITDEPTH, EBML_UINT, 0, offsetof(MatroskaTrackAudio,bitdepth) }, | |
277 | { MATROSKA_ID_AUDIOCHANNELS, EBML_UINT, 0, offsetof(MatroskaTrackAudio,channels), {.u=1} }, | |
2cbc8811 AJ |
278 | { 0 } |
279 | }; | |
280 | ||
281 | static EbmlSyntax matroska_track_encoding_compression[] = { | |
282 | { MATROSKA_ID_ENCODINGCOMPALGO, EBML_UINT, 0, offsetof(MatroskaTrackCompression,algo), {.u=0} }, | |
283 | { MATROSKA_ID_ENCODINGCOMPSETTINGS,EBML_BIN, 0, offsetof(MatroskaTrackCompression,settings) }, | |
2cbc8811 AJ |
284 | { 0 } |
285 | }; | |
286 | ||
287 | static EbmlSyntax matroska_track_encoding[] = { | |
288 | { MATROSKA_ID_ENCODINGSCOPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,scope), {.u=1} }, | |
289 | { MATROSKA_ID_ENCODINGTYPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,type), {.u=0} }, | |
290 | { MATROSKA_ID_ENCODINGCOMPRESSION,EBML_NEST, 0, offsetof(MatroskaTrackEncoding,compression), {.n=matroska_track_encoding_compression} }, | |
5df3cc6f | 291 | { MATROSKA_ID_ENCODINGORDER, EBML_NONE }, |
2cbc8811 AJ |
292 | { 0 } |
293 | }; | |
294 | ||
295 | static EbmlSyntax matroska_track_encodings[] = { | |
296 | { MATROSKA_ID_TRACKCONTENTENCODING, EBML_NEST, sizeof(MatroskaTrackEncoding), offsetof(MatroskaTrack,encodings), {.n=matroska_track_encoding} }, | |
2cbc8811 AJ |
297 | { 0 } |
298 | }; | |
299 | ||
300 | static EbmlSyntax matroska_track[] = { | |
301 | { MATROSKA_ID_TRACKNUMBER, EBML_UINT, 0, offsetof(MatroskaTrack,num) }, | |
302 | { MATROSKA_ID_TRACKTYPE, EBML_UINT, 0, offsetof(MatroskaTrack,type) }, | |
303 | { MATROSKA_ID_CODECID, EBML_STR, 0, offsetof(MatroskaTrack,codec_id) }, | |
304 | { MATROSKA_ID_CODECPRIVATE, EBML_BIN, 0, offsetof(MatroskaTrack,codec_priv) }, | |
305 | { MATROSKA_ID_TRACKLANGUAGE, EBML_UTF8, 0, offsetof(MatroskaTrack,language), {.s="eng"} }, | |
306 | { MATROSKA_ID_TRACKDEFAULTDURATION, EBML_UINT, 0, offsetof(MatroskaTrack,default_duration) }, | |
307 | { MATROSKA_ID_TRACKTIMECODESCALE, EBML_FLOAT,0, offsetof(MatroskaTrack,time_scale), {.f=1.0} }, | |
308 | { MATROSKA_ID_TRACKFLAGDEFAULT, EBML_UINT, 0, offsetof(MatroskaTrack,flag_default), {.u=1} }, | |
309 | { MATROSKA_ID_TRACKVIDEO, EBML_NEST, 0, offsetof(MatroskaTrack,video), {.n=matroska_track_video} }, | |
310 | { MATROSKA_ID_TRACKAUDIO, EBML_NEST, 0, offsetof(MatroskaTrack,audio), {.n=matroska_track_audio} }, | |
311 | { MATROSKA_ID_TRACKCONTENTENCODINGS,EBML_NEST, 0, 0, {.n=matroska_track_encodings} }, | |
312 | { MATROSKA_ID_TRACKUID, EBML_NONE }, | |
313 | { MATROSKA_ID_TRACKNAME, EBML_NONE }, | |
314 | { MATROSKA_ID_TRACKFLAGENABLED, EBML_NONE }, | |
315 | { MATROSKA_ID_TRACKFLAGFORCED, EBML_NONE }, | |
316 | { MATROSKA_ID_TRACKFLAGLACING, EBML_NONE }, | |
317 | { MATROSKA_ID_CODECNAME, EBML_NONE }, | |
318 | { MATROSKA_ID_CODECDECODEALL, EBML_NONE }, | |
319 | { MATROSKA_ID_CODECINFOURL, EBML_NONE }, | |
320 | { MATROSKA_ID_CODECDOWNLOADURL, EBML_NONE }, | |
321 | { MATROSKA_ID_TRACKMINCACHE, EBML_NONE }, | |
322 | { MATROSKA_ID_TRACKMAXCACHE, EBML_NONE }, | |
5df3cc6f | 323 | { MATROSKA_ID_TRACKMAXBLKADDID, EBML_NONE }, |
2cbc8811 AJ |
324 | { 0 } |
325 | }; | |
326 | ||
327 | static EbmlSyntax matroska_tracks[] = { | |
328 | { MATROSKA_ID_TRACKENTRY, EBML_NEST, sizeof(MatroskaTrack), offsetof(MatroskaDemuxContext,tracks), {.n=matroska_track} }, | |
2cbc8811 AJ |
329 | { 0 } |
330 | }; | |
331 | ||
b414cb89 AJ |
332 | static EbmlSyntax matroska_attachment[] = { |
333 | { MATROSKA_ID_FILENAME, EBML_UTF8, 0, offsetof(MatroskaAttachement,filename) }, | |
334 | { MATROSKA_ID_FILEMIMETYPE, EBML_STR, 0, offsetof(MatroskaAttachement,mime) }, | |
335 | { MATROSKA_ID_FILEDATA, EBML_BIN, 0, offsetof(MatroskaAttachement,bin) }, | |
5df3cc6f | 336 | { MATROSKA_ID_FILEDESC, EBML_NONE }, |
b414cb89 | 337 | { MATROSKA_ID_FILEUID, EBML_NONE }, |
b414cb89 AJ |
338 | { 0 } |
339 | }; | |
340 | ||
341 | static EbmlSyntax matroska_attachments[] = { | |
342 | { MATROSKA_ID_ATTACHEDFILE, EBML_NEST, sizeof(MatroskaAttachement), offsetof(MatroskaDemuxContext,attachments), {.n=matroska_attachment} }, | |
b414cb89 AJ |
343 | { 0 } |
344 | }; | |
345 | ||
6bbd7c7b AJ |
346 | static EbmlSyntax matroska_chapter_display[] = { |
347 | { MATROSKA_ID_CHAPSTRING, EBML_UTF8, 0, offsetof(MatroskaChapter,title) }, | |
5df3cc6f | 348 | { MATROSKA_ID_CHAPLANG, EBML_NONE }, |
6bbd7c7b AJ |
349 | { 0 } |
350 | }; | |
351 | ||
352 | static EbmlSyntax matroska_chapter_entry[] = { | |
353 | { MATROSKA_ID_CHAPTERTIMESTART, EBML_UINT, 0, offsetof(MatroskaChapter,start), {.u=AV_NOPTS_VALUE} }, | |
354 | { MATROSKA_ID_CHAPTERTIMEEND, EBML_UINT, 0, offsetof(MatroskaChapter,end), {.u=AV_NOPTS_VALUE} }, | |
355 | { MATROSKA_ID_CHAPTERUID, EBML_UINT, 0, offsetof(MatroskaChapter,uid) }, | |
356 | { MATROSKA_ID_CHAPTERDISPLAY, EBML_NEST, 0, 0, {.n=matroska_chapter_display} }, | |
357 | { MATROSKA_ID_CHAPTERFLAGHIDDEN, EBML_NONE }, | |
5df3cc6f AJ |
358 | { MATROSKA_ID_CHAPTERFLAGENABLED, EBML_NONE }, |
359 | { MATROSKA_ID_CHAPTERPHYSEQUIV, EBML_NONE }, | |
360 | { MATROSKA_ID_CHAPTERATOM, EBML_NONE }, | |
6bbd7c7b AJ |
361 | { 0 } |
362 | }; | |
363 | ||
364 | static EbmlSyntax matroska_chapter[] = { | |
365 | { MATROSKA_ID_CHAPTERATOM, EBML_NEST, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext,chapters), {.n=matroska_chapter_entry} }, | |
366 | { MATROSKA_ID_EDITIONUID, EBML_NONE }, | |
367 | { MATROSKA_ID_EDITIONFLAGHIDDEN, EBML_NONE }, | |
368 | { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE }, | |
5df3cc6f | 369 | { MATROSKA_ID_EDITIONFLAGORDERED, EBML_NONE }, |
6bbd7c7b AJ |
370 | { 0 } |
371 | }; | |
372 | ||
373 | static EbmlSyntax matroska_chapters[] = { | |
374 | { MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, 0, {.n=matroska_chapter} }, | |
6bbd7c7b AJ |
375 | { 0 } |
376 | }; | |
377 | ||
e5929fdf AJ |
378 | static EbmlSyntax matroska_index_pos[] = { |
379 | { MATROSKA_ID_CUETRACK, EBML_UINT, 0, offsetof(MatroskaIndexPos,track) }, | |
380 | { MATROSKA_ID_CUECLUSTERPOSITION, EBML_UINT, 0, offsetof(MatroskaIndexPos,pos) }, | |
5df3cc6f | 381 | { MATROSKA_ID_CUEBLOCKNUMBER, EBML_NONE }, |
e5929fdf AJ |
382 | { 0 } |
383 | }; | |
384 | ||
385 | static EbmlSyntax matroska_index_entry[] = { | |
386 | { MATROSKA_ID_CUETIME, EBML_UINT, 0, offsetof(MatroskaIndex,time) }, | |
387 | { MATROSKA_ID_CUETRACKPOSITION, EBML_NEST, sizeof(MatroskaIndexPos), offsetof(MatroskaIndex,pos), {.n=matroska_index_pos} }, | |
e5929fdf AJ |
388 | { 0 } |
389 | }; | |
390 | ||
391 | static EbmlSyntax matroska_index[] = { | |
392 | { MATROSKA_ID_POINTENTRY, EBML_NEST, sizeof(MatroskaIndex), offsetof(MatroskaDemuxContext,index), {.n=matroska_index_entry} }, | |
e5929fdf AJ |
393 | { 0 } |
394 | }; | |
395 | ||
44015c56 AJ |
396 | static EbmlSyntax matroska_simpletag[] = { |
397 | { MATROSKA_ID_TAGNAME, EBML_UTF8, 0, offsetof(MatroskaTag,name) }, | |
398 | { MATROSKA_ID_TAGSTRING, EBML_UTF8, 0, offsetof(MatroskaTag,string) }, | |
399 | { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTag,sub), {.n=matroska_simpletag} }, | |
400 | { MATROSKA_ID_TAGLANG, EBML_NONE }, | |
401 | { MATROSKA_ID_TAGDEFAULT, EBML_NONE }, | |
44015c56 AJ |
402 | { 0 } |
403 | }; | |
404 | ||
405 | static EbmlSyntax matroska_tag[] = { | |
406 | { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), 0, {.n=matroska_simpletag} }, | |
407 | { MATROSKA_ID_TAGTARGETS, EBML_NONE }, | |
44015c56 AJ |
408 | { 0 } |
409 | }; | |
410 | ||
434d496a | 411 | static EbmlSyntax matroska_tags[] = { |
44015c56 | 412 | { MATROSKA_ID_TAG, EBML_NEST, 0, offsetof(MatroskaDemuxContext,tags), {.n=matroska_tag} }, |
434d496a AJ |
413 | { 0 } |
414 | }; | |
415 | ||
13b350a3 AJ |
416 | static EbmlSyntax matroska_seekhead_entry[] = { |
417 | { MATROSKA_ID_SEEKID, EBML_UINT, 0, offsetof(MatroskaSeekhead,id) }, | |
418 | { MATROSKA_ID_SEEKPOSITION, EBML_UINT, 0, offsetof(MatroskaSeekhead,pos), {.u=-1} }, | |
13b350a3 AJ |
419 | { 0 } |
420 | }; | |
421 | ||
422 | static EbmlSyntax matroska_seekhead[] = { | |
423 | { MATROSKA_ID_SEEKENTRY, EBML_NEST, sizeof(MatroskaSeekhead), offsetof(MatroskaDemuxContext,seekhead), {.n=matroska_seekhead_entry} }, | |
13b350a3 AJ |
424 | { 0 } |
425 | }; | |
426 | ||
ce6f28bd AJ |
427 | static EbmlSyntax matroska_segment[] = { |
428 | { MATROSKA_ID_INFO, EBML_NEST, 0, 0, {.n=matroska_info } }, | |
429 | { MATROSKA_ID_TRACKS, EBML_NEST, 0, 0, {.n=matroska_tracks } }, | |
430 | { MATROSKA_ID_ATTACHMENTS, EBML_NEST, 0, 0, {.n=matroska_attachments} }, | |
431 | { MATROSKA_ID_CHAPTERS, EBML_NEST, 0, 0, {.n=matroska_chapters } }, | |
432 | { MATROSKA_ID_CUES, EBML_NEST, 0, 0, {.n=matroska_index } }, | |
433 | { MATROSKA_ID_TAGS, EBML_NEST, 0, 0, {.n=matroska_tags } }, | |
434 | { MATROSKA_ID_SEEKHEAD, EBML_NEST, 0, 0, {.n=matroska_seekhead } }, | |
435 | { MATROSKA_ID_CLUSTER, EBML_STOP, 0, offsetof(MatroskaDemuxContext,has_cluster_id) }, | |
ce6f28bd AJ |
436 | { 0 } |
437 | }; | |
438 | ||
439 | static EbmlSyntax matroska_segments[] = { | |
440 | { MATROSKA_ID_SEGMENT, EBML_NEST, 0, 0, {.n=matroska_segment } }, | |
441 | { 0 } | |
442 | }; | |
443 | ||
209472b4 AJ |
444 | static EbmlSyntax matroska_blockgroup[] = { |
445 | { MATROSKA_ID_BLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) }, | |
446 | { MATROSKA_ID_SIMPLEBLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) }, | |
447 | { MATROSKA_ID_BLOCKDURATION, EBML_UINT, 0, offsetof(MatroskaBlock,duration), {.u=AV_NOPTS_VALUE} }, | |
448 | { MATROSKA_ID_BLOCKREFERENCE, EBML_UINT, 0, offsetof(MatroskaBlock,reference) }, | |
209472b4 AJ |
449 | { 0 } |
450 | }; | |
451 | ||
452 | static EbmlSyntax matroska_cluster[] = { | |
453 | { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) }, | |
454 | { MATROSKA_ID_BLOCKGROUP, EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} }, | |
455 | { MATROSKA_ID_SIMPLEBLOCK, EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} }, | |
5df3cc6f AJ |
456 | { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE }, |
457 | { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE }, | |
209472b4 AJ |
458 | { 0 } |
459 | }; | |
460 | ||
461 | static EbmlSyntax matroska_clusters[] = { | |
462 | { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster} }, | |
5df3cc6f AJ |
463 | { MATROSKA_ID_INFO, EBML_NONE }, |
464 | { MATROSKA_ID_CUES, EBML_NONE }, | |
465 | { MATROSKA_ID_TAGS, EBML_NONE }, | |
466 | { MATROSKA_ID_SEEKHEAD, EBML_NONE }, | |
209472b4 AJ |
467 | { 0 } |
468 | }; | |
469 | ||
44015c56 AJ |
470 | #define SIZE_OFF(x) sizeof(((AVFormatContext*)0)->x),offsetof(AVFormatContext,x) |
471 | const struct { | |
472 | const char name[16]; | |
473 | int size; | |
474 | int offset; | |
475 | } metadata[] = { | |
476 | { "TITLE", SIZE_OFF(title) }, | |
477 | { "ARTIST", SIZE_OFF(author) }, | |
478 | { "WRITTEN_BY", SIZE_OFF(author) }, | |
479 | { "LEAD_PERFORMER", SIZE_OFF(author) }, | |
480 | { "COPYRIGHT", SIZE_OFF(copyright) }, | |
481 | { "COMMENT", SIZE_OFF(comment) }, | |
482 | { "ALBUM", SIZE_OFF(album) }, | |
483 | { "DATE_WRITTEN", SIZE_OFF(year) }, | |
484 | { "DATE_RELEASED", SIZE_OFF(year) }, | |
485 | { "PART_NUMBER", SIZE_OFF(track) }, | |
486 | { "GENRE", SIZE_OFF(genre) }, | |
487 | }; | |
488 | ||
b061d892 | 489 | /* |
5968d2dd | 490 | * Return: Whether we reached the end of a level in the hierarchy or not. |
b061d892 | 491 | */ |
592110c2 | 492 | static int ebml_level_end(MatroskaDemuxContext *matroska) |
b061d892 | 493 | { |
899681cd | 494 | ByteIOContext *pb = matroska->ctx->pb; |
b061d892 | 495 | offset_t pos = url_ftell(pb); |
b061d892 | 496 | |
592110c2 | 497 | if (matroska->num_levels > 0) { |
b061d892 | 498 | MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1]; |
592110c2 | 499 | if (pos - level->start >= level->length) { |
b061d892 | 500 | matroska->num_levels--; |
592110c2 | 501 | return 1; |
b061d892 DC |
502 | } |
503 | } | |
592110c2 | 504 | return 0; |
b061d892 DC |
505 | } |
506 | ||
507 | /* | |
508 | * Read: an "EBML number", which is defined as a variable-length | |
509 | * array of bytes. The first byte indicates the length by giving a | |
510 | * number of 0-bits followed by a one. The position of the first | |
511 | * "one" bit inside the first byte indicates the length of this | |
512 | * number. | |
5968d2dd | 513 | * Returns: number of bytes read, < 0 on error |
b061d892 | 514 | */ |
c1e01133 | 515 | static int ebml_read_num(MatroskaDemuxContext *matroska, ByteIOContext *pb, |
f7b9687c | 516 | int max_size, uint64_t *number) |
b061d892 | 517 | { |
b061d892 DC |
518 | int len_mask = 0x80, read = 1, n = 1; |
519 | int64_t total = 0; | |
520 | ||
5968d2dd | 521 | /* The first byte tells us the length in bytes - get_byte() can normally |
b061d892 DC |
522 | * return 0, but since that's not a valid first ebmlID byte, we can |
523 | * use it safely here to catch EOS. */ | |
524 | if (!(total = get_byte(pb))) { | |
525 | /* we might encounter EOS here */ | |
526 | if (!url_feof(pb)) { | |
527 | offset_t pos = url_ftell(pb); | |
528 | av_log(matroska->ctx, AV_LOG_ERROR, | |
529 | "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", | |
530 | pos, pos); | |
531 | } | |
6f3e0b21 | 532 | return AVERROR(EIO); /* EOS or actual I/O error */ |
b061d892 DC |
533 | } |
534 | ||
535 | /* get the length of the EBML number */ | |
536 | while (read <= max_size && !(total & len_mask)) { | |
537 | read++; | |
538 | len_mask >>= 1; | |
539 | } | |
540 | if (read > max_size) { | |
541 | offset_t pos = url_ftell(pb) - 1; | |
542 | av_log(matroska->ctx, AV_LOG_ERROR, | |
543 | "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n", | |
544 | (uint8_t) total, pos, pos); | |
545 | return AVERROR_INVALIDDATA; | |
546 | } | |
547 | ||
548 | /* read out length */ | |
549 | total &= ~len_mask; | |
550 | while (n++ < read) | |
551 | total = (total << 8) | get_byte(pb); | |
552 | ||
553 | *number = total; | |
554 | ||
555 | return read; | |
556 | } | |
557 | ||
558 | /* | |
b061d892 DC |
559 | * Read the next element as an unsigned int. |
560 | * 0 is success, < 0 is failure. | |
561 | */ | |
f7b9687c | 562 | static int ebml_read_uint(ByteIOContext *pb, int size, uint64_t *num) |
b061d892 | 563 | { |
c6cd2b3d | 564 | int n = 0; |
b061d892 | 565 | |
ba5a1f99 | 566 | if (size < 1 || size > 8) |
b061d892 | 567 | return AVERROR_INVALIDDATA; |
b061d892 | 568 | |
5968d2dd | 569 | /* big-endian ordering; build up number */ |
b061d892 DC |
570 | *num = 0; |
571 | while (n++ < size) | |
572 | *num = (*num << 8) | get_byte(pb); | |
573 | ||
574 | return 0; | |
575 | } | |
576 | ||
577 | /* | |
b061d892 DC |
578 | * Read the next element as a float. |
579 | * 0 is success, < 0 is failure. | |
580 | */ | |
f7b9687c | 581 | static int ebml_read_float(ByteIOContext *pb, int size, double *num) |
b061d892 | 582 | { |
b061d892 DC |
583 | if (size == 4) { |
584 | *num= av_int2flt(get_be32(pb)); | |
585 | } else if(size==8){ | |
586 | *num= av_int2dbl(get_be64(pb)); | |
ba5a1f99 | 587 | } else |
b061d892 | 588 | return AVERROR_INVALIDDATA; |
b061d892 DC |
589 | |
590 | return 0; | |
591 | } | |
592 | ||
593 | /* | |
594 | * Read the next element as an ASCII string. | |
595 | * 0 is success, < 0 is failure. | |
596 | */ | |
f7b9687c | 597 | static int ebml_read_ascii(ByteIOContext *pb, int size, char **str) |
b061d892 | 598 | { |
c6cd2b3d | 599 | av_free(*str); |
5968d2dd | 600 | /* EBML strings are usually not 0-terminated, so we allocate one |
b061d892 | 601 | * byte more, read the string and NULL-terminate it ourselves. */ |
c6cd2b3d | 602 | if (!(*str = av_malloc(size + 1))) |
769e10f0 | 603 | return AVERROR(ENOMEM); |
b061d892 | 604 | if (get_buffer(pb, (uint8_t *) *str, size) != size) { |
ff2c222c | 605 | av_free(*str); |
6f3e0b21 | 606 | return AVERROR(EIO); |
b061d892 DC |
607 | } |
608 | (*str)[size] = '\0'; | |
609 | ||
610 | return 0; | |
611 | } | |
612 | ||
613 | /* | |
737c40da AJ |
614 | * Read the next element as binary data. |
615 | * 0 is success, < 0 is failure. | |
616 | */ | |
617 | static int ebml_read_binary(ByteIOContext *pb, int length, EbmlBin *bin) | |
618 | { | |
619 | av_free(bin->data); | |
620 | if (!(bin->data = av_malloc(length))) | |
621 | return AVERROR(ENOMEM); | |
622 | ||
623 | bin->size = length; | |
624 | bin->pos = url_ftell(pb); | |
625 | if (get_buffer(pb, bin->data, length) != length) | |
626 | return AVERROR(EIO); | |
627 | ||
628 | return 0; | |
629 | } | |
630 | ||
631 | /* | |
b061d892 DC |
632 | * Read the next element, but only the header. The contents |
633 | * are supposed to be sub-elements which can be read separately. | |
634 | * 0 is success, < 0 is failure. | |
635 | */ | |
f7b9687c | 636 | static int ebml_read_master(MatroskaDemuxContext *matroska, int length) |
b061d892 | 637 | { |
899681cd | 638 | ByteIOContext *pb = matroska->ctx->pb; |
b061d892 | 639 | MatroskaLevel *level; |
b061d892 | 640 | |
b061d892 DC |
641 | if (matroska->num_levels >= EBML_MAX_DEPTH) { |
642 | av_log(matroska->ctx, AV_LOG_ERROR, | |
643 | "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH); | |
85565db0 | 644 | return AVERROR(ENOSYS); |
b061d892 DC |
645 | } |
646 | ||
b061d892 DC |
647 | level = &matroska->levels[matroska->num_levels++]; |
648 | level->start = url_ftell(pb); | |
649 | level->length = length; | |
650 | ||
651 | return 0; | |
652 | } | |
653 | ||
654 | /* | |
b061d892 | 655 | * Read signed/unsigned "EBML" numbers. |
5968d2dd | 656 | * Return: number of bytes processed, < 0 on error |
b061d892 | 657 | */ |
c1e01133 AJ |
658 | static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska, |
659 | uint8_t *data, uint32_t size, uint64_t *num) | |
b061d892 | 660 | { |
c1e01133 AJ |
661 | ByteIOContext pb; |
662 | init_put_byte(&pb, data, size, 0, NULL, NULL, NULL, NULL); | |
663 | return ebml_read_num(matroska, &pb, 8, num); | |
b061d892 DC |
664 | } |
665 | ||
666 | /* | |
667 | * Same as above, but signed. | |
668 | */ | |
c1e01133 AJ |
669 | static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska, |
670 | uint8_t *data, uint32_t size, int64_t *num) | |
b061d892 DC |
671 | { |
672 | uint64_t unum; | |
673 | int res; | |
674 | ||
675 | /* read as unsigned number first */ | |
c1e01133 | 676 | if ((res = matroska_ebmlnum_uint(matroska, data, size, &unum)) < 0) |
b061d892 DC |
677 | return res; |
678 | ||
679 | /* make signed (weird way) */ | |
33ac07ea | 680 | *num = unum - ((1LL << (7*res - 1)) - 1); |
b061d892 DC |
681 | |
682 | return res; | |
683 | } | |
684 | ||
737c40da AJ |
685 | static int ebml_parse_elem(MatroskaDemuxContext *matroska, |
686 | EbmlSyntax *syntax, void *data); | |
b061d892 | 687 | |
737c40da AJ |
688 | static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, |
689 | uint32_t id, void *data) | |
b061d892 DC |
690 | { |
691 | int i; | |
737c40da AJ |
692 | for (i=0; syntax[i].id; i++) |
693 | if (id == syntax[i].id) | |
694 | break; | |
b49d17b7 | 695 | if (!syntax[i].id && id != EBML_ID_VOID && id != EBML_ID_CRC32) |
737c40da AJ |
696 | av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id); |
697 | return ebml_parse_elem(matroska, &syntax[i], data); | |
b061d892 DC |
698 | } |
699 | ||
737c40da AJ |
700 | static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, |
701 | void *data) | |
b061d892 | 702 | { |
88cca989 AJ |
703 | uint64_t id; |
704 | int res = ebml_read_num(matroska, matroska->ctx->pb, 4, &id); | |
705 | id |= 1 << 7*res; | |
737c40da | 706 | return res < 0 ? res : ebml_parse_id(matroska, syntax, id, data); |
b061d892 DC |
707 | } |
708 | ||
737c40da AJ |
709 | static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, |
710 | void *data) | |
b061d892 | 711 | { |
737c40da | 712 | int i, res = 0; |
b061d892 | 713 | |
737c40da AJ |
714 | for (i=0; syntax[i].id; i++) |
715 | switch (syntax[i].type) { | |
716 | case EBML_UINT: | |
717 | *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u; | |
718 | break; | |
719 | case EBML_FLOAT: | |
720 | *(double *)((char *)data+syntax[i].data_offset) = syntax[i].def.f; | |
721 | break; | |
722 | case EBML_STR: | |
723 | case EBML_UTF8: | |
724 | *(char **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s); | |
725 | break; | |
34c9c1ba | 726 | } |
b061d892 | 727 | |
737c40da AJ |
728 | while (!res && !ebml_level_end(matroska)) |
729 | res = ebml_parse(matroska, syntax, data); | |
b061d892 | 730 | |
737c40da | 731 | return res; |
b061d892 DC |
732 | } |
733 | ||
789ed100 AJ |
734 | static int ebml_parse_elem(MatroskaDemuxContext *matroska, |
735 | EbmlSyntax *syntax, void *data) | |
736 | { | |
c6cd2b3d | 737 | ByteIOContext *pb = matroska->ctx->pb; |
789ed100 | 738 | uint32_t id = syntax->id; |
c6cd2b3d | 739 | uint64_t length; |
789ed100 AJ |
740 | int res; |
741 | ||
742 | data = (char *)data + syntax->data_offset; | |
743 | if (syntax->list_elem_size) { | |
744 | EbmlList *list = data; | |
745 | list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size); | |
746 | data = (char*)list->elem + list->nb_elem*syntax->list_elem_size; | |
747 | memset(data, 0, syntax->list_elem_size); | |
748 | list->nb_elem++; | |
749 | } | |
789ed100 | 750 | |
c6cd2b3d | 751 | if (syntax->type != EBML_PASS && syntax->type != EBML_STOP) |
c1e01133 | 752 | if ((res = ebml_read_num(matroska, pb, 8, &length)) < 0) |
c6cd2b3d AJ |
753 | return res; |
754 | ||
789ed100 | 755 | switch (syntax->type) { |
c6cd2b3d AJ |
756 | case EBML_UINT: res = ebml_read_uint (pb, length, data); break; |
757 | case EBML_FLOAT: res = ebml_read_float (pb, length, data); break; | |
789ed100 | 758 | case EBML_STR: |
c6cd2b3d | 759 | case EBML_UTF8: res = ebml_read_ascii (pb, length, data); break; |
eb05cacc | 760 | case EBML_BIN: res = ebml_read_binary(pb, length, data); break; |
c6cd2b3d | 761 | case EBML_NEST: if ((res=ebml_read_master(matroska, length)) < 0) |
789ed100 AJ |
762 | return res; |
763 | if (id == MATROSKA_ID_SEGMENT) | |
764 | matroska->segment_start = url_ftell(matroska->ctx->pb); | |
6314cca0 | 765 | return ebml_parse_nest(matroska, syntax->def.n, data); |
c4d3d9ba | 766 | case EBML_PASS: return ebml_parse_id(matroska, syntax->def.n, id, data); |
789ed100 | 767 | case EBML_STOP: *(int *)data = 1; return 1; |
dc3e0211 | 768 | default: return url_fseek(pb,length,SEEK_CUR)<0 ? AVERROR(EIO) : 0; |
789ed100 | 769 | } |
ba5a1f99 AJ |
770 | if (res == AVERROR_INVALIDDATA) |
771 | av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n"); | |
772 | else if (res == AVERROR(EIO)) | |
773 | av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n"); | |
774 | return res; | |
789ed100 AJ |
775 | } |
776 | ||
789ed100 AJ |
777 | static void ebml_free(EbmlSyntax *syntax, void *data) |
778 | { | |
779 | int i, j; | |
780 | for (i=0; syntax[i].id; i++) { | |
781 | void *data_off = (char *)data + syntax[i].data_offset; | |
782 | switch (syntax[i].type) { | |
783 | case EBML_STR: | |
784 | case EBML_UTF8: av_freep(data_off); break; | |
785 | case EBML_BIN: av_freep(&((EbmlBin *)data_off)->data); break; | |
786 | case EBML_NEST: | |
787 | if (syntax[i].list_elem_size) { | |
788 | EbmlList *list = data_off; | |
789 | char *ptr = list->elem; | |
790 | for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size) | |
791 | ebml_free(syntax[i].def.n, ptr); | |
792 | av_free(list->elem); | |
793 | } else | |
794 | ebml_free(syntax[i].def.n, data_off); | |
795 | default: break; | |
796 | } | |
797 | } | |
798 | } | |
799 | ||
737c40da AJ |
800 | |
801 | /* | |
802 | * Autodetecting... | |
803 | */ | |
804 | static int matroska_probe(AVProbeData *p) | |
805 | { | |
806 | uint64_t total = 0; | |
807 | int len_mask = 0x80, size = 1, n = 1; | |
7b571fd7 | 808 | static const char probe_data[] = "matroska"; |
737c40da | 809 | |
5968d2dd | 810 | /* EBML header? */ |
737c40da AJ |
811 | if (AV_RB32(p->buf) != EBML_ID_HEADER) |
812 | return 0; | |
813 | ||
814 | /* length of header */ | |
815 | total = p->buf[4]; | |
816 | while (size <= 8 && !(total & len_mask)) { | |
817 | size++; | |
818 | len_mask >>= 1; | |
819 | } | |
820 | if (size > 8) | |
821 | return 0; | |
822 | total &= (len_mask - 1); | |
823 | while (n < size) | |
824 | total = (total << 8) | p->buf[4 + n++]; | |
825 | ||
5968d2dd | 826 | /* Does the probe data contain the whole header? */ |
737c40da AJ |
827 | if (p->buf_size < 4 + size + total) |
828 | return 0; | |
829 | ||
5968d2dd | 830 | /* The header must contain the document type 'matroska'. For now, |
737c40da AJ |
831 | * we don't parse the whole header but simply check for the |
832 | * availability of that array of characters inside the header. | |
833 | * Not fully fool-proof, but good enough. */ | |
834 | for (n = 4+size; n <= 4+size+total-(sizeof(probe_data)-1); n++) | |
835 | if (!memcmp(p->buf+n, probe_data, sizeof(probe_data)-1)) | |
836 | return AVPROBE_SCORE_MAX; | |
837 | ||
838 | return 0; | |
839 | } | |
840 | ||
841 | static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska, | |
842 | int num) | |
843 | { | |
844 | MatroskaTrack *tracks = matroska->tracks.elem; | |
845 | int i; | |
846 | ||
847 | for (i=0; i < matroska->tracks.nb_elem; i++) | |
848 | if (tracks[i].num == num) | |
849 | return &tracks[i]; | |
850 | ||
851 | av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num); | |
852 | return NULL; | |
853 | } | |
854 | ||
f7b9687c AJ |
855 | static int matroska_decode_buffer(uint8_t** buf, int* buf_size, |
856 | MatroskaTrack *track) | |
935ec5a1 | 857 | { |
2cbc8811 | 858 | MatroskaTrackEncoding *encodings = track->encodings.elem; |
935ec5a1 ES |
859 | uint8_t* data = *buf; |
860 | int isize = *buf_size; | |
861 | uint8_t* pkt_data = NULL; | |
862 | int pkt_size = isize; | |
863 | int result = 0; | |
864 | int olen; | |
865 | ||
2cbc8811 | 866 | switch (encodings[0].compression.algo) { |
935ec5a1 | 867 | case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP: |
2cbc8811 | 868 | return encodings[0].compression.settings.size; |
935ec5a1 ES |
869 | case MATROSKA_TRACK_ENCODING_COMP_LZO: |
870 | do { | |
871 | olen = pkt_size *= 3; | |
872 | pkt_data = av_realloc(pkt_data, | |
873 | pkt_size+LZO_OUTPUT_PADDING); | |
874 | result = lzo1x_decode(pkt_data, &olen, data, &isize); | |
875 | } while (result==LZO_OUTPUT_FULL && pkt_size<10000000); | |
876 | if (result) | |
877 | goto failed; | |
878 | pkt_size -= olen; | |
879 | break; | |
880 | #ifdef CONFIG_ZLIB | |
881 | case MATROSKA_TRACK_ENCODING_COMP_ZLIB: { | |
882 | z_stream zstream = {0}; | |
883 | if (inflateInit(&zstream) != Z_OK) | |
884 | return -1; | |
885 | zstream.next_in = data; | |
886 | zstream.avail_in = isize; | |
887 | do { | |
888 | pkt_size *= 3; | |
889 | pkt_data = av_realloc(pkt_data, pkt_size); | |
890 | zstream.avail_out = pkt_size - zstream.total_out; | |
891 | zstream.next_out = pkt_data + zstream.total_out; | |
892 | result = inflate(&zstream, Z_NO_FLUSH); | |
893 | } while (result==Z_OK && pkt_size<10000000); | |
894 | pkt_size = zstream.total_out; | |
895 | inflateEnd(&zstream); | |
896 | if (result != Z_STREAM_END) | |
897 | goto failed; | |
898 | break; | |
899 | } | |
900 | #endif | |
901 | #ifdef CONFIG_BZLIB | |
902 | case MATROSKA_TRACK_ENCODING_COMP_BZLIB: { | |
903 | bz_stream bzstream = {0}; | |
904 | if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK) | |
905 | return -1; | |
906 | bzstream.next_in = data; | |
907 | bzstream.avail_in = isize; | |
908 | do { | |
909 | pkt_size *= 3; | |
910 | pkt_data = av_realloc(pkt_data, pkt_size); | |
911 | bzstream.avail_out = pkt_size - bzstream.total_out_lo32; | |
912 | bzstream.next_out = pkt_data + bzstream.total_out_lo32; | |
913 | result = BZ2_bzDecompress(&bzstream); | |
914 | } while (result==BZ_OK && pkt_size<10000000); | |
915 | pkt_size = bzstream.total_out_lo32; | |
916 | BZ2_bzDecompressEnd(&bzstream); | |
917 | if (result != BZ_STREAM_END) | |
918 | goto failed; | |
919 | break; | |
920 | } | |
921 | #endif | |
28f27e0c AJ |
922 | default: |
923 | return -1; | |
935ec5a1 ES |
924 | } |
925 | ||
926 | *buf = pkt_data; | |
927 | *buf_size = pkt_size; | |
928 | return 0; | |
929 | failed: | |
930 | av_free(pkt_data); | |
931 | return -1; | |
932 | } | |
933 | ||
44015c56 AJ |
934 | static void matroska_convert_tags(AVFormatContext *s, EbmlList *list) |
935 | { | |
936 | MatroskaTag *tags = list->elem; | |
937 | int i, j; | |
938 | ||
939 | for (i=0; i < list->nb_elem; i++) { | |
940 | for (j=0; j < ARRAY_SIZE(metadata); j++){ | |
941 | if (!strcmp(tags[i].name, metadata[j].name)) { | |
942 | int *ptr = (int *)((char *)s + metadata[j].offset); | |
943 | if (*ptr) continue; | |
944 | if (metadata[j].size > sizeof(int)) | |
945 | av_strlcpy((char *)ptr, tags[i].string, metadata[j].size); | |
946 | else | |
947 | *ptr = atoi(tags[i].string); | |
948 | } | |
949 | } | |
950 | if (tags[i].sub.nb_elem) | |
951 | matroska_convert_tags(s, &tags[i].sub); | |
952 | } | |
953 | } | |
954 | ||
f7b9687c | 955 | static void matroska_execute_seekhead(MatroskaDemuxContext *matroska) |
13b350a3 AJ |
956 | { |
957 | EbmlList *seekhead_list = &matroska->seekhead; | |
958 | MatroskaSeekhead *seekhead = seekhead_list->elem; | |
13b350a3 AJ |
959 | uint32_t level_up = matroska->level_up; |
960 | offset_t before_pos = url_ftell(matroska->ctx->pb); | |
13b350a3 | 961 | MatroskaLevel level; |
f06a4886 | 962 | int i; |
b061d892 | 963 | |
13b350a3 | 964 | for (i=0; i<seekhead_list->nb_elem; i++) { |
66cfc385 AJ |
965 | offset_t offset = seekhead[i].pos + matroska->segment_start; |
966 | ||
13b350a3 AJ |
967 | if (seekhead[i].pos <= before_pos |
968 | || seekhead[i].id == MATROSKA_ID_SEEKHEAD | |
969 | || seekhead[i].id == MATROSKA_ID_CLUSTER) | |
970 | continue; | |
b061d892 | 971 | |
4348571b | 972 | /* seek */ |
66cfc385 | 973 | if (url_fseek(matroska->ctx->pb, offset, SEEK_SET) != offset) |
4348571b AJ |
974 | continue; |
975 | ||
5968d2dd | 976 | /* We don't want to lose our seekhead level, so we add |
4348571b AJ |
977 | * a dummy. This is a crude hack. */ |
978 | if (matroska->num_levels == EBML_MAX_DEPTH) { | |
979 | av_log(matroska->ctx, AV_LOG_INFO, | |
980 | "Max EBML element depth (%d) reached, " | |
981 | "cannot parse further.\n", EBML_MAX_DEPTH); | |
982 | break; | |
983 | } | |
984 | ||
985 | level.start = 0; | |
986 | level.length = (uint64_t)-1; | |
987 | matroska->levels[matroska->num_levels] = level; | |
988 | matroska->num_levels++; | |
989 | ||
66a37e06 | 990 | ebml_parse(matroska, matroska_segment, matroska); |
4348571b | 991 | |
4348571b AJ |
992 | /* remove dummy level */ |
993 | while (matroska->num_levels) { | |
994 | uint64_t length = matroska->levels[--matroska->num_levels].length; | |
995 | if (length == (uint64_t)-1) | |
996 | break; | |
997 | } | |
13b350a3 | 998 | } |
b061d892 | 999 | |
4348571b | 1000 | /* seek back */ |
66cfc385 | 1001 | url_fseek(matroska->ctx->pb, before_pos, SEEK_SET); |
4348571b | 1002 | matroska->level_up = level_up; |
b061d892 DC |
1003 | } |
1004 | ||
f7b9687c | 1005 | static int matroska_aac_profile(char *codec_id) |
b061d892 | 1006 | { |
ba18b99f | 1007 | static const char * const aac_profiles[] = { "MAIN", "LC", "SSR" }; |
b061d892 DC |
1008 | int profile; |
1009 | ||
1010 | for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++) | |
1011 | if (strstr(codec_id, aac_profiles[profile])) | |
1012 | break; | |
1013 | return profile + 1; | |
1014 | } | |
1015 | ||
f7b9687c | 1016 | static int matroska_aac_sri(int samplerate) |
b061d892 | 1017 | { |
b061d892 DC |
1018 | int sri; |
1019 | ||
7bfacd4e AJ |
1020 | for (sri=0; sri<ARRAY_SIZE(ff_mpeg4audio_sample_rates); sri++) |
1021 | if (ff_mpeg4audio_sample_rates[sri] == samplerate) | |
b061d892 DC |
1022 | break; |
1023 | return sri; | |
1024 | } | |
1025 | ||
f7b9687c | 1026 | static int matroska_read_header(AVFormatContext *s, AVFormatParameters *ap) |
b061d892 DC |
1027 | { |
1028 | MatroskaDemuxContext *matroska = s->priv_data; | |
9c25bafa AJ |
1029 | EbmlList *attachements_list = &matroska->attachments; |
1030 | MatroskaAttachement *attachements; | |
1031 | EbmlList *chapters_list = &matroska->chapters; | |
1032 | MatroskaChapter *chapters; | |
9a9a3b03 | 1033 | MatroskaTrack *tracks; |
e5929fdf AJ |
1034 | EbmlList *index_list; |
1035 | MatroskaIndex *index; | |
63511324 | 1036 | Ebml ebml = { 0 }; |
9a9a3b03 | 1037 | AVStream *st; |
ce6f28bd | 1038 | int i, j; |
b061d892 DC |
1039 | |
1040 | matroska->ctx = s; | |
1041 | ||
1042 | /* First read the EBML header. */ | |
c4d3d9ba | 1043 | if (ebml_parse(matroska, ebml_syntax, &ebml) |
63511324 AJ |
1044 | || ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t) |
1045 | || ebml.id_length > sizeof(uint32_t) || strcmp(ebml.doctype, "matroska") | |
1046 | || ebml.doctype_version > 2) { | |
b061d892 | 1047 | av_log(matroska->ctx, AV_LOG_ERROR, |
63511324 AJ |
1048 | "EBML header using unsupported features\n" |
1049 | "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n", | |
1050 | ebml.version, ebml.doctype, ebml.doctype_version); | |
b061d892 DC |
1051 | return AVERROR_NOFMT; |
1052 | } | |
63511324 | 1053 | ebml_free(ebml_syntax, &ebml); |
b061d892 DC |
1054 | |
1055 | /* The next thing is a segment. */ | |
c4d3d9ba | 1056 | if (ebml_parse(matroska, matroska_segments, matroska) < 0) |
ce6f28bd | 1057 | return -1; |
13b350a3 | 1058 | matroska_execute_seekhead(matroska); |
b061d892 | 1059 | |
9c25bafa AJ |
1060 | if (matroska->duration) |
1061 | matroska->ctx->duration = matroska->duration * matroska->time_scale | |
1062 | * 1000 / AV_TIME_BASE; | |
1063 | if (matroska->title) | |
1064 | strncpy(matroska->ctx->title, matroska->title, | |
1065 | sizeof(matroska->ctx->title)-1); | |
44015c56 | 1066 | matroska_convert_tags(s, &matroska->tags); |
9c25bafa | 1067 | |
d88d806b AJ |
1068 | tracks = matroska->tracks.elem; |
1069 | for (i=0; i < matroska->tracks.nb_elem; i++) { | |
1070 | MatroskaTrack *track = &tracks[i]; | |
1071 | enum CodecID codec_id = CODEC_ID_NONE; | |
9c25bafa AJ |
1072 | EbmlList *encodings_list = &tracks->encodings; |
1073 | MatroskaTrackEncoding *encodings = encodings_list->elem; | |
d88d806b AJ |
1074 | uint8_t *extradata = NULL; |
1075 | int extradata_size = 0; | |
1076 | int extradata_offset = 0; | |
1077 | ||
1078 | /* Apply some sanity checks. */ | |
9c25bafa AJ |
1079 | if (track->type != MATROSKA_TRACK_TYPE_VIDEO && |
1080 | track->type != MATROSKA_TRACK_TYPE_AUDIO && | |
1081 | track->type != MATROSKA_TRACK_TYPE_SUBTITLE) { | |
1082 | av_log(matroska->ctx, AV_LOG_INFO, | |
1083 | "Unknown or unsupported track type %"PRIu64"\n", | |
1084 | track->type); | |
1085 | continue; | |
1086 | } | |
d88d806b AJ |
1087 | if (track->codec_id == NULL) |
1088 | continue; | |
1089 | ||
9c25bafa AJ |
1090 | if (track->type == MATROSKA_TRACK_TYPE_VIDEO) { |
1091 | if (!track->default_duration) | |
1092 | track->default_duration = 1000000000/track->video.frame_rate; | |
1093 | if (!track->video.display_width) | |
1094 | track->video.display_width = track->video.pixel_width; | |
1095 | if (!track->video.display_height) | |
1096 | track->video.display_height = track->video.pixel_height; | |
1097 | } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) { | |
1098 | if (!track->audio.out_samplerate) | |
1099 | track->audio.out_samplerate = track->audio.samplerate; | |
1100 | } | |
1101 | if (encodings_list->nb_elem > 1) { | |
1102 | av_log(matroska->ctx, AV_LOG_ERROR, | |
1103 | "Multiple combined encodings no supported"); | |
1104 | } else if (encodings_list->nb_elem == 1) { | |
1105 | if (encodings[0].type || | |
1106 | (encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP && | |
1107 | #ifdef CONFIG_ZLIB | |
1108 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB && | |
1109 | #endif | |
1110 | #ifdef CONFIG_BZLIB | |
1111 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB && | |
1112 | #endif | |
1113 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO)) { | |
1114 | encodings[0].scope = 0; | |
1115 | av_log(matroska->ctx, AV_LOG_ERROR, | |
1116 | "Unsupported encoding type"); | |
1117 | } else if (track->codec_priv.size && encodings[0].scope&2) { | |
1118 | uint8_t *codec_priv = track->codec_priv.data; | |
1119 | int offset = matroska_decode_buffer(&track->codec_priv.data, | |
1120 | &track->codec_priv.size, | |
1121 | track); | |
1122 | if (offset < 0) { | |
1123 | track->codec_priv.data = NULL; | |
1124 | track->codec_priv.size = 0; | |
1125 | av_log(matroska->ctx, AV_LOG_ERROR, | |
1126 | "Failed to decode codec private data\n"); | |
1127 | } else if (offset > 0) { | |
1128 | track->codec_priv.data = av_malloc(track->codec_priv.size + offset); | |
1129 | memcpy(track->codec_priv.data, | |
1130 | encodings[0].compression.settings.data, offset); | |
1131 | memcpy(track->codec_priv.data+offset, codec_priv, | |
1132 | track->codec_priv.size); | |
1133 | track->codec_priv.size += offset; | |
1134 | } | |
1135 | if (codec_priv != track->codec_priv.data) | |
1136 | av_free(codec_priv); | |
1137 | } | |
1138 | } | |
1139 | ||
d88d806b AJ |
1140 | for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){ |
1141 | if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id, | |
1142 | strlen(ff_mkv_codec_tags[j].str))){ | |
1143 | codec_id= ff_mkv_codec_tags[j].id; | |
1144 | break; | |
b061d892 | 1145 | } |
d88d806b | 1146 | } |
b061d892 | 1147 | |
cc70d14b | 1148 | st = track->stream = av_new_stream(s, 0); |
d88d806b AJ |
1149 | if (st == NULL) |
1150 | return AVERROR(ENOMEM); | |
1151 | ||
cc8be506 | 1152 | if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC") |
28ba69e0 AJ |
1153 | && track->codec_priv.size >= 40 |
1154 | && track->codec_priv.data != NULL) { | |
d88d806b AJ |
1155 | track->video.fourcc = AV_RL32(track->codec_priv.data + 16); |
1156 | codec_id = codec_get_id(codec_bmp_tags, track->video.fourcc); | |
cc8be506 | 1157 | } else if (!strcmp(track->codec_id, "A_MS/ACM") |
28ba69e0 AJ |
1158 | && track->codec_priv.size >= 18 |
1159 | && track->codec_priv.data != NULL) { | |
d88d806b AJ |
1160 | uint16_t tag = AV_RL16(track->codec_priv.data); |
1161 | codec_id = codec_get_id(codec_wav_tags, tag); | |
28ba69e0 AJ |
1162 | } else if (!strcmp(track->codec_id, "V_QUICKTIME") |
1163 | && (track->codec_priv.size >= 86) | |
1164 | && (track->codec_priv.data != NULL)) { | |
d88d806b AJ |
1165 | track->video.fourcc = AV_RL32(track->codec_priv.data); |
1166 | codec_id=codec_get_id(codec_movvideo_tags, track->video.fourcc); | |
eb9cf50a AJ |
1167 | } else if (codec_id == CODEC_ID_PCM_S16BE) { |
1168 | switch (track->audio.bitdepth) { | |
1169 | case 8: codec_id = CODEC_ID_PCM_U8; break; | |
1170 | case 24: codec_id = CODEC_ID_PCM_S24BE; break; | |
1171 | case 32: codec_id = CODEC_ID_PCM_S32BE; break; | |
1172 | } | |
1173 | } else if (codec_id == CODEC_ID_PCM_S16LE) { | |
1174 | switch (track->audio.bitdepth) { | |
1175 | case 8: codec_id = CODEC_ID_PCM_U8; break; | |
1176 | case 24: codec_id = CODEC_ID_PCM_S24LE; break; | |
1177 | case 32: codec_id = CODEC_ID_PCM_S32LE; break; | |
1178 | } | |
1179 | } else if (codec_id==CODEC_ID_PCM_F32LE && track->audio.bitdepth==64) { | |
1180 | codec_id = CODEC_ID_PCM_F64LE; | |
28ba69e0 | 1181 | } else if (codec_id == CODEC_ID_AAC && !track->codec_priv.size) { |
d88d806b AJ |
1182 | int profile = matroska_aac_profile(track->codec_id); |
1183 | int sri = matroska_aac_sri(track->audio.samplerate); | |
1184 | extradata = av_malloc(5); | |
1185 | if (extradata == NULL) | |
1186 | return AVERROR(ENOMEM); | |
1187 | extradata[0] = (profile << 3) | ((sri&0x0E) >> 1); | |
1188 | extradata[1] = ((sri&0x01) << 7) | (track->audio.channels<<3); | |
1189 | if (strstr(track->codec_id, "SBR")) { | |
1190 | sri = matroska_aac_sri(track->audio.out_samplerate); | |
1191 | extradata[2] = 0x56; | |
1192 | extradata[3] = 0xE5; | |
1193 | extradata[4] = 0x80 | (sri<<3); | |
1194 | extradata_size = 5; | |
16f97ab0 | 1195 | } else |
d88d806b | 1196 | extradata_size = 2; |
28ba69e0 | 1197 | } else if (codec_id == CODEC_ID_TTA) { |
d88d806b AJ |
1198 | ByteIOContext b; |
1199 | extradata_size = 30; | |
1200 | extradata = av_mallocz(extradata_size); | |
1201 | if (extradata == NULL) | |
1202 | return AVERROR(ENOMEM); | |
1203 | init_put_byte(&b, extradata, extradata_size, 1, | |
1204 | NULL, NULL, NULL, NULL); | |
1205 | put_buffer(&b, "TTA1", 4); | |
1206 | put_le16(&b, 1); | |
1207 | put_le16(&b, track->audio.channels); | |
1208 | put_le16(&b, track->audio.bitdepth); | |
1209 | put_le32(&b, track->audio.out_samplerate); | |
1210 | put_le32(&b, matroska->ctx->duration * track->audio.out_samplerate); | |
28ba69e0 AJ |
1211 | } else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 || |
1212 | codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) { | |
d88d806b AJ |
1213 | extradata_offset = 26; |
1214 | track->codec_priv.size -= extradata_offset; | |
28ba69e0 | 1215 | } else if (codec_id == CODEC_ID_RA_144) { |
d88d806b AJ |
1216 | track->audio.out_samplerate = 8000; |
1217 | track->audio.channels = 1; | |
28ba69e0 AJ |
1218 | } else if (codec_id == CODEC_ID_RA_288 || codec_id == CODEC_ID_COOK || |
1219 | codec_id == CODEC_ID_ATRAC3) { | |
d88d806b AJ |
1220 | ByteIOContext b; |
1221 | ||
1222 | init_put_byte(&b, track->codec_priv.data,track->codec_priv.size, | |
1223 | 0, NULL, NULL, NULL, NULL); | |
1224 | url_fskip(&b, 24); | |
1225 | track->audio.coded_framesize = get_be32(&b); | |
1226 | url_fskip(&b, 12); | |
1227 | track->audio.sub_packet_h = get_be16(&b); | |
1228 | track->audio.frame_size = get_be16(&b); | |
1229 | track->audio.sub_packet_size = get_be16(&b); | |
1230 | track->audio.buf = av_malloc(track->audio.frame_size * track->audio.sub_packet_h); | |
1231 | if (codec_id == CODEC_ID_RA_288) { | |
1232 | st->codec->block_align = track->audio.coded_framesize; | |
1233 | track->codec_priv.size = 0; | |
1234 | } else { | |
1235 | st->codec->block_align = track->audio.sub_packet_size; | |
1236 | extradata_offset = 78; | |
2cbc8811 | 1237 | track->codec_priv.size -= extradata_offset; |
b061d892 | 1238 | } |
d88d806b | 1239 | } |
b061d892 | 1240 | |
16f97ab0 | 1241 | if (codec_id == CODEC_ID_NONE) |
d88d806b | 1242 | av_log(matroska->ctx, AV_LOG_INFO, |
8f35a2c0 | 1243 | "Unknown/unsupported CodecID %s.\n", track->codec_id); |
eabb8ba4 | 1244 | |
d88d806b | 1245 | av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */ |
eabb8ba4 | 1246 | |
d88d806b AJ |
1247 | st->codec->codec_id = codec_id; |
1248 | st->start_time = 0; | |
1249 | if (strcmp(track->language, "und")) | |
1250 | av_strlcpy(st->language, track->language, 4); | |
b061d892 | 1251 | |
d88d806b AJ |
1252 | if (track->flag_default) |
1253 | st->disposition |= AV_DISPOSITION_DEFAULT; | |
b061d892 | 1254 | |
d88d806b AJ |
1255 | if (track->default_duration) |
1256 | av_reduce(&st->codec->time_base.num, &st->codec->time_base.den, | |
1257 | track->default_duration, 1000000000, 30000); | |
b061d892 | 1258 | |
d88d806b AJ |
1259 | if(extradata){ |
1260 | st->codec->extradata = extradata; | |
1261 | st->codec->extradata_size = extradata_size; | |
1262 | } else if(track->codec_priv.data && track->codec_priv.size > 0){ | |
1263 | st->codec->extradata = av_malloc(track->codec_priv.size); | |
1264 | if(st->codec->extradata == NULL) | |
1265 | return AVERROR(ENOMEM); | |
1266 | st->codec->extradata_size = track->codec_priv.size; | |
1267 | memcpy(st->codec->extradata, | |
1268 | track->codec_priv.data + extradata_offset, | |
1269 | track->codec_priv.size); | |
b061d892 | 1270 | } |
d88d806b AJ |
1271 | |
1272 | if (track->type == MATROSKA_TRACK_TYPE_VIDEO) { | |
1273 | st->codec->codec_type = CODEC_TYPE_VIDEO; | |
1274 | st->codec->codec_tag = track->video.fourcc; | |
1275 | st->codec->width = track->video.pixel_width; | |
1276 | st->codec->height = track->video.pixel_height; | |
59729451 AJ |
1277 | av_reduce(&st->sample_aspect_ratio.num, |
1278 | &st->sample_aspect_ratio.den, | |
d88d806b AJ |
1279 | st->codec->height * track->video.display_width, |
1280 | st->codec-> width * track->video.display_height, | |
1281 | 255); | |
1282 | st->need_parsing = AVSTREAM_PARSE_HEADERS; | |
1283 | } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) { | |
1284 | st->codec->codec_type = CODEC_TYPE_AUDIO; | |
1285 | st->codec->sample_rate = track->audio.out_samplerate; | |
1286 | st->codec->channels = track->audio.channels; | |
1287 | } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) { | |
1288 | st->codec->codec_type = CODEC_TYPE_SUBTITLE; | |
1289 | } | |
b061d892 DC |
1290 | } |
1291 | ||
9c25bafa AJ |
1292 | attachements = attachements_list->elem; |
1293 | for (j=0; j<attachements_list->nb_elem; j++) { | |
1294 | if (!(attachements[j].filename && attachements[j].mime && | |
1295 | attachements[j].bin.data && attachements[j].bin.size > 0)) { | |
1296 | av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n"); | |
1297 | } else { | |
cc70d14b | 1298 | AVStream *st = av_new_stream(s, 0); |
9c25bafa AJ |
1299 | if (st == NULL) |
1300 | break; | |
1301 | st->filename = av_strdup(attachements[j].filename); | |
1302 | st->codec->codec_id = CODEC_ID_NONE; | |
1303 | st->codec->codec_type = CODEC_TYPE_ATTACHMENT; | |
1304 | st->codec->extradata = av_malloc(attachements[j].bin.size); | |
1305 | if(st->codec->extradata == NULL) | |
1306 | break; | |
1307 | st->codec->extradata_size = attachements[j].bin.size; | |
1308 | memcpy(st->codec->extradata, attachements[j].bin.data, attachements[j].bin.size); | |
1309 | ||
1310 | for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) { | |
1311 | if (!strncmp(ff_mkv_mime_tags[i].str, attachements[j].mime, | |
1312 | strlen(ff_mkv_mime_tags[i].str))) { | |
1313 | st->codec->codec_id = ff_mkv_mime_tags[i].id; | |
1314 | break; | |
1315 | } | |
1316 | } | |
1317 | } | |
1318 | } | |
1319 | ||
1320 | chapters = chapters_list->elem; | |
1321 | for (i=0; i<chapters_list->nb_elem; i++) | |
1322 | if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid) | |
1323 | ff_new_chapter(s, chapters[i].uid, (AVRational){1, 1000000000}, | |
1324 | chapters[i].start, chapters[i].end, | |
1325 | chapters[i].title); | |
1326 | ||
e5929fdf AJ |
1327 | index_list = &matroska->index; |
1328 | index = index_list->elem; | |
1329 | for (i=0; i<index_list->nb_elem; i++) { | |
1330 | EbmlList *pos_list = &index[i].pos; | |
1331 | MatroskaIndexPos *pos = pos_list->elem; | |
1332 | for (j=0; j<pos_list->nb_elem; j++) { | |
009ecd50 | 1333 | MatroskaTrack *track = matroska_find_track_by_num(matroska, |
e5929fdf | 1334 | pos[j].track); |
009ecd50 AJ |
1335 | if (track && track->stream) |
1336 | av_add_index_entry(track->stream, | |
e5929fdf AJ |
1337 | pos[j].pos + matroska->segment_start, |
1338 | index[i].time*matroska->time_scale/AV_TIME_BASE, | |
ffaa3ecb | 1339 | 0, 0, AVINDEX_KEYFRAME); |
b061d892 DC |
1340 | } |
1341 | } | |
1342 | ||
ce6f28bd | 1343 | return 0; |
b061d892 DC |
1344 | } |
1345 | ||
737c40da | 1346 | /* |
737c40da AJ |
1347 | * Put one packet in an application-supplied AVPacket struct. |
1348 | * Returns 0 on success or -1 on failure. | |
1349 | */ | |
1350 | static int matroska_deliver_packet(MatroskaDemuxContext *matroska, | |
1351 | AVPacket *pkt) | |
1352 | { | |
1353 | if (matroska->num_packets > 0) { | |
1354 | memcpy(pkt, matroska->packets[0], sizeof(AVPacket)); | |
1355 | av_free(matroska->packets[0]); | |
1356 | if (matroska->num_packets > 1) { | |
1357 | memmove(&matroska->packets[0], &matroska->packets[1], | |
1358 | (matroska->num_packets - 1) * sizeof(AVPacket *)); | |
1359 | matroska->packets = | |
1360 | av_realloc(matroska->packets, (matroska->num_packets - 1) * | |
1361 | sizeof(AVPacket *)); | |
1362 | } else { | |
1363 | av_freep(&matroska->packets); | |
1364 | } | |
1365 | matroska->num_packets--; | |
1366 | return 0; | |
1367 | } | |
1368 | ||
1369 | return -1; | |
1370 | } | |
1371 | ||
1372 | /* | |
1373 | * Free all packets in our internal queue. | |
1374 | */ | |
1375 | static void matroska_clear_queue(MatroskaDemuxContext *matroska) | |
1376 | { | |
1377 | if (matroska->packets) { | |
1378 | int n; | |
1379 | for (n = 0; n < matroska->num_packets; n++) { | |
1380 | av_free_packet(matroska->packets[n]); | |
1381 | av_free(matroska->packets[n]); | |
1382 | } | |
00a3431c | 1383 | av_freep(&matroska->packets); |
737c40da AJ |
1384 | matroska->num_packets = 0; |
1385 | } | |
1386 | } | |
1387 | ||
f7b9687c AJ |
1388 | static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, |
1389 | int size, int64_t pos, uint64_t cluster_time, | |
1390 | uint64_t duration, int is_keyframe) | |
b061d892 | 1391 | { |
009ecd50 | 1392 | MatroskaTrack *track; |
b061d892 | 1393 | int res = 0; |
b061d892 DC |
1394 | AVStream *st; |
1395 | AVPacket *pkt; | |
b061d892 DC |
1396 | int16_t block_time; |
1397 | uint32_t *lace_size = NULL; | |
1398 | int n, flags, laces = 0; | |
1399 | uint64_t num; | |
1400 | ||
c1e01133 | 1401 | if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) { |
b061d892 | 1402 | av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n"); |
b061d892 DC |
1403 | return res; |
1404 | } | |
1405 | data += n; | |
1406 | size -= n; | |
1407 | ||
b061d892 | 1408 | track = matroska_find_track_by_num(matroska, num); |
009ecd50 | 1409 | if (size <= 3 || !track || !track->stream) { |
b061d892 | 1410 | av_log(matroska->ctx, AV_LOG_INFO, |
009ecd50 | 1411 | "Invalid stream %"PRIu64" or size %u\n", num, size); |
b061d892 DC |
1412 | return res; |
1413 | } | |
009ecd50 | 1414 | st = track->stream; |
16f97ab0 | 1415 | if (st->discard >= AVDISCARD_ALL) |
b061d892 | 1416 | return res; |
b061d892 | 1417 | if (duration == AV_NOPTS_VALUE) |
009ecd50 | 1418 | duration = track->default_duration / matroska->time_scale; |
b061d892 | 1419 | |
2ce746c6 | 1420 | block_time = AV_RB16(data); |
b061d892 | 1421 | data += 2; |
1607c534 AJ |
1422 | flags = *data++; |
1423 | size -= 3; | |
b061d892 | 1424 | if (is_keyframe == -1) |
84fa6e23 | 1425 | is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0; |
b061d892 DC |
1426 | |
1427 | if (matroska->skip_to_keyframe) { | |
16f97ab0 | 1428 | if (!is_keyframe || st != matroska->skip_to_stream) |
b061d892 DC |
1429 | return res; |
1430 | matroska->skip_to_keyframe = 0; | |
1431 | } | |
1432 | ||
1433 | switch ((flags & 0x06) >> 1) { | |
1434 | case 0x0: /* no lacing */ | |
1435 | laces = 1; | |
1436 | lace_size = av_mallocz(sizeof(int)); | |
1437 | lace_size[0] = size; | |
1438 | break; | |
1439 | ||
5968d2dd | 1440 | case 0x1: /* Xiph lacing */ |
b061d892 DC |
1441 | case 0x2: /* fixed-size lacing */ |
1442 | case 0x3: /* EBML lacing */ | |
9bf8b562 | 1443 | assert(size>0); // size <=3 is checked before size-=3 above |
b061d892 DC |
1444 | laces = (*data) + 1; |
1445 | data += 1; | |
1446 | size -= 1; | |
1447 | lace_size = av_mallocz(laces * sizeof(int)); | |
1448 | ||
1449 | switch ((flags & 0x06) >> 1) { | |
5968d2dd | 1450 | case 0x1: /* Xiph lacing */ { |
b061d892 DC |
1451 | uint8_t temp; |
1452 | uint32_t total = 0; | |
1453 | for (n = 0; res == 0 && n < laces - 1; n++) { | |
1454 | while (1) { | |
1455 | if (size == 0) { | |
1456 | res = -1; | |
1457 | break; | |
1458 | } | |
1459 | temp = *data; | |
1460 | lace_size[n] += temp; | |
1461 | data += 1; | |
1462 | size -= 1; | |
1463 | if (temp != 0xff) | |
1464 | break; | |
1465 | } | |
1466 | total += lace_size[n]; | |
1467 | } | |
1468 | lace_size[n] = size - total; | |
1469 | break; | |
1470 | } | |
1471 | ||
1472 | case 0x2: /* fixed-size lacing */ | |
1473 | for (n = 0; n < laces; n++) | |
1474 | lace_size[n] = size / laces; | |
1475 | break; | |
1476 | ||
1477 | case 0x3: /* EBML lacing */ { | |
1478 | uint32_t total; | |
c1e01133 | 1479 | n = matroska_ebmlnum_uint(matroska, data, size, &num); |
b061d892 DC |
1480 | if (n < 0) { |
1481 | av_log(matroska->ctx, AV_LOG_INFO, | |
1482 | "EBML block data error\n"); | |
1483 | break; | |
1484 | } | |
1485 | data += n; | |
1486 | size -= n; | |
1487 | total = lace_size[0] = num; | |
1488 | for (n = 1; res == 0 && n < laces - 1; n++) { | |
1489 | int64_t snum; | |
1490 | int r; | |
c1e01133 | 1491 | r = matroska_ebmlnum_sint(matroska, data, size, &snum); |
b061d892 DC |
1492 | if (r < 0) { |
1493 | av_log(matroska->ctx, AV_LOG_INFO, | |
1494 | "EBML block data error\n"); | |
1495 | break; | |
1496 | } | |
1497 | data += r; | |
1498 | size -= r; | |
1499 | lace_size[n] = lace_size[n - 1] + snum; | |
1500 | total += lace_size[n]; | |
1501 | } | |
1502 | lace_size[n] = size - total; | |
1503 | break; | |
1504 | } | |
1505 | } | |
1506 | break; | |
1507 | } | |
1508 | ||
1509 | if (res == 0) { | |
b061d892 DC |
1510 | uint64_t timecode = AV_NOPTS_VALUE; |
1511 | ||
9c3e2f78 AJ |
1512 | if (cluster_time != (uint64_t)-1 |
1513 | && (block_time >= 0 || cluster_time >= -block_time)) | |
b061d892 DC |
1514 | timecode = cluster_time + block_time; |
1515 | ||
1516 | for (n = 0; n < laces; n++) { | |
ba8a76b8 AJ |
1517 | if (st->codec->codec_id == CODEC_ID_RA_288 || |
1518 | st->codec->codec_id == CODEC_ID_COOK || | |
1519 | st->codec->codec_id == CODEC_ID_ATRAC3) { | |
ba8a76b8 | 1520 | int a = st->codec->block_align; |
2cbc8811 AJ |
1521 | int sps = track->audio.sub_packet_size; |
1522 | int cfs = track->audio.coded_framesize; | |
1523 | int h = track->audio.sub_packet_h; | |
1524 | int y = track->audio.sub_packet_cnt; | |
1525 | int w = track->audio.frame_size; | |
ba8a76b8 AJ |
1526 | int x; |
1527 | ||
2cbc8811 | 1528 | if (!track->audio.pkt_cnt) { |
ba8a76b8 AJ |
1529 | if (st->codec->codec_id == CODEC_ID_RA_288) |
1530 | for (x=0; x<h/2; x++) | |
2cbc8811 | 1531 | memcpy(track->audio.buf+x*2*w+y*cfs, |
ba8a76b8 AJ |
1532 | data+x*cfs, cfs); |
1533 | else | |
1534 | for (x=0; x<w/sps; x++) | |
2cbc8811 | 1535 | memcpy(track->audio.buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps); |
ba8a76b8 | 1536 | |
2cbc8811 AJ |
1537 | if (++track->audio.sub_packet_cnt >= h) { |
1538 | track->audio.sub_packet_cnt = 0; | |
1539 | track->audio.pkt_cnt = h*w / a; | |
979c0910 | 1540 | } |
ba8a76b8 | 1541 | } |
2cbc8811 | 1542 | while (track->audio.pkt_cnt) { |
77abe5e8 | 1543 | pkt = av_mallocz(sizeof(AVPacket)); |
ba8a76b8 | 1544 | av_new_packet(pkt, a); |
2cbc8811 AJ |
1545 | memcpy(pkt->data, track->audio.buf |
1546 | + a * (h*w / a - track->audio.pkt_cnt--), a); | |
77abe5e8 | 1547 | pkt->pos = pos; |
fc4d335f | 1548 | pkt->stream_index = st->index; |
b8702539 | 1549 | dynarray_add(&matroska->packets,&matroska->num_packets,pkt); |
eabb8ba4 | 1550 | } |
ba8a76b8 | 1551 | } else { |
2cbc8811 | 1552 | MatroskaTrackEncoding *encodings = track->encodings.elem; |
935ec5a1 | 1553 | int offset = 0, pkt_size = lace_size[n]; |
de3230fb | 1554 | uint8_t *pkt_data = data; |
ba8a76b8 | 1555 | |
2cbc8811 | 1556 | if (encodings && encodings->scope & 1) { |
8f35a2c0 | 1557 | offset = matroska_decode_buffer(&pkt_data,&pkt_size, track); |
935ec5a1 ES |
1558 | if (offset < 0) |
1559 | continue; | |
53a1e82b AJ |
1560 | } |
1561 | ||
ba8a76b8 AJ |
1562 | pkt = av_mallocz(sizeof(AVPacket)); |
1563 | /* XXX: prevent data copy... */ | |
de3230fb | 1564 | if (av_new_packet(pkt, pkt_size+offset) < 0) { |
34ae4097 | 1565 | av_free(pkt); |
ba8a76b8 AJ |
1566 | res = AVERROR(ENOMEM); |
1567 | n = laces-1; | |
1568 | break; | |
1569 | } | |
53a1e82b | 1570 | if (offset) |
2cbc8811 | 1571 | memcpy (pkt->data, encodings->compression.settings.data, offset); |
de3230fb | 1572 | memcpy (pkt->data+offset, pkt_data, pkt_size); |
ba8a76b8 | 1573 | |
51e1cc16 AJ |
1574 | if (pkt_data != data) |
1575 | av_free(pkt_data); | |
1576 | ||
ba8a76b8 AJ |
1577 | if (n == 0) |
1578 | pkt->flags = is_keyframe; | |
fc4d335f | 1579 | pkt->stream_index = st->index; |
ba8a76b8 AJ |
1580 | |
1581 | pkt->pts = timecode; | |
1582 | pkt->pos = pos; | |
1583 | pkt->duration = duration; | |
1584 | ||
b8702539 | 1585 | dynarray_add(&matroska->packets, &matroska->num_packets, pkt); |
ba8a76b8 | 1586 | } |
b061d892 | 1587 | |
ba8a76b8 AJ |
1588 | if (timecode != AV_NOPTS_VALUE) |
1589 | timecode = duration ? timecode + duration : AV_NOPTS_VALUE; | |
b061d892 DC |
1590 | data += lace_size[n]; |
1591 | } | |
1592 | } | |
1593 | ||
1594 | av_free(lace_size); | |
b061d892 DC |
1595 | return res; |
1596 | } | |
1597 | ||
f7b9687c | 1598 | static int matroska_parse_cluster(MatroskaDemuxContext *matroska) |
b061d892 | 1599 | { |
209472b4 AJ |
1600 | MatroskaCluster cluster = { 0 }; |
1601 | EbmlList *blocks_list; | |
1602 | MatroskaBlock *blocks; | |
38797638 AJ |
1603 | int i, res; |
1604 | if (matroska->has_cluster_id){ | |
5968d2dd | 1605 | /* For the first cluster we parse, its ID was already read as |
38797638 AJ |
1606 | part of matroska_read_header(), so don't read it again */ |
1607 | res = ebml_parse_id(matroska, matroska_clusters, | |
1608 | MATROSKA_ID_CLUSTER, &cluster); | |
1609 | matroska->has_cluster_id = 0; | |
1610 | } else | |
1611 | res = ebml_parse(matroska, matroska_clusters, &cluster); | |
209472b4 AJ |
1612 | blocks_list = &cluster.blocks; |
1613 | blocks = blocks_list->elem; | |
131f1cb2 | 1614 | for (i=0; i<blocks_list->nb_elem; i++) |
209472b4 AJ |
1615 | if (blocks[i].bin.size > 0) |
1616 | res=matroska_parse_block(matroska, | |
1617 | blocks[i].bin.data, blocks[i].bin.size, | |
1618 | blocks[i].bin.pos, cluster.timecode, | |
1619 | blocks[i].duration, !blocks[i].reference); | |
1620 | ebml_free(matroska_cluster, &cluster); | |
b061d892 DC |
1621 | return res; |
1622 | } | |
1623 | ||
f7b9687c | 1624 | static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt) |
b061d892 DC |
1625 | { |
1626 | MatroskaDemuxContext *matroska = s->priv_data; | |
b061d892 | 1627 | |
b061d892 | 1628 | while (matroska_deliver_packet(matroska, pkt)) { |
b061d892 | 1629 | if (matroska->done) |
6f3e0b21 | 1630 | return AVERROR(EIO); |
209472b4 | 1631 | if (matroska_parse_cluster(matroska) < 0) |
b061d892 DC |
1632 | matroska->done = 1; |
1633 | } | |
1634 | ||
1635 | return 0; | |
1636 | } | |
1637 | ||
f7b9687c AJ |
1638 | static int matroska_read_seek(AVFormatContext *s, int stream_index, |
1639 | int64_t timestamp, int flags) | |
b061d892 DC |
1640 | { |
1641 | MatroskaDemuxContext *matroska = s->priv_data; | |
1642 | AVStream *st = s->streams[stream_index]; | |
1643 | int index; | |
1644 | ||
b061d892 DC |
1645 | index = av_index_search_timestamp(st, timestamp, flags); |
1646 | if (index < 0) | |
1647 | return 0; | |
1648 | ||
243cc4c3 AJ |
1649 | matroska_clear_queue(matroska); |
1650 | ||
899681cd | 1651 | url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET); |
b061d892 DC |
1652 | matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY); |
1653 | matroska->skip_to_stream = st; | |
de6a9a26 | 1654 | av_update_cur_dts(s, st, st->index_entries[index].timestamp); |
b061d892 DC |
1655 | return 0; |
1656 | } | |
1657 | ||
f7b9687c | 1658 | static int matroska_read_close(AVFormatContext *s) |
b061d892 DC |
1659 | { |
1660 | MatroskaDemuxContext *matroska = s->priv_data; | |
2cbc8811 | 1661 | MatroskaTrack *tracks = matroska->tracks.elem; |
70109c0d | 1662 | int n; |
b061d892 | 1663 | |
34c9c1ba | 1664 | matroska_clear_queue(matroska); |
b061d892 | 1665 | |
2cbc8811 AJ |
1666 | for (n=0; n < matroska->tracks.nb_elem; n++) |
1667 | if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO) | |
1668 | av_free(tracks[n].audio.buf); | |
ce6f28bd | 1669 | ebml_free(matroska_segment, matroska); |
b061d892 DC |
1670 | |
1671 | return 0; | |
1672 | } | |
1673 | ||
1674 | AVInputFormat matroska_demuxer = { | |
1675 | "matroska", | |
bde15e74 | 1676 | NULL_IF_CONFIG_SMALL("Matroska file format"), |
b061d892 DC |
1677 | sizeof(MatroskaDemuxContext), |
1678 | matroska_probe, | |
1679 | matroska_read_header, | |
1680 | matroska_read_packet, | |
1681 | matroska_read_close, | |
1682 | matroska_read_seek, | |
1683 | }; |