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.

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