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.

1352 lines
44KB

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