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.

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