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.

2033 lines
70KB

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