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.

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