Statistics
| Branch: | Revision:

ffmpeg / libavformat / matroskadec.c @ 194d4b49

History | View | Annotate | Download (65.6 KB)

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
} MatroskaTrack;
147

    
148
typedef struct {
149
    uint64_t uid;
150
    char *filename;
151
    char *mime;
152
    EbmlBin bin;
153

    
154
    AVStream *stream;
155
} MatroskaAttachement;
156

    
157
typedef struct {
158
    uint64_t start;
159
    uint64_t end;
160
    uint64_t uid;
161
    char    *title;
162

    
163
    AVChapter *chapter;
164
} MatroskaChapter;
165

    
166
typedef struct {
167
    uint64_t track;
168
    uint64_t pos;
169
} MatroskaIndexPos;
170

    
171
typedef struct {
172
    uint64_t time;
173
    EbmlList pos;
174
} MatroskaIndex;
175

    
176
typedef struct {
177
    char *name;
178
    char *string;
179
    char *lang;
180
    uint64_t def;
181
    EbmlList sub;
182
} MatroskaTag;
183

    
184
typedef struct {
185
    char    *type;
186
    uint64_t typevalue;
187
    uint64_t trackuid;
188
    uint64_t chapteruid;
189
    uint64_t attachuid;
190
} MatroskaTagTarget;
191

    
192
typedef struct {
193
    MatroskaTagTarget target;
194
    EbmlList tag;
195
} MatroskaTags;
196

    
197
typedef struct {
198
    uint64_t id;
199
    uint64_t pos;
200
} MatroskaSeekhead;
201

    
202
typedef struct {
203
    uint64_t start;
204
    uint64_t length;
205
} MatroskaLevel;
206

    
207
typedef struct {
208
    AVFormatContext *ctx;
209

    
210
    /* EBML stuff */
211
    int num_levels;
212
    MatroskaLevel levels[EBML_MAX_DEPTH];
213
    int level_up;
214

    
215
    uint64_t time_scale;
216
    double   duration;
217
    char    *title;
218
    EbmlList tracks;
219
    EbmlList attachments;
220
    EbmlList chapters;
221
    EbmlList index;
222
    EbmlList tags;
223
    EbmlList seekhead;
224

    
225
    /* byte position of the segment inside the stream */
226
    int64_t segment_start;
227

    
228
    /* the packet queue */
229
    AVPacket **packets;
230
    int num_packets;
231
    AVPacket *prev_pkt;
232

    
233
    int done;
234
    int has_cluster_id;
235

    
236
    /* What to skip before effectively reading a packet. */
237
    int skip_to_keyframe;
238
    uint64_t skip_to_timecode;
239
} MatroskaDemuxContext;
240

    
241
typedef struct {
242
    uint64_t duration;
243
    int64_t  reference;
244
    uint64_t non_simple;
245
    EbmlBin  bin;
246
} MatroskaBlock;
247

    
248
typedef struct {
249
    uint64_t timecode;
250
    EbmlList blocks;
251
} MatroskaCluster;
252

    
253
static EbmlSyntax ebml_header[] = {
254
    { EBML_ID_EBMLREADVERSION,        EBML_UINT, 0, offsetof(Ebml,version), {.u=EBML_VERSION} },
255
    { EBML_ID_EBMLMAXSIZELENGTH,      EBML_UINT, 0, offsetof(Ebml,max_size), {.u=8} },
256
    { EBML_ID_EBMLMAXIDLENGTH,        EBML_UINT, 0, offsetof(Ebml,id_length), {.u=4} },
257
    { EBML_ID_DOCTYPE,                EBML_STR,  0, offsetof(Ebml,doctype), {.s="(none)"} },
258
    { EBML_ID_DOCTYPEREADVERSION,     EBML_UINT, 0, offsetof(Ebml,doctype_version), {.u=1} },
259
    { EBML_ID_EBMLVERSION,            EBML_NONE },
260
    { EBML_ID_DOCTYPEVERSION,         EBML_NONE },
261
    { 0 }
262
};
263

    
264
static EbmlSyntax ebml_syntax[] = {
265
    { EBML_ID_HEADER,                 EBML_NEST, 0, 0, {.n=ebml_header} },
266
    { 0 }
267
};
268

    
269
static EbmlSyntax matroska_info[] = {
270
    { MATROSKA_ID_TIMECODESCALE,      EBML_UINT,  0, offsetof(MatroskaDemuxContext,time_scale), {.u=1000000} },
271
    { MATROSKA_ID_DURATION,           EBML_FLOAT, 0, offsetof(MatroskaDemuxContext,duration) },
272
    { MATROSKA_ID_TITLE,              EBML_UTF8,  0, offsetof(MatroskaDemuxContext,title) },
273
    { MATROSKA_ID_WRITINGAPP,         EBML_NONE },
274
    { MATROSKA_ID_MUXINGAPP,          EBML_NONE },
275
    { MATROSKA_ID_DATEUTC,            EBML_NONE },
276
    { MATROSKA_ID_SEGMENTUID,         EBML_NONE },
277
    { 0 }
278
};
279

    
280
static EbmlSyntax matroska_track_video[] = {
281
    { MATROSKA_ID_VIDEOFRAMERATE,     EBML_FLOAT,0, offsetof(MatroskaTrackVideo,frame_rate) },
282
    { MATROSKA_ID_VIDEODISPLAYWIDTH,  EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_width) },
283
    { MATROSKA_ID_VIDEODISPLAYHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_height) },
284
    { MATROSKA_ID_VIDEOPIXELWIDTH,    EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_width) },
285
    { MATROSKA_ID_VIDEOPIXELHEIGHT,   EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_height) },
286
    { MATROSKA_ID_VIDEOCOLORSPACE,    EBML_UINT, 0, offsetof(MatroskaTrackVideo,fourcc) },
287
    { MATROSKA_ID_VIDEOPIXELCROPB,    EBML_NONE },
288
    { MATROSKA_ID_VIDEOPIXELCROPT,    EBML_NONE },
289
    { MATROSKA_ID_VIDEOPIXELCROPL,    EBML_NONE },
290
    { MATROSKA_ID_VIDEOPIXELCROPR,    EBML_NONE },
291
    { MATROSKA_ID_VIDEODISPLAYUNIT,   EBML_NONE },
292
    { MATROSKA_ID_VIDEOFLAGINTERLACED,EBML_NONE },
293
    { MATROSKA_ID_VIDEOSTEREOMODE,    EBML_NONE },
294
    { MATROSKA_ID_VIDEOASPECTRATIO,   EBML_NONE },
295
    { 0 }
