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.

1837 lines
60KB

  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 <limits.h>
  20. #include "avformat.h"
  21. #include "avi.h"
  22. #ifdef CONFIG_ZLIB
  23. #include <zlib.h>
  24. #endif
  25. /*
  26. * First version by Francois Revol revol@free.fr
  27. *
  28. * Features and limitations:
  29. * - reads most of the QT files I have (at least the structure),
  30. * the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement.
  31. * FIXED, Francois Revol, 07/17/2002
  32. * - ffmpeg has nearly none of the usual QuickTime codecs,
  33. * although I succesfully dumped raw and mp3 audio tracks off .mov files.
  34. * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
  35. * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes
  36. * (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at
  37. * http://mpeg.telecomitalialab.com/faq.htm
  38. * - the code is quite ugly... maybe I won't do it recursive next time :-)
  39. *
  40. * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
  41. * when coding this :) (it's a writer anyway)
  42. *
  43. * Reference documents:
  44. * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
  45. * Apple:
  46. * http://developer.apple.com/techpubs/quicktime/qtdevdocs/QTFF/qtff.html
  47. * http://developer.apple.com/techpubs/quicktime/qtdevdocs/PDF/QTFileFormat.pdf
  48. * QuickTime is a trademark of Apple (AFAIK :))
  49. */
  50. //#define DEBUG
  51. #ifdef DEBUG
  52. #include <stdio.h>
  53. #include <fcntl.h>
  54. #endif
  55. #include "qtpalette.h"
  56. /* allows chunk splitting - should work now... */
  57. /* in case you can't read a file, try commenting */
  58. #define MOV_SPLIT_CHUNKS
  59. /* Special handling for movies created with Minolta Dimaxe Xi*/
  60. /* this fix should not interfere with other .mov files, but just in case*/
  61. #define MOV_MINOLTA_FIX
  62. /* some streams in QT (and in MP4 mostly) aren't either video nor audio */
  63. /* so we first list them as this, then clean up the list of streams we give back, */
  64. /* getting rid of these */
  65. #define CODEC_TYPE_MOV_OTHER (enum CodecType) 2
  66. static const CodecTag mov_video_tags[] = {
  67. /* { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
  68. /* { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, *//* Uncompressed RGB */
  69. /* { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, *//* Uncompressed YUV422 */
  70. /* { CODEC_ID_RAWVIDEO, MKTAG('A', 'V', 'U', 'I') }, *//* YUV with alpha-channel (AVID Uncompressed) */
  71. /* Graphics */
  72. /* Animation */
  73. /* Apple video */
  74. /* Kodak Photo CD */
  75. { CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
  76. { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
  77. { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
  78. { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
  79. { CODEC_ID_MJPEG, MKTAG('A', 'V', 'D', 'J') }, /* MJPEG with alpha-channel (AVID JFIF meridien compressed) */
  80. /* { CODEC_ID_MJPEG, MKTAG('A', 'V', 'R', 'n') }, *//* MJPEG with alpha-channel (AVID ABVB/Truevision NuVista) */
  81. /* { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
  82. /* Sorenson video */
  83. { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') }, /* Sorenson Video v1 */
  84. { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, /* Sorenson Video v1 */
  85. { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', 'i') }, /* Sorenson Video v1 (from QT specs)*/
  86. { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') }, /* Sorenson Video v3 */
  87. { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
  88. { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
  89. { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D') },
  90. /* { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
  91. { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */
  92. { CODEC_ID_H263, MKTAG('s', '2', '6', '3') }, /* H263 ?? works */
  93. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
  94. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
  95. /* { CODEC_ID_DVVIDEO, MKTAG('A', 'V', 'd', 'v') }, *//* AVID dv */
  96. { CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') }, /* On2 VP3 */
  97. { CODEC_ID_RPZA, MKTAG('r', 'p', 'z', 'a') }, /* Apple Video (RPZA) */
  98. { CODEC_ID_CINEPAK, MKTAG('c', 'v', 'i', 'd') }, /* Cinepak */
  99. { CODEC_ID_8BPS, MKTAG('8', 'B', 'P', 'S') }, /* Planar RGB (8BPS) */
  100. { CODEC_ID_SMC, MKTAG('s', 'm', 'c', ' ') }, /* Apple Graphics (SMC) */
  101. { CODEC_ID_QTRLE, MKTAG('r', 'l', 'e', ' ') }, /* Apple Animation (RLE) */
  102. { CODEC_ID_NONE, 0 },
  103. };
  104. static const CodecTag mov_audio_tags[] = {
  105. /* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
  106. { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
  107. /* { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') },*/ /* 8 bits */
  108. { CODEC_ID_PCM_U8, MKTAG('r', 'a', 'w', ' ') }, /* 8 bits unsigned */
  109. { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */
  110. { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */
  111. { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */
  112. { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
  113. { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') }, /* Macintosh Audio Compression and Expansion 3:1 */
  114. { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') }, /* Macintosh Audio Compression and Expansion 6:1 */
  115. { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
  116. { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
  117. { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
  118. /* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
  119. /* MP4 tags */
  120. { CODEC_ID_MPEG4AAC, MKTAG('m', 'p', '4', 'a') }, /* MPEG-4 AAC */
  121. /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
  122. { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') }, /* AMR-NB 3gp */
  123. { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') }, /* AMR-WB 3gp */
  124. { CODEC_ID_AC3, MKTAG('m', 's', 0x20, 0x00) }, /* Dolby AC-3 */
  125. { CODEC_ID_NONE, 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 {
  137. uint32_t type;
  138. int64_t offset;
  139. int64_t size; /* total size (excluding the size and type fields) */
  140. } MOV_atom_t;
  141. typedef struct {
  142. int seed;
  143. int flags;
  144. int size;
  145. void* clrs;
  146. } MOV_ctab_t;
  147. typedef struct {
  148. uint8_t version;
  149. uint32_t flags; // 24bit
  150. /* 0x03 ESDescrTag */
  151. uint16_t es_id;
  152. #define MP4ODescrTag 0x01
  153. #define MP4IODescrTag 0x02
  154. #define MP4ESDescrTag 0x03
  155. #define MP4DecConfigDescrTag 0x04
  156. #define MP4DecSpecificDescrTag 0x05
  157. #define MP4SLConfigDescrTag 0x06
  158. #define MP4ContentIdDescrTag 0x07
  159. #define MP4SupplContentIdDescrTag 0x08
  160. #define MP4IPIPtrDescrTag 0x09
  161. #define MP4IPMPPtrDescrTag 0x0A
  162. #define MP4IPMPDescrTag 0x0B
  163. #define MP4RegistrationDescrTag 0x0D
  164. #define MP4ESIDIncDescrTag 0x0E
  165. #define MP4ESIDRefDescrTag 0x0F
  166. #define MP4FileIODescrTag 0x10
  167. #define MP4FileODescrTag 0x11
  168. #define MP4ExtProfileLevelDescrTag 0x13
  169. #define MP4ExtDescrTagsStart 0x80
  170. #define MP4ExtDescrTagsEnd 0xFE
  171. uint8_t stream_priority;
  172. /* 0x04 DecConfigDescrTag */
  173. uint8_t object_type_id;
  174. uint8_t stream_type;
  175. /* XXX: really streamType is
  176. * only 6bit, followed by:
  177. * 1bit upStream
  178. * 1bit reserved
  179. */
  180. uint32_t buffer_size_db; // 24
  181. uint32_t max_bitrate;
  182. uint32_t avg_bitrate;
  183. /* 0x05 DecSpecificDescrTag */
  184. uint8_t decoder_cfg_len;
  185. uint8_t *decoder_cfg;
  186. /* 0x06 SLConfigDescrTag */
  187. uint8_t sl_config_len;
  188. uint8_t *sl_config;
  189. } MOV_esds_t;
  190. struct MOVParseTableEntry;
  191. typedef struct MOVStreamContext {
  192. int ffindex; /* the ffmpeg stream id */
  193. int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
  194. long next_chunk;
  195. long chunk_count;
  196. int64_t *chunk_offsets;
  197. long sample_to_chunk_sz;
  198. MOV_sample_to_chunk_tbl *sample_to_chunk;
  199. long sample_to_chunk_index;
  200. long sample_size;
  201. long sample_count;
  202. long *sample_sizes;
  203. long keyframe_count;
  204. long *keyframes;
  205. int time_scale;
  206. long current_sample;
  207. long left_in_chunk; /* how many samples before next chunk */
  208. /* specific MPEG4 header which is added at the beginning of the stream */
  209. int header_len;
  210. uint8_t *header_data;
  211. MOV_esds_t esds;
  212. } MOVStreamContext;
  213. typedef struct MOVContext {
  214. 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) */
  215. AVFormatContext *fc;
  216. int time_scale;
  217. int duration; /* duration of the longest track */
  218. int found_moov; /* when both 'moov' and 'mdat' sections has been found */
  219. int found_mdat; /* we suppose we have enough data to read the file */
  220. int64_t mdat_size;
  221. int64_t mdat_offset;
  222. int total_streams;
  223. /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
  224. * but we need the info to be able to skip data from those streams in the 'mdat' section
  225. */
  226. MOVStreamContext *streams[MAX_STREAMS];
  227. int64_t next_chunk_offset;
  228. MOVStreamContext *partial; /* != 0 : there is still to read in the current chunk */
  229. int ctab_size;
  230. MOV_ctab_t **ctab; /* color tables */
  231. const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
  232. /* NOTE: for recursion save to/ restore from local variable! */
  233. AVPaletteControl palette_control;
  234. } MOVContext;
  235. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  236. /* those functions parse an atom */
  237. /* return code:
  238. 1: found what I wanted, exit
  239. 0: continue to parse next atom
  240. -1: error occured, exit
  241. */
  242. typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
  243. /* links atom IDs to parse functions */
  244. typedef struct MOVParseTableEntry {
  245. uint32_t type;
  246. mov_parse_function func;
  247. } MOVParseTableEntry;
  248. #ifdef DEBUG
  249. /*
  250. * XXX: static sux, even more in a multithreaded environment...
  251. * Avoid them. This is here just to help debugging.
  252. */
  253. static int debug_indent = 0;
  254. void print_atom(const char *str, MOV_atom_t atom)
  255. {
  256. unsigned int tag, i;
  257. tag = (unsigned int) atom.type;
  258. i=debug_indent;
  259. if(tag == 0) tag = MKTAG('N', 'U', 'L', 'L');
  260. while(i--)
  261. printf("|");
  262. printf("parse:");
  263. printf(" %s: tag=%c%c%c%c offset=0x%x size=0x%x\n",
  264. str, tag & 0xff,
  265. (tag >> 8) & 0xff,
  266. (tag >> 16) & 0xff,
  267. (tag >> 24) & 0xff,
  268. (unsigned int)atom.offset,
  269. (unsigned int)atom.size);
  270. assert((unsigned int)atom.size < 0x7fffffff);// catching errors
  271. }
  272. #else
  273. #define print_atom(a,b)
  274. #endif
  275. static int mov_read_leaf(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  276. {
  277. print_atom("leaf", atom);
  278. if (atom.size>1)
  279. url_fskip(pb, atom.size);
  280. /* url_seek(pb, atom_offset+atom.size, SEEK_SET); */
  281. return 0;
  282. }
  283. static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  284. {
  285. int64_t total_size = 0;
  286. MOV_atom_t a;
  287. int i;
  288. int err = 0;
  289. #ifdef DEBUG
  290. print_atom("default", atom);
  291. debug_indent++;
  292. #endif
  293. a.offset = atom.offset;
  294. if (atom.size < 0)
  295. atom.size = 0x7fffffffffffffffLL;
  296. while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
  297. a.size = atom.size;
  298. a.type=0L;
  299. if(atom.size >= 8) {
  300. a.size = get_be32(pb);
  301. a.type = get_le32(pb);
  302. }
  303. total_size += 8;
  304. a.offset += 8;
  305. //printf("type: %08x %.4s sz: %Lx %Lx %Lx\n", type, (char*)&type, size, atom.size, total_size);
  306. if (a.size == 1) { /* 64 bit extended size */
  307. a.size = get_be64(pb) - 8;
  308. a.offset += 8;
  309. total_size += 8;
  310. }
  311. if (a.size == 0) {
  312. a.size = atom.size - total_size;
  313. if (a.size <= 8)
  314. break;
  315. }
  316. for (i = 0; c->parse_table[i].type != 0L
  317. && c->parse_table[i].type != a.type; i++)
  318. /* empty */;
  319. a.size -= 8;
  320. // printf(" i=%ld\n", i);
  321. if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
  322. // url_seek(pb, atom.offset+atom.size, SEEK_SET);
  323. #ifdef DEBUG
  324. print_atom("unknown", a);
  325. #endif
  326. url_fskip(pb, a.size);
  327. } else {
  328. #ifdef DEBUG
  329. //char b[5] = { type & 0xff, (type >> 8) & 0xff, (type >> 16) & 0xff, (type >> 24) & 0xff, 0 };
  330. //print_atom(b, type, offset, size);
  331. #endif
  332. err = (c->parse_table[i].func)(c, pb, a);
  333. }
  334. a.offset += a.size;
  335. total_size += a.size;
  336. }
  337. if (!err && total_size < atom.size && atom.size < 0x7ffff) {
  338. //printf("RESET %Ld %Ld err:%d\n", atom.size, total_size, err);
  339. url_fskip(pb, atom.size - total_size);
  340. }
  341. #ifdef DEBUG
  342. debug_indent--;
  343. #endif
  344. return err;
  345. }
  346. static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  347. {
  348. unsigned int len;
  349. MOV_ctab_t *t;
  350. //url_fskip(pb, atom.size); // for now
  351. c->ctab = av_realloc(c->ctab, ++c->ctab_size);
  352. t = c->ctab[c->ctab_size];
  353. t->seed = get_be32(pb);
  354. t->flags = get_be16(pb);
  355. t->size = get_be16(pb) + 1;
  356. len = 2 * t->size * 4;
  357. if (len > 0) {
  358. t->clrs = av_malloc(len); // 16bit A R G B
  359. if (t->clrs)
  360. get_buffer(pb, t->clrs, len);
  361. }
  362. return 0;
  363. }
  364. static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  365. {
  366. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  367. int len = 0;
  368. uint32_t type;
  369. uint32_t ctype;
  370. print_atom("hdlr", atom);
  371. get_byte(pb); /* version */
  372. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  373. /* component type */
  374. ctype = get_le32(pb);
  375. type = get_le32(pb); /* component subtype */
  376. #ifdef DEBUG
  377. printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
  378. printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
  379. #endif
  380. #ifdef DEBUG
  381. /* XXX: yeah this is ugly... */
  382. if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
  383. if(type == MKTAG('v', 'i', 'd', 'e'))
  384. puts("hdlr: vide");
  385. else if(type == MKTAG('s', 'o', 'u', 'n'))
  386. puts("hdlr: soun");
  387. } else if(ctype == 0) { /* MP4 */
  388. if(type == MKTAG('v', 'i', 'd', 'e'))
  389. puts("hdlr: vide");
  390. else if(type == MKTAG('s', 'o', 'u', 'n'))
  391. puts("hdlr: soun");
  392. else if(type == MKTAG('o', 'd', 's', 'm'))
  393. puts("hdlr: odsm");
  394. else if(type == MKTAG('s', 'd', 's', 'm'))
  395. puts("hdlr: sdsm");
  396. } else puts("hdlr: meta");
  397. #endif
  398. if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
  399. /* helps parsing the string hereafter... */
  400. c->mp4 = 0;
  401. if(type == MKTAG('v', 'i', 'd', 'e'))
  402. st->codec.codec_type = CODEC_TYPE_VIDEO;
  403. else if(type == MKTAG('s', 'o', 'u', 'n'))
  404. st->codec.codec_type = CODEC_TYPE_AUDIO;
  405. } else if(ctype == 0) { /* MP4 */
  406. /* helps parsing the string hereafter... */
  407. c->mp4 = 1;
  408. if(type == MKTAG('v', 'i', 'd', 'e'))
  409. st->codec.codec_type = CODEC_TYPE_VIDEO;
  410. else if(type == MKTAG('s', 'o', 'u', 'n'))
  411. st->codec.codec_type = CODEC_TYPE_AUDIO;
  412. }
  413. get_be32(pb); /* component manufacture */
  414. get_be32(pb); /* component flags */
  415. get_be32(pb); /* component flags mask */
  416. if(atom.size <= 24)
  417. return 0; /* nothing left to read */
  418. /* XXX: MP4 uses a C string, not a pascal one */
  419. /* component name */
  420. if(c->mp4) {
  421. /* .mp4: C string */
  422. while(get_byte(pb) && (++len < (atom.size - 24)));
  423. } else {
  424. /* .mov: PASCAL string */
  425. #ifdef DEBUG
  426. char* buf;
  427. #endif
  428. len = get_byte(pb);
  429. #ifdef DEBUG
  430. buf = (uint8_t*) av_malloc(len+1);
  431. if (buf) {
  432. get_buffer(pb, buf, len);
  433. buf[len] = '\0';
  434. printf("**buf='%s'\n", buf);
  435. av_free(buf);
  436. } else
  437. #endif
  438. url_fskip(pb, len);
  439. }
  440. return 0;
  441. }
  442. static int mov_mp4_read_descr_len(ByteIOContext *pb)
  443. {
  444. int len = 0;
  445. int count = 4;
  446. while (count--) {
  447. int c = get_byte(pb);
  448. len = (len << 7) | (c & 0x7f);
  449. if (!(c & 0x80))
  450. break;
  451. }
  452. return len;
  453. }
  454. static int mov_mp4_read_descr(ByteIOContext *pb, int *tag)
  455. {
  456. int len;
  457. *tag = get_byte(pb);
  458. len = mov_mp4_read_descr_len(pb);
  459. #ifdef DEBUG
  460. printf("MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
  461. #endif
  462. return len;
  463. }
  464. static inline unsigned int get_be24(ByteIOContext *s)
  465. {
  466. unsigned int val;
  467. val = get_byte(s) << 16;
  468. val |= get_byte(s) << 8;
  469. val |= get_byte(s);
  470. return val;
  471. }
  472. static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  473. {
  474. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  475. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  476. int64_t start_pos = url_ftell(pb);
  477. int tag, len;
  478. print_atom("esds", atom);
  479. /* Well, broken but suffisant for some MP4 streams */
  480. get_be32(pb); /* version + flags */
  481. len = mov_mp4_read_descr(pb, &tag);
  482. if (tag == MP4ESDescrTag) {
  483. get_be16(pb); /* ID */
  484. get_byte(pb); /* priority */
  485. } else
  486. get_be16(pb); /* ID */
  487. len = mov_mp4_read_descr(pb, &tag);
  488. if (tag == MP4DecConfigDescrTag) {
  489. sc->esds.object_type_id = get_byte(pb);
  490. sc->esds.stream_type = get_byte(pb);
  491. sc->esds.buffer_size_db = get_be24(pb);
  492. sc->esds.max_bitrate = get_be32(pb);
  493. sc->esds.avg_bitrate = get_be32(pb);
  494. len = mov_mp4_read_descr(pb, &tag);
  495. //printf("LEN %d TAG %d m:%d a:%d\n", len, tag, sc->esds.max_bitrate, sc->esds.avg_bitrate);
  496. if (tag == MP4DecSpecificDescrTag) {
  497. #ifdef DEBUG
  498. printf("Specific MPEG4 header len=%d\n", len);
  499. #endif
  500. st->codec.extradata = (uint8_t*) av_mallocz(len);
  501. if (st->codec.extradata) {
  502. get_buffer(pb, st->codec.extradata, len);
  503. st->codec.extradata_size = len;
  504. }
  505. }
  506. }
  507. /* in any case, skip garbage */
  508. url_fskip(pb, atom.size - ((url_ftell(pb) - start_pos)));
  509. return 0;
  510. }
  511. /* this atom contains actual media data */
  512. static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  513. {
  514. print_atom("mdat", atom);
  515. if(atom.size == 0) /* wrong one (MP4) */
  516. return 0;
  517. c->found_mdat=1;
  518. c->mdat_offset = atom.offset;
  519. c->mdat_size = atom.size;
  520. if(c->found_moov)
  521. return 1; /* found both, just go */
  522. url_fskip(pb, atom.size);
  523. return 0; /* now go for moov */
  524. }
  525. /* this atom should contain all header atoms */
  526. static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  527. {
  528. int err;
  529. print_atom("moov", atom);
  530. err = mov_read_default(c, pb, atom);
  531. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  532. /* so we don't parse the whole file if over a network */
  533. c->found_moov=1;
  534. if(c->found_mdat)
  535. return 1; /* found both, just go */
  536. return 0; /* now go for mdat */
  537. }
  538. static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  539. {
  540. print_atom("mdhd", atom);
  541. get_byte(pb); /* version */
  542. get_byte(pb); get_byte(pb);
  543. get_byte(pb); /* flags */
  544. get_be32(pb); /* creation time */
  545. get_be32(pb); /* modification time */
  546. c->streams[c->total_streams]->time_scale = get_be32(pb);
  547. #ifdef DEBUG
  548. printf("track[%i].time_scale = %i\n", c->fc->nb_streams-1, c->streams[c->total_streams]->time_scale); /* time scale */
  549. #endif
  550. get_be32(pb); /* duration */
  551. get_be16(pb); /* language */
  552. get_be16(pb); /* quality */
  553. return 0;
  554. }
  555. static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  556. {
  557. print_atom("mvhd", atom);
  558. get_byte(pb); /* version */
  559. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  560. get_be32(pb); /* creation time */
  561. get_be32(pb); /* modification time */
  562. c->time_scale = get_be32(pb); /* time scale */
  563. #ifdef DEBUG
  564. printf("time scale = %i\n", c->time_scale);
  565. #endif
  566. c->duration = get_be32(pb); /* duration */
  567. get_be32(pb); /* preferred scale */
  568. get_be16(pb); /* preferred volume */
  569. url_fskip(pb, 10); /* reserved */
  570. url_fskip(pb, 36); /* display matrix */
  571. get_be32(pb); /* preview time */
  572. get_be32(pb); /* preview duration */
  573. get_be32(pb); /* poster time */
  574. get_be32(pb); /* selection time */
  575. get_be32(pb); /* selection duration */
  576. get_be32(pb); /* current time */
  577. get_be32(pb); /* next track ID */
  578. return 0;
  579. }
  580. static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  581. {
  582. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  583. // currently SVQ3 decoder expect full STSD header - so let's fake it
  584. // this should be fixed and just SMI header should be passed
  585. av_free(st->codec.extradata);
  586. st->codec.extradata_size = 0x5a + atom.size;
  587. st->codec.extradata = (uint8_t*) av_mallocz(st->codec.extradata_size);
  588. if (st->codec.extradata) {
  589. strcpy(st->codec.extradata, "SVQ3"); // fake
  590. get_buffer(pb, st->codec.extradata + 0x5a, atom.size);
  591. //printf("Reading SMI %Ld %s\n", atom.size, (char*)st->codec.extradata + 0x5a);
  592. } else
  593. url_fskip(pb, atom.size);
  594. return 0;
  595. }
  596. static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  597. {
  598. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  599. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  600. int entries, i;
  601. print_atom("stco", atom);
  602. get_byte(pb); /* version */
  603. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  604. entries = get_be32(pb);
  605. sc->chunk_count = entries;
  606. sc->chunk_offsets = (int64_t*) av_malloc(entries * sizeof(int64_t));
  607. if (!sc->chunk_offsets)
  608. return -1;
  609. if (atom.type == MKTAG('s', 't', 'c', 'o')) {
  610. for(i=0; i<entries; i++) {
  611. sc->chunk_offsets[i] = get_be32(pb);
  612. }
  613. } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
  614. for(i=0; i<entries; i++) {
  615. sc->chunk_offsets[i] = get_be64(pb);
  616. }
  617. } else
  618. return -1;
  619. #ifdef DEBUG
  620. /*
  621. for(i=0; i<entries; i++) {
  622. printf("chunk offset=0x%Lx\n", sc->chunk_offsets[i]);
  623. }
  624. */
  625. #endif
  626. return 0;
  627. }
  628. static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  629. {
  630. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  631. //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  632. int entries, frames_per_sample;
  633. uint32_t format;
  634. /* for palette traversal */
  635. int color_depth;
  636. int color_start;
  637. int color_count;
  638. int color_end;
  639. int color_index;
  640. int color_dec;
  641. int color_greyscale;
  642. unsigned char *color_table;
  643. int j;
  644. unsigned char r, g, b;
  645. print_atom("stsd", atom);
  646. get_byte(pb); /* version */
  647. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  648. entries = get_be32(pb);
  649. while(entries--) { //Parsing Sample description table
  650. enum CodecID id;
  651. int size = get_be32(pb); /* size */
  652. format = get_le32(pb); /* data format */
  653. get_be32(pb); /* reserved */
  654. get_be16(pb); /* reserved */
  655. get_be16(pb); /* index */
  656. /* for MPEG4: set codec type by looking for it */
  657. id = codec_get_id(mov_video_tags, format);
  658. if (id >= 0) {
  659. AVCodec *codec;
  660. codec = avcodec_find_decoder(id);
  661. if (codec)
  662. st->codec.codec_type = codec->type;
  663. }
  664. #ifdef DEBUG
  665. printf("size=%d 4CC= %c%c%c%c codec_type=%d\n",
  666. size,
  667. (format >> 0) & 0xff,
  668. (format >> 8) & 0xff,
  669. (format >> 16) & 0xff,
  670. (format >> 24) & 0xff,
  671. st->codec.codec_type);
  672. #endif
  673. st->codec.codec_tag = format;
  674. if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
  675. MOV_atom_t a = { 0, 0, 0 };
  676. st->codec.codec_id = id;
  677. get_be16(pb); /* version */
  678. get_be16(pb); /* revision level */
  679. get_be32(pb); /* vendor */
  680. get_be32(pb); /* temporal quality */
  681. get_be32(pb); /* spacial quality */
  682. st->codec.width = get_be16(pb); /* width */
  683. st->codec.height = get_be16(pb); /* height */
  684. #if 1
  685. if (st->codec.codec_id == CODEC_ID_MPEG4) {
  686. /* in some MPEG4 the width/height are not correct, so
  687. we ignore this info */
  688. st->codec.width = 0;
  689. st->codec.height = 0;
  690. }
  691. #endif
  692. get_be32(pb); /* horiz resolution */
  693. get_be32(pb); /* vert resolution */
  694. get_be32(pb); /* data size, always 0 */
  695. frames_per_sample = get_be16(pb); /* frames per samples */
  696. #ifdef DEBUG
  697. printf("frames/samples = %d\n", frames_per_sample);
  698. #endif
  699. get_buffer(pb, (uint8_t *)st->codec.codec_name, 32); /* codec name */
  700. st->codec.bits_per_sample = get_be16(pb); /* depth */
  701. st->codec.color_table_id = get_be16(pb); /* colortable id */
  702. /* These are set in mov_read_stts and might already be set!
  703. st->codec.frame_rate = 25;
  704. st->codec.frame_rate_base = 1;
  705. */
  706. size -= (16+8*4+2+32+2*2);
  707. #if 0
  708. while (size >= 8) {
  709. MOV_atom_t a;
  710. int64_t start_pos;
  711. a.size = get_be32(pb);
  712. a.type = get_le32(pb);
  713. size -= 8;
  714. #ifdef DEBUG
  715. printf("VIDEO: atom_type=%c%c%c%c atom.size=%Ld size_left=%d\n",
  716. (a.type >> 0) & 0xff,
  717. (a.type >> 8) & 0xff,
  718. (a.type >> 16) & 0xff,
  719. (a.type >> 24) & 0xff,
  720. a.size, size);
  721. #endif
  722. start_pos = url_ftell(pb);
  723. switch(a.type) {
  724. case MKTAG('e', 's', 'd', 's'):
  725. {
  726. int tag, len;
  727. /* Well, broken but suffisant for some MP4 streams */
  728. get_be32(pb); /* version + flags */
  729. len = mov_mp4_read_descr(pb, &tag);
  730. if (tag == 0x03) {
  731. /* MP4ESDescrTag */
  732. get_be16(pb); /* ID */
  733. get_byte(pb); /* priority */
  734. len = mov_mp4_read_descr(pb, &tag);
  735. if (tag != 0x04)
  736. goto fail;
  737. /* MP4DecConfigDescrTag */
  738. get_byte(pb); /* objectTypeId */
  739. get_be32(pb); /* streamType + buffer size */
  740. get_be32(pb); /* max bit rate */
  741. get_be32(pb); /* avg bit rate */
  742. len = mov_mp4_read_descr(pb, &tag);
  743. if (tag != 0x05)
  744. goto fail;
  745. /* MP4DecSpecificDescrTag */
  746. #ifdef DEBUG
  747. printf("Specific MPEG4 header len=%d\n", len);
  748. #endif
  749. sc->header_data = av_mallocz(len);
  750. if (sc->header_data) {
  751. get_buffer(pb, sc->header_data, len);
  752. sc->header_len = len;
  753. }
  754. }
  755. /* in any case, skip garbage */
  756. }
  757. break;
  758. default:
  759. break;
  760. }
  761. fail:
  762. printf("ATOMENEWSIZE %Ld %d\n", atom.size, url_ftell(pb) - start_pos);
  763. if (atom.size > 8) {
  764. url_fskip(pb, (atom.size - 8) -
  765. ((url_ftell(pb) - start_pos)));
  766. size -= atom.size - 8;
  767. }
  768. }
  769. if (size > 0) {
  770. /* unknown extension */
  771. url_fskip(pb, size);
  772. }
  773. #else
  774. /* figure out the palette situation */
  775. color_depth = st->codec.bits_per_sample & 0x1F;
  776. color_greyscale = st->codec.bits_per_sample & 0x20;
  777. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  778. if ((color_depth == 2) || (color_depth == 4) ||
  779. (color_depth == 8)) {
  780. if (color_greyscale) {
  781. /* compute the greyscale palette */
  782. color_count = 1 << color_depth;
  783. color_index = 255;
  784. color_dec = 256 / (color_count - 1);
  785. for (j = 0; j < color_count; j++) {
  786. r = g = b = color_index;
  787. c->palette_control.palette[j] =
  788. (r << 16) | (g << 8) | (b);
  789. color_index -= color_dec;
  790. if (color_index < 0)
  791. color_index = 0;
  792. }
  793. } else if (st->codec.color_table_id & 0x08) {
  794. /* if flag bit 3 is set, use the default palette */
  795. color_count = 1 << color_depth;
  796. if (color_depth == 2)
  797. color_table = ff_qt_default_palette_4;
  798. else if (color_depth == 4)
  799. color_table = ff_qt_default_palette_16;
  800. else
  801. color_table = ff_qt_default_palette_256;
  802. for (j = 0; j < color_count; j++) {
  803. r = color_table[j * 4 + 0];
  804. g = color_table[j * 4 + 1];
  805. b = color_table[j * 4 + 2];
  806. c->palette_control.palette[j] =
  807. (r << 16) | (g << 8) | (b);
  808. }
  809. } else {
  810. /* load the palette from the file */
  811. color_start = get_be32(pb);
  812. color_count = get_be16(pb);
  813. color_end = get_be16(pb);
  814. for (j = color_start; j <= color_end; j++) {
  815. /* each R, G, or B component is 16 bits;
  816. * only use the top 8 bits; skip alpha bytes
  817. * up front */
  818. get_byte(pb);
  819. get_byte(pb);
  820. r = get_byte(pb);
  821. get_byte(pb);
  822. g = get_byte(pb);
  823. get_byte(pb);
  824. b = get_byte(pb);
  825. get_byte(pb);
  826. c->palette_control.palette[j] =
  827. (r << 16) | (g << 8) | (b);
  828. }
  829. }
  830. st->codec.palctrl = &c->palette_control;
  831. st->codec.palctrl->palette_changed = 1;
  832. } else
  833. st->codec.palctrl = NULL;
  834. a.size = size;
  835. mov_read_default(c, pb, a);
  836. #endif
  837. } else {
  838. st->codec.codec_id = codec_get_id(mov_audio_tags, format);
  839. if(st->codec.codec_id==CODEC_ID_AMR_NB || st->codec.codec_id==CODEC_ID_AMR_WB) //from TS26.244
  840. {
  841. #ifdef DEBUG
  842. printf("AMR-NB or AMR-WB audio identified!!\n");
  843. #endif
  844. get_be32(pb);get_be32(pb); //Reserved_8
  845. get_be16(pb);//Reserved_2
  846. get_be16(pb);//Reserved_2
  847. get_be32(pb);//Reserved_4
  848. get_be16(pb);//TimeScale
  849. get_be16(pb);//Reserved_2
  850. //AMRSpecificBox.(10 bytes)
  851. get_be32(pb); //size
  852. get_be32(pb); //type=='damr'
  853. get_be32(pb); //vendor
  854. get_byte(pb); //decoder version
  855. get_be16(pb); //mode_set
  856. get_byte(pb); //mode_change_period
  857. get_byte(pb); //frames_per_sample
  858. st->duration = AV_NOPTS_VALUE;//Not possible to get from this info, must count number of AMR frames
  859. if(st->codec.codec_id==CODEC_ID_AMR_NB)
  860. {
  861. st->codec.sample_rate=8000;
  862. st->codec.channels=1;
  863. }
  864. else //AMR-WB
  865. {
  866. st->codec.sample_rate=16000;
  867. st->codec.channels=1;
  868. }
  869. st->codec.bits_per_sample=16;
  870. st->codec.bit_rate=0; /*It is not possible to tell this before we have
  871. an audio frame and even then every frame can be different*/
  872. }
  873. else if( st->codec.codec_tag == MKTAG( 'm', 'p', '4', 's' ))
  874. {
  875. //This is some stuff for the hint track, lets ignore it!
  876. //Do some mp4 auto detect.
  877. c->mp4=1;
  878. size-=(16);
  879. url_fskip(pb, size); /* The mp4s atom also contians a esds atom that we can skip*/
  880. }
  881. else if( st->codec.codec_tag == MKTAG( 'm', 'p', '4', 'a' ))
  882. {
  883. /* Handle mp4 audio tag */
  884. get_be32(pb); /* version */
  885. get_be32(pb);
  886. st->codec.channels = get_be16(pb); /* channels */
  887. st->codec.bits_per_sample = get_be16(pb); /* bits per sample */
  888. get_be32(pb);
  889. st->codec.sample_rate = get_be16(pb); /* sample rate, not always correct */
  890. get_be16(pb);
  891. c->mp4=1;
  892. {
  893. MOV_atom_t a = { format, url_ftell(pb), size - (20 + 20 + 8) };
  894. mov_read_default(c, pb, a);
  895. }
  896. /* Get correct sample rate from extradata */
  897. if(st->codec.extradata_size) {
  898. const int samplerate_table[] = {
  899. 96000, 88200, 64000, 48000, 44100, 32000,
  900. 24000, 22050, 16000, 12000, 11025, 8000,
  901. 7350, 0, 0, 0
  902. };
  903. unsigned char *px = st->codec.extradata;
  904. // 5 bits objectTypeIndex, 4 bits sampleRateIndex, 4 bits channels
  905. int samplerate_index = ((px[0] & 7) << 1) + ((px[1] >> 7) & 1);
  906. st->codec.sample_rate = samplerate_table[samplerate_index];
  907. }
  908. }
  909. else if(size>=(16+20))
  910. {//16 bytes read, reading atleast 20 more
  911. #ifdef DEBUG
  912. printf("audio size=0x%X\n",size);
  913. #endif
  914. uint16_t version = get_be16(pb); /* version */
  915. get_be16(pb); /* revision level */
  916. get_be32(pb); /* vendor */
  917. st->codec.channels = get_be16(pb); /* channel count */
  918. st->codec.bits_per_sample = get_be16(pb); /* sample size */
  919. /* handle specific s8 codec */
  920. get_be16(pb); /* compression id = 0*/
  921. get_be16(pb); /* packet size = 0 */
  922. st->codec.sample_rate = ((get_be32(pb) >> 16));
  923. //printf("CODECID %d %d %.4s\n", st->codec.codec_id, CODEC_ID_PCM_S16BE, (char*)&format);
  924. switch (st->codec.codec_id) {
  925. case CODEC_ID_PCM_S16BE:
  926. if (st->codec.bits_per_sample == 8)
  927. st->codec.codec_id = CODEC_ID_PCM_S8;
  928. /* fall */
  929. case CODEC_ID_PCM_U8:
  930. st->codec.bit_rate = st->codec.sample_rate * 8;
  931. break;
  932. default:
  933. ;
  934. }
  935. //Read QT version 1 fields. In version 0 theese dont exist
  936. #ifdef DEBUG
  937. printf("version =%d mp4=%d\n",version,c->mp4);
  938. printf("size-(16+20+16)=%d\n",size-(16+20+16));
  939. #endif
  940. if((version==1) && size>=(16+20+16))
  941. {
  942. get_be32(pb); /* samples per packet */
  943. get_be32(pb); /* bytes per packet */
  944. get_be32(pb); /* bytes per frame */
  945. get_be32(pb); /* bytes per sample */
  946. if(size>(16+20+16))
  947. {
  948. //Optional, additional atom-based fields
  949. #ifdef DEBUG
  950. printf("offest=0x%X, sizeleft=%d=0x%x,format=%c%c%c%c\n",(int)url_ftell(pb),size - (16 + 20 + 16 ),size - (16 + 20 + 16 ),
  951. (format >> 0) & 0xff,
  952. (format >> 8) & 0xff,
  953. (format >> 16) & 0xff,
  954. (format >> 24) & 0xff);
  955. #endif
  956. MOV_atom_t a = { format, url_ftell(pb), size - (16 + 20 + 16 + 8) };
  957. mov_read_default(c, pb, a);
  958. }
  959. }
  960. else
  961. {
  962. //We should be down to 0 bytes here, but lets make sure.
  963. size-=(16+20);
  964. #ifdef DEBUG
  965. if(size>0)
  966. printf("skipping 0x%X bytes\n",size-(16+20));
  967. #endif
  968. url_fskip(pb, size);
  969. }
  970. }
  971. else
  972. {
  973. size-=16;
  974. //Unknown size, but lets do our best and skip the rest.
  975. #ifdef DEBUG
  976. printf("Strange size, skipping 0x%X bytes\n",size);
  977. #endif
  978. url_fskip(pb, size);
  979. }
  980. }
  981. }
  982. return 0;
  983. }
  984. static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  985. {
  986. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  987. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  988. int entries, i;
  989. print_atom("stsc", atom);
  990. get_byte(pb); /* version */
  991. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  992. entries = get_be32(pb);
  993. #ifdef DEBUG
  994. printf("track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  995. #endif
  996. sc->sample_to_chunk_sz = entries;
  997. sc->sample_to_chunk = (MOV_sample_to_chunk_tbl*) av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
  998. if (!sc->sample_to_chunk)
  999. return -1;
  1000. for(i=0; i<entries; i++) {
  1001. sc->sample_to_chunk[i].first = get_be32(pb);
  1002. sc->sample_to_chunk[i].count = get_be32(pb);
  1003. sc->sample_to_chunk[i].id = get_be32(pb);
  1004. #ifdef DEBUG
  1005. /* 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); */
  1006. #endif
  1007. }
  1008. return 0;
  1009. }
  1010. static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1011. {
  1012. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1013. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1014. int entries, i;
  1015. print_atom("stss", atom);
  1016. get_byte(pb); /* version */
  1017. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1018. entries = get_be32(pb);
  1019. sc->keyframe_count = entries;
  1020. #ifdef DEBUG
  1021. av_log(NULL, AV_LOG_DEBUG, "keyframe_count = %ld\n", sc->keyframe_count);
  1022. #endif
  1023. sc->keyframes = (long*) av_malloc(entries * sizeof(long));
  1024. if (!sc->keyframes)
  1025. return -1;
  1026. for(i=0; i<entries; i++) {
  1027. sc->keyframes[i] = get_be32(pb);
  1028. #ifdef DEBUG
  1029. /* av_log(NULL, AV_LOG_DEBUG, "keyframes[]=%ld\n", sc->keyframes[i]); */
  1030. #endif
  1031. }
  1032. return 0;
  1033. }
  1034. static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1035. {
  1036. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1037. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1038. int entries, i;
  1039. print_atom("stsz", atom);
  1040. get_byte(pb); /* version */
  1041. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1042. sc->sample_size = get_be32(pb);
  1043. entries = get_be32(pb);
  1044. sc->sample_count = entries;
  1045. #ifdef DEBUG
  1046. printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
  1047. #endif
  1048. if(sc->sample_size)
  1049. return 0; /* there isn't any table following */
  1050. sc->sample_sizes = (long*) av_malloc(entries * sizeof(long));
  1051. if (!sc->sample_sizes)
  1052. return -1;
  1053. for(i=0; i<entries; i++) {
  1054. sc->sample_sizes[i] = get_be32(pb);
  1055. #ifdef DEBUG
  1056. /* printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
  1057. #endif
  1058. }
  1059. return 0;
  1060. }
  1061. static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1062. {
  1063. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1064. //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1065. int entries, i;
  1066. int64_t duration=0;
  1067. int64_t total_sample_count=0;
  1068. print_atom("stts", atom);
  1069. get_byte(pb); /* version */
  1070. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1071. entries = get_be32(pb);
  1072. #ifdef DEBUG
  1073. printf("track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  1074. #endif
  1075. for(i=0; i<entries; i++) {
  1076. int sample_duration;
  1077. int sample_count;
  1078. sample_count=get_be32(pb);
  1079. sample_duration = get_be32(pb);
  1080. #ifdef DEBUG
  1081. printf("sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  1082. #endif
  1083. duration+=sample_duration*sample_count;
  1084. total_sample_count+=sample_count;
  1085. #if 0 //We calculate an average instead, needed by .mp4-files created with nec e606 3g phone
  1086. if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
  1087. st->codec.frame_rate_base = sample_duration ? sample_duration : 1;
  1088. st->codec.frame_rate = c->streams[c->total_streams]->time_scale;
  1089. #ifdef DEBUG
  1090. printf("VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
  1091. #endif
  1092. }
  1093. #endif
  1094. }
  1095. /*The stsd atom which contain codec type sometimes comes after the stts so we cannot check for codec_type*/
  1096. if(duration>0)
  1097. {
  1098. av_reduce(
  1099. &st->codec.frame_rate,
  1100. &st->codec.frame_rate_base,
  1101. c->streams[c->total_streams]->time_scale * total_sample_count,
  1102. duration,
  1103. INT_MAX
  1104. );
  1105. #ifdef DEBUG
  1106. printf("FRAME RATE average (video or audio)= %f (tot sample count= %i ,tot dur= %i timescale=%d)\n", (float)st->codec.frame_rate/st->codec.frame_rate_base,total_sample_count,duration,c->streams[c->total_streams]->time_scale);
  1107. #endif
  1108. }
  1109. else
  1110. {
  1111. st->codec.frame_rate_base = 1;
  1112. st->codec.frame_rate = c->streams[c->total_streams]->time_scale;
  1113. }
  1114. return 0;
  1115. }
  1116. static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1117. {
  1118. AVStream *st;
  1119. MOVStreamContext *sc;
  1120. print_atom("trak", atom);
  1121. st = av_new_stream(c->fc, c->fc->nb_streams);
  1122. if (!st) return -2;
  1123. sc = (MOVStreamContext*) av_mallocz(sizeof(MOVStreamContext));
  1124. if (!sc) {
  1125. av_free(st);
  1126. return -1;
  1127. }
  1128. sc->sample_to_chunk_index = -1;
  1129. st->priv_data = sc;
  1130. st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
  1131. st->start_time = 0; /* XXX: check */
  1132. st->duration = (c->duration * (int64_t)AV_TIME_BASE) / c->time_scale;
  1133. c->streams[c->fc->nb_streams-1] = sc;
  1134. return mov_read_default(c, pb, atom);
  1135. }
  1136. static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1137. {
  1138. AVStream *st;
  1139. print_atom("tkhd", atom);
  1140. st = c->fc->streams[c->fc->nb_streams-1];
  1141. get_byte(pb); /* version */
  1142. get_byte(pb); get_byte(pb);
  1143. get_byte(pb); /* flags */
  1144. /*
  1145. MOV_TRACK_ENABLED 0x0001
  1146. MOV_TRACK_IN_MOVIE 0x0002
  1147. MOV_TRACK_IN_PREVIEW 0x0004
  1148. MOV_TRACK_IN_POSTER 0x0008
  1149. */
  1150. get_be32(pb); /* creation time */
  1151. get_be32(pb); /* modification time */
  1152. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  1153. get_be32(pb); /* reserved */
  1154. st->start_time = 0; /* check */
  1155. st->duration = (get_be32(pb) * (int64_t)AV_TIME_BASE) / c->time_scale; /* duration */
  1156. get_be32(pb); /* reserved */
  1157. get_be32(pb); /* reserved */
  1158. get_be16(pb); /* layer */
  1159. get_be16(pb); /* alternate group */
  1160. get_be16(pb); /* volume */
  1161. get_be16(pb); /* reserved */
  1162. url_fskip(pb, 36); /* display matrix */
  1163. /* those are fixed-point */
  1164. st->codec.width = get_be32(pb) >> 16; /* track width */
  1165. st->codec.height = get_be32(pb) >> 16; /* track height */
  1166. return 0;
  1167. }
  1168. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1169. /* like the files created with Adobe Premiere 5.0, for samples see */
  1170. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1171. static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1172. {
  1173. int err;
  1174. #ifdef DEBUG
  1175. print_atom("wide", atom);
  1176. debug_indent++;
  1177. #endif
  1178. if (atom.size < 8)
  1179. return 0; /* continue */
  1180. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1181. url_fskip(pb, atom.size - 4);
  1182. return 0;
  1183. }
  1184. atom.type = get_le32(pb);
  1185. atom.offset += 8;
  1186. atom.size -= 8;
  1187. if (atom.type != MKTAG('m', 'd', 'a', 't')) {
  1188. url_fskip(pb, atom.size);
  1189. return 0;
  1190. }
  1191. err = mov_read_mdat(c, pb, atom);
  1192. #ifdef DEBUG
  1193. debug_indent--;
  1194. #endif
  1195. return err;
  1196. }
  1197. #ifdef CONFIG_ZLIB
  1198. static int null_read_packet(void *opaque, uint8_t *buf, int buf_size)
  1199. {
  1200. return -1;
  1201. }
  1202. static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1203. {
  1204. ByteIOContext ctx;
  1205. uint8_t *cmov_data;
  1206. uint8_t *moov_data; /* uncompressed data */
  1207. long cmov_len, moov_len;
  1208. int ret;
  1209. print_atom("cmov", atom);
  1210. get_be32(pb); /* dcom atom */
  1211. if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
  1212. return -1;
  1213. if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
  1214. dprintf("unknown compression for cmov atom !");
  1215. return -1;
  1216. }
  1217. get_be32(pb); /* cmvd atom */
  1218. if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
  1219. return -1;
  1220. moov_len = get_be32(pb); /* uncompressed size */
  1221. cmov_len = atom.size - 6 * 4;
  1222. cmov_data = (uint8_t *) av_malloc(cmov_len);
  1223. if (!cmov_data)
  1224. return -1;
  1225. moov_data = (uint8_t *) av_malloc(moov_len);
  1226. if (!moov_data) {
  1227. av_free(cmov_data);
  1228. return -1;
  1229. }
  1230. get_buffer(pb, cmov_data, cmov_len);
  1231. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1232. return -1;
  1233. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
  1234. return -1;
  1235. ctx.buf_end = ctx.buffer + moov_len;
  1236. atom.type = MKTAG( 'm', 'o', 'o', 'v' );
  1237. atom.offset = 0;
  1238. atom.size = moov_len;
  1239. #ifdef DEBUG
  1240. { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
  1241. #endif
  1242. ret = mov_read_default(c, &ctx, atom);
  1243. av_free(moov_data);
  1244. av_free(cmov_data);
  1245. return ret;
  1246. }
  1247. #endif
  1248. static const MOVParseTableEntry mov_default_parse_table[] = {
  1249. /* mp4 atoms */
  1250. { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
  1251. { MKTAG( 'c', 'p', 'r', 't' ), mov_read_default },
  1252. { MKTAG( 'c', 'r', 'h', 'd' ), mov_read_default },
  1253. { MKTAG( 'c', 't', 't', 's' ), mov_read_leaf }, /* composition time to sample */
  1254. { MKTAG( 'd', 'i', 'n', 'f' ), mov_read_default }, /* data information */
  1255. { MKTAG( 'd', 'p', 'n', 'd' ), mov_read_leaf },
  1256. { MKTAG( 'd', 'r', 'e', 'f' ), mov_read_leaf },
  1257. { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
  1258. { MKTAG( 'e', 'l', 's', 't' ), mov_read_leaf },
  1259. { MKTAG( 'f', 'r', 'e', 'e' ), mov_read_leaf },
  1260. { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
  1261. { MKTAG( 'h', 'i', 'n', 't' ), mov_read_leaf },
  1262. { MKTAG( 'h', 'm', 'h', 'd' ), mov_read_leaf },
  1263. { MKTAG( 'i', 'o', 'd', 's' ), mov_read_leaf },
  1264. { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
  1265. { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
  1266. { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
  1267. { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
  1268. { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
  1269. { MKTAG( 'm', 'p', '4', 'a' ), mov_read_default },
  1270. { MKTAG( 'm', 'p', '4', 's' ), mov_read_default },
  1271. { MKTAG( 'm', 'p', '4', 'v' ), mov_read_default },
  1272. { MKTAG( 'm', 'p', 'o', 'd' ), mov_read_leaf },
  1273. { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
  1274. { MKTAG( 'n', 'm', 'h', 'd' ), mov_read_leaf },
  1275. { MKTAG( 'o', 'd', 'h', 'd' ), mov_read_default },
  1276. { MKTAG( 's', 'd', 'h', 'd' ), mov_read_default },
  1277. { MKTAG( 's', 'k', 'i', 'p' ), mov_read_default },
  1278. { MKTAG( 's', 'm', 'h', 'd' ), mov_read_leaf }, /* sound media info header */
  1279. { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorrenson extension ??? */
  1280. { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
  1281. { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
  1282. { MKTAG( 's', 't', 'd', 'p' ), mov_read_default },
  1283. { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
  1284. { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
  1285. { MKTAG( 's', 't', 's', 'h' ), mov_read_default },
  1286. { MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */
  1287. { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
  1288. { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
  1289. { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
  1290. { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
  1291. { MKTAG( 't', 'r', 'e', 'f' ), mov_read_default }, /* not really */
  1292. { MKTAG( 'u', 'd', 't', 'a' ), mov_read_leaf },
  1293. { MKTAG( 'u', 'r', 'l', ' ' ), mov_read_leaf },
  1294. { MKTAG( 'u', 'r', 'n', ' ' ), mov_read_leaf },
  1295. { MKTAG( 'u', 'u', 'i', 'd' ), mov_read_default },
  1296. { MKTAG( 'v', 'm', 'h', 'd' ), mov_read_leaf }, /* video media info header */
  1297. { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_default },
  1298. /* extra mp4 */
  1299. { MKTAG( 'M', 'D', 'E', 'S' ), mov_read_leaf },
  1300. /* QT atoms */
  1301. { MKTAG( 'c', 'h', 'a', 'p' ), mov_read_leaf },
  1302. { MKTAG( 'c', 'l', 'i', 'p' ), mov_read_default },
  1303. { MKTAG( 'c', 'r', 'g', 'n' ), mov_read_leaf },
  1304. { MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab },
  1305. { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
  1306. { MKTAG( 'k', 'm', 'a', 't' ), mov_read_leaf },
  1307. { MKTAG( 'm', 'a', 't', 't' ), mov_read_default },
  1308. { MKTAG( 'r', 'd', 'r', 'f' ), mov_read_leaf },
  1309. { MKTAG( 'r', 'm', 'd', 'a' ), mov_read_default },
  1310. { MKTAG( 'r', 'm', 'd', 'r' ), mov_read_leaf },
  1311. { MKTAG( 'r', 'm', 'r', 'a' ), mov_read_default },
  1312. { MKTAG( 's', 'c', 'p', 't' ), mov_read_leaf },
  1313. { MKTAG( 's', 's', 'r', 'c' ), mov_read_leaf },
  1314. { MKTAG( 's', 'y', 'n', 'c' ), mov_read_leaf },
  1315. { MKTAG( 't', 'c', 'm', 'd' ), mov_read_leaf },
  1316. { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
  1317. //{ MKTAG( 'r', 'm', 'q', 'u' ), mov_read_leaf },
  1318. #ifdef CONFIG_ZLIB
  1319. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
  1320. #else
  1321. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_leaf },
  1322. #endif
  1323. { 0L, mov_read_leaf }
  1324. };
  1325. static void mov_free_stream_context(MOVStreamContext *sc)
  1326. {
  1327. if(sc) {
  1328. av_free(sc->chunk_offsets);
  1329. av_free(sc->sample_to_chunk);
  1330. av_free(sc->sample_sizes);
  1331. av_free(sc->keyframes);
  1332. av_free(sc->header_data);
  1333. av_free(sc);
  1334. }
  1335. }
  1336. static inline uint32_t mov_to_tag(uint8_t *buf)
  1337. {
  1338. return MKTAG(buf[0], buf[1], buf[2], buf[3]);
  1339. }
  1340. static inline uint32_t to_be32(uint8_t *buf)
  1341. {
  1342. return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  1343. }
  1344. /* XXX: is it sufficient ? */
  1345. static int mov_probe(AVProbeData *p)
  1346. {
  1347. unsigned int offset;
  1348. uint32_t tag;
  1349. /* check file header */
  1350. if (p->buf_size <= 12)
  1351. return 0;
  1352. offset = 0;
  1353. for(;;) {
  1354. /* ignore invalid offset */
  1355. if ((offset + 8) > (unsigned int)p->buf_size)
  1356. return 0;
  1357. tag = mov_to_tag(p->buf + offset + 4);
  1358. switch(tag) {
  1359. case MKTAG( 'm', 'o', 'o', 'v' ):
  1360. case MKTAG( 'w', 'i', 'd', 'e' ):
  1361. case MKTAG( 'f', 'r', 'e', 'e' ):
  1362. case MKTAG( 'm', 'd', 'a', 't' ):
  1363. case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
  1364. case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
  1365. return AVPROBE_SCORE_MAX;
  1366. case MKTAG( 'f', 't', 'y', 'p' ):
  1367. case MKTAG( 's', 'k', 'i', 'p' ):
  1368. offset = to_be32(p->buf+offset) + offset;
  1369. break;
  1370. default:
  1371. /* unrecognized tag */
  1372. return 0;
  1373. }
  1374. }
  1375. return 0;
  1376. }
  1377. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1378. {
  1379. MOVContext *mov = (MOVContext *) s->priv_data;
  1380. ByteIOContext *pb = &s->pb;
  1381. int i, j, nb, err;
  1382. MOV_atom_t atom = { 0, 0, 0 };
  1383. mov->fc = s;
  1384. mov->parse_table = mov_default_parse_table;
  1385. #if 0
  1386. /* XXX: I think we should auto detect */
  1387. if(s->iformat->name[1] == 'p')
  1388. mov->mp4 = 1;
  1389. #endif
  1390. if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  1391. atom.size = url_filesize(url_fileno(pb));
  1392. else
  1393. atom.size = 0x7FFFFFFFFFFFFFFFLL;
  1394. #ifdef DEBUG
  1395. printf("filesz=%Ld\n", atom.size);
  1396. #endif
  1397. /* check MOV header */
  1398. err = mov_read_default(mov, pb, atom);
  1399. if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
  1400. av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%lld\n",
  1401. err, mov->found_moov, mov->found_mdat, url_ftell(pb));
  1402. return -1;
  1403. }
  1404. #ifdef DEBUG
  1405. printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
  1406. #endif
  1407. /* some cleanup : make sure we are on the mdat atom */
  1408. if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
  1409. url_fseek(pb, mov->mdat_offset, SEEK_SET);
  1410. mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
  1411. #ifdef DEBUG
  1412. printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
  1413. #endif
  1414. #ifdef DEBUG
  1415. printf("streams= %d\n", s->nb_streams);
  1416. #endif
  1417. mov->total_streams = nb = s->nb_streams;
  1418. #if 1
  1419. for(i=0; i<s->nb_streams;) {
  1420. if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
  1421. av_free(s->streams[i]);
  1422. for(j=i+1; j<s->nb_streams; j++)
  1423. s->streams[j-1] = s->streams[j];
  1424. s->nb_streams--;
  1425. } else
  1426. i++;
  1427. }
  1428. for(i=0; i<s->nb_streams;i++) {
  1429. MOVStreamContext *sc;
  1430. sc = (MOVStreamContext *)s->streams[i]->priv_data;
  1431. sc->ffindex = i;
  1432. sc->is_ff_stream = 1;
  1433. }
  1434. #endif
  1435. #ifdef DEBUG
  1436. printf("real streams= %d\n", s->nb_streams);
  1437. #endif
  1438. return 0;
  1439. }
  1440. /* Yes, this is ugly... I didn't write the specs of QT :p */
  1441. /* XXX:remove useless commented code sometime */
  1442. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  1443. {
  1444. MOVContext *mov = (MOVContext *) s->priv_data;
  1445. MOVStreamContext *sc;
  1446. int64_t offset = 0x0FFFFFFFFFFFFFFFLL;
  1447. int i, a, b, m;
  1448. int size;
  1449. size = 0x0FFFFFFF;
  1450. #ifdef MOV_SPLIT_CHUNKS
  1451. if (mov->partial) {
  1452. int idx;
  1453. sc = mov->partial;
  1454. idx = sc->sample_to_chunk_index;
  1455. if (idx < 0) return 0;
  1456. size = sc->sample_sizes[sc->current_sample];
  1457. sc->current_sample++;
  1458. sc->left_in_chunk--;
  1459. if (sc->left_in_chunk <= 0)
  1460. mov->partial = 0;
  1461. offset = mov->next_chunk_offset;
  1462. /* extract the sample */
  1463. goto readchunk;
  1464. }
  1465. #endif
  1466. again:
  1467. sc = 0;
  1468. for(i=0; i<mov->total_streams; i++) {
  1469. MOVStreamContext *msc = mov->streams[i];
  1470. //printf("MOCHUNK %ld %d %p pos:%Ld\n", mov->streams[i]->next_chunk, mov->total_streams, mov->streams[i], url_ftell(&s->pb));
  1471. if ((msc->next_chunk < msc->chunk_count) && msc->next_chunk >= 0
  1472. && (msc->chunk_offsets[msc->next_chunk] < offset)) {
  1473. sc = msc;
  1474. offset = msc->chunk_offsets[msc->next_chunk];
  1475. //printf("SELETED %Ld i:%d\n", offset, i);
  1476. }
  1477. }
  1478. if (!sc || offset==0x0FFFFFFFFFFFFFFFLL)
  1479. return -1;
  1480. sc->next_chunk++;
  1481. if(mov->next_chunk_offset < offset) { /* some meta data */
  1482. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1483. mov->next_chunk_offset = offset;
  1484. }
  1485. //printf("chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
  1486. if(!sc->is_ff_stream) {
  1487. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1488. mov->next_chunk_offset = offset;
  1489. offset = 0x0FFFFFFFFFFFFFFFLL;
  1490. goto again;
  1491. }
  1492. /* now get the chunk size... */
  1493. for(i=0; i<mov->total_streams; i++) {
  1494. MOVStreamContext *msc = mov->streams[i];
  1495. if ((msc->next_chunk < msc->chunk_count)
  1496. && ((msc->chunk_offsets[msc->next_chunk] - offset) < size))
  1497. size = msc->chunk_offsets[msc->next_chunk] - offset;
  1498. }
  1499. #ifdef MOV_MINOLTA_FIX
  1500. //Make sure that size is according to sample_size (Needed by .mov files
  1501. //created on a Minolta Dimage Xi where audio chunks contains waste data in the end)
  1502. //Maybe we should really not only check sc->sample_size, but also sc->sample_sizes
  1503. //but I have no such movies
  1504. if (sc->sample_size > 0) {
  1505. int foundsize=0;
  1506. for(i=0; i<(sc->sample_to_chunk_sz); i++) {
  1507. if( (sc->sample_to_chunk[i].first)<=(sc->next_chunk) && (sc->sample_size>0) )
  1508. {
  1509. // I can't figure out why for PCM audio sample_size is always 1
  1510. // (it should actually be channels*bits_per_second/8) but it is.
  1511. AVCodecContext* cod = &s->streams[sc->ffindex]->codec;
  1512. if (sc->sample_size == 1 && (cod->codec_id == CODEC_ID_PCM_S16BE || cod->codec_id == CODEC_ID_PCM_S16LE))
  1513. foundsize=(sc->sample_to_chunk[i].count*cod->channels*cod->bits_per_sample)/8;
  1514. else
  1515. foundsize=sc->sample_to_chunk[i].count*sc->sample_size;
  1516. }
  1517. #ifdef DEBUG
  1518. /*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);*/
  1519. #endif
  1520. }
  1521. if( (foundsize>0) && (foundsize<size) )
  1522. {
  1523. #ifdef DEBUG
  1524. /*printf("this size should actually be %d\n",foundsize);*/
  1525. #endif
  1526. size=foundsize;
  1527. }
  1528. }
  1529. #endif //MOV_MINOLTA_FIX
  1530. #ifdef MOV_SPLIT_CHUNKS
  1531. /* split chunks into samples */
  1532. if (sc->sample_size == 0) {
  1533. int idx = sc->sample_to_chunk_index;
  1534. if ((idx + 1 < sc->sample_to_chunk_sz)
  1535. && (sc->next_chunk >= sc->sample_to_chunk[idx + 1].first))
  1536. idx++;
  1537. sc->sample_to_chunk_index = idx;
  1538. if (idx >= 0 && sc->sample_to_chunk[idx].count != 1) {
  1539. mov->partial = sc;
  1540. /* we'll have to get those samples before next chunk */
  1541. sc->left_in_chunk = sc->sample_to_chunk[idx].count - 1;
  1542. size = sc->sample_sizes[sc->current_sample];
  1543. }
  1544. sc->current_sample++;
  1545. }
  1546. #endif
  1547. readchunk:
  1548. //printf("chunk: [%i] %lli -> %lli (%i)\n", st_id, offset, offset + size, size);
  1549. if(size == 0x0FFFFFFF)
  1550. size = mov->mdat_size + mov->mdat_offset - offset;
  1551. if(size < 0)
  1552. return -1;
  1553. if(size == 0)
  1554. return -1;
  1555. url_fseek(&s->pb, offset, SEEK_SET);
  1556. //printf("READCHUNK hlen: %d %d off: %Ld pos:%Ld\n", size, sc->header_len, offset, url_ftell(&s->pb));
  1557. if (sc->header_len > 0) {
  1558. av_new_packet(pkt, size + sc->header_len);
  1559. memcpy(pkt->data, sc->header_data, sc->header_len);
  1560. get_buffer(&s->pb, pkt->data + sc->header_len, size);
  1561. /* free header */
  1562. av_freep(&sc->header_data);
  1563. sc->header_len = 0;
  1564. } else {
  1565. av_new_packet(pkt, size);
  1566. get_buffer(&s->pb, pkt->data, pkt->size);
  1567. }
  1568. pkt->stream_index = sc->ffindex;
  1569. // If the keyframes table exists, mark any samples that are in the table as key frames.
  1570. // If no table exists, treat very sample as a key frame.
  1571. if (sc->keyframes) {
  1572. a = 0;
  1573. b = sc->keyframe_count - 1;
  1574. while (a < b) {
  1575. m = (a + b + 1) >> 1;
  1576. if (sc->keyframes[m] > sc->current_sample) {
  1577. b = m - 1;
  1578. } else {
  1579. a = m;
  1580. }
  1581. }
  1582. if (sc->keyframes[a] == sc->current_sample)
  1583. pkt->flags |= PKT_FLAG_KEY;
  1584. }
  1585. else
  1586. pkt->flags |= PKT_FLAG_KEY;
  1587. #ifdef DEBUG
  1588. /*
  1589. printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
  1590. for(i=0; i<8; i++)
  1591. printf("%02x ", pkt->data[i]);
  1592. for(i=0; i<8; i++)
  1593. printf("%c ", (pkt->data[i]) & 0x7F);
  1594. puts("");
  1595. */
  1596. #endif
  1597. mov->next_chunk_offset = offset + size;
  1598. return 0;
  1599. }
  1600. static int mov_read_close(AVFormatContext *s)
  1601. {
  1602. int i;
  1603. MOVContext *mov = (MOVContext *) s->priv_data;
  1604. for(i=0; i<mov->total_streams; i++)
  1605. mov_free_stream_context(mov->streams[i]);
  1606. /* free color tabs */
  1607. for(i=0; i<mov->ctab_size; i++)
  1608. av_freep(&mov->ctab[i]);
  1609. av_freep(&mov->ctab);
  1610. return 0;
  1611. }
  1612. static AVInputFormat mov_iformat = {
  1613. "mov,mp4,m4a,3gp",
  1614. "QuickTime/MPEG4 format",
  1615. sizeof(MOVContext),
  1616. mov_probe,
  1617. mov_read_header,
  1618. mov_read_packet,
  1619. mov_read_close,
  1620. };
  1621. int mov_init(void)
  1622. {
  1623. av_register_input_format(&mov_iformat);
  1624. return 0;
  1625. }