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.

1578 lines
50KB

  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 "avformat.h"
  20. #include "avi.h"
  21. #ifdef CONFIG_ZLIB
  22. #include <zlib.h>
  23. #endif
  24. /*
  25. * First version by Francois Revol revol@free.fr
  26. *
  27. * Features and limitations:
  28. * - reads most of the QT files I have (at least the structure),
  29. * the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement.
  30. * FIXED, Francois Revol, 07/17/2002
  31. * - ffmpeg has nearly none of the usual QuickTime codecs,
  32. * although I succesfully dumped raw and mp3 audio tracks off .mov files.
  33. * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
  34. * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes
  35. * (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at
  36. * http://mpeg.telecomitalialab.com/faq.htm
  37. * - the code is quite ugly... maybe I won't do it recursive next time :-)
  38. *
  39. * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
  40. * when coding this :) (it's a writer anyway)
  41. *
  42. * Reference documents:
  43. * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
  44. * Apple:
  45. * http://developer.apple.com/techpubs/quicktime/qtdevdocs/QTFF/qtff.html
  46. * http://developer.apple.com/techpubs/quicktime/qtdevdocs/PDF/QTFileFormat.pdf
  47. * QuickTime is a trademark of Apple (AFAIK :))
  48. */
  49. //#define DEBUG
  50. #ifdef DEBUG
  51. #include <stdio.h>
  52. #include <fcntl.h>
  53. #endif
  54. /* allows chunk splitting - should work now... */
  55. /* in case you can't read a file, try commenting */
  56. #define MOV_SPLIT_CHUNKS
  57. /* some streams in QT (and in MP4 mostly) aren't either video nor audio */
  58. /* so we first list them as this, then clean up the list of streams we give back, */
  59. /* getting rid of these */
  60. #define CODEC_TYPE_MOV_OTHER (enum CodecType) 2
  61. static const CodecTag mov_video_tags[] = {
  62. /* { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
  63. /* { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, *//* Uncompressed RGB */
  64. /* { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, *//* Uncompressed YUV422 */
  65. /* { CODEC_ID_RAWVIDEO, MKTAG('A', 'V', 'U', 'I') }, *//* YUV with alpha-channel (AVID Uncompressed) */
  66. /* Graphics */
  67. /* Animation */
  68. /* Apple video */
  69. /* Kodak Photo CD */
  70. { CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
  71. { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
  72. { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
  73. { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
  74. { CODEC_ID_MJPEG, MKTAG('A', 'V', 'D', 'J') }, /* MJPEG with alpha-channel (AVID JFIF meridien compressed) */
  75. /* { CODEC_ID_MJPEG, MKTAG('A', 'V', 'R', 'n') }, *//* MJPEG with alpha-channel (AVID ABVB/Truevision NuVista) */
  76. /* { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
  77. /* Sorenson video */
  78. { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') }, /* Sorenson Video v1 */
  79. { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, /* Sorenson Video v1 */
  80. { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', 'i') }, /* Sorenson Video v1 (from QT specs)*/
  81. { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') }, /* Sorenson Video v3 */
  82. { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
  83. { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
  84. /* { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
  85. { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */
  86. { CODEC_ID_H263, MKTAG('s', '2', '6', '3') }, /* H263 ?? works */
  87. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
  88. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
  89. /* { CODEC_ID_DVVIDEO, MKTAG('A', 'V', 'd', 'v') }, *//* AVID dv */
  90. { CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') }, /* On2 VP3 */
  91. { CODEC_ID_NONE, 0 },
  92. };
  93. static const CodecTag mov_audio_tags[] = {
  94. /* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
  95. { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
  96. /* { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') },*/ /* 8 bits */
  97. { CODEC_ID_PCM_U8, MKTAG('r', 'a', 'w', ' ') }, /* 8 bits unsigned */
  98. { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */
  99. { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */
  100. { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */
  101. { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
  102. { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') }, /* Macintosh Audio Compression and Expansion 3:1 */
  103. { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') }, /* Macintosh Audio Compression and Expansion 6:1 */
  104. { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
  105. { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
  106. { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
  107. /* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
  108. /* MP4 tags */
  109. { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, /* MPEG 4 AAC or audio ? */
  110. /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
  111. { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') }, /* AMR-NB 3gp */
  112. { CODEC_ID_NONE, 0 },
  113. };
  114. /* the QuickTime file format is quite convoluted...
  115. * it has lots of index tables, each indexing something in another one...
  116. * Here we just use what is needed to read the chunks
  117. */
  118. typedef struct MOV_sample_to_chunk_tbl {
  119. long first;
  120. long count;
  121. long id;
  122. } MOV_sample_to_chunk_tbl;
  123. typedef struct {
  124. uint32_t type;
  125. int64_t offset;
  126. int64_t size; /* total size (excluding the size and type fields) */
  127. } MOV_atom_t;
  128. typedef struct {
  129. int seed;
  130. int flags;
  131. int size;
  132. void* clrs;
  133. } MOV_ctab_t;
  134. typedef struct {
  135. uint8_t version;
  136. uint32_t flags; // 24bit
  137. /* 0x03 ESDescrTag */
  138. uint16_t es_id;
  139. #define MP4ODescrTag 0x01
  140. #define MP4IODescrTag 0x02
  141. #define MP4ESDescrTag 0x03
  142. #define MP4DecConfigDescrTag 0x04
  143. #define MP4DecSpecificDescrTag 0x05
  144. #define MP4SLConfigDescrTag 0x06
  145. #define MP4ContentIdDescrTag 0x07
  146. #define MP4SupplContentIdDescrTag 0x08
  147. #define MP4IPIPtrDescrTag 0x09
  148. #define MP4IPMPPtrDescrTag 0x0A
  149. #define MP4IPMPDescrTag 0x0B
  150. #define MP4RegistrationDescrTag 0x0D
  151. #define MP4ESIDIncDescrTag 0x0E
  152. #define MP4ESIDRefDescrTag 0x0F
  153. #define MP4FileIODescrTag 0x10
  154. #define MP4FileODescrTag 0x11
  155. #define MP4ExtProfileLevelDescrTag 0x13
  156. #define MP4ExtDescrTagsStart 0x80
  157. #define MP4ExtDescrTagsEnd 0xFE
  158. uint8_t stream_priority;
  159. /* 0x04 DecConfigDescrTag */
  160. uint8_t object_type_id;
  161. uint8_t stream_type;
  162. /* XXX: really streamType is
  163. * only 6bit, followed by:
  164. * 1bit upStream
  165. * 1bit reserved
  166. */
  167. uint32_t buffer_size_db; // 24
  168. uint32_t max_bitrate;
  169. uint32_t avg_bitrate;
  170. /* 0x05 DecSpecificDescrTag */
  171. uint8_t decoder_cfg_len;
  172. uint8_t *decoder_cfg;
  173. /* 0x06 SLConfigDescrTag */
  174. uint8_t sl_config_len;
  175. uint8_t *sl_config;
  176. } MOV_esds_t;
  177. struct MOVParseTableEntry;
  178. typedef struct MOVStreamContext {
  179. int ffindex; /* the ffmpeg stream id */
  180. int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
  181. long next_chunk;
  182. long chunk_count;
  183. int64_t *chunk_offsets;
  184. long sample_to_chunk_sz;
  185. MOV_sample_to_chunk_tbl *sample_to_chunk;
  186. long sample_to_chunk_index;
  187. long sample_size;
  188. long sample_count;
  189. long *sample_sizes;
  190. int time_scale;
  191. long current_sample;
  192. long left_in_chunk; /* how many samples before next chunk */
  193. /* specific MPEG4 header which is added at the beginning of the stream */
  194. int header_len;
  195. uint8_t *header_data;
  196. MOV_esds_t esds;
  197. } MOVStreamContext;
  198. typedef struct MOVContext {
  199. 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) */
  200. AVFormatContext *fc;
  201. int time_scale;
  202. int duration; /* duration of the longest track */
  203. int found_moov; /* when both 'moov' and 'mdat' sections has been found */
  204. int found_mdat; /* we suppose we have enough data to read the file */
  205. int64_t mdat_size;
  206. int64_t mdat_offset;
  207. int total_streams;
  208. /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
  209. * but we need the info to be able to skip data from those streams in the 'mdat' section
  210. */
  211. MOVStreamContext *streams[MAX_STREAMS];
  212. int64_t next_chunk_offset;
  213. MOVStreamContext *partial; /* != 0 : there is still to read in the current chunk */
  214. int ctab_size;
  215. MOV_ctab_t **ctab; /* color tables */
  216. const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
  217. /* NOTE: for recursion save to/ restore from local variable! */
  218. } MOVContext;
  219. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  220. /* those functions parse an atom */
  221. /* return code:
  222. 1: found what I wanted, exit
  223. 0: continue to parse next atom
  224. -1: error occured, exit
  225. */
  226. typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
  227. /* links atom IDs to parse functions */
  228. typedef struct MOVParseTableEntry {
  229. uint32_t type;
  230. mov_parse_function func;
  231. } MOVParseTableEntry;
  232. #ifdef DEBUG
  233. /*
  234. * XXX: static sux, even more in a multithreaded environment...
  235. * Avoid them. This is here just to help debugging.
  236. */
  237. static int debug_indent = 0;
  238. void print_atom(const char *str, MOV_atom_t atom)
  239. {
  240. unsigned int tag, i;
  241. tag = (unsigned int) atom.type;
  242. i=debug_indent;
  243. if(tag == 0) tag = MKTAG('N', 'U', 'L', 'L');
  244. while(i--)
  245. printf("|");
  246. printf("parse:");
  247. printf(" %s: tag=%c%c%c%c offset=0x%x size=0x%x\n",
  248. str, tag & 0xff,
  249. (tag >> 8) & 0xff,
  250. (tag >> 16) & 0xff,
  251. (tag >> 24) & 0xff,
  252. (unsigned int)atom.offset,
  253. (unsigned int)atom.size);
  254. assert((unsigned int)atom.size < 0x7fffffff);// catching errors
  255. }
  256. #else
  257. #define print_atom(a,b)
  258. #endif
  259. static int mov_read_leaf(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  260. {
  261. print_atom("leaf", atom);
  262. if (atom.size>1)
  263. url_fskip(pb, atom.size);
  264. /* url_seek(pb, atom_offset+atom.size, SEEK_SET); */
  265. return 0;
  266. }
  267. static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  268. {
  269. int64_t total_size = 0;
  270. MOV_atom_t a;
  271. int i;
  272. int err = 0;
  273. #ifdef DEBUG
  274. print_atom("default", atom);
  275. debug_indent++;
  276. #endif
  277. a.offset = atom.offset;
  278. if (atom.size < 0)
  279. atom.size = 0x7fffffffffffffffLL;
  280. while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
  281. a.size = atom.size;
  282. a.type=0L;
  283. if(atom.size >= 8) {
  284. a.size = get_be32(pb);
  285. a.type = get_le32(pb);
  286. }
  287. total_size += 8;
  288. a.offset += 8;
  289. //printf("type: %08x %.4s sz: %Lx %Lx %Lx\n", type, (char*)&type, size, atom.size, total_size);
  290. if (a.size == 1) { /* 64 bit extended size */
  291. a.size = get_be64(pb) - 8;
  292. a.offset += 8;
  293. total_size += 8;
  294. }
  295. if (a.size == 0) {
  296. a.size = atom.size - total_size;
  297. if (a.size <= 8)
  298. break;
  299. }
  300. for (i = 0; c->parse_table[i].type != 0L
  301. && c->parse_table[i].type != a.type; i++)
  302. /* empty */;
  303. a.size -= 8;
  304. // printf(" i=%ld\n", i);
  305. if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
  306. // url_seek(pb, atom.offset+atom.size, SEEK_SET);
  307. #ifdef DEBUG
  308. print_atom("unknown", a);
  309. #endif
  310. url_fskip(pb, a.size);
  311. } else {
  312. #ifdef DEBUG
  313. //char b[5] = { type & 0xff, (type >> 8) & 0xff, (type >> 16) & 0xff, (type >> 24) & 0xff, 0 };
  314. //print_atom(b, type, offset, size);
  315. #endif
  316. err = (c->parse_table[i].func)(c, pb, a);
  317. }
  318. a.offset += a.size;
  319. total_size += a.size;
  320. }
  321. if (!err && total_size < atom.size && atom.size < 0x7ffff) {
  322. //printf("RESET %Ld %Ld err:%d\n", atom.size, total_size, err);
  323. url_fskip(pb, atom.size - total_size);
  324. }
  325. #ifdef DEBUG
  326. debug_indent--;
  327. #endif
  328. return err;
  329. }
  330. static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  331. {
  332. unsigned int len;
  333. MOV_ctab_t *t;
  334. //url_fskip(pb, atom.size); // for now
  335. c->ctab = av_realloc(c->ctab, ++c->ctab_size);
  336. t = c->ctab[c->ctab_size];
  337. t->seed = get_be32(pb);
  338. t->flags = get_be16(pb);
  339. t->size = get_be16(pb) + 1;
  340. len = 2 * t->size * 4;
  341. if (len > 0) {
  342. t->clrs = av_malloc(len); // 16bit A R G B
  343. if (t->clrs)
  344. get_buffer(pb, t->clrs, len);
  345. }
  346. return 0;
  347. }
  348. static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  349. {
  350. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  351. int len = 0;
  352. uint32_t type;
  353. uint32_t ctype;
  354. print_atom("hdlr", atom);
  355. get_byte(pb); /* version */
  356. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  357. /* component type */
  358. ctype = get_le32(pb);
  359. type = get_le32(pb); /* component subtype */
  360. #ifdef DEBUG
  361. printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
  362. printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
  363. #endif
  364. #ifdef DEBUG
  365. /* XXX: yeah this is ugly... */
  366. if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
  367. if(type == MKTAG('v', 'i', 'd', 'e'))
  368. puts("hdlr: vide");
  369. else if(type == MKTAG('s', 'o', 'u', 'n'))
  370. puts("hdlr: soun");
  371. } else if(ctype == 0) { /* MP4 */
  372. if(type == MKTAG('v', 'i', 'd', 'e'))
  373. puts("hdlr: vide");
  374. else if(type == MKTAG('s', 'o', 'u', 'n'))
  375. puts("hdlr: soun");
  376. else if(type == MKTAG('o', 'd', 's', 'm'))
  377. puts("hdlr: odsm");
  378. else if(type == MKTAG('s', 'd', 's', 'm'))
  379. puts("hdlr: sdsm");
  380. } else puts("hdlr: meta");
  381. #endif
  382. if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
  383. /* helps parsing the string hereafter... */
  384. c->mp4 = 0;
  385. if(type == MKTAG('v', 'i', 'd', 'e'))
  386. st->codec.codec_type = CODEC_TYPE_VIDEO;
  387. else if(type == MKTAG('s', 'o', 'u', 'n'))
  388. st->codec.codec_type = CODEC_TYPE_AUDIO;
  389. } else if(ctype == 0) { /* MP4 */
  390. /* helps parsing the string hereafter... */
  391. c->mp4 = 1;
  392. if(type == MKTAG('v', 'i', 'd', 'e'))
  393. st->codec.codec_type = CODEC_TYPE_VIDEO;
  394. else if(type == MKTAG('s', 'o', 'u', 'n'))
  395. st->codec.codec_type = CODEC_TYPE_AUDIO;
  396. }
  397. get_be32(pb); /* component manufacture */
  398. get_be32(pb); /* component flags */
  399. get_be32(pb); /* component flags mask */
  400. if(atom.size <= 24)
  401. return 0; /* nothing left to read */
  402. /* XXX: MP4 uses a C string, not a pascal one */
  403. /* component name */
  404. if(c->mp4) {
  405. /* .mp4: C string */
  406. while(get_byte(pb) && (++len < (atom.size - 24)));
  407. } else {
  408. /* .mov: PASCAL string */
  409. #ifdef DEBUG
  410. char* buf;
  411. #endif
  412. len = get_byte(pb);
  413. #ifdef DEBUG
  414. buf = (uint8_t*) av_malloc(len+1);
  415. if (buf) {
  416. get_buffer(pb, buf, len);
  417. buf[len] = '\0';
  418. printf("**buf='%s'\n", buf);
  419. av_free(buf);
  420. } else
  421. #endif
  422. url_fskip(pb, len);
  423. }
  424. return 0;
  425. }
  426. static int mov_mp4_read_descr_len(ByteIOContext *pb)
  427. {
  428. int len = 0;
  429. int count = 4;
  430. while (count--) {
  431. int c = get_byte(pb);
  432. len = (len << 7) | (c & 0x7f);
  433. if (!(c & 0x80))
  434. break;
  435. }
  436. return len;
  437. }
  438. static int mov_mp4_read_descr(ByteIOContext *pb, int *tag)
  439. {
  440. int len;
  441. *tag = get_byte(pb);
  442. len = mov_mp4_read_descr_len(pb);
  443. #ifdef DEBUG
  444. printf("MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
  445. #endif
  446. return len;
  447. }
  448. static inline unsigned int get_be24(ByteIOContext *s)
  449. {
  450. unsigned int val;
  451. val = get_byte(s) << 16;
  452. val |= get_byte(s) << 8;
  453. val |= get_byte(s);
  454. return val;
  455. }
  456. static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  457. {
  458. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  459. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  460. int64_t start_pos = url_ftell(pb);
  461. int tag, len;
  462. print_atom("esds", atom);
  463. /* Well, broken but suffisant for some MP4 streams */
  464. get_be32(pb); /* version + flags */
  465. len = mov_mp4_read_descr(pb, &tag);
  466. if (tag == MP4ESDescrTag) {
  467. get_be16(pb); /* ID */
  468. get_byte(pb); /* priority */
  469. } else
  470. get_be16(pb); /* ID */
  471. len = mov_mp4_read_descr(pb, &tag);
  472. if (tag == MP4DecConfigDescrTag) {
  473. sc->esds.object_type_id = get_byte(pb);
  474. sc->esds.stream_type = get_byte(pb);
  475. sc->esds.buffer_size_db = get_be24(pb);
  476. sc->esds.max_bitrate = get_be32(pb);
  477. sc->esds.avg_bitrate = get_be32(pb);
  478. len = mov_mp4_read_descr(pb, &tag);
  479. //printf("LEN %d TAG %d m:%d a:%d\n", len, tag, sc->esds.max_bitrate, sc->esds.avg_bitrate);
  480. if (tag == MP4DecSpecificDescrTag) {
  481. #ifdef DEBUG
  482. printf("Specific MPEG4 header len=%d\n", len);
  483. #endif
  484. st->codec.extradata = (uint8_t*) av_mallocz(len);
  485. if (st->codec.extradata) {
  486. get_buffer(pb, st->codec.extradata, len);
  487. st->codec.extradata_size = len;
  488. }
  489. }
  490. }
  491. /* in any case, skip garbage */
  492. url_fskip(pb, atom.size - ((url_ftell(pb) - start_pos)));
  493. return 0;
  494. }
  495. /* this atom contains actual media data */
  496. static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  497. {
  498. print_atom("mdat", atom);
  499. if(atom.size == 0) /* wrong one (MP4) */
  500. return 0;
  501. c->found_mdat=1;
  502. c->mdat_offset = atom.offset;
  503. c->mdat_size = atom.size;
  504. if(c->found_moov)
  505. return 1; /* found both, just go */
  506. url_fskip(pb, atom.size);
  507. return 0; /* now go for moov */
  508. }
  509. /* this atom should contain all header atoms */
  510. static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  511. {
  512. int err;
  513. print_atom("moov", atom);
  514. err = mov_read_default(c, pb, atom);
  515. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  516. /* so we don't parse the whole file if over a network */
  517. c->found_moov=1;
  518. if(c->found_mdat)
  519. return 1; /* found both, just go */
  520. return 0; /* now go for mdat */
  521. }
  522. static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  523. {
  524. print_atom("mdhd", atom);
  525. get_byte(pb); /* version */
  526. get_byte(pb); get_byte(pb);
  527. get_byte(pb); /* flags */
  528. get_be32(pb); /* creation time */
  529. get_be32(pb); /* modification time */
  530. c->streams[c->total_streams]->time_scale = get_be32(pb);
  531. #ifdef DEBUG
  532. printf("track[%i].time_scale = %i\n", c->fc->nb_streams-1, c->streams[c->total_streams]->time_scale); /* time scale */
  533. #endif
  534. get_be32(pb); /* duration */
  535. get_be16(pb); /* language */
  536. get_be16(pb); /* quality */
  537. return 0;
  538. }
  539. static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  540. {
  541. print_atom("mvhd", atom);
  542. get_byte(pb); /* version */
  543. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  544. get_be32(pb); /* creation time */
  545. get_be32(pb); /* modification time */
  546. c->time_scale = get_be32(pb); /* time scale */
  547. #ifdef DEBUG
  548. printf("time scale = %i\n", c->time_scale);
  549. #endif
  550. c->duration = get_be32(pb); /* duration */
  551. get_be32(pb); /* preferred scale */
  552. get_be16(pb); /* preferred volume */
  553. url_fskip(pb, 10); /* reserved */
  554. url_fskip(pb, 36); /* display matrix */
  555. get_be32(pb); /* preview time */
  556. get_be32(pb); /* preview duration */
  557. get_be32(pb); /* poster time */
  558. get_be32(pb); /* selection time */
  559. get_be32(pb); /* selection duration */
  560. get_be32(pb); /* current time */
  561. get_be32(pb); /* next track ID */
  562. return 0;
  563. }
  564. static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  565. {
  566. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  567. // currently SVQ3 decoder expect full STSD header - so let's fake it
  568. // this should be fixed and just SMI header should be passed
  569. av_free(st->codec.extradata);
  570. st->codec.extradata_size = 0x5a + atom.size;
  571. st->codec.extradata = (uint8_t*) av_mallocz(st->codec.extradata_size);
  572. if (st->codec.extradata) {
  573. int i;
  574. strcpy(st->codec.extradata, "SVQ3"); // fake
  575. get_buffer(pb, st->codec.extradata + 0x5a, atom.size);
  576. //printf("Reading SMI %Ld %s\n", atom.size, (char*)st->codec.extradata + 0x5a);
  577. } else
  578. url_fskip(pb, atom.size);
  579. return 0;
  580. }
  581. static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  582. {
  583. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  584. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  585. int entries, i;
  586. print_atom("stco", atom);
  587. get_byte(pb); /* version */
  588. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  589. entries = get_be32(pb);
  590. sc->chunk_count = entries;
  591. sc->chunk_offsets = (int64_t*) av_malloc(entries * sizeof(int64_t));
  592. if (!sc->chunk_offsets)
  593. return -1;
  594. if (atom.type == MKTAG('s', 't', 'c', 'o')) {
  595. for(i=0; i<entries; i++) {
  596. sc->chunk_offsets[i] = get_be32(pb);
  597. }
  598. } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
  599. for(i=0; i<entries; i++) {
  600. sc->chunk_offsets[i] = get_be64(pb);
  601. }
  602. } else
  603. return -1;
  604. #ifdef DEBUG
  605. /*
  606. for(i=0; i<entries; i++) {
  607. printf("chunk offset=0x%Lx\n", sc->chunk_offsets[i]);
  608. }
  609. */
  610. #endif
  611. return 0;
  612. }
  613. static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  614. {
  615. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  616. //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  617. int entries, frames_per_sample;
  618. uint32_t format;
  619. print_atom("stsd", atom);
  620. get_byte(pb); /* version */
  621. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  622. entries = get_be32(pb);
  623. while(entries--) { //Parsing Sample description table
  624. enum CodecID id;
  625. int size = get_be32(pb); /* size */
  626. format = get_le32(pb); /* data format */
  627. get_be32(pb); /* reserved */
  628. get_be16(pb); /* reserved */
  629. get_be16(pb); /* index */
  630. /* for MPEG4: set codec type by looking for it */
  631. id = codec_get_id(mov_video_tags, format);
  632. if (id >= 0) {
  633. AVCodec *codec;
  634. codec = avcodec_find_decoder(id);
  635. if (codec)
  636. st->codec.codec_type = codec->type;
  637. }
  638. #ifdef DEBUG
  639. printf("size=%d 4CC= %c%c%c%c codec_type=%d\n",
  640. size,
  641. (format >> 0) & 0xff,
  642. (format >> 8) & 0xff,
  643. (format >> 16) & 0xff,
  644. (format >> 24) & 0xff,
  645. st->codec.codec_type);
  646. #endif
  647. st->codec.codec_tag = format;
  648. if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
  649. MOV_atom_t a = { 0, 0, 0 };
  650. st->codec.codec_id = id;
  651. get_be16(pb); /* version */
  652. get_be16(pb); /* revision level */
  653. get_be32(pb); /* vendor */
  654. get_be32(pb); /* temporal quality */
  655. get_be32(pb); /* spacial quality */
  656. st->codec.width = get_be16(pb); /* width */
  657. st->codec.height = get_be16(pb); /* height */
  658. #if 1
  659. if (st->codec.codec_id == CODEC_ID_MPEG4) {
  660. /* in some MPEG4 the width/height are not correct, so
  661. we ignore this info */
  662. st->codec.width = 0;
  663. st->codec.height = 0;
  664. }
  665. #endif
  666. get_be32(pb); /* horiz resolution */
  667. get_be32(pb); /* vert resolution */
  668. get_be32(pb); /* data size, always 0 */
  669. frames_per_sample = get_be16(pb); /* frames per samples */
  670. #ifdef DEBUG
  671. printf("frames/samples = %d\n", frames_per_sample);
  672. #endif
  673. get_buffer(pb, (uint8_t *)st->codec.codec_name, 32); /* codec name */
  674. st->codec.bits_per_sample = get_be16(pb); /* depth */
  675. st->codec.color_table_id = get_be16(pb); /* colortable id */
  676. st->codec.frame_rate = 25;
  677. st->codec.frame_rate_base = 1;
  678. size -= (16+8*4+2+32+2*2);
  679. #if 0
  680. while (size >= 8) {
  681. MOV_atom_t a;
  682. int64_t start_pos;
  683. a.size = get_be32(pb);
  684. a.type = get_le32(pb);
  685. size -= 8;
  686. #ifdef DEBUG
  687. printf("VIDEO: atom_type=%c%c%c%c atom.size=%Ld size_left=%d\n",
  688. (a.type >> 0) & 0xff,
  689. (a.type >> 8) & 0xff,
  690. (a.type >> 16) & 0xff,
  691. (a.type >> 24) & 0xff,
  692. a.size, size);
  693. #endif
  694. start_pos = url_ftell(pb);
  695. switch(a.type) {
  696. case MKTAG('e', 's', 'd', 's'):
  697. {
  698. int tag, len;
  699. /* Well, broken but suffisant for some MP4 streams */
  700. get_be32(pb); /* version + flags */
  701. len = mov_mp4_read_descr(pb, &tag);
  702. if (tag == 0x03) {
  703. /* MP4ESDescrTag */
  704. get_be16(pb); /* ID */
  705. get_byte(pb); /* priority */
  706. len = mov_mp4_read_descr(pb, &tag);
  707. if (tag != 0x04)
  708. goto fail;
  709. /* MP4DecConfigDescrTag */
  710. get_byte(pb); /* objectTypeId */
  711. get_be32(pb); /* streamType + buffer size */
  712. get_be32(pb); /* max bit rate */
  713. get_be32(pb); /* avg bit rate */
  714. len = mov_mp4_read_descr(pb, &tag);
  715. if (tag != 0x05)
  716. goto fail;
  717. /* MP4DecSpecificDescrTag */
  718. #ifdef DEBUG
  719. printf("Specific MPEG4 header len=%d\n", len);
  720. #endif
  721. sc->header_data = av_mallocz(len);
  722. if (sc->header_data) {
  723. get_buffer(pb, sc->header_data, len);
  724. sc->header_len = len;
  725. }
  726. }
  727. /* in any case, skip garbage */
  728. }
  729. break;
  730. default:
  731. break;
  732. }
  733. fail:
  734. printf("ATOMENEWSIZE %Ld %d\n", atom.size, url_ftell(pb) - start_pos);
  735. if (atom.size > 8) {
  736. url_fskip(pb, (atom.size - 8) -
  737. ((url_ftell(pb) - start_pos)));
  738. size -= atom.size - 8;
  739. }
  740. }
  741. if (size > 0) {
  742. /* unknown extension */
  743. url_fskip(pb, size);
  744. }
  745. #else
  746. a.size = size;
  747. mov_read_default(c, pb, a);
  748. #endif
  749. } else {
  750. st->codec.codec_id = codec_get_id(mov_audio_tags, format);
  751. if(st->codec.codec_id==CODEC_ID_AMR_NB) //from TS26.244
  752. {
  753. #ifdef DEBUG
  754. printf("AMR-NB audio identified!!\n");
  755. #endif
  756. get_be32(pb);get_be32(pb); //Reserved_8
  757. get_be16(pb);//Reserved_2
  758. get_be16(pb);//Reserved_2
  759. get_be32(pb);//Reserved_4
  760. get_be16(pb);//TimeScale
  761. get_be16(pb);//Reserved_2
  762. //AMRSpecificBox.(10 bytes)
  763. #ifdef DEBUG
  764. int damr_size=
  765. #endif
  766. get_be32(pb); //size
  767. #ifdef DEBUG
  768. int damr_type=
  769. #endif
  770. get_be32(pb); //type=='damr'
  771. #ifdef DEBUG
  772. int damr_vendor=
  773. #endif
  774. get_be32(pb); //vendor
  775. get_byte(pb); //decoder version
  776. get_be16(pb); //mode_set
  777. get_byte(pb); //mode_change_period
  778. get_byte(pb); //frames_per_sample
  779. #ifdef DEBUG
  780. printf("Audio: damr_type=%c%c%c%c damr_size=%Ld damr_vendor=%c%c%c%c\n",
  781. (damr_type >> 24) & 0xff,
  782. (damr_type >> 16) & 0xff,
  783. (damr_type >> 8) & 0xff,
  784. (damr_type >> 0) & 0xff,
  785. damr_size,
  786. (damr_vendor >> 24) & 0xff,
  787. (damr_vendor >> 16) & 0xff,
  788. (damr_vendor >> 8) & 0xff,
  789. (damr_vendor >> 0) & 0xff
  790. );
  791. #endif
  792. st->time_length=0;//Not possible to get from this info, must count number of AMR frames
  793. st->codec.sample_rate=8000;
  794. st->codec.channels=1;
  795. st->codec.bits_per_sample=16;
  796. st->codec.bit_rate=0; /*It is not possible to tell this before we have
  797. an audio frame and even then every frame can be different*/
  798. }
  799. else
  800. {
  801. get_be16(pb); /* version */
  802. get_be16(pb); /* revision level */
  803. get_be32(pb); /* vendor */
  804. st->codec.channels = get_be16(pb); /* channel count */
  805. st->codec.bits_per_sample = get_be16(pb); /* sample size */
  806. /* handle specific s8 codec */
  807. get_be16(pb); /* compression id = 0*/
  808. get_be16(pb); /* packet size = 0 */
  809. st->codec.sample_rate = ((get_be32(pb) >> 16));
  810. //printf("CODECID %d %d %.4s\n", st->codec.codec_id, CODEC_ID_PCM_S16BE, (char*)&format);
  811. switch (st->codec.codec_id) {
  812. case CODEC_ID_PCM_S16BE:
  813. if (st->codec.bits_per_sample == 8)
  814. st->codec.codec_id = CODEC_ID_PCM_S8;
  815. /* fall */
  816. case CODEC_ID_PCM_U8:
  817. st->codec.bit_rate = st->codec.sample_rate * 8;
  818. break;
  819. default:
  820. ;
  821. }
  822. get_be32(pb); /* samples per packet */
  823. get_be32(pb); /* bytes per packet */
  824. get_be32(pb); /* bytes per frame */
  825. get_be32(pb); /* bytes per sample */
  826. {
  827. MOV_atom_t a = { format, url_ftell(pb), size - (16 + 20 + 16 + 8) };
  828. mov_read_default(c, pb, a);
  829. }
  830. }
  831. }
  832. }
  833. return 0;
  834. }
  835. static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  836. {
  837. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  838. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  839. int entries, i;
  840. print_atom("stsc", atom);
  841. get_byte(pb); /* version */
  842. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  843. entries = get_be32(pb);
  844. #ifdef DEBUG
  845. printf("track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  846. #endif
  847. sc->sample_to_chunk_sz = entries;
  848. sc->sample_to_chunk = (MOV_sample_to_chunk_tbl*) av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
  849. if (!sc->sample_to_chunk)
  850. return -1;
  851. for(i=0; i<entries; i++) {
  852. sc->sample_to_chunk[i].first = get_be32(pb);
  853. sc->sample_to_chunk[i].count = get_be32(pb);
  854. sc->sample_to_chunk[i].id = get_be32(pb);
  855. #ifdef DEBUG
  856. /* printf("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); */
  857. #endif
  858. }
  859. return 0;
  860. }
  861. static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  862. {
  863. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  864. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  865. int entries, i;
  866. print_atom("stsz", atom);
  867. get_byte(pb); /* version */
  868. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  869. sc->sample_size = get_be32(pb);
  870. entries = get_be32(pb);
  871. sc->sample_count = entries;
  872. #ifdef DEBUG
  873. printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
  874. #endif
  875. if(sc->sample_size)
  876. return 0; /* there isn't any table following */
  877. sc->sample_sizes = (long*) av_malloc(entries * sizeof(long));
  878. if (!sc->sample_sizes)
  879. return -1;
  880. for(i=0; i<entries; i++) {
  881. sc->sample_sizes[i] = get_be32(pb);
  882. #ifdef DEBUG
  883. /* printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
  884. #endif
  885. }
  886. return 0;
  887. }
  888. static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  889. {
  890. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  891. //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  892. int entries, i;
  893. int duration=0;
  894. int total_sample_count=0;
  895. print_atom("stts", atom);
  896. get_byte(pb); /* version */
  897. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  898. entries = get_be32(pb);
  899. #ifdef DEBUG
  900. printf("track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  901. #endif
  902. for(i=0; i<entries; i++) {
  903. int sample_duration;
  904. int sample_count;
  905. sample_count=get_be32(pb);
  906. sample_duration = get_be32(pb);
  907. #ifdef DEBUG
  908. printf("sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  909. #endif
  910. duration+=sample_duration*sample_count;
  911. total_sample_count+=sample_count;
  912. #if 0 //We calculate an average instead, needed by .mp4-files created with nec e606 3g phone
  913. if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
  914. st->codec.frame_rate_base = sample_duration ? sample_duration : 1;
  915. st->codec.frame_rate = c->streams[c->total_streams]->time_scale;
  916. #ifdef DEBUG
  917. printf("VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
  918. #endif
  919. }
  920. #endif
  921. }
  922. if(st->codec.codec_type==CODEC_TYPE_VIDEO)
  923. {
  924. //Only support constant frame rate. But lets calculate the average. Needed by .mp4-files created with nec e606 3g phone.
  925. //Hmm, lets set base to 1
  926. st->codec.frame_rate_base=1;
  927. st->codec.frame_rate = st->codec.frame_rate_base * c->streams[c->total_streams]->time_scale * total_sample_count / duration;
  928. #ifdef DEBUG
  929. printf("VIDEO FRAME RATE average= %i (tot sample count= %i ,tot dur= %i)\n", st->codec.frame_rate,total_sample_count,duration);
  930. #endif
  931. }
  932. return 0;
  933. }
  934. static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  935. {
  936. AVStream *st;
  937. MOVStreamContext *sc;
  938. print_atom("trak", atom);
  939. st = av_new_stream(c->fc, c->fc->nb_streams);
  940. if (!st) return -2;
  941. sc = (MOVStreamContext*) av_mallocz(sizeof(MOVStreamContext));
  942. if (!sc) {
  943. av_free(st);
  944. return -1;
  945. }
  946. sc->sample_to_chunk_index = -1;
  947. st->priv_data = sc;
  948. st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
  949. st->time_length = (c->duration * 1000) / c->time_scale; // time in miliseconds
  950. c->streams[c->fc->nb_streams-1] = sc;
  951. return mov_read_default(c, pb, atom);
  952. }
  953. static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  954. {
  955. AVStream *st;
  956. print_atom("tkhd", atom);
  957. st = c->fc->streams[c->fc->nb_streams-1];
  958. get_byte(pb); /* version */
  959. get_byte(pb); get_byte(pb);
  960. get_byte(pb); /* flags */
  961. /*
  962. MOV_TRACK_ENABLED 0x0001
  963. MOV_TRACK_IN_MOVIE 0x0002
  964. MOV_TRACK_IN_PREVIEW 0x0004
  965. MOV_TRACK_IN_POSTER 0x0008
  966. */
  967. get_be32(pb); /* creation time */
  968. get_be32(pb); /* modification time */
  969. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  970. get_be32(pb); /* reserved */
  971. st->time_length = get_be32(pb) * 1000 / c->time_scale; /* duration */
  972. get_be32(pb); /* reserved */
  973. get_be32(pb); /* reserved */
  974. get_be16(pb); /* layer */
  975. get_be16(pb); /* alternate group */
  976. get_be16(pb); /* volume */
  977. get_be16(pb); /* reserved */
  978. url_fskip(pb, 36); /* display matrix */
  979. /* those are fixed-point */
  980. st->codec.width = get_be32(pb) >> 16; /* track width */
  981. st->codec.height = get_be32(pb) >> 16; /* track height */
  982. return 0;
  983. }
  984. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  985. /* like the files created with Adobe Premiere 5.0, for samples see */
  986. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  987. static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  988. {
  989. int err;
  990. #ifdef DEBUG
  991. print_atom("wide", atom);
  992. debug_indent++;
  993. #endif
  994. if (atom.size < 8)
  995. return 0; /* continue */
  996. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  997. url_fskip(pb, atom.size - 4);
  998. return 0;
  999. }
  1000. atom.type = get_le32(pb);
  1001. atom.offset += 8;
  1002. atom.size -= 8;
  1003. if (atom.type != MKTAG('m', 'd', 'a', 't')) {
  1004. url_fskip(pb, atom.size);
  1005. return 0;
  1006. }
  1007. err = mov_read_mdat(c, pb, atom);
  1008. #ifdef DEBUG
  1009. debug_indent--;
  1010. #endif
  1011. return err;
  1012. }
  1013. #ifdef CONFIG_ZLIB
  1014. static int null_read_packet(void *opaque, uint8_t *buf, int buf_size)
  1015. {
  1016. return -1;
  1017. }
  1018. static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1019. {
  1020. ByteIOContext ctx;
  1021. uint8_t *cmov_data;
  1022. uint8_t *moov_data; /* uncompressed data */
  1023. long cmov_len, moov_len;
  1024. int ret;
  1025. print_atom("cmov", atom);
  1026. get_be32(pb); /* dcom atom */
  1027. if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
  1028. return -1;
  1029. if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
  1030. dprintf("unknown compression for cmov atom !");
  1031. return -1;
  1032. }
  1033. get_be32(pb); /* cmvd atom */
  1034. if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
  1035. return -1;
  1036. moov_len = get_be32(pb); /* uncompressed size */
  1037. cmov_len = atom.size - 6 * 4;
  1038. cmov_data = (uint8_t *) av_malloc(cmov_len);
  1039. if (!cmov_data)
  1040. return -1;
  1041. moov_data = (uint8_t *) av_malloc(moov_len);
  1042. if (!moov_data) {
  1043. av_free(cmov_data);
  1044. return -1;
  1045. }
  1046. get_buffer(pb, cmov_data, cmov_len);
  1047. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1048. return -1;
  1049. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
  1050. return -1;
  1051. ctx.buf_end = ctx.buffer + moov_len;
  1052. atom.type = MKTAG( 'm', 'o', 'o', 'v' );
  1053. atom.offset = 0;
  1054. atom.size = moov_len;
  1055. #ifdef DEBUG
  1056. { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
  1057. #endif
  1058. ret = mov_read_default(c, &ctx, atom);
  1059. av_free(moov_data);
  1060. av_free(cmov_data);
  1061. return ret;
  1062. }
  1063. #endif
  1064. static const MOVParseTableEntry mov_default_parse_table[] = {
  1065. /* mp4 atoms */
  1066. { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
  1067. { MKTAG( 'c', 'p', 'r', 't' ), mov_read_default },
  1068. { MKTAG( 'c', 'r', 'h', 'd' ), mov_read_default },
  1069. { MKTAG( 'c', 't', 't', 's' ), mov_read_leaf }, /* composition time to sample */
  1070. { MKTAG( 'd', 'i', 'n', 'f' ), mov_read_default }, /* data information */
  1071. { MKTAG( 'd', 'p', 'n', 'd' ), mov_read_leaf },
  1072. { MKTAG( 'd', 'r', 'e', 'f' ), mov_read_leaf },
  1073. { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
  1074. { MKTAG( 'e', 'l', 's', 't' ), mov_read_leaf },
  1075. { MKTAG( 'f', 'r', 'e', 'e' ), mov_read_leaf },
  1076. { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
  1077. { MKTAG( 'h', 'i', 'n', 't' ), mov_read_leaf },
  1078. { MKTAG( 'h', 'm', 'h', 'd' ), mov_read_leaf },
  1079. { MKTAG( 'i', 'o', 'd', 's' ), mov_read_leaf },
  1080. { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
  1081. { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
  1082. { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
  1083. { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
  1084. { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
  1085. { MKTAG( 'm', 'p', '4', 'a' ), mov_read_default },
  1086. { MKTAG( 'm', 'p', '4', 's' ), mov_read_default },
  1087. { MKTAG( 'm', 'p', '4', 'v' ), mov_read_default },
  1088. { MKTAG( 'm', 'p', 'o', 'd' ), mov_read_leaf },
  1089. { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
  1090. { MKTAG( 'n', 'm', 'h', 'd' ), mov_read_leaf },
  1091. { MKTAG( 'o', 'd', 'h', 'd' ), mov_read_default },
  1092. { MKTAG( 's', 'd', 'h', 'd' ), mov_read_default },
  1093. { MKTAG( 's', 'k', 'i', 'p' ), mov_read_default },
  1094. { MKTAG( 's', 'm', 'h', 'd' ), mov_read_leaf }, /* sound media info header */
  1095. { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorrenson extension ??? */
  1096. { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
  1097. { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
  1098. { MKTAG( 's', 't', 'd', 'p' ), mov_read_default },
  1099. { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
  1100. { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
  1101. { MKTAG( 's', 't', 's', 'h' ), mov_read_default },
  1102. { MKTAG( 's', 't', 's', 's' ), mov_read_leaf }, /* sync sample */
  1103. { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
  1104. { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
  1105. { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
  1106. { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
  1107. { MKTAG( 't', 'r', 'e', 'f' ), mov_read_default }, /* not really */
  1108. { MKTAG( 'u', 'd', 't', 'a' ), mov_read_leaf },
  1109. { MKTAG( 'u', 'r', 'l', ' ' ), mov_read_leaf },
  1110. { MKTAG( 'u', 'r', 'n', ' ' ), mov_read_leaf },
  1111. { MKTAG( 'u', 'u', 'i', 'd' ), mov_read_default },
  1112. { MKTAG( 'v', 'm', 'h', 'd' ), mov_read_leaf }, /* video media info header */
  1113. { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_default },
  1114. /* extra mp4 */
  1115. { MKTAG( 'M', 'D', 'E', 'S' ), mov_read_leaf },
  1116. /* QT atoms */
  1117. { MKTAG( 'c', 'h', 'a', 'p' ), mov_read_leaf },
  1118. { MKTAG( 'c', 'l', 'i', 'p' ), mov_read_default },
  1119. { MKTAG( 'c', 'r', 'g', 'n' ), mov_read_leaf },
  1120. { MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab },
  1121. { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
  1122. { MKTAG( 'k', 'm', 'a', 't' ), mov_read_leaf },
  1123. { MKTAG( 'm', 'a', 't', 't' ), mov_read_default },
  1124. { MKTAG( 'r', 'd', 'r', 'f' ), mov_read_leaf },
  1125. { MKTAG( 'r', 'm', 'd', 'a' ), mov_read_default },
  1126. { MKTAG( 'r', 'm', 'd', 'r' ), mov_read_leaf },
  1127. { MKTAG( 'r', 'm', 'r', 'a' ), mov_read_default },
  1128. { MKTAG( 's', 'c', 'p', 't' ), mov_read_leaf },
  1129. { MKTAG( 's', 's', 'r', 'c' ), mov_read_leaf },
  1130. { MKTAG( 's', 'y', 'n', 'c' ), mov_read_leaf },
  1131. { MKTAG( 't', 'c', 'm', 'd' ), mov_read_leaf },
  1132. { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
  1133. //{ MKTAG( 'r', 'm', 'q', 'u' ), mov_read_leaf },
  1134. #ifdef CONFIG_ZLIB
  1135. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
  1136. #else
  1137. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_leaf },
  1138. #endif
  1139. { 0L, mov_read_leaf }
  1140. };
  1141. static void mov_free_stream_context(MOVStreamContext *sc)
  1142. {
  1143. if(sc) {
  1144. av_free(sc->chunk_offsets);
  1145. av_free(sc->sample_to_chunk);
  1146. av_free(sc->sample_sizes);
  1147. av_free(sc->header_data);
  1148. av_free(sc);
  1149. }
  1150. }
  1151. static inline uint32_t mov_to_tag(uint8_t *buf)
  1152. {
  1153. return MKTAG(buf[0], buf[1], buf[2], buf[3]);
  1154. }
  1155. static inline uint32_t to_be32(uint8_t *buf)
  1156. {
  1157. return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  1158. }
  1159. /* XXX: is it sufficient ? */
  1160. static int mov_probe(AVProbeData *p)
  1161. {
  1162. unsigned int offset;
  1163. uint32_t tag;
  1164. /* check file header */
  1165. if (p->buf_size <= 12)
  1166. return 0;
  1167. offset = 0;
  1168. for(;;) {
  1169. /* ignore invalid offset */
  1170. if ((offset + 8) > (unsigned int)p->buf_size)
  1171. return 0;
  1172. tag = mov_to_tag(p->buf + offset + 4);
  1173. switch(tag) {
  1174. case MKTAG( 'm', 'o', 'o', 'v' ):
  1175. case MKTAG( 'w', 'i', 'd', 'e' ):
  1176. case MKTAG( 'f', 'r', 'e', 'e' ):
  1177. case MKTAG( 'm', 'd', 'a', 't' ):
  1178. case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
  1179. return AVPROBE_SCORE_MAX;
  1180. case MKTAG( 'f', 't', 'y', 'p' ):
  1181. case MKTAG( 's', 'k', 'i', 'p' ):
  1182. offset = to_be32(p->buf) + offset;
  1183. break;
  1184. default:
  1185. /* unrecognized tag */
  1186. return 0;
  1187. }
  1188. }
  1189. return 0;
  1190. }
  1191. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1192. {
  1193. MOVContext *mov = (MOVContext *) s->priv_data;
  1194. ByteIOContext *pb = &s->pb;
  1195. int i, j, nb, err;
  1196. MOV_atom_t atom = { 0, 0, 0 };
  1197. mov->fc = s;
  1198. mov->parse_table = mov_default_parse_table;
  1199. #if 0
  1200. /* XXX: I think we should auto detect */
  1201. if(s->iformat->name[1] == 'p')
  1202. mov->mp4 = 1;
  1203. #endif
  1204. if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  1205. atom.size = url_filesize(url_fileno(pb));
  1206. else
  1207. atom.size = 0x7FFFFFFFFFFFFFFFLL;
  1208. #ifdef DEBUG
  1209. printf("filesz=%Ld\n", atom.size);
  1210. #endif
  1211. /* check MOV header */
  1212. err = mov_read_default(mov, pb, atom);
  1213. if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
  1214. fprintf(stderr, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%Ld\n",
  1215. err, mov->found_moov, mov->found_mdat, url_ftell(pb));
  1216. return -1;
  1217. }
  1218. #ifdef DEBUG
  1219. printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
  1220. #endif
  1221. /* some cleanup : make sure we are on the mdat atom */
  1222. if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
  1223. url_fseek(pb, mov->mdat_offset, SEEK_SET);
  1224. mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
  1225. #ifdef DEBUG
  1226. printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
  1227. #endif
  1228. #ifdef DEBUG
  1229. printf("streams= %d\n", s->nb_streams);
  1230. #endif
  1231. mov->total_streams = nb = s->nb_streams;
  1232. #if 1
  1233. for(i=0; i<s->nb_streams;) {
  1234. if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
  1235. av_free(s->streams[i]);
  1236. for(j=i+1; j<s->nb_streams; j++)
  1237. s->streams[j-1] = s->streams[j];
  1238. s->nb_streams--;
  1239. } else
  1240. i++;
  1241. }
  1242. for(i=0; i<s->nb_streams;i++) {
  1243. MOVStreamContext *sc;
  1244. sc = (MOVStreamContext *)s->streams[i]->priv_data;
  1245. sc->ffindex = i;
  1246. sc->is_ff_stream = 1;
  1247. }
  1248. #endif
  1249. #ifdef DEBUG
  1250. printf("real streams= %d\n", s->nb_streams);
  1251. #endif
  1252. return 0;
  1253. }
  1254. /* Yes, this is ugly... I didn't write the specs of QT :p */
  1255. /* XXX:remove useless commented code sometime */
  1256. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  1257. {
  1258. MOVContext *mov = (MOVContext *) s->priv_data;
  1259. MOVStreamContext *sc;
  1260. int64_t offset = 0x0FFFFFFFFFFFFFFFLL;
  1261. int i;
  1262. int size;
  1263. size = 0x0FFFFFFF;
  1264. #ifdef MOV_SPLIT_CHUNKS
  1265. if (mov->partial) {
  1266. int idx;
  1267. sc = mov->partial;
  1268. idx = sc->sample_to_chunk_index;
  1269. if (idx < 0) return 0;
  1270. size = sc->sample_sizes[sc->current_sample];
  1271. sc->current_sample++;
  1272. sc->left_in_chunk--;
  1273. if (sc->left_in_chunk <= 0)
  1274. mov->partial = 0;
  1275. offset = mov->next_chunk_offset;
  1276. /* extract the sample */
  1277. goto readchunk;
  1278. }
  1279. #endif
  1280. again:
  1281. sc = 0;
  1282. for(i=0; i<mov->total_streams; i++) {
  1283. MOVStreamContext *msc = mov->streams[i];
  1284. //printf("MOCHUNK %ld %d %p pos:%Ld\n", mov->streams[i]->next_chunk, mov->total_streams, mov->streams[i], url_ftell(&s->pb));
  1285. if ((msc->next_chunk < msc->chunk_count) && msc->next_chunk >= 0
  1286. && (msc->chunk_offsets[msc->next_chunk] < offset)) {
  1287. sc = msc;
  1288. offset = msc->chunk_offsets[msc->next_chunk];
  1289. //printf("SELETED %Ld i:%d\n", offset, i);
  1290. }
  1291. }
  1292. if (!sc || offset==0x0FFFFFFFFFFFFFFFLL)
  1293. return -1;
  1294. sc->next_chunk++;
  1295. if(mov->next_chunk_offset < offset) { /* some meta data */
  1296. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1297. mov->next_chunk_offset = offset;
  1298. }
  1299. //printf("chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
  1300. if(!sc->is_ff_stream) {
  1301. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1302. mov->next_chunk_offset = offset;
  1303. offset = 0x0FFFFFFFFFFFFFFFLL;
  1304. goto again;
  1305. }
  1306. /* now get the chunk size... */
  1307. for(i=0; i<mov->total_streams; i++) {
  1308. MOVStreamContext *msc = mov->streams[i];
  1309. if ((msc->next_chunk < msc->chunk_count)
  1310. && ((msc->chunk_offsets[msc->next_chunk] - offset) < size))
  1311. size = msc->chunk_offsets[msc->next_chunk] - offset;
  1312. }
  1313. #ifdef MOV_SPLIT_CHUNKS
  1314. /* split chunks into samples */
  1315. if (sc->sample_size == 0) {
  1316. int idx = sc->sample_to_chunk_index;
  1317. if ((idx + 1 < sc->sample_to_chunk_sz)
  1318. && (sc->next_chunk >= sc->sample_to_chunk[idx + 1].first))
  1319. idx++;
  1320. sc->sample_to_chunk_index = idx;
  1321. if (idx >= 0 && sc->sample_to_chunk[idx].count != 1) {
  1322. mov->partial = sc;
  1323. /* we'll have to get those samples before next chunk */
  1324. sc->left_in_chunk = sc->sample_to_chunk[idx].count - 1;
  1325. size = sc->sample_sizes[sc->current_sample];
  1326. }
  1327. sc->current_sample++;
  1328. }
  1329. #endif
  1330. readchunk:
  1331. //printf("chunk: [%i] %lli -> %lli (%i)\n", st_id, offset, offset + size, size);
  1332. if(size == 0x0FFFFFFF)
  1333. size = mov->mdat_size + mov->mdat_offset - offset;
  1334. if(size < 0)
  1335. return -1;
  1336. if(size == 0)
  1337. return -1;
  1338. url_fseek(&s->pb, offset, SEEK_SET);
  1339. //printf("READCHUNK hlen: %d %d off: %Ld pos:%Ld\n", size, sc->header_len, offset, url_ftell(&s->pb));
  1340. if (sc->header_len > 0) {
  1341. av_new_packet(pkt, size + sc->header_len);
  1342. memcpy(pkt->data, sc->header_data, sc->header_len);
  1343. get_buffer(&s->pb, pkt->data + sc->header_len, size);
  1344. /* free header */
  1345. av_freep(&sc->header_data);
  1346. sc->header_len = 0;
  1347. } else {
  1348. av_new_packet(pkt, size);
  1349. get_buffer(&s->pb, pkt->data, pkt->size);
  1350. }
  1351. pkt->stream_index = sc->ffindex;
  1352. #ifdef DEBUG
  1353. /*
  1354. printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
  1355. for(i=0; i<8; i++)
  1356. printf("%02x ", pkt->data[i]);
  1357. for(i=0; i<8; i++)
  1358. printf("%c ", (pkt->data[i]) & 0x7F);
  1359. puts("");
  1360. */
  1361. #endif
  1362. mov->next_chunk_offset = offset + size;
  1363. return 0;
  1364. }
  1365. static int mov_read_close(AVFormatContext *s)
  1366. {
  1367. int i;
  1368. MOVContext *mov = (MOVContext *) s->priv_data;
  1369. for(i=0; i<mov->total_streams; i++)
  1370. mov_free_stream_context(mov->streams[i]);
  1371. for(i=0; i<s->nb_streams; i++)
  1372. av_freep(&s->streams[i]);
  1373. /* free color tabs */
  1374. for(i=0; i<mov->ctab_size; i++)
  1375. av_freep(&mov->ctab[i]);
  1376. av_freep(&mov->ctab);
  1377. return 0;
  1378. }
  1379. static AVInputFormat mov_iformat = {
  1380. "mov",
  1381. "QuickTime/MPEG4 format",
  1382. sizeof(MOVContext),
  1383. mov_probe,
  1384. mov_read_header,
  1385. mov_read_packet,
  1386. mov_read_close,
  1387. };
  1388. int mov_init(void)
  1389. {
  1390. av_register_input_format(&mov_iformat);
  1391. return 0;
  1392. }