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.

2183 lines
77KB

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