You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3409 lines
114KB

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