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.

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