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.

2034 lines
71KB

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