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