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.

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