mov.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664
  1. /*
  2. * MOV demuxer
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <limits.h>
  23. //#define DEBUG
  24. //#define MOV_EXPORT_ALL_METADATA
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/intfloat_readwrite.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/dict.h"
  30. #include "avformat.h"
  31. #include "avio_internal.h"
  32. #include "riff.h"
  33. #include "isom.h"
  34. #include "libavcodec/get_bits.h"
  35. #if CONFIG_ZLIB
  36. #include <zlib.h>
  37. #endif
  38. /*
  39. * First version by Francois Revol revol@free.fr
  40. * Seek function by Gael Chardon gael.dev@4now.net
  41. *
  42. * Features and limitations:
  43. * - reads most of the QT files I have (at least the structure),
  44. * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
  45. * - the code is quite ugly... maybe I won't do it recursive next time :-)
  46. *
  47. * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
  48. * when coding this :) (it's a writer anyway)
  49. *
  50. * Reference documents:
  51. * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
  52. * Apple:
  53. * http://developer.apple.com/documentation/QuickTime/QTFF/
  54. * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
  55. * QuickTime is a trademark of Apple (AFAIK :))
  56. */
  57. #include "qtpalette.h"
  58. #undef NDEBUG
  59. #include <assert.h>
  60. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  61. /* those functions parse an atom */
  62. /* return code:
  63. 0: continue to parse next atom
  64. <0: error occurred, exit
  65. */
  66. /* links atom IDs to parse functions */
  67. typedef struct MOVParseTableEntry {
  68. uint32_t type;
  69. int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
  70. } MOVParseTableEntry;
  71. static const MOVParseTableEntry mov_default_parse_table[];
  72. static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb, unsigned len, const char *type)
  73. {
  74. char buf[16];
  75. avio_rb16(pb); // unknown
  76. snprintf(buf, sizeof(buf), "%d", avio_rb16(pb));
  77. av_dict_set(&c->fc->metadata, type, buf, 0);
  78. avio_rb16(pb); // total tracks/discs
  79. return 0;
  80. }
  81. static const uint32_t mac_to_unicode[128] = {
  82. 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
  83. 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
  84. 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
  85. 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
  86. 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
  87. 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
  88. 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
  89. 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
  90. 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
  91. 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
  92. 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
  93. 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
  94. 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
  95. 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
  96. 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
  97. 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
  98. };
  99. static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
  100. char *dst, int dstlen)
  101. {
  102. char *p = dst;
  103. char *end = dst+dstlen-1;
  104. int i;
  105. for (i = 0; i < len; i++) {
  106. uint8_t t, c = avio_r8(pb);
  107. if (c < 0x80 && p < end)
  108. *p++ = c;
  109. else
  110. PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
  111. }
  112. *p = 0;
  113. return p - dst;
  114. }
  115. static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  116. {
  117. #ifdef MOV_EXPORT_ALL_METADATA
  118. char tmp_key[5];
  119. #endif
  120. char str[1024], key2[16], language[4] = {0};
  121. const char *key = NULL;
  122. uint16_t str_size, langcode = 0;
  123. uint32_t data_type = 0;
  124. int (*parse)(MOVContext*, AVIOContext*, unsigned, const char *) = NULL;
  125. switch (atom.type) {
  126. case MKTAG(0xa9,'n','a','m'): key = "title"; break;
  127. case MKTAG(0xa9,'a','u','t'):
  128. case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
  129. case MKTAG( 'a','A','R','T'): key = "album_artist";break;
  130. case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
  131. case MKTAG( 'c','p','r','t'):
  132. case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
  133. case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
  134. case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
  135. case MKTAG(0xa9,'c','m','t'):
  136. case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
  137. case MKTAG(0xa9,'a','l','b'): key = "album"; break;
  138. case MKTAG(0xa9,'d','a','y'): key = "date"; break;
  139. case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
  140. case MKTAG(0xa9,'t','o','o'):
  141. case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
  142. case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
  143. case MKTAG( 'd','e','s','c'): key = "description";break;
  144. case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
  145. case MKTAG( 't','v','s','h'): key = "show"; break;
  146. case MKTAG( 't','v','e','n'): key = "episode_id";break;
  147. case MKTAG( 't','v','n','n'): key = "network"; break;
  148. case MKTAG( 't','r','k','n'): key = "track";
  149. parse = mov_metadata_track_or_disc_number; break;
  150. case MKTAG( 'd','i','s','k'): key = "disc";
  151. parse = mov_metadata_track_or_disc_number; break;
  152. }
  153. if (c->itunes_metadata && atom.size > 8) {
  154. int data_size = avio_rb32(pb);
  155. int tag = avio_rl32(pb);
  156. if (tag == MKTAG('d','a','t','a')) {
  157. data_type = avio_rb32(pb); // type
  158. avio_rb32(pb); // unknown
  159. str_size = data_size - 16;
  160. atom.size -= 16;
  161. } else return 0;
  162. } else if (atom.size > 4 && key && !c->itunes_metadata) {
  163. str_size = avio_rb16(pb); // string length
  164. langcode = avio_rb16(pb);
  165. ff_mov_lang_to_iso639(langcode, language);
  166. atom.size -= 4;
  167. } else
  168. str_size = atom.size;
  169. #ifdef MOV_EXPORT_ALL_METADATA
  170. if (!key) {
  171. snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
  172. key = tmp_key;
  173. }
  174. #endif
  175. if (!key)
  176. return 0;
  177. if (atom.size < 0)
  178. return -1;
  179. str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
  180. if (parse)
  181. parse(c, pb, str_size, key);
  182. else {
  183. if (data_type == 3 || (data_type == 0 && langcode < 0x800)) { // MAC Encoded
  184. mov_read_mac_string(c, pb, str_size, str, sizeof(str));
  185. } else {
  186. avio_read(pb, str, str_size);
  187. str[str_size] = 0;
  188. }
  189. av_dict_set(&c->fc->metadata, key, str, 0);
  190. if (*language && strcmp(language, "und")) {
  191. snprintf(key2, sizeof(key2), "%s-%s", key, language);
  192. av_dict_set(&c->fc->metadata, key2, str, 0);
  193. }
  194. }
  195. av_dlog(c->fc, "lang \"%3s\" ", language);
  196. av_dlog(c->fc, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
  197. key, str, (char*)&atom.type, str_size, atom.size);
  198. return 0;
  199. }
  200. static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  201. {
  202. int64_t start;
  203. int i, nb_chapters, str_len, version;
  204. char str[256+1];
  205. if ((atom.size -= 5) < 0)
  206. return 0;
  207. version = avio_r8(pb);
  208. avio_rb24(pb);
  209. if (version)
  210. avio_rb32(pb); // ???
  211. nb_chapters = avio_r8(pb);
  212. for (i = 0; i < nb_chapters; i++) {
  213. if (atom.size < 9)
  214. return 0;
  215. start = avio_rb64(pb);
  216. str_len = avio_r8(pb);
  217. if ((atom.size -= 9+str_len) < 0)
  218. return 0;
  219. avio_read(pb, str, str_len);
  220. str[str_len] = 0;
  221. ff_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
  222. }
  223. return 0;
  224. }
  225. static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  226. {
  227. int64_t total_size = 0;
  228. MOVAtom a;
  229. int i;
  230. if (atom.size < 0)
  231. atom.size = INT64_MAX;
  232. while (total_size + 8 < atom.size && !url_feof(pb)) {
  233. int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
  234. a.size = atom.size;
  235. a.type=0;
  236. if(atom.size >= 8) {
  237. a.size = avio_rb32(pb);
  238. a.type = avio_rl32(pb);
  239. }
  240. av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
  241. a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
  242. total_size += 8;
  243. if (a.size == 1) { /* 64 bit extended size */
  244. a.size = avio_rb64(pb) - 8;
  245. total_size += 8;
  246. }
  247. if (a.size == 0) {
  248. a.size = atom.size - total_size;
  249. if (a.size <= 8)
  250. break;
  251. }
  252. a.size -= 8;
  253. if(a.size < 0)
  254. break;
  255. a.size = FFMIN(a.size, atom.size - total_size);
  256. for (i = 0; mov_default_parse_table[i].type; i++)
  257. if (mov_default_parse_table[i].type == a.type) {
  258. parse = mov_default_parse_table[i].parse;
  259. break;
  260. }
  261. // container is user data
  262. if (!parse && (atom.type == MKTAG('u','d','t','a') ||
  263. atom.type == MKTAG('i','l','s','t')))
  264. parse = mov_read_udta_string;
  265. if (!parse) { /* skip leaf atoms data */
  266. avio_skip(pb, a.size);
  267. } else {
  268. int64_t start_pos = avio_tell(pb);
  269. int64_t left;
  270. int err = parse(c, pb, a);
  271. if (err < 0)
  272. return err;
  273. if (c->found_moov && c->found_mdat &&
  274. (!pb->seekable || start_pos + a.size == avio_size(pb)))
  275. return 0;
  276. left = a.size - avio_tell(pb) + start_pos;
  277. if (left > 0) /* skip garbage at atom end */
  278. avio_skip(pb, left);
  279. }
  280. total_size += a.size;
  281. }
  282. if (total_size < atom.size && atom.size < 0x7ffff)
  283. avio_skip(pb, atom.size - total_size);
  284. return 0;
  285. }
  286. static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  287. {
  288. AVStream *st;
  289. MOVStreamContext *sc;
  290. int entries, i, j;
  291. if (c->fc->nb_streams < 1)
  292. return 0;
  293. st = c->fc->streams[c->fc->nb_streams-1];
  294. sc = st->priv_data;
  295. avio_rb32(pb); // version + flags
  296. entries = avio_rb32(pb);
  297. if (entries >= UINT_MAX / sizeof(*sc->drefs))
  298. return -1;
  299. sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
  300. if (!sc->drefs)
  301. return AVERROR(ENOMEM);
  302. sc->drefs_count = entries;
  303. for (i = 0; i < sc->drefs_count; i++) {
  304. MOVDref *dref = &sc->drefs[i];
  305. uint32_t size = avio_rb32(pb);
  306. int64_t next = avio_tell(pb) + size - 4;
  307. if (size < 12)
  308. return -1;
  309. dref->type = avio_rl32(pb);
  310. avio_rb32(pb); // version + flags
  311. av_dlog(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
  312. if (dref->type == MKTAG('a','l','i','s') && size > 150) {
  313. /* macintosh alias record */
  314. uint16_t volume_len, len;
  315. int16_t type;
  316. avio_skip(pb, 10);
  317. volume_len = avio_r8(pb);
  318. volume_len = FFMIN(volume_len, 27);
  319. avio_read(pb, dref->volume, 27);
  320. dref->volume[volume_len] = 0;
  321. av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
  322. avio_skip(pb, 12);
  323. len = avio_r8(pb);
  324. len = FFMIN(len, 63);
  325. avio_read(pb, dref->filename, 63);
  326. dref->filename[len] = 0;
  327. av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
  328. avio_skip(pb, 16);
  329. /* read next level up_from_alias/down_to_target */
  330. dref->nlvl_from = avio_rb16(pb);
  331. dref->nlvl_to = avio_rb16(pb);
  332. av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
  333. dref->nlvl_from, dref->nlvl_to);
  334. avio_skip(pb, 16);
  335. for (type = 0; type != -1 && avio_tell(pb) < next; ) {
  336. type = avio_rb16(pb);
  337. len = avio_rb16(pb);
  338. av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
  339. if (len&1)
  340. len += 1;
  341. if (type == 2) { // absolute path
  342. av_free(dref->path);
  343. dref->path = av_mallocz(len+1);
  344. if (!dref->path)
  345. return AVERROR(ENOMEM);
  346. avio_read(pb, dref->path, len);
  347. if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
  348. len -= volume_len;
  349. memmove(dref->path, dref->path+volume_len, len);
  350. dref->path[len] = 0;
  351. }
  352. for (j = 0; j < len; j++)
  353. if (dref->path[j] == ':')
  354. dref->path[j] = '/';
  355. av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
  356. } else if (type == 0) { // directory name
  357. av_free(dref->dir);
  358. dref->dir = av_malloc(len+1);
  359. if (!dref->dir)
  360. return AVERROR(ENOMEM);
  361. avio_read(pb, dref->dir, len);
  362. dref->dir[len] = 0;
  363. for (j = 0; j < len; j++)
  364. if (dref->dir[j] == ':')
  365. dref->dir[j] = '/';
  366. av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
  367. } else
  368. avio_skip(pb, len);
  369. }
  370. }
  371. avio_seek(pb, next, SEEK_SET);
  372. }
  373. return 0;
  374. }
  375. static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  376. {
  377. AVStream *st;
  378. uint32_t type;
  379. uint32_t av_unused ctype;
  380. if (c->fc->nb_streams < 1) // meta before first trak
  381. return 0;
  382. st = c->fc->streams[c->fc->nb_streams-1];
  383. avio_r8(pb); /* version */
  384. avio_rb24(pb); /* flags */
  385. /* component type */
  386. ctype = avio_rl32(pb);
  387. type = avio_rl32(pb); /* component subtype */
  388. av_dlog(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
  389. av_dlog(c->fc, "stype= %.4s\n", (char*)&type);
  390. if (type == MKTAG('v','i','d','e'))
  391. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  392. else if(type == MKTAG('s','o','u','n'))
  393. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  394. else if(type == MKTAG('m','1','a',' '))
  395. st->codec->codec_id = CODEC_ID_MP2;
  396. else if((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
  397. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  398. avio_rb32(pb); /* component manufacture */
  399. avio_rb32(pb); /* component flags */
  400. avio_rb32(pb); /* component flags mask */
  401. return 0;
  402. }
  403. int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb, MOVAtom atom)
  404. {
  405. AVStream *st;
  406. int tag;
  407. if (fc->nb_streams < 1)
  408. return 0;
  409. st = fc->streams[fc->nb_streams-1];
  410. avio_rb32(pb); /* version + flags */
  411. ff_mp4_read_descr(fc, pb, &tag);
  412. if (tag == MP4ESDescrTag) {
  413. avio_rb16(pb); /* ID */
  414. avio_r8(pb); /* priority */
  415. } else
  416. avio_rb16(pb); /* ID */
  417. ff_mp4_read_descr(fc, pb, &tag);
  418. if (tag == MP4DecConfigDescrTag)
  419. ff_mp4_read_dec_config_descr(fc, st, pb);
  420. return 0;
  421. }
  422. static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  423. {
  424. return ff_mov_read_esds(c->fc, pb, atom);
  425. }
  426. static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  427. {
  428. AVStream *st;
  429. int ac3info, acmod, lfeon, bsmod;
  430. if (c->fc->nb_streams < 1)
  431. return 0;
  432. st = c->fc->streams[c->fc->nb_streams-1];
  433. ac3info = avio_rb24(pb);
  434. bsmod = (ac3info >> 14) & 0x7;
  435. acmod = (ac3info >> 11) & 0x7;
  436. lfeon = (ac3info >> 10) & 0x1;
  437. st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
  438. st->codec->audio_service_type = bsmod;
  439. if (st->codec->channels > 1 && bsmod == 0x7)
  440. st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  441. return 0;
  442. }
  443. static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  444. {
  445. AVStream *st;
  446. if (c->fc->nb_streams < 1)
  447. return 0;
  448. st = c->fc->streams[c->fc->nb_streams-1];
  449. ff_get_wav_header(pb, st->codec, atom.size);
  450. return 0;
  451. }
  452. static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  453. {
  454. const int num = avio_rb32(pb);
  455. const int den = avio_rb32(pb);
  456. AVStream *st;
  457. if (c->fc->nb_streams < 1)
  458. return 0;
  459. st = c->fc->streams[c->fc->nb_streams-1];
  460. if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
  461. (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
  462. av_log(c->fc, AV_LOG_WARNING,
  463. "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
  464. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  465. num, den);
  466. } else if (den != 0) {
  467. st->sample_aspect_ratio.num = num;
  468. st->sample_aspect_ratio.den = den;
  469. }
  470. return 0;
  471. }
  472. /* this atom contains actual media data */
  473. static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  474. {
  475. if(atom.size == 0) /* wrong one (MP4) */
  476. return 0;
  477. c->found_mdat=1;
  478. return 0; /* now go for moov */
  479. }
  480. /* read major brand, minor version and compatible brands and store them as metadata */
  481. static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  482. {
  483. uint32_t minor_ver;
  484. int comp_brand_size;
  485. char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
  486. char* comp_brands_str;
  487. uint8_t type[5] = {0};
  488. avio_read(pb, type, 4);
  489. if (strcmp(type, "qt "))
  490. c->isom = 1;
  491. av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
  492. av_dict_set(&c->fc->metadata, "major_brand", type, 0);
  493. minor_ver = avio_rb32(pb); /* minor version */
  494. snprintf(minor_ver_str, sizeof(minor_ver_str), "%d", minor_ver);
  495. av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);
  496. comp_brand_size = atom.size - 8;
  497. if (comp_brand_size < 0)
  498. return -1;
  499. comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
  500. if (!comp_brands_str)
  501. return AVERROR(ENOMEM);
  502. avio_read(pb, comp_brands_str, comp_brand_size);
  503. comp_brands_str[comp_brand_size] = 0;
  504. av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
  505. av_freep(&comp_brands_str);
  506. return 0;
  507. }
  508. /* this atom should contain all header atoms */
  509. static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  510. {
  511. if (mov_read_default(c, pb, atom) < 0)
  512. return -1;
  513. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  514. /* so we don't parse the whole file if over a network */
  515. c->found_moov=1;
  516. return 0; /* now go for mdat */
  517. }
  518. static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  519. {
  520. c->fragment.moof_offset = avio_tell(pb) - 8;
  521. av_dlog(c->fc, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
  522. return mov_read_default(c, pb, atom);
  523. }
  524. static void mov_metadata_creation_time(AVDictionary **metadata, time_t time)
  525. {
  526. char buffer[32];
  527. if (time) {
  528. struct tm *ptm;
  529. time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
  530. ptm = gmtime(&time);
  531. if (!ptm) return;
  532. strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
  533. av_dict_set(metadata, "creation_time", buffer, 0);
  534. }
  535. }
  536. static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  537. {
  538. AVStream *st;
  539. MOVStreamContext *sc;
  540. int version;
  541. char language[4] = {0};
  542. unsigned lang;
  543. time_t creation_time;
  544. if (c->fc->nb_streams < 1)
  545. return 0;
  546. st = c->fc->streams[c->fc->nb_streams-1];
  547. sc = st->priv_data;
  548. version = avio_r8(pb);
  549. if (version > 1)
  550. return -1; /* unsupported */
  551. avio_rb24(pb); /* flags */
  552. if (version == 1) {
  553. creation_time = avio_rb64(pb);
  554. avio_rb64(pb);
  555. } else {
  556. creation_time = avio_rb32(pb);
  557. avio_rb32(pb); /* modification time */
  558. }
  559. mov_metadata_creation_time(&st->metadata, creation_time);
  560. sc->time_scale = avio_rb32(pb);
  561. st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  562. lang = avio_rb16(pb); /* language */
  563. if (ff_mov_lang_to_iso639(lang, language))
  564. av_dict_set(&st->metadata, "language", language, 0);
  565. avio_rb16(pb); /* quality */
  566. return 0;
  567. }
  568. static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  569. {
  570. time_t creation_time;
  571. int version = avio_r8(pb); /* version */
  572. avio_rb24(pb); /* flags */
  573. if (version == 1) {
  574. creation_time = avio_rb64(pb);
  575. avio_rb64(pb);
  576. } else {
  577. creation_time = avio_rb32(pb);
  578. avio_rb32(pb); /* modification time */
  579. }
  580. mov_metadata_creation_time(&c->fc->metadata, creation_time);
  581. c->time_scale = avio_rb32(pb); /* time scale */
  582. av_dlog(c->fc, "time scale = %i\n", c->time_scale);
  583. c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  584. avio_rb32(pb); /* preferred scale */
  585. avio_rb16(pb); /* preferred volume */
  586. avio_skip(pb, 10); /* reserved */
  587. avio_skip(pb, 36); /* display matrix */
  588. avio_rb32(pb); /* preview time */
  589. avio_rb32(pb); /* preview duration */
  590. avio_rb32(pb); /* poster time */
  591. avio_rb32(pb); /* selection time */
  592. avio_rb32(pb); /* selection duration */
  593. avio_rb32(pb); /* current time */
  594. avio_rb32(pb); /* next track ID */
  595. return 0;
  596. }
  597. static int mov_read_smi(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  598. {
  599. AVStream *st;
  600. if (c->fc->nb_streams < 1)
  601. return 0;
  602. st = c->fc->streams[c->fc->nb_streams-1];
  603. if((uint64_t)atom.size > (1<<30))
  604. return -1;
  605. // currently SVQ3 decoder expect full STSD header - so let's fake it
  606. // this should be fixed and just SMI header should be passed
  607. av_free(st->codec->extradata);
  608. st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE);
  609. if (!st->codec->extradata)
  610. return AVERROR(ENOMEM);
  611. st->codec->extradata_size = 0x5a + atom.size;
  612. memcpy(st->codec->extradata, "SVQ3", 4); // fake
  613. avio_read(pb, st->codec->extradata + 0x5a, atom.size);
  614. av_dlog(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
  615. return 0;
  616. }
  617. static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  618. {
  619. AVStream *st;
  620. int little_endian;
  621. if (c->fc->nb_streams < 1)
  622. return 0;
  623. st = c->fc->streams[c->fc->nb_streams-1];
  624. little_endian = avio_rb16(pb) & 0xFF;
  625. av_dlog(c->fc, "enda %d\n", little_endian);
  626. if (little_endian == 1) {
  627. switch (st->codec->codec_id) {
  628. case CODEC_ID_PCM_S24BE:
  629. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  630. break;
  631. case CODEC_ID_PCM_S32BE:
  632. st->codec->codec_id = CODEC_ID_PCM_S32LE;
  633. break;
  634. case CODEC_ID_PCM_F32BE:
  635. st->codec->codec_id = CODEC_ID_PCM_F32LE;
  636. break;
  637. case CODEC_ID_PCM_F64BE:
  638. st->codec->codec_id = CODEC_ID_PCM_F64LE;
  639. break;
  640. default:
  641. break;
  642. }
  643. }
  644. return 0;
  645. }
  646. /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
  647. static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
  648. enum CodecID codec_id)
  649. {
  650. AVStream *st;
  651. uint64_t size;
  652. uint8_t *buf;
  653. if (c->fc->nb_streams < 1) // will happen with jp2 files
  654. return 0;
  655. st= c->fc->streams[c->fc->nb_streams-1];
  656. if (st->codec->codec_id != codec_id)
  657. return 0; /* unexpected codec_id - don't mess with extradata */
  658. size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
  659. if(size > INT_MAX || (uint64_t)atom.size > INT_MAX)
  660. return -1;
  661. buf= av_realloc(st->codec->extradata, size);
  662. if(!buf)
  663. return -1;
  664. st->codec->extradata= buf;
  665. buf+= st->codec->extradata_size;
  666. st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
  667. AV_WB32( buf , atom.size + 8);
  668. AV_WL32( buf + 4, atom.type);
  669. avio_read(pb, buf + 8, atom.size);
  670. return 0;
  671. }
  672. /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
  673. static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  674. {
  675. return mov_read_extradata(c, pb, atom, CODEC_ID_ALAC);
  676. }
  677. static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  678. {
  679. return mov_read_extradata(c, pb, atom, CODEC_ID_AVS);
  680. }
  681. static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  682. {
  683. return mov_read_extradata(c, pb, atom, CODEC_ID_MJPEG);
  684. }
  685. static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  686. {
  687. return mov_read_extradata(c, pb, atom, CODEC_ID_JPEG2000);
  688. }
  689. static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  690. {
  691. AVStream *st;
  692. if (c->fc->nb_streams < 1)
  693. return 0;
  694. st = c->fc->streams[c->fc->nb_streams-1];
  695. if((uint64_t)atom.size > (1<<30))
  696. return -1;
  697. if (st->codec->codec_id == CODEC_ID_QDM2 || st->codec->codec_id == CODEC_ID_QDMC) {
  698. // pass all frma atom to codec, needed at least for QDMC and QDM2
  699. av_free(st->codec->extradata);
  700. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  701. if (!st->codec->extradata)
  702. return AVERROR(ENOMEM);
  703. st->codec->extradata_size = atom.size;
  704. avio_read(pb, st->codec->extradata, atom.size);
  705. } else if (atom.size > 8) { /* to read frma, esds atoms */
  706. if (mov_read_default(c, pb, atom) < 0)
  707. return -1;
  708. } else
  709. avio_skip(pb, atom.size);
  710. return 0;
  711. }
  712. /**
  713. * This function reads atom content and puts data in extradata without tag
  714. * nor size unlike mov_read_extradata.
  715. */
  716. static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  717. {
  718. AVStream *st;
  719. if (c->fc->nb_streams < 1)
  720. return 0;
  721. st = c->fc->streams[c->fc->nb_streams-1];
  722. if((uint64_t)atom.size > (1<<30))
  723. return -1;
  724. av_free(st->codec->extradata);
  725. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  726. if (!st->codec->extradata)
  727. return AVERROR(ENOMEM);
  728. st->codec->extradata_size = atom.size;
  729. avio_read(pb, st->codec->extradata, atom.size);
  730. return 0;
  731. }
  732. /**
  733. * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
  734. * but can have extradata appended at the end after the 40 bytes belonging
  735. * to the struct.
  736. */
  737. static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  738. {
  739. AVStream *st;
  740. if (c->fc->nb_streams < 1)
  741. return 0;
  742. if (atom.size <= 40)
  743. return 0;
  744. st = c->fc->streams[c->fc->nb_streams-1];
  745. if((uint64_t)atom.size > (1<<30))
  746. return -1;
  747. av_free(st->codec->extradata);
  748. st->codec->extradata = av_mallocz(atom.size - 40 + FF_INPUT_BUFFER_PADDING_SIZE);
  749. if (!st->codec->extradata)
  750. return AVERROR(ENOMEM);
  751. st->codec->extradata_size = atom.size - 40;
  752. avio_skip(pb, 40);
  753. avio_read(pb, st->codec->extradata, atom.size - 40);
  754. return 0;
  755. }
  756. static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  757. {
  758. AVStream *st;
  759. MOVStreamContext *sc;
  760. unsigned int i, entries;
  761. if (c->fc->nb_streams < 1)
  762. return 0;
  763. st = c->fc->streams[c->fc->nb_streams-1];
  764. sc = st->priv_data;
  765. avio_r8(pb); /* version */
  766. avio_rb24(pb); /* flags */
  767. entries = avio_rb32(pb);
  768. if(entries >= UINT_MAX/sizeof(int64_t))
  769. return -1;
  770. sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
  771. if (!sc->chunk_offsets)
  772. return AVERROR(ENOMEM);
  773. sc->chunk_count = entries;
  774. if (atom.type == MKTAG('s','t','c','o'))
  775. for(i=0; i<entries; i++)
  776. sc->chunk_offsets[i] = avio_rb32(pb);
  777. else if (atom.type == MKTAG('c','o','6','4'))
  778. for(i=0; i<entries; i++)
  779. sc->chunk_offsets[i] = avio_rb64(pb);
  780. else
  781. return -1;
  782. return 0;
  783. }
  784. /**
  785. * Compute codec id for 'lpcm' tag.
  786. * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
  787. */
  788. enum CodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
  789. {
  790. if (flags & 1) { // floating point
  791. if (flags & 2) { // big endian
  792. if (bps == 32) return CODEC_ID_PCM_F32BE;
  793. else if (bps == 64) return CODEC_ID_PCM_F64BE;
  794. } else {
  795. if (bps == 32) return CODEC_ID_PCM_F32LE;
  796. else if (bps == 64) return CODEC_ID_PCM_F64LE;
  797. }
  798. } else {
  799. if (flags & 2) {
  800. if (bps == 8)
  801. // signed integer
  802. if (flags & 4) return CODEC_ID_PCM_S8;
  803. else return CODEC_ID_PCM_U8;
  804. else if (bps == 16) return CODEC_ID_PCM_S16BE;
  805. else if (bps == 24) return CODEC_ID_PCM_S24BE;
  806. else if (bps == 32) return CODEC_ID_PCM_S32BE;
  807. } else {
  808. if (bps == 8)
  809. if (flags & 4) return CODEC_ID_PCM_S8;
  810. else return CODEC_ID_PCM_U8;
  811. else if (bps == 16) return CODEC_ID_PCM_S16LE;
  812. else if (bps == 24) return CODEC_ID_PCM_S24LE;
  813. else if (bps == 32) return CODEC_ID_PCM_S32LE;
  814. }
  815. }
  816. return CODEC_ID_NONE;
  817. }
  818. int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
  819. {
  820. AVStream *st;
  821. MOVStreamContext *sc;
  822. int j, pseudo_stream_id;
  823. if (c->fc->nb_streams < 1)
  824. return 0;
  825. st = c->fc->streams[c->fc->nb_streams-1];
  826. sc = st->priv_data;
  827. for(pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
  828. //Parsing Sample description table
  829. enum CodecID id;
  830. int dref_id = 1;
  831. MOVAtom a = { AV_RL32("stsd") };
  832. int64_t start_pos = avio_tell(pb);
  833. int size = avio_rb32(pb); /* size */
  834. uint32_t format = avio_rl32(pb); /* data format */
  835. if (size >= 16) {
  836. avio_rb32(pb); /* reserved */
  837. avio_rb16(pb); /* reserved */
  838. dref_id = avio_rb16(pb);
  839. }
  840. if (st->codec->codec_tag &&
  841. st->codec->codec_tag != format &&
  842. (c->fc->video_codec_id ? ff_codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id
  843. : st->codec->codec_tag != MKTAG('j','p','e','g'))
  844. ){
  845. /* Multiple fourcc, we skip JPEG. This is not correct, we should
  846. * export it as a separate AVStream but this needs a few changes
  847. * in the MOV demuxer, patch welcome. */
  848. multiple_stsd:
  849. av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
  850. avio_skip(pb, size - (avio_tell(pb) - start_pos));
  851. continue;
  852. }
  853. /* we cannot demux concatenated h264 streams because of different extradata */
  854. if (st->codec->codec_tag && st->codec->codec_tag == AV_RL32("avc1"))
  855. goto multiple_stsd;
  856. sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
  857. sc->dref_id= dref_id;
  858. st->codec->codec_tag = format;
  859. id = ff_codec_get_id(codec_movaudio_tags, format);
  860. if (id<=0 && ((format&0xFFFF) == 'm'+('s'<<8) || (format&0xFFFF) == 'T'+('S'<<8)))
  861. id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format)&0xFFFF);
  862. if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
  863. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  864. } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO && /* do not overwrite codec type */
  865. format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */
  866. id = ff_codec_get_id(codec_movvideo_tags, format);
  867. if (id <= 0)
  868. id = ff_codec_get_id(ff_codec_bmp_tags, format);
  869. if (id > 0)
  870. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  871. else if(st->codec->codec_type == AVMEDIA_TYPE_DATA){
  872. id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
  873. if(id > 0)
  874. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  875. }
  876. }
  877. av_dlog(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
  878. (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
  879. (format >> 24) & 0xff, st->codec->codec_type);
  880. if(st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
  881. unsigned int color_depth, len;
  882. int color_greyscale;
  883. st->codec->codec_id = id;
  884. avio_rb16(pb); /* version */
  885. avio_rb16(pb); /* revision level */
  886. avio_rb32(pb); /* vendor */
  887. avio_rb32(pb); /* temporal quality */
  888. avio_rb32(pb); /* spatial quality */
  889. st->codec->width = avio_rb16(pb); /* width */
  890. st->codec->height = avio_rb16(pb); /* height */
  891. avio_rb32(pb); /* horiz resolution */
  892. avio_rb32(pb); /* vert resolution */
  893. avio_rb32(pb); /* data size, always 0 */
  894. avio_rb16(pb); /* frames per samples */
  895. len = avio_r8(pb); /* codec name, pascal string */
  896. if (len > 31)
  897. len = 31;
  898. mov_read_mac_string(c, pb, len, st->codec->codec_name, 32);
  899. if (len < 31)
  900. avio_skip(pb, 31 - len);
  901. /* codec_tag YV12 triggers an UV swap in rawdec.c */
  902. if (!memcmp(st->codec->codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25))
  903. st->codec->codec_tag=MKTAG('I', '4', '2', '0');
  904. st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
  905. st->codec->color_table_id = avio_rb16(pb); /* colortable id */
  906. av_dlog(c->fc, "depth %d, ctab id %d\n",
  907. st->codec->bits_per_coded_sample, st->codec->color_table_id);
  908. /* figure out the palette situation */
  909. color_depth = st->codec->bits_per_coded_sample & 0x1F;
  910. color_greyscale = st->codec->bits_per_coded_sample & 0x20;
  911. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  912. if ((color_depth == 2) || (color_depth == 4) ||
  913. (color_depth == 8)) {
  914. /* for palette traversal */
  915. unsigned int color_start, color_count, color_end;
  916. unsigned char r, g, b;
  917. st->codec->palctrl = av_malloc(sizeof(*st->codec->palctrl));
  918. if (color_greyscale) {
  919. int color_index, color_dec;
  920. /* compute the greyscale palette */
  921. st->codec->bits_per_coded_sample = color_depth;
  922. color_count = 1 << color_depth;
  923. color_index = 255;
  924. color_dec = 256 / (color_count - 1);
  925. for (j = 0; j < color_count; j++) {
  926. r = g = b = color_index;
  927. st->codec->palctrl->palette[j] =
  928. (r << 16) | (g << 8) | (b);
  929. color_index -= color_dec;
  930. if (color_index < 0)
  931. color_index = 0;
  932. }
  933. } else if (st->codec->color_table_id) {
  934. const uint8_t *color_table;
  935. /* if flag bit 3 is set, use the default palette */
  936. color_count = 1 << color_depth;
  937. if (color_depth == 2)
  938. color_table = ff_qt_default_palette_4;
  939. else if (color_depth == 4)
  940. color_table = ff_qt_default_palette_16;
  941. else
  942. color_table = ff_qt_default_palette_256;
  943. for (j = 0; j < color_count; j++) {
  944. r = color_table[j * 3 + 0];
  945. g = color_table[j * 3 + 1];
  946. b = color_table[j * 3 + 2];
  947. st->codec->palctrl->palette[j] =
  948. (r << 16) | (g << 8) | (b);
  949. }
  950. } else {
  951. /* load the palette from the file */
  952. color_start = avio_rb32(pb);
  953. color_count = avio_rb16(pb);
  954. color_end = avio_rb16(pb);
  955. if ((color_start <= 255) &&
  956. (color_end <= 255)) {
  957. for (j = color_start; j <= color_end; j++) {
  958. /* each R, G, or B component is 16 bits;
  959. * only use the top 8 bits; skip alpha bytes
  960. * up front */
  961. avio_r8(pb);
  962. avio_r8(pb);
  963. r = avio_r8(pb);
  964. avio_r8(pb);
  965. g = avio_r8(pb);
  966. avio_r8(pb);
  967. b = avio_r8(pb);
  968. avio_r8(pb);
  969. st->codec->palctrl->palette[j] =
  970. (r << 16) | (g << 8) | (b);
  971. }
  972. }
  973. }
  974. st->codec->palctrl->palette_changed = 1;
  975. }
  976. } else if(st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
  977. int bits_per_sample, flags;
  978. uint16_t version = avio_rb16(pb);
  979. st->codec->codec_id = id;
  980. avio_rb16(pb); /* revision level */
  981. avio_rb32(pb); /* vendor */
  982. st->codec->channels = avio_rb16(pb); /* channel count */
  983. av_dlog(c->fc, "audio channels %d\n", st->codec->channels);
  984. st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
  985. sc->audio_cid = avio_rb16(pb);
  986. avio_rb16(pb); /* packet size = 0 */
  987. st->codec->sample_rate = ((avio_rb32(pb) >> 16));
  988. //Read QT version 1 fields. In version 0 these do not exist.
  989. av_dlog(c->fc, "version =%d, isom =%d\n",version,c->isom);
  990. if(!c->isom) {
  991. if(version==1) {
  992. sc->samples_per_frame = avio_rb32(pb);
  993. avio_rb32(pb); /* bytes per packet */
  994. sc->bytes_per_frame = avio_rb32(pb);
  995. avio_rb32(pb); /* bytes per sample */
  996. } else if(version==2) {
  997. avio_rb32(pb); /* sizeof struct only */
  998. st->codec->sample_rate = av_int2dbl(avio_rb64(pb)); /* float 64 */
  999. st->codec->channels = avio_rb32(pb);
  1000. avio_rb32(pb); /* always 0x7F000000 */
  1001. st->codec->bits_per_coded_sample = avio_rb32(pb); /* bits per channel if sound is uncompressed */
  1002. flags = avio_rb32(pb); /* lpcm format specific flag */
  1003. sc->bytes_per_frame = avio_rb32(pb); /* bytes per audio packet if constant */
  1004. sc->samples_per_frame = avio_rb32(pb); /* lpcm frames per audio packet if constant */
  1005. if (format == MKTAG('l','p','c','m'))
  1006. st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
  1007. }
  1008. }
  1009. switch (st->codec->codec_id) {
  1010. case CODEC_ID_PCM_S8:
  1011. case CODEC_ID_PCM_U8:
  1012. if (st->codec->bits_per_coded_sample == 16)
  1013. st->codec->codec_id = CODEC_ID_PCM_S16BE;
  1014. break;
  1015. case CODEC_ID_PCM_S16LE:
  1016. case CODEC_ID_PCM_S16BE:
  1017. if (st->codec->bits_per_coded_sample == 8)
  1018. st->codec->codec_id = CODEC_ID_PCM_S8;
  1019. else if (st->codec->bits_per_coded_sample == 24)
  1020. st->codec->codec_id =
  1021. st->codec->codec_id == CODEC_ID_PCM_S16BE ?
  1022. CODEC_ID_PCM_S24BE : CODEC_ID_PCM_S24LE;
  1023. break;
  1024. /* set values for old format before stsd version 1 appeared */
  1025. case CODEC_ID_MACE3:
  1026. sc->samples_per_frame = 6;
  1027. sc->bytes_per_frame = 2*st->codec->channels;
  1028. break;
  1029. case CODEC_ID_MACE6:
  1030. sc->samples_per_frame = 6;
  1031. sc->bytes_per_frame = 1*st->codec->channels;
  1032. break;
  1033. case CODEC_ID_ADPCM_IMA_QT:
  1034. sc->samples_per_frame = 64;
  1035. sc->bytes_per_frame = 34*st->codec->channels;
  1036. break;
  1037. case CODEC_ID_GSM:
  1038. sc->samples_per_frame = 160;
  1039. sc->bytes_per_frame = 33;
  1040. break;
  1041. default:
  1042. break;
  1043. }
  1044. bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
  1045. if (bits_per_sample) {
  1046. st->codec->bits_per_coded_sample = bits_per_sample;
  1047. sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
  1048. }
  1049. } else if(st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
  1050. // ttxt stsd contains display flags, justification, background
  1051. // color, fonts, and default styles, so fake an atom to read it
  1052. MOVAtom fake_atom = { .size = size - (avio_tell(pb) - start_pos) };
  1053. if (format != AV_RL32("mp4s")) // mp4s contains a regular esds atom
  1054. mov_read_glbl(c, pb, fake_atom);
  1055. st->codec->codec_id= id;
  1056. st->codec->width = sc->width;
  1057. st->codec->height = sc->height;
  1058. } else {
  1059. /* other codec type, just skip (rtp, mp4s, tmcd ...) */
  1060. avio_skip(pb, size - (avio_tell(pb) - start_pos));
  1061. }
  1062. /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
  1063. a.size = size - (avio_tell(pb) - start_pos);
  1064. if (a.size > 8) {
  1065. if (mov_read_default(c, pb, a) < 0)
  1066. return -1;
  1067. } else if (a.size > 0)
  1068. avio_skip(pb, a.size);
  1069. }
  1070. if(st->codec->codec_type==AVMEDIA_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
  1071. st->codec->sample_rate= sc->time_scale;
  1072. /* special codec parameters handling */
  1073. switch (st->codec->codec_id) {
  1074. #if CONFIG_DV_DEMUXER
  1075. case CODEC_ID_DVAUDIO:
  1076. c->dv_fctx = avformat_alloc_context();
  1077. c->dv_demux = dv_init_demux(c->dv_fctx);
  1078. if (!c->dv_demux) {
  1079. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  1080. return -1;
  1081. }
  1082. sc->dv_audio_container = 1;
  1083. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  1084. break;
  1085. #endif
  1086. /* no ifdef since parameters are always those */
  1087. case CODEC_ID_QCELP:
  1088. // force sample rate for qcelp when not stored in mov
  1089. if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
  1090. st->codec->sample_rate = 8000;
  1091. st->codec->frame_size= 160;
  1092. st->codec->channels= 1; /* really needed */
  1093. break;
  1094. case CODEC_ID_AMR_NB:
  1095. case CODEC_ID_AMR_WB:
  1096. st->codec->frame_size= sc->samples_per_frame;
  1097. st->codec->channels= 1; /* really needed */
  1098. /* force sample rate for amr, stsd in 3gp does not store sample rate */
  1099. if (st->codec->codec_id == CODEC_ID_AMR_NB)
  1100. st->codec->sample_rate = 8000;
  1101. else if (st->codec->codec_id == CODEC_ID_AMR_WB)
  1102. st->codec->sample_rate = 16000;
  1103. break;
  1104. case CODEC_ID_MP2:
  1105. case CODEC_ID_MP3:
  1106. st->codec->codec_type = AVMEDIA_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
  1107. st->need_parsing = AVSTREAM_PARSE_FULL;
  1108. break;
  1109. case CODEC_ID_GSM:
  1110. case CODEC_ID_ADPCM_MS:
  1111. case CODEC_ID_ADPCM_IMA_WAV:
  1112. st->codec->frame_size = sc->samples_per_frame;
  1113. st->codec->block_align = sc->bytes_per_frame;
  1114. break;
  1115. case CODEC_ID_ALAC:
  1116. if (st->codec->extradata_size == 36) {
  1117. st->codec->frame_size = AV_RB32(st->codec->extradata+12);
  1118. st->codec->channels = AV_RB8 (st->codec->extradata+21);
  1119. st->codec->sample_rate = AV_RB32(st->codec->extradata+32);
  1120. }
  1121. break;
  1122. default:
  1123. break;
  1124. }
  1125. return 0;
  1126. }
  1127. static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1128. {
  1129. int entries;
  1130. avio_r8(pb); /* version */
  1131. avio_rb24(pb); /* flags */
  1132. entries = avio_rb32(pb);
  1133. return ff_mov_read_stsd_entries(c, pb, entries);
  1134. }
  1135. static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1136. {
  1137. AVStream *st;
  1138. MOVStreamContext *sc;
  1139. unsigned int i, entries;
  1140. if (c->fc->nb_streams < 1)
  1141. return 0;
  1142. st = c->fc->streams[c->fc->nb_streams-1];
  1143. sc = st->priv_data;
  1144. avio_r8(pb); /* version */
  1145. avio_rb24(pb); /* flags */
  1146. entries = avio_rb32(pb);
  1147. av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  1148. if(entries >= UINT_MAX / sizeof(*sc->stsc_data))
  1149. return -1;
  1150. sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
  1151. if (!sc->stsc_data)
  1152. return AVERROR(ENOMEM);
  1153. sc->stsc_count = entries;
  1154. for(i=0; i<entries; i++) {
  1155. sc->stsc_data[i].first = avio_rb32(pb);
  1156. sc->stsc_data[i].count = avio_rb32(pb);
  1157. sc->stsc_data[i].id = avio_rb32(pb);
  1158. }
  1159. return 0;
  1160. }
  1161. static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1162. {
  1163. AVStream *st;
  1164. MOVStreamContext *sc;
  1165. unsigned i, entries;
  1166. if (c->fc->nb_streams < 1)
  1167. return 0;
  1168. st = c->fc->streams[c->fc->nb_streams-1];
  1169. sc = st->priv_data;
  1170. avio_rb32(pb); // version + flags
  1171. entries = avio_rb32(pb);
  1172. if (entries >= UINT_MAX / sizeof(*sc->stps_data))
  1173. return -1;
  1174. sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
  1175. if (!sc->stps_data)
  1176. return AVERROR(ENOMEM);
  1177. sc->stps_count = entries;
  1178. for (i = 0; i < entries; i++) {
  1179. sc->stps_data[i] = avio_rb32(pb);
  1180. //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]);
  1181. }
  1182. return 0;
  1183. }
  1184. static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1185. {
  1186. AVStream *st;
  1187. MOVStreamContext *sc;
  1188. unsigned int i, entries;
  1189. if (c->fc->nb_streams < 1)
  1190. return 0;
  1191. st = c->fc->streams[c->fc->nb_streams-1];
  1192. sc = st->priv_data;
  1193. avio_r8(pb); /* version */
  1194. avio_rb24(pb); /* flags */
  1195. entries = avio_rb32(pb);
  1196. av_dlog(c->fc, "keyframe_count = %d\n", entries);
  1197. if(entries >= UINT_MAX / sizeof(int))
  1198. return -1;
  1199. sc->keyframes = av_malloc(entries * sizeof(int));
  1200. if (!sc->keyframes)
  1201. return AVERROR(ENOMEM);
  1202. sc->keyframe_count = entries;
  1203. for(i=0; i<entries; i++) {
  1204. sc->keyframes[i] = avio_rb32(pb);
  1205. //av_dlog(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
  1206. }
  1207. return 0;
  1208. }
  1209. static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1210. {
  1211. AVStream *st;
  1212. MOVStreamContext *sc;
  1213. unsigned int i, entries, sample_size, field_size, num_bytes;
  1214. GetBitContext gb;
  1215. unsigned char* buf;
  1216. if (c->fc->nb_streams < 1)
  1217. return 0;
  1218. st = c->fc->streams[c->fc->nb_streams-1];
  1219. sc = st->priv_data;
  1220. avio_r8(pb); /* version */
  1221. avio_rb24(pb); /* flags */
  1222. if (atom.type == MKTAG('s','t','s','z')) {
  1223. sample_size = avio_rb32(pb);
  1224. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  1225. sc->sample_size = sample_size;
  1226. field_size = 32;
  1227. } else {
  1228. sample_size = 0;
  1229. avio_rb24(pb); /* reserved */
  1230. field_size = avio_r8(pb);
  1231. }
  1232. entries = avio_rb32(pb);
  1233. av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
  1234. sc->sample_count = entries;
  1235. if (sample_size)
  1236. return 0;
  1237. if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
  1238. av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
  1239. return -1;
  1240. }
  1241. if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
  1242. return -1;
  1243. sc->sample_sizes = av_malloc(entries * sizeof(int));
  1244. if (!sc->sample_sizes)
  1245. return AVERROR(ENOMEM);
  1246. num_bytes = (entries*field_size+4)>>3;
  1247. buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
  1248. if (!buf) {
  1249. av_freep(&sc->sample_sizes);
  1250. return AVERROR(ENOMEM);
  1251. }
  1252. if (avio_read(pb, buf, num_bytes) < num_bytes) {
  1253. av_freep(&sc->sample_sizes);
  1254. av_free(buf);
  1255. return -1;
  1256. }
  1257. init_get_bits(&gb, buf, 8*num_bytes);
  1258. for(i=0; i<entries; i++)
  1259. sc->sample_sizes[i] = get_bits_long(&gb, field_size);
  1260. av_free(buf);
  1261. return 0;
  1262. }
  1263. static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1264. {
  1265. AVStream *st;
  1266. MOVStreamContext *sc;
  1267. unsigned int i, entries;
  1268. int64_t duration=0;
  1269. int64_t total_sample_count=0;
  1270. if (c->fc->nb_streams < 1)
  1271. return 0;
  1272. st = c->fc->streams[c->fc->nb_streams-1];
  1273. sc = st->priv_data;
  1274. avio_r8(pb); /* version */
  1275. avio_rb24(pb); /* flags */
  1276. entries = avio_rb32(pb);
  1277. av_dlog(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  1278. if(entries >= UINT_MAX / sizeof(*sc->stts_data))
  1279. return -1;
  1280. sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
  1281. if (!sc->stts_data)
  1282. return AVERROR(ENOMEM);
  1283. sc->stts_count = entries;
  1284. for(i=0; i<entries; i++) {
  1285. int sample_duration;
  1286. int sample_count;
  1287. sample_count=avio_rb32(pb);
  1288. sample_duration = avio_rb32(pb);
  1289. sc->stts_data[i].count= sample_count;
  1290. sc->stts_data[i].duration= sample_duration;
  1291. av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  1292. duration+=(int64_t)sample_duration*sample_count;
  1293. total_sample_count+=sample_count;
  1294. }
  1295. st->nb_frames= total_sample_count;
  1296. if(duration)
  1297. st->duration= duration;
  1298. return 0;
  1299. }
  1300. static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1301. {
  1302. AVStream *st;
  1303. MOVStreamContext *sc;
  1304. unsigned int i, entries;
  1305. if (c->fc->nb_streams < 1)
  1306. return 0;
  1307. st = c->fc->streams[c->fc->nb_streams-1];
  1308. sc = st->priv_data;
  1309. avio_r8(pb); /* version */
  1310. avio_rb24(pb); /* flags */
  1311. entries = avio_rb32(pb);
  1312. av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1313. if(entries >= UINT_MAX / sizeof(*sc->ctts_data))
  1314. return -1;
  1315. sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
  1316. if (!sc->ctts_data)
  1317. return AVERROR(ENOMEM);
  1318. sc->ctts_count = entries;
  1319. for(i=0; i<entries; i++) {
  1320. int count =avio_rb32(pb);
  1321. int duration =avio_rb32(pb);
  1322. sc->ctts_data[i].count = count;
  1323. sc->ctts_data[i].duration= duration;
  1324. if (duration < 0 && i+1<entries)
  1325. sc->dts_shift = FFMAX(sc->dts_shift, -duration);
  1326. }
  1327. av_dlog(c->fc, "dts shift %d\n", sc->dts_shift);
  1328. return 0;
  1329. }
  1330. static void mov_build_index(MOVContext *mov, AVStream *st)
  1331. {
  1332. MOVStreamContext *sc = st->priv_data;
  1333. int64_t current_offset;
  1334. int64_t current_dts = 0;
  1335. unsigned int stts_index = 0;
  1336. unsigned int stsc_index = 0;
  1337. unsigned int stss_index = 0;
  1338. unsigned int stps_index = 0;
  1339. unsigned int i, j;
  1340. uint64_t stream_size = 0;
  1341. /* adjust first dts according to edit list */
  1342. if (sc->time_offset && mov->time_scale > 0) {
  1343. if (sc->time_offset < 0)
  1344. sc->time_offset = av_rescale(sc->time_offset, sc->time_scale, mov->time_scale);
  1345. current_dts = -sc->time_offset;
  1346. if (sc->ctts_data && sc->stts_data &&
  1347. sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
  1348. /* more than 16 frames delay, dts are likely wrong
  1349. this happens with files created by iMovie */
  1350. sc->wrong_dts = 1;
  1351. st->codec->has_b_frames = 1;
  1352. }
  1353. }
  1354. /* only use old uncompressed audio chunk demuxing when stts specifies it */
  1355. if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  1356. sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
  1357. unsigned int current_sample = 0;
  1358. unsigned int stts_sample = 0;
  1359. unsigned int sample_size;
  1360. unsigned int distance = 0;
  1361. int key_off = sc->keyframes && sc->keyframes[0] == 1;
  1362. current_dts -= sc->dts_shift;
  1363. if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries))
  1364. return;
  1365. st->index_entries = av_malloc(sc->sample_count*sizeof(*st->index_entries));
  1366. if (!st->index_entries)
  1367. return;
  1368. st->index_entries_allocated_size = sc->sample_count*sizeof(*st->index_entries);
  1369. for (i = 0; i < sc->chunk_count; i++) {
  1370. current_offset = sc->chunk_offsets[i];
  1371. while (stsc_index + 1 < sc->stsc_count &&
  1372. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1373. stsc_index++;
  1374. for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
  1375. int keyframe = 0;
  1376. if (current_sample >= sc->sample_count) {
  1377. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  1378. return;
  1379. }
  1380. if (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index]) {
  1381. keyframe = 1;
  1382. if (stss_index + 1 < sc->keyframe_count)
  1383. stss_index++;
  1384. } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
  1385. keyframe = 1;
  1386. if (stps_index + 1 < sc->stps_count)
  1387. stps_index++;
  1388. }
  1389. if (keyframe)
  1390. distance = 0;
  1391. sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
  1392. if(sc->pseudo_stream_id == -1 ||
  1393. sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
  1394. AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
  1395. e->pos = current_offset;
  1396. e->timestamp = current_dts;
  1397. e->size = sample_size;
  1398. e->min_distance = distance;
  1399. e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
  1400. av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1401. "size %d, distance %d, keyframe %d\n", st->index, current_sample,
  1402. current_offset, current_dts, sample_size, distance, keyframe);
  1403. }
  1404. current_offset += sample_size;
  1405. stream_size += sample_size;
  1406. current_dts += sc->stts_data[stts_index].duration;
  1407. distance++;
  1408. stts_sample++;
  1409. current_sample++;
  1410. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  1411. stts_sample = 0;
  1412. stts_index++;
  1413. }
  1414. }
  1415. }
  1416. if (st->duration > 0)
  1417. st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
  1418. } else {
  1419. unsigned chunk_samples, total = 0;
  1420. // compute total chunk count
  1421. for (i = 0; i < sc->stsc_count; i++) {
  1422. unsigned count, chunk_count;
  1423. chunk_samples = sc->stsc_data[i].count;
  1424. if (sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
  1425. av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
  1426. return;
  1427. }
  1428. if (sc->samples_per_frame >= 160) { // gsm
  1429. count = chunk_samples / sc->samples_per_frame;
  1430. } else if (sc->samples_per_frame > 1) {
  1431. unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
  1432. count = (chunk_samples+samples-1) / samples;
  1433. } else {
  1434. count = (chunk_samples+1023) / 1024;
  1435. }
  1436. if (i < sc->stsc_count - 1)
  1437. chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
  1438. else
  1439. chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
  1440. total += chunk_count * count;
  1441. }
  1442. av_dlog(mov->fc, "chunk count %d\n", total);
  1443. if (total >= UINT_MAX / sizeof(*st->index_entries))
  1444. return;
  1445. st->index_entries = av_malloc(total*sizeof(*st->index_entries));
  1446. if (!st->index_entries)
  1447. return;
  1448. st->index_entries_allocated_size = total*sizeof(*st->index_entries);
  1449. // populate index
  1450. for (i = 0; i < sc->chunk_count; i++) {
  1451. current_offset = sc->chunk_offsets[i];
  1452. if (stsc_index + 1 < sc->stsc_count &&
  1453. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1454. stsc_index++;
  1455. chunk_samples = sc->stsc_data[stsc_index].count;
  1456. while (chunk_samples > 0) {
  1457. AVIndexEntry *e;
  1458. unsigned size, samples;
  1459. if (sc->samples_per_frame >= 160) { // gsm
  1460. samples = sc->samples_per_frame;
  1461. size = sc->bytes_per_frame;
  1462. } else {
  1463. if (sc->samples_per_frame > 1) {
  1464. samples = FFMIN((1024 / sc->samples_per_frame)*
  1465. sc->samples_per_frame, chunk_samples);
  1466. size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
  1467. } else {
  1468. samples = FFMIN(1024, chunk_samples);
  1469. size = samples * sc->sample_size;
  1470. }
  1471. }
  1472. if (st->nb_index_entries >= total) {
  1473. av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
  1474. return;
  1475. }
  1476. e = &st->index_entries[st->nb_index_entries++];
  1477. e->pos = current_offset;
  1478. e->timestamp = current_dts;
  1479. e->size = size;
  1480. e->min_distance = 0;
  1481. e->flags = AVINDEX_KEYFRAME;
  1482. av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
  1483. "size %d, duration %d\n", st->index, i, current_offset, current_dts,
  1484. size, samples);
  1485. current_offset += size;
  1486. current_dts += samples;
  1487. chunk_samples -= samples;
  1488. }
  1489. }
  1490. }
  1491. }
  1492. static int mov_open_dref(AVIOContext **pb, const char *src, MOVDref *ref)
  1493. {
  1494. /* try relative path, we do not try the absolute because it can leak information about our
  1495. system to an attacker */
  1496. if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
  1497. char filename[1024];
  1498. const char *src_path;
  1499. int i, l;
  1500. /* find a source dir */
  1501. src_path = strrchr(src, '/');
  1502. if (src_path)
  1503. src_path++;
  1504. else
  1505. src_path = src;
  1506. /* find a next level down to target */
  1507. for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
  1508. if (ref->path[l] == '/') {
  1509. if (i == ref->nlvl_to - 1)
  1510. break;
  1511. else
  1512. i++;
  1513. }
  1514. /* compose filename if next level down to target was found */
  1515. if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
  1516. memcpy(filename, src, src_path - src);
  1517. filename[src_path - src] = 0;
  1518. for (i = 1; i < ref->nlvl_from; i++)
  1519. av_strlcat(filename, "../", 1024);
  1520. av_strlcat(filename, ref->path + l + 1, 1024);
  1521. if (!avio_open(pb, filename, AVIO_RDONLY))
  1522. return 0;
  1523. }
  1524. }
  1525. return AVERROR(ENOENT);
  1526. }
  1527. static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1528. {
  1529. AVStream *st;
  1530. MOVStreamContext *sc;
  1531. int ret;
  1532. st = av_new_stream(c->fc, c->fc->nb_streams);
  1533. if (!st) return AVERROR(ENOMEM);
  1534. sc = av_mallocz(sizeof(MOVStreamContext));
  1535. if (!sc) return AVERROR(ENOMEM);
  1536. st->priv_data = sc;
  1537. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1538. sc->ffindex = st->index;
  1539. if ((ret = mov_read_default(c, pb, atom)) < 0)
  1540. return ret;
  1541. /* sanity checks */
  1542. if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
  1543. (!sc->sample_size && !sc->sample_count))) {
  1544. av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
  1545. st->index);
  1546. return 0;
  1547. }
  1548. if (sc->time_scale <= 0) {
  1549. av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index);
  1550. sc->time_scale = c->time_scale;
  1551. if (sc->time_scale <= 0)
  1552. sc->time_scale = 1;
  1553. }
  1554. av_set_pts_info(st, 64, 1, sc->time_scale);
  1555. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  1556. !st->codec->frame_size && sc->stts_count == 1) {
  1557. st->codec->frame_size = av_rescale(sc->stts_data[0].duration,
  1558. st->codec->sample_rate, sc->time_scale);
  1559. av_dlog(c->fc, "frame size %d\n", st->codec->frame_size);
  1560. }
  1561. mov_build_index(c, st);
  1562. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  1563. MOVDref *dref = &sc->drefs[sc->dref_id - 1];
  1564. if (mov_open_dref(&sc->pb, c->fc->filename, dref) < 0)
  1565. av_log(c->fc, AV_LOG_ERROR,
  1566. "stream %d, error opening alias: path='%s', dir='%s', "
  1567. "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
  1568. st->index, dref->path, dref->dir, dref->filename,
  1569. dref->volume, dref->nlvl_from, dref->nlvl_to);
  1570. } else
  1571. sc->pb = c->fc->pb;
  1572. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  1573. if (!st->sample_aspect_ratio.num &&
  1574. (st->codec->width != sc->width || st->codec->height != sc->height)) {
  1575. st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
  1576. ((double)st->codec->width * sc->height), INT_MAX);
  1577. }
  1578. av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
  1579. sc->time_scale*st->nb_frames, st->duration, INT_MAX);
  1580. if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
  1581. av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
  1582. sc->time_scale, sc->stts_data[0].duration, INT_MAX);
  1583. }
  1584. switch (st->codec->codec_id) {
  1585. #if CONFIG_H261_DECODER
  1586. case CODEC_ID_H261:
  1587. #endif
  1588. #if CONFIG_H263_DECODER
  1589. case CODEC_ID_H263:
  1590. #endif
  1591. #if CONFIG_H264_DECODER
  1592. case CODEC_ID_H264:
  1593. #endif
  1594. #if CONFIG_MPEG4_DECODER
  1595. case CODEC_ID_MPEG4:
  1596. #endif
  1597. st->codec->width = 0; /* let decoder init width/height */
  1598. st->codec->height= 0;
  1599. break;
  1600. }
  1601. /* Do not need those anymore. */
  1602. av_freep(&sc->chunk_offsets);
  1603. av_freep(&sc->stsc_data);
  1604. av_freep(&sc->sample_sizes);
  1605. av_freep(&sc->keyframes);
  1606. av_freep(&sc->stts_data);
  1607. av_freep(&sc->stps_data);
  1608. return 0;
  1609. }
  1610. static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1611. {
  1612. int ret;
  1613. c->itunes_metadata = 1;
  1614. ret = mov_read_default(c, pb, atom);
  1615. c->itunes_metadata = 0;
  1616. return ret;
  1617. }
  1618. static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1619. {
  1620. while (atom.size > 8) {
  1621. uint32_t tag = avio_rl32(pb);
  1622. atom.size -= 4;
  1623. if (tag == MKTAG('h','d','l','r')) {
  1624. avio_seek(pb, -8, SEEK_CUR);
  1625. atom.size += 8;
  1626. return mov_read_default(c, pb, atom);
  1627. }
  1628. }
  1629. return 0;
  1630. }
  1631. static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1632. {
  1633. int i;
  1634. int width;
  1635. int height;
  1636. int64_t disp_transform[2];
  1637. int display_matrix[3][2];
  1638. AVStream *st;
  1639. MOVStreamContext *sc;
  1640. int version;
  1641. if (c->fc->nb_streams < 1)
  1642. return 0;
  1643. st = c->fc->streams[c->fc->nb_streams-1];
  1644. sc = st->priv_data;
  1645. version = avio_r8(pb);
  1646. avio_rb24(pb); /* flags */
  1647. /*
  1648. MOV_TRACK_ENABLED 0x0001
  1649. MOV_TRACK_IN_MOVIE 0x0002
  1650. MOV_TRACK_IN_PREVIEW 0x0004
  1651. MOV_TRACK_IN_POSTER 0x0008
  1652. */
  1653. if (version == 1) {
  1654. avio_rb64(pb);
  1655. avio_rb64(pb);
  1656. } else {
  1657. avio_rb32(pb); /* creation time */
  1658. avio_rb32(pb); /* modification time */
  1659. }
  1660. st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
  1661. avio_rb32(pb); /* reserved */
  1662. /* highlevel (considering edits) duration in movie timebase */
  1663. (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
  1664. avio_rb32(pb); /* reserved */
  1665. avio_rb32(pb); /* reserved */
  1666. avio_rb16(pb); /* layer */
  1667. avio_rb16(pb); /* alternate group */
  1668. avio_rb16(pb); /* volume */
  1669. avio_rb16(pb); /* reserved */
  1670. //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
  1671. // they're kept in fixed point format through all calculations
  1672. // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
  1673. for (i = 0; i < 3; i++) {
  1674. display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
  1675. display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
  1676. avio_rb32(pb); // 2.30 fixed point (not used)
  1677. }
  1678. width = avio_rb32(pb); // 16.16 fixed point track width
  1679. height = avio_rb32(pb); // 16.16 fixed point track height
  1680. sc->width = width >> 16;
  1681. sc->height = height >> 16;
  1682. if (display_matrix[0][0] == -65536 && display_matrix[1][1] == -65536) {
  1683. av_dict_set(&st->metadata, "rotate", "180", 0);
  1684. }
  1685. // transform the display width/height according to the matrix
  1686. // skip this if the display matrix is the default identity matrix
  1687. // or if it is rotating the picture, ex iPhone 3GS
  1688. // to keep the same scale, use [width height 1<<16]
  1689. if (width && height &&
  1690. ((display_matrix[0][0] != 65536 ||
  1691. display_matrix[1][1] != 65536) &&
  1692. !display_matrix[0][1] &&
  1693. !display_matrix[1][0] &&
  1694. !display_matrix[2][0] && !display_matrix[2][1])) {
  1695. for (i = 0; i < 2; i++)
  1696. disp_transform[i] =
  1697. (int64_t) width * display_matrix[0][i] +
  1698. (int64_t) height * display_matrix[1][i] +
  1699. ((int64_t) display_matrix[2][i] << 16);
  1700. //sample aspect ratio is new width/height divided by old width/height
  1701. st->sample_aspect_ratio = av_d2q(
  1702. ((double) disp_transform[0] * height) /
  1703. ((double) disp_transform[1] * width), INT_MAX);
  1704. }
  1705. return 0;
  1706. }
  1707. static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1708. {
  1709. MOVFragment *frag = &c->fragment;
  1710. MOVTrackExt *trex = NULL;
  1711. int flags, track_id, i;
  1712. avio_r8(pb); /* version */
  1713. flags = avio_rb24(pb);
  1714. track_id = avio_rb32(pb);
  1715. if (!track_id)
  1716. return -1;
  1717. frag->track_id = track_id;
  1718. for (i = 0; i < c->trex_count; i++)
  1719. if (c->trex_data[i].track_id == frag->track_id) {
  1720. trex = &c->trex_data[i];
  1721. break;
  1722. }
  1723. if (!trex) {
  1724. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
  1725. return -1;
  1726. }
  1727. if (flags & 0x01) frag->base_data_offset = avio_rb64(pb);
  1728. else frag->base_data_offset = frag->moof_offset;
  1729. if (flags & 0x02) frag->stsd_id = avio_rb32(pb);
  1730. else frag->stsd_id = trex->stsd_id;
  1731. frag->duration = flags & 0x08 ? avio_rb32(pb) : trex->duration;
  1732. frag->size = flags & 0x10 ? avio_rb32(pb) : trex->size;
  1733. frag->flags = flags & 0x20 ? avio_rb32(pb) : trex->flags;
  1734. av_dlog(c->fc, "frag flags 0x%x\n", frag->flags);
  1735. return 0;
  1736. }
  1737. static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1738. {
  1739. c->chapter_track = avio_rb32(pb);
  1740. return 0;
  1741. }
  1742. static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1743. {
  1744. MOVTrackExt *trex;
  1745. if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
  1746. return -1;
  1747. trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
  1748. if (!trex)
  1749. return AVERROR(ENOMEM);
  1750. c->trex_data = trex;
  1751. trex = &c->trex_data[c->trex_count++];
  1752. avio_r8(pb); /* version */
  1753. avio_rb24(pb); /* flags */
  1754. trex->track_id = avio_rb32(pb);
  1755. trex->stsd_id = avio_rb32(pb);
  1756. trex->duration = avio_rb32(pb);
  1757. trex->size = avio_rb32(pb);
  1758. trex->flags = avio_rb32(pb);
  1759. return 0;
  1760. }
  1761. static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1762. {
  1763. MOVFragment *frag = &c->fragment;
  1764. AVStream *st = NULL;
  1765. MOVStreamContext *sc;
  1766. MOVStts *ctts_data;
  1767. uint64_t offset;
  1768. int64_t dts;
  1769. int data_offset = 0;
  1770. unsigned entries, first_sample_flags = frag->flags;
  1771. int flags, distance, i;
  1772. for (i = 0; i < c->fc->nb_streams; i++) {
  1773. if (c->fc->streams[i]->id == frag->track_id) {
  1774. st = c->fc->streams[i];
  1775. break;
  1776. }
  1777. }
  1778. if (!st) {
  1779. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
  1780. return -1;
  1781. }
  1782. sc = st->priv_data;
  1783. if (sc->pseudo_stream_id+1 != frag->stsd_id)
  1784. return 0;
  1785. avio_r8(pb); /* version */
  1786. flags = avio_rb24(pb);
  1787. entries = avio_rb32(pb);
  1788. av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries);
  1789. /* Always assume the presence of composition time offsets.
  1790. * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
  1791. * 1) in the initial movie, there are no samples.
  1792. * 2) in the first movie fragment, there is only one sample without composition time offset.
  1793. * 3) in the subsequent movie fragments, there are samples with composition time offset. */
  1794. if (!sc->ctts_count && sc->sample_count)
  1795. {
  1796. /* Complement ctts table if moov atom doesn't have ctts atom. */
  1797. ctts_data = av_malloc(sizeof(*sc->ctts_data));
  1798. if (!ctts_data)
  1799. return AVERROR(ENOMEM);
  1800. sc->ctts_data = ctts_data;
  1801. sc->ctts_data[sc->ctts_count].count = sc->sample_count;
  1802. sc->ctts_data[sc->ctts_count].duration = 0;
  1803. sc->ctts_count++;
  1804. }
  1805. if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
  1806. return -1;
  1807. ctts_data = av_realloc(sc->ctts_data,
  1808. (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
  1809. if (!ctts_data)
  1810. return AVERROR(ENOMEM);
  1811. sc->ctts_data = ctts_data;
  1812. if (flags & 0x001) data_offset = avio_rb32(pb);
  1813. if (flags & 0x004) first_sample_flags = avio_rb32(pb);
  1814. dts = st->duration - sc->time_offset;
  1815. offset = frag->base_data_offset + data_offset;
  1816. distance = 0;
  1817. av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags);
  1818. for (i = 0; i < entries; i++) {
  1819. unsigned sample_size = frag->size;
  1820. int sample_flags = i ? frag->flags : first_sample_flags;
  1821. unsigned sample_duration = frag->duration;
  1822. int keyframe;
  1823. if (flags & 0x100) sample_duration = avio_rb32(pb);
  1824. if (flags & 0x200) sample_size = avio_rb32(pb);
  1825. if (flags & 0x400) sample_flags = avio_rb32(pb);
  1826. sc->ctts_data[sc->ctts_count].count = 1;
  1827. sc->ctts_data[sc->ctts_count].duration = (flags & 0x800) ? avio_rb32(pb) : 0;
  1828. sc->ctts_count++;
  1829. if ((keyframe = st->codec->codec_type == AVMEDIA_TYPE_AUDIO ||
  1830. (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))
  1831. distance = 0;
  1832. av_add_index_entry(st, offset, dts, sample_size, distance,
  1833. keyframe ? AVINDEX_KEYFRAME : 0);
  1834. av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1835. "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
  1836. offset, dts, sample_size, distance, keyframe);
  1837. distance++;
  1838. dts += sample_duration;
  1839. offset += sample_size;
  1840. }
  1841. frag->moof_offset = offset;
  1842. st->duration = dts + sc->time_offset;
  1843. return 0;
  1844. }
  1845. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1846. /* like the files created with Adobe Premiere 5.0, for samples see */
  1847. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1848. static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1849. {
  1850. int err;
  1851. if (atom.size < 8)
  1852. return 0; /* continue */
  1853. if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1854. avio_skip(pb, atom.size - 4);
  1855. return 0;
  1856. }
  1857. atom.type = avio_rl32(pb);
  1858. atom.size -= 8;
  1859. if (atom.type != MKTAG('m','d','a','t')) {
  1860. avio_skip(pb, atom.size);
  1861. return 0;
  1862. }
  1863. err = mov_read_mdat(c, pb, atom);
  1864. return err;
  1865. }
  1866. static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1867. {
  1868. #if CONFIG_ZLIB
  1869. AVIOContext ctx;
  1870. uint8_t *cmov_data;
  1871. uint8_t *moov_data; /* uncompressed data */
  1872. long cmov_len, moov_len;
  1873. int ret = -1;
  1874. avio_rb32(pb); /* dcom atom */
  1875. if (avio_rl32(pb) != MKTAG('d','c','o','m'))
  1876. return -1;
  1877. if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
  1878. av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !");
  1879. return -1;
  1880. }
  1881. avio_rb32(pb); /* cmvd atom */
  1882. if (avio_rl32(pb) != MKTAG('c','m','v','d'))
  1883. return -1;
  1884. moov_len = avio_rb32(pb); /* uncompressed size */
  1885. cmov_len = atom.size - 6 * 4;
  1886. cmov_data = av_malloc(cmov_len);
  1887. if (!cmov_data)
  1888. return AVERROR(ENOMEM);
  1889. moov_data = av_malloc(moov_len);
  1890. if (!moov_data) {
  1891. av_free(cmov_data);
  1892. return AVERROR(ENOMEM);
  1893. }
  1894. avio_read(pb, cmov_data, cmov_len);
  1895. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1896. goto free_and_return;
  1897. if(ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  1898. goto free_and_return;
  1899. atom.type = MKTAG('m','o','o','v');
  1900. atom.size = moov_len;
  1901. ret = mov_read_default(c, &ctx, atom);
  1902. free_and_return:
  1903. av_free(moov_data);
  1904. av_free(cmov_data);
  1905. return ret;
  1906. #else
  1907. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  1908. return -1;
  1909. #endif
  1910. }
  1911. /* edit list atom */
  1912. static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1913. {
  1914. MOVStreamContext *sc;
  1915. int i, edit_count, version;
  1916. if (c->fc->nb_streams < 1)
  1917. return 0;
  1918. sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  1919. version = avio_r8(pb); /* version */
  1920. avio_rb24(pb); /* flags */
  1921. edit_count = avio_rb32(pb); /* entries */
  1922. if((uint64_t)edit_count*12+8 > atom.size)
  1923. return -1;
  1924. for(i=0; i<edit_count; i++){
  1925. int64_t time;
  1926. int64_t duration;
  1927. if (version == 1) {
  1928. duration = avio_rb64(pb);
  1929. time = avio_rb64(pb);
  1930. } else {
  1931. duration = avio_rb32(pb); /* segment duration */
  1932. time = (int32_t)avio_rb32(pb); /* media time */
  1933. }
  1934. avio_rb32(pb); /* Media rate */
  1935. if (i == 0 && time >= -1) {
  1936. sc->time_offset = time != -1 ? time : -duration;
  1937. }
  1938. }
  1939. if(edit_count > 1)
  1940. av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
  1941. "a/v desync might occur, patch welcome\n");
  1942. av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
  1943. return 0;
  1944. }
  1945. static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1946. {
  1947. if (atom.size < 16)
  1948. return AVERROR_INVALIDDATA;
  1949. avio_skip(pb, 4);
  1950. ff_mov_read_chan(c->fc, atom.size - 4, c->fc->streams[0]->codec);
  1951. return 0;
  1952. }
  1953. static const MOVParseTableEntry mov_default_parse_table[] = {
  1954. { MKTAG('a','v','s','s'), mov_read_avss },
  1955. { MKTAG('c','h','p','l'), mov_read_chpl },
  1956. { MKTAG('c','o','6','4'), mov_read_stco },
  1957. { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
  1958. { MKTAG('d','i','n','f'), mov_read_default },
  1959. { MKTAG('d','r','e','f'), mov_read_dref },
  1960. { MKTAG('e','d','t','s'), mov_read_default },
  1961. { MKTAG('e','l','s','t'), mov_read_elst },
  1962. { MKTAG('e','n','d','a'), mov_read_enda },
  1963. { MKTAG('f','i','e','l'), mov_read_fiel },
  1964. { MKTAG('f','t','y','p'), mov_read_ftyp },
  1965. { MKTAG('g','l','b','l'), mov_read_glbl },
  1966. { MKTAG('h','d','l','r'), mov_read_hdlr },
  1967. { MKTAG('i','l','s','t'), mov_read_ilst },
  1968. { MKTAG('j','p','2','h'), mov_read_jp2h },
  1969. { MKTAG('m','d','a','t'), mov_read_mdat },
  1970. { MKTAG('m','d','h','d'), mov_read_mdhd },
  1971. { MKTAG('m','d','i','a'), mov_read_default },
  1972. { MKTAG('m','e','t','a'), mov_read_meta },
  1973. { MKTAG('m','i','n','f'), mov_read_default },
  1974. { MKTAG('m','o','o','f'), mov_read_moof },
  1975. { MKTAG('m','o','o','v'), mov_read_moov },
  1976. { MKTAG('m','v','e','x'), mov_read_default },
  1977. { MKTAG('m','v','h','d'), mov_read_mvhd },
  1978. { MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
  1979. { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
  1980. { MKTAG('a','v','c','C'), mov_read_glbl },
  1981. { MKTAG('p','a','s','p'), mov_read_pasp },
  1982. { MKTAG('s','t','b','l'), mov_read_default },
  1983. { MKTAG('s','t','c','o'), mov_read_stco },
  1984. { MKTAG('s','t','p','s'), mov_read_stps },
  1985. { MKTAG('s','t','r','f'), mov_read_strf },
  1986. { MKTAG('s','t','s','c'), mov_read_stsc },
  1987. { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
  1988. { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
  1989. { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
  1990. { MKTAG('s','t','t','s'), mov_read_stts },
  1991. { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
  1992. { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
  1993. { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
  1994. { MKTAG('t','r','a','k'), mov_read_trak },
  1995. { MKTAG('t','r','a','f'), mov_read_default },
  1996. { MKTAG('t','r','e','f'), mov_read_default },
  1997. { MKTAG('c','h','a','p'), mov_read_chap },
  1998. { MKTAG('t','r','e','x'), mov_read_trex },
  1999. { MKTAG('t','r','u','n'), mov_read_trun },
  2000. { MKTAG('u','d','t','a'), mov_read_default },
  2001. { MKTAG('w','a','v','e'), mov_read_wave },
  2002. { MKTAG('e','s','d','s'), mov_read_esds },
  2003. { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
  2004. { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
  2005. { MKTAG('w','f','e','x'), mov_read_wfex },
  2006. { MKTAG('c','m','o','v'), mov_read_cmov },
  2007. { MKTAG('c','h','a','n'), mov_read_chan },
  2008. { 0, NULL }
  2009. };
  2010. static int mov_probe(AVProbeData *p)
  2011. {
  2012. unsigned int offset;
  2013. uint32_t tag;
  2014. int score = 0;
  2015. /* check file header */
  2016. offset = 0;
  2017. for(;;) {
  2018. /* ignore invalid offset */
  2019. if ((offset + 8) > (unsigned int)p->buf_size)
  2020. return score;
  2021. tag = AV_RL32(p->buf + offset + 4);
  2022. switch(tag) {
  2023. /* check for obvious tags */
  2024. case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
  2025. case MKTAG('m','o','o','v'):
  2026. case MKTAG('m','d','a','t'):
  2027. case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
  2028. case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
  2029. case MKTAG('f','t','y','p'):
  2030. return AVPROBE_SCORE_MAX;
  2031. /* those are more common words, so rate then a bit less */
  2032. case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
  2033. case MKTAG('w','i','d','e'):
  2034. case MKTAG('f','r','e','e'):
  2035. case MKTAG('j','u','n','k'):
  2036. case MKTAG('p','i','c','t'):
  2037. return AVPROBE_SCORE_MAX - 5;
  2038. case MKTAG(0x82,0x82,0x7f,0x7d):
  2039. case MKTAG('s','k','i','p'):
  2040. case MKTAG('u','u','i','d'):
  2041. case MKTAG('p','r','f','l'):
  2042. offset = AV_RB32(p->buf+offset) + offset;
  2043. /* if we only find those cause probedata is too small at least rate them */
  2044. score = AVPROBE_SCORE_MAX - 50;
  2045. break;
  2046. default:
  2047. /* unrecognized tag */
  2048. return score;
  2049. }
  2050. }
  2051. }
  2052. // must be done after parsing all trak because there's no order requirement
  2053. static void mov_read_chapters(AVFormatContext *s)
  2054. {
  2055. MOVContext *mov = s->priv_data;
  2056. AVStream *st = NULL;
  2057. MOVStreamContext *sc;
  2058. int64_t cur_pos;
  2059. int i;
  2060. for (i = 0; i < s->nb_streams; i++)
  2061. if (s->streams[i]->id == mov->chapter_track) {
  2062. st = s->streams[i];
  2063. break;
  2064. }
  2065. if (!st) {
  2066. av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
  2067. return;
  2068. }
  2069. st->discard = AVDISCARD_ALL;
  2070. sc = st->priv_data;
  2071. cur_pos = avio_tell(sc->pb);
  2072. for (i = 0; i < st->nb_index_entries; i++) {
  2073. AVIndexEntry *sample = &st->index_entries[i];
  2074. int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
  2075. uint8_t *title;
  2076. uint16_t ch;
  2077. int len, title_len;
  2078. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  2079. av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
  2080. goto finish;
  2081. }
  2082. // the first two bytes are the length of the title
  2083. len = avio_rb16(sc->pb);
  2084. if (len > sample->size-2)
  2085. continue;
  2086. title_len = 2*len + 1;
  2087. if (!(title = av_mallocz(title_len)))
  2088. goto finish;
  2089. // The samples could theoretically be in any encoding if there's an encd
  2090. // atom following, but in practice are only utf-8 or utf-16, distinguished
  2091. // instead by the presence of a BOM
  2092. if (!len) {
  2093. title[0] = 0;
  2094. } else {
  2095. ch = avio_rb16(sc->pb);
  2096. if (ch == 0xfeff)
  2097. avio_get_str16be(sc->pb, len, title, title_len);
  2098. else if (ch == 0xfffe)
  2099. avio_get_str16le(sc->pb, len, title, title_len);
  2100. else {
  2101. AV_WB16(title, ch);
  2102. if (len == 1 || len == 2)
  2103. title[len] = 0;
  2104. else
  2105. get_strz(sc->pb, title + 2, len - 1);
  2106. }
  2107. }
  2108. ff_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
  2109. av_freep(&title);
  2110. }
  2111. finish:
  2112. avio_seek(sc->pb, cur_pos, SEEK_SET);
  2113. }
  2114. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  2115. {
  2116. MOVContext *mov = s->priv_data;
  2117. AVIOContext *pb = s->pb;
  2118. int err;
  2119. MOVAtom atom = { AV_RL32("root") };
  2120. mov->fc = s;
  2121. /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  2122. if(pb->seekable)
  2123. atom.size = avio_size(pb);
  2124. else
  2125. atom.size = INT64_MAX;
  2126. /* check MOV header */
  2127. if ((err = mov_read_default(mov, pb, atom)) < 0) {
  2128. av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
  2129. return err;
  2130. }
  2131. if (!mov->found_moov) {
  2132. av_log(s, AV_LOG_ERROR, "moov atom not found\n");
  2133. return -1;
  2134. }
  2135. av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
  2136. if (pb->seekable && mov->chapter_track > 0)
  2137. mov_read_chapters(s);
  2138. return 0;
  2139. }
  2140. static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
  2141. {
  2142. AVIndexEntry *sample = NULL;
  2143. int64_t best_dts = INT64_MAX;
  2144. int i;
  2145. for (i = 0; i < s->nb_streams; i++) {
  2146. AVStream *avst = s->streams[i];
  2147. MOVStreamContext *msc = avst->priv_data;
  2148. if (msc->pb && msc->current_sample < avst->nb_index_entries) {
  2149. AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
  2150. int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
  2151. av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  2152. if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
  2153. (s->pb->seekable &&
  2154. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
  2155. ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  2156. (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
  2157. sample = current_sample;
  2158. best_dts = dts;
  2159. *st = avst;
  2160. }
  2161. }
  2162. }
  2163. return sample;
  2164. }
  2165. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  2166. {
  2167. MOVContext *mov = s->priv_data;
  2168. MOVStreamContext *sc;
  2169. AVIndexEntry *sample;
  2170. AVStream *st = NULL;
  2171. int ret;
  2172. retry:
  2173. sample = mov_find_next_sample(s, &st);
  2174. if (!sample) {
  2175. mov->found_mdat = 0;
  2176. if (s->pb->seekable||
  2177. mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
  2178. url_feof(s->pb))
  2179. return AVERROR_EOF;
  2180. av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
  2181. goto retry;
  2182. }
  2183. sc = st->priv_data;
  2184. /* must be done just before reading, to avoid infinite loop on sample */
  2185. sc->current_sample++;
  2186. if (st->discard != AVDISCARD_ALL) {
  2187. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  2188. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  2189. sc->ffindex, sample->pos);
  2190. return -1;
  2191. }
  2192. ret = av_get_packet(sc->pb, pkt, sample->size);
  2193. if (ret < 0)
  2194. return ret;
  2195. #if CONFIG_DV_DEMUXER
  2196. if (mov->dv_demux && sc->dv_audio_container) {
  2197. dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
  2198. av_free(pkt->data);
  2199. pkt->size = 0;
  2200. ret = dv_get_packet(mov->dv_demux, pkt);
  2201. if (ret < 0)
  2202. return ret;
  2203. }
  2204. #endif
  2205. }
  2206. pkt->stream_index = sc->ffindex;
  2207. pkt->dts = sample->timestamp;
  2208. if (sc->ctts_data) {
  2209. pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
  2210. /* update ctts context */
  2211. sc->ctts_sample++;
  2212. if (sc->ctts_index < sc->ctts_count &&
  2213. sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
  2214. sc->ctts_index++;
  2215. sc->ctts_sample = 0;
  2216. }
  2217. if (sc->wrong_dts)
  2218. pkt->dts = AV_NOPTS_VALUE;
  2219. } else {
  2220. int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
  2221. st->index_entries[sc->current_sample].timestamp : st->duration;
  2222. pkt->duration = next_dts - pkt->dts;
  2223. pkt->pts = pkt->dts;
  2224. }
  2225. if (st->discard == AVDISCARD_ALL)
  2226. goto retry;
  2227. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
  2228. pkt->pos = sample->pos;
  2229. av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
  2230. pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
  2231. return 0;
  2232. }
  2233. static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
  2234. {
  2235. MOVStreamContext *sc = st->priv_data;
  2236. int sample, time_sample;
  2237. int i;
  2238. sample = av_index_search_timestamp(st, timestamp, flags);
  2239. av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  2240. if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
  2241. sample = 0;
  2242. if (sample < 0) /* not sure what to do */
  2243. return -1;
  2244. sc->current_sample = sample;
  2245. av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
  2246. /* adjust ctts index */
  2247. if (sc->ctts_data) {
  2248. time_sample = 0;
  2249. for (i = 0; i < sc->ctts_count; i++) {
  2250. int next = time_sample + sc->ctts_data[i].count;
  2251. if (next > sc->current_sample) {
  2252. sc->ctts_index = i;
  2253. sc->ctts_sample = sc->current_sample - time_sample;
  2254. break;
  2255. }
  2256. time_sample = next;
  2257. }
  2258. }
  2259. return sample;
  2260. }
  2261. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  2262. {
  2263. AVStream *st;
  2264. int64_t seek_timestamp, timestamp;
  2265. int sample;
  2266. int i;
  2267. if (stream_index >= s->nb_streams)
  2268. return -1;
  2269. if (sample_time < 0)
  2270. sample_time = 0;
  2271. st = s->streams[stream_index];
  2272. sample = mov_seek_stream(s, st, sample_time, flags);
  2273. if (sample < 0)
  2274. return -1;
  2275. /* adjust seek timestamp to found sample timestamp */
  2276. seek_timestamp = st->index_entries[sample].timestamp;
  2277. for (i = 0; i < s->nb_streams; i++) {
  2278. st = s->streams[i];
  2279. if (stream_index == i)
  2280. continue;
  2281. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  2282. mov_seek_stream(s, st, timestamp, flags);
  2283. }
  2284. return 0;
  2285. }
  2286. static int mov_read_close(AVFormatContext *s)
  2287. {
  2288. MOVContext *mov = s->priv_data;
  2289. int i, j;
  2290. for (i = 0; i < s->nb_streams; i++) {
  2291. AVStream *st = s->streams[i];
  2292. MOVStreamContext *sc = st->priv_data;
  2293. av_freep(&sc->ctts_data);
  2294. for (j = 0; j < sc->drefs_count; j++) {
  2295. av_freep(&sc->drefs[j].path);
  2296. av_freep(&sc->drefs[j].dir);
  2297. }
  2298. av_freep(&sc->drefs);
  2299. if (sc->pb && sc->pb != s->pb)
  2300. avio_close(sc->pb);
  2301. av_freep(&st->codec->palctrl);
  2302. }
  2303. if (mov->dv_demux) {
  2304. for(i = 0; i < mov->dv_fctx->nb_streams; i++) {
  2305. av_freep(&mov->dv_fctx->streams[i]->codec);
  2306. av_freep(&mov->dv_fctx->streams[i]);
  2307. }
  2308. av_freep(&mov->dv_fctx);
  2309. av_freep(&mov->dv_demux);
  2310. }
  2311. av_freep(&mov->trex_data);
  2312. return 0;
  2313. }
  2314. AVInputFormat ff_mov_demuxer = {
  2315. .name = "mov,mp4,m4a,3gp,3g2,mj2",
  2316. .long_name = NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),
  2317. .priv_data_size = sizeof(MOVContext),
  2318. .read_probe = mov_probe,
  2319. .read_header = mov_read_header,
  2320. .read_packet = mov_read_packet,
  2321. .read_close = mov_read_close,
  2322. .read_seek = mov_read_seek,
  2323. };