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.

1795 lines
62KB

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