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.

2057 lines
71KB

  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. #include "libavcodec/mpeg4audio.h"
  28. #include "libavcodec/mpegaudiodata.h"
  29. #ifdef CONFIG_ZLIB
  30. #include <zlib.h>
  31. #endif
  32. /*
  33. * First version by Francois Revol revol@free.fr
  34. * Seek function by Gael Chardon gael.dev@4now.net
  35. *
  36. * Features and limitations:
  37. * - reads most of the QT files I have (at least the structure),
  38. * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
  39. * - the code is quite ugly... maybe I won't do it recursive next time :-)
  40. *
  41. * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
  42. * when coding this :) (it's a writer anyway)
  43. *
  44. * Reference documents:
  45. * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
  46. * Apple:
  47. * http://developer.apple.com/documentation/QuickTime/QTFF/
  48. * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
  49. * QuickTime is a trademark of Apple (AFAIK :))
  50. */
  51. #include "qtpalette.h"
  52. #undef NDEBUG
  53. #include <assert.h>
  54. /* the QuickTime file format is quite convoluted...
  55. * it has lots of index tables, each indexing something in another one...
  56. * Here we just use what is needed to read the chunks
  57. */
  58. typedef struct {
  59. int first;
  60. int count;
  61. int id;
  62. } MOVStsc;
  63. typedef struct {
  64. uint32_t type;
  65. char *path;
  66. } MOVDref;
  67. typedef struct {
  68. uint32_t type;
  69. int64_t offset;
  70. int64_t size; /* total size (excluding the size and type fields) */
  71. } MOVAtom;
  72. struct MOVParseTableEntry;
  73. typedef struct {
  74. unsigned track_id;
  75. uint64_t base_data_offset;
  76. uint64_t moof_offset;
  77. unsigned stsd_id;
  78. unsigned duration;
  79. unsigned size;
  80. unsigned flags;
  81. } MOVFragment;
  82. typedef struct {
  83. unsigned track_id;
  84. unsigned stsd_id;
  85. unsigned duration;
  86. unsigned size;
  87. unsigned flags;
  88. } MOVTrackExt;
  89. typedef struct MOVStreamContext {
  90. ByteIOContext *pb;
  91. int ffindex; /* the ffmpeg stream id */
  92. int next_chunk;
  93. unsigned int chunk_count;
  94. int64_t *chunk_offsets;
  95. unsigned int stts_count;
  96. MOVStts *stts_data;
  97. unsigned int ctts_count;
  98. MOVStts *ctts_data;
  99. unsigned int edit_count; /* number of 'edit' (elst atom) */
  100. unsigned int sample_to_chunk_sz;
  101. MOVStsc *sample_to_chunk;
  102. int sample_to_ctime_index;
  103. int sample_to_ctime_sample;
  104. unsigned int sample_size;
  105. unsigned int sample_count;
  106. int *sample_sizes;
  107. unsigned int keyframe_count;
  108. int *keyframes;
  109. int time_scale;
  110. int time_rate;
  111. int current_sample;
  112. unsigned int bytes_per_frame;
  113. unsigned int samples_per_frame;
  114. int dv_audio_container;
  115. int pseudo_stream_id; ///< -1 means demux all ids
  116. int16_t audio_cid; ///< stsd audio compression id
  117. unsigned drefs_count;
  118. MOVDref *drefs;
  119. int dref_id;
  120. int wrong_dts; ///< dts are wrong due to negative ctts
  121. } MOVStreamContext;
  122. typedef struct MOVContext {
  123. AVFormatContext *fc;
  124. int time_scale;
  125. int64_t duration; /* duration of the longest track */
  126. int found_moov; /* when both 'moov' and 'mdat' sections has been found */
  127. int found_mdat; /* we suppose we have enough data to read the file */
  128. AVPaletteControl palette_control;
  129. DVDemuxContext *dv_demux;
  130. AVFormatContext *dv_fctx;
  131. int isom; /* 1 if file is ISO Media (mp4/3gp) */
  132. MOVFragment fragment; ///< current fragment in moof atom
  133. MOVTrackExt *trex_data;
  134. unsigned trex_count;
  135. int itunes_metadata; ///< metadata are itunes style
  136. } MOVContext;
  137. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  138. /* those functions parse an atom */
  139. /* return code:
  140. 0: continue to parse next atom
  141. <0: error occurred, exit
  142. */
  143. /* links atom IDs to parse functions */
  144. typedef struct MOVParseTableEntry {
  145. uint32_t type;
  146. int (*parse)(MOVContext *ctx, ByteIOContext *pb, MOVAtom atom);
  147. } MOVParseTableEntry;
  148. static const MOVParseTableEntry mov_default_parse_table[];
  149. static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  150. {
  151. int64_t total_size = 0;
  152. MOVAtom a;
  153. int i;
  154. int err = 0;
  155. a.offset = atom.offset;
  156. if (atom.size < 0)
  157. atom.size = INT64_MAX;
  158. while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
  159. a.size = atom.size;
  160. a.type=0;
  161. if(atom.size >= 8) {
  162. a.size = get_be32(pb);
  163. a.type = get_le32(pb);
  164. }
  165. total_size += 8;
  166. a.offset += 8;
  167. dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n",
  168. a.type, (char*)&a.type, a.size, atom.size, total_size);
  169. if (a.size == 1) { /* 64 bit extended size */
  170. a.size = get_be64(pb) - 8;
  171. a.offset += 8;
  172. total_size += 8;
  173. }
  174. if (a.size == 0) {
  175. a.size = atom.size - total_size;
  176. if (a.size <= 8)
  177. break;
  178. }
  179. a.size -= 8;
  180. if(a.size < 0)
  181. break;
  182. a.size = FFMIN(a.size, atom.size - total_size);
  183. for (i = 0; mov_default_parse_table[i].type != 0
  184. && mov_default_parse_table[i].type != a.type; i++)
  185. /* empty */;
  186. if (mov_default_parse_table[i].type == 0) { /* skip leaf atoms data */
  187. url_fskip(pb, a.size);
  188. } else {
  189. int64_t start_pos = url_ftell(pb);
  190. int64_t left;
  191. err = mov_default_parse_table[i].parse(c, pb, a);
  192. if (url_is_streamed(pb) && c->found_moov && c->found_mdat)
  193. break;
  194. left = a.size - url_ftell(pb) + start_pos;
  195. if (left > 0) /* skip garbage at atom end */
  196. url_fskip(pb, left);
  197. }
  198. a.offset += a.size;
  199. total_size += a.size;
  200. }
  201. if (!err && total_size < atom.size && atom.size < 0x7ffff)
  202. url_fskip(pb, atom.size - total_size);
  203. return err;
  204. }
  205. static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  206. {
  207. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  208. MOVStreamContext *sc = st->priv_data;
  209. int entries, i, j;
  210. get_be32(pb); // version + flags
  211. entries = get_be32(pb);
  212. if (entries >= UINT_MAX / sizeof(*sc->drefs))
  213. return -1;
  214. sc->drefs_count = entries;
  215. sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
  216. for (i = 0; i < sc->drefs_count; i++) {
  217. MOVDref *dref = &sc->drefs[i];
  218. uint32_t size = get_be32(pb);
  219. int64_t next = url_ftell(pb) + size - 4;
  220. dref->type = get_le32(pb);
  221. get_be32(pb); // version + flags
  222. dprintf(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
  223. if (dref->type == MKTAG('a','l','i','s') && size > 150) {
  224. /* macintosh alias record */
  225. uint16_t volume_len, len;
  226. char volume[28];
  227. int16_t type;
  228. url_fskip(pb, 10);
  229. volume_len = get_byte(pb);
  230. volume_len = FFMIN(volume_len, 27);
  231. get_buffer(pb, volume, 27);
  232. volume[volume_len] = 0;
  233. av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", volume, volume_len);
  234. url_fskip(pb, 112);
  235. for (type = 0; type != -1 && url_ftell(pb) < next; ) {
  236. type = get_be16(pb);
  237. len = get_be16(pb);
  238. av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
  239. if (len&1)
  240. len += 1;
  241. if (type == 2) { // absolute path
  242. av_free(dref->path);
  243. dref->path = av_mallocz(len+1);
  244. if (!dref->path)
  245. return AVERROR(ENOMEM);
  246. get_buffer(pb, dref->path, len);
  247. if (len > volume_len && !strncmp(dref->path, volume, volume_len)) {
  248. len -= volume_len;
  249. memmove(dref->path, dref->path+volume_len, len);
  250. dref->path[len] = 0;
  251. }
  252. for (j = 0; j < len; j++)
  253. if (dref->path[j] == ':')
  254. dref->path[j] = '/';
  255. av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
  256. } else
  257. url_fskip(pb, len);
  258. }
  259. }
  260. url_fseek(pb, next, SEEK_SET);
  261. }
  262. return 0;
  263. }
  264. static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  265. {
  266. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  267. uint32_t type;
  268. uint32_t ctype;
  269. get_byte(pb); /* version */
  270. get_be24(pb); /* flags */
  271. /* component type */
  272. ctype = get_le32(pb);
  273. type = get_le32(pb); /* component subtype */
  274. dprintf(c->fc, "ctype= %c%c%c%c (0x%08x)\n", *((char *)&ctype), ((char *)&ctype)[1],
  275. ((char *)&ctype)[2], ((char *)&ctype)[3], (int) ctype);
  276. dprintf(c->fc, "stype= %c%c%c%c\n",
  277. *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
  278. if(!ctype)
  279. c->isom = 1;
  280. if (type == MKTAG('v','i','d','e'))
  281. st->codec->codec_type = CODEC_TYPE_VIDEO;
  282. else if(type == MKTAG('s','o','u','n'))
  283. st->codec->codec_type = CODEC_TYPE_AUDIO;
  284. else if(type == MKTAG('m','1','a',' '))
  285. st->codec->codec_id = CODEC_ID_MP2;
  286. else if(type == MKTAG('s','u','b','p')) {
  287. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  288. }
  289. get_be32(pb); /* component manufacture */
  290. get_be32(pb); /* component flags */
  291. get_be32(pb); /* component flags mask */
  292. if(atom.size <= 24)
  293. return 0; /* nothing left to read */
  294. url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
  295. return 0;
  296. }
  297. static int mp4_read_descr_len(ByteIOContext *pb)
  298. {
  299. int len = 0;
  300. int count = 4;
  301. while (count--) {
  302. int c = get_byte(pb);
  303. len = (len << 7) | (c & 0x7f);
  304. if (!(c & 0x80))
  305. break;
  306. }
  307. return len;
  308. }
  309. static int mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag)
  310. {
  311. int len;
  312. *tag = get_byte(pb);
  313. len = mp4_read_descr_len(pb);
  314. dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
  315. return len;
  316. }
  317. #define MP4ESDescrTag 0x03
  318. #define MP4DecConfigDescrTag 0x04
  319. #define MP4DecSpecificDescrTag 0x05
  320. static const AVCodecTag mp4_audio_types[] = {
  321. { CODEC_ID_MP3ON4, 29 }, /* old mp3on4 draft */
  322. { CODEC_ID_MP3ON4, 32 }, /* layer 1 */
  323. { CODEC_ID_MP3ON4, 33 }, /* layer 2 */
  324. { CODEC_ID_MP3ON4, 34 }, /* layer 3 */
  325. { CODEC_ID_NONE, 0 },
  326. };
  327. static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  328. {
  329. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  330. int tag, len;
  331. get_be32(pb); /* version + flags */
  332. len = mp4_read_descr(c, pb, &tag);
  333. if (tag == MP4ESDescrTag) {
  334. get_be16(pb); /* ID */
  335. get_byte(pb); /* priority */
  336. } else
  337. get_be16(pb); /* ID */
  338. len = mp4_read_descr(c, pb, &tag);
  339. if (tag == MP4DecConfigDescrTag) {
  340. int object_type_id = get_byte(pb);
  341. get_byte(pb); /* stream type */
  342. get_be24(pb); /* buffer size db */
  343. get_be32(pb); /* max bitrate */
  344. get_be32(pb); /* avg bitrate */
  345. st->codec->codec_id= codec_get_id(ff_mp4_obj_type, object_type_id);
  346. dprintf(c->fc, "esds object type id %d\n", object_type_id);
  347. len = mp4_read_descr(c, pb, &tag);
  348. if (tag == MP4DecSpecificDescrTag) {
  349. dprintf(c->fc, "Specific MPEG4 header len=%d\n", len);
  350. if((uint64_t)len > (1<<30))
  351. return -1;
  352. st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
  353. if (!st->codec->extradata)
  354. return AVERROR(ENOMEM);
  355. get_buffer(pb, st->codec->extradata, len);
  356. st->codec->extradata_size = len;
  357. if (st->codec->codec_id == CODEC_ID_AAC) {
  358. MPEG4AudioConfig cfg;
  359. ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
  360. st->codec->extradata_size);
  361. if (cfg.chan_config > 7)
  362. return -1;
  363. st->codec->channels = ff_mpeg4audio_channels[cfg.chan_config];
  364. if (cfg.object_type == 29 && cfg.sampling_index < 3) // old mp3on4
  365. st->codec->sample_rate = ff_mpa_freq_tab[cfg.sampling_index];
  366. else
  367. st->codec->sample_rate = cfg.sample_rate; // ext sample rate ?
  368. dprintf(c->fc, "mp4a config channels %d obj %d ext obj %d "
  369. "sample rate %d ext sample rate %d\n", st->codec->channels,
  370. cfg.object_type, cfg.ext_object_type,
  371. cfg.sample_rate, cfg.ext_sample_rate);
  372. if (!(st->codec->codec_id = codec_get_id(mp4_audio_types,
  373. cfg.object_type)))
  374. st->codec->codec_id = CODEC_ID_AAC;
  375. }
  376. }
  377. }
  378. return 0;
  379. }
  380. static int mov_read_pasp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  381. {
  382. const int num = get_be32(pb);
  383. const int den = get_be32(pb);
  384. AVStream * const st = c->fc->streams[c->fc->nb_streams-1];
  385. if (den != 0) {
  386. if ((st->sample_aspect_ratio.den && den != st->sample_aspect_ratio.den) ||
  387. (st->sample_aspect_ratio.num && num != st->sample_aspect_ratio.num))
  388. av_log(c->fc, AV_LOG_WARNING,
  389. "sample aspect ratio already set, overriding by 'pasp' atom\n");
  390. st->sample_aspect_ratio.num = num;
  391. st->sample_aspect_ratio.den = den;
  392. }
  393. return 0;
  394. }
  395. /* this atom contains actual media data */
  396. static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  397. {
  398. if(atom.size == 0) /* wrong one (MP4) */
  399. return 0;
  400. c->found_mdat=1;
  401. return 0; /* now go for moov */
  402. }
  403. static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  404. {
  405. uint32_t type = get_le32(pb);
  406. if (type != MKTAG('q','t',' ',' '))
  407. c->isom = 1;
  408. av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
  409. get_be32(pb); /* minor version */
  410. url_fskip(pb, atom.size - 8);
  411. return 0;
  412. }
  413. /* this atom should contain all header atoms */
  414. static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  415. {
  416. if (mov_read_default(c, pb, atom) < 0)
  417. return -1;
  418. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  419. /* so we don't parse the whole file if over a network */
  420. c->found_moov=1;
  421. return 0; /* now go for mdat */
  422. }
  423. static int mov_read_moof(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  424. {
  425. c->fragment.moof_offset = url_ftell(pb) - 8;
  426. dprintf(c->fc, "moof offset %llx\n", c->fragment.moof_offset);
  427. return mov_read_default(c, pb, atom);
  428. }
  429. static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  430. {
  431. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  432. MOVStreamContext *sc = st->priv_data;
  433. int version = get_byte(pb);
  434. int lang;
  435. if (version > 1)
  436. return -1; /* unsupported */
  437. get_be24(pb); /* flags */
  438. if (version == 1) {
  439. get_be64(pb);
  440. get_be64(pb);
  441. } else {
  442. get_be32(pb); /* creation time */
  443. get_be32(pb); /* modification time */
  444. }
  445. sc->time_scale = get_be32(pb);
  446. st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
  447. lang = get_be16(pb); /* language */
  448. ff_mov_lang_to_iso639(lang, st->language);
  449. get_be16(pb); /* quality */
  450. return 0;
  451. }
  452. static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  453. {
  454. int version = get_byte(pb); /* version */
  455. get_be24(pb); /* flags */
  456. if (version == 1) {
  457. get_be64(pb);
  458. get_be64(pb);
  459. } else {
  460. get_be32(pb); /* creation time */
  461. get_be32(pb); /* modification time */
  462. }
  463. c->time_scale = get_be32(pb); /* time scale */
  464. dprintf(c->fc, "time scale = %i\n", c->time_scale);
  465. c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
  466. get_be32(pb); /* preferred scale */
  467. get_be16(pb); /* preferred volume */
  468. url_fskip(pb, 10); /* reserved */
  469. url_fskip(pb, 36); /* display matrix */
  470. get_be32(pb); /* preview time */
  471. get_be32(pb); /* preview duration */
  472. get_be32(pb); /* poster time */
  473. get_be32(pb); /* selection time */
  474. get_be32(pb); /* selection duration */
  475. get_be32(pb); /* current time */
  476. get_be32(pb); /* next track ID */
  477. return 0;
  478. }
  479. static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  480. {
  481. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  482. if((uint64_t)atom.size > (1<<30))
  483. return -1;
  484. // currently SVQ3 decoder expect full STSD header - so let's fake it
  485. // this should be fixed and just SMI header should be passed
  486. av_free(st->codec->extradata);
  487. st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE);
  488. if (!st->codec->extradata)
  489. return AVERROR(ENOMEM);
  490. st->codec->extradata_size = 0x5a + atom.size;
  491. memcpy(st->codec->extradata, "SVQ3", 4); // fake
  492. get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
  493. dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
  494. return 0;
  495. }
  496. static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  497. {
  498. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  499. int little_endian = get_be16(pb);
  500. if (little_endian) {
  501. switch (st->codec->codec_id) {
  502. case CODEC_ID_PCM_S24BE:
  503. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  504. break;
  505. case CODEC_ID_PCM_S32BE:
  506. st->codec->codec_id = CODEC_ID_PCM_S32LE;
  507. break;
  508. case CODEC_ID_PCM_F32BE:
  509. st->codec->codec_id = CODEC_ID_PCM_F32LE;
  510. break;
  511. case CODEC_ID_PCM_F64BE:
  512. st->codec->codec_id = CODEC_ID_PCM_F64LE;
  513. break;
  514. default:
  515. break;
  516. }
  517. }
  518. return 0;
  519. }
  520. /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
  521. static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  522. {
  523. AVStream *st;
  524. uint64_t size;
  525. uint8_t *buf;
  526. if (c->fc->nb_streams < 1) // will happen with jp2 files
  527. return 0;
  528. st= c->fc->streams[c->fc->nb_streams-1];
  529. size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
  530. if(size > INT_MAX || (uint64_t)atom.size > INT_MAX)
  531. return -1;
  532. buf= av_realloc(st->codec->extradata, size);
  533. if(!buf)
  534. return -1;
  535. st->codec->extradata= buf;
  536. buf+= st->codec->extradata_size;
  537. st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
  538. AV_WB32( buf , atom.size + 8);
  539. AV_WL32( buf + 4, atom.type);
  540. get_buffer(pb, buf + 8, atom.size);
  541. return 0;
  542. }
  543. static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  544. {
  545. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  546. if((uint64_t)atom.size > (1<<30))
  547. return -1;
  548. if (st->codec->codec_id == CODEC_ID_QDM2) {
  549. // pass all frma atom to codec, needed at least for QDM2
  550. av_free(st->codec->extradata);
  551. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  552. if (!st->codec->extradata)
  553. return AVERROR(ENOMEM);
  554. st->codec->extradata_size = atom.size;
  555. get_buffer(pb, st->codec->extradata, atom.size);
  556. } else if (atom.size > 8) { /* to read frma, esds atoms */
  557. if (mov_read_default(c, pb, atom) < 0)
  558. return -1;
  559. } else
  560. url_fskip(pb, atom.size);
  561. return 0;
  562. }
  563. /**
  564. * This function reads atom content and puts data in extradata without tag
  565. * nor size unlike mov_read_extradata.
  566. */
  567. static int mov_read_glbl(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  568. {
  569. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  570. if((uint64_t)atom.size > (1<<30))
  571. return -1;
  572. av_free(st->codec->extradata);
  573. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  574. if (!st->codec->extradata)
  575. return AVERROR(ENOMEM);
  576. st->codec->extradata_size = atom.size;
  577. get_buffer(pb, st->codec->extradata, atom.size);
  578. return 0;
  579. }
  580. static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  581. {
  582. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  583. MOVStreamContext *sc = st->priv_data;
  584. unsigned int i, entries;
  585. get_byte(pb); /* version */
  586. get_be24(pb); /* flags */
  587. entries = get_be32(pb);
  588. if(entries >= UINT_MAX/sizeof(int64_t))
  589. return -1;
  590. sc->chunk_count = entries;
  591. sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
  592. if (!sc->chunk_offsets)
  593. return -1;
  594. if (atom.type == MKTAG('s','t','c','o'))
  595. for(i=0; i<entries; i++)
  596. sc->chunk_offsets[i] = get_be32(pb);
  597. else if (atom.type == MKTAG('c','o','6','4'))
  598. for(i=0; i<entries; i++)
  599. sc->chunk_offsets[i] = get_be64(pb);
  600. else
  601. return -1;
  602. return 0;
  603. }
  604. /**
  605. * Compute codec id for 'lpcm' tag.
  606. * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
  607. */
  608. static enum CodecID mov_get_lpcm_codec_id(int bps, int flags)
  609. {
  610. if (flags & 1) { // floating point
  611. if (flags & 2) { // big endian
  612. if (bps == 32) return CODEC_ID_PCM_F32BE;
  613. else if (bps == 64) return CODEC_ID_PCM_F64BE;
  614. } else {
  615. if (bps == 32) return CODEC_ID_PCM_F32LE;
  616. else if (bps == 64) return CODEC_ID_PCM_F64LE;
  617. }
  618. } else {
  619. if (flags & 2) {
  620. if (bps == 8)
  621. // signed integer
  622. if (flags & 4) return CODEC_ID_PCM_S8;
  623. else return CODEC_ID_PCM_U8;
  624. else if (bps == 16) return CODEC_ID_PCM_S16BE;
  625. else if (bps == 24) return CODEC_ID_PCM_S24BE;
  626. else if (bps == 32) return CODEC_ID_PCM_S32BE;
  627. } else {
  628. if (bps == 8)
  629. if (flags & 4) return CODEC_ID_PCM_S8;
  630. else return CODEC_ID_PCM_U8;
  631. else if (bps == 16) return CODEC_ID_PCM_S16LE;
  632. else if (bps == 24) return CODEC_ID_PCM_S24LE;
  633. else if (bps == 32) return CODEC_ID_PCM_S32LE;
  634. }
  635. }
  636. return CODEC_ID_NONE;
  637. }
  638. static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  639. {
  640. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  641. MOVStreamContext *sc = st->priv_data;
  642. int j, entries, pseudo_stream_id;
  643. get_byte(pb); /* version */
  644. get_be24(pb); /* flags */
  645. entries = get_be32(pb);
  646. for(pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
  647. //Parsing Sample description table
  648. enum CodecID id;
  649. int dref_id;
  650. MOVAtom a = { 0, 0, 0 };
  651. int64_t start_pos = url_ftell(pb);
  652. int size = get_be32(pb); /* size */
  653. uint32_t format = get_le32(pb); /* data format */
  654. get_be32(pb); /* reserved */
  655. get_be16(pb); /* reserved */
  656. dref_id = get_be16(pb);
  657. if (st->codec->codec_tag &&
  658. st->codec->codec_tag != format &&
  659. (c->fc->video_codec_id ? codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id
  660. : st->codec->codec_tag != MKTAG('j','p','e','g'))
  661. ){
  662. /* Multiple fourcc, we skip JPEG. This is not correct, we should
  663. * export it as a separate AVStream but this needs a few changes
  664. * in the MOV demuxer, patch welcome. */
  665. av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
  666. url_fskip(pb, size - (url_ftell(pb) - start_pos));
  667. continue;
  668. }
  669. sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
  670. sc->dref_id= dref_id;
  671. st->codec->codec_tag = format;
  672. id = codec_get_id(codec_movaudio_tags, format);
  673. if (id<=0 && (format&0xFFFF) == 'm'+('s'<<8))
  674. id = codec_get_id(codec_wav_tags, bswap_32(format)&0xFFFF);
  675. if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
  676. st->codec->codec_type = CODEC_TYPE_AUDIO;
  677. } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
  678. format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */
  679. id = codec_get_id(codec_movvideo_tags, format);
  680. if (id <= 0)
  681. id = codec_get_id(codec_bmp_tags, format);
  682. if (id > 0)
  683. st->codec->codec_type = CODEC_TYPE_VIDEO;
  684. else if(st->codec->codec_type == CODEC_TYPE_DATA){
  685. id = codec_get_id(ff_codec_movsubtitle_tags, format);
  686. if(id > 0)
  687. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  688. }
  689. }
  690. dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
  691. (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
  692. (format >> 24) & 0xff, st->codec->codec_type);
  693. if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
  694. uint8_t codec_name[32];
  695. unsigned int color_depth;
  696. int color_greyscale;
  697. st->codec->codec_id = id;
  698. get_be16(pb); /* version */
  699. get_be16(pb); /* revision level */
  700. get_be32(pb); /* vendor */
  701. get_be32(pb); /* temporal quality */
  702. get_be32(pb); /* spatial quality */
  703. st->codec->width = get_be16(pb); /* width */
  704. st->codec->height = get_be16(pb); /* height */
  705. get_be32(pb); /* horiz resolution */
  706. get_be32(pb); /* vert resolution */
  707. get_be32(pb); /* data size, always 0 */
  708. get_be16(pb); /* frames per samples */
  709. get_buffer(pb, codec_name, 32); /* codec name, pascal string */
  710. if (codec_name[0] <= 31) {
  711. memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
  712. st->codec->codec_name[codec_name[0]] = 0;
  713. }
  714. st->codec->bits_per_coded_sample = get_be16(pb); /* depth */
  715. st->codec->color_table_id = get_be16(pb); /* colortable id */
  716. dprintf(c->fc, "depth %d, ctab id %d\n",
  717. st->codec->bits_per_coded_sample, st->codec->color_table_id);
  718. /* figure out the palette situation */
  719. color_depth = st->codec->bits_per_coded_sample & 0x1F;
  720. color_greyscale = st->codec->bits_per_coded_sample & 0x20;
  721. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  722. if ((color_depth == 2) || (color_depth == 4) ||
  723. (color_depth == 8)) {
  724. /* for palette traversal */
  725. unsigned int color_start, color_count, color_end;
  726. unsigned char r, g, b;
  727. if (color_greyscale) {
  728. int color_index, color_dec;
  729. /* compute the greyscale palette */
  730. st->codec->bits_per_coded_sample = color_depth;
  731. color_count = 1 << color_depth;
  732. color_index = 255;
  733. color_dec = 256 / (color_count - 1);
  734. for (j = 0; j < color_count; j++) {
  735. r = g = b = color_index;
  736. c->palette_control.palette[j] =
  737. (r << 16) | (g << 8) | (b);
  738. color_index -= color_dec;
  739. if (color_index < 0)
  740. color_index = 0;
  741. }
  742. } else if (st->codec->color_table_id) {
  743. const uint8_t *color_table;
  744. /* if flag bit 3 is set, use the default palette */
  745. color_count = 1 << color_depth;
  746. if (color_depth == 2)
  747. color_table = ff_qt_default_palette_4;
  748. else if (color_depth == 4)
  749. color_table = ff_qt_default_palette_16;
  750. else
  751. color_table = ff_qt_default_palette_256;
  752. for (j = 0; j < color_count; j++) {
  753. r = color_table[j * 4 + 0];
  754. g = color_table[j * 4 + 1];
  755. b = color_table[j * 4 + 2];
  756. c->palette_control.palette[j] =
  757. (r << 16) | (g << 8) | (b);
  758. }
  759. } else {
  760. /* load the palette from the file */
  761. color_start = get_be32(pb);
  762. color_count = get_be16(pb);
  763. color_end = get_be16(pb);
  764. if ((color_start <= 255) &&
  765. (color_end <= 255)) {
  766. for (j = color_start; j <= color_end; j++) {
  767. /* each R, G, or B component is 16 bits;
  768. * only use the top 8 bits; skip alpha bytes
  769. * up front */
  770. get_byte(pb);
  771. get_byte(pb);
  772. r = get_byte(pb);
  773. get_byte(pb);
  774. g = get_byte(pb);
  775. get_byte(pb);
  776. b = get_byte(pb);
  777. get_byte(pb);
  778. c->palette_control.palette[j] =
  779. (r << 16) | (g << 8) | (b);
  780. }
  781. }
  782. }
  783. st->codec->palctrl = &c->palette_control;
  784. st->codec->palctrl->palette_changed = 1;
  785. } else
  786. st->codec->palctrl = NULL;
  787. } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
  788. int bits_per_sample, flags;
  789. uint16_t version = get_be16(pb);
  790. st->codec->codec_id = id;
  791. get_be16(pb); /* revision level */
  792. get_be32(pb); /* vendor */
  793. st->codec->channels = get_be16(pb); /* channel count */
  794. dprintf(c->fc, "audio channels %d\n", st->codec->channels);
  795. st->codec->bits_per_coded_sample = get_be16(pb); /* sample size */
  796. sc->audio_cid = get_be16(pb);
  797. get_be16(pb); /* packet size = 0 */
  798. st->codec->sample_rate = ((get_be32(pb) >> 16));
  799. //Read QT version 1 fields. In version 0 these do not exist.
  800. dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
  801. if(!c->isom) {
  802. if(version==1) {
  803. sc->samples_per_frame = get_be32(pb);
  804. get_be32(pb); /* bytes per packet */
  805. sc->bytes_per_frame = get_be32(pb);
  806. get_be32(pb); /* bytes per sample */
  807. } else if(version==2) {
  808. get_be32(pb); /* sizeof struct only */
  809. st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
  810. st->codec->channels = get_be32(pb);
  811. get_be32(pb); /* always 0x7F000000 */
  812. st->codec->bits_per_coded_sample = get_be32(pb); /* bits per channel if sound is uncompressed */
  813. flags = get_be32(pb); /* lcpm format specific flag */
  814. sc->bytes_per_frame = get_be32(pb); /* bytes per audio packet if constant */
  815. sc->samples_per_frame = get_be32(pb); /* lpcm frames per audio packet if constant */
  816. if (format == MKTAG('l','p','c','m'))
  817. st->codec->codec_id = mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
  818. }
  819. }
  820. switch (st->codec->codec_id) {
  821. case CODEC_ID_PCM_S8:
  822. case CODEC_ID_PCM_U8:
  823. if (st->codec->bits_per_coded_sample == 16)
  824. st->codec->codec_id = CODEC_ID_PCM_S16BE;
  825. break;
  826. case CODEC_ID_PCM_S16LE:
  827. case CODEC_ID_PCM_S16BE:
  828. if (st->codec->bits_per_coded_sample == 8)
  829. st->codec->codec_id = CODEC_ID_PCM_S8;
  830. else if (st->codec->bits_per_coded_sample == 24)
  831. st->codec->codec_id =
  832. st->codec->codec_id == CODEC_ID_PCM_S16BE ?
  833. CODEC_ID_PCM_S24BE : CODEC_ID_PCM_S24LE;
  834. break;
  835. /* set values for old format before stsd version 1 appeared */
  836. case CODEC_ID_MACE3:
  837. sc->samples_per_frame = 6;
  838. sc->bytes_per_frame = 2*st->codec->channels;
  839. break;
  840. case CODEC_ID_MACE6:
  841. sc->samples_per_frame = 6;
  842. sc->bytes_per_frame = 1*st->codec->channels;
  843. break;
  844. case CODEC_ID_ADPCM_IMA_QT:
  845. sc->samples_per_frame = 64;
  846. sc->bytes_per_frame = 34*st->codec->channels;
  847. break;
  848. case CODEC_ID_GSM:
  849. sc->samples_per_frame = 160;
  850. sc->bytes_per_frame = 33;
  851. break;
  852. default:
  853. break;
  854. }
  855. bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
  856. if (bits_per_sample) {
  857. st->codec->bits_per_coded_sample = bits_per_sample;
  858. sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
  859. }
  860. } else if(st->codec->codec_type==CODEC_TYPE_SUBTITLE){
  861. st->codec->codec_id= id;
  862. } else {
  863. /* other codec type, just skip (rtp, mp4s, tmcd ...) */
  864. url_fskip(pb, size - (url_ftell(pb) - start_pos));
  865. }
  866. /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
  867. a.size = size - (url_ftell(pb) - start_pos);
  868. if (a.size > 8) {
  869. if (mov_read_default(c, pb, a) < 0)
  870. return -1;
  871. } else if (a.size > 0)
  872. url_fskip(pb, a.size);
  873. }
  874. if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
  875. st->codec->sample_rate= sc->time_scale;
  876. /* special codec parameters handling */
  877. switch (st->codec->codec_id) {
  878. #ifdef CONFIG_DV_DEMUXER
  879. case CODEC_ID_DVAUDIO:
  880. c->dv_fctx = av_alloc_format_context();
  881. c->dv_demux = dv_init_demux(c->dv_fctx);
  882. if (!c->dv_demux) {
  883. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  884. return -1;
  885. }
  886. sc->dv_audio_container = 1;
  887. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  888. break;
  889. #endif
  890. /* no ifdef since parameters are always those */
  891. case CODEC_ID_QCELP:
  892. st->codec->frame_size= 160;
  893. st->codec->channels= 1; /* really needed */
  894. break;
  895. case CODEC_ID_AMR_NB:
  896. case CODEC_ID_AMR_WB:
  897. st->codec->frame_size= sc->samples_per_frame;
  898. st->codec->channels= 1; /* really needed */
  899. /* force sample rate for amr, stsd in 3gp does not store sample rate */
  900. if (st->codec->codec_id == CODEC_ID_AMR_NB)
  901. st->codec->sample_rate = 8000;
  902. else if (st->codec->codec_id == CODEC_ID_AMR_WB)
  903. st->codec->sample_rate = 16000;
  904. break;
  905. case CODEC_ID_MP2:
  906. case CODEC_ID_MP3:
  907. st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
  908. st->need_parsing = AVSTREAM_PARSE_FULL;
  909. break;
  910. case CODEC_ID_GSM:
  911. case CODEC_ID_ADPCM_MS:
  912. case CODEC_ID_ADPCM_IMA_WAV:
  913. st->codec->block_align = sc->bytes_per_frame;
  914. break;
  915. case CODEC_ID_ALAC:
  916. if (st->codec->extradata_size == 36) {
  917. st->codec->frame_size = AV_RB32(st->codec->extradata+12);
  918. st->codec->channels = AV_RB8 (st->codec->extradata+21);
  919. }
  920. break;
  921. default:
  922. break;
  923. }
  924. return 0;
  925. }
  926. static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  927. {
  928. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  929. MOVStreamContext *sc = st->priv_data;
  930. unsigned int i, entries;
  931. get_byte(pb); /* version */
  932. get_be24(pb); /* flags */
  933. entries = get_be32(pb);
  934. if(entries >= UINT_MAX / sizeof(*sc->sample_to_chunk))
  935. return -1;
  936. dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  937. sc->sample_to_chunk_sz = entries;
  938. sc->sample_to_chunk = av_malloc(entries * sizeof(*sc->sample_to_chunk));
  939. if (!sc->sample_to_chunk)
  940. return -1;
  941. for(i=0; i<entries; i++) {
  942. sc->sample_to_chunk[i].first = get_be32(pb);
  943. sc->sample_to_chunk[i].count = get_be32(pb);
  944. sc->sample_to_chunk[i].id = get_be32(pb);
  945. }
  946. return 0;
  947. }
  948. static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  949. {
  950. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  951. MOVStreamContext *sc = st->priv_data;
  952. unsigned int i, entries;
  953. get_byte(pb); /* version */
  954. get_be24(pb); /* flags */
  955. entries = get_be32(pb);
  956. if(entries >= UINT_MAX / sizeof(int))
  957. return -1;
  958. sc->keyframe_count = entries;
  959. dprintf(c->fc, "keyframe_count = %d\n", sc->keyframe_count);
  960. sc->keyframes = av_malloc(entries * sizeof(int));
  961. if (!sc->keyframes)
  962. return -1;
  963. for(i=0; i<entries; i++) {
  964. sc->keyframes[i] = get_be32(pb);
  965. //dprintf(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
  966. }
  967. return 0;
  968. }
  969. static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  970. {
  971. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  972. MOVStreamContext *sc = st->priv_data;
  973. unsigned int i, entries, sample_size;
  974. get_byte(pb); /* version */
  975. get_be24(pb); /* flags */
  976. sample_size = get_be32(pb);
  977. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  978. sc->sample_size = sample_size;
  979. entries = get_be32(pb);
  980. if(entries >= UINT_MAX / sizeof(int))
  981. return -1;
  982. sc->sample_count = entries;
  983. if (sample_size)
  984. return 0;
  985. dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, sc->sample_count);
  986. sc->sample_sizes = av_malloc(entries * sizeof(int));
  987. if (!sc->sample_sizes)
  988. return -1;
  989. for(i=0; i<entries; i++)
  990. sc->sample_sizes[i] = get_be32(pb);
  991. return 0;
  992. }
  993. static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  994. {
  995. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  996. MOVStreamContext *sc = st->priv_data;
  997. unsigned int i, entries;
  998. int64_t duration=0;
  999. int64_t total_sample_count=0;
  1000. get_byte(pb); /* version */
  1001. get_be24(pb); /* flags */
  1002. entries = get_be32(pb);
  1003. if(entries >= UINT_MAX / sizeof(*sc->stts_data))
  1004. return -1;
  1005. sc->stts_count = entries;
  1006. sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
  1007. if (!sc->stts_data)
  1008. return -1;
  1009. dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  1010. sc->time_rate=0;
  1011. for(i=0; i<entries; i++) {
  1012. int sample_duration;
  1013. int sample_count;
  1014. sample_count=get_be32(pb);
  1015. sample_duration = get_be32(pb);
  1016. sc->stts_data[i].count= sample_count;
  1017. sc->stts_data[i].duration= sample_duration;
  1018. sc->time_rate= ff_gcd(sc->time_rate, sample_duration);
  1019. dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  1020. duration+=(int64_t)sample_duration*sample_count;
  1021. total_sample_count+=sample_count;
  1022. }
  1023. st->nb_frames= total_sample_count;
  1024. if(duration)
  1025. st->duration= duration;
  1026. return 0;
  1027. }
  1028. static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1029. {
  1030. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1031. MOVStreamContext *sc = st->priv_data;
  1032. unsigned int i, entries;
  1033. get_byte(pb); /* version */
  1034. get_be24(pb); /* flags */
  1035. entries = get_be32(pb);
  1036. if(entries >= UINT_MAX / sizeof(*sc->ctts_data))
  1037. return -1;
  1038. sc->ctts_count = entries;
  1039. sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
  1040. if (!sc->ctts_data)
  1041. return -1;
  1042. dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1043. for(i=0; i<entries; i++) {
  1044. int count =get_be32(pb);
  1045. int duration =get_be32(pb);
  1046. if (duration < 0) {
  1047. sc->wrong_dts = 1;
  1048. st->codec->has_b_frames = 1;
  1049. }
  1050. sc->ctts_data[i].count = count;
  1051. sc->ctts_data[i].duration= duration;
  1052. sc->time_rate= ff_gcd(sc->time_rate, FFABS(duration));
  1053. }
  1054. return 0;
  1055. }
  1056. static void mov_build_index(MOVContext *mov, AVStream *st)
  1057. {
  1058. MOVStreamContext *sc = st->priv_data;
  1059. int64_t current_offset;
  1060. int64_t current_dts = 0;
  1061. unsigned int stts_index = 0;
  1062. unsigned int stsc_index = 0;
  1063. unsigned int stss_index = 0;
  1064. unsigned int i, j;
  1065. /* only use old uncompressed audio chunk demuxing when stts specifies it */
  1066. if (!(st->codec->codec_type == CODEC_TYPE_AUDIO &&
  1067. sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
  1068. unsigned int current_sample = 0;
  1069. unsigned int stts_sample = 0;
  1070. unsigned int keyframe, sample_size;
  1071. unsigned int distance = 0;
  1072. int key_off = sc->keyframes && sc->keyframes[0] == 1;
  1073. st->nb_frames = sc->sample_count;
  1074. for (i = 0; i < sc->chunk_count; i++) {
  1075. current_offset = sc->chunk_offsets[i];
  1076. if (stsc_index + 1 < sc->sample_to_chunk_sz &&
  1077. i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
  1078. stsc_index++;
  1079. for (j = 0; j < sc->sample_to_chunk[stsc_index].count; j++) {
  1080. if (current_sample >= sc->sample_count) {
  1081. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  1082. goto out;
  1083. }
  1084. keyframe = !sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index];
  1085. if (keyframe) {
  1086. distance = 0;
  1087. if (stss_index + 1 < sc->keyframe_count)
  1088. stss_index++;
  1089. }
  1090. sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
  1091. if(sc->pseudo_stream_id == -1 ||
  1092. sc->sample_to_chunk[stsc_index].id - 1 == sc->pseudo_stream_id) {
  1093. av_add_index_entry(st, current_offset, current_dts, sample_size, distance,
  1094. keyframe ? AVINDEX_KEYFRAME : 0);
  1095. dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1096. "size %d, distance %d, keyframe %d\n", st->index, current_sample,
  1097. current_offset, current_dts, sample_size, distance, keyframe);
  1098. }
  1099. current_offset += sample_size;
  1100. assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
  1101. current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
  1102. distance++;
  1103. stts_sample++;
  1104. current_sample++;
  1105. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  1106. stts_sample = 0;
  1107. stts_index++;
  1108. }
  1109. }
  1110. }
  1111. } else { /* read whole chunk */
  1112. unsigned int chunk_samples, chunk_size, chunk_duration;
  1113. unsigned int frames = 1;
  1114. for (i = 0; i < sc->chunk_count; i++) {
  1115. current_offset = sc->chunk_offsets[i];
  1116. if (stsc_index + 1 < sc->sample_to_chunk_sz &&
  1117. i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
  1118. stsc_index++;
  1119. chunk_samples = sc->sample_to_chunk[stsc_index].count;
  1120. /* get chunk size, beware of alaw/ulaw/mace */
  1121. if (sc->samples_per_frame > 0 &&
  1122. (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0)) {
  1123. if (sc->samples_per_frame < 160)
  1124. chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
  1125. else {
  1126. chunk_size = sc->bytes_per_frame;
  1127. frames = chunk_samples / sc->samples_per_frame;
  1128. chunk_samples = sc->samples_per_frame;
  1129. }
  1130. } else
  1131. chunk_size = chunk_samples * sc->sample_size;
  1132. for (j = 0; j < frames; j++) {
  1133. av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
  1134. /* get chunk duration */
  1135. chunk_duration = 0;
  1136. while (chunk_samples > 0) {
  1137. if (chunk_samples < sc->stts_data[stts_index].count) {
  1138. chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
  1139. sc->stts_data[stts_index].count -= chunk_samples;
  1140. break;
  1141. } else {
  1142. chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
  1143. chunk_samples -= sc->stts_data[stts_index].count;
  1144. if (stts_index + 1 < sc->stts_count)
  1145. stts_index++;
  1146. }
  1147. }
  1148. current_offset += sc->bytes_per_frame;
  1149. dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
  1150. "size %d, duration %d\n", st->index, i, current_offset, current_dts,
  1151. chunk_size, chunk_duration);
  1152. assert(chunk_duration % sc->time_rate == 0);
  1153. current_dts += chunk_duration / sc->time_rate;
  1154. }
  1155. }
  1156. }
  1157. out:
  1158. /* adjust sample count to avindex entries */
  1159. sc->sample_count = st->nb_index_entries;
  1160. }
  1161. static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1162. {
  1163. AVStream *st;
  1164. MOVStreamContext *sc;
  1165. int ret;
  1166. st = av_new_stream(c->fc, c->fc->nb_streams);
  1167. if (!st) return AVERROR(ENOMEM);
  1168. sc = av_mallocz(sizeof(MOVStreamContext));
  1169. if (!sc) return AVERROR(ENOMEM);
  1170. st->priv_data = sc;
  1171. st->codec->codec_type = CODEC_TYPE_DATA;
  1172. st->start_time = 0; /* XXX: check */
  1173. if ((ret = mov_read_default(c, pb, atom)) < 0)
  1174. return ret;
  1175. /* sanity checks */
  1176. if(sc->chunk_count && (!sc->stts_count || !sc->sample_to_chunk_sz ||
  1177. (!sc->sample_size && !sc->sample_count))){
  1178. av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
  1179. st->index);
  1180. sc->sample_count = 0; //ignore track
  1181. return 0;
  1182. }
  1183. if(!sc->time_rate)
  1184. sc->time_rate=1;
  1185. if(!sc->time_scale)
  1186. sc->time_scale= c->time_scale;
  1187. av_set_pts_info(st, 64, sc->time_rate, sc->time_scale);
  1188. if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
  1189. !st->codec->frame_size && sc->stts_count == 1)
  1190. st->codec->frame_size = av_rescale(sc->time_rate, st->codec->sample_rate, sc->time_scale);
  1191. if(st->duration != AV_NOPTS_VALUE){
  1192. assert(st->duration % sc->time_rate == 0);
  1193. st->duration /= sc->time_rate;
  1194. }
  1195. sc->ffindex = st->index;
  1196. mov_build_index(c, st);
  1197. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  1198. if (url_fopen(&sc->pb, sc->drefs[sc->dref_id-1].path, URL_RDONLY) < 0)
  1199. av_log(c->fc, AV_LOG_ERROR, "stream %d, error opening file %s: %s\n",
  1200. st->index, sc->drefs[sc->dref_id-1].path, strerror(errno));
  1201. } else
  1202. sc->pb = c->fc->pb;
  1203. switch (st->codec->codec_id) {
  1204. #ifdef CONFIG_H261_DECODER
  1205. case CODEC_ID_H261:
  1206. #endif
  1207. #ifdef CONFIG_H263_DECODER
  1208. case CODEC_ID_H263:
  1209. #endif
  1210. #ifdef CONFIG_MPEG4_DECODER
  1211. case CODEC_ID_MPEG4:
  1212. #endif
  1213. st->codec->width= 0; /* let decoder init width/height */
  1214. st->codec->height= 0;
  1215. break;
  1216. }
  1217. /* Do not need those anymore. */
  1218. av_freep(&sc->chunk_offsets);
  1219. av_freep(&sc->sample_to_chunk);
  1220. av_freep(&sc->sample_sizes);
  1221. av_freep(&sc->keyframes);
  1222. av_freep(&sc->stts_data);
  1223. return 0;
  1224. }
  1225. static int mov_read_ilst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1226. {
  1227. int ret;
  1228. c->itunes_metadata = 1;
  1229. ret = mov_read_default(c, pb, atom);
  1230. c->itunes_metadata = 0;
  1231. return ret;
  1232. }
  1233. static int mov_read_meta(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1234. {
  1235. url_fskip(pb, 4); // version + flags
  1236. atom.size -= 4;
  1237. return mov_read_default(c, pb, atom);
  1238. }
  1239. static int mov_read_trkn(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1240. {
  1241. get_be32(pb); // type
  1242. get_be32(pb); // unknown
  1243. c->fc->track = get_be32(pb);
  1244. dprintf(c->fc, "%.4s %d\n", (char*)&atom.type, c->fc->track);
  1245. return 0;
  1246. }
  1247. static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1248. {
  1249. char *str = NULL;
  1250. int size;
  1251. uint16_t str_size;
  1252. if (c->itunes_metadata) {
  1253. int data_size = get_be32(pb);
  1254. int tag = get_le32(pb);
  1255. if (tag == MKTAG('d','a','t','a')) {
  1256. get_be32(pb); // type
  1257. get_be32(pb); // unknown
  1258. str_size = data_size - 16;
  1259. } else return 0;
  1260. } else {
  1261. str_size = get_be16(pb); // string length
  1262. get_be16(pb); // language
  1263. }
  1264. switch (atom.type) {
  1265. case MKTAG(0xa9,'n','a','m'):
  1266. str = c->fc->title; size = sizeof(c->fc->title); break;
  1267. case MKTAG(0xa9,'A','R','T'):
  1268. case MKTAG(0xa9,'w','r','t'):
  1269. str = c->fc->author; size = sizeof(c->fc->author); break;
  1270. case MKTAG(0xa9,'c','p','y'):
  1271. str = c->fc->copyright; size = sizeof(c->fc->copyright); break;
  1272. case MKTAG(0xa9,'c','m','t'):
  1273. case MKTAG(0xa9,'i','n','f'):
  1274. str = c->fc->comment; size = sizeof(c->fc->comment); break;
  1275. case MKTAG(0xa9,'a','l','b'):
  1276. str = c->fc->album; size = sizeof(c->fc->album); break;
  1277. }
  1278. if (!str)
  1279. return 0;
  1280. get_buffer(pb, str, FFMIN(size, str_size));
  1281. dprintf(c->fc, "%.4s %s\n", (char*)&atom.type, str);
  1282. return 0;
  1283. }
  1284. static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1285. {
  1286. int i;
  1287. int width;
  1288. int height;
  1289. int64_t disp_transform[2];
  1290. int display_matrix[3][2];
  1291. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1292. int version = get_byte(pb);
  1293. get_be24(pb); /* flags */
  1294. /*
  1295. MOV_TRACK_ENABLED 0x0001
  1296. MOV_TRACK_IN_MOVIE 0x0002
  1297. MOV_TRACK_IN_PREVIEW 0x0004
  1298. MOV_TRACK_IN_POSTER 0x0008
  1299. */
  1300. if (version == 1) {
  1301. get_be64(pb);
  1302. get_be64(pb);
  1303. } else {
  1304. get_be32(pb); /* creation time */
  1305. get_be32(pb); /* modification time */
  1306. }
  1307. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  1308. get_be32(pb); /* reserved */
  1309. st->start_time = 0; /* check */
  1310. /* highlevel (considering edits) duration in movie timebase */
  1311. (version == 1) ? get_be64(pb) : get_be32(pb);
  1312. get_be32(pb); /* reserved */
  1313. get_be32(pb); /* reserved */
  1314. get_be16(pb); /* layer */
  1315. get_be16(pb); /* alternate group */
  1316. get_be16(pb); /* volume */
  1317. get_be16(pb); /* reserved */
  1318. //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
  1319. // they're kept in fixed point format through all calculations
  1320. // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
  1321. for (i = 0; i < 3; i++) {
  1322. display_matrix[i][0] = get_be32(pb); // 16.16 fixed point
  1323. display_matrix[i][1] = get_be32(pb); // 16.16 fixed point
  1324. get_be32(pb); // 2.30 fixed point (not used)
  1325. }
  1326. width = get_be32(pb); // 16.16 fixed point track width
  1327. height = get_be32(pb); // 16.16 fixed point track height
  1328. //transform the display width/height according to the matrix
  1329. // skip this if the display matrix is the default identity matrix
  1330. // to keep the same scale, use [width height 1<<16]
  1331. if (width && height &&
  1332. (display_matrix[0][0] != 65536 || display_matrix[0][1] ||
  1333. display_matrix[1][0] || display_matrix[1][1] != 65536 ||
  1334. display_matrix[2][0] || display_matrix[2][1])) {
  1335. for (i = 0; i < 2; i++)
  1336. disp_transform[i] =
  1337. (int64_t) width * display_matrix[0][i] +
  1338. (int64_t) height * display_matrix[1][i] +
  1339. ((int64_t) display_matrix[2][i] << 16);
  1340. //sample aspect ratio is new width/height divided by old width/height
  1341. st->sample_aspect_ratio = av_d2q(
  1342. ((double) disp_transform[0] * height) /
  1343. ((double) disp_transform[1] * width), INT_MAX);
  1344. }
  1345. return 0;
  1346. }
  1347. static int mov_read_tfhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1348. {
  1349. MOVFragment *frag = &c->fragment;
  1350. MOVTrackExt *trex = NULL;
  1351. int flags, track_id, i;
  1352. get_byte(pb); /* version */
  1353. flags = get_be24(pb);
  1354. track_id = get_be32(pb);
  1355. if (!track_id || track_id > c->fc->nb_streams)
  1356. return -1;
  1357. frag->track_id = track_id;
  1358. for (i = 0; i < c->trex_count; i++)
  1359. if (c->trex_data[i].track_id == frag->track_id) {
  1360. trex = &c->trex_data[i];
  1361. break;
  1362. }
  1363. if (!trex) {
  1364. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
  1365. return -1;
  1366. }
  1367. if (flags & 0x01) frag->base_data_offset = get_be64(pb);
  1368. else frag->base_data_offset = frag->moof_offset;
  1369. if (flags & 0x02) frag->stsd_id = get_be32(pb);
  1370. else frag->stsd_id = trex->stsd_id;
  1371. frag->duration = flags & 0x08 ? get_be32(pb) : trex->duration;
  1372. frag->size = flags & 0x10 ? get_be32(pb) : trex->size;
  1373. frag->flags = flags & 0x20 ? get_be32(pb) : trex->flags;
  1374. dprintf(c->fc, "frag flags 0x%x\n", frag->flags);
  1375. return 0;
  1376. }
  1377. static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1378. {
  1379. MOVTrackExt *trex;
  1380. if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
  1381. return -1;
  1382. c->trex_data = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
  1383. if (!c->trex_data)
  1384. return AVERROR(ENOMEM);
  1385. trex = &c->trex_data[c->trex_count++];
  1386. get_byte(pb); /* version */
  1387. get_be24(pb); /* flags */
  1388. trex->track_id = get_be32(pb);
  1389. trex->stsd_id = get_be32(pb);
  1390. trex->duration = get_be32(pb);
  1391. trex->size = get_be32(pb);
  1392. trex->flags = get_be32(pb);
  1393. return 0;
  1394. }
  1395. static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1396. {
  1397. MOVFragment *frag = &c->fragment;
  1398. AVStream *st;
  1399. MOVStreamContext *sc;
  1400. uint64_t offset;
  1401. int64_t dts;
  1402. int data_offset = 0;
  1403. unsigned entries, first_sample_flags = frag->flags;
  1404. int flags, distance, i;
  1405. if (!frag->track_id || frag->track_id > c->fc->nb_streams)
  1406. return -1;
  1407. st = c->fc->streams[frag->track_id-1];
  1408. sc = st->priv_data;
  1409. if (sc->pseudo_stream_id+1 != frag->stsd_id)
  1410. return 0;
  1411. get_byte(pb); /* version */
  1412. flags = get_be24(pb);
  1413. entries = get_be32(pb);
  1414. dprintf(c->fc, "flags 0x%x entries %d\n", flags, entries);
  1415. if (flags & 0x001) data_offset = get_be32(pb);
  1416. if (flags & 0x004) first_sample_flags = get_be32(pb);
  1417. if (flags & 0x800) {
  1418. if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
  1419. return -1;
  1420. sc->ctts_data = av_realloc(sc->ctts_data,
  1421. (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
  1422. if (!sc->ctts_data)
  1423. return AVERROR(ENOMEM);
  1424. }
  1425. dts = st->duration;
  1426. offset = frag->base_data_offset + data_offset;
  1427. distance = 0;
  1428. dprintf(c->fc, "first sample flags 0x%x\n", first_sample_flags);
  1429. for (i = 0; i < entries; i++) {
  1430. unsigned sample_size = frag->size;
  1431. int sample_flags = i ? frag->flags : first_sample_flags;
  1432. unsigned sample_duration = frag->duration;
  1433. int keyframe;
  1434. if (flags & 0x100) sample_duration = get_be32(pb);
  1435. if (flags & 0x200) sample_size = get_be32(pb);
  1436. if (flags & 0x400) sample_flags = get_be32(pb);
  1437. if (flags & 0x800) {
  1438. sc->ctts_data[sc->ctts_count].count = 1;
  1439. sc->ctts_data[sc->ctts_count].duration = get_be32(pb);
  1440. sc->ctts_count++;
  1441. }
  1442. if ((keyframe = st->codec->codec_type == CODEC_TYPE_AUDIO ||
  1443. (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))
  1444. distance = 0;
  1445. av_add_index_entry(st, offset, dts, sample_size, distance,
  1446. keyframe ? AVINDEX_KEYFRAME : 0);
  1447. dprintf(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1448. "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
  1449. offset, dts, sample_size, distance, keyframe);
  1450. distance++;
  1451. assert(sample_duration % sc->time_rate == 0);
  1452. dts += sample_duration / sc->time_rate;
  1453. offset += sample_size;
  1454. }
  1455. frag->moof_offset = offset;
  1456. sc->sample_count = st->nb_index_entries;
  1457. st->duration = dts;
  1458. return 0;
  1459. }
  1460. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1461. /* like the files created with Adobe Premiere 5.0, for samples see */
  1462. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1463. static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1464. {
  1465. int err;
  1466. if (atom.size < 8)
  1467. return 0; /* continue */
  1468. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1469. url_fskip(pb, atom.size - 4);
  1470. return 0;
  1471. }
  1472. atom.type = get_le32(pb);
  1473. atom.offset += 8;
  1474. atom.size -= 8;
  1475. if (atom.type != MKTAG('m','d','a','t')) {
  1476. url_fskip(pb, atom.size);
  1477. return 0;
  1478. }
  1479. err = mov_read_mdat(c, pb, atom);
  1480. return err;
  1481. }
  1482. static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1483. {
  1484. #ifdef CONFIG_ZLIB
  1485. ByteIOContext ctx;
  1486. uint8_t *cmov_data;
  1487. uint8_t *moov_data; /* uncompressed data */
  1488. long cmov_len, moov_len;
  1489. int ret = -1;
  1490. get_be32(pb); /* dcom atom */
  1491. if (get_le32(pb) != MKTAG('d','c','o','m'))
  1492. return -1;
  1493. if (get_le32(pb) != MKTAG('z','l','i','b')) {
  1494. av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !");
  1495. return -1;
  1496. }
  1497. get_be32(pb); /* cmvd atom */
  1498. if (get_le32(pb) != MKTAG('c','m','v','d'))
  1499. return -1;
  1500. moov_len = get_be32(pb); /* uncompressed size */
  1501. cmov_len = atom.size - 6 * 4;
  1502. cmov_data = av_malloc(cmov_len);
  1503. if (!cmov_data)
  1504. return -1;
  1505. moov_data = av_malloc(moov_len);
  1506. if (!moov_data) {
  1507. av_free(cmov_data);
  1508. return -1;
  1509. }
  1510. get_buffer(pb, cmov_data, cmov_len);
  1511. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1512. goto free_and_return;
  1513. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  1514. goto free_and_return;
  1515. atom.type = MKTAG('m','o','o','v');
  1516. atom.offset = 0;
  1517. atom.size = moov_len;
  1518. #ifdef DEBUG
  1519. // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
  1520. #endif
  1521. ret = mov_read_default(c, &ctx, atom);
  1522. free_and_return:
  1523. av_free(moov_data);
  1524. av_free(cmov_data);
  1525. return ret;
  1526. #else
  1527. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  1528. return -1;
  1529. #endif
  1530. }
  1531. /* edit list atom */
  1532. static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1533. {
  1534. MOVStreamContext *sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  1535. int i, edit_count;
  1536. get_byte(pb); /* version */
  1537. get_be24(pb); /* flags */
  1538. edit_count= sc->edit_count = get_be32(pb); /* entries */
  1539. for(i=0; i<edit_count; i++){
  1540. int time;
  1541. get_be32(pb); /* Track duration */
  1542. time = get_be32(pb); /* Media time */
  1543. get_be32(pb); /* Media rate */
  1544. if (time != 0)
  1545. av_log(c->fc, AV_LOG_WARNING, "edit list not starting at 0, "
  1546. "a/v desync might occur, patch welcome\n");
  1547. }
  1548. dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, sc->edit_count);
  1549. return 0;
  1550. }
  1551. static const MOVParseTableEntry mov_default_parse_table[] = {
  1552. { MKTAG('a','v','s','s'), mov_read_extradata },
  1553. { MKTAG('c','o','6','4'), mov_read_stco },
  1554. { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
  1555. { MKTAG('d','i','n','f'), mov_read_default },
  1556. { MKTAG('d','r','e','f'), mov_read_dref },
  1557. { MKTAG('e','d','t','s'), mov_read_default },
  1558. { MKTAG('e','l','s','t'), mov_read_elst },
  1559. { MKTAG('e','n','d','a'), mov_read_enda },
  1560. { MKTAG('f','i','e','l'), mov_read_extradata },
  1561. { MKTAG('f','t','y','p'), mov_read_ftyp },
  1562. { MKTAG('g','l','b','l'), mov_read_glbl },
  1563. { MKTAG('h','d','l','r'), mov_read_hdlr },
  1564. { MKTAG('i','l','s','t'), mov_read_ilst },
  1565. { MKTAG('j','p','2','h'), mov_read_extradata },
  1566. { MKTAG('m','d','a','t'), mov_read_mdat },
  1567. { MKTAG('m','d','h','d'), mov_read_mdhd },
  1568. { MKTAG('m','d','i','a'), mov_read_default },
  1569. { MKTAG('m','e','t','a'), mov_read_meta },
  1570. { MKTAG('m','i','n','f'), mov_read_default },
  1571. { MKTAG('m','o','o','f'), mov_read_moof },
  1572. { MKTAG('m','o','o','v'), mov_read_moov },
  1573. { MKTAG('m','v','e','x'), mov_read_default },
  1574. { MKTAG('m','v','h','d'), mov_read_mvhd },
  1575. { MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
  1576. { MKTAG('a','l','a','c'), mov_read_extradata }, /* alac specific atom */
  1577. { MKTAG('a','v','c','C'), mov_read_glbl },
  1578. { MKTAG('p','a','s','p'), mov_read_pasp },
  1579. { MKTAG('s','t','b','l'), mov_read_default },
  1580. { MKTAG('s','t','c','o'), mov_read_stco },
  1581. { MKTAG('s','t','s','c'), mov_read_stsc },
  1582. { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
  1583. { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
  1584. { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
  1585. { MKTAG('s','t','t','s'), mov_read_stts },
  1586. { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
  1587. { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
  1588. { MKTAG('t','r','a','k'), mov_read_trak },
  1589. { MKTAG('t','r','a','f'), mov_read_default },
  1590. { MKTAG('t','r','e','x'), mov_read_trex },
  1591. { MKTAG('t','r','k','n'), mov_read_trkn },
  1592. { MKTAG('t','r','u','n'), mov_read_trun },
  1593. { MKTAG('u','d','t','a'), mov_read_default },
  1594. { MKTAG('w','a','v','e'), mov_read_wave },
  1595. { MKTAG('e','s','d','s'), mov_read_esds },
  1596. { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
  1597. { MKTAG('c','m','o','v'), mov_read_cmov },
  1598. { MKTAG(0xa9,'n','a','m'), mov_read_udta_string },
  1599. { MKTAG(0xa9,'w','r','t'), mov_read_udta_string },
  1600. { MKTAG(0xa9,'c','p','y'), mov_read_udta_string },
  1601. { MKTAG(0xa9,'i','n','f'), mov_read_udta_string },
  1602. { MKTAG(0xa9,'i','n','f'), mov_read_udta_string },
  1603. { MKTAG(0xa9,'A','R','T'), mov_read_udta_string },
  1604. { MKTAG(0xa9,'a','l','b'), mov_read_udta_string },
  1605. { MKTAG(0xa9,'c','m','t'), mov_read_udta_string },
  1606. { 0, NULL }
  1607. };
  1608. static int mov_probe(AVProbeData *p)
  1609. {
  1610. unsigned int offset;
  1611. uint32_t tag;
  1612. int score = 0;
  1613. /* check file header */
  1614. offset = 0;
  1615. for(;;) {
  1616. /* ignore invalid offset */
  1617. if ((offset + 8) > (unsigned int)p->buf_size)
  1618. return score;
  1619. tag = AV_RL32(p->buf + offset + 4);
  1620. switch(tag) {
  1621. /* check for obvious tags */
  1622. case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
  1623. case MKTAG('m','o','o','v'):
  1624. case MKTAG('m','d','a','t'):
  1625. case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
  1626. case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
  1627. case MKTAG('f','t','y','p'):
  1628. return AVPROBE_SCORE_MAX;
  1629. /* those are more common words, so rate then a bit less */
  1630. case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
  1631. case MKTAG('w','i','d','e'):
  1632. case MKTAG('f','r','e','e'):
  1633. case MKTAG('j','u','n','k'):
  1634. case MKTAG('p','i','c','t'):
  1635. return AVPROBE_SCORE_MAX - 5;
  1636. case MKTAG(0x82,0x82,0x7f,0x7d):
  1637. case MKTAG('s','k','i','p'):
  1638. case MKTAG('u','u','i','d'):
  1639. case MKTAG('p','r','f','l'):
  1640. offset = AV_RB32(p->buf+offset) + offset;
  1641. /* if we only find those cause probedata is too small at least rate them */
  1642. score = AVPROBE_SCORE_MAX - 50;
  1643. break;
  1644. default:
  1645. /* unrecognized tag */
  1646. return score;
  1647. }
  1648. }
  1649. return score;
  1650. }
  1651. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1652. {
  1653. MOVContext *mov = s->priv_data;
  1654. ByteIOContext *pb = s->pb;
  1655. int err;
  1656. MOVAtom atom = { 0, 0, 0 };
  1657. mov->fc = s;
  1658. /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  1659. if(!url_is_streamed(pb))
  1660. atom.size = url_fsize(pb);
  1661. else
  1662. atom.size = INT64_MAX;
  1663. /* check MOV header */
  1664. if ((err = mov_read_default(mov, pb, atom)) < 0) {
  1665. av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
  1666. return err;
  1667. }
  1668. if (!mov->found_moov) {
  1669. av_log(s, AV_LOG_ERROR, "moov atom not found\n");
  1670. return -1;
  1671. }
  1672. dprintf(mov->fc, "on_parse_exit_offset=%lld\n", url_ftell(pb));
  1673. return 0;
  1674. }
  1675. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  1676. {
  1677. MOVContext *mov = s->priv_data;
  1678. MOVStreamContext *sc = 0;
  1679. AVIndexEntry *sample = 0;
  1680. int64_t best_dts = INT64_MAX;
  1681. int i;
  1682. retry:
  1683. for (i = 0; i < s->nb_streams; i++) {
  1684. AVStream *st = s->streams[i];
  1685. MOVStreamContext *msc = st->priv_data;
  1686. if (st->discard != AVDISCARD_ALL && msc->pb && msc->current_sample < msc->sample_count) {
  1687. AVIndexEntry *current_sample = &st->index_entries[msc->current_sample];
  1688. int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate,
  1689. AV_TIME_BASE, msc->time_scale);
  1690. dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  1691. if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) ||
  1692. (!url_is_streamed(s->pb) &&
  1693. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
  1694. ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  1695. (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
  1696. sample = current_sample;
  1697. best_dts = dts;
  1698. sc = msc;
  1699. }
  1700. }
  1701. }
  1702. if (!sample) {
  1703. mov->found_mdat = 0;
  1704. if (!url_is_streamed(s->pb) ||
  1705. mov_read_default(mov, s->pb, (MOVAtom){ 0, 0, INT64_MAX }) < 0 ||
  1706. url_feof(s->pb))
  1707. return -1;
  1708. dprintf(s, "read fragments, offset 0x%llx\n", url_ftell(s->pb));
  1709. goto retry;
  1710. }
  1711. /* must be done just before reading, to avoid infinite loop on sample */
  1712. sc->current_sample++;
  1713. if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  1714. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  1715. sc->ffindex, sample->pos);
  1716. return -1;
  1717. }
  1718. av_get_packet(sc->pb, pkt, sample->size);
  1719. #ifdef CONFIG_DV_DEMUXER
  1720. if (mov->dv_demux && sc->dv_audio_container) {
  1721. dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
  1722. av_free(pkt->data);
  1723. pkt->size = 0;
  1724. if (dv_get_packet(mov->dv_demux, pkt) < 0)
  1725. return -1;
  1726. }
  1727. #endif
  1728. pkt->stream_index = sc->ffindex;
  1729. pkt->dts = sample->timestamp;
  1730. if (sc->ctts_data) {
  1731. assert(sc->ctts_data[sc->sample_to_ctime_index].duration % sc->time_rate == 0);
  1732. pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate;
  1733. /* update ctts context */
  1734. sc->sample_to_ctime_sample++;
  1735. if (sc->sample_to_ctime_index < sc->ctts_count &&
  1736. sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) {
  1737. sc->sample_to_ctime_index++;
  1738. sc->sample_to_ctime_sample = 0;
  1739. }
  1740. if (sc->wrong_dts)
  1741. pkt->dts = AV_NOPTS_VALUE;
  1742. } else {
  1743. AVStream *st = s->streams[sc->ffindex];
  1744. int64_t next_dts = (sc->current_sample < sc->sample_count) ?
  1745. st->index_entries[sc->current_sample].timestamp : st->duration;
  1746. pkt->duration = next_dts - pkt->dts;
  1747. pkt->pts = pkt->dts;
  1748. }
  1749. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
  1750. pkt->pos = sample->pos;
  1751. dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
  1752. pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
  1753. return 0;
  1754. }
  1755. static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
  1756. {
  1757. MOVStreamContext *sc = st->priv_data;
  1758. int sample, time_sample;
  1759. int i;
  1760. sample = av_index_search_timestamp(st, timestamp, flags);
  1761. dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  1762. if (sample < 0) /* not sure what to do */
  1763. return -1;
  1764. sc->current_sample = sample;
  1765. dprintf(st->codec, "stream %d, found sample %d\n", st->index, sc->current_sample);
  1766. /* adjust ctts index */
  1767. if (sc->ctts_data) {
  1768. time_sample = 0;
  1769. for (i = 0; i < sc->ctts_count; i++) {
  1770. int next = time_sample + sc->ctts_data[i].count;
  1771. if (next > sc->current_sample) {
  1772. sc->sample_to_ctime_index = i;
  1773. sc->sample_to_ctime_sample = sc->current_sample - time_sample;
  1774. break;
  1775. }
  1776. time_sample = next;
  1777. }
  1778. }
  1779. return sample;
  1780. }
  1781. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  1782. {
  1783. AVStream *st;
  1784. int64_t seek_timestamp, timestamp;
  1785. int sample;
  1786. int i;
  1787. if (stream_index >= s->nb_streams)
  1788. return -1;
  1789. st = s->streams[stream_index];
  1790. sample = mov_seek_stream(st, sample_time, flags);
  1791. if (sample < 0)
  1792. return -1;
  1793. /* adjust seek timestamp to found sample timestamp */
  1794. seek_timestamp = st->index_entries[sample].timestamp;
  1795. for (i = 0; i < s->nb_streams; i++) {
  1796. st = s->streams[i];
  1797. if (stream_index == i || st->discard == AVDISCARD_ALL)
  1798. continue;
  1799. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  1800. mov_seek_stream(st, timestamp, flags);
  1801. }
  1802. return 0;
  1803. }
  1804. static int mov_read_close(AVFormatContext *s)
  1805. {
  1806. int i, j;
  1807. MOVContext *mov = s->priv_data;
  1808. for(i=0; i<s->nb_streams; i++) {
  1809. MOVStreamContext *sc = s->streams[i]->priv_data;
  1810. av_freep(&sc->ctts_data);
  1811. for (j=0; j<sc->drefs_count; j++)
  1812. av_freep(&sc->drefs[j].path);
  1813. av_freep(&sc->drefs);
  1814. if (sc->pb && sc->pb != s->pb)
  1815. url_fclose(sc->pb);
  1816. }
  1817. if(mov->dv_demux){
  1818. for(i=0; i<mov->dv_fctx->nb_streams; i++){
  1819. av_freep(&mov->dv_fctx->streams[i]->codec);
  1820. av_freep(&mov->dv_fctx->streams[i]);
  1821. }
  1822. av_freep(&mov->dv_fctx);
  1823. av_freep(&mov->dv_demux);
  1824. }
  1825. av_freep(&mov->trex_data);
  1826. return 0;
  1827. }
  1828. AVInputFormat mov_demuxer = {
  1829. "mov,mp4,m4a,3gp,3g2,mj2",
  1830. NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),
  1831. sizeof(MOVContext),
  1832. mov_probe,
  1833. mov_read_header,
  1834. mov_read_packet,
  1835. mov_read_close,
  1836. mov_read_seek,
  1837. };