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.

2405 lines
84KB

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