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.

2901 lines
97KB

  1. /*
  2. * MOV demuxer
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <limits.h>
  23. //#define DEBUG
  24. //#define MOV_EXPORT_ALL_METADATA
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/audioconvert.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/intfloat.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/dict.h"
  32. #include "libavcodec/ac3tab.h"
  33. #include "avformat.h"
  34. #include "internal.h"
  35. #include "avio_internal.h"
  36. #include "riff.h"
  37. #include "isom.h"
  38. #include "libavcodec/get_bits.h"
  39. #include "id3v1.h"
  40. #include "mov_chan.h"
  41. #if CONFIG_ZLIB
  42. #include <zlib.h>
  43. #endif
  44. /*
  45. * First version by Francois Revol revol@free.fr
  46. * Seek function by Gael Chardon gael.dev@4now.net
  47. */
  48. #include "qtpalette.h"
  49. #undef NDEBUG
  50. #include <assert.h>
  51. /* those functions parse an atom */
  52. /* links atom IDs to parse functions */
  53. typedef struct MOVParseTableEntry {
  54. uint32_t type;
  55. int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
  56. } MOVParseTableEntry;
  57. static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
  58. static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
  59. unsigned len, const char *key)
  60. {
  61. char buf[16];
  62. short current, total = 0;
  63. avio_rb16(pb); // unknown
  64. current = avio_rb16(pb);
  65. if (len >= 6)
  66. total = avio_rb16(pb);
  67. if (!total)
  68. snprintf(buf, sizeof(buf), "%d", current);
  69. else
  70. snprintf(buf, sizeof(buf), "%d/%d", current, total);
  71. av_dict_set(&c->fc->metadata, key, buf, 0);
  72. return 0;
  73. }
  74. static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
  75. unsigned len, const char *key)
  76. {
  77. char buf[16];
  78. /* bypass padding bytes */
  79. avio_r8(pb);
  80. avio_r8(pb);
  81. avio_r8(pb);
  82. snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
  83. av_dict_set(&c->fc->metadata, key, buf, 0);
  84. return 0;
  85. }
  86. static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
  87. unsigned len, const char *key)
  88. {
  89. char buf[16];
  90. snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
  91. av_dict_set(&c->fc->metadata, key, buf, 0);
  92. return 0;
  93. }
  94. static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
  95. unsigned len, const char *key)
  96. {
  97. short genre;
  98. char buf[20];
  99. avio_r8(pb); // unknown
  100. genre = avio_r8(pb);
  101. if (genre < 1 || genre > ID3v1_GENRE_MAX)
  102. return 0;
  103. snprintf(buf, sizeof(buf), "%s", ff_id3v1_genre_str[genre-1]);
  104. av_dict_set(&c->fc->metadata, key, buf, 0);
  105. return 0;
  106. }
  107. static const uint32_t mac_to_unicode[128] = {
  108. 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
  109. 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
  110. 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
  111. 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
  112. 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
  113. 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
  114. 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
  115. 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
  116. 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
  117. 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
  118. 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
  119. 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
  120. 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
  121. 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
  122. 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
  123. 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
  124. };
  125. static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
  126. char *dst, int dstlen)
  127. {
  128. char *p = dst;
  129. char *end = dst+dstlen-1;
  130. int i;
  131. for (i = 0; i < len; i++) {
  132. uint8_t t, c = avio_r8(pb);
  133. if (c < 0x80 && p < end)
  134. *p++ = c;
  135. else
  136. PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
  137. }
  138. *p = 0;
  139. return p - dst;
  140. }
  141. static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
  142. {
  143. AVPacket pkt;
  144. AVStream *st;
  145. MOVStreamContext *sc;
  146. enum CodecID id;
  147. int ret;
  148. switch (type) {
  149. case 0xd: id = CODEC_ID_MJPEG; break;
  150. case 0xe: id = CODEC_ID_PNG; break;
  151. case 0x1b: id = CODEC_ID_BMP; break;
  152. default:
  153. av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
  154. avio_skip(pb, len);
  155. return 0;
  156. }
  157. st = avformat_new_stream(c->fc, NULL);
  158. if (!st)
  159. return AVERROR(ENOMEM);
  160. sc = av_mallocz(sizeof(*sc));
  161. if (!sc)
  162. return AVERROR(ENOMEM);
  163. st->priv_data = sc;
  164. ret = av_get_packet(pb, &pkt, len);
  165. if (ret < 0)
  166. return ret;
  167. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  168. st->attached_pic = pkt;
  169. st->attached_pic.stream_index = st->index;
  170. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  171. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  172. st->codec->codec_id = id;
  173. return 0;
  174. }
  175. static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  176. {
  177. #ifdef MOV_EXPORT_ALL_METADATA
  178. char tmp_key[5];
  179. #endif
  180. char str[1024], key2[16], language[4] = {0};
  181. const char *key = NULL;
  182. uint16_t langcode = 0;
  183. uint32_t data_type = 0, str_size;
  184. int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
  185. switch (atom.type) {
  186. case MKTAG(0xa9,'n','a','m'): key = "title"; break;
  187. case MKTAG(0xa9,'a','u','t'):
  188. case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
  189. case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
  190. case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
  191. case MKTAG( 'c','p','r','t'):
  192. case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
  193. case MKTAG(0xa9,'c','m','t'):
  194. case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
  195. case MKTAG(0xa9,'a','l','b'): key = "album"; break;
  196. case MKTAG(0xa9,'d','a','y'): key = "date"; break;
  197. case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
  198. case MKTAG( 'g','n','r','e'): key = "genre";
  199. parse = mov_metadata_gnre; break;
  200. case MKTAG(0xa9,'t','o','o'):
  201. case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
  202. case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
  203. case MKTAG( 'd','e','s','c'): key = "description";break;
  204. case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
  205. case MKTAG( 't','v','s','h'): key = "show"; break;
  206. case MKTAG( 't','v','e','n'): key = "episode_id";break;
  207. case MKTAG( 't','v','n','n'): key = "network"; break;
  208. case MKTAG( 't','r','k','n'): key = "track";
  209. parse = mov_metadata_track_or_disc_number; break;
  210. case MKTAG( 'd','i','s','k'): key = "disc";
  211. parse = mov_metadata_track_or_disc_number; break;
  212. case MKTAG( 't','v','e','s'): key = "episode_sort";
  213. parse = mov_metadata_int8_bypass_padding; break;
  214. case MKTAG( 't','v','s','n'): key = "season_number";
  215. parse = mov_metadata_int8_bypass_padding; break;
  216. case MKTAG( 's','t','i','k'): key = "media_type";
  217. parse = mov_metadata_int8_no_padding; break;
  218. case MKTAG( 'h','d','v','d'): key = "hd_video";
  219. parse = mov_metadata_int8_no_padding; break;
  220. case MKTAG( 'p','g','a','p'): key = "gapless_playback";
  221. parse = mov_metadata_int8_no_padding; break;
  222. }
  223. if (c->itunes_metadata && atom.size > 8) {
  224. int data_size = avio_rb32(pb);
  225. int tag = avio_rl32(pb);
  226. if (tag == MKTAG('d','a','t','a')) {
  227. data_type = avio_rb32(pb); // type
  228. avio_rb32(pb); // unknown
  229. str_size = data_size - 16;
  230. atom.size -= 16;
  231. if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
  232. int ret = mov_read_covr(c, pb, data_type, str_size);
  233. if (ret < 0) {
  234. av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
  235. return ret;
  236. }
  237. }
  238. } else return 0;
  239. } else if (atom.size > 4 && key && !c->itunes_metadata) {
  240. str_size = avio_rb16(pb); // string length
  241. langcode = avio_rb16(pb);
  242. ff_mov_lang_to_iso639(langcode, language);
  243. atom.size -= 4;
  244. } else
  245. str_size = atom.size;
  246. #ifdef MOV_EXPORT_ALL_METADATA
  247. if (!key) {
  248. snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
  249. key = tmp_key;
  250. }
  251. #endif
  252. if (!key)
  253. return 0;
  254. if (atom.size < 0)
  255. return AVERROR_INVALIDDATA;
  256. str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
  257. if (parse)
  258. parse(c, pb, str_size, key);
  259. else {
  260. if (data_type == 3 || (data_type == 0 && langcode < 0x800)) { // MAC Encoded
  261. mov_read_mac_string(c, pb, str_size, str, sizeof(str));
  262. } else {
  263. avio_read(pb, str, str_size);
  264. str[str_size] = 0;
  265. }
  266. av_dict_set(&c->fc->metadata, key, str, 0);
  267. if (*language && strcmp(language, "und")) {
  268. snprintf(key2, sizeof(key2), "%s-%s", key, language);
  269. av_dict_set(&c->fc->metadata, key2, str, 0);
  270. }
  271. }
  272. av_dlog(c->fc, "lang \"%3s\" ", language);
  273. av_dlog(c->fc, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
  274. key, str, (char*)&atom.type, str_size, atom.size);
  275. return 0;
  276. }
  277. static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  278. {
  279. int64_t start;
  280. int i, nb_chapters, str_len, version;
  281. char str[256+1];
  282. if ((atom.size -= 5) < 0)
  283. return 0;
  284. version = avio_r8(pb);
  285. avio_rb24(pb);
  286. if (version)
  287. avio_rb32(pb); // ???
  288. nb_chapters = avio_r8(pb);
  289. for (i = 0; i < nb_chapters; i++) {
  290. if (atom.size < 9)
  291. return 0;
  292. start = avio_rb64(pb);
  293. str_len = avio_r8(pb);
  294. if ((atom.size -= 9+str_len) < 0)
  295. return 0;
  296. avio_read(pb, str, str_len);
  297. str[str_len] = 0;
  298. avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
  299. }
  300. return 0;
  301. }
  302. static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  303. {
  304. AVStream *st;
  305. MOVStreamContext *sc;
  306. int entries, i, j;
  307. if (c->fc->nb_streams < 1)
  308. return 0;
  309. st = c->fc->streams[c->fc->nb_streams-1];
  310. sc = st->priv_data;
  311. avio_rb32(pb); // version + flags
  312. entries = avio_rb32(pb);
  313. if (entries >= UINT_MAX / sizeof(*sc->drefs))
  314. return AVERROR_INVALIDDATA;
  315. av_free(sc->drefs);
  316. sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
  317. if (!sc->drefs)
  318. return AVERROR(ENOMEM);
  319. sc->drefs_count = entries;
  320. for (i = 0; i < sc->drefs_count; i++) {
  321. MOVDref *dref = &sc->drefs[i];
  322. uint32_t size = avio_rb32(pb);
  323. int64_t next = avio_tell(pb) + size - 4;
  324. if (size < 12)
  325. return AVERROR_INVALIDDATA;
  326. dref->type = avio_rl32(pb);
  327. avio_rb32(pb); // version + flags
  328. av_dlog(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
  329. if (dref->type == MKTAG('a','l','i','s') && size > 150) {
  330. /* macintosh alias record */
  331. uint16_t volume_len, len;
  332. int16_t type;
  333. avio_skip(pb, 10);
  334. volume_len = avio_r8(pb);
  335. volume_len = FFMIN(volume_len, 27);
  336. avio_read(pb, dref->volume, 27);
  337. dref->volume[volume_len] = 0;
  338. av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
  339. avio_skip(pb, 12);
  340. len = avio_r8(pb);
  341. len = FFMIN(len, 63);
  342. avio_read(pb, dref->filename, 63);
  343. dref->filename[len] = 0;
  344. av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
  345. avio_skip(pb, 16);
  346. /* read next level up_from_alias/down_to_target */
  347. dref->nlvl_from = avio_rb16(pb);
  348. dref->nlvl_to = avio_rb16(pb);
  349. av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
  350. dref->nlvl_from, dref->nlvl_to);
  351. avio_skip(pb, 16);
  352. for (type = 0; type != -1 && avio_tell(pb) < next; ) {
  353. type = avio_rb16(pb);
  354. len = avio_rb16(pb);
  355. av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
  356. if (len&1)
  357. len += 1;
  358. if (type == 2) { // absolute path
  359. av_free(dref->path);
  360. dref->path = av_mallocz(len+1);
  361. if (!dref->path)
  362. return AVERROR(ENOMEM);
  363. avio_read(pb, dref->path, len);
  364. if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
  365. len -= volume_len;
  366. memmove(dref->path, dref->path+volume_len, len);
  367. dref->path[len] = 0;
  368. }
  369. for (j = 0; j < len; j++)
  370. if (dref->path[j] == ':')
  371. dref->path[j] = '/';
  372. av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
  373. } else if (type == 0) { // directory name
  374. av_free(dref->dir);
  375. dref->dir = av_malloc(len+1);
  376. if (!dref->dir)
  377. return AVERROR(ENOMEM);
  378. avio_read(pb, dref->dir, len);
  379. dref->dir[len] = 0;
  380. for (j = 0; j < len; j++)
  381. if (dref->dir[j] == ':')
  382. dref->dir[j] = '/';
  383. av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
  384. } else
  385. avio_skip(pb, len);
  386. }
  387. }
  388. avio_seek(pb, next, SEEK_SET);
  389. }
  390. return 0;
  391. }
  392. static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  393. {
  394. AVStream *st;
  395. uint32_t type;
  396. uint32_t av_unused ctype;
  397. if (c->fc->nb_streams < 1) // meta before first trak
  398. return 0;
  399. st = c->fc->streams[c->fc->nb_streams-1];
  400. avio_r8(pb); /* version */
  401. avio_rb24(pb); /* flags */
  402. /* component type */
  403. ctype = avio_rl32(pb);
  404. type = avio_rl32(pb); /* component subtype */
  405. av_dlog(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
  406. av_dlog(c->fc, "stype= %.4s\n", (char*)&type);
  407. if (type == MKTAG('v','i','d','e'))
  408. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  409. else if (type == MKTAG('s','o','u','n'))
  410. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  411. else if (type == MKTAG('m','1','a',' '))
  412. st->codec->codec_id = CODEC_ID_MP2;
  413. else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
  414. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  415. avio_rb32(pb); /* component manufacture */
  416. avio_rb32(pb); /* component flags */
  417. avio_rb32(pb); /* component flags mask */
  418. return 0;
  419. }
  420. int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb, MOVAtom atom)
  421. {
  422. AVStream *st;
  423. int tag;
  424. if (fc->nb_streams < 1)
  425. return 0;
  426. st = fc->streams[fc->nb_streams-1];
  427. avio_rb32(pb); /* version + flags */
  428. ff_mp4_read_descr(fc, pb, &tag);
  429. if (tag == MP4ESDescrTag) {
  430. ff_mp4_parse_es_descr(pb, NULL);
  431. } else
  432. avio_rb16(pb); /* ID */
  433. ff_mp4_read_descr(fc, pb, &tag);
  434. if (tag == MP4DecConfigDescrTag)
  435. ff_mp4_read_dec_config_descr(fc, st, pb);
  436. return 0;
  437. }
  438. static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  439. {
  440. return ff_mov_read_esds(c->fc, pb, atom);
  441. }
  442. static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  443. {
  444. AVStream *st;
  445. int ac3info, acmod, lfeon, bsmod;
  446. if (c->fc->nb_streams < 1)
  447. return 0;
  448. st = c->fc->streams[c->fc->nb_streams-1];
  449. ac3info = avio_rb24(pb);
  450. bsmod = (ac3info >> 14) & 0x7;
  451. acmod = (ac3info >> 11) & 0x7;
  452. lfeon = (ac3info >> 10) & 0x1;
  453. st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
  454. st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
  455. if (lfeon)
  456. st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
  457. st->codec->audio_service_type = bsmod;
  458. if (st->codec->channels > 1 && bsmod == 0x7)
  459. st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  460. return 0;
  461. }
  462. static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  463. {
  464. AVStream *st;
  465. int eac3info, acmod, lfeon, bsmod;
  466. if (c->fc->nb_streams < 1)
  467. return 0;
  468. st = c->fc->streams[c->fc->nb_streams-1];
  469. /* No need to parse fields for additional independent substreams and its
  470. * associated dependent substreams since libavcodec's E-AC-3 decoder
  471. * does not support them yet. */
  472. avio_rb16(pb); /* data_rate and num_ind_sub */
  473. eac3info = avio_rb24(pb);
  474. bsmod = (eac3info >> 12) & 0x1f;
  475. acmod = (eac3info >> 9) & 0x7;
  476. lfeon = (eac3info >> 8) & 0x1;
  477. st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
  478. if (lfeon)
  479. st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
  480. st->codec->channels = av_get_channel_layout_nb_channels(st->codec->channel_layout);
  481. st->codec->audio_service_type = bsmod;
  482. if (st->codec->channels > 1 && bsmod == 0x7)
  483. st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  484. return 0;
  485. }
  486. static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  487. {
  488. AVStream *st;
  489. if (c->fc->nb_streams < 1)
  490. return 0;
  491. st = c->fc->streams[c->fc->nb_streams-1];
  492. if (atom.size < 16)
  493. return 0;
  494. ff_mov_read_chan(c->fc, st, atom.size - 4);
  495. return 0;
  496. }
  497. static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  498. {
  499. AVStream *st;
  500. if (c->fc->nb_streams < 1)
  501. return 0;
  502. st = c->fc->streams[c->fc->nb_streams-1];
  503. ff_get_wav_header(pb, st->codec, atom.size);
  504. return 0;
  505. }
  506. static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  507. {
  508. const int num = avio_rb32(pb);
  509. const int den = avio_rb32(pb);
  510. AVStream *st;
  511. if (c->fc->nb_streams < 1)
  512. return 0;
  513. st = c->fc->streams[c->fc->nb_streams-1];
  514. if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
  515. (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
  516. av_log(c->fc, AV_LOG_WARNING,
  517. "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
  518. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  519. num, den);
  520. } else if (den != 0) {
  521. st->sample_aspect_ratio.num = num;
  522. st->sample_aspect_ratio.den = den;
  523. }
  524. return 0;
  525. }
  526. /* this atom contains actual media data */
  527. static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  528. {
  529. if (atom.size == 0) /* wrong one (MP4) */
  530. return 0;
  531. c->found_mdat=1;
  532. return 0; /* now go for moov */
  533. }
  534. /* read major brand, minor version and compatible brands and store them as metadata */
  535. static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  536. {
  537. uint32_t minor_ver;
  538. int comp_brand_size;
  539. char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
  540. char* comp_brands_str;
  541. uint8_t type[5] = {0};
  542. avio_read(pb, type, 4);
  543. if (strcmp(type, "qt "))
  544. c->isom = 1;
  545. av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
  546. av_dict_set(&c->fc->metadata, "major_brand", type, 0);
  547. minor_ver = avio_rb32(pb); /* minor version */
  548. snprintf(minor_ver_str, sizeof(minor_ver_str), "%d", minor_ver);
  549. av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);
  550. comp_brand_size = atom.size - 8;
  551. if (comp_brand_size < 0)
  552. return AVERROR_INVALIDDATA;
  553. comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
  554. if (!comp_brands_str)
  555. return AVERROR(ENOMEM);
  556. avio_read(pb, comp_brands_str, comp_brand_size);
  557. comp_brands_str[comp_brand_size] = 0;
  558. av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
  559. av_freep(&comp_brands_str);
  560. return 0;
  561. }
  562. /* this atom should contain all header atoms */
  563. static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  564. {
  565. int ret;
  566. if ((ret = mov_read_default(c, pb, atom)) < 0)
  567. return ret;
  568. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  569. /* so we don't parse the whole file if over a network */
  570. c->found_moov=1;
  571. return 0; /* now go for mdat */
  572. }
  573. static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  574. {
  575. c->fragment.moof_offset = avio_tell(pb) - 8;
  576. av_dlog(c->fc, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
  577. return mov_read_default(c, pb, atom);
  578. }
  579. static void mov_metadata_creation_time(AVDictionary **metadata, time_t time)
  580. {
  581. char buffer[32];
  582. if (time) {
  583. struct tm *ptm;
  584. time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
  585. ptm = gmtime(&time);
  586. if (!ptm) return;
  587. strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
  588. av_dict_set(metadata, "creation_time", buffer, 0);
  589. }
  590. }
  591. static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  592. {
  593. AVStream *st;
  594. MOVStreamContext *sc;
  595. int version;
  596. char language[4] = {0};
  597. unsigned lang;
  598. time_t creation_time;
  599. if (c->fc->nb_streams < 1)
  600. return 0;
  601. st = c->fc->streams[c->fc->nb_streams-1];
  602. sc = st->priv_data;
  603. version = avio_r8(pb);
  604. if (version > 1) {
  605. av_log_ask_for_sample(c, "unsupported version %d\n", version);
  606. return AVERROR_PATCHWELCOME;
  607. }
  608. avio_rb24(pb); /* flags */
  609. if (version == 1) {
  610. creation_time = avio_rb64(pb);
  611. avio_rb64(pb);
  612. } else {
  613. creation_time = avio_rb32(pb);
  614. avio_rb32(pb); /* modification time */
  615. }
  616. mov_metadata_creation_time(&st->metadata, creation_time);
  617. sc->time_scale = avio_rb32(pb);
  618. st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  619. lang = avio_rb16(pb); /* language */
  620. if (ff_mov_lang_to_iso639(lang, language))
  621. av_dict_set(&st->metadata, "language", language, 0);
  622. avio_rb16(pb); /* quality */
  623. return 0;
  624. }
  625. static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  626. {
  627. time_t creation_time;
  628. int version = avio_r8(pb); /* version */
  629. avio_rb24(pb); /* flags */
  630. if (version == 1) {
  631. creation_time = avio_rb64(pb);
  632. avio_rb64(pb);
  633. } else {
  634. creation_time = avio_rb32(pb);
  635. avio_rb32(pb); /* modification time */
  636. }
  637. mov_metadata_creation_time(&c->fc->metadata, creation_time);
  638. c->time_scale = avio_rb32(pb); /* time scale */
  639. av_dlog(c->fc, "time scale = %i\n", c->time_scale);
  640. c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  641. avio_rb32(pb); /* preferred scale */
  642. avio_rb16(pb); /* preferred volume */
  643. avio_skip(pb, 10); /* reserved */
  644. avio_skip(pb, 36); /* display matrix */
  645. avio_rb32(pb); /* preview time */
  646. avio_rb32(pb); /* preview duration */
  647. avio_rb32(pb); /* poster time */
  648. avio_rb32(pb); /* selection time */
  649. avio_rb32(pb); /* selection duration */
  650. avio_rb32(pb); /* current time */
  651. avio_rb32(pb); /* next track ID */
  652. return 0;
  653. }
  654. static int mov_read_smi(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  655. {
  656. AVStream *st;
  657. if (c->fc->nb_streams < 1)
  658. return 0;
  659. st = c->fc->streams[c->fc->nb_streams-1];
  660. if ((uint64_t)atom.size > (1<<30))
  661. return AVERROR_INVALIDDATA;
  662. // currently SVQ3 decoder expect full STSD header - so let's fake it
  663. // this should be fixed and just SMI header should be passed
  664. av_free(st->codec->extradata);
  665. st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE);
  666. if (!st->codec->extradata)
  667. return AVERROR(ENOMEM);
  668. st->codec->extradata_size = 0x5a + atom.size;
  669. memcpy(st->codec->extradata, "SVQ3", 4); // fake
  670. avio_read(pb, st->codec->extradata + 0x5a, atom.size);
  671. av_dlog(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
  672. return 0;
  673. }
  674. static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  675. {
  676. AVStream *st;
  677. int little_endian;
  678. if (c->fc->nb_streams < 1)
  679. return 0;
  680. st = c->fc->streams[c->fc->nb_streams-1];
  681. little_endian = avio_rb16(pb);
  682. av_dlog(c->fc, "enda %d\n", little_endian);
  683. if (little_endian == 1) {
  684. switch (st->codec->codec_id) {
  685. case CODEC_ID_PCM_S24BE:
  686. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  687. break;
  688. case CODEC_ID_PCM_S32BE:
  689. st->codec->codec_id = CODEC_ID_PCM_S32LE;
  690. break;
  691. case CODEC_ID_PCM_F32BE:
  692. st->codec->codec_id = CODEC_ID_PCM_F32LE;
  693. break;
  694. case CODEC_ID_PCM_F64BE:
  695. st->codec->codec_id = CODEC_ID_PCM_F64LE;
  696. break;
  697. default:
  698. break;
  699. }
  700. }
  701. return 0;
  702. }
  703. static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  704. {
  705. AVStream *st;
  706. unsigned mov_field_order;
  707. enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
  708. if (c->fc->nb_streams < 1) // will happen with jp2 files
  709. return 0;
  710. st = c->fc->streams[c->fc->nb_streams-1];
  711. if (atom.size < 2)
  712. return AVERROR_INVALIDDATA;
  713. mov_field_order = avio_rb16(pb);
  714. if ((mov_field_order & 0xFF00) == 0x0100)
  715. decoded_field_order = AV_FIELD_PROGRESSIVE;
  716. else if ((mov_field_order & 0xFF00) == 0x0200) {
  717. switch (mov_field_order & 0xFF) {
  718. case 0x01: decoded_field_order = AV_FIELD_TT;
  719. break;
  720. case 0x06: decoded_field_order = AV_FIELD_BB;
  721. break;
  722. case 0x09: decoded_field_order = AV_FIELD_TB;
  723. break;
  724. case 0x0E: decoded_field_order = AV_FIELD_BT;
  725. break;
  726. }
  727. }
  728. if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
  729. av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
  730. }
  731. st->codec->field_order = decoded_field_order;
  732. return 0;
  733. }
  734. /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
  735. static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  736. {
  737. AVStream *st;
  738. uint64_t size;
  739. uint8_t *buf;
  740. if (c->fc->nb_streams < 1) // will happen with jp2 files
  741. return 0;
  742. st= c->fc->streams[c->fc->nb_streams-1];
  743. size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
  744. if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
  745. return AVERROR_INVALIDDATA;
  746. buf= av_realloc(st->codec->extradata, size);
  747. if (!buf)
  748. return AVERROR(ENOMEM);
  749. st->codec->extradata= buf;
  750. buf+= st->codec->extradata_size;
  751. st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
  752. AV_WB32( buf , atom.size + 8);
  753. AV_WL32( buf + 4, atom.type);
  754. avio_read(pb, buf + 8, atom.size);
  755. return 0;
  756. }
  757. static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  758. {
  759. AVStream *st;
  760. if (c->fc->nb_streams < 1)
  761. return 0;
  762. st = c->fc->streams[c->fc->nb_streams-1];
  763. if ((uint64_t)atom.size > (1<<30))
  764. return AVERROR_INVALIDDATA;
  765. if (st->codec->codec_id == CODEC_ID_QDM2 || st->codec->codec_id == CODEC_ID_QDMC) {
  766. // pass all frma atom to codec, needed at least for QDMC and QDM2
  767. av_free(st->codec->extradata);
  768. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  769. if (!st->codec->extradata)
  770. return AVERROR(ENOMEM);
  771. st->codec->extradata_size = atom.size;
  772. avio_read(pb, st->codec->extradata, atom.size);
  773. } else if (atom.size > 8) { /* to read frma, esds atoms */
  774. int ret;
  775. if ((ret = mov_read_default(c, pb, atom)) < 0)
  776. return ret;
  777. } else
  778. avio_skip(pb, atom.size);
  779. return 0;
  780. }
  781. /**
  782. * This function reads atom content and puts data in extradata without tag
  783. * nor size unlike mov_read_extradata.
  784. */
  785. static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  786. {
  787. AVStream *st;
  788. if (c->fc->nb_streams < 1)
  789. return 0;
  790. st = c->fc->streams[c->fc->nb_streams-1];
  791. if ((uint64_t)atom.size > (1<<30))
  792. return AVERROR_INVALIDDATA;
  793. if (atom.size >= 10) {
  794. // Broken files created by legacy versions of libavformat will
  795. // wrap a whole fiel atom inside of a glbl atom.
  796. unsigned size = avio_rb32(pb);
  797. unsigned type = avio_rl32(pb);
  798. avio_seek(pb, -8, SEEK_CUR);
  799. if (type == MKTAG('f','i','e','l') && size == atom.size)
  800. return mov_read_default(c, pb, atom);
  801. }
  802. av_free(st->codec->extradata);
  803. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  804. if (!st->codec->extradata)
  805. return AVERROR(ENOMEM);
  806. st->codec->extradata_size = atom.size;
  807. avio_read(pb, st->codec->extradata, atom.size);
  808. return 0;
  809. }
  810. static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  811. {
  812. AVStream *st;
  813. uint8_t profile_level;
  814. if (c->fc->nb_streams < 1)
  815. return 0;
  816. st = c->fc->streams[c->fc->nb_streams-1];
  817. if (atom.size >= (1<<28) || atom.size < 7)
  818. return AVERROR_INVALIDDATA;
  819. profile_level = avio_r8(pb);
  820. if ((profile_level & 0xf0) != 0xc0)
  821. return 0;
  822. av_free(st->codec->extradata);
  823. st->codec->extradata = av_mallocz(atom.size - 7 + FF_INPUT_BUFFER_PADDING_SIZE);
  824. if (!st->codec->extradata)
  825. return AVERROR(ENOMEM);
  826. st->codec->extradata_size = atom.size - 7;
  827. avio_seek(pb, 6, SEEK_CUR);
  828. avio_read(pb, st->codec->extradata, st->codec->extradata_size);
  829. return 0;
  830. }
  831. /**
  832. * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
  833. * but can have extradata appended at the end after the 40 bytes belonging
  834. * to the struct.
  835. */
  836. static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  837. {
  838. AVStream *st;
  839. if (c->fc->nb_streams < 1)
  840. return 0;
  841. if (atom.size <= 40)
  842. return 0;
  843. st = c->fc->streams[c->fc->nb_streams-1];
  844. if ((uint64_t)atom.size > (1<<30))
  845. return AVERROR_INVALIDDATA;
  846. av_free(st->codec->extradata);
  847. st->codec->extradata = av_mallocz(atom.size - 40 + FF_INPUT_BUFFER_PADDING_SIZE);
  848. if (!st->codec->extradata)
  849. return AVERROR(ENOMEM);
  850. st->codec->extradata_size = atom.size - 40;
  851. avio_skip(pb, 40);
  852. avio_read(pb, st->codec->extradata, atom.size - 40);
  853. return 0;
  854. }
  855. static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  856. {
  857. AVStream *st;
  858. MOVStreamContext *sc;
  859. unsigned int i, entries;
  860. if (c->fc->nb_streams < 1)
  861. return 0;
  862. st = c->fc->streams[c->fc->nb_streams-1];
  863. sc = st->priv_data;
  864. avio_r8(pb); /* version */
  865. avio_rb24(pb); /* flags */
  866. entries = avio_rb32(pb);
  867. if (!entries)
  868. return 0;
  869. if (entries >= UINT_MAX/sizeof(int64_t))
  870. return AVERROR_INVALIDDATA;
  871. sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
  872. if (!sc->chunk_offsets)
  873. return AVERROR(ENOMEM);
  874. sc->chunk_count = entries;
  875. if (atom.type == MKTAG('s','t','c','o'))
  876. for (i=0; i<entries; i++)
  877. sc->chunk_offsets[i] = avio_rb32(pb);
  878. else if (atom.type == MKTAG('c','o','6','4'))
  879. for (i=0; i<entries; i++)
  880. sc->chunk_offsets[i] = avio_rb64(pb);
  881. else
  882. return AVERROR_INVALIDDATA;
  883. return 0;
  884. }
  885. /**
  886. * Compute codec id for 'lpcm' tag.
  887. * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
  888. */
  889. enum CodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
  890. {
  891. if (flags & 1) { // floating point
  892. if (flags & 2) { // big endian
  893. if (bps == 32) return CODEC_ID_PCM_F32BE;
  894. else if (bps == 64) return CODEC_ID_PCM_F64BE;
  895. } else {
  896. if (bps == 32) return CODEC_ID_PCM_F32LE;
  897. else if (bps == 64) return CODEC_ID_PCM_F64LE;
  898. }
  899. } else {
  900. if (flags & 2) {
  901. if (bps == 8)
  902. // signed integer
  903. if (flags & 4) return CODEC_ID_PCM_S8;
  904. else return CODEC_ID_PCM_U8;
  905. else if (bps == 16) return CODEC_ID_PCM_S16BE;
  906. else if (bps == 24) return CODEC_ID_PCM_S24BE;
  907. else if (bps == 32) return CODEC_ID_PCM_S32BE;
  908. } else {
  909. if (bps == 8)
  910. if (flags & 4) return CODEC_ID_PCM_S8;
  911. else return CODEC_ID_PCM_U8;
  912. else if (bps == 16) return CODEC_ID_PCM_S16LE;
  913. else if (bps == 24) return CODEC_ID_PCM_S24LE;
  914. else if (bps == 32) return CODEC_ID_PCM_S32LE;
  915. }
  916. }
  917. return CODEC_ID_NONE;
  918. }
  919. int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
  920. {
  921. AVStream *st;
  922. MOVStreamContext *sc;
  923. int j, pseudo_stream_id;
  924. if (c->fc->nb_streams < 1)
  925. return 0;
  926. st = c->fc->streams[c->fc->nb_streams-1];
  927. sc = st->priv_data;
  928. for (pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
  929. //Parsing Sample description table
  930. enum CodecID id;
  931. int dref_id = 1;
  932. MOVAtom a = { AV_RL32("stsd") };
  933. int64_t start_pos = avio_tell(pb);
  934. int size = avio_rb32(pb); /* size */
  935. uint32_t format = avio_rl32(pb); /* data format */
  936. if (size >= 16) {
  937. avio_rb32(pb); /* reserved */
  938. avio_rb16(pb); /* reserved */
  939. dref_id = avio_rb16(pb);
  940. }
  941. if (st->codec->codec_tag &&
  942. st->codec->codec_tag != format &&
  943. (c->fc->video_codec_id ? ff_codec_get_id(ff_codec_movvideo_tags, format) != c->fc->video_codec_id
  944. : st->codec->codec_tag != MKTAG('j','p','e','g'))
  945. ){
  946. /* Multiple fourcc, we skip JPEG. This is not correct, we should
  947. * export it as a separate AVStream but this needs a few changes
  948. * in the MOV demuxer, patch welcome. */
  949. multiple_stsd:
  950. av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
  951. avio_skip(pb, size - (avio_tell(pb) - start_pos));
  952. continue;
  953. }
  954. /* we cannot demux concatenated h264 streams because of different extradata */
  955. if (st->codec->codec_tag && st->codec->codec_tag == AV_RL32("avc1"))
  956. goto multiple_stsd;
  957. sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
  958. sc->dref_id= dref_id;
  959. st->codec->codec_tag = format;
  960. id = ff_codec_get_id(ff_codec_movaudio_tags, format);
  961. if (id<=0 && ((format&0xFFFF) == 'm'+('s'<<8) || (format&0xFFFF) == 'T'+('S'<<8)))
  962. id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format)&0xFFFF);
  963. if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
  964. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  965. } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO && /* do not overwrite codec type */
  966. format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */
  967. id = ff_codec_get_id(ff_codec_movvideo_tags, format);
  968. if (id <= 0)
  969. id = ff_codec_get_id(ff_codec_bmp_tags, format);
  970. if (id > 0)
  971. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  972. else if (st->codec->codec_type == AVMEDIA_TYPE_DATA){
  973. id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
  974. if (id > 0)
  975. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  976. }
  977. }
  978. av_dlog(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
  979. (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
  980. (format >> 24) & 0xff, st->codec->codec_type);
  981. if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
  982. unsigned int color_depth, len;
  983. int color_greyscale;
  984. int color_table_id;
  985. st->codec->codec_id = id;
  986. avio_rb16(pb); /* version */
  987. avio_rb16(pb); /* revision level */
  988. avio_rb32(pb); /* vendor */
  989. avio_rb32(pb); /* temporal quality */
  990. avio_rb32(pb); /* spatial quality */
  991. st->codec->width = avio_rb16(pb); /* width */
  992. st->codec->height = avio_rb16(pb); /* height */
  993. avio_rb32(pb); /* horiz resolution */
  994. avio_rb32(pb); /* vert resolution */
  995. avio_rb32(pb); /* data size, always 0 */
  996. avio_rb16(pb); /* frames per samples */
  997. len = avio_r8(pb); /* codec name, pascal string */
  998. if (len > 31)
  999. len = 31;
  1000. mov_read_mac_string(c, pb, len, st->codec->codec_name, 32);
  1001. if (len < 31)
  1002. avio_skip(pb, 31 - len);
  1003. /* codec_tag YV12 triggers an UV swap in rawdec.c */
  1004. if (!memcmp(st->codec->codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25))
  1005. st->codec->codec_tag=MKTAG('I', '4', '2', '0');
  1006. st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
  1007. color_table_id = avio_rb16(pb); /* colortable id */
  1008. av_dlog(c->fc, "depth %d, ctab id %d\n",
  1009. st->codec->bits_per_coded_sample, color_table_id);
  1010. /* figure out the palette situation */
  1011. color_depth = st->codec->bits_per_coded_sample & 0x1F;
  1012. color_greyscale = st->codec->bits_per_coded_sample & 0x20;
  1013. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  1014. if ((color_depth == 2) || (color_depth == 4) ||
  1015. (color_depth == 8)) {
  1016. /* for palette traversal */
  1017. unsigned int color_start, color_count, color_end;
  1018. unsigned char r, g, b;
  1019. if (color_greyscale) {
  1020. int color_index, color_dec;
  1021. /* compute the greyscale palette */
  1022. st->codec->bits_per_coded_sample = color_depth;
  1023. color_count = 1 << color_depth;
  1024. color_index = 255;
  1025. color_dec = 256 / (color_count - 1);
  1026. for (j = 0; j < color_count; j++) {
  1027. r = g = b = color_index;
  1028. sc->palette[j] =
  1029. (r << 16) | (g << 8) | (b);
  1030. color_index -= color_dec;
  1031. if (color_index < 0)
  1032. color_index = 0;
  1033. }
  1034. } else if (color_table_id) {
  1035. const uint8_t *color_table;
  1036. /* if flag bit 3 is set, use the default palette */
  1037. color_count = 1 << color_depth;
  1038. if (color_depth == 2)
  1039. color_table = ff_qt_default_palette_4;
  1040. else if (color_depth == 4)
  1041. color_table = ff_qt_default_palette_16;
  1042. else
  1043. color_table = ff_qt_default_palette_256;
  1044. for (j = 0; j < color_count; j++) {
  1045. r = color_table[j * 3 + 0];
  1046. g = color_table[j * 3 + 1];
  1047. b = color_table[j * 3 + 2];
  1048. sc->palette[j] =
  1049. (r << 16) | (g << 8) | (b);
  1050. }
  1051. } else {
  1052. /* load the palette from the file */
  1053. color_start = avio_rb32(pb);
  1054. color_count = avio_rb16(pb);
  1055. color_end = avio_rb16(pb);
  1056. if ((color_start <= 255) &&
  1057. (color_end <= 255)) {
  1058. for (j = color_start; j <= color_end; j++) {
  1059. /* each R, G, or B component is 16 bits;
  1060. * only use the top 8 bits; skip alpha bytes
  1061. * up front */
  1062. avio_r8(pb);
  1063. avio_r8(pb);
  1064. r = avio_r8(pb);
  1065. avio_r8(pb);
  1066. g = avio_r8(pb);
  1067. avio_r8(pb);
  1068. b = avio_r8(pb);
  1069. avio_r8(pb);
  1070. sc->palette[j] =
  1071. (r << 16) | (g << 8) | (b);
  1072. }
  1073. }
  1074. }
  1075. sc->has_palette = 1;
  1076. }
  1077. } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
  1078. int bits_per_sample, flags;
  1079. uint16_t version = avio_rb16(pb);
  1080. st->codec->codec_id = id;
  1081. avio_rb16(pb); /* revision level */
  1082. avio_rb32(pb); /* vendor */
  1083. st->codec->channels = avio_rb16(pb); /* channel count */
  1084. av_dlog(c->fc, "audio channels %d\n", st->codec->channels);
  1085. st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
  1086. sc->audio_cid = avio_rb16(pb);
  1087. avio_rb16(pb); /* packet size = 0 */
  1088. st->codec->sample_rate = ((avio_rb32(pb) >> 16));
  1089. //Read QT version 1 fields. In version 0 these do not exist.
  1090. av_dlog(c->fc, "version =%d, isom =%d\n",version,c->isom);
  1091. if (!c->isom) {
  1092. if (version==1) {
  1093. sc->samples_per_frame = avio_rb32(pb);
  1094. avio_rb32(pb); /* bytes per packet */
  1095. sc->bytes_per_frame = avio_rb32(pb);
  1096. avio_rb32(pb); /* bytes per sample */
  1097. } else if (version==2) {
  1098. avio_rb32(pb); /* sizeof struct only */
  1099. st->codec->sample_rate = av_int2double(avio_rb64(pb)); /* float 64 */
  1100. st->codec->channels = avio_rb32(pb);
  1101. avio_rb32(pb); /* always 0x7F000000 */
  1102. st->codec->bits_per_coded_sample = avio_rb32(pb); /* bits per channel if sound is uncompressed */
  1103. flags = avio_rb32(pb); /* lpcm format specific flag */
  1104. sc->bytes_per_frame = avio_rb32(pb); /* bytes per audio packet if constant */
  1105. sc->samples_per_frame = avio_rb32(pb); /* lpcm frames per audio packet if constant */
  1106. if (format == MKTAG('l','p','c','m'))
  1107. st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
  1108. }
  1109. }
  1110. switch (st->codec->codec_id) {
  1111. case CODEC_ID_PCM_S8:
  1112. case CODEC_ID_PCM_U8:
  1113. if (st->codec->bits_per_coded_sample == 16)
  1114. st->codec->codec_id = CODEC_ID_PCM_S16BE;
  1115. break;
  1116. case CODEC_ID_PCM_S16LE:
  1117. case CODEC_ID_PCM_S16BE:
  1118. if (st->codec->bits_per_coded_sample == 8)
  1119. st->codec->codec_id = CODEC_ID_PCM_S8;
  1120. else if (st->codec->bits_per_coded_sample == 24)
  1121. st->codec->codec_id =
  1122. st->codec->codec_id == CODEC_ID_PCM_S16BE ?
  1123. CODEC_ID_PCM_S24BE : CODEC_ID_PCM_S24LE;
  1124. break;
  1125. /* set values for old format before stsd version 1 appeared */
  1126. case CODEC_ID_MACE3:
  1127. sc->samples_per_frame = 6;
  1128. sc->bytes_per_frame = 2*st->codec->channels;
  1129. break;
  1130. case CODEC_ID_MACE6:
  1131. sc->samples_per_frame = 6;
  1132. sc->bytes_per_frame = 1*st->codec->channels;
  1133. break;
  1134. case CODEC_ID_ADPCM_IMA_QT:
  1135. sc->samples_per_frame = 64;
  1136. sc->bytes_per_frame = 34*st->codec->channels;
  1137. break;
  1138. case CODEC_ID_GSM:
  1139. sc->samples_per_frame = 160;
  1140. sc->bytes_per_frame = 33;
  1141. break;
  1142. default:
  1143. break;
  1144. }
  1145. bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
  1146. if (bits_per_sample) {
  1147. st->codec->bits_per_coded_sample = bits_per_sample;
  1148. sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
  1149. }
  1150. } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
  1151. // ttxt stsd contains display flags, justification, background
  1152. // color, fonts, and default styles, so fake an atom to read it
  1153. MOVAtom fake_atom = { .size = size - (avio_tell(pb) - start_pos) };
  1154. if (format != AV_RL32("mp4s")) // mp4s contains a regular esds atom
  1155. mov_read_glbl(c, pb, fake_atom);
  1156. st->codec->codec_id= id;
  1157. st->codec->width = sc->width;
  1158. st->codec->height = sc->height;
  1159. } else {
  1160. /* other codec type, just skip (rtp, mp4s, tmcd ...) */
  1161. avio_skip(pb, size - (avio_tell(pb) - start_pos));
  1162. }
  1163. /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
  1164. a.size = size - (avio_tell(pb) - start_pos);
  1165. if (a.size > 8) {
  1166. int ret;
  1167. if ((ret = mov_read_default(c, pb, a)) < 0)
  1168. return ret;
  1169. } else if (a.size > 0)
  1170. avio_skip(pb, a.size);
  1171. }
  1172. if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
  1173. st->codec->sample_rate= sc->time_scale;
  1174. /* special codec parameters handling */
  1175. switch (st->codec->codec_id) {
  1176. #if CONFIG_DV_DEMUXER
  1177. case CODEC_ID_DVAUDIO:
  1178. c->dv_fctx = avformat_alloc_context();
  1179. c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
  1180. if (!c->dv_demux) {
  1181. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  1182. return AVERROR(ENOMEM);
  1183. }
  1184. sc->dv_audio_container = 1;
  1185. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  1186. break;
  1187. #endif
  1188. /* no ifdef since parameters are always those */
  1189. case CODEC_ID_QCELP:
  1190. // force sample rate for qcelp when not stored in mov
  1191. if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
  1192. st->codec->sample_rate = 8000;
  1193. st->codec->channels= 1; /* really needed */
  1194. break;
  1195. case CODEC_ID_AMR_NB:
  1196. st->codec->channels= 1; /* really needed */
  1197. /* force sample rate for amr, stsd in 3gp does not store sample rate */
  1198. st->codec->sample_rate = 8000;
  1199. break;
  1200. case CODEC_ID_AMR_WB:
  1201. st->codec->channels = 1;
  1202. st->codec->sample_rate = 16000;
  1203. break;
  1204. case CODEC_ID_MP2:
  1205. case CODEC_ID_MP3:
  1206. st->codec->codec_type = AVMEDIA_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
  1207. st->need_parsing = AVSTREAM_PARSE_FULL;
  1208. break;
  1209. case CODEC_ID_GSM:
  1210. case CODEC_ID_ADPCM_MS:
  1211. case CODEC_ID_ADPCM_IMA_WAV:
  1212. case CODEC_ID_ILBC:
  1213. st->codec->block_align = sc->bytes_per_frame;
  1214. break;
  1215. case CODEC_ID_ALAC:
  1216. if (st->codec->extradata_size == 36) {
  1217. st->codec->channels = AV_RB8 (st->codec->extradata+21);
  1218. st->codec->sample_rate = AV_RB32(st->codec->extradata+32);
  1219. }
  1220. break;
  1221. case CODEC_ID_VC1:
  1222. st->need_parsing = AVSTREAM_PARSE_FULL;
  1223. break;
  1224. default:
  1225. break;
  1226. }
  1227. return 0;
  1228. }
  1229. static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1230. {
  1231. int entries;
  1232. avio_r8(pb); /* version */
  1233. avio_rb24(pb); /* flags */
  1234. entries = avio_rb32(pb);
  1235. return ff_mov_read_stsd_entries(c, pb, entries);
  1236. }
  1237. static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1238. {
  1239. AVStream *st;
  1240. MOVStreamContext *sc;
  1241. unsigned int i, entries;
  1242. if (c->fc->nb_streams < 1)
  1243. return 0;
  1244. st = c->fc->streams[c->fc->nb_streams-1];
  1245. sc = st->priv_data;
  1246. avio_r8(pb); /* version */
  1247. avio_rb24(pb); /* flags */
  1248. entries = avio_rb32(pb);
  1249. av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  1250. if (!entries)
  1251. return 0;
  1252. if (entries >= UINT_MAX / sizeof(*sc->stsc_data))
  1253. return AVERROR_INVALIDDATA;
  1254. sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
  1255. if (!sc->stsc_data)
  1256. return AVERROR(ENOMEM);
  1257. sc->stsc_count = entries;
  1258. for (i=0; i<entries; i++) {
  1259. sc->stsc_data[i].first = avio_rb32(pb);
  1260. sc->stsc_data[i].count = avio_rb32(pb);
  1261. sc->stsc_data[i].id = avio_rb32(pb);
  1262. }
  1263. return 0;
  1264. }
  1265. static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1266. {
  1267. AVStream *st;
  1268. MOVStreamContext *sc;
  1269. unsigned i, entries;
  1270. if (c->fc->nb_streams < 1)
  1271. return 0;
  1272. st = c->fc->streams[c->fc->nb_streams-1];
  1273. sc = st->priv_data;
  1274. avio_rb32(pb); // version + flags
  1275. entries = avio_rb32(pb);
  1276. if (entries >= UINT_MAX / sizeof(*sc->stps_data))
  1277. return AVERROR_INVALIDDATA;
  1278. sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
  1279. if (!sc->stps_data)
  1280. return AVERROR(ENOMEM);
  1281. sc->stps_count = entries;
  1282. for (i = 0; i < entries; i++) {
  1283. sc->stps_data[i] = avio_rb32(pb);
  1284. //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]);
  1285. }
  1286. return 0;
  1287. }
  1288. static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1289. {
  1290. AVStream *st;
  1291. MOVStreamContext *sc;
  1292. unsigned int i, entries;
  1293. if (c->fc->nb_streams < 1)
  1294. return 0;
  1295. st = c->fc->streams[c->fc->nb_streams-1];
  1296. sc = st->priv_data;
  1297. avio_r8(pb); /* version */
  1298. avio_rb24(pb); /* flags */
  1299. entries = avio_rb32(pb);
  1300. av_dlog(c->fc, "keyframe_count = %d\n", entries);
  1301. if (!entries)
  1302. {
  1303. sc->keyframe_absent = 1;
  1304. return 0;
  1305. }
  1306. if (entries >= UINT_MAX / sizeof(int))
  1307. return AVERROR_INVALIDDATA;
  1308. sc->keyframes = av_malloc(entries * sizeof(int));
  1309. if (!sc->keyframes)
  1310. return AVERROR(ENOMEM);
  1311. sc->keyframe_count = entries;
  1312. for (i=0; i<entries; i++) {
  1313. sc->keyframes[i] = avio_rb32(pb);
  1314. //av_dlog(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
  1315. }
  1316. return 0;
  1317. }
  1318. static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1319. {
  1320. AVStream *st;
  1321. MOVStreamContext *sc;
  1322. unsigned int i, entries, sample_size, field_size, num_bytes;
  1323. GetBitContext gb;
  1324. unsigned char* buf;
  1325. if (c->fc->nb_streams < 1)
  1326. return 0;
  1327. st = c->fc->streams[c->fc->nb_streams-1];
  1328. sc = st->priv_data;
  1329. avio_r8(pb); /* version */
  1330. avio_rb24(pb); /* flags */
  1331. if (atom.type == MKTAG('s','t','s','z')) {
  1332. sample_size = avio_rb32(pb);
  1333. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  1334. sc->sample_size = sample_size;
  1335. field_size = 32;
  1336. } else {
  1337. sample_size = 0;
  1338. avio_rb24(pb); /* reserved */
  1339. field_size = avio_r8(pb);
  1340. }
  1341. entries = avio_rb32(pb);
  1342. av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
  1343. sc->sample_count = entries;
  1344. if (sample_size)
  1345. return 0;
  1346. if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
  1347. av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
  1348. return AVERROR_INVALIDDATA;
  1349. }
  1350. if (!entries)
  1351. return 0;
  1352. if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
  1353. return AVERROR_INVALIDDATA;
  1354. sc->sample_sizes = av_malloc(entries * sizeof(int));
  1355. if (!sc->sample_sizes)
  1356. return AVERROR(ENOMEM);
  1357. num_bytes = (entries*field_size+4)>>3;
  1358. buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
  1359. if (!buf) {
  1360. av_freep(&sc->sample_sizes);
  1361. return AVERROR(ENOMEM);
  1362. }
  1363. if (avio_read(pb, buf, num_bytes) < num_bytes) {
  1364. av_freep(&sc->sample_sizes);
  1365. av_free(buf);
  1366. return AVERROR_INVALIDDATA;
  1367. }
  1368. init_get_bits(&gb, buf, 8*num_bytes);
  1369. for (i = 0; i < entries; i++) {
  1370. sc->sample_sizes[i] = get_bits_long(&gb, field_size);
  1371. sc->data_size += sc->sample_sizes[i];
  1372. }
  1373. av_free(buf);
  1374. return 0;
  1375. }
  1376. static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1377. {
  1378. AVStream *st;
  1379. MOVStreamContext *sc;
  1380. unsigned int i, entries;
  1381. int64_t duration=0;
  1382. int64_t total_sample_count=0;
  1383. if (c->fc->nb_streams < 1)
  1384. return 0;
  1385. st = c->fc->streams[c->fc->nb_streams-1];
  1386. sc = st->priv_data;
  1387. avio_r8(pb); /* version */
  1388. avio_rb24(pb); /* flags */
  1389. entries = avio_rb32(pb);
  1390. av_dlog(c->fc, "track[%i].stts.entries = %i\n",
  1391. c->fc->nb_streams-1, entries);
  1392. if (!entries)
  1393. return 0;
  1394. if (entries >= UINT_MAX / sizeof(*sc->stts_data))
  1395. return AVERROR(EINVAL);
  1396. sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
  1397. if (!sc->stts_data)
  1398. return AVERROR(ENOMEM);
  1399. sc->stts_count = entries;
  1400. for (i=0; i<entries; i++) {
  1401. int sample_duration;
  1402. int sample_count;
  1403. sample_count=avio_rb32(pb);
  1404. sample_duration = avio_rb32(pb);
  1405. sc->stts_data[i].count= sample_count;
  1406. sc->stts_data[i].duration= sample_duration;
  1407. av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n",
  1408. sample_count, sample_duration);
  1409. duration+=(int64_t)sample_duration*sample_count;
  1410. total_sample_count+=sample_count;
  1411. }
  1412. st->nb_frames= total_sample_count;
  1413. if (duration)
  1414. st->duration= duration;
  1415. sc->track_end = duration;
  1416. return 0;
  1417. }
  1418. static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1419. {
  1420. AVStream *st;
  1421. MOVStreamContext *sc;
  1422. unsigned int i, entries;
  1423. if (c->fc->nb_streams < 1)
  1424. return 0;
  1425. st = c->fc->streams[c->fc->nb_streams-1];
  1426. sc = st->priv_data;
  1427. avio_r8(pb); /* version */
  1428. avio_rb24(pb); /* flags */
  1429. entries = avio_rb32(pb);
  1430. av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1431. if (!entries)
  1432. return 0;
  1433. if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
  1434. return AVERROR_INVALIDDATA;
  1435. sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
  1436. if (!sc->ctts_data)
  1437. return AVERROR(ENOMEM);
  1438. sc->ctts_count = entries;
  1439. for (i=0; i<entries; i++) {
  1440. int count =avio_rb32(pb);
  1441. int duration =avio_rb32(pb);
  1442. sc->ctts_data[i].count = count;
  1443. sc->ctts_data[i].duration= duration;
  1444. if (duration < 0)
  1445. sc->dts_shift = FFMAX(sc->dts_shift, -duration);
  1446. }
  1447. av_dlog(c->fc, "dts shift %d\n", sc->dts_shift);
  1448. return 0;
  1449. }
  1450. static void mov_build_index(MOVContext *mov, AVStream *st)
  1451. {
  1452. MOVStreamContext *sc = st->priv_data;
  1453. int64_t current_offset;
  1454. int64_t current_dts = 0;
  1455. unsigned int stts_index = 0;
  1456. unsigned int stsc_index = 0;
  1457. unsigned int stss_index = 0;
  1458. unsigned int stps_index = 0;
  1459. unsigned int i, j;
  1460. uint64_t stream_size = 0;
  1461. AVIndexEntry *mem;
  1462. /* adjust first dts according to edit list */
  1463. if (sc->time_offset && mov->time_scale > 0) {
  1464. if (sc->time_offset < 0)
  1465. sc->time_offset = av_rescale(sc->time_offset, sc->time_scale, mov->time_scale);
  1466. current_dts = -sc->time_offset;
  1467. if (sc->ctts_data && sc->stts_data && sc->stts_data[0].duration &&
  1468. sc->ctts_data[0].duration / sc->stts_data[0].duration > 16) {
  1469. /* more than 16 frames delay, dts are likely wrong
  1470. this happens with files created by iMovie */
  1471. sc->wrong_dts = 1;
  1472. st->codec->has_b_frames = 1;
  1473. }
  1474. }
  1475. /* only use old uncompressed audio chunk demuxing when stts specifies it */
  1476. if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  1477. sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
  1478. unsigned int current_sample = 0;
  1479. unsigned int stts_sample = 0;
  1480. unsigned int sample_size;
  1481. unsigned int distance = 0;
  1482. int key_off = (sc->keyframes && sc->keyframes[0] > 0) || (sc->stps_data && sc->stps_data[0] > 0);
  1483. current_dts -= sc->dts_shift;
  1484. if (!sc->sample_count)
  1485. return;
  1486. if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
  1487. return;
  1488. mem = av_realloc(st->index_entries, (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries));
  1489. if (!mem)
  1490. return;
  1491. st->index_entries = mem;
  1492. st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries);
  1493. for (i = 0; i < sc->chunk_count; i++) {
  1494. current_offset = sc->chunk_offsets[i];
  1495. while (stsc_index + 1 < sc->stsc_count &&
  1496. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1497. stsc_index++;
  1498. for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
  1499. int keyframe = 0;
  1500. if (current_sample >= sc->sample_count) {
  1501. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  1502. return;
  1503. }
  1504. if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
  1505. keyframe = 1;
  1506. if (stss_index + 1 < sc->keyframe_count)
  1507. stss_index++;
  1508. } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
  1509. keyframe = 1;
  1510. if (stps_index + 1 < sc->stps_count)
  1511. stps_index++;
  1512. }
  1513. if (keyframe)
  1514. distance = 0;
  1515. sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
  1516. if (sc->pseudo_stream_id == -1 ||
  1517. sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
  1518. AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
  1519. e->pos = current_offset;
  1520. e->timestamp = current_dts;
  1521. e->size = sample_size;
  1522. e->min_distance = distance;
  1523. e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
  1524. av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1525. "size %d, distance %d, keyframe %d\n", st->index, current_sample,
  1526. current_offset, current_dts, sample_size, distance, keyframe);
  1527. }
  1528. current_offset += sample_size;
  1529. stream_size += sample_size;
  1530. current_dts += sc->stts_data[stts_index].duration;
  1531. distance++;
  1532. stts_sample++;
  1533. current_sample++;
  1534. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  1535. stts_sample = 0;
  1536. stts_index++;
  1537. }
  1538. }
  1539. }
  1540. if (st->duration > 0)
  1541. st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
  1542. } else {
  1543. unsigned chunk_samples, total = 0;
  1544. // compute total chunk count
  1545. for (i = 0; i < sc->stsc_count; i++) {
  1546. unsigned count, chunk_count;
  1547. chunk_samples = sc->stsc_data[i].count;
  1548. if (i != sc->stsc_count - 1 &&
  1549. sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
  1550. av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
  1551. return;
  1552. }
  1553. if (sc->samples_per_frame >= 160) { // gsm
  1554. count = chunk_samples / sc->samples_per_frame;
  1555. } else if (sc->samples_per_frame > 1) {
  1556. unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
  1557. count = (chunk_samples+samples-1) / samples;
  1558. } else {
  1559. count = (chunk_samples+1023) / 1024;
  1560. }
  1561. if (i < sc->stsc_count - 1)
  1562. chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
  1563. else
  1564. chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
  1565. total += chunk_count * count;
  1566. }
  1567. av_dlog(mov->fc, "chunk count %d\n", total);
  1568. if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
  1569. return;
  1570. mem = av_realloc(st->index_entries, (st->nb_index_entries + total) * sizeof(*st->index_entries));
  1571. if (!mem)
  1572. return;
  1573. st->index_entries = mem;
  1574. st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
  1575. // populate index
  1576. for (i = 0; i < sc->chunk_count; i++) {
  1577. current_offset = sc->chunk_offsets[i];
  1578. if (stsc_index + 1 < sc->stsc_count &&
  1579. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1580. stsc_index++;
  1581. chunk_samples = sc->stsc_data[stsc_index].count;
  1582. while (chunk_samples > 0) {
  1583. AVIndexEntry *e;
  1584. unsigned size, samples;
  1585. if (sc->samples_per_frame >= 160) { // gsm
  1586. samples = sc->samples_per_frame;
  1587. size = sc->bytes_per_frame;
  1588. } else {
  1589. if (sc->samples_per_frame > 1) {
  1590. samples = FFMIN((1024 / sc->samples_per_frame)*
  1591. sc->samples_per_frame, chunk_samples);
  1592. size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
  1593. } else {
  1594. samples = FFMIN(1024, chunk_samples);
  1595. size = samples * sc->sample_size;
  1596. }
  1597. }
  1598. if (st->nb_index_entries >= total) {
  1599. av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
  1600. return;
  1601. }
  1602. e = &st->index_entries[st->nb_index_entries++];
  1603. e->pos = current_offset;
  1604. e->timestamp = current_dts;
  1605. e->size = size;
  1606. e->min_distance = 0;
  1607. e->flags = AVINDEX_KEYFRAME;
  1608. av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
  1609. "size %d, duration %d\n", st->index, i, current_offset, current_dts,
  1610. size, samples);
  1611. current_offset += size;
  1612. current_dts += samples;
  1613. chunk_samples -= samples;
  1614. }
  1615. }
  1616. }
  1617. }
  1618. static int mov_open_dref(AVIOContext **pb, char *src, MOVDref *ref,
  1619. AVIOInterruptCB *int_cb)
  1620. {
  1621. /* try relative path, we do not try the absolute because it can leak information about our
  1622. system to an attacker */
  1623. if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
  1624. char filename[1024];
  1625. char *src_path;
  1626. int i, l;
  1627. /* find a source dir */
  1628. src_path = strrchr(src, '/');
  1629. if (src_path)
  1630. src_path++;
  1631. else
  1632. src_path = src;
  1633. /* find a next level down to target */
  1634. for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
  1635. if (ref->path[l] == '/') {
  1636. if (i == ref->nlvl_to - 1)
  1637. break;
  1638. else
  1639. i++;
  1640. }
  1641. /* compose filename if next level down to target was found */
  1642. if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
  1643. memcpy(filename, src, src_path - src);
  1644. filename[src_path - src] = 0;
  1645. for (i = 1; i < ref->nlvl_from; i++)
  1646. av_strlcat(filename, "../", 1024);
  1647. av_strlcat(filename, ref->path + l + 1, 1024);
  1648. if (!avio_open2(pb, filename, AVIO_FLAG_READ, int_cb, NULL))
  1649. return 0;
  1650. }
  1651. }
  1652. return AVERROR(ENOENT);
  1653. }
  1654. static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1655. {
  1656. AVStream *st;
  1657. MOVStreamContext *sc;
  1658. int ret;
  1659. st = avformat_new_stream(c->fc, NULL);
  1660. if (!st) return AVERROR(ENOMEM);
  1661. st->id = c->fc->nb_streams;
  1662. sc = av_mallocz(sizeof(MOVStreamContext));
  1663. if (!sc) return AVERROR(ENOMEM);
  1664. st->priv_data = sc;
  1665. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1666. sc->ffindex = st->index;
  1667. if ((ret = mov_read_default(c, pb, atom)) < 0)
  1668. return ret;
  1669. /* sanity checks */
  1670. if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
  1671. (!sc->sample_size && !sc->sample_count))) {
  1672. av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
  1673. st->index);
  1674. return 0;
  1675. }
  1676. if (sc->time_scale <= 0) {
  1677. av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index);
  1678. sc->time_scale = c->time_scale;
  1679. if (sc->time_scale <= 0)
  1680. sc->time_scale = 1;
  1681. }
  1682. avpriv_set_pts_info(st, 64, 1, sc->time_scale);
  1683. mov_build_index(c, st);
  1684. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  1685. MOVDref *dref = &sc->drefs[sc->dref_id - 1];
  1686. if (mov_open_dref(&sc->pb, c->fc->filename, dref, &c->fc->interrupt_callback) < 0)
  1687. av_log(c->fc, AV_LOG_ERROR,
  1688. "stream %d, error opening alias: path='%s', dir='%s', "
  1689. "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
  1690. st->index, dref->path, dref->dir, dref->filename,
  1691. dref->volume, dref->nlvl_from, dref->nlvl_to);
  1692. } else
  1693. sc->pb = c->fc->pb;
  1694. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  1695. if (!st->sample_aspect_ratio.num &&
  1696. (st->codec->width != sc->width || st->codec->height != sc->height)) {
  1697. st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
  1698. ((double)st->codec->width * sc->height), INT_MAX);
  1699. }
  1700. av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
  1701. sc->time_scale*st->nb_frames, st->duration, INT_MAX);
  1702. if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
  1703. av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
  1704. sc->time_scale, sc->stts_data[0].duration, INT_MAX);
  1705. }
  1706. switch (st->codec->codec_id) {
  1707. #if CONFIG_H261_DECODER
  1708. case CODEC_ID_H261:
  1709. #endif
  1710. #if CONFIG_H263_DECODER
  1711. case CODEC_ID_H263:
  1712. #endif
  1713. #if CONFIG_MPEG4_DECODER
  1714. case CODEC_ID_MPEG4:
  1715. #endif
  1716. st->codec->width = 0; /* let decoder init width/height */
  1717. st->codec->height= 0;
  1718. break;
  1719. }
  1720. /* Do not need those anymore. */
  1721. av_freep(&sc->chunk_offsets);
  1722. av_freep(&sc->stsc_data);
  1723. av_freep(&sc->sample_sizes);
  1724. av_freep(&sc->keyframes);
  1725. av_freep(&sc->stts_data);
  1726. av_freep(&sc->stps_data);
  1727. return 0;
  1728. }
  1729. static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1730. {
  1731. int ret;
  1732. c->itunes_metadata = 1;
  1733. ret = mov_read_default(c, pb, atom);
  1734. c->itunes_metadata = 0;
  1735. return ret;
  1736. }
  1737. static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1738. {
  1739. while (atom.size > 8) {
  1740. uint32_t tag = avio_rl32(pb);
  1741. atom.size -= 4;
  1742. if (tag == MKTAG('h','d','l','r')) {
  1743. avio_seek(pb, -8, SEEK_CUR);
  1744. atom.size += 8;
  1745. return mov_read_default(c, pb, atom);
  1746. }
  1747. }
  1748. return 0;
  1749. }
  1750. static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1751. {
  1752. int i;
  1753. int width;
  1754. int height;
  1755. int64_t disp_transform[2];
  1756. int display_matrix[3][2];
  1757. AVStream *st;
  1758. MOVStreamContext *sc;
  1759. int version;
  1760. if (c->fc->nb_streams < 1)
  1761. return 0;
  1762. st = c->fc->streams[c->fc->nb_streams-1];
  1763. sc = st->priv_data;
  1764. version = avio_r8(pb);
  1765. avio_rb24(pb); /* flags */
  1766. /*
  1767. MOV_TRACK_ENABLED 0x0001
  1768. MOV_TRACK_IN_MOVIE 0x0002
  1769. MOV_TRACK_IN_PREVIEW 0x0004
  1770. MOV_TRACK_IN_POSTER 0x0008
  1771. */
  1772. if (version == 1) {
  1773. avio_rb64(pb);
  1774. avio_rb64(pb);
  1775. } else {
  1776. avio_rb32(pb); /* creation time */
  1777. avio_rb32(pb); /* modification time */
  1778. }
  1779. st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
  1780. avio_rb32(pb); /* reserved */
  1781. /* highlevel (considering edits) duration in movie timebase */
  1782. (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
  1783. avio_rb32(pb); /* reserved */
  1784. avio_rb32(pb); /* reserved */
  1785. avio_rb16(pb); /* layer */
  1786. avio_rb16(pb); /* alternate group */
  1787. avio_rb16(pb); /* volume */
  1788. avio_rb16(pb); /* reserved */
  1789. //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
  1790. // they're kept in fixed point format through all calculations
  1791. // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
  1792. for (i = 0; i < 3; i++) {
  1793. display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
  1794. display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
  1795. avio_rb32(pb); // 2.30 fixed point (not used)
  1796. }
  1797. width = avio_rb32(pb); // 16.16 fixed point track width
  1798. height = avio_rb32(pb); // 16.16 fixed point track height
  1799. sc->width = width >> 16;
  1800. sc->height = height >> 16;
  1801. // transform the display width/height according to the matrix
  1802. // skip this if the display matrix is the default identity matrix
  1803. // or if it is rotating the picture, ex iPhone 3GS
  1804. // to keep the same scale, use [width height 1<<16]
  1805. if (width && height &&
  1806. ((display_matrix[0][0] != 65536 ||
  1807. display_matrix[1][1] != 65536) &&
  1808. !display_matrix[0][1] &&
  1809. !display_matrix[1][0] &&
  1810. !display_matrix[2][0] && !display_matrix[2][1])) {
  1811. for (i = 0; i < 2; i++)
  1812. disp_transform[i] =
  1813. (int64_t) width * display_matrix[0][i] +
  1814. (int64_t) height * display_matrix[1][i] +
  1815. ((int64_t) display_matrix[2][i] << 16);
  1816. //sample aspect ratio is new width/height divided by old width/height
  1817. st->sample_aspect_ratio = av_d2q(
  1818. ((double) disp_transform[0] * height) /
  1819. ((double) disp_transform[1] * width), INT_MAX);
  1820. }
  1821. return 0;
  1822. }
  1823. static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1824. {
  1825. MOVFragment *frag = &c->fragment;
  1826. MOVTrackExt *trex = NULL;
  1827. int flags, track_id, i;
  1828. avio_r8(pb); /* version */
  1829. flags = avio_rb24(pb);
  1830. track_id = avio_rb32(pb);
  1831. if (!track_id)
  1832. return AVERROR_INVALIDDATA;
  1833. frag->track_id = track_id;
  1834. for (i = 0; i < c->trex_count; i++)
  1835. if (c->trex_data[i].track_id == frag->track_id) {
  1836. trex = &c->trex_data[i];
  1837. break;
  1838. }
  1839. if (!trex) {
  1840. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
  1841. return AVERROR_INVALIDDATA;
  1842. }
  1843. frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ?
  1844. avio_rb64(pb) : frag->moof_offset;
  1845. frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
  1846. frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
  1847. avio_rb32(pb) : trex->duration;
  1848. frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
  1849. avio_rb32(pb) : trex->size;
  1850. frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
  1851. avio_rb32(pb) : trex->flags;
  1852. av_dlog(c->fc, "frag flags 0x%x\n", frag->flags);
  1853. return 0;
  1854. }
  1855. static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1856. {
  1857. c->chapter_track = avio_rb32(pb);
  1858. return 0;
  1859. }
  1860. static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1861. {
  1862. MOVTrackExt *trex;
  1863. if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
  1864. return AVERROR_INVALIDDATA;
  1865. trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
  1866. if (!trex)
  1867. return AVERROR(ENOMEM);
  1868. c->trex_data = trex;
  1869. trex = &c->trex_data[c->trex_count++];
  1870. avio_r8(pb); /* version */
  1871. avio_rb24(pb); /* flags */
  1872. trex->track_id = avio_rb32(pb);
  1873. trex->stsd_id = avio_rb32(pb);
  1874. trex->duration = avio_rb32(pb);
  1875. trex->size = avio_rb32(pb);
  1876. trex->flags = avio_rb32(pb);
  1877. return 0;
  1878. }
  1879. static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1880. {
  1881. MOVFragment *frag = &c->fragment;
  1882. AVStream *st = NULL;
  1883. MOVStreamContext *sc;
  1884. MOVStts *ctts_data;
  1885. uint64_t offset;
  1886. int64_t dts;
  1887. int data_offset = 0;
  1888. unsigned entries, first_sample_flags = frag->flags;
  1889. int flags, distance, i, found_keyframe = 0;
  1890. for (i = 0; i < c->fc->nb_streams; i++) {
  1891. if (c->fc->streams[i]->id == frag->track_id) {
  1892. st = c->fc->streams[i];
  1893. break;
  1894. }
  1895. }
  1896. if (!st) {
  1897. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
  1898. return AVERROR_INVALIDDATA;
  1899. }
  1900. sc = st->priv_data;
  1901. if (sc->pseudo_stream_id+1 != frag->stsd_id)
  1902. return 0;
  1903. avio_r8(pb); /* version */
  1904. flags = avio_rb24(pb);
  1905. entries = avio_rb32(pb);
  1906. av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries);
  1907. /* Always assume the presence of composition time offsets.
  1908. * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
  1909. * 1) in the initial movie, there are no samples.
  1910. * 2) in the first movie fragment, there is only one sample without composition time offset.
  1911. * 3) in the subsequent movie fragments, there are samples with composition time offset. */
  1912. if (!sc->ctts_count && sc->sample_count)
  1913. {
  1914. /* Complement ctts table if moov atom doesn't have ctts atom. */
  1915. ctts_data = av_malloc(sizeof(*sc->ctts_data));
  1916. if (!ctts_data)
  1917. return AVERROR(ENOMEM);
  1918. sc->ctts_data = ctts_data;
  1919. sc->ctts_data[sc->ctts_count].count = sc->sample_count;
  1920. sc->ctts_data[sc->ctts_count].duration = 0;
  1921. sc->ctts_count++;
  1922. }
  1923. if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
  1924. return AVERROR_INVALIDDATA;
  1925. ctts_data = av_realloc(sc->ctts_data,
  1926. (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
  1927. if (!ctts_data)
  1928. return AVERROR(ENOMEM);
  1929. sc->ctts_data = ctts_data;
  1930. if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
  1931. if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
  1932. dts = sc->track_end - sc->time_offset;
  1933. offset = frag->base_data_offset + data_offset;
  1934. distance = 0;
  1935. av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags);
  1936. for (i = 0; i < entries; i++) {
  1937. unsigned sample_size = frag->size;
  1938. int sample_flags = i ? frag->flags : first_sample_flags;
  1939. unsigned sample_duration = frag->duration;
  1940. int keyframe = 0;
  1941. if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
  1942. if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
  1943. if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
  1944. sc->ctts_data[sc->ctts_count].count = 1;
  1945. sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
  1946. avio_rb32(pb) : 0;
  1947. sc->ctts_count++;
  1948. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  1949. keyframe = 1;
  1950. else if (!found_keyframe)
  1951. keyframe = found_keyframe =
  1952. !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
  1953. MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES));
  1954. if (keyframe)
  1955. distance = 0;
  1956. av_add_index_entry(st, offset, dts, sample_size, distance,
  1957. keyframe ? AVINDEX_KEYFRAME : 0);
  1958. av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1959. "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
  1960. offset, dts, sample_size, distance, keyframe);
  1961. distance++;
  1962. dts += sample_duration;
  1963. offset += sample_size;
  1964. sc->data_size += sample_size;
  1965. }
  1966. frag->moof_offset = offset;
  1967. st->duration = sc->track_end = dts + sc->time_offset;
  1968. return 0;
  1969. }
  1970. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1971. /* like the files created with Adobe Premiere 5.0, for samples see */
  1972. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1973. static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1974. {
  1975. int err;
  1976. if (atom.size < 8)
  1977. return 0; /* continue */
  1978. if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1979. avio_skip(pb, atom.size - 4);
  1980. return 0;
  1981. }
  1982. atom.type = avio_rl32(pb);
  1983. atom.size -= 8;
  1984. if (atom.type != MKTAG('m','d','a','t')) {
  1985. avio_skip(pb, atom.size);
  1986. return 0;
  1987. }
  1988. err = mov_read_mdat(c, pb, atom);
  1989. return err;
  1990. }
  1991. static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1992. {
  1993. #if CONFIG_ZLIB
  1994. AVIOContext ctx;
  1995. uint8_t *cmov_data;
  1996. uint8_t *moov_data; /* uncompressed data */
  1997. long cmov_len, moov_len;
  1998. int ret = -1;
  1999. avio_rb32(pb); /* dcom atom */
  2000. if (avio_rl32(pb) != MKTAG('d','c','o','m'))
  2001. return AVERROR_INVALIDDATA;
  2002. if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
  2003. av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !");
  2004. return AVERROR_INVALIDDATA;
  2005. }
  2006. avio_rb32(pb); /* cmvd atom */
  2007. if (avio_rl32(pb) != MKTAG('c','m','v','d'))
  2008. return AVERROR_INVALIDDATA;
  2009. moov_len = avio_rb32(pb); /* uncompressed size */
  2010. cmov_len = atom.size - 6 * 4;
  2011. cmov_data = av_malloc(cmov_len);
  2012. if (!cmov_data)
  2013. return AVERROR(ENOMEM);
  2014. moov_data = av_malloc(moov_len);
  2015. if (!moov_data) {
  2016. av_free(cmov_data);
  2017. return AVERROR(ENOMEM);
  2018. }
  2019. avio_read(pb, cmov_data, cmov_len);
  2020. if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  2021. goto free_and_return;
  2022. if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  2023. goto free_and_return;
  2024. atom.type = MKTAG('m','o','o','v');
  2025. atom.size = moov_len;
  2026. ret = mov_read_default(c, &ctx, atom);
  2027. free_and_return:
  2028. av_free(moov_data);
  2029. av_free(cmov_data);
  2030. return ret;
  2031. #else
  2032. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  2033. return AVERROR(ENOSYS);
  2034. #endif
  2035. }
  2036. /* edit list atom */
  2037. static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2038. {
  2039. MOVStreamContext *sc;
  2040. int i, edit_count, version;
  2041. if (c->fc->nb_streams < 1)
  2042. return 0;
  2043. sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  2044. version = avio_r8(pb); /* version */
  2045. avio_rb24(pb); /* flags */
  2046. edit_count = avio_rb32(pb); /* entries */
  2047. if ((uint64_t)edit_count*12+8 > atom.size)
  2048. return AVERROR_INVALIDDATA;
  2049. for (i=0; i<edit_count; i++){
  2050. int64_t time;
  2051. int64_t duration;
  2052. if (version == 1) {
  2053. duration = avio_rb64(pb);
  2054. time = avio_rb64(pb);
  2055. } else {
  2056. duration = avio_rb32(pb); /* segment duration */
  2057. time = (int32_t)avio_rb32(pb); /* media time */
  2058. }
  2059. avio_rb32(pb); /* Media rate */
  2060. if (i == 0 && time >= -1) {
  2061. sc->time_offset = time != -1 ? time : -duration;
  2062. }
  2063. }
  2064. if (edit_count > 1)
  2065. av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
  2066. "a/v desync might occur, patch welcome\n");
  2067. av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
  2068. return 0;
  2069. }
  2070. static const MOVParseTableEntry mov_default_parse_table[] = {
  2071. { MKTAG('a','v','s','s'), mov_read_extradata },
  2072. { MKTAG('c','h','p','l'), mov_read_chpl },
  2073. { MKTAG('c','o','6','4'), mov_read_stco },
  2074. { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
  2075. { MKTAG('d','i','n','f'), mov_read_default },
  2076. { MKTAG('d','r','e','f'), mov_read_dref },
  2077. { MKTAG('e','d','t','s'), mov_read_default },
  2078. { MKTAG('e','l','s','t'), mov_read_elst },
  2079. { MKTAG('e','n','d','a'), mov_read_enda },
  2080. { MKTAG('f','i','e','l'), mov_read_fiel },
  2081. { MKTAG('f','t','y','p'), mov_read_ftyp },
  2082. { MKTAG('g','l','b','l'), mov_read_glbl },
  2083. { MKTAG('h','d','l','r'), mov_read_hdlr },
  2084. { MKTAG('i','l','s','t'), mov_read_ilst },
  2085. { MKTAG('j','p','2','h'), mov_read_extradata },
  2086. { MKTAG('m','d','a','t'), mov_read_mdat },
  2087. { MKTAG('m','d','h','d'), mov_read_mdhd },
  2088. { MKTAG('m','d','i','a'), mov_read_default },
  2089. { MKTAG('m','e','t','a'), mov_read_meta },
  2090. { MKTAG('m','i','n','f'), mov_read_default },
  2091. { MKTAG('m','o','o','f'), mov_read_moof },
  2092. { MKTAG('m','o','o','v'), mov_read_moov },
  2093. { MKTAG('m','v','e','x'), mov_read_default },
  2094. { MKTAG('m','v','h','d'), mov_read_mvhd },
  2095. { MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
  2096. { MKTAG('a','l','a','c'), mov_read_extradata }, /* alac specific atom */
  2097. { MKTAG('a','v','c','C'), mov_read_glbl },
  2098. { MKTAG('p','a','s','p'), mov_read_pasp },
  2099. { MKTAG('s','t','b','l'), mov_read_default },
  2100. { MKTAG('s','t','c','o'), mov_read_stco },
  2101. { MKTAG('s','t','p','s'), mov_read_stps },
  2102. { MKTAG('s','t','r','f'), mov_read_strf },
  2103. { MKTAG('s','t','s','c'), mov_read_stsc },
  2104. { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
  2105. { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
  2106. { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
  2107. { MKTAG('s','t','t','s'), mov_read_stts },
  2108. { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
  2109. { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
  2110. { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
  2111. { MKTAG('t','r','a','k'), mov_read_trak },
  2112. { MKTAG('t','r','a','f'), mov_read_default },
  2113. { MKTAG('t','r','e','f'), mov_read_default },
  2114. { MKTAG('c','h','a','p'), mov_read_chap },
  2115. { MKTAG('t','r','e','x'), mov_read_trex },
  2116. { MKTAG('t','r','u','n'), mov_read_trun },
  2117. { MKTAG('u','d','t','a'), mov_read_default },
  2118. { MKTAG('w','a','v','e'), mov_read_wave },
  2119. { MKTAG('e','s','d','s'), mov_read_esds },
  2120. { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
  2121. { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
  2122. { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
  2123. { MKTAG('w','f','e','x'), mov_read_wfex },
  2124. { MKTAG('c','m','o','v'), mov_read_cmov },
  2125. { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
  2126. { MKTAG('d','v','c','1'), mov_read_dvc1 },
  2127. { 0, NULL }
  2128. };
  2129. static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2130. {
  2131. int64_t total_size = 0;
  2132. MOVAtom a;
  2133. int i;
  2134. if (atom.size < 0)
  2135. atom.size = INT64_MAX;
  2136. while (total_size + 8 < atom.size && !pb->eof_reached) {
  2137. int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
  2138. a.size = atom.size;
  2139. a.type=0;
  2140. if (atom.size >= 8) {
  2141. a.size = avio_rb32(pb);
  2142. a.type = avio_rl32(pb);
  2143. }
  2144. av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
  2145. a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
  2146. total_size += 8;
  2147. if (a.size == 1) { /* 64 bit extended size */
  2148. a.size = avio_rb64(pb) - 8;
  2149. total_size += 8;
  2150. }
  2151. if (a.size == 0) {
  2152. a.size = atom.size - total_size;
  2153. if (a.size <= 8)
  2154. break;
  2155. }
  2156. a.size -= 8;
  2157. if (a.size < 0)
  2158. break;
  2159. a.size = FFMIN(a.size, atom.size - total_size);
  2160. for (i = 0; mov_default_parse_table[i].type; i++)
  2161. if (mov_default_parse_table[i].type == a.type) {
  2162. parse = mov_default_parse_table[i].parse;
  2163. break;
  2164. }
  2165. // container is user data
  2166. if (!parse && (atom.type == MKTAG('u','d','t','a') ||
  2167. atom.type == MKTAG('i','l','s','t')))
  2168. parse = mov_read_udta_string;
  2169. if (!parse) { /* skip leaf atoms data */
  2170. avio_skip(pb, a.size);
  2171. } else {
  2172. int64_t start_pos = avio_tell(pb);
  2173. int64_t left;
  2174. int err = parse(c, pb, a);
  2175. if (err < 0)
  2176. return err;
  2177. if (c->found_moov && c->found_mdat &&
  2178. ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) ||
  2179. start_pos + a.size == avio_size(pb))) {
  2180. if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX)
  2181. c->next_root_atom = start_pos + a.size;
  2182. return 0;
  2183. }
  2184. left = a.size - avio_tell(pb) + start_pos;
  2185. if (left > 0) /* skip garbage at atom end */
  2186. avio_skip(pb, left);
  2187. }
  2188. total_size += a.size;
  2189. }
  2190. if (total_size < atom.size && atom.size < 0x7ffff)
  2191. avio_skip(pb, atom.size - total_size);
  2192. return 0;
  2193. }
  2194. static int mov_probe(AVProbeData *p)
  2195. {
  2196. unsigned int offset;
  2197. uint32_t tag;
  2198. int score = 0;
  2199. /* check file header */
  2200. offset = 0;
  2201. for (;;) {
  2202. /* ignore invalid offset */
  2203. if ((offset + 8) > (unsigned int)p->buf_size)
  2204. return score;
  2205. tag = AV_RL32(p->buf + offset + 4);
  2206. switch(tag) {
  2207. /* check for obvious tags */
  2208. case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
  2209. case MKTAG('m','o','o','v'):
  2210. case MKTAG('m','d','a','t'):
  2211. case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
  2212. case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
  2213. case MKTAG('f','t','y','p'):
  2214. return AVPROBE_SCORE_MAX;
  2215. /* those are more common words, so rate then a bit less */
  2216. case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
  2217. case MKTAG('w','i','d','e'):
  2218. case MKTAG('f','r','e','e'):
  2219. case MKTAG('j','u','n','k'):
  2220. case MKTAG('p','i','c','t'):
  2221. return AVPROBE_SCORE_MAX - 5;
  2222. case MKTAG(0x82,0x82,0x7f,0x7d):
  2223. case MKTAG('s','k','i','p'):
  2224. case MKTAG('u','u','i','d'):
  2225. case MKTAG('p','r','f','l'):
  2226. offset = AV_RB32(p->buf+offset) + offset;
  2227. /* if we only find those cause probedata is too small at least rate them */
  2228. score = AVPROBE_SCORE_MAX - 50;
  2229. break;
  2230. default:
  2231. /* unrecognized tag */
  2232. return score;
  2233. }
  2234. }
  2235. }
  2236. // must be done after parsing all trak because there's no order requirement
  2237. static void mov_read_chapters(AVFormatContext *s)
  2238. {
  2239. MOVContext *mov = s->priv_data;
  2240. AVStream *st = NULL;
  2241. MOVStreamContext *sc;
  2242. int64_t cur_pos;
  2243. int i;
  2244. for (i = 0; i < s->nb_streams; i++)
  2245. if (s->streams[i]->id == mov->chapter_track) {
  2246. st = s->streams[i];
  2247. break;
  2248. }
  2249. if (!st) {
  2250. av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
  2251. return;
  2252. }
  2253. st->discard = AVDISCARD_ALL;
  2254. sc = st->priv_data;
  2255. cur_pos = avio_tell(sc->pb);
  2256. for (i = 0; i < st->nb_index_entries; i++) {
  2257. AVIndexEntry *sample = &st->index_entries[i];
  2258. int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
  2259. uint8_t *title;
  2260. uint16_t ch;
  2261. int len, title_len;
  2262. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  2263. av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
  2264. goto finish;
  2265. }
  2266. // the first two bytes are the length of the title
  2267. len = avio_rb16(sc->pb);
  2268. if (len > sample->size-2)
  2269. continue;
  2270. title_len = 2*len + 1;
  2271. if (!(title = av_mallocz(title_len)))
  2272. goto finish;
  2273. // The samples could theoretically be in any encoding if there's an encd
  2274. // atom following, but in practice are only utf-8 or utf-16, distinguished
  2275. // instead by the presence of a BOM
  2276. if (!len) {
  2277. title[0] = 0;
  2278. } else {
  2279. ch = avio_rb16(sc->pb);
  2280. if (ch == 0xfeff)
  2281. avio_get_str16be(sc->pb, len, title, title_len);
  2282. else if (ch == 0xfffe)
  2283. avio_get_str16le(sc->pb, len, title, title_len);
  2284. else {
  2285. AV_WB16(title, ch);
  2286. if (len == 1 || len == 2)
  2287. title[len] = 0;
  2288. else
  2289. avio_get_str(sc->pb, len - 2, title + 2, title_len - 2);
  2290. }
  2291. }
  2292. avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
  2293. av_freep(&title);
  2294. }
  2295. finish:
  2296. avio_seek(sc->pb, cur_pos, SEEK_SET);
  2297. }
  2298. static int mov_read_close(AVFormatContext *s)
  2299. {
  2300. MOVContext *mov = s->priv_data;
  2301. int i, j;
  2302. for (i = 0; i < s->nb_streams; i++) {
  2303. AVStream *st = s->streams[i];
  2304. MOVStreamContext *sc = st->priv_data;
  2305. av_freep(&sc->ctts_data);
  2306. for (j = 0; j < sc->drefs_count; j++) {
  2307. av_freep(&sc->drefs[j].path);
  2308. av_freep(&sc->drefs[j].dir);
  2309. }
  2310. av_freep(&sc->drefs);
  2311. if (sc->pb && sc->pb != s->pb)
  2312. avio_close(sc->pb);
  2313. }
  2314. if (mov->dv_demux) {
  2315. for (i = 0; i < mov->dv_fctx->nb_streams; i++) {
  2316. av_freep(&mov->dv_fctx->streams[i]->codec);
  2317. av_freep(&mov->dv_fctx->streams[i]);
  2318. }
  2319. av_freep(&mov->dv_fctx);
  2320. av_freep(&mov->dv_demux);
  2321. }
  2322. av_freep(&mov->trex_data);
  2323. return 0;
  2324. }
  2325. static int mov_read_header(AVFormatContext *s)
  2326. {
  2327. MOVContext *mov = s->priv_data;
  2328. AVIOContext *pb = s->pb;
  2329. int err;
  2330. MOVAtom atom = { AV_RL32("root") };
  2331. mov->fc = s;
  2332. /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  2333. if (pb->seekable)
  2334. atom.size = avio_size(pb);
  2335. else
  2336. atom.size = INT64_MAX;
  2337. /* check MOV header */
  2338. if ((err = mov_read_default(mov, pb, atom)) < 0) {
  2339. av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
  2340. mov_read_close(s);
  2341. return err;
  2342. }
  2343. if (!mov->found_moov) {
  2344. av_log(s, AV_LOG_ERROR, "moov atom not found\n");
  2345. mov_read_close(s);
  2346. return AVERROR_INVALIDDATA;
  2347. }
  2348. av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
  2349. if (pb->seekable && mov->chapter_track > 0)
  2350. mov_read_chapters(s);
  2351. if (mov->trex_data) {
  2352. int i;
  2353. for (i = 0; i < s->nb_streams; i++) {
  2354. AVStream *st = s->streams[i];
  2355. MOVStreamContext *sc = st->priv_data;
  2356. if (st->duration)
  2357. st->codec->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
  2358. }
  2359. }
  2360. return 0;
  2361. }
  2362. static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
  2363. {
  2364. AVIndexEntry *sample = NULL;
  2365. int64_t best_dts = INT64_MAX;
  2366. int i;
  2367. for (i = 0; i < s->nb_streams; i++) {
  2368. AVStream *avst = s->streams[i];
  2369. MOVStreamContext *msc = avst->priv_data;
  2370. if (msc->pb && msc->current_sample < avst->nb_index_entries) {
  2371. AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
  2372. int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
  2373. av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  2374. if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
  2375. (s->pb->seekable &&
  2376. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
  2377. ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  2378. (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
  2379. sample = current_sample;
  2380. best_dts = dts;
  2381. *st = avst;
  2382. }
  2383. }
  2384. }
  2385. return sample;
  2386. }
  2387. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  2388. {
  2389. MOVContext *mov = s->priv_data;
  2390. MOVStreamContext *sc;
  2391. AVIndexEntry *sample;
  2392. AVStream *st = NULL;
  2393. int ret;
  2394. retry:
  2395. sample = mov_find_next_sample(s, &st);
  2396. if (!sample) {
  2397. mov->found_mdat = 0;
  2398. if (!mov->next_root_atom)
  2399. return AVERROR_EOF;
  2400. avio_seek(s->pb, mov->next_root_atom, SEEK_SET);
  2401. mov->next_root_atom = 0;
  2402. if (mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
  2403. s->pb->eof_reached)
  2404. return AVERROR_EOF;
  2405. av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
  2406. goto retry;
  2407. }
  2408. sc = st->priv_data;
  2409. /* must be done just before reading, to avoid infinite loop on sample */
  2410. sc->current_sample++;
  2411. if (st->discard != AVDISCARD_ALL) {
  2412. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  2413. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  2414. sc->ffindex, sample->pos);
  2415. return AVERROR_INVALIDDATA;
  2416. }
  2417. ret = av_get_packet(sc->pb, pkt, sample->size);
  2418. if (ret < 0)
  2419. return ret;
  2420. if (sc->has_palette) {
  2421. uint8_t *pal;
  2422. pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
  2423. if (!pal) {
  2424. av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
  2425. } else {
  2426. memcpy(pal, sc->palette, AVPALETTE_SIZE);
  2427. sc->has_palette = 0;
  2428. }
  2429. }
  2430. #if CONFIG_DV_DEMUXER
  2431. if (mov->dv_demux && sc->dv_audio_container) {
  2432. avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
  2433. av_free(pkt->data);
  2434. pkt->size = 0;
  2435. ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
  2436. if (ret < 0)
  2437. return ret;
  2438. }
  2439. #endif
  2440. }
  2441. pkt->stream_index = sc->ffindex;
  2442. pkt->dts = sample->timestamp;
  2443. if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
  2444. pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
  2445. /* update ctts context */
  2446. sc->ctts_sample++;
  2447. if (sc->ctts_index < sc->ctts_count &&
  2448. sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
  2449. sc->ctts_index++;
  2450. sc->ctts_sample = 0;
  2451. }
  2452. if (sc->wrong_dts)
  2453. pkt->dts = AV_NOPTS_VALUE;
  2454. } else {
  2455. int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
  2456. st->index_entries[sc->current_sample].timestamp : st->duration;
  2457. pkt->duration = next_dts - pkt->dts;
  2458. pkt->pts = pkt->dts;
  2459. }
  2460. if (st->discard == AVDISCARD_ALL)
  2461. goto retry;
  2462. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
  2463. pkt->pos = sample->pos;
  2464. av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
  2465. pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
  2466. return 0;
  2467. }
  2468. static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
  2469. {
  2470. MOVStreamContext *sc = st->priv_data;
  2471. int sample, time_sample;
  2472. int i;
  2473. sample = av_index_search_timestamp(st, timestamp, flags);
  2474. av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  2475. if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
  2476. sample = 0;
  2477. if (sample < 0) /* not sure what to do */
  2478. return AVERROR_INVALIDDATA;
  2479. sc->current_sample = sample;
  2480. av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
  2481. /* adjust ctts index */
  2482. if (sc->ctts_data) {
  2483. time_sample = 0;
  2484. for (i = 0; i < sc->ctts_count; i++) {
  2485. int next = time_sample + sc->ctts_data[i].count;
  2486. if (next > sc->current_sample) {
  2487. sc->ctts_index = i;
  2488. sc->ctts_sample = sc->current_sample - time_sample;
  2489. break;
  2490. }
  2491. time_sample = next;
  2492. }
  2493. }
  2494. return sample;
  2495. }
  2496. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  2497. {
  2498. AVStream *st;
  2499. int64_t seek_timestamp, timestamp;
  2500. int sample;
  2501. int i;
  2502. if (stream_index >= s->nb_streams)
  2503. return AVERROR_INVALIDDATA;
  2504. if (sample_time < 0)
  2505. sample_time = 0;
  2506. st = s->streams[stream_index];
  2507. sample = mov_seek_stream(s, st, sample_time, flags);
  2508. if (sample < 0)
  2509. return sample;
  2510. /* adjust seek timestamp to found sample timestamp */
  2511. seek_timestamp = st->index_entries[sample].timestamp;
  2512. for (i = 0; i < s->nb_streams; i++) {
  2513. st = s->streams[i];
  2514. if (stream_index == i)
  2515. continue;
  2516. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  2517. mov_seek_stream(s, st, timestamp, flags);
  2518. }
  2519. return 0;
  2520. }
  2521. AVInputFormat ff_mov_demuxer = {
  2522. .name = "mov,mp4,m4a,3gp,3g2,mj2",
  2523. .long_name = NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),
  2524. .priv_data_size = sizeof(MOVContext),
  2525. .read_probe = mov_probe,
  2526. .read_header = mov_read_header,
  2527. .read_packet = mov_read_packet,
  2528. .read_close = mov_read_close,
  2529. .read_seek = mov_read_seek,
  2530. };