296
};
297

    
298
static EbmlSyntax matroska_track_audio[] = {
299
    { MATROSKA_ID_AUDIOSAMPLINGFREQ,  EBML_FLOAT,0, offsetof(MatroskaTrackAudio,samplerate), {.f=8000.0} },
300
    { MATROSKA_ID_AUDIOOUTSAMPLINGFREQ,EBML_FLOAT,0,offsetof(MatroskaTrackAudio,out_samplerate) },
301
    { MATROSKA_ID_AUDIOBITDEPTH,      EBML_UINT, 0, offsetof(MatroskaTrackAudio,bitdepth) },
302
    { MATROSKA_ID_AUDIOCHANNELS,      EBML_UINT, 0, offsetof(MatroskaTrackAudio,channels), {.u=1} },
303
    { 0 }
304
};
305

    
306
static EbmlSyntax matroska_track_encoding_compression[] = {
307
    { MATROSKA_ID_ENCODINGCOMPALGO,   EBML_UINT, 0, offsetof(MatroskaTrackCompression,algo), {.u=0} },
308
    { MATROSKA_ID_ENCODINGCOMPSETTINGS,EBML_BIN, 0, offsetof(MatroskaTrackCompression,settings) },
309
    { 0 }
310
};
311

    
312
static EbmlSyntax matroska_track_encoding[] = {
313
    { MATROSKA_ID_ENCODINGSCOPE,      EBML_UINT, 0, offsetof(MatroskaTrackEncoding,scope), {.u=1} },
314
    { MATROSKA_ID_ENCODINGTYPE,       EBML_UINT, 0, offsetof(MatroskaTrackEncoding,type), {.u=0} },
315
    { MATROSKA_ID_ENCODINGCOMPRESSION,EBML_NEST, 0, offsetof(MatroskaTrackEncoding,compression), {.n=matroska_track_encoding_compression} },
316
    { MATROSKA_ID_ENCODINGORDER,      EBML_NONE },
317
    { 0 }
318
};
319

    
320
static EbmlSyntax matroska_track_encodings[] = {
321
    { MATROSKA_ID_TRACKCONTENTENCODING, EBML_NEST, sizeof(MatroskaTrackEncoding), offsetof(MatroskaTrack,encodings), {.n=matroska_track_encoding} },
322
    { 0 }
323
};
324

    
325
static EbmlSyntax matroska_track[] = {
326
    { MATROSKA_ID_TRACKNUMBER,          EBML_UINT, 0, offsetof(MatroskaTrack,num) },
327
    { MATROSKA_ID_TRACKNAME,            EBML_UTF8, 0, offsetof(MatroskaTrack,name) },
328
    { MATROSKA_ID_TRACKUID,             EBML_UINT, 0, offsetof(MatroskaTrack,uid) },
329
    { MATROSKA_ID_TRACKTYPE,            EBML_UINT, 0, offsetof(MatroskaTrack,type) },
330
    { MATROSKA_ID_CODECID,              EBML_STR,  0, offsetof(MatroskaTrack,codec_id) },
331
    { MATROSKA_ID_CODECPRIVATE,         EBML_BIN,  0, offsetof(MatroskaTrack,codec_priv) },
332
    { MATROSKA_ID_TRACKLANGUAGE,        EBML_UTF8, 0, offsetof(MatroskaTrack,language), {.s="eng"} },
333
    { MATROSKA_ID_TRACKDEFAULTDURATION, EBML_UINT, 0, offsetof(MatroskaTrack,default_duration) },
334
    { MATROSKA_ID_TRACKTIMECODESCALE,   EBML_FLOAT,0, offsetof(MatroskaTrack,time_scale), {.f=1.0} },
335
    { MATROSKA_ID_TRACKFLAGDEFAULT,     EBML_UINT, 0, offsetof(MatroskaTrack,flag_default), {.u=1} },
336
    { MATROSKA_ID_TRACKVIDEO,           EBML_NEST, 0, offsetof(MatroskaTrack,video), {.n=matroska_track_video} },
337
    { MATROSKA_ID_TRACKAUDIO,           EBML_NEST, 0, offsetof(MatroskaTrack,audio), {.n=matroska_track_audio} },
338
    { MATROSKA_ID_TRACKCONTENTENCODINGS,EBML_NEST, 0, 0, {.n=matroska_track_encodings} },
339
    { MATROSKA_ID_TRACKFLAGENABLED,     EBML_NONE },
340
    { MATROSKA_ID_TRACKFLAGFORCED,      EBML_NONE },
341
    { MATROSKA_ID_TRACKFLAGLACING,      EBML_NONE },
342
    { MATROSKA_ID_CODECNAME,            EBML_NONE },
343
    { MATROSKA_ID_CODECDECODEALL,       EBML_NONE },
344
    { MATROSKA_ID_CODECINFOURL,         EBML_NONE },
345
    { MATROSKA_ID_CODECDOWNLOADURL,     EBML_NONE },
346
    { MATROSKA_ID_TRACKMINCACHE,        EBML_NONE },
347
    { MATROSKA_ID_TRACKMAXCACHE,        EBML_NONE },
348
    { MATROSKA_ID_TRACKMAXBLKADDID,     EBML_NONE },
349
    { 0 }
350
};
351

    
352
static EbmlSyntax matroska_tracks[] = {
353
    { MATROSKA_ID_TRACKENTRY,         EBML_NEST, sizeof(MatroskaTrack), offsetof(MatroskaDemuxContext,tracks), {.n=matroska_track} },
354
    { 0 }
355
};
356

    
357
static EbmlSyntax matroska_attachment[] = {
358
    { MATROSKA_ID_FILEUID,            EBML_UINT, 0, offsetof(MatroskaAttachement,uid) },
359
    { MATROSKA_ID_FILENAME,           EBML_UTF8, 0, offsetof(MatroskaAttachement,filename) },
360
    { MATROSKA_ID_FILEMIMETYPE,       EBML_STR,  0, offsetof(MatroskaAttachement,mime) },
361
    { MATROSKA_ID_FILEDATA,           EBML_BIN,  0, offsetof(MatroskaAttachement,bin) },
362
    { MATROSKA_ID_FILEDESC,           EBML_NONE },
363
    { 0 }
364
};
365

    
366
static EbmlSyntax matroska_attachments[] = {
367
    { MATROSKA_ID_ATTACHEDFILE,       EBML_NEST, sizeof(MatroskaAttachement), offsetof(MatroskaDemuxContext,attachments), {.n=matroska_attachment} },
368
    { 0 }
369
};
370

    
371
static EbmlSyntax matroska_chapter_display[] = {
372
    { MATROSKA_ID_CHAPSTRING,         EBML_UTF8, 0, offsetof(MatroskaChapter,title) },
373
    { MATROSKA_ID_CHAPLANG,           EBML_NONE },
374
    { 0 }
375
};
376

    
377
static EbmlSyntax matroska_chapter_entry[] = {
378
    { MATROSKA_ID_CHAPTERTIMESTART,   EBML_UINT, 0, offsetof(MatroskaChapter,start), {.u=AV_NOPTS_VALUE} },
379
    { MATROSKA_ID_CHAPTERTIMEEND,     EBML_UINT, 0, offsetof(MatroskaChapter,end), {.u=AV_NOPTS_VALUE} },
380
    { MATROSKA_ID_CHAPTERUID,         EBML_UINT, 0, offsetof(MatroskaChapter,uid) },
381
    { MATROSKA_ID_CHAPTERDISPLAY,     EBML_NEST, 0, 0, {.n=matroska_chapter_display} },
382
    { MATROSKA_ID_CHAPTERFLAGHIDDEN,  EBML_NONE },
383
    { MATROSKA_ID_CHAPTERFLAGENABLED, EBML_NONE },
384
    { MATROSKA_ID_CHAPTERPHYSEQUIV,   EBML_NONE },
385
    { MATROSKA_ID_CHAPTERATOM,        EBML_NONE },
386
    { 0 }
387
};
388

    
389
static EbmlSyntax matroska_chapter[] = {
390
    { MATROSKA_ID_CHAPTERATOM,        EBML_NEST, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext,chapters), {.n=matroska_chapter_entry} },
391
    { MATROSKA_ID_EDITIONUID,         EBML_NONE },
392
    { MATROSKA_ID_EDITIONFLAGHIDDEN,  EBML_NONE },
393
    { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE },
394
    { MATROSKA_ID_EDITIONFLAGORDERED, EBML_NONE },
395
    { 0 }
396
};
397

    
398
static EbmlSyntax matroska_chapters[] = {
399
    { MATROSKA_ID_EDITIONENTRY,       EBML_NEST, 0, 0, {.n=matroska_chapter} },
400
    { 0 }
401
};
402

    
403
static EbmlSyntax matroska_index_pos[] = {
404
    { MATROSKA_ID_CUETRACK,           EBML_UINT, 0, offsetof(MatroskaIndexPos,track) },
405
    { MATROSKA_ID_CUECLUSTERPOSITION, EBML_UINT, 0, offsetof(MatroskaIndexPos,pos)   },
406
    { MATROSKA_ID_CUEBLOCKNUMBER,     EBML_NONE },
407
    { 0 }
408
};
409

    
410
static EbmlSyntax matroska_index_entry[] = {
411
    { MATROSKA_ID_CUETIME,            EBML_UINT, 0, offsetof(MatroskaIndex,time) },
412
    { MATROSKA_ID_CUETRACKPOSITION,   EBML_NEST, sizeof(MatroskaIndexPos), offsetof(MatroskaIndex,pos), {.n=matroska_index_pos} },
413
    { 0 }
414
};
415

    
416
static EbmlSyntax matroska_index[] = {
417
    { MATROSKA_ID_POINTENTRY,         EBML_NEST, sizeof(MatroskaIndex), offsetof(MatroskaDemuxContext,index), {.n=matroska_index_entry} },
418
    { 0 }
419
};
420

    
421
static EbmlSyntax matroska_simpletag[] = {
422
    { MATROSKA_ID_TAGNAME,            EBML_UTF8, 0, offsetof(MatroskaTag,name) },
423
    { MATROSKA_ID_TAGSTRING,          EBML_UTF8, 0, offsetof(MatroskaTag,string) },
424
    { MATROSKA_ID_TAGLANG,            EBML_STR,  0, offsetof(MatroskaTag,lang), {.s="und"} },
425
    { MATROSKA_ID_TAGDEFAULT,         EBML_UINT, 0, offsetof(MatroskaTag,def) },
426
    { MATROSKA_ID_SIMPLETAG,          EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTag,sub), {.n=matroska_simpletag} },
427
    { 0 }
428
};
429

    
430
static EbmlSyntax matroska_tagtargets[] = {
431
    { MATROSKA_ID_TAGTARGETS_TYPE,      EBML_STR,  0, offsetof(MatroskaTagTarget,type) },
432
    { MATROSKA_ID_TAGTARGETS_TYPEVALUE, EBML_UINT, 0, offsetof(MatroskaTagTarget,typevalue), {.u=50} },
433
    { MATROSKA_ID_TAGTARGETS_TRACKUID,  EBML_UINT, 0, offsetof(MatroskaTagTarget,trackuid) },
434
    { MATROSKA_ID_TAGTARGETS_CHAPTERUID,EBML_UINT, 0, offsetof(MatroskaTagTarget,chapteruid) },
435
    { MATROSKA_ID_TAGTARGETS_ATTACHUID, EBML_UINT, 0, offsetof(MatroskaTagTarget,attachuid) },
436
    { 0 }
437
};
438

    
439
static EbmlSyntax matroska_tag[] = {
440
    { MATROSKA_ID_SIMPLETAG,          EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTags,tag), {.n=matroska_simpletag} },
441
    { MATROSKA_ID_TAGTARGETS,         EBML_NEST, 0, offsetof(MatroskaTags,target), {.n=matroska_tagtargets} },
442
    { 0 }
