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.

1736 lines
59KB

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