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.

4482 lines
151KB

  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 <inttypes.h>
  26. #include <limits.h>
  27. #include <stdint.h>
  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/time_internal.h"
  34. #include "libavutil/avstring.h"
  35. #include "libavutil/dict.h"
  36. #include "libavutil/display.h"
  37. #include "libavutil/opt.h"
  38. #include "libavutil/timecode.h"
  39. #include "libavcodec/ac3tab.h"
  40. #include "avformat.h"
  41. #include "internal.h"
  42. #include "avio_internal.h"
  43. #include "riff.h"
  44. #include "isom.h"
  45. #include "libavcodec/get_bits.h"
  46. #include "id3v1.h"
  47. #include "mov_chan.h"
  48. #include "replaygain.h"
  49. #if CONFIG_ZLIB
  50. #include <zlib.h>
  51. #endif
  52. #include "qtpalette.h"
  53. /* those functions parse an atom */
  54. /* links atom IDs to parse functions */
  55. typedef struct MOVParseTableEntry {
  56. uint32_t type;
  57. int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
  58. } MOVParseTableEntry;
  59. static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
  60. static int mov_read_mfra(MOVContext *c, AVIOContext *f);
  61. static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
  62. unsigned len, const char *key)
  63. {
  64. char buf[16];
  65. short current, total = 0;
  66. avio_rb16(pb); // unknown
  67. current = avio_rb16(pb);
  68. if (len >= 6)
  69. total = avio_rb16(pb);
  70. if (!total)
  71. snprintf(buf, sizeof(buf), "%d", current);
  72. else
  73. snprintf(buf, sizeof(buf), "%d/%d", current, total);
  74. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  75. av_dict_set(&c->fc->metadata, key, buf, 0);
  76. return 0;
  77. }
  78. static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
  79. unsigned len, const char *key)
  80. {
  81. /* bypass padding bytes */
  82. avio_r8(pb);
  83. avio_r8(pb);
  84. avio_r8(pb);
  85. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  86. av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
  87. return 0;
  88. }
  89. static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
  90. unsigned len, const char *key)
  91. {
  92. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  93. av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
  94. return 0;
  95. }
  96. static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
  97. unsigned len, const char *key)
  98. {
  99. short genre;
  100. avio_r8(pb); // unknown
  101. genre = avio_r8(pb);
  102. if (genre < 1 || genre > ID3v1_GENRE_MAX)
  103. return 0;
  104. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  105. av_dict_set(&c->fc->metadata, key, ff_id3v1_genre_str[genre-1], 0);
  106. return 0;
  107. }
  108. static const uint32_t mac_to_unicode[128] = {
  109. 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
  110. 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
  111. 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
  112. 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
  113. 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
  114. 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
  115. 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
  116. 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
  117. 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
  118. 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
  119. 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
  120. 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
  121. 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
  122. 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
  123. 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
  124. 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
  125. };
  126. static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
  127. char *dst, int dstlen)
  128. {
  129. char *p = dst;
  130. char *end = dst+dstlen-1;
  131. int i;
  132. for (i = 0; i < len; i++) {
  133. uint8_t t, c = avio_r8(pb);
  134. if (c < 0x80 && p < end)
  135. *p++ = c;
  136. else if (p < end)
  137. PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
  138. }
  139. *p = 0;
  140. return p - dst;
  141. }
  142. static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
  143. {
  144. AVPacket pkt;
  145. AVStream *st;
  146. MOVStreamContext *sc;
  147. enum AVCodecID id;
  148. int ret;
  149. switch (type) {
  150. case 0xd: id = AV_CODEC_ID_MJPEG; break;
  151. case 0xe: id = AV_CODEC_ID_PNG; break;
  152. case 0x1b: id = AV_CODEC_ID_BMP; break;
  153. default:
  154. av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
  155. avio_skip(pb, len);
  156. return 0;
  157. }
  158. st = avformat_new_stream(c->fc, NULL);
  159. if (!st)
  160. return AVERROR(ENOMEM);
  161. sc = av_mallocz(sizeof(*sc));
  162. if (!sc)
  163. return AVERROR(ENOMEM);
  164. st->priv_data = sc;
  165. ret = av_get_packet(pb, &pkt, len);
  166. if (ret < 0)
  167. return ret;
  168. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  169. st->attached_pic = pkt;
  170. st->attached_pic.stream_index = st->index;
  171. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  172. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  173. st->codec->codec_id = id;
  174. return 0;
  175. }
  176. static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
  177. {
  178. char language[4] = { 0 };
  179. char buf[100];
  180. uint16_t langcode = 0;
  181. double longitude, latitude;
  182. const char *key = "location";
  183. if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4) {
  184. av_log(c->fc, AV_LOG_ERROR, "loci too short\n");
  185. return AVERROR_INVALIDDATA;
  186. }
  187. avio_skip(pb, 4); // version+flags
  188. langcode = avio_rb16(pb);
  189. ff_mov_lang_to_iso639(langcode, language);
  190. len -= 6;
  191. len -= avio_get_str(pb, len, buf, sizeof(buf)); // place name
  192. if (len < 1) {
  193. av_log(c->fc, AV_LOG_ERROR, "place name too long\n");
  194. return AVERROR_INVALIDDATA;
  195. }
  196. avio_skip(pb, 1); // role
  197. len -= 1;
  198. if (len < 12) {
  199. av_log(c->fc, AV_LOG_ERROR, "no space for coordinates left (%d)\n", len);
  200. return AVERROR_INVALIDDATA;
  201. }
  202. longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
  203. latitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
  204. // Try to output in the same format as the ?xyz field
  205. snprintf(buf, sizeof(buf), "%+08.4f%+09.4f/", latitude, longitude);
  206. if (*language && strcmp(language, "und")) {
  207. char key2[16];
  208. snprintf(key2, sizeof(key2), "%s-%s", key, language);
  209. av_dict_set(&c->fc->metadata, key2, buf, 0);
  210. }
  211. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  212. return av_dict_set(&c->fc->metadata, key, buf, 0);
  213. }
  214. static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  215. {
  216. char tmp_key[5];
  217. char key2[32], language[4] = {0};
  218. char *str = NULL;
  219. const char *key = NULL;
  220. uint16_t langcode = 0;
  221. uint32_t data_type = 0, str_size, str_size_alloc;
  222. int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
  223. int raw = 0;
  224. switch (atom.type) {
  225. case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break;
  226. case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break;
  227. case MKTAG( 'X','M','P','_'):
  228. if (c->export_xmp) { key = "xmp"; raw = 1; } break;
  229. case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
  230. case MKTAG( 'a','k','I','D'): key = "account_type";
  231. parse = mov_metadata_int8_no_padding; break;
  232. case MKTAG( 'a','p','I','D'): key = "account_id"; break;
  233. case MKTAG( 'c','a','t','g'): key = "category"; break;
  234. case MKTAG( 'c','p','i','l'): key = "compilation";
  235. parse = mov_metadata_int8_no_padding; break;
  236. case MKTAG( 'c','p','r','t'): key = "copyright"; break;
  237. case MKTAG( 'd','e','s','c'): key = "description"; break;
  238. case MKTAG( 'd','i','s','k'): key = "disc";
  239. parse = mov_metadata_track_or_disc_number; break;
  240. case MKTAG( 'e','g','i','d'): key = "episode_uid";
  241. parse = mov_metadata_int8_no_padding; break;
  242. case MKTAG( 'g','n','r','e'): key = "genre";
  243. parse = mov_metadata_gnre; break;
  244. case MKTAG( 'h','d','v','d'): key = "hd_video";
  245. parse = mov_metadata_int8_no_padding; break;
  246. case MKTAG( 'k','e','y','w'): key = "keywords"; break;
  247. case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
  248. case MKTAG( 'l','o','c','i'):
  249. return mov_metadata_loci(c, pb, atom.size);
  250. case MKTAG( 'p','c','s','t'): key = "podcast";
  251. parse = mov_metadata_int8_no_padding; break;
  252. case MKTAG( 'p','g','a','p'): key = "gapless_playback";
  253. parse = mov_metadata_int8_no_padding; break;
  254. case MKTAG( 'p','u','r','d'): key = "purchase_date"; break;
  255. case MKTAG( 'r','t','n','g'): key = "rating";
  256. parse = mov_metadata_int8_no_padding; break;
  257. case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break;
  258. case MKTAG( 's','o','a','l'): key = "sort_album"; break;
  259. case MKTAG( 's','o','a','r'): key = "sort_artist"; break;
  260. case MKTAG( 's','o','c','o'): key = "sort_composer"; break;
  261. case MKTAG( 's','o','n','m'): key = "sort_name"; break;
  262. case MKTAG( 's','o','s','n'): key = "sort_show"; break;
  263. case MKTAG( 's','t','i','k'): key = "media_type";
  264. parse = mov_metadata_int8_no_padding; break;
  265. case MKTAG( 't','r','k','n'): key = "track";
  266. parse = mov_metadata_track_or_disc_number; break;
  267. case MKTAG( 't','v','e','n'): key = "episode_id"; break;
  268. case MKTAG( 't','v','e','s'): key = "episode_sort";
  269. parse = mov_metadata_int8_bypass_padding; break;
  270. case MKTAG( 't','v','n','n'): key = "network"; break;
  271. case MKTAG( 't','v','s','h'): key = "show"; break;
  272. case MKTAG( 't','v','s','n'): key = "season_number";
  273. parse = mov_metadata_int8_bypass_padding; break;
  274. case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
  275. case MKTAG(0xa9,'P','R','D'): key = "producer"; break;
  276. case MKTAG(0xa9,'a','l','b'): key = "album"; break;
  277. case MKTAG(0xa9,'a','u','t'): key = "artist"; break;
  278. case MKTAG(0xa9,'c','h','p'): key = "chapter"; break;
  279. case MKTAG(0xa9,'c','m','t'): key = "comment"; break;
  280. case MKTAG(0xa9,'c','o','m'): key = "composer"; break;
  281. case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
  282. case MKTAG(0xa9,'d','a','y'): key = "date"; break;
  283. case MKTAG(0xa9,'d','i','r'): key = "director"; break;
  284. case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break;
  285. case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break;
  286. case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
  287. case MKTAG(0xa9,'f','m','t'): key = "original_format"; break;
  288. case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
  289. case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
  290. case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break;
  291. case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
  292. case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
  293. case MKTAG(0xa9,'m','a','k'): key = "make"; break;
  294. case MKTAG(0xa9,'m','o','d'): key = "model"; break;
  295. case MKTAG(0xa9,'n','a','m'): key = "title"; break;
  296. case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break;
  297. case MKTAG(0xa9,'p','r','d'): key = "producer"; break;
  298. case MKTAG(0xa9,'p','r','f'): key = "performers"; break;
  299. case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break;
  300. case MKTAG(0xa9,'s','r','c'): key = "original_source"; break;
  301. case MKTAG(0xa9,'s','t','3'): key = "subtitle"; break;
  302. case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
  303. case MKTAG(0xa9,'t','o','o'): key = "encoder"; break;
  304. case MKTAG(0xa9,'t','r','k'): key = "track"; break;
  305. case MKTAG(0xa9,'u','r','l'): key = "URL"; break;
  306. case MKTAG(0xa9,'w','r','n'): key = "warning"; break;
  307. case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
  308. case MKTAG(0xa9,'x','y','z'): key = "location"; break;
  309. }
  310. retry:
  311. if (c->itunes_metadata && atom.size > 8) {
  312. int data_size = avio_rb32(pb);
  313. int tag = avio_rl32(pb);
  314. if (tag == MKTAG('d','a','t','a') && data_size <= atom.size) {
  315. data_type = avio_rb32(pb); // type
  316. avio_rb32(pb); // unknown
  317. str_size = data_size - 16;
  318. atom.size -= 16;
  319. if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
  320. int ret = mov_read_covr(c, pb, data_type, str_size);
  321. if (ret < 0) {
  322. av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
  323. }
  324. return ret;
  325. }
  326. } else return 0;
  327. } else if (atom.size > 4 && key && !c->itunes_metadata && !raw) {
  328. str_size = avio_rb16(pb); // string length
  329. if (str_size > atom.size) {
  330. raw = 1;
  331. avio_seek(pb, -2, SEEK_CUR);
  332. av_log(c->fc, AV_LOG_WARNING, "UDTA parsing failed retrying raw\n");
  333. goto retry;
  334. }
  335. langcode = avio_rb16(pb);
  336. ff_mov_lang_to_iso639(langcode, language);
  337. atom.size -= 4;
  338. } else
  339. str_size = atom.size;
  340. if (c->export_all && !key) {
  341. snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
  342. key = tmp_key;
  343. }
  344. if (!key)
  345. return 0;
  346. if (atom.size < 0 || str_size >= INT_MAX/2)
  347. return AVERROR_INVALIDDATA;
  348. // worst-case requirement for output string in case of utf8 coded input
  349. str_size_alloc = (raw ? str_size : str_size * 2) + 1;
  350. str = av_mallocz(str_size_alloc);
  351. if (!str)
  352. return AVERROR(ENOMEM);
  353. if (parse)
  354. parse(c, pb, str_size, key);
  355. else {
  356. if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded
  357. mov_read_mac_string(c, pb, str_size, str, str_size_alloc);
  358. } else {
  359. int ret = avio_read(pb, str, str_size);
  360. if (ret != str_size) {
  361. av_freep(&str);
  362. return ret < 0 ? ret : AVERROR_INVALIDDATA;
  363. }
  364. str[str_size] = 0;
  365. }
  366. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  367. av_dict_set(&c->fc->metadata, key, str, 0);
  368. if (*language && strcmp(language, "und")) {
  369. snprintf(key2, sizeof(key2), "%s-%s", key, language);
  370. av_dict_set(&c->fc->metadata, key2, str, 0);
  371. }
  372. }
  373. av_log(c->fc, AV_LOG_TRACE, "lang \"%3s\" ", language);
  374. av_log(c->fc, AV_LOG_TRACE, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
  375. key, str, (char*)&atom.type, str_size_alloc, atom.size);
  376. av_freep(&str);
  377. return 0;
  378. }
  379. static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  380. {
  381. int64_t start;
  382. int i, nb_chapters, str_len, version;
  383. char str[256+1];
  384. if ((atom.size -= 5) < 0)
  385. return 0;
  386. version = avio_r8(pb);
  387. avio_rb24(pb);
  388. if (version)
  389. avio_rb32(pb); // ???
  390. nb_chapters = avio_r8(pb);
  391. for (i = 0; i < nb_chapters; i++) {
  392. if (atom.size < 9)
  393. return 0;
  394. start = avio_rb64(pb);
  395. str_len = avio_r8(pb);
  396. if ((atom.size -= 9+str_len) < 0)
  397. return 0;
  398. if (avio_read(pb, str, str_len) != str_len)
  399. return AVERROR_INVALIDDATA;
  400. str[str_len] = 0;
  401. avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
  402. }
  403. return 0;
  404. }
  405. #define MIN_DATA_ENTRY_BOX_SIZE 12
  406. static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  407. {
  408. AVStream *st;
  409. MOVStreamContext *sc;
  410. int entries, i, j;
  411. if (c->fc->nb_streams < 1)
  412. return 0;
  413. st = c->fc->streams[c->fc->nb_streams-1];
  414. sc = st->priv_data;
  415. avio_rb32(pb); // version + flags
  416. entries = avio_rb32(pb);
  417. if (entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
  418. entries >= UINT_MAX / sizeof(*sc->drefs))
  419. return AVERROR_INVALIDDATA;
  420. av_free(sc->drefs);
  421. sc->drefs_count = 0;
  422. sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
  423. if (!sc->drefs)
  424. return AVERROR(ENOMEM);
  425. sc->drefs_count = entries;
  426. for (i = 0; i < sc->drefs_count; i++) {
  427. MOVDref *dref = &sc->drefs[i];
  428. uint32_t size = avio_rb32(pb);
  429. int64_t next = avio_tell(pb) + size - 4;
  430. if (size < 12)
  431. return AVERROR_INVALIDDATA;
  432. dref->type = avio_rl32(pb);
  433. avio_rb32(pb); // version + flags
  434. av_log(c->fc, AV_LOG_TRACE, "type %.4s size %d\n", (char*)&dref->type, size);
  435. if (dref->type == MKTAG('a','l','i','s') && size > 150) {
  436. /* macintosh alias record */
  437. uint16_t volume_len, len;
  438. int16_t type;
  439. avio_skip(pb, 10);
  440. volume_len = avio_r8(pb);
  441. volume_len = FFMIN(volume_len, 27);
  442. if (avio_read(pb, dref->volume, 27) != 27)
  443. return AVERROR_INVALIDDATA;
  444. dref->volume[volume_len] = 0;
  445. av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
  446. avio_skip(pb, 12);
  447. len = avio_r8(pb);
  448. len = FFMIN(len, 63);
  449. if (avio_read(pb, dref->filename, 63) != 63)
  450. return AVERROR_INVALIDDATA;
  451. dref->filename[len] = 0;
  452. av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
  453. avio_skip(pb, 16);
  454. /* read next level up_from_alias/down_to_target */
  455. dref->nlvl_from = avio_rb16(pb);
  456. dref->nlvl_to = avio_rb16(pb);
  457. av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
  458. dref->nlvl_from, dref->nlvl_to);
  459. avio_skip(pb, 16);
  460. for (type = 0; type != -1 && avio_tell(pb) < next; ) {
  461. if(avio_feof(pb))
  462. return AVERROR_EOF;
  463. type = avio_rb16(pb);
  464. len = avio_rb16(pb);
  465. av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
  466. if (len&1)
  467. len += 1;
  468. if (type == 2 || type == 18) { // absolute path
  469. av_free(dref->path);
  470. dref->path = av_mallocz(len+1);
  471. if (!dref->path)
  472. return AVERROR(ENOMEM);
  473. if (avio_read(pb, dref->path, len) != len) {
  474. av_freep(&dref->path);
  475. return AVERROR_INVALIDDATA;
  476. }
  477. if (type == 18) // no additional processing needed
  478. continue;
  479. if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
  480. len -= volume_len;
  481. memmove(dref->path, dref->path+volume_len, len);
  482. dref->path[len] = 0;
  483. }
  484. for (j = 0; j < len; j++)
  485. if (dref->path[j] == ':')
  486. dref->path[j] = '/';
  487. av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
  488. } else if (type == 0) { // directory name
  489. av_free(dref->dir);
  490. dref->dir = av_malloc(len+1);
  491. if (!dref->dir)
  492. return AVERROR(ENOMEM);
  493. if (avio_read(pb, dref->dir, len) != len) {
  494. av_freep(&dref->dir);
  495. return AVERROR_INVALIDDATA;
  496. }
  497. dref->dir[len] = 0;
  498. for (j = 0; j < len; j++)
  499. if (dref->dir[j] == ':')
  500. dref->dir[j] = '/';
  501. av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
  502. } else
  503. avio_skip(pb, len);
  504. }
  505. }
  506. avio_seek(pb, next, SEEK_SET);
  507. }
  508. return 0;
  509. }
  510. static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  511. {
  512. AVStream *st;
  513. uint32_t type;
  514. uint32_t av_unused ctype;
  515. int64_t title_size;
  516. char *title_str;
  517. if (c->fc->nb_streams < 1) // meta before first trak
  518. return 0;
  519. st = c->fc->streams[c->fc->nb_streams-1];
  520. avio_r8(pb); /* version */
  521. avio_rb24(pb); /* flags */
  522. /* component type */
  523. ctype = avio_rl32(pb);
  524. type = avio_rl32(pb); /* component subtype */
  525. av_log(c->fc, AV_LOG_TRACE, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
  526. av_log(c->fc, AV_LOG_TRACE, "stype= %.4s\n", (char*)&type);
  527. if (type == MKTAG('v','i','d','e'))
  528. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  529. else if (type == MKTAG('s','o','u','n'))
  530. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  531. else if (type == MKTAG('m','1','a',' '))
  532. st->codec->codec_id = AV_CODEC_ID_MP2;
  533. else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
  534. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  535. avio_rb32(pb); /* component manufacture */
  536. avio_rb32(pb); /* component flags */
  537. avio_rb32(pb); /* component flags mask */
  538. title_size = atom.size - 24;
  539. if (title_size > 0) {
  540. title_str = av_malloc(title_size + 1); /* Add null terminator */
  541. if (!title_str)
  542. return AVERROR(ENOMEM);
  543. if (avio_read(pb, title_str, title_size) != title_size) {
  544. av_freep(&title_str);
  545. return AVERROR_INVALIDDATA;
  546. }
  547. title_str[title_size] = 0;
  548. if (title_str[0]) {
  549. int off = (!c->isom && title_str[0] == title_size - 1);
  550. av_dict_set(&st->metadata, "handler_name", title_str + off, 0);
  551. }
  552. av_freep(&title_str);
  553. }
  554. return 0;
  555. }
  556. int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb)
  557. {
  558. AVStream *st;
  559. int tag;
  560. if (fc->nb_streams < 1)
  561. return 0;
  562. st = fc->streams[fc->nb_streams-1];
  563. avio_rb32(pb); /* version + flags */
  564. ff_mp4_read_descr(fc, pb, &tag);
  565. if (tag == MP4ESDescrTag) {
  566. ff_mp4_parse_es_descr(pb, NULL);
  567. } else
  568. avio_rb16(pb); /* ID */
  569. ff_mp4_read_descr(fc, pb, &tag);
  570. if (tag == MP4DecConfigDescrTag)
  571. ff_mp4_read_dec_config_descr(fc, st, pb);
  572. return 0;
  573. }
  574. static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  575. {
  576. return ff_mov_read_esds(c->fc, pb);
  577. }
  578. static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  579. {
  580. AVStream *st;
  581. enum AVAudioServiceType *ast;
  582. int ac3info, acmod, lfeon, bsmod;
  583. if (c->fc->nb_streams < 1)
  584. return 0;
  585. st = c->fc->streams[c->fc->nb_streams-1];
  586. ast = (enum AVAudioServiceType*)ff_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE,
  587. sizeof(*ast));
  588. if (!ast)
  589. return AVERROR(ENOMEM);
  590. ac3info = avio_rb24(pb);
  591. bsmod = (ac3info >> 14) & 0x7;
  592. acmod = (ac3info >> 11) & 0x7;
  593. lfeon = (ac3info >> 10) & 0x1;
  594. st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
  595. st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
  596. if (lfeon)
  597. st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
  598. *ast = bsmod;
  599. if (st->codec->channels > 1 && bsmod == 0x7)
  600. *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  601. st->codec->audio_service_type = *ast;
  602. return 0;
  603. }
  604. static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  605. {
  606. AVStream *st;
  607. enum AVAudioServiceType *ast;
  608. int eac3info, acmod, lfeon, bsmod;
  609. if (c->fc->nb_streams < 1)
  610. return 0;
  611. st = c->fc->streams[c->fc->nb_streams-1];
  612. ast = (enum AVAudioServiceType*)ff_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE,
  613. sizeof(*ast));
  614. if (!ast)
  615. return AVERROR(ENOMEM);
  616. /* No need to parse fields for additional independent substreams and its
  617. * associated dependent substreams since libavcodec's E-AC-3 decoder
  618. * does not support them yet. */
  619. avio_rb16(pb); /* data_rate and num_ind_sub */
  620. eac3info = avio_rb24(pb);
  621. bsmod = (eac3info >> 12) & 0x1f;
  622. acmod = (eac3info >> 9) & 0x7;
  623. lfeon = (eac3info >> 8) & 0x1;
  624. st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
  625. if (lfeon)
  626. st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
  627. st->codec->channels = av_get_channel_layout_nb_channels(st->codec->channel_layout);
  628. *ast = bsmod;
  629. if (st->codec->channels > 1 && bsmod == 0x7)
  630. *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  631. st->codec->audio_service_type = *ast;
  632. return 0;
  633. }
  634. static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  635. {
  636. AVStream *st;
  637. if (c->fc->nb_streams < 1)
  638. return 0;
  639. st = c->fc->streams[c->fc->nb_streams-1];
  640. if (atom.size < 16)
  641. return 0;
  642. /* skip version and flags */
  643. avio_skip(pb, 4);
  644. ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
  645. return 0;
  646. }
  647. static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  648. {
  649. AVStream *st;
  650. int ret;
  651. if (c->fc->nb_streams < 1)
  652. return 0;
  653. st = c->fc->streams[c->fc->nb_streams-1];
  654. if ((ret = ff_get_wav_header(pb, st->codec, atom.size, 0)) < 0)
  655. av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
  656. return ret;
  657. }
  658. static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  659. {
  660. const int num = avio_rb32(pb);
  661. const int den = avio_rb32(pb);
  662. AVStream *st;
  663. if (c->fc->nb_streams < 1)
  664. return 0;
  665. st = c->fc->streams[c->fc->nb_streams-1];
  666. if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
  667. (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
  668. av_log(c->fc, AV_LOG_WARNING,
  669. "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
  670. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  671. num, den);
  672. } else if (den != 0) {
  673. av_reduce(&st->sample_aspect_ratio.num, &st->sample_aspect_ratio.den,
  674. num, den, 32767);
  675. }
  676. return 0;
  677. }
  678. /* this atom contains actual media data */
  679. static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  680. {
  681. if (atom.size == 0) /* wrong one (MP4) */
  682. return 0;
  683. c->found_mdat=1;
  684. return 0; /* now go for moov */
  685. }
  686. /* read major brand, minor version and compatible brands and store them as metadata */
  687. static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  688. {
  689. uint32_t minor_ver;
  690. int comp_brand_size;
  691. char* comp_brands_str;
  692. uint8_t type[5] = {0};
  693. if (avio_read(pb, type, 4) != 4)
  694. return AVERROR_INVALIDDATA;
  695. if (strcmp(type, "qt "))
  696. c->isom = 1;
  697. av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
  698. av_dict_set(&c->fc->metadata, "major_brand", type, 0);
  699. minor_ver = avio_rb32(pb); /* minor version */
  700. av_dict_set_int(&c->fc->metadata, "minor_version", minor_ver, 0);
  701. comp_brand_size = atom.size - 8;
  702. if (comp_brand_size < 0)
  703. return AVERROR_INVALIDDATA;
  704. comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
  705. if (!comp_brands_str)
  706. return AVERROR(ENOMEM);
  707. if (avio_read(pb, comp_brands_str, comp_brand_size) != comp_brand_size) {
  708. av_freep(&comp_brands_str);
  709. return AVERROR_INVALIDDATA;
  710. }
  711. comp_brands_str[comp_brand_size] = 0;
  712. av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
  713. av_freep(&comp_brands_str);
  714. return 0;
  715. }
  716. /* this atom should contain all header atoms */
  717. static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  718. {
  719. int ret;
  720. if (c->found_moov) {
  721. av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n");
  722. avio_skip(pb, atom.size);
  723. return 0;
  724. }
  725. if ((ret = mov_read_default(c, pb, atom)) < 0)
  726. return ret;
  727. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  728. /* so we don't parse the whole file if over a network */
  729. c->found_moov=1;
  730. return 0; /* now go for mdat */
  731. }
  732. static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  733. {
  734. if (!c->has_looked_for_mfra && c->use_mfra_for > 0) {
  735. c->has_looked_for_mfra = 1;
  736. if (pb->seekable) {
  737. int ret;
  738. av_log(c->fc, AV_LOG_VERBOSE, "stream has moof boxes, will look "
  739. "for a mfra\n");
  740. if ((ret = mov_read_mfra(c, pb)) < 0) {
  741. av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but failed to "
  742. "read the mfra (may be a live ismv)\n");
  743. }
  744. } else {
  745. av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but stream is not "
  746. "seekable, can not look for mfra\n");
  747. }
  748. }
  749. c->fragment.moof_offset = c->fragment.implicit_offset = avio_tell(pb) - 8;
  750. av_log(c->fc, AV_LOG_TRACE, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
  751. return mov_read_default(c, pb, atom);
  752. }
  753. static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time)
  754. {
  755. char buffer[32];
  756. if (time) {
  757. struct tm *ptm, tmbuf;
  758. time_t timet;
  759. if(time >= 2082844800)
  760. time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
  761. timet = time;
  762. ptm = gmtime_r(&timet, &tmbuf);
  763. if (!ptm) return;
  764. if (strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm))
  765. av_dict_set(metadata, "creation_time", buffer, 0);
  766. }
  767. }
  768. static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  769. {
  770. AVStream *st;
  771. MOVStreamContext *sc;
  772. int version;
  773. char language[4] = {0};
  774. unsigned lang;
  775. int64_t creation_time;
  776. if (c->fc->nb_streams < 1)
  777. return 0;
  778. st = c->fc->streams[c->fc->nb_streams-1];
  779. sc = st->priv_data;
  780. if (sc->time_scale) {
  781. av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
  782. return AVERROR_INVALIDDATA;
  783. }
  784. version = avio_r8(pb);
  785. if (version > 1) {
  786. avpriv_request_sample(c->fc, "Version %d", version);
  787. return AVERROR_PATCHWELCOME;
  788. }
  789. avio_rb24(pb); /* flags */
  790. if (version == 1) {
  791. creation_time = avio_rb64(pb);
  792. avio_rb64(pb);
  793. } else {
  794. creation_time = avio_rb32(pb);
  795. avio_rb32(pb); /* modification time */
  796. }
  797. mov_metadata_creation_time(&st->metadata, creation_time);
  798. sc->time_scale = avio_rb32(pb);
  799. st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  800. lang = avio_rb16(pb); /* language */
  801. if (ff_mov_lang_to_iso639(lang, language))
  802. av_dict_set(&st->metadata, "language", language, 0);
  803. avio_rb16(pb); /* quality */
  804. return 0;
  805. }
  806. static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  807. {
  808. int64_t creation_time;
  809. int version = avio_r8(pb); /* version */
  810. avio_rb24(pb); /* flags */
  811. if (version == 1) {
  812. creation_time = avio_rb64(pb);
  813. avio_rb64(pb);
  814. } else {
  815. creation_time = avio_rb32(pb);
  816. avio_rb32(pb); /* modification time */
  817. }
  818. mov_metadata_creation_time(&c->fc->metadata, creation_time);
  819. c->time_scale = avio_rb32(pb); /* time scale */
  820. av_log(c->fc, AV_LOG_TRACE, "time scale = %i\n", c->time_scale);
  821. c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  822. // set the AVCodecContext duration because the duration of individual tracks
  823. // may be inaccurate
  824. if (c->time_scale > 0 && !c->trex_data)
  825. c->fc->duration = av_rescale(c->duration, AV_TIME_BASE, c->time_scale);
  826. avio_rb32(pb); /* preferred scale */
  827. avio_rb16(pb); /* preferred volume */
  828. avio_skip(pb, 10); /* reserved */
  829. avio_skip(pb, 36); /* display matrix */
  830. avio_rb32(pb); /* preview time */
  831. avio_rb32(pb); /* preview duration */
  832. avio_rb32(pb); /* poster time */
  833. avio_rb32(pb); /* selection time */
  834. avio_rb32(pb); /* selection duration */
  835. avio_rb32(pb); /* current time */
  836. avio_rb32(pb); /* next track ID */
  837. return 0;
  838. }
  839. static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  840. {
  841. AVStream *st;
  842. int little_endian;
  843. if (c->fc->nb_streams < 1)
  844. return 0;
  845. st = c->fc->streams[c->fc->nb_streams-1];
  846. little_endian = avio_rb16(pb) & 0xFF;
  847. av_log(c->fc, AV_LOG_TRACE, "enda %d\n", little_endian);
  848. if (little_endian == 1) {
  849. switch (st->codec->codec_id) {
  850. case AV_CODEC_ID_PCM_S24BE:
  851. st->codec->codec_id = AV_CODEC_ID_PCM_S24LE;
  852. break;
  853. case AV_CODEC_ID_PCM_S32BE:
  854. st->codec->codec_id = AV_CODEC_ID_PCM_S32LE;
  855. break;
  856. case AV_CODEC_ID_PCM_F32BE:
  857. st->codec->codec_id = AV_CODEC_ID_PCM_F32LE;
  858. break;
  859. case AV_CODEC_ID_PCM_F64BE:
  860. st->codec->codec_id = AV_CODEC_ID_PCM_F64LE;
  861. break;
  862. default:
  863. break;
  864. }
  865. }
  866. return 0;
  867. }
  868. static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  869. {
  870. AVStream *st;
  871. char color_parameter_type[5] = { 0 };
  872. int color_primaries, color_trc, color_matrix;
  873. if (c->fc->nb_streams < 1)
  874. return 0;
  875. st = c->fc->streams[c->fc->nb_streams - 1];
  876. if (avio_read(pb, color_parameter_type, 4) != 4)
  877. return AVERROR_INVALIDDATA;
  878. if (strncmp(color_parameter_type, "nclx", 4) &&
  879. strncmp(color_parameter_type, "nclc", 4)) {
  880. av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n",
  881. color_parameter_type);
  882. return 0;
  883. }
  884. color_primaries = avio_rb16(pb);
  885. color_trc = avio_rb16(pb);
  886. color_matrix = avio_rb16(pb);
  887. av_log(c->fc, AV_LOG_TRACE,
  888. "%s: pri %d trc %d matrix %d",
  889. color_parameter_type, color_primaries, color_trc, color_matrix);
  890. if (!strncmp(color_parameter_type, "nclx", 4)) {
  891. uint8_t color_range = avio_r8(pb) >> 7;
  892. av_log(c->fc, AV_LOG_TRACE, " full %"PRIu8"", color_range);
  893. if (color_range)
  894. st->codec->color_range = AVCOL_RANGE_JPEG;
  895. else
  896. st->codec->color_range = AVCOL_RANGE_MPEG;
  897. /* 14496-12 references JPEG XR specs (rather than the more complete
  898. * 23001-8) so some adjusting is required */
  899. if (color_primaries >= AVCOL_PRI_FILM)
  900. color_primaries = AVCOL_PRI_UNSPECIFIED;
  901. if ((color_trc >= AVCOL_TRC_LINEAR &&
  902. color_trc <= AVCOL_TRC_LOG_SQRT) ||
  903. color_trc >= AVCOL_TRC_BT2020_10)
  904. color_trc = AVCOL_TRC_UNSPECIFIED;
  905. if (color_matrix >= AVCOL_SPC_BT2020_NCL)
  906. color_matrix = AVCOL_SPC_UNSPECIFIED;
  907. st->codec->color_primaries = color_primaries;
  908. st->codec->color_trc = color_trc;
  909. st->codec->colorspace = color_matrix;
  910. } else if (!strncmp(color_parameter_type, "nclc", 4)) {
  911. /* color primaries, Table 4-4 */
  912. switch (color_primaries) {
  913. case 1: st->codec->color_primaries = AVCOL_PRI_BT709; break;
  914. case 5: st->codec->color_primaries = AVCOL_PRI_SMPTE170M; break;
  915. case 6: st->codec->color_primaries = AVCOL_PRI_SMPTE240M; break;
  916. }
  917. /* color transfer, Table 4-5 */
  918. switch (color_trc) {
  919. case 1: st->codec->color_trc = AVCOL_TRC_BT709; break;
  920. case 7: st->codec->color_trc = AVCOL_TRC_SMPTE240M; break;
  921. }
  922. /* color matrix, Table 4-6 */
  923. switch (color_matrix) {
  924. case 1: st->codec->colorspace = AVCOL_SPC_BT709; break;
  925. case 6: st->codec->colorspace = AVCOL_SPC_BT470BG; break;
  926. case 7: st->codec->colorspace = AVCOL_SPC_SMPTE240M; break;
  927. }
  928. }
  929. av_log(c->fc, AV_LOG_TRACE, "\n");
  930. return 0;
  931. }
  932. static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  933. {
  934. AVStream *st;
  935. unsigned mov_field_order;
  936. enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
  937. if (c->fc->nb_streams < 1) // will happen with jp2 files
  938. return 0;
  939. st = c->fc->streams[c->fc->nb_streams-1];
  940. if (atom.size < 2)
  941. return AVERROR_INVALIDDATA;
  942. mov_field_order = avio_rb16(pb);
  943. if ((mov_field_order & 0xFF00) == 0x0100)
  944. decoded_field_order = AV_FIELD_PROGRESSIVE;
  945. else if ((mov_field_order & 0xFF00) == 0x0200) {
  946. switch (mov_field_order & 0xFF) {
  947. case 0x01: decoded_field_order = AV_FIELD_TT;
  948. break;
  949. case 0x06: decoded_field_order = AV_FIELD_BB;
  950. break;
  951. case 0x09: decoded_field_order = AV_FIELD_TB;
  952. break;
  953. case 0x0E: decoded_field_order = AV_FIELD_BT;
  954. break;
  955. }
  956. }
  957. if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
  958. av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
  959. }
  960. st->codec->field_order = decoded_field_order;
  961. return 0;
  962. }
  963. static int mov_realloc_extradata(AVCodecContext *codec, MOVAtom atom)
  964. {
  965. int err = 0;
  966. uint64_t size = (uint64_t)codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
  967. if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
  968. return AVERROR_INVALIDDATA;
  969. if ((err = av_reallocp(&codec->extradata, size)) < 0) {
  970. codec->extradata_size = 0;
  971. return err;
  972. }
  973. codec->extradata_size = size - FF_INPUT_BUFFER_PADDING_SIZE;
  974. return 0;
  975. }
  976. /* Read a whole atom into the extradata return the size of the atom read, possibly truncated if != atom.size */
  977. static int64_t mov_read_atom_into_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
  978. AVCodecContext *codec, uint8_t *buf)
  979. {
  980. int64_t result = atom.size;
  981. int err;
  982. AV_WB32(buf , atom.size + 8);
  983. AV_WL32(buf + 4, atom.type);
  984. err = avio_read(pb, buf + 8, atom.size);
  985. if (err < 0) {
  986. codec->extradata_size -= atom.size;
  987. return err;
  988. } else if (err < atom.size) {
  989. av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n");
  990. codec->extradata_size -= atom.size - err;
  991. result = err;
  992. }
  993. memset(buf + 8 + err, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  994. return result;
  995. }
  996. /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
  997. static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
  998. enum AVCodecID codec_id)
  999. {
  1000. AVStream *st;
  1001. uint64_t original_size;
  1002. int err;
  1003. if (c->fc->nb_streams < 1) // will happen with jp2 files
  1004. return 0;
  1005. st = c->fc->streams[c->fc->nb_streams-1];
  1006. if (st->codec->codec_id != codec_id)
  1007. return 0; /* unexpected codec_id - don't mess with extradata */
  1008. original_size = st->codec->extradata_size;
  1009. err = mov_realloc_extradata(st->codec, atom);
  1010. if (err)
  1011. return err;
  1012. err = mov_read_atom_into_extradata(c, pb, atom, st->codec, st->codec->extradata + original_size);
  1013. if (err < 0)
  1014. return err;
  1015. return 0; // Note: this is the original behavior to ignore truncation.
  1016. }
  1017. /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
  1018. static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1019. {
  1020. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
  1021. }
  1022. static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1023. {
  1024. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS);
  1025. }
  1026. static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1027. {
  1028. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
  1029. }
  1030. static int mov_read_dpxe(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1031. {
  1032. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_R10K);
  1033. }
  1034. static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1035. {
  1036. int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
  1037. if(ret == 0)
  1038. ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_DNXHD);
  1039. return ret;
  1040. }
  1041. static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1042. {
  1043. int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216);
  1044. if (!ret && c->fc->nb_streams >= 1) {
  1045. AVCodecContext *avctx = c->fc->streams[c->fc->nb_streams-1]->codec;
  1046. if (avctx->extradata_size >= 40) {
  1047. avctx->height = AV_RB16(&avctx->extradata[36]);
  1048. avctx->width = AV_RB16(&avctx->extradata[38]);
  1049. }
  1050. }
  1051. return ret;
  1052. }
  1053. static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1054. {
  1055. if (c->fc->nb_streams >= 1) {
  1056. AVCodecContext *codec = c->fc->streams[c->fc->nb_streams-1]->codec;
  1057. if (codec->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
  1058. codec->codec_id == AV_CODEC_ID_H264 &&
  1059. atom.size > 11) {
  1060. avio_skip(pb, 10);
  1061. /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
  1062. if (avio_rb16(pb) == 0xd4d)
  1063. codec->width = 1440;
  1064. return 0;
  1065. }
  1066. }
  1067. return mov_read_avid(c, pb, atom);
  1068. }
  1069. static int mov_read_aclr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1070. {
  1071. int ret = 0;
  1072. int length = 0;
  1073. uint64_t original_size;
  1074. if (c->fc->nb_streams >= 1) {
  1075. AVCodecContext *codec = c->fc->streams[c->fc->nb_streams-1]->codec;
  1076. if (codec->codec_id == AV_CODEC_ID_H264)
  1077. return 0;
  1078. if (atom.size == 16) {
  1079. original_size = codec->extradata_size;
  1080. ret = mov_realloc_extradata(codec, atom);
  1081. if (!ret) {
  1082. length = mov_read_atom_into_extradata(c, pb, atom, codec, codec->extradata + original_size);
  1083. if (length == atom.size) {
  1084. const uint8_t range_value = codec->extradata[original_size + 19];
  1085. switch (range_value) {
  1086. case 1:
  1087. codec->color_range = AVCOL_RANGE_MPEG;
  1088. break;
  1089. case 2:
  1090. codec->color_range = AVCOL_RANGE_JPEG;
  1091. break;
  1092. default:
  1093. av_log(c, AV_LOG_WARNING, "ignored unknown aclr value (%d)\n", range_value);
  1094. break;
  1095. }
  1096. av_dlog(c, "color_range: %d\n", codec->color_range);
  1097. } else {
  1098. /* For some reason the whole atom was not added to the extradata */
  1099. av_log(c, AV_LOG_ERROR, "aclr not decoded - incomplete atom\n");
  1100. }
  1101. } else {
  1102. av_log(c, AV_LOG_ERROR, "aclr not decoded - unable to add atom to extradata\n");
  1103. }
  1104. } else {
  1105. av_log(c, AV_LOG_WARNING, "aclr not decoded - unexpected size %"PRId64"\n", atom.size);
  1106. }
  1107. }
  1108. return ret;
  1109. }
  1110. static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1111. {
  1112. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
  1113. }
  1114. static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1115. {
  1116. AVStream *st;
  1117. if (c->fc->nb_streams < 1)
  1118. return 0;
  1119. st = c->fc->streams[c->fc->nb_streams-1];
  1120. if ((uint64_t)atom.size > (1<<30))
  1121. return AVERROR_INVALIDDATA;
  1122. if (st->codec->codec_id == AV_CODEC_ID_QDM2 ||
  1123. st->codec->codec_id == AV_CODEC_ID_QDMC ||
  1124. st->codec->codec_id == AV_CODEC_ID_SPEEX) {
  1125. // pass all frma atom to codec, needed at least for QDMC and QDM2
  1126. av_freep(&st->codec->extradata);
  1127. if (ff_get_extradata(st->codec, pb, atom.size) < 0)
  1128. return AVERROR(ENOMEM);
  1129. } else if (atom.size > 8) { /* to read frma, esds atoms */
  1130. int ret;
  1131. if ((ret = mov_read_default(c, pb, atom)) < 0)
  1132. return ret;
  1133. } else
  1134. avio_skip(pb, atom.size);
  1135. return 0;
  1136. }
  1137. /**
  1138. * This function reads atom content and puts data in extradata without tag
  1139. * nor size unlike mov_read_extradata.
  1140. */
  1141. static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1142. {
  1143. AVStream *st;
  1144. if (c->fc->nb_streams < 1)
  1145. return 0;
  1146. st = c->fc->streams[c->fc->nb_streams-1];
  1147. if ((uint64_t)atom.size > (1<<30))
  1148. return AVERROR_INVALIDDATA;
  1149. if (atom.size >= 10) {
  1150. // Broken files created by legacy versions of libavformat will
  1151. // wrap a whole fiel atom inside of a glbl atom.
  1152. unsigned size = avio_rb32(pb);
  1153. unsigned type = avio_rl32(pb);
  1154. avio_seek(pb, -8, SEEK_CUR);
  1155. if (type == MKTAG('f','i','e','l') && size == atom.size)
  1156. return mov_read_default(c, pb, atom);
  1157. }
  1158. if (st->codec->extradata_size > 1 && st->codec->extradata) {
  1159. av_log(c, AV_LOG_WARNING, "ignoring multiple glbl\n");
  1160. return 0;
  1161. }
  1162. av_freep(&st->codec->extradata);
  1163. if (ff_get_extradata(st->codec, pb, atom.size) < 0)
  1164. return AVERROR(ENOMEM);
  1165. return 0;
  1166. }
  1167. static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1168. {
  1169. AVStream *st;
  1170. uint8_t profile_level;
  1171. int ret;
  1172. if (c->fc->nb_streams < 1)
  1173. return 0;
  1174. st = c->fc->streams[c->fc->nb_streams-1];
  1175. if (atom.size >= (1<<28) || atom.size < 7)
  1176. return AVERROR_INVALIDDATA;
  1177. profile_level = avio_r8(pb);
  1178. if ((profile_level & 0xf0) != 0xc0)
  1179. return 0;
  1180. avio_seek(pb, 6, SEEK_CUR);
  1181. av_freep(&st->codec->extradata);
  1182. if ((ret = ff_get_extradata(st->codec, pb, atom.size - 7)) < 0)
  1183. return ret;
  1184. return 0;
  1185. }
  1186. /**
  1187. * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
  1188. * but can have extradata appended at the end after the 40 bytes belonging
  1189. * to the struct.
  1190. */
  1191. static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1192. {
  1193. AVStream *st;
  1194. if (c->fc->nb_streams < 1)
  1195. return 0;
  1196. if (atom.size <= 40)
  1197. return 0;
  1198. st = c->fc->streams[c->fc->nb_streams-1];
  1199. if ((uint64_t)atom.size > (1<<30))
  1200. return AVERROR_INVALIDDATA;
  1201. avio_skip(pb, 40);
  1202. av_freep(&st->codec->extradata);
  1203. if (ff_get_extradata(st->codec, pb, atom.size - 40) < 0)
  1204. return AVERROR(ENOMEM);
  1205. return 0;
  1206. }
  1207. static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1208. {
  1209. AVStream *st;
  1210. MOVStreamContext *sc;
  1211. unsigned int i, entries;
  1212. if (c->fc->nb_streams < 1)
  1213. return 0;
  1214. st = c->fc->streams[c->fc->nb_streams-1];
  1215. sc = st->priv_data;
  1216. avio_r8(pb); /* version */
  1217. avio_rb24(pb); /* flags */
  1218. entries = avio_rb32(pb);
  1219. if (!entries)
  1220. return 0;
  1221. if (sc->chunk_offsets)
  1222. av_log(c->fc, AV_LOG_WARNING, "Duplicated STCO atom\n");
  1223. av_free(sc->chunk_offsets);
  1224. sc->chunk_count = 0;
  1225. sc->chunk_offsets = av_malloc_array(entries, sizeof(*sc->chunk_offsets));
  1226. if (!sc->chunk_offsets)
  1227. return AVERROR(ENOMEM);
  1228. sc->chunk_count = entries;
  1229. if (atom.type == MKTAG('s','t','c','o'))
  1230. for (i = 0; i < entries && !pb->eof_reached; i++)
  1231. sc->chunk_offsets[i] = avio_rb32(pb);
  1232. else if (atom.type == MKTAG('c','o','6','4'))
  1233. for (i = 0; i < entries && !pb->eof_reached; i++)
  1234. sc->chunk_offsets[i] = avio_rb64(pb);
  1235. else
  1236. return AVERROR_INVALIDDATA;
  1237. sc->chunk_count = i;
  1238. if (pb->eof_reached)
  1239. return AVERROR_EOF;
  1240. return 0;
  1241. }
  1242. /**
  1243. * Compute codec id for 'lpcm' tag.
  1244. * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
  1245. */
  1246. enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
  1247. {
  1248. /* lpcm flags:
  1249. * 0x1 = float
  1250. * 0x2 = big-endian
  1251. * 0x4 = signed
  1252. */
  1253. return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0);
  1254. }
  1255. static int mov_codec_id(AVStream *st, uint32_t format)
  1256. {
  1257. int id = ff_codec_get_id(ff_codec_movaudio_tags, format);
  1258. if (id <= 0 &&
  1259. ((format & 0xFFFF) == 'm' + ('s' << 8) ||
  1260. (format & 0xFFFF) == 'T' + ('S' << 8)))
  1261. id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format) & 0xFFFF);
  1262. if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
  1263. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  1264. } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO &&
  1265. /* skip old asf mpeg4 tag */
  1266. format && format != MKTAG('m','p','4','s')) {
  1267. id = ff_codec_get_id(ff_codec_movvideo_tags, format);
  1268. if (id <= 0)
  1269. id = ff_codec_get_id(ff_codec_bmp_tags, format);
  1270. if (id > 0)
  1271. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  1272. else if (st->codec->codec_type == AVMEDIA_TYPE_DATA ||
  1273. (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE &&
  1274. st->codec->codec_id == AV_CODEC_ID_NONE)) {
  1275. id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
  1276. if (id > 0)
  1277. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  1278. }
  1279. }
  1280. st->codec->codec_tag = format;
  1281. return id;
  1282. }
  1283. static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb,
  1284. AVStream *st, MOVStreamContext *sc)
  1285. {
  1286. uint8_t codec_name[32];
  1287. unsigned int color_depth, len, j;
  1288. int color_greyscale;
  1289. int color_table_id;
  1290. avio_rb16(pb); /* version */
  1291. avio_rb16(pb); /* revision level */
  1292. avio_rb32(pb); /* vendor */
  1293. avio_rb32(pb); /* temporal quality */
  1294. avio_rb32(pb); /* spatial quality */
  1295. st->codec->width = avio_rb16(pb); /* width */
  1296. st->codec->height = avio_rb16(pb); /* height */
  1297. avio_rb32(pb); /* horiz resolution */
  1298. avio_rb32(pb); /* vert resolution */
  1299. avio_rb32(pb); /* data size, always 0 */
  1300. avio_rb16(pb); /* frames per samples */
  1301. len = avio_r8(pb); /* codec name, pascal string */
  1302. if (len > 31)
  1303. len = 31;
  1304. mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
  1305. if (len < 31)
  1306. avio_skip(pb, 31 - len);
  1307. if (codec_name[0])
  1308. av_dict_set(&st->metadata, "encoder", codec_name, 0);
  1309. /* codec_tag YV12 triggers an UV swap in rawdec.c */
  1310. if (!memcmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
  1311. st->codec->codec_tag = MKTAG('I', '4', '2', '0');
  1312. st->codec->width &= ~1;
  1313. st->codec->height &= ~1;
  1314. }
  1315. /* Flash Media Server uses tag H263 with Sorenson Spark */
  1316. if (st->codec->codec_tag == MKTAG('H','2','6','3') &&
  1317. !memcmp(codec_name, "Sorenson H263", 13))
  1318. st->codec->codec_id = AV_CODEC_ID_FLV1;
  1319. st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
  1320. color_table_id = avio_rb16(pb); /* colortable id */
  1321. av_log(c->fc, AV_LOG_TRACE, "depth %d, ctab id %d\n",
  1322. st->codec->bits_per_coded_sample, color_table_id);
  1323. /* figure out the palette situation */
  1324. color_depth = st->codec->bits_per_coded_sample & 0x1F;
  1325. color_greyscale = st->codec->bits_per_coded_sample & 0x20;
  1326. /* Do not create a greyscale palette for cinepak */
  1327. if (color_greyscale && st->codec->codec_id == AV_CODEC_ID_CINEPAK)
  1328. return;
  1329. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  1330. if ((color_depth == 2) || (color_depth == 4) || (color_depth == 8)) {
  1331. /* for palette traversal */
  1332. unsigned int color_start, color_count, color_end;
  1333. unsigned char a, r, g, b;
  1334. if (color_greyscale) {
  1335. int color_index, color_dec;
  1336. /* compute the greyscale palette */
  1337. st->codec->bits_per_coded_sample = color_depth;
  1338. color_count = 1 << color_depth;
  1339. color_index = 255;
  1340. color_dec = 256 / (color_count - 1);
  1341. for (j = 0; j < color_count; j++) {
  1342. r = g = b = color_index;
  1343. sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
  1344. color_index -= color_dec;
  1345. if (color_index < 0)
  1346. color_index = 0;
  1347. }
  1348. } else if (color_table_id) {
  1349. const uint8_t *color_table;
  1350. /* if flag bit 3 is set, use the default palette */
  1351. color_count = 1 << color_depth;
  1352. if (color_depth == 2)
  1353. color_table = ff_qt_default_palette_4;
  1354. else if (color_depth == 4)
  1355. color_table = ff_qt_default_palette_16;
  1356. else
  1357. color_table = ff_qt_default_palette_256;
  1358. for (j = 0; j < color_count; j++) {
  1359. r = color_table[j * 3 + 0];
  1360. g = color_table[j * 3 + 1];
  1361. b = color_table[j * 3 + 2];
  1362. sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
  1363. }
  1364. } else {
  1365. /* load the palette from the file */
  1366. color_start = avio_rb32(pb);
  1367. color_count = avio_rb16(pb);
  1368. color_end = avio_rb16(pb);
  1369. if ((color_start <= 255) && (color_end <= 255)) {
  1370. for (j = color_start; j <= color_end; j++) {
  1371. /* each A, R, G, or B component is 16 bits;
  1372. * only use the top 8 bits */
  1373. a = avio_r8(pb);
  1374. avio_r8(pb);
  1375. r = avio_r8(pb);
  1376. avio_r8(pb);
  1377. g = avio_r8(pb);
  1378. avio_r8(pb);
  1379. b = avio_r8(pb);
  1380. avio_r8(pb);
  1381. sc->palette[j] = (a << 24 ) | (r << 16) | (g << 8) | (b);
  1382. }
  1383. }
  1384. }
  1385. sc->has_palette = 1;
  1386. }
  1387. }
  1388. static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb,
  1389. AVStream *st, MOVStreamContext *sc)
  1390. {
  1391. int bits_per_sample, flags;
  1392. uint16_t version = avio_rb16(pb);
  1393. AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
  1394. avio_rb16(pb); /* revision level */
  1395. avio_rb32(pb); /* vendor */
  1396. st->codec->channels = avio_rb16(pb); /* channel count */
  1397. st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
  1398. av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", st->codec->channels);
  1399. sc->audio_cid = avio_rb16(pb);
  1400. avio_rb16(pb); /* packet size = 0 */
  1401. st->codec->sample_rate = ((avio_rb32(pb) >> 16));
  1402. // Read QT version 1 fields. In version 0 these do not exist.
  1403. av_log(c->fc, AV_LOG_TRACE, "version =%d, isom =%d\n", version, c->isom);
  1404. if (!c->isom ||
  1405. (compatible_brands && strstr(compatible_brands->value, "qt "))) {
  1406. if (version == 1) {
  1407. sc->samples_per_frame = avio_rb32(pb);
  1408. avio_rb32(pb); /* bytes per packet */
  1409. sc->bytes_per_frame = avio_rb32(pb);
  1410. avio_rb32(pb); /* bytes per sample */
  1411. } else if (version == 2) {
  1412. avio_rb32(pb); /* sizeof struct only */
  1413. st->codec->sample_rate = av_int2double(avio_rb64(pb));
  1414. st->codec->channels = avio_rb32(pb);
  1415. avio_rb32(pb); /* always 0x7F000000 */
  1416. st->codec->bits_per_coded_sample = avio_rb32(pb);
  1417. flags = avio_rb32(pb); /* lpcm format specific flag */
  1418. sc->bytes_per_frame = avio_rb32(pb);
  1419. sc->samples_per_frame = avio_rb32(pb);
  1420. if (st->codec->codec_tag == MKTAG('l','p','c','m'))
  1421. st->codec->codec_id =
  1422. ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample,
  1423. flags);
  1424. }
  1425. }
  1426. switch (st->codec->codec_id) {
  1427. case AV_CODEC_ID_PCM_S8:
  1428. case AV_CODEC_ID_PCM_U8:
  1429. if (st->codec->bits_per_coded_sample == 16)
  1430. st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
  1431. break;
  1432. case AV_CODEC_ID_PCM_S16LE:
  1433. case AV_CODEC_ID_PCM_S16BE:
  1434. if (st->codec->bits_per_coded_sample == 8)
  1435. st->codec->codec_id = AV_CODEC_ID_PCM_S8;
  1436. else if (st->codec->bits_per_coded_sample == 24)
  1437. st->codec->codec_id =
  1438. st->codec->codec_id == AV_CODEC_ID_PCM_S16BE ?
  1439. AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
  1440. else if (st->codec->bits_per_coded_sample == 32)
  1441. st->codec->codec_id =
  1442. st->codec->codec_id == AV_CODEC_ID_PCM_S16BE ?
  1443. AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
  1444. break;
  1445. /* set values for old format before stsd version 1 appeared */
  1446. case AV_CODEC_ID_MACE3:
  1447. sc->samples_per_frame = 6;
  1448. sc->bytes_per_frame = 2 * st->codec->channels;
  1449. break;
  1450. case AV_CODEC_ID_MACE6:
  1451. sc->samples_per_frame = 6;
  1452. sc->bytes_per_frame = 1 * st->codec->channels;
  1453. break;
  1454. case AV_CODEC_ID_ADPCM_IMA_QT:
  1455. sc->samples_per_frame = 64;
  1456. sc->bytes_per_frame = 34 * st->codec->channels;
  1457. break;
  1458. case AV_CODEC_ID_GSM:
  1459. sc->samples_per_frame = 160;
  1460. sc->bytes_per_frame = 33;
  1461. break;
  1462. default:
  1463. break;
  1464. }
  1465. bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
  1466. if (bits_per_sample) {
  1467. st->codec->bits_per_coded_sample = bits_per_sample;
  1468. sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
  1469. }
  1470. }
  1471. static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb,
  1472. AVStream *st, MOVStreamContext *sc,
  1473. int64_t size)
  1474. {
  1475. // ttxt stsd contains display flags, justification, background
  1476. // color, fonts, and default styles, so fake an atom to read it
  1477. MOVAtom fake_atom = { .size = size };
  1478. // mp4s contains a regular esds atom
  1479. if (st->codec->codec_tag != AV_RL32("mp4s"))
  1480. mov_read_glbl(c, pb, fake_atom);
  1481. st->codec->width = sc->width;
  1482. st->codec->height = sc->height;
  1483. }
  1484. static uint32_t yuv_to_rgba(uint32_t ycbcr)
  1485. {
  1486. uint8_t r, g, b;
  1487. int y, cb, cr;
  1488. y = (ycbcr >> 16) & 0xFF;
  1489. cr = (ycbcr >> 8) & 0xFF;
  1490. cb = ycbcr & 0xFF;
  1491. b = av_clip_uint8(1.164 * (y - 16) + 2.018 * (cb - 128));
  1492. g = av_clip_uint8(1.164 * (y - 16) - 0.813 * (cr - 128) - 0.391 * (cb - 128));
  1493. r = av_clip_uint8(1.164 * (y - 16) + 1.596 * (cr - 128));
  1494. return (r << 16) | (g << 8) | b;
  1495. }
  1496. static int mov_rewrite_dvd_sub_extradata(AVStream *st)
  1497. {
  1498. char buf[256] = {0};
  1499. uint8_t *src = st->codec->extradata;
  1500. int i;
  1501. if (st->codec->extradata_size != 64)
  1502. return 0;
  1503. if (st->codec->width > 0 && st->codec->height > 0)
  1504. snprintf(buf, sizeof(buf), "size: %dx%d\n",
  1505. st->codec->width, st->codec->height);
  1506. av_strlcat(buf, "palette: ", sizeof(buf));
  1507. for (i = 0; i < 16; i++) {
  1508. uint32_t yuv = AV_RB32(src + i * 4);
  1509. uint32_t rgba = yuv_to_rgba(yuv);
  1510. av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : "");
  1511. }
  1512. if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf))
  1513. return 0;
  1514. av_freep(&st->codec->extradata);
  1515. st->codec->extradata_size = 0;
  1516. st->codec->extradata = av_mallocz(strlen(buf) + FF_INPUT_BUFFER_PADDING_SIZE);
  1517. if (!st->codec->extradata)
  1518. return AVERROR(ENOMEM);
  1519. st->codec->extradata_size = strlen(buf);
  1520. memcpy(st->codec->extradata, buf, st->codec->extradata_size);
  1521. return 0;
  1522. }
  1523. static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb,
  1524. AVStream *st, MOVStreamContext *sc,
  1525. int64_t size)
  1526. {
  1527. if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
  1528. if ((int)size != size || ff_get_extradata(st->codec, pb, size) < 0)
  1529. return AVERROR(ENOMEM);
  1530. if (size > 16) {
  1531. MOVStreamContext *tmcd_ctx = st->priv_data;
  1532. int val;
  1533. val = AV_RB32(st->codec->extradata + 4);
  1534. tmcd_ctx->tmcd_flags = val;
  1535. if (val & 1)
  1536. st->codec->flags2 |= CODEC_FLAG2_DROP_FRAME_TIMECODE;
  1537. st->codec->time_base.den = st->codec->extradata[16]; /* number of frame */
  1538. st->codec->time_base.num = 1;
  1539. /* adjust for per frame dur in counter mode */
  1540. if (tmcd_ctx->tmcd_flags & 0x0008) {
  1541. int timescale = AV_RB32(st->codec->extradata + 8);
  1542. int framedur = AV_RB32(st->codec->extradata + 12);
  1543. st->codec->time_base.den *= timescale;
  1544. st->codec->time_base.num *= framedur;
  1545. }
  1546. if (size > 30) {
  1547. uint32_t len = AV_RB32(st->codec->extradata + 18); /* name atom length */
  1548. uint32_t format = AV_RB32(st->codec->extradata + 22);
  1549. if (format == AV_RB32("name") && (int64_t)size >= (int64_t)len + 18) {
  1550. uint16_t str_size = AV_RB16(st->codec->extradata + 26); /* string length */
  1551. if (str_size > 0 && size >= (int)str_size + 26) {
  1552. char *reel_name = av_malloc(str_size + 1);
  1553. if (!reel_name)
  1554. return AVERROR(ENOMEM);
  1555. memcpy(reel_name, st->codec->extradata + 30, str_size);
  1556. reel_name[str_size] = 0; /* Add null terminator */
  1557. /* don't add reel_name if emtpy string */
  1558. if (*reel_name == 0) {
  1559. av_free(reel_name);
  1560. } else {
  1561. av_dict_set(&st->metadata, "reel_name", reel_name, AV_DICT_DONT_STRDUP_VAL);
  1562. }
  1563. }
  1564. }
  1565. }
  1566. }
  1567. } else {
  1568. /* other codec type, just skip (rtp, mp4s ...) */
  1569. avio_skip(pb, size);
  1570. }
  1571. return 0;
  1572. }
  1573. static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb,
  1574. AVStream *st, MOVStreamContext *sc)
  1575. {
  1576. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  1577. !st->codec->sample_rate && sc->time_scale > 1)
  1578. st->codec->sample_rate = sc->time_scale;
  1579. /* special codec parameters handling */
  1580. switch (st->codec->codec_id) {
  1581. #if CONFIG_DV_DEMUXER
  1582. case AV_CODEC_ID_DVAUDIO:
  1583. c->dv_fctx = avformat_alloc_context();
  1584. c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
  1585. if (!c->dv_demux) {
  1586. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  1587. return AVERROR(ENOMEM);
  1588. }
  1589. sc->dv_audio_container = 1;
  1590. st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  1591. break;
  1592. #endif
  1593. /* no ifdef since parameters are always those */
  1594. case AV_CODEC_ID_QCELP:
  1595. st->codec->channels = 1;
  1596. // force sample rate for qcelp when not stored in mov
  1597. if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
  1598. st->codec->sample_rate = 8000;
  1599. // FIXME: Why is the following needed for some files?
  1600. sc->samples_per_frame = 160;
  1601. if (!sc->bytes_per_frame)
  1602. sc->bytes_per_frame = 35;
  1603. break;
  1604. case AV_CODEC_ID_AMR_NB:
  1605. st->codec->channels = 1;
  1606. /* force sample rate for amr, stsd in 3gp does not store sample rate */
  1607. st->codec->sample_rate = 8000;
  1608. break;
  1609. case AV_CODEC_ID_AMR_WB:
  1610. st->codec->channels = 1;
  1611. st->codec->sample_rate = 16000;
  1612. break;
  1613. case AV_CODEC_ID_MP2:
  1614. case AV_CODEC_ID_MP3:
  1615. /* force type after stsd for m1a hdlr */
  1616. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  1617. st->need_parsing = AVSTREAM_PARSE_FULL;
  1618. break;
  1619. case AV_CODEC_ID_GSM:
  1620. case AV_CODEC_ID_ADPCM_MS:
  1621. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1622. case AV_CODEC_ID_ILBC:
  1623. case AV_CODEC_ID_MACE3:
  1624. case AV_CODEC_ID_MACE6:
  1625. case AV_CODEC_ID_QDM2:
  1626. st->codec->block_align = sc->bytes_per_frame;
  1627. break;
  1628. case AV_CODEC_ID_ALAC:
  1629. if (st->codec->extradata_size == 36) {
  1630. st->codec->channels = AV_RB8 (st->codec->extradata + 21);
  1631. st->codec->sample_rate = AV_RB32(st->codec->extradata + 32);
  1632. }
  1633. break;
  1634. case AV_CODEC_ID_AC3:
  1635. case AV_CODEC_ID_EAC3:
  1636. case AV_CODEC_ID_MPEG1VIDEO:
  1637. case AV_CODEC_ID_VC1:
  1638. st->need_parsing = AVSTREAM_PARSE_FULL;
  1639. break;
  1640. default:
  1641. break;
  1642. }
  1643. return 0;
  1644. }
  1645. static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb,
  1646. int codec_tag, int format,
  1647. int64_t size)
  1648. {
  1649. int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format);
  1650. if (codec_tag &&
  1651. (codec_tag != format &&
  1652. (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id
  1653. : codec_tag != MKTAG('j','p','e','g')))) {
  1654. /* Multiple fourcc, we skip JPEG. This is not correct, we should
  1655. * export it as a separate AVStream but this needs a few changes
  1656. * in the MOV demuxer, patch welcome. */
  1657. av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
  1658. avio_skip(pb, size);
  1659. return 1;
  1660. }
  1661. if ( codec_tag == AV_RL32("avc1") ||
  1662. codec_tag == AV_RL32("hvc1") ||
  1663. codec_tag == AV_RL32("hev1")
  1664. )
  1665. av_log(c->fc, AV_LOG_WARNING, "Concatenated H.264 or H.265 might not play correctly.\n");
  1666. return 0;
  1667. }
  1668. int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
  1669. {
  1670. AVStream *st;
  1671. MOVStreamContext *sc;
  1672. int pseudo_stream_id;
  1673. if (c->fc->nb_streams < 1)
  1674. return 0;
  1675. st = c->fc->streams[c->fc->nb_streams-1];
  1676. sc = st->priv_data;
  1677. for (pseudo_stream_id = 0;
  1678. pseudo_stream_id < entries && !pb->eof_reached;
  1679. pseudo_stream_id++) {
  1680. //Parsing Sample description table
  1681. enum AVCodecID id;
  1682. int ret, dref_id = 1;
  1683. MOVAtom a = { AV_RL32("stsd") };
  1684. int64_t start_pos = avio_tell(pb);
  1685. int64_t size = avio_rb32(pb); /* size */
  1686. uint32_t format = avio_rl32(pb); /* data format */
  1687. if (size >= 16) {
  1688. avio_rb32(pb); /* reserved */
  1689. avio_rb16(pb); /* reserved */
  1690. dref_id = avio_rb16(pb);
  1691. }else if (size <= 7){
  1692. av_log(c->fc, AV_LOG_ERROR, "invalid size %"PRId64" in stsd\n", size);
  1693. return AVERROR_INVALIDDATA;
  1694. }
  1695. if (mov_skip_multiple_stsd(c, pb, st->codec->codec_tag, format,
  1696. size - (avio_tell(pb) - start_pos)))
  1697. continue;
  1698. sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
  1699. sc->dref_id= dref_id;
  1700. id = mov_codec_id(st, format);
  1701. av_log(c->fc, AV_LOG_TRACE, "size=%"PRId64" 4CC= %c%c%c%c codec_type=%d\n", size,
  1702. (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
  1703. (format >> 24) & 0xff, st->codec->codec_type);
  1704. if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
  1705. st->codec->codec_id = id;
  1706. mov_parse_stsd_video(c, pb, st, sc);
  1707. } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
  1708. st->codec->codec_id = id;
  1709. mov_parse_stsd_audio(c, pb, st, sc);
  1710. } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
  1711. st->codec->codec_id = id;
  1712. mov_parse_stsd_subtitle(c, pb, st, sc,
  1713. size - (avio_tell(pb) - start_pos));
  1714. } else {
  1715. ret = mov_parse_stsd_data(c, pb, st, sc,
  1716. size - (avio_tell(pb) - start_pos));
  1717. if (ret < 0)
  1718. return ret;
  1719. }
  1720. /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
  1721. a.size = size - (avio_tell(pb) - start_pos);
  1722. if (a.size > 8) {
  1723. if ((ret = mov_read_default(c, pb, a)) < 0)
  1724. return ret;
  1725. } else if (a.size > 0)
  1726. avio_skip(pb, a.size);
  1727. }
  1728. if (pb->eof_reached)
  1729. return AVERROR_EOF;
  1730. return mov_finalize_stsd_codec(c, pb, st, sc);
  1731. }
  1732. static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1733. {
  1734. int entries;
  1735. avio_r8(pb); /* version */
  1736. avio_rb24(pb); /* flags */
  1737. entries = avio_rb32(pb);
  1738. return ff_mov_read_stsd_entries(c, pb, entries);
  1739. }
  1740. static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1741. {
  1742. AVStream *st;
  1743. MOVStreamContext *sc;
  1744. unsigned int i, entries;
  1745. if (c->fc->nb_streams < 1)
  1746. return 0;
  1747. st = c->fc->streams[c->fc->nb_streams-1];
  1748. sc = st->priv_data;
  1749. avio_r8(pb); /* version */
  1750. avio_rb24(pb); /* flags */
  1751. entries = avio_rb32(pb);
  1752. av_log(c->fc, AV_LOG_TRACE, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  1753. if (!entries)
  1754. return 0;
  1755. if (sc->stsc_data)
  1756. av_log(c->fc, AV_LOG_WARNING, "Duplicated STSC atom\n");
  1757. av_free(sc->stsc_data);
  1758. sc->stsc_count = 0;
  1759. sc->stsc_data = av_malloc_array(entries, sizeof(*sc->stsc_data));
  1760. if (!sc->stsc_data)
  1761. return AVERROR(ENOMEM);
  1762. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1763. sc->stsc_data[i].first = avio_rb32(pb);
  1764. sc->stsc_data[i].count = avio_rb32(pb);
  1765. sc->stsc_data[i].id = avio_rb32(pb);
  1766. }
  1767. sc->stsc_count = i;
  1768. if (pb->eof_reached)
  1769. return AVERROR_EOF;
  1770. return 0;
  1771. }
  1772. static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1773. {
  1774. AVStream *st;
  1775. MOVStreamContext *sc;
  1776. unsigned i, entries;
  1777. if (c->fc->nb_streams < 1)
  1778. return 0;
  1779. st = c->fc->streams[c->fc->nb_streams-1];
  1780. sc = st->priv_data;
  1781. avio_rb32(pb); // version + flags
  1782. entries = avio_rb32(pb);
  1783. if (sc->stps_data)
  1784. av_log(c->fc, AV_LOG_WARNING, "Duplicated STPS atom\n");
  1785. av_free(sc->stps_data);
  1786. sc->stps_count = 0;
  1787. sc->stps_data = av_malloc_array(entries, sizeof(*sc->stps_data));
  1788. if (!sc->stps_data)
  1789. return AVERROR(ENOMEM);
  1790. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1791. sc->stps_data[i] = avio_rb32(pb);
  1792. //av_log(c->fc, AV_LOG_TRACE, "stps %d\n", sc->stps_data[i]);
  1793. }
  1794. sc->stps_count = i;
  1795. if (pb->eof_reached)
  1796. return AVERROR_EOF;
  1797. return 0;
  1798. }
  1799. static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1800. {
  1801. AVStream *st;
  1802. MOVStreamContext *sc;
  1803. unsigned int i, entries;
  1804. if (c->fc->nb_streams < 1)
  1805. return 0;
  1806. st = c->fc->streams[c->fc->nb_streams-1];
  1807. sc = st->priv_data;
  1808. avio_r8(pb); /* version */
  1809. avio_rb24(pb); /* flags */
  1810. entries = avio_rb32(pb);
  1811. av_log(c->fc, AV_LOG_TRACE, "keyframe_count = %d\n", entries);
  1812. if (!entries)
  1813. {
  1814. sc->keyframe_absent = 1;
  1815. if (!st->need_parsing && st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  1816. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  1817. return 0;
  1818. }
  1819. if (sc->keyframes)
  1820. av_log(c->fc, AV_LOG_WARNING, "Duplicated STSS atom\n");
  1821. if (entries >= UINT_MAX / sizeof(int))
  1822. return AVERROR_INVALIDDATA;
  1823. av_freep(&sc->keyframes);
  1824. sc->keyframe_count = 0;
  1825. sc->keyframes = av_malloc_array(entries, sizeof(*sc->keyframes));
  1826. if (!sc->keyframes)
  1827. return AVERROR(ENOMEM);
  1828. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1829. sc->keyframes[i] = avio_rb32(pb);
  1830. //av_log(c->fc, AV_LOG_TRACE, "keyframes[]=%d\n", sc->keyframes[i]);
  1831. }
  1832. sc->keyframe_count = i;
  1833. if (pb->eof_reached)
  1834. return AVERROR_EOF;
  1835. return 0;
  1836. }
  1837. static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1838. {
  1839. AVStream *st;
  1840. MOVStreamContext *sc;
  1841. unsigned int i, entries, sample_size, field_size, num_bytes;
  1842. GetBitContext gb;
  1843. unsigned char* buf;
  1844. if (c->fc->nb_streams < 1)
  1845. return 0;
  1846. st = c->fc->streams[c->fc->nb_streams-1];
  1847. sc = st->priv_data;
  1848. avio_r8(pb); /* version */
  1849. avio_rb24(pb); /* flags */
  1850. if (atom.type == MKTAG('s','t','s','z')) {
  1851. sample_size = avio_rb32(pb);
  1852. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  1853. sc->sample_size = sample_size;
  1854. sc->stsz_sample_size = sample_size;
  1855. field_size = 32;
  1856. } else {
  1857. sample_size = 0;
  1858. avio_rb24(pb); /* reserved */
  1859. field_size = avio_r8(pb);
  1860. }
  1861. entries = avio_rb32(pb);
  1862. av_log(c->fc, AV_LOG_TRACE, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
  1863. sc->sample_count = entries;
  1864. if (sample_size)
  1865. return 0;
  1866. if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
  1867. av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
  1868. return AVERROR_INVALIDDATA;
  1869. }
  1870. if (!entries)
  1871. return 0;
  1872. if (entries >= (UINT_MAX - 4) / field_size)
  1873. return AVERROR_INVALIDDATA;
  1874. if (sc->sample_sizes)
  1875. av_log(c->fc, AV_LOG_WARNING, "Duplicated STSZ atom\n");
  1876. av_free(sc->sample_sizes);
  1877. sc->sample_count = 0;
  1878. sc->sample_sizes = av_malloc_array(entries, sizeof(*sc->sample_sizes));
  1879. if (!sc->sample_sizes)
  1880. return AVERROR(ENOMEM);
  1881. num_bytes = (entries*field_size+4)>>3;
  1882. buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
  1883. if (!buf) {
  1884. av_freep(&sc->sample_sizes);
  1885. return AVERROR(ENOMEM);
  1886. }
  1887. if (avio_read(pb, buf, num_bytes) < num_bytes) {
  1888. av_freep(&sc->sample_sizes);
  1889. av_free(buf);
  1890. return AVERROR_INVALIDDATA;
  1891. }
  1892. init_get_bits(&gb, buf, 8*num_bytes);
  1893. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1894. sc->sample_sizes[i] = get_bits_long(&gb, field_size);
  1895. sc->data_size += sc->sample_sizes[i];
  1896. }
  1897. sc->sample_count = i;
  1898. av_free(buf);
  1899. if (pb->eof_reached)
  1900. return AVERROR_EOF;
  1901. return 0;
  1902. }
  1903. static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1904. {
  1905. AVStream *st;
  1906. MOVStreamContext *sc;
  1907. unsigned int i, entries;
  1908. int64_t duration=0;
  1909. int64_t total_sample_count=0;
  1910. if (c->fc->nb_streams < 1)
  1911. return 0;
  1912. st = c->fc->streams[c->fc->nb_streams-1];
  1913. sc = st->priv_data;
  1914. avio_r8(pb); /* version */
  1915. avio_rb24(pb); /* flags */
  1916. entries = avio_rb32(pb);
  1917. av_log(c->fc, AV_LOG_TRACE, "track[%i].stts.entries = %i\n",
  1918. c->fc->nb_streams-1, entries);
  1919. if (sc->stts_data)
  1920. av_log(c->fc, AV_LOG_WARNING, "Duplicated STTS atom\n");
  1921. av_free(sc->stts_data);
  1922. sc->stts_count = 0;
  1923. sc->stts_data = av_malloc_array(entries, sizeof(*sc->stts_data));
  1924. if (!sc->stts_data)
  1925. return AVERROR(ENOMEM);
  1926. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1927. int sample_duration;
  1928. int sample_count;
  1929. sample_count=avio_rb32(pb);
  1930. sample_duration = avio_rb32(pb);
  1931. /* sample_duration < 0 is invalid based on the spec */
  1932. if (sample_duration < 0) {
  1933. av_log(c->fc, AV_LOG_ERROR, "Invalid SampleDelta %d in STTS, at %d st:%d\n",
  1934. sample_duration, i, c->fc->nb_streams-1);
  1935. sample_duration = 1;
  1936. }
  1937. if (sample_count < 0) {
  1938. av_log(c->fc, AV_LOG_ERROR, "Invalid sample_count=%d\n", sample_count);
  1939. return AVERROR_INVALIDDATA;
  1940. }
  1941. sc->stts_data[i].count= sample_count;
  1942. sc->stts_data[i].duration= sample_duration;
  1943. av_log(c->fc, AV_LOG_TRACE, "sample_count=%d, sample_duration=%d\n",
  1944. sample_count, sample_duration);
  1945. if ( i+1 == entries
  1946. && i
  1947. && sample_count == 1
  1948. && total_sample_count > 100
  1949. && sample_duration/10 > duration / total_sample_count)
  1950. sample_duration = duration / total_sample_count;
  1951. duration+=(int64_t)sample_duration*sample_count;
  1952. total_sample_count+=sample_count;
  1953. }
  1954. sc->stts_count = i;
  1955. sc->duration_for_fps += duration;
  1956. sc->nb_frames_for_fps += total_sample_count;
  1957. if (pb->eof_reached)
  1958. return AVERROR_EOF;
  1959. st->nb_frames= total_sample_count;
  1960. if (duration)
  1961. st->duration= duration;
  1962. sc->track_end = duration;
  1963. return 0;
  1964. }
  1965. static void mov_update_dts_shift(MOVStreamContext *sc, int duration)
  1966. {
  1967. if (duration < 0) {
  1968. sc->dts_shift = FFMAX(sc->dts_shift, -duration);
  1969. }
  1970. }
  1971. static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1972. {
  1973. AVStream *st;
  1974. MOVStreamContext *sc;
  1975. unsigned int i, entries;
  1976. if (c->fc->nb_streams < 1)
  1977. return 0;
  1978. st = c->fc->streams[c->fc->nb_streams-1];
  1979. sc = st->priv_data;
  1980. avio_r8(pb); /* version */
  1981. avio_rb24(pb); /* flags */
  1982. entries = avio_rb32(pb);
  1983. av_log(c->fc, AV_LOG_TRACE, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1984. if (!entries)
  1985. return 0;
  1986. if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
  1987. return AVERROR_INVALIDDATA;
  1988. av_freep(&sc->ctts_data);
  1989. sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
  1990. if (!sc->ctts_data)
  1991. return AVERROR(ENOMEM);
  1992. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1993. int count =avio_rb32(pb);
  1994. int duration =avio_rb32(pb);
  1995. sc->ctts_data[i].count = count;
  1996. sc->ctts_data[i].duration= duration;
  1997. av_log(c->fc, AV_LOG_TRACE, "count=%d, duration=%d\n",
  1998. count, duration);
  1999. if (FFABS(duration) > (1<<28) && i+2<entries) {
  2000. av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
  2001. av_freep(&sc->ctts_data);
  2002. sc->ctts_count = 0;
  2003. return 0;
  2004. }
  2005. if (i+2<entries)
  2006. mov_update_dts_shift(sc, duration);
  2007. }
  2008. sc->ctts_count = i;
  2009. if (pb->eof_reached)
  2010. return AVERROR_EOF;
  2011. av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift);
  2012. return 0;
  2013. }
  2014. static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2015. {
  2016. AVStream *st;
  2017. MOVStreamContext *sc;
  2018. unsigned int i, entries;
  2019. uint8_t version;
  2020. uint32_t grouping_type;
  2021. if (c->fc->nb_streams < 1)
  2022. return 0;
  2023. st = c->fc->streams[c->fc->nb_streams-1];
  2024. sc = st->priv_data;
  2025. version = avio_r8(pb); /* version */
  2026. avio_rb24(pb); /* flags */
  2027. grouping_type = avio_rl32(pb);
  2028. if (grouping_type != MKTAG( 'r','a','p',' '))
  2029. return 0; /* only support 'rap ' grouping */
  2030. if (version == 1)
  2031. avio_rb32(pb); /* grouping_type_parameter */
  2032. entries = avio_rb32(pb);
  2033. if (!entries)
  2034. return 0;
  2035. if (sc->rap_group)
  2036. av_log(c->fc, AV_LOG_WARNING, "Duplicated SBGP atom\n");
  2037. av_free(sc->rap_group);
  2038. sc->rap_group_count = 0;
  2039. sc->rap_group = av_malloc_array(entries, sizeof(*sc->rap_group));
  2040. if (!sc->rap_group)
  2041. return AVERROR(ENOMEM);
  2042. for (i = 0; i < entries && !pb->eof_reached; i++) {
  2043. sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
  2044. sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
  2045. }
  2046. sc->rap_group_count = i;
  2047. return pb->eof_reached ? AVERROR_EOF : 0;
  2048. }
  2049. static void mov_build_index(MOVContext *mov, AVStream *st)
  2050. {
  2051. MOVStreamContext *sc = st->priv_data;
  2052. int64_t current_offset;
  2053. int64_t current_dts = 0;
  2054. unsigned int stts_index = 0;
  2055. unsigned int stsc_index = 0;
  2056. unsigned int stss_index = 0;
  2057. unsigned int stps_index = 0;
  2058. unsigned int i, j;
  2059. uint64_t stream_size = 0;
  2060. if (sc->elst_count) {
  2061. int i, edit_start_index = 0, unsupported = 0;
  2062. int64_t empty_duration = 0; // empty duration of the first edit list entry
  2063. int64_t start_time = 0; // start time of the media
  2064. for (i = 0; i < sc->elst_count; i++) {
  2065. const MOVElst *e = &sc->elst_data[i];
  2066. if (i == 0 && e->time == -1) {
  2067. /* if empty, the first entry is the start time of the stream
  2068. * relative to the presentation itself */
  2069. empty_duration = e->duration;
  2070. edit_start_index = 1;
  2071. } else if (i == edit_start_index && e->time >= 0) {
  2072. start_time = e->time;
  2073. } else
  2074. unsupported = 1;
  2075. }
  2076. if (unsupported)
  2077. av_log(mov->fc, AV_LOG_WARNING, "multiple edit list entries, "
  2078. "a/v desync might occur, patch welcome\n");
  2079. /* adjust first dts according to edit list */
  2080. if ((empty_duration || start_time) && mov->time_scale > 0) {
  2081. if (empty_duration)
  2082. empty_duration = av_rescale(empty_duration, sc->time_scale, mov->time_scale);
  2083. sc->time_offset = start_time - empty_duration;
  2084. current_dts = -sc->time_offset;
  2085. if (sc->ctts_count>0 && sc->stts_count>0 &&
  2086. sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
  2087. /* more than 16 frames delay, dts are likely wrong
  2088. this happens with files created by iMovie */
  2089. sc->wrong_dts = 1;
  2090. st->codec->has_b_frames = 1;
  2091. }
  2092. }
  2093. }
  2094. /* only use old uncompressed audio chunk demuxing when stts specifies it */
  2095. if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  2096. sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
  2097. unsigned int current_sample = 0;
  2098. unsigned int stts_sample = 0;
  2099. unsigned int sample_size;
  2100. unsigned int distance = 0;
  2101. unsigned int rap_group_index = 0;
  2102. unsigned int rap_group_sample = 0;
  2103. int rap_group_present = sc->rap_group_count && sc->rap_group;
  2104. int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
  2105. current_dts -= sc->dts_shift;
  2106. if (!sc->sample_count || st->nb_index_entries)
  2107. return;
  2108. if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
  2109. return;
  2110. if (av_reallocp_array(&st->index_entries,
  2111. st->nb_index_entries + sc->sample_count,
  2112. sizeof(*st->index_entries)) < 0) {
  2113. st->nb_index_entries = 0;
  2114. return;
  2115. }
  2116. st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries);
  2117. for (i = 0; i < sc->chunk_count; i++) {
  2118. int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
  2119. current_offset = sc->chunk_offsets[i];
  2120. while (stsc_index + 1 < sc->stsc_count &&
  2121. i + 1 == sc->stsc_data[stsc_index + 1].first)
  2122. stsc_index++;
  2123. if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
  2124. sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
  2125. av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
  2126. sc->stsz_sample_size = sc->sample_size;
  2127. }
  2128. if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
  2129. av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
  2130. sc->stsz_sample_size = sc->sample_size;
  2131. }
  2132. for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
  2133. int keyframe = 0;
  2134. if (current_sample >= sc->sample_count) {
  2135. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  2136. return;
  2137. }
  2138. if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
  2139. keyframe = 1;
  2140. if (stss_index + 1 < sc->keyframe_count)
  2141. stss_index++;
  2142. } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
  2143. keyframe = 1;
  2144. if (stps_index + 1 < sc->stps_count)
  2145. stps_index++;
  2146. }
  2147. if (rap_group_present && rap_group_index < sc->rap_group_count) {
  2148. if (sc->rap_group[rap_group_index].index > 0)
  2149. keyframe = 1;
  2150. if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
  2151. rap_group_sample = 0;
  2152. rap_group_index++;
  2153. }
  2154. }
  2155. if (sc->keyframe_absent
  2156. && !sc->stps_count
  2157. && !rap_group_present
  2158. && (st->codec->codec_type == AVMEDIA_TYPE_AUDIO || (i==0 && j==0)))
  2159. keyframe = 1;
  2160. if (keyframe)
  2161. distance = 0;
  2162. sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
  2163. if (sc->pseudo_stream_id == -1 ||
  2164. sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
  2165. AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
  2166. e->pos = current_offset;
  2167. e->timestamp = current_dts;
  2168. e->size = sample_size;
  2169. e->min_distance = distance;
  2170. e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
  2171. av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  2172. "size %d, distance %d, keyframe %d\n", st->index, current_sample,
  2173. current_offset, current_dts, sample_size, distance, keyframe);
  2174. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->nb_index_entries < 100)
  2175. ff_rfps_add_frame(mov->fc, st, current_dts);
  2176. }
  2177. current_offset += sample_size;
  2178. stream_size += sample_size;
  2179. current_dts += sc->stts_data[stts_index].duration;
  2180. distance++;
  2181. stts_sample++;
  2182. current_sample++;
  2183. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  2184. stts_sample = 0;
  2185. stts_index++;
  2186. }
  2187. }
  2188. }
  2189. if (st->duration > 0)
  2190. st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
  2191. } else {
  2192. unsigned chunk_samples, total = 0;
  2193. // compute total chunk count
  2194. for (i = 0; i < sc->stsc_count; i++) {
  2195. unsigned count, chunk_count;
  2196. chunk_samples = sc->stsc_data[i].count;
  2197. if (i != sc->stsc_count - 1 &&
  2198. sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
  2199. av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
  2200. return;
  2201. }
  2202. if (sc->samples_per_frame >= 160) { // gsm
  2203. count = chunk_samples / sc->samples_per_frame;
  2204. } else if (sc->samples_per_frame > 1) {
  2205. unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
  2206. count = (chunk_samples+samples-1) / samples;
  2207. } else {
  2208. count = (chunk_samples+1023) / 1024;
  2209. }
  2210. if (i < sc->stsc_count - 1)
  2211. chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
  2212. else
  2213. chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
  2214. total += chunk_count * count;
  2215. }
  2216. av_log(mov->fc, AV_LOG_TRACE, "chunk count %d\n", total);
  2217. if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
  2218. return;
  2219. if (av_reallocp_array(&st->index_entries,
  2220. st->nb_index_entries + total,
  2221. sizeof(*st->index_entries)) < 0) {
  2222. st->nb_index_entries = 0;
  2223. return;
  2224. }
  2225. st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
  2226. // populate index
  2227. for (i = 0; i < sc->chunk_count; i++) {
  2228. current_offset = sc->chunk_offsets[i];
  2229. if (stsc_index + 1 < sc->stsc_count &&
  2230. i + 1 == sc->stsc_data[stsc_index + 1].first)
  2231. stsc_index++;
  2232. chunk_samples = sc->stsc_data[stsc_index].count;
  2233. while (chunk_samples > 0) {
  2234. AVIndexEntry *e;
  2235. unsigned size, samples;
  2236. if (sc->samples_per_frame >= 160) { // gsm
  2237. samples = sc->samples_per_frame;
  2238. size = sc->bytes_per_frame;
  2239. } else {
  2240. if (sc->samples_per_frame > 1) {
  2241. samples = FFMIN((1024 / sc->samples_per_frame)*
  2242. sc->samples_per_frame, chunk_samples);
  2243. size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
  2244. } else {
  2245. samples = FFMIN(1024, chunk_samples);
  2246. size = samples * sc->sample_size;
  2247. }
  2248. }
  2249. if (st->nb_index_entries >= total) {
  2250. av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
  2251. return;
  2252. }
  2253. e = &st->index_entries[st->nb_index_entries++];
  2254. e->pos = current_offset;
  2255. e->timestamp = current_dts;
  2256. e->size = size;
  2257. e->min_distance = 0;
  2258. e->flags = AVINDEX_KEYFRAME;
  2259. av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
  2260. "size %d, duration %d\n", st->index, i, current_offset, current_dts,
  2261. size, samples);
  2262. current_offset += size;
  2263. current_dts += samples;
  2264. chunk_samples -= samples;
  2265. }
  2266. }
  2267. }
  2268. }
  2269. static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref,
  2270. AVIOInterruptCB *int_cb)
  2271. {
  2272. AVOpenCallback open_func = c->fc->open_cb;
  2273. if (!open_func)
  2274. open_func = ffio_open2_wrapper;
  2275. /* try relative path, we do not try the absolute because it can leak information about our
  2276. system to an attacker */
  2277. if (ref->nlvl_to > 0 && ref->nlvl_from > 0 && ref->path[0] != '/') {
  2278. char filename[1025];
  2279. const char *src_path;
  2280. int i, l;
  2281. /* find a source dir */
  2282. src_path = strrchr(src, '/');
  2283. if (src_path)
  2284. src_path++;
  2285. else
  2286. src_path = src;
  2287. /* find a next level down to target */
  2288. for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
  2289. if (ref->path[l] == '/') {
  2290. if (i == ref->nlvl_to - 1)
  2291. break;
  2292. else
  2293. i++;
  2294. }
  2295. /* compose filename if next level down to target was found */
  2296. if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
  2297. memcpy(filename, src, src_path - src);
  2298. filename[src_path - src] = 0;
  2299. for (i = 1; i < ref->nlvl_from; i++)
  2300. av_strlcat(filename, "../", sizeof(filename));
  2301. av_strlcat(filename, ref->path + l + 1, sizeof(filename));
  2302. if (!c->use_absolute_path && !c->fc->open_cb)
  2303. if(strstr(ref->path + l + 1, "..") || ref->nlvl_from > 1)
  2304. return AVERROR(ENOENT);
  2305. if (strlen(filename) + 1 == sizeof(filename))
  2306. return AVERROR(ENOENT);
  2307. if (!open_func(c->fc, pb, filename, AVIO_FLAG_READ, int_cb, NULL))
  2308. return 0;
  2309. }
  2310. } else if (c->use_absolute_path) {
  2311. av_log(c->fc, AV_LOG_WARNING, "Using absolute path on user request, "
  2312. "this is a possible security issue\n");
  2313. if (!open_func(c->fc, pb, ref->path, AVIO_FLAG_READ, int_cb, NULL))
  2314. return 0;
  2315. } else if (c->fc->open_cb) {
  2316. if (!open_func(c->fc, pb, ref->path, AVIO_FLAG_READ, int_cb, NULL))
  2317. return 0;
  2318. } else {
  2319. av_log(c->fc, AV_LOG_ERROR,
  2320. "Absolute path %s not tried for security reasons, "
  2321. "set demuxer option use_absolute_path to allow absolute paths\n",
  2322. ref->path);
  2323. }
  2324. return AVERROR(ENOENT);
  2325. }
  2326. static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
  2327. {
  2328. if (sc->time_scale <= 0) {
  2329. av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
  2330. sc->time_scale = c->time_scale;
  2331. if (sc->time_scale <= 0)
  2332. sc->time_scale = 1;
  2333. }
  2334. }
  2335. static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2336. {
  2337. AVStream *st;
  2338. MOVStreamContext *sc;
  2339. int ret;
  2340. st = avformat_new_stream(c->fc, NULL);
  2341. if (!st) return AVERROR(ENOMEM);
  2342. st->id = c->fc->nb_streams;
  2343. sc = av_mallocz(sizeof(MOVStreamContext));
  2344. if (!sc) return AVERROR(ENOMEM);
  2345. st->priv_data = sc;
  2346. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  2347. sc->ffindex = st->index;
  2348. if ((ret = mov_read_default(c, pb, atom)) < 0)
  2349. return ret;
  2350. /* sanity checks */
  2351. if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
  2352. (!sc->sample_size && !sc->sample_count))) {
  2353. av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
  2354. st->index);
  2355. return 0;
  2356. }
  2357. fix_timescale(c, sc);
  2358. avpriv_set_pts_info(st, 64, 1, sc->time_scale);
  2359. mov_build_index(c, st);
  2360. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  2361. MOVDref *dref = &sc->drefs[sc->dref_id - 1];
  2362. if (mov_open_dref(c, &sc->pb, c->fc->filename, dref,
  2363. &c->fc->interrupt_callback) < 0)
  2364. av_log(c->fc, AV_LOG_ERROR,
  2365. "stream %d, error opening alias: path='%s', dir='%s', "
  2366. "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
  2367. st->index, dref->path, dref->dir, dref->filename,
  2368. dref->volume, dref->nlvl_from, dref->nlvl_to);
  2369. } else {
  2370. sc->pb = c->fc->pb;
  2371. sc->pb_is_copied = 1;
  2372. }
  2373. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  2374. if (!st->sample_aspect_ratio.num && st->codec->width && st->codec->height &&
  2375. sc->height && sc->width &&
  2376. (st->codec->width != sc->width || st->codec->height != sc->height)) {
  2377. st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
  2378. ((double)st->codec->width * sc->height), INT_MAX);
  2379. }
  2380. #if FF_API_R_FRAME_RATE
  2381. if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
  2382. av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
  2383. sc->time_scale, sc->stts_data[0].duration, INT_MAX);
  2384. #endif
  2385. }
  2386. // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
  2387. if (!st->codec->extradata_size && st->codec->codec_id == AV_CODEC_ID_H264 &&
  2388. TAG_IS_AVCI(st->codec->codec_tag)) {
  2389. ret = ff_generate_avci_extradata(st);
  2390. if (ret < 0)
  2391. return ret;
  2392. }
  2393. switch (st->codec->codec_id) {
  2394. #if CONFIG_H261_DECODER
  2395. case AV_CODEC_ID_H261:
  2396. #endif
  2397. #if CONFIG_H263_DECODER
  2398. case AV_CODEC_ID_H263:
  2399. #endif
  2400. #if CONFIG_MPEG4_DECODER
  2401. case AV_CODEC_ID_MPEG4:
  2402. #endif
  2403. st->codec->width = 0; /* let decoder init width/height */
  2404. st->codec->height= 0;
  2405. break;
  2406. }
  2407. /* Do not need those anymore. */
  2408. av_freep(&sc->chunk_offsets);
  2409. av_freep(&sc->stsc_data);
  2410. av_freep(&sc->sample_sizes);
  2411. av_freep(&sc->keyframes);
  2412. av_freep(&sc->stts_data);
  2413. av_freep(&sc->stps_data);
  2414. av_freep(&sc->elst_data);
  2415. av_freep(&sc->rap_group);
  2416. return 0;
  2417. }
  2418. static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2419. {
  2420. int ret;
  2421. c->itunes_metadata = 1;
  2422. ret = mov_read_default(c, pb, atom);
  2423. c->itunes_metadata = 0;
  2424. return ret;
  2425. }
  2426. static int mov_read_custom_2plus(MOVContext *c, AVIOContext *pb, int size)
  2427. {
  2428. int64_t end = avio_tell(pb) + size;
  2429. uint8_t *key = NULL, *val = NULL;
  2430. int i;
  2431. AVStream *st;
  2432. MOVStreamContext *sc;
  2433. if (c->fc->nb_streams < 1)
  2434. return 0;
  2435. st = c->fc->streams[c->fc->nb_streams-1];
  2436. sc = st->priv_data;
  2437. for (i = 0; i < 2; i++) {
  2438. uint8_t **p;
  2439. uint32_t len, tag;
  2440. if (end - avio_tell(pb) <= 12)
  2441. break;
  2442. len = avio_rb32(pb);
  2443. tag = avio_rl32(pb);
  2444. avio_skip(pb, 4); // flags
  2445. if (len < 12 || len - 12 > end - avio_tell(pb))
  2446. break;
  2447. len -= 12;
  2448. if (tag == MKTAG('n', 'a', 'm', 'e'))
  2449. p = &key;
  2450. else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
  2451. avio_skip(pb, 4);
  2452. len -= 4;
  2453. p = &val;
  2454. } else
  2455. break;
  2456. *p = av_malloc(len + 1);
  2457. if (!*p)
  2458. break;
  2459. if (avio_read(pb, *p, len) != len) {
  2460. av_freep(p);
  2461. return AVERROR_INVALIDDATA;
  2462. }
  2463. (*p)[len] = 0;
  2464. }
  2465. if (key && val) {
  2466. if (strcmp(key, "iTunSMPB") == 0) {
  2467. int priming, remainder, samples;
  2468. if(sscanf(val, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
  2469. if(priming>0 && priming<16384)
  2470. sc->start_pad = priming;
  2471. }
  2472. }
  2473. if (strcmp(key, "cdec") != 0) {
  2474. av_dict_set(&c->fc->metadata, key, val,
  2475. AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
  2476. key = val = NULL;
  2477. }
  2478. }
  2479. avio_seek(pb, end, SEEK_SET);
  2480. av_freep(&key);
  2481. av_freep(&val);
  2482. return 0;
  2483. }
  2484. static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2485. {
  2486. int64_t end = avio_tell(pb) + atom.size;
  2487. uint32_t tag, len;
  2488. if (atom.size < 8)
  2489. goto fail;
  2490. len = avio_rb32(pb);
  2491. tag = avio_rl32(pb);
  2492. if (len > atom.size)
  2493. goto fail;
  2494. if (tag == MKTAG('m', 'e', 'a', 'n') && len > 12) {
  2495. uint8_t domain[128];
  2496. int domain_len;
  2497. avio_skip(pb, 4); // flags
  2498. len -= 12;
  2499. domain_len = avio_get_str(pb, len, domain, sizeof(domain));
  2500. avio_skip(pb, len - domain_len);
  2501. return mov_read_custom_2plus(c, pb, end - avio_tell(pb));
  2502. }
  2503. fail:
  2504. av_log(c->fc, AV_LOG_VERBOSE,
  2505. "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
  2506. return 0;
  2507. }
  2508. static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2509. {
  2510. while (atom.size > 8) {
  2511. uint32_t tag = avio_rl32(pb);
  2512. atom.size -= 4;
  2513. if (tag == MKTAG('h','d','l','r')) {
  2514. avio_seek(pb, -8, SEEK_CUR);
  2515. atom.size += 8;
  2516. return mov_read_default(c, pb, atom);
  2517. }
  2518. }
  2519. return 0;
  2520. }
  2521. static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2522. {
  2523. int i;
  2524. int width;
  2525. int height;
  2526. int display_matrix[3][3];
  2527. AVStream *st;
  2528. MOVStreamContext *sc;
  2529. int version;
  2530. int flags;
  2531. if (c->fc->nb_streams < 1)
  2532. return 0;
  2533. st = c->fc->streams[c->fc->nb_streams-1];
  2534. sc = st->priv_data;
  2535. version = avio_r8(pb);
  2536. flags = avio_rb24(pb);
  2537. st->disposition |= (flags & MOV_TKHD_FLAG_ENABLED) ? AV_DISPOSITION_DEFAULT : 0;
  2538. if (version == 1) {
  2539. avio_rb64(pb);
  2540. avio_rb64(pb);
  2541. } else {
  2542. avio_rb32(pb); /* creation time */
  2543. avio_rb32(pb); /* modification time */
  2544. }
  2545. st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
  2546. avio_rb32(pb); /* reserved */
  2547. /* highlevel (considering edits) duration in movie timebase */
  2548. (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
  2549. avio_rb32(pb); /* reserved */
  2550. avio_rb32(pb); /* reserved */
  2551. avio_rb16(pb); /* layer */
  2552. avio_rb16(pb); /* alternate group */
  2553. avio_rb16(pb); /* volume */
  2554. avio_rb16(pb); /* reserved */
  2555. //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
  2556. // they're kept in fixed point format through all calculations
  2557. // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
  2558. // side data, but the scale factor is not needed to calculate aspect ratio
  2559. for (i = 0; i < 3; i++) {
  2560. display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
  2561. display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
  2562. display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
  2563. }
  2564. width = avio_rb32(pb); // 16.16 fixed point track width
  2565. height = avio_rb32(pb); // 16.16 fixed point track height
  2566. sc->width = width >> 16;
  2567. sc->height = height >> 16;
  2568. // save the matrix and add rotate metadata when it is not the default
  2569. // identity
  2570. if (display_matrix[0][0] != (1 << 16) ||
  2571. display_matrix[1][1] != (1 << 16) ||
  2572. display_matrix[2][2] != (1 << 30) ||
  2573. display_matrix[0][1] || display_matrix[0][2] ||
  2574. display_matrix[1][0] || display_matrix[1][2] ||
  2575. display_matrix[2][0] || display_matrix[2][1]) {
  2576. int i, j;
  2577. double rotate;
  2578. av_freep(&sc->display_matrix);
  2579. sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
  2580. if (!sc->display_matrix)
  2581. return AVERROR(ENOMEM);
  2582. for (i = 0; i < 3; i++)
  2583. for (j = 0; j < 3; j++)
  2584. sc->display_matrix[i * 3 + j] = display_matrix[i][j];
  2585. rotate = av_display_rotation_get(sc->display_matrix);
  2586. if (!isnan(rotate)) {
  2587. char rotate_buf[64];
  2588. rotate = -rotate;
  2589. if (rotate < 0) // for backward compatibility
  2590. rotate += 360;
  2591. snprintf(rotate_buf, sizeof(rotate_buf), "%g", rotate);
  2592. av_dict_set(&st->metadata, "rotate", rotate_buf, 0);
  2593. }
  2594. }
  2595. // transform the display width/height according to the matrix
  2596. // to keep the same scale, use [width height 1<<16]
  2597. if (width && height && sc->display_matrix) {
  2598. double disp_transform[2];
  2599. #define SQR(a) ((a)*(double)(a))
  2600. for (i = 0; i < 2; i++)
  2601. disp_transform[i] = sqrt(SQR(display_matrix[i][0]) + SQR(display_matrix[i][1]));
  2602. if (disp_transform[0] > 0 && disp_transform[1] > 0 &&
  2603. disp_transform[0] < (1<<24) && disp_transform[1] < (1<<24) &&
  2604. fabs((disp_transform[0] / disp_transform[1]) - 1.0) > 0.01)
  2605. st->sample_aspect_ratio = av_d2q(
  2606. disp_transform[0] / disp_transform[1],
  2607. INT_MAX);
  2608. }
  2609. return 0;
  2610. }
  2611. static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2612. {
  2613. MOVFragment *frag = &c->fragment;
  2614. MOVTrackExt *trex = NULL;
  2615. MOVFragmentIndex* index = NULL;
  2616. int flags, track_id, i;
  2617. avio_r8(pb); /* version */
  2618. flags = avio_rb24(pb);
  2619. track_id = avio_rb32(pb);
  2620. if (!track_id)
  2621. return AVERROR_INVALIDDATA;
  2622. frag->track_id = track_id;
  2623. for (i = 0; i < c->trex_count; i++)
  2624. if (c->trex_data[i].track_id == frag->track_id) {
  2625. trex = &c->trex_data[i];
  2626. break;
  2627. }
  2628. if (!trex) {
  2629. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
  2630. return AVERROR_INVALIDDATA;
  2631. }
  2632. for (i = 0; i < c->fragment_index_count; i++) {
  2633. MOVFragmentIndex* candidate = c->fragment_index_data[i];
  2634. if (candidate->track_id == frag->track_id) {
  2635. av_log(c->fc, AV_LOG_DEBUG,
  2636. "found fragment index for track %u\n", frag->track_id);
  2637. index = candidate;
  2638. break;
  2639. }
  2640. }
  2641. frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ?
  2642. avio_rb64(pb) : flags & MOV_TFHD_DEFAULT_BASE_IS_MOOF ?
  2643. frag->moof_offset : frag->implicit_offset;
  2644. frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
  2645. frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
  2646. avio_rb32(pb) : trex->duration;
  2647. frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
  2648. avio_rb32(pb) : trex->size;
  2649. frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
  2650. avio_rb32(pb) : trex->flags;
  2651. frag->time = AV_NOPTS_VALUE;
  2652. if (index) {
  2653. int i, found = 0;
  2654. for (i = index->current_item; i < index->item_count; i++) {
  2655. if (frag->implicit_offset == index->items[i].moof_offset) {
  2656. av_log(c->fc, AV_LOG_DEBUG, "found fragment index entry "
  2657. "for track %u and moof_offset %"PRId64"\n",
  2658. frag->track_id, index->items[i].moof_offset);
  2659. frag->time = index->items[i].time;
  2660. index->current_item = i + 1;
  2661. found = 1;
  2662. }
  2663. }
  2664. if (!found) {
  2665. av_log(c->fc, AV_LOG_WARNING, "track %u has a fragment index "
  2666. "but it doesn't have an (in-order) entry for moof_offset "
  2667. "%"PRId64"\n", frag->track_id, frag->implicit_offset);
  2668. }
  2669. }
  2670. av_log(c->fc, AV_LOG_TRACE, "frag flags 0x%x\n", frag->flags);
  2671. return 0;
  2672. }
  2673. static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2674. {
  2675. c->chapter_track = avio_rb32(pb);
  2676. return 0;
  2677. }
  2678. static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2679. {
  2680. MOVTrackExt *trex;
  2681. int err;
  2682. if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
  2683. return AVERROR_INVALIDDATA;
  2684. if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
  2685. sizeof(*c->trex_data))) < 0) {
  2686. c->trex_count = 0;
  2687. return err;
  2688. }
  2689. c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
  2690. trex = &c->trex_data[c->trex_count++];
  2691. avio_r8(pb); /* version */
  2692. avio_rb24(pb); /* flags */
  2693. trex->track_id = avio_rb32(pb);
  2694. trex->stsd_id = avio_rb32(pb);
  2695. trex->duration = avio_rb32(pb);
  2696. trex->size = avio_rb32(pb);
  2697. trex->flags = avio_rb32(pb);
  2698. return 0;
  2699. }
  2700. static int mov_read_tfdt(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2701. {
  2702. MOVFragment *frag = &c->fragment;
  2703. AVStream *st = NULL;
  2704. MOVStreamContext *sc;
  2705. int version, i;
  2706. for (i = 0; i < c->fc->nb_streams; i++) {
  2707. if (c->fc->streams[i]->id == frag->track_id) {
  2708. st = c->fc->streams[i];
  2709. break;
  2710. }
  2711. }
  2712. if (!st) {
  2713. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
  2714. return AVERROR_INVALIDDATA;
  2715. }
  2716. sc = st->priv_data;
  2717. if (sc->pseudo_stream_id + 1 != frag->stsd_id)
  2718. return 0;
  2719. version = avio_r8(pb);
  2720. avio_rb24(pb); /* flags */
  2721. if (version) {
  2722. sc->track_end = avio_rb64(pb);
  2723. } else {
  2724. sc->track_end = avio_rb32(pb);
  2725. }
  2726. return 0;
  2727. }
  2728. static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2729. {
  2730. MOVFragment *frag = &c->fragment;
  2731. AVStream *st = NULL;
  2732. MOVStreamContext *sc;
  2733. MOVStts *ctts_data;
  2734. uint64_t offset;
  2735. int64_t dts;
  2736. int data_offset = 0;
  2737. unsigned entries, first_sample_flags = frag->flags;
  2738. int flags, distance, i, found_keyframe = 0, err;
  2739. for (i = 0; i < c->fc->nb_streams; i++) {
  2740. if (c->fc->streams[i]->id == frag->track_id) {
  2741. st = c->fc->streams[i];
  2742. break;
  2743. }
  2744. }
  2745. if (!st) {
  2746. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
  2747. return AVERROR_INVALIDDATA;
  2748. }
  2749. sc = st->priv_data;
  2750. if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
  2751. return 0;
  2752. avio_r8(pb); /* version */
  2753. flags = avio_rb24(pb);
  2754. entries = avio_rb32(pb);
  2755. av_log(c->fc, AV_LOG_TRACE, "flags 0x%x entries %d\n", flags, entries);
  2756. /* Always assume the presence of composition time offsets.
  2757. * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
  2758. * 1) in the initial movie, there are no samples.
  2759. * 2) in the first movie fragment, there is only one sample without composition time offset.
  2760. * 3) in the subsequent movie fragments, there are samples with composition time offset. */
  2761. if (!sc->ctts_count && sc->sample_count)
  2762. {
  2763. /* Complement ctts table if moov atom doesn't have ctts atom. */
  2764. ctts_data = av_realloc(NULL, sizeof(*sc->ctts_data));
  2765. if (!ctts_data)
  2766. return AVERROR(ENOMEM);
  2767. sc->ctts_data = ctts_data;
  2768. sc->ctts_data[sc->ctts_count].count = sc->sample_count;
  2769. sc->ctts_data[sc->ctts_count].duration = 0;
  2770. sc->ctts_count++;
  2771. }
  2772. if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
  2773. return AVERROR_INVALIDDATA;
  2774. if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count,
  2775. sizeof(*sc->ctts_data))) < 0) {
  2776. sc->ctts_count = 0;
  2777. return err;
  2778. }
  2779. if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
  2780. if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
  2781. dts = sc->track_end - sc->time_offset;
  2782. offset = frag->base_data_offset + data_offset;
  2783. distance = 0;
  2784. av_log(c->fc, AV_LOG_TRACE, "first sample flags 0x%x\n", first_sample_flags);
  2785. for (i = 0; i < entries && !pb->eof_reached; i++) {
  2786. unsigned sample_size = frag->size;
  2787. int sample_flags = i ? frag->flags : first_sample_flags;
  2788. unsigned sample_duration = frag->duration;
  2789. int keyframe = 0;
  2790. if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
  2791. if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
  2792. if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
  2793. sc->ctts_data[sc->ctts_count].count = 1;
  2794. sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
  2795. avio_rb32(pb) : 0;
  2796. mov_update_dts_shift(sc, sc->ctts_data[sc->ctts_count].duration);
  2797. if (frag->time != AV_NOPTS_VALUE) {
  2798. if (c->use_mfra_for == FF_MOV_FLAG_MFRA_PTS) {
  2799. int64_t pts = frag->time;
  2800. av_log(c->fc, AV_LOG_DEBUG, "found frag time %"PRId64
  2801. " sc->dts_shift %d ctts.duration %d"
  2802. " sc->time_offset %"PRId64" flags & MOV_TRUN_SAMPLE_CTS %d\n", pts,
  2803. sc->dts_shift, sc->ctts_data[sc->ctts_count].duration,
  2804. sc->time_offset, flags & MOV_TRUN_SAMPLE_CTS);
  2805. dts = pts - sc->dts_shift;
  2806. if (flags & MOV_TRUN_SAMPLE_CTS) {
  2807. dts -= sc->ctts_data[sc->ctts_count].duration;
  2808. } else {
  2809. dts -= sc->time_offset;
  2810. }
  2811. av_log(c->fc, AV_LOG_DEBUG, "calculated into dts %"PRId64"\n", dts);
  2812. } else {
  2813. dts = frag->time;
  2814. av_log(c->fc, AV_LOG_DEBUG, "found frag time %"PRId64
  2815. ", using it for dts\n", dts);
  2816. }
  2817. frag->time = AV_NOPTS_VALUE;
  2818. }
  2819. sc->ctts_count++;
  2820. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  2821. keyframe = 1;
  2822. else if (!found_keyframe)
  2823. keyframe = found_keyframe =
  2824. !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
  2825. MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES));
  2826. if (keyframe)
  2827. distance = 0;
  2828. err = av_add_index_entry(st, offset, dts, sample_size, distance,
  2829. keyframe ? AVINDEX_KEYFRAME : 0);
  2830. if (err < 0) {
  2831. av_log(c->fc, AV_LOG_ERROR, "Failed to add index entry\n");
  2832. }
  2833. av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  2834. "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
  2835. offset, dts, sample_size, distance, keyframe);
  2836. distance++;
  2837. dts += sample_duration;
  2838. offset += sample_size;
  2839. sc->data_size += sample_size;
  2840. sc->duration_for_fps += sample_duration;
  2841. sc->nb_frames_for_fps ++;
  2842. }
  2843. if (pb->eof_reached)
  2844. return AVERROR_EOF;
  2845. frag->implicit_offset = offset;
  2846. st->duration = sc->track_end = dts + sc->time_offset;
  2847. return 0;
  2848. }
  2849. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  2850. /* like the files created with Adobe Premiere 5.0, for samples see */
  2851. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  2852. static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2853. {
  2854. int err;
  2855. if (atom.size < 8)
  2856. return 0; /* continue */
  2857. if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  2858. avio_skip(pb, atom.size - 4);
  2859. return 0;
  2860. }
  2861. atom.type = avio_rl32(pb);
  2862. atom.size -= 8;
  2863. if (atom.type != MKTAG('m','d','a','t')) {
  2864. avio_skip(pb, atom.size);
  2865. return 0;
  2866. }
  2867. err = mov_read_mdat(c, pb, atom);
  2868. return err;
  2869. }
  2870. static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2871. {
  2872. #if CONFIG_ZLIB
  2873. AVIOContext ctx;
  2874. uint8_t *cmov_data;
  2875. uint8_t *moov_data; /* uncompressed data */
  2876. long cmov_len, moov_len;
  2877. int ret = -1;
  2878. avio_rb32(pb); /* dcom atom */
  2879. if (avio_rl32(pb) != MKTAG('d','c','o','m'))
  2880. return AVERROR_INVALIDDATA;
  2881. if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
  2882. av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
  2883. return AVERROR_INVALIDDATA;
  2884. }
  2885. avio_rb32(pb); /* cmvd atom */
  2886. if (avio_rl32(pb) != MKTAG('c','m','v','d'))
  2887. return AVERROR_INVALIDDATA;
  2888. moov_len = avio_rb32(pb); /* uncompressed size */
  2889. cmov_len = atom.size - 6 * 4;
  2890. cmov_data = av_malloc(cmov_len);
  2891. if (!cmov_data)
  2892. return AVERROR(ENOMEM);
  2893. moov_data = av_malloc(moov_len);
  2894. if (!moov_data) {
  2895. av_free(cmov_data);
  2896. return AVERROR(ENOMEM);
  2897. }
  2898. if (avio_read(pb, cmov_data, cmov_len) != cmov_len) {
  2899. av_freep(&cmov_data);
  2900. av_freep(&moov_data);
  2901. return AVERROR_INVALIDDATA;
  2902. }
  2903. if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  2904. goto free_and_return;
  2905. if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  2906. goto free_and_return;
  2907. ctx.seekable = AVIO_SEEKABLE_NORMAL;
  2908. atom.type = MKTAG('m','o','o','v');
  2909. atom.size = moov_len;
  2910. ret = mov_read_default(c, &ctx, atom);
  2911. free_and_return:
  2912. av_free(moov_data);
  2913. av_free(cmov_data);
  2914. return ret;
  2915. #else
  2916. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  2917. return AVERROR(ENOSYS);
  2918. #endif
  2919. }
  2920. /* edit list atom */
  2921. static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2922. {
  2923. MOVStreamContext *sc;
  2924. int i, edit_count, version;
  2925. if (c->fc->nb_streams < 1 || c->ignore_editlist)
  2926. return 0;
  2927. sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  2928. version = avio_r8(pb); /* version */
  2929. avio_rb24(pb); /* flags */
  2930. edit_count = avio_rb32(pb); /* entries */
  2931. if (!edit_count)
  2932. return 0;
  2933. if (sc->elst_data)
  2934. av_log(c->fc, AV_LOG_WARNING, "Duplicated ELST atom\n");
  2935. av_free(sc->elst_data);
  2936. sc->elst_count = 0;
  2937. sc->elst_data = av_malloc_array(edit_count, sizeof(*sc->elst_data));
  2938. if (!sc->elst_data)
  2939. return AVERROR(ENOMEM);
  2940. av_log(c->fc, AV_LOG_TRACE, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
  2941. for (i = 0; i < edit_count && !pb->eof_reached; i++) {
  2942. MOVElst *e = &sc->elst_data[i];
  2943. if (version == 1) {
  2944. e->duration = avio_rb64(pb);
  2945. e->time = avio_rb64(pb);
  2946. } else {
  2947. e->duration = avio_rb32(pb); /* segment duration */
  2948. e->time = (int32_t)avio_rb32(pb); /* media time */
  2949. }
  2950. e->rate = avio_rb32(pb) / 65536.0;
  2951. av_log(c->fc, AV_LOG_TRACE, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
  2952. e->duration, e->time, e->rate);
  2953. }
  2954. sc->elst_count = i;
  2955. return 0;
  2956. }
  2957. static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2958. {
  2959. MOVStreamContext *sc;
  2960. if (c->fc->nb_streams < 1)
  2961. return AVERROR_INVALIDDATA;
  2962. sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
  2963. sc->timecode_track = avio_rb32(pb);
  2964. return 0;
  2965. }
  2966. static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2967. {
  2968. int ret;
  2969. uint8_t uuid[16];
  2970. static const uint8_t uuid_isml_manifest[] = {
  2971. 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
  2972. 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
  2973. };
  2974. if (atom.size < sizeof(uuid) || atom.size == INT64_MAX)
  2975. return AVERROR_INVALIDDATA;
  2976. ret = avio_read(pb, uuid, sizeof(uuid));
  2977. if (ret < 0) {
  2978. return ret;
  2979. } else if (ret != sizeof(uuid)) {
  2980. return AVERROR_INVALIDDATA;
  2981. }
  2982. if (!memcmp(uuid, uuid_isml_manifest, sizeof(uuid))) {
  2983. uint8_t *buffer, *ptr;
  2984. char *endptr;
  2985. size_t len = atom.size - sizeof(uuid);
  2986. if (len < 4) {
  2987. return AVERROR_INVALIDDATA;
  2988. }
  2989. ret = avio_skip(pb, 4); // zeroes
  2990. len -= 4;
  2991. buffer = av_mallocz(len + 1);
  2992. if (!buffer) {
  2993. return AVERROR(ENOMEM);
  2994. }
  2995. ret = avio_read(pb, buffer, len);
  2996. if (ret < 0) {
  2997. av_free(buffer);
  2998. return ret;
  2999. } else if (ret != len) {
  3000. av_free(buffer);
  3001. return AVERROR_INVALIDDATA;
  3002. }
  3003. ptr = buffer;
  3004. while ((ptr = av_stristr(ptr, "systemBitrate=\""))) {
  3005. ptr += sizeof("systemBitrate=\"") - 1;
  3006. c->bitrates_count++;
  3007. c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
  3008. if (!c->bitrates) {
  3009. c->bitrates_count = 0;
  3010. av_free(buffer);
  3011. return AVERROR(ENOMEM);
  3012. }
  3013. errno = 0;
  3014. ret = strtol(ptr, &endptr, 10);
  3015. if (ret < 0 || errno || *endptr != '"') {
  3016. c->bitrates[c->bitrates_count - 1] = 0;
  3017. } else {
  3018. c->bitrates[c->bitrates_count - 1] = ret;
  3019. }
  3020. }
  3021. av_free(buffer);
  3022. }
  3023. return 0;
  3024. }
  3025. static int mov_read_free(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  3026. {
  3027. int ret;
  3028. uint8_t content[16];
  3029. if (atom.size < 8)
  3030. return 0;
  3031. ret = avio_read(pb, content, FFMIN(sizeof(content), atom.size));
  3032. if (ret < 0)
  3033. return ret;
  3034. if ( !c->found_moov
  3035. && !c->found_mdat
  3036. && !memcmp(content, "Anevia\x1A\x1A", 8)
  3037. && c->use_mfra_for == FF_MOV_FLAG_MFRA_AUTO) {
  3038. c->use_mfra_for = FF_MOV_FLAG_MFRA_PTS;
  3039. }
  3040. return 0;
  3041. }
  3042. static const MOVParseTableEntry mov_default_parse_table[] = {
  3043. { MKTAG('A','C','L','R'), mov_read_aclr },
  3044. { MKTAG('A','P','R','G'), mov_read_avid },
  3045. { MKTAG('A','A','L','P'), mov_read_avid },
  3046. { MKTAG('A','R','E','S'), mov_read_ares },
  3047. { MKTAG('a','v','s','s'), mov_read_avss },
  3048. { MKTAG('c','h','p','l'), mov_read_chpl },
  3049. { MKTAG('c','o','6','4'), mov_read_stco },
  3050. { MKTAG('c','o','l','r'), mov_read_colr },
  3051. { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
  3052. { MKTAG('d','i','n','f'), mov_read_default },
  3053. { MKTAG('D','p','x','E'), mov_read_dpxe },
  3054. { MKTAG('d','r','e','f'), mov_read_dref },
  3055. { MKTAG('e','d','t','s'), mov_read_default },
  3056. { MKTAG('e','l','s','t'), mov_read_elst },
  3057. { MKTAG('e','n','d','a'), mov_read_enda },
  3058. { MKTAG('f','i','e','l'), mov_read_fiel },
  3059. { MKTAG('f','t','y','p'), mov_read_ftyp },
  3060. { MKTAG('g','l','b','l'), mov_read_glbl },
  3061. { MKTAG('h','d','l','r'), mov_read_hdlr },
  3062. { MKTAG('i','l','s','t'), mov_read_ilst },
  3063. { MKTAG('j','p','2','h'), mov_read_jp2h },
  3064. { MKTAG('m','d','a','t'), mov_read_mdat },
  3065. { MKTAG('m','d','h','d'), mov_read_mdhd },
  3066. { MKTAG('m','d','i','a'), mov_read_default },
  3067. { MKTAG('m','e','t','a'), mov_read_meta },
  3068. { MKTAG('m','i','n','f'), mov_read_default },
  3069. { MKTAG('m','o','o','f'), mov_read_moof },
  3070. { MKTAG('m','o','o','v'), mov_read_moov },
  3071. { MKTAG('m','v','e','x'), mov_read_default },
  3072. { MKTAG('m','v','h','d'), mov_read_mvhd },
  3073. { MKTAG('S','M','I',' '), mov_read_svq3 },
  3074. { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
  3075. { MKTAG('a','v','c','C'), mov_read_glbl },
  3076. { MKTAG('p','a','s','p'), mov_read_pasp },
  3077. { MKTAG('s','t','b','l'), mov_read_default },
  3078. { MKTAG('s','t','c','o'), mov_read_stco },
  3079. { MKTAG('s','t','p','s'), mov_read_stps },
  3080. { MKTAG('s','t','r','f'), mov_read_strf },
  3081. { MKTAG('s','t','s','c'), mov_read_stsc },
  3082. { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
  3083. { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
  3084. { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
  3085. { MKTAG('s','t','t','s'), mov_read_stts },
  3086. { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
  3087. { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
  3088. { MKTAG('t','f','d','t'), mov_read_tfdt },
  3089. { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
  3090. { MKTAG('t','r','a','k'), mov_read_trak },
  3091. { MKTAG('t','r','a','f'), mov_read_default },
  3092. { MKTAG('t','r','e','f'), mov_read_default },
  3093. { MKTAG('t','m','c','d'), mov_read_tmcd },
  3094. { MKTAG('c','h','a','p'), mov_read_chap },
  3095. { MKTAG('t','r','e','x'), mov_read_trex },
  3096. { MKTAG('t','r','u','n'), mov_read_trun },
  3097. { MKTAG('u','d','t','a'), mov_read_default },
  3098. { MKTAG('w','a','v','e'), mov_read_wave },
  3099. { MKTAG('e','s','d','s'), mov_read_esds },
  3100. { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
  3101. { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
  3102. { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
  3103. { MKTAG('w','f','e','x'), mov_read_wfex },
  3104. { MKTAG('c','m','o','v'), mov_read_cmov },
  3105. { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
  3106. { MKTAG('d','v','c','1'), mov_read_dvc1 },
  3107. { MKTAG('s','b','g','p'), mov_read_sbgp },
  3108. { MKTAG('h','v','c','C'), mov_read_glbl },
  3109. { MKTAG('u','u','i','d'), mov_read_uuid },
  3110. { MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
  3111. { MKTAG('f','r','e','e'), mov_read_free },
  3112. { MKTAG('-','-','-','-'), mov_read_custom },
  3113. { 0, NULL }
  3114. };
  3115. static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  3116. {
  3117. int64_t total_size = 0;
  3118. MOVAtom a;
  3119. int i;
  3120. if (c->atom_depth > 10) {
  3121. av_log(c->fc, AV_LOG_ERROR, "Atoms too deeply nested\n");
  3122. return AVERROR_INVALIDDATA;
  3123. }
  3124. c->atom_depth ++;
  3125. if (atom.size < 0)
  3126. atom.size = INT64_MAX;
  3127. while (total_size + 8 <= atom.size && !avio_feof(pb)) {
  3128. int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
  3129. a.size = atom.size;
  3130. a.type=0;
  3131. if (atom.size >= 8) {
  3132. a.size = avio_rb32(pb);
  3133. a.type = avio_rl32(pb);
  3134. if (a.type == MKTAG('f','r','e','e') &&
  3135. a.size >= 8 &&
  3136. c->moov_retry) {
  3137. uint8_t buf[8];
  3138. uint32_t *type = (uint32_t *)buf + 1;
  3139. if (avio_read(pb, buf, 8) != 8)
  3140. return AVERROR_INVALIDDATA;
  3141. avio_seek(pb, -8, SEEK_CUR);
  3142. if (*type == MKTAG('m','v','h','d') ||
  3143. *type == MKTAG('c','m','o','v')) {
  3144. av_log(c->fc, AV_LOG_ERROR, "Detected moov in a free atom.\n");
  3145. a.type = MKTAG('m','o','o','v');
  3146. }
  3147. }
  3148. if (atom.type != MKTAG('r','o','o','t') &&
  3149. atom.type != MKTAG('m','o','o','v'))
  3150. {
  3151. if (a.type == MKTAG('t','r','a','k') || a.type == MKTAG('m','d','a','t'))
  3152. {
  3153. av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
  3154. avio_skip(pb, -8);
  3155. c->atom_depth --;
  3156. return 0;
  3157. }
  3158. }
  3159. total_size += 8;
  3160. if (a.size == 1 && total_size + 8 <= atom.size) { /* 64 bit extended size */
  3161. a.size = avio_rb64(pb) - 8;
  3162. total_size += 8;
  3163. }
  3164. }
  3165. av_log(c->fc, AV_LOG_TRACE, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
  3166. a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
  3167. if (a.size == 0) {
  3168. a.size = atom.size - total_size + 8;
  3169. }
  3170. a.size -= 8;
  3171. if (a.size < 0)
  3172. break;
  3173. a.size = FFMIN(a.size, atom.size - total_size);
  3174. for (i = 0; mov_default_parse_table[i].type; i++)
  3175. if (mov_default_parse_table[i].type == a.type) {
  3176. parse = mov_default_parse_table[i].parse;
  3177. break;
  3178. }
  3179. // container is user data
  3180. if (!parse && (atom.type == MKTAG('u','d','t','a') ||
  3181. atom.type == MKTAG('i','l','s','t')))
  3182. parse = mov_read_udta_string;
  3183. if (!parse) { /* skip leaf atoms data */
  3184. avio_skip(pb, a.size);
  3185. } else {
  3186. int64_t start_pos = avio_tell(pb);
  3187. int64_t left;
  3188. int err = parse(c, pb, a);
  3189. if (err < 0) {
  3190. c->atom_depth --;
  3191. return err;
  3192. }
  3193. if (c->found_moov && c->found_mdat &&
  3194. ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) ||
  3195. start_pos + a.size == avio_size(pb))) {
  3196. if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX)
  3197. c->next_root_atom = start_pos + a.size;
  3198. c->atom_depth --;
  3199. return 0;
  3200. }
  3201. left = a.size - avio_tell(pb) + start_pos;
  3202. if (left > 0) /* skip garbage at atom end */
  3203. avio_skip(pb, left);
  3204. else if (left < 0) {
  3205. av_log(c->fc, AV_LOG_WARNING,
  3206. "overread end of atom '%.4s' by %"PRId64" bytes\n",
  3207. (char*)&a.type, -left);
  3208. avio_seek(pb, left, SEEK_CUR);
  3209. }
  3210. }
  3211. total_size += a.size;
  3212. }
  3213. if (total_size < atom.size && atom.size < 0x7ffff)
  3214. avio_skip(pb, atom.size - total_size);
  3215. c->atom_depth --;
  3216. return 0;
  3217. }
  3218. static int mov_probe(AVProbeData *p)
  3219. {
  3220. int64_t offset;
  3221. uint32_t tag;
  3222. int score = 0;
  3223. int moov_offset = -1;
  3224. /* check file header */
  3225. offset = 0;
  3226. for (;;) {
  3227. /* ignore invalid offset */
  3228. if ((offset + 8) > (unsigned int)p->buf_size)
  3229. break;
  3230. tag = AV_RL32(p->buf + offset + 4);
  3231. switch(tag) {
  3232. /* check for obvious tags */
  3233. case MKTAG('m','o','o','v'):
  3234. moov_offset = offset + 4;
  3235. case MKTAG('m','d','a','t'):
  3236. case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
  3237. case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
  3238. case MKTAG('f','t','y','p'):
  3239. if (AV_RB32(p->buf+offset) < 8 &&
  3240. (AV_RB32(p->buf+offset) != 1 ||
  3241. offset + 12 > (unsigned int)p->buf_size ||
  3242. AV_RB64(p->buf+offset + 8) == 0)) {
  3243. score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
  3244. } else if (tag == MKTAG('f','t','y','p') &&
  3245. ( AV_RL32(p->buf + offset + 8) == MKTAG('j','p','2',' ')
  3246. || AV_RL32(p->buf + offset + 8) == MKTAG('j','p','x',' ')
  3247. )) {
  3248. score = FFMAX(score, 5);
  3249. } else {
  3250. score = AVPROBE_SCORE_MAX;
  3251. }
  3252. offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
  3253. break;
  3254. /* those are more common words, so rate then a bit less */
  3255. case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
  3256. case MKTAG('w','i','d','e'):
  3257. case MKTAG('f','r','e','e'):
  3258. case MKTAG('j','u','n','k'):
  3259. case MKTAG('p','i','c','t'):
  3260. score = FFMAX(score, AVPROBE_SCORE_MAX - 5);
  3261. offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
  3262. break;
  3263. case MKTAG(0x82,0x82,0x7f,0x7d):
  3264. case MKTAG('s','k','i','p'):
  3265. case MKTAG('u','u','i','d'):
  3266. case MKTAG('p','r','f','l'):
  3267. /* if we only find those cause probedata is too small at least rate them */
  3268. score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
  3269. offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
  3270. break;
  3271. default:
  3272. offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
  3273. }
  3274. }
  3275. if(score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
  3276. /* moov atom in the header - we should make sure that this is not a
  3277. * MOV-packed MPEG-PS */
  3278. offset = moov_offset;
  3279. while(offset < (p->buf_size - 16)){ /* Sufficient space */
  3280. /* We found an actual hdlr atom */
  3281. if(AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') &&
  3282. AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') &&
  3283. AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')){
  3284. av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
  3285. /* We found a media handler reference atom describing an
  3286. * MPEG-PS-in-MOV, return a
  3287. * low score to force expanding the probe window until
  3288. * mpegps_probe finds what it needs */
  3289. return 5;
  3290. }else
  3291. /* Keep looking */
  3292. offset+=2;
  3293. }
  3294. }
  3295. return score;
  3296. }
  3297. // must be done after parsing all trak because there's no order requirement
  3298. static void mov_read_chapters(AVFormatContext *s)
  3299. {
  3300. MOVContext *mov = s->priv_data;
  3301. AVStream *st = NULL;
  3302. MOVStreamContext *sc;
  3303. int64_t cur_pos;
  3304. int i;
  3305. for (i = 0; i < s->nb_streams; i++)
  3306. if (s->streams[i]->id == mov->chapter_track) {
  3307. st = s->streams[i];
  3308. break;
  3309. }
  3310. if (!st) {
  3311. av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
  3312. return;
  3313. }
  3314. st->discard = AVDISCARD_ALL;
  3315. sc = st->priv_data;
  3316. cur_pos = avio_tell(sc->pb);
  3317. for (i = 0; i < st->nb_index_entries; i++) {
  3318. AVIndexEntry *sample = &st->index_entries[i];
  3319. int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
  3320. uint8_t *title;
  3321. uint16_t ch;
  3322. int len, title_len;
  3323. if (end < sample->timestamp) {
  3324. av_log(s, AV_LOG_WARNING, "ignoring stream duration which is shorter than chapters\n");
  3325. end = AV_NOPTS_VALUE;
  3326. }
  3327. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  3328. av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
  3329. goto finish;
  3330. }
  3331. // the first two bytes are the length of the title
  3332. len = avio_rb16(sc->pb);
  3333. if (len > sample->size-2)
  3334. continue;
  3335. title_len = 2*len + 1;
  3336. if (!(title = av_mallocz(title_len)))
  3337. goto finish;
  3338. // The samples could theoretically be in any encoding if there's an encd
  3339. // atom following, but in practice are only utf-8 or utf-16, distinguished
  3340. // instead by the presence of a BOM
  3341. if (!len) {
  3342. title[0] = 0;
  3343. } else {
  3344. ch = avio_rb16(sc->pb);
  3345. if (ch == 0xfeff)
  3346. avio_get_str16be(sc->pb, len, title, title_len);
  3347. else if (ch == 0xfffe)
  3348. avio_get_str16le(sc->pb, len, title, title_len);
  3349. else {
  3350. AV_WB16(title, ch);
  3351. if (len == 1 || len == 2)
  3352. title[len] = 0;
  3353. else
  3354. avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
  3355. }
  3356. }
  3357. avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
  3358. av_freep(&title);
  3359. }
  3360. finish:
  3361. avio_seek(sc->pb, cur_pos, SEEK_SET);
  3362. }
  3363. static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st,
  3364. uint32_t value, int flags)
  3365. {
  3366. AVTimecode tc;
  3367. char buf[AV_TIMECODE_STR_SIZE];
  3368. AVRational rate = {st->codec->time_base.den,
  3369. st->codec->time_base.num};
  3370. int ret = av_timecode_init(&tc, rate, flags, 0, s);
  3371. if (ret < 0)
  3372. return ret;
  3373. av_dict_set(&st->metadata, "timecode",
  3374. av_timecode_make_string(&tc, buf, value), 0);
  3375. return 0;
  3376. }
  3377. static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
  3378. {
  3379. MOVStreamContext *sc = st->priv_data;
  3380. int flags = 0;
  3381. int64_t cur_pos = avio_tell(sc->pb);
  3382. uint32_t value;
  3383. if (!st->nb_index_entries)
  3384. return -1;
  3385. avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
  3386. value = avio_rb32(s->pb);
  3387. if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
  3388. if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
  3389. if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
  3390. /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
  3391. * not the case) and thus assume "frame number format" instead of QT one.
  3392. * No sample with tmcd track can be found with a QT timecode at the moment,
  3393. * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
  3394. * format). */
  3395. parse_timecode_in_framenum_format(s, st, value, flags);
  3396. avio_seek(sc->pb, cur_pos, SEEK_SET);
  3397. return 0;
  3398. }
  3399. static int mov_read_close(AVFormatContext *s)
  3400. {
  3401. MOVContext *mov = s->priv_data;
  3402. int i, j;
  3403. for (i = 0; i < s->nb_streams; i++) {
  3404. AVStream *st = s->streams[i];
  3405. MOVStreamContext *sc = st->priv_data;
  3406. av_freep(&sc->ctts_data);
  3407. for (j = 0; j < sc->drefs_count; j++) {
  3408. av_freep(&sc->drefs[j].path);
  3409. av_freep(&sc->drefs[j].dir);
  3410. }
  3411. av_freep(&sc->drefs);
  3412. sc->drefs_count = 0;
  3413. if (!sc->pb_is_copied)
  3414. avio_closep(&sc->pb);
  3415. sc->pb = NULL;
  3416. av_freep(&sc->chunk_offsets);
  3417. av_freep(&sc->stsc_data);
  3418. av_freep(&sc->sample_sizes);
  3419. av_freep(&sc->keyframes);
  3420. av_freep(&sc->stts_data);
  3421. av_freep(&sc->stps_data);
  3422. av_freep(&sc->elst_data);
  3423. av_freep(&sc->rap_group);
  3424. av_freep(&sc->display_matrix);
  3425. }
  3426. if (mov->dv_demux) {
  3427. avformat_free_context(mov->dv_fctx);
  3428. mov->dv_fctx = NULL;
  3429. }
  3430. av_freep(&mov->trex_data);
  3431. av_freep(&mov->bitrates);
  3432. for (i = 0; i < mov->fragment_index_count; i++) {
  3433. MOVFragmentIndex* index = mov->fragment_index_data[i];
  3434. av_freep(&index->items);
  3435. av_freep(&mov->fragment_index_data[i]);
  3436. }
  3437. av_freep(&mov->fragment_index_data);
  3438. return 0;
  3439. }
  3440. static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
  3441. {
  3442. int i;
  3443. for (i = 0; i < s->nb_streams; i++) {
  3444. AVStream *st = s->streams[i];
  3445. MOVStreamContext *sc = st->priv_data;
  3446. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  3447. sc->timecode_track == tmcd_id)
  3448. return 1;
  3449. }
  3450. return 0;
  3451. }
  3452. /* look for a tmcd track not referenced by any video track, and export it globally */
  3453. static void export_orphan_timecode(AVFormatContext *s)
  3454. {
  3455. int i;
  3456. for (i = 0; i < s->nb_streams; i++) {
  3457. AVStream *st = s->streams[i];
  3458. if (st->codec->codec_tag == MKTAG('t','m','c','d') &&
  3459. !tmcd_is_referenced(s, i + 1)) {
  3460. AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
  3461. if (tcr) {
  3462. av_dict_set(&s->metadata, "timecode", tcr->value, 0);
  3463. break;
  3464. }
  3465. }
  3466. }
  3467. }
  3468. static int read_tfra(MOVContext *mov, AVIOContext *f)
  3469. {
  3470. MOVFragmentIndex* index = NULL;
  3471. int version, fieldlength, i, j;
  3472. int64_t pos = avio_tell(f);
  3473. uint32_t size = avio_rb32(f);
  3474. void *tmp;
  3475. if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a')) {
  3476. return 1;
  3477. }
  3478. av_log(mov->fc, AV_LOG_VERBOSE, "found tfra\n");
  3479. index = av_mallocz(sizeof(MOVFragmentIndex));
  3480. if (!index) {
  3481. return AVERROR(ENOMEM);
  3482. }
  3483. tmp = av_realloc_array(mov->fragment_index_data,
  3484. mov->fragment_index_count + 1,
  3485. sizeof(MOVFragmentIndex*));
  3486. if (!tmp) {
  3487. av_freep(&index);
  3488. return AVERROR(ENOMEM);
  3489. }
  3490. mov->fragment_index_data = tmp;
  3491. mov->fragment_index_data[mov->fragment_index_count++] = index;
  3492. version = avio_r8(f);
  3493. avio_rb24(f);
  3494. index->track_id = avio_rb32(f);
  3495. fieldlength = avio_rb32(f);
  3496. index->item_count = avio_rb32(f);
  3497. index->items = av_mallocz_array(
  3498. index->item_count, sizeof(MOVFragmentIndexItem));
  3499. if (!index->items) {
  3500. index->item_count = 0;
  3501. return AVERROR(ENOMEM);
  3502. }
  3503. for (i = 0; i < index->item_count; i++) {
  3504. int64_t time, offset;
  3505. if (version == 1) {
  3506. time = avio_rb64(f);
  3507. offset = avio_rb64(f);
  3508. } else {
  3509. time = avio_rb32(f);
  3510. offset = avio_rb32(f);
  3511. }
  3512. index->items[i].time = time;
  3513. index->items[i].moof_offset = offset;
  3514. for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
  3515. avio_r8(f);
  3516. for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
  3517. avio_r8(f);
  3518. for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
  3519. avio_r8(f);
  3520. }
  3521. avio_seek(f, pos + size, SEEK_SET);
  3522. return 0;
  3523. }
  3524. static int mov_read_mfra(MOVContext *c, AVIOContext *f)
  3525. {
  3526. int64_t stream_size = avio_size(f);
  3527. int64_t original_pos = avio_tell(f);
  3528. int64_t seek_ret;
  3529. int32_t mfra_size;
  3530. int ret = -1;
  3531. if ((seek_ret = avio_seek(f, stream_size - 4, SEEK_SET)) < 0) {
  3532. ret = seek_ret;
  3533. goto fail;
  3534. }
  3535. mfra_size = avio_rb32(f);
  3536. if (mfra_size < 0 || mfra_size > stream_size) {
  3537. av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (unreasonable size)\n");
  3538. goto fail;
  3539. }
  3540. if ((seek_ret = avio_seek(f, -mfra_size, SEEK_CUR)) < 0) {
  3541. ret = seek_ret;
  3542. goto fail;
  3543. }
  3544. if (avio_rb32(f) != mfra_size) {
  3545. av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (size mismatch)\n");
  3546. goto fail;
  3547. }
  3548. if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
  3549. av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (tag mismatch)\n");
  3550. goto fail;
  3551. }
  3552. av_log(c->fc, AV_LOG_VERBOSE, "stream has mfra\n");
  3553. do {
  3554. ret = read_tfra(c, f);
  3555. if (ret < 0)
  3556. goto fail;
  3557. } while (!ret);
  3558. ret = 0;
  3559. fail:
  3560. seek_ret = avio_seek(f, original_pos, SEEK_SET);
  3561. if (seek_ret < 0) {
  3562. av_log(c->fc, AV_LOG_ERROR,
  3563. "failed to seek back after looking for mfra\n");
  3564. ret = seek_ret;
  3565. }
  3566. return ret;
  3567. }
  3568. static int mov_read_header(AVFormatContext *s)
  3569. {
  3570. MOVContext *mov = s->priv_data;
  3571. AVIOContext *pb = s->pb;
  3572. int j, err;
  3573. MOVAtom atom = { AV_RL32("root") };
  3574. int i;
  3575. mov->fc = s;
  3576. /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  3577. if (pb->seekable)
  3578. atom.size = avio_size(pb);
  3579. else
  3580. atom.size = INT64_MAX;
  3581. /* check MOV header */
  3582. do {
  3583. if (mov->moov_retry)
  3584. avio_seek(pb, 0, SEEK_SET);
  3585. if ((err = mov_read_default(mov, pb, atom)) < 0) {
  3586. av_log(s, AV_LOG_ERROR, "error reading header\n");
  3587. mov_read_close(s);
  3588. return err;
  3589. }
  3590. } while (pb->seekable && !mov->found_moov && !mov->moov_retry++);
  3591. if (!mov->found_moov) {
  3592. av_log(s, AV_LOG_ERROR, "moov atom not found\n");
  3593. mov_read_close(s);
  3594. return AVERROR_INVALIDDATA;
  3595. }
  3596. av_log(mov->fc, AV_LOG_TRACE, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
  3597. if (pb->seekable) {
  3598. if (mov->chapter_track > 0)
  3599. mov_read_chapters(s);
  3600. for (i = 0; i < s->nb_streams; i++)
  3601. if (s->streams[i]->codec->codec_tag == AV_RL32("tmcd"))
  3602. mov_read_timecode_track(s, s->streams[i]);
  3603. }
  3604. /* copy timecode metadata from tmcd tracks to the related video streams */
  3605. for (i = 0; i < s->nb_streams; i++) {
  3606. AVStream *st = s->streams[i];
  3607. MOVStreamContext *sc = st->priv_data;
  3608. if (sc->timecode_track > 0) {
  3609. AVDictionaryEntry *tcr;
  3610. int tmcd_st_id = -1;
  3611. for (j = 0; j < s->nb_streams; j++)
  3612. if (s->streams[j]->id == sc->timecode_track)
  3613. tmcd_st_id = j;
  3614. if (tmcd_st_id < 0 || tmcd_st_id == i)
  3615. continue;
  3616. tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
  3617. if (tcr)
  3618. av_dict_set(&st->metadata, "timecode", tcr->value, 0);
  3619. }
  3620. }
  3621. export_orphan_timecode(s);
  3622. for (i = 0; i < s->nb_streams; i++) {
  3623. AVStream *st = s->streams[i];
  3624. MOVStreamContext *sc = st->priv_data;
  3625. fix_timescale(mov, sc);
  3626. if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->codec->codec_id == AV_CODEC_ID_AAC) {
  3627. st->skip_samples = sc->start_pad;
  3628. }
  3629. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
  3630. av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
  3631. sc->time_scale*(int64_t)sc->nb_frames_for_fps, sc->duration_for_fps, INT_MAX);
  3632. if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  3633. if (st->codec->width <= 0 || st->codec->height <= 0) {
  3634. st->codec->width = sc->width;
  3635. st->codec->height = sc->height;
  3636. }
  3637. if (st->codec->codec_id == AV_CODEC_ID_DVD_SUBTITLE) {
  3638. if ((err = mov_rewrite_dvd_sub_extradata(st)) < 0)
  3639. return err;
  3640. }
  3641. }
  3642. }
  3643. if (mov->trex_data) {
  3644. for (i = 0; i < s->nb_streams; i++) {
  3645. AVStream *st = s->streams[i];
  3646. MOVStreamContext *sc = st->priv_data;
  3647. if (st->duration > 0)
  3648. st->codec->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
  3649. }
  3650. }
  3651. if (mov->use_mfra_for > 0) {
  3652. for (i = 0; i < s->nb_streams; i++) {
  3653. AVStream *st = s->streams[i];
  3654. MOVStreamContext *sc = st->priv_data;
  3655. if (sc->duration_for_fps > 0) {
  3656. st->codec->bit_rate = sc->data_size * 8 * sc->time_scale /
  3657. sc->duration_for_fps;
  3658. }
  3659. }
  3660. }
  3661. for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
  3662. if (mov->bitrates[i]) {
  3663. s->streams[i]->codec->bit_rate = mov->bitrates[i];
  3664. }
  3665. }
  3666. ff_rfps_calculate(s);
  3667. for (i = 0; i < s->nb_streams; i++) {
  3668. AVStream *st = s->streams[i];
  3669. MOVStreamContext *sc = st->priv_data;
  3670. switch (st->codec->codec_type) {
  3671. case AVMEDIA_TYPE_AUDIO:
  3672. err = ff_replaygain_export(st, s->metadata);
  3673. if (err < 0) {
  3674. mov_read_close(s);
  3675. return err;
  3676. }
  3677. break;
  3678. case AVMEDIA_TYPE_VIDEO:
  3679. if (sc->display_matrix) {
  3680. AVPacketSideData *sd, *tmp;
  3681. tmp = av_realloc_array(st->side_data,
  3682. st->nb_side_data + 1, sizeof(*tmp));
  3683. if (!tmp)
  3684. return AVERROR(ENOMEM);
  3685. st->side_data = tmp;
  3686. st->nb_side_data++;
  3687. sd = &st->side_data[st->nb_side_data - 1];
  3688. sd->type = AV_PKT_DATA_DISPLAYMATRIX;
  3689. sd->size = sizeof(int32_t) * 9;
  3690. sd->data = (uint8_t*)sc->display_matrix;
  3691. sc->display_matrix = NULL;
  3692. }
  3693. break;
  3694. }
  3695. }
  3696. return 0;
  3697. }
  3698. static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
  3699. {
  3700. AVIndexEntry *sample = NULL;
  3701. int64_t best_dts = INT64_MAX;
  3702. int i;
  3703. for (i = 0; i < s->nb_streams; i++) {
  3704. AVStream *avst = s->streams[i];
  3705. MOVStreamContext *msc = avst->priv_data;
  3706. if (msc->pb && msc->current_sample < avst->nb_index_entries) {
  3707. AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
  3708. int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
  3709. av_log(s, AV_LOG_TRACE, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  3710. if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
  3711. (s->pb->seekable &&
  3712. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
  3713. ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  3714. (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
  3715. sample = current_sample;
  3716. best_dts = dts;
  3717. *st = avst;
  3718. }
  3719. }
  3720. }
  3721. return sample;
  3722. }
  3723. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  3724. {
  3725. MOVContext *mov = s->priv_data;
  3726. MOVStreamContext *sc;
  3727. AVIndexEntry *sample;
  3728. AVStream *st = NULL;
  3729. int ret;
  3730. mov->fc = s;
  3731. retry:
  3732. sample = mov_find_next_sample(s, &st);
  3733. if (!sample) {
  3734. mov->found_mdat = 0;
  3735. if (!mov->next_root_atom)
  3736. return AVERROR_EOF;
  3737. if (avio_seek(s->pb, mov->next_root_atom, SEEK_SET) != mov->next_root_atom) {
  3738. av_log(mov->fc, AV_LOG_ERROR, "next root atom offset 0x%"PRIx64": partial file\n", mov->next_root_atom);
  3739. return AVERROR_INVALIDDATA;
  3740. }
  3741. mov->next_root_atom = 0;
  3742. if (mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
  3743. avio_feof(s->pb))
  3744. return AVERROR_EOF;
  3745. av_log(s, AV_LOG_TRACE, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
  3746. goto retry;
  3747. }
  3748. sc = st->priv_data;
  3749. /* must be done just before reading, to avoid infinite loop on sample */
  3750. sc->current_sample++;
  3751. if (mov->next_root_atom) {
  3752. sample->pos = FFMIN(sample->pos, mov->next_root_atom);
  3753. sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
  3754. }
  3755. if (st->discard != AVDISCARD_ALL) {
  3756. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  3757. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  3758. sc->ffindex, sample->pos);
  3759. return AVERROR_INVALIDDATA;
  3760. }
  3761. ret = av_get_packet(sc->pb, pkt, sample->size);
  3762. if (ret < 0)
  3763. return ret;
  3764. if (sc->has_palette) {
  3765. uint8_t *pal;
  3766. pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
  3767. if (!pal) {
  3768. av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
  3769. } else {
  3770. memcpy(pal, sc->palette, AVPALETTE_SIZE);
  3771. sc->has_palette = 0;
  3772. }
  3773. }
  3774. #if CONFIG_DV_DEMUXER
  3775. if (mov->dv_demux && sc->dv_audio_container) {
  3776. avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
  3777. av_freep(&pkt->data);
  3778. pkt->size = 0;
  3779. ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
  3780. if (ret < 0)
  3781. return ret;
  3782. }
  3783. #endif
  3784. }
  3785. pkt->stream_index = sc->ffindex;
  3786. pkt->dts = sample->timestamp;
  3787. if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
  3788. pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
  3789. /* update ctts context */
  3790. sc->ctts_sample++;
  3791. if (sc->ctts_index < sc->ctts_count &&
  3792. sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
  3793. sc->ctts_index++;
  3794. sc->ctts_sample = 0;
  3795. }
  3796. if (sc->wrong_dts)
  3797. pkt->dts = AV_NOPTS_VALUE;
  3798. } else {
  3799. int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
  3800. st->index_entries[sc->current_sample].timestamp : st->duration;
  3801. pkt->duration = next_dts - pkt->dts;
  3802. pkt->pts = pkt->dts;
  3803. }
  3804. if (st->discard == AVDISCARD_ALL)
  3805. goto retry;
  3806. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
  3807. pkt->pos = sample->pos;
  3808. return 0;
  3809. }
  3810. static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
  3811. {
  3812. MOVStreamContext *sc = st->priv_data;
  3813. int sample, time_sample;
  3814. int i;
  3815. sample = av_index_search_timestamp(st, timestamp, flags);
  3816. av_log(s, AV_LOG_TRACE, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  3817. if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
  3818. sample = 0;
  3819. if (sample < 0) /* not sure what to do */
  3820. return AVERROR_INVALIDDATA;
  3821. sc->current_sample = sample;
  3822. av_log(s, AV_LOG_TRACE, "stream %d, found sample %d\n", st->index, sc->current_sample);
  3823. /* adjust ctts index */
  3824. if (sc->ctts_data) {
  3825. time_sample = 0;
  3826. for (i = 0; i < sc->ctts_count; i++) {
  3827. int next = time_sample + sc->ctts_data[i].count;
  3828. if (next > sc->current_sample) {
  3829. sc->ctts_index = i;
  3830. sc->ctts_sample = sc->current_sample - time_sample;
  3831. break;
  3832. }
  3833. time_sample = next;
  3834. }
  3835. }
  3836. return sample;
  3837. }
  3838. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  3839. {
  3840. MOVContext *mc = s->priv_data;
  3841. AVStream *st;
  3842. int sample;
  3843. int i;
  3844. if (stream_index >= s->nb_streams)
  3845. return AVERROR_INVALIDDATA;
  3846. st = s->streams[stream_index];
  3847. sample = mov_seek_stream(s, st, sample_time, flags);
  3848. if (sample < 0)
  3849. return sample;
  3850. if (mc->seek_individually) {
  3851. /* adjust seek timestamp to found sample timestamp */
  3852. int64_t seek_timestamp = st->index_entries[sample].timestamp;
  3853. for (i = 0; i < s->nb_streams; i++) {
  3854. int64_t timestamp;
  3855. MOVStreamContext *sc = s->streams[i]->priv_data;
  3856. st = s->streams[i];
  3857. st->skip_samples = (sample_time <= 0) ? sc->start_pad : 0;
  3858. if (stream_index == i)
  3859. continue;
  3860. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  3861. mov_seek_stream(s, st, timestamp, flags);
  3862. }
  3863. } else {
  3864. for (i = 0; i < s->nb_streams; i++) {
  3865. MOVStreamContext *sc;
  3866. st = s->streams[i];
  3867. sc = st->priv_data;
  3868. sc->current_sample = 0;
  3869. }
  3870. while (1) {
  3871. MOVStreamContext *sc;
  3872. AVIndexEntry *entry = mov_find_next_sample(s, &st);
  3873. if (!entry)
  3874. return AVERROR_INVALIDDATA;
  3875. sc = st->priv_data;
  3876. if (sc->ffindex == stream_index && sc->current_sample == sample)
  3877. break;
  3878. sc->current_sample++;
  3879. }
  3880. }
  3881. return 0;
  3882. }
  3883. #define OFFSET(x) offsetof(MOVContext, x)
  3884. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  3885. static const AVOption mov_options[] = {
  3886. {"use_absolute_path",
  3887. "allow using absolute path when opening alias, this is a possible security issue",
  3888. OFFSET(use_absolute_path), FF_OPT_TYPE_INT, {.i64 = 0},
  3889. 0, 1, FLAGS},
  3890. {"seek_streams_individually",
  3891. "Seek each stream individually to the to the closest point",
  3892. OFFSET(seek_individually), AV_OPT_TYPE_INT, { .i64 = 1 },
  3893. 0, 1, FLAGS},
  3894. {"ignore_editlist", "", OFFSET(ignore_editlist), FF_OPT_TYPE_INT, {.i64 = 0},
  3895. 0, 1, FLAGS},
  3896. {"use_mfra_for",
  3897. "use mfra for fragment timestamps",
  3898. OFFSET(use_mfra_for), FF_OPT_TYPE_INT, {.i64 = FF_MOV_FLAG_MFRA_AUTO},
  3899. -1, FF_MOV_FLAG_MFRA_PTS, FLAGS,
  3900. "use_mfra_for"},
  3901. {"auto", "auto", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_AUTO}, 0, 0,
  3902. FLAGS, "use_mfra_for" },
  3903. {"dts", "dts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_DTS}, 0, 0,
  3904. FLAGS, "use_mfra_for" },
  3905. {"pts", "pts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_PTS}, 0, 0,
  3906. FLAGS, "use_mfra_for" },
  3907. { "export_all", "Export unrecognized metadata entries", OFFSET(export_all),
  3908. AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = FLAGS },
  3909. { "export_xmp", "Export full XMP metadata", OFFSET(export_xmp),
  3910. AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = FLAGS },
  3911. { NULL },
  3912. };
  3913. static const AVClass mov_class = {
  3914. .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
  3915. .item_name = av_default_item_name,
  3916. .option = mov_options,
  3917. .version = LIBAVUTIL_VERSION_INT,
  3918. };
  3919. AVInputFormat ff_mov_demuxer = {
  3920. .name = "mov,mp4,m4a,3gp,3g2,mj2",
  3921. .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
  3922. .priv_class = &mov_class,
  3923. .priv_data_size = sizeof(MOVContext),
  3924. .extensions = "mov,mp4,m4a,3gp,3g2,mj2",
  3925. .read_probe = mov_probe,
  3926. .read_header = mov_read_header,
  3927. .read_packet = mov_read_packet,
  3928. .read_close = mov_read_close,
  3929. .read_seek = mov_read_seek,
  3930. .flags = AVFMT_NO_BYTE_SEEK,
  3931. };