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.

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