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.

1920 lines
65KB

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