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.

1887 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. }
  548. } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
  549. for(i=0; i<entries; i++) {
  550. sc->chunk_offsets[i] = get_be64(pb);
  551. }
  552. } else
  553. return -1;
  554. return 0;
  555. }
  556. static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  557. {
  558. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  559. MOVStreamContext *sc = st->priv_data;
  560. int entries, frames_per_sample;
  561. uint32_t format;
  562. uint8_t codec_name[32];
  563. /* for palette traversal */
  564. unsigned int color_depth;
  565. unsigned int color_start;
  566. unsigned int color_count;
  567. unsigned int color_end;
  568. int color_index;
  569. int color_dec;
  570. int color_greyscale;
  571. const uint8_t *color_table;
  572. int j, pseudo_stream_id;
  573. unsigned char r, g, b;
  574. get_byte(pb); /* version */
  575. get_be24(pb); /* flags */
  576. entries = get_be32(pb);
  577. for(pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
  578. //Parsing Sample description table
  579. enum CodecID id;
  580. int dref_id;
  581. MOV_atom_t a = { 0, 0, 0 };
  582. offset_t start_pos = url_ftell(pb);
  583. int size = get_be32(pb); /* size */
  584. format = get_le32(pb); /* data format */
  585. get_be32(pb); /* reserved */
  586. get_be16(pb); /* reserved */
  587. dref_id = get_be16(pb);
  588. if (st->codec->codec_tag &&
  589. (c->fc->video_codec_id ? codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id
  590. : st->codec->codec_tag != MKTAG('j', 'p', 'e', 'g'))
  591. ){
  592. /* Multiple fourcc, we skip JPEG. This is not correct, we should
  593. * export it as a separate AVStream but this needs a few changes
  594. * in the MOV demuxer, patch welcome. */
  595. url_fskip(pb, size - (url_ftell(pb) - start_pos));
  596. continue;
  597. }
  598. sc->pseudo_stream_id= pseudo_stream_id;
  599. sc->dref_id= dref_id;
  600. st->codec->codec_tag = format;
  601. id = codec_get_id(codec_movaudio_tags, format);
  602. if (id<=0 && (format&0xFFFF) == 'm' + ('s'<<8))
  603. id = codec_get_id(codec_wav_tags, bswap_32(format)&0xFFFF);
  604. if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
  605. st->codec->codec_type = CODEC_TYPE_AUDIO;
  606. } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
  607. format && format != MKTAG('m', 'p', '4', 's')) { /* skip old asf mpeg4 tag */
  608. id = codec_get_id(codec_movvideo_tags, format);
  609. if (id <= 0)
  610. id = codec_get_id(codec_bmp_tags, format);
  611. if (id > 0)
  612. st->codec->codec_type = CODEC_TYPE_VIDEO;
  613. else if(st->codec->codec_type == CODEC_TYPE_DATA){
  614. id = codec_get_id(ff_codec_movsubtitle_tags, format);
  615. if(id > 0)
  616. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  617. }
  618. }
  619. dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
  620. (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
  621. (format >> 24) & 0xff, st->codec->codec_type);
  622. if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
  623. st->codec->codec_id = id;
  624. get_be16(pb); /* version */
  625. get_be16(pb); /* revision level */
  626. get_be32(pb); /* vendor */
  627. get_be32(pb); /* temporal quality */
  628. get_be32(pb); /* spatial quality */
  629. st->codec->width = get_be16(pb); /* width */
  630. st->codec->height = get_be16(pb); /* height */
  631. get_be32(pb); /* horiz resolution */
  632. get_be32(pb); /* vert resolution */
  633. get_be32(pb); /* data size, always 0 */
  634. frames_per_sample = get_be16(pb); /* frames per samples */
  635. dprintf(c->fc, "frames/samples = %d\n", frames_per_sample);
  636. get_buffer(pb, codec_name, 32); /* codec name, pascal string (FIXME: true for mp4?) */
  637. if (codec_name[0] <= 31) {
  638. memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
  639. st->codec->codec_name[codec_name[0]] = 0;
  640. }
  641. st->codec->bits_per_sample = get_be16(pb); /* depth */
  642. st->codec->color_table_id = get_be16(pb); /* colortable id */
  643. dprintf(c->fc, "depth %d, ctab id %d\n",
  644. st->codec->bits_per_sample, st->codec->color_table_id);
  645. /* figure out the palette situation */
  646. color_depth = st->codec->bits_per_sample & 0x1F;
  647. color_greyscale = st->codec->bits_per_sample & 0x20;
  648. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  649. if ((color_depth == 2) || (color_depth == 4) ||
  650. (color_depth == 8)) {
  651. if (color_greyscale) {
  652. /* compute the greyscale palette */
  653. st->codec->bits_per_sample = color_depth;
  654. color_count = 1 << color_depth;
  655. color_index = 255;
  656. color_dec = 256 / (color_count - 1);
  657. for (j = 0; j < color_count; j++) {
  658. r = g = b = color_index;
  659. c->palette_control.palette[j] =
  660. (r << 16) | (g << 8) | (b);
  661. color_index -= color_dec;
  662. if (color_index < 0)
  663. color_index = 0;
  664. }
  665. } else if (st->codec->color_table_id) {
  666. /* if flag bit 3 is set, use the default palette */
  667. color_count = 1 << color_depth;
  668. if (color_depth == 2)
  669. color_table = ff_qt_default_palette_4;
  670. else if (color_depth == 4)
  671. color_table = ff_qt_default_palette_16;
  672. else
  673. color_table = ff_qt_default_palette_256;
  674. for (j = 0; j < color_count; j++) {
  675. r = color_table[j * 4 + 0];
  676. g = color_table[j * 4 + 1];
  677. b = color_table[j * 4 + 2];
  678. c->palette_control.palette[j] =
  679. (r << 16) | (g << 8) | (b);
  680. }
  681. } else {
  682. /* load the palette from the file */
  683. color_start = get_be32(pb);
  684. color_count = get_be16(pb);
  685. color_end = get_be16(pb);
  686. if ((color_start <= 255) &&
  687. (color_end <= 255)) {
  688. for (j = color_start; j <= color_end; j++) {
  689. /* each R, G, or B component is 16 bits;
  690. * only use the top 8 bits; skip alpha bytes
  691. * up front */
  692. get_byte(pb);
  693. get_byte(pb);
  694. r = get_byte(pb);
  695. get_byte(pb);
  696. g = get_byte(pb);
  697. get_byte(pb);
  698. b = get_byte(pb);
  699. get_byte(pb);
  700. c->palette_control.palette[j] =
  701. (r << 16) | (g << 8) | (b);
  702. }
  703. }
  704. }
  705. st->codec->palctrl = &c->palette_control;
  706. st->codec->palctrl->palette_changed = 1;
  707. } else
  708. st->codec->palctrl = NULL;
  709. } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
  710. int bits_per_sample;
  711. uint16_t version = get_be16(pb);
  712. st->codec->codec_id = id;
  713. get_be16(pb); /* revision level */
  714. get_be32(pb); /* vendor */
  715. st->codec->channels = get_be16(pb); /* channel count */
  716. dprintf(c->fc, "audio channels %d\n", st->codec->channels);
  717. st->codec->bits_per_sample = get_be16(pb); /* sample size */
  718. sc->audio_cid = get_be16(pb);
  719. get_be16(pb); /* packet size = 0 */
  720. st->codec->sample_rate = ((get_be32(pb) >> 16));
  721. switch (st->codec->codec_id) {
  722. case CODEC_ID_PCM_S8:
  723. case CODEC_ID_PCM_U8:
  724. if (st->codec->bits_per_sample == 16)
  725. st->codec->codec_id = CODEC_ID_PCM_S16BE;
  726. break;
  727. case CODEC_ID_PCM_S16LE:
  728. case CODEC_ID_PCM_S16BE:
  729. if (st->codec->bits_per_sample == 8)
  730. st->codec->codec_id = CODEC_ID_PCM_S8;
  731. else if (st->codec->bits_per_sample == 24)
  732. st->codec->codec_id = CODEC_ID_PCM_S24BE;
  733. break;
  734. /* set values for old format before stsd version 1 appeared */
  735. case CODEC_ID_MACE3:
  736. sc->samples_per_frame = 6;
  737. sc->bytes_per_frame = 2*st->codec->channels;
  738. break;
  739. case CODEC_ID_MACE6:
  740. sc->samples_per_frame = 6;
  741. sc->bytes_per_frame = 1*st->codec->channels;
  742. break;
  743. case CODEC_ID_ADPCM_IMA_QT:
  744. sc->samples_per_frame = 64;
  745. sc->bytes_per_frame = 34*st->codec->channels;
  746. break;
  747. default:
  748. break;
  749. }
  750. //Read QT version 1 fields. In version 0 these do not exist.
  751. dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
  752. if(!c->isom) {
  753. if(version==1) {
  754. sc->samples_per_frame = get_be32(pb);
  755. get_be32(pb); /* bytes per packet */
  756. sc->bytes_per_frame = get_be32(pb);
  757. get_be32(pb); /* bytes per sample */
  758. } else if(version==2) {
  759. get_be32(pb); /* sizeof struct only */
  760. st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
  761. st->codec->channels = get_be32(pb);
  762. get_be32(pb); /* always 0x7F000000 */
  763. get_be32(pb); /* bits per channel if sound is uncompressed */
  764. get_be32(pb); /* lcpm format specific flag */
  765. get_be32(pb); /* bytes per audio packet if constant */
  766. get_be32(pb); /* lpcm frames per audio packet if constant */
  767. }
  768. }
  769. bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
  770. if (bits_per_sample) {
  771. st->codec->bits_per_sample = bits_per_sample;
  772. sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
  773. }
  774. } else if(st->codec->codec_type==CODEC_TYPE_SUBTITLE){
  775. st->codec->codec_id= id;
  776. } else {
  777. /* other codec type, just skip (rtp, mp4s, tmcd ...) */
  778. url_fskip(pb, size - (url_ftell(pb) - start_pos));
  779. }
  780. /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
  781. a.size = size - (url_ftell(pb) - start_pos);
  782. if (a.size > 8) {
  783. if (mov_read_default(c, pb, a) < 0)
  784. return -1;
  785. } else if (a.size > 0)
  786. url_fskip(pb, a.size);
  787. }
  788. if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
  789. st->codec->sample_rate= sc->time_scale;
  790. /* special codec parameters handling */
  791. switch (st->codec->codec_id) {
  792. #ifdef CONFIG_DV_DEMUXER
  793. case CODEC_ID_DVAUDIO:
  794. c->dv_fctx = av_alloc_format_context();
  795. c->dv_demux = dv_init_demux(c->dv_fctx);
  796. if (!c->dv_demux) {
  797. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  798. return -1;
  799. }
  800. sc->dv_audio_container = 1;
  801. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  802. break;
  803. #endif
  804. /* no ifdef since parameters are always those */
  805. case CODEC_ID_AMR_WB:
  806. st->codec->sample_rate= 16000;
  807. st->codec->channels= 1; /* really needed */
  808. break;
  809. case CODEC_ID_AMR_NB:
  810. st->codec->sample_rate= 8000;
  811. st->codec->channels= 1; /* really needed */
  812. break;
  813. case CODEC_ID_MP2:
  814. case CODEC_ID_MP3:
  815. st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
  816. st->need_parsing = AVSTREAM_PARSE_FULL;
  817. break;
  818. case CODEC_ID_ADPCM_MS:
  819. case CODEC_ID_ADPCM_IMA_WAV:
  820. st->codec->block_align = sc->bytes_per_frame;
  821. break;
  822. default:
  823. break;
  824. }
  825. return 0;
  826. }
  827. static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  828. {
  829. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  830. MOVStreamContext *sc = st->priv_data;
  831. unsigned int i, entries;
  832. get_byte(pb); /* version */
  833. get_be24(pb); /* flags */
  834. entries = get_be32(pb);
  835. if(entries >= UINT_MAX / sizeof(MOV_stsc_t))
  836. return -1;
  837. dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  838. sc->sample_to_chunk_sz = entries;
  839. sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_stsc_t));
  840. if (!sc->sample_to_chunk)
  841. return -1;
  842. for(i=0; i<entries; i++) {
  843. sc->sample_to_chunk[i].first = get_be32(pb);
  844. sc->sample_to_chunk[i].count = get_be32(pb);
  845. sc->sample_to_chunk[i].id = get_be32(pb);
  846. }
  847. return 0;
  848. }
  849. static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  850. {
  851. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  852. MOVStreamContext *sc = st->priv_data;
  853. unsigned int i, entries;
  854. get_byte(pb); /* version */
  855. get_be24(pb); /* flags */
  856. entries = get_be32(pb);
  857. if(entries >= UINT_MAX / sizeof(int))
  858. return -1;
  859. sc->keyframe_count = entries;
  860. dprintf(c->fc, "keyframe_count = %d\n", sc->keyframe_count);
  861. sc->keyframes = av_malloc(entries * sizeof(int));
  862. if (!sc->keyframes)
  863. return -1;
  864. for(i=0; i<entries; i++) {
  865. sc->keyframes[i] = get_be32(pb);
  866. //dprintf(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
  867. }
  868. return 0;
  869. }
  870. static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  871. {
  872. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  873. MOVStreamContext *sc = st->priv_data;
  874. unsigned int i, entries, sample_size;
  875. get_byte(pb); /* version */
  876. get_be24(pb); /* flags */
  877. sample_size = get_be32(pb);
  878. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  879. sc->sample_size = sample_size;
  880. entries = get_be32(pb);
  881. if(entries >= UINT_MAX / sizeof(int))
  882. return -1;
  883. sc->sample_count = entries;
  884. if (sample_size)
  885. return 0;
  886. dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, sc->sample_count);
  887. sc->sample_sizes = av_malloc(entries * sizeof(int));
  888. if (!sc->sample_sizes)
  889. return -1;
  890. for(i=0; i<entries; i++)
  891. sc->sample_sizes[i] = get_be32(pb);
  892. return 0;
  893. }
  894. static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  895. {
  896. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  897. MOVStreamContext *sc = st->priv_data;
  898. unsigned int i, entries;
  899. int64_t duration=0;
  900. int64_t total_sample_count=0;
  901. get_byte(pb); /* version */
  902. get_be24(pb); /* flags */
  903. entries = get_be32(pb);
  904. if(entries >= UINT_MAX / sizeof(MOV_stts_t))
  905. return -1;
  906. sc->stts_count = entries;
  907. sc->stts_data = av_malloc(entries * sizeof(MOV_stts_t));
  908. if (!sc->stts_data)
  909. return -1;
  910. dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  911. sc->time_rate=0;
  912. for(i=0; i<entries; i++) {
  913. int sample_duration;
  914. int sample_count;
  915. sample_count=get_be32(pb);
  916. sample_duration = get_be32(pb);
  917. sc->stts_data[i].count= sample_count;
  918. sc->stts_data[i].duration= sample_duration;
  919. sc->time_rate= ff_gcd(sc->time_rate, sample_duration);
  920. dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  921. duration+=(int64_t)sample_duration*sample_count;
  922. total_sample_count+=sample_count;
  923. }
  924. st->nb_frames= total_sample_count;
  925. if(duration)
  926. st->duration= duration;
  927. return 0;
  928. }
  929. static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  930. {
  931. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  932. MOVStreamContext *sc = st->priv_data;
  933. unsigned int i, entries;
  934. get_byte(pb); /* version */
  935. get_be24(pb); /* flags */
  936. entries = get_be32(pb);
  937. if(entries >= UINT_MAX / sizeof(MOV_stts_t))
  938. return -1;
  939. sc->ctts_count = entries;
  940. sc->ctts_data = av_malloc(entries * sizeof(MOV_stts_t));
  941. if (!sc->ctts_data)
  942. return -1;
  943. dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  944. for(i=0; i<entries; i++) {
  945. int count =get_be32(pb);
  946. int duration =get_be32(pb);
  947. if (duration < 0) {
  948. av_log(c->fc, AV_LOG_ERROR, "negative ctts, ignoring\n");
  949. sc->ctts_count = 0;
  950. url_fskip(pb, 8 * (entries - i - 1));
  951. break;
  952. }
  953. sc->ctts_data[i].count = count;
  954. sc->ctts_data[i].duration= duration;
  955. sc->time_rate= ff_gcd(sc->time_rate, duration);
  956. }
  957. return 0;
  958. }
  959. static void mov_build_index(MOVContext *mov, AVStream *st)
  960. {
  961. MOVStreamContext *sc = st->priv_data;
  962. offset_t current_offset;
  963. int64_t current_dts = 0;
  964. unsigned int stts_index = 0;
  965. unsigned int stsc_index = 0;
  966. unsigned int stss_index = 0;
  967. unsigned int i, j;
  968. /* only use old uncompressed audio chunk demuxing when stts specifies it */
  969. if (!(st->codec->codec_type == CODEC_TYPE_AUDIO &&
  970. sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
  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, "stream %d, missing mandatory atoms, broken header\n",
  1080. st->index);
  1081. sc->sample_count = 0; //ignore track
  1082. return 0;
  1083. }
  1084. if(!sc->time_rate)
  1085. sc->time_rate=1;
  1086. if(!sc->time_scale)
  1087. sc->time_scale= c->time_scale;
  1088. av_set_pts_info(st, 64, sc->time_rate, sc->time_scale);
  1089. if (st->codec->codec_type == CODEC_TYPE_AUDIO && sc->stts_count == 1)
  1090. st->codec->frame_size = av_rescale(sc->time_rate, st->codec->sample_rate, sc->time_scale);
  1091. if(st->duration != AV_NOPTS_VALUE){
  1092. assert(st->duration % sc->time_rate == 0);
  1093. st->duration /= sc->time_rate;
  1094. }
  1095. sc->ffindex = st->index;
  1096. mov_build_index(c, st);
  1097. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  1098. if (url_fopen(&sc->pb, sc->drefs[sc->dref_id-1].path, URL_RDONLY) < 0)
  1099. av_log(c->fc, AV_LOG_ERROR, "stream %d, error opening file %s: %s\n",
  1100. st->index, sc->drefs[sc->dref_id-1].path, strerror(errno));
  1101. } else
  1102. sc->pb = c->fc->pb;
  1103. switch (st->codec->codec_id) {
  1104. #ifdef CONFIG_H261_DECODER
  1105. case CODEC_ID_H261:
  1106. #endif
  1107. #ifdef CONFIG_H263_DECODER
  1108. case CODEC_ID_H263:
  1109. #endif
  1110. #ifdef CONFIG_MPEG4_DECODER
  1111. case CODEC_ID_MPEG4:
  1112. #endif
  1113. st->codec->width= 0; /* let decoder init width/height */
  1114. st->codec->height= 0;
  1115. break;
  1116. #ifdef CONFIG_LIBFAAD
  1117. case CODEC_ID_AAC:
  1118. #endif
  1119. #ifdef CONFIG_VORBIS_DECODER
  1120. case CODEC_ID_VORBIS:
  1121. #endif
  1122. case CODEC_ID_MP3ON4:
  1123. st->codec->sample_rate= 0; /* let decoder init parameters properly */
  1124. break;
  1125. }
  1126. /* Do not need those anymore. */
  1127. av_freep(&sc->chunk_offsets);
  1128. av_freep(&sc->sample_to_chunk);
  1129. av_freep(&sc->sample_sizes);
  1130. av_freep(&sc->keyframes);
  1131. av_freep(&sc->stts_data);
  1132. return 0;
  1133. }
  1134. static void mov_parse_udta_string(ByteIOContext *pb, char *str, int size)
  1135. {
  1136. uint16_t str_size = get_be16(pb); /* string length */;
  1137. get_be16(pb); /* skip language */
  1138. get_buffer(pb, str, FFMIN(size, str_size));
  1139. }
  1140. static int mov_read_udta(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1141. {
  1142. uint64_t end = url_ftell(pb) + atom.size;
  1143. while (url_ftell(pb) + 8 < end) {
  1144. uint32_t tag_size = get_be32(pb);
  1145. uint32_t tag = get_le32(pb);
  1146. uint64_t next = url_ftell(pb) + tag_size - 8;
  1147. if (next > end) // stop if tag_size is wrong
  1148. break;
  1149. switch (tag) {
  1150. case MKTAG(0xa9,'n','a','m'):
  1151. mov_parse_udta_string(pb, c->fc->title, sizeof(c->fc->title));
  1152. break;
  1153. case MKTAG(0xa9,'w','r','t'):
  1154. mov_parse_udta_string(pb, c->fc->author, sizeof(c->fc->author));
  1155. break;
  1156. case MKTAG(0xa9,'c','p','y'):
  1157. mov_parse_udta_string(pb, c->fc->copyright, sizeof(c->fc->copyright));
  1158. break;
  1159. case MKTAG(0xa9,'i','n','f'):
  1160. mov_parse_udta_string(pb, c->fc->comment, sizeof(c->fc->comment));
  1161. break;
  1162. default:
  1163. break;
  1164. }
  1165. url_fseek(pb, next, SEEK_SET);
  1166. }
  1167. return 0;
  1168. }
  1169. static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1170. {
  1171. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1172. int version = get_byte(pb);
  1173. get_be24(pb); /* flags */
  1174. /*
  1175. MOV_TRACK_ENABLED 0x0001
  1176. MOV_TRACK_IN_MOVIE 0x0002
  1177. MOV_TRACK_IN_PREVIEW 0x0004
  1178. MOV_TRACK_IN_POSTER 0x0008
  1179. */
  1180. if (version == 1) {
  1181. get_be64(pb);
  1182. get_be64(pb);
  1183. } else {
  1184. get_be32(pb); /* creation time */
  1185. get_be32(pb); /* modification time */
  1186. }
  1187. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  1188. get_be32(pb); /* reserved */
  1189. st->start_time = 0; /* check */
  1190. /* highlevel (considering edits) duration in movie timebase */
  1191. (version == 1) ? get_be64(pb) : get_be32(pb);
  1192. get_be32(pb); /* reserved */
  1193. get_be32(pb); /* reserved */
  1194. get_be16(pb); /* layer */
  1195. get_be16(pb); /* alternate group */
  1196. get_be16(pb); /* volume */
  1197. get_be16(pb); /* reserved */
  1198. url_fskip(pb, 36); /* display matrix */
  1199. /* those are fixed-point */
  1200. get_be32(pb); /* track width */
  1201. get_be32(pb); /* track height */
  1202. return 0;
  1203. }
  1204. static int mov_read_tfhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1205. {
  1206. MOVFragment *frag = &c->fragment;
  1207. MOVTrackExt *trex = NULL;
  1208. int flags, track_id, i;
  1209. get_byte(pb); /* version */
  1210. flags = get_be24(pb);
  1211. track_id = get_be32(pb);
  1212. if (!track_id || track_id > c->fc->nb_streams)
  1213. return -1;
  1214. frag->track_id = track_id;
  1215. for (i = 0; i < c->trex_count; i++)
  1216. if (c->trex_data[i].track_id == frag->track_id) {
  1217. trex = &c->trex_data[i];
  1218. break;
  1219. }
  1220. if (!trex) {
  1221. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
  1222. return -1;
  1223. }
  1224. if (flags & 0x01) frag->base_data_offset = get_be64(pb);
  1225. else frag->base_data_offset = frag->moof_offset;
  1226. if (flags & 0x02) frag->stsd_id = get_be32(pb);
  1227. else frag->stsd_id = trex->stsd_id;
  1228. frag->duration = flags & 0x08 ? get_be32(pb) : trex->duration;
  1229. frag->size = flags & 0x10 ? get_be32(pb) : trex->size;
  1230. frag->flags = flags & 0x20 ? get_be32(pb) : trex->flags;
  1231. dprintf(c->fc, "frag flags 0x%x\n", frag->flags);
  1232. return 0;
  1233. }
  1234. static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1235. {
  1236. MOVTrackExt *trex;
  1237. if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
  1238. return -1;
  1239. c->trex_data = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
  1240. if (!c->trex_data)
  1241. return AVERROR(ENOMEM);
  1242. trex = &c->trex_data[c->trex_count++];
  1243. get_byte(pb); /* version */
  1244. get_be24(pb); /* flags */
  1245. trex->track_id = get_be32(pb);
  1246. trex->stsd_id = get_be32(pb);
  1247. trex->duration = get_be32(pb);
  1248. trex->size = get_be32(pb);
  1249. trex->flags = get_be32(pb);
  1250. return 0;
  1251. }
  1252. static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1253. {
  1254. MOVFragment *frag = &c->fragment;
  1255. AVStream *st = c->fc->streams[frag->track_id-1];
  1256. MOVStreamContext *sc = st->priv_data;
  1257. uint64_t offset;
  1258. int64_t dts;
  1259. int data_offset = 0;
  1260. unsigned entries, first_sample_flags = frag->flags;
  1261. int flags, distance, i;
  1262. if (sc->pseudo_stream_id+1 != frag->stsd_id)
  1263. return 0;
  1264. if (!st->nb_index_entries)
  1265. return -1;
  1266. get_byte(pb); /* version */
  1267. flags = get_be24(pb);
  1268. entries = get_be32(pb);
  1269. dprintf(c->fc, "flags 0x%x entries %d\n", flags, entries);
  1270. if (flags & 0x001) data_offset = get_be32(pb);
  1271. if (flags & 0x004) first_sample_flags = get_be32(pb);
  1272. if (flags & 0x800) {
  1273. if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
  1274. return -1;
  1275. sc->ctts_data = av_realloc(sc->ctts_data,
  1276. (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
  1277. if (!sc->ctts_data)
  1278. return AVERROR(ENOMEM);
  1279. }
  1280. dts = st->duration;
  1281. offset = frag->base_data_offset + data_offset;
  1282. distance = 0;
  1283. dprintf(c->fc, "first sample flags 0x%x\n", first_sample_flags);
  1284. for (i = 0; i < entries; i++) {
  1285. unsigned sample_size = frag->size;
  1286. int sample_flags = i ? frag->flags : first_sample_flags;
  1287. unsigned sample_duration = frag->duration;
  1288. int keyframe;
  1289. if (flags & 0x100) sample_duration = get_be32(pb);
  1290. if (flags & 0x200) sample_size = get_be32(pb);
  1291. if (flags & 0x400) sample_flags = get_be32(pb);
  1292. if (flags & 0x800) {
  1293. sc->ctts_data[sc->ctts_count].count = 1;
  1294. sc->ctts_data[sc->ctts_count].duration = get_be32(pb);
  1295. sc->ctts_count++;
  1296. }
  1297. if ((keyframe = st->codec->codec_type == CODEC_TYPE_AUDIO ||
  1298. (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))
  1299. distance = 0;
  1300. av_add_index_entry(st, offset, dts, sample_size, distance,
  1301. keyframe ? AVINDEX_KEYFRAME : 0);
  1302. dprintf(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1303. "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
  1304. offset, dts, sample_size, distance, keyframe);
  1305. distance++;
  1306. assert(sample_duration % sc->time_rate == 0);
  1307. dts += sample_duration / sc->time_rate;
  1308. offset += sample_size;
  1309. }
  1310. frag->moof_offset = offset;
  1311. sc->sample_count = st->nb_index_entries;
  1312. st->duration = dts;
  1313. return 0;
  1314. }
  1315. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1316. /* like the files created with Adobe Premiere 5.0, for samples see */
  1317. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1318. static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1319. {
  1320. int err;
  1321. if (atom.size < 8)
  1322. return 0; /* continue */
  1323. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1324. url_fskip(pb, atom.size - 4);
  1325. return 0;
  1326. }
  1327. atom.type = get_le32(pb);
  1328. atom.offset += 8;
  1329. atom.size -= 8;
  1330. if (atom.type != MKTAG('m', 'd', 'a', 't')) {
  1331. url_fskip(pb, atom.size);
  1332. return 0;
  1333. }
  1334. err = mov_read_mdat(c, pb, atom);
  1335. return err;
  1336. }
  1337. static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1338. {
  1339. #ifdef CONFIG_ZLIB
  1340. ByteIOContext ctx;
  1341. uint8_t *cmov_data;
  1342. uint8_t *moov_data; /* uncompressed data */
  1343. long cmov_len, moov_len;
  1344. int ret;
  1345. get_be32(pb); /* dcom atom */
  1346. if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
  1347. return -1;
  1348. if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
  1349. av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !");
  1350. return -1;
  1351. }
  1352. get_be32(pb); /* cmvd atom */
  1353. if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
  1354. return -1;
  1355. moov_len = get_be32(pb); /* uncompressed size */
  1356. cmov_len = atom.size - 6 * 4;
  1357. cmov_data = av_malloc(cmov_len);
  1358. if (!cmov_data)
  1359. return -1;
  1360. moov_data = av_malloc(moov_len);
  1361. if (!moov_data) {
  1362. av_free(cmov_data);
  1363. return -1;
  1364. }
  1365. get_buffer(pb, cmov_data, cmov_len);
  1366. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1367. return -1;
  1368. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  1369. return -1;
  1370. atom.type = MKTAG( 'm', 'o', 'o', 'v' );
  1371. atom.offset = 0;
  1372. atom.size = moov_len;
  1373. #ifdef DEBUG
  1374. // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
  1375. #endif
  1376. ret = mov_read_default(c, &ctx, atom);
  1377. av_free(moov_data);
  1378. av_free(cmov_data);
  1379. return ret;
  1380. #else
  1381. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  1382. return -1;
  1383. #endif
  1384. }
  1385. /* edit list atom */
  1386. static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1387. {
  1388. MOVStreamContext *sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  1389. int i, edit_count;
  1390. get_byte(pb); /* version */
  1391. get_be24(pb); /* flags */
  1392. edit_count= sc->edit_count = get_be32(pb); /* entries */
  1393. for(i=0; i<edit_count; i++){
  1394. int time;
  1395. get_be32(pb); /* Track duration */
  1396. time = get_be32(pb); /* Media time */
  1397. get_be32(pb); /* Media rate */
  1398. if (time != 0)
  1399. av_log(c->fc, AV_LOG_WARNING, "edit list not starting at 0, "
  1400. "a/v desync might occur, patch welcome\n");
  1401. }
  1402. dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, sc->edit_count);
  1403. return 0;
  1404. }
  1405. static const MOVParseTableEntry mov_default_parse_table[] = {
  1406. { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
  1407. { MKTAG( 'c', 't', 't', 's' ), mov_read_ctts }, /* composition time to sample */
  1408. { MKTAG( 'd', 'i', 'n', 'f' ), mov_read_default },
  1409. { MKTAG( 'd', 'r', 'e', 'f' ), mov_read_dref },
  1410. { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
  1411. { MKTAG( 'e', 'l', 's', 't' ), mov_read_elst },
  1412. { MKTAG( 'e', 'n', 'd', 'a' ), mov_read_enda },
  1413. { MKTAG( 'f', 'i', 'e', 'l' ), mov_read_extradata },
  1414. { MKTAG( 'f', 't', 'y', 'p' ), mov_read_ftyp },
  1415. { MKTAG( 'g', 'l', 'b', 'l' ), mov_read_glbl },
  1416. { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
  1417. { MKTAG( 'j', 'p', '2', 'h' ), mov_read_extradata },
  1418. { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
  1419. { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
  1420. { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
  1421. { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
  1422. { MKTAG( 'm', 'o', 'o', 'f' ), mov_read_moof },
  1423. { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
  1424. { MKTAG( 'm', 'v', 'e', 'x' ), mov_read_default },
  1425. { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
  1426. { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorenson extension ??? */
  1427. { MKTAG( 'a', 'l', 'a', 'c' ), mov_read_extradata }, /* alac specific atom */
  1428. { MKTAG( 'a', 'v', 'c', 'C' ), mov_read_glbl },
  1429. { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
  1430. { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
  1431. { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
  1432. { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
  1433. { MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */
  1434. { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
  1435. { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
  1436. { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
  1437. { MKTAG( 't', 'f', 'h', 'd' ), mov_read_tfhd }, /* track fragment header */
  1438. { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
  1439. { MKTAG( 't', 'r', 'a', 'f' ), mov_read_default },
  1440. { MKTAG( 't', 'r', 'e', 'x' ), mov_read_trex },
  1441. { MKTAG( 't', 'r', 'u', 'n' ), mov_read_trun },
  1442. { MKTAG( 'u', 'd', 't', 'a' ), mov_read_udta },
  1443. { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_wave },
  1444. { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
  1445. { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
  1446. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
  1447. { 0, NULL }
  1448. };
  1449. static int mov_probe(AVProbeData *p)
  1450. {
  1451. unsigned int offset;
  1452. uint32_t tag;
  1453. int score = 0;
  1454. /* check file header */
  1455. offset = 0;
  1456. for(;;) {
  1457. /* ignore invalid offset */
  1458. if ((offset + 8) > (unsigned int)p->buf_size)
  1459. return score;
  1460. tag = AV_RL32(p->buf + offset + 4);
  1461. switch(tag) {
  1462. /* check for obvious tags */
  1463. case MKTAG( 'j', 'P', ' ', ' ' ): /* jpeg 2000 signature */
  1464. case MKTAG( 'm', 'o', 'o', 'v' ):
  1465. case MKTAG( 'm', 'd', 'a', 't' ):
  1466. case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
  1467. case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
  1468. return AVPROBE_SCORE_MAX;
  1469. /* those are more common words, so rate then a bit less */
  1470. case MKTAG( 'e', 'd', 'i', 'w' ): /* xdcam files have reverted first tags */
  1471. case MKTAG( 'w', 'i', 'd', 'e' ):
  1472. case MKTAG( 'f', 'r', 'e', 'e' ):
  1473. case MKTAG( 'j', 'u', 'n', 'k' ):
  1474. case MKTAG( 'p', 'i', 'c', 't' ):
  1475. return AVPROBE_SCORE_MAX - 5;
  1476. case MKTAG(0x82,0x82,0x7f,0x7d ):
  1477. case MKTAG( 'f', 't', 'y', 'p' ):
  1478. case MKTAG( 's', 'k', 'i', 'p' ):
  1479. case MKTAG( 'u', 'u', 'i', 'd' ):
  1480. offset = AV_RB32(p->buf+offset) + offset;
  1481. /* if we only find those cause probedata is too small at least rate them */
  1482. score = AVPROBE_SCORE_MAX - 50;
  1483. break;
  1484. default:
  1485. /* unrecognized tag */
  1486. return score;
  1487. }
  1488. }
  1489. return score;
  1490. }
  1491. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1492. {
  1493. MOVContext *mov = s->priv_data;
  1494. ByteIOContext *pb = s->pb;
  1495. int err;
  1496. MOV_atom_t atom = { 0, 0, 0 };
  1497. mov->fc = s;
  1498. /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  1499. if(!url_is_streamed(pb))
  1500. atom.size = url_fsize(pb);
  1501. else
  1502. atom.size = INT64_MAX;
  1503. /* check MOV header */
  1504. err = mov_read_default(mov, pb, atom);
  1505. if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
  1506. av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%"PRId64"\n",
  1507. err, mov->found_moov, mov->found_mdat, url_ftell(pb));
  1508. return -1;
  1509. }
  1510. dprintf(mov->fc, "on_parse_exit_offset=%d\n", (int) url_ftell(pb));
  1511. return 0;
  1512. }
  1513. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  1514. {
  1515. MOVContext *mov = s->priv_data;
  1516. MOVStreamContext *sc = 0;
  1517. AVIndexEntry *sample = 0;
  1518. int64_t best_dts = INT64_MAX;
  1519. int i;
  1520. retry:
  1521. for (i = 0; i < s->nb_streams; i++) {
  1522. AVStream *st = s->streams[i];
  1523. MOVStreamContext *msc = st->priv_data;
  1524. if (st->discard != AVDISCARD_ALL && msc->pb && msc->current_sample < msc->sample_count) {
  1525. AVIndexEntry *current_sample = &st->index_entries[msc->current_sample];
  1526. int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate,
  1527. AV_TIME_BASE, msc->time_scale);
  1528. dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  1529. if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) ||
  1530. (!url_is_streamed(s->pb) &&
  1531. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
  1532. ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  1533. (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
  1534. sample = current_sample;
  1535. best_dts = dts;
  1536. sc = msc;
  1537. }
  1538. }
  1539. }
  1540. if (!sample) {
  1541. mov->found_mdat = 0;
  1542. if (!url_is_streamed(s->pb) ||
  1543. mov_read_default(mov, s->pb, (MOV_atom_t){ 0, 0, INT64_MAX }) < 0 ||
  1544. url_feof(s->pb))
  1545. return -1;
  1546. dprintf(s, "read fragments, offset 0x%llx\n", url_ftell(s->pb));
  1547. goto retry;
  1548. }
  1549. /* must be done just before reading, to avoid infinite loop on sample */
  1550. sc->current_sample++;
  1551. if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  1552. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  1553. sc->ffindex, sample->pos);
  1554. return -1;
  1555. }
  1556. av_get_packet(sc->pb, pkt, sample->size);
  1557. #ifdef CONFIG_DV_DEMUXER
  1558. if (mov->dv_demux && sc->dv_audio_container) {
  1559. dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
  1560. av_free(pkt->data);
  1561. pkt->size = 0;
  1562. if (dv_get_packet(mov->dv_demux, pkt) < 0)
  1563. return -1;
  1564. }
  1565. #endif
  1566. pkt->stream_index = sc->ffindex;
  1567. pkt->dts = sample->timestamp;
  1568. if (sc->ctts_data) {
  1569. assert(sc->ctts_data[sc->sample_to_ctime_index].duration % sc->time_rate == 0);
  1570. pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate;
  1571. /* update ctts context */
  1572. sc->sample_to_ctime_sample++;
  1573. if (sc->sample_to_ctime_index < sc->ctts_count &&
  1574. sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) {
  1575. sc->sample_to_ctime_index++;
  1576. sc->sample_to_ctime_sample = 0;
  1577. }
  1578. } else {
  1579. pkt->pts = pkt->dts;
  1580. }
  1581. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
  1582. pkt->pos = sample->pos;
  1583. dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
  1584. pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
  1585. return 0;
  1586. }
  1587. static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
  1588. {
  1589. MOVStreamContext *sc = st->priv_data;
  1590. int sample, time_sample;
  1591. int i;
  1592. sample = av_index_search_timestamp(st, timestamp, flags);
  1593. dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  1594. if (sample < 0) /* not sure what to do */
  1595. return -1;
  1596. sc->current_sample = sample;
  1597. dprintf(st->codec, "stream %d, found sample %d\n", st->index, sc->current_sample);
  1598. /* adjust ctts index */
  1599. if (sc->ctts_data) {
  1600. time_sample = 0;
  1601. for (i = 0; i < sc->ctts_count; i++) {
  1602. int next = time_sample + sc->ctts_data[i].count;
  1603. if (next > sc->current_sample) {
  1604. sc->sample_to_ctime_index = i;
  1605. sc->sample_to_ctime_sample = sc->current_sample - time_sample;
  1606. break;
  1607. }
  1608. time_sample = next;
  1609. }
  1610. }
  1611. return sample;
  1612. }
  1613. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  1614. {
  1615. AVStream *st;
  1616. int64_t seek_timestamp, timestamp;
  1617. int sample;
  1618. int i;
  1619. if (stream_index >= s->nb_streams)
  1620. return -1;
  1621. st = s->streams[stream_index];
  1622. sample = mov_seek_stream(st, sample_time, flags);
  1623. if (sample < 0)
  1624. return -1;
  1625. /* adjust seek timestamp to found sample timestamp */
  1626. seek_timestamp = st->index_entries[sample].timestamp;
  1627. for (i = 0; i < s->nb_streams; i++) {
  1628. st = s->streams[i];
  1629. if (stream_index == i || st->discard == AVDISCARD_ALL)
  1630. continue;
  1631. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  1632. mov_seek_stream(st, timestamp, flags);
  1633. }
  1634. return 0;
  1635. }
  1636. static int mov_read_close(AVFormatContext *s)
  1637. {
  1638. int i, j;
  1639. MOVContext *mov = s->priv_data;
  1640. for(i=0; i<s->nb_streams; i++) {
  1641. MOVStreamContext *sc = s->streams[i]->priv_data;
  1642. av_freep(&sc->ctts_data);
  1643. for (j=0; j<sc->drefs_count; j++)
  1644. av_freep(&sc->drefs[j].path);
  1645. av_freep(&sc->drefs);
  1646. if (sc->pb && sc->pb != s->pb)
  1647. url_fclose(sc->pb);
  1648. }
  1649. if(mov->dv_demux){
  1650. for(i=0; i<mov->dv_fctx->nb_streams; i++){
  1651. av_freep(&mov->dv_fctx->streams[i]->codec);
  1652. av_freep(&mov->dv_fctx->streams[i]);
  1653. }
  1654. av_freep(&mov->dv_fctx);
  1655. av_freep(&mov->dv_demux);
  1656. }
  1657. av_freep(&mov->trex_data);
  1658. return 0;
  1659. }
  1660. AVInputFormat mov_demuxer = {
  1661. "mov,mp4,m4a,3gp,3g2,mj2",
  1662. "QuickTime/MPEG4/Motion JPEG 2000 format",
  1663. sizeof(MOVContext),
  1664. mov_probe,
  1665. mov_read_header,
  1666. mov_read_packet,
  1667. mov_read_close,
  1668. mov_read_seek,
  1669. };