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