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.

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