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.

1876 lines
65KB

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