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