443
};
444

    
445
static EbmlSyntax matroska_tags[] = {
446
    { MATROSKA_ID_TAG,                EBML_NEST, sizeof(MatroskaTags), offsetof(MatroskaDemuxContext,tags), {.n=matroska_tag} },
447
    { 0 }
448
};
449

    
450
static EbmlSyntax matroska_seekhead_entry[] = {
451
    { MATROSKA_ID_SEEKID,             EBML_UINT, 0, offsetof(MatroskaSeekhead,id) },
452
    { MATROSKA_ID_SEEKPOSITION,       EBML_UINT, 0, offsetof(MatroskaSeekhead,pos), {.u=-1} },
453
    { 0 }
454
};
455

    
456
static EbmlSyntax matroska_seekhead[] = {
457
    { MATROSKA_ID_SEEKENTRY,          EBML_NEST, sizeof(MatroskaSeekhead), offsetof(MatroskaDemuxContext,seekhead), {.n=matroska_seekhead_entry} },
458
    { 0 }
459
};
460

    
461
static EbmlSyntax matroska_segment[] = {
462
    { MATROSKA_ID_INFO,           EBML_NEST, 0, 0, {.n=matroska_info       } },
463
    { MATROSKA_ID_TRACKS,         EBML_NEST, 0, 0, {.n=matroska_tracks     } },
464
    { MATROSKA_ID_ATTACHMENTS,    EBML_NEST, 0, 0, {.n=matroska_attachments} },
465
    { MATROSKA_ID_CHAPTERS,       EBML_NEST, 0, 0, {.n=matroska_chapters   } },
466
    { MATROSKA_ID_CUES,           EBML_NEST, 0, 0, {.n=matroska_index      } },
467
    { MATROSKA_ID_TAGS,           EBML_NEST, 0, 0, {.n=matroska_tags       } },
468
    { MATROSKA_ID_SEEKHEAD,       EBML_NEST, 0, 0, {.n=matroska_seekhead   } },
469
    { MATROSKA_ID_CLUSTER,        EBML_STOP, 0, offsetof(MatroskaDemuxContext,has_cluster_id) },
470
    { 0 }
471
};
472

    
473
static EbmlSyntax matroska_segments[] = {
474
    { MATROSKA_ID_SEGMENT,        EBML_NEST, 0, 0, {.n=matroska_segment    } },
475
    { 0 }
476
};
477

    
478
static EbmlSyntax matroska_blockgroup[] = {
479
    { MATROSKA_ID_BLOCK,          EBML_BIN,  0, offsetof(MatroskaBlock,bin) },
480
    { MATROSKA_ID_SIMPLEBLOCK,    EBML_BIN,  0, offsetof(MatroskaBlock,bin) },
481
    { MATROSKA_ID_BLOCKDURATION,  EBML_UINT, 0, offsetof(MatroskaBlock,duration), {.u=AV_NOPTS_VALUE} },
482
    { MATROSKA_ID_BLOCKREFERENCE, EBML_UINT, 0, offsetof(MatroskaBlock,reference) },
483
    { 1,                          EBML_UINT, 0, offsetof(MatroskaBlock,non_simple), {.u=1} },
484
    { 0 }
485
};
486

    
487
static EbmlSyntax matroska_cluster[] = {
488
    { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) },
489
    { MATROSKA_ID_BLOCKGROUP,     EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
490
    { MATROSKA_ID_SIMPLEBLOCK,    EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
491
    { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE },
492
    { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE },
493
    { 0 }
494
};
495

    
496
static EbmlSyntax matroska_clusters[] = {
497
    { MATROSKA_ID_CLUSTER,        EBML_NEST, 0, 0, {.n=matroska_cluster} },
498
    { MATROSKA_ID_INFO,           EBML_NONE },
499
    { MATROSKA_ID_CUES,           EBML_NONE },
500
    { MATROSKA_ID_TAGS,           EBML_NONE },
501
    { MATROSKA_ID_SEEKHEAD,       EBML_NONE },
502
    { 0 }
503
};
504

    
505
/*
506
 * Return: Whether we reached the end of a level in the hierarchy or not.
507
 */
508
static int ebml_level_end(MatroskaDemuxContext *matroska)
509
{
510
    ByteIOContext *pb = matroska->ctx->pb;
511
    int64_t pos = url_ftell(pb);
512

    
513
    if (matroska->num_levels > 0) {
514
        MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
515
        if (pos - level->start >= level->length) {
516
            matroska->num_levels--;
517
            return 1;
518
        }
519
    }
520
    return 0;
521
}
522

    
523
/*
524
 * Read: an "EBML number", which is defined as a variable-length
525
 * array of bytes. The first byte indicates the length by giving a
526
 * number of 0-bits followed by a one. The position of the first
527
 * "one" bit inside the first byte indicates the length of this
528
 * number.
529
 * Returns: number of bytes read, < 0 on error
530
 */
531
static int ebml_read_num(MatroskaDemuxContext *matroska, ByteIOContext *pb,
532
                         int max_size, uint64_t *number)
533
{
534
    int len_mask = 0x80, read = 1, n = 1;
535
    int64_t total = 0;
536

    
537
    /* The first byte tells us the length in bytes - get_byte() can normally
538
     * return 0, but since that's not a valid first ebmlID byte, we can
539
     * use it safely here to catch EOS. */
540
    if (!(total = get_byte(pb))) {
541
        /* we might encounter EOS here */
542
        if (!url_feof(pb)) {
543
            int64_t pos = url_ftell(pb);
544
            av_log(matroska->ctx, AV_LOG_ERROR,
545
                   "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
546
                   pos, pos);
547
        }
548
        return AVERROR(EIO); /* EOS or actual I/O error */
549
    }
550

    
551
    /* get the length of the EBML number */
552
    while (read <= max_size && !(total & len_mask)) {
553
        read++;
554
        len_mask >>= 1;
555
    }
556
    if (read > max_size) {
557
        int64_t pos = url_ftell(pb) - 1;
558
        av_log(matroska->ctx, AV_LOG_ERROR,
559
               "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
560
               (uint8_t) total, pos, pos);
561
        return AVERROR_INVALIDDATA;
562
    }
563

    
564
    /* read out length */
565
    total &= ~len_mask;
566
    while (n++ < read)
567
        total = (total << 8) | get_byte(pb);
568

    
569
    *number = total;
570

    
571
    return read;
572
}
573

    
574
/*
575
 * Read the next element as an unsigned int.
576
 * 0 is success, < 0 is failure.
577
 */
578
static int ebml_read_uint(ByteIOContext *pb, int size, uint64_t *num)
579
{
580
    int n = 0;
581

    
582
    if (size < 1 || size > 8)
583
        return AVERROR_INVALIDDATA;
584

    
585
    /* big-endian ordering; build up number */
586
    *num = 0;
587
    while (n++ < size)
588
        *num = (*num << 8) | get_byte(pb);
589

    
590
    return 0;
591
}
592

    
593
/*
594
 * Read the next element as a float.
595
 * 0 is success, < 0 is failure.
596
 */
597
static int ebml_read_float(ByteIOContext *pb, int size, double *num)
598
{
599
    if (size == 4) {
600
        *num= av_int2flt(get_be32(pb));
601
    } else if(size==8){
602
        *num= av_int2dbl(get_be64(pb));
603
    } else
604
        return AVERROR_INVALIDDATA;
605

    
606
    return 0;
607
}
608

    
609
/*
610
 * Read the next element as an ASCII string.
611
 * 0 is success, < 0 is failure.
612
 */
613
static int ebml_read_ascii(ByteIOContext *pb, int size, char **str)
614
{
615
    av_free(*str);
616
    /* EBML strings are usually not 0-terminated, so we allocate one
617
     * byte more, read the string and NULL-terminate it ourselves. */
618
    if (!(*str = av_malloc(size + 1)))
619
        return AVERROR(ENOMEM);
620
    if (get_buffer(pb, (uint8_t *) *str, size) != size) {
621
        av_free(*str);
622
        return AVERROR(EIO);
623
    }
624
    (*str)[size] = '\0';
625

    
626
    return 0;
627
}
628

    
629
/*
630
 * Read the next element as binary data.
631
 * 0 is success, < 0 is failure.
632
 */
633
static int ebml_read_binary(ByteIOContext *pb, int length, EbmlBin *bin)
634
{
635
    av_free(bin->data);
636
    if (!(bin->data = av_malloc(length)))
637
        return AVERROR(ENOMEM);
638

    
639
    bin->size = length;
640
    bin->pos  = url_ftell(pb);
641
    if (get_buffer(pb, bin->data, length) != length)
642
        return AVERROR(EIO);
643

    
644
    return 0;
645
}
646

    
647
/*
648
 * Read the next element, but only the header. The contents
649
 * are supposed to be sub-elements which can be read separately.
650
 * 0 is success, < 0 is failure.
651
 */
652
static int ebml_read_master(MatroskaDemuxContext *matroska, int length)
653
{
654
    ByteIOContext *pb = matroska->ctx->pb;
655
    MatroskaLevel *level;
656

    
657
    if (matroska->num_levels >= EBML_MAX_DEPTH) {
658
        av_log(matroska->ctx, AV_LOG_ERROR,
659
               "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
660
        return AVERROR(ENOSYS);
661
    }
662

    
663
    level = &matroska->levels[matroska->num_levels++];
664
    level->start = url_ftell(pb);
665
    level->length = length;
666

    
667
    return 0;
668
}
669

    
670
/*
671
 * Read signed/unsigned "EBML" numbers.
672
 * Return: number of bytes processed, < 0 on error
673
 */
674
static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska,
675
                                 uint8_t *data, uint32_t size, uint64_t *num)
676
{
677
    ByteIOContext pb;
678
    init_put_byte(&pb, data, size, 0, NULL, NULL, NULL, NULL);
679
    return ebml_read_num(matroska, &pb, 8, num);
680
}
681

    
682
/*
683
 * Same as above, but signed.
684
 */
685
static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska,
686
                                 uint8_t *data, uint32_t size, int64_t *num)
687
{
688
    uint64_t unum;
689
    int res;
690

    
691
    /* read as unsigned number first */
692
    if ((res = matroska_ebmlnum_uint(matroska, data, size, &unum)) < 0)
693
        return res;
694

    
695
    /* make signed (weird way) */
696
    *num = unum - ((1LL << (7*res - 1)) - 1);
697

    
698
    return res;
699
}
700

    
701
static int ebml_parse_elem(MatroskaDemuxContext *matroska,
702
                           EbmlSyntax *syntax, void *data);
703

    
704
static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
705
                         uint32_t id, void *data)
