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.

2030 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. case CODEC_ID_AMR_NB:
  874. case CODEC_ID_AMR_WB:
  875. st->codec->frame_size= sc->samples_per_frame;
  876. st->codec->channels= 1; /* really needed */
  877. /* force sample rate for amr, stsd in 3gp does not store sample rate */
  878. if (st->codec->codec_id == CODEC_ID_AMR_NB)
  879. st->codec->sample_rate = 8000;
  880. else if (st->codec->codec_id == CODEC_ID_AMR_WB)
  881. st->codec->sample_rate = 16000;
  882. break;
  883. case CODEC_ID_MP2:
  884. case CODEC_ID_MP3:
  885. st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
  886. st->need_parsing = AVSTREAM_PARSE_FULL;
  887. break;
  888. case CODEC_ID_GSM:
  889. case CODEC_ID_ADPCM_MS:
  890. case CODEC_ID_ADPCM_IMA_WAV:
  891. st->codec->block_align = sc->bytes_per_frame;
  892. break;
  893. case CODEC_ID_ALAC:
  894. if (st->codec->extradata_size == 36)
  895. st->codec->frame_size = AV_RB32((st->codec->extradata+12));
  896. break;
  897. default:
  898. break;
  899. }
  900. return 0;
  901. }
  902. static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  903. {
  904. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  905. MOVStreamContext *sc = st->priv_data;
  906. unsigned int i, entries;
  907. get_byte(pb); /* version */
  908. get_be24(pb); /* flags */
  909. entries = get_be32(pb);
  910. if(entries >= UINT_MAX / sizeof(MOV_stsc_t))
  911. return -1;
  912. dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  913. sc->sample_to_chunk_sz = entries;
  914. sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_stsc_t));
  915. if (!sc->sample_to_chunk)
  916. return -1;
  917. for(i=0; i<entries; i++) {
  918. sc->sample_to_chunk[i].first = get_be32(pb);
  919. sc->sample_to_chunk[i].count = get_be32(pb);
  920. sc->sample_to_chunk[i].id = get_be32(pb);
  921. }
  922. return 0;
  923. }
  924. static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  925. {
  926. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  927. MOVStreamContext *sc = st->priv_data;
  928. unsigned int i, entries;
  929. get_byte(pb); /* version */
  930. get_be24(pb); /* flags */
  931. entries = get_be32(pb);
  932. if(entries >= UINT_MAX / sizeof(int))
  933. return -1;
  934. sc->keyframe_count = entries;
  935. dprintf(c->fc, "keyframe_count = %d\n", sc->keyframe_count);
  936. sc->keyframes = av_malloc(entries * sizeof(int));
  937. if (!sc->keyframes)
  938. return -1;
  939. for(i=0; i<entries; i++) {
  940. sc->keyframes[i] = get_be32(pb);
  941. //dprintf(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
  942. }
  943. return 0;
  944. }
  945. static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  946. {
  947. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  948. MOVStreamContext *sc = st->priv_data;
  949. unsigned int i, entries, sample_size;
  950. get_byte(pb); /* version */
  951. get_be24(pb); /* flags */
  952. sample_size = get_be32(pb);
  953. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  954. sc->sample_size = sample_size;
  955. entries = get_be32(pb);
  956. if(entries >= UINT_MAX / sizeof(int))
  957. return -1;
  958. sc->sample_count = entries;
  959. if (sample_size)
  960. return 0;
  961. dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, sc->sample_count);
  962. sc->sample_sizes = av_malloc(entries * sizeof(int));
  963. if (!sc->sample_sizes)
  964. return -1;
  965. for(i=0; i<entries; i++)
  966. sc->sample_sizes[i] = get_be32(pb);
  967. return 0;
  968. }
  969. static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  970. {
  971. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  972. MOVStreamContext *sc = st->priv_data;
  973. unsigned int i, entries;
  974. int64_t duration=0;
  975. int64_t total_sample_count=0;
  976. get_byte(pb); /* version */
  977. get_be24(pb); /* flags */
  978. entries = get_be32(pb);
  979. if(entries >= UINT_MAX / sizeof(MOV_stts_t))
  980. return -1;
  981. sc->stts_count = entries;
  982. sc->stts_data = av_malloc(entries * sizeof(MOV_stts_t));
  983. if (!sc->stts_data)
  984. return -1;
  985. dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  986. sc->time_rate=0;
  987. for(i=0; i<entries; i++) {
  988. int sample_duration;
  989. int sample_count;
  990. sample_count=get_be32(pb);
  991. sample_duration = get_be32(pb);
  992. sc->stts_data[i].count= sample_count;
  993. sc->stts_data[i].duration= sample_duration;
  994. sc->time_rate= ff_gcd(sc->time_rate, sample_duration);
  995. dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  996. duration+=(int64_t)sample_duration*sample_count;
  997. total_sample_count+=sample_count;
  998. }
  999. st->nb_frames= total_sample_count;
  1000. if(duration)
  1001. st->duration= duration;
  1002. return 0;
  1003. }
  1004. static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1005. {
  1006. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1007. MOVStreamContext *sc = st->priv_data;
  1008. unsigned int i, entries;
  1009. get_byte(pb); /* version */
  1010. get_be24(pb); /* flags */
  1011. entries = get_be32(pb);
  1012. if(entries >= UINT_MAX / sizeof(MOV_stts_t))
  1013. return -1;
  1014. sc->ctts_count = entries;
  1015. sc->ctts_data = av_malloc(entries * sizeof(MOV_stts_t));
  1016. if (!sc->ctts_data)
  1017. return -1;
  1018. dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1019. for(i=0; i<entries; i++) {
  1020. int count =get_be32(pb);
  1021. int duration =get_be32(pb);
  1022. if (duration < 0) {
  1023. sc->wrong_dts = 1;
  1024. st->codec->has_b_frames = 1;
  1025. }
  1026. sc->ctts_data[i].count = count;
  1027. sc->ctts_data[i].duration= duration;
  1028. sc->time_rate= ff_gcd(sc->time_rate, FFABS(duration));
  1029. }
  1030. return 0;
  1031. }
  1032. static void mov_build_index(MOVContext *mov, AVStream *st)
  1033. {
  1034. MOVStreamContext *sc = st->priv_data;
  1035. int64_t current_offset;
  1036. int64_t current_dts = 0;
  1037. unsigned int stts_index = 0;
  1038. unsigned int stsc_index = 0;
  1039. unsigned int stss_index = 0;
  1040. unsigned int i, j;
  1041. /* only use old uncompressed audio chunk demuxing when stts specifies it */
  1042. if (!(st->codec->codec_type == CODEC_TYPE_AUDIO &&
  1043. sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
  1044. unsigned int current_sample = 0;
  1045. unsigned int stts_sample = 0;
  1046. unsigned int keyframe, sample_size;
  1047. unsigned int distance = 0;
  1048. int key_off = sc->keyframes && sc->keyframes[0] == 1;
  1049. st->nb_frames = sc->sample_count;
  1050. for (i = 0; i < sc->chunk_count; i++) {
  1051. current_offset = sc->chunk_offsets[i];
  1052. if (stsc_index + 1 < sc->sample_to_chunk_sz &&
  1053. i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
  1054. stsc_index++;
  1055. for (j = 0; j < sc->sample_to_chunk[stsc_index].count; j++) {
  1056. if (current_sample >= sc->sample_count) {
  1057. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  1058. goto out;
  1059. }
  1060. keyframe = !sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index];
  1061. if (keyframe) {
  1062. distance = 0;
  1063. if (stss_index + 1 < sc->keyframe_count)
  1064. stss_index++;
  1065. }
  1066. sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
  1067. if(sc->pseudo_stream_id == -1 ||
  1068. sc->sample_to_chunk[stsc_index].id - 1 == sc->pseudo_stream_id) {
  1069. av_add_index_entry(st, current_offset, current_dts, sample_size, distance,
  1070. keyframe ? AVINDEX_KEYFRAME : 0);
  1071. dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1072. "size %d, distance %d, keyframe %d\n", st->index, current_sample,
  1073. current_offset, current_dts, sample_size, distance, keyframe);
  1074. }
  1075. current_offset += sample_size;
  1076. assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
  1077. current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
  1078. distance++;
  1079. stts_sample++;
  1080. current_sample++;
  1081. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  1082. stts_sample = 0;
  1083. stts_index++;
  1084. }
  1085. }
  1086. }
  1087. } else { /* read whole chunk */
  1088. unsigned int chunk_samples, chunk_size, chunk_duration;
  1089. unsigned int frames = 1;
  1090. for (i = 0; i < sc->chunk_count; i++) {
  1091. current_offset = sc->chunk_offsets[i];
  1092. if (stsc_index + 1 < sc->sample_to_chunk_sz &&
  1093. i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
  1094. stsc_index++;
  1095. chunk_samples = sc->sample_to_chunk[stsc_index].count;
  1096. /* get chunk size, beware of alaw/ulaw/mace */
  1097. if (sc->samples_per_frame > 0 &&
  1098. (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0)) {
  1099. if (sc->samples_per_frame < 160)
  1100. chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
  1101. else {
  1102. chunk_size = sc->bytes_per_frame;
  1103. frames = chunk_samples / sc->samples_per_frame;
  1104. chunk_samples = sc->samples_per_frame;
  1105. }
  1106. } else
  1107. chunk_size = chunk_samples * sc->sample_size;
  1108. for (j = 0; j < frames; j++) {
  1109. av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
  1110. /* get chunk duration */
  1111. chunk_duration = 0;
  1112. while (chunk_samples > 0) {
  1113. if (chunk_samples < sc->stts_data[stts_index].count) {
  1114. chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
  1115. sc->stts_data[stts_index].count -= chunk_samples;
  1116. break;
  1117. } else {
  1118. chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
  1119. chunk_samples -= sc->stts_data[stts_index].count;
  1120. if (stts_index + 1 < sc->stts_count)
  1121. stts_index++;
  1122. }
  1123. }
  1124. current_offset += sc->bytes_per_frame;
  1125. dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
  1126. "size %d, duration %d\n", st->index, i, current_offset, current_dts,
  1127. chunk_size, chunk_duration);
  1128. assert(chunk_duration % sc->time_rate == 0);
  1129. current_dts += chunk_duration / sc->time_rate;
  1130. }
  1131. }
  1132. }
  1133. out:
  1134. /* adjust sample count to avindex entries */
  1135. sc->sample_count = st->nb_index_entries;
  1136. }
  1137. static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1138. {
  1139. AVStream *st;
  1140. MOVStreamContext *sc;
  1141. int ret;
  1142. st = av_new_stream(c->fc, c->fc->nb_streams);
  1143. if (!st) return AVERROR(ENOMEM);
  1144. sc = av_mallocz(sizeof(MOVStreamContext));
  1145. if (!sc) return AVERROR(ENOMEM);
  1146. st->priv_data = sc;
  1147. st->codec->codec_type = CODEC_TYPE_DATA;
  1148. st->start_time = 0; /* XXX: check */
  1149. if ((ret = mov_read_default(c, pb, atom)) < 0)
  1150. return ret;
  1151. /* sanity checks */
  1152. if(sc->chunk_count && (!sc->stts_count || !sc->sample_to_chunk_sz ||
  1153. (!sc->sample_size && !sc->sample_count))){
  1154. av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
  1155. st->index);
  1156. sc->sample_count = 0; //ignore track
  1157. return 0;
  1158. }
  1159. if(!sc->time_rate)
  1160. sc->time_rate=1;
  1161. if(!sc->time_scale)
  1162. sc->time_scale= c->time_scale;
  1163. av_set_pts_info(st, 64, sc->time_rate, sc->time_scale);
  1164. if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
  1165. !st->codec->frame_size && sc->stts_count == 1)
  1166. st->codec->frame_size = av_rescale(sc->time_rate, st->codec->sample_rate, sc->time_scale);
  1167. if(st->duration != AV_NOPTS_VALUE){
  1168. assert(st->duration % sc->time_rate == 0);
  1169. st->duration /= sc->time_rate;
  1170. }
  1171. sc->ffindex = st->index;
  1172. mov_build_index(c, st);
  1173. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  1174. if (url_fopen(&sc->pb, sc->drefs[sc->dref_id-1].path, URL_RDONLY) < 0)
  1175. av_log(c->fc, AV_LOG_ERROR, "stream %d, error opening file %s: %s\n",
  1176. st->index, sc->drefs[sc->dref_id-1].path, strerror(errno));
  1177. } else
  1178. sc->pb = c->fc->pb;
  1179. switch (st->codec->codec_id) {
  1180. #ifdef CONFIG_H261_DECODER
  1181. case CODEC_ID_H261:
  1182. #endif
  1183. #ifdef CONFIG_H263_DECODER
  1184. case CODEC_ID_H263:
  1185. #endif
  1186. #ifdef CONFIG_MPEG4_DECODER
  1187. case CODEC_ID_MPEG4:
  1188. #endif
  1189. st->codec->width= 0; /* let decoder init width/height */
  1190. st->codec->height= 0;
  1191. break;
  1192. }
  1193. /* Do not need those anymore. */
  1194. av_freep(&sc->chunk_offsets);
  1195. av_freep(&sc->sample_to_chunk);
  1196. av_freep(&sc->sample_sizes);
  1197. av_freep(&sc->keyframes);
  1198. av_freep(&sc->stts_data);
  1199. return 0;
  1200. }
  1201. static int mov_read_ilst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1202. {
  1203. int ret;
  1204. c->itunes_metadata = 1;
  1205. ret = mov_read_default(c, pb, atom);
  1206. c->itunes_metadata = 0;
  1207. return ret;
  1208. }
  1209. static int mov_read_meta(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1210. {
  1211. url_fskip(pb, 4); // version + flags
  1212. atom.size -= 4;
  1213. return mov_read_default(c, pb, atom);
  1214. }
  1215. static int mov_read_trkn(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1216. {
  1217. get_be32(pb); // type
  1218. get_be32(pb); // unknown
  1219. c->fc->track = get_be32(pb);
  1220. dprintf(c->fc, "%.4s %d\n", (char*)&atom.type, c->fc->track);
  1221. return 0;
  1222. }
  1223. static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1224. {
  1225. char *str = NULL;
  1226. int size;
  1227. uint16_t str_size;
  1228. if (c->itunes_metadata) {
  1229. int data_size = get_be32(pb);
  1230. int tag = get_le32(pb);
  1231. if (tag == MKTAG('d','a','t','a')) {
  1232. get_be32(pb); // type
  1233. get_be32(pb); // unknown
  1234. str_size = data_size - 16;
  1235. } else return 0;
  1236. } else {
  1237. str_size = get_be16(pb); // string length
  1238. get_be16(pb); // language
  1239. }
  1240. switch (atom.type) {
  1241. case MKTAG(0xa9,'n','a','m'):
  1242. str = c->fc->title; size = sizeof(c->fc->title); break;
  1243. case MKTAG(0xa9,'A','R','T'):
  1244. case MKTAG(0xa9,'w','r','t'):
  1245. str = c->fc->author; size = sizeof(c->fc->author); break;
  1246. case MKTAG(0xa9,'c','p','y'):
  1247. str = c->fc->copyright; size = sizeof(c->fc->copyright); break;
  1248. case MKTAG(0xa9,'c','m','t'):
  1249. case MKTAG(0xa9,'i','n','f'):
  1250. str = c->fc->comment; size = sizeof(c->fc->comment); break;
  1251. case MKTAG(0xa9,'a','l','b'):
  1252. str = c->fc->album; size = sizeof(c->fc->album); break;
  1253. }
  1254. if (!str)
  1255. return 0;
  1256. get_buffer(pb, str, FFMIN(size, str_size));
  1257. dprintf(c->fc, "%.4s %s\n", (char*)&atom.type, str);
  1258. return 0;
  1259. }
  1260. static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1261. {
  1262. int i;
  1263. int width;
  1264. int height;
  1265. int64_t disp_transform[2];
  1266. int display_matrix[3][2];
  1267. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1268. int version = get_byte(pb);
  1269. get_be24(pb); /* flags */
  1270. /*
  1271. MOV_TRACK_ENABLED 0x0001
  1272. MOV_TRACK_IN_MOVIE 0x0002
  1273. MOV_TRACK_IN_PREVIEW 0x0004
  1274. MOV_TRACK_IN_POSTER 0x0008
  1275. */
  1276. if (version == 1) {
  1277. get_be64(pb);
  1278. get_be64(pb);
  1279. } else {
  1280. get_be32(pb); /* creation time */
  1281. get_be32(pb); /* modification time */
  1282. }
  1283. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  1284. get_be32(pb); /* reserved */
  1285. st->start_time = 0; /* check */
  1286. /* highlevel (considering edits) duration in movie timebase */
  1287. (version == 1) ? get_be64(pb) : get_be32(pb);
  1288. get_be32(pb); /* reserved */
  1289. get_be32(pb); /* reserved */
  1290. get_be16(pb); /* layer */
  1291. get_be16(pb); /* alternate group */
  1292. get_be16(pb); /* volume */
  1293. get_be16(pb); /* reserved */
  1294. //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
  1295. // they're kept in fixed point format through all calculations
  1296. // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
  1297. for (i = 0; i < 3; i++) {
  1298. display_matrix[i][0] = get_be32(pb); // 16.16 fixed point
  1299. display_matrix[i][1] = get_be32(pb); // 16.16 fixed point
  1300. get_be32(pb); // 2.30 fixed point (not used)
  1301. }
  1302. width = get_be32(pb); // 16.16 fixed point track width
  1303. height = get_be32(pb); // 16.16 fixed point track height
  1304. //transform the display width/height according to the matrix
  1305. // skip this if the display matrix is the default identity matrix
  1306. // to keep the same scale, use [width height 1<<16]
  1307. if (width && height &&
  1308. (display_matrix[0][0] != 65536 || display_matrix[0][1] ||
  1309. display_matrix[1][0] || display_matrix[1][1] != 65536 ||
  1310. display_matrix[2][0] || display_matrix[2][1])) {
  1311. for (i = 0; i < 2; i++)
  1312. disp_transform[i] =
  1313. (int64_t) width * display_matrix[0][i] +
  1314. (int64_t) height * display_matrix[1][i] +
  1315. ((int64_t) display_matrix[2][i] << 16);
  1316. //sample aspect ratio is new width/height divided by old width/height
  1317. st->sample_aspect_ratio = av_d2q(
  1318. ((double) disp_transform[0] * height) /
  1319. ((double) disp_transform[1] * width), INT_MAX);
  1320. }
  1321. return 0;
  1322. }
  1323. static int mov_read_tfhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1324. {
  1325. MOVFragment *frag = &c->fragment;
  1326. MOVTrackExt *trex = NULL;
  1327. int flags, track_id, i;
  1328. get_byte(pb); /* version */
  1329. flags = get_be24(pb);
  1330. track_id = get_be32(pb);
  1331. if (!track_id || track_id > c->fc->nb_streams)
  1332. return -1;
  1333. frag->track_id = track_id;
  1334. for (i = 0; i < c->trex_count; i++)
  1335. if (c->trex_data[i].track_id == frag->track_id) {
  1336. trex = &c->trex_data[i];
  1337. break;
  1338. }
  1339. if (!trex) {
  1340. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
  1341. return -1;
  1342. }
  1343. if (flags & 0x01) frag->base_data_offset = get_be64(pb);
  1344. else frag->base_data_offset = frag->moof_offset;
  1345. if (flags & 0x02) frag->stsd_id = get_be32(pb);
  1346. else frag->stsd_id = trex->stsd_id;
  1347. frag->duration = flags & 0x08 ? get_be32(pb) : trex->duration;
  1348. frag->size = flags & 0x10 ? get_be32(pb) : trex->size;
  1349. frag->flags = flags & 0x20 ? get_be32(pb) : trex->flags;
  1350. dprintf(c->fc, "frag flags 0x%x\n", frag->flags);
  1351. return 0;
  1352. }
  1353. static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1354. {
  1355. MOVTrackExt *trex;
  1356. if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
  1357. return -1;
  1358. c->trex_data = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
  1359. if (!c->trex_data)
  1360. return AVERROR(ENOMEM);
  1361. trex = &c->trex_data[c->trex_count++];
  1362. get_byte(pb); /* version */
  1363. get_be24(pb); /* flags */
  1364. trex->track_id = get_be32(pb);
  1365. trex->stsd_id = get_be32(pb);
  1366. trex->duration = get_be32(pb);
  1367. trex->size = get_be32(pb);
  1368. trex->flags = get_be32(pb);
  1369. return 0;
  1370. }
  1371. static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1372. {
  1373. MOVFragment *frag = &c->fragment;
  1374. AVStream *st;
  1375. MOVStreamContext *sc;
  1376. uint64_t offset;
  1377. int64_t dts;
  1378. int data_offset = 0;
  1379. unsigned entries, first_sample_flags = frag->flags;
  1380. int flags, distance, i;
  1381. if (!frag->track_id || frag->track_id > c->fc->nb_streams)
  1382. return -1;
  1383. st = c->fc->streams[frag->track_id-1];
  1384. sc = st->priv_data;
  1385. if (sc->pseudo_stream_id+1 != frag->stsd_id)
  1386. return 0;
  1387. get_byte(pb); /* version */
  1388. flags = get_be24(pb);
  1389. entries = get_be32(pb);
  1390. dprintf(c->fc, "flags 0x%x entries %d\n", flags, entries);
  1391. if (flags & 0x001) data_offset = get_be32(pb);
  1392. if (flags & 0x004) first_sample_flags = get_be32(pb);
  1393. if (flags & 0x800) {
  1394. if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
  1395. return -1;
  1396. sc->ctts_data = av_realloc(sc->ctts_data,
  1397. (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
  1398. if (!sc->ctts_data)
  1399. return AVERROR(ENOMEM);
  1400. }
  1401. dts = st->duration;
  1402. offset = frag->base_data_offset + data_offset;
  1403. distance = 0;
  1404. dprintf(c->fc, "first sample flags 0x%x\n", first_sample_flags);
  1405. for (i = 0; i < entries; i++) {
  1406. unsigned sample_size = frag->size;
  1407. int sample_flags = i ? frag->flags : first_sample_flags;
  1408. unsigned sample_duration = frag->duration;
  1409. int keyframe;
  1410. if (flags & 0x100) sample_duration = get_be32(pb);
  1411. if (flags & 0x200) sample_size = get_be32(pb);
  1412. if (flags & 0x400) sample_flags = get_be32(pb);
  1413. if (flags & 0x800) {
  1414. sc->ctts_data[sc->ctts_count].count = 1;
  1415. sc->ctts_data[sc->ctts_count].duration = get_be32(pb);
  1416. sc->ctts_count++;
  1417. }
  1418. if ((keyframe = st->codec->codec_type == CODEC_TYPE_AUDIO ||
  1419. (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))
  1420. distance = 0;
  1421. av_add_index_entry(st, offset, dts, sample_size, distance,
  1422. keyframe ? AVINDEX_KEYFRAME : 0);
  1423. dprintf(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1424. "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
  1425. offset, dts, sample_size, distance, keyframe);
  1426. distance++;
  1427. assert(sample_duration % sc->time_rate == 0);
  1428. dts += sample_duration / sc->time_rate;
  1429. offset += sample_size;
  1430. }
  1431. frag->moof_offset = offset;
  1432. sc->sample_count = st->nb_index_entries;
  1433. st->duration = dts;
  1434. return 0;
  1435. }
  1436. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1437. /* like the files created with Adobe Premiere 5.0, for samples see */
  1438. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1439. static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1440. {
  1441. int err;
  1442. if (atom.size < 8)
  1443. return 0; /* continue */
  1444. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1445. url_fskip(pb, atom.size - 4);
  1446. return 0;
  1447. }
  1448. atom.type = get_le32(pb);
  1449. atom.offset += 8;
  1450. atom.size -= 8;
  1451. if (atom.type != MKTAG('m','d','a','t')) {
  1452. url_fskip(pb, atom.size);
  1453. return 0;
  1454. }
  1455. err = mov_read_mdat(c, pb, atom);
  1456. return err;
  1457. }
  1458. static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1459. {
  1460. #ifdef CONFIG_ZLIB
  1461. ByteIOContext ctx;
  1462. uint8_t *cmov_data;
  1463. uint8_t *moov_data; /* uncompressed data */
  1464. long cmov_len, moov_len;
  1465. int ret = -1;
  1466. get_be32(pb); /* dcom atom */
  1467. if (get_le32(pb) != MKTAG('d','c','o','m'))
  1468. return -1;
  1469. if (get_le32(pb) != MKTAG('z','l','i','b')) {
  1470. av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !");
  1471. return -1;
  1472. }
  1473. get_be32(pb); /* cmvd atom */
  1474. if (get_le32(pb) != MKTAG('c','m','v','d'))
  1475. return -1;
  1476. moov_len = get_be32(pb); /* uncompressed size */
  1477. cmov_len = atom.size - 6 * 4;
  1478. cmov_data = av_malloc(cmov_len);
  1479. if (!cmov_data)
  1480. return -1;
  1481. moov_data = av_malloc(moov_len);
  1482. if (!moov_data) {
  1483. av_free(cmov_data);
  1484. return -1;
  1485. }
  1486. get_buffer(pb, cmov_data, cmov_len);
  1487. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1488. goto free_and_return;
  1489. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  1490. goto free_and_return;
  1491. atom.type = MKTAG('m','o','o','v');
  1492. atom.offset = 0;
  1493. atom.size = moov_len;
  1494. #ifdef DEBUG
  1495. // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
  1496. #endif
  1497. ret = mov_read_default(c, &ctx, atom);
  1498. free_and_return:
  1499. av_free(moov_data);
  1500. av_free(cmov_data);
  1501. return ret;
  1502. #else
  1503. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  1504. return -1;
  1505. #endif
  1506. }
  1507. /* edit list atom */
  1508. static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1509. {
  1510. MOVStreamContext *sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  1511. int i, edit_count;
  1512. get_byte(pb); /* version */
  1513. get_be24(pb); /* flags */
  1514. edit_count= sc->edit_count = get_be32(pb); /* entries */
  1515. for(i=0; i<edit_count; i++){
  1516. int time;
  1517. get_be32(pb); /* Track duration */
  1518. time = get_be32(pb); /* Media time */
  1519. get_be32(pb); /* Media rate */
  1520. if (time != 0)
  1521. av_log(c->fc, AV_LOG_WARNING, "edit list not starting at 0, "
  1522. "a/v desync might occur, patch welcome\n");
  1523. }
  1524. dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, sc->edit_count);
  1525. return 0;
  1526. }
  1527. static const MOVParseTableEntry mov_default_parse_table[] = {
  1528. { MKTAG('a','v','s','s'), mov_read_extradata },
  1529. { MKTAG('c','o','6','4'), mov_read_stco },
  1530. { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
  1531. { MKTAG('d','i','n','f'), mov_read_default },
  1532. { MKTAG('d','r','e','f'), mov_read_dref },
  1533. { MKTAG('e','d','t','s'), mov_read_default },
  1534. { MKTAG('e','l','s','t'), mov_read_elst },
  1535. { MKTAG('e','n','d','a'), mov_read_enda },
  1536. { MKTAG('f','i','e','l'), mov_read_extradata },
  1537. { MKTAG('f','t','y','p'), mov_read_ftyp },
  1538. { MKTAG('g','l','b','l'), mov_read_glbl },
  1539. { MKTAG('h','d','l','r'), mov_read_hdlr },
  1540. { MKTAG('i','l','s','t'), mov_read_ilst },
  1541. { MKTAG('j','p','2','h'), mov_read_extradata },
  1542. { MKTAG('m','d','a','t'), mov_read_mdat },
  1543. { MKTAG('m','d','h','d'), mov_read_mdhd },
  1544. { MKTAG('m','d','i','a'), mov_read_default },
  1545. { MKTAG('m','e','t','a'), mov_read_meta },
  1546. { MKTAG('m','i','n','f'), mov_read_default },
  1547. { MKTAG('m','o','o','f'), mov_read_moof },
  1548. { MKTAG('m','o','o','v'), mov_read_moov },
  1549. { MKTAG('m','v','e','x'), mov_read_default },
  1550. { MKTAG('m','v','h','d'), mov_read_mvhd },
  1551. { MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
  1552. { MKTAG('a','l','a','c'), mov_read_extradata }, /* alac specific atom */
  1553. { MKTAG('a','v','c','C'), mov_read_glbl },
  1554. { MKTAG('s','t','b','l'), mov_read_default },
  1555. { MKTAG('s','t','c','o'), mov_read_stco },
  1556. { MKTAG('s','t','s','c'), mov_read_stsc },
  1557. { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
  1558. { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
  1559. { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
  1560. { MKTAG('s','t','t','s'), mov_read_stts },
  1561. { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
  1562. { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
  1563. { MKTAG('t','r','a','k'), mov_read_trak },
  1564. { MKTAG('t','r','a','f'), mov_read_default },
  1565. { MKTAG('t','r','e','x'), mov_read_trex },
  1566. { MKTAG('t','r','k','n'), mov_read_trkn },
  1567. { MKTAG('t','r','u','n'), mov_read_trun },
  1568. { MKTAG('u','d','t','a'), mov_read_default },
  1569. { MKTAG('w','a','v','e'), mov_read_wave },
  1570. { MKTAG('e','s','d','s'), mov_read_esds },
  1571. { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
  1572. { MKTAG('c','m','o','v'), mov_read_cmov },
  1573. { MKTAG(0xa9,'n','a','m'), mov_read_udta_string },
  1574. { MKTAG(0xa9,'w','r','t'), mov_read_udta_string },
  1575. { MKTAG(0xa9,'c','p','y'), mov_read_udta_string },
  1576. { MKTAG(0xa9,'i','n','f'), mov_read_udta_string },
  1577. { MKTAG(0xa9,'i','n','f'), mov_read_udta_string },
  1578. { MKTAG(0xa9,'A','R','T'), mov_read_udta_string },
  1579. { MKTAG(0xa9,'a','l','b'), mov_read_udta_string },
  1580. { MKTAG(0xa9,'c','m','t'), mov_read_udta_string },
  1581. { 0, NULL }
  1582. };
  1583. static int mov_probe(AVProbeData *p)
  1584. {
  1585. unsigned int offset;
  1586. uint32_t tag;
  1587. int score = 0;
  1588. /* check file header */
  1589. offset = 0;
  1590. for(;;) {
  1591. /* ignore invalid offset */
  1592. if ((offset + 8) > (unsigned int)p->buf_size)
  1593. return score;
  1594. tag = AV_RL32(p->buf + offset + 4);
  1595. switch(tag) {
  1596. /* check for obvious tags */
  1597. case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
  1598. case MKTAG('m','o','o','v'):
  1599. case MKTAG('m','d','a','t'):
  1600. case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
  1601. case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
  1602. case MKTAG('f','t','y','p'):
  1603. return AVPROBE_SCORE_MAX;
  1604. /* those are more common words, so rate then a bit less */
  1605. case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
  1606. case MKTAG('w','i','d','e'):
  1607. case MKTAG('f','r','e','e'):
  1608. case MKTAG('j','u','n','k'):
  1609. case MKTAG('p','i','c','t'):
  1610. return AVPROBE_SCORE_MAX - 5;
  1611. case MKTAG(0x82,0x82,0x7f,0x7d):
  1612. case MKTAG('s','k','i','p'):
  1613. case MKTAG('u','u','i','d'):
  1614. case MKTAG('p','r','f','l'):
  1615. offset = AV_RB32(p->buf+offset) + offset;
  1616. /* if we only find those cause probedata is too small at least rate them */
  1617. score = AVPROBE_SCORE_MAX - 50;
  1618. break;
  1619. default:
  1620. /* unrecognized tag */
  1621. return score;
  1622. }
  1623. }
  1624. return score;
  1625. }
  1626. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1627. {
  1628. MOVContext *mov = s->priv_data;
  1629. ByteIOContext *pb = s->pb;
  1630. int err;
  1631. MOV_atom_t atom = { 0, 0, 0 };
  1632. mov->fc = s;
  1633. /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  1634. if(!url_is_streamed(pb))
  1635. atom.size = url_fsize(pb);
  1636. else
  1637. atom.size = INT64_MAX;
  1638. /* check MOV header */
  1639. if ((err = mov_read_default(mov, pb, atom)) < 0) {
  1640. av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
  1641. return err;
  1642. }
  1643. if (!mov->found_moov) {
  1644. av_log(s, AV_LOG_ERROR, "moov atom not found\n");
  1645. return -1;
  1646. }
  1647. dprintf(mov->fc, "on_parse_exit_offset=%lld\n", url_ftell(pb));
  1648. return 0;
  1649. }
  1650. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  1651. {
  1652. MOVContext *mov = s->priv_data;
  1653. MOVStreamContext *sc = 0;
  1654. AVIndexEntry *sample = 0;
  1655. int64_t best_dts = INT64_MAX;
  1656. int i;
  1657. retry:
  1658. for (i = 0; i < s->nb_streams; i++) {
  1659. AVStream *st = s->streams[i];
  1660. MOVStreamContext *msc = st->priv_data;
  1661. if (st->discard != AVDISCARD_ALL && msc->pb && msc->current_sample < msc->sample_count) {
  1662. AVIndexEntry *current_sample = &st->index_entries[msc->current_sample];
  1663. int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate,
  1664. AV_TIME_BASE, msc->time_scale);
  1665. dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  1666. if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) ||
  1667. (!url_is_streamed(s->pb) &&
  1668. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
  1669. ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  1670. (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
  1671. sample = current_sample;
  1672. best_dts = dts;
  1673. sc = msc;
  1674. }
  1675. }
  1676. }
  1677. if (!sample) {
  1678. mov->found_mdat = 0;
  1679. if (!url_is_streamed(s->pb) ||
  1680. mov_read_default(mov, s->pb, (MOV_atom_t){ 0, 0, INT64_MAX }) < 0 ||
  1681. url_feof(s->pb))
  1682. return -1;
  1683. dprintf(s, "read fragments, offset 0x%llx\n", url_ftell(s->pb));
  1684. goto retry;
  1685. }
  1686. /* must be done just before reading, to avoid infinite loop on sample */
  1687. sc->current_sample++;
  1688. if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  1689. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  1690. sc->ffindex, sample->pos);
  1691. return -1;
  1692. }
  1693. av_get_packet(sc->pb, pkt, sample->size);
  1694. #ifdef CONFIG_DV_DEMUXER
  1695. if (mov->dv_demux && sc->dv_audio_container) {
  1696. dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
  1697. av_free(pkt->data);
  1698. pkt->size = 0;
  1699. if (dv_get_packet(mov->dv_demux, pkt) < 0)
  1700. return -1;
  1701. }
  1702. #endif
  1703. pkt->stream_index = sc->ffindex;
  1704. pkt->dts = sample->timestamp;
  1705. if (sc->ctts_data) {
  1706. assert(sc->ctts_data[sc->sample_to_ctime_index].duration % sc->time_rate == 0);
  1707. pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate;
  1708. /* update ctts context */
  1709. sc->sample_to_ctime_sample++;
  1710. if (sc->sample_to_ctime_index < sc->ctts_count &&
  1711. sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) {
  1712. sc->sample_to_ctime_index++;
  1713. sc->sample_to_ctime_sample = 0;
  1714. }
  1715. if (sc->wrong_dts)
  1716. pkt->dts = AV_NOPTS_VALUE;
  1717. } else {
  1718. AVStream *st = s->streams[sc->ffindex];
  1719. int64_t next_dts = (sc->current_sample < sc->sample_count) ?
  1720. st->index_entries[sc->current_sample].timestamp : st->duration;
  1721. pkt->duration = next_dts - pkt->dts;
  1722. pkt->pts = pkt->dts;
  1723. }
  1724. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
  1725. pkt->pos = sample->pos;
  1726. dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
  1727. pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
  1728. return 0;
  1729. }
  1730. static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
  1731. {
  1732. MOVStreamContext *sc = st->priv_data;
  1733. int sample, time_sample;
  1734. int i;
  1735. sample = av_index_search_timestamp(st, timestamp, flags);
  1736. dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  1737. if (sample < 0) /* not sure what to do */
  1738. return -1;
  1739. sc->current_sample = sample;
  1740. dprintf(st->codec, "stream %d, found sample %d\n", st->index, sc->current_sample);
  1741. /* adjust ctts index */
  1742. if (sc->ctts_data) {
  1743. time_sample = 0;
  1744. for (i = 0; i < sc->ctts_count; i++) {
  1745. int next = time_sample + sc->ctts_data[i].count;
  1746. if (next > sc->current_sample) {
  1747. sc->sample_to_ctime_index = i;
  1748. sc->sample_to_ctime_sample = sc->current_sample - time_sample;
  1749. break;
  1750. }
  1751. time_sample = next;
  1752. }
  1753. }
  1754. return sample;
  1755. }
  1756. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  1757. {
  1758. AVStream *st;
  1759. int64_t seek_timestamp, timestamp;
  1760. int sample;
  1761. int i;
  1762. if (stream_index >= s->nb_streams)
  1763. return -1;
  1764. st = s->streams[stream_index];
  1765. sample = mov_seek_stream(st, sample_time, flags);
  1766. if (sample < 0)
  1767. return -1;
  1768. /* adjust seek timestamp to found sample timestamp */
  1769. seek_timestamp = st->index_entries[sample].timestamp;
  1770. for (i = 0; i < s->nb_streams; i++) {
  1771. st = s->streams[i];
  1772. if (stream_index == i || st->discard == AVDISCARD_ALL)
  1773. continue;
  1774. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  1775. mov_seek_stream(st, timestamp, flags);
  1776. }
  1777. return 0;
  1778. }
  1779. static int mov_read_close(AVFormatContext *s)
  1780. {
  1781. int i, j;
  1782. MOVContext *mov = s->priv_data;
  1783. for(i=0; i<s->nb_streams; i++) {
  1784. MOVStreamContext *sc = s->streams[i]->priv_data;
  1785. av_freep(&sc->ctts_data);
  1786. for (j=0; j<sc->drefs_count; j++)
  1787. av_freep(&sc->drefs[j].path);
  1788. av_freep(&sc->drefs);
  1789. if (sc->pb && sc->pb != s->pb)
  1790. url_fclose(sc->pb);
  1791. }
  1792. if(mov->dv_demux){
  1793. for(i=0; i<mov->dv_fctx->nb_streams; i++){
  1794. av_freep(&mov->dv_fctx->streams[i]->codec);
  1795. av_freep(&mov->dv_fctx->streams[i]);
  1796. }
  1797. av_freep(&mov->dv_fctx);
  1798. av_freep(&mov->dv_demux);
  1799. }
  1800. av_freep(&mov->trex_data);
  1801. return 0;
  1802. }
  1803. AVInputFormat mov_demuxer = {
  1804. "mov,mp4,m4a,3gp,3g2,mj2",
  1805. NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),
  1806. sizeof(MOVContext),
  1807. mov_probe,
  1808. mov_read_header,
  1809. mov_read_packet,
  1810. mov_read_close,
  1811. mov_read_seek,
  1812. };