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.

1223 lines
40KB

  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') }, /* OpenDiVX *//* yeah ! */
  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. { 0, 0 },
  104. };
  105. static const CodecTag mov_audio_tags[] = {
  106. /* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
  107. { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
  108. { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') }, /* 8 bits */
  109. { CODEC_ID_PCM_U8, 0x20776172 }, /* 8 bits unsigned */
  110. { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */
  111. { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */
  112. { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */
  113. { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
  114. { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
  115. { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
  116. { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
  117. /* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
  118. /* MP4 tags */
  119. /* { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, *//* MPEG 4 AAC or audio ? */
  120. /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
  121. { 0, 0 },
  122. };
  123. /* the QuickTime file format is quite convoluted...
  124. * it has lots of index tables, each indexing something in another one...
  125. * Here we just use what is needed to read the chunks
  126. */
  127. typedef struct MOV_sample_to_chunk_tbl {
  128. long first;
  129. long count;
  130. long id;
  131. } MOV_sample_to_chunk_tbl;
  132. typedef struct MOVStreamContext {
  133. int ffindex; /* the ffmpeg stream id */
  134. int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
  135. long next_chunk;
  136. long chunk_count;
  137. INT64 *chunk_offsets;
  138. long sample_to_chunk_sz;
  139. MOV_sample_to_chunk_tbl *sample_to_chunk;
  140. long sample_to_chunk_index;
  141. long sample_size;
  142. long sample_count;
  143. long *sample_sizes;
  144. long time_scale;
  145. long current_sample;
  146. long left_in_chunk; /* how many samples before next chunk */
  147. } MOVStreamContext;
  148. typedef struct MOVContext {
  149. 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) */
  150. AVFormatContext *fc;
  151. long time_scale;
  152. int found_moov; /* when both 'moov' and 'mdat' sections has been found */
  153. int found_mdat; /* we suppose we have enough data to read the file */
  154. INT64 mdat_size;
  155. INT64 mdat_offset;
  156. int total_streams;
  157. /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
  158. * but we need the info to be able to skip data from those streams in the 'mdat' section
  159. */
  160. MOVStreamContext *streams[MAX_STREAMS];
  161. INT64 next_chunk_offset;
  162. int partial; /* != 0 : there is still to read in the current chunk (=id of the stream + 1) */
  163. } MOVContext;
  164. struct MOVParseTableEntry;
  165. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  166. /* those functions parse an atom */
  167. /* return code:
  168. 1: found what I wanted, exit
  169. 0: continue to parse next atom
  170. -1: error occured, exit
  171. */
  172. typedef int (*mov_parse_function)(const struct MOVParseTableEntry *parse_table,
  173. ByteIOContext *pb,
  174. UINT32 atom_type,
  175. INT64 atom_offset, /* after the size and type field (and eventually the extended size) */
  176. INT64 atom_size, /* total size (excluding the size and type fields) */
  177. void *param);
  178. /* links atom IDs to parse functions */
  179. typedef struct MOVParseTableEntry {
  180. UINT32 type;
  181. mov_parse_function func;
  182. } MOVParseTableEntry;
  183. static int parse_leaf(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  184. {
  185. #ifdef DEBUG
  186. print_atom("leaf", atom_type, atom_offset, atom_size);
  187. #endif
  188. if(atom_size>1)
  189. url_fskip(pb, atom_size);
  190. /* url_seek(pb, atom_offset+atom_size, SEEK_SET); */
  191. return 0;
  192. }
  193. static int parse_default(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  194. {
  195. UINT32 type, foo=0;
  196. UINT64 offset, size;
  197. UINT64 total_size = 0;
  198. int i;
  199. int err = 0;
  200. foo=0;
  201. #ifdef DEBUG
  202. print_atom("default", atom_type, atom_offset, atom_size);
  203. debug_indent++;
  204. #endif
  205. offset = atom_offset;
  206. if(atom_size < 0)
  207. atom_size = 0x0FFFFFFFFFFFFFFF;
  208. while((total_size < atom_size) && !url_feof(pb) && !err) {
  209. size=atom_size;
  210. type=0L;
  211. if(atom_size >= 8) {
  212. size = get_be32(pb);
  213. type = get_le32(pb);
  214. }
  215. total_size += 8;
  216. offset+=8;
  217. // printf("type: %08lx sz: %08lx", type, size);
  218. if(size == 1) { /* 64 bit extended size */
  219. size = get_be64(pb);
  220. offset+=8;
  221. total_size+=8;
  222. size-=8;
  223. }
  224. if(size == 0)
  225. size = atom_size - total_size;
  226. size-=8;
  227. for(i=0; parse_table[i].type != 0L && parse_table[i].type != type; i++);
  228. // printf(" i=%ld\n", i);
  229. if (parse_table[i].type == 0) { /* skip leaf atoms data */
  230. // url_seek(pb, atom_offset+atom_size, SEEK_SET);
  231. #ifdef DEBUG
  232. print_atom("unknown", type, offset, size);
  233. #endif
  234. url_fskip(pb, size);
  235. } else
  236. err = (parse_table[i].func)(parse_table, pb, type, offset, size, param);
  237. offset+=size;
  238. total_size+=size;
  239. }
  240. #ifdef DEBUG
  241. debug_indent--;
  242. #endif
  243. return err;
  244. }
  245. static int parse_mvhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  246. {
  247. MOVContext *c;
  248. #ifdef DEBUG
  249. print_atom("mvhd", atom_type, atom_offset, atom_size);
  250. #endif
  251. c = (MOVContext *)param;
  252. get_byte(pb); /* version */
  253. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  254. get_be32(pb); /* creation time */
  255. get_be32(pb); /* modification time */
  256. c->time_scale = get_be32(pb); /* time scale */
  257. #ifdef DEBUG
  258. printf("time scale = %li\n", c->time_scale);
  259. #endif
  260. get_be32(pb); /* duration */
  261. get_be32(pb); /* preferred scale */
  262. get_be16(pb); /* preferred volume */
  263. url_fskip(pb, 10); /* reserved */
  264. url_fskip(pb, 36); /* display matrix */
  265. get_be32(pb); /* preview time */
  266. get_be32(pb); /* preview duration */
  267. get_be32(pb); /* poster time */
  268. get_be32(pb); /* selection time */
  269. get_be32(pb); /* selection duration */
  270. get_be32(pb); /* current time */
  271. get_be32(pb); /* next track ID */
  272. return 0;
  273. }
  274. /* this atom should contain all header atoms */
  275. static int parse_moov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  276. {
  277. int err;
  278. MOVContext *c;
  279. #ifdef DEBUG
  280. print_atom("moov", atom_type, atom_offset, atom_size);
  281. #endif
  282. c = (MOVContext *)param;
  283. err = parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
  284. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  285. /* so we don't parse the whole file if over a network */
  286. c->found_moov=1;
  287. if(c->found_mdat)
  288. return 1; /* found both, just go */
  289. return 0; /* now go for mdat */
  290. }
  291. /* this atom contains actual media data */
  292. static int parse_mdat(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  293. {
  294. MOVContext *c;
  295. #ifdef DEBUG
  296. print_atom("mdat", atom_type, atom_offset, atom_size);
  297. #endif
  298. c = (MOVContext *)param;
  299. if(atom_size == 0) /* wrong one (MP4) */
  300. return 0;
  301. c->found_mdat=1;
  302. c->mdat_offset = atom_offset;
  303. c->mdat_size = atom_size;
  304. if(c->found_moov)
  305. return 1; /* found both, just go */
  306. url_fskip(pb, atom_size);
  307. return 0; /* now go for moov */
  308. }
  309. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  310. /* like the files created with Adobe Premiere 5.0, for samples see */
  311. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  312. static int parse_wide(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  313. {
  314. int err;
  315. UINT32 type;
  316. #ifdef DEBUG
  317. print_atom("wide", atom_type, atom_offset, atom_size);
  318. debug_indent++;
  319. #endif
  320. if (atom_size < 8)
  321. return 0; /* continue */
  322. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  323. url_fskip(pb, atom_size - 4);
  324. return 0;
  325. }
  326. type = get_le32(pb);
  327. if (type != MKTAG('m', 'd', 'a', 't')) {
  328. url_fskip(pb, atom_size - 8);
  329. return 0;
  330. }
  331. err = parse_mdat(parse_table, pb, type, atom_offset + 8, atom_size - 8, param);
  332. #ifdef DEBUG
  333. debug_indent--;
  334. #endif
  335. return err;
  336. }
  337. static int parse_trak(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  338. {
  339. MOVContext *c;
  340. AVStream *st;
  341. MOVStreamContext *sc;
  342. #ifdef DEBUG
  343. print_atom("trak", atom_type, atom_offset, atom_size);
  344. #endif
  345. c = (MOVContext *)param;
  346. st = av_new_stream(c->fc, c->fc->nb_streams);
  347. if (!st) return -2;
  348. sc = av_malloc(sizeof(MOVStreamContext));
  349. sc->sample_to_chunk_index = -1;
  350. st->priv_data = sc;
  351. st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
  352. c->streams[c->fc->nb_streams-1] = sc;
  353. return parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
  354. }
  355. static int parse_tkhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  356. {
  357. MOVContext *c;
  358. AVStream *st;
  359. #ifdef DEBUG
  360. print_atom("tkhd", atom_type, atom_offset, atom_size);
  361. #endif
  362. c = (MOVContext *)param;
  363. st = c->fc->streams[c->fc->nb_streams-1];
  364. get_byte(pb); /* version */
  365. get_byte(pb); get_byte(pb);
  366. get_byte(pb); /* flags */
  367. /*
  368. MOV_TRACK_ENABLED 0x0001
  369. MOV_TRACK_IN_MOVIE 0x0002
  370. MOV_TRACK_IN_PREVIEW 0x0004
  371. MOV_TRACK_IN_POSTER 0x0008
  372. */
  373. get_be32(pb); /* creation time */
  374. get_be32(pb); /* modification time */
  375. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  376. get_be32(pb); /* reserved */
  377. get_be32(pb); /* duration */
  378. get_be32(pb); /* reserved */
  379. get_be32(pb); /* reserved */
  380. get_be16(pb); /* layer */
  381. get_be16(pb); /* alternate group */
  382. get_be16(pb); /* volume */
  383. get_be16(pb); /* reserved */
  384. url_fskip(pb, 36); /* display matrix */
  385. /* those are fixed-point */
  386. st->codec.width = get_be32(pb) >> 16; /* track width */
  387. st->codec.height = get_be32(pb) >> 16; /* track height */
  388. return 0;
  389. }
  390. static int parse_mdhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  391. {
  392. MOVContext *c;
  393. AVStream *st;
  394. #ifdef DEBUG
  395. print_atom("mdhd", atom_type, atom_offset, atom_size);
  396. #endif
  397. c = (MOVContext *)param;
  398. st = c->fc->streams[c->fc->nb_streams-1];
  399. get_byte(pb); /* version */
  400. get_byte(pb); get_byte(pb);
  401. get_byte(pb); /* flags */
  402. get_be32(pb); /* creation time */
  403. get_be32(pb); /* modification time */
  404. c->streams[c->total_streams]->time_scale = get_be32(pb);
  405. #ifdef DEBUG
  406. printf("track[%i].time_scale = %li\n", c->fc->nb_streams-1, c->streams[c->total_streams]->time_scale); /* time scale */
  407. #endif
  408. get_be32(pb); /* duration */
  409. get_be16(pb); /* language */
  410. get_be16(pb); /* quality */
  411. return 0;
  412. }
  413. static int parse_hdlr(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  414. {
  415. MOVContext *c;
  416. int len = 0;
  417. char *buf;
  418. UINT32 type;
  419. AVStream *st;
  420. UINT32 ctype;
  421. #ifdef DEBUG
  422. print_atom("hdlr", atom_type, atom_offset, atom_size);
  423. #endif
  424. c = (MOVContext *)param;
  425. st = c->fc->streams[c->fc->nb_streams-1];
  426. get_byte(pb); /* version */
  427. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  428. /* component type */
  429. ctype = get_le32(pb);
  430. type = get_le32(pb); /* component subtype */
  431. #ifdef DEBUG
  432. printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
  433. printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
  434. #endif
  435. #ifdef DEBUG
  436. /* XXX: yeah this is ugly... */
  437. if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
  438. if(type == MKTAG('v', 'i', 'd', 'e'))
  439. puts("hdlr: vide");
  440. else if(type == MKTAG('s', 'o', 'u', 'n'))
  441. puts("hdlr: soun");
  442. } else if(ctype == 0) { /* MP4 */
  443. if(type == MKTAG('v', 'i', 'd', 'e'))
  444. puts("hdlr: vide");
  445. else if(type == MKTAG('s', 'o', 'u', 'n'))
  446. puts("hdlr: soun");
  447. else if(type == MKTAG('o', 'd', 's', 'm'))
  448. puts("hdlr: odsm");
  449. else if(type == MKTAG('s', 'd', 's', 'm'))
  450. puts("hdlr: sdsm");
  451. } else puts("hdlr: meta");
  452. #endif
  453. if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
  454. /* helps parsing the string hereafter... */
  455. c->mp4 = 0;
  456. if(type == MKTAG('v', 'i', 'd', 'e'))
  457. st->codec.codec_type = CODEC_TYPE_VIDEO;
  458. else if(type == MKTAG('s', 'o', 'u', 'n'))
  459. st->codec.codec_type = CODEC_TYPE_AUDIO;
  460. } else if(ctype == 0) { /* MP4 */
  461. /* helps parsing the string hereafter... */
  462. c->mp4 = 1;
  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. }
  468. get_be32(pb); /* component manufacture */
  469. get_be32(pb); /* component flags */
  470. get_be32(pb); /* component flags mask */
  471. if(atom_size <= 24)
  472. return 0; /* nothing left to read */
  473. /* XXX: MP4 uses a C string, not a pascal one */
  474. /* component name */
  475. if(c->mp4) {
  476. /* .mp4: C string */
  477. while(get_byte(pb) && (++len < (atom_size - 24)));
  478. } else {
  479. /* .mov: PASCAL string */
  480. len = get_byte(pb);
  481. buf = av_malloc(len+1);
  482. get_buffer(pb, buf, len);
  483. buf[len] = '\0';
  484. #ifdef DEBUG
  485. printf("**buf='%s'\n", buf);
  486. #endif
  487. av_free(buf);
  488. }
  489. #if 0
  490. len = get_byte(pb);
  491. /* XXX: use a better heuristic */
  492. if(len < 32) {
  493. /* assume that it is a Pascal like string */
  494. buf = av_malloc(len+1);
  495. get_buffer(pb, buf, len);
  496. buf[len] = '\0';
  497. #ifdef DEBUG
  498. printf("**buf='%s'\n", buf);
  499. #endif
  500. av_free(buf);
  501. } else {
  502. /* MP4 string */
  503. for(;;) {
  504. if (len == 0)
  505. break;
  506. len = get_byte(pb);
  507. }
  508. }
  509. #endif
  510. return 0;
  511. }
  512. static int parse_stsd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  513. {
  514. MOVContext *c;
  515. int entries, size, samp_sz, frames_per_sample;
  516. UINT32 format;
  517. AVStream *st;
  518. #ifdef DEBUG
  519. print_atom("stsd", atom_type, atom_offset, atom_size);
  520. #endif
  521. c = (MOVContext *)param;
  522. st = c->fc->streams[c->fc->nb_streams-1];
  523. get_byte(pb); /* version */
  524. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  525. entries = get_be32(pb);
  526. while(entries--) {
  527. size = get_be32(pb); /* size */
  528. format = get_le32(pb); /* data format */
  529. get_be32(pb); /* reserved */
  530. get_be16(pb); /* reserved */
  531. get_be16(pb); /* index */
  532. /* if(format == MKTAG('m', 'p', '4', 'v')) */
  533. /* st->codec.codec_type=CODEC_TYPE_VIDEO; *//* force things (XXX: was this for .mp4 ?) */
  534. if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
  535. st->codec.codec_tag = format;
  536. st->codec.codec_id = codec_get_id(mov_video_tags, format);
  537. get_be16(pb); /* version */
  538. get_be16(pb); /* revision level */
  539. get_be32(pb); /* vendor */
  540. get_be32(pb); /* temporal quality */
  541. get_be32(pb); /* spacial quality */
  542. st->codec.width = get_be16(pb); /* width */
  543. st->codec.height = get_be16(pb); /* height */
  544. get_be32(pb); /* horiz resolution */
  545. get_be32(pb); /* vert resolution */
  546. get_be32(pb); /* data size, always 0 */
  547. frames_per_sample = get_be16(pb); /* frame per samples */
  548. #ifdef DEBUG
  549. printf("frames/samples = %d\n", frames_per_sample);
  550. #endif
  551. url_fskip(pb, 32); /* codec name */
  552. get_be16(pb); /* depth */
  553. get_be16(pb); /* colortable id */
  554. get_be16(pb); /* */
  555. get_be16(pb); /* */
  556. st->codec.sample_rate = 25 * FRAME_RATE_BASE;
  557. if(size > 16)
  558. url_fskip(pb, size-(16+24+18+32));
  559. } else {
  560. st->codec.codec_tag = format;
  561. get_be16(pb); /* version */
  562. get_be16(pb); /* revision level */
  563. get_be32(pb); /* vendor */
  564. st->codec.channels = get_be16(pb);/* channel count */
  565. samp_sz = get_be16(pb); /* sample size */
  566. #ifdef DEBUG
  567. if(samp_sz != 16)
  568. puts("!!! stsd: audio sample size is not 16 bit !");
  569. #endif
  570. st->codec.codec_id = codec_get_id(mov_audio_tags, format);
  571. /* handle specific s8 codec */
  572. if (st->codec.codec_id == CODEC_ID_PCM_S16BE && samp_sz == 8)
  573. st->codec.codec_id = CODEC_ID_PCM_S8;
  574. get_be16(pb); /* compression id = 0*/
  575. get_be16(pb); /* packet size = 0 */
  576. st->codec.sample_rate = ((get_be32(pb) >> 16));
  577. st->codec.bit_rate = 0;
  578. /* this is for .mp4 files */
  579. if(format == MKTAG('m', 'p', '4', 'v')) { /* XXX */
  580. st->codec.codec_type=CODEC_TYPE_VIDEO; /* force things */
  581. st->codec.codec_id = CODEC_ID_MPEG4;
  582. st->codec.frame_rate = 25 * FRAME_RATE_BASE;
  583. st->codec.bit_rate = 100000;
  584. }
  585. // if(format == MKTAG('m', 'p', '4', 'a')) { /* XXX */
  586. // st->codec.codec_type=CODEC_TYPE_AUDIO; /* force things */
  587. // st->codec.codec_id = CODEC_ID_AAC;
  588. //// st->codec.frame_rate = 25 * FRAME_RATE_BASE;
  589. //// st->codec.bit_rate = 100000;
  590. // }
  591. #if 0
  592. get_be16(pb); get_be16(pb); /* */
  593. get_be16(pb); /* */
  594. get_be16(pb); /* */
  595. get_be16(pb); /* */
  596. get_be16(pb); /* */
  597. #endif
  598. if(size > 16)
  599. url_fskip(pb, size-(16+20));
  600. }
  601. #ifdef DEBUG
  602. printf("4CC= %c%c%c%c\n", *((char *)&format), ((char *)&format)[1], ((char *)&format)[2], ((char *)&format)[3]);
  603. #endif
  604. }
  605. /*
  606. if(len) {
  607. buf = av_malloc(len+1);
  608. get_buffer(pb, buf, len);
  609. buf[len] = '\0';
  610. puts(buf);
  611. av_free(buf);
  612. }
  613. */
  614. return 0;
  615. }
  616. static int parse_stco(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  617. {
  618. MOVContext *c;
  619. int entries, i;
  620. AVStream *st;
  621. MOVStreamContext *sc;
  622. #ifdef DEBUG
  623. print_atom("stco", atom_type, atom_offset, atom_size);
  624. #endif
  625. c = (MOVContext *)param;
  626. st = c->fc->streams[c->fc->nb_streams-1];
  627. sc = (MOVStreamContext *)st->priv_data;
  628. get_byte(pb); /* version */
  629. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  630. entries = get_be32(pb);
  631. sc->chunk_count = entries;
  632. sc->chunk_offsets = av_malloc(entries * sizeof(INT64));
  633. if(atom_type == MKTAG('s', 't', 'c', 'o')) {
  634. for(i=0; i<entries; i++) {
  635. sc->chunk_offsets[i] = get_be32(pb);
  636. /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
  637. }
  638. } else if(atom_type == MKTAG('c', 'o', '6', '4')) {
  639. for(i=0; i<entries; i++) {
  640. sc->chunk_offsets[i] = get_be64(pb);
  641. /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
  642. }
  643. } else
  644. return -1;
  645. return 0;
  646. }
  647. static int parse_stsc(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  648. {
  649. MOVContext *c;
  650. int entries, i;
  651. AVStream *st;
  652. MOVStreamContext *sc;
  653. #ifdef DEBUG
  654. print_atom("stsc", atom_type, atom_offset, atom_size);
  655. #endif
  656. c = (MOVContext *)param;
  657. st = c->fc->streams[c->fc->nb_streams-1];
  658. sc = (MOVStreamContext *)st->priv_data;
  659. get_byte(pb); /* version */
  660. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  661. entries = get_be32(pb);
  662. #ifdef DEBUG
  663. printf("track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  664. #endif
  665. sc->sample_to_chunk_sz = entries;
  666. sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
  667. for(i=0; i<entries; i++) {
  668. sc->sample_to_chunk[i].first = get_be32(pb);
  669. sc->sample_to_chunk[i].count = get_be32(pb);
  670. sc->sample_to_chunk[i].id = get_be32(pb);
  671. #ifdef DEBUG
  672. /* 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); */
  673. #endif
  674. }
  675. return 0;
  676. }
  677. static int parse_stsz(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  678. {
  679. MOVContext *c;
  680. int entries, i;
  681. AVStream *st;
  682. MOVStreamContext *sc;
  683. #ifdef DEBUG
  684. print_atom("stsz", atom_type, atom_offset, atom_size);
  685. #endif
  686. c = (MOVContext *)param;
  687. st = c->fc->streams[c->fc->nb_streams-1];
  688. sc = (MOVStreamContext *)st->priv_data;
  689. get_byte(pb); /* version */
  690. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  691. sc->sample_size = get_be32(pb);
  692. entries = get_be32(pb);
  693. sc->sample_count = entries;
  694. #ifdef DEBUG
  695. printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
  696. #endif
  697. if(sc->sample_size)
  698. return 0; /* there isn't any table following */
  699. sc->sample_sizes = av_malloc(entries * sizeof(long));
  700. for(i=0; i<entries; i++) {
  701. sc->sample_sizes[i] = get_be32(pb);
  702. #ifdef DEBUG
  703. /* printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
  704. #endif
  705. }
  706. return 0;
  707. }
  708. static int parse_stts(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  709. {
  710. MOVContext *c;
  711. int entries, i;
  712. AVStream *st;
  713. MOVStreamContext *sc;
  714. #ifdef DEBUG
  715. print_atom("stts", atom_type, atom_offset, atom_size);
  716. #endif
  717. c = (MOVContext *)param;
  718. st = c->fc->streams[c->fc->nb_streams-1];
  719. sc = (MOVStreamContext *)st->priv_data;
  720. get_byte(pb); /* version */
  721. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  722. entries = get_be32(pb);
  723. #ifdef DEBUG
  724. printf("track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  725. #endif
  726. for(i=0; i<entries; i++) {
  727. int sample_duration;
  728. get_be32(pb);
  729. sample_duration = get_be32(pb);
  730. if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
  731. st->codec.frame_rate = FRAME_RATE_BASE * c->streams[c->total_streams]->time_scale;
  732. if (sample_duration)
  733. st->codec.frame_rate /= sample_duration;
  734. #ifdef DEBUG
  735. printf("VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
  736. #endif
  737. }
  738. }
  739. return 0;
  740. }
  741. #ifdef CONFIG_ZLIB
  742. static int null_read_packet(void *opaque, UINT8 *buf, int buf_size)
  743. {
  744. return -1;
  745. }
  746. static int parse_cmov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
  747. {
  748. MOVContext *c;
  749. ByteIOContext ctx;
  750. char *cmov_data;
  751. unsigned char *moov_data; /* uncompressed data */
  752. long cmov_len, moov_len;
  753. int ret;
  754. #ifdef DEBUG
  755. print_atom("cmov", atom_type, atom_offset, atom_size);
  756. #endif
  757. c = (MOVContext *)param;
  758. get_be32(pb); /* dcom atom */
  759. if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
  760. return -1;
  761. if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
  762. puts("unknown compression for cmov atom !");
  763. return -1;
  764. }
  765. get_be32(pb); /* cmvd atom */
  766. if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
  767. return -1;
  768. moov_len = get_be32(pb); /* uncompressed size */
  769. cmov_len = atom_size - 6 * 4;
  770. cmov_data = av_malloc(cmov_len);
  771. if (!cmov_data)
  772. return -1;
  773. moov_data = av_malloc(moov_len);
  774. if (!moov_data) {
  775. av_free(cmov_data);
  776. return -1;
  777. }
  778. get_buffer(pb, cmov_data, cmov_len);
  779. if(uncompress (moov_data, &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  780. return -1;
  781. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
  782. return -1;
  783. ctx.buf_end = ctx.buffer + moov_len;
  784. ret = parse_default(parse_table, &ctx, MKTAG( 'm', 'o', 'o', 'v' ), 0, moov_len, param);
  785. av_free(moov_data);
  786. av_free(cmov_data);
  787. return ret;
  788. }
  789. #endif
  790. static const MOVParseTableEntry mov_default_parse_table[] = {
  791. /* mp4 atoms */
  792. { MKTAG( 'm', 'p', '4', 'a' ), parse_default },
  793. { MKTAG( 'c', 'o', '6', '4' ), parse_stco },
  794. { MKTAG( 's', 't', 'c', 'o' ), parse_stco },
  795. { MKTAG( 'c', 'r', 'h', 'd' ), parse_default },
  796. { MKTAG( 'c', 't', 't', 's' ), parse_leaf },
  797. { MKTAG( 'c', 'p', 'r', 't' ), parse_default },
  798. { MKTAG( 'u', 'r', 'l', ' ' ), parse_leaf },
  799. { MKTAG( 'u', 'r', 'n', ' ' ), parse_leaf },
  800. { MKTAG( 'd', 'i', 'n', 'f' ), parse_default },
  801. { MKTAG( 'd', 'r', 'e', 'f' ), parse_leaf },
  802. { MKTAG( 's', 't', 'd', 'p' ), parse_default },
  803. { MKTAG( 'e', 's', 'd', 's' ), parse_default },
  804. { MKTAG( 'e', 'd', 't', 's' ), parse_default },
  805. { MKTAG( 'e', 'l', 's', 't' ), parse_leaf },
  806. { MKTAG( 'u', 'u', 'i', 'd' ), parse_default },
  807. { MKTAG( 'f', 'r', 'e', 'e' ), parse_leaf },
  808. { MKTAG( 'h', 'd', 'l', 'r' ), parse_hdlr },
  809. { MKTAG( 'h', 'm', 'h', 'd' ), parse_leaf },
  810. { MKTAG( 'h', 'i', 'n', 't' ), parse_leaf },
  811. { MKTAG( 'n', 'm', 'h', 'd' ), parse_leaf },
  812. { MKTAG( 'm', 'p', '4', 's' ), parse_default },
  813. { MKTAG( 'm', 'd', 'i', 'a' ), parse_default },
  814. { MKTAG( 'm', 'd', 'a', 't' ), parse_mdat },
  815. { MKTAG( 'm', 'd', 'h', 'd' ), parse_mdhd },
  816. { MKTAG( 'm', 'i', 'n', 'f' ), parse_default },
  817. { MKTAG( 'm', 'o', 'o', 'v' ), parse_moov },
  818. { MKTAG( 'm', 'v', 'h', 'd' ), parse_mvhd },
  819. { MKTAG( 'i', 'o', 'd', 's' ), parse_leaf },
  820. { MKTAG( 'o', 'd', 'h', 'd' ), parse_default },
  821. { MKTAG( 'm', 'p', 'o', 'd' ), parse_leaf },
  822. { MKTAG( 's', 't', 's', 'd' ), parse_stsd },
  823. { MKTAG( 's', 't', 's', 'z' ), parse_stsz },
  824. { MKTAG( 's', 't', 'b', 'l' ), parse_default },
  825. { MKTAG( 's', 't', 's', 'c' ), parse_stsc },
  826. { MKTAG( 's', 'd', 'h', 'd' ), parse_default },
  827. { MKTAG( 's', 't', 's', 'h' ), parse_default },
  828. { MKTAG( 's', 'k', 'i', 'p' ), parse_default },
  829. { MKTAG( 's', 'm', 'h', 'd' ), parse_leaf },
  830. { MKTAG( 'd', 'p', 'n', 'd' ), parse_leaf },
  831. { MKTAG( 's', 't', 's', 's' ), parse_leaf },
  832. { MKTAG( 's', 't', 't', 's' ), parse_stts },
  833. { MKTAG( 't', 'r', 'a', 'k' ), parse_trak },
  834. { MKTAG( 't', 'k', 'h', 'd' ), parse_tkhd },
  835. { MKTAG( 't', 'r', 'e', 'f' ), parse_default }, /* not really */
  836. { MKTAG( 'u', 'd', 't', 'a' ), parse_leaf },
  837. { MKTAG( 'v', 'm', 'h', 'd' ), parse_leaf },
  838. { MKTAG( 'm', 'p', '4', 'v' ), parse_default },
  839. /* extra mp4 */
  840. { MKTAG( 'M', 'D', 'E', 'S' ), parse_leaf },
  841. /* QT atoms */
  842. { MKTAG( 'c', 'h', 'a', 'p' ), parse_leaf },
  843. { MKTAG( 'c', 'l', 'i', 'p' ), parse_default },
  844. { MKTAG( 'c', 'r', 'g', 'n' ), parse_leaf },
  845. { MKTAG( 'k', 'm', 'a', 't' ), parse_leaf },
  846. { MKTAG( 'm', 'a', 't', 't' ), parse_default },
  847. { MKTAG( 'r', 'd', 'r', 'f' ), parse_leaf },
  848. { MKTAG( 'r', 'm', 'd', 'a' ), parse_default },
  849. { MKTAG( 'r', 'm', 'd', 'r' ), parse_leaf },
  850. //{ MKTAG( 'r', 'm', 'q', 'u' ), parse_leaf },
  851. { MKTAG( 'r', 'm', 'r', 'a' ), parse_default },
  852. { MKTAG( 's', 'c', 'p', 't' ), parse_leaf },
  853. { MKTAG( 's', 'y', 'n', 'c' ), parse_leaf },
  854. { MKTAG( 's', 's', 'r', 'c' ), parse_leaf },
  855. { MKTAG( 't', 'c', 'm', 'd' ), parse_leaf },
  856. { MKTAG( 'w', 'i', 'd', 'e' ), parse_wide }, /* place holder */
  857. #ifdef CONFIG_ZLIB
  858. { MKTAG( 'c', 'm', 'o', 'v' ), parse_cmov },
  859. #else
  860. { MKTAG( 'c', 'm', 'o', 'v' ), parse_leaf },
  861. #endif
  862. { 0L, parse_leaf }
  863. };
  864. static void mov_free_stream_context(MOVStreamContext *sc)
  865. {
  866. if(sc) {
  867. av_free(sc->chunk_offsets);
  868. av_free(sc->sample_to_chunk);
  869. av_free(sc);
  870. }
  871. }
  872. /* XXX: is it suffisant ? */
  873. static int mov_probe(AVProbeData *p)
  874. {
  875. int offset;
  876. /* check file header */
  877. if (p->buf_size <= 12)
  878. return 0;
  879. if ((p->buf[4] == 'm' && p->buf[5] == 'o' &&
  880. p->buf[6] == 'o' && p->buf[7] == 'v') ||
  881. (p->buf[4] == 'w' && p->buf[5] == 'i' &&
  882. p->buf[6] == 'd' && p->buf[7] == 'e') ||
  883. (p->buf[4] == 'f' && p->buf[5] == 'r' &&
  884. p->buf[6] == 'e' && p->buf[7] == 'e') ||
  885. (p->buf[4] == 'm' && p->buf[5] == 'd' &&
  886. p->buf[6] == 'a' && p->buf[7] == 't'))
  887. return AVPROBE_SCORE_MAX;
  888. if (p->buf[4] == 'f' && p->buf[5] == 't' &&
  889. p->buf[6] == 'y' && p->buf[7] == 'p') {
  890. offset = p->buf[0]; offset <<= 8;
  891. offset |= p->buf[1]; offset <<= 8;
  892. offset |= p->buf[2]; offset <<= 8;
  893. offset |= p->buf[3];
  894. if (offset > p->buf_size)
  895. return 0;
  896. if ((p->buf[offset+4] == 'm' && p->buf[offset+5] == 'o' &&
  897. p->buf[offset+6] == 'o' && p->buf[offset+7] == 'v') ||
  898. (p->buf[offset+4] == 'w' && p->buf[offset+5] == 'i' &&
  899. p->buf[offset+6] == 'd' && p->buf[offset+7] == 'e') ||
  900. (p->buf[offset+4] == 'm' && p->buf[offset+5] == 'd' &&
  901. p->buf[offset+6] == 'a' && p->buf[offset+7] == 't'))
  902. return AVPROBE_SCORE_MAX;
  903. }
  904. return 0;
  905. }
  906. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  907. {
  908. MOVContext *mov = s->priv_data;
  909. ByteIOContext *pb = &s->pb;
  910. int i, j, nb, err;
  911. INT64 size;
  912. mov->fc = s;
  913. #if 0
  914. /* XXX: I think we should auto detect */
  915. if(s->iformat->name[1] == 'p')
  916. mov->mp4 = 1;
  917. #endif
  918. if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  919. size = url_filesize(url_fileno(pb));
  920. else
  921. size = 0x7FFFFFFFFFFFFFFF;
  922. #ifdef DEBUG
  923. printf("filesz=%Ld\n", size);
  924. #endif
  925. /* check MOV header */
  926. err = parse_default(mov_default_parse_table, pb, 0L, 0LL, size, mov);
  927. if(err<0 || (!mov->found_moov || !mov->found_mdat)) {
  928. puts("header not found !!!");
  929. exit(1);
  930. }
  931. #ifdef DEBUG
  932. printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
  933. #endif
  934. /* some cleanup : make sure we are on the mdat atom */
  935. if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
  936. url_fseek(pb, mov->mdat_offset, SEEK_SET);
  937. mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
  938. #ifdef DEBUG
  939. printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
  940. #endif
  941. #ifdef DEBUG
  942. printf("streams= %d\n", s->nb_streams);
  943. #endif
  944. mov->total_streams = nb = s->nb_streams;
  945. #if 1
  946. for(i=0; i<s->nb_streams;) {
  947. if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
  948. av_free(s->streams[i]);
  949. for(j=i+1; j<s->nb_streams; j++)
  950. s->streams[j-1] = s->streams[j];
  951. s->nb_streams--;
  952. } else
  953. i++;
  954. }
  955. for(i=0; i<s->nb_streams;i++) {
  956. MOVStreamContext *sc;
  957. sc = (MOVStreamContext *)s->streams[i]->priv_data;
  958. sc->ffindex = i;
  959. sc->is_ff_stream = 1;
  960. }
  961. #endif
  962. #ifdef DEBUG
  963. printf("real streams= %d\n", s->nb_streams);
  964. #endif
  965. return 0;
  966. }
  967. /* Yes, this is ugly... I didn't write the specs of QT :p */
  968. /* XXX:remove useless commented code sometime */
  969. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  970. {
  971. MOVContext *mov = s->priv_data;
  972. INT64 offset = 0x0FFFFFFFFFFFFFFF;
  973. int i;
  974. int st_id = 0, size;
  975. size = 0x0FFFFFFF;
  976. #ifdef MOV_SPLIT_CHUNKS
  977. if (mov->partial) {
  978. int idx;
  979. st_id = mov->partial - 1;
  980. idx = mov->streams[st_id]->sample_to_chunk_index;
  981. if (idx < 0) return 0;
  982. size = mov->streams[st_id]->sample_sizes[mov->streams[st_id]->current_sample];
  983. mov->streams[st_id]->current_sample++;
  984. mov->streams[st_id]->left_in_chunk--;
  985. if(mov->streams[st_id]->left_in_chunk <= 0)
  986. mov->partial = 0;
  987. offset = mov->next_chunk_offset;
  988. /* extract the sample */
  989. goto readchunk;
  990. }
  991. #endif
  992. again:
  993. for(i=0; i<mov->total_streams; i++) {
  994. if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
  995. && (mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] < offset)) {
  996. st_id = i;
  997. offset = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk];
  998. }
  999. }
  1000. mov->streams[st_id]->next_chunk++;
  1001. if(offset==0x0FFFFFFFFFFFFFFF)
  1002. return -1;
  1003. if(mov->next_chunk_offset < offset) { /* some meta data */
  1004. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1005. mov->next_chunk_offset = offset;
  1006. }
  1007. //printf("chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
  1008. if(!mov->streams[st_id]->is_ff_stream) {
  1009. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1010. mov->next_chunk_offset = offset;
  1011. offset = 0x0FFFFFFFFFFFFFFF;
  1012. goto again;
  1013. }
  1014. /* now get the chunk size... */
  1015. for(i=0; i<mov->total_streams; i++) {
  1016. if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
  1017. && ((mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset) < size)) {
  1018. size = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset;
  1019. }
  1020. }
  1021. #ifdef MOV_SPLIT_CHUNKS
  1022. /* split chunks into samples */
  1023. if(mov->streams[st_id]->sample_size == 0) {
  1024. int idx;
  1025. idx = mov->streams[st_id]->sample_to_chunk_index;
  1026. if ((idx + 1 < mov->streams[st_id]->sample_to_chunk_sz)
  1027. && (mov->streams[st_id]->next_chunk >= mov->streams[st_id]->sample_to_chunk[idx + 1].first))
  1028. idx++;
  1029. mov->streams[st_id]->sample_to_chunk_index = idx;
  1030. if(idx >= 0 && mov->streams[st_id]->sample_to_chunk[idx].count != 1) {
  1031. mov->partial = st_id+1;
  1032. /* we'll have to get those samples before next chunk */
  1033. mov->streams[st_id]->left_in_chunk = (mov->streams[st_id]->sample_to_chunk[idx].count) - 1;
  1034. size = mov->streams[st_id]->sample_sizes[mov->streams[st_id]->current_sample];
  1035. }
  1036. mov->streams[st_id]->current_sample++;
  1037. }
  1038. #endif
  1039. readchunk:
  1040. //printf("chunk: [%i] %lli -> %lli (%i)\n", st_id, offset, offset + size, size);
  1041. if(size == 0x0FFFFFFF)
  1042. size = mov->mdat_size + mov->mdat_offset - offset;
  1043. if(size < 0)
  1044. return -1;
  1045. if(size == 0)
  1046. return -1;
  1047. av_new_packet(pkt, size);
  1048. pkt->stream_index = mov->streams[st_id]->ffindex;
  1049. get_buffer(&s->pb, pkt->data, pkt->size);
  1050. #ifdef DEBUG
  1051. /*
  1052. printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
  1053. for(i=0; i<8; i++)
  1054. printf("%02x ", pkt->data[i]);
  1055. for(i=0; i<8; i++)
  1056. printf("%c ", (pkt->data[i]) & 0x7F);
  1057. puts("");
  1058. */
  1059. #endif
  1060. mov->next_chunk_offset = offset + size;
  1061. return 0;
  1062. }
  1063. static int mov_read_close(AVFormatContext *s)
  1064. {
  1065. int i;
  1066. MOVContext *mov = s->priv_data;
  1067. for(i=0; i<mov->total_streams; i++)
  1068. mov_free_stream_context(mov->streams[i]);
  1069. for(i=0; i<s->nb_streams; i++)
  1070. av_free(s->streams[i]);
  1071. return 0;
  1072. }
  1073. static AVInputFormat mov_iformat = {
  1074. "mov",
  1075. "QuickTime/MPEG4 format",
  1076. sizeof(MOVContext),
  1077. mov_probe,
  1078. mov_read_header,
  1079. mov_read_packet,
  1080. mov_read_close,
  1081. };
  1082. int mov_init(void)
  1083. {
  1084. av_register_input_format(&mov_iformat);
  1085. return 0;
  1086. }