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.

2315 lines
80KB

  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. 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. #if 0 //We calculate an average instead, needed by .mp4-files created with nec e606 3g phone
  1180. if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
  1181. st->codec.time_base.num = sample_duration ? sample_duration : 1;
  1182. st->codec.time_base.den = c->streams[c->fc->nb_streams-1]->time_scale;
  1183. #ifdef DEBUG
  1184. av_log(NULL, AV_LOG_DEBUG, "VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.time_base.den, sample_duration);
  1185. #endif
  1186. }
  1187. #endif
  1188. }
  1189. /*The stsd atom which contain codec type sometimes comes after the stts so we cannot check for codec_type*/
  1190. if(duration>0)
  1191. {
  1192. av_reduce(
  1193. &st->codec.time_base.den,
  1194. &st->codec.time_base.num,
  1195. c->streams[c->fc->nb_streams-1]->time_scale * total_sample_count,
  1196. duration,
  1197. INT_MAX
  1198. );
  1199. #ifdef DEBUG
  1200. av_log(NULL, AV_LOG_DEBUG, "FRAME RATE average (video or audio)= %f (tot sample count= %i ,tot dur= %i timescale=%d)\n", (float)st->codec.time_base.den/st->codec.time_base.num,total_sample_count,duration,c->streams[c->fc->nb_streams-1]->time_scale);
  1201. #endif
  1202. }
  1203. else
  1204. {
  1205. st->codec.time_base.num = 1;
  1206. st->codec.time_base.den = c->streams[c->fc->nb_streams-1]->time_scale;
  1207. }
  1208. return 0;
  1209. }
  1210. static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1211. {
  1212. // AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1213. //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1214. unsigned int i, entries;
  1215. print_atom("ctts", atom);
  1216. get_byte(pb); /* version */
  1217. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1218. entries = get_be32(pb);
  1219. if(entries >= UINT_MAX / sizeof(Time2Sample))
  1220. return -1;
  1221. c->streams[c->fc->nb_streams-1]->ctts_count = entries;
  1222. c->streams[c->fc->nb_streams-1]->ctts_data = av_malloc(entries * sizeof(Time2Sample));
  1223. // #ifdef DEBUG
  1224. av_log(NULL, AV_LOG_DEBUG, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1225. // #endif
  1226. for(i=0; i<entries; i++) {
  1227. c->streams[c->fc->nb_streams - 1]->ctts_data[i].count= get_be32(pb);
  1228. c->streams[c->fc->nb_streams - 1]->ctts_data[i].duration= get_be32(pb);
  1229. }
  1230. return 0;
  1231. }
  1232. static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1233. {
  1234. AVStream *st;
  1235. MOVStreamContext *sc;
  1236. print_atom("trak", atom);
  1237. st = av_new_stream(c->fc, c->fc->nb_streams);
  1238. if (!st) return -2;
  1239. sc = (MOVStreamContext*) av_mallocz(sizeof(MOVStreamContext));
  1240. if (!sc) {
  1241. av_free(st);
  1242. return -1;
  1243. }
  1244. sc->sample_to_chunk_index = -1;
  1245. st->priv_data = sc;
  1246. st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
  1247. st->start_time = 0; /* XXX: check */
  1248. st->duration = (c->duration * (int64_t)AV_TIME_BASE) / c->time_scale;
  1249. c->streams[c->fc->nb_streams-1] = sc;
  1250. return mov_read_default(c, pb, atom);
  1251. }
  1252. static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1253. {
  1254. AVStream *st;
  1255. print_atom("tkhd", atom);
  1256. st = c->fc->streams[c->fc->nb_streams-1];
  1257. get_byte(pb); /* version */
  1258. get_byte(pb); get_byte(pb);
  1259. get_byte(pb); /* flags */
  1260. /*
  1261. MOV_TRACK_ENABLED 0x0001
  1262. MOV_TRACK_IN_MOVIE 0x0002
  1263. MOV_TRACK_IN_PREVIEW 0x0004
  1264. MOV_TRACK_IN_POSTER 0x0008
  1265. */
  1266. get_be32(pb); /* creation time */
  1267. get_be32(pb); /* modification time */
  1268. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  1269. get_be32(pb); /* reserved */
  1270. st->start_time = 0; /* check */
  1271. st->duration = (get_be32(pb) * (int64_t)AV_TIME_BASE) / c->time_scale; /* duration */
  1272. get_be32(pb); /* reserved */
  1273. get_be32(pb); /* reserved */
  1274. get_be16(pb); /* layer */
  1275. get_be16(pb); /* alternate group */
  1276. get_be16(pb); /* volume */
  1277. get_be16(pb); /* reserved */
  1278. url_fskip(pb, 36); /* display matrix */
  1279. /* those are fixed-point */
  1280. st->codec.width = get_be32(pb) >> 16; /* track width */
  1281. st->codec.height = get_be32(pb) >> 16; /* track height */
  1282. return 0;
  1283. }
  1284. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1285. /* like the files created with Adobe Premiere 5.0, for samples see */
  1286. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1287. static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1288. {
  1289. int err;
  1290. #ifdef DEBUG
  1291. print_atom("wide", atom);
  1292. debug_indent++;
  1293. #endif
  1294. if (atom.size < 8)
  1295. return 0; /* continue */
  1296. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1297. url_fskip(pb, atom.size - 4);
  1298. return 0;
  1299. }
  1300. atom.type = get_le32(pb);
  1301. atom.offset += 8;
  1302. atom.size -= 8;
  1303. if (atom.type != MKTAG('m', 'd', 'a', 't')) {
  1304. url_fskip(pb, atom.size);
  1305. return 0;
  1306. }
  1307. err = mov_read_mdat(c, pb, atom);
  1308. #ifdef DEBUG
  1309. debug_indent--;
  1310. #endif
  1311. return err;
  1312. }
  1313. #ifdef CONFIG_ZLIB
  1314. static int null_read_packet(void *opaque, uint8_t *buf, int buf_size)
  1315. {
  1316. return -1;
  1317. }
  1318. static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1319. {
  1320. ByteIOContext ctx;
  1321. uint8_t *cmov_data;
  1322. uint8_t *moov_data; /* uncompressed data */
  1323. long cmov_len, moov_len;
  1324. int ret;
  1325. print_atom("cmov", atom);
  1326. get_be32(pb); /* dcom atom */
  1327. if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
  1328. return -1;
  1329. if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
  1330. av_log(NULL, AV_LOG_DEBUG, "unknown compression for cmov atom !");
  1331. return -1;
  1332. }
  1333. get_be32(pb); /* cmvd atom */
  1334. if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
  1335. return -1;
  1336. moov_len = get_be32(pb); /* uncompressed size */
  1337. cmov_len = atom.size - 6 * 4;
  1338. cmov_data = (uint8_t *) av_malloc(cmov_len);
  1339. if (!cmov_data)
  1340. return -1;
  1341. moov_data = (uint8_t *) av_malloc(moov_len);
  1342. if (!moov_data) {
  1343. av_free(cmov_data);
  1344. return -1;
  1345. }
  1346. get_buffer(pb, cmov_data, cmov_len);
  1347. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1348. return -1;
  1349. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
  1350. return -1;
  1351. ctx.buf_end = ctx.buffer + moov_len;
  1352. atom.type = MKTAG( 'm', 'o', 'o', 'v' );
  1353. atom.offset = 0;
  1354. atom.size = moov_len;
  1355. #ifdef DEBUG
  1356. { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
  1357. #endif
  1358. ret = mov_read_default(c, &ctx, atom);
  1359. av_free(moov_data);
  1360. av_free(cmov_data);
  1361. return ret;
  1362. }
  1363. #endif
  1364. /* edit list atom */
  1365. static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1366. {
  1367. int i, edit_count;
  1368. print_atom("elst", atom);
  1369. get_byte(pb); /* version */
  1370. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1371. edit_count= c->streams[c->fc->nb_streams-1]->edit_count = get_be32(pb); /* entries */
  1372. for(i=0; i<edit_count; i++){
  1373. get_be32(pb); /* Track duration */
  1374. get_be32(pb); /* Media time */
  1375. get_be32(pb); /* Media rate */
  1376. }
  1377. #ifdef DEBUG
  1378. 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);
  1379. #endif
  1380. return 0;
  1381. }
  1382. static const MOVParseTableEntry mov_default_parse_table[] = {
  1383. /* mp4 atoms */
  1384. { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
  1385. { MKTAG( 'c', 'p', 'r', 't' ), mov_read_default },
  1386. { MKTAG( 'c', 'r', 'h', 'd' ), mov_read_default },
  1387. { MKTAG( 'c', 't', 't', 's' ), mov_read_ctts }, /* composition time to sample */
  1388. { MKTAG( 'd', 'i', 'n', 'f' ), mov_read_default }, /* data information */
  1389. { MKTAG( 'd', 'p', 'n', 'd' ), mov_read_leaf },
  1390. { MKTAG( 'd', 'r', 'e', 'f' ), mov_read_leaf },
  1391. { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
  1392. { MKTAG( 'e', 'l', 's', 't' ), mov_read_elst },
  1393. { MKTAG( 'f', 'r', 'e', 'e' ), mov_read_leaf },
  1394. { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
  1395. { MKTAG( 'h', 'i', 'n', 't' ), mov_read_leaf },
  1396. { MKTAG( 'h', 'm', 'h', 'd' ), mov_read_leaf },
  1397. { MKTAG( 'i', 'o', 'd', 's' ), mov_read_leaf },
  1398. { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
  1399. { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
  1400. { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
  1401. { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
  1402. { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
  1403. { MKTAG( 'm', 'p', '4', 'a' ), mov_read_default },
  1404. { MKTAG( 'm', 'p', '4', 's' ), mov_read_default },
  1405. { MKTAG( 'm', 'p', '4', 'v' ), mov_read_default },
  1406. { MKTAG( 'm', 'p', 'o', 'd' ), mov_read_leaf },
  1407. { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
  1408. { MKTAG( 'n', 'm', 'h', 'd' ), mov_read_leaf },
  1409. { MKTAG( 'o', 'd', 'h', 'd' ), mov_read_default },
  1410. { MKTAG( 's', 'd', 'h', 'd' ), mov_read_default },
  1411. { MKTAG( 's', 'k', 'i', 'p' ), mov_read_leaf },
  1412. { MKTAG( 's', 'm', 'h', 'd' ), mov_read_leaf }, /* sound media info header */
  1413. { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorenson extension ??? */
  1414. { MKTAG( 'a', 'v', 'c', 'C' ), mov_read_avcC },
  1415. { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
  1416. { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
  1417. { MKTAG( 's', 't', 'd', 'p' ), mov_read_default },
  1418. { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
  1419. { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
  1420. { MKTAG( 's', 't', 's', 'h' ), mov_read_default },
  1421. { MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */
  1422. { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
  1423. { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
  1424. { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
  1425. { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
  1426. { MKTAG( 't', 'r', 'e', 'f' ), mov_read_default }, /* not really */
  1427. { MKTAG( 'u', 'd', 't', 'a' ), mov_read_leaf },
  1428. { MKTAG( 'u', 'r', 'l', ' ' ), mov_read_leaf },
  1429. { MKTAG( 'u', 'r', 'n', ' ' ), mov_read_leaf },
  1430. { MKTAG( 'u', 'u', 'i', 'd' ), mov_read_leaf },
  1431. { MKTAG( 'v', 'm', 'h', 'd' ), mov_read_leaf }, /* video media info header */
  1432. { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_default },
  1433. /* extra mp4 */
  1434. { MKTAG( 'M', 'D', 'E', 'S' ), mov_read_leaf },
  1435. /* QT atoms */
  1436. { MKTAG( 'c', 'h', 'a', 'p' ), mov_read_leaf },
  1437. { MKTAG( 'c', 'l', 'i', 'p' ), mov_read_default },
  1438. { MKTAG( 'c', 'r', 'g', 'n' ), mov_read_leaf },
  1439. { MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab },
  1440. { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
  1441. { MKTAG( 'k', 'm', 'a', 't' ), mov_read_leaf },
  1442. { MKTAG( 'm', 'a', 't', 't' ), mov_read_default },
  1443. { MKTAG( 'r', 'd', 'r', 'f' ), mov_read_leaf },
  1444. { MKTAG( 'r', 'm', 'd', 'a' ), mov_read_default },
  1445. { MKTAG( 'r', 'm', 'd', 'r' ), mov_read_leaf },
  1446. { MKTAG( 'r', 'm', 'r', 'a' ), mov_read_default },
  1447. { MKTAG( 's', 'c', 'p', 't' ), mov_read_leaf },
  1448. { MKTAG( 's', 's', 'r', 'c' ), mov_read_leaf },
  1449. { MKTAG( 's', 'y', 'n', 'c' ), mov_read_leaf },
  1450. { MKTAG( 't', 'c', 'm', 'd' ), mov_read_leaf },
  1451. { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
  1452. //{ MKTAG( 'r', 'm', 'q', 'u' ), mov_read_leaf },
  1453. #ifdef CONFIG_ZLIB
  1454. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
  1455. #else
  1456. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_leaf },
  1457. #endif
  1458. { 0L, mov_read_leaf }
  1459. };
  1460. static void mov_free_stream_context(MOVStreamContext *sc)
  1461. {
  1462. if(sc) {
  1463. av_freep(&sc->chunk_offsets);
  1464. av_freep(&sc->sample_to_chunk);
  1465. av_freep(&sc->sample_sizes);
  1466. av_freep(&sc->keyframes);
  1467. av_freep(&sc->stts_data);
  1468. av_freep(&sc->ctts_data);
  1469. av_freep(&sc);
  1470. }
  1471. }
  1472. static inline uint32_t mov_to_tag(uint8_t *buf)
  1473. {
  1474. return MKTAG(buf[0], buf[1], buf[2], buf[3]);
  1475. }
  1476. static inline uint32_t to_be32(uint8_t *buf)
  1477. {
  1478. return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  1479. }
  1480. /* XXX: is it sufficient ? */
  1481. static int mov_probe(AVProbeData *p)
  1482. {
  1483. unsigned int offset;
  1484. uint32_t tag;
  1485. int score = 0;
  1486. /* check file header */
  1487. if (p->buf_size <= 12)
  1488. return 0;
  1489. offset = 0;
  1490. for(;;) {
  1491. /* ignore invalid offset */
  1492. if ((offset + 8) > (unsigned int)p->buf_size)
  1493. return score;
  1494. tag = mov_to_tag(p->buf + offset + 4);
  1495. switch(tag) {
  1496. /* check for obvious tags */
  1497. case MKTAG( 'm', 'o', 'o', 'v' ):
  1498. case MKTAG( 'm', 'd', 'a', 't' ):
  1499. case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
  1500. case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
  1501. return AVPROBE_SCORE_MAX;
  1502. /* those are more common words, so rate then a bit less */
  1503. case MKTAG( 'w', 'i', 'd', 'e' ):
  1504. case MKTAG( 'f', 'r', 'e', 'e' ):
  1505. case MKTAG( 'j', 'u', 'n', 'k' ):
  1506. case MKTAG( 'p', 'i', 'c', 't' ):
  1507. return AVPROBE_SCORE_MAX - 5;
  1508. case MKTAG( 'f', 't', 'y', 'p' ):
  1509. case MKTAG( 's', 'k', 'i', 'p' ):
  1510. case MKTAG( 'u', 'u', 'i', 'd' ):
  1511. offset = to_be32(p->buf+offset) + offset;
  1512. /* if we only find those cause probedata is too small at least rate them */
  1513. score = AVPROBE_SCORE_MAX - 50;
  1514. break;
  1515. default:
  1516. /* unrecognized tag */
  1517. return score;
  1518. }
  1519. }
  1520. return score;
  1521. }
  1522. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1523. {
  1524. MOVContext *mov = (MOVContext *) s->priv_data;
  1525. ByteIOContext *pb = &s->pb;
  1526. int i, j, nb, err;
  1527. MOV_atom_t atom = { 0, 0, 0 };
  1528. mov->fc = s;
  1529. mov->parse_table = mov_default_parse_table;
  1530. #if 0
  1531. /* XXX: I think we should auto detect */
  1532. if(s->iformat->name[1] == 'p')
  1533. mov->mp4 = 1;
  1534. #endif
  1535. if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  1536. atom.size = url_fsize(pb);
  1537. else
  1538. atom.size = 0x7FFFFFFFFFFFFFFFLL;
  1539. #ifdef DEBUG
  1540. av_log(NULL, AV_LOG_DEBUG, "filesz=%Ld\n", atom.size);
  1541. #endif
  1542. /* check MOV header */
  1543. err = mov_read_default(mov, pb, atom);
  1544. if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
  1545. av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%lld\n",
  1546. err, mov->found_moov, mov->found_mdat, url_ftell(pb));
  1547. return -1;
  1548. }
  1549. #ifdef DEBUG
  1550. av_log(NULL, AV_LOG_DEBUG, "on_parse_exit_offset=%d\n", (int) url_ftell(pb));
  1551. #endif
  1552. /* some cleanup : make sure we are on the mdat atom */
  1553. if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
  1554. url_fseek(pb, mov->mdat_offset, SEEK_SET);
  1555. mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
  1556. #ifdef DEBUG
  1557. av_log(NULL, AV_LOG_DEBUG, "mdat_reset_offset=%d\n", (int) url_ftell(pb));
  1558. #endif
  1559. #ifdef DEBUG
  1560. av_log(NULL, AV_LOG_DEBUG, "streams= %d\n", s->nb_streams);
  1561. #endif
  1562. mov->total_streams = nb = s->nb_streams;
  1563. #if 1
  1564. for(i=0; i<s->nb_streams;) {
  1565. if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
  1566. av_free(s->streams[i]);
  1567. for(j=i+1; j<s->nb_streams; j++)
  1568. s->streams[j-1] = s->streams[j];
  1569. s->nb_streams--;
  1570. } else
  1571. i++;
  1572. }
  1573. for(i=0; i<s->nb_streams;i++) {
  1574. MOVStreamContext *sc;
  1575. sc = (MOVStreamContext *)s->streams[i]->priv_data;
  1576. sc->ffindex = i;
  1577. sc->is_ff_stream = 1;
  1578. }
  1579. #endif
  1580. #ifdef DEBUG
  1581. av_log(NULL, AV_LOG_DEBUG, "real streams= %d\n", s->nb_streams);
  1582. #endif
  1583. return 0;
  1584. }
  1585. /* Yes, this is ugly... I didn't write the specs of QT :p */
  1586. /* XXX:remove useless commented code sometime */
  1587. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  1588. {
  1589. MOVContext *mov = (MOVContext *) s->priv_data;
  1590. MOVStreamContext *sc;
  1591. int64_t offset = INT64_MAX;
  1592. int64_t best_dts = INT64_MAX;
  1593. int i, a, b, m;
  1594. int size;
  1595. int idx;
  1596. size = 0x0FFFFFFF;
  1597. #ifdef MOV_SPLIT_CHUNKS
  1598. if (mov->partial) {
  1599. sc = mov->partial;
  1600. idx = sc->sample_to_chunk_index;
  1601. if (idx < 0) return 0;
  1602. #ifdef DEBUG
  1603. fprintf(stderr, "sc[ffid %d]->sample_size = %d\n", sc->ffindex, sc->sample_size);
  1604. #endif
  1605. //size = sc->sample_sizes[sc->current_sample];
  1606. // that ain't working...
  1607. //size = (sc->sample_size)?sc->sample_size:sc->sample_sizes[sc->current_sample];
  1608. size = (sc->sample_size > 1)?sc->sample_size:sc->sample_sizes[sc->current_sample];
  1609. sc->current_sample++;
  1610. sc->left_in_chunk--;
  1611. if (sc->left_in_chunk <= 0)
  1612. mov->partial = 0;
  1613. offset = mov->next_chunk_offset;
  1614. /* extract the sample */
  1615. goto readchunk;
  1616. }
  1617. #endif
  1618. again:
  1619. sc = 0;
  1620. if(offset == INT64_MAX)
  1621. best_dts= INT64_MAX;
  1622. for(i=0; i<mov->total_streams; i++) {
  1623. MOVStreamContext *msc = mov->streams[i];
  1624. if ((msc->next_chunk < msc->chunk_count) && msc->next_chunk >= 0){
  1625. if (msc->sample_to_time_index < msc->stts_count && mov->ni) {
  1626. int64_t dts;
  1627. int index= msc->sample_to_time_index;
  1628. int sample= msc->sample_to_time_sample;
  1629. int time= msc->sample_to_time_time;
  1630. int duration = msc->stts_data[index].duration;
  1631. int count = msc->stts_data[index].count;
  1632. if (sample + count < msc->current_sample) {
  1633. sample += count;
  1634. time += count*duration;
  1635. index ++;
  1636. duration = msc->stts_data[index].duration;
  1637. }
  1638. dts = time + (msc->current_sample-1 - sample) * (int64_t)duration;
  1639. dts = av_rescale(dts, AV_TIME_BASE, msc->time_scale);
  1640. // av_log(NULL, AV_LOG_DEBUG, "%d %Ld %Ld %Ld \n", i, dts, best_dts, offset);
  1641. if(dts < best_dts){
  1642. best_dts= dts;
  1643. sc = msc;
  1644. offset = msc->chunk_offsets[msc->next_chunk];
  1645. }
  1646. }else{
  1647. //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));
  1648. if ((msc->chunk_offsets[msc->next_chunk] < offset)) {
  1649. sc = msc;
  1650. offset = msc->chunk_offsets[msc->next_chunk];
  1651. //av_log(NULL, AV_LOG_DEBUG, "SELETED %Ld i:%d\n", offset, i);
  1652. }
  1653. }
  1654. }
  1655. }
  1656. if (!sc || offset==INT64_MAX)
  1657. return -1;
  1658. sc->next_chunk++;
  1659. if(mov->next_chunk_offset < offset) { /* some meta data */
  1660. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1661. mov->next_chunk_offset = offset;
  1662. }
  1663. //av_log(NULL, AV_LOG_DEBUG, "chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
  1664. if(!sc->is_ff_stream || (s->streams[sc->ffindex]->discard >= AVDISCARD_ALL)) {
  1665. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1666. mov->next_chunk_offset = offset;
  1667. offset = INT64_MAX;
  1668. goto again;
  1669. }
  1670. /* now get the chunk size... */
  1671. for(i=0; i<mov->total_streams; i++) {
  1672. MOVStreamContext *msc = mov->streams[i];
  1673. if ((msc->next_chunk < msc->chunk_count)
  1674. && msc->chunk_offsets[msc->next_chunk] - offset < size
  1675. && msc->chunk_offsets[msc->next_chunk] > offset)
  1676. size = msc->chunk_offsets[msc->next_chunk] - offset;
  1677. }
  1678. #ifdef MOV_MINOLTA_FIX
  1679. //Make sure that size is according to sample_size (Needed by .mov files
  1680. //created on a Minolta Dimage Xi where audio chunks contains waste data in the end)
  1681. //Maybe we should really not only check sc->sample_size, but also sc->sample_sizes
  1682. //but I have no such movies
  1683. if (sc->sample_size > 0) {
  1684. int foundsize=0;
  1685. for(i=0; i<(sc->sample_to_chunk_sz); i++) {
  1686. if( (sc->sample_to_chunk[i].first)<=(sc->next_chunk) )
  1687. {
  1688. // I can't figure out why for PCM audio sample_size is always 1
  1689. // (it should actually be channels*bits_per_second/8) but it is.
  1690. AVCodecContext* cod = &s->streams[sc->ffindex]->codec;
  1691. if (sc->sample_size == 1 && (cod->codec_id == CODEC_ID_PCM_S16BE || cod->codec_id == CODEC_ID_PCM_S16LE))
  1692. foundsize=(sc->sample_to_chunk[i].count*cod->channels*cod->bits_per_sample)/8;
  1693. else
  1694. foundsize=sc->sample_to_chunk[i].count*sc->sample_size;
  1695. }
  1696. #ifdef DEBUG
  1697. 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);
  1698. #endif
  1699. }
  1700. if( (foundsize>0) && (foundsize<size) )
  1701. {
  1702. #ifdef DEBUG
  1703. /*av_log(NULL, AV_LOG_DEBUG, "this size should actually be %d\n",foundsize);*/
  1704. #endif
  1705. size=foundsize;
  1706. }
  1707. }
  1708. #endif //MOV_MINOLTA_FIX
  1709. idx = sc->sample_to_chunk_index;
  1710. if (idx + 1 < sc->sample_to_chunk_sz && sc->next_chunk >= sc->sample_to_chunk[idx + 1].first)
  1711. idx++;
  1712. sc->sample_to_chunk_index = idx;
  1713. #ifdef MOV_SPLIT_CHUNKS
  1714. /* split chunks into samples */
  1715. if (sc->sample_size == 0 || sc->sample_size > 100) {
  1716. if (idx >= 0 && sc->sample_to_chunk[idx].count != 1) {
  1717. mov->partial = sc;
  1718. /* we'll have to get those samples before next chunk */
  1719. sc->left_in_chunk = sc->sample_to_chunk[idx].count - 1;
  1720. size = (sc->sample_size > 1)?sc->sample_size:sc->sample_sizes[sc->current_sample];
  1721. }
  1722. sc->current_sample++;
  1723. }else if(idx + 1 < sc->sample_to_chunk_sz){
  1724. sc->current_sample += sc->sample_size * sc->sample_to_chunk[idx].count;
  1725. }
  1726. #endif
  1727. readchunk:
  1728. #ifdef DEBUG
  1729. av_log(NULL, AV_LOG_DEBUG, "chunk: %lli -> %lli (%i)\n", offset, offset + size, size);
  1730. #endif
  1731. if(size == 0x0FFFFFFF)
  1732. size = mov->mdat_size + mov->mdat_offset - offset;
  1733. if(size < 0)
  1734. return -1;
  1735. if(size == 0)
  1736. return -1;
  1737. url_fseek(&s->pb, offset, SEEK_SET);
  1738. av_get_packet(&s->pb, pkt, size);
  1739. pkt->stream_index = sc->ffindex;
  1740. // If the keyframes table exists, mark any samples that are in the table as key frames.
  1741. // If no table exists, treat very sample as a key frame.
  1742. if (sc->keyframes) {
  1743. a = 0;
  1744. b = sc->keyframe_count - 1;
  1745. while (a < b) {
  1746. m = (a + b + 1) >> 1;
  1747. if (sc->keyframes[m] > sc->current_sample) {
  1748. b = m - 1;
  1749. } else {
  1750. a = m;
  1751. }
  1752. }
  1753. if (sc->keyframes[a] == sc->current_sample)
  1754. pkt->flags |= PKT_FLAG_KEY;
  1755. }
  1756. else
  1757. pkt->flags |= PKT_FLAG_KEY;
  1758. #ifdef DEBUG
  1759. /*
  1760. av_log(NULL, AV_LOG_DEBUG, "Packet (%d, %ld) ", pkt->stream_index, pkt->size);
  1761. for(i=0; i<8; i++)
  1762. av_log(NULL, AV_LOG_DEBUG, "%02x ", pkt->data[i]);
  1763. for(i=0; i<8; i++)
  1764. av_log(NULL, AV_LOG_DEBUG, "%c ", (pkt->data[i]) & 0x7F);
  1765. av_log(NULL, AV_LOG_DEBUG, "\n");
  1766. */
  1767. #endif
  1768. mov->next_chunk_offset = offset + size;
  1769. /* find the corresponding dts */
  1770. if (sc && sc->sample_to_time_index < sc->stts_count && pkt) {
  1771. unsigned int count;
  1772. uint64_t dts, pts;
  1773. unsigned int duration = sc->stts_data[sc->sample_to_time_index].duration;
  1774. count = sc->stts_data[sc->sample_to_time_index].count;
  1775. if ((sc->sample_to_time_sample + count) < sc->current_sample) {
  1776. sc->sample_to_time_sample += count;
  1777. sc->sample_to_time_time += count*duration;
  1778. sc->sample_to_time_index ++;
  1779. duration = sc->stts_data[sc->sample_to_time_index].duration;
  1780. }
  1781. dts = sc->sample_to_time_time + (sc->current_sample-1 - sc->sample_to_time_sample) * (int64_t)duration;
  1782. /* find the corresponding pts */
  1783. if (sc->sample_to_ctime_index < sc->ctts_count) {
  1784. int duration = sc->ctts_data[sc->sample_to_ctime_index].duration;
  1785. int count = sc->ctts_data[sc->sample_to_ctime_index].count;
  1786. if ((sc->sample_to_ctime_sample + count) < sc->current_sample) {
  1787. sc->sample_to_ctime_sample += count;
  1788. sc->sample_to_ctime_index ++;
  1789. duration = sc->ctts_data[sc->sample_to_ctime_index].duration;
  1790. }
  1791. pts = dts + duration;
  1792. }else
  1793. pts = dts;
  1794. pkt->pts = av_rescale( pts,
  1795. (int64_t)s->streams[sc->ffindex]->time_base.den,
  1796. (int64_t)sc->time_scale * (int64_t)s->streams[sc->ffindex]->time_base.num );
  1797. pkt->dts = av_rescale( dts,
  1798. (int64_t)s->streams[sc->ffindex]->time_base.den,
  1799. (int64_t)sc->time_scale * (int64_t)s->streams[sc->ffindex]->time_base.num );
  1800. #ifdef DEBUG
  1801. 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"
  1802. , pkt->stream_index, sc->current_sample-1, pkt->dts, pkt->pts
  1803. , sc->sample_to_time_sample
  1804. , sc->sample_to_time_time
  1805. , sc->sample_to_time_index
  1806. , sc->stts_count
  1807. , count
  1808. , duration);
  1809. #endif
  1810. }
  1811. return 0;
  1812. }
  1813. #if defined(MOV_SPLIT_CHUNKS) && defined(MOV_SEEK)
  1814. /**
  1815. * Seek method based on the one described in the Appendix C of QTFileFormat.pdf
  1816. */
  1817. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  1818. {
  1819. MOVContext* mov = (MOVContext *) s->priv_data;
  1820. MOVStreamContext* sc;
  1821. int32_t i, a, b, m;
  1822. int64_t sample_time;
  1823. int64_t start_time;
  1824. int32_t seek_sample, sample;
  1825. int32_t duration;
  1826. int32_t count;
  1827. int32_t chunk;
  1828. int32_t left_in_chunk;
  1829. int64_t chunk_file_offset;
  1830. int64_t sample_file_offset;
  1831. int32_t first_chunk_sample;
  1832. int32_t sample_to_chunk_idx;
  1833. int sample_to_time_index;
  1834. long sample_to_time_sample = 0;
  1835. uint64_t sample_to_time_time = 0;
  1836. int mov_idx;
  1837. // Find the corresponding mov stream
  1838. for (mov_idx = 0; mov_idx < mov->total_streams; mov_idx++)
  1839. if (mov->streams[mov_idx]->ffindex == stream_index)
  1840. break;
  1841. if (mov_idx == mov->total_streams) {
  1842. av_log(s, AV_LOG_ERROR, "mov: requested stream was not found in mov streams (idx=%i)\n", stream_index);
  1843. return -1;
  1844. }
  1845. sc = mov->streams[mov_idx];
  1846. // Step 1. Find the edit that contains the requested time (elst)
  1847. if (sc->edit_count) {
  1848. // FIXME should handle edit list
  1849. av_log(s, AV_LOG_ERROR, "mov: does not handle seeking in files that contain edit list (c:%d)\n", sc->edit_count);
  1850. return -1;
  1851. }
  1852. // Step 2. Find the corresponding sample using the Time-to-sample atom (stts) */
  1853. #ifdef DEBUG
  1854. av_log(s, AV_LOG_DEBUG, "Searching for time %li in stream #%i (time_scale=%i)\n", (long)timestamp, mov_idx, sc->time_scale);
  1855. #endif
  1856. // convert timestamp from time_base unit to timescale unit
  1857. sample_time = av_rescale( timestamp,
  1858. (int64_t)sc->time_scale * s->streams[stream_index]->time_base.num,
  1859. (int64_t)s->streams[stream_index]->time_base.den);
  1860. start_time = 0; // FIXME use elst atom
  1861. sample = 1; // sample are 0 based in table
  1862. #ifdef DEBUG
  1863. av_log(s, AV_LOG_DEBUG, "Searching for sample_time %li \n", (long)sample_time);
  1864. #endif
  1865. for (i = 0; i < sc->stts_count; i++) {
  1866. count = sc->stts_data[i].count;
  1867. duration = sc->stts_data[i].duration;
  1868. //av_log(s, AV_LOG_DEBUG, "> sample_time %lli \n", (long)sample_time);
  1869. //av_log(s, AV_LOG_DEBUG, "> count=%i duration=%i\n", count, duration);
  1870. if ((start_time + count*duration) > sample_time) {
  1871. sample_to_time_time = start_time;
  1872. sample_to_time_index = i;
  1873. sample_to_time_sample = sample;
  1874. sample += (sample_time - start_time) / duration;
  1875. break;
  1876. }
  1877. sample += count;
  1878. start_time += count * duration;
  1879. }
  1880. sample_to_time_time = start_time;
  1881. sample_to_time_index = i;
  1882. /* NOTE: despite what qt doc say, the dt value (Display Time in qt vocabulary) computed with the stts atom
  1883. is a decoding time stamp (dts) not a presentation time stamp. And as usual dts != pts for stream with b frames */
  1884. #ifdef DEBUG
  1885. av_log(s, AV_LOG_DEBUG, "Found time %li at sample #%u\n", (long)sample_time, sample);
  1886. #endif
  1887. if (sample > sc->sample_count) {
  1888. 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);
  1889. return -1;
  1890. }
  1891. // Step 3. Find the prior sync. sample using the Sync sample atom (stss)
  1892. if (sc->keyframes) {
  1893. a = 0;
  1894. b = sc->keyframe_count - 1;
  1895. while (a < b) {
  1896. m = (a + b + 1) >> 1;
  1897. if (sc->keyframes[m] > sample) {
  1898. b = m - 1;
  1899. } else {
  1900. a = m;
  1901. }
  1902. #ifdef DEBUG
  1903. // 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);
  1904. #endif
  1905. }
  1906. // for low latency prob: always use the previous keyframe, just uncomment the next line
  1907. // if (a) a--;
  1908. seek_sample = sc->keyframes[a];
  1909. }
  1910. else
  1911. seek_sample = sample; // else all samples are key frames
  1912. #ifdef DEBUG
  1913. av_log(s, AV_LOG_DEBUG, "Found nearest keyframe at sample #%i \n", seek_sample);
  1914. #endif
  1915. // Step 4. Find the chunk of the sample using the Sample-to-chunk-atom (stsc)
  1916. for (first_chunk_sample = 1, i = 0; i < (sc->sample_to_chunk_sz - 1); i++) {
  1917. b = (sc->sample_to_chunk[i + 1].first - sc->sample_to_chunk[i].first) * sc->sample_to_chunk[i].count;
  1918. if (seek_sample >= first_chunk_sample && seek_sample < (first_chunk_sample + b))
  1919. break;
  1920. first_chunk_sample += b;
  1921. }
  1922. chunk = sc->sample_to_chunk[i].first + (seek_sample - first_chunk_sample) / sc->sample_to_chunk[i].count;
  1923. left_in_chunk = sc->sample_to_chunk[i].count - (seek_sample - first_chunk_sample) % sc->sample_to_chunk[i].count;
  1924. first_chunk_sample += ((seek_sample - first_chunk_sample) / sc->sample_to_chunk[i].count) * sc->sample_to_chunk[i].count;
  1925. sample_to_chunk_idx = i;
  1926. #ifdef DEBUG
  1927. 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);
  1928. #endif
  1929. // Step 5. Find the offset of the chunk using the chunk offset atom
  1930. if (!sc->chunk_offsets) {
  1931. av_log(s, AV_LOG_ERROR, "mov: no chunk offset atom, unable to seek\n");
  1932. return -1;
  1933. }
  1934. if (chunk > sc->chunk_count) {
  1935. 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);
  1936. return -1;
  1937. }
  1938. chunk_file_offset = sc->chunk_offsets[chunk - 1];
  1939. #ifdef DEBUG
  1940. av_log(s, AV_LOG_DEBUG, "Chunk file offset is #%llu \n", chunk_file_offset);
  1941. #endif
  1942. // Step 6. Find the byte offset within the chunk using the sample size atom
  1943. sample_file_offset = chunk_file_offset;
  1944. if (sc->sample_size)
  1945. sample_file_offset += (seek_sample - first_chunk_sample) * sc->sample_size;
  1946. else {
  1947. for (i = 0; i < (seek_sample - first_chunk_sample); i++) {
  1948. sample_file_offset += sc->sample_sizes[first_chunk_sample + i - 1];
  1949. }
  1950. }
  1951. #ifdef DEBUG
  1952. av_log(s, AV_LOG_DEBUG, "Sample file offset is #%llu \n", sample_file_offset);
  1953. #endif
  1954. // Step 6. Update the parser
  1955. mov->partial = sc;
  1956. mov->next_chunk_offset = sample_file_offset;
  1957. // Update current stream state
  1958. sc->current_sample = seek_sample - 1; // zero based
  1959. sc->left_in_chunk = left_in_chunk;
  1960. sc->next_chunk = chunk; // +1 -1 (zero based)
  1961. sc->sample_to_chunk_index = sample_to_chunk_idx;
  1962. // Update other streams
  1963. for (i = 0; i<mov->total_streams; i++) {
  1964. MOVStreamContext *msc;
  1965. if (i == mov_idx) continue;
  1966. // Find the nearest 'next' chunk
  1967. msc = mov->streams[i];
  1968. a = 0;
  1969. b = msc->chunk_count - 1;
  1970. while (a < b) {
  1971. m = (a + b + 1) >> 1;
  1972. if (msc->chunk_offsets[m] > chunk_file_offset) {
  1973. b = m - 1;
  1974. } else {
  1975. a = m;
  1976. }
  1977. #ifdef DEBUG
  1978. /* av_log(s, AV_LOG_DEBUG, "a=%i (%li) b=%i (%li) m=%i (%li) stream #%i\n"
  1979. , a, (long)msc->chunk_offsets[a], b, (long)msc->chunk_offsets[b], m, (long)msc->chunk_offsets[m], i); */
  1980. #endif
  1981. }
  1982. msc->next_chunk = a;
  1983. if (msc->chunk_offsets[a] < chunk_file_offset && a < (msc->chunk_count-1))
  1984. msc->next_chunk ++;
  1985. #ifdef DEBUG
  1986. 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]);
  1987. #endif
  1988. // Compute sample count and index in the sample_to_chunk table (what a pity)
  1989. msc->sample_to_chunk_index = 0;
  1990. msc->current_sample = 0;
  1991. for(; msc->sample_to_chunk_index < (msc->sample_to_chunk_sz - 1)
  1992. && msc->sample_to_chunk[msc->sample_to_chunk_index + 1].first <= (1 + msc->next_chunk); msc->sample_to_chunk_index++) {
  1993. msc->current_sample += (msc->sample_to_chunk[msc->sample_to_chunk_index + 1].first - msc->sample_to_chunk[msc->sample_to_chunk_index].first) \
  1994. * msc->sample_to_chunk[msc->sample_to_chunk_index].count;
  1995. }
  1996. 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;
  1997. msc->left_in_chunk = msc->sample_to_chunk[msc->sample_to_chunk_index].count - 1;
  1998. // Find corresponding position in stts (used later to compute dts)
  1999. sample = 0;
  2000. start_time = 0;
  2001. for (msc->sample_to_time_index = 0; msc->sample_to_time_index < msc->stts_count; msc->sample_to_time_index++) {
  2002. count = msc->stts_data[msc->sample_to_time_index].count;
  2003. duration = msc->stts_data[msc->sample_to_time_index].duration;
  2004. if ((sample + count - 1) > msc->current_sample) {
  2005. msc->sample_to_time_time = start_time;
  2006. msc->sample_to_time_sample = sample;
  2007. break;
  2008. }
  2009. sample += count;
  2010. start_time += count * duration;
  2011. }
  2012. sample = 0;
  2013. for (msc->sample_to_ctime_index = 0; msc->sample_to_ctime_index < msc->ctts_count; msc->sample_to_ctime_index++) {
  2014. count = msc->ctts_data[msc->sample_to_ctime_index].count;
  2015. duration = msc->ctts_data[msc->sample_to_ctime_index].duration;
  2016. if ((sample + count - 1) > msc->current_sample) {
  2017. msc->sample_to_ctime_sample = sample;
  2018. break;
  2019. }
  2020. sample += count;
  2021. }
  2022. #ifdef DEBUG
  2023. 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);
  2024. #endif
  2025. }
  2026. return 0;
  2027. }
  2028. #endif
  2029. static int mov_read_close(AVFormatContext *s)
  2030. {
  2031. int i;
  2032. MOVContext *mov = (MOVContext *) s->priv_data;
  2033. for(i=0; i<mov->total_streams; i++)
  2034. mov_free_stream_context(mov->streams[i]);
  2035. /* free color tabs */
  2036. for(i=0; i<mov->ctab_size; i++)
  2037. av_freep(&mov->ctab[i]);
  2038. av_freep(&mov->ctab);
  2039. return 0;
  2040. }
  2041. static AVInputFormat mov_iformat = {
  2042. "mov,mp4,m4a,3gp,3g2",
  2043. "QuickTime/MPEG4 format",
  2044. sizeof(MOVContext),
  2045. mov_probe,
  2046. mov_read_header,
  2047. mov_read_packet,
  2048. mov_read_close,
  2049. #if defined(MOV_SPLIT_CHUNKS) && defined(MOV_SEEK)
  2050. mov_read_seek,
  2051. #endif
  2052. };
  2053. int mov_init(void)
  2054. {
  2055. av_register_input_format(&mov_iformat);
  2056. return 0;
  2057. }