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.

1644 lines
55KB

  1. /*
  2. * MOV demuxer
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <limits.h>
  22. //#define DEBUG
  23. #include "avformat.h"
  24. #include "riff.h"
  25. #include "isom.h"
  26. #include "dv.h"
  27. #ifdef CONFIG_ZLIB
  28. #include <zlib.h>
  29. #endif
  30. /*
  31. * First version by Francois Revol revol@free.fr
  32. * Seek function by Gael Chardon gael.dev@4now.net
  33. *
  34. * Features and limitations:
  35. * - reads most of the QT files I have (at least the structure),
  36. * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
  37. * - the code is quite ugly... maybe I won't do it recursive next time :-)
  38. *
  39. * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
  40. * when coding this :) (it's a writer anyway)
  41. *
  42. * Reference documents:
  43. * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
  44. * Apple:
  45. * http://developer.apple.com/documentation/QuickTime/QTFF/
  46. * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
  47. * QuickTime is a trademark of Apple (AFAIK :))
  48. */
  49. #include "qtpalette.h"
  50. #undef NDEBUG
  51. #include <assert.h>
  52. /* the QuickTime file format is quite convoluted...
  53. * it has lots of index tables, each indexing something in another one...
  54. * Here we just use what is needed to read the chunks
  55. */
  56. typedef struct {
  57. int first;
  58. int count;
  59. int id;
  60. } MOV_stsc_t;
  61. typedef struct {
  62. uint32_t type;
  63. int64_t offset;
  64. int64_t size; /* total size (excluding the size and type fields) */
  65. } MOV_atom_t;
  66. 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. } MOVStreamContext;
  97. typedef struct MOVContext {
  98. AVFormatContext *fc;
  99. int time_scale;
  100. int64_t duration; /* duration of the longest track */
  101. int found_moov; /* when both 'moov' and 'mdat' sections has been found */
  102. int found_mdat; /* we suppose we have enough data to read the file */
  103. int64_t mdat_offset;
  104. int total_streams;
  105. MOVStreamContext *streams[MAX_STREAMS];
  106. const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
  107. /* NOTE: for recursion save to/ restore from local variable! */
  108. AVPaletteControl palette_control;
  109. MOV_mdat_t *mdat_list;
  110. int mdat_count;
  111. DVDemuxContext *dv_demux;
  112. AVFormatContext *dv_fctx;
  113. int isom; /* 1 if file is ISO Media (mp4/3gp) */
  114. } MOVContext;
  115. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  116. /* those functions parse an atom */
  117. /* return code:
  118. 1: found what I wanted, exit
  119. 0: continue to parse next atom
  120. -1: error occured, exit
  121. */
  122. typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
  123. /* links atom IDs to parse functions */
  124. typedef struct MOVParseTableEntry {
  125. uint32_t type;
  126. mov_parse_function func;
  127. } MOVParseTableEntry;
  128. static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  129. {
  130. int64_t total_size = 0;
  131. MOV_atom_t a;
  132. int i;
  133. int err = 0;
  134. a.offset = atom.offset;
  135. if (atom.size < 0)
  136. atom.size = INT64_MAX;
  137. while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
  138. a.size = atom.size;
  139. a.type=0L;
  140. if(atom.size >= 8) {
  141. a.size = get_be32(pb);
  142. a.type = get_le32(pb);
  143. }
  144. total_size += 8;
  145. a.offset += 8;
  146. dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n", a.type, (char*)&a.type, a.size, atom.size, total_size);
  147. if (a.size == 1) { /* 64 bit extended size */
  148. a.size = get_be64(pb) - 8;
  149. a.offset += 8;
  150. total_size += 8;
  151. }
  152. if (a.size == 0) {
  153. a.size = atom.size - total_size;
  154. if (a.size <= 8)
  155. break;
  156. }
  157. a.size -= 8;
  158. if(a.size < 0 || a.size > atom.size - total_size)
  159. break;
  160. for (i = 0; c->parse_table[i].type != 0L
  161. && c->parse_table[i].type != a.type; i++)
  162. /* empty */;
  163. if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
  164. url_fskip(pb, a.size);
  165. } else {
  166. offset_t start_pos = url_ftell(pb);
  167. int64_t left;
  168. err = (c->parse_table[i].func)(c, pb, a);
  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], ((char *)&ctype)[2], ((char *)&ctype)[3], (int) ctype);
  192. dprintf(c->fc, "stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
  193. if(!ctype)
  194. c->isom = 1;
  195. if(type == MKTAG('v', 'i', 'd', 'e'))
  196. st->codec->codec_type = CODEC_TYPE_VIDEO;
  197. else if(type == MKTAG('s', 'o', 'u', 'n'))
  198. st->codec->codec_type = CODEC_TYPE_AUDIO;
  199. else if(type == MKTAG('m', '1', 'a', ' '))
  200. st->codec->codec_id = CODEC_ID_MP2;
  201. else if(type == MKTAG('s', 'u', 'b', 'p')) {
  202. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  203. st->codec->codec_id = CODEC_ID_DVD_SUBTITLE;
  204. }
  205. get_be32(pb); /* component manufacture */
  206. get_be32(pb); /* component flags */
  207. get_be32(pb); /* component flags mask */
  208. if(atom.size <= 24)
  209. return 0; /* nothing left to read */
  210. url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
  211. return 0;
  212. }
  213. static int mp4_read_descr_len(ByteIOContext *pb)
  214. {
  215. int len = 0;
  216. int count = 4;
  217. while (count--) {
  218. int c = get_byte(pb);
  219. len = (len << 7) | (c & 0x7f);
  220. if (!(c & 0x80))
  221. break;
  222. }
  223. return len;
  224. }
  225. static int mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag)
  226. {
  227. int len;
  228. *tag = get_byte(pb);
  229. len = mp4_read_descr_len(pb);
  230. dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
  231. return len;
  232. }
  233. #define MP4ESDescrTag 0x03
  234. #define MP4DecConfigDescrTag 0x04
  235. #define MP4DecSpecificDescrTag 0x05
  236. static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  237. {
  238. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  239. int tag, len;
  240. get_be32(pb); /* version + flags */
  241. len = mp4_read_descr(c, pb, &tag);
  242. if (tag == MP4ESDescrTag) {
  243. get_be16(pb); /* ID */
  244. get_byte(pb); /* priority */
  245. } else
  246. get_be16(pb); /* ID */
  247. len = mp4_read_descr(c, pb, &tag);
  248. if (tag == MP4DecConfigDescrTag) {
  249. int object_type_id = get_byte(pb);
  250. get_byte(pb); /* stream type */
  251. get_be24(pb); /* buffer size db */
  252. get_be32(pb); /* max bitrate */
  253. get_be32(pb); /* avg bitrate */
  254. st->codec->codec_id= codec_get_id(ff_mp4_obj_type, object_type_id);
  255. dprintf(c->fc, "esds object type id %d\n", object_type_id);
  256. len = mp4_read_descr(c, pb, &tag);
  257. if (tag == MP4DecSpecificDescrTag) {
  258. dprintf(c->fc, "Specific MPEG4 header len=%d\n", len);
  259. st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
  260. if (st->codec->extradata) {
  261. get_buffer(pb, st->codec->extradata, len);
  262. st->codec->extradata_size = len;
  263. /* from mplayer */
  264. if ((*st->codec->extradata >> 3) == 29) {
  265. st->codec->codec_id = CODEC_ID_MP3ON4;
  266. }
  267. }
  268. }
  269. }
  270. return 0;
  271. }
  272. /* this atom contains actual media data */
  273. static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  274. {
  275. if(atom.size == 0) /* wrong one (MP4) */
  276. return 0;
  277. c->mdat_list = av_realloc(c->mdat_list, (c->mdat_count + 1) * sizeof(*c->mdat_list));
  278. c->mdat_list[c->mdat_count].offset = atom.offset;
  279. c->mdat_list[c->mdat_count].size = atom.size;
  280. c->mdat_count++;
  281. c->found_mdat=1;
  282. c->mdat_offset = atom.offset;
  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. int err;
  302. err = mov_read_default(c, pb, atom);
  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. mov_read_default(c, pb, atom);
  432. } else
  433. url_fskip(pb, atom.size);
  434. return 0;
  435. }
  436. static int mov_read_avcC(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  437. {
  438. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  439. if((uint64_t)atom.size > (1<<30))
  440. return -1;
  441. av_free(st->codec->extradata);
  442. st->codec->extradata_size = atom.size;
  443. st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  444. if (st->codec->extradata) {
  445. get_buffer(pb, st->codec->extradata, atom.size);
  446. } else
  447. url_fskip(pb, atom.size);
  448. return 0;
  449. }
  450. static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  451. {
  452. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  453. MOVStreamContext *sc = st->priv_data;
  454. unsigned int i, entries;
  455. get_byte(pb); /* version */
  456. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  457. entries = get_be32(pb);
  458. if(entries >= UINT_MAX/sizeof(int64_t))
  459. return -1;
  460. sc->chunk_count = entries;
  461. sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
  462. if (!sc->chunk_offsets)
  463. return -1;
  464. if (atom.type == MKTAG('s', 't', 'c', 'o')) {
  465. for(i=0; i<entries; i++) {
  466. sc->chunk_offsets[i] = get_be32(pb);
  467. }
  468. } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
  469. for(i=0; i<entries; i++) {
  470. sc->chunk_offsets[i] = get_be64(pb);
  471. }
  472. } else
  473. return -1;
  474. return 0;
  475. }
  476. static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  477. {
  478. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  479. MOVStreamContext *sc = st->priv_data;
  480. int entries, frames_per_sample;
  481. uint32_t format;
  482. uint8_t codec_name[32];
  483. /* for palette traversal */
  484. int color_depth;
  485. int color_start;
  486. int color_count;
  487. int color_end;
  488. int color_index;
  489. int color_dec;
  490. int color_greyscale;
  491. unsigned char *color_table;
  492. int j;
  493. unsigned char r, g, b;
  494. get_byte(pb); /* version */
  495. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  496. entries = get_be32(pb);
  497. while(entries--) { //Parsing Sample description table
  498. enum CodecID id;
  499. MOV_atom_t a = { 0, 0, 0 };
  500. offset_t start_pos = url_ftell(pb);
  501. int size = get_be32(pb); /* size */
  502. format = get_le32(pb); /* data format */
  503. get_be32(pb); /* reserved */
  504. get_be16(pb); /* reserved */
  505. get_be16(pb); /* index */
  506. if (st->codec->codec_tag) {
  507. /* multiple fourcc, just skip for now */
  508. url_fskip(pb, size - (url_ftell(pb) - start_pos));
  509. continue;
  510. }
  511. st->codec->codec_tag = format;
  512. id = codec_get_id(codec_movaudio_tags, format);
  513. if (id<=0 && (format&0xFFFF) == 'm' + ('s'<<8))
  514. id = codec_get_id(codec_wav_tags, bswap_32(format)&0xFFFF);
  515. if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
  516. st->codec->codec_type = CODEC_TYPE_AUDIO;
  517. } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
  518. format && format != MKTAG('m', 'p', '4', 's')) { /* skip old asf mpeg4 tag */
  519. id = codec_get_id(codec_movvideo_tags, format);
  520. if (id <= 0)
  521. id = codec_get_id(codec_bmp_tags, format);
  522. if (id > 0)
  523. st->codec->codec_type = CODEC_TYPE_VIDEO;
  524. }
  525. dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n",
  526. size,
  527. (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, (format >> 24) & 0xff,
  528. st->codec->codec_type);
  529. if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
  530. st->codec->codec_id = id;
  531. get_be16(pb); /* version */
  532. get_be16(pb); /* revision level */
  533. get_be32(pb); /* vendor */
  534. get_be32(pb); /* temporal quality */
  535. get_be32(pb); /* spacial quality */
  536. st->codec->width = get_be16(pb); /* width */
  537. st->codec->height = get_be16(pb); /* height */
  538. get_be32(pb); /* horiz resolution */
  539. get_be32(pb); /* vert resolution */
  540. get_be32(pb); /* data size, always 0 */
  541. frames_per_sample = get_be16(pb); /* frames per samples */
  542. dprintf(c->fc, "frames/samples = %d\n", frames_per_sample);
  543. get_buffer(pb, codec_name, 32); /* codec name, pascal string (FIXME: true for mp4?) */
  544. if (codec_name[0] <= 31) {
  545. memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
  546. st->codec->codec_name[codec_name[0]] = 0;
  547. }
  548. st->codec->bits_per_sample = get_be16(pb); /* depth */
  549. st->codec->color_table_id = get_be16(pb); /* colortable id */
  550. /* figure out the palette situation */
  551. color_depth = st->codec->bits_per_sample & 0x1F;
  552. color_greyscale = st->codec->bits_per_sample & 0x20;
  553. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  554. if ((color_depth == 2) || (color_depth == 4) ||
  555. (color_depth == 8)) {
  556. if (color_greyscale) {
  557. /* compute the greyscale palette */
  558. color_count = 1 << color_depth;
  559. color_index = 255;
  560. color_dec = 256 / (color_count - 1);
  561. for (j = 0; j < color_count; j++) {
  562. r = g = b = color_index;
  563. c->palette_control.palette[j] =
  564. (r << 16) | (g << 8) | (b);
  565. color_index -= color_dec;
  566. if (color_index < 0)
  567. color_index = 0;
  568. }
  569. } else if (st->codec->color_table_id & 0x08) {
  570. /* if flag bit 3 is set, use the default palette */
  571. color_count = 1 << color_depth;
  572. if (color_depth == 2)
  573. color_table = ff_qt_default_palette_4;
  574. else if (color_depth == 4)
  575. color_table = ff_qt_default_palette_16;
  576. else
  577. color_table = ff_qt_default_palette_256;
  578. for (j = 0; j < color_count; j++) {
  579. r = color_table[j * 4 + 0];
  580. g = color_table[j * 4 + 1];
  581. b = color_table[j * 4 + 2];
  582. c->palette_control.palette[j] =
  583. (r << 16) | (g << 8) | (b);
  584. }
  585. } else {
  586. /* load the palette from the file */
  587. color_start = get_be32(pb);
  588. color_count = get_be16(pb);
  589. color_end = get_be16(pb);
  590. for (j = color_start; j <= color_end; j++) {
  591. /* each R, G, or B component is 16 bits;
  592. * only use the top 8 bits; skip alpha bytes
  593. * up front */
  594. get_byte(pb);
  595. get_byte(pb);
  596. r = get_byte(pb);
  597. get_byte(pb);
  598. g = get_byte(pb);
  599. get_byte(pb);
  600. b = get_byte(pb);
  601. get_byte(pb);
  602. c->palette_control.palette[j] =
  603. (r << 16) | (g << 8) | (b);
  604. }
  605. }
  606. st->codec->palctrl = &c->palette_control;
  607. st->codec->palctrl->palette_changed = 1;
  608. } else
  609. st->codec->palctrl = NULL;
  610. } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
  611. int bits_per_sample;
  612. uint16_t version = get_be16(pb);
  613. st->codec->codec_id = id;
  614. get_be16(pb); /* revision level */
  615. get_be32(pb); /* vendor */
  616. st->codec->channels = get_be16(pb); /* channel count */
  617. dprintf(c->fc, "audio channels %d\n", st->codec->channels);
  618. st->codec->bits_per_sample = get_be16(pb); /* sample size */
  619. /* do we need to force to 16 for AMR ? */
  620. /* handle specific s8 codec */
  621. get_be16(pb); /* compression id = 0*/
  622. get_be16(pb); /* packet size = 0 */
  623. st->codec->sample_rate = ((get_be32(pb) >> 16));
  624. switch (st->codec->codec_id) {
  625. case CODEC_ID_PCM_S8:
  626. case CODEC_ID_PCM_U8:
  627. if (st->codec->bits_per_sample == 16)
  628. st->codec->codec_id = CODEC_ID_PCM_S16BE;
  629. break;
  630. case CODEC_ID_PCM_S16LE:
  631. case CODEC_ID_PCM_S16BE:
  632. if (st->codec->bits_per_sample == 8)
  633. st->codec->codec_id = CODEC_ID_PCM_S8;
  634. else if (st->codec->bits_per_sample == 24)
  635. st->codec->codec_id = CODEC_ID_PCM_S24BE;
  636. break;
  637. default:
  638. break;
  639. }
  640. //Read QT version 1 fields. In version 0 these do not exist.
  641. dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
  642. if(!c->isom) {
  643. if(version==1) {
  644. sc->samples_per_frame = get_be32(pb);
  645. get_be32(pb); /* bytes per packet */
  646. sc->bytes_per_frame = get_be32(pb);
  647. get_be32(pb); /* bytes per sample */
  648. } else if(version==2) {
  649. get_be32(pb); /* sizeof struct only */
  650. st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
  651. st->codec->channels = get_be32(pb);
  652. get_be32(pb); /* always 0x7F000000 */
  653. get_be32(pb); /* bits per channel if sound is uncompressed */
  654. get_be32(pb); /* lcpm format specific flag */
  655. get_be32(pb); /* bytes per audio packet if constant */
  656. get_be32(pb); /* lpcm frames per audio packet if constant */
  657. }
  658. }
  659. bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
  660. if (bits_per_sample) {
  661. st->codec->bits_per_sample = bits_per_sample;
  662. sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
  663. }
  664. } else {
  665. /* other codec type, just skip (rtp, mp4s, tmcd ...) */
  666. url_fskip(pb, size - (url_ftell(pb) - start_pos));
  667. }
  668. /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
  669. a.size = size - (url_ftell(pb) - start_pos);
  670. if (a.size > 8)
  671. mov_read_default(c, pb, a);
  672. else if (a.size > 0)
  673. url_fskip(pb, a.size);
  674. }
  675. if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1) {
  676. st->codec->sample_rate= sc->time_scale;
  677. }
  678. /* special codec parameters handling */
  679. switch (st->codec->codec_id) {
  680. #ifdef CONFIG_H261_DECODER
  681. case CODEC_ID_H261:
  682. #endif
  683. #ifdef CONFIG_H263_DECODER
  684. case CODEC_ID_H263:
  685. #endif
  686. #ifdef CONFIG_MPEG4_DECODER
  687. case CODEC_ID_MPEG4:
  688. #endif
  689. st->codec->width= 0; /* let decoder init width/height */
  690. st->codec->height= 0;
  691. break;
  692. #ifdef CONFIG_LIBFAAD
  693. case CODEC_ID_AAC:
  694. #endif
  695. #ifdef CONFIG_VORBIS_DECODER
  696. case CODEC_ID_VORBIS:
  697. #endif
  698. case CODEC_ID_MP3ON4:
  699. st->codec->sample_rate= 0; /* let decoder init parameters properly */
  700. break;
  701. #ifdef CONFIG_DV_DEMUXER
  702. case CODEC_ID_DVAUDIO:
  703. c->dv_fctx = av_alloc_format_context();
  704. c->dv_demux = dv_init_demux(c->dv_fctx);
  705. if (!c->dv_demux) {
  706. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  707. return -1;
  708. }
  709. sc->dv_audio_container = 1;
  710. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  711. break;
  712. #endif
  713. /* no ifdef since parameters are always those */
  714. case CODEC_ID_AMR_WB:
  715. st->codec->sample_rate= 16000;
  716. st->codec->channels= 1; /* really needed */
  717. break;
  718. case CODEC_ID_AMR_NB:
  719. st->codec->sample_rate= 8000;
  720. st->codec->channels= 1; /* really needed */
  721. break;
  722. case CODEC_ID_MP2:
  723. case CODEC_ID_MP3:
  724. st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
  725. st->need_parsing = AVSTREAM_PARSE_FULL;
  726. break;
  727. case CODEC_ID_ADPCM_MS:
  728. case CODEC_ID_ADPCM_IMA_WAV:
  729. st->codec->block_align = sc->bytes_per_frame;
  730. break;
  731. default:
  732. break;
  733. }
  734. return 0;
  735. }
  736. static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  737. {
  738. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  739. MOVStreamContext *sc = st->priv_data;
  740. unsigned int i, entries;
  741. get_byte(pb); /* version */
  742. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  743. entries = get_be32(pb);
  744. if(entries >= UINT_MAX / sizeof(MOV_stsc_t))
  745. return -1;
  746. dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  747. sc->sample_to_chunk_sz = entries;
  748. sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_stsc_t));
  749. if (!sc->sample_to_chunk)
  750. return -1;
  751. for(i=0; i<entries; i++) {
  752. sc->sample_to_chunk[i].first = get_be32(pb);
  753. sc->sample_to_chunk[i].count = get_be32(pb);
  754. sc->sample_to_chunk[i].id = get_be32(pb);
  755. }
  756. return 0;
  757. }
  758. static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  759. {
  760. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  761. MOVStreamContext *sc = st->priv_data;
  762. unsigned int i, entries;
  763. get_byte(pb); /* version */
  764. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  765. entries = get_be32(pb);
  766. if(entries >= UINT_MAX / sizeof(int))
  767. return -1;
  768. sc->keyframe_count = entries;
  769. dprintf(c->fc, "keyframe_count = %d\n", sc->keyframe_count);
  770. sc->keyframes = av_malloc(entries * sizeof(int));
  771. if (!sc->keyframes)
  772. return -1;
  773. for(i=0; i<entries; i++) {
  774. sc->keyframes[i] = get_be32(pb);
  775. //dprintf(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
  776. }
  777. return 0;
  778. }
  779. static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  780. {
  781. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  782. MOVStreamContext *sc = st->priv_data;
  783. unsigned int i, entries, sample_size;
  784. get_byte(pb); /* version */
  785. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  786. sample_size = get_be32(pb);
  787. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  788. sc->sample_size = sample_size;
  789. entries = get_be32(pb);
  790. if(entries >= UINT_MAX / sizeof(int))
  791. return -1;
  792. sc->sample_count = entries;
  793. if (sample_size)
  794. return 0;
  795. dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, sc->sample_count);
  796. sc->sample_sizes = av_malloc(entries * sizeof(int));
  797. if (!sc->sample_sizes)
  798. return -1;
  799. for(i=0; i<entries; i++) {
  800. sc->sample_sizes[i] = get_be32(pb);
  801. dprintf(c->fc, "sample_sizes[]=%d\n", sc->sample_sizes[i]);
  802. }
  803. return 0;
  804. }
  805. static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  806. {
  807. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  808. MOVStreamContext *sc = st->priv_data;
  809. unsigned int i, entries;
  810. int64_t duration=0;
  811. int64_t total_sample_count=0;
  812. get_byte(pb); /* version */
  813. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  814. entries = get_be32(pb);
  815. if(entries >= UINT_MAX / sizeof(MOV_stts_t))
  816. return -1;
  817. sc->stts_count = entries;
  818. sc->stts_data = av_malloc(entries * sizeof(MOV_stts_t));
  819. dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  820. sc->time_rate=0;
  821. for(i=0; i<entries; i++) {
  822. int sample_duration;
  823. int sample_count;
  824. sample_count=get_be32(pb);
  825. sample_duration = get_be32(pb);
  826. sc->stts_data[i].count= sample_count;
  827. sc->stts_data[i].duration= sample_duration;
  828. sc->time_rate= ff_gcd(sc->time_rate, sample_duration);
  829. dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  830. duration+=(int64_t)sample_duration*sample_count;
  831. total_sample_count+=sample_count;
  832. }
  833. st->nb_frames= total_sample_count;
  834. if(duration)
  835. st->duration= duration;
  836. return 0;
  837. }
  838. static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  839. {
  840. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  841. MOVStreamContext *sc = st->priv_data;
  842. unsigned int i, entries;
  843. get_byte(pb); /* version */
  844. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  845. entries = get_be32(pb);
  846. if(entries >= UINT_MAX / sizeof(MOV_stts_t))
  847. return -1;
  848. sc->ctts_count = entries;
  849. sc->ctts_data = av_malloc(entries * sizeof(MOV_stts_t));
  850. dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  851. for(i=0; i<entries; i++) {
  852. int count =get_be32(pb);
  853. int duration =get_be32(pb);
  854. if (duration < 0) {
  855. av_log(c->fc, AV_LOG_ERROR, "negative ctts, ignoring\n");
  856. sc->ctts_count = 0;
  857. url_fskip(pb, 8 * (entries - i - 1));
  858. break;
  859. }
  860. sc->ctts_data[i].count = count;
  861. sc->ctts_data[i].duration= duration;
  862. sc->time_rate= ff_gcd(sc->time_rate, duration);
  863. }
  864. return 0;
  865. }
  866. static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  867. {
  868. AVStream *st;
  869. MOVStreamContext *sc;
  870. st = av_new_stream(c->fc, c->fc->nb_streams);
  871. if (!st) return -2;
  872. sc = av_mallocz(sizeof(MOVStreamContext));
  873. if (!sc) {
  874. av_free(st);
  875. return -1;
  876. }
  877. st->priv_data = sc;
  878. st->codec->codec_type = CODEC_TYPE_DATA;
  879. st->start_time = 0; /* XXX: check */
  880. c->streams[c->fc->nb_streams-1] = sc;
  881. return mov_read_default(c, pb, atom);
  882. }
  883. static void mov_parse_udta_string(ByteIOContext *pb, char *str, int size)
  884. {
  885. uint16_t str_size = get_be16(pb); /* string length */;
  886. get_be16(pb); /* skip language */
  887. get_buffer(pb, str, FFMIN(size, str_size));
  888. }
  889. static int mov_read_udta(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  890. {
  891. uint64_t end = url_ftell(pb) + atom.size;
  892. while (url_ftell(pb) + 8 < end) {
  893. uint32_t tag_size = get_be32(pb);
  894. uint32_t tag = get_le32(pb);
  895. uint64_t next = url_ftell(pb) + tag_size - 8;
  896. if (next > end) // stop if tag_size is wrong
  897. break;
  898. switch (tag) {
  899. case MKTAG(0xa9,'n','a','m'):
  900. mov_parse_udta_string(pb, c->fc->title, sizeof(c->fc->title));
  901. break;
  902. case MKTAG(0xa9,'w','r','t'):
  903. mov_parse_udta_string(pb, c->fc->author, sizeof(c->fc->author));
  904. break;
  905. case MKTAG(0xa9,'c','p','y'):
  906. mov_parse_udta_string(pb, c->fc->copyright, sizeof(c->fc->copyright));
  907. break;
  908. case MKTAG(0xa9,'i','n','f'):
  909. mov_parse_udta_string(pb, c->fc->comment, sizeof(c->fc->comment));
  910. break;
  911. default:
  912. break;
  913. }
  914. url_fseek(pb, next, SEEK_SET);
  915. }
  916. return 0;
  917. }
  918. static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  919. {
  920. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  921. int version = get_byte(pb);
  922. get_byte(pb); get_byte(pb);
  923. get_byte(pb); /* flags */
  924. /*
  925. MOV_TRACK_ENABLED 0x0001
  926. MOV_TRACK_IN_MOVIE 0x0002
  927. MOV_TRACK_IN_PREVIEW 0x0004
  928. MOV_TRACK_IN_POSTER 0x0008
  929. */
  930. if (version == 1) {
  931. get_be64(pb);
  932. get_be64(pb);
  933. } else {
  934. get_be32(pb); /* creation time */
  935. get_be32(pb); /* modification time */
  936. }
  937. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  938. get_be32(pb); /* reserved */
  939. st->start_time = 0; /* check */
  940. (version == 1) ? get_be64(pb) : get_be32(pb); /* highlevel (considering edits) duration in movie timebase */
  941. get_be32(pb); /* reserved */
  942. get_be32(pb); /* reserved */
  943. get_be16(pb); /* layer */
  944. get_be16(pb); /* alternate group */
  945. get_be16(pb); /* volume */
  946. get_be16(pb); /* reserved */
  947. url_fskip(pb, 36); /* display matrix */
  948. /* those are fixed-point */
  949. get_be32(pb); /* track width */
  950. get_be32(pb); /* track height */
  951. return 0;
  952. }
  953. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  954. /* like the files created with Adobe Premiere 5.0, for samples see */
  955. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  956. static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  957. {
  958. int err;
  959. if (atom.size < 8)
  960. return 0; /* continue */
  961. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  962. url_fskip(pb, atom.size - 4);
  963. return 0;
  964. }
  965. atom.type = get_le32(pb);
  966. atom.offset += 8;
  967. atom.size -= 8;
  968. if (atom.type != MKTAG('m', 'd', 'a', 't')) {
  969. url_fskip(pb, atom.size);
  970. return 0;
  971. }
  972. err = mov_read_mdat(c, pb, atom);
  973. return err;
  974. }
  975. static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  976. {
  977. #ifdef CONFIG_ZLIB
  978. ByteIOContext ctx;
  979. uint8_t *cmov_data;
  980. uint8_t *moov_data; /* uncompressed data */
  981. long cmov_len, moov_len;
  982. int ret;
  983. get_be32(pb); /* dcom atom */
  984. if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
  985. return -1;
  986. if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
  987. av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !");
  988. return -1;
  989. }
  990. get_be32(pb); /* cmvd atom */
  991. if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
  992. return -1;
  993. moov_len = get_be32(pb); /* uncompressed size */
  994. cmov_len = atom.size - 6 * 4;
  995. cmov_data = av_malloc(cmov_len);
  996. if (!cmov_data)
  997. return -1;
  998. moov_data = av_malloc(moov_len);
  999. if (!moov_data) {
  1000. av_free(cmov_data);
  1001. return -1;
  1002. }
  1003. get_buffer(pb, cmov_data, cmov_len);
  1004. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1005. return -1;
  1006. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  1007. return -1;
  1008. atom.type = MKTAG( 'm', 'o', 'o', 'v' );
  1009. atom.offset = 0;
  1010. atom.size = moov_len;
  1011. #ifdef DEBUG
  1012. // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
  1013. #endif
  1014. ret = mov_read_default(c, &ctx, atom);
  1015. av_free(moov_data);
  1016. av_free(cmov_data);
  1017. return ret;
  1018. #else
  1019. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  1020. return -1;
  1021. #endif
  1022. }
  1023. /* edit list atom */
  1024. static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1025. {
  1026. int i, edit_count;
  1027. get_byte(pb); /* version */
  1028. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1029. edit_count= c->streams[c->fc->nb_streams-1]->edit_count = get_be32(pb); /* entries */
  1030. for(i=0; i<edit_count; i++){
  1031. get_be32(pb); /* Track duration */
  1032. get_be32(pb); /* Media time */
  1033. get_be32(pb); /* Media rate */
  1034. }
  1035. dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, c->streams[c->fc->nb_streams-1]->edit_count);
  1036. return 0;
  1037. }
  1038. static const MOVParseTableEntry mov_default_parse_table[] = {
  1039. /* mp4 atoms */
  1040. { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
  1041. { MKTAG( 'c', 't', 't', 's' ), mov_read_ctts }, /* composition time to sample */
  1042. { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
  1043. { MKTAG( 'e', 'l', 's', 't' ), mov_read_elst },
  1044. { MKTAG( 'e', 'n', 'd', 'a' ), mov_read_enda },
  1045. { MKTAG( 'f', 'i', 'e', 'l' ), mov_read_extradata },
  1046. { MKTAG( 'f', 't', 'y', 'p' ), mov_read_ftyp },
  1047. { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
  1048. { MKTAG( 'j', 'p', '2', 'h' ), mov_read_extradata },
  1049. { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
  1050. { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
  1051. { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
  1052. { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
  1053. { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
  1054. { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
  1055. { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorenson extension ??? */
  1056. { MKTAG( 'a', 'l', 'a', 'c' ), mov_read_extradata }, /* alac specific atom */
  1057. { MKTAG( 'a', 'v', 'c', 'C' ), mov_read_avcC },
  1058. { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
  1059. { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
  1060. { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
  1061. { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
  1062. { MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */
  1063. { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
  1064. { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
  1065. { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
  1066. { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
  1067. { MKTAG( 'u', 'd', 't', 'a' ), mov_read_udta },
  1068. { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_wave },
  1069. { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
  1070. { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
  1071. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
  1072. { 0L, NULL }
  1073. };
  1074. /* XXX: is it sufficient ? */
  1075. static int mov_probe(AVProbeData *p)
  1076. {
  1077. unsigned int offset;
  1078. uint32_t tag;
  1079. int score = 0;
  1080. /* check file header */
  1081. offset = 0;
  1082. for(;;) {
  1083. /* ignore invalid offset */
  1084. if ((offset + 8) > (unsigned int)p->buf_size)
  1085. return score;
  1086. tag = AV_RL32(p->buf + offset + 4);
  1087. switch(tag) {
  1088. /* check for obvious tags */
  1089. case MKTAG( 'j', 'P', ' ', ' ' ): /* jpeg 2000 signature */
  1090. case MKTAG( 'm', 'o', 'o', 'v' ):
  1091. case MKTAG( 'm', 'd', 'a', 't' ):
  1092. case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
  1093. case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
  1094. return AVPROBE_SCORE_MAX;
  1095. /* those are more common words, so rate then a bit less */
  1096. case MKTAG( 'e', 'd', 'i', 'w' ): /* xdcam files have reverted first tags */
  1097. case MKTAG( 'w', 'i', 'd', 'e' ):
  1098. case MKTAG( 'f', 'r', 'e', 'e' ):
  1099. case MKTAG( 'j', 'u', 'n', 'k' ):
  1100. case MKTAG( 'p', 'i', 'c', 't' ):
  1101. return AVPROBE_SCORE_MAX - 5;
  1102. case MKTAG( 'f', 't', 'y', 'p' ):
  1103. case MKTAG( 's', 'k', 'i', 'p' ):
  1104. case MKTAG( 'u', 'u', 'i', 'd' ):
  1105. offset = AV_RB32(p->buf+offset) + offset;
  1106. /* if we only find those cause probedata is too small at least rate them */
  1107. score = AVPROBE_SCORE_MAX - 50;
  1108. break;
  1109. default:
  1110. /* unrecognized tag */
  1111. return score;
  1112. }
  1113. }
  1114. return score;
  1115. }
  1116. static void mov_build_index(MOVContext *mov, AVStream *st)
  1117. {
  1118. MOVStreamContext *sc = st->priv_data;
  1119. offset_t current_offset;
  1120. int64_t current_dts = 0;
  1121. unsigned int stts_index = 0;
  1122. unsigned int stsc_index = 0;
  1123. unsigned int stss_index = 0;
  1124. unsigned int i, j, k;
  1125. if (sc->sample_sizes || st->codec->codec_type == CODEC_TYPE_VIDEO || sc->dv_audio_container) {
  1126. unsigned int current_sample = 0;
  1127. unsigned int stts_sample = 0;
  1128. unsigned int keyframe, sample_size;
  1129. unsigned int distance = 0;
  1130. st->nb_frames = sc->sample_count;
  1131. for (i = 0; i < sc->chunk_count; i++) {
  1132. current_offset = sc->chunk_offsets[i];
  1133. if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
  1134. stsc_index++;
  1135. for (j = 0; j < sc->sample_to_chunk[stsc_index].count; j++) {
  1136. if (current_sample >= sc->sample_count) {
  1137. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  1138. goto out;
  1139. }
  1140. keyframe = !sc->keyframe_count || current_sample + 1 == sc->keyframes[stss_index];
  1141. if (keyframe) {
  1142. distance = 0;
  1143. if (stss_index + 1 < sc->keyframe_count)
  1144. stss_index++;
  1145. }
  1146. sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
  1147. dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", size %d, distance %d, keyframe %d\n",
  1148. st->index, current_sample, current_offset, current_dts, sample_size, distance, keyframe);
  1149. av_add_index_entry(st, current_offset, current_dts, sample_size, distance, keyframe ? AVINDEX_KEYFRAME : 0);
  1150. current_offset += sample_size;
  1151. assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
  1152. current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
  1153. distance++;
  1154. stts_sample++;
  1155. current_sample++;
  1156. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  1157. stts_sample = 0;
  1158. stts_index++;
  1159. }
  1160. }
  1161. }
  1162. } else { /* read whole chunk */
  1163. unsigned int chunk_samples, chunk_size, chunk_duration;
  1164. for (i = 0; i < sc->chunk_count; i++) {
  1165. current_offset = sc->chunk_offsets[i];
  1166. if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
  1167. stsc_index++;
  1168. chunk_samples = sc->sample_to_chunk[stsc_index].count;
  1169. /* get chunk size */
  1170. if (sc->sample_size > 1 || st->codec->codec_id == CODEC_ID_PCM_U8 || st->codec->codec_id == CODEC_ID_PCM_S8)
  1171. chunk_size = chunk_samples * sc->sample_size;
  1172. else if (sc->samples_per_frame > 0 && (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0))
  1173. chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
  1174. else { /* workaround to find nearest next chunk offset */
  1175. chunk_size = INT_MAX;
  1176. for (j = 0; j < mov->total_streams; j++) {
  1177. MOVStreamContext *msc = mov->streams[j];
  1178. for (k = msc->next_chunk; k < msc->chunk_count; k++) {
  1179. if (msc->chunk_offsets[k] > current_offset && msc->chunk_offsets[k] - current_offset < chunk_size) {
  1180. chunk_size = msc->chunk_offsets[k] - current_offset;
  1181. msc->next_chunk = k;
  1182. break;
  1183. }
  1184. }
  1185. }
  1186. /* check for last chunk */
  1187. if (chunk_size == INT_MAX)
  1188. for (j = 0; j < mov->mdat_count; j++) {
  1189. dprintf(mov->fc, "mdat %d, offset %"PRIx64", size %"PRId64", current offset %"PRIx64"\n",
  1190. j, mov->mdat_list[j].offset, mov->mdat_list[j].size, current_offset);
  1191. if (mov->mdat_list[j].offset <= current_offset && mov->mdat_list[j].offset + mov->mdat_list[j].size > current_offset)
  1192. chunk_size = mov->mdat_list[j].offset + mov->mdat_list[j].size - current_offset;
  1193. }
  1194. assert(chunk_size != INT_MAX);
  1195. for (j = 0; j < mov->total_streams; j++) {
  1196. mov->streams[j]->next_chunk = 0;
  1197. }
  1198. }
  1199. av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
  1200. /* get chunk duration */
  1201. chunk_duration = 0;
  1202. while (chunk_samples > 0) {
  1203. if (chunk_samples < sc->stts_data[stts_index].count) {
  1204. chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
  1205. sc->stts_data[stts_index].count -= chunk_samples;
  1206. break;
  1207. } else {
  1208. chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
  1209. chunk_samples -= sc->stts_data[stts_index].count;
  1210. if (stts_index + 1 < sc->stts_count) {
  1211. stts_index++;
  1212. }
  1213. }
  1214. }
  1215. dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", size %d, duration %d\n",
  1216. st->index, i, current_offset, current_dts, chunk_size, chunk_duration);
  1217. assert(chunk_duration % sc->time_rate == 0);
  1218. current_dts += chunk_duration / sc->time_rate;
  1219. }
  1220. }
  1221. out:
  1222. /* adjust sample count to avindex entries */
  1223. sc->sample_count = st->nb_index_entries;
  1224. }
  1225. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1226. {
  1227. MOVContext *mov = s->priv_data;
  1228. ByteIOContext *pb = &s->pb;
  1229. int i, err;
  1230. MOV_atom_t atom = { 0, 0, 0 };
  1231. mov->fc = s;
  1232. mov->parse_table = mov_default_parse_table;
  1233. if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  1234. atom.size = url_fsize(pb);
  1235. else
  1236. atom.size = INT64_MAX;
  1237. /* check MOV header */
  1238. err = mov_read_default(mov, pb, atom);
  1239. if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
  1240. av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%"PRId64"\n",
  1241. err, mov->found_moov, mov->found_mdat, url_ftell(pb));
  1242. return -1;
  1243. }
  1244. dprintf(mov->fc, "on_parse_exit_offset=%d\n", (int) url_ftell(pb));
  1245. /* some cleanup : make sure we are on the mdat atom */
  1246. if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
  1247. url_fseek(pb, mov->mdat_offset, SEEK_SET);
  1248. mov->total_streams = s->nb_streams;
  1249. for(i=0; i<mov->total_streams; i++) {
  1250. MOVStreamContext *sc = mov->streams[i];
  1251. AVStream *st = s->streams[i];
  1252. /* sanity checks */
  1253. if(!sc->stts_count || !sc->chunk_count || !sc->sample_to_chunk_sz ||
  1254. (!sc->sample_size && !sc->sample_count)){
  1255. av_log(s, AV_LOG_ERROR, "missing mandatory atoms, broken header\n");
  1256. sc->sample_count = 0; //ignore track
  1257. continue;
  1258. }
  1259. if(!sc->time_rate)
  1260. sc->time_rate=1;
  1261. if(!sc->time_scale)
  1262. sc->time_scale= mov->time_scale;
  1263. av_set_pts_info(st, 64, sc->time_rate, sc->time_scale);
  1264. if (st->codec->codec_type == CODEC_TYPE_AUDIO && sc->stts_count == 1)
  1265. st->codec->frame_size = sc->stts_data[0].duration;
  1266. if(st->duration != AV_NOPTS_VALUE){
  1267. assert(st->duration % sc->time_rate == 0);
  1268. st->duration /= sc->time_rate;
  1269. }
  1270. sc->ffindex = i;
  1271. mov_build_index(mov, st);
  1272. }
  1273. for(i=0; i<mov->total_streams; i++) {
  1274. /* Do not need those anymore. */
  1275. av_freep(&mov->streams[i]->chunk_offsets);
  1276. av_freep(&mov->streams[i]->sample_to_chunk);
  1277. av_freep(&mov->streams[i]->sample_sizes);
  1278. av_freep(&mov->streams[i]->keyframes);
  1279. av_freep(&mov->streams[i]->stts_data);
  1280. }
  1281. av_freep(&mov->mdat_list);
  1282. return 0;
  1283. }
  1284. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  1285. {
  1286. MOVContext *mov = s->priv_data;
  1287. MOVStreamContext *sc = 0;
  1288. AVIndexEntry *sample = 0;
  1289. int64_t best_dts = INT64_MAX;
  1290. int i;
  1291. for (i = 0; i < mov->total_streams; i++) {
  1292. MOVStreamContext *msc = mov->streams[i];
  1293. if (s->streams[i]->discard != AVDISCARD_ALL && msc->current_sample < msc->sample_count) {
  1294. AVIndexEntry *current_sample = &s->streams[i]->index_entries[msc->current_sample];
  1295. int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate, AV_TIME_BASE, msc->time_scale);
  1296. dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  1297. if (dts < best_dts) {
  1298. sample = current_sample;
  1299. best_dts = dts;
  1300. sc = msc;
  1301. }
  1302. }
  1303. }
  1304. if (!sample)
  1305. return -1;
  1306. /* must be done just before reading, to avoid infinite loop on sample */
  1307. sc->current_sample++;
  1308. if (sample->pos >= url_fsize(&s->pb)) {
  1309. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n", sc->ffindex, sample->pos);
  1310. return -1;
  1311. }
  1312. #ifdef CONFIG_DV_DEMUXER
  1313. if (sc->dv_audio_container) {
  1314. dv_get_packet(mov->dv_demux, pkt);
  1315. dprintf(s, "dv audio pkt size %d\n", pkt->size);
  1316. } else {
  1317. #endif
  1318. url_fseek(&s->pb, sample->pos, SEEK_SET);
  1319. av_get_packet(&s->pb, pkt, sample->size);
  1320. #ifdef CONFIG_DV_DEMUXER
  1321. if (mov->dv_demux) {
  1322. void *pkt_destruct_func = pkt->destruct;
  1323. dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
  1324. pkt->destruct = pkt_destruct_func;
  1325. }
  1326. }
  1327. #endif
  1328. pkt->stream_index = sc->ffindex;
  1329. pkt->dts = sample->timestamp;
  1330. if (sc->ctts_data) {
  1331. assert(sc->ctts_data[sc->sample_to_ctime_index].duration % sc->time_rate == 0);
  1332. pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate;
  1333. /* update ctts context */
  1334. sc->sample_to_ctime_sample++;
  1335. if (sc->sample_to_ctime_index < sc->ctts_count && sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) {
  1336. sc->sample_to_ctime_index++;
  1337. sc->sample_to_ctime_sample = 0;
  1338. }
  1339. } else {
  1340. pkt->pts = pkt->dts;
  1341. }
  1342. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
  1343. pkt->pos = sample->pos;
  1344. dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n", pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
  1345. return 0;
  1346. }
  1347. static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
  1348. {
  1349. MOVStreamContext *sc = st->priv_data;
  1350. int sample, time_sample;
  1351. int i;
  1352. sample = av_index_search_timestamp(st, timestamp, flags);
  1353. dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  1354. if (sample < 0) /* not sure what to do */
  1355. return -1;
  1356. sc->current_sample = sample;
  1357. dprintf(st->codec, "stream %d, found sample %d\n", st->index, sc->current_sample);
  1358. /* adjust ctts index */
  1359. if (sc->ctts_data) {
  1360. time_sample = 0;
  1361. for (i = 0; i < sc->ctts_count; i++) {
  1362. int next = time_sample + sc->ctts_data[i].count;
  1363. if (next > sc->current_sample) {
  1364. sc->sample_to_ctime_index = i;
  1365. sc->sample_to_ctime_sample = sc->current_sample - time_sample;
  1366. break;
  1367. }
  1368. time_sample = next;
  1369. }
  1370. }
  1371. return sample;
  1372. }
  1373. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  1374. {
  1375. AVStream *st;
  1376. int64_t seek_timestamp, timestamp;
  1377. int sample;
  1378. int i;
  1379. if (stream_index >= s->nb_streams)
  1380. return -1;
  1381. st = s->streams[stream_index];
  1382. sample = mov_seek_stream(st, sample_time, flags);
  1383. if (sample < 0)
  1384. return -1;
  1385. /* adjust seek timestamp to found sample timestamp */
  1386. seek_timestamp = st->index_entries[sample].timestamp;
  1387. for (i = 0; i < s->nb_streams; i++) {
  1388. st = s->streams[i];
  1389. if (stream_index == i || st->discard == AVDISCARD_ALL)
  1390. continue;
  1391. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  1392. mov_seek_stream(st, timestamp, flags);
  1393. }
  1394. return 0;
  1395. }
  1396. static int mov_read_close(AVFormatContext *s)
  1397. {
  1398. int i;
  1399. MOVContext *mov = s->priv_data;
  1400. for(i=0; i<mov->total_streams; i++) {
  1401. av_freep(&mov->streams[i]->ctts_data);
  1402. av_freep(&mov->streams[i]);
  1403. }
  1404. if(mov->dv_demux){
  1405. for(i=0; i<mov->dv_fctx->nb_streams; i++){
  1406. av_freep(&mov->dv_fctx->streams[i]->codec);
  1407. av_freep(&mov->dv_fctx->streams[i]);
  1408. }
  1409. av_freep(&mov->dv_fctx);
  1410. av_freep(&mov->dv_demux);
  1411. }
  1412. return 0;
  1413. }
  1414. AVInputFormat mov_demuxer = {
  1415. "mov,mp4,m4a,3gp,3g2,mj2",
  1416. "QuickTime/MPEG4/Motion JPEG 2000 format",
  1417. sizeof(MOVContext),
  1418. mov_probe,
  1419. mov_read_header,
  1420. mov_read_packet,
  1421. mov_read_close,
  1422. mov_read_seek,
  1423. };