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.

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