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.

1498 lines
48KB

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