706
{
707
    int i;
708
    for (i=0; syntax[i].id; i++)
709
        if (id == syntax[i].id)
710
            break;
711
    if (!syntax[i].id && id != EBML_ID_VOID && id != EBML_ID_CRC32)
712
        av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id);
713
    return ebml_parse_elem(matroska, &syntax[i], data);
714
}
715

    
716
static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
717
                      void *data)
718
{
719
    uint64_t id;
720
    int res = ebml_read_num(matroska, matroska->ctx->pb, 4, &id);
721
    id |= 1 << 7*res;
722
    return res < 0 ? res : ebml_parse_id(matroska, syntax, id, data);
723
}
724

    
725
static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
726
                           void *data)
727
{
728
    int i, res = 0;
729

    
730
    for (i=0; syntax[i].id; i++)
731
        switch (syntax[i].type) {
732
        case EBML_UINT:
733
            *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u;
734
            break;
735
        case EBML_FLOAT:
736
            *(double   *)((char *)data+syntax[i].data_offset) = syntax[i].def.f;
737
            break;
738
        case EBML_STR:
739
        case EBML_UTF8:
740
            *(char    **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s);
741
            break;
742
        }
743

    
744
    while (!res && !ebml_level_end(matroska))
745
        res = ebml_parse(matroska, syntax, data);
746

    
747
    return res;
748
}
749

    
750
static int ebml_parse_elem(MatroskaDemuxContext *matroska,
751
                           EbmlSyntax *syntax, void *data)
752
{
753
    ByteIOContext *pb = matroska->ctx->pb;
754
    uint32_t id = syntax->id;
755
    uint64_t length;
756
    int res;
757

    
758
    data = (char *)data + syntax->data_offset;
759
    if (syntax->list_elem_size) {
760
        EbmlList *list = data;
761
        list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);
762
        data = (char*)list->elem + list->nb_elem*syntax->list_elem_size;
763
        memset(data, 0, syntax->list_elem_size);
764
        list->nb_elem++;
765
    }
766

    
767
    if (syntax->type != EBML_PASS && syntax->type != EBML_STOP)
768
        if ((res = ebml_read_num(matroska, pb, 8, &length)) < 0)
769
            return res;
770

    
771
    switch (syntax->type) {
772
    case EBML_UINT:  res = ebml_read_uint  (pb, length, data);  break;
773
    case EBML_FLOAT: res = ebml_read_float (pb, length, data);  break;
774
    case EBML_STR:
775
    case EBML_UTF8:  res = ebml_read_ascii (pb, length, data);  break;
776
    case EBML_BIN:   res = ebml_read_binary(pb, length, data);  break;
777
    case EBML_NEST:  if ((res=ebml_read_master(matroska, length)) < 0)
778
                         return res;
779
                     if (id == MATROSKA_ID_SEGMENT)
780
                         matroska->segment_start = url_ftell(matroska->ctx->pb);
781
                     return ebml_parse_nest(matroska, syntax->def.n, data);
782
    case EBML_PASS:  return ebml_parse_id(matroska, syntax->def.n, id, data);
783
    case EBML_STOP:  *(int *)data = 1;      return 1;
784
    default:         return url_fseek(pb,length,SEEK_CUR)<0 ? AVERROR(EIO) : 0;
785
    }
786
    if (res == AVERROR_INVALIDDATA)
787
        av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n");
788
    else if (res == AVERROR(EIO))
789
        av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n");
790
    return res;
791
}
792

    
793
static void ebml_free(EbmlSyntax *syntax, void *data)
794
{
795
    int i, j;
796
    for (i=0; syntax[i].id; i++) {
797
        void *data_off = (char *)data + syntax[i].data_offset;
798
        switch (syntax[i].type) {
799
        case EBML_STR:
800
        case EBML_UTF8:  av_freep(data_off);                      break;
801
        case EBML_BIN:   av_freep(&((EbmlBin *)data_off)->data);  break;
802
        case EBML_NEST:
803
            if (syntax[i].list_elem_size) {
804
                EbmlList *list = data_off;
805
                char *ptr = list->elem;
806
                for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size)
807
                    ebml_free(syntax[i].def.n, ptr);
808
                av_free(list->elem);
809
            } else
810
                ebml_free(syntax[i].def.n, data_off);
811
        default:  break;
812
        }
813
    }
814
}
815

    
816

    
817
/*
818
 * Autodetecting...
819
 */
820
static int matroska_probe(AVProbeData *p)
821
{
822
    uint64_t total = 0;
823
    int len_mask = 0x80, size = 1, n = 1;
824
    static const char probe_data[] = "matroska";
825

    
826
    /* EBML header? */
827
    if (AV_RB32(p->buf) != EBML_ID_HEADER)
828
        return 0;
829

    
830
    /* length of header */
831
    total = p->buf[4];
832
    while (size <= 8 && !(total & len_mask)) {
833
        size++;
834
        len_mask >>= 1;
835
    }
836
    if (size > 8)
837
      return 0;
838
    total &= (len_mask - 1);
839
    while (n < size)
840
        total = (total << 8) | p->buf[4 + n++];
841

    
842
    /* Does the probe data contain the whole header? */
843
    if (p->buf_size < 4 + size + total)
844
      return 0;
845

    
846
    /* The header must contain the document type 'matroska'. For now,
847
     * we don't parse the whole header but simply check for the
848
     * availability of that array of characters inside the header.
849
     * Not fully fool-proof, but good enough. */
850
    for (n = 4+size; n <= 4+size+total-(sizeof(probe_data)-1); n++)
851
        if (!memcmp(p->buf+n, probe_data, sizeof(probe_data)-1))
852
            return AVPROBE_SCORE_MAX;
853

    
854
    return 0;
855
}
856

    
857
static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska,
858
                                                 int num)
859
{
860
    MatroskaTrack *tracks = matroska->tracks.elem;
861
    int i;
862

    
863
    for (i=0; i < matroska->tracks.nb_elem; i++)
864
        if (tracks[i].num == num)
865
            return &tracks[i];
866

    
867
    av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num);
868
    return NULL;
869
}
870

    
871
static int matroska_decode_buffer(uint8_t** buf, int* buf_size,
872
                                  MatroskaTrack *track)
873
{
874
    MatroskaTrackEncoding *encodings = track->encodings.elem;
875
    uint8_t* data = *buf;
876
    int isize = *buf_size;
877
    uint8_t* pkt_data = NULL;
878
    int pkt_size = isize;
879
    int result = 0;
880
    int olen;
881

    
882
    switch (encodings[0].compression.algo) {
883
    case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
884
        return encodings[0].compression.settings.size;
885
    case MATROSKA_TRACK_ENCODING_COMP_LZO:
886
        do {
887
            olen = pkt_size *= 3;
888
            pkt_data = av_realloc(pkt_data, pkt_size+AV_LZO_OUTPUT_PADDING);
889
            result = av_lzo1x_decode(pkt_data, &olen, data, &isize);
890
        } while (result==AV_LZO_OUTPUT_FULL && pkt_size<10000000);
891
        if (result)
892
            goto failed;
893
        pkt_size -= olen;
894
        break;
895
#if CONFIG_ZLIB
896
    case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
897
        z_stream zstream = {0};
898
        if (inflateInit(&zstream) != Z_OK)
899
            return -1;
900
        zstream.next_in = data;
901
        zstream.avail_in = isize;
902
        do {
903
            pkt_size *= 3;
904
            pkt_data = av_realloc(pkt_data, pkt_size);
905
            zstream.avail_out = pkt_size - zstream.total_out;
906
            zstream.next_out = pkt_data + zstream.total_out;
907
            result = inflate(&zstream, Z_NO_FLUSH);
908
        } while (result==Z_OK && pkt_size<10000000);
909
        pkt_size = zstream.total_out;
910
        inflateEnd(&zstream);
911
        if (result != Z_STREAM_END)
912
            goto failed;
913
        break;
914
    }
915
#endif
916
#if CONFIG_BZLIB
917
    case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
918
        bz_stream bzstream = {0};
919
        if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
920
            return -1;
921
        bzstream.next_in = data;
922
        bzstream.avail_in = isize;
923
        do {
924
            pkt_size *= 3;
925
            pkt_data = av_realloc(pkt_data, pkt_size);
926
            bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
927
            bzstream.next_out = pkt_data + bzstream.total_out_lo32;
928
            result = BZ2_bzDecompress(&bzstream);
929
        } while (result==BZ_OK && pkt_size<10000000);
930
        pkt_size = bzstream.total_out_lo32;
931
        BZ2_bzDecompressEnd(&bzstream);
932
        if (result != BZ_STREAM_END)
933
            goto failed;
934
        break;
935
    }
936
#endif
937
    default:
938
        return -1;
939
    }
940

    
941
    *buf = pkt_data;
942
    *buf_size = pkt_size;
943
    return 0;
944
 failed:
945
    av_free(pkt_data);
946
    return -1;
947
}
948

    
949
static void matroska_fix_ass_packet(MatroskaDemuxContext *matroska,
950
                                    AVPacket *pkt, uint64_t display_duration)
