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.

1612 lines
54KB

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