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.

1784 lines
62KB

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