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.

957 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 = 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 = 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, ch;
  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. if(c->mp4) {
  403. #ifdef DEBUG
  404. puts("MP4!!!");
  405. #endif
  406. while ((ch = get_byte(pb)));
  407. } else {
  408. len = get_byte(pb);
  409. if(len) {
  410. buf = malloc(len+1);
  411. get_buffer(pb, buf, len);
  412. buf[len] = '\0';
  413. #ifdef DEBUG
  414. puts(buf);
  415. #endif
  416. free(buf);
  417. }
  418. }
  419. return 0;
  420. }
  421. static int parse_stsd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  422. {
  423. MOVContext *c;
  424. int entries, size, samp_sz, frames_per_sample;
  425. UINT32 format;
  426. AVStream *st;
  427. #ifdef DEBUG
  428. print_atom("stsd", 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. entries = get_be32(pb);
  435. while(entries--) {
  436. size = get_be32(pb); /* size */
  437. format = get_le32(pb); /* data format */
  438. get_be32(pb); /* reserved */
  439. get_be16(pb); /* reserved */
  440. get_be16(pb); /* index */
  441. /* if(format == MKTAG('m', 'p', '4', 'v')) */
  442. /* st->codec.codec_type=CODEC_TYPE_VIDEO; *//* force things (XXX: was this for .mp4 ?) */
  443. if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
  444. st->codec.codec_tag = format;
  445. st->codec.codec_id = codec_get_id(mov_video_tags, format);
  446. get_be16(pb); /* version */
  447. get_be16(pb); /* revision level */
  448. get_be32(pb); /* vendor */
  449. get_be32(pb); /* temporal quality */
  450. get_be32(pb); /* spacial quality */
  451. st->codec.width = get_be16(pb); /* width */
  452. st->codec.height = get_be16(pb); /* height */
  453. get_be32(pb); /* horiz resolution */
  454. get_be32(pb); /* vert resolution */
  455. get_be32(pb); /* data size, always 0 */
  456. frames_per_sample = get_be16(pb); /* frame per samples */
  457. #ifdef DEBUG
  458. printf("frames/samples = %d\n", frames_per_sample);
  459. #endif
  460. url_fskip(pb, 32); /* codec name */
  461. get_be16(pb); /* depth */
  462. get_be16(pb); /* colortable id */
  463. get_be16(pb); /* */
  464. get_be16(pb); /* */
  465. st->codec.sample_rate = 25 * FRAME_RATE_BASE;
  466. if(size > 16)
  467. url_fskip(pb, size-(16+24+18+32));
  468. } else {
  469. st->codec.codec_tag = format;
  470. get_be16(pb); /* version */
  471. get_be16(pb); /* revision level */
  472. get_be32(pb); /* vendor */
  473. st->codec.channels = get_be16(pb);/* channel count */
  474. samp_sz = get_be16(pb); /* sample size */
  475. #ifdef DEBUG
  476. if(samp_sz != 16)
  477. puts("!!! stsd: audio sample size is not 16 bit !");
  478. #endif
  479. st->codec.codec_id = codec_get_id(mov_audio_tags, format);
  480. /* handle specific s8 codec */
  481. if (st->codec.codec_id == CODEC_ID_PCM_S16BE && samp_sz == 8)
  482. st->codec.codec_id = CODEC_ID_PCM_S8;
  483. get_be16(pb); /* compression id = 0*/
  484. get_be16(pb); /* packet size = 0 */
  485. st->codec.sample_rate = ((get_be32(pb) >> 16));
  486. st->codec.bit_rate = 0;
  487. /* this is for .mp4 files */
  488. if(format == MKTAG('m', 'p', '4', 'v')) { /* XXX */
  489. st->codec.codec_type=CODEC_TYPE_VIDEO; /* force things */
  490. st->codec.codec_id = CODEC_ID_MPEG4;
  491. st->codec.frame_rate = 25;
  492. st->codec.bit_rate = 100000;
  493. }
  494. #if 0
  495. get_be16(pb); get_be16(pb); /* */
  496. get_be16(pb); /* */
  497. get_be16(pb); /* */
  498. get_be16(pb); /* */
  499. get_be16(pb); /* */
  500. #endif
  501. if(size > 16)
  502. url_fskip(pb, size-(16+20));
  503. }
  504. #ifdef DEBUG
  505. printf("4CC= %c%c%c%c\n", *((char *)&format), ((char *)&format)[1], ((char *)&format)[2], ((char *)&format)[3]);
  506. #endif
  507. }
  508. /*
  509. if(len) {
  510. buf = malloc(len+1);
  511. get_buffer(pb, buf, len);
  512. buf[len] = '\0';
  513. puts(buf);
  514. free(buf);
  515. }
  516. */
  517. return 0;
  518. }
  519. static int parse_stco(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  520. {
  521. MOVContext *c;
  522. int entries, i;
  523. AVStream *st;
  524. MOVStreamContext *sc;
  525. #ifdef DEBUG
  526. print_atom("stco", atom_type, atom_offset, atom_size);
  527. #endif
  528. c = (MOVContext *)param;
  529. st = c->fc->streams[c->fc->nb_streams-1];
  530. sc = (MOVStreamContext *)st->priv_data;
  531. get_byte(pb); /* version */
  532. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  533. entries = get_be32(pb);
  534. sc->chunk_count = entries;
  535. sc->chunk_offsets = malloc(entries * sizeof(INT64));
  536. if(atom_type == MKTAG('s', 't', 'c', 'o')) {
  537. for(i=0; i<entries; i++) {
  538. sc->chunk_offsets[i] = get_be32(pb);
  539. /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
  540. }
  541. } else if(atom_type == MKTAG('c', 'o', '6', '4')) {
  542. for(i=0; i<entries; i++) {
  543. sc->chunk_offsets[i] = get_be64(pb);
  544. /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
  545. }
  546. } else
  547. return -1;
  548. return 0;
  549. }
  550. static int parse_stsc(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  551. {
  552. MOVContext *c;
  553. int entries, i;
  554. AVStream *st;
  555. MOVStreamContext *sc;
  556. #ifdef DEBUG
  557. print_atom("stsc", atom_type, atom_offset, atom_size);
  558. #endif
  559. c = (MOVContext *)param;
  560. st = c->fc->streams[c->fc->nb_streams-1];
  561. sc = (MOVStreamContext *)st->priv_data;
  562. get_byte(pb); /* version */
  563. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  564. entries = get_be32(pb);
  565. sc->sample_to_chunk_sz = entries;
  566. sc->sample_to_chunk = malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
  567. for(i=0; i<entries; i++) {
  568. sc->sample_to_chunk[i].first = get_be32(pb);
  569. sc->sample_to_chunk[i].count = get_be32(pb);
  570. sc->sample_to_chunk[i].id = get_be32(pb);
  571. #ifdef DEBUG
  572. /* 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); */
  573. #endif
  574. }
  575. return 0;
  576. }
  577. static int parse_stsz(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  578. {
  579. MOVContext *c;
  580. int entries, i;
  581. AVStream *st;
  582. MOVStreamContext *sc;
  583. #ifdef DEBUG
  584. print_atom("stsz", atom_type, atom_offset, atom_size);
  585. #endif
  586. c = (MOVContext *)param;
  587. st = c->fc->streams[c->fc->nb_streams-1];
  588. sc = (MOVStreamContext *)st->priv_data;
  589. get_byte(pb); /* version */
  590. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  591. sc->sample_size = get_be32(pb);
  592. entries = get_be32(pb);
  593. sc->sample_count = entries;
  594. printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
  595. if(sc->sample_size)
  596. return 0; /* there isn't any table following */
  597. sc->sample_sizes = malloc(entries * sizeof(long));
  598. for(i=0; i<entries; i++) {
  599. sc->sample_sizes[i] = get_be32(pb);
  600. #ifdef DEBUG
  601. /* printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
  602. #endif
  603. }
  604. return 0;
  605. }
  606. static const MOVParseTableEntry mov_default_parse_table[] = {
  607. /* mp4 atoms */
  608. { MKTAG( 'm', 'p', '4', 'a' ), parse_default },
  609. { MKTAG( 'c', 'o', '6', '4' ), parse_stco },
  610. { MKTAG( 's', 't', 'c', 'o' ), parse_stco },
  611. { MKTAG( 'c', 'r', 'h', 'd' ), parse_default },
  612. { MKTAG( 'c', 't', 't', 's' ), parse_leaf },
  613. { MKTAG( 'c', 'p', 'r', 't' ), parse_default },
  614. { MKTAG( 'u', 'r', 'l', ' ' ), parse_leaf },
  615. { MKTAG( 'u', 'r', 'n', ' ' ), parse_leaf },
  616. { MKTAG( 'd', 'i', 'n', 'f' ), parse_default },
  617. { MKTAG( 'd', 'r', 'e', 'f' ), parse_leaf },
  618. { MKTAG( 's', 't', 'd', 'p' ), parse_default },
  619. { MKTAG( 'e', 's', 'd', 's' ), parse_default },
  620. { MKTAG( 'e', 'd', 't', 's' ), parse_default },
  621. { MKTAG( 'e', 'l', 's', 't' ), parse_leaf },
  622. { MKTAG( 'u', 'u', 'i', 'd' ), parse_default },
  623. { MKTAG( 'f', 'r', 'e', 'e' ), parse_leaf },
  624. { MKTAG( 'h', 'd', 'l', 'r' ), parse_hdlr },
  625. { MKTAG( 'h', 'm', 'h', 'd' ), parse_default },
  626. { MKTAG( 'h', 'i', 'n', 't' ), parse_leaf },
  627. { MKTAG( 'n', 'm', 'h', 'd' ), parse_leaf },
  628. { MKTAG( 'm', 'p', '4', 's' ), parse_default },
  629. { MKTAG( 'm', 'd', 'i', 'a' ), parse_default },
  630. { MKTAG( 'm', 'd', 'a', 't' ), parse_mdat },
  631. { MKTAG( 'm', 'd', 'h', 'd' ), parse_leaf },
  632. { MKTAG( 'm', 'i', 'n', 'f' ), parse_default },
  633. { MKTAG( 'm', 'o', 'o', 'v' ), parse_moov },
  634. { MKTAG( 'm', 'v', 'h', 'd' ), parse_mvhd },
  635. { MKTAG( 'i', 'o', 'd', 's' ), parse_leaf },
  636. { MKTAG( 'o', 'd', 'h', 'd' ), parse_default },
  637. { MKTAG( 'm', 'p', 'o', 'd' ), parse_leaf },
  638. { MKTAG( 's', 't', 's', 'd' ), parse_stsd },
  639. { MKTAG( 's', 't', 's', 'z' ), parse_stsz },
  640. { MKTAG( 's', 't', 'b', 'l' ), parse_default },
  641. { MKTAG( 's', 't', 's', 'c' ), parse_stsc },
  642. { MKTAG( 's', 'd', 'h', 'd' ), parse_default },
  643. { MKTAG( 's', 't', 's', 'h' ), parse_default },
  644. { MKTAG( 's', 'k', 'i', 'p' ), parse_default },
  645. { MKTAG( 's', 'm', 'h', 'd' ), parse_leaf },
  646. { MKTAG( 'd', 'p', 'n', 'd' ), parse_leaf },
  647. { MKTAG( 's', 't', 's', 's' ), parse_leaf },
  648. { MKTAG( 's', 't', 't', 's' ), parse_leaf },
  649. { MKTAG( 't', 'r', 'a', 'k' ), parse_trak },
  650. { MKTAG( 't', 'k', 'h', 'd' ), parse_tkhd },
  651. { MKTAG( 't', 'r', 'e', 'f' ), parse_default }, /* not really */
  652. { MKTAG( 'u', 'd', 't', 'a' ), parse_leaf },
  653. { MKTAG( 'v', 'm', 'h', 'd' ), parse_leaf },
  654. { MKTAG( 'm', 'p', '4', 'v' ), parse_default },
  655. /* extra mp4 */
  656. { MKTAG( 'M', 'D', 'E', 'S' ), parse_leaf },
  657. /* QT atoms */
  658. { MKTAG( 'c', 'h', 'a', 'p' ), parse_leaf },
  659. { MKTAG( 'c', 'l', 'i', 'p' ), parse_default },
  660. { MKTAG( 'c', 'r', 'g', 'n' ), parse_leaf },
  661. { MKTAG( 'k', 'm', 'a', 't' ), parse_leaf },
  662. { MKTAG( 'm', 'a', 't', 't' ), parse_default },
  663. { MKTAG( 'r', 'd', 'r', 'f' ), parse_leaf },
  664. { MKTAG( 'r', 'm', 'd', 'a' ), parse_default },
  665. { MKTAG( 'r', 'm', 'd', 'r' ), parse_leaf },
  666. //{ MKTAG( 'r', 'm', 'q', 'u' ), parse_leaf },
  667. { MKTAG( 'r', 'm', 'r', 'a' ), parse_default },
  668. { MKTAG( 's', 'c', 'p', 't' ), parse_leaf },
  669. { MKTAG( 's', 'y', 'n', 'c' ), parse_leaf },
  670. { MKTAG( 's', 's', 'r', 'c' ), parse_leaf },
  671. { MKTAG( 't', 'c', 'm', 'd' ), parse_leaf },
  672. { MKTAG( 'w', 'i', 'd', 'e' ), parse_leaf }, /* place holder */
  673. { 0L, parse_leaf }
  674. };
  675. static void mov_free_stream_context(MOVStreamContext *sc)
  676. {
  677. if(sc) {
  678. if(sc->chunk_offsets)
  679. free(sc->chunk_offsets);
  680. if(sc->sample_to_chunk)
  681. free(sc->sample_to_chunk);
  682. free(sc);
  683. }
  684. }
  685. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  686. {
  687. MOVContext *mov;
  688. ByteIOContext *pb = &s->pb;
  689. int i, j, nb, err;
  690. INT64 size;
  691. mov = malloc(sizeof(MOVContext));
  692. if (!mov)
  693. return -1;
  694. memset(mov, 0, sizeof(MOVContext));
  695. s->priv_data = mov;
  696. mov->fc = s;
  697. if(s->format->name[1] == 'p')
  698. mov->mp4 = 1;
  699. if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  700. size = url_filesize(url_fileno(pb));
  701. else
  702. size = 0x7FFFFFFFFFFFFFFF;
  703. #ifdef DEBUG
  704. printf("filesz=%Ld\n", size);
  705. #endif
  706. /* check MOV header */
  707. err = parse_default(mov_default_parse_table, pb, 0L, 0LL, size, mov);
  708. if(err<0 || (!mov->found_moov || !mov->found_mdat)) {
  709. puts("header not found !!!");
  710. exit(1);
  711. }
  712. #ifdef DEBUG
  713. printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
  714. #endif
  715. /* some cleanup : make sure we are on the mdat atom */
  716. if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
  717. url_fseek(pb, mov->mdat_offset, SEEK_SET);
  718. mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
  719. #ifdef DEBUG
  720. printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
  721. #endif
  722. #ifdef DEBUG
  723. printf("streams= %d\n", s->nb_streams);
  724. #endif
  725. mov->total_streams = nb = s->nb_streams;
  726. #if 1
  727. for(i=0; i<s->nb_streams;) {
  728. if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
  729. free(s->streams[i]);
  730. for(j=i+1; j<s->nb_streams; j++)
  731. s->streams[j-1] = s->streams[j];
  732. s->nb_streams--;
  733. } else
  734. i++;
  735. }
  736. for(i=0; i<s->nb_streams;i++) {
  737. MOVStreamContext *sc;
  738. sc = (MOVStreamContext *)s->streams[i]->priv_data;
  739. sc->ffindex = i;
  740. sc->is_ff_stream = 1;
  741. }
  742. #endif
  743. #ifdef DEBUG
  744. printf("real streams= %d\n", s->nb_streams);
  745. #endif
  746. return 0;
  747. }
  748. /* Yes, this is ugly... I didn't write the specs of QT :p */
  749. /* XXX:remove useless commented code sometime */
  750. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  751. {
  752. MOVContext *mov = s->priv_data;
  753. INT64 offset = 0x0FFFFFFFFFFFFFFF;
  754. int i;
  755. int st_id = 0, size;
  756. size = 0x0FFFFFFF;
  757. again:
  758. for(i=0; i<mov->total_streams; i++) {
  759. /* printf("%8ld ", mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk]); */
  760. if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
  761. && (mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] < offset)) {
  762. /* printf("y"); */
  763. st_id = i;
  764. offset = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk];
  765. }
  766. /* else printf("n"); */
  767. }
  768. mov->streams[st_id]->next_chunk++;
  769. if(offset==0x0FFFFFFFFFFFFFFF)
  770. return -1;
  771. if(mov->next_chunk_offset < offset) /* some meta data */
  772. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  773. if(!mov->streams[st_id]->is_ff_stream) {
  774. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  775. offset = 0x0FFFFFFFFFFFFFFF;
  776. /* puts("*"); */
  777. goto again;
  778. }
  779. /* printf("\nchunk offset = %ld\n", offset); */
  780. /* now get the chunk size... */
  781. for(i=0; i<mov->total_streams; i++) {
  782. /* printf("%ld ", mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset); */
  783. if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
  784. && ((mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset) < size)) {
  785. /* printf("y"); */
  786. size = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset;
  787. }
  788. /* else printf("n"); */
  789. }
  790. /* printf("\nchunk size = %ld\n", size); */
  791. if(size == 0x0FFFFFFF)
  792. size = mov->mdat_size + mov->mdat_offset - offset;
  793. if(size < 0)
  794. return -1;
  795. if(size == 0)
  796. return -1;
  797. av_new_packet(pkt, size);
  798. pkt->stream_index = mov->streams[st_id]->ffindex;
  799. get_buffer(&s->pb, pkt->data, pkt->size);
  800. #ifdef DEBUG
  801. /*
  802. printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
  803. for(i=0; i<8; i++)
  804. printf("%02x ", pkt->data[i]);
  805. for(i=0; i<8; i++)
  806. printf("%c ", (pkt->data[i]) & 0x7F);
  807. puts("");
  808. */
  809. #endif
  810. mov->next_chunk_offset = offset + size;
  811. return 0;
  812. }
  813. static int mov_read_close(AVFormatContext *s)
  814. {
  815. int i;
  816. MOVContext *mov = s->priv_data;
  817. for(i=0; i<mov->total_streams; i++)
  818. mov_free_stream_context(mov->streams[i]);
  819. for(i=0; i<s->nb_streams; i++)
  820. free(s->streams[i]);
  821. free(mov);
  822. return 0;
  823. }
  824. AVFormat mov_format = {
  825. "mov",
  826. "QuickTime format",
  827. "video/quicktime",
  828. "mov",
  829. CODEC_ID_MP2,
  830. CODEC_ID_MJPEG,
  831. NULL,
  832. NULL,
  833. NULL,
  834. mov_read_header,
  835. mov_read_packet,
  836. mov_read_close,
  837. };
  838. AVFormat mp4_format = {
  839. "mp4",
  840. "MPEG4 file format",
  841. "video/mpeg4",
  842. "mp4",
  843. CODEC_ID_MP2,
  844. CODEC_ID_MJPEG,
  845. NULL,
  846. NULL,
  847. NULL,
  848. mov_read_header,
  849. mov_read_packet,
  850. mov_read_close,
  851. };