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.

1803 lines
63KB

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