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.

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