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.

1765 lines
57KB

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