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.

2278 lines
79KB

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