You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2153 lines
75KB

  1. /*
  2. * MOV decoder.
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include <limits.h>
  20. //#define DEBUG
  21. #include "avformat.h"
  22. #include "avi.h"
  23. #include "mov.h"
  24. #ifdef CONFIG_ZLIB
  25. #include <zlib.h>
  26. #endif
  27. /*
  28. * First version by Francois Revol revol@free.fr
  29. * Seek function by Gael Chardon gael.dev@4now.net
  30. *
  31. * Features and limitations:
  32. * - reads most of the QT files I have (at least the structure),
  33. * the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement.
  34. * FIXED, Francois Revol, 07/17/2002
  35. * - ffmpeg has nearly none of the usual QuickTime codecs,
  36. * although I succesfully dumped raw and mp3 audio tracks off .mov files.
  37. * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
  38. * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes
  39. * (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at
  40. * http://mpeg.telecomitalialab.com/faq.htm
  41. * - the code is quite ugly... maybe I won't do it recursive next time :-)
  42. * - seek is not supported with files that contain edit list
  43. *
  44. * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
  45. * when coding this :) (it's a writer anyway)
  46. *
  47. * Reference documents:
  48. * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
  49. * Apple:
  50. * http://developer.apple.com/documentation/QuickTime/QTFF/
  51. * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
  52. * QuickTime is a trademark of Apple (AFAIK :))
  53. */
  54. #include "qtpalette.h"
  55. #undef NDEBUG
  56. #include <assert.h>
  57. /* Allows seeking */
  58. #define MOV_SEEK
  59. /* http://gpac.sourceforge.net/tutorial/mediatypes.htm */
  60. const CodecTag ff_mov_obj_type[] = {
  61. { CODEC_ID_MPEG4 , 32 },
  62. { CODEC_ID_H264 , 33 },
  63. { CODEC_ID_AAC , 64 },
  64. { CODEC_ID_MPEG2VIDEO, 96 }, /* MPEG2 Simple */
  65. { CODEC_ID_MPEG2VIDEO, 97 }, /* MPEG2 Main */
  66. { CODEC_ID_MPEG2VIDEO, 98 }, /* MPEG2 SNR */
  67. { CODEC_ID_MPEG2VIDEO, 99 }, /* MPEG2 Spatial */
  68. { CODEC_ID_MPEG2VIDEO, 100 }, /* MPEG2 High */
  69. { CODEC_ID_MPEG2VIDEO, 101 }, /* MPEG2 422 */
  70. { CODEC_ID_AAC , 102 }, /* MPEG2 AAC Main */
  71. { CODEC_ID_AAC , 103 }, /* MPEG2 AAC Low */
  72. { CODEC_ID_AAC , 104 }, /* MPEG2 AAC SSR */
  73. { CODEC_ID_MP3 , 105 },
  74. { CODEC_ID_MPEG1VIDEO, 106 },
  75. { CODEC_ID_MP2 , 107 },
  76. { CODEC_ID_MJPEG , 108 },
  77. { CODEC_ID_PCM_S16LE , 224 },
  78. { CODEC_ID_VORBIS , 221 },
  79. { CODEC_ID_AC3 , 226 },
  80. { CODEC_ID_PCM_ALAW , 227 },
  81. { CODEC_ID_PCM_MULAW , 228 },
  82. { CODEC_ID_PCM_S16BE , 230 },
  83. { CODEC_ID_H263 , 242 },
  84. { CODEC_ID_H261 , 243 },
  85. { 0, 0 },
  86. };
  87. static const CodecTag mov_video_tags[] = {
  88. /* { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
  89. /* { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, *//* Uncompressed RGB */
  90. /* { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, *//* Uncompressed YUV422 */
  91. /* { CODEC_ID_RAWVIDEO, MKTAG('A', 'V', 'U', 'I') }, *//* YUV with alpha-channel (AVID Uncompressed) */
  92. /* Graphics */
  93. /* Animation */
  94. /* Apple video */
  95. /* Kodak Photo CD */
  96. { CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
  97. { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
  98. { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
  99. { CODEC_ID_MJPEGB, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
  100. { CODEC_ID_MJPEG, MKTAG('A', 'V', 'D', 'J') }, /* MJPEG with alpha-channel (AVID JFIF meridien compressed) */
  101. /* { CODEC_ID_MJPEG, MKTAG('A', 'V', 'R', 'n') }, *//* MJPEG with alpha-channel (AVID ABVB/Truevision NuVista) */
  102. /* { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
  103. /* Sorenson video */
  104. { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') }, /* Sorenson Video v1 */
  105. { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, /* Sorenson Video v1 */
  106. { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', 'i') }, /* Sorenson Video v1 (from QT specs)*/
  107. { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') }, /* Sorenson Video v3 */
  108. { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
  109. { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
  110. { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D') },
  111. { CODEC_ID_MPEG4, MKTAG('3', 'I', 'V', '2') }, /* experimental: 3IVX files before ivx D4 4.5.1 */
  112. /* { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
  113. { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */
  114. { CODEC_ID_H263, MKTAG('s', '2', '6', '3') }, /* H263 ?? works */
  115. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
  116. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
  117. /* { CODEC_ID_DVVIDEO, MKTAG('A', 'V', 'd', 'v') }, *//* AVID dv */
  118. { CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') }, /* On2 VP3 */
  119. { CODEC_ID_RPZA, MKTAG('r', 'p', 'z', 'a') }, /* Apple Video (RPZA) */
  120. { CODEC_ID_CINEPAK, MKTAG('c', 'v', 'i', 'd') }, /* Cinepak */
  121. { CODEC_ID_8BPS, MKTAG('8', 'B', 'P', 'S') }, /* Planar RGB (8BPS) */
  122. { CODEC_ID_SMC, MKTAG('s', 'm', 'c', ' ') }, /* Apple Graphics (SMC) */
  123. { CODEC_ID_QTRLE, MKTAG('r', 'l', 'e', ' ') }, /* Apple Animation (RLE) */
  124. { CODEC_ID_QDRAW, MKTAG('q', 'd', 'r', 'w') }, /* QuickDraw */
  125. { CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') }, /* AVC-1/H.264 */
  126. { CODEC_ID_MPEG2VIDEO, MKTAG('h', 'd', 'v', '2') }, /* MPEG2 produced by Sony HD camera */
  127. { CODEC_ID_MPEG2VIDEO, MKTAG('h', 'd', 'v', '3') }, /* HDV produced by FCP */
  128. { CODEC_ID_MPEG2VIDEO, MKTAG('m', 'x', '5', 'n') }, /* MPEG2 IMX NTSC 525/60 50mb/s produced by FCP */
  129. { CODEC_ID_MPEG2VIDEO, MKTAG('m', 'x', '5', 'p') }, /* MPEG2 IMX PAL 625/50 50mb/s produced by FCP */
  130. { CODEC_ID_MPEG2VIDEO, MKTAG('m', 'x', '3', 'n') }, /* MPEG2 IMX NTSC 525/60 30mb/s produced by FCP */
  131. { CODEC_ID_MPEG2VIDEO, MKTAG('m', 'x', '3', 'p') }, /* MPEG2 IMX PAL 625/50 30mb/s produced by FCP */
  132. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'p', 'p') }, /* DVCPRO PAL produced by FCP */
  133. //{ CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', '5') }, /* DVCPRO HD 50i produced by FCP */
  134. //{ CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', '6') }, /* DVCPRO HD 60i produced by FCP */
  135. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', 'p') }, /* DVCPRO50 PAL produced by FCP */
  136. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', 'n') }, /* DVCPRO50 NTSC produced by FCP */
  137. { CODEC_ID_DVVIDEO, MKTAG('A', 'V', 'd', 'v') }, /* AVID DV */
  138. //{ CODEC_ID_JPEG2000, MKTAG('m', 'j', 'p', '2') }, /* JPEG 2000 produced by FCP */
  139. { CODEC_ID_RAWVIDEO, MKTAG('2', 'v', 'u', 'y') }, /* UNCOMPRESSED 8BIT 4:2:2 */
  140. { CODEC_ID_NONE, 0 },
  141. };
  142. static const CodecTag mov_audio_tags[] = {
  143. { CODEC_ID_PCM_S32BE, MKTAG('i', 'n', '3', '2') },
  144. { CODEC_ID_PCM_S24BE, MKTAG('i', 'n', '2', '4') },
  145. /* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
  146. { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
  147. /* { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') },*/ /* 8 bits */
  148. { CODEC_ID_PCM_U8, MKTAG('r', 'a', 'w', ' ') }, /* 8 bits unsigned */
  149. { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */
  150. { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */
  151. { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */
  152. { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
  153. { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') }, /* Macintosh Audio Compression and Expansion 3:1 */
  154. { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') }, /* Macintosh Audio Compression and Expansion 6:1 */
  155. { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
  156. { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
  157. { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
  158. /* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
  159. /* MP4 tags */
  160. { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, /* MPEG-4 AAC */
  161. /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
  162. { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') }, /* AMR-NB 3gp */
  163. { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') }, /* AMR-WB 3gp */
  164. { CODEC_ID_AC3, MKTAG('m', 's', 0x20, 0x00) }, /* Dolby AC-3 */
  165. { CODEC_ID_ALAC,MKTAG('a', 'l', 'a', 'c') }, /* Apple Lossless */
  166. { CODEC_ID_QDM2,MKTAG('Q', 'D', 'M', '2') }, /* QDM2 */
  167. { CODEC_ID_NONE, 0 },
  168. };
  169. /* map numeric codes from mdhd atom to ISO 639 */
  170. /* cf. QTFileFormat.pdf p253, qtff.pdf p205 */
  171. /* http://developer.apple.com/documentation/mac/Text/Text-368.html */
  172. /* deprecated by putting the code as 3*5bit ascii */
  173. static const char *mov_mdhd_language_map[] = {
  174. /* 0-9 */
  175. "eng", "fra", "ger", "ita", "dut", "sve", "spa", "dan", "por", "nor",
  176. "heb", "jpn", "ara", "fin", "gre", "ice", "mlt", "tur", "hr "/*scr*/, "chi"/*ace?*/,
  177. "urd", "hin", "tha", "kor", "lit", "pol", "hun", "est", "lav", NULL,
  178. "fo ", NULL, "rus", "chi", NULL, "iri", "alb", "ron", "ces", "slk",
  179. "slv", "yid", "sr ", "mac", "bul", "ukr", "bel", "uzb", "kaz", "aze",
  180. /*?*/
  181. "aze", "arm", "geo", "mol", "kir", "tgk", "tuk", "mon", NULL, "pus",
  182. "kur", "kas", "snd", "tib", "nep", "san", "mar", "ben", "asm", "guj",
  183. "pa ", "ori", "mal", "kan", "tam", "tel", NULL, "bur", "khm", "lao",
  184. /* roman? arabic? */
  185. "vie", "ind", "tgl", "may", "may", "amh", "tir", "orm", "som", "swa",
  186. /*==rundi?*/
  187. NULL, "run", NULL, "mlg", "epo", NULL, NULL, NULL, NULL, NULL,
  188. /* 100 */
  189. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  190. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  191. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "wel", "baq",
  192. "cat", "lat", "que", "grn", "aym", "tat", "uig", "dzo", "jav"
  193. };
  194. /* the QuickTime file format is quite convoluted...
  195. * it has lots of index tables, each indexing something in another one...
  196. * Here we just use what is needed to read the chunks
  197. */
  198. typedef struct MOV_sample_to_chunk_tbl {
  199. long first;
  200. long count;
  201. long id;
  202. } MOV_sample_to_chunk_tbl;
  203. typedef struct {
  204. uint32_t type;
  205. int64_t offset;
  206. int64_t size; /* total size (excluding the size and type fields) */
  207. } MOV_atom_t;
  208. typedef struct {
  209. int seed;
  210. int flags;
  211. int size;
  212. void* clrs;
  213. } MOV_ctab_t;
  214. typedef struct {
  215. uint8_t version;
  216. uint32_t flags; // 24bit
  217. /* 0x03 ESDescrTag */
  218. uint16_t es_id;
  219. #define MP4ODescrTag 0x01
  220. #define MP4IODescrTag 0x02
  221. #define MP4ESDescrTag 0x03
  222. #define MP4DecConfigDescrTag 0x04
  223. #define MP4DecSpecificDescrTag 0x05
  224. #define MP4SLConfigDescrTag 0x06
  225. #define MP4ContentIdDescrTag 0x07
  226. #define MP4SupplContentIdDescrTag 0x08
  227. #define MP4IPIPtrDescrTag 0x09
  228. #define MP4IPMPPtrDescrTag 0x0A
  229. #define MP4IPMPDescrTag 0x0B
  230. #define MP4RegistrationDescrTag 0x0D
  231. #define MP4ESIDIncDescrTag 0x0E
  232. #define MP4ESIDRefDescrTag 0x0F
  233. #define MP4FileIODescrTag 0x10
  234. #define MP4FileODescrTag 0x11
  235. #define MP4ExtProfileLevelDescrTag 0x13
  236. #define MP4ExtDescrTagsStart 0x80
  237. #define MP4ExtDescrTagsEnd 0xFE
  238. uint8_t stream_priority;
  239. /* 0x04 DecConfigDescrTag */
  240. uint8_t object_type_id;
  241. uint8_t stream_type;
  242. /* XXX: really streamType is
  243. * only 6bit, followed by:
  244. * 1bit upStream
  245. * 1bit reserved
  246. */
  247. uint32_t buffer_size_db; // 24
  248. uint32_t max_bitrate;
  249. uint32_t avg_bitrate;
  250. /* 0x05 DecSpecificDescrTag */
  251. uint8_t decoder_cfg_len;
  252. uint8_t *decoder_cfg;
  253. /* 0x06 SLConfigDescrTag */
  254. uint8_t sl_config_len;
  255. uint8_t *sl_config;
  256. } MOV_esds_t;
  257. struct MOVParseTableEntry;
  258. typedef struct MOVStreamContext {
  259. int ffindex; /* the ffmpeg stream id */
  260. long next_chunk;
  261. long chunk_count;
  262. int64_t *chunk_offsets;
  263. int stts_count;
  264. Time2Sample *stts_data;
  265. int ctts_count;
  266. Time2Sample *ctts_data;
  267. int edit_count; /* number of 'edit' (elst atom) */
  268. long sample_to_chunk_sz;
  269. MOV_sample_to_chunk_tbl *sample_to_chunk;
  270. long sample_to_chunk_index;
  271. int sample_to_time_index;
  272. long sample_to_time_sample;
  273. uint64_t sample_to_time_time;
  274. int sample_to_ctime_index;
  275. int sample_to_ctime_sample;
  276. long sample_size;
  277. long sample_count;
  278. long *sample_sizes;
  279. long keyframe_count;
  280. long *keyframes;
  281. int time_scale;
  282. int time_rate;
  283. long current_sample;
  284. long left_in_chunk; /* how many samples before next chunk */
  285. MOV_esds_t esds;
  286. } MOVStreamContext;
  287. typedef struct MOVContext {
  288. int mp4; /* set to 1 as soon as we are sure that the file is an .mp4 file (even some header parsing depends on this) */
  289. AVFormatContext *fc;
  290. int time_scale;
  291. int64_t duration; /* duration of the longest track */
  292. int found_moov; /* when both 'moov' and 'mdat' sections has been found */
  293. int found_mdat; /* we suppose we have enough data to read the file */
  294. int64_t mdat_size;
  295. int64_t mdat_offset;
  296. int ni; ///< non interleaved mode
  297. int total_streams;
  298. /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
  299. * but we need the info to be able to skip data from those streams in the 'mdat' section
  300. */
  301. MOVStreamContext *streams[MAX_STREAMS];
  302. int64_t next_chunk_offset;
  303. MOVStreamContext *partial; /* != 0 : there is still to read in the current chunk */
  304. int ctab_size;
  305. MOV_ctab_t **ctab; /* color tables */
  306. const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
  307. /* NOTE: for recursion save to/ restore from local variable! */
  308. AVPaletteControl palette_control;
  309. } MOVContext;
  310. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  311. /* those functions parse an atom */
  312. /* return code:
  313. 1: found what I wanted, exit
  314. 0: continue to parse next atom
  315. -1: error occured, exit
  316. */
  317. typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
  318. /* links atom IDs to parse functions */
  319. typedef struct MOVParseTableEntry {
  320. uint32_t type;
  321. mov_parse_function func;
  322. } MOVParseTableEntry;
  323. static int ff_mov_lang_to_iso639(int code, char *to)
  324. {
  325. int i;
  326. /* is it the mangled iso code? */
  327. /* see http://www.geocities.com/xhelmboyx/quicktime/formats/mp4-layout.txt */
  328. if (code > 138) {
  329. for (i = 2; i >= 0; i--) {
  330. to[i] = 0x60 + (code & 0x1f);
  331. code >>= 5;
  332. }
  333. return 1;
  334. }
  335. /* old fashion apple lang code */
  336. if (code >= (sizeof(mov_mdhd_language_map)/sizeof(char *)))
  337. return 0;
  338. if (!mov_mdhd_language_map[code])
  339. return 0;
  340. strncpy(to, mov_mdhd_language_map[code], 4);
  341. return 1;
  342. }
  343. int ff_mov_iso639_to_lang(const char *lang, int mp4)
  344. {
  345. int i, code = 0;
  346. /* old way, only for QT? */
  347. for (i = 0; !mp4 && (i < (sizeof(mov_mdhd_language_map)/sizeof(char *))); i++) {
  348. if (mov_mdhd_language_map[i] && !strcmp(lang, mov_mdhd_language_map[i]))
  349. return i;
  350. }
  351. /* XXX:can we do that in mov too? */
  352. if (!mp4)
  353. return 0;
  354. /* handle undefined as such */
  355. if (lang[0] == '\0')
  356. lang = "und";
  357. /* 5bit ascii */
  358. for (i = 0; i < 3; i++) {
  359. unsigned char c = (unsigned char)lang[i];
  360. if (c < 0x60)
  361. return 0;
  362. if (c > 0x60 + 0x1f)
  363. return 0;
  364. code <<= 5;
  365. code |= (c - 0x60);
  366. }
  367. return code;
  368. }
  369. static int mov_read_leaf(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  370. {
  371. if (atom.size>1)
  372. url_fskip(pb, atom.size);
  373. /* url_seek(pb, atom_offset+atom.size, SEEK_SET); */
  374. return 0;
  375. }
  376. static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  377. {
  378. int64_t total_size = 0;
  379. MOV_atom_t a;
  380. int i;
  381. int err = 0;
  382. a.offset = atom.offset;
  383. if (atom.size < 0)
  384. atom.size = 0x7fffffffffffffffLL;
  385. while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
  386. a.size = atom.size;
  387. a.type=0L;
  388. if(atom.size >= 8) {
  389. a.size = get_be32(pb);
  390. a.type = get_le32(pb);
  391. }
  392. total_size += 8;
  393. a.offset += 8;
  394. dprintf("type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n", a.type, (char*)&a.type, a.size, atom.size, total_size);
  395. if (a.size == 1) { /* 64 bit extended size */
  396. a.size = get_be64(pb) - 8;
  397. a.offset += 8;
  398. total_size += 8;
  399. }
  400. if (a.size == 0) {
  401. a.size = atom.size - total_size;
  402. if (a.size <= 8)
  403. break;
  404. }
  405. for (i = 0; c->parse_table[i].type != 0L
  406. && c->parse_table[i].type != a.type; i++)
  407. /* empty */;
  408. a.size -= 8;
  409. if(a.size < 0)
  410. break;
  411. if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
  412. url_fskip(pb, a.size);
  413. } else {
  414. offset_t start_pos = url_ftell(pb);
  415. int64_t left;
  416. err = (c->parse_table[i].func)(c, pb, a);
  417. left = a.size - url_ftell(pb) + start_pos;
  418. if (left > 0) /* skip garbage at atom end */
  419. url_fskip(pb, left);
  420. }
  421. a.offset += a.size;
  422. total_size += a.size;
  423. }
  424. if (!err && total_size < atom.size && atom.size < 0x7ffff) {
  425. url_fskip(pb, atom.size - total_size);
  426. }
  427. return err;
  428. }
  429. static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  430. {
  431. #if 1
  432. url_fskip(pb, atom.size); // for now
  433. #else
  434. VERY VERY BROKEN, NEVER execute this, needs rewrite
  435. unsigned int len;
  436. MOV_ctab_t *t;
  437. c->ctab = av_realloc(c->ctab, ++c->ctab_size);
  438. t = c->ctab[c->ctab_size];
  439. t->seed = get_be32(pb);
  440. t->flags = get_be16(pb);
  441. t->size = get_be16(pb) + 1;
  442. len = 2 * t->size * 4;
  443. if (len > 0) {
  444. t->clrs = av_malloc(len); // 16bit A R G B
  445. if (t->clrs)
  446. get_buffer(pb, t->clrs, len);
  447. }
  448. #endif
  449. return 0;
  450. }
  451. static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  452. {
  453. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  454. int len = 0;
  455. uint32_t type;
  456. uint32_t ctype;
  457. get_byte(pb); /* version */
  458. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  459. /* component type */
  460. ctype = get_le32(pb);
  461. type = get_le32(pb); /* component subtype */
  462. dprintf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
  463. dprintf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
  464. if(ctype == MKTAG('m', 'h', 'l', 'r')) /* MOV */
  465. c->mp4 = 0;
  466. else if(ctype == 0)
  467. c->mp4 = 1;
  468. if(type == MKTAG('v', 'i', 'd', 'e'))
  469. st->codec->codec_type = CODEC_TYPE_VIDEO;
  470. else if(type == MKTAG('s', 'o', 'u', 'n'))
  471. st->codec->codec_type = CODEC_TYPE_AUDIO;
  472. get_be32(pb); /* component manufacture */
  473. get_be32(pb); /* component flags */
  474. get_be32(pb); /* component flags mask */
  475. if(atom.size <= 24)
  476. return 0; /* nothing left to read */
  477. /* XXX: MP4 uses a C string, not a pascal one */
  478. /* component name */
  479. if(c->mp4) {
  480. /* .mp4: C string */
  481. while(get_byte(pb) && (++len < (atom.size - 24)));
  482. } else {
  483. /* .mov: PASCAL string */
  484. len = get_byte(pb);
  485. url_fskip(pb, len);
  486. }
  487. url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
  488. return 0;
  489. }
  490. static int mov_mp4_read_descr_len(ByteIOContext *pb)
  491. {
  492. int len = 0;
  493. int count = 4;
  494. while (count--) {
  495. int c = get_byte(pb);
  496. len = (len << 7) | (c & 0x7f);
  497. if (!(c & 0x80))
  498. break;
  499. }
  500. return len;
  501. }
  502. static int mov_mp4_read_descr(ByteIOContext *pb, int *tag)
  503. {
  504. int len;
  505. *tag = get_byte(pb);
  506. len = mov_mp4_read_descr_len(pb);
  507. dprintf("MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
  508. return len;
  509. }
  510. static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  511. {
  512. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  513. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  514. int tag, len;
  515. /* Well, broken but suffisant for some MP4 streams */
  516. get_be32(pb); /* version + flags */
  517. len = mov_mp4_read_descr(pb, &tag);
  518. if (tag == MP4ESDescrTag) {
  519. get_be16(pb); /* ID */
  520. get_byte(pb); /* priority */
  521. } else
  522. get_be16(pb); /* ID */
  523. len = mov_mp4_read_descr(pb, &tag);
  524. if (tag == MP4DecConfigDescrTag) {
  525. sc->esds.object_type_id = get_byte(pb);
  526. sc->esds.stream_type = get_byte(pb);
  527. sc->esds.buffer_size_db = get_be24(pb);
  528. sc->esds.max_bitrate = get_be32(pb);
  529. sc->esds.avg_bitrate = get_be32(pb);
  530. st->codec->codec_id= codec_get_id(ff_mov_obj_type, sc->esds.object_type_id);
  531. len = mov_mp4_read_descr(pb, &tag);
  532. if (tag == MP4DecSpecificDescrTag) {
  533. dprintf("Specific MPEG4 header len=%d\n", len);
  534. st->codec->extradata = (uint8_t*) av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
  535. if (st->codec->extradata) {
  536. get_buffer(pb, st->codec->extradata, len);
  537. st->codec->extradata_size = len;
  538. /* from mplayer */
  539. if ((*(uint8_t *)st->codec->extradata >> 3) == 29) {
  540. st->codec->codec_id = CODEC_ID_MP3ON4;
  541. }
  542. }
  543. }
  544. }
  545. return 0;
  546. }
  547. /* this atom contains actual media data */
  548. static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  549. {
  550. if(atom.size == 0) /* wrong one (MP4) */
  551. return 0;
  552. c->found_mdat=1;
  553. c->mdat_offset = atom.offset;
  554. c->mdat_size = atom.size;
  555. if(c->found_moov)
  556. return 1; /* found both, just go */
  557. url_fskip(pb, atom.size);
  558. return 0; /* now go for moov */
  559. }
  560. static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  561. {
  562. uint32_t type = get_le32(pb);
  563. /* from mplayer */
  564. switch (type) {
  565. case MKTAG('i', 's', 'o', 'm'):
  566. case MKTAG('m', 'p', '4', '1'):
  567. case MKTAG('m', 'p', '4', '2'):
  568. case MKTAG('3', 'g', 'p', '1'):
  569. case MKTAG('3', 'g', 'p', '2'):
  570. case MKTAG('3', 'g', '2', 'a'):
  571. case MKTAG('3', 'g', 'p', '3'):
  572. case MKTAG('3', 'g', 'p', '4'):
  573. case MKTAG('3', 'g', 'p', '5'):
  574. case MKTAG('m', 'm', 'p', '4'): /* Mobile MP4 */
  575. case MKTAG('M', '4', 'A', ' '): /* Apple iTunes AAC-LC Audio */
  576. case MKTAG('M', '4', 'P', ' '): /* Apple iTunes AAC-LC Protected Audio */
  577. case MKTAG('m', 'j', 'p', '2'): /* Motion Jpeg 2000 */
  578. c->mp4 = 1;
  579. case MKTAG('q', 't', ' ', ' '):
  580. default:
  581. av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
  582. }
  583. get_be32(pb); /* minor version */
  584. url_fskip(pb, atom.size - 8);
  585. return 0;
  586. }
  587. /* this atom should contain all header atoms */
  588. static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  589. {
  590. int err;
  591. err = mov_read_default(c, pb, atom);
  592. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  593. /* so we don't parse the whole file if over a network */
  594. c->found_moov=1;
  595. if(c->found_mdat)
  596. return 1; /* found both, just go */
  597. return 0; /* now go for mdat */
  598. }
  599. static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  600. {
  601. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  602. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  603. int version = get_byte(pb);
  604. int lang;
  605. if (version > 1)
  606. return 1; /* unsupported */
  607. get_byte(pb); get_byte(pb);
  608. get_byte(pb); /* flags */
  609. if (version == 1) {
  610. get_be64(pb);
  611. get_be64(pb);
  612. } else {
  613. get_be32(pb); /* creation time */
  614. get_be32(pb); /* modification time */
  615. }
  616. sc->time_scale = get_be32(pb);
  617. st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
  618. lang = get_be16(pb); /* language */
  619. ff_mov_lang_to_iso639(lang, st->language);
  620. get_be16(pb); /* quality */
  621. return 0;
  622. }
  623. static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  624. {
  625. int version = get_byte(pb); /* version */
  626. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  627. if (version == 1) {
  628. get_be64(pb);
  629. get_be64(pb);
  630. } else {
  631. get_be32(pb); /* creation time */
  632. get_be32(pb); /* modification time */
  633. }
  634. c->time_scale = get_be32(pb); /* time scale */
  635. #ifdef DEBUG
  636. av_log(NULL, AV_LOG_DEBUG, "time scale = %i\n", c->time_scale);
  637. #endif
  638. c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
  639. get_be32(pb); /* preferred scale */
  640. get_be16(pb); /* preferred volume */
  641. url_fskip(pb, 10); /* reserved */
  642. url_fskip(pb, 36); /* display matrix */
  643. get_be32(pb); /* preview time */
  644. get_be32(pb); /* preview duration */
  645. get_be32(pb); /* poster time */
  646. get_be32(pb); /* selection time */
  647. get_be32(pb); /* selection duration */
  648. get_be32(pb); /* current time */
  649. get_be32(pb); /* next track ID */
  650. return 0;
  651. }
  652. static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  653. {
  654. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  655. if((uint64_t)atom.size > (1<<30))
  656. return -1;
  657. // currently SVQ3 decoder expect full STSD header - so let's fake it
  658. // this should be fixed and just SMI header should be passed
  659. av_free(st->codec->extradata);
  660. st->codec->extradata_size = 0x5a + atom.size;
  661. st->codec->extradata = (uint8_t*) av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  662. if (st->codec->extradata) {
  663. strcpy(st->codec->extradata, "SVQ3"); // fake
  664. get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
  665. dprintf("Reading SMI %"PRId64" %s\n", atom.size, (char*)st->codec->extradata + 0x5a);
  666. } else
  667. url_fskip(pb, atom.size);
  668. return 0;
  669. }
  670. static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  671. {
  672. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  673. int little_endian = get_be16(pb);
  674. if (little_endian) {
  675. switch (st->codec->codec_id) {
  676. case CODEC_ID_PCM_S24BE:
  677. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  678. break;
  679. case CODEC_ID_PCM_S32BE:
  680. st->codec->codec_id = CODEC_ID_PCM_S32LE;
  681. break;
  682. default:
  683. break;
  684. }
  685. }
  686. return 0;
  687. }
  688. static int mov_read_alac(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  689. {
  690. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  691. // currently ALAC decoder expect full atom header - so let's fake it
  692. // this should be fixed and just ALAC header should be passed
  693. av_free(st->codec->extradata);
  694. st->codec->extradata_size = 36;
  695. st->codec->extradata = (uint8_t*) av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  696. if (st->codec->extradata) {
  697. strcpy(st->codec->extradata + 4, "alac"); // fake
  698. get_buffer(pb, st->codec->extradata + 8, 36 - 8);
  699. dprintf("Reading alac %d %s\n", st->codec->extradata_size, (char*)st->codec->extradata);
  700. } else
  701. url_fskip(pb, atom.size);
  702. return 0;
  703. }
  704. static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  705. {
  706. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  707. if((uint64_t)atom.size > (1<<30))
  708. return -1;
  709. if (st->codec->codec_id == CODEC_ID_QDM2) {
  710. // pass all frma atom to codec, needed at least for QDM2
  711. av_free(st->codec->extradata);
  712. st->codec->extradata_size = atom.size;
  713. st->codec->extradata = (uint8_t*) av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  714. if (st->codec->extradata) {
  715. get_buffer(pb, st->codec->extradata, atom.size);
  716. } else
  717. url_fskip(pb, atom.size);
  718. } else if (atom.size > 8) { /* to read frma, esds atoms */
  719. mov_read_default(c, pb, atom);
  720. } else
  721. url_fskip(pb, atom.size);
  722. return 0;
  723. }
  724. static int mov_read_jp2h(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  725. {
  726. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  727. if((uint64_t)atom.size > (1<<30))
  728. return -1;
  729. av_free(st->codec->extradata);
  730. st->codec->extradata_size = atom.size + 8;
  731. st->codec->extradata = (uint8_t*) av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  732. /* pass all jp2h atom to codec */
  733. if (st->codec->extradata) {
  734. strcpy(st->codec->extradata + 4, "jp2h");
  735. get_buffer(pb, st->codec->extradata + 8, atom.size);
  736. } else
  737. url_fskip(pb, atom.size);
  738. return 0;
  739. }
  740. static int mov_read_avcC(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  741. {
  742. AVStream *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_size = atom.size;
  747. st->codec->extradata = (uint8_t*) av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  748. if (st->codec->extradata) {
  749. get_buffer(pb, st->codec->extradata, atom.size);
  750. } else
  751. url_fskip(pb, atom.size);
  752. return 0;
  753. }
  754. static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  755. {
  756. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  757. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  758. unsigned int i, entries;
  759. get_byte(pb); /* version */
  760. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  761. entries = get_be32(pb);
  762. if(entries >= UINT_MAX/sizeof(int64_t))
  763. return -1;
  764. sc->chunk_count = entries;
  765. sc->chunk_offsets = (int64_t*) av_malloc(entries * sizeof(int64_t));
  766. if (!sc->chunk_offsets)
  767. return -1;
  768. if (atom.type == MKTAG('s', 't', 'c', 'o')) {
  769. for(i=0; i<entries; i++) {
  770. sc->chunk_offsets[i] = get_be32(pb);
  771. }
  772. } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
  773. for(i=0; i<entries; i++) {
  774. sc->chunk_offsets[i] = get_be64(pb);
  775. }
  776. } else
  777. return -1;
  778. for(i=0; i<c->fc->nb_streams; i++){
  779. MOVStreamContext *sc2 = (MOVStreamContext *)c->fc->streams[i]->priv_data;
  780. if(sc2 && sc2->chunk_offsets){
  781. int64_t first= sc2->chunk_offsets[0];
  782. int64_t last= sc2->chunk_offsets[sc2->chunk_count-1];
  783. if(first >= sc->chunk_offsets[entries-1] || last <= sc->chunk_offsets[0])
  784. c->ni=1;
  785. }
  786. }
  787. return 0;
  788. }
  789. static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  790. {
  791. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  792. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  793. int entries, frames_per_sample;
  794. uint32_t format;
  795. uint8_t codec_name[32];
  796. /* for palette traversal */
  797. int color_depth;
  798. int color_start;
  799. int color_count;
  800. int color_end;
  801. int color_index;
  802. int color_dec;
  803. int color_greyscale;
  804. unsigned char *color_table;
  805. int j;
  806. unsigned char r, g, b;
  807. get_byte(pb); /* version */
  808. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  809. entries = get_be32(pb);
  810. while(entries--) { //Parsing Sample description table
  811. enum CodecID id;
  812. MOV_atom_t a = { 0, 0, 0 };
  813. offset_t start_pos = url_ftell(pb);
  814. int size = get_be32(pb); /* size */
  815. format = get_le32(pb); /* data format */
  816. get_be32(pb); /* reserved */
  817. get_be16(pb); /* reserved */
  818. get_be16(pb); /* index */
  819. st->codec->codec_tag = format;
  820. id = codec_get_id(mov_audio_tags, format);
  821. if (id > 0) {
  822. st->codec->codec_type = CODEC_TYPE_AUDIO;
  823. } else if (format && format != MKTAG('m', 'p', '4', 's')) { /* skip old asf mpeg4 tag */
  824. id = codec_get_id(mov_video_tags, format);
  825. if (id <= 0)
  826. id = codec_get_id(codec_bmp_tags, format);
  827. if (id > 0)
  828. st->codec->codec_type = CODEC_TYPE_VIDEO;
  829. }
  830. dprintf("size=%d 4CC= %c%c%c%c codec_type=%d\n",
  831. size,
  832. (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, (format >> 24) & 0xff,
  833. st->codec->codec_type);
  834. if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
  835. st->codec->codec_id = id;
  836. get_be16(pb); /* version */
  837. get_be16(pb); /* revision level */
  838. get_be32(pb); /* vendor */
  839. get_be32(pb); /* temporal quality */
  840. get_be32(pb); /* spacial quality */
  841. st->codec->width = get_be16(pb); /* width */
  842. st->codec->height = get_be16(pb); /* height */
  843. get_be32(pb); /* horiz resolution */
  844. get_be32(pb); /* vert resolution */
  845. get_be32(pb); /* data size, always 0 */
  846. frames_per_sample = get_be16(pb); /* frames per samples */
  847. #ifdef DEBUG
  848. av_log(NULL, AV_LOG_DEBUG, "frames/samples = %d\n", frames_per_sample);
  849. #endif
  850. get_buffer(pb, codec_name, 32); /* codec name, pascal string (FIXME: true for mp4?) */
  851. if (codec_name[0] <= 31) {
  852. memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
  853. st->codec->codec_name[codec_name[0]] = 0;
  854. }
  855. st->codec->bits_per_sample = get_be16(pb); /* depth */
  856. st->codec->color_table_id = get_be16(pb); /* colortable id */
  857. /* These are set in mov_read_stts and might already be set!
  858. st->codec->time_base.den = 25;
  859. st->codec->time_base.num = 1;
  860. */
  861. /* figure out the palette situation */
  862. color_depth = st->codec->bits_per_sample & 0x1F;
  863. color_greyscale = st->codec->bits_per_sample & 0x20;
  864. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  865. if ((color_depth == 2) || (color_depth == 4) ||
  866. (color_depth == 8)) {
  867. if (color_greyscale) {
  868. /* compute the greyscale palette */
  869. color_count = 1 << color_depth;
  870. color_index = 255;
  871. color_dec = 256 / (color_count - 1);
  872. for (j = 0; j < color_count; j++) {
  873. r = g = b = color_index;
  874. c->palette_control.palette[j] =
  875. (r << 16) | (g << 8) | (b);
  876. color_index -= color_dec;
  877. if (color_index < 0)
  878. color_index = 0;
  879. }
  880. } else if (st->codec->color_table_id & 0x08) {
  881. /* if flag bit 3 is set, use the default palette */
  882. color_count = 1 << color_depth;
  883. if (color_depth == 2)
  884. color_table = ff_qt_default_palette_4;
  885. else if (color_depth == 4)
  886. color_table = ff_qt_default_palette_16;
  887. else
  888. color_table = ff_qt_default_palette_256;
  889. for (j = 0; j < color_count; j++) {
  890. r = color_table[j * 4 + 0];
  891. g = color_table[j * 4 + 1];
  892. b = color_table[j * 4 + 2];
  893. c->palette_control.palette[j] =
  894. (r << 16) | (g << 8) | (b);
  895. }
  896. } else {
  897. /* load the palette from the file */
  898. color_start = get_be32(pb);
  899. color_count = get_be16(pb);
  900. color_end = get_be16(pb);
  901. for (j = color_start; j <= color_end; j++) {
  902. /* each R, G, or B component is 16 bits;
  903. * only use the top 8 bits; skip alpha bytes
  904. * up front */
  905. get_byte(pb);
  906. get_byte(pb);
  907. r = get_byte(pb);
  908. get_byte(pb);
  909. g = get_byte(pb);
  910. get_byte(pb);
  911. b = get_byte(pb);
  912. get_byte(pb);
  913. c->palette_control.palette[j] =
  914. (r << 16) | (g << 8) | (b);
  915. }
  916. }
  917. st->codec->palctrl = &c->palette_control;
  918. st->codec->palctrl->palette_changed = 1;
  919. } else
  920. st->codec->palctrl = NULL;
  921. } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
  922. uint16_t version = get_be16(pb);
  923. st->codec->codec_id = id;
  924. get_be16(pb); /* revision level */
  925. get_be32(pb); /* vendor */
  926. st->codec->channels = get_be16(pb); /* channel count */
  927. st->codec->bits_per_sample = get_be16(pb); /* sample size */
  928. /* do we need to force to 16 for AMR ? */
  929. /* handle specific s8 codec */
  930. get_be16(pb); /* compression id = 0*/
  931. get_be16(pb); /* packet size = 0 */
  932. st->codec->sample_rate = ((get_be32(pb) >> 16));
  933. switch (st->codec->codec_id) {
  934. case CODEC_ID_PCM_S16LE:
  935. case CODEC_ID_PCM_S16BE:
  936. if (st->codec->bits_per_sample == 8)
  937. st->codec->codec_id = CODEC_ID_PCM_S8;
  938. st->codec->bit_rate = st->codec->sample_rate * 8;
  939. break;
  940. case CODEC_ID_PCM_U8:
  941. if (st->codec->bits_per_sample == 16)
  942. st->codec->codec_id = CODEC_ID_PCM_S16BE;
  943. st->codec->bit_rate = st->codec->sample_rate * 8;
  944. break;
  945. case CODEC_ID_AMR_WB:
  946. st->codec->sample_rate = 16000; /* should really we ? */
  947. st->codec->channels=1; /* really needed */
  948. break;
  949. case CODEC_ID_AMR_NB:
  950. st->codec->sample_rate = 8000; /* should really we ? */
  951. st->codec->channels=1; /* really needed */
  952. break;
  953. default:
  954. break;
  955. }
  956. //Read QT version 1 fields. In version 0 theese dont exist
  957. dprintf("version =%d mp4=%d\n",version,c->mp4);
  958. if(version==1) {
  959. get_be32(pb); /* samples per packet */
  960. get_be32(pb); /* bytes per packet */
  961. get_be32(pb); /* bytes per frame */
  962. get_be32(pb); /* bytes per sample */
  963. } else if(version==2) {
  964. get_be32(pb); /* sizeof struct only */
  965. st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
  966. st->codec->channels = get_be32(pb);
  967. get_be32(pb); /* always 0x7F000000 */
  968. get_be32(pb); /* bits per channel if sound is uncompressed */
  969. get_be32(pb); /* lcpm format specific flag */
  970. get_be32(pb); /* bytes per audio packet if constant */
  971. get_be32(pb); /* lpcm frames per audio packet if constant */
  972. }
  973. } else {
  974. /* other codec type, just skip (rtp, mp4s, tmcd ...) */
  975. url_fskip(pb, size - (url_ftell(pb) - start_pos));
  976. }
  977. /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
  978. a.size = size - (url_ftell(pb) - start_pos);
  979. if (a.size > 8)
  980. mov_read_default(c, pb, a);
  981. else if (a.size > 0)
  982. url_fskip(pb, a.size);
  983. }
  984. if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1) {
  985. st->codec->sample_rate= sc->time_scale;
  986. }
  987. switch (st->codec->codec_id) {
  988. #ifdef CONFIG_FAAD
  989. case CODEC_ID_AAC:
  990. #endif
  991. #ifdef CONFIG_VORBIS_DECODER
  992. case CODEC_ID_VORBIS:
  993. #endif
  994. case CODEC_ID_MP3ON4:
  995. st->codec->sample_rate= 0; /* let decoder init parameters properly */
  996. break;
  997. default:
  998. break;
  999. }
  1000. return 0;
  1001. }
  1002. static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1003. {
  1004. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1005. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1006. unsigned int i, entries;
  1007. get_byte(pb); /* version */
  1008. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1009. entries = get_be32(pb);
  1010. if(entries >= UINT_MAX / sizeof(MOV_sample_to_chunk_tbl))
  1011. return -1;
  1012. #ifdef DEBUG
  1013. av_log(NULL, AV_LOG_DEBUG, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  1014. #endif
  1015. sc->sample_to_chunk_sz = entries;
  1016. sc->sample_to_chunk = (MOV_sample_to_chunk_tbl*) av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
  1017. if (!sc->sample_to_chunk)
  1018. return -1;
  1019. for(i=0; i<entries; i++) {
  1020. sc->sample_to_chunk[i].first = get_be32(pb);
  1021. sc->sample_to_chunk[i].count = get_be32(pb);
  1022. sc->sample_to_chunk[i].id = get_be32(pb);
  1023. }
  1024. return 0;
  1025. }
  1026. static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1027. {
  1028. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1029. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1030. unsigned int i, entries;
  1031. get_byte(pb); /* version */
  1032. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1033. entries = get_be32(pb);
  1034. if(entries >= UINT_MAX / sizeof(long))
  1035. return -1;
  1036. sc->keyframe_count = entries;
  1037. #ifdef DEBUG
  1038. av_log(NULL, AV_LOG_DEBUG, "keyframe_count = %ld\n", sc->keyframe_count);
  1039. #endif
  1040. sc->keyframes = (long*) av_malloc(entries * sizeof(long));
  1041. if (!sc->keyframes)
  1042. return -1;
  1043. for(i=0; i<entries; i++) {
  1044. sc->keyframes[i] = get_be32(pb);
  1045. #ifdef DEBUG
  1046. /* av_log(NULL, AV_LOG_DEBUG, "keyframes[]=%ld\n", sc->keyframes[i]); */
  1047. #endif
  1048. }
  1049. return 0;
  1050. }
  1051. static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1052. {
  1053. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1054. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1055. unsigned int i, entries;
  1056. get_byte(pb); /* version */
  1057. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1058. sc->sample_size = get_be32(pb);
  1059. entries = get_be32(pb);
  1060. if(entries >= UINT_MAX / sizeof(long))
  1061. return -1;
  1062. sc->sample_count = entries;
  1063. #ifdef DEBUG
  1064. av_log(NULL, AV_LOG_DEBUG, "sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
  1065. #endif
  1066. if(sc->sample_size) {
  1067. /* override sample size for uncompressed sound */
  1068. switch (st->codec->codec_id) {
  1069. case CODEC_ID_PCM_S32BE:
  1070. case CODEC_ID_PCM_S32LE:
  1071. sc->sample_size = 4 * st->codec->channels;
  1072. break;
  1073. case CODEC_ID_PCM_S24BE:
  1074. case CODEC_ID_PCM_S24LE:
  1075. sc->sample_size = 3 * st->codec->channels;
  1076. break;
  1077. case CODEC_ID_PCM_S16BE:
  1078. case CODEC_ID_PCM_S16LE:
  1079. sc->sample_size = 2 * st->codec->channels;
  1080. break;
  1081. case CODEC_ID_PCM_MULAW:
  1082. case CODEC_ID_PCM_ALAW:
  1083. case CODEC_ID_PCM_S8:
  1084. case CODEC_ID_PCM_U8:
  1085. sc->sample_size = 1 * st->codec->channels;
  1086. break;
  1087. default:
  1088. break;
  1089. }
  1090. assert(sc->sample_size);
  1091. return 0; /* there isn't any table following */
  1092. }
  1093. sc->sample_sizes = (long*) av_malloc(entries * sizeof(long));
  1094. if (!sc->sample_sizes)
  1095. return -1;
  1096. for(i=0; i<entries; i++) {
  1097. sc->sample_sizes[i] = get_be32(pb);
  1098. #ifdef DEBUG
  1099. av_log(NULL, AV_LOG_DEBUG, "sample_sizes[]=%ld\n", sc->sample_sizes[i]);
  1100. #endif
  1101. }
  1102. return 0;
  1103. }
  1104. static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1105. {
  1106. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1107. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1108. unsigned int i, entries;
  1109. int64_t duration=0;
  1110. int64_t total_sample_count=0;
  1111. get_byte(pb); /* version */
  1112. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1113. entries = get_be32(pb);
  1114. if(entries >= UINT_MAX / sizeof(Time2Sample))
  1115. return -1;
  1116. sc->stts_count = entries;
  1117. sc->stts_data = av_malloc(entries * sizeof(Time2Sample));
  1118. #ifdef DEBUG
  1119. av_log(NULL, AV_LOG_DEBUG, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  1120. #endif
  1121. sc->time_rate=0;
  1122. for(i=0; i<entries; i++) {
  1123. int sample_duration;
  1124. int sample_count;
  1125. sample_count=get_be32(pb);
  1126. sample_duration = get_be32(pb);
  1127. sc->stts_data[i].count= sample_count;
  1128. sc->stts_data[i].duration= sample_duration;
  1129. sc->time_rate= ff_gcd(sc->time_rate, sample_duration);
  1130. dprintf("sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  1131. duration+=(int64_t)sample_duration*sample_count;
  1132. total_sample_count+=sample_count;
  1133. }
  1134. st->nb_frames= total_sample_count;
  1135. if(duration)
  1136. st->duration= duration;
  1137. return 0;
  1138. }
  1139. static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1140. {
  1141. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1142. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1143. unsigned int i, entries;
  1144. get_byte(pb); /* version */
  1145. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1146. entries = get_be32(pb);
  1147. if(entries >= UINT_MAX / sizeof(Time2Sample))
  1148. return -1;
  1149. sc->ctts_count = entries;
  1150. sc->ctts_data = av_malloc(entries * sizeof(Time2Sample));
  1151. dprintf("track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1152. for(i=0; i<entries; i++) {
  1153. int count =get_be32(pb);
  1154. int duration =get_be32(pb);
  1155. if (duration < 0) {
  1156. av_log(c->fc, AV_LOG_ERROR, "negative ctts, ignoring\n");
  1157. sc->ctts_count = 0;
  1158. url_fskip(pb, 8 * (entries - i - 1));
  1159. break;
  1160. }
  1161. sc->ctts_data[i].count = count;
  1162. sc->ctts_data[i].duration= duration;
  1163. sc->time_rate= ff_gcd(sc->time_rate, duration);
  1164. }
  1165. return 0;
  1166. }
  1167. static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1168. {
  1169. AVStream *st;
  1170. MOVStreamContext *sc;
  1171. st = av_new_stream(c->fc, c->fc->nb_streams);
  1172. if (!st) return -2;
  1173. sc = (MOVStreamContext*) av_mallocz(sizeof(MOVStreamContext));
  1174. if (!sc) {
  1175. av_free(st);
  1176. return -1;
  1177. }
  1178. sc->sample_to_chunk_index = -1;
  1179. st->priv_data = sc;
  1180. st->codec->codec_type = CODEC_TYPE_DATA;
  1181. st->start_time = 0; /* XXX: check */
  1182. c->streams[c->fc->nb_streams-1] = sc;
  1183. return mov_read_default(c, pb, atom);
  1184. }
  1185. static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1186. {
  1187. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1188. int version = get_byte(pb);
  1189. get_byte(pb); get_byte(pb);
  1190. get_byte(pb); /* flags */
  1191. /*
  1192. MOV_TRACK_ENABLED 0x0001
  1193. MOV_TRACK_IN_MOVIE 0x0002
  1194. MOV_TRACK_IN_PREVIEW 0x0004
  1195. MOV_TRACK_IN_POSTER 0x0008
  1196. */
  1197. if (version == 1) {
  1198. get_be64(pb);
  1199. get_be64(pb);
  1200. } else {
  1201. get_be32(pb); /* creation time */
  1202. get_be32(pb); /* modification time */
  1203. }
  1204. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  1205. get_be32(pb); /* reserved */
  1206. st->start_time = 0; /* check */
  1207. (version == 1) ? get_be64(pb) : get_be32(pb); /* highlevel (considering edits) duration in movie timebase */
  1208. get_be32(pb); /* reserved */
  1209. get_be32(pb); /* reserved */
  1210. get_be16(pb); /* layer */
  1211. get_be16(pb); /* alternate group */
  1212. get_be16(pb); /* volume */
  1213. get_be16(pb); /* reserved */
  1214. url_fskip(pb, 36); /* display matrix */
  1215. /* those are fixed-point */
  1216. get_be32(pb); /* track width */
  1217. get_be32(pb); /* track height */
  1218. return 0;
  1219. }
  1220. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1221. /* like the files created with Adobe Premiere 5.0, for samples see */
  1222. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1223. static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1224. {
  1225. int err;
  1226. if (atom.size < 8)
  1227. return 0; /* continue */
  1228. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1229. url_fskip(pb, atom.size - 4);
  1230. return 0;
  1231. }
  1232. atom.type = get_le32(pb);
  1233. atom.offset += 8;
  1234. atom.size -= 8;
  1235. if (atom.type != MKTAG('m', 'd', 'a', 't')) {
  1236. url_fskip(pb, atom.size);
  1237. return 0;
  1238. }
  1239. err = mov_read_mdat(c, pb, atom);
  1240. return err;
  1241. }
  1242. #ifdef CONFIG_ZLIB
  1243. static int null_read_packet(void *opaque, uint8_t *buf, int buf_size)
  1244. {
  1245. return -1;
  1246. }
  1247. static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1248. {
  1249. ByteIOContext ctx;
  1250. uint8_t *cmov_data;
  1251. uint8_t *moov_data; /* uncompressed data */
  1252. long cmov_len, moov_len;
  1253. int ret;
  1254. get_be32(pb); /* dcom atom */
  1255. if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
  1256. return -1;
  1257. if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
  1258. av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !");
  1259. return -1;
  1260. }
  1261. get_be32(pb); /* cmvd atom */
  1262. if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
  1263. return -1;
  1264. moov_len = get_be32(pb); /* uncompressed size */
  1265. cmov_len = atom.size - 6 * 4;
  1266. cmov_data = (uint8_t *) av_malloc(cmov_len);
  1267. if (!cmov_data)
  1268. return -1;
  1269. moov_data = (uint8_t *) av_malloc(moov_len);
  1270. if (!moov_data) {
  1271. av_free(cmov_data);
  1272. return -1;
  1273. }
  1274. get_buffer(pb, cmov_data, cmov_len);
  1275. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1276. return -1;
  1277. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
  1278. return -1;
  1279. ctx.buf_end = ctx.buffer + moov_len;
  1280. atom.type = MKTAG( 'm', 'o', 'o', 'v' );
  1281. atom.offset = 0;
  1282. atom.size = moov_len;
  1283. #ifdef DEBUG
  1284. // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
  1285. #endif
  1286. ret = mov_read_default(c, &ctx, atom);
  1287. av_free(moov_data);
  1288. av_free(cmov_data);
  1289. return ret;
  1290. }
  1291. #endif
  1292. /* edit list atom */
  1293. static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1294. {
  1295. int i, edit_count;
  1296. get_byte(pb); /* version */
  1297. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1298. edit_count= c->streams[c->fc->nb_streams-1]->edit_count = get_be32(pb); /* entries */
  1299. for(i=0; i<edit_count; i++){
  1300. get_be32(pb); /* Track duration */
  1301. get_be32(pb); /* Media time */
  1302. get_be32(pb); /* Media rate */
  1303. }
  1304. dprintf("track[%i].edit_count = %i\n", c->fc->nb_streams-1, c->streams[c->fc->nb_streams-1]->edit_count);
  1305. return 0;
  1306. }
  1307. static const MOVParseTableEntry mov_default_parse_table[] = {
  1308. /* mp4 atoms */
  1309. { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
  1310. { MKTAG( 'c', 'p', 'r', 't' ), mov_read_default },
  1311. { MKTAG( 'c', 'r', 'h', 'd' ), mov_read_default },
  1312. { MKTAG( 'c', 't', 't', 's' ), mov_read_ctts }, /* composition time to sample */
  1313. { MKTAG( 'd', 'i', 'n', 'f' ), mov_read_default }, /* data information */
  1314. { MKTAG( 'd', 'p', 'n', 'd' ), mov_read_leaf },
  1315. { MKTAG( 'd', 'r', 'e', 'f' ), mov_read_leaf },
  1316. { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
  1317. { MKTAG( 'e', 'l', 's', 't' ), mov_read_elst },
  1318. { MKTAG( 'e', 'n', 'd', 'a' ), mov_read_enda },
  1319. { MKTAG( 'f', 'r', 'e', 'e' ), mov_read_leaf },
  1320. { MKTAG( 'f', 't', 'y', 'p' ), mov_read_ftyp },
  1321. { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
  1322. { MKTAG( 'h', 'i', 'n', 't' ), mov_read_leaf },
  1323. { MKTAG( 'h', 'm', 'h', 'd' ), mov_read_leaf },
  1324. { MKTAG( 'i', 'o', 'd', 's' ), mov_read_leaf },
  1325. { MKTAG( 'j', 'p', '2', 'h' ), mov_read_jp2h },
  1326. { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
  1327. { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
  1328. { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
  1329. { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
  1330. { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
  1331. { MKTAG( 'm', 'p', '4', 'a' ), mov_read_default },
  1332. { MKTAG( 'm', 'p', '4', 's' ), mov_read_default },
  1333. { MKTAG( 'm', 'p', '4', 'v' ), mov_read_default },
  1334. { MKTAG( 'm', 'p', 'o', 'd' ), mov_read_leaf },
  1335. { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
  1336. { MKTAG( 'n', 'm', 'h', 'd' ), mov_read_leaf },
  1337. { MKTAG( 'o', 'd', 'h', 'd' ), mov_read_default },
  1338. { MKTAG( 's', 'd', 'h', 'd' ), mov_read_default },
  1339. { MKTAG( 's', 'k', 'i', 'p' ), mov_read_leaf },
  1340. { MKTAG( 's', 'm', 'h', 'd' ), mov_read_leaf }, /* sound media info header */
  1341. { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorenson extension ??? */
  1342. { MKTAG( 'a', 'l', 'a', 'c' ), mov_read_alac }, /* alac specific atom */
  1343. { MKTAG( 'a', 'v', 'c', 'C' ), mov_read_avcC },
  1344. { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
  1345. { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
  1346. { MKTAG( 's', 't', 'd', 'p' ), mov_read_default },
  1347. { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
  1348. { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
  1349. { MKTAG( 's', 't', 's', 'h' ), mov_read_default },
  1350. { MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */
  1351. { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
  1352. { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
  1353. { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
  1354. { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
  1355. { MKTAG( 't', 'r', 'e', 'f' ), mov_read_default }, /* not really */
  1356. { MKTAG( 'u', 'd', 't', 'a' ), mov_read_leaf },
  1357. { MKTAG( 'u', 'r', 'l', ' ' ), mov_read_leaf },
  1358. { MKTAG( 'u', 'r', 'n', ' ' ), mov_read_leaf },
  1359. { MKTAG( 'u', 'u', 'i', 'd' ), mov_read_leaf },
  1360. { MKTAG( 'v', 'm', 'h', 'd' ), mov_read_leaf }, /* video media info header */
  1361. { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_wave },
  1362. /* extra mp4 */
  1363. { MKTAG( 'M', 'D', 'E', 'S' ), mov_read_leaf },
  1364. /* QT atoms */
  1365. { MKTAG( 'c', 'h', 'a', 'p' ), mov_read_leaf },
  1366. { MKTAG( 'c', 'l', 'i', 'p' ), mov_read_default },
  1367. { MKTAG( 'c', 'r', 'g', 'n' ), mov_read_leaf },
  1368. { MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab },
  1369. { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
  1370. { MKTAG( 'k', 'm', 'a', 't' ), mov_read_leaf },
  1371. { MKTAG( 'm', 'a', 't', 't' ), mov_read_default },
  1372. { MKTAG( 'r', 'd', 'r', 'f' ), mov_read_leaf },
  1373. { MKTAG( 'r', 'm', 'd', 'a' ), mov_read_default },
  1374. { MKTAG( 'r', 'm', 'd', 'r' ), mov_read_leaf },
  1375. { MKTAG( 'r', 'm', 'r', 'a' ), mov_read_default },
  1376. { MKTAG( 's', 'c', 'p', 't' ), mov_read_leaf },
  1377. { MKTAG( 's', 's', 'r', 'c' ), mov_read_leaf },
  1378. { MKTAG( 's', 'y', 'n', 'c' ), mov_read_leaf },
  1379. { MKTAG( 't', 'c', 'm', 'd' ), mov_read_leaf },
  1380. { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
  1381. //{ MKTAG( 'r', 'm', 'q', 'u' ), mov_read_leaf },
  1382. #ifdef CONFIG_ZLIB
  1383. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
  1384. #else
  1385. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_leaf },
  1386. #endif
  1387. { 0L, mov_read_leaf }
  1388. };
  1389. static void mov_free_stream_context(MOVStreamContext *sc)
  1390. {
  1391. if(sc) {
  1392. av_freep(&sc->chunk_offsets);
  1393. av_freep(&sc->sample_to_chunk);
  1394. av_freep(&sc->sample_sizes);
  1395. av_freep(&sc->keyframes);
  1396. av_freep(&sc->stts_data);
  1397. av_freep(&sc->ctts_data);
  1398. av_freep(&sc);
  1399. }
  1400. }
  1401. static inline uint32_t mov_to_tag(uint8_t *buf)
  1402. {
  1403. return MKTAG(buf[0], buf[1], buf[2], buf[3]);
  1404. }
  1405. static inline uint32_t to_be32(uint8_t *buf)
  1406. {
  1407. return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  1408. }
  1409. /* XXX: is it sufficient ? */
  1410. static int mov_probe(AVProbeData *p)
  1411. {
  1412. unsigned int offset;
  1413. uint32_t tag;
  1414. int score = 0;
  1415. /* check file header */
  1416. if (p->buf_size <= 12)
  1417. return 0;
  1418. offset = 0;
  1419. for(;;) {
  1420. /* ignore invalid offset */
  1421. if ((offset + 8) > (unsigned int)p->buf_size)
  1422. return score;
  1423. tag = mov_to_tag(p->buf + offset + 4);
  1424. switch(tag) {
  1425. /* check for obvious tags */
  1426. case MKTAG( 'j', 'P', ' ', ' ' ): /* jpeg 2000 signature */
  1427. case MKTAG( 'm', 'o', 'o', 'v' ):
  1428. case MKTAG( 'm', 'd', 'a', 't' ):
  1429. case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
  1430. case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
  1431. return AVPROBE_SCORE_MAX;
  1432. /* those are more common words, so rate then a bit less */
  1433. case MKTAG( 'w', 'i', 'd', 'e' ):
  1434. case MKTAG( 'f', 'r', 'e', 'e' ):
  1435. case MKTAG( 'j', 'u', 'n', 'k' ):
  1436. case MKTAG( 'p', 'i', 'c', 't' ):
  1437. return AVPROBE_SCORE_MAX - 5;
  1438. case MKTAG( 'f', 't', 'y', 'p' ):
  1439. case MKTAG( 's', 'k', 'i', 'p' ):
  1440. case MKTAG( 'u', 'u', 'i', 'd' ):
  1441. offset = to_be32(p->buf+offset) + offset;
  1442. /* if we only find those cause probedata is too small at least rate them */
  1443. score = AVPROBE_SCORE_MAX - 50;
  1444. break;
  1445. default:
  1446. /* unrecognized tag */
  1447. return score;
  1448. }
  1449. }
  1450. return score;
  1451. }
  1452. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1453. {
  1454. MOVContext *mov = (MOVContext *) s->priv_data;
  1455. ByteIOContext *pb = &s->pb;
  1456. int i, err;
  1457. MOV_atom_t atom = { 0, 0, 0 };
  1458. mov->fc = s;
  1459. mov->parse_table = mov_default_parse_table;
  1460. if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  1461. atom.size = url_fsize(pb);
  1462. else
  1463. atom.size = 0x7FFFFFFFFFFFFFFFLL;
  1464. /* check MOV header */
  1465. err = mov_read_default(mov, pb, atom);
  1466. if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
  1467. av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%"PRId64"\n",
  1468. err, mov->found_moov, mov->found_mdat, url_ftell(pb));
  1469. return -1;
  1470. }
  1471. dprintf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
  1472. /* some cleanup : make sure we are on the mdat atom */
  1473. if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
  1474. url_fseek(pb, mov->mdat_offset, SEEK_SET);
  1475. mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
  1476. mov->total_streams = s->nb_streams;
  1477. for(i=0; i<mov->total_streams; i++) {
  1478. MOVStreamContext *sc = mov->streams[i];
  1479. if(!sc->time_rate)
  1480. sc->time_rate=1;
  1481. if(!sc->time_scale)
  1482. sc->time_scale= mov->time_scale;
  1483. av_set_pts_info(s->streams[i], 64, sc->time_rate, sc->time_scale);
  1484. if(s->streams[i]->duration != AV_NOPTS_VALUE){
  1485. assert(s->streams[i]->duration % sc->time_rate == 0);
  1486. s->streams[i]->duration /= sc->time_rate;
  1487. }
  1488. sc->ffindex = i;
  1489. }
  1490. return 0;
  1491. }
  1492. /* Yes, this is ugly... I didn't write the specs of QT :p */
  1493. /* XXX:remove useless commented code sometime */
  1494. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  1495. {
  1496. MOVContext *mov = (MOVContext *) s->priv_data;
  1497. MOVStreamContext *sc;
  1498. AVStream *st;
  1499. int64_t offset = INT64_MAX;
  1500. int64_t best_dts = INT64_MAX;
  1501. int i, a, b, m;
  1502. int next_sample= -99;
  1503. int size;
  1504. int idx;
  1505. int ret;
  1506. size = 0x0FFFFFFF;
  1507. if (mov->partial) {
  1508. sc = mov->partial;
  1509. idx = sc->sample_to_chunk_index;
  1510. if (idx < 0) return 0;
  1511. dprintf("sc[ffid %d]->sample_size = %ld\n", sc->ffindex, sc->sample_size);
  1512. //size = sc->sample_sizes[sc->current_sample];
  1513. // that ain't working...
  1514. //size = (sc->sample_size)?sc->sample_size:sc->sample_sizes[sc->current_sample];
  1515. size = (sc->sample_size > 1)?sc->sample_size:sc->sample_sizes[sc->current_sample];
  1516. next_sample= sc->current_sample+1;
  1517. sc->left_in_chunk--;
  1518. if (sc->left_in_chunk <= 0)
  1519. mov->partial = 0;
  1520. offset = mov->next_chunk_offset;
  1521. /* extract the sample */
  1522. goto readchunk;
  1523. }
  1524. again:
  1525. sc = 0;
  1526. if(offset == INT64_MAX)
  1527. best_dts= INT64_MAX;
  1528. for(i=0; i<mov->total_streams; i++) {
  1529. MOVStreamContext *msc = mov->streams[i];
  1530. if ((msc->next_chunk < msc->chunk_count) && msc->next_chunk >= 0){
  1531. if (msc->sample_to_time_index < msc->stts_count && mov->ni) {
  1532. int64_t dts;
  1533. int index= msc->sample_to_time_index;
  1534. int sample= msc->sample_to_time_sample;
  1535. int time= msc->sample_to_time_time;
  1536. int duration = msc->stts_data[index].duration;
  1537. int count = msc->stts_data[index].count;
  1538. if (sample + count <= msc->current_sample) {
  1539. sample += count;
  1540. time += count*duration;
  1541. index ++;
  1542. duration = msc->stts_data[index].duration;
  1543. }
  1544. dts = time + (msc->current_sample - sample) * (int64_t)duration;
  1545. dts = av_rescale(dts, AV_TIME_BASE, msc->time_scale);
  1546. dprintf("stream: %d dts: %"PRId64" best_dts: %"PRId64" offset: %"PRId64"\n", i, dts, best_dts, offset);
  1547. if(dts < best_dts){
  1548. best_dts= dts;
  1549. sc = msc;
  1550. offset = msc->chunk_offsets[msc->next_chunk];
  1551. }
  1552. }else{
  1553. if ((msc->chunk_offsets[msc->next_chunk] < offset)) {
  1554. sc = msc;
  1555. offset = msc->chunk_offsets[msc->next_chunk];
  1556. }
  1557. }
  1558. }
  1559. }
  1560. if (!sc || offset==INT64_MAX)
  1561. return -1;
  1562. sc->next_chunk++;
  1563. if(mov->next_chunk_offset < offset) { /* some meta data */
  1564. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1565. mov->next_chunk_offset = offset;
  1566. }
  1567. if(s->streams[sc->ffindex]->discard >= AVDISCARD_ALL) {
  1568. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1569. mov->next_chunk_offset = offset;
  1570. offset = INT64_MAX;
  1571. goto again;
  1572. }
  1573. idx = sc->sample_to_chunk_index;
  1574. if (idx + 1 < sc->sample_to_chunk_sz && sc->next_chunk >= sc->sample_to_chunk[idx + 1].first)
  1575. idx++;
  1576. sc->sample_to_chunk_index = idx;
  1577. /* split chunks into samples */
  1578. if (sc->sample_size == 0 || sc->sample_size > 100) {
  1579. if (idx >= 0 && sc->sample_to_chunk[idx].count != 1) {
  1580. mov->partial = sc;
  1581. /* we'll have to get those samples before next chunk */
  1582. sc->left_in_chunk = sc->sample_to_chunk[idx].count - 1;
  1583. }
  1584. size = (sc->sample_size > 1)?sc->sample_size:sc->sample_sizes[sc->current_sample];
  1585. next_sample= sc->current_sample+1;
  1586. }else{
  1587. int adjusted= 0;
  1588. /* get the chunk size... */
  1589. for(i=0; i<mov->total_streams; i++) {
  1590. MOVStreamContext *msc = mov->streams[i];
  1591. if ((msc->next_chunk < msc->chunk_count)
  1592. && msc->chunk_offsets[msc->next_chunk] - offset < size
  1593. && msc->chunk_offsets[msc->next_chunk] > offset)
  1594. size = msc->chunk_offsets[msc->next_chunk] - offset;
  1595. }
  1596. //Make sure that size is according to sample_size (Needed by .mov files
  1597. //created on a Minolta Dimage Xi where audio chunks contains waste data in the end)
  1598. //needed for 'raw '
  1599. //sample_size is already adjusted in read_stsz
  1600. adjusted= sc->sample_to_chunk[idx].count * sc->sample_size;
  1601. if (adjusted < size) {
  1602. dprintf("adjusted %d, size %d, sample count %ld\n", adjusted, size, sc->sample_to_chunk[idx].count);
  1603. size = adjusted;
  1604. }
  1605. if(idx < sc->sample_to_chunk_sz){
  1606. next_sample= sc->current_sample + sc->sample_to_chunk[idx].count;
  1607. }else
  1608. next_sample= sc->current_sample;
  1609. }
  1610. readchunk:
  1611. dprintf("chunk: %"PRId64" -> %"PRId64" (%i)\n", offset, offset + size, size);
  1612. if(size == 0x0FFFFFFF)
  1613. size = mov->mdat_size + mov->mdat_offset - offset;
  1614. if(size < 0)
  1615. return -1;
  1616. if(size == 0)
  1617. return -1;
  1618. ret = url_fseek(&s->pb, offset, SEEK_SET);
  1619. if (ret < 0)
  1620. return ret;
  1621. av_get_packet(&s->pb, pkt, size);
  1622. pkt->stream_index = sc->ffindex;
  1623. // If the keyframes table exists, mark any samples that are in the table as key frames.
  1624. // If no table exists, treat very sample as a key frame.
  1625. if (sc->keyframe_count) {
  1626. a = 0;
  1627. b = sc->keyframe_count - 1;
  1628. while (a < b) {
  1629. m = (a + b + 1) >> 1;
  1630. if (sc->keyframes[m] > sc->current_sample) {
  1631. b = m - 1;
  1632. } else {
  1633. a = m;
  1634. }
  1635. }
  1636. if (sc->keyframes[a] == sc->current_sample)
  1637. pkt->flags |= PKT_FLAG_KEY;
  1638. }
  1639. else
  1640. pkt->flags |= PKT_FLAG_KEY;
  1641. mov->next_chunk_offset = offset + size;
  1642. /* find the corresponding dts */
  1643. if (sc && sc->sample_to_time_index < sc->stts_count && pkt) {
  1644. unsigned int count;
  1645. uint64_t dts, pts;
  1646. unsigned int duration = sc->stts_data[sc->sample_to_time_index].duration;
  1647. count = sc->stts_data[sc->sample_to_time_index].count;
  1648. if ((sc->sample_to_time_sample + count) <= sc->current_sample) {
  1649. sc->sample_to_time_sample += count;
  1650. sc->sample_to_time_time += count*duration;
  1651. sc->sample_to_time_index ++;
  1652. duration = sc->stts_data[sc->sample_to_time_index].duration;
  1653. }
  1654. dts = sc->sample_to_time_time + (sc->current_sample - sc->sample_to_time_sample) * (int64_t)duration;
  1655. /* find the corresponding pts */
  1656. if (sc->sample_to_ctime_index < sc->ctts_count) {
  1657. int duration = sc->ctts_data[sc->sample_to_ctime_index].duration;
  1658. int count = sc->ctts_data[sc->sample_to_ctime_index].count;
  1659. if ((sc->sample_to_ctime_sample + count) <= sc->current_sample) {
  1660. sc->sample_to_ctime_sample += count;
  1661. sc->sample_to_ctime_index ++;
  1662. duration = sc->ctts_data[sc->sample_to_ctime_index].duration;
  1663. }
  1664. pts = dts + duration;
  1665. }else
  1666. pts = dts;
  1667. st= s->streams[ sc->ffindex ];
  1668. assert(pts % st->time_base.num == 0);
  1669. assert(dts % st->time_base.num == 0);
  1670. pkt->pts = pts / st->time_base.num;
  1671. pkt->dts = dts / st->time_base.num;
  1672. dprintf("stream #%d smp #%ld dts = %"PRId64" pts = %"PRId64" (smp:%ld time:%"PRId64" idx:%d ent:%d count:%d dur:%d)\n"
  1673. , pkt->stream_index, sc->current_sample-1, pkt->dts, pkt->pts
  1674. , sc->sample_to_time_sample
  1675. , sc->sample_to_time_time
  1676. , sc->sample_to_time_index
  1677. , sc->stts_count
  1678. , count
  1679. , duration);
  1680. }
  1681. assert(next_sample>=0);
  1682. sc->current_sample= next_sample;
  1683. return 0;
  1684. }
  1685. #if defined(MOV_SEEK)
  1686. /**
  1687. * Seek method based on the one described in the Appendix C of QTFileFormat.pdf
  1688. */
  1689. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  1690. {
  1691. MOVContext* mov = (MOVContext *) s->priv_data;
  1692. MOVStreamContext* sc;
  1693. int32_t i, a, b, m;
  1694. int64_t start_time;
  1695. int32_t seek_sample, sample;
  1696. int32_t duration;
  1697. int32_t count;
  1698. int32_t chunk;
  1699. int32_t left_in_chunk;
  1700. int64_t chunk_file_offset;
  1701. int64_t sample_file_offset;
  1702. int32_t first_chunk_sample;
  1703. int32_t sample_to_chunk_idx;
  1704. int sample_to_time_index;
  1705. long sample_to_time_sample = 0;
  1706. uint64_t sample_to_time_time = 0;
  1707. int mov_idx;
  1708. // Find the corresponding mov stream
  1709. for (mov_idx = 0; mov_idx < mov->total_streams; mov_idx++)
  1710. if (mov->streams[mov_idx]->ffindex == stream_index)
  1711. break;
  1712. if (mov_idx == mov->total_streams) {
  1713. av_log(s, AV_LOG_ERROR, "mov: requested stream was not found in mov streams (idx=%i)\n", stream_index);
  1714. return -1;
  1715. }
  1716. sc = mov->streams[mov_idx];
  1717. sample_time *= s->streams[stream_index]->time_base.num;
  1718. // Step 1. Find the edit that contains the requested time (elst)
  1719. if (sc->edit_count && 0) {
  1720. // FIXME should handle edit list
  1721. av_log(s, AV_LOG_ERROR, "mov: does not handle seeking in files that contain edit list (c:%d)\n", sc->edit_count);
  1722. return -1;
  1723. }
  1724. // Step 2. Find the corresponding sample using the Time-to-sample atom (stts) */
  1725. dprintf("Searching for time %li in stream #%i (time_scale=%i)\n", (long)sample_time, mov_idx, sc->time_scale);
  1726. start_time = 0; // FIXME use elst atom
  1727. sample = 1; // sample are 0 based in table
  1728. for (i = 0; i < sc->stts_count; i++) {
  1729. count = sc->stts_data[i].count;
  1730. duration = sc->stts_data[i].duration;
  1731. if ((start_time + count*duration) > sample_time) {
  1732. sample_to_time_time = start_time;
  1733. sample_to_time_index = i;
  1734. sample_to_time_sample = sample;
  1735. sample += (sample_time - start_time) / duration;
  1736. break;
  1737. }
  1738. sample += count;
  1739. start_time += count * duration;
  1740. }
  1741. sample_to_time_time = start_time;
  1742. sample_to_time_index = i;
  1743. /* NOTE: despite what qt doc say, the dt value (Display Time in qt vocabulary) computed with the stts atom
  1744. is a decoding time stamp (dts) not a presentation time stamp. And as usual dts != pts for stream with b frames */
  1745. dprintf("Found time %li at sample #%u\n", (long)sample_time, sample);
  1746. if (sample > sc->sample_count) {
  1747. av_log(s, AV_LOG_ERROR, "mov: sample pos is too high, unable to seek (req. sample=%i, sample count=%ld)\n", sample, sc->sample_count);
  1748. return -1;
  1749. }
  1750. // Step 3. Find the prior sync. sample using the Sync sample atom (stss)
  1751. if (sc->keyframe_count) {
  1752. a = 0;
  1753. b = sc->keyframe_count - 1;
  1754. while (a < b) {
  1755. m = (a + b + 1) >> 1;
  1756. if (sc->keyframes[m] > sample) {
  1757. b = m - 1;
  1758. } else {
  1759. a = m;
  1760. }
  1761. }
  1762. // for low latency prob: always use the previous keyframe, just uncomment the next line
  1763. // if (a) a--;
  1764. seek_sample = sc->keyframes[a];
  1765. }
  1766. else
  1767. seek_sample = sample; // else all samples are key frames
  1768. dprintf("Found nearest keyframe at sample #%i \n", seek_sample);
  1769. // Step 4. Find the chunk of the sample using the Sample-to-chunk-atom (stsc)
  1770. for (first_chunk_sample = 1, i = 0; i < (sc->sample_to_chunk_sz - 1); i++) {
  1771. b = (sc->sample_to_chunk[i + 1].first - sc->sample_to_chunk[i].first) * sc->sample_to_chunk[i].count;
  1772. if (seek_sample >= first_chunk_sample && seek_sample < (first_chunk_sample + b))
  1773. break;
  1774. first_chunk_sample += b;
  1775. }
  1776. chunk = sc->sample_to_chunk[i].first + (seek_sample - first_chunk_sample) / sc->sample_to_chunk[i].count;
  1777. left_in_chunk = sc->sample_to_chunk[i].count - (seek_sample - first_chunk_sample) % sc->sample_to_chunk[i].count;
  1778. first_chunk_sample += ((seek_sample - first_chunk_sample) / sc->sample_to_chunk[i].count) * sc->sample_to_chunk[i].count;
  1779. sample_to_chunk_idx = i;
  1780. dprintf("Sample was found in chunk #%i at sample offset %i (idx %i)\n", chunk, seek_sample - first_chunk_sample, sample_to_chunk_idx);
  1781. // Step 5. Find the offset of the chunk using the chunk offset atom
  1782. if (!sc->chunk_offsets) {
  1783. av_log(s, AV_LOG_ERROR, "mov: no chunk offset atom, unable to seek\n");
  1784. return -1;
  1785. }
  1786. if (chunk > sc->chunk_count) {
  1787. av_log(s, AV_LOG_ERROR, "mov: chunk offset atom too short, unable to seek (req. chunk=%i, chunk count=%li)\n", chunk, sc->chunk_count);
  1788. return -1;
  1789. }
  1790. chunk_file_offset = sc->chunk_offsets[chunk - 1];
  1791. dprintf("Chunk file offset is #%"PRIu64"\n", chunk_file_offset);
  1792. // Step 6. Find the byte offset within the chunk using the sample size atom
  1793. sample_file_offset = chunk_file_offset;
  1794. if (sc->sample_size)
  1795. sample_file_offset += (seek_sample - first_chunk_sample) * sc->sample_size;
  1796. else {
  1797. for (i = 0; i < (seek_sample - first_chunk_sample); i++) {
  1798. sample_file_offset += sc->sample_sizes[first_chunk_sample + i - 1];
  1799. }
  1800. }
  1801. dprintf("Sample file offset is #%"PRIu64"\n", sample_file_offset);
  1802. // Step 6. Update the parser
  1803. mov->partial = sc;
  1804. mov->next_chunk_offset = sample_file_offset;
  1805. // Update current stream state
  1806. sc->current_sample = seek_sample - 1; // zero based
  1807. sc->left_in_chunk = left_in_chunk;
  1808. sc->next_chunk = chunk; // +1 -1 (zero based)
  1809. sc->sample_to_chunk_index = sample_to_chunk_idx;
  1810. // Update other streams
  1811. for (i = 0; i<mov->total_streams; i++) {
  1812. MOVStreamContext *msc;
  1813. if (i == mov_idx) continue;
  1814. // Find the nearest 'next' chunk
  1815. msc = mov->streams[i];
  1816. a = 0;
  1817. b = msc->chunk_count - 1;
  1818. while (a < b) {
  1819. m = (a + b + 1) >> 1;
  1820. if (msc->chunk_offsets[m] > chunk_file_offset) {
  1821. b = m - 1;
  1822. } else {
  1823. a = m;
  1824. }
  1825. }
  1826. msc->next_chunk = a;
  1827. if (msc->chunk_offsets[a] < chunk_file_offset && a < (msc->chunk_count-1))
  1828. msc->next_chunk ++;
  1829. dprintf("Nearest next chunk for stream #%i is #%li @%"PRId64"\n", i, msc->next_chunk+1, msc->chunk_offsets[msc->next_chunk]);
  1830. // Compute sample count and index in the sample_to_chunk table (what a pity)
  1831. msc->sample_to_chunk_index = 0;
  1832. msc->current_sample = 0;
  1833. for(; msc->sample_to_chunk_index < (msc->sample_to_chunk_sz - 1)
  1834. && msc->sample_to_chunk[msc->sample_to_chunk_index + 1].first <= (1 + msc->next_chunk); msc->sample_to_chunk_index++) {
  1835. msc->current_sample += (msc->sample_to_chunk[msc->sample_to_chunk_index + 1].first - msc->sample_to_chunk[msc->sample_to_chunk_index].first) \
  1836. * msc->sample_to_chunk[msc->sample_to_chunk_index].count;
  1837. }
  1838. msc->current_sample += (msc->next_chunk - (msc->sample_to_chunk[msc->sample_to_chunk_index].first - 1)) * msc->sample_to_chunk[msc->sample_to_chunk_index].count;
  1839. msc->left_in_chunk = msc->sample_to_chunk[msc->sample_to_chunk_index].count - 1;
  1840. // Find corresponding position in stts (used later to compute dts)
  1841. sample = 0;
  1842. start_time = 0;
  1843. for (msc->sample_to_time_index = 0; msc->sample_to_time_index < msc->stts_count; msc->sample_to_time_index++) {
  1844. count = msc->stts_data[msc->sample_to_time_index].count;
  1845. duration = msc->stts_data[msc->sample_to_time_index].duration;
  1846. if ((sample + count - 1) > msc->current_sample) {
  1847. msc->sample_to_time_time = start_time;
  1848. msc->sample_to_time_sample = sample;
  1849. break;
  1850. }
  1851. sample += count;
  1852. start_time += count * duration;
  1853. }
  1854. sample = 0;
  1855. for (msc->sample_to_ctime_index = 0; msc->sample_to_ctime_index < msc->ctts_count; msc->sample_to_ctime_index++) {
  1856. count = msc->ctts_data[msc->sample_to_ctime_index].count;
  1857. duration = msc->ctts_data[msc->sample_to_ctime_index].duration;
  1858. if ((sample + count - 1) > msc->current_sample) {
  1859. msc->sample_to_ctime_sample = sample;
  1860. break;
  1861. }
  1862. sample += count;
  1863. }
  1864. dprintf("Next Sample for stream #%i is #%li @%li\n", i, msc->current_sample + 1, msc->sample_to_chunk_index + 1);
  1865. }
  1866. return 0;
  1867. }
  1868. #endif
  1869. static int mov_read_close(AVFormatContext *s)
  1870. {
  1871. int i;
  1872. MOVContext *mov = (MOVContext *) s->priv_data;
  1873. for(i=0; i<mov->total_streams; i++)
  1874. mov_free_stream_context(mov->streams[i]);
  1875. /* free color tabs */
  1876. for(i=0; i<mov->ctab_size; i++)
  1877. av_freep(&mov->ctab[i]);
  1878. av_freep(&mov->ctab);
  1879. return 0;
  1880. }
  1881. static AVInputFormat mov_iformat = {
  1882. "mov,mp4,m4a,3gp,3g2,mj2",
  1883. "QuickTime/MPEG4/Motion JPEG 2000 format",
  1884. sizeof(MOVContext),
  1885. mov_probe,
  1886. mov_read_header,
  1887. mov_read_packet,
  1888. mov_read_close,
  1889. #if defined(MOV_SEEK)
  1890. mov_read_seek,
  1891. #endif
  1892. };
  1893. int mov_init(void)
  1894. {
  1895. av_register_input_format(&mov_iformat);
  1896. return 0;
  1897. }