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.

2201 lines
76KB

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