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.

2168 lines
76KB

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