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 "rmsipr.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_fast_padded_malloc(&bin->data, &bin->size, length);
00766 if (!bin->data)
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 bin->size = 0;
00774 return AVERROR(EIO);
00775 }
00776
00777 return 0;
00778 }
00779
00780
00781
00782
00783
00784
00785 static int ebml_read_master(MatroskaDemuxContext *matroska, uint64_t length)
00786 {
00787 AVIOContext *pb = matroska->ctx->pb;
00788 MatroskaLevel *level;
00789
00790 if (matroska->num_levels >= EBML_MAX_DEPTH) {
00791 av_log(matroska->ctx, AV_LOG_ERROR,
00792 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
00793 return AVERROR(ENOSYS);
00794 }
00795
00796 level = &matroska->levels[matroska->num_levels++];
00797 level->start = avio_tell(pb);
00798 level->length = length;
00799
00800 return 0;
00801 }
00802
00803
00804
00805
00806
00807 static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska,
00808 uint8_t *data, uint32_t size, uint64_t *num)
00809 {
00810 AVIOContext pb;
00811 ffio_init_context(&pb, data, size, 0, NULL, NULL, NULL, NULL);
00812 return ebml_read_num(matroska, &pb, FFMIN(size, 8), num);
00813 }
00814
00815
00816
00817
00818 static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska,
00819 uint8_t *data, uint32_t size, int64_t *num)
00820 {
00821 uint64_t unum;
00822 int res;
00823
00824
00825 if ((res = matroska_ebmlnum_uint(matroska, data, size, &unum)) < 0)
00826 return res;
00827
00828
00829 *num = unum - ((1LL << (7*res - 1)) - 1);
00830
00831 return res;
00832 }
00833
00834 static int ebml_parse_elem(MatroskaDemuxContext *matroska,
00835 EbmlSyntax *syntax, void *data);
00836
00837 static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
00838 uint32_t id, void *data)
00839 {
00840 int i;
00841 for (i=0; syntax[i].id; i++)
00842 if (id == syntax[i].id)
00843 break;
00844 if (!syntax[i].id && id == MATROSKA_ID_CLUSTER &&
00845 matroska->num_levels > 0 &&
00846 matroska->levels[matroska->num_levels-1].length == 0xffffffffffffff)
00847 return 0;
00848 if (!syntax[i].id && id != EBML_ID_VOID && id != EBML_ID_CRC32) {
00849 av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id);
00850 if (matroska->ctx->error_recognition & AV_EF_EXPLODE)
00851 return AVERROR_INVALIDDATA;
00852 }
00853 return ebml_parse_elem(matroska, &syntax[i], data);
00854 }
00855
00856 static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
00857 void *data)
00858 {
00859 if (!matroska->current_id) {
00860 uint64_t id;
00861 int res = ebml_read_num(matroska, matroska->ctx->pb, 4, &id);
00862 if (res < 0)
00863 return res;
00864 matroska->current_id = id | 1 << 7*res;
00865 }
00866 return ebml_parse_id(matroska, syntax, matroska->current_id, data);
00867 }
00868
00869 static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
00870 void *data)
00871 {
00872 int i, res = 0;
00873
00874 for (i=0; syntax[i].id; i++)
00875 switch (syntax[i].type) {
00876 case EBML_UINT:
00877 *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u;
00878 break;
00879 case EBML_FLOAT:
00880 *(double *)((char *)data+syntax[i].data_offset) = syntax[i].def.f;
00881 break;
00882 case EBML_STR:
00883 case EBML_UTF8:
00884 *(char **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s);
00885 break;
00886 }
00887
00888 while (!res && !ebml_level_end(matroska))
00889 res = ebml_parse(matroska, syntax, data);
00890
00891 return res;
00892 }
00893
00894 static int ebml_parse_elem(MatroskaDemuxContext *matroska,
00895 EbmlSyntax *syntax, void *data)
00896 {
00897 static const uint64_t max_lengths[EBML_TYPE_COUNT] = {
00898 [EBML_UINT] = 8,
00899 [EBML_FLOAT] = 8,
00900
00901 [EBML_STR] = 0x1000000,
00902 [EBML_UTF8] = 0x1000000,
00903
00904 [EBML_BIN] = 0x10000000,
00905
00906 };
00907 AVIOContext *pb = matroska->ctx->pb;
00908 uint32_t id = syntax->id;
00909 uint64_t length;
00910 int res;
00911 void *newelem;
00912
00913 data = (char *)data + syntax->data_offset;
00914 if (syntax->list_elem_size) {
00915 EbmlList *list = data;
00916 newelem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);
00917 if (!newelem)
00918 return AVERROR(ENOMEM);
00919 list->elem = newelem;
00920 data = (char*)list->elem + list->nb_elem*syntax->list_elem_size;
00921 memset(data, 0, syntax->list_elem_size);
00922 list->nb_elem++;
00923 }
00924
00925 if (syntax->type != EBML_PASS && syntax->type != EBML_STOP) {
00926 matroska->current_id = 0;
00927 if ((res = ebml_read_length(matroska, pb, &length)) < 0)
00928 return res;
00929 if (max_lengths[syntax->type] && length > max_lengths[syntax->type]) {
00930 av_log(matroska->ctx, AV_LOG_ERROR,
00931 "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for syntax element %i\n",
00932 length, max_lengths[syntax->type], syntax->type);
00933 return AVERROR_INVALIDDATA;
00934 }
00935 }
00936
00937 switch (syntax->type) {
00938 case EBML_UINT: res = ebml_read_uint (pb, length, data); break;
00939 case EBML_FLOAT: res = ebml_read_float (pb, length, data); break;
00940 case EBML_STR:
00941 case EBML_UTF8: res = ebml_read_ascii (pb, length, data); break;
00942 case EBML_BIN: res = ebml_read_binary(pb, length, data); break;
00943 case EBML_NEST: if ((res=ebml_read_master(matroska, length)) < 0)
00944 return res;
00945 if (id == MATROSKA_ID_SEGMENT)
00946 matroska->segment_start = avio_tell(matroska->ctx->pb);
00947 return ebml_parse_nest(matroska, syntax->def.n, data);
00948 case EBML_PASS: return ebml_parse_id(matroska, syntax->def.n, id, data);
00949 case EBML_STOP: return 1;
00950 default:
00951 if(ffio_limit(pb, length) != length)
00952 return AVERROR(EIO);
00953 return avio_skip(pb,length)<0 ? AVERROR(EIO) : 0;
00954 }
00955 if (res == AVERROR_INVALIDDATA)
00956 av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n");
00957 else if (res == AVERROR(EIO))
00958 av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n");
00959 return res;
00960 }
00961
00962 static void ebml_free(EbmlSyntax *syntax, void *data)
00963 {
00964 int i, j;
00965 for (i=0; syntax[i].id; i++) {
00966 void *data_off = (char *)data + syntax[i].data_offset;
00967 switch (syntax[i].type) {
00968 case EBML_STR:
00969 case EBML_UTF8: av_freep(data_off); break;
00970 case EBML_BIN: av_freep(&((EbmlBin *)data_off)->data); break;
00971 case EBML_NEST:
00972 if (syntax[i].list_elem_size) {
00973 EbmlList *list = data_off;
00974 char *ptr = list->elem;
00975 for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size)
00976 ebml_free(syntax[i].def.n, ptr);
00977 av_free(list->elem);
00978 } else
00979 ebml_free(syntax[i].def.n, data_off);
00980 default: break;
00981 }
00982 }
00983 }
00984
00985
00986
00987
00988
00989 static int matroska_probe(AVProbeData *p)
00990 {
00991 uint64_t total = 0;
00992 int len_mask = 0x80, size = 1, n = 1, i;
00993
00994
00995 if (AV_RB32(p->buf) != EBML_ID_HEADER)
00996 return 0;
00997
00998
00999 total = p->buf[4];
01000 while (size <= 8 && !(total & len_mask)) {
01001 size++;
01002 len_mask >>= 1;
01003 }
01004 if (size > 8)
01005 return 0;
01006 total &= (len_mask - 1);
01007 while (n < size)
01008 total = (total << 8) | p->buf[4 + n++];
01009
01010
01011 if (p->buf_size < 4 + size + total)
01012 return 0;
01013
01014
01015
01016
01017
01018 for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) {
01019 int probelen = strlen(matroska_doctypes[i]);
01020 if (total < probelen)
01021 continue;
01022 for (n = 4+size; n <= 4+size+total-probelen; n++)
01023 if (!memcmp(p->buf+n, matroska_doctypes[i], probelen))
01024 return AVPROBE_SCORE_MAX;
01025 }
01026
01027
01028 return AVPROBE_SCORE_MAX/2;
01029 }
01030
01031 static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska,
01032 int num)
01033 {
01034 MatroskaTrack *tracks = matroska->tracks.elem;
01035 int i;
01036
01037 for (i=0; i < matroska->tracks.nb_elem; i++)
01038 if (tracks[i].num == num)
01039 return &tracks[i];
01040
01041 av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num);
01042 return NULL;
01043 }
01044
01045 static int matroska_decode_buffer(uint8_t** buf, int* buf_size,
01046 MatroskaTrack *track)
01047 {
01048 MatroskaTrackEncoding *encodings = track->encodings.elem;
01049 uint8_t* data = *buf;
01050 int isize = *buf_size;
01051 uint8_t* pkt_data = NULL;
01052 uint8_t av_unused *newpktdata;
01053 int pkt_size = isize;
01054 int result = 0;
01055 int olen;
01056
01057 if (pkt_size >= 10000000U)
01058 return AVERROR_INVALIDDATA;
01059
01060 switch (encodings[0].compression.algo) {
01061 case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP: {
01062 int header_size = encodings[0].compression.settings.size;
01063 uint8_t *header = encodings[0].compression.settings.data;
01064
01065 if (header_size && !header) {
01066 av_log(NULL, AV_LOG_ERROR, "Compression size but no data in headerstrip\n");
01067 return -1;
01068 }
01069
01070 if (!header_size)
01071 return 0;
01072
01073 pkt_size = isize + header_size;
01074 pkt_data = av_malloc(pkt_size);
01075 if (!pkt_data)
01076 return AVERROR(ENOMEM);
01077
01078 memcpy(pkt_data, header, header_size);
01079 memcpy(pkt_data + header_size, data, isize);
01080 break;
01081 }
01082 #if CONFIG_LZO
01083 case MATROSKA_TRACK_ENCODING_COMP_LZO:
01084 do {
01085 olen = pkt_size *= 3;
01086 newpktdata = av_realloc(pkt_data, pkt_size + AV_LZO_OUTPUT_PADDING);
01087 if (!newpktdata) {
01088 result = AVERROR(ENOMEM);
01089 goto failed;
01090 }
01091 pkt_data = newpktdata;
01092 result = av_lzo1x_decode(pkt_data, &olen, data, &isize);
01093 } while (result==AV_LZO_OUTPUT_FULL && pkt_size<10000000);
01094 if (result) {
01095 result = AVERROR_INVALIDDATA;
01096 goto failed;
01097 }
01098 pkt_size -= olen;
01099 break;
01100 #endif
01101 #if CONFIG_ZLIB
01102 case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
01103 z_stream zstream = {0};
01104 if (inflateInit(&zstream) != Z_OK)
01105 return -1;
01106 zstream.next_in = data;
01107 zstream.avail_in = isize;
01108 do {
01109 pkt_size *= 3;
01110 newpktdata = av_realloc(pkt_data, pkt_size);
01111 if (!newpktdata) {
01112 inflateEnd(&zstream);
01113 goto failed;
01114 }
01115 pkt_data = newpktdata;
01116 zstream.avail_out = pkt_size - zstream.total_out;
01117 zstream.next_out = pkt_data + zstream.total_out;
01118 if (pkt_data) {
01119 result = inflate(&zstream, Z_NO_FLUSH);
01120 } else
01121 result = Z_MEM_ERROR;
01122 } while (result==Z_OK && pkt_size<10000000);
01123 pkt_size = zstream.total_out;
01124 inflateEnd(&zstream);
01125 if (result != Z_STREAM_END) {
01126 if (result == Z_MEM_ERROR)
01127 result = AVERROR(ENOMEM);
01128 else
01129 result = AVERROR_INVALIDDATA;
01130 goto failed;
01131 }
01132 break;
01133 }
01134 #endif
01135 #if CONFIG_BZLIB
01136 case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
01137 bz_stream bzstream = {0};
01138 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
01139 return -1;
01140 bzstream.next_in = data;
01141 bzstream.avail_in = isize;
01142 do {
01143 pkt_size *= 3;
01144 newpktdata = av_realloc(pkt_data, pkt_size);
01145 if (!newpktdata) {
01146 BZ2_bzDecompressEnd(&bzstream);
01147 goto failed;
01148 }
01149 pkt_data = newpktdata;
01150 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
01151 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
01152 if (pkt_data) {
01153 result = BZ2_bzDecompress(&bzstream);
01154 } else
01155 result = BZ_MEM_ERROR;
01156 } while (result==BZ_OK && pkt_size<10000000);
01157 pkt_size = bzstream.total_out_lo32;
01158 BZ2_bzDecompressEnd(&bzstream);
01159 if (result != BZ_STREAM_END) {
01160 if (result == BZ_MEM_ERROR)
01161 result = AVERROR(ENOMEM);
01162 else
01163 result = AVERROR_INVALIDDATA;
01164 goto failed;
01165 }
01166 break;
01167 }
01168 #endif
01169 default:
01170 return AVERROR_INVALIDDATA;
01171 }
01172
01173 *buf = pkt_data;
01174 *buf_size = pkt_size;
01175 return 0;
01176 failed:
01177 av_free(pkt_data);
01178 return result;
01179 }
01180
01181 static void matroska_fix_ass_packet(MatroskaDemuxContext *matroska,
01182 AVPacket *pkt, uint64_t display_duration)
01183 {
01184 char *line, *layer, *ptr = pkt->data, *end = ptr+pkt->size;
01185 for (; *ptr!=',' && ptr<end-1; ptr++);
01186 if (*ptr == ',')
01187 ptr++;
01188 layer = ptr;
01189 for (; *ptr!=',' && ptr<end-1; ptr++);
01190 if (*ptr == ',') {
01191 int64_t end_pts = pkt->pts + display_duration;
01192 int sc = matroska->time_scale * pkt->pts / 10000000;
01193 int ec = matroska->time_scale * end_pts / 10000000;
01194 int sh, sm, ss, eh, em, es, len;
01195 sh = sc/360000; sc -= 360000*sh;
01196 sm = sc/ 6000; sc -= 6000*sm;
01197 ss = sc/ 100; sc -= 100*ss;
01198 eh = ec/360000; ec -= 360000*eh;
01199 em = ec/ 6000; ec -= 6000*em;
01200 es = ec/ 100; ec -= 100*es;
01201 *ptr++ = '\0';
01202 len = 50 + end-ptr + FF_INPUT_BUFFER_PADDING_SIZE;
01203 if (!(line = av_malloc(len)))
01204 return;
01205 snprintf(line,len,"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
01206 layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
01207 av_free(pkt->data);
01208 pkt->data = line;
01209 pkt->size = strlen(line);
01210 }
01211 }
01212
01213 static int matroska_merge_packets(AVPacket *out, AVPacket *in)
01214 {
01215 int ret = av_grow_packet(out, in->size);
01216 if (ret < 0)
01217 return ret;
01218
01219 memcpy(out->data + out->size - in->size, in->data, in->size);
01220
01221 av_free_packet(in);
01222 av_free(in);
01223 return 0;
01224 }
01225
01226 static void matroska_convert_tag(AVFormatContext *s, EbmlList *list,
01227 AVDictionary **metadata, char *prefix)
01228 {
01229 MatroskaTag *tags = list->elem;
01230 char key[1024];
01231 int i;
01232
01233 for (i=0; i < list->nb_elem; i++) {
01234 const char *lang= (tags[i].lang && strcmp(tags[i].lang, "und")) ? tags[i].lang : NULL;
01235
01236 if (!tags[i].name) {
01237 av_log(s, AV_LOG_WARNING, "Skipping invalid tag with no TagName.\n");
01238 continue;
01239 }
01240 if (prefix) snprintf(key, sizeof(key), "%s/%s", prefix, tags[i].name);
01241 else av_strlcpy(key, tags[i].name, sizeof(key));
01242 if (tags[i].def || !lang) {
01243 av_dict_set(metadata, key, tags[i].string, 0);
01244 if (tags[i].sub.nb_elem)
01245 matroska_convert_tag(s, &tags[i].sub, metadata, key);
01246 }
01247 if (lang) {
01248 av_strlcat(key, "-", sizeof(key));
01249 av_strlcat(key, lang, sizeof(key));
01250 av_dict_set(metadata, key, tags[i].string, 0);
01251 if (tags[i].sub.nb_elem)
01252 matroska_convert_tag(s, &tags[i].sub, metadata, key);
01253 }
01254 }
01255 ff_metadata_conv(metadata, NULL, ff_mkv_metadata_conv);
01256 }
01257
01258 static void matroska_convert_tags(AVFormatContext *s)
01259 {
01260 MatroskaDemuxContext *matroska = s->priv_data;
01261 MatroskaTags *tags = matroska->tags.elem;
01262 int i, j;
01263
01264 for (i=0; i < matroska->tags.nb_elem; i++) {
01265 if (tags[i].target.attachuid) {
01266 MatroskaAttachement *attachment = matroska->attachments.elem;
01267 for (j=0; j<matroska->attachments.nb_elem; j++)
01268 if (attachment[j].uid == tags[i].target.attachuid
01269 && attachment[j].stream)
01270 matroska_convert_tag(s, &tags[i].tag,
01271 &attachment[j].stream->metadata, NULL);
01272 } else if (tags[i].target.chapteruid) {
01273 MatroskaChapter *chapter = matroska->chapters.elem;
01274 for (j=0; j<matroska->chapters.nb_elem; j++)
01275 if (chapter[j].uid == tags[i].target.chapteruid
01276 && chapter[j].chapter)
01277 matroska_convert_tag(s, &tags[i].tag,
01278 &chapter[j].chapter->metadata, NULL);
01279 } else if (tags[i].target.trackuid) {
01280 MatroskaTrack *track = matroska->tracks.elem;
01281 for (j=0; j<matroska->tracks.nb_elem; j++)
01282 if (track[j].uid == tags[i].target.trackuid && track[j].stream)
01283 matroska_convert_tag(s, &tags[i].tag,
01284 &track[j].stream->metadata, NULL);
01285 } else {
01286 matroska_convert_tag(s, &tags[i].tag, &s->metadata,
01287 tags[i].target.type);
01288 }
01289 }
01290 }
01291
01292 static int matroska_parse_seekhead_entry(MatroskaDemuxContext *matroska, int idx)
01293 {
01294 EbmlList *seekhead_list = &matroska->seekhead;
01295 MatroskaSeekhead *seekhead = seekhead_list->elem;
01296 uint32_t level_up = matroska->level_up;
01297 int64_t before_pos = avio_tell(matroska->ctx->pb);
01298 uint32_t saved_id = matroska->current_id;
01299 MatroskaLevel level;
01300 int64_t offset;
01301 int ret = 0;
01302
01303 if (idx >= seekhead_list->nb_elem
01304 || seekhead[idx].id == MATROSKA_ID_SEEKHEAD
01305 || seekhead[idx].id == MATROSKA_ID_CLUSTER)
01306 return 0;
01307
01308
01309 offset = seekhead[idx].pos + matroska->segment_start;
01310 if (avio_seek(matroska->ctx->pb, offset, SEEK_SET) == offset) {
01311
01312
01313 if (matroska->num_levels == EBML_MAX_DEPTH) {
01314 av_log(matroska->ctx, AV_LOG_INFO,
01315 "Max EBML element depth (%d) reached, "
01316 "cannot parse further.\n", EBML_MAX_DEPTH);
01317 ret = AVERROR_INVALIDDATA;
01318 } else {
01319 level.start = 0;
01320 level.length = (uint64_t)-1;
01321 matroska->levels[matroska->num_levels] = level;
01322 matroska->num_levels++;
01323 matroska->current_id = 0;
01324
01325 ret = ebml_parse(matroska, matroska_segment, matroska);
01326
01327
01328 while (matroska->num_levels) {
01329 uint64_t length = matroska->levels[--matroska->num_levels].length;
01330 if (length == (uint64_t)-1)
01331 break;
01332 }
01333 }
01334 }
01335
01336 avio_seek(matroska->ctx->pb, before_pos, SEEK_SET);
01337 matroska->level_up = level_up;
01338 matroska->current_id = saved_id;
01339
01340 return ret;
01341 }
01342
01343 static void matroska_execute_seekhead(MatroskaDemuxContext *matroska)
01344 {
01345 EbmlList *seekhead_list = &matroska->seekhead;
01346 int64_t before_pos = avio_tell(matroska->ctx->pb);
01347 int i;
01348
01349
01350 if (!matroska->ctx->pb->seekable ||
01351 (matroska->ctx->flags & AVFMT_FLAG_IGNIDX))
01352 return;
01353
01354 for (i = 0; i < seekhead_list->nb_elem; i++) {
01355 MatroskaSeekhead *seekhead = seekhead_list->elem;
01356 if (seekhead[i].pos <= before_pos)
01357 continue;
01358
01359
01360 if (seekhead[i].id == MATROSKA_ID_CUES) {
01361 matroska->cues_parsing_deferred = 1;
01362 continue;
01363 }
01364
01365 if (matroska_parse_seekhead_entry(matroska, i) < 0) {
01366
01367 matroska->cues_parsing_deferred = -1;
01368 break;
01369 }
01370 }
01371 }
01372
01373 static void matroska_add_index_entries(MatroskaDemuxContext *matroska) {
01374 EbmlList *index_list;
01375 MatroskaIndex *index;
01376 int index_scale = 1;
01377 int i, j;
01378
01379 index_list = &matroska->index;
01380 index = index_list->elem;
01381 if (index_list->nb_elem
01382 && index[0].time > 1E14/matroska->time_scale) {
01383 av_log(matroska->ctx, AV_LOG_WARNING, "Working around broken index.\n");
01384 index_scale = matroska->time_scale;
01385 }
01386 for (i = 0; i < index_list->nb_elem; i++) {
01387 EbmlList *pos_list = &index[i].pos;
01388 MatroskaIndexPos *pos = pos_list->elem;
01389 for (j = 0; j < pos_list->nb_elem; j++) {
01390 MatroskaTrack *track = matroska_find_track_by_num(matroska, pos[j].track);
01391 if (track && track->stream)
01392 av_add_index_entry(track->stream,
01393 pos[j].pos + matroska->segment_start,
01394 index[i].time/index_scale, 0, 0,
01395 AVINDEX_KEYFRAME);
01396 }
01397 }
01398 }
01399
01400 static void matroska_parse_cues(MatroskaDemuxContext *matroska) {
01401 EbmlList *seekhead_list = &matroska->seekhead;
01402 MatroskaSeekhead *seekhead = seekhead_list->elem;
01403 int i;
01404
01405 for (i = 0; i < seekhead_list->nb_elem; i++)
01406 if (seekhead[i].id == MATROSKA_ID_CUES)
01407 break;
01408 assert(i <= seekhead_list->nb_elem);
01409
01410 if (matroska_parse_seekhead_entry(matroska, i) < 0)
01411 matroska->cues_parsing_deferred = -1;
01412 matroska_add_index_entries(matroska);
01413 }
01414
01415 static int matroska_aac_profile(char *codec_id)
01416 {
01417 static const char * const aac_profiles[] = { "MAIN", "LC", "SSR" };
01418 int profile;
01419
01420 for (profile=0; profile<FF_ARRAY_ELEMS(aac_profiles); profile++)
01421 if (strstr(codec_id, aac_profiles[profile]))
01422 break;
01423 return profile + 1;
01424 }
01425
01426 static int matroska_aac_sri(int samplerate)
01427 {
01428 int sri;
01429
01430 for (sri=0; sri<FF_ARRAY_ELEMS(avpriv_mpeg4audio_sample_rates); sri++)
01431 if (avpriv_mpeg4audio_sample_rates[sri] == samplerate)
01432 break;
01433 return sri;
01434 }
01435
01436 static void matroska_metadata_creation_time(AVDictionary **metadata, int64_t date_utc)
01437 {
01438 char buffer[32];
01439
01440 time_t creation_time = date_utc / 1000000000 + 978307200;
01441 struct tm *ptm = gmtime(&creation_time);
01442 if (!ptm) return;
01443 strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
01444 av_dict_set(metadata, "creation_time", buffer, 0);
01445 }
01446
01447 static int matroska_read_header(AVFormatContext *s)
01448 {
01449 MatroskaDemuxContext *matroska = s->priv_data;
01450 EbmlList *attachements_list = &matroska->attachments;
01451 MatroskaAttachement *attachements;
01452 EbmlList *chapters_list = &matroska->chapters;
01453 MatroskaChapter *chapters;
01454 MatroskaTrack *tracks;
01455 uint64_t max_start = 0;
01456 int64_t pos;
01457 Ebml ebml = { 0 };
01458 AVStream *st;
01459 int i, j, k, res;
01460
01461 matroska->ctx = s;
01462
01463
01464 if (ebml_parse(matroska, ebml_syntax, &ebml)
01465 || ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t)
01466 || ebml.id_length > sizeof(uint32_t) || ebml.doctype_version > 3 || !ebml.doctype) {
01467 av_log(matroska->ctx, AV_LOG_ERROR,
01468 "EBML header using unsupported features\n"
01469 "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
01470 ebml.version, ebml.doctype, ebml.doctype_version);
01471 ebml_free(ebml_syntax, &ebml);
01472 return AVERROR_PATCHWELCOME;
01473 } else if (ebml.doctype_version == 3) {
01474 av_log(matroska->ctx, AV_LOG_WARNING,
01475 "EBML header using unsupported features\n"
01476 "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
01477 ebml.version, ebml.doctype, ebml.doctype_version);
01478 }
01479 for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++)
01480 if (!strcmp(ebml.doctype, matroska_doctypes[i]))
01481 break;
01482 if (i >= FF_ARRAY_ELEMS(matroska_doctypes)) {
01483 av_log(s, AV_LOG_WARNING, "Unknown EBML doctype '%s'\n", ebml.doctype);
01484 if (matroska->ctx->error_recognition & AV_EF_EXPLODE) {
01485 ebml_free(ebml_syntax, &ebml);
01486 return AVERROR_INVALIDDATA;
01487 }
01488 }
01489 ebml_free(ebml_syntax, &ebml);
01490
01491
01492 pos = avio_tell(matroska->ctx->pb);
01493 res = ebml_parse(matroska, matroska_segments, matroska);
01494
01495 while (res != 1) {
01496 res = matroska_resync(matroska, pos);
01497 if (res < 0)
01498 return res;
01499 pos = avio_tell(matroska->ctx->pb);
01500 res = ebml_parse(matroska, matroska_segment, matroska);
01501 }
01502 matroska_execute_seekhead(matroska);
01503
01504 if (!matroska->time_scale)
01505 matroska->time_scale = 1000000;
01506 if (matroska->duration)
01507 matroska->ctx->duration = matroska->duration * matroska->time_scale
01508 * 1000 / AV_TIME_BASE;
01509 av_dict_set(&s->metadata, "title", matroska->title, 0);
01510
01511 if (matroska->date_utc.size == 8)
01512 matroska_metadata_creation_time(&s->metadata, AV_RB64(matroska->date_utc.data));
01513
01514 tracks = matroska->tracks.elem;
01515 for (i=0; i < matroska->tracks.nb_elem; i++) {
01516 MatroskaTrack *track = &tracks[i];
01517 enum AVCodecID codec_id = AV_CODEC_ID_NONE;
01518 EbmlList *encodings_list = &track->encodings;
01519 MatroskaTrackEncoding *encodings = encodings_list->elem;
01520 uint8_t *extradata = NULL;
01521 int extradata_size = 0;
01522 int extradata_offset = 0;
01523 uint32_t fourcc = 0;
01524 AVIOContext b;
01525
01526
01527 if (track->type != MATROSKA_TRACK_TYPE_VIDEO &&
01528 track->type != MATROSKA_TRACK_TYPE_AUDIO &&
01529 track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
01530 av_log(matroska->ctx, AV_LOG_INFO,
01531 "Unknown or unsupported track type %"PRIu64"\n",
01532 track->type);
01533 continue;
01534 }
01535 if (track->codec_id == NULL)
01536 continue;
01537
01538 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
01539 if (!track->default_duration && track->video.frame_rate > 0)
01540 track->default_duration = 1000000000/track->video.frame_rate;
01541 if (!track->video.display_width)
01542 track->video.display_width = track->video.pixel_width;
01543 if (!track->video.display_height)
01544 track->video.display_height = track->video.pixel_height;
01545 if (track->video.color_space.size == 4)
01546 fourcc = AV_RL32(track->video.color_space.data);
01547 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
01548 if (!track->audio.out_samplerate)
01549 track->audio.out_samplerate = track->audio.samplerate;
01550 }
01551 if (encodings_list->nb_elem > 1) {
01552 av_log(matroska->ctx, AV_LOG_ERROR,
01553 "Multiple combined encodings not supported");
01554 } else if (encodings_list->nb_elem == 1) {
01555 if (encodings[0].type ||
01556 (
01557 #if CONFIG_ZLIB
01558 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
01559 #endif
01560 #if CONFIG_BZLIB
01561 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
01562 #endif
01563 #if CONFIG_LZO
01564 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO &&
01565 #endif
01566 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP)) {
01567 encodings[0].scope = 0;
01568 av_log(matroska->ctx, AV_LOG_ERROR,
01569 "Unsupported encoding type");
01570 } else if (track->codec_priv.size && encodings[0].scope&2) {
01571 uint8_t *codec_priv = track->codec_priv.data;
01572 int ret = matroska_decode_buffer(&track->codec_priv.data,
01573 &track->codec_priv.size,
01574 track);
01575 if (ret < 0) {
01576 track->codec_priv.data = NULL;
01577 track->codec_priv.size = 0;
01578 av_log(matroska->ctx, AV_LOG_ERROR,
01579 "Failed to decode codec private data\n");
01580 }
01581
01582 if (codec_priv != track->codec_priv.data)
01583 av_free(codec_priv);
01584 }
01585 }
01586
01587 for(j=0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++){
01588 if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
01589 strlen(ff_mkv_codec_tags[j].str))){
01590 codec_id= ff_mkv_codec_tags[j].id;
01591 break;
01592 }
01593 }
01594
01595 st = track->stream = avformat_new_stream(s, NULL);
01596 if (st == NULL)
01597 return AVERROR(ENOMEM);
01598
01599 if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC")
01600 && track->codec_priv.size >= 40
01601 && track->codec_priv.data != NULL) {
01602 track->ms_compat = 1;
01603 fourcc = AV_RL32(track->codec_priv.data + 16);
01604 codec_id = ff_codec_get_id(ff_codec_bmp_tags, fourcc);
01605 extradata_offset = 40;
01606 } else if (!strcmp(track->codec_id, "A_MS/ACM")
01607 && track->codec_priv.size >= 14
01608 && track->codec_priv.data != NULL) {
01609 int ret;
01610 ffio_init_context(&b, track->codec_priv.data, track->codec_priv.size,
01611 AVIO_FLAG_READ, NULL, NULL, NULL, NULL);
01612 ret = ff_get_wav_header(&b, st->codec, track->codec_priv.size);
01613 if (ret < 0)
01614 return ret;
01615 codec_id = st->codec->codec_id;
01616 extradata_offset = FFMIN(track->codec_priv.size, 18);
01617 } else if (!strcmp(track->codec_id, "V_QUICKTIME")
01618 && (track->codec_priv.size >= 86)
01619 && (track->codec_priv.data != NULL)) {
01620 fourcc = AV_RL32(track->codec_priv.data);
01621 codec_id = ff_codec_get_id(ff_codec_movvideo_tags, fourcc);
01622 } else if (codec_id == AV_CODEC_ID_ALAC && track->codec_priv.size && track->codec_priv.size < INT_MAX-12) {
01623
01624
01625
01626 extradata_size = 12 + track->codec_priv.size;
01627 extradata = av_mallocz(extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
01628 if (extradata == NULL)
01629 return AVERROR(ENOMEM);
01630 AV_WB32(extradata, extradata_size);
01631 memcpy(&extradata[4], "alac", 4);
01632 AV_WB32(&extradata[8], 0);
01633 memcpy(&extradata[12], track->codec_priv.data, track->codec_priv.size);
01634 } else if (codec_id == AV_CODEC_ID_PCM_S16BE) {
01635 switch (track->audio.bitdepth) {
01636 case 8: codec_id = AV_CODEC_ID_PCM_U8; break;
01637 case 24: codec_id = AV_CODEC_ID_PCM_S24BE; break;
01638 case 32: codec_id = AV_CODEC_ID_PCM_S32BE; break;
01639 }
01640 } else if (codec_id == AV_CODEC_ID_PCM_S16LE) {
01641 switch (track->audio.bitdepth) {
01642 case 8: codec_id = AV_CODEC_ID_PCM_U8; break;
01643 case 24: codec_id = AV_CODEC_ID_PCM_S24LE; break;
01644 case 32: codec_id = AV_CODEC_ID_PCM_S32LE; break;
01645 }
01646 } else if (codec_id==AV_CODEC_ID_PCM_F32LE && track->audio.bitdepth==64) {
01647 codec_id = AV_CODEC_ID_PCM_F64LE;
01648 } else if (codec_id == AV_CODEC_ID_AAC && !track->codec_priv.size) {
01649 int profile = matroska_aac_profile(track->codec_id);
01650 int sri = matroska_aac_sri(track->audio.samplerate);
01651 extradata = av_mallocz(5 + FF_INPUT_BUFFER_PADDING_SIZE);
01652 if (extradata == NULL)
01653 return AVERROR(ENOMEM);
01654 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
01655 extradata[1] = ((sri&0x01) << 7) | (track->audio.channels<<3);
01656 if (strstr(track->codec_id, "SBR")) {
01657 sri = matroska_aac_sri(track->audio.out_samplerate);
01658 extradata[2] = 0x56;
01659 extradata[3] = 0xE5;
01660 extradata[4] = 0x80 | (sri<<3);
01661 extradata_size = 5;
01662 } else
01663 extradata_size = 2;
01664 } else if (codec_id == AV_CODEC_ID_TTA) {
01665 extradata_size = 30;
01666 extradata = av_mallocz(extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
01667 if (extradata == NULL)
01668 return AVERROR(ENOMEM);
01669 ffio_init_context(&b, extradata, extradata_size, 1,
01670 NULL, NULL, NULL, NULL);
01671 avio_write(&b, "TTA1", 4);
01672 avio_wl16(&b, 1);
01673 avio_wl16(&b, track->audio.channels);
01674 avio_wl16(&b, track->audio.bitdepth);
01675 avio_wl32(&b, track->audio.out_samplerate);
01676 avio_wl32(&b, matroska->ctx->duration * track->audio.out_samplerate);
01677 } else if (codec_id == AV_CODEC_ID_RV10 || codec_id == AV_CODEC_ID_RV20 ||
01678 codec_id == AV_CODEC_ID_RV30 || codec_id == AV_CODEC_ID_RV40) {
01679 extradata_offset = 26;
01680 } else if (codec_id == AV_CODEC_ID_RA_144) {
01681 track->audio.out_samplerate = 8000;
01682 track->audio.channels = 1;
01683 } else if ((codec_id == AV_CODEC_ID_RA_288 || codec_id == AV_CODEC_ID_COOK ||
01684 codec_id == AV_CODEC_ID_ATRAC3 || codec_id == AV_CODEC_ID_SIPR)
01685 && track->codec_priv.data) {
01686 int flavor;
01687
01688 ffio_init_context(&b, track->codec_priv.data,track->codec_priv.size,
01689 0, NULL, NULL, NULL, NULL);
01690 avio_skip(&b, 22);
01691 flavor = avio_rb16(&b);
01692 track->audio.coded_framesize = avio_rb32(&b);
01693 avio_skip(&b, 12);
01694 track->audio.sub_packet_h = avio_rb16(&b);
01695 track->audio.frame_size = avio_rb16(&b);
01696 track->audio.sub_packet_size = avio_rb16(&b);
01697 track->audio.buf = av_malloc(track->audio.frame_size * track->audio.sub_packet_h);
01698 if (codec_id == AV_CODEC_ID_RA_288) {
01699 st->codec->block_align = track->audio.coded_framesize;
01700 track->codec_priv.size = 0;
01701 } else {
01702 if (codec_id == AV_CODEC_ID_SIPR && flavor < 4) {
01703 const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
01704 track->audio.sub_packet_size = ff_sipr_subpk_size[flavor];
01705 st->codec->bit_rate = sipr_bit_rate[flavor];
01706 }
01707 st->codec->block_align = track->audio.sub_packet_size;
01708 extradata_offset = 78;
01709 }
01710 }
01711 track->codec_priv.size -= extradata_offset;
01712
01713 if (codec_id == AV_CODEC_ID_NONE)
01714 av_log(matroska->ctx, AV_LOG_INFO,
01715 "Unknown/unsupported AVCodecID %s.\n", track->codec_id);
01716
01717 if (track->time_scale < 0.01)
01718 track->time_scale = 1.0;
01719 avpriv_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000);
01720
01721 st->codec->codec_id = codec_id;
01722 st->start_time = 0;
01723 if (strcmp(track->language, "und"))
01724 av_dict_set(&st->metadata, "language", track->language, 0);
01725 av_dict_set(&st->metadata, "title", track->name, 0);
01726
01727 if (track->flag_default)
01728 st->disposition |= AV_DISPOSITION_DEFAULT;
01729 if (track->flag_forced)
01730 st->disposition |= AV_DISPOSITION_FORCED;
01731
01732 if (!st->codec->extradata) {
01733 if(extradata){
01734 st->codec->extradata = extradata;
01735 st->codec->extradata_size = extradata_size;
01736 } else if(track->codec_priv.data && track->codec_priv.size > 0){
01737 st->codec->extradata = av_mallocz(track->codec_priv.size +
01738 FF_INPUT_BUFFER_PADDING_SIZE);
01739 if(st->codec->extradata == NULL)
01740 return AVERROR(ENOMEM);
01741 st->codec->extradata_size = track->codec_priv.size;
01742 memcpy(st->codec->extradata,
01743 track->codec_priv.data + extradata_offset,
01744 track->codec_priv.size);
01745 }
01746 }
01747
01748 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
01749 MatroskaTrackPlane *planes = track->operation.combine_planes.elem;
01750
01751 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01752 st->codec->codec_tag = fourcc;
01753 st->codec->width = track->video.pixel_width;
01754 st->codec->height = track->video.pixel_height;
01755 av_reduce(&st->sample_aspect_ratio.num,
01756 &st->sample_aspect_ratio.den,
01757 st->codec->height * track->video.display_width,
01758 st->codec-> width * track->video.display_height,
01759 255);
01760 st->need_parsing = AVSTREAM_PARSE_HEADERS;
01761 if (track->default_duration) {
01762 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
01763 1000000000, track->default_duration, 30000);
01764 #if FF_API_R_FRAME_RATE
01765 st->r_frame_rate = st->avg_frame_rate;
01766 #endif
01767 }
01768
01769
01770 if (track->video.stereo_mode && track->video.stereo_mode < MATROSKA_VIDEO_STEREO_MODE_COUNT)
01771 av_dict_set(&st->metadata, "stereo_mode", ff_matroska_video_stereo_mode[track->video.stereo_mode], 0);
01772
01773
01774 for (j=0; j < track->operation.combine_planes.nb_elem; j++) {
01775 char buf[32];
01776 if (planes[j].type >= MATROSKA_VIDEO_STEREO_PLANE_COUNT)
01777 continue;
01778 snprintf(buf, sizeof(buf), "%s_%d",
01779 ff_matroska_video_stereo_plane[planes[j].type], i);
01780 for (k=0; k < matroska->tracks.nb_elem; k++)
01781 if (planes[j].uid == tracks[k].uid) {
01782 av_dict_set(&s->streams[k]->metadata,
01783 "stereo_mode", buf, 0);
01784 break;
01785 }
01786 }
01787 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
01788 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
01789 st->codec->sample_rate = track->audio.out_samplerate;
01790 st->codec->channels = track->audio.channels;
01791 if (st->codec->codec_id != AV_CODEC_ID_AAC)
01792 st->need_parsing = AVSTREAM_PARSE_HEADERS;
01793 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
01794 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
01795 if (st->codec->codec_id == AV_CODEC_ID_SSA)
01796 matroska->contains_ssa = 1;
01797 }
01798 }
01799
01800 attachements = attachements_list->elem;
01801 for (j=0; j<attachements_list->nb_elem; j++) {
01802 if (!(attachements[j].filename && attachements[j].mime &&
01803 attachements[j].bin.data && attachements[j].bin.size > 0)) {
01804 av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
01805 } else {
01806 AVStream *st = avformat_new_stream(s, NULL);
01807 if (st == NULL)
01808 break;
01809 av_dict_set(&st->metadata, "filename",attachements[j].filename, 0);
01810 av_dict_set(&st->metadata, "mimetype", attachements[j].mime, 0);
01811 st->codec->codec_id = AV_CODEC_ID_NONE;
01812 st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
01813 st->codec->extradata = av_malloc(attachements[j].bin.size + FF_INPUT_BUFFER_PADDING_SIZE);
01814 if(st->codec->extradata == NULL)
01815 break;
01816 st->codec->extradata_size = attachements[j].bin.size;
01817 memcpy(st->codec->extradata, attachements[j].bin.data, attachements[j].bin.size);
01818
01819 for (i=0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++) {
01820 if (!strncmp(ff_mkv_mime_tags[i].str, attachements[j].mime,
01821 strlen(ff_mkv_mime_tags[i].str))) {
01822 st->codec->codec_id = ff_mkv_mime_tags[i].id;
01823 break;
01824 }
01825 }
01826 attachements[j].stream = st;
01827 }
01828 }
01829
01830 chapters = chapters_list->elem;
01831 for (i=0; i<chapters_list->nb_elem; i++)
01832 if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid
01833 && (max_start==0 || chapters[i].start > max_start)) {
01834 chapters[i].chapter =
01835 avpriv_new_chapter(s, chapters[i].uid, (AVRational){1, 1000000000},
01836 chapters[i].start, chapters[i].end,
01837 chapters[i].title);
01838 av_dict_set(&chapters[i].chapter->metadata,
01839 "title", chapters[i].title, 0);
01840 max_start = chapters[i].start;
01841 }
01842
01843 matroska_add_index_entries(matroska);
01844
01845 matroska_convert_tags(s);
01846
01847 return 0;
01848 }
01849
01850
01851
01852
01853
01854 static int matroska_deliver_packet(MatroskaDemuxContext *matroska,
01855 AVPacket *pkt)
01856 {
01857 if (matroska->num_packets > 0) {
01858 memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
01859 av_free(matroska->packets[0]);
01860 if (matroska->num_packets > 1) {
01861 void *newpackets;
01862 memmove(&matroska->packets[0], &matroska->packets[1],
01863 (matroska->num_packets - 1) * sizeof(AVPacket *));
01864 newpackets = av_realloc(matroska->packets,
01865 (matroska->num_packets - 1) * sizeof(AVPacket *));
01866 if (newpackets)
01867 matroska->packets = newpackets;
01868 } else {
01869 av_freep(&matroska->packets);
01870 matroska->prev_pkt = NULL;
01871 }
01872 matroska->num_packets--;
01873 return 0;
01874 }
01875
01876 return -1;
01877 }
01878
01879
01880
01881
01882 static void matroska_clear_queue(MatroskaDemuxContext *matroska)
01883 {
01884 if (matroska->packets) {
01885 int n;
01886 for (n = 0; n < matroska->num_packets; n++) {
01887 av_free_packet(matroska->packets[n]);
01888 av_free(matroska->packets[n]);
01889 }
01890 av_freep(&matroska->packets);
01891 matroska->num_packets = 0;
01892 }
01893 }
01894
01895 static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf,
01896 int size, int type,
01897 uint32_t **lace_buf, int *laces)
01898 {
01899 int res = 0, n;
01900 uint8_t *data = *buf;
01901 uint32_t *lace_size;
01902
01903 if (!type) {
01904 *laces = 1;
01905 *lace_buf = av_mallocz(sizeof(int));
01906 if (!*lace_buf)
01907 return AVERROR(ENOMEM);
01908
01909 *lace_buf[0] = size;
01910 return 0;
01911 }
01912
01913 av_assert0(size > 0);
01914 *laces = *data + 1;
01915 data += 1;
01916 size -= 1;
01917 lace_size = av_mallocz(*laces * sizeof(int));
01918 if (!lace_size)
01919 return AVERROR(ENOMEM);
01920
01921 switch (type) {
01922 case 0x1: {
01923 uint8_t temp;
01924 uint32_t total = 0;
01925 for (n = 0; res == 0 && n < *laces - 1; n++) {
01926 while (1) {
01927 if (size == 0) {
01928 res = AVERROR_EOF;
01929 break;
01930 }
01931 temp = *data;
01932 lace_size[n] += temp;
01933 data += 1;
01934 size -= 1;
01935 if (temp != 0xff)
01936 break;
01937 }
01938 total += lace_size[n];
01939 }
01940 if (size <= total) {
01941 res = AVERROR_INVALIDDATA;
01942 break;
01943 }
01944
01945 lace_size[n] = size - total;
01946 break;
01947 }
01948
01949 case 0x2:
01950 if (size % (*laces)) {
01951 res = AVERROR_INVALIDDATA;
01952 break;
01953 }
01954 for (n = 0; n < *laces; n++)
01955 lace_size[n] = size / *laces;
01956 break;
01957
01958 case 0x3: {
01959 uint64_t num;
01960 uint32_t total;
01961 n = matroska_ebmlnum_uint(matroska, data, size, &num);
01962 if (n < 0) {
01963 av_log(matroska->ctx, AV_LOG_INFO,
01964 "EBML block data error\n");
01965 res = n;
01966 break;
01967 }
01968 data += n;
01969 size -= n;
01970 total = lace_size[0] = num;
01971 for (n = 1; res == 0 && n < *laces - 1; n++) {
01972 int64_t snum;
01973 int r;
01974 r = matroska_ebmlnum_sint(matroska, data, size, &snum);
01975 if (r < 0) {
01976 av_log(matroska->ctx, AV_LOG_INFO,
01977 "EBML block data error\n");
01978 res = r;
01979 break;
01980 }
01981 data += r;
01982 size -= r;
01983 lace_size[n] = lace_size[n - 1] + snum;
01984 total += lace_size[n];
01985 }
01986 if (size <= total) {
01987 res = AVERROR_INVALIDDATA;
01988 break;
01989 }
01990 lace_size[*laces - 1] = size - total;
01991 break;
01992 }
01993 }
01994
01995 *buf = data;
01996 *lace_buf = lace_size;
01997
01998 return res;
01999 }
02000
02001 static int matroska_parse_rm_audio(MatroskaDemuxContext *matroska,
02002 MatroskaTrack *track,
02003 AVStream *st,
02004 uint8_t *data, int size,
02005 uint64_t timecode,
02006 int64_t pos)
02007 {
02008 int a = st->codec->block_align;
02009 int sps = track->audio.sub_packet_size;
02010 int cfs = track->audio.coded_framesize;
02011 int h = track->audio.sub_packet_h;
02012 int y = track->audio.sub_packet_cnt;
02013 int w = track->audio.frame_size;
02014 int x;
02015
02016 if (!track->audio.pkt_cnt) {
02017 if (track->audio.sub_packet_cnt == 0)
02018 track->audio.buf_timecode = timecode;
02019 if (st->codec->codec_id == AV_CODEC_ID_RA_288) {
02020 if (size < cfs * h / 2) {
02021 av_log(matroska->ctx, AV_LOG_ERROR,
02022 "Corrupt int4 RM-style audio packet size\n");
02023 return AVERROR_INVALIDDATA;
02024 }
02025 for (x=0; x<h/2; x++)
02026 memcpy(track->audio.buf+x*2*w+y*cfs,
02027 data+x*cfs, cfs);
02028 } else if (st->codec->codec_id == AV_CODEC_ID_SIPR) {
02029 if (size < w) {
02030 av_log(matroska->ctx, AV_LOG_ERROR,
02031 "Corrupt sipr RM-style audio packet size\n");
02032 return AVERROR_INVALIDDATA;
02033 }
02034 memcpy(track->audio.buf + y*w, data, w);
02035 } else {
02036 if (size < sps * w / sps || h<=0) {
02037 av_log(matroska->ctx, AV_LOG_ERROR,
02038 "Corrupt generic RM-style audio packet size\n");
02039 return AVERROR_INVALIDDATA;
02040 }
02041 for (x=0; x<w/sps; x++)
02042 memcpy(track->audio.buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
02043 }
02044
02045 if (++track->audio.sub_packet_cnt >= h) {
02046 if (st->codec->codec_id == AV_CODEC_ID_SIPR)
02047 ff_rm_reorder_sipr_data(track->audio.buf, h, w);
02048 track->audio.sub_packet_cnt = 0;
02049 track->audio.pkt_cnt = h*w / a;
02050 }
02051 }
02052
02053 while (track->audio.pkt_cnt) {
02054 AVPacket *pkt = NULL;
02055 if (!(pkt = av_mallocz(sizeof(AVPacket))) || av_new_packet(pkt, a) < 0){
02056 av_free(pkt);
02057 return AVERROR(ENOMEM);
02058 }
02059 memcpy(pkt->data, track->audio.buf
02060 + a * (h*w / a - track->audio.pkt_cnt--), a);
02061 pkt->pts = track->audio.buf_timecode;
02062 track->audio.buf_timecode = AV_NOPTS_VALUE;
02063 pkt->pos = pos;
02064 pkt->stream_index = st->index;
02065 dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
02066 }
02067
02068 return 0;
02069 }
02070 static int matroska_parse_frame(MatroskaDemuxContext *matroska,
02071 MatroskaTrack *track,
02072 AVStream *st,
02073 uint8_t *data, int pkt_size,
02074 uint64_t timecode, uint64_t lace_duration,
02075 int64_t pos, int is_keyframe)
02076 {
02077 MatroskaTrackEncoding *encodings = track->encodings.elem;
02078 uint8_t *pkt_data = data;
02079 int offset = 0, res;
02080 AVPacket *pkt;
02081
02082 if (encodings && encodings->scope & 1) {
02083 res = matroska_decode_buffer(&pkt_data, &pkt_size, track);
02084 if (res < 0)
02085 return res;
02086 }
02087
02088 if (st->codec->codec_id == AV_CODEC_ID_PRORES)
02089 offset = 8;
02090
02091 pkt = av_mallocz(sizeof(AVPacket));
02092
02093 if (av_new_packet(pkt, pkt_size + offset) < 0) {
02094 av_free(pkt);
02095 return AVERROR(ENOMEM);
02096 }
02097
02098 if (st->codec->codec_id == AV_CODEC_ID_PRORES) {
02099 uint8_t *buf = pkt->data;
02100 bytestream_put_be32(&buf, pkt_size);
02101 bytestream_put_be32(&buf, MKBETAG('i', 'c', 'p', 'f'));
02102 }
02103
02104 memcpy(pkt->data + offset, pkt_data, pkt_size);
02105
02106 if (pkt_data != data)
02107 av_free(pkt_data);
02108
02109 pkt->flags = is_keyframe;
02110 pkt->stream_index = st->index;
02111
02112 if (track->ms_compat)
02113 pkt->dts = timecode;
02114 else
02115 pkt->pts = timecode;
02116 pkt->pos = pos;
02117 if (st->codec->codec_id == AV_CODEC_ID_SUBRIP) {
02118
02119
02120
02121
02122
02123
02124
02125
02126 pkt->convergence_duration = lace_duration;
02127 }
02128
02129 if (track->type != MATROSKA_TRACK_TYPE_SUBTITLE ||
02130 lace_duration <= INT_MAX) {
02131
02132
02133
02134
02135
02136
02137
02138 pkt->duration = lace_duration;
02139 }
02140
02141 if (st->codec->codec_id == AV_CODEC_ID_SSA)
02142 matroska_fix_ass_packet(matroska, pkt, lace_duration);
02143
02144 if (matroska->prev_pkt &&
02145 timecode != AV_NOPTS_VALUE &&
02146 matroska->prev_pkt->pts == timecode &&
02147 matroska->prev_pkt->stream_index == st->index &&
02148 st->codec->codec_id == AV_CODEC_ID_SSA)
02149 matroska_merge_packets(matroska->prev_pkt, pkt);
02150 else {
02151 dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
02152 matroska->prev_pkt = pkt;
02153 }
02154
02155 return 0;
02156 }
02157
02158 static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data,
02159 int size, int64_t pos, uint64_t cluster_time,
02160 uint64_t block_duration, int is_keyframe,
02161 int64_t cluster_pos)
02162 {
02163 uint64_t timecode = AV_NOPTS_VALUE;
02164 MatroskaTrack *track;
02165 int res = 0;
02166 AVStream *st;
02167 int16_t block_time;
02168 uint32_t *lace_size = NULL;
02169 int n, flags, laces = 0;
02170 uint64_t num;
02171
02172 if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) {
02173 av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
02174 return n;
02175 }
02176 data += n;
02177 size -= n;
02178
02179 track = matroska_find_track_by_num(matroska, num);
02180 if (!track || !track->stream) {
02181 av_log(matroska->ctx, AV_LOG_INFO,
02182 "Invalid stream %"PRIu64" or size %u\n", num, size);
02183 return AVERROR_INVALIDDATA;
02184 } else if (size <= 3)
02185 return 0;
02186 st = track->stream;
02187 if (st->discard >= AVDISCARD_ALL)
02188 return res;
02189 av_assert1(block_duration != AV_NOPTS_VALUE);
02190
02191 block_time = AV_RB16(data);
02192 data += 2;
02193 flags = *data++;
02194 size -= 3;
02195 if (is_keyframe == -1)
02196 is_keyframe = flags & 0x80 ? AV_PKT_FLAG_KEY : 0;
02197
02198 if (cluster_time != (uint64_t)-1
02199 && (block_time >= 0 || cluster_time >= -block_time)) {
02200 timecode = cluster_time + block_time;
02201 if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE
02202 && timecode < track->end_timecode)
02203 is_keyframe = 0;
02204 if (is_keyframe)
02205 av_add_index_entry(st, cluster_pos, timecode, 0,0,AVINDEX_KEYFRAME);
02206 }
02207
02208 if (matroska->skip_to_keyframe && track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
02209 if (timecode < matroska->skip_to_timecode)
02210 return res;
02211 if (!st->skip_to_keyframe) {
02212 av_log(matroska->ctx, AV_LOG_ERROR, "File is broken, keyframes not correctly marked!\n");
02213 matroska->skip_to_keyframe = 0;
02214 }
02215 if (is_keyframe)
02216 matroska->skip_to_keyframe = 0;
02217 }
02218
02219 res = matroska_parse_laces(matroska, &data, size, (flags & 0x06) >> 1,
02220 &lace_size, &laces);
02221
02222 if (res)
02223 goto end;
02224
02225 if (!block_duration)
02226 block_duration = track->default_duration * laces / matroska->time_scale;
02227
02228 if (cluster_time != (uint64_t)-1 && (block_time >= 0 || cluster_time >= -block_time))
02229 track->end_timecode =
02230 FFMAX(track->end_timecode, timecode + block_duration);
02231
02232 for (n = 0; n < laces; n++) {
02233 int64_t lace_duration = block_duration*(n+1) / laces - block_duration*n / laces;
02234
02235 if (lace_size[n] > size) {
02236 av_log(matroska->ctx, AV_LOG_ERROR, "Invalid packet size\n");
02237 break;
02238 }
02239
02240 if ((st->codec->codec_id == AV_CODEC_ID_RA_288 ||
02241 st->codec->codec_id == AV_CODEC_ID_COOK ||
02242 st->codec->codec_id == AV_CODEC_ID_SIPR ||
02243 st->codec->codec_id == AV_CODEC_ID_ATRAC3) &&
02244 st->codec->block_align && track->audio.sub_packet_size) {
02245
02246 res = matroska_parse_rm_audio(matroska, track, st, data, size,
02247 timecode, pos);
02248 if (res)
02249 goto end;
02250
02251 } else {
02252 res = matroska_parse_frame(matroska, track, st, data, lace_size[n],
02253 timecode, lace_duration,
02254 pos, !n? is_keyframe : 0);
02255 if (res)
02256 goto end;
02257 }
02258
02259 if (timecode != AV_NOPTS_VALUE)
02260 timecode = lace_duration ? timecode + lace_duration : AV_NOPTS_VALUE;
02261 data += lace_size[n];
02262 size -= lace_size[n];
02263 }
02264
02265 end:
02266 av_free(lace_size);
02267 return res;
02268 }
02269
02270 static int matroska_parse_cluster_incremental(MatroskaDemuxContext *matroska)
02271 {
02272 EbmlList *blocks_list;
02273 MatroskaBlock *blocks;
02274 int i, res;
02275 res = ebml_parse(matroska,
02276 matroska_cluster_incremental_parsing,
02277 &matroska->current_cluster);
02278 if (res == 1) {
02279
02280 if (matroska->current_cluster_pos)
02281 ebml_level_end(matroska);
02282 ebml_free(matroska_cluster, &matroska->current_cluster);
02283 memset(&matroska->current_cluster, 0, sizeof(MatroskaCluster));
02284 matroska->current_cluster_num_blocks = 0;
02285 matroska->current_cluster_pos = avio_tell(matroska->ctx->pb);
02286 matroska->prev_pkt = NULL;
02287
02288 if (matroska->current_id)
02289 matroska->current_cluster_pos -= 4;
02290 res = ebml_parse(matroska,
02291 matroska_clusters_incremental,
02292 &matroska->current_cluster);
02293
02294 if (res == 1)
02295 res = ebml_parse(matroska,
02296 matroska_cluster_incremental_parsing,
02297 &matroska->current_cluster);
02298 }
02299
02300 if (!res &&
02301 matroska->current_cluster_num_blocks <
02302 matroska->current_cluster.blocks.nb_elem) {
02303 blocks_list = &matroska->current_cluster.blocks;
02304 blocks = blocks_list->elem;
02305
02306 matroska->current_cluster_num_blocks = blocks_list->nb_elem;
02307 i = blocks_list->nb_elem - 1;
02308 if (blocks[i].bin.size > 0 && blocks[i].bin.data) {
02309 int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1;
02310 if (!blocks[i].non_simple)
02311 blocks[i].duration = 0;
02312 res = matroska_parse_block(matroska,
02313 blocks[i].bin.data, blocks[i].bin.size,
02314 blocks[i].bin.pos,
02315 matroska->current_cluster.timecode,
02316 blocks[i].duration, is_keyframe,
02317 matroska->current_cluster_pos);
02318 }
02319 }
02320
02321 if (res < 0) matroska->done = 1;
02322 return res;
02323 }
02324
02325 static int matroska_parse_cluster(MatroskaDemuxContext *matroska)
02326 {
02327 MatroskaCluster cluster = { 0 };
02328 EbmlList *blocks_list;
02329 MatroskaBlock *blocks;
02330 int i, res;
02331 int64_t pos;
02332 if (!matroska->contains_ssa)
02333 return matroska_parse_cluster_incremental(matroska);
02334 pos = avio_tell(matroska->ctx->pb);
02335 matroska->prev_pkt = NULL;
02336 if (matroska->current_id)
02337 pos -= 4;
02338 res = ebml_parse(matroska, matroska_clusters, &cluster);
02339 blocks_list = &cluster.blocks;
02340 blocks = blocks_list->elem;
02341 for (i=0; i<blocks_list->nb_elem; i++)
02342 if (blocks[i].bin.size > 0 && blocks[i].bin.data) {
02343 int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1;
02344 res=matroska_parse_block(matroska,
02345 blocks[i].bin.data, blocks[i].bin.size,
02346 blocks[i].bin.pos, cluster.timecode,
02347 blocks[i].duration, is_keyframe,
02348 pos);
02349 }
02350 ebml_free(matroska_cluster, &cluster);
02351 return res;
02352 }
02353
02354 static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt)
02355 {
02356 MatroskaDemuxContext *matroska = s->priv_data;
02357
02358 while (matroska_deliver_packet(matroska, pkt)) {
02359 int64_t pos = avio_tell(matroska->ctx->pb);
02360 if (matroska->done)
02361 return AVERROR_EOF;
02362 if (matroska_parse_cluster(matroska) < 0)
02363 matroska_resync(matroska, pos);
02364 }
02365
02366 return 0;
02367 }
02368
02369 static int matroska_read_seek(AVFormatContext *s, int stream_index,
02370 int64_t timestamp, int flags)
02371 {
02372 MatroskaDemuxContext *matroska = s->priv_data;
02373 MatroskaTrack *tracks = matroska->tracks.elem;
02374 AVStream *st = s->streams[stream_index];
02375 int i, index, index_sub, index_min;
02376
02377
02378 if (matroska->cues_parsing_deferred > 0) {
02379 matroska->cues_parsing_deferred = 0;
02380 matroska_parse_cues(matroska);
02381 }
02382
02383 if (!st->nb_index_entries)
02384 goto err;
02385 timestamp = FFMAX(timestamp, st->index_entries[0].timestamp);
02386
02387 if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
02388 avio_seek(s->pb, st->index_entries[st->nb_index_entries-1].pos, SEEK_SET);
02389 matroska->current_id = 0;
02390 while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
02391 matroska->prev_pkt = NULL;
02392 matroska_clear_queue(matroska);
02393 if (matroska_parse_cluster(matroska) < 0)
02394 break;
02395 }
02396 }
02397
02398 matroska_clear_queue(matroska);
02399 if (index < 0 || (matroska->cues_parsing_deferred < 0 && index == st->nb_index_entries - 1))
02400 goto err;
02401
02402 index_min = index;
02403 for (i=0; i < matroska->tracks.nb_elem; i++) {
02404 tracks[i].audio.pkt_cnt = 0;
02405 tracks[i].audio.sub_packet_cnt = 0;
02406 tracks[i].audio.buf_timecode = AV_NOPTS_VALUE;
02407 tracks[i].end_timecode = 0;
02408 if (tracks[i].type == MATROSKA_TRACK_TYPE_SUBTITLE
02409 && tracks[i].stream->discard != AVDISCARD_ALL) {
02410 index_sub = av_index_search_timestamp(tracks[i].stream, st->index_entries[index].timestamp, AVSEEK_FLAG_BACKWARD);
02411 if (index_sub >= 0
02412 && st->index_entries[index_sub].pos < st->index_entries[index_min].pos
02413 && st->index_entries[index].timestamp - st->index_entries[index_sub].timestamp < 30000000000/matroska->time_scale)
02414 index_min = index_sub;
02415 }
02416 }
02417
02418 avio_seek(s->pb, st->index_entries[index_min].pos, SEEK_SET);
02419 matroska->current_id = 0;
02420 st->skip_to_keyframe =
02421 matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
02422 matroska->skip_to_timecode = st->index_entries[index].timestamp;
02423 matroska->done = 0;
02424 matroska->num_levels = 0;
02425 ff_update_cur_dts(s, st, st->index_entries[index].timestamp);
02426 return 0;
02427 err:
02428
02429
02430 matroska_clear_queue(matroska);
02431 matroska->current_id = 0;
02432 st->skip_to_keyframe =
02433 matroska->skip_to_keyframe = 0;
02434 matroska->done = 0;
02435 matroska->num_levels = 0;
02436 return -1;
02437 }
02438
02439 static int matroska_read_close(AVFormatContext *s)
02440 {
02441 MatroskaDemuxContext *matroska = s->priv_data;
02442 MatroskaTrack *tracks = matroska->tracks.elem;
02443 int n;
02444
02445 matroska_clear_queue(matroska);
02446
02447 for (n=0; n < matroska->tracks.nb_elem; n++)
02448 if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO)
02449 av_free(tracks[n].audio.buf);
02450 ebml_free(matroska_cluster, &matroska->current_cluster);
02451 ebml_free(matroska_segment, matroska);
02452
02453 return 0;
02454 }
02455
02456 AVInputFormat ff_matroska_demuxer = {
02457 .name = "matroska,webm",
02458 .long_name = NULL_IF_CONFIG_SMALL("Matroska / WebM"),
02459 .priv_data_size = sizeof(MatroskaDemuxContext),
02460 .read_probe = matroska_probe,
02461 .read_header = matroska_read_header,
02462 .read_packet = matroska_read_packet,
02463 .read_close = matroska_read_close,
02464 .read_seek = matroska_read_seek,
02465 };