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.

2436 lines
85KB

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