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.

1871 lines
65KB

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