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.

1691 lines
56KB

  1. /*
  2. * MOV demuxer
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <limits.h>
  22. //#define DEBUG
  23. #include "avformat.h"
  24. #include "riff.h"
  25. #include "isom.h"
  26. #include "dv.h"
  27. #ifdef CONFIG_ZLIB
  28. #include <zlib.h>
  29. #endif
  30. /*
  31. * First version by Francois Revol revol@free.fr
  32. * Seek function by Gael Chardon gael.dev@4now.net
  33. *
  34. * Features and limitations:
  35. * - reads most of the QT files I have (at least the structure),
  36. * the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement.
  37. * FIXED, Francois Revol, 07/17/2002
  38. * - ffmpeg has nearly none of the usual QuickTime codecs,
  39. * although I succesfully dumped raw and mp3 audio tracks off .mov files.
  40. * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
  41. * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes
  42. * (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at
  43. * http://mpeg.telecomitalialab.com/faq.htm
  44. * - the code is quite ugly... maybe I won't do it recursive next time :-)
  45. * - seek is not supported with files that contain edit list
  46. *
  47. * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
  48. * when coding this :) (it's a writer anyway)
  49. *
  50. * Reference documents:
  51. * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
  52. * Apple:
  53. * http://developer.apple.com/documentation/QuickTime/QTFF/
  54. * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
  55. * QuickTime is a trademark of Apple (AFAIK :))
  56. */
  57. #include "qtpalette.h"
  58. #undef NDEBUG
  59. #include <assert.h>
  60. /* the QuickTime file format is quite convoluted...
  61. * it has lots of index tables, each indexing something in another one...
  62. * Here we just use what is needed to read the chunks
  63. */
  64. typedef struct MOV_sample_to_chunk_tbl {
  65. long first;
  66. long count;
  67. long id;
  68. } MOV_sample_to_chunk_tbl;
  69. typedef struct {
  70. uint32_t type;
  71. int64_t offset;
  72. int64_t size; /* total size (excluding the size and type fields) */
  73. } MOV_atom_t;
  74. typedef struct {
  75. int seed;
  76. int flags;
  77. int size;
  78. void* clrs;
  79. } MOV_ctab_t;
  80. typedef struct MOV_mdat_atom_s {
  81. offset_t offset;
  82. int64_t size;
  83. } MOV_mdat_atom_t;
  84. typedef struct {
  85. uint8_t version;
  86. uint32_t flags; // 24bit
  87. /* 0x03 ESDescrTag */
  88. uint16_t es_id;
  89. #define MP4ODescrTag 0x01
  90. #define MP4IODescrTag 0x02
  91. #define MP4ESDescrTag 0x03
  92. #define MP4DecConfigDescrTag 0x04
  93. #define MP4DecSpecificDescrTag 0x05
  94. #define MP4SLConfigDescrTag 0x06
  95. #define MP4ContentIdDescrTag 0x07
  96. #define MP4SupplContentIdDescrTag 0x08
  97. #define MP4IPIPtrDescrTag 0x09
  98. #define MP4IPMPPtrDescrTag 0x0A
  99. #define MP4IPMPDescrTag 0x0B
  100. #define MP4RegistrationDescrTag 0x0D
  101. #define MP4ESIDIncDescrTag 0x0E
  102. #define MP4ESIDRefDescrTag 0x0F
  103. #define MP4FileIODescrTag 0x10
  104. #define MP4FileODescrTag 0x11
  105. #define MP4ExtProfileLevelDescrTag 0x13
  106. #define MP4ExtDescrTagsStart 0x80
  107. #define MP4ExtDescrTagsEnd 0xFE
  108. uint8_t stream_priority;
  109. /* 0x04 DecConfigDescrTag */
  110. uint8_t object_type_id;
  111. uint8_t stream_type;
  112. /* XXX: really streamType is
  113. * only 6bit, followed by:
  114. * 1bit upStream
  115. * 1bit reserved
  116. */
  117. uint32_t buffer_size_db; // 24
  118. uint32_t max_bitrate;
  119. uint32_t avg_bitrate;
  120. /* 0x05 DecSpecificDescrTag */
  121. uint8_t decoder_cfg_len;
  122. uint8_t *decoder_cfg;
  123. /* 0x06 SLConfigDescrTag */
  124. uint8_t sl_config_len;
  125. uint8_t *sl_config;
  126. } MOV_esds_t;
  127. struct MOVParseTableEntry;
  128. typedef struct MOVStreamContext {
  129. int ffindex; /* the ffmpeg stream id */
  130. long next_chunk;
  131. unsigned int chunk_count;
  132. int64_t *chunk_offsets;
  133. unsigned int stts_count;
  134. Time2Sample *stts_data;
  135. unsigned int ctts_count;
  136. Time2Sample *ctts_data;
  137. unsigned int edit_count; /* number of 'edit' (elst atom) */
  138. unsigned int sample_to_chunk_sz;
  139. MOV_sample_to_chunk_tbl *sample_to_chunk;
  140. int sample_to_ctime_index;
  141. int sample_to_ctime_sample;
  142. unsigned int sample_size;
  143. unsigned int sample_count;
  144. long *sample_sizes;
  145. unsigned int keyframe_count;
  146. long *keyframes;
  147. int time_scale;
  148. int time_rate;
  149. long current_sample;
  150. MOV_esds_t esds;
  151. AVRational sample_size_v1;
  152. int dv_audio_container;
  153. } MOVStreamContext;
  154. typedef struct MOVContext {
  155. AVFormatContext *fc;
  156. int time_scale;
  157. int64_t duration; /* duration of the longest track */
  158. int found_moov; /* when both 'moov' and 'mdat' sections has been found */
  159. int found_mdat; /* we suppose we have enough data to read the file */
  160. int64_t mdat_offset;
  161. int total_streams;
  162. MOVStreamContext *streams[MAX_STREAMS];
  163. int ctab_size;
  164. MOV_ctab_t **ctab; /* color tables */
  165. const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
  166. /* NOTE: for recursion save to/ restore from local variable! */
  167. AVPaletteControl palette_control;
  168. MOV_mdat_atom_t *mdat_list;
  169. int mdat_count;
  170. DVDemuxContext *dv_demux;
  171. AVFormatContext *dv_fctx;
  172. int isom; /* 1 if file is ISO Media (mp4/3gp) */
  173. } MOVContext;
  174. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  175. /* those functions parse an atom */
  176. /* return code:
  177. 1: found what I wanted, exit
  178. 0: continue to parse next atom
  179. -1: error occured, exit
  180. */
  181. typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
  182. /* links atom IDs to parse functions */
  183. typedef struct MOVParseTableEntry {
  184. uint32_t type;
  185. mov_parse_function func;
  186. } MOVParseTableEntry;
  187. static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  188. {
  189. int64_t total_size = 0;
  190. MOV_atom_t a;
  191. int i;
  192. int err = 0;
  193. a.offset = atom.offset;
  194. if (atom.size < 0)
  195. atom.size = 0x7fffffffffffffffLL;
  196. while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
  197. a.size = atom.size;
  198. a.type=0L;
  199. if(atom.size >= 8) {
  200. a.size = get_be32(pb);
  201. a.type = get_le32(pb);
  202. }
  203. total_size += 8;
  204. a.offset += 8;
  205. dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n", a.type, (char*)&a.type, a.size, atom.size, total_size);
  206. if (a.size == 1) { /* 64 bit extended size */
  207. a.size = get_be64(pb) - 8;
  208. a.offset += 8;
  209. total_size += 8;
  210. }
  211. if (a.size == 0) {
  212. a.size = atom.size - total_size;
  213. if (a.size <= 8)
  214. break;
  215. }
  216. for (i = 0; c->parse_table[i].type != 0L
  217. && c->parse_table[i].type != a.type; i++)
  218. /* empty */;
  219. a.size -= 8;
  220. if(a.size < 0)
  221. break;
  222. if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
  223. url_fskip(pb, a.size);
  224. } else {
  225. offset_t start_pos = url_ftell(pb);
  226. int64_t left;
  227. err = (c->parse_table[i].func)(c, pb, a);
  228. left = a.size - url_ftell(pb) + start_pos;
  229. if (left > 0) /* skip garbage at atom end */
  230. url_fskip(pb, left);
  231. }
  232. a.offset += a.size;
  233. total_size += a.size;
  234. }
  235. if (!err && total_size < atom.size && atom.size < 0x7ffff) {
  236. url_fskip(pb, atom.size - total_size);
  237. }
  238. return err;
  239. }
  240. static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  241. {
  242. #if 1
  243. url_fskip(pb, atom.size); // for now
  244. #else
  245. VERY VERY BROKEN, NEVER execute this, needs rewrite
  246. unsigned int len;
  247. MOV_ctab_t *t;
  248. c->ctab = av_realloc(c->ctab, ++c->ctab_size);
  249. t = c->ctab[c->ctab_size];
  250. t->seed = get_be32(pb);
  251. t->flags = get_be16(pb);
  252. t->size = get_be16(pb) + 1;
  253. len = 2 * t->size * 4;
  254. if (len > 0) {
  255. t->clrs = av_malloc(len); // 16bit A R G B
  256. if (t->clrs)
  257. get_buffer(pb, t->clrs, len);
  258. }
  259. #endif
  260. return 0;
  261. }
  262. static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  263. {
  264. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  265. uint32_t type;
  266. uint32_t ctype;
  267. get_byte(pb); /* version */
  268. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  269. /* component type */
  270. ctype = get_le32(pb);
  271. type = get_le32(pb); /* component subtype */
  272. dprintf(c->fc, "ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
  273. dprintf(c->fc, "stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
  274. if(!ctype)
  275. c->isom = 1;
  276. if(type == MKTAG('v', 'i', 'd', 'e'))
  277. st->codec->codec_type = CODEC_TYPE_VIDEO;
  278. else if(type == MKTAG('s', 'o', 'u', 'n'))
  279. st->codec->codec_type = CODEC_TYPE_AUDIO;
  280. else if(type == MKTAG('m', '1', 'a', ' '))
  281. st->codec->codec_id = CODEC_ID_MP2;
  282. else if(type == MKTAG('s', 'u', 'b', 'p')) {
  283. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  284. st->codec->codec_id = CODEC_ID_DVD_SUBTITLE;
  285. }
  286. get_be32(pb); /* component manufacture */
  287. get_be32(pb); /* component flags */
  288. get_be32(pb); /* component flags mask */
  289. if(atom.size <= 24)
  290. return 0; /* nothing left to read */
  291. url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
  292. return 0;
  293. }
  294. static int mov_mp4_read_descr_len(ByteIOContext *pb)
  295. {
  296. int len = 0;
  297. int count = 4;
  298. while (count--) {
  299. int c = get_byte(pb);
  300. len = (len << 7) | (c & 0x7f);
  301. if (!(c & 0x80))
  302. break;
  303. }
  304. return len;
  305. }
  306. static int mov_mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag)
  307. {
  308. int len;
  309. *tag = get_byte(pb);
  310. len = mov_mp4_read_descr_len(pb);
  311. dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
  312. return len;
  313. }
  314. static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  315. {
  316. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  317. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  318. int tag, len;
  319. /* Well, broken but suffisant for some MP4 streams */
  320. get_be32(pb); /* version + flags */
  321. len = mov_mp4_read_descr(c, pb, &tag);
  322. if (tag == MP4ESDescrTag) {
  323. get_be16(pb); /* ID */
  324. get_byte(pb); /* priority */
  325. } else
  326. get_be16(pb); /* ID */
  327. len = mov_mp4_read_descr(c, pb, &tag);
  328. if (tag == MP4DecConfigDescrTag) {
  329. sc->esds.object_type_id = get_byte(pb);
  330. sc->esds.stream_type = get_byte(pb);
  331. sc->esds.buffer_size_db = get_be24(pb);
  332. sc->esds.max_bitrate = get_be32(pb);
  333. sc->esds.avg_bitrate = get_be32(pb);
  334. st->codec->codec_id= codec_get_id(ff_mov_obj_type, sc->esds.object_type_id);
  335. dprintf(c->fc, "esds object type id %d\n", sc->esds.object_type_id);
  336. len = mov_mp4_read_descr(c, pb, &tag);
  337. if (tag == MP4DecSpecificDescrTag) {
  338. dprintf(c->fc, "Specific MPEG4 header len=%d\n", len);
  339. st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
  340. if (st->codec->extradata) {
  341. get_buffer(pb, st->codec->extradata, len);
  342. st->codec->extradata_size = len;
  343. /* from mplayer */
  344. if ((*st->codec->extradata >> 3) == 29) {
  345. st->codec->codec_id = CODEC_ID_MP3ON4;
  346. }
  347. }
  348. }
  349. }
  350. return 0;
  351. }
  352. /* this atom contains actual media data */
  353. static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  354. {
  355. if(atom.size == 0) /* wrong one (MP4) */
  356. return 0;
  357. c->mdat_list = av_realloc(c->mdat_list, (c->mdat_count + 1) * sizeof(*c->mdat_list));
  358. c->mdat_list[c->mdat_count].offset = atom.offset;
  359. c->mdat_list[c->mdat_count].size = atom.size;
  360. c->mdat_count++;
  361. c->found_mdat=1;
  362. c->mdat_offset = atom.offset;
  363. if(c->found_moov)
  364. return 1; /* found both, just go */
  365. url_fskip(pb, atom.size);
  366. return 0; /* now go for moov */
  367. }
  368. static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  369. {
  370. uint32_t type = get_le32(pb);
  371. if (type != MKTAG('q','t',' ',' '))
  372. c->isom = 1;
  373. av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
  374. get_be32(pb); /* minor version */
  375. url_fskip(pb, atom.size - 8);
  376. return 0;
  377. }
  378. /* this atom should contain all header atoms */
  379. static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  380. {
  381. int err;
  382. err = mov_read_default(c, pb, atom);
  383. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  384. /* so we don't parse the whole file if over a network */
  385. c->found_moov=1;
  386. if(c->found_mdat)
  387. return 1; /* found both, just go */
  388. return 0; /* now go for mdat */
  389. }
  390. static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  391. {
  392. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  393. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  394. int version = get_byte(pb);
  395. int lang;
  396. if (version > 1)
  397. return 1; /* unsupported */
  398. get_byte(pb); get_byte(pb);
  399. get_byte(pb); /* flags */
  400. if (version == 1) {
  401. get_be64(pb);
  402. get_be64(pb);
  403. } else {
  404. get_be32(pb); /* creation time */
  405. get_be32(pb); /* modification time */
  406. }
  407. sc->time_scale = get_be32(pb);
  408. st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
  409. lang = get_be16(pb); /* language */
  410. ff_mov_lang_to_iso639(lang, st->language);
  411. get_be16(pb); /* quality */
  412. return 0;
  413. }
  414. static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  415. {
  416. int version = get_byte(pb); /* version */
  417. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  418. if (version == 1) {
  419. get_be64(pb);
  420. get_be64(pb);
  421. } else {
  422. get_be32(pb); /* creation time */
  423. get_be32(pb); /* modification time */
  424. }
  425. c->time_scale = get_be32(pb); /* time scale */
  426. #ifdef DEBUG
  427. av_log(NULL, AV_LOG_DEBUG, "time scale = %i\n", c->time_scale);
  428. #endif
  429. c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
  430. get_be32(pb); /* preferred scale */
  431. get_be16(pb); /* preferred volume */
  432. url_fskip(pb, 10); /* reserved */
  433. url_fskip(pb, 36); /* display matrix */
  434. get_be32(pb); /* preview time */
  435. get_be32(pb); /* preview duration */
  436. get_be32(pb); /* poster time */
  437. get_be32(pb); /* selection time */
  438. get_be32(pb); /* selection duration */
  439. get_be32(pb); /* current time */
  440. get_be32(pb); /* next track ID */
  441. return 0;
  442. }
  443. static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  444. {
  445. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  446. if((uint64_t)atom.size > (1<<30))
  447. return -1;
  448. // currently SVQ3 decoder expect full STSD header - so let's fake it
  449. // this should be fixed and just SMI header should be passed
  450. av_free(st->codec->extradata);
  451. st->codec->extradata_size = 0x5a + atom.size;
  452. st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  453. if (st->codec->extradata) {
  454. memcpy(st->codec->extradata, "SVQ3", 4); // fake
  455. get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
  456. dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
  457. } else
  458. url_fskip(pb, atom.size);
  459. return 0;
  460. }
  461. static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  462. {
  463. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  464. int little_endian = get_be16(pb);
  465. if (little_endian) {
  466. switch (st->codec->codec_id) {
  467. case CODEC_ID_PCM_S24BE:
  468. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  469. break;
  470. case CODEC_ID_PCM_S32BE:
  471. st->codec->codec_id = CODEC_ID_PCM_S32LE;
  472. break;
  473. default:
  474. break;
  475. }
  476. }
  477. return 0;
  478. }
  479. /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
  480. static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  481. {
  482. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  483. if((uint64_t)atom.size > (1<<30))
  484. return -1;
  485. av_free(st->codec->extradata);
  486. st->codec->extradata_size = atom.size + 8;
  487. st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  488. if (st->codec->extradata) {
  489. AV_WL32(st->codec->extradata + 4, atom.type);
  490. get_buffer(pb, st->codec->extradata + 8, atom.size);
  491. } else
  492. url_fskip(pb, atom.size);
  493. return 0;
  494. }
  495. static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  496. {
  497. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  498. if((uint64_t)atom.size > (1<<30))
  499. return -1;
  500. if (st->codec->codec_id == CODEC_ID_QDM2) {
  501. // pass all frma atom to codec, needed at least for QDM2
  502. av_free(st->codec->extradata);
  503. st->codec->extradata_size = atom.size;
  504. st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  505. if (st->codec->extradata) {
  506. get_buffer(pb, st->codec->extradata, atom.size);
  507. } else
  508. url_fskip(pb, atom.size);
  509. } else if (atom.size > 8) { /* to read frma, esds atoms */
  510. mov_read_default(c, pb, atom);
  511. } else
  512. url_fskip(pb, atom.size);
  513. return 0;
  514. }
  515. static int mov_read_avcC(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  516. {
  517. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  518. if((uint64_t)atom.size > (1<<30))
  519. return -1;
  520. av_free(st->codec->extradata);
  521. st->codec->extradata_size = atom.size;
  522. st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  523. if (st->codec->extradata) {
  524. get_buffer(pb, st->codec->extradata, atom.size);
  525. } else
  526. url_fskip(pb, atom.size);
  527. return 0;
  528. }
  529. static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  530. {
  531. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  532. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  533. unsigned int i, entries;
  534. get_byte(pb); /* version */
  535. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  536. entries = get_be32(pb);
  537. if(entries >= UINT_MAX/sizeof(int64_t))
  538. return -1;
  539. sc->chunk_count = entries;
  540. sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
  541. if (!sc->chunk_offsets)
  542. return -1;
  543. if (atom.type == MKTAG('s', 't', 'c', 'o')) {
  544. for(i=0; i<entries; i++) {
  545. sc->chunk_offsets[i] = get_be32(pb);
  546. }
  547. } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
  548. for(i=0; i<entries; i++) {
  549. sc->chunk_offsets[i] = get_be64(pb);
  550. }
  551. } else
  552. return -1;
  553. return 0;
  554. }
  555. static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  556. {
  557. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  558. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  559. int entries, frames_per_sample;
  560. uint32_t format;
  561. uint8_t codec_name[32];
  562. /* for palette traversal */
  563. int color_depth;
  564. int color_start;
  565. int color_count;
  566. int color_end;
  567. int color_index;
  568. int color_dec;
  569. int color_greyscale;
  570. unsigned char *color_table;
  571. int j;
  572. unsigned char r, g, b;
  573. get_byte(pb); /* version */
  574. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  575. entries = get_be32(pb);
  576. while(entries--) { //Parsing Sample description table
  577. enum CodecID id;
  578. MOV_atom_t a = { 0, 0, 0 };
  579. offset_t start_pos = url_ftell(pb);
  580. int size = get_be32(pb); /* size */
  581. format = get_le32(pb); /* data format */
  582. get_be32(pb); /* reserved */
  583. get_be16(pb); /* reserved */
  584. get_be16(pb); /* index */
  585. if (st->codec->codec_tag) {
  586. /* multiple fourcc, just skip for now */
  587. url_fskip(pb, size - (url_ftell(pb) - start_pos));
  588. continue;
  589. }
  590. st->codec->codec_tag = format;
  591. id = codec_get_id(codec_movaudio_tags, format);
  592. if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
  593. st->codec->codec_type = CODEC_TYPE_AUDIO;
  594. } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
  595. format && format != MKTAG('m', 'p', '4', 's')) { /* skip old asf mpeg4 tag */
  596. id = codec_get_id(codec_movvideo_tags, format);
  597. if (id <= 0)
  598. id = codec_get_id(codec_bmp_tags, format);
  599. if (id > 0)
  600. st->codec->codec_type = CODEC_TYPE_VIDEO;
  601. }
  602. dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n",
  603. size,
  604. (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, (format >> 24) & 0xff,
  605. st->codec->codec_type);
  606. if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
  607. st->codec->codec_id = id;
  608. get_be16(pb); /* version */
  609. get_be16(pb); /* revision level */
  610. get_be32(pb); /* vendor */
  611. get_be32(pb); /* temporal quality */
  612. get_be32(pb); /* spacial quality */
  613. st->codec->width = get_be16(pb); /* width */
  614. st->codec->height = get_be16(pb); /* height */
  615. get_be32(pb); /* horiz resolution */
  616. get_be32(pb); /* vert resolution */
  617. get_be32(pb); /* data size, always 0 */
  618. frames_per_sample = get_be16(pb); /* frames per samples */
  619. #ifdef DEBUG
  620. av_log(NULL, AV_LOG_DEBUG, "frames/samples = %d\n", frames_per_sample);
  621. #endif
  622. get_buffer(pb, codec_name, 32); /* codec name, pascal string (FIXME: true for mp4?) */
  623. if (codec_name[0] <= 31) {
  624. memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
  625. st->codec->codec_name[codec_name[0]] = 0;
  626. }
  627. st->codec->bits_per_sample = get_be16(pb); /* depth */
  628. st->codec->color_table_id = get_be16(pb); /* colortable id */
  629. /* figure out the palette situation */
  630. color_depth = st->codec->bits_per_sample & 0x1F;
  631. color_greyscale = st->codec->bits_per_sample & 0x20;
  632. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  633. if ((color_depth == 2) || (color_depth == 4) ||
  634. (color_depth == 8)) {
  635. if (color_greyscale) {
  636. /* compute the greyscale palette */
  637. color_count = 1 << color_depth;
  638. color_index = 255;
  639. color_dec = 256 / (color_count - 1);
  640. for (j = 0; j < color_count; j++) {
  641. r = g = b = color_index;
  642. c->palette_control.palette[j] =
  643. (r << 16) | (g << 8) | (b);
  644. color_index -= color_dec;
  645. if (color_index < 0)
  646. color_index = 0;
  647. }
  648. } else if (st->codec->color_table_id & 0x08) {
  649. /* if flag bit 3 is set, use the default palette */
  650. color_count = 1 << color_depth;
  651. if (color_depth == 2)
  652. color_table = ff_qt_default_palette_4;
  653. else if (color_depth == 4)
  654. color_table = ff_qt_default_palette_16;
  655. else
  656. color_table = ff_qt_default_palette_256;
  657. for (j = 0; j < color_count; j++) {
  658. r = color_table[j * 4 + 0];
  659. g = color_table[j * 4 + 1];
  660. b = color_table[j * 4 + 2];
  661. c->palette_control.palette[j] =
  662. (r << 16) | (g << 8) | (b);
  663. }
  664. } else {
  665. /* load the palette from the file */
  666. color_start = get_be32(pb);
  667. color_count = get_be16(pb);
  668. color_end = get_be16(pb);
  669. for (j = color_start; j <= color_end; j++) {
  670. /* each R, G, or B component is 16 bits;
  671. * only use the top 8 bits; skip alpha bytes
  672. * up front */
  673. get_byte(pb);
  674. get_byte(pb);
  675. r = get_byte(pb);
  676. get_byte(pb);
  677. g = get_byte(pb);
  678. get_byte(pb);
  679. b = get_byte(pb);
  680. get_byte(pb);
  681. c->palette_control.palette[j] =
  682. (r << 16) | (g << 8) | (b);
  683. }
  684. }
  685. st->codec->palctrl = &c->palette_control;
  686. st->codec->palctrl->palette_changed = 1;
  687. } else
  688. st->codec->palctrl = NULL;
  689. } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
  690. int bits_per_sample;
  691. uint16_t version = get_be16(pb);
  692. st->codec->codec_id = id;
  693. get_be16(pb); /* revision level */
  694. get_be32(pb); /* vendor */
  695. st->codec->channels = get_be16(pb); /* channel count */
  696. dprintf(c->fc, "audio channels %d\n", st->codec->channels);
  697. st->codec->bits_per_sample = get_be16(pb); /* sample size */
  698. /* do we need to force to 16 for AMR ? */
  699. /* handle specific s8 codec */
  700. get_be16(pb); /* compression id = 0*/
  701. get_be16(pb); /* packet size = 0 */
  702. st->codec->sample_rate = ((get_be32(pb) >> 16));
  703. switch (st->codec->codec_id) {
  704. case CODEC_ID_PCM_S8:
  705. case CODEC_ID_PCM_U8:
  706. if (st->codec->bits_per_sample == 16)
  707. st->codec->codec_id = CODEC_ID_PCM_S16BE;
  708. break;
  709. case CODEC_ID_PCM_S16LE:
  710. case CODEC_ID_PCM_S16BE:
  711. if (st->codec->bits_per_sample == 8)
  712. st->codec->codec_id = CODEC_ID_PCM_S8;
  713. else if (st->codec->bits_per_sample == 24)
  714. st->codec->codec_id = CODEC_ID_PCM_S24BE;
  715. break;
  716. default:
  717. break;
  718. }
  719. //Read QT version 1 fields. In version 0 theese dont exist
  720. dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
  721. if(!c->isom) {
  722. if(version==1) {
  723. sc->sample_size_v1.den = get_be32(pb); /* samples per packet */
  724. get_be32(pb); /* bytes per packet */
  725. sc->sample_size_v1.num = get_be32(pb); /* bytes per frame */
  726. get_be32(pb); /* bytes per sample */
  727. } else if(version==2) {
  728. get_be32(pb); /* sizeof struct only */
  729. st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
  730. st->codec->channels = get_be32(pb);
  731. get_be32(pb); /* always 0x7F000000 */
  732. get_be32(pb); /* bits per channel if sound is uncompressed */
  733. get_be32(pb); /* lcpm format specific flag */
  734. get_be32(pb); /* bytes per audio packet if constant */
  735. get_be32(pb); /* lpcm frames per audio packet if constant */
  736. }
  737. }
  738. bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
  739. if (bits_per_sample) {
  740. st->codec->bits_per_sample = bits_per_sample;
  741. sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
  742. }
  743. } else {
  744. /* other codec type, just skip (rtp, mp4s, tmcd ...) */
  745. url_fskip(pb, size - (url_ftell(pb) - start_pos));
  746. }
  747. /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
  748. a.size = size - (url_ftell(pb) - start_pos);
  749. if (a.size > 8)
  750. mov_read_default(c, pb, a);
  751. else if (a.size > 0)
  752. url_fskip(pb, a.size);
  753. }
  754. if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1) {
  755. st->codec->sample_rate= sc->time_scale;
  756. }
  757. /* special codec parameters handling */
  758. switch (st->codec->codec_id) {
  759. #ifdef CONFIG_H261_DECODER
  760. case CODEC_ID_H261:
  761. #endif
  762. #ifdef CONFIG_H263_DECODER
  763. case CODEC_ID_H263:
  764. #endif
  765. #ifdef CONFIG_MPEG4_DECODER
  766. case CODEC_ID_MPEG4:
  767. #endif
  768. st->codec->width= 0; /* let decoder init width/height */
  769. st->codec->height= 0;
  770. break;
  771. #ifdef CONFIG_LIBFAAD
  772. case CODEC_ID_AAC:
  773. #endif
  774. #ifdef CONFIG_VORBIS_DECODER
  775. case CODEC_ID_VORBIS:
  776. #endif
  777. case CODEC_ID_MP3ON4:
  778. st->codec->sample_rate= 0; /* let decoder init parameters properly */
  779. break;
  780. #ifdef CONFIG_DV_DEMUXER
  781. case CODEC_ID_DVAUDIO:
  782. c->dv_fctx = av_alloc_format_context();
  783. c->dv_demux = dv_init_demux(c->dv_fctx);
  784. if (!c->dv_demux) {
  785. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  786. return -1;
  787. }
  788. sc->dv_audio_container = 1;
  789. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  790. break;
  791. #endif
  792. /* no ifdef since parameters are always those */
  793. case CODEC_ID_AMR_WB:
  794. st->codec->sample_rate= 16000;
  795. st->codec->channels= 1; /* really needed */
  796. break;
  797. case CODEC_ID_AMR_NB:
  798. st->codec->sample_rate= 8000;
  799. st->codec->channels= 1; /* really needed */
  800. break;
  801. case CODEC_ID_MP2:
  802. st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
  803. st->need_parsing = 1;
  804. break;
  805. default:
  806. break;
  807. }
  808. return 0;
  809. }
  810. static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  811. {
  812. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  813. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  814. unsigned int i, entries;
  815. get_byte(pb); /* version */
  816. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  817. entries = get_be32(pb);
  818. if(entries >= UINT_MAX / sizeof(MOV_sample_to_chunk_tbl))
  819. return -1;
  820. #ifdef DEBUG
  821. av_log(NULL, AV_LOG_DEBUG, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  822. #endif
  823. sc->sample_to_chunk_sz = entries;
  824. sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
  825. if (!sc->sample_to_chunk)
  826. return -1;
  827. for(i=0; i<entries; i++) {
  828. sc->sample_to_chunk[i].first = get_be32(pb);
  829. sc->sample_to_chunk[i].count = get_be32(pb);
  830. sc->sample_to_chunk[i].id = get_be32(pb);
  831. }
  832. return 0;
  833. }
  834. static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  835. {
  836. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  837. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  838. unsigned int i, entries;
  839. get_byte(pb); /* version */
  840. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  841. entries = get_be32(pb);
  842. if(entries >= UINT_MAX / sizeof(long))
  843. return -1;
  844. sc->keyframe_count = entries;
  845. #ifdef DEBUG
  846. av_log(NULL, AV_LOG_DEBUG, "keyframe_count = %d\n", sc->keyframe_count);
  847. #endif
  848. sc->keyframes = av_malloc(entries * sizeof(long));
  849. if (!sc->keyframes)
  850. return -1;
  851. for(i=0; i<entries; i++) {
  852. sc->keyframes[i] = get_be32(pb);
  853. #ifdef DEBUG
  854. /* av_log(NULL, AV_LOG_DEBUG, "keyframes[]=%ld\n", sc->keyframes[i]); */
  855. #endif
  856. }
  857. return 0;
  858. }
  859. static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  860. {
  861. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  862. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  863. unsigned int i, entries, sample_size;
  864. get_byte(pb); /* version */
  865. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  866. sample_size = get_be32(pb);
  867. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  868. sc->sample_size = sample_size;
  869. entries = get_be32(pb);
  870. if(entries >= UINT_MAX / sizeof(long))
  871. return -1;
  872. sc->sample_count = entries;
  873. if (sample_size)
  874. return 0;
  875. #ifdef DEBUG
  876. av_log(NULL, AV_LOG_DEBUG, "sample_size = %d sample_count = %d\n", sc->sample_size, sc->sample_count);
  877. #endif
  878. sc->sample_sizes = av_malloc(entries * sizeof(long));
  879. if (!sc->sample_sizes)
  880. return -1;
  881. for(i=0; i<entries; i++) {
  882. sc->sample_sizes[i] = get_be32(pb);
  883. #ifdef DEBUG
  884. av_log(NULL, AV_LOG_DEBUG, "sample_sizes[]=%ld\n", sc->sample_sizes[i]);
  885. #endif
  886. }
  887. return 0;
  888. }
  889. static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  890. {
  891. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  892. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  893. unsigned int i, entries;
  894. int64_t duration=0;
  895. int64_t total_sample_count=0;
  896. get_byte(pb); /* version */
  897. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  898. entries = get_be32(pb);
  899. if(entries >= UINT_MAX / sizeof(Time2Sample))
  900. return -1;
  901. sc->stts_count = entries;
  902. sc->stts_data = av_malloc(entries * sizeof(Time2Sample));
  903. #ifdef DEBUG
  904. av_log(NULL, AV_LOG_DEBUG, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  905. #endif
  906. sc->time_rate=0;
  907. for(i=0; i<entries; i++) {
  908. int sample_duration;
  909. int sample_count;
  910. sample_count=get_be32(pb);
  911. sample_duration = get_be32(pb);
  912. sc->stts_data[i].count= sample_count;
  913. sc->stts_data[i].duration= sample_duration;
  914. sc->time_rate= ff_gcd(sc->time_rate, sample_duration);
  915. dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  916. duration+=(int64_t)sample_duration*sample_count;
  917. total_sample_count+=sample_count;
  918. }
  919. st->nb_frames= total_sample_count;
  920. if(duration)
  921. st->duration= duration;
  922. return 0;
  923. }
  924. static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  925. {
  926. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  927. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  928. unsigned int i, entries;
  929. get_byte(pb); /* version */
  930. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  931. entries = get_be32(pb);
  932. if(entries >= UINT_MAX / sizeof(Time2Sample))
  933. return -1;
  934. sc->ctts_count = entries;
  935. sc->ctts_data = av_malloc(entries * sizeof(Time2Sample));
  936. dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  937. for(i=0; i<entries; i++) {
  938. int count =get_be32(pb);
  939. int duration =get_be32(pb);
  940. if (duration < 0) {
  941. av_log(c->fc, AV_LOG_ERROR, "negative ctts, ignoring\n");
  942. sc->ctts_count = 0;
  943. url_fskip(pb, 8 * (entries - i - 1));
  944. break;
  945. }
  946. sc->ctts_data[i].count = count;
  947. sc->ctts_data[i].duration= duration;
  948. sc->time_rate= ff_gcd(sc->time_rate, duration);
  949. }
  950. return 0;
  951. }
  952. static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  953. {
  954. AVStream *st;
  955. MOVStreamContext *sc;
  956. st = av_new_stream(c->fc, c->fc->nb_streams);
  957. if (!st) return -2;
  958. sc = av_mallocz(sizeof(MOVStreamContext));
  959. if (!sc) {
  960. av_free(st);
  961. return -1;
  962. }
  963. st->priv_data = sc;
  964. st->codec->codec_type = CODEC_TYPE_DATA;
  965. st->start_time = 0; /* XXX: check */
  966. c->streams[c->fc->nb_streams-1] = sc;
  967. return mov_read_default(c, pb, atom);
  968. }
  969. static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  970. {
  971. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  972. int version = get_byte(pb);
  973. get_byte(pb); get_byte(pb);
  974. get_byte(pb); /* flags */
  975. /*
  976. MOV_TRACK_ENABLED 0x0001
  977. MOV_TRACK_IN_MOVIE 0x0002
  978. MOV_TRACK_IN_PREVIEW 0x0004
  979. MOV_TRACK_IN_POSTER 0x0008
  980. */
  981. if (version == 1) {
  982. get_be64(pb);
  983. get_be64(pb);
  984. } else {
  985. get_be32(pb); /* creation time */
  986. get_be32(pb); /* modification time */
  987. }
  988. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  989. get_be32(pb); /* reserved */
  990. st->start_time = 0; /* check */
  991. (version == 1) ? get_be64(pb) : get_be32(pb); /* highlevel (considering edits) duration in movie timebase */
  992. get_be32(pb); /* reserved */
  993. get_be32(pb); /* reserved */
  994. get_be16(pb); /* layer */
  995. get_be16(pb); /* alternate group */
  996. get_be16(pb); /* volume */
  997. get_be16(pb); /* reserved */
  998. url_fskip(pb, 36); /* display matrix */
  999. /* those are fixed-point */
  1000. get_be32(pb); /* track width */
  1001. get_be32(pb); /* track height */
  1002. return 0;
  1003. }
  1004. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1005. /* like the files created with Adobe Premiere 5.0, for samples see */
  1006. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1007. static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1008. {
  1009. int err;
  1010. if (atom.size < 8)
  1011. return 0; /* continue */
  1012. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1013. url_fskip(pb, atom.size - 4);
  1014. return 0;
  1015. }
  1016. atom.type = get_le32(pb);
  1017. atom.offset += 8;
  1018. atom.size -= 8;
  1019. if (atom.type != MKTAG('m', 'd', 'a', 't')) {
  1020. url_fskip(pb, atom.size);
  1021. return 0;
  1022. }
  1023. err = mov_read_mdat(c, pb, atom);
  1024. return err;
  1025. }
  1026. static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1027. {
  1028. #ifdef CONFIG_ZLIB
  1029. ByteIOContext ctx;
  1030. uint8_t *cmov_data;
  1031. uint8_t *moov_data; /* uncompressed data */
  1032. long cmov_len, moov_len;
  1033. int ret;
  1034. get_be32(pb); /* dcom atom */
  1035. if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
  1036. return -1;
  1037. if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
  1038. av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !");
  1039. return -1;
  1040. }
  1041. get_be32(pb); /* cmvd atom */
  1042. if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
  1043. return -1;
  1044. moov_len = get_be32(pb); /* uncompressed size */
  1045. cmov_len = atom.size - 6 * 4;
  1046. cmov_data = av_malloc(cmov_len);
  1047. if (!cmov_data)
  1048. return -1;
  1049. moov_data = av_malloc(moov_len);
  1050. if (!moov_data) {
  1051. av_free(cmov_data);
  1052. return -1;
  1053. }
  1054. get_buffer(pb, cmov_data, cmov_len);
  1055. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1056. return -1;
  1057. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  1058. return -1;
  1059. atom.type = MKTAG( 'm', 'o', 'o', 'v' );
  1060. atom.offset = 0;
  1061. atom.size = moov_len;
  1062. #ifdef DEBUG
  1063. // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
  1064. #endif
  1065. ret = mov_read_default(c, &ctx, atom);
  1066. av_free(moov_data);
  1067. av_free(cmov_data);
  1068. return ret;
  1069. #else
  1070. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  1071. return -1;
  1072. #endif
  1073. }
  1074. /* edit list atom */
  1075. static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1076. {
  1077. int i, edit_count;
  1078. get_byte(pb); /* version */
  1079. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1080. edit_count= c->streams[c->fc->nb_streams-1]->edit_count = get_be32(pb); /* entries */
  1081. for(i=0; i<edit_count; i++){
  1082. get_be32(pb); /* Track duration */
  1083. get_be32(pb); /* Media time */
  1084. get_be32(pb); /* Media rate */
  1085. }
  1086. dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, c->streams[c->fc->nb_streams-1]->edit_count);
  1087. return 0;
  1088. }
  1089. static const MOVParseTableEntry mov_default_parse_table[] = {
  1090. /* mp4 atoms */
  1091. { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
  1092. { MKTAG( 'c', 't', 't', 's' ), mov_read_ctts }, /* composition time to sample */
  1093. { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
  1094. { MKTAG( 'e', 'l', 's', 't' ), mov_read_elst },
  1095. { MKTAG( 'e', 'n', 'd', 'a' ), mov_read_enda },
  1096. { MKTAG( 'f', 'i', 'e', 'l' ), mov_read_extradata },
  1097. { MKTAG( 'f', 't', 'y', 'p' ), mov_read_ftyp },
  1098. { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
  1099. { MKTAG( 'j', 'p', '2', 'h' ), mov_read_extradata },
  1100. { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
  1101. { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
  1102. { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
  1103. { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
  1104. { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
  1105. { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
  1106. { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorenson extension ??? */
  1107. { MKTAG( 'a', 'l', 'a', 'c' ), mov_read_extradata }, /* alac specific atom */
  1108. { MKTAG( 'a', 'v', 'c', 'C' ), mov_read_avcC },
  1109. { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
  1110. { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
  1111. { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
  1112. { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
  1113. { MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */
  1114. { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
  1115. { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
  1116. { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
  1117. { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
  1118. { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_wave },
  1119. { MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab },
  1120. { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
  1121. { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
  1122. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
  1123. { 0L, NULL }
  1124. };
  1125. static void mov_free_stream_context(MOVStreamContext *sc)
  1126. {
  1127. if(sc) {
  1128. av_freep(&sc->ctts_data);
  1129. av_freep(&sc);
  1130. }
  1131. }
  1132. /* XXX: is it sufficient ? */
  1133. static int mov_probe(AVProbeData *p)
  1134. {
  1135. unsigned int offset;
  1136. uint32_t tag;
  1137. int score = 0;
  1138. /* check file header */
  1139. if (p->buf_size <= 12)
  1140. return 0;
  1141. offset = 0;
  1142. for(;;) {
  1143. /* ignore invalid offset */
  1144. if ((offset + 8) > (unsigned int)p->buf_size)
  1145. return score;
  1146. tag = AV_RL32(p->buf + offset + 4);
  1147. switch(tag) {
  1148. /* check for obvious tags */
  1149. case MKTAG( 'j', 'P', ' ', ' ' ): /* jpeg 2000 signature */
  1150. case MKTAG( 'm', 'o', 'o', 'v' ):
  1151. case MKTAG( 'm', 'd', 'a', 't' ):
  1152. case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
  1153. case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
  1154. return AVPROBE_SCORE_MAX;
  1155. /* those are more common words, so rate then a bit less */
  1156. case MKTAG( 'w', 'i', 'd', 'e' ):
  1157. case MKTAG( 'f', 'r', 'e', 'e' ):
  1158. case MKTAG( 'j', 'u', 'n', 'k' ):
  1159. case MKTAG( 'p', 'i', 'c', 't' ):
  1160. return AVPROBE_SCORE_MAX - 5;
  1161. case MKTAG( 'f', 't', 'y', 'p' ):
  1162. case MKTAG( 's', 'k', 'i', 'p' ):
  1163. case MKTAG( 'u', 'u', 'i', 'd' ):
  1164. offset = AV_RB32(p->buf+offset) + offset;
  1165. /* if we only find those cause probedata is too small at least rate them */
  1166. score = AVPROBE_SCORE_MAX - 50;
  1167. break;
  1168. default:
  1169. /* unrecognized tag */
  1170. return score;
  1171. }
  1172. }
  1173. return score;
  1174. }
  1175. static void mov_build_index(MOVContext *mov, AVStream *st)
  1176. {
  1177. MOVStreamContext *sc = st->priv_data;
  1178. offset_t current_offset;
  1179. int64_t current_dts = 0;
  1180. unsigned int stts_index = 0;
  1181. unsigned int stsc_index = 0;
  1182. unsigned int stss_index = 0;
  1183. unsigned int i, j, k;
  1184. if (sc->sample_sizes || st->codec->codec_type == CODEC_TYPE_VIDEO || sc->dv_audio_container) {
  1185. unsigned int current_sample = 0;
  1186. unsigned int stts_sample = 0;
  1187. unsigned int keyframe, sample_size;
  1188. unsigned int distance = 0;
  1189. st->nb_frames = sc->sample_count;
  1190. for (i = 0; i < sc->chunk_count; i++) {
  1191. current_offset = sc->chunk_offsets[i];
  1192. if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
  1193. stsc_index++;
  1194. for (j = 0; j < sc->sample_to_chunk[stsc_index].count; j++) {
  1195. if (current_sample >= sc->sample_count) {
  1196. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  1197. goto out;
  1198. }
  1199. keyframe = !sc->keyframe_count || current_sample + 1 == sc->keyframes[stss_index];
  1200. if (keyframe) {
  1201. distance = 0;
  1202. if (stss_index + 1 < sc->keyframe_count)
  1203. stss_index++;
  1204. }
  1205. sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
  1206. dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", size %d, distance %d, keyframe %d\n",
  1207. st->index, current_sample, current_offset, current_dts, sample_size, distance, keyframe);
  1208. av_add_index_entry(st, current_offset, current_dts, sample_size, distance, keyframe ? AVINDEX_KEYFRAME : 0);
  1209. current_offset += sample_size;
  1210. assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
  1211. current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
  1212. distance++;
  1213. stts_sample++;
  1214. current_sample++;
  1215. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  1216. stts_sample = 0;
  1217. stts_index++;
  1218. }
  1219. }
  1220. }
  1221. } else { /* read whole chunk */
  1222. unsigned int chunk_samples, chunk_size, chunk_duration;
  1223. for (i = 0; i < sc->chunk_count; i++) {
  1224. current_offset = sc->chunk_offsets[i];
  1225. if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
  1226. stsc_index++;
  1227. chunk_samples = sc->sample_to_chunk[stsc_index].count;
  1228. /* get chunk size */
  1229. if (sc->sample_size > 1 || st->codec->codec_id == CODEC_ID_PCM_U8 || st->codec->codec_id == CODEC_ID_PCM_S8)
  1230. chunk_size = chunk_samples * sc->sample_size;
  1231. else if (sc->sample_size_v1.den > 0 && (chunk_samples * sc->sample_size_v1.num % sc->sample_size_v1.den == 0))
  1232. chunk_size = chunk_samples * sc->sample_size_v1.num / sc->sample_size_v1.den;
  1233. else { /* workaround to find nearest next chunk offset */
  1234. chunk_size = INT_MAX;
  1235. for (j = 0; j < mov->total_streams; j++) {
  1236. MOVStreamContext *msc = mov->streams[j];
  1237. for (k = msc->next_chunk; k < msc->chunk_count; k++) {
  1238. if (msc->chunk_offsets[k] > current_offset && msc->chunk_offsets[k] - current_offset < chunk_size) {
  1239. chunk_size = msc->chunk_offsets[k] - current_offset;
  1240. msc->next_chunk = k;
  1241. break;
  1242. }
  1243. }
  1244. }
  1245. /* check for last chunk */
  1246. if (chunk_size == INT_MAX)
  1247. for (j = 0; j < mov->mdat_count; j++) {
  1248. dprintf(mov->fc, "mdat %d, offset %"PRIx64", size %"PRId64", current offset %"PRIx64"\n",
  1249. j, mov->mdat_list[j].offset, mov->mdat_list[j].size, current_offset);
  1250. if (mov->mdat_list[j].offset <= current_offset && mov->mdat_list[j].offset + mov->mdat_list[j].size > current_offset)
  1251. chunk_size = mov->mdat_list[j].offset + mov->mdat_list[j].size - current_offset;
  1252. }
  1253. assert(chunk_size != INT_MAX);
  1254. for (j = 0; j < mov->total_streams; j++) {
  1255. mov->streams[j]->next_chunk = 0;
  1256. }
  1257. }
  1258. av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
  1259. /* get chunk duration */
  1260. chunk_duration = 0;
  1261. while (chunk_samples > 0) {
  1262. if (chunk_samples < sc->stts_data[stts_index].count) {
  1263. chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
  1264. sc->stts_data[stts_index].count -= chunk_samples;
  1265. break;
  1266. } else {
  1267. chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
  1268. chunk_samples -= sc->stts_data[stts_index].count;
  1269. if (stts_index + 1 < sc->stts_count) {
  1270. stts_index++;
  1271. }
  1272. }
  1273. }
  1274. dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", size %d, duration %d\n",
  1275. st->index, i, current_offset, current_dts, chunk_size, chunk_duration);
  1276. assert(chunk_duration % sc->time_rate == 0);
  1277. current_dts += chunk_duration / sc->time_rate;
  1278. }
  1279. }
  1280. out:
  1281. /* adjust sample count to avindex entries */
  1282. sc->sample_count = st->nb_index_entries;
  1283. }
  1284. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1285. {
  1286. MOVContext *mov = (MOVContext *) s->priv_data;
  1287. ByteIOContext *pb = &s->pb;
  1288. int i, err;
  1289. MOV_atom_t atom = { 0, 0, 0 };
  1290. mov->fc = s;
  1291. mov->parse_table = mov_default_parse_table;
  1292. if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  1293. atom.size = url_fsize(pb);
  1294. else
  1295. atom.size = 0x7FFFFFFFFFFFFFFFLL;
  1296. /* check MOV header */
  1297. err = mov_read_default(mov, pb, atom);
  1298. if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
  1299. av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%"PRId64"\n",
  1300. err, mov->found_moov, mov->found_mdat, url_ftell(pb));
  1301. return -1;
  1302. }
  1303. dprintf(mov->fc, "on_parse_exit_offset=%d\n", (int) url_ftell(pb));
  1304. /* some cleanup : make sure we are on the mdat atom */
  1305. if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
  1306. url_fseek(pb, mov->mdat_offset, SEEK_SET);
  1307. mov->total_streams = s->nb_streams;
  1308. for(i=0; i<mov->total_streams; i++) {
  1309. MOVStreamContext *sc = mov->streams[i];
  1310. /* sanity checks */
  1311. if(!sc->stts_count || !sc->chunk_count || !sc->sample_to_chunk_sz ||
  1312. (!sc->sample_size && !sc->sample_count)){
  1313. av_log(s, AV_LOG_ERROR, "missing mandatory atoms, broken header\n");
  1314. return -1;
  1315. }
  1316. if(!sc->time_rate)
  1317. sc->time_rate=1;
  1318. if(!sc->time_scale)
  1319. sc->time_scale= mov->time_scale;
  1320. av_set_pts_info(s->streams[i], 64, sc->time_rate, sc->time_scale);
  1321. if(s->streams[i]->duration != AV_NOPTS_VALUE){
  1322. assert(s->streams[i]->duration % sc->time_rate == 0);
  1323. s->streams[i]->duration /= sc->time_rate;
  1324. }
  1325. sc->ffindex = i;
  1326. mov_build_index(mov, s->streams[i]);
  1327. }
  1328. for(i=0; i<mov->total_streams; i++) {
  1329. /* dont need those anymore */
  1330. av_freep(&mov->streams[i]->chunk_offsets);
  1331. av_freep(&mov->streams[i]->sample_to_chunk);
  1332. av_freep(&mov->streams[i]->sample_sizes);
  1333. av_freep(&mov->streams[i]->keyframes);
  1334. av_freep(&mov->streams[i]->stts_data);
  1335. }
  1336. av_freep(&mov->mdat_list);
  1337. return 0;
  1338. }
  1339. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  1340. {
  1341. MOVContext *mov = s->priv_data;
  1342. MOVStreamContext *sc = 0;
  1343. AVIndexEntry *sample = 0;
  1344. int64_t best_dts = INT64_MAX;
  1345. int i;
  1346. for (i = 0; i < mov->total_streams; i++) {
  1347. MOVStreamContext *msc = mov->streams[i];
  1348. if (s->streams[i]->discard != AVDISCARD_ALL && msc->current_sample < msc->sample_count) {
  1349. AVIndexEntry *current_sample = &s->streams[i]->index_entries[msc->current_sample];
  1350. int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate, AV_TIME_BASE, msc->time_scale);
  1351. dprintf(s, "stream %d, sample %ld, dts %"PRId64"\n", i, msc->current_sample, dts);
  1352. if (dts < best_dts) {
  1353. sample = current_sample;
  1354. best_dts = dts;
  1355. sc = msc;
  1356. }
  1357. }
  1358. }
  1359. if (!sample)
  1360. return -1;
  1361. /* must be done just before reading, to avoid infinite loop on sample */
  1362. sc->current_sample++;
  1363. if (sample->pos >= url_fsize(&s->pb)) {
  1364. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n", sc->ffindex, sample->pos);
  1365. return -1;
  1366. }
  1367. #ifdef CONFIG_DV_DEMUXER
  1368. if (sc->dv_audio_container) {
  1369. dv_get_packet(mov->dv_demux, pkt);
  1370. dprintf(s, "dv audio pkt size %d\n", pkt->size);
  1371. } else {
  1372. #endif
  1373. url_fseek(&s->pb, sample->pos, SEEK_SET);
  1374. av_get_packet(&s->pb, pkt, sample->size);
  1375. #ifdef CONFIG_DV_DEMUXER
  1376. if (mov->dv_demux) {
  1377. void *pkt_destruct_func = pkt->destruct;
  1378. dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
  1379. pkt->destruct = pkt_destruct_func;
  1380. }
  1381. }
  1382. #endif
  1383. pkt->stream_index = sc->ffindex;
  1384. pkt->dts = sample->timestamp;
  1385. if (sc->ctts_data) {
  1386. assert(sc->ctts_data[sc->sample_to_ctime_index].duration % sc->time_rate == 0);
  1387. pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate;
  1388. /* update ctts context */
  1389. sc->sample_to_ctime_sample++;
  1390. if (sc->sample_to_ctime_index < sc->ctts_count && sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) {
  1391. sc->sample_to_ctime_index++;
  1392. sc->sample_to_ctime_sample = 0;
  1393. }
  1394. } else {
  1395. pkt->pts = pkt->dts;
  1396. }
  1397. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
  1398. pkt->pos = sample->pos;
  1399. dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n", pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
  1400. return 0;
  1401. }
  1402. static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
  1403. {
  1404. MOVStreamContext *sc = st->priv_data;
  1405. int sample, time_sample;
  1406. int i;
  1407. sample = av_index_search_timestamp(st, timestamp, flags);
  1408. dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  1409. if (sample < 0) /* not sure what to do */
  1410. return -1;
  1411. sc->current_sample = sample;
  1412. dprintf(st->codec, "stream %d, found sample %ld\n", st->index, sc->current_sample);
  1413. /* adjust ctts index */
  1414. if (sc->ctts_data) {
  1415. time_sample = 0;
  1416. for (i = 0; i < sc->ctts_count; i++) {
  1417. time_sample += sc->ctts_data[i].count;
  1418. if (time_sample >= sc->current_sample) {
  1419. sc->sample_to_ctime_index = i;
  1420. sc->sample_to_ctime_sample = time_sample - sc->current_sample;
  1421. break;
  1422. }
  1423. }
  1424. }
  1425. return sample;
  1426. }
  1427. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  1428. {
  1429. AVStream *st;
  1430. int64_t seek_timestamp, timestamp;
  1431. int sample;
  1432. int i;
  1433. if (stream_index >= s->nb_streams)
  1434. return -1;
  1435. st = s->streams[stream_index];
  1436. sample = mov_seek_stream(st, sample_time, flags);
  1437. if (sample < 0)
  1438. return -1;
  1439. /* adjust seek timestamp to found sample timestamp */
  1440. seek_timestamp = st->index_entries[sample].timestamp;
  1441. for (i = 0; i < s->nb_streams; i++) {
  1442. st = s->streams[i];
  1443. if (stream_index == i || st->discard == AVDISCARD_ALL)
  1444. continue;
  1445. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  1446. mov_seek_stream(st, timestamp, flags);
  1447. }
  1448. return 0;
  1449. }
  1450. static int mov_read_close(AVFormatContext *s)
  1451. {
  1452. int i;
  1453. MOVContext *mov = (MOVContext *) s->priv_data;
  1454. for(i=0; i<mov->total_streams; i++)
  1455. mov_free_stream_context(mov->streams[i]);
  1456. /* free color tabs */
  1457. for(i=0; i<mov->ctab_size; i++)
  1458. av_freep(&mov->ctab[i]);
  1459. if(mov->dv_demux){
  1460. for(i=0; i<mov->dv_fctx->nb_streams; i++){
  1461. av_freep(&mov->dv_fctx->streams[i]->codec);
  1462. av_freep(&mov->dv_fctx->streams[i]);
  1463. }
  1464. av_freep(&mov->dv_fctx);
  1465. av_freep(&mov->dv_demux);
  1466. }
  1467. av_freep(&mov->ctab);
  1468. return 0;
  1469. }
  1470. AVInputFormat mov_demuxer = {
  1471. "mov,mp4,m4a,3gp,3g2,mj2",
  1472. "QuickTime/MPEG4/Motion JPEG 2000 format",
  1473. sizeof(MOVContext),
  1474. mov_probe,
  1475. mov_read_header,
  1476. mov_read_packet,
  1477. mov_read_close,
  1478. mov_read_seek,
  1479. };