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.

2857 lines
96KB

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