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.

2382 lines
83KB

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