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.

2282 lines
79KB

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