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.

952 lines
31KB

  1. /*
  2. * MOV decoder.
  3. * Copyright (c) 2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include "avformat.h"
  20. #include "avi.h"
  21. /*
  22. * First version by Francois Revol revol@free.fr
  23. *
  24. * Features and limitations:
  25. * - reads most of the QT files I have (at least the structure),
  26. * the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement.
  27. * - ffmpeg has nearly none of the usual QuickTime codecs,
  28. * although I succesfully dumped raw and mp3 audio tracks off .mov files.
  29. * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
  30. * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes
  31. * (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at
  32. * http://mpeg.telecomitalialab.com/faq.htm
  33. * - the code is quite ugly... maybe I won't do it recursive next time :-)
  34. *
  35. * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
  36. * when coding this :) (it's a writer anyway)
  37. *
  38. * Reference documents:
  39. * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
  40. * Apple:
  41. * http://developer.apple.com/techpubs/quicktime/qtdevdocs/QTFF/qtff.html
  42. * http://developer.apple.com/techpubs/quicktime/qtdevdocs/PDF/QTFileFormat.pdf
  43. * QuickTime is a trademark of Apple (AFAIK :))
  44. */
  45. //#define DEBUG
  46. #ifdef DEBUG
  47. /*
  48. * XXX: static sux, even more in a multithreaded environment...
  49. * Avoid them. This is here just to help debugging.
  50. */
  51. static int debug_indent = 0;
  52. void print_atom(const char *str, UINT32 type, UINT64 offset, UINT64 size)
  53. {
  54. unsigned int tag, i;
  55. tag = (unsigned int) type;
  56. i=debug_indent;
  57. if(tag == 0) tag = MKTAG('N', 'U', 'L', 'L');
  58. while(i--)
  59. printf("|");
  60. printf("parse:");
  61. printf(" %s: tag=%c%c%c%c offset=%d size=0x%x\n",
  62. str, tag & 0xff,
  63. (tag >> 8) & 0xff,
  64. (tag >> 16) & 0xff,
  65. (tag >> 24) & 0xff,
  66. (unsigned int)offset,
  67. (unsigned int)size);
  68. }
  69. #endif
  70. /* some streams in QT (and in MP4 mostly) aren't either video nor audio */
  71. /* so we first list them as this, then clean up the list of streams we give back, */
  72. /* getting rid of these */
  73. #define CODEC_TYPE_MOV_OTHER 2
  74. static const CodecTag mov_video_tags[] = {
  75. /* { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
  76. /* { CODEC_ID_JPEG, MKTAG('j', 'p', 'e', 'g') }, *//* JPEG */
  77. { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, /* Uncompressed RGB */
  78. { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, /* Uncompressed YUV422 */
  79. /* Graphics */
  80. /* Animation */
  81. /* Apple video */
  82. /* Kodak Photo CD */
  83. /* { CODEC_ID_JPEG, MKTAG('j', 'p', 'e', 'g') }, *//* JPEG ? */
  84. { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
  85. { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format A) */
  86. { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
  87. /* { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
  88. /* Sorenson video */
  89. { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') }, /* OpenDiVX *//* yeah ! */
  90. { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
  91. /* { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
  92. { 0, 0 },
  93. };
  94. static const CodecTag mov_audio_tags[] = {
  95. /* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
  96. { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
  97. { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') }, /* 8 bits */
  98. { CODEC_ID_PCM_U8, 0x20776172 }, /* 8 bits unsigned */
  99. { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */
  100. { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */
  101. { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */
  102. /* { CODEC_ID_, MKTAG('i', 'm', 'a', '4') }, *//* IMA-4 */
  103. { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
  104. { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
  105. { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
  106. /* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
  107. /* MP4 tags */
  108. /* { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, *//* MPEG 4 AAC or audio ? */
  109. /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
  110. { 0, 0 },
  111. };
  112. /* the QuickTime file format is quite convoluted...
  113. * it has lots of index tables, each indexing something in another one...
  114. * Here we just use what is needed to read the chunks
  115. */
  116. typedef struct MOV_sample_to_chunk_tbl {
  117. long first;
  118. long count;
  119. long id;
  120. } MOV_sample_to_chunk_tbl;
  121. typedef struct MOVStreamContext {
  122. int ffindex; /* the ffmpeg stream id */
  123. int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
  124. long next_chunk;
  125. long chunk_count;
  126. INT64 *chunk_offsets;
  127. long sample_to_chunk_sz;
  128. MOV_sample_to_chunk_tbl *sample_to_chunk;
  129. long sample_size;
  130. long sample_count;
  131. long *sample_sizes;
  132. } MOVStreamContext;
  133. typedef struct MOVContext {
  134. 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) */
  135. AVFormatContext *fc;
  136. long time_scale;
  137. int found_moov; /* when both 'moov' and 'mdat' sections has been found */
  138. int found_mdat; /* we suppose we have enough data to read the file */
  139. INT64 mdat_size;
  140. INT64 mdat_offset;
  141. int total_streams;
  142. /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
  143. * but we need the info to be able to skip data from those streams in the 'mdat' section
  144. */
  145. MOVStreamContext *streams[MAX_STREAMS];
  146. INT64 next_chunk_offset;
  147. } MOVContext;
  148. struct MOVParseTableEntry;
  149. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  150. /* those functions parse an atom */
  151. /* return code:
  152. 1: found what I wanted, exit
  153. 0: continue to parse next atom
  154. -1: error occured, exit
  155. */
  156. typedef int (*mov_parse_function)(const struct MOVParseTableEntry *parse_table,
  157. ByteIOContext *pb,
  158. UINT32 atom_type,
  159. INT64 atom_offset, /* after the size and type field (and eventually the extended size) */
  160. INT64 atom_size, /* total size (excluding the size and type fields) */
  161. void *param);
  162. /* links atom IDs to parse functions */
  163. typedef struct MOVParseTableEntry {
  164. UINT32 type;
  165. mov_parse_function func;
  166. } MOVParseTableEntry;
  167. static int parse_leaf(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  168. {
  169. #ifdef DEBUG
  170. print_atom("leaf", atom_type, atom_offset, atom_size);
  171. #endif
  172. if(atom_size>1)
  173. url_fskip(pb, atom_size);
  174. /* url_seek(pb, atom_offset+atom_size, SEEK_SET); */
  175. return 0;
  176. }
  177. static int parse_default(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  178. {
  179. UINT32 type, foo=0;
  180. UINT64 offset, size;
  181. UINT64 total_size = 0;
  182. int i;
  183. int err = 0;
  184. foo=0;
  185. #ifdef DEBUG
  186. print_atom("default", atom_type, atom_offset, atom_size);
  187. debug_indent++;
  188. #endif
  189. offset = atom_offset;
  190. if(atom_size < 0)
  191. atom_size = 0x0FFFFFFFFFFFFFFF;
  192. while((total_size < atom_size) && !url_feof(pb) && !err) {
  193. size=atom_size;
  194. type=0L;
  195. if(atom_size >= 8) {
  196. size = get_be32(pb);
  197. type = get_le32(pb);
  198. }
  199. total_size += 8;
  200. offset+=8;
  201. // printf("type: %08lx sz: %08lx", type, size);
  202. if(size == 1) { /* 64 bit extended size */
  203. size = get_be64(pb);
  204. offset+=8;
  205. total_size+=8;
  206. size-=8;
  207. }
  208. if(size == 0)
  209. size = atom_size - total_size;
  210. size-=8;
  211. for(i=0; parse_table[i].type != 0L && parse_table[i].type != type; i++);
  212. // printf(" i=%ld\n", i);
  213. if (parse_table[i].type == 0) { /* skip leaf atoms data */
  214. // url_seek(pb, atom_offset+atom_size, SEEK_SET);
  215. #ifdef DEBUG
  216. print_atom("unknown", type, offset, size);
  217. #endif
  218. url_fskip(pb, size);
  219. } else
  220. err = (parse_table[i].func)(parse_table, pb, type, offset, size, param);
  221. offset+=size;
  222. total_size+=size;
  223. }
  224. #ifdef DEBUG
  225. debug_indent--;
  226. #endif
  227. return err;
  228. }
  229. static int parse_mvhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  230. {
  231. MOVContext *c;
  232. #ifdef DEBUG
  233. print_atom("mvhd", atom_type, atom_offset, atom_size);
  234. #endif
  235. c = (MOVContext *)param;
  236. get_byte(pb); /* version */
  237. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  238. get_be32(pb); /* creation time */
  239. get_be32(pb); /* modification time */
  240. c->time_scale = get_be32(pb); /* time scale */
  241. get_be32(pb); /* duration */
  242. get_be32(pb); /* preferred scale */
  243. get_be16(pb); /* preferred volume */
  244. url_fskip(pb, 10); /* reserved */
  245. url_fskip(pb, 36); /* display matrix */
  246. get_be32(pb); /* preview time */
  247. get_be32(pb); /* preview duration */
  248. get_be32(pb); /* poster time */
  249. get_be32(pb); /* selection time */
  250. get_be32(pb); /* selection duration */
  251. get_be32(pb); /* current time */
  252. get_be32(pb); /* next track ID */
  253. return 0;
  254. }
  255. /* this atom should contain all header atoms */
  256. static int parse_moov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  257. {
  258. int err;
  259. MOVContext *c;
  260. #ifdef DEBUG
  261. print_atom("moov", atom_type, atom_offset, atom_size);
  262. #endif
  263. c = (MOVContext *)param;
  264. err = parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
  265. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  266. /* so we don't parse the whole file if over a network */
  267. c->found_moov=1;
  268. if(c->found_mdat)
  269. return 1; /* found both, just go */
  270. return 0; /* now go for mdat */
  271. }
  272. /* this atom contains actual media data */
  273. static int parse_mdat(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  274. {
  275. MOVContext *c;
  276. #ifdef DEBUG
  277. print_atom("mdat", atom_type, atom_offset, atom_size);
  278. #endif
  279. c = (MOVContext *)param;
  280. if(atom_size == 0) /* wrong one (MP4) */
  281. return 0;
  282. c->found_mdat=1;
  283. c->mdat_offset = atom_offset;
  284. c->mdat_size = atom_size;
  285. if(c->found_moov)
  286. return 1; /* found both, just go */
  287. url_fskip(pb, atom_size);
  288. return 0; /* now go for moov */
  289. }
  290. static int parse_trak(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  291. {
  292. MOVContext *c;
  293. AVStream *st;
  294. MOVStreamContext *sc;
  295. #ifdef DEBUG
  296. print_atom("trak", atom_type, atom_offset, atom_size);
  297. #endif
  298. c = (MOVContext *)param;
  299. st = av_malloc(sizeof(AVStream));
  300. if (!st) return -2;
  301. memset(st, 0, sizeof(AVStream));
  302. c->fc->streams[c->fc->nb_streams] = st;
  303. sc = av_malloc(sizeof(MOVStreamContext));
  304. st->priv_data = sc;
  305. st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
  306. c->streams[c->fc->nb_streams++] = sc;
  307. return parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
  308. }
  309. static int parse_tkhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  310. {
  311. MOVContext *c;
  312. AVStream *st;
  313. #ifdef DEBUG
  314. print_atom("tkhd", atom_type, atom_offset, atom_size);
  315. #endif
  316. c = (MOVContext *)param;
  317. st = c->fc->streams[c->fc->nb_streams-1];
  318. get_byte(pb); /* version */
  319. get_byte(pb); get_byte(pb);
  320. get_byte(pb); /* flags */
  321. /*
  322. MOV_TRACK_ENABLED 0x0001
  323. MOV_TRACK_IN_MOVIE 0x0002
  324. MOV_TRACK_IN_PREVIEW 0x0004
  325. MOV_TRACK_IN_POSTER 0x0008
  326. */
  327. get_be32(pb); /* creation time */
  328. get_be32(pb); /* modification time */
  329. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  330. get_be32(pb); /* reserved */
  331. get_be32(pb); /* duration */
  332. get_be32(pb); /* reserved */
  333. get_be32(pb); /* reserved */
  334. get_be16(pb); /* layer */
  335. get_be16(pb); /* alternate group */
  336. get_be16(pb); /* volume */
  337. get_be16(pb); /* reserved */
  338. url_fskip(pb, 36); /* display matrix */
  339. /* those are fixed-point */
  340. st->codec.width = get_be32(pb) >> 16; /* track width */
  341. st->codec.height = get_be32(pb) >> 16; /* track height */
  342. return 0;
  343. }
  344. static int parse_hdlr(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  345. {
  346. MOVContext *c;
  347. int len;
  348. char *buf;
  349. UINT32 type;
  350. AVStream *st;
  351. UINT32 ctype;
  352. #ifdef DEBUG
  353. print_atom("hdlr", atom_type, atom_offset, atom_size);
  354. #endif
  355. c = (MOVContext *)param;
  356. st = c->fc->streams[c->fc->nb_streams-1];
  357. get_byte(pb); /* version */
  358. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  359. /* component type */
  360. ctype = get_le32(pb);
  361. type = get_le32(pb); /* component subtype */
  362. #ifdef DEBUG
  363. printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
  364. printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
  365. #endif
  366. #ifdef DEBUG
  367. /* XXX: yeah this is ugly... */
  368. if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
  369. if(type == MKTAG('v', 'i', 'd', 'e'))
  370. puts("hdlr: vide");
  371. else if(type == MKTAG('s', 'o', 'u', 'n'))
  372. puts("hdlr: soun");
  373. } else if(ctype == 0) { /* MP4 */
  374. if(type == MKTAG('v', 'i', 'd', 'e'))
  375. puts("hdlr: vide");
  376. else if(type == MKTAG('s', 'o', 'u', 'n'))
  377. puts("hdlr: soun");
  378. else if(type == MKTAG('o', 'd', 's', 'm'))
  379. puts("hdlr: odsm");
  380. else if(type == MKTAG('s', 'd', 's', 'm'))
  381. puts("hdlr: sdsm");
  382. } else puts("hdlr: meta");
  383. #endif
  384. if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
  385. if(type == MKTAG('v', 'i', 'd', 'e'))
  386. st->codec.codec_type = CODEC_TYPE_VIDEO;
  387. else if(type == MKTAG('s', 'o', 'u', 'n'))
  388. st->codec.codec_type = CODEC_TYPE_AUDIO;
  389. } else if(ctype == 0) { /* MP4 */
  390. if(type == MKTAG('v', 'i', 'd', 'e'))
  391. st->codec.codec_type = CODEC_TYPE_VIDEO;
  392. else if(type == MKTAG('s', 'o', 'u', 'n'))
  393. st->codec.codec_type = CODEC_TYPE_AUDIO;
  394. }
  395. get_be32(pb); /* component manufacture */
  396. get_be32(pb); /* component flags */
  397. get_be32(pb); /* component flags mask */
  398. if(atom_size <= 24)
  399. return 0; /* nothing left to read */
  400. /* XXX: MP4 uses a C string, not a pascal one */
  401. /* component name */
  402. len = get_byte(pb);
  403. /* XXX: use a better heuristic */
  404. if(len < 32) {
  405. /* assume that it is a Pascal like string */
  406. buf = av_malloc(len+1);
  407. get_buffer(pb, buf, len);
  408. buf[len] = '\0';
  409. #ifdef DEBUG
  410. printf("**buf='%s'\n", buf);
  411. #endif
  412. av_free(buf);
  413. } else {
  414. /* MP4 string */
  415. for(;;) {
  416. if (len == 0)
  417. break;
  418. len = get_byte(pb);
  419. }
  420. }
  421. return 0;
  422. }
  423. static int parse_stsd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  424. {
  425. MOVContext *c;
  426. int entries, size, samp_sz, frames_per_sample;
  427. UINT32 format;
  428. AVStream *st;
  429. #ifdef DEBUG
  430. print_atom("stsd", atom_type, atom_offset, atom_size);
  431. #endif
  432. c = (MOVContext *)param;
  433. st = c->fc->streams[c->fc->nb_streams-1];
  434. get_byte(pb); /* version */
  435. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  436. entries = get_be32(pb);
  437. while(entries--) {
  438. size = get_be32(pb); /* size */
  439. format = get_le32(pb); /* data format */
  440. get_be32(pb); /* reserved */
  441. get_be16(pb); /* reserved */
  442. get_be16(pb); /* index */
  443. /* if(format == MKTAG('m', 'p', '4', 'v')) */
  444. /* st->codec.codec_type=CODEC_TYPE_VIDEO; *//* force things (XXX: was this for .mp4 ?) */
  445. if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
  446. st->codec.codec_tag = format;
  447. st->codec.codec_id = codec_get_id(mov_video_tags, format);
  448. get_be16(pb); /* version */
  449. get_be16(pb); /* revision level */
  450. get_be32(pb); /* vendor */
  451. get_be32(pb); /* temporal quality */
  452. get_be32(pb); /* spacial quality */
  453. st->codec.width = get_be16(pb); /* width */
  454. st->codec.height = get_be16(pb); /* height */
  455. get_be32(pb); /* horiz resolution */
  456. get_be32(pb); /* vert resolution */
  457. get_be32(pb); /* data size, always 0 */
  458. frames_per_sample = get_be16(pb); /* frame per samples */
  459. #ifdef DEBUG
  460. printf("frames/samples = %d\n", frames_per_sample);
  461. #endif
  462. url_fskip(pb, 32); /* codec name */
  463. get_be16(pb); /* depth */
  464. get_be16(pb); /* colortable id */
  465. get_be16(pb); /* */
  466. get_be16(pb); /* */
  467. st->codec.sample_rate = 25 * FRAME_RATE_BASE;
  468. if(size > 16)
  469. url_fskip(pb, size-(16+24+18+32));
  470. } else {
  471. st->codec.codec_tag = format;
  472. get_be16(pb); /* version */
  473. get_be16(pb); /* revision level */
  474. get_be32(pb); /* vendor */
  475. st->codec.channels = get_be16(pb);/* channel count */
  476. samp_sz = get_be16(pb); /* sample size */
  477. #ifdef DEBUG
  478. if(samp_sz != 16)
  479. puts("!!! stsd: audio sample size is not 16 bit !");
  480. #endif
  481. st->codec.codec_id = codec_get_id(mov_audio_tags, format);
  482. /* handle specific s8 codec */
  483. if (st->codec.codec_id == CODEC_ID_PCM_S16BE && samp_sz == 8)
  484. st->codec.codec_id = CODEC_ID_PCM_S8;
  485. get_be16(pb); /* compression id = 0*/
  486. get_be16(pb); /* packet size = 0 */
  487. st->codec.sample_rate = ((get_be32(pb) >> 16));
  488. st->codec.bit_rate = 0;
  489. /* this is for .mp4 files */
  490. if(format == MKTAG('m', 'p', '4', 'v')) { /* XXX */
  491. st->codec.codec_type=CODEC_TYPE_VIDEO; /* force things */
  492. st->codec.codec_id = CODEC_ID_MPEG4;
  493. st->codec.frame_rate = 25;
  494. st->codec.bit_rate = 100000;
  495. }
  496. #if 0
  497. get_be16(pb); get_be16(pb); /* */
  498. get_be16(pb); /* */
  499. get_be16(pb); /* */
  500. get_be16(pb); /* */
  501. get_be16(pb); /* */
  502. #endif
  503. if(size > 16)
  504. url_fskip(pb, size-(16+20));
  505. }
  506. #ifdef DEBUG
  507. printf("4CC= %c%c%c%c\n", *((char *)&format), ((char *)&format)[1], ((char *)&format)[2], ((char *)&format)[3]);
  508. #endif
  509. }
  510. /*
  511. if(len) {
  512. buf = av_malloc(len+1);
  513. get_buffer(pb, buf, len);
  514. buf[len] = '\0';
  515. puts(buf);
  516. av_free(buf);
  517. }
  518. */
  519. return 0;
  520. }
  521. static int parse_stco(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  522. {
  523. MOVContext *c;
  524. int entries, i;
  525. AVStream *st;
  526. MOVStreamContext *sc;
  527. #ifdef DEBUG
  528. print_atom("stco", atom_type, atom_offset, atom_size);
  529. #endif
  530. c = (MOVContext *)param;
  531. st = c->fc->streams[c->fc->nb_streams-1];
  532. sc = (MOVStreamContext *)st->priv_data;
  533. get_byte(pb); /* version */
  534. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  535. entries = get_be32(pb);
  536. sc->chunk_count = entries;
  537. sc->chunk_offsets = av_malloc(entries * sizeof(INT64));
  538. if(atom_type == MKTAG('s', 't', 'c', 'o')) {
  539. for(i=0; i<entries; i++) {
  540. sc->chunk_offsets[i] = get_be32(pb);
  541. /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
  542. }
  543. } else if(atom_type == MKTAG('c', 'o', '6', '4')) {
  544. for(i=0; i<entries; i++) {
  545. sc->chunk_offsets[i] = get_be64(pb);
  546. /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
  547. }
  548. } else
  549. return -1;
  550. return 0;
  551. }
  552. static int parse_stsc(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  553. {
  554. MOVContext *c;
  555. int entries, i;
  556. AVStream *st;
  557. MOVStreamContext *sc;
  558. #ifdef DEBUG
  559. print_atom("stsc", atom_type, atom_offset, atom_size);
  560. #endif
  561. c = (MOVContext *)param;
  562. st = c->fc->streams[c->fc->nb_streams-1];
  563. sc = (MOVStreamContext *)st->priv_data;
  564. get_byte(pb); /* version */
  565. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  566. entries = get_be32(pb);
  567. sc->sample_to_chunk_sz = entries;
  568. sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
  569. for(i=0; i<entries; i++) {
  570. sc->sample_to_chunk[i].first = get_be32(pb);
  571. sc->sample_to_chunk[i].count = get_be32(pb);
  572. sc->sample_to_chunk[i].id = get_be32(pb);
  573. #ifdef DEBUG
  574. /* 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); */
  575. #endif
  576. }
  577. return 0;
  578. }
  579. static int parse_stsz(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  580. {
  581. MOVContext *c;
  582. int entries, i;
  583. AVStream *st;
  584. MOVStreamContext *sc;
  585. #ifdef DEBUG
  586. print_atom("stsz", atom_type, atom_offset, atom_size);
  587. #endif
  588. c = (MOVContext *)param;
  589. st = c->fc->streams[c->fc->nb_streams-1];
  590. sc = (MOVStreamContext *)st->priv_data;
  591. get_byte(pb); /* version */
  592. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  593. sc->sample_size = get_be32(pb);
  594. entries = get_be32(pb);
  595. sc->sample_count = entries;
  596. printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
  597. if(sc->sample_size)
  598. return 0; /* there isn't any table following */
  599. sc->sample_sizes = av_malloc(entries * sizeof(long));
  600. for(i=0; i<entries; i++) {
  601. sc->sample_sizes[i] = get_be32(pb);
  602. #ifdef DEBUG
  603. /* printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
  604. #endif
  605. }
  606. return 0;
  607. }
  608. static const MOVParseTableEntry mov_default_parse_table[] = {
  609. /* mp4 atoms */
  610. { MKTAG( 'm', 'p', '4', 'a' ), parse_default },
  611. { MKTAG( 'c', 'o', '6', '4' ), parse_stco },
  612. { MKTAG( 's', 't', 'c', 'o' ), parse_stco },
  613. { MKTAG( 'c', 'r', 'h', 'd' ), parse_default },
  614. { MKTAG( 'c', 't', 't', 's' ), parse_leaf },
  615. { MKTAG( 'c', 'p', 'r', 't' ), parse_default },
  616. { MKTAG( 'u', 'r', 'l', ' ' ), parse_leaf },
  617. { MKTAG( 'u', 'r', 'n', ' ' ), parse_leaf },
  618. { MKTAG( 'd', 'i', 'n', 'f' ), parse_default },
  619. { MKTAG( 'd', 'r', 'e', 'f' ), parse_leaf },
  620. { MKTAG( 's', 't', 'd', 'p' ), parse_default },
  621. { MKTAG( 'e', 's', 'd', 's' ), parse_default },
  622. { MKTAG( 'e', 'd', 't', 's' ), parse_default },
  623. { MKTAG( 'e', 'l', 's', 't' ), parse_leaf },
  624. { MKTAG( 'u', 'u', 'i', 'd' ), parse_default },
  625. { MKTAG( 'f', 'r', 'e', 'e' ), parse_leaf },
  626. { MKTAG( 'h', 'd', 'l', 'r' ), parse_hdlr },
  627. { MKTAG( 'h', 'm', 'h', 'd' ), parse_default },
  628. { MKTAG( 'h', 'i', 'n', 't' ), parse_leaf },
  629. { MKTAG( 'n', 'm', 'h', 'd' ), parse_leaf },
  630. { MKTAG( 'm', 'p', '4', 's' ), parse_default },
  631. { MKTAG( 'm', 'd', 'i', 'a' ), parse_default },
  632. { MKTAG( 'm', 'd', 'a', 't' ), parse_mdat },
  633. { MKTAG( 'm', 'd', 'h', 'd' ), parse_leaf },
  634. { MKTAG( 'm', 'i', 'n', 'f' ), parse_default },
  635. { MKTAG( 'm', 'o', 'o', 'v' ), parse_moov },
  636. { MKTAG( 'm', 'v', 'h', 'd' ), parse_mvhd },
  637. { MKTAG( 'i', 'o', 'd', 's' ), parse_leaf },
  638. { MKTAG( 'o', 'd', 'h', 'd' ), parse_default },
  639. { MKTAG( 'm', 'p', 'o', 'd' ), parse_leaf },
  640. { MKTAG( 's', 't', 's', 'd' ), parse_stsd },
  641. { MKTAG( 's', 't', 's', 'z' ), parse_stsz },
  642. { MKTAG( 's', 't', 'b', 'l' ), parse_default },
  643. { MKTAG( 's', 't', 's', 'c' ), parse_stsc },
  644. { MKTAG( 's', 'd', 'h', 'd' ), parse_default },
  645. { MKTAG( 's', 't', 's', 'h' ), parse_default },
  646. { MKTAG( 's', 'k', 'i', 'p' ), parse_default },
  647. { MKTAG( 's', 'm', 'h', 'd' ), parse_leaf },
  648. { MKTAG( 'd', 'p', 'n', 'd' ), parse_leaf },
  649. { MKTAG( 's', 't', 's', 's' ), parse_leaf },
  650. { MKTAG( 's', 't', 't', 's' ), parse_leaf },
  651. { MKTAG( 't', 'r', 'a', 'k' ), parse_trak },
  652. { MKTAG( 't', 'k', 'h', 'd' ), parse_tkhd },
  653. { MKTAG( 't', 'r', 'e', 'f' ), parse_default }, /* not really */
  654. { MKTAG( 'u', 'd', 't', 'a' ), parse_leaf },
  655. { MKTAG( 'v', 'm', 'h', 'd' ), parse_leaf },
  656. { MKTAG( 'm', 'p', '4', 'v' ), parse_default },
  657. /* extra mp4 */
  658. { MKTAG( 'M', 'D', 'E', 'S' ), parse_leaf },
  659. /* QT atoms */
  660. { MKTAG( 'c', 'h', 'a', 'p' ), parse_leaf },
  661. { MKTAG( 'c', 'l', 'i', 'p' ), parse_default },
  662. { MKTAG( 'c', 'r', 'g', 'n' ), parse_leaf },
  663. { MKTAG( 'k', 'm', 'a', 't' ), parse_leaf },
  664. { MKTAG( 'm', 'a', 't', 't' ), parse_default },
  665. { MKTAG( 'r', 'd', 'r', 'f' ), parse_leaf },
  666. { MKTAG( 'r', 'm', 'd', 'a' ), parse_default },
  667. { MKTAG( 'r', 'm', 'd', 'r' ), parse_leaf },
  668. //{ MKTAG( 'r', 'm', 'q', 'u' ), parse_leaf },
  669. { MKTAG( 'r', 'm', 'r', 'a' ), parse_default },
  670. { MKTAG( 's', 'c', 'p', 't' ), parse_leaf },
  671. { MKTAG( 's', 'y', 'n', 'c' ), parse_leaf },
  672. { MKTAG( 's', 's', 'r', 'c' ), parse_leaf },
  673. { MKTAG( 't', 'c', 'm', 'd' ), parse_leaf },
  674. { MKTAG( 'w', 'i', 'd', 'e' ), parse_leaf }, /* place holder */
  675. { 0L, parse_leaf }
  676. };
  677. static void mov_free_stream_context(MOVStreamContext *sc)
  678. {
  679. if(sc) {
  680. av_free(sc->chunk_offsets);
  681. av_free(sc->sample_to_chunk);
  682. av_free(sc);
  683. }
  684. }
  685. /* XXX: is it suffisant ? */
  686. static int mov_probe(AVProbeData *p)
  687. {
  688. /* check file header */
  689. if (p->buf_size <= 12)
  690. return 0;
  691. if ((p->buf[4] == 'm' && p->buf[5] == 'o' &&
  692. p->buf[6] == 'o' && p->buf[7] == 'v') ||
  693. (p->buf[4] == 'm' && p->buf[5] == 'd' &&
  694. p->buf[6] == 'a' && p->buf[7] == 't'))
  695. return AVPROBE_SCORE_MAX;
  696. else
  697. return 0;
  698. }
  699. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  700. {
  701. MOVContext *mov = s->priv_data;
  702. ByteIOContext *pb = &s->pb;
  703. int i, j, nb, err;
  704. INT64 size;
  705. mov->fc = s;
  706. #if 0
  707. /* XXX: I think we should auto detect */
  708. if(s->iformat->name[1] == 'p')
  709. mov->mp4 = 1;
  710. #endif
  711. if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  712. size = url_filesize(url_fileno(pb));
  713. else
  714. size = 0x7FFFFFFFFFFFFFFF;
  715. #ifdef DEBUG
  716. printf("filesz=%Ld\n", size);
  717. #endif
  718. /* check MOV header */
  719. err = parse_default(mov_default_parse_table, pb, 0L, 0LL, size, mov);
  720. if(err<0 || (!mov->found_moov || !mov->found_mdat)) {
  721. puts("header not found !!!");
  722. exit(1);
  723. }
  724. #ifdef DEBUG
  725. printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
  726. #endif
  727. /* some cleanup : make sure we are on the mdat atom */
  728. if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
  729. url_fseek(pb, mov->mdat_offset, SEEK_SET);
  730. mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
  731. #ifdef DEBUG
  732. printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
  733. #endif
  734. #ifdef DEBUG
  735. printf("streams= %d\n", s->nb_streams);
  736. #endif
  737. mov->total_streams = nb = s->nb_streams;
  738. #if 1
  739. for(i=0; i<s->nb_streams;) {
  740. if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
  741. av_free(s->streams[i]);
  742. for(j=i+1; j<s->nb_streams; j++)
  743. s->streams[j-1] = s->streams[j];
  744. s->nb_streams--;
  745. } else
  746. i++;
  747. }
  748. for(i=0; i<s->nb_streams;i++) {
  749. MOVStreamContext *sc;
  750. sc = (MOVStreamContext *)s->streams[i]->priv_data;
  751. sc->ffindex = i;
  752. sc->is_ff_stream = 1;
  753. }
  754. #endif
  755. #ifdef DEBUG
  756. printf("real streams= %d\n", s->nb_streams);
  757. #endif
  758. return 0;
  759. }
  760. /* Yes, this is ugly... I didn't write the specs of QT :p */
  761. /* XXX:remove useless commented code sometime */
  762. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  763. {
  764. MOVContext *mov = s->priv_data;
  765. INT64 offset = 0x0FFFFFFFFFFFFFFF;
  766. int i;
  767. int st_id = 0, size;
  768. size = 0x0FFFFFFF;
  769. again:
  770. for(i=0; i<mov->total_streams; i++) {
  771. /* printf("%8ld ", mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk]); */
  772. if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
  773. && (mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] < offset)) {
  774. /* printf("y"); */
  775. st_id = i;
  776. offset = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk];
  777. }
  778. /* else printf("n"); */
  779. }
  780. mov->streams[st_id]->next_chunk++;
  781. if(offset==0x0FFFFFFFFFFFFFFF)
  782. return -1;
  783. if(mov->next_chunk_offset < offset) /* some meta data */
  784. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  785. if(!mov->streams[st_id]->is_ff_stream) {
  786. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  787. offset = 0x0FFFFFFFFFFFFFFF;
  788. /* puts("*"); */
  789. goto again;
  790. }
  791. /* printf("\nchunk offset = %ld\n", offset); */
  792. /* now get the chunk size... */
  793. for(i=0; i<mov->total_streams; i++) {
  794. /* printf("%ld ", mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset); */
  795. if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
  796. && ((mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset) < size)) {
  797. /* printf("y"); */
  798. size = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset;
  799. }
  800. /* else printf("n"); */
  801. }
  802. /* printf("\nchunk size = %ld\n", size); */
  803. if(size == 0x0FFFFFFF)
  804. size = mov->mdat_size + mov->mdat_offset - offset;
  805. if(size < 0)
  806. return -1;
  807. if(size == 0)
  808. return -1;
  809. av_new_packet(pkt, size);
  810. pkt->stream_index = mov->streams[st_id]->ffindex;
  811. get_buffer(&s->pb, pkt->data, pkt->size);
  812. #ifdef DEBUG
  813. /*
  814. printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
  815. for(i=0; i<8; i++)
  816. printf("%02x ", pkt->data[i]);
  817. for(i=0; i<8; i++)
  818. printf("%c ", (pkt->data[i]) & 0x7F);
  819. puts("");
  820. */
  821. #endif
  822. mov->next_chunk_offset = offset + size;
  823. return 0;
  824. }
  825. static int mov_read_close(AVFormatContext *s)
  826. {
  827. int i;
  828. MOVContext *mov = s->priv_data;
  829. for(i=0; i<mov->total_streams; i++)
  830. mov_free_stream_context(mov->streams[i]);
  831. for(i=0; i<s->nb_streams; i++)
  832. av_free(s->streams[i]);
  833. return 0;
  834. }
  835. static AVInputFormat mov_iformat = {
  836. "mov",
  837. "QuickTime/MPEG4 format",
  838. sizeof(MOVContext),
  839. mov_probe,
  840. mov_read_header,
  841. mov_read_packet,
  842. mov_read_close,
  843. };
  844. int mov_init(void)
  845. {
  846. av_register_input_format(&mov_iformat);
  847. return 0;
  848. }