00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00031 #include <stdio.h>
00032 #include "avformat.h"
00033 #include "internal.h"
00034 #include "avio_internal.h"
00035
00036 #include "riff.h"
00037 #include "isom.h"
00038 #include "rm.h"
00039 #include "matroska.h"
00040 #include "libavcodec/mpeg4audio.h"
00041 #include "libavutil/intfloat_readwrite.h"
00042 #include "libavutil/intreadwrite.h"
00043 #include "libavutil/avstring.h"
00044 #include "libavutil/lzo.h"
00045 #if CONFIG_ZLIB
00046 #include <zlib.h>
00047 #endif
00048 #if CONFIG_BZLIB
00049 #include <bzlib.h>
00050 #endif
00051
00052 typedef enum {
00053 EBML_NONE,
00054 EBML_UINT,
00055 EBML_FLOAT,
00056 EBML_STR,
00057 EBML_UTF8,
00058 EBML_BIN,
00059 EBML_NEST,
00060 EBML_PASS,
00061 EBML_STOP,
00062 EBML_TYPE_COUNT
00063 } EbmlType;
00064
00065 typedef const struct EbmlSyntax {
00066 uint32_t id;
00067 EbmlType type;
00068 int list_elem_size;
00069 int data_offset;
00070 union {
00071 uint64_t u;
00072 double f;
00073 const char *s;
00074 const struct EbmlSyntax *n;
00075 } def;
00076 } EbmlSyntax;
00077
00078 typedef struct {
00079 int nb_elem;
00080 void *elem;
00081 } EbmlList;
00082
00083 typedef struct {
00084 int size;
00085 uint8_t *data;
00086 int64_t pos;
00087 } EbmlBin;
00088
00089 typedef struct {
00090 uint64_t version;
00091 uint64_t max_size;
00092 uint64_t id_length;
00093 char *doctype;
00094 uint64_t doctype_version;
00095 } Ebml;
00096
00097 typedef struct {
00098 uint64_t algo;
00099 EbmlBin settings;
00100 } MatroskaTrackCompression;
00101
00102 typedef struct {
00103 uint64_t scope;
00104 uint64_t type;
00105 MatroskaTrackCompression compression;
00106 } MatroskaTrackEncoding;
00107
00108 typedef struct {
00109 double frame_rate;
00110 uint64_t display_width;
00111 uint64_t display_height;
00112 uint64_t pixel_width;
00113 uint64_t pixel_height;
00114 uint64_t fourcc;
00115 } MatroskaTrackVideo;
00116
00117 typedef struct {
00118 double samplerate;
00119 double out_samplerate;
00120 uint64_t bitdepth;
00121 uint64_t channels;
00122
00123
00124 int coded_framesize;
00125 int sub_packet_h;
00126 int frame_size;
00127 int sub_packet_size;
00128 int sub_packet_cnt;
00129 int pkt_cnt;
00130 uint8_t *buf;
00131 } MatroskaTrackAudio;
00132
00133 typedef struct {
00134 uint64_t num;
00135 uint64_t uid;
00136 uint64_t type;
00137 char *name;
00138 char *codec_id;
00139 EbmlBin codec_priv;
00140 char *language;
00141 double time_scale;
00142 uint64_t default_duration;
00143 uint64_t flag_default;
00144 uint64_t flag_forced;
00145 MatroskaTrackVideo video;
00146 MatroskaTrackAudio audio;
00147 EbmlList encodings;
00148
00149 AVStream *stream;
00150 int64_t end_timecode;
00151 int ms_compat;
00152 } MatroskaTrack;
00153
00154 typedef struct {
00155 uint64_t uid;
00156 char *filename;
00157 char *mime;
00158 EbmlBin bin;
00159
00160 AVStream *stream;
00161 } MatroskaAttachement;
00162
00163 typedef struct {
00164 uint64_t start;
00165 uint64_t end;
00166 uint64_t uid;
00167 char *title;
00168
00169 AVChapter *chapter;
00170 } MatroskaChapter;
00171
00172 typedef struct {
00173 uint64_t track;
00174 uint64_t pos;
00175 } MatroskaIndexPos;
00176
00177 typedef struct {
00178 uint64_t time;
00179 EbmlList pos;
00180 } MatroskaIndex;
00181
00182 typedef struct {
00183 char *name;
00184 char *string;
00185 char *lang;
00186 uint64_t def;
00187 EbmlList sub;
00188 } MatroskaTag;
00189
00190 typedef struct {
00191 char *type;
00192 uint64_t typevalue;
00193 uint64_t trackuid;
00194 uint64_t chapteruid;
00195 uint64_t attachuid;
00196 } MatroskaTagTarget;
00197
00198 typedef struct {
00199 MatroskaTagTarget target;
00200 EbmlList tag;
00201 } MatroskaTags;
00202
00203 typedef struct {
00204 uint64_t id;
00205 uint64_t pos;
00206 } MatroskaSeekhead;
00207
00208 typedef struct {
00209 uint64_t start;
00210 uint64_t length;
00211 } MatroskaLevel;
00212
00213 typedef struct {
00214 AVFormatContext *ctx;
00215
00216
00217 int num_levels;
00218 MatroskaLevel levels[EBML_MAX_DEPTH];
00219 int level_up;
00220 uint32_t current_id;
00221
00222 uint64_t time_scale;
00223 double duration;
00224 char *title;
00225 EbmlList tracks;
00226 EbmlList attachments;
00227 EbmlList chapters;
00228 EbmlList index;
00229 EbmlList tags;
00230 EbmlList seekhead;
00231
00232
00233 int64_t segment_start;
00234
00235
00236 AVPacket **packets;
00237 int num_packets;
00238 AVPacket *prev_pkt;
00239
00240 int done;
00241
00242
00243 int skip_to_keyframe;
00244 uint64_t skip_to_timecode;
00245 } MatroskaDemuxContext;
00246
00247 typedef struct {
00248 uint64_t duration;
00249 int64_t reference;
00250 uint64_t non_simple;
00251 EbmlBin bin;
00252 } MatroskaBlock;
00253
00254 typedef struct {
00255 uint64_t timecode;
00256 EbmlList blocks;
00257 } MatroskaCluster;
00258
00259 static EbmlSyntax ebml_header[] = {
00260 { EBML_ID_EBMLREADVERSION, EBML_UINT, 0, offsetof(Ebml,version), {.u=EBML_VERSION} },
00261 { EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, offsetof(Ebml,max_size), {.u=8} },
00262 { EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, offsetof(Ebml,id_length), {.u=4} },
00263 { EBML_ID_DOCTYPE, EBML_STR, 0, offsetof(Ebml,doctype), {.s="(none)"} },
00264 { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, offsetof(Ebml,doctype_version), {.u=1} },
00265 { EBML_ID_EBMLVERSION, EBML_NONE },
00266 { EBML_ID_DOCTYPEVERSION, EBML_NONE },
00267 { 0 }
00268 };
00269
00270 static EbmlSyntax ebml_syntax[] = {
00271 { EBML_ID_HEADER, EBML_NEST, 0, 0, {.n=ebml_header} },
00272 { 0 }
00273 };
00274
00275 static EbmlSyntax matroska_info[] = {
00276 { MATROSKA_ID_TIMECODESCALE, EBML_UINT, 0, offsetof(MatroskaDemuxContext,time_scale), {.u=1000000} },
00277 { MATROSKA_ID_DURATION, EBML_FLOAT, 0, offsetof(MatroskaDemuxContext,duration) },
00278 { MATROSKA_ID_TITLE, EBML_UTF8, 0, offsetof(MatroskaDemuxContext,title) },
00279 { MATROSKA_ID_WRITINGAPP, EBML_NONE },
00280 { MATROSKA_ID_MUXINGAPP, EBML_NONE },
00281 { MATROSKA_ID_DATEUTC, EBML_NONE },
00282 { MATROSKA_ID_SEGMENTUID, EBML_NONE },
00283 { 0 }
00284 };
00285
00286 static EbmlSyntax matroska_track_video[] = {
00287 { MATROSKA_ID_VIDEOFRAMERATE, EBML_FLOAT,0, offsetof(MatroskaTrackVideo,frame_rate) },
00288 { MATROSKA_ID_VIDEODISPLAYWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_width) },
00289 { MATROSKA_ID_VIDEODISPLAYHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_height) },
00290 { MATROSKA_ID_VIDEOPIXELWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_width) },
00291 { MATROSKA_ID_VIDEOPIXELHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_height) },
00292 { MATROSKA_ID_VIDEOCOLORSPACE, EBML_UINT, 0, offsetof(MatroskaTrackVideo,fourcc) },
00293 { MATROSKA_ID_VIDEOPIXELCROPB, EBML_NONE },
00294 { MATROSKA_ID_VIDEOPIXELCROPT, EBML_NONE },
00295 { MATROSKA_ID_VIDEOPIXELCROPL, EBML_NONE },
00296 { MATROSKA_ID_VIDEOPIXELCROPR, EBML_NONE },
00297 { MATROSKA_ID_VIDEODISPLAYUNIT, EBML_NONE },
00298 { MATROSKA_ID_VIDEOFLAGINTERLACED,EBML_NONE },
00299 { MATROSKA_ID_VIDEOSTEREOMODE, EBML_NONE },
00300 { MATROSKA_ID_VIDEOASPECTRATIO, EBML_NONE },
00301 { 0 }
00302 };
00303
00304 static EbmlSyntax matroska_track_audio[] = {
00305 { MATROSKA_ID_AUDIOSAMPLINGFREQ, EBML_FLOAT,0, offsetof(MatroskaTrackAudio,samplerate), {.f=8000.0} },
00306 { MATROSKA_ID_AUDIOOUTSAMPLINGFREQ,EBML_FLOAT,0,offsetof(MatroskaTrackAudio,out_samplerate) },
00307 { MATROSKA_ID_AUDIOBITDEPTH, EBML_UINT, 0, offsetof(MatroskaTrackAudio,bitdepth) },
00308 { MATROSKA_ID_AUDIOCHANNELS, EBML_UINT, 0, offsetof(MatroskaTrackAudio,channels), {.u=1} },
00309 { 0 }
00310 };
00311
00312 static EbmlSyntax matroska_track_encoding_compression[] = {
00313 { MATROSKA_ID_ENCODINGCOMPALGO, EBML_UINT, 0, offsetof(MatroskaTrackCompression,algo), {.u=0} },
00314 { MATROSKA_ID_ENCODINGCOMPSETTINGS,EBML_BIN, 0, offsetof(MatroskaTrackCompression,settings) },
00315 { 0 }
00316 };
00317
00318 static EbmlSyntax matroska_track_encoding[] = {
00319 { MATROSKA_ID_ENCODINGSCOPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,scope), {.u=1} },
00320 { MATROSKA_ID_ENCODINGTYPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,type), {.u=0} },
00321 { MATROSKA_ID_ENCODINGCOMPRESSION,EBML_NEST, 0, offsetof(MatroskaTrackEncoding,compression), {.n=matroska_track_encoding_compression} },
00322 { MATROSKA_ID_ENCODINGORDER, EBML_NONE },
00323 { 0 }
00324 };
00325
00326 static EbmlSyntax matroska_track_encodings[] = {
00327 { MATROSKA_ID_TRACKCONTENTENCODING, EBML_NEST, sizeof(MatroskaTrackEncoding), offsetof(MatroskaTrack,encodings), {.n=matroska_track_encoding} },
00328 { 0 }
00329 };
00330
00331 static EbmlSyntax matroska_track[] = {
00332 { MATROSKA_ID_TRACKNUMBER, EBML_UINT, 0, offsetof(MatroskaTrack,num) },
00333 { MATROSKA_ID_TRACKNAME, EBML_UTF8, 0, offsetof(MatroskaTrack,name) },
00334 { MATROSKA_ID_TRACKUID, EBML_UINT, 0, offsetof(MatroskaTrack,uid) },
00335 { MATROSKA_ID_TRACKTYPE, EBML_UINT, 0, offsetof(MatroskaTrack,type) },
00336 { MATROSKA_ID_CODECID, EBML_STR, 0, offsetof(MatroskaTrack,codec_id) },
00337 { MATROSKA_ID_CODECPRIVATE, EBML_BIN, 0, offsetof(MatroskaTrack,codec_priv) },
00338 { MATROSKA_ID_TRACKLANGUAGE, EBML_UTF8, 0, offsetof(MatroskaTrack,language), {.s="eng"} },
00339 { MATROSKA_ID_TRACKDEFAULTDURATION, EBML_UINT, 0, offsetof(MatroskaTrack,default_duration) },
00340 { MATROSKA_ID_TRACKTIMECODESCALE, EBML_FLOAT,0, offsetof(MatroskaTrack,time_scale), {.f=1.0} },
00341 { MATROSKA_ID_TRACKFLAGDEFAULT, EBML_UINT, 0, offsetof(MatroskaTrack,flag_default), {.u=1} },
00342 { MATROSKA_ID_TRACKFLAGFORCED, EBML_UINT, 0, offsetof(MatroskaTrack,flag_forced), {.u=0} },
00343 { MATROSKA_ID_TRACKVIDEO, EBML_NEST, 0, offsetof(MatroskaTrack,video), {.n=matroska_track_video} },
00344 { MATROSKA_ID_TRACKAUDIO, EBML_NEST, 0, offsetof(MatroskaTrack,audio), {.n=matroska_track_audio} },
00345 { MATROSKA_ID_TRACKCONTENTENCODINGS,EBML_NEST, 0, 0, {.n=matroska_track_encodings} },
00346 { MATROSKA_ID_TRACKFLAGENABLED, EBML_NONE },
00347 { MATROSKA_ID_TRACKFLAGLACING, EBML_NONE },
00348 { MATROSKA_ID_CODECNAME, EBML_NONE },
00349 { MATROSKA_ID_CODECDECODEALL, EBML_NONE },
00350 { MATROSKA_ID_CODECINFOURL, EBML_NONE },
00351 { MATROSKA_ID_CODECDOWNLOADURL, EBML_NONE },
00352 { MATROSKA_ID_TRACKMINCACHE, EBML_NONE },
00353 { MATROSKA_ID_TRACKMAXCACHE, EBML_NONE },
00354 { MATROSKA_ID_TRACKMAXBLKADDID, EBML_NONE },
00355 { 0 }
00356 };
00357
00358 static EbmlSyntax matroska_tracks[] = {
00359 { MATROSKA_ID_TRACKENTRY, EBML_NEST, sizeof(MatroskaTrack), offsetof(MatroskaDemuxContext,tracks), {.n=matroska_track} },
00360 { 0 }
00361 };
00362
00363 static EbmlSyntax matroska_attachment[] = {
00364 { MATROSKA_ID_FILEUID, EBML_UINT, 0, offsetof(MatroskaAttachement,uid) },
00365 { MATROSKA_ID_FILENAME, EBML_UTF8, 0, offsetof(MatroskaAttachement,filename) },
00366 { MATROSKA_ID_FILEMIMETYPE, EBML_STR, 0, offsetof(MatroskaAttachement,mime) },
00367 { MATROSKA_ID_FILEDATA, EBML_BIN, 0, offsetof(MatroskaAttachement,bin) },
00368 { MATROSKA_ID_FILEDESC, EBML_NONE },
00369 { 0 }
00370 };
00371
00372 static EbmlSyntax matroska_attachments[] = {
00373 { MATROSKA_ID_ATTACHEDFILE, EBML_NEST, sizeof(MatroskaAttachement), offsetof(MatroskaDemuxContext,attachments), {.n=matroska_attachment} },
00374 { 0 }
00375 };
00376
00377 static EbmlSyntax matroska_chapter_display[] = {
00378 { MATROSKA_ID_CHAPSTRING, EBML_UTF8, 0, offsetof(MatroskaChapter,title) },
00379 { MATROSKA_ID_CHAPLANG, EBML_NONE },
00380 { 0 }
00381 };
00382
00383 static EbmlSyntax matroska_chapter_entry[] = {
00384 { MATROSKA_ID_CHAPTERTIMESTART, EBML_UINT, 0, offsetof(MatroskaChapter,start), {.u=AV_NOPTS_VALUE} },
00385 { MATROSKA_ID_CHAPTERTIMEEND, EBML_UINT, 0, offsetof(MatroskaChapter,end), {.u=AV_NOPTS_VALUE} },
00386 { MATROSKA_ID_CHAPTERUID, EBML_UINT, 0, offsetof(MatroskaChapter,uid) },
00387 { MATROSKA_ID_CHAPTERDISPLAY, EBML_NEST, 0, 0, {.n=matroska_chapter_display} },
00388 { MATROSKA_ID_CHAPTERFLAGHIDDEN, EBML_NONE },
00389 { MATROSKA_ID_CHAPTERFLAGENABLED, EBML_NONE },
00390 { MATROSKA_ID_CHAPTERPHYSEQUIV, EBML_NONE },
00391 { MATROSKA_ID_CHAPTERATOM, EBML_NONE },
00392 { 0 }
00393 };
00394
00395 static EbmlSyntax matroska_chapter[] = {
00396 { MATROSKA_ID_CHAPTERATOM, EBML_NEST, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext,chapters), {.n=matroska_chapter_entry} },
00397 { MATROSKA_ID_EDITIONUID, EBML_NONE },
00398 { MATROSKA_ID_EDITIONFLAGHIDDEN, EBML_NONE },
00399 { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE },
00400 { MATROSKA_ID_EDITIONFLAGORDERED, EBML_NONE },
00401 { 0 }
00402 };
00403
00404 static EbmlSyntax matroska_chapters[] = {
00405 { MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, 0, {.n=matroska_chapter} },
00406 { 0 }
00407 };
00408
00409 static EbmlSyntax matroska_index_pos[] = {
00410 { MATROSKA_ID_CUETRACK, EBML_UINT, 0, offsetof(MatroskaIndexPos,track) },
00411 { MATROSKA_ID_CUECLUSTERPOSITION, EBML_UINT, 0, offsetof(MatroskaIndexPos,pos) },
00412 { MATROSKA_ID_CUEBLOCKNUMBER, EBML_NONE },
00413 { 0 }
00414 };
00415
00416 static EbmlSyntax matroska_index_entry[] = {
00417 { MATROSKA_ID_CUETIME, EBML_UINT, 0, offsetof(MatroskaIndex,time) },
00418 { MATROSKA_ID_CUETRACKPOSITION, EBML_NEST, sizeof(MatroskaIndexPos), offsetof(MatroskaIndex,pos), {.n=matroska_index_pos} },
00419 { 0 }
00420 };
00421
00422 static EbmlSyntax matroska_index[] = {
00423 { MATROSKA_ID_POINTENTRY, EBML_NEST, sizeof(MatroskaIndex), offsetof(MatroskaDemuxContext,index), {.n=matroska_index_entry} },
00424 { 0 }
00425 };
00426
00427 static EbmlSyntax matroska_simpletag[] = {
00428 { MATROSKA_ID_TAGNAME, EBML_UTF8, 0, offsetof(MatroskaTag,name) },
00429 { MATROSKA_ID_TAGSTRING, EBML_UTF8, 0, offsetof(MatroskaTag,string) },
00430 { MATROSKA_ID_TAGLANG, EBML_STR, 0, offsetof(MatroskaTag,lang), {.s="und"} },
00431 { MATROSKA_ID_TAGDEFAULT, EBML_UINT, 0, offsetof(MatroskaTag,def) },
00432 { MATROSKA_ID_TAGDEFAULT_BUG, EBML_UINT, 0, offsetof(MatroskaTag,def) },
00433 { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTag,sub), {.n=matroska_simpletag} },
00434 { 0 }
00435 };
00436
00437 static EbmlSyntax matroska_tagtargets[] = {
00438 { MATROSKA_ID_TAGTARGETS_TYPE, EBML_STR, 0, offsetof(MatroskaTagTarget,type) },
00439 { MATROSKA_ID_TAGTARGETS_TYPEVALUE, EBML_UINT, 0, offsetof(MatroskaTagTarget,typevalue), {.u=50} },
00440 { MATROSKA_ID_TAGTARGETS_TRACKUID, EBML_UINT, 0, offsetof(MatroskaTagTarget,trackuid) },
00441 { MATROSKA_ID_TAGTARGETS_CHAPTERUID,EBML_UINT, 0, offsetof(MatroskaTagTarget,chapteruid) },
00442 { MATROSKA_ID_TAGTARGETS_ATTACHUID, EBML_UINT, 0, offsetof(MatroskaTagTarget,attachuid) },
00443 { 0 }
00444 };
00445
00446 static EbmlSyntax matroska_tag[] = {
00447 { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTags,tag), {.n=matroska_simpletag} },
00448 { MATROSKA_ID_TAGTARGETS, EBML_NEST, 0, offsetof(MatroskaTags,target), {.n=matroska_tagtargets} },
00449 { 0 }
00450 };
00451
00452 static EbmlSyntax matroska_tags[] = {
00453 { MATROSKA_ID_TAG, EBML_NEST, sizeof(MatroskaTags), offsetof(MatroskaDemuxContext,tags), {.n=matroska_tag} },
00454 { 0 }
00455 };
00456
00457 static EbmlSyntax matroska_seekhead_entry[] = {
00458 { MATROSKA_ID_SEEKID, EBML_UINT, 0, offsetof(MatroskaSeekhead,id) },
00459 { MATROSKA_ID_SEEKPOSITION, EBML_UINT, 0, offsetof(MatroskaSeekhead,pos), {.u=-1} },
00460 { 0 }
00461 };
00462
00463 static EbmlSyntax matroska_seekhead[] = {
00464 { MATROSKA_ID_SEEKENTRY, EBML_NEST, sizeof(MatroskaSeekhead), offsetof(MatroskaDemuxContext,seekhead), {.n=matroska_seekhead_entry} },
00465 { 0 }
00466 };
00467
00468 static EbmlSyntax matroska_segment[] = {
00469 { MATROSKA_ID_INFO, EBML_NEST, 0, 0, {.n=matroska_info } },
00470 { MATROSKA_ID_TRACKS, EBML_NEST, 0, 0, {.n=matroska_tracks } },
00471 { MATROSKA_ID_ATTACHMENTS, EBML_NEST, 0, 0, {.n=matroska_attachments} },
00472 { MATROSKA_ID_CHAPTERS, EBML_NEST, 0, 0, {.n=matroska_chapters } },
00473 { MATROSKA_ID_CUES, EBML_NEST, 0, 0, {.n=matroska_index } },
00474 { MATROSKA_ID_TAGS, EBML_NEST, 0, 0, {.n=matroska_tags } },
00475 { MATROSKA_ID_SEEKHEAD, EBML_NEST, 0, 0, {.n=matroska_seekhead } },
00476 { MATROSKA_ID_CLUSTER, EBML_STOP },
00477 { 0 }
00478 };
00479
00480 static EbmlSyntax matroska_segments[] = {
00481 { MATROSKA_ID_SEGMENT, EBML_NEST, 0, 0, {.n=matroska_segment } },
00482 { 0 }
00483 };
00484
00485 static EbmlSyntax matroska_blockgroup[] = {
00486 { MATROSKA_ID_BLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) },
00487 { MATROSKA_ID_SIMPLEBLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) },
00488 { MATROSKA_ID_BLOCKDURATION, EBML_UINT, 0, offsetof(MatroskaBlock,duration), {.u=AV_NOPTS_VALUE} },
00489 { MATROSKA_ID_BLOCKREFERENCE, EBML_UINT, 0, offsetof(MatroskaBlock,reference) },
00490 { 1, EBML_UINT, 0, offsetof(MatroskaBlock,non_simple), {.u=1} },
00491 { 0 }
00492 };
00493
00494 static EbmlSyntax matroska_cluster[] = {
00495 { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) },
00496 { MATROSKA_ID_BLOCKGROUP, EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
00497 { MATROSKA_ID_SIMPLEBLOCK, EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
00498 { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE },
00499 { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE },
00500 { 0 }
00501 };
00502
00503 static EbmlSyntax matroska_clusters[] = {
00504 { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster} },
00505 { MATROSKA_ID_INFO, EBML_NONE },
00506 { MATROSKA_ID_CUES, EBML_NONE },
00507 { MATROSKA_ID_TAGS, EBML_NONE },
00508 { MATROSKA_ID_SEEKHEAD, EBML_NONE },
00509 { 0 }
00510 };
00511
00512 static const char *matroska_doctypes[] = { "matroska", "webm" };
00513
00514
00515
00516
00517 static int ebml_level_end(MatroskaDemuxContext *matroska)
00518 {
00519 AVIOContext *pb = matroska->ctx->pb;
00520 int64_t pos = avio_tell(pb);
00521
00522 if (matroska->num_levels > 0) {
00523 MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
00524 if (pos - level->start >= level->length || matroska->current_id) {
00525 matroska->num_levels--;
00526 return 1;
00527 }
00528 }
00529 return 0;
00530 }
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540 static int ebml_read_num(MatroskaDemuxContext *matroska, AVIOContext *pb,
00541 int max_size, uint64_t *number)
00542 {
00543 int read = 1, n = 1;
00544 uint64_t total = 0;
00545
00546
00547
00548
00549 if (!(total = avio_r8(pb))) {
00550
00551 if (!url_feof(pb)) {
00552 int64_t pos = avio_tell(pb);
00553 av_log(matroska->ctx, AV_LOG_ERROR,
00554 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
00555 pos, pos);
00556 }
00557 return AVERROR(EIO);
00558 }
00559
00560
00561 read = 8 - ff_log2_tab[total];
00562 if (read > max_size) {
00563 int64_t pos = avio_tell(pb) - 1;
00564 av_log(matroska->ctx, AV_LOG_ERROR,
00565 "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
00566 (uint8_t) total, pos, pos);
00567 return AVERROR_INVALIDDATA;
00568 }
00569
00570
00571 total ^= 1 << ff_log2_tab[total];
00572 while (n++ < read)
00573 total = (total << 8) | avio_r8(pb);
00574
00575 *number = total;
00576
00577 return read;
00578 }
00579
00585 static int ebml_read_length(MatroskaDemuxContext *matroska, AVIOContext *pb,
00586 uint64_t *number)
00587 {
00588 int res = ebml_read_num(matroska, pb, 8, number);
00589 if (res > 0 && *number + 1 == 1ULL << (7 * res))
00590 *number = 0xffffffffffffffULL;
00591 return res;
00592 }
00593
00594
00595
00596
00597
00598 static int ebml_read_uint(AVIOContext *pb, int size, uint64_t *num)
00599 {
00600 int n = 0;
00601
00602 if (size > 8)
00603 return AVERROR_INVALIDDATA;
00604
00605
00606 *num = 0;
00607 while (n++ < size)
00608 *num = (*num << 8) | avio_r8(pb);
00609
00610 return 0;
00611 }
00612
00613
00614
00615
00616
00617 static int ebml_read_float(AVIOContext *pb, int size, double *num)
00618 {
00619 if (size == 0) {
00620 *num = 0;
00621 } else if (size == 4) {
00622 *num= av_int2flt(avio_rb32(pb));
00623 } else if(size==8){
00624 *num= av_int2dbl(avio_rb64(pb));
00625 } else
00626 return AVERROR_INVALIDDATA;
00627
00628 return 0;
00629 }
00630
00631
00632
00633
00634
00635 static int ebml_read_ascii(AVIOContext *pb, int size, char **str)
00636 {
00637 av_free(*str);
00638
00639
00640 if (!(*str = av_malloc(size + 1)))
00641 return AVERROR(ENOMEM);
00642 if (avio_read(pb, (uint8_t *) *str, size) != size) {
00643 av_freep(str);
00644 return AVERROR(EIO);
00645 }
00646 (*str)[size] = '\0';
00647
00648 return 0;
00649 }
00650
00651
00652
00653
00654
00655 static int ebml_read_binary(AVIOContext *pb, int length, EbmlBin *bin)
00656 {
00657 av_free(bin->data);
00658 if (!(bin->data = av_malloc(length)))
00659 return AVERROR(ENOMEM);
00660
00661 bin->size = length;
00662 bin->pos = avio_tell(pb);
00663 if (avio_read(pb, bin->data, length) != length) {
00664 av_freep(&bin->data);
00665 return AVERROR(EIO);
00666 }
00667
00668 return 0;
00669 }
00670
00671
00672
00673
00674
00675
00676 static int ebml_read_master(MatroskaDemuxContext *matroska, uint64_t length)
00677 {
00678 AVIOContext *pb = matroska->ctx->pb;
00679 MatroskaLevel *level;
00680
00681 if (matroska->num_levels >= EBML_MAX_DEPTH) {
00682 av_log(matroska->ctx, AV_LOG_ERROR,
00683 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
00684 return AVERROR(ENOSYS);
00685 }
00686
00687 level = &matroska->levels[matroska->num_levels++];
00688 level->start = avio_tell(pb);
00689 level->length = length;
00690
00691 return 0;
00692 }
00693
00694
00695
00696
00697
00698 static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska,
00699 uint8_t *data, uint32_t size, uint64_t *num)
00700 {
00701 AVIOContext pb;
00702 ffio_init_context(&pb, data, size, 0, NULL, NULL, NULL, NULL);
00703 return ebml_read_num(matroska, &pb, FFMIN(size, 8), num);
00704 }
00705
00706
00707
00708
00709 static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska,
00710 uint8_t *data, uint32_t size, int64_t *num)
00711 {
00712 uint64_t unum;
00713 int res;
00714
00715
00716 if ((res = matroska_ebmlnum_uint(matroska, data, size, &unum)) < 0)
00717 return res;
00718
00719
00720 *num = unum - ((1LL << (7*res - 1)) - 1);
00721
00722 return res;
00723 }
00724
00725 static int ebml_parse_elem(MatroskaDemuxContext *matroska,
00726 EbmlSyntax *syntax, void *data);
00727
00728 static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
00729 uint32_t id, void *data)
00730 {
00731 int i;
00732 for (i=0; syntax[i].id; i++)
00733 if (id == syntax[i].id)
00734 break;
00735 if (!syntax[i].id && id == MATROSKA_ID_CLUSTER &&
00736 matroska->num_levels > 0 &&
00737 matroska->levels[matroska->num_levels-1].length == 0xffffffffffffff)
00738 return 0;
00739 if (!syntax[i].id && id != EBML_ID_VOID && id != EBML_ID_CRC32)
00740 av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id);
00741 return ebml_parse_elem(matroska, &syntax[i], data);
00742 }
00743
00744 static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
00745 void *data)
00746 {
00747 if (!matroska->current_id) {
00748 uint64_t id;
00749 int res = ebml_read_num(matroska, matroska->ctx->pb, 4, &id);
00750 if (res < 0)
00751 return res;
00752 matroska->current_id = id | 1 << 7*res;
00753 }
00754 return ebml_parse_id(matroska, syntax, matroska->current_id, data);
00755 }
00756
00757 static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
00758 void *data)
00759 {
00760 int i, res = 0;
00761
00762 for (i=0; syntax[i].id; i++)
00763 switch (syntax[i].type) {
00764 case EBML_UINT:
00765 *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u;
00766 break;
00767 case EBML_FLOAT:
00768 *(double *)((char *)data+syntax[i].data_offset) = syntax[i].def.f;
00769 break;
00770 case EBML_STR:
00771 case EBML_UTF8:
00772 *(char **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s);
00773 break;
00774 }
00775
00776 while (!res && !ebml_level_end(matroska))
00777 res = ebml_parse(matroska, syntax, data);
00778
00779 return res;
00780 }
00781
00782 static int ebml_parse_elem(MatroskaDemuxContext *matroska,
00783 EbmlSyntax *syntax, void *data)
00784 {
00785 static const uint64_t max_lengths[EBML_TYPE_COUNT] = {
00786 [EBML_UINT] = 8,
00787 [EBML_FLOAT] = 8,
00788
00789 [EBML_STR] = 0x1000000,
00790 [EBML_UTF8] = 0x1000000,
00791
00792 [EBML_BIN] = 0x10000000,
00793
00794 };
00795 AVIOContext *pb = matroska->ctx->pb;
00796 uint32_t id = syntax->id;
00797 uint64_t length;
00798 int res;
00799
00800 data = (char *)data + syntax->data_offset;
00801 if (syntax->list_elem_size) {
00802 EbmlList *list = data;
00803 list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);
00804 data = (char*)list->elem + list->nb_elem*syntax->list_elem_size;
00805 memset(data, 0, syntax->list_elem_size);
00806 list->nb_elem++;
00807 }
00808
00809 if (syntax->type != EBML_PASS && syntax->type != EBML_STOP) {
00810 matroska->current_id = 0;
00811 if ((res = ebml_read_length(matroska, pb, &length)) < 0)
00812 return res;
00813 if (max_lengths[syntax->type] && length > max_lengths[syntax->type]) {
00814 av_log(matroska->ctx, AV_LOG_ERROR,
00815 "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for syntax element %i\n",
00816 length, max_lengths[syntax->type], syntax->type);
00817 return AVERROR_INVALIDDATA;
00818 }
00819 }
00820
00821 switch (syntax->type) {
00822 case EBML_UINT: res = ebml_read_uint (pb, length, data); break;
00823 case EBML_FLOAT: res = ebml_read_float (pb, length, data); break;
00824 case EBML_STR:
00825 case EBML_UTF8: res = ebml_read_ascii (pb, length, data); break;
00826 case EBML_BIN: res = ebml_read_binary(pb, length, data); break;
00827 case EBML_NEST: if ((res=ebml_read_master(matroska, length)) < 0)
00828 return res;
00829 if (id == MATROSKA_ID_SEGMENT)
00830 matroska->segment_start = avio_tell(matroska->ctx->pb);
00831 return ebml_parse_nest(matroska, syntax->def.n, data);
00832 case EBML_PASS: return ebml_parse_id(matroska, syntax->def.n, id, data);
00833 case EBML_STOP: return 1;
00834 default: return avio_seek(pb,length,SEEK_CUR)<0 ? AVERROR(EIO) : 0;
00835 }
00836 if (res == AVERROR_INVALIDDATA)
00837 av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n");
00838 else if (res == AVERROR(EIO))
00839 av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n");
00840 return res;
00841 }
00842
00843 static void ebml_free(EbmlSyntax *syntax, void *data)
00844 {
00845 int i, j;
00846 for (i=0; syntax[i].id; i++) {
00847 void *data_off = (char *)data + syntax[i].data_offset;
00848 switch (syntax[i].type) {
00849 case EBML_STR:
00850 case EBML_UTF8: av_freep(data_off); break;
00851 case EBML_BIN: av_freep(&((EbmlBin *)data_off)->data); break;
00852 case EBML_NEST:
00853 if (syntax[i].list_elem_size) {
00854 EbmlList *list = data_off;
00855 char *ptr = list->elem;
00856 for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size)
00857 ebml_free(syntax[i].def.n, ptr);
00858 av_free(list->elem);
00859 } else
00860 ebml_free(syntax[i].def.n, data_off);
00861 default: break;
00862 }
00863 }
00864 }
00865
00866
00867
00868
00869
00870 static int matroska_probe(AVProbeData *p)
00871 {
00872 uint64_t total = 0;
00873 int len_mask = 0x80, size = 1, n = 1, i;
00874
00875
00876 if (AV_RB32(p->buf) != EBML_ID_HEADER)
00877 return 0;
00878
00879
00880 total = p->buf[4];
00881 while (size <= 8 && !(total & len_mask)) {
00882 size++;
00883 len_mask >>= 1;
00884 }
00885 if (size > 8)
00886 return 0;
00887 total &= (len_mask - 1);
00888 while (n < size)
00889 total = (total << 8) | p->buf[4 + n++];
00890
00891
00892 if (p->buf_size < 4 + size + total)
00893 return 0;
00894
00895
00896
00897
00898
00899 for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) {
00900 int probelen = strlen(matroska_doctypes[i]);
00901 for (n = 4+size; n <= 4+size+total-probelen; n++)
00902 if (!memcmp(p->buf+n, matroska_doctypes[i], probelen))
00903 return AVPROBE_SCORE_MAX;
00904 }
00905
00906
00907 return AVPROBE_SCORE_MAX/2;
00908 }
00909
00910 static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska,
00911 int num)
00912 {
00913 MatroskaTrack *tracks = matroska->tracks.elem;
00914 int i;
00915
00916 for (i=0; i < matroska->tracks.nb_elem; i++)
00917 if (tracks[i].num == num)
00918 return &tracks[i];
00919
00920 av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num);
00921 return NULL;
00922 }
00923
00924 static int matroska_decode_buffer(uint8_t** buf, int* buf_size,
00925 MatroskaTrack *track)
00926 {
00927 MatroskaTrackEncoding *encodings = track->encodings.elem;
00928 uint8_t* data = *buf;
00929 int isize = *buf_size;
00930 uint8_t* pkt_data = NULL;
00931 int pkt_size = isize;
00932 int result = 0;
00933 int olen;
00934
00935 if (pkt_size >= 10000000)
00936 return -1;
00937
00938 switch (encodings[0].compression.algo) {
00939 case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
00940 return encodings[0].compression.settings.size;
00941 case MATROSKA_TRACK_ENCODING_COMP_LZO:
00942 do {
00943 olen = pkt_size *= 3;
00944 pkt_data = av_realloc(pkt_data, pkt_size+AV_LZO_OUTPUT_PADDING);
00945 result = av_lzo1x_decode(pkt_data, &olen, data, &isize);
00946 } while (result==AV_LZO_OUTPUT_FULL && pkt_size<10000000);
00947 if (result)
00948 goto failed;
00949 pkt_size -= olen;
00950 break;
00951 #if CONFIG_ZLIB
00952 case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
00953 z_stream zstream = {0};
00954 if (inflateInit(&zstream) != Z_OK)
00955 return -1;
00956 zstream.next_in = data;
00957 zstream.avail_in = isize;
00958 do {
00959 pkt_size *= 3;
00960 pkt_data = av_realloc(pkt_data, pkt_size);
00961 zstream.avail_out = pkt_size - zstream.total_out;
00962 zstream.next_out = pkt_data + zstream.total_out;
00963 result = inflate(&zstream, Z_NO_FLUSH);
00964 } while (result==Z_OK && pkt_size<10000000);
00965 pkt_size = zstream.total_out;
00966 inflateEnd(&zstream);
00967 if (result != Z_STREAM_END)
00968 goto failed;
00969 break;
00970 }
00971 #endif
00972 #if CONFIG_BZLIB
00973 case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
00974 bz_stream bzstream = {0};
00975 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
00976 return -1;
00977 bzstream.next_in = data;
00978 bzstream.avail_in = isize;
00979 do {
00980 pkt_size *= 3;
00981 pkt_data = av_realloc(pkt_data, pkt_size);
00982 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
00983 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
00984 result = BZ2_bzDecompress(&bzstream);
00985 } while (result==BZ_OK && pkt_size<10000000);
00986 pkt_size = bzstream.total_out_lo32;
00987 BZ2_bzDecompressEnd(&bzstream);
00988 if (result != BZ_STREAM_END)
00989 goto failed;
00990 break;
00991 }
00992 #endif
00993 default:
00994 return -1;
00995 }
00996
00997 *buf = pkt_data;
00998 *buf_size = pkt_size;
00999 return 0;
01000 failed:
01001 av_free(pkt_data);
01002 return -1;
01003 }
01004
01005 static void matroska_fix_ass_packet(MatroskaDemuxContext *matroska,
01006 AVPacket *pkt, uint64_t display_duration)
01007 {
01008 char *line, *layer, *ptr = pkt->data, *end = ptr+pkt->size;
01009 for (; *ptr!=',' && ptr<end-1; ptr++);
01010 if (*ptr == ',')
01011 layer = ++ptr;
01012 for (; *ptr!=',' && ptr<end-1; ptr++);
01013 if (*ptr == ',') {
01014 int64_t end_pts = pkt->pts + display_duration;
01015 int sc = matroska->time_scale * pkt->pts / 10000000;
01016 int ec = matroska->time_scale * end_pts / 10000000;
01017 int sh, sm, ss, eh, em, es, len;
01018 sh = sc/360000; sc -= 360000*sh;
01019 sm = sc/ 6000; sc -= 6000*sm;
01020 ss = sc/ 100; sc -= 100*ss;
01021 eh = ec/360000; ec -= 360000*eh;
01022 em = ec/ 6000; ec -= 6000*em;
01023 es = ec/ 100; ec -= 100*es;
01024 *ptr++ = '\0';
01025 len = 50 + end-ptr + FF_INPUT_BUFFER_PADDING_SIZE;
01026 if (!(line = av_malloc(len)))
01027 return;
01028 snprintf(line,len,"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
01029 layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
01030 av_free(pkt->data);
01031 pkt->data = line;
01032 pkt->size = strlen(line);
01033 }
01034 }
01035
01036 static void matroska_merge_packets(AVPacket *out, AVPacket *in)
01037 {
01038 out->data = av_realloc(out->data, out->size+in->size);
01039 memcpy(out->data+out->size, in->data, in->size);
01040 out->size += in->size;
01041 av_destruct_packet(in);
01042 av_free(in);
01043 }
01044
01045 static void matroska_convert_tag(AVFormatContext *s, EbmlList *list,
01046 AVMetadata **metadata, char *prefix)
01047 {
01048 MatroskaTag *tags = list->elem;
01049 char key[1024];
01050 int i;
01051
01052 for (i=0; i < list->nb_elem; i++) {
01053 const char *lang = strcmp(tags[i].lang, "und") ? tags[i].lang : NULL;
01054
01055 if (!tags[i].name) {
01056 av_log(s, AV_LOG_WARNING, "Skipping invalid tag with no TagName.\n");
01057 continue;
01058 }
01059 if (prefix) snprintf(key, sizeof(key), "%s/%s", prefix, tags[i].name);
01060 else av_strlcpy(key, tags[i].name, sizeof(key));
01061 if (tags[i].def || !lang) {
01062 av_metadata_set2(metadata, key, tags[i].string, 0);
01063 if (tags[i].sub.nb_elem)
01064 matroska_convert_tag(s, &tags[i].sub, metadata, key);
01065 }
01066 if (lang) {
01067 av_strlcat(key, "-", sizeof(key));
01068 av_strlcat(key, lang, sizeof(key));
01069 av_metadata_set2(metadata, key, tags[i].string, 0);
01070 if (tags[i].sub.nb_elem)
01071 matroska_convert_tag(s, &tags[i].sub, metadata, key);
01072 }
01073 }
01074 ff_metadata_conv(metadata, NULL, ff_mkv_metadata_conv);
01075 }
01076
01077 static void matroska_convert_tags(AVFormatContext *s)
01078 {
01079 MatroskaDemuxContext *matroska = s->priv_data;
01080 MatroskaTags *tags = matroska->tags.elem;
01081 int i, j;
01082
01083 for (i=0; i < matroska->tags.nb_elem; i++) {
01084 if (tags[i].target.attachuid) {
01085 MatroskaAttachement *attachment = matroska->attachments.elem;
01086 for (j=0; j<matroska->attachments.nb_elem; j++)
01087 if (attachment[j].uid == tags[i].target.attachuid)
01088 matroska_convert_tag(s, &tags[i].tag,
01089 &attachment[j].stream->metadata, NULL);
01090 } else if (tags[i].target.chapteruid) {
01091 MatroskaChapter *chapter = matroska->chapters.elem;
01092 for (j=0; j<matroska->chapters.nb_elem; j++)
01093 if (chapter[j].uid == tags[i].target.chapteruid)
01094 matroska_convert_tag(s, &tags[i].tag,
01095 &chapter[j].chapter->metadata, NULL);
01096 } else if (tags[i].target.trackuid) {
01097 MatroskaTrack *track = matroska->tracks.elem;
01098 for (j=0; j<matroska->tracks.nb_elem; j++)
01099 if (track[j].uid == tags[i].target.trackuid)
01100 matroska_convert_tag(s, &tags[i].tag,
01101 &track[j].stream->metadata, NULL);
01102 } else {
01103 matroska_convert_tag(s, &tags[i].tag, &s->metadata,
01104 tags[i].target.type);
01105 }
01106 }
01107 }
01108
01109 static void matroska_execute_seekhead(MatroskaDemuxContext *matroska)
01110 {
01111 EbmlList *seekhead_list = &matroska->seekhead;
01112 MatroskaSeekhead *seekhead = seekhead_list->elem;
01113 uint32_t level_up = matroska->level_up;
01114 int64_t before_pos = avio_tell(matroska->ctx->pb);
01115 uint32_t saved_id = matroska->current_id;
01116 MatroskaLevel level;
01117 int i;
01118
01119
01120 if (url_is_streamed(matroska->ctx->pb) ||
01121 (matroska->ctx->flags & AVFMT_FLAG_IGNIDX))
01122 return;
01123
01124 for (i=0; i<seekhead_list->nb_elem; i++) {
01125 int64_t offset = seekhead[i].pos + matroska->segment_start;
01126
01127 if (seekhead[i].pos <= before_pos
01128 || seekhead[i].id == MATROSKA_ID_SEEKHEAD
01129 || seekhead[i].id == MATROSKA_ID_CLUSTER)
01130 continue;
01131
01132
01133 if (avio_seek(matroska->ctx->pb, offset, SEEK_SET) != offset)
01134 continue;
01135
01136
01137
01138 if (matroska->num_levels == EBML_MAX_DEPTH) {
01139 av_log(matroska->ctx, AV_LOG_INFO,
01140 "Max EBML element depth (%d) reached, "
01141 "cannot parse further.\n", EBML_MAX_DEPTH);
01142 break;
01143 }
01144
01145 level.start = 0;
01146 level.length = (uint64_t)-1;
01147 matroska->levels[matroska->num_levels] = level;
01148 matroska->num_levels++;
01149 matroska->current_id = 0;
01150
01151 ebml_parse(matroska, matroska_segment, matroska);
01152
01153
01154 while (matroska->num_levels) {
01155 uint64_t length = matroska->levels[--matroska->num_levels].length;
01156 if (length == (uint64_t)-1)
01157 break;
01158 }
01159 }
01160
01161
01162 avio_seek(matroska->ctx->pb, before_pos, SEEK_SET);
01163 matroska->level_up = level_up;
01164 matroska->current_id = saved_id;
01165 }
01166
01167 static int matroska_aac_profile(char *codec_id)
01168 {
01169 static const char * const aac_profiles[] = { "MAIN", "LC", "SSR" };
01170 int profile;
01171
01172 for (profile=0; profile<FF_ARRAY_ELEMS(aac_profiles); profile++)
01173 if (strstr(codec_id, aac_profiles[profile]))
01174 break;
01175 return profile + 1;
01176 }
01177
01178 static int matroska_aac_sri(int samplerate)
01179 {
01180 int sri;
01181
01182 for (sri=0; sri<FF_ARRAY_ELEMS(ff_mpeg4audio_sample_rates); sri++)
01183 if (ff_mpeg4audio_sample_rates[sri] == samplerate)
01184 break;
01185 return sri;
01186 }
01187
01188 static int matroska_read_header(AVFormatContext *s, AVFormatParameters *ap)
01189 {
01190 MatroskaDemuxContext *matroska = s->priv_data;
01191 EbmlList *attachements_list = &matroska->attachments;
01192 MatroskaAttachement *attachements;
01193 EbmlList *chapters_list = &matroska->chapters;
01194 MatroskaChapter *chapters;
01195 MatroskaTrack *tracks;
01196 EbmlList *index_list;
01197 MatroskaIndex *index;
01198 int index_scale = 1;
01199 uint64_t max_start = 0;
01200 Ebml ebml = { 0 };
01201 AVStream *st;
01202 int i, j, res;
01203
01204 matroska->ctx = s;
01205
01206
01207 if (ebml_parse(matroska, ebml_syntax, &ebml)
01208 || ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t)
01209 || ebml.id_length > sizeof(uint32_t) || ebml.doctype_version > 2) {
01210 av_log(matroska->ctx, AV_LOG_ERROR,
01211 "EBML header using unsupported features\n"
01212 "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
01213 ebml.version, ebml.doctype, ebml.doctype_version);
01214 ebml_free(ebml_syntax, &ebml);
01215 return AVERROR_PATCHWELCOME;
01216 }
01217 for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++)
01218 if (!strcmp(ebml.doctype, matroska_doctypes[i]))
01219 break;
01220 if (i >= FF_ARRAY_ELEMS(matroska_doctypes)) {
01221 av_log(s, AV_LOG_WARNING, "Unknown EBML doctype '%s'\n", ebml.doctype);
01222 }
01223 ebml_free(ebml_syntax, &ebml);
01224
01225
01226 if ((res = ebml_parse(matroska, matroska_segments, matroska)) < 0)
01227 return res;
01228 matroska_execute_seekhead(matroska);
01229
01230 if (!matroska->time_scale)
01231 matroska->time_scale = 1000000;
01232 if (matroska->duration)
01233 matroska->ctx->duration = matroska->duration * matroska->time_scale
01234 * 1000 / AV_TIME_BASE;
01235 av_metadata_set2(&s->metadata, "title", matroska->title, 0);
01236
01237 tracks = matroska->tracks.elem;
01238 for (i=0; i < matroska->tracks.nb_elem; i++) {
01239 MatroskaTrack *track = &tracks[i];
01240 enum CodecID codec_id = CODEC_ID_NONE;
01241 EbmlList *encodings_list = &tracks->encodings;
01242 MatroskaTrackEncoding *encodings = encodings_list->elem;
01243 uint8_t *extradata = NULL;
01244 int extradata_size = 0;
01245 int extradata_offset = 0;
01246 AVIOContext b;
01247
01248
01249 if (track->type != MATROSKA_TRACK_TYPE_VIDEO &&
01250 track->type != MATROSKA_TRACK_TYPE_AUDIO &&
01251 track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
01252 av_log(matroska->ctx, AV_LOG_INFO,
01253 "Unknown or unsupported track type %"PRIu64"\n",
01254 track->type);
01255 continue;
01256 }
01257 if (track->codec_id == NULL)
01258 continue;
01259
01260 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
01261 if (!track->default_duration)
01262 track->default_duration = 1000000000/track->video.frame_rate;
01263 if (!track->video.display_width)
01264 track->video.display_width = track->video.pixel_width;
01265 if (!track->video.display_height)
01266 track->video.display_height = track->video.pixel_height;
01267 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
01268 if (!track->audio.out_samplerate)
01269 track->audio.out_samplerate = track->audio.samplerate;
01270 }
01271 if (encodings_list->nb_elem > 1) {
01272 av_log(matroska->ctx, AV_LOG_ERROR,
01273 "Multiple combined encodings no supported");
01274 } else if (encodings_list->nb_elem == 1) {
01275 if (encodings[0].type ||
01276 (encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
01277 #if CONFIG_ZLIB
01278 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
01279 #endif
01280 #if CONFIG_BZLIB
01281 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
01282 #endif
01283 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO)) {
01284 encodings[0].scope = 0;
01285 av_log(matroska->ctx, AV_LOG_ERROR,
01286 "Unsupported encoding type");
01287 } else if (track->codec_priv.size && encodings[0].scope&2) {
01288 uint8_t *codec_priv = track->codec_priv.data;
01289 int offset = matroska_decode_buffer(&track->codec_priv.data,
01290 &track->codec_priv.size,
01291 track);
01292 if (offset < 0) {
01293 track->codec_priv.data = NULL;
01294 track->codec_priv.size = 0;
01295 av_log(matroska->ctx, AV_LOG_ERROR,
01296 "Failed to decode codec private data\n");
01297 } else if (offset > 0) {
01298 track->codec_priv.data = av_malloc(track->codec_priv.size + offset);
01299 memcpy(track->codec_priv.data,
01300 encodings[0].compression.settings.data, offset);
01301 memcpy(track->codec_priv.data+offset, codec_priv,
01302 track->codec_priv.size);
01303 track->codec_priv.size += offset;
01304 }
01305 if (codec_priv != track->codec_priv.data)
01306 av_free(codec_priv);
01307 }
01308 }
01309
01310 for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
01311 if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
01312 strlen(ff_mkv_codec_tags[j].str))){
01313 codec_id= ff_mkv_codec_tags[j].id;
01314 break;
01315 }
01316 }
01317
01318 st = track->stream = av_new_stream(s, 0);
01319 if (st == NULL)
01320 return AVERROR(ENOMEM);
01321
01322 if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC")
01323 && track->codec_priv.size >= 40
01324 && track->codec_priv.data != NULL) {
01325 track->ms_compat = 1;
01326 track->video.fourcc = AV_RL32(track->codec_priv.data + 16);
01327 codec_id = ff_codec_get_id(ff_codec_bmp_tags, track->video.fourcc);
01328 extradata_offset = 40;
01329 } else if (!strcmp(track->codec_id, "A_MS/ACM")
01330 && track->codec_priv.size >= 14
01331 && track->codec_priv.data != NULL) {
01332 ffio_init_context(&b, track->codec_priv.data, track->codec_priv.size,
01333 URL_RDONLY, NULL, NULL, NULL, NULL);
01334 ff_get_wav_header(&b, st->codec, track->codec_priv.size);
01335 codec_id = st->codec->codec_id;
01336 extradata_offset = FFMIN(track->codec_priv.size, 18);
01337 } else if (!strcmp(track->codec_id, "V_QUICKTIME")
01338 && (track->codec_priv.size >= 86)
01339 && (track->codec_priv.data != NULL)) {
01340 track->video.fourcc = AV_RL32(track->codec_priv.data);
01341 codec_id=ff_codec_get_id(codec_movvideo_tags, track->video.fourcc);
01342 } else if (codec_id == CODEC_ID_PCM_S16BE) {
01343 switch (track->audio.bitdepth) {
01344 case 8: codec_id = CODEC_ID_PCM_U8; break;
01345 case 24: codec_id = CODEC_ID_PCM_S24BE; break;
01346 case 32: codec_id = CODEC_ID_PCM_S32BE; break;
01347 }
01348 } else if (codec_id == CODEC_ID_PCM_S16LE) {
01349 switch (track->audio.bitdepth) {
01350 case 8: codec_id = CODEC_ID_PCM_U8; break;
01351 case 24: codec_id = CODEC_ID_PCM_S24LE; break;
01352 case 32: codec_id = CODEC_ID_PCM_S32LE; break;
01353 }
01354 } else if (codec_id==CODEC_ID_PCM_F32LE && track->audio.bitdepth==64) {
01355 codec_id = CODEC_ID_PCM_F64LE;
01356 } else if (codec_id == CODEC_ID_AAC && !track->codec_priv.size) {
01357 int profile = matroska_aac_profile(track->codec_id);
01358 int sri = matroska_aac_sri(track->audio.samplerate);
01359 extradata = av_malloc(5);
01360 if (extradata == NULL)
01361 return AVERROR(ENOMEM);
01362 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
01363 extradata[1] = ((sri&0x01) << 7) | (track->audio.channels<<3);
01364 if (strstr(track->codec_id, "SBR")) {
01365 sri = matroska_aac_sri(track->audio.out_samplerate);
01366 extradata[2] = 0x56;
01367 extradata[3] = 0xE5;
01368 extradata[4] = 0x80 | (sri<<3);
01369 extradata_size = 5;
01370 } else
01371 extradata_size = 2;
01372 } else if (codec_id == CODEC_ID_TTA) {
01373 extradata_size = 30;
01374 extradata = av_mallocz(extradata_size);
01375 if (extradata == NULL)
01376 return AVERROR(ENOMEM);
01377 ffio_init_context(&b, extradata, extradata_size, 1,
01378 NULL, NULL, NULL, NULL);
01379 avio_write(&b, "TTA1", 4);
01380 avio_wl16(&b, 1);
01381 avio_wl16(&b, track->audio.channels);
01382 avio_wl16(&b, track->audio.bitdepth);
01383 avio_wl32(&b, track->audio.out_samplerate);
01384 avio_wl32(&b, matroska->ctx->duration * track->audio.out_samplerate);
01385 } else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
01386 codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
01387 extradata_offset = 26;
01388 } else if (codec_id == CODEC_ID_RA_144) {
01389 track->audio.out_samplerate = 8000;
01390 track->audio.channels = 1;
01391 } else if (codec_id == CODEC_ID_RA_288 || codec_id == CODEC_ID_COOK ||
01392 codec_id == CODEC_ID_ATRAC3 || codec_id == CODEC_ID_SIPR) {
01393 int flavor;
01394 ffio_init_context(&b, track->codec_priv.data,track->codec_priv.size,
01395 0, NULL, NULL, NULL, NULL);
01396 avio_seek(&b, 22, SEEK_CUR);
01397 flavor = avio_rb16(&b);
01398 track->audio.coded_framesize = avio_rb32(&b);
01399 avio_seek(&b, 12, SEEK_CUR);
01400 track->audio.sub_packet_h = avio_rb16(&b);
01401 track->audio.frame_size = avio_rb16(&b);
01402 track->audio.sub_packet_size = avio_rb16(&b);
01403 track->audio.buf = av_malloc(track->audio.frame_size * track->audio.sub_packet_h);
01404 if (codec_id == CODEC_ID_RA_288) {
01405 st->codec->block_align = track->audio.coded_framesize;
01406 track->codec_priv.size = 0;
01407 } else {
01408 if (codec_id == CODEC_ID_SIPR && flavor < 4) {
01409 const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
01410 track->audio.sub_packet_size = ff_sipr_subpk_size[flavor];
01411 st->codec->bit_rate = sipr_bit_rate[flavor];
01412 }
01413 st->codec->block_align = track->audio.sub_packet_size;
01414 extradata_offset = 78;
01415 }
01416 }
01417 track->codec_priv.size -= extradata_offset;
01418
01419 if (codec_id == CODEC_ID_NONE)
01420 av_log(matroska->ctx, AV_LOG_INFO,
01421 "Unknown/unsupported CodecID %s.\n", track->codec_id);
01422
01423 if (track->time_scale < 0.01)
01424 track->time_scale = 1.0;
01425 av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000);
01426
01427 st->codec->codec_id = codec_id;
01428 st->start_time = 0;
01429 if (strcmp(track->language, "und"))
01430 av_metadata_set2(&st->metadata, "language", track->language, 0);
01431 av_metadata_set2(&st->metadata, "title", track->name, 0);
01432
01433 if (track->flag_default)
01434 st->disposition |= AV_DISPOSITION_DEFAULT;
01435 if (track->flag_forced)
01436 st->disposition |= AV_DISPOSITION_FORCED;
01437
01438 if (track->default_duration)
01439 av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
01440 track->default_duration, 1000000000, 30000);
01441
01442 if (!st->codec->extradata) {
01443 if(extradata){
01444 st->codec->extradata = extradata;
01445 st->codec->extradata_size = extradata_size;
01446 } else if(track->codec_priv.data && track->codec_priv.size > 0){
01447 st->codec->extradata = av_mallocz(track->codec_priv.size +
01448 FF_INPUT_BUFFER_PADDING_SIZE);
01449 if(st->codec->extradata == NULL)
01450 return AVERROR(ENOMEM);
01451 st->codec->extradata_size = track->codec_priv.size;
01452 memcpy(st->codec->extradata,
01453 track->codec_priv.data + extradata_offset,
01454 track->codec_priv.size);
01455 }
01456 }
01457
01458 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
01459 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01460 st->codec->codec_tag = track->video.fourcc;
01461 st->codec->width = track->video.pixel_width;
01462 st->codec->height = track->video.pixel_height;
01463 av_reduce(&st->sample_aspect_ratio.num,
01464 &st->sample_aspect_ratio.den,
01465 st->codec->height * track->video.display_width,
01466 st->codec-> width * track->video.display_height,
01467 255);
01468 if (st->codec->codec_id != CODEC_ID_H264)
01469 st->need_parsing = AVSTREAM_PARSE_HEADERS;
01470 if (track->default_duration)
01471 st->avg_frame_rate = av_d2q(1000000000.0/track->default_duration, INT_MAX);
01472 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
01473 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
01474 st->codec->sample_rate = track->audio.out_samplerate;
01475 st->codec->channels = track->audio.channels;
01476 if (st->codec->codec_id != CODEC_ID_AAC)
01477 st->need_parsing = AVSTREAM_PARSE_HEADERS;
01478 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
01479 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
01480 }
01481 }
01482
01483 attachements = attachements_list->elem;
01484 for (j=0; j<attachements_list->nb_elem; j++) {
01485 if (!(attachements[j].filename && attachements[j].mime &&
01486 attachements[j].bin.data && attachements[j].bin.size > 0)) {
01487 av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
01488 } else {
01489 AVStream *st = av_new_stream(s, 0);
01490 if (st == NULL)
01491 break;
01492 av_metadata_set2(&st->metadata, "filename",attachements[j].filename, 0);
01493 st->codec->codec_id = CODEC_ID_NONE;
01494 st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
01495 st->codec->extradata = av_malloc(attachements[j].bin.size);
01496 if(st->codec->extradata == NULL)
01497 break;
01498 st->codec->extradata_size = attachements[j].bin.size;
01499 memcpy(st->codec->extradata, attachements[j].bin.data, attachements[j].bin.size);
01500
01501 for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
01502 if (!strncmp(ff_mkv_mime_tags[i].str, attachements[j].mime,
01503 strlen(ff_mkv_mime_tags[i].str))) {
01504 st->codec->codec_id = ff_mkv_mime_tags[i].id;
01505 break;
01506 }
01507 }
01508 attachements[j].stream = st;
01509 }
01510 }
01511
01512 chapters = chapters_list->elem;
01513 for (i=0; i<chapters_list->nb_elem; i++)
01514 if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid
01515 && (max_start==0 || chapters[i].start > max_start)) {
01516 chapters[i].chapter =
01517 ff_new_chapter(s, chapters[i].uid, (AVRational){1, 1000000000},
01518 chapters[i].start, chapters[i].end,
01519 chapters[i].title);
01520 av_metadata_set2(&chapters[i].chapter->metadata,
01521 "title", chapters[i].title, 0);
01522 max_start = chapters[i].start;
01523 }
01524
01525 index_list = &matroska->index;
01526 index = index_list->elem;
01527 if (index_list->nb_elem
01528 && index[0].time > 100000000000000/matroska->time_scale) {
01529 av_log(matroska->ctx, AV_LOG_WARNING, "Working around broken index.\n");
01530 index_scale = matroska->time_scale;
01531 }
01532 for (i=0; i<index_list->nb_elem; i++) {
01533 EbmlList *pos_list = &index[i].pos;
01534 MatroskaIndexPos *pos = pos_list->elem;
01535 for (j=0; j<pos_list->nb_elem; j++) {
01536 MatroskaTrack *track = matroska_find_track_by_num(matroska,
01537 pos[j].track);
01538 if (track && track->stream)
01539 av_add_index_entry(track->stream,
01540 pos[j].pos + matroska->segment_start,
01541 index[i].time/index_scale, 0, 0,
01542 AVINDEX_KEYFRAME);
01543 }
01544 }
01545
01546 matroska_convert_tags(s);
01547
01548 return 0;
01549 }
01550
01551
01552
01553
01554
01555 static int matroska_deliver_packet(MatroskaDemuxContext *matroska,
01556 AVPacket *pkt)
01557 {
01558 if (matroska->num_packets > 0) {
01559 memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
01560 av_free(matroska->packets[0]);
01561 if (matroska->num_packets > 1) {
01562 memmove(&matroska->packets[0], &matroska->packets[1],
01563 (matroska->num_packets - 1) * sizeof(AVPacket *));
01564 matroska->packets =
01565 av_realloc(matroska->packets, (matroska->num_packets - 1) *
01566 sizeof(AVPacket *));
01567 } else {
01568 av_freep(&matroska->packets);
01569 }
01570 matroska->num_packets--;
01571 return 0;
01572 }
01573
01574 return -1;
01575 }
01576
01577
01578
01579
01580 static void matroska_clear_queue(MatroskaDemuxContext *matroska)
01581 {
01582 if (matroska->packets) {
01583 int n;
01584 for (n = 0; n < matroska->num_packets; n++) {
01585 av_free_packet(matroska->packets[n]);
01586 av_free(matroska->packets[n]);
01587 }
01588 av_freep(&matroska->packets);
01589 matroska->num_packets = 0;
01590 }
01591 }
01592
01593 static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data,
01594 int size, int64_t pos, uint64_t cluster_time,
01595 uint64_t duration, int is_keyframe,
01596 int64_t cluster_pos)
01597 {
01598 uint64_t timecode = AV_NOPTS_VALUE;
01599 MatroskaTrack *track;
01600 int res = 0;
01601 AVStream *st;
01602 AVPacket *pkt;
01603 int16_t block_time;
01604 uint32_t *lace_size = NULL;
01605 int n, flags, laces = 0;
01606 uint64_t num;
01607
01608 if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) {
01609 av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
01610 return res;
01611 }
01612 data += n;
01613 size -= n;
01614
01615 track = matroska_find_track_by_num(matroska, num);
01616 if (size <= 3 || !track || !track->stream) {
01617 av_log(matroska->ctx, AV_LOG_INFO,
01618 "Invalid stream %"PRIu64" or size %u\n", num, size);
01619 return res;
01620 }
01621 st = track->stream;
01622 if (st->discard >= AVDISCARD_ALL)
01623 return res;
01624 if (duration == AV_NOPTS_VALUE)
01625 duration = track->default_duration / matroska->time_scale;
01626
01627 block_time = AV_RB16(data);
01628 data += 2;
01629 flags = *data++;
01630 size -= 3;
01631 if (is_keyframe == -1)
01632 is_keyframe = flags & 0x80 ? AV_PKT_FLAG_KEY : 0;
01633
01634 if (cluster_time != (uint64_t)-1
01635 && (block_time >= 0 || cluster_time >= -block_time)) {
01636 timecode = cluster_time + block_time;
01637 if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE
01638 && timecode < track->end_timecode)
01639 is_keyframe = 0;
01640 if (is_keyframe)
01641 av_add_index_entry(st, cluster_pos, timecode, 0,0,AVINDEX_KEYFRAME);
01642 track->end_timecode = FFMAX(track->end_timecode, timecode+duration);
01643 }
01644
01645 if (matroska->skip_to_keyframe && track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
01646 if (!is_keyframe || timecode < matroska->skip_to_timecode)
01647 return res;
01648 matroska->skip_to_keyframe = 0;
01649 }
01650
01651 switch ((flags & 0x06) >> 1) {
01652 case 0x0:
01653 laces = 1;
01654 lace_size = av_mallocz(sizeof(int));
01655 lace_size[0] = size;
01656 break;
01657
01658 case 0x1:
01659 case 0x2:
01660 case 0x3:
01661 assert(size>0);
01662 laces = (*data) + 1;
01663 data += 1;
01664 size -= 1;
01665 lace_size = av_mallocz(laces * sizeof(int));
01666
01667 switch ((flags & 0x06) >> 1) {
01668 case 0x1: {
01669 uint8_t temp;
01670 uint32_t total = 0;
01671 for (n = 0; res == 0 && n < laces - 1; n++) {
01672 while (1) {
01673 if (size == 0) {
01674 res = -1;
01675 break;
01676 }
01677 temp = *data;
01678 lace_size[n] += temp;
01679 data += 1;
01680 size -= 1;
01681 if (temp != 0xff)
01682 break;
01683 }
01684 total += lace_size[n];
01685 }
01686 lace_size[n] = size - total;
01687 break;
01688 }
01689
01690 case 0x2:
01691 for (n = 0; n < laces; n++)
01692 lace_size[n] = size / laces;
01693 break;
01694
01695 case 0x3: {
01696 uint32_t total;
01697 n = matroska_ebmlnum_uint(matroska, data, size, &num);
01698 if (n < 0) {
01699 av_log(matroska->ctx, AV_LOG_INFO,
01700 "EBML block data error\n");
01701 break;
01702 }
01703 data += n;
01704 size -= n;
01705 total = lace_size[0] = num;
01706 for (n = 1; res == 0 && n < laces - 1; n++) {
01707 int64_t snum;
01708 int r;
01709 r = matroska_ebmlnum_sint(matroska, data, size, &snum);
01710 if (r < 0) {
01711 av_log(matroska->ctx, AV_LOG_INFO,
01712 "EBML block data error\n");
01713 break;
01714 }
01715 data += r;
01716 size -= r;
01717 lace_size[n] = lace_size[n - 1] + snum;
01718 total += lace_size[n];
01719 }
01720 lace_size[n] = size - total;
01721 break;
01722 }
01723 }
01724 break;
01725 }
01726
01727 if (res == 0) {
01728 for (n = 0; n < laces; n++) {
01729 if ((st->codec->codec_id == CODEC_ID_RA_288 ||
01730 st->codec->codec_id == CODEC_ID_COOK ||
01731 st->codec->codec_id == CODEC_ID_SIPR ||
01732 st->codec->codec_id == CODEC_ID_ATRAC3) &&
01733 st->codec->block_align && track->audio.sub_packet_size) {
01734 int a = st->codec->block_align;
01735 int sps = track->audio.sub_packet_size;
01736 int cfs = track->audio.coded_framesize;
01737 int h = track->audio.sub_packet_h;
01738 int y = track->audio.sub_packet_cnt;
01739 int w = track->audio.frame_size;
01740 int x;
01741
01742 if (!track->audio.pkt_cnt) {
01743 if (st->codec->codec_id == CODEC_ID_RA_288)
01744 for (x=0; x<h/2; x++)
01745 memcpy(track->audio.buf+x*2*w+y*cfs,
01746 data+x*cfs, cfs);
01747 else if (st->codec->codec_id == CODEC_ID_SIPR)
01748 memcpy(track->audio.buf + y*w, data, w);
01749 else
01750 for (x=0; x<w/sps; x++)
01751 memcpy(track->audio.buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
01752
01753 if (++track->audio.sub_packet_cnt >= h) {
01754 if (st->codec->codec_id == CODEC_ID_SIPR)
01755 ff_rm_reorder_sipr_data(track->audio.buf, h, w);
01756 track->audio.sub_packet_cnt = 0;
01757 track->audio.pkt_cnt = h*w / a;
01758 }
01759 }
01760 while (track->audio.pkt_cnt) {
01761 pkt = av_mallocz(sizeof(AVPacket));
01762 av_new_packet(pkt, a);
01763 memcpy(pkt->data, track->audio.buf
01764 + a * (h*w / a - track->audio.pkt_cnt--), a);
01765 pkt->pos = pos;
01766 pkt->stream_index = st->index;
01767 dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
01768 }
01769 } else {
01770 MatroskaTrackEncoding *encodings = track->encodings.elem;
01771 int offset = 0, pkt_size = lace_size[n];
01772 uint8_t *pkt_data = data;
01773
01774 if (pkt_size > size) {
01775 av_log(matroska->ctx, AV_LOG_ERROR, "Invalid packet size\n");
01776 break;
01777 }
01778
01779 if (encodings && encodings->scope & 1) {
01780 offset = matroska_decode_buffer(&pkt_data,&pkt_size, track);
01781 if (offset < 0)
01782 continue;
01783 }
01784
01785 pkt = av_mallocz(sizeof(AVPacket));
01786
01787 if (av_new_packet(pkt, pkt_size+offset) < 0) {
01788 av_free(pkt);
01789 res = AVERROR(ENOMEM);
01790 break;
01791 }
01792 if (offset)
01793 memcpy (pkt->data, encodings->compression.settings.data, offset);
01794 memcpy (pkt->data+offset, pkt_data, pkt_size);
01795
01796 if (pkt_data != data)
01797 av_free(pkt_data);
01798
01799 if (n == 0)
01800 pkt->flags = is_keyframe;
01801 pkt->stream_index = st->index;
01802
01803 if (track->ms_compat)
01804 pkt->dts = timecode;
01805 else
01806 pkt->pts = timecode;
01807 pkt->pos = pos;
01808 if (st->codec->codec_id == CODEC_ID_TEXT)
01809 pkt->convergence_duration = duration;
01810 else if (track->type != MATROSKA_TRACK_TYPE_SUBTITLE)
01811 pkt->duration = duration;
01812
01813 if (st->codec->codec_id == CODEC_ID_SSA)
01814 matroska_fix_ass_packet(matroska, pkt, duration);
01815
01816 if (matroska->prev_pkt &&
01817 timecode != AV_NOPTS_VALUE &&
01818 matroska->prev_pkt->pts == timecode &&
01819 matroska->prev_pkt->stream_index == st->index &&
01820 st->codec->codec_id == CODEC_ID_SSA)
01821 matroska_merge_packets(matroska->prev_pkt, pkt);
01822 else {
01823 dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
01824 matroska->prev_pkt = pkt;
01825 }
01826 }
01827
01828 if (timecode != AV_NOPTS_VALUE)
01829 timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
01830 data += lace_size[n];
01831 size -= lace_size[n];
01832 }
01833 }
01834
01835 av_free(lace_size);
01836 return res;
01837 }
01838
01839 static int matroska_parse_cluster(MatroskaDemuxContext *matroska)
01840 {
01841 MatroskaCluster cluster = { 0 };
01842 EbmlList *blocks_list;
01843 MatroskaBlock *blocks;
01844 int i, res;
01845 int64_t pos = avio_tell(matroska->ctx->pb);
01846 matroska->prev_pkt = NULL;
01847 if (matroska->current_id)
01848 pos -= 4;
01849 res = ebml_parse(matroska, matroska_clusters, &cluster);
01850 blocks_list = &cluster.blocks;
01851 blocks = blocks_list->elem;
01852 for (i=0; i<blocks_list->nb_elem; i++)
01853 if (blocks[i].bin.size > 0 && blocks[i].bin.data) {
01854 int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1;
01855 res=matroska_parse_block(matroska,
01856 blocks[i].bin.data, blocks[i].bin.size,
01857 blocks[i].bin.pos, cluster.timecode,
01858 blocks[i].duration, is_keyframe,
01859 pos);
01860 }
01861 ebml_free(matroska_cluster, &cluster);
01862 if (res < 0) matroska->done = 1;
01863 return res;
01864 }
01865
01866 static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt)
01867 {
01868 MatroskaDemuxContext *matroska = s->priv_data;
01869
01870 while (matroska_deliver_packet(matroska, pkt)) {
01871 if (matroska->done)
01872 return AVERROR_EOF;
01873 matroska_parse_cluster(matroska);
01874 }
01875
01876 return 0;
01877 }
01878
01879 static int matroska_read_seek(AVFormatContext *s, int stream_index,
01880 int64_t timestamp, int flags)
01881 {
01882 MatroskaDemuxContext *matroska = s->priv_data;
01883 MatroskaTrack *tracks = matroska->tracks.elem;
01884 AVStream *st = s->streams[stream_index];
01885 int i, index, index_sub, index_min;
01886
01887 if (!st->nb_index_entries)
01888 return 0;
01889 timestamp = FFMAX(timestamp, st->index_entries[0].timestamp);
01890
01891 if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
01892 avio_seek(s->pb, st->index_entries[st->nb_index_entries-1].pos, SEEK_SET);
01893 while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
01894 matroska_clear_queue(matroska);
01895 if (matroska_parse_cluster(matroska) < 0)
01896 break;
01897 }
01898 }
01899
01900 matroska_clear_queue(matroska);
01901 if (index < 0)
01902 return 0;
01903
01904 index_min = index;
01905 for (i=0; i < matroska->tracks.nb_elem; i++) {
01906 tracks[i].end_timecode = 0;
01907 if (tracks[i].type == MATROSKA_TRACK_TYPE_SUBTITLE
01908 && !tracks[i].stream->discard != AVDISCARD_ALL) {
01909 index_sub = av_index_search_timestamp(tracks[i].stream, st->index_entries[index].timestamp, AVSEEK_FLAG_BACKWARD);
01910 if (index_sub >= 0
01911 && st->index_entries[index_sub].pos < st->index_entries[index_min].pos
01912 && st->index_entries[index].timestamp - st->index_entries[index_sub].timestamp < 30000000000/matroska->time_scale)
01913 index_min = index_sub;
01914 }
01915 }
01916
01917 avio_seek(s->pb, st->index_entries[index_min].pos, SEEK_SET);
01918 matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
01919 matroska->skip_to_timecode = st->index_entries[index].timestamp;
01920 matroska->done = 0;
01921 av_update_cur_dts(s, st, st->index_entries[index].timestamp);
01922 return 0;
01923 }
01924
01925 static int matroska_read_close(AVFormatContext *s)
01926 {
01927 MatroskaDemuxContext *matroska = s->priv_data;
01928 MatroskaTrack *tracks = matroska->tracks.elem;
01929 int n;
01930
01931 matroska_clear_queue(matroska);
01932
01933 for (n=0; n < matroska->tracks.nb_elem; n++)
01934 if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO)
01935 av_free(tracks[n].audio.buf);
01936 ebml_free(matroska_segment, matroska);
01937
01938 return 0;
01939 }
01940
01941 AVInputFormat ff_matroska_demuxer = {
01942 "matroska,webm",
01943 NULL_IF_CONFIG_SMALL("Matroska/WebM file format"),
01944 sizeof(MatroskaDemuxContext),
01945 matroska_probe,
01946 matroska_read_header,
01947 matroska_read_packet,
01948 matroska_read_close,
01949 matroska_read_seek,
01950 };