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.

1725 lines
58KB

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