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.

1590 lines
53KB

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