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