You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1348 lines
44KB

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