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.

2069 lines
71KB

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