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.

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