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.

4452 lines
150KB

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