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.

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