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