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