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.

1818 lines
64KB

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