mov.c 89 KB

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