24
24
* Matroska file demuxer
25
25
* by Ronald Bultje <rbultje@ronald.bitfreak.net>
26
26
* with a little help from Moritz Bunkus <moritz@bunkus.org>
27
* Specs available on the matroska project page:
28
* http://www.matroska.org/.
27
* totally reworked by Aurelien Jacobs <aurel@gnuage.org>
28
* Specs available on the Matroska project page: http://www.matroska.org/.
31
32
#include "avformat.h"
32
33
/* For codec_get_id(). */
34
#include "intfloat_readwrite.h"
35
36
#include "matroska.h"
36
37
#include "libavcodec/mpeg4audio.h"
38
typedef struct Track {
39
MatroskaTrackType type;
41
/* Unique track number and track ID. stream_index is the index that
42
* the calling app uses for this track. */
53
unsigned char *codec_priv;
38
#include "libavutil/intfloat_readwrite.h"
39
#include "libavutil/avstring.h"
40
#include "libavutil/lzo.h"
60
typedef const struct EbmlSyntax {
69
const struct EbmlSyntax *n;
89
uint64_t doctype_version;
95
} MatroskaTrackCompression;
100
MatroskaTrackCompression compression;
101
} MatroskaTrackEncoding;
105
uint64_t display_width;
106
uint64_t display_height;
107
uint64_t pixel_width;
108
uint64_t pixel_height;
110
} MatroskaTrackVideo;
114
double out_samplerate;
118
/* real audio header (extracted from extradata) */
126
} MatroskaTrackAudio;
56
135
uint64_t default_duration;
57
MatroskaTrackFlags flags;
136
uint64_t flag_default;
137
MatroskaTrackVideo video;
138
MatroskaTrackAudio audio;
142
int64_t end_timecode;
60
typedef struct MatroskaVideoTrack {
70
MatroskaAspectRatioMode ar_mode;
71
MatroskaEyeMode eye_mode;
76
typedef struct MatroskaAudioTrack {
81
int internal_samplerate;
85
/* real audio header */
96
typedef struct MatroskaSubtitleTrack {
99
} MatroskaSubtitleTrack;
101
#define MAX_TRACK_SIZE (FFMAX3(sizeof(MatroskaVideoTrack), \
102
sizeof(MatroskaAudioTrack), \
103
sizeof(MatroskaSubtitleTrack)))
105
typedef struct MatroskaLevel {
149
} MatroskaAttachement;
110
typedef struct MatroskaDemuxIndex {
111
uint64_t pos; /* of the corresponding *cluster*! */
112
uint16_t track; /* reference to 'num' */
113
uint64_t time; /* in nanoseconds */
114
} MatroskaDemuxIndex;
116
typedef struct MatroskaDemuxContext {
117
185
AVFormatContext *ctx;
121
189
MatroskaLevel levels[EBML_MAX_DEPTH];
129
/* timescale in the file */
132
/* num_streams is the number of streams that av_new_stream() was called
133
* for ( = that are available to the calling program). */
136
MatroskaTrack *tracks[MAX_STREAMS];
138
/* cache for ID peeking */
196
EbmlList attachments;
141
202
/* byte position of the segment inside the stream */
142
offset_t segment_start;
203
int64_t segment_start;
144
/* The packet queue. */
205
/* the packet queue */
145
206
AVPacket **packets;
148
/* have we already parse metadata/cues/clusters? */
153
/* The index for seeking. */
155
MatroskaDemuxIndex *index;
157
213
/* What to skip before effectively reading a packet. */
158
214
int skip_to_keyframe;
159
AVStream *skip_to_stream;
215
uint64_t skip_to_timecode;
160
216
} MatroskaDemuxContext;
163
* The first few functions handle EBML file parsing. The rest
164
* is the document interpretation. Matroska really just is a
169
* Return: the amount of levels in the hierarchy that the
170
* current element lies higher than the previous one.
171
* The opposite isn't done - that's auto-done using master
176
ebml_read_element_level_up (MatroskaDemuxContext *matroska)
229
static EbmlSyntax ebml_header[] = {
230
{ EBML_ID_EBMLREADVERSION, EBML_UINT, 0, offsetof(Ebml,version), {.u=EBML_VERSION} },
231
{ EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, offsetof(Ebml,max_size), {.u=8} },
232
{ EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, offsetof(Ebml,id_length), {.u=4} },
233
{ EBML_ID_DOCTYPE, EBML_STR, 0, offsetof(Ebml,doctype), {.s="(none)"} },
234
{ EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, offsetof(Ebml,doctype_version), {.u=1} },
235
{ EBML_ID_EBMLVERSION, EBML_NONE },
236
{ EBML_ID_DOCTYPEVERSION, EBML_NONE },
240
static EbmlSyntax ebml_syntax[] = {
241
{ EBML_ID_HEADER, EBML_NEST, 0, 0, {.n=ebml_header} },
245
static EbmlSyntax matroska_info[] = {
246
{ MATROSKA_ID_TIMECODESCALE, EBML_UINT, 0, offsetof(MatroskaDemuxContext,time_scale), {.u=1000000} },
247
{ MATROSKA_ID_DURATION, EBML_FLOAT, 0, offsetof(MatroskaDemuxContext,duration) },
248
{ MATROSKA_ID_TITLE, EBML_UTF8, 0, offsetof(MatroskaDemuxContext,title) },
249
{ MATROSKA_ID_WRITINGAPP, EBML_NONE },
250
{ MATROSKA_ID_MUXINGAPP, EBML_NONE },
251
{ MATROSKA_ID_DATEUTC, EBML_NONE },
252
{ MATROSKA_ID_SEGMENTUID, EBML_NONE },
256
static EbmlSyntax matroska_track_video[] = {
257
{ MATROSKA_ID_VIDEOFRAMERATE, EBML_FLOAT,0, offsetof(MatroskaTrackVideo,frame_rate) },
258
{ MATROSKA_ID_VIDEODISPLAYWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_width) },
259
{ MATROSKA_ID_VIDEODISPLAYHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_height) },
260
{ MATROSKA_ID_VIDEOPIXELWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_width) },
261
{ MATROSKA_ID_VIDEOPIXELHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_height) },
262
{ MATROSKA_ID_VIDEOCOLORSPACE, EBML_UINT, 0, offsetof(MatroskaTrackVideo,fourcc) },
263
{ MATROSKA_ID_VIDEOPIXELCROPB, EBML_NONE },
264
{ MATROSKA_ID_VIDEOPIXELCROPT, EBML_NONE },
265
{ MATROSKA_ID_VIDEOPIXELCROPL, EBML_NONE },
266
{ MATROSKA_ID_VIDEOPIXELCROPR, EBML_NONE },
267
{ MATROSKA_ID_VIDEODISPLAYUNIT, EBML_NONE },
268
{ MATROSKA_ID_VIDEOFLAGINTERLACED,EBML_NONE },
269
{ MATROSKA_ID_VIDEOSTEREOMODE, EBML_NONE },
270
{ MATROSKA_ID_VIDEOASPECTRATIO, EBML_NONE },
274
static EbmlSyntax matroska_track_audio[] = {
275
{ MATROSKA_ID_AUDIOSAMPLINGFREQ, EBML_FLOAT,0, offsetof(MatroskaTrackAudio,samplerate), {.f=8000.0} },
276
{ MATROSKA_ID_AUDIOOUTSAMPLINGFREQ,EBML_FLOAT,0,offsetof(MatroskaTrackAudio,out_samplerate) },
277
{ MATROSKA_ID_AUDIOBITDEPTH, EBML_UINT, 0, offsetof(MatroskaTrackAudio,bitdepth) },
278
{ MATROSKA_ID_AUDIOCHANNELS, EBML_UINT, 0, offsetof(MatroskaTrackAudio,channels), {.u=1} },
282
static EbmlSyntax matroska_track_encoding_compression[] = {
283
{ MATROSKA_ID_ENCODINGCOMPALGO, EBML_UINT, 0, offsetof(MatroskaTrackCompression,algo), {.u=0} },
284
{ MATROSKA_ID_ENCODINGCOMPSETTINGS,EBML_BIN, 0, offsetof(MatroskaTrackCompression,settings) },
288
static EbmlSyntax matroska_track_encoding[] = {
289
{ MATROSKA_ID_ENCODINGSCOPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,scope), {.u=1} },
290
{ MATROSKA_ID_ENCODINGTYPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,type), {.u=0} },
291
{ MATROSKA_ID_ENCODINGCOMPRESSION,EBML_NEST, 0, offsetof(MatroskaTrackEncoding,compression), {.n=matroska_track_encoding_compression} },
292
{ MATROSKA_ID_ENCODINGORDER, EBML_NONE },
296
static EbmlSyntax matroska_track_encodings[] = {
297
{ MATROSKA_ID_TRACKCONTENTENCODING, EBML_NEST, sizeof(MatroskaTrackEncoding), offsetof(MatroskaTrack,encodings), {.n=matroska_track_encoding} },
301
static EbmlSyntax matroska_track[] = {
302
{ MATROSKA_ID_TRACKNUMBER, EBML_UINT, 0, offsetof(MatroskaTrack,num) },
303
{ MATROSKA_ID_TRACKTYPE, EBML_UINT, 0, offsetof(MatroskaTrack,type) },
304
{ MATROSKA_ID_CODECID, EBML_STR, 0, offsetof(MatroskaTrack,codec_id) },
305
{ MATROSKA_ID_CODECPRIVATE, EBML_BIN, 0, offsetof(MatroskaTrack,codec_priv) },
306
{ MATROSKA_ID_TRACKLANGUAGE, EBML_UTF8, 0, offsetof(MatroskaTrack,language), {.s="eng"} },
307
{ MATROSKA_ID_TRACKDEFAULTDURATION, EBML_UINT, 0, offsetof(MatroskaTrack,default_duration) },
308
{ MATROSKA_ID_TRACKTIMECODESCALE, EBML_FLOAT,0, offsetof(MatroskaTrack,time_scale), {.f=1.0} },
309
{ MATROSKA_ID_TRACKFLAGDEFAULT, EBML_UINT, 0, offsetof(MatroskaTrack,flag_default), {.u=1} },
310
{ MATROSKA_ID_TRACKVIDEO, EBML_NEST, 0, offsetof(MatroskaTrack,video), {.n=matroska_track_video} },
311
{ MATROSKA_ID_TRACKAUDIO, EBML_NEST, 0, offsetof(MatroskaTrack,audio), {.n=matroska_track_audio} },
312
{ MATROSKA_ID_TRACKCONTENTENCODINGS,EBML_NEST, 0, 0, {.n=matroska_track_encodings} },
313
{ MATROSKA_ID_TRACKUID, EBML_NONE },
314
{ MATROSKA_ID_TRACKNAME, EBML_NONE },
315
{ MATROSKA_ID_TRACKFLAGENABLED, EBML_NONE },
316
{ MATROSKA_ID_TRACKFLAGFORCED, EBML_NONE },
317
{ MATROSKA_ID_TRACKFLAGLACING, EBML_NONE },
318
{ MATROSKA_ID_CODECNAME, EBML_NONE },
319
{ MATROSKA_ID_CODECDECODEALL, EBML_NONE },
320
{ MATROSKA_ID_CODECINFOURL, EBML_NONE },
321
{ MATROSKA_ID_CODECDOWNLOADURL, EBML_NONE },
322
{ MATROSKA_ID_TRACKMINCACHE, EBML_NONE },
323
{ MATROSKA_ID_TRACKMAXCACHE, EBML_NONE },
324
{ MATROSKA_ID_TRACKMAXBLKADDID, EBML_NONE },
328
static EbmlSyntax matroska_tracks[] = {
329
{ MATROSKA_ID_TRACKENTRY, EBML_NEST, sizeof(MatroskaTrack), offsetof(MatroskaDemuxContext,tracks), {.n=matroska_track} },
333
static EbmlSyntax matroska_attachment[] = {
334
{ MATROSKA_ID_FILENAME, EBML_UTF8, 0, offsetof(MatroskaAttachement,filename) },
335
{ MATROSKA_ID_FILEMIMETYPE, EBML_STR, 0, offsetof(MatroskaAttachement,mime) },
336
{ MATROSKA_ID_FILEDATA, EBML_BIN, 0, offsetof(MatroskaAttachement,bin) },
337
{ MATROSKA_ID_FILEDESC, EBML_NONE },
338
{ MATROSKA_ID_FILEUID, EBML_NONE },
342
static EbmlSyntax matroska_attachments[] = {
343
{ MATROSKA_ID_ATTACHEDFILE, EBML_NEST, sizeof(MatroskaAttachement), offsetof(MatroskaDemuxContext,attachments), {.n=matroska_attachment} },
347
static EbmlSyntax matroska_chapter_display[] = {
348
{ MATROSKA_ID_CHAPSTRING, EBML_UTF8, 0, offsetof(MatroskaChapter,title) },
349
{ MATROSKA_ID_CHAPLANG, EBML_NONE },
353
static EbmlSyntax matroska_chapter_entry[] = {
354
{ MATROSKA_ID_CHAPTERTIMESTART, EBML_UINT, 0, offsetof(MatroskaChapter,start), {.u=AV_NOPTS_VALUE} },
355
{ MATROSKA_ID_CHAPTERTIMEEND, EBML_UINT, 0, offsetof(MatroskaChapter,end), {.u=AV_NOPTS_VALUE} },
356
{ MATROSKA_ID_CHAPTERUID, EBML_UINT, 0, offsetof(MatroskaChapter,uid) },
357
{ MATROSKA_ID_CHAPTERDISPLAY, EBML_NEST, 0, 0, {.n=matroska_chapter_display} },
358
{ MATROSKA_ID_CHAPTERFLAGHIDDEN, EBML_NONE },
359
{ MATROSKA_ID_CHAPTERFLAGENABLED, EBML_NONE },
360
{ MATROSKA_ID_CHAPTERPHYSEQUIV, EBML_NONE },
361
{ MATROSKA_ID_CHAPTERATOM, EBML_NONE },
365
static EbmlSyntax matroska_chapter[] = {
366
{ MATROSKA_ID_CHAPTERATOM, EBML_NEST, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext,chapters), {.n=matroska_chapter_entry} },
367
{ MATROSKA_ID_EDITIONUID, EBML_NONE },
368
{ MATROSKA_ID_EDITIONFLAGHIDDEN, EBML_NONE },
369
{ MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE },
370
{ MATROSKA_ID_EDITIONFLAGORDERED, EBML_NONE },
374
static EbmlSyntax matroska_chapters[] = {
375
{ MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, 0, {.n=matroska_chapter} },
379
static EbmlSyntax matroska_index_pos[] = {
380
{ MATROSKA_ID_CUETRACK, EBML_UINT, 0, offsetof(MatroskaIndexPos,track) },
381
{ MATROSKA_ID_CUECLUSTERPOSITION, EBML_UINT, 0, offsetof(MatroskaIndexPos,pos) },
382
{ MATROSKA_ID_CUEBLOCKNUMBER, EBML_NONE },
386
static EbmlSyntax matroska_index_entry[] = {
387
{ MATROSKA_ID_CUETIME, EBML_UINT, 0, offsetof(MatroskaIndex,time) },
388
{ MATROSKA_ID_CUETRACKPOSITION, EBML_NEST, sizeof(MatroskaIndexPos), offsetof(MatroskaIndex,pos), {.n=matroska_index_pos} },
392
static EbmlSyntax matroska_index[] = {
393
{ MATROSKA_ID_POINTENTRY, EBML_NEST, sizeof(MatroskaIndex), offsetof(MatroskaDemuxContext,index), {.n=matroska_index_entry} },
397
static EbmlSyntax matroska_simpletag[] = {
398
{ MATROSKA_ID_TAGNAME, EBML_UTF8, 0, offsetof(MatroskaTag,name) },
399
{ MATROSKA_ID_TAGSTRING, EBML_UTF8, 0, offsetof(MatroskaTag,string) },
400
{ MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTag,sub), {.n=matroska_simpletag} },
401
{ MATROSKA_ID_TAGLANG, EBML_NONE },
402
{ MATROSKA_ID_TAGDEFAULT, EBML_NONE },
406
static EbmlSyntax matroska_tag[] = {
407
{ MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), 0, {.n=matroska_simpletag} },
408
{ MATROSKA_ID_TAGTARGETS, EBML_NONE },
412
static EbmlSyntax matroska_tags[] = {
413
{ MATROSKA_ID_TAG, EBML_NEST, 0, offsetof(MatroskaDemuxContext,tags), {.n=matroska_tag} },
417
static EbmlSyntax matroska_seekhead_entry[] = {
418
{ MATROSKA_ID_SEEKID, EBML_UINT, 0, offsetof(MatroskaSeekhead,id) },
419
{ MATROSKA_ID_SEEKPOSITION, EBML_UINT, 0, offsetof(MatroskaSeekhead,pos), {.u=-1} },
423
static EbmlSyntax matroska_seekhead[] = {
424
{ MATROSKA_ID_SEEKENTRY, EBML_NEST, sizeof(MatroskaSeekhead), offsetof(MatroskaDemuxContext,seekhead), {.n=matroska_seekhead_entry} },
428
static EbmlSyntax matroska_segment[] = {
429
{ MATROSKA_ID_INFO, EBML_NEST, 0, 0, {.n=matroska_info } },
430
{ MATROSKA_ID_TRACKS, EBML_NEST, 0, 0, {.n=matroska_tracks } },
431
{ MATROSKA_ID_ATTACHMENTS, EBML_NEST, 0, 0, {.n=matroska_attachments} },
432
{ MATROSKA_ID_CHAPTERS, EBML_NEST, 0, 0, {.n=matroska_chapters } },
433
{ MATROSKA_ID_CUES, EBML_NEST, 0, 0, {.n=matroska_index } },
434
{ MATROSKA_ID_TAGS, EBML_NEST, 0, 0, {.n=matroska_tags } },
435
{ MATROSKA_ID_SEEKHEAD, EBML_NEST, 0, 0, {.n=matroska_seekhead } },
436
{ MATROSKA_ID_CLUSTER, EBML_STOP, 0, offsetof(MatroskaDemuxContext,has_cluster_id) },
440
static EbmlSyntax matroska_segments[] = {
441
{ MATROSKA_ID_SEGMENT, EBML_NEST, 0, 0, {.n=matroska_segment } },
445
static EbmlSyntax matroska_blockgroup[] = {
446
{ MATROSKA_ID_BLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) },
447
{ MATROSKA_ID_SIMPLEBLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) },
448
{ MATROSKA_ID_BLOCKDURATION, EBML_UINT, 0, offsetof(MatroskaBlock,duration), {.u=AV_NOPTS_VALUE} },
449
{ MATROSKA_ID_BLOCKREFERENCE, EBML_UINT, 0, offsetof(MatroskaBlock,reference) },
453
static EbmlSyntax matroska_cluster[] = {
454
{ MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) },
455
{ MATROSKA_ID_BLOCKGROUP, EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
456
{ MATROSKA_ID_SIMPLEBLOCK, EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
457
{ MATROSKA_ID_CLUSTERPOSITION,EBML_NONE },
458
{ MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE },
462
static EbmlSyntax matroska_clusters[] = {
463
{ MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster} },
464
{ MATROSKA_ID_INFO, EBML_NONE },
465
{ MATROSKA_ID_CUES, EBML_NONE },
466
{ MATROSKA_ID_TAGS, EBML_NONE },
467
{ MATROSKA_ID_SEEKHEAD, EBML_NONE },
471
#define SIZE_OFF(x) sizeof(((AVFormatContext*)0)->x),offsetof(AVFormatContext,x)
477
{ "TITLE", SIZE_OFF(title) },
478
{ "ARTIST", SIZE_OFF(author) },
479
{ "WRITTEN_BY", SIZE_OFF(author) },
480
{ "LEAD_PERFORMER", SIZE_OFF(author) },
481
{ "COPYRIGHT", SIZE_OFF(copyright) },
482
{ "COMMENT", SIZE_OFF(comment) },
483
{ "ALBUM", SIZE_OFF(album) },
484
{ "DATE_WRITTEN", SIZE_OFF(year) },
485
{ "DATE_RELEASED", SIZE_OFF(year) },
486
{ "PART_NUMBER", SIZE_OFF(track) },
487
{ "GENRE", SIZE_OFF(genre) },
491
* Return: Whether we reached the end of a level in the hierarchy or not.
493
static int ebml_level_end(MatroskaDemuxContext *matroska)
178
495
ByteIOContext *pb = matroska->ctx->pb;
179
offset_t pos = url_ftell(pb);
496
int64_t pos = url_ftell(pb);
182
while (matroska->num_levels > 0) {
498
if (matroska->num_levels > 0) {
183
499
MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
185
if (pos >= level->start + level->length) {
500
if (pos - level->start >= level->length) {
186
501
matroska->num_levels--;
892
825
total = (total << 8) | p->buf[4 + n++];
894
/* does the probe data contain the whole header? */
827
/* Does the probe data contain the whole header? */
895
828
if (p->buf_size < 4 + size + total)
898
/* the header must contain the document type 'matroska'. For now,
831
/* The header must contain the document type 'matroska'. For now,
899
832
* we don't parse the whole header but simply check for the
900
833
* availability of that array of characters inside the header.
901
834
* Not fully fool-proof, but good enough. */
902
for (n = 4 + size; n <= 4 + size + total - sizeof(probe_data); n++)
903
if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
835
for (n = 4+size; n <= 4+size+total-(sizeof(probe_data)-1); n++)
836
if (!memcmp(p->buf+n, probe_data, sizeof(probe_data)-1))
904
837
return AVPROBE_SCORE_MAX;
910
* From here on, it's all XML-style DTD stuff... Needs no comments.
914
matroska_parse_info (MatroskaDemuxContext *matroska)
919
av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
922
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
925
} else if (matroska->level_up) {
926
matroska->level_up--;
931
/* cluster timecode */
932
case MATROSKA_ID_TIMECODESCALE: {
934
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
936
matroska->time_scale = num;
940
case MATROSKA_ID_DURATION: {
942
if ((res = ebml_read_float(matroska, &id, &num)) < 0)
944
matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
948
case MATROSKA_ID_TITLE: {
950
if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
952
strncpy(matroska->ctx->title, text,
953
sizeof(matroska->ctx->title)-1);
958
case MATROSKA_ID_WRITINGAPP: {
960
if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
962
matroska->writing_app = text;
966
case MATROSKA_ID_MUXINGAPP: {
968
if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
970
matroska->muxing_app = text;
974
case MATROSKA_ID_DATEUTC: {
976
if ((res = ebml_read_date(matroska, &id, &time)) < 0)
978
matroska->created = time;
983
av_log(matroska->ctx, AV_LOG_INFO,
984
"Unknown entry 0x%x in info header\n", id);
988
res = ebml_read_skip(matroska);
992
if (matroska->level_up) {
993
matroska->level_up--;
1002
matroska_add_stream (MatroskaDemuxContext *matroska)
1006
MatroskaTrack *track;
1008
av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
1010
/* Allocate a generic track. As soon as we know its type we'll realloc. */
1011
track = av_mallocz(MAX_TRACK_SIZE);
1012
matroska->num_tracks++;
1013
strcpy(track->language, "eng");
1015
/* start with the master */
1016
if ((res = ebml_read_master(matroska, &id)) < 0)
1019
/* try reading the trackentry headers */
1021
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1024
} else if (matroska->level_up > 0) {
1025
matroska->level_up--;
1030
/* track number (unique stream ID) */
1031
case MATROSKA_ID_TRACKNUMBER: {
1033
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1039
/* track UID (unique identifier) */
1040
case MATROSKA_ID_TRACKUID: {
1042
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1048
/* track type (video, audio, combined, subtitle, etc.) */
1049
case MATROSKA_ID_TRACKTYPE: {
1051
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1053
if (track->type && track->type != num) {
1054
av_log(matroska->ctx, AV_LOG_INFO,
1055
"More than one tracktype in an entry - skip\n");
1060
switch (track->type) {
1061
case MATROSKA_TRACK_TYPE_VIDEO:
1062
case MATROSKA_TRACK_TYPE_AUDIO:
1063
case MATROSKA_TRACK_TYPE_SUBTITLE:
1065
case MATROSKA_TRACK_TYPE_COMPLEX:
1066
case MATROSKA_TRACK_TYPE_LOGO:
1067
case MATROSKA_TRACK_TYPE_CONTROL:
1069
av_log(matroska->ctx, AV_LOG_INFO,
1070
"Unknown or unsupported track type 0x%x\n",
1075
matroska->tracks[matroska->num_tracks - 1] = track;
1079
/* tracktype specific stuff for video */
1080
case MATROSKA_ID_TRACKVIDEO: {
1081
MatroskaVideoTrack *videotrack;
1083
track->type = MATROSKA_TRACK_TYPE_VIDEO;
1084
if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1085
av_log(matroska->ctx, AV_LOG_INFO,
1086
"video data in non-video track - ignoring\n");
1087
res = AVERROR_INVALIDDATA;
1089
} else if ((res = ebml_read_master(matroska, &id)) < 0)
1091
videotrack = (MatroskaVideoTrack *)track;
1094
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1097
} else if (matroska->level_up > 0) {
1098
matroska->level_up--;
1103
/* fixme, this should be one-up, but I get it here */
1104
case MATROSKA_ID_TRACKDEFAULTDURATION: {
1106
if ((res = ebml_read_uint (matroska, &id,
1109
track->default_duration = num;
1113
/* video framerate */
1114
case MATROSKA_ID_VIDEOFRAMERATE: {
1116
if ((res = ebml_read_float(matroska, &id,
1119
if (!track->default_duration)
1120
track->default_duration = 1000000000/num;
1124
/* width of the size to display the video at */
1125
case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1127
if ((res = ebml_read_uint(matroska, &id,
1130
videotrack->display_width = num;
1134
/* height of the size to display the video at */
1135
case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1137
if ((res = ebml_read_uint(matroska, &id,
1140
videotrack->display_height = num;
1144
/* width of the video in the file */
1145
case MATROSKA_ID_VIDEOPIXELWIDTH: {
1147
if ((res = ebml_read_uint(matroska, &id,
1150
videotrack->pixel_width = num;
1154
/* height of the video in the file */
1155
case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1157
if ((res = ebml_read_uint(matroska, &id,
1160
videotrack->pixel_height = num;
1164
/* whether the video is interlaced */
1165
case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1167
if ((res = ebml_read_uint(matroska, &id,
1172
MATROSKA_VIDEOTRACK_INTERLACED;
1175
~MATROSKA_VIDEOTRACK_INTERLACED;
1179
/* stereo mode (whether the video has two streams,
1180
* where one is for the left eye and the other for
1181
* the right eye, which creates a 3D-like
1183
case MATROSKA_ID_VIDEOSTEREOMODE: {
1185
if ((res = ebml_read_uint(matroska, &id,
1188
if (num != MATROSKA_EYE_MODE_MONO &&
1189
num != MATROSKA_EYE_MODE_LEFT &&
1190
num != MATROSKA_EYE_MODE_RIGHT &&
1191
num != MATROSKA_EYE_MODE_BOTH) {
1192
av_log(matroska->ctx, AV_LOG_INFO,
1193
"Ignoring unknown eye mode 0x%x\n",
1197
videotrack->eye_mode = num;
1201
/* aspect ratio behaviour */
1202
case MATROSKA_ID_VIDEOASPECTRATIO: {
1204
if ((res = ebml_read_uint(matroska, &id,
1207
if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
1208
num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
1209
num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
1210
av_log(matroska->ctx, AV_LOG_INFO,
1211
"Ignoring unknown aspect ratio 0x%x\n",
1215
videotrack->ar_mode = num;
1219
/* colorspace (only matters for raw video)
1221
case MATROSKA_ID_VIDEOCOLORSPACE: {
1223
if ((res = ebml_read_uint(matroska, &id,
1226
videotrack->fourcc = num;
1231
av_log(matroska->ctx, AV_LOG_INFO,
1232
"Unknown video track header entry "
1233
"0x%x - ignoring\n", id);
1237
res = ebml_read_skip(matroska);
1241
if (matroska->level_up) {
1242
matroska->level_up--;
1249
/* tracktype specific stuff for audio */
1250
case MATROSKA_ID_TRACKAUDIO: {
1251
MatroskaAudioTrack *audiotrack;
1253
track->type = MATROSKA_TRACK_TYPE_AUDIO;
1254
if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1255
av_log(matroska->ctx, AV_LOG_INFO,
1256
"audio data in non-audio track - ignoring\n");
1257
res = AVERROR_INVALIDDATA;
1259
} else if ((res = ebml_read_master(matroska, &id)) < 0)
1261
audiotrack = (MatroskaAudioTrack *)track;
1262
audiotrack->channels = 1;
1263
audiotrack->samplerate = 8000;
1266
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1269
} else if (matroska->level_up > 0) {
1270
matroska->level_up--;
1276
case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1278
if ((res = ebml_read_float(matroska, &id,
1281
audiotrack->internal_samplerate =
1282
audiotrack->samplerate = num;
1286
case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
1288
if ((res = ebml_read_float(matroska, &id,
1291
audiotrack->samplerate = num;
1296
case MATROSKA_ID_AUDIOBITDEPTH: {
1298
if ((res = ebml_read_uint(matroska, &id,
1301
audiotrack->bitdepth = num;
1306
case MATROSKA_ID_AUDIOCHANNELS: {
1308
if ((res = ebml_read_uint(matroska, &id,
1311
audiotrack->channels = num;
1316
av_log(matroska->ctx, AV_LOG_INFO,
1317
"Unknown audio track header entry "
1318
"0x%x - ignoring\n", id);
1322
res = ebml_read_skip(matroska);
1326
if (matroska->level_up) {
1327
matroska->level_up--;
1334
/* codec identifier */
1335
case MATROSKA_ID_CODECID: {
1337
if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1339
track->codec_id = text;
1343
/* codec private data */
1344
case MATROSKA_ID_CODECPRIVATE: {
1347
if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1349
track->codec_priv = data;
1350
track->codec_priv_size = size;
1354
/* name of the codec */
1355
case MATROSKA_ID_CODECNAME: {
1357
if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1359
track->codec_name = text;
1363
/* name of this track */
1364
case MATROSKA_ID_TRACKNAME: {
1366
if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1372
/* language (matters for audio/subtitles, mostly) */
1373
case MATROSKA_ID_TRACKLANGUAGE: {
1375
if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1377
if ((end = strchr(text, '-')))
1379
if (strlen(text) == 3)
1380
strcpy(track->language, text);
1385
/* whether this is actually used */
1386
case MATROSKA_ID_TRACKFLAGENABLED: {
1388
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1391
track->flags |= MATROSKA_TRACK_ENABLED;
1393
track->flags &= ~MATROSKA_TRACK_ENABLED;
1397
/* whether it's the default for this track type */
1398
case MATROSKA_ID_TRACKFLAGDEFAULT: {
1400
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1403
track->flags |= MATROSKA_TRACK_DEFAULT;
1405
track->flags &= ~MATROSKA_TRACK_DEFAULT;
1409
/* lacing (like MPEG, where blocks don't end/start on frame
1411
case MATROSKA_ID_TRACKFLAGLACING: {
1413
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1416
track->flags |= MATROSKA_TRACK_LACING;
1418
track->flags &= ~MATROSKA_TRACK_LACING;
1422
/* default length (in time) of one data block in this track */
1423
case MATROSKA_ID_TRACKDEFAULTDURATION: {
1425
if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1427
track->default_duration = num;
1432
av_log(matroska->ctx, AV_LOG_INFO,
1433
"Unknown track header entry 0x%x - ignoring\n", id);
1437
/* we ignore these because they're nothing useful. */
1438
case MATROSKA_ID_CODECINFOURL:
1439
case MATROSKA_ID_CODECDOWNLOADURL:
1440
case MATROSKA_ID_TRACKMINCACHE:
1441
case MATROSKA_ID_TRACKMAXCACHE:
1442
res = ebml_read_skip(matroska);
1446
if (matroska->level_up) {
1447
matroska->level_up--;
1456
matroska_parse_tracks (MatroskaDemuxContext *matroska)
1461
av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
1464
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1467
} else if (matroska->level_up) {
1468
matroska->level_up--;
1473
/* one track within the "all-tracks" header */
1474
case MATROSKA_ID_TRACKENTRY:
1475
res = matroska_add_stream(matroska);
1479
av_log(matroska->ctx, AV_LOG_INFO,
1480
"Unknown entry 0x%x in track header\n", id);
1484
res = ebml_read_skip(matroska);
1488
if (matroska->level_up) {
1489
matroska->level_up--;
1498
matroska_parse_index (MatroskaDemuxContext *matroska)
1502
MatroskaDemuxIndex idx;
1504
av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
1507
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1510
} else if (matroska->level_up) {
1511
matroska->level_up--;
1516
/* one single index entry ('point') */
1517
case MATROSKA_ID_POINTENTRY:
1518
if ((res = ebml_read_master(matroska, &id)) < 0)
1521
/* in the end, we hope to fill one entry with a
1522
* timestamp, a file position and a tracknum */
1523
idx.pos = (uint64_t) -1;
1524
idx.time = (uint64_t) -1;
1525
idx.track = (uint16_t) -1;
1528
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1531
} else if (matroska->level_up) {
1532
matroska->level_up--;
1537
/* one single index entry ('point') */
1538
case MATROSKA_ID_CUETIME: {
1540
if ((res = ebml_read_uint(matroska, &id,
1543
idx.time = time * matroska->time_scale;
1547
/* position in the file + track to which it
1549
case MATROSKA_ID_CUETRACKPOSITION:
1550
if ((res = ebml_read_master(matroska, &id)) < 0)
1554
if (!(id = ebml_peek_id (matroska,
1555
&matroska->level_up))) {
1558
} else if (matroska->level_up) {
1559
matroska->level_up--;
1565
case MATROSKA_ID_CUETRACK: {
1567
if ((res = ebml_read_uint(matroska,
1574
/* position in file */
1575
case MATROSKA_ID_CUECLUSTERPOSITION: {
1577
if ((res = ebml_read_uint(matroska,
1580
idx.pos = num+matroska->segment_start;
1585
av_log(matroska->ctx, AV_LOG_INFO,
1586
"Unknown entry 0x%x in "
1587
"CuesTrackPositions\n", id);
1591
res = ebml_read_skip(matroska);
1595
if (matroska->level_up) {
1596
matroska->level_up--;
1604
av_log(matroska->ctx, AV_LOG_INFO,
1605
"Unknown entry 0x%x in cuespoint "
1610
res = ebml_read_skip(matroska);
1614
if (matroska->level_up) {
1615
matroska->level_up--;
1620
/* so let's see if we got what we wanted */
1621
if (idx.pos != (uint64_t) -1 &&
1622
idx.time != (uint64_t) -1 &&
1623
idx.track != (uint16_t) -1) {
1624
if (matroska->num_indexes % 32 == 0) {
1625
/* re-allocate bigger index */
1627
av_realloc(matroska->index,
1628
(matroska->num_indexes + 32) *
1629
sizeof(MatroskaDemuxIndex));
1631
matroska->index[matroska->num_indexes] = idx;
1632
matroska->num_indexes++;
1637
av_log(matroska->ctx, AV_LOG_INFO,
1638
"Unknown entry 0x%x in cues header\n", id);
1642
res = ebml_read_skip(matroska);
1646
if (matroska->level_up) {
1647
matroska->level_up--;
1656
matroska_parse_metadata (MatroskaDemuxContext *matroska)
1662
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1665
} else if (matroska->level_up) {
1666
matroska->level_up--;
1671
/* Hm, this is unsupported... */
1673
av_log(matroska->ctx, AV_LOG_INFO,
1674
"Unknown entry 0x%x in metadata header\n", id);
1678
res = ebml_read_skip(matroska);
1682
if (matroska->level_up) {
1683
matroska->level_up--;
1692
matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1697
av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
1700
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1703
} else if (matroska->level_up) {
1704
matroska->level_up--;
1709
case MATROSKA_ID_SEEKENTRY: {
1710
uint32_t seek_id = 0, peek_id_cache = 0;
1711
uint64_t seek_pos = (uint64_t) -1, t;
1713
if ((res = ebml_read_master(matroska, &id)) < 0)
1717
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1720
} else if (matroska->level_up) {
1721
matroska->level_up--;
1726
case MATROSKA_ID_SEEKID:
1727
res = ebml_read_uint(matroska, &id, &t);
1731
case MATROSKA_ID_SEEKPOSITION:
1732
res = ebml_read_uint(matroska, &id, &seek_pos);
1736
av_log(matroska->ctx, AV_LOG_INFO,
1737
"Unknown seekhead ID 0x%x\n", id);
1741
res = ebml_read_skip(matroska);
1745
if (matroska->level_up) {
1746
matroska->level_up--;
1751
if (!seek_id || seek_pos == (uint64_t) -1) {
1752
av_log(matroska->ctx, AV_LOG_INFO,
1753
"Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1759
case MATROSKA_ID_CUES:
1760
case MATROSKA_ID_TAGS: {
1761
uint32_t level_up = matroska->level_up;
1762
offset_t before_pos;
1764
MatroskaLevel level;
1766
/* remember the peeked ID and the current position */
1767
peek_id_cache = matroska->peek_id;
1768
before_pos = url_ftell(matroska->ctx->pb);
1771
if ((res = ebml_read_seek(matroska, seek_pos +
1772
matroska->segment_start)) < 0)
1775
/* we don't want to lose our seekhead level, so we add
1776
* a dummy. This is a crude hack. */
1777
if (matroska->num_levels == EBML_MAX_DEPTH) {
1778
av_log(matroska->ctx, AV_LOG_INFO,
1779
"Max EBML element depth (%d) reached, "
1780
"cannot parse further.\n", EBML_MAX_DEPTH);
1781
return AVERROR_UNKNOWN;
1785
level.length = (uint64_t)-1;
1786
matroska->levels[matroska->num_levels] = level;
1787
matroska->num_levels++;
1790
if (!(id = ebml_peek_id (matroska,
1791
&matroska->level_up)))
1793
if (id != seek_id) {
1794
av_log(matroska->ctx, AV_LOG_INFO,
1795
"We looked for ID=0x%x but got "
1796
"ID=0x%x (pos=%"PRIu64")",
1797
seek_id, id, seek_pos +
1798
matroska->segment_start);
1802
/* read master + parse */
1803
if ((res = ebml_read_master(matroska, &id)) < 0)
1806
case MATROSKA_ID_CUES:
1807
if (!(res = matroska_parse_index(matroska)) ||
1808
url_feof(matroska->ctx->pb)) {
1809
matroska->index_parsed = 1;
1813
case MATROSKA_ID_TAGS:
1814
if (!(res = matroska_parse_metadata(matroska)) ||
1815
url_feof(matroska->ctx->pb)) {
1816
matroska->metadata_parsed = 1;
1823
/* remove dummy level */
1824
while (matroska->num_levels) {
1825
matroska->num_levels--;
1827
matroska->levels[matroska->num_levels].length;
1828
if (length == (uint64_t)-1)
1833
if ((res = ebml_read_seek(matroska, before_pos)) < 0)
1835
matroska->peek_id = peek_id_cache;
1836
matroska->level_up = level_up;
1841
av_log(matroska->ctx, AV_LOG_INFO,
1842
"Ignoring seekhead entry for ID=0x%x\n",
1851
av_log(matroska->ctx, AV_LOG_INFO,
1852
"Unknown seekhead ID 0x%x\n", id);
1856
res = ebml_read_skip(matroska);
1860
if (matroska->level_up) {
1861
matroska->level_up--;
1870
matroska_parse_attachments(AVFormatContext *s)
1872
MatroskaDemuxContext *matroska = s->priv_data;
1876
av_log(matroska->ctx, AV_LOG_DEBUG, "parsing attachments...\n");
1879
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1882
} else if (matroska->level_up) {
1883
matroska->level_up--;
1888
case MATROSKA_ID_ATTACHEDFILE: {
1891
uint8_t* data = NULL;
1892
int i, data_size = 0;
1895
if ((res = ebml_read_master(matroska, &id)) < 0)
1899
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1902
} else if (matroska->level_up) {
1903
matroska->level_up--;
1908
case MATROSKA_ID_FILENAME:
1909
res = ebml_read_utf8 (matroska, &id, &name);
1912
case MATROSKA_ID_FILEMIMETYPE:
1913
res = ebml_read_ascii (matroska, &id, &mime);
1916
case MATROSKA_ID_FILEDATA:
1917
res = ebml_read_binary(matroska, &id, &data, &data_size);
1921
av_log(matroska->ctx, AV_LOG_INFO,
1922
"Unknown attachedfile ID 0x%x\n", id);
1924
res = ebml_read_skip(matroska);
1928
if (matroska->level_up) {
1929
matroska->level_up--;
1934
if (!(name && mime && data && data_size > 0)) {
1935
av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
1939
st = av_new_stream(s, matroska->num_streams++);
1941
return AVERROR(ENOMEM);
1942
st->filename = av_strdup(name);
1943
st->codec->codec_id = CODEC_ID_NONE;
1944
st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
1945
st->codec->extradata = av_malloc(data_size);
1946
if(st->codec->extradata == NULL)
1947
return AVERROR(ENOMEM);
1948
st->codec->extradata_size = data_size;
1949
memcpy(st->codec->extradata, data, data_size);
1951
for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
1952
if (!strncmp(ff_mkv_mime_tags[i].str, mime,
1953
strlen(ff_mkv_mime_tags[i].str))) {
1954
st->codec->codec_id = ff_mkv_mime_tags[i].id;
1959
av_log(matroska->ctx, AV_LOG_DEBUG, "new attachment: %s, %s, size %d \n", name, mime, data_size);
842
static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska,
845
MatroskaTrack *tracks = matroska->tracks.elem;
848
for (i=0; i < matroska->tracks.nb_elem; i++)
849
if (tracks[i].num == num)
852
av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num);
856
static int matroska_decode_buffer(uint8_t** buf, int* buf_size,
857
MatroskaTrack *track)
859
MatroskaTrackEncoding *encodings = track->encodings.elem;
860
uint8_t* data = *buf;
861
int isize = *buf_size;
862
uint8_t* pkt_data = NULL;
863
int pkt_size = isize;
867
switch (encodings[0].compression.algo) {
868
case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
869
return encodings[0].compression.settings.size;
870
case MATROSKA_TRACK_ENCODING_COMP_LZO:
872
olen = pkt_size *= 3;
873
pkt_data = av_realloc(pkt_data,
874
pkt_size+LZO_OUTPUT_PADDING);
875
result = lzo1x_decode(pkt_data, &olen, data, &isize);
876
} while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
882
case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
883
z_stream zstream = {0};
884
if (inflateInit(&zstream) != Z_OK)
886
zstream.next_in = data;
887
zstream.avail_in = isize;
890
pkt_data = av_realloc(pkt_data, pkt_size);
891
zstream.avail_out = pkt_size - zstream.total_out;
892
zstream.next_out = pkt_data + zstream.total_out;
893
result = inflate(&zstream, Z_NO_FLUSH);
894
} while (result==Z_OK && pkt_size<10000000);
895
pkt_size = zstream.total_out;
896
inflateEnd(&zstream);
897
if (result != Z_STREAM_END)
903
case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
904
bz_stream bzstream = {0};
905
if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
907
bzstream.next_in = data;
908
bzstream.avail_in = isize;
911
pkt_data = av_realloc(pkt_data, pkt_size);
912
bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
913
bzstream.next_out = pkt_data + bzstream.total_out_lo32;
914
result = BZ2_bzDecompress(&bzstream);
915
} while (result==BZ_OK && pkt_size<10000000);
916
pkt_size = bzstream.total_out_lo32;
917
BZ2_bzDecompressEnd(&bzstream);
918
if (result != BZ_STREAM_END)
928
*buf_size = pkt_size;
935
static void matroska_fix_ass_packet(MatroskaDemuxContext *matroska,
936
AVPacket *pkt, uint64_t display_duration)
938
char *line, *layer, *ptr = pkt->data, *end = ptr+pkt->size;
939
for (; *ptr!=',' && ptr<end-1; ptr++);
942
for (; *ptr!=',' && ptr<end-1; ptr++);
944
int64_t end_pts = pkt->pts + display_duration;
945
int sc = matroska->time_scale * pkt->pts / 10000000;
946
int ec = matroska->time_scale * end_pts / 10000000;
947
int sh, sm, ss, eh, em, es, len;
948
sh = sc/360000; sc -= 360000*sh;
949
sm = sc/ 6000; sc -= 6000*sm;
950
ss = sc/ 100; sc -= 100*ss;
951
eh = ec/360000; ec -= 360000*eh;
952
em = ec/ 6000; ec -= 6000*em;
953
es = ec/ 100; ec -= 100*es;
955
len = 50 + end-ptr + FF_INPUT_BUFFER_PADDING_SIZE;
956
if (!(line = av_malloc(len)))
958
snprintf(line,len,"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
959
layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
962
pkt->size = strlen(line);
966
static void matroska_merge_packets(AVPacket *out, AVPacket *in)
968
out->data = av_realloc(out->data, out->size+in->size);
969
memcpy(out->data+out->size, in->data, in->size);
970
out->size += in->size;
971
av_destruct_packet(in);
975
static void matroska_convert_tags(AVFormatContext *s, EbmlList *list)
977
MatroskaTag *tags = list->elem;
980
for (i=0; i < list->nb_elem; i++) {
981
for (j=0; j < FF_ARRAY_ELEMS(metadata); j++){
982
if (!strcmp(tags[i].name, metadata[j].name)) {
983
int *ptr = (int *)((char *)s + metadata[j].offset);
985
if (metadata[j].size > sizeof(int))
986
av_strlcpy((char *)ptr, tags[i].string, metadata[j].size);
988
*ptr = atoi(tags[i].string);
991
if (tags[i].sub.nb_elem)
992
matroska_convert_tags(s, &tags[i].sub);
996
static void matroska_execute_seekhead(MatroskaDemuxContext *matroska)
998
EbmlList *seekhead_list = &matroska->seekhead;
999
MatroskaSeekhead *seekhead = seekhead_list->elem;
1000
uint32_t level_up = matroska->level_up;
1001
int64_t before_pos = url_ftell(matroska->ctx->pb);
1002
MatroskaLevel level;
1005
for (i=0; i<seekhead_list->nb_elem; i++) {
1006
int64_t offset = seekhead[i].pos + matroska->segment_start;
1008
if (seekhead[i].pos <= before_pos
1009
|| seekhead[i].id == MATROSKA_ID_SEEKHEAD
1010
|| seekhead[i].id == MATROSKA_ID_CLUSTER)
1014
if (url_fseek(matroska->ctx->pb, offset, SEEK_SET) != offset)
1017
/* We don't want to lose our seekhead level, so we add
1018
* a dummy. This is a crude hack. */
1019
if (matroska->num_levels == EBML_MAX_DEPTH) {
1964
1020
av_log(matroska->ctx, AV_LOG_INFO,
1965
"Unknown attachments ID 0x%x\n", id);
1969
res = ebml_read_skip(matroska);
1021
"Max EBML element depth (%d) reached, "
1022
"cannot parse further.\n", EBML_MAX_DEPTH);
1973
if (matroska->level_up) {
1974
matroska->level_up--;
1027
level.length = (uint64_t)-1;
1028
matroska->levels[matroska->num_levels] = level;
1029
matroska->num_levels++;
1031
ebml_parse(matroska, matroska_segment, matroska);
1033
/* remove dummy level */
1034
while (matroska->num_levels) {
1035
uint64_t length = matroska->levels[--matroska->num_levels].length;
1036
if (length == (uint64_t)-1)
1042
url_fseek(matroska->ctx->pb, before_pos, SEEK_SET);
1043
matroska->level_up = level_up;
1982
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
1985
matroska_aac_profile (char *codec_id)
1046
static int matroska_aac_profile(char *codec_id)
1987
static const char *aac_profiles[] = {
1048
static const char * const aac_profiles[] = { "MAIN", "LC", "SSR" };
1992
for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
1051
for (profile=0; profile<FF_ARRAY_ELEMS(aac_profiles); profile++)
1993
1052
if (strstr(codec_id, aac_profiles[profile]))
1995
1054
return profile + 1;
1999
matroska_aac_sri (int samplerate)
1057
static int matroska_aac_sri(int samplerate)
2003
for (sri=0; sri<ARRAY_SIZE(ff_mpeg4audio_sample_rates); sri++)
1061
for (sri=0; sri<FF_ARRAY_ELEMS(ff_mpeg4audio_sample_rates); sri++)
2004
1062
if (ff_mpeg4audio_sample_rates[sri] == samplerate)
2010
matroska_read_header (AVFormatContext *s,
2011
AVFormatParameters *ap)
1067
static int matroska_read_header(AVFormatContext *s, AVFormatParameters *ap)
2013
1069
MatroskaDemuxContext *matroska = s->priv_data;
2015
int version, last_level, res = 0;
1070
EbmlList *attachements_list = &matroska->attachments;
1071
MatroskaAttachement *attachements;
1072
EbmlList *chapters_list = &matroska->chapters;
1073
MatroskaChapter *chapters;
1074
MatroskaTrack *tracks;
1075
EbmlList *index_list;
1076
MatroskaIndex *index;
2018
1081
matroska->ctx = s;
2020
1083
/* First read the EBML header. */
2022
if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
2024
if ((doctype == NULL) || strcmp(doctype, "matroska")) {
2025
av_log(matroska->ctx, AV_LOG_ERROR,
2026
"Wrong EBML doctype ('%s' != 'matroska').\n",
2027
doctype ? doctype : "(none)");
2030
return AVERROR_NOFMT;
2034
av_log(matroska->ctx, AV_LOG_ERROR,
2035
"Matroska demuxer version 2 too old for file version %d\n",
2037
return AVERROR_NOFMT;
1084
if (ebml_parse(matroska, ebml_syntax, &ebml)
1085
|| ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t)
1086
|| ebml.id_length > sizeof(uint32_t) || strcmp(ebml.doctype, "matroska")
1087
|| ebml.doctype_version > 2) {
1088
av_log(matroska->ctx, AV_LOG_ERROR,
1089
"EBML header using unsupported features\n"
1090
"(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
1091
ebml.version, ebml.doctype, ebml.doctype_version);
1092
return AVERROR_NOFMT;
1094
ebml_free(ebml_syntax, &ebml);
2040
1096
/* The next thing is a segment. */
2042
if (!(id = ebml_peek_id(matroska, &last_level)))
2043
return AVERROR(EIO);
2044
if (id == MATROSKA_ID_SEGMENT)
2048
av_log(matroska->ctx, AV_LOG_INFO,
2049
"Expected a Segment ID (0x%x), but received 0x%x!\n",
2050
MATROSKA_ID_SEGMENT, id);
2051
if ((res = ebml_read_skip(matroska)) < 0)
2055
/* We now have a Matroska segment.
2056
* Seeks are from the beginning of the segment,
2057
* after the segment ID/length. */
2058
if ((res = ebml_read_master(matroska, &id)) < 0)
2060
matroska->segment_start = url_ftell(s->pb);
2062
matroska->time_scale = 1000000;
2063
/* we've found our segment, start reading the different contents in here */
2065
if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2068
} else if (matroska->level_up) {
2069
matroska->level_up--;
2075
case MATROSKA_ID_INFO: {
2076
if ((res = ebml_read_master(matroska, &id)) < 0)
2078
res = matroska_parse_info(matroska);
2082
/* track info headers */
2083
case MATROSKA_ID_TRACKS: {
2084
if ((res = ebml_read_master(matroska, &id)) < 0)
2086
res = matroska_parse_tracks(matroska);
2091
case MATROSKA_ID_CUES: {
2092
if (!matroska->index_parsed) {
2093
if ((res = ebml_read_master(matroska, &id)) < 0)
2095
res = matroska_parse_index(matroska);
2097
res = ebml_read_skip(matroska);
2102
case MATROSKA_ID_TAGS: {
2103
if (!matroska->metadata_parsed) {
2104
if ((res = ebml_read_master(matroska, &id)) < 0)
2106
res = matroska_parse_metadata(matroska);
2108
res = ebml_read_skip(matroska);
2112
/* file index (if seekable, seek to Cues/Tags to parse it) */
2113
case MATROSKA_ID_SEEKHEAD: {
2114
if ((res = ebml_read_master(matroska, &id)) < 0)
2116
res = matroska_parse_seekhead(matroska);
2120
case MATROSKA_ID_ATTACHMENTS: {
2121
if ((res = ebml_read_master(matroska, &id)) < 0)
2123
res = matroska_parse_attachments(s);
2127
case MATROSKA_ID_CLUSTER: {
2128
/* Do not read the master - this will be done in the next
2129
* call to matroska_read_packet. */
2135
av_log(matroska->ctx, AV_LOG_INFO,
2136
"Unknown matroska file header ID 0x%x\n", id);
2140
res = ebml_read_skip(matroska);
2144
if (matroska->level_up) {
2145
matroska->level_up--;
2150
/* Have we found a cluster? */
2151
if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
2153
MatroskaTrack *track;
2156
for (i = 0; i < matroska->num_tracks; i++) {
2157
enum CodecID codec_id = CODEC_ID_NONE;
2158
uint8_t *extradata = NULL;
2159
int extradata_size = 0;
2160
int extradata_offset = 0;
2161
track = matroska->tracks[i];
2162
track->stream_index = -1;
2164
/* Apply some sanity checks. */
2165
if (track->codec_id == NULL)
2168
for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
2169
if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
2170
strlen(ff_mkv_codec_tags[j].str))){
2171
codec_id= ff_mkv_codec_tags[j].id;
2176
/* Set the FourCC from the CodecID. */
2177
/* This is the MS compatibility mode which stores a
2178
* BITMAPINFOHEADER in the CodecPrivate. */
2179
if (!strcmp(track->codec_id,
2180
MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2181
(track->codec_priv_size >= 40) &&
2182
(track->codec_priv != NULL)) {
2183
MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2185
/* Offset of biCompression. Stored in LE. */
2186
vtrack->fourcc = AV_RL32(track->codec_priv + 16);
2187
codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
2191
/* This is the MS compatibility mode which stores a
2192
* WAVEFORMATEX in the CodecPrivate. */
2193
else if (!strcmp(track->codec_id,
2194
MATROSKA_CODEC_ID_AUDIO_ACM) &&
2195
(track->codec_priv_size >= 18) &&
2196
(track->codec_priv != NULL)) {
2199
/* Offset of wFormatTag. Stored in LE. */
2200
tag = AV_RL16(track->codec_priv);
2201
codec_id = codec_get_id(codec_wav_tags, tag);
2205
else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
2206
MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2207
int profile = matroska_aac_profile(track->codec_id);
2208
int sri = matroska_aac_sri(audiotrack->internal_samplerate);
2209
extradata = av_malloc(5);
2210
if (extradata == NULL)
2211
return AVERROR(ENOMEM);
2212
extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
2213
extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
2214
if (strstr(track->codec_id, "SBR")) {
2215
sri = matroska_aac_sri(audiotrack->samplerate);
2216
extradata[2] = 0x56;
2217
extradata[3] = 0xE5;
2218
extradata[4] = 0x80 | (sri<<3);
2225
else if (codec_id == CODEC_ID_TTA) {
2226
MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2228
extradata_size = 30;
2229
extradata = av_mallocz(extradata_size);
2230
if (extradata == NULL)
2231
return AVERROR(ENOMEM);
2232
init_put_byte(&b, extradata, extradata_size, 1,
2233
NULL, NULL, NULL, NULL);
2234
put_buffer(&b, "TTA1", 4);
2236
put_le16(&b, audiotrack->channels);
2237
put_le16(&b, audiotrack->bitdepth);
2238
put_le32(&b, audiotrack->samplerate);
2239
put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
2242
else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
2243
codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
2244
extradata_offset = 26;
2245
track->codec_priv_size -= extradata_offset;
2248
else if (codec_id == CODEC_ID_RA_144) {
2249
MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2250
audiotrack->samplerate = 8000;
2251
audiotrack->channels = 1;
2254
else if (codec_id == CODEC_ID_RA_288 ||
2255
codec_id == CODEC_ID_COOK ||
2256
codec_id == CODEC_ID_ATRAC3) {
2257
MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2260
init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
2261
NULL, NULL, NULL, NULL);
2263
audiotrack->coded_framesize = get_be32(&b);
2265
audiotrack->sub_packet_h = get_be16(&b);
2266
audiotrack->frame_size = get_be16(&b);
2267
audiotrack->sub_packet_size = get_be16(&b);
2268
audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
2269
if (codec_id == CODEC_ID_RA_288) {
2270
audiotrack->block_align = audiotrack->coded_framesize;
2271
track->codec_priv_size = 0;
2273
audiotrack->block_align = audiotrack->sub_packet_size;
2274
extradata_offset = 78;
2275
track->codec_priv_size -= extradata_offset;
2279
if (codec_id == CODEC_ID_NONE) {
2280
av_log(matroska->ctx, AV_LOG_INFO,
2281
"Unknown/unsupported CodecID %s.\n",
2285
track->stream_index = matroska->num_streams;
2287
matroska->num_streams++;
2288
st = av_new_stream(s, track->stream_index);
1097
if (ebml_parse(matroska, matroska_segments, matroska) < 0)
1099
matroska_execute_seekhead(matroska);
1101
if (matroska->duration)
1102
matroska->ctx->duration = matroska->duration * matroska->time_scale
1103
* 1000 / AV_TIME_BASE;
1104
if (matroska->title)
1105
strncpy(matroska->ctx->title, matroska->title,
1106
sizeof(matroska->ctx->title)-1);
1107
matroska_convert_tags(s, &matroska->tags);
1109
tracks = matroska->tracks.elem;
1110
for (i=0; i < matroska->tracks.nb_elem; i++) {
1111
MatroskaTrack *track = &tracks[i];
1112
enum CodecID codec_id = CODEC_ID_NONE;
1113
EbmlList *encodings_list = &tracks->encodings;
1114
MatroskaTrackEncoding *encodings = encodings_list->elem;
1115
uint8_t *extradata = NULL;
1116
int extradata_size = 0;
1117
int extradata_offset = 0;
1119
/* Apply some sanity checks. */
1120
if (track->type != MATROSKA_TRACK_TYPE_VIDEO &&
1121
track->type != MATROSKA_TRACK_TYPE_AUDIO &&
1122
track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
1123
av_log(matroska->ctx, AV_LOG_INFO,
1124
"Unknown or unsupported track type %"PRIu64"\n",
1128
if (track->codec_id == NULL)
1131
if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
1132
if (!track->default_duration)
1133
track->default_duration = 1000000000/track->video.frame_rate;
1134
if (!track->video.display_width)
1135
track->video.display_width = track->video.pixel_width;
1136
if (!track->video.display_height)
1137
track->video.display_height = track->video.pixel_height;
1138
} else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
1139
if (!track->audio.out_samplerate)
1140
track->audio.out_samplerate = track->audio.samplerate;
1142
if (encodings_list->nb_elem > 1) {
1143
av_log(matroska->ctx, AV_LOG_ERROR,
1144
"Multiple combined encodings no supported");
1145
} else if (encodings_list->nb_elem == 1) {
1146
if (encodings[0].type ||
1147
(encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
1149
encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
1152
encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
1154
encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO)) {
1155
encodings[0].scope = 0;
1156
av_log(matroska->ctx, AV_LOG_ERROR,
1157
"Unsupported encoding type");
1158
} else if (track->codec_priv.size && encodings[0].scope&2) {
1159
uint8_t *codec_priv = track->codec_priv.data;
1160
int offset = matroska_decode_buffer(&track->codec_priv.data,
1161
&track->codec_priv.size,
1164
track->codec_priv.data = NULL;
1165
track->codec_priv.size = 0;
1166
av_log(matroska->ctx, AV_LOG_ERROR,
1167
"Failed to decode codec private data\n");
1168
} else if (offset > 0) {
1169
track->codec_priv.data = av_malloc(track->codec_priv.size + offset);
1170
memcpy(track->codec_priv.data,
1171
encodings[0].compression.settings.data, offset);
1172
memcpy(track->codec_priv.data+offset, codec_priv,
1173
track->codec_priv.size);
1174
track->codec_priv.size += offset;
1176
if (codec_priv != track->codec_priv.data)
1177
av_free(codec_priv);
1181
for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
1182
if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
1183
strlen(ff_mkv_codec_tags[j].str))){
1184
codec_id= ff_mkv_codec_tags[j].id;
1189
st = track->stream = av_new_stream(s, 0);
1191
return AVERROR(ENOMEM);
1193
if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC")
1194
&& track->codec_priv.size >= 40
1195
&& track->codec_priv.data != NULL) {
1196
track->video.fourcc = AV_RL32(track->codec_priv.data + 16);
1197
codec_id = codec_get_id(codec_bmp_tags, track->video.fourcc);
1198
} else if (!strcmp(track->codec_id, "A_MS/ACM")
1199
&& track->codec_priv.size >= 18
1200
&& track->codec_priv.data != NULL) {
1201
uint16_t tag = AV_RL16(track->codec_priv.data);
1202
codec_id = codec_get_id(codec_wav_tags, tag);
1203
} else if (!strcmp(track->codec_id, "V_QUICKTIME")
1204
&& (track->codec_priv.size >= 86)
1205
&& (track->codec_priv.data != NULL)) {
1206
track->video.fourcc = AV_RL32(track->codec_priv.data);
1207
codec_id=codec_get_id(codec_movvideo_tags, track->video.fourcc);
1208
} else if (codec_id == CODEC_ID_PCM_S16BE) {
1209
switch (track->audio.bitdepth) {
1210
case 8: codec_id = CODEC_ID_PCM_U8; break;
1211
case 24: codec_id = CODEC_ID_PCM_S24BE; break;
1212
case 32: codec_id = CODEC_ID_PCM_S32BE; break;
1214
} else if (codec_id == CODEC_ID_PCM_S16LE) {
1215
switch (track->audio.bitdepth) {
1216
case 8: codec_id = CODEC_ID_PCM_U8; break;
1217
case 24: codec_id = CODEC_ID_PCM_S24LE; break;
1218
case 32: codec_id = CODEC_ID_PCM_S32LE; break;
1220
} else if (codec_id==CODEC_ID_PCM_F32LE && track->audio.bitdepth==64) {
1221
codec_id = CODEC_ID_PCM_F64LE;
1222
} else if (codec_id == CODEC_ID_AAC && !track->codec_priv.size) {
1223
int profile = matroska_aac_profile(track->codec_id);
1224
int sri = matroska_aac_sri(track->audio.samplerate);
1225
extradata = av_malloc(5);
1226
if (extradata == NULL)
1227
return AVERROR(ENOMEM);
1228
extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
1229
extradata[1] = ((sri&0x01) << 7) | (track->audio.channels<<3);
1230
if (strstr(track->codec_id, "SBR")) {
1231
sri = matroska_aac_sri(track->audio.out_samplerate);
1232
extradata[2] = 0x56;
1233
extradata[3] = 0xE5;
1234
extradata[4] = 0x80 | (sri<<3);
1238
} else if (codec_id == CODEC_ID_TTA) {
1240
extradata_size = 30;
1241
extradata = av_mallocz(extradata_size);
1242
if (extradata == NULL)
1243
return AVERROR(ENOMEM);
1244
init_put_byte(&b, extradata, extradata_size, 1,
1245
NULL, NULL, NULL, NULL);
1246
put_buffer(&b, "TTA1", 4);
1248
put_le16(&b, track->audio.channels);
1249
put_le16(&b, track->audio.bitdepth);
1250
put_le32(&b, track->audio.out_samplerate);
1251
put_le32(&b, matroska->ctx->duration * track->audio.out_samplerate);
1252
} else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
1253
codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
1254
extradata_offset = 26;
1255
track->codec_priv.size -= extradata_offset;
1256
} else if (codec_id == CODEC_ID_RA_144) {
1257
track->audio.out_samplerate = 8000;
1258
track->audio.channels = 1;
1259
} else if (codec_id == CODEC_ID_RA_288 || codec_id == CODEC_ID_COOK ||
1260
codec_id == CODEC_ID_ATRAC3) {
1263
init_put_byte(&b, track->codec_priv.data,track->codec_priv.size,
1264
0, NULL, NULL, NULL, NULL);
1266
track->audio.coded_framesize = get_be32(&b);
1268
track->audio.sub_packet_h = get_be16(&b);
1269
track->audio.frame_size = get_be16(&b);
1270
track->audio.sub_packet_size = get_be16(&b);
1271
track->audio.buf = av_malloc(track->audio.frame_size * track->audio.sub_packet_h);
1272
if (codec_id == CODEC_ID_RA_288) {
1273
st->codec->block_align = track->audio.coded_framesize;
1274
track->codec_priv.size = 0;
1276
st->codec->block_align = track->audio.sub_packet_size;
1277
extradata_offset = 78;
1278
track->codec_priv.size -= extradata_offset;
1282
if (codec_id == CODEC_ID_NONE)
1283
av_log(matroska->ctx, AV_LOG_INFO,
1284
"Unknown/unsupported CodecID %s.\n", track->codec_id);
1286
if (track->time_scale < 0.01)
1287
track->time_scale = 1.0;
1288
av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
1290
st->codec->codec_id = codec_id;
1292
if (strcmp(track->language, "und"))
1293
av_strlcpy(st->language, track->language, 4);
1295
if (track->flag_default)
1296
st->disposition |= AV_DISPOSITION_DEFAULT;
1298
if (track->default_duration)
1299
av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
1300
track->default_duration, 1000000000, 30000);
1303
st->codec->extradata = extradata;
1304
st->codec->extradata_size = extradata_size;
1305
} else if(track->codec_priv.data && track->codec_priv.size > 0){
1306
st->codec->extradata = av_malloc(track->codec_priv.size);
1307
if(st->codec->extradata == NULL)
1308
return AVERROR(ENOMEM);
1309
st->codec->extradata_size = track->codec_priv.size;
1310
memcpy(st->codec->extradata,
1311
track->codec_priv.data + extradata_offset,
1312
track->codec_priv.size);
1315
if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
1316
st->codec->codec_type = CODEC_TYPE_VIDEO;
1317
st->codec->codec_tag = track->video.fourcc;
1318
st->codec->width = track->video.pixel_width;
1319
st->codec->height = track->video.pixel_height;
1320
av_reduce(&st->sample_aspect_ratio.num,
1321
&st->sample_aspect_ratio.den,
1322
st->codec->height * track->video.display_width,
1323
st->codec-> width * track->video.display_height,
1325
st->need_parsing = AVSTREAM_PARSE_HEADERS;
1326
} else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
1327
st->codec->codec_type = CODEC_TYPE_AUDIO;
1328
st->codec->sample_rate = track->audio.out_samplerate;
1329
st->codec->channels = track->audio.channels;
1330
} else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
1331
st->codec->codec_type = CODEC_TYPE_SUBTITLE;
1335
attachements = attachements_list->elem;
1336
for (j=0; j<attachements_list->nb_elem; j++) {
1337
if (!(attachements[j].filename && attachements[j].mime &&
1338
attachements[j].bin.data && attachements[j].bin.size > 0)) {
1339
av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
1341
AVStream *st = av_new_stream(s, 0);
2289
1342
if (st == NULL)
2290
return AVERROR(ENOMEM);
2291
av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2293
st->codec->codec_id = codec_id;
2295
if (strcmp(track->language, "und"))
2296
strcpy(st->language, track->language);
2298
if (track->flags & MATROSKA_TRACK_DEFAULT)
2299
st->disposition |= AV_DISPOSITION_DEFAULT;
2301
if (track->default_duration)
2302
av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
2303
track->default_duration, 1000000000, 30000);
2306
st->codec->extradata = extradata;
2307
st->codec->extradata_size = extradata_size;
2308
} else if(track->codec_priv && track->codec_priv_size > 0){
2309
st->codec->extradata = av_malloc(track->codec_priv_size);
2310
if(st->codec->extradata == NULL)
2311
return AVERROR(ENOMEM);
2312
st->codec->extradata_size = track->codec_priv_size;
2313
memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
2314
track->codec_priv_size);
2317
if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2318
MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2320
st->codec->codec_type = CODEC_TYPE_VIDEO;
2321
st->codec->codec_tag = videotrack->fourcc;
2322
st->codec->width = videotrack->pixel_width;
2323
st->codec->height = videotrack->pixel_height;
2324
if (videotrack->display_width == 0)
2325
videotrack->display_width= videotrack->pixel_width;
2326
if (videotrack->display_height == 0)
2327
videotrack->display_height= videotrack->pixel_height;
2328
av_reduce(&st->codec->sample_aspect_ratio.num,
2329
&st->codec->sample_aspect_ratio.den,
2330
st->codec->height * videotrack->display_width,
2331
st->codec-> width * videotrack->display_height,
2333
st->need_parsing = AVSTREAM_PARSE_HEADERS;
2334
} else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2335
MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2337
st->codec->codec_type = CODEC_TYPE_AUDIO;
2338
st->codec->sample_rate = audiotrack->samplerate;
2339
st->codec->channels = audiotrack->channels;
2340
st->codec->block_align = audiotrack->block_align;
2341
} else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2342
st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2345
/* What do we do with private data? E.g. for Vorbis. */
2350
if (matroska->index_parsed) {
2351
int i, track, stream;
2352
for (i=0; i<matroska->num_indexes; i++) {
2353
MatroskaDemuxIndex *idx = &matroska->index[i];
2354
track = matroska_find_track_by_num(matroska, idx->track);
2355
if (track < 0) continue;
2356
stream = matroska->tracks[track]->stream_index;
2357
if (stream >= 0 && stream < matroska->ctx->nb_streams)
2358
av_add_index_entry(matroska->ctx->streams[stream],
2359
idx->pos, idx->time/matroska->time_scale,
2360
0, 0, AVINDEX_KEYFRAME);
2368
matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
2369
int64_t pos, uint64_t cluster_time, uint64_t duration,
2370
int is_keyframe, int is_bframe)
1344
st->filename = av_strdup(attachements[j].filename);
1345
st->codec->codec_id = CODEC_ID_NONE;
1346
st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
1347
st->codec->extradata = av_malloc(attachements[j].bin.size);
1348
if(st->codec->extradata == NULL)
1350
st->codec->extradata_size = attachements[j].bin.size;
1351
memcpy(st->codec->extradata, attachements[j].bin.data, attachements[j].bin.size);
1353
for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
1354
if (!strncmp(ff_mkv_mime_tags[i].str, attachements[j].mime,
1355
strlen(ff_mkv_mime_tags[i].str))) {
1356
st->codec->codec_id = ff_mkv_mime_tags[i].id;
1363
chapters = chapters_list->elem;
1364
for (i=0; i<chapters_list->nb_elem; i++)
1365
if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid)
1366
ff_new_chapter(s, chapters[i].uid, (AVRational){1, 1000000000},
1367
chapters[i].start, chapters[i].end,
1370
index_list = &matroska->index;
1371
index = index_list->elem;
1372
for (i=0; i<index_list->nb_elem; i++) {
1373
EbmlList *pos_list = &index[i].pos;
1374
MatroskaIndexPos *pos = pos_list->elem;
1375
for (j=0; j<pos_list->nb_elem; j++) {
1376
MatroskaTrack *track = matroska_find_track_by_num(matroska,
1378
if (track && track->stream)
1379
av_add_index_entry(track->stream,
1380
pos[j].pos + matroska->segment_start,
1381
index[i].time, 0, 0, AVINDEX_KEYFRAME);
1389
* Put one packet in an application-supplied AVPacket struct.
1390
* Returns 0 on success or -1 on failure.
1392
static int matroska_deliver_packet(MatroskaDemuxContext *matroska,
1395
if (matroska->num_packets > 0) {
1396
memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
1397
av_free(matroska->packets[0]);
1398
if (matroska->num_packets > 1) {
1399
memmove(&matroska->packets[0], &matroska->packets[1],
1400
(matroska->num_packets - 1) * sizeof(AVPacket *));
1402
av_realloc(matroska->packets, (matroska->num_packets - 1) *
1403
sizeof(AVPacket *));
1405
av_freep(&matroska->packets);
1407
matroska->num_packets--;
1415
* Free all packets in our internal queue.
1417
static void matroska_clear_queue(MatroskaDemuxContext *matroska)
1419
if (matroska->packets) {
1421
for (n = 0; n < matroska->num_packets; n++) {
1422
av_free_packet(matroska->packets[n]);
1423
av_free(matroska->packets[n]);
1425
av_freep(&matroska->packets);
1426
matroska->num_packets = 0;
1430
static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data,
1431
int size, int64_t pos, uint64_t cluster_time,
1432
uint64_t duration, int is_keyframe,
1433
int64_t cluster_pos)
1435
uint64_t timecode = AV_NOPTS_VALUE;
1436
MatroskaTrack *track;
2376
uint8_t *origdata = data;
2377
1440
int16_t block_time;
2378
1441
uint32_t *lace_size = NULL;
2379
1442
int n, flags, laces = 0;
2383
/* first byte(s): tracknum */
2384
if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
1445
if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) {
2385
1446
av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
2392
/* fetch track from num */
2393
1452
track = matroska_find_track_by_num(matroska, num);
2394
if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
1453
if (size <= 3 || !track || !track->stream) {
2395
1454
av_log(matroska->ctx, AV_LOG_INFO,
2396
"Invalid stream %d or size %u\n", track, size);
2400
stream_index = matroska->tracks[track]->stream_index;
2401
if (stream_index < 0 || stream_index >= matroska->ctx->nb_streams) {
2405
st = matroska->ctx->streams[stream_index];
2406
if (st->discard >= AVDISCARD_ALL) {
1455
"Invalid stream %"PRIu64" or size %u\n", num, size);
1459
if (st->discard >= AVDISCARD_ALL)
2410
1461
if (duration == AV_NOPTS_VALUE)
2411
duration = matroska->tracks[track]->default_duration / matroska->time_scale;
1462
duration = track->default_duration / matroska->time_scale;
2413
/* block_time (relative to cluster time) */
2414
1464
block_time = AV_RB16(data);
2416
1466
flags = *data++;