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.

1801 lines
62KB

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