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.

1348 lines
44KB

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