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.

1879 lines
65KB

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