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.

1886 lines
64KB

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