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.

2292 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. st->codec.width = get_be16(pb); /* width */
  731. st->codec.height = get_be16(pb); /* height */
  732. #if 1
  733. if (st->codec.codec_id == CODEC_ID_MPEG4) {
  734. /* in some MPEG4 the width/height are not correct, so
  735. we ignore this info */
  736. st->codec.width = 0;
  737. st->codec.height = 0;
  738. }
  739. #endif
  740. get_be32(pb); /* horiz resolution */
  741. get_be32(pb); /* vert resolution */
  742. get_be32(pb); /* data size, always 0 */
  743. frames_per_sample = get_be16(pb); /* frames per samples */
  744. #ifdef DEBUG
  745. av_log(NULL, AV_LOG_DEBUG, "frames/samples = %d\n", frames_per_sample);
  746. #endif
  747. get_buffer(pb, codec_name, 32); /* codec name, pascal string (FIXME: true for mp4?) */
  748. if (codec_name[0] <= 31) {
  749. memcpy(st->codec.codec_name, &codec_name[1],codec_name[0]);
  750. st->codec.codec_name[codec_name[0]] = 0;
  751. }
  752. st->codec.bits_per_sample = get_be16(pb); /* depth */
  753. st->codec.color_table_id = get_be16(pb); /* colortable id */
  754. /* These are set in mov_read_stts and might already be set!
  755. st->codec.frame_rate = 25;
  756. st->codec.frame_rate_base = 1;
  757. */
  758. size -= (16+8*4+2+32+2*2);
  759. #if 0
  760. while (size >= 8) {
  761. MOV_atom_t a;
  762. int64_t start_pos;
  763. a.size = get_be32(pb);
  764. a.type = get_le32(pb);
  765. size -= 8;
  766. #ifdef DEBUG
  767. av_log(NULL, AV_LOG_DEBUG, "VIDEO: atom_type=%c%c%c%c atom.size=%Ld size_left=%d\n",
  768. (a.type >> 0) & 0xff,
  769. (a.type >> 8) & 0xff,
  770. (a.type >> 16) & 0xff,
  771. (a.type >> 24) & 0xff,
  772. a.size, size);
  773. #endif
  774. start_pos = url_ftell(pb);
  775. switch(a.type) {
  776. case MKTAG('e', 's', 'd', 's'):
  777. {
  778. int tag, len;
  779. /* Well, broken but suffisant for some MP4 streams */
  780. get_be32(pb); /* version + flags */
  781. len = mov_mp4_read_descr(pb, &tag);
  782. if (tag == 0x03) {
  783. /* MP4ESDescrTag */
  784. get_be16(pb); /* ID */
  785. get_byte(pb); /* priority */
  786. len = mov_mp4_read_descr(pb, &tag);
  787. if (tag != 0x04)
  788. goto fail;
  789. /* MP4DecConfigDescrTag */
  790. get_byte(pb); /* objectTypeId */
  791. get_be32(pb); /* streamType + buffer size */
  792. get_be32(pb); /* max bit rate */
  793. get_be32(pb); /* avg bit rate */
  794. len = mov_mp4_read_descr(pb, &tag);
  795. if (tag != 0x05)
  796. goto fail;
  797. /* MP4DecSpecificDescrTag */
  798. #ifdef DEBUG
  799. av_log(NULL, AV_LOG_DEBUG, "Specific MPEG4 header len=%d\n", len);
  800. #endif
  801. sc->header_data = av_mallocz(len);
  802. if (sc->header_data) {
  803. get_buffer(pb, sc->header_data, len);
  804. sc->header_len = len;
  805. }
  806. }
  807. /* in any case, skip garbage */
  808. }
  809. break;
  810. default:
  811. break;
  812. }
  813. fail:
  814. av_log(NULL, AV_LOG_DEBUG, "ATOMENEWSIZE %Ld %d\n", atom.size, url_ftell(pb) - start_pos);
  815. if (atom.size > 8) {
  816. url_fskip(pb, (atom.size - 8) -
  817. ((url_ftell(pb) - start_pos)));
  818. size -= atom.size - 8;
  819. }
  820. }
  821. if (size > 0) {
  822. /* unknown extension */
  823. url_fskip(pb, size);
  824. }
  825. #else
  826. /* figure out the palette situation */
  827. color_depth = st->codec.bits_per_sample & 0x1F;
  828. color_greyscale = st->codec.bits_per_sample & 0x20;
  829. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  830. if ((color_depth == 2) || (color_depth == 4) ||
  831. (color_depth == 8)) {
  832. if (color_greyscale) {
  833. /* compute the greyscale palette */
  834. color_count = 1 << color_depth;
  835. color_index = 255;
  836. color_dec = 256 / (color_count - 1);
  837. for (j = 0; j < color_count; j++) {
  838. r = g = b = color_index;
  839. c->palette_control.palette[j] =
  840. (r << 16) | (g << 8) | (b);
  841. color_index -= color_dec;
  842. if (color_index < 0)
  843. color_index = 0;
  844. }
  845. } else if (st->codec.color_table_id & 0x08) {
  846. /* if flag bit 3 is set, use the default palette */
  847. color_count = 1 << color_depth;
  848. if (color_depth == 2)
  849. color_table = ff_qt_default_palette_4;
  850. else if (color_depth == 4)
  851. color_table = ff_qt_default_palette_16;
  852. else
  853. color_table = ff_qt_default_palette_256;
  854. for (j = 0; j < color_count; j++) {
  855. r = color_table[j * 4 + 0];
  856. g = color_table[j * 4 + 1];
  857. b = color_table[j * 4 + 2];
  858. c->palette_control.palette[j] =
  859. (r << 16) | (g << 8) | (b);
  860. }
  861. } else {
  862. /* load the palette from the file */
  863. color_start = get_be32(pb);
  864. color_count = get_be16(pb);
  865. color_end = get_be16(pb);
  866. for (j = color_start; j <= color_end; j++) {
  867. /* each R, G, or B component is 16 bits;
  868. * only use the top 8 bits; skip alpha bytes
  869. * up front */
  870. get_byte(pb);
  871. get_byte(pb);
  872. r = get_byte(pb);
  873. get_byte(pb);
  874. g = get_byte(pb);
  875. get_byte(pb);
  876. b = get_byte(pb);
  877. get_byte(pb);
  878. c->palette_control.palette[j] =
  879. (r << 16) | (g << 8) | (b);
  880. }
  881. }
  882. st->codec.palctrl = &c->palette_control;
  883. st->codec.palctrl->palette_changed = 1;
  884. } else
  885. st->codec.palctrl = NULL;
  886. a.size = size;
  887. mov_read_default(c, pb, a);
  888. #endif
  889. } else {
  890. st->codec.codec_id = codec_get_id(mov_audio_tags, format);
  891. if(st->codec.codec_id==CODEC_ID_AMR_NB || st->codec.codec_id==CODEC_ID_AMR_WB) //from TS26.244
  892. {
  893. #ifdef DEBUG
  894. av_log(NULL, AV_LOG_DEBUG, "AMR-NB or AMR-WB audio identified!!\n");
  895. #endif
  896. get_be32(pb);get_be32(pb); //Reserved_8
  897. get_be16(pb);//Reserved_2
  898. get_be16(pb);//Reserved_2
  899. get_be32(pb);//Reserved_4
  900. get_be16(pb);//TimeScale
  901. get_be16(pb);//Reserved_2
  902. //AMRSpecificBox.(10 bytes)
  903. get_be32(pb); //size
  904. get_be32(pb); //type=='damr'
  905. get_be32(pb); //vendor
  906. get_byte(pb); //decoder version
  907. get_be16(pb); //mode_set
  908. get_byte(pb); //mode_change_period
  909. get_byte(pb); //frames_per_sample
  910. st->duration = AV_NOPTS_VALUE;//Not possible to get from this info, must count number of AMR frames
  911. if(st->codec.codec_id==CODEC_ID_AMR_NB)
  912. {
  913. st->codec.sample_rate=8000;
  914. st->codec.channels=1;
  915. }
  916. else //AMR-WB
  917. {
  918. st->codec.sample_rate=16000;
  919. st->codec.channels=1;
  920. }
  921. st->codec.bits_per_sample=16;
  922. st->codec.bit_rate=0; /*It is not possible to tell this before we have
  923. an audio frame and even then every frame can be different*/
  924. }
  925. else if( st->codec.codec_tag == MKTAG( 'm', 'p', '4', 's' ))
  926. {
  927. //This is some stuff for the hint track, lets ignore it!
  928. //Do some mp4 auto detect.
  929. c->mp4=1;
  930. size-=(16);
  931. url_fskip(pb, size); /* The mp4s atom also contians a esds atom that we can skip*/
  932. }
  933. else if( st->codec.codec_tag == MKTAG( 'm', 'p', '4', 'a' ))
  934. {
  935. MOV_atom_t a;
  936. int mp4_version;
  937. /* Handle mp4 audio tag */
  938. mp4_version=get_be16(pb);/*version*/
  939. get_be16(pb); /*revesion*/
  940. get_be32(pb);
  941. st->codec.channels = get_be16(pb); /* channels */
  942. st->codec.bits_per_sample = get_be16(pb); /* bits per sample */
  943. get_be32(pb);
  944. st->codec.sample_rate = get_be16(pb); /* sample rate, not always correct */
  945. get_be16(pb);
  946. c->mp4=1;
  947. if(mp4_version==1)
  948. {
  949. url_fskip(pb,16);
  950. a.size=size-(16+20+16);
  951. }
  952. else
  953. a.size=size-(16+20);
  954. a.offset=url_ftell(pb);
  955. mov_read_default(c, pb, a);
  956. /* Get correct sample rate from extradata */
  957. if(st->codec.extradata_size) {
  958. const int samplerate_table[] = {
  959. 96000, 88200, 64000, 48000, 44100, 32000,
  960. 24000, 22050, 16000, 12000, 11025, 8000,
  961. 7350, 0, 0, 0
  962. };
  963. unsigned char *px = st->codec.extradata;
  964. // 5 bits objectTypeIndex, 4 bits sampleRateIndex, 4 bits channels
  965. int samplerate_index = ((px[0] & 7) << 1) + ((px[1] >> 7) & 1);
  966. st->codec.sample_rate = samplerate_table[samplerate_index];
  967. st->codec.channels = (px[1] >> 3) & 15;
  968. }
  969. }
  970. else if( st->codec.codec_tag == MKTAG( 'a', 'l', 'a', 'c' ))
  971. {
  972. /* Handle alac audio tag + special extradata */
  973. get_be32(pb); /* version */
  974. get_be32(pb);
  975. st->codec.channels = get_be16(pb); /* channels */
  976. st->codec.bits_per_sample = get_be16(pb); /* bits per sample */
  977. get_be32(pb);
  978. st->codec.sample_rate = get_be16(pb);
  979. get_be16(pb);
  980. /* fetch the 36-byte extradata needed for alac decoding */
  981. st->codec.extradata_size = 36;
  982. st->codec.extradata = (uint8_t*)
  983. av_mallocz(st->codec.extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  984. get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
  985. }
  986. else if(size>=(16+20))
  987. {//16 bytes read, reading atleast 20 more
  988. uint16_t version;
  989. #ifdef DEBUG
  990. av_log(NULL, AV_LOG_DEBUG, "audio size=0x%X\n",size);
  991. #endif
  992. version = get_be16(pb); /* version */
  993. get_be16(pb); /* revision level */
  994. get_be32(pb); /* vendor */
  995. st->codec.channels = get_be16(pb); /* channel count */
  996. st->codec.bits_per_sample = get_be16(pb); /* sample size */
  997. /* handle specific s8 codec */
  998. get_be16(pb); /* compression id = 0*/
  999. get_be16(pb); /* packet size = 0 */
  1000. st->codec.sample_rate = ((get_be32(pb) >> 16));
  1001. //av_log(NULL, AV_LOG_DEBUG, "CODECID %d %d %.4s\n", st->codec.codec_id, CODEC_ID_PCM_S16BE, (char*)&format);
  1002. switch (st->codec.codec_id) {
  1003. case CODEC_ID_PCM_S16BE:
  1004. if (st->codec.bits_per_sample == 8)
  1005. st->codec.codec_id = CODEC_ID_PCM_S8;
  1006. /* fall */
  1007. case CODEC_ID_PCM_U8:
  1008. st->codec.bit_rate = st->codec.sample_rate * 8;
  1009. break;
  1010. default:
  1011. ;
  1012. }
  1013. //Read QT version 1 fields. In version 0 theese dont exist
  1014. #ifdef DEBUG
  1015. av_log(NULL, AV_LOG_DEBUG, "version =%d mp4=%d\n",version,c->mp4);
  1016. av_log(NULL, AV_LOG_DEBUG, "size-(16+20+16)=%d\n",size-(16+20+16));
  1017. #endif
  1018. if((version==1) && size>=(16+20+16))
  1019. {
  1020. get_be32(pb); /* samples per packet */
  1021. get_be32(pb); /* bytes per packet */
  1022. get_be32(pb); /* bytes per frame */
  1023. get_be32(pb); /* bytes per sample */
  1024. if(size>(16+20+16))
  1025. {
  1026. //Optional, additional atom-based fields
  1027. MOV_atom_t a = { format, url_ftell(pb), size - (16 + 20 + 16 + 8) };
  1028. #ifdef DEBUG
  1029. 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 ),
  1030. (format >> 0) & 0xff,
  1031. (format >> 8) & 0xff,
  1032. (format >> 16) & 0xff,
  1033. (format >> 24) & 0xff);
  1034. #endif
  1035. mov_read_default(c, pb, a);
  1036. }
  1037. }
  1038. else
  1039. {
  1040. //We should be down to 0 bytes here, but lets make sure.
  1041. size-=(16+20);
  1042. #ifdef DEBUG
  1043. if(size>0)
  1044. av_log(NULL, AV_LOG_DEBUG, "skipping 0x%X bytes\n",size-(16+20));
  1045. #endif
  1046. url_fskip(pb, size);
  1047. }
  1048. }
  1049. else
  1050. {
  1051. size-=16;
  1052. //Unknown size, but lets do our best and skip the rest.
  1053. #ifdef DEBUG
  1054. av_log(NULL, AV_LOG_DEBUG, "Strange size, skipping 0x%X bytes\n",size);
  1055. #endif
  1056. url_fskip(pb, size);
  1057. }
  1058. }
  1059. }
  1060. return 0;
  1061. }
  1062. static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1063. {
  1064. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1065. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1066. unsigned int i, entries;
  1067. print_atom("stsc", atom);
  1068. get_byte(pb); /* version */
  1069. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1070. entries = get_be32(pb);
  1071. if(entries >= UINT_MAX / sizeof(MOV_sample_to_chunk_tbl))
  1072. return -1;
  1073. #ifdef DEBUG
  1074. av_log(NULL, AV_LOG_DEBUG, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  1075. #endif
  1076. sc->sample_to_chunk_sz = entries;
  1077. sc->sample_to_chunk = (MOV_sample_to_chunk_tbl*) av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
  1078. if (!sc->sample_to_chunk)
  1079. return -1;
  1080. for(i=0; i<entries; i++) {
  1081. sc->sample_to_chunk[i].first = get_be32(pb);
  1082. sc->sample_to_chunk[i].count = get_be32(pb);
  1083. sc->sample_to_chunk[i].id = get_be32(pb);
  1084. #ifdef DEBUG
  1085. /* 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); */
  1086. #endif
  1087. }
  1088. return 0;
  1089. }
  1090. static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1091. {
  1092. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1093. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1094. unsigned int i, entries;
  1095. print_atom("stss", atom);
  1096. get_byte(pb); /* version */
  1097. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1098. entries = get_be32(pb);
  1099. if(entries >= UINT_MAX / sizeof(long))
  1100. return -1;
  1101. sc->keyframe_count = entries;
  1102. #ifdef DEBUG
  1103. av_log(NULL, AV_LOG_DEBUG, "keyframe_count = %ld\n", sc->keyframe_count);
  1104. #endif
  1105. sc->keyframes = (long*) av_malloc(entries * sizeof(long));
  1106. if (!sc->keyframes)
  1107. return -1;
  1108. for(i=0; i<entries; i++) {
  1109. sc->keyframes[i] = get_be32(pb);
  1110. #ifdef DEBUG
  1111. /* av_log(NULL, AV_LOG_DEBUG, "keyframes[]=%ld\n", sc->keyframes[i]); */
  1112. #endif
  1113. }
  1114. return 0;
  1115. }
  1116. static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1117. {
  1118. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1119. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1120. unsigned int i, entries;
  1121. print_atom("stsz", atom);
  1122. get_byte(pb); /* version */
  1123. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1124. sc->sample_size = get_be32(pb);
  1125. entries = get_be32(pb);
  1126. if(entries >= UINT_MAX / sizeof(long))
  1127. return -1;
  1128. sc->sample_count = entries;
  1129. #ifdef DEBUG
  1130. av_log(NULL, AV_LOG_DEBUG, "sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
  1131. #endif
  1132. if(sc->sample_size)
  1133. return 0; /* there isn't any table following */
  1134. sc->sample_sizes = (long*) av_malloc(entries * sizeof(long));
  1135. if (!sc->sample_sizes)
  1136. return -1;
  1137. for(i=0; i<entries; i++) {
  1138. sc->sample_sizes[i] = get_be32(pb);
  1139. #ifdef DEBUG
  1140. av_log(NULL, AV_LOG_DEBUG, "sample_sizes[]=%ld\n", sc->sample_sizes[i]);
  1141. #endif
  1142. }
  1143. return 0;
  1144. }
  1145. static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1146. {
  1147. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1148. //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1149. unsigned int i, entries;
  1150. int64_t duration=0;
  1151. int64_t total_sample_count=0;
  1152. print_atom("stts", atom);
  1153. get_byte(pb); /* version */
  1154. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1155. entries = get_be32(pb);
  1156. if(entries >= UINT_MAX / sizeof(Time2Sample))
  1157. return -1;
  1158. c->streams[c->fc->nb_streams-1]->stts_count = entries;
  1159. c->streams[c->fc->nb_streams-1]->stts_data = av_malloc(entries * sizeof(Time2Sample));
  1160. #ifdef DEBUG
  1161. av_log(NULL, AV_LOG_DEBUG, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  1162. #endif
  1163. for(i=0; i<entries; i++) {
  1164. int sample_duration;
  1165. int sample_count;
  1166. sample_count=get_be32(pb);
  1167. sample_duration = get_be32(pb);
  1168. c->streams[c->fc->nb_streams - 1]->stts_data[i].count= sample_count;
  1169. c->streams[c->fc->nb_streams - 1]->stts_data[i].duration= sample_duration;
  1170. #ifdef DEBUG
  1171. av_log(NULL, AV_LOG_DEBUG, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  1172. #endif
  1173. duration+=sample_duration*sample_count;
  1174. total_sample_count+=sample_count;
  1175. #if 0 //We calculate an average instead, needed by .mp4-files created with nec e606 3g phone
  1176. if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
  1177. st->codec.frame_rate_base = sample_duration ? sample_duration : 1;
  1178. st->codec.frame_rate = c->streams[c->fc->nb_streams-1]->time_scale;
  1179. #ifdef DEBUG
  1180. av_log(NULL, AV_LOG_DEBUG, "VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
  1181. #endif
  1182. }
  1183. #endif
  1184. }
  1185. /*The stsd atom which contain codec type sometimes comes after the stts so we cannot check for codec_type*/
  1186. if(duration>0)
  1187. {
  1188. av_reduce(
  1189. &st->codec.frame_rate,
  1190. &st->codec.frame_rate_base,
  1191. c->streams[c->fc->nb_streams-1]->time_scale * total_sample_count,
  1192. duration,
  1193. INT_MAX
  1194. );
  1195. #ifdef DEBUG
  1196. 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);
  1197. #endif
  1198. }
  1199. else
  1200. {
  1201. st->codec.frame_rate_base = 1;
  1202. st->codec.frame_rate = c->streams[c->fc->nb_streams-1]->time_scale;
  1203. }
  1204. return 0;
  1205. }
  1206. static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1207. {
  1208. // AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1209. //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1210. unsigned int i, entries;
  1211. print_atom("ctts", atom);
  1212. get_byte(pb); /* version */
  1213. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1214. entries = get_be32(pb);
  1215. if(entries >= UINT_MAX / sizeof(Time2Sample))
  1216. return -1;
  1217. c->streams[c->fc->nb_streams-1]->ctts_count = entries;
  1218. c->streams[c->fc->nb_streams-1]->ctts_data = av_malloc(entries * sizeof(Time2Sample));
  1219. // #ifdef DEBUG
  1220. av_log(NULL, AV_LOG_DEBUG, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1221. // #endif
  1222. for(i=0; i<entries; i++) {
  1223. c->streams[c->fc->nb_streams - 1]->ctts_data[i].count= get_be32(pb);
  1224. c->streams[c->fc->nb_streams - 1]->ctts_data[i].duration= get_be32(pb);
  1225. }
  1226. return 0;
  1227. }
  1228. static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1229. {
  1230. AVStream *st;
  1231. MOVStreamContext *sc;
  1232. print_atom("trak", atom);
  1233. st = av_new_stream(c->fc, c->fc->nb_streams);
  1234. if (!st) return -2;
  1235. sc = (MOVStreamContext*) av_mallocz(sizeof(MOVStreamContext));
  1236. if (!sc) {
  1237. av_free(st);
  1238. return -1;
  1239. }
  1240. sc->sample_to_chunk_index = -1;
  1241. st->priv_data = sc;
  1242. st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
  1243. st->start_time = 0; /* XXX: check */
  1244. st->duration = (c->duration * (int64_t)AV_TIME_BASE) / c->time_scale;
  1245. c->streams[c->fc->nb_streams-1] = sc;
  1246. return mov_read_default(c, pb, atom);
  1247. }
  1248. static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1249. {
  1250. AVStream *st;
  1251. print_atom("tkhd", atom);
  1252. st = c->fc->streams[c->fc->nb_streams-1];
  1253. get_byte(pb); /* version */
  1254. get_byte(pb); get_byte(pb);
  1255. get_byte(pb); /* flags */
  1256. /*
  1257. MOV_TRACK_ENABLED 0x0001
  1258. MOV_TRACK_IN_MOVIE 0x0002
  1259. MOV_TRACK_IN_PREVIEW 0x0004
  1260. MOV_TRACK_IN_POSTER 0x0008
  1261. */
  1262. get_be32(pb); /* creation time */
  1263. get_be32(pb); /* modification time */
  1264. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  1265. get_be32(pb); /* reserved */
  1266. st->start_time = 0; /* check */
  1267. st->duration = (get_be32(pb) * (int64_t)AV_TIME_BASE) / c->time_scale; /* duration */
  1268. get_be32(pb); /* reserved */
  1269. get_be32(pb); /* reserved */
  1270. get_be16(pb); /* layer */
  1271. get_be16(pb); /* alternate group */
  1272. get_be16(pb); /* volume */
  1273. get_be16(pb); /* reserved */
  1274. url_fskip(pb, 36); /* display matrix */
  1275. /* those are fixed-point */
  1276. st->codec.width = get_be32(pb) >> 16; /* track width */
  1277. st->codec.height = get_be32(pb) >> 16; /* track height */
  1278. return 0;
  1279. }
  1280. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1281. /* like the files created with Adobe Premiere 5.0, for samples see */
  1282. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1283. static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1284. {
  1285. int err;
  1286. #ifdef DEBUG
  1287. print_atom("wide", atom);
  1288. debug_indent++;
  1289. #endif
  1290. if (atom.size < 8)
  1291. return 0; /* continue */
  1292. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1293. url_fskip(pb, atom.size - 4);
  1294. return 0;
  1295. }
  1296. atom.type = get_le32(pb);
  1297. atom.offset += 8;
  1298. atom.size -= 8;
  1299. if (atom.type != MKTAG('m', 'd', 'a', 't')) {
  1300. url_fskip(pb, atom.size);
  1301. return 0;
  1302. }
  1303. err = mov_read_mdat(c, pb, atom);
  1304. #ifdef DEBUG
  1305. debug_indent--;
  1306. #endif
  1307. return err;
  1308. }
  1309. #ifdef CONFIG_ZLIB
  1310. static int null_read_packet(void *opaque, uint8_t *buf, int buf_size)
  1311. {
  1312. return -1;
  1313. }
  1314. static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1315. {
  1316. ByteIOContext ctx;
  1317. uint8_t *cmov_data;
  1318. uint8_t *moov_data; /* uncompressed data */
  1319. long cmov_len, moov_len;
  1320. int ret;
  1321. print_atom("cmov", atom);
  1322. get_be32(pb); /* dcom atom */
  1323. if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
  1324. return -1;
  1325. if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
  1326. av_log(NULL, AV_LOG_DEBUG, "unknown compression for cmov atom !");
  1327. return -1;
  1328. }
  1329. get_be32(pb); /* cmvd atom */
  1330. if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
  1331. return -1;
  1332. moov_len = get_be32(pb); /* uncompressed size */
  1333. cmov_len = atom.size - 6 * 4;
  1334. cmov_data = (uint8_t *) av_malloc(cmov_len);
  1335. if (!cmov_data)
  1336. return -1;
  1337. moov_data = (uint8_t *) av_malloc(moov_len);
  1338. if (!moov_data) {
  1339. av_free(cmov_data);
  1340. return -1;
  1341. }
  1342. get_buffer(pb, cmov_data, cmov_len);
  1343. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1344. return -1;
  1345. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
  1346. return -1;
  1347. ctx.buf_end = ctx.buffer + moov_len;
  1348. atom.type = MKTAG( 'm', 'o', 'o', 'v' );
  1349. atom.offset = 0;
  1350. atom.size = moov_len;
  1351. #ifdef DEBUG
  1352. { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
  1353. #endif
  1354. ret = mov_read_default(c, &ctx, atom);
  1355. av_free(moov_data);
  1356. av_free(cmov_data);
  1357. return ret;
  1358. }
  1359. #endif
  1360. /* edit list atom */
  1361. static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1362. {
  1363. int i, edit_count;
  1364. print_atom("elst", atom);
  1365. get_byte(pb); /* version */
  1366. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1367. edit_count= c->streams[c->fc->nb_streams-1]->edit_count = get_be32(pb); /* entries */
  1368. for(i=0; i<edit_count; i++){
  1369. get_be32(pb); /* Track duration */
  1370. get_be32(pb); /* Media time */
  1371. get_be32(pb); /* Media rate */
  1372. }
  1373. #ifdef DEBUG
  1374. 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);
  1375. #endif
  1376. return 0;
  1377. }
  1378. static const MOVParseTableEntry mov_default_parse_table[] = {
  1379. /* mp4 atoms */
  1380. { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
  1381. { MKTAG( 'c', 'p', 'r', 't' ), mov_read_default },
  1382. { MKTAG( 'c', 'r', 'h', 'd' ), mov_read_default },
  1383. { MKTAG( 'c', 't', 't', 's' ), mov_read_ctts }, /* composition time to sample */
  1384. { MKTAG( 'd', 'i', 'n', 'f' ), mov_read_default }, /* data information */
  1385. { MKTAG( 'd', 'p', 'n', 'd' ), mov_read_leaf },
  1386. { MKTAG( 'd', 'r', 'e', 'f' ), mov_read_leaf },
  1387. { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
  1388. { MKTAG( 'e', 'l', 's', 't' ), mov_read_elst },
  1389. { MKTAG( 'f', 'r', 'e', 'e' ), mov_read_leaf },
  1390. { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
  1391. { MKTAG( 'h', 'i', 'n', 't' ), mov_read_leaf },
  1392. { MKTAG( 'h', 'm', 'h', 'd' ), mov_read_leaf },
  1393. { MKTAG( 'i', 'o', 'd', 's' ), mov_read_leaf },
  1394. { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
  1395. { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
  1396. { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
  1397. { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
  1398. { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
  1399. { MKTAG( 'm', 'p', '4', 'a' ), mov_read_default },
  1400. { MKTAG( 'm', 'p', '4', 's' ), mov_read_default },
  1401. { MKTAG( 'm', 'p', '4', 'v' ), mov_read_default },
  1402. { MKTAG( 'm', 'p', 'o', 'd' ), mov_read_leaf },
  1403. { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
  1404. { MKTAG( 'n', 'm', 'h', 'd' ), mov_read_leaf },
  1405. { MKTAG( 'o', 'd', 'h', 'd' ), mov_read_default },
  1406. { MKTAG( 's', 'd', 'h', 'd' ), mov_read_default },
  1407. { MKTAG( 's', 'k', 'i', 'p' ), mov_read_leaf },
  1408. { MKTAG( 's', 'm', 'h', 'd' ), mov_read_leaf }, /* sound media info header */
  1409. { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorenson extension ??? */
  1410. { MKTAG( 'a', 'v', 'c', 'C' ), mov_read_avcC },
  1411. { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
  1412. { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
  1413. { MKTAG( 's', 't', 'd', 'p' ), mov_read_default },
  1414. { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
  1415. { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
  1416. { MKTAG( 's', 't', 's', 'h' ), mov_read_default },
  1417. { MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */
  1418. { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
  1419. { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
  1420. { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
  1421. { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
  1422. { MKTAG( 't', 'r', 'e', 'f' ), mov_read_default }, /* not really */
  1423. { MKTAG( 'u', 'd', 't', 'a' ), mov_read_leaf },
  1424. { MKTAG( 'u', 'r', 'l', ' ' ), mov_read_leaf },
  1425. { MKTAG( 'u', 'r', 'n', ' ' ), mov_read_leaf },
  1426. { MKTAG( 'u', 'u', 'i', 'd' ), mov_read_leaf },
  1427. { MKTAG( 'v', 'm', 'h', 'd' ), mov_read_leaf }, /* video media info header */
  1428. { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_default },
  1429. /* extra mp4 */
  1430. { MKTAG( 'M', 'D', 'E', 'S' ), mov_read_leaf },
  1431. /* QT atoms */
  1432. { MKTAG( 'c', 'h', 'a', 'p' ), mov_read_leaf },
  1433. { MKTAG( 'c', 'l', 'i', 'p' ), mov_read_default },
  1434. { MKTAG( 'c', 'r', 'g', 'n' ), mov_read_leaf },
  1435. { MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab },
  1436. { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
  1437. { MKTAG( 'k', 'm', 'a', 't' ), mov_read_leaf },
  1438. { MKTAG( 'm', 'a', 't', 't' ), mov_read_default },
  1439. { MKTAG( 'r', 'd', 'r', 'f' ), mov_read_leaf },
  1440. { MKTAG( 'r', 'm', 'd', 'a' ), mov_read_default },
  1441. { MKTAG( 'r', 'm', 'd', 'r' ), mov_read_leaf },
  1442. { MKTAG( 'r', 'm', 'r', 'a' ), mov_read_default },
  1443. { MKTAG( 's', 'c', 'p', 't' ), mov_read_leaf },
  1444. { MKTAG( 's', 's', 'r', 'c' ), mov_read_leaf },
  1445. { MKTAG( 's', 'y', 'n', 'c' ), mov_read_leaf },
  1446. { MKTAG( 't', 'c', 'm', 'd' ), mov_read_leaf },
  1447. { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
  1448. //{ MKTAG( 'r', 'm', 'q', 'u' ), mov_read_leaf },
  1449. #ifdef CONFIG_ZLIB
  1450. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
  1451. #else
  1452. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_leaf },
  1453. #endif
  1454. { 0L, mov_read_leaf }
  1455. };
  1456. static void mov_free_stream_context(MOVStreamContext *sc)
  1457. {
  1458. if(sc) {
  1459. av_freep(&sc->chunk_offsets);
  1460. av_freep(&sc->sample_to_chunk);
  1461. av_freep(&sc->sample_sizes);
  1462. av_freep(&sc->keyframes);
  1463. av_freep(&sc->header_data);
  1464. av_freep(&sc->stts_data);
  1465. av_freep(&sc->ctts_data);
  1466. av_freep(&sc);
  1467. }
  1468. }
  1469. static inline uint32_t mov_to_tag(uint8_t *buf)
  1470. {
  1471. return MKTAG(buf[0], buf[1], buf[2], buf[3]);
  1472. }
  1473. static inline uint32_t to_be32(uint8_t *buf)
  1474. {
  1475. return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  1476. }
  1477. /* XXX: is it sufficient ? */
  1478. static int mov_probe(AVProbeData *p)
  1479. {
  1480. unsigned int offset;
  1481. uint32_t tag;
  1482. int score = 0;
  1483. /* check file header */
  1484. if (p->buf_size <= 12)
  1485. return 0;
  1486. offset = 0;
  1487. for(;;) {
  1488. /* ignore invalid offset */
  1489. if ((offset + 8) > (unsigned int)p->buf_size)
  1490. return score;
  1491. tag = mov_to_tag(p->buf + offset + 4);
  1492. switch(tag) {
  1493. /* check for obvious tags */
  1494. case MKTAG( 'm', 'o', 'o', 'v' ):
  1495. case MKTAG( 'm', 'd', 'a', 't' ):
  1496. case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
  1497. case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
  1498. return AVPROBE_SCORE_MAX;
  1499. /* those are more common words, so rate then a bit less */
  1500. case MKTAG( 'w', 'i', 'd', 'e' ):
  1501. case MKTAG( 'f', 'r', 'e', 'e' ):
  1502. case MKTAG( 'j', 'u', 'n', 'k' ):
  1503. case MKTAG( 'p', 'i', 'c', 't' ):
  1504. return AVPROBE_SCORE_MAX - 5;
  1505. case MKTAG( 'f', 't', 'y', 'p' ):
  1506. case MKTAG( 's', 'k', 'i', 'p' ):
  1507. case MKTAG( 'u', 'u', 'i', 'd' ):
  1508. offset = to_be32(p->buf+offset) + offset;
  1509. /* if we only find those cause probedata is too small at least rate them */
  1510. score = AVPROBE_SCORE_MAX - 50;
  1511. break;
  1512. default:
  1513. /* unrecognized tag */
  1514. return score;
  1515. }
  1516. }
  1517. return score;
  1518. }
  1519. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1520. {
  1521. MOVContext *mov = (MOVContext *) s->priv_data;
  1522. ByteIOContext *pb = &s->pb;
  1523. int i, j, nb, err;
  1524. MOV_atom_t atom = { 0, 0, 0 };
  1525. mov->fc = s;
  1526. mov->parse_table = mov_default_parse_table;
  1527. #if 0
  1528. /* XXX: I think we should auto detect */
  1529. if(s->iformat->name[1] == 'p')
  1530. mov->mp4 = 1;
  1531. #endif
  1532. if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  1533. atom.size = url_filesize(url_fileno(pb));
  1534. else
  1535. atom.size = 0x7FFFFFFFFFFFFFFFLL;
  1536. #ifdef DEBUG
  1537. av_log(NULL, AV_LOG_DEBUG, "filesz=%Ld\n", atom.size);
  1538. #endif
  1539. /* check MOV header */
  1540. err = mov_read_default(mov, pb, atom);
  1541. if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
  1542. av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%lld\n",
  1543. err, mov->found_moov, mov->found_mdat, url_ftell(pb));
  1544. return -1;
  1545. }
  1546. #ifdef DEBUG
  1547. av_log(NULL, AV_LOG_DEBUG, "on_parse_exit_offset=%d\n", (int) url_ftell(pb));
  1548. #endif
  1549. /* some cleanup : make sure we are on the mdat atom */
  1550. if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
  1551. url_fseek(pb, mov->mdat_offset, SEEK_SET);
  1552. mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
  1553. #ifdef DEBUG
  1554. av_log(NULL, AV_LOG_DEBUG, "mdat_reset_offset=%d\n", (int) url_ftell(pb));
  1555. #endif
  1556. #ifdef DEBUG
  1557. av_log(NULL, AV_LOG_DEBUG, "streams= %d\n", s->nb_streams);
  1558. #endif
  1559. mov->total_streams = nb = s->nb_streams;
  1560. #if 1
  1561. for(i=0; i<s->nb_streams;) {
  1562. if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
  1563. av_free(s->streams[i]);
  1564. for(j=i+1; j<s->nb_streams; j++)
  1565. s->streams[j-1] = s->streams[j];
  1566. s->nb_streams--;
  1567. } else
  1568. i++;
  1569. }
  1570. for(i=0; i<s->nb_streams;i++) {
  1571. MOVStreamContext *sc;
  1572. sc = (MOVStreamContext *)s->streams[i]->priv_data;
  1573. sc->ffindex = i;
  1574. sc->is_ff_stream = 1;
  1575. }
  1576. #endif
  1577. #ifdef DEBUG
  1578. av_log(NULL, AV_LOG_DEBUG, "real streams= %d\n", s->nb_streams);
  1579. #endif
  1580. return 0;
  1581. }
  1582. /* Yes, this is ugly... I didn't write the specs of QT :p */
  1583. /* XXX:remove useless commented code sometime */
  1584. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  1585. {
  1586. MOVContext *mov = (MOVContext *) s->priv_data;
  1587. MOVStreamContext *sc;
  1588. int64_t offset = 0x0FFFFFFFFFFFFFFFLL;
  1589. int i, a, b, m;
  1590. int size;
  1591. int idx;
  1592. size = 0x0FFFFFFF;
  1593. #ifdef MOV_SPLIT_CHUNKS
  1594. if (mov->partial) {
  1595. sc = mov->partial;
  1596. idx = sc->sample_to_chunk_index;
  1597. if (idx < 0) return 0;
  1598. #ifdef DEBUG
  1599. fprintf(stderr, "sc[ffid %d]->sample_size = %d\n", sc->ffindex, sc->sample_size);
  1600. #endif
  1601. //size = sc->sample_sizes[sc->current_sample];
  1602. // that ain't working...
  1603. //size = (sc->sample_size)?sc->sample_size:sc->sample_sizes[sc->current_sample];
  1604. size = (sc->sample_size > 1)?sc->sample_size:sc->sample_sizes[sc->current_sample];
  1605. sc->current_sample++;
  1606. sc->left_in_chunk--;
  1607. if (sc->left_in_chunk <= 0)
  1608. mov->partial = 0;
  1609. offset = mov->next_chunk_offset;
  1610. /* extract the sample */
  1611. goto readchunk;
  1612. }
  1613. #endif
  1614. again:
  1615. sc = 0;
  1616. for(i=0; i<mov->total_streams; i++) {
  1617. MOVStreamContext *msc = mov->streams[i];
  1618. //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));
  1619. if ((msc->next_chunk < msc->chunk_count) && msc->next_chunk >= 0
  1620. && (msc->chunk_offsets[msc->next_chunk] < offset)) {
  1621. sc = msc;
  1622. offset = msc->chunk_offsets[msc->next_chunk];
  1623. //av_log(NULL, AV_LOG_DEBUG, "SELETED %Ld i:%d\n", offset, i);
  1624. }
  1625. }
  1626. if (!sc || offset==0x0FFFFFFFFFFFFFFFLL)
  1627. return -1;
  1628. sc->next_chunk++;
  1629. if(mov->next_chunk_offset < offset) { /* some meta data */
  1630. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1631. mov->next_chunk_offset = offset;
  1632. }
  1633. //av_log(NULL, AV_LOG_DEBUG, "chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
  1634. if(!sc->is_ff_stream || (s->streams[sc->ffindex]->discard >= AVDISCARD_ALL)) {
  1635. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1636. mov->next_chunk_offset = offset;
  1637. offset = 0x0FFFFFFFFFFFFFFFLL;
  1638. goto again;
  1639. }
  1640. /* now get the chunk size... */
  1641. for(i=0; i<mov->total_streams; i++) {
  1642. MOVStreamContext *msc = mov->streams[i];
  1643. if ((msc->next_chunk < msc->chunk_count)
  1644. && ((msc->chunk_offsets[msc->next_chunk] - offset) < size))
  1645. size = msc->chunk_offsets[msc->next_chunk] - offset;
  1646. }
  1647. #ifdef MOV_MINOLTA_FIX
  1648. //Make sure that size is according to sample_size (Needed by .mov files
  1649. //created on a Minolta Dimage Xi where audio chunks contains waste data in the end)
  1650. //Maybe we should really not only check sc->sample_size, but also sc->sample_sizes
  1651. //but I have no such movies
  1652. if (sc->sample_size > 0) {
  1653. int foundsize=0;
  1654. for(i=0; i<(sc->sample_to_chunk_sz); i++) {
  1655. if( (sc->sample_to_chunk[i].first)<=(sc->next_chunk) )
  1656. {
  1657. // I can't figure out why for PCM audio sample_size is always 1
  1658. // (it should actually be channels*bits_per_second/8) but it is.
  1659. AVCodecContext* cod = &s->streams[sc->ffindex]->codec;
  1660. if (sc->sample_size == 1 && (cod->codec_id == CODEC_ID_PCM_S16BE || cod->codec_id == CODEC_ID_PCM_S16LE))
  1661. foundsize=(sc->sample_to_chunk[i].count*cod->channels*cod->bits_per_sample)/8;
  1662. else
  1663. foundsize=sc->sample_to_chunk[i].count*sc->sample_size;
  1664. }
  1665. #ifdef DEBUG
  1666. 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);
  1667. #endif
  1668. }
  1669. if( (foundsize>0) && (foundsize<size) )
  1670. {
  1671. #ifdef DEBUG
  1672. /*av_log(NULL, AV_LOG_DEBUG, "this size should actually be %d\n",foundsize);*/
  1673. #endif
  1674. size=foundsize;
  1675. }
  1676. }
  1677. #endif //MOV_MINOLTA_FIX
  1678. #ifdef MOV_SPLIT_CHUNKS
  1679. /* split chunks into samples */
  1680. if (sc->sample_size == 0) {
  1681. idx = sc->sample_to_chunk_index;
  1682. if ((idx + 1 < sc->sample_to_chunk_sz)
  1683. && (sc->next_chunk >= sc->sample_to_chunk[idx + 1].first))
  1684. idx++;
  1685. sc->sample_to_chunk_index = idx;
  1686. if (idx >= 0 && sc->sample_to_chunk[idx].count != 1) {
  1687. mov->partial = sc;
  1688. /* we'll have to get those samples before next chunk */
  1689. sc->left_in_chunk = sc->sample_to_chunk[idx].count - 1;
  1690. size = (sc->sample_size > 1)?sc->sample_size:sc->sample_sizes[sc->current_sample];
  1691. }
  1692. sc->current_sample++;
  1693. }
  1694. #endif
  1695. readchunk:
  1696. #ifdef DEBUG
  1697. av_log(NULL, AV_LOG_DEBUG, "chunk: %lli -> %lli (%i)\n", offset, offset + size, size);
  1698. #endif
  1699. if(size == 0x0FFFFFFF)
  1700. size = mov->mdat_size + mov->mdat_offset - offset;
  1701. if(size < 0)
  1702. return -1;
  1703. if(size == 0)
  1704. return -1;
  1705. url_fseek(&s->pb, offset, SEEK_SET);
  1706. //av_log(NULL, AV_LOG_DEBUG, "READCHUNK hlen: %d %d off: %Ld pos:%Ld\n", size, sc->header_len, offset, url_ftell(&s->pb));
  1707. if (sc->header_len > 0) {
  1708. av_new_packet(pkt, size + sc->header_len);
  1709. memcpy(pkt->data, sc->header_data, sc->header_len);
  1710. get_buffer(&s->pb, pkt->data + sc->header_len, size);
  1711. /* free header */
  1712. av_freep(&sc->header_data);
  1713. sc->header_len = 0;
  1714. } else {
  1715. av_new_packet(pkt, size);
  1716. get_buffer(&s->pb, pkt->data, pkt->size);
  1717. }
  1718. pkt->stream_index = sc->ffindex;
  1719. // If the keyframes table exists, mark any samples that are in the table as key frames.
  1720. // If no table exists, treat very sample as a key frame.
  1721. if (sc->keyframes) {
  1722. a = 0;
  1723. b = sc->keyframe_count - 1;
  1724. while (a < b) {
  1725. m = (a + b + 1) >> 1;
  1726. if (sc->keyframes[m] > sc->current_sample) {
  1727. b = m - 1;
  1728. } else {
  1729. a = m;
  1730. }
  1731. }
  1732. if (sc->keyframes[a] == sc->current_sample)
  1733. pkt->flags |= PKT_FLAG_KEY;
  1734. }
  1735. else
  1736. pkt->flags |= PKT_FLAG_KEY;
  1737. #ifdef DEBUG
  1738. /*
  1739. av_log(NULL, AV_LOG_DEBUG, "Packet (%d, %ld) ", pkt->stream_index, pkt->size);
  1740. for(i=0; i<8; i++)
  1741. av_log(NULL, AV_LOG_DEBUG, "%02x ", pkt->data[i]);
  1742. for(i=0; i<8; i++)
  1743. av_log(NULL, AV_LOG_DEBUG, "%c ", (pkt->data[i]) & 0x7F);
  1744. av_log(NULL, AV_LOG_DEBUG, "\n");
  1745. */
  1746. #endif
  1747. mov->next_chunk_offset = offset + size;
  1748. /* find the corresponding dts */
  1749. if (sc && sc->sample_to_time_index < sc->stts_count && pkt) {
  1750. unsigned int count;
  1751. uint64_t dts, pts;
  1752. unsigned int duration = sc->stts_data[sc->sample_to_time_index].duration;
  1753. count = sc->stts_data[sc->sample_to_time_index].count;
  1754. if ((sc->sample_to_time_sample + count) < sc->current_sample) {
  1755. sc->sample_to_time_sample += count;
  1756. sc->sample_to_time_time += count*duration;
  1757. sc->sample_to_time_index ++;
  1758. duration = sc->stts_data[sc->sample_to_time_index].duration;
  1759. }
  1760. dts = sc->sample_to_time_time + (sc->current_sample-1 - sc->sample_to_time_sample) * (int64_t)duration;
  1761. /* find the corresponding pts */
  1762. if (sc->sample_to_ctime_index < sc->ctts_count) {
  1763. int duration = sc->ctts_data[sc->sample_to_ctime_index].duration;
  1764. int count = sc->ctts_data[sc->sample_to_ctime_index].count;
  1765. if ((sc->sample_to_ctime_sample + count) < sc->current_sample) {
  1766. sc->sample_to_ctime_sample += count;
  1767. sc->sample_to_ctime_index ++;
  1768. duration = sc->ctts_data[sc->sample_to_ctime_index].duration;
  1769. }
  1770. pts = dts + duration;
  1771. }else
  1772. pts = dts;
  1773. pkt->pts = av_rescale( pts,
  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. pkt->dts = av_rescale( dts,
  1777. (int64_t)s->streams[sc->ffindex]->time_base.den,
  1778. (int64_t)sc->time_scale * (int64_t)s->streams[sc->ffindex]->time_base.num );
  1779. #ifdef DEBUG
  1780. 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"
  1781. , pkt->stream_index, sc->current_sample-1, pkt->dts, pkt->pts
  1782. , sc->sample_to_time_sample
  1783. , sc->sample_to_time_time
  1784. , sc->sample_to_time_index
  1785. , sc->stts_count
  1786. , count
  1787. , duration);
  1788. #endif
  1789. }
  1790. return 0;
  1791. }
  1792. #if defined(MOV_SPLIT_CHUNKS) && defined(MOV_SEEK)
  1793. /**
  1794. * Seek method based on the one described in the Appendix C of QTFileFormat.pdf
  1795. */
  1796. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  1797. {
  1798. MOVContext* mov = (MOVContext *) s->priv_data;
  1799. MOVStreamContext* sc;
  1800. int32_t i, a, b, m;
  1801. int64_t sample_time;
  1802. int64_t start_time;
  1803. int32_t seek_sample, sample;
  1804. int32_t duration;
  1805. int32_t count;
  1806. int32_t chunk;
  1807. int32_t left_in_chunk;
  1808. int64_t chunk_file_offset;
  1809. int64_t sample_file_offset;
  1810. int32_t first_chunk_sample;
  1811. int32_t sample_to_chunk_idx;
  1812. int sample_to_time_index;
  1813. long sample_to_time_sample = 0;
  1814. uint64_t sample_to_time_time = 0;
  1815. int mov_idx;
  1816. // Find the corresponding mov stream
  1817. for (mov_idx = 0; mov_idx < mov->total_streams; mov_idx++)
  1818. if (mov->streams[mov_idx]->ffindex == stream_index)
  1819. break;
  1820. if (mov_idx == mov->total_streams) {
  1821. av_log(s, AV_LOG_ERROR, "mov: requested stream was not found in mov streams (idx=%i)\n", stream_index);
  1822. return -1;
  1823. }
  1824. sc = mov->streams[mov_idx];
  1825. // Step 1. Find the edit that contains the requested time (elst)
  1826. if (sc->edit_count) {
  1827. // FIXME should handle edit list
  1828. av_log(s, AV_LOG_ERROR, "mov: does not handle seeking in files that contain edit list (c:%d)\n", sc->edit_count);
  1829. return -1;
  1830. }
  1831. // Step 2. Find the corresponding sample using the Time-to-sample atom (stts) */
  1832. #ifdef DEBUG
  1833. av_log(s, AV_LOG_DEBUG, "Searching for time %li in stream #%i (time_scale=%i)\n", (long)timestamp, mov_idx, sc->time_scale);
  1834. #endif
  1835. // convert timestamp from time_base unit to timescale unit
  1836. sample_time = av_rescale( timestamp,
  1837. (int64_t)sc->time_scale * s->streams[stream_index]->time_base.num,
  1838. (int64_t)s->streams[stream_index]->time_base.den);
  1839. start_time = 0; // FIXME use elst atom
  1840. sample = 1; // sample are 0 based in table
  1841. #ifdef DEBUG
  1842. av_log(s, AV_LOG_DEBUG, "Searching for sample_time %li \n", (long)sample_time);
  1843. #endif
  1844. for (i = 0; i < sc->stts_count; i++) {
  1845. count = sc->stts_data[i].count;
  1846. duration = sc->stts_data[i].duration;
  1847. //av_log(s, AV_LOG_DEBUG, "> sample_time %lli \n", (long)sample_time);
  1848. //av_log(s, AV_LOG_DEBUG, "> count=%i duration=%i\n", count, duration);
  1849. if ((start_time + count*duration) > sample_time) {
  1850. sample_to_time_time = start_time;
  1851. sample_to_time_index = i;
  1852. sample_to_time_sample = sample;
  1853. sample += (sample_time - start_time) / duration;
  1854. break;
  1855. }
  1856. sample += count;
  1857. start_time += count * duration;
  1858. }
  1859. sample_to_time_time = start_time;
  1860. sample_to_time_index = i;
  1861. /* NOTE: despite what qt doc say, the dt value (Display Time in qt vocabulary) computed with the stts atom
  1862. is a decoding time stamp (dts) not a presentation time stamp. And as usual dts != pts for stream with b frames */
  1863. #ifdef DEBUG
  1864. av_log(s, AV_LOG_DEBUG, "Found time %li at sample #%u\n", (long)sample_time, sample);
  1865. #endif
  1866. if (sample > sc->sample_count) {
  1867. 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);
  1868. return -1;
  1869. }
  1870. // Step 3. Find the prior sync. sample using the Sync sample atom (stss)
  1871. if (sc->keyframes) {
  1872. a = 0;
  1873. b = sc->keyframe_count - 1;
  1874. while (a < b) {
  1875. m = (a + b + 1) >> 1;
  1876. if (sc->keyframes[m] > sample) {
  1877. b = m - 1;
  1878. } else {
  1879. a = m;
  1880. }
  1881. #ifdef DEBUG
  1882. // 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);
  1883. #endif
  1884. }
  1885. // for low latency prob: always use the previous keyframe, just uncomment the next line
  1886. // if (a) a--;
  1887. seek_sample = sc->keyframes[a];
  1888. }
  1889. else
  1890. seek_sample = sample; // else all samples are key frames
  1891. #ifdef DEBUG
  1892. av_log(s, AV_LOG_DEBUG, "Found nearest keyframe at sample #%i \n", seek_sample);
  1893. #endif
  1894. // Step 4. Find the chunk of the sample using the Sample-to-chunk-atom (stsc)
  1895. for (first_chunk_sample = 1, i = 0; i < (sc->sample_to_chunk_sz - 1); i++) {
  1896. b = (sc->sample_to_chunk[i + 1].first - sc->sample_to_chunk[i].first) * sc->sample_to_chunk[i].count;
  1897. if (seek_sample >= first_chunk_sample && seek_sample < (first_chunk_sample + b))
  1898. break;
  1899. first_chunk_sample += b;
  1900. }
  1901. chunk = sc->sample_to_chunk[i].first + (seek_sample - first_chunk_sample) / sc->sample_to_chunk[i].count;
  1902. left_in_chunk = sc->sample_to_chunk[i].count - (seek_sample - first_chunk_sample) % sc->sample_to_chunk[i].count;
  1903. first_chunk_sample += ((seek_sample - first_chunk_sample) / sc->sample_to_chunk[i].count) * sc->sample_to_chunk[i].count;
  1904. sample_to_chunk_idx = i;
  1905. #ifdef DEBUG
  1906. 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);
  1907. #endif
  1908. // Step 5. Find the offset of the chunk using the chunk offset atom
  1909. if (!sc->chunk_offsets) {
  1910. av_log(s, AV_LOG_ERROR, "mov: no chunk offset atom, unable to seek\n");
  1911. return -1;
  1912. }
  1913. if (chunk > sc->chunk_count) {
  1914. 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);
  1915. return -1;
  1916. }
  1917. chunk_file_offset = sc->chunk_offsets[chunk - 1];
  1918. #ifdef DEBUG
  1919. av_log(s, AV_LOG_DEBUG, "Chunk file offset is #%llu \n", chunk_file_offset);
  1920. #endif
  1921. // Step 6. Find the byte offset within the chunk using the sample size atom
  1922. sample_file_offset = chunk_file_offset;
  1923. if (sc->sample_size)
  1924. sample_file_offset += (seek_sample - first_chunk_sample) * sc->sample_size;
  1925. else {
  1926. for (i = 0; i < (seek_sample - first_chunk_sample); i++) {
  1927. sample_file_offset += sc->sample_sizes[first_chunk_sample + i - 1];
  1928. }
  1929. }
  1930. #ifdef DEBUG
  1931. av_log(s, AV_LOG_DEBUG, "Sample file offset is #%llu \n", sample_file_offset);
  1932. #endif
  1933. // Step 6. Update the parser
  1934. mov->partial = sc;
  1935. mov->next_chunk_offset = sample_file_offset;
  1936. // Update current stream state
  1937. sc->current_sample = seek_sample - 1; // zero based
  1938. sc->left_in_chunk = left_in_chunk;
  1939. sc->next_chunk = chunk; // +1 -1 (zero based)
  1940. sc->sample_to_chunk_index = sample_to_chunk_idx;
  1941. // Update other streams
  1942. for (i = 0; i<mov->total_streams; i++) {
  1943. MOVStreamContext *msc;
  1944. if (i == mov_idx) continue;
  1945. // Find the nearest 'next' chunk
  1946. msc = mov->streams[i];
  1947. a = 0;
  1948. b = msc->chunk_count - 1;
  1949. while (a < b) {
  1950. m = (a + b + 1) >> 1;
  1951. if (msc->chunk_offsets[m] > chunk_file_offset) {
  1952. b = m - 1;
  1953. } else {
  1954. a = m;
  1955. }
  1956. #ifdef DEBUG
  1957. /* av_log(s, AV_LOG_DEBUG, "a=%i (%li) b=%i (%li) m=%i (%li) stream #%i\n"
  1958. , a, (long)msc->chunk_offsets[a], b, (long)msc->chunk_offsets[b], m, (long)msc->chunk_offsets[m], i); */
  1959. #endif
  1960. }
  1961. msc->next_chunk = a;
  1962. if (msc->chunk_offsets[a] < chunk_file_offset && a < (msc->chunk_count-1))
  1963. msc->next_chunk ++;
  1964. #ifdef DEBUG
  1965. 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]);
  1966. #endif
  1967. // Compute sample count and index in the sample_to_chunk table (what a pity)
  1968. msc->sample_to_chunk_index = 0;
  1969. msc->current_sample = 0;
  1970. for(; msc->sample_to_chunk_index < (msc->sample_to_chunk_sz - 1)
  1971. && msc->sample_to_chunk[msc->sample_to_chunk_index + 1].first <= (1 + msc->next_chunk); msc->sample_to_chunk_index++) {
  1972. msc->current_sample += (msc->sample_to_chunk[msc->sample_to_chunk_index + 1].first - msc->sample_to_chunk[msc->sample_to_chunk_index].first) \
  1973. * msc->sample_to_chunk[msc->sample_to_chunk_index].count;
  1974. }
  1975. 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;
  1976. msc->left_in_chunk = msc->sample_to_chunk[msc->sample_to_chunk_index].count - 1;
  1977. // Find corresponding position in stts (used later to compute dts)
  1978. sample = 0;
  1979. start_time = 0;
  1980. for (msc->sample_to_time_index = 0; msc->sample_to_time_index < msc->stts_count; msc->sample_to_time_index++) {
  1981. count = msc->stts_data[msc->sample_to_time_index].count;
  1982. duration = msc->stts_data[msc->sample_to_time_index].duration;
  1983. if ((sample + count - 1) > msc->current_sample) {
  1984. msc->sample_to_time_time = start_time;
  1985. msc->sample_to_time_sample = sample;
  1986. break;
  1987. }
  1988. sample += count;
  1989. start_time += count * duration;
  1990. }
  1991. sample = 0;
  1992. for (msc->sample_to_ctime_index = 0; msc->sample_to_ctime_index < msc->ctts_count; msc->sample_to_ctime_index++) {
  1993. count = msc->ctts_data[msc->sample_to_ctime_index].count;
  1994. duration = msc->ctts_data[msc->sample_to_ctime_index].duration;
  1995. if ((sample + count - 1) > msc->current_sample) {
  1996. msc->sample_to_ctime_sample = sample;
  1997. break;
  1998. }
  1999. sample += count;
  2000. }
  2001. #ifdef DEBUG
  2002. 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);
  2003. #endif
  2004. }
  2005. return 0;
  2006. }
  2007. #endif
  2008. static int mov_read_close(AVFormatContext *s)
  2009. {
  2010. int i;
  2011. MOVContext *mov = (MOVContext *) s->priv_data;
  2012. for(i=0; i<mov->total_streams; i++)
  2013. mov_free_stream_context(mov->streams[i]);
  2014. /* free color tabs */
  2015. for(i=0; i<mov->ctab_size; i++)
  2016. av_freep(&mov->ctab[i]);
  2017. av_freep(&mov->ctab);
  2018. return 0;
  2019. }
  2020. static AVInputFormat mov_iformat = {
  2021. "mov,mp4,m4a,3gp,3g2",
  2022. "QuickTime/MPEG4 format",
  2023. sizeof(MOVContext),
  2024. mov_probe,
  2025. mov_read_header,
  2026. mov_read_packet,
  2027. mov_read_close,
  2028. #if defined(MOV_SPLIT_CHUNKS) && defined(MOV_SEEK)
  2029. mov_read_seek,
  2030. #endif
  2031. };
  2032. int mov_init(void)
  2033. {
  2034. av_register_input_format(&mov_iformat);
  2035. return 0;
  2036. }