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.

1820 lines
63KB

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