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.

1799 lines
63KB

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