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.

2169 lines
76KB

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