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.

4560 lines
153KB

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