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.

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