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.

8193 lines
282KB

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