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.

1634 lines
54KB

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