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.

2272 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., 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_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
  95. { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */
  96. { CODEC_ID_H263, MKTAG('s', '2', '6', '3') }, /* H263 ?? works */
  97. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
  98. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
  99. /* { CODEC_ID_DVVIDEO, MKTAG('A', 'V', 'd', 'v') }, *//* AVID dv */
  100. { CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') }, /* On2 VP3 */
  101. { CODEC_ID_RPZA, MKTAG('r', 'p', 'z', 'a') }, /* Apple Video (RPZA) */
  102. { CODEC_ID_CINEPAK, MKTAG('c', 'v', 'i', 'd') }, /* Cinepak */
  103. { CODEC_ID_8BPS, MKTAG('8', 'B', 'P', 'S') }, /* Planar RGB (8BPS) */
  104. { CODEC_ID_SMC, MKTAG('s', 'm', 'c', ' ') }, /* Apple Graphics (SMC) */
  105. { CODEC_ID_QTRLE, MKTAG('r', 'l', 'e', ' ') }, /* Apple Animation (RLE) */
  106. { CODEC_ID_QDRAW, MKTAG('q', 'd', 'r', 'w') }, /* QuickDraw */
  107. { CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') }, /* AVC-1/H.264 */
  108. { CODEC_ID_NONE, 0 },
  109. };
  110. static const CodecTag mov_audio_tags[] = {
  111. /* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
  112. { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
  113. /* { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') },*/ /* 8 bits */
  114. { CODEC_ID_PCM_U8, MKTAG('r', 'a', 'w', ' ') }, /* 8 bits unsigned */
  115. { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */
  116. { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */
  117. { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */
  118. { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
  119. { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') }, /* Macintosh Audio Compression and Expansion 3:1 */
  120. { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') }, /* Macintosh Audio Compression and Expansion 6:1 */
  121. { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
  122. { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
  123. { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
  124. /* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
  125. /* MP4 tags */
  126. { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, /* MPEG-4 AAC */
  127. /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
  128. { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') }, /* AMR-NB 3gp */
  129. { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') }, /* AMR-WB 3gp */
  130. { CODEC_ID_AC3, MKTAG('m', 's', 0x20, 0x00) }, /* Dolby AC-3 */
  131. { CODEC_ID_NONE, 0 },
  132. };
  133. /* the QuickTime file format is quite convoluted...
  134. * it has lots of index tables, each indexing something in another one...
  135. * Here we just use what is needed to read the chunks
  136. */
  137. typedef struct MOV_sample_to_chunk_tbl {
  138. long first;
  139. long count;
  140. long id;
  141. } MOV_sample_to_chunk_tbl;
  142. typedef struct {
  143. uint32_t type;
  144. int64_t offset;
  145. int64_t size; /* total size (excluding the size and type fields) */
  146. } MOV_atom_t;
  147. typedef struct {
  148. int seed;
  149. int flags;
  150. int size;
  151. void* clrs;
  152. } MOV_ctab_t;
  153. typedef struct {
  154. uint8_t version;
  155. uint32_t flags; // 24bit
  156. /* 0x03 ESDescrTag */
  157. uint16_t es_id;
  158. #define MP4ODescrTag 0x01
  159. #define MP4IODescrTag 0x02
  160. #define MP4ESDescrTag 0x03
  161. #define MP4DecConfigDescrTag 0x04
  162. #define MP4DecSpecificDescrTag 0x05
  163. #define MP4SLConfigDescrTag 0x06
  164. #define MP4ContentIdDescrTag 0x07
  165. #define MP4SupplContentIdDescrTag 0x08
  166. #define MP4IPIPtrDescrTag 0x09
  167. #define MP4IPMPPtrDescrTag 0x0A
  168. #define MP4IPMPDescrTag 0x0B
  169. #define MP4RegistrationDescrTag 0x0D
  170. #define MP4ESIDIncDescrTag 0x0E
  171. #define MP4ESIDRefDescrTag 0x0F
  172. #define MP4FileIODescrTag 0x10
  173. #define MP4FileODescrTag 0x11
  174. #define MP4ExtProfileLevelDescrTag 0x13
  175. #define MP4ExtDescrTagsStart 0x80
  176. #define MP4ExtDescrTagsEnd 0xFE
  177. uint8_t stream_priority;
  178. /* 0x04 DecConfigDescrTag */
  179. uint8_t object_type_id;
  180. uint8_t stream_type;
  181. /* XXX: really streamType is
  182. * only 6bit, followed by:
  183. * 1bit upStream
  184. * 1bit reserved
  185. */
  186. uint32_t buffer_size_db; // 24
  187. uint32_t max_bitrate;
  188. uint32_t avg_bitrate;
  189. /* 0x05 DecSpecificDescrTag */
  190. uint8_t decoder_cfg_len;
  191. uint8_t *decoder_cfg;
  192. /* 0x06 SLConfigDescrTag */
  193. uint8_t sl_config_len;
  194. uint8_t *sl_config;
  195. } MOV_esds_t;
  196. struct MOVParseTableEntry;
  197. typedef struct Time2Sample{
  198. int count;
  199. int duration;
  200. }Time2Sample;
  201. typedef struct MOVStreamContext {
  202. int ffindex; /* the ffmpeg stream id */
  203. int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
  204. long next_chunk;
  205. long chunk_count;
  206. int64_t *chunk_offsets;
  207. int stts_count;
  208. Time2Sample *stts_data;
  209. int ctts_count;
  210. Time2Sample *ctts_data;
  211. int edit_count; /* number of 'edit' (elst atom) */
  212. long sample_to_chunk_sz;
  213. MOV_sample_to_chunk_tbl *sample_to_chunk;
  214. long sample_to_chunk_index;
  215. int sample_to_time_index;
  216. long sample_to_time_sample;
  217. uint64_t sample_to_time_time;
  218. int sample_to_ctime_index;
  219. int sample_to_ctime_sample;
  220. long sample_size;
  221. long sample_count;
  222. long *sample_sizes;
  223. long keyframe_count;
  224. long *keyframes;
  225. int time_scale;
  226. long current_sample;
  227. long left_in_chunk; /* how many samples before next chunk */
  228. /* specific MPEG4 header which is added at the beginning of the stream */
  229. unsigned int header_len;
  230. uint8_t *header_data;
  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 total_streams;
  243. /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
  244. * but we need the info to be able to skip data from those streams in the 'mdat' section
  245. */
  246. MOVStreamContext *streams[MAX_STREAMS];
  247. int64_t next_chunk_offset;
  248. MOVStreamContext *partial; /* != 0 : there is still to read in the current chunk */
  249. int ctab_size;
  250. MOV_ctab_t **ctab; /* color tables */
  251. const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
  252. /* NOTE: for recursion save to/ restore from local variable! */
  253. AVPaletteControl palette_control;
  254. } MOVContext;
  255. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  256. /* those functions parse an atom */
  257. /* return code:
  258. 1: found what I wanted, exit
  259. 0: continue to parse next atom
  260. -1: error occured, exit
  261. */
  262. typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
  263. /* links atom IDs to parse functions */
  264. typedef struct MOVParseTableEntry {
  265. uint32_t type;
  266. mov_parse_function func;
  267. } MOVParseTableEntry;
  268. #ifdef DEBUG
  269. /*
  270. * XXX: static sux, even more in a multithreaded environment...
  271. * Avoid them. This is here just to help debugging.
  272. */
  273. static int debug_indent = 0;
  274. void print_atom(const char *str, MOV_atom_t atom)
  275. {
  276. unsigned int tag, i;
  277. tag = (unsigned int) atom.type;
  278. i=debug_indent;
  279. if(tag == 0) tag = MKTAG('N', 'U', 'L', 'L');
  280. while(i--)
  281. av_log(NULL, AV_LOG_DEBUG, "|");
  282. av_log(NULL, AV_LOG_DEBUG, "parse:");
  283. av_log(NULL, AV_LOG_DEBUG, " %s: tag=%c%c%c%c offset=0x%x size=0x%x\n",
  284. str, tag & 0xff,
  285. (tag >> 8) & 0xff,
  286. (tag >> 16) & 0xff,
  287. (tag >> 24) & 0xff,
  288. (unsigned int)atom.offset,
  289. (unsigned int)atom.size);
  290. assert((unsigned int)atom.size < 0x7fffffff);// catching errors
  291. }
  292. #else
  293. #define print_atom(a,b)
  294. #endif
  295. static int mov_read_leaf(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  296. {
  297. print_atom("leaf", atom);
  298. if (atom.size>1)
  299. url_fskip(pb, atom.size);
  300. /* url_seek(pb, atom_offset+atom.size, SEEK_SET); */
  301. return 0;
  302. }
  303. static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  304. {
  305. int64_t total_size = 0;
  306. MOV_atom_t a;
  307. int i;
  308. int err = 0;
  309. #ifdef DEBUG
  310. print_atom("default", atom);
  311. debug_indent++;
  312. #endif
  313. a.offset = atom.offset;
  314. if (atom.size < 0)
  315. atom.size = 0x7fffffffffffffffLL;
  316. while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
  317. a.size = atom.size;
  318. a.type=0L;
  319. if(atom.size >= 8) {
  320. a.size = get_be32(pb);
  321. a.type = get_le32(pb);
  322. }
  323. total_size += 8;
  324. a.offset += 8;
  325. //av_log(NULL, AV_LOG_DEBUG, "type: %08x %.4s sz: %Lx %Lx %Lx\n", type, (char*)&type, size, atom.size, total_size);
  326. if (a.size == 1) { /* 64 bit extended size */
  327. a.size = get_be64(pb) - 8;
  328. a.offset += 8;
  329. total_size += 8;
  330. }
  331. if (a.size == 0) {
  332. a.size = atom.size - total_size;
  333. if (a.size <= 8)
  334. break;
  335. }
  336. for (i = 0; c->parse_table[i].type != 0L
  337. && c->parse_table[i].type != a.type; i++)
  338. /* empty */;
  339. a.size -= 8;
  340. if(a.size < 0)
  341. break;
  342. // av_log(NULL, AV_LOG_DEBUG, " i=%ld\n", i);
  343. if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
  344. // url_seek(pb, atom.offset+atom.size, SEEK_SET);
  345. #ifdef DEBUG
  346. print_atom("unknown", a);
  347. #endif
  348. url_fskip(pb, a.size);
  349. } else {
  350. #ifdef DEBUG
  351. //char b[5] = { type & 0xff, (type >> 8) & 0xff, (type >> 16) & 0xff, (type >> 24) & 0xff, 0 };
  352. //print_atom(b, type, offset, size);
  353. #endif
  354. err = (c->parse_table[i].func)(c, pb, a);
  355. }
  356. a.offset += a.size;
  357. total_size += a.size;
  358. }
  359. if (!err && total_size < atom.size && atom.size < 0x7ffff) {
  360. //av_log(NULL, AV_LOG_DEBUG, "RESET %Ld %Ld err:%d\n", atom.size, total_size, err);
  361. url_fskip(pb, atom.size - total_size);
  362. }
  363. #ifdef DEBUG
  364. debug_indent--;
  365. #endif
  366. return err;
  367. }
  368. static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  369. {
  370. #if 1
  371. url_fskip(pb, atom.size); // for now
  372. #else
  373. VERY VERY BROKEN, NEVER execute this, needs rewrite
  374. unsigned int len;
  375. MOV_ctab_t *t;
  376. c->ctab = av_realloc(c->ctab, ++c->ctab_size);
  377. t = c->ctab[c->ctab_size];
  378. t->seed = get_be32(pb);
  379. t->flags = get_be16(pb);
  380. t->size = get_be16(pb) + 1;
  381. len = 2 * t->size * 4;
  382. if (len > 0) {
  383. t->clrs = av_malloc(len); // 16bit A R G B
  384. if (t->clrs)
  385. get_buffer(pb, t->clrs, len);
  386. }
  387. #endif
  388. return 0;
  389. }
  390. static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  391. {
  392. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  393. int len = 0;
  394. uint32_t type;
  395. uint32_t ctype;
  396. print_atom("hdlr", atom);
  397. get_byte(pb); /* version */
  398. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  399. /* component type */
  400. ctype = get_le32(pb);
  401. type = get_le32(pb); /* component subtype */
  402. #ifdef DEBUG
  403. av_log(NULL, AV_LOG_DEBUG, "ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
  404. av_log(NULL, AV_LOG_DEBUG, "stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
  405. #endif
  406. #ifdef DEBUG
  407. /* XXX: yeah this is ugly... */
  408. if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
  409. if(type == MKTAG('v', 'i', 'd', 'e'))
  410. puts("hdlr: vide");
  411. else if(type == MKTAG('s', 'o', 'u', 'n'))
  412. puts("hdlr: soun");
  413. } else if(ctype == 0) { /* MP4 */
  414. if(type == MKTAG('v', 'i', 'd', 'e'))
  415. puts("hdlr: vide");
  416. else if(type == MKTAG('s', 'o', 'u', 'n'))
  417. puts("hdlr: soun");
  418. else if(type == MKTAG('o', 'd', 's', 'm'))
  419. puts("hdlr: odsm");
  420. else if(type == MKTAG('s', 'd', 's', 'm'))
  421. puts("hdlr: sdsm");
  422. } else puts("hdlr: meta");
  423. #endif
  424. if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
  425. /* helps parsing the string hereafter... */
  426. c->mp4 = 0;
  427. if(type == MKTAG('v', 'i', 'd', 'e'))
  428. st->codec.codec_type = CODEC_TYPE_VIDEO;
  429. else if(type == MKTAG('s', 'o', 'u', 'n'))
  430. st->codec.codec_type = CODEC_TYPE_AUDIO;
  431. } else if(ctype == 0) { /* MP4 */
  432. /* helps parsing the string hereafter... */
  433. c->mp4 = 1;
  434. if(type == MKTAG('v', 'i', 'd', 'e'))
  435. st->codec.codec_type = CODEC_TYPE_VIDEO;
  436. else if(type == MKTAG('s', 'o', 'u', 'n'))
  437. st->codec.codec_type = CODEC_TYPE_AUDIO;
  438. }
  439. get_be32(pb); /* component manufacture */
  440. get_be32(pb); /* component flags */
  441. get_be32(pb); /* component flags mask */
  442. if(atom.size <= 24)
  443. return 0; /* nothing left to read */
  444. /* XXX: MP4 uses a C string, not a pascal one */
  445. /* component name */
  446. if(c->mp4) {
  447. /* .mp4: C string */
  448. while(get_byte(pb) && (++len < (atom.size - 24)));
  449. } else {
  450. /* .mov: PASCAL string */
  451. #ifdef DEBUG
  452. char* buf;
  453. #endif
  454. len = get_byte(pb);
  455. #ifdef DEBUG
  456. buf = (uint8_t*) av_malloc(len+1);
  457. if (buf) {
  458. get_buffer(pb, buf, len);
  459. buf[len] = '\0';
  460. av_log(NULL, AV_LOG_DEBUG, "**buf='%s'\n", buf);
  461. av_free(buf);
  462. } else
  463. #endif
  464. url_fskip(pb, len);
  465. }
  466. url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
  467. return 0;
  468. }
  469. static int mov_mp4_read_descr_len(ByteIOContext *pb)
  470. {
  471. int len = 0;
  472. int count = 4;
  473. while (count--) {
  474. int c = get_byte(pb);
  475. len = (len << 7) | (c & 0x7f);
  476. if (!(c & 0x80))
  477. break;
  478. }
  479. return len;
  480. }
  481. static int mov_mp4_read_descr(ByteIOContext *pb, int *tag)
  482. {
  483. int len;
  484. *tag = get_byte(pb);
  485. len = mov_mp4_read_descr_len(pb);
  486. #ifdef DEBUG
  487. av_log(NULL, AV_LOG_DEBUG, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
  488. #endif
  489. return len;
  490. }
  491. static inline unsigned int get_be24(ByteIOContext *s)
  492. {
  493. unsigned int val;
  494. val = get_byte(s) << 16;
  495. val |= get_byte(s) << 8;
  496. val |= get_byte(s);
  497. return val;
  498. }
  499. static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  500. {
  501. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  502. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  503. int64_t start_pos = url_ftell(pb);
  504. int tag, len;
  505. print_atom("esds", atom);
  506. /* Well, broken but suffisant for some MP4 streams */
  507. get_be32(pb); /* version + flags */
  508. len = mov_mp4_read_descr(pb, &tag);
  509. if (tag == MP4ESDescrTag) {
  510. get_be16(pb); /* ID */
  511. get_byte(pb); /* priority */
  512. } else
  513. get_be16(pb); /* ID */
  514. len = mov_mp4_read_descr(pb, &tag);
  515. if (tag == MP4DecConfigDescrTag) {
  516. sc->esds.object_type_id = get_byte(pb);
  517. sc->esds.stream_type = get_byte(pb);
  518. sc->esds.buffer_size_db = get_be24(pb);
  519. sc->esds.max_bitrate = get_be32(pb);
  520. sc->esds.avg_bitrate = get_be32(pb);
  521. len = mov_mp4_read_descr(pb, &tag);
  522. //av_log(NULL, AV_LOG_DEBUG, "LEN %d TAG %d m:%d a:%d\n", len, tag, sc->esds.max_bitrate, sc->esds.avg_bitrate);
  523. if (tag == MP4DecSpecificDescrTag) {
  524. #ifdef DEBUG
  525. av_log(NULL, AV_LOG_DEBUG, "Specific MPEG4 header len=%d\n", len);
  526. #endif
  527. st->codec.extradata = (uint8_t*) av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
  528. if (st->codec.extradata) {
  529. get_buffer(pb, st->codec.extradata, len);
  530. st->codec.extradata_size = len;
  531. }
  532. }
  533. }
  534. /* in any case, skip garbage */
  535. url_fskip(pb, atom.size - ((url_ftell(pb) - start_pos)));
  536. return 0;
  537. }
  538. /* this atom contains actual media data */
  539. static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  540. {
  541. print_atom("mdat", atom);
  542. if(atom.size == 0) /* wrong one (MP4) */
  543. return 0;
  544. c->found_mdat=1;
  545. c->mdat_offset = atom.offset;
  546. c->mdat_size = atom.size;
  547. if(c->found_moov)
  548. return 1; /* found both, just go */
  549. url_fskip(pb, atom.size);
  550. return 0; /* now go for moov */
  551. }
  552. /* this atom should contain all header atoms */
  553. static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  554. {
  555. int err;
  556. print_atom("moov", atom);
  557. err = mov_read_default(c, pb, atom);
  558. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  559. /* so we don't parse the whole file if over a network */
  560. c->found_moov=1;
  561. if(c->found_mdat)
  562. return 1; /* found both, just go */
  563. return 0; /* now go for mdat */
  564. }
  565. static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  566. {
  567. print_atom("mdhd", atom);
  568. get_byte(pb); /* version */
  569. get_byte(pb); get_byte(pb);
  570. get_byte(pb); /* flags */
  571. get_be32(pb); /* creation time */
  572. get_be32(pb); /* modification time */
  573. c->streams[c->fc->nb_streams-1]->time_scale = get_be32(pb);
  574. #ifdef DEBUG
  575. av_log(NULL, AV_LOG_DEBUG, "track[%i].time_scale = %i\n", c->fc->nb_streams-1, c->streams[c->fc->nb_streams-1]->time_scale); /* time scale */
  576. #endif
  577. get_be32(pb); /* duration */
  578. get_be16(pb); /* language */
  579. get_be16(pb); /* quality */
  580. return 0;
  581. }
  582. static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  583. {
  584. print_atom("mvhd", atom);
  585. get_byte(pb); /* version */
  586. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  587. get_be32(pb); /* creation time */
  588. get_be32(pb); /* modification time */
  589. c->time_scale = get_be32(pb); /* time scale */
  590. #ifdef DEBUG
  591. av_log(NULL, AV_LOG_DEBUG, "time scale = %i\n", c->time_scale);
  592. #endif
  593. c->duration = get_be32(pb); /* duration */
  594. get_be32(pb); /* preferred scale */
  595. get_be16(pb); /* preferred volume */
  596. url_fskip(pb, 10); /* reserved */
  597. url_fskip(pb, 36); /* display matrix */
  598. get_be32(pb); /* preview time */
  599. get_be32(pb); /* preview duration */
  600. get_be32(pb); /* poster time */
  601. get_be32(pb); /* selection time */
  602. get_be32(pb); /* selection duration */
  603. get_be32(pb); /* current time */
  604. get_be32(pb); /* next track ID */
  605. return 0;
  606. }
  607. static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  608. {
  609. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  610. if((uint64_t)atom.size > (1<<30))
  611. return -1;
  612. // currently SVQ3 decoder expect full STSD header - so let's fake it
  613. // this should be fixed and just SMI header should be passed
  614. av_free(st->codec.extradata);
  615. st->codec.extradata_size = 0x5a + atom.size;
  616. st->codec.extradata = (uint8_t*) av_mallocz(st->codec.extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  617. if (st->codec.extradata) {
  618. strcpy(st->codec.extradata, "SVQ3"); // fake
  619. get_buffer(pb, st->codec.extradata + 0x5a, atom.size);
  620. //av_log(NULL, AV_LOG_DEBUG, "Reading SMI %Ld %s\n", atom.size, (char*)st->codec.extradata + 0x5a);
  621. } else
  622. url_fskip(pb, atom.size);
  623. return 0;
  624. }
  625. static int mov_read_avcC(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  626. {
  627. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  628. if((uint64_t)atom.size > (1<<30))
  629. return -1;
  630. av_free(st->codec.extradata);
  631. st->codec.extradata_size = atom.size;
  632. st->codec.extradata = (uint8_t*) av_mallocz(st->codec.extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  633. if (st->codec.extradata) {
  634. get_buffer(pb, st->codec.extradata, atom.size);
  635. } else
  636. url_fskip(pb, atom.size);
  637. return 0;
  638. }
  639. static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  640. {
  641. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  642. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  643. unsigned int i, entries;
  644. print_atom("stco", atom);
  645. get_byte(pb); /* version */
  646. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  647. entries = get_be32(pb);
  648. if(entries >= UINT_MAX/sizeof(int64_t))
  649. return -1;
  650. sc->chunk_count = entries;
  651. sc->chunk_offsets = (int64_t*) av_malloc(entries * sizeof(int64_t));
  652. if (!sc->chunk_offsets)
  653. return -1;
  654. if (atom.type == MKTAG('s', 't', 'c', 'o')) {
  655. for(i=0; i<entries; i++) {
  656. sc->chunk_offsets[i] = get_be32(pb);
  657. }
  658. } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
  659. for(i=0; i<entries; i++) {
  660. sc->chunk_offsets[i] = get_be64(pb);
  661. }
  662. } else
  663. return -1;
  664. #ifdef DEBUG
  665. /*
  666. for(i=0; i<entries; i++) {
  667. av_log(NULL, AV_LOG_DEBUG, "chunk offset=0x%Lx\n", sc->chunk_offsets[i]);
  668. }
  669. */
  670. #endif
  671. return 0;
  672. }
  673. static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  674. {
  675. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  676. //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  677. int entries, frames_per_sample;
  678. uint32_t format;
  679. uint8_t codec_name[32];
  680. /* for palette traversal */
  681. int color_depth;
  682. int color_start;
  683. int color_count;
  684. int color_end;
  685. int color_index;
  686. int color_dec;
  687. int color_greyscale;
  688. unsigned char *color_table;
  689. int j;
  690. unsigned char r, g, b;
  691. print_atom("stsd", atom);
  692. get_byte(pb); /* version */
  693. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  694. entries = get_be32(pb);
  695. while(entries--) { //Parsing Sample description table
  696. enum CodecID id;
  697. int size = get_be32(pb); /* size */
  698. format = get_le32(pb); /* data format */
  699. get_be32(pb); /* reserved */
  700. get_be16(pb); /* reserved */
  701. get_be16(pb); /* index */
  702. /* for MPEG4: set codec type by looking for it */
  703. id = codec_get_id(mov_video_tags, format);
  704. if (id >= 0) {
  705. AVCodec *codec;
  706. codec = avcodec_find_decoder(id);
  707. if (codec)
  708. st->codec.codec_type = codec->type;
  709. }
  710. #ifdef DEBUG
  711. av_log(NULL, AV_LOG_DEBUG, "size=%d 4CC= %c%c%c%c codec_type=%d\n",
  712. size,
  713. (format >> 0) & 0xff,
  714. (format >> 8) & 0xff,
  715. (format >> 16) & 0xff,
  716. (format >> 24) & 0xff,
  717. st->codec.codec_type);
  718. #endif
  719. st->codec.codec_tag = format;
  720. if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
  721. MOV_atom_t a = { 0, 0, 0 };
  722. st->codec.codec_id = id;
  723. get_be16(pb); /* version */
  724. get_be16(pb); /* revision level */
  725. get_be32(pb); /* vendor */
  726. get_be32(pb); /* temporal quality */
  727. get_be32(pb); /* spacial quality */
  728. st->codec.width = get_be16(pb); /* width */
  729. st->codec.height = get_be16(pb); /* height */
  730. #if 1
  731. if (st->codec.codec_id == CODEC_ID_MPEG4) {
  732. /* in some MPEG4 the width/height are not correct, so
  733. we ignore this info */
  734. st->codec.width = 0;
  735. st->codec.height = 0;
  736. }
  737. #endif
  738. get_be32(pb); /* horiz resolution */
  739. get_be32(pb); /* vert resolution */
  740. get_be32(pb); /* data size, always 0 */
  741. frames_per_sample = get_be16(pb); /* frames per samples */
  742. #ifdef DEBUG
  743. av_log(NULL, AV_LOG_DEBUG, "frames/samples = %d\n", frames_per_sample);
  744. #endif
  745. get_buffer(pb, codec_name, 32); /* codec name, pascal string (FIXME: true for mp4?) */
  746. if (codec_name[0] <= 31) {
  747. memcpy(st->codec.codec_name, &codec_name[1],codec_name[0]);
  748. st->codec.codec_name[codec_name[0]] = 0;
  749. }
  750. st->codec.bits_per_sample = get_be16(pb); /* depth */
  751. st->codec.color_table_id = get_be16(pb); /* colortable id */
  752. /* These are set in mov_read_stts and might already be set!
  753. st->codec.frame_rate = 25;
  754. st->codec.frame_rate_base = 1;
  755. */
  756. size -= (16+8*4+2+32+2*2);
  757. #if 0
  758. while (size >= 8) {
  759. MOV_atom_t a;
  760. int64_t start_pos;
  761. a.size = get_be32(pb);
  762. a.type = get_le32(pb);
  763. size -= 8;
  764. #ifdef DEBUG
  765. av_log(NULL, AV_LOG_DEBUG, "VIDEO: atom_type=%c%c%c%c atom.size=%Ld size_left=%d\n",
  766. (a.type >> 0) & 0xff,
  767. (a.type >> 8) & 0xff,
  768. (a.type >> 16) & 0xff,
  769. (a.type >> 24) & 0xff,
  770. a.size, size);
  771. #endif
  772. start_pos = url_ftell(pb);
  773. switch(a.type) {
  774. case MKTAG('e', 's', 'd', 's'):
  775. {
  776. int tag, len;
  777. /* Well, broken but suffisant for some MP4 streams */
  778. get_be32(pb); /* version + flags */
  779. len = mov_mp4_read_descr(pb, &tag);
  780. if (tag == 0x03) {
  781. /* MP4ESDescrTag */
  782. get_be16(pb); /* ID */
  783. get_byte(pb); /* priority */
  784. len = mov_mp4_read_descr(pb, &tag);
  785. if (tag != 0x04)
  786. goto fail;
  787. /* MP4DecConfigDescrTag */
  788. get_byte(pb); /* objectTypeId */
  789. get_be32(pb); /* streamType + buffer size */
  790. get_be32(pb); /* max bit rate */
  791. get_be32(pb); /* avg bit rate */
  792. len = mov_mp4_read_descr(pb, &tag);
  793. if (tag != 0x05)
  794. goto fail;
  795. /* MP4DecSpecificDescrTag */
  796. #ifdef DEBUG
  797. av_log(NULL, AV_LOG_DEBUG, "Specific MPEG4 header len=%d\n", len);
  798. #endif
  799. sc->header_data = av_mallocz(len);
  800. if (sc->header_data) {
  801. get_buffer(pb, sc->header_data, len);
  802. sc->header_len = len;
  803. }
  804. }
  805. /* in any case, skip garbage */
  806. }
  807. break;
  808. default:
  809. break;
  810. }
  811. fail:
  812. av_log(NULL, AV_LOG_DEBUG, "ATOMENEWSIZE %Ld %d\n", atom.size, url_ftell(pb) - start_pos);
  813. if (atom.size > 8) {
  814. url_fskip(pb, (atom.size - 8) -
  815. ((url_ftell(pb) - start_pos)));
  816. size -= atom.size - 8;
  817. }
  818. }
  819. if (size > 0) {
  820. /* unknown extension */
  821. url_fskip(pb, size);
  822. }
  823. #else
  824. /* figure out the palette situation */
  825. color_depth = st->codec.bits_per_sample & 0x1F;
  826. color_greyscale = st->codec.bits_per_sample & 0x20;
  827. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  828. if ((color_depth == 2) || (color_depth == 4) ||
  829. (color_depth == 8)) {
  830. if (color_greyscale) {
  831. /* compute the greyscale palette */
  832. color_count = 1 << color_depth;
  833. color_index = 255;
  834. color_dec = 256 / (color_count - 1);
  835. for (j = 0; j < color_count; j++) {
  836. r = g = b = color_index;
  837. c->palette_control.palette[j] =
  838. (r << 16) | (g << 8) | (b);
  839. color_index -= color_dec;
  840. if (color_index < 0)
  841. color_index = 0;
  842. }
  843. } else if (st->codec.color_table_id & 0x08) {
  844. /* if flag bit 3 is set, use the default palette */
  845. color_count = 1 << color_depth;
  846. if (color_depth == 2)
  847. color_table = ff_qt_default_palette_4;
  848. else if (color_depth == 4)
  849. color_table = ff_qt_default_palette_16;
  850. else
  851. color_table = ff_qt_default_palette_256;
  852. for (j = 0; j < color_count; j++) {
  853. r = color_table[j * 4 + 0];
  854. g = color_table[j * 4 + 1];
  855. b = color_table[j * 4 + 2];
  856. c->palette_control.palette[j] =
  857. (r << 16) | (g << 8) | (b);
  858. }
  859. } else {
  860. /* load the palette from the file */
  861. color_start = get_be32(pb);
  862. color_count = get_be16(pb);
  863. color_end = get_be16(pb);
  864. for (j = color_start; j <= color_end; j++) {
  865. /* each R, G, or B component is 16 bits;
  866. * only use the top 8 bits; skip alpha bytes
  867. * up front */
  868. get_byte(pb);
  869. get_byte(pb);
  870. r = get_byte(pb);
  871. get_byte(pb);
  872. g = get_byte(pb);
  873. get_byte(pb);
  874. b = get_byte(pb);
  875. get_byte(pb);
  876. c->palette_control.palette[j] =
  877. (r << 16) | (g << 8) | (b);
  878. }
  879. }
  880. st->codec.palctrl = &c->palette_control;
  881. st->codec.palctrl->palette_changed = 1;
  882. } else
  883. st->codec.palctrl = NULL;
  884. a.size = size;
  885. mov_read_default(c, pb, a);
  886. #endif
  887. } else {
  888. st->codec.codec_id = codec_get_id(mov_audio_tags, format);
  889. if(st->codec.codec_id==CODEC_ID_AMR_NB || st->codec.codec_id==CODEC_ID_AMR_WB) //from TS26.244
  890. {
  891. #ifdef DEBUG
  892. av_log(NULL, AV_LOG_DEBUG, "AMR-NB or AMR-WB audio identified!!\n");
  893. #endif
  894. get_be32(pb);get_be32(pb); //Reserved_8
  895. get_be16(pb);//Reserved_2
  896. get_be16(pb);//Reserved_2
  897. get_be32(pb);//Reserved_4
  898. get_be16(pb);//TimeScale
  899. get_be16(pb);//Reserved_2
  900. //AMRSpecificBox.(10 bytes)
  901. get_be32(pb); //size
  902. get_be32(pb); //type=='damr'
  903. get_be32(pb); //vendor
  904. get_byte(pb); //decoder version
  905. get_be16(pb); //mode_set
  906. get_byte(pb); //mode_change_period
  907. get_byte(pb); //frames_per_sample
  908. st->duration = AV_NOPTS_VALUE;//Not possible to get from this info, must count number of AMR frames
  909. if(st->codec.codec_id==CODEC_ID_AMR_NB)
  910. {
  911. st->codec.sample_rate=8000;
  912. st->codec.channels=1;
  913. }
  914. else //AMR-WB
  915. {
  916. st->codec.sample_rate=16000;
  917. st->codec.channels=1;
  918. }
  919. st->codec.bits_per_sample=16;
  920. st->codec.bit_rate=0; /*It is not possible to tell this before we have
  921. an audio frame and even then every frame can be different*/
  922. }
  923. else if( st->codec.codec_tag == MKTAG( 'm', 'p', '4', 's' ))
  924. {
  925. //This is some stuff for the hint track, lets ignore it!
  926. //Do some mp4 auto detect.
  927. c->mp4=1;
  928. size-=(16);
  929. url_fskip(pb, size); /* The mp4s atom also contians a esds atom that we can skip*/
  930. }
  931. else if( st->codec.codec_tag == MKTAG( 'm', 'p', '4', 'a' ))
  932. {
  933. MOV_atom_t a;
  934. int mp4_version;
  935. /* Handle mp4 audio tag */
  936. mp4_version=get_be16(pb);/*version*/
  937. get_be16(pb); /*revesion*/
  938. get_be32(pb);
  939. st->codec.channels = get_be16(pb); /* channels */
  940. st->codec.bits_per_sample = get_be16(pb); /* bits per sample */
  941. get_be32(pb);
  942. st->codec.sample_rate = get_be16(pb); /* sample rate, not always correct */
  943. get_be16(pb);
  944. c->mp4=1;
  945. if(mp4_version==1)
  946. {
  947. url_fskip(pb,16);
  948. a.size=size-(16+20+16);
  949. }
  950. else
  951. a.size=size-(16+20);
  952. a.offset=url_ftell(pb);
  953. mov_read_default(c, pb, a);
  954. /* Get correct sample rate from extradata */
  955. if(st->codec.extradata_size) {
  956. const int samplerate_table[] = {
  957. 96000, 88200, 64000, 48000, 44100, 32000,
  958. 24000, 22050, 16000, 12000, 11025, 8000,
  959. 7350, 0, 0, 0
  960. };
  961. unsigned char *px = st->codec.extradata;
  962. // 5 bits objectTypeIndex, 4 bits sampleRateIndex, 4 bits channels
  963. int samplerate_index = ((px[0] & 7) << 1) + ((px[1] >> 7) & 1);
  964. st->codec.sample_rate = samplerate_table[samplerate_index];
  965. st->codec.channels = (px[1] >> 3) & 15;
  966. }
  967. }
  968. else if(size>=(16+20))
  969. {//16 bytes read, reading atleast 20 more
  970. uint16_t version;
  971. #ifdef DEBUG
  972. av_log(NULL, AV_LOG_DEBUG, "audio size=0x%X\n",size);
  973. #endif
  974. version = get_be16(pb); /* version */
  975. get_be16(pb); /* revision level */
  976. get_be32(pb); /* vendor */
  977. st->codec.channels = get_be16(pb); /* channel count */
  978. st->codec.bits_per_sample = get_be16(pb); /* sample size */
  979. /* handle specific s8 codec */
  980. get_be16(pb); /* compression id = 0*/
  981. get_be16(pb); /* packet size = 0 */
  982. st->codec.sample_rate = ((get_be32(pb) >> 16));
  983. //av_log(NULL, AV_LOG_DEBUG, "CODECID %d %d %.4s\n", st->codec.codec_id, CODEC_ID_PCM_S16BE, (char*)&format);
  984. switch (st->codec.codec_id) {
  985. case CODEC_ID_PCM_S16BE:
  986. if (st->codec.bits_per_sample == 8)
  987. st->codec.codec_id = CODEC_ID_PCM_S8;
  988. /* fall */
  989. case CODEC_ID_PCM_U8:
  990. st->codec.bit_rate = st->codec.sample_rate * 8;
  991. break;
  992. default:
  993. ;
  994. }
  995. //Read QT version 1 fields. In version 0 theese dont exist
  996. #ifdef DEBUG
  997. av_log(NULL, AV_LOG_DEBUG, "version =%d mp4=%d\n",version,c->mp4);
  998. av_log(NULL, AV_LOG_DEBUG, "size-(16+20+16)=%d\n",size-(16+20+16));
  999. #endif
  1000. if((version==1) && size>=(16+20+16))
  1001. {
  1002. get_be32(pb); /* samples per packet */
  1003. get_be32(pb); /* bytes per packet */
  1004. get_be32(pb); /* bytes per frame */
  1005. get_be32(pb); /* bytes per sample */
  1006. if(size>(16+20+16))
  1007. {
  1008. //Optional, additional atom-based fields
  1009. MOV_atom_t a = { format, url_ftell(pb), size - (16 + 20 + 16 + 8) };
  1010. #ifdef DEBUG
  1011. 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 ),
  1012. (format >> 0) & 0xff,
  1013. (format >> 8) & 0xff,
  1014. (format >> 16) & 0xff,
  1015. (format >> 24) & 0xff);
  1016. #endif
  1017. mov_read_default(c, pb, a);
  1018. }
  1019. }
  1020. else
  1021. {
  1022. //We should be down to 0 bytes here, but lets make sure.
  1023. size-=(16+20);
  1024. #ifdef DEBUG
  1025. if(size>0)
  1026. av_log(NULL, AV_LOG_DEBUG, "skipping 0x%X bytes\n",size-(16+20));
  1027. #endif
  1028. url_fskip(pb, size);
  1029. }
  1030. }
  1031. else
  1032. {
  1033. size-=16;
  1034. //Unknown size, but lets do our best and skip the rest.
  1035. #ifdef DEBUG
  1036. av_log(NULL, AV_LOG_DEBUG, "Strange size, skipping 0x%X bytes\n",size);
  1037. #endif
  1038. url_fskip(pb, size);
  1039. }
  1040. }
  1041. }
  1042. return 0;
  1043. }
  1044. static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1045. {
  1046. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1047. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1048. unsigned int i, entries;
  1049. print_atom("stsc", atom);
  1050. get_byte(pb); /* version */
  1051. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1052. entries = get_be32(pb);
  1053. if(entries >= UINT_MAX / sizeof(MOV_sample_to_chunk_tbl))
  1054. return -1;
  1055. #ifdef DEBUG
  1056. av_log(NULL, AV_LOG_DEBUG, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  1057. #endif
  1058. sc->sample_to_chunk_sz = entries;
  1059. sc->sample_to_chunk = (MOV_sample_to_chunk_tbl*) av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
  1060. if (!sc->sample_to_chunk)
  1061. return -1;
  1062. for(i=0; i<entries; i++) {
  1063. sc->sample_to_chunk[i].first = get_be32(pb);
  1064. sc->sample_to_chunk[i].count = get_be32(pb);
  1065. sc->sample_to_chunk[i].id = get_be32(pb);
  1066. #ifdef DEBUG
  1067. /* 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); */
  1068. #endif
  1069. }
  1070. return 0;
  1071. }
  1072. static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1073. {
  1074. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1075. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1076. unsigned int i, entries;
  1077. print_atom("stss", atom);
  1078. get_byte(pb); /* version */
  1079. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1080. entries = get_be32(pb);
  1081. if(entries >= UINT_MAX / sizeof(long))
  1082. return -1;
  1083. sc->keyframe_count = entries;
  1084. #ifdef DEBUG
  1085. av_log(NULL, AV_LOG_DEBUG, "keyframe_count = %ld\n", sc->keyframe_count);
  1086. #endif
  1087. sc->keyframes = (long*) av_malloc(entries * sizeof(long));
  1088. if (!sc->keyframes)
  1089. return -1;
  1090. for(i=0; i<entries; i++) {
  1091. sc->keyframes[i] = get_be32(pb);
  1092. #ifdef DEBUG
  1093. /* av_log(NULL, AV_LOG_DEBUG, "keyframes[]=%ld\n", sc->keyframes[i]); */
  1094. #endif
  1095. }
  1096. return 0;
  1097. }
  1098. static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1099. {
  1100. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1101. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1102. unsigned int i, entries;
  1103. print_atom("stsz", atom);
  1104. get_byte(pb); /* version */
  1105. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1106. sc->sample_size = get_be32(pb);
  1107. entries = get_be32(pb);
  1108. if(entries >= UINT_MAX / sizeof(long))
  1109. return -1;
  1110. sc->sample_count = entries;
  1111. #ifdef DEBUG
  1112. av_log(NULL, AV_LOG_DEBUG, "sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
  1113. #endif
  1114. if(sc->sample_size)
  1115. return 0; /* there isn't any table following */
  1116. sc->sample_sizes = (long*) av_malloc(entries * sizeof(long));
  1117. if (!sc->sample_sizes)
  1118. return -1;
  1119. for(i=0; i<entries; i++) {
  1120. sc->sample_sizes[i] = get_be32(pb);
  1121. #ifdef DEBUG
  1122. av_log(NULL, AV_LOG_DEBUG, "sample_sizes[]=%ld\n", sc->sample_sizes[i]);
  1123. #endif
  1124. }
  1125. return 0;
  1126. }
  1127. static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1128. {
  1129. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1130. //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1131. unsigned int i, entries;
  1132. int64_t duration=0;
  1133. int64_t total_sample_count=0;
  1134. print_atom("stts", atom);
  1135. get_byte(pb); /* version */
  1136. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1137. entries = get_be32(pb);
  1138. if(entries >= UINT_MAX / sizeof(Time2Sample))
  1139. return -1;
  1140. c->streams[c->fc->nb_streams-1]->stts_count = entries;
  1141. c->streams[c->fc->nb_streams-1]->stts_data = av_malloc(entries * sizeof(Time2Sample));
  1142. #ifdef DEBUG
  1143. av_log(NULL, AV_LOG_DEBUG, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  1144. #endif
  1145. for(i=0; i<entries; i++) {
  1146. int sample_duration;
  1147. int sample_count;
  1148. sample_count=get_be32(pb);
  1149. sample_duration = get_be32(pb);
  1150. c->streams[c->fc->nb_streams - 1]->stts_data[i].count= sample_count;
  1151. c->streams[c->fc->nb_streams - 1]->stts_data[i].duration= sample_duration;
  1152. #ifdef DEBUG
  1153. av_log(NULL, AV_LOG_DEBUG, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  1154. #endif
  1155. duration+=sample_duration*sample_count;
  1156. total_sample_count+=sample_count;
  1157. #if 0 //We calculate an average instead, needed by .mp4-files created with nec e606 3g phone
  1158. if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
  1159. st->codec.frame_rate_base = sample_duration ? sample_duration : 1;
  1160. st->codec.frame_rate = c->streams[c->fc->nb_streams-1]->time_scale;
  1161. #ifdef DEBUG
  1162. av_log(NULL, AV_LOG_DEBUG, "VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
  1163. #endif
  1164. }
  1165. #endif
  1166. }
  1167. /*The stsd atom which contain codec type sometimes comes after the stts so we cannot check for codec_type*/
  1168. if(duration>0)
  1169. {
  1170. av_reduce(
  1171. &st->codec.frame_rate,
  1172. &st->codec.frame_rate_base,
  1173. c->streams[c->fc->nb_streams-1]->time_scale * total_sample_count,
  1174. duration,
  1175. INT_MAX
  1176. );
  1177. #ifdef DEBUG
  1178. av_log(NULL, AV_LOG_DEBUG, "FRAME RATE average (video or audio)= %f (tot sample count= %i ,tot dur= %i timescale=%d)\n", (float)st->codec.frame_rate/st->codec.frame_rate_base,total_sample_count,duration,c->streams[c->fc->nb_streams-1]->time_scale);
  1179. #endif
  1180. }
  1181. else
  1182. {
  1183. st->codec.frame_rate_base = 1;
  1184. st->codec.frame_rate = c->streams[c->fc->nb_streams-1]->time_scale;
  1185. }
  1186. return 0;
  1187. }
  1188. static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1189. {
  1190. // AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1191. //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1192. unsigned int i, entries;
  1193. print_atom("ctts", atom);
  1194. get_byte(pb); /* version */
  1195. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1196. entries = get_be32(pb);
  1197. if(entries >= UINT_MAX / sizeof(Time2Sample))
  1198. return -1;
  1199. c->streams[c->fc->nb_streams-1]->ctts_count = entries;
  1200. c->streams[c->fc->nb_streams-1]->ctts_data = av_malloc(entries * sizeof(Time2Sample));
  1201. // #ifdef DEBUG
  1202. av_log(NULL, AV_LOG_DEBUG, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1203. // #endif
  1204. for(i=0; i<entries; i++) {
  1205. c->streams[c->fc->nb_streams - 1]->ctts_data[i].count= get_be32(pb);
  1206. c->streams[c->fc->nb_streams - 1]->ctts_data[i].duration= get_be32(pb);
  1207. }
  1208. return 0;
  1209. }
  1210. static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1211. {
  1212. AVStream *st;
  1213. MOVStreamContext *sc;
  1214. print_atom("trak", atom);
  1215. st = av_new_stream(c->fc, c->fc->nb_streams);
  1216. if (!st) return -2;
  1217. sc = (MOVStreamContext*) av_mallocz(sizeof(MOVStreamContext));
  1218. if (!sc) {
  1219. av_free(st);
  1220. return -1;
  1221. }
  1222. sc->sample_to_chunk_index = -1;
  1223. st->priv_data = sc;
  1224. st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
  1225. st->start_time = 0; /* XXX: check */
  1226. st->duration = (c->duration * (int64_t)AV_TIME_BASE) / c->time_scale;
  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. st->duration = (get_be32(pb) * (int64_t)AV_TIME_BASE) / c->time_scale; /* duration */
  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_default },
  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->header_data);
  1446. av_freep(&sc->stts_data);
  1447. av_freep(&sc->ctts_data);
  1448. av_freep(&sc);
  1449. }
  1450. }
  1451. static inline uint32_t mov_to_tag(uint8_t *buf)
  1452. {
  1453. return MKTAG(buf[0], buf[1], buf[2], buf[3]);
  1454. }
  1455. static inline uint32_t to_be32(uint8_t *buf)
  1456. {
  1457. return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  1458. }
  1459. /* XXX: is it sufficient ? */
  1460. static int mov_probe(AVProbeData *p)
  1461. {
  1462. unsigned int offset;
  1463. uint32_t tag;
  1464. int score = 0;
  1465. /* check file header */
  1466. if (p->buf_size <= 12)
  1467. return 0;
  1468. offset = 0;
  1469. for(;;) {
  1470. /* ignore invalid offset */
  1471. if ((offset + 8) > (unsigned int)p->buf_size)
  1472. return score;
  1473. tag = mov_to_tag(p->buf + offset + 4);
  1474. switch(tag) {
  1475. /* check for obvious tags */
  1476. case MKTAG( 'm', 'o', 'o', 'v' ):
  1477. case MKTAG( 'm', 'd', 'a', 't' ):
  1478. case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
  1479. case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
  1480. return AVPROBE_SCORE_MAX;
  1481. /* those are more common words, so rate then a bit less */
  1482. case MKTAG( 'w', 'i', 'd', 'e' ):
  1483. case MKTAG( 'f', 'r', 'e', 'e' ):
  1484. case MKTAG( 'j', 'u', 'n', 'k' ):
  1485. case MKTAG( 'p', 'i', 'c', 't' ):
  1486. return AVPROBE_SCORE_MAX - 5;
  1487. case MKTAG( 'f', 't', 'y', 'p' ):
  1488. case MKTAG( 's', 'k', 'i', 'p' ):
  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_filesize(url_fileno(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 = 0x0FFFFFFFFFFFFFFFLL;
  1570. int i, a, b, m;
  1571. int size;
  1572. int idx;
  1573. size = 0x0FFFFFFF;
  1574. #ifdef MOV_SPLIT_CHUNKS
  1575. if (mov->partial) {
  1576. sc = mov->partial;
  1577. idx = sc->sample_to_chunk_index;
  1578. if (idx < 0) return 0;
  1579. #ifdef DEBUG
  1580. fprintf(stderr, "sc[ffid %d]->sample_size = %d\n", sc->ffindex, sc->sample_size);
  1581. #endif
  1582. // sample_size is not always correct for audio. Quicktime ignores this value and
  1583. // computes it differently.
  1584. if(s->streams[sc->ffindex]->codec.codec_type == CODEC_TYPE_VIDEO)
  1585. size = sc->sample_size?sc->sample_size:sc->sample_sizes[sc->current_sample];
  1586. sc->current_sample++;
  1587. sc->left_in_chunk--;
  1588. if (sc->left_in_chunk <= 0)
  1589. mov->partial = 0;
  1590. offset = mov->next_chunk_offset;
  1591. /* extract the sample */
  1592. goto readchunk;
  1593. }
  1594. #endif
  1595. again:
  1596. sc = 0;
  1597. for(i=0; i<mov->total_streams; i++) {
  1598. MOVStreamContext *msc = mov->streams[i];
  1599. //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));
  1600. if ((msc->next_chunk < msc->chunk_count) && msc->next_chunk >= 0
  1601. && (msc->chunk_offsets[msc->next_chunk] < offset)) {
  1602. sc = msc;
  1603. offset = msc->chunk_offsets[msc->next_chunk];
  1604. //av_log(NULL, AV_LOG_DEBUG, "SELETED %Ld i:%d\n", offset, i);
  1605. }
  1606. }
  1607. if (!sc || offset==0x0FFFFFFFFFFFFFFFLL)
  1608. return -1;
  1609. sc->next_chunk++;
  1610. if(mov->next_chunk_offset < offset) { /* some meta data */
  1611. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1612. mov->next_chunk_offset = offset;
  1613. }
  1614. //av_log(NULL, AV_LOG_DEBUG, "chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
  1615. if(!sc->is_ff_stream || s->streams[sc->ffindex]->discard) {
  1616. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1617. mov->next_chunk_offset = offset;
  1618. offset = 0x0FFFFFFFFFFFFFFFLL;
  1619. goto again;
  1620. }
  1621. /* now get the chunk size... */
  1622. for(i=0; i<mov->total_streams; i++) {
  1623. MOVStreamContext *msc = mov->streams[i];
  1624. if ((msc->next_chunk < msc->chunk_count)
  1625. && ((msc->chunk_offsets[msc->next_chunk] - offset) < size))
  1626. size = msc->chunk_offsets[msc->next_chunk] - offset;
  1627. }
  1628. #ifdef MOV_MINOLTA_FIX
  1629. //Make sure that size is according to sample_size (Needed by .mov files
  1630. //created on a Minolta Dimage Xi where audio chunks contains waste data in the end)
  1631. //Maybe we should really not only check sc->sample_size, but also sc->sample_sizes
  1632. //but I have no such movies
  1633. if (sc->sample_size > 0) {
  1634. int foundsize=0;
  1635. for(i=0; i<(sc->sample_to_chunk_sz); i++) {
  1636. if( (sc->sample_to_chunk[i].first)<=(sc->next_chunk) )
  1637. {
  1638. // I can't figure out why for PCM audio sample_size is always 1
  1639. // (it should actually be channels*bits_per_second/8) but it is.
  1640. AVCodecContext* cod = &s->streams[sc->ffindex]->codec;
  1641. if (sc->sample_size == 1 && (cod->codec_id == CODEC_ID_PCM_S16BE || cod->codec_id == CODEC_ID_PCM_S16LE))
  1642. foundsize=(sc->sample_to_chunk[i].count*cod->channels*cod->bits_per_sample)/8;
  1643. else
  1644. foundsize=sc->sample_to_chunk[i].count*sc->sample_size;
  1645. }
  1646. #ifdef DEBUG
  1647. 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);
  1648. #endif
  1649. }
  1650. if( (foundsize>0) && (foundsize<size) )
  1651. {
  1652. #ifdef DEBUG
  1653. /*av_log(NULL, AV_LOG_DEBUG, "this size should actually be %d\n",foundsize);*/
  1654. #endif
  1655. size=foundsize;
  1656. }
  1657. }
  1658. #endif //MOV_MINOLTA_FIX
  1659. #ifdef MOV_SPLIT_CHUNKS
  1660. /* split chunks into samples */
  1661. if (s->streams[sc->ffindex]->codec.codec_type == CODEC_TYPE_VIDEO) {
  1662. // This does not support split audio, as the sample_size is often not correct
  1663. idx = sc->sample_to_chunk_index;
  1664. if ((idx + 1 < sc->sample_to_chunk_sz)
  1665. && (sc->next_chunk >= sc->sample_to_chunk[idx + 1].first))
  1666. idx++;
  1667. sc->sample_to_chunk_index = idx;
  1668. if (idx >= 0 && sc->sample_to_chunk[idx].count != 1) {
  1669. mov->partial = sc;
  1670. /* we'll have to get those samples before next chunk */
  1671. sc->left_in_chunk = sc->sample_to_chunk[idx].count - 1;
  1672. size = sc->sample_size?sc->sample_size:sc->sample_sizes[sc->current_sample];
  1673. }
  1674. sc->current_sample++;
  1675. }
  1676. #endif
  1677. readchunk:
  1678. #ifdef DEBUG
  1679. av_log(NULL, AV_LOG_DEBUG, "chunk: %lli -> %lli (%i)\n", offset, offset + size, size);
  1680. #endif
  1681. if(size == 0x0FFFFFFF)
  1682. size = mov->mdat_size + mov->mdat_offset - offset;
  1683. if(size < 0)
  1684. return -1;
  1685. if(size == 0)
  1686. return -1;
  1687. url_fseek(&s->pb, offset, SEEK_SET);
  1688. //av_log(NULL, AV_LOG_DEBUG, "READCHUNK hlen: %d %d off: %Ld pos:%Ld\n", size, sc->header_len, offset, url_ftell(&s->pb));
  1689. if (sc->header_len > 0) {
  1690. av_new_packet(pkt, size + sc->header_len);
  1691. memcpy(pkt->data, sc->header_data, sc->header_len);
  1692. get_buffer(&s->pb, pkt->data + sc->header_len, size);
  1693. /* free header */
  1694. av_freep(&sc->header_data);
  1695. sc->header_len = 0;
  1696. } else {
  1697. av_new_packet(pkt, size);
  1698. get_buffer(&s->pb, pkt->data, pkt->size);
  1699. }
  1700. pkt->stream_index = sc->ffindex;
  1701. // If the keyframes table exists, mark any samples that are in the table as key frames.
  1702. // If no table exists, treat very sample as a key frame.
  1703. if (sc->keyframes) {
  1704. a = 0;
  1705. b = sc->keyframe_count - 1;
  1706. while (a < b) {
  1707. m = (a + b + 1) >> 1;
  1708. if (sc->keyframes[m] > sc->current_sample) {
  1709. b = m - 1;
  1710. } else {
  1711. a = m;
  1712. }
  1713. }
  1714. if (sc->keyframes[a] == sc->current_sample)
  1715. pkt->flags |= PKT_FLAG_KEY;
  1716. }
  1717. else
  1718. pkt->flags |= PKT_FLAG_KEY;
  1719. #ifdef DEBUG
  1720. /*
  1721. av_log(NULL, AV_LOG_DEBUG, "Packet (%d, %ld) ", pkt->stream_index, pkt->size);
  1722. for(i=0; i<8; i++)
  1723. av_log(NULL, AV_LOG_DEBUG, "%02x ", pkt->data[i]);
  1724. for(i=0; i<8; i++)
  1725. av_log(NULL, AV_LOG_DEBUG, "%c ", (pkt->data[i]) & 0x7F);
  1726. av_log(NULL, AV_LOG_DEBUG, "\n");
  1727. */
  1728. #endif
  1729. mov->next_chunk_offset = offset + size;
  1730. /* find the corresponding dts */
  1731. if (sc && sc->sample_to_time_index < sc->stts_count && pkt) {
  1732. unsigned int count;
  1733. uint64_t dts, pts;
  1734. unsigned int duration = sc->stts_data[sc->sample_to_time_index].duration;
  1735. count = sc->stts_data[sc->sample_to_time_index].count;
  1736. if ((sc->sample_to_time_sample + count) < sc->current_sample) {
  1737. sc->sample_to_time_sample += count;
  1738. sc->sample_to_time_time += count*duration;
  1739. sc->sample_to_time_index ++;
  1740. duration = sc->stts_data[sc->sample_to_time_index].duration;
  1741. }
  1742. dts = sc->sample_to_time_time + (sc->current_sample-1 - sc->sample_to_time_sample) * (int64_t)duration;
  1743. /* find the corresponding pts */
  1744. if (sc->sample_to_ctime_index < sc->ctts_count) {
  1745. int duration = sc->ctts_data[sc->sample_to_ctime_index].duration;
  1746. int count = sc->ctts_data[sc->sample_to_ctime_index].count;
  1747. if ((sc->sample_to_ctime_sample + count) < sc->current_sample) {
  1748. sc->sample_to_ctime_sample += count;
  1749. sc->sample_to_ctime_index ++;
  1750. duration = sc->ctts_data[sc->sample_to_ctime_index].duration;
  1751. }
  1752. pts = dts + duration;
  1753. }else
  1754. pts = dts;
  1755. pkt->pts = av_rescale( pts,
  1756. (int64_t)s->streams[sc->ffindex]->time_base.den,
  1757. (int64_t)sc->time_scale * (int64_t)s->streams[sc->ffindex]->time_base.num );
  1758. pkt->dts = av_rescale( dts,
  1759. (int64_t)s->streams[sc->ffindex]->time_base.den,
  1760. (int64_t)sc->time_scale * (int64_t)s->streams[sc->ffindex]->time_base.num );
  1761. #ifdef DEBUG
  1762. 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"
  1763. , pkt->stream_index, sc->current_sample-1, pkt->dts, pkt->pts
  1764. , sc->sample_to_time_sample
  1765. , sc->sample_to_time_time
  1766. , sc->sample_to_time_index
  1767. , sc->stts_count
  1768. , count
  1769. , duration);
  1770. #endif
  1771. }
  1772. return 0;
  1773. }
  1774. #if defined(MOV_SPLIT_CHUNKS) && defined(MOV_SEEK)
  1775. /**
  1776. * Seek method based on the one described in the Appendix C of QTFileFormat.pdf
  1777. */
  1778. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  1779. {
  1780. MOVContext* mov = (MOVContext *) s->priv_data;
  1781. MOVStreamContext* sc;
  1782. int32_t i, a, b, m;
  1783. int64_t sample_time;
  1784. int64_t start_time;
  1785. int32_t seek_sample, sample;
  1786. int32_t duration;
  1787. int32_t count;
  1788. int32_t chunk;
  1789. int32_t left_in_chunk;
  1790. int64_t chunk_file_offset;
  1791. int64_t sample_file_offset;
  1792. int32_t first_chunk_sample;
  1793. int32_t sample_to_chunk_idx;
  1794. int sample_to_time_index;
  1795. long sample_to_time_sample = 0;
  1796. uint64_t sample_to_time_time = 0;
  1797. int mov_idx;
  1798. // Find the corresponding mov stream
  1799. for (mov_idx = 0; mov_idx < mov->total_streams; mov_idx++)
  1800. if (mov->streams[mov_idx]->ffindex == stream_index)
  1801. break;
  1802. if (mov_idx == mov->total_streams) {
  1803. av_log(s, AV_LOG_ERROR, "mov: requested stream was not found in mov streams (idx=%i)\n", stream_index);
  1804. return -1;
  1805. }
  1806. sc = mov->streams[mov_idx];
  1807. // Step 1. Find the edit that contains the requested time (elst)
  1808. if (sc->edit_count) {
  1809. // FIXME should handle edit list
  1810. av_log(s, AV_LOG_ERROR, "mov: does not handle seeking in files that contain edit list (c:%d)\n", sc->edit_count);
  1811. return -1;
  1812. }
  1813. // Step 2. Find the corresponding sample using the Time-to-sample atom (stts) */
  1814. #ifdef DEBUG
  1815. av_log(s, AV_LOG_DEBUG, "Searching for time %li in stream #%i (time_scale=%i)\n", (long)timestamp, mov_idx, sc->time_scale);
  1816. #endif
  1817. // convert timestamp from time_base unit to timescale unit
  1818. sample_time = av_rescale( timestamp,
  1819. (int64_t)sc->time_scale * s->streams[stream_index]->time_base.num,
  1820. (int64_t)s->streams[stream_index]->time_base.den);
  1821. start_time = 0; // FIXME use elst atom
  1822. sample = 1; // sample are 0 based in table
  1823. #ifdef DEBUG
  1824. av_log(s, AV_LOG_DEBUG, "Searching for sample_time %li \n", (long)sample_time);
  1825. #endif
  1826. for (i = 0; i < sc->stts_count; i++) {
  1827. count = sc->stts_data[i].count;
  1828. duration = sc->stts_data[i].duration;
  1829. //av_log(s, AV_LOG_DEBUG, "> sample_time %lli \n", (long)sample_time);
  1830. //av_log(s, AV_LOG_DEBUG, "> count=%i duration=%i\n", count, duration);
  1831. if ((start_time + count*duration) > sample_time) {
  1832. sample_to_time_time = start_time;
  1833. sample_to_time_index = i;
  1834. sample_to_time_sample = sample;
  1835. sample += (sample_time - start_time) / duration;
  1836. break;
  1837. }
  1838. sample += count;
  1839. start_time += count * duration;
  1840. }
  1841. sample_to_time_time = start_time;
  1842. sample_to_time_index = i;
  1843. /* NOTE: despite what qt doc say, the dt value (Display Time in qt vocabulary) computed with the stts atom
  1844. is a decoding time stamp (dts) not a presentation time stamp. And as usual dts != pts for stream with b frames */
  1845. #ifdef DEBUG
  1846. av_log(s, AV_LOG_DEBUG, "Found time %li at sample #%u\n", (long)sample_time, sample);
  1847. #endif
  1848. if (sample > sc->sample_count) {
  1849. 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);
  1850. return -1;
  1851. }
  1852. // Step 3. Find the prior sync. sample using the Sync sample atom (stss)
  1853. if (sc->keyframes) {
  1854. a = 0;
  1855. b = sc->keyframe_count - 1;
  1856. while (a < b) {
  1857. m = (a + b + 1) >> 1;
  1858. if (sc->keyframes[m] > sample) {
  1859. b = m - 1;
  1860. } else {
  1861. a = m;
  1862. }
  1863. #ifdef DEBUG
  1864. // 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);
  1865. #endif
  1866. }
  1867. // for low latency prob: always use the previous keyframe, just uncomment the next line
  1868. // if (a) a--;
  1869. seek_sample = sc->keyframes[a];
  1870. }
  1871. else
  1872. seek_sample = sample; // else all samples are key frames
  1873. #ifdef DEBUG
  1874. av_log(s, AV_LOG_DEBUG, "Found nearest keyframe at sample #%i \n", seek_sample);
  1875. #endif
  1876. // Step 4. Find the chunk of the sample using the Sample-to-chunk-atom (stsc)
  1877. for (first_chunk_sample = 1, i = 0; i < (sc->sample_to_chunk_sz - 1); i++) {
  1878. b = (sc->sample_to_chunk[i + 1].first - sc->sample_to_chunk[i].first) * sc->sample_to_chunk[i].count;
  1879. if (seek_sample >= first_chunk_sample && seek_sample < (first_chunk_sample + b))
  1880. break;
  1881. first_chunk_sample += b;
  1882. }
  1883. chunk = sc->sample_to_chunk[i].first + (seek_sample - first_chunk_sample) / sc->sample_to_chunk[i].count;
  1884. left_in_chunk = sc->sample_to_chunk[i].count - (seek_sample - first_chunk_sample) % sc->sample_to_chunk[i].count;
  1885. first_chunk_sample += ((seek_sample - first_chunk_sample) / sc->sample_to_chunk[i].count) * sc->sample_to_chunk[i].count;
  1886. sample_to_chunk_idx = i;
  1887. #ifdef DEBUG
  1888. 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);
  1889. #endif
  1890. // Step 5. Find the offset of the chunk using the chunk offset atom
  1891. if (!sc->chunk_offsets) {
  1892. av_log(s, AV_LOG_ERROR, "mov: no chunk offset atom, unable to seek\n");
  1893. return -1;
  1894. }
  1895. if (chunk > sc->chunk_count) {
  1896. 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);
  1897. return -1;
  1898. }
  1899. chunk_file_offset = sc->chunk_offsets[chunk - 1];
  1900. #ifdef DEBUG
  1901. av_log(s, AV_LOG_DEBUG, "Chunk file offset is #%llu \n", chunk_file_offset);
  1902. #endif
  1903. // Step 6. Find the byte offset within the chunk using the sample size atom
  1904. sample_file_offset = chunk_file_offset;
  1905. if (sc->sample_size)
  1906. sample_file_offset += (seek_sample - first_chunk_sample) * sc->sample_size;
  1907. else {
  1908. for (i = 0; i < (seek_sample - first_chunk_sample); i++) {
  1909. sample_file_offset += sc->sample_sizes[first_chunk_sample + i - 1];
  1910. }
  1911. }
  1912. #ifdef DEBUG
  1913. av_log(s, AV_LOG_DEBUG, "Sample file offset is #%llu \n", sample_file_offset);
  1914. #endif
  1915. // Step 6. Update the parser
  1916. mov->partial = sc;
  1917. mov->next_chunk_offset = sample_file_offset;
  1918. // Update current stream state
  1919. sc->current_sample = seek_sample - 1; // zero based
  1920. sc->left_in_chunk = left_in_chunk;
  1921. sc->next_chunk = chunk; // +1 -1 (zero based)
  1922. sc->sample_to_chunk_index = sample_to_chunk_idx;
  1923. // Update other streams
  1924. for (i = 0; i<mov->total_streams; i++) {
  1925. MOVStreamContext *msc;
  1926. if (i == mov_idx) continue;
  1927. // Find the nearest 'next' chunk
  1928. msc = mov->streams[i];
  1929. a = 0;
  1930. b = msc->chunk_count - 1;
  1931. while (a < b) {
  1932. m = (a + b + 1) >> 1;
  1933. if (msc->chunk_offsets[m] > chunk_file_offset) {
  1934. b = m - 1;
  1935. } else {
  1936. a = m;
  1937. }
  1938. #ifdef DEBUG
  1939. /* av_log(s, AV_LOG_DEBUG, "a=%i (%li) b=%i (%li) m=%i (%li) stream #%i\n"
  1940. , a, (long)msc->chunk_offsets[a], b, (long)msc->chunk_offsets[b], m, (long)msc->chunk_offsets[m], i); */
  1941. #endif
  1942. }
  1943. msc->next_chunk = a;
  1944. if (msc->chunk_offsets[a] < chunk_file_offset && a < (msc->chunk_count-1))
  1945. msc->next_chunk ++;
  1946. #ifdef DEBUG
  1947. 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]);
  1948. #endif
  1949. // Compute sample count and index in the sample_to_chunk table (what a pity)
  1950. msc->sample_to_chunk_index = 0;
  1951. msc->current_sample = 0;
  1952. for(; msc->sample_to_chunk_index < (msc->sample_to_chunk_sz - 1)
  1953. && msc->sample_to_chunk[msc->sample_to_chunk_index + 1].first <= (1 + msc->next_chunk); msc->sample_to_chunk_index++) {
  1954. msc->current_sample += (msc->sample_to_chunk[msc->sample_to_chunk_index + 1].first - msc->sample_to_chunk[msc->sample_to_chunk_index].first) \
  1955. * msc->sample_to_chunk[msc->sample_to_chunk_index].count;
  1956. }
  1957. 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;
  1958. msc->left_in_chunk = msc->sample_to_chunk[msc->sample_to_chunk_index].count - 1;
  1959. // Find corresponding position in stts (used later to compute dts)
  1960. sample = 0;
  1961. start_time = 0;
  1962. for (msc->sample_to_time_index = 0; msc->sample_to_time_index < msc->stts_count; msc->sample_to_time_index++) {
  1963. count = msc->stts_data[msc->sample_to_time_index].count;
  1964. duration = msc->stts_data[msc->sample_to_time_index].duration;
  1965. if ((sample + count - 1) > msc->current_sample) {
  1966. msc->sample_to_time_time = start_time;
  1967. msc->sample_to_time_sample = sample;
  1968. break;
  1969. }
  1970. sample += count;
  1971. start_time += count * duration;
  1972. }
  1973. sample = 0;
  1974. for (msc->sample_to_ctime_index = 0; msc->sample_to_ctime_index < msc->ctts_count; msc->sample_to_ctime_index++) {
  1975. count = msc->ctts_data[msc->sample_to_ctime_index].count;
  1976. duration = msc->ctts_data[msc->sample_to_ctime_index].duration;
  1977. if ((sample + count - 1) > msc->current_sample) {
  1978. msc->sample_to_ctime_sample = sample;
  1979. break;
  1980. }
  1981. sample += count;
  1982. }
  1983. #ifdef DEBUG
  1984. 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);
  1985. #endif
  1986. }
  1987. return 0;
  1988. }
  1989. #endif
  1990. static int mov_read_close(AVFormatContext *s)
  1991. {
  1992. int i;
  1993. MOVContext *mov = (MOVContext *) s->priv_data;
  1994. for(i=0; i<mov->total_streams; i++)
  1995. mov_free_stream_context(mov->streams[i]);
  1996. /* free color tabs */
  1997. for(i=0; i<mov->ctab_size; i++)
  1998. av_freep(&mov->ctab[i]);
  1999. av_freep(&mov->ctab);
  2000. return 0;
  2001. }
  2002. static AVInputFormat mov_iformat = {
  2003. "mov,mp4,m4a,3gp,3g2",
  2004. "QuickTime/MPEG4 format",
  2005. sizeof(MOVContext),
  2006. mov_probe,
  2007. mov_read_header,
  2008. mov_read_packet,
  2009. mov_read_close,
  2010. #if defined(MOV_SPLIT_CHUNKS) && defined(MOV_SEEK)
  2011. mov_read_seek,
  2012. #endif
  2013. };
  2014. int mov_init(void)
  2015. {
  2016. av_register_input_format(&mov_iformat);
  2017. return 0;
  2018. }