951
{
952
    char *line, *layer, *ptr = pkt->data, *end = ptr+pkt->size;
953
    for (; *ptr!=',' && ptr<end-1; ptr++);
954
    if (*ptr == ',')
955
        layer = ++ptr;
956
    for (; *ptr!=',' && ptr<end-1; ptr++);
957
    if (*ptr == ',') {
958
        int64_t end_pts = pkt->pts + display_duration;
959
        int sc = matroska->time_scale * pkt->pts / 10000000;
960
        int ec = matroska->time_scale * end_pts  / 10000000;
961
        int sh, sm, ss, eh, em, es, len;
962
        sh = sc/360000;  sc -= 360000*sh;
963
        sm = sc/  6000;  sc -=   6000*sm;
964
        ss = sc/   100;  sc -=    100*ss;
965
        eh = ec/360000;  ec -= 360000*eh;
966
        em = ec/  6000;  ec -=   6000*em;
967
        es = ec/   100;  ec -=    100*es;
968
        *ptr++ = '\0';
969
        len = 50 + end-ptr + FF_INPUT_BUFFER_PADDING_SIZE;
970
        if (!(line = av_malloc(len)))
971
            return;
972
        snprintf(line,len,"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
973
                 layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
974
        av_free(pkt->data);
975
        pkt->data = line;
976
        pkt->size = strlen(line);
977
    }
978
}
979

    
980
static void matroska_merge_packets(AVPacket *out, AVPacket *in)
981
{
982
    out->data = av_realloc(out->data, out->size+in->size);
983
    memcpy(out->data+out->size, in->data, in->size);
984
    out->size += in->size;
985
    av_destruct_packet(in);
986
    av_free(in);
987
}
988

    
989
static void matroska_convert_tag(AVFormatContext *s, EbmlList *list,
990
                                 AVMetadata **metadata, char *prefix)
991
{
992
    MatroskaTag *tags = list->elem;
993
    char key[1024];
994
    int i;
995

    
996
    for (i=0; i < list->nb_elem; i++) {
997
        const char *lang = strcmp(tags[i].lang, "und") ? tags[i].lang : NULL;
998
        if (prefix)  snprintf(key, sizeof(key), "%s/%s", prefix, tags[i].name);
999
        else         av_strlcpy(key, tags[i].name, sizeof(key));
1000
        if (tags[i].def || !lang) {
1001
        av_metadata_set(metadata, key, tags[i].string);
1002
        if (tags[i].sub.nb_elem)
1003
            matroska_convert_tag(s, &tags[i].sub, metadata, key);
1004
        }
1005
        if (lang) {
1006
            av_strlcat(key, "-", sizeof(key));
1007
            av_strlcat(key, lang, sizeof(key));
1008
            av_metadata_set(metadata, key, tags[i].string);
1009
            if (tags[i].sub.nb_elem)
1010
                matroska_convert_tag(s, &tags[i].sub, metadata, key);
1011
        }
1012
    }
1013
}
1014

    
1015
static void matroska_convert_tags(AVFormatContext *s)
1016
{
1017
    MatroskaDemuxContext *matroska = s->priv_data;
1018
    MatroskaTags *tags = matroska->tags.elem;
1019
    int i, j;
1020

    
1021
    for (i=0; i < matroska->tags.nb_elem; i++) {
1022
        if (tags[i].target.attachuid) {
1023
            MatroskaAttachement *attachment = matroska->attachments.elem;
1024
            for (j=0; j<matroska->attachments.nb_elem; j++)
1025
                if (attachment[j].uid == tags[i].target.attachuid)
1026
                    matroska_convert_tag(s, &tags[i].tag,
1027
                                         &attachment[j].stream->metadata, NULL);
1028
        } else if (tags[i].target.chapteruid) {
1029
            MatroskaChapter *chapter = matroska->chapters.elem;
1030
            for (j=0; j<matroska->chapters.nb_elem; j++)
1031
                if (chapter[j].uid == tags[i].target.chapteruid)
1032
                    matroska_convert_tag(s, &tags[i].tag,
1033
                                         &chapter[j].chapter->metadata, NULL);
1034
        } else if (tags[i].target.trackuid) {
1035
            MatroskaTrack *track = matroska->tracks.elem;
1036
            for (j=0; j<matroska->tracks.nb_elem; j++)
1037
                if (track[j].uid == tags[i].target.trackuid)
1038
                    matroska_convert_tag(s, &tags[i].tag,
1039
                                         &track[j].stream->metadata, NULL);
1040
        } else {
1041
            matroska_convert_tag(s, &tags[i].tag, &s->metadata,
1042
                                 tags[i].target.type);
1043
        }
1044
    }
1045
}
1046

    
1047
static void matroska_execute_seekhead(MatroskaDemuxContext *matroska)
1048
{
1049
    EbmlList *seekhead_list = &matroska->seekhead;
1050
    MatroskaSeekhead *seekhead = seekhead_list->elem;
1051
    uint32_t level_up = matroska->level_up;
1052
    int64_t before_pos = url_ftell(matroska->ctx->pb);
1053
    MatroskaLevel level;
1054
    int i;
1055

    
1056
    for (i=0; i<seekhead_list->nb_elem; i++) {
1057
        int64_t offset = seekhead[i].pos + matroska->segment_start;
1058

    
1059
        if (seekhead[i].pos <= before_pos
1060
            || seekhead[i].id == MATROSKA_ID_SEEKHEAD
1061
            || seekhead[i].id == MATROSKA_ID_CLUSTER)
1062
            continue;
1063

    
1064
        /* seek */
1065
        if (url_fseek(matroska->ctx->pb, offset, SEEK_SET) != offset)
1066
            continue;
1067

    
1068
        /* We don't want to lose our seekhead level, so we add
1069
         * a dummy. This is a crude hack. */
1070
        if (matroska->num_levels == EBML_MAX_DEPTH) {
1071
            av_log(matroska->ctx, AV_LOG_INFO,
1072
                   "Max EBML element depth (%d) reached, "
1073
                   "cannot parse further.\n", EBML_MAX_DEPTH);
1074
            break;
1075
        }
1076

    
1077
        level.start = 0;
1078
        level.length = (uint64_t)-1;
1079
        matroska->levels[matroska->num_levels] = level;
1080
        matroska->num_levels++;
1081

    
1082
        ebml_parse(matroska, matroska_segment, matroska);
1083

    
1084
        /* remove dummy level */
1085
        while (matroska->num_levels) {
1086
            uint64_t length = matroska->levels[--matroska->num_levels].length;
1087
            if (length == (uint64_t)-1)
1088
                break;
1089
        }
1090
    }
1091

    
1092
    /* seek back */
1093
    url_fseek(matroska->ctx->pb, before_pos, SEEK_SET);
1094
    matroska->level_up = level_up;
1095
}
1096

    
1097
static int matroska_aac_profile(char *codec_id)
1098
{
1099
    static const char * const aac_profiles[] = { "MAIN", "LC", "SSR" };
1100
    int profile;
1101

    
1102
    for (profile=0; profile<FF_ARRAY_ELEMS(aac_profiles); profile++)
1103
        if (strstr(codec_id, aac_profiles[profile]))
1104
            break;
1105
    return profile + 1;
1106
}
1107

    
1108
static int matroska_aac_sri(int samplerate)
1109
{
1110
    int sri;
1111

    
1112
    for (sri=0; sri<FF_ARRAY_ELEMS(ff_mpeg4audio_sample_rates); sri++)
1113
        if (ff_mpeg4audio_sample_rates[sri] == samplerate)
1114
            break;
1115
    return sri;
1116
}
1117

    
1118
static int matroska_read_header(AVFormatContext *s, AVFormatParameters *ap)
1119
{
1120
    MatroskaDemuxContext *matroska = s->priv_data;
1121
    EbmlList *attachements_list = &matroska->attachments;
1122
    MatroskaAttachement *attachements;
1123
    EbmlList *chapters_list = &matroska->chapters;
1124
    MatroskaChapter *chapters;
1125
    MatroskaTrack *tracks;
1126
    EbmlList *index_list;
1127
    MatroskaIndex *index;
1128
    int index_scale = 1;
1129
    uint64_t max_start = 0;
1130
    Ebml ebml = { 0 };
1131
    AVStream *st;
1132
    int i, j;
1133

    
1134
    matroska->ctx = s;
1135

    
1136
    /* First read the EBML header. */
1137
    if (ebml_parse(matroska, ebml_syntax, &ebml)
1138
        || ebml.version > EBML_VERSION       || ebml.max_size > sizeof(uint64_t)
1139
        || ebml.id_length > sizeof(uint32_t) || strcmp(ebml.doctype, "matroska")
1140
        || ebml.doctype_version > 2) {
1141
        av_log(matroska->ctx, AV_LOG_ERROR,
1142
               "EBML header using unsupported features\n"
1143
               "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
1144
               ebml.version, ebml.doctype, ebml.doctype_version);
1145
        return AVERROR_NOFMT;
1146
    }
1147
    ebml_free(ebml_syntax, &ebml);
1148

    
1149
    /* The next thing is a segment. */
1150
    if (ebml_parse(matroska, matroska_segments, matroska) < 0)
1151
        return -1;
1152
    matroska_execute_seekhead(matroska);
1153

    
1154
    if (matroska->duration)
1155
        matroska->ctx->duration = matroska->duration * matroska->time_scale
1156
                                  * 1000 / AV_TIME_BASE;
1157
    av_metadata_set(&s->metadata, "title", matroska->title);
1158

    
1159
    tracks = matroska->tracks.elem;
1160
    for (i=0; i < matroska->tracks.nb_elem; i++) {
1161
        MatroskaTrack *track = &tracks[i];
1162
        enum CodecID codec_id = CODEC_ID_NONE;
1163
        EbmlList *encodings_list = &tracks->encodings;
1164
        MatroskaTrackEncoding *encodings = encodings_list->elem;
1165
        uint8_t *extradata = NULL;
1166
        int extradata_size = 0;
1167
        int extradata_offset = 0;
1168
        ByteIOContext b;
1169

    
1170
        /* Apply some sanity checks. */
1171
        if (track->type != MATROSKA_TRACK_TYPE_VIDEO &&
1172
            track->type != MATROSKA_TRACK_TYPE_AUDIO &&
1173
            track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
1174
            av_log(matroska->ctx, AV_LOG_INFO,
1175
                   "Unknown or unsupported track type %"PRIu64"\n",
1176
                   track->type);
1177
            continue;
1178
        }
1179
        if (track->codec_id == NULL)
1180
            continue;
1181

    
1182
        if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
1183
            if (!track->default_duration)
1184
                track->default_duration = 1000000000/track->video.frame_rate;
1185
            if (!track->video.display_width)
1186
                track->video.display_width = track->video.pixel_width;
1187
            if (!track->video.display_height)
1188
                track->video.display_height = track->video.pixel_height;
1189
        } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
1190
            if (!track->audio.out_samplerate)
1191
                track->audio.out_samplerate = track->audio.samplerate;
1192
        }
1193
        if (encodings_list->nb_elem > 1) {
1194
            av_log(matroska->ctx, AV_LOG_ERROR,
1195
                   "Multiple combined encodings no supported");
1196
        } else if (encodings_list->nb_elem == 1) {
1197
            if (encodings[0].type ||
1198
                (encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
1199
#if CONFIG_ZLIB
1200
                 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
1201
#endif
1202
#if CONFIG_BZLIB
1203
                 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
1204
#endif
1205
                 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO)) {
1206
                encodings[0].scope = 0;
1207
                av_log(matroska->ctx, AV_LOG_ERROR,
1208
                       "Unsupported encoding type");
1209
            } else if (track->codec_priv.size && encodings[0].scope&2) {
1210
                uint8_t *codec_priv = track->codec_priv.data;
1211
                int offset = matroska_decode_buffer(&track->codec_priv.data,
1212
                                                    &track->codec_priv.size,
1213
                                                    track);
1214
                if (offset < 0) {
1215
                    track->codec_priv.data = NULL;
1216
                    track->codec_priv.size = 0;
1217
                    av_log(matroska->ctx, AV_LOG_ERROR,
1218
                           "Failed to decode codec private data\n");
1219
                } else if (offset > 0) {
1220
                    track->codec_priv.data = av_malloc(track->codec_priv.size + offset);
1221
                    memcpy(track->codec_priv.data,
1222
                           encodings[0].compression.settings.data, offset);
1223
                    memcpy(track->codec_priv.data+offset, codec_priv,
1224
                           track->codec_priv.size);
1225
                    track->codec_priv.size += offset;
1226
                }
1227
                if (codec_priv != track->codec_priv.data)
1228
                    av_free(codec_priv);
1229
            }
1230
        }
1231

    
1232
        for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
1233
            if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
1234
                        strlen(ff_mkv_codec_tags[j].str))){
1235
                codec_id= ff_mkv_codec_tags[j].id;
1236
                break;
1237
            }
