matroskadec: initial support for ordered chapters.
[FFMpeg-mirror/ordered_chapters.git] / libavformat / matroskadec.c
blob20200c6b668b474a3d509a99ab3aa213f241e53b
1 /*
2 * Matroska file demuxer
3 * Copyright (c) 2003-2008 The FFmpeg Project
5 * This file is part of FFmpeg.
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.
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.
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
22 /**
23 * @file 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/.
31 #include <stdio.h>
32 #include "avformat.h"
33 /* For 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 #include "libavcodec/xiph.h"
43 #if CONFIG_ZLIB
44 #include <zlib.h>
45 #endif
46 #if CONFIG_BZLIB
47 #include <bzlib.h>
48 #endif
49 #include <libgen.h>
51 typedef 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;
63 typedef 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;
76 typedef struct {
77 int nb_elem;
78 void *elem;
79 } EbmlList;
81 typedef struct {
82 int size;
83 uint8_t *data;
84 int64_t pos;
85 } EbmlBin;
87 typedef 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;
95 typedef struct {
96 uint64_t algo;
97 EbmlBin settings;
98 } MatroskaTrackCompression;
100 typedef struct {
101 uint64_t scope;
102 uint64_t type;
103 MatroskaTrackCompression compression;
104 } MatroskaTrackEncoding;
106 typedef 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;
115 typedef struct {
116 double samplerate;
117 double out_samplerate;
118 uint64_t bitdepth;
119 uint64_t channels;
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;
131 typedef struct {
132 uint64_t num;
133 uint64_t type;
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;
144 AVStream *stream;
145 int64_t end_timecode;
146 } MatroskaTrack;
148 typedef struct {
149 char *filename;
150 char *mime;
151 EbmlBin bin;
152 } MatroskaAttachement;
154 typedef struct {
155 EbmlBin segment_uid; //< This segment's UID
156 AVFormatContext *ctx; //< This segment's context
157 } MatroskaSegment;
159 typedef struct {
160 uint64_t start;
161 uint64_t end;
162 uint64_t uid;
163 char *title;
164 EbmlBin segment_uid;
165 MatroskaSegment *segment;//< a segment containing this chapter
166 } MatroskaChapter;
168 typedef struct {
169 uint64_t track;
170 uint64_t pos;
171 } MatroskaIndexPos;
173 typedef struct {
174 uint64_t time;
175 EbmlList pos;
176 } MatroskaIndex;
178 typedef struct {
179 char *name;
180 char *string;
181 EbmlList sub;
182 } MatroskaTag;
184 typedef struct {
185 uint64_t id;
186 uint64_t pos;
187 } MatroskaSeekhead;
189 typedef struct {
190 uint64_t start;
191 uint64_t length;
192 } MatroskaLevel;
194 typedef struct {
195 AVFormatContext *ctx;
197 /* EBML stuff */
198 int num_levels;
199 MatroskaLevel levels[EBML_MAX_DEPTH];
200 int level_up;
202 uint64_t time_scale;
203 double duration;
204 char *title;
205 EbmlBin segment_uid; //< this segment's uid
206 EbmlList tracks;
207 EbmlList attachments;
208 EbmlList chapters;
209 EbmlList index;
210 EbmlList tags;
211 EbmlList seekhead;
213 /* byte position of the segment inside the stream */
214 int64_t segment_start;
216 /* the packet queue */
217 AVPacket **packets;
218 int num_packets;
219 AVPacket *prev_pkt;
221 int done;
222 int has_cluster_id;
224 /* What to skip before effectively reading a packet. */
225 int skip_to_keyframe;
226 uint64_t skip_to_timecode;
228 /* ordered chapters stuff */
229 uint64_t ordered_chapters; //< ordered edition found?
230 int current_chapter;
231 MatroskaSegment *segment; //< this segment
232 MatroskaSegment **linked_segments;
233 int nb_linked_segments;
235 int default_stream;
236 } MatroskaDemuxContext;
238 typedef struct {
239 uint64_t duration;
240 int64_t reference;
241 EbmlBin bin;
242 } MatroskaBlock;
244 typedef struct {
245 uint64_t timecode;
246 EbmlList blocks;
247 } MatroskaCluster;
249 static EbmlSyntax ebml_header[] = {
250 { EBML_ID_EBMLREADVERSION, EBML_UINT, 0, offsetof(Ebml,version), {.u=EBML_VERSION} },
251 { EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, offsetof(Ebml,max_size), {.u=8} },
252 { EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, offsetof(Ebml,id_length), {.u=4} },
253 { EBML_ID_DOCTYPE, EBML_STR, 0, offsetof(Ebml,doctype), {.s="(none)"} },
254 { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, offsetof(Ebml,doctype_version), {.u=1} },
255 { EBML_ID_EBMLVERSION, EBML_NONE },
256 { EBML_ID_DOCTYPEVERSION, EBML_NONE },
257 { 0 }
260 static EbmlSyntax ebml_syntax[] = {
261 { EBML_ID_HEADER, EBML_NEST, 0, 0, {.n=ebml_header} },
262 { 0 }
265 static EbmlSyntax matroska_info[] = {
266 { MATROSKA_ID_TIMECODESCALE, EBML_UINT, 0, offsetof(MatroskaDemuxContext,time_scale), {.u=1000000} },
267 { MATROSKA_ID_DURATION, EBML_FLOAT, 0, offsetof(MatroskaDemuxContext,duration) },
268 { MATROSKA_ID_TITLE, EBML_UTF8, 0, offsetof(MatroskaDemuxContext,title) },
269 { MATROSKA_ID_SEGMENTUID, EBML_BIN, 0, offsetof(MatroskaDemuxContext, segment_uid) },
270 { MATROSKA_ID_WRITINGAPP, EBML_NONE },
271 { MATROSKA_ID_MUXINGAPP, EBML_NONE },
272 { MATROSKA_ID_DATEUTC, EBML_NONE },
273 { 0 }
276 static EbmlSyntax matroska_track_video[] = {
277 { MATROSKA_ID_VIDEOFRAMERATE, EBML_FLOAT,0, offsetof(MatroskaTrackVideo,frame_rate) },
278 { MATROSKA_ID_VIDEODISPLAYWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_width) },
279 { MATROSKA_ID_VIDEODISPLAYHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_height) },
280 { MATROSKA_ID_VIDEOPIXELWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_width) },
281 { MATROSKA_ID_VIDEOPIXELHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_height) },
282 { MATROSKA_ID_VIDEOCOLORSPACE, EBML_UINT, 0, offsetof(MatroskaTrackVideo,fourcc) },
283 { MATROSKA_ID_VIDEOPIXELCROPB, EBML_NONE },
284 { MATROSKA_ID_VIDEOPIXELCROPT, EBML_NONE },
285 { MATROSKA_ID_VIDEOPIXELCROPL, EBML_NONE },
286 { MATROSKA_ID_VIDEOPIXELCROPR, EBML_NONE },
287 { MATROSKA_ID_VIDEODISPLAYUNIT, EBML_NONE },
288 { MATROSKA_ID_VIDEOFLAGINTERLACED,EBML_NONE },
289 { MATROSKA_ID_VIDEOSTEREOMODE, EBML_NONE },
290 { MATROSKA_ID_VIDEOASPECTRATIO, EBML_NONE },
291 { 0 }
294 static EbmlSyntax matroska_track_audio[] = {
295 { MATROSKA_ID_AUDIOSAMPLINGFREQ, EBML_FLOAT,0, offsetof(MatroskaTrackAudio,samplerate), {.f=8000.0} },
296 { MATROSKA_ID_AUDIOOUTSAMPLINGFREQ,EBML_FLOAT,0,offsetof(MatroskaTrackAudio,out_samplerate) },
297 { MATROSKA_ID_AUDIOBITDEPTH, EBML_UINT, 0, offsetof(MatroskaTrackAudio,bitdepth) },
298 { MATROSKA_ID_AUDIOCHANNELS, EBML_UINT, 0, offsetof(MatroskaTrackAudio,channels), {.u=1} },
299 { 0 }
302 static EbmlSyntax matroska_track_encoding_compression[] = {
303 { MATROSKA_ID_ENCODINGCOMPALGO, EBML_UINT, 0, offsetof(MatroskaTrackCompression,algo), {.u=0} },
304 { MATROSKA_ID_ENCODINGCOMPSETTINGS,EBML_BIN, 0, offsetof(MatroskaTrackCompression,settings) },
305 { 0 }
308 static EbmlSyntax matroska_track_encoding[] = {
309 { MATROSKA_ID_ENCODINGSCOPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,scope), {.u=1} },
310 { MATROSKA_ID_ENCODINGTYPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,type), {.u=0} },
311 { MATROSKA_ID_ENCODINGCOMPRESSION,EBML_NEST, 0, offsetof(MatroskaTrackEncoding,compression), {.n=matroska_track_encoding_compression} },
312 { MATROSKA_ID_ENCODINGORDER, EBML_NONE },
313 { 0 }
316 static EbmlSyntax matroska_track_encodings[] = {
317 { MATROSKA_ID_TRACKCONTENTENCODING, EBML_NEST, sizeof(MatroskaTrackEncoding), offsetof(MatroskaTrack,encodings), {.n=matroska_track_encoding} },
318 { 0 }
321 static EbmlSyntax matroska_track[] = {
322 { MATROSKA_ID_TRACKNUMBER, EBML_UINT, 0, offsetof(MatroskaTrack,num) },
323 { MATROSKA_ID_TRACKTYPE, EBML_UINT, 0, offsetof(MatroskaTrack,type) },
324 { MATROSKA_ID_CODECID, EBML_STR, 0, offsetof(MatroskaTrack,codec_id) },
325 { MATROSKA_ID_CODECPRIVATE, EBML_BIN, 0, offsetof(MatroskaTrack,codec_priv) },
326 { MATROSKA_ID_TRACKLANGUAGE, EBML_UTF8, 0, offsetof(MatroskaTrack,language), {.s="eng"} },
327 { MATROSKA_ID_TRACKDEFAULTDURATION, EBML_UINT, 0, offsetof(MatroskaTrack,default_duration) },
328 { MATROSKA_ID_TRACKTIMECODESCALE, EBML_FLOAT,0, offsetof(MatroskaTrack,time_scale), {.f=1.0} },
329 { MATROSKA_ID_TRACKFLAGDEFAULT, EBML_UINT, 0, offsetof(MatroskaTrack,flag_default), {.u=1} },
330 { MATROSKA_ID_TRACKVIDEO, EBML_NEST, 0, offsetof(MatroskaTrack,video), {.n=matroska_track_video} },
331 { MATROSKA_ID_TRACKAUDIO, EBML_NEST, 0, offsetof(MatroskaTrack,audio), {.n=matroska_track_audio} },
332 { MATROSKA_ID_TRACKCONTENTENCODINGS,EBML_NEST, 0, 0, {.n=matroska_track_encodings} },
333 { MATROSKA_ID_TRACKUID, EBML_NONE },
334 { MATROSKA_ID_TRACKNAME, EBML_NONE },
335 { MATROSKA_ID_TRACKFLAGENABLED, EBML_NONE },
336 { MATROSKA_ID_TRACKFLAGFORCED, EBML_NONE },
337 { MATROSKA_ID_TRACKFLAGLACING, EBML_NONE },
338 { MATROSKA_ID_CODECNAME, EBML_NONE },
339 { MATROSKA_ID_CODECDECODEALL, EBML_NONE },
340 { MATROSKA_ID_CODECINFOURL, EBML_NONE },
341 { MATROSKA_ID_CODECDOWNLOADURL, EBML_NONE },
342 { MATROSKA_ID_TRACKMINCACHE, EBML_NONE },
343 { MATROSKA_ID_TRACKMAXCACHE, EBML_NONE },
344 { MATROSKA_ID_TRACKMAXBLKADDID, EBML_NONE },
345 { 0 }
348 static EbmlSyntax matroska_tracks[] = {
349 { MATROSKA_ID_TRACKENTRY, EBML_NEST, sizeof(MatroskaTrack), offsetof(MatroskaDemuxContext,tracks), {.n=matroska_track} },
350 { 0 }
353 static EbmlSyntax matroska_attachment[] = {
354 { MATROSKA_ID_FILENAME, EBML_UTF8, 0, offsetof(MatroskaAttachement,filename) },
355 { MATROSKA_ID_FILEMIMETYPE, EBML_STR, 0, offsetof(MatroskaAttachement,mime) },
356 { MATROSKA_ID_FILEDATA, EBML_BIN, 0, offsetof(MatroskaAttachement,bin) },
357 { MATROSKA_ID_FILEDESC, EBML_NONE },
358 { MATROSKA_ID_FILEUID, EBML_NONE },
359 { 0 }
362 static EbmlSyntax matroska_attachments[] = {
363 { MATROSKA_ID_ATTACHEDFILE, EBML_NEST, sizeof(MatroskaAttachement), offsetof(MatroskaDemuxContext,attachments), {.n=matroska_attachment} },
364 { 0 }
367 static EbmlSyntax matroska_chapter_display[] = {
368 { MATROSKA_ID_CHAPSTRING, EBML_UTF8, 0, offsetof(MatroskaChapter,title) },
369 { MATROSKA_ID_CHAPLANG, EBML_NONE },
370 { 0 }
373 static EbmlSyntax matroska_chapter_entry[] = {
374 { MATROSKA_ID_CHAPTERTIMESTART, EBML_UINT, 0, offsetof(MatroskaChapter,start), {.u=AV_NOPTS_VALUE} },
375 { MATROSKA_ID_CHAPTERTIMEEND, EBML_UINT, 0, offsetof(MatroskaChapter,end), {.u=AV_NOPTS_VALUE} },
376 { MATROSKA_ID_CHAPTERUID, EBML_UINT, 0, offsetof(MatroskaChapter,uid) },
377 { MATROSKA_ID_CHAPTERDISPLAY, EBML_NEST, 0, 0, {.n=matroska_chapter_display} },
378 { MATROSKA_ID_CHAPTERSEGMENTUID, EBML_BIN, 0, offsetof(MatroskaChapter, segment_uid) },
379 { MATROSKA_ID_CHAPTERFLAGHIDDEN, EBML_NONE },
380 { MATROSKA_ID_CHAPTERFLAGENABLED, EBML_NONE },
381 { MATROSKA_ID_CHAPTERPHYSEQUIV, EBML_NONE },
382 { MATROSKA_ID_CHAPTERATOM, EBML_NONE },
383 { 0 }
386 static EbmlSyntax matroska_chapter[] = {
387 { MATROSKA_ID_CHAPTERATOM, EBML_NEST, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext,chapters), {.n=matroska_chapter_entry} },
388 { MATROSKA_ID_EDITIONFLAGORDERED, EBML_UINT, 0, offsetof(MatroskaDemuxContext, ordered_chapters), {.u=0} },
389 { MATROSKA_ID_EDITIONUID, EBML_NONE },
390 { MATROSKA_ID_EDITIONFLAGHIDDEN, EBML_NONE },
391 { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE },
392 { 0 }
395 static EbmlSyntax matroska_chapters[] = {
396 { MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, 0, {.n=matroska_chapter} },
397 { 0 }
400 static EbmlSyntax matroska_index_pos[] = {
401 { MATROSKA_ID_CUETRACK, EBML_UINT, 0, offsetof(MatroskaIndexPos,track) },
402 { MATROSKA_ID_CUECLUSTERPOSITION, EBML_UINT, 0, offsetof(MatroskaIndexPos,pos) },
403 { MATROSKA_ID_CUEBLOCKNUMBER, EBML_NONE },
404 { 0 }
407 static EbmlSyntax matroska_index_entry[] = {
408 { MATROSKA_ID_CUETIME, EBML_UINT, 0, offsetof(MatroskaIndex,time) },
409 { MATROSKA_ID_CUETRACKPOSITION, EBML_NEST, sizeof(MatroskaIndexPos), offsetof(MatroskaIndex,pos), {.n=matroska_index_pos} },
410 { 0 }
413 static EbmlSyntax matroska_index[] = {
414 { MATROSKA_ID_POINTENTRY, EBML_NEST, sizeof(MatroskaIndex), offsetof(MatroskaDemuxContext,index), {.n=matroska_index_entry} },
415 { 0 }
418 static EbmlSyntax matroska_simpletag[] = {
419 { MATROSKA_ID_TAGNAME, EBML_UTF8, 0, offsetof(MatroskaTag,name) },
420 { MATROSKA_ID_TAGSTRING, EBML_UTF8, 0, offsetof(MatroskaTag,string) },
421 { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTag,sub), {.n=matroska_simpletag} },
422 { MATROSKA_ID_TAGLANG, EBML_NONE },
423 { MATROSKA_ID_TAGDEFAULT, EBML_NONE },
424 { 0 }
427 static EbmlSyntax matroska_tag[] = {
428 { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), 0, {.n=matroska_simpletag} },
429 { MATROSKA_ID_TAGTARGETS, EBML_NONE },
430 { 0 }
433 static EbmlSyntax matroska_tags[] = {
434 { MATROSKA_ID_TAG, EBML_NEST, 0, offsetof(MatroskaDemuxContext,tags), {.n=matroska_tag} },
435 { 0 }
438 static EbmlSyntax matroska_seekhead_entry[] = {
439 { MATROSKA_ID_SEEKID, EBML_UINT, 0, offsetof(MatroskaSeekhead,id) },
440 { MATROSKA_ID_SEEKPOSITION, EBML_UINT, 0, offsetof(MatroskaSeekhead,pos), {.u=-1} },
441 { 0 }
444 static EbmlSyntax matroska_seekhead[] = {
445 { MATROSKA_ID_SEEKENTRY, EBML_NEST, sizeof(MatroskaSeekhead), offsetof(MatroskaDemuxContext,seekhead), {.n=matroska_seekhead_entry} },
446 { 0 }
449 static EbmlSyntax matroska_segment[] = {
450 { MATROSKA_ID_INFO, EBML_NEST, 0, 0, {.n=matroska_info } },
451 { MATROSKA_ID_TRACKS, EBML_NEST, 0, 0, {.n=matroska_tracks } },
452 { MATROSKA_ID_ATTACHMENTS, EBML_NEST, 0, 0, {.n=matroska_attachments} },
453 { MATROSKA_ID_CHAPTERS, EBML_NEST, 0, 0, {.n=matroska_chapters } },
454 { MATROSKA_ID_CUES, EBML_NEST, 0, 0, {.n=matroska_index } },
455 { MATROSKA_ID_TAGS, EBML_NEST, 0, 0, {.n=matroska_tags } },
456 { MATROSKA_ID_SEEKHEAD, EBML_NEST, 0, 0, {.n=matroska_seekhead } },
457 { MATROSKA_ID_CLUSTER, EBML_STOP, 0, offsetof(MatroskaDemuxContext,has_cluster_id) },
458 { 0 }
461 static EbmlSyntax matroska_segments[] = {
462 { MATROSKA_ID_SEGMENT, EBML_NEST, 0, 0, {.n=matroska_segment } },
463 { 0 }
466 static EbmlSyntax matroska_blockgroup[] = {
467 { MATROSKA_ID_BLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) },
468 { MATROSKA_ID_SIMPLEBLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) },
469 { MATROSKA_ID_BLOCKDURATION, EBML_UINT, 0, offsetof(MatroskaBlock,duration), {.u=AV_NOPTS_VALUE} },
470 { MATROSKA_ID_BLOCKREFERENCE, EBML_UINT, 0, offsetof(MatroskaBlock,reference) },
471 { 0 }
474 static EbmlSyntax matroska_cluster[] = {
475 { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) },
476 { MATROSKA_ID_BLOCKGROUP, EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
477 { MATROSKA_ID_SIMPLEBLOCK, EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
478 { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE },
479 { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE },
480 { 0 }
483 static EbmlSyntax matroska_clusters[] = {
484 { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster} },
485 { MATROSKA_ID_INFO, EBML_NONE },
486 { MATROSKA_ID_CUES, EBML_NONE },
487 { MATROSKA_ID_TAGS, EBML_NONE },
488 { MATROSKA_ID_SEEKHEAD, EBML_NONE },
489 { 0 }
492 #define SIZE_OFF(x) sizeof(((AVFormatContext*)0)->x),offsetof(AVFormatContext,x)
493 const struct {
494 const char name[16];
495 int size;
496 int offset;
497 } metadata[] = {
498 { "TITLE", SIZE_OFF(title) },
499 { "ARTIST", SIZE_OFF(author) },
500 { "WRITTEN_BY", SIZE_OFF(author) },
501 { "LEAD_PERFORMER", SIZE_OFF(author) },
502 { "COPYRIGHT", SIZE_OFF(copyright) },
503 { "COMMENT", SIZE_OFF(comment) },
504 { "ALBUM", SIZE_OFF(album) },
505 { "DATE_WRITTEN", SIZE_OFF(year) },
506 { "DATE_RELEASED", SIZE_OFF(year) },
507 { "PART_NUMBER", SIZE_OFF(track) },
508 { "GENRE", SIZE_OFF(genre) },
512 * Return: Whether we reached the end of a level in the hierarchy or not.
514 static int ebml_level_end(MatroskaDemuxContext *matroska)
516 ByteIOContext *pb = matroska->ctx->pb;
517 int64_t pos = url_ftell(pb);
519 if (matroska->num_levels > 0) {
520 MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
521 if (pos - level->start >= level->length) {
522 matroska->num_levels--;
523 return 1;
526 return 0;
530 * Read: an "EBML number", which is defined as a variable-length
531 * array of bytes. The first byte indicates the length by giving a
532 * number of 0-bits followed by a one. The position of the first
533 * "one" bit inside the first byte indicates the length of this
534 * number.
535 * Returns: number of bytes read, < 0 on error
537 static int ebml_read_num(MatroskaDemuxContext *matroska, ByteIOContext *pb,
538 int max_size, uint64_t *number)
540 int len_mask = 0x80, read = 1, n = 1;
541 int64_t total = 0;
543 /* The first byte tells us the length in bytes - get_byte() can normally
544 * return 0, but since that's not a valid first ebmlID byte, we can
545 * use it safely here to catch EOS. */
546 if (!(total = get_byte(pb))) {
547 /* we might encounter EOS here */
548 if (!url_feof(pb)) {
549 int64_t pos = url_ftell(pb);
550 av_log(matroska->ctx, AV_LOG_ERROR,
551 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
552 pos, pos);
554 return AVERROR(EIO); /* EOS or actual I/O error */
557 /* get the length of the EBML number */
558 while (read <= max_size && !(total & len_mask)) {
559 read++;
560 len_mask >>= 1;
562 if (read > max_size) {
563 int64_t pos = url_ftell(pb) - 1;
564 av_log(matroska->ctx, AV_LOG_ERROR,
565 "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
566 (uint8_t) total, pos, pos);
567 return AVERROR_INVALIDDATA;
570 /* read out length */
571 total &= ~len_mask;
572 while (n++ < read)
573 total = (total << 8) | get_byte(pb);
575 *number = total;
577 return read;
581 * Read the next element as an unsigned int.
582 * 0 is success, < 0 is failure.
584 static int ebml_read_uint(ByteIOContext *pb, int size, uint64_t *num)
586 int n = 0;
588 if (size < 1 || size > 8)
589 return AVERROR_INVALIDDATA;
591 /* big-endian ordering; build up number */
592 *num = 0;
593 while (n++ < size)
594 *num = (*num << 8) | get_byte(pb);
596 return 0;
600 * Read the next element as a float.
601 * 0 is success, < 0 is failure.
603 static int ebml_read_float(ByteIOContext *pb, int size, double *num)
605 if (size == 4) {
606 *num= av_int2flt(get_be32(pb));
607 } else if(size==8){
608 *num= av_int2dbl(get_be64(pb));
609 } else
610 return AVERROR_INVALIDDATA;
612 return 0;
616 * Read the next element as an ASCII string.
617 * 0 is success, < 0 is failure.
619 static int ebml_read_ascii(ByteIOContext *pb, int size, char **str)
621 av_free(*str);
622 /* EBML strings are usually not 0-terminated, so we allocate one
623 * byte more, read the string and NULL-terminate it ourselves. */
624 if (!(*str = av_malloc(size + 1)))
625 return AVERROR(ENOMEM);
626 if (get_buffer(pb, (uint8_t *) *str, size) != size) {
627 av_free(*str);
628 return AVERROR(EIO);
630 (*str)[size] = '\0';
632 return 0;
636 * Read the next element as binary data.
637 * 0 is success, < 0 is failure.
639 static int ebml_read_binary(ByteIOContext *pb, int length, EbmlBin *bin)
641 av_free(bin->data);
642 if (!(bin->data = av_malloc(length)))
643 return AVERROR(ENOMEM);
645 bin->size = length;
646 bin->pos = url_ftell(pb);
647 if (get_buffer(pb, bin->data, length) != length)
648 return AVERROR(EIO);
650 return 0;
654 * Read the next element, but only the header. The contents
655 * are supposed to be sub-elements which can be read separately.
656 * 0 is success, < 0 is failure.
658 static int ebml_read_master(MatroskaDemuxContext *matroska, int length)
660 ByteIOContext *pb = matroska->ctx->pb;
661 MatroskaLevel *level;
663 if (matroska->num_levels >= EBML_MAX_DEPTH) {
664 av_log(matroska->ctx, AV_LOG_ERROR,
665 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
666 return AVERROR(ENOSYS);
669 level = &matroska->levels[matroska->num_levels++];
670 level->start = url_ftell(pb);
671 level->length = length;
673 return 0;
677 * Read signed/unsigned "EBML" numbers.
678 * Return: number of bytes processed, < 0 on error
680 static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska,
681 uint8_t *data, uint32_t size, uint64_t *num)
683 ByteIOContext pb;
684 init_put_byte(&pb, data, size, 0, NULL, NULL, NULL, NULL);
685 return ebml_read_num(matroska, &pb, 8, num);
689 * Same as above, but signed.
691 static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska,
692 uint8_t *data, uint32_t size, int64_t *num)
694 uint64_t unum;
695 int res;
697 /* read as unsigned number first */
698 if ((res = matroska_ebmlnum_uint(matroska, data, size, &unum)) < 0)
699 return res;
701 /* make signed (weird way) */
702 *num = unum - ((1LL << (7*res - 1)) - 1);
704 return res;
707 static int ebml_parse_elem(MatroskaDemuxContext *matroska,
708 EbmlSyntax *syntax, void *data);
710 static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
711 uint32_t id, void *data)
713 int i;
714 for (i=0; syntax[i].id; i++)
715 if (id == syntax[i].id)
716 break;
717 if (!syntax[i].id && id != EBML_ID_VOID && id != EBML_ID_CRC32)
718 av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id);
719 return ebml_parse_elem(matroska, &syntax[i], data);
722 static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
723 void *data)
725 uint64_t id;
726 int res = ebml_read_num(matroska, matroska->ctx->pb, 4, &id);
727 id |= 1 << 7*res;
728 return res < 0 ? res : ebml_parse_id(matroska, syntax, id, data);
731 static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
732 void *data)
734 int i, res = 0;
736 for (i=0; syntax[i].id; i++)
737 switch (syntax[i].type) {
738 case EBML_UINT:
739 *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u;
740 break;
741 case EBML_FLOAT:
742 *(double *)((char *)data+syntax[i].data_offset) = syntax[i].def.f;
743 break;
744 case EBML_STR:
745 case EBML_UTF8:
746 *(char **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s);
747 break;
750 while (!res && !ebml_level_end(matroska))
751 res = ebml_parse(matroska, syntax, data);
753 return res;
756 static int ebml_parse_elem(MatroskaDemuxContext *matroska,
757 EbmlSyntax *syntax, void *data)
759 ByteIOContext *pb = matroska->ctx->pb;
760 uint32_t id = syntax->id;
761 uint64_t length;
762 int res;
764 data = (char *)data + syntax->data_offset;
765 if (syntax->list_elem_size) {
766 EbmlList *list = data;
767 list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);
768 data = (char*)list->elem + list->nb_elem*syntax->list_elem_size;
769 memset(data, 0, syntax->list_elem_size);
770 list->nb_elem++;
773 if (syntax->type != EBML_PASS && syntax->type != EBML_STOP)
774 if ((res = ebml_read_num(matroska, pb, 8, &length)) < 0)
775 return res;
777 switch (syntax->type) {
778 case EBML_UINT: res = ebml_read_uint (pb, length, data); break;
779 case EBML_FLOAT: res = ebml_read_float (pb, length, data); break;
780 case EBML_STR:
781 case EBML_UTF8: res = ebml_read_ascii (pb, length, data); break;
782 case EBML_BIN: res = ebml_read_binary(pb, length, data); break;
783 case EBML_NEST: if ((res=ebml_read_master(matroska, length)) < 0)
784 return res;
785 if (id == MATROSKA_ID_SEGMENT)
786 matroska->segment_start = url_ftell(matroska->ctx->pb);
787 return ebml_parse_nest(matroska, syntax->def.n, data);
788 case EBML_PASS: return ebml_parse_id(matroska, syntax->def.n, id, data);
789 case EBML_STOP: *(int *)data = 1; return 1;
790 default: return url_fseek(pb,length,SEEK_CUR)<0 ? AVERROR(EIO) : 0;
792 if (res == AVERROR_INVALIDDATA)
793 av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n");
794 else if (res == AVERROR(EIO))
795 av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n");
796 return res;
799 static void ebml_free(EbmlSyntax *syntax, void *data)
801 int i, j;
802 for (i=0; syntax[i].id; i++) {
803 void *data_off = (char *)data + syntax[i].data_offset;
804 switch (syntax[i].type) {
805 case EBML_STR:
806 case EBML_UTF8: av_freep(data_off); break;
807 case EBML_BIN: av_freep(&((EbmlBin *)data_off)->data); break;
808 case EBML_NEST:
809 if (syntax[i].list_elem_size) {
810 EbmlList *list = data_off;
811 char *ptr = list->elem;
812 for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size)
813 ebml_free(syntax[i].def.n, ptr);
814 av_free(list->elem);
815 } else
816 ebml_free(syntax[i].def.n, data_off);
817 default: break;
824 * Autodetecting...
826 static int matroska_probe(AVProbeData *p)
828 uint64_t total = 0;
829 int len_mask = 0x80, size = 1, n = 1;
830 static const char probe_data[] = "matroska";
832 /* EBML header? */
833 if (AV_RB32(p->buf) != EBML_ID_HEADER)
834 return 0;
836 /* length of header */
837 total = p->buf[4];
838 while (size <= 8 && !(total & len_mask)) {
839 size++;
840 len_mask >>= 1;
842 if (size > 8)
843 return 0;
844 total &= (len_mask - 1);
845 while (n < size)
846 total = (total << 8) | p->buf[4 + n++];
848 /* Does the probe data contain the whole header? */
849 if (p->buf_size < 4 + size + total)
850 return 0;
852 /* The header must contain the document type 'matroska'. For now,
853 * we don't parse the whole header but simply check for the
854 * availability of that array of characters inside the header.
855 * Not fully fool-proof, but good enough. */
856 for (n = 4+size; n <= 4+size+total-(sizeof(probe_data)-1); n++)
857 if (!memcmp(p->buf+n, probe_data, sizeof(probe_data)-1))
858 return AVPROBE_SCORE_MAX;
860 return 0;
863 static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska,
864 int num)
866 MatroskaTrack *tracks = matroska->tracks.elem;
867 int i;
869 for (i=0; i < matroska->tracks.nb_elem; i++)
870 if (tracks[i].num == num)
871 return &tracks[i];
873 av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num);
874 return NULL;
877 static int matroska_decode_buffer(uint8_t** buf, int* buf_size,
878 MatroskaTrack *track)
880 MatroskaTrackEncoding *encodings = track->encodings.elem;
881 uint8_t* data = *buf;
882 int isize = *buf_size;
883 uint8_t* pkt_data = NULL;
884 int pkt_size = isize;
885 int result = 0;
886 int olen;
888 switch (encodings[0].compression.algo) {
889 case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
890 return encodings[0].compression.settings.size;
891 case MATROSKA_TRACK_ENCODING_COMP_LZO:
892 do {
893 olen = pkt_size *= 3;
894 pkt_data = av_realloc(pkt_data,
895 pkt_size+LZO_OUTPUT_PADDING);
896 result = lzo1x_decode(pkt_data, &olen, data, &isize);
897 } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
898 if (result)
899 goto failed;
900 pkt_size -= olen;
901 break;
902 #if CONFIG_ZLIB
903 case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
904 z_stream zstream = {0};
905 if (inflateInit(&zstream) != Z_OK)
906 return -1;
907 zstream.next_in = data;
908 zstream.avail_in = isize;
909 do {
910 pkt_size *= 3;
911 pkt_data = av_realloc(pkt_data, pkt_size);
912 zstream.avail_out = pkt_size - zstream.total_out;
913 zstream.next_out = pkt_data + zstream.total_out;
914 result = inflate(&zstream, Z_NO_FLUSH);
915 } while (result==Z_OK && pkt_size<10000000);
916 pkt_size = zstream.total_out;
917 inflateEnd(&zstream);
918 if (result != Z_STREAM_END)
919 goto failed;
920 break;
922 #endif
923 #if CONFIG_BZLIB
924 case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
925 bz_stream bzstream = {0};
926 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
927 return -1;
928 bzstream.next_in = data;
929 bzstream.avail_in = isize;
930 do {
931 pkt_size *= 3;
932 pkt_data = av_realloc(pkt_data, pkt_size);
933 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
934 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
935 result = BZ2_bzDecompress(&bzstream);
936 } while (result==BZ_OK && pkt_size<10000000);
937 pkt_size = bzstream.total_out_lo32;
938 BZ2_bzDecompressEnd(&bzstream);
939 if (result != BZ_STREAM_END)
940 goto failed;
941 break;
943 #endif
944 default:
945 return -1;
948 *buf = pkt_data;
949 *buf_size = pkt_size;
950 return 0;
951 failed:
952 av_free(pkt_data);
953 return -1;
956 static void matroska_fix_ass_packet(MatroskaDemuxContext *matroska,
957 AVPacket *pkt, uint64_t display_duration)
959 char *line, *layer, *ptr = pkt->data, *end = ptr+pkt->size;
960 for (; *ptr!=',' && ptr<end-1; ptr++);
961 if (*ptr == ',')
962 layer = ++ptr;
963 for (; *ptr!=',' && ptr<end-1; ptr++);
964 if (*ptr == ',') {
965 int64_t end_pts = pkt->pts + display_duration;
966 int sc = matroska->time_scale * pkt->pts / 10000000;
967 int ec = matroska->time_scale * end_pts / 10000000;
968 int sh, sm, ss, eh, em, es, len;
969 sh = sc/360000; sc -= 360000*sh;
970 sm = sc/ 6000; sc -= 6000*sm;
971 ss = sc/ 100; sc -= 100*ss;
972 eh = ec/360000; ec -= 360000*eh;
973 em = ec/ 6000; ec -= 6000*em;
974 es = ec/ 100; ec -= 100*es;
975 *ptr++ = '\0';
976 len = 50 + end-ptr + FF_INPUT_BUFFER_PADDING_SIZE;
977 if (!(line = av_malloc(len)))
978 return;
979 snprintf(line,len,"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
980 layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
981 av_free(pkt->data);
982 pkt->data = line;
983 pkt->size = strlen(line);
987 static void matroska_merge_packets(AVPacket *out, AVPacket *in)
989 out->data = av_realloc(out->data, out->size+in->size);
990 memcpy(out->data+out->size, in->data, in->size);
991 out->size += in->size;
992 av_destruct_packet(in);
993 av_free(in);
996 static void matroska_convert_tags(AVFormatContext *s, EbmlList *list)
998 MatroskaTag *tags = list->elem;
999 int i, j;
1001 for (i=0; i < list->nb_elem; i++) {
1002 for (j=0; j < FF_ARRAY_ELEMS(metadata); j++){
1003 if (!strcmp(tags[i].name, metadata[j].name)) {
1004 int *ptr = (int *)((char *)s + metadata[j].offset);
1005 if (*ptr) continue;
1006 if (metadata[j].size > sizeof(int))
1007 av_strlcpy((char *)ptr, tags[i].string, metadata[j].size);
1008 else
1009 *ptr = atoi(tags[i].string);
1012 if (tags[i].sub.nb_elem)
1013 matroska_convert_tags(s, &tags[i].sub);
1017 static void matroska_execute_seekhead(MatroskaDemuxContext *matroska)
1019 EbmlList *seekhead_list = &matroska->seekhead;
1020 MatroskaSeekhead *seekhead = seekhead_list->elem;
1021 uint32_t level_up = matroska->level_up;
1022 int64_t before_pos = url_ftell(matroska->ctx->pb);
1023 MatroskaLevel level;
1024 int i;
1026 for (i=0; i<seekhead_list->nb_elem; i++) {
1027 int64_t offset = seekhead[i].pos + matroska->segment_start;
1029 if (seekhead[i].pos <= before_pos
1030 || seekhead[i].id == MATROSKA_ID_SEEKHEAD
1031 || seekhead[i].id == MATROSKA_ID_CLUSTER)
1032 continue;
1034 /* seek */
1035 if (url_fseek(matroska->ctx->pb, offset, SEEK_SET) != offset)
1036 continue;
1038 /* We don't want to lose our seekhead level, so we add
1039 * a dummy. This is a crude hack. */
1040 if (matroska->num_levels == EBML_MAX_DEPTH) {
1041 av_log(matroska->ctx, AV_LOG_INFO,
1042 "Max EBML element depth (%d) reached, "
1043 "cannot parse further.\n", EBML_MAX_DEPTH);
1044 break;
1047 level.start = 0;
1048 level.length = (uint64_t)-1;
1049 matroska->levels[matroska->num_levels] = level;
1050 matroska->num_levels++;
1052 ebml_parse(matroska, matroska_segment, matroska);
1054 /* remove dummy level */
1055 while (matroska->num_levels) {
1056 uint64_t length = matroska->levels[--matroska->num_levels].length;
1057 if (length == (uint64_t)-1)
1058 break;
1062 /* seek back */
1063 url_fseek(matroska->ctx->pb, before_pos, SEEK_SET);
1064 matroska->level_up = level_up;
1067 static int matroska_aac_profile(char *codec_id)
1069 static const char * const aac_profiles[] = { "MAIN", "LC", "SSR" };
1070 int profile;
1072 for (profile=0; profile<FF_ARRAY_ELEMS(aac_profiles); profile++)
1073 if (strstr(codec_id, aac_profiles[profile]))
1074 break;
1075 return profile + 1;
1078 static int matroska_aac_sri(int samplerate)
1080 int sri;
1082 for (sri=0; sri<FF_ARRAY_ELEMS(ff_mpeg4audio_sample_rates); sri++)
1083 if (ff_mpeg4audio_sample_rates[sri] == samplerate)
1084 break;
1085 return sri;
1088 static int matroska_do_seek(AVFormatContext *s, int stream_index,
1089 int64_t timestamp, int flags);
1091 static int
1092 matroska_seek_next_chapter(AVFormatContext *s)
1094 MatroskaDemuxContext *matroska = s->priv_data;
1095 MatroskaChapter *chapters = matroska->chapters.elem;
1096 MatroskaChapter *mc, *cur_mc = &chapters[matroska->current_chapter];
1098 if(matroska->current_chapter + 1 >= s->nb_chapters)
1099 return AVERROR(EIO);
1100 mc = &chapters[++matroska->current_chapter];
1101 /* don't seek if not necessary */
1102 if(mc->segment == cur_mc->segment &&
1103 ((mc->start - cur_mc->end) * av_q2d(MATROSKA_UNSCALED_TIMEBASE)) < 3)
1104 return 1;
1105 return matroska_do_seek(mc->segment->ctx, matroska->default_stream,\
1106 av_rescale_q((int64_t)mc->start, MATROSKA_UNSCALED_TIMEBASE, \
1107 s->streams[matroska->default_stream]->time_base), AVSEEK_FLAG_BACKWARD);
1110 static int
1111 matroska_check_linked_tracks(MatroskaTrack *track, MatroskaTrack *track2)
1113 if(strcmp(track->codec_id, track2->codec_id))
1114 return -1;
1115 if(track->type == MATROSKA_TRACK_TYPE_VIDEO) {
1116 if(track->video.pixel_width != track2->video.pixel_width ||
1117 track->video.pixel_height != track2->video.pixel_height ||
1118 track->codec_priv.size != track2->codec_priv.size)
1119 return -1;
1121 else if(track->type == MATROSKA_TRACK_TYPE_AUDIO) {
1122 if(track->audio.channels != track2->audio.channels ||
1123 track->audio.out_samplerate != track2->audio.out_samplerate)
1124 return -1;
1125 if(strstr(track->codec_id, "A_AAC") ||
1126 strcmp(track->codec_id, "A_AC3") ||
1127 strcmp(track->codec_id, "A_VORBIS") ||
1128 strcmp(track->codec_id, "A_DTS") ||
1129 strstr(track->codec_id, "A_MPEG"))
1130 if(track->audio.bitdepth != track2->audio.bitdepth)
1131 return -1;
1132 if(strstr(track->codec_id, "A_AAC"))
1133 if(matroska_aac_profile(track->codec_id) != matroska_aac_profile(track2->codec_id))
1134 return -1;
1135 if(!strcmp(track->codec_id, "A_FLAC") &&
1136 memcmp(track->codec_priv.data, track2->codec_priv.data, track->codec_priv.size))
1137 return -1;
1138 if(!strcmp(track->codec_id, "A_VORBIS")) {
1139 uint8_t *header_start[3], *header_start2[3];
1140 int header_len[3], header_len2[3];
1142 if(ff_split_xiph_headers(track->codec_priv.data, track->codec_priv.size,
1143 30, header_start, header_len) < 0 ||
1144 ff_split_xiph_headers(track2->codec_priv.data, track2->codec_priv.size,
1145 30, header_start2, header_len2) < 0)
1146 return -1;
1147 if(header_len[0] != header_len2[0] || header_len[2] != header_len2[2])
1148 return -1;
1149 if(memcmp(header_start[0], header_start2[0],header_len[0]) ||
1150 memcmp(header_start[2], header_start2[2], header_len[2]))
1151 return -1;
1154 return 0;
1157 static int
1158 matroska_check_linked_segment(AVFormatContext *s, AVFormatContext *ext_ctx)
1160 MatroskaDemuxContext *ext_matroska, *matroska = s->priv_data;
1161 int i;
1163 if(strcmp(ext_ctx->iformat->name, "matroska"))
1164 return -1;
1165 ext_matroska = ext_ctx->priv_data;
1166 for(i = 0; i < matroska->nb_linked_segments; i++) {
1167 MatroskaSegment *seg = matroska->linked_segments[i];
1168 if(!seg->ctx && !memcmp(seg->segment_uid.data, ext_matroska->segment->segment_uid.data, 16)) {
1169 MatroskaTrack *tracks = matroska->tracks.elem, *tracks2 = ext_matroska->tracks.elem;
1170 int j;
1171 av_log(s, AV_LOG_INFO, "Found segment ");
1172 for(j = 0; j < 16; j++)
1173 av_log(s, AV_LOG_INFO, "%X", seg->segment_uid.data[j]);
1174 av_log(s, AV_LOG_INFO, ".\n");
1176 /* check that audio/video streams in linked
1177 * segments have same formats */
1178 if(!(s->flags & AVFMT_FLAG_FORCEEXTERNS))
1179 for(j = 0; j < matroska->tracks.nb_elem; j++) {
1180 MatroskaTrack *track = &tracks[j], *track2;
1181 if(j >= ext_matroska->tracks.nb_elem) {
1182 av_log(s, AV_LOG_INFO, "File %s has too few tracks, skipping this segment.\n",
1183 ext_ctx->filename);
1184 return -1;
1186 track2 = &tracks2[j];
1187 if(matroska_check_linked_tracks(track, track2) < 0) {
1188 av_log(s, AV_LOG_INFO, "Track %d in file %s is incompatible,"
1189 " skipping this segment.\n", j, ext_ctx->filename);
1190 return -1;
1194 seg->ctx = ext_ctx;
1195 return 0;
1198 return -1;
1201 static int
1202 matroska_load_linked_segments(AVFormatContext *s)
1204 URLContext *uc;
1205 char *dir, filename[1024];
1207 if(s->filename == '\0')
1208 return -1;
1209 dir = av_strdup(dirname(s->filename));
1211 if(url_open(&uc, dir, URL_DIR) < 0) {
1212 // falllback to file - workaround for e.g. mp:/
1213 char *p = strchr(dir, ':');
1215 if(*(p+1) == '\0')
1216 goto finish;
1217 if(url_open(&uc, p, URL_DIR) < 0)
1218 goto finish;
1221 av_log(s, AV_LOG_DEBUG, "Looking for linked files in %s.\n", dir);
1222 while(url_read(uc, filename, sizeof(filename)) >= 0) {
1223 AVFormatContext *ext_ctx;
1225 if(!strcmp(s->filename, filename) || !match_ext(filename, "mkv"))
1226 continue;
1228 if(!(ext_ctx = ff_open_external_stream(s, filename)))
1229 continue;
1230 if(matroska_check_linked_segment(s, ext_ctx) < 0) {
1231 av_close_input_file(ext_ctx);
1232 continue;
1234 dynarray_add(&s->external_ctx, &s->nb_external_ctx, ext_ctx);
1236 url_close(uc);
1237 finish:
1238 av_freep(&dir);
1239 return 0;
1242 static void
1243 matroska_setup_ordered_chapters(AVFormatContext *s)
1245 MatroskaDemuxContext *matroska = s->priv_data;
1246 MatroskaChapter *chapters = matroska->chapters.elem;
1247 int i;
1249 /* load linked segments if needed */
1250 if(matroska->linked_segments)
1251 matroska_load_linked_segments(s);
1253 /* compute chapter start/end times and
1254 * overall duration*/
1255 s->duration = 0;
1256 for(i = 0; i < s->nb_chapters; i++) {
1257 int j;
1258 int64_t duration;
1259 AVChapter *c = s->chapters[i];
1260 MatroskaChapter *mc = &chapters[i];
1261 duration = mc->segment->ctx ? c->end - c->start : 0;
1263 c->start = 0;
1264 c->end = duration;
1265 for(j = 0; j < i; j++)
1266 c->start += s->chapters[j]->end - s->chapters[j]->start;
1267 c->end += c->start;
1268 if(!duration)
1269 mc->start = mc->end = 0;
1270 s->duration += av_rescale_q(duration, MATROSKA_UNSCALED_TIMEBASE, AV_TIME_BASE_Q);
1272 for(i = 0; i < matroska->nb_linked_segments; i++)
1273 if(!matroska->linked_segments[i]->ctx)
1274 matroska->linked_segments[i]->ctx = s;
1277 static int matroska_read_header(AVFormatContext *s, AVFormatParameters *ap)
1279 MatroskaDemuxContext *matroska = s->priv_data;
1280 EbmlList *attachements_list = &matroska->attachments;
1281 MatroskaAttachement *attachements;
1282 EbmlList *chapters_list = &matroska->chapters;
1283 MatroskaChapter *chapters;
1284 MatroskaTrack *tracks;
1285 EbmlList *index_list;
1286 MatroskaIndex *index;
1287 int index_scale = 1;
1288 uint64_t max_start = 0;
1289 Ebml ebml = { 0 };
1290 AVStream *st;
1291 int i, j;
1293 matroska->ctx = s;
1295 /* First read the EBML header. */
1296 if (ebml_parse(matroska, ebml_syntax, &ebml)
1297 || ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t)
1298 || ebml.id_length > sizeof(uint32_t) || strcmp(ebml.doctype, "matroska")
1299 || ebml.doctype_version > 2) {
1300 av_log(matroska->ctx, AV_LOG_ERROR,
1301 "EBML header using unsupported features\n"
1302 "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
1303 ebml.version, ebml.doctype, ebml.doctype_version);
1304 return AVERROR_NOFMT;
1306 ebml_free(ebml_syntax, &ebml);
1308 /* The next thing is a segment. */
1309 if (ebml_parse(matroska, matroska_segments, matroska) < 0)
1310 return -1;
1311 matroska_execute_seekhead(matroska);
1313 if (matroska->duration)
1314 matroska->ctx->duration = matroska->duration * matroska->time_scale
1315 * 1000 / AV_TIME_BASE;
1316 if (matroska->title)
1317 strncpy(matroska->ctx->title, matroska->title,
1318 sizeof(matroska->ctx->title)-1);
1319 matroska_convert_tags(s, &matroska->tags);
1321 if(matroska->segment_uid.size == 16) { // segment UIDs are 128-bit
1322 MatroskaSegment *segment = av_mallocz(sizeof(MatroskaSegment));
1323 if(!segment)
1324 return AVERROR(ENOMEM);
1325 segment->segment_uid = matroska->segment_uid;
1326 segment->ctx = matroska->ctx;
1327 matroska->segment = segment;
1330 tracks = matroska->tracks.elem;
1331 for (i=0; i < matroska->tracks.nb_elem; i++) {
1332 MatroskaTrack *track = &tracks[i];
1333 enum CodecID codec_id = CODEC_ID_NONE;
1334 EbmlList *encodings_list = &tracks->encodings;
1335 MatroskaTrackEncoding *encodings = encodings_list->elem;
1336 uint8_t *extradata = NULL;
1337 int extradata_size = 0;
1338 int extradata_offset = 0;
1340 /* Apply some sanity checks. */
1341 if (track->type != MATROSKA_TRACK_TYPE_VIDEO &&
1342 track->type != MATROSKA_TRACK_TYPE_AUDIO &&
1343 track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
1344 av_log(matroska->ctx, AV_LOG_INFO,
1345 "Unknown or unsupported track type %"PRIu64"\n",
1346 track->type);
1347 continue;
1349 if (track->codec_id == NULL)
1350 continue;
1352 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
1353 if (!track->default_duration)
1354 track->default_duration = 1000000000/track->video.frame_rate;
1355 if (!track->video.display_width)
1356 track->video.display_width = track->video.pixel_width;
1357 if (!track->video.display_height)
1358 track->video.display_height = track->video.pixel_height;
1359 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
1360 if (!track->audio.out_samplerate)
1361 track->audio.out_samplerate = track->audio.samplerate;
1363 if (encodings_list->nb_elem > 1) {
1364 av_log(matroska->ctx, AV_LOG_ERROR,
1365 "Multiple combined encodings no supported");
1366 } else if (encodings_list->nb_elem == 1) {
1367 if (encodings[0].type ||
1368 (encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
1369 #if CONFIG_ZLIB
1370 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
1371 #endif
1372 #if CONFIG_BZLIB
1373 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
1374 #endif
1375 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO)) {
1376 encodings[0].scope = 0;
1377 av_log(matroska->ctx, AV_LOG_ERROR,
1378 "Unsupported encoding type");
1379 } else if (track->codec_priv.size && encodings[0].scope&2) {
1380 uint8_t *codec_priv = track->codec_priv.data;
1381 int offset = matroska_decode_buffer(&track->codec_priv.data,
1382 &track->codec_priv.size,
1383 track);
1384 if (offset < 0) {
1385 track->codec_priv.data = NULL;
1386 track->codec_priv.size = 0;
1387 av_log(matroska->ctx, AV_LOG_ERROR,
1388 "Failed to decode codec private data\n");
1389 } else if (offset > 0) {
1390 track->codec_priv.data = av_malloc(track->codec_priv.size + offset);
1391 memcpy(track->codec_priv.data,
1392 encodings[0].compression.settings.data, offset);
1393 memcpy(track->codec_priv.data+offset, codec_priv,
1394 track->codec_priv.size);
1395 track->codec_priv.size += offset;
1397 if (codec_priv != track->codec_priv.data)
1398 av_free(codec_priv);
1402 for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
1403 if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
1404 strlen(ff_mkv_codec_tags[j].str))){
1405 codec_id= ff_mkv_codec_tags[j].id;
1406 break;
1410 st = track->stream = av_new_stream(s, 0);
1411 if (st == NULL)
1412 return AVERROR(ENOMEM);
1414 if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC")
1415 && track->codec_priv.size >= 40
1416 && track->codec_priv.data != NULL) {
1417 track->video.fourcc = AV_RL32(track->codec_priv.data + 16);
1418 codec_id = codec_get_id(codec_bmp_tags, track->video.fourcc);
1419 } else if (!strcmp(track->codec_id, "A_MS/ACM")
1420 && track->codec_priv.size >= 18
1421 && track->codec_priv.data != NULL) {
1422 uint16_t tag = AV_RL16(track->codec_priv.data);
1423 codec_id = codec_get_id(codec_wav_tags, tag);
1424 } else if (!strcmp(track->codec_id, "V_QUICKTIME")
1425 && (track->codec_priv.size >= 86)
1426 && (track->codec_priv.data != NULL)) {
1427 track->video.fourcc = AV_RL32(track->codec_priv.data);
1428 codec_id=codec_get_id(codec_movvideo_tags, track->video.fourcc);
1429 } else if (codec_id == CODEC_ID_PCM_S16BE) {
1430 switch (track->audio.bitdepth) {
1431 case 8: codec_id = CODEC_ID_PCM_U8; break;
1432 case 24: codec_id = CODEC_ID_PCM_S24BE; break;
1433 case 32: codec_id = CODEC_ID_PCM_S32BE; break;
1435 } else if (codec_id == CODEC_ID_PCM_S16LE) {
1436 switch (track->audio.bitdepth) {
1437 case 8: codec_id = CODEC_ID_PCM_U8; break;
1438 case 24: codec_id = CODEC_ID_PCM_S24LE; break;
1439 case 32: codec_id = CODEC_ID_PCM_S32LE; break;
1441 } else if (codec_id==CODEC_ID_PCM_F32LE && track->audio.bitdepth==64) {
1442 codec_id = CODEC_ID_PCM_F64LE;
1443 } else if (codec_id == CODEC_ID_AAC && !track->codec_priv.size) {
1444 int profile = matroska_aac_profile(track->codec_id);
1445 int sri = matroska_aac_sri(track->audio.samplerate);
1446 extradata = av_malloc(5);
1447 if (extradata == NULL)
1448 return AVERROR(ENOMEM);
1449 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
1450 extradata[1] = ((sri&0x01) << 7) | (track->audio.channels<<3);
1451 if (strstr(track->codec_id, "SBR")) {
1452 sri = matroska_aac_sri(track->audio.out_samplerate);
1453 extradata[2] = 0x56;
1454 extradata[3] = 0xE5;
1455 extradata[4] = 0x80 | (sri<<3);
1456 extradata_size = 5;
1457 } else
1458 extradata_size = 2;
1459 } else if (codec_id == CODEC_ID_TTA) {
1460 ByteIOContext b;
1461 extradata_size = 30;
1462 extradata = av_mallocz(extradata_size);
1463 if (extradata == NULL)
1464 return AVERROR(ENOMEM);
1465 init_put_byte(&b, extradata, extradata_size, 1,
1466 NULL, NULL, NULL, NULL);
1467 put_buffer(&b, "TTA1", 4);
1468 put_le16(&b, 1);
1469 put_le16(&b, track->audio.channels);
1470 put_le16(&b, track->audio.bitdepth);
1471 put_le32(&b, track->audio.out_samplerate);
1472 put_le32(&b, matroska->ctx->duration * track->audio.out_samplerate);
1473 } else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
1474 codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
1475 extradata_offset = 26;
1476 track->codec_priv.size -= extradata_offset;
1477 } else if (codec_id == CODEC_ID_RA_144) {
1478 track->audio.out_samplerate = 8000;
1479 track->audio.channels = 1;
1480 } else if (codec_id == CODEC_ID_RA_288 || codec_id == CODEC_ID_COOK ||
1481 codec_id == CODEC_ID_ATRAC3) {
1482 ByteIOContext b;
1484 init_put_byte(&b, track->codec_priv.data,track->codec_priv.size,
1485 0, NULL, NULL, NULL, NULL);
1486 url_fskip(&b, 24);
1487 track->audio.coded_framesize = get_be32(&b);
1488 url_fskip(&b, 12);
1489 track->audio.sub_packet_h = get_be16(&b);
1490 track->audio.frame_size = get_be16(&b);
1491 track->audio.sub_packet_size = get_be16(&b);
1492 track->audio.buf = av_malloc(track->audio.frame_size * track->audio.sub_packet_h);
1493 if (codec_id == CODEC_ID_RA_288) {
1494 st->codec->block_align = track->audio.coded_framesize;
1495 track->codec_priv.size = 0;
1496 } else {
1497 st->codec->block_align = track->audio.sub_packet_size;
1498 extradata_offset = 78;
1499 track->codec_priv.size -= extradata_offset;
1503 if (codec_id == CODEC_ID_NONE)
1504 av_log(matroska->ctx, AV_LOG_INFO,
1505 "Unknown/unsupported CodecID %s.\n", track->codec_id);
1507 if (track->time_scale < 0.01)
1508 track->time_scale = 1.0;
1509 av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
1511 st->codec->codec_id = codec_id;
1512 st->start_time = 0;
1513 if (strcmp(track->language, "und"))
1514 av_strlcpy(st->language, track->language, 4);
1516 if (track->flag_default)
1517 st->disposition |= AV_DISPOSITION_DEFAULT;
1519 if (track->default_duration)
1520 av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
1521 track->default_duration, 1000000000, 30000);
1523 if(extradata){
1524 st->codec->extradata = extradata;
1525 st->codec->extradata_size = extradata_size;
1526 } else if(track->codec_priv.data && track->codec_priv.size > 0){
1527 st->codec->extradata = av_mallocz(track->codec_priv.size +
1528 FF_INPUT_BUFFER_PADDING_SIZE);
1529 if(st->codec->extradata == NULL)
1530 return AVERROR(ENOMEM);
1531 st->codec->extradata_size = track->codec_priv.size;
1532 memcpy(st->codec->extradata,
1533 track->codec_priv.data + extradata_offset,
1534 track->codec_priv.size);
1537 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
1538 st->codec->codec_type = CODEC_TYPE_VIDEO;
1539 st->codec->codec_tag = track->video.fourcc;
1540 st->codec->width = track->video.pixel_width;
1541 st->codec->height = track->video.pixel_height;
1542 av_reduce(&st->sample_aspect_ratio.num,
1543 &st->sample_aspect_ratio.den,
1544 st->codec->height * track->video.display_width,
1545 st->codec-> width * track->video.display_height,
1546 255);
1547 st->need_parsing = AVSTREAM_PARSE_HEADERS;
1548 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
1549 st->codec->codec_type = CODEC_TYPE_AUDIO;
1550 st->codec->sample_rate = track->audio.out_samplerate;
1551 st->codec->channels = track->audio.channels;
1552 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
1553 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
1557 attachements = attachements_list->elem;
1558 for (j=0; j<attachements_list->nb_elem; j++) {
1559 if (!(attachements[j].filename && attachements[j].mime &&
1560 attachements[j].bin.data && attachements[j].bin.size > 0)) {
1561 av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
1562 } else {
1563 AVStream *st = av_new_stream(s, 0);
1564 if (st == NULL)
1565 break;
1566 st->filename = av_strdup(attachements[j].filename);
1567 st->codec->codec_id = CODEC_ID_NONE;
1568 st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
1569 st->codec->extradata = av_malloc(attachements[j].bin.size);
1570 if(st->codec->extradata == NULL)
1571 break;
1572 st->codec->extradata_size = attachements[j].bin.size;
1573 memcpy(st->codec->extradata, attachements[j].bin.data, attachements[j].bin.size);
1575 for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
1576 if (!strncmp(ff_mkv_mime_tags[i].str, attachements[j].mime,
1577 strlen(ff_mkv_mime_tags[i].str))) {
1578 st->codec->codec_id = ff_mkv_mime_tags[i].id;
1579 break;
1585 chapters = chapters_list->elem;
1586 for (i=0; i<chapters_list->nb_elem; i++) {
1587 if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid
1588 && (max_start==0 || chapters[i].start > max_start ||
1589 matroska->ordered_chapters)) {
1590 ff_new_chapter(s, chapters[i].uid, MATROSKA_UNSCALED_TIMEBASE,
1591 chapters[i].start, chapters[i].end,
1592 chapters[i].title);
1593 max_start = chapters[i].start;
1595 /* add a linked segment */
1596 if(matroska->ordered_chapters && matroska->segment) {
1597 if(chapters[i].segment_uid.size == 16) {
1598 EbmlBin *segment_uid = &chapters[i].segment_uid;
1599 MatroskaSegment *segment = av_mallocz(sizeof(MatroskaSegment));
1600 if(!segment)
1601 return AVERROR(ENOMEM);
1603 for(j = 0; j < matroska->nb_linked_segments; j++)
1604 if(!memcmp(segment_uid->data,
1605 matroska->linked_segments[j]->segment_uid.data, 16)) {
1606 chapters[i].segment = matroska->linked_segments[j];
1607 av_freep(&segment);
1608 break;
1611 if(segment) { // if segment survived the loop then its worthy to live
1612 segment->segment_uid = *segment_uid;
1613 dynarray_add(&matroska->linked_segments,
1614 &matroska->nb_linked_segments, segment);
1615 chapters[i].segment = segment;
1618 else chapters[i].segment = matroska->segment;
1622 index_list = &matroska->index;
1623 index = index_list->elem;
1624 if (index_list->nb_elem
1625 && index[0].time > 100000000000000/matroska->time_scale) {
1626 av_log(matroska->ctx, AV_LOG_WARNING, "Working around broken index.\n");
1627 index_scale = matroska->time_scale;
1629 for (i=0; i<index_list->nb_elem; i++) {
1630 EbmlList *pos_list = &index[i].pos;
1631 MatroskaIndexPos *pos = pos_list->elem;
1632 for (j=0; j<pos_list->nb_elem; j++) {
1633 MatroskaTrack *track = matroska_find_track_by_num(matroska,
1634 pos[j].track);
1635 if (track && track->stream)
1636 av_add_index_entry(track->stream,
1637 pos[j].pos + matroska->segment_start,
1638 index[i].time/index_scale, 0, 0,
1639 AVINDEX_KEYFRAME);
1643 matroska->default_stream = av_find_default_stream_index(s);
1645 if(matroska->ordered_chapters && !(s->flags&AVFMT_FLAG_IGNEXTERNS))
1646 matroska_setup_ordered_chapters(s);
1648 return 0;
1653 * Free all packets in our internal queue.
1655 static void matroska_clear_queue(MatroskaDemuxContext *matroska)
1657 if (matroska->packets) {
1658 int n;
1659 for (n = 0; n < matroska->num_packets; n++) {
1660 av_free_packet(matroska->packets[n]);
1661 av_free(matroska->packets[n]);
1663 av_freep(&matroska->packets);
1664 matroska->num_packets = 0;
1668 static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data,
1669 int size, int64_t pos, uint64_t cluster_time,
1670 uint64_t duration, int is_keyframe,
1671 int64_t cluster_pos)
1673 uint64_t timecode = AV_NOPTS_VALUE;
1674 MatroskaTrack *track;
1675 int res = 0;
1676 AVStream *st;
1677 AVPacket *pkt;
1678 int16_t block_time;
1679 uint32_t *lace_size = NULL;
1680 int n, flags, laces = 0;
1681 uint64_t num;
1683 if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) {
1684 av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
1685 return res;
1687 data += n;
1688 size -= n;
1690 track = matroska_find_track_by_num(matroska, num);
1691 if (size <= 3 || !track || !track->stream) {
1692 av_log(matroska->ctx, AV_LOG_INFO,
1693 "Invalid stream %"PRIu64" or size %u\n", num, size);
1694 return res;
1696 st = track->stream;
1697 if (st->discard >= AVDISCARD_ALL)
1698 return res;
1699 if (duration == AV_NOPTS_VALUE)
1700 duration = track->default_duration / matroska->time_scale;
1702 block_time = AV_RB16(data);
1703 data += 2;
1704 flags = *data++;
1705 size -= 3;
1706 if (is_keyframe == -1)
1707 is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
1709 if (cluster_time != (uint64_t)-1
1710 && (block_time >= 0 || cluster_time >= -block_time)) {
1711 timecode = cluster_time + block_time;
1712 if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE
1713 && timecode < track->end_timecode)
1714 is_keyframe = 0; /* overlapping subtitles are not key frame */
1715 if (is_keyframe)
1716 av_add_index_entry(st, cluster_pos, timecode, 0,0,AVINDEX_KEYFRAME);
1717 track->end_timecode = FFMAX(track->end_timecode, timecode+duration);
1720 if (matroska->skip_to_keyframe && track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
1721 if (!is_keyframe || timecode < matroska->skip_to_timecode)
1722 return res;
1723 matroska->skip_to_keyframe = 0;
1726 switch ((flags & 0x06) >> 1) {
1727 case 0x0: /* no lacing */
1728 laces = 1;
1729 lace_size = av_mallocz(sizeof(int));
1730 lace_size[0] = size;
1731 break;
1733 case 0x1: /* Xiph lacing */
1734 case 0x2: /* fixed-size lacing */
1735 case 0x3: /* EBML lacing */
1736 assert(size>0); // size <=3 is checked before size-=3 above
1737 laces = (*data) + 1;
1738 data += 1;
1739 size -= 1;
1740 lace_size = av_mallocz(laces * sizeof(int));
1742 switch ((flags & 0x06) >> 1) {
1743 case 0x1: /* Xiph lacing */ {
1744 uint8_t temp;
1745 uint32_t total = 0;
1746 for (n = 0; res == 0 && n < laces - 1; n++) {
1747 while (1) {
1748 if (size == 0) {
1749 res = -1;
1750 break;
1752 temp = *data;
1753 lace_size[n] += temp;
1754 data += 1;
1755 size -= 1;
1756 if (temp != 0xff)
1757 break;
1759 total += lace_size[n];
1761 lace_size[n] = size - total;
1762 break;
1765 case 0x2: /* fixed-size lacing */
1766 for (n = 0; n < laces; n++)
1767 lace_size[n] = size / laces;
1768 break;
1770 case 0x3: /* EBML lacing */ {
1771 uint32_t total;
1772 n = matroska_ebmlnum_uint(matroska, data, size, &num);
1773 if (n < 0) {
1774 av_log(matroska->ctx, AV_LOG_INFO,
1775 "EBML block data error\n");
1776 break;
1778 data += n;
1779 size -= n;
1780 total = lace_size[0] = num;
1781 for (n = 1; res == 0 && n < laces - 1; n++) {
1782 int64_t snum;
1783 int r;
1784 r = matroska_ebmlnum_sint(matroska, data, size, &snum);
1785 if (r < 0) {
1786 av_log(matroska->ctx, AV_LOG_INFO,
1787 "EBML block data error\n");
1788 break;
1790 data += r;
1791 size -= r;
1792 lace_size[n] = lace_size[n - 1] + snum;
1793 total += lace_size[n];
1795 lace_size[n] = size - total;
1796 break;
1799 break;
1802 if (res == 0) {
1803 for (n = 0; n < laces; n++) {
1804 if (st->codec->codec_id == CODEC_ID_RA_288 ||
1805 st->codec->codec_id == CODEC_ID_COOK ||
1806 st->codec->codec_id == CODEC_ID_ATRAC3) {
1807 int a = st->codec->block_align;
1808 int sps = track->audio.sub_packet_size;
1809 int cfs = track->audio.coded_framesize;
1810 int h = track->audio.sub_packet_h;
1811 int y = track->audio.sub_packet_cnt;
1812 int w = track->audio.frame_size;
1813 int x;
1815 if (!track->audio.pkt_cnt) {
1816 if (st->codec->codec_id == CODEC_ID_RA_288)
1817 for (x=0; x<h/2; x++)
1818 memcpy(track->audio.buf+x*2*w+y*cfs,
1819 data+x*cfs, cfs);
1820 else
1821 for (x=0; x<w/sps; x++)
1822 memcpy(track->audio.buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
1824 if (++track->audio.sub_packet_cnt >= h) {
1825 track->audio.sub_packet_cnt = 0;
1826 track->audio.pkt_cnt = h*w / a;
1829 while (track->audio.pkt_cnt) {
1830 pkt = av_mallocz(sizeof(AVPacket));
1831 av_new_packet(pkt, a);
1832 memcpy(pkt->data, track->audio.buf
1833 + a * (h*w / a - track->audio.pkt_cnt--), a);
1834 pkt->pos = pos;
1835 pkt->stream_index = st->index;
1836 dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
1838 } else {
1839 MatroskaTrackEncoding *encodings = track->encodings.elem;
1840 int offset = 0, pkt_size = lace_size[n];
1841 uint8_t *pkt_data = data;
1843 if (encodings && encodings->scope & 1) {
1844 offset = matroska_decode_buffer(&pkt_data,&pkt_size, track);
1845 if (offset < 0)
1846 continue;
1849 pkt = av_mallocz(sizeof(AVPacket));
1850 /* XXX: prevent data copy... */
1851 if (av_new_packet(pkt, pkt_size+offset) < 0) {
1852 av_free(pkt);
1853 res = AVERROR(ENOMEM);
1854 n = laces-1;
1855 break;
1857 if (offset)
1858 memcpy (pkt->data, encodings->compression.settings.data, offset);
1859 memcpy (pkt->data+offset, pkt_data, pkt_size);
1861 if (pkt_data != data)
1862 av_free(pkt_data);
1864 if (n == 0)
1865 pkt->flags = is_keyframe;
1866 pkt->stream_index = st->index;
1868 pkt->pts = timecode;
1869 pkt->pos = pos;
1870 if (st->codec->codec_id == CODEC_ID_TEXT)
1871 pkt->convergence_duration = duration;
1872 else if (track->type != MATROSKA_TRACK_TYPE_SUBTITLE)
1873 pkt->duration = duration;
1875 if (st->codec->codec_id == CODEC_ID_SSA)
1876 matroska_fix_ass_packet(matroska, pkt, duration);
1878 if (matroska->prev_pkt &&
1879 timecode != AV_NOPTS_VALUE &&
1880 matroska->prev_pkt->pts == timecode &&
1881 matroska->prev_pkt->stream_index == st->index)
1882 matroska_merge_packets(matroska->prev_pkt, pkt);
1883 else {
1884 dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
1885 matroska->prev_pkt = pkt;
1889 if (timecode != AV_NOPTS_VALUE)
1890 timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
1891 data += lace_size[n];
1895 av_free(lace_size);
1896 return res;
1899 static int matroska_parse_cluster(MatroskaDemuxContext *matroska)
1901 MatroskaCluster cluster = { 0 };
1902 EbmlList *blocks_list;
1903 MatroskaBlock *blocks;
1904 int i, res;
1905 int64_t pos = url_ftell(matroska->ctx->pb);
1906 matroska->prev_pkt = NULL;
1907 if (matroska->has_cluster_id){
1908 /* For the first cluster we parse, its ID was already read as
1909 part of matroska_read_header(), so don't read it again */
1910 res = ebml_parse_id(matroska, matroska_clusters,
1911 MATROSKA_ID_CLUSTER, &cluster);
1912 pos -= 4; /* sizeof the ID which was already read */
1913 matroska->has_cluster_id = 0;
1914 } else
1915 res = ebml_parse(matroska, matroska_clusters, &cluster);
1916 blocks_list = &cluster.blocks;
1917 blocks = blocks_list->elem;
1918 for (i=0; i<blocks_list->nb_elem; i++)
1919 if (blocks[i].bin.size > 0)
1920 res=matroska_parse_block(matroska,
1921 blocks[i].bin.data, blocks[i].bin.size,
1922 blocks[i].bin.pos, cluster.timecode,
1923 blocks[i].duration, !blocks[i].reference,
1924 pos);
1925 ebml_free(matroska_cluster, &cluster);
1926 if (res < 0) matroska->done = 1;
1927 return res;
1931 * Put one packet in an application-supplied AVPacket struct.
1933 static int matroska_deliver_packet(MatroskaDemuxContext *matroska,
1934 AVPacket *pkt)
1936 for(;;) {
1937 if (matroska->num_packets > 0) {
1938 memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
1939 av_free(matroska->packets[0]);
1940 if (matroska->num_packets > 1) {
1941 memmove(&matroska->packets[0], &matroska->packets[1],
1942 (matroska->num_packets - 1) * sizeof(AVPacket *));
1943 matroska->packets =
1944 av_realloc(matroska->packets, (matroska->num_packets - 1) *
1945 sizeof(AVPacket *));
1946 } else {
1947 av_freep(&matroska->packets);
1949 matroska->num_packets--;
1950 return 0;
1952 else if(matroska->done)
1953 return AVERROR(EIO);
1954 else matroska_parse_cluster(matroska);
1958 static int
1959 matroska_read_packet(AVFormatContext *s, AVPacket *pkt)
1961 MatroskaDemuxContext *matroska = s->priv_data;
1962 MatroskaChapter *chapters, *mc;
1963 int res;
1965 if(!matroska->ordered_chapters || !s->flags&AVFMT_FLAG_IGNEXTERNS)
1966 return matroska_deliver_packet(matroska, pkt);
1968 chapters = matroska->chapters.elem;
1969 /* get a packet */
1970 for(;;) {
1971 mc = &chapters[matroska->current_chapter];
1972 if((res = matroska_deliver_packet((MatroskaDemuxContext*)mc->segment->ctx->priv_data, pkt) < 0)) {
1973 if(matroska_seek_next_chapter(s) < 0)
1974 return AVERROR(EIO);
1975 else continue;
1977 /* end of chapter? */
1978 if(pkt->stream_index == matroska->default_stream)
1979 if(av_rescale_q(pkt->pts, s->streams[pkt->stream_index]->time_base,
1980 MATROSKA_UNSCALED_TIMEBASE) >= mc->end) {
1981 if((res = matroska_seek_next_chapter(s)) < 0)
1982 return AVERROR(EIO);
1983 else if(!res) {
1984 av_free_packet(pkt);
1985 continue;
1987 else mc = &chapters[matroska->current_chapter];
1989 break;
1992 /* compute packet pts */
1993 if(pkt->pts != AV_NOPTS_VALUE) {
1994 AVChapter *c = s->chapters[matroska->current_chapter];
1995 pkt->pts -= av_rescale_q((int64_t)mc->start, MATROSKA_UNSCALED_TIMEBASE,
1996 s->streams[pkt->stream_index]->time_base);
1997 pkt->pts += av_rescale_q(c->start, c->time_base, s->streams[pkt->stream_index]->time_base);
1999 return 0;
2002 static int matroska_do_seek(AVFormatContext *s, int stream_index,
2003 int64_t timestamp, int flags)
2005 MatroskaDemuxContext *matroska = s->priv_data;
2006 MatroskaTrack *tracks = matroska->tracks.elem;
2007 AVStream *st = s->streams[stream_index];
2008 int i, index, index_sub, index_min;
2010 if (!st->nb_index_entries)
2011 return 0;
2012 timestamp = FFMAX(timestamp, st->index_entries[0].timestamp);
2014 if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
2015 url_fseek(s->pb, st->index_entries[st->nb_index_entries-1].pos, SEEK_SET);
2016 while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
2017 matroska_clear_queue(matroska);
2018 if (matroska_parse_cluster(matroska) < 0)
2019 break;
2023 matroska_clear_queue(matroska);
2024 if (index < 0)
2025 return 0;
2027 index_min = index;
2028 for (i=0; i < matroska->tracks.nb_elem; i++) {
2029 tracks[i].end_timecode = 0;
2030 if (tracks[i].type == MATROSKA_TRACK_TYPE_SUBTITLE
2031 && !tracks[i].stream->discard != AVDISCARD_ALL) {
2032 index_sub = av_index_search_timestamp(tracks[i].stream, st->index_entries[index].timestamp, AVSEEK_FLAG_BACKWARD);
2033 if (index_sub >= 0
2034 && st->index_entries[index_sub].pos < st->index_entries[index_min].pos
2035 && st->index_entries[index].timestamp - st->index_entries[index_sub].timestamp < 30000000000/matroska->time_scale)
2036 index_min = index_sub;
2040 url_fseek(s->pb, st->index_entries[index_min].pos, SEEK_SET);
2041 matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
2042 matroska->skip_to_timecode = st->index_entries[index].timestamp;
2043 matroska->done = 0;
2044 av_update_cur_dts(s, st, st->index_entries[index].timestamp);
2045 matroska->has_cluster_id = 0; // is there a better way to do this?
2046 return 0;
2049 static int
2050 matroska_read_seek(AVFormatContext *s, int stream_index,
2051 int64_t timestamp, int flags)
2053 MatroskaDemuxContext *matroska = s->priv_data;
2054 MatroskaChapter *chapters;
2055 int i;
2056 int64_t time;
2058 if(!matroska->ordered_chapters || !s->flags&AVFMT_FLAG_IGNEXTERNS)
2059 return matroska_do_seek(s, stream_index, timestamp, flags);
2061 chapters = matroska->chapters.elem;
2062 av_log(s, AV_LOG_DEBUG, "Seeking to %lld.\n", timestamp);
2063 if(timestamp < 0)
2064 return 0;
2066 /* find target chapter and segment */
2067 time = av_rescale_q(timestamp, s->streams[stream_index]->time_base, MATROSKA_UNSCALED_TIMEBASE);
2068 for(i = 0; i < s->nb_chapters; i++) {
2069 MatroskaChapter *mc = &chapters[i];
2070 MatroskaSegment *seg = mc->segment;
2072 if(time <= (int64_t)(mc->end - mc->start)) {
2073 av_log(s, AV_LOG_DEBUG, "Target chapter: %d.\n", i);
2074 matroska->current_chapter = i;
2075 time = av_rescale_q(time + (int64_t)mc->start, MATROSKA_UNSCALED_TIMEBASE, \
2076 s->streams[stream_index]->time_base);
2077 // av_log(s, AV_LOG_INFO, "matroska_read_seek(ctx, %d, %lld, %d)\n", stream_index, time, flags);
2078 return matroska_do_seek(seg->ctx, stream_index, time, flags);
2080 else time -= (int64_t)(mc->end - mc->start);
2082 return AVERROR(EIO);
2085 static int matroska_read_close(AVFormatContext *s)
2087 MatroskaDemuxContext *matroska = s->priv_data;
2088 MatroskaTrack *tracks = matroska->tracks.elem;
2089 int n;
2091 matroska_clear_queue(matroska);
2093 for (n=0; n < matroska->tracks.nb_elem; n++)
2094 if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO)
2095 av_free(tracks[n].audio.buf);
2096 ebml_free(matroska_segment, matroska);
2097 av_freep(&matroska->segment);
2098 for(n = 0; n < matroska->nb_linked_segments; n++)
2099 av_free(matroska->linked_segments[n]);
2100 av_freep(&matroska->linked_segments);
2102 return 0;
2105 AVInputFormat matroska_demuxer = {
2106 "matroska",
2107 NULL_IF_CONFIG_SMALL("Matroska file format"),
2108 sizeof(MatroskaDemuxContext),
2109 matroska_probe,
2110 matroska_read_header,
2111 matroska_read_packet,
2112 matroska_read_close,
2113 matroska_read_seek,