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.

1696 lines
57KB

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