matroska.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648
  1. /*
  2. * Matroska file demuxer (no muxer yet)
  3. * Copyright (c) 2003-2004 The ffmpeg Project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file matroska.c
  23. * Matroska file demuxer
  24. * by Ronald Bultje <rbultje@ronald.bitfreak.net>
  25. * with a little help from Moritz Bunkus <moritz@bunkus.org>
  26. * Specs available on the matroska project page:
  27. * http://www.matroska.org/.
  28. */
  29. #include "avformat.h"
  30. /* For codec_get_bmp_id and codec_get_wav_id. */
  31. #include "riff.h"
  32. #include "intfloat_readwrite.h"
  33. /* EBML version supported */
  34. #define EBML_VERSION 1
  35. /* top-level master-IDs */
  36. #define EBML_ID_HEADER 0x1A45DFA3
  37. /* IDs in the HEADER master */
  38. #define EBML_ID_EBMLVERSION 0x4286
  39. #define EBML_ID_EBMLREADVERSION 0x42F7
  40. #define EBML_ID_EBMLMAXIDLENGTH 0x42F2
  41. #define EBML_ID_EBMLMAXSIZELENGTH 0x42F3
  42. #define EBML_ID_DOCTYPE 0x4282
  43. #define EBML_ID_DOCTYPEVERSION 0x4287
  44. #define EBML_ID_DOCTYPEREADVERSION 0x4285
  45. /* general EBML types */
  46. #define EBML_ID_VOID 0xEC
  47. /*
  48. * Matroska element IDs. max. 32-bit.
  49. */
  50. /* toplevel segment */
  51. #define MATROSKA_ID_SEGMENT 0x18538067
  52. /* matroska top-level master IDs */
  53. #define MATROSKA_ID_INFO 0x1549A966
  54. #define MATROSKA_ID_TRACKS 0x1654AE6B
  55. #define MATROSKA_ID_CUES 0x1C53BB6B
  56. #define MATROSKA_ID_TAGS 0x1254C367
  57. #define MATROSKA_ID_SEEKHEAD 0x114D9B74
  58. #define MATROSKA_ID_CLUSTER 0x1F43B675
  59. /* IDs in the info master */
  60. #define MATROSKA_ID_TIMECODESCALE 0x2AD7B1
  61. #define MATROSKA_ID_DURATION 0x4489
  62. #define MATROSKA_ID_WRITINGAPP 0x5741
  63. #define MATROSKA_ID_MUXINGAPP 0x4D80
  64. #define MATROSKA_ID_DATEUTC 0x4461
  65. /* ID in the tracks master */
  66. #define MATROSKA_ID_TRACKENTRY 0xAE
  67. /* IDs in the trackentry master */
  68. #define MATROSKA_ID_TRACKNUMBER 0xD7
  69. #define MATROSKA_ID_TRACKUID 0x73C5
  70. #define MATROSKA_ID_TRACKTYPE 0x83
  71. #define MATROSKA_ID_TRACKAUDIO 0xE1
  72. #define MATROSKA_ID_TRACKVIDEO 0xE0
  73. #define MATROSKA_ID_CODECID 0x86
  74. #define MATROSKA_ID_CODECPRIVATE 0x63A2
  75. #define MATROSKA_ID_CODECNAME 0x258688
  76. #define MATROSKA_ID_CODECINFOURL 0x3B4040
  77. #define MATROSKA_ID_CODECDOWNLOADURL 0x26B240
  78. #define MATROSKA_ID_TRACKNAME 0x536E
  79. #define MATROSKA_ID_TRACKLANGUAGE 0x22B59C
  80. #define MATROSKA_ID_TRACKFLAGENABLED 0xB9
  81. #define MATROSKA_ID_TRACKFLAGDEFAULT 0x88
  82. #define MATROSKA_ID_TRACKFLAGLACING 0x9C
  83. #define MATROSKA_ID_TRACKMINCACHE 0x6DE7
  84. #define MATROSKA_ID_TRACKMAXCACHE 0x6DF8
  85. #define MATROSKA_ID_TRACKDEFAULTDURATION 0x23E383
  86. /* IDs in the trackvideo master */
  87. #define MATROSKA_ID_VIDEOFRAMERATE 0x2383E3
  88. #define MATROSKA_ID_VIDEODISPLAYWIDTH 0x54B0
  89. #define MATROSKA_ID_VIDEODISPLAYHEIGHT 0x54BA
  90. #define MATROSKA_ID_VIDEOPIXELWIDTH 0xB0
  91. #define MATROSKA_ID_VIDEOPIXELHEIGHT 0xBA
  92. #define MATROSKA_ID_VIDEOFLAGINTERLACED 0x9A
  93. #define MATROSKA_ID_VIDEOSTEREOMODE 0x53B9
  94. #define MATROSKA_ID_VIDEOASPECTRATIO 0x54B3
  95. #define MATROSKA_ID_VIDEOCOLOURSPACE 0x2EB524
  96. /* IDs in the trackaudio master */
  97. #define MATROSKA_ID_AUDIOSAMPLINGFREQ 0xB5
  98. #define MATROSKA_ID_AUDIOBITDEPTH 0x6264
  99. #define MATROSKA_ID_AUDIOCHANNELS 0x9F
  100. /* ID in the cues master */
  101. #define MATROSKA_ID_POINTENTRY 0xBB
  102. /* IDs in the pointentry master */
  103. #define MATROSKA_ID_CUETIME 0xB3
  104. #define MATROSKA_ID_CUETRACKPOSITION 0xB7
  105. /* IDs in the cuetrackposition master */
  106. #define MATROSKA_ID_CUETRACK 0xF7
  107. #define MATROSKA_ID_CUECLUSTERPOSITION 0xF1
  108. /* IDs in the tags master */
  109. /* TODO */
  110. /* IDs in the seekhead master */
  111. #define MATROSKA_ID_SEEKENTRY 0x4DBB
  112. /* IDs in the seekpoint master */
  113. #define MATROSKA_ID_SEEKID 0x53AB
  114. #define MATROSKA_ID_SEEKPOSITION 0x53AC
  115. /* IDs in the cluster master */
  116. #define MATROSKA_ID_CLUSTERTIMECODE 0xE7
  117. #define MATROSKA_ID_BLOCKGROUP 0xA0
  118. /* IDs in the blockgroup master */
  119. #define MATROSKA_ID_BLOCK 0xA1
  120. #define MATROSKA_ID_BLOCKDURATION 0x9B
  121. #define MATROSKA_ID_BLOCKREFERENCE 0xFB
  122. typedef enum {
  123. MATROSKA_TRACK_TYPE_VIDEO = 0x1,
  124. MATROSKA_TRACK_TYPE_AUDIO = 0x2,
  125. MATROSKA_TRACK_TYPE_COMPLEX = 0x3,
  126. MATROSKA_TRACK_TYPE_LOGO = 0x10,
  127. MATROSKA_TRACK_TYPE_SUBTITLE = 0x11,
  128. MATROSKA_TRACK_TYPE_CONTROL = 0x20,
  129. } MatroskaTrackType;
  130. typedef enum {
  131. MATROSKA_EYE_MODE_MONO = 0x0,
  132. MATROSKA_EYE_MODE_RIGHT = 0x1,
  133. MATROSKA_EYE_MODE_LEFT = 0x2,
  134. MATROSKA_EYE_MODE_BOTH = 0x3,
  135. } MatroskaEyeMode;
  136. typedef enum {
  137. MATROSKA_ASPECT_RATIO_MODE_FREE = 0x0,
  138. MATROSKA_ASPECT_RATIO_MODE_KEEP = 0x1,
  139. MATROSKA_ASPECT_RATIO_MODE_FIXED = 0x2,
  140. } MatroskaAspectRatioMode;
  141. /*
  142. * These aren't in any way "matroska-form" things,
  143. * it's just something I use in the muxer/demuxer.
  144. */
  145. typedef enum {
  146. MATROSKA_TRACK_ENABLED = (1<<0),
  147. MATROSKA_TRACK_DEFAULT = (1<<1),
  148. MATROSKA_TRACK_LACING = (1<<2),
  149. MATROSKA_TRACK_SHIFT = (1<<16)
  150. } MatroskaTrackFlags;
  151. typedef enum {
  152. MATROSKA_VIDEOTRACK_INTERLACED = (MATROSKA_TRACK_SHIFT<<0)
  153. } MatroskaVideoTrackFlags;
  154. /*
  155. * Matroska Codec IDs. Strings.
  156. */
  157. typedef struct CodecTags{
  158. const char *str;
  159. enum CodecID id;
  160. }CodecTags;
  161. #define MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC "V_MS/VFW/FOURCC"
  162. #define MATROSKA_CODEC_ID_AUDIO_ACM "A_MS/ACM"
  163. static CodecTags codec_tags[]={
  164. // {"V_MS/VFW/FOURCC" , CODEC_ID_NONE},
  165. {"V_UNCOMPRESSED" , CODEC_ID_RAWVIDEO},
  166. {"V_MPEG4/ISO/SP" , CODEC_ID_MPEG4},
  167. {"V_MPEG4/ISO/ASP" , CODEC_ID_MPEG4},
  168. {"V_MPEG4/ISO/AP" , CODEC_ID_MPEG4},
  169. {"V_MPEG4/ISO/AVC" , CODEC_ID_H264},
  170. {"V_MPEG4/MS/V3" , CODEC_ID_MSMPEG4V3},
  171. {"V_MPEG1" , CODEC_ID_MPEG1VIDEO},
  172. {"V_MPEG2" , CODEC_ID_MPEG2VIDEO},
  173. {"V_MJPEG" , CODEC_ID_MJPEG},
  174. {"V_REAL/RV10" , CODEC_ID_RV10},
  175. {"V_REAL/RV20" , CODEC_ID_RV20},
  176. {"V_REAL/RV30" , CODEC_ID_RV30},
  177. {"V_REAL/RV40" , CODEC_ID_RV40},
  178. /* TODO: Real/Quicktime */
  179. // {"A_MS/ACM" , CODEC_ID_NONE},
  180. {"A_MPEG/L1" , CODEC_ID_MP3},
  181. {"A_MPEG/L2" , CODEC_ID_MP3},
  182. {"A_MPEG/L3" , CODEC_ID_MP3},
  183. {"A_PCM/INT/BIG" , CODEC_ID_PCM_U16BE},
  184. {"A_PCM/INT/LIT" , CODEC_ID_PCM_U16LE},
  185. // {"A_PCM/FLOAT/IEEE" , CODEC_ID_NONE},
  186. {"A_AC3" , CODEC_ID_AC3},
  187. {"A_DTS" , CODEC_ID_DTS},
  188. {"A_VORBIS" , CODEC_ID_VORBIS},
  189. {"A_AAC/MPEG2/" , CODEC_ID_AAC},
  190. {"A_AAC/MPEG4/" , CODEC_ID_AAC},
  191. {NULL , CODEC_ID_NONE}
  192. /* TODO: AC3-9/10 (?), Real, Musepack, Quicktime */
  193. };
  194. /* max. depth in the EBML tree structure */
  195. #define EBML_MAX_DEPTH 16
  196. typedef struct Track {
  197. MatroskaTrackType type;
  198. /* Unique track number and track ID. stream_index is the index that
  199. * the calling app uses for this track. */
  200. uint32_t num,
  201. uid,
  202. stream_index;
  203. char *name,
  204. *language;
  205. char *codec_id,
  206. *codec_name;
  207. unsigned char *codec_priv;
  208. int codec_priv_size;
  209. int64_t default_duration;
  210. MatroskaTrackFlags flags;
  211. } MatroskaTrack;
  212. typedef struct MatroskaVideoTrack {
  213. MatroskaTrack track;
  214. int pixel_width,
  215. pixel_height,
  216. display_width,
  217. display_height;
  218. uint32_t fourcc;
  219. MatroskaAspectRatioMode ar_mode;
  220. MatroskaEyeMode eye_mode;
  221. //..
  222. } MatroskaVideoTrack;
  223. typedef struct MatroskaAudioTrack {
  224. MatroskaTrack track;
  225. int channels,
  226. bitdepth,
  227. samplerate;
  228. //..
  229. } MatroskaAudioTrack;
  230. typedef struct MatroskaSubtitleTrack {
  231. MatroskaTrack track;
  232. //..
  233. } MatroskaSubtitleTrack;
  234. typedef struct MatroskaLevel {
  235. uint64_t start, length;
  236. } MatroskaLevel;
  237. typedef struct MatroskaDemuxIndex {
  238. uint64_t pos; /* of the corresponding *cluster*! */
  239. uint16_t track; /* reference to 'num' */
  240. uint64_t time; /* in nanoseconds */
  241. } MatroskaDemuxIndex;
  242. typedef struct MatroskaDemuxContext {
  243. AVFormatContext *ctx;
  244. /* ebml stuff */
  245. int num_levels;
  246. MatroskaLevel levels[EBML_MAX_DEPTH];
  247. int level_up;
  248. /* matroska stuff */
  249. char *writing_app,
  250. *muxing_app;
  251. int64_t created;
  252. /* timescale in the file */
  253. int64_t time_scale;
  254. /* position (time, ns) */
  255. int64_t pos;
  256. /* num_streams is the number of streams that av_new_stream() was called
  257. * for ( = that are available to the calling program). */
  258. int num_tracks, num_streams;
  259. MatroskaTrack *tracks[MAX_STREAMS];
  260. /* cache for ID peeking */
  261. uint32_t peek_id;
  262. /* byte position of the segment inside the stream */
  263. offset_t segment_start;
  264. /* The packet queue. */
  265. AVPacket **packets;
  266. int num_packets;
  267. /* have we already parse metadata/cues/clusters? */
  268. int metadata_parsed,
  269. index_parsed,
  270. done;
  271. /* The index for seeking. */
  272. int num_indexes;
  273. MatroskaDemuxIndex *index;
  274. } MatroskaDemuxContext;
  275. /*
  276. * The first few functions handle EBML file parsing. The rest
  277. * is the document interpretation. Matroska really just is a
  278. * EBML file.
  279. */
  280. /*
  281. * Return: the amount of levels in the hierarchy that the
  282. * current element lies higher than the previous one.
  283. * The opposite isn't done - that's auto-done using master
  284. * element reading.
  285. */
  286. static int
  287. ebml_read_element_level_up (MatroskaDemuxContext *matroska)
  288. {
  289. ByteIOContext *pb = &matroska->ctx->pb;
  290. offset_t pos = url_ftell(pb);
  291. int num = 0;
  292. while (matroska->num_levels > 0) {
  293. MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
  294. if (pos >= level->start + level->length) {
  295. matroska->num_levels--;
  296. num++;
  297. } else {
  298. break;
  299. }
  300. }
  301. return num;
  302. }
  303. /*
  304. * Read: an "EBML number", which is defined as a variable-length
  305. * array of bytes. The first byte indicates the length by giving a
  306. * number of 0-bits followed by a one. The position of the first
  307. * "one" bit inside the first byte indicates the length of this
  308. * number.
  309. * Returns: num. of bytes read. < 0 on error.
  310. */
  311. static int
  312. ebml_read_num (MatroskaDemuxContext *matroska,
  313. int max_size,
  314. uint64_t *number)
  315. {
  316. ByteIOContext *pb = &matroska->ctx->pb;
  317. int len_mask = 0x80, read = 1, n = 1;
  318. int64_t total = 0;
  319. /* the first byte tells us the length in bytes - get_byte() can normally
  320. * return 0, but since that's not a valid first ebmlID byte, we can
  321. * use it safely here to catch EOS. */
  322. if (!(total = get_byte(pb))) {
  323. /* we might encounter EOS here */
  324. if (!url_feof(pb)) {
  325. offset_t pos = url_ftell(pb);
  326. av_log(matroska->ctx, AV_LOG_ERROR,
  327. "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
  328. pos, pos);
  329. }
  330. return AVERROR_IO; /* EOS or actual I/O error */
  331. }
  332. /* get the length of the EBML number */
  333. while (read <= max_size && !(total & len_mask)) {
  334. read++;
  335. len_mask >>= 1;
  336. }
  337. if (read > max_size) {
  338. offset_t pos = url_ftell(pb) - 1;
  339. av_log(matroska->ctx, AV_LOG_ERROR,
  340. "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
  341. (uint8_t) total, pos, pos);
  342. return AVERROR_INVALIDDATA;
  343. }
  344. /* read out length */
  345. total &= ~len_mask;
  346. while (n++ < read)
  347. total = (total << 8) | get_byte(pb);
  348. *number = total;
  349. return read;
  350. }
  351. /*
  352. * Read: the element content data ID.
  353. * Return: the number of bytes read or < 0 on error.
  354. */
  355. static int
  356. ebml_read_element_id (MatroskaDemuxContext *matroska,
  357. uint32_t *id,
  358. int *level_up)
  359. {
  360. int read;
  361. uint64_t total;
  362. /* if we re-call this, use our cached ID */
  363. if (matroska->peek_id != 0) {
  364. if (level_up)
  365. *level_up = 0;
  366. *id = matroska->peek_id;
  367. return 0;
  368. }
  369. /* read out the "EBML number", include tag in ID */
  370. if ((read = ebml_read_num(matroska, 4, &total)) < 0)
  371. return read;
  372. *id = matroska->peek_id = total | (1 << (read * 7));
  373. /* level tracking */
  374. if (level_up)
  375. *level_up = ebml_read_element_level_up(matroska);
  376. return read;
  377. }
  378. /*
  379. * Read: element content length.
  380. * Return: the number of bytes read or < 0 on error.
  381. */
  382. static int
  383. ebml_read_element_length (MatroskaDemuxContext *matroska,
  384. uint64_t *length)
  385. {
  386. /* clear cache since we're now beyond that data point */
  387. matroska->peek_id = 0;
  388. /* read out the "EBML number", include tag in ID */
  389. return ebml_read_num(matroska, 8, length);
  390. }
  391. /*
  392. * Return: the ID of the next element, or 0 on error.
  393. * Level_up contains the amount of levels that this
  394. * next element lies higher than the previous one.
  395. */
  396. static uint32_t
  397. ebml_peek_id (MatroskaDemuxContext *matroska,
  398. int *level_up)
  399. {
  400. uint32_t id;
  401. assert(level_up != NULL);
  402. if (ebml_read_element_id(matroska, &id, level_up) < 0)
  403. return 0;
  404. return id;
  405. }
  406. /*
  407. * Seek to a given offset.
  408. * 0 is success, -1 is failure.
  409. */
  410. static int
  411. ebml_read_seek (MatroskaDemuxContext *matroska,
  412. offset_t offset)
  413. {
  414. ByteIOContext *pb = &matroska->ctx->pb;
  415. /* clear ID cache, if any */
  416. matroska->peek_id = 0;
  417. return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
  418. }
  419. /*
  420. * Skip the next element.
  421. * 0 is success, -1 is failure.
  422. */
  423. static int
  424. ebml_read_skip (MatroskaDemuxContext *matroska)
  425. {
  426. ByteIOContext *pb = &matroska->ctx->pb;
  427. uint32_t id;
  428. uint64_t length;
  429. int res;
  430. if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
  431. (res = ebml_read_element_length(matroska, &length)) < 0)
  432. return res;
  433. url_fskip(pb, length);
  434. return 0;
  435. }
  436. /*
  437. * Read the next element as an unsigned int.
  438. * 0 is success, < 0 is failure.
  439. */
  440. static int
  441. ebml_read_uint (MatroskaDemuxContext *matroska,
  442. uint32_t *id,
  443. uint64_t *num)
  444. {
  445. ByteIOContext *pb = &matroska->ctx->pb;
  446. int n = 0, size, res;
  447. uint64_t rlength;
  448. if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
  449. (res = ebml_read_element_length(matroska, &rlength)) < 0)
  450. return res;
  451. size = rlength;
  452. if (size < 1 || size > 8) {
  453. offset_t pos = url_ftell(pb);
  454. av_log(matroska->ctx, AV_LOG_ERROR,
  455. "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
  456. size, pos, pos);
  457. return AVERROR_INVALIDDATA;
  458. }
  459. /* big-endian ordening; build up number */
  460. *num = 0;
  461. while (n++ < size)
  462. *num = (*num << 8) | get_byte(pb);
  463. return 0;
  464. }
  465. /*
  466. * Read the next element as a signed int.
  467. * 0 is success, < 0 is failure.
  468. */
  469. static int
  470. ebml_read_sint (MatroskaDemuxContext *matroska,
  471. uint32_t *id,
  472. int64_t *num)
  473. {
  474. ByteIOContext *pb = &matroska->ctx->pb;
  475. int size, n = 1, negative = 0, res;
  476. uint64_t rlength;
  477. if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
  478. (res = ebml_read_element_length(matroska, &rlength)) < 0)
  479. return res;
  480. size = rlength;
  481. if (size < 1 || size > 8) {
  482. offset_t pos = url_ftell(pb);
  483. av_log(matroska->ctx, AV_LOG_ERROR,
  484. "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
  485. size, pos, pos);
  486. return AVERROR_INVALIDDATA;
  487. }
  488. if ((*num = get_byte(pb)) & 0x80) {
  489. negative = 1;
  490. *num &= ~0x80;
  491. }
  492. *num = 0;
  493. while (n++ < size)
  494. *num = (*num << 8) | get_byte(pb);
  495. /* make signed */
  496. if (negative)
  497. *num = *num - (1LL << ((8 * size) - 1));
  498. return 0;
  499. }
  500. /*
  501. * Read the next element as a float.
  502. * 0 is success, < 0 is failure.
  503. */
  504. static int
  505. ebml_read_float (MatroskaDemuxContext *matroska,
  506. uint32_t *id,
  507. double *num)
  508. {
  509. ByteIOContext *pb = &matroska->ctx->pb;
  510. int size, res;
  511. uint64_t rlength;
  512. if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
  513. (res = ebml_read_element_length(matroska, &rlength)) < 0)
  514. return res;
  515. size = rlength;
  516. if (size == 4) {
  517. *num= av_int2flt(get_be32(pb));
  518. } else if(size==8){
  519. *num= av_int2dbl(get_be64(pb));
  520. } else if(size==10){
  521. av_log(matroska->ctx, AV_LOG_ERROR,
  522. "FIXME! 10-byte floats unimplemented\n");
  523. return AVERROR_UNKNOWN;
  524. } else{
  525. offset_t pos = url_ftell(pb);
  526. av_log(matroska->ctx, AV_LOG_ERROR,
  527. "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
  528. size, pos, pos);
  529. return AVERROR_INVALIDDATA;
  530. }
  531. return 0;
  532. }
  533. /*
  534. * Read the next element as an ASCII string.
  535. * 0 is success, < 0 is failure.
  536. */
  537. static int
  538. ebml_read_ascii (MatroskaDemuxContext *matroska,
  539. uint32_t *id,
  540. char **str)
  541. {
  542. ByteIOContext *pb = &matroska->ctx->pb;
  543. int size, res;
  544. uint64_t rlength;
  545. if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
  546. (res = ebml_read_element_length(matroska, &rlength)) < 0)
  547. return res;
  548. size = rlength;
  549. /* ebml strings are usually not 0-terminated, so we allocate one
  550. * byte more, read the string and NULL-terminate it ourselves. */
  551. if (size < 0 || !(*str = av_malloc(size + 1))) {
  552. av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
  553. return AVERROR_NOMEM;
  554. }
  555. if (get_buffer(pb, (uint8_t *) *str, size) != size) {
  556. offset_t pos = url_ftell(pb);
  557. av_log(matroska->ctx, AV_LOG_ERROR,
  558. "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
  559. return AVERROR_IO;
  560. }
  561. (*str)[size] = '\0';
  562. return 0;
  563. }
  564. /*
  565. * Read the next element as a UTF-8 string.
  566. * 0 is success, < 0 is failure.
  567. */
  568. static int
  569. ebml_read_utf8 (MatroskaDemuxContext *matroska,
  570. uint32_t *id,
  571. char **str)
  572. {
  573. return ebml_read_ascii(matroska, id, str);
  574. }
  575. /*
  576. * Read the next element as a date (nanoseconds since 1/1/2000).
  577. * 0 is success, < 0 is failure.
  578. */
  579. static int
  580. ebml_read_date (MatroskaDemuxContext *matroska,
  581. uint32_t *id,
  582. int64_t *date)
  583. {
  584. return ebml_read_sint(matroska, id, date);
  585. }
  586. /*
  587. * Read the next element, but only the header. The contents
  588. * are supposed to be sub-elements which can be read separately.
  589. * 0 is success, < 0 is failure.
  590. */
  591. static int
  592. ebml_read_master (MatroskaDemuxContext *matroska,
  593. uint32_t *id)
  594. {
  595. ByteIOContext *pb = &matroska->ctx->pb;
  596. uint64_t length;
  597. MatroskaLevel *level;
  598. int res;
  599. if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
  600. (res = ebml_read_element_length(matroska, &length)) < 0)
  601. return res;
  602. /* protect... (Heaven forbids that the '>' is true) */
  603. if (matroska->num_levels >= EBML_MAX_DEPTH) {
  604. av_log(matroska->ctx, AV_LOG_ERROR,
  605. "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
  606. return AVERROR_NOTSUPP;
  607. }
  608. /* remember level */
  609. level = &matroska->levels[matroska->num_levels++];
  610. level->start = url_ftell(pb);
  611. level->length = length;
  612. return 0;
  613. }
  614. /*
  615. * Read the next element as binary data.
  616. * 0 is success, < 0 is failure.
  617. */
  618. static int
  619. ebml_read_binary (MatroskaDemuxContext *matroska,
  620. uint32_t *id,
  621. uint8_t **binary,
  622. int *size)
  623. {
  624. ByteIOContext *pb = &matroska->ctx->pb;
  625. uint64_t rlength;
  626. int res;
  627. if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
  628. (res = ebml_read_element_length(matroska, &rlength)) < 0)
  629. return res;
  630. *size = rlength;
  631. if (!(*binary = av_malloc(*size))) {
  632. av_log(matroska->ctx, AV_LOG_ERROR,
  633. "Memory allocation error\n");
  634. return AVERROR_NOMEM;
  635. }
  636. if (get_buffer(pb, *binary, *size) != *size) {
  637. offset_t pos = url_ftell(pb);
  638. av_log(matroska->ctx, AV_LOG_ERROR,
  639. "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
  640. return AVERROR_IO;
  641. }
  642. return 0;
  643. }
  644. /*
  645. * Read signed/unsigned "EBML" numbers.
  646. * Return: number of bytes processed, < 0 on error.
  647. * XXX: use ebml_read_num().
  648. */
  649. static int
  650. matroska_ebmlnum_uint (uint8_t *data,
  651. uint32_t size,
  652. uint64_t *num)
  653. {
  654. int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
  655. uint64_t total;
  656. if (size <= 0)
  657. return AVERROR_INVALIDDATA;
  658. total = data[0];
  659. while (read <= 8 && !(total & len_mask)) {
  660. read++;
  661. len_mask >>= 1;
  662. }
  663. if (read > 8)
  664. return AVERROR_INVALIDDATA;
  665. if ((total &= (len_mask - 1)) == len_mask - 1)
  666. num_ffs++;
  667. if (size < read)
  668. return AVERROR_INVALIDDATA;
  669. while (n < read) {
  670. if (data[n] == 0xff)
  671. num_ffs++;
  672. total = (total << 8) | data[n];
  673. n++;
  674. }
  675. if (read == num_ffs)
  676. *num = (uint64_t)-1;
  677. else
  678. *num = total;
  679. return read;
  680. }
  681. /*
  682. * Same as above, but signed.
  683. */
  684. static int
  685. matroska_ebmlnum_sint (uint8_t *data,
  686. uint32_t size,
  687. int64_t *num)
  688. {
  689. uint64_t unum;
  690. int res;
  691. /* read as unsigned number first */
  692. if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
  693. return res;
  694. /* make signed (weird way) */
  695. if (unum == (uint64_t)-1)
  696. *num = INT64_MAX;
  697. else
  698. *num = unum - ((1LL << ((7 * res) - 1)) - 1);
  699. return res;
  700. }
  701. /*
  702. * Read an EBML header.
  703. * 0 is success, < 0 is failure.
  704. */
  705. static int
  706. ebml_read_header (MatroskaDemuxContext *matroska,
  707. char **doctype,
  708. int *version)
  709. {
  710. uint32_t id;
  711. int level_up, res = 0;
  712. /* default init */
  713. if (doctype)
  714. *doctype = NULL;
  715. if (version)
  716. *version = 1;
  717. if (!(id = ebml_peek_id(matroska, &level_up)) ||
  718. level_up != 0 || id != EBML_ID_HEADER) {
  719. av_log(matroska->ctx, AV_LOG_ERROR,
  720. "This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
  721. return AVERROR_INVALIDDATA;
  722. }
  723. if ((res = ebml_read_master(matroska, &id)) < 0)
  724. return res;
  725. while (res == 0) {
  726. if (!(id = ebml_peek_id(matroska, &level_up)))
  727. return AVERROR_IO;
  728. /* end-of-header */
  729. if (level_up)
  730. break;
  731. switch (id) {
  732. /* is our read version uptodate? */
  733. case EBML_ID_EBMLREADVERSION: {
  734. uint64_t num;
  735. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  736. return res;
  737. if (num > EBML_VERSION) {
  738. av_log(matroska->ctx, AV_LOG_ERROR,
  739. "EBML version %"PRIu64" (> %d) is not supported\n",
  740. num, EBML_VERSION);
  741. return AVERROR_INVALIDDATA;
  742. }
  743. break;
  744. }
  745. /* we only handle 8 byte lengths at max */
  746. case EBML_ID_EBMLMAXSIZELENGTH: {
  747. uint64_t num;
  748. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  749. return res;
  750. if (num > sizeof(uint64_t)) {
  751. av_log(matroska->ctx, AV_LOG_ERROR,
  752. "Integers of size %"PRIu64" (> %zd) not supported\n",
  753. num, sizeof(uint64_t));
  754. return AVERROR_INVALIDDATA;
  755. }
  756. break;
  757. }
  758. /* we handle 4 byte IDs at max */
  759. case EBML_ID_EBMLMAXIDLENGTH: {
  760. uint64_t num;
  761. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  762. return res;
  763. if (num > sizeof(uint32_t)) {
  764. av_log(matroska->ctx, AV_LOG_ERROR,
  765. "IDs of size %"PRIu64" (> %zu) not supported\n",
  766. num, sizeof(uint32_t));
  767. return AVERROR_INVALIDDATA;
  768. }
  769. break;
  770. }
  771. case EBML_ID_DOCTYPE: {
  772. char *text;
  773. if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
  774. return res;
  775. if (doctype) {
  776. if (*doctype)
  777. av_free(*doctype);
  778. *doctype = text;
  779. } else
  780. av_free(text);
  781. break;
  782. }
  783. case EBML_ID_DOCTYPEREADVERSION: {
  784. uint64_t num;
  785. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  786. return res;
  787. if (version)
  788. *version = num;
  789. break;
  790. }
  791. default:
  792. av_log(matroska->ctx, AV_LOG_INFO,
  793. "Unknown data type 0x%x in EBML header", id);
  794. /* pass-through */
  795. case EBML_ID_VOID:
  796. /* we ignore these two, as they don't tell us anything we
  797. * care about */
  798. case EBML_ID_EBMLVERSION:
  799. case EBML_ID_DOCTYPEVERSION:
  800. res = ebml_read_skip (matroska);
  801. break;
  802. }
  803. }
  804. return 0;
  805. }
  806. /*
  807. * Put one packet in an application-supplied AVPacket struct.
  808. * Returns 0 on success or -1 on failure.
  809. */
  810. static int
  811. matroska_deliver_packet (MatroskaDemuxContext *matroska,
  812. AVPacket *pkt)
  813. {
  814. if (matroska->num_packets > 0) {
  815. memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
  816. av_free(matroska->packets[0]);
  817. if (matroska->num_packets > 1) {
  818. memmove(&matroska->packets[0], &matroska->packets[1],
  819. (matroska->num_packets - 1) * sizeof(AVPacket *));
  820. matroska->packets =
  821. av_realloc(matroska->packets, (matroska->num_packets - 1) *
  822. sizeof(AVPacket *));
  823. } else {
  824. av_free(matroska->packets);
  825. matroska->packets = NULL;
  826. }
  827. matroska->num_packets--;
  828. return 0;
  829. }
  830. return -1;
  831. }
  832. /*
  833. * Put a packet into our internal queue. Will be delivered to the
  834. * user/application during the next get_packet() call.
  835. */
  836. static void
  837. matroska_queue_packet (MatroskaDemuxContext *matroska,
  838. AVPacket *pkt)
  839. {
  840. matroska->packets =
  841. av_realloc(matroska->packets, (matroska->num_packets + 1) *
  842. sizeof(AVPacket *));
  843. matroska->packets[matroska->num_packets] = pkt;
  844. matroska->num_packets++;
  845. }
  846. /*
  847. * Autodetecting...
  848. */
  849. static int
  850. matroska_probe (AVProbeData *p)
  851. {
  852. uint64_t total = 0;
  853. int len_mask = 0x80, size = 1, n = 1;
  854. uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
  855. if (p->buf_size < 5)
  856. return 0;
  857. /* ebml header? */
  858. if ((p->buf[0] << 24 | p->buf[1] << 16 |
  859. p->buf[2] << 8 | p->buf[3]) != EBML_ID_HEADER)
  860. return 0;
  861. /* length of header */
  862. total = p->buf[4];
  863. while (size <= 8 && !(total & len_mask)) {
  864. size++;
  865. len_mask >>= 1;
  866. }
  867. if (size > 8)
  868. return 0;
  869. total &= (len_mask - 1);
  870. while (n < size)
  871. total = (total << 8) | p->buf[4 + n++];
  872. /* does the probe data contain the whole header? */
  873. if (p->buf_size < 4 + size + total)
  874. return 0;
  875. /* the header must contain the document type 'matroska'. For now,
  876. * we don't parse the whole header but simply check for the
  877. * availability of that array of characters inside the header.
  878. * Not fully fool-proof, but good enough. */
  879. for (n = 4 + size; n < 4 + size + total - sizeof(probe_data); n++)
  880. if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
  881. return AVPROBE_SCORE_MAX;
  882. return 0;
  883. }
  884. /*
  885. * From here on, it's all XML-style DTD stuff... Needs no comments.
  886. */
  887. static int
  888. matroska_parse_info (MatroskaDemuxContext *matroska)
  889. {
  890. int res = 0;
  891. uint32_t id;
  892. av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
  893. while (res == 0) {
  894. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  895. res = AVERROR_IO;
  896. break;
  897. } else if (matroska->level_up) {
  898. matroska->level_up--;
  899. break;
  900. }
  901. switch (id) {
  902. /* cluster timecode */
  903. case MATROSKA_ID_TIMECODESCALE: {
  904. uint64_t num;
  905. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  906. break;
  907. matroska->time_scale = num;
  908. break;
  909. }
  910. case MATROSKA_ID_DURATION: {
  911. double num;
  912. if ((res = ebml_read_float(matroska, &id, &num)) < 0)
  913. break;
  914. matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
  915. break;
  916. }
  917. case MATROSKA_ID_WRITINGAPP: {
  918. char *text;
  919. if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
  920. break;
  921. matroska->writing_app = text;
  922. break;
  923. }
  924. case MATROSKA_ID_MUXINGAPP: {
  925. char *text;
  926. if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
  927. break;
  928. matroska->muxing_app = text;
  929. break;
  930. }
  931. case MATROSKA_ID_DATEUTC: {
  932. int64_t time;
  933. if ((res = ebml_read_date(matroska, &id, &time)) < 0)
  934. break;
  935. matroska->created = time;
  936. break;
  937. }
  938. default:
  939. av_log(matroska->ctx, AV_LOG_INFO,
  940. "Unknown entry 0x%x in info header\n", id);
  941. /* fall-through */
  942. case EBML_ID_VOID:
  943. res = ebml_read_skip(matroska);
  944. break;
  945. }
  946. if (matroska->level_up) {
  947. matroska->level_up--;
  948. break;
  949. }
  950. }
  951. return res;
  952. }
  953. static int
  954. matroska_add_stream (MatroskaDemuxContext *matroska)
  955. {
  956. int res = 0;
  957. uint32_t id;
  958. MatroskaTrack *track;
  959. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
  960. /* Allocate a generic track. As soon as we know its type we'll realloc. */
  961. track = av_mallocz(sizeof(MatroskaTrack));
  962. matroska->num_tracks++;
  963. /* start with the master */
  964. if ((res = ebml_read_master(matroska, &id)) < 0)
  965. return res;
  966. /* try reading the trackentry headers */
  967. while (res == 0) {
  968. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  969. res = AVERROR_IO;
  970. break;
  971. } else if (matroska->level_up > 0) {
  972. matroska->level_up--;
  973. break;
  974. }
  975. switch (id) {
  976. /* track number (unique stream ID) */
  977. case MATROSKA_ID_TRACKNUMBER: {
  978. uint64_t num;
  979. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  980. break;
  981. track->num = num;
  982. break;
  983. }
  984. /* track UID (unique identifier) */
  985. case MATROSKA_ID_TRACKUID: {
  986. uint64_t num;
  987. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  988. break;
  989. track->uid = num;
  990. break;
  991. }
  992. /* track type (video, audio, combined, subtitle, etc.) */
  993. case MATROSKA_ID_TRACKTYPE: {
  994. uint64_t num;
  995. if (track->type != 0) {
  996. av_log(matroska->ctx, AV_LOG_INFO,
  997. "More than one tracktype in an entry - skip\n");
  998. break;
  999. }
  1000. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1001. break;
  1002. track->type = num;
  1003. /* ok, so we're actually going to reallocate this thing */
  1004. switch (track->type) {
  1005. case MATROSKA_TRACK_TYPE_VIDEO:
  1006. track = (MatroskaTrack *)
  1007. av_realloc(track, sizeof(MatroskaVideoTrack));
  1008. break;
  1009. case MATROSKA_TRACK_TYPE_AUDIO:
  1010. track = (MatroskaTrack *)
  1011. av_realloc(track, sizeof(MatroskaAudioTrack));
  1012. ((MatroskaAudioTrack *)track)->channels = 1;
  1013. ((MatroskaAudioTrack *)track)->samplerate = 8000;
  1014. break;
  1015. case MATROSKA_TRACK_TYPE_SUBTITLE:
  1016. track = (MatroskaTrack *)
  1017. av_realloc(track, sizeof(MatroskaSubtitleTrack));
  1018. break;
  1019. case MATROSKA_TRACK_TYPE_COMPLEX:
  1020. case MATROSKA_TRACK_TYPE_LOGO:
  1021. case MATROSKA_TRACK_TYPE_CONTROL:
  1022. default:
  1023. av_log(matroska->ctx, AV_LOG_INFO,
  1024. "Unknown or unsupported track type 0x%x\n",
  1025. track->type);
  1026. track->type = 0;
  1027. break;
  1028. }
  1029. matroska->tracks[matroska->num_tracks - 1] = track;
  1030. break;
  1031. }
  1032. /* tracktype specific stuff for video */
  1033. case MATROSKA_ID_TRACKVIDEO: {
  1034. MatroskaVideoTrack *videotrack;
  1035. if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
  1036. av_log(matroska->ctx, AV_LOG_INFO,
  1037. "video data in non-video track - ignoring\n");
  1038. res = AVERROR_INVALIDDATA;
  1039. break;
  1040. } else if ((res = ebml_read_master(matroska, &id)) < 0)
  1041. break;
  1042. videotrack = (MatroskaVideoTrack *)track;
  1043. while (res == 0) {
  1044. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1045. res = AVERROR_IO;
  1046. break;
  1047. } else if (matroska->level_up > 0) {
  1048. matroska->level_up--;
  1049. break;
  1050. }
  1051. switch (id) {
  1052. /* fixme, this should be one-up, but I get it here */
  1053. case MATROSKA_ID_TRACKDEFAULTDURATION: {
  1054. uint64_t num;
  1055. if ((res = ebml_read_uint (matroska, &id,
  1056. &num)) < 0)
  1057. break;
  1058. track->default_duration = num;
  1059. break;
  1060. }
  1061. /* video framerate */
  1062. case MATROSKA_ID_VIDEOFRAMERATE: {
  1063. double num;
  1064. if ((res = ebml_read_float(matroska, &id,
  1065. &num)) < 0)
  1066. break;
  1067. track->default_duration = 1000000000 * (1. / num);
  1068. break;
  1069. }
  1070. /* width of the size to display the video at */
  1071. case MATROSKA_ID_VIDEODISPLAYWIDTH: {
  1072. uint64_t num;
  1073. if ((res = ebml_read_uint(matroska, &id,
  1074. &num)) < 0)
  1075. break;
  1076. videotrack->display_width = num;
  1077. break;
  1078. }
  1079. /* height of the size to display the video at */
  1080. case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
  1081. uint64_t num;
  1082. if ((res = ebml_read_uint(matroska, &id,
  1083. &num)) < 0)
  1084. break;
  1085. videotrack->display_height = num;
  1086. break;
  1087. }
  1088. /* width of the video in the file */
  1089. case MATROSKA_ID_VIDEOPIXELWIDTH: {
  1090. uint64_t num;
  1091. if ((res = ebml_read_uint(matroska, &id,
  1092. &num)) < 0)
  1093. break;
  1094. videotrack->pixel_width = num;
  1095. break;
  1096. }
  1097. /* height of the video in the file */
  1098. case MATROSKA_ID_VIDEOPIXELHEIGHT: {
  1099. uint64_t num;
  1100. if ((res = ebml_read_uint(matroska, &id,
  1101. &num)) < 0)
  1102. break;
  1103. videotrack->pixel_height = num;
  1104. break;
  1105. }
  1106. /* whether the video is interlaced */
  1107. case MATROSKA_ID_VIDEOFLAGINTERLACED: {
  1108. uint64_t num;
  1109. if ((res = ebml_read_uint(matroska, &id,
  1110. &num)) < 0)
  1111. break;
  1112. if (num)
  1113. track->flags |=
  1114. MATROSKA_VIDEOTRACK_INTERLACED;
  1115. else
  1116. track->flags &=
  1117. ~MATROSKA_VIDEOTRACK_INTERLACED;
  1118. break;
  1119. }
  1120. /* stereo mode (whether the video has two streams,
  1121. * where one is for the left eye and the other for
  1122. * the right eye, which creates a 3D-like
  1123. * effect) */
  1124. case MATROSKA_ID_VIDEOSTEREOMODE: {
  1125. uint64_t num;
  1126. if ((res = ebml_read_uint(matroska, &id,
  1127. &num)) < 0)
  1128. break;
  1129. if (num != MATROSKA_EYE_MODE_MONO &&
  1130. num != MATROSKA_EYE_MODE_LEFT &&
  1131. num != MATROSKA_EYE_MODE_RIGHT &&
  1132. num != MATROSKA_EYE_MODE_BOTH) {
  1133. av_log(matroska->ctx, AV_LOG_INFO,
  1134. "Ignoring unknown eye mode 0x%x\n",
  1135. (uint32_t) num);
  1136. break;
  1137. }
  1138. videotrack->eye_mode = num;
  1139. break;
  1140. }
  1141. /* aspect ratio behaviour */
  1142. case MATROSKA_ID_VIDEOASPECTRATIO: {
  1143. uint64_t num;
  1144. if ((res = ebml_read_uint(matroska, &id,
  1145. &num)) < 0)
  1146. break;
  1147. if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
  1148. num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
  1149. num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
  1150. av_log(matroska->ctx, AV_LOG_INFO,
  1151. "Ignoring unknown aspect ratio 0x%x\n",
  1152. (uint32_t) num);
  1153. break;
  1154. }
  1155. videotrack->ar_mode = num;
  1156. break;
  1157. }
  1158. /* colourspace (only matters for raw video)
  1159. * fourcc */
  1160. case MATROSKA_ID_VIDEOCOLOURSPACE: {
  1161. uint64_t num;
  1162. if ((res = ebml_read_uint(matroska, &id,
  1163. &num)) < 0)
  1164. break;
  1165. videotrack->fourcc = num;
  1166. break;
  1167. }
  1168. default:
  1169. av_log(matroska->ctx, AV_LOG_INFO,
  1170. "Unknown video track header entry "
  1171. "0x%x - ignoring\n", id);
  1172. /* pass-through */
  1173. case EBML_ID_VOID:
  1174. res = ebml_read_skip(matroska);
  1175. break;
  1176. }
  1177. if (matroska->level_up) {
  1178. matroska->level_up--;
  1179. break;
  1180. }
  1181. }
  1182. break;
  1183. }
  1184. /* tracktype specific stuff for audio */
  1185. case MATROSKA_ID_TRACKAUDIO: {
  1186. MatroskaAudioTrack *audiotrack;
  1187. if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
  1188. av_log(matroska->ctx, AV_LOG_INFO,
  1189. "audio data in non-audio track - ignoring\n");
  1190. res = AVERROR_INVALIDDATA;
  1191. break;
  1192. } else if ((res = ebml_read_master(matroska, &id)) < 0)
  1193. break;
  1194. audiotrack = (MatroskaAudioTrack *)track;
  1195. while (res == 0) {
  1196. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1197. res = AVERROR_IO;
  1198. break;
  1199. } else if (matroska->level_up > 0) {
  1200. matroska->level_up--;
  1201. break;
  1202. }
  1203. switch (id) {
  1204. /* samplerate */
  1205. case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
  1206. double num;
  1207. if ((res = ebml_read_float(matroska, &id,
  1208. &num)) < 0)
  1209. break;
  1210. audiotrack->samplerate = num;
  1211. break;
  1212. }
  1213. /* bitdepth */
  1214. case MATROSKA_ID_AUDIOBITDEPTH: {
  1215. uint64_t num;
  1216. if ((res = ebml_read_uint(matroska, &id,
  1217. &num)) < 0)
  1218. break;
  1219. audiotrack->bitdepth = num;
  1220. break;
  1221. }
  1222. /* channels */
  1223. case MATROSKA_ID_AUDIOCHANNELS: {
  1224. uint64_t num;
  1225. if ((res = ebml_read_uint(matroska, &id,
  1226. &num)) < 0)
  1227. break;
  1228. audiotrack->channels = num;
  1229. break;
  1230. }
  1231. default:
  1232. av_log(matroska->ctx, AV_LOG_INFO,
  1233. "Unknown audio track header entry "
  1234. "0x%x - ignoring\n", id);
  1235. /* pass-through */
  1236. case EBML_ID_VOID:
  1237. res = ebml_read_skip(matroska);
  1238. break;
  1239. }
  1240. if (matroska->level_up) {
  1241. matroska->level_up--;
  1242. break;
  1243. }
  1244. }
  1245. break;
  1246. }
  1247. /* codec identifier */
  1248. case MATROSKA_ID_CODECID: {
  1249. char *text;
  1250. if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
  1251. break;
  1252. track->codec_id = text;
  1253. break;
  1254. }
  1255. /* codec private data */
  1256. case MATROSKA_ID_CODECPRIVATE: {
  1257. uint8_t *data;
  1258. int size;
  1259. if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
  1260. break;
  1261. track->codec_priv = data;
  1262. track->codec_priv_size = size;
  1263. break;
  1264. }
  1265. /* name of the codec */
  1266. case MATROSKA_ID_CODECNAME: {
  1267. char *text;
  1268. if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
  1269. break;
  1270. track->codec_name = text;
  1271. break;
  1272. }
  1273. /* name of this track */
  1274. case MATROSKA_ID_TRACKNAME: {
  1275. char *text;
  1276. if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
  1277. break;
  1278. track->name = text;
  1279. break;
  1280. }
  1281. /* language (matters for audio/subtitles, mostly) */
  1282. case MATROSKA_ID_TRACKLANGUAGE: {
  1283. char *text;
  1284. if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
  1285. break;
  1286. track->language = text;
  1287. break;
  1288. }
  1289. /* whether this is actually used */
  1290. case MATROSKA_ID_TRACKFLAGENABLED: {
  1291. uint64_t num;
  1292. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1293. break;
  1294. if (num)
  1295. track->flags |= MATROSKA_TRACK_ENABLED;
  1296. else
  1297. track->flags &= ~MATROSKA_TRACK_ENABLED;
  1298. break;
  1299. }
  1300. /* whether it's the default for this track type */
  1301. case MATROSKA_ID_TRACKFLAGDEFAULT: {
  1302. uint64_t num;
  1303. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1304. break;
  1305. if (num)
  1306. track->flags |= MATROSKA_TRACK_DEFAULT;
  1307. else
  1308. track->flags &= ~MATROSKA_TRACK_DEFAULT;
  1309. break;
  1310. }
  1311. /* lacing (like MPEG, where blocks don't end/start on frame
  1312. * boundaries) */
  1313. case MATROSKA_ID_TRACKFLAGLACING: {
  1314. uint64_t num;
  1315. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1316. break;
  1317. if (num)
  1318. track->flags |= MATROSKA_TRACK_LACING;
  1319. else
  1320. track->flags &= ~MATROSKA_TRACK_LACING;
  1321. break;
  1322. }
  1323. /* default length (in time) of one data block in this track */
  1324. case MATROSKA_ID_TRACKDEFAULTDURATION: {
  1325. uint64_t num;
  1326. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1327. break;
  1328. track->default_duration = num;
  1329. break;
  1330. }
  1331. default:
  1332. av_log(matroska->ctx, AV_LOG_INFO,
  1333. "Unknown track header entry 0x%x - ignoring\n", id);
  1334. /* pass-through */
  1335. case EBML_ID_VOID:
  1336. /* we ignore these because they're nothing useful. */
  1337. case MATROSKA_ID_CODECINFOURL:
  1338. case MATROSKA_ID_CODECDOWNLOADURL:
  1339. case MATROSKA_ID_TRACKMINCACHE:
  1340. case MATROSKA_ID_TRACKMAXCACHE:
  1341. res = ebml_read_skip(matroska);
  1342. break;
  1343. }
  1344. if (matroska->level_up) {
  1345. matroska->level_up--;
  1346. break;
  1347. }
  1348. }
  1349. return res;
  1350. }
  1351. static int
  1352. matroska_parse_tracks (MatroskaDemuxContext *matroska)
  1353. {
  1354. int res = 0;
  1355. uint32_t id;
  1356. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
  1357. while (res == 0) {
  1358. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1359. res = AVERROR_IO;
  1360. break;
  1361. } else if (matroska->level_up) {
  1362. matroska->level_up--;
  1363. break;
  1364. }
  1365. switch (id) {
  1366. /* one track within the "all-tracks" header */
  1367. case MATROSKA_ID_TRACKENTRY:
  1368. res = matroska_add_stream(matroska);
  1369. break;
  1370. default:
  1371. av_log(matroska->ctx, AV_LOG_INFO,
  1372. "Unknown entry 0x%x in track header\n", id);
  1373. /* fall-through */
  1374. case EBML_ID_VOID:
  1375. res = ebml_read_skip(matroska);
  1376. break;
  1377. }
  1378. if (matroska->level_up) {
  1379. matroska->level_up--;
  1380. break;
  1381. }
  1382. }
  1383. return res;
  1384. }
  1385. static int
  1386. matroska_parse_index (MatroskaDemuxContext *matroska)
  1387. {
  1388. int res = 0;
  1389. uint32_t id;
  1390. MatroskaDemuxIndex idx;
  1391. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
  1392. while (res == 0) {
  1393. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1394. res = AVERROR_IO;
  1395. break;
  1396. } else if (matroska->level_up) {
  1397. matroska->level_up--;
  1398. break;
  1399. }
  1400. switch (id) {
  1401. /* one single index entry ('point') */
  1402. case MATROSKA_ID_POINTENTRY:
  1403. if ((res = ebml_read_master(matroska, &id)) < 0)
  1404. break;
  1405. /* in the end, we hope to fill one entry with a
  1406. * timestamp, a file position and a tracknum */
  1407. idx.pos = (uint64_t) -1;
  1408. idx.time = (uint64_t) -1;
  1409. idx.track = (uint16_t) -1;
  1410. while (res == 0) {
  1411. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1412. res = AVERROR_IO;
  1413. break;
  1414. } else if (matroska->level_up) {
  1415. matroska->level_up--;
  1416. break;
  1417. }
  1418. switch (id) {
  1419. /* one single index entry ('point') */
  1420. case MATROSKA_ID_CUETIME: {
  1421. uint64_t time;
  1422. if ((res = ebml_read_uint(matroska, &id,
  1423. &time)) < 0)
  1424. break;
  1425. idx.time = time * matroska->time_scale;
  1426. break;
  1427. }
  1428. /* position in the file + track to which it
  1429. * belongs */
  1430. case MATROSKA_ID_CUETRACKPOSITION:
  1431. if ((res = ebml_read_master(matroska, &id)) < 0)
  1432. break;
  1433. while (res == 0) {
  1434. if (!(id = ebml_peek_id (matroska,
  1435. &matroska->level_up))) {
  1436. res = AVERROR_IO;
  1437. break;
  1438. } else if (matroska->level_up) {
  1439. matroska->level_up--;
  1440. break;
  1441. }
  1442. switch (id) {
  1443. /* track number */
  1444. case MATROSKA_ID_CUETRACK: {
  1445. uint64_t num;
  1446. if ((res = ebml_read_uint(matroska,
  1447. &id, &num)) < 0)
  1448. break;
  1449. idx.track = num;
  1450. break;
  1451. }
  1452. /* position in file */
  1453. case MATROSKA_ID_CUECLUSTERPOSITION: {
  1454. uint64_t num;
  1455. if ((res = ebml_read_uint(matroska,
  1456. &id, &num)) < 0)
  1457. break;
  1458. idx.pos = num;
  1459. break;
  1460. }
  1461. default:
  1462. av_log(matroska->ctx, AV_LOG_INFO,
  1463. "Unknown entry 0x%x in "
  1464. "CuesTrackPositions\n", id);
  1465. /* fall-through */
  1466. case EBML_ID_VOID:
  1467. res = ebml_read_skip(matroska);
  1468. break;
  1469. }
  1470. if (matroska->level_up) {
  1471. matroska->level_up--;
  1472. break;
  1473. }
  1474. }
  1475. break;
  1476. default:
  1477. av_log(matroska->ctx, AV_LOG_INFO,
  1478. "Unknown entry 0x%x in cuespoint "
  1479. "index\n", id);
  1480. /* fall-through */
  1481. case EBML_ID_VOID:
  1482. res = ebml_read_skip(matroska);
  1483. break;
  1484. }
  1485. if (matroska->level_up) {
  1486. matroska->level_up--;
  1487. break;
  1488. }
  1489. }
  1490. /* so let's see if we got what we wanted */
  1491. if (idx.pos != (uint64_t) -1 &&
  1492. idx.time != (uint64_t) -1 &&
  1493. idx.track != (uint16_t) -1) {
  1494. if (matroska->num_indexes % 32 == 0) {
  1495. /* re-allocate bigger index */
  1496. matroska->index =
  1497. av_realloc(matroska->index,
  1498. (matroska->num_indexes + 32) *
  1499. sizeof(MatroskaDemuxIndex));
  1500. }
  1501. matroska->index[matroska->num_indexes] = idx;
  1502. matroska->num_indexes++;
  1503. }
  1504. break;
  1505. default:
  1506. av_log(matroska->ctx, AV_LOG_INFO,
  1507. "Unknown entry 0x%x in cues header\n", id);
  1508. /* fall-through */
  1509. case EBML_ID_VOID:
  1510. res = ebml_read_skip(matroska);
  1511. break;
  1512. }
  1513. if (matroska->level_up) {
  1514. matroska->level_up--;
  1515. break;
  1516. }
  1517. }
  1518. return res;
  1519. }
  1520. static int
  1521. matroska_parse_metadata (MatroskaDemuxContext *matroska)
  1522. {
  1523. int res = 0;
  1524. uint32_t id;
  1525. while (res == 0) {
  1526. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1527. res = AVERROR_IO;
  1528. break;
  1529. } else if (matroska->level_up) {
  1530. matroska->level_up--;
  1531. break;
  1532. }
  1533. switch (id) {
  1534. /* Hm, this is unsupported... */
  1535. default:
  1536. av_log(matroska->ctx, AV_LOG_INFO,
  1537. "Unknown entry 0x%x in metadata header\n", id);
  1538. /* fall-through */
  1539. case EBML_ID_VOID:
  1540. res = ebml_read_skip(matroska);
  1541. break;
  1542. }
  1543. if (matroska->level_up) {
  1544. matroska->level_up--;
  1545. break;
  1546. }
  1547. }
  1548. return res;
  1549. }
  1550. static int
  1551. matroska_parse_seekhead (MatroskaDemuxContext *matroska)
  1552. {
  1553. int res = 0;
  1554. uint32_t id;
  1555. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
  1556. while (res == 0) {
  1557. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1558. res = AVERROR_IO;
  1559. break;
  1560. } else if (matroska->level_up) {
  1561. matroska->level_up--;
  1562. break;
  1563. }
  1564. switch (id) {
  1565. case MATROSKA_ID_SEEKENTRY: {
  1566. uint32_t seek_id = 0, peek_id_cache = 0;
  1567. uint64_t seek_pos = (uint64_t) -1, t;
  1568. if ((res = ebml_read_master(matroska, &id)) < 0)
  1569. break;
  1570. while (res == 0) {
  1571. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1572. res = AVERROR_IO;
  1573. break;
  1574. } else if (matroska->level_up) {
  1575. matroska->level_up--;
  1576. break;
  1577. }
  1578. switch (id) {
  1579. case MATROSKA_ID_SEEKID:
  1580. res = ebml_read_uint(matroska, &id, &t);
  1581. seek_id = t;
  1582. break;
  1583. case MATROSKA_ID_SEEKPOSITION:
  1584. res = ebml_read_uint(matroska, &id, &seek_pos);
  1585. break;
  1586. default:
  1587. av_log(matroska->ctx, AV_LOG_INFO,
  1588. "Unknown seekhead ID 0x%x\n", id);
  1589. /* fall-through */
  1590. case EBML_ID_VOID:
  1591. res = ebml_read_skip(matroska);
  1592. break;
  1593. }
  1594. if (matroska->level_up) {
  1595. matroska->level_up--;
  1596. break;
  1597. }
  1598. }
  1599. if (!seek_id || seek_pos == (uint64_t) -1) {
  1600. av_log(matroska->ctx, AV_LOG_INFO,
  1601. "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
  1602. seek_id, seek_pos);
  1603. break;
  1604. }
  1605. switch (seek_id) {
  1606. case MATROSKA_ID_CUES:
  1607. case MATROSKA_ID_TAGS: {
  1608. uint32_t level_up = matroska->level_up;
  1609. offset_t before_pos;
  1610. uint64_t length;
  1611. MatroskaLevel level;
  1612. /* remember the peeked ID and the current position */
  1613. peek_id_cache = matroska->peek_id;
  1614. before_pos = url_ftell(&matroska->ctx->pb);
  1615. /* seek */
  1616. if ((res = ebml_read_seek(matroska, seek_pos +
  1617. matroska->segment_start)) < 0)
  1618. return res;
  1619. /* we don't want to lose our seekhead level, so we add
  1620. * a dummy. This is a crude hack. */
  1621. if (matroska->num_levels == EBML_MAX_DEPTH) {
  1622. av_log(matroska->ctx, AV_LOG_INFO,
  1623. "Max EBML element depth (%d) reached, "
  1624. "cannot parse further.\n", EBML_MAX_DEPTH);
  1625. return AVERROR_UNKNOWN;
  1626. }
  1627. level.start = 0;
  1628. level.length = (uint64_t)-1;
  1629. matroska->levels[matroska->num_levels] = level;
  1630. matroska->num_levels++;
  1631. /* check ID */
  1632. if (!(id = ebml_peek_id (matroska,
  1633. &matroska->level_up)))
  1634. break;
  1635. if (id != seek_id) {
  1636. av_log(matroska->ctx, AV_LOG_INFO,
  1637. "We looked for ID=0x%x but got "
  1638. "ID=0x%x (pos=%"PRIu64")",
  1639. seek_id, id, seek_pos +
  1640. matroska->segment_start);
  1641. goto finish;
  1642. }
  1643. /* read master + parse */
  1644. if ((res = ebml_read_master(matroska, &id)) < 0)
  1645. break;
  1646. switch (id) {
  1647. case MATROSKA_ID_CUES:
  1648. if (!(res = matroska_parse_index(matroska)) ||
  1649. url_feof(&matroska->ctx->pb)) {
  1650. matroska->index_parsed = 1;
  1651. res = 0;
  1652. }
  1653. break;
  1654. case MATROSKA_ID_TAGS:
  1655. if (!(res = matroska_parse_metadata(matroska)) ||
  1656. url_feof(&matroska->ctx->pb)) {
  1657. matroska->metadata_parsed = 1;
  1658. res = 0;
  1659. }
  1660. break;
  1661. }
  1662. if (res < 0)
  1663. break;
  1664. finish:
  1665. /* remove dummy level */
  1666. while (matroska->num_levels) {
  1667. matroska->num_levels--;
  1668. length =
  1669. matroska->levels[matroska->num_levels].length;
  1670. if (length == (uint64_t)-1)
  1671. break;
  1672. }
  1673. /* seek back */
  1674. if ((res = ebml_read_seek(matroska, before_pos)) < 0)
  1675. return res;
  1676. matroska->peek_id = peek_id_cache;
  1677. matroska->level_up = level_up;
  1678. break;
  1679. }
  1680. default:
  1681. av_log(matroska->ctx, AV_LOG_INFO,
  1682. "Ignoring seekhead entry for ID=0x%x\n",
  1683. seek_id);
  1684. break;
  1685. }
  1686. break;
  1687. }
  1688. default:
  1689. av_log(matroska->ctx, AV_LOG_INFO,
  1690. "Unknown seekhead ID 0x%x\n", id);
  1691. /* fall-through */
  1692. case EBML_ID_VOID:
  1693. res = ebml_read_skip(matroska);
  1694. break;
  1695. }
  1696. if (matroska->level_up) {
  1697. matroska->level_up--;
  1698. break;
  1699. }
  1700. }
  1701. return res;
  1702. }
  1703. static int
  1704. matroska_read_header (AVFormatContext *s,
  1705. AVFormatParameters *ap)
  1706. {
  1707. MatroskaDemuxContext *matroska = s->priv_data;
  1708. char *doctype;
  1709. int version, last_level, res = 0;
  1710. uint32_t id;
  1711. matroska->ctx = s;
  1712. /* First read the EBML header. */
  1713. doctype = NULL;
  1714. if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
  1715. return res;
  1716. if ((doctype == NULL) || strcmp(doctype, "matroska")) {
  1717. av_log(matroska->ctx, AV_LOG_ERROR,
  1718. "Wrong EBML doctype ('%s' != 'matroska').\n",
  1719. doctype ? doctype : "(none)");
  1720. if (doctype)
  1721. av_free(doctype);
  1722. return AVERROR_NOFMT;
  1723. }
  1724. av_free(doctype);
  1725. if (version != 1) {
  1726. av_log(matroska->ctx, AV_LOG_ERROR,
  1727. "Matroska demuxer version 1 too old for file version %d\n",
  1728. version);
  1729. return AVERROR_NOFMT;
  1730. }
  1731. /* The next thing is a segment. */
  1732. while (1) {
  1733. if (!(id = ebml_peek_id(matroska, &last_level)))
  1734. return AVERROR_IO;
  1735. if (id == MATROSKA_ID_SEGMENT)
  1736. break;
  1737. /* oi! */
  1738. av_log(matroska->ctx, AV_LOG_INFO,
  1739. "Expected a Segment ID (0x%x), but received 0x%x!\n",
  1740. MATROSKA_ID_SEGMENT, id);
  1741. if ((res = ebml_read_skip(matroska)) < 0)
  1742. return res;
  1743. }
  1744. /* We now have a Matroska segment.
  1745. * Seeks are from the beginning of the segment,
  1746. * after the segment ID/length. */
  1747. if ((res = ebml_read_master(matroska, &id)) < 0)
  1748. return res;
  1749. matroska->segment_start = url_ftell(&s->pb);
  1750. matroska->time_scale = 1000000;
  1751. /* we've found our segment, start reading the different contents in here */
  1752. while (res == 0) {
  1753. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1754. res = AVERROR_IO;
  1755. break;
  1756. } else if (matroska->level_up) {
  1757. matroska->level_up--;
  1758. break;
  1759. }
  1760. switch (id) {
  1761. /* stream info */
  1762. case MATROSKA_ID_INFO: {
  1763. if ((res = ebml_read_master(matroska, &id)) < 0)
  1764. break;
  1765. res = matroska_parse_info(matroska);
  1766. break;
  1767. }
  1768. /* track info headers */
  1769. case MATROSKA_ID_TRACKS: {
  1770. if ((res = ebml_read_master(matroska, &id)) < 0)
  1771. break;
  1772. res = matroska_parse_tracks(matroska);
  1773. break;
  1774. }
  1775. /* stream index */
  1776. case MATROSKA_ID_CUES: {
  1777. if (!matroska->index_parsed) {
  1778. if ((res = ebml_read_master(matroska, &id)) < 0)
  1779. break;
  1780. res = matroska_parse_index(matroska);
  1781. } else
  1782. res = ebml_read_skip(matroska);
  1783. break;
  1784. }
  1785. /* metadata */
  1786. case MATROSKA_ID_TAGS: {
  1787. if (!matroska->metadata_parsed) {
  1788. if ((res = ebml_read_master(matroska, &id)) < 0)
  1789. break;
  1790. res = matroska_parse_metadata(matroska);
  1791. } else
  1792. res = ebml_read_skip(matroska);
  1793. break;
  1794. }
  1795. /* file index (if seekable, seek to Cues/Tags to parse it) */
  1796. case MATROSKA_ID_SEEKHEAD: {
  1797. if ((res = ebml_read_master(matroska, &id)) < 0)
  1798. break;
  1799. res = matroska_parse_seekhead(matroska);
  1800. break;
  1801. }
  1802. case MATROSKA_ID_CLUSTER: {
  1803. /* Do not read the master - this will be done in the next
  1804. * call to matroska_read_packet. */
  1805. res = 1;
  1806. break;
  1807. }
  1808. default:
  1809. av_log(matroska->ctx, AV_LOG_INFO,
  1810. "Unknown matroska file header ID 0x%x\n", id);
  1811. /* fall-through */
  1812. case EBML_ID_VOID:
  1813. res = ebml_read_skip(matroska);
  1814. break;
  1815. }
  1816. if (matroska->level_up) {
  1817. matroska->level_up--;
  1818. break;
  1819. }
  1820. }
  1821. if (res < 0)
  1822. return res;
  1823. /* Have we found a cluster? */
  1824. if (res == 1) {
  1825. int i, j;
  1826. enum CodecID codec_id= CODEC_ID_NONE;
  1827. MatroskaTrack *track;
  1828. AVStream *st;
  1829. for (i = 0; i < matroska->num_tracks; i++) {
  1830. void *extradata = NULL;
  1831. int extradata_size = 0;
  1832. track = matroska->tracks[i];
  1833. /* libavformat does not really support subtitles.
  1834. * Also apply some sanity checks. */
  1835. if ((track->type == MATROSKA_TRACK_TYPE_SUBTITLE) ||
  1836. (track->codec_id == NULL))
  1837. continue;
  1838. for(j=0; codec_tags[j].str; j++){
  1839. if(!strcmp(codec_tags[j].str, track->codec_id)){
  1840. codec_id= codec_tags[j].id;
  1841. break;
  1842. }
  1843. }
  1844. /* Set the FourCC from the CodecID. */
  1845. /* This is the MS compatibility mode which stores a
  1846. * BITMAPINFOHEADER in the CodecPrivate. */
  1847. if (!strcmp(track->codec_id,
  1848. MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
  1849. (track->codec_priv_size >= 40) &&
  1850. (track->codec_priv != NULL)) {
  1851. unsigned char *p;
  1852. /* Offset of biCompression. Stored in LE. */
  1853. p = (unsigned char *)track->codec_priv + 16;
  1854. ((MatroskaVideoTrack *)track)->fourcc = (p[3] << 24) |
  1855. (p[2] << 16) | (p[1] << 8) | p[0];
  1856. codec_id = codec_get_bmp_id(((MatroskaVideoTrack *)track)->fourcc);
  1857. }
  1858. /* This is the MS compatibility mode which stores a
  1859. * WAVEFORMATEX in the CodecPrivate. */
  1860. else if (!strcmp(track->codec_id,
  1861. MATROSKA_CODEC_ID_AUDIO_ACM) &&
  1862. (track->codec_priv_size >= 18) &&
  1863. (track->codec_priv != NULL)) {
  1864. unsigned char *p;
  1865. uint16_t tag;
  1866. /* Offset of wFormatTag. Stored in LE. */
  1867. p = (unsigned char *)track->codec_priv;
  1868. tag = (p[1] << 8) | p[0];
  1869. codec_id = codec_get_wav_id(tag);
  1870. }
  1871. if (codec_id == CODEC_ID_NONE) {
  1872. av_log(matroska->ctx, AV_LOG_INFO,
  1873. "Unknown/unsupported CodecID %s.\n",
  1874. track->codec_id);
  1875. }
  1876. track->stream_index = matroska->num_streams;
  1877. matroska->num_streams++;
  1878. st = av_new_stream(s, track->stream_index);
  1879. if (st == NULL)
  1880. return AVERROR_NOMEM;
  1881. av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
  1882. st->codec->codec_id = codec_id;
  1883. if(extradata){
  1884. st->codec->extradata = extradata;
  1885. st->codec->extradata_size = extradata_size;
  1886. } else if(track->codec_priv && track->codec_priv_size > 0){
  1887. st->codec->extradata = av_malloc(track->codec_priv_size);
  1888. if(st->codec->extradata == NULL)
  1889. return AVERROR_NOMEM;
  1890. st->codec->extradata_size = track->codec_priv_size;
  1891. memcpy(st->codec->extradata, track->codec_priv,
  1892. track->codec_priv_size);
  1893. }
  1894. if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
  1895. MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
  1896. st->codec->codec_type = CODEC_TYPE_VIDEO;
  1897. st->codec->codec_tag = videotrack->fourcc;
  1898. st->codec->width = videotrack->pixel_width;
  1899. st->codec->height = videotrack->pixel_height;
  1900. if (videotrack->display_width == 0)
  1901. videotrack->display_width= videotrack->pixel_width;
  1902. if (videotrack->display_height == 0)
  1903. videotrack->display_height= videotrack->pixel_height;
  1904. av_reduce(&st->codec->sample_aspect_ratio.num,
  1905. &st->codec->sample_aspect_ratio.den,
  1906. st->codec->height * videotrack->display_width,
  1907. st->codec-> width * videotrack->display_height,
  1908. 255);
  1909. } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  1910. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
  1911. st->codec->codec_type = CODEC_TYPE_AUDIO;
  1912. st->codec->sample_rate = audiotrack->samplerate;
  1913. st->codec->channels = audiotrack->channels;
  1914. } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
  1915. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  1916. }
  1917. /* What do we do with private data? E.g. for Vorbis. */
  1918. }
  1919. }
  1920. return 0;
  1921. }
  1922. static int
  1923. matroska_find_track_by_num (MatroskaDemuxContext *matroska,
  1924. int num)
  1925. {
  1926. int i;
  1927. for (i = 0; i < matroska->num_tracks; i++)
  1928. if (matroska->tracks[i]->num == num)
  1929. return i;
  1930. return -1;
  1931. }
  1932. static int
  1933. matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
  1934. uint64_t cluster_time)
  1935. {
  1936. int res = 0;
  1937. uint32_t id;
  1938. AVPacket *pkt;
  1939. int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
  1940. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
  1941. while (res == 0) {
  1942. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1943. res = AVERROR_IO;
  1944. break;
  1945. } else if (matroska->level_up) {
  1946. matroska->level_up--;
  1947. break;
  1948. }
  1949. switch (id) {
  1950. /* one block inside the group. Note, block parsing is one
  1951. * of the harder things, so this code is a bit complicated.
  1952. * See http://www.matroska.org/ for documentation. */
  1953. case MATROSKA_ID_BLOCK: {
  1954. uint8_t *data, *origdata;
  1955. int size;
  1956. int16_t block_time;
  1957. uint32_t *lace_size = NULL;
  1958. int n, track, flags, laces = 0;
  1959. uint64_t num;
  1960. int64_t pos= url_ftell(&matroska->ctx->pb);
  1961. if ((res = ebml_read_binary(matroska, &id, &data, &size)) < 0)
  1962. break;
  1963. origdata = data;
  1964. /* first byte(s): blocknum */
  1965. if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
  1966. av_log(matroska->ctx, AV_LOG_ERROR,
  1967. "EBML block data error\n");
  1968. av_free(origdata);
  1969. break;
  1970. }
  1971. data += n;
  1972. size -= n;
  1973. /* fetch track from num */
  1974. track = matroska_find_track_by_num(matroska, num);
  1975. if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
  1976. av_log(matroska->ctx, AV_LOG_INFO,
  1977. "Invalid stream %d or size %u\n", track, size);
  1978. av_free(origdata);
  1979. break;
  1980. }
  1981. if(matroska->ctx->streams[ matroska->tracks[track]->stream_index ]->discard >= AVDISCARD_ALL){
  1982. av_free(origdata);
  1983. break;
  1984. }
  1985. /* block_time (relative to cluster time) */
  1986. block_time = (data[0] << 8) | data[1];
  1987. data += 2;
  1988. size -= 2;
  1989. flags = *data;
  1990. data += 1;
  1991. size -= 1;
  1992. switch ((flags & 0x06) >> 1) {
  1993. case 0x0: /* no lacing */
  1994. laces = 1;
  1995. lace_size = av_mallocz(sizeof(int));
  1996. lace_size[0] = size;
  1997. break;
  1998. case 0x1: /* xiph lacing */
  1999. case 0x2: /* fixed-size lacing */
  2000. case 0x3: /* EBML lacing */
  2001. if (size == 0) {
  2002. res = -1;
  2003. break;
  2004. }
  2005. laces = (*data) + 1;
  2006. data += 1;
  2007. size -= 1;
  2008. lace_size = av_mallocz(laces * sizeof(int));
  2009. switch ((flags & 0x06) >> 1) {
  2010. case 0x1: /* xiph lacing */ {
  2011. uint8_t temp;
  2012. uint32_t total = 0;
  2013. for (n = 0; res == 0 && n < laces - 1; n++) {
  2014. while (1) {
  2015. if (size == 0) {
  2016. res = -1;
  2017. break;
  2018. }
  2019. temp = *data;
  2020. lace_size[n] += temp;
  2021. data += 1;
  2022. size -= 1;
  2023. if (temp != 0xff)
  2024. break;
  2025. }
  2026. total += lace_size[n];
  2027. }
  2028. lace_size[n] = size - total;
  2029. break;
  2030. }
  2031. case 0x2: /* fixed-size lacing */
  2032. for (n = 0; n < laces; n++)
  2033. lace_size[n] = size / laces;
  2034. break;
  2035. case 0x3: /* EBML lacing */ {
  2036. uint32_t total;
  2037. n = matroska_ebmlnum_uint(data, size, &num);
  2038. if (n < 0) {
  2039. av_log(matroska->ctx, AV_LOG_INFO,
  2040. "EBML block data error\n");
  2041. break;
  2042. }
  2043. data += n;
  2044. size -= n;
  2045. total = lace_size[0] = num;
  2046. for (n = 1; res == 0 && n < laces - 1; n++) {
  2047. int64_t snum;
  2048. int r;
  2049. r = matroska_ebmlnum_sint (data, size,
  2050. &snum);
  2051. if (r < 0) {
  2052. av_log(matroska->ctx, AV_LOG_INFO,
  2053. "EBML block data error\n");
  2054. break;
  2055. }
  2056. data += r;
  2057. size -= r;
  2058. lace_size[n] = lace_size[n - 1] + snum;
  2059. total += lace_size[n];
  2060. }
  2061. lace_size[n] = size - total;
  2062. break;
  2063. }
  2064. }
  2065. break;
  2066. }
  2067. if (res == 0) {
  2068. for (n = 0; n < laces; n++) {
  2069. uint64_t timecode = AV_NOPTS_VALUE;
  2070. pkt = av_mallocz(sizeof(AVPacket));
  2071. /* XXX: prevent data copy... */
  2072. if (av_new_packet(pkt,lace_size[n]) < 0) {
  2073. res = AVERROR_NOMEM;
  2074. break;
  2075. }
  2076. if (cluster_time != (uint64_t)-1 && n == 0) {
  2077. if (cluster_time + block_time >= 0)
  2078. timecode = cluster_time + block_time;
  2079. }
  2080. /* FIXME: duration */
  2081. memcpy(pkt->data, data, lace_size[n]);
  2082. data += lace_size[n];
  2083. if (n == 0)
  2084. pkt->flags = is_keyframe;
  2085. pkt->stream_index =
  2086. matroska->tracks[track]->stream_index;
  2087. pkt->pts = timecode;
  2088. pkt->pos= pos;
  2089. matroska_queue_packet(matroska, pkt);
  2090. }
  2091. }
  2092. av_free(lace_size);
  2093. av_free(origdata);
  2094. break;
  2095. }
  2096. case MATROSKA_ID_BLOCKDURATION: {
  2097. uint64_t num;
  2098. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  2099. break;
  2100. av_log(matroska->ctx, AV_LOG_INFO,
  2101. "FIXME: implement support for BlockDuration\n");
  2102. break;
  2103. }
  2104. case MATROSKA_ID_BLOCKREFERENCE:
  2105. /* We've found a reference, so not even the first frame in
  2106. * the lace is a key frame. */
  2107. is_keyframe = 0;
  2108. if (last_num_packets != matroska->num_packets)
  2109. matroska->packets[last_num_packets]->flags = 0;
  2110. res = ebml_read_skip(matroska);
  2111. break;
  2112. default:
  2113. av_log(matroska->ctx, AV_LOG_INFO,
  2114. "Unknown entry 0x%x in blockgroup data\n", id);
  2115. /* fall-through */
  2116. case EBML_ID_VOID:
  2117. res = ebml_read_skip(matroska);
  2118. break;
  2119. }
  2120. if (matroska->level_up) {
  2121. matroska->level_up--;
  2122. break;
  2123. }
  2124. }
  2125. return res;
  2126. }
  2127. static int
  2128. matroska_parse_cluster (MatroskaDemuxContext *matroska)
  2129. {
  2130. int res = 0;
  2131. uint32_t id;
  2132. uint64_t cluster_time = 0;
  2133. av_log(matroska->ctx, AV_LOG_DEBUG,
  2134. "parsing cluster at %"PRId64"\n", url_ftell(&matroska->ctx->pb));
  2135. while (res == 0) {
  2136. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  2137. res = AVERROR_IO;
  2138. break;
  2139. } else if (matroska->level_up) {
  2140. matroska->level_up--;
  2141. break;
  2142. }
  2143. switch (id) {
  2144. /* cluster timecode */
  2145. case MATROSKA_ID_CLUSTERTIMECODE: {
  2146. uint64_t num;
  2147. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  2148. break;
  2149. cluster_time = num;
  2150. break;
  2151. }
  2152. /* a group of blocks inside a cluster */
  2153. case MATROSKA_ID_BLOCKGROUP:
  2154. if ((res = ebml_read_master(matroska, &id)) < 0)
  2155. break;
  2156. res = matroska_parse_blockgroup(matroska, cluster_time);
  2157. break;
  2158. default:
  2159. av_log(matroska->ctx, AV_LOG_INFO,
  2160. "Unknown entry 0x%x in cluster data\n", id);
  2161. /* fall-through */
  2162. case EBML_ID_VOID:
  2163. res = ebml_read_skip(matroska);
  2164. break;
  2165. }
  2166. if (matroska->level_up) {
  2167. matroska->level_up--;
  2168. break;
  2169. }
  2170. }
  2171. return res;
  2172. }
  2173. static int
  2174. matroska_read_packet (AVFormatContext *s,
  2175. AVPacket *pkt)
  2176. {
  2177. MatroskaDemuxContext *matroska = s->priv_data;
  2178. int res = 0;
  2179. uint32_t id;
  2180. /* Do we still have a packet queued? */
  2181. if (matroska_deliver_packet(matroska, pkt) == 0)
  2182. return 0;
  2183. /* Have we already reached the end? */
  2184. if (matroska->done)
  2185. return AVERROR_IO;
  2186. while (res == 0) {
  2187. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  2188. res = AVERROR_IO;
  2189. break;
  2190. } else if (matroska->level_up) {
  2191. matroska->level_up--;
  2192. break;
  2193. }
  2194. switch (id) {
  2195. case MATROSKA_ID_CLUSTER:
  2196. if ((res = ebml_read_master(matroska, &id)) < 0)
  2197. break;
  2198. if ((res = matroska_parse_cluster(matroska)) == 0)
  2199. res = 1; /* Parsed one cluster, let's get out. */
  2200. break;
  2201. default:
  2202. case EBML_ID_VOID:
  2203. res = ebml_read_skip(matroska);
  2204. break;
  2205. }
  2206. if (matroska->level_up) {
  2207. matroska->level_up--;
  2208. break;
  2209. }
  2210. }
  2211. if (res == -1)
  2212. matroska->done = 1;
  2213. return matroska_deliver_packet(matroska, pkt);
  2214. }
  2215. static int
  2216. matroska_read_close (AVFormatContext *s)
  2217. {
  2218. MatroskaDemuxContext *matroska = s->priv_data;
  2219. int n = 0;
  2220. if (matroska->writing_app)
  2221. av_free(matroska->writing_app);
  2222. if (matroska->muxing_app)
  2223. av_free(matroska->muxing_app);
  2224. if (matroska->index)
  2225. av_free(matroska->index);
  2226. if (matroska->packets != NULL) {
  2227. for (n = 0; n < matroska->num_packets; n++) {
  2228. av_free_packet(matroska->packets[n]);
  2229. av_free(matroska->packets[n]);
  2230. }
  2231. av_free(matroska->packets);
  2232. }
  2233. for (n = 0; n < matroska->num_tracks; n++) {
  2234. MatroskaTrack *track = matroska->tracks[n];
  2235. if (track->codec_id)
  2236. av_free(track->codec_id);
  2237. if (track->codec_name)
  2238. av_free(track->codec_name);
  2239. if (track->codec_priv)
  2240. av_free(track->codec_priv);
  2241. if (track->name)
  2242. av_free(track->name);
  2243. if (track->language)
  2244. av_free(track->language);
  2245. av_free(track);
  2246. }
  2247. memset(matroska, 0, sizeof(MatroskaDemuxContext));
  2248. return 0;
  2249. }
  2250. AVInputFormat matroska_demuxer = {
  2251. "matroska",
  2252. "Matroska file format",
  2253. sizeof(MatroskaDemuxContext),
  2254. matroska_probe,
  2255. matroska_read_header,
  2256. matroska_read_packet,
  2257. matroska_read_close,
  2258. };