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.

1638 lines
55KB

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