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.

1873 lines
65KB

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