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.

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