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