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.

1494 lines
47KB

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