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.

2088 lines
71KB

  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <limits.h>
  20. #include "avformat.h"
  21. #include "avi.h"
  22. #ifdef CONFIG_ZLIB
  23. #include <zlib.h>
  24. #endif
  25. /*
  26. * First version by Francois Revol revol@free.fr
  27. * Seek function by Gael Chardon gael.dev@4now.net
  28. *
  29. * Features and limitations:
  30. * - reads most of the QT files I have (at least the structure),
  31. * the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement.
  32. * FIXED, Francois Revol, 07/17/2002
  33. * - ffmpeg has nearly none of the usual QuickTime codecs,
  34. * although I succesfully dumped raw and mp3 audio tracks off .mov files.
  35. * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
  36. * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes
  37. * (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at
  38. * http://mpeg.telecomitalialab.com/faq.htm
  39. * - the code is quite ugly... maybe I won't do it recursive next time :-)
  40. * - seek is not supported with files that contain edit list
  41. *
  42. * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
  43. * when coding this :) (it's a writer anyway)
  44. *
  45. * Reference documents:
  46. * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
  47. * Apple:
  48. * http://developer.apple.com/documentation/QuickTime/QTFF/
  49. * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
  50. * QuickTime is a trademark of Apple (AFAIK :))
  51. */
  52. //#define DEBUG
  53. #ifdef DEBUG
  54. #include <stdio.h>
  55. #include <fcntl.h>
  56. #endif
  57. #include "qtpalette.h"
  58. /* Allows seeking (MOV_SPLIT_CHUNKS should also be defined) */
  59. #define MOV_SEEK
  60. /* allows chunk splitting - should work now... */
  61. /* in case you can't read a file, try commenting */
  62. #define MOV_SPLIT_CHUNKS
  63. /* Special handling for movies created with Minolta Dimaxe Xi*/
  64. /* this fix should not interfere with other .mov files, but just in case*/
  65. #define MOV_MINOLTA_FIX
  66. /* some streams in QT (and in MP4 mostly) aren't either video nor audio */
  67. /* so we first list them as this, then clean up the list of streams we give back, */
  68. /* getting rid of these */
  69. #define CODEC_TYPE_MOV_OTHER (enum CodecType) 2
  70. static const CodecTag mov_video_tags[] = {
  71. /* { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
  72. /* { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, *//* Uncompressed RGB */
  73. /* { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, *//* Uncompressed YUV422 */
  74. /* { CODEC_ID_RAWVIDEO, MKTAG('A', 'V', 'U', 'I') }, *//* YUV with alpha-channel (AVID Uncompressed) */
  75. /* Graphics */
  76. /* Animation */
  77. /* Apple video */
  78. /* Kodak Photo CD */
  79. { CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
  80. { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
  81. { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
  82. { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
  83. { CODEC_ID_MJPEG, MKTAG('A', 'V', 'D', 'J') }, /* MJPEG with alpha-channel (AVID JFIF meridien compressed) */
  84. /* { CODEC_ID_MJPEG, MKTAG('A', 'V', 'R', 'n') }, *//* MJPEG with alpha-channel (AVID ABVB/Truevision NuVista) */
  85. /* { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
  86. /* Sorenson video */
  87. { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') }, /* Sorenson Video v1 */
  88. { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, /* Sorenson Video v1 */
  89. { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', 'i') }, /* Sorenson Video v1 (from QT specs)*/
  90. { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') }, /* Sorenson Video v3 */
  91. { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
  92. { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
  93. { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D') },
  94. /* { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
  95. { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */
  96. { CODEC_ID_H263, MKTAG('s', '2', '6', '3') }, /* H263 ?? works */
  97. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
  98. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
  99. /* { CODEC_ID_DVVIDEO, MKTAG('A', 'V', 'd', 'v') }, *//* AVID dv */
  100. { CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') }, /* On2 VP3 */
  101. { CODEC_ID_RPZA, MKTAG('r', 'p', 'z', 'a') }, /* Apple Video (RPZA) */
  102. { CODEC_ID_CINEPAK, MKTAG('c', 'v', 'i', 'd') }, /* Cinepak */
  103. { CODEC_ID_8BPS, MKTAG('8', 'B', 'P', 'S') }, /* Planar RGB (8BPS) */
  104. { CODEC_ID_SMC, MKTAG('s', 'm', 'c', ' ') }, /* Apple Graphics (SMC) */
  105. { CODEC_ID_QTRLE, MKTAG('r', 'l', 'e', ' ') }, /* Apple Animation (RLE) */
  106. { CODEC_ID_QDRAW, MKTAG('q', 'd', 'r', 'w') }, /* QuickDraw */
  107. { CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') }, /* AVC-1/H.264 */
  108. { CODEC_ID_NONE, 0 },
  109. };
  110. static const CodecTag mov_audio_tags[] = {
  111. /* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
  112. { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
  113. /* { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') },*/ /* 8 bits */
  114. { CODEC_ID_PCM_U8, MKTAG('r', 'a', 'w', ' ') }, /* 8 bits unsigned */
  115. { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */
  116. { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */
  117. { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */
  118. { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
  119. { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') }, /* Macintosh Audio Compression and Expansion 3:1 */
  120. { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') }, /* Macintosh Audio Compression and Expansion 6:1 */
  121. { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
  122. { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
  123. { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
  124. /* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
  125. /* MP4 tags */
  126. { CODEC_ID_MPEG4AAC, MKTAG('m', 'p', '4', 'a') }, /* MPEG-4 AAC */
  127. /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
  128. { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') }, /* AMR-NB 3gp */
  129. { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') }, /* AMR-WB 3gp */
  130. { CODEC_ID_AC3, MKTAG('m', 's', 0x20, 0x00) }, /* Dolby AC-3 */
  131. { CODEC_ID_NONE, 0 },
  132. };
  133. /* the QuickTime file format is quite convoluted...
  134. * it has lots of index tables, each indexing something in another one...
  135. * Here we just use what is needed to read the chunks
  136. */
  137. typedef struct MOV_sample_to_chunk_tbl {
  138. long first;
  139. long count;
  140. long id;
  141. } MOV_sample_to_chunk_tbl;
  142. typedef struct {
  143. uint32_t type;
  144. int64_t offset;
  145. int64_t size; /* total size (excluding the size and type fields) */
  146. } MOV_atom_t;
  147. typedef struct {
  148. int seed;
  149. int flags;
  150. int size;
  151. void* clrs;
  152. } MOV_ctab_t;
  153. typedef struct {
  154. uint8_t version;
  155. uint32_t flags; // 24bit
  156. /* 0x03 ESDescrTag */
  157. uint16_t es_id;
  158. #define MP4ODescrTag 0x01
  159. #define MP4IODescrTag 0x02
  160. #define MP4ESDescrTag 0x03
  161. #define MP4DecConfigDescrTag 0x04
  162. #define MP4DecSpecificDescrTag 0x05
  163. #define MP4SLConfigDescrTag 0x06
  164. #define MP4ContentIdDescrTag 0x07
  165. #define MP4SupplContentIdDescrTag 0x08
  166. #define MP4IPIPtrDescrTag 0x09
  167. #define MP4IPMPPtrDescrTag 0x0A
  168. #define MP4IPMPDescrTag 0x0B
  169. #define MP4RegistrationDescrTag 0x0D
  170. #define MP4ESIDIncDescrTag 0x0E
  171. #define MP4ESIDRefDescrTag 0x0F
  172. #define MP4FileIODescrTag 0x10
  173. #define MP4FileODescrTag 0x11
  174. #define MP4ExtProfileLevelDescrTag 0x13
  175. #define MP4ExtDescrTagsStart 0x80
  176. #define MP4ExtDescrTagsEnd 0xFE
  177. uint8_t stream_priority;
  178. /* 0x04 DecConfigDescrTag */
  179. uint8_t object_type_id;
  180. uint8_t stream_type;
  181. /* XXX: really streamType is
  182. * only 6bit, followed by:
  183. * 1bit upStream
  184. * 1bit reserved
  185. */
  186. uint32_t buffer_size_db; // 24
  187. uint32_t max_bitrate;
  188. uint32_t avg_bitrate;
  189. /* 0x05 DecSpecificDescrTag */
  190. uint8_t decoder_cfg_len;
  191. uint8_t *decoder_cfg;
  192. /* 0x06 SLConfigDescrTag */
  193. uint8_t sl_config_len;
  194. uint8_t *sl_config;
  195. } MOV_esds_t;
  196. struct MOVParseTableEntry;
  197. typedef struct MOVStreamContext {
  198. int ffindex; /* the ffmpeg stream id */
  199. int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
  200. long next_chunk;
  201. long chunk_count;
  202. int64_t *chunk_offsets;
  203. int32_t stts_count;
  204. uint64_t *stts_data; /* concatenated data from the time-to-sample atom (count|duration) */
  205. int32_t edit_count; /* number of 'edit' (elst atom) */
  206. long sample_to_chunk_sz;
  207. MOV_sample_to_chunk_tbl *sample_to_chunk;
  208. long sample_to_chunk_index;
  209. long sample_size;
  210. long sample_count;
  211. long *sample_sizes;
  212. long keyframe_count;
  213. long *keyframes;
  214. int time_scale;
  215. long current_sample;
  216. long left_in_chunk; /* how many samples before next chunk */
  217. /* specific MPEG4 header which is added at the beginning of the stream */
  218. unsigned int header_len;
  219. uint8_t *header_data;
  220. MOV_esds_t esds;
  221. } MOVStreamContext;
  222. typedef struct MOVContext {
  223. 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) */
  224. AVFormatContext *fc;
  225. int time_scale;
  226. int duration; /* duration of the longest track */
  227. int found_moov; /* when both 'moov' and 'mdat' sections has been found */
  228. int found_mdat; /* we suppose we have enough data to read the file */
  229. int64_t mdat_size;
  230. int64_t mdat_offset;
  231. int total_streams;
  232. /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
  233. * but we need the info to be able to skip data from those streams in the 'mdat' section
  234. */
  235. MOVStreamContext *streams[MAX_STREAMS];
  236. int64_t next_chunk_offset;
  237. MOVStreamContext *partial; /* != 0 : there is still to read in the current chunk */
  238. int ctab_size;
  239. MOV_ctab_t **ctab; /* color tables */
  240. const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
  241. /* NOTE: for recursion save to/ restore from local variable! */
  242. AVPaletteControl palette_control;
  243. } MOVContext;
  244. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  245. /* those functions parse an atom */
  246. /* return code:
  247. 1: found what I wanted, exit
  248. 0: continue to parse next atom
  249. -1: error occured, exit
  250. */
  251. typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
  252. /* links atom IDs to parse functions */
  253. typedef struct MOVParseTableEntry {
  254. uint32_t type;
  255. mov_parse_function func;
  256. } MOVParseTableEntry;
  257. #ifdef DEBUG
  258. /*
  259. * XXX: static sux, even more in a multithreaded environment...
  260. * Avoid them. This is here just to help debugging.
  261. */
  262. static int debug_indent = 0;
  263. void print_atom(const char *str, MOV_atom_t atom)
  264. {
  265. unsigned int tag, i;
  266. tag = (unsigned int) atom.type;
  267. i=debug_indent;
  268. if(tag == 0) tag = MKTAG('N', 'U', 'L', 'L');
  269. while(i--)
  270. av_log(NULL, AV_LOG_DEBUG, "|");
  271. av_log(NULL, AV_LOG_DEBUG, "parse:");
  272. av_log(NULL, AV_LOG_DEBUG, " %s: tag=%c%c%c%c offset=0x%x size=0x%x\n",
  273. str, tag & 0xff,
  274. (tag >> 8) & 0xff,
  275. (tag >> 16) & 0xff,
  276. (tag >> 24) & 0xff,
  277. (unsigned int)atom.offset,
  278. (unsigned int)atom.size);
  279. assert((unsigned int)atom.size < 0x7fffffff);// catching errors
  280. }
  281. #else
  282. #define print_atom(a,b)
  283. #endif
  284. static int mov_read_leaf(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  285. {
  286. print_atom("leaf", atom);
  287. if (atom.size>1)
  288. url_fskip(pb, atom.size);
  289. /* url_seek(pb, atom_offset+atom.size, SEEK_SET); */
  290. return 0;
  291. }
  292. static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  293. {
  294. int64_t total_size = 0;
  295. MOV_atom_t a;
  296. int i;
  297. int err = 0;
  298. #ifdef DEBUG
  299. print_atom("default", atom);
  300. debug_indent++;
  301. #endif
  302. a.offset = atom.offset;
  303. if (atom.size < 0)
  304. atom.size = 0x7fffffffffffffffLL;
  305. while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
  306. a.size = atom.size;
  307. a.type=0L;
  308. if(atom.size >= 8) {
  309. a.size = get_be32(pb);
  310. a.type = get_le32(pb);
  311. }
  312. total_size += 8;
  313. a.offset += 8;
  314. //av_log(NULL, AV_LOG_DEBUG, "type: %08x %.4s sz: %Lx %Lx %Lx\n", type, (char*)&type, size, atom.size, total_size);
  315. if (a.size == 1) { /* 64 bit extended size */
  316. a.size = get_be64(pb) - 8;
  317. a.offset += 8;
  318. total_size += 8;
  319. }
  320. if (a.size == 0) {
  321. a.size = atom.size - total_size;
  322. if (a.size <= 8)
  323. break;
  324. }
  325. for (i = 0; c->parse_table[i].type != 0L
  326. && c->parse_table[i].type != a.type; i++)
  327. /* empty */;
  328. a.size -= 8;
  329. // av_log(NULL, AV_LOG_DEBUG, " i=%ld\n", i);
  330. if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
  331. // url_seek(pb, atom.offset+atom.size, SEEK_SET);
  332. #ifdef DEBUG
  333. print_atom("unknown", a);
  334. #endif
  335. url_fskip(pb, a.size);
  336. } else {
  337. #ifdef DEBUG
  338. //char b[5] = { type & 0xff, (type >> 8) & 0xff, (type >> 16) & 0xff, (type >> 24) & 0xff, 0 };
  339. //print_atom(b, type, offset, size);
  340. #endif
  341. err = (c->parse_table[i].func)(c, pb, a);
  342. }
  343. a.offset += a.size;
  344. total_size += a.size;
  345. }
  346. if (!err && total_size < atom.size && atom.size < 0x7ffff) {
  347. //av_log(NULL, AV_LOG_DEBUG, "RESET %Ld %Ld err:%d\n", atom.size, total_size, err);
  348. url_fskip(pb, atom.size - total_size);
  349. }
  350. #ifdef DEBUG
  351. debug_indent--;
  352. #endif
  353. return err;
  354. }
  355. static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  356. {
  357. unsigned int len;
  358. MOV_ctab_t *t;
  359. //url_fskip(pb, atom.size); // for now
  360. c->ctab = av_realloc(c->ctab, ++c->ctab_size);
  361. t = c->ctab[c->ctab_size];
  362. t->seed = get_be32(pb);
  363. t->flags = get_be16(pb);
  364. t->size = get_be16(pb) + 1;
  365. len = 2 * t->size * 4;
  366. if (len > 0) {
  367. t->clrs = av_malloc(len); // 16bit A R G B
  368. if (t->clrs)
  369. get_buffer(pb, t->clrs, len);
  370. }
  371. return 0;
  372. }
  373. static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  374. {
  375. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  376. int len = 0;
  377. uint32_t type;
  378. uint32_t ctype;
  379. print_atom("hdlr", atom);
  380. get_byte(pb); /* version */
  381. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  382. /* component type */
  383. ctype = get_le32(pb);
  384. type = get_le32(pb); /* component subtype */
  385. #ifdef DEBUG
  386. av_log(NULL, AV_LOG_DEBUG, "ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
  387. av_log(NULL, AV_LOG_DEBUG, "stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
  388. #endif
  389. #ifdef DEBUG
  390. /* XXX: yeah this is ugly... */
  391. if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
  392. if(type == MKTAG('v', 'i', 'd', 'e'))
  393. puts("hdlr: vide");
  394. else if(type == MKTAG('s', 'o', 'u', 'n'))
  395. puts("hdlr: soun");
  396. } else if(ctype == 0) { /* MP4 */
  397. if(type == MKTAG('v', 'i', 'd', 'e'))
  398. puts("hdlr: vide");
  399. else if(type == MKTAG('s', 'o', 'u', 'n'))
  400. puts("hdlr: soun");
  401. else if(type == MKTAG('o', 'd', 's', 'm'))
  402. puts("hdlr: odsm");
  403. else if(type == MKTAG('s', 'd', 's', 'm'))
  404. puts("hdlr: sdsm");
  405. } else puts("hdlr: meta");
  406. #endif
  407. if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
  408. /* helps parsing the string hereafter... */
  409. c->mp4 = 0;
  410. if(type == MKTAG('v', 'i', 'd', 'e'))
  411. st->codec.codec_type = CODEC_TYPE_VIDEO;
  412. else if(type == MKTAG('s', 'o', 'u', 'n'))
  413. st->codec.codec_type = CODEC_TYPE_AUDIO;
  414. } else if(ctype == 0) { /* MP4 */
  415. /* helps parsing the string hereafter... */
  416. c->mp4 = 1;
  417. if(type == MKTAG('v', 'i', 'd', 'e'))
  418. st->codec.codec_type = CODEC_TYPE_VIDEO;
  419. else if(type == MKTAG('s', 'o', 'u', 'n'))
  420. st->codec.codec_type = CODEC_TYPE_AUDIO;
  421. }
  422. get_be32(pb); /* component manufacture */
  423. get_be32(pb); /* component flags */
  424. get_be32(pb); /* component flags mask */
  425. if(atom.size <= 24)
  426. return 0; /* nothing left to read */
  427. /* XXX: MP4 uses a C string, not a pascal one */
  428. /* component name */
  429. if(c->mp4) {
  430. /* .mp4: C string */
  431. while(get_byte(pb) && (++len < (atom.size - 24)));
  432. } else {
  433. /* .mov: PASCAL string */
  434. #ifdef DEBUG
  435. char* buf;
  436. #endif
  437. len = get_byte(pb);
  438. #ifdef DEBUG
  439. buf = (uint8_t*) av_malloc(len+1);
  440. if (buf) {
  441. get_buffer(pb, buf, len);
  442. buf[len] = '\0';
  443. av_log(NULL, AV_LOG_DEBUG, "**buf='%s'\n", buf);
  444. av_free(buf);
  445. } else
  446. #endif
  447. url_fskip(pb, len);
  448. }
  449. url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
  450. return 0;
  451. }
  452. static int mov_mp4_read_descr_len(ByteIOContext *pb)
  453. {
  454. int len = 0;
  455. int count = 4;
  456. while (count--) {
  457. int c = get_byte(pb);
  458. len = (len << 7) | (c & 0x7f);
  459. if (!(c & 0x80))
  460. break;
  461. }
  462. return len;
  463. }
  464. static int mov_mp4_read_descr(ByteIOContext *pb, int *tag)
  465. {
  466. int len;
  467. *tag = get_byte(pb);
  468. len = mov_mp4_read_descr_len(pb);
  469. #ifdef DEBUG
  470. av_log(NULL, AV_LOG_DEBUG, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
  471. #endif
  472. return len;
  473. }
  474. static inline unsigned int get_be24(ByteIOContext *s)
  475. {
  476. unsigned int val;
  477. val = get_byte(s) << 16;
  478. val |= get_byte(s) << 8;
  479. val |= get_byte(s);
  480. return val;
  481. }
  482. static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  483. {
  484. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  485. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  486. int64_t start_pos = url_ftell(pb);
  487. int tag, len;
  488. print_atom("esds", atom);
  489. /* Well, broken but suffisant for some MP4 streams */
  490. get_be32(pb); /* version + flags */
  491. len = mov_mp4_read_descr(pb, &tag);
  492. if (tag == MP4ESDescrTag) {
  493. get_be16(pb); /* ID */
  494. get_byte(pb); /* priority */
  495. } else
  496. get_be16(pb); /* ID */
  497. len = mov_mp4_read_descr(pb, &tag);
  498. if (tag == MP4DecConfigDescrTag) {
  499. sc->esds.object_type_id = get_byte(pb);
  500. sc->esds.stream_type = get_byte(pb);
  501. sc->esds.buffer_size_db = get_be24(pb);
  502. sc->esds.max_bitrate = get_be32(pb);
  503. sc->esds.avg_bitrate = get_be32(pb);
  504. len = mov_mp4_read_descr(pb, &tag);
  505. //av_log(NULL, AV_LOG_DEBUG, "LEN %d TAG %d m:%d a:%d\n", len, tag, sc->esds.max_bitrate, sc->esds.avg_bitrate);
  506. if (tag == MP4DecSpecificDescrTag) {
  507. #ifdef DEBUG
  508. av_log(NULL, AV_LOG_DEBUG, "Specific MPEG4 header len=%d\n", len);
  509. #endif
  510. st->codec.extradata = (uint8_t*) av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
  511. if (st->codec.extradata) {
  512. get_buffer(pb, st->codec.extradata, len);
  513. st->codec.extradata_size = len;
  514. }
  515. }
  516. }
  517. /* in any case, skip garbage */
  518. url_fskip(pb, atom.size - ((url_ftell(pb) - start_pos)));
  519. return 0;
  520. }
  521. /* this atom contains actual media data */
  522. static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  523. {
  524. print_atom("mdat", atom);
  525. if(atom.size == 0) /* wrong one (MP4) */
  526. return 0;
  527. c->found_mdat=1;
  528. c->mdat_offset = atom.offset;
  529. c->mdat_size = atom.size;
  530. if(c->found_moov)
  531. return 1; /* found both, just go */
  532. url_fskip(pb, atom.size);
  533. return 0; /* now go for moov */
  534. }
  535. /* this atom should contain all header atoms */
  536. static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  537. {
  538. int err;
  539. print_atom("moov", atom);
  540. err = mov_read_default(c, pb, atom);
  541. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  542. /* so we don't parse the whole file if over a network */
  543. c->found_moov=1;
  544. if(c->found_mdat)
  545. return 1; /* found both, just go */
  546. return 0; /* now go for mdat */
  547. }
  548. static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  549. {
  550. print_atom("mdhd", atom);
  551. get_byte(pb); /* version */
  552. get_byte(pb); get_byte(pb);
  553. get_byte(pb); /* flags */
  554. get_be32(pb); /* creation time */
  555. get_be32(pb); /* modification time */
  556. c->streams[c->fc->nb_streams-1]->time_scale = get_be32(pb);
  557. #ifdef DEBUG
  558. av_log(NULL, AV_LOG_DEBUG, "track[%i].time_scale = %i\n", c->fc->nb_streams-1, c->streams[c->fc->nb_streams-1]->time_scale); /* time scale */
  559. #endif
  560. get_be32(pb); /* duration */
  561. get_be16(pb); /* language */
  562. get_be16(pb); /* quality */
  563. return 0;
  564. }
  565. static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  566. {
  567. print_atom("mvhd", atom);
  568. get_byte(pb); /* version */
  569. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  570. get_be32(pb); /* creation time */
  571. get_be32(pb); /* modification time */
  572. c->time_scale = get_be32(pb); /* time scale */
  573. #ifdef DEBUG
  574. av_log(NULL, AV_LOG_DEBUG, "time scale = %i\n", c->time_scale);
  575. #endif
  576. c->duration = get_be32(pb); /* duration */
  577. get_be32(pb); /* preferred scale */
  578. get_be16(pb); /* preferred volume */
  579. url_fskip(pb, 10); /* reserved */
  580. url_fskip(pb, 36); /* display matrix */
  581. get_be32(pb); /* preview time */
  582. get_be32(pb); /* preview duration */
  583. get_be32(pb); /* poster time */
  584. get_be32(pb); /* selection time */
  585. get_be32(pb); /* selection duration */
  586. get_be32(pb); /* current time */
  587. get_be32(pb); /* next track ID */
  588. return 0;
  589. }
  590. static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  591. {
  592. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  593. // currently SVQ3 decoder expect full STSD header - so let's fake it
  594. // this should be fixed and just SMI header should be passed
  595. av_free(st->codec.extradata);
  596. st->codec.extradata_size = 0x5a + atom.size;
  597. st->codec.extradata = (uint8_t*) av_mallocz(st->codec.extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  598. if (st->codec.extradata) {
  599. strcpy(st->codec.extradata, "SVQ3"); // fake
  600. get_buffer(pb, st->codec.extradata + 0x5a, atom.size);
  601. //av_log(NULL, AV_LOG_DEBUG, "Reading SMI %Ld %s\n", atom.size, (char*)st->codec.extradata + 0x5a);
  602. } else
  603. url_fskip(pb, atom.size);
  604. return 0;
  605. }
  606. static int mov_read_avcC(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  607. {
  608. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  609. av_free(st->codec.extradata);
  610. st->codec.extradata_size = atom.size;
  611. st->codec.extradata = (uint8_t*) av_mallocz(st->codec.extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  612. if (st->codec.extradata) {
  613. get_buffer(pb, st->codec.extradata, atom.size);
  614. } else
  615. url_fskip(pb, atom.size);
  616. return 0;
  617. }
  618. static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  619. {
  620. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  621. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  622. int entries, i;
  623. print_atom("stco", atom);
  624. get_byte(pb); /* version */
  625. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  626. entries = get_be32(pb);
  627. sc->chunk_count = entries;
  628. sc->chunk_offsets = (int64_t*) av_malloc(entries * sizeof(int64_t));
  629. if (!sc->chunk_offsets)
  630. return -1;
  631. if (atom.type == MKTAG('s', 't', 'c', 'o')) {
  632. for(i=0; i<entries; i++) {
  633. sc->chunk_offsets[i] = get_be32(pb);
  634. }
  635. } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
  636. for(i=0; i<entries; i++) {
  637. sc->chunk_offsets[i] = get_be64(pb);
  638. }
  639. } else
  640. return -1;
  641. #ifdef DEBUG
  642. /*
  643. for(i=0; i<entries; i++) {
  644. av_log(NULL, AV_LOG_DEBUG, "chunk offset=0x%Lx\n", sc->chunk_offsets[i]);
  645. }
  646. */
  647. #endif
  648. return 0;
  649. }
  650. static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  651. {
  652. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  653. //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  654. int entries, frames_per_sample;
  655. uint32_t format;
  656. /* for palette traversal */
  657. int color_depth;
  658. int color_start;
  659. int color_count;
  660. int color_end;
  661. int color_index;
  662. int color_dec;
  663. int color_greyscale;
  664. unsigned char *color_table;
  665. int j;
  666. unsigned char r, g, b;
  667. print_atom("stsd", atom);
  668. get_byte(pb); /* version */
  669. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  670. entries = get_be32(pb);
  671. while(entries--) { //Parsing Sample description table
  672. enum CodecID id;
  673. int size = get_be32(pb); /* size */
  674. format = get_le32(pb); /* data format */
  675. get_be32(pb); /* reserved */
  676. get_be16(pb); /* reserved */
  677. get_be16(pb); /* index */
  678. /* for MPEG4: set codec type by looking for it */
  679. id = codec_get_id(mov_video_tags, format);
  680. if (id >= 0) {
  681. AVCodec *codec;
  682. codec = avcodec_find_decoder(id);
  683. if (codec)
  684. st->codec.codec_type = codec->type;
  685. }
  686. #ifdef DEBUG
  687. av_log(NULL, AV_LOG_DEBUG, "size=%d 4CC= %c%c%c%c codec_type=%d\n",
  688. size,
  689. (format >> 0) & 0xff,
  690. (format >> 8) & 0xff,
  691. (format >> 16) & 0xff,
  692. (format >> 24) & 0xff,
  693. st->codec.codec_type);
  694. #endif
  695. st->codec.codec_tag = format;
  696. if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
  697. MOV_atom_t a = { 0, 0, 0 };
  698. st->codec.codec_id = id;
  699. get_be16(pb); /* version */
  700. get_be16(pb); /* revision level */
  701. get_be32(pb); /* vendor */
  702. get_be32(pb); /* temporal quality */
  703. get_be32(pb); /* spacial quality */
  704. st->codec.width = get_be16(pb); /* width */
  705. st->codec.height = get_be16(pb); /* height */
  706. #if 1
  707. if (st->codec.codec_id == CODEC_ID_MPEG4) {
  708. /* in some MPEG4 the width/height are not correct, so
  709. we ignore this info */
  710. st->codec.width = 0;
  711. st->codec.height = 0;
  712. }
  713. #endif
  714. get_be32(pb); /* horiz resolution */
  715. get_be32(pb); /* vert resolution */
  716. get_be32(pb); /* data size, always 0 */
  717. frames_per_sample = get_be16(pb); /* frames per samples */
  718. #ifdef DEBUG
  719. av_log(NULL, AV_LOG_DEBUG, "frames/samples = %d\n", frames_per_sample);
  720. #endif
  721. get_buffer(pb, (uint8_t *)st->codec.codec_name, 32); /* codec name */
  722. st->codec.bits_per_sample = get_be16(pb); /* depth */
  723. st->codec.color_table_id = get_be16(pb); /* colortable id */
  724. /* These are set in mov_read_stts and might already be set!
  725. st->codec.frame_rate = 25;
  726. st->codec.frame_rate_base = 1;
  727. */
  728. size -= (16+8*4+2+32+2*2);
  729. #if 0
  730. while (size >= 8) {
  731. MOV_atom_t a;
  732. int64_t start_pos;
  733. a.size = get_be32(pb);
  734. a.type = get_le32(pb);
  735. size -= 8;
  736. #ifdef DEBUG
  737. av_log(NULL, AV_LOG_DEBUG, "VIDEO: atom_type=%c%c%c%c atom.size=%Ld size_left=%d\n",
  738. (a.type >> 0) & 0xff,
  739. (a.type >> 8) & 0xff,
  740. (a.type >> 16) & 0xff,
  741. (a.type >> 24) & 0xff,
  742. a.size, size);
  743. #endif
  744. start_pos = url_ftell(pb);
  745. switch(a.type) {
  746. case MKTAG('e', 's', 'd', 's'):
  747. {
  748. int tag, len;
  749. /* Well, broken but suffisant for some MP4 streams */
  750. get_be32(pb); /* version + flags */
  751. len = mov_mp4_read_descr(pb, &tag);
  752. if (tag == 0x03) {
  753. /* MP4ESDescrTag */
  754. get_be16(pb); /* ID */
  755. get_byte(pb); /* priority */
  756. len = mov_mp4_read_descr(pb, &tag);
  757. if (tag != 0x04)
  758. goto fail;
  759. /* MP4DecConfigDescrTag */
  760. get_byte(pb); /* objectTypeId */
  761. get_be32(pb); /* streamType + buffer size */
  762. get_be32(pb); /* max bit rate */
  763. get_be32(pb); /* avg bit rate */
  764. len = mov_mp4_read_descr(pb, &tag);
  765. if (tag != 0x05)
  766. goto fail;
  767. /* MP4DecSpecificDescrTag */
  768. #ifdef DEBUG
  769. av_log(NULL, AV_LOG_DEBUG, "Specific MPEG4 header len=%d\n", len);
  770. #endif
  771. sc->header_data = av_mallocz(len);
  772. if (sc->header_data) {
  773. get_buffer(pb, sc->header_data, len);
  774. sc->header_len = len;
  775. }
  776. }
  777. /* in any case, skip garbage */
  778. }
  779. break;
  780. default:
  781. break;
  782. }
  783. fail:
  784. av_log(NULL, AV_LOG_DEBUG, "ATOMENEWSIZE %Ld %d\n", atom.size, url_ftell(pb) - start_pos);
  785. if (atom.size > 8) {
  786. url_fskip(pb, (atom.size - 8) -
  787. ((url_ftell(pb) - start_pos)));
  788. size -= atom.size - 8;
  789. }
  790. }
  791. if (size > 0) {
  792. /* unknown extension */
  793. url_fskip(pb, size);
  794. }
  795. #else
  796. /* figure out the palette situation */
  797. color_depth = st->codec.bits_per_sample & 0x1F;
  798. color_greyscale = st->codec.bits_per_sample & 0x20;
  799. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  800. if ((color_depth == 2) || (color_depth == 4) ||
  801. (color_depth == 8)) {
  802. if (color_greyscale) {
  803. /* compute the greyscale palette */
  804. color_count = 1 << color_depth;
  805. color_index = 255;
  806. color_dec = 256 / (color_count - 1);
  807. for (j = 0; j < color_count; j++) {
  808. r = g = b = color_index;
  809. c->palette_control.palette[j] =
  810. (r << 16) | (g << 8) | (b);
  811. color_index -= color_dec;
  812. if (color_index < 0)
  813. color_index = 0;
  814. }
  815. } else if (st->codec.color_table_id & 0x08) {
  816. /* if flag bit 3 is set, use the default palette */
  817. color_count = 1 << color_depth;
  818. if (color_depth == 2)
  819. color_table = ff_qt_default_palette_4;
  820. else if (color_depth == 4)
  821. color_table = ff_qt_default_palette_16;
  822. else
  823. color_table = ff_qt_default_palette_256;
  824. for (j = 0; j < color_count; j++) {
  825. r = color_table[j * 4 + 0];
  826. g = color_table[j * 4 + 1];
  827. b = color_table[j * 4 + 2];
  828. c->palette_control.palette[j] =
  829. (r << 16) | (g << 8) | (b);
  830. }
  831. } else {
  832. /* load the palette from the file */
  833. color_start = get_be32(pb);
  834. color_count = get_be16(pb);
  835. color_end = get_be16(pb);
  836. for (j = color_start; j <= color_end; j++) {
  837. /* each R, G, or B component is 16 bits;
  838. * only use the top 8 bits; skip alpha bytes
  839. * up front */
  840. get_byte(pb);
  841. get_byte(pb);
  842. r = get_byte(pb);
  843. get_byte(pb);
  844. g = get_byte(pb);
  845. get_byte(pb);
  846. b = get_byte(pb);
  847. get_byte(pb);
  848. c->palette_control.palette[j] =
  849. (r << 16) | (g << 8) | (b);
  850. }
  851. }
  852. st->codec.palctrl = &c->palette_control;
  853. st->codec.palctrl->palette_changed = 1;
  854. } else
  855. st->codec.palctrl = NULL;
  856. a.size = size;
  857. mov_read_default(c, pb, a);
  858. #endif
  859. } else {
  860. st->codec.codec_id = codec_get_id(mov_audio_tags, format);
  861. if(st->codec.codec_id==CODEC_ID_AMR_NB || st->codec.codec_id==CODEC_ID_AMR_WB) //from TS26.244
  862. {
  863. #ifdef DEBUG
  864. av_log(NULL, AV_LOG_DEBUG, "AMR-NB or AMR-WB audio identified!!\n");
  865. #endif
  866. get_be32(pb);get_be32(pb); //Reserved_8
  867. get_be16(pb);//Reserved_2
  868. get_be16(pb);//Reserved_2
  869. get_be32(pb);//Reserved_4
  870. get_be16(pb);//TimeScale
  871. get_be16(pb);//Reserved_2
  872. //AMRSpecificBox.(10 bytes)
  873. get_be32(pb); //size
  874. get_be32(pb); //type=='damr'
  875. get_be32(pb); //vendor
  876. get_byte(pb); //decoder version
  877. get_be16(pb); //mode_set
  878. get_byte(pb); //mode_change_period
  879. get_byte(pb); //frames_per_sample
  880. st->duration = AV_NOPTS_VALUE;//Not possible to get from this info, must count number of AMR frames
  881. if(st->codec.codec_id==CODEC_ID_AMR_NB)
  882. {
  883. st->codec.sample_rate=8000;
  884. st->codec.channels=1;
  885. }
  886. else //AMR-WB
  887. {
  888. st->codec.sample_rate=16000;
  889. st->codec.channels=1;
  890. }
  891. st->codec.bits_per_sample=16;
  892. st->codec.bit_rate=0; /*It is not possible to tell this before we have
  893. an audio frame and even then every frame can be different*/
  894. }
  895. else if( st->codec.codec_tag == MKTAG( 'm', 'p', '4', 's' ))
  896. {
  897. //This is some stuff for the hint track, lets ignore it!
  898. //Do some mp4 auto detect.
  899. c->mp4=1;
  900. size-=(16);
  901. url_fskip(pb, size); /* The mp4s atom also contians a esds atom that we can skip*/
  902. }
  903. else if( st->codec.codec_tag == MKTAG( 'm', 'p', '4', 'a' ))
  904. {
  905. /* Handle mp4 audio tag */
  906. get_be32(pb); /* version */
  907. get_be32(pb);
  908. st->codec.channels = get_be16(pb); /* channels */
  909. st->codec.bits_per_sample = get_be16(pb); /* bits per sample */
  910. get_be32(pb);
  911. st->codec.sample_rate = get_be16(pb); /* sample rate, not always correct */
  912. get_be16(pb);
  913. c->mp4=1;
  914. {
  915. MOV_atom_t a = { format, url_ftell(pb), size - (20 + 20 + 8) };
  916. mov_read_default(c, pb, a);
  917. }
  918. /* Get correct sample rate from extradata */
  919. if(st->codec.extradata_size) {
  920. const int samplerate_table[] = {
  921. 96000, 88200, 64000, 48000, 44100, 32000,
  922. 24000, 22050, 16000, 12000, 11025, 8000,
  923. 7350, 0, 0, 0
  924. };
  925. unsigned char *px = st->codec.extradata;
  926. // 5 bits objectTypeIndex, 4 bits sampleRateIndex, 4 bits channels
  927. int samplerate_index = ((px[0] & 7) << 1) + ((px[1] >> 7) & 1);
  928. st->codec.sample_rate = samplerate_table[samplerate_index];
  929. }
  930. }
  931. else if(size>=(16+20))
  932. {//16 bytes read, reading atleast 20 more
  933. #ifdef DEBUG
  934. av_log(NULL, AV_LOG_DEBUG, "audio size=0x%X\n",size);
  935. #endif
  936. uint16_t version = get_be16(pb); /* version */
  937. get_be16(pb); /* revision level */
  938. get_be32(pb); /* vendor */
  939. st->codec.channels = get_be16(pb); /* channel count */
  940. st->codec.bits_per_sample = get_be16(pb); /* sample size */
  941. /* handle specific s8 codec */
  942. get_be16(pb); /* compression id = 0*/
  943. get_be16(pb); /* packet size = 0 */
  944. st->codec.sample_rate = ((get_be32(pb) >> 16));
  945. //av_log(NULL, AV_LOG_DEBUG, "CODECID %d %d %.4s\n", st->codec.codec_id, CODEC_ID_PCM_S16BE, (char*)&format);
  946. switch (st->codec.codec_id) {
  947. case CODEC_ID_PCM_S16BE:
  948. if (st->codec.bits_per_sample == 8)
  949. st->codec.codec_id = CODEC_ID_PCM_S8;
  950. /* fall */
  951. case CODEC_ID_PCM_U8:
  952. st->codec.bit_rate = st->codec.sample_rate * 8;
  953. break;
  954. default:
  955. ;
  956. }
  957. //Read QT version 1 fields. In version 0 theese dont exist
  958. #ifdef DEBUG
  959. av_log(NULL, AV_LOG_DEBUG, "version =%d mp4=%d\n",version,c->mp4);
  960. av_log(NULL, AV_LOG_DEBUG, "size-(16+20+16)=%d\n",size-(16+20+16));
  961. #endif
  962. if((version==1) && size>=(16+20+16))
  963. {
  964. get_be32(pb); /* samples per packet */
  965. get_be32(pb); /* bytes per packet */
  966. get_be32(pb); /* bytes per frame */
  967. get_be32(pb); /* bytes per sample */
  968. if(size>(16+20+16))
  969. {
  970. //Optional, additional atom-based fields
  971. #ifdef DEBUG
  972. av_log(NULL, AV_LOG_DEBUG, "offest=0x%X, sizeleft=%d=0x%x,format=%c%c%c%c\n",(int)url_ftell(pb),size - (16 + 20 + 16 ),size - (16 + 20 + 16 ),
  973. (format >> 0) & 0xff,
  974. (format >> 8) & 0xff,
  975. (format >> 16) & 0xff,
  976. (format >> 24) & 0xff);
  977. #endif
  978. MOV_atom_t a = { format, url_ftell(pb), size - (16 + 20 + 16 + 8) };
  979. mov_read_default(c, pb, a);
  980. }
  981. }
  982. else
  983. {
  984. //We should be down to 0 bytes here, but lets make sure.
  985. size-=(16+20);
  986. #ifdef DEBUG
  987. if(size>0)
  988. av_log(NULL, AV_LOG_DEBUG, "skipping 0x%X bytes\n",size-(16+20));
  989. #endif
  990. url_fskip(pb, size);
  991. }
  992. }
  993. else
  994. {
  995. size-=16;
  996. //Unknown size, but lets do our best and skip the rest.
  997. #ifdef DEBUG
  998. av_log(NULL, AV_LOG_DEBUG, "Strange size, skipping 0x%X bytes\n",size);
  999. #endif
  1000. url_fskip(pb, size);
  1001. }
  1002. }
  1003. }
  1004. return 0;
  1005. }
  1006. static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1007. {
  1008. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1009. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1010. int entries, i;
  1011. print_atom("stsc", atom);
  1012. get_byte(pb); /* version */
  1013. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1014. entries = get_be32(pb);
  1015. #ifdef DEBUG
  1016. av_log(NULL, AV_LOG_DEBUG, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  1017. #endif
  1018. sc->sample_to_chunk_sz = entries;
  1019. sc->sample_to_chunk = (MOV_sample_to_chunk_tbl*) av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
  1020. if (!sc->sample_to_chunk)
  1021. return -1;
  1022. for(i=0; i<entries; i++) {
  1023. sc->sample_to_chunk[i].first = get_be32(pb);
  1024. sc->sample_to_chunk[i].count = get_be32(pb);
  1025. sc->sample_to_chunk[i].id = get_be32(pb);
  1026. #ifdef DEBUG
  1027. /* av_log(NULL, AV_LOG_DEBUG, "sample_to_chunk first=%ld count=%ld, id=%ld\n", sc->sample_to_chunk[i].first, sc->sample_to_chunk[i].count, sc->sample_to_chunk[i].id); */
  1028. #endif
  1029. }
  1030. return 0;
  1031. }
  1032. static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1033. {
  1034. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1035. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1036. int entries, i;
  1037. print_atom("stss", atom);
  1038. get_byte(pb); /* version */
  1039. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1040. entries = get_be32(pb);
  1041. sc->keyframe_count = entries;
  1042. #ifdef DEBUG
  1043. av_log(NULL, AV_LOG_DEBUG, "keyframe_count = %ld\n", sc->keyframe_count);
  1044. #endif
  1045. sc->keyframes = (long*) av_malloc(entries * sizeof(long));
  1046. if (!sc->keyframes)
  1047. return -1;
  1048. for(i=0; i<entries; i++) {
  1049. sc->keyframes[i] = get_be32(pb);
  1050. #ifdef DEBUG
  1051. /* av_log(NULL, AV_LOG_DEBUG, "keyframes[]=%ld\n", sc->keyframes[i]); */
  1052. #endif
  1053. }
  1054. return 0;
  1055. }
  1056. static int mov_read_stsz(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. int entries, i;
  1061. print_atom("stsz", atom);
  1062. get_byte(pb); /* version */
  1063. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1064. sc->sample_size = get_be32(pb);
  1065. entries = get_be32(pb);
  1066. sc->sample_count = entries;
  1067. #ifdef DEBUG
  1068. av_log(NULL, AV_LOG_DEBUG, "sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
  1069. #endif
  1070. if(sc->sample_size)
  1071. return 0; /* there isn't any table following */
  1072. sc->sample_sizes = (long*) av_malloc(entries * sizeof(long));
  1073. if (!sc->sample_sizes)
  1074. return -1;
  1075. for(i=0; i<entries; i++) {
  1076. sc->sample_sizes[i] = get_be32(pb);
  1077. #ifdef DEBUG
  1078. /* av_log(NULL, AV_LOG_DEBUG, "sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
  1079. #endif
  1080. }
  1081. return 0;
  1082. }
  1083. static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1084. {
  1085. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1086. //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1087. int entries, i;
  1088. int64_t duration=0;
  1089. int64_t total_sample_count=0;
  1090. print_atom("stts", atom);
  1091. get_byte(pb); /* version */
  1092. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1093. entries = get_be32(pb);
  1094. c->streams[c->fc->nb_streams-1]->stts_count = entries;
  1095. c->streams[c->fc->nb_streams-1]->stts_data = (uint64_t*) av_malloc(entries * sizeof(uint64_t));
  1096. #ifdef DEBUG
  1097. av_log(NULL, AV_LOG_DEBUG, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  1098. #endif
  1099. for(i=0; i<entries; i++) {
  1100. int32_t sample_duration;
  1101. int32_t sample_count;
  1102. sample_count=get_be32(pb);
  1103. sample_duration = get_be32(pb);
  1104. c->streams[c->fc->nb_streams - 1]->stts_data[i] = (uint64_t)sample_count<<32 | (uint64_t)sample_duration;
  1105. #ifdef DEBUG
  1106. av_log(NULL, AV_LOG_DEBUG, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  1107. #endif
  1108. duration+=sample_duration*sample_count;
  1109. total_sample_count+=sample_count;
  1110. #if 0 //We calculate an average instead, needed by .mp4-files created with nec e606 3g phone
  1111. if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
  1112. st->codec.frame_rate_base = sample_duration ? sample_duration : 1;
  1113. st->codec.frame_rate = c->streams[c->fc->nb_streams-1]->time_scale;
  1114. #ifdef DEBUG
  1115. av_log(NULL, AV_LOG_DEBUG, "VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
  1116. #endif
  1117. }
  1118. #endif
  1119. }
  1120. /*The stsd atom which contain codec type sometimes comes after the stts so we cannot check for codec_type*/
  1121. if(duration>0)
  1122. {
  1123. av_reduce(
  1124. &st->codec.frame_rate,
  1125. &st->codec.frame_rate_base,
  1126. c->streams[c->fc->nb_streams-1]->time_scale * total_sample_count,
  1127. duration,
  1128. INT_MAX
  1129. );
  1130. #ifdef DEBUG
  1131. av_log(NULL, AV_LOG_DEBUG, "FRAME RATE average (video or audio)= %f (tot sample count= %i ,tot dur= %i timescale=%d)\n", (float)st->codec.frame_rate/st->codec.frame_rate_base,total_sample_count,duration,c->streams[c->fc->nb_streams-1]->time_scale);
  1132. #endif
  1133. }
  1134. else
  1135. {
  1136. st->codec.frame_rate_base = 1;
  1137. st->codec.frame_rate = c->streams[c->fc->nb_streams-1]->time_scale;
  1138. }
  1139. return 0;
  1140. }
  1141. static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1142. {
  1143. AVStream *st;
  1144. MOVStreamContext *sc;
  1145. print_atom("trak", atom);
  1146. st = av_new_stream(c->fc, c->fc->nb_streams);
  1147. if (!st) return -2;
  1148. sc = (MOVStreamContext*) av_mallocz(sizeof(MOVStreamContext));
  1149. if (!sc) {
  1150. av_free(st);
  1151. return -1;
  1152. }
  1153. sc->sample_to_chunk_index = -1;
  1154. st->priv_data = sc;
  1155. st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
  1156. st->start_time = 0; /* XXX: check */
  1157. st->duration = (c->duration * (int64_t)AV_TIME_BASE) / c->time_scale;
  1158. c->streams[c->fc->nb_streams-1] = sc;
  1159. return mov_read_default(c, pb, atom);
  1160. }
  1161. static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1162. {
  1163. AVStream *st;
  1164. print_atom("tkhd", atom);
  1165. st = c->fc->streams[c->fc->nb_streams-1];
  1166. get_byte(pb); /* version */
  1167. get_byte(pb); get_byte(pb);
  1168. get_byte(pb); /* flags */
  1169. /*
  1170. MOV_TRACK_ENABLED 0x0001
  1171. MOV_TRACK_IN_MOVIE 0x0002
  1172. MOV_TRACK_IN_PREVIEW 0x0004
  1173. MOV_TRACK_IN_POSTER 0x0008
  1174. */
  1175. get_be32(pb); /* creation time */
  1176. get_be32(pb); /* modification time */
  1177. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  1178. get_be32(pb); /* reserved */
  1179. st->start_time = 0; /* check */
  1180. st->duration = (get_be32(pb) * (int64_t)AV_TIME_BASE) / c->time_scale; /* duration */
  1181. get_be32(pb); /* reserved */
  1182. get_be32(pb); /* reserved */
  1183. get_be16(pb); /* layer */
  1184. get_be16(pb); /* alternate group */
  1185. get_be16(pb); /* volume */
  1186. get_be16(pb); /* reserved */
  1187. url_fskip(pb, 36); /* display matrix */
  1188. /* those are fixed-point */
  1189. st->codec.width = get_be32(pb) >> 16; /* track width */
  1190. st->codec.height = get_be32(pb) >> 16; /* track height */
  1191. return 0;
  1192. }
  1193. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1194. /* like the files created with Adobe Premiere 5.0, for samples see */
  1195. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1196. static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1197. {
  1198. int err;
  1199. #ifdef DEBUG
  1200. print_atom("wide", atom);
  1201. debug_indent++;
  1202. #endif
  1203. if (atom.size < 8)
  1204. return 0; /* continue */
  1205. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1206. url_fskip(pb, atom.size - 4);
  1207. return 0;
  1208. }
  1209. atom.type = get_le32(pb);
  1210. atom.offset += 8;
  1211. atom.size -= 8;
  1212. if (atom.type != MKTAG('m', 'd', 'a', 't')) {
  1213. url_fskip(pb, atom.size);
  1214. return 0;
  1215. }
  1216. err = mov_read_mdat(c, pb, atom);
  1217. #ifdef DEBUG
  1218. debug_indent--;
  1219. #endif
  1220. return err;
  1221. }
  1222. #ifdef CONFIG_ZLIB
  1223. static int null_read_packet(void *opaque, uint8_t *buf, int buf_size)
  1224. {
  1225. return -1;
  1226. }
  1227. static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1228. {
  1229. ByteIOContext ctx;
  1230. uint8_t *cmov_data;
  1231. uint8_t *moov_data; /* uncompressed data */
  1232. long cmov_len, moov_len;
  1233. int ret;
  1234. print_atom("cmov", atom);
  1235. get_be32(pb); /* dcom atom */
  1236. if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
  1237. return -1;
  1238. if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
  1239. av_log(NULL, AV_LOG_DEBUG, "unknown compression for cmov atom !");
  1240. return -1;
  1241. }
  1242. get_be32(pb); /* cmvd atom */
  1243. if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
  1244. return -1;
  1245. moov_len = get_be32(pb); /* uncompressed size */
  1246. cmov_len = atom.size - 6 * 4;
  1247. cmov_data = (uint8_t *) av_malloc(cmov_len);
  1248. if (!cmov_data)
  1249. return -1;
  1250. moov_data = (uint8_t *) av_malloc(moov_len);
  1251. if (!moov_data) {
  1252. av_free(cmov_data);
  1253. return -1;
  1254. }
  1255. get_buffer(pb, cmov_data, cmov_len);
  1256. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1257. return -1;
  1258. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
  1259. return -1;
  1260. ctx.buf_end = ctx.buffer + moov_len;
  1261. atom.type = MKTAG( 'm', 'o', 'o', 'v' );
  1262. atom.offset = 0;
  1263. atom.size = moov_len;
  1264. #ifdef DEBUG
  1265. { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
  1266. #endif
  1267. ret = mov_read_default(c, &ctx, atom);
  1268. av_free(moov_data);
  1269. av_free(cmov_data);
  1270. return ret;
  1271. }
  1272. #endif
  1273. /* edit list atom */
  1274. static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1275. {
  1276. int i, edit_count;
  1277. print_atom("elst", atom);
  1278. get_byte(pb); /* version */
  1279. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1280. edit_count= c->streams[c->fc->nb_streams-1]->edit_count = get_be32(pb); /* entries */
  1281. for(i=0; i<edit_count; i++){
  1282. get_be32(pb); /* Track duration */
  1283. get_be32(pb); /* Media time */
  1284. get_be32(pb); /* Media rate */
  1285. }
  1286. #ifdef DEBUG
  1287. av_log(NULL, AV_LOG_DEBUG, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, c->streams[c->fc->nb_streams-1]->edit_count);
  1288. #endif
  1289. return 0;
  1290. }
  1291. static const MOVParseTableEntry mov_default_parse_table[] = {
  1292. /* mp4 atoms */
  1293. { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
  1294. { MKTAG( 'c', 'p', 'r', 't' ), mov_read_default },
  1295. { MKTAG( 'c', 'r', 'h', 'd' ), mov_read_default },
  1296. { MKTAG( 'c', 't', 't', 's' ), mov_read_leaf }, /* composition time to sample */
  1297. { MKTAG( 'd', 'i', 'n', 'f' ), mov_read_default }, /* data information */
  1298. { MKTAG( 'd', 'p', 'n', 'd' ), mov_read_leaf },
  1299. { MKTAG( 'd', 'r', 'e', 'f' ), mov_read_leaf },
  1300. { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
  1301. { MKTAG( 'e', 'l', 's', 't' ), mov_read_elst },
  1302. { MKTAG( 'f', 'r', 'e', 'e' ), mov_read_leaf },
  1303. { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
  1304. { MKTAG( 'h', 'i', 'n', 't' ), mov_read_leaf },
  1305. { MKTAG( 'h', 'm', 'h', 'd' ), mov_read_leaf },
  1306. { MKTAG( 'i', 'o', 'd', 's' ), mov_read_leaf },
  1307. { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
  1308. { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
  1309. { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
  1310. { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
  1311. { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
  1312. { MKTAG( 'm', 'p', '4', 'a' ), mov_read_default },
  1313. { MKTAG( 'm', 'p', '4', 's' ), mov_read_default },
  1314. { MKTAG( 'm', 'p', '4', 'v' ), mov_read_default },
  1315. { MKTAG( 'm', 'p', 'o', 'd' ), mov_read_leaf },
  1316. { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
  1317. { MKTAG( 'n', 'm', 'h', 'd' ), mov_read_leaf },
  1318. { MKTAG( 'o', 'd', 'h', 'd' ), mov_read_default },
  1319. { MKTAG( 's', 'd', 'h', 'd' ), mov_read_default },
  1320. { MKTAG( 's', 'k', 'i', 'p' ), mov_read_leaf },
  1321. { MKTAG( 's', 'm', 'h', 'd' ), mov_read_leaf }, /* sound media info header */
  1322. { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorenson extension ??? */
  1323. { MKTAG( 'a', 'v', 'c', 'C' ), mov_read_avcC },
  1324. { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
  1325. { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
  1326. { MKTAG( 's', 't', 'd', 'p' ), mov_read_default },
  1327. { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
  1328. { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
  1329. { MKTAG( 's', 't', 's', 'h' ), mov_read_default },
  1330. { MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */
  1331. { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
  1332. { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
  1333. { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
  1334. { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
  1335. { MKTAG( 't', 'r', 'e', 'f' ), mov_read_default }, /* not really */
  1336. { MKTAG( 'u', 'd', 't', 'a' ), mov_read_leaf },
  1337. { MKTAG( 'u', 'r', 'l', ' ' ), mov_read_leaf },
  1338. { MKTAG( 'u', 'r', 'n', ' ' ), mov_read_leaf },
  1339. { MKTAG( 'u', 'u', 'i', 'd' ), mov_read_default },
  1340. { MKTAG( 'v', 'm', 'h', 'd' ), mov_read_leaf }, /* video media info header */
  1341. { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_default },
  1342. /* extra mp4 */
  1343. { MKTAG( 'M', 'D', 'E', 'S' ), mov_read_leaf },
  1344. /* QT atoms */
  1345. { MKTAG( 'c', 'h', 'a', 'p' ), mov_read_leaf },
  1346. { MKTAG( 'c', 'l', 'i', 'p' ), mov_read_default },
  1347. { MKTAG( 'c', 'r', 'g', 'n' ), mov_read_leaf },
  1348. { MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab },
  1349. { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
  1350. { MKTAG( 'k', 'm', 'a', 't' ), mov_read_leaf },
  1351. { MKTAG( 'm', 'a', 't', 't' ), mov_read_default },
  1352. { MKTAG( 'r', 'd', 'r', 'f' ), mov_read_leaf },
  1353. { MKTAG( 'r', 'm', 'd', 'a' ), mov_read_default },
  1354. { MKTAG( 'r', 'm', 'd', 'r' ), mov_read_leaf },
  1355. { MKTAG( 'r', 'm', 'r', 'a' ), mov_read_default },
  1356. { MKTAG( 's', 'c', 'p', 't' ), mov_read_leaf },
  1357. { MKTAG( 's', 's', 'r', 'c' ), mov_read_leaf },
  1358. { MKTAG( 's', 'y', 'n', 'c' ), mov_read_leaf },
  1359. { MKTAG( 't', 'c', 'm', 'd' ), mov_read_leaf },
  1360. { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
  1361. //{ MKTAG( 'r', 'm', 'q', 'u' ), mov_read_leaf },
  1362. #ifdef CONFIG_ZLIB
  1363. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
  1364. #else
  1365. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_leaf },
  1366. #endif
  1367. { 0L, mov_read_leaf }
  1368. };
  1369. static void mov_free_stream_context(MOVStreamContext *sc)
  1370. {
  1371. if(sc) {
  1372. av_free(sc->chunk_offsets);
  1373. av_free(sc->sample_to_chunk);
  1374. av_free(sc->sample_sizes);
  1375. av_free(sc->keyframes);
  1376. av_free(sc->header_data);
  1377. av_free(sc->stts_data);
  1378. av_free(sc);
  1379. }
  1380. }
  1381. static inline uint32_t mov_to_tag(uint8_t *buf)
  1382. {
  1383. return MKTAG(buf[0], buf[1], buf[2], buf[3]);
  1384. }
  1385. static inline uint32_t to_be32(uint8_t *buf)
  1386. {
  1387. return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  1388. }
  1389. /* XXX: is it sufficient ? */
  1390. static int mov_probe(AVProbeData *p)
  1391. {
  1392. unsigned int offset;
  1393. uint32_t tag;
  1394. /* check file header */
  1395. if (p->buf_size <= 12)
  1396. return 0;
  1397. offset = 0;
  1398. for(;;) {
  1399. /* ignore invalid offset */
  1400. if ((offset + 8) > (unsigned int)p->buf_size)
  1401. return 0;
  1402. tag = mov_to_tag(p->buf + offset + 4);
  1403. switch(tag) {
  1404. case MKTAG( 'm', 'o', 'o', 'v' ):
  1405. case MKTAG( 'w', 'i', 'd', 'e' ):
  1406. case MKTAG( 'f', 'r', 'e', 'e' ):
  1407. case MKTAG( 'm', 'd', 'a', 't' ):
  1408. case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
  1409. case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
  1410. return AVPROBE_SCORE_MAX;
  1411. case MKTAG( 'f', 't', 'y', 'p' ):
  1412. case MKTAG( 's', 'k', 'i', 'p' ):
  1413. offset = to_be32(p->buf+offset) + offset;
  1414. break;
  1415. default:
  1416. /* unrecognized tag */
  1417. return 0;
  1418. }
  1419. }
  1420. return 0;
  1421. }
  1422. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1423. {
  1424. MOVContext *mov = (MOVContext *) s->priv_data;
  1425. ByteIOContext *pb = &s->pb;
  1426. int i, j, nb, err;
  1427. MOV_atom_t atom = { 0, 0, 0 };
  1428. mov->fc = s;
  1429. mov->parse_table = mov_default_parse_table;
  1430. #if 0
  1431. /* XXX: I think we should auto detect */
  1432. if(s->iformat->name[1] == 'p')
  1433. mov->mp4 = 1;
  1434. #endif
  1435. if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  1436. atom.size = url_filesize(url_fileno(pb));
  1437. else
  1438. atom.size = 0x7FFFFFFFFFFFFFFFLL;
  1439. #ifdef DEBUG
  1440. av_log(NULL, AV_LOG_DEBUG, "filesz=%Ld\n", atom.size);
  1441. #endif
  1442. /* check MOV header */
  1443. err = mov_read_default(mov, pb, atom);
  1444. if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
  1445. av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%lld\n",
  1446. err, mov->found_moov, mov->found_mdat, url_ftell(pb));
  1447. return -1;
  1448. }
  1449. #ifdef DEBUG
  1450. av_log(NULL, AV_LOG_DEBUG, "on_parse_exit_offset=%d\n", (int) url_ftell(pb));
  1451. #endif
  1452. /* some cleanup : make sure we are on the mdat atom */
  1453. if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
  1454. url_fseek(pb, mov->mdat_offset, SEEK_SET);
  1455. mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
  1456. #ifdef DEBUG
  1457. av_log(NULL, AV_LOG_DEBUG, "mdat_reset_offset=%d\n", (int) url_ftell(pb));
  1458. #endif
  1459. #ifdef DEBUG
  1460. av_log(NULL, AV_LOG_DEBUG, "streams= %d\n", s->nb_streams);
  1461. #endif
  1462. mov->total_streams = nb = s->nb_streams;
  1463. #if 1
  1464. for(i=0; i<s->nb_streams;) {
  1465. if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
  1466. av_free(s->streams[i]);
  1467. for(j=i+1; j<s->nb_streams; j++)
  1468. s->streams[j-1] = s->streams[j];
  1469. s->nb_streams--;
  1470. } else
  1471. i++;
  1472. }
  1473. for(i=0; i<s->nb_streams;i++) {
  1474. MOVStreamContext *sc;
  1475. sc = (MOVStreamContext *)s->streams[i]->priv_data;
  1476. sc->ffindex = i;
  1477. sc->is_ff_stream = 1;
  1478. }
  1479. #endif
  1480. #ifdef DEBUG
  1481. av_log(NULL, AV_LOG_DEBUG, "real streams= %d\n", s->nb_streams);
  1482. #endif
  1483. return 0;
  1484. }
  1485. /* Yes, this is ugly... I didn't write the specs of QT :p */
  1486. /* XXX:remove useless commented code sometime */
  1487. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  1488. {
  1489. MOVContext *mov = (MOVContext *) s->priv_data;
  1490. MOVStreamContext *sc;
  1491. int64_t offset = 0x0FFFFFFFFFFFFFFFLL;
  1492. int i, a, b, m;
  1493. int size;
  1494. size = 0x0FFFFFFF;
  1495. #ifdef MOV_SPLIT_CHUNKS
  1496. if (mov->partial) {
  1497. int idx;
  1498. sc = mov->partial;
  1499. idx = sc->sample_to_chunk_index;
  1500. if (idx < 0) return 0;
  1501. size = sc->sample_sizes[sc->current_sample];
  1502. sc->current_sample++;
  1503. sc->left_in_chunk--;
  1504. if (sc->left_in_chunk <= 0)
  1505. mov->partial = 0;
  1506. offset = mov->next_chunk_offset;
  1507. /* extract the sample */
  1508. goto readchunk;
  1509. }
  1510. #endif
  1511. again:
  1512. sc = 0;
  1513. for(i=0; i<mov->total_streams; i++) {
  1514. MOVStreamContext *msc = mov->streams[i];
  1515. //av_log(NULL, AV_LOG_DEBUG, "MOCHUNK %ld %d %p pos:%Ld\n", mov->streams[i]->next_chunk, mov->total_streams, mov->streams[i], url_ftell(&s->pb));
  1516. if ((msc->next_chunk < msc->chunk_count) && msc->next_chunk >= 0
  1517. && (msc->chunk_offsets[msc->next_chunk] < offset)) {
  1518. sc = msc;
  1519. offset = msc->chunk_offsets[msc->next_chunk];
  1520. //av_log(NULL, AV_LOG_DEBUG, "SELETED %Ld i:%d\n", offset, i);
  1521. }
  1522. }
  1523. if (!sc || offset==0x0FFFFFFFFFFFFFFFLL)
  1524. return -1;
  1525. sc->next_chunk++;
  1526. if(mov->next_chunk_offset < offset) { /* some meta data */
  1527. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1528. mov->next_chunk_offset = offset;
  1529. }
  1530. //av_log(NULL, AV_LOG_DEBUG, "chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
  1531. if(!sc->is_ff_stream) {
  1532. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1533. mov->next_chunk_offset = offset;
  1534. offset = 0x0FFFFFFFFFFFFFFFLL;
  1535. goto again;
  1536. }
  1537. /* now get the chunk size... */
  1538. for(i=0; i<mov->total_streams; i++) {
  1539. MOVStreamContext *msc = mov->streams[i];
  1540. if ((msc->next_chunk < msc->chunk_count)
  1541. && ((msc->chunk_offsets[msc->next_chunk] - offset) < size))
  1542. size = msc->chunk_offsets[msc->next_chunk] - offset;
  1543. }
  1544. #ifdef MOV_MINOLTA_FIX
  1545. //Make sure that size is according to sample_size (Needed by .mov files
  1546. //created on a Minolta Dimage Xi where audio chunks contains waste data in the end)
  1547. //Maybe we should really not only check sc->sample_size, but also sc->sample_sizes
  1548. //but I have no such movies
  1549. if (sc->sample_size > 0) {
  1550. int foundsize=0;
  1551. for(i=0; i<(sc->sample_to_chunk_sz); i++) {
  1552. if( (sc->sample_to_chunk[i].first)<=(sc->next_chunk) && (sc->sample_size>0) )
  1553. {
  1554. // I can't figure out why for PCM audio sample_size is always 1
  1555. // (it should actually be channels*bits_per_second/8) but it is.
  1556. AVCodecContext* cod = &s->streams[sc->ffindex]->codec;
  1557. if (sc->sample_size == 1 && (cod->codec_id == CODEC_ID_PCM_S16BE || cod->codec_id == CODEC_ID_PCM_S16LE))
  1558. foundsize=(sc->sample_to_chunk[i].count*cod->channels*cod->bits_per_sample)/8;
  1559. else
  1560. foundsize=sc->sample_to_chunk[i].count*sc->sample_size;
  1561. }
  1562. #ifdef DEBUG
  1563. /*av_log(NULL, AV_LOG_DEBUG, "sample_to_chunk first=%ld count=%ld, id=%ld\n", sc->sample_to_chunk[i].first, sc->sample_to_chunk[i].count, sc->sample_to_chunk[i].id);*/
  1564. #endif
  1565. }
  1566. if( (foundsize>0) && (foundsize<size) )
  1567. {
  1568. #ifdef DEBUG
  1569. /*av_log(NULL, AV_LOG_DEBUG, "this size should actually be %d\n",foundsize);*/
  1570. #endif
  1571. size=foundsize;
  1572. }
  1573. }
  1574. #endif //MOV_MINOLTA_FIX
  1575. #ifdef MOV_SPLIT_CHUNKS
  1576. /* split chunks into samples */
  1577. if (sc->sample_size == 0) {
  1578. int idx = sc->sample_to_chunk_index;
  1579. if ((idx + 1 < sc->sample_to_chunk_sz)
  1580. && (sc->next_chunk >= sc->sample_to_chunk[idx + 1].first))
  1581. idx++;
  1582. sc->sample_to_chunk_index = idx;
  1583. if (idx >= 0 && sc->sample_to_chunk[idx].count != 1) {
  1584. mov->partial = sc;
  1585. /* we'll have to get those samples before next chunk */
  1586. sc->left_in_chunk = sc->sample_to_chunk[idx].count - 1;
  1587. size = sc->sample_sizes[sc->current_sample];
  1588. }
  1589. sc->current_sample++;
  1590. }
  1591. #endif
  1592. readchunk:
  1593. //av_log(NULL, AV_LOG_DEBUG, "chunk: [%i] %lli -> %lli (%i)\n", st_id, offset, offset + size, size);
  1594. if(size == 0x0FFFFFFF)
  1595. size = mov->mdat_size + mov->mdat_offset - offset;
  1596. if(size < 0)
  1597. return -1;
  1598. if(size == 0)
  1599. return -1;
  1600. url_fseek(&s->pb, offset, SEEK_SET);
  1601. //av_log(NULL, AV_LOG_DEBUG, "READCHUNK hlen: %d %d off: %Ld pos:%Ld\n", size, sc->header_len, offset, url_ftell(&s->pb));
  1602. if (sc->header_len > 0) {
  1603. av_new_packet(pkt, size + sc->header_len);
  1604. memcpy(pkt->data, sc->header_data, sc->header_len);
  1605. get_buffer(&s->pb, pkt->data + sc->header_len, size);
  1606. /* free header */
  1607. av_freep(&sc->header_data);
  1608. sc->header_len = 0;
  1609. } else {
  1610. av_new_packet(pkt, size);
  1611. get_buffer(&s->pb, pkt->data, pkt->size);
  1612. }
  1613. pkt->stream_index = sc->ffindex;
  1614. // If the keyframes table exists, mark any samples that are in the table as key frames.
  1615. // If no table exists, treat very sample as a key frame.
  1616. if (sc->keyframes) {
  1617. a = 0;
  1618. b = sc->keyframe_count - 1;
  1619. while (a < b) {
  1620. m = (a + b + 1) >> 1;
  1621. if (sc->keyframes[m] > sc->current_sample) {
  1622. b = m - 1;
  1623. } else {
  1624. a = m;
  1625. }
  1626. }
  1627. if (sc->keyframes[a] == sc->current_sample)
  1628. pkt->flags |= PKT_FLAG_KEY;
  1629. }
  1630. else
  1631. pkt->flags |= PKT_FLAG_KEY;
  1632. #ifdef DEBUG
  1633. /*
  1634. av_log(NULL, AV_LOG_DEBUG, "Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
  1635. for(i=0; i<8; i++)
  1636. av_log(NULL, AV_LOG_DEBUG, "%02x ", pkt->data[i]);
  1637. for(i=0; i<8; i++)
  1638. av_log(NULL, AV_LOG_DEBUG, "%c ", (pkt->data[i]) & 0x7F);
  1639. puts("");
  1640. */
  1641. #endif
  1642. mov->next_chunk_offset = offset + size;
  1643. return 0;
  1644. }
  1645. #if defined(MOV_SPLIT_CHUNKS) && defined(MOV_SEEK)
  1646. /**
  1647. * Seek method based on the one described in the Appendix C of QTFileFormat.pdf
  1648. */
  1649. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  1650. {
  1651. MOVContext* mov = (MOVContext *) s->priv_data;
  1652. MOVStreamContext* sc;
  1653. int32_t i, a, b, m;
  1654. int64_t sample_time;
  1655. int64_t start_time;
  1656. int32_t seek_sample, sample;
  1657. int32_t duration;
  1658. int32_t count;
  1659. int32_t chunk;
  1660. int32_t left_in_chunk;
  1661. int64_t chunk_file_offset;
  1662. int64_t sample_file_offset;
  1663. int32_t first_chunk_sample;
  1664. int32_t sample_to_chunk_idx;
  1665. int mov_idx;
  1666. // Find the corresponding mov stream
  1667. for (mov_idx = 0; mov_idx < mov->total_streams; mov_idx++)
  1668. if (mov->streams[mov_idx]->ffindex == stream_index)
  1669. break;
  1670. if (mov_idx == mov->total_streams) {
  1671. av_log(s, AV_LOG_ERROR, "mov: requested stream was not found in mov streams (idx=%i)\n", stream_index);
  1672. return -1;
  1673. }
  1674. sc = mov->streams[mov_idx];
  1675. // Step 1. Find the edit that contains the requested time (elst)
  1676. if (sc->edit_count) {
  1677. // FIXME should handle edit list
  1678. av_log(s, AV_LOG_ERROR, "mov: does not handle seeking in files that contain edit list (c:%d)\n", sc->edit_count);
  1679. return -1;
  1680. }
  1681. // Step 2. Find the corresponding sample using the Time-to-sample atom (stts) */
  1682. #ifdef DEBUG
  1683. av_log(s, AV_LOG_DEBUG, "Searching for time %li in stream #%i (time_scale=%i)\n", (long)timestamp, mov_idx, sc->time_scale);
  1684. #endif
  1685. // convert timestamp from time_base unit to timescale unit
  1686. sample_time = av_rescale( timestamp,
  1687. (int64_t)sc->time_scale * s->streams[stream_index]->time_base.num,
  1688. (int64_t)s->streams[stream_index]->time_base.den);
  1689. start_time = 0; // FIXME use elst atom
  1690. sample = 1; // sample are 0 based in table
  1691. #ifdef DEBUG
  1692. av_log(s, AV_LOG_DEBUG, "Searching for sample_time %li \n", (long)sample_time);
  1693. #endif
  1694. for (i = 0; i < sc->stts_count; i++) {
  1695. count = (uint32_t)(sc->stts_data[i]>>32);
  1696. duration = (uint32_t)(sc->stts_data[i]&0xffff);
  1697. //av_log(s, AV_LOG_DEBUG, "> sample_time %lli \n", (long)sample_time);
  1698. //av_log(s, AV_LOG_DEBUG, "> count=%i duration=%i\n", count, duration);
  1699. if ((start_time + count*duration) > sample_time) {
  1700. sample += (sample_time - start_time) / duration;
  1701. break;
  1702. }
  1703. sample += count;
  1704. start_time += count * duration;
  1705. }
  1706. /* NOTE: despite what qt doc say, the dt value (Display Time in qt vocabulary) computed with the stts atom
  1707. is a decoding time stamp (dts) not a presentation time stamp. And as usual dts != pts for stream with b frames */
  1708. #ifdef DEBUG
  1709. av_log(s, AV_LOG_DEBUG, "Found time %li at sample #%u\n", (long)sample_time, sample);
  1710. #endif
  1711. if (sample > sc->sample_count) {
  1712. av_log(s, AV_LOG_ERROR, "mov: sample pos is too high, unable to seek (req. sample=%i, sample count=%ld)\n", sample, sc->sample_count);
  1713. return -1;
  1714. }
  1715. // Step 3. Find the prior sync. sample using the Sync sample atom (stss)
  1716. if (sc->keyframes) {
  1717. a = 0;
  1718. b = sc->keyframe_count - 1;
  1719. while (a < b) {
  1720. m = (a + b + 1) >> 1;
  1721. if (sc->keyframes[m] > sample) {
  1722. b = m - 1;
  1723. } else {
  1724. a = m;
  1725. }
  1726. #ifdef DEBUG
  1727. // av_log(s, AV_LOG_DEBUG, "a=%i (%i) b=%i (%i) m=%i (%i) stream #%i\n", a, sc->keyframes[a], b, sc->keyframes[b], m, sc->keyframes[m], mov_idx);
  1728. #endif
  1729. }
  1730. seek_sample = sc->keyframes[a];
  1731. }
  1732. else
  1733. seek_sample = sample; // else all samples are key frames
  1734. #ifdef DEBUG
  1735. av_log(s, AV_LOG_DEBUG, "Found nearest keyframe at sample #%i \n", seek_sample);
  1736. #endif
  1737. // Step 4. Find the chunk of the sample using the Sample-to-chunk-atom (stsc)
  1738. for (first_chunk_sample = 1, i = 0; i < (sc->sample_to_chunk_sz - 1); i++) {
  1739. b = (sc->sample_to_chunk[i + 1].first - sc->sample_to_chunk[i].first) * sc->sample_to_chunk[i].count;
  1740. if (seek_sample >= first_chunk_sample && seek_sample < (first_chunk_sample + b))
  1741. break;
  1742. first_chunk_sample += b;
  1743. }
  1744. chunk = sc->sample_to_chunk[i].first + (seek_sample - first_chunk_sample) / sc->sample_to_chunk[i].count;
  1745. left_in_chunk = sc->sample_to_chunk[i].count - (seek_sample - first_chunk_sample) % sc->sample_to_chunk[i].count;
  1746. first_chunk_sample += ((seek_sample - first_chunk_sample) / sc->sample_to_chunk[i].count) * sc->sample_to_chunk[i].count;
  1747. sample_to_chunk_idx = i;
  1748. #ifdef DEBUG
  1749. av_log(s, AV_LOG_DEBUG, "Sample was found in chunk #%i at sample offset %i (idx %i)\n", chunk, seek_sample - first_chunk_sample, sample_to_chunk_idx);
  1750. #endif
  1751. // Step 5. Find the offset of the chunk using the chunk offset atom
  1752. if (!sc->chunk_offsets) {
  1753. av_log(s, AV_LOG_ERROR, "mov: no chunk offset atom, unable to seek\n");
  1754. return -1;
  1755. }
  1756. if (chunk > sc->chunk_count) {
  1757. av_log(s, AV_LOG_ERROR, "mov: chunk offset atom too short, unable to seek (req. chunk=%i, chunk count=%li)\n", chunk, sc->chunk_count);
  1758. return -1;
  1759. }
  1760. chunk_file_offset = sc->chunk_offsets[chunk - 1];
  1761. #ifdef DEBUG
  1762. av_log(s, AV_LOG_DEBUG, "Chunk file offset is #%llu \n", chunk_file_offset);
  1763. #endif
  1764. // Step 6. Find the byte offset within the chunk using the sample size atom
  1765. sample_file_offset = chunk_file_offset;
  1766. if (sc->sample_size)
  1767. sample_file_offset += (seek_sample - first_chunk_sample) * sc->sample_size;
  1768. else {
  1769. for (i = 0; i < (seek_sample - first_chunk_sample); i++) {
  1770. sample_file_offset += sc->sample_sizes[first_chunk_sample + i - 1];
  1771. }
  1772. }
  1773. #ifdef DEBUG
  1774. av_log(s, AV_LOG_DEBUG, "Sample file offset is #%llu \n", sample_file_offset);
  1775. #endif
  1776. // Step 6. Update the parser
  1777. mov->partial = sc;
  1778. mov->next_chunk_offset = sample_file_offset;
  1779. // Update current stream state
  1780. sc->current_sample = seek_sample - 1; // zero based
  1781. sc->left_in_chunk = left_in_chunk;
  1782. sc->next_chunk = chunk; // +1 -1 (zero based)
  1783. sc->sample_to_chunk_index = sample_to_chunk_idx;
  1784. // Update other streams
  1785. for (i = 0; i<mov->total_streams; i++) {
  1786. MOVStreamContext *msc;
  1787. if (i == mov_idx) continue;
  1788. // Find the nearest 'next' chunk
  1789. msc = mov->streams[i];
  1790. a = 0;
  1791. b = msc->chunk_count - 1;
  1792. while (a < b) {
  1793. m = (a + b + 1) >> 1;
  1794. if (msc->chunk_offsets[m] > chunk_file_offset) {
  1795. b = m - 1;
  1796. } else {
  1797. a = m;
  1798. }
  1799. #ifdef DEBUG
  1800. /* av_log(s, AV_LOG_DEBUG, "a=%i (%li) b=%i (%li) m=%i (%li) stream #%i\n"
  1801. , a, (long)msc->chunk_offsets[a], b, (long)msc->chunk_offsets[b], m, (long)msc->chunk_offsets[m], i); */
  1802. #endif
  1803. }
  1804. msc->next_chunk = a;
  1805. if (msc->chunk_offsets[a] < chunk_file_offset && a < (msc->chunk_count-1))
  1806. msc->next_chunk ++;
  1807. #ifdef DEBUG
  1808. av_log(s, AV_LOG_DEBUG, "Nearest next chunk for stream #%i is #%i @%lli\n", i, msc->next_chunk+1, msc->chunk_offsets[msc->next_chunk]);
  1809. #endif
  1810. // Compute sample count and index in the sample_to_chunk table (what a pity)
  1811. msc->sample_to_chunk_index = 0;
  1812. msc->current_sample = 0;
  1813. for(; msc->sample_to_chunk_index < (msc->sample_to_chunk_sz - 1)
  1814. && msc->sample_to_chunk[msc->sample_to_chunk_index + 1].first <= (1 + msc->next_chunk); msc->sample_to_chunk_index++) {
  1815. msc->current_sample += (msc->sample_to_chunk[msc->sample_to_chunk_index + 1].first - msc->sample_to_chunk[msc->sample_to_chunk_index].first) \
  1816. * msc->sample_to_chunk[msc->sample_to_chunk_index].count;
  1817. }
  1818. msc->current_sample += (msc->next_chunk - (msc->sample_to_chunk[msc->sample_to_chunk_index].first - 1)) * sc->sample_to_chunk[msc->sample_to_chunk_index].count;
  1819. msc->left_in_chunk = msc->sample_to_chunk[msc->sample_to_chunk_index].count - 1;
  1820. #ifdef DEBUG
  1821. av_log(s, AV_LOG_DEBUG, "Next Sample for stream #%i is #%i @%i\n", i, msc->current_sample + 1, msc->sample_to_chunk_index + 1);
  1822. #endif
  1823. }
  1824. return 0;
  1825. }
  1826. #endif
  1827. static int mov_read_close(AVFormatContext *s)
  1828. {
  1829. int i;
  1830. MOVContext *mov = (MOVContext *) s->priv_data;
  1831. for(i=0; i<mov->total_streams; i++)
  1832. mov_free_stream_context(mov->streams[i]);
  1833. /* free color tabs */
  1834. for(i=0; i<mov->ctab_size; i++)
  1835. av_freep(&mov->ctab[i]);
  1836. av_freep(&mov->ctab);
  1837. return 0;
  1838. }
  1839. static AVInputFormat mov_iformat = {
  1840. "mov,mp4,m4a,3gp",
  1841. "QuickTime/MPEG4 format",
  1842. sizeof(MOVContext),
  1843. mov_probe,
  1844. mov_read_header,
  1845. mov_read_packet,
  1846. mov_read_close,
  1847. #if defined(MOV_SPLIT_CHUNKS) && defined(MOV_SEEK)
  1848. mov_read_seek,
  1849. #endif
  1850. };
  1851. int mov_init(void)
  1852. {
  1853. av_register_input_format(&mov_iformat);
  1854. return 0;
  1855. }