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.

5599 lines
188KB

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