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.

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