1238
        }
1239

    
1240
        st = track->stream = av_new_stream(s, 0);
1241
        if (st == NULL)
1242
            return AVERROR(ENOMEM);
1243

    
1244
        if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC")
1245
            && track->codec_priv.size >= 40
1246
            && track->codec_priv.data != NULL) {
1247
            track->video.fourcc = AV_RL32(track->codec_priv.data + 16);
1248
            codec_id = ff_codec_get_id(ff_codec_bmp_tags, track->video.fourcc);
1249
        } else if (!strcmp(track->codec_id, "A_MS/ACM")
1250
                   && track->codec_priv.size >= 18
1251
                   && track->codec_priv.data != NULL) {
1252
            init_put_byte(&b, track->codec_priv.data, track->codec_priv.size,
1253
                          URL_RDONLY, NULL, NULL, NULL, NULL);
1254
            ff_get_wav_header(&b, st->codec, track->codec_priv.size);
1255
            codec_id = st->codec->codec_id;
1256
            extradata_offset = 18;
1257
            track->codec_priv.size -= extradata_offset;
1258
        } else if (!strcmp(track->codec_id, "V_QUICKTIME")
1259
                   && (track->codec_priv.size >= 86)
1260
                   && (track->codec_priv.data != NULL)) {
1261
            track->video.fourcc = AV_RL32(track->codec_priv.data);
1262
            codec_id=ff_codec_get_id(codec_movvideo_tags, track->video.fourcc);
1263
        } else if (codec_id == CODEC_ID_PCM_S16BE) {
1264
            switch (track->audio.bitdepth) {
1265
            case  8:  codec_id = CODEC_ID_PCM_U8;     break;
1266
            case 24:  codec_id = CODEC_ID_PCM_S24BE;  break;
1267
            case 32:  codec_id = CODEC_ID_PCM_S32BE;  break;
1268
            }
1269
        } else if (codec_id == CODEC_ID_PCM_S16LE) {
1270
            switch (track->audio.bitdepth) {
1271
            case  8:  codec_id = CODEC_ID_PCM_U8;     break;
1272
            case 24:  codec_id = CODEC_ID_PCM_S24LE;  break;
1273
            case 32:  codec_id = CODEC_ID_PCM_S32LE;  break;
1274
            }
1275
        } else if (codec_id==CODEC_ID_PCM_F32LE && track->audio.bitdepth==64) {
1276
            codec_id = CODEC_ID_PCM_F64LE;
1277
        } else if (codec_id == CODEC_ID_AAC && !track->codec_priv.size) {
1278
            int profile = matroska_aac_profile(track->codec_id);
1279
            int sri = matroska_aac_sri(track->audio.samplerate);
1280
            extradata = av_malloc(5);
1281
            if (extradata == NULL)
1282
                return AVERROR(ENOMEM);
1283
            extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
1284
            extradata[1] = ((sri&0x01) << 7) | (track->audio.channels<<3);
1285
            if (strstr(track->codec_id, "SBR")) {
1286
                sri = matroska_aac_sri(track->audio.out_samplerate);
1287
                extradata[2] = 0x56;
1288
                extradata[3] = 0xE5;
1289
                extradata[4] = 0x80 | (sri<<3);
1290
                extradata_size = 5;
1291
            } else
1292
                extradata_size = 2;
1293
        } else if (codec_id == CODEC_ID_TTA) {
1294
            extradata_size = 30;
1295
            extradata = av_mallocz(extradata_size);
1296
            if (extradata == NULL)
1297
                return AVERROR(ENOMEM);
1298
            init_put_byte(&b, extradata, extradata_size, 1,
1299
                          NULL, NULL, NULL, NULL);
1300
            put_buffer(&b, "TTA1", 4);
1301
            put_le16(&b, 1);
1302
            put_le16(&b, track->audio.channels);
1303
            put_le16(&b, track->audio.bitdepth);
1304
            put_le32(&b, track->audio.out_samplerate);
1305
            put_le32(&b, matroska->ctx->duration * track->audio.out_samplerate);
1306
        } else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
1307
                   codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
1308
            extradata_offset = 26;
1309
            track->codec_priv.size -= extradata_offset;
1310
        } else if (codec_id == CODEC_ID_RA_144) {
1311
            track->audio.out_samplerate = 8000;
1312
            track->audio.channels = 1;
1313
        } else if (codec_id == CODEC_ID_RA_288 || codec_id == CODEC_ID_COOK ||
1314
                   codec_id == CODEC_ID_ATRAC3) {
1315
            init_put_byte(&b, track->codec_priv.data,track->codec_priv.size,
1316
                          0, NULL, NULL, NULL, NULL);
1317
            url_fskip(&b, 24);
1318
            track->audio.coded_framesize = get_be32(&b);
1319
            url_fskip(&b, 12);
1320
            track->audio.sub_packet_h    = get_be16(&b);
1321
            track->audio.frame_size      = get_be16(&b);
1322
            track->audio.sub_packet_size = get_be16(&b);
1323
            track->audio.buf = av_malloc(track->audio.frame_size * track->audio.sub_packet_h);
1324
            if (codec_id == CODEC_ID_RA_288) {
1325
                st->codec->block_align = track->audio.coded_framesize;
1326
                track->codec_priv.size = 0;
1327
            } else {
1328
                st->codec->block_align = track->audio.sub_packet_size;
1329
                extradata_offset = 78;
1330
                track->codec_priv.size -= extradata_offset;
1331
            }
1332
        }
1333

    
1334
        if (codec_id == CODEC_ID_NONE)
1335
            av_log(matroska->ctx, AV_LOG_INFO,
1336
                   "Unknown/unsupported CodecID %s.\n", track->codec_id);
1337

    
1338
        if (track->time_scale < 0.01)
1339
            track->time_scale = 1.0;
1340
        av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
1341

    
1342
        st->codec->codec_id = codec_id;
1343
        st->start_time = 0;
1344
        if (strcmp(track->language, "und"))
1345
            av_metadata_set(&st->metadata, "language", track->language);
1346
        av_metadata_set(&st->metadata, "description", track->name);
1347

    
1348
        if (track->flag_default)
1349
            st->disposition |= AV_DISPOSITION_DEFAULT;
1350

    
1351
        if (track->default_duration)
1352
            av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
1353
                      track->default_duration, 1000000000, 30000);
1354

    
1355
        if(extradata){
1356
            st->codec->extradata = extradata;
1357
            st->codec->extradata_size = extradata_size;
1358
        } else if(track->codec_priv.data && track->codec_priv.size > 0){
1359
            st->codec->extradata = av_mallocz(track->codec_priv.size +
1360
                                              FF_INPUT_BUFFER_PADDING_SIZE);
1361
            if(st->codec->extradata == NULL)
1362
                return AVERROR(ENOMEM);
1363
            st->codec->extradata_size = track->codec_priv.size;
1364
            memcpy(st->codec->extradata,
1365
                   track->codec_priv.data + extradata_offset,
1366
                   track->codec_priv.size);
1367
        }
1368

    
1369
        if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
1370
            st->codec->codec_type = CODEC_TYPE_VIDEO;
1371
            st->codec->codec_tag  = track->video.fourcc;
1372
            st->codec->width  = track->video.pixel_width;
1373
            st->codec->height = track->video.pixel_height;
