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.

1920 lines
67KB

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