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.

1926 lines
66KB

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