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.

1656 lines
56KB

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