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.

2070 lines
73KB

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