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.

2281 lines
78KB

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