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.

8182 lines
281KB

  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/avassert.h"
  36. #include "libavutil/avstring.h"
  37. #include "libavutil/dict.h"
  38. #include "libavutil/display.h"
  39. #include "libavutil/opt.h"
  40. #include "libavutil/aes.h"
  41. #include "libavutil/aes_ctr.h"
  42. #include "libavutil/pixdesc.h"
  43. #include "libavutil/sha.h"
  44. #include "libavutil/spherical.h"
  45. #include "libavutil/stereo3d.h"
  46. #include "libavutil/timecode.h"
  47. #include "libavutil/dovi_meta.h"
  48. #include "libavcodec/ac3tab.h"
  49. #include "libavcodec/flac.h"
  50. #include "libavcodec/mpegaudiodecheader.h"
  51. #include "libavcodec/mlp_parse.h"
  52. #include "avformat.h"
  53. #include "internal.h"
  54. #include "avio_internal.h"
  55. #include "riff.h"
  56. #include "isom.h"
  57. #include "libavcodec/get_bits.h"
  58. #include "id3v1.h"
  59. #include "mov_chan.h"
  60. #include "replaygain.h"
  61. #if CONFIG_ZLIB
  62. #include <zlib.h>
  63. #endif
  64. #include "qtpalette.h"
  65. /* those functions parse an atom */
  66. /* links atom IDs to parse functions */
  67. typedef struct MOVParseTableEntry {
  68. uint32_t type;
  69. int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
  70. } MOVParseTableEntry;
  71. static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
  72. static int mov_read_mfra(MOVContext *c, AVIOContext *f);
  73. static int64_t add_ctts_entry(MOVStts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size,
  74. int count, int duration);
  75. static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
  76. unsigned len, const char *key)
  77. {
  78. char buf[16];
  79. short current, total = 0;
  80. avio_rb16(pb); // unknown
  81. current = avio_rb16(pb);
  82. if (len >= 6)
  83. total = avio_rb16(pb);
  84. if (!total)
  85. snprintf(buf, sizeof(buf), "%d", current);
  86. else
  87. snprintf(buf, sizeof(buf), "%d/%d", current, total);
  88. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  89. av_dict_set(&c->fc->metadata, key, buf, 0);
  90. return 0;
  91. }
  92. static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
  93. unsigned len, const char *key)
  94. {
  95. /* bypass padding bytes */
  96. avio_r8(pb);
  97. avio_r8(pb);
  98. avio_r8(pb);
  99. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  100. av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
  101. return 0;
  102. }
  103. static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
  104. unsigned len, const char *key)
  105. {
  106. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  107. av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
  108. return 0;
  109. }
  110. static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
  111. unsigned len, const char *key)
  112. {
  113. short genre;
  114. avio_r8(pb); // unknown
  115. genre = avio_r8(pb);
  116. if (genre < 1 || genre > ID3v1_GENRE_MAX)
  117. return 0;
  118. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  119. av_dict_set(&c->fc->metadata, key, ff_id3v1_genre_str[genre-1], 0);
  120. return 0;
  121. }
  122. static const uint32_t mac_to_unicode[128] = {
  123. 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
  124. 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
  125. 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
  126. 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
  127. 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
  128. 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
  129. 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
  130. 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
  131. 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
  132. 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
  133. 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
  134. 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
  135. 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
  136. 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
  137. 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
  138. 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
  139. };
  140. static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
  141. char *dst, int dstlen)
  142. {
  143. char *p = dst;
  144. char *end = dst+dstlen-1;
  145. int i;
  146. for (i = 0; i < len; i++) {
  147. uint8_t t, c = avio_r8(pb);
  148. if (p >= end)
  149. continue;
  150. if (c < 0x80)
  151. *p++ = c;
  152. else if (p < end)
  153. PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
  154. }
  155. *p = 0;
  156. return p - dst;
  157. }
  158. static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
  159. {
  160. AVStream *st;
  161. MOVStreamContext *sc;
  162. enum AVCodecID id;
  163. int ret;
  164. switch (type) {
  165. case 0xd: id = AV_CODEC_ID_MJPEG; break;
  166. case 0xe: id = AV_CODEC_ID_PNG; break;
  167. case 0x1b: id = AV_CODEC_ID_BMP; break;
  168. default:
  169. av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
  170. avio_skip(pb, len);
  171. return 0;
  172. }
  173. st = avformat_new_stream(c->fc, NULL);
  174. if (!st)
  175. return AVERROR(ENOMEM);
  176. sc = av_mallocz(sizeof(*sc));
  177. if (!sc)
  178. return AVERROR(ENOMEM);
  179. st->priv_data = sc;
  180. ret = av_get_packet(pb, &st->attached_pic, len);
  181. if (ret < 0)
  182. return ret;
  183. if (st->attached_pic.size >= 8 && id != AV_CODEC_ID_BMP) {
  184. if (AV_RB64(st->attached_pic.data) == 0x89504e470d0a1a0a) {
  185. id = AV_CODEC_ID_PNG;
  186. } else {
  187. id = AV_CODEC_ID_MJPEG;
  188. }
  189. }
  190. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  191. st->attached_pic.stream_index = st->index;
  192. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  193. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  194. st->codecpar->codec_id = id;
  195. return 0;
  196. }
  197. // 3GPP TS 26.244
  198. static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
  199. {
  200. char language[4] = { 0 };
  201. char buf[200], place[100];
  202. uint16_t langcode = 0;
  203. double longitude, latitude, altitude;
  204. const char *key = "location";
  205. if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4) {
  206. av_log(c->fc, AV_LOG_ERROR, "loci too short\n");
  207. return AVERROR_INVALIDDATA;
  208. }
  209. avio_skip(pb, 4); // version+flags
  210. langcode = avio_rb16(pb);
  211. ff_mov_lang_to_iso639(langcode, language);
  212. len -= 6;
  213. len -= avio_get_str(pb, len, place, sizeof(place));
  214. if (len < 1) {
  215. av_log(c->fc, AV_LOG_ERROR, "place name too long\n");
  216. return AVERROR_INVALIDDATA;
  217. }
  218. avio_skip(pb, 1); // role
  219. len -= 1;
  220. if (len < 12) {
  221. av_log(c->fc, AV_LOG_ERROR,
  222. "loci too short (%u bytes left, need at least %d)\n", len, 12);
  223. return AVERROR_INVALIDDATA;
  224. }
  225. longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
  226. latitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
  227. altitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
  228. // Try to output in the same format as the ?xyz field
  229. snprintf(buf, sizeof(buf), "%+08.4f%+09.4f", latitude, longitude);
  230. if (altitude)
  231. av_strlcatf(buf, sizeof(buf), "%+f", altitude);
  232. av_strlcatf(buf, sizeof(buf), "/%s", place);
  233. if (*language && strcmp(language, "und")) {
  234. char key2[16];
  235. snprintf(key2, sizeof(key2), "%s-%s", key, language);
  236. av_dict_set(&c->fc->metadata, key2, buf, 0);
  237. }
  238. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  239. return av_dict_set(&c->fc->metadata, key, buf, 0);
  240. }
  241. static int mov_metadata_hmmt(MOVContext *c, AVIOContext *pb, unsigned len)
  242. {
  243. int i, n_hmmt;
  244. if (len < 2)
  245. return 0;
  246. if (c->ignore_chapters)
  247. return 0;
  248. n_hmmt = avio_rb32(pb);
  249. for (i = 0; i < n_hmmt && !pb->eof_reached; i++) {
  250. int moment_time = avio_rb32(pb);
  251. avpriv_new_chapter(c->fc, i, av_make_q(1, 1000), moment_time, AV_NOPTS_VALUE, NULL);
  252. }
  253. return 0;
  254. }
  255. static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  256. {
  257. char tmp_key[AV_FOURCC_MAX_STRING_SIZE] = {0};
  258. char key2[32], language[4] = {0};
  259. char *str = NULL;
  260. const char *key = NULL;
  261. uint16_t langcode = 0;
  262. uint32_t data_type = 0, str_size, str_size_alloc;
  263. int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
  264. int raw = 0;
  265. int num = 0;
  266. switch (atom.type) {
  267. case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break;
  268. case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break;
  269. case MKTAG( 'X','M','P','_'):
  270. if (c->export_xmp) { key = "xmp"; raw = 1; } break;
  271. case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
  272. case MKTAG( 'a','k','I','D'): key = "account_type";
  273. parse = mov_metadata_int8_no_padding; break;
  274. case MKTAG( 'a','p','I','D'): key = "account_id"; break;
  275. case MKTAG( 'c','a','t','g'): key = "category"; break;
  276. case MKTAG( 'c','p','i','l'): key = "compilation";
  277. parse = mov_metadata_int8_no_padding; break;
  278. case MKTAG( 'c','p','r','t'): key = "copyright"; break;
  279. case MKTAG( 'd','e','s','c'): key = "description"; break;
  280. case MKTAG( 'd','i','s','k'): key = "disc";
  281. parse = mov_metadata_track_or_disc_number; break;
  282. case MKTAG( 'e','g','i','d'): key = "episode_uid";
  283. parse = mov_metadata_int8_no_padding; break;
  284. case MKTAG( 'F','I','R','M'): key = "firmware"; raw = 1; break;
  285. case MKTAG( 'g','n','r','e'): key = "genre";
  286. parse = mov_metadata_gnre; break;
  287. case MKTAG( 'h','d','v','d'): key = "hd_video";
  288. parse = mov_metadata_int8_no_padding; break;
  289. case MKTAG( 'H','M','M','T'):
  290. return mov_metadata_hmmt(c, pb, atom.size);
  291. case MKTAG( 'k','e','y','w'): key = "keywords"; break;
  292. case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
  293. case MKTAG( 'l','o','c','i'):
  294. return mov_metadata_loci(c, pb, atom.size);
  295. case MKTAG( 'm','a','n','u'): key = "make"; break;
  296. case MKTAG( 'm','o','d','l'): key = "model"; break;
  297. case MKTAG( 'p','c','s','t'): key = "podcast";
  298. parse = mov_metadata_int8_no_padding; break;
  299. case MKTAG( 'p','g','a','p'): key = "gapless_playback";
  300. parse = mov_metadata_int8_no_padding; break;
  301. case MKTAG( 'p','u','r','d'): key = "purchase_date"; break;
  302. case MKTAG( 'r','t','n','g'): key = "rating";
  303. parse = mov_metadata_int8_no_padding; break;
  304. case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break;
  305. case MKTAG( 's','o','a','l'): key = "sort_album"; break;
  306. case MKTAG( 's','o','a','r'): key = "sort_artist"; break;
  307. case MKTAG( 's','o','c','o'): key = "sort_composer"; break;
  308. case MKTAG( 's','o','n','m'): key = "sort_name"; break;
  309. case MKTAG( 's','o','s','n'): key = "sort_show"; break;
  310. case MKTAG( 's','t','i','k'): key = "media_type";
  311. parse = mov_metadata_int8_no_padding; break;
  312. case MKTAG( 't','r','k','n'): key = "track";
  313. parse = mov_metadata_track_or_disc_number; break;
  314. case MKTAG( 't','v','e','n'): key = "episode_id"; break;
  315. case MKTAG( 't','v','e','s'): key = "episode_sort";
  316. parse = mov_metadata_int8_bypass_padding; break;
  317. case MKTAG( 't','v','n','n'): key = "network"; break;
  318. case MKTAG( 't','v','s','h'): key = "show"; break;
  319. case MKTAG( 't','v','s','n'): key = "season_number";
  320. parse = mov_metadata_int8_bypass_padding; break;
  321. case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
  322. case MKTAG(0xa9,'P','R','D'): key = "producer"; break;
  323. case MKTAG(0xa9,'a','l','b'): key = "album"; break;
  324. case MKTAG(0xa9,'a','u','t'): key = "artist"; break;
  325. case MKTAG(0xa9,'c','h','p'): key = "chapter"; break;
  326. case MKTAG(0xa9,'c','m','t'): key = "comment"; break;
  327. case MKTAG(0xa9,'c','o','m'): key = "composer"; break;
  328. case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
  329. case MKTAG(0xa9,'d','a','y'): key = "date"; break;
  330. case MKTAG(0xa9,'d','i','r'): key = "director"; break;
  331. case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break;
  332. case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break;
  333. case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
  334. case MKTAG(0xa9,'f','m','t'): key = "original_format"; break;
  335. case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
  336. case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
  337. case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break;
  338. case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
  339. case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
  340. case MKTAG(0xa9,'m','a','k'): key = "make"; break;
  341. case MKTAG(0xa9,'m','o','d'): key = "model"; break;
  342. case MKTAG(0xa9,'n','a','m'): key = "title"; break;
  343. case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break;
  344. case MKTAG(0xa9,'p','r','d'): key = "producer"; break;
  345. case MKTAG(0xa9,'p','r','f'): key = "performers"; break;
  346. case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break;
  347. case MKTAG(0xa9,'s','r','c'): key = "original_source"; break;
  348. case MKTAG(0xa9,'s','t','3'): key = "subtitle"; break;
  349. case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
  350. case MKTAG(0xa9,'t','o','o'): key = "encoder"; break;
  351. case MKTAG(0xa9,'t','r','k'): key = "track"; break;
  352. case MKTAG(0xa9,'u','r','l'): key = "URL"; break;
  353. case MKTAG(0xa9,'w','r','n'): key = "warning"; break;
  354. case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
  355. case MKTAG(0xa9,'x','y','z'): key = "location"; break;
  356. }
  357. retry:
  358. if (c->itunes_metadata && atom.size > 8) {
  359. int data_size = avio_rb32(pb);
  360. int tag = avio_rl32(pb);
  361. if (tag == MKTAG('d','a','t','a') && data_size <= atom.size) {
  362. data_type = avio_rb32(pb); // type
  363. avio_rb32(pb); // unknown
  364. str_size = data_size - 16;
  365. atom.size -= 16;
  366. if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
  367. int ret = mov_read_covr(c, pb, data_type, str_size);
  368. if (ret < 0) {
  369. av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
  370. return ret;
  371. }
  372. atom.size -= str_size;
  373. if (atom.size > 8)
  374. goto retry;
  375. return ret;
  376. } else if (!key && c->found_hdlr_mdta && c->meta_keys) {
  377. uint32_t index = AV_RB32(&atom.type);
  378. if (index < c->meta_keys_count && index > 0) {
  379. key = c->meta_keys[index];
  380. } else {
  381. av_log(c->fc, AV_LOG_WARNING,
  382. "The index of 'data' is out of range: %"PRId32" < 1 or >= %d.\n",
  383. index, c->meta_keys_count);
  384. }
  385. }
  386. } else return 0;
  387. } else if (atom.size > 4 && key && !c->itunes_metadata && !raw) {
  388. str_size = avio_rb16(pb); // string length
  389. if (str_size > atom.size) {
  390. raw = 1;
  391. avio_seek(pb, -2, SEEK_CUR);
  392. av_log(c->fc, AV_LOG_WARNING, "UDTA parsing failed retrying raw\n");
  393. goto retry;
  394. }
  395. langcode = avio_rb16(pb);
  396. ff_mov_lang_to_iso639(langcode, language);
  397. atom.size -= 4;
  398. } else
  399. str_size = atom.size;
  400. if (c->export_all && !key) {
  401. key = av_fourcc_make_string(tmp_key, atom.type);
  402. }
  403. if (!key)
  404. return 0;
  405. if (atom.size < 0 || str_size >= INT_MAX/2)
  406. return AVERROR_INVALIDDATA;
  407. // Allocates enough space if data_type is a int32 or float32 number, otherwise
  408. // worst-case requirement for output string in case of utf8 coded input
  409. num = (data_type >= 21 && data_type <= 23);
  410. str_size_alloc = (num ? 512 : (raw ? str_size : str_size * 2)) + 1;
  411. str = av_mallocz(str_size_alloc);
  412. if (!str)
  413. return AVERROR(ENOMEM);
  414. if (parse)
  415. parse(c, pb, str_size, key);
  416. else {
  417. if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded
  418. mov_read_mac_string(c, pb, str_size, str, str_size_alloc);
  419. } else if (data_type == 21) { // BE signed integer, variable size
  420. int val = 0;
  421. if (str_size == 1)
  422. val = (int8_t)avio_r8(pb);
  423. else if (str_size == 2)
  424. val = (int16_t)avio_rb16(pb);
  425. else if (str_size == 3)
  426. val = ((int32_t)(avio_rb24(pb)<<8))>>8;
  427. else if (str_size == 4)
  428. val = (int32_t)avio_rb32(pb);
  429. if (snprintf(str, str_size_alloc, "%d", val) >= str_size_alloc) {
  430. av_log(c->fc, AV_LOG_ERROR,
  431. "Failed to store the number (%d) in string.\n", val);
  432. av_free(str);
  433. return AVERROR_INVALIDDATA;
  434. }
  435. } else if (data_type == 22) { // BE unsigned integer, variable size
  436. unsigned int val = 0;
  437. if (str_size == 1)
  438. val = avio_r8(pb);
  439. else if (str_size == 2)
  440. val = avio_rb16(pb);
  441. else if (str_size == 3)
  442. val = avio_rb24(pb);
  443. else if (str_size == 4)
  444. val = avio_rb32(pb);
  445. if (snprintf(str, str_size_alloc, "%u", val) >= str_size_alloc) {
  446. av_log(c->fc, AV_LOG_ERROR,
  447. "Failed to store the number (%u) in string.\n", val);
  448. av_free(str);
  449. return AVERROR_INVALIDDATA;
  450. }
  451. } else if (data_type == 23 && str_size >= 4) { // BE float32
  452. float val = av_int2float(avio_rb32(pb));
  453. if (snprintf(str, str_size_alloc, "%f", val) >= str_size_alloc) {
  454. av_log(c->fc, AV_LOG_ERROR,
  455. "Failed to store the float32 number (%f) in string.\n", val);
  456. av_free(str);
  457. return AVERROR_INVALIDDATA;
  458. }
  459. } else {
  460. int ret = ffio_read_size(pb, str, str_size);
  461. if (ret < 0) {
  462. av_free(str);
  463. return ret;
  464. }
  465. str[str_size] = 0;
  466. }
  467. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  468. av_dict_set(&c->fc->metadata, key, str, 0);
  469. if (*language && strcmp(language, "und")) {
  470. snprintf(key2, sizeof(key2), "%s-%s", key, language);
  471. av_dict_set(&c->fc->metadata, key2, str, 0);
  472. }
  473. if (!strcmp(key, "encoder")) {
  474. int major, minor, micro;
  475. if (sscanf(str, "HandBrake %d.%d.%d", &major, &minor, &micro) == 3) {
  476. c->handbrake_version = 1000000*major + 1000*minor + micro;
  477. }
  478. }
  479. }
  480. av_freep(&str);
  481. return 0;
  482. }
  483. static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  484. {
  485. int64_t start;
  486. int i, nb_chapters, str_len, version;
  487. char str[256+1];
  488. int ret;
  489. if (c->ignore_chapters)
  490. return 0;
  491. if ((atom.size -= 5) < 0)
  492. return 0;
  493. version = avio_r8(pb);
  494. avio_rb24(pb);
  495. if (version)
  496. avio_rb32(pb); // ???
  497. nb_chapters = avio_r8(pb);
  498. for (i = 0; i < nb_chapters; i++) {
  499. if (atom.size < 9)
  500. return 0;
  501. start = avio_rb64(pb);
  502. str_len = avio_r8(pb);
  503. if ((atom.size -= 9+str_len) < 0)
  504. return 0;
  505. ret = ffio_read_size(pb, str, str_len);
  506. if (ret < 0)
  507. return ret;
  508. str[str_len] = 0;
  509. avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
  510. }
  511. return 0;
  512. }
  513. #define MIN_DATA_ENTRY_BOX_SIZE 12
  514. static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  515. {
  516. AVStream *st;
  517. MOVStreamContext *sc;
  518. int entries, i, j;
  519. if (c->fc->nb_streams < 1)
  520. return 0;
  521. st = c->fc->streams[c->fc->nb_streams-1];
  522. sc = st->priv_data;
  523. avio_rb32(pb); // version + flags
  524. entries = avio_rb32(pb);
  525. if (!entries ||
  526. entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
  527. entries >= UINT_MAX / sizeof(*sc->drefs))
  528. return AVERROR_INVALIDDATA;
  529. sc->drefs_count = 0;
  530. av_free(sc->drefs);
  531. sc->drefs_count = 0;
  532. sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
  533. if (!sc->drefs)
  534. return AVERROR(ENOMEM);
  535. sc->drefs_count = entries;
  536. for (i = 0; i < entries; i++) {
  537. MOVDref *dref = &sc->drefs[i];
  538. uint32_t size = avio_rb32(pb);
  539. int64_t next = avio_tell(pb) + size - 4;
  540. if (size < 12)
  541. return AVERROR_INVALIDDATA;
  542. dref->type = avio_rl32(pb);
  543. avio_rb32(pb); // version + flags
  544. if (dref->type == MKTAG('a','l','i','s') && size > 150) {
  545. /* macintosh alias record */
  546. uint16_t volume_len, len;
  547. int16_t type;
  548. int ret;
  549. avio_skip(pb, 10);
  550. volume_len = avio_r8(pb);
  551. volume_len = FFMIN(volume_len, 27);
  552. ret = ffio_read_size(pb, dref->volume, 27);
  553. if (ret < 0)
  554. return ret;
  555. dref->volume[volume_len] = 0;
  556. av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
  557. avio_skip(pb, 12);
  558. len = avio_r8(pb);
  559. len = FFMIN(len, 63);
  560. ret = ffio_read_size(pb, dref->filename, 63);
  561. if (ret < 0)
  562. return ret;
  563. dref->filename[len] = 0;
  564. av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
  565. avio_skip(pb, 16);
  566. /* read next level up_from_alias/down_to_target */
  567. dref->nlvl_from = avio_rb16(pb);
  568. dref->nlvl_to = avio_rb16(pb);
  569. av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
  570. dref->nlvl_from, dref->nlvl_to);
  571. avio_skip(pb, 16);
  572. for (type = 0; type != -1 && avio_tell(pb) < next; ) {
  573. if (avio_feof(pb))
  574. return AVERROR_EOF;
  575. type = avio_rb16(pb);
  576. len = avio_rb16(pb);
  577. av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
  578. if (len&1)
  579. len += 1;
  580. if (type == 2) { // absolute path
  581. av_free(dref->path);
  582. dref->path = av_mallocz(len+1);
  583. if (!dref->path)
  584. return AVERROR(ENOMEM);
  585. ret = ffio_read_size(pb, dref->path, len);
  586. if (ret < 0) {
  587. av_freep(&dref->path);
  588. return ret;
  589. }
  590. if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
  591. len -= volume_len;
  592. memmove(dref->path, dref->path+volume_len, len);
  593. dref->path[len] = 0;
  594. }
  595. // trim string of any ending zeros
  596. for (j = len - 1; j >= 0; j--) {
  597. if (dref->path[j] == 0)
  598. len--;
  599. else
  600. break;
  601. }
  602. for (j = 0; j < len; j++)
  603. if (dref->path[j] == ':' || dref->path[j] == 0)
  604. dref->path[j] = '/';
  605. av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
  606. } else if (type == 0) { // directory name
  607. av_free(dref->dir);
  608. dref->dir = av_malloc(len+1);
  609. if (!dref->dir)
  610. return AVERROR(ENOMEM);
  611. ret = ffio_read_size(pb, dref->dir, len);
  612. if (ret < 0) {
  613. av_freep(&dref->dir);
  614. return ret;
  615. }
  616. dref->dir[len] = 0;
  617. for (j = 0; j < len; j++)
  618. if (dref->dir[j] == ':')
  619. dref->dir[j] = '/';
  620. av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
  621. } else
  622. avio_skip(pb, len);
  623. }
  624. } else {
  625. av_log(c->fc, AV_LOG_DEBUG, "Unknown dref type 0x%08"PRIx32" size %"PRIu32"\n",
  626. dref->type, size);
  627. entries--;
  628. i--;
  629. }
  630. avio_seek(pb, next, SEEK_SET);
  631. }
  632. return 0;
  633. }
  634. static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  635. {
  636. AVStream *st;
  637. uint32_t type;
  638. uint32_t ctype;
  639. int64_t title_size;
  640. char *title_str;
  641. int ret;
  642. avio_r8(pb); /* version */
  643. avio_rb24(pb); /* flags */
  644. /* component type */
  645. ctype = avio_rl32(pb);
  646. type = avio_rl32(pb); /* component subtype */
  647. av_log(c->fc, AV_LOG_TRACE, "ctype=%s\n", av_fourcc2str(ctype));
  648. av_log(c->fc, AV_LOG_TRACE, "stype=%s\n", av_fourcc2str(type));
  649. if (c->trak_index < 0) { // meta not inside a trak
  650. if (type == MKTAG('m','d','t','a')) {
  651. c->found_hdlr_mdta = 1;
  652. }
  653. return 0;
  654. }
  655. st = c->fc->streams[c->fc->nb_streams-1];
  656. if (type == MKTAG('v','i','d','e'))
  657. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  658. else if (type == MKTAG('s','o','u','n'))
  659. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  660. else if (type == MKTAG('m','1','a',' '))
  661. st->codecpar->codec_id = AV_CODEC_ID_MP2;
  662. else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
  663. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  664. avio_rb32(pb); /* component manufacture */
  665. avio_rb32(pb); /* component flags */
  666. avio_rb32(pb); /* component flags mask */
  667. title_size = atom.size - 24;
  668. if (title_size > 0) {
  669. if (title_size > FFMIN(INT_MAX, SIZE_MAX-1))
  670. return AVERROR_INVALIDDATA;
  671. title_str = av_malloc(title_size + 1); /* Add null terminator */
  672. if (!title_str)
  673. return AVERROR(ENOMEM);
  674. ret = ffio_read_size(pb, title_str, title_size);
  675. if (ret < 0) {
  676. av_freep(&title_str);
  677. return ret;
  678. }
  679. title_str[title_size] = 0;
  680. if (title_str[0]) {
  681. int off = (!c->isom && title_str[0] == title_size - 1);
  682. // flag added so as to not set stream handler name if already set from mdia->hdlr
  683. av_dict_set(&st->metadata, "handler_name", title_str + off, AV_DICT_DONT_OVERWRITE);
  684. }
  685. av_freep(&title_str);
  686. }
  687. return 0;
  688. }
  689. static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  690. {
  691. return ff_mov_read_esds(c->fc, pb);
  692. }
  693. static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  694. {
  695. AVStream *st;
  696. enum AVAudioServiceType *ast;
  697. int ac3info, acmod, lfeon, bsmod;
  698. if (c->fc->nb_streams < 1)
  699. return 0;
  700. st = c->fc->streams[c->fc->nb_streams-1];
  701. ast = (enum AVAudioServiceType*)av_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE,
  702. sizeof(*ast));
  703. if (!ast)
  704. return AVERROR(ENOMEM);
  705. ac3info = avio_rb24(pb);
  706. bsmod = (ac3info >> 14) & 0x7;
  707. acmod = (ac3info >> 11) & 0x7;
  708. lfeon = (ac3info >> 10) & 0x1;
  709. st->codecpar->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
  710. st->codecpar->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
  711. if (lfeon)
  712. st->codecpar->channel_layout |= AV_CH_LOW_FREQUENCY;
  713. *ast = bsmod;
  714. if (st->codecpar->channels > 1 && bsmod == 0x7)
  715. *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  716. #if FF_API_LAVF_AVCTX
  717. FF_DISABLE_DEPRECATION_WARNINGS
  718. st->codec->audio_service_type = *ast;
  719. FF_ENABLE_DEPRECATION_WARNINGS
  720. #endif
  721. return 0;
  722. }
  723. static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  724. {
  725. AVStream *st;
  726. enum AVAudioServiceType *ast;
  727. int eac3info, acmod, lfeon, bsmod;
  728. if (c->fc->nb_streams < 1)
  729. return 0;
  730. st = c->fc->streams[c->fc->nb_streams-1];
  731. ast = (enum AVAudioServiceType*)av_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE,
  732. sizeof(*ast));
  733. if (!ast)
  734. return AVERROR(ENOMEM);
  735. /* No need to parse fields for additional independent substreams and its
  736. * associated dependent substreams since libavcodec's E-AC-3 decoder
  737. * does not support them yet. */
  738. avio_rb16(pb); /* data_rate and num_ind_sub */
  739. eac3info = avio_rb24(pb);
  740. bsmod = (eac3info >> 12) & 0x1f;
  741. acmod = (eac3info >> 9) & 0x7;
  742. lfeon = (eac3info >> 8) & 0x1;
  743. st->codecpar->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
  744. if (lfeon)
  745. st->codecpar->channel_layout |= AV_CH_LOW_FREQUENCY;
  746. st->codecpar->channels = av_get_channel_layout_nb_channels(st->codecpar->channel_layout);
  747. *ast = bsmod;
  748. if (st->codecpar->channels > 1 && bsmod == 0x7)
  749. *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  750. #if FF_API_LAVF_AVCTX
  751. FF_DISABLE_DEPRECATION_WARNINGS
  752. st->codec->audio_service_type = *ast;
  753. FF_ENABLE_DEPRECATION_WARNINGS
  754. #endif
  755. return 0;
  756. }
  757. static int mov_read_ddts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  758. {
  759. #define DDTS_SIZE 20
  760. uint8_t buf[DDTS_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  761. AVStream *st = NULL;
  762. uint32_t frame_duration_code = 0;
  763. uint32_t channel_layout_code = 0;
  764. GetBitContext gb;
  765. int ret;
  766. if ((ret = ffio_read_size(pb, buf, DDTS_SIZE)) < 0)
  767. return ret;
  768. init_get_bits(&gb, buf, 8 * DDTS_SIZE);
  769. if (c->fc->nb_streams < 1) {
  770. return 0;
  771. }
  772. st = c->fc->streams[c->fc->nb_streams-1];
  773. st->codecpar->sample_rate = get_bits_long(&gb, 32);
  774. if (st->codecpar->sample_rate <= 0) {
  775. av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
  776. return AVERROR_INVALIDDATA;
  777. }
  778. skip_bits_long(&gb, 32); /* max bitrate */
  779. st->codecpar->bit_rate = get_bits_long(&gb, 32);
  780. st->codecpar->bits_per_coded_sample = get_bits(&gb, 8);
  781. frame_duration_code = get_bits(&gb, 2);
  782. skip_bits(&gb, 30); /* various fields */
  783. channel_layout_code = get_bits(&gb, 16);
  784. st->codecpar->frame_size =
  785. (frame_duration_code == 0) ? 512 :
  786. (frame_duration_code == 1) ? 1024 :
  787. (frame_duration_code == 2) ? 2048 :
  788. (frame_duration_code == 3) ? 4096 : 0;
  789. if (channel_layout_code > 0xff) {
  790. av_log(c->fc, AV_LOG_WARNING, "Unsupported DTS audio channel layout\n");
  791. }
  792. st->codecpar->channel_layout =
  793. ((channel_layout_code & 0x1) ? AV_CH_FRONT_CENTER : 0) |
  794. ((channel_layout_code & 0x2) ? AV_CH_FRONT_LEFT : 0) |
  795. ((channel_layout_code & 0x2) ? AV_CH_FRONT_RIGHT : 0) |
  796. ((channel_layout_code & 0x4) ? AV_CH_SIDE_LEFT : 0) |
  797. ((channel_layout_code & 0x4) ? AV_CH_SIDE_RIGHT : 0) |
  798. ((channel_layout_code & 0x8) ? AV_CH_LOW_FREQUENCY : 0);
  799. st->codecpar->channels = av_get_channel_layout_nb_channels(st->codecpar->channel_layout);
  800. return 0;
  801. }
  802. static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  803. {
  804. AVStream *st;
  805. if (c->fc->nb_streams < 1)
  806. return 0;
  807. st = c->fc->streams[c->fc->nb_streams-1];
  808. if (atom.size < 16)
  809. return 0;
  810. /* skip version and flags */
  811. avio_skip(pb, 4);
  812. ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
  813. return 0;
  814. }
  815. static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  816. {
  817. AVStream *st;
  818. int ret;
  819. if (c->fc->nb_streams < 1)
  820. return 0;
  821. st = c->fc->streams[c->fc->nb_streams-1];
  822. if ((ret = ff_get_wav_header(c->fc, pb, st->codecpar, atom.size, 0)) < 0)
  823. av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
  824. return ret;
  825. }
  826. static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  827. {
  828. const int num = avio_rb32(pb);
  829. const int den = avio_rb32(pb);
  830. AVStream *st;
  831. if (c->fc->nb_streams < 1)
  832. return 0;
  833. st = c->fc->streams[c->fc->nb_streams-1];
  834. if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
  835. (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
  836. av_log(c->fc, AV_LOG_WARNING,
  837. "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
  838. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  839. num, den);
  840. } else if (den != 0) {
  841. av_reduce(&st->sample_aspect_ratio.num, &st->sample_aspect_ratio.den,
  842. num, den, 32767);
  843. }
  844. return 0;
  845. }
  846. /* this atom contains actual media data */
  847. static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  848. {
  849. if (atom.size == 0) /* wrong one (MP4) */
  850. return 0;
  851. c->found_mdat=1;
  852. return 0; /* now go for moov */
  853. }
  854. #define DRM_BLOB_SIZE 56
  855. static int mov_read_adrm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  856. {
  857. uint8_t intermediate_key[20];
  858. uint8_t intermediate_iv[20];
  859. uint8_t input[64];
  860. uint8_t output[64];
  861. uint8_t file_checksum[20];
  862. uint8_t calculated_checksum[20];
  863. struct AVSHA *sha;
  864. int i;
  865. int ret = 0;
  866. uint8_t *activation_bytes = c->activation_bytes;
  867. uint8_t *fixed_key = c->audible_fixed_key;
  868. c->aax_mode = 1;
  869. sha = av_sha_alloc();
  870. if (!sha)
  871. return AVERROR(ENOMEM);
  872. av_free(c->aes_decrypt);
  873. c->aes_decrypt = av_aes_alloc();
  874. if (!c->aes_decrypt) {
  875. ret = AVERROR(ENOMEM);
  876. goto fail;
  877. }
  878. /* drm blob processing */
  879. avio_read(pb, output, 8); // go to offset 8, absolute position 0x251
  880. avio_read(pb, input, DRM_BLOB_SIZE);
  881. avio_read(pb, output, 4); // go to offset 4, absolute position 0x28d
  882. avio_read(pb, file_checksum, 20);
  883. av_log(c->fc, AV_LOG_INFO, "[aax] file checksum == "); // required by external tools
  884. for (i = 0; i < 20; i++)
  885. av_log(c->fc, AV_LOG_INFO, "%02x", file_checksum[i]);
  886. av_log(c->fc, AV_LOG_INFO, "\n");
  887. /* verify activation data */
  888. if (!activation_bytes) {
  889. av_log(c->fc, AV_LOG_WARNING, "[aax] activation_bytes option is missing!\n");
  890. ret = 0; /* allow ffprobe to continue working on .aax files */
  891. goto fail;
  892. }
  893. if (c->activation_bytes_size != 4) {
  894. av_log(c->fc, AV_LOG_FATAL, "[aax] activation_bytes value needs to be 4 bytes!\n");
  895. ret = AVERROR(EINVAL);
  896. goto fail;
  897. }
  898. /* verify fixed key */
  899. if (c->audible_fixed_key_size != 16) {
  900. av_log(c->fc, AV_LOG_FATAL, "[aax] audible_fixed_key value needs to be 16 bytes!\n");
  901. ret = AVERROR(EINVAL);
  902. goto fail;
  903. }
  904. /* AAX (and AAX+) key derivation */
  905. av_sha_init(sha, 160);
  906. av_sha_update(sha, fixed_key, 16);
  907. av_sha_update(sha, activation_bytes, 4);
  908. av_sha_final(sha, intermediate_key);
  909. av_sha_init(sha, 160);
  910. av_sha_update(sha, fixed_key, 16);
  911. av_sha_update(sha, intermediate_key, 20);
  912. av_sha_update(sha, activation_bytes, 4);
  913. av_sha_final(sha, intermediate_iv);
  914. av_sha_init(sha, 160);
  915. av_sha_update(sha, intermediate_key, 16);
  916. av_sha_update(sha, intermediate_iv, 16);
  917. av_sha_final(sha, calculated_checksum);
  918. if (memcmp(calculated_checksum, file_checksum, 20)) { // critical error
  919. av_log(c->fc, AV_LOG_ERROR, "[aax] mismatch in checksums!\n");
  920. ret = AVERROR_INVALIDDATA;
  921. goto fail;
  922. }
  923. av_aes_init(c->aes_decrypt, intermediate_key, 128, 1);
  924. av_aes_crypt(c->aes_decrypt, output, input, DRM_BLOB_SIZE >> 4, intermediate_iv, 1);
  925. for (i = 0; i < 4; i++) {
  926. // file data (in output) is stored in big-endian mode
  927. if (activation_bytes[i] != output[3 - i]) { // critical error
  928. av_log(c->fc, AV_LOG_ERROR, "[aax] error in drm blob decryption!\n");
  929. ret = AVERROR_INVALIDDATA;
  930. goto fail;
  931. }
  932. }
  933. memcpy(c->file_key, output + 8, 16);
  934. memcpy(input, output + 26, 16);
  935. av_sha_init(sha, 160);
  936. av_sha_update(sha, input, 16);
  937. av_sha_update(sha, c->file_key, 16);
  938. av_sha_update(sha, fixed_key, 16);
  939. av_sha_final(sha, c->file_iv);
  940. fail:
  941. av_free(sha);
  942. return ret;
  943. }
  944. // Audible AAX (and AAX+) bytestream decryption
  945. static int aax_filter(uint8_t *input, int size, MOVContext *c)
  946. {
  947. int blocks = 0;
  948. unsigned char iv[16];
  949. memcpy(iv, c->file_iv, 16); // iv is overwritten
  950. blocks = size >> 4; // trailing bytes are not encrypted!
  951. av_aes_init(c->aes_decrypt, c->file_key, 128, 1);
  952. av_aes_crypt(c->aes_decrypt, input, input, blocks, iv, 1);
  953. return 0;
  954. }
  955. /* read major brand, minor version and compatible brands and store them as metadata */
  956. static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  957. {
  958. uint32_t minor_ver;
  959. int comp_brand_size;
  960. char* comp_brands_str;
  961. uint8_t type[5] = {0};
  962. int ret = ffio_read_size(pb, type, 4);
  963. if (ret < 0)
  964. return ret;
  965. if (strcmp(type, "qt "))
  966. c->isom = 1;
  967. av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
  968. av_dict_set(&c->fc->metadata, "major_brand", type, 0);
  969. minor_ver = avio_rb32(pb); /* minor version */
  970. av_dict_set_int(&c->fc->metadata, "minor_version", minor_ver, 0);
  971. comp_brand_size = atom.size - 8;
  972. if (comp_brand_size < 0 || comp_brand_size == INT_MAX)
  973. return AVERROR_INVALIDDATA;
  974. comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
  975. if (!comp_brands_str)
  976. return AVERROR(ENOMEM);
  977. ret = ffio_read_size(pb, comp_brands_str, comp_brand_size);
  978. if (ret < 0) {
  979. av_freep(&comp_brands_str);
  980. return ret;
  981. }
  982. comp_brands_str[comp_brand_size] = 0;
  983. av_dict_set(&c->fc->metadata, "compatible_brands",
  984. comp_brands_str, AV_DICT_DONT_STRDUP_VAL);
  985. return 0;
  986. }
  987. /* this atom should contain all header atoms */
  988. static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  989. {
  990. int ret;
  991. if (c->found_moov) {
  992. av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n");
  993. avio_skip(pb, atom.size);
  994. return 0;
  995. }
  996. if ((ret = mov_read_default(c, pb, atom)) < 0)
  997. return ret;
  998. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  999. /* so we don't parse the whole file if over a network */
  1000. c->found_moov=1;
  1001. return 0; /* now go for mdat */
  1002. }
  1003. static MOVFragmentStreamInfo * get_frag_stream_info(
  1004. MOVFragmentIndex *frag_index,
  1005. int index,
  1006. int id)
  1007. {
  1008. int i;
  1009. MOVFragmentIndexItem * item;
  1010. if (index < 0 || index >= frag_index->nb_items)
  1011. return NULL;
  1012. item = &frag_index->item[index];
  1013. for (i = 0; i < item->nb_stream_info; i++)
  1014. if (item->stream_info[i].id == id)
  1015. return &item->stream_info[i];
  1016. // This shouldn't happen
  1017. return NULL;
  1018. }
  1019. static void set_frag_stream(MOVFragmentIndex *frag_index, int id)
  1020. {
  1021. int i;
  1022. MOVFragmentIndexItem * item;
  1023. if (frag_index->current < 0 ||
  1024. frag_index->current >= frag_index->nb_items)
  1025. return;
  1026. item = &frag_index->item[frag_index->current];
  1027. for (i = 0; i < item->nb_stream_info; i++)
  1028. if (item->stream_info[i].id == id) {
  1029. item->current = i;
  1030. return;
  1031. }
  1032. // id not found. This shouldn't happen.
  1033. item->current = -1;
  1034. }
  1035. static MOVFragmentStreamInfo * get_current_frag_stream_info(
  1036. MOVFragmentIndex *frag_index)
  1037. {
  1038. MOVFragmentIndexItem *item;
  1039. if (frag_index->current < 0 ||
  1040. frag_index->current >= frag_index->nb_items)
  1041. return NULL;
  1042. item = &frag_index->item[frag_index->current];
  1043. if (item->current >= 0 && item->current < item->nb_stream_info)
  1044. return &item->stream_info[item->current];
  1045. // This shouldn't happen
  1046. return NULL;
  1047. }
  1048. static int search_frag_moof_offset(MOVFragmentIndex *frag_index, int64_t offset)
  1049. {
  1050. int a, b, m;
  1051. int64_t moof_offset;
  1052. // Optimize for appending new entries
  1053. if (!frag_index->nb_items ||
  1054. frag_index->item[frag_index->nb_items - 1].moof_offset < offset)
  1055. return frag_index->nb_items;
  1056. a = -1;
  1057. b = frag_index->nb_items;
  1058. while (b - a > 1) {
  1059. m = (a + b) >> 1;
  1060. moof_offset = frag_index->item[m].moof_offset;
  1061. if (moof_offset >= offset)
  1062. b = m;
  1063. if (moof_offset <= offset)
  1064. a = m;
  1065. }
  1066. return b;
  1067. }
  1068. static int64_t get_stream_info_time(MOVFragmentStreamInfo * frag_stream_info)
  1069. {
  1070. av_assert0(frag_stream_info);
  1071. if (frag_stream_info->sidx_pts != AV_NOPTS_VALUE)
  1072. return frag_stream_info->sidx_pts;
  1073. if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE)
  1074. return frag_stream_info->first_tfra_pts;
  1075. return frag_stream_info->tfdt_dts;
  1076. }
  1077. static int64_t get_frag_time(MOVFragmentIndex *frag_index,
  1078. int index, int track_id)
  1079. {
  1080. MOVFragmentStreamInfo * frag_stream_info;
  1081. int64_t timestamp;
  1082. int i;
  1083. if (track_id >= 0) {
  1084. frag_stream_info = get_frag_stream_info(frag_index, index, track_id);
  1085. return frag_stream_info->sidx_pts;
  1086. }
  1087. for (i = 0; i < frag_index->item[index].nb_stream_info; i++) {
  1088. frag_stream_info = &frag_index->item[index].stream_info[i];
  1089. timestamp = get_stream_info_time(frag_stream_info);
  1090. if (timestamp != AV_NOPTS_VALUE)
  1091. return timestamp;
  1092. }
  1093. return AV_NOPTS_VALUE;
  1094. }
  1095. static int search_frag_timestamp(MOVFragmentIndex *frag_index,
  1096. AVStream *st, int64_t timestamp)
  1097. {
  1098. int a, b, m, m0;
  1099. int64_t frag_time;
  1100. int id = -1;
  1101. if (st) {
  1102. // If the stream is referenced by any sidx, limit the search
  1103. // to fragments that referenced this stream in the sidx
  1104. MOVStreamContext *sc = st->priv_data;
  1105. if (sc->has_sidx)
  1106. id = st->id;
  1107. }
  1108. a = -1;
  1109. b = frag_index->nb_items;
  1110. while (b - a > 1) {
  1111. m0 = m = (a + b) >> 1;
  1112. while (m < b &&
  1113. (frag_time = get_frag_time(frag_index, m, id)) == AV_NOPTS_VALUE)
  1114. m++;
  1115. if (m < b && frag_time <= timestamp)
  1116. a = m;
  1117. else
  1118. b = m0;
  1119. }
  1120. return a;
  1121. }
  1122. static int update_frag_index(MOVContext *c, int64_t offset)
  1123. {
  1124. int index, i;
  1125. MOVFragmentIndexItem * item;
  1126. MOVFragmentStreamInfo * frag_stream_info;
  1127. // If moof_offset already exists in frag_index, return index to it
  1128. index = search_frag_moof_offset(&c->frag_index, offset);
  1129. if (index < c->frag_index.nb_items &&
  1130. c->frag_index.item[index].moof_offset == offset)
  1131. return index;
  1132. // offset is not yet in frag index.
  1133. // Insert new item at index (sorted by moof offset)
  1134. item = av_fast_realloc(c->frag_index.item,
  1135. &c->frag_index.allocated_size,
  1136. (c->frag_index.nb_items + 1) *
  1137. sizeof(*c->frag_index.item));
  1138. if (!item)
  1139. return -1;
  1140. c->frag_index.item = item;
  1141. frag_stream_info = av_realloc_array(NULL, c->fc->nb_streams,
  1142. sizeof(*item->stream_info));
  1143. if (!frag_stream_info)
  1144. return -1;
  1145. for (i = 0; i < c->fc->nb_streams; i++) {
  1146. // Avoid building frag index if streams lack track id.
  1147. if (c->fc->streams[i]->id < 0) {
  1148. av_free(frag_stream_info);
  1149. return AVERROR_INVALIDDATA;
  1150. }
  1151. frag_stream_info[i].id = c->fc->streams[i]->id;
  1152. frag_stream_info[i].sidx_pts = AV_NOPTS_VALUE;
  1153. frag_stream_info[i].tfdt_dts = AV_NOPTS_VALUE;
  1154. frag_stream_info[i].next_trun_dts = AV_NOPTS_VALUE;
  1155. frag_stream_info[i].first_tfra_pts = AV_NOPTS_VALUE;
  1156. frag_stream_info[i].index_entry = -1;
  1157. frag_stream_info[i].encryption_index = NULL;
  1158. }
  1159. if (index < c->frag_index.nb_items)
  1160. memmove(c->frag_index.item + index + 1, c->frag_index.item + index,
  1161. (c->frag_index.nb_items - index) * sizeof(*c->frag_index.item));
  1162. item = &c->frag_index.item[index];
  1163. item->headers_read = 0;
  1164. item->current = 0;
  1165. item->nb_stream_info = c->fc->nb_streams;
  1166. item->moof_offset = offset;
  1167. item->stream_info = frag_stream_info;
  1168. c->frag_index.nb_items++;
  1169. return index;
  1170. }
  1171. static void fix_frag_index_entries(MOVFragmentIndex *frag_index, int index,
  1172. int id, int entries)
  1173. {
  1174. int i;
  1175. MOVFragmentStreamInfo * frag_stream_info;
  1176. if (index < 0)
  1177. return;
  1178. for (i = index; i < frag_index->nb_items; i++) {
  1179. frag_stream_info = get_frag_stream_info(frag_index, i, id);
  1180. if (frag_stream_info && frag_stream_info->index_entry >= 0)
  1181. frag_stream_info->index_entry += entries;
  1182. }
  1183. }
  1184. static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1185. {
  1186. // Set by mov_read_tfhd(). mov_read_trun() will reject files missing tfhd.
  1187. c->fragment.found_tfhd = 0;
  1188. if (!c->has_looked_for_mfra && c->use_mfra_for > 0) {
  1189. c->has_looked_for_mfra = 1;
  1190. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  1191. int ret;
  1192. av_log(c->fc, AV_LOG_VERBOSE, "stream has moof boxes, will look "
  1193. "for a mfra\n");
  1194. if ((ret = mov_read_mfra(c, pb)) < 0) {
  1195. av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but failed to "
  1196. "read the mfra (may be a live ismv)\n");
  1197. }
  1198. } else {
  1199. av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but stream is not "
  1200. "seekable, can not look for mfra\n");
  1201. }
  1202. }
  1203. c->fragment.moof_offset = c->fragment.implicit_offset = avio_tell(pb) - 8;
  1204. av_log(c->fc, AV_LOG_TRACE, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
  1205. c->frag_index.current = update_frag_index(c, c->fragment.moof_offset);
  1206. return mov_read_default(c, pb, atom);
  1207. }
  1208. static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time, void *logctx)
  1209. {
  1210. if (time) {
  1211. if (time >= 2082844800)
  1212. time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
  1213. if ((int64_t)(time * 1000000ULL) / 1000000 != time) {
  1214. av_log(logctx, AV_LOG_DEBUG, "creation_time is not representable\n");
  1215. return;
  1216. }
  1217. avpriv_dict_set_timestamp(metadata, "creation_time", time * 1000000);
  1218. }
  1219. }
  1220. static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1221. {
  1222. AVStream *st;
  1223. MOVStreamContext *sc;
  1224. int version;
  1225. char language[4] = {0};
  1226. unsigned lang;
  1227. int64_t creation_time;
  1228. if (c->fc->nb_streams < 1)
  1229. return 0;
  1230. st = c->fc->streams[c->fc->nb_streams-1];
  1231. sc = st->priv_data;
  1232. if (sc->time_scale) {
  1233. av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
  1234. return AVERROR_INVALIDDATA;
  1235. }
  1236. version = avio_r8(pb);
  1237. if (version > 1) {
  1238. avpriv_request_sample(c->fc, "Version %d", version);
  1239. return AVERROR_PATCHWELCOME;
  1240. }
  1241. avio_rb24(pb); /* flags */
  1242. if (version == 1) {
  1243. creation_time = avio_rb64(pb);
  1244. avio_rb64(pb);
  1245. } else {
  1246. creation_time = avio_rb32(pb);
  1247. avio_rb32(pb); /* modification time */
  1248. }
  1249. mov_metadata_creation_time(&st->metadata, creation_time, c->fc);
  1250. sc->time_scale = avio_rb32(pb);
  1251. if (sc->time_scale <= 0) {
  1252. av_log(c->fc, AV_LOG_ERROR, "Invalid mdhd time scale %d, defaulting to 1\n", sc->time_scale);
  1253. sc->time_scale = 1;
  1254. }
  1255. st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  1256. lang = avio_rb16(pb); /* language */
  1257. if (ff_mov_lang_to_iso639(lang, language))
  1258. av_dict_set(&st->metadata, "language", language, 0);
  1259. avio_rb16(pb); /* quality */
  1260. return 0;
  1261. }
  1262. static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1263. {
  1264. int i;
  1265. int64_t creation_time;
  1266. int version = avio_r8(pb); /* version */
  1267. avio_rb24(pb); /* flags */
  1268. if (version == 1) {
  1269. creation_time = avio_rb64(pb);
  1270. avio_rb64(pb);
  1271. } else {
  1272. creation_time = avio_rb32(pb);
  1273. avio_rb32(pb); /* modification time */
  1274. }
  1275. mov_metadata_creation_time(&c->fc->metadata, creation_time, c->fc);
  1276. c->time_scale = avio_rb32(pb); /* time scale */
  1277. if (c->time_scale <= 0) {
  1278. av_log(c->fc, AV_LOG_ERROR, "Invalid mvhd time scale %d, defaulting to 1\n", c->time_scale);
  1279. c->time_scale = 1;
  1280. }
  1281. av_log(c->fc, AV_LOG_TRACE, "time scale = %i\n", c->time_scale);
  1282. c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  1283. // set the AVCodecContext duration because the duration of individual tracks
  1284. // may be inaccurate
  1285. if (c->time_scale > 0 && !c->trex_data)
  1286. c->fc->duration = av_rescale(c->duration, AV_TIME_BASE, c->time_scale);
  1287. avio_rb32(pb); /* preferred scale */
  1288. avio_rb16(pb); /* preferred volume */
  1289. avio_skip(pb, 10); /* reserved */
  1290. /* movie display matrix, store it in main context and use it later on */
  1291. for (i = 0; i < 3; i++) {
  1292. c->movie_display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
  1293. c->movie_display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
  1294. c->movie_display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
  1295. }
  1296. avio_rb32(pb); /* preview time */
  1297. avio_rb32(pb); /* preview duration */
  1298. avio_rb32(pb); /* poster time */
  1299. avio_rb32(pb); /* selection time */
  1300. avio_rb32(pb); /* selection duration */
  1301. avio_rb32(pb); /* current time */
  1302. avio_rb32(pb); /* next track ID */
  1303. return 0;
  1304. }
  1305. static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1306. {
  1307. AVStream *st;
  1308. int little_endian;
  1309. if (c->fc->nb_streams < 1)
  1310. return 0;
  1311. st = c->fc->streams[c->fc->nb_streams-1];
  1312. little_endian = avio_rb16(pb) & 0xFF;
  1313. av_log(c->fc, AV_LOG_TRACE, "enda %d\n", little_endian);
  1314. if (little_endian == 1) {
  1315. switch (st->codecpar->codec_id) {
  1316. case AV_CODEC_ID_PCM_S24BE:
  1317. st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE;
  1318. break;
  1319. case AV_CODEC_ID_PCM_S32BE:
  1320. st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
  1321. break;
  1322. case AV_CODEC_ID_PCM_F32BE:
  1323. st->codecpar->codec_id = AV_CODEC_ID_PCM_F32LE;
  1324. break;
  1325. case AV_CODEC_ID_PCM_F64BE:
  1326. st->codecpar->codec_id = AV_CODEC_ID_PCM_F64LE;
  1327. break;
  1328. default:
  1329. break;
  1330. }
  1331. }
  1332. return 0;
  1333. }
  1334. static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1335. {
  1336. AVStream *st;
  1337. uint8_t *icc_profile;
  1338. char color_parameter_type[5] = { 0 };
  1339. uint16_t color_primaries, color_trc, color_matrix;
  1340. int ret;
  1341. if (c->fc->nb_streams < 1)
  1342. return 0;
  1343. st = c->fc->streams[c->fc->nb_streams - 1];
  1344. ret = ffio_read_size(pb, color_parameter_type, 4);
  1345. if (ret < 0)
  1346. return ret;
  1347. if (strncmp(color_parameter_type, "nclx", 4) &&
  1348. strncmp(color_parameter_type, "nclc", 4) &&
  1349. strncmp(color_parameter_type, "prof", 4)) {
  1350. av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n",
  1351. color_parameter_type);
  1352. return 0;
  1353. }
  1354. if (!strncmp(color_parameter_type, "prof", 4)) {
  1355. icc_profile = av_stream_new_side_data(st, AV_PKT_DATA_ICC_PROFILE, atom.size - 4);
  1356. if (!icc_profile)
  1357. return AVERROR(ENOMEM);
  1358. ret = ffio_read_size(pb, icc_profile, atom.size - 4);
  1359. if (ret < 0)
  1360. return ret;
  1361. } else {
  1362. color_primaries = avio_rb16(pb);
  1363. color_trc = avio_rb16(pb);
  1364. color_matrix = avio_rb16(pb);
  1365. av_log(c->fc, AV_LOG_TRACE,
  1366. "%s: pri %d trc %d matrix %d",
  1367. color_parameter_type, color_primaries, color_trc, color_matrix);
  1368. if (!strncmp(color_parameter_type, "nclx", 4)) {
  1369. uint8_t color_range = avio_r8(pb) >> 7;
  1370. av_log(c->fc, AV_LOG_TRACE, " full %"PRIu8"", color_range);
  1371. if (color_range)
  1372. st->codecpar->color_range = AVCOL_RANGE_JPEG;
  1373. else
  1374. st->codecpar->color_range = AVCOL_RANGE_MPEG;
  1375. }
  1376. if (!av_color_primaries_name(color_primaries))
  1377. color_primaries = AVCOL_PRI_UNSPECIFIED;
  1378. if (!av_color_transfer_name(color_trc))
  1379. color_trc = AVCOL_TRC_UNSPECIFIED;
  1380. if (!av_color_space_name(color_matrix))
  1381. color_matrix = AVCOL_SPC_UNSPECIFIED;
  1382. st->codecpar->color_primaries = color_primaries;
  1383. st->codecpar->color_trc = color_trc;
  1384. st->codecpar->color_space = color_matrix;
  1385. av_log(c->fc, AV_LOG_TRACE, "\n");
  1386. }
  1387. return 0;
  1388. }
  1389. static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1390. {
  1391. AVStream *st;
  1392. unsigned mov_field_order;
  1393. enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
  1394. if (c->fc->nb_streams < 1) // will happen with jp2 files
  1395. return 0;
  1396. st = c->fc->streams[c->fc->nb_streams-1];
  1397. if (atom.size < 2)
  1398. return AVERROR_INVALIDDATA;
  1399. mov_field_order = avio_rb16(pb);
  1400. if ((mov_field_order & 0xFF00) == 0x0100)
  1401. decoded_field_order = AV_FIELD_PROGRESSIVE;
  1402. else if ((mov_field_order & 0xFF00) == 0x0200) {
  1403. switch (mov_field_order & 0xFF) {
  1404. case 0x01: decoded_field_order = AV_FIELD_TT;
  1405. break;
  1406. case 0x06: decoded_field_order = AV_FIELD_BB;
  1407. break;
  1408. case 0x09: decoded_field_order = AV_FIELD_TB;
  1409. break;
  1410. case 0x0E: decoded_field_order = AV_FIELD_BT;
  1411. break;
  1412. }
  1413. }
  1414. if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
  1415. av_log(c->fc, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
  1416. }
  1417. st->codecpar->field_order = decoded_field_order;
  1418. return 0;
  1419. }
  1420. static int mov_realloc_extradata(AVCodecParameters *par, MOVAtom atom)
  1421. {
  1422. int err = 0;
  1423. uint64_t size = (uint64_t)par->extradata_size + atom.size + 8 + AV_INPUT_BUFFER_PADDING_SIZE;
  1424. if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
  1425. return AVERROR_INVALIDDATA;
  1426. if ((err = av_reallocp(&par->extradata, size)) < 0) {
  1427. par->extradata_size = 0;
  1428. return err;
  1429. }
  1430. par->extradata_size = size - AV_INPUT_BUFFER_PADDING_SIZE;
  1431. return 0;
  1432. }
  1433. /* Read a whole atom into the extradata return the size of the atom read, possibly truncated if != atom.size */
  1434. static int64_t mov_read_atom_into_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
  1435. AVCodecParameters *par, uint8_t *buf)
  1436. {
  1437. int64_t result = atom.size;
  1438. int err;
  1439. AV_WB32(buf , atom.size + 8);
  1440. AV_WL32(buf + 4, atom.type);
  1441. err = ffio_read_size(pb, buf + 8, atom.size);
  1442. if (err < 0) {
  1443. par->extradata_size -= atom.size;
  1444. return err;
  1445. } else if (err < atom.size) {
  1446. av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n");
  1447. par->extradata_size -= atom.size - err;
  1448. result = err;
  1449. }
  1450. memset(buf + 8 + err, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  1451. return result;
  1452. }
  1453. /* FIXME modify QDM2/SVQ3/H.264 decoders to take full atom as extradata */
  1454. static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
  1455. enum AVCodecID codec_id)
  1456. {
  1457. AVStream *st;
  1458. uint64_t original_size;
  1459. int err;
  1460. if (c->fc->nb_streams < 1) // will happen with jp2 files
  1461. return 0;
  1462. st = c->fc->streams[c->fc->nb_streams-1];
  1463. if (st->codecpar->codec_id != codec_id)
  1464. return 0; /* unexpected codec_id - don't mess with extradata */
  1465. original_size = st->codecpar->extradata_size;
  1466. err = mov_realloc_extradata(st->codecpar, atom);
  1467. if (err)
  1468. return err;
  1469. err = mov_read_atom_into_extradata(c, pb, atom, st->codecpar, st->codecpar->extradata + original_size);
  1470. if (err < 0)
  1471. return err;
  1472. return 0; // Note: this is the original behavior to ignore truncation.
  1473. }
  1474. /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
  1475. static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1476. {
  1477. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
  1478. }
  1479. static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1480. {
  1481. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS);
  1482. }
  1483. static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1484. {
  1485. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
  1486. }
  1487. static int mov_read_dpxe(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1488. {
  1489. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_R10K);
  1490. }
  1491. static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1492. {
  1493. int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
  1494. if (!ret)
  1495. ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_DNXHD);
  1496. return ret;
  1497. }
  1498. static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1499. {
  1500. int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216);
  1501. if (!ret && c->fc->nb_streams >= 1) {
  1502. AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
  1503. if (par->extradata_size >= 40) {
  1504. par->height = AV_RB16(&par->extradata[36]);
  1505. par->width = AV_RB16(&par->extradata[38]);
  1506. }
  1507. }
  1508. return ret;
  1509. }
  1510. static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1511. {
  1512. if (c->fc->nb_streams >= 1) {
  1513. AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
  1514. if (par->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
  1515. par->codec_id == AV_CODEC_ID_H264 &&
  1516. atom.size > 11) {
  1517. int cid;
  1518. avio_skip(pb, 10);
  1519. cid = avio_rb16(pb);
  1520. /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
  1521. if (cid == 0xd4d || cid == 0xd4e)
  1522. par->width = 1440;
  1523. return 0;
  1524. } else if ((par->codec_tag == MKTAG('A', 'V', 'd', '1') ||
  1525. par->codec_tag == MKTAG('A', 'V', 'j', '2') ||
  1526. par->codec_tag == MKTAG('A', 'V', 'd', 'n')) &&
  1527. atom.size >= 24) {
  1528. int num, den;
  1529. avio_skip(pb, 12);
  1530. num = avio_rb32(pb);
  1531. den = avio_rb32(pb);
  1532. if (num <= 0 || den <= 0)
  1533. return 0;
  1534. switch (avio_rb32(pb)) {
  1535. case 2:
  1536. if (den >= INT_MAX / 2)
  1537. return 0;
  1538. den *= 2;
  1539. case 1:
  1540. c->fc->streams[c->fc->nb_streams-1]->display_aspect_ratio.num = num;
  1541. c->fc->streams[c->fc->nb_streams-1]->display_aspect_ratio.den = den;
  1542. default:
  1543. return 0;
  1544. }
  1545. }
  1546. }
  1547. return mov_read_avid(c, pb, atom);
  1548. }
  1549. static int mov_read_aclr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1550. {
  1551. int ret = 0;
  1552. int length = 0;
  1553. uint64_t original_size;
  1554. if (c->fc->nb_streams >= 1) {
  1555. AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
  1556. if (par->codec_id == AV_CODEC_ID_H264)
  1557. return 0;
  1558. if (atom.size == 16) {
  1559. original_size = par->extradata_size;
  1560. ret = mov_realloc_extradata(par, atom);
  1561. if (!ret) {
  1562. length = mov_read_atom_into_extradata(c, pb, atom, par, par->extradata + original_size);
  1563. if (length == atom.size) {
  1564. const uint8_t range_value = par->extradata[original_size + 19];
  1565. switch (range_value) {
  1566. case 1:
  1567. par->color_range = AVCOL_RANGE_MPEG;
  1568. break;
  1569. case 2:
  1570. par->color_range = AVCOL_RANGE_JPEG;
  1571. break;
  1572. default:
  1573. av_log(c->fc, AV_LOG_WARNING, "ignored unknown aclr value (%d)\n", range_value);
  1574. break;
  1575. }
  1576. ff_dlog(c->fc, "color_range: %d\n", par->color_range);
  1577. } else {
  1578. /* For some reason the whole atom was not added to the extradata */
  1579. av_log(c->fc, AV_LOG_ERROR, "aclr not decoded - incomplete atom\n");
  1580. }
  1581. } else {
  1582. av_log(c->fc, AV_LOG_ERROR, "aclr not decoded - unable to add atom to extradata\n");
  1583. }
  1584. } else {
  1585. av_log(c->fc, AV_LOG_WARNING, "aclr not decoded - unexpected size %"PRId64"\n", atom.size);
  1586. }
  1587. }
  1588. return ret;
  1589. }
  1590. static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1591. {
  1592. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
  1593. }
  1594. static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1595. {
  1596. AVStream *st;
  1597. int ret;
  1598. if (c->fc->nb_streams < 1)
  1599. return 0;
  1600. st = c->fc->streams[c->fc->nb_streams-1];
  1601. if ((uint64_t)atom.size > (1<<30))
  1602. return AVERROR_INVALIDDATA;
  1603. if (st->codecpar->codec_id == AV_CODEC_ID_QDM2 ||
  1604. st->codecpar->codec_id == AV_CODEC_ID_QDMC ||
  1605. st->codecpar->codec_id == AV_CODEC_ID_SPEEX) {
  1606. // pass all frma atom to codec, needed at least for QDMC and QDM2
  1607. ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
  1608. if (ret < 0)
  1609. return ret;
  1610. } else if (atom.size > 8) { /* to read frma, esds atoms */
  1611. if (st->codecpar->codec_id == AV_CODEC_ID_ALAC && atom.size >= 24) {
  1612. uint64_t buffer;
  1613. ret = ffio_ensure_seekback(pb, 8);
  1614. if (ret < 0)
  1615. return ret;
  1616. buffer = avio_rb64(pb);
  1617. atom.size -= 8;
  1618. if ( (buffer & 0xFFFFFFFF) == MKBETAG('f','r','m','a')
  1619. && buffer >> 32 <= atom.size
  1620. && buffer >> 32 >= 8) {
  1621. avio_skip(pb, -8);
  1622. atom.size += 8;
  1623. } else if (!st->codecpar->extradata_size) {
  1624. #define ALAC_EXTRADATA_SIZE 36
  1625. st->codecpar->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
  1626. if (!st->codecpar->extradata)
  1627. return AVERROR(ENOMEM);
  1628. st->codecpar->extradata_size = ALAC_EXTRADATA_SIZE;
  1629. AV_WB32(st->codecpar->extradata , ALAC_EXTRADATA_SIZE);
  1630. AV_WB32(st->codecpar->extradata + 4, MKTAG('a','l','a','c'));
  1631. AV_WB64(st->codecpar->extradata + 12, buffer);
  1632. avio_read(pb, st->codecpar->extradata + 20, 16);
  1633. avio_skip(pb, atom.size - 24);
  1634. return 0;
  1635. }
  1636. }
  1637. if ((ret = mov_read_default(c, pb, atom)) < 0)
  1638. return ret;
  1639. } else
  1640. avio_skip(pb, atom.size);
  1641. return 0;
  1642. }
  1643. /**
  1644. * This function reads atom content and puts data in extradata without tag
  1645. * nor size unlike mov_read_extradata.
  1646. */
  1647. static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1648. {
  1649. AVStream *st;
  1650. int ret;
  1651. if (c->fc->nb_streams < 1)
  1652. return 0;
  1653. st = c->fc->streams[c->fc->nb_streams-1];
  1654. if ((uint64_t)atom.size > (1<<30))
  1655. return AVERROR_INVALIDDATA;
  1656. if (atom.size >= 10) {
  1657. // Broken files created by legacy versions of libavformat will
  1658. // wrap a whole fiel atom inside of a glbl atom.
  1659. unsigned size = avio_rb32(pb);
  1660. unsigned type = avio_rl32(pb);
  1661. avio_seek(pb, -8, SEEK_CUR);
  1662. if (type == MKTAG('f','i','e','l') && size == atom.size)
  1663. return mov_read_default(c, pb, atom);
  1664. }
  1665. if (st->codecpar->extradata_size > 1 && st->codecpar->extradata) {
  1666. av_log(c->fc, AV_LOG_WARNING, "ignoring multiple glbl\n");
  1667. return 0;
  1668. }
  1669. ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
  1670. if (ret < 0)
  1671. return ret;
  1672. if (atom.type == MKTAG('h','v','c','C') && st->codecpar->codec_tag == MKTAG('d','v','h','1'))
  1673. /* HEVC-based Dolby Vision derived from hvc1.
  1674. Happens to match with an identifier
  1675. previously utilized for DV. Thus, if we have
  1676. the hvcC extradata box available as specified,
  1677. set codec to HEVC */
  1678. st->codecpar->codec_id = AV_CODEC_ID_HEVC;
  1679. return 0;
  1680. }
  1681. static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1682. {
  1683. AVStream *st;
  1684. uint8_t profile_level;
  1685. int ret;
  1686. if (c->fc->nb_streams < 1)
  1687. return 0;
  1688. st = c->fc->streams[c->fc->nb_streams-1];
  1689. if (atom.size >= (1<<28) || atom.size < 7)
  1690. return AVERROR_INVALIDDATA;
  1691. profile_level = avio_r8(pb);
  1692. if ((profile_level & 0xf0) != 0xc0)
  1693. return 0;
  1694. avio_seek(pb, 6, SEEK_CUR);
  1695. ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 7);
  1696. if (ret < 0)
  1697. return ret;
  1698. return 0;
  1699. }
  1700. /**
  1701. * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
  1702. * but can have extradata appended at the end after the 40 bytes belonging
  1703. * to the struct.
  1704. */
  1705. static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1706. {
  1707. AVStream *st;
  1708. int ret;
  1709. if (c->fc->nb_streams < 1)
  1710. return 0;
  1711. if (atom.size <= 40)
  1712. return 0;
  1713. st = c->fc->streams[c->fc->nb_streams-1];
  1714. if ((uint64_t)atom.size > (1<<30))
  1715. return AVERROR_INVALIDDATA;
  1716. avio_skip(pb, 40);
  1717. ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 40);
  1718. if (ret < 0)
  1719. return ret;
  1720. return 0;
  1721. }
  1722. static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1723. {
  1724. AVStream *st;
  1725. MOVStreamContext *sc;
  1726. unsigned int i, entries;
  1727. if (c->trak_index < 0) {
  1728. av_log(c->fc, AV_LOG_WARNING, "STCO outside TRAK\n");
  1729. return 0;
  1730. }
  1731. if (c->fc->nb_streams < 1)
  1732. return 0;
  1733. st = c->fc->streams[c->fc->nb_streams-1];
  1734. sc = st->priv_data;
  1735. avio_r8(pb); /* version */
  1736. avio_rb24(pb); /* flags */
  1737. entries = avio_rb32(pb);
  1738. if (!entries)
  1739. return 0;
  1740. if (sc->chunk_offsets)
  1741. av_log(c->fc, AV_LOG_WARNING, "Duplicated STCO atom\n");
  1742. av_free(sc->chunk_offsets);
  1743. sc->chunk_count = 0;
  1744. sc->chunk_offsets = av_malloc_array(entries, sizeof(*sc->chunk_offsets));
  1745. if (!sc->chunk_offsets)
  1746. return AVERROR(ENOMEM);
  1747. sc->chunk_count = entries;
  1748. if (atom.type == MKTAG('s','t','c','o'))
  1749. for (i = 0; i < entries && !pb->eof_reached; i++)
  1750. sc->chunk_offsets[i] = avio_rb32(pb);
  1751. else if (atom.type == MKTAG('c','o','6','4'))
  1752. for (i = 0; i < entries && !pb->eof_reached; i++)
  1753. sc->chunk_offsets[i] = avio_rb64(pb);
  1754. else
  1755. return AVERROR_INVALIDDATA;
  1756. sc->chunk_count = i;
  1757. if (pb->eof_reached) {
  1758. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STCO atom\n");
  1759. return AVERROR_EOF;
  1760. }
  1761. return 0;
  1762. }
  1763. static int mov_codec_id(AVStream *st, uint32_t format)
  1764. {
  1765. int id = ff_codec_get_id(ff_codec_movaudio_tags, format);
  1766. if (id <= 0 &&
  1767. ((format & 0xFFFF) == 'm' + ('s' << 8) ||
  1768. (format & 0xFFFF) == 'T' + ('S' << 8)))
  1769. id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format) & 0xFFFF);
  1770. if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
  1771. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  1772. } else if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
  1773. /* skip old ASF MPEG-4 tag */
  1774. format && format != MKTAG('m','p','4','s')) {
  1775. id = ff_codec_get_id(ff_codec_movvideo_tags, format);
  1776. if (id <= 0)
  1777. id = ff_codec_get_id(ff_codec_bmp_tags, format);
  1778. if (id > 0)
  1779. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  1780. else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA ||
  1781. (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE &&
  1782. st->codecpar->codec_id == AV_CODEC_ID_NONE)) {
  1783. id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
  1784. if (id > 0)
  1785. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  1786. else
  1787. id = ff_codec_get_id(ff_codec_movdata_tags, format);
  1788. }
  1789. }
  1790. st->codecpar->codec_tag = format;
  1791. return id;
  1792. }
  1793. static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb,
  1794. AVStream *st, MOVStreamContext *sc)
  1795. {
  1796. uint8_t codec_name[32] = { 0 };
  1797. int64_t stsd_start;
  1798. unsigned int len;
  1799. /* The first 16 bytes of the video sample description are already
  1800. * read in ff_mov_read_stsd_entries() */
  1801. stsd_start = avio_tell(pb) - 16;
  1802. avio_rb16(pb); /* version */
  1803. avio_rb16(pb); /* revision level */
  1804. avio_rb32(pb); /* vendor */
  1805. avio_rb32(pb); /* temporal quality */
  1806. avio_rb32(pb); /* spatial quality */
  1807. st->codecpar->width = avio_rb16(pb); /* width */
  1808. st->codecpar->height = avio_rb16(pb); /* height */
  1809. avio_rb32(pb); /* horiz resolution */
  1810. avio_rb32(pb); /* vert resolution */
  1811. avio_rb32(pb); /* data size, always 0 */
  1812. avio_rb16(pb); /* frames per samples */
  1813. len = avio_r8(pb); /* codec name, pascal string */
  1814. if (len > 31)
  1815. len = 31;
  1816. mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
  1817. if (len < 31)
  1818. avio_skip(pb, 31 - len);
  1819. if (codec_name[0])
  1820. av_dict_set(&st->metadata, "encoder", codec_name, 0);
  1821. /* codec_tag YV12 triggers an UV swap in rawdec.c */
  1822. if (!strncmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
  1823. st->codecpar->codec_tag = MKTAG('I', '4', '2', '0');
  1824. st->codecpar->width &= ~1;
  1825. st->codecpar->height &= ~1;
  1826. }
  1827. /* Flash Media Server uses tag H.263 with Sorenson Spark */
  1828. if (st->codecpar->codec_tag == MKTAG('H','2','6','3') &&
  1829. !strncmp(codec_name, "Sorenson H263", 13))
  1830. st->codecpar->codec_id = AV_CODEC_ID_FLV1;
  1831. st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* depth */
  1832. avio_seek(pb, stsd_start, SEEK_SET);
  1833. if (ff_get_qtpalette(st->codecpar->codec_id, pb, sc->palette)) {
  1834. st->codecpar->bits_per_coded_sample &= 0x1F;
  1835. sc->has_palette = 1;
  1836. }
  1837. }
  1838. static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb,
  1839. AVStream *st, MOVStreamContext *sc)
  1840. {
  1841. int bits_per_sample, flags;
  1842. uint16_t version = avio_rb16(pb);
  1843. AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
  1844. avio_rb16(pb); /* revision level */
  1845. avio_rb32(pb); /* vendor */
  1846. st->codecpar->channels = avio_rb16(pb); /* channel count */
  1847. st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* sample size */
  1848. av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", st->codecpar->channels);
  1849. sc->audio_cid = avio_rb16(pb);
  1850. avio_rb16(pb); /* packet size = 0 */
  1851. st->codecpar->sample_rate = ((avio_rb32(pb) >> 16));
  1852. // Read QT version 1 fields. In version 0 these do not exist.
  1853. av_log(c->fc, AV_LOG_TRACE, "version =%d, isom =%d\n", version, c->isom);
  1854. if (!c->isom ||
  1855. (compatible_brands && strstr(compatible_brands->value, "qt ")) ||
  1856. (sc->stsd_version == 0 && version > 0)) {
  1857. if (version == 1) {
  1858. sc->samples_per_frame = avio_rb32(pb);
  1859. avio_rb32(pb); /* bytes per packet */
  1860. sc->bytes_per_frame = avio_rb32(pb);
  1861. avio_rb32(pb); /* bytes per sample */
  1862. } else if (version == 2) {
  1863. avio_rb32(pb); /* sizeof struct only */
  1864. st->codecpar->sample_rate = av_int2double(avio_rb64(pb));
  1865. st->codecpar->channels = avio_rb32(pb);
  1866. avio_rb32(pb); /* always 0x7F000000 */
  1867. st->codecpar->bits_per_coded_sample = avio_rb32(pb);
  1868. flags = avio_rb32(pb); /* lpcm format specific flag */
  1869. sc->bytes_per_frame = avio_rb32(pb);
  1870. sc->samples_per_frame = avio_rb32(pb);
  1871. if (st->codecpar->codec_tag == MKTAG('l','p','c','m'))
  1872. st->codecpar->codec_id =
  1873. ff_mov_get_lpcm_codec_id(st->codecpar->bits_per_coded_sample,
  1874. flags);
  1875. }
  1876. if (version == 0 || (version == 1 && sc->audio_cid != -2)) {
  1877. /* can't correctly handle variable sized packet as audio unit */
  1878. switch (st->codecpar->codec_id) {
  1879. case AV_CODEC_ID_MP2:
  1880. case AV_CODEC_ID_MP3:
  1881. st->need_parsing = AVSTREAM_PARSE_FULL;
  1882. break;
  1883. }
  1884. }
  1885. }
  1886. if (sc->format == 0) {
  1887. if (st->codecpar->bits_per_coded_sample == 8)
  1888. st->codecpar->codec_id = mov_codec_id(st, MKTAG('r','a','w',' '));
  1889. else if (st->codecpar->bits_per_coded_sample == 16)
  1890. st->codecpar->codec_id = mov_codec_id(st, MKTAG('t','w','o','s'));
  1891. }
  1892. switch (st->codecpar->codec_id) {
  1893. case AV_CODEC_ID_PCM_S8:
  1894. case AV_CODEC_ID_PCM_U8:
  1895. if (st->codecpar->bits_per_coded_sample == 16)
  1896. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
  1897. break;
  1898. case AV_CODEC_ID_PCM_S16LE:
  1899. case AV_CODEC_ID_PCM_S16BE:
  1900. if (st->codecpar->bits_per_coded_sample == 8)
  1901. st->codecpar->codec_id = AV_CODEC_ID_PCM_S8;
  1902. else if (st->codecpar->bits_per_coded_sample == 24)
  1903. st->codecpar->codec_id =
  1904. st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE ?
  1905. AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
  1906. else if (st->codecpar->bits_per_coded_sample == 32)
  1907. st->codecpar->codec_id =
  1908. st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE ?
  1909. AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
  1910. break;
  1911. /* set values for old format before stsd version 1 appeared */
  1912. case AV_CODEC_ID_MACE3:
  1913. sc->samples_per_frame = 6;
  1914. sc->bytes_per_frame = 2 * st->codecpar->channels;
  1915. break;
  1916. case AV_CODEC_ID_MACE6:
  1917. sc->samples_per_frame = 6;
  1918. sc->bytes_per_frame = 1 * st->codecpar->channels;
  1919. break;
  1920. case AV_CODEC_ID_ADPCM_IMA_QT:
  1921. sc->samples_per_frame = 64;
  1922. sc->bytes_per_frame = 34 * st->codecpar->channels;
  1923. break;
  1924. case AV_CODEC_ID_GSM:
  1925. sc->samples_per_frame = 160;
  1926. sc->bytes_per_frame = 33;
  1927. break;
  1928. default:
  1929. break;
  1930. }
  1931. bits_per_sample = av_get_bits_per_sample(st->codecpar->codec_id);
  1932. if (bits_per_sample) {
  1933. st->codecpar->bits_per_coded_sample = bits_per_sample;
  1934. sc->sample_size = (bits_per_sample >> 3) * st->codecpar->channels;
  1935. }
  1936. }
  1937. static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb,
  1938. AVStream *st, MOVStreamContext *sc,
  1939. int64_t size)
  1940. {
  1941. // ttxt stsd contains display flags, justification, background
  1942. // color, fonts, and default styles, so fake an atom to read it
  1943. MOVAtom fake_atom = { .size = size };
  1944. // mp4s contains a regular esds atom
  1945. if (st->codecpar->codec_tag != AV_RL32("mp4s"))
  1946. mov_read_glbl(c, pb, fake_atom);
  1947. st->codecpar->width = sc->width;
  1948. st->codecpar->height = sc->height;
  1949. }
  1950. static uint32_t yuv_to_rgba(uint32_t ycbcr)
  1951. {
  1952. uint8_t r, g, b;
  1953. int y, cb, cr;
  1954. y = (ycbcr >> 16) & 0xFF;
  1955. cr = (ycbcr >> 8) & 0xFF;
  1956. cb = ycbcr & 0xFF;
  1957. b = av_clip_uint8((1164 * (y - 16) + 2018 * (cb - 128)) / 1000);
  1958. g = av_clip_uint8((1164 * (y - 16) - 813 * (cr - 128) - 391 * (cb - 128)) / 1000);
  1959. r = av_clip_uint8((1164 * (y - 16) + 1596 * (cr - 128) ) / 1000);
  1960. return (r << 16) | (g << 8) | b;
  1961. }
  1962. static int mov_rewrite_dvd_sub_extradata(AVStream *st)
  1963. {
  1964. char buf[256] = {0};
  1965. uint8_t *src = st->codecpar->extradata;
  1966. int i, ret;
  1967. if (st->codecpar->extradata_size != 64)
  1968. return 0;
  1969. if (st->codecpar->width > 0 && st->codecpar->height > 0)
  1970. snprintf(buf, sizeof(buf), "size: %dx%d\n",
  1971. st->codecpar->width, st->codecpar->height);
  1972. av_strlcat(buf, "palette: ", sizeof(buf));
  1973. for (i = 0; i < 16; i++) {
  1974. uint32_t yuv = AV_RB32(src + i * 4);
  1975. uint32_t rgba = yuv_to_rgba(yuv);
  1976. av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : "");
  1977. }
  1978. if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf))
  1979. return 0;
  1980. ret = ff_alloc_extradata(st->codecpar, strlen(buf));
  1981. if (ret < 0)
  1982. return ret;
  1983. memcpy(st->codecpar->extradata, buf, st->codecpar->extradata_size);
  1984. return 0;
  1985. }
  1986. static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb,
  1987. AVStream *st, MOVStreamContext *sc,
  1988. int64_t size)
  1989. {
  1990. int ret;
  1991. if (st->codecpar->codec_tag == MKTAG('t','m','c','d')) {
  1992. if ((int)size != size)
  1993. return AVERROR(ENOMEM);
  1994. ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
  1995. if (ret < 0)
  1996. return ret;
  1997. if (size > 16) {
  1998. MOVStreamContext *tmcd_ctx = st->priv_data;
  1999. int val;
  2000. val = AV_RB32(st->codecpar->extradata + 4);
  2001. tmcd_ctx->tmcd_flags = val;
  2002. st->avg_frame_rate.num = AV_RB32(st->codecpar->extradata + 8); /* timescale */
  2003. st->avg_frame_rate.den = AV_RB32(st->codecpar->extradata + 12); /* frameDuration */
  2004. #if FF_API_LAVF_AVCTX
  2005. FF_DISABLE_DEPRECATION_WARNINGS
  2006. st->codec->time_base = av_inv_q(st->avg_frame_rate);
  2007. FF_ENABLE_DEPRECATION_WARNINGS
  2008. #endif
  2009. /* adjust for per frame dur in counter mode */
  2010. if (tmcd_ctx->tmcd_flags & 0x0008) {
  2011. int timescale = AV_RB32(st->codecpar->extradata + 8);
  2012. int framedur = AV_RB32(st->codecpar->extradata + 12);
  2013. st->avg_frame_rate.num *= timescale;
  2014. st->avg_frame_rate.den *= framedur;
  2015. #if FF_API_LAVF_AVCTX
  2016. FF_DISABLE_DEPRECATION_WARNINGS
  2017. st->codec->time_base.den *= timescale;
  2018. st->codec->time_base.num *= framedur;
  2019. FF_ENABLE_DEPRECATION_WARNINGS
  2020. #endif
  2021. }
  2022. if (size > 30) {
  2023. uint32_t len = AV_RB32(st->codecpar->extradata + 18); /* name atom length */
  2024. uint32_t format = AV_RB32(st->codecpar->extradata + 22);
  2025. if (format == AV_RB32("name") && (int64_t)size >= (int64_t)len + 18) {
  2026. uint16_t str_size = AV_RB16(st->codecpar->extradata + 26); /* string length */
  2027. if (str_size > 0 && size >= (int)str_size + 30 &&
  2028. st->codecpar->extradata[30] /* Don't add empty string */) {
  2029. char *reel_name = av_malloc(str_size + 1);
  2030. if (!reel_name)
  2031. return AVERROR(ENOMEM);
  2032. memcpy(reel_name, st->codecpar->extradata + 30, str_size);
  2033. reel_name[str_size] = 0; /* Add null terminator */
  2034. av_dict_set(&st->metadata, "reel_name", reel_name,
  2035. AV_DICT_DONT_STRDUP_VAL);
  2036. }
  2037. }
  2038. }
  2039. }
  2040. } else {
  2041. /* other codec type, just skip (rtp, mp4s ...) */
  2042. avio_skip(pb, size);
  2043. }
  2044. return 0;
  2045. }
  2046. static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb,
  2047. AVStream *st, MOVStreamContext *sc)
  2048. {
  2049. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  2050. !st->codecpar->sample_rate && sc->time_scale > 1)
  2051. st->codecpar->sample_rate = sc->time_scale;
  2052. /* special codec parameters handling */
  2053. switch (st->codecpar->codec_id) {
  2054. #if CONFIG_DV_DEMUXER
  2055. case AV_CODEC_ID_DVAUDIO:
  2056. c->dv_fctx = avformat_alloc_context();
  2057. if (!c->dv_fctx) {
  2058. av_log(c->fc, AV_LOG_ERROR, "dv demux context alloc error\n");
  2059. return AVERROR(ENOMEM);
  2060. }
  2061. c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
  2062. if (!c->dv_demux) {
  2063. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  2064. return AVERROR(ENOMEM);
  2065. }
  2066. sc->dv_audio_container = 1;
  2067. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  2068. break;
  2069. #endif
  2070. /* no ifdef since parameters are always those */
  2071. case AV_CODEC_ID_QCELP:
  2072. st->codecpar->channels = 1;
  2073. // force sample rate for qcelp when not stored in mov
  2074. if (st->codecpar->codec_tag != MKTAG('Q','c','l','p'))
  2075. st->codecpar->sample_rate = 8000;
  2076. // FIXME: Why is the following needed for some files?
  2077. sc->samples_per_frame = 160;
  2078. if (!sc->bytes_per_frame)
  2079. sc->bytes_per_frame = 35;
  2080. break;
  2081. case AV_CODEC_ID_AMR_NB:
  2082. st->codecpar->channels = 1;
  2083. /* force sample rate for amr, stsd in 3gp does not store sample rate */
  2084. st->codecpar->sample_rate = 8000;
  2085. break;
  2086. case AV_CODEC_ID_AMR_WB:
  2087. st->codecpar->channels = 1;
  2088. st->codecpar->sample_rate = 16000;
  2089. break;
  2090. case AV_CODEC_ID_MP2:
  2091. case AV_CODEC_ID_MP3:
  2092. /* force type after stsd for m1a hdlr */
  2093. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  2094. break;
  2095. case AV_CODEC_ID_GSM:
  2096. case AV_CODEC_ID_ADPCM_MS:
  2097. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2098. case AV_CODEC_ID_ILBC:
  2099. case AV_CODEC_ID_MACE3:
  2100. case AV_CODEC_ID_MACE6:
  2101. case AV_CODEC_ID_QDM2:
  2102. st->codecpar->block_align = sc->bytes_per_frame;
  2103. break;
  2104. case AV_CODEC_ID_ALAC:
  2105. if (st->codecpar->extradata_size == 36) {
  2106. st->codecpar->channels = AV_RB8 (st->codecpar->extradata + 21);
  2107. st->codecpar->sample_rate = AV_RB32(st->codecpar->extradata + 32);
  2108. }
  2109. break;
  2110. case AV_CODEC_ID_AC3:
  2111. case AV_CODEC_ID_EAC3:
  2112. case AV_CODEC_ID_MPEG1VIDEO:
  2113. case AV_CODEC_ID_VC1:
  2114. case AV_CODEC_ID_VP8:
  2115. case AV_CODEC_ID_VP9:
  2116. st->need_parsing = AVSTREAM_PARSE_FULL;
  2117. break;
  2118. default:
  2119. break;
  2120. }
  2121. return 0;
  2122. }
  2123. static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb,
  2124. int codec_tag, int format,
  2125. int64_t size)
  2126. {
  2127. int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format);
  2128. if (codec_tag &&
  2129. (codec_tag != format &&
  2130. // AVID 1:1 samples with differing data format and codec tag exist
  2131. (codec_tag != AV_RL32("AV1x") || format != AV_RL32("AVup")) &&
  2132. // prores is allowed to have differing data format and codec tag
  2133. codec_tag != AV_RL32("apcn") && codec_tag != AV_RL32("apch") &&
  2134. // so is dv (sigh)
  2135. codec_tag != AV_RL32("dvpp") && codec_tag != AV_RL32("dvcp") &&
  2136. (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id
  2137. : codec_tag != MKTAG('j','p','e','g')))) {
  2138. /* Multiple fourcc, we skip JPEG. This is not correct, we should
  2139. * export it as a separate AVStream but this needs a few changes
  2140. * in the MOV demuxer, patch welcome. */
  2141. av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
  2142. avio_skip(pb, size);
  2143. return 1;
  2144. }
  2145. return 0;
  2146. }
  2147. int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
  2148. {
  2149. AVStream *st;
  2150. MOVStreamContext *sc;
  2151. int pseudo_stream_id;
  2152. av_assert0 (c->fc->nb_streams >= 1);
  2153. st = c->fc->streams[c->fc->nb_streams-1];
  2154. sc = st->priv_data;
  2155. for (pseudo_stream_id = 0;
  2156. pseudo_stream_id < entries && !pb->eof_reached;
  2157. pseudo_stream_id++) {
  2158. //Parsing Sample description table
  2159. enum AVCodecID id;
  2160. int ret, dref_id = 1;
  2161. MOVAtom a = { AV_RL32("stsd") };
  2162. int64_t start_pos = avio_tell(pb);
  2163. int64_t size = avio_rb32(pb); /* size */
  2164. uint32_t format = avio_rl32(pb); /* data format */
  2165. if (size >= 16) {
  2166. avio_rb32(pb); /* reserved */
  2167. avio_rb16(pb); /* reserved */
  2168. dref_id = avio_rb16(pb);
  2169. } else if (size <= 7) {
  2170. av_log(c->fc, AV_LOG_ERROR,
  2171. "invalid size %"PRId64" in stsd\n", size);
  2172. return AVERROR_INVALIDDATA;
  2173. }
  2174. if (mov_skip_multiple_stsd(c, pb, st->codecpar->codec_tag, format,
  2175. size - (avio_tell(pb) - start_pos))) {
  2176. sc->stsd_count++;
  2177. continue;
  2178. }
  2179. sc->pseudo_stream_id = st->codecpar->codec_tag ? -1 : pseudo_stream_id;
  2180. sc->dref_id= dref_id;
  2181. sc->format = format;
  2182. id = mov_codec_id(st, format);
  2183. av_log(c->fc, AV_LOG_TRACE,
  2184. "size=%"PRId64" 4CC=%s codec_type=%d\n", size,
  2185. av_fourcc2str(format), st->codecpar->codec_type);
  2186. st->codecpar->codec_id = id;
  2187. if (st->codecpar->codec_type==AVMEDIA_TYPE_VIDEO) {
  2188. mov_parse_stsd_video(c, pb, st, sc);
  2189. } else if (st->codecpar->codec_type==AVMEDIA_TYPE_AUDIO) {
  2190. mov_parse_stsd_audio(c, pb, st, sc);
  2191. if (st->codecpar->sample_rate < 0) {
  2192. av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
  2193. return AVERROR_INVALIDDATA;
  2194. }
  2195. } else if (st->codecpar->codec_type==AVMEDIA_TYPE_SUBTITLE){
  2196. mov_parse_stsd_subtitle(c, pb, st, sc,
  2197. size - (avio_tell(pb) - start_pos));
  2198. } else {
  2199. ret = mov_parse_stsd_data(c, pb, st, sc,
  2200. size - (avio_tell(pb) - start_pos));
  2201. if (ret < 0)
  2202. return ret;
  2203. }
  2204. /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
  2205. a.size = size - (avio_tell(pb) - start_pos);
  2206. if (a.size > 8) {
  2207. if ((ret = mov_read_default(c, pb, a)) < 0)
  2208. return ret;
  2209. } else if (a.size > 0)
  2210. avio_skip(pb, a.size);
  2211. if (sc->extradata && st->codecpar->extradata) {
  2212. int extra_size = st->codecpar->extradata_size;
  2213. /* Move the current stream extradata to the stream context one. */
  2214. sc->extradata_size[pseudo_stream_id] = extra_size;
  2215. sc->extradata[pseudo_stream_id] = st->codecpar->extradata;
  2216. st->codecpar->extradata = NULL;
  2217. st->codecpar->extradata_size = 0;
  2218. }
  2219. sc->stsd_count++;
  2220. }
  2221. if (pb->eof_reached) {
  2222. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSD atom\n");
  2223. return AVERROR_EOF;
  2224. }
  2225. return 0;
  2226. }
  2227. static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2228. {
  2229. AVStream *st;
  2230. MOVStreamContext *sc;
  2231. int ret, entries;
  2232. if (c->fc->nb_streams < 1)
  2233. return 0;
  2234. st = c->fc->streams[c->fc->nb_streams - 1];
  2235. sc = st->priv_data;
  2236. sc->stsd_version = avio_r8(pb);
  2237. avio_rb24(pb); /* flags */
  2238. entries = avio_rb32(pb);
  2239. /* Each entry contains a size (4 bytes) and format (4 bytes). */
  2240. if (entries <= 0 || entries > atom.size / 8) {
  2241. av_log(c->fc, AV_LOG_ERROR, "invalid STSD entries %d\n", entries);
  2242. return AVERROR_INVALIDDATA;
  2243. }
  2244. if (sc->extradata) {
  2245. av_log(c->fc, AV_LOG_ERROR,
  2246. "Duplicate stsd found in this track.\n");
  2247. return AVERROR_INVALIDDATA;
  2248. }
  2249. /* Prepare space for hosting multiple extradata. */
  2250. sc->extradata = av_mallocz_array(entries, sizeof(*sc->extradata));
  2251. if (!sc->extradata)
  2252. return AVERROR(ENOMEM);
  2253. sc->extradata_size = av_mallocz_array(entries, sizeof(*sc->extradata_size));
  2254. if (!sc->extradata_size) {
  2255. ret = AVERROR(ENOMEM);
  2256. goto fail;
  2257. }
  2258. ret = ff_mov_read_stsd_entries(c, pb, entries);
  2259. if (ret < 0)
  2260. goto fail;
  2261. /* Restore back the primary extradata. */
  2262. av_freep(&st->codecpar->extradata);
  2263. st->codecpar->extradata_size = sc->extradata_size[0];
  2264. if (sc->extradata_size[0]) {
  2265. st->codecpar->extradata = av_mallocz(sc->extradata_size[0] + AV_INPUT_BUFFER_PADDING_SIZE);
  2266. if (!st->codecpar->extradata)
  2267. return AVERROR(ENOMEM);
  2268. memcpy(st->codecpar->extradata, sc->extradata[0], sc->extradata_size[0]);
  2269. }
  2270. return mov_finalize_stsd_codec(c, pb, st, sc);
  2271. fail:
  2272. if (sc->extradata) {
  2273. int j;
  2274. for (j = 0; j < sc->stsd_count; j++)
  2275. av_freep(&sc->extradata[j]);
  2276. }
  2277. av_freep(&sc->extradata);
  2278. av_freep(&sc->extradata_size);
  2279. return ret;
  2280. }
  2281. static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2282. {
  2283. AVStream *st;
  2284. MOVStreamContext *sc;
  2285. unsigned int i, entries;
  2286. if (c->fc->nb_streams < 1)
  2287. return 0;
  2288. st = c->fc->streams[c->fc->nb_streams-1];
  2289. sc = st->priv_data;
  2290. avio_r8(pb); /* version */
  2291. avio_rb24(pb); /* flags */
  2292. entries = avio_rb32(pb);
  2293. if ((uint64_t)entries * 12 + 4 > atom.size)
  2294. return AVERROR_INVALIDDATA;
  2295. av_log(c->fc, AV_LOG_TRACE, "track[%u].stsc.entries = %u\n", c->fc->nb_streams - 1, entries);
  2296. if (!entries)
  2297. return 0;
  2298. if (sc->stsc_data)
  2299. av_log(c->fc, AV_LOG_WARNING, "Duplicated STSC atom\n");
  2300. av_free(sc->stsc_data);
  2301. sc->stsc_count = 0;
  2302. sc->stsc_data = av_malloc_array(entries, sizeof(*sc->stsc_data));
  2303. if (!sc->stsc_data)
  2304. return AVERROR(ENOMEM);
  2305. for (i = 0; i < entries && !pb->eof_reached; i++) {
  2306. sc->stsc_data[i].first = avio_rb32(pb);
  2307. sc->stsc_data[i].count = avio_rb32(pb);
  2308. sc->stsc_data[i].id = avio_rb32(pb);
  2309. }
  2310. sc->stsc_count = i;
  2311. for (i = sc->stsc_count - 1; i < UINT_MAX; i--) {
  2312. int64_t first_min = i + 1;
  2313. if ((i+1 < sc->stsc_count && sc->stsc_data[i].first >= sc->stsc_data[i+1].first) ||
  2314. (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first) ||
  2315. sc->stsc_data[i].first < first_min ||
  2316. sc->stsc_data[i].count < 1 ||
  2317. sc->stsc_data[i].id < 1) {
  2318. av_log(c->fc, AV_LOG_WARNING, "STSC entry %d is invalid (first=%d count=%d id=%d)\n", i, sc->stsc_data[i].first, sc->stsc_data[i].count, sc->stsc_data[i].id);
  2319. if (i+1 >= sc->stsc_count) {
  2320. if (sc->stsc_data[i].count == 0 && i > 0) {
  2321. sc->stsc_count --;
  2322. continue;
  2323. }
  2324. sc->stsc_data[i].first = FFMAX(sc->stsc_data[i].first, first_min);
  2325. if (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first)
  2326. sc->stsc_data[i].first = FFMIN(sc->stsc_data[i-1].first + 1LL, INT_MAX);
  2327. sc->stsc_data[i].count = FFMAX(sc->stsc_data[i].count, 1);
  2328. sc->stsc_data[i].id = FFMAX(sc->stsc_data[i].id, 1);
  2329. continue;
  2330. }
  2331. av_assert0(sc->stsc_data[i+1].first >= 2);
  2332. // We replace this entry by the next valid
  2333. sc->stsc_data[i].first = sc->stsc_data[i+1].first - 1;
  2334. sc->stsc_data[i].count = sc->stsc_data[i+1].count;
  2335. sc->stsc_data[i].id = sc->stsc_data[i+1].id;
  2336. }
  2337. }
  2338. if (pb->eof_reached) {
  2339. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSC atom\n");
  2340. return AVERROR_EOF;
  2341. }
  2342. return 0;
  2343. }
  2344. static inline int mov_stsc_index_valid(unsigned int index, unsigned int count)
  2345. {
  2346. return index < count - 1;
  2347. }
  2348. /* Compute the samples value for the stsc entry at the given index. */
  2349. static inline int64_t mov_get_stsc_samples(MOVStreamContext *sc, unsigned int index)
  2350. {
  2351. int chunk_count;
  2352. if (mov_stsc_index_valid(index, sc->stsc_count))
  2353. chunk_count = sc->stsc_data[index + 1].first - sc->stsc_data[index].first;
  2354. else {
  2355. // Validation for stsc / stco happens earlier in mov_read_stsc + mov_read_trak.
  2356. av_assert0(sc->stsc_data[index].first <= sc->chunk_count);
  2357. chunk_count = sc->chunk_count - (sc->stsc_data[index].first - 1);
  2358. }
  2359. return sc->stsc_data[index].count * (int64_t)chunk_count;
  2360. }
  2361. static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2362. {
  2363. AVStream *st;
  2364. MOVStreamContext *sc;
  2365. unsigned i, entries;
  2366. if (c->fc->nb_streams < 1)
  2367. return 0;
  2368. st = c->fc->streams[c->fc->nb_streams-1];
  2369. sc = st->priv_data;
  2370. avio_rb32(pb); // version + flags
  2371. entries = avio_rb32(pb);
  2372. if (sc->stps_data)
  2373. av_log(c->fc, AV_LOG_WARNING, "Duplicated STPS atom\n");
  2374. av_free(sc->stps_data);
  2375. sc->stps_count = 0;
  2376. sc->stps_data = av_malloc_array(entries, sizeof(*sc->stps_data));
  2377. if (!sc->stps_data)
  2378. return AVERROR(ENOMEM);
  2379. for (i = 0; i < entries && !pb->eof_reached; i++) {
  2380. sc->stps_data[i] = avio_rb32(pb);
  2381. }
  2382. sc->stps_count = i;
  2383. if (pb->eof_reached) {
  2384. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STPS atom\n");
  2385. return AVERROR_EOF;
  2386. }
  2387. return 0;
  2388. }
  2389. static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2390. {
  2391. AVStream *st;
  2392. MOVStreamContext *sc;
  2393. unsigned int i, entries;
  2394. if (c->fc->nb_streams < 1)
  2395. return 0;
  2396. st = c->fc->streams[c->fc->nb_streams-1];
  2397. sc = st->priv_data;
  2398. avio_r8(pb); /* version */
  2399. avio_rb24(pb); /* flags */
  2400. entries = avio_rb32(pb);
  2401. av_log(c->fc, AV_LOG_TRACE, "keyframe_count = %u\n", entries);
  2402. if (!entries) {
  2403. sc->keyframe_absent = 1;
  2404. if (!st->need_parsing && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
  2405. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  2406. return 0;
  2407. }
  2408. if (sc->keyframes)
  2409. av_log(c->fc, AV_LOG_WARNING, "Duplicated STSS atom\n");
  2410. if (entries >= UINT_MAX / sizeof(int))
  2411. return AVERROR_INVALIDDATA;
  2412. av_freep(&sc->keyframes);
  2413. sc->keyframe_count = 0;
  2414. sc->keyframes = av_malloc_array(entries, sizeof(*sc->keyframes));
  2415. if (!sc->keyframes)
  2416. return AVERROR(ENOMEM);
  2417. for (i = 0; i < entries && !pb->eof_reached; i++) {
  2418. sc->keyframes[i] = avio_rb32(pb);
  2419. }
  2420. sc->keyframe_count = i;
  2421. if (pb->eof_reached) {
  2422. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSS atom\n");
  2423. return AVERROR_EOF;
  2424. }
  2425. return 0;
  2426. }
  2427. static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2428. {
  2429. AVStream *st;
  2430. MOVStreamContext *sc;
  2431. unsigned int i, entries, sample_size, field_size, num_bytes;
  2432. GetBitContext gb;
  2433. unsigned char* buf;
  2434. int ret;
  2435. if (c->fc->nb_streams < 1)
  2436. return 0;
  2437. st = c->fc->streams[c->fc->nb_streams-1];
  2438. sc = st->priv_data;
  2439. avio_r8(pb); /* version */
  2440. avio_rb24(pb); /* flags */
  2441. if (atom.type == MKTAG('s','t','s','z')) {
  2442. sample_size = avio_rb32(pb);
  2443. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  2444. sc->sample_size = sample_size;
  2445. sc->stsz_sample_size = sample_size;
  2446. field_size = 32;
  2447. } else {
  2448. sample_size = 0;
  2449. avio_rb24(pb); /* reserved */
  2450. field_size = avio_r8(pb);
  2451. }
  2452. entries = avio_rb32(pb);
  2453. av_log(c->fc, AV_LOG_TRACE, "sample_size = %u sample_count = %u\n", sc->sample_size, entries);
  2454. sc->sample_count = entries;
  2455. if (sample_size)
  2456. return 0;
  2457. if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
  2458. av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %u\n", field_size);
  2459. return AVERROR_INVALIDDATA;
  2460. }
  2461. if (!entries)
  2462. return 0;
  2463. if (entries >= (UINT_MAX - 4) / field_size)
  2464. return AVERROR_INVALIDDATA;
  2465. if (sc->sample_sizes)
  2466. av_log(c->fc, AV_LOG_WARNING, "Duplicated STSZ atom\n");
  2467. av_free(sc->sample_sizes);
  2468. sc->sample_count = 0;
  2469. sc->sample_sizes = av_malloc_array(entries, sizeof(*sc->sample_sizes));
  2470. if (!sc->sample_sizes)
  2471. return AVERROR(ENOMEM);
  2472. num_bytes = (entries*field_size+4)>>3;
  2473. buf = av_malloc(num_bytes+AV_INPUT_BUFFER_PADDING_SIZE);
  2474. if (!buf) {
  2475. av_freep(&sc->sample_sizes);
  2476. return AVERROR(ENOMEM);
  2477. }
  2478. ret = ffio_read_size(pb, buf, num_bytes);
  2479. if (ret < 0) {
  2480. av_freep(&sc->sample_sizes);
  2481. av_free(buf);
  2482. av_log(c->fc, AV_LOG_WARNING, "STSZ atom truncated\n");
  2483. return 0;
  2484. }
  2485. init_get_bits(&gb, buf, 8*num_bytes);
  2486. for (i = 0; i < entries && !pb->eof_reached; i++) {
  2487. sc->sample_sizes[i] = get_bits_long(&gb, field_size);
  2488. if (sc->sample_sizes[i] < 0) {
  2489. av_free(buf);
  2490. av_log(c->fc, AV_LOG_ERROR, "Invalid sample size %d\n", sc->sample_sizes[i]);
  2491. return AVERROR_INVALIDDATA;
  2492. }
  2493. sc->data_size += sc->sample_sizes[i];
  2494. }
  2495. sc->sample_count = i;
  2496. av_free(buf);
  2497. if (pb->eof_reached) {
  2498. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSZ atom\n");
  2499. return AVERROR_EOF;
  2500. }
  2501. return 0;
  2502. }
  2503. static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2504. {
  2505. AVStream *st;
  2506. MOVStreamContext *sc;
  2507. unsigned int i, entries, alloc_size = 0;
  2508. int64_t duration = 0;
  2509. int64_t total_sample_count = 0;
  2510. if (c->fc->nb_streams < 1)
  2511. return 0;
  2512. st = c->fc->streams[c->fc->nb_streams-1];
  2513. sc = st->priv_data;
  2514. avio_r8(pb); /* version */
  2515. avio_rb24(pb); /* flags */
  2516. entries = avio_rb32(pb);
  2517. av_log(c->fc, AV_LOG_TRACE, "track[%u].stts.entries = %u\n",
  2518. c->fc->nb_streams-1, entries);
  2519. if (sc->stts_data)
  2520. av_log(c->fc, AV_LOG_WARNING, "Duplicated STTS atom\n");
  2521. av_freep(&sc->stts_data);
  2522. sc->stts_count = 0;
  2523. if (entries >= INT_MAX / sizeof(*sc->stts_data))
  2524. return AVERROR(ENOMEM);
  2525. for (i = 0; i < entries && !pb->eof_reached; i++) {
  2526. int sample_duration;
  2527. unsigned int sample_count;
  2528. unsigned int min_entries = FFMIN(FFMAX(i + 1, 1024 * 1024), entries);
  2529. MOVStts *stts_data = av_fast_realloc(sc->stts_data, &alloc_size,
  2530. min_entries * sizeof(*sc->stts_data));
  2531. if (!stts_data) {
  2532. av_freep(&sc->stts_data);
  2533. sc->stts_count = 0;
  2534. return AVERROR(ENOMEM);
  2535. }
  2536. sc->stts_count = min_entries;
  2537. sc->stts_data = stts_data;
  2538. sample_count = avio_rb32(pb);
  2539. sample_duration = avio_rb32(pb);
  2540. sc->stts_data[i].count= sample_count;
  2541. sc->stts_data[i].duration= sample_duration;
  2542. av_log(c->fc, AV_LOG_TRACE, "sample_count=%d, sample_duration=%d\n",
  2543. sample_count, sample_duration);
  2544. duration+=(int64_t)sample_duration*(uint64_t)sample_count;
  2545. total_sample_count+=sample_count;
  2546. }
  2547. sc->stts_count = i;
  2548. if (duration > 0 &&
  2549. duration <= INT64_MAX - sc->duration_for_fps &&
  2550. total_sample_count <= INT_MAX - sc->nb_frames_for_fps) {
  2551. sc->duration_for_fps += duration;
  2552. sc->nb_frames_for_fps += total_sample_count;
  2553. }
  2554. if (pb->eof_reached) {
  2555. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STTS atom\n");
  2556. return AVERROR_EOF;
  2557. }
  2558. st->nb_frames= total_sample_count;
  2559. if (duration)
  2560. st->duration= FFMIN(st->duration, duration);
  2561. sc->track_end = duration;
  2562. return 0;
  2563. }
  2564. static int mov_read_sdtp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2565. {
  2566. AVStream *st;
  2567. MOVStreamContext *sc;
  2568. int64_t i, entries;
  2569. if (c->fc->nb_streams < 1)
  2570. return 0;
  2571. st = c->fc->streams[c->fc->nb_streams - 1];
  2572. sc = st->priv_data;
  2573. avio_r8(pb); /* version */
  2574. avio_rb24(pb); /* flags */
  2575. entries = atom.size - 4;
  2576. av_log(c->fc, AV_LOG_TRACE, "track[%u].sdtp.entries = %" PRId64 "\n",
  2577. c->fc->nb_streams - 1, entries);
  2578. if (sc->sdtp_data)
  2579. av_log(c->fc, AV_LOG_WARNING, "Duplicated SDTP atom\n");
  2580. av_freep(&sc->sdtp_data);
  2581. sc->sdtp_count = 0;
  2582. sc->sdtp_data = av_mallocz(entries);
  2583. if (!sc->sdtp_data)
  2584. return AVERROR(ENOMEM);
  2585. for (i = 0; i < entries && !pb->eof_reached; i++)
  2586. sc->sdtp_data[i] = avio_r8(pb);
  2587. sc->sdtp_count = i;
  2588. return 0;
  2589. }
  2590. static void mov_update_dts_shift(MOVStreamContext *sc, int duration, void *logctx)
  2591. {
  2592. if (duration < 0) {
  2593. if (duration == INT_MIN) {
  2594. av_log(logctx, AV_LOG_WARNING, "mov_update_dts_shift(): dts_shift set to %d\n", INT_MAX);
  2595. duration++;
  2596. }
  2597. sc->dts_shift = FFMAX(sc->dts_shift, -duration);
  2598. }
  2599. }
  2600. static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2601. {
  2602. AVStream *st;
  2603. MOVStreamContext *sc;
  2604. unsigned int i, entries, ctts_count = 0;
  2605. if (c->fc->nb_streams < 1)
  2606. return 0;
  2607. st = c->fc->streams[c->fc->nb_streams-1];
  2608. sc = st->priv_data;
  2609. avio_r8(pb); /* version */
  2610. avio_rb24(pb); /* flags */
  2611. entries = avio_rb32(pb);
  2612. av_log(c->fc, AV_LOG_TRACE, "track[%u].ctts.entries = %u\n", c->fc->nb_streams - 1, entries);
  2613. if (!entries)
  2614. return 0;
  2615. if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
  2616. return AVERROR_INVALIDDATA;
  2617. av_freep(&sc->ctts_data);
  2618. sc->ctts_data = av_fast_realloc(NULL, &sc->ctts_allocated_size, entries * sizeof(*sc->ctts_data));
  2619. if (!sc->ctts_data)
  2620. return AVERROR(ENOMEM);
  2621. for (i = 0; i < entries && !pb->eof_reached; i++) {
  2622. int count = avio_rb32(pb);
  2623. int duration = avio_rb32(pb);
  2624. if (count <= 0) {
  2625. av_log(c->fc, AV_LOG_TRACE,
  2626. "ignoring CTTS entry with count=%d duration=%d\n",
  2627. count, duration);
  2628. continue;
  2629. }
  2630. add_ctts_entry(&sc->ctts_data, &ctts_count, &sc->ctts_allocated_size,
  2631. count, duration);
  2632. av_log(c->fc, AV_LOG_TRACE, "count=%d, duration=%d\n",
  2633. count, duration);
  2634. if (FFNABS(duration) < -(1<<28) && i+2<entries) {
  2635. av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
  2636. av_freep(&sc->ctts_data);
  2637. sc->ctts_count = 0;
  2638. return 0;
  2639. }
  2640. if (i+2<entries)
  2641. mov_update_dts_shift(sc, duration, c->fc);
  2642. }
  2643. sc->ctts_count = ctts_count;
  2644. if (pb->eof_reached) {
  2645. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted CTTS atom\n");
  2646. return AVERROR_EOF;
  2647. }
  2648. av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift);
  2649. return 0;
  2650. }
  2651. static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2652. {
  2653. AVStream *st;
  2654. MOVStreamContext *sc;
  2655. unsigned int i, entries;
  2656. uint8_t version;
  2657. uint32_t grouping_type;
  2658. if (c->fc->nb_streams < 1)
  2659. return 0;
  2660. st = c->fc->streams[c->fc->nb_streams-1];
  2661. sc = st->priv_data;
  2662. version = avio_r8(pb); /* version */
  2663. avio_rb24(pb); /* flags */
  2664. grouping_type = avio_rl32(pb);
  2665. if (grouping_type != MKTAG( 'r','a','p',' '))
  2666. return 0; /* only support 'rap ' grouping */
  2667. if (version == 1)
  2668. avio_rb32(pb); /* grouping_type_parameter */
  2669. entries = avio_rb32(pb);
  2670. if (!entries)
  2671. return 0;
  2672. if (sc->rap_group)
  2673. av_log(c->fc, AV_LOG_WARNING, "Duplicated SBGP atom\n");
  2674. av_free(sc->rap_group);
  2675. sc->rap_group_count = 0;
  2676. sc->rap_group = av_malloc_array(entries, sizeof(*sc->rap_group));
  2677. if (!sc->rap_group)
  2678. return AVERROR(ENOMEM);
  2679. for (i = 0; i < entries && !pb->eof_reached; i++) {
  2680. sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
  2681. sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
  2682. }
  2683. sc->rap_group_count = i;
  2684. if (pb->eof_reached) {
  2685. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted SBGP atom\n");
  2686. return AVERROR_EOF;
  2687. }
  2688. return 0;
  2689. }
  2690. /**
  2691. * Get ith edit list entry (media time, duration).
  2692. */
  2693. static int get_edit_list_entry(MOVContext *mov,
  2694. const MOVStreamContext *msc,
  2695. unsigned int edit_list_index,
  2696. int64_t *edit_list_media_time,
  2697. int64_t *edit_list_duration,
  2698. int64_t global_timescale)
  2699. {
  2700. if (edit_list_index == msc->elst_count) {
  2701. return 0;
  2702. }
  2703. *edit_list_media_time = msc->elst_data[edit_list_index].time;
  2704. *edit_list_duration = msc->elst_data[edit_list_index].duration;
  2705. /* duration is in global timescale units;convert to msc timescale */
  2706. if (global_timescale == 0) {
  2707. avpriv_request_sample(mov->fc, "Support for mvhd.timescale = 0 with editlists");
  2708. return 0;
  2709. }
  2710. *edit_list_duration = av_rescale(*edit_list_duration, msc->time_scale,
  2711. global_timescale);
  2712. return 1;
  2713. }
  2714. /**
  2715. * Find the closest previous frame to the timestamp_pts, in e_old index
  2716. * entries. Searching for just any frame / just key frames can be controlled by
  2717. * last argument 'flag'.
  2718. * Note that if ctts_data is not NULL, we will always search for a key frame
  2719. * irrespective of the value of 'flag'. If we don't find any keyframe, we will
  2720. * return the first frame of the video.
  2721. *
  2722. * Here the timestamp_pts is considered to be a presentation timestamp and
  2723. * the timestamp of index entries are considered to be decoding timestamps.
  2724. *
  2725. * Returns 0 if successful in finding a frame, else returns -1.
  2726. * Places the found index corresponding output arg.
  2727. *
  2728. * If ctts_old is not NULL, then refines the searched entry by searching
  2729. * backwards from the found timestamp, to find the frame with correct PTS.
  2730. *
  2731. * Places the found ctts_index and ctts_sample in corresponding output args.
  2732. */
  2733. static int find_prev_closest_index(AVStream *st,
  2734. AVIndexEntry *e_old,
  2735. int nb_old,
  2736. MOVStts* ctts_data,
  2737. int64_t ctts_count,
  2738. int64_t timestamp_pts,
  2739. int flag,
  2740. int64_t* index,
  2741. int64_t* ctts_index,
  2742. int64_t* ctts_sample)
  2743. {
  2744. MOVStreamContext *msc = st->priv_data;
  2745. AVIndexEntry *e_keep = st->index_entries;
  2746. int nb_keep = st->nb_index_entries;
  2747. int64_t i = 0;
  2748. int64_t index_ctts_count;
  2749. av_assert0(index);
  2750. // If dts_shift > 0, then all the index timestamps will have to be offset by
  2751. // at least dts_shift amount to obtain PTS.
  2752. // Hence we decrement the searched timestamp_pts by dts_shift to find the closest index element.
  2753. if (msc->dts_shift > 0) {
  2754. timestamp_pts -= msc->dts_shift;
  2755. }
  2756. st->index_entries = e_old;
  2757. st->nb_index_entries = nb_old;
  2758. *index = av_index_search_timestamp(st, timestamp_pts, flag | AVSEEK_FLAG_BACKWARD);
  2759. // Keep going backwards in the index entries until the timestamp is the same.
  2760. if (*index >= 0) {
  2761. for (i = *index; i > 0 && e_old[i].timestamp == e_old[i - 1].timestamp;
  2762. i--) {
  2763. if ((flag & AVSEEK_FLAG_ANY) ||
  2764. (e_old[i - 1].flags & AVINDEX_KEYFRAME)) {
  2765. *index = i - 1;
  2766. }
  2767. }
  2768. }
  2769. // If we have CTTS then refine the search, by searching backwards over PTS
  2770. // computed by adding corresponding CTTS durations to index timestamps.
  2771. if (ctts_data && *index >= 0) {
  2772. av_assert0(ctts_index);
  2773. av_assert0(ctts_sample);
  2774. // Find out the ctts_index for the found frame.
  2775. *ctts_index = 0;
  2776. *ctts_sample = 0;
  2777. for (index_ctts_count = 0; index_ctts_count < *index; index_ctts_count++) {
  2778. if (*ctts_index < ctts_count) {
  2779. (*ctts_sample)++;
  2780. if (ctts_data[*ctts_index].count == *ctts_sample) {
  2781. (*ctts_index)++;
  2782. *ctts_sample = 0;
  2783. }
  2784. }
  2785. }
  2786. while (*index >= 0 && (*ctts_index) >= 0 && (*ctts_index) < ctts_count) {
  2787. // Find a "key frame" with PTS <= timestamp_pts (So that we can decode B-frames correctly).
  2788. // No need to add dts_shift to the timestamp here becase timestamp_pts has already been
  2789. // compensated by dts_shift above.
  2790. if ((e_old[*index].timestamp + ctts_data[*ctts_index].duration) <= timestamp_pts &&
  2791. (e_old[*index].flags & AVINDEX_KEYFRAME)) {
  2792. break;
  2793. }
  2794. (*index)--;
  2795. if (*ctts_sample == 0) {
  2796. (*ctts_index)--;
  2797. if (*ctts_index >= 0)
  2798. *ctts_sample = ctts_data[*ctts_index].count - 1;
  2799. } else {
  2800. (*ctts_sample)--;
  2801. }
  2802. }
  2803. }
  2804. /* restore AVStream state*/
  2805. st->index_entries = e_keep;
  2806. st->nb_index_entries = nb_keep;
  2807. return *index >= 0 ? 0 : -1;
  2808. }
  2809. /**
  2810. * Add index entry with the given values, to the end of st->index_entries.
  2811. * Returns the new size st->index_entries if successful, else returns -1.
  2812. *
  2813. * This function is similar to ff_add_index_entry in libavformat/utils.c
  2814. * except that here we are always unconditionally adding an index entry to
  2815. * the end, instead of searching the entries list and skipping the add if
  2816. * there is an existing entry with the same timestamp.
  2817. * This is needed because the mov_fix_index calls this func with the same
  2818. * unincremented timestamp for successive discarded frames.
  2819. */
  2820. static int64_t add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
  2821. int size, int distance, int flags)
  2822. {
  2823. AVIndexEntry *entries, *ie;
  2824. int64_t index = -1;
  2825. const size_t min_size_needed = (st->nb_index_entries + 1) * sizeof(AVIndexEntry);
  2826. // Double the allocation each time, to lower memory fragmentation.
  2827. // Another difference from ff_add_index_entry function.
  2828. const size_t requested_size =
  2829. min_size_needed > st->index_entries_allocated_size ?
  2830. FFMAX(min_size_needed, 2 * st->index_entries_allocated_size) :
  2831. min_size_needed;
  2832. if (st->nb_index_entries + 1U >= UINT_MAX / sizeof(AVIndexEntry))
  2833. return -1;
  2834. entries = av_fast_realloc(st->index_entries,
  2835. &st->index_entries_allocated_size,
  2836. requested_size);
  2837. if (!entries)
  2838. return -1;
  2839. st->index_entries= entries;
  2840. index= st->nb_index_entries++;
  2841. ie= &entries[index];
  2842. ie->pos = pos;
  2843. ie->timestamp = timestamp;
  2844. ie->min_distance= distance;
  2845. ie->size= size;
  2846. ie->flags = flags;
  2847. return index;
  2848. }
  2849. /**
  2850. * Rewrite timestamps of index entries in the range [end_index - frame_duration_buffer_size, end_index)
  2851. * by subtracting end_ts successively by the amounts given in frame_duration_buffer.
  2852. */
  2853. static void fix_index_entry_timestamps(AVStream* st, int end_index, int64_t end_ts,
  2854. int64_t* frame_duration_buffer,
  2855. int frame_duration_buffer_size) {
  2856. int i = 0;
  2857. av_assert0(end_index >= 0 && end_index <= st->nb_index_entries);
  2858. for (i = 0; i < frame_duration_buffer_size; i++) {
  2859. end_ts -= frame_duration_buffer[frame_duration_buffer_size - 1 - i];
  2860. st->index_entries[end_index - 1 - i].timestamp = end_ts;
  2861. }
  2862. }
  2863. /**
  2864. * Append a new ctts entry to ctts_data.
  2865. * Returns the new ctts_count if successful, else returns -1.
  2866. */
  2867. static int64_t add_ctts_entry(MOVStts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size,
  2868. int count, int duration)
  2869. {
  2870. MOVStts *ctts_buf_new;
  2871. const size_t min_size_needed = (*ctts_count + 1) * sizeof(MOVStts);
  2872. const size_t requested_size =
  2873. min_size_needed > *allocated_size ?
  2874. FFMAX(min_size_needed, 2 * (*allocated_size)) :
  2875. min_size_needed;
  2876. if ((unsigned)(*ctts_count) >= UINT_MAX / sizeof(MOVStts) - 1)
  2877. return -1;
  2878. ctts_buf_new = av_fast_realloc(*ctts_data, allocated_size, requested_size);
  2879. if (!ctts_buf_new)
  2880. return -1;
  2881. *ctts_data = ctts_buf_new;
  2882. ctts_buf_new[*ctts_count].count = count;
  2883. ctts_buf_new[*ctts_count].duration = duration;
  2884. *ctts_count = (*ctts_count) + 1;
  2885. return *ctts_count;
  2886. }
  2887. #define MAX_REORDER_DELAY 16
  2888. static void mov_estimate_video_delay(MOVContext *c, AVStream* st)
  2889. {
  2890. MOVStreamContext *msc = st->priv_data;
  2891. int ind;
  2892. int ctts_ind = 0;
  2893. int ctts_sample = 0;
  2894. int64_t pts_buf[MAX_REORDER_DELAY + 1]; // Circular buffer to sort pts.
  2895. int buf_start = 0;
  2896. int j, r, num_swaps;
  2897. for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
  2898. pts_buf[j] = INT64_MIN;
  2899. if (st->codecpar->video_delay <= 0 && msc->ctts_data &&
  2900. st->codecpar->codec_id == AV_CODEC_ID_H264) {
  2901. st->codecpar->video_delay = 0;
  2902. for (ind = 0; ind < st->nb_index_entries && ctts_ind < msc->ctts_count; ++ind) {
  2903. // Point j to the last elem of the buffer and insert the current pts there.
  2904. j = buf_start;
  2905. buf_start = (buf_start + 1);
  2906. if (buf_start == MAX_REORDER_DELAY + 1)
  2907. buf_start = 0;
  2908. pts_buf[j] = st->index_entries[ind].timestamp + msc->ctts_data[ctts_ind].duration;
  2909. // The timestamps that are already in the sorted buffer, and are greater than the
  2910. // current pts, are exactly the timestamps that need to be buffered to output PTS
  2911. // in correct sorted order.
  2912. // Hence the video delay (which is the buffer size used to sort DTS and output PTS),
  2913. // can be computed as the maximum no. of swaps any particular timestamp needs to
  2914. // go through, to keep this buffer in sorted order.
  2915. num_swaps = 0;
  2916. while (j != buf_start) {
  2917. r = j - 1;
  2918. if (r < 0) r = MAX_REORDER_DELAY;
  2919. if (pts_buf[j] < pts_buf[r]) {
  2920. FFSWAP(int64_t, pts_buf[j], pts_buf[r]);
  2921. ++num_swaps;
  2922. } else {
  2923. break;
  2924. }
  2925. j = r;
  2926. }
  2927. st->codecpar->video_delay = FFMAX(st->codecpar->video_delay, num_swaps);
  2928. ctts_sample++;
  2929. if (ctts_sample == msc->ctts_data[ctts_ind].count) {
  2930. ctts_ind++;
  2931. ctts_sample = 0;
  2932. }
  2933. }
  2934. av_log(c->fc, AV_LOG_DEBUG, "Setting codecpar->delay to %d for stream st: %d\n",
  2935. st->codecpar->video_delay, st->index);
  2936. }
  2937. }
  2938. static void mov_current_sample_inc(MOVStreamContext *sc)
  2939. {
  2940. sc->current_sample++;
  2941. sc->current_index++;
  2942. if (sc->index_ranges &&
  2943. sc->current_index >= sc->current_index_range->end &&
  2944. sc->current_index_range->end) {
  2945. sc->current_index_range++;
  2946. sc->current_index = sc->current_index_range->start;
  2947. }
  2948. }
  2949. static void mov_current_sample_dec(MOVStreamContext *sc)
  2950. {
  2951. sc->current_sample--;
  2952. sc->current_index--;
  2953. if (sc->index_ranges &&
  2954. sc->current_index < sc->current_index_range->start &&
  2955. sc->current_index_range > sc->index_ranges) {
  2956. sc->current_index_range--;
  2957. sc->current_index = sc->current_index_range->end - 1;
  2958. }
  2959. }
  2960. static void mov_current_sample_set(MOVStreamContext *sc, int current_sample)
  2961. {
  2962. int64_t range_size;
  2963. sc->current_sample = current_sample;
  2964. sc->current_index = current_sample;
  2965. if (!sc->index_ranges) {
  2966. return;
  2967. }
  2968. for (sc->current_index_range = sc->index_ranges;
  2969. sc->current_index_range->end;
  2970. sc->current_index_range++) {
  2971. range_size = sc->current_index_range->end - sc->current_index_range->start;
  2972. if (range_size > current_sample) {
  2973. sc->current_index = sc->current_index_range->start + current_sample;
  2974. break;
  2975. }
  2976. current_sample -= range_size;
  2977. }
  2978. }
  2979. /**
  2980. * Fix st->index_entries, so that it contains only the entries (and the entries
  2981. * which are needed to decode them) that fall in the edit list time ranges.
  2982. * Also fixes the timestamps of the index entries to match the timeline
  2983. * specified the edit lists.
  2984. */
  2985. static void mov_fix_index(MOVContext *mov, AVStream *st)
  2986. {
  2987. MOVStreamContext *msc = st->priv_data;
  2988. AVIndexEntry *e_old = st->index_entries;
  2989. int nb_old = st->nb_index_entries;
  2990. const AVIndexEntry *e_old_end = e_old + nb_old;
  2991. const AVIndexEntry *current = NULL;
  2992. MOVStts *ctts_data_old = msc->ctts_data;
  2993. int64_t ctts_index_old = 0;
  2994. int64_t ctts_sample_old = 0;
  2995. int64_t ctts_count_old = msc->ctts_count;
  2996. int64_t edit_list_media_time = 0;
  2997. int64_t edit_list_duration = 0;
  2998. int64_t frame_duration = 0;
  2999. int64_t edit_list_dts_counter = 0;
  3000. int64_t edit_list_dts_entry_end = 0;
  3001. int64_t edit_list_start_ctts_sample = 0;
  3002. int64_t curr_cts;
  3003. int64_t curr_ctts = 0;
  3004. int64_t empty_edits_sum_duration = 0;
  3005. int64_t edit_list_index = 0;
  3006. int64_t index;
  3007. int flags;
  3008. int64_t start_dts = 0;
  3009. int64_t edit_list_start_encountered = 0;
  3010. int64_t search_timestamp = 0;
  3011. int64_t* frame_duration_buffer = NULL;
  3012. int num_discarded_begin = 0;
  3013. int first_non_zero_audio_edit = -1;
  3014. int packet_skip_samples = 0;
  3015. MOVIndexRange *current_index_range;
  3016. int i;
  3017. int found_keyframe_after_edit = 0;
  3018. int found_non_empty_edit = 0;
  3019. if (!msc->elst_data || msc->elst_count <= 0 || nb_old <= 0) {
  3020. return;
  3021. }
  3022. // allocate the index ranges array
  3023. msc->index_ranges = av_malloc((msc->elst_count + 1) * sizeof(msc->index_ranges[0]));
  3024. if (!msc->index_ranges) {
  3025. av_log(mov->fc, AV_LOG_ERROR, "Cannot allocate index ranges buffer\n");
  3026. return;
  3027. }
  3028. msc->current_index_range = msc->index_ranges;
  3029. current_index_range = msc->index_ranges - 1;
  3030. // Clean AVStream from traces of old index
  3031. st->index_entries = NULL;
  3032. st->index_entries_allocated_size = 0;
  3033. st->nb_index_entries = 0;
  3034. // Clean ctts fields of MOVStreamContext
  3035. msc->ctts_data = NULL;
  3036. msc->ctts_count = 0;
  3037. msc->ctts_index = 0;
  3038. msc->ctts_sample = 0;
  3039. msc->ctts_allocated_size = 0;
  3040. // Reinitialize min_corrected_pts so that it can be computed again.
  3041. msc->min_corrected_pts = -1;
  3042. // If the dts_shift is positive (in case of negative ctts values in mov),
  3043. // then negate the DTS by dts_shift
  3044. if (msc->dts_shift > 0) {
  3045. edit_list_dts_entry_end -= msc->dts_shift;
  3046. av_log(mov->fc, AV_LOG_DEBUG, "Shifting DTS by %d because of negative CTTS.\n", msc->dts_shift);
  3047. }
  3048. start_dts = edit_list_dts_entry_end;
  3049. while (get_edit_list_entry(mov, msc, edit_list_index, &edit_list_media_time,
  3050. &edit_list_duration, mov->time_scale)) {
  3051. av_log(mov->fc, AV_LOG_DEBUG, "Processing st: %d, edit list %"PRId64" - media time: %"PRId64", duration: %"PRId64"\n",
  3052. st->index, edit_list_index, edit_list_media_time, edit_list_duration);
  3053. edit_list_index++;
  3054. edit_list_dts_counter = edit_list_dts_entry_end;
  3055. edit_list_dts_entry_end += edit_list_duration;
  3056. num_discarded_begin = 0;
  3057. if (!found_non_empty_edit && edit_list_media_time == -1) {
  3058. empty_edits_sum_duration += edit_list_duration;
  3059. continue;
  3060. }
  3061. found_non_empty_edit = 1;
  3062. // If we encounter a non-negative edit list reset the skip_samples/start_pad fields and set them
  3063. // according to the edit list below.
  3064. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  3065. if (first_non_zero_audio_edit < 0) {
  3066. first_non_zero_audio_edit = 1;
  3067. } else {
  3068. first_non_zero_audio_edit = 0;
  3069. }
  3070. if (first_non_zero_audio_edit > 0)
  3071. st->skip_samples = msc->start_pad = 0;
  3072. }
  3073. // While reordering frame index according to edit list we must handle properly
  3074. // the scenario when edit list entry starts from none key frame.
  3075. // We find closest previous key frame and preserve it and consequent frames in index.
  3076. // All frames which are outside edit list entry time boundaries will be dropped after decoding.
  3077. search_timestamp = edit_list_media_time;
  3078. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  3079. // Audio decoders like AAC need need a decoder delay samples previous to the current sample,
  3080. // to correctly decode this frame. Hence for audio we seek to a frame 1 sec. before the
  3081. // edit_list_media_time to cover the decoder delay.
  3082. search_timestamp = FFMAX(search_timestamp - msc->time_scale, e_old[0].timestamp);
  3083. }
  3084. if (find_prev_closest_index(st, e_old, nb_old, ctts_data_old, ctts_count_old, search_timestamp, 0,
  3085. &index, &ctts_index_old, &ctts_sample_old) < 0) {
  3086. av_log(mov->fc, AV_LOG_WARNING,
  3087. "st: %d edit list: %"PRId64" Missing key frame while searching for timestamp: %"PRId64"\n",
  3088. st->index, edit_list_index, search_timestamp);
  3089. if (find_prev_closest_index(st, e_old, nb_old, ctts_data_old, ctts_count_old, search_timestamp, AVSEEK_FLAG_ANY,
  3090. &index, &ctts_index_old, &ctts_sample_old) < 0) {
  3091. av_log(mov->fc, AV_LOG_WARNING,
  3092. "st: %d edit list %"PRId64" Cannot find an index entry before timestamp: %"PRId64".\n",
  3093. st->index, edit_list_index, search_timestamp);
  3094. index = 0;
  3095. ctts_index_old = 0;
  3096. ctts_sample_old = 0;
  3097. }
  3098. }
  3099. current = e_old + index;
  3100. edit_list_start_ctts_sample = ctts_sample_old;
  3101. // Iterate over index and arrange it according to edit list
  3102. edit_list_start_encountered = 0;
  3103. found_keyframe_after_edit = 0;
  3104. for (; current < e_old_end; current++, index++) {
  3105. // check if frame outside edit list mark it for discard
  3106. frame_duration = (current + 1 < e_old_end) ?
  3107. ((current + 1)->timestamp - current->timestamp) : edit_list_duration;
  3108. flags = current->flags;
  3109. // frames (pts) before or after edit list
  3110. curr_cts = current->timestamp + msc->dts_shift;
  3111. curr_ctts = 0;
  3112. if (ctts_data_old && ctts_index_old < ctts_count_old) {
  3113. curr_ctts = ctts_data_old[ctts_index_old].duration;
  3114. av_log(mov->fc, AV_LOG_DEBUG, "stts: %"PRId64" ctts: %"PRId64", ctts_index: %"PRId64", ctts_count: %"PRId64"\n",
  3115. curr_cts, curr_ctts, ctts_index_old, ctts_count_old);
  3116. curr_cts += curr_ctts;
  3117. ctts_sample_old++;
  3118. if (ctts_sample_old == ctts_data_old[ctts_index_old].count) {
  3119. if (add_ctts_entry(&msc->ctts_data, &msc->ctts_count,
  3120. &msc->ctts_allocated_size,
  3121. ctts_data_old[ctts_index_old].count - edit_list_start_ctts_sample,
  3122. ctts_data_old[ctts_index_old].duration) == -1) {
  3123. av_log(mov->fc, AV_LOG_ERROR, "Cannot add CTTS entry %"PRId64" - {%"PRId64", %d}\n",
  3124. ctts_index_old,
  3125. ctts_data_old[ctts_index_old].count - edit_list_start_ctts_sample,
  3126. ctts_data_old[ctts_index_old].duration);
  3127. break;
  3128. }
  3129. ctts_index_old++;
  3130. ctts_sample_old = 0;
  3131. edit_list_start_ctts_sample = 0;
  3132. }
  3133. }
  3134. if (curr_cts < edit_list_media_time || curr_cts >= (edit_list_duration + edit_list_media_time)) {
  3135. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->codec_id != AV_CODEC_ID_VORBIS &&
  3136. curr_cts < edit_list_media_time && curr_cts + frame_duration > edit_list_media_time &&
  3137. first_non_zero_audio_edit > 0) {
  3138. packet_skip_samples = edit_list_media_time - curr_cts;
  3139. st->skip_samples += packet_skip_samples;
  3140. // Shift the index entry timestamp by packet_skip_samples to be correct.
  3141. edit_list_dts_counter -= packet_skip_samples;
  3142. if (edit_list_start_encountered == 0) {
  3143. edit_list_start_encountered = 1;
  3144. // Make timestamps strictly monotonically increasing for audio, by rewriting timestamps for
  3145. // discarded packets.
  3146. if (frame_duration_buffer) {
  3147. fix_index_entry_timestamps(st, st->nb_index_entries, edit_list_dts_counter,
  3148. frame_duration_buffer, num_discarded_begin);
  3149. av_freep(&frame_duration_buffer);
  3150. }
  3151. }
  3152. av_log(mov->fc, AV_LOG_DEBUG, "skip %d audio samples from curr_cts: %"PRId64"\n", packet_skip_samples, curr_cts);
  3153. } else {
  3154. flags |= AVINDEX_DISCARD_FRAME;
  3155. av_log(mov->fc, AV_LOG_DEBUG, "drop a frame at curr_cts: %"PRId64" @ %"PRId64"\n", curr_cts, index);
  3156. if (edit_list_start_encountered == 0) {
  3157. num_discarded_begin++;
  3158. frame_duration_buffer = av_realloc(frame_duration_buffer,
  3159. num_discarded_begin * sizeof(int64_t));
  3160. if (!frame_duration_buffer) {
  3161. av_log(mov->fc, AV_LOG_ERROR, "Cannot reallocate frame duration buffer\n");
  3162. break;
  3163. }
  3164. frame_duration_buffer[num_discarded_begin - 1] = frame_duration;
  3165. // Increment skip_samples for the first non-zero audio edit list
  3166. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  3167. first_non_zero_audio_edit > 0 && st->codecpar->codec_id != AV_CODEC_ID_VORBIS) {
  3168. st->skip_samples += frame_duration;
  3169. }
  3170. }
  3171. }
  3172. } else {
  3173. if (msc->min_corrected_pts < 0) {
  3174. msc->min_corrected_pts = edit_list_dts_counter + curr_ctts + msc->dts_shift;
  3175. } else {
  3176. msc->min_corrected_pts = FFMIN(msc->min_corrected_pts, edit_list_dts_counter + curr_ctts + msc->dts_shift);
  3177. }
  3178. if (edit_list_start_encountered == 0) {
  3179. edit_list_start_encountered = 1;
  3180. // Make timestamps strictly monotonically increasing by rewriting timestamps for
  3181. // discarded packets.
  3182. if (frame_duration_buffer) {
  3183. fix_index_entry_timestamps(st, st->nb_index_entries, edit_list_dts_counter,
  3184. frame_duration_buffer, num_discarded_begin);
  3185. av_freep(&frame_duration_buffer);
  3186. }
  3187. }
  3188. }
  3189. if (add_index_entry(st, current->pos, edit_list_dts_counter, current->size,
  3190. current->min_distance, flags) == -1) {
  3191. av_log(mov->fc, AV_LOG_ERROR, "Cannot add index entry\n");
  3192. break;
  3193. }
  3194. // Update the index ranges array
  3195. if (current_index_range < msc->index_ranges || index != current_index_range->end) {
  3196. current_index_range++;
  3197. current_index_range->start = index;
  3198. }
  3199. current_index_range->end = index + 1;
  3200. // Only start incrementing DTS in frame_duration amounts, when we encounter a frame in edit list.
  3201. if (edit_list_start_encountered > 0) {
  3202. edit_list_dts_counter = edit_list_dts_counter + frame_duration;
  3203. }
  3204. // Break when found first key frame after edit entry completion
  3205. if ((curr_cts + frame_duration >= (edit_list_duration + edit_list_media_time)) &&
  3206. ((flags & AVINDEX_KEYFRAME) || ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)))) {
  3207. if (ctts_data_old) {
  3208. // If we have CTTS and this is the first keyframe after edit elist,
  3209. // wait for one more, because there might be trailing B-frames after this I-frame
  3210. // that do belong to the edit.
  3211. if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO && found_keyframe_after_edit == 0) {
  3212. found_keyframe_after_edit = 1;
  3213. continue;
  3214. }
  3215. if (ctts_sample_old != 0) {
  3216. if (add_ctts_entry(&msc->ctts_data, &msc->ctts_count,
  3217. &msc->ctts_allocated_size,
  3218. ctts_sample_old - edit_list_start_ctts_sample,
  3219. ctts_data_old[ctts_index_old].duration) == -1) {
  3220. av_log(mov->fc, AV_LOG_ERROR, "Cannot add CTTS entry %"PRId64" - {%"PRId64", %d}\n",
  3221. ctts_index_old, ctts_sample_old - edit_list_start_ctts_sample,
  3222. ctts_data_old[ctts_index_old].duration);
  3223. break;
  3224. }
  3225. }
  3226. }
  3227. break;
  3228. }
  3229. }
  3230. }
  3231. // If there are empty edits, then msc->min_corrected_pts might be positive
  3232. // intentionally. So we subtract the sum duration of emtpy edits here.
  3233. msc->min_corrected_pts -= empty_edits_sum_duration;
  3234. // If the minimum pts turns out to be greater than zero after fixing the index, then we subtract the
  3235. // dts by that amount to make the first pts zero.
  3236. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  3237. if (msc->min_corrected_pts > 0) {
  3238. av_log(mov->fc, AV_LOG_DEBUG, "Offset DTS by %"PRId64" to make first pts zero.\n", msc->min_corrected_pts);
  3239. for (i = 0; i < st->nb_index_entries; ++i) {
  3240. st->index_entries[i].timestamp -= msc->min_corrected_pts;
  3241. }
  3242. }
  3243. }
  3244. // Start time should be equal to zero or the duration of any empty edits.
  3245. st->start_time = empty_edits_sum_duration;
  3246. // Update av stream length, if it ends up shorter than the track's media duration
  3247. st->duration = FFMIN(st->duration, edit_list_dts_entry_end - start_dts);
  3248. msc->start_pad = st->skip_samples;
  3249. // Free the old index and the old CTTS structures
  3250. av_free(e_old);
  3251. av_free(ctts_data_old);
  3252. av_freep(&frame_duration_buffer);
  3253. // Null terminate the index ranges array
  3254. current_index_range++;
  3255. current_index_range->start = 0;
  3256. current_index_range->end = 0;
  3257. msc->current_index = msc->index_ranges[0].start;
  3258. }
  3259. static void mov_build_index(MOVContext *mov, AVStream *st)
  3260. {
  3261. MOVStreamContext *sc = st->priv_data;
  3262. int64_t current_offset;
  3263. int64_t current_dts = 0;
  3264. unsigned int stts_index = 0;
  3265. unsigned int stsc_index = 0;
  3266. unsigned int stss_index = 0;
  3267. unsigned int stps_index = 0;
  3268. unsigned int i, j;
  3269. uint64_t stream_size = 0;
  3270. MOVStts *ctts_data_old = sc->ctts_data;
  3271. unsigned int ctts_count_old = sc->ctts_count;
  3272. if (sc->elst_count) {
  3273. int i, edit_start_index = 0, multiple_edits = 0;
  3274. int64_t empty_duration = 0; // empty duration of the first edit list entry
  3275. int64_t start_time = 0; // start time of the media
  3276. for (i = 0; i < sc->elst_count; i++) {
  3277. const MOVElst *e = &sc->elst_data[i];
  3278. if (i == 0 && e->time == -1) {
  3279. /* if empty, the first entry is the start time of the stream
  3280. * relative to the presentation itself */
  3281. empty_duration = e->duration;
  3282. edit_start_index = 1;
  3283. } else if (i == edit_start_index && e->time >= 0) {
  3284. start_time = e->time;
  3285. } else {
  3286. multiple_edits = 1;
  3287. }
  3288. }
  3289. if (multiple_edits && !mov->advanced_editlist)
  3290. av_log(mov->fc, AV_LOG_WARNING, "multiple edit list entries, "
  3291. "Use -advanced_editlist to correctly decode otherwise "
  3292. "a/v desync might occur\n");
  3293. /* adjust first dts according to edit list */
  3294. if ((empty_duration || start_time) && mov->time_scale > 0) {
  3295. if (empty_duration)
  3296. empty_duration = av_rescale(empty_duration, sc->time_scale, mov->time_scale);
  3297. sc->time_offset = start_time - empty_duration;
  3298. sc->min_corrected_pts = start_time;
  3299. if (!mov->advanced_editlist)
  3300. current_dts = -sc->time_offset;
  3301. }
  3302. if (!multiple_edits && !mov->advanced_editlist &&
  3303. st->codecpar->codec_id == AV_CODEC_ID_AAC && start_time > 0)
  3304. sc->start_pad = start_time;
  3305. }
  3306. /* only use old uncompressed audio chunk demuxing when stts specifies it */
  3307. if (!(st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  3308. sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
  3309. unsigned int current_sample = 0;
  3310. unsigned int stts_sample = 0;
  3311. unsigned int sample_size;
  3312. unsigned int distance = 0;
  3313. unsigned int rap_group_index = 0;
  3314. unsigned int rap_group_sample = 0;
  3315. int64_t last_dts = 0;
  3316. int64_t dts_correction = 0;
  3317. int rap_group_present = sc->rap_group_count && sc->rap_group;
  3318. int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
  3319. current_dts -= sc->dts_shift;
  3320. last_dts = current_dts;
  3321. if (!sc->sample_count || st->nb_index_entries)
  3322. return;
  3323. if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
  3324. return;
  3325. if (av_reallocp_array(&st->index_entries,
  3326. st->nb_index_entries + sc->sample_count,
  3327. sizeof(*st->index_entries)) < 0) {
  3328. st->nb_index_entries = 0;
  3329. return;
  3330. }
  3331. st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries);
  3332. if (ctts_data_old) {
  3333. // Expand ctts entries such that we have a 1-1 mapping with samples
  3334. if (sc->sample_count >= UINT_MAX / sizeof(*sc->ctts_data))
  3335. return;
  3336. sc->ctts_count = 0;
  3337. sc->ctts_allocated_size = 0;
  3338. sc->ctts_data = av_fast_realloc(NULL, &sc->ctts_allocated_size,
  3339. sc->sample_count * sizeof(*sc->ctts_data));
  3340. if (!sc->ctts_data) {
  3341. av_free(ctts_data_old);
  3342. return;
  3343. }
  3344. memset((uint8_t*)(sc->ctts_data), 0, sc->ctts_allocated_size);
  3345. for (i = 0; i < ctts_count_old &&
  3346. sc->ctts_count < sc->sample_count; i++)
  3347. for (j = 0; j < ctts_data_old[i].count &&
  3348. sc->ctts_count < sc->sample_count; j++)
  3349. add_ctts_entry(&sc->ctts_data, &sc->ctts_count,
  3350. &sc->ctts_allocated_size, 1,
  3351. ctts_data_old[i].duration);
  3352. av_free(ctts_data_old);
  3353. }
  3354. for (i = 0; i < sc->chunk_count; i++) {
  3355. int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
  3356. current_offset = sc->chunk_offsets[i];
  3357. while (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
  3358. i + 1 == sc->stsc_data[stsc_index + 1].first)
  3359. stsc_index++;
  3360. if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
  3361. sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
  3362. av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
  3363. sc->stsz_sample_size = sc->sample_size;
  3364. }
  3365. if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
  3366. av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
  3367. sc->stsz_sample_size = sc->sample_size;
  3368. }
  3369. for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
  3370. int keyframe = 0;
  3371. if (current_sample >= sc->sample_count) {
  3372. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  3373. return;
  3374. }
  3375. if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
  3376. keyframe = 1;
  3377. if (stss_index + 1 < sc->keyframe_count)
  3378. stss_index++;
  3379. } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
  3380. keyframe = 1;
  3381. if (stps_index + 1 < sc->stps_count)
  3382. stps_index++;
  3383. }
  3384. if (rap_group_present && rap_group_index < sc->rap_group_count) {
  3385. if (sc->rap_group[rap_group_index].index > 0)
  3386. keyframe = 1;
  3387. if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
  3388. rap_group_sample = 0;
  3389. rap_group_index++;
  3390. }
  3391. }
  3392. if (sc->keyframe_absent
  3393. && !sc->stps_count
  3394. && !rap_group_present
  3395. && (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || (i==0 && j==0)))
  3396. keyframe = 1;
  3397. if (keyframe)
  3398. distance = 0;
  3399. sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
  3400. if (sc->pseudo_stream_id == -1 ||
  3401. sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
  3402. AVIndexEntry *e;
  3403. if (sample_size > 0x3FFFFFFF) {
  3404. av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", sample_size);
  3405. return;
  3406. }
  3407. e = &st->index_entries[st->nb_index_entries++];
  3408. e->pos = current_offset;
  3409. e->timestamp = current_dts;
  3410. e->size = sample_size;
  3411. e->min_distance = distance;
  3412. e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
  3413. av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %u, offset %"PRIx64", dts %"PRId64", "
  3414. "size %u, distance %u, keyframe %d\n", st->index, current_sample,
  3415. current_offset, current_dts, sample_size, distance, keyframe);
  3416. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->nb_index_entries < 100)
  3417. ff_rfps_add_frame(mov->fc, st, current_dts);
  3418. }
  3419. current_offset += sample_size;
  3420. stream_size += sample_size;
  3421. /* A negative sample duration is invalid based on the spec,
  3422. * but some samples need it to correct the DTS. */
  3423. if (sc->stts_data[stts_index].duration < 0) {
  3424. av_log(mov->fc, AV_LOG_WARNING,
  3425. "Invalid SampleDelta %d in STTS, at %d st:%d\n",
  3426. sc->stts_data[stts_index].duration, stts_index,
  3427. st->index);
  3428. dts_correction += sc->stts_data[stts_index].duration - 1;
  3429. sc->stts_data[stts_index].duration = 1;
  3430. }
  3431. current_dts += sc->stts_data[stts_index].duration;
  3432. if (!dts_correction || current_dts + dts_correction > last_dts) {
  3433. current_dts += dts_correction;
  3434. dts_correction = 0;
  3435. } else {
  3436. /* Avoid creating non-monotonous DTS */
  3437. dts_correction += current_dts - last_dts - 1;
  3438. current_dts = last_dts + 1;
  3439. }
  3440. last_dts = current_dts;
  3441. distance++;
  3442. stts_sample++;
  3443. current_sample++;
  3444. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  3445. stts_sample = 0;
  3446. stts_index++;
  3447. }
  3448. }
  3449. }
  3450. if (st->duration > 0)
  3451. st->codecpar->bit_rate = stream_size*8*sc->time_scale/st->duration;
  3452. } else {
  3453. unsigned chunk_samples, total = 0;
  3454. if (!sc->chunk_count)
  3455. return;
  3456. // compute total chunk count
  3457. for (i = 0; i < sc->stsc_count; i++) {
  3458. unsigned count, chunk_count;
  3459. chunk_samples = sc->stsc_data[i].count;
  3460. if (i != sc->stsc_count - 1 &&
  3461. sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
  3462. av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
  3463. return;
  3464. }
  3465. if (sc->samples_per_frame >= 160) { // gsm
  3466. count = chunk_samples / sc->samples_per_frame;
  3467. } else if (sc->samples_per_frame > 1) {
  3468. unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
  3469. count = (chunk_samples+samples-1) / samples;
  3470. } else {
  3471. count = (chunk_samples+1023) / 1024;
  3472. }
  3473. if (mov_stsc_index_valid(i, sc->stsc_count))
  3474. chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
  3475. else
  3476. chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
  3477. total += chunk_count * count;
  3478. }
  3479. av_log(mov->fc, AV_LOG_TRACE, "chunk count %u\n", total);
  3480. if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
  3481. return;
  3482. if (av_reallocp_array(&st->index_entries,
  3483. st->nb_index_entries + total,
  3484. sizeof(*st->index_entries)) < 0) {
  3485. st->nb_index_entries = 0;
  3486. return;
  3487. }
  3488. st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
  3489. // populate index
  3490. for (i = 0; i < sc->chunk_count; i++) {
  3491. current_offset = sc->chunk_offsets[i];
  3492. if (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
  3493. i + 1 == sc->stsc_data[stsc_index + 1].first)
  3494. stsc_index++;
  3495. chunk_samples = sc->stsc_data[stsc_index].count;
  3496. while (chunk_samples > 0) {
  3497. AVIndexEntry *e;
  3498. unsigned size, samples;
  3499. if (sc->samples_per_frame > 1 && !sc->bytes_per_frame) {
  3500. avpriv_request_sample(mov->fc,
  3501. "Zero bytes per frame, but %d samples per frame",
  3502. sc->samples_per_frame);
  3503. return;
  3504. }
  3505. if (sc->samples_per_frame >= 160) { // gsm
  3506. samples = sc->samples_per_frame;
  3507. size = sc->bytes_per_frame;
  3508. } else {
  3509. if (sc->samples_per_frame > 1) {
  3510. samples = FFMIN((1024 / sc->samples_per_frame)*
  3511. sc->samples_per_frame, chunk_samples);
  3512. size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
  3513. } else {
  3514. samples = FFMIN(1024, chunk_samples);
  3515. size = samples * sc->sample_size;
  3516. }
  3517. }
  3518. if (st->nb_index_entries >= total) {
  3519. av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %u\n", total);
  3520. return;
  3521. }
  3522. if (size > 0x3FFFFFFF) {
  3523. av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", size);
  3524. return;
  3525. }
  3526. e = &st->index_entries[st->nb_index_entries++];
  3527. e->pos = current_offset;
  3528. e->timestamp = current_dts;
  3529. e->size = size;
  3530. e->min_distance = 0;
  3531. e->flags = AVINDEX_KEYFRAME;
  3532. av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, chunk %u, offset %"PRIx64", dts %"PRId64", "
  3533. "size %u, duration %u\n", st->index, i, current_offset, current_dts,
  3534. size, samples);
  3535. current_offset += size;
  3536. current_dts += samples;
  3537. chunk_samples -= samples;
  3538. }
  3539. }
  3540. }
  3541. if (!mov->ignore_editlist && mov->advanced_editlist) {
  3542. // Fix index according to edit lists.
  3543. mov_fix_index(mov, st);
  3544. }
  3545. // Update start time of the stream.
  3546. if (st->start_time == AV_NOPTS_VALUE && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->nb_index_entries > 0) {
  3547. st->start_time = st->index_entries[0].timestamp + sc->dts_shift;
  3548. if (sc->ctts_data) {
  3549. st->start_time += sc->ctts_data[0].duration;
  3550. }
  3551. }
  3552. mov_estimate_video_delay(mov, st);
  3553. }
  3554. static int test_same_origin(const char *src, const char *ref) {
  3555. char src_proto[64];
  3556. char ref_proto[64];
  3557. char src_auth[256];
  3558. char ref_auth[256];
  3559. char src_host[256];
  3560. char ref_host[256];
  3561. int src_port=-1;
  3562. int ref_port=-1;
  3563. av_url_split(src_proto, sizeof(src_proto), src_auth, sizeof(src_auth), src_host, sizeof(src_host), &src_port, NULL, 0, src);
  3564. av_url_split(ref_proto, sizeof(ref_proto), ref_auth, sizeof(ref_auth), ref_host, sizeof(ref_host), &ref_port, NULL, 0, ref);
  3565. if (strlen(src) == 0) {
  3566. return -1;
  3567. } else if (strlen(src_auth) + 1 >= sizeof(src_auth) ||
  3568. strlen(ref_auth) + 1 >= sizeof(ref_auth) ||
  3569. strlen(src_host) + 1 >= sizeof(src_host) ||
  3570. strlen(ref_host) + 1 >= sizeof(ref_host)) {
  3571. return 0;
  3572. } else if (strcmp(src_proto, ref_proto) ||
  3573. strcmp(src_auth, ref_auth) ||
  3574. strcmp(src_host, ref_host) ||
  3575. src_port != ref_port) {
  3576. return 0;
  3577. } else
  3578. return 1;
  3579. }
  3580. static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref)
  3581. {
  3582. /* try relative path, we do not try the absolute because it can leak information about our
  3583. system to an attacker */
  3584. if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
  3585. char filename[1025];
  3586. const char *src_path;
  3587. int i, l;
  3588. /* find a source dir */
  3589. src_path = strrchr(src, '/');
  3590. if (src_path)
  3591. src_path++;
  3592. else
  3593. src_path = src;
  3594. /* find a next level down to target */
  3595. for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
  3596. if (ref->path[l] == '/') {
  3597. if (i == ref->nlvl_to - 1)
  3598. break;
  3599. else
  3600. i++;
  3601. }
  3602. /* compose filename if next level down to target was found */
  3603. if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
  3604. memcpy(filename, src, src_path - src);
  3605. filename[src_path - src] = 0;
  3606. for (i = 1; i < ref->nlvl_from; i++)
  3607. av_strlcat(filename, "../", sizeof(filename));
  3608. av_strlcat(filename, ref->path + l + 1, sizeof(filename));
  3609. if (!c->use_absolute_path) {
  3610. int same_origin = test_same_origin(src, filename);
  3611. if (!same_origin) {
  3612. av_log(c->fc, AV_LOG_ERROR,
  3613. "Reference with mismatching origin, %s not tried for security reasons, "
  3614. "set demuxer option use_absolute_path to allow it anyway\n",
  3615. ref->path);
  3616. return AVERROR(ENOENT);
  3617. }
  3618. if (strstr(ref->path + l + 1, "..") ||
  3619. strstr(ref->path + l + 1, ":") ||
  3620. (ref->nlvl_from > 1 && same_origin < 0) ||
  3621. (filename[0] == '/' && src_path == src))
  3622. return AVERROR(ENOENT);
  3623. }
  3624. if (strlen(filename) + 1 == sizeof(filename))
  3625. return AVERROR(ENOENT);
  3626. if (!c->fc->io_open(c->fc, pb, filename, AVIO_FLAG_READ, NULL))
  3627. return 0;
  3628. }
  3629. } else if (c->use_absolute_path) {
  3630. av_log(c->fc, AV_LOG_WARNING, "Using absolute path on user request, "
  3631. "this is a possible security issue\n");
  3632. if (!c->fc->io_open(c->fc, pb, ref->path, AVIO_FLAG_READ, NULL))
  3633. return 0;
  3634. } else {
  3635. av_log(c->fc, AV_LOG_ERROR,
  3636. "Absolute path %s not tried for security reasons, "
  3637. "set demuxer option use_absolute_path to allow absolute paths\n",
  3638. ref->path);
  3639. }
  3640. return AVERROR(ENOENT);
  3641. }
  3642. static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
  3643. {
  3644. if (sc->time_scale <= 0) {
  3645. av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
  3646. sc->time_scale = c->time_scale;
  3647. if (sc->time_scale <= 0)
  3648. sc->time_scale = 1;
  3649. }
  3650. }
  3651. static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  3652. {
  3653. AVStream *st;
  3654. MOVStreamContext *sc;
  3655. int ret;
  3656. st = avformat_new_stream(c->fc, NULL);
  3657. if (!st) return AVERROR(ENOMEM);
  3658. st->id = -1;
  3659. sc = av_mallocz(sizeof(MOVStreamContext));
  3660. if (!sc) return AVERROR(ENOMEM);
  3661. st->priv_data = sc;
  3662. st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
  3663. sc->ffindex = st->index;
  3664. c->trak_index = st->index;
  3665. if ((ret = mov_read_default(c, pb, atom)) < 0)
  3666. return ret;
  3667. c->trak_index = -1;
  3668. // Here stsc refers to a chunk not described in stco. This is technically invalid,
  3669. // but we can overlook it (clearing stsc) whenever stts_count == 0 (indicating no samples).
  3670. if (!sc->chunk_count && !sc->stts_count && sc->stsc_count) {
  3671. sc->stsc_count = 0;
  3672. av_freep(&sc->stsc_data);
  3673. }
  3674. /* sanity checks */
  3675. if ((sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
  3676. (!sc->sample_size && !sc->sample_count))) ||
  3677. (!sc->chunk_count && sc->sample_count)) {
  3678. av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
  3679. st->index);
  3680. return 0;
  3681. }
  3682. if (sc->stsc_count && sc->stsc_data[ sc->stsc_count - 1 ].first > sc->chunk_count) {
  3683. av_log(c->fc, AV_LOG_ERROR, "stream %d, contradictionary STSC and STCO\n",
  3684. st->index);
  3685. return AVERROR_INVALIDDATA;
  3686. }
  3687. fix_timescale(c, sc);
  3688. avpriv_set_pts_info(st, 64, 1, sc->time_scale);
  3689. mov_build_index(c, st);
  3690. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  3691. MOVDref *dref = &sc->drefs[sc->dref_id - 1];
  3692. if (c->enable_drefs) {
  3693. if (mov_open_dref(c, &sc->pb, c->fc->url, dref) < 0)
  3694. av_log(c->fc, AV_LOG_ERROR,
  3695. "stream %d, error opening alias: path='%s', dir='%s', "
  3696. "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
  3697. st->index, dref->path, dref->dir, dref->filename,
  3698. dref->volume, dref->nlvl_from, dref->nlvl_to);
  3699. } else {
  3700. av_log(c->fc, AV_LOG_WARNING,
  3701. "Skipped opening external track: "
  3702. "stream %d, alias: path='%s', dir='%s', "
  3703. "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d."
  3704. "Set enable_drefs to allow this.\n",
  3705. st->index, dref->path, dref->dir, dref->filename,
  3706. dref->volume, dref->nlvl_from, dref->nlvl_to);
  3707. }
  3708. } else {
  3709. sc->pb = c->fc->pb;
  3710. sc->pb_is_copied = 1;
  3711. }
  3712. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  3713. if (!st->sample_aspect_ratio.num && st->codecpar->width && st->codecpar->height &&
  3714. sc->height && sc->width &&
  3715. (st->codecpar->width != sc->width || st->codecpar->height != sc->height)) {
  3716. st->sample_aspect_ratio = av_d2q(((double)st->codecpar->height * sc->width) /
  3717. ((double)st->codecpar->width * sc->height), INT_MAX);
  3718. }
  3719. #if FF_API_R_FRAME_RATE
  3720. if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
  3721. av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
  3722. sc->time_scale, sc->stts_data[0].duration, INT_MAX);
  3723. #endif
  3724. }
  3725. // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
  3726. if (!st->codecpar->extradata_size && st->codecpar->codec_id == AV_CODEC_ID_H264 &&
  3727. TAG_IS_AVCI(st->codecpar->codec_tag)) {
  3728. ret = ff_generate_avci_extradata(st);
  3729. if (ret < 0)
  3730. return ret;
  3731. }
  3732. switch (st->codecpar->codec_id) {
  3733. #if CONFIG_H261_DECODER
  3734. case AV_CODEC_ID_H261:
  3735. #endif
  3736. #if CONFIG_H263_DECODER
  3737. case AV_CODEC_ID_H263:
  3738. #endif
  3739. #if CONFIG_MPEG4_DECODER
  3740. case AV_CODEC_ID_MPEG4:
  3741. #endif
  3742. st->codecpar->width = 0; /* let decoder init width/height */
  3743. st->codecpar->height= 0;
  3744. break;
  3745. }
  3746. // If the duration of the mp3 packets is not constant, then they could need a parser
  3747. if (st->codecpar->codec_id == AV_CODEC_ID_MP3
  3748. && sc->stts_count > 3
  3749. && sc->stts_count*10 > st->nb_frames
  3750. && sc->time_scale == st->codecpar->sample_rate) {
  3751. st->need_parsing = AVSTREAM_PARSE_FULL;
  3752. }
  3753. /* Do not need those anymore. */
  3754. av_freep(&sc->chunk_offsets);
  3755. av_freep(&sc->sample_sizes);
  3756. av_freep(&sc->keyframes);
  3757. av_freep(&sc->stts_data);
  3758. av_freep(&sc->stps_data);
  3759. av_freep(&sc->elst_data);
  3760. av_freep(&sc->rap_group);
  3761. return 0;
  3762. }
  3763. static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  3764. {
  3765. int ret;
  3766. c->itunes_metadata = 1;
  3767. ret = mov_read_default(c, pb, atom);
  3768. c->itunes_metadata = 0;
  3769. return ret;
  3770. }
  3771. static int mov_read_keys(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  3772. {
  3773. uint32_t count;
  3774. uint32_t i;
  3775. if (atom.size < 8)
  3776. return 0;
  3777. avio_skip(pb, 4);
  3778. count = avio_rb32(pb);
  3779. if (count > UINT_MAX / sizeof(*c->meta_keys) - 1) {
  3780. av_log(c->fc, AV_LOG_ERROR,
  3781. "The 'keys' atom with the invalid key count: %"PRIu32"\n", count);
  3782. return AVERROR_INVALIDDATA;
  3783. }
  3784. c->meta_keys_count = count + 1;
  3785. c->meta_keys = av_mallocz(c->meta_keys_count * sizeof(*c->meta_keys));
  3786. if (!c->meta_keys)
  3787. return AVERROR(ENOMEM);
  3788. for (i = 1; i <= count; ++i) {
  3789. uint32_t key_size = avio_rb32(pb);
  3790. uint32_t type = avio_rl32(pb);
  3791. if (key_size < 8) {
  3792. av_log(c->fc, AV_LOG_ERROR,
  3793. "The key# %"PRIu32" in meta has invalid size:"
  3794. "%"PRIu32"\n", i, key_size);
  3795. return AVERROR_INVALIDDATA;
  3796. }
  3797. key_size -= 8;
  3798. if (type != MKTAG('m','d','t','a')) {
  3799. avio_skip(pb, key_size);
  3800. }
  3801. c->meta_keys[i] = av_mallocz(key_size + 1);
  3802. if (!c->meta_keys[i])
  3803. return AVERROR(ENOMEM);
  3804. avio_read(pb, c->meta_keys[i], key_size);
  3805. }
  3806. return 0;
  3807. }
  3808. static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  3809. {
  3810. int64_t end = avio_tell(pb) + atom.size;
  3811. uint8_t *key = NULL, *val = NULL, *mean = NULL;
  3812. int i;
  3813. int ret = 0;
  3814. AVStream *st;
  3815. MOVStreamContext *sc;
  3816. if (c->fc->nb_streams < 1)
  3817. return 0;
  3818. st = c->fc->streams[c->fc->nb_streams-1];
  3819. sc = st->priv_data;
  3820. for (i = 0; i < 3; i++) {
  3821. uint8_t **p;
  3822. uint32_t len, tag;
  3823. if (end - avio_tell(pb) <= 12)
  3824. break;
  3825. len = avio_rb32(pb);
  3826. tag = avio_rl32(pb);
  3827. avio_skip(pb, 4); // flags
  3828. if (len < 12 || len - 12 > end - avio_tell(pb))
  3829. break;
  3830. len -= 12;
  3831. if (tag == MKTAG('m', 'e', 'a', 'n'))
  3832. p = &mean;
  3833. else if (tag == MKTAG('n', 'a', 'm', 'e'))
  3834. p = &key;
  3835. else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
  3836. avio_skip(pb, 4);
  3837. len -= 4;
  3838. p = &val;
  3839. } else
  3840. break;
  3841. if (*p)
  3842. break;
  3843. *p = av_malloc(len + 1);
  3844. if (!*p) {
  3845. ret = AVERROR(ENOMEM);
  3846. break;
  3847. }
  3848. ret = ffio_read_size(pb, *p, len);
  3849. if (ret < 0) {
  3850. av_freep(p);
  3851. break;
  3852. }
  3853. (*p)[len] = 0;
  3854. }
  3855. if (mean && key && val) {
  3856. if (strcmp(key, "iTunSMPB") == 0) {
  3857. int priming, remainder, samples;
  3858. if(sscanf(val, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
  3859. if(priming>0 && priming<16384)
  3860. sc->start_pad = priming;
  3861. }
  3862. }
  3863. if (strcmp(key, "cdec") != 0) {
  3864. av_dict_set(&c->fc->metadata, key, val,
  3865. AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
  3866. key = val = NULL;
  3867. }
  3868. } else {
  3869. av_log(c->fc, AV_LOG_VERBOSE,
  3870. "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
  3871. }
  3872. avio_seek(pb, end, SEEK_SET);
  3873. av_freep(&key);
  3874. av_freep(&val);
  3875. av_freep(&mean);
  3876. return ret;
  3877. }
  3878. static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  3879. {
  3880. while (atom.size > 8) {
  3881. uint32_t tag;
  3882. if (avio_feof(pb))
  3883. return AVERROR_EOF;
  3884. tag = avio_rl32(pb);
  3885. atom.size -= 4;
  3886. if (tag == MKTAG('h','d','l','r')) {
  3887. avio_seek(pb, -8, SEEK_CUR);
  3888. atom.size += 8;
  3889. return mov_read_default(c, pb, atom);
  3890. }
  3891. }
  3892. return 0;
  3893. }
  3894. // return 1 when matrix is identity, 0 otherwise
  3895. #define IS_MATRIX_IDENT(matrix) \
  3896. ( (matrix)[0][0] == (1 << 16) && \
  3897. (matrix)[1][1] == (1 << 16) && \
  3898. (matrix)[2][2] == (1 << 30) && \
  3899. !(matrix)[0][1] && !(matrix)[0][2] && \
  3900. !(matrix)[1][0] && !(matrix)[1][2] && \
  3901. !(matrix)[2][0] && !(matrix)[2][1])
  3902. static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  3903. {
  3904. int i, j, e;
  3905. int width;
  3906. int height;
  3907. int display_matrix[3][3];
  3908. int res_display_matrix[3][3] = { { 0 } };
  3909. AVStream *st;
  3910. MOVStreamContext *sc;
  3911. int version;
  3912. int flags;
  3913. if (c->fc->nb_streams < 1)
  3914. return 0;
  3915. st = c->fc->streams[c->fc->nb_streams-1];
  3916. sc = st->priv_data;
  3917. // Each stream (trak) should have exactly 1 tkhd. This catches bad files and
  3918. // avoids corrupting AVStreams mapped to an earlier tkhd.
  3919. if (st->id != -1)
  3920. return AVERROR_INVALIDDATA;
  3921. version = avio_r8(pb);
  3922. flags = avio_rb24(pb);
  3923. st->disposition |= (flags & MOV_TKHD_FLAG_ENABLED) ? AV_DISPOSITION_DEFAULT : 0;
  3924. if (version == 1) {
  3925. avio_rb64(pb);
  3926. avio_rb64(pb);
  3927. } else {
  3928. avio_rb32(pb); /* creation time */
  3929. avio_rb32(pb); /* modification time */
  3930. }
  3931. st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
  3932. avio_rb32(pb); /* reserved */
  3933. /* highlevel (considering edits) duration in movie timebase */
  3934. (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
  3935. avio_rb32(pb); /* reserved */
  3936. avio_rb32(pb); /* reserved */
  3937. avio_rb16(pb); /* layer */
  3938. avio_rb16(pb); /* alternate group */
  3939. avio_rb16(pb); /* volume */
  3940. avio_rb16(pb); /* reserved */
  3941. //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
  3942. // they're kept in fixed point format through all calculations
  3943. // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
  3944. // side data, but the scale factor is not needed to calculate aspect ratio
  3945. for (i = 0; i < 3; i++) {
  3946. display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
  3947. display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
  3948. display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
  3949. }
  3950. width = avio_rb32(pb); // 16.16 fixed point track width
  3951. height = avio_rb32(pb); // 16.16 fixed point track height
  3952. sc->width = width >> 16;
  3953. sc->height = height >> 16;
  3954. // apply the moov display matrix (after the tkhd one)
  3955. for (i = 0; i < 3; i++) {
  3956. const int sh[3] = { 16, 16, 30 };
  3957. for (j = 0; j < 3; j++) {
  3958. for (e = 0; e < 3; e++) {
  3959. res_display_matrix[i][j] +=
  3960. ((int64_t) display_matrix[i][e] *
  3961. c->movie_display_matrix[e][j]) >> sh[e];
  3962. }
  3963. }
  3964. }
  3965. // save the matrix when it is not the default identity
  3966. if (!IS_MATRIX_IDENT(res_display_matrix)) {
  3967. double rotate;
  3968. av_freep(&sc->display_matrix);
  3969. sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
  3970. if (!sc->display_matrix)
  3971. return AVERROR(ENOMEM);
  3972. for (i = 0; i < 3; i++)
  3973. for (j = 0; j < 3; j++)
  3974. sc->display_matrix[i * 3 + j] = res_display_matrix[i][j];
  3975. #if FF_API_OLD_ROTATE_API
  3976. rotate = av_display_rotation_get(sc->display_matrix);
  3977. if (!isnan(rotate)) {
  3978. char rotate_buf[64];
  3979. rotate = -rotate;
  3980. if (rotate < 0) // for backward compatibility
  3981. rotate += 360;
  3982. snprintf(rotate_buf, sizeof(rotate_buf), "%g", rotate);
  3983. av_dict_set(&st->metadata, "rotate", rotate_buf, 0);
  3984. }
  3985. #endif
  3986. }
  3987. // transform the display width/height according to the matrix
  3988. // to keep the same scale, use [width height 1<<16]
  3989. if (width && height && sc->display_matrix) {
  3990. double disp_transform[2];
  3991. for (i = 0; i < 2; i++)
  3992. disp_transform[i] = hypot(sc->display_matrix[0 + i],
  3993. sc->display_matrix[3 + i]);
  3994. if (disp_transform[0] > 0 && disp_transform[1] > 0 &&
  3995. disp_transform[0] < (1<<24) && disp_transform[1] < (1<<24) &&
  3996. fabs((disp_transform[0] / disp_transform[1]) - 1.0) > 0.01)
  3997. st->sample_aspect_ratio = av_d2q(
  3998. disp_transform[0] / disp_transform[1],
  3999. INT_MAX);
  4000. }
  4001. return 0;
  4002. }
  4003. static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4004. {
  4005. MOVFragment *frag = &c->fragment;
  4006. MOVTrackExt *trex = NULL;
  4007. int flags, track_id, i;
  4008. MOVFragmentStreamInfo * frag_stream_info;
  4009. avio_r8(pb); /* version */
  4010. flags = avio_rb24(pb);
  4011. track_id = avio_rb32(pb);
  4012. if (!track_id)
  4013. return AVERROR_INVALIDDATA;
  4014. for (i = 0; i < c->trex_count; i++)
  4015. if (c->trex_data[i].track_id == track_id) {
  4016. trex = &c->trex_data[i];
  4017. break;
  4018. }
  4019. if (!trex) {
  4020. av_log(c->fc, AV_LOG_WARNING, "could not find corresponding trex (id %u)\n", track_id);
  4021. return 0;
  4022. }
  4023. c->fragment.found_tfhd = 1;
  4024. frag->track_id = track_id;
  4025. set_frag_stream(&c->frag_index, track_id);
  4026. frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ?
  4027. avio_rb64(pb) : flags & MOV_TFHD_DEFAULT_BASE_IS_MOOF ?
  4028. frag->moof_offset : frag->implicit_offset;
  4029. frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
  4030. frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
  4031. avio_rb32(pb) : trex->duration;
  4032. frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
  4033. avio_rb32(pb) : trex->size;
  4034. frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
  4035. avio_rb32(pb) : trex->flags;
  4036. av_log(c->fc, AV_LOG_TRACE, "frag flags 0x%x\n", frag->flags);
  4037. frag_stream_info = get_current_frag_stream_info(&c->frag_index);
  4038. if (frag_stream_info)
  4039. frag_stream_info->next_trun_dts = AV_NOPTS_VALUE;
  4040. return 0;
  4041. }
  4042. static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4043. {
  4044. unsigned i, num;
  4045. void *new_tracks;
  4046. num = atom.size / 4;
  4047. if (!(new_tracks = av_malloc_array(num, sizeof(int))))
  4048. return AVERROR(ENOMEM);
  4049. av_free(c->chapter_tracks);
  4050. c->chapter_tracks = new_tracks;
  4051. c->nb_chapter_tracks = num;
  4052. for (i = 0; i < num && !pb->eof_reached; i++)
  4053. c->chapter_tracks[i] = avio_rb32(pb);
  4054. return 0;
  4055. }
  4056. static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4057. {
  4058. MOVTrackExt *trex;
  4059. int err;
  4060. if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
  4061. return AVERROR_INVALIDDATA;
  4062. if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
  4063. sizeof(*c->trex_data))) < 0) {
  4064. c->trex_count = 0;
  4065. return err;
  4066. }
  4067. c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
  4068. trex = &c->trex_data[c->trex_count++];
  4069. avio_r8(pb); /* version */
  4070. avio_rb24(pb); /* flags */
  4071. trex->track_id = avio_rb32(pb);
  4072. trex->stsd_id = avio_rb32(pb);
  4073. trex->duration = avio_rb32(pb);
  4074. trex->size = avio_rb32(pb);
  4075. trex->flags = avio_rb32(pb);
  4076. return 0;
  4077. }
  4078. static int mov_read_tfdt(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4079. {
  4080. MOVFragment *frag = &c->fragment;
  4081. AVStream *st = NULL;
  4082. MOVStreamContext *sc;
  4083. int version, i;
  4084. MOVFragmentStreamInfo * frag_stream_info;
  4085. int64_t base_media_decode_time;
  4086. for (i = 0; i < c->fc->nb_streams; i++) {
  4087. if (c->fc->streams[i]->id == frag->track_id) {
  4088. st = c->fc->streams[i];
  4089. break;
  4090. }
  4091. }
  4092. if (!st) {
  4093. av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %u\n", frag->track_id);
  4094. return 0;
  4095. }
  4096. sc = st->priv_data;
  4097. if (sc->pseudo_stream_id + 1 != frag->stsd_id && sc->pseudo_stream_id != -1)
  4098. return 0;
  4099. version = avio_r8(pb);
  4100. avio_rb24(pb); /* flags */
  4101. if (version) {
  4102. base_media_decode_time = avio_rb64(pb);
  4103. } else {
  4104. base_media_decode_time = avio_rb32(pb);
  4105. }
  4106. frag_stream_info = get_current_frag_stream_info(&c->frag_index);
  4107. if (frag_stream_info)
  4108. frag_stream_info->tfdt_dts = base_media_decode_time;
  4109. sc->track_end = base_media_decode_time;
  4110. return 0;
  4111. }
  4112. static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4113. {
  4114. MOVFragment *frag = &c->fragment;
  4115. AVStream *st = NULL;
  4116. MOVStreamContext *sc;
  4117. MOVStts *ctts_data;
  4118. uint64_t offset;
  4119. int64_t dts, pts = AV_NOPTS_VALUE;
  4120. int data_offset = 0;
  4121. unsigned entries, first_sample_flags = frag->flags;
  4122. int flags, distance, i;
  4123. int64_t prev_dts = AV_NOPTS_VALUE;
  4124. int next_frag_index = -1, index_entry_pos;
  4125. size_t requested_size;
  4126. size_t old_ctts_allocated_size;
  4127. AVIndexEntry *new_entries;
  4128. MOVFragmentStreamInfo * frag_stream_info;
  4129. if (!frag->found_tfhd) {
  4130. av_log(c->fc, AV_LOG_ERROR, "trun track id unknown, no tfhd was found\n");
  4131. return AVERROR_INVALIDDATA;
  4132. }
  4133. for (i = 0; i < c->fc->nb_streams; i++) {
  4134. if (c->fc->streams[i]->id == frag->track_id) {
  4135. st = c->fc->streams[i];
  4136. break;
  4137. }
  4138. }
  4139. if (!st) {
  4140. av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %u\n", frag->track_id);
  4141. return 0;
  4142. }
  4143. sc = st->priv_data;
  4144. if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
  4145. return 0;
  4146. // Find the next frag_index index that has a valid index_entry for
  4147. // the current track_id.
  4148. //
  4149. // A valid index_entry means the trun for the fragment was read
  4150. // and it's samples are in index_entries at the given position.
  4151. // New index entries will be inserted before the index_entry found.
  4152. index_entry_pos = st->nb_index_entries;
  4153. for (i = c->frag_index.current + 1; i < c->frag_index.nb_items; i++) {
  4154. frag_stream_info = get_frag_stream_info(&c->frag_index, i, frag->track_id);
  4155. if (frag_stream_info && frag_stream_info->index_entry >= 0) {
  4156. next_frag_index = i;
  4157. index_entry_pos = frag_stream_info->index_entry;
  4158. break;
  4159. }
  4160. }
  4161. av_assert0(index_entry_pos <= st->nb_index_entries);
  4162. avio_r8(pb); /* version */
  4163. flags = avio_rb24(pb);
  4164. entries = avio_rb32(pb);
  4165. av_log(c->fc, AV_LOG_TRACE, "flags 0x%x entries %u\n", flags, entries);
  4166. if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
  4167. return AVERROR_INVALIDDATA;
  4168. if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
  4169. if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
  4170. frag_stream_info = get_current_frag_stream_info(&c->frag_index);
  4171. if (frag_stream_info) {
  4172. if (frag_stream_info->next_trun_dts != AV_NOPTS_VALUE) {
  4173. dts = frag_stream_info->next_trun_dts - sc->time_offset;
  4174. } else if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE &&
  4175. c->use_mfra_for == FF_MOV_FLAG_MFRA_PTS) {
  4176. pts = frag_stream_info->first_tfra_pts;
  4177. av_log(c->fc, AV_LOG_DEBUG, "found mfra time %"PRId64
  4178. ", using it for pts\n", pts);
  4179. } else if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE &&
  4180. c->use_mfra_for == FF_MOV_FLAG_MFRA_DTS) {
  4181. dts = frag_stream_info->first_tfra_pts;
  4182. av_log(c->fc, AV_LOG_DEBUG, "found mfra time %"PRId64
  4183. ", using it for dts\n", pts);
  4184. } else if (frag_stream_info->sidx_pts != AV_NOPTS_VALUE) {
  4185. // FIXME: sidx earliest_presentation_time is *PTS*, s.b.
  4186. // pts = frag_stream_info->sidx_pts;
  4187. dts = frag_stream_info->sidx_pts - sc->time_offset;
  4188. av_log(c->fc, AV_LOG_DEBUG, "found sidx time %"PRId64
  4189. ", using it for pts\n", pts);
  4190. } else if (frag_stream_info->tfdt_dts != AV_NOPTS_VALUE) {
  4191. dts = frag_stream_info->tfdt_dts - sc->time_offset;
  4192. av_log(c->fc, AV_LOG_DEBUG, "found tfdt time %"PRId64
  4193. ", using it for dts\n", dts);
  4194. } else {
  4195. dts = sc->track_end - sc->time_offset;
  4196. av_log(c->fc, AV_LOG_DEBUG, "found track end time %"PRId64
  4197. ", using it for dts\n", dts);
  4198. }
  4199. } else {
  4200. dts = sc->track_end - sc->time_offset;
  4201. av_log(c->fc, AV_LOG_DEBUG, "found track end time %"PRId64
  4202. ", using it for dts\n", dts);
  4203. }
  4204. offset = frag->base_data_offset + data_offset;
  4205. distance = 0;
  4206. av_log(c->fc, AV_LOG_TRACE, "first sample flags 0x%x\n", first_sample_flags);
  4207. // realloc space for new index entries
  4208. if((uint64_t)st->nb_index_entries + entries >= UINT_MAX / sizeof(AVIndexEntry)) {
  4209. entries = UINT_MAX / sizeof(AVIndexEntry) - st->nb_index_entries;
  4210. av_log(c->fc, AV_LOG_ERROR, "Failed to add index entry\n");
  4211. }
  4212. if (entries == 0)
  4213. return 0;
  4214. requested_size = (st->nb_index_entries + entries) * sizeof(AVIndexEntry);
  4215. new_entries = av_fast_realloc(st->index_entries,
  4216. &st->index_entries_allocated_size,
  4217. requested_size);
  4218. if (!new_entries)
  4219. return AVERROR(ENOMEM);
  4220. st->index_entries= new_entries;
  4221. requested_size = (st->nb_index_entries + entries) * sizeof(*sc->ctts_data);
  4222. old_ctts_allocated_size = sc->ctts_allocated_size;
  4223. ctts_data = av_fast_realloc(sc->ctts_data, &sc->ctts_allocated_size,
  4224. requested_size);
  4225. if (!ctts_data)
  4226. return AVERROR(ENOMEM);
  4227. sc->ctts_data = ctts_data;
  4228. // In case there were samples without ctts entries, ensure they get
  4229. // zero valued entries. This ensures clips which mix boxes with and
  4230. // without ctts entries don't pickup uninitialized data.
  4231. memset((uint8_t*)(sc->ctts_data) + old_ctts_allocated_size, 0,
  4232. sc->ctts_allocated_size - old_ctts_allocated_size);
  4233. if (index_entry_pos < st->nb_index_entries) {
  4234. // Make hole in index_entries and ctts_data for new samples
  4235. memmove(st->index_entries + index_entry_pos + entries,
  4236. st->index_entries + index_entry_pos,
  4237. sizeof(*st->index_entries) *
  4238. (st->nb_index_entries - index_entry_pos));
  4239. memmove(sc->ctts_data + index_entry_pos + entries,
  4240. sc->ctts_data + index_entry_pos,
  4241. sizeof(*sc->ctts_data) * (sc->ctts_count - index_entry_pos));
  4242. if (index_entry_pos < sc->current_sample) {
  4243. sc->current_sample += entries;
  4244. }
  4245. }
  4246. st->nb_index_entries += entries;
  4247. sc->ctts_count = st->nb_index_entries;
  4248. // Record the index_entry position in frag_index of this fragment
  4249. if (frag_stream_info)
  4250. frag_stream_info->index_entry = index_entry_pos;
  4251. if (index_entry_pos > 0)
  4252. prev_dts = st->index_entries[index_entry_pos-1].timestamp;
  4253. for (i = 0; i < entries && !pb->eof_reached; i++) {
  4254. unsigned sample_size = frag->size;
  4255. int sample_flags = i ? frag->flags : first_sample_flags;
  4256. unsigned sample_duration = frag->duration;
  4257. unsigned ctts_duration = 0;
  4258. int keyframe = 0;
  4259. int index_entry_flags = 0;
  4260. if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
  4261. if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
  4262. if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
  4263. if (flags & MOV_TRUN_SAMPLE_CTS) ctts_duration = avio_rb32(pb);
  4264. mov_update_dts_shift(sc, ctts_duration, c->fc);
  4265. if (pts != AV_NOPTS_VALUE) {
  4266. dts = pts - sc->dts_shift;
  4267. if (flags & MOV_TRUN_SAMPLE_CTS) {
  4268. dts -= ctts_duration;
  4269. } else {
  4270. dts -= sc->time_offset;
  4271. }
  4272. av_log(c->fc, AV_LOG_DEBUG,
  4273. "pts %"PRId64" calculated dts %"PRId64
  4274. " sc->dts_shift %d ctts.duration %d"
  4275. " sc->time_offset %"PRId64
  4276. " flags & MOV_TRUN_SAMPLE_CTS %d\n",
  4277. pts, dts,
  4278. sc->dts_shift, ctts_duration,
  4279. sc->time_offset, flags & MOV_TRUN_SAMPLE_CTS);
  4280. pts = AV_NOPTS_VALUE;
  4281. }
  4282. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
  4283. keyframe = 1;
  4284. else
  4285. keyframe =
  4286. !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
  4287. MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES));
  4288. if (keyframe) {
  4289. distance = 0;
  4290. index_entry_flags |= AVINDEX_KEYFRAME;
  4291. }
  4292. // Fragments can overlap in time. Discard overlapping frames after
  4293. // decoding.
  4294. if (prev_dts >= dts)
  4295. index_entry_flags |= AVINDEX_DISCARD_FRAME;
  4296. st->index_entries[index_entry_pos].pos = offset;
  4297. st->index_entries[index_entry_pos].timestamp = dts;
  4298. st->index_entries[index_entry_pos].size= sample_size;
  4299. st->index_entries[index_entry_pos].min_distance= distance;
  4300. st->index_entries[index_entry_pos].flags = index_entry_flags;
  4301. sc->ctts_data[index_entry_pos].count = 1;
  4302. sc->ctts_data[index_entry_pos].duration = ctts_duration;
  4303. index_entry_pos++;
  4304. av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  4305. "size %u, distance %d, keyframe %d\n", st->index,
  4306. index_entry_pos, offset, dts, sample_size, distance, keyframe);
  4307. distance++;
  4308. dts += sample_duration;
  4309. offset += sample_size;
  4310. sc->data_size += sample_size;
  4311. if (sample_duration <= INT64_MAX - sc->duration_for_fps &&
  4312. 1 <= INT_MAX - sc->nb_frames_for_fps
  4313. ) {
  4314. sc->duration_for_fps += sample_duration;
  4315. sc->nb_frames_for_fps ++;
  4316. }
  4317. }
  4318. if (frag_stream_info)
  4319. frag_stream_info->next_trun_dts = dts + sc->time_offset;
  4320. if (i < entries) {
  4321. // EOF found before reading all entries. Fix the hole this would
  4322. // leave in index_entries and ctts_data
  4323. int gap = entries - i;
  4324. memmove(st->index_entries + index_entry_pos,
  4325. st->index_entries + index_entry_pos + gap,
  4326. sizeof(*st->index_entries) *
  4327. (st->nb_index_entries - (index_entry_pos + gap)));
  4328. memmove(sc->ctts_data + index_entry_pos,
  4329. sc->ctts_data + index_entry_pos + gap,
  4330. sizeof(*sc->ctts_data) *
  4331. (sc->ctts_count - (index_entry_pos + gap)));
  4332. st->nb_index_entries -= gap;
  4333. sc->ctts_count -= gap;
  4334. if (index_entry_pos < sc->current_sample) {
  4335. sc->current_sample -= gap;
  4336. }
  4337. entries = i;
  4338. }
  4339. // The end of this new fragment may overlap in time with the start
  4340. // of the next fragment in index_entries. Mark the samples in the next
  4341. // fragment that overlap with AVINDEX_DISCARD_FRAME
  4342. prev_dts = AV_NOPTS_VALUE;
  4343. if (index_entry_pos > 0)
  4344. prev_dts = st->index_entries[index_entry_pos-1].timestamp;
  4345. for (i = index_entry_pos; i < st->nb_index_entries; i++) {
  4346. if (prev_dts < st->index_entries[i].timestamp)
  4347. break;
  4348. st->index_entries[i].flags |= AVINDEX_DISCARD_FRAME;
  4349. }
  4350. // If a hole was created to insert the new index_entries into,
  4351. // the index_entry recorded for all subsequent moof must
  4352. // be incremented by the number of entries inserted.
  4353. fix_frag_index_entries(&c->frag_index, next_frag_index,
  4354. frag->track_id, entries);
  4355. if (pb->eof_reached) {
  4356. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted TRUN atom\n");
  4357. return AVERROR_EOF;
  4358. }
  4359. frag->implicit_offset = offset;
  4360. sc->track_end = dts + sc->time_offset;
  4361. if (st->duration < sc->track_end)
  4362. st->duration = sc->track_end;
  4363. return 0;
  4364. }
  4365. static int mov_read_sidx(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4366. {
  4367. int64_t offset = avio_tell(pb) + atom.size, pts, timestamp;
  4368. uint8_t version;
  4369. unsigned i, j, track_id, item_count;
  4370. AVStream *st = NULL;
  4371. AVStream *ref_st = NULL;
  4372. MOVStreamContext *sc, *ref_sc = NULL;
  4373. AVRational timescale;
  4374. version = avio_r8(pb);
  4375. if (version > 1) {
  4376. avpriv_request_sample(c->fc, "sidx version %u", version);
  4377. return 0;
  4378. }
  4379. avio_rb24(pb); // flags
  4380. track_id = avio_rb32(pb); // Reference ID
  4381. for (i = 0; i < c->fc->nb_streams; i++) {
  4382. if (c->fc->streams[i]->id == track_id) {
  4383. st = c->fc->streams[i];
  4384. break;
  4385. }
  4386. }
  4387. if (!st) {
  4388. av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %d\n", track_id);
  4389. return 0;
  4390. }
  4391. sc = st->priv_data;
  4392. timescale = av_make_q(1, avio_rb32(pb));
  4393. if (timescale.den <= 0) {
  4394. av_log(c->fc, AV_LOG_ERROR, "Invalid sidx timescale 1/%d\n", timescale.den);
  4395. return AVERROR_INVALIDDATA;
  4396. }
  4397. if (version == 0) {
  4398. pts = avio_rb32(pb);
  4399. offset += avio_rb32(pb);
  4400. } else {
  4401. pts = avio_rb64(pb);
  4402. offset += avio_rb64(pb);
  4403. }
  4404. avio_rb16(pb); // reserved
  4405. item_count = avio_rb16(pb);
  4406. for (i = 0; i < item_count; i++) {
  4407. int index;
  4408. MOVFragmentStreamInfo * frag_stream_info;
  4409. uint32_t size = avio_rb32(pb);
  4410. uint32_t duration = avio_rb32(pb);
  4411. if (size & 0x80000000) {
  4412. avpriv_request_sample(c->fc, "sidx reference_type 1");
  4413. return AVERROR_PATCHWELCOME;
  4414. }
  4415. avio_rb32(pb); // sap_flags
  4416. timestamp = av_rescale_q(pts, timescale, st->time_base);
  4417. index = update_frag_index(c, offset);
  4418. frag_stream_info = get_frag_stream_info(&c->frag_index, index, track_id);
  4419. if (frag_stream_info)
  4420. frag_stream_info->sidx_pts = timestamp;
  4421. offset += size;
  4422. pts += duration;
  4423. }
  4424. st->duration = sc->track_end = pts;
  4425. sc->has_sidx = 1;
  4426. if (offset == avio_size(pb)) {
  4427. // Find first entry in fragment index that came from an sidx.
  4428. // This will pretty much always be the first entry.
  4429. for (i = 0; i < c->frag_index.nb_items; i++) {
  4430. MOVFragmentIndexItem * item = &c->frag_index.item[i];
  4431. for (j = 0; ref_st == NULL && j < item->nb_stream_info; j++) {
  4432. MOVFragmentStreamInfo * si;
  4433. si = &item->stream_info[j];
  4434. if (si->sidx_pts != AV_NOPTS_VALUE) {
  4435. ref_st = c->fc->streams[j];
  4436. ref_sc = ref_st->priv_data;
  4437. break;
  4438. }
  4439. }
  4440. }
  4441. if (ref_st) for (i = 0; i < c->fc->nb_streams; i++) {
  4442. st = c->fc->streams[i];
  4443. sc = st->priv_data;
  4444. if (!sc->has_sidx) {
  4445. st->duration = sc->track_end = av_rescale(ref_st->duration, sc->time_scale, ref_sc->time_scale);
  4446. }
  4447. }
  4448. c->frag_index.complete = 1;
  4449. }
  4450. return 0;
  4451. }
  4452. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  4453. /* like the files created with Adobe Premiere 5.0, for samples see */
  4454. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  4455. static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4456. {
  4457. int err;
  4458. if (atom.size < 8)
  4459. return 0; /* continue */
  4460. if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  4461. avio_skip(pb, atom.size - 4);
  4462. return 0;
  4463. }
  4464. atom.type = avio_rl32(pb);
  4465. atom.size -= 8;
  4466. if (atom.type != MKTAG('m','d','a','t')) {
  4467. avio_skip(pb, atom.size);
  4468. return 0;
  4469. }
  4470. err = mov_read_mdat(c, pb, atom);
  4471. return err;
  4472. }
  4473. static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4474. {
  4475. #if CONFIG_ZLIB
  4476. AVIOContext ctx;
  4477. uint8_t *cmov_data;
  4478. uint8_t *moov_data; /* uncompressed data */
  4479. long cmov_len, moov_len;
  4480. int ret = -1;
  4481. avio_rb32(pb); /* dcom atom */
  4482. if (avio_rl32(pb) != MKTAG('d','c','o','m'))
  4483. return AVERROR_INVALIDDATA;
  4484. if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
  4485. av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
  4486. return AVERROR_INVALIDDATA;
  4487. }
  4488. avio_rb32(pb); /* cmvd atom */
  4489. if (avio_rl32(pb) != MKTAG('c','m','v','d'))
  4490. return AVERROR_INVALIDDATA;
  4491. moov_len = avio_rb32(pb); /* uncompressed size */
  4492. cmov_len = atom.size - 6 * 4;
  4493. cmov_data = av_malloc(cmov_len);
  4494. if (!cmov_data)
  4495. return AVERROR(ENOMEM);
  4496. moov_data = av_malloc(moov_len);
  4497. if (!moov_data) {
  4498. av_free(cmov_data);
  4499. return AVERROR(ENOMEM);
  4500. }
  4501. ret = ffio_read_size(pb, cmov_data, cmov_len);
  4502. if (ret < 0)
  4503. goto free_and_return;
  4504. ret = AVERROR_INVALIDDATA;
  4505. if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  4506. goto free_and_return;
  4507. if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  4508. goto free_and_return;
  4509. ctx.seekable = AVIO_SEEKABLE_NORMAL;
  4510. atom.type = MKTAG('m','o','o','v');
  4511. atom.size = moov_len;
  4512. ret = mov_read_default(c, &ctx, atom);
  4513. free_and_return:
  4514. av_free(moov_data);
  4515. av_free(cmov_data);
  4516. return ret;
  4517. #else
  4518. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  4519. return AVERROR(ENOSYS);
  4520. #endif
  4521. }
  4522. /* edit list atom */
  4523. static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4524. {
  4525. MOVStreamContext *sc;
  4526. int i, edit_count, version;
  4527. int64_t elst_entry_size;
  4528. if (c->fc->nb_streams < 1 || c->ignore_editlist)
  4529. return 0;
  4530. sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  4531. version = avio_r8(pb); /* version */
  4532. avio_rb24(pb); /* flags */
  4533. edit_count = avio_rb32(pb); /* entries */
  4534. atom.size -= 8;
  4535. elst_entry_size = version == 1 ? 20 : 12;
  4536. if (atom.size != edit_count * elst_entry_size) {
  4537. if (c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
  4538. av_log(c->fc, AV_LOG_ERROR, "Invalid edit list entry_count: %d for elst atom of size: %"PRId64" bytes.\n",
  4539. edit_count, atom.size + 8);
  4540. return AVERROR_INVALIDDATA;
  4541. } else {
  4542. edit_count = atom.size / elst_entry_size;
  4543. if (edit_count * elst_entry_size != atom.size) {
  4544. av_log(c->fc, AV_LOG_WARNING, "ELST atom of %"PRId64" bytes, bigger than %d entries.\n", atom.size, edit_count);
  4545. }
  4546. }
  4547. }
  4548. if (!edit_count)
  4549. return 0;
  4550. if (sc->elst_data)
  4551. av_log(c->fc, AV_LOG_WARNING, "Duplicated ELST atom\n");
  4552. av_free(sc->elst_data);
  4553. sc->elst_count = 0;
  4554. sc->elst_data = av_malloc_array(edit_count, sizeof(*sc->elst_data));
  4555. if (!sc->elst_data)
  4556. return AVERROR(ENOMEM);
  4557. av_log(c->fc, AV_LOG_TRACE, "track[%u].edit_count = %i\n", c->fc->nb_streams - 1, edit_count);
  4558. for (i = 0; i < edit_count && atom.size > 0 && !pb->eof_reached; i++) {
  4559. MOVElst *e = &sc->elst_data[i];
  4560. if (version == 1) {
  4561. e->duration = avio_rb64(pb);
  4562. e->time = avio_rb64(pb);
  4563. atom.size -= 16;
  4564. } else {
  4565. e->duration = avio_rb32(pb); /* segment duration */
  4566. e->time = (int32_t)avio_rb32(pb); /* media time */
  4567. atom.size -= 8;
  4568. }
  4569. e->rate = avio_rb32(pb) / 65536.0;
  4570. atom.size -= 4;
  4571. av_log(c->fc, AV_LOG_TRACE, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
  4572. e->duration, e->time, e->rate);
  4573. if (e->time < 0 && e->time != -1 &&
  4574. c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
  4575. av_log(c->fc, AV_LOG_ERROR, "Track %d, edit %d: Invalid edit list media time=%"PRId64"\n",
  4576. c->fc->nb_streams-1, i, e->time);
  4577. return AVERROR_INVALIDDATA;
  4578. }
  4579. }
  4580. sc->elst_count = i;
  4581. return 0;
  4582. }
  4583. static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4584. {
  4585. MOVStreamContext *sc;
  4586. if (c->fc->nb_streams < 1)
  4587. return AVERROR_INVALIDDATA;
  4588. sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
  4589. sc->timecode_track = avio_rb32(pb);
  4590. return 0;
  4591. }
  4592. static int mov_read_av1c(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4593. {
  4594. AVStream *st;
  4595. int ret;
  4596. if (c->fc->nb_streams < 1)
  4597. return 0;
  4598. st = c->fc->streams[c->fc->nb_streams - 1];
  4599. if (atom.size < 4) {
  4600. av_log(c->fc, AV_LOG_ERROR, "Empty AV1 Codec Configuration Box\n");
  4601. return AVERROR_INVALIDDATA;
  4602. }
  4603. /* For now, propagate only the OBUs, if any. Once libavcodec is
  4604. updated to handle isobmff style extradata this can be removed. */
  4605. avio_skip(pb, 4);
  4606. if (atom.size == 4)
  4607. return 0;
  4608. ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 4);
  4609. if (ret < 0)
  4610. return ret;
  4611. return 0;
  4612. }
  4613. static int mov_read_vpcc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4614. {
  4615. AVStream *st;
  4616. int version, color_range, color_primaries, color_trc, color_space;
  4617. if (c->fc->nb_streams < 1)
  4618. return 0;
  4619. st = c->fc->streams[c->fc->nb_streams - 1];
  4620. if (atom.size < 5) {
  4621. av_log(c->fc, AV_LOG_ERROR, "Empty VP Codec Configuration box\n");
  4622. return AVERROR_INVALIDDATA;
  4623. }
  4624. version = avio_r8(pb);
  4625. if (version != 1) {
  4626. av_log(c->fc, AV_LOG_WARNING, "Unsupported VP Codec Configuration box version %d\n", version);
  4627. return 0;
  4628. }
  4629. avio_skip(pb, 3); /* flags */
  4630. avio_skip(pb, 2); /* profile + level */
  4631. color_range = avio_r8(pb); /* bitDepth, chromaSubsampling, videoFullRangeFlag */
  4632. color_primaries = avio_r8(pb);
  4633. color_trc = avio_r8(pb);
  4634. color_space = avio_r8(pb);
  4635. if (avio_rb16(pb)) /* codecIntializationDataSize */
  4636. return AVERROR_INVALIDDATA;
  4637. if (!av_color_primaries_name(color_primaries))
  4638. color_primaries = AVCOL_PRI_UNSPECIFIED;
  4639. if (!av_color_transfer_name(color_trc))
  4640. color_trc = AVCOL_TRC_UNSPECIFIED;
  4641. if (!av_color_space_name(color_space))
  4642. color_space = AVCOL_SPC_UNSPECIFIED;
  4643. st->codecpar->color_range = (color_range & 1) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  4644. st->codecpar->color_primaries = color_primaries;
  4645. st->codecpar->color_trc = color_trc;
  4646. st->codecpar->color_space = color_space;
  4647. return 0;
  4648. }
  4649. static int mov_read_smdm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4650. {
  4651. MOVStreamContext *sc;
  4652. int i, version;
  4653. if (c->fc->nb_streams < 1)
  4654. return AVERROR_INVALIDDATA;
  4655. sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
  4656. if (atom.size < 5) {
  4657. av_log(c->fc, AV_LOG_ERROR, "Empty Mastering Display Metadata box\n");
  4658. return AVERROR_INVALIDDATA;
  4659. }
  4660. version = avio_r8(pb);
  4661. if (version) {
  4662. av_log(c->fc, AV_LOG_WARNING, "Unsupported Mastering Display Metadata box version %d\n", version);
  4663. return 0;
  4664. }
  4665. avio_skip(pb, 3); /* flags */
  4666. sc->mastering = av_mastering_display_metadata_alloc();
  4667. if (!sc->mastering)
  4668. return AVERROR(ENOMEM);
  4669. for (i = 0; i < 3; i++) {
  4670. sc->mastering->display_primaries[i][0] = av_make_q(avio_rb16(pb), 1 << 16);
  4671. sc->mastering->display_primaries[i][1] = av_make_q(avio_rb16(pb), 1 << 16);
  4672. }
  4673. sc->mastering->white_point[0] = av_make_q(avio_rb16(pb), 1 << 16);
  4674. sc->mastering->white_point[1] = av_make_q(avio_rb16(pb), 1 << 16);
  4675. sc->mastering->max_luminance = av_make_q(avio_rb32(pb), 1 << 8);
  4676. sc->mastering->min_luminance = av_make_q(avio_rb32(pb), 1 << 14);
  4677. sc->mastering->has_primaries = 1;
  4678. sc->mastering->has_luminance = 1;
  4679. return 0;
  4680. }
  4681. static int mov_read_mdcv(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4682. {
  4683. MOVStreamContext *sc;
  4684. const int mapping[3] = {1, 2, 0};
  4685. const int chroma_den = 50000;
  4686. const int luma_den = 10000;
  4687. int i;
  4688. if (c->fc->nb_streams < 1)
  4689. return AVERROR_INVALIDDATA;
  4690. sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
  4691. if (atom.size < 24) {
  4692. av_log(c->fc, AV_LOG_ERROR, "Invalid Mastering Display Color Volume box\n");
  4693. return AVERROR_INVALIDDATA;
  4694. }
  4695. sc->mastering = av_mastering_display_metadata_alloc();
  4696. if (!sc->mastering)
  4697. return AVERROR(ENOMEM);
  4698. for (i = 0; i < 3; i++) {
  4699. const int j = mapping[i];
  4700. sc->mastering->display_primaries[j][0] = av_make_q(avio_rb16(pb), chroma_den);
  4701. sc->mastering->display_primaries[j][1] = av_make_q(avio_rb16(pb), chroma_den);
  4702. }
  4703. sc->mastering->white_point[0] = av_make_q(avio_rb16(pb), chroma_den);
  4704. sc->mastering->white_point[1] = av_make_q(avio_rb16(pb), chroma_den);
  4705. sc->mastering->max_luminance = av_make_q(avio_rb32(pb), luma_den);
  4706. sc->mastering->min_luminance = av_make_q(avio_rb32(pb), luma_den);
  4707. sc->mastering->has_luminance = 1;
  4708. sc->mastering->has_primaries = 1;
  4709. return 0;
  4710. }
  4711. static int mov_read_coll(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4712. {
  4713. MOVStreamContext *sc;
  4714. int version;
  4715. if (c->fc->nb_streams < 1)
  4716. return AVERROR_INVALIDDATA;
  4717. sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
  4718. if (atom.size < 5) {
  4719. av_log(c->fc, AV_LOG_ERROR, "Empty Content Light Level box\n");
  4720. return AVERROR_INVALIDDATA;
  4721. }
  4722. version = avio_r8(pb);
  4723. if (version) {
  4724. av_log(c->fc, AV_LOG_WARNING, "Unsupported Content Light Level box version %d\n", version);
  4725. return 0;
  4726. }
  4727. avio_skip(pb, 3); /* flags */
  4728. sc->coll = av_content_light_metadata_alloc(&sc->coll_size);
  4729. if (!sc->coll)
  4730. return AVERROR(ENOMEM);
  4731. sc->coll->MaxCLL = avio_rb16(pb);
  4732. sc->coll->MaxFALL = avio_rb16(pb);
  4733. return 0;
  4734. }
  4735. static int mov_read_clli(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4736. {
  4737. MOVStreamContext *sc;
  4738. if (c->fc->nb_streams < 1)
  4739. return AVERROR_INVALIDDATA;
  4740. sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
  4741. if (atom.size < 4) {
  4742. av_log(c->fc, AV_LOG_ERROR, "Empty Content Light Level Info box\n");
  4743. return AVERROR_INVALIDDATA;
  4744. }
  4745. sc->coll = av_content_light_metadata_alloc(&sc->coll_size);
  4746. if (!sc->coll)
  4747. return AVERROR(ENOMEM);
  4748. sc->coll->MaxCLL = avio_rb16(pb);
  4749. sc->coll->MaxFALL = avio_rb16(pb);
  4750. return 0;
  4751. }
  4752. static int mov_read_st3d(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4753. {
  4754. AVStream *st;
  4755. MOVStreamContext *sc;
  4756. enum AVStereo3DType type;
  4757. int mode;
  4758. if (c->fc->nb_streams < 1)
  4759. return 0;
  4760. st = c->fc->streams[c->fc->nb_streams - 1];
  4761. sc = st->priv_data;
  4762. if (atom.size < 5) {
  4763. av_log(c->fc, AV_LOG_ERROR, "Empty stereoscopic video box\n");
  4764. return AVERROR_INVALIDDATA;
  4765. }
  4766. avio_skip(pb, 4); /* version + flags */
  4767. mode = avio_r8(pb);
  4768. switch (mode) {
  4769. case 0:
  4770. type = AV_STEREO3D_2D;
  4771. break;
  4772. case 1:
  4773. type = AV_STEREO3D_TOPBOTTOM;
  4774. break;
  4775. case 2:
  4776. type = AV_STEREO3D_SIDEBYSIDE;
  4777. break;
  4778. default:
  4779. av_log(c->fc, AV_LOG_WARNING, "Unknown st3d mode value %d\n", mode);
  4780. return 0;
  4781. }
  4782. sc->stereo3d = av_stereo3d_alloc();
  4783. if (!sc->stereo3d)
  4784. return AVERROR(ENOMEM);
  4785. sc->stereo3d->type = type;
  4786. return 0;
  4787. }
  4788. static int mov_read_sv3d(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4789. {
  4790. AVStream *st;
  4791. MOVStreamContext *sc;
  4792. int size, version, layout;
  4793. int32_t yaw, pitch, roll;
  4794. uint32_t l = 0, t = 0, r = 0, b = 0;
  4795. uint32_t tag, padding = 0;
  4796. enum AVSphericalProjection projection;
  4797. if (c->fc->nb_streams < 1)
  4798. return 0;
  4799. st = c->fc->streams[c->fc->nb_streams - 1];
  4800. sc = st->priv_data;
  4801. if (atom.size < 8) {
  4802. av_log(c->fc, AV_LOG_ERROR, "Empty spherical video box\n");
  4803. return AVERROR_INVALIDDATA;
  4804. }
  4805. size = avio_rb32(pb);
  4806. if (size <= 12 || size > atom.size)
  4807. return AVERROR_INVALIDDATA;
  4808. tag = avio_rl32(pb);
  4809. if (tag != MKTAG('s','v','h','d')) {
  4810. av_log(c->fc, AV_LOG_ERROR, "Missing spherical video header\n");
  4811. return 0;
  4812. }
  4813. version = avio_r8(pb);
  4814. if (version != 0) {
  4815. av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
  4816. version);
  4817. return 0;
  4818. }
  4819. avio_skip(pb, 3); /* flags */
  4820. avio_skip(pb, size - 12); /* metadata_source */
  4821. size = avio_rb32(pb);
  4822. if (size > atom.size)
  4823. return AVERROR_INVALIDDATA;
  4824. tag = avio_rl32(pb);
  4825. if (tag != MKTAG('p','r','o','j')) {
  4826. av_log(c->fc, AV_LOG_ERROR, "Missing projection box\n");
  4827. return 0;
  4828. }
  4829. size = avio_rb32(pb);
  4830. if (size > atom.size)
  4831. return AVERROR_INVALIDDATA;
  4832. tag = avio_rl32(pb);
  4833. if (tag != MKTAG('p','r','h','d')) {
  4834. av_log(c->fc, AV_LOG_ERROR, "Missing projection header box\n");
  4835. return 0;
  4836. }
  4837. version = avio_r8(pb);
  4838. if (version != 0) {
  4839. av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
  4840. version);
  4841. return 0;
  4842. }
  4843. avio_skip(pb, 3); /* flags */
  4844. /* 16.16 fixed point */
  4845. yaw = avio_rb32(pb);
  4846. pitch = avio_rb32(pb);
  4847. roll = avio_rb32(pb);
  4848. size = avio_rb32(pb);
  4849. if (size > atom.size)
  4850. return AVERROR_INVALIDDATA;
  4851. tag = avio_rl32(pb);
  4852. version = avio_r8(pb);
  4853. if (version != 0) {
  4854. av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
  4855. version);
  4856. return 0;
  4857. }
  4858. avio_skip(pb, 3); /* flags */
  4859. switch (tag) {
  4860. case MKTAG('c','b','m','p'):
  4861. layout = avio_rb32(pb);
  4862. if (layout) {
  4863. av_log(c->fc, AV_LOG_WARNING,
  4864. "Unsupported cubemap layout %d\n", layout);
  4865. return 0;
  4866. }
  4867. projection = AV_SPHERICAL_CUBEMAP;
  4868. padding = avio_rb32(pb);
  4869. break;
  4870. case MKTAG('e','q','u','i'):
  4871. t = avio_rb32(pb);
  4872. b = avio_rb32(pb);
  4873. l = avio_rb32(pb);
  4874. r = avio_rb32(pb);
  4875. if (b >= UINT_MAX - t || r >= UINT_MAX - l) {
  4876. av_log(c->fc, AV_LOG_ERROR,
  4877. "Invalid bounding rectangle coordinates "
  4878. "%"PRIu32",%"PRIu32",%"PRIu32",%"PRIu32"\n", l, t, r, b);
  4879. return AVERROR_INVALIDDATA;
  4880. }
  4881. if (l || t || r || b)
  4882. projection = AV_SPHERICAL_EQUIRECTANGULAR_TILE;
  4883. else
  4884. projection = AV_SPHERICAL_EQUIRECTANGULAR;
  4885. break;
  4886. default:
  4887. av_log(c->fc, AV_LOG_ERROR, "Unknown projection type: %s\n", av_fourcc2str(tag));
  4888. return 0;
  4889. }
  4890. sc->spherical = av_spherical_alloc(&sc->spherical_size);
  4891. if (!sc->spherical)
  4892. return AVERROR(ENOMEM);
  4893. sc->spherical->projection = projection;
  4894. sc->spherical->yaw = yaw;
  4895. sc->spherical->pitch = pitch;
  4896. sc->spherical->roll = roll;
  4897. sc->spherical->padding = padding;
  4898. sc->spherical->bound_left = l;
  4899. sc->spherical->bound_top = t;
  4900. sc->spherical->bound_right = r;
  4901. sc->spherical->bound_bottom = b;
  4902. return 0;
  4903. }
  4904. static int mov_parse_uuid_spherical(MOVStreamContext *sc, AVIOContext *pb, size_t len)
  4905. {
  4906. int ret = 0;
  4907. uint8_t *buffer = av_malloc(len + 1);
  4908. const char *val;
  4909. if (!buffer)
  4910. return AVERROR(ENOMEM);
  4911. buffer[len] = '\0';
  4912. ret = ffio_read_size(pb, buffer, len);
  4913. if (ret < 0)
  4914. goto out;
  4915. /* Check for mandatory keys and values, try to support XML as best-effort */
  4916. if (!sc->spherical &&
  4917. av_stristr(buffer, "<GSpherical:StitchingSoftware>") &&
  4918. (val = av_stristr(buffer, "<GSpherical:Spherical>")) &&
  4919. av_stristr(val, "true") &&
  4920. (val = av_stristr(buffer, "<GSpherical:Stitched>")) &&
  4921. av_stristr(val, "true") &&
  4922. (val = av_stristr(buffer, "<GSpherical:ProjectionType>")) &&
  4923. av_stristr(val, "equirectangular")) {
  4924. sc->spherical = av_spherical_alloc(&sc->spherical_size);
  4925. if (!sc->spherical)
  4926. goto out;
  4927. sc->spherical->projection = AV_SPHERICAL_EQUIRECTANGULAR;
  4928. if (av_stristr(buffer, "<GSpherical:StereoMode>") && !sc->stereo3d) {
  4929. enum AVStereo3DType mode;
  4930. if (av_stristr(buffer, "left-right"))
  4931. mode = AV_STEREO3D_SIDEBYSIDE;
  4932. else if (av_stristr(buffer, "top-bottom"))
  4933. mode = AV_STEREO3D_TOPBOTTOM;
  4934. else
  4935. mode = AV_STEREO3D_2D;
  4936. sc->stereo3d = av_stereo3d_alloc();
  4937. if (!sc->stereo3d)
  4938. goto out;
  4939. sc->stereo3d->type = mode;
  4940. }
  4941. /* orientation */
  4942. val = av_stristr(buffer, "<GSpherical:InitialViewHeadingDegrees>");
  4943. if (val)
  4944. sc->spherical->yaw = strtol(val, NULL, 10) * (1 << 16);
  4945. val = av_stristr(buffer, "<GSpherical:InitialViewPitchDegrees>");
  4946. if (val)
  4947. sc->spherical->pitch = strtol(val, NULL, 10) * (1 << 16);
  4948. val = av_stristr(buffer, "<GSpherical:InitialViewRollDegrees>");
  4949. if (val)
  4950. sc->spherical->roll = strtol(val, NULL, 10) * (1 << 16);
  4951. }
  4952. out:
  4953. av_free(buffer);
  4954. return ret;
  4955. }
  4956. static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4957. {
  4958. AVStream *st;
  4959. MOVStreamContext *sc;
  4960. int64_t ret;
  4961. uint8_t uuid[16];
  4962. static const uint8_t uuid_isml_manifest[] = {
  4963. 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
  4964. 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
  4965. };
  4966. static const uint8_t uuid_xmp[] = {
  4967. 0xbe, 0x7a, 0xcf, 0xcb, 0x97, 0xa9, 0x42, 0xe8,
  4968. 0x9c, 0x71, 0x99, 0x94, 0x91, 0xe3, 0xaf, 0xac
  4969. };
  4970. static const uint8_t uuid_spherical[] = {
  4971. 0xff, 0xcc, 0x82, 0x63, 0xf8, 0x55, 0x4a, 0x93,
  4972. 0x88, 0x14, 0x58, 0x7a, 0x02, 0x52, 0x1f, 0xdd,
  4973. };
  4974. if (atom.size < sizeof(uuid) || atom.size >= FFMIN(INT_MAX, SIZE_MAX))
  4975. return AVERROR_INVALIDDATA;
  4976. if (c->fc->nb_streams < 1)
  4977. return 0;
  4978. st = c->fc->streams[c->fc->nb_streams - 1];
  4979. sc = st->priv_data;
  4980. ret = ffio_read_size(pb, uuid, sizeof(uuid));
  4981. if (ret < 0)
  4982. return ret;
  4983. if (!memcmp(uuid, uuid_isml_manifest, sizeof(uuid))) {
  4984. uint8_t *buffer, *ptr;
  4985. char *endptr;
  4986. size_t len = atom.size - sizeof(uuid);
  4987. if (len < 4) {
  4988. return AVERROR_INVALIDDATA;
  4989. }
  4990. ret = avio_skip(pb, 4); // zeroes
  4991. len -= 4;
  4992. buffer = av_mallocz(len + 1);
  4993. if (!buffer) {
  4994. return AVERROR(ENOMEM);
  4995. }
  4996. ret = ffio_read_size(pb, buffer, len);
  4997. if (ret < 0) {
  4998. av_free(buffer);
  4999. return ret;
  5000. }
  5001. ptr = buffer;
  5002. while ((ptr = av_stristr(ptr, "systemBitrate=\""))) {
  5003. ptr += sizeof("systemBitrate=\"") - 1;
  5004. c->bitrates_count++;
  5005. c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
  5006. if (!c->bitrates) {
  5007. c->bitrates_count = 0;
  5008. av_free(buffer);
  5009. return AVERROR(ENOMEM);
  5010. }
  5011. errno = 0;
  5012. ret = strtol(ptr, &endptr, 10);
  5013. if (ret < 0 || errno || *endptr != '"') {
  5014. c->bitrates[c->bitrates_count - 1] = 0;
  5015. } else {
  5016. c->bitrates[c->bitrates_count - 1] = ret;
  5017. }
  5018. }
  5019. av_free(buffer);
  5020. } else if (!memcmp(uuid, uuid_xmp, sizeof(uuid))) {
  5021. uint8_t *buffer;
  5022. size_t len = atom.size - sizeof(uuid);
  5023. if (c->export_xmp) {
  5024. buffer = av_mallocz(len + 1);
  5025. if (!buffer) {
  5026. return AVERROR(ENOMEM);
  5027. }
  5028. ret = ffio_read_size(pb, buffer, len);
  5029. if (ret < 0) {
  5030. av_free(buffer);
  5031. return ret;
  5032. }
  5033. buffer[len] = '\0';
  5034. av_dict_set(&c->fc->metadata, "xmp",
  5035. buffer, AV_DICT_DONT_STRDUP_VAL);
  5036. } else {
  5037. // skip all uuid atom, which makes it fast for long uuid-xmp file
  5038. ret = avio_skip(pb, len);
  5039. if (ret < 0)
  5040. return ret;
  5041. }
  5042. } else if (!memcmp(uuid, uuid_spherical, sizeof(uuid))) {
  5043. size_t len = atom.size - sizeof(uuid);
  5044. ret = mov_parse_uuid_spherical(sc, pb, len);
  5045. if (ret < 0)
  5046. return ret;
  5047. if (!sc->spherical)
  5048. av_log(c->fc, AV_LOG_WARNING, "Invalid spherical metadata found\n");
  5049. }
  5050. return 0;
  5051. }
  5052. static int mov_read_free(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5053. {
  5054. int ret;
  5055. uint8_t content[16];
  5056. if (atom.size < 8)
  5057. return 0;
  5058. ret = avio_read(pb, content, FFMIN(sizeof(content), atom.size));
  5059. if (ret < 0)
  5060. return ret;
  5061. if ( !c->found_moov
  5062. && !c->found_mdat
  5063. && !memcmp(content, "Anevia\x1A\x1A", 8)
  5064. && c->use_mfra_for == FF_MOV_FLAG_MFRA_AUTO) {
  5065. c->use_mfra_for = FF_MOV_FLAG_MFRA_PTS;
  5066. }
  5067. return 0;
  5068. }
  5069. static int mov_read_frma(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5070. {
  5071. uint32_t format = avio_rl32(pb);
  5072. MOVStreamContext *sc;
  5073. enum AVCodecID id;
  5074. AVStream *st;
  5075. if (c->fc->nb_streams < 1)
  5076. return 0;
  5077. st = c->fc->streams[c->fc->nb_streams - 1];
  5078. sc = st->priv_data;
  5079. switch (sc->format)
  5080. {
  5081. case MKTAG('e','n','c','v'): // encrypted video
  5082. case MKTAG('e','n','c','a'): // encrypted audio
  5083. id = mov_codec_id(st, format);
  5084. if (st->codecpar->codec_id != AV_CODEC_ID_NONE &&
  5085. st->codecpar->codec_id != id) {
  5086. av_log(c->fc, AV_LOG_WARNING,
  5087. "ignoring 'frma' atom of '%.4s', stream has codec id %d\n",
  5088. (char*)&format, st->codecpar->codec_id);
  5089. break;
  5090. }
  5091. st->codecpar->codec_id = id;
  5092. sc->format = format;
  5093. break;
  5094. default:
  5095. if (format != sc->format) {
  5096. av_log(c->fc, AV_LOG_WARNING,
  5097. "ignoring 'frma' atom of '%.4s', stream format is '%.4s'\n",
  5098. (char*)&format, (char*)&sc->format);
  5099. }
  5100. break;
  5101. }
  5102. return 0;
  5103. }
  5104. /**
  5105. * Gets the current encryption info and associated current stream context. If
  5106. * we are parsing a track fragment, this will return the specific encryption
  5107. * info for this fragment; otherwise this will return the global encryption
  5108. * info for the current stream.
  5109. */
  5110. static int get_current_encryption_info(MOVContext *c, MOVEncryptionIndex **encryption_index, MOVStreamContext **sc)
  5111. {
  5112. MOVFragmentStreamInfo *frag_stream_info;
  5113. AVStream *st;
  5114. int i;
  5115. frag_stream_info = get_current_frag_stream_info(&c->frag_index);
  5116. if (frag_stream_info) {
  5117. for (i = 0; i < c->fc->nb_streams; i++) {
  5118. if (c->fc->streams[i]->id == frag_stream_info->id) {
  5119. st = c->fc->streams[i];
  5120. break;
  5121. }
  5122. }
  5123. if (i == c->fc->nb_streams)
  5124. return 0;
  5125. *sc = st->priv_data;
  5126. if (!frag_stream_info->encryption_index) {
  5127. // If this stream isn't encrypted, don't create the index.
  5128. if (!(*sc)->cenc.default_encrypted_sample)
  5129. return 0;
  5130. frag_stream_info->encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
  5131. if (!frag_stream_info->encryption_index)
  5132. return AVERROR(ENOMEM);
  5133. }
  5134. *encryption_index = frag_stream_info->encryption_index;
  5135. return 1;
  5136. } else {
  5137. // No current track fragment, using stream level encryption info.
  5138. if (c->fc->nb_streams < 1)
  5139. return 0;
  5140. st = c->fc->streams[c->fc->nb_streams - 1];
  5141. *sc = st->priv_data;
  5142. if (!(*sc)->cenc.encryption_index) {
  5143. // If this stream isn't encrypted, don't create the index.
  5144. if (!(*sc)->cenc.default_encrypted_sample)
  5145. return 0;
  5146. (*sc)->cenc.encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
  5147. if (!(*sc)->cenc.encryption_index)
  5148. return AVERROR(ENOMEM);
  5149. }
  5150. *encryption_index = (*sc)->cenc.encryption_index;
  5151. return 1;
  5152. }
  5153. }
  5154. static int mov_read_sample_encryption_info(MOVContext *c, AVIOContext *pb, MOVStreamContext *sc, AVEncryptionInfo **sample, int use_subsamples)
  5155. {
  5156. int i, ret;
  5157. unsigned int subsample_count;
  5158. AVSubsampleEncryptionInfo *subsamples;
  5159. if (!sc->cenc.default_encrypted_sample) {
  5160. av_log(c->fc, AV_LOG_ERROR, "Missing schm or tenc\n");
  5161. return AVERROR_INVALIDDATA;
  5162. }
  5163. *sample = av_encryption_info_clone(sc->cenc.default_encrypted_sample);
  5164. if (!*sample)
  5165. return AVERROR(ENOMEM);
  5166. if (sc->cenc.per_sample_iv_size != 0) {
  5167. if ((ret = ffio_read_size(pb, (*sample)->iv, sc->cenc.per_sample_iv_size)) < 0) {
  5168. av_log(c->fc, AV_LOG_ERROR, "failed to read the initialization vector\n");
  5169. av_encryption_info_free(*sample);
  5170. *sample = NULL;
  5171. return ret;
  5172. }
  5173. }
  5174. if (use_subsamples) {
  5175. subsample_count = avio_rb16(pb);
  5176. av_free((*sample)->subsamples);
  5177. (*sample)->subsamples = av_mallocz_array(subsample_count, sizeof(*subsamples));
  5178. if (!(*sample)->subsamples) {
  5179. av_encryption_info_free(*sample);
  5180. *sample = NULL;
  5181. return AVERROR(ENOMEM);
  5182. }
  5183. for (i = 0; i < subsample_count && !pb->eof_reached; i++) {
  5184. (*sample)->subsamples[i].bytes_of_clear_data = avio_rb16(pb);
  5185. (*sample)->subsamples[i].bytes_of_protected_data = avio_rb32(pb);
  5186. }
  5187. if (pb->eof_reached) {
  5188. av_log(c->fc, AV_LOG_ERROR, "hit EOF while reading sub-sample encryption info\n");
  5189. av_encryption_info_free(*sample);
  5190. *sample = NULL;
  5191. return AVERROR_INVALIDDATA;
  5192. }
  5193. (*sample)->subsample_count = subsample_count;
  5194. }
  5195. return 0;
  5196. }
  5197. static int mov_read_senc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5198. {
  5199. AVEncryptionInfo **encrypted_samples;
  5200. MOVEncryptionIndex *encryption_index;
  5201. MOVStreamContext *sc;
  5202. int use_subsamples, ret;
  5203. unsigned int sample_count, i, alloc_size = 0;
  5204. ret = get_current_encryption_info(c, &encryption_index, &sc);
  5205. if (ret != 1)
  5206. return ret;
  5207. if (encryption_index->nb_encrypted_samples) {
  5208. // This can happen if we have both saio/saiz and senc atoms.
  5209. av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in senc\n");
  5210. return 0;
  5211. }
  5212. avio_r8(pb); /* version */
  5213. use_subsamples = avio_rb24(pb) & 0x02; /* flags */
  5214. sample_count = avio_rb32(pb);
  5215. if (sample_count >= INT_MAX / sizeof(*encrypted_samples))
  5216. return AVERROR(ENOMEM);
  5217. for (i = 0; i < sample_count; i++) {
  5218. unsigned int min_samples = FFMIN(FFMAX(i + 1, 1024 * 1024), sample_count);
  5219. encrypted_samples = av_fast_realloc(encryption_index->encrypted_samples, &alloc_size,
  5220. min_samples * sizeof(*encrypted_samples));
  5221. if (encrypted_samples) {
  5222. encryption_index->encrypted_samples = encrypted_samples;
  5223. ret = mov_read_sample_encryption_info(
  5224. c, pb, sc, &encryption_index->encrypted_samples[i], use_subsamples);
  5225. } else {
  5226. ret = AVERROR(ENOMEM);
  5227. }
  5228. if (pb->eof_reached) {
  5229. av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading senc\n");
  5230. ret = AVERROR_INVALIDDATA;
  5231. }
  5232. if (ret < 0) {
  5233. for (; i > 0; i--)
  5234. av_encryption_info_free(encryption_index->encrypted_samples[i - 1]);
  5235. av_freep(&encryption_index->encrypted_samples);
  5236. return ret;
  5237. }
  5238. }
  5239. encryption_index->nb_encrypted_samples = sample_count;
  5240. return 0;
  5241. }
  5242. static int mov_parse_auxiliary_info(MOVContext *c, MOVStreamContext *sc, AVIOContext *pb, MOVEncryptionIndex *encryption_index)
  5243. {
  5244. AVEncryptionInfo **sample, **encrypted_samples;
  5245. int64_t prev_pos;
  5246. size_t sample_count, sample_info_size, i;
  5247. int ret = 0;
  5248. unsigned int alloc_size = 0;
  5249. if (encryption_index->nb_encrypted_samples)
  5250. return 0;
  5251. sample_count = encryption_index->auxiliary_info_sample_count;
  5252. if (encryption_index->auxiliary_offsets_count != 1) {
  5253. av_log(c->fc, AV_LOG_ERROR, "Multiple auxiliary info chunks are not supported\n");
  5254. return AVERROR_PATCHWELCOME;
  5255. }
  5256. if (sample_count >= INT_MAX / sizeof(*encrypted_samples))
  5257. return AVERROR(ENOMEM);
  5258. prev_pos = avio_tell(pb);
  5259. if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) ||
  5260. avio_seek(pb, encryption_index->auxiliary_offsets[0], SEEK_SET) != encryption_index->auxiliary_offsets[0]) {
  5261. av_log(c->fc, AV_LOG_INFO, "Failed to seek for auxiliary info, will only parse senc atoms for encryption info\n");
  5262. goto finish;
  5263. }
  5264. for (i = 0; i < sample_count && !pb->eof_reached; i++) {
  5265. unsigned int min_samples = FFMIN(FFMAX(i + 1, 1024 * 1024), sample_count);
  5266. encrypted_samples = av_fast_realloc(encryption_index->encrypted_samples, &alloc_size,
  5267. min_samples * sizeof(*encrypted_samples));
  5268. if (!encrypted_samples) {
  5269. ret = AVERROR(ENOMEM);
  5270. goto finish;
  5271. }
  5272. encryption_index->encrypted_samples = encrypted_samples;
  5273. sample = &encryption_index->encrypted_samples[i];
  5274. sample_info_size = encryption_index->auxiliary_info_default_size
  5275. ? encryption_index->auxiliary_info_default_size
  5276. : encryption_index->auxiliary_info_sizes[i];
  5277. ret = mov_read_sample_encryption_info(c, pb, sc, sample, sample_info_size > sc->cenc.per_sample_iv_size);
  5278. if (ret < 0)
  5279. goto finish;
  5280. }
  5281. if (pb->eof_reached) {
  5282. av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading auxiliary info\n");
  5283. ret = AVERROR_INVALIDDATA;
  5284. } else {
  5285. encryption_index->nb_encrypted_samples = sample_count;
  5286. }
  5287. finish:
  5288. avio_seek(pb, prev_pos, SEEK_SET);
  5289. if (ret < 0) {
  5290. for (; i > 0; i--) {
  5291. av_encryption_info_free(encryption_index->encrypted_samples[i - 1]);
  5292. }
  5293. av_freep(&encryption_index->encrypted_samples);
  5294. }
  5295. return ret;
  5296. }
  5297. /**
  5298. * Tries to read the given number of bytes from the stream and puts it in a
  5299. * newly allocated buffer. This reads in small chunks to avoid allocating large
  5300. * memory if the file contains an invalid/malicious size value.
  5301. */
  5302. static int mov_try_read_block(AVIOContext *pb, size_t size, uint8_t **data)
  5303. {
  5304. const unsigned int block_size = 1024 * 1024;
  5305. uint8_t *buffer = NULL;
  5306. unsigned int alloc_size = 0, offset = 0;
  5307. while (offset < size) {
  5308. unsigned int new_size =
  5309. alloc_size >= INT_MAX - block_size ? INT_MAX : alloc_size + block_size;
  5310. uint8_t *new_buffer = av_fast_realloc(buffer, &alloc_size, new_size);
  5311. unsigned int to_read = FFMIN(size, alloc_size) - offset;
  5312. if (!new_buffer) {
  5313. av_free(buffer);
  5314. return AVERROR(ENOMEM);
  5315. }
  5316. buffer = new_buffer;
  5317. if (avio_read(pb, buffer + offset, to_read) != to_read) {
  5318. av_free(buffer);
  5319. return AVERROR_INVALIDDATA;
  5320. }
  5321. offset += to_read;
  5322. }
  5323. *data = buffer;
  5324. return 0;
  5325. }
  5326. static int mov_read_saiz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5327. {
  5328. MOVEncryptionIndex *encryption_index;
  5329. MOVStreamContext *sc;
  5330. int ret;
  5331. unsigned int sample_count, aux_info_type, aux_info_param;
  5332. ret = get_current_encryption_info(c, &encryption_index, &sc);
  5333. if (ret != 1)
  5334. return ret;
  5335. if (encryption_index->nb_encrypted_samples) {
  5336. // This can happen if we have both saio/saiz and senc atoms.
  5337. av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in saiz\n");
  5338. return 0;
  5339. }
  5340. if (encryption_index->auxiliary_info_sample_count) {
  5341. av_log(c->fc, AV_LOG_ERROR, "Duplicate saiz atom\n");
  5342. return AVERROR_INVALIDDATA;
  5343. }
  5344. avio_r8(pb); /* version */
  5345. if (avio_rb24(pb) & 0x01) { /* flags */
  5346. aux_info_type = avio_rb32(pb);
  5347. aux_info_param = avio_rb32(pb);
  5348. if (sc->cenc.default_encrypted_sample) {
  5349. if (aux_info_type != sc->cenc.default_encrypted_sample->scheme) {
  5350. av_log(c->fc, AV_LOG_DEBUG, "Ignoring saiz box with non-zero aux_info_type\n");
  5351. return 0;
  5352. }
  5353. if (aux_info_param != 0) {
  5354. av_log(c->fc, AV_LOG_DEBUG, "Ignoring saiz box with non-zero aux_info_type_parameter\n");
  5355. return 0;
  5356. }
  5357. } else {
  5358. // Didn't see 'schm' or 'tenc', so this isn't encrypted.
  5359. if ((aux_info_type == MKBETAG('c','e','n','c') ||
  5360. aux_info_type == MKBETAG('c','e','n','s') ||
  5361. aux_info_type == MKBETAG('c','b','c','1') ||
  5362. aux_info_type == MKBETAG('c','b','c','s')) &&
  5363. aux_info_param == 0) {
  5364. av_log(c->fc, AV_LOG_ERROR, "Saw encrypted saiz without schm/tenc\n");
  5365. return AVERROR_INVALIDDATA;
  5366. } else {
  5367. return 0;
  5368. }
  5369. }
  5370. } else if (!sc->cenc.default_encrypted_sample) {
  5371. // Didn't see 'schm' or 'tenc', so this isn't encrypted.
  5372. return 0;
  5373. }
  5374. encryption_index->auxiliary_info_default_size = avio_r8(pb);
  5375. sample_count = avio_rb32(pb);
  5376. encryption_index->auxiliary_info_sample_count = sample_count;
  5377. if (encryption_index->auxiliary_info_default_size == 0) {
  5378. ret = mov_try_read_block(pb, sample_count, &encryption_index->auxiliary_info_sizes);
  5379. if (ret < 0) {
  5380. av_log(c->fc, AV_LOG_ERROR, "Failed to read the auxiliary info\n");
  5381. return ret;
  5382. }
  5383. }
  5384. if (encryption_index->auxiliary_offsets_count) {
  5385. return mov_parse_auxiliary_info(c, sc, pb, encryption_index);
  5386. }
  5387. return 0;
  5388. }
  5389. static int mov_read_saio(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5390. {
  5391. uint64_t *auxiliary_offsets;
  5392. MOVEncryptionIndex *encryption_index;
  5393. MOVStreamContext *sc;
  5394. int i, ret;
  5395. unsigned int version, entry_count, aux_info_type, aux_info_param;
  5396. unsigned int alloc_size = 0;
  5397. ret = get_current_encryption_info(c, &encryption_index, &sc);
  5398. if (ret != 1)
  5399. return ret;
  5400. if (encryption_index->nb_encrypted_samples) {
  5401. // This can happen if we have both saio/saiz and senc atoms.
  5402. av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in saio\n");
  5403. return 0;
  5404. }
  5405. if (encryption_index->auxiliary_offsets_count) {
  5406. av_log(c->fc, AV_LOG_ERROR, "Duplicate saio atom\n");
  5407. return AVERROR_INVALIDDATA;
  5408. }
  5409. version = avio_r8(pb); /* version */
  5410. if (avio_rb24(pb) & 0x01) { /* flags */
  5411. aux_info_type = avio_rb32(pb);
  5412. aux_info_param = avio_rb32(pb);
  5413. if (sc->cenc.default_encrypted_sample) {
  5414. if (aux_info_type != sc->cenc.default_encrypted_sample->scheme) {
  5415. av_log(c->fc, AV_LOG_DEBUG, "Ignoring saio box with non-zero aux_info_type\n");
  5416. return 0;
  5417. }
  5418. if (aux_info_param != 0) {
  5419. av_log(c->fc, AV_LOG_DEBUG, "Ignoring saio box with non-zero aux_info_type_parameter\n");
  5420. return 0;
  5421. }
  5422. } else {
  5423. // Didn't see 'schm' or 'tenc', so this isn't encrypted.
  5424. if ((aux_info_type == MKBETAG('c','e','n','c') ||
  5425. aux_info_type == MKBETAG('c','e','n','s') ||
  5426. aux_info_type == MKBETAG('c','b','c','1') ||
  5427. aux_info_type == MKBETAG('c','b','c','s')) &&
  5428. aux_info_param == 0) {
  5429. av_log(c->fc, AV_LOG_ERROR, "Saw encrypted saio without schm/tenc\n");
  5430. return AVERROR_INVALIDDATA;
  5431. } else {
  5432. return 0;
  5433. }
  5434. }
  5435. } else if (!sc->cenc.default_encrypted_sample) {
  5436. // Didn't see 'schm' or 'tenc', so this isn't encrypted.
  5437. return 0;
  5438. }
  5439. entry_count = avio_rb32(pb);
  5440. if (entry_count >= INT_MAX / sizeof(*auxiliary_offsets))
  5441. return AVERROR(ENOMEM);
  5442. for (i = 0; i < entry_count && !pb->eof_reached; i++) {
  5443. unsigned int min_offsets = FFMIN(FFMAX(i + 1, 1024), entry_count);
  5444. auxiliary_offsets = av_fast_realloc(
  5445. encryption_index->auxiliary_offsets, &alloc_size,
  5446. min_offsets * sizeof(*auxiliary_offsets));
  5447. if (!auxiliary_offsets) {
  5448. av_freep(&encryption_index->auxiliary_offsets);
  5449. return AVERROR(ENOMEM);
  5450. }
  5451. encryption_index->auxiliary_offsets = auxiliary_offsets;
  5452. if (version == 0) {
  5453. encryption_index->auxiliary_offsets[i] = avio_rb32(pb);
  5454. } else {
  5455. encryption_index->auxiliary_offsets[i] = avio_rb64(pb);
  5456. }
  5457. if (c->frag_index.current >= 0) {
  5458. encryption_index->auxiliary_offsets[i] += c->fragment.base_data_offset;
  5459. }
  5460. }
  5461. if (pb->eof_reached) {
  5462. av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading saio\n");
  5463. av_freep(&encryption_index->auxiliary_offsets);
  5464. return AVERROR_INVALIDDATA;
  5465. }
  5466. encryption_index->auxiliary_offsets_count = entry_count;
  5467. if (encryption_index->auxiliary_info_sample_count) {
  5468. return mov_parse_auxiliary_info(c, sc, pb, encryption_index);
  5469. }
  5470. return 0;
  5471. }
  5472. static int mov_read_pssh(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5473. {
  5474. AVEncryptionInitInfo *info, *old_init_info;
  5475. uint8_t **key_ids;
  5476. AVStream *st;
  5477. uint8_t *side_data, *extra_data, *old_side_data;
  5478. size_t side_data_size;
  5479. int ret = 0, old_side_data_size;
  5480. unsigned int version, kid_count, extra_data_size, alloc_size = 0;
  5481. if (c->fc->nb_streams < 1)
  5482. return 0;
  5483. st = c->fc->streams[c->fc->nb_streams-1];
  5484. version = avio_r8(pb); /* version */
  5485. avio_rb24(pb); /* flags */
  5486. info = av_encryption_init_info_alloc(/* system_id_size */ 16, /* num_key_ids */ 0,
  5487. /* key_id_size */ 16, /* data_size */ 0);
  5488. if (!info)
  5489. return AVERROR(ENOMEM);
  5490. if ((ret = ffio_read_size(pb, info->system_id, 16)) < 0) {
  5491. av_log(c->fc, AV_LOG_ERROR, "Failed to read the system id\n");
  5492. goto finish;
  5493. }
  5494. if (version > 0) {
  5495. kid_count = avio_rb32(pb);
  5496. if (kid_count >= INT_MAX / sizeof(*key_ids)) {
  5497. ret = AVERROR(ENOMEM);
  5498. goto finish;
  5499. }
  5500. for (unsigned int i = 0; i < kid_count && !pb->eof_reached; i++) {
  5501. unsigned int min_kid_count = FFMIN(FFMAX(i + 1, 1024), kid_count);
  5502. key_ids = av_fast_realloc(info->key_ids, &alloc_size,
  5503. min_kid_count * sizeof(*key_ids));
  5504. if (!key_ids) {
  5505. ret = AVERROR(ENOMEM);
  5506. goto finish;
  5507. }
  5508. info->key_ids = key_ids;
  5509. info->key_ids[i] = av_mallocz(16);
  5510. if (!info->key_ids[i]) {
  5511. ret = AVERROR(ENOMEM);
  5512. goto finish;
  5513. }
  5514. info->num_key_ids = i + 1;
  5515. if ((ret = ffio_read_size(pb, info->key_ids[i], 16)) < 0) {
  5516. av_log(c->fc, AV_LOG_ERROR, "Failed to read the key id\n");
  5517. goto finish;
  5518. }
  5519. }
  5520. if (pb->eof_reached) {
  5521. av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading pssh\n");
  5522. ret = AVERROR_INVALIDDATA;
  5523. goto finish;
  5524. }
  5525. }
  5526. extra_data_size = avio_rb32(pb);
  5527. ret = mov_try_read_block(pb, extra_data_size, &extra_data);
  5528. if (ret < 0)
  5529. goto finish;
  5530. av_freep(&info->data); // malloc(0) may still allocate something.
  5531. info->data = extra_data;
  5532. info->data_size = extra_data_size;
  5533. // If there is existing initialization data, append to the list.
  5534. old_side_data = av_stream_get_side_data(st, AV_PKT_DATA_ENCRYPTION_INIT_INFO, &old_side_data_size);
  5535. if (old_side_data) {
  5536. old_init_info = av_encryption_init_info_get_side_data(old_side_data, old_side_data_size);
  5537. if (old_init_info) {
  5538. // Append to the end of the list.
  5539. for (AVEncryptionInitInfo *cur = old_init_info;; cur = cur->next) {
  5540. if (!cur->next) {
  5541. cur->next = info;
  5542. break;
  5543. }
  5544. }
  5545. info = old_init_info;
  5546. } else {
  5547. // Assume existing side-data will be valid, so the only error we could get is OOM.
  5548. ret = AVERROR(ENOMEM);
  5549. goto finish;
  5550. }
  5551. }
  5552. side_data = av_encryption_init_info_add_side_data(info, &side_data_size);
  5553. if (!side_data) {
  5554. ret = AVERROR(ENOMEM);
  5555. goto finish;
  5556. }
  5557. ret = av_stream_add_side_data(st, AV_PKT_DATA_ENCRYPTION_INIT_INFO,
  5558. side_data, side_data_size);
  5559. if (ret < 0)
  5560. av_free(side_data);
  5561. finish:
  5562. av_encryption_init_info_free(info);
  5563. return ret;
  5564. }
  5565. static int mov_read_schm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5566. {
  5567. AVStream *st;
  5568. MOVStreamContext *sc;
  5569. if (c->fc->nb_streams < 1)
  5570. return 0;
  5571. st = c->fc->streams[c->fc->nb_streams-1];
  5572. sc = st->priv_data;
  5573. if (sc->pseudo_stream_id != 0) {
  5574. av_log(c->fc, AV_LOG_ERROR, "schm boxes are only supported in first sample descriptor\n");
  5575. return AVERROR_PATCHWELCOME;
  5576. }
  5577. if (atom.size < 8)
  5578. return AVERROR_INVALIDDATA;
  5579. avio_rb32(pb); /* version and flags */
  5580. if (!sc->cenc.default_encrypted_sample) {
  5581. sc->cenc.default_encrypted_sample = av_encryption_info_alloc(0, 16, 16);
  5582. if (!sc->cenc.default_encrypted_sample) {
  5583. return AVERROR(ENOMEM);
  5584. }
  5585. }
  5586. sc->cenc.default_encrypted_sample->scheme = avio_rb32(pb);
  5587. return 0;
  5588. }
  5589. static int mov_read_tenc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5590. {
  5591. AVStream *st;
  5592. MOVStreamContext *sc;
  5593. unsigned int version, pattern, is_protected, iv_size;
  5594. if (c->fc->nb_streams < 1)
  5595. return 0;
  5596. st = c->fc->streams[c->fc->nb_streams-1];
  5597. sc = st->priv_data;
  5598. if (sc->pseudo_stream_id != 0) {
  5599. av_log(c->fc, AV_LOG_ERROR, "tenc atom are only supported in first sample descriptor\n");
  5600. return AVERROR_PATCHWELCOME;
  5601. }
  5602. if (!sc->cenc.default_encrypted_sample) {
  5603. sc->cenc.default_encrypted_sample = av_encryption_info_alloc(0, 16, 16);
  5604. if (!sc->cenc.default_encrypted_sample) {
  5605. return AVERROR(ENOMEM);
  5606. }
  5607. }
  5608. if (atom.size < 20)
  5609. return AVERROR_INVALIDDATA;
  5610. version = avio_r8(pb); /* version */
  5611. avio_rb24(pb); /* flags */
  5612. avio_r8(pb); /* reserved */
  5613. pattern = avio_r8(pb);
  5614. if (version > 0) {
  5615. sc->cenc.default_encrypted_sample->crypt_byte_block = pattern >> 4;
  5616. sc->cenc.default_encrypted_sample->skip_byte_block = pattern & 0xf;
  5617. }
  5618. is_protected = avio_r8(pb);
  5619. if (is_protected && !sc->cenc.encryption_index) {
  5620. // The whole stream should be by-default encrypted.
  5621. sc->cenc.encryption_index = av_mallocz(sizeof(MOVEncryptionIndex));
  5622. if (!sc->cenc.encryption_index)
  5623. return AVERROR(ENOMEM);
  5624. }
  5625. sc->cenc.per_sample_iv_size = avio_r8(pb);
  5626. if (sc->cenc.per_sample_iv_size != 0 && sc->cenc.per_sample_iv_size != 8 &&
  5627. sc->cenc.per_sample_iv_size != 16) {
  5628. av_log(c->fc, AV_LOG_ERROR, "invalid per-sample IV size value\n");
  5629. return AVERROR_INVALIDDATA;
  5630. }
  5631. if (avio_read(pb, sc->cenc.default_encrypted_sample->key_id, 16) != 16) {
  5632. av_log(c->fc, AV_LOG_ERROR, "failed to read the default key ID\n");
  5633. return AVERROR_INVALIDDATA;
  5634. }
  5635. if (is_protected && !sc->cenc.per_sample_iv_size) {
  5636. iv_size = avio_r8(pb);
  5637. if (iv_size != 8 && iv_size != 16) {
  5638. av_log(c->fc, AV_LOG_ERROR, "invalid default_constant_IV_size in tenc atom\n");
  5639. return AVERROR_INVALIDDATA;
  5640. }
  5641. if (avio_read(pb, sc->cenc.default_encrypted_sample->iv, iv_size) != iv_size) {
  5642. av_log(c->fc, AV_LOG_ERROR, "failed to read the default IV\n");
  5643. return AVERROR_INVALIDDATA;
  5644. }
  5645. }
  5646. return 0;
  5647. }
  5648. static int mov_read_dfla(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5649. {
  5650. AVStream *st;
  5651. int last, type, size, ret;
  5652. uint8_t buf[4];
  5653. if (c->fc->nb_streams < 1)
  5654. return 0;
  5655. st = c->fc->streams[c->fc->nb_streams-1];
  5656. if ((uint64_t)atom.size > (1<<30) || atom.size < 42)
  5657. return AVERROR_INVALIDDATA;
  5658. /* Check FlacSpecificBox version. */
  5659. if (avio_r8(pb) != 0)
  5660. return AVERROR_INVALIDDATA;
  5661. avio_rb24(pb); /* Flags */
  5662. avio_read(pb, buf, sizeof(buf));
  5663. flac_parse_block_header(buf, &last, &type, &size);
  5664. if (type != FLAC_METADATA_TYPE_STREAMINFO || size != FLAC_STREAMINFO_SIZE) {
  5665. av_log(c->fc, AV_LOG_ERROR, "STREAMINFO must be first FLACMetadataBlock\n");
  5666. return AVERROR_INVALIDDATA;
  5667. }
  5668. ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
  5669. if (ret < 0)
  5670. return ret;
  5671. if (!last)
  5672. av_log(c->fc, AV_LOG_WARNING, "non-STREAMINFO FLACMetadataBlock(s) ignored\n");
  5673. return 0;
  5674. }
  5675. static int cenc_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
  5676. {
  5677. int i, ret;
  5678. if (sample->scheme != MKBETAG('c','e','n','c') || sample->crypt_byte_block != 0 || sample->skip_byte_block != 0) {
  5679. av_log(c->fc, AV_LOG_ERROR, "Only the 'cenc' encryption scheme is supported\n");
  5680. return AVERROR_PATCHWELCOME;
  5681. }
  5682. if (!sc->cenc.aes_ctr) {
  5683. /* initialize the cipher */
  5684. sc->cenc.aes_ctr = av_aes_ctr_alloc();
  5685. if (!sc->cenc.aes_ctr) {
  5686. return AVERROR(ENOMEM);
  5687. }
  5688. ret = av_aes_ctr_init(sc->cenc.aes_ctr, c->decryption_key);
  5689. if (ret < 0) {
  5690. return ret;
  5691. }
  5692. }
  5693. av_aes_ctr_set_full_iv(sc->cenc.aes_ctr, sample->iv);
  5694. if (!sample->subsample_count) {
  5695. /* decrypt the whole packet */
  5696. av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, size);
  5697. return 0;
  5698. }
  5699. for (i = 0; i < sample->subsample_count; i++) {
  5700. if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) {
  5701. av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
  5702. return AVERROR_INVALIDDATA;
  5703. }
  5704. /* skip the clear bytes */
  5705. input += sample->subsamples[i].bytes_of_clear_data;
  5706. size -= sample->subsamples[i].bytes_of_clear_data;
  5707. /* decrypt the encrypted bytes */
  5708. av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, sample->subsamples[i].bytes_of_protected_data);
  5709. input += sample->subsamples[i].bytes_of_protected_data;
  5710. size -= sample->subsamples[i].bytes_of_protected_data;
  5711. }
  5712. if (size > 0) {
  5713. av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
  5714. return AVERROR_INVALIDDATA;
  5715. }
  5716. return 0;
  5717. }
  5718. static int cenc_filter(MOVContext *mov, AVStream* st, MOVStreamContext *sc, AVPacket *pkt, int current_index)
  5719. {
  5720. MOVFragmentStreamInfo *frag_stream_info;
  5721. MOVEncryptionIndex *encryption_index;
  5722. AVEncryptionInfo *encrypted_sample;
  5723. int encrypted_index, ret;
  5724. frag_stream_info = get_frag_stream_info(&mov->frag_index, mov->frag_index.current, st->id);
  5725. encrypted_index = current_index;
  5726. encryption_index = NULL;
  5727. if (frag_stream_info) {
  5728. // Note this only supports encryption info in the first sample descriptor.
  5729. if (mov->fragment.stsd_id == 1) {
  5730. if (frag_stream_info->encryption_index) {
  5731. encrypted_index = current_index - frag_stream_info->index_entry;
  5732. encryption_index = frag_stream_info->encryption_index;
  5733. } else {
  5734. encryption_index = sc->cenc.encryption_index;
  5735. }
  5736. }
  5737. } else {
  5738. encryption_index = sc->cenc.encryption_index;
  5739. }
  5740. if (encryption_index) {
  5741. if (encryption_index->auxiliary_info_sample_count &&
  5742. !encryption_index->nb_encrypted_samples) {
  5743. av_log(mov->fc, AV_LOG_ERROR, "saiz atom found without saio\n");
  5744. return AVERROR_INVALIDDATA;
  5745. }
  5746. if (encryption_index->auxiliary_offsets_count &&
  5747. !encryption_index->nb_encrypted_samples) {
  5748. av_log(mov->fc, AV_LOG_ERROR, "saio atom found without saiz\n");
  5749. return AVERROR_INVALIDDATA;
  5750. }
  5751. if (!encryption_index->nb_encrypted_samples) {
  5752. // Full-sample encryption with default settings.
  5753. encrypted_sample = sc->cenc.default_encrypted_sample;
  5754. } else if (encrypted_index >= 0 && encrypted_index < encryption_index->nb_encrypted_samples) {
  5755. // Per-sample setting override.
  5756. encrypted_sample = encryption_index->encrypted_samples[encrypted_index];
  5757. } else {
  5758. av_log(mov->fc, AV_LOG_ERROR, "Incorrect number of samples in encryption info\n");
  5759. return AVERROR_INVALIDDATA;
  5760. }
  5761. if (mov->decryption_key) {
  5762. return cenc_decrypt(mov, sc, encrypted_sample, pkt->data, pkt->size);
  5763. } else {
  5764. size_t size;
  5765. uint8_t *side_data = av_encryption_info_add_side_data(encrypted_sample, &size);
  5766. if (!side_data)
  5767. return AVERROR(ENOMEM);
  5768. ret = av_packet_add_side_data(pkt, AV_PKT_DATA_ENCRYPTION_INFO, side_data, size);
  5769. if (ret < 0)
  5770. av_free(side_data);
  5771. return ret;
  5772. }
  5773. }
  5774. return 0;
  5775. }
  5776. static int mov_read_dops(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5777. {
  5778. const int OPUS_SEEK_PREROLL_MS = 80;
  5779. int ret;
  5780. AVStream *st;
  5781. size_t size;
  5782. uint16_t pre_skip;
  5783. if (c->fc->nb_streams < 1)
  5784. return 0;
  5785. st = c->fc->streams[c->fc->nb_streams-1];
  5786. if ((uint64_t)atom.size > (1<<30) || atom.size < 11)
  5787. return AVERROR_INVALIDDATA;
  5788. /* Check OpusSpecificBox version. */
  5789. if (avio_r8(pb) != 0) {
  5790. av_log(c->fc, AV_LOG_ERROR, "unsupported OpusSpecificBox version\n");
  5791. return AVERROR_INVALIDDATA;
  5792. }
  5793. /* OpusSpecificBox size plus magic for Ogg OpusHead header. */
  5794. size = atom.size + 8;
  5795. if ((ret = ff_alloc_extradata(st->codecpar, size)) < 0)
  5796. return ret;
  5797. AV_WL32(st->codecpar->extradata, MKTAG('O','p','u','s'));
  5798. AV_WL32(st->codecpar->extradata + 4, MKTAG('H','e','a','d'));
  5799. AV_WB8(st->codecpar->extradata + 8, 1); /* OpusHead version */
  5800. avio_read(pb, st->codecpar->extradata + 9, size - 9);
  5801. /* OpusSpecificBox is stored in big-endian, but OpusHead is
  5802. little-endian; aside from the preceeding magic and version they're
  5803. otherwise currently identical. Data after output gain at offset 16
  5804. doesn't need to be bytewapped. */
  5805. pre_skip = AV_RB16(st->codecpar->extradata + 10);
  5806. AV_WL16(st->codecpar->extradata + 10, pre_skip);
  5807. AV_WL32(st->codecpar->extradata + 12, AV_RB32(st->codecpar->extradata + 12));
  5808. AV_WL16(st->codecpar->extradata + 16, AV_RB16(st->codecpar->extradata + 16));
  5809. st->codecpar->initial_padding = pre_skip;
  5810. st->codecpar->seek_preroll = av_rescale_q(OPUS_SEEK_PREROLL_MS,
  5811. (AVRational){1, 1000},
  5812. (AVRational){1, 48000});
  5813. return 0;
  5814. }
  5815. static int mov_read_dmlp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5816. {
  5817. AVStream *st;
  5818. unsigned format_info;
  5819. int channel_assignment, channel_assignment1, channel_assignment2;
  5820. int ratebits;
  5821. if (c->fc->nb_streams < 1)
  5822. return 0;
  5823. st = c->fc->streams[c->fc->nb_streams-1];
  5824. if (atom.size < 10)
  5825. return AVERROR_INVALIDDATA;
  5826. format_info = avio_rb32(pb);
  5827. ratebits = (format_info >> 28) & 0xF;
  5828. channel_assignment1 = (format_info >> 15) & 0x1F;
  5829. channel_assignment2 = format_info & 0x1FFF;
  5830. if (channel_assignment2)
  5831. channel_assignment = channel_assignment2;
  5832. else
  5833. channel_assignment = channel_assignment1;
  5834. st->codecpar->frame_size = 40 << (ratebits & 0x7);
  5835. st->codecpar->sample_rate = mlp_samplerate(ratebits);
  5836. st->codecpar->channels = truehd_channels(channel_assignment);
  5837. st->codecpar->channel_layout = truehd_layout(channel_assignment);
  5838. return 0;
  5839. }
  5840. static int mov_read_dvcc_dvvc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5841. {
  5842. AVStream *st;
  5843. uint32_t buf;
  5844. AVDOVIDecoderConfigurationRecord *dovi;
  5845. size_t dovi_size;
  5846. int ret;
  5847. if (c->fc->nb_streams < 1)
  5848. return 0;
  5849. st = c->fc->streams[c->fc->nb_streams-1];
  5850. if ((uint64_t)atom.size > (1<<30) || atom.size < 4)
  5851. return AVERROR_INVALIDDATA;
  5852. dovi = av_dovi_alloc(&dovi_size);
  5853. if (!dovi)
  5854. return AVERROR(ENOMEM);
  5855. dovi->dv_version_major = avio_r8(pb);
  5856. dovi->dv_version_minor = avio_r8(pb);
  5857. buf = avio_rb16(pb);
  5858. dovi->dv_profile = (buf >> 9) & 0x7f; // 7 bits
  5859. dovi->dv_level = (buf >> 3) & 0x3f; // 6 bits
  5860. dovi->rpu_present_flag = (buf >> 2) & 0x01; // 1 bit
  5861. dovi->el_present_flag = (buf >> 1) & 0x01; // 1 bit
  5862. dovi->bl_present_flag = buf & 0x01; // 1 bit
  5863. if (atom.size >= 24) { // 4 + 4 + 4 * 4
  5864. buf = avio_r8(pb);
  5865. dovi->dv_bl_signal_compatibility_id = (buf >> 4) & 0x0f; // 4 bits
  5866. } else {
  5867. // 0 stands for None
  5868. // Dolby Vision V1.2.93 profiles and levels
  5869. dovi->dv_bl_signal_compatibility_id = 0;
  5870. }
  5871. ret = av_stream_add_side_data(st, AV_PKT_DATA_DOVI_CONF,
  5872. (uint8_t *)dovi, dovi_size);
  5873. if (ret < 0) {
  5874. av_free(dovi);
  5875. return ret;
  5876. }
  5877. av_log(c, AV_LOG_TRACE, "DOVI in dvcC/dvvC box, version: %d.%d, profile: %d, level: %d, "
  5878. "rpu flag: %d, el flag: %d, bl flag: %d, compatibility id: %d\n",
  5879. dovi->dv_version_major, dovi->dv_version_minor,
  5880. dovi->dv_profile, dovi->dv_level,
  5881. dovi->rpu_present_flag,
  5882. dovi->el_present_flag,
  5883. dovi->bl_present_flag,
  5884. dovi->dv_bl_signal_compatibility_id
  5885. );
  5886. return 0;
  5887. }
  5888. static const MOVParseTableEntry mov_default_parse_table[] = {
  5889. { MKTAG('A','C','L','R'), mov_read_aclr },
  5890. { MKTAG('A','P','R','G'), mov_read_avid },
  5891. { MKTAG('A','A','L','P'), mov_read_avid },
  5892. { MKTAG('A','R','E','S'), mov_read_ares },
  5893. { MKTAG('a','v','s','s'), mov_read_avss },
  5894. { MKTAG('a','v','1','C'), mov_read_av1c },
  5895. { MKTAG('c','h','p','l'), mov_read_chpl },
  5896. { MKTAG('c','o','6','4'), mov_read_stco },
  5897. { MKTAG('c','o','l','r'), mov_read_colr },
  5898. { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
  5899. { MKTAG('d','i','n','f'), mov_read_default },
  5900. { MKTAG('D','p','x','E'), mov_read_dpxe },
  5901. { MKTAG('d','r','e','f'), mov_read_dref },
  5902. { MKTAG('e','d','t','s'), mov_read_default },
  5903. { MKTAG('e','l','s','t'), mov_read_elst },
  5904. { MKTAG('e','n','d','a'), mov_read_enda },
  5905. { MKTAG('f','i','e','l'), mov_read_fiel },
  5906. { MKTAG('a','d','r','m'), mov_read_adrm },
  5907. { MKTAG('f','t','y','p'), mov_read_ftyp },
  5908. { MKTAG('g','l','b','l'), mov_read_glbl },
  5909. { MKTAG('h','d','l','r'), mov_read_hdlr },
  5910. { MKTAG('i','l','s','t'), mov_read_ilst },
  5911. { MKTAG('j','p','2','h'), mov_read_jp2h },
  5912. { MKTAG('m','d','a','t'), mov_read_mdat },
  5913. { MKTAG('m','d','h','d'), mov_read_mdhd },
  5914. { MKTAG('m','d','i','a'), mov_read_default },
  5915. { MKTAG('m','e','t','a'), mov_read_meta },
  5916. { MKTAG('m','i','n','f'), mov_read_default },
  5917. { MKTAG('m','o','o','f'), mov_read_moof },
  5918. { MKTAG('m','o','o','v'), mov_read_moov },
  5919. { MKTAG('m','v','e','x'), mov_read_default },
  5920. { MKTAG('m','v','h','d'), mov_read_mvhd },
  5921. { MKTAG('S','M','I',' '), mov_read_svq3 },
  5922. { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
  5923. { MKTAG('a','v','c','C'), mov_read_glbl },
  5924. { MKTAG('p','a','s','p'), mov_read_pasp },
  5925. { MKTAG('s','i','d','x'), mov_read_sidx },
  5926. { MKTAG('s','t','b','l'), mov_read_default },
  5927. { MKTAG('s','t','c','o'), mov_read_stco },
  5928. { MKTAG('s','t','p','s'), mov_read_stps },
  5929. { MKTAG('s','t','r','f'), mov_read_strf },
  5930. { MKTAG('s','t','s','c'), mov_read_stsc },
  5931. { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
  5932. { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
  5933. { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
  5934. { MKTAG('s','t','t','s'), mov_read_stts },
  5935. { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
  5936. { MKTAG('s','d','t','p'), mov_read_sdtp }, /* independent and disposable samples */
  5937. { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
  5938. { MKTAG('t','f','d','t'), mov_read_tfdt },
  5939. { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
  5940. { MKTAG('t','r','a','k'), mov_read_trak },
  5941. { MKTAG('t','r','a','f'), mov_read_default },
  5942. { MKTAG('t','r','e','f'), mov_read_default },
  5943. { MKTAG('t','m','c','d'), mov_read_tmcd },
  5944. { MKTAG('c','h','a','p'), mov_read_chap },
  5945. { MKTAG('t','r','e','x'), mov_read_trex },
  5946. { MKTAG('t','r','u','n'), mov_read_trun },
  5947. { MKTAG('u','d','t','a'), mov_read_default },
  5948. { MKTAG('w','a','v','e'), mov_read_wave },
  5949. { MKTAG('e','s','d','s'), mov_read_esds },
  5950. { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
  5951. { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
  5952. { MKTAG('d','d','t','s'), mov_read_ddts }, /* DTS audio descriptor */
  5953. { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
  5954. { MKTAG('w','f','e','x'), mov_read_wfex },
  5955. { MKTAG('c','m','o','v'), mov_read_cmov },
  5956. { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
  5957. { MKTAG('d','v','c','1'), mov_read_dvc1 },
  5958. { MKTAG('s','b','g','p'), mov_read_sbgp },
  5959. { MKTAG('h','v','c','C'), mov_read_glbl },
  5960. { MKTAG('u','u','i','d'), mov_read_uuid },
  5961. { MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
  5962. { MKTAG('f','r','e','e'), mov_read_free },
  5963. { MKTAG('-','-','-','-'), mov_read_custom },
  5964. { MKTAG('s','i','n','f'), mov_read_default },
  5965. { MKTAG('f','r','m','a'), mov_read_frma },
  5966. { MKTAG('s','e','n','c'), mov_read_senc },
  5967. { MKTAG('s','a','i','z'), mov_read_saiz },
  5968. { MKTAG('s','a','i','o'), mov_read_saio },
  5969. { MKTAG('p','s','s','h'), mov_read_pssh },
  5970. { MKTAG('s','c','h','m'), mov_read_schm },
  5971. { MKTAG('s','c','h','i'), mov_read_default },
  5972. { MKTAG('t','e','n','c'), mov_read_tenc },
  5973. { MKTAG('d','f','L','a'), mov_read_dfla },
  5974. { MKTAG('s','t','3','d'), mov_read_st3d }, /* stereoscopic 3D video box */
  5975. { MKTAG('s','v','3','d'), mov_read_sv3d }, /* spherical video box */
  5976. { MKTAG('d','O','p','s'), mov_read_dops },
  5977. { MKTAG('d','m','l','p'), mov_read_dmlp },
  5978. { MKTAG('S','m','D','m'), mov_read_smdm },
  5979. { MKTAG('C','o','L','L'), mov_read_coll },
  5980. { MKTAG('v','p','c','C'), mov_read_vpcc },
  5981. { MKTAG('m','d','c','v'), mov_read_mdcv },
  5982. { MKTAG('c','l','l','i'), mov_read_clli },
  5983. { MKTAG('d','v','c','C'), mov_read_dvcc_dvvc },
  5984. { MKTAG('d','v','v','C'), mov_read_dvcc_dvvc },
  5985. { 0, NULL }
  5986. };
  5987. static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5988. {
  5989. int64_t total_size = 0;
  5990. MOVAtom a;
  5991. int i;
  5992. if (c->atom_depth > 10) {
  5993. av_log(c->fc, AV_LOG_ERROR, "Atoms too deeply nested\n");
  5994. return AVERROR_INVALIDDATA;
  5995. }
  5996. c->atom_depth ++;
  5997. if (atom.size < 0)
  5998. atom.size = INT64_MAX;
  5999. while (total_size <= atom.size - 8 && !avio_feof(pb)) {
  6000. int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
  6001. a.size = atom.size;
  6002. a.type=0;
  6003. if (atom.size >= 8) {
  6004. a.size = avio_rb32(pb);
  6005. a.type = avio_rl32(pb);
  6006. if (((a.type == MKTAG('f','r','e','e') && c->moov_retry) ||
  6007. a.type == MKTAG('h','o','o','v')) &&
  6008. a.size >= 8 &&
  6009. c->fc->strict_std_compliance < FF_COMPLIANCE_STRICT) {
  6010. uint32_t type;
  6011. avio_skip(pb, 4);
  6012. type = avio_rl32(pb);
  6013. avio_seek(pb, -8, SEEK_CUR);
  6014. if (type == MKTAG('m','v','h','d') ||
  6015. type == MKTAG('c','m','o','v')) {
  6016. av_log(c->fc, AV_LOG_ERROR, "Detected moov in a free or hoov atom.\n");
  6017. a.type = MKTAG('m','o','o','v');
  6018. }
  6019. }
  6020. if (atom.type != MKTAG('r','o','o','t') &&
  6021. atom.type != MKTAG('m','o','o','v')) {
  6022. if (a.type == MKTAG('t','r','a','k') ||
  6023. a.type == MKTAG('m','d','a','t')) {
  6024. av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
  6025. avio_skip(pb, -8);
  6026. c->atom_depth --;
  6027. return 0;
  6028. }
  6029. }
  6030. total_size += 8;
  6031. if (a.size == 1 && total_size + 8 <= atom.size) { /* 64 bit extended size */
  6032. a.size = avio_rb64(pb) - 8;
  6033. total_size += 8;
  6034. }
  6035. }
  6036. av_log(c->fc, AV_LOG_TRACE, "type:'%s' parent:'%s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
  6037. av_fourcc2str(a.type), av_fourcc2str(atom.type), a.size, total_size, atom.size);
  6038. if (a.size == 0) {
  6039. a.size = atom.size - total_size + 8;
  6040. }
  6041. a.size -= 8;
  6042. if (a.size < 0)
  6043. break;
  6044. a.size = FFMIN(a.size, atom.size - total_size);
  6045. for (i = 0; mov_default_parse_table[i].type; i++)
  6046. if (mov_default_parse_table[i].type == a.type) {
  6047. parse = mov_default_parse_table[i].parse;
  6048. break;
  6049. }
  6050. // container is user data
  6051. if (!parse && (atom.type == MKTAG('u','d','t','a') ||
  6052. atom.type == MKTAG('i','l','s','t')))
  6053. parse = mov_read_udta_string;
  6054. // Supports parsing the QuickTime Metadata Keys.
  6055. // https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/Metadata/Metadata.html
  6056. if (!parse && c->found_hdlr_mdta &&
  6057. atom.type == MKTAG('m','e','t','a') &&
  6058. a.type == MKTAG('k','e','y','s') &&
  6059. c->meta_keys_count == 0) {
  6060. parse = mov_read_keys;
  6061. }
  6062. if (!parse) { /* skip leaf atoms data */
  6063. avio_skip(pb, a.size);
  6064. } else {
  6065. int64_t start_pos = avio_tell(pb);
  6066. int64_t left;
  6067. int err = parse(c, pb, a);
  6068. if (err < 0) {
  6069. c->atom_depth --;
  6070. return err;
  6071. }
  6072. if (c->found_moov && c->found_mdat &&
  6073. ((!(pb->seekable & AVIO_SEEKABLE_NORMAL) || c->fc->flags & AVFMT_FLAG_IGNIDX || c->frag_index.complete) ||
  6074. start_pos + a.size == avio_size(pb))) {
  6075. if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) || c->fc->flags & AVFMT_FLAG_IGNIDX || c->frag_index.complete)
  6076. c->next_root_atom = start_pos + a.size;
  6077. c->atom_depth --;
  6078. return 0;
  6079. }
  6080. left = a.size - avio_tell(pb) + start_pos;
  6081. if (left > 0) /* skip garbage at atom end */
  6082. avio_skip(pb, left);
  6083. else if (left < 0) {
  6084. av_log(c->fc, AV_LOG_WARNING,
  6085. "overread end of atom '%s' by %"PRId64" bytes\n",
  6086. av_fourcc2str(a.type), -left);
  6087. avio_seek(pb, left, SEEK_CUR);
  6088. }
  6089. }
  6090. total_size += a.size;
  6091. }
  6092. if (total_size < atom.size && atom.size < 0x7ffff)
  6093. avio_skip(pb, atom.size - total_size);
  6094. c->atom_depth --;
  6095. return 0;
  6096. }
  6097. static int mov_probe(const AVProbeData *p)
  6098. {
  6099. int64_t offset;
  6100. uint32_t tag;
  6101. int score = 0;
  6102. int moov_offset = -1;
  6103. /* check file header */
  6104. offset = 0;
  6105. for (;;) {
  6106. /* ignore invalid offset */
  6107. if ((offset + 8) > (unsigned int)p->buf_size)
  6108. break;
  6109. tag = AV_RL32(p->buf + offset + 4);
  6110. switch(tag) {
  6111. /* check for obvious tags */
  6112. case MKTAG('m','o','o','v'):
  6113. moov_offset = offset + 4;
  6114. case MKTAG('m','d','a','t'):
  6115. case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
  6116. case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
  6117. case MKTAG('f','t','y','p'):
  6118. if (AV_RB32(p->buf+offset) < 8 &&
  6119. (AV_RB32(p->buf+offset) != 1 ||
  6120. offset + 12 > (unsigned int)p->buf_size ||
  6121. AV_RB64(p->buf+offset + 8) == 0)) {
  6122. score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
  6123. } else if (tag == MKTAG('f','t','y','p') &&
  6124. ( AV_RL32(p->buf + offset + 8) == MKTAG('j','p','2',' ')
  6125. || AV_RL32(p->buf + offset + 8) == MKTAG('j','p','x',' ')
  6126. )) {
  6127. score = FFMAX(score, 5);
  6128. } else {
  6129. score = AVPROBE_SCORE_MAX;
  6130. }
  6131. offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
  6132. break;
  6133. /* those are more common words, so rate then a bit less */
  6134. case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
  6135. case MKTAG('w','i','d','e'):
  6136. case MKTAG('f','r','e','e'):
  6137. case MKTAG('j','u','n','k'):
  6138. case MKTAG('p','i','c','t'):
  6139. score = FFMAX(score, AVPROBE_SCORE_MAX - 5);
  6140. offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
  6141. break;
  6142. case MKTAG(0x82,0x82,0x7f,0x7d):
  6143. case MKTAG('s','k','i','p'):
  6144. case MKTAG('u','u','i','d'):
  6145. case MKTAG('p','r','f','l'):
  6146. /* if we only find those cause probedata is too small at least rate them */
  6147. score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
  6148. offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
  6149. break;
  6150. default:
  6151. offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
  6152. }
  6153. }
  6154. if (score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
  6155. /* moov atom in the header - we should make sure that this is not a
  6156. * MOV-packed MPEG-PS */
  6157. offset = moov_offset;
  6158. while (offset < (p->buf_size - 16)) { /* Sufficient space */
  6159. /* We found an actual hdlr atom */
  6160. if (AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') &&
  6161. AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') &&
  6162. AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')) {
  6163. av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
  6164. /* We found a media handler reference atom describing an
  6165. * MPEG-PS-in-MOV, return a
  6166. * low score to force expanding the probe window until
  6167. * mpegps_probe finds what it needs */
  6168. return 5;
  6169. } else {
  6170. /* Keep looking */
  6171. offset += 2;
  6172. }
  6173. }
  6174. }
  6175. return score;
  6176. }
  6177. // must be done after parsing all trak because there's no order requirement
  6178. static void mov_read_chapters(AVFormatContext *s)
  6179. {
  6180. MOVContext *mov = s->priv_data;
  6181. AVStream *st;
  6182. MOVStreamContext *sc;
  6183. int64_t cur_pos;
  6184. int i, j;
  6185. int chapter_track;
  6186. for (j = 0; j < mov->nb_chapter_tracks; j++) {
  6187. chapter_track = mov->chapter_tracks[j];
  6188. st = NULL;
  6189. for (i = 0; i < s->nb_streams; i++)
  6190. if (s->streams[i]->id == chapter_track) {
  6191. st = s->streams[i];
  6192. break;
  6193. }
  6194. if (!st) {
  6195. av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
  6196. continue;
  6197. }
  6198. sc = st->priv_data;
  6199. cur_pos = avio_tell(sc->pb);
  6200. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  6201. st->disposition |= AV_DISPOSITION_ATTACHED_PIC | AV_DISPOSITION_TIMED_THUMBNAILS;
  6202. if (st->nb_index_entries) {
  6203. // Retrieve the first frame, if possible
  6204. AVIndexEntry *sample = &st->index_entries[0];
  6205. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  6206. av_log(s, AV_LOG_ERROR, "Failed to retrieve first frame\n");
  6207. goto finish;
  6208. }
  6209. if (av_get_packet(sc->pb, &st->attached_pic, sample->size) < 0)
  6210. goto finish;
  6211. st->attached_pic.stream_index = st->index;
  6212. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  6213. }
  6214. } else {
  6215. st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
  6216. st->codecpar->codec_id = AV_CODEC_ID_BIN_DATA;
  6217. st->discard = AVDISCARD_ALL;
  6218. for (i = 0; i < st->nb_index_entries; i++) {
  6219. AVIndexEntry *sample = &st->index_entries[i];
  6220. int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
  6221. uint8_t *title;
  6222. uint16_t ch;
  6223. int len, title_len;
  6224. if (end < sample->timestamp) {
  6225. av_log(s, AV_LOG_WARNING, "ignoring stream duration which is shorter than chapters\n");
  6226. end = AV_NOPTS_VALUE;
  6227. }
  6228. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  6229. av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
  6230. goto finish;
  6231. }
  6232. // the first two bytes are the length of the title
  6233. len = avio_rb16(sc->pb);
  6234. if (len > sample->size-2)
  6235. continue;
  6236. title_len = 2*len + 1;
  6237. if (!(title = av_mallocz(title_len)))
  6238. goto finish;
  6239. // The samples could theoretically be in any encoding if there's an encd
  6240. // atom following, but in practice are only utf-8 or utf-16, distinguished
  6241. // instead by the presence of a BOM
  6242. if (!len) {
  6243. title[0] = 0;
  6244. } else {
  6245. ch = avio_rb16(sc->pb);
  6246. if (ch == 0xfeff)
  6247. avio_get_str16be(sc->pb, len, title, title_len);
  6248. else if (ch == 0xfffe)
  6249. avio_get_str16le(sc->pb, len, title, title_len);
  6250. else {
  6251. AV_WB16(title, ch);
  6252. if (len == 1 || len == 2)
  6253. title[len] = 0;
  6254. else
  6255. avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
  6256. }
  6257. }
  6258. avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
  6259. av_freep(&title);
  6260. }
  6261. }
  6262. finish:
  6263. avio_seek(sc->pb, cur_pos, SEEK_SET);
  6264. }
  6265. }
  6266. static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st,
  6267. uint32_t value, int flags)
  6268. {
  6269. AVTimecode tc;
  6270. char buf[AV_TIMECODE_STR_SIZE];
  6271. AVRational rate = st->avg_frame_rate;
  6272. int ret = av_timecode_init(&tc, rate, flags, 0, s);
  6273. if (ret < 0)
  6274. return ret;
  6275. av_dict_set(&st->metadata, "timecode",
  6276. av_timecode_make_string(&tc, buf, value), 0);
  6277. return 0;
  6278. }
  6279. static int mov_read_rtmd_track(AVFormatContext *s, AVStream *st)
  6280. {
  6281. MOVStreamContext *sc = st->priv_data;
  6282. char buf[AV_TIMECODE_STR_SIZE];
  6283. int64_t cur_pos = avio_tell(sc->pb);
  6284. int hh, mm, ss, ff, drop;
  6285. if (!st->nb_index_entries)
  6286. return -1;
  6287. avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
  6288. avio_skip(s->pb, 13);
  6289. hh = avio_r8(s->pb);
  6290. mm = avio_r8(s->pb);
  6291. ss = avio_r8(s->pb);
  6292. drop = avio_r8(s->pb);
  6293. ff = avio_r8(s->pb);
  6294. snprintf(buf, AV_TIMECODE_STR_SIZE, "%02d:%02d:%02d%c%02d",
  6295. hh, mm, ss, drop ? ';' : ':', ff);
  6296. av_dict_set(&st->metadata, "timecode", buf, 0);
  6297. avio_seek(sc->pb, cur_pos, SEEK_SET);
  6298. return 0;
  6299. }
  6300. static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
  6301. {
  6302. MOVStreamContext *sc = st->priv_data;
  6303. int flags = 0;
  6304. int64_t cur_pos = avio_tell(sc->pb);
  6305. uint32_t value;
  6306. if (!st->nb_index_entries)
  6307. return -1;
  6308. avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
  6309. value = avio_rb32(s->pb);
  6310. if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
  6311. if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
  6312. if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
  6313. /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
  6314. * not the case) and thus assume "frame number format" instead of QT one.
  6315. * No sample with tmcd track can be found with a QT timecode at the moment,
  6316. * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
  6317. * format). */
  6318. parse_timecode_in_framenum_format(s, st, value, flags);
  6319. avio_seek(sc->pb, cur_pos, SEEK_SET);
  6320. return 0;
  6321. }
  6322. static void mov_free_encryption_index(MOVEncryptionIndex **index) {
  6323. int i;
  6324. if (!index || !*index) return;
  6325. for (i = 0; i < (*index)->nb_encrypted_samples; i++) {
  6326. av_encryption_info_free((*index)->encrypted_samples[i]);
  6327. }
  6328. av_freep(&(*index)->encrypted_samples);
  6329. av_freep(&(*index)->auxiliary_info_sizes);
  6330. av_freep(&(*index)->auxiliary_offsets);
  6331. av_freep(index);
  6332. }
  6333. static int mov_read_close(AVFormatContext *s)
  6334. {
  6335. MOVContext *mov = s->priv_data;
  6336. int i, j;
  6337. for (i = 0; i < s->nb_streams; i++) {
  6338. AVStream *st = s->streams[i];
  6339. MOVStreamContext *sc = st->priv_data;
  6340. if (!sc)
  6341. continue;
  6342. av_freep(&sc->ctts_data);
  6343. for (j = 0; j < sc->drefs_count; j++) {
  6344. av_freep(&sc->drefs[j].path);
  6345. av_freep(&sc->drefs[j].dir);
  6346. }
  6347. av_freep(&sc->drefs);
  6348. sc->drefs_count = 0;
  6349. if (!sc->pb_is_copied)
  6350. ff_format_io_close(s, &sc->pb);
  6351. sc->pb = NULL;
  6352. av_freep(&sc->chunk_offsets);
  6353. av_freep(&sc->stsc_data);
  6354. av_freep(&sc->sample_sizes);
  6355. av_freep(&sc->keyframes);
  6356. av_freep(&sc->stts_data);
  6357. av_freep(&sc->sdtp_data);
  6358. av_freep(&sc->stps_data);
  6359. av_freep(&sc->elst_data);
  6360. av_freep(&sc->rap_group);
  6361. av_freep(&sc->display_matrix);
  6362. av_freep(&sc->index_ranges);
  6363. if (sc->extradata)
  6364. for (j = 0; j < sc->stsd_count; j++)
  6365. av_free(sc->extradata[j]);
  6366. av_freep(&sc->extradata);
  6367. av_freep(&sc->extradata_size);
  6368. mov_free_encryption_index(&sc->cenc.encryption_index);
  6369. av_encryption_info_free(sc->cenc.default_encrypted_sample);
  6370. av_aes_ctr_free(sc->cenc.aes_ctr);
  6371. av_freep(&sc->stereo3d);
  6372. av_freep(&sc->spherical);
  6373. av_freep(&sc->mastering);
  6374. av_freep(&sc->coll);
  6375. }
  6376. av_freep(&mov->dv_demux);
  6377. avformat_free_context(mov->dv_fctx);
  6378. mov->dv_fctx = NULL;
  6379. if (mov->meta_keys) {
  6380. for (i = 1; i < mov->meta_keys_count; i++) {
  6381. av_freep(&mov->meta_keys[i]);
  6382. }
  6383. av_freep(&mov->meta_keys);
  6384. }
  6385. av_freep(&mov->trex_data);
  6386. av_freep(&mov->bitrates);
  6387. for (i = 0; i < mov->frag_index.nb_items; i++) {
  6388. MOVFragmentStreamInfo *frag = mov->frag_index.item[i].stream_info;
  6389. for (j = 0; j < mov->frag_index.item[i].nb_stream_info; j++) {
  6390. mov_free_encryption_index(&frag[j].encryption_index);
  6391. }
  6392. av_freep(&mov->frag_index.item[i].stream_info);
  6393. }
  6394. av_freep(&mov->frag_index.item);
  6395. av_freep(&mov->aes_decrypt);
  6396. av_freep(&mov->chapter_tracks);
  6397. return 0;
  6398. }
  6399. static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
  6400. {
  6401. int i;
  6402. for (i = 0; i < s->nb_streams; i++) {
  6403. AVStream *st = s->streams[i];
  6404. MOVStreamContext *sc = st->priv_data;
  6405. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  6406. sc->timecode_track == tmcd_id)
  6407. return 1;
  6408. }
  6409. return 0;
  6410. }
  6411. /* look for a tmcd track not referenced by any video track, and export it globally */
  6412. static void export_orphan_timecode(AVFormatContext *s)
  6413. {
  6414. int i;
  6415. for (i = 0; i < s->nb_streams; i++) {
  6416. AVStream *st = s->streams[i];
  6417. if (st->codecpar->codec_tag == MKTAG('t','m','c','d') &&
  6418. !tmcd_is_referenced(s, i + 1)) {
  6419. AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
  6420. if (tcr) {
  6421. av_dict_set(&s->metadata, "timecode", tcr->value, 0);
  6422. break;
  6423. }
  6424. }
  6425. }
  6426. }
  6427. static int read_tfra(MOVContext *mov, AVIOContext *f)
  6428. {
  6429. int version, fieldlength, i, j;
  6430. int64_t pos = avio_tell(f);
  6431. uint32_t size = avio_rb32(f);
  6432. unsigned track_id, item_count;
  6433. if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a')) {
  6434. return 1;
  6435. }
  6436. av_log(mov->fc, AV_LOG_VERBOSE, "found tfra\n");
  6437. version = avio_r8(f);
  6438. avio_rb24(f);
  6439. track_id = avio_rb32(f);
  6440. fieldlength = avio_rb32(f);
  6441. item_count = avio_rb32(f);
  6442. for (i = 0; i < item_count; i++) {
  6443. int64_t time, offset;
  6444. int index;
  6445. MOVFragmentStreamInfo * frag_stream_info;
  6446. if (avio_feof(f)) {
  6447. return AVERROR_INVALIDDATA;
  6448. }
  6449. if (version == 1) {
  6450. time = avio_rb64(f);
  6451. offset = avio_rb64(f);
  6452. } else {
  6453. time = avio_rb32(f);
  6454. offset = avio_rb32(f);
  6455. }
  6456. // The first sample of each stream in a fragment is always a random
  6457. // access sample. So it's entry in the tfra can be used as the
  6458. // initial PTS of the fragment.
  6459. index = update_frag_index(mov, offset);
  6460. frag_stream_info = get_frag_stream_info(&mov->frag_index, index, track_id);
  6461. if (frag_stream_info &&
  6462. frag_stream_info->first_tfra_pts == AV_NOPTS_VALUE)
  6463. frag_stream_info->first_tfra_pts = time;
  6464. for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
  6465. avio_r8(f);
  6466. for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
  6467. avio_r8(f);
  6468. for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
  6469. avio_r8(f);
  6470. }
  6471. avio_seek(f, pos + size, SEEK_SET);
  6472. return 0;
  6473. }
  6474. static int mov_read_mfra(MOVContext *c, AVIOContext *f)
  6475. {
  6476. int64_t stream_size = avio_size(f);
  6477. int64_t original_pos = avio_tell(f);
  6478. int64_t seek_ret;
  6479. int32_t mfra_size;
  6480. int ret = -1;
  6481. if ((seek_ret = avio_seek(f, stream_size - 4, SEEK_SET)) < 0) {
  6482. ret = seek_ret;
  6483. goto fail;
  6484. }
  6485. mfra_size = avio_rb32(f);
  6486. if (mfra_size < 0 || mfra_size > stream_size) {
  6487. av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (unreasonable size)\n");
  6488. goto fail;
  6489. }
  6490. if ((seek_ret = avio_seek(f, -mfra_size, SEEK_CUR)) < 0) {
  6491. ret = seek_ret;
  6492. goto fail;
  6493. }
  6494. if (avio_rb32(f) != mfra_size) {
  6495. av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (size mismatch)\n");
  6496. goto fail;
  6497. }
  6498. if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
  6499. av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (tag mismatch)\n");
  6500. goto fail;
  6501. }
  6502. av_log(c->fc, AV_LOG_VERBOSE, "stream has mfra\n");
  6503. do {
  6504. ret = read_tfra(c, f);
  6505. if (ret < 0)
  6506. goto fail;
  6507. } while (!ret);
  6508. ret = 0;
  6509. fail:
  6510. seek_ret = avio_seek(f, original_pos, SEEK_SET);
  6511. if (seek_ret < 0) {
  6512. av_log(c->fc, AV_LOG_ERROR,
  6513. "failed to seek back after looking for mfra\n");
  6514. ret = seek_ret;
  6515. }
  6516. return ret;
  6517. }
  6518. static int mov_read_header(AVFormatContext *s)
  6519. {
  6520. MOVContext *mov = s->priv_data;
  6521. AVIOContext *pb = s->pb;
  6522. int j, err;
  6523. MOVAtom atom = { AV_RL32("root") };
  6524. int i;
  6525. if (mov->decryption_key_len != 0 && mov->decryption_key_len != AES_CTR_KEY_SIZE) {
  6526. av_log(s, AV_LOG_ERROR, "Invalid decryption key len %d expected %d\n",
  6527. mov->decryption_key_len, AES_CTR_KEY_SIZE);
  6528. return AVERROR(EINVAL);
  6529. }
  6530. mov->fc = s;
  6531. mov->trak_index = -1;
  6532. /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  6533. if (pb->seekable & AVIO_SEEKABLE_NORMAL)
  6534. atom.size = avio_size(pb);
  6535. else
  6536. atom.size = INT64_MAX;
  6537. /* check MOV header */
  6538. do {
  6539. if (mov->moov_retry)
  6540. avio_seek(pb, 0, SEEK_SET);
  6541. if ((err = mov_read_default(mov, pb, atom)) < 0) {
  6542. av_log(s, AV_LOG_ERROR, "error reading header\n");
  6543. goto fail;
  6544. }
  6545. } while ((pb->seekable & AVIO_SEEKABLE_NORMAL) && !mov->found_moov && !mov->moov_retry++);
  6546. if (!mov->found_moov) {
  6547. av_log(s, AV_LOG_ERROR, "moov atom not found\n");
  6548. err = AVERROR_INVALIDDATA;
  6549. goto fail;
  6550. }
  6551. av_log(mov->fc, AV_LOG_TRACE, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
  6552. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  6553. if (mov->nb_chapter_tracks > 0 && !mov->ignore_chapters)
  6554. mov_read_chapters(s);
  6555. for (i = 0; i < s->nb_streams; i++)
  6556. if (s->streams[i]->codecpar->codec_tag == AV_RL32("tmcd")) {
  6557. mov_read_timecode_track(s, s->streams[i]);
  6558. } else if (s->streams[i]->codecpar->codec_tag == AV_RL32("rtmd")) {
  6559. mov_read_rtmd_track(s, s->streams[i]);
  6560. }
  6561. }
  6562. /* copy timecode metadata from tmcd tracks to the related video streams */
  6563. for (i = 0; i < s->nb_streams; i++) {
  6564. AVStream *st = s->streams[i];
  6565. MOVStreamContext *sc = st->priv_data;
  6566. if (sc->timecode_track > 0) {
  6567. AVDictionaryEntry *tcr;
  6568. int tmcd_st_id = -1;
  6569. for (j = 0; j < s->nb_streams; j++)
  6570. if (s->streams[j]->id == sc->timecode_track)
  6571. tmcd_st_id = j;
  6572. if (tmcd_st_id < 0 || tmcd_st_id == i)
  6573. continue;
  6574. tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
  6575. if (tcr)
  6576. av_dict_set(&st->metadata, "timecode", tcr->value, 0);
  6577. }
  6578. }
  6579. export_orphan_timecode(s);
  6580. for (i = 0; i < s->nb_streams; i++) {
  6581. AVStream *st = s->streams[i];
  6582. MOVStreamContext *sc = st->priv_data;
  6583. fix_timescale(mov, sc);
  6584. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  6585. st->codecpar->codec_id == AV_CODEC_ID_AAC) {
  6586. st->skip_samples = sc->start_pad;
  6587. }
  6588. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
  6589. av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
  6590. sc->time_scale*(int64_t)sc->nb_frames_for_fps, sc->duration_for_fps, INT_MAX);
  6591. if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  6592. if (st->codecpar->width <= 0 || st->codecpar->height <= 0) {
  6593. st->codecpar->width = sc->width;
  6594. st->codecpar->height = sc->height;
  6595. }
  6596. if (st->codecpar->codec_id == AV_CODEC_ID_DVD_SUBTITLE) {
  6597. if ((err = mov_rewrite_dvd_sub_extradata(st)) < 0)
  6598. goto fail;
  6599. }
  6600. }
  6601. if (mov->handbrake_version &&
  6602. mov->handbrake_version <= 1000000*0 + 1000*10 + 2 && // 0.10.2
  6603. st->codecpar->codec_id == AV_CODEC_ID_MP3) {
  6604. av_log(s, AV_LOG_VERBOSE, "Forcing full parsing for mp3 stream\n");
  6605. st->need_parsing = AVSTREAM_PARSE_FULL;
  6606. }
  6607. }
  6608. if (mov->trex_data) {
  6609. for (i = 0; i < s->nb_streams; i++) {
  6610. AVStream *st = s->streams[i];
  6611. MOVStreamContext *sc = st->priv_data;
  6612. if (st->duration > 0) {
  6613. if (sc->data_size > INT64_MAX / sc->time_scale / 8) {
  6614. av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %"PRId64" * 8 * %d\n",
  6615. sc->data_size, sc->time_scale);
  6616. err = AVERROR_INVALIDDATA;
  6617. goto fail;
  6618. }
  6619. st->codecpar->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
  6620. }
  6621. }
  6622. }
  6623. if (mov->use_mfra_for > 0) {
  6624. for (i = 0; i < s->nb_streams; i++) {
  6625. AVStream *st = s->streams[i];
  6626. MOVStreamContext *sc = st->priv_data;
  6627. if (sc->duration_for_fps > 0) {
  6628. if (sc->data_size > INT64_MAX / sc->time_scale / 8) {
  6629. av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %"PRId64" * 8 * %d\n",
  6630. sc->data_size, sc->time_scale);
  6631. err = AVERROR_INVALIDDATA;
  6632. goto fail;
  6633. }
  6634. st->codecpar->bit_rate = sc->data_size * 8 * sc->time_scale /
  6635. sc->duration_for_fps;
  6636. }
  6637. }
  6638. }
  6639. for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
  6640. if (mov->bitrates[i]) {
  6641. s->streams[i]->codecpar->bit_rate = mov->bitrates[i];
  6642. }
  6643. }
  6644. ff_rfps_calculate(s);
  6645. for (i = 0; i < s->nb_streams; i++) {
  6646. AVStream *st = s->streams[i];
  6647. MOVStreamContext *sc = st->priv_data;
  6648. switch (st->codecpar->codec_type) {
  6649. case AVMEDIA_TYPE_AUDIO:
  6650. err = ff_replaygain_export(st, s->metadata);
  6651. if (err < 0)
  6652. goto fail;
  6653. break;
  6654. case AVMEDIA_TYPE_VIDEO:
  6655. if (sc->display_matrix) {
  6656. err = av_stream_add_side_data(st, AV_PKT_DATA_DISPLAYMATRIX, (uint8_t*)sc->display_matrix,
  6657. sizeof(int32_t) * 9);
  6658. if (err < 0)
  6659. goto fail;
  6660. sc->display_matrix = NULL;
  6661. }
  6662. if (sc->stereo3d) {
  6663. err = av_stream_add_side_data(st, AV_PKT_DATA_STEREO3D,
  6664. (uint8_t *)sc->stereo3d,
  6665. sizeof(*sc->stereo3d));
  6666. if (err < 0)
  6667. goto fail;
  6668. sc->stereo3d = NULL;
  6669. }
  6670. if (sc->spherical) {
  6671. err = av_stream_add_side_data(st, AV_PKT_DATA_SPHERICAL,
  6672. (uint8_t *)sc->spherical,
  6673. sc->spherical_size);
  6674. if (err < 0)
  6675. goto fail;
  6676. sc->spherical = NULL;
  6677. }
  6678. if (sc->mastering) {
  6679. err = av_stream_add_side_data(st, AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
  6680. (uint8_t *)sc->mastering,
  6681. sizeof(*sc->mastering));
  6682. if (err < 0)
  6683. goto fail;
  6684. sc->mastering = NULL;
  6685. }
  6686. if (sc->coll) {
  6687. err = av_stream_add_side_data(st, AV_PKT_DATA_CONTENT_LIGHT_LEVEL,
  6688. (uint8_t *)sc->coll,
  6689. sc->coll_size);
  6690. if (err < 0)
  6691. goto fail;
  6692. sc->coll = NULL;
  6693. }
  6694. break;
  6695. }
  6696. }
  6697. ff_configure_buffers_for_index(s, AV_TIME_BASE);
  6698. for (i = 0; i < mov->frag_index.nb_items; i++)
  6699. if (mov->frag_index.item[i].moof_offset <= mov->fragment.moof_offset)
  6700. mov->frag_index.item[i].headers_read = 1;
  6701. return 0;
  6702. fail:
  6703. mov_read_close(s);
  6704. return err;
  6705. }
  6706. static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
  6707. {
  6708. AVIndexEntry *sample = NULL;
  6709. int64_t best_dts = INT64_MAX;
  6710. int i;
  6711. for (i = 0; i < s->nb_streams; i++) {
  6712. AVStream *avst = s->streams[i];
  6713. MOVStreamContext *msc = avst->priv_data;
  6714. if (msc->pb && msc->current_sample < avst->nb_index_entries) {
  6715. AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
  6716. int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
  6717. av_log(s, AV_LOG_TRACE, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  6718. if (!sample || (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) && current_sample->pos < sample->pos) ||
  6719. ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
  6720. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb && dts != AV_NOPTS_VALUE &&
  6721. ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  6722. (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
  6723. sample = current_sample;
  6724. best_dts = dts;
  6725. *st = avst;
  6726. }
  6727. }
  6728. }
  6729. return sample;
  6730. }
  6731. static int should_retry(AVIOContext *pb, int error_code) {
  6732. if (error_code == AVERROR_EOF || avio_feof(pb))
  6733. return 0;
  6734. return 1;
  6735. }
  6736. static int mov_switch_root(AVFormatContext *s, int64_t target, int index)
  6737. {
  6738. int ret;
  6739. MOVContext *mov = s->priv_data;
  6740. if (index >= 0 && index < mov->frag_index.nb_items)
  6741. target = mov->frag_index.item[index].moof_offset;
  6742. if (avio_seek(s->pb, target, SEEK_SET) != target) {
  6743. av_log(mov->fc, AV_LOG_ERROR, "root atom offset 0x%"PRIx64": partial file\n", target);
  6744. return AVERROR_INVALIDDATA;
  6745. }
  6746. mov->next_root_atom = 0;
  6747. if (index < 0 || index >= mov->frag_index.nb_items)
  6748. index = search_frag_moof_offset(&mov->frag_index, target);
  6749. if (index < mov->frag_index.nb_items &&
  6750. mov->frag_index.item[index].moof_offset == target) {
  6751. if (index + 1 < mov->frag_index.nb_items)
  6752. mov->next_root_atom = mov->frag_index.item[index + 1].moof_offset;
  6753. if (mov->frag_index.item[index].headers_read)
  6754. return 0;
  6755. mov->frag_index.item[index].headers_read = 1;
  6756. }
  6757. mov->found_mdat = 0;
  6758. ret = mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX });
  6759. if (ret < 0)
  6760. return ret;
  6761. if (avio_feof(s->pb))
  6762. return AVERROR_EOF;
  6763. av_log(s, AV_LOG_TRACE, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
  6764. return 1;
  6765. }
  6766. static int mov_change_extradata(MOVStreamContext *sc, AVPacket *pkt)
  6767. {
  6768. uint8_t *side, *extradata;
  6769. int extradata_size;
  6770. /* Save the current index. */
  6771. sc->last_stsd_index = sc->stsc_data[sc->stsc_index].id - 1;
  6772. /* Notify the decoder that extradata changed. */
  6773. extradata_size = sc->extradata_size[sc->last_stsd_index];
  6774. extradata = sc->extradata[sc->last_stsd_index];
  6775. if (extradata_size > 0 && extradata) {
  6776. side = av_packet_new_side_data(pkt,
  6777. AV_PKT_DATA_NEW_EXTRADATA,
  6778. extradata_size);
  6779. if (!side)
  6780. return AVERROR(ENOMEM);
  6781. memcpy(side, extradata, extradata_size);
  6782. }
  6783. return 0;
  6784. }
  6785. static int get_eia608_packet(AVIOContext *pb, AVPacket *pkt, int size)
  6786. {
  6787. int new_size, ret;
  6788. if (size <= 8)
  6789. return AVERROR_INVALIDDATA;
  6790. new_size = ((size - 8) / 2) * 3;
  6791. ret = av_new_packet(pkt, new_size);
  6792. if (ret < 0)
  6793. return ret;
  6794. avio_skip(pb, 8);
  6795. for (int j = 0; j < new_size; j += 3) {
  6796. pkt->data[j] = 0xFC;
  6797. pkt->data[j+1] = avio_r8(pb);
  6798. pkt->data[j+2] = avio_r8(pb);
  6799. }
  6800. return 0;
  6801. }
  6802. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  6803. {
  6804. MOVContext *mov = s->priv_data;
  6805. MOVStreamContext *sc;
  6806. AVIndexEntry *sample;
  6807. AVStream *st = NULL;
  6808. int64_t current_index;
  6809. int ret;
  6810. mov->fc = s;
  6811. retry:
  6812. sample = mov_find_next_sample(s, &st);
  6813. if (!sample || (mov->next_root_atom && sample->pos > mov->next_root_atom)) {
  6814. if (!mov->next_root_atom)
  6815. return AVERROR_EOF;
  6816. if ((ret = mov_switch_root(s, mov->next_root_atom, -1)) < 0)
  6817. return ret;
  6818. goto retry;
  6819. }
  6820. sc = st->priv_data;
  6821. /* must be done just before reading, to avoid infinite loop on sample */
  6822. current_index = sc->current_index;
  6823. mov_current_sample_inc(sc);
  6824. if (mov->next_root_atom) {
  6825. sample->pos = FFMIN(sample->pos, mov->next_root_atom);
  6826. sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
  6827. }
  6828. if (st->discard != AVDISCARD_ALL) {
  6829. int64_t ret64 = avio_seek(sc->pb, sample->pos, SEEK_SET);
  6830. if (ret64 != sample->pos) {
  6831. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  6832. sc->ffindex, sample->pos);
  6833. if (should_retry(sc->pb, ret64)) {
  6834. mov_current_sample_dec(sc);
  6835. }
  6836. return AVERROR_INVALIDDATA;
  6837. }
  6838. if (st->discard == AVDISCARD_NONKEY && !(sample->flags & AVINDEX_KEYFRAME)) {
  6839. av_log(mov->fc, AV_LOG_DEBUG, "Nonkey frame from stream %d discarded due to AVDISCARD_NONKEY\n", sc->ffindex);
  6840. goto retry;
  6841. }
  6842. if (st->codecpar->codec_id == AV_CODEC_ID_EIA_608 && sample->size > 8)
  6843. ret = get_eia608_packet(sc->pb, pkt, sample->size);
  6844. else
  6845. ret = av_get_packet(sc->pb, pkt, sample->size);
  6846. if (ret < 0) {
  6847. if (should_retry(sc->pb, ret)) {
  6848. mov_current_sample_dec(sc);
  6849. }
  6850. return ret;
  6851. }
  6852. #if CONFIG_DV_DEMUXER
  6853. if (mov->dv_demux && sc->dv_audio_container) {
  6854. AVBufferRef *buf = pkt->buf;
  6855. ret = avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
  6856. pkt->buf = buf;
  6857. av_packet_unref(pkt);
  6858. if (ret < 0)
  6859. return ret;
  6860. ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
  6861. if (ret < 0)
  6862. return ret;
  6863. }
  6864. #endif
  6865. if (sc->has_palette) {
  6866. uint8_t *pal;
  6867. pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
  6868. if (!pal) {
  6869. av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
  6870. } else {
  6871. memcpy(pal, sc->palette, AVPALETTE_SIZE);
  6872. sc->has_palette = 0;
  6873. }
  6874. }
  6875. if (st->codecpar->codec_id == AV_CODEC_ID_MP3 && !st->need_parsing && pkt->size > 4) {
  6876. if (ff_mpa_check_header(AV_RB32(pkt->data)) < 0)
  6877. st->need_parsing = AVSTREAM_PARSE_FULL;
  6878. }
  6879. }
  6880. pkt->stream_index = sc->ffindex;
  6881. pkt->dts = sample->timestamp;
  6882. if (sample->flags & AVINDEX_DISCARD_FRAME) {
  6883. pkt->flags |= AV_PKT_FLAG_DISCARD;
  6884. }
  6885. if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
  6886. pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
  6887. /* update ctts context */
  6888. sc->ctts_sample++;
  6889. if (sc->ctts_index < sc->ctts_count &&
  6890. sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
  6891. sc->ctts_index++;
  6892. sc->ctts_sample = 0;
  6893. }
  6894. } else {
  6895. int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
  6896. st->index_entries[sc->current_sample].timestamp : st->duration;
  6897. if (next_dts >= pkt->dts)
  6898. pkt->duration = next_dts - pkt->dts;
  6899. pkt->pts = pkt->dts;
  6900. }
  6901. if (st->discard == AVDISCARD_ALL)
  6902. goto retry;
  6903. if (sc->sdtp_data && sc->current_sample <= sc->sdtp_count) {
  6904. uint8_t sample_flags = sc->sdtp_data[sc->current_sample - 1];
  6905. uint8_t sample_is_depended_on = (sample_flags >> 2) & 0x3;
  6906. pkt->flags |= sample_is_depended_on == MOV_SAMPLE_DEPENDENCY_NO ? AV_PKT_FLAG_DISPOSABLE : 0;
  6907. }
  6908. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
  6909. pkt->pos = sample->pos;
  6910. /* Multiple stsd handling. */
  6911. if (sc->stsc_data) {
  6912. /* Keep track of the stsc index for the given sample, then check
  6913. * if the stsd index is different from the last used one. */
  6914. sc->stsc_sample++;
  6915. if (mov_stsc_index_valid(sc->stsc_index, sc->stsc_count) &&
  6916. mov_get_stsc_samples(sc, sc->stsc_index) == sc->stsc_sample) {
  6917. sc->stsc_index++;
  6918. sc->stsc_sample = 0;
  6919. /* Do not check indexes after a switch. */
  6920. } else if (sc->stsc_data[sc->stsc_index].id > 0 &&
  6921. sc->stsc_data[sc->stsc_index].id - 1 < sc->stsd_count &&
  6922. sc->stsc_data[sc->stsc_index].id - 1 != sc->last_stsd_index) {
  6923. ret = mov_change_extradata(sc, pkt);
  6924. if (ret < 0)
  6925. return ret;
  6926. }
  6927. }
  6928. if (mov->aax_mode)
  6929. aax_filter(pkt->data, pkt->size, mov);
  6930. ret = cenc_filter(mov, st, sc, pkt, current_index);
  6931. if (ret < 0) {
  6932. return ret;
  6933. }
  6934. return 0;
  6935. }
  6936. static int mov_seek_fragment(AVFormatContext *s, AVStream *st, int64_t timestamp)
  6937. {
  6938. MOVContext *mov = s->priv_data;
  6939. int index;
  6940. if (!mov->frag_index.complete)
  6941. return 0;
  6942. index = search_frag_timestamp(&mov->frag_index, st, timestamp);
  6943. if (index < 0)
  6944. index = 0;
  6945. if (!mov->frag_index.item[index].headers_read)
  6946. return mov_switch_root(s, -1, index);
  6947. if (index + 1 < mov->frag_index.nb_items)
  6948. mov->next_root_atom = mov->frag_index.item[index + 1].moof_offset;
  6949. return 0;
  6950. }
  6951. static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
  6952. {
  6953. MOVStreamContext *sc = st->priv_data;
  6954. int sample, time_sample, ret;
  6955. unsigned int i;
  6956. // Here we consider timestamp to be PTS, hence try to offset it so that we
  6957. // can search over the DTS timeline.
  6958. timestamp -= (sc->min_corrected_pts + sc->dts_shift);
  6959. ret = mov_seek_fragment(s, st, timestamp);
  6960. if (ret < 0)
  6961. return ret;
  6962. sample = av_index_search_timestamp(st, timestamp, flags);
  6963. av_log(s, AV_LOG_TRACE, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  6964. if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
  6965. sample = 0;
  6966. if (sample < 0) /* not sure what to do */
  6967. return AVERROR_INVALIDDATA;
  6968. mov_current_sample_set(sc, sample);
  6969. av_log(s, AV_LOG_TRACE, "stream %d, found sample %d\n", st->index, sc->current_sample);
  6970. /* adjust ctts index */
  6971. if (sc->ctts_data) {
  6972. time_sample = 0;
  6973. for (i = 0; i < sc->ctts_count; i++) {
  6974. int next = time_sample + sc->ctts_data[i].count;
  6975. if (next > sc->current_sample) {
  6976. sc->ctts_index = i;
  6977. sc->ctts_sample = sc->current_sample - time_sample;
  6978. break;
  6979. }
  6980. time_sample = next;
  6981. }
  6982. }
  6983. /* adjust stsd index */
  6984. if (sc->chunk_count) {
  6985. time_sample = 0;
  6986. for (i = 0; i < sc->stsc_count; i++) {
  6987. int64_t next = time_sample + mov_get_stsc_samples(sc, i);
  6988. if (next > sc->current_sample) {
  6989. sc->stsc_index = i;
  6990. sc->stsc_sample = sc->current_sample - time_sample;
  6991. break;
  6992. }
  6993. av_assert0(next == (int)next);
  6994. time_sample = next;
  6995. }
  6996. }
  6997. return sample;
  6998. }
  6999. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  7000. {
  7001. MOVContext *mc = s->priv_data;
  7002. AVStream *st;
  7003. int sample;
  7004. int i;
  7005. if (stream_index >= s->nb_streams)
  7006. return AVERROR_INVALIDDATA;
  7007. st = s->streams[stream_index];
  7008. sample = mov_seek_stream(s, st, sample_time, flags);
  7009. if (sample < 0)
  7010. return sample;
  7011. if (mc->seek_individually) {
  7012. /* adjust seek timestamp to found sample timestamp */
  7013. int64_t seek_timestamp = st->index_entries[sample].timestamp;
  7014. for (i = 0; i < s->nb_streams; i++) {
  7015. int64_t timestamp;
  7016. MOVStreamContext *sc = s->streams[i]->priv_data;
  7017. st = s->streams[i];
  7018. st->skip_samples = (sample_time <= 0) ? sc->start_pad : 0;
  7019. if (stream_index == i)
  7020. continue;
  7021. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  7022. mov_seek_stream(s, st, timestamp, flags);
  7023. }
  7024. } else {
  7025. for (i = 0; i < s->nb_streams; i++) {
  7026. MOVStreamContext *sc;
  7027. st = s->streams[i];
  7028. sc = st->priv_data;
  7029. mov_current_sample_set(sc, 0);
  7030. }
  7031. while (1) {
  7032. MOVStreamContext *sc;
  7033. AVIndexEntry *entry = mov_find_next_sample(s, &st);
  7034. if (!entry)
  7035. return AVERROR_INVALIDDATA;
  7036. sc = st->priv_data;
  7037. if (sc->ffindex == stream_index && sc->current_sample == sample)
  7038. break;
  7039. mov_current_sample_inc(sc);
  7040. }
  7041. }
  7042. return 0;
  7043. }
  7044. #define OFFSET(x) offsetof(MOVContext, x)
  7045. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  7046. static const AVOption mov_options[] = {
  7047. {"use_absolute_path",
  7048. "allow using absolute path when opening alias, this is a possible security issue",
  7049. OFFSET(use_absolute_path), AV_OPT_TYPE_BOOL, {.i64 = 0},
  7050. 0, 1, FLAGS},
  7051. {"seek_streams_individually",
  7052. "Seek each stream individually to the closest point",
  7053. OFFSET(seek_individually), AV_OPT_TYPE_BOOL, { .i64 = 1 },
  7054. 0, 1, FLAGS},
  7055. {"ignore_editlist", "Ignore the edit list atom.", OFFSET(ignore_editlist), AV_OPT_TYPE_BOOL, {.i64 = 0},
  7056. 0, 1, FLAGS},
  7057. {"advanced_editlist",
  7058. "Modify the AVIndex according to the editlists. Use this option to decode in the order specified by the edits.",
  7059. OFFSET(advanced_editlist), AV_OPT_TYPE_BOOL, {.i64 = 1},
  7060. 0, 1, FLAGS},
  7061. {"ignore_chapters", "", OFFSET(ignore_chapters), AV_OPT_TYPE_BOOL, {.i64 = 0},
  7062. 0, 1, FLAGS},
  7063. {"use_mfra_for",
  7064. "use mfra for fragment timestamps",
  7065. OFFSET(use_mfra_for), AV_OPT_TYPE_INT, {.i64 = FF_MOV_FLAG_MFRA_AUTO},
  7066. -1, FF_MOV_FLAG_MFRA_PTS, FLAGS,
  7067. "use_mfra_for"},
  7068. {"auto", "auto", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_AUTO}, 0, 0,
  7069. FLAGS, "use_mfra_for" },
  7070. {"dts", "dts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_DTS}, 0, 0,
  7071. FLAGS, "use_mfra_for" },
  7072. {"pts", "pts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_PTS}, 0, 0,
  7073. FLAGS, "use_mfra_for" },
  7074. { "export_all", "Export unrecognized metadata entries", OFFSET(export_all),
  7075. AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
  7076. { "export_xmp", "Export full XMP metadata", OFFSET(export_xmp),
  7077. AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
  7078. { "activation_bytes", "Secret bytes for Audible AAX files", OFFSET(activation_bytes),
  7079. AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
  7080. { "audible_fixed_key", // extracted from libAAX_SDK.so and AAXSDKWin.dll files!
  7081. "Fixed key used for handling Audible AAX files", OFFSET(audible_fixed_key),
  7082. AV_OPT_TYPE_BINARY, {.str="77214d4b196a87cd520045fd20a51d67"},
  7083. .flags = AV_OPT_FLAG_DECODING_PARAM },
  7084. { "decryption_key", "The media decryption key (hex)", OFFSET(decryption_key), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
  7085. { "enable_drefs", "Enable external track support.", OFFSET(enable_drefs), AV_OPT_TYPE_BOOL,
  7086. {.i64 = 0}, 0, 1, FLAGS },
  7087. { NULL },
  7088. };
  7089. static const AVClass mov_class = {
  7090. .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
  7091. .item_name = av_default_item_name,
  7092. .option = mov_options,
  7093. .version = LIBAVUTIL_VERSION_INT,
  7094. };
  7095. AVInputFormat ff_mov_demuxer = {
  7096. .name = "mov,mp4,m4a,3gp,3g2,mj2",
  7097. .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
  7098. .priv_class = &mov_class,
  7099. .priv_data_size = sizeof(MOVContext),
  7100. .extensions = "mov,mp4,m4a,3gp,3g2,mj2,psp,m4b,ism,ismv,isma,f4v",
  7101. .read_probe = mov_probe,
  7102. .read_header = mov_read_header,
  7103. .read_packet = mov_read_packet,
  7104. .read_close = mov_read_close,
  7105. .read_seek = mov_read_seek,
  7106. .flags = AVFMT_NO_BYTE_SEEK | AVFMT_SEEK_TO_PTS,
  7107. };