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.

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