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.

2199 lines
78KB

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