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.

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