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.

5093 lines
170KB

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