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.

1997 lines
69KB

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