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.

1816 lines
64KB

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