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.

1817 lines
59KB

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