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.

1995 lines
69KB

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