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.

3400 lines
114KB

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