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.

961 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. 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. 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)(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(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(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 == NULL) { /* 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(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(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(MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  274. {
  275. int err;
  276. MOVContext *c;
  277. #ifdef DEBUG
  278. print_atom("mdat", atom_type, atom_offset, atom_size);
  279. #endif
  280. c = (MOVContext *)param;
  281. if(atom_size == 0) /* wrong one (MP4) */
  282. return 0;
  283. c->found_mdat=1;
  284. c->mdat_offset = atom_offset;
  285. c->mdat_size = atom_size;
  286. if(c->found_moov)
  287. return 1; /* found both, just go */
  288. url_fskip(pb, atom_size);
  289. return 0; /* now go for moov */
  290. }
  291. static int parse_trak(MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  292. {
  293. MOVContext *c;
  294. AVStream *st;
  295. MOVStreamContext *sc;
  296. #ifdef DEBUG
  297. print_atom("trak", atom_type, atom_offset, atom_size);
  298. #endif
  299. c = (MOVContext *)param;
  300. st = malloc(sizeof(AVStream));
  301. if (!st) return -2;
  302. memset(st, 0, sizeof(AVStream));
  303. c->fc->streams[c->fc->nb_streams] = st;
  304. sc = malloc(sizeof(MOVStreamContext));
  305. st->priv_data = sc;
  306. st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
  307. c->streams[c->fc->nb_streams++] = sc;
  308. return parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
  309. }
  310. static int parse_tkhd(MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  311. {
  312. MOVContext *c;
  313. AVStream *st;
  314. #ifdef DEBUG
  315. print_atom("tkhd", atom_type, atom_offset, atom_size);
  316. #endif
  317. c = (MOVContext *)param;
  318. st = c->fc->streams[c->fc->nb_streams-1];
  319. get_byte(pb); /* version */
  320. get_byte(pb); get_byte(pb);
  321. get_byte(pb); /* flags */
  322. /*
  323. MOV_TRACK_ENABLED 0x0001
  324. MOV_TRACK_IN_MOVIE 0x0002
  325. MOV_TRACK_IN_PREVIEW 0x0004
  326. MOV_TRACK_IN_POSTER 0x0008
  327. */
  328. get_be32(pb); /* creation time */
  329. get_be32(pb); /* modification time */
  330. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  331. get_be32(pb); /* reserved */
  332. get_be32(pb); /* duration */
  333. get_be32(pb); /* reserved */
  334. get_be32(pb); /* reserved */
  335. get_be16(pb); /* layer */
  336. get_be16(pb); /* alternate group */
  337. get_be16(pb); /* volume */
  338. get_be16(pb); /* reserved */
  339. url_fskip(pb, 36); /* display matrix */
  340. /* those are fixed-point */
  341. st->codec.width = get_be32(pb) >> 16; /* track width */
  342. st->codec.height = get_be32(pb) >> 16; /* track height */
  343. return 0;
  344. }
  345. static int parse_hdlr(MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  346. {
  347. MOVContext *c;
  348. int len;
  349. char *buf, ch;
  350. UINT32 type;
  351. AVStream *st;
  352. UINT32 ctype;
  353. #ifdef DEBUG
  354. print_atom("hdlr", atom_type, atom_offset, atom_size);
  355. #endif
  356. c = (MOVContext *)param;
  357. st = c->fc->streams[c->fc->nb_streams-1];
  358. get_byte(pb); /* version */
  359. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  360. /* component type */
  361. ctype = get_le32(pb);
  362. type = get_le32(pb); /* component subtype */
  363. #ifdef DEBUG
  364. printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], ctype);
  365. printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
  366. #endif
  367. #ifdef DEBUG
  368. /* XXX: yeah this is ugly... */
  369. if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
  370. if(type == MKTAG('v', 'i', 'd', 'e'))
  371. puts("hdlr: vide");
  372. else if(type == MKTAG('s', 'o', 'u', 'n'))
  373. puts("hdlr: soun");
  374. } else if(ctype == 0) { /* MP4 */
  375. if(type == MKTAG('v', 'i', 'd', 'e'))
  376. puts("hdlr: vide");
  377. else if(type == MKTAG('s', 'o', 'u', 'n'))
  378. puts("hdlr: soun");
  379. else if(type == MKTAG('o', 'd', 's', 'm'))
  380. puts("hdlr: odsm");
  381. else if(type == MKTAG('s', 'd', 's', 'm'))
  382. puts("hdlr: sdsm");
  383. } else puts("hdlr: meta");
  384. #endif
  385. if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
  386. if(type == MKTAG('v', 'i', 'd', 'e'))
  387. st->codec.codec_type = CODEC_TYPE_VIDEO;
  388. else if(type == MKTAG('s', 'o', 'u', 'n'))
  389. st->codec.codec_type = CODEC_TYPE_AUDIO;
  390. } else if(ctype == 0) { /* MP4 */
  391. if(type == MKTAG('v', 'i', 'd', 'e'))
  392. st->codec.codec_type = CODEC_TYPE_VIDEO;
  393. else if(type == MKTAG('s', 'o', 'u', 'n'))
  394. st->codec.codec_type = CODEC_TYPE_AUDIO;
  395. }
  396. get_be32(pb); /* component manufacture */
  397. get_be32(pb); /* component flags */
  398. get_be32(pb); /* component flags mask */
  399. if(atom_size <= 24)
  400. return 0; /* nothing left to read */
  401. /* XXX: MP4 uses a C string, not a pascal one */
  402. /* component name */
  403. if(c->mp4) {
  404. #ifdef DEBUG
  405. puts("MP4!!!");
  406. #endif
  407. while(ch=get_byte(pb));
  408. } else {
  409. len = get_byte(pb);
  410. if(len) {
  411. buf = malloc(len+1);
  412. get_buffer(pb, buf, len);
  413. buf[len] = '\0';
  414. #ifdef DEBUG
  415. puts(buf);
  416. #endif
  417. free(buf);
  418. }
  419. }
  420. return 0;
  421. }
  422. static int parse_stsd(MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  423. {
  424. MOVContext *c;
  425. int entries, size, samp_sz, frames_per_sample;
  426. char *buf;
  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 = %ld\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 = malloc(len+1);
  513. get_buffer(pb, buf, len);
  514. buf[len] = '\0';
  515. puts(buf);
  516. free(buf);
  517. }
  518. */
  519. return 0;
  520. }
  521. static int parse_stco(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 = 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(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 = 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(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 = 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. if(sc->chunk_offsets)
  681. free(sc->chunk_offsets);
  682. if(sc->sample_to_chunk)
  683. free(sc->sample_to_chunk);
  684. free(sc);
  685. }
  686. }
  687. int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  688. {
  689. MOVContext *mov;
  690. ByteIOContext *pb = &s->pb;
  691. UINT32 tag, tag1;
  692. int i, j, nb, bps, err;
  693. INT64 size;
  694. AVStream *st;
  695. mov = malloc(sizeof(MOVContext));
  696. if (!mov)
  697. return -1;
  698. memset(mov, 0, sizeof(MOVContext));
  699. s->priv_data = mov;
  700. mov->fc = s;
  701. if(s->format->name[1] == 'p')
  702. mov->mp4 = 1;
  703. if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  704. size = url_filesize(url_fileno(pb));
  705. else
  706. size = 0x7FFFFFFFFFFFFFFF;
  707. #ifdef DEBUG
  708. printf("filesz=%ld\n", size);
  709. #endif
  710. /* check MOV header */
  711. err = parse_default(mov_default_parse_table, pb, 0L, 0LL, size, mov);
  712. if(err<0 || (!mov->found_moov || !mov->found_mdat)) {
  713. puts("header not found !!!");
  714. exit(1);
  715. }
  716. #ifdef DEBUG
  717. printf("on_parse_exit_offset=%ld\n", url_ftell(pb));
  718. #endif
  719. /* some cleanup : make sure we are on the mdat atom */
  720. if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
  721. url_fseek(pb, mov->mdat_offset, SEEK_SET);
  722. mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
  723. #ifdef DEBUG
  724. printf("mdat_reset_offset=%ld\n", url_ftell(pb));
  725. #endif
  726. #ifdef DEBUG
  727. printf("streams= %ld\n", s->nb_streams);
  728. #endif
  729. mov->total_streams = nb = s->nb_streams;
  730. #if 1
  731. for(i=0; i<s->nb_streams;) {
  732. if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
  733. free(s->streams[i]);
  734. for(j=i+1; j<s->nb_streams; j++)
  735. s->streams[j-1] = s->streams[j];
  736. s->nb_streams--;
  737. } else
  738. i++;
  739. }
  740. for(i=0; i<s->nb_streams;i++) {
  741. MOVStreamContext *sc;
  742. sc = (MOVStreamContext *)s->streams[i]->priv_data;
  743. sc->ffindex = i;
  744. sc->is_ff_stream = 1;
  745. }
  746. #endif
  747. #ifdef DEBUG
  748. printf("real streams= %ld\n", s->nb_streams);
  749. #endif
  750. return 0;
  751. }
  752. /* Yes, this is ugly... I didn't write the specs of QT :p */
  753. /* XXX:remove useless commented code sometime */
  754. int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  755. {
  756. MOVContext *mov = s->priv_data;
  757. INT64 offset = 0x0FFFFFFFFFFFFFFF;
  758. int i, j;
  759. int st_id = 0, size;
  760. size = 0x0FFFFFFF;
  761. again:
  762. for(i=0; i<mov->total_streams; i++) {
  763. /* printf("%8ld ", mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk]); */
  764. if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
  765. && (mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] < offset)) {
  766. /* printf("y"); */
  767. st_id = i;
  768. offset = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk];
  769. }
  770. /* else printf("n"); */
  771. }
  772. mov->streams[st_id]->next_chunk++;
  773. if(offset==0x0FFFFFFFFFFFFFFF)
  774. return -1;
  775. if(mov->next_chunk_offset < offset) /* some meta data */
  776. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  777. if(!mov->streams[st_id]->is_ff_stream) {
  778. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  779. offset = 0x0FFFFFFFFFFFFFFF;
  780. /* puts("*"); */
  781. goto again;
  782. }
  783. /* printf("\nchunk offset = %ld\n", offset); */
  784. /* now get the chunk size... */
  785. for(i=0; i<mov->total_streams; i++) {
  786. /* printf("%ld ", mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset); */
  787. if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
  788. && ((mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset) < size)) {
  789. /* printf("y"); */
  790. size = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset;
  791. }
  792. /* else printf("n"); */
  793. }
  794. /* printf("\nchunk size = %ld\n", size); */
  795. if(size == 0x0FFFFFFF)
  796. size = mov->mdat_size + mov->mdat_offset - offset;
  797. if(size < 0)
  798. return -1;
  799. if(size == 0)
  800. return -1;
  801. av_new_packet(pkt, size);
  802. pkt->stream_index = mov->streams[st_id]->ffindex;
  803. get_buffer(&s->pb, pkt->data, pkt->size);
  804. #ifdef DEBUG
  805. /*
  806. printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
  807. for(i=0; i<8; i++)
  808. printf("%02x ", pkt->data[i]);
  809. for(i=0; i<8; i++)
  810. printf("%c ", (pkt->data[i]) & 0x7F);
  811. puts("");
  812. */
  813. #endif
  814. mov->next_chunk_offset = offset + size;
  815. return 0;
  816. }
  817. int mov_read_close(AVFormatContext *s)
  818. {
  819. int i;
  820. MOVContext *mov = s->priv_data;
  821. for(i=0; i<mov->total_streams; i++)
  822. mov_free_stream_context(mov->streams[i]);
  823. for(i=0; i<s->nb_streams; i++)
  824. free(s->streams[i]);
  825. free(mov);
  826. return 0;
  827. }
  828. AVFormat mov_format = {
  829. "mov",
  830. "QuickTime format",
  831. "video/quicktime",
  832. "mov",
  833. CODEC_ID_MP2,
  834. CODEC_ID_MJPEG,
  835. NULL,
  836. NULL,
  837. NULL,
  838. mov_read_header,
  839. mov_read_packet,
  840. mov_read_close,
  841. };
  842. AVFormat mp4_format = {
  843. "mp4",
  844. "MPEG4 file format",
  845. "video/mpeg4",
  846. "mp4",
  847. CODEC_ID_MP2,
  848. CODEC_ID_MJPEG,
  849. NULL,
  850. NULL,
  851. NULL,
  852. mov_read_header,
  853. mov_read_packet,
  854. mov_read_close,
  855. };