1374
            av_reduce(&st->sample_aspect_ratio.num,
1375
                      &st->sample_aspect_ratio.den,
1376
                      st->codec->height * track->video.display_width,
1377
                      st->codec-> width * track->video.display_height,
1378
                      255);
1379
            st->need_parsing = AVSTREAM_PARSE_HEADERS;
1380
        } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
1381
            st->codec->codec_type = CODEC_TYPE_AUDIO;
1382
            st->codec->sample_rate = track->audio.out_samplerate;
1383
            st->codec->channels = track->audio.channels;
1384
        } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
1385
            st->codec->codec_type = CODEC_TYPE_SUBTITLE;
1386
        }
1387
    }
1388

    
1389
    attachements = attachements_list->elem;
1390
    for (j=0; j<attachements_list->nb_elem; j++) {
1391
        if (!(attachements[j].filename && attachements[j].mime &&
1392
              attachements[j].bin.data && attachements[j].bin.size > 0)) {
1393
            av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
1394
        } else {
1395
            AVStream *st = av_new_stream(s, 0);
1396
            if (st == NULL)
1397
                break;
1398
            av_metadata_set(&st->metadata, "filename",attachements[j].filename);
1399
            st->codec->codec_id = CODEC_ID_NONE;
1400
            st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
1401
            st->codec->extradata  = av_malloc(attachements[j].bin.size);
1402
            if(st->codec->extradata == NULL)
1403
                break;
1404
            st->codec->extradata_size = attachements[j].bin.size;
1405
            memcpy(st->codec->extradata, attachements[j].bin.data, attachements[j].bin.size);
1406

    
1407
            for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
1408
                if (!strncmp(ff_mkv_mime_tags[i].str, attachements[j].mime,
1409
                             strlen(ff_mkv_mime_tags[i].str))) {
1410
                    st->codec->codec_id = ff_mkv_mime_tags[i].id;
1411
                    break;
1412
                }
1413
            }
1414
            attachements[j].stream = st;
1415
        }
1416
    }
1417

    
1418
    chapters = chapters_list->elem;
1419
    for (i=0; i<chapters_list->nb_elem; i++)
1420
        if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid
1421
            && (max_start==0 || chapters[i].start > max_start)) {
1422
            chapters[i].chapter =
1423
            ff_new_chapter(s, chapters[i].uid, (AVRational){1, 1000000000},
1424
                           chapters[i].start, chapters[i].end,
1425
                           chapters[i].title);
1426
            av_metadata_set(&chapters[i].chapter->metadata,
1427
                            "title", chapters[i].title);
1428
            max_start = chapters[i].start;
1429
        }
1430

    
1431
    index_list = &matroska->index;
1432
    index = index_list->elem;
1433
    if (index_list->nb_elem
1434
        && index[0].time > 100000000000000/matroska->time_scale) {
1435
        av_log(matroska->ctx, AV_LOG_WARNING, "Working around broken index.\n");
1436
        index_scale = matroska->time_scale;
1437
    }
1438
    for (i=0; i<index_list->nb_elem; i++) {
1439
        EbmlList *pos_list = &index[i].pos;
1440
        MatroskaIndexPos *pos = pos_list->elem;
1441
        for (j=0; j<pos_list->nb_elem; j++) {
1442
            MatroskaTrack *track = matroska_find_track_by_num(matroska,
1443
                                                              pos[j].track);
1444
            if (track && track->stream)
1445
                av_add_index_entry(track->stream,
1446
                                   pos[j].pos + matroska->segment_start,
1447
                                   index[i].time/index_scale, 0, 0,
1448
                                   AVINDEX_KEYFRAME);
1449
        }
1450
    }
1451

    
1452
    matroska_convert_tags(s);
1453

    
1454
    return 0;
1455
}
1456

    
1457
/*
1458
 * Put one packet in an application-supplied AVPacket struct.
1459
 * Returns 0 on success or -1 on failure.
1460
 */
1461
static int matroska_deliver_packet(MatroskaDemuxContext *matroska,
1462
                                   AVPacket *pkt)
1463
{
1464
    if (matroska->num_packets > 0) {
1465
        memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
1466
        av_free(matroska->packets[0]);
1467
        if (matroska->num_packets > 1) {
1468
            memmove(&matroska->packets[0], &matroska->packets[1],
1469
                    (matroska->num_packets - 1) * sizeof(AVPacket *));
1470
            matroska->packets =
1471
                av_realloc(matroska->packets, (matroska->num_packets - 1) *
1472
                           sizeof(AVPacket *));
1473
        } else {
1474
            av_freep(&matroska->packets);
1475
        }
1476
        matroska->num_packets--;
1477
        return 0;
1478
    }
1479

    
1480
    return -1;
1481
}
1482

    
1483
/*
1484
 * Free all packets in our internal queue.
1485
 */
1486
static void matroska_clear_queue(MatroskaDemuxContext *matroska)
1487
{
1488
    if (matroska->packets) {
1489
        int n;
1490
        for (n = 0; n < matroska->num_packets; n++) {
1491
            av_free_packet(matroska->packets[n]);
1492
            av_free(matroska->packets[n]);
1493
        }
1494
        av_freep(&matroska->packets);
1495
        matroska->num_packets = 0;
1496
    }
1497
}
1498

    
1499
static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data,
1500
                                int size, int64_t pos, uint64_t cluster_time,
1501
                                uint64_t duration, int is_keyframe,
1502
                                int64_t cluster_pos)
1503
{
1504
    uint64_t timecode = AV_NOPTS_VALUE;
1505
    MatroskaTrack *track;
1506
    int res = 0;
1507
    AVStream *st;
1508
    AVPacket *pkt;
1509
    int16_t block_time;
1510
    uint32_t *lace_size = NULL;
1511
    int n, flags, laces = 0;
1512
    uint64_t num;
1513

    
1514
    if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) {
1515
        av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
1516
        return res;
1517
    }
1518
    data += n;
1519
    size -= n;
1520

    
1521
    track = matroska_find_track_by_num(matroska, num);
1522
    if (size <= 3 || !track || !track->stream) {
1523
        av_log(matroska->ctx, AV_LOG_INFO,
1524
               "Invalid stream %"PRIu64" or size %u\n", num, size);
1525
        return res;
1526
    }
1527
    st = track->stream;
1528
    if (st->discard >= AVDISCARD_ALL)
1529
        return res;
1530
    if (duration == AV_NOPTS_VALUE)
1531
        duration = track->default_duration / matroska->time_scale;
1532

    
1533
    block_time = AV_RB16(data);
1534
    data += 2;
1535
    flags = *data++;
1536
    size -= 3;
1537
    if (is_keyframe == -1)
1538
        is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
1539

    
1540
    if (cluster_time != (uint64_t)-1
1541
        && (block_time >= 0 || cluster_time >= -block_time)) {
1542
        timecode = cluster_time + block_time;
1543
        if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE
1544
            && timecode < track->end_timecode)
1545
            is_keyframe = 0;  /* overlapping subtitles are not key frame */
1546
        if (is_keyframe)
1547
            av_add_index_entry(st, cluster_pos, timecode, 0,0,AVINDEX_KEYFRAME);
1548
        track->end_timecode = FFMAX(track->end_timecode, timecode+duration);
1549
    }
1550

    
1551
    if (matroska->skip_to_keyframe && track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
1552
        if (!is_keyframe || timecode < matroska->skip_to_timecode)
1553
            return res;
1554
        matroska->skip_to_keyframe = 0;
1555
    }
1556

    
1557
    switch ((flags & 0x06) >> 1) {
1558
        case 0x0: /* no lacing */
1559
            laces = 1;
1560
            lace_size = av_mallocz(sizeof(int));
1561
            lace_size[0] = size;
1562
            break;
1563

    
1564
        case 0x1: /* Xiph lacing */
1565
        case 0x2: /* fixed-size lacing */
1566
        case 0x3: /* EBML lacing */
1567
            assert(size>0); // size <=3 is checked before size-=3 above
1568
            laces = (*data) + 1;
1569
            data += 1;
1570
            size -= 1;
1571
            lace_size = av_mallocz(laces * sizeof(int));
1572

    
1573
            switch ((flags & 0x06) >> 1) {
1574
                case 0x1: /* Xiph lacing */ {
1575
                    uint8_t temp;
1576
                    uint32_t total = 0;
1577
                    for (n = 0; res == 0 && n < laces - 1; n++) {
1578
                        while (1) {
1579
                            if (size == 0) {
1580
                                res = -1;
1581
                                break;
1582
                            }
1583
                            temp = *data;
1584
                            lace_size[n] += temp;
1585
                            data += 1;
1586
                            size -= 1;
1587
                            if (temp != 0xff)
1588
                                break;
1589
                        }
1590
                        total += lace_size[n];
1591
                    }
1592
                    lace_size[n] = size - total;
1593
                    break;
1594
                }
1595

    
1596
                case 0x2: /* fixed-size lacing */
1597
                    for (n = 0; n < laces; n++)
1598
                        lace_size[n] = size / laces;
1599
                    break;
1600

    
1601
                case 0x3: /* EBML lacing */ {
1602
                    uint32_t total;
1603
                    n = matroska_ebmlnum_uint(matroska, data, size, &num);
1604
                    if (n < 0) {
1605
                        av_log(matroska->ctx, AV_LOG_INFO,
1606
                               "EBML block data error\n");
1607
                        break;
1608
                    }
1609
                    data += n;
1610
                    size -= n;
1611
                    total = lace_size[0] = num;
1612
                    for (n = 1; res == 0 && n < laces - 1; n++) {
1613
                        int64_t snum;
1614
                        int r;
1615
                        r = matroska_ebmlnum_sint(matroska, data, size, &snum);
1616
                        if (r < 0) {
1617
                            av_log(matroska->ctx, AV_LOG_INFO,
1618
                                   "EBML block data error\n");
1619
                            break;
1620
                        }
1621
                        data += r;
1622
                        size -= r;
1623
                        lace_size[n] = lace_size[n - 1] + snum;
1624
                        total += lace_size[n];
1625
                    }
1626
                    lace_size[n] = size - total;
1627
                    break;
1628
                }
1629
            }
1630
            break;
1631
    }
