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