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.

1642 lines
55KB

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