1632

    
1633
    if (res == 0) {
1634
        for (n = 0; n < laces; n++) {
1635
            if (st->codec->codec_id == CODEC_ID_RA_288 ||
1636
                st->codec->codec_id == CODEC_ID_COOK ||
1637
                st->codec->codec_id == CODEC_ID_ATRAC3) {
1638
                int a = st->codec->block_align;
1639
                int sps = track->audio.sub_packet_size;
1640
                int cfs = track->audio.coded_framesize;
1641
                int h = track->audio.sub_packet_h;
1642
                int y = track->audio.sub_packet_cnt;
1643
                int w = track->audio.frame_size;
1644
                int x;
1645

    
1646
                if (!track->audio.pkt_cnt) {
1647
                    if (st->codec->codec_id == CODEC_ID_RA_288)
1648
                        for (x=0; x<h/2; x++)
1649
                            memcpy(track->audio.buf+x*2*w+y*cfs,
1650
                                   data+x*cfs, cfs);
1651
                    else
1652
                        for (x=0; x<w/sps; x++)
1653
                            memcpy(track->audio.buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
1654

    
1655
                    if (++track->audio.sub_packet_cnt >= h) {
1656
                        track->audio.sub_packet_cnt = 0;
1657
                        track->audio.pkt_cnt = h*w / a;
1658
                    }
1659
                }
1660
                while (track->audio.pkt_cnt) {
1661
                    pkt = av_mallocz(sizeof(AVPacket));
1662
                    av_new_packet(pkt, a);
1663
                    memcpy(pkt->data, track->audio.buf
1664
                           + a * (h*w / a - track->audio.pkt_cnt--), a);
1665
                    pkt->pos = pos;
1666
                    pkt->stream_index = st->index;
1667
                    dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
1668
                }
1669
            } else {
1670
                MatroskaTrackEncoding *encodings = track->encodings.elem;
1671
                int offset = 0, pkt_size = lace_size[n];
1672
                uint8_t *pkt_data = data;
1673

    
1674
                if (encodings && encodings->scope & 1) {
1675
                    offset = matroska_decode_buffer(&pkt_data,&pkt_size, track);
1676
                    if (offset < 0)
1677
                        continue;
1678
                }
1679

    
1680
                pkt = av_mallocz(sizeof(AVPacket));
1681
                /* XXX: prevent data copy... */
1682
                if (av_new_packet(pkt, pkt_size+offset) < 0) {
1683
                    av_free(pkt);
1684
                    res = AVERROR(ENOMEM);
1685
                    break;
1686
                }
1687
                if (offset)
1688
                    memcpy (pkt->data, encodings->compression.settings.data, offset);
1689
                memcpy (pkt->data+offset, pkt_data, pkt_size);
1690

    
1691
                if (pkt_data != data)
1692
                    av_free(pkt_data);
1693

    
1694
                if (n == 0)
1695
                    pkt->flags = is_keyframe;
1696
                pkt->stream_index = st->index;
1697

    
1698
                pkt->pts = timecode;
1699
                pkt->pos = pos;
1700
                if (st->codec->codec_id == CODEC_ID_TEXT)
1701
                    pkt->convergence_duration = duration;
1702
                else if (track->type != MATROSKA_TRACK_TYPE_SUBTITLE)
1703
                    pkt->duration = duration;
1704

    
1705
                if (st->codec->codec_id == CODEC_ID_SSA)
1706
                    matroska_fix_ass_packet(matroska, pkt, duration);
1707

    
1708
                if (matroska->prev_pkt &&
1709
                    timecode != AV_NOPTS_VALUE &&
1710
                    matroska->prev_pkt->pts == timecode &&
1711
                    matroska->prev_pkt->stream_index == st->index)
1712
                    matroska_merge_packets(matroska->prev_pkt, pkt);
1713
                else {
1714
                    dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
1715
                    matroska->prev_pkt = pkt;
1716
                }
1717
            }
1718

    
1719
            if (timecode != AV_NOPTS_VALUE)
1720
                timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
1721
            data += lace_size[n];
1722
        }
1723
    }
1724

    
1725
    av_free(lace_size);
1726
    return res;
1727
}
1728

    
1729
static int matroska_parse_cluster(MatroskaDemuxContext *matroska)
1730
{
1731
    MatroskaCluster cluster = { 0 };
1732
    EbmlList *blocks_list;
1733
    MatroskaBlock *blocks;
1734
    int i, res;
1735
    int64_t pos = url_ftell(matroska->ctx->pb);
1736
    matroska->prev_pkt = NULL;
1737
    if (matroska->has_cluster_id){
1738
        /* For the first cluster we parse, its ID was already read as
1739
           part of matroska_read_header(), so don't read it again */
1740
        res = ebml_parse_id(matroska, matroska_clusters,
1741
                            MATROSKA_ID_CLUSTER, &cluster);
1742
        pos -= 4;  /* sizeof the ID which was already read */
1743
        matroska->has_cluster_id = 0;
1744
    } else
1745
        res = ebml_parse(matroska, matroska_clusters, &cluster);
1746
    blocks_list = &cluster.blocks;
1747
    blocks = blocks_list->elem;
1748
    for (i=0; i<blocks_list->nb_elem; i++)
1749
        if (blocks[i].bin.size > 0) {
1750
            int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1;
1751
            res=matroska_parse_block(matroska,
1752
                                     blocks[i].bin.data, blocks[i].bin.size,
1753
                                     blocks[i].bin.pos,  cluster.timecode,
1754
                                     blocks[i].duration, is_keyframe,
1755
                                     pos);
1756
        }
1757
    ebml_free(matroska_cluster, &cluster);
1758
    if (res < 0)  matroska->done = 1;
1759
    return res;
1760
}
1761

    
1762
static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt)
1763
{
1764
    MatroskaDemuxContext *matroska = s->priv_data;
1765

    
1766
    while (matroska_deliver_packet(matroska, pkt)) {
1767
        if (matroska->done)
1768
            return AVERROR_EOF;
1769
        matroska_parse_cluster(matroska);
1770
    }
1771

    
1772
    return 0;
1773
}
1774

    
1775
static int matroska_read_seek(AVFormatContext *s, int stream_index,
1776
                              int64_t timestamp, int flags)
1777
{
1778
    MatroskaDemuxContext *matroska = s->priv_data;
1779
    MatroskaTrack *tracks = matroska->tracks.elem;
1780
    AVStream *st = s->streams[stream_index];
1781
    int i, index, index_sub, index_min;
1782

    
1783
    if (!st->nb_index_entries)
1784
        return 0;
1785
    timestamp = FFMAX(timestamp, st->index_entries[0].timestamp);
1786

    
1787
    if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
1788
        url_fseek(s->pb, st->index_entries[st->nb_index_entries-1].pos, SEEK_SET);
1789
        while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
1790
            matroska_clear_queue(matroska);
1791
            if (matroska_parse_cluster(matroska) < 0)
1792
                break;
1793
        }
1794
    }
1795

    
1796
    matroska_clear_queue(matroska);
1797
    if (index < 0)
1798
        return 0;
1799

    
1800
    index_min = index;
1801
    for (i=0; i < matroska->tracks.nb_elem; i++) {
1802
        tracks[i].end_timecode = 0;
1803
        if (tracks[i].type == MATROSKA_TRACK_TYPE_SUBTITLE
1804
            && !tracks[i].stream->discard != AVDISCARD_ALL) {
1805
            index_sub = av_index_search_timestamp(tracks[i].stream, st->index_entries[index].timestamp, AVSEEK_FLAG_BACKWARD);
1806
            if (index_sub >= 0
1807
                && st->index_entries[index_sub].pos < st->index_entries[index_min].pos
1808
                && st->index_entries[index].timestamp - st->index_entries[index_sub].timestamp < 30000000000/matroska->time_scale)
1809
                index_min = index_sub;
1810
        }
1811
    }
1812

    
1813
    url_fseek(s->pb, st->index_entries[index_min].pos, SEEK_SET);
1814
    matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
1815
    matroska->skip_to_timecode = st->index_entries[index].timestamp;
1816
    matroska->done = 0;
1817
    av_update_cur_dts(s, st, st->index_entries[index].timestamp);
1818
    return 0;
1819
}
1820

    
1821
static int matroska_read_close(AVFormatContext *s)
1822
{
1823
    MatroskaDemuxContext *matroska = s->priv_data;
1824
    MatroskaTrack *tracks = matroska->tracks.elem;
1825
    int n;
1826

    
1827
    matroska_clear_queue(matroska);
1828

    
1829
    for (n=0; n < matroska->tracks.nb_elem; n++)
1830
        if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO)
1831
            av_free(tracks[n].audio.buf);
1832
    ebml_free(matroska_segment, matroska);
1833

    
1834
    return 0;
1835
}
1836

    
1837
AVInputFormat matroska_demuxer = {
1838
    "matroska",
1839
    NULL_IF_CONFIG_SMALL("Matroska file format"),
1840
    sizeof(MatroskaDemuxContext),
1841
    matroska_probe,
1842
    matroska_read_header,
1843
    matroska_read_packet,
1844
    matroska_read_close,
1845
    matroska_read_seek,
1846
    .metadata_conv = ff_mkv_metadata_conv,
1847
};