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.

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