mov.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199
  1. /*
  2. * MOV demuxer
  3. * Copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <limits.h>
  22. //#define DEBUG
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/avstring.h"
  25. #include "avformat.h"
  26. #include "riff.h"
  27. #include "isom.h"
  28. #include "dv.h"
  29. #include "libavcodec/mpeg4audio.h"
  30. #include "libavcodec/mpegaudiodata.h"
  31. #if CONFIG_ZLIB
  32. #include <zlib.h>
  33. #endif
  34. /*
  35. * First version by Francois Revol revol@free.fr
  36. * Seek function by Gael Chardon gael.dev@4now.net
  37. *
  38. * Features and limitations:
  39. * - reads most of the QT files I have (at least the structure),
  40. * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
  41. * - the code is quite ugly... maybe I won't do it recursive next time :-)
  42. *
  43. * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
  44. * when coding this :) (it's a writer anyway)
  45. *
  46. * Reference documents:
  47. * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
  48. * Apple:
  49. * http://developer.apple.com/documentation/QuickTime/QTFF/
  50. * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
  51. * QuickTime is a trademark of Apple (AFAIK :))
  52. */
  53. #include "qtpalette.h"
  54. #undef NDEBUG
  55. #include <assert.h>
  56. /* the QuickTime file format is quite convoluted...
  57. * it has lots of index tables, each indexing something in another one...
  58. * Here we just use what is needed to read the chunks
  59. */
  60. typedef struct {
  61. int first;
  62. int count;
  63. int id;
  64. } MOVStsc;
  65. typedef struct {
  66. uint32_t type;
  67. char *path;
  68. } MOVDref;
  69. typedef struct {
  70. uint32_t type;
  71. int64_t offset;
  72. int64_t size; /* total size (excluding the size and type fields) */
  73. } MOVAtom;
  74. struct MOVParseTableEntry;
  75. typedef struct {
  76. unsigned track_id;
  77. uint64_t base_data_offset;
  78. uint64_t moof_offset;
  79. unsigned stsd_id;
  80. unsigned duration;
  81. unsigned size;
  82. unsigned flags;
  83. } MOVFragment;
  84. typedef struct {
  85. unsigned track_id;
  86. unsigned stsd_id;
  87. unsigned duration;
  88. unsigned size;
  89. unsigned flags;
  90. } MOVTrackExt;
  91. typedef struct MOVStreamContext {
  92. ByteIOContext *pb;
  93. int ffindex; /* the ffmpeg stream id */
  94. int next_chunk;
  95. unsigned int chunk_count;
  96. int64_t *chunk_offsets;
  97. unsigned int stts_count;
  98. MOVStts *stts_data;
  99. unsigned int ctts_count;
  100. MOVStts *ctts_data;
  101. unsigned int stsc_count;
  102. MOVStsc *stsc_data;
  103. int ctts_index;
  104. int ctts_sample;
  105. unsigned int sample_size;
  106. unsigned int sample_count;
  107. int *sample_sizes;
  108. unsigned int keyframe_count;
  109. int *keyframes;
  110. int time_scale;
  111. int time_rate;
  112. int time_offset; ///< time offset of the first edit list entry
  113. int current_sample;
  114. unsigned int bytes_per_frame;
  115. unsigned int samples_per_frame;
  116. int dv_audio_container;
  117. int pseudo_stream_id; ///< -1 means demux all ids
  118. int16_t audio_cid; ///< stsd audio compression id
  119. unsigned drefs_count;
  120. MOVDref *drefs;
  121. int dref_id;
  122. int wrong_dts; ///< dts are wrong due to negative ctts
  123. int width; ///< tkhd width
  124. int height; ///< tkhd height
  125. } MOVStreamContext;
  126. typedef struct MOVContext {
  127. AVFormatContext *fc;
  128. int time_scale;
  129. int64_t duration; /* duration of the longest track */
  130. int found_moov; /* when both 'moov' and 'mdat' sections has been found */
  131. int found_mdat; /* we suppose we have enough data to read the file */
  132. AVPaletteControl palette_control;
  133. DVDemuxContext *dv_demux;
  134. AVFormatContext *dv_fctx;
  135. int isom; /* 1 if file is ISO Media (mp4/3gp) */
  136. MOVFragment fragment; ///< current fragment in moof atom
  137. MOVTrackExt *trex_data;
  138. unsigned trex_count;
  139. int itunes_metadata; ///< metadata are itunes style
  140. } MOVContext;
  141. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  142. /* those functions parse an atom */
  143. /* return code:
  144. 0: continue to parse next atom
  145. <0: error occurred, exit
  146. */
  147. /* links atom IDs to parse functions */
  148. typedef struct MOVParseTableEntry {
  149. uint32_t type;
  150. int (*parse)(MOVContext *ctx, ByteIOContext *pb, MOVAtom atom);
  151. } MOVParseTableEntry;
  152. static const MOVParseTableEntry mov_default_parse_table[];
  153. static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  154. {
  155. int64_t total_size = 0;
  156. MOVAtom a;
  157. int i;
  158. int err = 0;
  159. a.offset = atom.offset;
  160. if (atom.size < 0)
  161. atom.size = INT64_MAX;
  162. while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
  163. a.size = atom.size;
  164. a.type=0;
  165. if(atom.size >= 8) {
  166. a.size = get_be32(pb);
  167. a.type = get_le32(pb);
  168. }
  169. total_size += 8;
  170. a.offset += 8;
  171. dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n",
  172. a.type, (char*)&a.type, a.size, atom.size, total_size);
  173. if (a.size == 1) { /* 64 bit extended size */
  174. a.size = get_be64(pb) - 8;
  175. a.offset += 8;
  176. total_size += 8;
  177. }
  178. if (a.size == 0) {
  179. a.size = atom.size - total_size;
  180. if (a.size <= 8)
  181. break;
  182. }
  183. a.size -= 8;
  184. if(a.size < 0)
  185. break;
  186. a.size = FFMIN(a.size, atom.size - total_size);
  187. for (i = 0; mov_default_parse_table[i].type != 0
  188. && mov_default_parse_table[i].type != a.type; i++)
  189. /* empty */;
  190. if (mov_default_parse_table[i].type == 0) { /* skip leaf atoms data */
  191. url_fskip(pb, a.size);
  192. } else {
  193. int64_t start_pos = url_ftell(pb);
  194. int64_t left;
  195. err = mov_default_parse_table[i].parse(c, pb, a);
  196. if (url_is_streamed(pb) && c->found_moov && c->found_mdat)
  197. break;
  198. left = a.size - url_ftell(pb) + start_pos;
  199. if (left > 0) /* skip garbage at atom end */
  200. url_fskip(pb, left);
  201. }
  202. a.offset += a.size;
  203. total_size += a.size;
  204. }
  205. if (!err && total_size < atom.size && atom.size < 0x7ffff)
  206. url_fskip(pb, atom.size - total_size);
  207. return err;
  208. }
  209. static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  210. {
  211. AVStream *st;
  212. MOVStreamContext *sc;
  213. int entries, i, j;
  214. if (c->fc->nb_streams < 1)
  215. return 0;
  216. st = c->fc->streams[c->fc->nb_streams-1];
  217. sc = st->priv_data;
  218. get_be32(pb); // version + flags
  219. entries = get_be32(pb);
  220. if (entries >= UINT_MAX / sizeof(*sc->drefs))
  221. return -1;
  222. sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
  223. if (!sc->drefs)
  224. return AVERROR(ENOMEM);
  225. sc->drefs_count = entries;
  226. for (i = 0; i < sc->drefs_count; i++) {
  227. MOVDref *dref = &sc->drefs[i];
  228. uint32_t size = get_be32(pb);
  229. int64_t next = url_ftell(pb) + size - 4;
  230. dref->type = get_le32(pb);
  231. get_be32(pb); // version + flags
  232. dprintf(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
  233. if (dref->type == MKTAG('a','l','i','s') && size > 150) {
  234. /* macintosh alias record */
  235. uint16_t volume_len, len;
  236. char volume[28];
  237. int16_t type;
  238. url_fskip(pb, 10);
  239. volume_len = get_byte(pb);
  240. volume_len = FFMIN(volume_len, 27);
  241. get_buffer(pb, volume, 27);
  242. volume[volume_len] = 0;
  243. av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", volume, volume_len);
  244. url_fskip(pb, 112);
  245. for (type = 0; type != -1 && url_ftell(pb) < next; ) {
  246. type = get_be16(pb);
  247. len = get_be16(pb);
  248. av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
  249. if (len&1)
  250. len += 1;
  251. if (type == 2) { // absolute path
  252. av_free(dref->path);
  253. dref->path = av_mallocz(len+1);
  254. if (!dref->path)
  255. return AVERROR(ENOMEM);
  256. get_buffer(pb, dref->path, len);
  257. if (len > volume_len && !strncmp(dref->path, volume, volume_len)) {
  258. len -= volume_len;
  259. memmove(dref->path, dref->path+volume_len, len);
  260. dref->path[len] = 0;
  261. }
  262. for (j = 0; j < len; j++)
  263. if (dref->path[j] == ':')
  264. dref->path[j] = '/';
  265. av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
  266. } else
  267. url_fskip(pb, len);
  268. }
  269. }
  270. url_fseek(pb, next, SEEK_SET);
  271. }
  272. return 0;
  273. }
  274. static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  275. {
  276. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  277. uint32_t type;
  278. uint32_t ctype;
  279. get_byte(pb); /* version */
  280. get_be24(pb); /* flags */
  281. /* component type */
  282. ctype = get_le32(pb);
  283. type = get_le32(pb); /* component subtype */
  284. dprintf(c->fc, "ctype= %c%c%c%c (0x%08x)\n", *((char *)&ctype), ((char *)&ctype)[1],
  285. ((char *)&ctype)[2], ((char *)&ctype)[3], (int) ctype);
  286. dprintf(c->fc, "stype= %c%c%c%c\n",
  287. *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
  288. if(!ctype)
  289. c->isom = 1;
  290. if (type == MKTAG('v','i','d','e'))
  291. st->codec->codec_type = CODEC_TYPE_VIDEO;
  292. else if(type == MKTAG('s','o','u','n'))
  293. st->codec->codec_type = CODEC_TYPE_AUDIO;
  294. else if(type == MKTAG('m','1','a',' '))
  295. st->codec->codec_id = CODEC_ID_MP2;
  296. else if(type == MKTAG('s','u','b','p')) {
  297. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  298. }
  299. get_be32(pb); /* component manufacture */
  300. get_be32(pb); /* component flags */
  301. get_be32(pb); /* component flags mask */
  302. if(atom.size <= 24)
  303. return 0; /* nothing left to read */
  304. url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
  305. return 0;
  306. }
  307. static int mp4_read_descr_len(ByteIOContext *pb)
  308. {
  309. int len = 0;
  310. int count = 4;
  311. while (count--) {
  312. int c = get_byte(pb);
  313. len = (len << 7) | (c & 0x7f);
  314. if (!(c & 0x80))
  315. break;
  316. }
  317. return len;
  318. }
  319. static int mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag)
  320. {
  321. int len;
  322. *tag = get_byte(pb);
  323. len = mp4_read_descr_len(pb);
  324. dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
  325. return len;
  326. }
  327. #define MP4ESDescrTag 0x03
  328. #define MP4DecConfigDescrTag 0x04
  329. #define MP4DecSpecificDescrTag 0x05
  330. static const AVCodecTag mp4_audio_types[] = {
  331. { CODEC_ID_MP3ON4, 29 }, /* old mp3on4 draft */
  332. { CODEC_ID_MP3ON4, 32 }, /* layer 1 */
  333. { CODEC_ID_MP3ON4, 33 }, /* layer 2 */
  334. { CODEC_ID_MP3ON4, 34 }, /* layer 3 */
  335. { CODEC_ID_NONE, 0 },
  336. };
  337. static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  338. {
  339. AVStream *st;
  340. int tag, len;
  341. if (c->fc->nb_streams < 1)
  342. return 0;
  343. st = c->fc->streams[c->fc->nb_streams-1];
  344. get_be32(pb); /* version + flags */
  345. len = mp4_read_descr(c, pb, &tag);
  346. if (tag == MP4ESDescrTag) {
  347. get_be16(pb); /* ID */
  348. get_byte(pb); /* priority */
  349. } else
  350. get_be16(pb); /* ID */
  351. len = mp4_read_descr(c, pb, &tag);
  352. if (tag == MP4DecConfigDescrTag) {
  353. int object_type_id = get_byte(pb);
  354. get_byte(pb); /* stream type */
  355. get_be24(pb); /* buffer size db */
  356. get_be32(pb); /* max bitrate */
  357. get_be32(pb); /* avg bitrate */
  358. st->codec->codec_id= codec_get_id(ff_mp4_obj_type, object_type_id);
  359. dprintf(c->fc, "esds object type id %d\n", object_type_id);
  360. len = mp4_read_descr(c, pb, &tag);
  361. if (tag == MP4DecSpecificDescrTag) {
  362. dprintf(c->fc, "Specific MPEG4 header len=%d\n", len);
  363. if((uint64_t)len > (1<<30))
  364. return -1;
  365. st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
  366. if (!st->codec->extradata)
  367. return AVERROR(ENOMEM);
  368. get_buffer(pb, st->codec->extradata, len);
  369. st->codec->extradata_size = len;
  370. if (st->codec->codec_id == CODEC_ID_AAC) {
  371. MPEG4AudioConfig cfg;
  372. ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
  373. st->codec->extradata_size);
  374. if (cfg.chan_config > 7)
  375. return -1;
  376. st->codec->channels = ff_mpeg4audio_channels[cfg.chan_config];
  377. if (cfg.object_type == 29 && cfg.sampling_index < 3) // old mp3on4
  378. st->codec->sample_rate = ff_mpa_freq_tab[cfg.sampling_index];
  379. else
  380. st->codec->sample_rate = cfg.sample_rate; // ext sample rate ?
  381. dprintf(c->fc, "mp4a config channels %d obj %d ext obj %d "
  382. "sample rate %d ext sample rate %d\n", st->codec->channels,
  383. cfg.object_type, cfg.ext_object_type,
  384. cfg.sample_rate, cfg.ext_sample_rate);
  385. if (!(st->codec->codec_id = codec_get_id(mp4_audio_types,
  386. cfg.object_type)))
  387. st->codec->codec_id = CODEC_ID_AAC;
  388. }
  389. }
  390. }
  391. return 0;
  392. }
  393. static int mov_read_pasp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  394. {
  395. const int num = get_be32(pb);
  396. const int den = get_be32(pb);
  397. AVStream *st;
  398. if (c->fc->nb_streams < 1)
  399. return 0;
  400. st = c->fc->streams[c->fc->nb_streams-1];
  401. if (den != 0) {
  402. if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
  403. (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num))
  404. av_log(c->fc, AV_LOG_WARNING,
  405. "sample aspect ratio already set to %d:%d, overriding by 'pasp' atom\n",
  406. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
  407. st->sample_aspect_ratio.num = num;
  408. st->sample_aspect_ratio.den = den;
  409. }
  410. return 0;
  411. }
  412. /* this atom contains actual media data */
  413. static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  414. {
  415. if(atom.size == 0) /* wrong one (MP4) */
  416. return 0;
  417. c->found_mdat=1;
  418. return 0; /* now go for moov */
  419. }
  420. static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  421. {
  422. uint32_t type = get_le32(pb);
  423. if (type != MKTAG('q','t',' ',' '))
  424. c->isom = 1;
  425. av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
  426. get_be32(pb); /* minor version */
  427. url_fskip(pb, atom.size - 8);
  428. return 0;
  429. }
  430. /* this atom should contain all header atoms */
  431. static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  432. {
  433. if (mov_read_default(c, pb, atom) < 0)
  434. return -1;
  435. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  436. /* so we don't parse the whole file if over a network */
  437. c->found_moov=1;
  438. return 0; /* now go for mdat */
  439. }
  440. static int mov_read_moof(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  441. {
  442. c->fragment.moof_offset = url_ftell(pb) - 8;
  443. dprintf(c->fc, "moof offset %llx\n", c->fragment.moof_offset);
  444. return mov_read_default(c, pb, atom);
  445. }
  446. static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  447. {
  448. AVStream *st;
  449. MOVStreamContext *sc;
  450. int version;
  451. char language[4] = {0};
  452. unsigned lang;
  453. if (c->fc->nb_streams < 1)
  454. return 0;
  455. st = c->fc->streams[c->fc->nb_streams-1];
  456. sc = st->priv_data;
  457. version = get_byte(pb);
  458. if (version > 1)
  459. return -1; /* unsupported */
  460. get_be24(pb); /* flags */
  461. if (version == 1) {
  462. get_be64(pb);
  463. get_be64(pb);
  464. } else {
  465. get_be32(pb); /* creation time */
  466. get_be32(pb); /* modification time */
  467. }
  468. sc->time_scale = get_be32(pb);
  469. st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
  470. lang = get_be16(pb); /* language */
  471. if (ff_mov_lang_to_iso639(lang, language))
  472. av_metadata_set(&st->metadata, "language", language);
  473. get_be16(pb); /* quality */
  474. return 0;
  475. }
  476. static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  477. {
  478. int version = get_byte(pb); /* version */
  479. get_be24(pb); /* flags */
  480. if (version == 1) {
  481. get_be64(pb);
  482. get_be64(pb);
  483. } else {
  484. get_be32(pb); /* creation time */
  485. get_be32(pb); /* modification time */
  486. }
  487. c->time_scale = get_be32(pb); /* time scale */
  488. dprintf(c->fc, "time scale = %i\n", c->time_scale);
  489. c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
  490. get_be32(pb); /* preferred scale */
  491. get_be16(pb); /* preferred volume */
  492. url_fskip(pb, 10); /* reserved */
  493. url_fskip(pb, 36); /* display matrix */
  494. get_be32(pb); /* preview time */
  495. get_be32(pb); /* preview duration */
  496. get_be32(pb); /* poster time */
  497. get_be32(pb); /* selection time */
  498. get_be32(pb); /* selection duration */
  499. get_be32(pb); /* current time */
  500. get_be32(pb); /* next track ID */
  501. return 0;
  502. }
  503. static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  504. {
  505. AVStream *st;
  506. if (c->fc->nb_streams < 1)
  507. return 0;
  508. st = c->fc->streams[c->fc->nb_streams-1];
  509. if((uint64_t)atom.size > (1<<30))
  510. return -1;
  511. // currently SVQ3 decoder expect full STSD header - so let's fake it
  512. // this should be fixed and just SMI header should be passed
  513. av_free(st->codec->extradata);
  514. st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE);
  515. if (!st->codec->extradata)
  516. return AVERROR(ENOMEM);
  517. st->codec->extradata_size = 0x5a + atom.size;
  518. memcpy(st->codec->extradata, "SVQ3", 4); // fake
  519. get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
  520. dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
  521. return 0;
  522. }
  523. static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  524. {
  525. AVStream *st;
  526. int little_endian;
  527. if (c->fc->nb_streams < 1)
  528. return 0;
  529. st = c->fc->streams[c->fc->nb_streams-1];
  530. little_endian = get_be16(pb);
  531. dprintf(c->fc, "enda %d\n", little_endian);
  532. if (little_endian == 1) {
  533. switch (st->codec->codec_id) {
  534. case CODEC_ID_PCM_S24BE:
  535. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  536. break;
  537. case CODEC_ID_PCM_S32BE:
  538. st->codec->codec_id = CODEC_ID_PCM_S32LE;
  539. break;
  540. case CODEC_ID_PCM_F32BE:
  541. st->codec->codec_id = CODEC_ID_PCM_F32LE;
  542. break;
  543. case CODEC_ID_PCM_F64BE:
  544. st->codec->codec_id = CODEC_ID_PCM_F64LE;
  545. break;
  546. default:
  547. break;
  548. }
  549. }
  550. return 0;
  551. }
  552. /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
  553. static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  554. {
  555. AVStream *st;
  556. uint64_t size;
  557. uint8_t *buf;
  558. if (c->fc->nb_streams < 1) // will happen with jp2 files
  559. return 0;
  560. st= c->fc->streams[c->fc->nb_streams-1];
  561. size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
  562. if(size > INT_MAX || (uint64_t)atom.size > INT_MAX)
  563. return -1;
  564. buf= av_realloc(st->codec->extradata, size);
  565. if(!buf)
  566. return -1;
  567. st->codec->extradata= buf;
  568. buf+= st->codec->extradata_size;
  569. st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
  570. AV_WB32( buf , atom.size + 8);
  571. AV_WL32( buf + 4, atom.type);
  572. get_buffer(pb, buf + 8, atom.size);
  573. return 0;
  574. }
  575. static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  576. {
  577. AVStream *st;
  578. if (c->fc->nb_streams < 1)
  579. return 0;
  580. st = c->fc->streams[c->fc->nb_streams-1];
  581. if((uint64_t)atom.size > (1<<30))
  582. return -1;
  583. if (st->codec->codec_id == CODEC_ID_QDM2) {
  584. // pass all frma atom to codec, needed at least for QDM2
  585. av_free(st->codec->extradata);
  586. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  587. if (!st->codec->extradata)
  588. return AVERROR(ENOMEM);
  589. st->codec->extradata_size = atom.size;
  590. get_buffer(pb, st->codec->extradata, atom.size);
  591. } else if (atom.size > 8) { /* to read frma, esds atoms */
  592. if (mov_read_default(c, pb, atom) < 0)
  593. return -1;
  594. } else
  595. url_fskip(pb, atom.size);
  596. return 0;
  597. }
  598. /**
  599. * This function reads atom content and puts data in extradata without tag
  600. * nor size unlike mov_read_extradata.
  601. */
  602. static int mov_read_glbl(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  603. {
  604. AVStream *st;
  605. if (c->fc->nb_streams < 1)
  606. return 0;
  607. st = c->fc->streams[c->fc->nb_streams-1];
  608. if((uint64_t)atom.size > (1<<30))
  609. return -1;
  610. av_free(st->codec->extradata);
  611. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  612. if (!st->codec->extradata)
  613. return AVERROR(ENOMEM);
  614. st->codec->extradata_size = atom.size;
  615. get_buffer(pb, st->codec->extradata, atom.size);
  616. return 0;
  617. }
  618. static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  619. {
  620. AVStream *st;
  621. MOVStreamContext *sc;
  622. unsigned int i, entries;
  623. if (c->fc->nb_streams < 1)
  624. return 0;
  625. st = c->fc->streams[c->fc->nb_streams-1];
  626. sc = st->priv_data;
  627. get_byte(pb); /* version */
  628. get_be24(pb); /* flags */
  629. entries = get_be32(pb);
  630. if(entries >= UINT_MAX/sizeof(int64_t))
  631. return -1;
  632. sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
  633. if (!sc->chunk_offsets)
  634. return AVERROR(ENOMEM);
  635. sc->chunk_count = entries;
  636. if (atom.type == MKTAG('s','t','c','o'))
  637. for(i=0; i<entries; i++)
  638. sc->chunk_offsets[i] = get_be32(pb);
  639. else if (atom.type == MKTAG('c','o','6','4'))
  640. for(i=0; i<entries; i++)
  641. sc->chunk_offsets[i] = get_be64(pb);
  642. else
  643. return -1;
  644. return 0;
  645. }
  646. /**
  647. * Compute codec id for 'lpcm' tag.
  648. * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
  649. */
  650. static enum CodecID mov_get_lpcm_codec_id(int bps, int flags)
  651. {
  652. if (flags & 1) { // floating point
  653. if (flags & 2) { // big endian
  654. if (bps == 32) return CODEC_ID_PCM_F32BE;
  655. else if (bps == 64) return CODEC_ID_PCM_F64BE;
  656. } else {
  657. if (bps == 32) return CODEC_ID_PCM_F32LE;
  658. else if (bps == 64) return CODEC_ID_PCM_F64LE;
  659. }
  660. } else {
  661. if (flags & 2) {
  662. if (bps == 8)
  663. // signed integer
  664. if (flags & 4) return CODEC_ID_PCM_S8;
  665. else return CODEC_ID_PCM_U8;
  666. else if (bps == 16) return CODEC_ID_PCM_S16BE;
  667. else if (bps == 24) return CODEC_ID_PCM_S24BE;
  668. else if (bps == 32) return CODEC_ID_PCM_S32BE;
  669. } else {
  670. if (bps == 8)
  671. if (flags & 4) return CODEC_ID_PCM_S8;
  672. else return CODEC_ID_PCM_U8;
  673. else if (bps == 16) return CODEC_ID_PCM_S16LE;
  674. else if (bps == 24) return CODEC_ID_PCM_S24LE;
  675. else if (bps == 32) return CODEC_ID_PCM_S32LE;
  676. }
  677. }
  678. return CODEC_ID_NONE;
  679. }
  680. static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  681. {
  682. AVStream *st;
  683. MOVStreamContext *sc;
  684. int j, entries, pseudo_stream_id;
  685. if (c->fc->nb_streams < 1)
  686. return 0;
  687. st = c->fc->streams[c->fc->nb_streams-1];
  688. sc = st->priv_data;
  689. get_byte(pb); /* version */
  690. get_be24(pb); /* flags */
  691. entries = get_be32(pb);
  692. for(pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
  693. //Parsing Sample description table
  694. enum CodecID id;
  695. int dref_id;
  696. MOVAtom a = { 0, 0, 0 };
  697. int64_t start_pos = url_ftell(pb);
  698. int size = get_be32(pb); /* size */
  699. uint32_t format = get_le32(pb); /* data format */
  700. get_be32(pb); /* reserved */
  701. get_be16(pb); /* reserved */
  702. dref_id = get_be16(pb);
  703. if (st->codec->codec_tag &&
  704. st->codec->codec_tag != format &&
  705. (c->fc->video_codec_id ? codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id
  706. : st->codec->codec_tag != MKTAG('j','p','e','g'))
  707. ){
  708. /* Multiple fourcc, we skip JPEG. This is not correct, we should
  709. * export it as a separate AVStream but this needs a few changes
  710. * in the MOV demuxer, patch welcome. */
  711. av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
  712. url_fskip(pb, size - (url_ftell(pb) - start_pos));
  713. continue;
  714. }
  715. sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
  716. sc->dref_id= dref_id;
  717. st->codec->codec_tag = format;
  718. id = codec_get_id(codec_movaudio_tags, format);
  719. if (id<=0 && (format&0xFFFF) == 'm'+('s'<<8))
  720. id = codec_get_id(codec_wav_tags, bswap_32(format)&0xFFFF);
  721. if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
  722. st->codec->codec_type = CODEC_TYPE_AUDIO;
  723. } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
  724. format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */
  725. id = codec_get_id(codec_movvideo_tags, format);
  726. if (id <= 0)
  727. id = codec_get_id(codec_bmp_tags, format);
  728. if (id > 0)
  729. st->codec->codec_type = CODEC_TYPE_VIDEO;
  730. else if(st->codec->codec_type == CODEC_TYPE_DATA){
  731. id = codec_get_id(ff_codec_movsubtitle_tags, format);
  732. if(id > 0)
  733. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  734. }
  735. }
  736. dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
  737. (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
  738. (format >> 24) & 0xff, st->codec->codec_type);
  739. if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
  740. uint8_t codec_name[32];
  741. unsigned int color_depth;
  742. int color_greyscale;
  743. st->codec->codec_id = id;
  744. get_be16(pb); /* version */
  745. get_be16(pb); /* revision level */
  746. get_be32(pb); /* vendor */
  747. get_be32(pb); /* temporal quality */
  748. get_be32(pb); /* spatial quality */
  749. st->codec->width = get_be16(pb); /* width */
  750. st->codec->height = get_be16(pb); /* height */
  751. get_be32(pb); /* horiz resolution */
  752. get_be32(pb); /* vert resolution */
  753. get_be32(pb); /* data size, always 0 */
  754. get_be16(pb); /* frames per samples */
  755. get_buffer(pb, codec_name, 32); /* codec name, pascal string */
  756. if (codec_name[0] <= 31) {
  757. memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
  758. st->codec->codec_name[codec_name[0]] = 0;
  759. }
  760. st->codec->bits_per_coded_sample = get_be16(pb); /* depth */
  761. st->codec->color_table_id = get_be16(pb); /* colortable id */
  762. dprintf(c->fc, "depth %d, ctab id %d\n",
  763. st->codec->bits_per_coded_sample, st->codec->color_table_id);
  764. /* figure out the palette situation */
  765. color_depth = st->codec->bits_per_coded_sample & 0x1F;
  766. color_greyscale = st->codec->bits_per_coded_sample & 0x20;
  767. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  768. if ((color_depth == 2) || (color_depth == 4) ||
  769. (color_depth == 8)) {
  770. /* for palette traversal */
  771. unsigned int color_start, color_count, color_end;
  772. unsigned char r, g, b;
  773. if (color_greyscale) {
  774. int color_index, color_dec;
  775. /* compute the greyscale palette */
  776. st->codec->bits_per_coded_sample = color_depth;
  777. color_count = 1 << color_depth;
  778. color_index = 255;
  779. color_dec = 256 / (color_count - 1);
  780. for (j = 0; j < color_count; j++) {
  781. r = g = b = color_index;
  782. c->palette_control.palette[j] =
  783. (r << 16) | (g << 8) | (b);
  784. color_index -= color_dec;
  785. if (color_index < 0)
  786. color_index = 0;
  787. }
  788. } else if (st->codec->color_table_id) {
  789. const uint8_t *color_table;
  790. /* if flag bit 3 is set, use the default palette */
  791. color_count = 1 << color_depth;
  792. if (color_depth == 2)
  793. color_table = ff_qt_default_palette_4;
  794. else if (color_depth == 4)
  795. color_table = ff_qt_default_palette_16;
  796. else
  797. color_table = ff_qt_default_palette_256;
  798. for (j = 0; j < color_count; j++) {
  799. r = color_table[j * 4 + 0];
  800. g = color_table[j * 4 + 1];
  801. b = color_table[j * 4 + 2];
  802. c->palette_control.palette[j] =
  803. (r << 16) | (g << 8) | (b);
  804. }
  805. } else {
  806. /* load the palette from the file */
  807. color_start = get_be32(pb);
  808. color_count = get_be16(pb);
  809. color_end = get_be16(pb);
  810. if ((color_start <= 255) &&
  811. (color_end <= 255)) {
  812. for (j = color_start; j <= color_end; j++) {
  813. /* each R, G, or B component is 16 bits;
  814. * only use the top 8 bits; skip alpha bytes
  815. * up front */
  816. get_byte(pb);
  817. get_byte(pb);
  818. r = get_byte(pb);
  819. get_byte(pb);
  820. g = get_byte(pb);
  821. get_byte(pb);
  822. b = get_byte(pb);
  823. get_byte(pb);
  824. c->palette_control.palette[j] =
  825. (r << 16) | (g << 8) | (b);
  826. }
  827. }
  828. }
  829. st->codec->palctrl = &c->palette_control;
  830. st->codec->palctrl->palette_changed = 1;
  831. } else
  832. st->codec->palctrl = NULL;
  833. } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
  834. int bits_per_sample, flags;
  835. uint16_t version = get_be16(pb);
  836. st->codec->codec_id = id;
  837. get_be16(pb); /* revision level */
  838. get_be32(pb); /* vendor */
  839. st->codec->channels = get_be16(pb); /* channel count */
  840. dprintf(c->fc, "audio channels %d\n", st->codec->channels);
  841. st->codec->bits_per_coded_sample = get_be16(pb); /* sample size */
  842. sc->audio_cid = get_be16(pb);
  843. get_be16(pb); /* packet size = 0 */
  844. st->codec->sample_rate = ((get_be32(pb) >> 16));
  845. //Read QT version 1 fields. In version 0 these do not exist.
  846. dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
  847. if(!c->isom) {
  848. if(version==1) {
  849. sc->samples_per_frame = get_be32(pb);
  850. get_be32(pb); /* bytes per packet */
  851. sc->bytes_per_frame = get_be32(pb);
  852. get_be32(pb); /* bytes per sample */
  853. } else if(version==2) {
  854. get_be32(pb); /* sizeof struct only */
  855. st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
  856. st->codec->channels = get_be32(pb);
  857. get_be32(pb); /* always 0x7F000000 */
  858. st->codec->bits_per_coded_sample = get_be32(pb); /* bits per channel if sound is uncompressed */
  859. flags = get_be32(pb); /* lcpm format specific flag */
  860. sc->bytes_per_frame = get_be32(pb); /* bytes per audio packet if constant */
  861. sc->samples_per_frame = get_be32(pb); /* lpcm frames per audio packet if constant */
  862. if (format == MKTAG('l','p','c','m'))
  863. st->codec->codec_id = mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
  864. }
  865. }
  866. switch (st->codec->codec_id) {
  867. case CODEC_ID_PCM_S8:
  868. case CODEC_ID_PCM_U8:
  869. if (st->codec->bits_per_coded_sample == 16)
  870. st->codec->codec_id = CODEC_ID_PCM_S16BE;
  871. break;
  872. case CODEC_ID_PCM_S16LE:
  873. case CODEC_ID_PCM_S16BE:
  874. if (st->codec->bits_per_coded_sample == 8)
  875. st->codec->codec_id = CODEC_ID_PCM_S8;
  876. else if (st->codec->bits_per_coded_sample == 24)
  877. st->codec->codec_id =
  878. st->codec->codec_id == CODEC_ID_PCM_S16BE ?
  879. CODEC_ID_PCM_S24BE : CODEC_ID_PCM_S24LE;
  880. break;
  881. /* set values for old format before stsd version 1 appeared */
  882. case CODEC_ID_MACE3:
  883. sc->samples_per_frame = 6;
  884. sc->bytes_per_frame = 2*st->codec->channels;
  885. break;
  886. case CODEC_ID_MACE6:
  887. sc->samples_per_frame = 6;
  888. sc->bytes_per_frame = 1*st->codec->channels;
  889. break;
  890. case CODEC_ID_ADPCM_IMA_QT:
  891. sc->samples_per_frame = 64;
  892. sc->bytes_per_frame = 34*st->codec->channels;
  893. break;
  894. case CODEC_ID_GSM:
  895. sc->samples_per_frame = 160;
  896. sc->bytes_per_frame = 33;
  897. break;
  898. default:
  899. break;
  900. }
  901. bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
  902. if (bits_per_sample) {
  903. st->codec->bits_per_coded_sample = bits_per_sample;
  904. sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
  905. }
  906. } else if(st->codec->codec_type==CODEC_TYPE_SUBTITLE){
  907. // ttxt stsd contains display flags, justification, background
  908. // color, fonts, and default styles, so fake an atom to read it
  909. MOVAtom fake_atom = { .size = size - (url_ftell(pb) - start_pos) };
  910. mov_read_glbl(c, pb, fake_atom);
  911. st->codec->codec_id= id;
  912. st->codec->width = sc->width;
  913. st->codec->height = sc->height;
  914. } else {
  915. /* other codec type, just skip (rtp, mp4s, tmcd ...) */
  916. url_fskip(pb, size - (url_ftell(pb) - start_pos));
  917. }
  918. /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
  919. a.size = size - (url_ftell(pb) - start_pos);
  920. if (a.size > 8) {
  921. if (mov_read_default(c, pb, a) < 0)
  922. return -1;
  923. } else if (a.size > 0)
  924. url_fskip(pb, a.size);
  925. }
  926. if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
  927. st->codec->sample_rate= sc->time_scale;
  928. /* special codec parameters handling */
  929. switch (st->codec->codec_id) {
  930. #if CONFIG_DV_DEMUXER
  931. case CODEC_ID_DVAUDIO:
  932. c->dv_fctx = avformat_alloc_context();
  933. c->dv_demux = dv_init_demux(c->dv_fctx);
  934. if (!c->dv_demux) {
  935. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  936. return -1;
  937. }
  938. sc->dv_audio_container = 1;
  939. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  940. break;
  941. #endif
  942. /* no ifdef since parameters are always those */
  943. case CODEC_ID_QCELP:
  944. st->codec->frame_size= 160;
  945. st->codec->channels= 1; /* really needed */
  946. break;
  947. case CODEC_ID_AMR_NB:
  948. case CODEC_ID_AMR_WB:
  949. st->codec->frame_size= sc->samples_per_frame;
  950. st->codec->channels= 1; /* really needed */
  951. /* force sample rate for amr, stsd in 3gp does not store sample rate */
  952. if (st->codec->codec_id == CODEC_ID_AMR_NB)
  953. st->codec->sample_rate = 8000;
  954. else if (st->codec->codec_id == CODEC_ID_AMR_WB)
  955. st->codec->sample_rate = 16000;
  956. break;
  957. case CODEC_ID_MP2:
  958. case CODEC_ID_MP3:
  959. st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
  960. st->need_parsing = AVSTREAM_PARSE_FULL;
  961. break;
  962. case CODEC_ID_GSM:
  963. case CODEC_ID_ADPCM_MS:
  964. case CODEC_ID_ADPCM_IMA_WAV:
  965. st->codec->block_align = sc->bytes_per_frame;
  966. break;
  967. case CODEC_ID_ALAC:
  968. if (st->codec->extradata_size == 36) {
  969. st->codec->frame_size = AV_RB32(st->codec->extradata+12);
  970. st->codec->channels = AV_RB8 (st->codec->extradata+21);
  971. }
  972. break;
  973. default:
  974. break;
  975. }
  976. return 0;
  977. }
  978. static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  979. {
  980. AVStream *st;
  981. MOVStreamContext *sc;
  982. unsigned int i, entries;
  983. if (c->fc->nb_streams < 1)
  984. return 0;
  985. st = c->fc->streams[c->fc->nb_streams-1];
  986. sc = st->priv_data;
  987. get_byte(pb); /* version */
  988. get_be24(pb); /* flags */
  989. entries = get_be32(pb);
  990. dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  991. if(entries >= UINT_MAX / sizeof(*sc->stsc_data))
  992. return -1;
  993. sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
  994. if (!sc->stsc_data)
  995. return AVERROR(ENOMEM);
  996. sc->stsc_count = entries;
  997. for(i=0; i<entries; i++) {
  998. sc->stsc_data[i].first = get_be32(pb);
  999. sc->stsc_data[i].count = get_be32(pb);
  1000. sc->stsc_data[i].id = get_be32(pb);
  1001. }
  1002. return 0;
  1003. }
  1004. static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1005. {
  1006. AVStream *st;
  1007. MOVStreamContext *sc;
  1008. unsigned int i, entries;
  1009. if (c->fc->nb_streams < 1)
  1010. return 0;
  1011. st = c->fc->streams[c->fc->nb_streams-1];
  1012. sc = st->priv_data;
  1013. get_byte(pb); /* version */
  1014. get_be24(pb); /* flags */
  1015. entries = get_be32(pb);
  1016. dprintf(c->fc, "keyframe_count = %d\n", entries);
  1017. if(entries >= UINT_MAX / sizeof(int))
  1018. return -1;
  1019. sc->keyframes = av_malloc(entries * sizeof(int));
  1020. if (!sc->keyframes)
  1021. return AVERROR(ENOMEM);
  1022. sc->keyframe_count = entries;
  1023. for(i=0; i<entries; i++) {
  1024. sc->keyframes[i] = get_be32(pb);
  1025. //dprintf(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
  1026. }
  1027. return 0;
  1028. }
  1029. static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1030. {
  1031. AVStream *st;
  1032. MOVStreamContext *sc;
  1033. unsigned int i, entries, sample_size;
  1034. if (c->fc->nb_streams < 1)
  1035. return 0;
  1036. st = c->fc->streams[c->fc->nb_streams-1];
  1037. sc = st->priv_data;
  1038. get_byte(pb); /* version */
  1039. get_be24(pb); /* flags */
  1040. sample_size = get_be32(pb);
  1041. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  1042. sc->sample_size = sample_size;
  1043. entries = get_be32(pb);
  1044. dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
  1045. sc->sample_count = entries;
  1046. if (sample_size)
  1047. return 0;
  1048. if(entries >= UINT_MAX / sizeof(int))
  1049. return -1;
  1050. sc->sample_sizes = av_malloc(entries * sizeof(int));
  1051. if (!sc->sample_sizes)
  1052. return AVERROR(ENOMEM);
  1053. for(i=0; i<entries; i++)
  1054. sc->sample_sizes[i] = get_be32(pb);
  1055. return 0;
  1056. }
  1057. static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1058. {
  1059. AVStream *st;
  1060. MOVStreamContext *sc;
  1061. unsigned int i, entries;
  1062. int64_t duration=0;
  1063. int64_t total_sample_count=0;
  1064. if (c->fc->nb_streams < 1)
  1065. return 0;
  1066. st = c->fc->streams[c->fc->nb_streams-1];
  1067. sc = st->priv_data;
  1068. get_byte(pb); /* version */
  1069. get_be24(pb); /* flags */
  1070. entries = get_be32(pb);
  1071. dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  1072. if(entries >= UINT_MAX / sizeof(*sc->stts_data))
  1073. return -1;
  1074. sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
  1075. if (!sc->stts_data)
  1076. return AVERROR(ENOMEM);
  1077. sc->stts_count = entries;
  1078. for(i=0; i<entries; i++) {
  1079. int sample_duration;
  1080. int sample_count;
  1081. sample_count=get_be32(pb);
  1082. sample_duration = get_be32(pb);
  1083. sc->stts_data[i].count= sample_count;
  1084. sc->stts_data[i].duration= sample_duration;
  1085. sc->time_rate= av_gcd(sc->time_rate, sample_duration);
  1086. dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  1087. duration+=(int64_t)sample_duration*sample_count;
  1088. total_sample_count+=sample_count;
  1089. }
  1090. st->nb_frames= total_sample_count;
  1091. if(duration)
  1092. st->duration= duration;
  1093. return 0;
  1094. }
  1095. static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1096. {
  1097. AVStream *st;
  1098. MOVStreamContext *sc;
  1099. unsigned int i, entries;
  1100. if (c->fc->nb_streams < 1)
  1101. return 0;
  1102. st = c->fc->streams[c->fc->nb_streams-1];
  1103. sc = st->priv_data;
  1104. get_byte(pb); /* version */
  1105. get_be24(pb); /* flags */
  1106. entries = get_be32(pb);
  1107. dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1108. if(entries >= UINT_MAX / sizeof(*sc->ctts_data))
  1109. return -1;
  1110. sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
  1111. if (!sc->ctts_data)
  1112. return AVERROR(ENOMEM);
  1113. sc->ctts_count = entries;
  1114. for(i=0; i<entries; i++) {
  1115. int count =get_be32(pb);
  1116. int duration =get_be32(pb);
  1117. if (duration < 0) {
  1118. sc->wrong_dts = 1;
  1119. st->codec->has_b_frames = 1;
  1120. }
  1121. sc->ctts_data[i].count = count;
  1122. sc->ctts_data[i].duration= duration;
  1123. sc->time_rate= av_gcd(sc->time_rate, FFABS(duration));
  1124. }
  1125. return 0;
  1126. }
  1127. static void mov_build_index(MOVContext *mov, AVStream *st)
  1128. {
  1129. MOVStreamContext *sc = st->priv_data;
  1130. int64_t current_offset;
  1131. int64_t current_dts = 0;
  1132. unsigned int stts_index = 0;
  1133. unsigned int stsc_index = 0;
  1134. unsigned int stss_index = 0;
  1135. unsigned int i, j;
  1136. /* adjust first dts according to edit list */
  1137. if (sc->time_offset) {
  1138. assert(sc->time_offset % sc->time_rate == 0);
  1139. current_dts = - (sc->time_offset / sc->time_rate);
  1140. }
  1141. /* only use old uncompressed audio chunk demuxing when stts specifies it */
  1142. if (!(st->codec->codec_type == CODEC_TYPE_AUDIO &&
  1143. sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
  1144. unsigned int current_sample = 0;
  1145. unsigned int stts_sample = 0;
  1146. unsigned int keyframe, sample_size;
  1147. unsigned int distance = 0;
  1148. int key_off = sc->keyframes && sc->keyframes[0] == 1;
  1149. st->nb_frames = sc->sample_count;
  1150. for (i = 0; i < sc->chunk_count; i++) {
  1151. current_offset = sc->chunk_offsets[i];
  1152. if (stsc_index + 1 < sc->stsc_count &&
  1153. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1154. stsc_index++;
  1155. for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
  1156. if (current_sample >= sc->sample_count) {
  1157. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  1158. goto out;
  1159. }
  1160. keyframe = !sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index];
  1161. if (keyframe) {
  1162. distance = 0;
  1163. if (stss_index + 1 < sc->keyframe_count)
  1164. stss_index++;
  1165. }
  1166. sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
  1167. if(sc->pseudo_stream_id == -1 ||
  1168. sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
  1169. av_add_index_entry(st, current_offset, current_dts, sample_size, distance,
  1170. keyframe ? AVINDEX_KEYFRAME : 0);
  1171. dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1172. "size %d, distance %d, keyframe %d\n", st->index, current_sample,
  1173. current_offset, current_dts, sample_size, distance, keyframe);
  1174. }
  1175. current_offset += sample_size;
  1176. assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
  1177. current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
  1178. distance++;
  1179. stts_sample++;
  1180. current_sample++;
  1181. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  1182. stts_sample = 0;
  1183. stts_index++;
  1184. }
  1185. }
  1186. }
  1187. } else { /* read whole chunk */
  1188. unsigned int chunk_samples, chunk_size, chunk_duration;
  1189. unsigned int frames = 1;
  1190. for (i = 0; i < sc->chunk_count; i++) {
  1191. current_offset = sc->chunk_offsets[i];
  1192. if (stsc_index + 1 < sc->stsc_count &&
  1193. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1194. stsc_index++;
  1195. chunk_samples = sc->stsc_data[stsc_index].count;
  1196. /* get chunk size, beware of alaw/ulaw/mace */
  1197. if (sc->samples_per_frame > 0 &&
  1198. (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0)) {
  1199. if (sc->samples_per_frame < 160)
  1200. chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
  1201. else {
  1202. chunk_size = sc->bytes_per_frame;
  1203. frames = chunk_samples / sc->samples_per_frame;
  1204. chunk_samples = sc->samples_per_frame;
  1205. }
  1206. } else
  1207. chunk_size = chunk_samples * sc->sample_size;
  1208. for (j = 0; j < frames; j++) {
  1209. av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
  1210. /* get chunk duration */
  1211. chunk_duration = 0;
  1212. while (chunk_samples > 0) {
  1213. if (chunk_samples < sc->stts_data[stts_index].count) {
  1214. chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
  1215. sc->stts_data[stts_index].count -= chunk_samples;
  1216. break;
  1217. } else {
  1218. chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
  1219. chunk_samples -= sc->stts_data[stts_index].count;
  1220. if (stts_index + 1 < sc->stts_count)
  1221. stts_index++;
  1222. }
  1223. }
  1224. current_offset += sc->bytes_per_frame;
  1225. dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
  1226. "size %d, duration %d\n", st->index, i, current_offset, current_dts,
  1227. chunk_size, chunk_duration);
  1228. assert(chunk_duration % sc->time_rate == 0);
  1229. current_dts += chunk_duration / sc->time_rate;
  1230. }
  1231. }
  1232. }
  1233. out:
  1234. /* adjust sample count to avindex entries */
  1235. sc->sample_count = st->nb_index_entries;
  1236. }
  1237. static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1238. {
  1239. AVStream *st;
  1240. MOVStreamContext *sc;
  1241. int ret;
  1242. st = av_new_stream(c->fc, c->fc->nb_streams);
  1243. if (!st) return AVERROR(ENOMEM);
  1244. sc = av_mallocz(sizeof(MOVStreamContext));
  1245. if (!sc) return AVERROR(ENOMEM);
  1246. st->priv_data = sc;
  1247. st->codec->codec_type = CODEC_TYPE_DATA;
  1248. sc->ffindex = st->index;
  1249. if ((ret = mov_read_default(c, pb, atom)) < 0)
  1250. return ret;
  1251. /* sanity checks */
  1252. if(sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
  1253. (!sc->sample_size && !sc->sample_count))){
  1254. av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
  1255. st->index);
  1256. sc->sample_count = 0; //ignore track
  1257. return 0;
  1258. }
  1259. if(!sc->time_rate)
  1260. sc->time_rate=1;
  1261. if(!sc->time_scale)
  1262. sc->time_scale= c->time_scale;
  1263. av_set_pts_info(st, 64, sc->time_rate, sc->time_scale);
  1264. if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
  1265. !st->codec->frame_size && sc->stts_count == 1) {
  1266. st->codec->frame_size = av_rescale(sc->stts_data[0].duration,
  1267. st->codec->sample_rate, sc->time_scale);
  1268. dprintf(c->fc, "frame size %d\n", st->codec->frame_size);
  1269. }
  1270. if(st->duration != AV_NOPTS_VALUE){
  1271. assert(st->duration % sc->time_rate == 0);
  1272. st->duration /= sc->time_rate;
  1273. }
  1274. mov_build_index(c, st);
  1275. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  1276. if (url_fopen(&sc->pb, sc->drefs[sc->dref_id-1].path, URL_RDONLY) < 0)
  1277. av_log(c->fc, AV_LOG_ERROR, "stream %d, error opening file %s: %s\n",
  1278. st->index, sc->drefs[sc->dref_id-1].path, strerror(errno));
  1279. } else
  1280. sc->pb = c->fc->pb;
  1281. switch (st->codec->codec_id) {
  1282. #if CONFIG_H261_DECODER
  1283. case CODEC_ID_H261:
  1284. #endif
  1285. #if CONFIG_H263_DECODER
  1286. case CODEC_ID_H263:
  1287. #endif
  1288. #if CONFIG_MPEG4_DECODER
  1289. case CODEC_ID_MPEG4:
  1290. #endif
  1291. st->codec->width= 0; /* let decoder init width/height */
  1292. st->codec->height= 0;
  1293. break;
  1294. }
  1295. /* Do not need those anymore. */
  1296. av_freep(&sc->chunk_offsets);
  1297. av_freep(&sc->stsc_data);
  1298. av_freep(&sc->sample_sizes);
  1299. av_freep(&sc->keyframes);
  1300. av_freep(&sc->stts_data);
  1301. return 0;
  1302. }
  1303. static int mov_read_ilst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1304. {
  1305. int ret;
  1306. c->itunes_metadata = 1;
  1307. ret = mov_read_default(c, pb, atom);
  1308. c->itunes_metadata = 0;
  1309. return ret;
  1310. }
  1311. static int mov_read_meta(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1312. {
  1313. url_fskip(pb, 4); // version + flags
  1314. atom.size -= 4;
  1315. return mov_read_default(c, pb, atom);
  1316. }
  1317. static int mov_read_trkn(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1318. {
  1319. char track[16];
  1320. get_be32(pb); // type
  1321. get_be32(pb); // unknown
  1322. snprintf(track, sizeof(track), "%d", get_be32(pb));
  1323. av_metadata_set(&c->fc->metadata, "track", track);
  1324. dprintf(c->fc, "%.4s %s\n", (char*)&atom.type, track);
  1325. return 0;
  1326. }
  1327. static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1328. {
  1329. char str[1024], key2[16], language[4] = {0};
  1330. const char *key = NULL;
  1331. uint16_t str_size;
  1332. if (c->itunes_metadata) {
  1333. int data_size = get_be32(pb);
  1334. int tag = get_le32(pb);
  1335. if (tag == MKTAG('d','a','t','a')) {
  1336. get_be32(pb); // type
  1337. get_be32(pb); // unknown
  1338. str_size = data_size - 16;
  1339. atom.size -= 16;
  1340. } else return 0;
  1341. } else {
  1342. str_size = get_be16(pb); // string length
  1343. ff_mov_lang_to_iso639(get_be16(pb), language);
  1344. atom.size -= 4;
  1345. }
  1346. switch (atom.type) {
  1347. case MKTAG(0xa9,'n','a','m'): key = "title"; break;
  1348. case MKTAG(0xa9,'a','u','t'):
  1349. case MKTAG(0xa9,'A','R','T'):
  1350. case MKTAG(0xa9,'w','r','t'): key = "author"; break;
  1351. case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
  1352. case MKTAG(0xa9,'c','m','t'):
  1353. case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
  1354. case MKTAG(0xa9,'a','l','b'): key = "album"; break;
  1355. case MKTAG(0xa9,'d','a','y'): key = "year"; break;
  1356. case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
  1357. case MKTAG(0xa9,'t','o','o'):
  1358. case MKTAG(0xa9,'e','n','c'): key = "muxer"; break;
  1359. }
  1360. if (!key)
  1361. return 0;
  1362. if (atom.size < 0)
  1363. return -1;
  1364. str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
  1365. get_buffer(pb, str, str_size);
  1366. str[str_size] = 0;
  1367. av_metadata_set(&c->fc->metadata, key, str);
  1368. if (*language && strcmp(language, "und")) {
  1369. snprintf(key2, sizeof(key2), "%s-%s", key, language);
  1370. av_metadata_set(&c->fc->metadata, key2, str);
  1371. }
  1372. dprintf(c->fc, "%.4s %s %d %lld\n", (char*)&atom.type, str, str_size, atom.size);
  1373. return 0;
  1374. }
  1375. static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1376. {
  1377. int i;
  1378. int width;
  1379. int height;
  1380. int64_t disp_transform[2];
  1381. int display_matrix[3][2];
  1382. AVStream *st;
  1383. MOVStreamContext *sc;
  1384. int version;
  1385. if (c->fc->nb_streams < 1)
  1386. return 0;
  1387. st = c->fc->streams[c->fc->nb_streams-1];
  1388. sc = st->priv_data;
  1389. version = get_byte(pb);
  1390. get_be24(pb); /* flags */
  1391. /*
  1392. MOV_TRACK_ENABLED 0x0001
  1393. MOV_TRACK_IN_MOVIE 0x0002
  1394. MOV_TRACK_IN_PREVIEW 0x0004
  1395. MOV_TRACK_IN_POSTER 0x0008
  1396. */
  1397. if (version == 1) {
  1398. get_be64(pb);
  1399. get_be64(pb);
  1400. } else {
  1401. get_be32(pb); /* creation time */
  1402. get_be32(pb); /* modification time */
  1403. }
  1404. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  1405. get_be32(pb); /* reserved */
  1406. /* highlevel (considering edits) duration in movie timebase */
  1407. (version == 1) ? get_be64(pb) : get_be32(pb);
  1408. get_be32(pb); /* reserved */
  1409. get_be32(pb); /* reserved */
  1410. get_be16(pb); /* layer */
  1411. get_be16(pb); /* alternate group */
  1412. get_be16(pb); /* volume */
  1413. get_be16(pb); /* reserved */
  1414. //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
  1415. // they're kept in fixed point format through all calculations
  1416. // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
  1417. for (i = 0; i < 3; i++) {
  1418. display_matrix[i][0] = get_be32(pb); // 16.16 fixed point
  1419. display_matrix[i][1] = get_be32(pb); // 16.16 fixed point
  1420. get_be32(pb); // 2.30 fixed point (not used)
  1421. }
  1422. width = get_be32(pb); // 16.16 fixed point track width
  1423. height = get_be32(pb); // 16.16 fixed point track height
  1424. sc->width = width >> 16;
  1425. sc->height = height >> 16;
  1426. //transform the display width/height according to the matrix
  1427. // skip this if the display matrix is the default identity matrix
  1428. // to keep the same scale, use [width height 1<<16]
  1429. if (width && height &&
  1430. (display_matrix[0][0] != 65536 || display_matrix[0][1] ||
  1431. display_matrix[1][0] || display_matrix[1][1] != 65536 ||
  1432. display_matrix[2][0] || display_matrix[2][1])) {
  1433. for (i = 0; i < 2; i++)
  1434. disp_transform[i] =
  1435. (int64_t) width * display_matrix[0][i] +
  1436. (int64_t) height * display_matrix[1][i] +
  1437. ((int64_t) display_matrix[2][i] << 16);
  1438. //sample aspect ratio is new width/height divided by old width/height
  1439. st->sample_aspect_ratio = av_d2q(
  1440. ((double) disp_transform[0] * height) /
  1441. ((double) disp_transform[1] * width), INT_MAX);
  1442. }
  1443. return 0;
  1444. }
  1445. static int mov_read_tfhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1446. {
  1447. MOVFragment *frag = &c->fragment;
  1448. MOVTrackExt *trex = NULL;
  1449. int flags, track_id, i;
  1450. get_byte(pb); /* version */
  1451. flags = get_be24(pb);
  1452. track_id = get_be32(pb);
  1453. if (!track_id || track_id > c->fc->nb_streams)
  1454. return -1;
  1455. frag->track_id = track_id;
  1456. for (i = 0; i < c->trex_count; i++)
  1457. if (c->trex_data[i].track_id == frag->track_id) {
  1458. trex = &c->trex_data[i];
  1459. break;
  1460. }
  1461. if (!trex) {
  1462. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
  1463. return -1;
  1464. }
  1465. if (flags & 0x01) frag->base_data_offset = get_be64(pb);
  1466. else frag->base_data_offset = frag->moof_offset;
  1467. if (flags & 0x02) frag->stsd_id = get_be32(pb);
  1468. else frag->stsd_id = trex->stsd_id;
  1469. frag->duration = flags & 0x08 ? get_be32(pb) : trex->duration;
  1470. frag->size = flags & 0x10 ? get_be32(pb) : trex->size;
  1471. frag->flags = flags & 0x20 ? get_be32(pb) : trex->flags;
  1472. dprintf(c->fc, "frag flags 0x%x\n", frag->flags);
  1473. return 0;
  1474. }
  1475. static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1476. {
  1477. MOVTrackExt *trex;
  1478. if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
  1479. return -1;
  1480. trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
  1481. if (!trex)
  1482. return AVERROR(ENOMEM);
  1483. c->trex_data = trex;
  1484. trex = &c->trex_data[c->trex_count++];
  1485. get_byte(pb); /* version */
  1486. get_be24(pb); /* flags */
  1487. trex->track_id = get_be32(pb);
  1488. trex->stsd_id = get_be32(pb);
  1489. trex->duration = get_be32(pb);
  1490. trex->size = get_be32(pb);
  1491. trex->flags = get_be32(pb);
  1492. return 0;
  1493. }
  1494. static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1495. {
  1496. MOVFragment *frag = &c->fragment;
  1497. AVStream *st;
  1498. MOVStreamContext *sc;
  1499. uint64_t offset;
  1500. int64_t dts;
  1501. int data_offset = 0;
  1502. unsigned entries, first_sample_flags = frag->flags;
  1503. int flags, distance, i;
  1504. if (!frag->track_id || frag->track_id > c->fc->nb_streams)
  1505. return -1;
  1506. st = c->fc->streams[frag->track_id-1];
  1507. sc = st->priv_data;
  1508. if (sc->pseudo_stream_id+1 != frag->stsd_id)
  1509. return 0;
  1510. get_byte(pb); /* version */
  1511. flags = get_be24(pb);
  1512. entries = get_be32(pb);
  1513. dprintf(c->fc, "flags 0x%x entries %d\n", flags, entries);
  1514. if (flags & 0x001) data_offset = get_be32(pb);
  1515. if (flags & 0x004) first_sample_flags = get_be32(pb);
  1516. if (flags & 0x800) {
  1517. MOVStts *ctts_data;
  1518. if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
  1519. return -1;
  1520. ctts_data = av_realloc(sc->ctts_data,
  1521. (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
  1522. if (!ctts_data)
  1523. return AVERROR(ENOMEM);
  1524. sc->ctts_data = ctts_data;
  1525. }
  1526. dts = st->duration;
  1527. offset = frag->base_data_offset + data_offset;
  1528. distance = 0;
  1529. dprintf(c->fc, "first sample flags 0x%x\n", first_sample_flags);
  1530. for (i = 0; i < entries; i++) {
  1531. unsigned sample_size = frag->size;
  1532. int sample_flags = i ? frag->flags : first_sample_flags;
  1533. unsigned sample_duration = frag->duration;
  1534. int keyframe;
  1535. if (flags & 0x100) sample_duration = get_be32(pb);
  1536. if (flags & 0x200) sample_size = get_be32(pb);
  1537. if (flags & 0x400) sample_flags = get_be32(pb);
  1538. if (flags & 0x800) {
  1539. sc->ctts_data[sc->ctts_count].count = 1;
  1540. sc->ctts_data[sc->ctts_count].duration = get_be32(pb);
  1541. sc->ctts_count++;
  1542. }
  1543. if ((keyframe = st->codec->codec_type == CODEC_TYPE_AUDIO ||
  1544. (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))
  1545. distance = 0;
  1546. av_add_index_entry(st, offset, dts, sample_size, distance,
  1547. keyframe ? AVINDEX_KEYFRAME : 0);
  1548. dprintf(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1549. "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
  1550. offset, dts, sample_size, distance, keyframe);
  1551. distance++;
  1552. assert(sample_duration % sc->time_rate == 0);
  1553. dts += sample_duration / sc->time_rate;
  1554. offset += sample_size;
  1555. }
  1556. frag->moof_offset = offset;
  1557. sc->sample_count = st->nb_index_entries;
  1558. st->duration = dts;
  1559. return 0;
  1560. }
  1561. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1562. /* like the files created with Adobe Premiere 5.0, for samples see */
  1563. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1564. static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1565. {
  1566. int err;
  1567. if (atom.size < 8)
  1568. return 0; /* continue */
  1569. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1570. url_fskip(pb, atom.size - 4);
  1571. return 0;
  1572. }
  1573. atom.type = get_le32(pb);
  1574. atom.offset += 8;
  1575. atom.size -= 8;
  1576. if (atom.type != MKTAG('m','d','a','t')) {
  1577. url_fskip(pb, atom.size);
  1578. return 0;
  1579. }
  1580. err = mov_read_mdat(c, pb, atom);
  1581. return err;
  1582. }
  1583. static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1584. {
  1585. #if CONFIG_ZLIB
  1586. ByteIOContext ctx;
  1587. uint8_t *cmov_data;
  1588. uint8_t *moov_data; /* uncompressed data */
  1589. long cmov_len, moov_len;
  1590. int ret = -1;
  1591. get_be32(pb); /* dcom atom */
  1592. if (get_le32(pb) != MKTAG('d','c','o','m'))
  1593. return -1;
  1594. if (get_le32(pb) != MKTAG('z','l','i','b')) {
  1595. av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !");
  1596. return -1;
  1597. }
  1598. get_be32(pb); /* cmvd atom */
  1599. if (get_le32(pb) != MKTAG('c','m','v','d'))
  1600. return -1;
  1601. moov_len = get_be32(pb); /* uncompressed size */
  1602. cmov_len = atom.size - 6 * 4;
  1603. cmov_data = av_malloc(cmov_len);
  1604. if (!cmov_data)
  1605. return AVERROR(ENOMEM);
  1606. moov_data = av_malloc(moov_len);
  1607. if (!moov_data) {
  1608. av_free(cmov_data);
  1609. return AVERROR(ENOMEM);
  1610. }
  1611. get_buffer(pb, cmov_data, cmov_len);
  1612. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1613. goto free_and_return;
  1614. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  1615. goto free_and_return;
  1616. atom.type = MKTAG('m','o','o','v');
  1617. atom.offset = 0;
  1618. atom.size = moov_len;
  1619. #ifdef DEBUG
  1620. // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
  1621. #endif
  1622. ret = mov_read_default(c, &ctx, atom);
  1623. free_and_return:
  1624. av_free(moov_data);
  1625. av_free(cmov_data);
  1626. return ret;
  1627. #else
  1628. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  1629. return -1;
  1630. #endif
  1631. }
  1632. /* edit list atom */
  1633. static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1634. {
  1635. MOVStreamContext *sc;
  1636. int i, edit_count;
  1637. if (c->fc->nb_streams < 1)
  1638. return 0;
  1639. sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  1640. get_byte(pb); /* version */
  1641. get_be24(pb); /* flags */
  1642. edit_count = get_be32(pb); /* entries */
  1643. for(i=0; i<edit_count; i++){
  1644. int time;
  1645. get_be32(pb); /* Track duration */
  1646. time = get_be32(pb); /* Media time */
  1647. get_be32(pb); /* Media rate */
  1648. if (i == 0 && time != -1) {
  1649. sc->time_offset = time;
  1650. sc->time_rate = av_gcd(sc->time_rate, time);
  1651. }
  1652. }
  1653. if(edit_count > 1)
  1654. av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
  1655. "a/v desync might occur, patch welcome\n");
  1656. dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
  1657. return 0;
  1658. }
  1659. static const MOVParseTableEntry mov_default_parse_table[] = {
  1660. { MKTAG('a','v','s','s'), mov_read_extradata },
  1661. { MKTAG('c','o','6','4'), mov_read_stco },
  1662. { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
  1663. { MKTAG('d','i','n','f'), mov_read_default },
  1664. { MKTAG('d','r','e','f'), mov_read_dref },
  1665. { MKTAG('e','d','t','s'), mov_read_default },
  1666. { MKTAG('e','l','s','t'), mov_read_elst },
  1667. { MKTAG('e','n','d','a'), mov_read_enda },
  1668. { MKTAG('f','i','e','l'), mov_read_extradata },
  1669. { MKTAG('f','t','y','p'), mov_read_ftyp },
  1670. { MKTAG('g','l','b','l'), mov_read_glbl },
  1671. { MKTAG('h','d','l','r'), mov_read_hdlr },
  1672. { MKTAG('i','l','s','t'), mov_read_ilst },
  1673. { MKTAG('j','p','2','h'), mov_read_extradata },
  1674. { MKTAG('m','d','a','t'), mov_read_mdat },
  1675. { MKTAG('m','d','h','d'), mov_read_mdhd },
  1676. { MKTAG('m','d','i','a'), mov_read_default },
  1677. { MKTAG('m','e','t','a'), mov_read_meta },
  1678. { MKTAG('m','i','n','f'), mov_read_default },
  1679. { MKTAG('m','o','o','f'), mov_read_moof },
  1680. { MKTAG('m','o','o','v'), mov_read_moov },
  1681. { MKTAG('m','v','e','x'), mov_read_default },
  1682. { MKTAG('m','v','h','d'), mov_read_mvhd },
  1683. { MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
  1684. { MKTAG('a','l','a','c'), mov_read_extradata }, /* alac specific atom */
  1685. { MKTAG('a','v','c','C'), mov_read_glbl },
  1686. { MKTAG('p','a','s','p'), mov_read_pasp },
  1687. { MKTAG('s','t','b','l'), mov_read_default },
  1688. { MKTAG('s','t','c','o'), mov_read_stco },
  1689. { MKTAG('s','t','s','c'), mov_read_stsc },
  1690. { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
  1691. { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
  1692. { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
  1693. { MKTAG('s','t','t','s'), mov_read_stts },
  1694. { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
  1695. { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
  1696. { MKTAG('t','r','a','k'), mov_read_trak },
  1697. { MKTAG('t','r','a','f'), mov_read_default },
  1698. { MKTAG('t','r','e','x'), mov_read_trex },
  1699. { MKTAG('t','r','k','n'), mov_read_trkn },
  1700. { MKTAG('t','r','u','n'), mov_read_trun },
  1701. { MKTAG('u','d','t','a'), mov_read_default },
  1702. { MKTAG('w','a','v','e'), mov_read_wave },
  1703. { MKTAG('e','s','d','s'), mov_read_esds },
  1704. { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
  1705. { MKTAG('c','m','o','v'), mov_read_cmov },
  1706. { MKTAG(0xa9,'n','a','m'), mov_read_udta_string },
  1707. { MKTAG(0xa9,'w','r','t'), mov_read_udta_string },
  1708. { MKTAG(0xa9,'c','p','y'), mov_read_udta_string },
  1709. { MKTAG(0xa9,'i','n','f'), mov_read_udta_string },
  1710. { MKTAG(0xa9,'i','n','f'), mov_read_udta_string },
  1711. { MKTAG(0xa9,'A','R','T'), mov_read_udta_string },
  1712. { MKTAG(0xa9,'a','l','b'), mov_read_udta_string },
  1713. { MKTAG(0xa9,'c','m','t'), mov_read_udta_string },
  1714. { MKTAG(0xa9,'a','u','t'), mov_read_udta_string },
  1715. { MKTAG(0xa9,'d','a','y'), mov_read_udta_string },
  1716. { MKTAG(0xa9,'g','e','n'), mov_read_udta_string },
  1717. { MKTAG(0xa9,'e','n','c'), mov_read_udta_string },
  1718. { MKTAG(0xa9,'t','o','o'), mov_read_udta_string },
  1719. { 0, NULL }
  1720. };
  1721. static int mov_probe(AVProbeData *p)
  1722. {
  1723. unsigned int offset;
  1724. uint32_t tag;
  1725. int score = 0;
  1726. /* check file header */
  1727. offset = 0;
  1728. for(;;) {
  1729. /* ignore invalid offset */
  1730. if ((offset + 8) > (unsigned int)p->buf_size)
  1731. return score;
  1732. tag = AV_RL32(p->buf + offset + 4);
  1733. switch(tag) {
  1734. /* check for obvious tags */
  1735. case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
  1736. case MKTAG('m','o','o','v'):
  1737. case MKTAG('m','d','a','t'):
  1738. case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
  1739. case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
  1740. case MKTAG('f','t','y','p'):
  1741. return AVPROBE_SCORE_MAX;
  1742. /* those are more common words, so rate then a bit less */
  1743. case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
  1744. case MKTAG('w','i','d','e'):
  1745. case MKTAG('f','r','e','e'):
  1746. case MKTAG('j','u','n','k'):
  1747. case MKTAG('p','i','c','t'):
  1748. return AVPROBE_SCORE_MAX - 5;
  1749. case MKTAG(0x82,0x82,0x7f,0x7d):
  1750. case MKTAG('s','k','i','p'):
  1751. case MKTAG('u','u','i','d'):
  1752. case MKTAG('p','r','f','l'):
  1753. offset = AV_RB32(p->buf+offset) + offset;
  1754. /* if we only find those cause probedata is too small at least rate them */
  1755. score = AVPROBE_SCORE_MAX - 50;
  1756. break;
  1757. default:
  1758. /* unrecognized tag */
  1759. return score;
  1760. }
  1761. }
  1762. return score;
  1763. }
  1764. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1765. {
  1766. MOVContext *mov = s->priv_data;
  1767. ByteIOContext *pb = s->pb;
  1768. int err;
  1769. MOVAtom atom = { 0, 0, 0 };
  1770. mov->fc = s;
  1771. /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  1772. if(!url_is_streamed(pb))
  1773. atom.size = url_fsize(pb);
  1774. else
  1775. atom.size = INT64_MAX;
  1776. /* check MOV header */
  1777. if ((err = mov_read_default(mov, pb, atom)) < 0) {
  1778. av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
  1779. return err;
  1780. }
  1781. if (!mov->found_moov) {
  1782. av_log(s, AV_LOG_ERROR, "moov atom not found\n");
  1783. return -1;
  1784. }
  1785. dprintf(mov->fc, "on_parse_exit_offset=%lld\n", url_ftell(pb));
  1786. return 0;
  1787. }
  1788. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  1789. {
  1790. MOVContext *mov = s->priv_data;
  1791. MOVStreamContext *sc = 0;
  1792. AVIndexEntry *sample = 0;
  1793. int64_t best_dts = INT64_MAX;
  1794. int i, ret;
  1795. retry:
  1796. for (i = 0; i < s->nb_streams; i++) {
  1797. AVStream *st = s->streams[i];
  1798. MOVStreamContext *msc = st->priv_data;
  1799. if (st->discard != AVDISCARD_ALL && msc->pb && msc->current_sample < msc->sample_count) {
  1800. AVIndexEntry *current_sample = &st->index_entries[msc->current_sample];
  1801. int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate,
  1802. AV_TIME_BASE, msc->time_scale);
  1803. dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  1804. if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) ||
  1805. (!url_is_streamed(s->pb) &&
  1806. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
  1807. ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  1808. (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
  1809. sample = current_sample;
  1810. best_dts = dts;
  1811. sc = msc;
  1812. }
  1813. }
  1814. }
  1815. if (!sample) {
  1816. mov->found_mdat = 0;
  1817. if (!url_is_streamed(s->pb) ||
  1818. mov_read_default(mov, s->pb, (MOVAtom){ 0, 0, INT64_MAX }) < 0 ||
  1819. url_feof(s->pb))
  1820. return -1;
  1821. dprintf(s, "read fragments, offset 0x%llx\n", url_ftell(s->pb));
  1822. goto retry;
  1823. }
  1824. /* must be done just before reading, to avoid infinite loop on sample */
  1825. sc->current_sample++;
  1826. if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  1827. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  1828. sc->ffindex, sample->pos);
  1829. return -1;
  1830. }
  1831. ret = av_get_packet(sc->pb, pkt, sample->size);
  1832. if (ret < 0)
  1833. return ret;
  1834. #if CONFIG_DV_DEMUXER
  1835. if (mov->dv_demux && sc->dv_audio_container) {
  1836. dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
  1837. av_free(pkt->data);
  1838. pkt->size = 0;
  1839. if (dv_get_packet(mov->dv_demux, pkt) < 0)
  1840. return -1;
  1841. }
  1842. #endif
  1843. pkt->stream_index = sc->ffindex;
  1844. pkt->dts = sample->timestamp;
  1845. if (sc->ctts_data) {
  1846. assert(sc->ctts_data[sc->ctts_index].duration % sc->time_rate == 0);
  1847. pkt->pts = pkt->dts + sc->ctts_data[sc->ctts_index].duration / sc->time_rate;
  1848. /* update ctts context */
  1849. sc->ctts_sample++;
  1850. if (sc->ctts_index < sc->ctts_count &&
  1851. sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
  1852. sc->ctts_index++;
  1853. sc->ctts_sample = 0;
  1854. }
  1855. if (sc->wrong_dts)
  1856. pkt->dts = AV_NOPTS_VALUE;
  1857. } else {
  1858. AVStream *st = s->streams[sc->ffindex];
  1859. int64_t next_dts = (sc->current_sample < sc->sample_count) ?
  1860. st->index_entries[sc->current_sample].timestamp : st->duration;
  1861. pkt->duration = next_dts - pkt->dts;
  1862. pkt->pts = pkt->dts;
  1863. }
  1864. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
  1865. pkt->pos = sample->pos;
  1866. dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
  1867. pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
  1868. return 0;
  1869. }
  1870. static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
  1871. {
  1872. MOVStreamContext *sc = st->priv_data;
  1873. int sample, time_sample;
  1874. int i;
  1875. sample = av_index_search_timestamp(st, timestamp, flags);
  1876. dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  1877. if (sample < 0) /* not sure what to do */
  1878. return -1;
  1879. sc->current_sample = sample;
  1880. dprintf(st->codec, "stream %d, found sample %d\n", st->index, sc->current_sample);
  1881. /* adjust ctts index */
  1882. if (sc->ctts_data) {
  1883. time_sample = 0;
  1884. for (i = 0; i < sc->ctts_count; i++) {
  1885. int next = time_sample + sc->ctts_data[i].count;
  1886. if (next > sc->current_sample) {
  1887. sc->ctts_index = i;
  1888. sc->ctts_sample = sc->current_sample - time_sample;
  1889. break;
  1890. }
  1891. time_sample = next;
  1892. }
  1893. }
  1894. return sample;
  1895. }
  1896. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  1897. {
  1898. AVStream *st;
  1899. int64_t seek_timestamp, timestamp;
  1900. int sample;
  1901. int i;
  1902. if (stream_index >= s->nb_streams)
  1903. return -1;
  1904. if (sample_time < 0)
  1905. sample_time = 0;
  1906. st = s->streams[stream_index];
  1907. sample = mov_seek_stream(st, sample_time, flags);
  1908. if (sample < 0)
  1909. return -1;
  1910. /* adjust seek timestamp to found sample timestamp */
  1911. seek_timestamp = st->index_entries[sample].timestamp;
  1912. for (i = 0; i < s->nb_streams; i++) {
  1913. st = s->streams[i];
  1914. if (stream_index == i || st->discard == AVDISCARD_ALL)
  1915. continue;
  1916. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  1917. mov_seek_stream(st, timestamp, flags);
  1918. }
  1919. return 0;
  1920. }
  1921. static int mov_read_close(AVFormatContext *s)
  1922. {
  1923. int i, j;
  1924. MOVContext *mov = s->priv_data;
  1925. for(i=0; i<s->nb_streams; i++) {
  1926. MOVStreamContext *sc = s->streams[i]->priv_data;
  1927. av_freep(&sc->ctts_data);
  1928. for (j=0; j<sc->drefs_count; j++)
  1929. av_freep(&sc->drefs[j].path);
  1930. av_freep(&sc->drefs);
  1931. if (sc->pb && sc->pb != s->pb)
  1932. url_fclose(sc->pb);
  1933. }
  1934. if(mov->dv_demux){
  1935. for(i=0; i<mov->dv_fctx->nb_streams; i++){
  1936. av_freep(&mov->dv_fctx->streams[i]->codec);
  1937. av_freep(&mov->dv_fctx->streams[i]);
  1938. }
  1939. av_freep(&mov->dv_fctx);
  1940. av_freep(&mov->dv_demux);
  1941. }
  1942. av_freep(&mov->trex_data);
  1943. return 0;
  1944. }
  1945. AVInputFormat mov_demuxer = {
  1946. "mov,mp4,m4a,3gp,3g2,mj2",
  1947. NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),
  1948. sizeof(MOVContext),
  1949. mov_probe,
  1950. mov_read_header,
  1951. mov_read_packet,
  1952. mov_read_close,
  1953. mov_read_seek,
  1954. };