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